Skip to content

Commit 1ef9f8f

Browse files
committed
Generalized some base.rs methods
1 parent 626d3cb commit 1ef9f8f

File tree

9 files changed

+490
-135
lines changed

9 files changed

+490
-135
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use super::CachedModuleCodegen;
3030

3131
use abi;
3232
use back::write::{self, OngoingCodegen};
33-
use llvm::{self, TypeKind, get_param};
33+
use llvm::{self, get_param};
3434
use metadata;
3535
use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
3636
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
@@ -55,7 +55,7 @@ use builder::{Builder, MemFlags};
5555
use callee;
5656
use rustc_mir::monomorphize::collector::{self, MonoItemCollectionMode};
5757
use rustc_mir::monomorphize::item::DefPathBasedNames;
58-
use common::{self, IntPredicate, RealPredicate};
58+
use common::{self, IntPredicate, RealPredicate, TypeKind};
5959
use consts;
6060
use context::CodegenCx;
6161
use debuginfo;
@@ -67,7 +67,6 @@ use monomorphize::partitioning::{self, PartitioningStrategy, CodegenUnit, Codege
6767
use rustc_codegen_utils::symbol_names_test;
6868
use time_graph;
6969
use mono_item::{MonoItem, BaseMonoItemExt, MonoItemExt};
70-
use type_::Type;
7170
use type_of::LayoutLlvmExt;
7271
use rustc::util::nodemap::{FxHashMap, DefIdSet};
7372
use CrateInfo;
@@ -334,21 +333,31 @@ pub fn coerce_unsized_into(
334333
}
335334
}
336335

337-
pub fn cast_shift_expr_rhs(
338-
bx: &Builder<'_, 'll, '_, &'ll Value>, op: hir::BinOpKind, lhs: &'ll Value, rhs: &'ll Value
339-
) -> &'ll Value {
336+
pub fn cast_shift_expr_rhs<'a, 'll: 'a, 'tcx: 'll, Builder : BuilderMethods<'a, 'll, 'tcx>>(
337+
bx: &Builder,
338+
op: hir::BinOpKind,
339+
lhs: <Builder::CodegenCx as Backend>::Value,
340+
rhs: <Builder::CodegenCx as Backend>::Value
341+
) -> <Builder::CodegenCx as Backend>::Value {
340342
cast_shift_rhs(bx, op, lhs, rhs, |a, b| bx.trunc(a, b), |a, b| bx.zext(a, b))
341343
}
342344

343-
fn cast_shift_rhs<'ll, F, G>(bx: &Builder<'_, 'll, '_, &'ll Value>,
344-
op: hir::BinOpKind,
345-
lhs: &'ll Value,
346-
rhs: &'ll Value,
347-
trunc: F,
348-
zext: G)
349-
-> &'ll Value
350-
where F: FnOnce(&'ll Value, &'ll Type) -> &'ll Value,
351-
G: FnOnce(&'ll Value, &'ll Type) -> &'ll Value
345+
fn cast_shift_rhs<'a, 'll :'a, 'tcx : 'll, F, G, Builder : BuilderMethods<'a, 'll, 'tcx>>(
346+
bx: &Builder,
347+
op: hir::BinOpKind,
348+
lhs: <Builder::CodegenCx as Backend>::Value,
349+
rhs: <Builder::CodegenCx as Backend>::Value,
350+
trunc: F,
351+
zext: G
352+
) -> <Builder::CodegenCx as Backend>::Value
353+
where F: FnOnce(
354+
<Builder::CodegenCx as Backend>::Value,
355+
<Builder::CodegenCx as Backend>::Type
356+
) -> <Builder::CodegenCx as Backend>::Value,
357+
G: FnOnce(
358+
<Builder::CodegenCx as Backend>::Value,
359+
<Builder::CodegenCx as Backend>::Type
360+
) -> <Builder::CodegenCx as Backend>::Value
352361
{
353362
// Shifts may have any size int on the rhs
354363
if op.is_shift() {
@@ -390,33 +399,33 @@ pub fn call_assume(bx: &Builder<'_, 'll, '_, &'ll Value>, val: &'ll Value) {
390399
bx.call(assume_intrinsic, &[val], None);
391400
}
392401

393-
pub fn from_immediate<'a, 'll: 'a, 'tcx: 'll>(
394-
bx: &Builder<'_ ,'ll, '_, &'ll Value>,
395-
val: &'ll Value
396-
) -> &'ll Value {
402+
pub fn from_immediate<'a, 'll: 'a, 'tcx: 'll, Builder : BuilderMethods<'a, 'll ,'tcx>>(
403+
bx: &Builder,
404+
val: <Builder::CodegenCx as Backend>::Value
405+
) -> <Builder::CodegenCx as Backend>::Value {
397406
if bx.cx().val_ty(val) == bx.cx().type_i1() {
398407
bx.zext(val, bx.cx().type_i8())
399408
} else {
400409
val
401410
}
402411
}
403412

404-
pub fn to_immediate(
405-
bx: &Builder<'_, 'll, '_, &'ll Value>,
406-
val: &'ll Value,
413+
pub fn to_immediate<'a, 'll, 'tcx, Builder : BuilderMethods<'a, 'll, 'tcx>>(
414+
bx: &Builder,
415+
val: <Builder::CodegenCx as Backend>::Value,
407416
layout: layout::TyLayout,
408-
) -> &'ll Value {
417+
) -> <Builder::CodegenCx as Backend>::Value {
409418
if let layout::Abi::Scalar(ref scalar) = layout.abi {
410419
return to_immediate_scalar(bx, val, scalar);
411420
}
412421
val
413422
}
414423

415-
pub fn to_immediate_scalar(
416-
bx: &Builder<'_, 'll, '_, &'ll Value>,
417-
val: &'ll Value,
424+
pub fn to_immediate_scalar<'a, 'll, 'tcx, Builder : BuilderMethods<'a, 'll, 'tcx>>(
425+
bx: &Builder,
426+
val: <Builder::CodegenCx as Backend>::Value,
418427
scalar: &layout::Scalar,
419-
) -> &'ll Value {
428+
) -> <Builder::CodegenCx as Backend>::Value {
420429
if scalar.is_bool() {
421430
return bx.trunc(val, bx.cx().type_i1());
422431
}

src/librustc_codegen_llvm/builder.rs

Lines changed: 4 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, ConstMethods, TypeMethods};
22+
use interfaces::{BuilderMethods, ConstMethods, TypeMethods};
2323
use syntax;
2424

2525
use std::borrow::Cow;
@@ -56,14 +56,6 @@ bitflags! {
5656
}
5757
}
5858

