def convert_to_ssa(func): # return labeled blocks blocks = block_map(form_blocks(func['instrs'])) # add entry block if required add_entry(blocks) # add terminators to block if required add_terminators(blocks) # get the successor of each block succ = {name: successors(block[-1]) for name, block in blocks.items()} # get the predecessor of each block pred = map_inv(succ) # get dominator of each block dom = get_dom(succ, list(blocks.keys())[0]) # get dominate frontier of each block df = dom_fronts(dom, succ) # varaible map to block label defs = def_blocks(blocks) # get function type types = get_types(func) # get argument name arg_names = {a['name'] for a in func['args']} if 'args' in func else set() # find phi nodes phis = get_phis(blocks, df, defs) # rename variables phi_args, phi_dests = ssa_rename(blocks, phis, succ, dom_tree(dom), arg_names) # inserte phi nodes insert_phis(blocks, phi_args, phi_dests, types) # rebuild the updated blocks func['instrs'] = reassemble(blocks)
def func_from_ssa(func): blocks = block_map(form_blocks(func['instrs'])) add_entry(blocks) add_terminators(blocks) # Replace each phi-node. for block in blocks.values(): # Insert copies for each phi. for instr in block: if instr.get('op') == 'phi': dest = instr['dest'] type = instr['type'] for i, label in enumerate(instr['labels']): var = instr['args'][i] # Insert a copy in the predecessor block, before the # terminator. pred = blocks[label] pred.insert(-1, { 'op': 'id', 'type': type, 'args': [var], 'dest': dest, }) # Remove all phis. new_block = [i for i in block if i.get('op') != 'phi'] block[:] = new_block func['instrs'] = reassemble(blocks)
def to_ssa(json_input): inp = json.loads(json_input) for func in inp['functions']: blocks = block_map(form_blocks(func['instrs'])) add_entry(blocks) add_terminators(blocks) blocks = insert_phi(blocks) blocks = rename_vars(blocks) func['instrs'] = reassemble(blocks) return json.dumps(inp)
def func_to_ssa(func): blocks = block_map(form_blocks(func['instrs'])) add_entry(blocks) add_terminators(blocks) succ = {name: successors(block[-1]) for name, block in blocks.items()} dom = get_dom(succ, list(blocks.keys())[0]) df = dom_fronts(dom, succ) defs = def_blocks(blocks) types = get_types(func) arg_names = {a['name'] for a in func['args']} if 'args' in func else set() phis = get_phis(blocks, df, defs) phi_args, phi_dests = ssa_rename(blocks, phis, succ, dom_tree(dom), arg_names) insert_phis(blocks, phi_args, phi_dests, types) func['instrs'] = reassemble(blocks)