Skip to content

Commit c2739a9

Browse files
committed
Traitification of common.rs methods
1 parent 37badad commit c2739a9

File tree

24 files changed

+480
-391
lines changed

24 files changed

+480
-391
lines changed

src/librustc_codegen_llvm/abi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
use llvm::{self, AttributePlace};
1212
use base;
1313
use builder::{Builder, MemFlags};
14-
use common::{ty_fn_sig, C_usize};
14+
use common::ty_fn_sig;
1515
use context::CodegenCx;
1616
use mir::place::PlaceRef;
1717
use mir::operand::OperandValue;
1818
use type_::Type;
1919
use type_of::{LayoutLlvmExt, PointerKind};
2020
use value::Value;
2121

22-
use interfaces::BuilderMethods;
22+
use interfaces::{BuilderMethods, CommonMethods};
2323

2424
use rustc_target::abi::{LayoutOf, Size, TyLayout};
2525
use rustc::ty::{self, Ty};
@@ -242,7 +242,7 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
242242
base::call_memcpy(bx,
243243
bx.pointercast(dst.llval, Type::i8p(cx)),
244244
bx.pointercast(llscratch, Type::i8p(cx)),
245-
C_usize(cx, self.layout.size.bytes()),
245+
CodegenCx::c_usize(cx, self.layout.size.bytes()),
246246
self.layout.align.min(scratch_align),
247247
MemFlags::empty());
248248

src/librustc_codegen_llvm/asm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
// except according to those terms.
1010

1111
use llvm;
12-
use common::*;
12+
use context::CodegenCx;
1313
use type_::Type;
1414
use type_of::LayoutLlvmExt;
1515
use builder::Builder;
1616
use value::Value;
1717

1818
use rustc::hir;
19-
use interfaces::BuilderMethods;
19+
use interfaces::{BuilderMethods, CommonMethods};
2020

