From dda6c8cf2f035d23ff3f67c5fc0e805bb18cd0a4 Mon Sep 17 00:00:00 2001 From: Mark-Simulacrum Date: Thu, 15 Dec 2016 18:00:19 -0700 Subject: [PATCH] Inline base::malloc_raw_dyn. Move comment about not unwinding into liballoc. --- src/liballoc/heap.rs | 1 + src/librustc/middle/lang_items.rs | 2 -- src/librustc_trans/base.rs | 29 +---------------------------- src/librustc_trans/mir/rvalue.rs | 14 +++++++++++++- 4 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs index 12809171b74..a1e32636980 100644 --- a/src/liballoc/heap.rs +++ b/src/liballoc/heap.rs @@ -127,6 +127,7 @@ pub fn usable_size(size: usize, align: usize) -> usize { pub const EMPTY: *mut () = 0x1 as *mut (); /// The allocator for unique pointers. +// This function must not unwind. If it does, MIR trans will fail. #[cfg(not(test))] #[lang = "exchange_malloc"] #[inline] diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 3bc39fad7f1..1efc211b8c3 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -327,8 +327,6 @@ language_item_table! { PanicBoundsCheckFnLangItem, "panic_bounds_check", panic_bounds_check_fn; PanicFmtLangItem, "panic_fmt", panic_fmt; - // ExchangeMallocFnLangItem cannot unwind, or MIR trans will break. See note - // on `malloc_raw_dyn` in librustc_trans/base.rs. ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn; ExchangeFreeFnLangItem, "exchange_free", exchange_free_fn; BoxFreeFnLangItem, "box_free", box_free_fn; diff --git a/src/librustc_trans/base.rs b/src/librustc_trans/base.rs index 011552b3962..3f477a463ab 100644 --- a/src/librustc_trans/base.rs +++ b/src/librustc_trans/base.rs @@ -37,7 +37,7 @@ use back::symbol_export::{self, ExportedSymbols}; use llvm::{Linkage, ValueRef, Vector, get_param}; use llvm; use rustc::hir::def_id::{DefId, LOCAL_CRATE}; -use middle::lang_items::{LangItem, ExchangeMallocFnLangItem, StartFnLangItem}; +use middle::lang_items::StartFnLangItem; use rustc::ty::subst::Substs; use rustc::traits; use rustc::ty::{self, Ty, TyCtxt, TypeFoldable}; @@ -168,33 +168,6 @@ pub fn get_dataptr(bcx: &Builder, fat_ptr: ValueRef) -> ValueRef { bcx.struct_gep(fat_ptr, abi::FAT_PTR_ADDR) } -fn require_alloc_fn<'blk, 'tcx>( - bcx: &BlockAndBuilder<'blk, 'tcx>, info_ty: Ty<'tcx>, it: LangItem -) -> DefId { - match bcx.tcx().lang_items.require(it) { - Ok(id) => id, - Err(s) => { - bcx.sess().fatal(&format!("allocation of `{}` {}", info_ty, s)); - } - } -} - -// malloc_raw_dyn allocates a box to contain a given type, but with a potentially dynamic size. -// -// MIR requires that ExchangeMallocFnLangItem cannot unwind. -pub fn malloc_raw_dyn<'blk, 'tcx>(bcx: &BlockAndBuilder<'blk, 'tcx>, - llty_ptr: Type, - info_ty: Ty<'tcx>, - size: ValueRef, - align: ValueRef) - -> ValueRef { - // Allocate space: - let def_id = require_alloc_fn(bcx, info_ty, ExchangeMallocFnLangItem); - let r = Callee::def(bcx.ccx(), def_id, bcx.tcx().intern_substs(&[])).reify(bcx.ccx()); - bcx.pointercast(bcx.call(r, &[size, align], None), llty_ptr) -} - - pub fn bin_op_to_icmp_predicate(op: hir::BinOp_, signed: bool) -> llvm::IntPredicate { diff --git a/src/librustc_trans/mir/rvalue.rs b/src/librustc_trans/mir/rvalue.rs index 5aba7160c23..d15598e76af 100644 --- a/src/librustc_trans/mir/rvalue.rs +++ b/src/librustc_trans/mir/rvalue.rs @@ -13,6 +13,7 @@ use rustc::ty::{self, Ty}; use rustc::ty::cast::{CastTy, IntTy}; use rustc::ty::layout::Layout; use rustc::mir; +use middle::lang_items::ExchangeMallocFnLangItem; use asm; use base; @@ -449,7 +450,18 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { let llalign = C_uint(bcx.ccx(), align); let llty_ptr = llty.ptr_to(); let box_ty = bcx.tcx().mk_box(content_ty); - let val = base::malloc_raw_dyn(&bcx, llty_ptr, box_ty, llsize, llalign); + + // Allocate space: + let def_id = match bcx.tcx().lang_items.require(ExchangeMallocFnLangItem) { + Ok(id) => id, + Err(s) => { + bcx.sess().fatal(&format!("allocation of `{}` {}", box_ty, s)); + } + }; + let r = Callee::def(bcx.ccx(), def_id, bcx.tcx().intern_substs(&[])) + .reify(bcx.ccx()); + let val = bcx.pointercast(bcx.call(r, &[llsize, llalign], None), llty_ptr); + let operand = OperandRef { val: OperandValue::Immediate(val), ty: box_ty,