Skip to content

Commit fe98983

Browse files
committed
AST: move timing to storage variables only
1 parent 93d7fe6 commit fe98983

File tree

12 files changed

+96
-103
lines changed

12 files changed

+96
-103
lines changed

src/Act/Coq.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ retVal _ = return ""
147147
-- 'handler' defines what to do in cases where a given name isn't updated
148148
stateval :: Store -> Id -> (Ref Storage -> SlotType -> T.Text) -> [StorageUpdate] -> T.Text
149149
stateval store contract handler updates = T.unwords $
150-
stateConstructor : fmap (\(n, (t, _)) -> updateVar store updates handler (SVar nowhere contract n) t) (M.toList store')
150+
stateConstructor : fmap (\(n, (t, _)) -> updateVar store updates handler (SVar nowhere contract n Pre) t) (M.toList store')
151151
where
152152
store' = contractStore contract store
153153

@@ -166,7 +166,7 @@ eqRef r (Update _ (Item _ _ r') _) = r == r'
166166
baseRef :: Ref Storage -> StorageUpdate -> Bool
167167
baseRef baseref (Update _ (Item _ _ r) _) = hasBase r
168168
where
169-
hasBase (SVar _ _ _) = False
169+
hasBase (SVar _ _ _ _) = False
170170
hasBase (SMapping _ r' _) = r' == baseref || hasBase r'
171171
hasBase (SField _ r' _ _) = r' == baseref || hasBase r'
172172

@@ -306,7 +306,7 @@ coqexp (UIntMax _ n) = parens $ "UINT_MAX " <> T.pack (show n)
306306
coqexp (InRange _ t e) = coqexp (bound t e)
307307

308308
-- polymorphic
309-
coqexp (TEntry _ w _ e) = entry e w
309+
coqexp (TEntry _ _ e) = entry e
310310
coqexp (ITE _ b e1 e2) = parens $ "if "
311311
<> coqexp b
312312
<> " then "
@@ -349,13 +349,13 @@ coqprop e = error "ill formed proposition: " <> T.pack (show e)
349349
typedexp :: TypedExp -> T.Text
350350
typedexp (TExp _ e) = coqexp e
351351

352-
entry :: TItem k a -> When -> T.Text
353-
entry (Item SByteStr _ _) _ = error "bytestrings not supported"
354-
entry e Post = error $ "TODO: missing support for poststate references in coq backend. Entry: \n" <> show e
355-
entry (Item _ _ r) _ = ref r
352+
entry :: TItem k a -> T.Text
353+
entry (Item SByteStr _ _) = error "bytestrings not supported"
354+
entry (Item _ _ r) = ref r
356355

357356
ref :: Ref k -> T.Text
358-
ref (SVar _ _ name) = parens $ T.pack name <> " " <> stateVar
357+
ref (SVar _ _ name Post) = error $ "TODO: missing support for poststate references in coq backend. Entry: \n" <> name
358+
ref (SVar _ _ name _) = parens $ T.pack name <> " " <> stateVar
359359
ref (CVar _ _ name) = T.pack name
360360
ref (SMapping _ r ixs) = parens $ ref r <> " " <> coqargs ixs
361361
ref (SField _ r cid name) = parens $ T.pack cid <> "." <> T.pack name <> " " <> ref r

src/Act/Decompile.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ mkRewrites cname (DistinctStore writes) = forM (Map.toList writes) $ \(name,(val
277277
AbiTupleType _ -> Left "cannot decompile methods that write to tuple in storage"
278278
AbiFunctionType -> Left "cannot decompile methods that store function pointers"
279279
_ -> do
280-
pure (Update SInteger (Item SInteger v (SVar nowhere (T.unpack cname) (T.unpack name))) val)
280+
pure (Update SInteger (Item SInteger v (SVar nowhere (T.unpack cname) (T.unpack name) Post)) val)
281281
Dynamic -> Left "cannot decompile methods that store dynamically sized types"
282282
ContractType {} -> Left "cannot decompile contracts that have contract types in storage"
283283
StorageMapping {} -> Left "cannot decompile contracts that write to mappings"
@@ -378,7 +378,7 @@ fromWord layout w = simplify <$> go w
378378
go :: EVM.Expr EVM.EWord -> Either Text (Exp AInteger)
379379
go (EVM.Lit a) = Right $ LitInt nowhere (toInteger a)
380380
-- TODO: get the actual abi type from the compiler output
381-
go (EVM.Var a) = Right $ _Var Pre (AbiBytesType 32) (T.unpack a)
381+
go (EVM.Var a) = Right $ _Var (AbiBytesType 32) (T.unpack a)
382382
go (EVM.TxValue) = Right $ IntEnv nowhere Callvalue
383383

384384
-- overflow checks
@@ -438,7 +438,7 @@ fromWord layout w = simplify <$> go w
438438
Nothing -> Left "read from a storage location that is not present in the solc layout"
439439
Just (nm, tp) -> case tp of
440440
-- TODO: get lookup contract name by address
441-
StorageValue t@(PrimitiveType _) -> Right $ TEntry nowhere Pre SStorage (Item SInteger t (SVar nowhere (T.unpack "Basic") (T.unpack nm)))
441+
StorageValue t@(PrimitiveType _) -> Right $ TEntry nowhere SStorage (Item SInteger t (SVar nowhere (T.unpack "Basic") (T.unpack nm) Pre))
442442
_ -> Left $ "unable to handle storage reads for variables of type: " <> T.pack (show tp)
443443

444444
go e = err e

src/Act/Enrich.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ mkStorageBounds refs = concatMap mkBound refs
8383

8484
-- TODO why only Pre items here?
8585
fromItem :: TItem AInteger Storage -> Exp ABoolean
86-
fromItem item@(Item _ (PrimitiveType vt) _) = bound vt (TEntry nowhere Pre SStorage item)
86+
fromItem item@(Item _ (PrimitiveType vt) _) = bound vt (TEntry nowhere SStorage item)
8787
fromItem (Item _ (ContractType _) _) = LitBool nowhere True
8888

8989
mkStorageBoundsLoc :: [StorageLocation] -> [Exp ABoolean]
@@ -95,5 +95,5 @@ mkStorageBoundsLoc refs = concatMap mkBound refs
9595

9696
mkCallDataBounds :: [Decl] -> [Exp ABoolean]
9797
mkCallDataBounds = concatMap $ \(Decl typ name) -> case fromAbiType typ of
98-
AInteger -> [bound typ (_Var Pre typ name)]
98+
AInteger -> [bound typ (_Var typ name)]
9999
_ -> []

src/Act/HEVM.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@ substItem subst (Item st vt sref) = case substRef subst sref of
274274
ERef k ref -> ETItem k (Item st vt ref)
275275

276276
substRef :: M.Map Id TypedExp -> Ref k -> ERef
277-
substRef _ var@(SVar _ _ _) = ERef SStorage var
277+
substRef _ var@(SVar _ _ _ _) = ERef SStorage var
278278
substRef subst (CVar _ _ x) = case M.lookup x subst of
279-
Just (TExp _ (TEntry _ _ k (Item _ _ ref))) -> ERef k ref
279+
Just (TExp _ (TEntry _ k (Item _ _ ref))) -> ERef k ref
280280
Just _ -> error "Internal error: cannot access fields of non-pointer var"
281281
Nothing -> error "Internal error: ill-formed substitution"
282282
substRef subst (SMapping pn sref args) = case substRef subst sref of
@@ -328,11 +328,11 @@ substExp subst expr = case expr of
328328

329329
ITE pn a b c -> ITE pn (substExp subst a) (substExp subst b) (substExp subst c)
330330

331-
TEntry _ _ SCalldata (Item st _ (CVar _ _ x)) -> case M.lookup x subst of
331+
TEntry _ SCalldata (Item st _ (CVar _ _ x)) -> case M.lookup x subst of
332332
Just (TExp st' exp') -> maybe (error "Internal error: type missmatch") (\Refl -> exp') $ testEquality st st'
333333
Nothing -> error "Internal error: Ill-defined substitution"
334-
TEntry pn whn _ item -> case substItem subst item of
335-
ETItem k' item' -> TEntry pn whn k' item'
334+
TEntry pn _ item -> case substItem subst item of
335+
ETItem k' item' -> TEntry pn k' item'
336336

337337
Create pn a b -> Create pn a (substArgs subst b)
338338

@@ -373,7 +373,7 @@ getSlot layout cid name =
373373

374374
refOffset :: Monad m => ContractMap -> Ref k -> ActT m (EVM.Expr EVM.EWord)
375375
refOffset _ (CVar _ _ _) = error "Internal error: ill-typed entry"
376-
refOffset _ (SVar _ cid name) = do
376+
refOffset _ (SVar _ cid name _) = do
377377
layout <- getLayout
378378
let slot = getSlot layout cid name
379379
pure $ EVM.Lit (fromIntegral slot)
@@ -388,7 +388,7 @@ refOffset _ (SField _ _ cid name) = do
388388
pure $ EVM.Lit (fromIntegral slot)
389389

390390
baseAddr :: Monad m => ContractMap -> Ref k -> ActT m (EVM.Expr EVM.EAddr)
391-
baseAddr _ (SVar _ _ _) = getCaddr
391+
baseAddr _ (SVar _ _ _ _) = getCaddr
392392
baseAddr _ (CVar _ _ _) = error "Internal error: ill-typed entry"
393393
baseAddr cmap (SField _ ref _ _) = do
394394
expr <- refToExp cmap ref
@@ -445,7 +445,7 @@ toProp cmap = \case
445445
pure $ EVM.PNeg e
446446
(NEq _ _ _ _) -> error "unsupported"
447447
(ITE _ _ _ _) -> error "Internal error: expecting flat expression"
448-
(TEntry _ _ _ _) -> error "TODO" -- EVM.SLoad addr idx
448+
(TEntry _ _ _) -> error "TODO" -- EVM.SLoad addr idx
449449
(InRange _ t e) -> toProp cmap (inRange t e)
450450
where
451451
op2 :: Monad m => forall a b. (EVM.Expr (ExprType b) -> EVM.Expr (ExprType b) -> a) -> Exp b -> Exp b -> ActT m a
@@ -527,7 +527,7 @@ toExpr cmap = fmap stripMods . go
527527
pure $ EVM.Not e
528528
(NEq _ _ _ _) -> error "unsupported"
529529

530-
(TEntry _ _ _ (Item SInteger _ ref)) -> refToExp cmap ref
530+
(TEntry _ _ (Item SInteger _ ref)) -> refToExp cmap ref
531531

532532
e@(ITE _ _ _ _) -> error $ "Internal error: expecting flat expression. got: " <> show e
533533

@@ -572,7 +572,7 @@ inRange t e = bound t e
572572

573573
checkOp :: Exp AInteger -> Exp ABoolean
574574
checkOp (LitInt _ i) = LitBool nowhere $ i <= (fromIntegral (maxBound :: Word256))
575-
checkOp (TEntry _ _ _ _) = LitBool nowhere True
575+
checkOp (TEntry _ _ _) = LitBool nowhere True
576576
checkOp e@(Add _ e1 _) = LEQ nowhere e1 e -- check for addition overflow
577577
checkOp e@(Sub _ e1 _) = LEQ nowhere e e1
578578
checkOp (Mul _ e1 e2) = Or nowhere (Eq nowhere SInteger e1 (LitInt nowhere 0))

src/Act/Print.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ prettyExp e = case e of
142142

143143
--polymorphic
144144
ITE _ a b c -> "(if " <> prettyExp a <> " then " <> prettyExp b <> " else " <> prettyExp c <> ")"
145-
TEntry _ t _ a -> timeParens t $ prettyItem a
145+
TEntry _ _ a -> prettyItem a
146146
where
147147
print2 sym a b = "(" <> prettyExp a <> " " <> sym <> " " <> prettyExp b <> ")"
148148

@@ -155,7 +155,7 @@ prettyItem (Item _ _ r) = prettyRef r
155155
prettyRef :: Ref k t -> String
156156
prettyRef = \case
157157
CVar _ _ n -> n
158-
SVar _ _ n -> n
158+
SVar _ n _ t -> timeParens t n
159159
SMapping _ r args -> prettyRef r <> concatMap (brackets . prettyTypedExp) args
160160
SField _ r _ n -> prettyRef r <> "." <> n
161161
where
@@ -197,7 +197,7 @@ prettyInvPred = prettyExp . untime . fst
197197
untimeTyped (TExp t e) = TExp t (untime e)
198198

199199
untimeRef:: Ref k t -> Ref k Untimed
200-
untimeRef (SVar p c a) = SVar p c a
200+
untimeRef (SVar p c a _) = SVar p c a Neither
201201
untimeRef (CVar p c a) = CVar p c a
202202
untimeRef (SMapping p e xs) = SMapping p (untimeRef e) (fmap untimeTyped xs)
203203
untimeRef (SField p e c x) = SField p (untimeRef e) c x
@@ -235,7 +235,7 @@ prettyInvPred = prettyExp . untime . fst
235235
ByEnv p a -> ByEnv p a
236236
ITE p x y z -> ITE p (untime x) (untime y) (untime z)
237237
Slice p a b c -> Slice p (untime a) (untime b) (untime c)
238-
TEntry p _ k (Item t vt a) -> TEntry p Neither k (Item t vt (untimeRef a))
238+
TEntry p k (Item t vt a) -> TEntry p k (Item t vt (untimeRef a))
239239

240240

241241
-- | Doc type for terminal output

src/Act/SMT.hs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ parseSMTModel s = if length s0Caps == 1
540540
encodeUpdate :: Id -> StorageUpdate -> SMT2
541541
encodeUpdate behvName (Update _ item expr) =
542542
let
543-
postentry = withInterface behvName $ expToSMT2 (TEntry nowhere Post SStorage item)
543+
postentry = withInterface behvName $ expToSMT2 (TEntry nowhere SStorage item)
544544
expression = withInterface behvName $ expToSMT2 expr
545545
in "(assert (= " <> postentry <> " " <> expression <> "))"
546546

@@ -551,7 +551,7 @@ encodeConstant loc = "(assert (= " <> nameFromLoc Pre loc <> " " <> nameFromLoc
551551
declareStorage :: [When] -> StorageLocation -> [SMT2]
552552
declareStorage times (Loc _ item@(Item _ _ ref)) = declareRef ref
553553
where
554-
declareRef (SVar _ _ _) = (\t -> constant (nameFromSItem t item) (itemType item) ) <$> times
554+
declareRef (SVar _ _ _ _) = (\t -> constant (nameFromSItem t item) (itemType item) ) <$> times
555555
declareRef (SMapping _ _ ixs) = (\t -> array (nameFromSItem t item) ixs (itemType item)) <$> times
556556
declareRef (SField _ ref' _ _) = declareRef ref'
557557

@@ -625,7 +625,7 @@ expToSMT2 expr = case expr of
625625
Eq _ _ a b -> binop "=" a b
626626
NEq p s a b -> unop "not" (Eq p s a b)
627627
ITE _ a b c -> triop "ite" a b c
628-
TEntry _ w _ item -> entry item w
628+
TEntry _ _ item -> entry item
629629
where
630630
unop :: String -> Exp a -> Ctx SMT2
631631
unop op a = ["(" <> op <> " " <> a' <> ")" | a' <- expToSMT2 a]
@@ -638,11 +638,11 @@ expToSMT2 expr = case expr of
638638
triop op a b c = ["(" <> op <> " " <> a' <> " " <> b' <> " " <> c' <> ")"
639639
| a' <- expToSMT2 a, b' <- expToSMT2 b, c' <- expToSMT2 c]
640640

641-
entry :: TItem k a -> When -> Ctx SMT2
642-
entry item whn = case ixsFromItem item of
643-
[] -> nameFromItem whn item
641+
entry :: TItem k a -> Ctx SMT2
642+
entry item = case ixsFromItem item of
643+
[] -> nameFromItem item
644644
(ix:ixs) -> do
645-
name <- nameFromItem whn item
645+
name <- nameFromItem item
646646
select name (ix :| ixs)
647647

648648
-- | SMT2 has no support for exponentiation, but we can do some preprocessing
@@ -689,23 +689,19 @@ sType' (TExp t _) = sType $ actType t
689689

690690
-- Construct the smt2 variable name for a given storage item
691691
nameFromSItem :: When -> TItem a Storage -> Id
692-
nameFromSItem whn (Item _ _ ref) = nameFromSRef ref @@ show whn
692+
nameFromSItem whn (Item _ _ ref) = nameFromSRef whn ref @@ show whn
693693

694-
nameFromSRef :: Ref Storage -> Id
695-
nameFromSRef (SVar _ c name) = c @@ name
696-
nameFromSRef (SMapping _ e _) = nameFromSRef e
697-
nameFromSRef (SField _ ref c x) = nameFromSRef ref @@ c @@ x
694+
nameFromSRef :: When -> Ref Storage -> Id
695+
nameFromSRef whn (SVar _ c name _) = c @@ name @@ show whn
696+
nameFromSRef whn (SMapping _ e _) = nameFromSRef whn e
697+
nameFromSRef whn (SField _ ref c x) = nameFromSRef whn ref @@ c @@ x
698698

699-
nameFromItem :: When -> TItem k a -> Ctx Id
700-
nameFromItem whn (Item _ _ ref) = do
701-
name <- nameFromRef ref
702-
case ref of -- TODO: this feels rather adhoc, but I can't find a better way to handle timings
703-
CVar _ _ _ -> pure name
704-
_ -> pure $ name @@ show whn
699+
nameFromItem :: TItem k a -> Ctx Id
700+
nameFromItem (Item _ _ ref) = nameFromRef ref
705701

706702
nameFromRef :: Ref k -> Ctx Id
707703
nameFromRef (CVar _ _ name) = nameFromVarId name
708-
nameFromRef (SVar _ c name) = pure $ c @@ name
704+
nameFromRef (SVar _ c name whn) = pure $ c @@ name @@ show whn
709705
nameFromRef (SMapping _ e _) = nameFromRef e
710706
nameFromRef (SField _ ref c x) = do
711707
name <- nameFromRef ref

src/Act/Syntax.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ locsFromExp = nub . go
113113
ByEnv {} -> []
114114
Create _ _ es -> concatMap locsFromTypedExp es
115115
ITE _ x y z -> go x <> go y <> go z
116-
TEntry _ _ k a -> locsFromItem k a
116+
TEntry _ k a -> locsFromItem k a
117117

118118
createsFromExp :: Exp a t -> [Id]
119119
createsFromExp = nub . go
@@ -151,7 +151,7 @@ createsFromExp = nub . go
151151
ByEnv {} -> []
152152
Create _ f es -> [f] <> concatMap createsFromTypedExp es
153153
ITE _ x y z -> go x <> go y <> go z
154-
TEntry _ _ _ a -> createsFromItem a
154+
TEntry _ _ a -> createsFromItem a
155155

156156
createsFromItem :: TItem k a t -> [Id]
157157
createsFromItem item = concatMap createsFromTypedExp (ixsFromItem item)
@@ -264,13 +264,13 @@ ethEnvFromExp = nub . go
264264
IntEnv _ a -> [a]
265265
ByEnv _ a -> [a]
266266
Create _ _ ixs -> concatMap ethEnvFromTypedExp ixs
267-
TEntry _ _ _ a -> ethEnvFromItem a
267+
TEntry _ _ a -> ethEnvFromItem a
268268

269269
idFromItem :: TItem k a t -> Id
270270
idFromItem (Item _ _ ref) = idFromRef ref
271271

272272
idFromRef :: Ref k t -> Id
273-
idFromRef (SVar _ _ x) = x
273+
idFromRef (SVar _ _ x _) = x
274274
idFromRef (CVar _ _ x) = x
275275
idFromRef (SMapping _ e _) = idFromRef e
276276
idFromRef (SField _ e _ _) = idFromRef e
@@ -285,7 +285,7 @@ ixsFromItem :: TItem k a t -> [TypedExp t]
285285
ixsFromItem (Item _ _ item) = ixsFromRef item
286286

287287
ixsFromRef :: Ref k t -> [TypedExp t]
288-
ixsFromRef (SVar _ _ _) = []
288+
ixsFromRef (SVar _ _ _ _) = []
289289
ixsFromRef (CVar _ _ _) = []
290290
ixsFromRef (SMapping _ ref ixs) = ixs ++ ixsFromRef ref
291291
ixsFromRef (SField _ ref _ _) = ixsFromRef ref
@@ -342,7 +342,7 @@ posnFromExp e = case e of
342342
Eq p _ _ _ -> p
343343
NEq p _ _ _ -> p
344344
ITE p _ _ _ -> p
345-
TEntry p _ _ _ -> p
345+
TEntry p _ _ -> p
346346
--------------------------------------
347347
-- * Extraction from untyped ASTs * --
348348
--------------------------------------

src/Act/Syntax/Annotated.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE DataKinds #-}
33
{-# LANGUAGE PatternSynonyms #-}
44
{-# LANGUAGE RecordWildCards #-}
5+
{-# LANGUAGE InstanceSigs #-}
56

67
{-|
78
Module : Syntax.Annotated
@@ -63,6 +64,5 @@ instance Annotatable Agnostic.Behaviour where
6364
}
6465

6566
instance Annotatable Agnostic.StorageUpdate where
66-
-- The timing in items only refers to the timing of mapping indices of a
67-
-- storage update. Hence, it should be Pre
68-
annotate (Update typ item expr) = Update typ (setPre item) (setPre expr)
67+
annotate :: Agnostic.StorageUpdate Untimed -> Agnostic.StorageUpdate Timed
68+
annotate (Update typ item expr) = Update typ (setPost item) (setPre expr)

0 commit comments

Comments
 (0)