Skip to content

Instantly share code, notes, and snippets.

@douglascamata
Created March 7, 2012 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglascamata/1990982 to your computer and use it in GitHub Desktop.
Save douglascamata/1990982 to your computer and use it in GitHub Desktop.
Code Generator
require 'llvm/core'
require 'llvm/execution_engine'
class Gerador
def initialize(syntax_tree)
@tree = syntax_tree
end
def compile
LLVM.init_x86
@module = LLVM::Module.new("pasc")
@builder = LLVM::Builder.new
@function = @module.functions.add("main", [LLVM::Int], LLVM::Int)
@basic_block = @function.basic_blocks.append
@builder.position_at_end(@basic_block)
@locals = Hash.new
generate_code
@builder.ret_void
p
@module.dump
@module.verify
# Execution objects.
@engine = LLVM::JITCompiler.new(@module)
@fpm = LLVM::FunctionPassManager.new(@engine, @module)
# Add passes to the Function Pass Manager.
@fpm << :instcombine
@fpm << :reassociate
@fpm << :gvn
@fpm << :simplifycfg
@function
end
def optimize(function)
@fpm.run(function)
end
private
def generate_code
@tree.generate_code @function, @locals, @builder, @basic_block
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment