Skip to content

Commit a229789

Browse files
committed
Added StaticMethods trait
1 parent 8d8eba1 commit a229789

File tree

10 files changed

+385
-342
lines changed

10 files changed

+385
-342
lines changed

src/librustc_codegen_llvm/consts.rs

Lines changed: 316 additions & 308 deletions
Large diffs are not rendered by default.

src/librustc_codegen_llvm/interfaces/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ mod backend;
1313
mod consts;
1414
mod type_;
1515
mod intrinsic;
16+
mod statics;
1617

1718
pub use self::builder::BuilderMethods;
1819
pub use self::backend::Backend;
1920
pub use self::consts::ConstMethods;
2021
pub use self::type_::{TypeMethods, BaseTypeMethods, DerivedTypeMethods};
2122
pub use self::intrinsic::{IntrinsicMethods, BaseIntrinsicMethods, DerivedIntrinsicMethods};
23+
pub use self::statics::StaticMethods;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use rustc::ty::layout::Align;
12+
use rustc::hir::def_id::DefId;
13+
use super::backend::Backend;
14+
15+
pub trait StaticMethods<'tcx> : Backend {
16+
fn static_ptrcast(&self, val: Self::Value, ty: Self::Type) -> Self::Value;
17+
fn static_bitcast(&self, val: Self::Value, ty: Self::Type) -> Self::Value;
18+
fn static_addr_of_mut(
19+
&self,
20+
cv: Self::Value,
21+
align: Align,
22+
kind: Option<&str>,
23+
) -> Self::Value;
24+
fn static_addr_of(
25+
&self,
26+
cv: Self::Value,
27+
align: Align,
28+
kind: Option<&str>,
29+
) -> Self::Value;
30+
fn get_static(&self, def_id: DefId) -> Self::Value;
31+
fn codegen_static(
32+
&self,
33+
def_id: DefId,
34+
is_mutable: bool,
35+
);
36+
}

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use syntax::symbol::Symbol;
3131
use builder::Builder;
3232
use value::Value;
3333

34-
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, DerivedIntrinsicMethods};
34+
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, DerivedIntrinsicMethods, StaticMethods};
3535

3636
use rustc::session::Session;
3737
use syntax_pos::Span;
@@ -845,7 +845,7 @@ fn codegen_msvc_try(
845845

846846
let tcx = cx.tcx;
847847
let tydesc = match tcx.lang_items().msvc_try_filter() {
848-
Some(did) => ::consts::get_static(cx, did),
848+
Some(did) => cx.get_static(did),
849849
None => bug!("msvc_try_filter not defined"),
850850
};
851851
let tok = catchpad.catch_pad(cs, &[tydesc, cx.const_i32(0), slot]);

src/librustc_codegen_llvm/meth.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ use abi::{FnType, FnTypeExt};
1212
use callee;
1313
use context::CodegenCx;
1414
use builder::Builder;
15-
use consts;
1615
use monomorphize;
1716
use value::Value;
1817

19-
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods};
18+
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, StaticMethods};
2019

2120
use rustc::ty::{self, Ty};
2221
use rustc::ty::layout::HasDataLayout;
@@ -120,7 +119,7 @@ pub fn get_vtable(
120119

121120
let vtable_const = cx.const_struct(&components, false);
122121
let align = cx.data_layout().pointer_align;
123-
let vtable = consts::addr_of(cx, vtable_const, align, Some("vtable"));
122+
let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
124123

125124
debuginfo::create_vtable_metadata(cx, ty, vtable);
126125

src/librustc_codegen_llvm/mir/block.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ use base;
1919
use callee;
2020
use builder::{Builder, MemFlags};
2121
use common::{self, IntPredicate};
22-
use consts;
2322
use meth;
2423
use monomorphize;
2524
use type_of::LayoutLlvmExt;
2625
use type_::Type;
2726
use value::Value;
2827

29-
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, DerivedIntrinsicMethods};
28+
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, DerivedIntrinsicMethods, StaticMethods};
3029

3130
use syntax::symbol::Symbol;
3231
use syntax_pos::Pos;
@@ -378,10 +377,11 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
378377
let index = self.codegen_operand(&mut bx, index).immediate();
379378

