diff --git a/src/librustc_trans/base.rs b/src/librustc_trans/base.rs index 30c1074b89c1ecc7b06f017d797e09da526a7a11..7e03b196cfe3786668f0a7df160ea9bbd6172563 100644 --- a/src/librustc_trans/base.rs +++ b/src/librustc_trans/base.rs @@ -49,7 +49,7 @@ use abi::{self, Abi, FnType}; use adt; use attributes; -use builder::{Builder, noname}; +use builder::Builder; use callee::{Callee}; use common::{BlockAndBuilder, C_bool, C_bytes_in_context, C_i32, C_uint}; use collector::{self, TransItemCollectionMode}; @@ -80,7 +80,6 @@ use libc::c_uint; use std::ffi::{CStr, CString}; use std::cell::RefCell; -use std::ptr; use std::rc::Rc; use std::str; use std::i32; @@ -870,9 +869,7 @@ pub fn maybe_create_entry_wrapper(ccx: &CrateContext) { let et = ccx.sess().entry_type.get().unwrap(); match et { - config::EntryMain => { - create_entry_fn(ccx, span, main_llfn, true); - } + config::EntryMain => create_entry_fn(ccx, span, main_llfn, true), config::EntryStart => create_entry_fn(ccx, span, main_llfn, false), config::EntryNone => {} // Do nothing. } @@ -897,47 +894,27 @@ fn create_entry_fn(ccx: &CrateContext, attributes::set_frame_pointer_elimination(ccx, llfn); let llbb = unsafe { - llvm::LLVMAppendBasicBlockInContext(ccx.llcx(), llfn, "top\0".as_ptr() as *const _) + let name = CString::new("top").unwrap(); + llvm::LLVMAppendBasicBlockInContext(ccx.llcx(), llfn, name.as_ptr()) }; - let bld = ccx.raw_builder(); - unsafe { - llvm::LLVMPositionBuilderAtEnd(bld, llbb); - - debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(ccx); + let bld = Builder::with_ccx(ccx); + bld.position_at_end(llbb); - let (start_fn, args) = if use_start_lang_item { - let start_def_id = match ccx.tcx().lang_items.require(StartFnLangItem) { - Ok(id) => id, - Err(s) => ccx.sess().fatal(&s) - }; - let empty_substs = ccx.tcx().intern_substs(&[]); - let start_fn = Callee::def(ccx, start_def_id, empty_substs).reify(ccx); - let args = { - let opaque_rust_main = - llvm::LLVMBuildPointerCast(bld, - rust_main, - Type::i8p(ccx).to_ref(), - "rust_main\0".as_ptr() as *const _); - - vec![opaque_rust_main, get_param(llfn, 0), get_param(llfn, 1)] - }; - (start_fn, args) - } else { - debug!("using user-defined start fn"); - let args = vec![get_param(llfn, 0 as c_uint), get_param(llfn, 1 as c_uint)]; + debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(ccx, &bld); - (rust_main, args) - }; - - let result = llvm::LLVMRustBuildCall(bld, - start_fn, - args.as_ptr(), - args.len() as c_uint, - ptr::null_mut(), - noname()); + let (start_fn, args) = if use_start_lang_item { + let start_def_id = ccx.tcx().require_lang_item(StartFnLangItem); + let empty_substs = ccx.tcx().intern_substs(&[]); + let start_fn = Callee::def(ccx, start_def_id, empty_substs).reify(ccx); + (start_fn, vec![bld.pointercast(rust_main, Type::i8p(ccx).ptr_to()), get_param(llfn, 0), + get_param(llfn, 1)]) + } else { + debug!("using user-defined start fn"); + (rust_main, vec![get_param(llfn, 0 as c_uint), get_param(llfn, 1 as c_uint)]) + }; - llvm::LLVMBuildRet(bld, result); - } + let result = bld.call(start_fn, &args, None); + bld.ret(result); } } diff --git a/src/librustc_trans/common.rs b/src/librustc_trans/common.rs index d09cb8ce2c8a8b6ff1267d9f0132de3a980fdbd7..0a79c8d43f6fe8d7027edbde8f6d45d8eaa89e7b 100644 --- a/src/librustc_trans/common.rs +++ b/src/librustc_trans/common.rs @@ -14,7 +14,7 @@ use session::Session; use llvm; -use llvm::{ValueRef, BasicBlockRef, BuilderRef, ContextRef, TypeKind}; +use llvm::{ValueRef, BasicBlockRef, ContextRef, TypeKind}; use llvm::{True, False, Bool, OperandBundleDef, get_param}; use llvm::debuginfo::DIScope; use monomorphize::Instance; @@ -235,24 +235,6 @@ pub fn from_ty(tcx: TyCtxt<'a, 'tcx, 'tcx>, } } -pub struct BuilderRef_res { - pub b: BuilderRef, -} - -impl Drop for BuilderRef_res { - fn drop(&mut self) { - unsafe { - llvm::LLVMDisposeBuilder(self.b); - } - } -} - -pub fn BuilderRef_res(b: BuilderRef) -> BuilderRef_res { - BuilderRef_res { - b: b - } -} - pub fn validate_substs(substs: &Substs) { assert!(!substs.needs_infer()); } diff --git a/src/librustc_trans/context.rs b/src/librustc_trans/context.rs index 0f1fe15a7ecb5116dce9cbcf0103f3e33bfc32a4..ead23b333ff249a40c9ceb54bfe1016787875c4d 100644 --- a/src/librustc_trans/context.rs +++ b/src/librustc_trans/context.rs @@ -9,15 +9,13 @@ // except according to those terms. use llvm; -use llvm::{ContextRef, ModuleRef, ValueRef, BuilderRef}; -use rustc::dep_graph::{DepGraph, DepNode, DepTrackingMap, DepTrackingMapConfig, - WorkProduct}; +use llvm::{ContextRef, ModuleRef, ValueRef}; +use rustc::dep_graph::{DepGraph, DepNode, DepTrackingMap, DepTrackingMapConfig, WorkProduct}; use middle::cstore::LinkMeta; use rustc::hir::def::ExportMap; use rustc::hir::def_id::DefId; use rustc::traits; use base; -use common::BuilderRef_res; use debuginfo; use declare; use glue::DropGlueKind; @@ -139,7 +137,6 @@ pub struct LocalCrateContext<'tcx> { int_type: Type, opaque_vec_type: Type, str_slice_type: Type, - builder: BuilderRef_res, /// Holds the LLVM values for closure IDs. closure_vals: RefCell, ValueRef>>, @@ -605,7 +602,6 @@ fn new<'a>(shared: &SharedCrateContext<'a, 'tcx>, int_type: Type::from_ref(ptr::null_mut()), opaque_vec_type: Type::from_ref(ptr::null_mut()), str_slice_type: Type::from_ref(ptr::null_mut()), - builder: BuilderRef_res(llvm::LLVMCreateBuilderInContext(llcx)), closure_vals: RefCell::new(FxHashMap()), dbg_cx: dbg_cx, eh_personality: Cell::new(None), @@ -682,10 +678,6 @@ pub fn sess<'a>(&'a self) -> &'a Session { &self.shared.tcx.sess } - pub fn raw_builder<'a>(&'a self) -> BuilderRef { - self.local().builder.b - } - pub fn get_intrinsic(&self, key: &str) -> ValueRef { if let Some(v) = self.intrinsics().borrow().get(key).cloned() { return v; diff --git a/src/librustc_trans/debuginfo/gdb.rs b/src/librustc_trans/debuginfo/gdb.rs index 8f937d3fe25cbd4e6716b22e0c3a9c4baa28bb1b..e8728a39993081bc3bcc45966b43537922ea2b37 100644 --- a/src/librustc_trans/debuginfo/gdb.rs +++ b/src/librustc_trans/debuginfo/gdb.rs @@ -13,37 +13,26 @@ use llvm; use common::{C_bytes, CrateContext, C_i32}; +use builder::Builder; use declare; use type_::Type; use session::config::NoDebugInfo; -use std::ffi::CString; use std::ptr; use syntax::attr; /// Inserts a side-effect free instruction sequence that makes sure that the /// .debug_gdb_scripts global is referenced, so it isn't removed by the linker. -pub fn insert_reference_to_gdb_debug_scripts_section_global(ccx: &CrateContext) { +pub fn insert_reference_to_gdb_debug_scripts_section_global(ccx: &CrateContext, builder: &Builder) { if needs_gdb_debug_scripts_section(ccx) { - let empty = CString::new("").unwrap(); - let gdb_debug_scripts_section_global = - get_or_insert_gdb_debug_scripts_section_global(ccx); + let gdb_debug_scripts_section_global = get_or_insert_gdb_debug_scripts_section_global(ccx); + // Load just the first byte as that's all that's necessary to force + // LLVM to keep around the reference to the global. + let indices = [C_i32(ccx, 0), C_i32(ccx, 0)]; + let element = builder.inbounds_gep(gdb_debug_scripts_section_global, &indices); + let volative_load_instruction = builder.volatile_load(element); unsafe { - // Load just the first byte as that's all that's necessary to force - // LLVM to keep around the reference to the global. - let indices = [C_i32(ccx, 0), C_i32(ccx, 0)]; - let element = - llvm::LLVMBuildInBoundsGEP(ccx.raw_builder(), - gdb_debug_scripts_section_global, - indices.as_ptr(), - indices.len() as ::libc::c_uint, - empty.as_ptr()); - let volative_load_instruction = - llvm::LLVMBuildLoad(ccx.raw_builder(), - element, - empty.as_ptr()); - llvm::LLVMSetVolatile(volative_load_instruction, llvm::True); llvm::LLVMSetAlignment(volative_load_instruction, 1); } } diff --git a/src/librustc_trans/debuginfo/mod.rs b/src/librustc_trans/debuginfo/mod.rs index f915f60c294607761b2baad7044d29e33b64b428..9cc2c72648f0216583e04e0b857352a5b430ea9e 100644 --- a/src/librustc_trans/debuginfo/mod.rs +++ b/src/librustc_trans/debuginfo/mod.rs @@ -66,7 +66,6 @@ pub struct CrateDebugContext<'tcx> { llcontext: ContextRef, builder: DIBuilderRef, - current_debug_location: Cell, created_files: RefCell>, created_enum_disr_types: RefCell>, @@ -84,16 +83,15 @@ pub fn new(llmod: ModuleRef) -> CrateDebugContext<'tcx> { let builder = unsafe { llvm::LLVMRustDIBuilderCreate(llmod) }; // DIBuilder inherits context from the module, so we'd better use the same one let llcontext = unsafe { llvm::LLVMGetModuleContext(llmod) }; - return CrateDebugContext { + CrateDebugContext { llcontext: llcontext, builder: builder, - current_debug_location: Cell::new(InternalDebugLocation::UnknownLocation), created_files: RefCell::new(FxHashMap()), created_enum_disr_types: RefCell::new(FxHashMap()), type_map: RefCell::new(TypeMap::new()), namespace_map: RefCell::new(DefIdMap()), composite_types_completed: RefCell::new(FxHashSet()), - }; + } } } @@ -198,15 +196,12 @@ pub fn finalize(cx: &CrateContext) { } /// Creates a function-specific debug context for a function w/o debuginfo. -pub fn empty_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>) - -> FunctionDebugContext { +pub fn empty_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>) -> FunctionDebugContext { if cx.sess().opts.debuginfo == NoDebugInfo { - return FunctionDebugContext::DebugInfoDisabled; + FunctionDebugContext::DebugInfoDisabled + } else { + FunctionDebugContext::FunctionWithoutDebugInfo } - - // Clear the debug location so we don't assign them in the function prelude. - source_loc::set_debug_location(cx, None, UnknownLocation); - FunctionDebugContext::FunctionWithoutDebugInfo } /// Creates the function-specific debug context. @@ -225,10 +220,6 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, return FunctionDebugContext::DebugInfoDisabled; } - // Clear the debug location so we don't assign them in the function prelude. - // Do this here already, in case we do an early exit from this function. - source_loc::set_debug_location(cx, None, UnknownLocation); - let containing_scope = get_containing_scope(cx, instance); let span = mir.span; @@ -482,10 +473,10 @@ pub fn declare_local<'blk, 'tcx>(bcx: &BlockAndBuilder<'blk, 'tcx>, align as u64, ) }; - source_loc::set_debug_location(cx, None, + source_loc::set_debug_location(cx, bcx, InternalDebugLocation::new(scope_metadata, loc.line, loc.col.to_usize())); unsafe { - let debug_loc = llvm::LLVMGetCurrentDebugLocation(cx.raw_builder()); + let debug_loc = llvm::LLVMGetCurrentDebugLocation(bcx.llbuilder); let instr = llvm::LLVMRustDIBuilderInsertDeclareAtEnd( DIB(cx), alloca, @@ -503,7 +494,7 @@ pub fn declare_local<'blk, 'tcx>(bcx: &BlockAndBuilder<'blk, 'tcx>, match variable_kind { ArgumentVariable(_) | CapturedVariable => { assert!(!bcx.fcx().debug_context.get_ref(span).source_locations_enabled.get()); - source_loc::set_debug_location(cx, None, UnknownLocation); + source_loc::set_debug_location(cx, bcx, UnknownLocation); } _ => { /* nothing to do */ } } diff --git a/src/librustc_trans/debuginfo/source_loc.rs b/src/librustc_trans/debuginfo/source_loc.rs index e03ad1a8c8fdb1c19e70e3b2a64c367b9510d472..2a168e342d05ebd879f97d408bc232b370091cc8 100644 --- a/src/librustc_trans/debuginfo/source_loc.rs +++ b/src/librustc_trans/debuginfo/source_loc.rs @@ -27,11 +27,10 @@ /// /// Maps to a call to llvm::LLVMSetCurrentDebugLocation(...). pub fn set_source_location(fcx: &FunctionContext, builder: &Builder, scope: DIScope, span: Span) { - let builder = builder.llbuilder; let function_debug_context = match fcx.debug_context { FunctionDebugContext::DebugInfoDisabled => return, FunctionDebugContext::FunctionWithoutDebugInfo => { - set_debug_location(fcx.ccx, Some(builder), UnknownLocation); + set_debug_location(fcx.ccx, builder, UnknownLocation); return; } FunctionDebugContext::RegularContext(ref data) => data @@ -44,7 +43,7 @@ pub fn set_source_location(fcx: &FunctionContext, builder: &Builder, scope: DISc } else { UnknownLocation }; - set_debug_location(fcx.ccx, Some(builder), dbg_loc); + set_debug_location(fcx.ccx, builder, dbg_loc); } /// Enables emitting source locations for the given functions. @@ -80,14 +79,8 @@ pub fn new(scope: DIScope, line: usize, col: usize) -> InternalDebugLocation { } pub fn set_debug_location(cx: &CrateContext, - builder: Option, + builder: &Builder, debug_location: InternalDebugLocation) { - if builder.is_none() { - if debug_location == debug_context(cx).current_debug_location.get() { - return; - } - } - let metadata_node = match debug_location { KnownLocation { scope, line, .. } => { // Always set the column to zero like Clang and GCC @@ -109,12 +102,7 @@ pub fn set_debug_location(cx: &CrateContext, } }; - if builder.is_none() { - debug_context(cx).current_debug_location.set(debug_location); - } - - let builder = builder.unwrap_or_else(|| cx.raw_builder()); unsafe { - llvm::LLVMSetCurrentDebugLocation(builder, metadata_node); + llvm::LLVMSetCurrentDebugLocation(builder.llbuilder, metadata_node); } }