| 1 |
from rpython.memory.gctransform.transform import BaseGCTransformer |
| 2 |
from rpython.flowspace.model import Variable |
| 3 |
from rpython.translator.backendopt.support import var_needsgc |
| 4 |
from rpython.translator.translator import TranslationContext, graphof |
| 5 |
from rpython.translator.exceptiontransform import ExceptionTransformer |
| 6 |
from rpython.rtyper.lltypesystem import lltype |
| 7 |
from rpython.conftest import option |
| 8 |
from rpython.rtyper.rtyper import llinterp_backend |
| 9 |
|
| 10 |
|
| 11 |
class LLInterpedTranformerTests: |
| 12 |
|
| 13 |
def llinterpreter_for_transformed_graph(self, f, args_s): |
| 14 |
from rpython.rtyper.llinterp import LLInterpreter |
| 15 |
from rpython.translator.c.genc import CStandaloneBuilder |
| 16 |
|
| 17 |
t = rtype(f, args_s) |
| 18 |
|
| 19 |
cbuild = CStandaloneBuilder(t, f, t.config, gcpolicy=self.gcpolicy) |
| 20 |
cbuild.make_entrypoint_wrapper = False |
| 21 |
cbuild.build_database() |
| 22 |
graph = cbuild.getentrypointptr()._obj.graph |
| 23 |
|
| 24 |
|
| 25 |
for v in graph.getargs(): |
| 26 |
if isinstance(v.concretetype, lltype.Ptr): |
| 27 |
assert v.concretetype.TO._gckind != 'gc', "fix the test!" |
| 28 |
llinterp = LLInterpreter(t.rtyper) |
| 29 |
if option.view: |
| 30 |
t.view() |
| 31 |
return llinterp, graph |
| 32 |
|
| 33 |
def test_simple(self): |
| 34 |
from rpython.annotator.model import SomeInteger |
| 35 |
|
| 36 |
class C: |
| 37 |
pass |
| 38 |
c = C() |
| 39 |
c.x = 1 |
| 40 |
def g(x): |
| 41 |
if x: |
| 42 |
return c |
| 43 |
else: |
| 44 |
d = C() |
| 45 |
d.x = 2 |
| 46 |
return d |
| 47 |
def f(x): |
| 48 |
return g(x).x |
| 49 |
|
| 50 |
llinterp, graph = self.llinterpreter_for_transformed_graph(f, [SomeInteger()]) |
| 51 |
|
| 52 |
res = llinterp.eval_graph(graph, [0]) |
| 53 |
assert res == f(0) |
| 54 |
res = llinterp.eval_graph(graph, [1]) |
| 55 |
assert res == f(1) |
| 56 |
|
| 57 |
def test_simple_varsize(self): |
| 58 |
from rpython.annotator.model import SomeInteger |
| 59 |
|
| 60 |
def f(x): |
| 61 |
r = [] |
| 62 |
for i in range(x): |
| 63 |
if i % 2: |
| 64 |
r.append(x) |
| 65 |
return len(r) |
| 66 |
|
| 67 |
llinterp, graph = self.llinterpreter_for_transformed_graph(f, [SomeInteger()]) |
| 68 |
|
| 69 |
res = llinterp.eval_graph(graph, [0]) |
| 70 |
assert res == f(0) |
| 71 |
res = llinterp.eval_graph(graph, [10]) |
| 72 |
assert res == f(10) |
| 73 |
|
| 74 |
def test_str(self): |
| 75 |
from rpython.annotator.model import SomeBool |
| 76 |
|
| 77 |
def f(flag): |
| 78 |
if flag: |
| 79 |
x = 'a' |
| 80 |
else: |
| 81 |
x = 'brrrrrrr' |
| 82 |
return len(x + 'a') |
| 83 |
|
| 84 |
llinterp, graph = self.llinterpreter_for_transformed_graph(f, [SomeBool()]) |
| 85 |
|
| 86 |
res = llinterp.eval_graph(graph, [True]) |
| 87 |
assert res == f(True) |
| 88 |
res = llinterp.eval_graph(graph, [False]) |
| 89 |
assert res == f(False) |
| 90 |
|
| 91 |
|
| 92 |
class _TestGCTransformer(BaseGCTransformer): |
| 93 |
|
| 94 |
def push_alive(self, var, llops): |
| 95 |
llops.genop("gc_push_alive", [var]) |
| 96 |
|
| 97 |
def pop_alive(self, var, llops): |
| 98 |
llops.genop("gc_pop_alive", [var]) |
| 99 |
|
| 100 |
|
| 101 |
def checkblock(block, is_borrowed, is_start_block): |
| 102 |
if block.operations == (): |
| 103 |
|
| 104 |
|
| 105 |
return |
| 106 |
if is_start_block: |
| 107 |
refs_in = 0 |
| 108 |
else: |
| 109 |
refs_in = len([v for v in block.inputargs if isinstance(v, Variable) |
| 110 |
and var_needsgc(v) |
| 111 |
and not is_borrowed(v)]) |
| 112 |
push_alives = len([op for op in block.operations |
| 113 |
if op.opname == 'gc_push_alive']) |
| 114 |
|
| 115 |
gc_returning_calls = len([op for op in block.operations |
| 116 |
if op.opname in ('direct_call', 'indirect_call') |
| 117 |
and var_needsgc(op.result)]) |
| 118 |
|
| 119 |
pop_alives = len([op for op in block.operations |
| 120 |
if op.opname == 'gc_pop_alive']) |
| 121 |
if pop_alives == len(block.operations): |
| 122 |
|
| 123 |
return |
| 124 |
assert not block.canraise |
| 125 |
for link in block.exits: |
| 126 |
refs_out = 0 |
| 127 |
for v2 in link.target.inputargs: |
| 128 |
if var_needsgc(v2) and not is_borrowed(v2): |
| 129 |
refs_out += 1 |
| 130 |
pushes = push_alives + gc_returning_calls |
| 131 |
assert refs_in + pushes == pop_alives + refs_out |
| 132 |
|
| 133 |
def rtype(func, inputtypes, specialize=True): |
| 134 |
t = TranslationContext() |
| 135 |
t.buildannotator().build_types(func, inputtypes) |
| 136 |
rtyper = t.buildrtyper() |
| 137 |
rtyper.backend = llinterp_backend |
| 138 |
if specialize: |
| 139 |
rtyper.specialize() |
| 140 |
if option.view: |
| 141 |
t.view() |
| 142 |
return t |
| 143 |
|
| 144 |
def rtype_and_transform(func, inputtypes, transformcls, specialize=True, check=True): |
| 145 |
t = rtype(func, inputtypes, specialize) |
| 146 |
transformer = transformcls(t) |
| 147 |
etrafo = ExceptionTransformer(t) |
| 148 |
etrafo.transform_completely() |
| 149 |
graphs_borrowed = {} |
| 150 |
for graph in t.graphs[:]: |
| 151 |
graphs_borrowed[graph] = transformer.transform_graph(graph) |
| 152 |
if option.view: |
| 153 |
t.view() |
| 154 |
t.checkgraphs() |
| 155 |
if check: |
| 156 |
for graph, is_borrowed in graphs_borrowed.iteritems(): |
| 157 |
for block in graph.iterblocks(): |
| 158 |
checkblock(block, is_borrowed, block is graph.startblock) |
| 159 |
return t, transformer |
| 160 |
|
| 161 |
def getops(graph): |
| 162 |
ops = {} |
| 163 |
for block in graph.iterblocks(): |
| 164 |
for op in block.operations: |
| 165 |
ops.setdefault(op.opname, []).append(op) |
| 166 |
return ops |
| 167 |
|
| 168 |
def test_simple(): |
| 169 |
def f(): |
| 170 |
return 1 |
| 171 |
rtype_and_transform(f, [], _TestGCTransformer) |
| 172 |
|
| 173 |
def test_fairly_simple(): |
| 174 |
class C: |
| 175 |
pass |
| 176 |
def f(): |
| 177 |
c = C() |
| 178 |
c.x = 1 |
| 179 |
return c.x |
| 180 |
t, transformer = rtype_and_transform(f, [], _TestGCTransformer) |
| 181 |
|
| 182 |
def test_return_gcpointer(): |
| 183 |
class C: |
| 184 |
pass |
| 185 |
def f(): |
| 186 |
c = C() |
| 187 |
c.x = 1 |
| 188 |
return c |
| 189 |
t, transformer = rtype_and_transform(f, [], _TestGCTransformer) |
| 190 |
|
| 191 |
def test_call_function(): |
| 192 |
class C: |
| 193 |
pass |
| 194 |
def f(): |
| 195 |
c = C() |
| 196 |
c.x = 1 |
| 197 |
return c |
| 198 |
def g(): |
| 199 |
return f().x |
| 200 |
t, transformer = rtype_and_transform(g, [], _TestGCTransformer) |
| 201 |
ggraph = graphof(t, g) |
| 202 |
for i, op in enumerate(ggraph.startblock.operations): |
| 203 |
if op.opname == "direct_call": |
| 204 |
break |
| 205 |
else: |
| 206 |
assert False, "direct_call not found!" |
| 207 |
assert ggraph.startblock.operations[i + 1].opname != 'gc_push_alive' |
| 208 |
|
| 209 |
|
| 210 |
def test_multiply_passed_var(): |
| 211 |
S = lltype.GcStruct("S", ('x', lltype.Signed)) |
| 212 |
def f(x): |
| 213 |
if x: |
| 214 |
a = lltype.malloc(S) |
| 215 |
a.x = 1 |
| 216 |
b = a |
| 217 |
else: |
| 218 |
a = lltype.malloc(S) |
| 219 |
a.x = 1 |
| 220 |
b = lltype.malloc(S) |
| 221 |
b.x = 2 |
| 222 |
return a.x + b.x |
| 223 |
t, transformer = rtype_and_transform(f, [int], _TestGCTransformer) |
| 224 |
|
| 225 |
def test_pass_gc_pointer(): |
| 226 |
S = lltype.GcStruct("S", ('x', lltype.Signed)) |
| 227 |
def f(s): |
| 228 |
s.x = 1 |
| 229 |
def g(): |
| 230 |
s = lltype.malloc(S) |
| 231 |
f(s) |
| 232 |
return s.x |
| 233 |
t, transformer = rtype_and_transform(g, [], _TestGCTransformer) |
| 234 |
|
| 235 |
def test_except_block(): |
| 236 |
S = lltype.GcStruct("S", ('x', lltype.Signed)) |
| 237 |
def f(a, n): |
| 238 |
if n == 0: |
| 239 |
raise ValueError |
| 240 |
a.x = 1 |
| 241 |
return a |
| 242 |
def g(n): |
| 243 |
a = lltype.malloc(S) |
| 244 |
try: |
| 245 |
return f(a, n).x |
| 246 |
except ValueError: |
| 247 |
return 0 |
| 248 |
t, transformer = rtype_and_transform(g, [int], _TestGCTransformer) |
| 249 |
|
| 250 |
def test_except_block2(): |
| 251 |
|
| 252 |
S = lltype.GcStruct("S", ('x', lltype.Signed)) |
| 253 |
def f(a, n): |
| 254 |
if n == 0: |
| 255 |
raise ValueError |
| 256 |
a.x = 1 |
| 257 |
def g(n): |
| 258 |
a = lltype.malloc(S) |
| 259 |
try: |
| 260 |
f(a, n) |
| 261 |
return a.x |
| 262 |
except ValueError: |
| 263 |
return 0 |
| 264 |
t, transformer = rtype_and_transform(g, [int], _TestGCTransformer) |
| 265 |
|
| 266 |
def test_no_livevars_with_exception(): |
| 267 |
def g(): |
| 268 |
raise TypeError |
| 269 |
def f(): |
| 270 |
try: |
| 271 |
g() |
| 272 |
except TypeError: |
| 273 |
return 0 |
| 274 |
return 1 |
| 275 |
t, transformer = rtype_and_transform(f, [], _TestGCTransformer) |
| 276 |
|
| 277 |
def test_bare_setfield(): |
| 278 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 279 |
class A: |
| 280 |
def __init__(self, obj): |
| 281 |
self.x = obj |
| 282 |
class B: |
| 283 |
def __init__(self, i): |
| 284 |
self.i = i |
| 285 |
def f(i): |
| 286 |
v = B(i) |
| 287 |
inst = A(v) |
| 288 |
llop.setfield(lltype.Void, inst, 'x', v) |
| 289 |
llop.bare_setfield(lltype.Void, inst, 'x', v) |
| 290 |
|
| 291 |
t, transformer = rtype_and_transform(f, [int], _TestGCTransformer, |
| 292 |
check=False) |
| 293 |
ops = getops(graphof(t, f)) |
| 294 |
|