Skip to content

Commit d53ba64

Browse files
authored
Rollup merge of rust-lang#65681 - sunfishcode:followup, r=Centril
Code cleanups following up on rust-lang#65576. This makes a few code cleanups to follow up on the review comments in rust-lang#65576. r? @Centril
2 parents 21af776 + 3f1af90 commit d53ba64

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/librustc_codegen_ssa/base.rs

+24-13
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(cx: &'
406406
rust_main_def_id: DefId,
407407
use_start_lang_item: bool,
408408
) {
409+
// The entry function is either `int main(void)` or `int main(int argc, char **argv)`,
410+
// depending on whether the target needs `argc` and `argv` to be passed in.
409411
let llfty = if cx.sess().target.target.options.main_needs_argc_argv {
410412
cx.type_func(&[cx.type_int(), cx.type_ptr_to(cx.type_i8p())], cx.type_int())
411413
} else {
@@ -440,19 +442,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(cx: &'
440442

441443
bx.insert_reference_to_gdb_debug_scripts_section_global();
442444

443-
let (arg_argc, arg_argv) = if cx.sess().target.target.options.main_needs_argc_argv {
444-
// Params from native main() used as args for rust start function
445-
let param_argc = bx.get_param(0);
446-
let param_argv = bx.get_param(1);
447-
let arg_argc = bx.intcast(param_argc, cx.type_isize(), true);
448-
let arg_argv = param_argv;
449-
(arg_argc, arg_argv)
450-
} else {
451-
// The Rust start function doesn't need argc and argv, so just pass zeros.
452-
let arg_argc = bx.const_int(cx.type_int(), 0);
453-
let arg_argv = bx.const_null(cx.type_ptr_to(cx.type_i8p()));
454-
(arg_argc, arg_argv)
455-
};
445+
let (arg_argc, arg_argv) = get_argc_argv(cx, &mut bx);
456446

457447
let (start_fn, args) = if use_start_lang_item {
458448
let start_def_id = cx.tcx().require_lang_item(StartFnLangItem, None);
@@ -477,6 +467,27 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(cx: &'
477467
}
478468
}
479469

470+
/// Obtain the `argc` and `argv` values to pass to the rust start function.
471+
fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
472+
cx: &'a Bx::CodegenCx,
473+
bx: &mut Bx
474+
) -> (Bx::Value, Bx::Value)
475+
{
476+
if cx.sess().target.target.options.main_needs_argc_argv {
477+
// Params from native `main()` used as args for rust start function
478+
let param_argc = bx.get_param(0);
479+
let param_argv = bx.get_param(1);
480+
let arg_argc = bx.intcast(param_argc, cx.type_isize(), true);
481+
let arg_argv = param_argv;
482+
(arg_argc, arg_argv)
483+
} else {
484+
// The Rust start function doesn't need `argc` and `argv`, so just pass zeros.
485+
let arg_argc = bx.const_int(cx.type_int(), 0);
486+
let arg_argv = bx.const_null(cx.type_ptr_to(cx.type_i8p()));
487+
(arg_argc, arg_argv)
488+
}
489+
}
490+
480491
pub const CODEGEN_WORKER_ID: usize = ::std::usize::MAX;
481492

482493
pub fn codegen_crate<B: ExtraBackendMethods>(

0 commit comments

Comments
 (0)