Skip to content

Commit

Permalink
Merge pull request #124 from jakbyte/128-bit-integers
Browse files Browse the repository at this point in the history
128-bit integers i128 and u128
  • Loading branch information
baszalmstra authored Apr 10, 2020
2 parents d8191f0 + 770b143 commit 36b9809
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 53 deletions.
2 changes: 2 additions & 0 deletions crates/mun_codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,13 @@ impl_fundamental_ir_types!(
i16 => i16_type():IntType,
i32 => i32_type():IntType,
i64 => i64_type():IntType,
i128 => i128_type():IntType,

u8 => i8_type():IntType,
u16 => i16_type():IntType,
u32 => i32_type():IntType,
u64 => i64_type():IntType,
u128 => i128_type():IntType,

bool => bool_type():IntType,

Expand Down
1 change: 1 addition & 0 deletions crates/mun_codegen/src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn float_ty_query(db: &impl IrDatabase, fty: FloatTy) -> FloatType {
fn int_ty_query(db: &impl IrDatabase, ity: IntTy) -> IntType {
let context = db.context();
match ity.resolve(&db.target()).bitness {
IntBitness::X128 => context.i128_type(),
IntBitness::X64 => context.i64_type(),
IntBitness::X32 => context.i32_type(),
IntBitness::X16 => context.i16_type(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/mun_codegen/src/test.rs
expression: "fn add(a: u8, b: u8): u8 { a+b }\n fn less(a: u16, b: u16): bool { a<b }\n fn greater(a: u32, b: u32): bool { a>b }\n fn equal(a: u64, b: u64): bool { a==b }\n fn greater_equal(a: usize, b: usize): bool { a>=b }\n fn less_equal(a: uint, b: uint): bool { a<=b }\n\n fn iadd(a: i8, b: i8): i8 { a+b }\n fn iless(a: i16, b: i16): bool { a<b }\n fn igreater(a: i32, b: i32): bool { a>b }\n fn iequal(a: i64, b: i64): bool { a==b }\n fn igreater_equal(a: isize, b: isize): bool { a>=b }\n fn iless_equal(a: int, b: int): bool { a<=b }"
expression: "fn add(a: u8, b: u8) -> u8 { a+b }\n fn less(a: u16, b: u16) -> bool { a<b }\n fn greater(a: u32, b: u32) -> bool { a>b }\n fn equal(a: u64, b: u64) -> bool { a==b }\n fn nequal(a: u128, b: u128) -> bool { a!=b }\n fn greater_equal(a: usize, b: usize) -> bool { a>=b }\n fn less_equal(a: uint, b: uint) -> bool { a<=b }\n\n fn iadd(a: i8, b: i8) -> i8 { a+b }\n fn iless(a: i16, b: i16) -> bool { a<b }\n fn igreater(a: i32, b: i32) -> bool { a>b }\n fn iequal(a: i64, b: i64) -> bool { a==b }\n fn inequal(a: i128, b: i128) -> bool { a!=b }\n fn igreater_equal(a: isize, b: isize) -> bool { a>=b }\n fn iless_equal(a: int, b: int) -> bool { a<=b }"
---
; ModuleID = 'main.mun'
source_filename = "main.mun"
Expand Down Expand Up @@ -29,6 +29,12 @@ body:
ret i1 %eq
}

define i1 @nequal(i128, i128) {
body:
%neq = icmp ne i128 %0, %1
ret i1 %neq
}

define i1 @greater_equal(i64, i64) {
body:
%greatereq = icmp uge i64 %0, %1
Expand Down Expand Up @@ -65,6 +71,12 @@ body:
ret i1 %eq
}

define i1 @inequal(i128, i128) {
body:
%neq = icmp ne i128 %0, %1
ret i1 %neq
}

define i1 @igreater_equal(i64, i64) {
body:
%greatereq = icmp sge i64 %0, %1
Expand Down
2 changes: 2 additions & 0 deletions crates/mun_codegen/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,15 @@ fn primitive_types() {
fn less(a: u16, b: u16) -> bool { a<b }
fn greater(a: u32, b: u32) -> bool { a>b }
fn equal(a: u64, b: u64) -> bool { a==b }
fn nequal(a: u128, b: u128) -> bool { a!=b }
fn greater_equal(a: usize, b: usize) -> bool { a>=b }
fn less_equal(a: uint, b: uint) -> bool { a<=b }
fn iadd(a: i8, b: i8) -> i8 { a+b }
fn iless(a: i16, b: i16) -> bool { a<b }
fn igreater(a: i32, b: i32) -> bool { a>b }
fn iequal(a: i64, b: i64) -> bool { a==b }
fn inequal(a: i128, b: i128) -> bool { a!=b }
fn igreater_equal(a: isize, b: isize) -> bool { a>=b }
fn iless_equal(a: int, b: int) -> bool { a<=b }
"#,
Expand Down
4 changes: 3 additions & 1 deletion crates/mun_codegen/src/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ macro_rules! impl_fundamental_static_type_info {
}
}

impl_fundamental_static_type_info!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64, bool);
impl_fundamental_static_type_info!(
u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, f32, f64, bool
);

impl<T: HasStaticTypeName> HasStaticTypeInfo for *mut T {
fn type_info(context: &Context, target: &TargetData) -> TypeInfo {
Expand Down
9 changes: 9 additions & 0 deletions crates/mun_hir/src/builtin_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum IntBitness {
X16,
X32,
X64,
X128,
Undefined,
}

Expand Down Expand Up @@ -53,13 +54,15 @@ impl BuiltinType {
(name![i16], BuiltinType::Int(BuiltinInt::I16)),
(name![i32], BuiltinType::Int(BuiltinInt::I32)),
(name![i64], BuiltinType::Int(BuiltinInt::I64)),
(name![i128], BuiltinType::Int(BuiltinInt::I128)),

(name![uint], BuiltinType::Int(BuiltinInt::UINT)),
(name![usize], BuiltinType::Int(BuiltinInt::USIZE)),
(name![u8], BuiltinType::Int(BuiltinInt::U8)),
(name![u16], BuiltinType::Int(BuiltinInt::U16)),
(name![u32], BuiltinType::Int(BuiltinInt::U32)),
(name![u64], BuiltinType::Int(BuiltinInt::U64)),
(name![u128], BuiltinType::Int(BuiltinInt::U128)),

(name![float], BuiltinType::Float(BuiltinFloat::FLOAT)),
(name![f32], BuiltinType::Float(BuiltinFloat::F32)),
Expand All @@ -80,13 +83,15 @@ impl fmt::Display for BuiltinType {
(Signedness::Signed, IntBitness::X16) => "i16",
(Signedness::Signed, IntBitness::X32) => "i32",
(Signedness::Signed, IntBitness::X64) => "i64",
(Signedness::Signed, IntBitness::X128) => "i128",
(Signedness::Signed, IntBitness::Undefined) => "int",

(Signedness::Unsigned, IntBitness::Xsize) => "usize",
(Signedness::Unsigned, IntBitness::X8) => "u8",
(Signedness::Unsigned, IntBitness::X16) => "u16",
(Signedness::Unsigned, IntBitness::X32) => "u32",
(Signedness::Unsigned, IntBitness::X64) => "u64",
(Signedness::Unsigned, IntBitness::X128) => "u128",
(Signedness::Unsigned, IntBitness::Undefined) => "uint",
},
BuiltinType::Float(BuiltinFloat { bitness }) => match bitness {
Expand All @@ -107,13 +112,15 @@ impl BuiltinInt {
pub const I16 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X16 };
pub const I32 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X32 };
pub const I64 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X64 };
pub const I128 : BuiltinInt = BuiltinInt { signedness: Signedness::Signed, bitness: IntBitness::X128 };

pub const UINT : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::Undefined };
pub const USIZE: BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize };
pub const U8 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X8 };
pub const U16 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X16 };
pub const U32 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X32 };
pub const U64 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X64 };
pub const U128 : BuiltinInt = BuiltinInt { signedness: Signedness::Unsigned, bitness: IntBitness::X128 };


