Skip to content

Instantly share code, notes, and snippets.

@zehnpaard
Created February 22, 2022 07:51
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 zehnpaard/3ade589b7171dba6da96991c9eb19711 to your computer and use it in GitHub Desktop.
Save zehnpaard/3ade589b7171dba6da96991c9eb19711 to your computer and use it in GitHub Desktop.
OCaml LLVM JIT minimal setup
(executable
(name llvmjit)
(libraries
llvm
llvm.executionengine
ctypes.foreign))
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