Skip to content

Commit

Permalink
feat: add more venom instructions (#3733)
Browse files Browse the repository at this point in the history
- add support for more IRnode instructions:
  * not
  * ceil32
  * select
  * blockhash
- improve assert reason for missed cases
- fix CFG_ALTERING_INSTRUCTIONS - invoke/call/staticcall should not
  change the CFG!
  • Loading branch information
charles-cooper authored Jan 18, 2024
1 parent c42b077 commit 4177745
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion vyper/venom/basicblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
]
)

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

if TYPE_CHECKING:
from vyper.venom.function import IRFunction
Expand Down
19 changes: 16 additions & 3 deletions vyper/venom/ir_node_to_venom.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"chainid",
"basefee",
"timestamp",
"blockhash",
"caller",
"selfbalance",
"calldatasize",
Expand All @@ -65,7 +66,7 @@
"coinbase",
"number",
"iszero",
"ceil32",
"not",
"calldataload",
"extcodesize",
"extcodehash",
Expand Down Expand Up @@ -252,12 +253,13 @@ def _convert_ir_bb_list(ctx, ir, symbols, variables, allocated_variables):
ret = []
for ir_node in ir:
venom = _convert_ir_bb(ctx, ir_node, symbols, variables, allocated_variables)
assert venom is not None, ir_node
ret.append(venom)
return ret


def _convert_ir_bb(ctx, ir, symbols, variables, allocated_variables):
assert isinstance(ir, IRnode)
assert isinstance(ir, IRnode), ir
assert isinstance(variables, OrderedSet)
global _break_target, _continue_target

Expand Down Expand Up @@ -631,7 +633,9 @@ def _convert_ir_bb(ctx, ir, symbols, variables, allocated_variables):
if sym_ir.is_literal:
sym = symbols.get(f"&{sym_ir.value}", None)
if sym is None:
new_var = bb.append_instruction("store", sym_ir)
new_var = _convert_ir_bb(
ctx, sym_ir, symbols, variables, allocated_variables
)
symbols[f"&{sym_ir.value}"] = new_var
if allocated_variables.get(var.name, None) is None:
allocated_variables[var.name] = new_var
Expand Down Expand Up @@ -709,6 +713,15 @@ def _convert_ir_bb(ctx, ir, symbols, variables, allocated_variables):
else:
symbols[sym_ir.value] = arg_1
return arg_1
elif ir.value == "ceil32":
x = ir.args[0]
expanded = IRnode.from_list(["and", ["add", x, 31], ["not", 31]])
return _convert_ir_bb(ctx, expanded, symbols, variables, allocated_variables)
elif ir.value == "select":
# b ^ ((a ^ b) * cond) where cond is 1 or 0
cond, a, b = ir.args
expanded = IRnode.from_list(["xor", b, ["mul", cond, ["xor", a, b]]])
return _convert_ir_bb(ctx, expanded, symbols, variables, allocated_variables)

elif ir.value in ["sload", "iload"]:
arg_0 = _convert_ir_bb(ctx, ir.args[0], symbols, variables, allocated_variables)
Expand Down
1 change: 1 addition & 0 deletions vyper/venom/venom_to_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"exp",
"eq",
"iszero",
"not",
"lg",
"lt",
"slt",
Expand Down

0 comments on commit 4177745

Please sign in to comment.