Skip to content

Commit

Permalink
"naming things" refactor
Browse files Browse the repository at this point in the history
Refactored append instructions into a single append_instruction() method
with named arguments for returning or not a variable
  • Loading branch information
harkal committed Dec 8, 2023
1 parent fb8b8b6 commit cefe061
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 111 deletions.
4 changes: 2 additions & 2 deletions vyper/venom/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from vyper.utils import OrderedSet
from vyper.venom.basicblock import (
BB_TERMINATORS,
CFG_ALTERING_OPS,
CFG_ALTERING_INSTRUCTIONS,
IRBasicBlock,
IRInstruction,
IRVariable,
Expand Down Expand Up @@ -55,7 +55,7 @@ def calculate_cfg(ctx: IRFunction) -> None:
assert last_inst.opcode in BB_TERMINATORS, f"Last instruction should be a terminator {bb}"

for inst in bb.instructions:
if inst.opcode in CFG_ALTERING_OPS:
if inst.opcode in CFG_ALTERING_INSTRUCTIONS:
ops = inst.get_label_operands()
for op in ops:
ctx.get_basic_block(op.value).add_cfg_in(bb)
Expand Down
51 changes: 42 additions & 9 deletions vyper/venom/basicblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,25 @@
]
)

CFG_ALTERING_OPS = frozenset(["jmp", "jnz", "call", "staticcall", "invoke", "deploy"])
NO_OUTPUT_INSTRUCTIONS = frozenset(
[
"mstore",
"sstore",
"dstore",
"calldatacopy",
"codecopy",
"return",
"ret",
"revert",
"assert",
"selfdestruct",
"stop",
"invalid",
"log",
]
)

CFG_ALTERING_INSTRUCTIONS = frozenset(["jmp", "jnz", "call", "staticcall", "invoke", "deploy"])

if TYPE_CHECKING:
from vyper.venom.function import IRFunction
Expand Down Expand Up @@ -243,8 +260,8 @@ class IRBasicBlock:
%2 = mul %1, 2
is represented as:
bb = IRBasicBlock("bb", function)
r1 = bb.append_inst("add", "%0", "1")
r2 = bb.append_inst("mul", r1, "2")
r1 = bb.append_instruction("add", "%0", "1")
r2 = bb.append_instruction("mul", r1, "2")
The label of a basic block is used to refer to it from other basic blocks
in order to branch to it.
Expand Down Expand Up @@ -296,13 +313,29 @@ def remove_cfg_out(self, bb: "IRBasicBlock") -> None:
def is_reachable(self) -> bool:
return len(self.cfg_in) > 0

def append_inst_no_ret(self, opcode: str, *args) -> None:
inst = IRInstruction(opcode, list(args))
inst.parent = self
self.instructions.append(inst)
def append_instruction(
self, opcode: str, *args: IROperand, output: bool = None, force: bool = False
) -> Optional[IRVariable]:
"""
Append an instruction to basic block. If output is True, the instruction
produces an output value, which can be used as an operand in other
instructions. If output is False, the instruction does not produce an
output value, and the return value is None. If output is None, the function
will automatically determine if the instruction produces an output value based
on the opcode.
The method will assert the output value is appropriate for the opcode, unless
force is True. This is useful for the case that you want to append an instruction
and performa manual manipulations later.
"""
if output is None:
output = opcode not in NO_OUTPUT_INSTRUCTIONS

assert force or not (
output is True and opcode in NO_OUTPUT_INSTRUCTIONS
), f"output=={output} without appropriate opcode '{opcode}'"

def append_inst(self, opcode: str, *args) -> IRVariable:
ret = self.parent.get_next_variable()
ret = self.parent.get_next_variable() if output else None
inst = IRInstruction(opcode, list(args), ret)
inst.parent = self
self.instructions.append(inst)
Expand Down
Loading

0 comments on commit cefe061

Please sign in to comment.