3 /// Create the `main` function which will initialize the rust runtime and call
4 /// users main function.
5 pub(crate) fn maybe_create_entry_wrapper(
7 module: &mut Module<impl Backend + 'static>,
8 unwind_context: &mut UnwindContext<'_>,
10 use rustc_hir::lang_items::StartFnLangItem;
11 use rustc_session::config::EntryFnType;
13 let (main_def_id, use_start_lang_item) = match tcx.entry_fn(LOCAL_CRATE) {
14 Some((def_id, entry_ty)) => (
17 EntryFnType::Main => true,
18 EntryFnType::Start => false,
24 let instance = Instance::mono(tcx, main_def_id).polymorphize(tcx);
25 if module.get_name(&*tcx.symbol_name(instance).name).is_none() {
39 m: &mut Module<impl Backend + 'static>,
40 unwind_context: &mut UnwindContext<'_>,
41 rust_main_def_id: DefId,
42 use_start_lang_item: bool,
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
50 let main_ret_ty = tcx.erase_regions(&main_ret_ty.no_bound_vars().unwrap());
52 let cmain_sig = Signature {
54 AbiParam::new(m.target_config().pointer_type()),
55 AbiParam::new(m.target_config().pointer_type()),
57 returns: vec![AbiParam::new(
58 m.target_config().pointer_type(), /*isize*/
60 call_conv: CallConv::triple_default(m.isa().triple()),
64 .declare_function("main", Linkage::Export, &cmain_sig)
67 let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx);
69 let (main_name, main_sig) =
70 get_function_name_and_sig(tcx, m.isa().triple(), instance, false);
72 .declare_function(&main_name, Linkage::Import, &main_sig)
75 let mut ctx = Context::new();
76 ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig.clone());
78 let mut func_ctx = FunctionBuilderContext::new();
79 let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
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());
86 crate::atomic_shim::init_global_lock(m, &mut bcx);
88 let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
90 let call_inst = if use_start_lang_item {
91 let start_def_id = tcx.require_lang_item(StartFnLangItem, None);
92 let start_instance = Instance::resolve(
94 ParamEnv::reveal_all(),
96 tcx.intern_substs(&[main_ret_ty.into()]),
101 let start_func_id = import_function(tcx, m, start_instance);
105 .func_addr(m.target_config().pointer_type(), main_func_ref);
107 let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
108 bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv])
110 // using user-defined start fn
111 bcx.ins().call(main_func_ref, &[arg_argc, arg_argv])
114 let result = bcx.inst_results(call_inst)[0];
115 bcx.ins().return_(&[result]);
116 bcx.seal_all_blocks();
122 &mut cranelift_codegen::binemit::NullTrapSink {},
125 unwind_context.add_function(cmain_func_id, &ctx, m.isa());