Class: Citrus::Compiler
- Inherits:
-
Object
- Object
- Citrus::Compiler
- Defined in:
- lib/citrus/compiler.rb
Instance Attribute Summary collapse
-
#generator ⇒ Object
readonly
Returns the value of attribute generator.
-
#module ⇒ Object
readonly
Returns the value of attribute module.
Instance Method Summary collapse
- #compile(file) ⇒ Object
- #finish ⇒ Object
-
#initialize ⇒ Compiler
constructor
A new instance of Compiler.
- #inspect ⇒ Object
- #optimize ⇒ Object
- #preamble ⇒ Object
- #run ⇒ Object
- #to_file(file) ⇒ Object
Constructor Details
#initialize ⇒ Compiler
Returns a new instance of Compiler.
16 17 18 19 20 21 22 23 |
# File 'lib/citrus/compiler.rb', line 16 def initialize @module = LLVM::Module.create("Citrus") GlobalVariables.init(@module) GlobalFunctions.init(@module) @function = @module.functions.add("main", LLVM::Type.function([INT, LLVM::Type.pointer(PCHAR)], INT)) @generator = Generator.new(@module, @function) GlobalStrings.init(@generator.builder) end |
Instance Attribute Details
#generator ⇒ Object (readonly)
Returns the value of attribute generator.
13 14 15 |
# File 'lib/citrus/compiler.rb', line 13 def generator @generator end |
#module ⇒ Object (readonly)
Returns the value of attribute module.
14 15 16 |
# File 'lib/citrus/compiler.rb', line 14 def module @module end |
Instance Method Details
#compile(file) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/citrus/compiler.rb', line 43 def compile(file) #bc = Tempfile.new("#{file}.bc", "/tmp") #as = Tempfile.new("#{file}.s", "/tmp") to_file("#{file}.ctc.bc") tool = LLVM.bin_path.empty? ? "llc" : File.join(LLVM.bin_path, "llc") %x[#{tool} #{file}.ctc.bc -o #{file}.ctc.s] %x[gcc #{file}.ctc.s -o #{file}] File.delete("#{file}.ctc.bc") File.delete("#{file}.ctc.s") #bc.close! #as.close! end |
#finish ⇒ Object
29 30 31 32 |
# File 'lib/citrus/compiler.rb', line 29 def finish @generator.builder.ret(INT.from_i(0)) @generator.finish end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/citrus/compiler.rb', line 60 def inspect @module.dump end |
#optimize ⇒ Object
56 57 58 |
# File 'lib/citrus/compiler.rb', line 56 def optimize PassManager.new(@engine).run(@module) unless @engine.nil? end |
#preamble ⇒ Object
25 26 27 |
# File 'lib/citrus/compiler.rb', line 25 def preamble Runtime.build_lib(@generator) end |
#run ⇒ Object
34 35 36 37 |
# File 'lib/citrus/compiler.rb', line 34 def run @engine = LLVM::ExecutionEngine.create_jit_compiler(@module) @engine.run_function(@function, 0, 0) end |
#to_file(file) ⇒ Object
39 40 41 |
# File 'lib/citrus/compiler.rb', line 39 def to_file(file) @module.write_bitcode(file) end |