Created
March 7, 2012 04:42
-
-
Save douglascamata/1990982 to your computer and use it in GitHub Desktop.
Code Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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