2121
use mir::place::PlaceRef;
2222
use mir::operand::OperandValue;
@@ -107,7 +107,7 @@ pub fn codegen_inline_asm(
107107
let kind = llvm::LLVMGetMDKindIDInContext(bx.cx.llcx,
108108
key.as_ptr() as *const c_char, key.len() as c_uint);
109109

110-
let val: &'ll Value = C_i32(bx.cx, ia.ctxt.outer().as_u32() as i32);
110+
let val: &'ll Value = CodegenCx::c_i32(bx.cx, ia.ctxt.outer().as_u32() as i32);
111111

112112
llvm::LLVMSetMetadata(r, kind,
113113
llvm::LLVMMDNodeInContext(bx.cx.llcx, &val, 1));

src/librustc_codegen_llvm/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ fn link_natively(sess: &Session,
756756
// with some thread pool working in the background. It seems that no one
757757
// currently knows a fix for this so in the meantime we're left with this...
758758
info!("{:?}", &cmd);
759-
let retry_on_segfault = env::var("RUSTC_RETRY_LINKER_ON_SEGFAULT").is_ok();
759+
let retry_on_segfault = env::var("RUSTc_RETRY_LINKER_ON_SEGFAULT").is_ok();
760760
let mut prog;
761761
let mut i = 0;
762762
loop {

src/librustc_codegen_llvm/back/write.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ use syntax::ext::hygiene::Mark;
4444
use syntax_pos::MultiSpan;
4545
use syntax_pos::symbol::Symbol;
4646
use type_::Type;
47-
use context::{is_pie_binary, get_reloc_model};
48-
use common::{C_bytes_in_context, val_ty};
47+
use context::{is_pie_binary, get_reloc_model, CodegenCx};
48+
use interfaces::CommonMethods;
4949
use jobserver::{Client, Acquired};
5050
use rustc_demangle;
5151

@@ -884,10 +884,10 @@ unsafe fn embed_bitcode(cgcx: &CodegenContext,
884884
llcx: &llvm::Context,
885885
llmod: &llvm::Module,
886886
bitcode: Option<&[u8]>) {
887-
let llconst = C_bytes_in_context(llcx, bitcode.unwrap_or(&[]));
887+
let llconst = CodegenCx::c_bytes_in_context(llcx, bitcode.unwrap_or(&[]));
888888
let llglobal = llvm::LLVMAddGlobal(
889889
llmod,
890-
val_ty(llconst),
890+
CodegenCx::val_ty(llconst),
891891
"rustc.embedded.module\0".as_ptr() as *const _,
892892
);
893893
llvm::LLVMSetInitializer(llglobal, llconst);
@@ -904,10 +904,10 @@ unsafe fn embed_bitcode(cgcx: &CodegenContext,
904904
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
905905
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
906906

907-
let llconst = C_bytes_in_context(llcx, &[]);
907+
let llconst = CodegenCx::c_bytes_in_context(llcx, &[]);
908908
let llglobal = llvm::LLVMAddGlobal(
909909
llmod,
910-
val_ty(llconst),
910+
CodegenCx::val_ty(llconst),
911911
"rustc.embedded.cmdline\0".as_ptr() as *const _,
912912
);
913913
llvm::LLVMSetInitializer(llglobal, llconst);

src/librustc_codegen_llvm/base.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ use mir::place::PlaceRef;
5252
use attributes;
5353
use builder::{Builder, MemFlags};
5454
use callee;
55-
use common::{C_bool, C_bytes_in_context, C_i32, C_usize};
5655
use rustc_mir::monomorphize::collector::{self, MonoItemCollectionMode};
5756
use rustc_mir::monomorphize::item::DefPathBasedNames;
58-
use common::{self, C_struct_in_context, C_array, val_ty, IntPredicate, RealPredicate};
57+
use common::{self, IntPredicate, RealPredicate};
5958
use consts;
6059
use context::CodegenCx;
6160
use debuginfo;
@@ -74,7 +73,7 @@ use CrateInfo;
7473
use rustc_data_structures::small_c_str::SmallCStr;
7574
use rustc_data_structures::sync::Lrc;
7675

77-
use interfaces::BuilderMethods;
76+
use interfaces::{BuilderMethods, CommonMethods};
7877

7978
use std::any::Any;
8079
use std::ffi::CString;
@@ -199,7 +198,7 @@ pub fn unsized_info(
199198
let (source, target) = cx.tcx.struct_lockstep_tails(source, target);
200199
match (&source.sty, &target.sty) {
201200
(&ty::Array(_, len), &ty::Slice(_)) => {
202-
C_usize(cx, len.unwrap_usize(cx.tcx))
201+
CodegenCx::c_usize(cx, len.unwrap_usize(cx.tcx))
203202
}
204203
(&ty::Dynamic(..), &ty::Dynamic(..)) => {
205204
// For now, upcasts are limited to changes in marker
@@ -351,8 +350,8 @@ fn cast_shift_rhs<'ll, F, G>(op: hir::BinOpKind,
351350
{
352351
// Shifts may have any size int on the rhs
353352
if op.is_shift() {
354-
let mut rhs_llty = val_ty(rhs);
355-
let mut lhs_llty = val_ty(lhs);
353+
let mut rhs_llty = CodegenCx::val_ty(rhs);
354+
let mut lhs_llty = CodegenCx::val_ty(lhs);
356355
if rhs_llty.kind() == TypeKind::Vector {
357356
rhs_llty = rhs_llty.element_type()
358357
}
@@ -393,7 +392,7 @@ pub fn from_immediate<'a, 'll: 'a, 'tcx: 'll>(
393392
bx: &Builder<'_ ,'ll, '_, &'ll Value>,
394393
val: &'ll Value
395394
) -> &'ll Value {
396-
if val_ty(val) == Type::i1(bx.cx()) {
395+
if CodegenCx::val_ty(val) == Type::i1(bx.cx()) {
397396
bx.zext(val, Type::i8(bx.cx()))
398397
} else {
399398
val
@@ -433,7 +432,7 @@ pub fn call_memcpy<'a, 'll: 'a, 'tcx: 'll>(
433432
if flags.contains(MemFlags::NONTEMPORAL) {
434433
// HACK(nox): This is inefficient but there is no nontemporal memcpy.
435434
let val = bx.load(src, align);
436-
let ptr = bx.pointercast(dst, val_ty(val).ptr_to());
435+
let ptr = bx.pointercast(dst, CodegenCx::val_ty(val).ptr_to());
437436
bx.store_with_flags(val, ptr, align, flags);
438437
return;
439438
}
@@ -444,8 +443,8 @@ pub fn call_memcpy<'a, 'll: 'a, 'tcx: 'll>(
444443
let src_ptr = bx.pointercast(src, Type::i8p(cx));
445444
let dst_ptr = bx.pointercast(dst, Type::i8p(cx));
446445
let size = bx.intcast(n_bytes, cx.isize_ty, false);
447-
let align = C_i32(cx, align.abi() as i32);
448-
let volatile = C_bool(cx, flags.contains(MemFlags::VOLATILE));
446+
let align = CodegenCx::c_i32(cx, align.abi() as i32);
447+
let volatile = CodegenCx::c_bool(cx, flags.contains(MemFlags::VOLATILE));
449448
bx.call(memcpy, &[dst_ptr, src_ptr, size, align, volatile], None);
450449
}
451450

@@ -462,7 +461,7 @@ pub fn memcpy_ty<'a, 'll: 'a, 'tcx: 'll>(
462461
return;
463462
}
464463

465-
call_memcpy(bx, dst, src, C_usize(bx.cx(), size), align, flags);
464+
call_memcpy(bx, dst, src, CodegenCx::c_usize(bx.cx(), size), align, flags);
466465
}
467466

468467
pub fn call_memset(
@@ -476,7 +475,7 @@ pub fn call_memset(
476475
let ptr_width = &bx.cx.sess().target.target.target_pointer_width;
477476
let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
478477
let llintrinsicfn = bx.cx.get_intrinsic(&intrinsic_key);
479-
let volatile = C_bool(bx.cx, volatile);
478+
let volatile = CodegenCx::c_bool(bx.cx, volatile);
480479
bx.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None)
481480
}
482481

@@ -654,12 +653,12 @@ fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
654653
DeflateEncoder::new(&mut compressed, Compression::fast())
655654
.write_all(&metadata.raw_data).unwrap();
656655

657-
let llmeta = C_bytes_in_context(metadata_llcx, &compressed);
658-
let llconst = C_struct_in_context(metadata_llcx, &[llmeta], false);
656+
let llmeta = CodegenCx::c_bytes_in_context(metadata_llcx, &compressed);
657+
let llconst = CodegenCx::c_struct_in_context(metadata_llcx, &[llmeta], false);
659658
let name = exported_symbols::metadata_symbol_name(tcx);
660659
let buf = CString::new(name).unwrap();
661660
let llglobal = unsafe {
662-
llvm::LLVMAddGlobal(metadata_llmod, val_ty(llconst), buf.as_ptr())
661+
llvm::LLVMAddGlobal(metadata_llmod, CodegenCx::val_ty(llconst), buf.as_ptr())
663662
};
664663
unsafe {
665664
llvm::LLVMSetInitializer(llglobal, llconst);
@@ -1247,7 +1246,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12471246
// Run replace-all-uses-with for statics that need it
12481247
for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
12491248
unsafe {
1250-
let bitcast = llvm::LLVMConstPointerCast(new_g, val_ty(old_g));
1249+
let bitcast = llvm::LLVMConstPointerCast(new_g, CodegenCx::val_ty(old_g));
12511250
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
12521251
llvm::LLVMDeleteGlobal(old_g);
12531252
}
@@ -1258,11 +1257,11 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12581257
if !cx.used_statics.borrow().is_empty() {
12591258
let name = const_cstr!("llvm.used");
12601259
let section = const_cstr!("llvm.metadata");
1261-
let array = C_array(Type::i8(&cx).ptr_to(), &*cx.used_statics.borrow());
1260+
let array = CodegenCx::c_array(Type::i8(&cx).ptr_to(), &*cx.used_statics.borrow());
12621261

12631262
unsafe {
12641263
let g = llvm::LLVMAddGlobal(cx.llmod,
1265-
val_ty(array),
1264+
CodegenCx::val_ty(array),
12661265
name.as_ptr());
12671266
llvm::LLVMSetInitializer(g, array);
12681267
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);

src/librustc_codegen_llvm/builder.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
1212
use llvm::{self, False, OperandBundleDef, BasicBlock};
1313
use common::{self, *};
14+
use context::CodegenCx;
1415
use type_;
1516
use value::Value;
1617
use libc::{c_uint, c_char};
1718
use rustc::ty::TyCtxt;
1819
use rustc::ty::layout::{Align, Size};
1920
use rustc::session::{config, Session};
2021
use rustc_data_structures::small_c_str::SmallCStr;
21-
use interfaces::{BuilderMethods, Backend};
22+
use interfaces::{BuilderMethods, Backend, CommonMethods};
2223
use syntax;
2324

2425
use std::borrow::Cow;
@@ -59,6 +60,7 @@ impl Backend for Builder<'a, 'll, 'tcx, &'ll Value> {
5960
type Value = &'ll Value;
6061
type BasicBlock = &'ll BasicBlock;
6162
type Type = &'ll type_::Type;
63+
type Context = &'ll llvm::Context;
6264
}
6365

6466
impl BuilderMethods<'a, 'll, 'tcx>
@@ -537,10 +539,10 @@ impl BuilderMethods<'a, 'll, 'tcx>
537539

538540
fn range_metadata(&self, load: &'ll Value, range: Range<u128>) {
539541
unsafe {
540-
let llty = val_ty(load);
542+
let llty = CodegenCx::val_ty(load);
541543
let v = [
542-
C_uint_big(llty, range.start),
543-
C_uint_big(llty, range.end)
544+
CodegenCx::c_uint_big(llty, range.start),
545+
CodegenCx::c_uint_big(llty, range.end)
544546
];
545547

546548
llvm::LLVMSetMetadata(load, llvm::MD_range as c_uint,
@@ -605,7 +607,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
605607
// *always* point to a metadata value of the integer 1.
606608
//
607609
// [1]: http://llvm.org/docs/LangRef.html#store-instruction
608-
let one = C_i32(self.cx, 1);
610+
let one = CodegenCx::c_i32(self.cx, 1);
609611
let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
610612
llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
611613
}
@@ -771,7 +773,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
771773

772774
let argtys = inputs.iter().map(|v| {
773775
debug!("Asm Input Type: {:?}", *v);
774-
val_ty(*v)
776+
CodegenCx::val_ty(*v)
775777
}).collect::<Vec<_>>();
776778

777779
debug!("Asm Output Type: {:?}", output);
@@ -844,11 +846,11 @@ impl BuilderMethods<'a, 'll, 'tcx>
844846

845847
fn vector_splat(&self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
846848
unsafe {
847-
let elt_ty = val_ty(elt);
849+
let elt_ty = CodegenCx::val_ty(elt);
848850
let undef = llvm::LLVMGetUndef(type_::Type::vector(elt_ty, num_elts as u64));
849-
let vec = self.insert_element(undef, elt, C_i32(self.cx, 0));
851+
let vec = self.insert_element(undef, elt, CodegenCx::c_i32(self.cx, 0));
850852
let vec_i32_ty = type_::Type::vector(type_::Type::i32(self.cx), num_elts as u64);
851-
self.shuffle_vector(vec, undef, C_null(vec_i32_ty))
853+
self.shuffle_vector(vec, undef, CodegenCx::c_null(vec_i32_ty))
852854
}
853855
}
854856

@@ -1155,8 +1157,8 @@ impl BuilderMethods<'a, 'll, 'tcx>
11551157
fn check_store<'b>(&self,
11561158
val: &'ll Value,
11571159
ptr: &'ll Value) -> &'ll Value {
1158-
let dest_ptr_ty = val_ty(ptr);
1159-
let stored_ty = val_ty(val);
1160+
let dest_ptr_ty = CodegenCx::val_ty(ptr);
1161+
let stored_ty = CodegenCx::val_ty(val);
11601162
let stored_ptr_ty = stored_ty.ptr_to();
11611163

11621164
assert_eq!(dest_ptr_ty.kind(), llvm::TypeKind::Pointer);
@@ -1176,7 +1178,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
11761178
typ: &str,
11771179
llfn: &'ll Value,
11781180
args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
1179-
let mut fn_ty = val_ty(llfn);
1181+
let mut fn_ty = CodegenCx::val_ty(llfn);
11801182
// Strip off pointers
11811183
while fn_ty.kind() == llvm::TypeKind::Pointer {
11821184
fn_ty = fn_ty.element_type();
@@ -1188,7 +1190,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
11881190
let param_tys = fn_ty.func_params();
11891191

11901192
let all_args_match = param_tys.iter()
1191-
.zip(args.iter().map(|&v| val_ty(v)))
1193+
.zip(args.iter().map(|&v| CodegenCx::val_ty(v)))
11921194
.all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
11931195

11941196
if all_args_match {
@@ -1199,7 +1201,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
11991201
.zip(args.iter())
12001202
.enumerate()
12011203
.map(|(i, (expected_ty, &actual_val))| {
1202-
let actual_ty = val_ty(actual_val);
1204+
let actual_ty = CodegenCx::val_ty(actual_val);
12031205
if expected_ty != actual_ty {
12041206
debug!("Type mismatch in function call of {:?}. \
12051207
Expected {:?} for param {}, got {:?}; injecting bitcast",
@@ -1243,7 +1245,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
12431245
let lifetime_intrinsic = self.cx.get_intrinsic(intrinsic);
12441246

12451247
let ptr = self.pointercast(ptr, type_::Type::i8p(self.cx));
1246-
self.call(lifetime_intrinsic, &[C_u64(self.cx, size), ptr], None);
1248+
self.call(lifetime_intrinsic, &[CodegenCx::c_u64(self.cx, size), ptr], None);
12471249
}
12481250

12491251
fn call(&self, llfn: &'ll Value, args: &[&'ll Value],

src/librustc_codegen_llvm/callee.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use llvm;
2222
use monomorphize::Instance;
2323
use type_of::LayoutLlvmExt;
2424
use value::Value;
25+
use interfaces::CommonMethods;
2526

2627
use rustc::hir::def_id::DefId;
2728
use rustc::ty::{self, TypeFoldable};
@@ -83,7 +84,7 @@ pub fn get_fn(
8384
// This can occur on either a crate-local or crate-external
8485
// reference. It also occurs when testing libcore and in some
8586
// other weird situations. Annoying.
86-
if common::val_ty(llfn) != llptrty {
87+
if CodegenCx::val_ty(llfn) != llptrty {
8788
debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
8889
consts::ptrcast(llfn, llptrty)
8990
} else {
@@ -92,7 +93,7 @@ pub fn get_fn(
9293
}
9394
} else {
9495
let llfn = declare::declare_fn(cx, &sym, fn_ty);
95-
assert_eq!(common::val_ty(llfn), llptrty);
96+
assert_eq!(CodegenCx::val_ty(llfn), llptrty);
9697
debug!("get_fn: not casting pointer!");
9798

9899
if instance.def.is_inline(tcx) {

0 commit comments

Comments
 (0)