59-
impl Backend for Builder<'a, 'll, 'tcx, &'ll Value> {
60-
type Value = &'ll Value;
61-
type BasicBlock = &'ll BasicBlock;
62-
type Type = &'ll Type;
63-
type TypeKind = llvm::TypeKind;
64-
type Context = &'ll llvm::Context;
65-
}
66-
6759
impl BuilderMethods<'a, 'll, 'tcx>
6860
for Builder<'a, 'll, 'tcx, &'ll Value> {
6961

@@ -1180,7 +1172,7 @@ impl BuilderMethods<'a, 'll, 'tcx>
11801172
let stored_ty = self.cx.val_ty(val);
11811173
let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
11821174

1183-
assert_eq!(self.cx.type_kind(dest_ptr_ty), llvm::TypeKind::Pointer);
1175+
assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
11841176

11851177
if dest_ptr_ty == stored_ptr_ty {
11861178
ptr
@@ -1199,11 +1191,11 @@ impl BuilderMethods<'a, 'll, 'tcx>
11991191
args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
12001192
let mut fn_ty = self.cx.val_ty(llfn);
12011193
// Strip off pointers
1202-
while self.cx.type_kind(fn_ty) == llvm::TypeKind::Pointer {
1194+
while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
12031195
fn_ty = self.cx.element_type(fn_ty);
12041196
}
12051197

1206-
assert!(self.cx.type_kind(fn_ty) == llvm::TypeKind::Function,
1198+
assert!(self.cx.type_kind(fn_ty) == TypeKind::Function,
12071199
"builder::{} not passed a function, but {:?}", typ, fn_ty);
12081200

12091201
let param_tys = self.cx.func_params_types(fn_ty);

src/librustc_codegen_llvm/common.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
//! Code that is useful in various codegen modules.
1414
15-
use llvm::{self, TypeKind};
16-
use llvm::{True, False, Bool, BasicBlock};
15+
use llvm::{self, True, False, Bool, BasicBlock};
1716
use rustc::hir::def_id::DefId;
1817
use rustc::middle::lang_items::LangItem;
1918
use abi;
@@ -133,6 +132,27 @@ pub enum SynchronizationScope {
133132
CrossThread,
134133
}
135134

135+
#[derive(Copy, Clone, PartialEq, Debug)]
136+
pub enum TypeKind {
137+
Void,
138+
Half,
139+
Float,
140+
Double,
141+
X86_FP80,
142+
FP128,
143+
PPc_FP128,
144+
Label,
145+
Integer,
146+
Function,
147+
Struct,
148+
Array,
149+
Pointer,
150+
Vector,
151+
Metadata,
152+
X86_MMX,
153+
Token,
154+
}
155+
136156
/*
137157
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
138158
*
@@ -199,7 +219,6 @@ impl Backend for CodegenCx<'ll, 'tcx, &'ll Value> {
199219
type Value = &'ll Value;
200220
type BasicBlock = &'ll BasicBlock;
201221
type Type = &'ll Type;
202-
type TypeKind = llvm::TypeKind;
203222
type Context = &'ll llvm::Context;
204223
}
205224

src/librustc_codegen_llvm/interfaces/backend.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
use std::fmt::Debug;
1212

1313
pub trait Backend {
14-
type Value : Debug + PartialEq;
14+
type Value : Debug + PartialEq + Copy;
1515
type BasicBlock;
16-
type Type : Debug + PartialEq;
17-
type TypeKind;
16+
type Type : Debug + PartialEq + Copy;
1817
type Context;
1918
}

0 commit comments

Comments
 (0)