Debian Codegraph @

rpython/jit/backend/llsupport/test/test_regalloc_integration.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, JitCellToken,\
7 TargetToken
8 from rpython.jit.metainterp.resoperation import rop
9 from rpython.jit.backend.detect_cpu import getcpuclass
10 from rpython.jit.tool.oparser import parse
11 from rpython.rtyper.lltypesystem import lltype, llmemory
12 from rpython.rtyper.annlowlevel import llhelper
13 from rpython.rtyper.lltypesystem import rstr
14 from rpython.rtyper import rclass
15 from rpython.jit.codewriter import longlong
16 from rpython.jit.codewriter.effectinfo import EffectInfo
17
18
19 def get_zero_division_error(self):
20 # for tests, a random emulated ll_inst will do
21 ll_inst = lltype.malloc(rclass.OBJECT)
22 ll_inst.typeptr = lltype.malloc(rclass.OBJECT_VTABLE,
23 immortal=True)
24 _zer_error_vtable = llmemory.cast_ptr_to_adr(ll_inst.typeptr)
25 zer_vtable = self.cast_adr_to_int(_zer_error_vtable)
26 zer_inst = lltype.cast_opaque_ptr(llmemory.GCREF, ll_inst)
27 return zer_vtable, zer_inst
28
29
30 CPU = getcpuclass()
31 class BaseTestRegalloc(object):
32 cpu = CPU(None, None)
33 cpu.setup_once()
34
35 def raising_func(i):
36 if i:
37 raise LLException(zero_division_error,
38 zero_division_value)
39 FPTR = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Void))
40 raising_fptr = llhelper(FPTR, raising_func)
41 zero_division_tp, zero_division_value = get_zero_division_error(cpu)
42 zd_addr = cpu.cast_int_to_adr(zero_division_tp)
43 zero_division_error = llmemory.cast_adr_to_ptr(zd_addr,
44 lltype.Ptr(rclass.OBJECT_VTABLE))
45 raising_calldescr = cpu.calldescrof(FPTR.TO, FPTR.TO.ARGS, FPTR.TO.RESULT,
46 EffectInfo.MOST_GENERAL)
47
48 targettoken = TargetToken()
49 targettoken2 = TargetToken()
50 fdescr1 = BasicFailDescr(1)
51 fdescr2 = BasicFailDescr(2)
52 fdescr3 = BasicFailDescr(3)
53
54 def setup_method(self, meth):
55 self.targettoken._ll_loop_code = 0
56 self.targettoken2._ll_loop_code = 0
57
58 def f1(x):
59 return x+1
60
61 def f2(x, y):
62 return x*y
63
64 def f10(*args):
65 assert len(args) == 10
66 return sum(args)
67
68 def fgcref(x):
69 return 17
70
71 def fppii(x, y, i, j):
72 return 19
73
74 def ff(x, y):
75 return x + y + 0.1
76
77 F1PTR = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed))
78 F2PTR = lltype.Ptr(lltype.FuncType([lltype.Signed]*2, lltype.Signed))
79 F10PTR = lltype.Ptr(lltype.FuncType([lltype.Signed]*10, lltype.Signed))
80 FGCREFPTR = lltype.Ptr(lltype.FuncType([llmemory.GCREF], lltype.Signed))
81 FPPIIPTR = lltype.Ptr(lltype.FuncType([llmemory.GCREF, llmemory.GCREF, lltype.Signed, lltype.Signed], lltype.Signed))
82 FFPTR = lltype.Ptr(lltype.FuncType([lltype.Float]*2, lltype.Float))
83
84 f1ptr = llhelper(F1PTR, f1)
85 f2ptr = llhelper(F2PTR, f2)
86 f10ptr = llhelper(F10PTR, f10)
87 fgcrefptr = llhelper(FGCREFPTR, fgcref)
88 fppiiptr = llhelper(FPPIIPTR, fppii)
89 ffptr = llhelper(FFPTR, ff)
90
91 f1_calldescr = cpu.calldescrof(F1PTR.TO, F1PTR.TO.ARGS, F1PTR.TO.RESULT,
92 EffectInfo.MOST_GENERAL)
93 f2_calldescr = cpu.calldescrof(F2PTR.TO, F2PTR.TO.ARGS, F2PTR.TO.RESULT,
94 EffectInfo.MOST_GENERAL)
95 f10_calldescr = cpu.calldescrof(F10PTR.TO, F10PTR.TO.ARGS, F10PTR.TO.RESULT,
96 EffectInfo.MOST_GENERAL)
97 fgcref_calldescr = cpu.calldescrof(FGCREFPTR.TO, FGCREFPTR.TO.ARGS, FGCREFPTR.TO.RESULT,
98 EffectInfo.MOST_GENERAL)
99 fppii_calldescr = cpu.calldescrof(FPPIIPTR.TO, FPPIIPTR.TO.ARGS, FPPIIPTR.TO.RESULT,
100 EffectInfo.MOST_GENERAL)
101 ff_calldescr = cpu.calldescrof(FFPTR.TO, FFPTR.TO.ARGS, FFPTR.TO.RESULT,
102 EffectInfo.MOST_GENERAL)
103
104 namespace = locals().copy()
105
106 def parse(self, s, boxkinds=None, namespace=None):
107 return parse(s, self.cpu, namespace or self.namespace,
108 boxkinds=boxkinds)
109
110 def interpret(self, ops, args, run=True, namespace=None):
111 loop = self.parse(ops, namespace=namespace)
112 self.loop = loop
113 looptoken = JitCellToken()
114 self.cpu.compile_loop(loop.inputargs, loop.operations, looptoken)
115 arguments = []
116 for arg in args:
117 if isinstance(arg, int):
118 arguments.append(arg)
119 elif isinstance(arg, float):
120 arg = longlong.getfloatstorage(arg)
121 arguments.append(arg)
122 else:
123 assert isinstance(lltype.typeOf(arg), lltype.Ptr)
124 llgcref = lltype.cast_opaque_ptr(llmemory.GCREF, arg)
125 arguments.append(llgcref)
126 loop._jitcelltoken = looptoken
127 if run:
128 self.deadframe = self.cpu.execute_token(looptoken, *arguments)
129 return loop
130
131 def prepare_loop(self, ops):
132 loop = self.parse(ops)
133 self.loop = loop
134 regalloc = self.cpu.build_regalloc()
135 regalloc.prepare_loop(loop.inputargs, loop.operations,
136 loop.original_jitcell_token, [])
137 return regalloc
138
139 def getint(self, index):
140 return self.cpu.get_int_value(self.deadframe, index)
141
142 def getfloat(self, index):
143 return self.cpu.get_float_value(self.deadframe, index)
144
145 def getints(self, end):
146 return [self.cpu.get_int_value(self.deadframe, index) for
147 index in range(0, end)]
148
149 def getfloats(self, end):
150 return [longlong.getrealfloat(
151 self.cpu.get_float_value(self.deadframe, index))
152 for index in range(0, end)]
153
154 def getptr(self, index, T):
155 gcref = self.cpu.get_ref_value(self.deadframe, index)
156 return lltype.cast_opaque_ptr(T, gcref)
157
158 def attach_bridge(self, ops, loop, guard_op_index, **kwds):
159 guard_op = loop.operations[guard_op_index]
160 assert guard_op.is_guard()
161 bridge = self.parse(ops, **kwds)
162 assert ([box.type for box in bridge.inputargs] ==
163 [box.type for box in guard_op.getfailargs()])
164 faildescr = guard_op.getdescr()
165 self.cpu.compile_bridge(faildescr, bridge.inputargs,
166 bridge.operations,
167 loop._jitcelltoken)
168 return bridge
169
170 def run(self, loop, *arguments):
171 self.deadframe = self.cpu.execute_token(loop._jitcelltoken, *arguments)
172 return self.cpu.get_latest_descr(self.deadframe)
173
174 class TestRegallocSimple(BaseTestRegalloc):
175 def test_simple_loop(self):
176 ops = '''
177 [i0]
178 label(i0, descr=targettoken)
179 i1 = int_add(i0, 1)
180 i2 = int_lt(i1, 20)
181 guard_true(i2) [i1]
182 jump(i1, descr=targettoken)
183 '''
184 self.interpret(ops, [0])
185 assert self.getint(0) == 20
186
187 def test_two_loops_and_a_bridge(self):
188 ops = '''
189 [i0, i1, i2, i3]
190 label(i0, i1, i2, i3, descr=targettoken)
191 i4 = int_add(i0, 1)
192 i5 = int_lt(i4, 20)
193 guard_true(i5) [i4, i1, i2, i3]
194 jump(i4, i1, i2, i3, descr=targettoken)
195 '''
196 loop = self.interpret(ops, [0, 0, 0, 0])
197 ops2 = '''
198 [i5, i6, i7, i8]
199 label(i5, descr=targettoken2)
200 i1 = int_add(i5, 1)
201 i3 = int_add(i1, 1)
202 i4 = int_add(i3, 1)
203 i2 = int_lt(i4, 30)
204 guard_true(i2) [i4]
205 jump(i4, descr=targettoken2)
206 '''
207 loop2 = self.interpret(ops2, [0, 0, 0, 0])
208 bridge_ops = '''
209 [i4]
210 jump(i4, i4, i4, i4, descr=targettoken)
211 '''
212 bridge = self.attach_bridge(bridge_ops, loop2, 5)
213 self.run(loop2, 0, 0, 0, 0)
214 assert self.getint(0) == 31
215 assert self.getint(1) == 30
216 assert self.getint(2) == 30
217 assert self.getint(3) == 30
218
219 def test_pointer_arg(self):
220 ops = '''
221 [i0, p0]
222 label(i0, p0, descr=targettoken)
223 i1 = int_add(i0, 1)
224 i2 = int_lt(i1, 10)
225 guard_true(i2) [p0]
226 jump(i1, p0, descr=targettoken)
227 '''
228 S = lltype.GcStruct('S')
229 ptr = lltype.malloc(S)
230 self.interpret(ops, [0, ptr])
231 assert self.getptr(0, lltype.Ptr(S)) == ptr
232
233 def test_exception_bridge_no_exception(self):
234 ops = '''
235 [i0]
236 i1 = same_as_i(1)
237 call_n(ConstClass(raising_fptr), i0, descr=raising_calldescr)
238 guard_exception(ConstClass(zero_division_error)) [i1]
239 finish(0)
240 '''
241 bridge_ops = '''
242 [i3]
243 i2 = same_as_i(2)
244 guard_no_exception() [i2]
245 finish(1)
246 '''
247 loop = self.interpret(ops, [0])
248 assert self.getint(0) == 1
249 bridge = self.attach_bridge(bridge_ops, loop, 2)
250 self.run(loop, 0)
251 assert self.getint(0) == 1
252
253 def test_inputarg_unused(self):
254 ops = '''
255 [i0]
256 finish(1)
257 '''
258 self.interpret(ops, [0])
259 # assert did not explode
260
261 def test_nested_guards(self):
262 ops = '''
263 [i0, i1]
264 guard_true(i0) [i0, i1]
265 finish(4)
266 '''
267 bridge_ops = '''
268 [i0, i1]
269 guard_true(i0) [i0, i1]
270 finish(3)
271 '''
272 loop = self.interpret(ops, [0, 10])
273 assert self.getint(0) == 0
274 assert self.getint(1) == 10
275 bridge = self.attach_bridge(bridge_ops, loop, 0)
276 self.run(loop, 0, 10)
277 assert self.getint(0) == 0
278 assert self.getint(1) == 10
279
280 def test_nested_unused_arg(self):
281 ops = '''
282 [i0, i1]
283 guard_true(i0) [i0, i1]
284 finish(1)
285 '''
286 loop = self.interpret(ops, [0, 1])
287 assert self.getint(0) == 0
288 bridge_ops = '''
289 [i0, i1]
290 finish(2)
291 '''
292 self.attach_bridge(bridge_ops, loop, 0)
293 self.run(loop, 0, 1)
294
295 def test_spill_for_constant(self):
296 ops = '''
297 [i0, i1, i2, i3]
298 label(i0, i1, i2, i3, descr=targettoken)
299 i4 = int_add(3, i1)
300 i5 = int_lt(i4, 30)
301 guard_true(i5) [i0, i4, i2, i3]
302 jump(1, i4, 3, 4, descr=targettoken)
303 '''
304 self.interpret(ops, [0, 0, 0, 0])
305 assert self.getints(4) == [1, 30, 3, 4]
306
307 def test_spill_for_constant_lshift(self):
308 ops = '''
309 [i0, i2, i1, i3]
310 label(i0, i2, i1, i3, descr=targettoken)
311 i4 = int_lshift(1, i1)
312 i5 = int_add(1, i1)
313 i6 = int_lt(i5, 30)
314 guard_true(i6) [i4, i5, i2, i3]
315 jump(i4, 3, i5, 4, descr=targettoken)
316 '''
317 self.interpret(ops, [0, 0, 0, 0])
318 assert self.getints(4) == [1<<29, 30, 3, 4]
319 ops = '''
320 [i0, i1, i2, i3]
321 label(i0, i1, i2, i3, descr=targettoken)
322 i4 = int_lshift(1, i1)
323 i5 = int_add(1, i1)
324 i6 = int_lt(i5, 30)
325 guard_true(i6) [i4, i5, i2, i3]
326 jump(i4, i5, 3, 4, descr=targettoken)
327 '''
328 self.interpret(ops, [0, 0, 0, 0])
329 assert self.getints(4) == [1<<29, 30, 3, 4]
330 ops = '''
331 [i0, i3, i1, i2]
332 label(i0, i3, i1, i2, descr=targettoken)
333 i4 = int_lshift(1, i1)
334 i5 = int_add(1, i1)
335 i6 = int_lt(i5, 30)
336 guard_true(i6) [i4, i5, i2, i3]
337 jump(i4, 4, i5, 3, descr=targettoken)
338 '''
339 self.interpret(ops, [0, 0, 0, 0])
340 assert self.getints(4) == [1<<29, 30, 3, 4]
341
342 def test_result_selected_reg_via_neg(self):
343 ops = '''
344 [i0, i1, i2, i3]
345 label(i0, i1, i2, i3, descr=targettoken)
346 i6 = int_neg(i2)
347 i7 = int_add(1, i1)
348 i4 = int_lt(i7, 10)
349 guard_true(i4) [i0, i6, i7]
350 jump(1, i7, i2, i6, descr=targettoken)
351 '''
352 self.interpret(ops, [0, 0, 3, 0])
353 assert self.getints(3) == [1, -3, 10]
354
355 def test_compare_memory_result_survives(self):
356 ops = '''
357 [i0, i1, i2, i3]
358 label(i0, i1, i2, i3, descr=targettoken)
359 i4 = int_lt(i0, i1)
360 i5 = int_add(i3, 1)
361 i6 = int_lt(i5, 30)
362 guard_true(i6) [i4]
363 jump(i0, i1, i4, i5, descr=targettoken)
364 '''
365 self.interpret(ops, [0, 10, 0, 0])
366 assert self.getint(0) == 1
367
368 def test_jump_different_args(self):
369 ops = '''
370 [i0, i15, i16, i18, i1, i2, i3]
371 label(i0, i15, i16, i18, i1, i2, i3, descr=targettoken)
372 i4 = int_add(i3, 1)
373 i5 = int_lt(i4, 20)
374 guard_true(i5) [i2, i1]
375 jump(i0, i18, i15, i16, i2, i1, i4, descr=targettoken)
376 '''
377 self.interpret(ops, [0, 1, 2, 3, 0, 0, 0])
378
379 def test_op_result_unused(self):
380 ops = '''
381 [i0, i1]
382 i2 = int_add(i0, i1)
383 finish(0)
384 '''
385 self.interpret(ops, [0, 0])
386
387 def test_guard_value_two_boxes(self):
388 ops = '''
389 [i0, i1, i2, i3, i4, i5, i6, i7]
390 guard_value(i6, i1) [i0, i2, i3, i4, i5, i6]
391 finish(i0)
392 '''
393 self.interpret(ops, [0, 0, 0, 0, 0, 0, 0, 0])
394 assert self.getint(0) == 0
395
396 def test_bug_wrong_stack_adj(self):
397 ops = '''
398 [i0, i1, i2, i3, i4, i5, i6, i7, i8]
399 i9 = same_as_i(0)
400 guard_true(i0) [i9, i0, i1, i2, i3, i4, i5, i6, i7, i8]
401 finish(1)
402 '''
403 loop = self.interpret(ops, [0, 1, 2, 3, 4, 5, 6, 7, 8])
404 assert self.getint(0) == 0
405 bridge_ops = '''
406 [i9, i0, i1, i2, i3, i4, i5, i6, i7, i8]
407 call_n(ConstClass(raising_fptr), 0, descr=raising_calldescr)
408 guard_true(i9) [i0, i1, i2, i3, i4, i5, i6, i7, i8]
409 finish()
410 '''
411 self.attach_bridge(bridge_ops, loop, 1)
412 self.run(loop, 0, 1, 2, 3, 4, 5, 6, 7, 8)
413 assert self.getints(9) == range(9)
414
415 def test_loopargs(self):
416 ops = """
417 [i0, i1, i2, i3]
418 i4 = int_add(i0, i1)
419 jump(i4, i1, i2, i3)
420 """
421 regalloc = self.prepare_loop(ops)
422 # we pass stuff on the frame
423 assert len(regalloc.rm.reg_bindings) == 0
424 assert regalloc.fm.bindings_len_for_tests() == 4
425
426 def test_longevity(self):
427 ops = """
428 [i0, i1, i2, i3, i4, i10]
429 i5 = int_add(i0, i1) # 0
430 i8 = int_add(i0, i1) # 1 unused result, so not in real_usages
431 i6 = int_is_true(i5) # 2
432 i11 = int_add(i5, i10) # 3
433 guard_true(i6) [i0, i4] # 4
434 jump(i5, i1, i2, i3, i5, i11) # 5
435 """
436 regalloc = self.prepare_loop(ops)
437 i0, i1, i2, i3, i4, i10 = self.loop.inputargs
438 i5 = self.loop.operations[0]
439 i6 = self.loop.operations[2]
440 longevity = regalloc.longevity
441 assert longevity[i0].last_usage == 4
442 assert longevity[i0].real_usages == [0]
443 assert longevity[i1].last_usage == 5
444 assert longevity[i1].real_usages == [0]
445 assert longevity[i2].last_usage == 5
446 assert longevity[i2].real_usages is None
447 assert longevity[i3].last_usage == 5
448 assert longevity[i3].real_usages is None
449 assert longevity[i4].last_usage == 4
450 assert longevity[i4].real_usages is None
451 assert longevity[i5].last_usage == 5
452 assert longevity[i5].real_usages == [2, 3]
453 assert longevity[i6].last_usage == 4
454 assert longevity[i6].real_usages == [4]
455 assert longevity[i10].last_usage == 3
456 assert longevity[i10].real_usages == [3]
457
458 class TestRegallocCompOps(BaseTestRegalloc):
459
460 def test_cmp_op_0(self):
461 ops = '''
462 [i0, i3]
463 i1 = same_as_i(1)
464 i2 = int_lt(i0, 100)
465 guard_true(i3) [i1, i2]
466 i4 = int_neg(i2)
467 finish(0)
468 '''
469 self.interpret(ops, [0, 1])
470 assert self.getint(0) == 0
471
472 class TestRegallocMoreRegisters(BaseTestRegalloc):
473
474 cpu = BaseTestRegalloc.cpu
475 targettoken = TargetToken()
476
477 S = lltype.GcStruct('S', ('field', lltype.Char))
478 fielddescr = cpu.fielddescrof(S, 'field')
479
480 A = lltype.GcArray(lltype.Char)
481 arraydescr = cpu.arraydescrof(A)
482
483 namespace = locals().copy()
484
485 def test_int_is_true(self):
486 ops = '''
487 [i0, i1, i2, i3, i4, i5, i6, i7]
488 i10 = int_is_true(i0)
489 i11 = int_is_true(i1)
490 i12 = int_is_true(i2)
491 i13 = int_is_true(i3)
492 i14 = int_is_true(i4)
493 i15 = int_is_true(i5)
494 i16 = int_is_true(i6)
495 i17 = int_is_true(i7)
496 guard_true(i0) [i10, i11, i12, i13, i14, i15, i16, i17]
497 finish()
498 '''
499 self.interpret(ops, [0, 42, 12, 0, 13, 0, 0, 3333])
500 assert self.getints(8) == [0, 1, 1, 0, 1, 0, 0, 1]
501
502 def test_comparison_ops(self):
503 ops = '''
504 [i0, i1, i2, i3, i4, i5, i6]
505 i10 = int_lt(i0, i1)
506 i11 = int_le(i2, i3)
507 i12 = int_ge(i4, i5)
508 i13 = int_eq(i5, i6)
509 i14 = int_gt(i6, i2)
510 i15 = int_ne(i2, i6)
511 guard_true(i0) [i10, i11, i12, i13, i14, i15]
512 finish()
513 '''
514 self.interpret(ops, [0, 1, 2, 3, 4, 5, 6])
515 assert self.getints(6) == [1, 1, 0, 0, 1, 1]
516
517 def test_strsetitem(self):
518 ops = '''
519 [p0, i]
520 strsetitem(p0, 1, i)
521 finish()
522 '''
523 llstr = rstr.mallocstr(10)
524 self.interpret(ops, [llstr, ord('a')])
525 assert llstr.chars[1] == 'a'
526
527 def test_setfield_char(self):
528 ops = '''
529 [p0, i]
530 setfield_gc(p0, i, descr=fielddescr)
531 finish()
532 '''
533 s = lltype.malloc(self.S)
534 self.interpret(ops, [s, ord('a')])
535 assert s.field == 'a'
536
537 def test_setarrayitem_gc(self):
538 ops = '''
539 [p0, i]
540 setarrayitem_gc(p0, 1, i, descr=arraydescr)
541 finish()
542 '''
543 s = lltype.malloc(self.A, 3)
544 self.interpret(ops, [s, ord('a')])
545 assert s[1] == 'a'
546
547
548 class TestRegallocFloats(BaseTestRegalloc):
549 def setup_class(cls):
550 if not cls.cpu.supports_floats:
551 py.test.skip("needs float support")
552
553 def test_float_add(self):
554 ops = '''
555 [f0, f1]
556 f2 = float_add(f0, f1)
557 i0 = same_as_i(0)
558 guard_true(i0) [f2, f0, f1]
559 finish()
560 '''
561 self.interpret(ops, [3.0, 1.5])
562 assert self.getfloats(3) == [4.5, 3.0, 1.5]
563
564 def test_float_adds_stack(self):
565 ops = '''
566 [f0, f1, f2, f3, f4, f5, f6, f7, f8]
567 f9 = float_add(f0, f1)
568 f10 = float_add(f8, 3.5)
569 i0 = same_as_i(0)
570 guard_true(i0) [f9, f10, f2, f3, f4, f5, f6, f7, f8]
571 finish()
572 '''
573 self.interpret(ops, [0.1, .2, .3, .4, .5, .6, .7, .8, .9])
574 assert self.getfloats(9) == [.1+.2, .9+3.5, .3, .4, .5, .6, .7, .8, .9]
575
576 def test_lt_const(self):
577 ops = '''
578 [f0]
579 i1 = float_lt(3.5, f0)
580 finish(i1)
581 '''
582 self.interpret(ops, [0.1])
583 assert self.getint(0) == 0
584
585 def test_bug_float_is_true_stack(self):
586 # NB. float_is_true no longer exists. Unsure if keeping this test
587 # makes sense any more.
588 ops = '''
589 [f0, f1, f2, f3, f4, f5, f6, f7, f8, f9]
590 i0 = float_ne(f0, 0.0)
591 i1 = float_ne(f1, 0.0)
592 i2 = float_ne(f2, 0.0)
593 i3 = float_ne(f3, 0.0)
594 i4 = float_ne(f4, 0.0)
595 i5 = float_ne(f5, 0.0)
596 i6 = float_ne(f6, 0.0)
597 i7 = float_ne(f7, 0.0)
598 i8 = float_ne(f8, 0.0)
599 i9 = float_ne(f9, 0.0)
600 guard_true(i0) [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]
601 finish()
602 '''
603 loop = self.interpret(ops, [0.0, .1, .2, .3, .4, .5, .6, .7, .8, .9])
604 assert self.getints(9) == [0, 1, 1, 1, 1, 1, 1, 1, 1]
605
606 class TestRegAllocCallAndStackDepth(BaseTestRegalloc):
607 def setup_class(cls):
608 py.test.skip("skip for now, not sure what do we do")
609
610 def expected_frame_depth(self, num_call_args, num_pushed_input_args=0):
611 # Assumes the arguments are all non-float
612 if not self.cpu.IS_64_BIT:
613 extra_esp = num_call_args
614 return extra_esp
615 elif self.cpu.IS_64_BIT:
616 # 'num_pushed_input_args' is for X86_64 only
617 extra_esp = max(num_call_args - 6, 0)
618 return num_pushed_input_args + extra_esp
619
620 def test_one_call(self):
621 ops = '''
622 [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i9b]
623 i10 = call(ConstClass(f1ptr), i0, descr=f1_calldescr)
624 guard_false(i10) [i10, i1, i2, i3, i4, i5, i6, i7, i8, i9, i9b]
625 '''
626 loop = self.interpret(ops, [4, 7, 9, 9 ,9, 9, 9, 9, 9, 9, 8])
627 assert self.getints(11) == [5, 7, 9, 9, 9, 9, 9, 9, 9, 9, 8]
628 clt = loop._jitcelltoken.compiled_loop_token
629 assert clt.frame_depth == self.expected_frame_depth(1, 5)
630
631 def test_one_call_reverse(self):
632 ops = '''
633 [i1, i2, i3, i4, i5, i6, i7, i8, i9, i9b, i0]
634 i10 = call(ConstClass(f1ptr), i0, descr=f1_calldescr)
635 guard_false(i10) [i10, i1, i2, i3, i4, i5, i6, i7, i8, i9, i9b]
636 '''
637 loop = self.interpret(ops, [7, 9, 9 ,9, 9, 9, 9, 9, 9, 8, 4])
638 assert self.getints(11) == [5, 7, 9, 9, 9, 9, 9, 9, 9, 9, 8]
639 clt = loop._jitcelltoken.compiled_loop_token
640 assert clt.frame_depth == self.expected_frame_depth(1, 6)
641
642 def test_two_calls(self):
643 ops = '''
644 [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]
645 i10 = call(ConstClass(f1ptr), i0, descr=f1_calldescr)
646 i11 = call(ConstClass(f2ptr), i10, i1, descr=f2_calldescr)
647 guard_false(i5) [i11, i1, i2, i3, i4, i5, i6, i7, i8, i9]
648 '''
649 loop = self.interpret(ops, [4, 7, 9, 9 ,9, 9, 9, 9, 9, 9])
650 assert self.getints(10) == [5*7, 7, 9, 9, 9, 9, 9, 9, 9, 9]
651 clt = loop._jitcelltoken.compiled_loop_token
652 assert clt.frame_depth == self.expected_frame_depth(2, 5)
653
654 def test_call_many_arguments(self):
655 # NB: The first and last arguments in the call are constants. This
656 # is primarily for x86-64, to ensure that loading a constant to an
657 # argument register or to the stack works correctly
658 ops = '''
659 [i0, i1, i2, i3, i4, i5, i6, i7]
660 i8 = call(ConstClass(f10ptr), 1, i0, i1, i2, i3, i4, i5, i6, i7, 10, descr=f10_calldescr)
661 finish(i8)
662 '''
663 loop = self.interpret(ops, [2, 3, 4, 5, 6, 7, 8, 9])
664 assert self.getint(0) == 55
665 clt = loop._jitcelltoken.compiled_loop_token
666 assert clt.frame_depth == self.expected_frame_depth(10)
667
668 def test_bridge_calls_1(self):
669 ops = '''
670 [i0, i1]
671 i2 = call(ConstClass(f1ptr), i0, descr=f1_calldescr)
672 guard_value(i2, 0, descr=fdescr1) [i2, i0, i1]
673 guard_false(i1) [i1]
674 '''
675 loop = self.interpret(ops, [4, 7])
676 assert self.getint(0) == 5
677 clt = loop._jitcelltoken.compiled_loop_token
678 orgdepth = clt.frame_depth
679 assert orgdepth == self.expected_frame_depth(1, 2)
680
681 ops = '''
682 [i2, i0, i1]
683 i3 = call(ConstClass(f2ptr), i2, i1, descr=f2_calldescr)
684 guard_false(i0, descr=fdescr2) [i3, i0]
685 '''
686 bridge = self.attach_bridge(ops, loop, -2)
687
688 assert clt.frame_depth == max(orgdepth, self.expected_frame_depth(2, 2))
689 assert loop.operations[-2].getdescr()._x86_bridge_frame_depth == \
690 self.expected_frame_depth(2, 2)
691
692 self.run(loop, 4, 7)
693 assert self.getint(0) == 5*7
694
695 def test_bridge_calls_2(self):
696 ops = '''
697 [i0, i1]
698 i2 = call(ConstClass(f2ptr), i0, i1, descr=f2_calldescr)
699 guard_value(i2, 0, descr=fdescr1) [i2]
700 guard_false(i2) [i2]
701 '''
702 loop = self.interpret(ops, [4, 7])
703 assert self.getint(0) == 4*7
704 clt = loop._jitcelltoken.compiled_loop_token
705 orgdepth = clt.frame_depth
706 assert orgdepth == self.expected_frame_depth(2)
707
708 ops = '''
709 [i2]
710 i3 = call(ConstClass(f1ptr), i2, descr=f1_calldescr)
711 guard_false(i3, descr=fdescr2) [i3]
712 '''
713 bridge = self.attach_bridge(ops, loop, -2)
714
715 assert clt.frame_depth == max(orgdepth, self.expected_frame_depth(1))
716 assert loop.operations[-2].getdescr()._x86_bridge_frame_depth == \
717 self.expected_frame_depth(1)
718
719 self.run(loop, 4, 7)
720 assert self.getint(0) == 29
721