Debian Codegraph @

Lib/test/test_generated_cases.py

raw
1 import contextlib
2 import os
3 import sys
4 import tempfile
5 import unittest
6
7 from test import support
8 from test import test_tools
9
10
11 def skip_if_different_mount_drives():
12 if sys.platform != "win32":
13 return
14 ROOT = os.path.dirname(os.path.dirname(__file__))
15 root_drive = os.path.splitroot(ROOT)[0]
16 cwd_drive = os.path.splitroot(os.getcwd())[0]
17 if root_drive != cwd_drive:
18 # May raise ValueError if ROOT and the current working
19 # different have different mount drives (on Windows).
20 raise unittest.SkipTest(
21 f"the current working directory and the Python source code "
22 f"directory have different mount drives "
23 f"({cwd_drive} and {root_drive})"
24 )
25
26
27 skip_if_different_mount_drives()
28
29
30 test_tools.skip_if_missing("cases_generator")
31 with test_tools.imports_under_tool("cases_generator"):
32 from analyzer import StackItem
33 import parser
34 from stack import Stack
35 import tier1_generator
36 import optimizer_generator
37
38
39 def handle_stderr():
40 if support.verbose > 1:
41 return contextlib.nullcontext()
42 else:
43 return support.captured_stderr()
44
45
46 class TestEffects(unittest.TestCase):
47 def test_effect_sizes(self):
48 stack = Stack()
49 inputs = [
50 x := StackItem("x", None, "", "1"),
51 y := StackItem("y", None, "", "oparg"),
52 z := StackItem("z", None, "", "oparg*2"),
53 ]
54 outputs = [
55 StackItem("x", None, "", "1"),
56 StackItem("b", None, "", "oparg*4"),
57 StackItem("c", None, "", "1"),
58 ]
59 stack.pop(z)
60 stack.pop(y)
61 stack.pop(x)
62 for out in outputs:
63 stack.push(out)
64 self.assertEqual(stack.base_offset.to_c(), "-1 - oparg*2 - oparg")
65 self.assertEqual(stack.top_offset.to_c(), "1 - oparg*2 - oparg + oparg*4")
66
67
68 class TestGeneratedCases(unittest.TestCase):
69 def setUp(self) -> None:
70 super().setUp()
71 self.maxDiff = None
72
73 self.temp_dir = tempfile.gettempdir()
74 self.temp_input_filename = os.path.join(self.temp_dir, "input.txt")
75 self.temp_output_filename = os.path.join(self.temp_dir, "output.txt")
76 self.temp_metadata_filename = os.path.join(self.temp_dir, "metadata.txt")
77 self.temp_pymetadata_filename = os.path.join(self.temp_dir, "pymetadata.txt")
78 self.temp_executor_filename = os.path.join(self.temp_dir, "executor.txt")
79
80 def tearDown(self) -> None:
81 for filename in [
82 self.temp_input_filename,
83 self.temp_output_filename,
84 self.temp_metadata_filename,
85 self.temp_pymetadata_filename,
86 self.temp_executor_filename,
87 ]:
88 try:
89 os.remove(filename)
90 except:
91 pass
92 super().tearDown()
93
94 def run_cases_test(self, input: str, expected: str):
95 with open(self.temp_input_filename, "w+") as temp_input:
96 temp_input.write(parser.BEGIN_MARKER)
97 temp_input.write(input)
98 temp_input.write(parser.END_MARKER)
99 temp_input.flush()
100
101 with handle_stderr():
102 tier1_generator.generate_tier1_from_files(
103 [self.temp_input_filename], self.temp_output_filename, False
104 )
105
106 with open(self.temp_output_filename) as temp_output:
107 lines = temp_output.readlines()
108 while lines and lines[0].startswith(("// ", "#", " #", "\n")):
109 lines.pop(0)
110 while lines and lines[-1].startswith(("#", "\n")):
111 lines.pop(-1)
112 actual = "".join(lines)
113 # if actual.strip() != expected.strip():
114 # print("Actual:")
115 # print(actual)
116 # print("Expected:")
117 # print(expected)
118 # print("End")
119
120 self.assertEqual(actual.strip(), expected.strip())
121
122 def test_inst_no_args(self):
123 input = """
124 inst(OP, (--)) {
125 spam();
126 }
127 """
128 output = """
129 TARGET(OP) {
130 frame->instr_ptr = next_instr;
131 next_instr += 1;
132 INSTRUCTION_STATS(OP);
133 spam();
134 DISPATCH();
135 }
136 """
137 self.run_cases_test(input, output)
138
139 def test_inst_one_pop(self):
140 input = """
141 inst(OP, (value --)) {
142 spam();
143 }
144 """
145 output = """
146 TARGET(OP) {
147 frame->instr_ptr = next_instr;
148 next_instr += 1;
149 INSTRUCTION_STATS(OP);
150 PyObject *value;
151 value = stack_pointer[-1];
152 spam();
153 stack_pointer += -1;
154 DISPATCH();
155 }
156 """
157 self.run_cases_test(input, output)
158
159 def test_inst_one_push(self):
160 input = """
161 inst(OP, (-- res)) {
162 spam();
163 }
164 """
165 output = """
166 TARGET(OP) {
167 frame->instr_ptr = next_instr;
168 next_instr += 1;
169 INSTRUCTION_STATS(OP);
170 PyObject *res;
171 spam();
172 stack_pointer[0] = res;
173 stack_pointer += 1;
174 DISPATCH();
175 }
176 """
177 self.run_cases_test(input, output)
178
179 def test_inst_one_push_one_pop(self):
180 input = """
181 inst(OP, (value -- res)) {
182 spam();
183 }
184 """
185 output = """
186 TARGET(OP) {
187 frame->instr_ptr = next_instr;
188 next_instr += 1;
189 INSTRUCTION_STATS(OP);
190 PyObject *value;
191 PyObject *res;
192 value = stack_pointer[-1];
193 spam();
194 stack_pointer[-1] = res;
195 DISPATCH();
196 }
197 """
198 self.run_cases_test(input, output)
199
200 def test_binary_op(self):
201 input = """
202 inst(OP, (left, right -- res)) {
203 spam();
204 }
205 """
206 output = """
207 TARGET(OP) {
208 frame->instr_ptr = next_instr;
209 next_instr += 1;
210 INSTRUCTION_STATS(OP);
211 PyObject *right;
212 PyObject *left;
213 PyObject *res;
214 right = stack_pointer[-1];
215 left = stack_pointer[-2];
216 spam();
217 stack_pointer[-2] = res;
218 stack_pointer += -1;
219 DISPATCH();
220 }
221 """
222 self.run_cases_test(input, output)
223
224 def test_overlap(self):
225 input = """
226 inst(OP, (left, right -- left, result)) {
227 spam();
228 }
229 """
230 output = """
231 TARGET(OP) {
232 frame->instr_ptr = next_instr;
233 next_instr += 1;
234 INSTRUCTION_STATS(OP);
235 PyObject *right;
236 PyObject *left;
237 PyObject *result;
238 right = stack_pointer[-1];
239 left = stack_pointer[-2];
240 spam();
241 stack_pointer[-1] = result;
242 DISPATCH();
243 }
244 """
245 self.run_cases_test(input, output)
246
247 def test_predictions_and_eval_breaker(self):
248 input = """
249 inst(OP1, (arg -- rest)) {
250 }
251 inst(OP3, (arg -- res)) {
252 DEOPT_IF(xxx);
253 CHECK_EVAL_BREAKER();
254 }
255 family(OP1, INLINE_CACHE_ENTRIES_OP1) = { OP3 };
256 """
257 output = """
258 TARGET(OP1) {
259 frame->instr_ptr = next_instr;
260 next_instr += 1;
261 INSTRUCTION_STATS(OP1);
262 PREDICTED(OP1);
263 PyObject *arg;
264 PyObject *rest;
265 arg = stack_pointer[-1];
266 stack_pointer[-1] = rest;
267 DISPATCH();
268 }
269
270 TARGET(OP3) {
271 frame->instr_ptr = next_instr;
272 next_instr += 1;
273 INSTRUCTION_STATS(OP3);
274 static_assert(INLINE_CACHE_ENTRIES_OP1 == 0, "incorrect cache size");
275 PyObject *arg;
276 PyObject *res;
277 arg = stack_pointer[-1];
278 DEOPT_IF(xxx, OP1);
279 stack_pointer[-1] = res;
280 CHECK_EVAL_BREAKER();
281 DISPATCH();
282 }
283 """
284 self.run_cases_test(input, output)
285
286 def test_error_if_plain(self):
287 input = """
288 inst(OP, (--)) {
289 ERROR_IF(cond, label);
290 }
291 """
292 output = """
293 TARGET(OP) {
294 frame->instr_ptr = next_instr;
295 next_instr += 1;
296 INSTRUCTION_STATS(OP);
297 if (cond) goto label;
298 DISPATCH();
299 }
300 """
301 self.run_cases_test(input, output)
302
303 def test_error_if_plain_with_comment(self):
304 input = """
305 inst(OP, (--)) {
306 ERROR_IF(cond, label); // Comment is ok
307 }
308 """
309 output = """
310 TARGET(OP) {
311 frame->instr_ptr = next_instr;
312 next_instr += 1;
313 INSTRUCTION_STATS(OP);
314 if (cond) goto label;
315 // Comment is ok
316 DISPATCH();
317 }
318 """
319 self.run_cases_test(input, output)
320
321 def test_error_if_pop(self):
322 input = """
323 inst(OP, (left, right -- res)) {
324 ERROR_IF(cond, label);
325 }
326 """
327 output = """
328 TARGET(OP) {
329 frame->instr_ptr = next_instr;
330 next_instr += 1;
331 INSTRUCTION_STATS(OP);
332 PyObject *right;
333 PyObject *left;
334 PyObject *res;
335 right = stack_pointer[-1];
336 left = stack_pointer[-2];
337 if (cond) goto pop_2_label;
338 stack_pointer[-2] = res;
339 stack_pointer += -1;
340 DISPATCH();
341 }
342 """
343 self.run_cases_test(input, output)
344
345 def test_cache_effect(self):
346 input = """
347 inst(OP, (counter/1, extra/2, value --)) {
348 }
349 """
350 output = """
351 TARGET(OP) {
352 _Py_CODEUNIT *this_instr = frame->instr_ptr = next_instr;
353 (void)this_instr;
354 next_instr += 4;
355 INSTRUCTION_STATS(OP);
356 PyObject *value;
357 value = stack_pointer[-1];
358 uint16_t counter = read_u16(&this_instr[1].cache);
359 (void)counter;
360 uint32_t extra = read_u32(&this_instr[2].cache);
361 (void)extra;
362 stack_pointer += -1;
363 DISPATCH();
364 }
365 """
366 self.run_cases_test(input, output)
367
368 def test_suppress_dispatch(self):
369 input = """
370 inst(OP, (--)) {
371 goto somewhere;
372 }
373 """
374 output = """
375 TARGET(OP) {
376 frame->instr_ptr = next_instr;
377 next_instr += 1;
378 INSTRUCTION_STATS(OP);
379 goto somewhere;
380 }
381 """
382 self.run_cases_test(input, output)
383
384 def test_macro_instruction(self):
385 input = """
386 inst(OP1, (counter/1, left, right -- left, right)) {
387 op1(left, right);
388 }
389 op(OP2, (extra/2, arg2, left, right -- res)) {
390 res = op2(arg2, left, right);
391 }
392 macro(OP) = OP1 + cache/2 + OP2;
393 inst(OP3, (unused/5, arg2, left, right -- res)) {
394 res = op3(arg2, left, right);
395 }
396 family(OP, INLINE_CACHE_ENTRIES_OP) = { OP3 };
397 """
398 output = """
399 TARGET(OP) {
400 frame->instr_ptr = next_instr;
401 next_instr += 6;
402 INSTRUCTION_STATS(OP);
403 PREDICTED(OP);
404 _Py_CODEUNIT *this_instr = next_instr - 6;
405 (void)this_instr;
406 PyObject *right;
407 PyObject *left;
408 PyObject *arg2;
409 PyObject *res;
410 // _OP1
411 right = stack_pointer[-1];
412 left = stack_pointer[-2];
413 {
414 uint16_t counter = read_u16(&this_instr[1].cache);
415 (void)counter;
416 op1(left, right);
417 }
418 /* Skip 2 cache entries */
419 // OP2
420 arg2 = stack_pointer[-3];
421 {
422 uint32_t extra = read_u32(&this_instr[4].cache);
423 (void)extra;
424 res = op2(arg2, left, right);
425 }
426 stack_pointer[-3] = res;
427 stack_pointer += -2;
428 DISPATCH();
429 }
430
431 TARGET(OP1) {
432 _Py_CODEUNIT *this_instr = frame->instr_ptr = next_instr;
433 (void)this_instr;
434 next_instr += 2;
435 INSTRUCTION_STATS(OP1);
436 PyObject *right;
437 PyObject *left;
438 right = stack_pointer[-1];
439 left = stack_pointer[-2];
440 uint16_t counter = read_u16(&this_instr[1].cache);
441 (void)counter;
442 op1(left, right);
443 DISPATCH();
444 }
445
446 TARGET(OP3) {
447 frame->instr_ptr = next_instr;
448 next_instr += 6;
449 INSTRUCTION_STATS(OP3);
450 static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
451 PyObject *right;
452 PyObject *left;
453 PyObject *arg2;
454 PyObject *res;
455 /* Skip 5 cache entries */
456 right = stack_pointer[-1];
457 left = stack_pointer[-2];
458 arg2 = stack_pointer[-3];
459 res = op3(arg2, left, right);
460 stack_pointer[-3] = res;
461 stack_pointer += -2;
462 DISPATCH();
463 }
464 """
465 self.run_cases_test(input, output)
466
467 def test_unused_caches(self):
468 input = """
469 inst(OP, (unused/1, unused/2 --)) {
470 body();
471 }
472 """
473 output = """
474 TARGET(OP) {
475 frame->instr_ptr = next_instr;
476 next_instr += 4;
477 INSTRUCTION_STATS(OP);
478 /* Skip 1 cache entry */
479 /* Skip 2 cache entries */
480 body();
481 DISPATCH();
482 }
483 """
484 self.run_cases_test(input, output)
485
486 def test_pseudo_instruction_no_flags(self):
487 input = """
488 pseudo(OP) = {
489 OP1,
490 };
491
492 inst(OP1, (--)) {
493 }
494 """
495 output = """
496 TARGET(OP1) {
497 frame->instr_ptr = next_instr;
498 next_instr += 1;
499 INSTRUCTION_STATS(OP1);
500 DISPATCH();
501 }
502 """
503 self.run_cases_test(input, output)
504
505 def test_pseudo_instruction_with_flags(self):
506 input = """
507 pseudo(OP, (HAS_ARG, HAS_JUMP)) = {
508 OP1,
509 };
510
511 inst(OP1, (--)) {
512 }
513 """
514 output = """
515 TARGET(OP1) {
516 frame->instr_ptr = next_instr;
517 next_instr += 1;
518 INSTRUCTION_STATS(OP1);
519 DISPATCH();
520 }
521 """
522 self.run_cases_test(input, output)
523
524 def test_array_input(self):
525 input = """
526 inst(OP, (below, values[oparg*2], above --)) {
527 spam();
528 }
529 """
530 output = """
531 TARGET(OP) {
532 frame->instr_ptr = next_instr;
533 next_instr += 1;
534 INSTRUCTION_STATS(OP);
535 PyObject *above;
536 PyObject **values;
537 PyObject *below;
538 above = stack_pointer[-1];
539 values = &stack_pointer[-1 - oparg*2];
540 below = stack_pointer[-2 - oparg*2];
541 spam();
542 stack_pointer += -2 - oparg*2;
543 DISPATCH();
544 }
545 """
546 self.run_cases_test(input, output)
547
548 def test_array_output(self):
549 input = """
550 inst(OP, (unused, unused -- below, values[oparg*3], above)) {
551 spam(values, oparg);
552 }
553 """
554 output = """
555 TARGET(OP) {
556 frame->instr_ptr = next_instr;
557 next_instr += 1;
558 INSTRUCTION_STATS(OP);
559 PyObject *below;
560 PyObject **values;
561 PyObject *above;
562 values = &stack_pointer[-1];
563 spam(values, oparg);
564 stack_pointer[-2] = below;
565 stack_pointer[-1 + oparg*3] = above;
566 stack_pointer += oparg*3;
567 DISPATCH();
568 }
569 """
570 self.run_cases_test(input, output)
571
572 def test_array_input_output(self):
573 input = """
574 inst(OP, (values[oparg] -- values[oparg], above)) {
575 spam(values, oparg);
576 }
577 """
578 output = """
579 TARGET(OP) {
580 frame->instr_ptr = next_instr;
581 next_instr += 1;
582 INSTRUCTION_STATS(OP);
583 PyObject **values;
584 PyObject *above;
585 values = &stack_pointer[-oparg];
586 spam(values, oparg);
587 stack_pointer[0] = above;
588 stack_pointer += 1;
589 DISPATCH();
590 }
591 """
592 self.run_cases_test(input, output)
593
594 def test_array_error_if(self):
595 input = """
596 inst(OP, (extra, values[oparg] --)) {
597 ERROR_IF(oparg == 0, somewhere);
598 }
599 """
600 output = """
601 TARGET(OP) {
602 frame->instr_ptr = next_instr;
603 next_instr += 1;
604 INSTRUCTION_STATS(OP);
605 PyObject **values;
606 PyObject *extra;
607 values = &stack_pointer[-oparg];
608 extra = stack_pointer[-1 - oparg];
609 if (oparg == 0) { stack_pointer += -1 - oparg; goto somewhere; }
610 stack_pointer += -1 - oparg;
611 DISPATCH();
612 }
613 """
614 self.run_cases_test(input, output)
615
616 def test_cond_effect(self):
617 input = """
618 inst(OP, (aa, input if ((oparg & 1) == 1), cc -- xx, output if (oparg & 2), zz)) {
619 output = spam(oparg, input);
620 }
621 """
622 output = """
623 TARGET(OP) {
624 frame->instr_ptr = next_instr;
625 next_instr += 1;
626 INSTRUCTION_STATS(OP);
627 PyObject *cc;
628 PyObject *input = NULL;
629 PyObject *aa;
630 PyObject *xx;
631 PyObject *output = NULL;
632 PyObject *zz;
633 cc = stack_pointer[-1];
634 if ((oparg & 1) == 1) { input = stack_pointer[-1 - (((oparg & 1) == 1) ? 1 : 0)]; }
635 aa = stack_pointer[-2 - (((oparg & 1) == 1) ? 1 : 0)];
636 output = spam(oparg, input);
637 stack_pointer[-2 - (((oparg & 1) == 1) ? 1 : 0)] = xx;
638 if (oparg & 2) stack_pointer[-1 - (((oparg & 1) == 1) ? 1 : 0)] = output;
639 stack_pointer[-1 - (((oparg & 1) == 1) ? 1 : 0) + ((oparg & 2) ? 1 : 0)] = zz;
640 stack_pointer += -(((oparg & 1) == 1) ? 1 : 0) + ((oparg & 2) ? 1 : 0);
641 DISPATCH();
642 }
643 """
644 self.run_cases_test(input, output)
645
646 def test_macro_cond_effect(self):
647 input = """
648 op(A, (left, middle, right --)) {
649 # Body of A
650 }
651 op(B, (-- deep, extra if (oparg), res)) {
652 # Body of B
653 }
654 macro(M) = A + B;
655 """
656 output = """
657 TARGET(M) {
658 frame->instr_ptr = next_instr;
659 next_instr += 1;
660 INSTRUCTION_STATS(M);
661 PyObject *right;
662 PyObject *middle;
663 PyObject *left;
664 PyObject *deep;
665 PyObject *extra = NULL;
666 PyObject *res;
667 // A
668 right = stack_pointer[-1];
669 middle = stack_pointer[-2];
670 left = stack_pointer[-3];
671 {
672 # Body of A
673 }
674 // B
675 {
676 # Body of B
677 }
678 stack_pointer[-3] = deep;
679 if (oparg) stack_pointer[-2] = extra;
680 stack_pointer[-2 + ((oparg) ? 1 : 0)] = res;
681 stack_pointer += -1 + ((oparg) ? 1 : 0);
682 DISPATCH();
683 }
684 """
685 self.run_cases_test(input, output)
686
687 def test_macro_push_push(self):
688 input = """
689 op(A, (-- val1)) {
690 val1 = spam();
691 }
692 op(B, (-- val2)) {
693 val2 = spam();
694 }
695 macro(M) = A + B;
696 """
697 output = """
698 TARGET(M) {
699 frame->instr_ptr = next_instr;
700 next_instr += 1;
701 INSTRUCTION_STATS(M);
702 PyObject *val1;
703 PyObject *val2;
704 // A
705 {
706 val1 = spam();
707 }
708 // B
709 {
710 val2 = spam();
711 }
712 stack_pointer[0] = val1;
713 stack_pointer[1] = val2;
714 stack_pointer += 2;
715 DISPATCH();
716 }
717 """
718 self.run_cases_test(input, output)
719
720 def test_override_inst(self):
721 input = """
722 inst(OP, (--)) {
723 spam();
724 }
725 override inst(OP, (--)) {
726 ham();
727 }
728 """
729 output = """
730 TARGET(OP) {
731 frame->instr_ptr = next_instr;
732 next_instr += 1;
733 INSTRUCTION_STATS(OP);
734 ham();
735 DISPATCH();
736 }
737 """
738 self.run_cases_test(input, output)
739
740 def test_override_op(self):
741 input = """
742 op(OP, (--)) {
743 spam();
744 }
745 macro(M) = OP;
746 override op(OP, (--)) {
747 ham();
748 }
749 """
750 output = """
751 TARGET(M) {
752 frame->instr_ptr = next_instr;
753 next_instr += 1;
754 INSTRUCTION_STATS(M);
755 ham();
756 DISPATCH();
757 }
758 """
759 self.run_cases_test(input, output)
760
761 def test_annotated_inst(self):
762 input = """
763 pure inst(OP, (--)) {
764 ham();
765 }
766 """
767 output = """
768 TARGET(OP) {
769 frame->instr_ptr = next_instr;
770 next_instr += 1;
771 INSTRUCTION_STATS(OP);
772 ham();
773 DISPATCH();
774 }
775 """
776 self.run_cases_test(input, output)
777
778 def test_annotated_op(self):
779 input = """
780 pure op(OP, (--)) {
781 spam();
782 }
783 macro(M) = OP;
784 """
785 output = """
786 TARGET(M) {
787 frame->instr_ptr = next_instr;
788 next_instr += 1;
789 INSTRUCTION_STATS(M);
790 spam();
791 DISPATCH();
792 }
793 """
794 self.run_cases_test(input, output)
795
796 input = """
797 pure register specializing op(OP, (--)) {
798 spam();
799 }
800 macro(M) = OP;
801 """
802 self.run_cases_test(input, output)
803
804
805 def test_deopt_and_exit(self):
806 input = """
807 pure op(OP, (arg1 -- out)) {
808 DEOPT_IF(1);
809 EXIT_IF(1);
810 }
811 """
812 output = ""
813 with self.assertRaises(Exception):
814 self.run_cases_test(input, output)
815
816 def test_pop_on_error_peeks(self):
817
818 input = """
819 op(FIRST, (x, y -- a, b)) {
820 a = x;
821 b = y;
822 }
823
824 op(SECOND, (a, b -- a, b)) {
825 }
826
827 op(THIRD, (j, k --)) {
828 ERROR_IF(cond, error);
829 }
830
831 macro(TEST) = FIRST + SECOND + THIRD;
832 """
833 output = """
834 TARGET(TEST) {
835 frame->instr_ptr = next_instr;
836 next_instr += 1;
837 INSTRUCTION_STATS(TEST);
838 PyObject *y;
839 PyObject *x;
840 PyObject *a;
841 PyObject *b;
842 PyObject *k;
843 PyObject *j;
844 // FIRST
845 y = stack_pointer[-1];
846 x = stack_pointer[-2];
847 {
848 a = x;
849 b = y;
850 }
851 // SECOND
852 {
853 }
854 // THIRD
855 k = b;
856 j = a;
857 {
858 if (cond) goto pop_2_error;
859 }
860 stack_pointer += -2;
861 DISPATCH();
862 }
863 """
864 self.run_cases_test(input, output)
865
866 class TestGeneratedAbstractCases(unittest.TestCase):
867 def setUp(self) -> None:
868 super().setUp()
869 self.maxDiff = None
870
871 self.temp_dir = tempfile.gettempdir()
872 self.temp_input_filename = os.path.join(self.temp_dir, "input.txt")
873 self.temp_input2_filename = os.path.join(self.temp_dir, "input2.txt")
874 self.temp_output_filename = os.path.join(self.temp_dir, "output.txt")
875
876 def tearDown(self) -> None:
877 for filename in [
878 self.temp_input_filename,
879 self.temp_input2_filename,
880 self.temp_output_filename,
881 ]:
882 try:
883 os.remove(filename)
884 except:
885 pass
886 super().tearDown()
887
888 def run_cases_test(self, input: str, input2: str, expected: str):
889 with open(self.temp_input_filename, "w+") as temp_input:
890 temp_input.write(parser.BEGIN_MARKER)
891 temp_input.write(input)
892 temp_input.write(parser.END_MARKER)
893 temp_input.flush()
894
895 with open(self.temp_input2_filename, "w+") as temp_input:
896 temp_input.write(parser.BEGIN_MARKER)
897 temp_input.write(input2)
898 temp_input.write(parser.END_MARKER)
899 temp_input.flush()
900
901 with handle_stderr():
902 optimizer_generator.generate_tier2_abstract_from_files(
903 [self.temp_input_filename, self.temp_input2_filename],
904 self.temp_output_filename
905 )
906
907 with open(self.temp_output_filename) as temp_output:
908 lines = temp_output.readlines()
909 while lines and lines[0].startswith(("// ", "#", " #", "\n")):
910 lines.pop(0)
911 while lines and lines[-1].startswith(("#", "\n")):
912 lines.pop(-1)
913 actual = "".join(lines)
914 self.assertEqual(actual.strip(), expected.strip())
915
916 def test_overridden_abstract(self):
917 input = """
918 pure op(OP, (--)) {
919 spam();
920 }
921 """
922 input2 = """
923 pure op(OP, (--)) {
924 eggs();
925 }
926 """
927 output = """
928 case OP: {
929 eggs();
930 break;
931 }
932 """
933 self.run_cases_test(input, input2, output)
934
935 def test_overridden_abstract_args(self):
936 input = """
937 pure op(OP, (arg1 -- out)) {
938 spam();
939 }
940 op(OP2, (arg1 -- out)) {
941 eggs();
942 }
943 """
944 input2 = """
945 op(OP, (arg1 -- out)) {
946 eggs();
947 }
948 """
949 output = """
950 case OP: {
951 _Py_UopsSymbol *arg1;
952 _Py_UopsSymbol *out;
953 arg1 = stack_pointer[-1];
954 eggs();
955 stack_pointer[-1] = out;
956 break;
957 }
958
959 case OP2: {
960 _Py_UopsSymbol *out;
961 out = sym_new_not_null(ctx);
962 if (out == NULL) goto out_of_space;
963 stack_pointer[-1] = out;
964 break;
965 }
966 """
967 self.run_cases_test(input, input2, output)
968
969 def test_no_overridden_case(self):
970 input = """
971 pure op(OP, (arg1 -- out)) {
972 spam();
973 }
974
975 pure op(OP2, (arg1 -- out)) {
976 }
977
978 """
979 input2 = """
980 pure op(OP2, (arg1 -- out)) {
981 }
982 """
983 output = """
984 case OP: {
985 _Py_UopsSymbol *out;
986 out = sym_new_not_null(ctx);
987 if (out == NULL) goto out_of_space;
988 stack_pointer[-1] = out;
989 break;
990 }
991
992 case OP2: {
993 _Py_UopsSymbol *arg1;
994 _Py_UopsSymbol *out;
995 arg1 = stack_pointer[-1];
996 stack_pointer[-1] = out;
997 break;
998 }
999 """
1000 self.run_cases_test(input, input2, output)
1001
1002 def test_missing_override_failure(self):
1003 input = """
1004 pure op(OP, (arg1 -- out)) {
1005 spam();
1006 }
1007 """
1008 input2 = """
1009 pure op(OTHER, (arg1 -- out)) {
1010 }
1011 """
1012 output = """
1013 """
1014 with self.assertRaisesRegex(AssertionError, "All abstract uops"):
1015 self.run_cases_test(input, input2, output)
1016
1017
1018 if __name__ == "__main__":
1019 unittest.main()