Created
July 25, 2011 19:37
-
-
Save jvoorhis/1104984 to your computer and use it in GitHub Desktop.
function pointers with ruby-llvm
This file contains hidden or 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' | |
require 'llvm/transforms/scalar' | |
LLVM.init_x86 | |
mod = LLVM::Module.new("Function Pointers") | |
F = LLVM::Function([LLVM::Int], LLVM::Int) | |
FP = LLVM::Pointer(F) | |
mod.functions.add("f", [LLVM::Int], LLVM::Int) do |f, n| | |
f.basic_blocks.append("entry").build do |b| | |
b.ret(b.add(LLVM::Int(1), n)) | |
end | |
end | |
mod.functions.add("g", [FP, LLVM::Int], LLVM::Int) do |g, fp, n| | |
g.basic_blocks.append("entry").build do |b| | |
b.ret(b.call(fp, n)) | |
end | |
end | |
mod.functions.add("test", [], LLVM::Int) do |test| | |
test.basic_blocks.append("entry").build do |b| | |
b.ret(b.call(mod.functions["g"], | |
mod.functions["f"], | |
LLVM::Int(41))) | |
end | |
end | |
mod.verify | |
mod.dump | |
jit = LLVM::JITCompiler.new(mod) | |
puts jit.run_function(mod.functions["test"]).to_i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment