2018-10-20 11:41:26 -05:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
/// Create the `main` function which will initialize the rust runtime and call
|
|
|
|
/// users main function.
|
2019-08-31 12:28:09 -05:00
|
|
|
pub fn maybe_create_entry_wrapper(tcx: TyCtxt<'_>, module: &mut Module<impl Backend + 'static>) {
|
2018-11-07 06:29:38 -06:00
|
|
|
use rustc::middle::lang_items::StartFnLangItem;
|
|
|
|
use rustc::session::config::EntryFnType;
|
2018-10-20 11:41:26 -05:00
|
|
|
|
2019-01-26 04:59:34 -06:00
|
|
|
let (main_def_id, use_start_lang_item) = match tcx.entry_fn(LOCAL_CRATE) {
|
|
|
|
Some((def_id, entry_ty)) => (
|
|
|
|
def_id,
|
2018-10-20 11:41:26 -05:00
|
|
|
match entry_ty {
|
|
|
|
EntryFnType::Main => true,
|
|
|
|
EntryFnType::Start => false,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2019-08-18 04:30:13 -05:00
|
|
|
create_entry_fn(tcx, module, main_def_id, use_start_lang_item);
|
2018-10-20 11:41:26 -05:00
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
fn create_entry_fn(
|
|
|
|
tcx: TyCtxt<'_>,
|
2018-10-20 11:41:26 -05:00
|
|
|
m: &mut Module<impl Backend + 'static>,
|
|
|
|
rust_main_def_id: DefId,
|
|
|
|
use_start_lang_item: bool,
|
|
|
|
) {
|
|
|
|
let main_ret_ty = tcx.fn_sig(rust_main_def_id).output();
|
|
|
|
// Given that `main()` has no arguments,
|
|
|
|
// then its return type cannot have
|
|
|
|
// late-bound regions, since late-bound
|
|
|
|
// regions must appear in the argument
|
|
|
|
// listing.
|
2018-11-03 12:22:05 -05:00
|
|
|
let main_ret_ty = tcx.erase_regions(&main_ret_ty.no_bound_vars().unwrap());
|
2018-10-20 11:41:26 -05:00
|
|
|
|
|
|
|
let cmain_sig = Signature {
|
|
|
|
params: vec![
|
2018-11-03 07:14:28 -05:00
|
|
|
AbiParam::new(m.target_config().pointer_type()),
|
|
|
|
AbiParam::new(m.target_config().pointer_type()),
|
2018-10-20 11:41:26 -05:00
|
|
|
],
|
2018-11-07 06:32:02 -06:00
|
|
|
returns: vec![AbiParam::new(
|
|
|
|
m.target_config().pointer_type(), /*isize*/
|
|
|
|
)],
|
2019-12-17 08:03:32 -06:00
|
|
|
call_conv: CallConv::triple_default(m.isa().triple()),
|
2018-10-20 11:41:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let cmain_func_id = m
|
|
|
|
.declare_function("main", Linkage::Export, &cmain_sig)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let instance = Instance::mono(tcx, rust_main_def_id);
|
|
|
|
|
2019-12-17 08:03:32 -06:00
|
|
|
let (main_name, main_sig) =
|
|
|
|
get_function_name_and_sig(tcx, m.isa().triple(), instance, false);
|
2018-10-20 11:41:26 -05:00
|
|
|
let main_func_id = m
|
|
|
|
.declare_function(&main_name, Linkage::Import, &main_sig)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut ctx = Context::new();
|
2019-09-28 10:59:27 -05:00
|
|
|
ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig.clone());
|
|
|
|
{
|
|
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
|
|
let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
2018-10-20 11:41:26 -05:00
|
|
|
|
2020-02-14 11:23:29 -06:00
|
|
|
let block = bcx.create_block();
|
|
|
|
bcx.switch_to_block(block);
|
|
|
|
let arg_argc = bcx.append_block_param(block, m.target_config().pointer_type());
|
|
|
|
let arg_argv = bcx.append_block_param(block, m.target_config().pointer_type());
|
2018-10-20 11:41:26 -05:00
|
|
|
|
2020-01-18 05:43:31 -06:00
|
|
|
crate::atomic_shim::init_global_lock(m, &mut bcx);
|
|
|
|
|
2018-10-20 11:41:26 -05:00
|
|
|
let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
|
|
|
|
|
|
|
|
let call_inst = if use_start_lang_item {
|
2019-08-30 04:42:42 -05:00
|
|
|
let start_def_id = tcx.require_lang_item(StartFnLangItem, None);
|
2018-10-20 11:41:26 -05:00
|
|
|
let start_instance = Instance::resolve(
|
|
|
|
tcx,
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
start_def_id,
|
|
|
|
tcx.intern_substs(&[main_ret_ty.into()]),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-01-02 05:20:32 -06:00
|
|
|
let start_func_id = import_function(tcx, m, start_instance);
|
2018-10-20 11:41:26 -05:00
|
|
|
|
2018-11-07 06:32:02 -06:00
|
|
|
let main_val = bcx
|
|
|
|
.ins()
|
|
|
|
.func_addr(m.target_config().pointer_type(), main_func_ref);
|
2018-10-20 11:41:26 -05:00
|
|
|
|
|
|
|
let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
|
|
|
|
bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv])
|
|
|
|
} else {
|
|
|
|
// using user-defined start fn
|
|
|
|
bcx.ins().call(main_func_ref, &[arg_argc, arg_argv])
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = bcx.inst_results(call_inst)[0];
|
|
|
|
bcx.ins().return_(&[result]);
|
|
|
|
bcx.seal_all_blocks();
|
2019-09-28 10:59:27 -05:00
|
|
|
bcx.finalize();
|
|
|
|
}
|
2018-10-20 11:41:26 -05:00
|
|
|
m.define_function(cmain_func_id, &mut ctx).unwrap();
|
|
|
|
}
|
|
|
|
}
|