Skip to content

Commit

Permalink
Move get_static from CodegenCx to Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Nov 29, 2018
1 parent ceb29e2 commit d108a91
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 33 deletions.
7 changes: 7 additions & 0 deletions src/librustc_codegen_llvm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use value::Value;
use libc::{c_uint, c_char};
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::layout::{self, Align, Size, TyLayout};
use rustc::hir::def_id::DefId;
use rustc::session::config;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_codegen_ssa::traits::*;
Expand Down Expand Up @@ -1486,6 +1487,12 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
}
}

impl StaticBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
fn get_static(&self, def_id: DefId) -> &'ll Value {
self.cx().get_static(def_id)
}
}

impl Builder<'a, 'll, 'tcx> {
fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
if self.cx.sess().opts.optimize == config::OptLevel::No {
Expand Down
56 changes: 28 additions & 28 deletions src/librustc_codegen_llvm/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,35 +203,8 @@ impl CodegenCx<'ll, 'tcx> {
gv
}
}
}

impl StaticMethods for CodegenCx<'ll, 'tcx> {
fn static_addr_of(
&self,
cv: &'ll Value,
align: Align,
kind: Option<&str>,
) -> &'ll Value {
if let Some(&gv) = self.const_globals.borrow().get(&cv) {
unsafe {
// Upgrade the alignment in cases where the same constant is used with different
// alignment requirements
let llalign = align.bytes() as u32;
if llalign > llvm::LLVMGetAlignment(gv) {
llvm::LLVMSetAlignment(gv, llalign);
}
}
return gv;
}
let gv = self.static_addr_of_mut(cv, align, kind);
unsafe {
llvm::LLVMSetGlobalConstant(gv, True);
}
self.const_globals.borrow_mut().insert(cv, gv);
gv
}

fn get_static(&self, def_id: DefId) -> &'ll Value {
crate fn get_static(&self, def_id: DefId) -> &'ll Value {
let instance = Instance::mono(self.tcx, def_id);
if let Some(&g) = self.instances.borrow().get(&instance) {
return g;
Expand Down Expand Up @@ -351,6 +324,33 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> {
self.instances.borrow_mut().insert(instance, g);
g
}
}

impl StaticMethods for CodegenCx<'ll, 'tcx> {
fn static_addr_of(
&self,
cv: &'ll Value,
align: Align,
kind: Option<&str>,
) -> &'ll Value {
if let Some(&gv) = self.const_globals.borrow().get(&cv) {
unsafe {
// Upgrade the alignment in cases where the same constant is used with different
// alignment requirements
let llalign = align.bytes() as u32;
if llalign > llvm::LLVMGetAlignment(gv) {
llvm::LLVMSetAlignment(gv, llalign);
}
}
return gv;
}
let gv = self.static_addr_of_mut(cv, align, kind);
unsafe {
llvm::LLVMSetGlobalConstant(gv, True);
}
self.const_globals.borrow_mut().insert(cv, gv);
gv
}

fn codegen_static(
&self,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ fn codegen_msvc_try(
catchswitch.add_handler(cs, catchpad.llbb());

let tydesc = match bx.tcx().lang_items().msvc_try_filter() {
Some(did) => bx.cx().get_static(did),
Some(did) => bx.get_static(did),
None => bug!("msvc_try_filter not defined"),
};
let funclet = catchpad.catch_pad(cs, &[tydesc, bx.const_i32(0), slot]);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mir/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
mir::Place::Static(box mir::Static { def_id, ty }) => {
let layout = cx.layout_of(self.monomorphize(&ty));
PlaceRef::new_sized(cx.get_static(def_id), layout, layout.align.abi)
PlaceRef::new_sized(bx.get_static(def_id), layout, layout.align.abi)
},
mir::Place::Projection(box mir::Projection {
ref base,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_codegen_ssa/traits/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::asm::AsmBuilderMethods;
use super::debuginfo::DebugInfoBuilderMethods;
use super::intrinsic::IntrinsicCallMethods;
use super::type_::ArgTypeMethods;
use super::HasCodegen;
use super::{HasCodegen, StaticBuilderMethods};
use common::{AtomicOrdering, AtomicRmwBinOp, IntPredicate, RealPredicate, SynchronizationScope};
use mir::operand::OperandRef;
use mir::place::PlaceRef;
Expand All @@ -40,6 +40,7 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
+ AbiBuilderMethods<'tcx>
+ IntrinsicCallMethods<'tcx>
+ AsmBuilderMethods<'tcx>
+ StaticBuilderMethods<'tcx>
{
fn new_block<'b>(cx: &'a Self::CodegenCx, llfn: Self::Value, name: &'b str) -> Self;
fn with_cx(cx: &'a Self::CodegenCx) -> Self;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};
pub use self::declare::{DeclareMethods, PreDefineMethods};
pub use self::intrinsic::IntrinsicCallMethods;
pub use self::misc::MiscMethods;
pub use self::statics::StaticMethods;
pub use self::statics::{StaticMethods, StaticBuilderMethods};
pub use self::type_::{
ArgTypeMethods, BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods, TypeMethods,
};
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_codegen_ssa/traits/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use rustc::ty::layout::Align;

pub trait StaticMethods: BackendTypes {
fn static_addr_of(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value;
fn get_static(&self, def_id: DefId) -> Self::Value;
fn codegen_static(&self, def_id: DefId, is_mutable: bool);
}

pub trait StaticBuilderMethods<'tcx>: BackendTypes {
fn get_static(&self, def_id: DefId) -> Self::Value;
}

0 comments on commit d108a91

Please sign in to comment.