Skip to content

Commit

Permalink
Compute types of phi-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sampsyo committed Sep 30, 2020
1 parent c6e6edd commit cba98ce
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions examples/to_ssa.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ def _rename(block):
return phi_args, phi_dests


def insert_phis(blocks, phi_args, phi_dests):
def insert_phis(blocks, phi_args, phi_dests, types):
for block, instrs in blocks.items():
for dest, pairs in phi_args[block].items():
phi = {
'op': 'phi',
'dest': phi_dests[block][dest],
'type': 'XXX',
'type': types[dest],
'labels': [p[0] for p in pairs],
'args': [p[1] for p in pairs],
}
Expand All @@ -109,6 +109,18 @@ def reassemble(blocks):
return instrs


def get_types(blocks):
# Silly way to get the type of variables. (According to the Bril
# spec, well-formed programs must use only a single type for every
# variable within a given function.)
types = {}
for block in blocks.values():
for instr in block:
if 'dest' in instr:
types[instr['dest']] = instr['type']
return types


def func_to_ssa(func):
blocks = block_map(form_blocks(func['instrs']))
add_terminators(blocks)
Expand All @@ -117,11 +129,12 @@ def func_to_ssa(func):

df = dom_fronts(dom, succ)
defs = def_blocks(blocks)
types = get_types(blocks)

phis = get_phis(blocks, df, defs)
phi_args, phi_dests = ssa_rename(blocks, phis, succ,
dom_tree(dom), set(defs.keys()))
insert_phis(blocks, phi_args, phi_dests)
insert_phis(blocks, phi_args, phi_dests, types)

func['instrs'] = reassemble(blocks)

Expand Down

0 comments on commit cba98ce

Please sign in to comment.