Skip to content

Commit

Permalink
update effects to be an enum.Flag
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Sep 30, 2024
1 parent 8be0bef commit fcb688f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
4 changes: 2 additions & 2 deletions vyper/venom/basicblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ def is_bb_terminator(self) -> bool:
return self.opcode in BB_TERMINATORS

def get_read_effects(self):
return effects.reads.get(self.opcode, ())
return effects.reads.get(self.opcode, effects.EMPTY)

def get_write_effects(self):
return effects.writes.get(self.opcode, ())
return effects.writes.get(self.opcode, effects.EMPTY)

def get_label_operands(self) -> Iterator[IRLabel]:
"""
Expand Down
97 changes: 54 additions & 43 deletions vyper/venom/effects.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
_ALL = ("storage", "transient", "memory", "immutables", "balance", "returndata")
from enum import Flag, auto

_writes = {
"sstore": "storage",
"tstore": "transient",
"mstore": "memory",
"istore": "immutables",
"call": _ALL,
"delegatecall": _ALL,
"staticcall": ("memory", "returndata"),
"create": _ALL,
"create2": _ALL,
"invoke": _ALL, # could be smarter, look up the effects of the invoked function
"dloadbytes": "memory",
"returndatacopy": "memory",
"calldatacopy": "memory",
"codecopy": "memory",
"extcodecopy": "memory",
"mcopy": "memory",
}
_reads = {
"sload": "storage",
"tload": "transient",
"iload": "immutables",
"mload": "memory",
"mcopy": "memory",
"call": _ALL,
"delegatecall": _ALL,
"staticcall": _ALL,
"returndatasize": "returndata",
"returndatacopy": "returndata",
"balance": "balance",
"selfbalance": "balance",
"log": "memory",
"revert": "memory",
"return": "memory",
"sha3": "memory",
}

class Effects(Flag):
STORAGE = auto()
TRANSIENT = auto()
MEMORY = auto()
IMMUTABLES = auto()
BALANCE = auto()
RETURNDATA = auto()


def _mktuple(x):
if not isinstance(x, tuple):
x = (x,)
return x
EMPTY = Effects(0)
ALL = ~EMPTY
STORAGE = Effects.STORAGE
TRANSIENT = Effects.TRANSIENT
MEMORY = Effects.MEMORY
IMMUTABLES = Effects.IMMUTABLES
BALANCE = Effects.BALANCE
RETURNDATA = Effects.RETURNDATA


writes = {k: _mktuple(v) for k, v in _writes.items()}
reads = {k: _mktuple(v) for k, v in _reads.items()}
writes = {
"sstore": STORAGE,
"tstore": TRANSIENT,
"mstore": MEMORY,
"istore": IMMUTABLES,
"call": ALL,
"delegatecall": ALL,
"staticcall": MEMORY | RETURNDATA,
"create": ALL,
"create2": ALL,
"invoke": ALL, # could be smarter, look up the effects of the invoked function
"dloadbytes": MEMORY,
"returndatacopy": MEMORY,
"calldatacopy": MEMORY,
"codecopy": MEMORY,
"extcodecopy": MEMORY,
"mcopy": MEMORY,
}

reads = {
"sload": STORAGE,
"tload": TRANSIENT,
"iload": IMMUTABLES,
"mload": MEMORY,
"mcopy": MEMORY,
"call": ALL,
"delegatecall": ALL,
"staticcall": ALL,
"returndatasize": RETURNDATA,
"returndatacopy": RETURNDATA,
"balance": BALANCE,
"selfbalance": BALANCE,
"log": MEMORY,
"revert": MEMORY,
"return": MEMORY,
"sha3": MEMORY,
}

0 comments on commit fcb688f

Please sign in to comment.