Skip to content

Commit f3d1037

Browse files
committed
Traitification of type_ methods
The methods are now attached to CodegenCx instead of Type
1 parent c6affbe commit f3d1037

File tree

20 files changed

+408
-408
lines changed

20 files changed

+408
-408
lines changed

src/librustc_codegen_llvm/abi.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use type_::Type;
1919
use type_of::{LayoutLlvmExt, PointerKind};
2020
use value::Value;
2121

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

2424
use rustc_target::abi::{LayoutOf, Size, TyLayout};
2525
use rustc::ty::{self, Ty};
@@ -112,16 +112,16 @@ pub trait LlvmType {
112112
impl LlvmType for Reg {
113113
fn llvm_type(&self, cx: &CodegenCx<'ll, '_, &'ll Value>) -> &'ll Type {
114114
match self.kind {
115-
RegKind::Integer => Type::ix(cx, self.size.bits()),
115+
RegKind::Integer => cx.ix(self.size.bits()),
116116
RegKind::Float => {
117117
match self.size.bits() {
118-
32 => Type::f32(cx),
119-
64 => Type::f64(cx),
118+
32 => cx.f32(),
119+
64 => cx.f64(),
120120
_ => bug!("unsupported float: {:?}", self)
121121
}
122122
}
123123
RegKind::Vector => {
124-
Type::vector(Type::i8(cx), self.size.bytes())
124+
cx.vector(cx.i8(), self.size.bytes())
125125
}
126126
}
127127
}
@@ -145,7 +145,7 @@ impl LlvmType for CastTarget {
145145

146146
// Simplify to array when all chunks are the same size and type
147147
if rem_bytes == 0 {
148-
return Type::array(rest_ll_unit, rest_count);
148+
return cx.array(rest_ll_unit, rest_count);
149149
}
150150
}
151151

@@ -160,10 +160,10 @@ impl LlvmType for CastTarget {
160160
if rem_bytes != 0 {
161161
// Only integers can be really split further.
162162
assert_eq!(self.rest.unit.kind, RegKind::Integer);
163-
args.push(Type::ix(cx, rem_bytes * 8));
163+
args.push(cx.ix(rem_bytes * 8));
164164
}
165165

166-
Type::struct_(cx, &args, false)
166+
cx.struct_(&args, false)
167167
}
168168
}
169169

@@ -212,7 +212,7 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
212212
// uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
213213
let can_store_through_cast_ptr = false;
214214
if can_store_through_cast_ptr {
215-
let cast_dst = bx.pointercast(dst.llval, cast.llvm_type(cx).ptr_to());
215+
let cast_dst = bx.pointercast(dst.llval, cx.ptr_to(cast.llvm_type(cx)));
216216
bx.store(val, cast_dst, self.layout.align);
217217
} else {
218218
// The actual return type is a struct, but the ABI
@@ -240,8 +240,8 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
240240

241241
// ...and then memcpy it to the intended destination.
242242
base::call_memcpy(bx,
243-
bx.pointercast(dst.llval, Type::i8p(cx)),
244-
bx.pointercast(llscratch, Type::i8p(cx)),
243+
bx.pointercast(dst.llval, cx.i8p()),
244+
bx.pointercast(llscratch, cx.i8p()),
245245
cx.c_usize(self.layout.size.bytes()),
246246
self.layout.align.min(scratch_align),
247247
MemFlags::empty());
@@ -605,14 +605,14 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
605605
);
606606

607607
let llreturn_ty = match self.ret.mode {
608-
PassMode::Ignore => Type::void(cx),
608+
PassMode::Ignore => cx.void(),
609609
PassMode::Direct(_) | PassMode::Pair(..) => {
610610
self.ret.layout.immediate_llvm_type(cx)
611611
}
612612
PassMode::Cast(cast) => cast.llvm_type(cx),
613613
PassMode::Indirect(..) => {
614-
llargument_tys.push(self.ret.memory_ty(cx).ptr_to());
615-
Type::void(cx)
614+
llargument_tys.push(cx.ptr_to(self.ret.memory_ty(cx)));
615+
cx.void()
616616
}
617617
};
618618

@@ -638,15 +638,15 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
638638
continue;
639639
}
640640
PassMode::Cast(cast) => cast.llvm_type(cx),
641-
PassMode::Indirect(_, None) => arg.memory_ty(cx).ptr_to(),
641+
PassMode::Indirect(_, None) => cx.ptr_to(arg.memory_ty(cx)),
642642
};
643643
llargument_tys.push(llarg_ty);
644644
}
645645

