about summary refs log tree commit diff
path: root/src/librustc_codegen_llvm
diff options
context:
space:
mode:
authorDenis Merigoux <denis.merigoux@gmail.com>2018-09-24 15:26:39 +0200
committerEduard-Mihai Burtescu <edy.burt@gmail.com>2018-11-16 14:34:26 +0200
commitac34068ed9c1fa96d7fd172c1033df96db1d7143 (patch)
treeac59e74c6ef1de98682866ae2cb7c68ae3dcf831 /src/librustc_codegen_llvm
parentb14f3e54906b9e0e8906affd406923bb8fc12b1e (diff)
downloadrust-ac34068ed9c1fa96d7fd172c1033df96db1d7143.tar.gz
rust-ac34068ed9c1fa96d7fd172c1033df96db1d7143.zip
Generalized base:maybe_create_entry_wrapper
Diffstat (limited to 'src/librustc_codegen_llvm')
-rw-r--r--src/librustc_codegen_llvm/base.rs49
-rw-r--r--src/librustc_codegen_llvm/context.rs8
-rw-r--r--src/librustc_codegen_llvm/debuginfo/mod.rs3
-rw-r--r--src/librustc_codegen_llvm/interfaces/debuginfo.rs1
-rw-r--r--src/librustc_codegen_llvm/interfaces/misc.rs2
5 files changed, 39 insertions, 24 deletions
diff --git a/src/librustc_codegen_llvm/base.rs b/src/librustc_codegen_llvm/base.rs
index d55c156cead..0648f0d7802 100644
--- a/src/librustc_codegen_llvm/base.rs
+++ b/src/librustc_codegen_llvm/base.rs
@@ -30,7 +30,7 @@ use super::CachedModuleCodegen;
 
 use abi;
 use back::write::{self, OngoingCodegen};
-use llvm::{self, get_param};
+use llvm;
 use metadata;
 use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
@@ -50,7 +50,6 @@ use rustc::session::Session;
 use rustc_incremental;
 use allocator;
 use mir::place::PlaceRef;
-use attributes;
 use builder::{Builder, MemFlags};
 use callee;
 use rustc_mir::monomorphize::item::DefPathBasedNames;
@@ -495,48 +494,50 @@ pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
 
 /// Create the `main` function which will initialize the rust runtime and call
 /// users main function.
-fn maybe_create_entry_wrapper(cx: &CodegenCx) {
+fn maybe_create_entry_wrapper<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
+    cx: &'a Bx::CodegenCx
+) {
     let (main_def_id, span) = match *cx.sess().entry_fn.borrow() {
         Some((id, span, _)) => {
-            (cx.tcx.hir.local_def_id(id), span)
+            (cx.tcx().hir.local_def_id(id), span)
         }
         None => return,
     };
 
-    let instance = Instance::mono(cx.tcx, main_def_id);
+    let instance = Instance::mono(cx.tcx(), main_def_id);
 
-    if !cx.codegen_unit.contains_item(&MonoItem::Fn(instance)) {
+    if !cx.codegen_unit().contains_item(&MonoItem::Fn(instance)) {
         // We want to create the wrapper in the same codegen unit as Rust's main
         // function.
         return;
     }
 
-    let main_llfn = callee::get_fn(cx, instance);
+    let main_llfn = cx.get_fn(instance);
 
     let et = cx.sess().entry_fn.get().map(|e| e.2);
     match et {
-        Some(EntryFnType::Main) => create_entry_fn(cx, span, main_llfn, main_def_id, true),
-        Some(EntryFnType::Start) => create_entry_fn(cx, span, main_llfn, main_def_id, false),
+        Some(EntryFnType::Main) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, true),
+        Some(EntryFnType::Start) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, false),
         None => {}    // Do nothing.
     }
 
