Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created May 4, 2014 02:23
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 ishikawa/a7b456d720c65e075751 to your computer and use it in GitHub Desktop.
Save ishikawa/a7b456d720c65e075751 to your computer and use it in GitHub Desktop.
How to link modules and JIT execution by using ruby-llvm
#include <stdio.h>
int putd(double x) {
printf("putd: %.2f\n", x);
return 1;
}
#include <stdio.h>
extern int putd(double x);
int main(void) {
int v = putd(10.5);
printf("v = %d\n", v);
return 0;
}
require 'llvm/core'
require 'llvm/linker'
require 'llvm/execution_engine'
# Initialize JIT
LLVM.init_jit
# Parse .bc files into modules
putd_mod = LLVM::Module.parse_bitcode(File.expand_path("./putd.bc", File.dirname(__FILE__)))
main_mod = LLVM::Module.parse_bitcode(File.expand_path("./test.bc", File.dirname(__FILE__)))
# Verify and dump
putd_mod.verify!
main_mod.verify!
putd_mod.dump
main_mod.dump
# Linking
putd_mod.link_into_and_destroy(main_mod)
# Execute
engine = LLVM::JITCompiler.new(main_mod)
engine.run_function(main_mod.functions["main"])
engine.dispose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment