Skip to content

Commit 0278706

Browse files
committed
Add NOT_TAKEN in ADDOP_JUMP to tidy up a bit and make the code less error prone
1 parent a604a08 commit 0278706

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

Include/internal/pycore_magic_number.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ PC/launcher.c must also be updated.
275275
276276
*/
277277

278-
#define PYC_MAGIC_NUMBER 3610
278+
#define PYC_MAGIC_NUMBER 3611
279279
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
280280
(little-endian) and then appending b'\r\n'. */
281281
#define PYC_MAGIC_NUMBER_TOKEN \

Python/codegen.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,18 @@ codegen_addop_j(instr_sequence *seq, location loc,
406406
assert(IS_JUMP_TARGET_LABEL(target));
407407
assert(OPCODE_HAS_JUMP(opcode) || IS_BLOCK_PUSH_OPCODE(opcode));
408408
assert(!IS_ASSEMBLER_OPCODE(opcode));
409-
return _PyInstructionSequence_Addop(seq, opcode, target.id, loc);
409+
if (_PyInstructionSequence_Addop(seq, opcode, target.id, loc) != SUCCESS) {
410+
return ERROR;
411+
}
412+
switch (opcode) {
413+
case POP_JUMP_IF_FALSE:
414+
case POP_JUMP_IF_TRUE:
415+
case POP_JUMP_IF_NONE:
416+
case POP_JUMP_IF_NOT_NONE:
417+
case FOR_ITER:
418+
return _PyInstructionSequence_Addop(seq, NOT_TAKEN, 0, NO_LOCATION);
419+
}
420+
return SUCCESS;
410421
}
411422

412423
#define ADDOP_JUMP(C, LOC, OP, O) \
@@ -682,7 +693,6 @@ codegen_setup_annotations_scope(compiler *c, location loc,
682693
ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
683694
NEW_JUMP_TARGET_LABEL(c, body);
684695
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body);
685-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
686696
ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, CONSTANT_NOTIMPLEMENTEDERROR);
687697
ADDOP_I(c, loc, RAISE_VARARGS, 1);
688698
USE_LABEL(c, body);
@@ -1826,13 +1836,11 @@ codegen_jump_if(compiler *c, location loc,
18261836
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, i));
18271837
ADDOP(c, LOC(e), TO_BOOL);
18281838
ADDOP_JUMP(c, LOC(e), POP_JUMP_IF_FALSE, cleanup);
1829-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
18301839
}
18311840
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
18321841
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, n));
18331842
ADDOP(c, LOC(e), TO_BOOL);
18341843
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
1835-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
18361844
NEW_JUMP_TARGET_LABEL(c, end);
18371845
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
18381846

@@ -1857,7 +1865,6 @@ codegen_jump_if(compiler *c, location loc,
18571865
VISIT(c, expr, e);
18581866
ADDOP(c, LOC(e), TO_BOOL);
18591867
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
1860-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
18611868
return SUCCESS;
18621869
}
18631870

@@ -1971,7 +1978,6 @@ codegen_for(compiler *c, stmt_ty s)
19711978

19721979
USE_LABEL(c, start);
19731980
ADDOP_JUMP(c, loc, FOR_ITER, cleanup);
1974-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
19751981

19761982
/* Add NOP to ensure correct line tracing of multiline for statements.
19771983
* It will be removed later if redundant.
@@ -2353,7 +2359,6 @@ codegen_try_except(compiler *c, stmt_ty s)
23532359
VISIT(c, expr, handler->v.ExceptHandler.type);
23542360
ADDOP(c, loc, CHECK_EXC_MATCH);
23552361
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, except);
2356-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
23572362
}
23582363
if (handler->v.ExceptHandler.name) {
23592364
NEW_JUMP_TARGET_LABEL(c, cleanup_end);
@@ -2548,7 +2553,6 @@ codegen_try_star_except(compiler *c, stmt_ty s)
25482553
ADDOP(c, loc, CHECK_EG_MATCH);
25492554
ADDOP_I(c, loc, COPY, 1);
25502555
ADDOP_JUMP(c, loc, POP_JUMP_IF_NONE, no_match);
2551-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
25522556
}
25532557

25542558
NEW_JUMP_TARGET_LABEL(c, cleanup_end);
@@ -2634,7 +2638,6 @@ codegen_try_star_except(compiler *c, stmt_ty s)
26342638
ADDOP_I(c, NO_LOCATION, CALL_INTRINSIC_2, INTRINSIC_PREP_RERAISE_STAR);
26352639
ADDOP_I(c, NO_LOCATION, COPY, 1);
26362640
ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_NOT_NONE, reraise);
2637-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
26382641

26392642
/* Nothing to reraise */
26402643
ADDOP(c, NO_LOCATION, POP_TOP);
@@ -3484,7 +3487,6 @@ codegen_compare(compiler *c, expr_ty e)
34843487
ADDOP_I(c, loc, COPY, 1);
34853488
ADDOP(c, loc, TO_BOOL);
34863489
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, cleanup);
3487-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
34883490
ADDOP(c, loc, POP_TOP);
34893491
}
34903492
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
@@ -4197,7 +4199,6 @@ codegen_sync_comprehension_generator(compiler *c, location loc,
41974199
ADDOP(c, LOC(gen->iter), GET_ITER);
41984200
USE_LABEL(c, start);
41994201
ADDOP_JUMP(c, LOC(gen->iter), FOR_ITER, anchor);
4200-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
42014202
}
42024203
VISIT(c, expr, gen->target);
42034204

@@ -4690,7 +4691,6 @@ codegen_with_except_finish(compiler *c, jump_target_label cleanup) {
46904691
NEW_JUMP_TARGET_LABEL(c, suppress);
46914692
ADDOP(c, NO_LOCATION, TO_BOOL);
46924693
ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress);
4693-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
46944694
ADDOP_I(c, NO_LOCATION, RERAISE, 2);
46954695

46964696
USE_LABEL(c, suppress);
@@ -5412,9 +5412,6 @@ jump_to_fail_pop(compiler *c, location loc,
54125412
Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
54135413
RETURN_IF_ERROR(ensure_fail_pop(c, pc, pops));
54145414
ADDOP_JUMP(c, loc, op, pc->fail_pop[pops]);
5415-
if (op != JUMP) {
5416-
ADDOP(c, NO_LOCATION, NOT_TAKEN);
5417-
}
54185415
return SUCCESS;
54195416
}
54205417

0 commit comments

Comments
 (0)