feat: add support for USize literals in LCNF (#8456)

This PR adds support for primitive USize literals in LCNF.
This commit is contained in:
Cameron Zwarich 2025-05-23 10:22:31 -07:00 committed by GitHub
parent 3b205505ef
commit 9ea4946560
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 15 additions and 5 deletions

View file

@ -497,8 +497,13 @@ def emitNumLit (t : IRType) (v : Nat) : M Unit := do
else
if v < UInt32.size then
emit v
else if t == .usize then
emit "((size_t)"
emit v
emit "ULL)"
else
emit v; emit "ULL"
emit v
emit "ULL"
def emitLit (z : VarId) (t : IRType) (v : LitVal) : M Unit := do
emitLhs z;

View file

@ -70,7 +70,7 @@ def lowerLitValue (v : LCNF.LitValue) : LitVal :=
| .uint8 v => .num (UInt8.toNat v)
| .uint16 v => .num (UInt16.toNat v)
| .uint32 v => .num (UInt32.toNat v)
| .uint64 v => .num (UInt64.toNat v)
| .uint64 v | .usize v => .num (UInt64.toNat v)
-- TODO: This should be cached.
def lowerEnumToScalarType (name : Name) : M (Option IRType) := do

View file

@ -40,7 +40,9 @@ inductive LitValue where
| uint16 (val : UInt16)
| uint32 (val : UInt32)
| uint64 (val : UInt64)
-- TODO: add constructors for `Int`, `Float`, `USize` ...
-- USize has a maximum size of 64 bits
| usize (val : UInt64)
-- TODO: add constructors for `Int`, `Float`, ...
deriving Inhabited, BEq, Hashable
def LitValue.toExpr : LitValue → Expr
@ -50,6 +52,7 @@ def LitValue.toExpr : LitValue → Expr
| .uint16 v => .app (.const ``UInt16.ofNat []) (.lit (.natVal (UInt16.toNat v)))
| .uint32 v => .app (.const ``UInt32.ofNat []) (.lit (.natVal (UInt32.toNat v)))
| .uint64 v => .app (.const ``UInt64.ofNat []) (.lit (.natVal (UInt64.toNat v)))
| .usize v => .app (.const ``USize.ofNat []) (.lit (.natVal (UInt64.toNat v)))
inductive Arg where
| erased

View file

@ -177,7 +177,7 @@ def ofLCNFLit : LCNF.LitValue → Value
| .uint8 v => ofNat (UInt8.toNat v)
| .uint16 v => ofNat (UInt16.toNat v)
| .uint32 v => ofNat (UInt32.toNat v)
| .uint64 v => ofNat (UInt64.toNat v)
| .uint64 v | .usize v => ofNat (UInt64.toNat v)
partial def proj : Value → Nat → Value
| .ctor _ vs , i => vs.getD i bot

View file

@ -109,6 +109,7 @@ def inferLitValueType (value : LitValue) : Expr :=
| .uint16 .. => mkConst ``UInt16
| .uint32 .. => mkConst ``UInt32
| .uint64 .. => mkConst ``UInt64
| .usize .. => mkConst ``USize
mutual
partial def inferArgType (arg : Arg) : InferTypeM Expr :=

View file

@ -58,7 +58,7 @@ def ppArgs (args : Array Arg) : M Format := do
def ppLitValue (lit : LitValue) : M Format := do
match lit with
| .nat v | .uint8 v | .uint16 v | .uint32 v | .uint64 v => return format v
| .nat v | .uint8 v | .uint16 v | .uint32 v | .uint64 v | .usize v => return format v
| .str v => return format (repr v)
def ppLetValue (e : LetValue) : M Format := do

View file

@ -368,6 +368,7 @@ def conversionFolders : List (Name × Folder) := [
(``UInt16.ofNat, Folder.ofNat (fun v => .uint16 (UInt16.ofNat v))),
(``UInt32.ofNat, Folder.ofNat (fun v => .uint32 (UInt32.ofNat v))),
(``UInt64.ofNat, Folder.ofNat (fun v => .uint64 (UInt64.ofNat v))),
(``USize.ofNat, Folder.ofNat (fun v => .usize (UInt64.ofNat v))),
]
/--