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