Debian Codegraph @

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

raw symbols SCIP
1 from rpython.rlib.objectmodel import instantiate
2 from rpython.jit.backend.arm.assembler import AssemblerARM
3 from rpython.jit.backend.arm.locations import imm, ConstFloatLoc
4 from rpython.jit.backend.arm.locations import RegisterLocation, StackLocation
5 from rpython.jit.backend.arm.locations import VFPRegisterLocation, get_fp_offset
6 from rpython.jit.backend.arm.locations import RawSPStackLocation
7 from rpython.jit.backend.arm.registers import lr, ip, fp, vfp_ip, sp
8 from rpython.jit.backend.arm.conditions import AL
9 from rpython.jit.backend.arm.arch import WORD
10 from rpython.jit.metainterp.history import FLOAT
11 import py
12
13
14 class MockInstr(object):
15 def __init__(self, name, *args, **kwargs):
16 self.name = name
17 self.args = args
18 self.kwargs = kwargs
19
20 def __call__(self, *args, **kwargs):
21 self.args = args
22 self.kwargs = kwargs
23
24 def __repr__(self):
25 return "%s %r %r" % (self.name, self.args, self.kwargs)
26
27 __str__ = __repr__
28
29 def __eq__(self, other):
30 return (self.__class__ == other.__class__
31 and self.name == other.name
32 and self.args == other.args
33 and self.kwargs == other.kwargs)
34 mi = MockInstr
35
36
37 # helper method for tests
38 def r(i):
39 return RegisterLocation(i)
40
41
42 def vfp(i):
43 return VFPRegisterLocation(i)
44
45
46 def stack(i, **kwargs):
47 return StackLocation(i, get_fp_offset(0, i), **kwargs)
48
49
50 def stack_float(i, **kwargs):
51 return StackLocation(i, get_fp_offset(0, i + 1), type=FLOAT)
52
53
54 def imm_float(value):
55 addr = int(value) # whatever
56 return ConstFloatLoc(addr)
57
58 def raw_stack(i):
59 return RawSPStackLocation(i)
60
61 def raw_stack_float(i):
62 return RawSPStackLocation(i, type=FLOAT)
63
64
65 class MockBuilder(object):
66 def __init__(self):
67 self.instrs = []
68
69 def __getattr__(self, name):
70 i = MockInstr(name)
71 self.instrs.append(i)
72 return i
73
74 class MockRegalloc(object):
75 def get_free_reg(self):
76 return r('helper')
77
78 class BaseMovTest(object):
79 def setup_method(self, method):
80 self.builder = MockBuilder()
81 self.asm = instantiate(AssemblerARM)
82 self.asm._regalloc = MockRegalloc()
83 self.asm.mc = self.builder
84
85 def validate(self, expected):
86 result = self.builder.instrs
87 assert result == expected
88
89 def mov(self, a, b, expected=None):
90 self.asm.regalloc_mov(a, b)
91 self.validate(expected)
92
93
94 class TestRegallocMov(BaseMovTest):
95
96 def test_mov_imm_to_reg(self):
97 val = imm(123)
98 reg = r(7)
99 expected = [mi('gen_load_int', 7, 123, cond=AL)]
100 self.mov(val, reg, expected)
101
102 def test_mov_large_imm_to_reg(self):
103 val = imm(65536)
104 reg = r(7)
105 expected = [mi('gen_load_int', 7, 65536, cond=AL)]
106 self.mov(val, reg, expected)
107
108 def test_mov_imm_to_stacklock(self):
109 val = imm(100)
110 s = stack(7)
111 expected = [
112 mi('gen_load_int', lr.value, 100, cond=AL),
113 mi('STR_ri', lr.value, fp.value, imm=s.value, cond=AL),
114 ]
115 self.mov(val, s, expected)
116
117 def test_mov_big_imm_to_stacklock(self):
118 val = imm(65536)
119 s = stack(7)
120 expected = [
121 mi('gen_load_int', lr.value, 65536, cond=AL),
122 mi('STR_ri', lr.value, fp.value, imm=s.value, cond=AL),
123 ]
124 self.mov(val, s, expected)
125
126 def test_mov_imm_to_big_stacklock(self):
127 val = imm(100)
128 s = stack(8191)
129 expected = [ mi('gen_load_int', lr.value, 100, cond=AL),
130 mi('gen_load_int', ip.value, s.value, cond=AL),
131 mi('STR_rr', lr.value, fp.value, ip.value, cond=AL),
132 ]
133 self.mov(val, s, expected)
134
135 def test_mov_big_imm_to_big_stacklock(self):
136 val = imm(65536)
137 s = stack(8191)
138 expected = [
139 mi('gen_load_int', lr.value, 65536, cond=AL),
140 mi('gen_load_int', ip.value, s.value, cond=AL),
141 mi('STR_rr', lr.value, fp.value, ip.value, cond=AL),
142 ]
143 self.mov(val, s, expected)
144
145 def test_mov_reg_to_reg(self):
146 r1 = r(1)
147 r9 = r(9)
148 expected = [mi('MOV_rr', r9.value, r1.value, cond=AL)]
149 self.mov(r1, r9, expected)
150
151 def test_mov_reg_to_stack(self):
152 s = stack(10)
153 r6 = r(6)
154 expected = [mi('STR_ri', r6.value, fp.value, imm=s.value, cond=AL)]
155 self.mov(r6, s, expected)
156
157 def test_mov_reg_to_big_stackloc(self):
158 s = stack(8191)
159 r6 = r(6)
160 expected = [
161 mi('gen_load_int', ip.value, s.value, cond=AL),
162 mi('STR_rr', r6.value, fp.value, ip.value, cond=AL),
163 ]
164 self.mov(r6, s, expected)
165
166 def test_mov_stack_to_reg(self):
167 s = stack(10)
168 r6 = r(6)
169 expected = [mi('LDR_ri', r6.value, fp.value, imm=s.value, cond=AL)]
170 self.mov(s, r6, expected)
171
172 def test_mov_big_stackloc_to_reg(self):
173 s = stack(8191)
174 r6 = r(6)
175 expected = [
176 mi('gen_load_int', ip.value, 32940, cond=AL),
177 mi('LDR_rr', r6.value, fp.value, ip.value, cond=AL),
178 ]
179 self.mov(s, r6, expected)
180
181 def test_mov_float_imm_to_vfp_reg(self):
182 f = imm_float(3.5)
183 reg = vfp(5)
184 expected = [
185 mi('gen_load_int', ip.value, f.value, cond=AL),
186 mi('VLDR', 5, ip.value, imm=0, cond=AL),
187 ]
188 self.mov(f, reg, expected)
189
190 def test_mov_vfp_reg_to_vfp_reg(self):
191 reg1 = vfp(5)
192 reg2 = vfp(14)
193 expected = [mi('VMOV_cc', reg2.value, reg1.value, cond=AL)]
194 self.mov(reg1, reg2, expected)
195
196 def test_mov_vfp_reg_to_stack(self):
197 reg = vfp(7)
198 s = stack_float(3)
199 expected = [mi('VSTR', reg.value, fp.value, imm=192, cond=AL)]
200 self.mov(reg, s, expected)
201
202 def test_mov_vfp_reg_to_large_stackloc(self):
203 reg = vfp(7)
204 s = stack_float(800)
205 expected = [
206 mi('gen_load_int', ip.value, s.value, cond=AL),
207 mi('ADD_rr', ip.value, fp.value, ip.value, cond=AL),
208 mi('VSTR', reg.value, ip.value, cond=AL),
209 ]
210 self.mov(reg, s, expected)
211
212 def test_mov_stack_to_vfp_reg(self):
213 reg = vfp(7)
214 s = stack_float(3)
215 expected = [mi('VLDR', reg.value, fp.value, imm=192, cond=AL)]
216 self.mov(s, reg, expected)
217
218 def test_mov_big_stackloc_to_vfp_reg(self):
219 reg = vfp(7)
220 s = stack_float(800)
221 expected = [
222 mi('gen_load_int', ip.value, s.value, cond=AL),
223 mi('ADD_rr', ip.value, fp.value, ip.value, cond=AL),
224 mi('VSTR', reg.value, ip.value, cond=AL),
225 ]
226 self.mov(reg, s, expected)
227
228 def test_unsopported_cases(self):
229 py.test.raises(AssertionError,
230 'self.asm.regalloc_mov(imm(1), imm(2))')
231 py.test.raises(AssertionError,
232 'self.asm.regalloc_mov(imm(1), imm_float(2))')
233 py.test.raises(AssertionError,
234 'self.asm.regalloc_mov(imm(1), vfp(2))')
235 py.test.raises(AssertionError,
236 'self.asm.regalloc_mov(imm(1), stack_float(2))')
237 py.test.raises(AssertionError,
238 'self.asm.regalloc_mov(imm_float(1), imm(2))')
239 py.test.raises(AssertionError,
240 'self.asm.regalloc_mov(imm_float(1), imm_float(2))')
241 py.test.raises(AssertionError,
242 'self.asm.regalloc_mov(imm_float(1), r(2))')
243 py.test.raises(AssertionError,
244 'self.asm.regalloc_mov(imm_float(1), stack(2))')
245 py.test.raises(AssertionError,
246 'self.asm.regalloc_mov(r(1), imm(2))')
247 py.test.raises(AssertionError,
248 'self.asm.regalloc_mov(r(1), imm_float(2))')
249 py.test.raises(AssertionError,
250 'self.asm.regalloc_mov(r(1), stack_float(2))')
251 py.test.raises(AssertionError,
252 'self.asm.regalloc_mov(r(1), vfp(2))')
253 py.test.raises(AssertionError,
254 'self.asm.regalloc_mov(stack(1), imm(2))')
255 py.test.raises(AssertionError,
256 'self.asm.regalloc_mov(stack(1), imm_float(2))')
257 py.test.raises(AssertionError,
258 'self.asm.regalloc_mov(stack(1), stack(2))')
259 py.test.raises(AssertionError,
260 'self.asm.regalloc_mov(stack(1), stack_float(2))')
261 py.test.raises(AssertionError,
262 'self.asm.regalloc_mov(stack(1), vfp(2))')
263 py.test.raises(AssertionError,
264 'self.asm.regalloc_mov(stack_float(1), imm(2))')
265 py.test.raises(AssertionError,
266 'self.asm.regalloc_mov(stack_float(1), imm_float(2))')
267 py.test.raises(AssertionError,
268 'self.asm.regalloc_mov(stack_float(1), r(2))')
269 py.test.raises(AssertionError,
270 'self.asm.regalloc_mov(stack_float(1), stack(2))')
271 py.test.raises(AssertionError,
272 'self.asm.regalloc_mov(stack_float(1), stack_float(2))')
273 py.test.raises(AssertionError,
274 'self.asm.regalloc_mov(vfp(1), imm(2))')
275 py.test.raises(AssertionError,
276 'self.asm.regalloc_mov(vfp(1), imm_float(2))')
277 py.test.raises(AssertionError,
278 'self.asm.regalloc_mov(vfp(1), r(2))')
279 py.test.raises(AssertionError,
280 'self.asm.regalloc_mov(vfp(1), stack(2))')
281
282
283 class TestMovFromVFPLoc(BaseMovTest):
284 def mov(self, a, b, c, expected=None):
285 self.asm.mov_from_vfp_loc(a, b, c)
286 self.validate(expected)
287
288 def test_from_vfp(self):
289 vr = vfp(10)
290 r1 = r(1)
291 r2 = r(2)
292 e = [mi('VMOV_rc', r1.value, r2.value, vr.value, cond=AL)]
293 self.mov(vr, r1, r2, e)
294
295 def test_from_vfp_stack(self):
296 s = stack_float(4)
297 r1 = r(1)
298 r2 = r(2)
299 e = [
300 mi('LDR_ri', r1.value, fp.value, imm=s.value, cond=AL),
301 mi('LDR_ri', r2.value, fp.value, imm=s.value + WORD, cond=AL)]
302 self.mov(s, r1, r2, e)
303
304 def test_from_big_vfp_stack(self):
305 s = stack_float(2049)
306 r1 = r(1)
307 r2 = r(2)
308 e = [
309 mi('gen_load_int', ip.value, s.value, cond=AL),
310 mi('LDR_rr', r1.value, fp.value, ip.value, cond=AL),
311 mi('ADD_ri', ip.value, ip.value, imm=WORD, cond=AL),
312 mi('LDR_rr', r2.value, fp.value, ip.value, cond=AL),
313 ]
314 self.mov(s, r1, r2, e)
315
316 def test_from_imm_float(self):
317 i = imm_float(4)
318 r1 = r(1)
319 r2 = r(2)
320 e = [
321 mi('gen_load_int', ip.value, i.value, cond=AL),
322 mi('LDR_ri', r1.value, ip.value, cond=AL),
323 mi('LDR_ri', r2.value, ip.value, imm=4, cond=AL),
324 ]
325 self.mov(i, r1, r2, e)
326
327 def test_unsupported(self):
328 py.test.raises(AssertionError,
329 'self.asm.mov_from_vfp_loc(vfp(1), r(5), r(2))')
330 py.test.raises(AssertionError,
331 'self.asm.mov_from_vfp_loc(stack(1), r(1), r(2))')
332 py.test.raises(AssertionError,
333 'self.asm.mov_from_vfp_loc(imm(1), r(1), r(2))')
334 py.test.raises(AssertionError,
335 'self.asm.mov_from_vfp_loc(r(1), r(1), r(2))')
336
337
338 class TestMoveToVFPLoc(BaseMovTest):
339 def mov(self, r1, r2, vfp, expected):
340 self.asm.mov_to_vfp_loc(r1, r2, vfp)
341 self.validate(expected)
342
343 def mov_to_vfp_reg(self):
344 vr = vfp(10)
345 r1 = r(1)
346 r2 = r(2)
347 e = [mi('VMOV_cr', vr.value, r1.value, r2.value, cond=AL)]
348 self.mov(vr, r1, r2, e)
349
350 def test_to_vfp_stack(self):
351 s = stack_float(4)
352 r1 = r(1)
353 r2 = r(2)
354 e = [
355 mi('STR_ri', r1.value, fp.value, imm=s.value, cond=AL),
356 mi('STR_ri', r2.value, fp.value, imm=s.value + WORD, cond=AL)]
357 self.mov(r1, r2, s, e)
358
359 def test_from_big_vfp_stack(self):
360 s = stack_float(2049)
361 r1 = r(1)
362 r2 = r(2)
363 e = [
364 mi('gen_load_int', ip.value, s.value, cond=AL),
365 mi('STR_rr', r1.value, fp.value, ip.value, cond=AL),
366 mi('ADD_ri', ip.value, ip.value, imm=4, cond=AL),
367 mi('STR_rr', r2.value, fp.value, ip.value, cond=AL),
368 ]
369 self.mov(r1, r2, s, e)
370
371 def unsupported(self):
372 py.test.raises(AssertionError,
373 'self.asm.mov_from_vfp_loc(r(5), r(2), vfp(4))')
374 py.test.raises(AssertionError,
375 'self.asm.mov_from_vfp_loc(r(1), r(2), stack(2))')
376 py.test.raises(AssertionError,
377 'self.asm.mov_from_vfp_loc(r(1), r(2), imm(2))')
378 py.test.raises(AssertionError,
379 'self.asm.mov_from_vfp_loc(r(1), r(2), imm_float(2))')
380 py.test.raises(AssertionError,
381 'self.asm.mov_from_vfp_loc(r(1), r(1), r(2))')
382
383
384 class TestRegallocPush(BaseMovTest):
385 def push(self, v, e):
386 self.asm.regalloc_push(v)
387 self.validate(e)
388
389 def test_push_imm(self):
390 i = imm(12)
391 e = [mi('gen_load_int', ip.value, 12, cond=AL),
392 mi('PUSH', [ip.value], cond=AL)]
393 self.push(i, e)
394
395 def test_push_reg(self):
396 r7 = r(7)
397 e = [mi('PUSH', [r7.value], cond=AL)]
398 self.push(r7, e)
399
400 def test_push_imm_float(self):
401 f = imm_float(7)
402 e = [
403 mi('gen_load_int', ip.value, 7, cond=AL),
404 mi('VLDR', vfp_ip.value, ip.value, imm=0, cond=AL),
405 mi('VPUSH', [vfp_ip.value], cond=AL)
406 ]
407 self.push(f, e)
408
409 def test_push_stack(self):
410 s = stack(7)
411 e = [mi('LDR_ri', ip.value, fp.value, imm=s.value, cond=AL),
412 mi('PUSH', [ip.value], cond=AL)
413 ]
414 self.push(s, e)
415
416 def test_push_big_stack(self):
417 s = stack(1025)
418 e = [
419 mi('gen_load_int', lr.value, s.value, cond=AL),
420 mi('LDR_rr', ip.value, fp.value, lr.value, cond=AL),
421 mi('PUSH', [ip.value], cond=AL)
422 ]
423 self.push(s, e)
424
425 def test_push_vfp_reg(self):
426 v1 = vfp(1)
427 e = [mi('VPUSH', [v1.value], cond=AL)]
428 self.push(v1, e)
429
430 def test_push_stack_float(self):
431 sf = stack_float(4)
432 e = [
433 mi('VLDR', vfp_ip.value, fp.value, imm=196, cond=AL),
434 mi('VPUSH', [vfp_ip.value], cond=AL),
435 ]
436 self.push(sf, e)
437
438 def test_push_large_stackfloat(self):
439 sf = stack_float(1000)
440 e = [
441 mi('gen_load_int', ip.value, sf.value, cond=AL),
442 mi('ADD_rr', ip.value, fp.value, ip.value, cond=AL),
443 mi('VLDR', vfp_ip.value, ip.value, cond=AL),
444 mi('VPUSH', [vfp_ip.value], cond=AL),
445 ]
446 self.push(sf, e)
447
448
449 class TestRegallocPop(BaseMovTest):
450 def pop(self, loc, e):
451 self.asm.regalloc_pop(loc)
452 self.validate(e)
453
454 def test_pop_reg(self):
455 r1 = r(1)
456 e = [mi('POP', [r1.value], cond=AL)]
457 self.pop(r1, e)
458
459 def test_pop_vfp_reg(self):
460 vr1 = vfp(1)
461 e = [mi('VPOP', [vr1.value], cond=AL)]
462 self.pop(vr1, e)
463
464 def test_pop_stackloc(self):
465 s = stack(12)
466 e = [
467 mi('POP', [ip.value], cond=AL),
468 mi('STR_ri', ip.value, fp.value, imm=s.value, cond=AL)]
469 self.pop(s, e)
470
471 def test_pop_big_stackloc(self):
472 s = stack(1200)
473 e = [
474 mi('POP', [ip.value], cond=AL),
475 mi('gen_load_int', lr.value, s.value, cond=AL),
476 mi('STR_rr', ip.value, fp.value, lr.value, cond=AL),
477 ]
478 self.pop(s, e)
479
480 def test_pop_float_stackloc(self):
481 s = stack_float(12)
482 e = [
483 mi('VPOP', [vfp_ip.value], cond=AL),
484 mi('VSTR', vfp_ip.value, fp.value, imm=s.value, cond=AL),
485 ]
486 self.pop(s, e)
487
488 def test_pop_big_float_stackloc(self):
489 s = stack_float(1200)
490 e = [
491 mi('VPOP', [vfp_ip.value], cond=AL),
492 mi('gen_load_int', ip.value, s.value, cond=AL),
493 mi('ADD_rr', ip.value, fp.value, ip.value, cond=AL),
494 mi('VSTR', vfp_ip.value, ip.value, cond=AL),
495 ]
496 self.pop(s, e)
497
498 def test_unsupported(self):
499 py.test.raises(AssertionError, 'self.asm.regalloc_pop(imm(1))')
500 py.test.raises(AssertionError, 'self.asm.regalloc_pop(imm_float(1))')
501
502 class TestRawStackLocs(BaseMovTest):
503 def test_unsupported(self):
504 py.test.raises(AssertionError, 'self.asm.regalloc_mov(raw_stack(0), imm(1))')
505 py.test.raises(AssertionError, 'self.asm.regalloc_mov(raw_stack(0), imm_float(1))')
506 py.test.raises(AssertionError, 'self.asm.regalloc_mov(raw_stack(0), vfp(1))')
507 py.test.raises(AssertionError, 'self.asm.regalloc_mov(raw_stack(0), stack(1))')
508 py.test.raises(AssertionError, 'self.asm.regalloc_mov(raw_stack(0), stack_float(1))')
509
510 py.test.raises(AssertionError, 'self.asm.regalloc_mov(imm_float(1), raw_stack(1))')
511 py.test.raises(AssertionError, 'self.asm.regalloc_mov(imm(1), raw_stack_float(1))')
512
513 py.test.raises(AssertionError, 'self.asm.regalloc_mov(vfp(1), raw_stack(1))')
514 py.test.raises(AssertionError, 'self.asm.regalloc_mov(r(1), raw_stack_float(1))')
515
516 py.test.raises(AssertionError, 'self.asm.regalloc_mov(stack_float(1), raw_stack(1))')
517 py.test.raises(AssertionError, 'self.asm.regalloc_mov(stack(1), raw_stack_float(1))')
518
519 def test_from_imm(self):
520 s = raw_stack(1024)
521 i = imm(999)
522 e = [
523 mi('gen_load_int', lr.value, i.value, cond=AL),
524 mi('gen_load_int', ip.value, s.value, cond=AL),
525 mi('STR_rr', lr.value, sp.value, ip.value, cond=AL),
526 ]
527 self.mov(i, s, e)
528
529 def test_from_vfp_imm(self):
530 s = raw_stack_float(1024)
531 i = imm_float(999)
532 e = [
533 mi('gen_load_int', ip.value, i.value, cond=AL),
534 mi('VLDR', vfp_ip.value, ip.value, cond=AL, imm=0),
535 mi('gen_load_int', ip.value, s.value, cond=AL),
536 mi('ADD_rr', ip.value, sp.value, ip.value, cond=AL),
537 mi('VSTR', vfp_ip.value, ip.value, cond=AL),
538 ]
539 self.mov(i, s, e)
540
541 def test_from_reg(self):
542 s = raw_stack(1024)
543 reg = r(10)
544 e = [mi('gen_load_int', ip.value, s.value, cond=AL),
545 mi('STR_rr', reg.value, sp.value, ip.value, cond=AL),
546 ]
547 self.mov(reg, s, e)
548
549 def test_from_vfp_reg(self):
550 s = raw_stack_float(1024)
551 reg = vfp(10)
552 e = [mi('gen_load_int', ip.value, s.value, cond=AL),
553 mi('ADD_rr', ip.value, sp.value, ip.value, cond=AL),
554 mi('VSTR', reg.value, ip.value, cond=AL),
555 ]
556 self.mov(reg, s, e)
557
558 def test_from_stack(self):
559 s = raw_stack(1024)
560 reg = stack(10)
561 e = [mi('LDR_ri', ip.value, fp.value, imm=216, cond=AL),
562 mi('gen_load_int', lr.value, s.value, cond=AL),
563 mi('STR_rr', ip.value, sp.value, lr.value, cond=AL),
564 ]
565 self.mov(reg, s, e)
566
567 def test_from_vfp_stack(self):
568 s = raw_stack_float(1024)
569 reg = stack_float(10)
570 e = [mi('VLDR', vfp_ip.value, fp.value, imm=220, cond=AL),
571 mi('gen_load_int', ip.value, s.value, cond=AL),
572 mi('ADD_rr', ip.value, sp.value, ip.value, cond=AL),
573 mi('VSTR', vfp_ip.value, ip.value, cond=AL),
574 ]
575 self.mov(reg, s, e)