Skip to content

Commit 665c0f6

Browse files
Catch up with labels-as-dsl branch
1 parent 15da9c7 commit 665c0f6

File tree

9 files changed

+174
-282
lines changed

9 files changed

+174
-282
lines changed

Lib/test/test_generated_cases.py

Lines changed: 0 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,155 +1697,6 @@ def test_multiple_labels(self):
16971697
}
16981698
"""
16991699

1700-
class TestGeneratedTailCallErorHandlers(unittest.TestCase):
1701-
def setUp(self) -> None:
1702-
super().setUp()
1703-
self.maxDiff = None
1704-
1705-
self.temp_dir = tempfile.gettempdir()
1706-
self.temp_input_filename = os.path.join(self.temp_dir, "input.txt")
1707-
self.temp_output_filename = os.path.join(self.temp_dir, "output.txt")
1708-
self.temp_metadata_filename = os.path.join(self.temp_dir, "metadata.txt")
1709-
self.temp_pymetadata_filename = os.path.join(self.temp_dir, "pymetadata.txt")
1710-
self.temp_executor_filename = os.path.join(self.temp_dir, "executor.txt")
1711-
1712-
def tearDown(self) -> None:
1713-
for filename in [
1714-
self.temp_input_filename,
1715-
self.temp_output_filename,
1716-
self.temp_metadata_filename,
1717-
self.temp_pymetadata_filename,
1718-
self.temp_executor_filename,
1719-
]:
1720-
try:
1721-
os.remove(filename)
1722-
except:
1723-
pass
1724-
super().tearDown()
1725-
1726-
def run_cases_test(self, input: str, expected: str):
1727-
with open(self.temp_input_filename, "w+") as temp_input:
1728-
temp_input.write(textwrap.dedent(input))
1729-
temp_input.flush()
1730-
1731-
with handle_stderr():
1732-
tier1_tail_call_generator.generate_label_handlers_from_files(
1733-
self.temp_input_filename, self.temp_output_filename
1734-
)
1735-
1736-
with open(self.temp_output_filename) as temp_output:
1737-
lines = temp_output.readlines()
1738-
while lines and lines[0].startswith(("// ", "#", " #", "\n")):
1739-
lines.pop(0)
1740-
while lines and lines[-1].startswith(("#", "\n")):
1741-
lines.pop(-1)
1742-
actual = "".join(lines)
1743-
1744-
self.assertEqual(actual.strip(), textwrap.dedent(expected).strip())
1745-
1746-
def test_correctly_finds_pyeval_framedefault(self):
1747-
input = """
1748-
PyObject* _Py_HOT_FUNCTION
1749-
_PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
1750-
{
1751-
1752-
/* END_BASE_INTERPRETER */
1753-
}
1754-
"""
1755-
output = """
1756-
"""
1757-
self.run_cases_test(input, output)
1758-
1759-
def test_simple_label(self):
1760-
input = """
1761-
PyObject* _Py_HOT_FUNCTION
1762-
_PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
1763-
{
1764-
1765-
TAIL_CALL_TARGET(error):
1766-
DO_THING();
1767-
/* END_BASE_INTERPRETER */
1768-
}
1769-
"""
1770-
output = """
1771-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_error(TAIL_CALL_PARAMS);
1772-
1773-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_error(TAIL_CALL_PARAMS)
1774-
{
1775-
DO_THING();
1776-
/* END_BASE_INTERPRETER */
1777-
}
1778-
"""
1779-
self.run_cases_test(input, output)
1780-
1781-
def test_fallthrough_label(self):
1782-
input = """
1783-
PyObject* _Py_HOT_FUNCTION
1784-
_PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
1785-
{
1786-
1787-
TAIL_CALL_TARGET(error):
1788-
DO_THING();
1789-
TAIL_CALL_TARGET(fallthrough):
1790-
DO_THING2();
1791-
/* END_BASE_INTERPRETER */
1792-
}
1793-
"""
1794-
output = """
1795-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_error(TAIL_CALL_PARAMS);
1796-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_fallthrough(TAIL_CALL_PARAMS);
1797-
1798-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_error(TAIL_CALL_PARAMS)
1799-
{
1800-
DO_THING();
1801-
TAIL_CALL(fallthrough);
1802-
}
1803-
1804-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_fallthrough(TAIL_CALL_PARAMS)
1805-
{
1806-
DO_THING2();
1807-
/* END_BASE_INTERPRETER */
1808-
}
1809-
"""
1810-
self.run_cases_test(input, output)
1811-
1812-
def test_transform_gotos(self):
1813-
input = """
1814-
PyObject* _Py_HOT_FUNCTION
1815-
_PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
1816-
{
1817-
1818-
TAIL_CALL_TARGET(error):
1819-
if (thing) {
1820-
goto fallthrough;
1821-
}
1822-
DO_THING();
1823-
TAIL_CALL_TARGET(fallthrough):
1824-
DO_THING2();
1825-
/* END_BASE_INTERPRETER */
1826-
}
1827-
"""
1828-
output = """
1829-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_error(TAIL_CALL_PARAMS);
1830-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_fallthrough(TAIL_CALL_PARAMS);
1831-
1832-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_error(TAIL_CALL_PARAMS)
1833-
{
1834-
if (thing) {
1835-
TAIL_CALL(fallthrough);
1836-
}
1837-
DO_THING();
1838-
TAIL_CALL(fallthrough);
1839-
}
1840-
1841-
Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_fallthrough(TAIL_CALL_PARAMS)
1842-
{
1843-
DO_THING2();
1844-
/* END_BASE_INTERPRETER */
1845-
}
1846-
"""
1847-
self.run_cases_test(input, output)
1848-
18491700

18501701
class TestGeneratedAbstractCases(unittest.TestCase):
18511702
def setUp(self) -> None:

Python/bytecodes.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5287,7 +5287,14 @@ dummy_func(
52875287
lltrace_resume_frame(frame);
52885288
}
52895289
#endif
5290+
// This is a little complicated...
5291+
// If we are in a tail call handler, we want to tail call (DISPATCH).
5292+
// If we're not then we need the shim frame.
5293+
#if defined(Py_TAIL_CALL_INTERP) && !defined(IN_TAIL_CALL_INTERP)
5294+
return _TAIL_CALL_shim(frame, stack_pointer, tstate, next_instr, 0, 0);
5295+
#else
52905296
DISPATCH();
5297+
#endif
52915298
}
52925299

52935300
label(exit_unwind) {
@@ -5299,7 +5306,7 @@ dummy_func(
52995306
frame = tstate->current_frame = dying->previous;
53005307
_PyEval_FrameClearAndPop(tstate, dying);
53015308
frame->return_offset = 0;
5302-
if (frame == &entry_frame) {
5309+
if (frame->owner == FRAME_OWNED_BY_INTERPRETER) {
53035310
/* Restore previous frame and exit */
53045311
tstate->current_frame = frame->previous;
53055312
tstate->c_recursion_remaining += PY_EVAL_C_STACK_UNITS;

Python/ceval.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
808808
#ifdef Py_STATS
809809
int lastopcode = 0;
810810
#endif
811-
#ifndef Py_TAIL_CALL_INTERP
812811
uint8_t opcode; /* Current opcode */
813812
int oparg; /* Current opcode argument, if any */
814813

@@ -835,7 +834,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
835834
#endif
836835
/* Push frame */
837836
entry_frame.previous = tstate->current_frame;
838-
entry_frame.is_entry_frame = 1;
839837
frame->previous = &entry_frame;
840838
tstate->current_frame = frame;
841839

@@ -910,18 +908,13 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
910908
#endif
911909

912910
#ifdef Py_TAIL_CALL_INTERP
913-
#ifdef LLTRACE
914-
return _TAIL_CALL_shim(frame, stack_pointer, tstate, next_instr, 0, 0, lltrace);
915-
#else
916911
return _TAIL_CALL_shim(frame, stack_pointer, tstate, next_instr, 0, 0);
917-
#endif
918912
#else
919913
DISPATCH();
920914
#endif
921915

922916
#include "generated_cases.c.h"
923917

924-
925918
#ifdef _Py_TIER2
926919

927920
// Tier 2 is also here!

Python/ceval_macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ GETITEM(PyObject *v, Py_ssize_t i) {
313313
UPDATE_MISS_STATS((INSTNAME)); \
314314
assert(_PyOpcode_Deopt[opcode] == (INSTNAME)); \
315315
Py_MUSTTAIL \
316-
return (INSTRUCTION_TABLE[op])(frame, stack_pointer, tstate, next_instr - 1 - size, opcode, oparg); \
316+
return (INSTRUCTION_TABLE[INSTNAME])(frame, stack_pointer, tstate, next_instr - 1 - SIZE, opcode, oparg); \
317317
}
318318
#else
319319
# define DEOPT_IF(COND, INSTNAME, SIZE) \

Python/generated_cases.c.h

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)