]> git.lizzy.rs Git - rust.git/blob - src/main_shim.rs
Sync from rust 03a8cc7df1d65554a4d40825b0490c93ac0f0236
[rust.git] / src / main_shim.rs
1 use rustc_hir::LangItem;
2 use rustc_middle::ty::subst::GenericArg;
3 use rustc_middle::ty::AssocKind;
4 use rustc_session::config::EntryFnType;
5 use rustc_span::symbol::Ident;
6
7 use crate::prelude::*;
8
9 /// Create the `main` function which will initialize the rust runtime and call
10 /// users main function.
11 pub(crate) fn maybe_create_entry_wrapper(
12     tcx: TyCtxt<'_>,
13     module: &mut impl Module,
14     unwind_context: &mut UnwindContext,
15     is_jit: bool,
16     is_primary_cgu: bool,
17 ) {
18     let (main_def_id, is_main_fn) = match tcx.entry_fn(()) {
19         Some((def_id, entry_ty)) => (
20             def_id,
21             match entry_ty {
22                 EntryFnType::Main => true,
23                 EntryFnType::Start => false,
24             },
25         ),
26         None => return,
27     };
28
29     if main_def_id.is_local() {
30         let instance = Instance::mono(tcx, main_def_id).polymorphize(tcx);
31         if !is_jit && module.get_name(&*tcx.symbol_name(instance).name).is_none() {
32             return;
33         }
34     } else if !is_primary_cgu {
35         return;
36     }
37
38     create_entry_fn(tcx, module, unwind_context, main_def_id, is_jit, is_main_fn);
39
40     fn create_entry_fn(
41         tcx: TyCtxt<'_>,
42         m: &mut impl Module,
43         unwind_context: &mut UnwindContext,
44         rust_main_def_id: DefId,
45         ignore_lang_start_wrapper: bool,
46         is_main_fn: bool,
47     ) {
48         let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
49         // Given that `main()` has no arguments,
50         // then its return type cannot have
51         // late-bound regions, since late-bound
52         // regions must appear in the argument
53         // listing.
54         let main_ret_ty = tcx.erase_regions(main_ret_ty.no_bound_vars().unwrap());
55
56         let cmain_sig = Signature {
57             params: vec![
58                 AbiParam::new(m.target_config().pointer_type()),
59                 AbiParam::new(m.target_config().pointer_type()),
60             ],
61             returns: vec![AbiParam::new(m.target_config().pointer_type() /*isize*/)],
62             call_conv: CallConv::triple_default(m.isa().triple()),
63         };
64
65         let cmain_func_id = m.declare_function("main", Linkage::Export, &cmain_sig).unwrap();
66
67         let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx);
68
69         let main_name = tcx.symbol_name(instance).name;
70         let main_sig = get_function_sig(tcx, m.isa().triple(), instance);
71         let main_func_id = m.declare_function(main_name, Linkage::Import, &main_sig).unwrap();
72
73         let mut ctx = Context::new();
74         ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig);
75         {
76             let mut func_ctx = FunctionBuilderContext::new();
77             let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
78
79             let block = bcx.create_block();
80             bcx.switch_to_block(block);
81             let arg_argc = bcx.append_block_param(block, m.target_config().pointer_type());
82             let arg_argv = bcx.append_block_param(block, m.target_config().pointer_type());
83
84             let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
85
86             let result = if is_main_fn && ignore_lang_start_wrapper {
87                 // regular main fn, but ignoring #[lang = "start"] as we are running in the jit
88                 // FIXME set program arguments somehow
89                 let call_inst = bcx.ins().call(main_func_ref, &[]);
90                 let call_results = bcx.func.dfg.inst_results(call_inst).to_owned();
91
92                 let termination_trait = tcx.require_lang_item(LangItem::Termination, None);
93                 let report = tcx
94                     .associated_items(termination_trait)
95                     .find_by_name_and_kind(
96                         tcx,
97                         Ident::from_str("report"),
98                         AssocKind::Fn,
99                         termination_trait,
100                     )
101                     .unwrap();
102                 let report = Instance::resolve(
103                     tcx,
104                     ParamEnv::reveal_all(),
105                     report.def_id,
106                     tcx.mk_substs([GenericArg::from(main_ret_ty)].iter()),
107                 )
108                 .unwrap()
109                 .unwrap();
110
111                 let report_name = tcx.symbol_name(report).name;
112                 let report_sig = get_function_sig(tcx, m.isa().triple(), report);
113                 let report_func_id =
114                     m.declare_function(report_name, Linkage::Import, &report_sig).unwrap();
115                 let report_func_ref = m.declare_func_in_func(report_func_id, &mut bcx.func);
116
117                 // FIXME do proper abi handling instead of expecting the pass mode to be identical
118                 // for returns and arguments.
119                 let report_call_inst = bcx.ins().call(report_func_ref, &call_results);
120                 let res = bcx.func.dfg.inst_results(report_call_inst)[0];
121                 match m.target_config().pointer_type() {
122                     types::I32 => res,
123                     types::I64 => bcx.ins().sextend(types::I64, res),
124                     _ => unimplemented!("16bit systems are not yet supported"),
125                 }
126             } else if is_main_fn {
127                 let start_def_id = tcx.require_lang_item(LangItem::Start, None);
128                 let start_instance = Instance::resolve(
129                     tcx,
130                     ParamEnv::reveal_all(),
131                     start_def_id,
132                     tcx.intern_substs(&[main_ret_ty.into()]),
133                 )
134                 .unwrap()
135                 .unwrap()
136                 .polymorphize(tcx);
137                 let start_func_id = import_function(tcx, m, start_instance);
138
139                 let main_val = bcx.ins().func_addr(m.target_config().pointer_type(), main_func_ref);
140
141                 let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
142                 let call_inst = bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv]);
143                 bcx.inst_results(call_inst)[0]
144             } else {
145                 // using user-defined start fn
146                 let call_inst = bcx.ins().call(main_func_ref, &[arg_argc, arg_argv]);
147                 bcx.inst_results(call_inst)[0]
148             };
149
150             bcx.ins().return_(&[result]);
151             bcx.seal_all_blocks();
152             bcx.finalize();
153         }
154         m.define_function(cmain_func_id, &mut ctx).unwrap();
155         unwind_context.add_function(cmain_func_id, &ctx, m.isa());
156     }
157 }