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..1b3d3aa1b5f5 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,144 @@ 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 { + matches!( + self, + Self::I8 | Self::I16 | Self::I32 | Self::I64 | Self::I128 | Self::ISize + ) + } +} + +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 +280,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 +591,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 } => ( @@ -994,7 +1118,7 @@ impl TypeEnv { match def { &ast::Def::Type(ref td) => { let tid = TypeId(tyenv.type_map.len()); - let name = tyenv.intern_mut(&td.name); + let name = tyenv.get_or_intern(&td.name); if let Some(existing) = tyenv.type_map.get(&name).copied() { tyenv.report_error( @@ -1051,7 +1175,7 @@ impl TypeEnv { continue; } }; - let name = tyenv.intern_mut(name); + let name = tyenv.get_or_intern(name); tyenv.const_types.insert(name, ty); } } @@ -1081,7 +1205,7 @@ impl TypeEnv { self.report_error(ty.pos, "primitive types cannot be marked `extern`"); return None; } - Some(Type::Primitive(tid, self.intern_mut(id), ty.pos)) + Some(Type::Primitive(tid, self.get_or_intern(id), ty.pos)) } &ast::TypeValue::Enum(ref ty_variants, ..) => { if ty.is_extern && ty.is_nodebug { @@ -1093,8 +1217,8 @@ impl TypeEnv { for variant in ty_variants { let combined_ident = ast::Ident(format!("{}.{}", ty.name.0, variant.name.0), variant.name.1); - let fullname = self.intern_mut(&combined_ident); - let name = self.intern_mut(&variant.name); + let fullname = self.get_or_intern(&combined_ident); + let name = self.get_or_intern(&variant.name); let id = VariantId(variants.len()); if variants.iter().any(|v: &Variant| v.name == name) { self.report_error( @@ -1105,7 +1229,7 @@ impl TypeEnv { } let mut fields = vec![]; for field in &variant.fields { - let field_name = self.intern_mut(&field.name); + let field_name = self.get_or_intern(&field.name); if fields.iter().any(|f: &Field| f.name == field_name) { self.report_error( field.pos, @@ -1166,7 +1290,7 @@ impl TypeEnv { self.errors.push(err); } - fn intern_mut(&mut self, ident: &ast::Ident) -> Sym { + fn get_or_intern(&mut self, ident: &ast::Ident) -> Sym { if let Some(s) = self.sym_map.get(&ident.0).copied() { s } else { @@ -1277,7 +1401,7 @@ impl TermEnv { for def in defs { match def { &ast::Def::Decl(ref decl) => { - let name = tyenv.intern_mut(&decl.term); + let name = tyenv.get_or_intern(&decl.term); if let Some(tid) = self.term_map.get(&name) { tyenv.report_error( decl.pos, @@ -1645,7 +1769,7 @@ impl TermEnv { ref func, pos, }) => { - let func_sym = tyenv.intern_mut(func); + let func_sym = tyenv.get_or_intern(func); let term_id = match self.get_term_by_name(tyenv, term) { Some(term) => term, None => { @@ -1698,7 +1822,7 @@ impl TermEnv { pos, infallible, }) => { - let func_sym = tyenv.intern_mut(func); + let func_sym = tyenv.get_or_intern(func); let term_id = match self.get_term_by_name(tyenv, term) { Some(term) => term, None => { @@ -1845,7 +1969,7 @@ impl TermEnv { vars: bindings.seen, prio, pos, - name: rule.name.as_ref().map(|i| tyenv.intern_mut(i)), + name: rule.name.as_ref().map(|i| tyenv.get_or_intern(i)), }); } _ => {} @@ -1941,11 +2065,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, ), @@ -1967,7 +2091,7 @@ impl TermEnv { Some(Pattern::ConstBool(TypeId::BOOL, val)) } &ast::Pattern::ConstPrim { ref val, pos } => { - let val = tyenv.intern_mut(val); + let val = tyenv.get_or_intern(val); let const_ty = match tyenv.const_types.get(&val) { Some(ty) => *ty, None => { @@ -2006,7 +2130,7 @@ impl TermEnv { // generate fewer follow-on error messages. let ty = subpat.ty(); - let name = tyenv.intern_mut(var); + let name = tyenv.get_or_intern(var); if bindings.lookup(name).is_some() { tyenv.report_error( pos, @@ -2023,7 +2147,7 @@ impl TermEnv { // existing bound value), otherwise it becomes a // `BindPattern` with a wildcard subpattern to capture // at this location. - let name = tyenv.intern_mut(var); + let name = tyenv.get_or_intern(var); match bindings.lookup(name) { None => { let id = bindings.add_var(name, expected_ty); @@ -2198,7 +2322,7 @@ impl TermEnv { pos, } => { // Look up the term. - let name = tyenv.intern_mut(&sym); + let name = tyenv.get_or_intern(&sym); let tid = match self.term_map.get(&name) { Some(&t) => t, None => { @@ -2311,7 +2435,7 @@ impl TermEnv { Some(Expr::Term(ty, tid, subexprs)) } &ast::Expr::Var { ref name, pos } => { - let sym = tyenv.intern_mut(name); + let sym = tyenv.get_or_intern(name); // Look through bindings, innermost (most recent) first. let bv = match bindings.lookup(sym) { None => { @@ -2364,20 +2488,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, ), @@ -2386,7 +2511,7 @@ impl TermEnv { Some(Expr::ConstInt(ty, val)) } &ast::Expr::ConstPrim { ref val, pos } => { - let val = tyenv.intern_mut(val); + let val = tyenv.get_or_intern(val); let const_ty = match tyenv.const_types.get(&val) { Some(ty) => *ty, None => { @@ -2419,7 +2544,7 @@ impl TermEnv { let mut let_defs = vec![]; for def in defs { // Check that the given variable name does not already exist. - let name = tyenv.intern_mut(&def.var); + let name = tyenv.get_or_intern(&def.var); // Look up the type. let tid = match tyenv.get_type_by_name(&def.ty) { @@ -2497,7 +2622,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 +2643,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 +2653,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 +2667,7 @@ mod test { ), Type::Enum { name: sym_a, - id: TypeId(2), + id: TypeId(14), is_extern: true, is_nodebug: false, variants: vec![ @@ -2554,12 +2679,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 +2695,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))