| 1 |
"""Flow graph building for generators""" |
| 2 |
|
| 3 |
from rpython.flowspace.argument import Signature |
| 4 |
from rpython.flowspace.bytecode import HostCode |
| 5 |
from rpython.flowspace.pygraph import PyGraph |
| 6 |
from rpython.flowspace.model import (Block, Link, Variable, |
| 7 |
Constant, checkgraph, const) |
| 8 |
from rpython.flowspace.operation import op |
| 9 |
from rpython.translator.unsimplify import insert_empty_startblock, split_block |
| 10 |
from rpython.translator.simplify import eliminate_empty_blocks, simplify_graph |
| 11 |
from rpython.tool.sourcetools import func_with_new_name |
| 12 |
|
| 13 |
|
| 14 |
class AbstractPosition(object): |
| 15 |
_immutable_ = True |
| 16 |
_attrs_ = () |
| 17 |
|
| 18 |
def make_generator_entry_graph(func): |
| 19 |
|
| 20 |
|
| 21 |
code = HostCode._from_code(func.__code__) |
| 22 |
graph = PyGraph(func, code) |
| 23 |
block = graph.startblock |
| 24 |
for name, w_value in zip(code.co_varnames, block.framestate.mergeable): |
| 25 |
if isinstance(w_value, Variable): |
| 26 |
w_value.rename(name) |
| 27 |
varnames = get_variable_names(graph.startblock.inputargs) |
| 28 |
GeneratorIterator = make_generatoriterator_class(varnames) |
| 29 |
replace_graph_with_bootstrap(GeneratorIterator, graph) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
attach_next_method(GeneratorIterator, graph) |
| 34 |
return graph |
| 35 |
|
| 36 |
def tweak_generator_graph(graph): |
| 37 |
|
| 38 |
GeneratorIterator = graph.func._generator_next_method_of_ |
| 39 |
tweak_generator_body_graph(GeneratorIterator.Entry, graph) |
| 40 |
|
| 41 |
|
| 42 |
def make_generatoriterator_class(var_names): |
| 43 |
class GeneratorIterator(object): |
| 44 |
class Entry(AbstractPosition): |
| 45 |
_immutable_ = True |
| 46 |
varnames = var_names |
| 47 |
|
| 48 |
def __init__(self, entry): |
| 49 |
self.current = entry |
| 50 |
|
| 51 |
def __iter__(self): |
| 52 |
return self |
| 53 |
|
| 54 |
return GeneratorIterator |
| 55 |
|
| 56 |
def replace_graph_with_bootstrap(GeneratorIterator, graph): |
| 57 |
Entry = GeneratorIterator.Entry |
| 58 |
newblock = Block(graph.startblock.inputargs) |
| 59 |
op_entry = op.simple_call(const(Entry)) |
| 60 |
v_entry = op_entry.result |
| 61 |
newblock.operations.append(op_entry) |
| 62 |
assert len(graph.startblock.inputargs) == len(Entry.varnames) |
| 63 |
for v, name in zip(graph.startblock.inputargs, Entry.varnames): |
| 64 |
newblock.operations.append(op.setattr(v_entry, Constant(name), v)) |
| 65 |
op_generator = op.simple_call(const(GeneratorIterator), v_entry) |
| 66 |
newblock.operations.append(op_generator) |
| 67 |
newblock.closeblock(Link([op_generator.result], graph.returnblock)) |
| 68 |
graph.startblock = newblock |
| 69 |
|
| 70 |
def attach_next_method(GeneratorIterator, graph): |
| 71 |
func = graph.func |
| 72 |
func = func_with_new_name(func, '%s__next' % (func.__name__,)) |
| 73 |
func._generator_next_method_of_ = GeneratorIterator |
| 74 |
func._always_inline_ = True |
| 75 |
|
| 76 |
def next(self): |
| 77 |
entry = self.current |
| 78 |
self.current = None |
| 79 |
assert entry is not None |
| 80 |
(next_entry, return_value) = func(entry) |
| 81 |
self.current = next_entry |
| 82 |
return return_value |
| 83 |
GeneratorIterator.next = next |
| 84 |
graph._tweaked_func = func |
| 85 |
|
| 86 |
def get_variable_names(variables): |
| 87 |
seen = set() |
| 88 |
result = [] |
| 89 |
for v in variables: |
| 90 |
name = v._name.strip('_') |
| 91 |
while name in seen: |
| 92 |
name += '_' |
| 93 |
result.append('g_' + name) |
| 94 |
seen.add(name) |
| 95 |
return result |
| 96 |
|
| 97 |
def _insert_reads(block, varnames): |
| 98 |
assert len(varnames) == len(block.inputargs) |
| 99 |
v_entry1 = Variable('entry') |
| 100 |
for i, name in enumerate(varnames): |
| 101 |
hlop = op.getattr(v_entry1, const(name)) |
| 102 |
hlop.result = block.inputargs[i] |
| 103 |
block.operations.insert(i, hlop) |
| 104 |
block.inputargs = [v_entry1] |
| 105 |
|
| 106 |
def tweak_generator_body_graph(Entry, graph): |
| 107 |
|
| 108 |
|
| 109 |
simplify_graph(graph) |
| 110 |
insert_empty_startblock(graph) |
| 111 |
_insert_reads(graph.startblock, Entry.varnames) |
| 112 |
Entry.block = graph.startblock |
| 113 |
|
| 114 |
mappings = [Entry] |
| 115 |
|
| 116 |
stopblock = Block([]) |
| 117 |
op0 = op.simple_call(const(StopIteration)) |
| 118 |
op1 = op.type(op0.result) |
| 119 |
stopblock.operations = [op0, op1] |
| 120 |
stopblock.closeblock(Link([op1.result, op0.result], graph.exceptblock)) |
| 121 |
|
| 122 |
for block in list(graph.iterblocks()): |
| 123 |
for exit in block.exits: |
| 124 |
if exit.target is graph.returnblock: |
| 125 |
exit.args = [] |
| 126 |
exit.target = stopblock |
| 127 |
assert block is not stopblock |
| 128 |
for index in range(len(block.operations)-1, -1, -1): |
| 129 |
hlop = block.operations[index] |
| 130 |
if hlop.opname == 'yield_': |
| 131 |
[v_yielded_value] = hlop.args |
| 132 |
del block.operations[index] |
| 133 |
newlink = split_block(block, index) |
| 134 |
newblock = newlink.target |
| 135 |
varnames = get_variable_names(newlink.args) |
| 136 |
|
| 137 |
class Resume(AbstractPosition): |
| 138 |
_immutable_ = True |
| 139 |
_attrs_ = varnames |
| 140 |
block = newblock |
| 141 |
Resume.__name__ = 'Resume%d' % len(mappings) |
| 142 |
mappings.append(Resume) |
| 143 |
|
| 144 |
_insert_reads(newblock, varnames) |
| 145 |
|
| 146 |
op_resume = op.simple_call(const(Resume)) |
| 147 |
block.operations.append(op_resume) |
| 148 |
v_resume = op_resume.result |
| 149 |
for i, name in enumerate(varnames): |
| 150 |
block.operations.append( |
| 151 |
op.setattr(v_resume, const(name), newlink.args[i])) |
| 152 |
op_pair = op.newtuple(v_resume, v_yielded_value) |
| 153 |
block.operations.append(op_pair) |
| 154 |
newlink.args = [op_pair.result] |
| 155 |
newlink.target = graph.returnblock |
| 156 |
|
| 157 |
regular_entry_block = Block([Variable('entry')]) |
| 158 |
block = regular_entry_block |
| 159 |
for Resume in mappings: |
| 160 |
op_check = op.isinstance(block.inputargs[0], const(Resume)) |
| 161 |
block.operations.append(op_check) |
| 162 |
block.exitswitch = op_check.result |
| 163 |
link1 = Link([block.inputargs[0]], Resume.block) |
| 164 |
link1.exitcase = True |
| 165 |
nextblock = Block([Variable('entry')]) |
| 166 |
link2 = Link([block.inputargs[0]], nextblock) |
| 167 |
link2.exitcase = False |
| 168 |
block.closeblock(link1, link2) |
| 169 |
block = nextblock |
| 170 |
block.closeblock(Link([Constant(AssertionError), |
| 171 |
Constant(AssertionError("bad generator class"))], |
| 172 |
graph.exceptblock)) |
| 173 |
graph.startblock = regular_entry_block |
| 174 |
graph.signature = Signature(['entry']) |
| 175 |
graph.defaults = () |
| 176 |
checkgraph(graph) |
| 177 |
eliminate_empty_blocks(graph) |