Skip to content

Commit 30ec363

Browse files
author
Dylan McKay
committed
Reduce rustc::trans's dependence on pointer width
1 parent 7ebc5e5 commit 30ec363

File tree

4 files changed

+32
-41
lines changed

4 files changed

+32
-41
lines changed

src/librustc_trans/trans/base.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -943,11 +943,8 @@ pub fn call_lifetime_end(cx: Block, ptr: ValueRef) {
943943
pub fn call_memcpy(cx: Block, dst: ValueRef, src: ValueRef, n_bytes: ValueRef, align: u32) {
944944
let _icx = push_ctxt("call_memcpy");
945945
let ccx = cx.ccx();
946-
let key = match &ccx.sess().target.target.target_pointer_width[..] {
947-
"32" => "llvm.memcpy.p0i8.p0i8.i32",
948-
"64" => "llvm.memcpy.p0i8.p0i8.i64",
949-
tws => panic!("Unsupported target word size for memcpy: {}", tws),
950-
};
946+
let ptr_width = &ccx.sess().target.target.target_pointer_width[..];
947+
let key = format!("llvm.memcpy.p0i8.p0i8.i{}", ptr_width);
951948
let memcpy = ccx.get_intrinsic(&key);
952949
let src_ptr = PointerCast(cx, src, Type::i8p(ccx));
953950
let dst_ptr = PointerCast(cx, dst, Type::i8p(ccx));
@@ -996,12 +993,8 @@ fn memfill<'a, 'tcx>(b: &Builder<'a, 'tcx>, llptr: ValueRef, ty: Ty<'tcx>, byte:
996993
let ccx = b.ccx;
997994

998995
let llty = type_of::type_of(ccx, ty);
999-
1000-
let intrinsic_key = match &ccx.sess().target.target.target_pointer_width[..] {
1001-
"32" => "llvm.memset.p0i8.i32",
1002-
"64" => "llvm.memset.p0i8.i64",
1003-
tws => panic!("Unsupported target word size for memset: {}", tws),
1004-
};
996+
let ptr_width = &ccx.sess().target.target.target_pointer_width[..];
997+
let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
1005998

1006999
let llintrinsicfn = ccx.get_intrinsic(&intrinsic_key);
10071000
let llptr = b.pointercast(llptr, Type::i8(ccx).ptr_to());

src/librustc_trans/trans/common.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -833,10 +833,11 @@ pub fn C_u64(ccx: &CrateContext, i: u64) -> ValueRef {
833833
pub fn C_int<I: AsI64>(ccx: &CrateContext, i: I) -> ValueRef {
834834
let v = i.as_i64();
835835

836-
match machine::llbitsize_of_real(ccx, ccx.int_type()) {
837-
32 => assert!(v < (1<<31) && v >= -(1<<31)),
838-
64 => {},
839-
n => panic!("unsupported target size: {}", n)
836+
let bit_size = machine::llbitsize_of_real(ccx, ccx.int_type());
837+
838+
if bit_size < 64 {
839+
// make sure it doesn't overflow
840+
assert!(v < (1<<(bit_size-1)) && v >= -(1<<(bit_size-1)));
840841
}
841842

842843
C_integral(ccx.int_type(), v as u64, true)
@@ -845,10 +846,11 @@ pub fn C_int<I: AsI64>(ccx: &CrateContext, i: I) -> ValueRef {
845846
pub fn C_uint<I: AsU64>(ccx: &CrateContext, i: I) -> ValueRef {
846847
let v = i.as_u64();
847848

848-
match machine::llbitsize_of_real(ccx, ccx.int_type()) {
849-
32 => assert!(v < (1<<32)),
850-
64 => {},
851-
n => panic!("unsupported target size: {}", n)
849+
let bit_size = machine::llbitsize_of_real(ccx, ccx.int_type());
850+
851+
if bit_size < 64 {
852+
// make sure it doesn't overflow
853+
assert!(v < (1<<bit_size));
852854
}
853855

854856
C_integral(ccx.int_type(), v, false)

src/librustc_trans/trans/context.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
560560
self.local.builder.b
561561
}
562562

563-
pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
563+
pub fn get_intrinsic(&self, key: &str) -> ValueRef {
564564
if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
565565
return v;
566566
}
@@ -791,18 +791,18 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
791791
}
792792

793793
/// Declare any llvm intrinsics that you might need
794-
fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef> {
794+
fn declare_intrinsic(ccx: &CrateContext, key: &str) -> Option<ValueRef> {
795795
macro_rules! ifn {
796796
($name:expr, fn() -> $ret:expr) => (
797-
if *key == $name {
797+
if key == $name {
798798
let f = declare::declare_cfn(ccx, $name, Type::func(&[], &$ret),
799799
ccx.tcx().mk_nil());
800800
ccx.intrinsics().borrow_mut().insert($name, f.clone());
801801
return Some(f);
802802
}
803803
);
804804
($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
805-
if *key == $name {
805+
if key == $name {
806806
let f = declare::declare_cfn(ccx, $name, Type::func(&[$($arg),*], &$ret),
807807
ccx.tcx().mk_nil());
808808
ccx.intrinsics().borrow_mut().insert($name, f.clone());
@@ -824,10 +824,13 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
824824
let t_f32 = Type::f32(ccx);
825825
let t_f64 = Type::f64(ccx);
826826

827+
ifn!("llvm.memcpy.p0i8.p0i8.i16", fn(i8p, i8p, t_i16, t_i32, i1) -> void);
827828
ifn!("llvm.memcpy.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
828829
ifn!("llvm.memcpy.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
830+
ifn!("llvm.memmove.p0i8.p0i8.i16", fn(i8p, i8p, t_i16, t_i32, i1) -> void);
829831
ifn!("llvm.memmove.p0i8.p0i8.i32", fn(i8p, i8p, t_i32, t_i32, i1) -> void);
830832
ifn!("llvm.memmove.p0i8.p0i8.i64", fn(i8p, i8p, t_i64, t_i32, i1) -> void);
833+
ifn!("llvm.memset.p0i8.i16", fn(i8p, t_i8, t_i16, t_i32, i1) -> void);
831834
ifn!("llvm.memset.p0i8.i32", fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
832835
ifn!("llvm.memset.p0i8.i64", fn(i8p, t_i8, t_i64, t_i32, i1) -> void);
833836

@@ -942,7 +945,7 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
942945
if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
943946
// The `if key == $name` is already in ifn!
944947
ifn!($name, fn($($arg),*) -> void);
945-
} else if *key == $name {
948+
} else if key == $name {
946949
let f = declare::declare_cfn(ccx, stringify!($cname),
947950
Type::func(&[$($arg),*], &void),
948951
ccx.tcx().mk_nil());
@@ -965,7 +968,7 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
965968
if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
966969
// The `if key == $name` is already in ifn!
967970
ifn!($name, fn($($arg),*) -> $ret);
968-
} else if *key == $name {
971+
} else if key == $name {
969972
let f = declare::declare_cfn(ccx, stringify!($cname),
970973
Type::func(&[$($arg),*], &$ret),
971974
ccx.tcx().mk_nil());

src/librustc_trans/trans/intrinsic.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -932,20 +932,15 @@ fn copy_intrinsic<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
932932
let align = C_i32(ccx, type_of::align_of(ccx, tp_ty) as i32);
933933
let size = machine::llsize_of(ccx, lltp_ty);
934934
let int_size = machine::llbitsize_of_real(ccx, ccx.int_type());
935-
let name = if allow_overlap {
936-
if int_size == 32 {
937-
"llvm.memmove.p0i8.p0i8.i32"
938-
} else {
939-
"llvm.memmove.p0i8.p0i8.i64"
940-
}
935+
936+
let operation = if allow_overlap {
937+
"memmove"
941938
} else {
942-
if int_size == 32 {
943-
"llvm.memcpy.p0i8.p0i8.i32"
944-
} else {
945-
"llvm.memcpy.p0i8.p0i8.i64"
946-
}
939+
"memcpy"
947940
};
948941

942+
let name = format!("llvm.{}.p0i8.p0i8.i{}", operation, int_size);
943+
949944
let dst_ptr = PointerCast(bcx, dst, Type::i8p(ccx));
950945
let src_ptr = PointerCast(bcx, src, Type::i8p(ccx));
951946
let llfn = ccx.get_intrinsic(&name);
@@ -973,11 +968,9 @@ fn memset_intrinsic<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
973968
let lltp_ty = type_of::type_of(ccx, tp_ty);
974969
let align = C_i32(ccx, type_of::align_of(ccx, tp_ty) as i32);
975970
let size = machine::llsize_of(ccx, lltp_ty);
976-
let name = if machine::llbitsize_of_real(ccx, ccx.int_type()) == 32 {
977-
"llvm.memset.p0i8.i32"
978-
} else {
979-
"llvm.memset.p0i8.i64"
980-
};
971+
let int_size = machine::llbitsize_of_real(ccx, ccx.int_type());
972+
973+
let name = format!("llvm.memset.p0i8.i{}", int_size);
981974

982975
let dst_ptr = PointerCast(bcx, dst, Type::i8p(ccx));
983976
let llfn = ccx.get_intrinsic(&name);

0 commit comments

Comments
 (0)