1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
import py
from rpython.rlib.nonconst import NonConstant
from rpython.translator.translator import TranslationContext, graphof
from rpython.translator.backendopt.storesink import storesink_graph
from rpython.translator.backendopt import removenoops
from rpython.flowspace.model import checkgraph
from rpython.conftest import option
class TestStoreSink(object):
def translate(self, func, argtypes):
t = TranslationContext()
t.buildannotator().build_types(func, argtypes)
t.buildrtyper().specialize()
return t
def check(self, f, argtypes, no_getfields=0):
t = self.translate(f, argtypes)
getfields = 0
graph = graphof(t, f)
removenoops.remove_same_as(graph)
checkgraph(graph)
storesink_graph(graph)
checkgraph(graph)
if option.view:
t.view()
for block in graph.iterblocks():
for op in block.operations:
if op.opname == 'getfield':
getfields += 1
if no_getfields != getfields:
py.test.fail("Expected %d, got %d getfields" %
(no_getfields, getfields))
def test_infrastructure(self):
class A(object):
pass
def f(i):
a = A()
a.x = i
return a.x
self.check(f, [int], 0)
def test_simple(self):
class A(object):
pass
def f(i):
a = A()
a.x = i
return a.x + a.x
self.check(f, [int], 0)
def test_irrelevant_setfield(self):
class A(object):
pass
def f(i):
a = A()
a.x = i
one = a.x
a.y = 3
two = a.x
return one + two
self.check(f, [int], 0)
def test_relevant_setfield(self):
class A(object):
pass
def f(i):
a = A()
b = A()
a.x = i
b.x = i + 1
one = a.x
b.x = i
two = a.x
return one + two
self.check(f, [int], 2)
def test_different_concretetype(self):
class A(object):
pass
class B(object):
pass
def f(i):
a = A()
b = B()
a.x = i
one = a.x
b.x = i + 1
two = a.x
return one + two
self.check(f, [int], 0)
def test_subclass(self):
class A(object):
pass
class B(A):
pass
def f(i):
a = A()
b = B()
a.x = i
one = a.x
b.x = i + 1
two = a.x
return one + two
self.check(f, [int], 1)
def test_bug_1(self):
class A(object):
pass
def f(i):
a = A()
a.cond = i > 0
n = a.cond
if a.cond:
return True
return n
self.check(f, [int], 0)
def test_cfg_splits(self):
class A(object):
pass
def f(i):
a = A()
j = i
for i in range(i):
a.x = i
if i:
j = a.x + a.x
else:
j = a.x * 5
return j
self.check(f, [int], 0)
def test_malloc_does_not_invalidate(self):
class A(object):
pass
class B(object):
pass
def f(i):
a = A()
a.x = i
b = B()
return a.x
self.check(f, [int], 0)
def test_debug_assert_not_none(self):
from rpython.rlib.debug import ll_assert_not_none
class A(object):
pass
def g(i):
if i:
return None
else:
return A()
def f(i):
a1 = g(i)
a = A()
a.x = i
ll_assert_not_none(a1)
return a.x
self.check(f, [int], 0)
def test_read_none_field_bug(self):
from rpython.translator.backendopt import inline, constfold
class A(object):
_immutable_fields_ = ['next']
def g(i):
if i == 1:
return None
a = A()
a.next = 12 * i
return a
def f(i):
g(i)
a = g(1)
return a.next
t = self.translate(f, [int])
graph = graphof(t, f)
inline.auto_inline_graphs(t, [graph, graphof(t, g)], 100)
constfold.constant_fold_graph(graph)
removenoops.remove_same_as(graph)
checkgraph(graph)
storesink_graph(graph)
checkgraph(graph)
|