Debian Codegraph @

rpython/jit/backend/arm/test/test_regalloc.py

raw symbols SCIP
1
2 """ Tests for register allocation for common constructs
3 """
4
5 import py
6 from rpython.jit.metainterp.history import (BasicFailDescr,
7 BasicFinalDescr,
8 JitCellToken,
9 TargetToken)
10 from rpython.jit.metainterp.resoperation import rop
11 from rpython.jit.backend.llsupport.descr import GcCache
12 from rpython.jit.backend.detect_cpu import getcpuclass
13 from rpython.jit.backend.arm.regalloc import Regalloc, ARMFrameManager
14 from rpython.jit.tool.oparser import parse
15 from rpython.rtyper.lltypesystem import lltype, llmemory
16 from rpython.rtyper.annlowlevel import llhelper
17 from rpython.rtyper.lltypesystem import rstr
18 from rpython.rtyper import rclass
19 from rpython.jit.codewriter.effectinfo import EffectInfo
20 from rpython.jit.codewriter import longlong
21 from rpython.jit.backend.llsupport.test.test_regalloc_integration import BaseTestRegalloc
22
23
24 CPU = getcpuclass()
25
26
27 class MockGcDescr(GcCache):
28 def get_funcptr_for_new(self):
29 return 123
30 get_funcptr_for_newarray = get_funcptr_for_new
31 get_funcptr_for_newstr = get_funcptr_for_new
32 get_funcptr_for_newunicode = get_funcptr_for_new
33
34 def rewrite_assembler(self, cpu, operations):
35 pass
36
37
38 class MockAssembler(object):
39 gcrefs = None
40 _float_constants = None
41
42 def __init__(self, cpu=None, gc_ll_descr=None):
43 self.movs = []
44 self.performs = []
45 self.lea = []
46 if cpu is None:
47 cpu = CPU(None, None)
48 cpu.setup_once()
49 self.cpu = cpu
50 if gc_ll_descr is None:
51 gc_ll_descr = MockGcDescr(False)
52 self.cpu.gc_ll_descr = gc_ll_descr
53
54 def dump(self, *args):
55 pass
56
57 def regalloc_mov(self, from_loc, to_loc):
58 self.movs.append((from_loc, to_loc))
59
60 def regalloc_perform(self, op, arglocs, resloc):
61 self.performs.append((op, arglocs, resloc))
62
63 def regalloc_perform_discard(self, op, arglocs):
64 self.performs.append((op, arglocs))
65
66 def load_effective_addr(self, *args):
67 self.lea.append(args)
68
69
70 class RegAllocForTests(Regalloc):
71 position = 0
72
73 def _compute_next_usage(self, v, _):
74 return -1
75
76
77 def get_zero_division_error(self):
78 # for tests, a random emulated ll_inst will do
79 ll_inst = lltype.malloc(rclass.OBJECT)
80 ll_inst.typeptr = lltype.malloc(rclass.OBJECT_VTABLE,
81 immortal=True)
82 _zer_error_vtable = llmemory.cast_ptr_to_adr(ll_inst.typeptr)
83 zer_vtable = self.cast_adr_to_int(_zer_error_vtable)
84 zer_inst = lltype.cast_opaque_ptr(llmemory.GCREF, ll_inst)
85 return zer_vtable, zer_inst
86
87
88 class CustomBaseTestRegalloc(BaseTestRegalloc):
89 cpu = CPU(None, None)
90 cpu.setup_once()
91
92 def raising_func(i):
93 if i:
94 raise LLException(zero_division_error,
95 zero_division_value)
96 FPTR = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Void))
97 raising_fptr = llhelper(FPTR, raising_func)
98
99 def f(a):
100 return 23
101
102 FPTR = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed))
103 f_fptr = llhelper(FPTR, f)
104 f_calldescr = cpu.calldescrof(FPTR.TO, FPTR.TO.ARGS, FPTR.TO.RESULT,
105 EffectInfo.MOST_GENERAL)
106
107 zero_division_tp, zero_division_value = get_zero_division_error(cpu)
108 zd_addr = cpu.cast_int_to_adr(zero_division_tp)
109 zero_division_error = llmemory.cast_adr_to_ptr(zd_addr,
110 lltype.Ptr(rclass.OBJECT_VTABLE))
111 raising_calldescr = cpu.calldescrof(FPTR.TO, FPTR.TO.ARGS, FPTR.TO.RESULT,
112 EffectInfo.MOST_GENERAL)
113
114 targettoken = TargetToken()
115 targettoken2 = TargetToken()
116 fdescr1 = BasicFailDescr(1)
117 fdescr2 = BasicFailDescr(2)
118 fdescr3 = BasicFailDescr(3)
119
120 def setup_method(self, meth):
121 self.targettoken._ll_loop_code = 0
122 self.targettoken2._ll_loop_code = 0
123
124 def f1(x):
125 return x + 1
126
127 def f2(x, y):
128 return x * y
129
130 def f10(*args):
131 assert len(args) == 10
132 return sum(args)
133
134 F1PTR = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed))
135 F2PTR = lltype.Ptr(lltype.FuncType([lltype.Signed] * 2, lltype.Signed))
136 F10PTR = lltype.Ptr(lltype.FuncType([lltype.Signed] * 10, lltype.Signed))
137 f1ptr = llhelper(F1PTR, f1)
138 f2ptr = llhelper(F2PTR, f2)
139 f10ptr = llhelper(F10PTR, f10)
140
141 f1_calldescr = cpu.calldescrof(F1PTR.TO, F1PTR.TO.ARGS, F1PTR.TO.RESULT,
142 EffectInfo.MOST_GENERAL)
143 f2_calldescr = cpu.calldescrof(F2PTR.TO, F2PTR.TO.ARGS, F2PTR.TO.RESULT,
144 EffectInfo.MOST_GENERAL)
145 f10_calldescr = cpu.calldescrof(F10PTR.TO, F10PTR.TO.ARGS,
146 F10PTR.TO.RESULT, EffectInfo.MOST_GENERAL)
147 namespace = locals().copy()
148
149 class TestRegallocSimple(CustomBaseTestRegalloc):
150 def test_simple_loop(self):
151 ops = '''
152 [i0]
153 label(i0, descr=targettoken)
154 i1 = int_add(i0, 1)
155 i2 = int_lt(i1, 20)
156 guard_true(i2) [i1]
157 jump(i1, descr=targettoken)
158 '''
159 self.interpret(ops, [0])
160 assert self.getint(0) == 20
161
162 def test_two_loops_and_a_bridge(self):
163 ops = '''
164 [i0, i1, i2, i3]
165 label(i0, i1, i2, i3, descr=targettoken)
166 i4 = int_add(i0, 1)
167 i5 = int_lt(i4, 20)
168 guard_true(i5) [i4, i1, i2, i3]
169 jump(i4, i1, i2, i3, descr=targettoken)
170 '''
171 loop = self.interpret(ops, [0, 0, 0, 0])
172 ops2 = '''
173 [i5, i6, i7, i8]
174 label(i5, descr=targettoken2)
175 i1 = int_add(i5, 1)
176 i3 = int_add(i1, 1)
177 i4 = int_add(i3, 1)
178 i2 = int_lt(i4, 30)
179 guard_true(i2) [i4]
180 jump(i4, descr=targettoken2)
181 '''
182 loop2 = self.interpret(ops2, [0, 0, 0, 0])
183 bridge_ops = '''
184 [i4]
185 jump(i4, i4, i4, i4, descr=targettoken)
186 '''
187 bridge = self.attach_bridge(bridge_ops, loop2, 5)
188 self.run(loop2, 0, 0, 0, 0)
189 assert self.getint(0) == 31
190 assert self.getint(1) == 30
191 assert self.getint(2) == 30
192 assert self.getint(3) == 30
193
194 def test_pointer_arg(self):
195 ops = '''
196 [i0, p0]
197 label(i0, p0, descr=targettoken)
198 i1 = int_add(i0, 1)
199 i2 = int_lt(i1, 10)
200 guard_true(i2) [p0]
201 jump(i1, p0, descr=targettoken)
202 '''
203 S = lltype.GcStruct('S')
204 ptr = lltype.malloc(S)
205 self.interpret(ops, [0, ptr])
206 assert self.getptr(0, lltype.Ptr(S)) == ptr
207
208 def test_exception_bridge_no_exception(self):
209 ops = '''
210 [i0]
211 i1 = same_as_i(1)
212 call_n(ConstClass(raising_fptr), i0, descr=raising_calldescr)
213 guard_exception(ConstClass(zero_division_error)) [i1]
214 finish(0)
215 '''
216 bridge_ops = '''
217 [i3]
218 i2 = same_as_i(2)
219 guard_no_exception() [i2]
220 finish(1)
221 '''
222 loop = self.interpret(ops, [0])
223 assert self.getint(0) == 1
224 bridge = self.attach_bridge(bridge_ops, loop, 2)
225 self.run(loop, 0)
226 assert self.getint(0) == 1
227
228 def test_inputarg_unused(self):
229 ops = '''
230 [i0]
231 finish(1)
232 '''
233 self.interpret(ops, [0])
234 # assert did not explode
235
236 def test_nested_guards(self):
237 ops = '''
238 [i0, i1]
239 guard_true(i0) [i0, i1]
240 finish(4)
241 '''
242 bridge_ops = '''
243 [i0, i1]
244 guard_true(i0) [i0, i1]
245 finish(3)
246 '''
247 loop = self.interpret(ops, [0, 10])
248 assert self.getint(0) == 0
249 assert self.getint(1) == 10
250 bridge = self.attach_bridge(bridge_ops, loop, 0)
251 self.run(loop, 0, 10)
252 assert self.getint(0) == 0
253 assert self.getint(1) == 10
254
255 def test_nested_unused_arg(self):
256 ops = '''
257 [i0, i1]
258 guard_true(i0) [i0, i1]
259 finish(1)
260 '''
261 loop = self.interpret(ops, [0, 1])
262 assert self.getint(0) == 0
263 bridge_ops = '''
264 [i0, i1]
265 guard_true(i0) [i0]
266 finish(1)
267 '''
268 self.attach_bridge(bridge_ops, loop, 0)
269 self.run(loop, 0, 1)
270
271 def test_spill_for_constant(self):
272 ops = '''
273 [i0, i1, i2, i3]
274 label(i0, i1, i2, i3, descr=targettoken)
275 i4 = int_add(3, i1)
276 i5 = int_lt(i4, 30)
277 guard_true(i5) [i0, i4, i2, i3]
278 jump(1, i4, 3, 4, descr=targettoken)
279 '''
280 self.interpret(ops, [0, 0, 0, 0])
281 assert self.getints(4) == [1, 30, 3, 4]
282
283 def test_spill_for_constant_lshift(self):
284 ops = '''
285 [i0, i2, i1, i3]
286 label(i0, i2, i1, i3, descr=targettoken)
287 i4 = int_lshift(1, i1)
288 i5 = int_add(1, i1)
289 i6 = int_lt(i5, 30)
290 guard_true(i6) [i4, i5, i2, i3]
291 jump(i4, 3, i5, 4, descr=targettoken)
292 '''
293 self.interpret(ops, [0, 0, 0, 0])
294 assert self.getints(4) == [1<<29, 30, 3, 4]
295 ops = '''
296 [i0, i1, i2, i3]
297 label(i0, i1, i2, i3, descr=targettoken)
298 i4 = int_lshift(1, i1)
299 i5 = int_add(1, i1)
300 i6 = int_lt(i5, 30)
301 guard_true(i6) [i4, i5, i2, i3]
302 jump(i4, i5, 3, 4, descr=targettoken)
303 '''
304 self.interpret(ops, [0, 0, 0, 0])
305 assert self.getints(4) == [1<<29, 30, 3, 4]
306 ops = '''
307 [i0, i3, i1, i2]
308 label(i0, i3, i1, i2, descr=targettoken)
309 i4 = int_lshift(1, i1)
310 i5 = int_add(1, i1)
311 i6 = int_lt(i5, 30)
312 guard_true(i6) [i4, i5, i2, i3]
313 jump(i4, 4, i5, 3, descr=targettoken)
314 '''
315 self.interpret(ops, [0, 0, 0, 0])
316 assert self.getints(4) == [1<<29, 30, 3, 4]
317
318 def test_result_selected_reg_via_neg(self):
319 ops = '''
320 [i0, i1, i2, i3]
321 label(i0, i1, i2, i3, descr=targettoken)
322 i6 = int_neg(i2)
323 i7 = int_add(1, i1)
324 i4 = int_lt(i7, 10)
325 guard_true(i4) [i0, i6, i7]
326 jump(1, i7, i2, i6, descr=targettoken)
327 '''
328 self.interpret(ops, [0, 0, 3, 0])
329 assert self.getints(3) == [1, -3, 10]
330
331 def test_compare_memory_result_survives(self):
332 ops = '''
333 [i0, i1, i2, i3]
334 label(i0, i1, i2, i3, descr=targettoken)
335 i4 = int_lt(i0, i1)
336 i5 = int_add(i3, 1)
337 i6 = int_lt(i5, 30)
338 guard_true(i6) [i4]
339 jump(i0, i1, i4, i5, descr=targettoken)
340 '''
341 self.interpret(ops, [0, 10, 0, 0])
342 assert self.getint(0) == 1
343
344 def test_jump_different_args(self):
345 ops = '''
346 [i0, i15, i16, i18, i1, i2, i3]
347 label(i0, i15, i16, i18, i1, i2, i3, descr=targettoken)
348 i4 = int_add(i3, 1)
349 i5 = int_lt(i4, 20)
350 guard_true(i5) [i2, i1]
351 jump(i0, i18, i15, i16, i2, i1, i4, descr=targettoken)
352 '''
353 self.interpret(ops, [0, 1, 2, 3, 0, 0, 0])
354
355 def test_op_result_unused(self):
356 ops = '''
357 [i0, i1]
358 i2 = int_add(i0, i1)
359 finish(0)
360 '''
361 self.interpret(ops, [0, 0])
362
363 def test_guard_value_two_boxes(self):
364 ops = '''
365 [i0, i1, i2, i3, i4, i5, i6, i7]
366 guard_value(i6, i1) [i0, i2, i3, i4, i5, i6]
367 finish(i0)
368 '''
369 self.interpret(ops, [0, 0, 0, 0, 0, 0, 0, 0])
370 assert self.getint(0) == 0
371
372 def test_bug_wrong_stack_adj(self):
373 ops = '''
374 [i0, i1, i2, i3, i4, i5, i6, i7, i8]
375 i9 = same_as_i(0)
376 guard_true(i0) [i9, i0, i1, i2, i3, i4, i5, i6, i7, i8]
377 finish(1)
378 '''
379 loop = self.interpret(ops, [0, 1, 2, 3, 4, 5, 6, 7, 8])
380 assert self.getint(0) == 0
381 bridge_ops = '''
382 [i9, i0, i1, i2, i3, i4, i5, i6, i7, i8]
383 call_n(ConstClass(raising_fptr), 0, descr=raising_calldescr)
384 guard_true(i0) [i0, i1, i2, i3, i4, i5, i6, i7, i8]
385 finish(1)
386 '''
387 self.attach_bridge(bridge_ops, loop, 1)
388 self.run(loop, 0, 1, 2, 3, 4, 5, 6, 7, 8)
389 assert self.getints(9) == range(9)
390
391 def test_loopargs(self):
392 ops = """
393 [i0, i1, i2, i3]
394 i4 = int_add(i0, i1)
395 jump(i4, i1, i2, i3)
396 """
397 regalloc = self.prepare_loop(ops)
398 assert len(regalloc.rm.reg_bindings) == 0
399 assert regalloc.frame_manager.bindings_len_for_tests() == 4
400
401 def test_loopargs_2(self):
402 ops = """
403 [i0, i1, i2, i3]
404 i4 = int_add(i0, i1)
405 guard_false(i0) [i4, i1, i2, i3]
406 """
407 regalloc = self.prepare_loop(ops)
408 assert regalloc.frame_manager.bindings_len_for_tests() == 4
409
410 def test_loopargs_3(self):
411 ops = """
412 [i0, i1, i2, i3]
413 i4 = int_add(i0, i1)
414 guard_true(i4) [i0, i1, i2, i3, i4]
415 jump(i4, i1, i2, i3)
416 """
417 regalloc = self.prepare_loop(ops)
418 assert regalloc.frame_manager.bindings_len_for_tests() == 4
419
420
421 class TestRegallocCompOps(CustomBaseTestRegalloc):
422
423 def test_cmp_op_0(self):
424 ops = '''
425 [i0, i3]
426 i1 = same_as_i(1)
427 i2 = int_lt(i0, 100)
428 guard_true(i3) [i1, i2]
429 finish(i2)
430 '''
431 self.interpret(ops, [0, 1])
432 assert self.getint(0) == 1
433
434
435 class TestRegallocMoreRegisters(CustomBaseTestRegalloc):
436
437 cpu = CustomBaseTestRegalloc.cpu
438 targettoken = TargetToken()
439
440 S = lltype.GcStruct('S', ('field', lltype.Char))
441 fielddescr = cpu.fielddescrof(S, 'field')
442
443 A = lltype.GcArray(lltype.Char)
444 I = lltype.GcArray(lltype.Signed)
445 arraydescr = cpu.arraydescrof(A)
446 arraydescr_i = cpu.arraydescrof(I)
447
448 namespace = locals().copy()
449
450 def test_int_is_true(self):
451 ops = '''
452 [i0, i1, i2, i3, i4, i5, i6, i7]
453 i10 = int_is_true(i0)
454 i11 = int_is_true(i1)
455 i12 = int_is_true(i2)
456 i13 = int_is_true(i3)
457 i14 = int_is_true(i4)
458 i15 = int_is_true(i5)
459 i16 = int_is_true(i6)
460 i17 = int_is_true(i7)
461 guard_true(i0) [i10, i11, i12, i13, i14, i15, i16, i17]
462 '''
463 self.interpret(ops, [0, 42, 12, 0, 13, 0, 0, 3333])
464 assert self.getints(8) == [0, 1, 1, 0, 1, 0, 0, 1]
465
466 def test_comparison_ops(self):
467 ops = '''
468 [i0, i1, i2, i3, i4, i5, i6]
469 i10 = int_lt(i0, i1)
470 i11 = int_le(i2, i3)
471 i12 = int_ge(i4, i5)
472 i13 = int_eq(i5, i6)
473 i14 = int_gt(i6, i2)
474 i15 = int_ne(i2, i6)
475 guard_true(i0) [i10, i11, i12, i13, i14, i15]
476
477 '''
478 self.interpret(ops, [0, 1, 2, 3, 4, 5, 6])
479 assert self.getints(6) == [1, 1, 0, 0, 1, 1]
480
481 def test_strsetitem(self):
482 ops = '''
483 [p0, i]
484 strsetitem(p0, 1, i)
485 finish()
486 '''
487 llstr = rstr.mallocstr(10)
488 self.interpret(ops, [llstr, ord('a')])
489 assert llstr.chars[1] == 'a'
490
491 def test_setfield_char(self):
492 ops = '''
493 [p0, i]
494 setfield_gc(p0, i, descr=fielddescr)
495 finish()
496 '''
497 s = lltype.malloc(self.S)
498 self.interpret(ops, [s, ord('a')])
499 assert s.field == 'a'
500
501 def test_setarrayitem_gc(self):
502 ops = '''
503 [p0, i]
504 setarrayitem_gc(p0, 1, i, descr=arraydescr)
505 finish()
506 '''
507 s = lltype.malloc(self.A, 3)
508 self.interpret(ops, [s, ord('a')])
509 assert s[1] == 'a'
510
511 def test_setarrayitem2_gc(self):
512 ops = '''
513 [p0, i, i1]
514 setarrayitem_gc(p0, i1, i, descr=arraydescr)
515 finish()
516 '''
517 s = lltype.malloc(self.A, 3)
518 self.interpret(ops, [s, ord('a'), 1])
519 assert s[1] == 'a'
520
521 def test_setarrayitem3_gc(self):
522 ops = '''
523 [p0, i0, i1]
524 setarrayitem_gc(p0, i1, i0, descr=arraydescr_i)
525 finish()
526 '''
527 s = lltype.malloc(self.I, 3)
528 self.interpret(ops, [s, 1234567890, 1])
529 assert s[1] == 1234567890
530
531 def test_setarrayitem4_gc(self):
532 ops = '''
533 [p0, i0]
534 setarrayitem_gc(p0, 1, i0, descr=arraydescr_i)
535 finish()
536 '''
537 s = lltype.malloc(self.I, 3)
538 self.interpret(ops, [s, 1234567890])
539 assert s[1] == 1234567890
540
541
542 class TestRegallocFloats(CustomBaseTestRegalloc):
543 def test_float_add(self):
544 if not self.cpu.supports_floats:
545 py.test.skip("requires floats")
546 ops = '''
547 [f0, f1]
548 f2 = float_add(f0, f1)
549 guard_value(f0, f1) [f2, f0, f1]
550 '''
551 self.interpret(ops, [3.0, 1.5])
552 assert self.getfloats(3) == [4.5, 3.0, 1.5]
553
554 def test_float_adds_stack(self):
555 if not self.cpu.supports_floats:
556 py.test.skip("requires floats")
557 ops = '''
558 [f0, f1, f2, f3, f4, f5, f6, f7, f8]
559 f9 = float_add(f0, f1)
560 f10 = float_add(f8, 3.5)
561 guard_value(f0, f1) [f9, f10, f2, f3, f4, f5, f6, f7, f8]
562 '''
563 self.interpret(ops, [0.1, .2, .3, .4, .5, .6, .7, .8, .9])
564 assert self.getfloats(9) == [.1 + .2, .9 + 3.5, .3,
565 .4, .5, .6, .7, .8, .9]
566
567 def test_lt_const(self):
568 if not self.cpu.supports_floats:
569 py.test.skip("requires floats")
570 ops = '''
571 [f0]
572 i1 = float_lt(3.5, f0)
573 finish(i1)
574 '''
575 self.interpret(ops, [0.1])
576 assert self.getint(0) == 0
577
578 def test_bug_float_is_true_stack(self):
579 if not self.cpu.supports_floats:
580 py.test.skip("requires floats")
581 # NB. float_is_true no longer exists. Unsure if keeping this test
582 # makes sense any more.
583 ops = '''
584 [f0, f1, f2, f3, f4, f5, f6, f7, f8, f9]
585 i0 = float_ne(f0, 0.0)
586 i1 = float_ne(f1, 0.0)
587 i2 = float_ne(f2, 0.0)
588 i3 = float_ne(f3, 0.0)
589 i4 = float_ne(f4, 0.0)
590 i5 = float_ne(f5, 0.0)
591 i6 = float_ne(f6, 0.0)
592 i7 = float_ne(f7, 0.0)
593 i8 = float_ne(f8, 0.0)
594 i9 = float_ne(f9, 0.0)
595 guard_false(i9), [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]
596 '''
597 self.interpret(ops, [0.0, .1, .2, .3, .4, .5, .6, .7, .8, .9])
598 assert self.getints(9) == [0, 1, 1, 1, 1, 1, 1, 1, 1]
599
600
601 class TestRegAllocCallAndStackDepth(CustomBaseTestRegalloc):
602 def expected_param_depth(self, num_args):
603 # Assumes the arguments are all non-float
604 return num_args
605
606 def test_one_call(self):
607 ops = '''
608 [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]
609 i10 = call_i(ConstClass(f1ptr), i0, descr=f1_calldescr)
610 guard_false(i10), [i10, i1, i2, i3, i4, i5, i6, i7, i8, i9]
611 '''
612 self.interpret(ops, [4, 7, 9, 9, 9, 9, 9, 9, 9, 9])
613 assert self.getints(10) == [5, 7, 9, 9, 9, 9, 9, 9, 9, 9]
614
615 def test_two_calls(self):
616 ops = '''
617 [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]
618 i10 = call_i(ConstClass(f1ptr), i0, descr=f1_calldescr)
619 i11 = call_i(ConstClass(f2ptr), i10, i1, descr=f2_calldescr)
620 guard_false(i11) [i11, i1, i2, i3, i4, i5, i6, i7, i8, i9]
621 '''
622 self.interpret(ops, [4, 7, 9, 9, 9, 9, 9, 9, 9, 9])
623 assert self.getints(10) == [5 * 7, 7, 9, 9, 9, 9, 9, 9, 9, 9]
624
625 def test_call_many_arguments(self):
626 ops = '''
627 [i0, i1, i2, i3, i4, i5, i6, i7]
628 i8 = call_i(ConstClass(f10ptr), 1, i0, i1, i2, i3, i4, i5, i6, i7, 10, descr=f10_calldescr)
629 finish(i8)
630 '''
631 self.interpret(ops, [2, 3, 4, 5, 6, 7, 8, 9])
632 assert self.getint(0) == 55
633
634 def test_bridge_calls_1(self):
635 ops = '''
636 [i0, i1]
637 i2 = call_i(ConstClass(f1ptr), i0, descr=f1_calldescr)
638 guard_value(i2, 0, descr=fdescr1) [i2, i1]
639 finish(i1)
640 '''
641 loop = self.interpret(ops, [4, 7])
642 assert self.getint(0) == 5
643 ops = '''
644 [i2, i1]
645 i3 = call_i(ConstClass(f2ptr), i2, i1, descr=f2_calldescr)
646 finish(i3)
647 '''
648 self.attach_bridge(ops, loop, -2)
649
650 self.run(loop, 4, 7)
651 assert self.getint(0) == 5 * 7
652
653 def test_bridge_calls_2(self):
654 ops = '''
655 [i0, i1]
656 i2 = call_i(ConstClass(f2ptr), i0, i1, descr=f2_calldescr)
657 guard_value(i2, 0, descr=fdescr1) [i2]
658 finish(i1)
659 '''
660 loop = self.interpret(ops, [4, 7])
661 assert self.getint(0) == 4 * 7
662 ops = '''
663 [i2]
664 i3 = call_i(ConstClass(f1ptr), i2, descr=f1_calldescr)
665 finish(i3)
666 '''
667 self.attach_bridge(ops, loop, -2)
668
669 self.run(loop, 4, 7)
670 assert self.getint(0) == 29
671
672
673 class TestJumps(TestRegallocSimple):
674 def test_jump_with_consts(self):
675 loop = """
676 [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14]
677 label(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, descr=targettoken)
678 jump(i1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, descr=targettoken)
679 """
680 self.interpret(loop, range(15), run=False)
681 # ensure compiling this loop works
682 assert 1
683
684 def test_from_loop_to_loop(self):
685 def assembler_helper(failindex, virtualizable):
686 return 3
687
688 FUNCPTR = lltype.Ptr(lltype.FuncType([lltype.Signed, llmemory.GCREF],
689 lltype.Signed))
690
691 class FakeJitDriverSD:
692 index_of_virtualizable = -1
693 _assembler_helper_ptr = llhelper(FUNCPTR, assembler_helper)
694 assembler_helper_adr = llmemory.cast_ptr_to_adr(
695 _assembler_helper_ptr)
696
697 FakeJitDriverSD.portal_calldescr = self.cpu.calldescrof(
698 lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed)), \
699 [lltype.Signed], lltype.Signed, EffectInfo.MOST_GENERAL)
700 self.cpu.done_with_this_frame_descr_int = BasicFinalDescr()
701 loop1 = """
702 [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
703 i11 = int_add(i0, i1)
704 guard_false(i0) [i11, i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
705 """
706 large = self.interpret(loop1, range(11), run=False)
707 large._jitcelltoken.outermost_jitdriver_sd = FakeJitDriverSD()
708 self.namespace['looptoken'] = large._jitcelltoken
709 assert self.namespace['looptoken']._ll_function_addr != 0
710 loop2 = """
711 [i0]
712 i1 = force_token()
713 i2 = call_assembler_i(1,2,3,4,5,6,7,8,9,10,11, descr=looptoken)
714 guard_not_forced() [i0]
715 guard_false(i0) [i0, i2]
716 """
717
718 self.interpret(loop2, [110])
719 assert self.getint(0) == 110
720 assert self.getint(1) == 3
721
722 def test_far_far_jump(self):
723 ops = """
724 [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
725 label(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, descr=targettoken)
726 i11 = int_add(i0, 1)
727 i12 = int_lt(i11, 2)
728 i13 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
729 i14 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
730 i15 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
731 i16 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
732 i17 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
733 i18 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
734 i19 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
735 i20 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
736 i21 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
737 i22 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
738 i23 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
739 i24 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
740 i26 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
741 i27 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
742 i28 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
743 i29 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
744 i30 = call_i(ConstClass(f_fptr), i12, descr=f_calldescr)
745 guard_true(i12) [i11, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10]
746 jump(i11, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, descr=targettoken)
747 """
748 self.interpret(ops, range(11))
749 assert self.getint(0) == 2 # and not segfault()
750
751
752 class TestStrOps(CustomBaseTestRegalloc):
753 def test_newstr(self):
754 ops = """
755 [i0]
756 p1 = newstr(300)
757 i2 = strlen(p1)
758 finish(i2)
759 """
760 self.interpret(ops, [0])
761 assert self.getint(0) == 300
762 ops = """
763 [i0]
764 p1 = newstr(i0)
765 i2 = strlen(p1)
766 finish(i2)
767 """
768 self.interpret(ops, [300])
769 assert self.getint(0) == 300
770
771 def test_strlen(self):
772 s = rstr.mallocstr(300)
773 ops = """
774 [p0]
775 i1 = strlen(p0)
776 finish(i1)
777 """
778 self.interpret(ops, [s])
779 assert self.getint(0) == 300
780
781 def test_len_of_newstr(self):
782 ops = """
783 []
784 p0 = newstr(300)
785 finish(p0)
786 """
787 self.interpret(ops, [])
788 string = self.getptr(0, lltype.Ptr(rstr.STR))
789 assert len(string.chars) == 300