Class: Citrus::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/citrus/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompiler

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

#generatorObject (readonly)

Returns the value of attribute generator.



13
14
15
# File 'lib/citrus/compiler.rb', line 13

def generator
  @generator
end

#moduleObject (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

#finishObject



29
30
31
32
# File 'lib/citrus/compiler.rb', line 29

def finish
  @generator.builder.ret(INT.from_i(0))
  @generator.finish
end

#inspectObject



60
61
62
# File 'lib/citrus/compiler.rb', line 60

def inspect
  @module.dump
end

#optimizeObject



56
57
58
# File 'lib/citrus/compiler.rb', line 56

def optimize
  PassManager.new(@engine).run(@module) unless @engine.nil?
end

#preambleObject



25
26
27
# File 'lib/citrus/compiler.rb', line 25

def preamble
  Runtime.build_lib(@generator)
end

#runObject



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