Skip to content

Commit

Permalink
simplify ir_node_to_venom
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Mar 19, 2024
1 parent 5f4f548 commit c3e0ac9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 43 deletions.
55 changes: 14 additions & 41 deletions vyper/venom/ir_node_to_venom.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
)
from vyper.venom.function import IRFunction

_BINARY_IR_INSTRUCTIONS = frozenset(
# Instructions that are mapped to their inverse
INVERSE_MAPPED_IR_INSTRUCTIONS = {"ne": "eq", "le": "gt", "sle": "sgt", "ge": "lt", "sge": "slt"}

# Instructions that have a direct EVM opcode equivalent and can
# be passed through to the EVM assembly without special handling
PASS_THROUGH_INSTRUCTIONS = frozenset(
[
# binary instructions
"eq",
"gt",
"lt",
Expand All @@ -40,16 +46,6 @@
"sha3",
"sha3_64",
"signextend",
]
)

# Instructions that are mapped to their inverse
INVERSE_MAPPED_IR_INSTRUCTIONS = {"ne": "eq", "le": "gt", "sle": "sgt", "ge": "lt", "sge": "slt"}

# Instructions that have a direct EVM opcode equivalent and can
# be passed through to the EVM assembly without special handling
PASS_THROUGH_INSTRUCTIONS = frozenset(
[
"chainid",
"basefee",
"timestamp",
Expand Down Expand Up @@ -86,11 +82,6 @@
"assert",
"assert_unreachable",
"exit",
]
)

PASS_THROUGH_REVERSED_INSTRUCTIONS = frozenset(
[
"calldatacopy",
"mcopy",
"extcodecopy",
Expand All @@ -104,6 +95,9 @@
"create2",
"addmod",
"mulmod",
"call",
"delegatecall",
"staticcall",
]
)

Expand Down Expand Up @@ -135,16 +129,6 @@ def ir_node_to_venom(ir: IRnode) -> IRFunction:
return ctx


def _convert_binary_op(
ctx: IRFunction, ir: IRnode, symbols: SymbolTable, swap: bool = False
) -> Optional[IRVariable]:
ir_args = ir.args[::-1] if swap else ir.args
arg_0, arg_1 = _convert_ir_bb_list(ctx, ir_args, symbols)

assert isinstance(ir.value, str) # mypy hint
return ctx.get_basic_block().append_instruction(ir.value, arg_1, arg_0)


def _append_jmp(ctx: IRFunction, label: IRLabel) -> None:
bb = ctx.get_basic_block()
if bb.is_terminated:
Expand Down Expand Up @@ -210,12 +194,8 @@ def _handle_internal_func(
_convert_ir_bb(ctx, ir.args[0].args[2], symbols)


def _convert_ir_simple_node(
ctx: IRFunction, ir: IRnode, symbols: SymbolTable, reverse: bool = False
) -> Optional[IRVariable]:
args = [_convert_ir_bb(ctx, arg, symbols) for arg in ir.args]
if reverse:
args = reversed(args) # type: ignore
def _convert_ir_simple_node( ctx: IRFunction, ir: IRnode, symbols: SymbolTable) -> Optional[IRVariable]:
args = reversed([_convert_ir_bb(ctx, arg, symbols) for arg in ir.args])
return ctx.get_basic_block().append_instruction(ir.value, *args) # type: ignore


Expand Down Expand Up @@ -253,18 +233,14 @@ def _convert_ir_bb(ctx, ir, symbols):

ctx.push_source(ir)

if ir.value in _BINARY_IR_INSTRUCTIONS:
return _convert_binary_op(ctx, ir, symbols, ir.value in ["sha3_64"])
elif ir.value in INVERSE_MAPPED_IR_INSTRUCTIONS:
if ir.value in INVERSE_MAPPED_IR_INSTRUCTIONS:
org_value = ir.value
ir.value = INVERSE_MAPPED_IR_INSTRUCTIONS[ir.value]
new_var = _convert_binary_op(ctx, ir, symbols)
new_var = _convert_ir_simple_node(ctx, ir, symbols)
ir.value = org_value
return ctx.get_basic_block().append_instruction("iszero", new_var)
elif ir.value in PASS_THROUGH_INSTRUCTIONS:
return _convert_ir_simple_node(ctx, ir, symbols)
elif ir.value in PASS_THROUGH_REVERSED_INSTRUCTIONS:
return _convert_ir_simple_node(ctx, ir, symbols, reverse=True)
elif ir.value == "return":
ctx.get_basic_block().append_instruction(
"return", IRVariable("ret_size"), IRVariable("ret_ofst")
Expand Down Expand Up @@ -306,9 +282,6 @@ def _convert_ir_bb(ctx, ir, symbols):
ret = _convert_ir_bb(ctx, ir_node, symbols)

return ret
elif ir.value in ["delegatecall", "staticcall", "call"]:
args = reversed(_convert_ir_bb_list(ctx, ir.args, symbols))
return ctx.get_basic_block().append_instruction(ir.value, *args)
elif ir.value == "if":
cond = ir.args[0]

Expand Down
4 changes: 2 additions & 2 deletions vyper/venom/venom_to_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,10 @@ def _generate_evm_for_instruction(
elif opcode == "sha3_64":
assembly.extend(
[
*PUSH(MemoryPositions.FREE_VAR_SPACE2),
"MSTORE",
*PUSH(MemoryPositions.FREE_VAR_SPACE),
"MSTORE",
*PUSH(MemoryPositions.FREE_VAR_SPACE2),
"MSTORE",
*PUSH(64),
*PUSH(MemoryPositions.FREE_VAR_SPACE),
"SHA3",
Expand Down

0 comments on commit c3e0ac9

Please sign in to comment.