pub fn from_suffix(suffix: &str) -> Option<BuiltinInt> {
Expand All @@ -123,12 +130,14 @@ impl BuiltinInt {
"i16" => Self::I16,
"i32" => Self::I32,
"i64" => Self::I64,
"i128" => Self::I128,

"usize" => Self::USIZE,
"u8" => Self::U8,
"u16" => Self::U16,
"u32" => Self::U32,
"u64" => Self::U64,
"u128" => Self::U128,

_ => return None,
};
Expand Down
3 changes: 2 additions & 1 deletion crates/mun_hir/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ pub mod known {

known_names!(
// Primitives
int, isize, i8, i16, i32, i64, uint, usize, u8, u16, u32, u64, float, f32, f64, bool,
int, isize, i8, i16, i32, i64, i128, uint, usize, u8, u16, u32, u64, u128, float, f32, f64,
bool,
);

#[macro_export]
Expand Down
16 changes: 16 additions & 0 deletions crates/mun_hir/src/ty/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ impl IntTy {
}
}

pub fn i128() -> IntTy {
IntTy {
signedness: Signedness::Signed,
bitness: IntBitness::X128,
}
}

pub fn usize() -> IntTy {
IntTy {
signedness: Signedness::Unsigned,
Expand Down Expand Up @@ -97,6 +104,13 @@ impl IntTy {
}
}

pub fn u128() -> IntTy {
IntTy {
signedness: Signedness::Unsigned,
bitness: IntBitness::X128,
}
}

pub fn ty_to_string(self) -> &'static str {
match (self.signedness, self.bitness) {
(Signedness::Signed, IntBitness::Undefined) => "int",
Expand All @@ -105,12 +119,14 @@ impl IntTy {
(Signedness::Signed, IntBitness::X16) => "i16",
(Signedness::Signed, IntBitness::X32) => "i32",
(Signedness::Signed, IntBitness::X64) => "i64",
(Signedness::Signed, IntBitness::X128) => "i128",
(Signedness::Unsigned, IntBitness::Undefined) => "uint",
(Signedness::Unsigned, IntBitness::Xsize) => "usize",
(Signedness::Unsigned, IntBitness::X8) => "u8",
(Signedness::Unsigned, IntBitness::X16) => "u16",
(Signedness::Unsigned, IntBitness::X32) => "u32",
(Signedness::Unsigned, IntBitness::X64) => "u64",
(Signedness::Unsigned, IntBitness::X128) => "u128",
}
}
}
Expand Down
38 changes: 20 additions & 18 deletions crates/mun_hir/src/ty/snapshots/tests__primitives.snap
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
---
source: crates/mun_hir/src/ty/tests.rs
expression: "fn unsigned_primitives(a: u8, b: u16, c: u32, d: u64, e: usize, f: uint) -> u8 { a }\nfn signed_primitives(a: i8, b: i16, c: i32, d: i64, e: isize, f: int) -> i8 { a }\nfn float_primitives(a: f32, b: f64, c: float) -> f32 { a }"
expression: "fn unsigned_primitives(a: u8, b: u16, c: u32, d: u64, e: u128, f: usize, g: uint) -> u8 { a }\nfn signed_primitives(a: i8, b: i16, c: i32, d: i64, e: i128, f: isize, g: int) -> i8 { a }\nfn float_primitives(a: f32, b: f64, c: float) -> f32 { a }"
---
[23; 24) 'a': u8
[30; 31) 'b': u16
[38; 39) 'c': u32
[46; 47) 'd': u64
[54; 55) 'e': usize
[64; 65) 'f': uint
[79; 84) '{ a }': u8
[81; 82) 'a': u8
[106; 107) 'a': i8
[113; 114) 'b': i16
[121; 122) 'c': i32
[129; 130) 'd': i64
[137; 138) 'e': isize
[147; 148) 'f': int
[161; 166) '{ a }': i8
[163; 164) 'a': i8
[187; 188) 'a': f32
[195; 196) 'b': f64
[203; 204) 'c': float
[220; 225) '{ a }': f32
[222; 223) 'a': f32
[54; 55) 'e': u128
[63; 64) 'f': usize
[73; 74) 'g': uint
[88; 93) '{ a }': u8
[90; 91) 'a': u8
[115; 116) 'a': i8
[122; 123) 'b': i16
[130; 131) 'c': i32
[138; 139) 'd': i64
[146; 147) 'e': i128
[155; 156) 'f': isize
[165; 166) 'g': int
[179; 184) '{ a }': i8
[181; 182) 'a': i8
[205; 206) 'a': f32
[213; 214) 'b': f64
[221; 222) 'c': float
[238; 243) '{ a }': f32
[240; 241) 'a': f32
4 changes: 2 additions & 2 deletions crates/mun_hir/src/ty/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ fn struct_field_index() {
fn primitives() {
infer_snapshot(
r#"
fn unsigned_primitives(a: u8, b: u16, c: u32, d: u64, e: usize, f: uint) -> u8 { a }
fn signed_primitives(a: i8, b: i16, c: i32, d: i64, e: isize, f: int) -> i8 { a }
fn unsigned_primitives(a: u8, b: u16, c: u32, d: u64, e: u128, f: usize, g: uint) -> u8 { a }
fn signed_primitives(a: i8, b: i16, c: i32, d: i64, e: i128, f: isize, g: int) -> i8 { a }
fn float_primitives(a: f32, b: f64, c: float) -> f32 { a }
"#,
)
Expand Down
1 change: 1 addition & 0 deletions crates/mun_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,5 @@ invoke_fn_impl! {
fn invoke_fn12(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L) -> InvokeErr12;
fn invoke_fn13(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M) -> InvokeErr13;
fn invoke_fn14(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N) -> InvokeErr14;
fn invoke_fn15(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L, m: M, n: N, o:O) -> InvokeErr15;
}
5 changes: 5 additions & 0 deletions crates/mun_runtime/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,9 @@ macro_rules! invoke_fn {
&$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N,
)
};
($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr, $H:expr, $I:expr, $J:expr, $K:expr, $L:expr, $M:expr, $N:expr, $O:expr) => {
$crate::Runtime::invoke_fn15(
&$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N, $O,
)
};
}
4 changes: 3 additions & 1 deletion crates/mun_runtime/src/reflection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ macro_rules! impl_primitive_type {
}
}

