From cb4570311bda1789761a98997bbe57ac6c794101 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sat, 28 Feb 2015 23:55:50 +0200 Subject: [PATCH] Move get_extern_fn and get_res_dtor out of base These functions have only a single use and functionally belong to foreign and glue respectively anyway --- src/librustc_trans/trans/base.rs | 61 +---------------------------- src/librustc_trans/trans/foreign.rs | 24 +++++++++--- src/librustc_trans/trans/glue.rs | 52 ++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 69 deletions(-) diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index 25750b6ec0e1..5dfa84c15d04 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -54,7 +54,7 @@ use trans::cleanup; use trans::closure; use trans::common::{Block, C_bool, C_bytes_in_context, C_i32, C_int, C_integral}; use trans::common::{C_null, C_struct_in_context, C_u64, C_u8, C_undef}; -use trans::common::{CrateContext, ExternMap, FunctionContext}; +use trans::common::{CrateContext, FunctionContext}; use trans::common::{Result, NodeIdAndSpan}; use trans::common::{node_id_type, return_type_is_void}; use trans::common::{type_is_immediate, type_is_zero_size, val_ty}; @@ -67,7 +67,6 @@ use trans::debuginfo::{self, DebugLoc, ToDebugLoc}; use trans::expr; use trans::foreign; use trans::glue; -use trans::inline; use trans::intrinsic; use trans::machine; use trans::machine::{llsize_of, llsize_of_real}; @@ -84,7 +83,7 @@ use util::sha2::Sha256; use util::nodemap::NodeMap; use arena::TypedArena; -use libc::{c_uint, uint64_t}; +use libc::c_uint; use std::ffi::{CStr, CString}; use std::cell::{Cell, RefCell}; use std::collections::HashSet; @@ -218,23 +217,6 @@ pub fn decl_cdecl_fn(ccx: &CrateContext, decl_fn(ccx, name, llvm::CCallConv, ty, ty::FnConverging(output)) } -// only use this for foreign function ABIs and glue, use `get_extern_rust_fn` for Rust functions -pub fn get_extern_fn(ccx: &CrateContext, - externs: &mut ExternMap, - name: &str, - cc: llvm::CallConv, - ty: Type, - output: Ty) - -> ValueRef { - match externs.get(name) { - Some(n) => return *n, - None => {} - } - let f = decl_fn(ccx, name, cc, ty, ty::FnConverging(output)); - externs.insert(name.to_string(), f); - f -} - fn get_extern_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ty: Ty<'tcx>, name: &str, did: ast::DefId) -> ValueRef { match ccx.externs().borrow().get(name) { @@ -400,45 +382,6 @@ pub fn note_unique_llvm_symbol(ccx: &CrateContext, sym: String) { ccx.all_llvm_symbols().borrow_mut().insert(sym); } - -pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, - did: ast::DefId, - t: Ty<'tcx>, - parent_id: ast::DefId, - substs: &Substs<'tcx>) - -> ValueRef { - let _icx = push_ctxt("trans_res_dtor"); - let did = inline::maybe_instantiate_inline(ccx, did); - - if !substs.types.is_empty() { - assert_eq!(did.krate, ast::LOCAL_CRATE); - - // Since we're in trans we don't care for any region parameters - let substs = ccx.tcx().mk_substs(Substs::erased(substs.types.clone())); - - let (val, _, _) = monomorphize::monomorphic_fn(ccx, did, substs, None); - - val - } else if did.krate == ast::LOCAL_CRATE { - get_item_val(ccx, did.node) - } else { - let tcx = ccx.tcx(); - let name = csearch::get_symbol(&ccx.sess().cstore, did); - let class_ty = ty::lookup_item_type(tcx, parent_id).ty.subst(tcx, substs); - let llty = type_of_dtor(ccx, class_ty); - let dtor_ty = ty::mk_ctor_fn(ccx.tcx(), - did, - &[glue::get_drop_glue_type(ccx, t)], - ty::mk_nil(ccx.tcx())); - get_extern_fn(ccx, - &mut *ccx.externs().borrow_mut(), - &name[..], - llvm::CCallConv, - llty, - dtor_ty) - } -} - pub fn bin_op_to_icmp_predicate(ccx: &CrateContext, op: ast::BinOp_, signed: bool) -> llvm::IntPredicate { match op { diff --git a/src/librustc_trans/trans/foreign.rs b/src/librustc_trans/trans/foreign.rs index ce80c72b8478..e0299ba63f52 100644 --- a/src/librustc_trans/trans/foreign.rs +++ b/src/librustc_trans/trans/foreign.rs @@ -165,6 +165,23 @@ pub fn register_static(ccx: &CrateContext, } } +// only use this for foreign function ABIs and glue, use `get_extern_rust_fn` for Rust functions +pub fn get_extern_fn(ccx: &CrateContext, + externs: &mut ExternMap, + name: &str, + cc: llvm::CallConv, + ty: Type, + output: Ty) + -> ValueRef { + match externs.get(name) { + Some(n) => return *n, + None => {} + } + let f = base::decl_fn(ccx, name, cc, ty, ty::FnConverging(output)); + externs.insert(name.to_string(), f); + f +} + /// Registers a foreign function found in a library. Just adds a LLVM global. pub fn register_foreign_item_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, abi: Abi, fty: Ty<'tcx>, @@ -190,12 +207,7 @@ pub fn register_foreign_item_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, // Create the LLVM value for the C extern fn let llfn_ty = lltype_for_fn_from_foreign_types(ccx, &tys); - let llfn = base::get_extern_fn(ccx, - &mut *ccx.externs().borrow_mut(), - name, - cc, - llfn_ty, - fty); + let llfn = get_extern_fn(ccx, &mut *ccx.externs().borrow_mut(), name, cc, llfn_ty, fty); add_argument_attributes(&tys, llfn); llfn diff --git a/src/librustc_trans/trans/glue.rs b/src/librustc_trans/trans/glue.rs index 32b4d14177c2..898d4216e5a6 100644 --- a/src/librustc_trans/trans/glue.rs +++ b/src/librustc_trans/trans/glue.rs @@ -15,11 +15,13 @@ use back::abi; use back::link::*; -use llvm::{ValueRef, get_param}; use llvm; +use llvm::{ValueRef, get_param}; +use metadata::csearch; use middle::lang_items::ExchangeFreeFnLangItem; use middle::subst; use middle::subst::{Subst, Substs}; +use middle::ty::{self, Ty}; use trans::adt; use trans::adt::GetDtorType; // for tcx.dtor_type() use trans::base::*; @@ -28,15 +30,19 @@ use trans::callee; use trans::cleanup; use trans::cleanup::CleanupMethods; use trans::common::*; +use trans::consts; use trans::datum; use trans::debuginfo::DebugLoc; use trans::expr; +use trans::foreign; +use trans::inline; use trans::machine::*; -use trans::type_::Type; +use trans::monomorphize; +use trans::tvec; use trans::type_of::{type_of, sizing_type_of, align_of}; -use middle::ty::{self, Ty}; -use util::ppaux::{ty_to_short_str, Repr}; +use trans::type_::Type; use util::ppaux; +use util::ppaux::{ty_to_short_str, Repr}; use arena::TypedArena; use libc::c_uint; @@ -259,6 +265,44 @@ fn trans_struct_drop_flag<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, } +pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, + did: ast::DefId, + t: Ty<'tcx>, + parent_id: ast::DefId, + substs: &Substs<'tcx>) + -> ValueRef { + let _icx = push_ctxt("trans_res_dtor"); + let did = inline::maybe_instantiate_inline(ccx, did); + + if !substs.types.is_empty() { + assert_eq!(did.krate, ast::LOCAL_CRATE); + + // Since we're in trans we don't care for any region parameters + let substs = ccx.tcx().mk_substs(Substs::erased(substs.types.clone())); + + let (val, _, _) = monomorphize::monomorphic_fn(ccx, did, substs, None); + + val + } else if did.krate == ast::LOCAL_CRATE { + get_item_val(ccx, did.node) + } else { + let tcx = ccx.tcx(); + let name = csearch::get_symbol(&ccx.sess().cstore, did); + let class_ty = ty::lookup_item_type(tcx, parent_id).ty.subst(tcx, substs); + let llty = type_of_dtor(ccx, class_ty); + let dtor_ty = ty::mk_ctor_fn(ccx.tcx(), + did, + &[get_drop_glue_type(ccx, t)], + ty::mk_nil(ccx.tcx())); + foreign::get_extern_fn(ccx, + &mut *ccx.externs().borrow_mut(), + &name[..], + llvm::CCallConv, + llty, + dtor_ty) + } +} + fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: Ty<'tcx>, v0: ValueRef,