| 1 |
import py |
| 2 |
import sys |
| 3 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 4 |
|
| 5 |
from rpython.jit.tool.oparser import parse, OpParser |
| 6 |
from rpython.jit.metainterp.resoperation import rop |
| 7 |
from rpython.jit.metainterp.history import AbstractDescr, JitCellToken,\ |
| 8 |
TargetToken |
| 9 |
|
| 10 |
class BaseTestOparser(object): |
| 11 |
|
| 12 |
OpParser = None |
| 13 |
|
| 14 |
def parse(self, *args, **kwds): |
| 15 |
kwds['OpParser'] = self.OpParser |
| 16 |
return parse(*args, **kwds) |
| 17 |
|
| 18 |
def test_basic_parse(self): |
| 19 |
x = """ |
| 20 |
[i0, i1] |
| 21 |
# a comment |
| 22 |
i2 = int_add(i0, i1) |
| 23 |
i3 = int_sub(i2, 3) # another comment |
| 24 |
finish() # (tricky) |
| 25 |
""" |
| 26 |
loop = self.parse(x) |
| 27 |
assert len(loop.operations) == 3 |
| 28 |
assert [op.getopnum() for op in loop.operations] == [rop.INT_ADD, rop.INT_SUB, |
| 29 |
rop.FINISH] |
| 30 |
assert len(loop.inputargs) == 2 |
| 31 |
assert loop.operations[-1].getdescr() |
| 32 |
|
| 33 |
def test_const_ptr_subops(self): |
| 34 |
x = """ |
| 35 |
[p0] |
| 36 |
guard_class(p0, ConstClass(vtable)) [] |
| 37 |
""" |
| 38 |
S = lltype.Struct('S') |
| 39 |
vtable = lltype.nullptr(S) |
| 40 |
loop = self.parse(x, None, locals()) |
| 41 |
assert len(loop.operations) == 1 |
| 42 |
assert loop.operations[0].getdescr() |
| 43 |
assert loop.operations[0].getfailargs() == [] |
| 44 |
|
| 45 |
def test_descr(self): |
| 46 |
class Xyz(AbstractDescr): |
| 47 |
I_am_a_descr = True |
| 48 |
|
| 49 |
x = """ |
| 50 |
[p0] |
| 51 |
i1 = getfield_gc_i(p0, descr=stuff) |
| 52 |
""" |
| 53 |
stuff = Xyz() |
| 54 |
loop = self.parse(x, None, locals()) |
| 55 |
assert loop.operations[0].getdescr() is stuff |
| 56 |
|
| 57 |
def test_after_fail(self): |
| 58 |
x = """ |
| 59 |
[i0] |
| 60 |
guard_value(i0, 3) [] |
| 61 |
i1 = int_add(1, 2) |
| 62 |
""" |
| 63 |
loop = self.parse(x, None, {}) |
| 64 |
assert len(loop.operations) == 2 |
| 65 |
|
| 66 |
def test_descr_setfield(self): |
| 67 |
class Xyz(AbstractDescr): |
| 68 |
I_am_a_descr = True |
| 69 |
|
| 70 |
x = """ |
| 71 |
[p0] |
| 72 |
setfield_gc(p0, 3, descr=stuff) |
| 73 |
""" |
| 74 |
stuff = Xyz() |
| 75 |
loop = self.parse(x, None, locals()) |
| 76 |
assert loop.operations[0].getdescr() is stuff |
| 77 |
|
| 78 |
def test_getvar_const_ptr(self): |
| 79 |
x = ''' |
| 80 |
[] |
| 81 |
call_n(ConstPtr(func_ptr)) |
| 82 |
''' |
| 83 |
TP = lltype.GcArray(lltype.Signed) |
| 84 |
NULL = lltype.cast_opaque_ptr(llmemory.GCREF, lltype.nullptr(TP)) |
| 85 |
loop = self.parse(x, None, {'func_ptr' : NULL}) |
| 86 |
assert loop.operations[0].getarg(0).value == NULL |
| 87 |
|
| 88 |
def test_jump_target(self): |
| 89 |
x = ''' |
| 90 |
[] |
| 91 |
jump() |
| 92 |
''' |
| 93 |
loop = self.parse(x) |
| 94 |
assert loop.operations[0].getdescr() is loop.original_jitcell_token |
| 95 |
|
| 96 |
def test_jump_target_other(self): |
| 97 |
looptoken = JitCellToken() |
| 98 |
looptoken.I_am_a_descr = True |
| 99 |
x = ''' |
| 100 |
[] |
| 101 |
jump(descr=looptoken) |
| 102 |
''' |
| 103 |
loop = self.parse(x, namespace=locals()) |
| 104 |
assert loop.operations[0].getdescr() is looptoken |
| 105 |
|
| 106 |
def test_floats(self): |
| 107 |
x = ''' |
| 108 |
[f0] |
| 109 |
f1 = float_add(f0, 3.5) |
| 110 |
''' |
| 111 |
loop = self.parse(x) |
| 112 |
box = loop.operations[0].getarg(0) |
| 113 |
|
| 114 |
|
| 115 |
assert box.__class__.__name__ == 'InputArgFloat' |
| 116 |
|
| 117 |
def test_debug_merge_point(self): |
| 118 |
x = ''' |
| 119 |
[] |
| 120 |
debug_merge_point(0, 0, "info") |
| 121 |
debug_merge_point(0, 0, 'info') |
| 122 |
debug_merge_point(1, 1, '<some ('other.')> info') |
| 123 |
debug_merge_point(0, 0, '(stuff) #1') |
| 124 |
''' |
| 125 |
loop = self.parse(x) |
| 126 |
assert loop.operations[0].getarg(2)._get_str() == 'info' |
| 127 |
assert loop.operations[0].getarg(1).value == 0 |
| 128 |
assert loop.operations[1].getarg(2)._get_str() == 'info' |
| 129 |
assert loop.operations[2].getarg(2)._get_str() == "<some ('other.')> info" |
| 130 |
assert loop.operations[2].getarg(1).value == 1 |
| 131 |
assert loop.operations[3].getarg(2)._get_str() == "(stuff) #1" |
| 132 |
|
| 133 |
|
| 134 |
def test_descr_with_obj_print(self): |
| 135 |
x = ''' |
| 136 |
[p0] |
| 137 |
setfield_gc(p0, 1, descr=<SomeDescr>) |
| 138 |
''' |
| 139 |
loop = self.parse(x) |
| 140 |
|
| 141 |
|
| 142 |
example_loop_log = '''\ |
| 143 |
# bridge out of Guard12, 6 ops |
| 144 |
[i0, i1, i2] |
| 145 |
i4 = int_add(i0, 2) |
| 146 |
i6 = int_sub(i1, 1) |
| 147 |
i8 = int_gt(i6, 3) |
| 148 |
guard_true(i8, descr=<Guard15>) [i4, i6] |
| 149 |
debug_merge_point('(no jitdriver.get_printable_location!)', 0) |
| 150 |
jump(i6, i4, descr=<Loop0>) |
| 151 |
''' |
| 152 |
|
| 153 |
def test_parse_no_namespace(self): |
| 154 |
loop = self.parse(self.example_loop_log, no_namespace=True) |
| 155 |
|
| 156 |
def test_attach_comment_to_loop(self): |
| 157 |
loop = self.parse(self.example_loop_log, no_namespace=True) |
| 158 |
assert loop.comment == ' # bridge out of Guard12, 6 ops' |
| 159 |
|
| 160 |
def test_parse_new_with_comma(self): |
| 161 |
|
| 162 |
x = ''' |
| 163 |
[] |
| 164 |
p0 = new(, descr=<SizeDescr 12>) |
| 165 |
''' |
| 166 |
loop = self.parse(x) |
| 167 |
assert loop.operations[0].getopname() == 'new' |
| 168 |
|
| 169 |
def test_no_fail_args(self): |
| 170 |
x = ''' |
| 171 |
[i0] |
| 172 |
guard_true(i0, descr=<Guard0>) |
| 173 |
''' |
| 174 |
loop = self.parse(x, nonstrict=True) |
| 175 |
assert loop.operations[0].getfailargs() == [] |
| 176 |
|
| 177 |
def test_offsets(self): |
| 178 |
x = """ |
| 179 |
[i0, i1] |
| 180 |
+10: i2 = int_add(i0, i1) |
| 181 |
i3 = int_add(i2, 3) |
| 182 |
""" |
| 183 |
|
| 184 |
loop = self.parse(x) |
| 185 |
assert loop.operations[0].offset == 10 |
| 186 |
assert not hasattr(loop.operations[1], 'offset') |
| 187 |
|
| 188 |
def test_last_offset(self): |
| 189 |
x = """ |
| 190 |
[i0, i1] |
| 191 |
+10: i2 = int_add(i0, i1) |
| 192 |
i3 = int_add(i2, 3) |
| 193 |
+30: --end of the loop-- |
| 194 |
""" |
| 195 |
loop = self.parse(x) |
| 196 |
assert len(loop.operations) == 2 |
| 197 |
assert loop.last_offset == 30 |
| 198 |
|
| 199 |
|
| 200 |
class TestOpParser(BaseTestOparser): |
| 201 |
|
| 202 |
OpParser = OpParser |
| 203 |
|
| 204 |
def test_label(self): |
| 205 |
x = """ |
| 206 |
[i0] |
| 207 |
label(i0, descr=1) |
| 208 |
jump(i0, descr=1) |
| 209 |
""" |
| 210 |
loop = self.parse(x) |
| 211 |
assert loop.operations[0].getdescr() is loop.operations[1].getdescr() |
| 212 |
assert isinstance(loop.operations[0].getdescr(), TargetToken) |
| 213 |
|
| 214 |
|
| 215 |
class ForbiddenModule(object): |
| 216 |
def __init__(self, name, old_mod): |
| 217 |
self.name = name |
| 218 |
self.old_mod = old_mod |
| 219 |
|
| 220 |
def __getattr__(self, attr): |
| 221 |
assert False, "You should not import module %s" % self.name |
| 222 |
|
| 223 |
|
| 224 |
class TestOpParserWithMock(BaseTestOparser): |
| 225 |
|
| 226 |
class OpParser(OpParser): |
| 227 |
use_mock_model = True |
| 228 |
|
| 229 |
def setup_class(cls): |
| 230 |
forbidden_mods = [ |
| 231 |
'rpython.jit.metainterp.history', |
| 232 |
'rpython.rtyper.lltypesystem.lltype', |
| 233 |
] |
| 234 |
for modname in forbidden_mods: |
| 235 |
if modname in sys.modules: |
| 236 |
newmod = ForbiddenModule(modname, sys.modules[modname]) |
| 237 |
sys.modules[modname] = newmod |
| 238 |
|
| 239 |
def teardown_class(cls): |
| 240 |
for modname, mod in sys.modules.iteritems(): |
| 241 |
if isinstance(mod, ForbiddenModule): |
| 242 |
sys.modules[modname] = mod.old_mod |