646646
if self.variadic {
647-
Type::variadic_func(&llargument_tys, llreturn_ty)
647+
cx.variadic_func(&llargument_tys, llreturn_ty)
648648
} else {
649-
Type::func(&llargument_tys, llreturn_ty)
649+
cx.func(&llargument_tys, llreturn_ty)
650650
}
651651
}
652652

src/librustc_codegen_llvm/asm.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010

1111
use llvm;
1212
use context::CodegenCx;
13-
use type_::Type;
1413
use type_of::LayoutLlvmExt;
1514
use builder::Builder;
1615
use value::Value;
1716

1817
use rustc::hir;
19-
use interfaces::{BuilderMethods, CommonMethods};
18+
use interfaces::{BuilderMethods, CommonMethods, TypeMethods};
2019

2120
use mir::place::PlaceRef;
2221
use mir::operand::OperandValue;
@@ -76,9 +75,9 @@ pub fn codegen_inline_asm(
7675
// Depending on how many outputs we have, the return type is different
7776
let num_outputs = output_types.len();
7877
let output_type = match num_outputs {
79-
0 => Type::void(bx.cx()),
78+
0 => bx.cx().void(),
8079
1 => output_types[0],
81-
_ => Type::struct_(bx.cx(), &output_types, false)
80+
_ => bx.cx().struct_(&output_types, false)
8281
};
8382

8483
let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();

src/librustc_codegen_llvm/back/write.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,14 @@ impl CommonWriteMethods for CodegenContext<'ll> {
452452
}
453453
}
454454

455+
impl CodegenContext<'ll> {
456+
pub fn ptr_to(&self, ty: &'ll Type) -> &'ll Type {
457+
unsafe {
458+
llvm::LLVMPointerType(ty, 0)
459+
}
460+
}
461+
}
462+
455463

