From 1efa56d4800e0a9d41ec82e206e208075f46b198 Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Sat, 13 Jun 2026 00:37:14 +0200 Subject: [PATCH 1/3] Support dynamic internal helper args --- Compiler/CompilationModel.lean | 1 + Compiler/CompilationModel/Compile.lean | 82 ++++-- Compiler/CompilationModel/Dispatch.lean | 52 ++-- Compiler/CompilationModel/InternalArgs.lean | 78 ++++++ .../CompilationModel/ScopeValidation.lean | 23 +- .../CompilationModel/ValidationCalls.lean | 150 +++++++---- Compiler/CompilationModelFeatureTest.lean | 53 ++++ Compiler/Proofs/IRGeneration/Contract.lean | 222 ++++++++-------- .../IRGeneration/ContractFeatureTest.lean | 85 +++++-- .../Proofs/IRGeneration/ContractShape.lean | 238 +++++++++--------- Compiler/Proofs/IRGeneration/Function.lean | 6 +- .../IRGeneration/FunctionBody/Stmt.lean | 10 +- .../IRGeneration/GenericInduction/Calls.lean | 6 +- .../GenericInduction/Helpers.lean | 9 +- .../GenericInduction/ResultRelation.lean | 3 +- .../Proofs/IRGeneration/IRInterpreter.lean | 14 +- .../Proofs/IRGeneration/SupportedSpec.lean | 18 +- .../Backends/EvmYulLeanCallClosure.lean | 117 ++++++--- PrintAxioms.lean | 12 +- 19 files changed, 726 insertions(+), 453 deletions(-) create mode 100644 Compiler/CompilationModel/InternalArgs.lean diff --git a/Compiler/CompilationModel.lean b/Compiler/CompilationModel.lean index 5f1aa9d4a..a43c7755d 100644 --- a/Compiler/CompilationModel.lean +++ b/Compiler/CompilationModel.lean @@ -13,6 +13,7 @@ import Compiler.CompilationModel.DynamicData import Compiler.CompilationModel.EcmAxiomCollection import Compiler.CompilationModel.EventEmission import Compiler.CompilationModel.EventAbiHelpers +import Compiler.CompilationModel.InternalArgs import Compiler.CompilationModel.InternalNaming import Compiler.CompilationModel.IssueRefs import Compiler.CompilationModel.LayoutReport diff --git a/Compiler/CompilationModel/Compile.lean b/Compiler/CompilationModel/Compile.lean index e57d8ce90..2077e2406 100644 --- a/Compiler/CompilationModel/Compile.lean +++ b/Compiler/CompilationModel/Compile.lean @@ -26,6 +26,7 @@ import Compiler.CompilationModel.AbiEncoding import Compiler.CompilationModel.DynamicData import Compiler.CompilationModel.EcmAxiomCollection import Compiler.CompilationModel.EventEmission +import Compiler.CompilationModel.InternalArgs import Compiler.CompilationModel.InternalNaming import Compiler.CompilationModel.LayoutValidation import Compiler.CompilationModel.MappingWrites @@ -52,6 +53,52 @@ def unsafeYulToEVMYul (fragment : UnsafeYulFragment) : List YulStmt := theorem unsafeYulToEVMYul_eq (fragment : UnsafeYulFragment) : unsafeYulToEVMYul fragment = fragment.stmts := rfl +def findInternalFunctionForCall? (functions : List FunctionSpec) (name : String) : Option FunctionSpec := + match functions.filter (fun fn => fn.isInternal && fn.name == name) with + | [fn] => some fn + | _ => none + +def directForwardedInternalCallArgName? : Expr → Option String + | Expr.param name => some name + | Expr.localVar name => some name + | _ => none + +def compileInternalCallArg (fields : List Field) (dynamicSource : DynamicDataSource) + (calleeName : String) (param : Param) (arg : Expr) : Except String (List YulExpr) := do + if isExpandedInternalParamType param.ty then + match directForwardedInternalCallArgName? arg with + | some name => + pure ((internalCallYulArgNamesForParam name param).map YulExpr.ident) + | none => + throw s!"Compilation error: internal call '{calleeName}' argument for parameter '{param.name}' with type {repr param.ty} must be a direct parameter/local forwarding expression (issue #1889)." + else + pure [← compileExpr fields dynamicSource arg] + +def compileInternalCallArgsWithParams (fields : List Field) (dynamicSource : DynamicDataSource) + (calleeName : String) : List Param → List Expr → Except String (List YulExpr) + | [], [] => pure [] + | param :: params, arg :: args => do + let head ← compileInternalCallArg fields dynamicSource calleeName param arg + let tail ← compileInternalCallArgsWithParams fields dynamicSource calleeName params args + pure (head ++ tail) + | params, args => + throw s!"Compilation error: internal call '{calleeName}' received {args.length} source arg(s), expected {params.length} (issue #1889)." + +def compileInternalCallArgs (fields : List Field) (dynamicSource : DynamicDataSource) + (internalFunctions : List FunctionSpec) (calleeName : String) (args : List Expr) : + Except String (List YulExpr) := + match findInternalFunctionForCall? internalFunctions calleeName with + | some callee => + let legacyArgCount := + callee.params.foldl (fun acc param => acc + (internalFunctionYulParamNames [param]).length) 0 + if args.length == callee.params.length then + compileInternalCallArgsWithParams fields dynamicSource calleeName callee.params args + else if args.length == legacyArgCount then + compileExprList fields dynamicSource args + else + compileInternalCallArgsWithParams fields dynamicSource calleeName callee.params args + | none => compileExprList fields dynamicSource args + private def compileAdtStorageWrite (fields : List Field) (dynamicSource : DynamicDataSource) (adtTypes : List AdtTypeDef) (storageField adtName variantName : String) (args : List Expr) : @@ -118,13 +165,15 @@ def compileStmtList (fields : List Field) (events : List EventDef := []) (internalRetNames : List String := []) (isInternal : Bool := false) (inScopeNames : List String := []) - (adtTypes : List AdtTypeDef := []) : - List Stmt → Except String (List YulStmt) + (adtTypes : List AdtTypeDef := []) + (stmts : List Stmt) (internalFunctions : List FunctionSpec := []) : + Except String (List YulStmt) := + match stmts with | [] => pure [] | s :: ss => do - let head ← compileStmt fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes s + let head ← compileStmt fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes s internalFunctions let nextScopeNames := collectStmtNames s ++ inScopeNames - let tail ← compileStmtList fields events errors dynamicSource internalRetNames isInternal nextScopeNames adtTypes ss + let tail ← compileStmtList fields events errors dynamicSource internalRetNames isInternal nextScopeNames adtTypes ss internalFunctions pure (head ++ tail) def compileStmt (fields : List Field) (events : List EventDef := []) @@ -133,8 +182,10 @@ def compileStmt (fields : List Field) (events : List EventDef := []) (internalRetNames : List String := []) (isInternal : Bool := false) (inScopeNames : List String := []) - (adtTypes : List AdtTypeDef := []) : - Stmt → Except String (List YulStmt) + (adtTypes : List AdtTypeDef := []) + (stmt : Stmt) (internalFunctions : List FunctionSpec := []) : + Except String (List YulStmt) + := match stmt with | Stmt.letVar name value => do pure [YulStmt.let_ name (← compileExpr fields dynamicSource value)] | Stmt.assignVar name value => do @@ -251,8 +302,8 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | Stmt.ite cond thenBranch elseBranch => do -- If/else: compile to Yul if + negated if (#179) let condExpr ← compileExpr fields dynamicSource cond - let thenStmts ← compileStmtList fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes thenBranch - let elseStmts ← compileStmtList fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes elseBranch + let thenStmts ← compileStmtList fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes thenBranch internalFunctions + let elseStmts ← compileStmtList fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes elseBranch internalFunctions if elseBranch.isEmpty then -- Simple if (no else) pure [YulStmt.if_ condExpr thenStmts] @@ -281,7 +332,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) let countName := pickFreshName "__forEach_count" (idxName :: forUsedNames) -- Compile the body with the synthetic counters in scope (see `forEachBodyScope`), -- so a nested `forEach` cannot re-derive colliding `__forEach_idx`/`__forEach_count`. - let bodyStmts ← compileStmtList fields events errors dynamicSource internalRetNames isInternal (forEachBodyScope inScopeNames varName count body) adtTypes body + let bodyStmts ← compileStmtList fields events errors dynamicSource internalRetNames isInternal (forEachBodyScope inScopeNames varName count body) adtTypes body internalFunctions let initStmts := [ YulStmt.let_ idxName (YulExpr.lit 0), YulStmt.let_ countName countExpr, @@ -294,7 +345,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | Stmt.unsafeBlock _ body => do -- Unsafe block: transparent wrapper, compile inner body directly (#1728, Axis 6 Step 6a) - compileStmtList fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes body + compileStmtList fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes body internalFunctions | Stmt.unsafeYul fragment => pure (unsafeYulToEVMYul fragment) @@ -304,10 +355,10 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | Stmt.internalCall functionName args => do -- Internal function call as statement (#181) - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileInternalCallArgs fields dynamicSource internalFunctions functionName args pure [YulStmt.expr (YulExpr.call (internalFunctionYulName functionName) argExprs)] | Stmt.internalCallAssign names functionName args => do - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileInternalCallArgs fields dynamicSource internalFunctions functionName args pure [YulStmt.letMany names (YulExpr.call (internalFunctionYulName functionName) argExprs)] | Stmt.externalCallBind resultVars externalName args => do let argExprs ← compileExprList fields dynamicSource args @@ -508,7 +559,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | none => throw s!"Compilation error: unknown storage field '{storageFieldName}' for matchAdt on '{adtName}'" -- Build switch cases: each branch matches on the variant's tag let cases ← compileMatchAdtBranches fields events errors dynamicSource internalRetNames isInternal - inScopeNames adtTypes def_ baseSlot branches + inScopeNames adtTypes internalFunctions def_ baseSlot branches -- Default case: revert (should be unreachable for exhaustive matches) let defaultCase := [YulStmt.expr (YulExpr.call "revert" [YulExpr.lit 0, YulExpr.lit 0])] pure [YulStmt.switch scrutineeExpr cases (some defaultCase)] @@ -517,6 +568,7 @@ def compileMatchAdtBranches (fields : List Field) (events : List EventDef) (errors : List ErrorDef) (dynamicSource : DynamicDataSource) (internalRetNames : List String) (isInternal : Bool) (inScopeNames : List String) (adtTypes : List AdtTypeDef) + (internalFunctions : List FunctionSpec) (def_ : AdtTypeDef) (baseSlot : Nat) : List (String × List String × List Stmt) → Except String (List (Nat × List YulStmt)) | [] => pure [] @@ -528,9 +580,9 @@ def compileMatchAdtBranches (fields : List Field) (events : List EventDef) let fieldBindings := boundVarNames.zipIdx.map fun (varName, idx) => YulStmt.let_ varName (compileAdtFieldRead (YulExpr.lit baseSlot) idx) let bodyStmts ← compileStmtList fields events errors dynamicSource internalRetNames isInternal - (boundVarNames.reverse ++ inScopeNames) adtTypes body + (boundVarNames.reverse ++ inScopeNames) adtTypes body internalFunctions let restCases ← compileMatchAdtBranches fields events errors dynamicSource internalRetNames isInternal - inScopeNames adtTypes def_ baseSlot rest + inScopeNames adtTypes internalFunctions def_ baseSlot rest pure ((variant.tag, fieldBindings ++ bodyStmts) :: restCases) end diff --git a/Compiler/CompilationModel/Dispatch.lean b/Compiler/CompilationModel/Dispatch.lean index 18022ca9c..ada86d54e 100644 --- a/Compiler/CompilationModel/Dispatch.lean +++ b/Compiler/CompilationModel/Dispatch.lean @@ -5,6 +5,7 @@ the lower-level statement/expression compilation helpers. -/ import Compiler.CompilationModel.Compile +import Compiler.CompilationModel.InternalArgs import Compiler.CompilationModel.ParamLoading import Compiler.CompilationModel.ScopeValidation import Compiler.CompilationModel.TrustSurface @@ -35,33 +36,10 @@ def freshInternalRetNames (returns : List ParamType) (usedNames : List String) : (usedNames, []) namesRev.reverse -def internalFunctionYulParamNames (params : List Param) : List String := - params.flatMap fun param => - match param.ty with - | ParamType.array _ => - [s!"{param.name}_data_offset", s!"{param.name}_length"] - | ParamType.bytes | ParamType.string => - [s!"{param.name}_data_offset", s!"{param.name}_length"] - | ParamType.fixedArray _ _ => - if isDynamicParamType param.ty then - [s!"{param.name}_data_offset"] - else - staticParamBindingNames param.name param.ty - | ParamType.tuple _ => - if isDynamicParamType param.ty then - [s!"{param.name}_data_offset"] - else - staticParamBindingNames param.name param.ty - | ParamType.newtypeOf _ baseTy => - if isDynamicParamType param.ty then - [s!"{param.name}_data_offset"] - else - staticParamBindingNames param.name baseTy - | _ => [param.name] - -- Compile internal function to a Yul function definition (#181) def compileInternalFunction (fields : List Field) (events : List EventDef) (errors : List ErrorDef) - (adtTypes : List AdtTypeDef := []) (spec : FunctionSpec) : + (adtTypes : List AdtTypeDef := []) (spec : FunctionSpec) + (internalFunctions : List FunctionSpec := []) : Except String YulStmt := do validateFunctionSpec spec let returns ← functionReturns spec @@ -69,7 +47,7 @@ def compileInternalFunction (fields : List Field) (events : List EventDef) (erro let usedNames := paramNames ++ collectStmtListBindNames spec.body let retNames := freshInternalRetNames returns usedNames let bodyStmts ← compileStmtList fields events errors .calldata retNames true - (paramNames ++ retNames) adtTypes spec.body + (paramNames ++ retNames) adtTypes spec.body internalFunctions pure (YulStmt.funcDef (internalFunctionYulName spec.name) paramNames retNames bodyStmts) theorem compileInternalFunction_ok_components @@ -189,13 +167,14 @@ theorem compileInternalFunction_some_ok_of_components -- Compile function spec to IR function def compileFunctionSpec (fields : List Field) (events : List EventDef) (errors : List ErrorDef) - (adtTypes : List AdtTypeDef := []) (selector : Nat) (spec : FunctionSpec) : + (adtTypes : List AdtTypeDef := []) (selector : Nat) (spec : FunctionSpec) + (internalFunctions : List FunctionSpec := []) : Except String IRFunction := do validateFunctionSpec spec let returns ← functionReturns spec let paramLoads := genParamLoads spec.params let bodyStmts ← compileStmtList fields events errors .calldata [] false - (spec.params.map (·.name)) adtTypes spec.body + (spec.params.map (·.name)) adtTypes spec.body internalFunctions let allStmts := paramLoads ++ bodyStmts let retType := match returns with | [single] => single.toIRType @@ -310,14 +289,15 @@ def usesMapping (fields : List Field) : Bool := -- Compile deploy code (constructor) -- Note: Don't append datacopy/return here - Codegen.deployCode does that def compileConstructor (fields : List Field) (events : List EventDef) (errors : List ErrorDef) - (adtTypes : List AdtTypeDef := []) (ctor : Option ConstructorSpec) : + (adtTypes : List AdtTypeDef := []) (ctor : Option ConstructorSpec) + (internalFunctions : List FunctionSpec := []) : Except String (List YulStmt) := do match ctor with | none => return [] | some spec => let argLoads := genConstructorArgLoads spec.params let bodyChunks ← compileStmtList fields events errors .memory [] false - (spec.params.map (·.name)) adtTypes spec.body + (spec.params.map (·.name)) adtTypes spec.body internalFunctions return argLoads ++ bodyChunks -- Main compilation function @@ -412,7 +392,7 @@ private def validateCompileInputsBeforeFieldWriteConflict | some ctor => do ctor.body.forM (validateEventArgShapesInStmt "constructor" ctor.params spec.events) ctor.body.forM (validateCustomErrorArgShapesInStmt "constructor" ctor.params spec.errors) - ctor.body.forM (validateInternalCallShapesInStmt spec.functions "constructor") + ctor.body.forM (validateInternalCallShapesInStmt spec.functions "constructor" ctor.params) for ext in spec.externals do let _ ← externalFunctionReturns ext validateInteropExternalSpec ext @@ -552,8 +532,9 @@ def validateCompileInputs (spec : CompilationModel) (selectors : List Nat) `compileFunctionSpec` (see `attachNonReentrantGuard`). -/ def compileGuardedFunctionSpec (fields : List Field) (events : List EventDef) (errors : List ErrorDef) (adtTypes : List AdtTypeDef) + (internalFunctions : List FunctionSpec) (sel : Nat) (fnSpec : FunctionSpec) : Except String IRFunction := do - let irFn ← compileFunctionSpec fields events errors adtTypes sel fnSpec + let irFn ← compileFunctionSpec fields events errors adtTypes sel fnSpec internalFunctions attachNonReentrantGuard fields fnSpec irFn def compileValidatedCore (spec : CompilationModel) (selectors : List Nat) : Except String IRContract := do @@ -570,8 +551,9 @@ def compileValidatedCore (spec : CompilationModel) (selectors : List Nat) : Exce let fallbackSpec ← pickUniqueFunctionByName "fallback" spec.functions let receiveSpec ← pickUniqueFunctionByName "receive" spec.functions let functions ← (externalFns.zip selectors).mapM fun entry => - compileGuardedFunctionSpec fields spec.events spec.errors spec.adtTypes entry.2 entry.1 - let internalFuncDefs ← internalFns.mapM (compileInternalFunction fields spec.events spec.errors spec.adtTypes) + compileGuardedFunctionSpec fields spec.events spec.errors spec.adtTypes internalFns entry.2 entry.1 + let internalFuncDefs ← internalFns.mapM fun fn => + compileInternalFunction fields spec.events spec.errors spec.adtTypes fn internalFns let arrayElementHelpers := (if arrayHelpersRequired then [ checkedArrayElementCalldataHelper @@ -635,7 +617,7 @@ def compileValidatedCore (spec : CompilationModel) (selectors : List Nat) : Exce let receiveEntrypoint ← receiveSpec.mapM (compileSpecialEntrypoint fields spec.events spec.errors spec.adtTypes) return { name := spec.name - deploy := (← compileConstructor fields spec.events spec.errors spec.adtTypes spec.constructor) + deploy := (← compileConstructor fields spec.events spec.errors spec.adtTypes spec.constructor internalFns) constructorPayable := spec.constructor.map (·.isPayable) |>.getD false functions := functions fallbackEntrypoint := fallbackEntrypoint diff --git a/Compiler/CompilationModel/InternalArgs.lean b/Compiler/CompilationModel/InternalArgs.lean new file mode 100644 index 000000000..aee13ff39 --- /dev/null +++ b/Compiler/CompilationModel/InternalArgs.lean @@ -0,0 +1,78 @@ +import Compiler.CompilationModel.Types +import Compiler.CompilationModel.AbiTypeLayout + +namespace Compiler.CompilationModel + +partial def staticParamBindingNames (name : String) (ty : ParamType) : List String := + match ty with + | ParamType.uint256 | ParamType.int256 | ParamType.uint8 | ParamType.uint16 + | ParamType.address | ParamType.bool | ParamType.bytes32 => + [name] + | ParamType.fixedArray elemTy n => + (List.range n).flatMap (fun i => staticParamBindingNames s!"{name}_{i}" elemTy) + | ParamType.tuple elemTys => + let rec go (tys : List ParamType) (idx : Nat) : List String := + match tys with + | [] => [] + | elemTy :: rest => + staticParamBindingNames s!"{name}_{idx}" elemTy ++ go rest (idx + 1) + go elemTys 0 + | ParamType.adt _ maxFields => + name :: (List.range maxFields).map (fun i => s!"{name}_f{i}") + | ParamType.newtypeOf _ baseType => + staticParamBindingNames name baseType + | _ => [] + +def dynamicParamBindingNames (name : String) : List String := + [s!"{name}_offset", s!"{name}_length", s!"{name}_data_offset"] + +def internalFunctionYulParamNames (params : List Param) : List String := + params.flatMap fun param => + match param.ty with + | ParamType.array _ => + [s!"{param.name}_data_offset", s!"{param.name}_length"] + | ParamType.bytes | ParamType.string => + [s!"{param.name}_data_offset", s!"{param.name}_length"] + | ParamType.fixedArray _ _ => + if isDynamicParamType param.ty then + [s!"{param.name}_data_offset"] + else + staticParamBindingNames param.name param.ty + | ParamType.tuple _ => + if isDynamicParamType param.ty then + [s!"{param.name}_data_offset"] + else + staticParamBindingNames param.name param.ty + | ParamType.newtypeOf _ baseTy => + if isDynamicParamType param.ty then + [s!"{param.name}_data_offset"] + else + staticParamBindingNames param.name baseTy + | ParamType.adt _ _ => + staticParamBindingNames param.name param.ty + | _ => [param.name] + +def internalCallYulArgNamesForBase (name : String) : ParamType → List String + | ParamType.array _ => [s!"{name}_data_offset", s!"{name}_length"] + | ParamType.bytes | ParamType.string => [s!"{name}_data_offset", s!"{name}_length"] + | ty@(ParamType.fixedArray _ _) => + if isDynamicParamType ty then [s!"{name}_data_offset"] else staticParamBindingNames name ty + | ty@(ParamType.tuple _) => + if isDynamicParamType ty then [s!"{name}_data_offset"] else staticParamBindingNames name ty + | ParamType.newtypeOf _ baseTy => internalCallYulArgNamesForBase name baseTy + | ty@(ParamType.adt _ _) => staticParamBindingNames name ty + | _ => [name] + +def internalCallYulArgNamesForParam (sourceName : String) (param : Param) : List String := + match param.ty with + | ParamType.adt _ _ => staticParamBindingNames sourceName param.ty + | _ => internalCallYulArgNamesForBase sourceName param.ty + +def isExpandedInternalParamType : ParamType → Bool + | ParamType.array _ | ParamType.bytes | ParamType.string => true + | ParamType.fixedArray _ _ | ParamType.tuple _ => true + | ParamType.newtypeOf _ baseTy => isExpandedInternalParamType baseTy + | ParamType.adt _ _ => true + | _ => false + +end Compiler.CompilationModel diff --git a/Compiler/CompilationModel/ScopeValidation.lean b/Compiler/CompilationModel/ScopeValidation.lean index 9755385b5..b5280f22a 100644 --- a/Compiler/CompilationModel/ScopeValidation.lean +++ b/Compiler/CompilationModel/ScopeValidation.lean @@ -1,5 +1,6 @@ import Compiler.CompilationModel.Types import Compiler.CompilationModel.AbiTypeLayout +import Compiler.CompilationModel.InternalArgs import Compiler.CompilationModel.IssueRefs import Compiler.CompilationModel.LogicalPurity import Compiler.CompilationModel.EcmAxiomCollection @@ -11,28 +12,6 @@ namespace Compiler.CompilationModel def findParamType (params : List Param) (name : String) : Option ParamType := (params.find? (fun p => p.name == name)).map (·.ty) -partial def staticParamBindingNames (name : String) (ty : ParamType) : List String := - match ty with - | ParamType.uint256 | ParamType.int256 | ParamType.uint8 | ParamType.uint16 | ParamType.address | ParamType.bool | ParamType.bytes32 => - [name] - | ParamType.fixedArray elemTy n => - (List.range n).flatMap (fun i => staticParamBindingNames s!"{name}_{i}" elemTy) - | ParamType.tuple elemTys => - let rec go (tys : List ParamType) (idx : Nat) : List String := - match tys with - | [] => [] - | elemTy :: rest => - staticParamBindingNames s!"{name}_{idx}" elemTy ++ go rest (idx + 1) - go elemTys 0 - | ParamType.adt _ maxFields => - name :: (List.range maxFields).map (fun i => s!"{name}_f{i}") - | ParamType.newtypeOf _ baseType => - staticParamBindingNames name baseType - | _ => [] - -def dynamicParamBindingNames (name : String) : List String := - [s!"{name}_offset", s!"{name}_length", s!"{name}_data_offset"] - mutual def isDynamicParamTypeForScope : ParamType → Bool | ParamType.uint256 => false diff --git a/Compiler/CompilationModel/ValidationCalls.lean b/Compiler/CompilationModel/ValidationCalls.lean index d187b01d9..ed5bc3db1 100644 --- a/Compiler/CompilationModel/ValidationCalls.lean +++ b/Compiler/CompilationModel/ValidationCalls.lean @@ -5,6 +5,7 @@ import Compiler.CompilationModel.Types import Compiler.CompilationModel.AbiHelpers import Compiler.CompilationModel.AbiTypeLayout import Compiler.CompilationModel.DynamicData +import Compiler.CompilationModel.InternalArgs import Compiler.CompilationModel.InternalNaming import Compiler.CompilationModel.IssueRefs import Compiler.CompilationModel.ScopeValidation @@ -156,90 +157,133 @@ def findInternalFunctionByName (functions : List FunctionSpec) | _ => throw s!"Compilation error: function '{callerName}' references ambiguous internal function '{calleeName}' ({issue625Ref})." +def directForwardedInternalArgName? : Expr → Option String + | Expr.param name => some name + | Expr.localVar name => some name + | _ => none + +def validateInternalCallArgForParam + (callerParams : List Param) (callerName calleeName : String) + (param : Param) (arg : Expr) : Except String Unit := do + if isExpandedInternalParamType param.ty then + match directForwardedInternalArgName? arg with + | none => + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with a computed argument for expanded parameter '{param.name}' ({repr param.ty}); issue #1889 currently supports direct parameter/local forwarding only." + | some _ => pure () + else + pure () + if isExpandedInternalParamType param.ty then + match arg with + | Expr.param sourceName => + match findParamType callerParams sourceName with + | some sourceTy => + if sourceTy == param.ty then + pure () + else + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with parameter '{sourceName}' of type {repr sourceTy}, expected {repr param.ty} for expanded callee parameter '{param.name}' (issue #1889)." + | none => pure () + | _ => pure () + else + pure () + +def validateInternalCallSourceArgs + (callerParams : List Param) (callerName calleeName : String) + (params : List Param) (args : List Expr) : Except String Unit := do + let legacyArgCount := + params.foldl (fun acc param => acc + (internalFunctionYulParamNames [param]).length) 0 + if args.length == legacyArgCount && args.length != params.length then + pure () + else if args.length != params.length then + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with {args.length} source arg(s), expected {params.length} (or {legacyArgCount} expanded Yul arg(s) for legacy call sites) ({issue625Ref}, issue #1889)." + else + let rec go : List Param → List Expr → Except String Unit + | [], [] => pure () + | param :: params, arg :: args => do + validateInternalCallArgForParam callerParams callerName calleeName param arg + go params args + | _, _ => pure () + go params args + /-- Node-local check: shape of an `Expr.internalCall` node. Operands are reached via the canonical post-order `Expr.forDeepPostM`, matching the old walk which validated arguments before the call's own arity/return shape. -/ def validateInternalCallShapesNodeExpr - (functions : List FunctionSpec) (callerName : String) : Expr → Except String Unit + (functions : List FunctionSpec) (callerName : String) (callerParams : List Param) : Expr → Except String Unit | Expr.internalCall calleeName args => do let callee ← findInternalFunctionByName functions callerName calleeName - let expectedArgs := internalCallYulArgCount callee.params - if args.length != expectedArgs then - throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with {args.length} Yul arg(s), expected {expectedArgs} ({issue625Ref})." + validateInternalCallSourceArgs callerParams callerName calleeName callee.params args let returns ← functionReturns callee if returns.length != 1 || internalReturnYulCount returns != 1 then throw s!"Compilation error: function '{callerName}' uses Expr.internalCall '{calleeName}' but callee returns {returns.length} logical value(s) / {internalReturnYulCount returns} Yul value(s); use Stmt.internalCallAssign for multi-return calls ({issue625Ref})." | _ => pure () def validateInternalCallShapesInExpr - (functions : List FunctionSpec) (callerName : String) (e : Expr) : Except String Unit := - e.forDeepPostM (validateInternalCallShapesNodeExpr functions callerName) + (functions : List FunctionSpec) (callerName : String) (callerParams : List Param) (e : Expr) : Except String Unit := + e.forDeepPostM (validateInternalCallShapesNodeExpr functions callerName callerParams) def validateInternalCallShapesInExprList - (functions : List FunctionSpec) (callerName : String) (es : List Expr) : Except String Unit := - es.forM (validateInternalCallShapesInExpr functions callerName) + (functions : List FunctionSpec) (callerName : String) (callerParams : List Param) (es : List Expr) : Except String Unit := + es.forM (validateInternalCallShapesInExpr functions callerName callerParams) /-- Node-local statement check: validates the statement's own expressions and internal-call shape; nested statement bodies are reached via the canonical `Stmt.forDeepM`. `returnArray`/`returnBytes`/`returnStorageWords`/ `returnCodeData` deliberately contribute nothing here (as in the old walk). -/ def validateInternalCallShapesNodeStmt - (functions : List FunctionSpec) (callerName : String) : Stmt → Except String Unit + (functions : List FunctionSpec) (callerName : String) (callerParams : List Param) : Stmt → Except String Unit | Stmt.letVar _ value | Stmt.assignVar _ value | Stmt.setStorage _ value | Stmt.setStorageAddr _ value | Stmt.setStorageWord _ _ value | Stmt.storageArrayPush _ value | Stmt.return value | Stmt.require value _ => - validateInternalCallShapesInExpr functions callerName value + validateInternalCallShapesInExpr functions callerName callerParams value | Stmt.setStorageArrayElement _ index value => do - validateInternalCallShapesInExpr functions callerName index - validateInternalCallShapesInExpr functions callerName value + validateInternalCallShapesInExpr functions callerName callerParams index + validateInternalCallShapesInExpr functions callerName callerParams value | Stmt.storageArrayPop _ => pure () | Stmt.requireError cond _ args => do - validateInternalCallShapesInExpr functions callerName cond - validateInternalCallShapesInExprList functions callerName args + validateInternalCallShapesInExpr functions callerName callerParams cond + validateInternalCallShapesInExprList functions callerName callerParams args | Stmt.revertError _ args => - validateInternalCallShapesInExprList functions callerName args + validateInternalCallShapesInExprList functions callerName callerParams args | Stmt.mstore offset value | Stmt.tstore offset value => do - validateInternalCallShapesInExpr functions callerName offset - validateInternalCallShapesInExpr functions callerName value + validateInternalCallShapesInExpr functions callerName callerParams offset + validateInternalCallShapesInExpr functions callerName callerParams value | Stmt.calldatacopy destOffset sourceOffset size | Stmt.returndataCopy destOffset sourceOffset size => do - validateInternalCallShapesInExpr functions callerName destOffset - validateInternalCallShapesInExpr functions callerName sourceOffset - validateInternalCallShapesInExpr functions callerName size + validateInternalCallShapesInExpr functions callerName callerParams destOffset + validateInternalCallShapesInExpr functions callerName callerParams sourceOffset + validateInternalCallShapesInExpr functions callerName callerParams size | Stmt.revertReturndata => pure () | Stmt.setMapping _ key value | Stmt.setMappingWord _ key _ value | Stmt.setMappingPackedWord _ key _ _ value | Stmt.setMappingUint _ key value | Stmt.setStructMember _ key _ value => do - validateInternalCallShapesInExpr functions callerName key - validateInternalCallShapesInExpr functions callerName value + validateInternalCallShapesInExpr functions callerName callerParams key + validateInternalCallShapesInExpr functions callerName callerParams value | Stmt.setMappingChain _ keys value => do - validateInternalCallShapesInExprList functions callerName keys - validateInternalCallShapesInExpr functions callerName value + validateInternalCallShapesInExprList functions callerName callerParams keys + validateInternalCallShapesInExpr functions callerName callerParams value | Stmt.setMapping2 _ key1 key2 value | Stmt.setMapping2Word _ key1 key2 _ value | Stmt.setStructMember2 _ key1 key2 _ value => do - validateInternalCallShapesInExpr functions callerName key1 - validateInternalCallShapesInExpr functions callerName key2 - validateInternalCallShapesInExpr functions callerName value + validateInternalCallShapesInExpr functions callerName callerParams key1 + validateInternalCallShapesInExpr functions callerName callerParams key2 + validateInternalCallShapesInExpr functions callerName callerParams value | Stmt.ite cond _ _ => - validateInternalCallShapesInExpr functions callerName cond + validateInternalCallShapesInExpr functions callerName callerParams cond | Stmt.forEach _ count _ => - validateInternalCallShapesInExpr functions callerName count + validateInternalCallShapesInExpr functions callerName callerParams count | Stmt.unsafeBlock _ _ => pure () | Stmt.matchAdt _ scrutinee _ => - validateInternalCallShapesInExpr functions callerName scrutinee + validateInternalCallShapesInExpr functions callerName callerParams scrutinee | Stmt.emit _ args => - validateInternalCallShapesInExprList functions callerName args + validateInternalCallShapesInExprList functions callerName callerParams args | Stmt.returnValues values => - validateInternalCallShapesInExprList functions callerName values + validateInternalCallShapesInExprList functions callerName callerParams values | Stmt.internalCall calleeName args => do - validateInternalCallShapesInExprList functions callerName args + validateInternalCallShapesInExprList functions callerName callerParams args let callee ← findInternalFunctionByName functions callerName calleeName - let expectedArgs := internalCallYulArgCount callee.params - if args.length != expectedArgs then - throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with {args.length} Yul arg(s), expected {expectedArgs} ({issue625Ref})." + validateInternalCallSourceArgs callerParams callerName calleeName callee.params args let returns ← functionReturns callee if !returns.isEmpty then throw s!"Compilation error: function '{callerName}' uses Stmt.internalCall '{calleeName}' but callee returns {returns.length} values; use Expr.internalCall for single-return or Stmt.internalCallAssign for multi-return calls ({issue625Ref})." @@ -258,45 +302,45 @@ def validateInternalCallShapesNodeStmt throw s!"Compilation error: function '{callerName}' uses Stmt.internalCallAssign with duplicate target '{dup}' ({issue625Ref})." | none => pure () - validateInternalCallShapesInExprList functions callerName args + validateInternalCallShapesInExprList functions callerName callerParams args let callee ← findInternalFunctionByName functions callerName calleeName - let expectedArgs := internalCallYulArgCount callee.params - if args.length != expectedArgs then - throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with {args.length} Yul arg(s), expected {expectedArgs} ({issue625Ref})." + validateInternalCallSourceArgs callerParams callerName calleeName callee.params args let returns ← functionReturns callee let expectedReturns := internalReturnYulCount returns if expectedReturns != names.length then throw s!"Compilation error: function '{callerName}' binds {names.length} Yul value(s) from internal function '{calleeName}', but callee returns {returns.length} logical value(s) / {expectedReturns} Yul value(s) ({issue625Ref})." | Stmt.rawLog topics dataOffset dataSize => do - validateInternalCallShapesInExprList functions callerName topics - validateInternalCallShapesInExpr functions callerName dataOffset - validateInternalCallShapesInExpr functions callerName dataSize + validateInternalCallShapesInExprList functions callerName callerParams topics + validateInternalCallShapesInExpr functions callerName callerParams dataOffset + validateInternalCallShapesInExpr functions callerName callerParams dataSize | Stmt.externalCallBind _resultVars _ args => - validateInternalCallShapesInExprList functions callerName args + validateInternalCallShapesInExprList functions callerName callerParams args | Stmt.tryExternalCallBind _ _resultVars _ args => - validateInternalCallShapesInExprList functions callerName args + validateInternalCallShapesInExprList functions callerName callerParams args | Stmt.ecm _ args => - validateInternalCallShapesInExprList functions callerName args + validateInternalCallShapesInExprList functions callerName callerParams args | _ => pure () def validateInternalCallShapesInStmt - (functions : List FunctionSpec) (callerName : String) (stmt : Stmt) : Except String Unit := - stmt.forDeepM (validateInternalCallShapesNodeStmt functions callerName) + (functions : List FunctionSpec) (callerName : String) (callerParams : List Param) + (stmt : Stmt) : Except String Unit := + stmt.forDeepM (validateInternalCallShapesNodeStmt functions callerName callerParams) def validateInternalCallShapesInStmtList - (functions : List FunctionSpec) (callerName : String) (stmts : List Stmt) : Except String Unit := - Stmt.forDeepListM (validateInternalCallShapesNodeStmt functions callerName) stmts + (functions : List FunctionSpec) (callerName : String) (callerParams : List Param) + (stmts : List Stmt) : Except String Unit := + Stmt.forDeepListM (validateInternalCallShapesNodeStmt functions callerName callerParams) stmts def validateInternalCallShapesInMatchBranches - (functions : List FunctionSpec) (callerName : String) + (functions : List FunctionSpec) (callerName : String) (callerParams : List Param) (branches : List (String × List String × List Stmt)) : Except String Unit := branches.forM fun (_, _, body) => - validateInternalCallShapesInStmtList functions callerName body + validateInternalCallShapesInStmtList functions callerName callerParams body def validateInternalCallShapesInFunction (functions : List FunctionSpec) (spec : FunctionSpec) : Except String Unit := do - spec.body.forM (validateInternalCallShapesInStmt functions spec.name) + spec.body.forM (validateInternalCallShapesInStmt functions spec.name spec.params) /-- Node-local check: shape of an `Expr.externalCall` node. Operands are reached via the canonical pre-order `Expr.forDeepM`, matching the old walk diff --git a/Compiler/CompilationModelFeatureTest.lean b/Compiler/CompilationModelFeatureTest.lean index ef0c0bffb..9472829db 100644 --- a/Compiler/CompilationModelFeatureTest.lean +++ b/Compiler/CompilationModelFeatureTest.lean @@ -2054,6 +2054,55 @@ def forwardedEchoedAmountPassesMemoryArray : Bool := example : forwardedEchoedAmountPassesMemoryArray = true := by native_decide +namespace InternalHelperDynamicArgs + +open Compiler.Yul + +def permitTy : ParamType := + ParamType.tuple [ParamType.address, ParamType.uint256] + +def transferWithBalanceCheck : FunctionSpec := { + name := "_transferWithBalanceCheck" + params := + [ { name := "permit", ty := permitTy } + , { name := "depositor", ty := ParamType.address } + , { name := "signature", ty := ParamType.bytes } + , { name := "amount", ty := ParamType.uint256 } + , { name := "noteCommitment", ty := ParamType.bytes32 } + ] + returnType := none + body := [] + isInternal := true +} + +def helperParamNamesExpandStaticCompositeAndBytes : Bool := + internalFunctionYulParamNames transferWithBalanceCheck.params == + [ "permit_0", "permit_1", "depositor", "signature_data_offset" + , "signature_length", "amount", "noteCommitment" ] + +def sourceInternalCallArgsExpandStaticCompositeAndBytes : Bool := + match compileInternalCallArgs [] .calldata [transferWithBalanceCheck] + "_transferWithBalanceCheck" + [ Expr.param "permit" + , Expr.param "depositor" + , Expr.param "signature" + , Expr.param "amount" + , Expr.param "noteCommitment" + ] with + | Except.ok + [ YulExpr.ident "permit_0" + , YulExpr.ident "permit_1" + , YulExpr.ident "depositor" + , YulExpr.ident "signature_data_offset" + , YulExpr.ident "signature_length" + , YulExpr.ident "amount" + , YulExpr.ident "noteCommitment" + ] => true + | Except.error _ => false + | _ => false + +end InternalHelperDynamicArgs + def compactAmountsAllocatesMemoryArray : Bool := let body := MacroDynamicArray.compactAmounts_modelBody body.any (fun stmt => @@ -5203,6 +5252,10 @@ set_option maxRecDepth 4096 in | .ok _ => true | .error _ => false expectTrue "local CompilationModel smoke spec compiles with deterministic selectors" compiled + expectTrue "internal helper params expand static composite and bytes slots" + MacroDynamicArraySmoke.InternalHelperDynamicArgs.helperParamNamesExpandStaticCompositeAndBytes + expectTrue "source internal helper call args expand static composite and bytes slots" + MacroDynamicArraySmoke.InternalHelperDynamicArgs.sourceInternalCallArgsExpandStaticCompositeAndBytes -- Regression: selector mismatch must fail closed. let mismatchRejected := diff --git a/Compiler/Proofs/IRGeneration/Contract.lean b/Compiler/Proofs/IRGeneration/Contract.lean index fa91d12cd..1a951e1c9 100644 --- a/Compiler/Proofs/IRGeneration/Contract.lean +++ b/Compiler/Proofs/IRGeneration/Contract.lean @@ -110,6 +110,34 @@ private theorem exists_right_of_forall₂_mem_left · rcases ih hmemTail with ⟨y, hy, hRy⟩ exact ⟨y, by simp [hy], hRy⟩ +private theorem filterInternalFunctions_eq_nil_of_all_nonInternal : + ∀ (fns : List FunctionSpec), + (∀ fn ∈ fns, fn.isInternal = false) → + fns.filter (·.isInternal) = [] + | [], _ => rfl + | fn :: rest, hall => by + have hfn : fn.isInternal = false := hall fn (by simp) + have hrest : ∀ fn' ∈ rest, fn'.isInternal = false := by + intro fn' hmem + exact hall fn' (by simp [hmem]) + simp [hfn, filterInternalFunctions_eq_nil_of_all_nonInternal rest hrest] + +private theorem filterInternalFunctions_eq_nil_of_supported + (model : CompilationModel) + (selectors : List Nat) + (hSupported : SupportedSpec model selectors) : + model.functions.filter (·.isInternal) = [] := by + exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions + (hSupported.noInternalFunctions) + +private theorem filterInternalFunctions_eq_nil_of_supported_except_mapping_writes + (model : CompilationModel) + (selectors : List Nat) + (hSupported : SupportedSpecExceptMappingWrites model selectors) : + model.functions.filter (·.isInternal) = [] := by + exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions + (hSupported.noInternalFunctions) + private theorem compileValidatedCore_ok_yields_compiled_functions (model : CompilationModel) (selectors : List Nat) @@ -129,12 +157,15 @@ private theorem compileValidatedCore_ok_yields_compiled_functions pickUniqueFunctionByName "receive" model.functions = Except.ok none := pickUniqueFunctionByName_eq_ok_none_of_absent "receive" model.functions hSupported.noReceive + have hnoInternalFns : + model.functions.filter (·.isInternal) = [] := + filterInternalFunctions_eq_nil_of_supported model selectors hSupported unfold compileValidatedCore at hcore rw [hSupported.normalizedFields, hSupported.noAdtTypes, hSupported.noEvents, hSupported.noErrors, - hfallback, hreceive] at hcore + hnoInternalFns, hfallback, hreceive] at hcore simp only [bind, Except.bind, pure, Except.pure] at hcore - rw [ContractShape.guardedFunctionsMapM_eq model.fields [] [] [] _ + rw [ContractShape.guardedFunctionsMapM_eq model.fields [] [] [] [] _ (ContractShape.supportedSpec_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -142,31 +173,27 @@ private theorem compileValidatedCore_ok_yields_compiled_functions (fun x => compileFunctionSpec model.fields [] [] [] x.2 x.1) with _ | irFns · simp [hmap] at hcore · simp [hmap] at hcore - rcases hinternal : - (model.functions.filter (·.isInternal)).mapM - (compileInternalFunction model.fields [] [] []) with _ | internalFuncDefs - · simp [hinternal] at hcore - · rcases hctor : - compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts - · simp [hinternal, hctor] at hcore - cases hcore - · simp [hinternal, hctor] at hcore - have hfunctions : ir.functions = irFns := by - injection hcore with hir - cases hir - rfl - have hcompiled : - List.Forall₂ - (fun (entry : FunctionSpec × Nat) irFn => - compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn) - ((model.functions.filter - (fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors) - irFns := - by - simpa [hSupported.noEvents, hSupported.noErrors] using - (compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap) - simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions, - hfunctions] using hcompiled + rcases hctor : + compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts + · simp [hctor] at hcore + cases hcore + · simp [hctor] at hcore + have hfunctions : ir.functions = irFns := by + injection hcore with hir + cases hir + rfl + have hcompiled : + List.Forall₂ + (fun (entry : FunctionSpec × Nat) irFn => + compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn) + ((model.functions.filter + (fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors) + irFns := + by + simpa [hSupported.noEvents, hSupported.noErrors] using + (compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap) + simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions, + hfunctions] using hcompiled private theorem compileValidatedCore_ok_yields_compiled_functions_except_mapping_writes (model : CompilationModel) @@ -187,12 +214,15 @@ private theorem compileValidatedCore_ok_yields_compiled_functions_except_mapping pickUniqueFunctionByName "receive" model.functions = Except.ok none := pickUniqueFunctionByName_eq_ok_none_of_absent "receive" model.functions hSupported.noReceive + have hnoInternalFns : + model.functions.filter (·.isInternal) = [] := + filterInternalFunctions_eq_nil_of_supported_except_mapping_writes model selectors hSupported unfold compileValidatedCore at hcore rw [hSupported.normalizedFields, hSupported.noAdtTypes, hSupported.noEvents, hSupported.noErrors, - hfallback, hreceive] at hcore + hnoInternalFns, hfallback, hreceive] at hcore simp only [bind, Except.bind, pure, Except.pure] at hcore - rw [ContractShape.guardedFunctionsMapM_eq model.fields [] [] [] _ + rw [ContractShape.guardedFunctionsMapM_eq model.fields [] [] [] [] _ (ContractShape.supportedSpecExceptMappingWrites_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -200,59 +230,27 @@ private theorem compileValidatedCore_ok_yields_compiled_functions_except_mapping (fun x => compileFunctionSpec model.fields [] [] [] x.2 x.1) with _ | irFns · simp [hmap] at hcore · simp [hmap] at hcore - rcases hinternal : - (model.functions.filter (·.isInternal)).mapM - (compileInternalFunction model.fields [] [] []) with _ | internalFuncDefs - · simp [hinternal] at hcore - · rcases hctor : - compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts - · simp [hinternal, hctor] at hcore - cases hcore - · simp [hinternal, hctor] at hcore - have hfunctions : ir.functions = irFns := by - injection hcore with hir - cases hir - rfl - have hcompiled : - List.Forall₂ - (fun (entry : FunctionSpec × Nat) irFn => - compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn) - ((model.functions.filter - (fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors) - irFns := - by - simpa [hSupported.noEvents, hSupported.noErrors] using - (compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap) - simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions, - hfunctions] using hcompiled - -private theorem filterInternalFunctions_eq_nil_of_all_nonInternal : - ∀ (fns : List FunctionSpec), - (∀ fn ∈ fns, fn.isInternal = false) → - fns.filter (·.isInternal) = [] - | [], _ => rfl - | fn :: rest, hall => by - have hfn : fn.isInternal = false := hall fn (by simp) - have hrest : ∀ fn' ∈ rest, fn'.isInternal = false := by - intro fn' hmem - exact hall fn' (by simp [hmem]) - simp [hfn, filterInternalFunctions_eq_nil_of_all_nonInternal rest hrest] - -private theorem filterInternalFunctions_eq_nil_of_supported - (model : CompilationModel) - (selectors : List Nat) - (hSupported : SupportedSpec model selectors) : - model.functions.filter (·.isInternal) = [] := by - exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions - (hSupported.noInternalFunctions) - -private theorem filterInternalFunctions_eq_nil_of_supported_except_mapping_writes - (model : CompilationModel) - (selectors : List Nat) - (hSupported : SupportedSpecExceptMappingWrites model selectors) : - model.functions.filter (·.isInternal) = [] := by - exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions - (hSupported.noInternalFunctions) + rcases hctor : + compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts + · simp [hctor] at hcore + cases hcore + · simp [hctor] at hcore + have hfunctions : ir.functions = irFns := by + injection hcore with hir + cases hir + rfl + have hcompiled : + List.Forall₂ + (fun (entry : FunctionSpec × Nat) irFn => + compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn) + ((model.functions.filter + (fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors) + irFns := + by + simpa [hSupported.noEvents, hSupported.noErrors] using + (compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap) + simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions, + hfunctions] using hcompiled private theorem compileValidatedCore_ok_yields_internalFunctions_nil (model : CompilationModel) @@ -288,7 +286,7 @@ private theorem compileValidatedCore_ok_yields_internalFunctions_nil hstorageArray, hdynamicBytesEq, hmulDiv512, hparamDyn, hnoInternalFns, hSupported.noAdtTypes] at hcore simp only [bind, Except.bind, pure, Except.pure, List.mapM_nil] at hcore - rw [ContractShape.guardedFunctionsMapM_eq model.fields model.events model.errors [] _ + rw [ContractShape.guardedFunctionsMapM_eq model.fields model.events model.errors [] [] _ (ContractShape.supportedSpec_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -318,11 +316,14 @@ private theorem compileValidatedCore_ok_yields_noFallbackEntrypoint pickUniqueFunctionByName "receive" model.functions = Except.ok none := pickUniqueFunctionByName_eq_ok_none_of_absent "receive" model.functions hSupported.noReceive + have hnoInternalFns : + model.functions.filter (·.isInternal) = [] := + filterInternalFunctions_eq_nil_of_supported model selectors hSupported unfold compileValidatedCore at hcore - rw [hfallback, hreceive] at hcore + rw [hnoInternalFns, hfallback, hreceive] at hcore simp only [bind, Except.bind, Option.mapM_none, pure, Except.pure] at hcore rw [ContractShape.guardedFunctionsMapM_eq (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes _ + model.events model.errors model.adtTypes [] _ (ContractShape.supportedSpec_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -330,18 +331,13 @@ private theorem compileValidatedCore_ok_yields_noFallbackEntrypoint (fun x => compileFunctionSpec (applySlotAliasRanges model.fields model.slotAliasRanges) model.events model.errors model.adtTypes x.2 x.1) with _ | irFns · simp [hmap] at hcore - · rcases hinternal : - (model.functions.filter (·.isInternal)).mapM - (compileInternalFunction (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes) with _ | internalFuncDefs - · simp [hmap, hinternal] at hcore - · rcases hctor : - compileConstructor (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes model.constructor with _ | deployStmts - · simp [hmap, hinternal, hctor] at hcore - · simp [hmap, hinternal, hctor] at hcore - cases hcore - rfl + · rcases hctor : + compileConstructor (applySlotAliasRanges model.fields model.slotAliasRanges) + model.events model.errors model.adtTypes model.constructor with _ | deployStmts + · simp [hmap, hctor, Pure.pure, Except.pure] at hcore + · simp [hmap, hctor, Pure.pure, Except.pure] at hcore + cases hcore + rfl private theorem compileValidatedCore_ok_yields_noReceiveEntrypoint (model : CompilationModel) @@ -358,11 +354,14 @@ private theorem compileValidatedCore_ok_yields_noReceiveEntrypoint pickUniqueFunctionByName "receive" model.functions = Except.ok none := pickUniqueFunctionByName_eq_ok_none_of_absent "receive" model.functions hSupported.noReceive + have hnoInternalFns : + model.functions.filter (·.isInternal) = [] := + filterInternalFunctions_eq_nil_of_supported model selectors hSupported unfold compileValidatedCore at hcore - rw [hfallback, hreceive] at hcore + rw [hnoInternalFns, hfallback, hreceive] at hcore simp only [bind, Except.bind, Option.mapM_none, pure, Except.pure] at hcore rw [ContractShape.guardedFunctionsMapM_eq (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes _ + model.events model.errors model.adtTypes [] _ (ContractShape.supportedSpec_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -370,18 +369,13 @@ private theorem compileValidatedCore_ok_yields_noReceiveEntrypoint (fun x => compileFunctionSpec (applySlotAliasRanges model.fields model.slotAliasRanges) model.events model.errors model.adtTypes x.2 x.1) with _ | irFns · simp [hmap] at hcore - · rcases hinternal : - (model.functions.filter (·.isInternal)).mapM - (compileInternalFunction (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes) with _ | internalFuncDefs - · simp [hmap, hinternal] at hcore - · rcases hctor : - compileConstructor (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes model.constructor with _ | deployStmts - · simp [hmap, hinternal, hctor] at hcore - · simp [hmap, hinternal, hctor] at hcore - cases hcore - rfl + · rcases hctor : + compileConstructor (applySlotAliasRanges model.fields model.slotAliasRanges) + model.events model.errors model.adtTypes model.constructor with _ | deployStmts + · simp [hmap, hctor, Pure.pure, Except.pure] at hcore + · simp [hmap, hctor, Pure.pure, Except.pure] at hcore + cases hcore + rfl theorem supported_params_of_supportedSpec (model : CompilationModel) @@ -671,7 +665,7 @@ theorem compile_ok_yields_internalFunctions_nil_except_mapping_writes hstorageArray, hdynamicBytesEq, hmulDiv512, hparamDyn, hnoInternalFns, hSupported.noAdtTypes] at hcompile simp only [bind, Except.bind, pure, Except.pure, List.mapM_nil] at hcompile - rw [ContractShape.guardedFunctionsMapM_eq model.fields model.events model.errors [] _ + rw [ContractShape.guardedFunctionsMapM_eq model.fields model.events model.errors [] [] _ (ContractShape.supportedSpecExceptMappingWrites_entries_lock_free hSupported)] at hcompile rcases hmap : ((model.functions.filter @@ -721,7 +715,7 @@ theorem compile_ok_yields_noFallbackEntrypoint_except_mapping_writes contractUsesPlainArrayElement, contractUsesArrayElementWord, harray, hstorageArray, hdynamicBytesEq, hnoInternalFns, hSupported.noAdtTypes] at hcompile simp only [bind, Except.bind, pure, Except.pure, List.mapM_nil] at hcompile - rw [ContractShape.guardedFunctionsMapM_eq model.fields model.events model.errors [] _ + rw [ContractShape.guardedFunctionsMapM_eq model.fields model.events model.errors [] [] _ (ContractShape.supportedSpecExceptMappingWrites_entries_lock_free hSupported)] at hcompile rcases hmap : ((model.functions.filter @@ -771,7 +765,7 @@ theorem compile_ok_yields_noReceiveEntrypoint_except_mapping_writes contractUsesPlainArrayElement, contractUsesArrayElementWord, harray, hstorageArray, hdynamicBytesEq, hnoInternalFns, hSupported.noAdtTypes] at hcompile simp only [bind, Except.bind, pure, Except.pure, List.mapM_nil] at hcompile - rw [ContractShape.guardedFunctionsMapM_eq model.fields model.events model.errors [] _ + rw [ContractShape.guardedFunctionsMapM_eq model.fields model.events model.errors [] [] _ (ContractShape.supportedSpecExceptMappingWrites_entries_lock_free hSupported)] at hcompile rcases hmap : ((model.functions.filter diff --git a/Compiler/Proofs/IRGeneration/ContractFeatureTest.lean b/Compiler/Proofs/IRGeneration/ContractFeatureTest.lean index baeb95651..8ce2f62c5 100644 --- a/Compiler/Proofs/IRGeneration/ContractFeatureTest.lean +++ b/Compiler/Proofs/IRGeneration/ContractFeatureTest.lean @@ -153,6 +153,11 @@ private theorem constructorOnly_owner_resolved : some ({ name := "owner", ty := FieldType.address }, 0) := by rfl +private theorem constructorOnly_owner_resolved_lit : + findFieldWithResolvedSlot [{ name := "owner", ty := FieldType.address }] "owner" = + some ({ name := "owner", ty := FieldType.address }, 0) := by + rfl + private def constructorOnlySupported : SupportedConstructor constructorOnlySpec constructorOnlyCtor := { params := @@ -499,6 +504,38 @@ private theorem constructorOnly_noConflict : firstFieldWriteSlotConflict constructorOnlySpec.fields = none := by native_decide +private theorem constructorOnly_compileBody : + ∃ bodyStmts, + compileStmtList + constructorOnlySpec.fields + constructorOnlySpec.events + constructorOnlySpec.errors + .memory + [] + false + (constructorOnlyCtor.params.map (·.name)) + [] + constructorOnlyCtor.body [] = + Except.ok bodyStmts := by + refine ⟨ + match compileStmtList + constructorOnlySpec.fields + constructorOnlySpec.events + constructorOnlySpec.errors + .memory + [] + false + (constructorOnlyCtor.params.map (·.name)) + [] + constructorOnlyCtor.body [] with + | .ok body => body + | .error _ => [], ?_⟩ + simp [constructorOnlySpec, constructorOnlyCtor, constructorOnlyOwnerField, + CompilationModel.compileStmtList, CompilationModel.compileStmt, + CompilationModel.compileSetStorage, CompilationModel.compileExpr, + CompilationModel.isMapping, constructorOnly_owner_resolved_lit, + Bind.bind, Except.bind, Pure.pure, Except.pure] + private theorem constructorOnly_compileConstructor : ∃ bodyStmts, compileConstructor @@ -517,27 +554,19 @@ private theorem constructorOnly_compileConstructor : false (constructorOnlyCtor.params.map (·.name)) [] - constructorOnlyCtor.body = + constructorOnlyCtor.body [] = Except.ok bodyStmts := by + rcases constructorOnly_compileBody with ⟨bodyStmts, hbodyCompile⟩ rcases Function.compileConstructor_ok_components constructorOnlySpec.fields constructorOnlySpec.events constructorOnlySpec.errors constructorOnlyCtor - (genConstructorArgLoads constructorOnlyCtor.params ++ - match compileStmtList - constructorOnlySpec.fields - constructorOnlySpec.events - constructorOnlySpec.errors - .memory - [] - false - (constructorOnlyCtor.params.map (·.name)) - [] - constructorOnlyCtor.body with - | .ok body => body - | .error _ => []) - (by rfl) with ⟨bodyStmts, hbodyCompile, hdeploy⟩ + (genConstructorArgLoads constructorOnlyCtor.params ++ bodyStmts) + (by + simp [CompilationModel.compileConstructor, hbodyCompile, Bind.bind, + Except.bind, Pure.pure, Except.pure]) with + ⟨_, _, hdeploy⟩ refine ⟨bodyStmts, ?_, hbodyCompile⟩ exact Function.compileConstructor_some_ok_of_body constructorOnlySpec.fields @@ -813,7 +842,7 @@ example : constructorOnlySpec.fields [] [] .memory [] false (constructorOnlyCtor.params.map (·.name)) [] - [Stmt.setStorageAddr "owner" (.param "initialOwner"), .stop] with + [Stmt.setStorageAddr "owner" (.param "initialOwner"), .stop] [] with | .ok body => body | .error _ => []) + 1) (ParamLoading.applyBindingsToIRState @@ -823,18 +852,22 @@ example : constructorOnlySpec.fields [] [] .memory [] false (constructorOnlyCtor.params.map (·.name)) [] - [Stmt.setStorageAddr "owner" (.param "initialOwner"), .stop] with + [Stmt.setStorageAddr "owner" (.param "initialOwner"), .stop] [] with | .ok body => body | .error _ => []))) := by have hbodyCompile : compileStmtList constructorOnlySpec.fields constructorOnlySpec.events constructorOnlySpec.errors - .memory [] false (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body = + .memory [] false (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body [] = Except.ok (match compileStmtList constructorOnlySpec.fields [] [] .memory [] false - (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body with + (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body [] with | .ok body => body | .error _ => []) := by - rfl + simp [constructorOnlySpec, constructorOnlyCtor, constructorOnlyOwnerField, + CompilationModel.compileStmtList, CompilationModel.compileStmt, + CompilationModel.compileSetStorage, CompilationModel.compileExpr, + CompilationModel.isMapping, constructorOnly_owner_resolved_lit, + Bind.bind, Except.bind, Pure.pure, Except.pure] have hbind : SourceSemantics.bindSupportedParams [{ name := "initialOwner", ty := .address }] @@ -862,7 +895,7 @@ example : (initialWorld := Verity.defaultState) (bindings := [("initialOwner", Compiler.Constants.addressMask &&& 11)]) (bodyStmts := match compileStmtList constructorOnlySpec.fields [] [] .memory [] false - (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body with + (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body [] with | .ok body => body | .error _ => []) (hbodyCompile := hbodyCompile) @@ -890,7 +923,7 @@ example : bodyStmts)) := by let bodyStmts := match compileStmtList constructorOnlySpec.fields [] [] .memory [] false - (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body with + (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body [] with | .ok body => body | .error _ => [] let bindings := [("initialOwner", Compiler.Constants.addressMask &&& 11)] @@ -898,9 +931,13 @@ example : · native_decide · have hbodyCompile : compileStmtList constructorOnlySpec.fields constructorOnlySpec.events constructorOnlySpec.errors - .memory [] false (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body = + .memory [] false (constructorOnlyCtor.params.map (·.name)) [] constructorOnlyCtor.body [] = Except.ok bodyStmts := by - rfl + simp [bodyStmts, constructorOnlySpec, constructorOnlyCtor, constructorOnlyOwnerField, + CompilationModel.compileStmtList, CompilationModel.compileStmt, + CompilationModel.compileSetStorage, CompilationModel.compileExpr, + CompilationModel.isMapping, constructorOnly_owner_resolved_lit, + Bind.bind, Except.bind, Pure.pure, Except.pure] have hbind : SourceSemantics.bindSupportedParams constructorOnlyCtor.params (constructorOnlyTrailingTx.args.take constructorOnlyCtor.params.length) = diff --git a/Compiler/Proofs/IRGeneration/ContractShape.lean b/Compiler/Proofs/IRGeneration/ContractShape.lean index 6aa95e4ed..da46bd5b3 100644 --- a/Compiler/Proofs/IRGeneration/ContractShape.lean +++ b/Compiler/Proofs/IRGeneration/ContractShape.lean @@ -70,33 +70,34 @@ theorem attachNonReentrantGuard_eq_of_none theorem compileGuardedFunctionSpec_eq_of_none (fields : List Field) (events : List EventDef) (errors : List ErrorDef) - (adtTypes : List AdtTypeDef) (sel : Nat) (fnSpec : FunctionSpec) + (adtTypes : List AdtTypeDef) (internalFunctions : List FunctionSpec) + (sel : Nat) (fnSpec : FunctionSpec) (hnone : fnSpec.nonReentrantLock = none) : - compileGuardedFunctionSpec fields events errors adtTypes sel fnSpec = - compileFunctionSpec fields events errors adtTypes sel fnSpec := by + compileGuardedFunctionSpec fields events errors adtTypes internalFunctions sel fnSpec = + compileFunctionSpec fields events errors adtTypes sel fnSpec internalFunctions := by unfold compileGuardedFunctionSpec - cases hcomp : compileFunctionSpec fields events errors adtTypes sel fnSpec with - | error err => simp [hcomp, bind, Except.bind] + cases hcomp : compileFunctionSpec fields events errors adtTypes sel fnSpec internalFunctions with + | error err => simp [bind, Except.bind] | ok irFn => - simp [hcomp, bind, Except.bind, + simp [bind, Except.bind, attachNonReentrantGuard_eq_of_none fields fnSpec irFn hnone] theorem guardedFunctionsMapM_eq (fields : List Field) (events : List EventDef) (errors : List ErrorDef) - (adtTypes : List AdtTypeDef) : + (adtTypes : List AdtTypeDef) (internalFunctions : List FunctionSpec) : ∀ (entries : List (FunctionSpec × Nat)), (∀ e ∈ entries, e.1.nonReentrantLock = none) → (entries.mapM fun entry => - compileGuardedFunctionSpec fields events errors adtTypes entry.2 entry.1) = + compileGuardedFunctionSpec fields events errors adtTypes internalFunctions entry.2 entry.1) = entries.mapM fun entry => - compileFunctionSpec fields events errors adtTypes entry.2 entry.1 + compileFunctionSpec fields events errors adtTypes entry.2 entry.1 internalFunctions | [], _ => rfl | e :: rest, hnolock => by have hhead : e.1.nonReentrantLock = none := hnolock e (by simp) - have htail := guardedFunctionsMapM_eq fields events errors adtTypes rest + have htail := guardedFunctionsMapM_eq fields events errors adtTypes internalFunctions rest (fun e' he' => hnolock e' (List.mem_cons_of_mem _ he')) simp only [List.mapM_cons, - compileGuardedFunctionSpec_eq_of_none fields events errors adtTypes e.2 e.1 hhead, + compileGuardedFunctionSpec_eq_of_none fields events errors adtTypes internalFunctions e.2 e.1 hhead, htail] theorem supportedSpecExceptMappingWrites_entries_lock_free @@ -129,6 +130,26 @@ theorem supportedSpecWithScalarEvents_entries_lock_free have hmem := (List.of_mem_zip he).1 exact (hSupported.functions e.1 (List.mem_filter.mp hmem).1).noNonReentrant +private theorem filterInternalFunctions_eq_nil_of_all_nonInternal : + ∀ (fns : List FunctionSpec), + (∀ fn ∈ fns, fn.isInternal = false) → + fns.filter (·.isInternal) = [] + | [], _ => rfl + | fn :: rest, hall => by + have hfn : fn.isInternal = false := hall fn (by simp) + have hrest : ∀ fn' ∈ rest, fn'.isInternal = false := by + intro fn' hmem + exact hall fn' (by simp [hmem]) + simp [hfn, filterInternalFunctions_eq_nil_of_all_nonInternal rest hrest] + +private theorem filterInternalFunctions_eq_nil_of_supported + (model : CompilationModel) + (selectors : List Nat) + (hSupported : SupportedSpec model selectors) : + model.functions.filter (·.isInternal) = [] := by + exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions + (hSupported.noInternalFunctions) + private theorem compileValidatedCore_ok_yields_compiled_functions (model : CompilationModel) (selectors : List Nat) @@ -148,12 +169,15 @@ private theorem compileValidatedCore_ok_yields_compiled_functions pickUniqueFunctionByName "receive" model.functions = Except.ok none := pickUniqueFunctionByName_eq_ok_none_of_absent "receive" model.functions hSupported.noReceive + have hnoInternalFns : + model.functions.filter (·.isInternal) = [] := + filterInternalFunctions_eq_nil_of_supported model selectors hSupported unfold compileValidatedCore at hcore rw [hSupported.normalizedFields, hSupported.noAdtTypes, hSupported.noEvents, hSupported.noErrors, - hfallback, hreceive] at hcore + hnoInternalFns, hfallback, hreceive] at hcore simp only [bind, Except.bind, pure, Except.pure] at hcore - simp only [guardedFunctionsMapM_eq model.fields [] [] [] _ + simp only [guardedFunctionsMapM_eq model.fields [] [] [] [] _ (supportedSpec_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -161,51 +185,27 @@ private theorem compileValidatedCore_ok_yields_compiled_functions (fun x => compileFunctionSpec model.fields [] [] [] x.2 x.1) with _ | irFns · simp [hmap] at hcore · simp [hmap] at hcore - rcases hinternal : - (model.functions.filter (·.isInternal)).mapM - (compileInternalFunction model.fields [] [] []) with _ | internalFuncDefs - · simp [hinternal] at hcore - · rcases hctor : - compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts - · simp [hinternal, hctor] at hcore - cases hcore - · simp [hinternal, hctor] at hcore - have hfunctions : ir.functions = irFns := by - injection hcore with hir - cases hir - rfl - have hcompiled : - List.Forall₂ - (fun (entry : FunctionSpec × Nat) irFn => - compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn) - ((model.functions.filter - (fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors) - irFns := - by - simpa [hSupported.noEvents, hSupported.noErrors] using - (compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap) - simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions, - hfunctions] using hcompiled - -private theorem filterInternalFunctions_eq_nil_of_all_nonInternal : - ∀ (fns : List FunctionSpec), - (∀ fn ∈ fns, fn.isInternal = false) → - fns.filter (·.isInternal) = [] - | [], _ => rfl - | fn :: rest, hall => by - have hfn : fn.isInternal = false := hall fn (by simp) - have hrest : ∀ fn' ∈ rest, fn'.isInternal = false := by - intro fn' hmem - exact hall fn' (by simp [hmem]) - simp [hfn, filterInternalFunctions_eq_nil_of_all_nonInternal rest hrest] - -private theorem filterInternalFunctions_eq_nil_of_supported - (model : CompilationModel) - (selectors : List Nat) - (hSupported : SupportedSpec model selectors) : - model.functions.filter (·.isInternal) = [] := by - exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions - (hSupported.noInternalFunctions) + rcases hctor : + compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts + · simp [hctor] at hcore + cases hcore + · simp [hctor] at hcore + have hfunctions : ir.functions = irFns := by + injection hcore with hir + cases hir + rfl + have hcompiled : + List.Forall₂ + (fun (entry : FunctionSpec × Nat) irFn => + compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn) + ((model.functions.filter + (fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors) + irFns := + by + simpa [hSupported.noEvents, hSupported.noErrors] using + (compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap) + simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions, + hfunctions] using hcompiled private theorem compileValidatedCore_ok_yields_internalFunctions_nil (model : CompilationModel) @@ -241,7 +241,7 @@ private theorem compileValidatedCore_ok_yields_internalFunctions_nil hstorageArray, hdynamicBytesEq, hmulDiv512, hparamDyn, hnoInternalFns, hSupported.noAdtTypes] at hcore simp only [bind, Except.bind, pure, Except.pure, List.mapM_nil] at hcore - simp only [guardedFunctionsMapM_eq model.fields model.events model.errors [] _ + simp only [guardedFunctionsMapM_eq model.fields model.events model.errors [] [] _ (supportedSpec_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -250,9 +250,8 @@ private theorem compileValidatedCore_ok_yields_internalFunctions_nil · simp [hmap] at hcore · rcases hctor : compileConstructor model.fields model.events model.errors [] model.constructor with _ | deployStmts - · simp [hmap, hctor] at hcore - cases hcore - · simp [hmap, hctor] at hcore + · simp [hmap, hctor, pure, Except.pure] at hcore + · simp [hmap, hctor, pure, Except.pure] at hcore cases hcore rfl @@ -272,12 +271,15 @@ private theorem compileValidatedCore_ok_yields_deploy_compileConstructor pickUniqueFunctionByName "receive" model.functions = Except.ok none := pickUniqueFunctionByName_eq_ok_none_of_absent "receive" model.functions hSupported.noReceive + have hnoInternalFns : + model.functions.filter (·.isInternal) = [] := + filterInternalFunctions_eq_nil_of_supported model selectors hSupported unfold compileValidatedCore at hcore rw [hSupported.normalizedFields, hSupported.noAdtTypes, hSupported.noEvents, hSupported.noErrors, - hfallback, hreceive] at hcore + hnoInternalFns, hfallback, hreceive] at hcore simp only [bind, Except.bind, pure, Except.pure] at hcore - simp only [guardedFunctionsMapM_eq model.fields [] [] [] _ + simp only [guardedFunctionsMapM_eq model.fields [] [] [] [] _ (supportedSpec_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -285,17 +287,13 @@ private theorem compileValidatedCore_ok_yields_deploy_compileConstructor (fun x => compileFunctionSpec model.fields [] [] [] x.2 x.1) with _ | irFns · simp [hmap] at hcore · simp [hmap] at hcore - rcases hinternal : - (model.functions.filter (·.isInternal)).mapM - (compileInternalFunction model.fields [] [] []) with _ | internalFuncDefs - · simp [hinternal] at hcore - · rcases hctor : - compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts - · simp [hinternal, hctor] at hcore - cases hcore - · simp [hinternal, hctor] at hcore - cases hcore - simpa [hSupported.noEvents, hSupported.noErrors] using hctor + rcases hctor : + compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts + · simp [hctor] at hcore + cases hcore + · simp [hctor] at hcore + cases hcore + simpa [hSupported.noEvents, hSupported.noErrors] using hctor private theorem compileValidatedCore_ok_yields_noFallbackEntrypoint (model : CompilationModel) @@ -312,11 +310,14 @@ private theorem compileValidatedCore_ok_yields_noFallbackEntrypoint pickUniqueFunctionByName "receive" model.functions = Except.ok none := pickUniqueFunctionByName_eq_ok_none_of_absent "receive" model.functions hSupported.noReceive + have hnoInternalFns : + model.functions.filter (·.isInternal) = [] := + filterInternalFunctions_eq_nil_of_supported model selectors hSupported unfold compileValidatedCore at hcore - rw [hfallback, hreceive] at hcore + rw [hnoInternalFns, hfallback, hreceive] at hcore simp only [bind, Except.bind, Option.mapM_none, pure, Except.pure] at hcore simp only [guardedFunctionsMapM_eq (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes _ + model.events model.errors model.adtTypes [] _ (supportedSpec_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -324,18 +325,13 @@ private theorem compileValidatedCore_ok_yields_noFallbackEntrypoint (fun x => compileFunctionSpec (applySlotAliasRanges model.fields model.slotAliasRanges) model.events model.errors model.adtTypes x.2 x.1) with _ | irFns · simp [hmap] at hcore - · rcases hinternal : - (model.functions.filter (·.isInternal)).mapM - (compileInternalFunction (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes) with _ | internalFuncDefs - · simp [hmap, hinternal] at hcore - · rcases hctor : - compileConstructor (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes model.constructor with _ | deployStmts - · simp [hmap, hinternal, hctor] at hcore - · simp [hmap, hinternal, hctor] at hcore - cases hcore - rfl + · rcases hctor : + compileConstructor (applySlotAliasRanges model.fields model.slotAliasRanges) + model.events model.errors model.adtTypes model.constructor with _ | deployStmts + · simp [hmap, hctor, Pure.pure, Except.pure] at hcore + · simp [hmap, hctor, Pure.pure, Except.pure] at hcore + cases hcore + rfl private theorem compileValidatedCore_ok_yields_noReceiveEntrypoint (model : CompilationModel) @@ -352,11 +348,14 @@ private theorem compileValidatedCore_ok_yields_noReceiveEntrypoint pickUniqueFunctionByName "receive" model.functions = Except.ok none := pickUniqueFunctionByName_eq_ok_none_of_absent "receive" model.functions hSupported.noReceive + have hnoInternalFns : + model.functions.filter (·.isInternal) = [] := + filterInternalFunctions_eq_nil_of_supported model selectors hSupported unfold compileValidatedCore at hcore - rw [hfallback, hreceive] at hcore + rw [hnoInternalFns, hfallback, hreceive] at hcore simp only [bind, Except.bind, Option.mapM_none, pure, Except.pure] at hcore simp only [guardedFunctionsMapM_eq (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes _ + model.events model.errors model.adtTypes [] _ (supportedSpec_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter @@ -364,18 +363,13 @@ private theorem compileValidatedCore_ok_yields_noReceiveEntrypoint (fun x => compileFunctionSpec (applySlotAliasRanges model.fields model.slotAliasRanges) model.events model.errors model.adtTypes x.2 x.1) with _ | irFns · simp [hmap] at hcore - · rcases hinternal : - (model.functions.filter (·.isInternal)).mapM - (compileInternalFunction (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes) with _ | internalFuncDefs - · simp [hmap, hinternal] at hcore - · rcases hctor : - compileConstructor (applySlotAliasRanges model.fields model.slotAliasRanges) - model.events model.errors model.adtTypes model.constructor with _ | deployStmts - · simp [hmap, hinternal, hctor] at hcore - · simp [hmap, hinternal, hctor] at hcore - cases hcore - rfl + · rcases hctor : + compileConstructor (applySlotAliasRanges model.fields model.slotAliasRanges) + model.events model.errors model.adtTypes model.constructor with _ | deployStmts + · simp [hmap, hctor, Pure.pure, Except.pure] at hcore + · simp [hmap, hctor, Pure.pure, Except.pure] at hcore + cases hcore + rfl theorem compile_ok_yields_compiled_functions (model : CompilationModel) @@ -415,34 +409,34 @@ private theorem compileValidatedCore_ok_yields_compiled_functions_with_scalar_ev "fallback" model.functions hSupported.surface.noFallback have hreceive := pickUniqueFunctionByName_eq_ok_none_of_absent "receive" model.functions hSupported.surface.noReceive + have hnoInternalFns : + model.functions.filter (·.isInternal) = [] := + filterInternalFunctions_eq_nil_of_all_nonInternal model.functions + hSupported.noInternalFunctions unfold compileValidatedCore at hcore rw [hSupported.normalizedFields, hSupported.noAdtTypes, hSupported.noErrors, - hfallback, hreceive] at hcore + hnoInternalFns, hfallback, hreceive] at hcore simp only [bind, Except.bind, pure, Except.pure] at hcore - simp only [guardedFunctionsMapM_eq model.fields model.events [] [] _ + simp only [guardedFunctionsMapM_eq model.fields model.events [] [] [] _ (supportedSpecWithScalarEvents_entries_lock_free hSupported)] at hcore rcases hmap : ((model.functions.filter (fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors).mapM (fun x => compileFunctionSpec model.fields model.events [] [] x.2 x.1) with _ | irFns · simp [hmap] at hcore · simp [hmap] at hcore - rcases hinternal : - (model.functions.filter (·.isInternal)).mapM - (compileInternalFunction model.fields model.events [] []) with _ | internalFuncDefs - · simp [hinternal] at hcore - · rcases hctor : - compileConstructor model.fields model.events [] [] model.constructor with _ | deployStmts - · simp [hinternal, hctor] at hcore - cases hcore - · simp [hinternal, hctor] at hcore - have hfunctions : ir.functions = irFns := by - injection hcore with hir - cases hir - rfl - have hcompiled := compiled_functions_forall₂_of_mapM_ok - model.fields model.events [] _ _ hmap - simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions, - hfunctions, hSupported.noErrors] using hcompiled + rcases hctor : + compileConstructor model.fields model.events [] [] model.constructor with _ | deployStmts + · simp [hctor] at hcore + cases hcore + · simp [hctor] at hcore + have hfunctions : ir.functions = irFns := by + injection hcore with hir + cases hir + rfl + have hcompiled := compiled_functions_forall₂_of_mapM_ok + model.fields model.events [] _ _ hmap + simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions, + hfunctions, hSupported.noErrors] using hcompiled theorem compile_ok_yields_compiled_functions_with_scalar_events (model : CompilationModel) diff --git a/Compiler/Proofs/IRGeneration/Function.lean b/Compiler/Proofs/IRGeneration/Function.lean index 939948312..0818ebc49 100644 --- a/Compiler/Proofs/IRGeneration/Function.lean +++ b/Compiler/Proofs/IRGeneration/Function.lean @@ -300,7 +300,7 @@ theorem compileConstructor_some_ok_of_body (ctor : ConstructorSpec) (bodyStmts : List YulStmt) (hbody : compileStmtList fields events errors .memory [] false - (ctor.params.map (·.name)) [] ctor.body = Except.ok bodyStmts) : + (ctor.params.map (·.name)) [] ctor.body [] = Except.ok bodyStmts) : compileConstructor fields events errors [] (some ctor) = Except.ok (genConstructorArgLoads ctor.params ++ bodyStmts) := by simp [CompilationModel.compileConstructor, hbody] @@ -312,11 +312,11 @@ theorem compileConstructor_ok_components compileConstructor fields events errors [] (some ctor) = Except.ok deployStmts) : ∃ bodyStmts, compileStmtList fields events errors .memory [] false - (ctor.params.map (·.name)) [] ctor.body = Except.ok bodyStmts ∧ + (ctor.params.map (·.name)) [] ctor.body [] = Except.ok bodyStmts ∧ deployStmts = genConstructorArgLoads ctor.params ++ bodyStmts := by cases hbody : compileStmtList fields events errors .memory [] false - (ctor.params.map (·.name)) [] ctor.body with + (ctor.params.map (·.name)) [] ctor.body [] with | error err => simp [CompilationModel.compileConstructor, hbody] at hcompile | ok bodyStmts => diff --git a/Compiler/Proofs/IRGeneration/FunctionBody/Stmt.lean b/Compiler/Proofs/IRGeneration/FunctionBody/Stmt.lean index 2f2c1dc96..23d4357c6 100644 --- a/Compiler/Proofs/IRGeneration/FunctionBody/Stmt.lean +++ b/Compiler/Proofs/IRGeneration/FunctionBody/Stmt.lean @@ -930,7 +930,7 @@ private theorem compileStmt_ok_any_scope_aux · -- compileStmtList part intro stmts scope1 scope2 hlt hok cases stmts with - | nil => exact ⟨[], rfl⟩ + | nil => exact ⟨[], by simp [CompilationModel.compileStmtList, Pure.pure, Except.pure]⟩ | cons s ss => rcases hok with ⟨ir, hir⟩ simp only [CompilationModel.compileStmtList, bind, Except.bind] at hir ⊢ @@ -1049,7 +1049,7 @@ private theorem compileStmt_ok_any_scope_with_surface_aux simp only [CompilationModel.compileStmt] at hok ⊢; exact hok · intro stmts scope1 scope2 hlt hok cases stmts with - | nil => exact ⟨[], rfl⟩ + | nil => exact ⟨[], by simp [CompilationModel.compileStmtList, Pure.pure, Except.pure]⟩ | cons s ss => rcases hok with ⟨ir, hir⟩ simp only [CompilationModel.compileStmtList, bind, Except.bind] at hir ⊢ @@ -1309,7 +1309,7 @@ theorem compileStmtList_core_ok fields [] [] .calldata [] false inScopeNames [] stmts = Except.ok bodyIR := by induction hcore generalizing inScopeNames case nil => - exact ⟨[], rfl⟩ + exact ⟨[], by simp [CompilationModel.compileStmtList, Pure.pure, Except.pure]⟩ case letVar scope name value rest hvalue _ hrest ih => rcases compileStmt_core_ok_any_scope (fields := fields) (inScopeNames := inScopeNames) (stmt := .letVar name value) (.letVar hvalue) with ⟨headIR, hheadIR⟩ @@ -2765,7 +2765,7 @@ theorem exec_compileStmtList_core stmtResultMatchesIRExecExact sourceResult irExec := by induction hcore generalizing runtime state inScopeNames with | nil => - refine ⟨[], rfl, ?_⟩ + refine ⟨[], by simp [CompilationModel.compileStmtList, Pure.pure, Except.pure], ?_⟩ constructor · simpa [SourceSemantics.execStmtList, execIRStmts, stmtResultMatchesIRExec] using hruntime · simpa [SourceSemantics.execStmtList, execIRStmts, stmtResultMatchesIRExecExact] using @@ -3182,7 +3182,7 @@ theorem exec_compileStmtList_core_extraFuel stmtResultMatchesIRExecExact sourceResult irExec := by induction hcore generalizing runtime state inScopeNames with | nil => - refine ⟨[], rfl, ?_⟩ + refine ⟨[], by simp [CompilationModel.compileStmtList, Pure.pure, Except.pure], ?_⟩ constructor · simpa [SourceSemantics.execStmtList, execIRStmts, stmtResultMatchesIRExec] using hruntime · simpa [SourceSemantics.execStmtList, execIRStmts, stmtResultMatchesIRExecExact] using diff --git a/Compiler/Proofs/IRGeneration/GenericInduction/Calls.lean b/Compiler/Proofs/IRGeneration/GenericInduction/Calls.lean index 284b01609..302803dc0 100644 --- a/Compiler/Proofs/IRGeneration/GenericInduction/Calls.lean +++ b/Compiler/Proofs/IRGeneration/GenericInduction/Calls.lean @@ -55,7 +55,8 @@ theorem compiledStmtStepWithHelpersAndHelperIR_internalCallAssign (Stmt.internalCallAssign names calleeName args) compiledIR := by refine { - compileOk := hcompile + compileOk := by + simpa [CompilationModel.compileStmt] using hcompile preserves := ?_ } intro runtime state helperFuel extraFuel hfuelPos hexact hscope hbounded hruntime hslack obtain ⟨argExprs', hargOk, hshape⟩ := compileStmt_internalCallAssign_shape hcompile @@ -115,7 +116,8 @@ theorem compiledStmtStepWithHelpersAndHelperIR_internalCall (Stmt.internalCall calleeName args) compiledIR := by refine { - compileOk := hcompile + compileOk := by + simpa [CompilationModel.compileStmt] using hcompile preserves := ?_ } intro runtime state helperFuel extraFuel hfuelPos hexact hscope hbounded hruntime hslack obtain ⟨argExprs', hargOk, hshape⟩ := compileStmt_internalCall_shape hcompile diff --git a/Compiler/Proofs/IRGeneration/GenericInduction/Helpers.lean b/Compiler/Proofs/IRGeneration/GenericInduction/Helpers.lean index d3abd55e4..a382ef188 100644 --- a/Compiler/Proofs/IRGeneration/GenericInduction/Helpers.lean +++ b/Compiler/Proofs/IRGeneration/GenericInduction/Helpers.lean @@ -2312,7 +2312,8 @@ theorem compileStmtList_ok_of_stmtListGenericCore CompilationModel.compileStmtList fields [] [] .calldata [] false inScopeNames [] stmts = Except.ok bodyIR := by induction hgeneric generalizing inScopeNames with - | nil => exact ⟨[], rfl⟩ + | nil => + exact ⟨[], by simp [CompilationModel.compileStmtList, Pure.pure, Except.pure]⟩ | cons hstep _hrest ih => rcases FunctionBody.compileStmt_ok_any_scope (scope2 := inScopeNames) ⟨_, hstep.compileOk⟩ with ⟨headIR, hhead⟩ @@ -2337,7 +2338,8 @@ theorem compileStmtList_ok_of_stmtListGenericWithHelpers CompilationModel.compileStmtList fields spec.events spec.errors .calldata [] false inScopeNames [] stmts = Except.ok bodyIR := by induction hgeneric generalizing inScopeNames with - | nil => exact ⟨[], rfl⟩ + | nil => + exact ⟨[], by simp [CompilationModel.compileStmtList, Pure.pure, Except.pure]⟩ | cons hstep _hrest ih => rcases FunctionBody.compileStmt_ok_any_scope_with_surface (scope2 := inScopeNames) ⟨_, hstep.compileOk⟩ with ⟨headIR, hhead⟩ @@ -2364,7 +2366,8 @@ theorem compileStmtList_ok_of_stmtListGenericWithHelpersAndHelperIR CompilationModel.compileStmtList fields spec.events spec.errors .calldata [] false inScopeNames [] stmts = Except.ok bodyIR := by induction hgeneric generalizing inScopeNames with - | nil => exact ⟨[], rfl⟩ + | nil => + exact ⟨[], by simp [CompilationModel.compileStmtList, Pure.pure, Except.pure]⟩ | cons hstep _hrest ih => rcases FunctionBody.compileStmt_ok_any_scope_with_surface (scope2 := inScopeNames) ⟨_, hstep.compileOk⟩ with ⟨headIR, hhead⟩ diff --git a/Compiler/Proofs/IRGeneration/GenericInduction/ResultRelation.lean b/Compiler/Proofs/IRGeneration/GenericInduction/ResultRelation.lean index 431fbbf30..f9e934db8 100644 --- a/Compiler/Proofs/IRGeneration/GenericInduction/ResultRelation.lean +++ b/Compiler/Proofs/IRGeneration/GenericInduction/ResultRelation.lean @@ -272,7 +272,8 @@ theorem compileStmtList_ok_of_stmtListGenericCore_early CompilationModel.compileStmtList fields [] [] .calldata [] false inScopeNames [] stmts = Except.ok bodyIR := by induction hgeneric generalizing inScopeNames with - | nil => exact ⟨[], rfl⟩ + | nil => + exact ⟨[], by simp [CompilationModel.compileStmtList, Pure.pure, Except.pure]⟩ | cons hstep _hrest ih => rcases FunctionBody.compileStmt_ok_any_scope (scope2 := inScopeNames) ⟨_, hstep.compileOk⟩ with ⟨headIR, hhead⟩ diff --git a/Compiler/Proofs/IRGeneration/IRInterpreter.lean b/Compiler/Proofs/IRGeneration/IRInterpreter.lean index ef52dc3b4..620c0c1a1 100644 --- a/Compiler/Proofs/IRGeneration/IRInterpreter.lean +++ b/Compiler/Proofs/IRGeneration/IRInterpreter.lean @@ -5575,10 +5575,13 @@ theorem compileStmt_internalCallAssign_shape (YulExpr.call (CompilationModel.internalFunctionYulName functionName) argExprs)] := by simp only [CompilationModel.compileStmt, bind, Except.bind] at hok match hargs : CompilationModel.compileExprList fields .calldata args with - | .error e => simp [hargs] at hok + | .error e => + simp [CompilationModel.compileInternalCallArgs, + CompilationModel.findInternalFunctionForCall?, hargs] at hok | .ok argExprs => refine ⟨argExprs, rfl, ?_⟩ - simp [hargs, pure, Except.pure] at hok + simp [CompilationModel.compileInternalCallArgs, + CompilationModel.findInternalFunctionForCall?, hargs, pure, Except.pure] at hok exact hok.symm /-- Compilation of `Stmt.internalCall` produces exactly @@ -5596,10 +5599,13 @@ theorem compileStmt_internalCall_shape (YulExpr.call (CompilationModel.internalFunctionYulName functionName) argExprs)] := by simp only [CompilationModel.compileStmt, bind, Except.bind] at hok match hargs : CompilationModel.compileExprList fields .calldata args with - | .error e => simp [hargs] at hok + | .error e => + simp [CompilationModel.compileInternalCallArgs, + CompilationModel.findInternalFunctionForCall?, hargs] at hok | .ok argExprs => refine ⟨argExprs, rfl, ?_⟩ - simp [hargs, pure, Except.pure] at hok + simp [CompilationModel.compileInternalCallArgs, + CompilationModel.findInternalFunctionForCall?, hargs, pure, Except.pure] at hok exact hok.symm private theorem internalFunctionYulName_head (name : String) : diff --git a/Compiler/Proofs/IRGeneration/SupportedSpec.lean b/Compiler/Proofs/IRGeneration/SupportedSpec.lean index e82dbd399..fd0dc8671 100644 --- a/Compiler/Proofs/IRGeneration/SupportedSpec.lean +++ b/Compiler/Proofs/IRGeneration/SupportedSpec.lean @@ -1892,13 +1892,13 @@ private theorem compileStmt_eventsErrorsAgnostic_aux (∀ (stmt : Stmt) (scope : List String), sizeOf stmt < n → stmtTouchesUnsupportedContractSurface stmt = false → - CompilationModel.compileStmt fields events errors .calldata [] false scope [] stmt = - CompilationModel.compileStmt fields [] [] .calldata [] false scope [] stmt) ∧ + CompilationModel.compileStmt fields events errors .calldata [] false scope [] stmt [] = + CompilationModel.compileStmt fields [] [] .calldata [] false scope [] stmt []) ∧ (∀ (stmts : List Stmt) (scope : List String), sizeOf stmts < n → stmtListTouchesUnsupportedContractSurface stmts = false → - CompilationModel.compileStmtList fields events errors .calldata [] false scope [] stmts = - CompilationModel.compileStmtList fields [] [] .calldata [] false scope [] stmts) := by + CompilationModel.compileStmtList fields events errors .calldata [] false scope [] stmts [] = + CompilationModel.compileStmtList fields [] [] .calldata [] false scope [] stmts []) := by induction n with | zero => exact ⟨fun _ _ hlt => absurd hlt (Nat.not_lt_zero _), @@ -1935,7 +1935,7 @@ private theorem compileStmt_eventsErrorsAgnostic_aux simp [stmtTouchesUnsupportedContractSurface] at hsurface · intro stmts scope hlt hsurface cases stmts with - | nil => rfl + | nil => simp only [CompilationModel.compileStmtList] | cons s ss => simp only [stmtListTouchesUnsupportedContractSurface, Bool.or_eq_false_iff] at hsurface @@ -1954,8 +1954,8 @@ theorem compileStmt_eventsErrorsAgnostic_of_contractSurfaceClosed {scope : List String} {stmt : Stmt} (hsurface : stmtTouchesUnsupportedContractSurface stmt = false) : - CompilationModel.compileStmt fields events errors .calldata [] false scope [] stmt = - CompilationModel.compileStmt fields [] [] .calldata [] false scope [] stmt := + CompilationModel.compileStmt fields events errors .calldata [] false scope [] stmt [] = + CompilationModel.compileStmt fields [] [] .calldata [] false scope [] stmt [] := (compileStmt_eventsErrorsAgnostic_aux (sizeOf stmt + 1) fields events errors).1 stmt scope (Nat.lt_succ_of_le (Nat.le_refl _)) hsurface @@ -1968,8 +1968,8 @@ theorem compileStmtList_eventsErrorsAgnostic_of_contractSurfaceClosed {scope : List String} {stmts : List Stmt} (hsurface : stmtListTouchesUnsupportedContractSurface stmts = false) : - CompilationModel.compileStmtList fields events errors .calldata [] false scope [] stmts = - CompilationModel.compileStmtList fields [] [] .calldata [] false scope [] stmts := + CompilationModel.compileStmtList fields events errors .calldata [] false scope [] stmts [] = + CompilationModel.compileStmtList fields [] [] .calldata [] false scope [] stmts [] := (compileStmt_eventsErrorsAgnostic_aux (sizeOf stmts + 1) fields events errors).2 stmts scope (Nat.lt_succ_of_le (Nat.le_refl _)) hsurface diff --git a/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanCallClosure.lean b/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanCallClosure.lean index 6da0f2272..aa075a828 100644 --- a/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanCallClosure.lean +++ b/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanCallClosure.lean @@ -248,6 +248,71 @@ inductive BridgedSourceInternalCallStmt BridgedSourceInternalCallStmt table (.internalCallAssign names funcName args) +private theorem compileStmt_internalCall_call_bridged + {table : BridgedFunctionTable} + (fields : List Field) (events : List EventDef) (errors : List ErrorDef) + (dynamicSource : DynamicDataSource) (internalRetNames : List String) + (isInternal : Bool) (inScopeNames : List String) (adtTypes : List AdtTypeDef) + (funcName : String) (args : List Expr) + (hArgs : ∀ a ∈ args, BridgedSourceExpr a) + (hFn : (BridgedFunctionTable.lookup table + (internalFunctionYulName funcName)).isSome) + {out : List YulStmt} + (hOk : compileStmt fields events errors dynamicSource internalRetNames + isInternal inScopeNames adtTypes (.internalCall funcName args) = .ok out) : + BridgedStmts out := by + simp only [compileStmt, bind, Except.bind] at hOk + cases hExprs : compileExprList fields dynamicSource args with + | error _ => + simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs] at hOk + | ok argExprs => + simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs, + Pure.pure, Except.pure] at hOk + subst out + have hArgsBridged : ∀ y ∈ argExprs, BridgedExpr y := + compileExprList_bridgedSource fields dynamicSource hArgs hExprs + intro yulStmt hMem + simp only [List.mem_singleton] at hMem + subst yulStmt + exact BridgedStmt.of_userFunctionCallExpr + (BridgedUserFunctionCallExpr.mk (internalFunctionYulName funcName) + argExprs + (BridgedUserFunctionCall.call (internalFunctionYulName funcName) + argExprs hArgsBridged hFn)) + +private theorem compileStmt_internalCallAssign_bridged + {table : BridgedFunctionTable} + (fields : List Field) (events : List EventDef) (errors : List ErrorDef) + (dynamicSource : DynamicDataSource) (internalRetNames : List String) + (isInternal : Bool) (inScopeNames : List String) (adtTypes : List AdtTypeDef) + (names : List String) (funcName : String) (args : List Expr) + (hArgs : ∀ a ∈ args, BridgedSourceExpr a) + (hFn : (BridgedFunctionTable.lookup table + (internalFunctionYulName funcName)).isSome) + {out : List YulStmt} + (hOk : compileStmt fields events errors dynamicSource internalRetNames + isInternal inScopeNames adtTypes (.internalCallAssign names funcName args) = + .ok out) : + BridgedStmts out := by + simp only [compileStmt, bind, Except.bind] at hOk + cases hExprs : compileExprList fields dynamicSource args with + | error _ => + simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs] at hOk + | ok argExprs => + simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs, + Pure.pure, Except.pure] at hOk + subst out + have hArgsBridged : ∀ y ∈ argExprs, BridgedExpr y := + compileExprList_bridgedSource fields dynamicSource hArgs hExprs + intro yulStmt hMem + simp only [List.mem_singleton] at hMem + subst yulStmt + exact BridgedStmt.of_userFunctionCallBind + (BridgedUserFunctionCallBind.mk names (internalFunctionYulName funcName) + argExprs + (BridgedUserFunctionCall.call (internalFunctionYulName funcName) + argExprs hArgsBridged hFn)) + /-- Phase 2.1: compiling a source `Stmt.internalCall` with bridged arguments and a callee that resolves in `table` yields a `BridgedStmts` output. -/ theorem compileStmt_internalCall_bridged @@ -262,39 +327,12 @@ theorem compileStmt_internalCall_bridged BridgedStmts out := by cases hStmt with | call funcName args hArgs hFn => - simp only [compileStmt, bind, Except.bind] at hOk - cases hExprs : compileExprList fields dynamicSource args with - | error _ => simp [hExprs] at hOk - | ok argExprs => - simp [hExprs, Pure.pure, Except.pure] at hOk - subst out - have hArgsBridged : ∀ y ∈ argExprs, BridgedExpr y := - compileExprList_bridgedSource fields dynamicSource hArgs hExprs - intro yulStmt hMem - simp only [List.mem_singleton] at hMem - subst yulStmt - exact BridgedStmt.of_userFunctionCallExpr - (BridgedUserFunctionCallExpr.mk (internalFunctionYulName funcName) - argExprs - (BridgedUserFunctionCall.call (internalFunctionYulName funcName) - argExprs hArgsBridged hFn)) + exact compileStmt_internalCall_call_bridged fields events errors dynamicSource + internalRetNames isInternal inScopeNames adtTypes funcName args hArgs hFn hOk | callAssign names funcName args hArgs hFn => - simp only [compileStmt, bind, Except.bind] at hOk - cases hExprs : compileExprList fields dynamicSource args with - | error _ => simp [hExprs] at hOk - | ok argExprs => - simp [hExprs, Pure.pure, Except.pure] at hOk - subst out - have hArgsBridged : ∀ y ∈ argExprs, BridgedExpr y := - compileExprList_bridgedSource fields dynamicSource hArgs hExprs - intro yulStmt hMem - simp only [List.mem_singleton] at hMem - subst yulStmt - exact BridgedStmt.of_userFunctionCallBind - (BridgedUserFunctionCallBind.mk names (internalFunctionYulName funcName) - argExprs - (BridgedUserFunctionCall.call (internalFunctionYulName funcName) - argExprs hArgsBridged hFn)) + exact compileStmt_internalCallAssign_bridged fields events errors dynamicSource + internalRetNames isInternal inScopeNames adtTypes names funcName args + hArgs hFn hOk /-- A list of source statements, each in `BridgedSourceInternalCallStmt`. -/ def BridgedSourceInternalCallStmts (table : BridgedFunctionTable) @@ -357,7 +395,8 @@ theorem compileStmt_externalCallBind_bridged | mk resultVars externalName args hArgs hFn => simp only [compileStmt, bind, Except.bind] at hOk cases hExprs : compileExprList fields dynamicSource args with - | error _ => simp [hExprs] at hOk + | error _ => + simp [hExprs] at hOk | ok argExprs => simp [hExprs] at hOk have hArgsBridged : ∀ y ∈ argExprs, BridgedExpr y := @@ -456,17 +495,21 @@ theorem compileStmt_internalCall_noFuncDefs | call funcName args hArgs hFn => simp only [compileStmt, bind, Except.bind] at hOk cases hExprs : compileExprList fields dynamicSource args with - | error _ => simp [hExprs] at hOk + | error _ => + simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs] at hOk | ok argExprs => - simp [hExprs, Pure.pure, Except.pure] at hOk + simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs, + Pure.pure, Except.pure] at hOk subst out simp [Native.yulStmtContainsFuncDef] | callAssign names funcName args hArgs hFn => simp only [compileStmt, bind, Except.bind] at hOk cases hExprs : compileExprList fields dynamicSource args with - | error _ => simp [hExprs] at hOk + | error _ => + simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs] at hOk | ok argExprs => - simp [hExprs, Pure.pure, Except.pure] at hOk + simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs, + Pure.pure, Except.pure] at hOk subst out simp [Native.yulStmtContainsFuncDef] diff --git a/PrintAxioms.lean b/PrintAxioms.lean index d9ed9e5db..eae40a01c 100644 --- a/PrintAxioms.lean +++ b/PrintAxioms.lean @@ -1634,11 +1634,11 @@ end Verity.AxiomAudit -- Compiler.Proofs.IRGeneration.Contract.compiled_functions_forall₂_of_mapM_ok -- private -- Compiler.Proofs.IRGeneration.Contract.compiled_internal_functions_forall₂_of_mapM_ok -- private -- Compiler.Proofs.IRGeneration.Contract.exists_right_of_forall₂_mem_left -- private - -- Compiler.Proofs.IRGeneration.Contract.compileValidatedCore_ok_yields_compiled_functions -- private - -- Compiler.Proofs.IRGeneration.Contract.compileValidatedCore_ok_yields_compiled_functions_except_mapping_writes -- private -- Compiler.Proofs.IRGeneration.Contract.filterInternalFunctions_eq_nil_of_all_nonInternal -- private -- Compiler.Proofs.IRGeneration.Contract.filterInternalFunctions_eq_nil_of_supported -- private -- Compiler.Proofs.IRGeneration.Contract.filterInternalFunctions_eq_nil_of_supported_except_mapping_writes -- private + -- Compiler.Proofs.IRGeneration.Contract.compileValidatedCore_ok_yields_compiled_functions -- private + -- Compiler.Proofs.IRGeneration.Contract.compileValidatedCore_ok_yields_compiled_functions_except_mapping_writes -- private -- Compiler.Proofs.IRGeneration.Contract.compileValidatedCore_ok_yields_internalFunctions_nil -- private -- Compiler.Proofs.IRGeneration.Contract.compileValidatedCore_ok_yields_noFallbackEntrypoint -- private -- Compiler.Proofs.IRGeneration.Contract.compileValidatedCore_ok_yields_noReceiveEntrypoint -- private @@ -1680,6 +1680,7 @@ end Verity.AxiomAudit -- Compiler.Proofs.IRGeneration.ContractFeatureTest.literalMappingWrite_noReceive -- private -- Compiler.Proofs.IRGeneration.ContractFeatureTest.literalMappingWrite_noConflict -- private -- Compiler.Proofs.IRGeneration.ContractFeatureTest.constructorOnly_owner_resolved -- private + -- Compiler.Proofs.IRGeneration.ContractFeatureTest.constructorOnly_owner_resolved_lit -- private -- Compiler.Proofs.IRGeneration.ContractFeatureTest.literalMappingWrite_txNormalized -- private -- Compiler.Proofs.IRGeneration.ContractFeatureTest.literalMappingWrite_calldataFits -- private -- Compiler.Proofs.IRGeneration.ContractFeatureTest.constructorOnly_txNormalized -- private @@ -1690,6 +1691,7 @@ end Verity.AxiomAudit -- Compiler.Proofs.IRGeneration.ContractFeatureTest.stopOnly_txNormalized -- private -- Compiler.Proofs.IRGeneration.ContractFeatureTest.stopOnly_calldataFits -- private -- Compiler.Proofs.IRGeneration.ContractFeatureTest.constructorOnly_noConflict -- private + -- Compiler.Proofs.IRGeneration.ContractFeatureTest.constructorOnly_compileBody -- private -- Compiler.Proofs.IRGeneration.ContractFeatureTest.constructorOnly_compileConstructor -- private -- Compiler/Proofs/IRGeneration/ContractShape.lean @@ -1701,9 +1703,9 @@ end Verity.AxiomAudit Compiler.Proofs.IRGeneration.ContractShape.supportedSpecExceptMappingWrites_entries_lock_free Compiler.Proofs.IRGeneration.ContractShape.supportedSpec_entries_lock_free Compiler.Proofs.IRGeneration.ContractShape.supportedSpecWithScalarEvents_entries_lock_free - -- Compiler.Proofs.IRGeneration.ContractShape.compileValidatedCore_ok_yields_compiled_functions -- private -- Compiler.Proofs.IRGeneration.ContractShape.filterInternalFunctions_eq_nil_of_all_nonInternal -- private -- Compiler.Proofs.IRGeneration.ContractShape.filterInternalFunctions_eq_nil_of_supported -- private + -- Compiler.Proofs.IRGeneration.ContractShape.compileValidatedCore_ok_yields_compiled_functions -- private -- Compiler.Proofs.IRGeneration.ContractShape.compileValidatedCore_ok_yields_internalFunctions_nil -- private -- Compiler.Proofs.IRGeneration.ContractShape.compileValidatedCore_ok_yields_deploy_compileConstructor -- private -- Compiler.Proofs.IRGeneration.ContractShape.compileValidatedCore_ok_yields_noFallbackEntrypoint -- private @@ -4013,6 +4015,8 @@ end Verity.AxiomAudit Compiler.Proofs.YulGeneration.Backends.BridgedStmts_of_userFunctionCallStmts -- Compiler.Proofs.YulGeneration.Backends.compileStmtList_cons_ok_inv_generic -- private -- Compiler.Proofs.YulGeneration.Backends.compileStmtList_bridged_of_perStmtBridge -- private + -- Compiler.Proofs.YulGeneration.Backends.compileStmt_internalCall_call_bridged -- private + -- Compiler.Proofs.YulGeneration.Backends.compileStmt_internalCallAssign_bridged -- private Compiler.Proofs.YulGeneration.Backends.compileStmt_internalCall_bridged Compiler.Proofs.YulGeneration.Backends.compileStmtList_internalCall_bridged Compiler.Proofs.YulGeneration.Backends.compileStmt_externalCallBind_bridged @@ -5542,4 +5546,4 @@ end Verity.AxiomAudit Compiler.Proofs.YulGeneration.YulTransaction.ofIR_args ] --- Total: 5187 theorems/lemmas (3588 public, 1599 private, 0 sorry'd) +-- Total: 5191 theorems/lemmas (3588 public, 1603 private, 0 sorry'd) From f90bc95dfeb11139ab12af7188d9475d8af48d6e Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Sat, 13 Jun 2026 22:36:26 +0200 Subject: [PATCH 2/3] fix(verity): exact type+layout validation for forwarded internal helper args (#2016) --- Compiler/CompilationModel/Compile.lean | 144 ++-- Compiler/CompilationModel/EventEmission.lean | 20 +- .../CompilationModel/ExpressionCompile.lean | 394 +++++++--- Compiler/CompilationModel/StorageWrites.lean | 62 +- .../CompilationModel/ValidationCalls.lean | 119 ++- Compiler/CompilationModelFeatureTest.lean | 64 ++ .../IRGeneration/ContractFeatureTest.lean | 6 +- Compiler/Proofs/IRGeneration/Function.lean | 62 +- .../IRGeneration/FunctionBody/Base.lean | 727 ++++++++++-------- .../IRGeneration/FunctionBody/Stmt.lean | 235 ++++-- .../GenericInduction/EventBridge.lean | 49 +- .../GenericInduction/ExprStmt.lean | 16 +- .../IRGeneration/GenericInduction/Loops.lean | 5 +- .../IRGeneration/GenericInduction/Scope.lean | 11 +- .../GenericInduction/Storage.lean | 156 ++-- .../Proofs/IRGeneration/IRInterpreter.lean | 8 +- .../Proofs/IRGeneration/IntrinsicProofs.lean | 55 +- .../Backends/EvmYulLeanBodyClosure/Base.lean | 382 ++++----- .../Backends/EvmYulLeanCallClosure.lean | 14 +- .../Backends/EvmYulLeanSourceExprClosure.lean | 184 ++--- PrintAxioms.lean | 10 +- 21 files changed, 1675 insertions(+), 1048 deletions(-) diff --git a/Compiler/CompilationModel/Compile.lean b/Compiler/CompilationModel/Compile.lean index 2077e2406..989c2e012 100644 --- a/Compiler/CompilationModel/Compile.lean +++ b/Compiler/CompilationModel/Compile.lean @@ -53,52 +53,6 @@ def unsafeYulToEVMYul (fragment : UnsafeYulFragment) : List YulStmt := theorem unsafeYulToEVMYul_eq (fragment : UnsafeYulFragment) : unsafeYulToEVMYul fragment = fragment.stmts := rfl -def findInternalFunctionForCall? (functions : List FunctionSpec) (name : String) : Option FunctionSpec := - match functions.filter (fun fn => fn.isInternal && fn.name == name) with - | [fn] => some fn - | _ => none - -def directForwardedInternalCallArgName? : Expr → Option String - | Expr.param name => some name - | Expr.localVar name => some name - | _ => none - -def compileInternalCallArg (fields : List Field) (dynamicSource : DynamicDataSource) - (calleeName : String) (param : Param) (arg : Expr) : Except String (List YulExpr) := do - if isExpandedInternalParamType param.ty then - match directForwardedInternalCallArgName? arg with - | some name => - pure ((internalCallYulArgNamesForParam name param).map YulExpr.ident) - | none => - throw s!"Compilation error: internal call '{calleeName}' argument for parameter '{param.name}' with type {repr param.ty} must be a direct parameter/local forwarding expression (issue #1889)." - else - pure [← compileExpr fields dynamicSource arg] - -def compileInternalCallArgsWithParams (fields : List Field) (dynamicSource : DynamicDataSource) - (calleeName : String) : List Param → List Expr → Except String (List YulExpr) - | [], [] => pure [] - | param :: params, arg :: args => do - let head ← compileInternalCallArg fields dynamicSource calleeName param arg - let tail ← compileInternalCallArgsWithParams fields dynamicSource calleeName params args - pure (head ++ tail) - | params, args => - throw s!"Compilation error: internal call '{calleeName}' received {args.length} source arg(s), expected {params.length} (issue #1889)." - -def compileInternalCallArgs (fields : List Field) (dynamicSource : DynamicDataSource) - (internalFunctions : List FunctionSpec) (calleeName : String) (args : List Expr) : - Except String (List YulExpr) := - match findInternalFunctionForCall? internalFunctions calleeName with - | some callee => - let legacyArgCount := - callee.params.foldl (fun acc param => acc + (internalFunctionYulParamNames [param]).length) 0 - if args.length == callee.params.length then - compileInternalCallArgsWithParams fields dynamicSource calleeName callee.params args - else if args.length == legacyArgCount then - compileExprList fields dynamicSource args - else - compileInternalCallArgsWithParams fields dynamicSource calleeName callee.params args - | none => compileExprList fields dynamicSource args - private def compileAdtStorageWrite (fields : List Field) (dynamicSource : DynamicDataSource) (adtTypes : List AdtTypeDef) (storageField adtName variantName : String) (args : List Expr) : @@ -187,24 +141,24 @@ def compileStmt (fields : List Field) (events : List EventDef := []) Except String (List YulStmt) := match stmt with | Stmt.letVar name value => do - pure [YulStmt.let_ name (← compileExpr fields dynamicSource value)] + pure [YulStmt.let_ name (← compileExprWithInternals fields dynamicSource internalFunctions value)] | Stmt.assignVar name value => do - pure [YulStmt.assign name (← compileExpr fields dynamicSource value)] + pure [YulStmt.assign name (← compileExprWithInternals fields dynamicSource internalFunctions value)] | Stmt.setStorage field value => match adtTypes with - | [] => compileSetStorage fields dynamicSource field value + | [] => compileSetStorage fields dynamicSource field value false internalFunctions | _ => match value with | Expr.adtConstruct adtName variantName args => compileAdtStorageWrite fields dynamicSource adtTypes field adtName variantName args | _ => - compileSetStorage fields dynamicSource field value + compileSetStorage fields dynamicSource field value false internalFunctions | Stmt.setStorageAddr field value => - compileSetStorage fields dynamicSource field value true + compileSetStorage fields dynamicSource field value true internalFunctions | Stmt.setStorageWord field wordOffset value => match findFieldWithResolvedSlot fields field with | some (f, slot) => do - let valueExpr ← compileExpr fields dynamicSource value + let valueExpr ← compileExprWithInternals fields dynamicSource internalFunctions value let slotExpr (baseSlot : Nat) := if wordOffset == 0 then YulExpr.lit baseSlot else YulExpr.call "add" [YulExpr.lit baseSlot, YulExpr.lit wordOffset] @@ -225,57 +179,57 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | none => throw s!"Compilation error: unknown storage field '{field}' in setStorageWord" | Stmt.storageArrayPush field value => - compileStorageArrayPush fields dynamicSource field value + compileStorageArrayPush fields dynamicSource field value internalFunctions | Stmt.storageArrayPop field => compileStorageArrayPop fields field | Stmt.setStorageArrayElement field index value => - compileSetStorageArrayElement fields dynamicSource field index value + compileSetStorageArrayElement fields dynamicSource field index value internalFunctions | Stmt.setMapping field key value => do compileMappingSlotWrite fields field - (← compileExpr fields dynamicSource key) - (← compileExpr fields dynamicSource value) + (← compileExprWithInternals fields dynamicSource internalFunctions key) + (← compileExprWithInternals fields dynamicSource internalFunctions value) "setMapping" | Stmt.setMappingWord field key wordOffset value => do compileMappingSlotWrite fields field - (← compileExpr fields dynamicSource key) - (← compileExpr fields dynamicSource value) + (← compileExprWithInternals fields dynamicSource internalFunctions key) + (← compileExprWithInternals fields dynamicSource internalFunctions value) "setMappingWord" wordOffset | Stmt.setMappingPackedWord field key wordOffset packed value => do compileMappingPackedSlotWrite fields field - (← compileExpr fields dynamicSource key) - (← compileExpr fields dynamicSource value) + (← compileExprWithInternals fields dynamicSource internalFunctions key) + (← compileExprWithInternals fields dynamicSource internalFunctions value) wordOffset packed "setMappingPackedWord" | Stmt.setMapping2 field key1 key2 value => - compileSetMapping2 fields dynamicSource field key1 key2 value + compileSetMapping2 fields dynamicSource field key1 key2 value internalFunctions | Stmt.setMapping2Word field key1 key2 wordOffset value => - compileSetMapping2Word fields dynamicSource field key1 key2 wordOffset value + compileSetMapping2Word fields dynamicSource field key1 key2 wordOffset value internalFunctions | Stmt.setMappingUint field key value => do compileMappingSlotWrite fields field - (← compileExpr fields dynamicSource key) - (← compileExpr fields dynamicSource value) + (← compileExprWithInternals fields dynamicSource internalFunctions key) + (← compileExprWithInternals fields dynamicSource internalFunctions value) "setMappingUint" | Stmt.setMappingChain field keys value => - compileSetMappingChain fields dynamicSource field keys value + compileSetMappingChain fields dynamicSource field keys value internalFunctions | Stmt.setStructMember field key memberName value => - compileSetStructMember fields dynamicSource field key memberName value + compileSetStructMember fields dynamicSource field key memberName value internalFunctions | Stmt.setStructMember2 field key1 key2 memberName value => - compileSetStructMember2 fields dynamicSource field key1 key2 memberName value + compileSetStructMember2 fields dynamicSource field key1 key2 memberName value internalFunctions | Stmt.require cond message => do - let failCond ← compileRequireFailCond fields dynamicSource cond + let failCond ← compileRequireFailCondWithInternals fields dynamicSource internalFunctions cond pure [ YulStmt.if_ failCond (revertWithMessage message) ] | Stmt.requireError cond errorName args => do - let failCond ← compileRequireFailCond fields dynamicSource cond + let failCond ← compileRequireFailCondWithInternals fields dynamicSource internalFunctions cond let errorDef ← match errors.find? (·.name == errorName) with | some defn => pure defn | none => throw s!"Compilation error: unknown custom error '{errorName}' ({issue586Ref})" - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileExprListWithInternals fields dynamicSource internalFunctions args let revertStmts ← revertWithCustomError dynamicSource errorDef args argExprs pure [YulStmt.if_ failCond revertStmts] | Stmt.revertError errorName args => do @@ -283,11 +237,11 @@ def compileStmt (fields : List Field) (events : List EventDef := []) match errors.find? (·.name == errorName) with | some defn => pure defn | none => throw s!"Compilation error: unknown custom error '{errorName}' ({issue586Ref})" - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileExprListWithInternals fields dynamicSource internalFunctions args revertWithCustomError dynamicSource errorDef args argExprs | Stmt.return value => do - let valueExpr ← compileExpr fields dynamicSource value + let valueExpr ← compileExprWithInternals fields dynamicSource internalFunctions value if isInternal then match internalRetNames with | retName :: _ => pure [YulStmt.assign retName valueExpr, YulStmt.leave] @@ -301,7 +255,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | Stmt.ite cond thenBranch elseBranch => do -- If/else: compile to Yul if + negated if (#179) - let condExpr ← compileExpr fields dynamicSource cond + let condExpr ← compileExprWithInternals fields dynamicSource internalFunctions cond let thenStmts ← compileStmtList fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes thenBranch internalFunctions let elseStmts ← compileStmtList fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes elseBranch internalFunctions if elseBranch.isEmpty then @@ -326,7 +280,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) -- the current counter at the top of each iteration. This matches the source -- semantics where `count` is evaluated once and `varName` holds the last -- iteration state after the loop rather than the post-incremented counter. - let countExpr ← compileExpr fields dynamicSource count + let countExpr ← compileExprWithInternals fields dynamicSource internalFunctions count let forUsedNames := varName :: (inScopeNames ++ collectExprNames count ++ collectStmtListNames body) let idxName := pickFreshName "__forEach_idx" forUsedNames let countName := pickFreshName "__forEach_count" (idxName :: forUsedNames) @@ -351,7 +305,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) pure (unsafeYulToEVMYul fragment) | Stmt.emit eventName args => do - compileEmit fields events dynamicSource eventName args + compileEmit fields events dynamicSource eventName args internalFunctions | Stmt.internalCall functionName args => do -- Internal function call as statement (#181) @@ -361,7 +315,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) let argExprs ← compileInternalCallArgs fields dynamicSource internalFunctions functionName args pure [YulStmt.letMany names (YulExpr.call (internalFunctionYulName functionName) argExprs)] | Stmt.externalCallBind resultVars externalName args => do - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileExprListWithInternals fields dynamicSource internalFunctions args if resultVars.isEmpty then pure [YulStmt.expr (YulExpr.call externalName argExprs)] else @@ -369,7 +323,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) -- Try-call variant: calls {externalName}_try which returns (success, result...) -- instead of reverting on failure. (#1727, Axis 1 Step 5f) | Stmt.tryExternalCallBind successVar resultVars externalName args => do - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileExprListWithInternals fields dynamicSource internalFunctions args let tryFnName := s!"{externalName}_try" pure [YulStmt.letMany (successVar :: resultVars) (YulExpr.call tryFnName argExprs)] -- NOTE: safeTransfer, safeTransferFrom, externalCallWithReturn, callback, ecrecover @@ -377,7 +331,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | Stmt.ecm mod args => do if args.length != mod.numArgs then throw s!"ECM '{mod.name}': expected {mod.numArgs} arguments, got {args.length}" - let compiledArgs ← compileExprList fields dynamicSource args + let compiledArgs ← compileExprListWithInternals fields dynamicSource internalFunctions args let ctx : ECM.CompilationContext := { isDynamicFromCalldata := dynamicSource == .calldata } @@ -387,14 +341,14 @@ def compileStmt (fields : List Field) (events : List EventDef := []) if values.length != internalRetNames.length then throw s!"Compilation error: internal return arity mismatch: expected {internalRetNames.length}, got {values.length}" else - let compiled ← compileExprList fields dynamicSource values + let compiled ← compileExprListWithInternals fields dynamicSource internalFunctions values let assigns := (internalRetNames.zip compiled).map fun (name, valueExpr) => YulStmt.assign name valueExpr pure (assigns ++ [YulStmt.leave]) else if values.isEmpty then pure [YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 0])] else - let compiled ← compileExprList fields dynamicSource values + let compiled ← compileExprListWithInternals fields dynamicSource internalFunctions values let stores := (compiled.zipIdx.map fun (valueExpr, idx) => YulStmt.expr (YulExpr.call "mstore" [YulExpr.lit (idx * 32), valueExpr])) pure (stores ++ [YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit (values.length * 32)])]) @@ -463,7 +417,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | Stmt.returnCodeData pointer => do if isInternal then throw s!"Compilation error: internal functions cannot use returnCodeData" - let pointerExpr ← compileExpr fields dynamicSource pointer + let pointerExpr ← compileExprWithInternals fields dynamicSource internalFunctions pointer pure [ YulStmt.block [ YulStmt.let_ "__return_code_pointer" @@ -500,25 +454,25 @@ def compileStmt (fields : List Field) (events : List EventDef := []) ] | Stmt.mstore offset value => do pure [YulStmt.expr (YulExpr.call "mstore" [ - ← compileExpr fields dynamicSource offset, - ← compileExpr fields dynamicSource value + ← compileExprWithInternals fields dynamicSource internalFunctions offset, + ← compileExprWithInternals fields dynamicSource internalFunctions value ])] | Stmt.tstore offset value => do pure [YulStmt.expr (YulExpr.call "tstore" [ - ← compileExpr fields dynamicSource offset, - ← compileExpr fields dynamicSource value + ← compileExprWithInternals fields dynamicSource internalFunctions offset, + ← compileExprWithInternals fields dynamicSource internalFunctions value ])] | Stmt.calldatacopy destOffset sourceOffset size => do pure [YulStmt.expr (YulExpr.call "calldatacopy" [ - ← compileExpr fields dynamicSource destOffset, - ← compileExpr fields dynamicSource sourceOffset, - ← compileExpr fields dynamicSource size + ← compileExprWithInternals fields dynamicSource internalFunctions destOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions sourceOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions size ])] | Stmt.returndataCopy destOffset sourceOffset size => do pure [YulStmt.expr (YulExpr.call "returndatacopy" [ - ← compileExpr fields dynamicSource destOffset, - ← compileExpr fields dynamicSource sourceOffset, - ← compileExpr fields dynamicSource size + ← compileExprWithInternals fields dynamicSource internalFunctions destOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions sourceOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions size ])] | Stmt.revertReturndata => pure [YulStmt.block [ @@ -536,16 +490,16 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | Stmt.rawLog topics dataOffset dataSize => do if topics.length > 4 then throw s!"Compilation error: rawLog supports at most 4 topics (log0–log4), got {topics.length}" - let topicExprs ← compileExprList fields dynamicSource topics - let offsetExpr ← compileExpr fields dynamicSource dataOffset - let sizeExpr ← compileExpr fields dynamicSource dataSize + let topicExprs ← compileExprListWithInternals fields dynamicSource internalFunctions topics + let offsetExpr ← compileExprWithInternals fields dynamicSource internalFunctions dataOffset + let sizeExpr ← compileExprWithInternals fields dynamicSource internalFunctions dataSize let logFn := s!"log{topics.length}" pure [YulStmt.expr (YulExpr.call logFn ([offsetExpr, sizeExpr] ++ topicExprs))] -- ADT pattern match: compile to YulStmt.switch on tag value (#1727 Steps 5c/5d) | Stmt.matchAdt adtName scrutinee branches => do let def_ ← lookupAdtTypeDef adtTypes adtName -- Compile the scrutinee (tag value expression) - let scrutineeExpr ← compileExpr fields dynamicSource scrutinee + let scrutineeExpr ← compileExprWithInternals fields dynamicSource internalFunctions scrutinee -- Extract storage field name from scrutinee for field bindings let storageFieldName ← match scrutinee with | Expr.adtTag scrutineeAdtName fieldName => diff --git a/Compiler/CompilationModel/EventEmission.lean b/Compiler/CompilationModel/EventEmission.lean index 78138b8a3..2cda761c8 100644 --- a/Compiler/CompilationModel/EventEmission.lean +++ b/Compiler/CompilationModel/EventEmission.lean @@ -55,7 +55,8 @@ structure EventDynamicArraySource where source : DynamicDataSource def eventDynamicArraySource? - (fields : List Field) (dynamicSource : DynamicDataSource) : + (fields : List Field) (dynamicSource : DynamicDataSource) + (internalFunctions : List FunctionSpec := []) : Expr → Except String (Option EventDynamicArraySource) | Expr.param name => pure (some @@ -68,14 +69,14 @@ def eventDynamicArraySource? dataOffsetExpr := YulExpr.ident s!"{name}_data_offset" source := .memory }) | e@(Expr.paramDynamicMemberLength name wordOffset) => do - let dataOffsetExpr ← compileExpr fields dynamicSource + let dataOffsetExpr ← compileExprWithInternals fields dynamicSource internalFunctions (Expr.paramDynamicMemberDataOffset name wordOffset) - let lengthExpr ← compileExpr fields dynamicSource e + let lengthExpr ← compileExprWithInternals fields dynamicSource internalFunctions e pure (some { lengthExpr, dataOffsetExpr, source := dynamicSource }) | e@(Expr.arrayElementDynamicMemberLength name index wordOffset) => do - let dataOffsetExpr ← compileExpr fields dynamicSource + let dataOffsetExpr ← compileExprWithInternals fields dynamicSource internalFunctions (Expr.arrayElementDynamicMemberDataOffset name index wordOffset) - let lengthExpr ← compileExpr fields dynamicSource e + let lengthExpr ← compileExprWithInternals fields dynamicSource internalFunctions e pure (some { lengthExpr, dataOffsetExpr, source := dynamicSource }) | _ => pure none @@ -187,7 +188,8 @@ def compileScalarEmitFromCompiledArgs def compileEmit (fields : List Field) (events : List EventDef) (dynamicSource : DynamicDataSource := .calldata) - (eventName : String) (args : List Expr) : Except String (List YulStmt) := do + (eventName : String) (args : List Expr) (internalFunctions : List FunctionSpec := []) : + Except String (List YulStmt) := do let eventDef? := events.find? (·.name == eventName) let eventDef ← match eventDef? with @@ -195,7 +197,7 @@ def compileEmit (fields : List Field) (events : List EventDef) | none => throw s!"Compilation error: unknown event '{eventName}'" if args.length != eventDef.params.length then throw s!"Compilation error: event '{eventName}' expects {eventDef.params.length} args, got {args.length}" - let compiledArgs ← compileExprList fields dynamicSource args + let compiledArgs ← compileExprListWithInternals fields dynamicSource internalFunctions args let zippedWithSource := eventZippedWithSource eventDef args compiledArgs let indexed := eventIndexedArgs zippedWithSource let unindexed := eventUnindexedArgs zippedWithSource @@ -344,7 +346,7 @@ def compileEmit (fields : List Field) (events : List EventDef) | _ => throw s!"Compilation error: unindexed dynamic array event param '{p.name}' in event '{eventName}' currently requires direct parameter reference ({issue586Ref})." else if indexedDynamicArrayElemSupported elemTy then - match ← eventDynamicArraySource? fields dynamicSource srcExpr with + match ← eventDynamicArraySource? fields dynamicSource internalFunctions srcExpr with | some source => let lenName := s!"__evt_arg{argIdx}_len" let dstName := s!"__evt_arg{argIdx}_dst" @@ -527,7 +529,7 @@ def compileEmit (fields : List Field) (events : List EventDef) throw s!"Compilation error: indexed dynamic array event param '{p.name}' in event '{eventName}' currently requires direct parameter reference ({issue586Ref})." | _ => if indexedDynamicArrayElemSupported elemTy then - match ← eventDynamicArraySource? fields dynamicSource srcExpr with + match ← eventDynamicArraySource? fields dynamicSource internalFunctions srcExpr with | some source => let topicName := s!"__evt_topic{idx + 1}" let byteLenName := s!"__evt_arg{idx}_byte_len" diff --git a/Compiler/CompilationModel/ExpressionCompile.lean b/Compiler/CompilationModel/ExpressionCompile.lean index 20403a33e..4f92318d3 100644 --- a/Compiler/CompilationModel/ExpressionCompile.lean +++ b/Compiler/CompilationModel/ExpressionCompile.lean @@ -1,6 +1,7 @@ import Compiler.CompilationModel.Types import Compiler.CompilationModel.AdtStorageLayout import Compiler.CompilationModel.DynamicData +import Compiler.CompilationModel.InternalArgs import Compiler.CompilationModel.InternalNaming import Compiler.CompilationModel.ValidationHelpers @@ -38,19 +39,151 @@ def compileMappingSlotRead (fields : List Field) (field : String) (keyExpr : Yul def compileMappingSlotChain (baseSlot : YulExpr) (keys : List YulExpr) : YulExpr := keys.foldl (fun slotExpr keyExpr => YulExpr.call "mappingSlot" [slotExpr, keyExpr]) baseSlot +def findInternalFunctionForCall? (functions : List FunctionSpec) (name : String) : Option FunctionSpec := + match functions.filter (fun fn => fn.isInternal && fn.name == name) with + | [fn] => some fn + | _ => none + +def directForwardedInternalCallArgName? : Expr → Option String + | Expr.param name => some name + | _ => none + -- Compile expression to Yul (using mutual recursion for lists) +set_option maxHeartbeats 800000 in mutual -def compileExprList (fields : List Field) - (dynamicSource : DynamicDataSource := .calldata) : +def compileExprListWithInternals (fields : List Field) + (dynamicSource : DynamicDataSource := .calldata) + (internalFunctions : List FunctionSpec := []) : List Expr → Except String (List YulExpr) | [] => pure [] | e :: es => do - let head ← compileExpr fields dynamicSource e - let tail ← compileExprList fields dynamicSource es + let head ← compileExprWithInternals fields dynamicSource internalFunctions e + let tail ← compileExprListWithInternals fields dynamicSource internalFunctions es pure (head :: tail) -def compileExpr (fields : List Field) - (dynamicSource : DynamicDataSource := .calldata) : +def compileInternalCallArg (fields : List Field) (dynamicSource : DynamicDataSource) + (internalFunctions : List FunctionSpec) (calleeName : String) (param : Param) (arg : Expr) : + Except String (List YulExpr) := do + if isExpandedInternalParamType param.ty then + match directForwardedInternalCallArgName? arg with + | some name => + pure ((internalCallYulArgNamesForParam name param).map YulExpr.ident) + | none => + throw s!"Compilation error: internal call '{calleeName}' argument for parameter '{param.name}' with type {repr param.ty} must be a direct parameter forwarding expression (issue #1889)." + else + pure [← compileExprWithInternals fields dynamicSource internalFunctions arg] + +def compileInternalCallArgsWithParams (fields : List Field) (dynamicSource : DynamicDataSource) + (internalFunctions : List FunctionSpec) (calleeName : String) : List Param → List Expr → + Except String (List YulExpr) + | [], [] => pure [] + | param :: params, arg :: args => do + let head ← compileInternalCallArg fields dynamicSource internalFunctions calleeName param arg + let tail ← compileInternalCallArgsWithParams fields dynamicSource internalFunctions calleeName params args + pure (head ++ tail) + | params, args => + throw s!"Compilation error: internal call '{calleeName}' received {args.length} source arg(s), expected {params.length} (issue #1889)." + +def compileExpandedInternalCallArgsWithParams + (fields : List Field) (dynamicSource : DynamicDataSource) + (internalFunctions : List FunctionSpec) (calleeName : String) : List Param → List Expr → + Except String (List YulExpr) + | [], [] => pure [] + | param :: params, args => do + let expectedNames := internalFunctionYulParamNames [param] + let head := args.take expectedNames.length + let tail := args.drop expectedNames.length + let compileForwardedIndex : Expr → Except String YulExpr + | Expr.param name => pure (YulExpr.ident name) + | Expr.localVar name => pure (YulExpr.ident name) + | Expr.literal n => pure (YulExpr.lit (n % uint256Modulus)) + | _ => + throw s!"Compilation error: internal call '{calleeName}' checked dynamic-member projection uses an unsupported computed index (issue #1889)." + let dynamicMemberHelperNames + (lengthCalldata lengthMemory offsetCalldata offsetMemory : String) + (isLength : Bool) : String := + match dynamicSource with + | .calldata => if isLength then lengthCalldata else offsetCalldata + | .memory => if isLength then lengthMemory else offsetMemory + let compileProjection : Expr → Except String YulExpr + | Expr.paramDynamicMemberDataOffset name wordOffset => + pure (YulExpr.call + (dynamicMemberHelperNames + checkedParamDynamicMemberLengthCalldataHelperName + checkedParamDynamicMemberLengthMemoryHelperName + checkedParamDynamicMemberDataOffsetCalldataHelperName + checkedParamDynamicMemberDataOffsetMemoryHelperName + false) + [YulExpr.ident s!"{name}_data_offset", YulExpr.lit wordOffset]) + | Expr.paramDynamicMemberLength name wordOffset => + pure (YulExpr.call + (dynamicMemberHelperNames + checkedParamDynamicMemberLengthCalldataHelperName + checkedParamDynamicMemberLengthMemoryHelperName + checkedParamDynamicMemberDataOffsetCalldataHelperName + checkedParamDynamicMemberDataOffsetMemoryHelperName + true) + [YulExpr.ident s!"{name}_data_offset", YulExpr.lit wordOffset]) + | Expr.arrayElementDynamicMemberDataOffset name index wordOffset => do + let indexExpr ← compileForwardedIndex index + pure (YulExpr.call + (dynamicMemberHelperNames + checkedArrayElementDynamicMemberLengthCalldataHelperName + checkedArrayElementDynamicMemberLengthMemoryHelperName + checkedArrayElementDynamicMemberDataOffsetCalldataHelperName + checkedArrayElementDynamicMemberDataOffsetMemoryHelperName + false) + [YulExpr.ident s!"{name}_data_offset", YulExpr.ident s!"{name}_length", indexExpr, YulExpr.lit wordOffset]) + | Expr.arrayElementDynamicMemberLength name index wordOffset => do + let indexExpr ← compileForwardedIndex index + pure (YulExpr.call + (dynamicMemberHelperNames + checkedArrayElementDynamicMemberLengthCalldataHelperName + checkedArrayElementDynamicMemberLengthMemoryHelperName + checkedArrayElementDynamicMemberDataOffsetCalldataHelperName + checkedArrayElementDynamicMemberDataOffsetMemoryHelperName + true) + [YulExpr.ident s!"{name}_data_offset", YulExpr.ident s!"{name}_length", indexExpr, YulExpr.lit wordOffset]) + | _ => + throw s!"Compilation error: internal call '{calleeName}' expanded arguments must be direct parameters or checked dynamic-member projections (issue #1889)." + let rec compileExpanded : List Expr → Except String (List YulExpr) + | [] => pure [] + | Expr.param argName :: rest => do + let compiledRest ← compileExpanded rest + pure (YulExpr.ident argName :: compiledRest) + | e@(Expr.paramDynamicMemberDataOffset _ _) :: rest + | e@(Expr.paramDynamicMemberLength _ _) :: rest + | e@(Expr.arrayElementDynamicMemberDataOffset _ _ _) :: rest + | e@(Expr.arrayElementDynamicMemberLength _ _ _) :: rest => do + let compiledHead ← compileProjection e + let compiledRest ← compileExpanded rest + pure (compiledHead :: compiledRest) + | _ :: _ => + throw s!"Compilation error: internal call '{calleeName}' expanded arguments must be direct parameters or checked dynamic-member projections (issue #1889)." + let headExprs ← compileExpanded head + let tailExprs ← compileExpandedInternalCallArgsWithParams fields dynamicSource internalFunctions calleeName params tail + pure (headExprs ++ tailExprs) + | [], _ :: _ => + throw s!"Compilation error: internal call '{calleeName}' received extra expanded argument(s) (issue #1889)." + +def compileInternalCallArgs (fields : List Field) (dynamicSource : DynamicDataSource) + (internalFunctions : List FunctionSpec) (calleeName : String) (args : List Expr) : + Except String (List YulExpr) := + match findInternalFunctionForCall? internalFunctions calleeName with + | some callee => + let expandedArgCount := + callee.params.foldl (fun acc param => acc + (internalFunctionYulParamNames [param]).length) 0 + if args.length == callee.params.length then + compileInternalCallArgsWithParams fields dynamicSource internalFunctions calleeName callee.params args + else if args.length == expandedArgCount then + compileExpandedInternalCallArgsWithParams fields dynamicSource internalFunctions calleeName callee.params args + else + compileInternalCallArgsWithParams fields dynamicSource internalFunctions calleeName callee.params args + | none => compileExprListWithInternals fields dynamicSource internalFunctions args + +def compileExprWithInternals (fields : List Field) + (dynamicSource : DynamicDataSource := .calldata) + (internalFunctions : List FunctionSpec := []) : Expr → Except String YulExpr | Expr.literal n => pure (YulExpr.lit (n % uint256Modulus)) | Expr.param name => pure (YulExpr.ident name) @@ -90,14 +223,14 @@ def compileExpr (fields : List Field) throw s!"Compilation error: field '{field}' is not address-typed; use Expr.storage instead" | none => throw s!"Compilation error: unknown storage field '{field}'" | Expr.mapping field key => do - compileMappingSlotRead fields field (← compileExpr fields dynamicSource key) "mapping" + compileMappingSlotRead fields field (← compileExprWithInternals fields dynamicSource internalFunctions key) "mapping" | Expr.mappingWord field key wordOffset => do - compileMappingSlotRead fields field (← compileExpr fields dynamicSource key) "mappingWord" wordOffset + compileMappingSlotRead fields field (← compileExprWithInternals fields dynamicSource internalFunctions key) "mappingWord" wordOffset | Expr.mappingPackedWord field key wordOffset packed => do if !packedBitsValid packed then throw s!"Compilation error: Expr.mappingPackedWord for field '{field}' has invalid packed range offset={packed.offset} width={packed.width}. Require 0 < width <= 256, offset < 256, and offset + width <= 256." else do - let slotWord ← compileMappingSlotRead fields field (← compileExpr fields dynamicSource key) "mappingPackedWord" wordOffset + let slotWord ← compileMappingSlotRead fields field (← compileExprWithInternals fields dynamicSource internalFunctions key) "mappingPackedWord" wordOffset pure (YulExpr.call "and" [ YulExpr.call "shr" [YulExpr.lit packed.offset, slotWord], YulExpr.lit (packedMaskNat packed) @@ -108,8 +241,8 @@ def compileExpr (fields : List Field) else match findFieldSlot fields field with | some slot => do - let key1Expr ← compileExpr fields dynamicSource key1 - let key2Expr ← compileExpr fields dynamicSource key2 + let key1Expr ← compileExprWithInternals fields dynamicSource internalFunctions key1 + let key2Expr ← compileExprWithInternals fields dynamicSource internalFunctions key2 let innerSlot := YulExpr.call "mappingSlot" [YulExpr.lit slot, key1Expr] pure (YulExpr.call "sload" [YulExpr.call "mappingSlot" [innerSlot, key2Expr]]) | none => throw s!"Compilation error: unknown mapping field '{field}'" @@ -119,22 +252,22 @@ def compileExpr (fields : List Field) else match findFieldSlot fields field with | some slot => do - let key1Expr ← compileExpr fields dynamicSource key1 - let key2Expr ← compileExpr fields dynamicSource key2 + let key1Expr ← compileExprWithInternals fields dynamicSource internalFunctions key1 + let key2Expr ← compileExprWithInternals fields dynamicSource internalFunctions key2 let innerSlot := YulExpr.call "mappingSlot" [YulExpr.lit slot, key1Expr] let outerSlot := YulExpr.call "mappingSlot" [innerSlot, key2Expr] let finalSlot := if wordOffset == 0 then outerSlot else YulExpr.call "add" [outerSlot, YulExpr.lit wordOffset] pure (YulExpr.call "sload" [finalSlot]) | none => throw s!"Compilation error: unknown mapping field '{field}'" | Expr.mappingUint field key => do - compileMappingSlotRead fields field (← compileExpr fields dynamicSource key) "mappingUint" + compileMappingSlotRead fields field (← compileExprWithInternals fields dynamicSource internalFunctions key) "mappingUint" | Expr.mappingChain field keys => if !isMapping fields field then throw s!"Compilation error: field '{field}' is not a mapping" else match findFieldSlot fields field with | some slot => do - let keyExprs ← compileExprList fields dynamicSource keys + let keyExprs ← compileExprListWithInternals fields dynamicSource internalFunctions keys pure (YulExpr.call "sload" [compileMappingSlotChain (YulExpr.lit slot) keyExprs]) | none => throw s!"Compilation error: unknown mapping field '{field}'" | Expr.structMember field key memberName => do @@ -148,9 +281,9 @@ def compileExpr (fields : List Field) | some member => match member.packed with | none => - compileMappingSlotRead fields field (← compileExpr fields dynamicSource key) s!"structMember.{memberName}" member.wordOffset + compileMappingSlotRead fields field (← compileExprWithInternals fields dynamicSource internalFunctions key) s!"structMember.{memberName}" member.wordOffset | some packed => - let slotWord ← compileMappingSlotRead fields field (← compileExpr fields dynamicSource key) s!"structMember.{memberName}" member.wordOffset + let slotWord ← compileMappingSlotRead fields field (← compileExprWithInternals fields dynamicSource internalFunctions key) s!"structMember.{memberName}" member.wordOffset pure (YulExpr.call "and" [ YulExpr.call "shr" [YulExpr.lit packed.offset, slotWord], YulExpr.lit (packedMaskNat packed) @@ -167,8 +300,8 @@ def compileExpr (fields : List Field) | some member => match findFieldSlot fields field with | some slot => do - let key1Expr ← compileExpr fields dynamicSource key1 - let key2Expr ← compileExpr fields dynamicSource key2 + let key1Expr ← compileExprWithInternals fields dynamicSource internalFunctions key1 + let key2Expr ← compileExprWithInternals fields dynamicSource internalFunctions key2 let innerSlot := YulExpr.call "mappingSlot" [YulExpr.lit slot, key1Expr] let outerSlot := YulExpr.call "mappingSlot" [innerSlot, key2Expr] let finalSlot := if member.wordOffset == 0 then outerSlot else YulExpr.call "add" [outerSlot, YulExpr.lit member.wordOffset] @@ -186,55 +319,55 @@ def compileExpr (fields : List Field) | Expr.txOrigin => pure (YulExpr.call "origin" []) | Expr.chainid => pure (YulExpr.call "chainid" []) | Expr.extcodesize addr => do - pure (YulExpr.call "extcodesize" [← compileExpr fields dynamicSource addr]) + pure (YulExpr.call "extcodesize" [← compileExprWithInternals fields dynamicSource internalFunctions addr]) | Expr.msgValue => pure (YulExpr.call "callvalue" []) | Expr.selfBalance => pure (YulExpr.call "selfbalance" []) | Expr.blockTimestamp => pure (YulExpr.call "timestamp" []) | Expr.blockNumber => pure (YulExpr.call "number" []) | Expr.blobbasefee => pure (YulExpr.call "blobbasefee" []) | Expr.mload offset => do - pure (YulExpr.call "mload" [← compileExpr fields dynamicSource offset]) + pure (YulExpr.call "mload" [← compileExprWithInternals fields dynamicSource internalFunctions offset]) | Expr.tload offset => do - pure (YulExpr.call "tload" [← compileExpr fields dynamicSource offset]) + pure (YulExpr.call "tload" [← compileExprWithInternals fields dynamicSource internalFunctions offset]) | Expr.keccak256 offset size => do pure (YulExpr.call "keccak256" [ - ← compileExpr fields dynamicSource offset, - ← compileExpr fields dynamicSource size + ← compileExprWithInternals fields dynamicSource internalFunctions offset, + ← compileExprWithInternals fields dynamicSource internalFunctions size ]) | Expr.call gas target value inOffset inSize outOffset outSize => do pure (YulExpr.call "call" [ - ← compileExpr fields dynamicSource gas, - ← compileExpr fields dynamicSource target, - ← compileExpr fields dynamicSource value, - ← compileExpr fields dynamicSource inOffset, - ← compileExpr fields dynamicSource inSize, - ← compileExpr fields dynamicSource outOffset, - ← compileExpr fields dynamicSource outSize + ← compileExprWithInternals fields dynamicSource internalFunctions gas, + ← compileExprWithInternals fields dynamicSource internalFunctions target, + ← compileExprWithInternals fields dynamicSource internalFunctions value, + ← compileExprWithInternals fields dynamicSource internalFunctions inOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions inSize, + ← compileExprWithInternals fields dynamicSource internalFunctions outOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions outSize ]) | Expr.staticcall gas target inOffset inSize outOffset outSize => do pure (YulExpr.call "staticcall" [ - ← compileExpr fields dynamicSource gas, - ← compileExpr fields dynamicSource target, - ← compileExpr fields dynamicSource inOffset, - ← compileExpr fields dynamicSource inSize, - ← compileExpr fields dynamicSource outOffset, - ← compileExpr fields dynamicSource outSize + ← compileExprWithInternals fields dynamicSource internalFunctions gas, + ← compileExprWithInternals fields dynamicSource internalFunctions target, + ← compileExprWithInternals fields dynamicSource internalFunctions inOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions inSize, + ← compileExprWithInternals fields dynamicSource internalFunctions outOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions outSize ]) | Expr.delegatecall gas target inOffset inSize outOffset outSize => do pure (YulExpr.call "delegatecall" [ - ← compileExpr fields dynamicSource gas, - ← compileExpr fields dynamicSource target, - ← compileExpr fields dynamicSource inOffset, - ← compileExpr fields dynamicSource inSize, - ← compileExpr fields dynamicSource outOffset, - ← compileExpr fields dynamicSource outSize + ← compileExprWithInternals fields dynamicSource internalFunctions gas, + ← compileExprWithInternals fields dynamicSource internalFunctions target, + ← compileExprWithInternals fields dynamicSource internalFunctions inOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions inSize, + ← compileExprWithInternals fields dynamicSource internalFunctions outOffset, + ← compileExprWithInternals fields dynamicSource internalFunctions outSize ]) | Expr.calldatasize => pure (YulExpr.call "calldatasize" []) | Expr.calldataload offset => do - pure (YulExpr.call "calldataload" [← compileExpr fields dynamicSource offset]) + pure (YulExpr.call "calldataload" [← compileExprWithInternals fields dynamicSource internalFunctions offset]) | Expr.returndataSize => pure (YulExpr.call "returndatasize" []) | Expr.returndataOptionalBoolAt outOffset => do - let outOffsetExpr ← compileExpr fields dynamicSource outOffset + let outOffsetExpr ← compileExprWithInternals fields dynamicSource internalFunctions outOffset let rdSize := YulExpr.call "returndatasize" [] pure (YulExpr.call "or" [ YulExpr.call "eq" [rdSize, YulExpr.lit 0], @@ -245,7 +378,7 @@ def compileExpr (fields : List Field) ]) | Expr.localVar name => pure (YulExpr.ident name) | Expr.externalCall name args => do - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileExprListWithInternals fields dynamicSource internalFunctions args if name == builtinExpName then match argExprs with | [base, exponent] => pure (YulExpr.call "exp" [base, exponent]) @@ -253,12 +386,12 @@ def compileExpr (fields : List Field) else pure (YulExpr.call name argExprs) | Expr.internalCall functionName args => do - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileInternalCallArgs fields dynamicSource internalFunctions functionName args pure (YulExpr.call (internalFunctionYulName functionName) argExprs) | Expr.arrayLength name => pure (YulExpr.ident s!"{name}_length") | Expr.memoryArrayLength name => pure (YulExpr.ident s!"{name}_length") | Expr.arrayElement name index => do - let indexExpr ← compileExpr fields dynamicSource index + let indexExpr ← compileExprWithInternals fields dynamicSource internalFunctions index let helperName := match dynamicSource with | .calldata => checkedArrayElementCalldataHelperName | .memory => checkedArrayElementMemoryHelperName @@ -268,7 +401,7 @@ def compileExpr (fields : List Field) indexExpr ]) | Expr.memoryArrayElement name index => do - let indexExpr ← compileExpr fields dynamicSource index + let indexExpr ← compileExprWithInternals fields dynamicSource internalFunctions index pure (YulExpr.call checkedArrayElementMemoryHelperName [ YulExpr.ident s!"{name}_data_offset", YulExpr.ident s!"{name}_length", @@ -280,7 +413,7 @@ def compileExpr (fields : List Field) else if wordOffset >= elementWords then throw s!"Compilation error: Expr.arrayElementWord '{name}' wordOffset {wordOffset} is outside element width {elementWords}" else - let indexExpr ← compileExpr fields dynamicSource index + let indexExpr ← compileExprWithInternals fields dynamicSource internalFunctions index let helperName := match dynamicSource with | .calldata => checkedArrayElementWordCalldataHelperName | .memory => checkedArrayElementWordMemoryHelperName @@ -292,7 +425,7 @@ def compileExpr (fields : List Field) YulExpr.lit wordOffset ]) | Expr.arrayElementDynamicWord name index wordOffset => do - let indexExpr ← compileExpr fields dynamicSource index + let indexExpr ← compileExprWithInternals fields dynamicSource internalFunctions index let helperName := match dynamicSource with | .calldata => checkedArrayElementDynamicWordCalldataHelperName | .memory => checkedArrayElementDynamicWordMemoryHelperName @@ -303,7 +436,7 @@ def compileExpr (fields : List Field) YulExpr.lit wordOffset ]) | Expr.arrayElementDynamicDataOffset name index => do - let indexExpr ← compileExpr fields dynamicSource index + let indexExpr ← compileExprWithInternals fields dynamicSource internalFunctions index let helperName := match dynamicSource with | .calldata => checkedArrayElementDynamicDataOffsetCalldataHelperName | .memory => checkedArrayElementDynamicDataOffsetMemoryHelperName @@ -337,7 +470,7 @@ def compileExpr (fields : List Field) YulExpr.lit wordOffset ]) | Expr.paramDynamicMemberElement name wordOffset innerIndex => do - let innerIndexExpr ← compileExpr fields dynamicSource innerIndex + let innerIndexExpr ← compileExprWithInternals fields dynamicSource internalFunctions innerIndex let helperName := match dynamicSource with | .calldata => checkedParamDynamicMemberElementCalldataHelperName | .memory => checkedParamDynamicMemberElementMemoryHelperName @@ -352,7 +485,7 @@ def compileExpr (fields : List Field) YulExpr.lit (wordOffset * 32) ]) | Expr.arrayElementDynamicMemberLength name index wordOffset => do - let indexExpr ← compileExpr fields dynamicSource index + let indexExpr ← compileExprWithInternals fields dynamicSource internalFunctions index let helperName := match dynamicSource with | .calldata => checkedArrayElementDynamicMemberLengthCalldataHelperName | .memory => checkedArrayElementDynamicMemberLengthMemoryHelperName @@ -363,7 +496,7 @@ def compileExpr (fields : List Field) YulExpr.lit wordOffset ]) | Expr.arrayElementDynamicMemberDataOffset name index wordOffset => do - let indexExpr ← compileExpr fields dynamicSource index + let indexExpr ← compileExprWithInternals fields dynamicSource internalFunctions index let helperName := match dynamicSource with | .calldata => checkedArrayElementDynamicMemberDataOffsetCalldataHelperName | .memory => checkedArrayElementDynamicMemberDataOffsetMemoryHelperName @@ -374,8 +507,8 @@ def compileExpr (fields : List Field) YulExpr.lit wordOffset ]) | Expr.arrayElementDynamicMemberElement name index wordOffset innerIndex => do - let indexExpr ← compileExpr fields dynamicSource index - let innerIndexExpr ← compileExpr fields dynamicSource innerIndex + let indexExpr ← compileExprWithInternals fields dynamicSource internalFunctions index + let innerIndexExpr ← compileExprWithInternals fields dynamicSource internalFunctions innerIndex let helperName := match dynamicSource with | .calldata => checkedArrayElementDynamicMemberElementCalldataHelperName | .memory => checkedArrayElementDynamicMemberElementMemoryHelperName @@ -403,7 +536,7 @@ def compileExpr (fields : List Field) | .dynamicArray _ => do pure (YulExpr.call checkedStorageArrayElementHelperName [ YulExpr.lit slot, - ← compileExpr fields dynamicSource index + ← compileExprWithInternals fields dynamicSource internalFunctions index ]) | _ => throw s!"Compilation error: field '{field}' is not a storage dynamic array; use Expr.storageArrayElement only with FieldType.dynamicArray" @@ -419,25 +552,25 @@ def compileExpr (fields : List Field) YulExpr.ident s!"{rhsName}_data_offset", YulExpr.ident s!"{rhsName}_length" ]) - | Expr.add a b => return yulBinOp "add" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.sub a b => return yulBinOp "sub" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.mul a b => return yulBinOp "mul" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.div a b => return yulBinOp "div" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.sdiv a b => return yulBinOp "sdiv" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.mod a b => return yulBinOp "mod" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.smod a b => return yulBinOp "smod" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.bitAnd a b => return yulBinOp "and" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.bitOr a b => return yulBinOp "or" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.bitXor a b => return yulBinOp "xor" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.bitNot a => return YulExpr.call "not" [← compileExpr fields dynamicSource a] - | Expr.shl s v => return yulBinOp "shl" (← compileExpr fields dynamicSource s) (← compileExpr fields dynamicSource v) - | Expr.shr s v => return yulBinOp "shr" (← compileExpr fields dynamicSource s) (← compileExpr fields dynamicSource v) - | Expr.sar s v => return yulBinOp "sar" (← compileExpr fields dynamicSource s) (← compileExpr fields dynamicSource v) - | Expr.byte i v => return yulBinOp "byte" (← compileExpr fields dynamicSource i) (← compileExpr fields dynamicSource v) + | Expr.add a b => return yulBinOp "add" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.sub a b => return yulBinOp "sub" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.mul a b => return yulBinOp "mul" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.div a b => return yulBinOp "div" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.sdiv a b => return yulBinOp "sdiv" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.mod a b => return yulBinOp "mod" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.smod a b => return yulBinOp "smod" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.bitAnd a b => return yulBinOp "and" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.bitOr a b => return yulBinOp "or" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.bitXor a b => return yulBinOp "xor" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.bitNot a => return YulExpr.call "not" [← compileExprWithInternals fields dynamicSource internalFunctions a] + | Expr.shl s v => return yulBinOp "shl" (← compileExprWithInternals fields dynamicSource internalFunctions s) (← compileExprWithInternals fields dynamicSource internalFunctions v) + | Expr.shr s v => return yulBinOp "shr" (← compileExprWithInternals fields dynamicSource internalFunctions s) (← compileExprWithInternals fields dynamicSource internalFunctions v) + | Expr.sar s v => return yulBinOp "sar" (← compileExprWithInternals fields dynamicSource internalFunctions s) (← compileExprWithInternals fields dynamicSource internalFunctions v) + | Expr.byte i v => return yulBinOp "byte" (← compileExprWithInternals fields dynamicSource internalFunctions i) (← compileExprWithInternals fields dynamicSource internalFunctions v) | Expr.signextend b v => - return yulBinOp "signextend" (← compileExpr fields dynamicSource b) (← compileExpr fields dynamicSource v) + return yulBinOp "signextend" (← compileExprWithInternals fields dynamicSource internalFunctions b) (← compileExprWithInternals fields dynamicSource internalFunctions v) | Expr.intrinsic name lowering _minFork args => do - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileExprListWithInternals fields dynamicSource internalFunctions args match lowering with | .verbatim inArity outArity opcodeHex => if outArity != 1 then @@ -456,19 +589,19 @@ def compileExpr (fields : List Field) pure (YulExpr.call builtinName argExprs) | Expr.forkIfAtLeast required _thenExpr _elseExpr => throw s!"Compilation error: unresolved fork_if_at_least {required}; compile through compileSpecsWithOptions so the branch can be selected from --target-fork before Yul emission" - | Expr.eq a b => return yulBinOp "eq" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.gt a b => return yulBinOp "gt" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.sgt a b => return yulBinOp "sgt" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.lt a b => return yulBinOp "lt" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.slt a b => return yulBinOp "slt" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.ge a b => return yulNegatedBinOp "lt" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.le a b => return yulNegatedBinOp "gt" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.logicalAnd a b => return yulBinOp "and" (yulToBool (← compileExpr fields dynamicSource a)) (yulToBool (← compileExpr fields dynamicSource b)) - | Expr.logicalOr a b => return yulBinOp "or" (yulToBool (← compileExpr fields dynamicSource a)) (yulToBool (← compileExpr fields dynamicSource b)) - | Expr.logicalNot a => return YulExpr.call "iszero" [← compileExpr fields dynamicSource a] + | Expr.eq a b => return yulBinOp "eq" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.gt a b => return yulBinOp "gt" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.sgt a b => return yulBinOp "sgt" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.lt a b => return yulBinOp "lt" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.slt a b => return yulBinOp "slt" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.ge a b => return yulNegatedBinOp "lt" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.le a b => return yulNegatedBinOp "gt" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.logicalAnd a b => return yulBinOp "and" (yulToBool (← compileExprWithInternals fields dynamicSource internalFunctions a)) (yulToBool (← compileExprWithInternals fields dynamicSource internalFunctions b)) + | Expr.logicalOr a b => return yulBinOp "or" (yulToBool (← compileExprWithInternals fields dynamicSource internalFunctions a)) (yulToBool (← compileExprWithInternals fields dynamicSource internalFunctions b)) + | Expr.logicalNot a => return YulExpr.call "iszero" [← compileExprWithInternals fields dynamicSource internalFunctions a] | Expr.ceilDiv a b => do - let ca ← compileExpr fields dynamicSource a - let cb ← compileExpr fields dynamicSource b + let ca ← compileExprWithInternals fields dynamicSource internalFunctions a + let cb ← compileExprWithInternals fields dynamicSource internalFunctions b -- mul(iszero(iszero(a)), add(div(sub(a, 1), b), 1)) -- When a == 0: iszero(iszero(0)) = 0, so result = 0 -- When a > 0: iszero(iszero(a)) = 1, so result = (a-1)/b + 1 @@ -480,15 +613,15 @@ def compileExpr (fields : List Field) ] ]) | Expr.mulDivDown a b c => do - let ca ← compileExpr fields dynamicSource a - let cb ← compileExpr fields dynamicSource b - let cc ← compileExpr fields dynamicSource c + let ca ← compileExprWithInternals fields dynamicSource internalFunctions a + let cb ← compileExprWithInternals fields dynamicSource internalFunctions b + let cc ← compileExprWithInternals fields dynamicSource internalFunctions c -- div(mul(a, b), c) pure (YulExpr.call "div" [YulExpr.call "mul" [ca, cb], cc]) | Expr.mulDivUp a b c => do - let ca ← compileExpr fields dynamicSource a - let cb ← compileExpr fields dynamicSource b - let cc ← compileExpr fields dynamicSource c + let ca ← compileExprWithInternals fields dynamicSource internalFunctions a + let cb ← compileExprWithInternals fields dynamicSource internalFunctions b + let cc ← compileExprWithInternals fields dynamicSource internalFunctions c -- div(add(mul(a, b), sub(c, 1)), c) pure (YulExpr.call "div" [ YulExpr.call "add" [ @@ -502,23 +635,23 @@ def compileExpr (fields : List Field) -- handled at 512-bit precision; the helper reverts on zero divisor -- or when the quotient does not fit in `uint256`. | Expr.mulDiv512Down a b c => do - let ca ← compileExpr fields dynamicSource a - let cb ← compileExpr fields dynamicSource b - let cc ← compileExpr fields dynamicSource c + let ca ← compileExprWithInternals fields dynamicSource internalFunctions a + let cb ← compileExprWithInternals fields dynamicSource internalFunctions b + let cc ← compileExprWithInternals fields dynamicSource internalFunctions c pure (YulExpr.call fullMulDivHelperName [ca, cb, cc]) | Expr.mulDiv512Up a b c => do - let ca ← compileExpr fields dynamicSource a - let cb ← compileExpr fields dynamicSource b - let cc ← compileExpr fields dynamicSource c + let ca ← compileExprWithInternals fields dynamicSource internalFunctions a + let cb ← compileExprWithInternals fields dynamicSource internalFunctions b + let cc ← compileExprWithInternals fields dynamicSource internalFunctions c pure (YulExpr.call fullMulDivUpHelperName [ca, cb, cc]) | Expr.wMulDown a b => do - let ca ← compileExpr fields dynamicSource a - let cb ← compileExpr fields dynamicSource b + let ca ← compileExprWithInternals fields dynamicSource internalFunctions a + let cb ← compileExprWithInternals fields dynamicSource internalFunctions b -- div(mul(a, b), 1000000000000000000) pure (YulExpr.call "div" [YulExpr.call "mul" [ca, cb], YulExpr.lit 1000000000000000000]) | Expr.wDivUp a b => do - let ca ← compileExpr fields dynamicSource a - let cb ← compileExpr fields dynamicSource b + let ca ← compileExprWithInternals fields dynamicSource internalFunctions a + let cb ← compileExprWithInternals fields dynamicSource internalFunctions b -- div(add(mul(a, 1000000000000000000), sub(b, 1)), b) pure (YulExpr.call "div" [ YulExpr.call "add" [ @@ -528,8 +661,8 @@ def compileExpr (fields : List Field) cb ]) | Expr.min a b => do - let ca ← compileExpr fields dynamicSource a - let cb ← compileExpr fields dynamicSource b + let ca ← compileExprWithInternals fields dynamicSource internalFunctions a + let cb ← compileExprWithInternals fields dynamicSource internalFunctions b -- sub(a, mul(sub(a, b), gt(a, b))) pure (YulExpr.call "sub" [ca, YulExpr.call "mul" [ @@ -538,8 +671,8 @@ def compileExpr (fields : List Field) ] ]) | Expr.max a b => do - let ca ← compileExpr fields dynamicSource a - let cb ← compileExpr fields dynamicSource b + let ca ← compileExprWithInternals fields dynamicSource internalFunctions a + let cb ← compileExprWithInternals fields dynamicSource internalFunctions b -- add(a, mul(sub(b, a), gt(b, a))) pure (YulExpr.call "add" [ca, YulExpr.call "mul" [ @@ -548,9 +681,9 @@ def compileExpr (fields : List Field) ] ]) | Expr.ite cond thenVal elseVal => do - let condExpr ← compileExpr fields dynamicSource cond - let thenExpr ← compileExpr fields dynamicSource thenVal - let elseExpr ← compileExpr fields dynamicSource elseVal + let condExpr ← compileExprWithInternals fields dynamicSource internalFunctions cond + let thenExpr ← compileExprWithInternals fields dynamicSource internalFunctions thenVal + let elseExpr ← compileExprWithInternals fields dynamicSource internalFunctions elseVal -- Branchless ternary: add(mul(iszero(iszero(cond)), thenVal), mul(iszero(cond), elseVal)) let condBool := YulExpr.call "iszero" [YulExpr.call "iszero" [condExpr]] let condNeg := YulExpr.call "iszero" [condExpr] @@ -575,12 +708,43 @@ def compileExpr (fields : List Field) | none => throw s!"Compilation error: unknown storage field '{storageField}' for ADT field read" end +def compileExprList (fields : List Field) + (dynamicSource : DynamicDataSource := .calldata) (exprs : List Expr) : + Except String (List YulExpr) := + compileExprListWithInternals fields dynamicSource [] exprs + +def compileExpr (fields : List Field) + (dynamicSource : DynamicDataSource := .calldata) (expr : Expr) : + Except String YulExpr := + compileExprWithInternals fields dynamicSource [] expr + -- Compile require condition to a "failure" predicate to avoid double-negation. -def compileRequireFailCond (fields : List Field) - (dynamicSource : DynamicDataSource := .calldata) : +def compileRequireFailCondWithInternals (fields : List Field) + (dynamicSource : DynamicDataSource := .calldata) + (internalFunctions : List FunctionSpec := []) : Expr → Except String YulExpr - | Expr.ge a b => return yulBinOp "lt" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | Expr.le a b => return yulBinOp "gt" (← compileExpr fields dynamicSource a) (← compileExpr fields dynamicSource b) - | cond => return YulExpr.call "iszero" [← compileExpr fields dynamicSource cond] + | Expr.ge a b => return yulBinOp "lt" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | Expr.le a b => return yulBinOp "gt" (← compileExprWithInternals fields dynamicSource internalFunctions a) (← compileExprWithInternals fields dynamicSource internalFunctions b) + | cond => return YulExpr.call "iszero" [← compileExprWithInternals fields dynamicSource internalFunctions cond] + +def compileRequireFailCond (fields : List Field) + (dynamicSource : DynamicDataSource := .calldata) (cond : Expr) : + Except String YulExpr := + compileRequireFailCondWithInternals fields dynamicSource [] cond + +theorem compileExprWithInternals_nil_eq + (fields : List Field) (dynamicSource : DynamicDataSource) (expr : Expr) : + compileExprWithInternals fields dynamicSource [] expr = + compileExpr fields dynamicSource expr := rfl + +theorem compileExprListWithInternals_nil_eq + (fields : List Field) (dynamicSource : DynamicDataSource) (exprs : List Expr) : + compileExprListWithInternals fields dynamicSource [] exprs = + compileExprList fields dynamicSource exprs := rfl + +theorem compileRequireFailCondWithInternals_nil_eq + (fields : List Field) (dynamicSource : DynamicDataSource) (cond : Expr) : + compileRequireFailCondWithInternals fields dynamicSource [] cond = + compileRequireFailCond fields dynamicSource cond := rfl end Compiler.CompilationModel diff --git a/Compiler/CompilationModel/StorageWrites.lean b/Compiler/CompilationModel/StorageWrites.lean index 152811aa2..7a6373b8d 100644 --- a/Compiler/CompilationModel/StorageWrites.lean +++ b/Compiler/CompilationModel/StorageWrites.lean @@ -72,7 +72,8 @@ def compileCompatPackedStorageWrites (writeSlots : List YulExpr) (valueExpr : Yu ] def compileSetStorage (fields : List Field) (dynamicSource : DynamicDataSource) - (field : String) (value : Expr) (requireAddressField : Bool := false) : + (field : String) (value : Expr) (requireAddressField : Bool := false) + (internalFunctions : List FunctionSpec := []) : Except String (List YulStmt) := do if isMapping fields field then throw s!"Compilation error: field '{field}' is a mapping; use setMapping, setMappingWord, or setMappingPackedWord" @@ -85,7 +86,7 @@ def compileSetStorage (fields : List Field) (dynamicSource : DynamicDataSource) | _ => throw s!"Compilation error: field '{field}' is not address-typed; use Stmt.setStorage instead" let slots := slot :: f.aliasSlots - let valueExpr ← compileExpr fields dynamicSource value + let valueExpr ← compileExprWithInternals fields dynamicSource internalFunctions value let storedValueExpr := if requireAddressField then YulExpr.call "and" [valueExpr, YulExpr.hex Compiler.Constants.addressMask] @@ -120,9 +121,10 @@ def compileSetStorage (fields : List Field) (dynamicSource : DynamicDataSource) | none => throw s!"Compilation error: unknown storage field '{field}' in setStorage" def compileStorageArrayPush (fields : List Field) (dynamicSource : DynamicDataSource) - (field : String) (value : Expr) : Except String (List YulStmt) := do + (field : String) (value : Expr) (internalFunctions : List FunctionSpec := []) : + Except String (List YulStmt) := do let (slot, _) ← validateDynamicArrayField fields field - let valueExpr ← compileExpr fields dynamicSource value + let valueExpr ← compileExprWithInternals fields dynamicSource internalFunctions value pure [ YulStmt.block [ YulStmt.let_ "__array_len" (YulExpr.call "sload" [YulExpr.lit slot]), @@ -159,10 +161,11 @@ def compileStorageArrayPop (fields : List Field) (field : String) : Except Strin ] def compileSetStorageArrayElement (fields : List Field) (dynamicSource : DynamicDataSource) - (field : String) (index value : Expr) : Except String (List YulStmt) := do + (field : String) (index value : Expr) (internalFunctions : List FunctionSpec := []) : + Except String (List YulStmt) := do let (slot, _) ← validateDynamicArrayField fields field - let indexExpr ← compileExpr fields dynamicSource index - let valueExpr ← compileExpr fields dynamicSource value + let indexExpr ← compileExprWithInternals fields dynamicSource internalFunctions index + let valueExpr ← compileExprWithInternals fields dynamicSource internalFunctions value pure [ YulStmt.block [ YulStmt.let_ "__array_len" (YulExpr.call "sload" [YulExpr.lit slot]), @@ -182,15 +185,16 @@ def compileSetStorageArrayElement (fields : List Field) (dynamicSource : Dynamic ] def compileSetMapping2 (fields : List Field) (dynamicSource : DynamicDataSource) - (field : String) (key1 key2 value : Expr) : Except String (List YulStmt) := do + (field : String) (key1 key2 value : Expr) (internalFunctions : List FunctionSpec := []) : + Except String (List YulStmt) := do if !isMapping2 fields field then throw s!"Compilation error: field '{field}' is not a double mapping" else match findFieldWriteSlots fields field with | some slots => do - let key1Expr ← compileExpr fields dynamicSource key1 - let key2Expr ← compileExpr fields dynamicSource key2 - let valueExpr ← compileExpr fields dynamicSource value + let key1Expr ← compileExprWithInternals fields dynamicSource internalFunctions key1 + let key2Expr ← compileExprWithInternals fields dynamicSource internalFunctions key2 + let valueExpr ← compileExprWithInternals fields dynamicSource internalFunctions value match slots with | [] => throw s!"Compilation error: internal invariant failure: no write slots for mapping field '{field}' in setMapping2" @@ -218,16 +222,17 @@ def compileSetMapping2 (fields : List Field) (dynamicSource : DynamicDataSource) | none => throw s!"Compilation error: unknown mapping field '{field}' in setMapping2" def compileSetMapping2Word (fields : List Field) (dynamicSource : DynamicDataSource) - (field : String) (key1 key2 : Expr) (wordOffset : Nat) (value : Expr) : + (field : String) (key1 key2 : Expr) (wordOffset : Nat) (value : Expr) + (internalFunctions : List FunctionSpec := []) : Except String (List YulStmt) := do if !isMapping2 fields field then throw s!"Compilation error: field '{field}' is not a double mapping" else match findFieldWriteSlots fields field with | some slots => do - let key1Expr ← compileExpr fields dynamicSource key1 - let key2Expr ← compileExpr fields dynamicSource key2 - let valueExpr ← compileExpr fields dynamicSource value + let key1Expr ← compileExprWithInternals fields dynamicSource internalFunctions key1 + let key2Expr ← compileExprWithInternals fields dynamicSource internalFunctions key2 + let valueExpr ← compileExprWithInternals fields dynamicSource internalFunctions value match slots with | [] => throw s!"Compilation error: internal invariant failure: no write slots for mapping field '{field}' in setMapping2Word" @@ -250,14 +255,15 @@ def compileSetMapping2Word (fields : List Field) (dynamicSource : DynamicDataSou | none => throw s!"Compilation error: unknown mapping field '{field}' in setMapping2Word" def compileSetMappingChain (fields : List Field) (dynamicSource : DynamicDataSource) - (field : String) (keys : List Expr) (value : Expr) : Except String (List YulStmt) := do + (field : String) (keys : List Expr) (value : Expr) + (internalFunctions : List FunctionSpec := []) : Except String (List YulStmt) := do if !isMapping fields field then throw s!"Compilation error: field '{field}' is not a mapping" else match findFieldWriteSlots fields field with | some slots => do - let keyExprs ← compileExprList fields dynamicSource keys - let valueExpr ← compileExpr fields dynamicSource value + let keyExprs ← compileExprListWithInternals fields dynamicSource internalFunctions keys + let valueExpr ← compileExprWithInternals fields dynamicSource internalFunctions value let writeAt (slot : Nat) (keysRef : List YulExpr) (valueRef : YulExpr) : YulStmt := YulStmt.expr (YulExpr.call "sstore" [ keysRef.foldl (fun slotExpr keyExpr => YulExpr.call "mappingSlot" [slotExpr, keyExpr]) (YulExpr.lit slot), @@ -281,7 +287,8 @@ def compileSetMappingChain (fields : List Field) (dynamicSource : DynamicDataSou | none => throw s!"Compilation error: unknown mapping field '{field}' in setMappingChain" def compileSetStructMember (fields : List Field) (dynamicSource : DynamicDataSource) - (field : String) (key : Expr) (memberName : String) (value : Expr) : + (field : String) (key : Expr) (memberName : String) (value : Expr) + (internalFunctions : List FunctionSpec := []) : Except String (List YulStmt) := do if isMapping2 fields field then throw s!"Compilation error: field '{field}' is a double mapping; use Stmt.setStructMember2 instead of Stmt.setStructMember" @@ -294,20 +301,21 @@ def compileSetStructMember (fields : List Field) (dynamicSource : DynamicDataSou match member.packed with | none => compileMappingSlotWrite fields field - (← compileExpr fields dynamicSource key) - (← compileExpr fields dynamicSource value) + (← compileExprWithInternals fields dynamicSource internalFunctions key) + (← compileExprWithInternals fields dynamicSource internalFunctions value) s!"setStructMember.{memberName}" member.wordOffset | some packed => compileMappingPackedSlotWrite fields field - (← compileExpr fields dynamicSource key) - (← compileExpr fields dynamicSource value) + (← compileExprWithInternals fields dynamicSource internalFunctions key) + (← compileExprWithInternals fields dynamicSource internalFunctions value) member.wordOffset packed s!"setStructMember.{memberName}" def compileSetStructMember2 (fields : List Field) (dynamicSource : DynamicDataSource) - (field : String) (key1 key2 : Expr) (memberName : String) (value : Expr) : + (field : String) (key1 key2 : Expr) (memberName : String) (value : Expr) + (internalFunctions : List FunctionSpec := []) : Except String (List YulStmt) := do if !isMapping2 fields field then throw s!"Compilation error: field '{field}' is not a double mapping; use Stmt.setStructMember instead of Stmt.setStructMember2" @@ -320,9 +328,9 @@ def compileSetStructMember2 (fields : List Field) (dynamicSource : DynamicDataSo | some member => match findFieldWriteSlots fields field with | some slots => do - let key1Expr ← compileExpr fields dynamicSource key1 - let key2Expr ← compileExpr fields dynamicSource key2 - let valueExpr ← compileExpr fields dynamicSource value + let key1Expr ← compileExprWithInternals fields dynamicSource internalFunctions key1 + let key2Expr ← compileExprWithInternals fields dynamicSource internalFunctions key2 + let valueExpr ← compileExprWithInternals fields dynamicSource internalFunctions value match slots with | [] => throw s!"Compilation error: internal invariant failure: no write slots for mapping field '{field}' in setStructMember2.{memberName}" diff --git a/Compiler/CompilationModel/ValidationCalls.lean b/Compiler/CompilationModel/ValidationCalls.lean index ed5bc3db1..48c3b65d3 100644 --- a/Compiler/CompilationModel/ValidationCalls.lean +++ b/Compiler/CompilationModel/ValidationCalls.lean @@ -159,16 +159,43 @@ def findInternalFunctionByName (functions : List FunctionSpec) def directForwardedInternalArgName? : Expr → Option String | Expr.param name => some name - | Expr.localVar name => some name | _ => none +def internalParamTypeAndLayoutMatches (sourceTy expectedTy : ParamType) : Bool := + sourceTy == expectedTy && + internalCallYulArgNamesForParam "__arg" { name := "__arg", ty := sourceTy } == + internalCallYulArgNamesForParam "__arg" { name := "__arg", ty := expectedTy } + +partial def dynamicMemberTypeAtWordOffset : ParamType → Nat → Option ParamType + | ParamType.tuple elemTys, wordOffset => + let rec goTuple : List ParamType → Nat → Option ParamType + | [], _ => none + | elemTy :: rest, cursor => + if cursor == wordOffset then + some elemTy + else + goTuple rest (cursor + paramParentHeadWords elemTy) + goTuple elemTys 0 + | ParamType.fixedArray elemTy n, wordOffset => + let rec goArray : Nat → Nat → Option ParamType + | 0, _ => none + | count + 1, cursor => + if cursor == wordOffset then + some elemTy + else + goArray count (cursor + paramParentHeadWords elemTy) + goArray n 0 + | ParamType.newtypeOf _ baseTy, wordOffset => + dynamicMemberTypeAtWordOffset baseTy wordOffset + | _, _ => none + def validateInternalCallArgForParam (callerParams : List Param) (callerName calleeName : String) (param : Param) (arg : Expr) : Except String Unit := do if isExpandedInternalParamType param.ty then match directForwardedInternalArgName? arg with | none => - throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with a computed argument for expanded parameter '{param.name}' ({repr param.ty}); issue #1889 currently supports direct parameter/local forwarding only." + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with a computed argument for expanded parameter '{param.name}' ({repr param.ty}); issue #1889 currently supports direct parameter forwarding only." | some _ => pure () else pure () @@ -177,24 +204,100 @@ def validateInternalCallArgForParam | Expr.param sourceName => match findParamType callerParams sourceName with | some sourceTy => - if sourceTy == param.ty then + if internalParamTypeAndLayoutMatches sourceTy param.ty then pure () else - throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with parameter '{sourceName}' of type {repr sourceTy}, expected {repr param.ty} for expanded callee parameter '{param.name}' (issue #1889)." + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with parameter '{sourceName}' of type/layout {repr sourceTy}, expected {repr param.ty} for expanded callee parameter '{param.name}' (issue #1889)." | none => pure () | _ => pure () else pure () +def expandedExprParamNames? : List Expr → Option (List String) + | [] => some [] + | Expr.param name :: rest => + match expandedExprParamNames? rest with + | some names => some (name :: names) + | none => none + | _ => none + +def expandedProjectionType? + (callerParams : List Param) : List Expr → Option ParamType + | [ Expr.paramDynamicMemberDataOffset name wordOffset + , Expr.paramDynamicMemberLength lengthName lengthWordOffset ] => + if name == lengthName && wordOffset == lengthWordOffset then + match findParamType callerParams name with + | some sourceTy => dynamicMemberTypeAtWordOffset sourceTy wordOffset + | none => none + else + none + | [ Expr.arrayElementDynamicMemberDataOffset name index wordOffset + , Expr.arrayElementDynamicMemberLength lengthName lengthIndex lengthWordOffset ] => + let sameIndex := + match index, lengthIndex with + | Expr.param lhs, Expr.param rhs => lhs == rhs + | Expr.localVar lhs, Expr.localVar rhs => lhs == rhs + | Expr.literal lhs, Expr.literal rhs => lhs == rhs + | _, _ => false + if name == lengthName && sameIndex && wordOffset == lengthWordOffset then + match findParamType callerParams name with + | some (ParamType.array elemTy) => dynamicMemberTypeAtWordOffset elemTy wordOffset + | some (ParamType.newtypeOf _ (ParamType.array elemTy)) => + dynamicMemberTypeAtWordOffset elemTy wordOffset + | _ => none + else + none + | _ => none + +def expandedArgsMatchCallerParam + (param : Param) (argNames : List String) (source : Param) : Bool := + internalParamTypeAndLayoutMatches source.ty param.ty && + argNames == internalCallYulArgNamesForParam source.name param + +def validateExpandedInternalCallArgNames + (callerParams : List Param) (callerName calleeName : String) (param : Param) (args : List Expr) : + Except String Unit := do + let expectedNames := internalFunctionYulParamNames [param] + if args.length != expectedNames.length then + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with {args.length} expanded arg(s) for parameter '{param.name}', expected {expectedNames.length} ({issue625Ref}, issue #1889)." + else + match expandedExprParamNames? args with + | some argNames => + if callerParams.any (expandedArgsMatchCallerParam param argNames) then + pure () + else + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with expanded args {repr argNames} for parameter '{param.name}', but no caller parameter has exact type/layout {repr param.ty} and matching generated names (issue #1889)." + | none => + match expandedProjectionType? callerParams args with + | some sourceTy => + if internalParamTypeAndLayoutMatches sourceTy param.ty then + pure () + else + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with projected expanded args of type/layout {repr sourceTy}, expected {repr param.ty} for parameter '{param.name}' (issue #1889)." + | none => + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with non-parameter expanded args for parameter '{param.name}' without a checked projection proving exact type/layout forwarding (issue #1889)." + +def validateExpandedInternalCallArgs + (callerParams : List Param) (callerName calleeName : String) : List Param → List Expr → Except String Unit + | [], [] => pure () + | param :: params, args => do + let expectedCount := (internalFunctionYulParamNames [param]).length + let head := args.take expectedCount + let tail := args.drop expectedCount + validateExpandedInternalCallArgNames callerParams callerName calleeName param head + validateExpandedInternalCallArgs callerParams callerName calleeName params tail + | [], _ :: _ => + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with extra expanded arg(s) after exact type/layout validation ({issue625Ref}, issue #1889)." + def validateInternalCallSourceArgs (callerParams : List Param) (callerName calleeName : String) (params : List Param) (args : List Expr) : Except String Unit := do - let legacyArgCount := + let expandedArgCount := params.foldl (fun acc param => acc + (internalFunctionYulParamNames [param]).length) 0 - if args.length == legacyArgCount && args.length != params.length then - pure () + if args.length == expandedArgCount && args.length != params.length then + validateExpandedInternalCallArgs callerParams callerName calleeName params args else if args.length != params.length then - throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with {args.length} source arg(s), expected {params.length} (or {legacyArgCount} expanded Yul arg(s) for legacy call sites) ({issue625Ref}, issue #1889)." + throw s!"Compilation error: function '{callerName}' calls internal function '{calleeName}' with {args.length} source arg(s), expected {params.length} (or {expandedArgCount} exact expanded Yul arg(s) for legacy call sites) ({issue625Ref}, issue #1889)." else let rec go : List Param → List Expr → Except String Unit | [], [] => pure () diff --git a/Compiler/CompilationModelFeatureTest.lean b/Compiler/CompilationModelFeatureTest.lean index 9472829db..844ab2f76 100644 --- a/Compiler/CompilationModelFeatureTest.lean +++ b/Compiler/CompilationModelFeatureTest.lean @@ -2101,6 +2101,62 @@ def sourceInternalCallArgsExpandStaticCompositeAndBytes : Bool := | Except.error _ => false | _ => false +def containsText (haystack needle : String) : Bool := + let h := haystack.toList + let n := needle.toList + if n.isEmpty then true + else + let rec startsWithChars : List Char → List Char → Bool + | _, [] => true + | [], _ :: _ => false + | h :: hs, n :: ns => h == n && startsWithChars hs ns + let rec go : List Char → Bool + | [] => false + | chars@(_ :: rest) => startsWithChars chars n || go rest + go h + +def localExpandedForwardingRejected : Bool := + match validateInternalCallSourceArgs + [{ name := "amounts", ty := ParamType.array ParamType.uint256 }] + "caller" "internal_echoAmounts" + [{ name := "amounts", ty := ParamType.array ParamType.uint256 }] + [Expr.localVar "amounts"] with + | Except.ok _ => false + | Except.error msg => containsText msg "direct parameter forwarding only" + +def mismatchedSourceParamTypeRejected : Bool := + match validateInternalCallSourceArgs + [{ name := "flags", ty := ParamType.array ParamType.bool }] + "caller" "internal_echoAmounts" + [{ name := "amounts", ty := ParamType.array ParamType.uint256 }] + [Expr.param "flags"] with + | Except.ok _ => false + | Except.error msg => containsText msg "type/layout" + +def legacyExpandedArgsRequireExactNames : Bool := + match validateInternalCallSourceArgs + [{ name := "amounts", ty := ParamType.array ParamType.uint256 }] + "caller" "internal_echoAmounts" + [{ name := "amounts", ty := ParamType.array ParamType.uint256 }] + [Expr.param "other_data_offset", Expr.param "amounts_length"] with + | Except.ok _ => false + | Except.error msg => containsText msg "no caller parameter has exact type/layout" + +def exprInternalCallArgsUseHelperSignature : Bool := + let helper : FunctionSpec := { + name := "echoLength" + params := [{ name := "payload", ty := ParamType.bytes }] + returnType := some FieldType.uint256 + body := [Stmt.return (Expr.arrayLength "payload")] + isInternal := true + } + match compileExprWithInternals [] .calldata [helper] + (Expr.internalCall "echoLength" [Expr.param "payload"]) with + | Except.ok + (YulExpr.call "internal_echoLength" + [YulExpr.ident "payload_data_offset", YulExpr.ident "payload_length"]) => true + | _ => false + end InternalHelperDynamicArgs def compactAmountsAllocatesMemoryArray : Bool := @@ -5256,6 +5312,14 @@ set_option maxRecDepth 4096 in MacroDynamicArraySmoke.InternalHelperDynamicArgs.helperParamNamesExpandStaticCompositeAndBytes expectTrue "source internal helper call args expand static composite and bytes slots" MacroDynamicArraySmoke.InternalHelperDynamicArgs.sourceInternalCallArgsExpandStaticCompositeAndBytes + expectTrue "expanded internal helper args reject local-variable forwarding" + MacroDynamicArraySmoke.InternalHelperDynamicArgs.localExpandedForwardingRejected + expectTrue "expanded internal helper args reject mismatched source type/layout" + MacroDynamicArraySmoke.InternalHelperDynamicArgs.mismatchedSourceParamTypeRejected + expectTrue "legacy expanded internal helper args require exact generated names" + MacroDynamicArraySmoke.InternalHelperDynamicArgs.legacyExpandedArgsRequireExactNames + expectTrue "expression-position internal helper calls expand args from helper signature" + MacroDynamicArraySmoke.InternalHelperDynamicArgs.exprInternalCallArgsUseHelperSignature -- Regression: selector mismatch must fail closed. let mismatchRejected := diff --git a/Compiler/Proofs/IRGeneration/ContractFeatureTest.lean b/Compiler/Proofs/IRGeneration/ContractFeatureTest.lean index 8ce2f62c5..92cdfb104 100644 --- a/Compiler/Proofs/IRGeneration/ContractFeatureTest.lean +++ b/Compiler/Proofs/IRGeneration/ContractFeatureTest.lean @@ -532,7 +532,7 @@ private theorem constructorOnly_compileBody : | .error _ => [], ?_⟩ simp [constructorOnlySpec, constructorOnlyCtor, constructorOnlyOwnerField, CompilationModel.compileStmtList, CompilationModel.compileStmt, - CompilationModel.compileSetStorage, CompilationModel.compileExpr, + CompilationModel.compileSetStorage, CompilationModel.compileExprWithInternals, CompilationModel.isMapping, constructorOnly_owner_resolved_lit, Bind.bind, Except.bind, Pure.pure, Except.pure] @@ -865,7 +865,7 @@ example : | .error _ => []) := by simp [constructorOnlySpec, constructorOnlyCtor, constructorOnlyOwnerField, CompilationModel.compileStmtList, CompilationModel.compileStmt, - CompilationModel.compileSetStorage, CompilationModel.compileExpr, + CompilationModel.compileSetStorage, CompilationModel.compileExprWithInternals, CompilationModel.isMapping, constructorOnly_owner_resolved_lit, Bind.bind, Except.bind, Pure.pure, Except.pure] have hbind : @@ -935,7 +935,7 @@ example : Except.ok bodyStmts := by simp [bodyStmts, constructorOnlySpec, constructorOnlyCtor, constructorOnlyOwnerField, CompilationModel.compileStmtList, CompilationModel.compileStmt, - CompilationModel.compileSetStorage, CompilationModel.compileExpr, + CompilationModel.compileSetStorage, CompilationModel.compileExprWithInternals, CompilationModel.isMapping, constructorOnly_owner_resolved_lit, Bind.bind, Except.bind, Pure.pure, Except.pure] have hbind : diff --git a/Compiler/Proofs/IRGeneration/Function.lean b/Compiler/Proofs/IRGeneration/Function.lean index 0818ebc49..8fcfa0c89 100644 --- a/Compiler/Proofs/IRGeneration/Function.lean +++ b/Compiler/Proofs/IRGeneration/Function.lean @@ -2685,12 +2685,13 @@ private theorem compileExpr_constructor_mode_eq ∀ {expr : Expr}, exprTouchesUnsupportedCoreSurface expr = false → exprTouchesUnsupportedConstructorRawCalldataSurface expr = false → - compileExpr fields .memory expr = compileExpr fields .calldata expr - | .literal _, _, _ => by simp [compileExpr] - | .param _, _, _ => by simp [compileExpr] + compileExprWithInternals fields .memory [] expr = + compileExprWithInternals fields .calldata [] expr + | .literal _, _, _ => by simp [compileExprWithInternals] + | .param _, _, _ => by simp [compileExprWithInternals] | .constructorArg _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore - | .storage _, _, _ => by simp [compileExpr] - | .storageAddr _, _, _ => by simp [compileExpr] + | .storage _, _, _ => by simp [compileExprWithInternals] + | .storageAddr _, _, _ => by simp [compileExprWithInternals] | .mapping _ _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .mappingWord _ _ _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .mappingPackedWord _ _ _ _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore @@ -2700,22 +2701,22 @@ private theorem compileExpr_constructor_mode_eq | .mappingChain _ _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .structMember _ _ _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .structMember2 _ _ _ _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore - | .caller, _, _ => by simp [compileExpr] - | .contractAddress, _, _ => by simp [compileExpr] - | .txOrigin, _, _ => by simp [compileExpr] - | .chainid, _, _ => by simp [compileExpr] - | .msgValue, _, _ => by simp [compileExpr] - | .blockTimestamp, _, _ => by simp [compileExpr] - | .blockNumber, _, _ => by simp [compileExpr] - | .blobbasefee, _, _ => by simp [compileExpr] + | .caller, _, _ => by simp [compileExprWithInternals] + | .contractAddress, _, _ => by simp [compileExprWithInternals] + | .txOrigin, _, _ => by simp [compileExprWithInternals] + | .chainid, _, _ => by simp [compileExprWithInternals] + | .msgValue, _, _ => by simp [compileExprWithInternals] + | .blockTimestamp, _, _ => by simp [compileExprWithInternals] + | .blockNumber, _, _ => by simp [compileExprWithInternals] + | .blobbasefee, _, _ => by simp [compileExprWithInternals] | .mload _, hcore, hraw => by simp only [exprTouchesUnsupportedCoreSurface] at hcore simp only [exprTouchesUnsupportedConstructorRawCalldataSurface] at hraw - simp [compileExpr, compileExpr_constructor_mode_eq hcore hraw] + simp [compileExprWithInternals, compileExpr_constructor_mode_eq hcore hraw] | .tload _, hcore, hraw => by simp only [exprTouchesUnsupportedCoreSurface] at hcore simp only [exprTouchesUnsupportedConstructorRawCalldataSurface] at hraw - simp [compileExpr, compileExpr_constructor_mode_eq hcore hraw] + simp [compileExprWithInternals, compileExpr_constructor_mode_eq hcore hraw] | .keccak256 _ _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .call .., hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .staticcall .., hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore @@ -2725,7 +2726,7 @@ private theorem compileExpr_constructor_mode_eq | .returndataSize, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .extcodesize _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .returndataOptionalBoolAt _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore - | .localVar _, _, _ => by simp [compileExpr] + | .localVar _, _, _ => by simp [compileExprWithInternals] | .externalCall _ _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .internalCall _ _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore | .arrayLength _, hcore, _ => by simp [exprTouchesUnsupportedCoreSurface] at hcore @@ -2767,13 +2768,13 @@ private theorem compileExpr_constructor_mode_eq Bool.or_eq_false_iff] at hraw rcases hcore with ⟨hcoreA, hcoreB⟩ rcases hraw with ⟨hrawA, hrawB⟩ - simp [compileExpr, compileExpr_constructor_mode_eq hcoreA hrawA, + simp [compileExprWithInternals, compileExpr_constructor_mode_eq hcoreA hrawA, compileExpr_constructor_mode_eq hcoreB hrawB] | .bitNot a, hcore, hraw | .logicalNot a, hcore, hraw => by simp only [exprTouchesUnsupportedCoreSurface] at hcore simp only [exprTouchesUnsupportedConstructorRawCalldataSurface] at hraw - simp [compileExpr, compileExpr_constructor_mode_eq hcore hraw] + simp [compileExprWithInternals, compileExpr_constructor_mode_eq hcore hraw] | .mulDivDown a b c, hcore, hraw | .mulDivUp a b c, hcore, hraw | .ite a b c, hcore, hraw => by @@ -2783,7 +2784,7 @@ private theorem compileExpr_constructor_mode_eq Bool.or_eq_false_iff, Bool.or_assoc] at hraw rcases hcore with ⟨hcoreA, hcoreB, hcoreC⟩ rcases hraw with ⟨hrawA, hrawB, hrawC⟩ - simp [compileExpr, compileExpr_constructor_mode_eq hcoreA hrawA, + simp [compileExprWithInternals, compileExpr_constructor_mode_eq hcoreA hrawA, compileExpr_constructor_mode_eq hcoreB hrawB, compileExpr_constructor_mode_eq hcoreC hrawC] @@ -2792,8 +2793,9 @@ private theorem compileExprList_constructor_mode_eq ∀ {exprs : List Expr}, exprs.all (fun expr => exprTouchesUnsupportedCoreSurface expr == false) = true → exprListTouchesUnsupportedConstructorRawCalldataSurface exprs = false → - compileExprList fields .memory exprs = compileExprList fields .calldata exprs - | [], _, _ => by simp [compileExprList] + compileExprListWithInternals fields .memory [] exprs = + compileExprListWithInternals fields .calldata [] exprs + | [], _, _ => by simp [compileExprListWithInternals, pure, Except.pure] | expr :: rest, hcore, hraw => by simp only [List.all_cons, Bool.and_eq_true, Bool.beq_eq_decide_eq, decide_eq_true_eq] at hcore @@ -2801,28 +2803,33 @@ private theorem compileExprList_constructor_mode_eq Bool.or_eq_false_iff] at hraw rcases hcore with ⟨hcoreHead, hcoreTail⟩ rcases hraw with ⟨hrawHead, hrawTail⟩ - simp [compileExprList, compileExpr_constructor_mode_eq hcoreHead hrawHead, - compileExprList_constructor_mode_eq hcoreTail hrawTail] + simp [compileExprListWithInternals, + compileExpr_constructor_mode_eq hcoreHead hrawHead, + compileExprList_constructor_mode_eq hcoreTail hrawTail, + Bind.bind, Except.bind, Functor.map, Except.map] private theorem compileRequireFailCond_constructor_mode_eq {fields : List Field} {cond : Expr} (hcoreClosed : exprTouchesUnsupportedCoreSurface cond = false) (hrawClosed : exprTouchesUnsupportedConstructorRawCalldataSurface cond = false) : - compileRequireFailCond fields .memory cond = - compileRequireFailCond fields .calldata cond := by + compileRequireFailCondWithInternals fields .memory [] cond = + compileRequireFailCondWithInternals fields .calldata [] cond := by cases cond <;> - try simp_all [compileRequireFailCond, compileExpr_constructor_mode_eq] + try simp_all [compileRequireFailCondWithInternals, + compileExpr_constructor_mode_eq] · simp only [exprTouchesUnsupportedCoreSurface, Bool.or_eq_false_iff] at hcoreClosed simp only [exprTouchesUnsupportedConstructorRawCalldataSurface, Bool.or_eq_false_iff] at hrawClosed simp [ + compileRequireFailCondWithInternals, compileExpr_constructor_mode_eq hcoreClosed.1 hrawClosed.1, compileExpr_constructor_mode_eq hcoreClosed.2 hrawClosed.2] · simp only [exprTouchesUnsupportedCoreSurface, Bool.or_eq_false_iff] at hcoreClosed simp only [exprTouchesUnsupportedConstructorRawCalldataSurface, Bool.or_eq_false_iff] at hrawClosed simp [ + compileRequireFailCondWithInternals, compileExpr_constructor_mode_eq hcoreClosed.1 hrawClosed.1, compileExpr_constructor_mode_eq hcoreClosed.2 hrawClosed.2] @@ -2848,7 +2855,8 @@ private theorem compileStmt_constructor_mode_eq compileSetStorageArrayElement, compileSetMapping2, compileSetMapping2Word, compileSetMappingChain, compileSetStructMember, compileSetStructMember2, compileRequireFailCond_constructor_mode_eq, compileExpr_constructor_mode_eq, - compileExprList_constructor_mode_eq, compileStmtList_constructor_mode_eq'] + compileExprList_constructor_mode_eq, compileStmtList_constructor_mode_eq', + compileExprWithInternals, compileExprListWithInternals] private theorem compileStmtList_constructor_mode_eq' {fields : List Field} diff --git a/Compiler/Proofs/IRGeneration/FunctionBody/Base.lean b/Compiler/Proofs/IRGeneration/FunctionBody/Base.lean index eb09b2030..a5a92a045 100644 --- a/Compiler/Proofs/IRGeneration/FunctionBody/Base.lean +++ b/Compiler/Proofs/IRGeneration/FunctionBody/Base.lean @@ -341,7 +341,7 @@ theorem eval_compileExpr_caller (hmatch : runtimeStateMatchesIR fields runtime state) : evalIRExpr state (CompilationModel.compileExpr fields .calldata .caller |>.toOption.getD (YulExpr.lit 0)) = some (SourceSemantics.evalExpr fields runtime (.caller)) := by - simp [CompilationModel.compileExpr] + simp [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] exact evalIRExpr_caller_of_runtimeStateMatchesIR hmatch theorem eval_compileExpr_contractAddress @@ -351,7 +351,7 @@ theorem eval_compileExpr_contractAddress (hmatch : runtimeStateMatchesIR fields runtime state) : evalIRExpr state (CompilationModel.compileExpr fields .calldata .contractAddress |>.toOption.getD (YulExpr.lit 0)) = some (SourceSemantics.evalExpr fields runtime (.contractAddress)) := by - simp [CompilationModel.compileExpr] + simp [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] exact evalIRExpr_contractAddress_of_runtimeStateMatchesIR hmatch theorem eval_compileExpr_msgValue @@ -361,7 +361,7 @@ theorem eval_compileExpr_msgValue (hmatch : runtimeStateMatchesIR fields runtime state) : evalIRExpr state (CompilationModel.compileExpr fields .calldata .msgValue |>.toOption.getD (YulExpr.lit 0)) = some (SourceSemantics.evalExpr fields runtime (.msgValue)) := by - simp [CompilationModel.compileExpr] + simp [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] exact evalIRExpr_msgValue_of_runtimeStateMatchesIR hmatch theorem eval_compileExpr_blockTimestamp @@ -371,7 +371,7 @@ theorem eval_compileExpr_blockTimestamp (hmatch : runtimeStateMatchesIR fields runtime state) : evalIRExpr state (CompilationModel.compileExpr fields .calldata .blockTimestamp |>.toOption.getD (YulExpr.lit 0)) = some (SourceSemantics.evalExpr fields runtime (.blockTimestamp)) := by - simp [CompilationModel.compileExpr] + simp [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] exact evalIRExpr_blockTimestamp_of_runtimeStateMatchesIR hmatch theorem eval_compileExpr_blockNumber @@ -381,7 +381,7 @@ theorem eval_compileExpr_blockNumber (hmatch : runtimeStateMatchesIR fields runtime state) : evalIRExpr state (CompilationModel.compileExpr fields .calldata .blockNumber |>.toOption.getD (YulExpr.lit 0)) = some (SourceSemantics.evalExpr fields runtime (.blockNumber)) := by - simp [CompilationModel.compileExpr] + simp [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] exact evalIRExpr_blockNumber_of_runtimeStateMatchesIR hmatch theorem eval_compileExpr_chainid @@ -391,7 +391,7 @@ theorem eval_compileExpr_chainid (hmatch : runtimeStateMatchesIR fields runtime state) : evalIRExpr state (CompilationModel.compileExpr fields .calldata .chainid |>.toOption.getD (YulExpr.lit 0)) = some (SourceSemantics.evalExpr fields runtime (.chainid)) := by - simp [CompilationModel.compileExpr] + simp [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] exact evalIRExpr_chainid_of_runtimeStateMatchesIR hmatch theorem eval_compileExpr_blobbasefee @@ -401,7 +401,7 @@ theorem eval_compileExpr_blobbasefee (hmatch : runtimeStateMatchesIR fields runtime state) : evalIRExpr state (CompilationModel.compileExpr fields .calldata .blobbasefee |>.toOption.getD (YulExpr.lit 0)) = some (SourceSemantics.evalExpr fields runtime (.blobbasefee)) := by - simp [CompilationModel.compileExpr] + simp [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] exact evalIRExpr_blobbasefee_of_runtimeStateMatchesIR hmatch theorem eval_compileExpr_txOrigin @@ -411,7 +411,7 @@ theorem eval_compileExpr_txOrigin (hmatch : runtimeStateMatchesIR fields runtime state) : evalIRExpr state (CompilationModel.compileExpr fields .calldata .txOrigin |>.toOption.getD (YulExpr.lit 0)) = some (SourceSemantics.evalExpr fields runtime (.txOrigin)) := by - simp [CompilationModel.compileExpr] + simp [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] exact evalIRExpr_txOrigin_of_runtimeStateMatchesIR hmatch theorem evalIRExpr_calldatasize_of_runtimeStateMatchesIR @@ -441,7 +441,7 @@ theorem eval_compileExpr_calldatasize (hmatch : runtimeStateMatchesIR fields runtime state) : evalIRExpr state (CompilationModel.compileExpr fields .calldata .calldatasize |>.toOption.getD (YulExpr.lit 0)) = some (SourceSemantics.evalExpr fields runtime (.calldatasize)) := by - simp [CompilationModel.compileExpr] + simp [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] exact evalIRExpr_calldatasize_of_runtimeStateMatchesIR hmatch theorem eval_compileExpr_literal @@ -1454,7 +1454,8 @@ private theorem eval_compileExpr_ge_raw have hcompile : (CompilationModel.compileExpr fields .calldata (.ge lhs rhs) |>.toOption.getD (YulExpr.lit 0)) = YulExpr.call "iszero" [YulExpr.call "lt" [lhsIR, rhsIR]] := by - rw [CompilationModel.compileExpr, hlhsCompile, hrhsCompile] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhsCompile hrhsCompile + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhsCompile, hrhsCompile] rfl rw [hcompile] simpa [hlhsSrc, hrhsSrc] using @@ -1499,7 +1500,8 @@ private theorem eval_compileExpr_le_raw have hcompile : (CompilationModel.compileExpr fields .calldata (.le lhs rhs) |>.toOption.getD (YulExpr.lit 0)) = YulExpr.call "iszero" [YulExpr.call "gt" [lhsIR, rhsIR]] := by - rw [CompilationModel.compileExpr, hlhsCompile, hrhsCompile] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhsCompile hrhsCompile + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhsCompile, hrhsCompile] rfl rw [hcompile] simpa [hlhsSrc, hrhsSrc] using @@ -1515,7 +1517,8 @@ theorem compileExpr_eq_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.eq lhs rhs) = Except.ok (YulExpr.call "eq" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_lt_ok @@ -1526,7 +1529,8 @@ theorem compileExpr_lt_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.lt lhs rhs) = Except.ok (YulExpr.call "lt" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_slt_ok @@ -1537,7 +1541,8 @@ theorem compileExpr_slt_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.slt lhs rhs) = Except.ok (YulExpr.call "slt" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_sgt_ok @@ -1548,7 +1553,8 @@ theorem compileExpr_sgt_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.sgt lhs rhs) = Except.ok (YulExpr.call "sgt" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_sdiv_ok @@ -1559,7 +1565,8 @@ theorem compileExpr_sdiv_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.sdiv lhs rhs) = Except.ok (YulExpr.call "sdiv" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_smod_ok @@ -1570,7 +1577,8 @@ theorem compileExpr_smod_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.smod lhs rhs) = Except.ok (YulExpr.call "smod" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_sar_ok @@ -1581,7 +1589,8 @@ theorem compileExpr_sar_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.sar lhs rhs) = Except.ok (YulExpr.call "sar" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_byte_ok @@ -1592,7 +1601,8 @@ theorem compileExpr_byte_ok (hvalue : CompilationModel.compileExpr fields .calldata value = Except.ok valueIR) : CompilationModel.compileExpr fields .calldata (.byte index value) = Except.ok (YulExpr.call "byte" [indexIR, valueIR]) := by - rw [CompilationModel.compileExpr, hindex, hvalue] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hindex hvalue + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hindex, hvalue] rfl theorem compileExpr_signextend_ok @@ -1603,7 +1613,8 @@ theorem compileExpr_signextend_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.signextend lhs rhs) = Except.ok (YulExpr.call "signextend" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_gt_ok @@ -1614,7 +1625,8 @@ theorem compileExpr_gt_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.gt lhs rhs) = Except.ok (YulExpr.call "gt" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_ge_ok @@ -1625,7 +1637,8 @@ theorem compileExpr_ge_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.ge lhs rhs) = Except.ok (YulExpr.call "iszero" [YulExpr.call "lt" [lhsIR, rhsIR]]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_le_ok @@ -1636,7 +1649,8 @@ theorem compileExpr_le_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.le lhs rhs) = Except.ok (YulExpr.call "iszero" [YulExpr.call "gt" [lhsIR, rhsIR]]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_logicalNot_ok @@ -1646,7 +1660,8 @@ theorem compileExpr_logicalNot_ok (hexpr : CompilationModel.compileExpr fields .calldata expr = Except.ok exprIR) : CompilationModel.compileExpr fields .calldata (.logicalNot expr) = Except.ok (YulExpr.call "iszero" [exprIR]) := by - rw [CompilationModel.compileExpr, hexpr] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hexpr] rfl theorem compileExpr_logicalAnd_ok @@ -1658,7 +1673,8 @@ theorem compileExpr_logicalAnd_ok CompilationModel.compileExpr fields .calldata (.logicalAnd lhs rhs) = Except.ok (YulExpr.call "and" [CompilationModel.yulToBool lhsIR, CompilationModel.yulToBool rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_logicalOr_ok @@ -1670,7 +1686,8 @@ theorem compileExpr_logicalOr_ok CompilationModel.compileExpr fields .calldata (.logicalOr lhs rhs) = Except.ok (YulExpr.call "or" [CompilationModel.yulToBool lhsIR, CompilationModel.yulToBool rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_bitAnd_ok @@ -1681,7 +1698,8 @@ theorem compileExpr_bitAnd_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.bitAnd lhs rhs) = Except.ok (YulExpr.call "and" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_bitOr_ok @@ -1692,7 +1710,8 @@ theorem compileExpr_bitOr_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.bitOr lhs rhs) = Except.ok (YulExpr.call "or" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_bitXor_ok @@ -1703,7 +1722,8 @@ theorem compileExpr_bitXor_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.bitXor lhs rhs) = Except.ok (YulExpr.call "xor" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_bitNot_ok @@ -1713,7 +1733,8 @@ theorem compileExpr_bitNot_ok (hexpr : CompilationModel.compileExpr fields .calldata expr = Except.ok exprIR) : CompilationModel.compileExpr fields .calldata (.bitNot expr) = Except.ok (YulExpr.call "not" [exprIR]) := by - rw [CompilationModel.compileExpr, hexpr] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hexpr] rfl theorem compileExpr_shl_ok @@ -1724,7 +1745,8 @@ theorem compileExpr_shl_ok (hvalue : CompilationModel.compileExpr fields .calldata value = Except.ok valueIR) : CompilationModel.compileExpr fields .calldata (.shl shift value) = Except.ok (YulExpr.call "shl" [shiftIR, valueIR]) := by - rw [CompilationModel.compileExpr, hshift, hvalue] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hshift hvalue + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hshift, hvalue] rfl theorem compileExpr_shr_ok @@ -1735,7 +1757,8 @@ theorem compileExpr_shr_ok (hvalue : CompilationModel.compileExpr fields .calldata value = Except.ok valueIR) : CompilationModel.compileExpr fields .calldata (.shr shift value) = Except.ok (YulExpr.call "shr" [shiftIR, valueIR]) := by - rw [CompilationModel.compileExpr, hshift, hvalue] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hshift hvalue + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hshift, hvalue] rfl theorem compileExpr_min_ok @@ -1751,7 +1774,8 @@ theorem compileExpr_min_ok YulExpr.call "gt" [lhsIR, rhsIR] ] ]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_max_ok @@ -1767,7 +1791,8 @@ theorem compileExpr_max_ok YulExpr.call "gt" [rhsIR, lhsIR] ] ]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_wMulDown_ok @@ -1781,7 +1806,8 @@ theorem compileExpr_wMulDown_ok YulExpr.call "mul" [lhsIR, rhsIR], YulExpr.lit 1000000000000000000 ]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_wDivUp_ok @@ -1798,7 +1824,8 @@ theorem compileExpr_wDivUp_ok ], rhsIR ]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_mulDivDown_ok @@ -1810,7 +1837,8 @@ theorem compileExpr_mulDivDown_ok (hc : CompilationModel.compileExpr fields .calldata c = Except.ok cIR) : CompilationModel.compileExpr fields .calldata (.mulDivDown a b c) = Except.ok (YulExpr.call "div" [YulExpr.call "mul" [aIR, bIR], cIR]) := by - rw [CompilationModel.compileExpr, ha, hb, hc] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at ha hb hc + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, ha, hb, hc] rfl theorem compileExpr_mulDivUp_ok @@ -1825,7 +1853,8 @@ theorem compileExpr_mulDivUp_ok YulExpr.call "add" [YulExpr.call "mul" [aIR, bIR], YulExpr.call "sub" [cIR, YulExpr.lit 1]], cIR]) := by - rw [CompilationModel.compileExpr, ha, hb, hc] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at ha hb hc + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, ha, hb, hc] rfl theorem compileExpr_ceilDiv_ok @@ -1842,7 +1871,8 @@ theorem compileExpr_ceilDiv_ok YulExpr.lit 1 ] ]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_ite_ok @@ -1863,7 +1893,8 @@ theorem compileExpr_ite_ok elseIR ] ]) := by - rw [CompilationModel.compileExpr, hcond, hthen, helse] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcond hthen helse + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hcond, hthen, helse] rfl theorem compileExpr_add_ok @@ -1874,7 +1905,8 @@ theorem compileExpr_add_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.add lhs rhs) = Except.ok (YulExpr.call "add" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_sub_ok @@ -1885,7 +1917,8 @@ theorem compileExpr_sub_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.sub lhs rhs) = Except.ok (YulExpr.call "sub" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_mul_ok @@ -1896,7 +1929,8 @@ theorem compileExpr_mul_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.mul lhs rhs) = Except.ok (YulExpr.call "mul" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_div_ok @@ -1907,7 +1941,8 @@ theorem compileExpr_div_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.div lhs rhs) = Except.ok (YulExpr.call "div" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_mod_ok @@ -1918,7 +1953,8 @@ theorem compileExpr_mod_ok (hrhs : CompilationModel.compileExpr fields .calldata rhs = Except.ok rhsIR) : CompilationModel.compileExpr fields .calldata (.mod lhs rhs) = Except.ok (YulExpr.call "mod" [lhsIR, rhsIR]) := by - rw [CompilationModel.compileExpr, hlhs, hrhs] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hlhs, hrhs] rfl theorem compileExpr_mload_ok @@ -1928,7 +1964,8 @@ theorem compileExpr_mload_ok (hexpr : CompilationModel.compileExpr fields .calldata expr = Except.ok exprIR) : CompilationModel.compileExpr fields .calldata (.mload expr) = Except.ok (YulExpr.call "mload" [exprIR]) := by - rw [CompilationModel.compileExpr, hexpr] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hexpr] rfl private theorem eval_compileExpr_mload_of_compiled @@ -1968,7 +2005,8 @@ theorem compileExpr_tload_ok (hexpr : CompilationModel.compileExpr fields .calldata expr = Except.ok exprIR) : CompilationModel.compileExpr fields .calldata (.tload expr) = Except.ok (YulExpr.call "tload" [exprIR]) := by - rw [CompilationModel.compileExpr, hexpr] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hexpr] rfl private theorem calldataloadWord_lt_evmModulus @@ -2010,7 +2048,8 @@ theorem compileExpr_calldataload_ok (hexpr : CompilationModel.compileExpr fields .calldata expr = Except.ok exprIR) : CompilationModel.compileExpr fields .calldata (.calldataload expr) = Except.ok (YulExpr.call "calldataload" [exprIR]) := by - rw [CompilationModel.compileExpr, hexpr] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + rw [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, hexpr] rfl private theorem eval_compileExpr_calldataload_of_compiled @@ -4525,29 +4564,53 @@ theorem compileExpr_core_ok ∃ exprIR, CompilationModel.compileExpr fields .calldata expr = Except.ok exprIR := by induction hcore with | literal value => - exact ⟨YulExpr.lit (value % CompilationModel.uint256Modulus), rfl⟩ + exact ⟨YulExpr.lit (value % CompilationModel.uint256Modulus), by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | param name => - exact ⟨YulExpr.ident name, rfl⟩ + exact ⟨YulExpr.ident name, by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | localVar name => - exact ⟨YulExpr.ident name, rfl⟩ + exact ⟨YulExpr.ident name, by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | caller => - exact ⟨YulExpr.call "caller" [], rfl⟩ + exact ⟨YulExpr.call "caller" [], by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | contractAddress => - exact ⟨YulExpr.call "address" [], rfl⟩ + exact ⟨YulExpr.call "address" [], by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | txOrigin => - exact ⟨YulExpr.call "origin" [], rfl⟩ + exact ⟨YulExpr.call "origin" [], by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | msgValue => - exact ⟨YulExpr.call "callvalue" [], rfl⟩ + exact ⟨YulExpr.call "callvalue" [], by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | blockTimestamp => - exact ⟨YulExpr.call "timestamp" [], rfl⟩ + exact ⟨YulExpr.call "timestamp" [], by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | blockNumber => - exact ⟨YulExpr.call "number" [], rfl⟩ + exact ⟨YulExpr.call "number" [], by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | chainid => - exact ⟨YulExpr.call "chainid" [], rfl⟩ + exact ⟨YulExpr.call "chainid" [], by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | blobbasefee => - exact ⟨YulExpr.call "blobbasefee" [], rfl⟩ + exact ⟨YulExpr.call "blobbasefee" [], by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | calldatasize => - exact ⟨YulExpr.call "calldatasize" [], rfl⟩ + exact ⟨YulExpr.call "calldatasize" [], by + unfold CompilationModel.compileExpr CompilationModel.compileExprWithInternals + rfl⟩ | add hL hR ihL ihR => rename_i lhs rhs rcases ihL with ⟨lhsIR, hlhs⟩ @@ -4781,12 +4844,12 @@ theorem eval_compileExpr_core_onExpr some (SourceSemantics.evalExpr fields runtime expr) := by induction hcore generalizing runtime state with | literal value => - simpa [CompilationModel.compileExpr] using eval_compileExpr_literal fields runtime state value + simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] using eval_compileExpr_literal fields runtime state value | param name => - simpa [CompilationModel.compileExpr] using + simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] using eval_compileExpr_param_of_expr_bindings name hexact hpresent | localVar name => - simpa [CompilationModel.compileExpr] using + simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals] using eval_compileExpr_localVar_of_expr_bindings name hexact hpresent | caller => exact eval_compileExpr_caller hruntime @@ -6343,307 +6406,299 @@ theorem compileRequireFailCond_core_ok CompilationModel.compileRequireFailCond fields .calldata cond = Except.ok failCond := by cases hcore with | literal value => - exact ⟨YulExpr.call "iszero" [YulExpr.lit (value % CompilationModel.uint256Modulus)], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.lit (value % CompilationModel.uint256Modulus)], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | param name => - exact ⟨YulExpr.call "iszero" [YulExpr.ident name], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.ident name], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | localVar name => - exact ⟨YulExpr.call "iszero" [YulExpr.ident name], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.ident name], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | caller => - exact ⟨YulExpr.call "iszero" [YulExpr.call "caller" []], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.call "caller" []], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | contractAddress => - exact ⟨YulExpr.call "iszero" [YulExpr.call "address" []], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.call "address" []], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | txOrigin => - exact ⟨YulExpr.call "iszero" [YulExpr.call "origin" []], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.call "origin" []], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | msgValue => - exact ⟨YulExpr.call "iszero" [YulExpr.call "callvalue" []], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.call "callvalue" []], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | blockTimestamp => - exact ⟨YulExpr.call "iszero" [YulExpr.call "timestamp" []], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.call "timestamp" []], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | blockNumber => - exact ⟨YulExpr.call "iszero" [YulExpr.call "number" []], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.call "number" []], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | chainid => - exact ⟨YulExpr.call "iszero" [YulExpr.call "chainid" []], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.call "chainid" []], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | blobbasefee => - exact ⟨YulExpr.call "iszero" [YulExpr.call "blobbasefee" []], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.call "blobbasefee" []], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | calldatasize => - exact ⟨YulExpr.call "iszero" [YulExpr.call "calldatasize" []], rfl⟩ + exact ⟨YulExpr.call "iszero" [YulExpr.call "calldatasize" []], by + unfold CompilationModel.compileRequireFailCond CompilationModel.compileRequireFailCondWithInternals + unfold CompilationModel.compileExprWithInternals + rfl⟩ | add hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "add" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_add_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_add_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | sub hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "sub" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_sub_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_sub_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | mul hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "mul" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_mul_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_mul_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | div hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "div" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_div_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_div_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | mod hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "mod" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_mod_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_mod_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | eq hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "eq" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_eq_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_eq_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | lt hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "lt" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_lt_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_lt_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | slt hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "slt" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_slt_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_slt_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | sgt hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "sgt" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_sgt_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_sgt_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | sdiv hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "sdiv" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_sdiv_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_sdiv_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | smod hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "smod" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_smod_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_smod_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | sar hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "sar" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_sar_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_sar_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | byte hL hR => rename_i index value rcases compileExpr_core_ok (fields := fields) hL with ⟨indexIR, hindex⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨valueIR, hvalue⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "byte" [indexIR, valueIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_byte_ok hindex hvalue] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_byte_ok hindex hvalue + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | signextend hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "signextend" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_signextend_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_signextend_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | gt hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "gt" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_gt_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_gt_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | ge hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "lt" [lhsIR, rhsIR], by - rw [CompilationModel.compileRequireFailCond, hlhs, hrhs] + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + simp [CompilationModel.compileRequireFailCondWithInternals, CompilationModel.yulBinOp, hlhs, hrhs] rfl⟩ | le hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "gt" [lhsIR, rhsIR], by - rw [CompilationModel.compileRequireFailCond, hlhs, hrhs] + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + simp [CompilationModel.compileRequireFailCondWithInternals, CompilationModel.yulBinOp, hlhs, hrhs] rfl⟩ | logicalNot h => rename_i expr rcases compileExpr_core_ok (fields := fields) h with ⟨exprIR, hexpr⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "iszero" [exprIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_logicalNot_ok hexpr] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_logicalNot_ok hexpr + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | logicalAnd hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "and" [CompilationModel.yulToBool lhsIR, CompilationModel.yulToBool rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_logicalAnd_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_logicalAnd_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | logicalOr hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "or" [CompilationModel.yulToBool lhsIR, CompilationModel.yulToBool rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_logicalOr_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_logicalOr_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | bitAnd hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "and" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_bitAnd_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_bitAnd_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | bitOr hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "or" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_bitOr_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_bitOr_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | bitXor hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "xor" [lhsIR, rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_bitXor_ok hlhs hrhs] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_bitXor_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | bitNot h => rename_i expr rcases compileExpr_core_ok (fields := fields) h with ⟨exprIR, hexpr⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "not" [exprIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_bitNot_ok hexpr] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_bitNot_ok hexpr + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | shl hS hV => rename_i shift value rcases compileExpr_core_ok (fields := fields) hS with ⟨shiftIR, hshift⟩ rcases compileExpr_core_ok (fields := fields) hV with ⟨valueIR, hvalue⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "shl" [shiftIR, valueIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_shl_ok hshift hvalue] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_shl_ok hshift hvalue + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | shr hS hV => rename_i shift value rcases compileExpr_core_ok (fields := fields) hS with ⟨shiftIR, hshift⟩ rcases compileExpr_core_ok (fields := fields) hV with ⟨valueIR, hvalue⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "shr" [shiftIR, valueIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_shr_ok hshift hvalue] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_shr_ok hshift hvalue + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | min hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ @@ -6651,8 +6706,10 @@ theorem compileRequireFailCond_core_ok exact ⟨YulExpr.call "iszero" [YulExpr.call "sub" [lhsIR, YulExpr.call "mul" [YulExpr.call "sub" [lhsIR, rhsIR], YulExpr.call "gt" [lhsIR, rhsIR]]]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_min_ok hlhs hrhs] - all_goals first | rfl | (intro a b; exact nofun)⟩ + have hcompile := compileExpr_min_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | max hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ @@ -6660,8 +6717,10 @@ theorem compileRequireFailCond_core_ok exact ⟨YulExpr.call "iszero" [YulExpr.call "add" [lhsIR, YulExpr.call "mul" [YulExpr.call "sub" [rhsIR, lhsIR], YulExpr.call "gt" [rhsIR, lhsIR]]]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_max_ok hlhs hrhs] - all_goals first | rfl | (intro a b; exact nofun)⟩ + have hcompile := compileExpr_max_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | ite hC hT hE => rename_i cond thenVal elseVal rcases compileExpr_core_ok (fields := fields) hC with ⟨condIR, hcond⟩ @@ -6672,8 +6731,10 @@ theorem compileRequireFailCond_core_ok YulExpr.call "iszero" [YulExpr.call "iszero" [condIR]], thenIR], YulExpr.call "mul" [ YulExpr.call "iszero" [condIR], elseIR]]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_ite_ok hcond hthen helse] - all_goals first | rfl | (intro a b; exact nofun)⟩ + have hcompile := compileExpr_ite_ok hcond hthen helse + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | ceilDiv hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ @@ -6683,16 +6744,20 @@ theorem compileRequireFailCond_core_ok YulExpr.call "add" [ YulExpr.call "div" [YulExpr.call "sub" [lhsIR, YulExpr.lit 1], rhsIR], YulExpr.lit 1]]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_ceilDiv_ok hlhs hrhs] - all_goals first | rfl | (intro a b; exact nofun)⟩ + have hcompile := compileExpr_ceilDiv_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | wMulDown hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ rcases compileExpr_core_ok (fields := fields) hR with ⟨rhsIR, hrhs⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "div" [ YulExpr.call "mul" [lhsIR, rhsIR], YulExpr.lit 1000000000000000000]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_wMulDown_ok hlhs hrhs] - all_goals first | rfl | (intro a b; exact nofun)⟩ + have hcompile := compileExpr_wMulDown_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | wDivUp hL hR => rename_i lhs rhs rcases compileExpr_core_ok (fields := fields) hL with ⟨lhsIR, hlhs⟩ @@ -6702,8 +6767,10 @@ theorem compileRequireFailCond_core_ok YulExpr.call "mul" [lhsIR, YulExpr.lit 1000000000000000000], YulExpr.call "sub" [rhsIR, YulExpr.lit 1]], rhsIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_wDivUp_ok hlhs hrhs] - all_goals first | rfl | (intro a b; exact nofun)⟩ + have hcompile := compileExpr_wDivUp_ok hlhs hrhs + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | mulDivDown hA hB hC => rename_i a b c rcases compileExpr_core_ok (fields := fields) hA with ⟨aIR, ha⟩ @@ -6711,8 +6778,10 @@ theorem compileRequireFailCond_core_ok rcases compileExpr_core_ok (fields := fields) hC with ⟨cIR, hc⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "div" [ YulExpr.call "mul" [aIR, bIR], cIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_mulDivDown_ok ha hb hc] - all_goals first | rfl | (intro a b; exact nofun)⟩ + have hcompile := compileExpr_mulDivDown_ok ha hb hc + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | mulDivUp hA hB hC => rename_i a b c rcases compileExpr_core_ok (fields := fields) hA with ⟨aIR, ha⟩ @@ -6722,39 +6791,34 @@ theorem compileRequireFailCond_core_ok YulExpr.call "add" [YulExpr.call "mul" [aIR, bIR], YulExpr.call "sub" [cIR, YulExpr.lit 1]], cIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_mulDivUp_ok ha hb hc] - all_goals first | rfl | (intro a b; exact nofun)⟩ + have hcompile := compileExpr_mulDivUp_ok ha hb hc + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | tload h => rename_i expr rcases compileExpr_core_ok (fields := fields) h with ⟨exprIR, hexpr⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "tload" [exprIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_tload_ok hexpr] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_tload_ok hexpr + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | calldataload h => rename_i expr rcases compileExpr_core_ok (fields := fields) h with ⟨exprIR, hexpr⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "calldataload" [exprIR]], by - rw [CompilationModel.compileRequireFailCond, - compileExpr_calldataload_ok hexpr] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_calldataload_ok hexpr + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ | mload h => rename_i expr rcases compileExpr_core_ok (fields := fields) h with ⟨exprIR, hexpr⟩ exact ⟨YulExpr.call "iszero" [YulExpr.call "mload" [exprIR]], by - rw [CompilationModel.compileRequireFailCond, compileExpr_mload_ok hexpr] - all_goals - try rfl - try - intro a b hEq - cases hEq⟩ + have hcompile := compileExpr_mload_ok hexpr + rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcompile + simp [CompilationModel.compileRequireFailCondWithInternals, hcompile]⟩ theorem eval_compileRequireFailCond_core_onExpr {fields : List Field} @@ -6807,84 +6871,96 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.literal value) from ExprCompileCore.literal value) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .literal value) (show ExprCompileCore (.literal value) from ExprCompileCore.literal value) hexact hpresent hexpr | param name => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.param name) from ExprCompileCore.param name) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .param name) (show ExprCompileCore (.param name) from ExprCompileCore.param name) hexact hpresent hexpr | localVar name => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.localVar name) from ExprCompileCore.localVar name) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .localVar name) (show ExprCompileCore (.localVar name) from ExprCompileCore.localVar name) hexact hpresent hexpr | caller => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.caller) from ExprCompileCore.caller) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .caller) (show ExprCompileCore (.caller) from ExprCompileCore.caller) hexact hpresent hexpr | contractAddress => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.contractAddress) from ExprCompileCore.contractAddress) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .contractAddress) (show ExprCompileCore (.contractAddress) from ExprCompileCore.contractAddress) hexact hpresent hexpr | txOrigin => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.txOrigin) from ExprCompileCore.txOrigin) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .txOrigin) (show ExprCompileCore (.txOrigin) from ExprCompileCore.txOrigin) hexact hpresent hexpr | msgValue => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.msgValue) from ExprCompileCore.msgValue) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .msgValue) (show ExprCompileCore (.msgValue) from ExprCompileCore.msgValue) hexact hpresent hexpr | blockTimestamp => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.blockTimestamp) from ExprCompileCore.blockTimestamp) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .blockTimestamp) (show ExprCompileCore (.blockTimestamp) from ExprCompileCore.blockTimestamp) hexact hpresent hexpr | blockNumber => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.blockNumber) from ExprCompileCore.blockNumber) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .blockNumber) (show ExprCompileCore (.blockNumber) from ExprCompileCore.blockNumber) hexact hpresent hexpr | chainid => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.chainid) from ExprCompileCore.chainid) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .chainid) (show ExprCompileCore (.chainid) from ExprCompileCore.chainid) hexact hpresent hexpr | blobbasefee => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.blobbasefee) from ExprCompileCore.blobbasefee) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .blobbasefee) (show ExprCompileCore (.blobbasefee) from ExprCompileCore.blobbasefee) hexact hpresent hexpr | calldatasize => rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.calldatasize) from ExprCompileCore.calldatasize) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .calldatasize) (show ExprCompileCore (.calldatasize) from ExprCompileCore.calldatasize) hexact hpresent hexpr | add hL hR => @@ -6892,7 +6968,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.add lhs rhs) from ExprCompileCore.add hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .add lhs rhs) (show ExprCompileCore (.add lhs rhs) from ExprCompileCore.add hL hR) hexact hpresent hexpr | sub hL hR => @@ -6900,7 +6977,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.sub lhs rhs) from ExprCompileCore.sub hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .sub lhs rhs) (show ExprCompileCore (.sub lhs rhs) from ExprCompileCore.sub hL hR) hexact hpresent hexpr | mul hL hR => @@ -6908,7 +6986,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.mul lhs rhs) from ExprCompileCore.mul hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .mul lhs rhs) (show ExprCompileCore (.mul lhs rhs) from ExprCompileCore.mul hL hR) hexact hpresent hexpr | div hL hR => @@ -6916,7 +6995,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.div lhs rhs) from ExprCompileCore.div hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .div lhs rhs) (show ExprCompileCore (.div lhs rhs) from ExprCompileCore.div hL hR) hexact hpresent hexpr | mod hL hR => @@ -6924,7 +7004,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.mod lhs rhs) from ExprCompileCore.mod hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .mod lhs rhs) (show ExprCompileCore (.mod lhs rhs) from ExprCompileCore.mod hL hR) hexact hpresent hexpr | eq hL hR => @@ -6932,7 +7013,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.eq lhs rhs) from ExprCompileCore.eq hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .eq lhs rhs) (show ExprCompileCore (.eq lhs rhs) from ExprCompileCore.eq hL hR) hexact hpresent hexpr | lt hL hR => @@ -6940,7 +7022,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.lt lhs rhs) from ExprCompileCore.lt hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .lt lhs rhs) (show ExprCompileCore (.lt lhs rhs) from ExprCompileCore.lt hL hR) hexact hpresent hexpr | slt hL hR => @@ -6948,7 +7031,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.slt lhs rhs) from ExprCompileCore.slt hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .slt lhs rhs) (show ExprCompileCore (.slt lhs rhs) from ExprCompileCore.slt hL hR) hexact hpresent hexpr | sgt hL hR => @@ -6956,7 +7040,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.sgt lhs rhs) from ExprCompileCore.sgt hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .sgt lhs rhs) (show ExprCompileCore (.sgt lhs rhs) from ExprCompileCore.sgt hL hR) hexact hpresent hexpr | sdiv hL hR => @@ -6964,7 +7049,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.sdiv lhs rhs) from ExprCompileCore.sdiv hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .sdiv lhs rhs) (show ExprCompileCore (.sdiv lhs rhs) from ExprCompileCore.sdiv hL hR) hexact hpresent hexpr | smod hL hR => @@ -6972,7 +7058,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.smod lhs rhs) from ExprCompileCore.smod hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .smod lhs rhs) (show ExprCompileCore (.smod lhs rhs) from ExprCompileCore.smod hL hR) hexact hpresent hexpr | sar hL hR => @@ -6980,7 +7067,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.sar lhs rhs) from ExprCompileCore.sar hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .sar lhs rhs) (show ExprCompileCore (.sar lhs rhs) from ExprCompileCore.sar hL hR) hexact hpresent hexpr | byte hL hR => @@ -6988,7 +7076,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.byte index value) from ExprCompileCore.byte hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .byte index value) (show ExprCompileCore (.byte index value) from ExprCompileCore.byte hL hR) hexact hpresent hexpr | signextend hL hR => @@ -6996,7 +7085,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.signextend lhs rhs) from ExprCompileCore.signextend hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .signextend lhs rhs) (show ExprCompileCore (.signextend lhs rhs) from ExprCompileCore.signextend hL hR) hexact hpresent hexpr | gt hL hR => @@ -7004,7 +7094,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.gt lhs rhs) from ExprCompileCore.gt hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .gt lhs rhs) (show ExprCompileCore (.gt lhs rhs) from ExprCompileCore.gt hL hR) hexact hpresent hexpr | ge hL hR => @@ -7049,7 +7140,10 @@ theorem eval_compileRequireFailCond_core_onExpr have hrhsLt := evalExpr_lt_evmModulus_core_onExpr hR hexactR hbounded hpresentR hruntime rw [hRhsSrc] at hrhsLt; simp at hrhsLt refine ⟨YulExpr.call "lt" [lhsIR, rhsIR], ?_, ?_⟩ - · rw [CompilationModel.compileRequireFailCond, hlhs, hrhs]; rfl + · rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + simp [CompilationModel.compileRequireFailCondWithInternals, CompilationModel.yulBinOp, hlhs, hrhs] + rfl · have hltEval := evalIRExpr_lt_of_eval hLhsIR hRhsIR -- evalExpr (.ge lhs rhs) = do lhsV ← ...; rhsV ← ...; pure (boolWord (decide (rhsV ≤ lhsV))) -- With lhs = some lhsVal, rhs = some rhsVal: @@ -7118,7 +7212,10 @@ theorem eval_compileRequireFailCond_core_onExpr have hrhsLt := evalExpr_lt_evmModulus_core_onExpr hR hexactR hbounded hpresentR hruntime rw [hRhsSrc] at hrhsLt; simp at hrhsLt refine ⟨YulExpr.call "gt" [lhsIR, rhsIR], ?_, ?_⟩ - · rw [CompilationModel.compileRequireFailCond, hlhs, hrhs]; rfl + · rw [CompilationModel.compileRequireFailCond] + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hlhs hrhs + simp [CompilationModel.compileRequireFailCondWithInternals, CompilationModel.yulBinOp, hlhs, hrhs] + rfl · have hgtEval := evalIRExpr_gt_of_eval hLhsIR hRhsIR simp [Nat.mod_eq_of_lt hlhsLt, Nat.mod_eq_of_lt hrhsLt] at hgtEval -- hgtEval : evalIRExpr state (call "gt" [..]) = some (boolWord (rhsVal < lhsVal)) @@ -7138,7 +7235,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.logicalNot expr) from ExprCompileCore.logicalNot h) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .logicalNot expr) (show ExprCompileCore (.logicalNot expr) from ExprCompileCore.logicalNot h) hexact hpresent hexpr | logicalAnd hL hR => @@ -7146,7 +7244,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.logicalAnd lhs rhs) from ExprCompileCore.logicalAnd hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .logicalAnd lhs rhs) (show ExprCompileCore (.logicalAnd lhs rhs) from ExprCompileCore.logicalAnd hL hR) hexact hpresent hexpr | logicalOr hL hR => @@ -7154,7 +7253,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.logicalOr lhs rhs) from ExprCompileCore.logicalOr hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .logicalOr lhs rhs) (show ExprCompileCore (.logicalOr lhs rhs) from ExprCompileCore.logicalOr hL hR) hexact hpresent hexpr | bitAnd hL hR => @@ -7162,7 +7262,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.bitAnd lhs rhs) from ExprCompileCore.bitAnd hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .bitAnd lhs rhs) (show ExprCompileCore (.bitAnd lhs rhs) from ExprCompileCore.bitAnd hL hR) hexact hpresent hexpr | bitOr hL hR => @@ -7170,7 +7271,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.bitOr lhs rhs) from ExprCompileCore.bitOr hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .bitOr lhs rhs) (show ExprCompileCore (.bitOr lhs rhs) from ExprCompileCore.bitOr hL hR) hexact hpresent hexpr | bitXor hL hR => @@ -7178,7 +7280,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.bitXor lhs rhs) from ExprCompileCore.bitXor hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .bitXor lhs rhs) (show ExprCompileCore (.bitXor lhs rhs) from ExprCompileCore.bitXor hL hR) hexact hpresent hexpr | bitNot h => @@ -7186,7 +7289,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.bitNot expr) from ExprCompileCore.bitNot h) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .bitNot expr) (show ExprCompileCore (.bitNot expr) from ExprCompileCore.bitNot h) hexact hpresent hexpr | shl hS hV => @@ -7194,7 +7298,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.shl shift value) from ExprCompileCore.shl hS hV) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .shl shift value) (show ExprCompileCore (.shl shift value) from ExprCompileCore.shl hS hV) hexact hpresent hexpr | shr hS hV => @@ -7202,7 +7307,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.shr shift value) from ExprCompileCore.shr hS hV) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .shr shift value) (show ExprCompileCore (.shr shift value) from ExprCompileCore.shr hS hV) hexact hpresent hexpr | min hL hR => @@ -7210,7 +7316,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.min lhs rhs) from ExprCompileCore.min hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .min lhs rhs) (show ExprCompileCore (.min lhs rhs) from ExprCompileCore.min hL hR) hexact hpresent hexpr | max hL hR => @@ -7218,7 +7325,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.max lhs rhs) from ExprCompileCore.max hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .max lhs rhs) (show ExprCompileCore (.max lhs rhs) from ExprCompileCore.max hL hR) hexact hpresent hexpr | ite hC hT hE => @@ -7226,7 +7334,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.ite cond thenVal elseVal) from ExprCompileCore.ite hC hT hE) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .ite cond thenVal elseVal) (show ExprCompileCore (.ite cond thenVal elseVal) from ExprCompileCore.ite hC hT hE) hexact hpresent hexpr | ceilDiv hL hR => @@ -7234,7 +7343,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.ceilDiv lhs rhs) from ExprCompileCore.ceilDiv hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .ceilDiv lhs rhs) (show ExprCompileCore (.ceilDiv lhs rhs) from ExprCompileCore.ceilDiv hL hR) hexact hpresent hexpr | wMulDown hL hR => @@ -7242,7 +7352,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.wMulDown lhs rhs) from ExprCompileCore.wMulDown hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .wMulDown lhs rhs) (show ExprCompileCore (.wMulDown lhs rhs) from ExprCompileCore.wMulDown hL hR) hexact hpresent hexpr | wDivUp hL hR => @@ -7250,7 +7361,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.wDivUp lhs rhs) from ExprCompileCore.wDivUp hL hR) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .wDivUp lhs rhs) (show ExprCompileCore (.wDivUp lhs rhs) from ExprCompileCore.wDivUp hL hR) hexact hpresent hexpr | mulDivDown hA hB hC => @@ -7258,7 +7370,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.mulDivDown a b c) from ExprCompileCore.mulDivDown hA hB hC) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .mulDivDown a b c) (show ExprCompileCore (.mulDivDown a b c) from ExprCompileCore.mulDivDown hA hB hC) hexact hpresent hexpr | mulDivUp hA hB hC => @@ -7266,7 +7379,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.mulDivUp a b c) from ExprCompileCore.mulDivUp hA hB hC) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .mulDivUp a b c) (show ExprCompileCore (.mulDivUp a b c) from ExprCompileCore.mulDivUp hA hB hC) hexact hpresent hexpr | tload h => @@ -7274,7 +7388,8 @@ theorem eval_compileRequireFailCond_core_onExpr rcases compileExpr_core_ok (fields := fields) (show ExprCompileCore (.tload expr) from ExprCompileCore.tload h) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .tload expr) (show ExprCompileCore (.tload expr) from ExprCompileCore.tload h) hexact hpresent hexpr | calldataload h => @@ -7283,7 +7398,8 @@ theorem eval_compileRequireFailCond_core_onExpr (show ExprCompileCore (.calldataload expr) from ExprCompileCore.calldataload h) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .calldataload expr) (show ExprCompileCore (.calldataload expr) from ExprCompileCore.calldataload h) hexact hpresent hexpr @@ -7293,7 +7409,8 @@ theorem eval_compileRequireFailCond_core_onExpr (show ExprCompileCore (.mload expr) from ExprCompileCore.mload h) with ⟨exprIR, hexpr⟩ refine ⟨YulExpr.call "iszero" [exprIR], ?_, ?_⟩ - · simp [CompilationModel.compileRequireFailCond, hexpr] + · rw [← CompilationModel.compileExprWithInternals_nil_eq] at hexpr + simp [CompilationModel.compileRequireFailCond, CompilationModel.compileRequireFailCondWithInternals, hexpr] · simpa using finishIszeroEval (expr := .mload expr) (show ExprCompileCore (.mload expr) from ExprCompileCore.mload h) hexact hpresent hexpr diff --git a/Compiler/Proofs/IRGeneration/FunctionBody/Stmt.lean b/Compiler/Proofs/IRGeneration/FunctionBody/Stmt.lean index 23d4357c6..6aa8bf57b 100644 --- a/Compiler/Proofs/IRGeneration/FunctionBody/Stmt.lean +++ b/Compiler/Proofs/IRGeneration/FunctionBody/Stmt.lean @@ -87,6 +87,19 @@ inductive StmtCompileCore : Stmt → Prop where ExprCompileCore offset → ExprCompileCore value → StmtCompileCore (.tstore offset value) +private theorem compileExprWithInternals_nil_ok + {fields : List Field} {dynamicSource : DynamicDataSource} {expr : Expr} {exprIR : YulExpr} + (h : CompilationModel.compileExpr fields dynamicSource expr = Except.ok exprIR) : + CompilationModel.compileExprWithInternals fields dynamicSource [] expr = Except.ok exprIR := by + simpa [CompilationModel.compileExprWithInternals_nil_eq] using h + +private theorem compileRequireFailCondWithInternals_nil_ok + {fields : List Field} {dynamicSource : DynamicDataSource} {expr : Expr} {exprIR : YulExpr} + (h : CompilationModel.compileRequireFailCond fields dynamicSource expr = Except.ok exprIR) : + CompilationModel.compileRequireFailCondWithInternals fields dynamicSource [] expr = + Except.ok exprIR := by + simpa [CompilationModel.compileRequireFailCondWithInternals_nil_eq] using h + theorem compileStmt_core_ok {fields : List Field} {stmt : Stmt} @@ -95,46 +108,42 @@ theorem compileStmt_core_ok cases hcore with | letVar hvalue => rename_i name value - rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ - exact ⟨[YulStmt.let_ name valueIR], by - rw [CompilationModel.compileStmt, hvalueIR] - rfl⟩ + rcases compileExpr_core_ok (fields := fields) hvalue with ⟨valueIR, hvalueIR⟩ + exact ⟨[YulStmt.let_ name valueIR], + by simp [CompilationModel.compileStmt, compileExprWithInternals_nil_ok hvalueIR]⟩ | assignVar hvalue => rename_i name value - rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ - exact ⟨[YulStmt.assign name valueIR], by - rw [CompilationModel.compileStmt, hvalueIR] - rfl⟩ + rcases compileExpr_core_ok (fields := fields) hvalue with ⟨valueIR, hvalueIR⟩ + exact ⟨[YulStmt.assign name valueIR], + by simp [CompilationModel.compileStmt, compileExprWithInternals_nil_ok hvalueIR]⟩ | require_ hcond => rename_i cond message - rcases compileRequireFailCond_core_ok hcond with ⟨failCond, hfailCond⟩ - exact ⟨[YulStmt.if_ failCond (CompilationModel.revertWithMessage message)], by - rw [CompilationModel.compileStmt, hfailCond] - rfl⟩ + rcases compileRequireFailCond_core_ok (fields := fields) hcond with ⟨failCond, hfailCond⟩ + exact ⟨[YulStmt.if_ failCond (CompilationModel.revertWithMessage message)], + by simp [CompilationModel.compileStmt, compileRequireFailCondWithInternals_nil_ok hfailCond]⟩ | return_ hvalue => - rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ + rcases compileExpr_core_ok (fields := fields) hvalue with ⟨valueIR, hvalueIR⟩ exact ⟨[ YulStmt.expr (YulExpr.call "mstore" [YulExpr.lit 0, valueIR]) - , YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32]) ], by - rw [CompilationModel.compileStmt, hvalueIR] - rfl⟩ + , YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32]) ], + by simp [CompilationModel.compileStmt, compileExprWithInternals_nil_ok hvalueIR]⟩ | stop => exact ⟨[YulStmt.expr (YulExpr.call "stop" [])], by rw [CompilationModel.compileStmt] rfl⟩ | mstore hoffset hvalue => rename_i offset value - rcases compileExpr_core_ok hoffset with ⟨offsetIR, hoffsetIR⟩ - rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ - exact ⟨[YulStmt.expr (YulExpr.call "mstore" [offsetIR, valueIR])], by - rw [CompilationModel.compileStmt, hoffsetIR, hvalueIR] - rfl⟩ + rcases compileExpr_core_ok (fields := fields) hoffset with ⟨offsetIR, hoffsetIR⟩ + rcases compileExpr_core_ok (fields := fields) hvalue with ⟨valueIR, hvalueIR⟩ + exact ⟨[YulStmt.expr (YulExpr.call "mstore" [offsetIR, valueIR])], + by simp [CompilationModel.compileStmt, compileExprWithInternals_nil_ok hoffsetIR, + compileExprWithInternals_nil_ok hvalueIR, Bind.bind, Except.bind, pure, Except.pure]⟩ | tstore hoffset hvalue => rename_i offset value - rcases compileExpr_core_ok hoffset with ⟨offsetIR, hoffsetIR⟩ - rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ - exact ⟨[YulStmt.expr (YulExpr.call "tstore" [offsetIR, valueIR])], by - rw [CompilationModel.compileStmt, hoffsetIR, hvalueIR] - rfl⟩ + rcases compileExpr_core_ok (fields := fields) hoffset with ⟨offsetIR, hoffsetIR⟩ + rcases compileExpr_core_ok (fields := fields) hvalue with ⟨valueIR, hvalueIR⟩ + exact ⟨[YulStmt.expr (YulExpr.call "tstore" [offsetIR, valueIR])], + by simp [CompilationModel.compileStmt, compileExprWithInternals_nil_ok hoffsetIR, + compileExprWithInternals_nil_ok hvalueIR, Bind.bind, Except.bind, pure, Except.pure]⟩ theorem runtimeStateMatchesIR_setBothMemory {fields : List Field} @@ -406,7 +415,10 @@ theorem exec_compileStmt_letVar_core stmtResultMatchesIRExecExact sourceResult irExec := by rcases compileExpr_core_ok hcore with ⟨valueIR, hvalueIR⟩ refine ⟨[YulStmt.let_ name valueIR], ?_, ?_⟩ - · rw [CompilationModel.compileStmt, hvalueIR]; rfl + · have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] + rfl · -- Get the bridge: both evaluations succeed with same value have heval := eval_compileExpr_core hcore hexact hbounded hpresent hruntime rw [hvalueIR] at heval @@ -449,7 +461,10 @@ theorem exec_compileStmt_assignVar_core stmtResultMatchesIRExecExact sourceResult irExec := by rcases compileExpr_core_ok hcore with ⟨valueIR, hvalueIR⟩ refine ⟨[YulStmt.assign name valueIR], ?_, ?_⟩ - · rw [CompilationModel.compileStmt, hvalueIR]; rfl + · have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] + rfl · have heval := eval_compileExpr_core hcore hexact hbounded hpresent hruntime rw [hvalueIR] at heval simp [Except.toOption] at heval @@ -483,7 +498,10 @@ theorem exec_compileStmt_return_core rcases compileExpr_core_ok hcore with ⟨valueIR, hvalueIR⟩ refine ⟨[ YulStmt.expr (YulExpr.call "mstore" [YulExpr.lit 0, valueIR]) , YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32]) ], ?_, ?_⟩ - · rw [CompilationModel.compileStmt, hvalueIR]; rfl + · have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] + rfl · have heval := eval_compileExpr_core hcore hexact hbounded hpresent hruntime rw [hvalueIR] at heval simp [Except.toOption] at heval @@ -519,7 +537,10 @@ theorem exec_compileStmt_return_core_extraFuel rcases compileExpr_core_ok hcore with ⟨valueIR, hvalueIR⟩ refine ⟨[ YulStmt.expr (YulExpr.call "mstore" [YulExpr.lit 0, valueIR]) , YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32]) ], ?_, ?_⟩ - · rw [CompilationModel.compileStmt, hvalueIR]; rfl + · have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] + rfl · have heval := eval_compileExpr_core hcore hexact hbounded hpresent hruntime rw [hvalueIR] at heval simp [Except.toOption] at heval @@ -794,25 +815,33 @@ theorem compileStmt_core_ok_any_scope rename_i name value rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ exact ⟨[YulStmt.let_ name valueIR], by - rw [CompilationModel.compileStmt, hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] rfl⟩ | assignVar hvalue => rename_i name value rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ exact ⟨[YulStmt.assign name valueIR], by - rw [CompilationModel.compileStmt, hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] rfl⟩ | require_ hcond => rename_i cond message rcases compileRequireFailCond_core_ok hcond with ⟨failCond, hfailCond⟩ exact ⟨[YulStmt.if_ failCond (CompilationModel.revertWithMessage message)], by - rw [CompilationModel.compileStmt, hfailCond] + have hfailCondInternal := hfailCond + rw [← CompilationModel.compileRequireFailCondWithInternals_nil_eq] at hfailCondInternal + rw [CompilationModel.compileStmt, hfailCondInternal] rfl⟩ | return_ hvalue => rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ exact ⟨[ YulStmt.expr (YulExpr.call "mstore" [YulExpr.lit 0, valueIR]) , YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32]) ], by - rw [CompilationModel.compileStmt, hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] rfl⟩ | stop => exact ⟨[YulStmt.expr (YulExpr.call "stop" [])], by @@ -823,14 +852,20 @@ theorem compileStmt_core_ok_any_scope rcases compileExpr_core_ok hoffset with ⟨offsetIR, hoffsetIR⟩ rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ exact ⟨[YulStmt.expr (YulExpr.call "mstore" [offsetIR, valueIR])], by - rw [CompilationModel.compileStmt, hoffsetIR, hvalueIR] + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [CompilationModel.compileStmt, hoffsetIRInternal, hvalueIRInternal] rfl⟩ | tstore hoffset hvalue => rename_i offset value rcases compileExpr_core_ok hoffset with ⟨offsetIR, hoffsetIR⟩ rcases compileExpr_core_ok hvalue with ⟨valueIR, hvalueIR⟩ exact ⟨[YulStmt.expr (YulExpr.call "tstore" [offsetIR, valueIR])], by - rw [CompilationModel.compileStmt, hoffsetIR, hvalueIR] + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [CompilationModel.compileStmt, hoffsetIRInternal, hvalueIRInternal] rfl⟩ /-! ### Scope-independence of compileStmt / compileStmtList success @@ -867,7 +902,7 @@ private theorem compileStmt_ok_any_scope_aux | ite cond thenBranch elseBranch => rcases hok with ⟨ir, hir⟩ simp only [CompilationModel.compileStmt, bind, Except.bind] at hir ⊢ - cases hcond : CompilationModel.compileExpr fields .calldata cond with + cases hcond : CompilationModel.compileExprWithInternals fields .calldata [] cond with | error e => simp [hcond] at hir | ok condIR => simp only [hcond] at hir ⊢ @@ -891,7 +926,7 @@ private theorem compileStmt_ok_any_scope_aux | forEach varName count body => rcases hok with ⟨ir, hir⟩ simp only [CompilationModel.compileStmt, bind, Except.bind] at hir ⊢ - cases hcount : CompilationModel.compileExpr fields .calldata count with + cases hcount : CompilationModel.compileExprWithInternals fields .calldata [] count with | error e => simp [hcount] at hir | ok countIR => simp only [hcount] at hir ⊢ @@ -989,7 +1024,7 @@ private theorem compileStmt_ok_any_scope_with_surface_aux | ite cond thenBranch elseBranch => rcases hok with ⟨ir, hir⟩ simp only [CompilationModel.compileStmt, bind, Except.bind] at hir ⊢ - cases hcond : CompilationModel.compileExpr fields .calldata cond with + cases hcond : CompilationModel.compileExprWithInternals fields .calldata [] cond with | error e => simp [hcond] at hir | ok condIR => simp only [hcond] at hir ⊢ @@ -1013,7 +1048,7 @@ private theorem compileStmt_ok_any_scope_with_surface_aux | forEach varName count body => rcases hok with ⟨ir, hir⟩ simp only [CompilationModel.compileStmt, bind, Except.bind] at hir ⊢ - cases hcount : CompilationModel.compileExpr fields .calldata count with + cases hcount : CompilationModel.compileExprWithInternals fields .calldata [] count with | error e => simp [hcount] at hir | ok countIR => simp only [hcount] at hir ⊢ @@ -1219,7 +1254,7 @@ theorem compileStmt_terminal_ite_ok_inv , YulStmt.if_ (YulExpr.ident tempName) thenIR , YulStmt.if_ (YulExpr.call "iszero" [YulExpr.ident tempName]) elseIR ]] := by unfold CompilationModel.compileStmt at hcompile - cases hcond : CompilationModel.compileExpr fields .calldata cond with + cases hcond : CompilationModel.compileExprWithInternals fields .calldata [] cond with | error err => simp [hcond] at hcompile cases hcompile @@ -1505,7 +1540,9 @@ theorem compileStmtList_terminal_core_ok ]] ++ tailIR, ?_⟩ rw [CompilationModel.compileStmtList] unfold CompilationModel.compileStmt - rw [hcondIR, hthenIR, helseIR] + have hcondIRInternal := hcondIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcondIRInternal + rw [hcondIRInternal, hthenIR, helseIR] dsimp rw [htailIR] simp [helseNonempty] @@ -1528,7 +1565,9 @@ theorem compileStmtList_terminal_core_ok_nonempty rcases compileStmtList_cons_ok_inv (fields := fields) (inScopeNames := inScopeNames) (stmt := .letVar name value) (rest := rest) hcompile with ⟨headIR, tailIR, hhead, _, hbody⟩ - rw [CompilationModel.compileStmt, hvalueIR] at hhead + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] at hhead injection hhead with hheadEq subst hheadEq simp [hbody] @@ -1538,7 +1577,9 @@ theorem compileStmtList_terminal_core_ok_nonempty rcases compileStmtList_cons_ok_inv (fields := fields) (inScopeNames := inScopeNames) (stmt := .assignVar name value) (rest := rest) hcompile with ⟨headIR, tailIR, hhead, _, hbody⟩ - rw [CompilationModel.compileStmt, hvalueIR] at hhead + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] at hhead injection hhead with hheadEq subst hheadEq simp [hbody] @@ -1548,7 +1589,9 @@ theorem compileStmtList_terminal_core_ok_nonempty rcases compileStmtList_cons_ok_inv (fields := fields) (inScopeNames := inScopeNames) (stmt := .require cond message) (rest := rest) hcompile with ⟨headIR, tailIR, hhead, _, hbody⟩ - rw [CompilationModel.compileStmt, hfailCond] at hhead + have hfailCondInternal := hfailCond + rw [← CompilationModel.compileRequireFailCondWithInternals_nil_eq] at hfailCondInternal + rw [CompilationModel.compileStmt, hfailCondInternal] at hhead injection hhead with hheadEq subst hheadEq simp [hbody] @@ -1558,7 +1601,9 @@ theorem compileStmtList_terminal_core_ok_nonempty rcases compileStmtList_cons_ok_inv (fields := fields) (inScopeNames := inScopeNames) (stmt := .return value) (rest := rest) hcompile with ⟨headIR, tailIR, hhead, _, hbody⟩ - rw [CompilationModel.compileStmt, hvalueIR] at hhead + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [CompilationModel.compileStmt, hvalueIRInternal] at hhead injection hhead with hheadEq subst hheadEq simp [hbody] @@ -1578,7 +1623,10 @@ theorem compileStmtList_terminal_core_ok_nonempty rcases compileStmtList_cons_ok_inv (fields := fields) (inScopeNames := inScopeNames) (stmt := .mstore offset value) (rest := rest) hcompile with ⟨headIR, tailIR, hhead, _, hbody⟩ - rw [CompilationModel.compileStmt, hoffsetIR, hvalueIR] at hhead + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [CompilationModel.compileStmt, hoffsetIRInternal, hvalueIRInternal] at hhead injection hhead with hheadEq subst hheadEq simp [hbody] @@ -1589,7 +1637,10 @@ theorem compileStmtList_terminal_core_ok_nonempty rcases compileStmtList_cons_ok_inv (fields := fields) (inScopeNames := inScopeNames) (stmt := .tstore offset value) (rest := rest) hcompile with ⟨headIR, tailIR, hhead, _, hbody⟩ - rw [CompilationModel.compileStmt, hoffsetIR, hvalueIR] at hhead + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [CompilationModel.compileStmt, hoffsetIRInternal, hvalueIRInternal] at hhead injection hhead with hheadEq subst hheadEq simp [hbody] @@ -1612,9 +1663,11 @@ theorem compileStmtList_terminal_core_ok_nonempty rcases compileStmtList_terminal_core_ok (fields := fields) (scope := scope) (inScopeNames := inScopeNames) (stmts := elseBranch) helse with ⟨elseIR', helseOk⟩ - cases hcondIR : CompilationModel.compileExpr fields .calldata cond with + have hcondOkInternal := hcondOk + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcondOkInternal + cases hcondIR : CompilationModel.compileExprWithInternals fields .calldata [] cond with | error err => - rw [hcondOk] at hcondIR + rw [hcondOkInternal] at hcondIR cases hcondIR | ok condIR => cases hthenIR : @@ -2806,7 +2859,9 @@ theorem exec_compileStmtList_core ⟨tailIR, htailCompile, htailSem, htailExact⟩ refine ⟨[YulStmt.let_ name valueIR] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -2856,7 +2911,9 @@ theorem exec_compileStmtList_core ⟨tailIR, htailCompile, htailSem, htailExact⟩ refine ⟨[YulStmt.assign name valueIR] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -2899,7 +2956,9 @@ theorem exec_compileStmtList_core ⟨tailIR, htailCompile, htailSem, htailExact⟩ refine ⟨[YulStmt.if_ failCond (CompilationModel.revertWithMessage message)] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hfailCompile] + have hfailCompileInternal := hfailCompile + rw [← CompilationModel.compileRequireFailCondWithInternals_nil_eq] at hfailCompileInternal + rw [hfailCompileInternal] simp [htailCompile] exact rfl · rw [SourceSemantics.execStmtList, SourceSemantics.execStmt, hCondSrc] @@ -2976,7 +3035,9 @@ theorem exec_compileStmtList_core , YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32]) ] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [hvalueIRInternal] simp [htailCompile] exact rfl · have hruntime' : runtimeStateMatchesIR fields runtime' state' := @@ -3082,7 +3143,10 @@ theorem exec_compileStmtList_core ⟨tailIR, htailCompile, htailSem, htailExact⟩ refine ⟨[YulStmt.expr (YulExpr.call "mstore" [offsetIR, valueIR])] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hoffsetIR, hvalueIR] + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [hoffsetIRInternal, hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -3143,7 +3207,10 @@ theorem exec_compileStmtList_core ⟨tailIR, htailCompile, htailSem, htailExact⟩ refine ⟨[YulStmt.expr (YulExpr.call "tstore" [offsetIR, valueIR])] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hoffsetIR, hvalueIR] + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [hoffsetIRInternal, hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -3218,7 +3285,9 @@ theorem exec_compileStmtList_core_extraFuel ⟨tailIR, htailCompile, htailSem, htailExact⟩ refine ⟨[YulStmt.let_ name valueIR] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -3280,7 +3349,9 @@ theorem exec_compileStmtList_core_extraFuel ⟨tailIR, htailCompile, htailSem, htailExact⟩ refine ⟨[YulStmt.assign name valueIR] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -3336,7 +3407,9 @@ theorem exec_compileStmtList_core_extraFuel refine ⟨[YulStmt.if_ failCond (CompilationModel.revertWithMessage message)] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hfailCompile] + have hfailCompileInternal := hfailCompile + rw [← CompilationModel.compileRequireFailCondWithInternals_nil_eq] at hfailCompileInternal + rw [hfailCompileInternal] simp [htailCompile] exact rfl · rw [SourceSemantics.execStmtList, SourceSemantics.execStmt, hCondSrc] @@ -3426,7 +3499,9 @@ theorem exec_compileStmtList_core_extraFuel , YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32]) ] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [hvalueIRInternal] simp [htailCompile] exact rfl · have hruntime' : runtimeStateMatchesIR fields runtime' state' := @@ -3546,7 +3621,10 @@ theorem exec_compileStmtList_core_extraFuel ⟨tailIR, htailCompile, htailSem, htailExact⟩ refine ⟨[YulStmt.expr (YulExpr.call "mstore" [offsetIR, valueIR])] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hoffsetIR, hvalueIR] + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [hoffsetIRInternal, hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -3612,7 +3690,10 @@ theorem exec_compileStmtList_core_extraFuel ⟨tailIR, htailCompile, htailSem, htailExact⟩ refine ⟨[YulStmt.expr (YulExpr.call "tstore" [offsetIR, valueIR])] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hoffsetIR, hvalueIR] + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [hoffsetIRInternal, hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -7141,7 +7222,9 @@ theorem exec_compileStmtList_terminal_core_sizeOf_extraFuel ⟨tailIR, htailCompile, htailSem⟩ refine ⟨[YulStmt.let_ name valueIR] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -7198,7 +7281,9 @@ theorem exec_compileStmtList_terminal_core_sizeOf_extraFuel ⟨tailIR, htailCompile, htailSem⟩ refine ⟨[YulStmt.assign name valueIR] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -7242,7 +7327,9 @@ theorem exec_compileStmtList_terminal_core_sizeOf_extraFuel refine ⟨[YulStmt.if_ failCond (CompilationModel.revertWithMessage message)] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hfailCompile] + have hfailCompileInternal := hfailCompile + rw [← CompilationModel.compileRequireFailCondWithInternals_nil_eq] at hfailCompileInternal + rw [hfailCompileInternal] simp [htailCompile] exact rfl · by_cases hzero : condVal = 0 @@ -7317,7 +7404,9 @@ theorem exec_compileStmtList_terminal_core_sizeOf_extraFuel , YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32]) ] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + rw [hvalueIRInternal] simp [htailCompile] exact rfl · exact stmtResultMatchesIRExec_compiled_return_core_append_wholeFuel_of_scope @@ -7378,7 +7467,10 @@ theorem exec_compileStmtList_terminal_core_sizeOf_extraFuel ⟨tailIR, htailCompile, htailSem⟩ refine ⟨[YulStmt.expr (YulExpr.call "mstore" [offsetIR, valueIR])] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hoffsetIR, hvalueIR] + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [hoffsetIRInternal, hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -7446,7 +7538,10 @@ theorem exec_compileStmtList_terminal_core_sizeOf_extraFuel ⟨tailIR, htailCompile, htailSem⟩ refine ⟨[YulStmt.expr (YulExpr.call "tstore" [offsetIR, valueIR])] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hoffsetIR, hvalueIR] + have hoffsetIRInternal := hoffsetIR + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hoffsetIRInternal hvalueIRInternal + rw [hoffsetIRInternal, hvalueIRInternal] simp [htailCompile] exact rfl · have hstmt : @@ -7493,7 +7588,9 @@ theorem exec_compileStmtList_terminal_core_sizeOf_extraFuel , YulStmt.if_ (YulExpr.call "iszero" [YulExpr.ident tempName]) elseIR ]] ++ tailIR, ?_, ?_⟩ · unfold CompilationModel.compileStmtList CompilationModel.compileStmt - rw [hcondIR, hthenIR, helseIR] + have hcondIRInternal := hcondIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hcondIRInternal + rw [hcondIRInternal, hthenIR, helseIR] simp [helseNonempty, htailIR, tempName] exact rfl · -- Evaluate the condition diff --git a/Compiler/Proofs/IRGeneration/GenericInduction/EventBridge.lean b/Compiler/Proofs/IRGeneration/GenericInduction/EventBridge.lean index 486617177..e00fafc50 100644 --- a/Compiler/Proofs/IRGeneration/GenericInduction/EventBridge.lean +++ b/Compiler/Proofs/IRGeneration/GenericInduction/EventBridge.lean @@ -222,33 +222,33 @@ private theorem compileExpr_atomic_shape cases expr <;> simp [exprEventArgAtomic] at hatomic case literal n => refine Or.inl ⟨n % CompilationModel.uint256Modulus, ?_⟩ - simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm + simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm case param name => refine Or.inr (Or.inl ⟨name, ?_, ?_⟩) - · simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm + · simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm · exact hinScope name (by simp [FunctionBody.exprBoundNames]) case localVar name => refine Or.inr (Or.inl ⟨name, ?_, ?_⟩) - · simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm + · simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm · exact hinScope name (by simp [FunctionBody.exprBoundNames]) case caller => - exact Or.inr (Or.inr ⟨"caller", by simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm⟩) + exact Or.inr (Or.inr ⟨"caller", by simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm⟩) case contractAddress => - exact Or.inr (Or.inr ⟨"address", by simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm⟩) + exact Or.inr (Or.inr ⟨"address", by simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm⟩) case txOrigin => - exact Or.inr (Or.inr ⟨"origin", by simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm⟩) + exact Or.inr (Or.inr ⟨"origin", by simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm⟩) case msgValue => - exact Or.inr (Or.inr ⟨"callvalue", by simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm⟩) + exact Or.inr (Or.inr ⟨"callvalue", by simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm⟩) case blockTimestamp => - exact Or.inr (Or.inr ⟨"timestamp", by simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm⟩) + exact Or.inr (Or.inr ⟨"timestamp", by simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm⟩) case blockNumber => - exact Or.inr (Or.inr ⟨"number", by simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm⟩) + exact Or.inr (Or.inr ⟨"number", by simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm⟩) case chainid => - exact Or.inr (Or.inr ⟨"chainid", by simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm⟩) + exact Or.inr (Or.inr ⟨"chainid", by simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm⟩) case blobbasefee => - exact Or.inr (Or.inr ⟨"blobbasefee", by simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm⟩) + exact Or.inr (Or.inr ⟨"blobbasefee", by simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm⟩) case calldatasize => - exact Or.inr (Or.inr ⟨"calldatasize", by simpa [CompilationModel.compileExpr, pure, Except.pure] using hcompile.symm⟩) + exact Or.inr (Or.inr ⟨"calldatasize", by simpa [CompilationModel.compileExpr, CompilationModel.compileExprWithInternals, pure, Except.pure] using hcompile.symm⟩) private theorem eventExprCompileCore_of_exprEventArgAtomic {expr : Expr} @@ -266,9 +266,9 @@ private theorem eventCompileExprList_atomic_shapes List.Forall₂ (fun argExpr _ => AtomicArgIR scope argExpr) argExprs args := by induction args generalizing argExprs with | nil => - simp [CompilationModel.compileExprList] at hcompile - injection hcompile with hargs - subst hargs + simp [CompilationModel.compileExprList, CompilationModel.compileExprListWithInternals, + pure, Except.pure] at hcompile + cases hcompile exact .nil | cons arg rest ih => simp only [List.all_cons, Bool.and_eq_true] at hatomic @@ -288,9 +288,16 @@ private theorem eventCompileExprList_atomic_shapes ((List.all_eq_true.mp hatomic.2) tailArg hmem) rcases compileExprList_core_ok (fields := fields) htailCore with ⟨restIRs, hrestIRs⟩ - rw [CompilationModel.compileExprList, hargIR, hrestIRs] at hcompile - injection hcompile with hcompiledTail - subst hcompiledTail + have hargIRInternal : + CompilationModel.compileExprWithInternals fields .calldata [] arg = Except.ok argIR := by + simpa [CompilationModel.compileExprWithInternals_nil_eq] using hargIR + have hrestIRsInternal : + CompilationModel.compileExprListWithInternals fields .calldata [] rest = + Except.ok restIRs := by + simpa [CompilationModel.compileExprListWithInternals_nil_eq] using hrestIRs + simp [CompilationModel.compileExprList, CompilationModel.compileExprListWithInternals, + hargIRInternal, hrestIRsInternal, Bind.bind, Except.bind, pure, Except.pure] at hcompile + subst hcompile exact .cons (compileExpr_atomic_shape hatomic.1 hheadScope hargIR) (ih hatomic.2 htailScope hrestIRs) @@ -2280,8 +2287,12 @@ theorem eventCompileStmt_emit_scalar_shape have hscalarCompile : eventDefScalarCompileSupported eventDef = true := by simpa [eventDefScalarProofSupported] using hscalar + have hargExprsInternal : + CompilationModel.compileExprListWithInternals fields .calldata [] args = + Except.ok argExprs := by + simpa [CompilationModel.compileExprListWithInternals_nil_eq] using hargExprs simp only [CompilationModel.compileStmt, CompilationModel.compileEmit] at hcompile - simp [hfind, hlen, hargExprs, hindexedGuard, hscalarCompile, + simp [hfind, hlen, hargExprsInternal, hindexedGuard, hscalarCompile, Bind.bind, Except.bind, pure, Except.pure] at hcompile exact ⟨eventDef, argExprs, hfind, hargExprs, hcompile.symm⟩ diff --git a/Compiler/Proofs/IRGeneration/GenericInduction/ExprStmt.lean b/Compiler/Proofs/IRGeneration/GenericInduction/ExprStmt.lean index 3a1d2bf94..5cd3e50f0 100644 --- a/Compiler/Proofs/IRGeneration/GenericInduction/ExprStmt.lean +++ b/Compiler/Proofs/IRGeneration/GenericInduction/ExprStmt.lean @@ -21,7 +21,9 @@ theorem compiledStmtStep_letVar (hvalueIR : CompilationModel.compileExpr fields .calldata value = Except.ok valueIR) : CompiledStmtStep fields scope (.letVar name value) [YulStmt.let_ name valueIR] where compileOk := by - simp [CompilationModel.compileStmt, hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + simp [CompilationModel.compileStmt, hvalueIRInternal] preserves runtime state extraFuel hexact hscope hbounded hruntime hslack := by -- Establish that evalExpr succeeds (returns some) via the compile-eval theorem have heval := FunctionBody.eval_compileExpr_core_of_scope hcore hexact hinScope @@ -114,7 +116,9 @@ theorem compiledStmtStep_assignVar (hvalueIR : CompilationModel.compileExpr fields .calldata value = Except.ok valueIR) : CompiledStmtStep fields scope (.assignVar name value) [YulStmt.assign name valueIR] where compileOk := by - simp [CompilationModel.compileStmt, hvalueIR] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + simp [CompilationModel.compileStmt, hvalueIRInternal] preserves runtime state extraFuel hexact hscope hbounded hruntime hslack := by -- Establish that evalExpr succeeds (returns some) via the compile-eval theorem have heval := FunctionBody.eval_compileExpr_core_of_scope hcore hexact hinScope @@ -200,7 +204,9 @@ theorem compiledStmtStep_require CompiledStmtStep fields scope (.require cond message) [YulStmt.if_ failCond (CompilationModel.revertWithMessage message)] where compileOk := by - simp [CompilationModel.compileStmt, hfailCompile] + have hfailCompileInternal := hfailCompile + rw [← CompilationModel.compileRequireFailCondWithInternals_nil_eq] at hfailCompileInternal + simp [CompilationModel.compileStmt, hfailCompileInternal] preserves runtime state extraFuel hexact hscope hbounded hruntime hslack := by have hpresent : FunctionBody.exprBoundNamesPresent cond runtime.bindings := FunctionBody.exprBoundNamesPresent_of_scope hscope hinScope @@ -305,7 +311,9 @@ theorem compiledStmtStep_return [ YulStmt.expr (YulExpr.call "mstore" [YulExpr.lit 0, valueIR]) , YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32]) ] where compileOk := by - simp [CompilationModel.compileStmt, hvalueIR, pure, Except.pure, bind, Except.bind] + have hvalueIRInternal := hvalueIR + rw [← CompilationModel.compileExprWithInternals_nil_eq] at hvalueIRInternal + simp [CompilationModel.compileStmt, hvalueIRInternal, pure, Except.pure, bind, Except.bind] preserves runtime state extraFuel hexact hscope hbounded hruntime hslack := by set compiledIR := [ YulStmt.expr (YulExpr.call "mstore" [YulExpr.lit 0, valueIR]) diff --git a/Compiler/Proofs/IRGeneration/GenericInduction/Loops.lean b/Compiler/Proofs/IRGeneration/GenericInduction/Loops.lean index 4e3cb499b..231883397 100644 --- a/Compiler/Proofs/IRGeneration/GenericInduction/Loops.lean +++ b/Compiler/Proofs/IRGeneration/GenericInduction/Loops.lean @@ -886,7 +886,8 @@ theorem compiledStmtStep_forEach_literal_zero · dsimp [forEachZeroCompiledIR, forEachZeroInitStmts, forEachZeroCondExpr, forEachZeroPostStmts, forEachZeroBodyWithBind, forEachZeroIdxName, forEachZeroCountName, forEachZeroUsedNames] - simp [CompilationModel.compileStmt, CompilationModel.compileExpr, hbodyCompile] + simp [CompilationModel.compileStmt, CompilationModel.compileExprWithInternals, hbodyCompile, + Bind.bind, Except.bind, pure, Except.pure] · intro runtime state extraFuel hexact hscope hbounded hruntime hslack rcases forEachZero_fresh_facts (scope := scope) (varName := varName) (body := body) with ⟨hidx_ne_var, hcount_ne_var, hcount_ne_idx, hidx_not_scope, hcount_not_scope⟩ @@ -917,7 +918,7 @@ theorem compiledStmtStep_forEach_literal_empty · dsimp [forEachLiteralCompiledIR, forEachLiteralInitStmts, forEachLiteralIdxName, forEachLiteralCountName, forEachLiteralUsedNames, forEachLiteralBound] simp [CompilationModel.compileStmt, CompilationModel.compileStmtList, - CompilationModel.compileExpr, + CompilationModel.compileExprWithInternals, CompilationModel.uint256Modulus] rfl · intro runtime state extraFuel hexact hscope hbounded hruntime hslack diff --git a/Compiler/Proofs/IRGeneration/GenericInduction/Scope.lean b/Compiler/Proofs/IRGeneration/GenericInduction/Scope.lean index b248ef071..a139122e9 100644 --- a/Compiler/Proofs/IRGeneration/GenericInduction/Scope.lean +++ b/Compiler/Proofs/IRGeneration/GenericInduction/Scope.lean @@ -337,7 +337,7 @@ private theorem compileStmt_ite_ok_inv CompilationModel.compileStmtList fields [] [] .calldata [] false scope [] elseBranch = Except.ok elseIR := by unfold CompilationModel.compileStmt at hcompile - rcases hcond : CompilationModel.compileExpr fields .calldata cond with _ | condIR + rcases hcond : CompilationModel.compileExprWithInternals fields .calldata [] cond with _ | condIR · simp [hcond] at hcompile cases hcompile · simp [hcond] at hcompile @@ -351,7 +351,10 @@ private theorem compileStmt_ite_ok_inv · simp [helse] at hcompile cases hcompile · - simpa [hcond, hthen, helse] using + have hcondPublic : + CompilationModel.compileExpr fields .calldata cond = Except.ok condIR := by + simpa [CompilationModel.compileExprWithInternals_nil_eq] using hcond + simpa [hcondPublic, hthen, helse] using (show ∃ condIR thenIR elseIR, Except.ok condIR = Except.ok condIR ∧ Except.ok thenIR = Except.ok thenIR ∧ @@ -460,7 +463,7 @@ private theorem stmtListScopeCore_of_unsupportedContractSurface_eq_false cases hbody : CompilationModel.compileStmtList fields [] [] .calldata [] false (CompilationModel.forEachBodyScope scope varName (Expr.literal 0) body) [] body with - | error e => simp [CompilationModel.compileExpr, pure, Except.pure, hbody] at hhead + | error e => simp [CompilationModel.compileExprWithInternals, pure, Except.pure, hbody] at hhead | ok loopBodyIR => exact .forEachLiteralZero (stmtListScopeCore_of_unsupportedContractSurface_eq_false @@ -597,7 +600,7 @@ theorem stmtListScopeCore_prefix_of_compileStmtList_ok_of_stmtListTouchesUnsuppo cases hbody : CompilationModel.compileStmtList fields [] [] .calldata [] false (CompilationModel.forEachBodyScope scope varName (Expr.literal 0) body) [] body with - | error e => simp [CompilationModel.compileExpr, pure, Except.pure, hbody] at hhead + | error e => simp [CompilationModel.compileExprWithInternals, pure, Except.pure, hbody] at hhead | ok loopBodyIR => exact StmtListScopeCore.forEachLiteralZero (stmtListScopeCore_of_unsupportedContractSurface_eq_false diff --git a/Compiler/Proofs/IRGeneration/GenericInduction/Storage.lean b/Compiler/Proofs/IRGeneration/GenericInduction/Storage.lean index a6475ad01..cb37d4053 100644 --- a/Compiler/Proofs/IRGeneration/GenericInduction/Storage.lean +++ b/Compiler/Proofs/IRGeneration/GenericInduction/Storage.lean @@ -10,6 +10,22 @@ open Compiler open Compiler.CompilationModel open Compiler.Yul +attribute [local simp] CompilationModel.compileExprWithInternals_nil_eq + +private theorem compileExprWithInternals_nil_ok + {fields : List Field} {dynamicSource : DynamicDataSource} {expr : Expr} {exprIR : YulExpr} + (h : CompilationModel.compileExpr fields dynamicSource expr = Except.ok exprIR) : + CompilationModel.compileExprWithInternals fields dynamicSource [] expr = Except.ok exprIR := by + simpa [CompilationModel.compileExprWithInternals_nil_eq] using h + +private theorem compileExprListWithInternals_nil_ok + {fields : List Field} {dynamicSource : DynamicDataSource} {exprs : List Expr} + {exprIRs : List YulExpr} + (h : CompilationModel.compileExprList fields dynamicSource exprs = Except.ok exprIRs) : + CompilationModel.compileExprListWithInternals fields dynamicSource [] exprs = + Except.ok exprIRs := by + simpa [CompilationModel.compileExprListWithInternals_nil_eq] using h + private theorem encodeStorageAt_writeUintSlots_singleton_other {fields : List Field} {world : Verity.ContractState} @@ -2730,7 +2746,9 @@ theorem compiledStmtStep_mstore_single CompiledStmtStep fields scope (.mstore offset value) [YulStmt.expr (YulExpr.call "mstore" [offsetIR, valueIR])] where compileOk := by - simp only [CompilationModel.compileStmt, hoffsetIR, hvalueIR] + have hoffsetIRInternal := compileExprWithInternals_nil_ok hoffsetIR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR + simp only [CompilationModel.compileStmt, hoffsetIRInternal, hvalueIRInternal] rfl preserves := compiledStmtStep_mstore_single_preserves hcoreOffset hinScopeOffset hcoreValue hinScopeValue hoffsetIR hvalueIR @@ -2864,7 +2882,9 @@ theorem compiledStmtStep_tstore_single CompiledStmtStep fields scope (.tstore offset value) [YulStmt.expr (YulExpr.call "tstore" [offsetIR, valueIR])] where compileOk := by - simp only [CompilationModel.compileStmt, hoffsetIR, hvalueIR] + have hoffsetIRInternal := compileExprWithInternals_nil_ok hoffsetIR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR + simp only [CompilationModel.compileStmt, hoffsetIRInternal, hvalueIRInternal] rfl preserves := compiledStmtStep_tstore_single_preserves hcoreOffset hinScopeOffset hcoreValue hinScopeValue hoffsetIR hvalueIR @@ -3032,8 +3052,10 @@ theorem compiledStmtStep_setMappingUint_singleSlot_of_slotSafety (YulExpr.call "sstore" [YulExpr.call "mappingSlot" [YulExpr.lit slot, keyIR], valueIR])] where compileOk := by + have hkeyIRInternal := compileExprWithInternals_nil_ok hkeyIR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR simp only [CompilationModel.compileStmt, CompilationModel.compileMappingSlotWrite, - hmapping, hwriteSlots, hkeyIR, hvalueIR] + hmapping, hwriteSlots, hkeyIRInternal, hvalueIRInternal] rfl preserves := compiledStmtStep_setMappingUint_singleSlot_of_slotSafety_preserves hcoreKey hinScopeKey hcoreValue hinScopeValue hwriteSlots hslotSafety hkeyIR hvalueIR @@ -3045,7 +3067,9 @@ theorem compileExprList_core_ok ∃ exprIRs, CompilationModel.compileExprList fields .calldata exprs = Except.ok exprIRs := by induction exprs with | nil => - exact ⟨[], rfl⟩ + exact ⟨[], by + simp [CompilationModel.compileExprList, CompilationModel.compileExprListWithInternals, + pure, Except.pure]⟩ | cons expr rest ih => have hhead : FunctionBody.ExprCompileCore expr := hcore expr (by simp) have htail : ∀ e ∈ rest, FunctionBody.ExprCompileCore e := by @@ -3053,9 +3077,11 @@ theorem compileExprList_core_ok exact hcore e (by simp [he]) rcases FunctionBody.compileExpr_core_ok (fields := fields) hhead with ⟨exprIR, hexprIR⟩ rcases ih htail with ⟨restIR, hrestIR⟩ + have hexprIRInternal := compileExprWithInternals_nil_ok hexprIR + have hrestIRInternal := compileExprListWithInternals_nil_ok hrestIR exact ⟨exprIR :: restIR, by - rw [CompilationModel.compileExprList, hexprIR, hrestIR] - rfl + simp [CompilationModel.compileExprList, CompilationModel.compileExprListWithInternals, + hexprIRInternal, hrestIRInternal, bind, Except.bind, pure, Except.pure] ⟩ theorem compileStmt_emit_scalar_supported_ok @@ -3093,9 +3119,10 @@ theorem compileStmt_emit_scalar_supported_ok have hscalarCompile : eventDefScalarCompileSupported eventDef = true := by simpa [eventDefScalarProofSupported] using hscalar + have hargExprsInternal := compileExprListWithInternals_nil_ok hargExprs refine ⟨compileScalarEmitFromCompiledArgs eventDef args argExprs, ?_⟩ simp only [CompilationModel.compileStmt, CompilationModel.compileEmit] - simp [hfind, hlen, hargExprs, hindexedGuard, hscalarCompile, + simp [hfind, hlen, hargExprsInternal, hindexedGuard, hscalarCompile, Bind.bind, Except.bind, pure, Except.pure] /-- Fill the event-head compile obligation from the scalar `.emit` compile @@ -3198,7 +3225,7 @@ theorem eval_compileExprList_core_of_scope List.Forall₂ (fun exprIR value => evalIRExpr state exprIR = some value) exprIRs values := by induction exprs generalizing exprIRs with | nil => - simp [CompilationModel.compileExprList] at hcompiled + simp [CompilationModel.compileExprList, CompilationModel.compileExprListWithInternals] at hcompiled cases hcompiled exact ⟨[], rfl, .nil⟩ | cons expr rest ih => @@ -3213,7 +3240,10 @@ theorem eval_compileExprList_core_of_scope exact hinScope expr' (by simp [hexpr']) rcases compileExprList_core_ok (fields := fields) htail with ⟨restIRs, hrestIRs⟩ rcases FunctionBody.compileExpr_core_ok (fields := fields) hhead with ⟨exprIR, hexprIR⟩ - rw [CompilationModel.compileExprList, hexprIR, hrestIRs] at hcompiled + have hexprIRInternal := compileExprWithInternals_nil_ok hexprIR + have hrestIRsInternal := compileExprListWithInternals_nil_ok hrestIRs + simp [CompilationModel.compileExprList, CompilationModel.compileExprListWithInternals, + hexprIRInternal, hrestIRsInternal] at hcompiled injection hcompiled with hcompiledTail subst hcompiledTail rcases eval_compileExpr_core_some_of_scope @@ -3551,8 +3581,10 @@ theorem compiledStmtStep_setMappingChain_singleSlot_of_slotSafety (fun slotExpr keyExpr => YulExpr.call "mappingSlot" [slotExpr, keyExpr]) (YulExpr.lit slot), valueIR])] where compileOk := by + have hkeyIRsInternal := compileExprListWithInternals_nil_ok hkeyIRs + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR simp only [CompilationModel.compileStmt, CompilationModel.compileSetMappingChain, - hmapping, hwriteSlots, hkeyIRs, hvalueIR] + hmapping, hwriteSlots, hkeyIRsInternal, hvalueIRInternal] rfl preserves := compiledStmtStep_setMappingChain_singleSlot_of_slotSafety_preserves hcoreKeys hinScopeKeys hcoreValue hinScopeValue hwriteSlots hslotSafety hkeyIRs hvalueIR @@ -3720,8 +3752,10 @@ theorem compiledStmtStep_setMapping_singleSlot_of_slotSafety (YulExpr.call "sstore" [YulExpr.call "mappingSlot" [YulExpr.lit slot, keyIR], valueIR])] where compileOk := by + have hkeyIRInternal := compileExprWithInternals_nil_ok hkeyIR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR simp only [CompilationModel.compileStmt, CompilationModel.compileMappingSlotWrite, - hmapping, hwriteSlots, hkeyIR, hvalueIR] + hmapping, hwriteSlots, hkeyIRInternal, hvalueIRInternal] rfl preserves := compiledStmtStep_setMapping_singleSlot_of_slotSafety_preserves hcoreKey hinScopeKey hcoreValue hinScopeValue hwriteSlots hslotSafety hkeyIR hvalueIR @@ -3982,8 +4016,10 @@ theorem compiledStmtStep_setMappingWord_singleSlot_of_slotSafety if wordOffset == 0 then mappingBase else YulExpr.call "add" [mappingBase, YulExpr.lit wordOffset], valueIR])] where compileOk := by + have hkeyIRInternal := compileExprWithInternals_nil_ok hkeyIR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR simp only [CompilationModel.compileStmt, CompilationModel.compileMappingSlotWrite, - hmapping, hwriteSlots, hkeyIR, hvalueIR] + hmapping, hwriteSlots, hkeyIRInternal, hvalueIRInternal] rfl preserves := compiledStmtStep_setMappingWord_singleSlot_of_slotSafety_preserves hcoreKey hinScopeKey hcoreValue hinScopeValue hwriteSlots hslotSafety hkeyIR hvalueIR @@ -4819,8 +4855,10 @@ theorem compiledStmtStep_setMappingPackedWord_singleSlot_of_slotSafety YulExpr.call "shl" [YulExpr.lit packed.offset, YulExpr.ident "__compat_packed"]]])]] where compileOk := by + have hkeyIRInternal := compileExprWithInternals_nil_ok hkeyIR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR simp only [CompilationModel.compileStmt, CompilationModel.compileMappingPackedSlotWrite, - hmapping, hpacked, hwriteSlots, hkeyIR, hvalueIR, Bool.not_true, bne_self_eq_false, + hmapping, hpacked, hwriteSlots, hkeyIRInternal, hvalueIRInternal, Bool.not_true, bne_self_eq_false, ite_false, ite_true, pure, Except.pure, bind, Except.bind] rfl preserves := compiledStmtStep_setMappingPackedWord_singleSlot_of_slotSafety_preserves @@ -4842,9 +4880,8 @@ private theorem compiledStmtStep_setStructMember_singleSlot_of_slotSafety_preser (hcoreValue : FunctionBody.ExprCompileCore value) (hinScopeValue : FunctionBody.exprBoundNamesInScope value scope) (hmembers : findStructMembers fields fieldName = some members) - (hmember : - findStructMember members memberName = - some { name := memberName, wordOffset := wordOffset, packed := none }) + (hmember : findStructMember members memberName = + some { name := memberName, wordOffset := wordOffset, packed := none }) (hwriteSlots : findFieldWriteSlots fields fieldName = some [slot]) (hslotSafety : ∀ runtime keyNat, @@ -5082,9 +5119,11 @@ theorem compiledStmtStep_setStructMember_singleSlot_of_slotSafety if wordOffset == 0 then mappingBase else YulExpr.call "add" [mappingBase, YulExpr.lit wordOffset], valueIR])] where compileOk := by + have hkeyIRInternal := compileExprWithInternals_nil_ok hkeyIR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR simp only [CompilationModel.compileStmt, CompilationModel.compileSetStructMember, CompilationModel.compileMappingSlotWrite, hmapping, hnotMapping2, hmembers, hmember, - hwriteSlots, hkeyIR, hvalueIR] + hwriteSlots, hkeyIRInternal, hvalueIRInternal] rfl preserves := compiledStmtStep_setStructMember_singleSlot_of_slotSafety_preserves hcoreKey hinScopeKey hcoreValue hinScopeValue hmembers hmember hwriteSlots @@ -5097,10 +5136,8 @@ private theorem compiledStmtStep_setMapping2_singleSlot_of_slotSafety_preserves {key1 key2 value : Expr} {key1IR key2IR valueIR : YulExpr} {slot : Nat} - (hcoreKey1 : FunctionBody.ExprCompileCore key1) - (hinScopeKey1 : FunctionBody.exprBoundNamesInScope key1 scope) - (hcoreKey2 : FunctionBody.ExprCompileCore key2) - (hinScopeKey2 : FunctionBody.exprBoundNamesInScope key2 scope) + (hcoreKey1 : FunctionBody.ExprCompileCore key1) (hinScopeKey1 : FunctionBody.exprBoundNamesInScope key1 scope) + (hcoreKey2 : FunctionBody.ExprCompileCore key2) (hinScopeKey2 : FunctionBody.exprBoundNamesInScope key2 scope) (hcoreValue : FunctionBody.ExprCompileCore value) (hinScopeValue : FunctionBody.exprBoundNamesInScope value scope) (hwriteSlots : findFieldWriteSlots fields fieldName = some [slot]) @@ -5269,10 +5306,8 @@ theorem compiledStmtStep_setMapping2_singleSlot_of_slotSafety {key1IR key2IR valueIR : YulExpr} {slot : Nat} (hmapping2 : isMapping2 fields fieldName = true) - (hcoreKey1 : FunctionBody.ExprCompileCore key1) - (hinScopeKey1 : FunctionBody.exprBoundNamesInScope key1 scope) - (hcoreKey2 : FunctionBody.ExprCompileCore key2) - (hinScopeKey2 : FunctionBody.exprBoundNamesInScope key2 scope) + (hcoreKey1 : FunctionBody.ExprCompileCore key1) (hinScopeKey1 : FunctionBody.exprBoundNamesInScope key1 scope) + (hcoreKey2 : FunctionBody.ExprCompileCore key2) (hinScopeKey2 : FunctionBody.exprBoundNamesInScope key2 scope) (hcoreValue : FunctionBody.ExprCompileCore value) (hinScopeValue : FunctionBody.exprBoundNamesInScope value scope) (hwriteSlots : findFieldWriteSlots fields fieldName = some [slot]) @@ -5297,8 +5332,11 @@ theorem compiledStmtStep_setMapping2_singleSlot_of_slotSafety [YulExpr.call "mappingSlot" [YulExpr.call "mappingSlot" [YulExpr.lit slot, key1IR], key2IR], valueIR])] where compileOk := by + have hkey1IRInternal := compileExprWithInternals_nil_ok hkey1IR + have hkey2IRInternal := compileExprWithInternals_nil_ok hkey2IR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR simp only [CompilationModel.compileStmt, CompilationModel.compileSetMapping2, - hmapping2, hwriteSlots, hkey1IR, hkey2IR, hvalueIR] + hmapping2, hwriteSlots, hkey1IRInternal, hkey2IRInternal, hvalueIRInternal] rfl preserves := compiledStmtStep_setMapping2_singleSlot_of_slotSafety_preserves hcoreKey1 hinScopeKey1 hcoreKey2 hinScopeKey2 hcoreValue hinScopeValue @@ -5622,22 +5660,21 @@ theorem compiledStmtStep_setMapping2Word_singleSlot_of_slotSafety if wordOffset == 0 then mappingSlot2 else YulExpr.call "add" [mappingSlot2, YulExpr.lit wordOffset], valueIR])] where compileOk := by + have hkey1IRInternal := compileExprWithInternals_nil_ok hkey1IR + have hkey2IRInternal := compileExprWithInternals_nil_ok hkey2IR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR simp only [CompilationModel.compileStmt, CompilationModel.compileSetMapping2Word, - hmapping2, hwriteSlots, hkey1IR, hkey2IR, hvalueIR] + hmapping2, hwriteSlots, hkey1IRInternal, hkey2IRInternal, hvalueIRInternal] rfl preserves := compiledStmtStep_setMapping2Word_singleSlot_of_slotSafety_preserves hcoreKey1 hinScopeKey1 hcoreKey2 hinScopeKey2 hcoreValue hinScopeValue hwriteSlots hslotSafety hkey1IR hkey2IR hvalueIR private theorem compiledStmtStep_setStructMember2_singleSlot_of_slotSafety_preserves - {fields : List Field} - {scope : List String} - {fieldName memberName : String} - {key1 key2 value : Expr} - {wordOffset : Nat} - {members : List StructMember} - {key1IR key2IR valueIR : YulExpr} - {slot : Nat} + {fields : List Field} {scope : List String} + {fieldName memberName : String} {key1 key2 value : Expr} + {wordOffset : Nat} {members : List StructMember} + {key1IR key2IR valueIR : YulExpr} {slot : Nat} (hcoreKey1 : FunctionBody.ExprCompileCore key1) (hinScopeKey1 : FunctionBody.exprBoundNamesInScope key1 scope) (hcoreKey2 : FunctionBody.ExprCompileCore key2) @@ -5645,9 +5682,8 @@ private theorem compiledStmtStep_setStructMember2_singleSlot_of_slotSafety_prese (hcoreValue : FunctionBody.ExprCompileCore value) (hinScopeValue : FunctionBody.exprBoundNamesInScope value scope) (hmembers : findStructMembers fields fieldName = some members) - (hmember : - findStructMember members memberName = - some { name := memberName, wordOffset := wordOffset, packed := none }) + (hmember : findStructMember members memberName = + some { name := memberName, wordOffset := wordOffset, packed := none }) (hwriteSlots : findFieldWriteSlots fields fieldName = some [slot]) (hslotSafety : ∀ runtime keyNat1 keyNat2, @@ -5919,25 +5955,18 @@ private theorem compiledStmtStep_setStructMember2_singleSlot_of_slotSafety_prese hexact', hbounded, hscope'⟩ theorem compiledStmtStep_setStructMember2_singleSlot_of_slotSafety - {fields : List Field} - {scope : List String} - {fieldName memberName : String} - {key1 key2 value : Expr} - {wordOffset : Nat} - {members : List StructMember} - {key1IR key2IR valueIR : YulExpr} - {slot : Nat} + {fields : List Field} {scope : List String} + {fieldName memberName : String} {key1 key2 value : Expr} + {wordOffset : Nat} {members : List StructMember} + {key1IR key2IR valueIR : YulExpr} {slot : Nat} (hmapping2 : isMapping2 fields fieldName = true) - (hcoreKey1 : FunctionBody.ExprCompileCore key1) - (hinScopeKey1 : FunctionBody.exprBoundNamesInScope key1 scope) - (hcoreKey2 : FunctionBody.ExprCompileCore key2) - (hinScopeKey2 : FunctionBody.exprBoundNamesInScope key2 scope) + (hcoreKey1 : FunctionBody.ExprCompileCore key1) (hinScopeKey1 : FunctionBody.exprBoundNamesInScope key1 scope) + (hcoreKey2 : FunctionBody.ExprCompileCore key2) (hinScopeKey2 : FunctionBody.exprBoundNamesInScope key2 scope) (hcoreValue : FunctionBody.ExprCompileCore value) (hinScopeValue : FunctionBody.exprBoundNamesInScope value scope) (hmembers : findStructMembers fields fieldName = some members) - (hmember : - findStructMember members memberName = - some { name := memberName, wordOffset := wordOffset, packed := none }) + (hmember : findStructMember members memberName = + some { name := memberName, wordOffset := wordOffset, packed := none }) (hwriteSlots : findFieldWriteSlots fields fieldName = some [slot]) (hslotSafety : ∀ runtime keyNat1 keyNat2, @@ -5958,8 +5987,12 @@ theorem compiledStmtStep_setStructMember2_singleSlot_of_slotSafety if wordOffset == 0 then mappingSlot2 else YulExpr.call "add" [mappingSlot2, YulExpr.lit wordOffset], valueIR])] where compileOk := by + have hkey1IRInternal := compileExprWithInternals_nil_ok hkey1IR + have hkey2IRInternal := compileExprWithInternals_nil_ok hkey2IR + have hvalueIRInternal := compileExprWithInternals_nil_ok hvalueIR simp only [CompilationModel.compileStmt, CompilationModel.compileSetStructMember2, - hmapping2, hmembers, hmember, hwriteSlots, hkey1IR, hkey2IR, hvalueIR] + hmapping2, hmembers, hmember, hwriteSlots, hkey1IRInternal, hkey2IRInternal, + hvalueIRInternal] rfl preserves := compiledStmtStep_setStructMember2_singleSlot_of_slotSafety_preserves hcoreKey1 hinScopeKey1 hcoreKey2 hinScopeKey2 hcoreValue hinScopeValue @@ -6456,8 +6489,9 @@ theorem compiledStmtStep_ite preserves := ?_ } · show CompilationModel.compileStmt fields [] [] .calldata [] false scope [] (.ite cond thenBranch elseBranch) = Except.ok compiledIR + have hcondIRInternal := compileExprWithInternals_nil_ok hcondIR unfold CompilationModel.compileStmt - simp only [hcondIR, hthenIR, helseIR, Except.bind, helseNonempty, ↓reduceIte] + simp only [hcondIRInternal, hthenIR, helseIR, Except.bind, helseNonempty, ↓reduceIte] rfl · intro runtime state extraFuel hexact hscope hbounded hruntime hslack set wholeExtraFuel := extraFuel - (sizeOf compiledIR - compiledIR.length) with hWF @@ -6720,7 +6754,8 @@ private theorem compiledStmtStep_letStorageField [YulStmt.let_ tmp (YulExpr.call "sload" [YulExpr.lit slot])] where compileOk := by have hNotMapping := isMapping_false_of_findFieldWithResolvedSlot_uint256 hfind rfl - simp only [CompilationModel.compileStmt, CompilationModel.compileExpr, hNotMapping, hfind] + simp only [CompilationModel.compileStmt, CompilationModel.compileExprWithInternals, + hNotMapping, hfind] rfl preserves runtime state extraFuel hexact hscope hbounded hruntime hslack := by have hEvalSrc : SourceSemantics.evalExpr fields runtime (.storage fieldName) = @@ -6798,7 +6833,8 @@ private theorem compiledStmtStep_letStorageAddrField [YulStmt.let_ tmp (YulExpr.call "sload" [YulExpr.lit slot])] where compileOk := by have hNotMapping := isMapping_false_of_findFieldWithResolvedSlot_address hfind rfl - simp only [CompilationModel.compileStmt, CompilationModel.compileExpr, hNotMapping, hfind] + simp only [CompilationModel.compileStmt, CompilationModel.compileExprWithInternals, + hNotMapping, hfind] rfl preserves runtime state extraFuel hexact hscope hbounded hruntime hslack := by have hEvalSrc : SourceSemantics.evalExpr fields runtime (.storageAddr fieldName) = @@ -6877,7 +6913,8 @@ private theorem compiledStmtStep_assignStorageField [YulStmt.assign name (YulExpr.call "sload" [YulExpr.lit slot])] where compileOk := by have hNotMapping := isMapping_false_of_findFieldWithResolvedSlot_uint256 hfind rfl - simp only [CompilationModel.compileStmt, CompilationModel.compileExpr, hNotMapping, hfind] + simp only [CompilationModel.compileStmt, CompilationModel.compileExprWithInternals, + hNotMapping, hfind] rfl preserves runtime state extraFuel hexact hscope hbounded hruntime hslack := by have hEvalSrc : SourceSemantics.evalExpr fields runtime (.storage fieldName) = @@ -6955,7 +6992,8 @@ private theorem compiledStmtStep_assignStorageAddrField [YulStmt.assign name (YulExpr.call "sload" [YulExpr.lit slot])] where compileOk := by have hNotMapping := isMapping_false_of_findFieldWithResolvedSlot_address hfind rfl - simp only [CompilationModel.compileStmt, CompilationModel.compileExpr, hNotMapping, hfind] + simp only [CompilationModel.compileStmt, CompilationModel.compileExprWithInternals, + hNotMapping, hfind] rfl preserves runtime state extraFuel hexact hscope hbounded hruntime hslack := by have hEvalSrc : SourceSemantics.evalExpr fields runtime (.storageAddr fieldName) = diff --git a/Compiler/Proofs/IRGeneration/IRInterpreter.lean b/Compiler/Proofs/IRGeneration/IRInterpreter.lean index 620c0c1a1..e5c96b4ca 100644 --- a/Compiler/Proofs/IRGeneration/IRInterpreter.lean +++ b/Compiler/Proofs/IRGeneration/IRInterpreter.lean @@ -5574,12 +5574,12 @@ theorem compileStmt_internalCallAssign_shape compiledIR = [YulStmt.letMany names (YulExpr.call (CompilationModel.internalFunctionYulName functionName) argExprs)] := by simp only [CompilationModel.compileStmt, bind, Except.bind] at hok - match hargs : CompilationModel.compileExprList fields .calldata args with + match hargs : CompilationModel.compileExprListWithInternals fields .calldata [] args with | .error e => simp [CompilationModel.compileInternalCallArgs, CompilationModel.findInternalFunctionForCall?, hargs] at hok | .ok argExprs => - refine ⟨argExprs, rfl, ?_⟩ + refine ⟨argExprs, by simpa [CompilationModel.compileExprList] using hargs, ?_⟩ simp [CompilationModel.compileInternalCallArgs, CompilationModel.findInternalFunctionForCall?, hargs, pure, Except.pure] at hok exact hok.symm @@ -5598,12 +5598,12 @@ theorem compileStmt_internalCall_shape compiledIR = [YulStmt.expr (YulExpr.call (CompilationModel.internalFunctionYulName functionName) argExprs)] := by simp only [CompilationModel.compileStmt, bind, Except.bind] at hok - match hargs : CompilationModel.compileExprList fields .calldata args with + match hargs : CompilationModel.compileExprListWithInternals fields .calldata [] args with | .error e => simp [CompilationModel.compileInternalCallArgs, CompilationModel.findInternalFunctionForCall?, hargs] at hok | .ok argExprs => - refine ⟨argExprs, rfl, ?_⟩ + refine ⟨argExprs, by simpa [CompilationModel.compileExprList] using hargs, ?_⟩ simp [CompilationModel.compileInternalCallArgs, CompilationModel.findInternalFunctionForCall?, hargs, pure, Except.pure] at hok exact hok.symm diff --git a/Compiler/Proofs/IRGeneration/IntrinsicProofs.lean b/Compiler/Proofs/IRGeneration/IntrinsicProofs.lean index 6a8456e9b..3adef52c1 100644 --- a/Compiler/Proofs/IRGeneration/IntrinsicProofs.lean +++ b/Compiler/Proofs/IRGeneration/IntrinsicProofs.lean @@ -55,21 +55,56 @@ theorem verbatim_lowering_hexLiteral some s!"hex\"{opcodeHex}\"" := by rfl +private theorem compileExprWithInternals_param + (fields : List Field) (dynamicSource : DynamicDataSource) (x : String) : + compileExprWithInternals fields dynamicSource [] (.param x) = + .ok (YulExpr.ident x) := by + unfold compileExprWithInternals + rfl + +private theorem compileExprListWithInternals_nil + (fields : List Field) (dynamicSource : DynamicDataSource) : + compileExprListWithInternals fields dynamicSource [] [] = + .ok [] := by + unfold compileExprListWithInternals + rfl + +private theorem compileExprListWithInternals_param_one + (fields : List Field) (dynamicSource : DynamicDataSource) (x : String) : + compileExprListWithInternals fields dynamicSource [] [.param x] = + .ok [YulExpr.ident x] := by + unfold compileExprListWithInternals + rw [compileExprWithInternals_param, compileExprListWithInternals_nil] + rfl + +private theorem compileExprListWithInternals_param_two + (fields : List Field) (dynamicSource : DynamicDataSource) (x y : String) : + compileExprListWithInternals fields dynamicSource [] [.param x, .param y] = + .ok [YulExpr.ident x, YulExpr.ident y] := by + unfold compileExprListWithInternals + rw [compileExprWithInternals_param, compileExprListWithInternals_param_one] + rfl + theorem compileExpr_intrinsic_verbatim_one_param (fields : List Field) (dynamicSource : DynamicDataSource) (name opcodeHex x : String) : - compileExpr fields dynamicSource (.intrinsic name (.verbatim 1 1 opcodeHex) .cancun [.param x]) = + compileExpr fields dynamicSource (.intrinsic name (.verbatim 1 1 opcodeHex) .cancun [.param x]) = .ok (YulExpr.call s!"verbatim_{1}i_{1}o" [YulExpr.verbatimHex opcodeHex, YulExpr.ident x]) := by - simp [compileExpr, compileExprList, YulLowering.callName, Pure.pure, Except.pure, - bind, Except.bind] + unfold compileExpr + unfold compileExprWithInternals + rw [compileExprListWithInternals_param_one] + rfl theorem compileExpr_intrinsic_builtin_one_param (fields : List Field) (dynamicSource : DynamicDataSource) (name x : String) : - compileExpr fields dynamicSource (.intrinsic name (.builtin "not") .cancun [.param x]) = + compileExpr fields dynamicSource (.intrinsic name (.builtin "not") .cancun [.param x]) = .ok (YulExpr.call "not" [YulExpr.ident x]) := by - simp [compileExpr, compileExprList, yulBuiltinArity?, Pure.pure, Except.pure, bind, Except.bind] + unfold compileExpr + unfold compileExprWithInternals + rw [compileExprListWithInternals_param_one] + rfl theorem compileExpr_intrinsic_verbatim_zero_output_error (fields : List Field) (dynamicSource : DynamicDataSource) @@ -79,7 +114,10 @@ theorem compileExpr_intrinsic_verbatim_zero_output_error .error msg := by refine ⟨toString "Compilation error: intrinsic " ++ toString name ++ toString " must produce exactly 1 output, got " ++ toString 0 ++ toString "", ?_⟩ - simp [compileExpr, compileExprList, Pure.pure, Except.pure, bind, Except.bind] + unfold compileExpr + unfold compileExprWithInternals + rw [compileExprListWithInternals_param_one] + rfl theorem compileExpr_intrinsic_verbatim_wrong_arity_error (fields : List Field) (dynamicSource : DynamicDataSource) @@ -90,7 +128,10 @@ theorem compileExpr_intrinsic_verbatim_wrong_arity_error refine ⟨toString "Compilation error: intrinsic " ++ toString name ++ toString " expects " ++ toString 1 ++ toString " arg(s), got " ++ toString 2 ++ toString "", ?_⟩ - simp [compileExpr, compileExprList, Pure.pure, Except.pure, bind, Except.bind] + unfold compileExpr + unfold compileExprWithInternals + rw [compileExprListWithInternals_param_two] + rfl end IntrinsicProofs diff --git a/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanBodyClosure/Base.lean b/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanBodyClosure/Base.lean index 4485f2bb9..ac76753d2 100644 --- a/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanBodyClosure/Base.lean +++ b/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanBodyClosure/Base.lean @@ -702,7 +702,7 @@ theorem compileStmt_pure_binding_bridged cases hStmt with | letVar name value hValue => simp only [compileStmt, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => @@ -717,7 +717,7 @@ theorem compileStmt_pure_binding_bridged (BridgedStraightStmt.let_ name valueExpr hBridged) | assignVar name value hValue => simp only [compileStmt, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => @@ -746,7 +746,7 @@ theorem compileStmt_pure_binding_noFuncDefs cases hStmt with | letVar name value _hValue => simp only [compileStmt, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => @@ -755,7 +755,7 @@ theorem compileStmt_pure_binding_noFuncDefs simp [Native.yulStmtContainsFuncDef] | assignVar name value _hValue => simp only [compileStmt, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => @@ -830,7 +830,7 @@ theorem compileStmt_setStorage_singleSlot_pure_bridged | adt name maxFields => exact False.elim (hNotAdt name maxFields hty) | uint256 | address | dynamicArray | mappingTyped | mappingStruct | mappingStruct2 => - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr, hty] at hOk | ok valueExpr => @@ -869,7 +869,7 @@ theorem compileStmt_setStorage_singleSlot_pure_noFuncDefs | adt name maxFields => exact False.elim (hNotAdt name maxFields hty) | uint256 | address | dynamicArray | mappingTyped | mappingStruct | mappingStruct2 => - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr, hty] at hOk | ok valueExpr => @@ -987,7 +987,7 @@ private theorem compileStmt_return_external_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => simp [hExpr, Pure.pure, Except.pure] at hOk @@ -1013,7 +1013,7 @@ private theorem compileStmt_return_external_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => simp [hExpr, Pure.pure, Except.pure] at hOk @@ -1088,7 +1088,7 @@ theorem compileStmt_return_internal_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => cases internalRetNames with @@ -1117,7 +1117,7 @@ theorem compileStmt_return_internal_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => cases internalRetNames with @@ -1283,7 +1283,7 @@ theorem compileStmt_require_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hFail : compileRequireFailCond fields dynamicSource cond with + cases hFail : compileRequireFailCondWithInternals fields dynamicSource [] cond with | error err => simp [hFail] at hOk | ok failCond => @@ -1310,7 +1310,7 @@ theorem compileStmt_require_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hFail : compileRequireFailCond fields dynamicSource cond with + cases hFail : compileRequireFailCondWithInternals fields dynamicSource [] cond with | error err => simp [hFail] at hOk | ok failCond => @@ -1397,10 +1397,10 @@ theorem compileStmt_setMapping_singleSlot_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -1427,10 +1427,10 @@ theorem compileStmt_setMappingUint_singleSlot_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -1454,10 +1454,10 @@ theorem compileStmt_setMapping_singleSlot_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -1478,10 +1478,10 @@ theorem compileStmt_setMappingUint_singleSlot_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -1830,7 +1830,7 @@ mutual dynamicSource internalRetNames inScopeNames hBase hOk | ite cond thenBranch elseBranch hCond hThen hElse => simp only [compileStmt, bind, Except.bind] at hOk - cases hCondExpr : compileExpr fields dynamicSource cond with + cases hCondExpr : compileExprWithInternals fields dynamicSource [] cond with | error err => simp [hCondExpr] at hOk | ok condExpr => @@ -1937,7 +1937,7 @@ mutual dynamicSource internalRetNames inScopeNames hBase hOk | ite cond thenBranch elseBranch _ hThen hElse => simp only [compileStmt, bind, Except.bind] at hOk - cases hCondExpr : compileExpr fields dynamicSource cond with + cases hCondExpr : compileExprWithInternals fields dynamicSource [] cond with | error err => simp [hCondExpr] at hOk | ok condExpr => cases hThenCompile : compileStmtList fields events errors dynamicSource @@ -2020,7 +2020,7 @@ mutual dynamicSource internalRetNames inScopeNames hBase hOk | ite cond thenBranch elseBranch hCond hThen hElse => simp only [compileStmt, bind, Except.bind] at hOk - cases hCondExpr : compileExpr fields dynamicSource cond with + cases hCondExpr : compileExprWithInternals fields dynamicSource [] cond with | error err => simp [hCondExpr] at hOk | ok condExpr => @@ -2127,7 +2127,7 @@ mutual dynamicSource internalRetNames inScopeNames hBase hOk | ite cond thenBranch elseBranch _ hThen hElse => simp only [compileStmt, bind, Except.bind] at hOk - cases hCondExpr : compileExpr fields dynamicSource cond with + cases hCondExpr : compileExprWithInternals fields dynamicSource [] cond with | error err => simp [hCondExpr] at hOk | ok condExpr => cases hThenCompile : compileStmtList fields events errors dynamicSource @@ -2226,11 +2226,11 @@ theorem compileStmt_memoryWrite_bridged cases hStmt with | mstore offset value hOffset hValue => simp only [compileStmt, bind, Except.bind] at hOk - cases hOExpr : compileExpr fields dynamicSource offset with + cases hOExpr : compileExprWithInternals fields dynamicSource [] offset with | error err => simp [hOExpr] at hOk | ok offsetExpr => simp [hOExpr] at hOk - cases hVExpr : compileExpr fields dynamicSource value with + cases hVExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hVExpr] at hOk | ok valueExpr => simp [hVExpr, Pure.pure, Except.pure] at hOk @@ -2246,11 +2246,11 @@ theorem compileStmt_memoryWrite_bridged (BridgedStraightStmt.expr_mstore offsetExpr valueExpr hBO hBV) | tstore offset value hOffset hValue => simp only [compileStmt, bind, Except.bind] at hOk - cases hOExpr : compileExpr fields dynamicSource offset with + cases hOExpr : compileExprWithInternals fields dynamicSource [] offset with | error err => simp [hOExpr] at hOk | ok offsetExpr => simp [hOExpr] at hOk - cases hVExpr : compileExpr fields dynamicSource value with + cases hVExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hVExpr] at hOk | ok valueExpr => simp [hVExpr, Pure.pure, Except.pure] at hOk @@ -2278,11 +2278,11 @@ theorem compileStmt_memoryWrite_noFuncDefs cases hStmt with | mstore offset value _ _ => simp only [compileStmt, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hO : compileExpr fields dynamicSource offset with + cases hO : compileExprWithInternals fields dynamicSource [] offset with | error err => simp [hO] at hOk | ok compiledOffset => simp [hO] at hOk - cases hV : compileExpr fields dynamicSource value with + cases hV : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hV] at hOk | ok compiledValue => simp [hV, Native.yulStmtContainsFuncDef] at hOk @@ -2290,11 +2290,11 @@ theorem compileStmt_memoryWrite_noFuncDefs simp [Native.yulStmtContainsFuncDef] | tstore offset value _ _ => simp only [compileStmt, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hO : compileExpr fields dynamicSource offset with + cases hO : compileExprWithInternals fields dynamicSource [] offset with | error err => simp [hO] at hOk | ok compiledOffset => simp [hO] at hOk - cases hV : compileExpr fields dynamicSource value with + cases hV : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hV] at hOk | ok compiledValue => simp [hV, Native.yulStmtContainsFuncDef] at hOk @@ -2330,7 +2330,7 @@ theorem compileStmt_forEach_with_bridged_body BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hCExpr : compileExpr fields dynamicSource count with + cases hCExpr : compileExprWithInternals fields dynamicSource [] count with | error err => simp [hCExpr] at hOk | ok countExpr => simp [hCExpr] at hOk @@ -2418,7 +2418,7 @@ theorem compileStmt_ite_with_noFuncDefs_body Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hCondExpr : compileExpr fields dynamicSource cond with + cases hCondExpr : compileExprWithInternals fields dynamicSource [] cond with | error err => simp [hCondExpr] at hOk | ok condExpr => cases hThenCompile : compileStmtList fields events errors dynamicSource @@ -2456,7 +2456,7 @@ theorem compileStmt_forEach_with_noFuncDefs_body Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hCExpr : compileExpr fields dynamicSource count with + cases hCExpr : compileExprWithInternals fields dynamicSource [] count with | error err => simp [hCExpr] at hOk | ok countExpr => simp [hCExpr] at hOk @@ -2684,7 +2684,7 @@ theorem compileStmt_revertError_zero_bridged inScopeNames [] (.revertError errorName []) = .ok out → BridgedStmts out := by intro out hOk - simp only [compileStmt, bind, Except.bind, hLookup, compileExprList, + simp only [compileStmt, bind, Except.bind, hLookup, compileExprListWithInternals, Pure.pure, Except.pure] at hOk exact revertWithCustomError_zero_bridged dynamicSource errorDef hZeroParams hOk @@ -2706,10 +2706,10 @@ theorem compileStmt_requireError_zero_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hFail : compileRequireFailCond fields dynamicSource cond with + cases hFail : compileRequireFailCondWithInternals fields dynamicSource [] cond with | error err => simp [hFail] at hOk | ok failCond => - simp [hFail, hLookup, compileExprList, Pure.pure, Except.pure] at hOk + simp [hFail, hLookup, compileExprListWithInternals, Pure.pure, Except.pure] at hOk cases hRevert : revertWithCustomError dynamicSource errorDef [] [] with | error err => simp [hRevert] at hOk | ok revertStmts => @@ -2754,7 +2754,7 @@ theorem compileStmt_revertError_zero_noFuncDefs inScopeNames [] (.revertError errorName []) = .ok out → Native.yulStmtsContainFuncDef out = false := by intro out hOk - simp only [compileStmt, bind, Except.bind, hLookup, compileExprList, + simp only [compileStmt, bind, Except.bind, hLookup, compileExprListWithInternals, Pure.pure, Except.pure] at hOk exact revertWithCustomError_zero_noFuncDefs dynamicSource errorDef hZeroParams hOk @@ -2774,10 +2774,10 @@ theorem compileStmt_requireError_zero_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hFail : compileRequireFailCond fields dynamicSource cond with + cases hFail : compileRequireFailCondWithInternals fields dynamicSource [] cond with | error err => simp [hFail] at hOk | ok failCond => - simp [hFail, hLookup, compileExprList, Pure.pure, Except.pure] at hOk + simp [hFail, hLookup, compileExprListWithInternals, Pure.pure, Except.pure] at hOk cases hRevert : revertWithCustomError dynamicSource errorDef [] [] with | error err => simp [hRevert] at hOk | ok revertStmts => @@ -3210,7 +3210,7 @@ mutual dynamicSource internalRetNames inScopeNames hBase hOk | ite cond thenBranch elseBranch hCond hThen hElse => simp only [compileStmt, bind, Except.bind] at hOk - cases hCondExpr : compileExpr fields dynamicSource cond with + cases hCondExpr : compileExprWithInternals fields dynamicSource [] cond with | error err => simp [hCondExpr] at hOk | ok condExpr => @@ -3327,7 +3327,7 @@ mutual dynamicSource internalRetNames inScopeNames hBase hOk | ite cond thenBranch elseBranch hCond hThen hElse => simp only [compileStmt, bind, Except.bind] at hOk - cases hCondExpr : compileExpr fields dynamicSource cond with + cases hCondExpr : compileExprWithInternals fields dynamicSource [] cond with | error err => simp [hCondExpr] at hOk | ok condExpr => @@ -3576,15 +3576,15 @@ theorem compileStmt_rawLog_bridged exact Except.noConfusion hOk · simp only [if_neg hLen, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hTopicsExpr : compileExprList fields dynamicSource topics with + cases hTopicsExpr : compileExprListWithInternals fields dynamicSource [] topics with | error err => simp [hTopicsExpr] at hOk | ok topicExprs => simp only [hTopicsExpr] at hOk - cases hOffsetExpr : compileExpr fields dynamicSource dataOffset with + cases hOffsetExpr : compileExprWithInternals fields dynamicSource [] dataOffset with | error err => simp [hOffsetExpr] at hOk | ok offsetExpr => simp only [hOffsetExpr] at hOk - cases hSizeExpr : compileExpr fields dynamicSource dataSize with + cases hSizeExpr : compileExprWithInternals fields dynamicSource [] dataSize with | error err => simp [hSizeExpr] at hOk | ok sizeExpr => simp only [hSizeExpr, Except.ok.injEq] at hOk @@ -3638,15 +3638,15 @@ theorem compileStmt_rawLog_noFuncDefs exact Except.noConfusion hOk · simp only [if_neg hLen, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hTopicsExpr : compileExprList fields dynamicSource topics with + cases hTopicsExpr : compileExprListWithInternals fields dynamicSource [] topics with | error err => simp [hTopicsExpr] at hOk | ok topicExprs => simp only [hTopicsExpr] at hOk - cases hOffsetExpr : compileExpr fields dynamicSource dataOffset with + cases hOffsetExpr : compileExprWithInternals fields dynamicSource [] dataOffset with | error err => simp [hOffsetExpr] at hOk | ok offsetExpr => simp only [hOffsetExpr] at hOk - cases hSizeExpr : compileExpr fields dynamicSource dataSize with + cases hSizeExpr : compileExprWithInternals fields dynamicSource [] dataSize with | error err => simp [hSizeExpr] at hOk | ok sizeExpr => simp [hSizeExpr, Native.yulStmtContainsFuncDef] at hOk @@ -3882,7 +3882,7 @@ mutual dynamicSource internalRetNames inScopeNames hBase hOk | ite cond thenBranch elseBranch hCond hThen hElse => simp only [compileStmt, bind, Except.bind] at hOk - cases hCondExpr : compileExpr fields dynamicSource cond with + cases hCondExpr : compileExprWithInternals fields dynamicSource [] cond with | error err => simp [hCondExpr] at hOk | ok condExpr => @@ -3999,7 +3999,7 @@ mutual dynamicSource internalRetNames inScopeNames hBase hOk | ite cond thenBranch elseBranch hCond hThen hElse => simp only [compileStmt, bind, Except.bind] at hOk - cases hCondExpr : compileExpr fields dynamicSource cond with + cases hCondExpr : compileExprWithInternals fields dynamicSource [] cond with | error err => simp [hCondExpr] at hOk | ok condExpr => @@ -4280,13 +4280,13 @@ theorem compileStmt_setMapping2_singleSlot_bridged simp only [compileStmt] at hOk unfold compileSetMapping2 at hOk simp [hMapping2, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk @@ -4347,15 +4347,15 @@ theorem compileStmt_setMapping2_singleSlot_noFuncDefs simp only [compileStmt] at hOk unfold compileSetMapping2 at hOk simp [hMapping2, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -4442,7 +4442,7 @@ theorem compileStmt_setStorageAddr_singleSlot_bridged simp only [compileStmt] at hOk unfold compileSetStorage at hOk simp [hNotMapping, hFind, hAddrTy] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => simp [hExpr] at hOk @@ -4498,7 +4498,7 @@ theorem compileStmt_storageAddr_noFuncDefs simp only [compileStmt] at hOk unfold compileSetStorage at hOk simp [hNotMapping, hFind, hAddrTy] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr] at hOk | ok valueExpr => simp [hExpr] at hOk @@ -4557,10 +4557,10 @@ theorem compileStmt_setStructMember_singleSlot_bridged simp only [compileStmt, compileSetStructMember, hNotMapping2, hMembers, hFindMember, hUnpacked, hWordOffset, bind, Except.bind, Bool.false_eq_true, if_false] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr, pure, Pure.pure, Except.pure] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk @@ -4612,10 +4612,10 @@ theorem compileStmt_setStructMember_singleSlot_noFuncDefs simp only [compileStmt, compileSetStructMember, hNotMapping2, hMembers, hFindMember, hUnpacked, hWordOffset, bind, Except.bind, Bool.false_eq_true, if_false] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr, pure, Pure.pure, Except.pure] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk @@ -4737,10 +4737,10 @@ theorem compileStmt_setStructMember_singleSlot_nonzero_bridged simp only [compileStmt, compileSetStructMember, hNotMapping2, hMembers, hFindMember, hUnpacked, bind, Except.bind, Bool.false_eq_true, if_false] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr, pure, Pure.pure, Except.pure] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk @@ -4790,10 +4790,10 @@ theorem compileStmt_setStructMember_singleSlot_nonzero_noFuncDefs simp only [compileStmt, compileSetStructMember, hNotMapping2, hMembers, hFindMember, hUnpacked, bind, Except.bind, Bool.false_eq_true, if_false] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr, pure, Pure.pure, Except.pure] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk @@ -4873,13 +4873,13 @@ theorem compileStmt_setStructMember2_singleSlot_bridged simp only [compileStmt] at hOk unfold compileSetStructMember2 at hOk simp [hMapping2, hMembers, hFindMember, hUnpacked, hWordOffset, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk @@ -4949,13 +4949,13 @@ theorem compileStmt_setStructMember2_singleSlot_noFuncDefs simp only [compileStmt] at hOk unfold compileSetStructMember2 at hOk simp [hMapping2, hMembers, hFindMember, hUnpacked, hWordOffset, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk @@ -5029,13 +5029,13 @@ theorem compileStmt_setStructMember2_singleSlot_nonzero_bridged simp only [compileStmt] at hOk unfold compileSetStructMember2 at hOk simp [hMapping2, hMembers, hFindMember, hUnpacked, hBeq, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk @@ -5119,13 +5119,13 @@ theorem compileStmt_setStructMember2_singleSlot_nonzero_noFuncDefs simp only [compileStmt] at hOk unfold compileSetStructMember2 at hOk simp [hMapping2, hMembers, hFindMember, hUnpacked, hBeq, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk @@ -5193,10 +5193,10 @@ theorem compileStmt_setMappingWord_singleSlot_bridged intro out hOk subst hWordOffset simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -5240,10 +5240,10 @@ theorem compileStmt_setMappingWord_singleSlot_noFuncDefs intro out hOk subst hWordOffset simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -5313,13 +5313,13 @@ theorem compileStmt_setMapping2Word_singleSlot_bridged simp only [compileStmt] at hOk unfold compileSetMapping2Word at hOk simp [hMapping2, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk @@ -5384,13 +5384,13 @@ theorem compileStmt_setMapping2Word_singleSlot_noFuncDefs simp only [compileStmt] at hOk unfold compileSetMapping2Word at hOk simp [hMapping2, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk @@ -5530,7 +5530,7 @@ private theorem compileStmt_returnValuesInternal_bridged simp [hLen] simp only [compileStmt, hLenFalse, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hCompiled : compileExprList fields dynamicSource values with + cases hCompiled : compileExprListWithInternals fields dynamicSource [] values with | error err => simp [hCompiled] at hOk | ok compiled => simp [hCompiled] at hOk @@ -5555,7 +5555,7 @@ private theorem compileStmt_returnValuesInternal_noFuncDefs simp [hLen] simp only [compileStmt, hLenFalse, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hCompiled : compileExprList fields dynamicSource values with + cases hCompiled : compileExprListWithInternals fields dynamicSource [] values with | error err => simp [hCompiled] at hOk | ok compiled => simp [hCompiled, Native.yulStmtContainsFuncDef] at hOk @@ -5698,7 +5698,7 @@ private theorem compileStmt_returnValuesExternal_bridged simp [hValuesNil] simp only [compileStmt, hEmptyFalse, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hCompiled : compileExprList fields dynamicSource values with + cases hCompiled : compileExprListWithInternals fields dynamicSource [] values with | error err => simp [hCompiled] at hOk | ok compiled => simp [hCompiled] at hOk @@ -5730,7 +5730,7 @@ private theorem compileStmt_returnValuesExternal_noFuncDefs simp [hValuesNil] simp only [compileStmt, hEmptyFalse, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hCompiled : compileExprList fields dynamicSource values with + cases hCompiled : compileExprListWithInternals fields dynamicSource [] values with | error err => simp [hCompiled] at hOk | ok compiled => simp [hCompiled, Native.yulStmtContainsFuncDef] at hOk @@ -5811,11 +5811,11 @@ private theorem compileStmt_mstore_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hO : compileExpr fields dynamicSource offset with + cases hO : compileExprWithInternals fields dynamicSource [] offset with | error err => simp [hO] at hOk | ok compiledOffset => simp [hO] at hOk - cases hV : compileExpr fields dynamicSource value with + cases hV : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hV] at hOk | ok compiledValue => simp [hV] at hOk @@ -5841,11 +5841,11 @@ private theorem compileStmt_mstore_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hO : compileExpr fields dynamicSource offset with + cases hO : compileExprWithInternals fields dynamicSource [] offset with | error err => simp [hO] at hOk | ok compiledOffset => simp [hO] at hOk - cases hV : compileExpr fields dynamicSource value with + cases hV : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hV] at hOk | ok compiledValue => simp [hV, Native.yulStmtContainsFuncDef] at hOk @@ -5914,11 +5914,11 @@ private theorem compileStmt_tstore_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hO : compileExpr fields dynamicSource offset with + cases hO : compileExprWithInternals fields dynamicSource [] offset with | error err => simp [hO] at hOk | ok compiledOffset => simp [hO] at hOk - cases hV : compileExpr fields dynamicSource value with + cases hV : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hV] at hOk | ok compiledValue => simp [hV] at hOk @@ -5944,11 +5944,11 @@ private theorem compileStmt_tstore_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind, Pure.pure, Except.pure] at hOk - cases hO : compileExpr fields dynamicSource offset with + cases hO : compileExprWithInternals fields dynamicSource [] offset with | error err => simp [hO] at hOk | ok compiledOffset => simp [hO] at hOk - cases hV : compileExpr fields dynamicSource value with + cases hV : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hV] at hOk | ok compiledValue => simp [hV, Native.yulStmtContainsFuncDef] at hOk @@ -6034,7 +6034,7 @@ theorem compileStmt_storageArrayPush_singleSlot_bridged unfold compileStorageArrayPush at hOk unfold validateDynamicArrayField at hOk simp [hFind, hDynArr, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr, Pure.pure, Except.pure] at hOk | ok valueExpr => simp [hExpr, Pure.pure, Except.pure] at hOk @@ -6098,7 +6098,7 @@ private theorem compileStmt_storageArrayPush_singleSlot_noFuncDefs unfold compileStorageArrayPush at hOk unfold validateDynamicArrayField at hOk simp [hFind, hDynArr, bind, Except.bind] at hOk - cases hExpr : compileExpr fields dynamicSource value with + cases hExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hExpr, Pure.pure, Except.pure] at hOk | ok valueExpr => simp [hExpr, Pure.pure, Except.pure] at hOk @@ -6360,11 +6360,11 @@ theorem compileStmt_setStorageArrayElement_singleSlot_bridged unfold compileSetStorageArrayElement at hOk unfold validateDynamicArrayField at hOk simp [hFind, hDynArr, bind, Except.bind] at hOk - cases hIdxExpr : compileExpr fields dynamicSource index with + cases hIdxExpr : compileExprWithInternals fields dynamicSource [] index with | error err => simp [hIdxExpr, Pure.pure, Except.pure] at hOk | ok indexExpr => simp [hIdxExpr, Pure.pure, Except.pure] at hOk - cases hValExpr : compileExpr fields dynamicSource value with + cases hValExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hValExpr] at hOk | ok valueExpr => simp [hValExpr] at hOk @@ -6446,11 +6446,11 @@ private theorem compileStmt_setStorageArrayElement_singleSlot_noFuncDefs unfold compileSetStorageArrayElement at hOk unfold validateDynamicArrayField at hOk simp [hFind, hDynArr, bind, Except.bind] at hOk - cases hIdxExpr : compileExpr fields dynamicSource index with + cases hIdxExpr : compileExprWithInternals fields dynamicSource [] index with | error err => simp [hIdxExpr, Pure.pure, Except.pure] at hOk | ok indexExpr => simp [hIdxExpr, Pure.pure, Except.pure] at hOk - cases hValExpr : compileExpr fields dynamicSource value with + cases hValExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hValExpr] at hOk | ok valueExpr => simp [hValExpr] at hOk @@ -6535,10 +6535,10 @@ theorem compileStmt_setMappingWord_singleSlot_nonzero_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -6581,10 +6581,10 @@ theorem compileStmt_setMappingWord_singleSlot_nonzero_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -6658,13 +6658,13 @@ theorem compileStmt_setMapping2Word_singleSlot_nonzero_bridged simp only [compileStmt] at hOk unfold compileSetMapping2Word at hOk simp [hMapping2, hSlots, hBeq] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk @@ -6744,13 +6744,13 @@ theorem compileStmt_setMapping2Word_singleSlot_nonzero_noFuncDefs simp only [compileStmt] at hOk unfold compileSetMapping2Word at hOk simp [hMapping2, hSlots, hBeq] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk @@ -6853,10 +6853,10 @@ theorem compileStmt_setMappingChain_singleSlot_bridged simp only [compileStmt] at hOk unfold compileSetMappingChain at hOk simp [hMapping, hSlots] at hOk - cases hKeyExprs : compileExprList fields dynamicSource keys with + cases hKeyExprs : compileExprListWithInternals fields dynamicSource [] keys with | error err => simp [hKeyExprs, bind, Except.bind] at hOk | ok keyExprs => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExprs, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKeyExprs, hValueExpr, bind, Except.bind] at hOk @@ -6906,10 +6906,10 @@ private theorem compileStmt_setMappingChain_singleSlot_noFuncDefs simp only [compileStmt] at hOk unfold compileSetMappingChain at hOk simp [hMapping, hSlots] at hOk - cases hKeyExprs : compileExprList fields dynamicSource keys with + cases hKeyExprs : compileExprListWithInternals fields dynamicSource [] keys with | error err => simp [hKeyExprs, bind, Except.bind] at hOk | ok keyExprs => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExprs, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => simp [hKeyExprs, hValueExpr, bind, Except.bind] at hOk @@ -7127,10 +7127,10 @@ theorem compileStmt_setMapping_multiSlot_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -7155,10 +7155,10 @@ theorem compileStmt_setMapping_multiSlot_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -7184,10 +7184,10 @@ theorem compileStmt_setMappingUint_multiSlot_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -7212,10 +7212,10 @@ theorem compileStmt_setMappingUint_multiSlot_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -7384,13 +7384,13 @@ theorem compileStmt_setMapping2_multiSlot_bridged simp only [compileStmt] at hOk unfold compileSetMapping2 at hOk simp [hMapping2, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -7478,13 +7478,13 @@ theorem compileStmt_setMapping2_multiSlot_noFuncDefs simp only [compileStmt] at hOk unfold compileSetMapping2 at hOk simp [hMapping2, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -7585,10 +7585,10 @@ theorem compileStmt_setStructMember_multiSlot_bridged simp only [compileStmt, compileSetStructMember, hNotMapping2, hMembers, hFindMember, hUnpacked, hWordOffset, bind, Except.bind, Bool.false_eq_true, if_false] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr, pure, Pure.pure, Except.pure] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk @@ -7621,10 +7621,10 @@ theorem compileStmt_setStructMember_multiSlot_noFuncDefs simp only [compileStmt, compileSetStructMember, hNotMapping2, hMembers, hFindMember, hUnpacked, hWordOffset, bind, Except.bind, Bool.false_eq_true, if_false] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr, pure, Pure.pure, Except.pure] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk @@ -7730,13 +7730,13 @@ theorem compileStmt_setStructMember2_multiSlot_bridged simp only [compileStmt] at hOk unfold compileSetStructMember2 at hOk simp [hMapping2, hMembers, hFindMember, hUnpacked, hWordOffset, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -7830,13 +7830,13 @@ theorem compileStmt_setStructMember2_multiSlot_noFuncDefs simp only [compileStmt] at hOk unfold compileSetStructMember2 at hOk simp [hMapping2, hMembers, hFindMember, hUnpacked, hWordOffset, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -7935,10 +7935,10 @@ theorem compileStmt_setMappingWord_multiSlot_bridged intro out hOk subst hWordOffset simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -7965,10 +7965,10 @@ theorem compileStmt_setMappingWord_multiSlot_noFuncDefs intro out hOk subst hWordOffset simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -8063,13 +8063,13 @@ theorem compileStmt_setMapping2Word_multiSlot_bridged simp only [compileStmt] at hOk unfold compileSetMapping2Word at hOk simp [hMapping2, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -8156,13 +8156,13 @@ theorem compileStmt_setMapping2Word_multiSlot_noFuncDefs simp only [compileStmt] at hOk unfold compileSetMapping2Word at hOk simp [hMapping2, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -8383,10 +8383,10 @@ theorem compileStmt_setMappingWord_multiSlot_nonzero_bridged BridgedStmts out := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -8412,10 +8412,10 @@ theorem compileStmt_setMappingWord_multiSlot_nonzero_noFuncDefs Native.yulStmtsContainFuncDef out = false := by intro out hOk simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr] at hOk @@ -8603,13 +8603,13 @@ theorem compileStmt_setMapping2Word_multiSlot_nonzero_bridged simp only [compileStmt] at hOk unfold compileSetMapping2Word at hOk simp [hMapping2, hSlots, hBeq] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -8725,13 +8725,13 @@ theorem compileStmt_setMapping2Word_multiSlot_nonzero_noFuncDefs simp only [compileStmt] at hOk unfold compileSetMapping2Word at hOk simp [hMapping2, hSlots, hBeq] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -8836,10 +8836,10 @@ theorem compileStmt_setStructMember_multiSlot_nonzero_bridged simp only [compileStmt, compileSetStructMember, hNotMapping2, hMembers, hFindMember, hUnpacked, bind, Except.bind, Bool.false_eq_true, if_false] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr, pure, Pure.pure, Except.pure] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk | ok valueExpr => @@ -8873,10 +8873,10 @@ theorem compileStmt_setStructMember_multiSlot_nonzero_noFuncDefs simp only [compileStmt, compileSetStructMember, hNotMapping2, hMembers, hFindMember, hUnpacked, bind, Except.bind, Bool.false_eq_true, if_false] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr, pure, Pure.pure, Except.pure] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr, pure, Pure.pure, Except.pure] at hOk | ok valueExpr => @@ -8992,13 +8992,13 @@ theorem compileStmt_setStructMember2_multiSlot_nonzero_bridged simp only [compileStmt] at hOk unfold compileSetStructMember2 at hOk simp [hMapping2, hMembers, hFindMember, hUnpacked, hBeq, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -9119,13 +9119,13 @@ theorem compileStmt_setStructMember2_multiSlot_nonzero_noFuncDefs simp only [compileStmt] at hOk unfold compileSetStructMember2 at hOk simp [hMapping2, hMembers, hFindMember, hUnpacked, hBeq, hSlots] at hOk - cases hKey1Expr : compileExpr fields dynamicSource key1 with + cases hKey1Expr : compileExprWithInternals fields dynamicSource [] key1 with | error err => simp [hKey1Expr, bind, Except.bind] at hOk | ok key1Expr => - cases hKey2Expr : compileExpr fields dynamicSource key2 with + cases hKey2Expr : compileExprWithInternals fields dynamicSource [] key2 with | error err => simp [hKey1Expr, hKey2Expr, bind, Except.bind] at hOk | ok key2Expr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKey1Expr, hKey2Expr, hValueExpr, bind, Except.bind] at hOk | ok valueExpr => @@ -9229,10 +9229,10 @@ theorem compileStmt_setMappingPackedWord_singleSlot_bridged intro out hOk subst hWordOffset simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, compileMappingPackedSlotWrite, @@ -9341,10 +9341,10 @@ theorem compileStmt_setMappingPackedWord_singleSlot_noFuncDefs intro out hOk subst hWordOffset simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, compileMappingPackedSlotWrite, @@ -9443,10 +9443,10 @@ theorem compileStmt_setMappingPackedWord_singleSlot_nonzero_bridged | zero => exact absurd rfl hNonzero | succ n => rfl simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, compileMappingPackedSlotWrite, @@ -9572,10 +9572,10 @@ theorem compileStmt_setMappingPackedWord_singleSlot_nonzero_noFuncDefs | zero => exact absurd rfl hNonzero | succ n => rfl simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, compileMappingPackedSlotWrite, @@ -9834,10 +9834,10 @@ theorem compileStmt_setMappingPackedWord_multiSlot_bridged intro out hOk subst hWordOffset simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, compileMappingPackedSlotWrite, @@ -9898,10 +9898,10 @@ theorem compileStmt_setMappingPackedWord_multiSlot_noFuncDefs intro out hOk subst hWordOffset simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, compileMappingPackedSlotWrite, @@ -10190,10 +10190,10 @@ theorem compileStmt_setMappingPackedWord_multiSlot_nonzero_bridged | zero => exact absurd rfl hNonzero | succ n => rfl simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, compileMappingPackedSlotWrite, @@ -10257,10 +10257,10 @@ theorem compileStmt_setMappingPackedWord_multiSlot_nonzero_noFuncDefs | zero => exact absurd rfl hNonzero | succ n => rfl simp only [compileStmt, bind, Except.bind] at hOk - cases hKeyExpr : compileExpr fields dynamicSource key with + cases hKeyExpr : compileExprWithInternals fields dynamicSource [] key with | error err => simp [hKeyExpr] at hOk | ok keyExpr => - cases hValueExpr : compileExpr fields dynamicSource value with + cases hValueExpr : compileExprWithInternals fields dynamicSource [] value with | error err => simp [hKeyExpr, hValueExpr] at hOk | ok valueExpr => simp [hKeyExpr, hValueExpr, compileMappingPackedSlotWrite, diff --git a/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanCallClosure.lean b/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanCallClosure.lean index aa075a828..985cdfc53 100644 --- a/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanCallClosure.lean +++ b/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanCallClosure.lean @@ -262,7 +262,7 @@ private theorem compileStmt_internalCall_call_bridged isInternal inScopeNames adtTypes (.internalCall funcName args) = .ok out) : BridgedStmts out := by simp only [compileStmt, bind, Except.bind] at hOk - cases hExprs : compileExprList fields dynamicSource args with + cases hExprs : compileExprListWithInternals fields dynamicSource [] args with | error _ => simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs] at hOk | ok argExprs => @@ -295,7 +295,7 @@ private theorem compileStmt_internalCallAssign_bridged .ok out) : BridgedStmts out := by simp only [compileStmt, bind, Except.bind] at hOk - cases hExprs : compileExprList fields dynamicSource args with + cases hExprs : compileExprListWithInternals fields dynamicSource [] args with | error _ => simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs] at hOk | ok argExprs => @@ -394,7 +394,7 @@ theorem compileStmt_externalCallBind_bridged cases hStmt with | mk resultVars externalName args hArgs hFn => simp only [compileStmt, bind, Except.bind] at hOk - cases hExprs : compileExprList fields dynamicSource args with + cases hExprs : compileExprListWithInternals fields dynamicSource [] args with | error _ => simp [hExprs] at hOk | ok argExprs => @@ -494,7 +494,7 @@ theorem compileStmt_internalCall_noFuncDefs cases hStmt with | call funcName args hArgs hFn => simp only [compileStmt, bind, Except.bind] at hOk - cases hExprs : compileExprList fields dynamicSource args with + cases hExprs : compileExprListWithInternals fields dynamicSource [] args with | error _ => simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs] at hOk | ok argExprs => @@ -504,7 +504,7 @@ theorem compileStmt_internalCall_noFuncDefs simp [Native.yulStmtContainsFuncDef] | callAssign names funcName args hArgs hFn => simp only [compileStmt, bind, Except.bind] at hOk - cases hExprs : compileExprList fields dynamicSource args with + cases hExprs : compileExprListWithInternals fields dynamicSource [] args with | error _ => simp [compileInternalCallArgs, findInternalFunctionForCall?, hExprs] at hOk | ok argExprs => @@ -543,7 +543,7 @@ theorem compileStmt_externalCallBind_noFuncDefs cases hStmt with | mk resultVars externalName args hArgs hFn => simp only [compileStmt, bind, Except.bind] at hOk - cases hExprs : compileExprList fields dynamicSource args with + cases hExprs : compileExprListWithInternals fields dynamicSource [] args with | error _ => simp [hExprs] at hOk | ok argExprs => simp [hExprs] at hOk @@ -622,7 +622,7 @@ theorem compileStmt_ecm_bridged · simp only [bind, Except.bind] at hOk cases hOk · simp only [Pure.pure, Except.pure, bind, Except.bind] at hOk - cases hExprs : compileExprList fields dynamicSource args with + cases hExprs : compileExprListWithInternals fields dynamicSource [] args with | error _ => simp [hExprs] at hOk | ok argExprs => simp [hExprs] at hOk diff --git a/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanSourceExprClosure.lean b/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanSourceExprClosure.lean index 4e5d2a679..8c4495d7c 100644 --- a/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanSourceExprClosure.lean +++ b/Compiler/Proofs/YulGeneration/Backends/EvmYulLeanSourceExprClosure.lean @@ -74,19 +74,19 @@ theorem compileExpr_bridgedSource_leaf intro e hE out hOk cases hE with | literal n => - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact BridgedExpr.lit _ | param name => - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact BridgedExpr.ident name | constructorArg idx => - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact BridgedExpr.ident _ | localVar name => - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact BridgedExpr.ident name @@ -479,7 +479,7 @@ private theorem bridgedExpr_ite {cond thenVal elseVal : YulExpr} exact bridgedExpr_binopBuiltin (by simp [bridgedBuiltins]) hThenTerm hElseTerm /-- Destructure a `do`-block emission of `yulBinOp` into its sub-results. - This shape matches what `simp only [compileExpr]` produces for every + This shape matches what `simp only [compileExpr, compileExprWithInternals]` produces for every binop constructor case. -/ private theorem compileExpr_yulBinOp_ok {fields : List CompilationModel.Field} {src : DynamicDataSource} @@ -813,27 +813,27 @@ theorem compileExpr_bridgedSource induction hE with | literal n => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out; exact BridgedExpr.lit _ | param name => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out; exact BridgedExpr.ident name | constructorArg idx => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out; exact BridgedExpr.ident _ | localVar name => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out; exact BridgedExpr.ident name | arrayLength name => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out; exact BridgedExpr.ident s!"{name}_length" | storage fieldName => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk split at hOk · simp at hOk · split at hOk @@ -851,7 +851,7 @@ theorem compileExpr_bridgedSource · simp at hOk | storageAddr fieldName => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk split at hOk · simp at hOk · split at hOk @@ -874,7 +874,7 @@ theorem compileExpr_bridgedSource · simp at hOk | storageArrayLength fieldName => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk split at hOk · rename_i f slot hFind cases hTy : f.ty with @@ -887,7 +887,7 @@ theorem compileExpr_bridgedSource · simp at hOk | adtTag adtName storageField => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk split at hOk · rename_i baseSlot hFind simp [Pure.pure, Except.pure] at hOk @@ -896,7 +896,7 @@ theorem compileExpr_bridgedSource · simp at hOk | adtField adtName variantName fieldName fieldIndex storageField => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk split at hOk · rename_i baseSlot hFind simp [Pure.pure, Except.pure] at hOk @@ -905,8 +905,8 @@ theorem compileExpr_bridgedSource · simp at hOk | mapping fieldName hKey ihKey => intro out hOk - simp only [compileExpr, bind, Except.bind] at hOk - cases hCompiledKey : compileExpr fields src _ with + simp only [compileExpr, compileExprWithInternals, bind, Except.bind] at hOk + cases hCompiledKey : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey] at hOk cases hOk @@ -915,8 +915,8 @@ theorem compileExpr_bridgedSource exact compileMappingSlotRead_bridged (ihKey hCompiledKey) hOk | mappingWord fieldName hKey wordOffset ihKey => intro out hOk - simp only [compileExpr, bind, Except.bind] at hOk - cases hCompiledKey : compileExpr fields src _ with + simp only [compileExpr, compileExprWithInternals, bind, Except.bind] at hOk + cases hCompiledKey : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey] at hOk cases hOk @@ -925,8 +925,8 @@ theorem compileExpr_bridgedSource exact compileMappingSlotRead_bridged (ihKey hCompiledKey) hOk | mappingUint fieldName hKey ihKey => intro out hOk - simp only [compileExpr, bind, Except.bind] at hOk - cases hCompiledKey : compileExpr fields src _ with + simp only [compileExpr, compileExprWithInternals, bind, Except.bind] at hOk + cases hCompiledKey : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey] at hOk cases hOk @@ -935,19 +935,19 @@ theorem compileExpr_bridgedSource exact compileMappingSlotRead_bridged (ihKey hCompiledKey) hOk | mapping2 fieldName hKey1 hKey2 ihKey1 ihKey2 => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk split at hOk · simp at hOk · split at hOk · rename_i slot hFind simp only [bind, Except.bind] at hOk - cases hCompiledKey1 : compileExpr fields src _ with + cases hCompiledKey1 : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey1] at hOk cases hOk | ok keyExpr1 => rw [hCompiledKey1] at hOk - cases hCompiledKey2 : compileExpr fields src _ with + cases hCompiledKey2 : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey2] at hOk cases hOk @@ -960,19 +960,19 @@ theorem compileExpr_bridgedSource · simp at hOk | mapping2Word fieldName hKey1 hKey2 wordOffset ihKey1 ihKey2 => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk split at hOk · simp at hOk · split at hOk · rename_i slot hFind simp only [bind, Except.bind] at hOk - cases hCompiledKey1 : compileExpr fields src _ with + cases hCompiledKey1 : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey1] at hOk cases hOk | ok keyExpr1 => rw [hCompiledKey1] at hOk - cases hCompiledKey2 : compileExpr fields src _ with + cases hCompiledKey2 : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey2] at hOk cases hOk @@ -991,7 +991,7 @@ theorem compileExpr_bridgedSource · simp at hOk | structMember fieldName hKey memberName ihKey => intro out hOk - simp [compileExpr, bind, Except.bind] at hOk + simp [compileExpr, compileExprWithInternals, bind, Except.bind] at hOk split at hOk · cases hOk · simp [Pure.pure, Except.pure] at hOk @@ -1003,7 +1003,7 @@ theorem compileExpr_bridgedSource cases hPacked : member.packed with | none => rw [hPacked] at hOk - cases hCompiledKey : compileExpr fields src _ with + cases hCompiledKey : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey] at hOk cases hOk @@ -1013,7 +1013,7 @@ theorem compileExpr_bridgedSource exact compileMappingSlotRead_bridged (ihKey hCompiledKey) hOk | some packed => rw [hPacked] at hOk - cases hCompiledKey : compileExpr fields src _ with + cases hCompiledKey : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey] at hOk cases hOk @@ -1036,7 +1036,7 @@ theorem compileExpr_bridgedSource packed.offset (packedMaskNat packed) | structMember2 fieldName hKey1 hKey2 memberName ihKey1 ihKey2 => intro out hOk - simp [compileExpr, bind, Except.bind] at hOk + simp [compileExpr, compileExprWithInternals, bind, Except.bind] at hOk split at hOk · cases hOk · split at hOk @@ -1046,13 +1046,13 @@ theorem compileExpr_bridgedSource · rename_i member hMember split at hOk · rename_i slot hFindSlot - cases hCompiledKey1 : compileExpr fields src _ with + cases hCompiledKey1 : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey1] at hOk cases hOk | ok keyExpr1 => rw [hCompiledKey1] at hOk - cases hCompiledKey2 : compileExpr fields src _ with + cases hCompiledKey2 : compileExprWithInternals fields src [] _ with | error err => rw [hCompiledKey2] at hOk cases hOk @@ -1092,293 +1092,293 @@ theorem compileExpr_bridgedSource · simp at hOk | caller => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact bridgedExpr_nullaryBuiltin (by simp [bridgedBuiltins]) | txOrigin => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact bridgedExpr_nullaryBuiltin (by simp [bridgedBuiltins]) | contractAddress => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact bridgedExpr_nullaryBuiltin (by simp [bridgedBuiltins]) | msgValue => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact bridgedExpr_nullaryBuiltin (by simp [bridgedBuiltins]) | blockTimestamp => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact bridgedExpr_nullaryBuiltin (by simp [bridgedBuiltins]) | blockNumber => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact bridgedExpr_nullaryBuiltin (by simp [bridgedBuiltins]) | chainid => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact bridgedExpr_nullaryBuiltin (by simp [bridgedBuiltins]) | blobbasefee => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact bridgedExpr_nullaryBuiltin (by simp [bridgedBuiltins]) | calldatasize => intro out hOk - simp [compileExpr, Pure.pure, Except.pure] at hOk + simp [compileExpr, compileExprWithInternals, Pure.pure, Except.pure] at hOk subst out exact bridgedExpr_nullaryBuiltin (by simp [bridgedBuiltins]) | calldataload _ iho => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨co, hO, hEq⟩ := compileExpr_unopBuiltin_ok hOk subst hEq exact bridgedExpr_unopBuiltin (by simp [bridgedBuiltins]) (iho hO) | mload _ iho => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨co, hO, hEq⟩ := compileExpr_unopBuiltin_ok hOk subst hEq exact bridgedExpr_mload co (iho hO) | tload _ iho => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨co, hO, hEq⟩ := compileExpr_unopBuiltin_ok hOk subst hEq exact bridgedExpr_tload co (iho hO) | keccak256 _ _ ihOffset ihSize => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨co, cs, hO, hS, hEq⟩ := compileExpr_binaryShape_ok hOk subst hEq exact bridgedExpr_keccak256 co cs (ihOffset hO) (ihSize hS) | add _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | sub _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | mul _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | div _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | sdiv _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | mod _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | smod _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | bitAnd _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | bitOr _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | bitXor _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | bitNot _ iha => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, hA, hEq⟩ := compileExpr_unopBuiltin_ok hOk subst hEq exact bridgedExpr_unopBuiltin (by simp [bridgedBuiltins]) (iha hA) | shl _ _ ihs ihv => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (ihs hA) (ihv hB) | shr _ _ ihs ihv => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (ihs hA) (ihv hB) | sar _ _ ihs ihv => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (ihs hA) (ihv hB) | byte _ _ ihi ihv => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (ihi hA) (ihv hB) | signextend _ _ ihb ihv => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (ihb hA) (ihv hB) | eq _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | gt _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | sgt _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | lt _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | slt _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | logicalAnd _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBoolBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (bridgedExpr_yulToBool (iha hA)) (bridgedExpr_yulToBool (ihb hB)) | logicalOr _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulBoolBinOp_ok hOk subst hEq exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (bridgedExpr_yulToBool (iha hA)) (bridgedExpr_yulToBool (ihb hB)) | logicalNot _ iha => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, hA, hEq⟩ := compileExpr_unopBuiltin_ok hOk subst hEq exact bridgedExpr_unopBuiltin (by simp [bridgedBuiltins]) (iha hA) | ceilDiv _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_binaryShape_ok hOk subst hEq exact bridgedExpr_ceilDiv (iha hA) (ihb hB) | mulDivDown _ _ _ iha ihb ihc => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, cc, hA, hB, hC, hEq⟩ := compileExpr_ternaryShape_ok hOk subst hEq exact bridgedExpr_mulDivDown (iha hA) (ihb hB) (ihc hC) | mulDivUp _ _ _ iha ihb ihc => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, cc, hA, hB, hC, hEq⟩ := compileExpr_ternaryShape_ok hOk subst hEq exact bridgedExpr_mulDivUp (iha hA) (ihb hB) (ihc hC) | wMulDown _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_binaryShape_ok hOk subst hEq exact bridgedExpr_wMulDown (iha hA) (ihb hB) | wDivUp _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_binaryShape_ok hOk subst hEq exact bridgedExpr_wDivUp (iha hA) (ihb hB) | builtinExp hBase hExponent iha ihb => rename_i base exponent intro out hOk - simp only [compileExpr] at hOk - cases hA : compileExpr fields src base with + simp only [compileExpr, compileExprWithInternals] at hOk + cases hA : compileExprWithInternals fields src [] base with | error err => - simp [compileExprList, hA, bind, Except.bind] at hOk + simp [compileExprListWithInternals, hA, bind, Except.bind] at hOk | ok ca => - cases hB : compileExpr fields src exponent with + cases hB : compileExprWithInternals fields src [] exponent with | error err => - simp [compileExprList, hA, hB, bind, Except.bind] at hOk + simp [compileExprListWithInternals, hA, hB, bind, Except.bind] at hOk | ok cb => - simp [compileExprList, hA, hB, builtinExpName, Pure.pure, + simp [compileExprListWithInternals, hA, hB, builtinExpName, Pure.pure, Except.pure, bind, Except.bind] at hOk cases hOk exact bridgedExpr_yulBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | min _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_binaryShape_ok hOk subst hEq exact bridgedExpr_min (iha hA) (ihb hB) | max _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_binaryShape_ok hOk subst hEq exact bridgedExpr_max (iha hA) (ihb hB) | ite _ _ _ ihc iht ihe => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨cc, ct, ce, hC, hT, hE, hEq⟩ := compileExpr_ternaryShape_ok hOk subst hEq exact bridgedExpr_ite (ihc hC) (iht hT) (ihe hE) | ge _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulNegatedBinOp_ok hOk subst hEq exact bridgedExpr_yulNegatedBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) | le _ _ iha ihb => intro out hOk - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk obtain ⟨ca, cb, hA, hB, hEq⟩ := compileExpr_yulNegatedBinOp_ok hOk subst hEq exact bridgedExpr_yulNegatedBinOp (by simp [bridgedBuiltins]) (iha hA) (ihb hB) @@ -1650,19 +1650,19 @@ theorem compileExprList_bridgedSource induction exprs with | nil => intro _ out hOk - simp [compileExprList, Pure.pure, Except.pure] at hOk + simp [compileExprList, compileExprListWithInternals, Pure.pure, Except.pure] at hOk subst out intro yulExpr hMem cases hMem | cons e es ih => intro hAll out hOk - simp only [compileExprList, bind, Except.bind] at hOk - cases hHead : compileExpr fields src e with + simp only [compileExprList, compileExprListWithInternals, bind, Except.bind] at hOk + cases hHead : compileExprWithInternals fields src [] e with | error err => simp [hHead] at hOk | ok headExpr => simp [hHead] at hOk - cases hTail : compileExprList fields src es with + cases hTail : compileExprListWithInternals fields src [] es with | error err => simp [hTail] at hOk | ok tailExprs => @@ -1695,12 +1695,12 @@ theorem compileExpr_mappingChain_bridgedSource (hKeys : ∀ key ∈ keys, BridgedSourceExpr key) (hOk : compileExpr fields src (.mappingChain fieldName keys) = .ok out) : BridgedExpr out := by - simp only [compileExpr] at hOk + simp only [compileExpr, compileExprWithInternals] at hOk split at hOk · simp at hOk · split at hOk · rename_i slot hFind - cases hCompiledKeys : compileExprList fields src keys with + cases hCompiledKeys : compileExprListWithInternals fields src [] keys with | error err => simp [bind, Except.bind, hCompiledKeys] at hOk | ok keyExprs => diff --git a/PrintAxioms.lean b/PrintAxioms.lean index eae40a01c..0921294f9 100644 --- a/PrintAxioms.lean +++ b/PrintAxioms.lean @@ -2098,6 +2098,8 @@ end Verity.AxiomAudit Compiler.Proofs.IRGeneration.FunctionBody.runtimeStateMatchesIR_setVar_bindValue Compiler.Proofs.IRGeneration.FunctionBody.runtimeStateMatchesIR_setVar_irrelevant Compiler.Proofs.IRGeneration.FunctionBody.runtimeStateMatchesIR_setVars_irrelevant + -- Compiler.Proofs.IRGeneration.FunctionBody.compileExprWithInternals_nil_ok -- private + -- Compiler.Proofs.IRGeneration.FunctionBody.compileRequireFailCondWithInternals_nil_ok -- private Compiler.Proofs.IRGeneration.FunctionBody.compileStmt_core_ok Compiler.Proofs.IRGeneration.FunctionBody.runtimeStateMatchesIR_setBothMemory Compiler.Proofs.IRGeneration.FunctionBody.runtimeStateMatchesIR_updateMemoryEvents @@ -2653,6 +2655,8 @@ end Verity.AxiomAudit Compiler.Proofs.IRGeneration.stmtListScopeDiscipline_scope_names -- Compiler/Proofs/IRGeneration/GenericInduction/Storage.lean + -- Compiler.Proofs.IRGeneration.compileExprWithInternals_nil_ok -- private + -- Compiler.Proofs.IRGeneration.compileExprListWithInternals_nil_ok -- private -- Compiler.Proofs.IRGeneration.encodeStorageAt_writeUintSlots_singleton_other -- private -- Compiler.Proofs.IRGeneration.encodeStorageAt_writeUintSlots_other -- private -- Compiler.Proofs.IRGeneration.encodeStorageAt_writeUintKeyedMappingSlots_singleton_other -- private @@ -3046,6 +3050,10 @@ end Verity.AxiomAudit Compiler.Proofs.IRGeneration.IntrinsicProofs.intrinsic_boundNamesInScope_of_args Compiler.Proofs.IRGeneration.IntrinsicProofs.verbatim_lowering_callName Compiler.Proofs.IRGeneration.IntrinsicProofs.verbatim_lowering_hexLiteral + -- Compiler.Proofs.IRGeneration.IntrinsicProofs.compileExprWithInternals_param -- private + -- Compiler.Proofs.IRGeneration.IntrinsicProofs.compileExprListWithInternals_nil -- private + -- Compiler.Proofs.IRGeneration.IntrinsicProofs.compileExprListWithInternals_param_one -- private + -- Compiler.Proofs.IRGeneration.IntrinsicProofs.compileExprListWithInternals_param_two -- private Compiler.Proofs.IRGeneration.IntrinsicProofs.compileExpr_intrinsic_verbatim_one_param Compiler.Proofs.IRGeneration.IntrinsicProofs.compileExpr_intrinsic_builtin_one_param Compiler.Proofs.IRGeneration.IntrinsicProofs.compileExpr_intrinsic_verbatim_zero_output_error @@ -5546,4 +5554,4 @@ end Verity.AxiomAudit Compiler.Proofs.YulGeneration.YulTransaction.ofIR_args ] --- Total: 5191 theorems/lemmas (3588 public, 1603 private, 0 sorry'd) +-- Total: 5199 theorems/lemmas (3588 public, 1611 private, 0 sorry'd) From 70d116e8aa8ab3d5666b0a7cf3bd79acdd11afb9 Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Tue, 16 Jun 2026 13:04:22 +0200 Subject: [PATCH 3/3] thread internal function table through fallback/receive entrypoints and ADT storage writes (#1889) --- Compiler/CompilationModel/Compile.lean | 7 ++- Compiler/CompilationModel/Dispatch.lean | 9 +-- Compiler/CompilationModelFeatureTest.lean | 77 +++++++++++++++++++++++ 3 files changed, 86 insertions(+), 7 deletions(-) diff --git a/Compiler/CompilationModel/Compile.lean b/Compiler/CompilationModel/Compile.lean index 989c2e012..59d88709f 100644 --- a/Compiler/CompilationModel/Compile.lean +++ b/Compiler/CompilationModel/Compile.lean @@ -55,7 +55,8 @@ theorem unsafeYulToEVMYul_eq (fragment : UnsafeYulFragment) : private def compileAdtStorageWrite (fields : List Field) (dynamicSource : DynamicDataSource) (adtTypes : List AdtTypeDef) - (storageField adtName variantName : String) (args : List Expr) : + (storageField adtName variantName : String) (args : List Expr) + (internalFunctions : List FunctionSpec := []) : Except String (List YulStmt) := do let adt ← lookupAdtTypeDef adtTypes adtName let variant ← lookupAdtVariant adt variantName @@ -76,7 +77,7 @@ private def compileAdtStorageWrite (fields : List Field) throw s!"Compilation error: storage field '{storageField}' is not ADT-typed" | none => throw s!"Compilation error: unknown storage field '{storageField}' for ADT construct '{adtName}.{variantName}'" let baseSlots := baseSlot :: aliasSlots - let argExprs ← compileExprList fields dynamicSource args + let argExprs ← compileExprListWithInternals fields dynamicSource internalFunctions args let payloadBindings := argExprs.zipIdx.map fun (argExpr, idx) => YulStmt.let_ s!"__adt_payload_{idx}" argExpr @@ -150,7 +151,7 @@ def compileStmt (fields : List Field) (events : List EventDef := []) | _ => match value with | Expr.adtConstruct adtName variantName args => - compileAdtStorageWrite fields dynamicSource adtTypes field adtName variantName args + compileAdtStorageWrite fields dynamicSource adtTypes field adtName variantName args internalFunctions | _ => compileSetStorage fields dynamicSource field value false internalFunctions | Stmt.setStorageAddr field value => diff --git a/Compiler/CompilationModel/Dispatch.lean b/Compiler/CompilationModel/Dispatch.lean index ada86d54e..a37934d9d 100644 --- a/Compiler/CompilationModel/Dispatch.lean +++ b/Compiler/CompilationModel/Dispatch.lean @@ -255,9 +255,10 @@ def attachNonReentrantGuard (fields : List Field) (spec : FunctionSpec) pure { irFn with body := prefixLoads ++ guardStmts ++ suffix ++ [release] } private def compileSpecialEntrypoint (fields : List Field) (events : List EventDef) - (errors : List ErrorDef) (adtTypes : List AdtTypeDef := []) (spec : FunctionSpec) : + (errors : List ErrorDef) (adtTypes : List AdtTypeDef := []) + (internalFunctions : List FunctionSpec := []) (spec : FunctionSpec) : Except String IREntrypoint := do - let bodyChunks ← compileStmtList fields events errors .calldata [] false [] adtTypes spec.body + let bodyChunks ← compileStmtList fields events errors .calldata [] false [] adtTypes spec.body internalFunctions -- Apply nonreentrant guard for fallback/receive if annotated (high-severity -- Bugbot: previously these special entrypoints were compiled without the -- transient lock even when `nonreentrant(lock)` was declared). @@ -613,8 +614,8 @@ def compileValidatedCore (spec : CompilationModel) (selectors : List Nat) : Exce [dynamicBytesEqCalldataHelper, dynamicBytesEqMemoryHelper] else [] - let fallbackEntrypoint ← fallbackSpec.mapM (compileSpecialEntrypoint fields spec.events spec.errors spec.adtTypes) - let receiveEntrypoint ← receiveSpec.mapM (compileSpecialEntrypoint fields spec.events spec.errors spec.adtTypes) + let fallbackEntrypoint ← fallbackSpec.mapM (compileSpecialEntrypoint fields spec.events spec.errors spec.adtTypes internalFns) + let receiveEntrypoint ← receiveSpec.mapM (compileSpecialEntrypoint fields spec.events spec.errors spec.adtTypes internalFns) return { name := spec.name deploy := (← compileConstructor fields spec.events spec.errors spec.adtTypes spec.constructor internalFns) diff --git a/Compiler/CompilationModelFeatureTest.lean b/Compiler/CompilationModelFeatureTest.lean index 844ab2f76..87b564888 100644 --- a/Compiler/CompilationModelFeatureTest.lean +++ b/Compiler/CompilationModelFeatureTest.lean @@ -3473,6 +3473,65 @@ private def adtAliasPayloadMemoizesExprSpec : CompilationModel := { ] } +-- Regression tests for Bugbot MEDIUM issues in PR #2016 (task/1889-internal-helper-args): +-- (a) internal helper call with dynamic/composite args inside fallback body must use callee-aware +-- compileInternalCallArgs (not plain compileExprList) => correct expansion to offset/length. +-- (b) internal helper call inside ADT ctor payload for setStorage must thread internals through +-- compileAdtStorageWrite (not compileExprList) => correct expansion. +private def fallbackInternalDynamicArgSpec : CompilationModel := { + name := "FallbackInternalDynamicArgRegression" + fields := [] + «constructor» := none + functions := [ + { name := "internal_first" + params := [{ name := "xs", ty := ParamType.array ParamType.uint256 }] + returnType := some FieldType.uint256 + isInternal := true + body := [Stmt.return (Expr.arrayElement "xs" (Expr.literal 0))] + }, + { name := "fallback" + params := [] + returnType := none + body := [ + Stmt.return (Expr.internalCall "internal_first" [Expr.param "xs"]) + ] + } + ] +} + +private def adtStorageInternalDynamicArgSpec : CompilationModel := { + name := "AdtStorageInternalDynamicArgRegression" + fields := [ + { name := "choice", ty := FieldType.adt "Choice" 1, «slot» := some 10, aliasSlots := [] } + ] + «constructor» := none + functions := [ + { name := "internal_first" + params := [{ name := "xs", ty := ParamType.array ParamType.uint256 }] + returnType := some FieldType.uint256 + isInternal := true + body := [Stmt.return (Expr.arrayElement "xs" (Expr.literal 0))] + }, + { name := "storeDyn" + params := [{ name := "xs", ty := ParamType.array ParamType.uint256 }] + returnType := none + body := [ + Stmt.setStorage "choice" + (Expr.adtConstruct "Choice" "Some" [Expr.internalCall "internal_first" [Expr.param "xs"]]), + Stmt.stop + ] + } + ] + adtTypes := [ + { name := "Choice" + variants := [ + { name := "None", tag := 0, fields := [] }, + { name := "Some", tag := 1, fields := [{ name := "amount", ty := ParamType.uint256 }] } + ] + } + ] +} + private def ceiInitialInternalCallAllowedSpec : CompilationModel := { name := "CEIInitialInternalCallAllowed" fields := [{ name := "value", ty := FieldType.uint256 }] @@ -5632,6 +5691,24 @@ set_option maxRecDepth 4096 in expectTrue "ADT alias writes reuse the generated payload local" ((contains adtAliasPayloadMemoYul "let __adt_payload_0 := echo(input)") && (countOccurrences adtAliasPayloadMemoYul "__adt_payload_0" >= 3)) + -- Bugbot regression (a): fallback/receive must receive real internalFunctions table so + -- dynamic/composite internal calls inside them expand args correctly (not fall to compileExprList). + let fallbackInternalDynYul ← expectCompileToYul + "fallback with internal dynamic/composite arg call (Bugbot regression a: fallback omits internal function table)" + fallbackInternalDynamicArgSpec + expectTrue "fallback internalCall (short-form array arg) expands via callee-aware path to data_offset + length (two args, not single 'xs')" + ((contains fallbackInternalDynYul "internal_first") && + (contains fallbackInternalDynYul "xs_data_offset") && + (contains fallbackInternalDynYul "xs_length")) + -- Bugbot regression (b): ADT storage write payload must use internals-aware expr compile + -- so internal calls with dynamic args inside adtConstruct args expand correctly. + let adtInternalDynYul ← expectCompileToYul + "ADT ctor payload with internal dynamic/composite arg (Bugbot regression b: adt storage write skips internals)" + adtStorageInternalDynamicArgSpec + expectTrue "adtConstruct payload internalCall (short-form array arg) expands via threaded internalFunctions in compileAdtStorageWrite" + ((contains adtInternalDynYul "internal_first") && + (contains adtInternalDynYul "xs_data_offset") && + (contains adtInternalDynYul "xs_length")) let ceiInitialInternalCallCompiled := match Compiler.CompilationModel.compile ceiInitialInternalCallAllowedSpec (selectorsFor ceiInitialInternalCallAllowedSpec) with