]> git.lizzy.rs Git - rust.git/blob - src/main_shim.rs
[OPT] Avoid calling target_triple for every call to codegen
[rust.git] / src / main_shim.rs
1 use crate::prelude::*;
2
3 /// Create the `main` function which will initialize the rust runtime and call
4 /// users main function.
5 pub fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<impl Backend + 'static>) {
6     use rustc::middle::lang_items::StartFnLangItem;
7     use rustc::session::config::EntryFnType;
8
9     let (main_def_id, use_start_lang_item) = match tcx.entry_fn(LOCAL_CRATE) {
10         Some((def_id, entry_ty)) => (
11             def_id,
12             match entry_ty {
13                 EntryFnType::Main => true,
14                 EntryFnType::Start => false,
15             },
16         ),
17         None => return,
18     };
19
20     create_entry_fn(tcx, module, main_def_id, use_start_lang_item);
21
22     fn create_entry_fn(
23         tcx: TyCtxt<'_>,
24         m: &mut Module<impl Backend + 'static>,
25         rust_main_def_id: DefId,
26         use_start_lang_item: bool,
27     ) {
28         let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
29         // Given that `main()` has no arguments,
30         // then its return type cannot have
31         // late-bound regions, since late-bound
32         // regions must appear in the argument
33         // listing.
34         let main_ret_ty = tcx.erase_regions(&main_ret_ty.no_bound_vars().unwrap());
35
36         let cmain_sig = Signature {
37             params: vec![
38                 AbiParam::new(m.target_config().pointer_type()),
39                 AbiParam::new(m.target_config().pointer_type()),
40             ],
41             returns: vec![AbiParam::new(
42                 m.target_config().pointer_type(), /*isize*/
43             )],
44             call_conv: CallConv::triple_default(m.isa().triple()),
45         };
46
47         let cmain_func_id = m
48             .declare_function("main", Linkage::Export, &cmain_sig)
49             .unwrap();
50
51         let instance = Instance::mono(tcx, rust_main_def_id);
52
53         let (main_name, main_sig) =
54             get_function_name_and_sig(tcx, m.isa().triple(), instance, false);
55         let main_func_id = m
56             .declare_function(&main_name, Linkage::Import, &main_sig)
57             .unwrap();
58
59         let mut ctx = Context::new();
60         ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig.clone());
61         {
62             let mut func_ctx = FunctionBuilderContext::new();
63             let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
64
65             let ebb = bcx.create_ebb();
66             bcx.switch_to_block(ebb);
67             let arg_argc = bcx.append_ebb_param(ebb, m.target_config().pointer_type());
68             let arg_argv = bcx.append_ebb_param(ebb, m.target_config().pointer_type());
69
70             let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
71
72             let call_inst = if use_start_lang_item {
73                 let start_def_id = tcx.require_lang_item(StartFnLangItem, None);
74                 let start_instance = Instance::resolve(
75                     tcx,
76                     ParamEnv::reveal_all(),
77                     start_def_id,
78                     tcx.intern_substs(&[main_ret_ty.into()]),
79                 )
80                 .unwrap();
81                 let start_func_id = import_function(tcx, m, start_instance);
82
83                 let main_val = bcx
84                     .ins()
85                     .func_addr(m.target_config().pointer_type(), main_func_ref);
86
87                 let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
88                 bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv])
89             } else {
90                 // using user-defined start fn
91                 bcx.ins().call(main_func_ref, &[arg_argc, arg_argv])
92             };
93
94             let result = bcx.inst_results(call_inst)[0];
95             bcx.ins().return_(&[result]);
96             bcx.seal_all_blocks();
97             bcx.finalize();
98         }
99         m.define_function(cmain_func_id, &mut ctx).unwrap();
100     }
101 }