]> git.lizzy.rs Git - rust.git/blob - src/main_shim.rs
Sync from rust 3e826bb11228508fbe749e594038d6727208aa94
[rust.git] / src / main_shim.rs
1 use rustc_hir::LangItem;
2 use rustc_session::config::EntryFnType;
3
4 use crate::prelude::*;
5
6 /// Create the `main` function which will initialize the rust runtime and call
7 /// users main function.
8 pub(crate) fn maybe_create_entry_wrapper(
9     tcx: TyCtxt<'_>,
10     module: &mut impl Module,
11     unwind_context: &mut UnwindContext<'_>,
12 ) {
13     let (main_def_id, use_start_lang_item) = match tcx.entry_fn(LOCAL_CRATE) {
14         Some((def_id, entry_ty)) => (
15             def_id.to_def_id(),
16             match entry_ty {
17                 EntryFnType::Main => true,
18                 EntryFnType::Start => false,
19             },
20         ),
21         None => return,
22     };
23
24     let instance = Instance::mono(tcx, main_def_id).polymorphize(tcx);
25     if module.get_name(&*tcx.symbol_name(instance).name).is_none() {
26         return;
27     }
28
29     create_entry_fn(
30         tcx,
31         module,
32         unwind_context,
33         main_def_id,
34         use_start_lang_item,
35     );
36
37     fn create_entry_fn(
38         tcx: TyCtxt<'_>,
39         m: &mut impl Module,
40         unwind_context: &mut UnwindContext<'_>,
41         rust_main_def_id: DefId,
42         use_start_lang_item: bool,
43     ) {
44         let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
45         // Given that `main()` has no arguments,
46         // then its return type cannot have
47         // late-bound regions, since late-bound
48         // regions must appear in the argument
49         // listing.
50         let main_ret_ty = tcx.erase_regions(main_ret_ty.no_bound_vars().unwrap());
51
52         let cmain_sig = Signature {
53             params: vec![
54                 AbiParam::new(m.target_config().pointer_type()),
55                 AbiParam::new(m.target_config().pointer_type()),
56             ],
57             returns: vec![AbiParam::new(
58                 m.target_config().pointer_type(), /*isize*/
59             )],
60             call_conv: CallConv::triple_default(m.isa().triple()),
61         };
62
63         let cmain_func_id = m
64             .declare_function("main", Linkage::Export, &cmain_sig)
65             .unwrap();
66
67         let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx);
68
69         let main_name = tcx.symbol_name(instance).name.to_string();
70         let main_sig = get_function_sig(tcx, m.isa().triple(), instance);
71         let main_func_id = m
72             .declare_function(&main_name, Linkage::Import, &main_sig)
73             .unwrap();
74
75         let mut ctx = Context::new();
76         ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig);
77         {
78             let mut func_ctx = FunctionBuilderContext::new();
79             let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
80
81             let block = bcx.create_block();
82             bcx.switch_to_block(block);
83             let arg_argc = bcx.append_block_param(block, m.target_config().pointer_type());
84             let arg_argv = bcx.append_block_param(block, m.target_config().pointer_type());
85
86             let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
87
88             let call_inst = if use_start_lang_item {
89                 let start_def_id = tcx.require_lang_item(LangItem::Start, None);
90                 let start_instance = Instance::resolve(
91                     tcx,
92                     ParamEnv::reveal_all(),
93                     start_def_id,
94                     tcx.intern_substs(&[main_ret_ty.into()]),
95                 )
96                 .unwrap()
97                 .unwrap()
98                 .polymorphize(tcx);
99                 let start_func_id = import_function(tcx, m, start_instance);
100
101                 let main_val = bcx
102                     .ins()
103                     .func_addr(m.target_config().pointer_type(), main_func_ref);
104
105                 let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
106                 bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv])
107             } else {
108                 // using user-defined start fn
109                 bcx.ins().call(main_func_ref, &[arg_argc, arg_argv])
110             };
111
112             let result = bcx.inst_results(call_inst)[0];
113             bcx.ins().return_(&[result]);
114             bcx.seal_all_blocks();
115             bcx.finalize();
116         }
117         m.define_function(
118             cmain_func_id,
119             &mut ctx,
120             &mut cranelift_codegen::binemit::NullTrapSink {},
121         )
122         .unwrap();
123         unwind_context.add_function(cmain_func_id, &ctx, m.isa());
124     }
125 }