2024-04-21 12:35:09 -05:00
|
|
|
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
|
2020-09-05 03:38:49 -05:00
|
|
|
use rustc_hir::LangItem;
|
2021-04-30 07:49:58 -05:00
|
|
|
use rustc_middle::ty::AssocKind;
|
2023-07-11 16:35:29 -05:00
|
|
|
use rustc_middle::ty::GenericArg;
|
2022-07-05 12:56:22 -05:00
|
|
|
use rustc_session::config::{sigpipe, EntryFnType};
|
2021-04-30 07:49:58 -05:00
|
|
|
use rustc_span::symbol::Ident;
|
2020-09-05 03:38:49 -05:00
|
|
|
|
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.
|
2020-06-12 12:31:35 -05:00
|
|
|
pub(crate) fn maybe_create_entry_wrapper(
|
|
|
|
tcx: TyCtxt<'_>,
|
2020-10-01 03:38:23 -05:00
|
|
|
module: &mut impl Module,
|
2021-04-30 07:49:58 -05:00
|
|
|
unwind_context: &mut UnwindContext,
|
|
|
|
is_jit: bool,
|
2021-05-27 06:08:14 -05:00
|
|
|
is_primary_cgu: bool,
|
2020-06-12 12:31:35 -05:00
|
|
|
) {
|
2022-07-05 12:56:22 -05:00
|
|
|
let (main_def_id, (is_main_fn, sigpipe)) = match tcx.entry_fn(()) {
|
2019-01-26 04:59:34 -06:00
|
|
|
Some((def_id, entry_ty)) => (
|
2021-04-25 12:09:35 -05:00
|
|
|
def_id,
|
2018-10-20 11:41:26 -05:00
|
|
|
match entry_ty {
|
2022-07-05 12:56:22 -05:00
|
|
|
EntryFnType::Main { sigpipe } => (true, sigpipe),
|
|
|
|
EntryFnType::Start => (false, sigpipe::DEFAULT),
|
2018-10-20 11:41:26 -05:00
|
|
|
},
|
|
|
|
),
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2021-05-27 06:08:14 -05:00
|
|
|
if main_def_id.is_local() {
|
|
|
|
let instance = Instance::mono(tcx, main_def_id).polymorphize(tcx);
|
2023-03-15 09:41:48 -05:00
|
|
|
if !is_jit && module.get_name(tcx.symbol_name(instance).name).is_none() {
|
2021-05-27 06:08:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if !is_primary_cgu {
|
2020-03-07 05:13:54 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-05 12:56:22 -05:00
|
|
|
create_entry_fn(tcx, module, unwind_context, main_def_id, is_jit, is_main_fn, sigpipe);
|
2018-10-20 11:41:26 -05:00
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
fn create_entry_fn(
|
|
|
|
tcx: TyCtxt<'_>,
|
2020-10-01 03:38:23 -05:00
|
|
|
m: &mut impl Module,
|
2021-04-30 07:49:58 -05:00
|
|
|
unwind_context: &mut UnwindContext,
|
2018-10-20 11:41:26 -05:00
|
|
|
rust_main_def_id: DefId,
|
2021-04-30 07:49:58 -05:00
|
|
|
ignore_lang_start_wrapper: bool,
|
|
|
|
is_main_fn: bool,
|
2022-07-05 12:56:22 -05:00
|
|
|
sigpipe: u8,
|
2018-10-20 11:41:26 -05:00
|
|
|
) {
|
2023-01-19 13:09:01 -06:00
|
|
|
let main_ret_ty = tcx.fn_sig(rust_main_def_id).no_bound_vars().unwrap().output();
|
2018-10-20 11:41:26 -05:00
|
|
|
// 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.
|
2022-02-22 18:00:00 -06:00
|
|
|
let main_ret_ty = tcx.normalize_erasing_regions(
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
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
|
|
|
],
|
2021-03-05 12:12:59 -06:00
|
|
|
returns: vec![AbiParam::new(m.target_config().pointer_type() /*isize*/)],
|
2022-11-17 03:29:32 -06:00
|
|
|
call_conv: crate::conv_to_call_conv(
|
2023-01-24 11:56:42 -06:00
|
|
|
tcx.sess,
|
2022-11-17 03:29:32 -06:00
|
|
|
tcx.sess.target.options.entry_abi,
|
2022-12-14 12:30:46 -06:00
|
|
|
m.target_config().default_call_conv,
|
2022-11-17 03:29:32 -06:00
|
|
|
),
|
2018-10-20 11:41:26 -05:00
|
|
|
};
|
|
|
|
|
2022-11-17 03:29:32 -06:00
|
|
|
let entry_name = tcx.sess.target.options.entry_name.as_ref();
|
2023-01-24 11:56:42 -06:00
|
|
|
let cmain_func_id = match m.declare_function(entry_name, Linkage::Export, &cmain_sig) {
|
|
|
|
Ok(func_id) => func_id,
|
|
|
|
Err(err) => {
|
2023-12-18 05:21:37 -06:00
|
|
|
tcx.dcx()
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-19 22:26:58 -05:00
|
|
|
.fatal(format!("entry symbol `{entry_name}` declared multiple times: {err}"));
|
2023-01-24 11:56:42 -06:00
|
|
|
}
|
|
|
|
};
|
2018-10-20 11:41:26 -05:00
|
|
|
|
2020-07-23 11:07:38 -05:00
|
|
|
let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx);
|
2018-10-20 11:41:26 -05:00
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
let main_name = tcx.symbol_name(instance).name;
|
2022-12-14 12:30:46 -06:00
|
|
|
let main_sig = get_function_sig(tcx, m.target_config().default_call_conv, instance);
|
2021-04-30 07:49:58 -05:00
|
|
|
let main_func_id = m.declare_function(main_name, Linkage::Import, &main_sig).unwrap();
|
2018-10-20 11:41:26 -05:00
|
|
|
|
|
|
|
let mut ctx = Context::new();
|
2022-10-23 09:22:55 -05:00
|
|
|
ctx.func.signature = cmain_sig;
|
2019-09-28 10:59:27 -05:00
|
|
|
{
|
|
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
2020-04-05 06:48:26 -05:00
|
|
|
let mut bcx = 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());
|
2022-07-05 12:56:22 -05:00
|
|
|
let arg_sigpipe = bcx.ins().iconst(types::I8, sigpipe as i64);
|
2018-10-20 11:41:26 -05:00
|
|
|
|
|
|
|
let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
|
|
|
|
|
2021-04-30 07:49:58 -05:00
|
|
|
let result = if is_main_fn && ignore_lang_start_wrapper {
|
|
|
|
// regular main fn, but ignoring #[lang = "start"] as we are running in the jit
|
|
|
|
// FIXME set program arguments somehow
|
|
|
|
let call_inst = bcx.ins().call(main_func_ref, &[]);
|
|
|
|
let call_results = bcx.func.dfg.inst_results(call_inst).to_owned();
|
|
|
|
|
|
|
|
let termination_trait = tcx.require_lang_item(LangItem::Termination, None);
|
|
|
|
let report = tcx
|
|
|
|
.associated_items(termination_trait)
|
|
|
|
.find_by_name_and_kind(
|
|
|
|
tcx,
|
|
|
|
Ident::from_str("report"),
|
|
|
|
AssocKind::Fn,
|
|
|
|
termination_trait,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-10 05:49:27 -05:00
|
|
|
let report = Instance::expect_resolve(
|
2021-04-30 07:49:58 -05:00
|
|
|
tcx,
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
report.def_id,
|
2023-07-11 16:35:29 -05:00
|
|
|
tcx.mk_args(&[GenericArg::from(main_ret_ty)]),
|
2021-04-30 07:49:58 -05:00
|
|
|
)
|
2022-07-25 09:07:57 -05:00
|
|
|
.polymorphize(tcx);
|
2021-04-30 07:49:58 -05:00
|
|
|
|
|
|
|
let report_name = tcx.symbol_name(report).name;
|
2022-12-14 12:30:46 -06:00
|
|
|
let report_sig = get_function_sig(tcx, m.target_config().default_call_conv, report);
|
2021-04-30 07:49:58 -05:00
|
|
|
let report_func_id =
|
|
|
|
m.declare_function(report_name, Linkage::Import, &report_sig).unwrap();
|
|
|
|
let report_func_ref = m.declare_func_in_func(report_func_id, &mut bcx.func);
|
|
|
|
|
|
|
|
// FIXME do proper abi handling instead of expecting the pass mode to be identical
|
|
|
|
// for returns and arguments.
|
|
|
|
let report_call_inst = bcx.ins().call(report_func_ref, &call_results);
|
|
|
|
let res = bcx.func.dfg.inst_results(report_call_inst)[0];
|
|
|
|
match m.target_config().pointer_type() {
|
|
|
|
types::I32 => res,
|
|
|
|
types::I64 => bcx.ins().sextend(types::I64, res),
|
|
|
|
_ => unimplemented!("16bit systems are not yet supported"),
|
|
|
|
}
|
|
|
|
} else if is_main_fn {
|
2020-09-05 03:38:49 -05:00
|
|
|
let start_def_id = tcx.require_lang_item(LangItem::Start, None);
|
2024-03-10 05:49:27 -05:00
|
|
|
let start_instance = Instance::expect_resolve(
|
2018-10-20 11:41:26 -05:00
|
|
|
tcx,
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
start_def_id,
|
2023-07-11 16:35:29 -05:00
|
|
|
tcx.mk_args(&[main_ret_ty.into()]),
|
2018-10-20 11:41:26 -05:00
|
|
|
)
|
2020-07-23 11:07:38 -05:00
|
|
|
.polymorphize(tcx);
|
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
|
|
|
|
2021-03-05 12:12:59 -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);
|
2022-07-05 12:56:22 -05:00
|
|
|
let call_inst =
|
|
|
|
bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv, arg_sigpipe]);
|
2021-04-30 07:49:58 -05:00
|
|
|
bcx.inst_results(call_inst)[0]
|
2018-10-20 11:41:26 -05:00
|
|
|
} else {
|
|
|
|
// using user-defined start fn
|
2021-04-30 07:49:58 -05:00
|
|
|
let call_inst = bcx.ins().call(main_func_ref, &[arg_argc, arg_argv]);
|
|
|
|
bcx.inst_results(call_inst)[0]
|
2018-10-20 11:41:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
bcx.ins().return_(&[result]);
|
|
|
|
bcx.seal_all_blocks();
|
2019-09-28 10:59:27 -05:00
|
|
|
bcx.finalize();
|
|
|
|
}
|
2023-01-24 11:56:42 -06:00
|
|
|
|
|
|
|
if let Err(err) = m.define_function(cmain_func_id, &mut ctx) {
|
2023-12-18 05:21:37 -06:00
|
|
|
tcx.dcx().fatal(format!("entry symbol `{entry_name}` defined multiple times: {err}"));
|
2023-01-24 11:56:42 -06:00
|
|
|
}
|
|
|
|
|
2020-06-12 12:31:35 -05:00
|
|
|
unwind_context.add_function(cmain_func_id, &ctx, m.isa());
|
2018-10-20 11:41:26 -05:00
|
|
|
}
|
|
|
|
}
|