380379
let file_line_col = bx.cx().const_struct(&[filename, line, col], false);
381-
let file_line_col = consts::addr_of(bx.cx(),
382-
file_line_col,
383-
align,
384-
Some("panic_bounds_check_loc"));
380+
let file_line_col = bx.cx().static_addr_of(
381+
file_line_col,
382+
align,
383+
Some("panic_bounds_check_loc")
384+
);
385385
(lang_items::PanicBoundsCheckFnLangItem,
386386
vec![file_line_col, index, len])
387387
}
@@ -393,10 +393,11 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
393393
&[msg_str, filename, line, col],
394394
false
395395
);
396-
let msg_file_line_col = consts::addr_of(bx.cx(),
397-
msg_file_line_col,
398-
align,
399-
Some("panic_loc"));
396+
let msg_file_line_col = bx.cx().static_addr_of(
397+
msg_file_line_col,
398+
align,
399+
Some("panic_loc")
400+
);
400401
(lang_items::PanicFnLangItem,
401402
vec![msg_file_line_col])
402403
}

src/librustc_codegen_llvm/mir/constant.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ use rustc::ty::{self, Ty};
1919
use rustc::ty::layout::{self, HasDataLayout, LayoutOf, Size};
2020
use builder::Builder;
2121
use common::{CodegenCx};
22-
use consts;
2322
use type_of::LayoutLlvmExt;
2423
use type_::Type;
2524
use syntax::ast::Mutability;
2625
use syntax::source_map::Span;
2726
use value::Value;
28-
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods};
27+
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, StaticMethods};
2928

3029
use super::super::callee;
3130
use super::FunctionCx;
@@ -48,7 +47,7 @@ pub fn scalar_to_llvm(
4847
if layout.value == layout::Pointer {
4948
unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
5049
} else {
51-
consts::bitcast(llval, llty)
50+
cx.static_bitcast(llval, llty)
5251
}
5352
},
5453
Scalar::Ptr(ptr) => {
@@ -57,29 +56,29 @@ pub fn scalar_to_llvm(
5756
Some(AllocType::Memory(alloc)) => {
5857
let init = const_alloc_to_llvm(cx, alloc);
5958
if alloc.mutability == Mutability::Mutable {
60-
consts::addr_of_mut(cx, init, alloc.align, None)
59+
cx.static_addr_of_mut(init, alloc.align, None)
6160
} else {
62-
consts::addr_of(cx, init, alloc.align, None)
61+
cx.static_addr_of(init, alloc.align, None)
6362
}
6463
}
6564
Some(AllocType::Function(fn_instance)) => {
6665
callee::get_fn(cx, fn_instance)
6766
}
6867
Some(AllocType::Static(def_id)) => {
6968
assert!(cx.tcx.is_static(def_id).is_some());
70-
consts::get_static(cx, def_id)
69+
cx.get_static(def_id)
7170
}
7271
None => bug!("missing allocation {:?}", ptr.alloc_id),
7372
};
7473
let llval = unsafe { llvm::LLVMConstInBoundsGEP(
75-
consts::bitcast(base_addr, cx.type_i8p()),
74+
cx.static_bitcast(base_addr, cx.type_i8p()),
7675
&cx.const_usize(ptr.offset.bytes()),
7776
1,
7877
) };
7978
if layout.value != layout::Pointer {
8079
unsafe { llvm::LLVMConstPtrToInt(llval, llty) }
8180
} else {
82-
consts::bitcast(llval, llty)
81+
cx.static_bitcast(llval, llty)
8382
}
8483
}
8584
}

src/librustc_codegen_llvm/mir/place.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ use rustc::mir::tcx::PlaceTy;
1616
use base;
1717
use builder::Builder;
1818
use common::{CodegenCx, IntPredicate};
19-
use consts;
2019
use type_of::LayoutLlvmExt;
2120
use value::Value;
2221
use glue;
2322
use mir::constant::const_alloc_to_llvm;
2423

25-
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, DerivedIntrinsicMethods};
24+
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, DerivedIntrinsicMethods, StaticMethods};
2625

