@@ -36,7 +36,6 @@ def skip_if_different_mount_drives():
36
36
import parser
37
37
from stack import Local , Stack
38
38
import tier1_generator
39
- import tier1_tail_call_generator
40
39
import opcode_metadata_generator
41
40
import optimizer_generator
42
41
@@ -462,7 +461,7 @@ def test_predictions(self):
462
461
if (xxx) {
463
462
UPDATE_MISS_STATS(OP1);
464
463
assert(_PyOpcode_Deopt[opcode] == (OP1));
465
- goto PREDICTED_OP1 ;
464
+ JUMP_TO_PREDICTED(OP1) ;
466
465
}
467
466
res = Py_None;
468
467
stack_pointer[-1] = res;
@@ -544,7 +543,7 @@ def test_error_if_plain(self):
544
543
next_instr += 1;
545
544
INSTRUCTION_STATS(OP);
546
545
if (cond) {
547
- goto label;
546
+ JUMP_TO_LABEL( label) ;
548
547
}
549
548
DISPATCH();
550
549
}
@@ -563,7 +562,7 @@ def test_error_if_plain_with_comment(self):
563
562
next_instr += 1;
564
563
INSTRUCTION_STATS(OP);
565
564
if (cond) {
566
- goto label;
565
+ JUMP_TO_LABEL( label) ;
567
566
}
568
567
// Comment is ok
569
568
DISPATCH();
@@ -592,7 +591,7 @@ def test_error_if_pop(self):
592
591
left = stack_pointer[-2];
593
592
SPAM(left, right);
594
593
if (cond) {
595
- goto pop_2_label;
594
+ JUMP_TO_LABEL( pop_2_label) ;
596
595
}
597
596
res = 0;
598
597
stack_pointer[-2] = res;
@@ -623,7 +622,7 @@ def test_error_if_pop_with_result(self):
623
622
left = stack_pointer[-2];
624
623
res = SPAM(left, right);
625
624
if (cond) {
626
- goto pop_2_label;
625
+ JUMP_TO_LABEL( pop_2_label) ;
627
626
}
628
627
stack_pointer[-2] = res;
629
628
stack_pointer += -1;
@@ -640,8 +639,9 @@ def test_cache_effect(self):
640
639
"""
641
640
output = """
642
641
TARGET(OP) {
643
- _Py_CODEUNIT* const this_instr = frame->instr_ptr = next_instr;
642
+ _Py_CODEUNIT* const this_instr = next_instr;
644
643
(void)this_instr;
644
+ frame->instr_ptr = next_instr;
645
645
next_instr += 4;
646
646
INSTRUCTION_STATS(OP);
647
647
uint16_t counter = read_u16(&this_instr[1].cache);
@@ -726,8 +726,9 @@ def test_macro_instruction(self):
726
726
}
727
727
728
728
TARGET(OP1) {
729
- _Py_CODEUNIT* const this_instr = frame->instr_ptr = next_instr;
729
+ _Py_CODEUNIT* const this_instr = next_instr;
730
730
(void)this_instr;
731
+ frame->instr_ptr = next_instr;
731
732
next_instr += 2;
732
733
INSTRUCTION_STATS(OP1);
733
734
_PyStackRef left;
@@ -942,7 +943,7 @@ def test_array_error_if(self):
942
943
if (oparg == 0) {
943
944
stack_pointer += -1 - oparg;
944
945
assert(WITHIN_STACK_BOUNDS());
945
- goto somewhere;
946
+ JUMP_TO_LABEL( somewhere) ;
946
947
}
947
948
stack_pointer += -1 - oparg;
948
949
assert(WITHIN_STACK_BOUNDS());
@@ -1406,7 +1407,7 @@ def test_pop_on_error_peeks(self):
1406
1407
{
1407
1408
// Mark j and k as used
1408
1409
if (cond) {
1409
- goto pop_2_error;
1410
+ JUMP_TO_LABEL( pop_2_error) ;
1410
1411
}
1411
1412
}
1412
1413
stack_pointer += -2;
@@ -1450,7 +1451,7 @@ def test_push_then_error(self):
1450
1451
stack_pointer[1] = b;
1451
1452
stack_pointer += 2;
1452
1453
assert(WITHIN_STACK_BOUNDS());
1453
- goto error;
1454
+ JUMP_TO_LABEL( error) ;
1454
1455
}
1455
1456
}
1456
1457
stack_pointer[0] = a;
@@ -1477,14 +1478,14 @@ def test_error_if_true(self):
1477
1478
frame->instr_ptr = next_instr;
1478
1479
next_instr += 1;
1479
1480
INSTRUCTION_STATS(OP1);
1480
- goto here;
1481
+ JUMP_TO_LABEL( here) ;
1481
1482
}
1482
1483
1483
1484
TARGET(OP2) {
1484
1485
frame->instr_ptr = next_instr;
1485
1486
next_instr += 1;
1486
1487
INSTRUCTION_STATS(OP2);
1487
- goto there;
1488
+ JUMP_TO_LABEL( there) ;
1488
1489
}
1489
1490
"""
1490
1491
self .run_cases_test (input , output )
@@ -1784,7 +1785,7 @@ def test_complex_label(self):
1784
1785
"""
1785
1786
1786
1787
output = """
1787
- my_label:
1788
+ LABEL( my_label)
1788
1789
{
1789
1790
// Comment
1790
1791
do_thing()
@@ -1812,146 +1813,21 @@ def test_multiple_labels(self):
1812
1813
"""
1813
1814
1814
1815
output = """
1815
- my_label_1:
1816
+ LABEL( my_label_1)
1816
1817
{
1817
1818
// Comment
1818
1819
do_thing1();
1819
1820
goto my_label_2;
1820
1821
}
1821
1822
1822
- my_label_2:
1823
+ LABEL( my_label_2)
1823
1824
{
1824
1825
// Comment
1825
1826
do_thing2();
1826
1827
goto my_label_3;
1827
1828
}
1828
1829
"""
1829
-
1830
- class TestGeneratedTailCallHandlers (unittest .TestCase ):
1831
- def setUp (self ) -> None :
1832
- super ().setUp ()
1833
- self .maxDiff = None
1834
-
1835
- self .temp_dir = tempfile .gettempdir ()
1836
- self .temp_input_filename = os .path .join (self .temp_dir , "input.txt" )
1837
- self .temp_output_filename = os .path .join (self .temp_dir , "output.txt" )
1838
- self .temp_labels_output_filename = os .path .join (self .temp_dir , "labels_output.txt" )
1839
-
1840
- def tearDown (self ) -> None :
1841
- for filename in [
1842
- self .temp_input_filename ,
1843
- self .temp_output_filename ,
1844
- self .temp_labels_output_filename ,
1845
- ]:
1846
- try :
1847
- os .remove (filename )
1848
- except Exception :
1849
- pass
1850
- super ().tearDown ()
1851
-
1852
- def run_cases_test (self , input : str , expected : str , expected_labels : str ):
1853
- with open (self .temp_input_filename , "w+" ) as temp_input :
1854
- temp_input .write (parser .BEGIN_MARKER )
1855
- temp_input .write (input )
1856
- temp_input .write (parser .END_MARKER )
1857
- temp_input .flush ()
1858
-
1859
- with handle_stderr ():
1860
- tier1_tail_call_generator .generate_tier1_from_files (
1861
- [self .temp_input_filename ], self .temp_output_filename , self .temp_labels_output_filename , False
1862
- )
1863
-
1864
- with open (self .temp_output_filename ) as temp_output :
1865
- lines = temp_output .read ()
1866
- _ , rest = lines .split (tier1_generator .INSTRUCTION_START_MARKER )
1867
- instructions , _ = rest .split (tier1_generator .INSTRUCTION_END_MARKER )
1868
-
1869
- with open (self .temp_labels_output_filename ) as temp_output :
1870
- lines = temp_output .readlines ()
1871
- while lines and lines [0 ].startswith (("// " , "#" , " #" , "\n " )):
1872
- lines .pop (0 )
1873
- while lines and lines [- 1 ].startswith (("#" , "\n " )):
1874
- lines .pop (- 1 )
1875
- actual_labels = "" .join (lines )
1876
-
1877
- self .assertEqual (instructions .strip (), expected .strip ())
1878
- self .assertEqual (actual_labels .strip (), expected_labels .strip ())
1879
-
1880
- def test_basic (self ):
1881
- input = """
1882
- inst(OP, (--)) {
1883
- SPAM();
1884
- }
1885
- """
1886
- output = """
1887
- Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_OP(TAIL_CALL_PARAMS) {
1888
- {
1889
- int opcode = next_instr->op.code;
1890
- (void)(opcode);
1891
- frame->instr_ptr = next_instr;
1892
- next_instr += 1;
1893
- INSTRUCTION_STATS(OP);
1894
- SPAM();
1895
- }
1896
- DISPATCH();
1897
- }
1898
- """
1899
- output_labels = ""
1900
- self .run_cases_test (input , output , output_labels )
1901
-
1902
- def test_label_transformed (self ):
1903
- """This tests that the labels and their gotos/DISPATCH get transformed to tail calls in the
1904
- tail-calling interpreter, while staying the same/becoming an entry call in the normal interpreter.
1905
- """
1906
- input = """
1907
- inst(OP, (--)) {
1908
- SPAM();
1909
- }
1910
-
1911
- label(hello) {
1912
- EGGS();
1913
- if (x) {
1914
- goto baz;
1915
- }
1916
- DISPATCH();
1917
- }
1918
- """
1919
- output = """
1920
- Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_hello(TAIL_CALL_PARAMS)
1921
- {
1922
- EGGS();
1923
- if (x) {
1924
- TAIL_CALL(baz);
1925
- }
1926
- DISPATCH();
1927
- }
1928
-
1929
-
1930
- Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_OP(TAIL_CALL_PARAMS) {
1931
- {
1932
- int opcode = next_instr->op.code;
1933
- (void)(opcode);
1934
- frame->instr_ptr = next_instr;
1935
- next_instr += 1;
1936
- INSTRUCTION_STATS(OP);
1937
- SPAM();
1938
- }
1939
- DISPATCH();
1940
- hello:
1941
- TAIL_CALL(hello);
1942
- }
1943
- """
1944
- output_labels = """
1945
- hello:
1946
- {
1947
- EGGS();
1948
- if (x) {
1949
- goto baz;
1950
- }
1951
- return _TAIL_CALL_entry(frame, stack_pointer, tstate, next_instr, 0);
1952
- }
1953
- """
1954
- self .run_cases_test (input , output , output_labels )
1830
+ self .run_cases_test (input , output )
1955
1831
1956
1832
1957
1833
class TestGeneratedAbstractCases (unittest .TestCase ):
0 commit comments