Skip to content

Commit 253ec9b

Browse files
committed
JUMP_IF_FALSE_OR_POP
1 parent 9e5b991 commit 253ec9b

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

Python/bytecodes.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -1992,25 +1992,24 @@ dummy_func(
19921992
}
19931993
}
19941994

1995-
// error: JUMP_IF_FALSE_OR_POP stack effect depends on jump flag
1996-
inst(JUMP_IF_FALSE_OR_POP) {
1997-
PyObject *cond = TOP();
1995+
inst(JUMP_IF_FALSE_OR_POP, (cond -- cond if (jump))) {
1996+
bool jump = false;
19981997
int err;
19991998
if (Py_IsTrue(cond)) {
2000-
STACK_SHRINK(1);
20011999
_Py_DECREF_NO_DEALLOC(cond);
20022000
}
20032001
else if (Py_IsFalse(cond)) {
20042002
JUMPBY(oparg);
2003+
jump = true;
20052004
}
20062005
else {
20072006
err = PyObject_IsTrue(cond);
20082007
if (err > 0) {
2009-
STACK_SHRINK(1);
20102008
Py_DECREF(cond);
20112009
}
20122010
else if (err == 0) {
20132011
JUMPBY(oparg);
2012+
jump = true;
20142013
}
20152014
else {
20162015
goto error;

Python/compile.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -8630,17 +8630,19 @@ opcode_metadata_is_sane(cfg_builder *g) {
86308630
int opcode = instr->i_opcode;
86318631
int oparg = instr->i_oparg;
86328632
assert(opcode <= MAX_REAL_OPCODE);
8633-
int popped = _PyOpcode_num_popped(opcode, oparg);
8634-
int pushed = _PyOpcode_num_pushed(opcode, oparg);
8635-
assert((pushed < 0) == (popped < 0));
8636-
if (pushed >= 0) {
8637-
assert(_PyOpcode_opcode_metadata[opcode].valid_entry);
8638-
int effect = stack_effect(opcode, instr->i_oparg, -1);
8639-
if (effect != pushed - popped) {
8640-
fprintf(stderr,
8641-
"op=%d: stack_effect (%d) != pushed (%d) - popped (%d)\n",
8642-
opcode, effect, pushed, popped);
8643-
result = false;
8633+
for (int jump = 0; jump <= 1; jump++) {
8634+
int popped = _PyOpcode_num_popped(opcode, oparg, jump ? true : false);
8635+
int pushed = _PyOpcode_num_pushed(opcode, oparg, jump ? true : false);
8636+
assert((pushed < 0) == (popped < 0));
8637+
if (pushed >= 0) {
8638+
assert(_PyOpcode_opcode_metadata[opcode].valid_entry);
8639+
int effect = stack_effect(opcode, instr->i_oparg, jump);
8640+
if (effect != pushed - popped) {
8641+
fprintf(stderr,
8642+
"op=%d arg=%d jump=%d: stack_effect (%d) != pushed (%d) - popped (%d)\n",
8643+
opcode, oparg, jump, effect, pushed, popped);
8644+
result = false;
8645+
}
86448646
}
86458647
}
86468648
}

Python/generated_cases.c.h

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

Python/opcode_metadata.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#ifndef NDEBUG
66
static int
7-
_PyOpcode_num_popped(int opcode, int oparg) {
7+
_PyOpcode_num_popped(int opcode, int oparg, bool jump) {
88
switch(opcode) {
99
case NOP:
1010
return 0;
@@ -241,7 +241,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
241241
case POP_JUMP_IF_NONE:
242242
return 1;
243243
case JUMP_IF_FALSE_OR_POP:
244-
return -1;
244+
return 1;
245245
case JUMP_IF_TRUE_OR_POP:
246246
return -1;
247247
case JUMP_BACKWARD_NO_INTERRUPT:
@@ -350,7 +350,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
350350

351351
#ifndef NDEBUG
352352
static int
353-
_PyOpcode_num_pushed(int opcode, int oparg) {
353+
_PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
354354
switch(opcode) {
355355
case NOP:
356356
return 0;
@@ -587,7 +587,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
587587
case POP_JUMP_IF_NONE:
588588
return 0;
589589
case JUMP_IF_FALSE_OR_POP:
590-
return -1;
590+
return (jump ? 1 : 0);
591591
case JUMP_IF_TRUE_OR_POP:
592592
return -1;
593593
case JUMP_BACKWARD_NO_INTERRUPT:

Tools/cases_generator/generate_cases.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ def write_function(
868868
) -> None:
869869
self.out.emit("\n#ifndef NDEBUG")
870870
self.out.emit("static int")
871-
self.out.emit(f"_PyOpcode_num_{direction}(int opcode, int oparg) {{")
871+
self.out.emit(f"_PyOpcode_num_{direction}(int opcode, int oparg, bool jump) {{")
872872
self.out.emit(" switch(opcode) {")
873873
for instr, effect in data:
874874
self.out.emit(f" case {instr.name}:")

0 commit comments

Comments
 (0)