Skip to content

Commit 2e8af91

Browse files
committed
Generalized base:maybe_create_entry_wrapper
1 parent 8ea0331 commit 2e8af91

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::collector::{self, MonoItemCollectionMode};
@@ -502,48 +501,50 @@ pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
502501

503502
/// Create the `main` function which will initialize the rust runtime and call
504503
/// users main function.
505-
fn maybe_create_entry_wrapper(cx: &CodegenCx<'ll, '_, &'ll Value>) {
504+
fn maybe_create_entry_wrapper<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>>(
505+
cx: &'a Bx::CodegenCx
506+
) {
506507
let (main_def_id, span) = match *cx.sess().entry_fn.borrow() {
507508
Some((id, span, _)) => {
508-
(cx.tcx.hir.local_def_id(id), span)
509+
(cx.tcx().hir.local_def_id(id), span)
509510
}
510511
None => return,
511512
};
512513

513-
let instance = Instance::mono(cx.tcx, main_def_id);
514+
let instance = Instance::mono(*cx.tcx(), main_def_id);
514515

515-
if !cx.codegen_unit.contains_item(&MonoItem::Fn(instance)) {
516+
if !cx.codegen_unit().contains_item(&MonoItem::Fn(instance)) {
516517
// We want to create the wrapper in the same codegen unit as Rust's main
517518
// function.
518519
return;
519520
}
520521

521-
let main_llfn = callee::get_fn(cx, instance);
522+
let main_llfn = cx.get_fn(instance);
522523

523524
let et = cx.sess().entry_fn.get().map(|e| e.2);
524525
match et {
525-
Some(EntryFnType::Main) => create_entry_fn(cx, span, main_llfn, main_def_id, true),
526-
Some(EntryFnType::Start) => create_entry_fn(cx, span, main_llfn, main_def_id, false),
526+
Some(EntryFnType::Main) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, true),
527+
Some(EntryFnType::Start) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, false),
527528
None => {} // Do nothing.
528529
}
529530

530-
fn create_entry_fn(
531-
cx: &CodegenCx<'ll, '_, &'ll Value>,
531+
fn create_entry_fn<'a, 'll: 'a, 'tcx: 'll, Bx: BuilderMethods<'a, 'll, 'tcx>>(
532+
cx: &'a Bx::CodegenCx,
532533
sp: Span,
533-
rust_main: &'ll Value,
534+
rust_main: <Bx::CodegenCx as Backend<'ll>>::Value,
534535
rust_main_def_id: DefId,
535536
use_start_lang_item: bool,
536537
) {
537538
let llfty =
538539
cx.type_func(&[cx.type_int(), cx.type_ptr_to(cx.type_i8p())], cx.type_int());
539540

540-
let main_ret_ty = cx.tcx.fn_sig(rust_main_def_id).output();
541+
let main_ret_ty = cx.tcx().fn_sig(rust_main_def_id).output();
541542
// Given that `main()` has no arguments,
542543
// then its return type cannot have
543544
// late-bound regions, since late-bound
544545
// regions must appear in the argument
545546
// listing.
546-
let main_ret_ty = cx.tcx.erase_regions(
547+
let main_ret_ty = cx.tcx().erase_regions(
547548
&main_ret_ty.no_late_bound_regions().unwrap(),
548549
);
549550

@@ -558,25 +559,25 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx<'ll, '_, &'ll Value>) {
558559
let llfn = cx.declare_cfn("main", llfty);
559560

560561
// `main` should respect same config for frame pointer elimination as rest of code
561-
attributes::set_frame_pointer_elimination(cx, llfn);
562-
attributes::apply_target_cpu_attr(cx, llfn);
562+
cx.set_frame_pointer_elimination(llfn);
563+
cx.apply_target_cpu_attr(llfn);
563564

564-
let bx = Builder::new_block(cx, llfn, "top");
565+
let bx = Bx::new_block(&cx, llfn, "top");
565566

566-
debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(&bx);
567+
bx.insert_reference_to_gdb_debug_scripts_section_global();
567568

568569
// Params from native main() used as args for rust start function
569-
let param_argc = get_param(llfn, 0);
570-
let param_argv = get_param(llfn, 1);
571-
let arg_argc = bx.intcast(param_argc, cx.isize_ty, true);
570+
let param_argc = cx.get_param(llfn, 0);
571+
let param_argv = cx.get_param(llfn, 1);
572+
let arg_argc = bx.intcast(param_argc, cx.type_isize(), true);
572573
let arg_argv = param_argv;
573574

574575
let (start_fn, args) = if use_start_lang_item {
575-
let start_def_id = cx.tcx.require_lang_item(StartFnLangItem);
576+
let start_def_id = cx.tcx().require_lang_item(StartFnLangItem);
576577
let start_fn = callee::resolve_and_get_fn(
577578
cx,
578579
start_def_id,
579-
cx.tcx.intern_substs(&[main_ret_ty.into()]),
580+
cx.tcx().intern_substs(&[main_ret_ty.into()]),
580581
);
581582
(start_fn, vec![bx.pointercast(rust_main, cx.type_ptr_to(cx.type_i8p())),
582583
arg_argc, arg_argv])
@@ -1212,7 +1213,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12121213

12131214
// If this codegen unit contains the main function, also create the
12141215
// wrapper here
1215-
maybe_create_entry_wrapper(&cx);
1216+
maybe_create_entry_wrapper::<Builder<&Value>>(&cx);
12161217

12171218
// Run replace-all-uses-with for statics that need it
12181219
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
@@ -426,6 +426,14 @@ impl MiscMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {
426426
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
427427
&self.codegen_unit
428428
}
429+
430+
fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
431+
attributes::set_frame_pointer_elimination(self, llfn)
432+
}
433+
434+
fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
435+
attributes::apply_target_cpu_attr(self, llfn)
436+
}
429437
}
430438

431439
impl<'ll, 'tcx: 'll> CodegenMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {}

src/librustc_codegen_llvm/debuginfo/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ impl<'a, 'll: 'a, 'tcx: 'll> DebugInfoBuilderMethods<'a, 'll, 'tcx>
281281
) {
282282
set_source_location(debug_context, &self, scope, span)
283283
}
284+
fn insert_reference_to_gdb_debug_scripts_section_global(&self) {
285+
gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
286+
}
284287
}
285288

286289
impl<'ll, 'tcx: 'll> DebugInfoMethods<'ll, 'tcx> for CodegenCx<'ll, 'tcx, &'ll Value> {

src/librustc_codegen_llvm/interfaces/debuginfo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ pub trait DebugInfoBuilderMethods<'a, 'll: 'a, 'tcx: 'll> : HasCodegen<'a, 'll,
6464
scope: Option<<Self::CodegenCx as DebugInfoMethods<'ll, 'tcx>>::DIScope>,
6565
span: syntax_pos::Span,
6666
);
67+
fn insert_reference_to_gdb_debug_scripts_section_global(&self);
6768
}

src/librustc_codegen_llvm/interfaces/misc.rs

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

0 commit comments

Comments
 (0)