]> git.lizzy.rs Git - rust.git/blob - src/main_shim.rs
Merge pull request #1159 from bjorn3/driver_refactorings
[rust.git] / 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     is_jit: bool,
14 ) {
15     let (main_def_id, is_main_fn) = match tcx.entry_fn(LOCAL_CRATE) {
16         Some((def_id, entry_ty)) => (
17             def_id.to_def_id(),
18             match entry_ty {
19                 EntryFnType::Main => true,
20                 EntryFnType::Start => false,
21             },
22         ),
23         None => return,
24     };
25
26     let instance = Instance::mono(tcx, main_def_id).polymorphize(tcx);
27     if !is_jit && module.get_name(&*tcx.symbol_name(instance).name).is_none() {
28         return;
29     }
30
31     create_entry_fn(tcx, module, unwind_context, main_def_id, is_jit, is_main_fn);
32
33     fn create_entry_fn(
34         tcx: TyCtxt<'_>,
35         m: &mut impl Module,
36         unwind_context: &mut UnwindContext,
37         rust_main_def_id: DefId,
38         ignore_lang_start_wrapper: bool,
39         is_main_fn: bool,
40     ) {
41         let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
42         // Given that `main()` has no arguments,
43         // then its return type cannot have
44         // late-bound regions, since late-bound
45         // regions must appear in the argument
46         // listing.
47         let main_ret_ty = tcx.erase_regions(main_ret_ty.no_bound_vars().unwrap());
48
49         let cmain_sig = Signature {
50             params: vec![
51                 AbiParam::new(m.target_config().pointer_type()),
52                 AbiParam::new(m.target_config().pointer_type()),
53             ],
54             returns: vec![AbiParam::new(m.target_config().pointer_type() /*isize*/)],
55             call_conv: CallConv::triple_default(m.isa().triple()),
56         };
57
58         let cmain_func_id = m.declare_function("main", Linkage::Export, &cmain_sig).unwrap();
59
60         let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx);
61
62         let main_name = tcx.symbol_name(instance).name.to_string();
63         let main_sig = get_function_sig(tcx, m.isa().triple(), instance);
64         let main_func_id = m.declare_function(&main_name, Linkage::Import, &main_sig).unwrap();
65
66         let mut ctx = Context::new();
67         ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig);
68         {
69             let mut func_ctx = FunctionBuilderContext::new();
70             let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
71
72             let block = bcx.create_block();
73             bcx.switch_to_block(block);
74             let arg_argc = bcx.append_block_param(block, m.target_config().pointer_type());
75             let arg_argv = bcx.append_block_param(block, m.target_config().pointer_type());
76
77             let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
78
79             let result = if is_main_fn && ignore_lang_start_wrapper {
80                 // regular main fn, but ignoring #[lang = "start"] as we are running in the jit
81                 // FIXME set program arguments somehow
82                 bcx.ins().call(main_func_ref, &[]);
83                 bcx.ins().iconst(m.target_config().pointer_type(), 0)
84             } else if is_main_fn {
85                 let start_def_id = tcx.require_lang_item(LangItem::Start, None);
86                 let start_instance = Instance::resolve(
87                     tcx,
88                     ParamEnv::reveal_all(),
89                     start_def_id,
90                     tcx.intern_substs(&[main_ret_ty.into()]),
91                 )
92                 .unwrap()
93                 .unwrap()
94                 .polymorphize(tcx);
95                 let start_func_id = import_function(tcx, m, start_instance);
96
97                 let main_val = bcx.ins().func_addr(m.target_config().pointer_type(), main_func_ref);
98
99                 let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
100                 let call_inst = bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv]);
101                 bcx.inst_results(call_inst)[0]
102             } else {
103                 // using user-defined start fn
104                 let call_inst = bcx.ins().call(main_func_ref, &[arg_argc, arg_argv]);
105                 bcx.inst_results(call_inst)[0]
106             };
107
108             bcx.ins().return_(&[result]);
109             bcx.seal_all_blocks();
110             bcx.finalize();
111         }
112         m.define_function(cmain_func_id, &mut ctx, &mut NullTrapSink {}, &mut NullStackMapSink {})
113             .unwrap();
114         unwind_context.add_function(cmain_func_id, &ctx, m.isa());
115     }
116 }