-    fn create_entry_fn(
-        cx: &CodegenCx<'ll, '_>,
+    fn create_entry_fn<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
+        cx: &'a Bx::CodegenCx,
         sp: Span,
-        rust_main: &'ll Value,
+        rust_main: Bx::Value,
         rust_main_def_id: DefId,
         use_start_lang_item: bool,
     ) {
         let llfty =
             cx.type_func(&[cx.type_int(), cx.type_ptr_to(cx.type_i8p())], cx.type_int());
 
-        let main_ret_ty = cx.tcx.fn_sig(rust_main_def_id).output();
+        let main_ret_ty = cx.tcx().fn_sig(rust_main_def_id).output();
         // 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.
-        let main_ret_ty = cx.tcx.erase_regions(
+        let main_ret_ty = cx.tcx().erase_regions(
             &main_ret_ty.no_bound_vars().unwrap(),
         );
 
@@ -551,25 +552,25 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx) {
         let llfn = cx.declare_cfn("main", llfty);
 
         // `main` should respect same config for frame pointer elimination as rest of code
-        attributes::set_frame_pointer_elimination(cx, llfn);
-        attributes::apply_target_cpu_attr(cx, llfn);
+        cx.set_frame_pointer_elimination(llfn);
+        cx.apply_target_cpu_attr(llfn);
 
-        let bx = Builder::new_block(cx, llfn, "top");
+        let bx = Bx::new_block(&cx, llfn, "top");
 
-        debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(&bx);
+        bx.insert_reference_to_gdb_debug_scripts_section_global();
 
         // Params from native main() used as args for rust start function
-        let param_argc = get_param(llfn, 0);
-        let param_argv = get_param(llfn, 1);
-        let arg_argc = bx.intcast(param_argc, cx.isize_ty, true);
+        let param_argc = cx.get_param(llfn, 0);
+        let param_argv = cx.get_param(llfn, 1);
+        let arg_argc = bx.intcast(param_argc, cx.type_isize(), true);
         let arg_argv = param_argv;
 
         let (start_fn, args) = if use_start_lang_item {
-            let start_def_id = cx.tcx.require_lang_item(StartFnLangItem);
+            let start_def_id = cx.tcx().require_lang_item(StartFnLangItem);
             let start_fn = callee::resolve_and_get_fn(
                 cx,
                 start_def_id,
-                cx.tcx.intern_substs(&[main_ret_ty.into()]),
+                cx.tcx().intern_substs(&[main_ret_ty.into()]),
             );
             (start_fn, vec![bx.pointercast(rust_main, cx.type_ptr_to(cx.type_i8p())),
                             arg_argc, arg_argv])
@@ -1112,7 +1113,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
 
             // If this codegen unit contains the main function, also create the
             // wrapper here
-            maybe_create_entry_wrapper(&cx);
+            maybe_create_entry_wrapper::<Builder<&Value>>(&cx);
 
             // Run replace-all-uses-with for statics that need it
             for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
diff --git a/src/librustc_codegen_llvm/context.rs b/src/librustc_codegen_llvm/context.rs
index 1ccab0ec81a..39955c07c09 100644
--- a/src/librustc_codegen_llvm/context.rs
+++ b/src/librustc_codegen_llvm/context.rs
@@ -425,6 +425,14 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
     fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
         &self.codegen_unit
     }
+
+    fn set_frame_pointer_elimination(&self, llfn: &'ll Value) {
+        attributes::set_frame_pointer_elimination(self, llfn)
+    }
+
+    fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
+        attributes::apply_target_cpu_attr(self, llfn)
+    }
 }
 
 impl IntrinsicDeclarationMethods<'tcx> for CodegenCx<'b, 'tcx> {
diff --git a/src/librustc_codegen_llvm/debuginfo/mod.rs b/src/librustc_codegen_llvm/debuginfo/mod.rs
index b802acb04bd..81a889b5134 100644
--- a/src/librustc_codegen_llvm/debuginfo/mod.rs
+++ b/src/librustc_codegen_llvm/debuginfo/mod.rs
@@ -279,6 +279,9 @@ impl DebugInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
     ) {
         set_source_location(debug_context, &self, scope, span)
     }
+    fn insert_reference_to_gdb_debug_scripts_section_global(&self) {
+        gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
+    }
 }
 
 impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
diff --git a/src/librustc_codegen_llvm/interfaces/debuginfo.rs b/src/librustc_codegen_llvm/interfaces/debuginfo.rs
index 9b583e9c753..773c035939c 100644
--- a/src/librustc_codegen_llvm/interfaces/debuginfo.rs
+++ b/src/librustc_codegen_llvm/interfaces/debuginfo.rs
@@ -58,4 +58,5 @@ pub trait DebugInfoBuilderMethods<'tcx>: HasCodegen<'tcx> {
         scope: Option<Self::DIScope>,
         span: Span,
     );
+    fn insert_reference_to_gdb_debug_scripts_section_global(&self);
 }
diff --git a/src/librustc_codegen_llvm/interfaces/misc.rs b/src/librustc_codegen_llvm/interfaces/misc.rs
index a9e6f930a8d..7c5108500f1 100644
--- a/src/librustc_codegen_llvm/interfaces/misc.rs
+++ b/src/librustc_codegen_llvm/interfaces/misc.rs
@@ -31,4 +31,6 @@ pub trait MiscMethods<'tcx>: Backend<'tcx> {
     fn sess(&self) -> &Session;
     fn stats(&self) -> &RefCell<Stats>;
     fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>>;
+    fn set_frame_pointer_elimination(&self, llfn: Self::Value);
+    fn apply_target_cpu_attr(&self, llfn: Self::Value);
 }