456464
pub struct DiagnosticHandlers<'a> {
457465
data: *mut (&'a CodegenContext<'a>, &'a Handler),
@@ -2579,7 +2587,7 @@ fn create_msvc_imps(cgcx: &CodegenContext, llcx: &llvm::Context, llmod: &llvm::M
25792587
"\x01__imp_"
25802588
};
25812589
unsafe {
2582-
let i8p_ty = Type::i8p_llcx(llcx);
2590+
let i8p_ty = Type::i8p_llcx(cgcx, llcx);
25832591
let globals = base::iter_globals(llmod)
25842592
.filter(|&val| {
25852593
llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage &&

src/librustc_codegen_llvm/base.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use CrateInfo;
7474
use rustc_data_structures::small_c_str::SmallCStr;
7575
use rustc_data_structures::sync::Lrc;
7676

77-
use interfaces::{BuilderMethods, CommonMethods, CommonWriteMethods};
77+
use interfaces::{BuilderMethods, CommonMethods, CommonWriteMethods, TypeMethods};
7878

7979
use std::any::Any;
8080
use std::ffi::CString;
@@ -235,13 +235,13 @@ pub fn unsize_thin_ptr(
235235
(&ty::RawPtr(ty::TypeAndMut { ty: a, .. }),
236236
&ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
237237
assert!(bx.cx().type_is_sized(a));
238-
let ptr_ty = bx.cx().layout_of(b).llvm_type(bx.cx()).ptr_to();
238+
let ptr_ty = bx.cx().ptr_to(bx.cx().layout_of(b).llvm_type(bx.cx()));
239239
(bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
240240
}
241241
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
242242
let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
243243
assert!(bx.cx().type_is_sized(a));
244-
let ptr_ty = bx.cx().layout_of(b).llvm_type(bx.cx()).ptr_to();
244+
let ptr_ty = bx.cx().ptr_to(bx.cx().layout_of(b).llvm_type(bx.cx()));
245245
(bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
246246
}
247247
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
@@ -354,14 +354,14 @@ fn cast_shift_rhs<'ll, F, G>(bx: &Builder<'_, 'll, '_, &'ll Value>,
354354
if op.is_shift() {
355355
let mut rhs_llty = bx.cx().val_ty(rhs);
356356
let mut lhs_llty = bx.cx().val_ty(lhs);
357-
if rhs_llty.kind() == TypeKind::Vector {
358-
rhs_llty = rhs_llty.element_type()
357+
if bx.cx().kind(rhs_llty) == TypeKind::Vector {
358+
rhs_llty = bx.cx().element_type(rhs_llty)
359359
}
360-
if lhs_llty.kind() == TypeKind::Vector {
361-
lhs_llty = lhs_llty.element_type()
360+
if bx.cx().kind(lhs_llty) == TypeKind::Vector {
361+
lhs_llty = bx.cx().element_type(lhs_llty)
362362
}
363-
let rhs_sz = rhs_llty.int_width();
364-
let lhs_sz = lhs_llty.int_width();
363+
let rhs_sz = bx.cx().int_width(rhs_llty);
364+
let lhs_sz = bx.cx().int_width(lhs_llty);
365365
if lhs_sz < rhs_sz {
366366
trunc(rhs, lhs_llty)
367367
} else if lhs_sz > rhs_sz {
@@ -394,8 +394,8 @@ pub fn from_immediate<'a, 'll: 'a, 'tcx: 'll>(
394394
bx: &Builder<'_ ,'ll, '_, &'ll Value>,
395395
val: &'ll Value
396396
) -> &'ll Value {
397-
if bx.cx().val_ty(val) == Type::i1(bx.cx()) {
398-
bx.zext(val, Type::i8(bx.cx()))
397+
if bx.cx().val_ty(val) == bx.cx().i1() {
398+
bx.zext(val, bx.cx().i8())
399399
} else {
400400
val
401401
}
@@ -418,7 +418,7 @@ pub fn to_immediate_scalar(
418418
scalar: &layout::Scalar,
419419
) -> &'ll Value {
420420
if scalar.is_bool() {
421-
return bx.trunc(val, Type::i1(bx.cx()));
421+
return bx.trunc(val, bx.cx().i1());
422422
}
423423
val
424424
}
@@ -434,16 +434,16 @@ pub fn call_memcpy<'a, 'll: 'a, 'tcx: 'll>(
434434
if flags.contains(MemFlags::NONTEMPORAL) {
435435
// HACK(nox): This is inefficient but there is no nontemporal memcpy.
436436
let val = bx.load(src, align);
437-
let ptr = bx.pointercast(dst, bx.cx().val_ty(val).ptr_to());
437+
let ptr = bx.pointercast(dst, bx.cx().ptr_to(bx.cx().val_ty(val)));
438438
bx.store_with_flags(val, ptr, align, flags);
439439
return;
440440
}
441441
let cx = bx.cx();
442442
let ptr_width = &cx.sess().target.target.target_pointer_width;
443443
let key = format!("llvm.memcpy.p0i8.p0i8.i{}", ptr_width);
444444
let memcpy = cx.get_intrinsic(&key);
445-
let src_ptr = bx.pointercast(src, Type::i8p(cx));
446-
let dst_ptr = bx.pointercast(dst, Type::i8p(cx));
445+
let src_ptr = bx.pointercast(src, cx.i8p());
446+
let dst_ptr = bx.pointercast(dst, cx.i8p());
447447
let size = bx.intcast(n_bytes, cx.isize_ty, false);
448448
let align = cx.c_i32(align.abi() as i32);
449449
let volatile = cx.c_bool(flags.contains(MemFlags::VOLATILE));
@@ -557,7 +557,7 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx<'ll, '_, &'ll Value>) {
557557
use_start_lang_item: bool,
558558
) {
559559
let llfty =
560-
Type::func(&[Type::c_int(cx), Type::i8p(cx).ptr_to()], Type::c_int(cx));
560+
cx.func(&[cx.t_int(), cx.ptr_to(cx.i8p())], cx.t_int());
561561

562562
let main_ret_ty = cx.tcx.fn_sig(rust_main_def_id).output();
563563
// Given that `main()` has no arguments,
@@ -600,15 +600,15 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx<'ll, '_, &'ll Value>) {
600600
start_def_id,
601601
cx.tcx.intern_substs(&[main_ret_ty.into()]),
602602
);
603-
(start_fn, vec![bx.pointercast(rust_main, Type::i8p(cx).ptr_to()),
603+
(start_fn, vec![bx.pointercast(rust_main, cx.ptr_to(cx.i8p())),
604604
arg_argc, arg_argv])
605605
} else {
606606
debug!("using user-defined start fn");
607607
(rust_main, vec![arg_argc, arg_argv])
608608
};
609609

610610
let result = bx.call(start_fn, &args, None);
611-
bx.ret(bx.intcast(result, Type::c_int(cx), true));
611+
bx.ret(bx.intcast(result, cx.t_int(), true));
612612
}
613613
}
614614

@@ -1247,7 +1247,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12471247
if !cx.used_statics.borrow().is_empty() {
12481248
let name = const_cstr!("llvm.used");
12491249
let section = const_cstr!("llvm.metadata");
1250-
let array = cx.c_array(Type::i8(&cx).ptr_to(), &*cx.used_statics.borrow());
1250+
let array = cx.c_array(&cx.ptr_to(cx.i8()), &*cx.used_statics.borrow());
12511251

12521252
unsafe {
12531253
let g = llvm::LLVMAddGlobal(cx.llmod,

src/librustc_codegen_llvm/builder.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc::ty::TyCtxt;
1919
use rustc::ty::layout::{Align, Size};
2020
use rustc::session::{config, Session};
2121
use rustc_data_structures::small_c_str::SmallCStr;
22-
use interfaces::{BuilderMethods, Backend, CommonMethods, CommonWriteMethods};
22+
use interfaces::{BuilderMethods, Backend, CommonMethods, CommonWriteMethods, TypeMethods};
2323
use syntax;
2424

2525
use std::borrow::Cow;
@@ -786,7 +786,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
786786
}).collect::<Vec<_>>();
787787

788788
debug!("Asm Output Type: {:?}", output);
789-
let fty = type_::Type::func(&argtys[..], output);
789+
let fty = &self.cx().func(&argtys[..], output);
790790
unsafe {
791791
// Ask LLVM to verify that the constraints are well-formed.
792792
let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons);
@@ -864,10 +864,10 @@ impl BuilderMethods<'a, 'll, 'tcx>
864864
fn vector_splat(&self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
865865
unsafe {
866866
let elt_ty = self.cx.val_ty(elt);
867-
let undef = llvm::LLVMGetUndef(type_::Type::vector(elt_ty, num_elts as u64));
867+
let undef = llvm::LLVMGetUndef(&self.cx().vector(elt_ty, num_elts as u64));
868868
let vec = self.insert_element(undef, elt, self.cx.c_i32(0));
869-
let vec_i32_ty = type_::Type::vector(type_::Type::i32(self.cx), num_elts as u64);
870-
self.shuffle_vector(vec, undef, self.cx.c_null(vec_i32_ty))
869+
let vec_i32_ty = &self.cx().vector(&self.cx().i32(), num_elts as u64);
870+
self.shuffle_vector(vec, undef, self.cx().c_null(vec_i32_ty))
871871
}
872872
}
873873

@@ -1176,9 +1176,9 @@ impl BuilderMethods<'a, 'll, 'tcx>
11761176
ptr: &'ll Value) -> &'ll Value {
11771177
let dest_ptr_ty = self.cx.val_ty(ptr);
11781178
let stored_ty = self.cx.val_ty(val);
1179-
let stored_ptr_ty = stored_ty.ptr_to();
1179+
let stored_ptr_ty = self.cx.ptr_to(stored_ty);
11801180

1181-
assert_eq!(dest_ptr_ty.kind(), llvm::TypeKind::Pointer);
1181+
assert_eq!(self.cx.kind(dest_ptr_ty), llvm::TypeKind::Pointer);
11821182

11831183
if dest_ptr_ty == stored_ptr_ty {
11841184
ptr
@@ -1197,14 +1197,14 @@ impl BuilderMethods<'a, 'll, 'tcx>
11971197
args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
11981198
let mut fn_ty = self.cx.val_ty(llfn);
11991199
// Strip off pointers
1200-
while fn_ty.kind() == llvm::TypeKind::Pointer {
1201-
fn_ty = fn_ty.element_type();
1200+
while self.cx.kind(fn_ty) == llvm::TypeKind::Pointer {
1201+
fn_ty = self.cx.element_type(fn_ty);
12021202
}
12031203

1204-
assert!(fn_ty.kind() == llvm::TypeKind::Function,
1204+
assert!(self.cx.kind(fn_ty) == llvm::TypeKind::Function,
12051205
"builder::{} not passed a function, but {:?}", typ, fn_ty);
12061206

1207-
let param_tys = fn_ty.func_params();
1207+
let param_tys = self.cx.func_params(fn_ty);
12081208

12091209
let all_args_match = param_tys.iter()
12101210
.zip(args.iter().map(|&v| self.cx().val_ty(v)))
@@ -1261,7 +1261,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
12611261

12621262
let lifetime_intrinsic = self.cx.get_intrinsic(intrinsic);
12631263

1264-
let ptr = self.pointercast(ptr, type_::Type::i8p(self.cx));
1264+
let ptr = self.pointercast(ptr, self.cx.i8p());
12651265
self.call(lifetime_intrinsic, &[self.cx.c_u64(size), ptr], None);
12661266
}
12671267

0 commit comments

Comments
 (0)