2726
use super::{FunctionCx, LocalRef};
2827
use super::operand::{OperandRef, OperandValue};
@@ -64,14 +63,14 @@ impl PlaceRef<'tcx, &'ll Value> {
6463
offset: Size,
6564
) -> PlaceRef<'tcx, &'ll Value> {
6665
let init = const_alloc_to_llvm(bx.cx(), alloc);
67-
let base_addr = consts::addr_of(bx.cx(), init, layout.align, None);
66+
let base_addr = bx.cx().static_addr_of(init, layout.align, None);
6867

6968
let llval = unsafe { LLVMConstInBoundsGEP(
70-
consts::bitcast(base_addr, bx.cx().type_i8p()),
69+
bx.cx().static_bitcast(base_addr, bx.cx().type_i8p()),
7170
&bx.cx().const_usize(offset.bytes()),
7271
1,
7372
)};
74-
let llval = consts::bitcast(llval, bx.cx().type_ptr_to(layout.llvm_type(bx.cx())));
73+
let llval = bx.cx().static_bitcast(llval, bx.cx().type_ptr_to(layout.llvm_type(bx.cx())));
7574
PlaceRef::new_sized(llval, layout, alloc.align)
7675
}
7776

@@ -492,7 +491,7 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
492491
}
493492
mir::Place::Static(box mir::Static { def_id, ty }) => {
494493
let layout = cx.layout_of(self.monomorphize(&ty));
495-
PlaceRef::new_sized(consts::get_static(cx, def_id), layout, layout.align)
494+
PlaceRef::new_sized(cx.get_static(def_id), layout, layout.align)
496495
},
497496
mir::Place::Projection(box mir::Projection {
498497
ref base,

src/librustc_codegen_llvm/mir/rvalue.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use base;
2020
use builder::Builder;
2121
use callee;
2222
use common::{self, IntPredicate, RealPredicate};
23-
use consts;
2423
use monomorphize;
2524
use type_::Type;
2625
use type_of::LayoutLlvmExt;
@@ -841,7 +840,7 @@ fn cast_int_to_float(bx: &Builder<'_, 'll, '_, &'ll Value>,
841840
let max = bx.cx().const_uint_big(int_ty, MAX_F32_PLUS_HALF_ULP);
842841
let overflow = bx.icmp(IntPredicate::IntUGE, x, max);
843842
let infinity_bits = bx.cx().const_u32(ieee::Single::INFINITY.to_bits() as u32);
844-
let infinity = consts::bitcast(infinity_bits, float_ty);
843+
let infinity = bx.bitcast(infinity_bits, float_ty);
845844
bx.select(overflow, infinity, bx.uitofp(x, float_ty))
846845
} else {
847846
if signed {
@@ -922,7 +921,7 @@ fn cast_float_to_int(bx: &Builder<'_, 'll, '_, &'ll Value>,
922921
64 => bx.cx().const_u64(bits as u64),
923922
n => bug!("unsupported float width {}", n),
924923
};
925-
consts::bitcast(bits_llval, float_ty)
924+
bx.bitcast(bits_llval, float_ty)
926925
};
927926
let (f_min, f_max) = match bx.cx().float_width(float_ty) {
928927
32 => compute_clamp_bounds_single(signed, int_ty),

src/librustc_codegen_llvm/mono_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use asm;
1818
use attributes;
1919
use base;
20-
use consts;
2120
use context::CodegenCx;
2221
use declare;
2322
use llvm;
@@ -31,6 +30,7 @@ use rustc::ty::TypeFoldable;
3130
use rustc::ty::layout::LayoutOf;
3231
use std::fmt;
3332
use value::Value;
33+
use interfaces::StaticMethods;
3434

3535
pub use rustc::mir::mono::MonoItem;
3636

@@ -55,7 +55,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
5555
bug!("Expected Def::Static for {:?}, found nothing", def_id)
5656
}
5757
};
58-
consts::codegen_static(&cx, def_id, is_mutable);
58+
cx.codegen_static(def_id, is_mutable);
5959
}
6060
MonoItem::GlobalAsm(node_id) => {
6161
let item = cx.tcx.hir.expect_item(node_id);

0 commit comments

Comments
 (0)