From ad3928c01aec212d8b92fa515d118446e6da3c0b Mon Sep 17 00:00:00 2001 From: jakbyte Date: Fri, 10 Apr 2020 12:27:50 -0400 Subject: [PATCH 1/2] feat: 128-bit integers bonus: Runtime::invoke_fn15 --- crates/mun_codegen/src/ir.rs | 2 + crates/mun_codegen/src/ir/ty.rs | 1 + .../test__primitive_types_file_ir.snap | 14 +++- crates/mun_codegen/src/test.rs | 2 + crates/mun_codegen/src/type_info.rs | 4 +- crates/mun_hir/src/builtin_type.rs | 9 +++ crates/mun_hir/src/name.rs | 3 +- crates/mun_hir/src/ty/primitives.rs | 16 +++++ .../src/ty/snapshots/tests__primitives.snap | 38 +++++------ crates/mun_hir/src/ty/tests.rs | 4 +- crates/mun_runtime/src/lib.rs | 1 + crates/mun_runtime/src/macros.rs | 5 ++ crates/mun_runtime/src/reflection.rs | 4 +- crates/mun_runtime/src/test.rs | 64 ++++++++++--------- 14 files changed, 114 insertions(+), 53 deletions(-) diff --git a/crates/mun_codegen/src/ir.rs b/crates/mun_codegen/src/ir.rs index a97281272..90a328e35 100644 --- a/crates/mun_codegen/src/ir.rs +++ b/crates/mun_codegen/src/ir.rs @@ -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, diff --git a/crates/mun_codegen/src/ir/ty.rs b/crates/mun_codegen/src/ir/ty.rs index cc561de12..d5cc09f1c 100644 --- a/crates/mun_codegen/src/ir/ty.rs +++ b/crates/mun_codegen/src/ir/ty.rs @@ -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(), diff --git a/crates/mun_codegen/src/snapshots/test__primitive_types_file_ir.snap b/crates/mun_codegen/src/snapshots/test__primitive_types_file_ir.snap index a835baffd..632441059 100644 --- a/crates/mun_codegen/src/snapshots/test__primitive_types_file_ir.snap +++ b/crates/mun_codegen/src/snapshots/test__primitive_types_file_ir.snap @@ -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 { ab }\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 { ab }\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 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 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" @@ -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 @@ -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 diff --git a/crates/mun_codegen/src/test.rs b/crates/mun_codegen/src/test.rs index 08a9e24c1..9abca80c2 100644 --- a/crates/mun_codegen/src/test.rs +++ b/crates/mun_codegen/src/test.rs @@ -454,6 +454,7 @@ fn primitive_types() { fn less(a: u16, b: u16) -> bool { a 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 } @@ -461,6 +462,7 @@ fn primitive_types() { fn iless(a: i16, b: i16) -> bool { a 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 } "#, diff --git a/crates/mun_codegen/src/type_info.rs b/crates/mun_codegen/src/type_info.rs index 10ba18c22..96430b9e0 100644 --- a/crates/mun_codegen/src/type_info.rs +++ b/crates/mun_codegen/src/type_info.rs @@ -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 HasStaticTypeInfo for *mut T { fn type_info(context: &Context, target: &TargetData) -> TypeInfo { diff --git a/crates/mun_hir/src/builtin_type.rs b/crates/mun_hir/src/builtin_type.rs index 0726d15e3..f93facbd8 100644 --- a/crates/mun_hir/src/builtin_type.rs +++ b/crates/mun_hir/src/builtin_type.rs @@ -14,6 +14,7 @@ pub enum IntBitness { X16, X32, X64, + X128, Undefined, } @@ -53,6 +54,7 @@ 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)), @@ -60,6 +62,7 @@ impl BuiltinType { (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)), @@ -80,6 +83,7 @@ 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", @@ -87,6 +91,7 @@ impl fmt::Display for BuiltinType { (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 { @@ -107,6 +112,7 @@ 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 }; @@ -114,6 +120,7 @@ impl BuiltinInt { 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 { @@ -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, }; diff --git a/crates/mun_hir/src/name.rs b/crates/mun_hir/src/name.rs index b9013eba4..4f41f78c1 100644 --- a/crates/mun_hir/src/name.rs +++ b/crates/mun_hir/src/name.rs @@ -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] diff --git a/crates/mun_hir/src/ty/primitives.rs b/crates/mun_hir/src/ty/primitives.rs index afdad558f..280a9b382 100644 --- a/crates/mun_hir/src/ty/primitives.rs +++ b/crates/mun_hir/src/ty/primitives.rs @@ -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, @@ -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", @@ -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", } } } diff --git a/crates/mun_hir/src/ty/snapshots/tests__primitives.snap b/crates/mun_hir/src/ty/snapshots/tests__primitives.snap index 968ff6e1e..b26d5528d 100644 --- a/crates/mun_hir/src/ty/snapshots/tests__primitives.snap +++ b/crates/mun_hir/src/ty/snapshots/tests__primitives.snap @@ -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 diff --git a/crates/mun_hir/src/ty/tests.rs b/crates/mun_hir/src/ty/tests.rs index d4a61cbe4..982530b5b 100644 --- a/crates/mun_hir/src/ty/tests.rs +++ b/crates/mun_hir/src/ty/tests.rs @@ -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 } "#, ) diff --git a/crates/mun_runtime/src/lib.rs b/crates/mun_runtime/src/lib.rs index 9d352ca35..e1927ebe1 100644 --- a/crates/mun_runtime/src/lib.rs +++ b/crates/mun_runtime/src/lib.rs @@ -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; } diff --git a/crates/mun_runtime/src/macros.rs b/crates/mun_runtime/src/macros.rs index 20491c0dc..68ae84ca7 100644 --- a/crates/mun_runtime/src/macros.rs +++ b/crates/mun_runtime/src/macros.rs @@ -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, + ) + }; } diff --git a/crates/mun_runtime/src/reflection.rs b/crates/mun_runtime/src/reflection.rs index 199a87823..40e7b8250 100644 --- a/crates/mun_runtime/src/reflection.rs +++ b/crates/mun_runtime/src/reflection.rs @@ -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 = (); diff --git a/crates/mun_runtime/src/test.rs b/crates/mun_runtime/src/test.rs index 3cace9b96..26df4dc12 100644 --- a/crates/mun_runtime/src/test.rs +++ b/crates/mun_runtime/src/test.rs @@ -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 } } "#, ); @@ -708,15 +710,17 @@ 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(); @@ -724,15 +728,17 @@ fn test_primitive_types() { 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] From 770b1434d2a248b44626f1ca4ea78fa7fc7a0e5b Mon Sep 17 00:00:00 2001 From: jakbyte Date: Fri, 10 Apr 2020 12:50:25 -0400 Subject: [PATCH 2/2] style: fix spacing in builtin types list --- crates/mun_hir/src/builtin_type.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/mun_hir/src/builtin_type.rs b/crates/mun_hir/src/builtin_type.rs index f93facbd8..ddddaf7e7 100644 --- a/crates/mun_hir/src/builtin_type.rs +++ b/crates/mun_hir/src/builtin_type.rs @@ -54,7 +54,7 @@ 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![i128], BuiltinType::Int(BuiltinInt::I128)), (name![uint], BuiltinType::Int(BuiltinInt::UINT)), (name![usize], BuiltinType::Int(BuiltinInt::USIZE)),