diff --git a/src/Lean/Compiler/IR/EmitC.lean b/src/Lean/Compiler/IR/EmitC.lean index bf6489bdd4..3fa7fab2c5 100644 --- a/src/Lean/Compiler/IR/EmitC.lean +++ b/src/Lean/Compiler/IR/EmitC.lean @@ -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; diff --git a/src/Lean/Compiler/IR/ToIR.lean b/src/Lean/Compiler/IR/ToIR.lean index 6ca8bd730a..be11ebab2b 100644 --- a/src/Lean/Compiler/IR/ToIR.lean +++ b/src/Lean/Compiler/IR/ToIR.lean @@ -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 diff --git a/src/Lean/Compiler/LCNF/Basic.lean b/src/Lean/Compiler/LCNF/Basic.lean index 2da4e53f03..41d3e4df4a 100644 --- a/src/Lean/Compiler/LCNF/Basic.lean +++ b/src/Lean/Compiler/LCNF/Basic.lean @@ -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 diff --git a/src/Lean/Compiler/LCNF/ElimDeadBranches.lean b/src/Lean/Compiler/LCNF/ElimDeadBranches.lean index 227194df39..52acc3f602 100644 --- a/src/Lean/Compiler/LCNF/ElimDeadBranches.lean +++ b/src/Lean/Compiler/LCNF/ElimDeadBranches.lean @@ -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 diff --git a/src/Lean/Compiler/LCNF/InferType.lean b/src/Lean/Compiler/LCNF/InferType.lean index f6b0aad9dd..95ae15423d 100644 --- a/src/Lean/Compiler/LCNF/InferType.lean +++ b/src/Lean/Compiler/LCNF/InferType.lean @@ -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 := diff --git a/src/Lean/Compiler/LCNF/PrettyPrinter.lean b/src/Lean/Compiler/LCNF/PrettyPrinter.lean index 2032787f44..8b64541fbc 100644 --- a/src/Lean/Compiler/LCNF/PrettyPrinter.lean +++ b/src/Lean/Compiler/LCNF/PrettyPrinter.lean @@ -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 diff --git a/src/Lean/Compiler/LCNF/Simp/ConstantFold.lean b/src/Lean/Compiler/LCNF/Simp/ConstantFold.lean index 0dcf52b684..bda10a8fd1 100644 --- a/src/Lean/Compiler/LCNF/Simp/ConstantFold.lean +++ b/src/Lean/Compiler/LCNF/Simp/ConstantFold.lean @@ -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))), ] /--