Skip to content

Commit 5edbe1f

Browse files
committed
Add Type::int_width for retrieving integer's bit width.
1 parent 3d59a47 commit 5edbe1f

File tree

7 files changed

+41
-77
lines changed

7 files changed

+41
-77
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -847,26 +847,24 @@ pub fn cast_shift_rhs<F, G>(op: ast::BinOp,
847847
G: FnOnce(ValueRef, Type) -> ValueRef,
848848
{
849849
// Shifts may have any size int on the rhs
850-
unsafe {
851-
if ast_util::is_shift_binop(op) {
852-
let mut rhs_llty = val_ty(rhs);
853-
let mut lhs_llty = val_ty(lhs);
854-
if rhs_llty.kind() == Vector { rhs_llty = rhs_llty.element_type() }
855-
if lhs_llty.kind() == Vector { lhs_llty = lhs_llty.element_type() }
856-
let rhs_sz = llvm::LLVMGetIntTypeWidth(rhs_llty.to_ref());
857-
let lhs_sz = llvm::LLVMGetIntTypeWidth(lhs_llty.to_ref());
858-
if lhs_sz < rhs_sz {
859-
trunc(rhs, lhs_llty)
860-
} else if lhs_sz > rhs_sz {
861-
// FIXME (#1877: If shifting by negative
862-
// values becomes not undefined then this is wrong.
863-
zext(rhs, lhs_llty)
864-
} else {
865-
rhs
866-
}
850+
if ast_util::is_shift_binop(op) {
851+
let mut rhs_llty = val_ty(rhs);
852+
let mut lhs_llty = val_ty(lhs);
853+
if rhs_llty.kind() == Vector { rhs_llty = rhs_llty.element_type() }
854+
if lhs_llty.kind() == Vector { lhs_llty = lhs_llty.element_type() }
855+
let rhs_sz = rhs_llty.int_width();
856+
let lhs_sz = lhs_llty.int_width();
857+
if lhs_sz < rhs_sz {
858+
trunc(rhs, lhs_llty)
859+
} else if lhs_sz > rhs_sz {
860+
// FIXME (#1877: If shifting by negative
861+
// values becomes not undefined then this is wrong.
862+
zext(rhs, lhs_llty)
867863
} else {
868864
rhs
869865
}
866+
} else {
867+
rhs
870868
}
871869
}
872870

src/librustc_trans/trans/cabi_aarch64.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#![allow(non_upper_case_globals)]
1212

13-
use llvm;
1413
use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector};
1514
use llvm::{StructRetAttribute, ZExtAttribute};
1615
use trans::cabi::{FnType, ArgType};
@@ -30,11 +29,7 @@ fn align(off: uint, ty: Type) -> uint {
3029

3130
fn ty_align(ty: Type) -> uint {
3231
match ty.kind() {
33-
Integer => {
34-
unsafe {
35-
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
36-
}
37-
}
32+
Integer => ((ty.int_width() as uint) + 7) / 8,
3833
Pointer => 8,
3934
Float => 4,
4035
Double => 8,
@@ -61,11 +56,7 @@ fn ty_align(ty: Type) -> uint {
6156

6257
fn ty_size(ty: Type) -> uint {
6358
match ty.kind() {
64-
Integer => {
65-
unsafe {
66-
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
67-
}
68-
}
59+
Integer => ((ty.int_width() as uint) + 7) / 8,
6960
Pointer => 8,
7061
Float => 4,
7162
Double => 8,

src/librustc_trans/trans/cabi_arm.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#![allow(non_upper_case_globals)]
1212

13-
use llvm;
1413
use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector};
1514
use llvm::{StructRetAttribute, ZExtAttribute};
1615
use trans::cabi::{FnType, ArgType};
@@ -37,11 +36,7 @@ fn align(off: uint, ty: Type, align_fn: TyAlignFn) -> uint {
3736

3837
fn general_ty_align(ty: Type) -> uint {
3938
match ty.kind() {
40-
Integer => {
41-
unsafe {
42-
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
43-
}
44-
}
39+
Integer => ((ty.int_width() as uint) + 7) / 8,
4540
Pointer => 4,
4641
Float => 4,
4742
Double => 8,
@@ -75,11 +70,7 @@ fn general_ty_align(ty: Type) -> uint {
7570
// /iPhoneOSABIReference/Articles/ARMv6FunctionCallingConventions.html
7671
fn ios_ty_align(ty: Type) -> uint {
7772
match ty.kind() {
78-
Integer => {
79-
unsafe {
80-
cmp::min(4, ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8)
81-
}
82-
}
73+
Integer => cmp::min(4, ((ty.int_width() as uint) + 7) / 8),
8374
Pointer => 4,
8475
Float => 4,
8576
Double => 4,
@@ -106,11 +97,7 @@ fn ios_ty_align(ty: Type) -> uint {
10697

10798
fn ty_size(ty: Type, align_fn: TyAlignFn) -> uint {
10899
match ty.kind() {
109-
Integer => {
110-
unsafe {
111-
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
112-
}
113-
}
100+
Integer => ((ty.int_width() as uint) + 7) / 8,
114101
Pointer => 4,
115102
Float => 4,
116103
Double => 8,

src/librustc_trans/trans/cabi_mips.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ fn align(off: uint, ty: Type) -> uint {
3030

3131
fn ty_align(ty: Type) -> uint {
3232
match ty.kind() {
33-
Integer => {
34-
unsafe {
35-
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
36-
}
37-
}
33+
Integer => ((ty.int_width() as uint) + 7) / 8,
3834
Pointer => 4,
3935
Float => 4,
4036
Double => 8,
@@ -61,11 +57,7 @@ fn ty_align(ty: Type) -> uint {
6157

6258
fn ty_size(ty: Type) -> uint {
6359
match ty.kind() {
64-
Integer => {
65-
unsafe {
66-
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
67-
}
68-
}
60+
Integer => ((ty.int_width() as uint) + 7) / 8,
6961
Pointer => 4,
7062
Float => 4,
7163
Double => 8,

src/librustc_trans/trans/cabi_x86_64.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#![allow(non_upper_case_globals)]
1515
use self::RegClass::*;
1616

17-
use llvm;
1817
use llvm::{Integer, Pointer, Float, Double};
1918
use llvm::{Struct, Array, Attribute, Vector};
2019
use llvm::{StructRetAttribute, ByValAttribute, ZExtAttribute};
@@ -94,11 +93,7 @@ fn classify_ty(ty: Type) -> Vec<RegClass> {
9493

9594
fn ty_align(ty: Type) -> uint {
9695
match ty.kind() {
97-
Integer => {
98-
unsafe {
99-
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
100-
}
101-
}
96+
Integer => ((ty.int_width() as uint) + 7) / 8,
10297
Pointer => 8,
10398
Float => 4,
10499
Double => 8,
@@ -125,11 +120,7 @@ fn classify_ty(ty: Type) -> Vec<RegClass> {
125120

126121
fn ty_size(ty: Type) -> uint {
127122
match ty.kind() {
128-
Integer => {
129-
unsafe {
130-
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
131-
}
132-
}
123+
Integer => (ty.int_width() as uint + 7) / 8,
133124
Pointer => 8,
134125
Float => 4,
135126
Double => 8,

src/librustc_trans/trans/expr.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,18 +1906,16 @@ fn int_cast(bcx: Block,
19061906
signed: bool)
19071907
-> ValueRef {
19081908
let _icx = push_ctxt("int_cast");
1909-
unsafe {
1910-
let srcsz = llvm::LLVMGetIntTypeWidth(llsrctype.to_ref());
1911-
let dstsz = llvm::LLVMGetIntTypeWidth(lldsttype.to_ref());
1912-
return if dstsz == srcsz {
1913-
BitCast(bcx, llsrc, lldsttype)
1914-
} else if srcsz > dstsz {
1915-
TruncOrBitCast(bcx, llsrc, lldsttype)
1916-
} else if signed {
1917-
SExtOrBitCast(bcx, llsrc, lldsttype)
1918-
} else {
1919-
ZExtOrBitCast(bcx, llsrc, lldsttype)
1920-
};
1909+
let srcsz = llsrctype.int_width();
1910+
let dstsz = lldsttype.int_width();
1911+
return if dstsz == srcsz {
1912+
BitCast(bcx, llsrc, lldsttype)
1913+
} else if srcsz > dstsz {
1914+
TruncOrBitCast(bcx, llsrc, lldsttype)
1915+
} else if signed {
1916+
SExtOrBitCast(bcx, llsrc, lldsttype)
1917+
} else {
1918+
ZExtOrBitCast(bcx, llsrc, lldsttype)
19211919
}
19221920
}
19231921

src/librustc_trans/trans/type_.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ impl Type {
333333
_ => panic!("llvm_float_width called on a non-float type")
334334
}
335335
}
336+
337+
/// Retrieve the bit width of the integer type `self`.
338+
pub fn int_width(&self) -> u64 {
339+
unsafe {
340+
llvm::LLVMGetIntTypeWidth(self.to_ref()) as u64
341+
}
342+
}
336343
}
337344

338345

0 commit comments

Comments
 (0)