std:EVM/Base

lib/EVM/Base.omg

lib/EVM/Base.omg141 lines
1{-  The EVM monad: the base every action runs on (docs/evm.md, sections 3.1
2    and 4.1).
3
4    A base is a record of the EVM's operations over any monad `m`.  The
5    actions of `std:EVM/Effect` reach the EVM only through it, so a test
6    can run a contract over any base it likes: `Model.ops` for the
7    reference model, the staging of `src/contract` for the compiler.
8    `cps o` is the base of the compiler and of the reference model alike:
9    continuation passing over the signature `o`, whose `bind` is
10    meta-level, so that staging computes it away.
11-}
12
13let Monad = std:Monad.Monad
14
15let Signature = std:EVM/Signature
16
17let Key = Signature.Key
18
19let Chunk = Signature.Chunk
20
21-- The constants of the frame: the caller, the contract's own address, the
22-- value sent with the call, the call data (its size, and CALLDATALOAD),
23-- the block's timestamp and the immutables of the code, by number.
24let Frame =
25      \(Val : Type) ->
26        { caller : Val
27        , address : Val
28        , callvalue : Val
29        , cdsize : Val
30        , cdload : Val -> Val
31        , timestamp : Val
32        , immutable : Natural -> Val
33        }
34
35-- A case of a switch: its key (a word known at compile time) and its
36-- action.
37let Case = \(Val : Type) (a : Type) -> { key : Val, action : a }
38
39let Base =
40      \(Val : Type) (Flag : Type) (m : Type -> Type) ->
41        { monad : Monad m
42        , words : Signature.Words Val Flag
43        , keccakText : Text -> Val
44        , frame : Frame Val
45        , sload : Key Val -> m Val
46        , sstore : Key Val -> Val -> m unit
47        , tload : Val -> m Val
48        , tstore : Val -> Val -> m unit
49        , log : List Val -> List (Chunk Val) -> m unit
50        , call : Signature.Call Val -> m Flag
51        , staticcall : Signature.StaticCall Val -> m Flag
52        , rdsize : m Val
53        , rdload : Val -> m Val
54        , branch : forall {a : Type} -> Flag -> m a -> m a -> m a
55        , switch : forall {a : Type} -> Val -> List (Case Val (m a)) -> m a -> m a
56        , loop : Val -> List Val -> (Val -> List Val -> m (List Val)) -> m (List Val)
57        , share : forall {a : Type} -> List Val -> (List Val -> m a) -> m a
58        , ret : forall {a : Type} -> List (Chunk Val) -> m a
59        , output : forall {a : Type} -> List (Signature.Piece Val) -> m a
60        , revert : forall {a : Type} -> List (Chunk Val) -> m a
61        , bubble : forall {a : Type} -> m a
62        }
63
64-- Continuation passing: an action is given the rest of the execution.
65let Cps = \(Code : Type) (a : Type) -> (a -> Code) -> Code
66
67let cpsMonad
68    : forall {Code : Type} -> Monad (Cps Code)
69    = \{Code} ->
70        { pure = \{a} (x : a) (k : a -> Code) -> k x
71        , bind =
72            \{a} {b} (x : Cps Code a) (f : a -> Cps Code b) (k : b -> Code) -> x (\(y : a) -> f y k)
73        }
74
75-- The base over a signature.  Each operation is the signature's, given
76-- the continuation; a branch or a switch gives every arm the rest of the
77-- execution through `join`, which lets the compiler generate it once, and
78-- a halt drops it.
79let cps
80    : forall {Val : Type} {Flag : Type} {Code : Type}
81      -> Signature.Ops Val Flag Code
82      -> Base Val Flag (Cps Code)
83    = \{Val} {Flag} {Code} o ->
84        { monad = cpsMonad @Code
85        , words = o.words
86        , keccakText = o.keccakText
87        , frame =
88            { caller = o.caller
89            , address = o.address
90            , callvalue = o.callvalue
91            , cdsize = o.cdsize
92            , cdload = o.cdload
93            , timestamp = o.timestamp
94            , immutable = o.immutable
95            }
96        , sload = \(key : Key Val) (k : Val -> Code) -> o.sload key k
97        , sstore = \(key : Key Val) (v : Val) (k : unit -> Code) -> o.sstore key v (k {=})
98        , tload = \(slot : Val) (k : Val -> Code) -> o.tload slot k
99        , tstore = \(slot : Val) (v : Val) (k : unit -> Code) -> o.tstore slot v (k {=})
100        , log =
101            \(topics : List Val) (data : List (Chunk Val)) (k : unit -> Code) ->
102              o.log topics data (k {=})
103        , call = \(req : Signature.Call Val) (k : Flag -> Code) -> o.call req k
104        , staticcall = \(req : Signature.StaticCall Val) (k : Flag -> Code) -> o.staticcall req k
105        , rdsize = \(k : Val -> Code) -> o.rdsize k
106        , rdload = \(offset : Val) (k : Val -> Code) -> o.rdload offset k
107        , branch =
108            \{a} (c : Flag) (x : Cps Code a) (y : Cps Code a) (k : a -> Code) ->
109              o.join a k (\(j : a -> Code) -> o.branch c (x j) (y j))
110        , switch =
111            \{a}
112             (v : Val)
113             (cases : List (Case Val (Cps Code a)))
114             (other : Cps Code a)
115             (k : a -> Code) ->
116              o.join
117                a
118                k
119                (\(j : a -> Code) ->
120                   o.switch
121                     v
122                     (std:List.map
123                        (\(c : Case Val (Cps Code a)) -> { key = c.key, code = c.action j })
124                        cases)
125                     (other j))
126        , loop =
127            \(n : Val)
128             (init : List Val)
129             (body : Val -> List Val -> Cps Code (List Val))
130             (k : List Val -> Code) ->
131              o.loop n init body k
132        , share =
133            \{a} (ws : List Val) (f : List Val -> Cps Code a) (k : a -> Code) ->
134              o.share ws (\(vs : List Val) -> f vs k)
135        , ret = \{a} (data : List (Chunk Val)) (_ : a -> Code) -> o.ret data
136        , output = \{a} (data : List (Signature.Piece Val)) (_ : a -> Code) -> o.output data
137        , revert = \{a} (data : List (Chunk Val)) (_ : a -> Code) -> o.revert data
138        , bubble = \{a} (_ : a -> Code) -> o.bubble
139        }
140
141in  { Frame, Case, Base, Cps, cpsMonad, cps }
Your result
Complete file: lib/EVM/Base.omg · with imports