Skip to content

Commit b400219

Browse files
authored
gh-98831: rewrite RAISE_VARARGS in the instruction definition DSL (#101306)
1 parent 6162a0e commit b400219

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

Python/bytecodes.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -505,27 +505,24 @@ dummy_func(
505505
ERROR_IF(res == NULL, error);
506506
}
507507

508-
// This should remain a legacy instruction.
509-
inst(RAISE_VARARGS) {
508+
inst(RAISE_VARARGS, (args[oparg] -- )) {
510509
PyObject *cause = NULL, *exc = NULL;
511510
switch (oparg) {
512511
case 2:
513-
cause = POP(); /* cause */
512+
cause = args[1];
514513
/* fall through */
515514
case 1:
516-
exc = POP(); /* exc */
515+
exc = args[0];
517516
/* fall through */
518517
case 0:
519-
if (do_raise(tstate, exc, cause)) {
520-
goto exception_unwind;
521-
}
518+
ERROR_IF(do_raise(tstate, exc, cause), exception_unwind);
522519
break;
523520
default:
524521
_PyErr_SetString(tstate, PyExc_SystemError,
525522
"bad RAISE_VARARGS oparg");
526523
break;
527524
}
528-
goto error;
525+
ERROR_IF(true, error);
529526
}
530527

531528
inst(INTERPRETER_EXIT, (retval --)) {

Python/generated_cases.c.h

+5-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/opcode_metadata.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// from Python/bytecodes.c
33
// Do not edit!
44

5+
#ifndef NDEBUG
56
static int
67
_PyOpcode_num_popped(int opcode, int oparg) {
78
switch(opcode) {
@@ -86,7 +87,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
8687
case CALL_INTRINSIC_1:
8788
return 1;
8889
case RAISE_VARARGS:
89-
return -1;
90+
return oparg;
9091
case INTERPRETER_EXIT:
9192
return 1;
9293
case RETURN_VALUE:
@@ -345,7 +346,9 @@ _PyOpcode_num_popped(int opcode, int oparg) {
345346
Py_UNREACHABLE();
346347
}
347348
}
349+
#endif
348350

351+
#ifndef NDEBUG
349352
static int
350353
_PyOpcode_num_pushed(int opcode, int oparg) {
351354
switch(opcode) {
@@ -430,7 +433,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
430433
case CALL_INTRINSIC_1:
431434
return 1;
432435
case RAISE_VARARGS:
433-
return -1;
436+
return 0;
434437
case INTERPRETER_EXIT:
435438
return 0;
436439
case RETURN_VALUE:
@@ -689,6 +692,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
689692
Py_UNREACHABLE();
690693
}
691694
}
695+
#endif
692696
enum Direction { DIR_NONE, DIR_READ, DIR_WRITE };
693697
enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC0, INSTR_FMT_IBC000, INSTR_FMT_IBIB, INSTR_FMT_IX, INSTR_FMT_IXC, INSTR_FMT_IXC000 };
694698
struct opcode_metadata {

Tools/cases_generator/generate_cases.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ def write_stack_effect_functions(self) -> None:
774774
pushed_data.append( (instr, pushed) )
775775

776776
def write_function(direction: str, data: list[tuple[Instruction, str]]) -> None:
777-
self.out.emit("\nstatic int");
777+
self.out.emit("\n#ifndef NDEBUG");
778+
self.out.emit("static int");
778779
self.out.emit(f"_PyOpcode_num_{direction}(int opcode, int oparg) {{")
779780
self.out.emit(" switch(opcode) {");
780781
for instr, effect in data:
@@ -784,6 +785,7 @@ def write_function(direction: str, data: list[tuple[Instruction, str]]) -> None:
784785
self.out.emit(" Py_UNREACHABLE();")
785786
self.out.emit(" }")
786787
self.out.emit("}")
788+
self.out.emit("#endif");
787789

788790
write_function('popped', popped_data)
789791
write_function('pushed', pushed_data)
@@ -1023,7 +1025,7 @@ def always_exits(lines: list[str]) -> bool:
10231025
return False
10241026
line = line[12:]
10251027
return line.startswith(
1026-
("goto ", "return ", "DISPATCH", "GO_TO_", "Py_UNREACHABLE()")
1028+
("goto ", "return ", "DISPATCH", "GO_TO_", "Py_UNREACHABLE()", "ERROR_IF(true, ")
10271029
)
10281030

10291031

0 commit comments

Comments
 (0)