| 1 | import py |
| 2 | from rpython.jit.tl.tlopcode import compile, NEW, RETURN |
| 3 | from rpython.jit.tl.test import test_tl |
| 4 | from rpython.jit.tl.tlc import ConstantPool |
| 5 | |
| 6 | def test_constant_pool(): |
| 7 | pool = ConstantPool() |
| 8 | bytecode = compile(""" |
| 9 | NEW foo,bar,meth=f |
| 10 | f: |
| 11 | RETURN |
| 12 | """, pool) |
| 13 | expected = test_tl.list2bytecode([NEW, 0, RETURN]) |
| 14 | assert expected == bytecode |
| 15 | assert len(pool.classdescrs) == 1 |
| 16 | descr = pool.classdescrs[0] |
| 17 | assert descr.attributes == ['foo', 'bar'] |
| 18 | assert descr.methods == [('meth', 2)] |
| 19 | |
| 20 | def test_serialization(): |
| 21 | from rpython.jit.tl.tlopcode import serialize_program, decode_program |
| 22 | pool = ConstantPool() |
| 23 | bytecode = compile(""" |
| 24 | NEW foo,bar,meth=f |
| 25 | SETATTR foobar |
| 26 | f: |
| 27 | RETURN |
| 28 | """, pool) |
| 29 | s = serialize_program(bytecode, pool) |
| 30 | bytecode2, pool2 = decode_program(s) |
| 31 | assert bytecode == bytecode2 |
| 32 | assert pool == pool2 |
| 33 | |
| 34 | class TestTLC(object): |
| 35 | @staticmethod |
| 36 | def interp(code='', pc=0, inputarg=0): |
| 37 | from rpython.jit.tl.tlc import interp |
| 38 | return interp(code, pc, inputarg) |
| 39 | |
| 40 | def test_unconditional_branch(self): |
| 41 | bytecode = compile(""" |
| 42 | main: |
| 43 | BR target |
| 44 | PUSH 123 |
| 45 | RETURN |
| 46 | target: |
| 47 | PUSH 42 |
| 48 | RETURN |
| 49 | """) |
| 50 | res = self.interp(bytecode, 0, 0) |
| 51 | assert res == 42 |
| 52 | |
| 53 | def test_basic_cons_cell(self): |
| 54 | bytecode = compile(""" |
| 55 | NIL |
| 56 | PUSHARG |
| 57 | CONS |
| 58 | PUSH 1 |
| 59 | CONS |
| 60 | CDR |
| 61 | CAR |
| 62 | """) |
| 63 | |
| 64 | res = self.interp(bytecode, 0, 42) |
| 65 | assert res == 42 |
| 66 | |
| 67 | def test_nth(self): |
| 68 | bytecode = compile(""" |
| 69 | NIL |
| 70 | PUSH 4 |
| 71 | CONS |
| 72 | PUSH 2 |
| 73 | CONS |
| 74 | PUSH 1 |
| 75 | CONS |
| 76 | PUSHARG |
| 77 | DIV |
| 78 | """) |
| 79 | |
| 80 | res = self.interp(bytecode, 0, 0) |
| 81 | assert res == 1 |
| 82 | res = self.interp(bytecode, 0, 1) |
| 83 | assert res == 2 |
| 84 | res = self.interp(bytecode, 0, 2) |
| 85 | assert res == 4 |
| 86 | |
| 87 | py.test.raises(IndexError, self.interp, bytecode, 0, 3) |
| 88 | |
| 89 | def test_concat(self): |
| 90 | bytecode = compile(""" |
| 91 | NIL |
| 92 | PUSH 4 |
| 93 | CONS |
| 94 | PUSH 2 |
| 95 | CONS |
| 96 | NIL |
| 97 | PUSH 5 |
| 98 | CONS |
| 99 | PUSH 3 |
| 100 | CONS |
| 101 | PUSH 1 |
| 102 | CONS |
| 103 | ADD |
| 104 | PUSHARG |
| 105 | DIV |
| 106 | """) |
| 107 | |
| 108 | for i, n in enumerate([2, 4, 1, 3, 5]): |
| 109 | res = self.interp(bytecode, 0, i) |
| 110 | assert res == n |
| 111 | |
| 112 | def test_concat_errors(self): |
| 113 | bytecode = compile(""" |
| 114 | NIL |
| 115 | PUSH 4 |
| 116 | ADD |
| 117 | """) |
| 118 | py.test.raises(TypeError, self.interp, bytecode, 0, 0) |
| 119 | |
| 120 | bytecode = compile(""" |
| 121 | PUSH 4 |
| 122 | NIL |
| 123 | ADD |
| 124 | """) |
| 125 | py.test.raises(TypeError, self.interp, bytecode, 0, 0) |
| 126 | |
| 127 | |
| 128 | bytecode = compile(""" |
| 129 | NIL |
| 130 | PUSH 1 |
| 131 | CONS |
| 132 | PUSH 4 |
| 133 | ADD |
| 134 | """) |
| 135 | py.test.raises(TypeError, self.interp, bytecode, 0, 0) |
| 136 | |
| 137 | bytecode = compile(""" |
| 138 | PUSH 4 |
| 139 | NIL |
| 140 | PUSH 1 |
| 141 | CONS |
| 142 | ADD |
| 143 | """) |
| 144 | py.test.raises(TypeError, self.interp, bytecode, 0, 0) |
| 145 | |
| 146 | |
| 147 | bytecode = compile(""" |
| 148 | PUSH 2 |
| 149 | PUSH 1 |
| 150 | CONS |
| 151 | NIL |
| 152 | ADD |
| 153 | """) |
| 154 | py.test.raises(TypeError, self.interp, bytecode, 0, 0) |
| 155 | |
| 156 | def test_new_obj(self): |
| 157 | from rpython.jit.tl.tlc import interp_eval, InstanceObj, nil |
| 158 | pool = ConstantPool() |
| 159 | bytecode = compile(""" |
| 160 | NEW foo,bar |
| 161 | """, pool) |
| 162 | obj = interp_eval(bytecode, 0, [nil], pool) |
| 163 | assert isinstance(obj, InstanceObj) |
| 164 | assert len(obj.values) == 2 |
| 165 | assert sorted(obj.cls.attributes.keys()) == ['bar', 'foo'] |
| 166 | |
| 167 | def test_setattr(self): |
| 168 | from rpython.jit.tl.tlc import interp_eval, nil |
| 169 | pool = ConstantPool() |
| 170 | bytecode = compile(""" |
| 171 | NEW foo,bar |
| 172 | PICK 0 |
| 173 | PUSH 42 |
| 174 | SETATTR foo |
| 175 | """, pool) |
| 176 | obj = interp_eval(bytecode, 0, [nil], pool) |
| 177 | assert obj.values[0].int_o() == 42 |
| 178 | assert obj.values[1] is nil |
| 179 | |
| 180 | def test_getattr(self): |
| 181 | from rpython.jit.tl.tlc import interp_eval, nil |
| 182 | pool = ConstantPool() |
| 183 | bytecode = compile(""" |
| 184 | NEW foo,bar |
| 185 | PICK 0 |
| 186 | PUSH 42 |
| 187 | SETATTR bar |
| 188 | GETATTR bar |
| 189 | """, pool) |
| 190 | res = interp_eval(bytecode, 0, [nil], pool) |
| 191 | assert res.int_o() == 42 |
| 192 | |
| 193 | def test_obj_truth(self): |
| 194 | from rpython.jit.tl.tlc import interp_eval, nil |
| 195 | pool = ConstantPool() |
| 196 | bytecode = compile(""" |
| 197 | NEW foo,bar |
| 198 | BR_COND true |
| 199 | PUSH 12 |
| 200 | PUSH 1 |
| 201 | BR_COND exit |
| 202 | true: |
| 203 | PUSH 42 |
| 204 | exit: |
| 205 | RETURN |
| 206 | """, pool) |
| 207 | res = interp_eval(bytecode, 0, [nil], pool) |
| 208 | assert res.int_o() == 42 |
| 209 | |
| 210 | def test_obj_equality(self): |
| 211 | from rpython.jit.tl.tlc import interp_eval, nil |
| 212 | pool = ConstantPool() |
| 213 | bytecode = compile(""" |
| 214 | NEW foo,bar |
| 215 | NEW foo,bar |
| 216 | EQ |
| 217 | """, pool) |
| 218 | res = interp_eval(bytecode, 0, [nil], pool) |
| 219 | assert res.int_o() == 0 |
| 220 | |
| 221 | def test_method(self): |
| 222 | from rpython.jit.tl.tlc import interp_eval, nil |
| 223 | pool = ConstantPool() |
| 224 | bytecode = compile(""" |
| 225 | NEW foo,meth=meth |
| 226 | PICK 0 |
| 227 | PUSH 42 |
| 228 | SETATTR foo |
| 229 | SEND meth/0 |
| 230 | RETURN |
| 231 | meth: |
| 232 | PUSHARG |
| 233 | GETATTR foo |
| 234 | RETURN |
| 235 | """, pool) |
| 236 | res = interp_eval(bytecode, 0, [nil], pool) |
| 237 | assert res.int_o() == 42 |
| 238 | |
| 239 | def test_method_arg(self): |
| 240 | from rpython.jit.tl.tlc import interp_eval, nil |
| 241 | pool = ConstantPool() |
| 242 | bytecode = compile(""" |
| 243 | NEW foo,meth=meth |
| 244 | PICK 0 |
| 245 | PUSH 40 |
| 246 | SETATTR foo |
| 247 | PUSH 2 |
| 248 | SEND meth/1 |
| 249 | RETURN |
| 250 | meth: |
| 251 | PUSHARG |
| 252 | GETATTR foo |
| 253 | PUSHARGN 1 |
| 254 | ADD |
| 255 | RETURN |
| 256 | """, pool) |
| 257 | res = interp_eval(bytecode, 0, [nil], pool) |
| 258 | assert res.int_o() == 42 |
| 259 | |
| 260 | def test_call_without_return_value(self): |
| 261 | from rpython.jit.tl.tlc import interp_eval, nil |
| 262 | pool = ConstantPool() |
| 263 | bytecode = compile(""" |
| 264 | CALL foo |
| 265 | PUSH 42 |
| 266 | RETURN |
| 267 | foo: |
| 268 | RETURN |
| 269 | """, pool) |
| 270 | res = interp_eval(bytecode, 0, [nil], pool) |
| 271 | assert res.int_o() == 42 |
| 272 | |
| 273 | def compile(self, filename): |
| 274 | from rpython.jit.tl.tlc import interp_eval, IntObj |
| 275 | pool = ConstantPool() |
| 276 | path = py.path.local(__file__).join(filename) |
| 277 | src = path.read() |
| 278 | bytecode = compile(src, pool) |
| 279 | def fn(n): |
| 280 | obj = IntObj(n) |
| 281 | res = interp_eval(bytecode, 0, [obj], pool) |
| 282 | return res.int_o() |
| 283 | return fn |
| 284 | |
| 285 | def test_binarytree(self): |
| 286 | search = self.compile('../../binarytree.tlc.src') |
| 287 | assert search(20) |
| 288 | assert search(10) |
| 289 | assert search(15) |
| 290 | assert search(30) |
| 291 | assert not search(1) |
| 292 | assert not search(40) |
| 293 | assert not search(12) |
| 294 | assert not search(27) |
| 295 | |
| 296 | def test_fibo(self): |
| 297 | fibo = self.compile('../../fibo.tlc.src') |
| 298 | assert fibo(1) == 1 |
| 299 | assert fibo(2) == 1 |
| 300 | assert fibo(3) == 2 |
| 301 | assert fibo(7) == 13 |
| 302 | |
| 303 | def test_accumulator(self): |
| 304 | acc = self.compile('../../accumulator.tlc.src') |
| 305 | assert acc(0) == 0 |
| 306 | assert acc(1) == 0 |
| 307 | assert acc(10) == sum(range(10)) |
| 308 | assert acc(20) == sum(range(20)) |
| 309 | assert acc(-1) == 1 |
| 310 | assert acc(-2) == 2 |
| 311 | assert acc(-10) == 10 |