2 * LLVM ABI-level stuff that needs to happen after modules have been
10 (asm_glue:Llasm.asm_glue)
11 (exit_task_glue:Llvm.llvalue)
12 (crate_ptr:Llvm.llvalue)
14 let i32 = Llvm.i32_type llctx in
17 * Count the number of Rust functions and the number of C functions by
18 * simply (and crudely) testing whether each function in the module begins
22 let (rust_fn_count, c_fn_count) =
23 let count (rust_fn_count, c_fn_count) fn =
24 let begins_with prefix str =
25 let (str_len, prefix_len) =
26 (String.length str, String.length prefix)
28 prefix_len <= str_len && (String.sub str 0 prefix_len) = prefix
30 if begins_with "_rust_" (Llvm.value_name fn) then
31 (rust_fn_count + 1, c_fn_count)
33 (rust_fn_count, c_fn_count + 1)
35 Llvm.fold_left_functions count (0, 0) llmod
39 let crate_addr = Llvm.const_ptrtoint crate_ptr i32 in
41 let addr = Llvm.const_ptrtoint glue i32 in
42 Llvm.const_sub addr crate_addr
44 let activate_glue_off = glue_off asm_glue.Llasm.asm_activate_glue in
45 let yield_glue_off = glue_off asm_glue.Llasm.asm_yield_glue in
46 let exit_task_glue_off = glue_off exit_task_glue in
48 Llvm.const_struct llctx [|
49 Llvm.const_int i32 0; (* ptrdiff_t image_base_off *)
50 crate_ptr; (* uintptr_t self_addr *)
51 Llvm.const_int i32 0; (* ptrdiff_t debug_abbrev_off *)
52 Llvm.const_int i32 0; (* size_t debug_abbrev_sz *)
53 Llvm.const_int i32 0; (* ptrdiff_t debug_info_off *)
54 Llvm.const_int i32 0; (* size_t debug_info_sz *)
55 activate_glue_off; (* size_t activate_glue_off *)
56 exit_task_glue_off; (* size_t main_exit_task_glue_off *)
57 Llvm.const_int i32 0; (* size_t unwind_glue_off *)
58 yield_glue_off; (* size_t yield_glue_off *)
59 Llvm.const_int i32 rust_fn_count; (* int n_rust_syms *)
60 Llvm.const_int i32 c_fn_count; (* int n_c_syms *)
61 Llvm.const_int i32 0 (* int n_libs *)
65 Llvm.set_initializer crate_val crate_ptr;
67 (* Define the main function for crt0 to call. *)
69 let main_ty = Llvm.function_type i32 [| i32; i32 |] in
70 Llvm.define_function "main" main_ty llmod
72 let argc = Llvm.param main_fn 0 in
73 let argv = Llvm.param main_fn 1 in
74 let main_builder = Llvm.builder_at_end llctx (Llvm.entry_block main_fn) in
76 match Llvm.lookup_function "_rust_main" llmod with
77 None -> raise (Failure "no main function found")
80 let rust_start = abi.Llabi.rust_start in
81 let rust_start_args = [| rust_main_fn; crate_ptr; argc; argv |] in
82 ignore (Llvm.build_call
83 rust_start rust_start_args "start_rust" main_builder);
84 ignore (Llvm.build_ret (Llvm.const_int i32 0) main_builder)
91 * indent-tabs-mode: nil
92 * buffer-file-coding-system: utf-8-unix
93 * compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";