Skip to content

Commit 5fd3e4d

Browse files
authored
Rollup merge of rust-lang#104001 - Ayush1325:custom-entry, r=bjorn3
Improve generating Custom entry function This commit is aimed at making compiler-generated entry functions (Basically just C `main` right now) more generic so other targets can do similar things for custom entry. This was initially implemented as part of rust-lang#100316. Currently, this moves the entry function name and Call convention to the target spec. Signed-off-by: Ayush Singh <[email protected]>
2 parents 2d0c41a + 6ee9712 commit 5fd3e4d

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/abi/mod.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,19 @@ fn clif_sig_from_fn_abi<'tcx>(
2222
default_call_conv: CallConv,
2323
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
2424
) -> Signature {
25-
let call_conv = match fn_abi.conv {
25+
let call_conv = conv_to_call_conv(fn_abi.conv, default_call_conv);
26+
27+
let inputs = fn_abi.args.iter().map(|arg_abi| arg_abi.get_abi_param(tcx).into_iter()).flatten();
28+
29+
let (return_ptr, returns) = fn_abi.ret.get_abi_return(tcx);
30+
// Sometimes the first param is an pointer to the place where the return value needs to be stored.
31+
let params: Vec<_> = return_ptr.into_iter().chain(inputs).collect();
32+
33+
Signature { params, returns, call_conv }
34+
}
35+
36+
pub(crate) fn conv_to_call_conv(c: Conv, default_call_conv: CallConv) -> CallConv {
37+
match c {
2638
Conv::Rust | Conv::C => default_call_conv,
2739
Conv::RustCold => CallConv::Cold,
2840
Conv::X86_64SysV => CallConv::SystemV,
@@ -38,15 +50,8 @@ fn clif_sig_from_fn_abi<'tcx>(
3850
| Conv::X86VectorCall
3951
| Conv::AmdGpuKernel
4052
| Conv::AvrInterrupt
41-
| Conv::AvrNonBlockingInterrupt => todo!("{:?}", fn_abi.conv),
42-
};
43-
let inputs = fn_abi.args.iter().map(|arg_abi| arg_abi.get_abi_param(tcx).into_iter()).flatten();
44-
45-
let (return_ptr, returns) = fn_abi.ret.get_abi_return(tcx);
46-
// Sometimes the first param is an pointer to the place where the return value needs to be stored.
47-
let params: Vec<_> = return_ptr.into_iter().chain(inputs).collect();
48-
49-
Signature { params, returns, call_conv }
53+
| Conv::AvrNonBlockingInterrupt => todo!("{:?}", c),
54+
}
5055
}
5156

5257
pub(crate) fn get_function_sig<'tcx>(

src/main_shim.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ pub(crate) fn maybe_create_entry_wrapper(
6363
AbiParam::new(m.target_config().pointer_type()),
6464
],
6565
returns: vec![AbiParam::new(m.target_config().pointer_type() /*isize*/)],
66-
call_conv: CallConv::triple_default(m.isa().triple()),
66+
call_conv: crate::conv_to_call_conv(
67+
tcx.sess.target.options.entry_abi,
68+
CallConv::triple_default(m.isa().triple()),
69+
),
6770
};
6871

69-
let cmain_func_id = m.declare_function("main", Linkage::Export, &cmain_sig).unwrap();
72+
let entry_name = tcx.sess.target.options.entry_name.as_ref();
73+
let cmain_func_id = m.declare_function(entry_name, Linkage::Export, &cmain_sig).unwrap();
7074

7175
let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx);
7276

0 commit comments

Comments
 (0)