Debian Codegraph @

Lib/test/test_generated_cases.py

raw
1 import contextlib
2 import os
3 import re
4 import sys
5 import tempfile
6 import unittest
7
8 from io import StringIO
9 from test import support
10 from test import test_tools
11
12
13 def skip_if_different_mount_drives():
14 if sys.platform != "win32":
15 return
16 ROOT = os.path.dirname(os.path.dirname(__file__))
17 root_drive = os.path.splitroot(ROOT)[0]
18 cwd_drive = os.path.splitroot(os.getcwd())[0]
19 if root_drive != cwd_drive:
20 # May raise ValueError if ROOT and the current working
21 # different have different mount drives (on Windows).
22 raise unittest.SkipTest(
23 f"the current working directory and the Python source code "
24 f"directory have different mount drives "
25 f"({cwd_drive} and {root_drive})"
26 )
27
28
29 skip_if_different_mount_drives()
30
31
32 test_tools.skip_if_missing("cases_generator")
33 with test_tools.imports_under_tool("cases_generator"):
34 from analyzer import analyze_forest, StackItem
35 from cwriter import CWriter
36 import parser
37 from stack import Local, Stack
38 import tier1_generator
39 import opcode_metadata_generator
40 import optimizer_generator
41
42
43 def handle_stderr():
44 if support.verbose > 1:
45 return contextlib.nullcontext()
46 else:
47 return support.captured_stderr()
48
49
50 def parse_src(src):
51 p = parser.Parser(src, "test.c")
52 nodes = []
53 while node := p.definition():
54 nodes.append(node)
55 return nodes
56
57
58 class TestEffects(unittest.TestCase):
59 def test_effect_sizes(self):
60 stack = Stack()
61 inputs = [
62 x := StackItem("x", None, "1"),
63 y := StackItem("y", None, "oparg"),
64 z := StackItem("z", None, "oparg*2"),
65 ]
66 outputs = [
67 StackItem("x", None, "1"),
68 StackItem("b", None, "oparg*4"),
69 StackItem("c", None, "1"),
70 ]
71 null = CWriter.null()
72 stack.pop(z, null)
73 stack.pop(y, null)
74 stack.pop(x, null)
75 for out in outputs:
76 stack.push(Local.undefined(out))
77 self.assertEqual(stack.base_offset.to_c(), "-1 - oparg - oparg*2")
78 self.assertEqual(stack.physical_sp.to_c(), "0")
79 self.assertEqual(stack.logical_sp.to_c(), "1 - oparg - oparg*2 + oparg*4")
80
81
82 class TestGeneratedCases(unittest.TestCase):
83 def setUp(self) -> None:
84 super().setUp()
85 self.maxDiff = None
86
87 self.temp_dir = tempfile.gettempdir()
88 self.temp_input_filename = os.path.join(self.temp_dir, "input.txt")
89 self.temp_output_filename = os.path.join(self.temp_dir, "output.txt")
90 self.temp_metadata_filename = os.path.join(self.temp_dir, "metadata.txt")
91 self.temp_pymetadata_filename = os.path.join(self.temp_dir, "pymetadata.txt")
92 self.temp_executor_filename = os.path.join(self.temp_dir, "executor.txt")
93
94 def tearDown(self) -> None:
95 for filename in [
96 self.temp_input_filename,
97 self.temp_output_filename,
98 self.temp_metadata_filename,
99 self.temp_pymetadata_filename,
100 self.temp_executor_filename,
101 ]:
102 try:
103 os.remove(filename)
104 except:
105 pass
106 super().tearDown()
107
108 def run_cases_test(self, input: str, expected: str):
109 with open(self.temp_input_filename, "w+") as temp_input:
110 temp_input.write(parser.BEGIN_MARKER)
111 temp_input.write(input)
112 temp_input.write(parser.END_MARKER)
113 temp_input.flush()
114
115 with handle_stderr():
116 tier1_generator.generate_tier1_from_files(
117 [self.temp_input_filename], self.temp_output_filename, False
118 )
119
120 with open(self.temp_output_filename) as temp_output:
121 lines = temp_output.read()
122 _, rest = lines.split(tier1_generator.INSTRUCTION_START_MARKER)
123 instructions, labels_with_prelude_and_postlude = rest.split(tier1_generator.INSTRUCTION_END_MARKER)
124 _, labels_with_postlude = labels_with_prelude_and_postlude.split(tier1_generator.LABEL_START_MARKER)
125 labels, _ = labels_with_postlude.split(tier1_generator.LABEL_END_MARKER)
126 actual = instructions.strip() + "\n\n " + labels.strip()
127
128 self.assertEqual(actual.strip(), expected.strip())
129
130 def test_inst_no_args(self):
131 input = """
132 inst(OP, (--)) {
133 SPAM();
134 }
135 """
136 output = """
137 TARGET(OP) {
138 #if Py_TAIL_CALL_INTERP
139 int opcode = OP;
140 (void)(opcode);
141 #endif
142 frame->instr_ptr = next_instr;
143 next_instr += 1;
144 INSTRUCTION_STATS(OP);
145 SPAM();
146 DISPATCH();
147 }
148 """
149 self.run_cases_test(input, output)
150
151 def test_inst_one_pop(self):
152 input = """
153 inst(OP, (value --)) {
154 SPAM(value);
155 DEAD(value);
156 }
157 """
158 output = """
159 TARGET(OP) {
160 #if Py_TAIL_CALL_INTERP
161 int opcode = OP;
162 (void)(opcode);
163 #endif
164 frame->instr_ptr = next_instr;
165 next_instr += 1;
166 INSTRUCTION_STATS(OP);
167 _PyStackRef value;
168 value = stack_pointer[-1];
169 SPAM(value);
170 stack_pointer += -1;
171 assert(WITHIN_STACK_BOUNDS());
172 DISPATCH();
173 }
174 """
175 self.run_cases_test(input, output)
176
177 def test_inst_one_push(self):
178 input = """
179 inst(OP, (-- res)) {
180 res = SPAM();
181 }
182 """
183 output = """
184 TARGET(OP) {
185 #if Py_TAIL_CALL_INTERP
186 int opcode = OP;
187 (void)(opcode);
188 #endif
189 frame->instr_ptr = next_instr;
190 next_instr += 1;
191 INSTRUCTION_STATS(OP);
192 _PyStackRef res;
193 res = SPAM();
194 stack_pointer[0] = res;
195 stack_pointer += 1;
196 assert(WITHIN_STACK_BOUNDS());
197 DISPATCH();
198 }
199 """
200 self.run_cases_test(input, output)
201
202 def test_inst_one_push_one_pop(self):
203 input = """
204 inst(OP, (value -- res)) {
205 res = SPAM(value);
206 DEAD(value);
207 }
208 """
209 output = """
210 TARGET(OP) {
211 #if Py_TAIL_CALL_INTERP
212 int opcode = OP;
213 (void)(opcode);
214 #endif
215 frame->instr_ptr = next_instr;
216 next_instr += 1;
217 INSTRUCTION_STATS(OP);
218 _PyStackRef value;
219 _PyStackRef res;
220 value = stack_pointer[-1];
221 res = SPAM(value);
222 stack_pointer[-1] = res;
223 DISPATCH();
224 }
225 """
226 self.run_cases_test(input, output)
227
228 def test_binary_op(self):
229 input = """
230 inst(OP, (left, right -- res)) {
231 res = SPAM(left, right);
232 INPUTS_DEAD();
233
234 }
235 """
236 output = """
237 TARGET(OP) {
238 #if Py_TAIL_CALL_INTERP
239 int opcode = OP;
240 (void)(opcode);
241 #endif
242 frame->instr_ptr = next_instr;
243 next_instr += 1;
244 INSTRUCTION_STATS(OP);
245 _PyStackRef left;
246 _PyStackRef right;
247 _PyStackRef res;
248 right = stack_pointer[-1];
249 left = stack_pointer[-2];
250 res = SPAM(left, right);
251 stack_pointer[-2] = res;
252 stack_pointer += -1;
253 assert(WITHIN_STACK_BOUNDS());
254 DISPATCH();
255 }
256 """
257 self.run_cases_test(input, output)
258
259 def test_overlap(self):
260 input = """
261 inst(OP, (left, right -- left, result)) {
262 result = SPAM(left, right);
263 INPUTS_DEAD();
264 }
265 """
266 output = """
267 TARGET(OP) {
268 #if Py_TAIL_CALL_INTERP
269 int opcode = OP;
270 (void)(opcode);
271 #endif
272 frame->instr_ptr = next_instr;
273 next_instr += 1;
274 INSTRUCTION_STATS(OP);
275 _PyStackRef left;
276 _PyStackRef right;
277 _PyStackRef result;
278 right = stack_pointer[-1];
279 left = stack_pointer[-2];
280 result = SPAM(left, right);
281 stack_pointer[-1] = result;
282 DISPATCH();
283 }
284 """
285 self.run_cases_test(input, output)
286
287 def test_predictions(self):
288 input = """
289 inst(OP1, (arg -- res)) {
290 DEAD(arg);
291 res = Py_None;
292 }
293 inst(OP3, (arg -- res)) {
294 DEAD(arg);
295 DEOPT_IF(xxx);
296 res = Py_None;
297 }
298 family(OP1, INLINE_CACHE_ENTRIES_OP1) = { OP3 };
299 """
300 output = """
301 TARGET(OP1) {
302 #if Py_TAIL_CALL_INTERP
303 int opcode = OP1;
304 (void)(opcode);
305 #endif
306 frame->instr_ptr = next_instr;
307 next_instr += 1;
308 INSTRUCTION_STATS(OP1);
309 PREDICTED_OP1:;
310 _PyStackRef arg;
311 _PyStackRef res;
312 arg = stack_pointer[-1];
313 res = Py_None;
314 stack_pointer[-1] = res;
315 DISPATCH();
316 }
317
318 TARGET(OP3) {
319 #if Py_TAIL_CALL_INTERP
320 int opcode = OP3;
321 (void)(opcode);
322 #endif
323 _Py_CODEUNIT* const this_instr = next_instr;
324 (void)this_instr;
325 frame->instr_ptr = next_instr;
326 next_instr += 1;
327 INSTRUCTION_STATS(OP3);
328 static_assert(INLINE_CACHE_ENTRIES_OP1 == 0, "incorrect cache size");
329 _PyStackRef arg;
330 _PyStackRef res;
331 arg = stack_pointer[-1];
332 if (xxx) {
333 UPDATE_MISS_STATS(OP1);
334 assert(_PyOpcode_Deopt[opcode] == (OP1));
335 JUMP_TO_PREDICTED(OP1);
336 }
337 res = Py_None;
338 stack_pointer[-1] = res;
339 DISPATCH();
340 }
341 """
342 self.run_cases_test(input, output)
343
344 def test_sync_sp(self):
345 input = """
346 inst(A, (arg -- res)) {
347 DEAD(arg);
348 SYNC_SP();
349 escaping_call();
350 res = Py_None;
351 }
352 inst(B, (arg -- res)) {
353 DEAD(arg);
354 res = Py_None;
355 SYNC_SP();
356 escaping_call();
357 }
358 """
359 output = """
360 TARGET(A) {
361 #if Py_TAIL_CALL_INTERP
362 int opcode = A;
363 (void)(opcode);
364 #endif
365 frame->instr_ptr = next_instr;
366 next_instr += 1;
367 INSTRUCTION_STATS(A);
368 _PyStackRef arg;
369 _PyStackRef res;
370 arg = stack_pointer[-1];
371 stack_pointer += -1;
372 assert(WITHIN_STACK_BOUNDS());
373 _PyFrame_SetStackPointer(frame, stack_pointer);
374 escaping_call();
375 stack_pointer = _PyFrame_GetStackPointer(frame);
376 res = Py_None;
377 stack_pointer[0] = res;
378 stack_pointer += 1;
379 assert(WITHIN_STACK_BOUNDS());
380 DISPATCH();
381 }
382
383 TARGET(B) {
384 #if Py_TAIL_CALL_INTERP
385 int opcode = B;
386 (void)(opcode);
387 #endif
388 frame->instr_ptr = next_instr;
389 next_instr += 1;
390 INSTRUCTION_STATS(B);
391 _PyStackRef arg;
392 _PyStackRef res;
393 arg = stack_pointer[-1];
394 res = Py_None;
395 stack_pointer[-1] = res;
396 _PyFrame_SetStackPointer(frame, stack_pointer);
397 escaping_call();
398 stack_pointer = _PyFrame_GetStackPointer(frame);
399 DISPATCH();
400 }
401 """
402 self.run_cases_test(input, output)
403
404
405 def test_pep7_condition(self):
406 input = """
407 inst(OP, (arg1 -- out)) {
408 if (arg1)
409 out = 0;
410 else {
411 out = 1;
412 }
413 }
414 """
415 output = ""
416 with self.assertRaises(SyntaxError):
417 self.run_cases_test(input, output)
418
419 def test_error_if_plain(self):
420 input = """
421 inst(OP, (--)) {
422 ERROR_IF(cond);
423 }
424 """
425 output = """
426 TARGET(OP) {
427 #if Py_TAIL_CALL_INTERP
428 int opcode = OP;
429 (void)(opcode);
430 #endif
431 frame->instr_ptr = next_instr;
432 next_instr += 1;
433 INSTRUCTION_STATS(OP);
434 if (cond) {
435 JUMP_TO_LABEL(error);
436 }
437 DISPATCH();
438 }
439 """
440 self.run_cases_test(input, output)
441
442 def test_error_if_plain_with_comment(self):
443 input = """
444 inst(OP, (--)) {
445 ERROR_IF(cond); // Comment is ok
446 }
447 """
448 output = """
449 TARGET(OP) {
450 #if Py_TAIL_CALL_INTERP
451 int opcode = OP;
452 (void)(opcode);
453 #endif
454 frame->instr_ptr = next_instr;
455 next_instr += 1;
456 INSTRUCTION_STATS(OP);
457 if (cond) {
458 JUMP_TO_LABEL(error);
459 }
460 DISPATCH();
461 }
462 """
463 self.run_cases_test(input, output)
464
465 def test_error_if_pop(self):
466 input = """
467 inst(OP, (left, right -- res)) {
468 SPAM(left, right);
469 INPUTS_DEAD();
470 ERROR_IF(cond);
471 res = 0;
472 }
473 """
474 output = """
475 TARGET(OP) {
476 #if Py_TAIL_CALL_INTERP
477 int opcode = OP;
478 (void)(opcode);
479 #endif
480 frame->instr_ptr = next_instr;
481 next_instr += 1;
482 INSTRUCTION_STATS(OP);
483 _PyStackRef left;
484 _PyStackRef right;
485 _PyStackRef res;
486 right = stack_pointer[-1];
487 left = stack_pointer[-2];
488 SPAM(left, right);
489 if (cond) {
490 JUMP_TO_LABEL(pop_2_error);
491 }
492 res = 0;
493 stack_pointer[-2] = res;
494 stack_pointer += -1;
495 assert(WITHIN_STACK_BOUNDS());
496 DISPATCH();
497 }
498 """
499 self.run_cases_test(input, output)
500
501 def test_error_if_pop_with_result(self):
502 input = """
503 inst(OP, (left, right -- res)) {
504 res = SPAM(left, right);
505 INPUTS_DEAD();
506 ERROR_IF(cond);
507 }
508 """
509 output = """
510 TARGET(OP) {
511 #if Py_TAIL_CALL_INTERP
512 int opcode = OP;
513 (void)(opcode);
514 #endif
515 frame->instr_ptr = next_instr;
516 next_instr += 1;
517 INSTRUCTION_STATS(OP);
518 _PyStackRef left;
519 _PyStackRef right;
520 _PyStackRef res;
521 right = stack_pointer[-1];
522 left = stack_pointer[-2];
523 res = SPAM(left, right);
524 if (cond) {
525 JUMP_TO_LABEL(pop_2_error);
526 }
527 stack_pointer[-2] = res;
528 stack_pointer += -1;
529 assert(WITHIN_STACK_BOUNDS());
530 DISPATCH();
531 }
532 """
533 self.run_cases_test(input, output)
534
535 def test_cache_effect(self):
536 input = """
537 inst(OP, (counter/1, extra/2, value --)) {
538 DEAD(value);
539 }
540 """
541 output = """
542 TARGET(OP) {
543 #if Py_TAIL_CALL_INTERP
544 int opcode = OP;
545 (void)(opcode);
546 #endif
547 _Py_CODEUNIT* const this_instr = next_instr;
548 (void)this_instr;
549 frame->instr_ptr = next_instr;
550 next_instr += 4;
551 INSTRUCTION_STATS(OP);
552 _PyStackRef value;
553 value = stack_pointer[-1];
554 uint16_t counter = read_u16(&this_instr[1].cache);
555 (void)counter;
556 uint32_t extra = read_u32(&this_instr[2].cache);
557 (void)extra;
558 stack_pointer += -1;
559 assert(WITHIN_STACK_BOUNDS());
560 DISPATCH();
561 }
562 """
563 self.run_cases_test(input, output)
564
565 def test_suppress_dispatch(self):
566 input = """
567 label(somewhere) {
568 }
569
570 inst(OP, (--)) {
571 goto somewhere;
572 }
573 """
574 output = """
575 TARGET(OP) {
576 #if Py_TAIL_CALL_INTERP
577 int opcode = OP;
578 (void)(opcode);
579 #endif
580 frame->instr_ptr = next_instr;
581 next_instr += 1;
582 INSTRUCTION_STATS(OP);
583 JUMP_TO_LABEL(somewhere);
584 }
585
586 LABEL(somewhere)
587 {
588 }
589 """
590 self.run_cases_test(input, output)
591
592 def test_macro_instruction(self):
593 input = """
594 inst(OP1, (counter/1, left, right -- left, right)) {
595 op1(left, right);
596 }
597 op(OP2, (extra/2, arg2, left, right -- res)) {
598 res = op2(arg2, left, right);
599 INPUTS_DEAD();
600 }
601 macro(OP) = OP1 + cache/2 + OP2;
602 inst(OP3, (unused/5, arg2, left, right -- res)) {
603 res = op3(arg2, left, right);
604 INPUTS_DEAD();
605 }
606 family(OP, INLINE_CACHE_ENTRIES_OP) = { OP3 };
607 """
608 output = """
609 TARGET(OP) {
610 #if Py_TAIL_CALL_INTERP
611 int opcode = OP;
612 (void)(opcode);
613 #endif
614 frame->instr_ptr = next_instr;
615 next_instr += 6;
616 INSTRUCTION_STATS(OP);
617 PREDICTED_OP:;
618 _Py_CODEUNIT* const this_instr = next_instr - 6;
619 (void)this_instr;
620 _PyStackRef left;
621 _PyStackRef right;
622 _PyStackRef arg2;
623 _PyStackRef res;
624 // _OP1
625 {
626 right = stack_pointer[-1];
627 left = stack_pointer[-2];
628 uint16_t counter = read_u16(&this_instr[1].cache);
629 (void)counter;
630 _PyFrame_SetStackPointer(frame, stack_pointer);
631 op1(left, right);
632 stack_pointer = _PyFrame_GetStackPointer(frame);
633 }
634 /* Skip 2 cache entries */
635 // OP2
636 {
637 arg2 = stack_pointer[-3];
638 uint32_t extra = read_u32(&this_instr[4].cache);
639 (void)extra;
640 _PyFrame_SetStackPointer(frame, stack_pointer);
641 res = op2(arg2, left, right);
642 stack_pointer = _PyFrame_GetStackPointer(frame);
643 }
644 stack_pointer[-3] = res;
645 stack_pointer += -2;
646 assert(WITHIN_STACK_BOUNDS());
647 DISPATCH();
648 }
649
650 TARGET(OP1) {
651 #if Py_TAIL_CALL_INTERP
652 int opcode = OP1;
653 (void)(opcode);
654 #endif
655 _Py_CODEUNIT* const this_instr = next_instr;
656 (void)this_instr;
657 frame->instr_ptr = next_instr;
658 next_instr += 2;
659 INSTRUCTION_STATS(OP1);
660 _PyStackRef left;
661 _PyStackRef right;
662 right = stack_pointer[-1];
663 left = stack_pointer[-2];
664 uint16_t counter = read_u16(&this_instr[1].cache);
665 (void)counter;
666 _PyFrame_SetStackPointer(frame, stack_pointer);
667 op1(left, right);
668 stack_pointer = _PyFrame_GetStackPointer(frame);
669 DISPATCH();
670 }
671
672 TARGET(OP3) {
673 #if Py_TAIL_CALL_INTERP
674 int opcode = OP3;
675 (void)(opcode);
676 #endif
677 frame->instr_ptr = next_instr;
678 next_instr += 6;
679 INSTRUCTION_STATS(OP3);
680 static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
681 _PyStackRef arg2;
682 _PyStackRef left;
683 _PyStackRef right;
684 _PyStackRef res;
685 /* Skip 5 cache entries */
686 right = stack_pointer[-1];
687 left = stack_pointer[-2];
688 arg2 = stack_pointer[-3];
689 _PyFrame_SetStackPointer(frame, stack_pointer);
690 res = op3(arg2, left, right);
691 stack_pointer = _PyFrame_GetStackPointer(frame);
692 stack_pointer[-3] = res;
693 stack_pointer += -2;
694 assert(WITHIN_STACK_BOUNDS());
695 DISPATCH();
696 }
697 """
698 self.run_cases_test(input, output)
699
700 def test_unused_caches(self):
701 input = """
702 inst(OP, (unused/1, unused/2 --)) {
703 body;
704 }
705 """
706 output = """
707 TARGET(OP) {
708 #if Py_TAIL_CALL_INTERP
709 int opcode = OP;
710 (void)(opcode);
711 #endif
712 frame->instr_ptr = next_instr;
713 next_instr += 4;
714 INSTRUCTION_STATS(OP);
715 /* Skip 1 cache entry */
716 /* Skip 2 cache entries */
717 body;
718 DISPATCH();
719 }
720 """
721 self.run_cases_test(input, output)
722
723 def test_pseudo_instruction_no_flags(self):
724 input = """
725 pseudo(OP, (in -- out1, out2)) = {
726 OP1,
727 };
728
729 inst(OP1, (--)) {
730 }
731 """
732 output = """
733 TARGET(OP1) {
734 #if Py_TAIL_CALL_INTERP
735 int opcode = OP1;
736 (void)(opcode);
737 #endif
738 frame->instr_ptr = next_instr;
739 next_instr += 1;
740 INSTRUCTION_STATS(OP1);
741 DISPATCH();
742 }
743 """
744 self.run_cases_test(input, output)
745
746 def test_pseudo_instruction_with_flags(self):
747 input = """
748 pseudo(OP, (in1, in2 --), (HAS_ARG, HAS_JUMP)) = {
749 OP1,
750 };
751
752 inst(OP1, (--)) {
753 }
754 """
755 output = """
756 TARGET(OP1) {
757 #if Py_TAIL_CALL_INTERP
758 int opcode = OP1;
759 (void)(opcode);
760 #endif
761 frame->instr_ptr = next_instr;
762 next_instr += 1;
763 INSTRUCTION_STATS(OP1);
764 DISPATCH();
765 }
766 """
767 self.run_cases_test(input, output)
768
769 def test_pseudo_instruction_as_sequence(self):
770 input = """
771 pseudo(OP, (in -- out1, out2)) = [
772 OP1, OP2
773 ];
774
775 inst(OP1, (--)) {
776 }
777
778 inst(OP2, (--)) {
779 }
780 """
781 output = """
782 TARGET(OP1) {
783 #if Py_TAIL_CALL_INTERP
784 int opcode = OP1;
785 (void)(opcode);
786 #endif
787 frame->instr_ptr = next_instr;
788 next_instr += 1;
789 INSTRUCTION_STATS(OP1);
790 DISPATCH();
791 }
792
793 TARGET(OP2) {
794 #if Py_TAIL_CALL_INTERP
795 int opcode = OP2;
796 (void)(opcode);
797 #endif
798 frame->instr_ptr = next_instr;
799 next_instr += 1;
800 INSTRUCTION_STATS(OP2);
801 DISPATCH();
802 }
803 """
804 self.run_cases_test(input, output)
805
806
807 def test_array_input(self):
808 input = """
809 inst(OP, (below, values[oparg*2], above --)) {
810 SPAM(values, oparg);
811 DEAD(below);
812 DEAD(values);
813 DEAD(above);
814 }
815 """
816 output = """
817 TARGET(OP) {
818 #if Py_TAIL_CALL_INTERP
819 int opcode = OP;
820 (void)(opcode);
821 #endif
822 frame->instr_ptr = next_instr;
823 next_instr += 1;
824 INSTRUCTION_STATS(OP);
825 _PyStackRef below;
826 _PyStackRef *values;
827 _PyStackRef above;
828 above = stack_pointer[-1];
829 values = &stack_pointer[-1 - oparg*2];
830 below = stack_pointer[-2 - oparg*2];
831 SPAM(values, oparg);
832 stack_pointer += -2 - oparg*2;
833 assert(WITHIN_STACK_BOUNDS());
834 DISPATCH();
835 }
836 """
837 self.run_cases_test(input, output)
838
839 def test_array_output(self):
840 input = """
841 inst(OP, (unused, unused -- below, values[oparg*3], above)) {
842 SPAM(values, oparg);
843 below = 0;
844 above = 0;
845 }
846 """
847 output = """
848 TARGET(OP) {
849 #if Py_TAIL_CALL_INTERP
850 int opcode = OP;
851 (void)(opcode);
852 #endif
853 frame->instr_ptr = next_instr;
854 next_instr += 1;
855 INSTRUCTION_STATS(OP);
856 _PyStackRef below;
857 _PyStackRef *values;
858 _PyStackRef above;
859 values = &stack_pointer[-1];
860 SPAM(values, oparg);
861 below = 0;
862 above = 0;
863 stack_pointer[-2] = below;
864 stack_pointer[-1 + oparg*3] = above;
865 stack_pointer += oparg*3;
866 assert(WITHIN_STACK_BOUNDS());
867 DISPATCH();
868 }
869 """
870 self.run_cases_test(input, output)
871
872 def test_array_input_output(self):
873 input = """
874 inst(OP, (values[oparg] -- values[oparg], above)) {
875 SPAM(values, oparg);
876 above = 0;
877 }
878 """
879 output = """
880 TARGET(OP) {
881 #if Py_TAIL_CALL_INTERP
882 int opcode = OP;
883 (void)(opcode);
884 #endif
885 frame->instr_ptr = next_instr;
886 next_instr += 1;
887 INSTRUCTION_STATS(OP);
888 _PyStackRef *values;
889 _PyStackRef above;
890 values = &stack_pointer[-oparg];
891 SPAM(values, oparg);
892 above = 0;
893 stack_pointer[0] = above;
894 stack_pointer += 1;
895 assert(WITHIN_STACK_BOUNDS());
896 DISPATCH();
897 }
898 """
899 self.run_cases_test(input, output)
900
901 def test_array_error_if(self):
902 input = """
903 inst(OP, (extra, values[oparg] --)) {
904 DEAD(extra);
905 DEAD(values);
906 ERROR_IF(oparg == 0);
907 }
908 """
909 output = """
910 TARGET(OP) {
911 #if Py_TAIL_CALL_INTERP
912 int opcode = OP;
913 (void)(opcode);
914 #endif
915 frame->instr_ptr = next_instr;
916 next_instr += 1;
917 INSTRUCTION_STATS(OP);
918 _PyStackRef extra;
919 _PyStackRef *values;
920 values = &stack_pointer[-oparg];
921 extra = stack_pointer[-1 - oparg];
922 if (oparg == 0) {
923 stack_pointer += -1 - oparg;
924 assert(WITHIN_STACK_BOUNDS());
925 JUMP_TO_LABEL(error);
926 }
927 stack_pointer += -1 - oparg;
928 assert(WITHIN_STACK_BOUNDS());
929 DISPATCH();
930 }
931 """
932 self.run_cases_test(input, output)
933
934 def test_macro_push_push(self):
935 input = """
936 op(A, (-- val1)) {
937 val1 = SPAM();
938 }
939 op(B, (-- val2)) {
940 val2 = SPAM();
941 }
942 macro(M) = A + B;
943 """
944 output = """
945 TARGET(M) {
946 #if Py_TAIL_CALL_INTERP
947 int opcode = M;
948 (void)(opcode);
949 #endif
950 frame->instr_ptr = next_instr;
951 next_instr += 1;
952 INSTRUCTION_STATS(M);
953 _PyStackRef val1;
954 _PyStackRef val2;
955 // A
956 {
957 val1 = SPAM();
958 }
959 // B
960 {
961 val2 = SPAM();
962 }
963 stack_pointer[0] = val1;
964 stack_pointer[1] = val2;
965 stack_pointer += 2;
966 assert(WITHIN_STACK_BOUNDS());
967 DISPATCH();
968 }
969 """
970 self.run_cases_test(input, output)
971
972 def test_override_inst(self):
973 input = """
974 inst(OP, (--)) {
975 spam;
976 }
977 override inst(OP, (--)) {
978 ham;
979 }
980 """
981 output = """
982 TARGET(OP) {
983 #if Py_TAIL_CALL_INTERP
984 int opcode = OP;
985 (void)(opcode);
986 #endif
987 frame->instr_ptr = next_instr;
988 next_instr += 1;
989 INSTRUCTION_STATS(OP);
990 ham;
991 DISPATCH();
992 }
993 """
994 self.run_cases_test(input, output)
995
996 def test_override_op(self):
997 input = """
998 op(OP, (--)) {
999 spam;
1000 }
1001 macro(M) = OP;
1002 override op(OP, (--)) {
1003 ham;
1004 }
1005 """
1006 output = """
1007 TARGET(M) {
1008 #if Py_TAIL_CALL_INTERP
1009 int opcode = M;
1010 (void)(opcode);
1011 #endif
1012 frame->instr_ptr = next_instr;
1013 next_instr += 1;
1014 INSTRUCTION_STATS(M);
1015 ham;
1016 DISPATCH();
1017 }
1018 """
1019 self.run_cases_test(input, output)
1020
1021 def test_annotated_inst(self):
1022 input = """
1023 pure inst(OP, (--)) {
1024 ham;
1025 }
1026 """
1027 output = """
1028 TARGET(OP) {
1029 #if Py_TAIL_CALL_INTERP
1030 int opcode = OP;
1031 (void)(opcode);
1032 #endif
1033 frame->instr_ptr = next_instr;
1034 next_instr += 1;
1035 INSTRUCTION_STATS(OP);
1036 ham;
1037 DISPATCH();
1038 }
1039 """
1040 self.run_cases_test(input, output)
1041
1042 def test_annotated_op(self):
1043 input = """
1044 pure op(OP, (--)) {
1045 SPAM();
1046 }
1047 macro(M) = OP;
1048 """
1049 output = """
1050 TARGET(M) {
1051 #if Py_TAIL_CALL_INTERP
1052 int opcode = M;
1053 (void)(opcode);
1054 #endif
1055 frame->instr_ptr = next_instr;
1056 next_instr += 1;
1057 INSTRUCTION_STATS(M);
1058 SPAM();
1059 DISPATCH();
1060 }
1061 """
1062 self.run_cases_test(input, output)
1063
1064 input = """
1065 pure register specializing op(OP, (--)) {
1066 SPAM();
1067 }
1068 macro(M) = OP;
1069 """
1070 self.run_cases_test(input, output)
1071
1072 def test_deopt_and_exit(self):
1073 input = """
1074 pure op(OP, (arg1 -- out)) {
1075 DEOPT_IF(1);
1076 EXIT_IF(1);
1077 }
1078 """
1079 output = ""
1080 with self.assertRaises(SyntaxError):
1081 self.run_cases_test(input, output)
1082
1083 def test_array_of_one(self):
1084 input = """
1085 inst(OP, (arg[1] -- out[1])) {
1086 out[0] = arg[0];
1087 DEAD(arg);
1088 }
1089 """
1090 output = """
1091 TARGET(OP) {
1092 #if Py_TAIL_CALL_INTERP
1093 int opcode = OP;
1094 (void)(opcode);
1095 #endif
1096 frame->instr_ptr = next_instr;
1097 next_instr += 1;
1098 INSTRUCTION_STATS(OP);
1099 _PyStackRef *arg;
1100 _PyStackRef *out;
1101 arg = &stack_pointer[-1];
1102 out = &stack_pointer[-1];
1103 out[0] = arg[0];
1104 DISPATCH();
1105 }
1106 """
1107 self.run_cases_test(input, output)
1108
1109 def test_pointer_to_stackref(self):
1110 input = """
1111 inst(OP, (arg: _PyStackRef * -- out)) {
1112 out = *arg;
1113 DEAD(arg);
1114 }
1115 """
1116 output = """
1117 TARGET(OP) {
1118 #if Py_TAIL_CALL_INTERP
1119 int opcode = OP;
1120 (void)(opcode);
1121 #endif
1122 frame->instr_ptr = next_instr;
1123 next_instr += 1;
1124 INSTRUCTION_STATS(OP);
1125 _PyStackRef *arg;
1126 _PyStackRef out;
1127 arg = (_PyStackRef *)stack_pointer[-1].bits;
1128 out = *arg;
1129 stack_pointer[-1] = out;
1130 DISPATCH();
1131 }
1132 """
1133 self.run_cases_test(input, output)
1134
1135 def test_unused_cached_value(self):
1136 input = """
1137 op(FIRST, (arg1 -- out)) {
1138 out = arg1;
1139 }
1140
1141 op(SECOND, (unused -- unused)) {
1142 }
1143
1144 macro(BOTH) = FIRST + SECOND;
1145 """
1146 output = """
1147 """
1148 with self.assertRaises(SyntaxError):
1149 self.run_cases_test(input, output)
1150
1151 def test_unused_named_values(self):
1152 input = """
1153 op(OP, (named -- named)) {
1154 }
1155
1156 macro(INST) = OP;
1157 """
1158 output = """
1159 TARGET(INST) {
1160 #if Py_TAIL_CALL_INTERP
1161 int opcode = INST;
1162 (void)(opcode);
1163 #endif
1164 frame->instr_ptr = next_instr;
1165 next_instr += 1;
1166 INSTRUCTION_STATS(INST);
1167 DISPATCH();
1168 }
1169
1170 """
1171 self.run_cases_test(input, output)
1172
1173 def test_used_unused_used(self):
1174 input = """
1175 op(FIRST, (w -- w)) {
1176 USE(w);
1177 }
1178
1179 op(SECOND, (x -- x)) {
1180 }
1181
1182 op(THIRD, (y -- y)) {
1183 USE(y);
1184 }
1185
1186 macro(TEST) = FIRST + SECOND + THIRD;
1187 """
1188 output = """
1189 TARGET(TEST) {
1190 #if Py_TAIL_CALL_INTERP
1191 int opcode = TEST;
1192 (void)(opcode);
1193 #endif
1194 frame->instr_ptr = next_instr;
1195 next_instr += 1;
1196 INSTRUCTION_STATS(TEST);
1197 _PyStackRef w;
1198 _PyStackRef y;
1199 // FIRST
1200 {
1201 w = stack_pointer[-1];
1202 USE(w);
1203 }
1204 // SECOND
1205 {
1206 }
1207 // THIRD
1208 {
1209 y = w;
1210 USE(y);
1211 }
1212 DISPATCH();
1213 }
1214 """
1215 self.run_cases_test(input, output)
1216
1217 def test_unused_used_used(self):
1218 input = """
1219 op(FIRST, (w -- w)) {
1220 }
1221
1222 op(SECOND, (x -- x)) {
1223 USE(x);
1224 }
1225
1226 op(THIRD, (y -- y)) {
1227 USE(y);
1228 }
1229
1230 macro(TEST) = FIRST + SECOND + THIRD;
1231 """
1232 output = """
1233 TARGET(TEST) {
1234 #if Py_TAIL_CALL_INTERP
1235 int opcode = TEST;
1236 (void)(opcode);
1237 #endif
1238 frame->instr_ptr = next_instr;
1239 next_instr += 1;
1240 INSTRUCTION_STATS(TEST);
1241 _PyStackRef x;
1242 _PyStackRef y;
1243 // FIRST
1244 {
1245 }
1246 // SECOND
1247 {
1248 x = stack_pointer[-1];
1249 USE(x);
1250 }
1251 // THIRD
1252 {
1253 y = x;
1254 USE(y);
1255 }
1256 DISPATCH();
1257 }
1258 """
1259 self.run_cases_test(input, output)
1260
1261 def test_flush(self):
1262 input = """
1263 op(FIRST, ( -- a, b)) {
1264 a = 0;
1265 b = 1;
1266 }
1267
1268 op(SECOND, (a, b -- )) {
1269 USE(a, b);
1270 INPUTS_DEAD();
1271 }
1272
1273 macro(TEST) = FIRST + flush + SECOND;
1274 """
1275 output = """
1276 TARGET(TEST) {
1277 #if Py_TAIL_CALL_INTERP
1278 int opcode = TEST;
1279 (void)(opcode);
1280 #endif
1281 frame->instr_ptr = next_instr;
1282 next_instr += 1;
1283 INSTRUCTION_STATS(TEST);
1284 _PyStackRef a;
1285 _PyStackRef b;
1286 // FIRST
1287 {
1288 a = 0;
1289 b = 1;
1290 }
1291 // flush
1292 stack_pointer[0] = a;
1293 stack_pointer[1] = b;
1294 stack_pointer += 2;
1295 assert(WITHIN_STACK_BOUNDS());
1296 // SECOND
1297 {
1298 USE(a, b);
1299 }
1300 stack_pointer += -2;
1301 assert(WITHIN_STACK_BOUNDS());
1302 DISPATCH();
1303 }
1304 """
1305 self.run_cases_test(input, output)
1306
1307 def test_pop_on_error_peeks(self):
1308
1309 input = """
1310 op(FIRST, (x, y -- a, b)) {
1311 a = x;
1312 DEAD(x);
1313 b = y;
1314 DEAD(y);
1315 }
1316
1317 op(SECOND, (a, b -- a, b)) {
1318 }
1319
1320 op(THIRD, (j, k --)) {
1321 INPUTS_DEAD(); // Mark j and k as used
1322 ERROR_IF(cond);
1323 }
1324
1325 macro(TEST) = FIRST + SECOND + THIRD;
1326 """
1327 output = """
1328 TARGET(TEST) {
1329 #if Py_TAIL_CALL_INTERP
1330 int opcode = TEST;
1331 (void)(opcode);
1332 #endif
1333 frame->instr_ptr = next_instr;
1334 next_instr += 1;
1335 INSTRUCTION_STATS(TEST);
1336 _PyStackRef x;
1337 _PyStackRef y;
1338 _PyStackRef a;
1339 _PyStackRef b;
1340 // FIRST
1341 {
1342 y = stack_pointer[-1];
1343 x = stack_pointer[-2];
1344 a = x;
1345 b = y;
1346 }
1347 // SECOND
1348 {
1349 }
1350 // THIRD
1351 {
1352 if (cond) {
1353 JUMP_TO_LABEL(pop_2_error);
1354 }
1355 }
1356 stack_pointer += -2;
1357 assert(WITHIN_STACK_BOUNDS());
1358 DISPATCH();
1359 }
1360 """
1361 self.run_cases_test(input, output)
1362
1363 def test_push_then_error(self):
1364
1365 input = """
1366 op(FIRST, ( -- a)) {
1367 a = 1;
1368 }
1369
1370 op(SECOND, (a -- a, b)) {
1371 b = 1;
1372 ERROR_IF(cond);
1373 }
1374
1375 macro(TEST) = FIRST + SECOND;
1376 """
1377
1378 output = """
1379 TARGET(TEST) {
1380 #if Py_TAIL_CALL_INTERP
1381 int opcode = TEST;
1382 (void)(opcode);
1383 #endif
1384 frame->instr_ptr = next_instr;
1385 next_instr += 1;
1386 INSTRUCTION_STATS(TEST);
1387 _PyStackRef a;
1388 _PyStackRef b;
1389 // FIRST
1390 {
1391 a = 1;
1392 }
1393 // SECOND
1394 {
1395 b = 1;
1396 if (cond) {
1397 stack_pointer[0] = a;
1398 stack_pointer[1] = b;
1399 stack_pointer += 2;
1400 assert(WITHIN_STACK_BOUNDS());
1401 JUMP_TO_LABEL(error);
1402 }
1403 }
1404 stack_pointer[0] = a;
1405 stack_pointer[1] = b;
1406 stack_pointer += 2;
1407 assert(WITHIN_STACK_BOUNDS());
1408 DISPATCH();
1409 }
1410 """
1411 self.run_cases_test(input, output)
1412
1413 def test_error_if_true(self):
1414
1415 input = """
1416 inst(OP1, ( --)) {
1417 ERROR_IF(true);
1418 }
1419 inst(OP2, ( --)) {
1420 ERROR_IF(1);
1421 }
1422 """
1423 output = """
1424 TARGET(OP1) {
1425 #if Py_TAIL_CALL_INTERP
1426 int opcode = OP1;
1427 (void)(opcode);
1428 #endif
1429 frame->instr_ptr = next_instr;
1430 next_instr += 1;
1431 INSTRUCTION_STATS(OP1);
1432 JUMP_TO_LABEL(error);
1433 }
1434
1435 TARGET(OP2) {
1436 #if Py_TAIL_CALL_INTERP
1437 int opcode = OP2;
1438 (void)(opcode);
1439 #endif
1440 frame->instr_ptr = next_instr;
1441 next_instr += 1;
1442 INSTRUCTION_STATS(OP2);
1443 JUMP_TO_LABEL(error);
1444 }
1445 """
1446 self.run_cases_test(input, output)
1447
1448 def test_scalar_array_inconsistency(self):
1449
1450 input = """
1451 op(FIRST, ( -- a)) {
1452 a = 1;
1453 }
1454
1455 op(SECOND, (a[1] -- b)) {
1456 b = 1;
1457 }
1458
1459 macro(TEST) = FIRST + SECOND;
1460 """
1461
1462 output = """
1463 """
1464 with self.assertRaises(SyntaxError):
1465 self.run_cases_test(input, output)
1466
1467 def test_array_size_inconsistency(self):
1468
1469 input = """
1470 op(FIRST, ( -- a[2])) {
1471 a[0] = 1;
1472 }
1473
1474 op(SECOND, (a[1] -- b)) {
1475 b = 1;
1476 }
1477
1478 macro(TEST) = FIRST + SECOND;
1479 """
1480
1481 output = """
1482 """
1483 with self.assertRaises(SyntaxError):
1484 self.run_cases_test(input, output)
1485
1486 def test_stack_save_reload(self):
1487
1488 input = """
1489 inst(BALANCED, ( -- )) {
1490 SAVE_STACK();
1491 code();
1492 RELOAD_STACK();
1493 }
1494 """
1495
1496 output = """
1497 TARGET(BALANCED) {
1498 #if Py_TAIL_CALL_INTERP
1499 int opcode = BALANCED;
1500 (void)(opcode);
1501 #endif
1502 frame->instr_ptr = next_instr;
1503 next_instr += 1;
1504 INSTRUCTION_STATS(BALANCED);
1505 _PyFrame_SetStackPointer(frame, stack_pointer);
1506 code();
1507 stack_pointer = _PyFrame_GetStackPointer(frame);
1508 DISPATCH();
1509 }
1510 """
1511 self.run_cases_test(input, output)
1512
1513 def test_stack_save_reload_paired(self):
1514
1515 input = """
1516 inst(BALANCED, ( -- )) {
1517 SAVE_STACK();
1518 RELOAD_STACK();
1519 }
1520 """
1521
1522 output = """
1523 TARGET(BALANCED) {
1524 #if Py_TAIL_CALL_INTERP
1525 int opcode = BALANCED;
1526 (void)(opcode);
1527 #endif
1528 frame->instr_ptr = next_instr;
1529 next_instr += 1;
1530 INSTRUCTION_STATS(BALANCED);
1531 DISPATCH();
1532 }
1533 """
1534 self.run_cases_test(input, output)
1535
1536 def test_stack_reload_only(self):
1537
1538 input = """
1539 inst(BALANCED, ( -- )) {
1540 RELOAD_STACK();
1541 }
1542 """
1543
1544 output = """
1545 TARGET(BALANCED) {
1546 #if Py_TAIL_CALL_INTERP
1547 int opcode = BALANCED;
1548 (void)(opcode);
1549 #endif
1550 frame->instr_ptr = next_instr;
1551 next_instr += 1;
1552 INSTRUCTION_STATS(BALANCED);
1553 _PyFrame_SetStackPointer(frame, stack_pointer);
1554 stack_pointer = _PyFrame_GetStackPointer(frame);
1555 DISPATCH();
1556 }
1557 """
1558 with self.assertRaises(SyntaxError):
1559 self.run_cases_test(input, output)
1560
1561 def test_stack_save_only(self):
1562
1563 input = """
1564 inst(BALANCED, ( -- )) {
1565 SAVE_STACK();
1566 }
1567 """
1568
1569 output = """
1570 TARGET(BALANCED) {
1571 #if Py_TAIL_CALL_INTERP
1572 int opcode = BALANCED;
1573 (void)(opcode);
1574 #endif
1575 _Py_CODEUNIT* const this_instr = next_instr;
1576 (void)this_instr;
1577 frame->instr_ptr = next_instr;
1578 next_instr += 1;
1579 INSTRUCTION_STATS(BALANCED);
1580 _PyFrame_SetStackPointer(frame, stack_pointer);
1581 stack_pointer = _PyFrame_GetStackPointer(frame);
1582 DISPATCH();
1583 }
1584 """
1585 with self.assertRaises(SyntaxError):
1586 self.run_cases_test(input, output)
1587
1588 def test_instruction_size_macro(self):
1589 input = """
1590 inst(OP, (--)) {
1591 frame->return_offset = INSTRUCTION_SIZE;
1592 }
1593 """
1594
1595 output = """
1596 TARGET(OP) {
1597 #if Py_TAIL_CALL_INTERP
1598 int opcode = OP;
1599 (void)(opcode);
1600 #endif
1601 frame->instr_ptr = next_instr;
1602 next_instr += 1;
1603 INSTRUCTION_STATS(OP);
1604 frame->return_offset = 1u ;
1605 DISPATCH();
1606 }
1607 """
1608 self.run_cases_test(input, output)
1609
1610 # Two instructions of different sizes referencing the same
1611 # uop containing the `INSTRUCTION_SIZE` macro is not allowed.
1612 input = """
1613 inst(OP, (--)) {
1614 frame->return_offset = INSTRUCTION_SIZE;
1615 }
1616 macro(OP2) = unused/1 + OP;
1617 """
1618
1619 output = "" # No output needed as this should raise an error.
1620 with self.assertRaisesRegex(SyntaxError, "All instructions containing a uop"):
1621 self.run_cases_test(input, output)
1622
1623 def test_escaping_call_next_to_cmacro(self):
1624 input = """
1625 inst(OP, (--)) {
1626 #ifdef Py_GIL_DISABLED
1627 escaping_call();
1628 #else
1629 another_escaping_call();
1630 #endif
1631 yet_another_escaping_call();
1632 }
1633 """
1634 output = """
1635 TARGET(OP) {
1636 #if Py_TAIL_CALL_INTERP
1637 int opcode = OP;
1638 (void)(opcode);
1639 #endif
1640 frame->instr_ptr = next_instr;
1641 next_instr += 1;
1642 INSTRUCTION_STATS(OP);
1643 #ifdef Py_GIL_DISABLED
1644 _PyFrame_SetStackPointer(frame, stack_pointer);
1645 escaping_call();
1646 stack_pointer = _PyFrame_GetStackPointer(frame);
1647 #else
1648 _PyFrame_SetStackPointer(frame, stack_pointer);
1649 another_escaping_call();
1650 stack_pointer = _PyFrame_GetStackPointer(frame);
1651 #endif
1652 _PyFrame_SetStackPointer(frame, stack_pointer);
1653 yet_another_escaping_call();
1654 stack_pointer = _PyFrame_GetStackPointer(frame);
1655 DISPATCH();
1656 }
1657 """
1658 self.run_cases_test(input, output)
1659
1660 def test_pystackref_frompyobject_new_next_to_cmacro(self):
1661 input = """
1662 inst(OP, (-- out1, out2)) {
1663 PyObject *obj = SPAM();
1664 #ifdef Py_GIL_DISABLED
1665 out1 = PyStackRef_FromPyObjectNew(obj);
1666 #else
1667 out1 = PyStackRef_FromPyObjectNew(obj);
1668 #endif
1669 out2 = PyStackRef_FromPyObjectNew(obj);
1670 }
1671 """
1672 output = """
1673 TARGET(OP) {
1674 #if Py_TAIL_CALL_INTERP
1675 int opcode = OP;
1676 (void)(opcode);
1677 #endif
1678 frame->instr_ptr = next_instr;
1679 next_instr += 1;
1680 INSTRUCTION_STATS(OP);
1681 _PyStackRef out1;
1682 _PyStackRef out2;
1683 PyObject *obj = SPAM();
1684 #ifdef Py_GIL_DISABLED
1685 out1 = PyStackRef_FromPyObjectNew(obj);
1686 #else
1687 out1 = PyStackRef_FromPyObjectNew(obj);
1688 #endif
1689 out2 = PyStackRef_FromPyObjectNew(obj);
1690 stack_pointer[0] = out1;
1691 stack_pointer[1] = out2;
1692 stack_pointer += 2;
1693 assert(WITHIN_STACK_BOUNDS());
1694 DISPATCH();
1695 }
1696 """
1697 self.run_cases_test(input, output)
1698
1699 def test_no_escaping_calls_in_branching_macros(self):
1700
1701 input = """
1702 inst(OP, ( -- )) {
1703 DEOPT_IF(escaping_call());
1704 }
1705 """
1706 with self.assertRaises(SyntaxError):
1707 self.run_cases_test(input, "")
1708
1709 input = """
1710 inst(OP, ( -- )) {
1711 EXIT_IF(escaping_call());
1712 }
1713 """
1714 with self.assertRaises(SyntaxError):
1715 self.run_cases_test(input, "")
1716
1717 input = """
1718 inst(OP, ( -- )) {
1719 ERROR_IF(escaping_call());
1720 }
1721 """
1722 with self.assertRaises(SyntaxError):
1723 self.run_cases_test(input, "")
1724
1725 def test_kill_in_wrong_order(self):
1726 input = """
1727 inst(OP, (a, b -- c)) {
1728 c = b;
1729 PyStackRef_CLOSE(a);
1730 PyStackRef_CLOSE(b);
1731 }
1732 """
1733 with self.assertRaises(SyntaxError):
1734 self.run_cases_test(input, "")
1735
1736 def test_complex_label(self):
1737 input = """
1738 label(other_label) {
1739 }
1740
1741 label(other_label2) {
1742 }
1743
1744 label(my_label) {
1745 // Comment
1746 do_thing();
1747 if (complex) {
1748 goto other_label;
1749 }
1750 goto other_label2;
1751 }
1752 """
1753
1754 output = """
1755 LABEL(other_label)
1756 {
1757 }
1758
1759 LABEL(other_label2)
1760 {
1761 }
1762
1763 LABEL(my_label)
1764 {
1765 _PyFrame_SetStackPointer(frame, stack_pointer);
1766 do_thing();
1767 stack_pointer = _PyFrame_GetStackPointer(frame);
1768 if (complex) {
1769 JUMP_TO_LABEL(other_label);
1770 }
1771 JUMP_TO_LABEL(other_label2);
1772 }
1773 """
1774 self.run_cases_test(input, output)
1775
1776 def test_spilled_label(self):
1777 input = """
1778 spilled label(one) {
1779 RELOAD_STACK();
1780 goto two;
1781 }
1782
1783 label(two) {
1784 SAVE_STACK();
1785 goto one;
1786 }
1787 """
1788
1789 output = """
1790 LABEL(one)
1791 {
1792 stack_pointer = _PyFrame_GetStackPointer(frame);
1793 JUMP_TO_LABEL(two);
1794 }
1795
1796 LABEL(two)
1797 {
1798 _PyFrame_SetStackPointer(frame, stack_pointer);
1799 JUMP_TO_LABEL(one);
1800 }
1801 """
1802 self.run_cases_test(input, output)
1803
1804
1805 def test_incorrect_spills(self):
1806 input1 = """
1807 spilled label(one) {
1808 goto two;
1809 }
1810
1811 label(two) {
1812 }
1813 """
1814
1815 input2 = """
1816 spilled label(one) {
1817 }
1818
1819 label(two) {
1820 goto one;
1821 }
1822 """
1823 with self.assertRaisesRegex(SyntaxError, ".*reload.*"):
1824 self.run_cases_test(input1, "")
1825 with self.assertRaisesRegex(SyntaxError, ".*spill.*"):
1826 self.run_cases_test(input2, "")
1827
1828
1829 def test_multiple_labels(self):
1830 input = """
1831 label(my_label_1) {
1832 // Comment
1833 do_thing1();
1834 goto my_label_2;
1835 }
1836
1837 label(my_label_2) {
1838 // Comment
1839 do_thing2();
1840 goto my_label_1;
1841 }
1842 """
1843
1844 output = """
1845 LABEL(my_label_1)
1846 {
1847 _PyFrame_SetStackPointer(frame, stack_pointer);
1848 do_thing1();
1849 stack_pointer = _PyFrame_GetStackPointer(frame);
1850 JUMP_TO_LABEL(my_label_2);
1851 }
1852
1853 LABEL(my_label_2)
1854 {
1855 _PyFrame_SetStackPointer(frame, stack_pointer);
1856 do_thing2();
1857 stack_pointer = _PyFrame_GetStackPointer(frame);
1858 JUMP_TO_LABEL(my_label_1);
1859 }
1860 """
1861 self.run_cases_test(input, output)
1862
1863 def test_reassigning_live_inputs(self):
1864 input = """
1865 inst(OP, (in -- in)) {
1866 in = 0;
1867 }
1868 """
1869
1870 output = """
1871 TARGET(OP) {
1872 #if Py_TAIL_CALL_INTERP
1873 int opcode = OP;
1874 (void)(opcode);
1875 #endif
1876 frame->instr_ptr = next_instr;
1877 next_instr += 1;
1878 INSTRUCTION_STATS(OP);
1879 _PyStackRef in;
1880 in = stack_pointer[-1];
1881 in = 0;
1882 stack_pointer[-1] = in;
1883 DISPATCH();
1884 }
1885 """
1886 self.run_cases_test(input, output)
1887
1888 def test_reassigning_dead_inputs(self):
1889 input = """
1890 inst(OP, (in -- )) {
1891 temp = use(in);
1892 DEAD(in);
1893 in = temp;
1894 PyStackRef_CLOSE(in);
1895 }
1896 """
1897 output = """
1898 TARGET(OP) {
1899 #if Py_TAIL_CALL_INTERP
1900 int opcode = OP;
1901 (void)(opcode);
1902 #endif
1903 frame->instr_ptr = next_instr;
1904 next_instr += 1;
1905 INSTRUCTION_STATS(OP);
1906 _PyStackRef in;
1907 in = stack_pointer[-1];
1908 _PyFrame_SetStackPointer(frame, stack_pointer);
1909 temp = use(in);
1910 stack_pointer = _PyFrame_GetStackPointer(frame);
1911 in = temp;
1912 stack_pointer += -1;
1913 assert(WITHIN_STACK_BOUNDS());
1914 _PyFrame_SetStackPointer(frame, stack_pointer);
1915 PyStackRef_CLOSE(in);
1916 stack_pointer = _PyFrame_GetStackPointer(frame);
1917 DISPATCH();
1918 }
1919 """
1920 self.run_cases_test(input, output)
1921
1922
1923 class TestGeneratedAbstractCases(unittest.TestCase):
1924 def setUp(self) -> None:
1925 super().setUp()
1926 self.maxDiff = None
1927
1928 self.temp_dir = tempfile.gettempdir()
1929 self.temp_input_filename = os.path.join(self.temp_dir, "input.txt")
1930 self.temp_input2_filename = os.path.join(self.temp_dir, "input2.txt")
1931 self.temp_output_filename = os.path.join(self.temp_dir, "output.txt")
1932
1933 def tearDown(self) -> None:
1934 for filename in [
1935 self.temp_input_filename,
1936 self.temp_input2_filename,
1937 self.temp_output_filename,
1938 ]:
1939 try:
1940 os.remove(filename)
1941 except:
1942 pass
1943 super().tearDown()
1944
1945 def run_cases_test(self, input: str, input2: str, expected: str):
1946 with open(self.temp_input_filename, "w+") as temp_input:
1947 temp_input.write(parser.BEGIN_MARKER)
1948 temp_input.write(input)
1949 temp_input.write(parser.END_MARKER)
1950 temp_input.flush()
1951
1952 with open(self.temp_input2_filename, "w+") as temp_input:
1953 temp_input.write(parser.BEGIN_MARKER)
1954 temp_input.write(input2)
1955 temp_input.write(parser.END_MARKER)
1956 temp_input.flush()
1957
1958 with handle_stderr():
1959 optimizer_generator.generate_tier2_abstract_from_files(
1960 [self.temp_input_filename, self.temp_input2_filename],
1961 self.temp_output_filename
1962 )
1963
1964 with open(self.temp_output_filename) as temp_output:
1965 lines = temp_output.readlines()
1966 while lines and lines[0].startswith(("// ", "#", " #", "\n")):
1967 lines.pop(0)
1968 while lines and lines[-1].startswith(("#", "\n")):
1969 lines.pop(-1)
1970 actual = "".join(lines)
1971 self.assertEqual(actual.strip(), expected.strip())
1972
1973 def test_overridden_abstract(self):
1974 input = """
1975 pure op(OP, (--)) {
1976 SPAM();
1977 }
1978 """
1979 input2 = """
1980 pure op(OP, (--)) {
1981 eggs();
1982 }
1983 """
1984 output = """
1985 case OP: {
1986 eggs();
1987 break;
1988 }
1989 """
1990 self.run_cases_test(input, input2, output)
1991
1992 def test_overridden_abstract_args(self):
1993 input = """
1994 pure op(OP, (arg1 -- out)) {
1995 out = SPAM(arg1);
1996 }
1997 op(OP2, (arg1 -- out)) {
1998 out = EGGS(arg1);
1999 }
2000 """
2001 input2 = """
2002 op(OP, (arg1 -- out)) {
2003 out = EGGS(arg1);
2004 }
2005 """
2006 output = """
2007 case OP: {
2008 JitOptSymbol *arg1;
2009 JitOptSymbol *out;
2010 arg1 = stack_pointer[-1];
2011 out = EGGS(arg1);
2012 stack_pointer[-1] = out;
2013 break;
2014 }
2015
2016 case OP2: {
2017 JitOptSymbol *out;
2018 out = sym_new_not_null(ctx);
2019 stack_pointer[-1] = out;
2020 break;
2021 }
2022 """
2023 self.run_cases_test(input, input2, output)
2024
2025 def test_no_overridden_case(self):
2026 input = """
2027 pure op(OP, (arg1 -- out)) {
2028 out = SPAM(arg1);
2029 }
2030
2031 pure op(OP2, (arg1 -- out)) {
2032 }
2033
2034 """
2035 input2 = """
2036 pure op(OP2, (arg1 -- out)) {
2037 out = NULL;
2038 }
2039 """
2040 output = """
2041 case OP: {
2042 JitOptSymbol *out;
2043 out = sym_new_not_null(ctx);
2044 stack_pointer[-1] = out;
2045 break;
2046 }
2047
2048 case OP2: {
2049 JitOptSymbol *out;
2050 out = NULL;
2051 stack_pointer[-1] = out;
2052 break;
2053 }
2054 """
2055 self.run_cases_test(input, input2, output)
2056
2057 def test_missing_override_failure(self):
2058 input = """
2059 pure op(OP, (arg1 -- out)) {
2060 SPAM();
2061 }
2062 """
2063 input2 = """
2064 pure op(OTHER, (arg1 -- out)) {
2065 }
2066 """
2067 output = """
2068 """
2069 with self.assertRaisesRegex(AssertionError, "All abstract uops"):
2070 self.run_cases_test(input, input2, output)
2071
2072
2073 if __name__ == "__main__":
2074 unittest.main()