Skip to content

Commit 41bc101

Browse files
authored
GH-98831: "Generate" the interpreter (#98830)
The switch cases (really TARGET(opcode) macros) have been moved from ceval.c to generated_cases.c.h. That file is generated from instruction definitions in bytecodes.c (which impersonates a C file so the C code it contains can be edited without custom support in e.g. VS Code). The code generator lives in Tools/cases_generator (it has a README.md explaining how it works). The DSL used to describe the instructions is a work in progress, described in https://github.com/faster-cpython/ideas/blob/main/3.12/interpreter_definition.md. This is surely a work-in-progress. An easy next step could be auto-generating super-instructions. **IMPORTANT: Merge Conflicts** If you get a merge conflict for instruction implementations in ceval.c, your best bet is to port your changes to bytecodes.c. That file looks almost the same as the original cases, except instead of `TARGET(NAME)` it uses `inst(NAME)`, and the trailing `DISPATCH()` call is omitted (the code generator adds it automatically).
1 parent 2cfcaf5 commit 41bc101

13 files changed

+8961
-3851
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Parser/parser.c generated
8282
Parser/token.c generated
8383
Programs/test_frozenmain.h generated
8484
Python/Python-ast.c generated
85+
Python/generated_cases.c.h generated
8586
Python/opcode_targets.h generated
8687
Python/stdlib_module_names.h generated
8788
Tools/peg_generator/pegen/grammar_parser.py generated

Makefile.pre.in

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,19 @@ regen-opcode-targets:
14451445
$(srcdir)/Python/opcode_targets.h.new
14461446
$(UPDATE_FILE) $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/opcode_targets.h.new
14471447

1448-
Python/ceval.o: $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/condvar.h
1448+
.PHONY: regen-cases
1449+
regen-cases:
1450+
# Regenerate Python/generated_cases.c.h from Python/bytecodes.c
1451+
# using Tools/cases_generator/generate_cases.py
1452+
PYTHONPATH=$(srcdir)/Tools/cases_generator \
1453+
$(PYTHON_FOR_REGEN) \
1454+
$(srcdir)/Tools/cases_generator/generate_cases.py \
1455+
-i $(srcdir)/Python/bytecodes.c \
1456+
-o $(srcdir)/Python/generated_cases.c.h.new
1457+
$(UPDATE_FILE) $(srcdir)/Python/generated_cases.c.h $(srcdir)/Python/generated_cases.c.h.new
1458+
1459+
Python/ceval.o: $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/condvar.h $(srcdir)/Python/generated_cases.c.h
1460+
14491461

14501462
Python/frozen.o: $(FROZEN_FILES_OUT)
14511463

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
We have new tooling, in ``Tools/cases_generator``, to generate the interpreter switch from a list of opcode definitions.

0 commit comments

Comments
 (0)