Skip to content

Commit ac34068

Browse files
denismerigouxeddyb
authored andcommitted
Generalized base:maybe_create_entry_wrapper
1 parent b14f3e5 commit ac34068

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use super::CachedModuleCodegen;
3030

3131
use abi;
3232
use back::write::{self, OngoingCodegen};
33-
use llvm::{self, get_param};
33+
use llvm;
3434
use metadata;
3535
use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
3636
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
@@ -50,7 +50,6 @@ use rustc::session::Session;
5050
use rustc_incremental;
5151
use allocator;
5252
use mir::place::PlaceRef;
53-
use attributes;
5453
use builder::{Builder, MemFlags};
5554
use callee;
5655
use rustc_mir::monomorphize::item::DefPathBasedNames;
@@ -495,48 +494,50 @@ pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
495494

496495
/// Create the `main` function which will initialize the rust runtime and call
497496
/// users main function.
498-
fn maybe_create_entry_wrapper(cx: &CodegenCx) {
497+
fn maybe_create_entry_wrapper<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
498+
cx: &'a Bx::CodegenCx
499+
) {
499500
let (main_def_id, span) = match *cx.sess().entry_fn.borrow() {
500501
Some((id, span, _)) => {
501-
(cx.tcx.hir.local_def_id(id), span)
502+
(cx.tcx().hir.local_def_id(id), span)
502503
}
503504
None => return,
504505
};
505506

506-
let instance = Instance::mono(cx.tcx, main_def_id);
507+
let instance = Instance::mono(cx.tcx(), main_def_id);
507508

508-
if !cx.codegen_unit.contains_item(&MonoItem::Fn(instance)) {
509+
if !cx.codegen_unit().contains_item(&MonoItem::Fn(instance)) {
509510
// We want to create the wrapper in the same codegen unit as Rust's main
510511
// function.
511512
return;
512513
}
513514

514-
let main_llfn = callee::get_fn(cx, instance);
515+
let main_llfn = cx.get_fn(instance);
515516

516517
let et = cx.sess().entry_fn.get().map(|e| e.2);
517518
match et {
518-
Some(EntryFnType::Main) => create_entry_fn(cx, span, main_llfn, main_def_id, true),
519-
Some(EntryFnType::Start) => create_entry_fn(cx, span, main_llfn, main_def_id, false),
519+
Some(EntryFnType::Main) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, true),
520+
Some(EntryFnType::Start) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, false),
520521
None => {} // Do nothing.
521522
}
522523

523-
fn create_entry_fn(
524-
cx: &CodegenCx<'ll, '_>,
524+
fn create_entry_fn<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
525+
cx: &'a Bx::CodegenCx,
525526
sp: Span,
526-
rust_main: &'ll Value,
527+
rust_main: Bx::Value,
527528
rust_main_def_id: DefId,
528529
use_start_lang_item: bool,
529530
) {
530531
let llfty =
531532
cx.type_func(&[cx.type_int(), cx.type_ptr_to(cx.type_i8p())], cx.type_int());
532533

533-
let main_ret_ty = cx.tcx.fn_sig(rust_main_def_id).output();
534+
let main_ret_ty = cx.tcx().fn_sig(rust_main_def_id).output();
534535
// Given that `main()` has no arguments,
535536
// then its return type cannot have
536537
// late-bound regions, since late-bound
537538
// regions must appear in the argument
538539
// listing.
539-
let main_ret_ty = cx.tcx.erase_regions(
540+
let main_ret_ty = cx.tcx().erase_regions(
540541
&main_ret_ty.no_bound_vars().unwrap(),
541542
);
542543

@@ -551,25 +552,25 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx) {
551552
let llfn = cx.declare_cfn("main", llfty);
552553

553554
// `main` should respect same config for frame pointer elimination as rest of code
554-
attributes::set_frame_pointer_elimination(cx, llfn);
555-
attributes::apply_target_cpu_attr(cx, llfn);
555+
cx.set_frame_pointer_elimination(llfn);
556+
cx.apply_target_cpu_attr(llfn);
556557

557-
let bx = Builder::new_block(cx, llfn, "top");
558+
let bx = Bx::new_block(&cx, llfn, "top");
558559

559-
debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(&bx);
560+
bx.insert_reference_to_gdb_debug_scripts_section_global();
560561

561562
// Params from native main() used as args for rust start function
562-
let param_argc = get_param(llfn, 0);
563-
let param_argv = get_param(llfn, 1);
564-
let arg_argc = bx.intcast(param_argc, cx.isize_ty, true);
563+
let param_argc = cx.get_param(llfn, 0);
564+
let param_argv = cx.get_param(llfn, 1);
565+
let arg_argc = bx.intcast(param_argc, cx.type_isize(), true);
565566
let arg_argv = param_argv;
566567

567568
let (start_fn, args) = if use_start_lang_item {
568-
let start_def_id = cx.tcx.require_lang_item(StartFnLangItem);
569+
let start_def_id = cx.tcx().require_lang_item(StartFnLangItem);
569570
let start_fn = callee::resolve_and_get_fn(
570571
cx,
571572
start_def_id,
572-
cx.tcx.intern_substs(&[main_ret_ty.into()]),
573+
cx.tcx().intern_substs(&[main_ret_ty.into()]),
573574
);
574575
(start_fn, vec![bx.pointercast(rust_main, cx.type_ptr_to(cx.type_i8p())),
575576
arg_argc, arg_argv])
@@ -1112,7 +1113,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11121113

11131114
// If this codegen unit contains the main function, also create the
11141115
// wrapper here
1115-
maybe_create_entry_wrapper(&cx);
1116+
maybe_create_entry_wrapper::<Builder<&Value>>(&cx);
11161117

11171118
// Run replace-all-uses-with for statics that need it
11181119
for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {

src/librustc_codegen_llvm/context.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,14 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
425425
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
426426
&self.codegen_unit
427427
}
428+
429+
fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
430+
attributes::set_frame_pointer_elimination(self, llfn)
431+
}
432+
433+
fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
434+
attributes::apply_target_cpu_attr(self, llfn)
435+
}
428436
}
429437

430438
impl IntrinsicDeclarationMethods<'tcx> for CodegenCx<'b, 'tcx> {

src/librustc_codegen_llvm/debuginfo/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ impl DebugInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
279279
) {
280280
set_source_location(debug_context, &self, scope, span)
281281
}
282+
fn insert_reference_to_gdb_debug_scripts_section_global(&self) {
283+
gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
284+
}
282285
}
283286

284287
impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {

src/librustc_codegen_llvm/interfaces/debuginfo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ pub trait DebugInfoBuilderMethods<'tcx>: HasCodegen<'tcx> {
5858
scope: Option<Self::DIScope>,
5959
span: Span,
6060
);
61+
fn insert_reference_to_gdb_debug_scripts_section_global(&self);
6162
}

src/librustc_codegen_llvm/interfaces/misc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ pub trait MiscMethods<'tcx>: Backend<'tcx> {
3131
fn sess(&self) -> &Session;
3232
fn stats(&self) -> &RefCell<Stats>;
3333
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>>;
34+
fn set_frame_pointer_elimination(&self, llfn: Self::Value);
35+
fn apply_target_cpu_attr(&self, llfn: Self::Value);
3436
}

0 commit comments

Comments
 (0)