]> git.lizzy.rs Git - rust.git/blob - src/main_shim.rs
Rustup to rustc 1.37.0-nightly (0dc9e9c10 2019-06-15)
[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(
6     tcx: TyCtxt<'_>,
7     module: &mut Module<impl Backend + 'static>,
8 ) {
9     use rustc::middle::lang_items::StartFnLangItem;
10     use rustc::session::config::EntryFnType;
11
12     let (main_def_id, use_start_lang_item) = match tcx.entry_fn(LOCAL_CRATE) {
13         Some((def_id, entry_ty)) => (
14             def_id,
15             match entry_ty {
16                 EntryFnType::Main => true,
17                 EntryFnType::Start => false,
18             },
19         ),
20         None => return,
21     };
22
23     create_entry_fn(tcx, module, main_def_id, use_start_lang_item);;
24
25     fn create_entry_fn(
26         tcx: TyCtxt<'_>,
27         m: &mut Module<impl Backend + 'static>,
28         rust_main_def_id: DefId,
29         use_start_lang_item: bool,
30     ) {
31         let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
32         // Given that `main()` has no arguments,
33         // then its return type cannot have
34         // late-bound regions, since late-bound
35         // regions must appear in the argument
36         // listing.
37         let main_ret_ty = tcx.erase_regions(&main_ret_ty.no_bound_vars().unwrap());
38
39         let cmain_sig = Signature {
40             params: vec![
41                 AbiParam::new(m.target_config().pointer_type()),
42                 AbiParam::new(m.target_config().pointer_type()),
43             ],
44             returns: vec![AbiParam::new(
45                 m.target_config().pointer_type(), /*isize*/
46             )],
47             call_conv: CallConv::SystemV,
48         };
49
50         let cmain_func_id = m
51             .declare_function("main", Linkage::Export, &cmain_sig)
52             .unwrap();
53
54         let instance = Instance::mono(tcx, rust_main_def_id);
55
56         let (main_name, main_sig) = get_function_name_and_sig(tcx, instance, false);
57         let main_func_id = m
58             .declare_function(&main_name, Linkage::Import, &main_sig)
59             .unwrap();
60
61         let mut ctx = Context::new();
62         ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig.clone());
63         {
64             let mut func_ctx = FunctionBuilderContext::new();
65             let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
66
67             let ebb = bcx.create_ebb();
68             bcx.switch_to_block(ebb);
69             let arg_argc = bcx.append_ebb_param(ebb, m.target_config().pointer_type());
70             let arg_argv = bcx.append_ebb_param(ebb, m.target_config().pointer_type());
71
72             let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
73
74             let call_inst = if use_start_lang_item {
75                 let start_def_id = tcx.require_lang_item(StartFnLangItem);
76                 let start_instance = Instance::resolve(
77                     tcx,
78                     ParamEnv::reveal_all(),
79                     start_def_id,
80                     tcx.intern_substs(&[main_ret_ty.into()]),
81                 )
82                 .unwrap();
83                 let start_func_id = import_function(tcx, m, start_instance);
84
85                 let main_val = bcx
86                     .ins()
87                     .func_addr(m.target_config().pointer_type(), main_func_ref);
88
89                 let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
90                 bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv])
91             } else {
92                 // using user-defined start fn
93                 bcx.ins().call(main_func_ref, &[arg_argc, arg_argv])
94             };
95
96             let result = bcx.inst_results(call_inst)[0];
97             bcx.ins().return_(&[result]);
98             bcx.seal_all_blocks();
99             bcx.finalize();
100         }
101         m.define_function(cmain_func_id, &mut ctx).unwrap();
102     }
103 }