| 1 |
from rpython.flowspace.model import (Variable, Constant, Block, Link, |
| 2 |
SpaceOperation, c_last_exception, checkgraph) |
| 3 |
|
| 4 |
|
| 5 |
def varoftype(concretetype, name=None): |
| 6 |
var = Variable(name) |
| 7 |
var.concretetype = concretetype |
| 8 |
return var |
| 9 |
|
| 10 |
def insert_empty_block(link, newops=[]): |
| 11 |
"""Insert and return a new block along the given link.""" |
| 12 |
vars = {} |
| 13 |
for v in link.args: |
| 14 |
if isinstance(v, Variable): |
| 15 |
vars[v] = True |
| 16 |
for op in newops: |
| 17 |
for v in op.args: |
| 18 |
if isinstance(v, Variable): |
| 19 |
vars.setdefault(v, True) |
| 20 |
vars[op.result] = False |
| 21 |
vars = [v for v, keep in vars.items() if keep] |
| 22 |
mapping = {} |
| 23 |
for v in vars: |
| 24 |
mapping[v] = v.copy() |
| 25 |
newblock = Block(vars) |
| 26 |
newblock.operations.extend(newops) |
| 27 |
newblock.closeblock(Link(link.args, link.target)) |
| 28 |
newblock.renamevariables(mapping) |
| 29 |
link.args[:] = vars |
| 30 |
link.target = newblock |
| 31 |
return newblock |
| 32 |
|
| 33 |
def insert_empty_startblock(graph): |
| 34 |
vars = [v.copy() for v in graph.startblock.inputargs] |
| 35 |
newblock = Block(vars) |
| 36 |
newblock.closeblock(Link(vars, graph.startblock)) |
| 37 |
graph.startblock = newblock |
| 38 |
|
| 39 |
def starts_with_empty_block(graph): |
| 40 |
return (not graph.startblock.operations |
| 41 |
and graph.startblock.exitswitch is None |
| 42 |
and graph.startblock.exits[0].args == graph.getargs()) |
| 43 |
|
| 44 |
def split_block(block, index, _forcelink=None): |
| 45 |
"""return a link where prevblock is the block leading up but excluding the |
| 46 |
index'th operation and target is a new block with the neccessary variables |
| 47 |
passed on. |
| 48 |
""" |
| 49 |
assert 0 <= index <= len(block.operations) |
| 50 |
if block.exitswitch == c_last_exception: |
| 51 |
assert index < len(block.operations) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
varmap = {} |
| 56 |
vars_produced_in_new_block = set() |
| 57 |
def get_new_name(var): |
| 58 |
if var is None: |
| 59 |
return None |
| 60 |
if isinstance(var, Constant): |
| 61 |
return var |
| 62 |
if var in vars_produced_in_new_block: |
| 63 |
return var |
| 64 |
if var not in varmap: |
| 65 |
varmap[var] = var.copy() |
| 66 |
return varmap[var] |
| 67 |
moved_operations = block.operations[index:] |
| 68 |
new_moved_ops = [] |
| 69 |
for op in moved_operations: |
| 70 |
repl = dict((arg, get_new_name(arg)) for arg in op.args) |
| 71 |
newop = op.replace(repl) |
| 72 |
new_moved_ops.append(newop) |
| 73 |
vars_produced_in_new_block.add(op.result) |
| 74 |
moved_operations = new_moved_ops |
| 75 |
links = block.exits |
| 76 |
block.exits = None |
| 77 |
for link in links: |
| 78 |
for i, arg in enumerate(link.args): |
| 79 |
|
| 80 |
|
| 81 |
if link.args[i] not in [link.last_exception, link.last_exc_value]: |
| 82 |
link.args[i] = get_new_name(link.args[i]) |
| 83 |
exitswitch = get_new_name(block.exitswitch) |
| 84 |
|
| 85 |
|
| 86 |
if _forcelink is not None: |
| 87 |
assert index == 0 |
| 88 |
linkargs = list(_forcelink) |
| 89 |
for v in varmap: |
| 90 |
if v not in linkargs: |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
from rpython.rtyper.lltypesystem import lltype |
| 96 |
if v.concretetype is not lltype.Void: |
| 97 |
raise Exception( |
| 98 |
"The variable %r of type %r was not explicitly listed" |
| 99 |
" in _forcelink. This issue can be caused by a" |
| 100 |
" jitdriver.jit_merge_point() where some variable" |
| 101 |
" containing an int or str or instance is actually" |
| 102 |
" known to be constant, e.g. always 42." % ( |
| 103 |
v, v.concretetype)) |
| 104 |
c = Constant(None, lltype.Void) |
| 105 |
w = varmap[v] |
| 106 |
newop = SpaceOperation('same_as', [c], w) |
| 107 |
i = 0 |
| 108 |
while i < len(moved_operations): |
| 109 |
if w in moved_operations[i].args: |
| 110 |
break |
| 111 |
i += 1 |
| 112 |
moved_operations.insert(i, newop) |
| 113 |
else: |
| 114 |
linkargs = varmap.keys() |
| 115 |
newblock = Block([get_new_name(v) for v in linkargs]) |
| 116 |
newblock.operations = moved_operations |
| 117 |
newblock.recloseblock(*links) |
| 118 |
newblock.exitswitch = exitswitch |
| 119 |
link = Link(linkargs, newblock) |
| 120 |
block.operations = block.operations[:index] |
| 121 |
block.recloseblock(link) |
| 122 |
block.exitswitch = None |
| 123 |
return link |
| 124 |
|
| 125 |
def call_initial_function(translator, initial_func, annhelper=None): |
| 126 |
"""Before the program starts, call 'initial_func()'.""" |
| 127 |
from rpython.annotator import model as annmodel |
| 128 |
from rpython.rtyper.lltypesystem import lltype |
| 129 |
from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator |
| 130 |
|
| 131 |
own_annhelper = (annhelper is None) |
| 132 |
if own_annhelper: |
| 133 |
annhelper = MixLevelHelperAnnotator(translator.rtyper) |
| 134 |
c_initial_func = annhelper.constfunc(initial_func, [], annmodel.s_None) |
| 135 |
if own_annhelper: |
| 136 |
annhelper.finish() |
| 137 |
|
| 138 |
entry_point = translator.entry_point_graph |
| 139 |
args = [v.copy() for v in entry_point.getargs()] |
| 140 |
extrablock = Block(args) |
| 141 |
v_none = varoftype(lltype.Void) |
| 142 |
newop = SpaceOperation('direct_call', [c_initial_func], v_none) |
| 143 |
extrablock.operations = [newop] |
| 144 |
extrablock.closeblock(Link(args, entry_point.startblock)) |
| 145 |
entry_point.startblock = extrablock |
| 146 |
checkgraph(entry_point) |
| 147 |
|
| 148 |
def call_final_function(translator, final_func, annhelper=None): |
| 149 |
"""When the program finishes normally, call 'final_func()'.""" |
| 150 |
from rpython.annotator import model as annmodel |
| 151 |
from rpython.rtyper.lltypesystem import lltype |
| 152 |
from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator |
| 153 |
|
| 154 |
own_annhelper = (annhelper is None) |
| 155 |
if own_annhelper: |
| 156 |
annhelper = MixLevelHelperAnnotator(translator.rtyper) |
| 157 |
c_final_func = annhelper.constfunc(final_func, [], annmodel.s_None) |
| 158 |
if own_annhelper: |
| 159 |
annhelper.finish() |
| 160 |
|
| 161 |
entry_point = translator.entry_point_graph |
| 162 |
v = entry_point.getreturnvar().copy() |
| 163 |
extrablock = Block([v]) |
| 164 |
v_none = varoftype(lltype.Void) |
| 165 |
newop = SpaceOperation('direct_call', [c_final_func], v_none) |
| 166 |
extrablock.operations = [newop] |
| 167 |
extrablock.closeblock(Link([v], entry_point.returnblock)) |
| 168 |
for block in entry_point.iterblocks(): |
| 169 |
if block is not extrablock: |
| 170 |
for link in block.exits: |
| 171 |
if link.target is entry_point.returnblock: |
| 172 |
link.target = extrablock |
| 173 |
checkgraph(entry_point) |