Created
May 4, 2014 02:23
-
-
Save ishikawa/a7b456d720c65e075751 to your computer and use it in GitHub Desktop.
How to link modules and JIT execution by using ruby-llvm
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
#include <stdio.h> | |
int putd(double x) { | |
printf("putd: %.2f\n", x); | |
return 1; | |
} |
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
#include <stdio.h> | |
extern int putd(double x); | |
int main(void) { | |
int v = putd(10.5); | |
printf("v = %d\n", v); | |
return 0; | |
} |
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/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