From 99f708e04e138f1e2baaf859fb4c8b3a0464a57e Mon Sep 17 00:00:00 2001 From: Karl Meakin Date: Fri, 15 Nov 2024 01:41:17 +0000 Subject: [PATCH] ISLE: built-in integer types Copyright (c) 2024, Arm Limited. Signed-off-by: Karl Meakin --- cranelift/codegen/src/prelude.isle | 19 -- .../fail/bound_var_type_mismatch.isle | 3 - .../isle/isle/isle_examples/fail/error1.isle | 1 - .../isle/isle_examples/fail/extra_parens.isle | 2 - .../isle_examples/fail/impure_expression.isle | 2 - .../isle/isle_examples/fail/impure_rhs.isle | 2 - .../fail/multi_internal_etor.isle | 2 - .../isle/isle_examples/fail/multi_prio.isle | 2 - .../isle/isle/isle_examples/link/borrows.isle | 1 - .../isle/isle/isle_examples/link/iflets.isle | 2 - .../isle_examples/link/multi_constructor.isle | 2 - .../isle_examples/link/multi_extractor.isle | 1 - .../isle/isle/isle_examples/link/test.isle | 1 - .../isle/isle_examples/pass/bound_var.isle | 2 - .../pass/construct_and_extract.isle | 2 - .../isle/isle_examples/pass/conversions.isle | 1 - .../isle/isle/isle_examples/pass/let.isle | 1 - .../isle_examples/pass/prio_trie_bug.isle | 2 - .../isle/isle/isle_examples/pass/test2.isle | 1 - .../isle/isle/isle_examples/pass/test3.isle | 1 - .../isle/isle/isle_examples/pass/test4.isle | 1 - .../isle/isle_examples/pass/tutorial.isle | 3 - .../isle/isle_examples/pass/veri_spec.isle | 2 - .../isle/isle/isle_examples/run/iconst.isle | 5 - .../isle/isle_examples/run/let_shadowing.isle | 3 - cranelift/isle/isle/src/codegen.rs | 21 +- cranelift/isle/isle/src/sema.rs | 180 +++++++++++++++--- .../mid-end/broken_bor_band_consts.isle | 1 - .../examples/x86/amode_add_shl.isle | 2 - .../examples/x86/amode_add_uextend_shl.isle | 2 - 30 files changed, 166 insertions(+), 104 deletions(-) diff --git a/cranelift/codegen/src/prelude.isle b/cranelift/codegen/src/prelude.isle index 04b17df8e098..4385c02615bd 100644 --- a/cranelift/codegen/src/prelude.isle +++ b/cranelift/codegen/src/prelude.isle @@ -16,34 +16,15 @@ (model bool (type Bool)) (model u8 (type (bv 8))) -(type u8 (primitive u8)) - (model u16 (type (bv 16))) -(type u16 (primitive u16)) - (model u32 (type (bv 32))) -(type u32 (primitive u32)) - (model u64 (type (bv 64))) -(type u64 (primitive u64)) -(type u128 (primitive u128)) - (model usize (type (bv))) -(type usize (primitive usize)) (model i8 (type (bv 8))) -(type i8 (primitive i8)) - (model i16 (type (bv 16))) -(type i16 (primitive i16)) - (model i32 (type (bv 32))) -(type i32 (primitive i32)) - (model i64 (type (bv 64))) -(type i64 (primitive i64)) -(type i128 (primitive i128)) -(type isize (primitive isize)) ;; `cranelift-entity`-based identifiers. (model Type (type Int)) diff --git a/cranelift/isle/isle/isle_examples/fail/bound_var_type_mismatch.isle b/cranelift/isle/isle/isle_examples/fail/bound_var_type_mismatch.isle index d33f741e403b..a58d51215142 100644 --- a/cranelift/isle/isle/isle_examples/fail/bound_var_type_mismatch.isle +++ b/cranelift/isle/isle/isle_examples/fail/bound_var_type_mismatch.isle @@ -1,6 +1,3 @@ -(type u32 (primitive u32)) -(type u64 (primitive u64)) - (decl A (u32 u64) u32) (rule 1 (A x x) x) diff --git a/cranelift/isle/isle/isle_examples/fail/error1.isle b/cranelift/isle/isle/isle_examples/fail/error1.isle index f5c448a3a5ef..b24e18e54c1d 100644 --- a/cranelift/isle/isle/isle_examples/fail/error1.isle +++ b/cranelift/isle/isle/isle_examples/fail/error1.isle @@ -1,4 +1,3 @@ -(type u32 (primitive u32)) (type A (enum (A1 (x u32)))) (decl Ext1 (u32) A) diff --git a/cranelift/isle/isle/isle_examples/fail/extra_parens.isle b/cranelift/isle/isle/isle_examples/fail/extra_parens.isle index 06c05c820ffa..9c37123571da 100644 --- a/cranelift/isle/isle/isle_examples/fail/extra_parens.isle +++ b/cranelift/isle/isle/isle_examples/fail/extra_parens.isle @@ -1,5 +1,3 @@ -(type u32 (primitive u32)) - (decl f (u32) u32) ;; Should get an error about `x` not being a term, with a suggestion that it is ;; a bound var instead. diff --git a/cranelift/isle/isle/isle_examples/fail/impure_expression.isle b/cranelift/isle/isle/isle_examples/fail/impure_expression.isle index 6b993b1501b8..0adb4759708e 100644 --- a/cranelift/isle/isle/isle_examples/fail/impure_expression.isle +++ b/cranelift/isle/isle/isle_examples/fail/impure_expression.isle @@ -1,5 +1,3 @@ -(type u32 (primitive u32)) - (decl ctor (u32) u32) (rule (ctor x) x) diff --git a/cranelift/isle/isle/isle_examples/fail/impure_rhs.isle b/cranelift/isle/isle/isle_examples/fail/impure_rhs.isle index eae68c2f0290..4bfc95e5c555 100644 --- a/cranelift/isle/isle/isle_examples/fail/impure_rhs.isle +++ b/cranelift/isle/isle/isle_examples/fail/impure_rhs.isle @@ -1,5 +1,3 @@ -(type u32 (primitive u32)) - (decl pure ctor (u32) u32) (decl impure (u32) u32) diff --git a/cranelift/isle/isle/isle_examples/fail/multi_internal_etor.isle b/cranelift/isle/isle/isle_examples/fail/multi_internal_etor.isle index c2f62894544e..d876231dec75 100644 --- a/cranelift/isle/isle/isle_examples/fail/multi_internal_etor.isle +++ b/cranelift/isle/isle/isle_examples/fail/multi_internal_etor.isle @@ -1,4 +1,2 @@ -(type u32 (primitive u32)) - (decl multi A (u32) u32) (extractor (A x) x) diff --git a/cranelift/isle/isle/isle_examples/fail/multi_prio.isle b/cranelift/isle/isle/isle_examples/fail/multi_prio.isle index 30ce9c7a5fbc..feb766688de1 100644 --- a/cranelift/isle/isle/isle_examples/fail/multi_prio.isle +++ b/cranelift/isle/isle/isle_examples/fail/multi_prio.isle @@ -1,4 +1,2 @@ -(type u32 (primitive u32)) - (decl multi A (u32) u32) (rule 0 (A x) x) diff --git a/cranelift/isle/isle/isle_examples/link/borrows.isle b/cranelift/isle/isle/isle_examples/link/borrows.isle index 526c21ede966..2b1ce8f6f481 100644 --- a/cranelift/isle/isle/isle_examples/link/borrows.isle +++ b/cranelift/isle/isle/isle_examples/link/borrows.isle @@ -1,4 +1,3 @@ -(type u32 (primitive u32)) (type A extern (enum (B (x u32) (y u32)))) (decl get_a (A) u32) diff --git a/cranelift/isle/isle/isle_examples/link/iflets.isle b/cranelift/isle/isle/isle_examples/link/iflets.isle index bd768b4ece30..cf7f324696ef 100644 --- a/cranelift/isle/isle/isle_examples/link/iflets.isle +++ b/cranelift/isle/isle/isle_examples/link/iflets.isle @@ -1,5 +1,3 @@ -(type u32 (primitive u32)) - (decl pure partial A (u32 u32) u32) (extern constructor A A) diff --git a/cranelift/isle/isle/isle_examples/link/multi_constructor.isle b/cranelift/isle/isle/isle_examples/link/multi_constructor.isle index 45919fe221c7..86758c983142 100644 --- a/cranelift/isle/isle/isle_examples/link/multi_constructor.isle +++ b/cranelift/isle/isle/isle_examples/link/multi_constructor.isle @@ -1,5 +1,3 @@ -(type u32 (primitive u32)) - (decl multi A (u32) u32) (decl multi B (u32) u32) (decl multi C (u32) u32) diff --git a/cranelift/isle/isle/isle_examples/link/multi_extractor.isle b/cranelift/isle/isle/isle_examples/link/multi_extractor.isle index 2630d8682651..f69b2a6f0045 100644 --- a/cranelift/isle/isle/isle_examples/link/multi_extractor.isle +++ b/cranelift/isle/isle/isle_examples/link/multi_extractor.isle @@ -1,4 +1,3 @@ -(type u32 (primitive u32)) (type A extern (enum (B) (C))) (decl multi E1 (A u32) u32) diff --git a/cranelift/isle/isle/isle_examples/link/test.isle b/cranelift/isle/isle/isle_examples/link/test.isle index 09d5ea92afb9..80190e126736 100644 --- a/cranelift/isle/isle/isle_examples/link/test.isle +++ b/cranelift/isle/isle/isle_examples/link/test.isle @@ -1,4 +1,3 @@ -(type u32 (primitive u32)) (type A (enum (A1 (x u32)) (A2 (x u32)))) (type B (enum (B1 (x u32)) (B2 (x u32)))) diff --git a/cranelift/isle/isle/isle_examples/pass/bound_var.isle b/cranelift/isle/isle/isle_examples/pass/bound_var.isle index f822dc8c35e5..0647330a4372 100644 --- a/cranelift/isle/isle/isle_examples/pass/bound_var.isle +++ b/cranelift/isle/isle/isle_examples/pass/bound_var.isle @@ -1,5 +1,3 @@ -(type u32 (primitive u32)) - (decl A (u32 u32) u32) (rule 1 (A x x) x) diff --git a/cranelift/isle/isle/isle_examples/pass/construct_and_extract.isle b/cranelift/isle/isle/isle_examples/pass/construct_and_extract.isle index a0951edd4198..3d8becd54334 100644 --- a/cranelift/isle/isle/isle_examples/pass/construct_and_extract.isle +++ b/cranelift/isle/isle/isle_examples/pass/construct_and_extract.isle @@ -1,5 +1,3 @@ -(type i32 (primitive i32)) - (type B (enum (B (x i32) (y i32)))) ;; `isub` has a constructor and extractor. diff --git a/cranelift/isle/isle/isle_examples/pass/conversions.isle b/cranelift/isle/isle/isle_examples/pass/conversions.isle index 1b3e6bfb13a5..a1bb7ea94180 100644 --- a/cranelift/isle/isle/isle_examples/pass/conversions.isle +++ b/cranelift/isle/isle/isle_examples/pass/conversions.isle @@ -1,7 +1,6 @@ (type T (enum (A) (B))) (type U (enum (C) (D))) (type V (enum (E) (F))) -(type u32 (primitive u32)) (convert T U t_to_u) (convert U T u_to_t) diff --git a/cranelift/isle/isle/isle_examples/pass/let.isle b/cranelift/isle/isle/isle_examples/pass/let.isle index c43d1ec668c2..59670852ec12 100644 --- a/cranelift/isle/isle/isle_examples/pass/let.isle +++ b/cranelift/isle/isle/isle_examples/pass/let.isle @@ -1,4 +1,3 @@ -(type u32 (primitive u32)) (type A (enum (Add (x u32) (y u32)) (Sub (x u32) (y u32)))) (type B (enum (B (z u32)))) diff --git a/cranelift/isle/isle/isle_examples/pass/prio_trie_bug.isle b/cranelift/isle/isle/isle_examples/pass/prio_trie_bug.isle index 19fa9c78fa07..b63de1ab152b 100644 --- a/cranelift/isle/isle/isle_examples/pass/prio_trie_bug.isle +++ b/cranelift/isle/isle/isle_examples/pass/prio_trie_bug.isle @@ -5,8 +5,6 @@ ;; priority 1 below. (type Unit (primitive Unit)) -(type u8 (primitive u8)) -(type u32 (primitive u32)) (type Reg (primitive Reg)) (type MemFlags (primitive MemFlags)) (type MachLabel (primitive MachLabel)) diff --git a/cranelift/isle/isle/isle_examples/pass/test2.isle b/cranelift/isle/isle/isle_examples/pass/test2.isle index 5c0977a70211..209769d39dd9 100644 --- a/cranelift/isle/isle/isle_examples/pass/test2.isle +++ b/cranelift/isle/isle/isle_examples/pass/test2.isle @@ -1,4 +1,3 @@ -(type u32 (primitive u32)) (type A (enum (A1 (x B) (y B)))) (type B (enum diff --git a/cranelift/isle/isle/isle_examples/pass/test3.isle b/cranelift/isle/isle/isle_examples/pass/test3.isle index 82d37624d532..5f54a489b764 100644 --- a/cranelift/isle/isle/isle_examples/pass/test3.isle +++ b/cranelift/isle/isle/isle_examples/pass/test3.isle @@ -7,7 +7,6 @@ (type Inst (primitive Inst)) (type InstInput (primitive InstInput)) (type Reg (primitive Reg)) -(type u32 (primitive u32)) (decl Op (Opcode) Inst) (extern extractor infallible Op get_opcode) diff --git a/cranelift/isle/isle/isle_examples/pass/test4.isle b/cranelift/isle/isle/isle_examples/pass/test4.isle index 0acb162e2ccd..714fd7bd997d 100644 --- a/cranelift/isle/isle/isle_examples/pass/test4.isle +++ b/cranelift/isle/isle/isle_examples/pass/test4.isle @@ -1,4 +1,3 @@ -(type u32 (primitive u32)) (type A (enum (A1 (x u32)))) (decl Ext1 (u32) A) diff --git a/cranelift/isle/isle/isle_examples/pass/tutorial.isle b/cranelift/isle/isle/isle_examples/pass/tutorial.isle index f072fee74f4e..871ac843b3c9 100644 --- a/cranelift/isle/isle/isle_examples/pass/tutorial.isle +++ b/cranelift/isle/isle/isle_examples/pass/tutorial.isle @@ -1,8 +1,5 @@ ;;;; Type Definitions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Declare that we are using the `i32` primitive type from Rust. -(type i32 (primitive i32)) - ;; Our high-level, RISC-y input IR. (type HighLevelInst (enum (Add (a Value) (b Value)) diff --git a/cranelift/isle/isle/isle_examples/pass/veri_spec.isle b/cranelift/isle/isle/isle_examples/pass/veri_spec.isle index 5aae9faa007e..daa0d8ead7a4 100644 --- a/cranelift/isle/isle/isle_examples/pass/veri_spec.isle +++ b/cranelift/isle/isle/isle_examples/pass/veri_spec.isle @@ -1,5 +1,3 @@ -(type u8 (primitive u8)) - (form bv_unary_8_to_64 ((args (bv 8)) (ret (bv 8)) (canon (bv 8))) diff --git a/cranelift/isle/isle/isle_examples/run/iconst.isle b/cranelift/isle/isle/isle_examples/run/iconst.isle index 84270607467d..7f9614708a03 100644 --- a/cranelift/isle/isle/isle_examples/run/iconst.isle +++ b/cranelift/isle/isle/isle_examples/run/iconst.isle @@ -1,13 +1,9 @@ -(type i64 (primitive i64)) - (decl partial X (i64) i64) (rule (X -1) -2) (rule (X -2) -3) (rule (X 0x7fff_ffff_ffff_ffff) -0x8000_0000_0000_0000) (rule (X -16) 1) -(type i128 (primitive i128)) - (decl partial Y (i128) i128) (rule (Y 0x1000_0000_0000_0000_1234_5678_9abc_def0) -1) @@ -17,7 +13,6 @@ (rule (Y -0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff) -3) ;; Test some various syntaxes for numbers -(type i32 (primitive i32)) (decl partial Z (i32) i32) (rule (Z 0) 0x01) (rule (Z 0x01) 0x0_2) diff --git a/cranelift/isle/isle/isle_examples/run/let_shadowing.isle b/cranelift/isle/isle/isle_examples/run/let_shadowing.isle index f25db3e2f61f..5db327b14103 100644 --- a/cranelift/isle/isle/isle_examples/run/let_shadowing.isle +++ b/cranelift/isle/isle/isle_examples/run/let_shadowing.isle @@ -1,6 +1,3 @@ - -(type u64 (primitive u64)) - (decl foo (u64) u64) (rule (foo x) x) diff --git a/cranelift/isle/isle/src/codegen.rs b/cranelift/isle/isle/src/codegen.rs index 1535add2c160..769223d38368 100644 --- a/cranelift/isle/isle/src/codegen.rs +++ b/cranelift/isle/isle/src/codegen.rs @@ -1,7 +1,9 @@ //! Generate Rust code from a series of Sequences. use crate::files::Files; -use crate::sema::{ExternalSig, ReturnKind, Term, TermEnv, TermId, Type, TypeEnv, TypeId}; +use crate::sema::{ + BuiltinType, ExternalSig, ReturnKind, Term, TermEnv, TermId, Type, TypeEnv, TypeId, +}; use crate::serialize::{Block, ControlFlow, EvalStep, MatchArm}; use crate::stablemapset::StableSet; use crate::trie_again::{Binding, BindingId, Constraint, RuleSet}; @@ -915,18 +917,11 @@ impl Length for ContextIterWrapper {{ val: i128, ty: TypeId, ) -> Result<(), std::fmt::Error> { - // For the kinds of situations where we use ISLE, magic numbers are - // much more likely to be understandable if they're in hex rather than - // decimal. - // TODO: use better type info (https://github.com/bytecodealliance/wasmtime/issues/5431) - if val < 0 - && self.typeenv.types[ty.index()] - .name(self.typeenv) - .starts_with('i') - { - write!(ctx.out, "-{:#X}", -val) - } else { - write!(ctx.out, "{val:#X}") + let ty_data = &self.typeenv.types[ty.index()]; + match ty_data { + Type::Builtin(BuiltinType::Int(ty)) if ty.is_signed() => write!(ctx.out, "{val}_{ty}"), + Type::Builtin(BuiltinType::Int(ty)) => write!(ctx.out, "{val:#x}_{ty}"), + _ => write!(ctx.out, "{val:#x}"), } } } diff --git a/cranelift/isle/isle/src/sema.rs b/cranelift/isle/isle/src/sema.rs index 2ce75937d429..e09c037b645e 100644 --- a/cranelift/isle/isle/src/sema.rs +++ b/cranelift/isle/isle/src/sema.rs @@ -22,6 +22,7 @@ use std::collections::hash_map::Entry; use std::collections::BTreeMap; use std::collections::BTreeSet; use std::collections::HashMap; +use std::fmt; declare_id!( /// The id of an interned symbol. @@ -82,26 +83,155 @@ pub struct TypeEnv { /// A built-in type. #[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[repr(u8)] pub enum BuiltinType { /// The type of booleans, with values `true` and `false`. Bool, + /// The types of fixed-width integers. + Int(IntType), +} + +/// A built-in fixed-width integer type. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum IntType { + /// Unsigned, 8 bits. + U8, + /// Unsigned, 16 bits. + U16, + /// Unsigned, 32 bits. + U32, + /// Unsigned, 64 bits. + U64, + /// Unsigned, 128 bits. + U128, + /// Unsigned, enough bits to hold a pointer. + USize, + /// Signed, 8 bits. + I8, + /// Signed, 16 bits. + I16, + /// Signed, 32 bits. + I32, + /// Signed, 64 bits. + I64, + /// Signed, 128 bits. + I128, + /// Unsigned, enough bits to hold a pointer. + ISize, +} + +impl IntType { + /// Get the integer type's name. + pub fn name(&self) -> &'static str { + match self { + IntType::U8 => "u8", + IntType::U16 => "u16", + IntType::U32 => "u32", + IntType::U64 => "u64", + IntType::U128 => "u128", + IntType::USize => "usize", + IntType::I8 => "i8", + IntType::I16 => "i16", + IntType::I32 => "i32", + IntType::I64 => "i64", + IntType::I128 => "i128", + IntType::ISize => "isize", + } + } + + /// Is this integer type signed? + pub fn is_signed(&self) -> bool { + match self { + IntType::U8 + | IntType::U16 + | IntType::U32 + | IntType::U64 + | IntType::U128 + | IntType::USize => false, + + IntType::I8 + | IntType::I16 + | IntType::I32 + | IntType::I64 + | IntType::I128 + | IntType::ISize => true, + } + } +} + +impl fmt::Display for IntType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.name()) + } } impl BuiltinType { /// All the built-in types. - pub const ALL: &'static [Self] = &[Self::Bool]; + pub const ALL: &'static [Self] = &[ + Self::Bool, + Self::Int(IntType::U8), + Self::Int(IntType::U16), + Self::Int(IntType::U32), + Self::Int(IntType::U64), + Self::Int(IntType::U128), + Self::Int(IntType::USize), + Self::Int(IntType::I8), + Self::Int(IntType::I16), + Self::Int(IntType::I32), + Self::Int(IntType::I64), + Self::Int(IntType::I128), + Self::Int(IntType::ISize), + ]; /// Get the built-in type's name. pub fn name(&self) -> &'static str { match self { BuiltinType::Bool => "bool", + BuiltinType::Int(it) => it.name(), + } + } + + const fn to_usize(&self) -> usize { + match self { + Self::Bool => 0, + Self::Int(ty) => *ty as usize + 1, } } } impl TypeId { + const fn builtin(builtin: BuiltinType) -> Self { + Self(builtin.to_usize()) + } + /// TypeId for `bool`. - pub const BOOL: Self = Self(0); + pub const BOOL: Self = Self::builtin(BuiltinType::Bool); + + /// TypeId for `u8`. + pub const U8: Self = Self::builtin(BuiltinType::Int(IntType::U8)); + /// TypeId for `u16`. + pub const U16: Self = Self::builtin(BuiltinType::Int(IntType::U16)); + /// TypeId for `u32`. + pub const U32: Self = Self::builtin(BuiltinType::Int(IntType::U32)); + /// TypeId for `u64`. + pub const U64: Self = Self::builtin(BuiltinType::Int(IntType::U64)); + /// TypeId for `u128`. + pub const U128: Self = Self::builtin(BuiltinType::Int(IntType::U128)); + /// TypeId for `usize`. + pub const USIZE: Self = Self::builtin(BuiltinType::Int(IntType::USize)); + + /// TypeId for `i8`. + pub const I8: Self = Self::builtin(BuiltinType::Int(IntType::I8)); + /// TypeId for `i16`. + pub const I16: Self = Self::builtin(BuiltinType::Int(IntType::I16)); + /// TypeId for `i32`. + pub const I32: Self = Self::builtin(BuiltinType::Int(IntType::I32)); + /// TypeId for `i64`. + pub const I64: Self = Self::builtin(BuiltinType::Int(IntType::I64)); + /// TypeId for `i128`. + pub const I128: Self = Self::builtin(BuiltinType::Int(IntType::I128)); + /// TypeId for `isize`. + pub const ISIZE: Self = Self::builtin(BuiltinType::Int(IntType::ISize)); } /// A type. @@ -161,6 +291,11 @@ impl Type { pub fn is_prim(&self) -> bool { matches!(self, Type::Primitive(..)) } + + /// Is this a built-in integer type? + pub fn is_int(&self) -> bool { + matches!(self, Self::Builtin(BuiltinType::Int(_))) + } } /// A variant of an enum. @@ -467,7 +602,7 @@ impl Term { } => { let (func_name, full_name) = match kind { ConstructorKind::InternalConstructor => { - let name = format!("constructor_{}", tyenv.syms[self.name.index()]); + let name = format!("{}", tyenv.syms[self.name.index()]); (name.clone(), name) } ConstructorKind::ExternalConstructor { name } => ( @@ -1941,11 +2076,11 @@ impl TermEnv { // TODO: flag on primitive type decl indicating it's an integer type? &ast::Pattern::ConstInt { val, pos } => { let ty = &tyenv.types[expected_ty.index()]; - if !ty.is_prim() { + if !ty.is_int() && !ty.is_prim() { tyenv.report_error( pos, format!( - "expected non-primitive type {}, but found integer literal '{}'", + "expected non-integer type {}, but found integer literal '{}'", ty.name(tyenv), val, ), @@ -2364,20 +2499,21 @@ impl TermEnv { Some(Expr::ConstBool(TypeId::BOOL, val)) } &ast::Expr::ConstInt { val, pos } => { - if ty.is_none() { + let Some(ty) = ty else { tyenv.report_error( pos, "integer literal in a context that needs an explicit type".to_string(), ); return None; - } - let ty = ty.unwrap(); + }; + + let typ = &tyenv.types[ty.index()]; - if !tyenv.types[ty.index()].is_prim() { + if !typ.is_int() && !typ.is_prim() { tyenv.report_error( pos, format!( - "expected non-primitive type {}, but found integer literal '{}'", + "expected non-integer type {}, but found integer literal '{}'", tyenv.types[ty.index()].name(tyenv), val, ), @@ -2497,7 +2633,7 @@ mod test { #[test] fn build_type_env() { let text = r" - (type u32 (primitive u32)) + (type UImm8 (primitive UImm8)) (type A extern (enum (B (f1 u32) (f2 u32)) (C (f1 u32)))) "; let ast = parse(Lexer::new(0, text).unwrap()).expect("should parse"); @@ -2518,8 +2654,8 @@ mod test { let sym_a_c = tyenv .intern(&Ident("A.C".to_string(), Default::default())) .unwrap(); - let sym_u32 = tyenv - .intern(&Ident("u32".to_string(), Default::default())) + let sym_uimm8 = tyenv + .intern(&Ident("UImm8".to_string(), Default::default())) .unwrap(); let sym_f1 = tyenv .intern(&Ident("f1".to_string(), Default::default())) @@ -2528,13 +2664,13 @@ mod test { .intern(&Ident("f2".to_string(), Default::default())) .unwrap(); - assert_eq!(tyenv.type_map.get(&sym_u32).unwrap(), &TypeId(1)); - assert_eq!(tyenv.type_map.get(&sym_a).unwrap(), &TypeId(2)); + assert_eq!(tyenv.type_map.get(&sym_uimm8).unwrap(), &TypeId(13)); + assert_eq!(tyenv.type_map.get(&sym_a).unwrap(), &TypeId(14)); let expected_types = vec![ Type::Primitive( - TypeId(1), - sym_u32, + TypeId(13), + sym_uimm8, Pos { file: 0, offset: 19, @@ -2542,7 +2678,7 @@ mod test { ), Type::Enum { name: sym_a, - id: TypeId(2), + id: TypeId(14), is_extern: true, is_nodebug: false, variants: vec![ @@ -2554,12 +2690,12 @@ mod test { Field { name: sym_f1, id: FieldId(0), - ty: TypeId(1), + ty: TypeId::U32, }, Field { name: sym_f2, id: FieldId(1), - ty: TypeId(1), + ty: TypeId::U32, }, ], }, @@ -2570,13 +2706,13 @@ mod test { fields: vec![Field { name: sym_f1, id: FieldId(0), - ty: TypeId(1), + ty: TypeId::U32, }], }, ], pos: Pos { file: 0, - offset: 58, + offset: 62, }, }, ]; diff --git a/cranelift/isle/veri/veri_engine/examples/mid-end/broken_bor_band_consts.isle b/cranelift/isle/veri/veri_engine/examples/mid-end/broken_bor_band_consts.isle index cf55369a480d..c0363cf548c9 100644 --- a/cranelift/isle/veri/veri_engine/examples/mid-end/broken_bor_band_consts.isle +++ b/cranelift/isle/veri/veri_engine/examples/mid-end/broken_bor_band_consts.isle @@ -1,6 +1,5 @@ (type Type (primitive Type)) (type Value (primitive Value)) -(type u64 (primitive u64)) (type Imm64 (primitive Imm64)) (extern const true bool) diff --git a/cranelift/isle/veri/veri_engine/examples/x86/amode_add_shl.isle b/cranelift/isle/veri/veri_engine/examples/x86/amode_add_shl.isle index aec20bc42c81..94e56c767d2a 100644 --- a/cranelift/isle/veri/veri_engine/examples/x86/amode_add_shl.isle +++ b/cranelift/isle/veri/veri_engine/examples/x86/amode_add_shl.isle @@ -6,8 +6,6 @@ (type MemFlags (primitive MemFlags)) (type Gpr (primitive Gpr)) (type Imm64 (primitive Imm64)) -(type u32 (primitive u32)) -(type u8 (primitive u8)) (type MInst (enum)) diff --git a/cranelift/isle/veri/veri_engine/examples/x86/amode_add_uextend_shl.isle b/cranelift/isle/veri/veri_engine/examples/x86/amode_add_uextend_shl.isle index f6675a3b2771..5e8aa8652bbf 100644 --- a/cranelift/isle/veri/veri_engine/examples/x86/amode_add_uextend_shl.isle +++ b/cranelift/isle/veri/veri_engine/examples/x86/amode_add_uextend_shl.isle @@ -8,8 +8,6 @@ (type MemFlags (primitive MemFlags)) (type Gpr (primitive Gpr)) (type Imm64 (primitive Imm64)) -(type u32 (primitive u32)) -(type u8 (primitive u8)) (type MInst (enum))