Skip to content
1 change: 1 addition & 0 deletions Compiler/CompilationModel.lean
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Compiler.CompilationModel.MappingWrites
import Compiler.CompilationModel.ScopeValidation
import Compiler.CompilationModel.StorageWrites
import Compiler.CompilationModel.TrustSurface
import Compiler.CompilationModel.YulImporter
import Compiler.CompilationModel.UsageAnalysis
import Compiler.CompilationModel.ValidationCalls
import Compiler.CompilationModel.ValidationEvents
Expand Down
26 changes: 26 additions & 0 deletions Compiler/CompilationModel/Compile.lean
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ namespace Compiler.CompilationModel
open Compiler
open Compiler.Yul

/-- Single bridge from typed unsafe/raw Yul fragments into the EVMYul AST.
Proof obligations and trust metadata live on `UnsafeYulFragment`; this
function is intentionally the only compiler lowering point for that escape
hatch. -/
def unsafeYulToEVMYul (fragment : UnsafeYulFragment) : List YulStmt :=
fragment.stmts

theorem unsafeYulToEVMYul_eq (fragment : UnsafeYulFragment) :
unsafeYulToEVMYul fragment = fragment.stmts := rfl

private def compileAdtStorageWrite (fields : List Field)
(dynamicSource : DynamicDataSource) (adtTypes : List AdtTypeDef)
(storageField adtName variantName : String) (args : List Expr) :
Expand Down Expand Up @@ -270,6 +280,9 @@ def compileStmt (fields : List Field) (events : List EventDef := [])
-- Unsafe block: transparent wrapper, compile inner body directly (#1728, Axis 6 Step 6a)
compileStmtList fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes body

| Stmt.unsafeYul fragment =>
pure (unsafeYulToEVMYul fragment)

| Stmt.emit eventName args => do
compileEmit fields events dynamicSource eventName args

Expand Down Expand Up @@ -467,4 +480,17 @@ def compileMatchAdtBranches (fields : List Field) (events : List EventDef)
pure ((variant.tag, fieldBindings ++ bodyStmts) :: restCases)
end

theorem compileStmt_unsafeYul
(fields : List Field) (events : List EventDef := [])
(errors : List ErrorDef := [])
(dynamicSource : DynamicDataSource := .calldata)
(internalRetNames : List String := [])
(isInternal : Bool := false)
(inScopeNames : List String := [])
(adtTypes : List AdtTypeDef := [])
(fragment : UnsafeYulFragment) :
compileStmt fields events errors dynamicSource internalRetNames isInternal inScopeNames adtTypes
(Stmt.unsafeYul fragment) = pure fragment.stmts := by
simp [compileStmt, unsafeYulToEVMYul]

end Compiler.CompilationModel
2 changes: 2 additions & 0 deletions Compiler/CompilationModel/LogicalPurity.lean
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ def stmtContainsUnsafeLogicalCallLike : Stmt → Bool
exprListAnyUnsafeLogicalCallLike args
| Stmt.ecm _ args =>
exprListAnyUnsafeLogicalCallLike args
| Stmt.unsafeYul _ =>
false
termination_by s => sizeOf s
decreasing_by all_goals simp_wf; all_goals omega

Expand Down
12 changes: 12 additions & 0 deletions Compiler/CompilationModel/ScopeValidation.lean
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,18 @@ def validateScopedStmtIdentifiers
| Stmt.returnBytes _ | Stmt.returnStorageWords _
| Stmt.revertReturndata | Stmt.stop =>
pure localScope
| Stmt.unsafeYul fragment => do
let mut scope := localScope
for name in fragment.scopeEffects.bindNames do
if paramScope.contains name then
throw s!"Compilation error: {context} unsafe Yul fragment '{fragment.label}' result '{name}' shadows a parameter"
if scope.contains name then
throw s!"Compilation error: {context} unsafe Yul fragment '{fragment.label}' redeclares result '{name}' in the same scope"
scope := name :: scope
for name in fragment.scopeEffects.assignNames do
if !scope.contains name then
throw s!"Compilation error: {context} unsafe Yul fragment '{fragment.label}' assigns to undeclared local variable '{name}'"
pure scope
termination_by s => sizeOf s
decreasing_by all_goals simp_wf; all_goals omega

Expand Down
Loading
Loading