Skip to content

Instantly share code, notes, and snippets.

@hackwaly
Created May 13, 2016 02:24
Show Gist options
  • Save hackwaly/ac785114a7d88ba03fef1084ae1c5454 to your computer and use it in GitHub Desktop.
Save hackwaly/ac785114a7d88ba03fef1084ae1c5454 to your computer and use it in GitHub Desktop.
ocaml + llvm
open Ctypes
open Foreign
let main () =
ignore (Llvm_executionengine.initialize ());
let ctx = Llvm.global_context () in
let m_main = Llvm.create_module ctx "hello" in
let builder = Llvm.builder ctx in
let t_i32 = Llvm.i32_type ctx in
let ft_main = Llvm.function_type t_i32 [||] in
let f_main = Llvm.declare_function "main" ft_main m_main in
let bb = Llvm.append_block ctx "entry" f_main in
Llvm.position_at_end bb builder;
ignore (Llvm.build_ret (Llvm.const_int t_i32 123456) builder);
Llvm_analysis.assert_valid_function f_main;
let ee = Llvm_executionengine.create m_main in
let ct = funptr ( void @-> returning int ) in
let f = Llvm_executionengine.get_function_address "main" ct ee in
let v = f () in
print_int v;
;;
main ()
open Ocamlbuild_plugin;;
tag_any ["package(ctypes, ctypes.foreign, llvm, llvm.executionengine, llvm.all_backends, llvm.analysis)"];;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment