]> git.lizzy.rs Git - rust.git/blob - src/main_shim.rs
Rustup to rustc 1.44.0-nightly (1edd389cc 2020-03-23)
[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     let instance = Instance::mono(tcx, main_def_id);
21     if module.get_name(&*tcx.symbol_name(instance).name.as_str()).is_none() {
22         return;
23     }
24
25     create_entry_fn(tcx, module, main_def_id, use_start_lang_item);
26
27     fn create_entry_fn(
28         tcx: TyCtxt<'_>,
29         m: &mut Module<impl Backend + 'static>,
30         rust_main_def_id: DefId,
31         use_start_lang_item: bool,
32     ) {
33         let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
34         // Given that `main()` has no arguments,
35         // then its return type cannot have
36         // late-bound regions, since late-bound
37         // regions must appear in the argument
38         // listing.
39         let main_ret_ty = tcx.erase_regions(&main_ret_ty.no_bound_vars().unwrap());
40
41         let cmain_sig = Signature {
42             params: vec![
43                 AbiParam::new(m.target_config().pointer_type()),
44                 AbiParam::new(m.target_config().pointer_type()),
45             ],
46             returns: vec![AbiParam::new(
47                 m.target_config().pointer_type(), /*isize*/
48             )],
49             call_conv: CallConv::triple_default(m.isa().triple()),
50         };
51
52         let cmain_func_id = m
53             .declare_function("main", Linkage::Export, &cmain_sig)
54             .unwrap();
55
56         let instance = Instance::mono(tcx, rust_main_def_id);
57
58         let (main_name, main_sig) =
59             get_function_name_and_sig(tcx, m.isa().triple(), instance, false);
60         let main_func_id = m
61             .declare_function(&main_name, Linkage::Import, &main_sig)
62             .unwrap();
63
64         let mut ctx = Context::new();
65         ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig.clone());
66         {
67             let mut func_ctx = FunctionBuilderContext::new();
68             let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
69
70             let block = bcx.create_block();
71             bcx.switch_to_block(block);
72             let arg_argc = bcx.append_block_param(block, m.target_config().pointer_type());
73             let arg_argv = bcx.append_block_param(block, m.target_config().pointer_type());
74
75             crate::atomic_shim::init_global_lock(m, &mut bcx);
76
77             let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
78
79             let call_inst = if use_start_lang_item {
80                 let start_def_id = tcx.require_lang_item(StartFnLangItem, None);
81                 let start_instance = Instance::resolve(
82                     tcx,
83                     ParamEnv::reveal_all(),
84                     start_def_id,
85                     tcx.intern_substs(&[main_ret_ty.into()]),
86                 )
87                 .unwrap();
88                 let start_func_id = import_function(tcx, m, start_instance);
89
90                 let main_val = bcx
91                     .ins()
92                     .func_addr(m.target_config().pointer_type(), main_func_ref);
93
94                 let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
95                 bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv])
96             } else {
97                 // using user-defined start fn
98                 bcx.ins().call(main_func_ref, &[arg_argc, arg_argv])
99             };
100
101             let result = bcx.inst_results(call_inst)[0];
102             bcx.ins().return_(&[result]);
103             bcx.seal_all_blocks();
104             bcx.finalize();
105         }
106         m.define_function(cmain_func_id, &mut ctx).unwrap();
107     }
108 }