Skip to content

Commit 78da075

Browse files
committed
feat: add support for USize literals in LCNF
1 parent 3b20550 commit 78da075

File tree

7 files changed

+15
-5
lines changed

7 files changed

+15
-5
lines changed

src/Lean/Compiler/IR/EmitC.lean

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,13 @@ def emitNumLit (t : IRType) (v : Nat) : M Unit := do
497497
else
498498
if v < UInt32.size then
499499
emit v
500+
else if t == .usize then
501+
emit "((size_t)"
502+
emit v
503+
emit "ULL)"
500504
else
501-
emit v; emit "ULL"
505+
emit v
506+
emit "ULL"
502507

503508
def emitLit (z : VarId) (t : IRType) (v : LitVal) : M Unit := do
504509
emitLhs z;

src/Lean/Compiler/IR/ToIR.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def lowerLitValue (v : LCNF.LitValue) : LitVal :=
7070
| .uint8 v => .num (UInt8.toNat v)
7171
| .uint16 v => .num (UInt16.toNat v)
7272
| .uint32 v => .num (UInt32.toNat v)
73-
| .uint64 v => .num (UInt64.toNat v)
73+
| .uint64 v | .usize v => .num (UInt64.toNat v)
7474

7575
-- TODO: This should be cached.
7676
def lowerEnumToScalarType (name : Name) : M (Option IRType) := do

src/Lean/Compiler/LCNF/Basic.lean

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ inductive LitValue where
4040
| uint16 (val : UInt16)
4141
| uint32 (val : UInt32)
4242
| uint64 (val : UInt64)
43-
-- TODO: add constructors for `Int`, `Float`, `USize` ...
43+
-- USize has a maximum size of 64 bits
44+
| usize (val : UInt64)
45+
-- TODO: add constructors for `Int`, `Float`, ...
4446
deriving Inhabited, BEq, Hashable
4547

4648
def LitValue.toExpr : LitValue → Expr
@@ -50,6 +52,7 @@ def LitValue.toExpr : LitValue → Expr
5052
| .uint16 v => .app (.const ``UInt16.ofNat []) (.lit (.natVal (UInt16.toNat v)))
5153
| .uint32 v => .app (.const ``UInt32.ofNat []) (.lit (.natVal (UInt32.toNat v)))
5254
| .uint64 v => .app (.const ``UInt64.ofNat []) (.lit (.natVal (UInt64.toNat v)))
55+
| .usize v => .app (.const ``USize.ofNat []) (.lit (.natVal (UInt64.toNat v)))
5356

5457
inductive Arg where
5558
| erased

src/Lean/Compiler/LCNF/ElimDeadBranches.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def ofLCNFLit : LCNF.LitValue → Value
177177
| .uint8 v => ofNat (UInt8.toNat v)
178178
| .uint16 v => ofNat (UInt16.toNat v)
179179
| .uint32 v => ofNat (UInt32.toNat v)
180-
| .uint64 v => ofNat (UInt64.toNat v)
180+
| .uint64 v | .usize v => ofNat (UInt64.toNat v)
181181

182182
partial def proj : Value → Nat → Value
183183
| .ctor _ vs , i => vs.getD i bot

src/Lean/Compiler/LCNF/InferType.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def inferLitValueType (value : LitValue) : Expr :=
109109
| .uint16 .. => mkConst ``UInt16
110110
| .uint32 .. => mkConst ``UInt32
111111
| .uint64 .. => mkConst ``UInt64
112+
| .usize .. => mkConst ``USize
112113

113114
mutual
114115
partial def inferArgType (arg : Arg) : InferTypeM Expr :=

src/Lean/Compiler/LCNF/PrettyPrinter.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def ppArgs (args : Array Arg) : M Format := do
5858

5959
def ppLitValue (lit : LitValue) : M Format := do
6060
match lit with
61-
| .nat v | .uint8 v | .uint16 v | .uint32 v | .uint64 v => return format v
61+
| .nat v | .uint8 v | .uint16 v | .uint32 v | .uint64 v | .usize v => return format v
6262
| .str v => return format (repr v)
6363

6464
def ppLetValue (e : LetValue) : M Format := do

src/Lean/Compiler/LCNF/Simp/ConstantFold.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ def conversionFolders : List (Name × Folder) := [
368368
(``UInt16.ofNat, Folder.ofNat (fun v => .uint16 (UInt16.ofNat v))),
369369
(``UInt32.ofNat, Folder.ofNat (fun v => .uint32 (UInt32.ofNat v))),
370370
(``UInt64.ofNat, Folder.ofNat (fun v => .uint64 (UInt64.ofNat v))),
371+
(``USize.ofNat, Folder.ofNat (fun v => .usize (UInt64.ofNat v))),
371372
]
372373

373374
/--

0 commit comments

Comments
 (0)