Skip to content

Commit

Permalink
add some comments, minor nits
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Dec 1, 2023
1 parent febd073 commit 1b75ce0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
26 changes: 12 additions & 14 deletions vyper/venom/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ def calculate_cfg(ctx: IRFunction) -> None:
bb.out_vars = OrderedSet()

# TODO: This is a hack to support the old IR format
# where deploy is an instruction. Going directly to the new IR we should have just
# one entry for the contructor and one for the runtime core. These will be propertly
# linked in the CFG, and the deploy instruction will be removed completely.
# where deploy is an instruction. Going directly to the new IR we should
# have two entry points, one for the initcode and one for the runtime code.
deploy_bb = None
after_deploy_bb = None
for i, bb in enumerate(ctx.basic_blocks):
Expand All @@ -31,33 +30,32 @@ def calculate_cfg(ctx: IRFunction) -> None:
after_deploy_bb = ctx.basic_blocks[i + 1]
break

if deploy_bb:
if deploy_bb is not None:
assert after_deploy_bb is not None, "No block after deploy block"
entry_block = after_deploy_bb
has_constructor = True if ctx.basic_blocks[0].instructions[0].opcode != "deploy" else False
has_constructor = ctx.basic_blocks[0].instructions[0].opcode != "deploy"
if has_constructor:
deploy_bb.add_cfg_in(ctx.basic_blocks[0])
entry_block.add_cfg_in(deploy_bb)
else:
entry_block = ctx.basic_blocks[0]

# TODO: Special case for the jump table of selector buckets and fallback.
# It will be generalized when the dispacher code is directly generated in Venom.
# The when directly generating the dispatcher code from the AST we should be emitting
# a dynamic jmp instruction with all the posible targets (this in EOF this will be also
# a jump via jump table). This will remove the need for this special case, and will result
# in cleaner code for normalization just according to CFG, without the need to examine
# the block termination instructions.
# It will be generalized when the dispacher code is directly generated in
# Venom. The when directly generating the dispatcher code from the AST we
# should be emitting a dynamic jmp instruction with all the posible targets
# (this in EOF this will be also a jump via jump table). This will remove
# the need for this special case, and will result in cleaner code for
# normalization just according to CFG, without the need to examine the
# block termination instructions.
for bb in ctx.basic_blocks:
if "selector_bucket_" in bb.label.value or bb.label.value == "fallback":
bb.add_cfg_in(entry_block)

for bb in ctx.basic_blocks:
assert len(bb.instructions) > 0, "Basic block should not be empty"
last_inst = bb.instructions[-1]
assert last_inst.opcode in BB_TERMINATORS, "Last instruction should be a terminator" + str(
bb
)
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:
Expand Down
5 changes: 5 additions & 0 deletions vyper/venom/passes/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def _split_for_dynamic_branch(self, bb: IRBasicBlock, in_bb: IRBasicBlock) -> No
split_bb = self._insert_split_basicblock(bb, in_bb)

# Update any affected labels in the data segment
# TODO: this DESTROYS the cfg! refactor so the translation of the
# selector table produces indirect jumps properly.
for inst in self.ctx.data_segment:
if inst.opcode == "db" and inst.operands[0] == bb.label:
inst.operands[0] = split_bb.label
Expand All @@ -63,6 +65,9 @@ def _insert_split_basicblock(self, bb: IRBasicBlock, in_bb: IRBasicBlock) -> IRB
self.ctx.append_basic_block(split_bb)

# Rewire the CFG
# TODO: this is cursed code, it is necessary instead of just running
# calculate_cfg() because split_for_dynamic_branch destroys the CFG!
# ideally, remove this rewiring and just re-run calculate_cfg().
split_bb.add_cfg_in(in_bb)
split_bb.add_cfg_out(bb)
in_bb.remove_cfg_out(bb)
Expand Down

0 comments on commit 1b75ce0

Please sign in to comment.