impl_primitive_type!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, f32, f64, bool);
impl_primitive_type!(
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, bool
);

impl ReturnTypeReflection for () {
type Marshalled = ();
Expand Down
64 changes: 35 additions & 29 deletions crates/mun_runtime/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,22 +668,24 @@ fn test_primitive_types() {
b:u16,
c:u32,
d:u64,
e:u128,
e:i8,
f:i16,
g:i32,
h:i64,
f:i8,
g:i16,
h:i32,
i:i64,
j:i128,
i:f32,
j:f64,
k:f32,
l:f64,
k: int,
l: uint,
m: float
m: int,
n: uint,
o: float
}
pub fn new_primitives(a:u8, b:u16, c:u32, d:u64, e:i8, f:i16, g:i32, h:i64, i:f32, j:f64, k: int, l: uint, m: float) -> Primitives {
Primitives { a:a, b:b, c:c, d:d, e:e, f:f, g:g, h:h, i:i, j:j, k:k, l:l, m:m }
pub fn new_primitives(a:u8, b:u16, c:u32, d:u64, e:u128, f:i8, g:i16, h:i32, i:i64, j:i128, k:f32, l:f64, m: int, n: uint, o: float) -> Primitives {
Primitives { a:a, b:b, c:c, d:d, e:e, f:f, g:g, h:h, i:i, j:j, k:k, l:l, m:m, n:n, o:o }
}
"#,
);
Expand All @@ -708,31 +710,35 @@ fn test_primitive_types() {
2u16,
3u32,
4u64,
5i8,
6i16,
7i32,
8i64,
9.0f32,
10.0f64,
11isize,
12usize,
13.0f64
5u128,
6i8,
7i16,
8i32,
9i64,
10i128,
11.0f32,
12.0f64,
13isize,
14usize,
15.0f64
)
.unwrap();

test_field(&mut foo, (1u8, 100u8), "a");
test_field(&mut foo, (2u16, 101u16), "b");
test_field(&mut foo, (3u32, 102u32), "c");
test_field(&mut foo, (4u64, 103u64), "d");
test_field(&mut foo, (5i8, 104i8), "e");
test_field(&mut foo, (6i16, 105i16), "f");
test_field(&mut foo, (7i32, 106i32), "g");
test_field(&mut foo, (8i64, 107i64), "h");
test_field(&mut foo, (9f32, 108f32), "i");
test_field(&mut foo, (10f64, 109f64), "j");
test_field(&mut foo, (11isize, 110isize), "k");
test_field(&mut foo, (12usize, 111usize), "l");
test_field(&mut foo, (13f64, 112f64), "m");
test_field(&mut foo, (5u128, 104u128), "e");
test_field(&mut foo, (6i8, 105i8), "f");
test_field(&mut foo, (7i16, 106i16), "g");
test_field(&mut foo, (8i32, 107i32), "h");
test_field(&mut foo, (9i64, 108i64), "i");
test_field(&mut foo, (10i128, 109i128), "j");
test_field(&mut foo, (11f32, 110f32), "k");
test_field(&mut foo, (12f64, 111f64), "l");
test_field(&mut foo, (13isize, 112isize), "m");
test_field(&mut foo, (14usize, 113usize), "n");
test_field(&mut foo, (15f64, 114f64), "o");
}

#[test]
Expand Down

0 comments on commit 36b9809

Please sign in to comment.