]> git.lizzy.rs Git - rust.git/blob - src/boot/llvm/llfinal.ml
Populate tree.
[rust.git] / src / boot / llvm / llfinal.ml
1 (*
2  * LLVM ABI-level stuff that needs to happen after modules have been
3  * translated.
4  *)
5
6 let finalize_module
7     (llctx:Llvm.llcontext)
8     (llmod:Llvm.llmodule)
9     (abi:Llabi.abi)
10     (asm_glue:Llasm.asm_glue)
11     (exit_task_glue:Llvm.llvalue)
12     (crate_ptr:Llvm.llvalue)
13     : unit =
14   let i32 = Llvm.i32_type llctx in
15
16   (*
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
19    * with "_rust_".
20    *)
21
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)
27         in
28         prefix_len <= str_len && (String.sub str 0 prefix_len) = prefix
29       in
30       if begins_with "_rust_" (Llvm.value_name fn) then
31         (rust_fn_count + 1, c_fn_count)
32       else
33         (rust_fn_count, c_fn_count + 1)
34     in
35     Llvm.fold_left_functions count (0, 0) llmod
36   in
37
38   let crate_val =
39     let crate_addr = Llvm.const_ptrtoint crate_ptr i32 in
40     let glue_off glue =
41       let addr = Llvm.const_ptrtoint glue i32 in
42         Llvm.const_sub addr crate_addr
43     in
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
47
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 *)
62     |]
63   in
64
65   Llvm.set_initializer crate_val crate_ptr;
66
67   (* Define the main function for crt0 to call. *)
68   let main_fn =
69     let main_ty = Llvm.function_type i32 [| i32; i32 |] in
70     Llvm.define_function "main" main_ty llmod
71   in
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
75   let rust_main_fn =
76     match Llvm.lookup_function "_rust_main" llmod with
77         None -> raise (Failure "no main function found")
78       | Some fn -> fn
79   in
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)
85 ;;
86
87
88 (*
89  * Local Variables:
90  * fill-column: 78;
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'";
94  * End:
95  *)
96