-
-
Save zehnpaard/3ade589b7171dba6da96991c9eb19711 to your computer and use it in GitHub Desktop.
OCaml LLVM JIT minimal setup
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
(executable | |
(name llvmjit) | |
(libraries | |
llvm | |
llvm.executionengine | |
ctypes.foreign)) |
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
module L = Llvm | |
let context = L.global_context () | |
let the_module = L.create_module context "my module" | |
let builder = L.builder context | |
let double_type = L.double_type context | |
let top_func_type = L.function_type double_type [||] | |
let the_function = L.declare_function "__toplevel1" top_func_type the_module | |
let bb = L.append_block context "entry" the_function | |
let () = L.position_at_end bb builder | |
let n = L.const_float double_type 1.0 | |
let m = L.const_float double_type 2.0 | |
let ret_val = L.build_add n m "addtmp" builder | |
let () = ignore @@ L.build_ret ret_val builder | |
let _ = Llvm_executionengine.initialize () | |
let the_execution_engine = Llvm_executionengine.create the_module | |
let () = L.dump_value the_function | |
let fp = | |
Llvm_executionengine.get_function_address | |
function_name | |
(Foreign.funptr Ctypes.(void @-> returning double)) | |
the_execution_engine | |
let () = print_endline @@ string_of_float @@ fp () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment