Skip to content

Commit 7bb8104

Browse files
committed
move next_instr updates to where so that we always know which opcode's OPSIZE to use
1 parent 8e37f3d commit 7bb8104

File tree

6 files changed

+228
-57
lines changed

6 files changed

+228
-57
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def _write_atomic(path, data, mode=0o666):
438438
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
439439
# in PC/launcher.c must also be updated.
440440

441-
MAGIC_NUMBER = (3572).to_bytes(2, 'little') + b'\r\n'
441+
MAGIC_NUMBER = (3573).to_bytes(2, 'little') + b'\r\n'
442442

443443
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
444444

Objects/genobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ _PyGen_yf(PyGenObject *gen)
335335
assert(_Py_OPCODE(_PyCode_CODE(gen->gi_code)[0]) != SEND);
336336
return NULL;
337337
}
338-
_Py_CODEUNIT next = frame->prev_instr[OPSIZE(-1)]; /* default OPSIZE - I don't think we know the op */
338+
_Py_CODEUNIT next = frame->prev_instr[1];
339339
if (_Py_OPCODE(next) != RESUME || _Py_OPARG(next) < 2)
340340
{
341341
/* Not in a yield from */

Python/bytecodes.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,6 @@ dummy_func(
390390
_PyBinarySubscrCache *cache = (_PyBinarySubscrCache *)next_instr;
391391
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
392392
assert(cframe.use_tracing == 0);
393-
next_instr -= OPSIZE(opcode);
394393
_Py_Specialize_BinarySubscr(container, sub, next_instr);
395394
DISPATCH_SAME_OPARG();
396395
}
@@ -535,7 +534,6 @@ dummy_func(
535534
inst(STORE_SUBSCR, (counter/1, v, container, sub -- )) {
536535
if (ADAPTIVE_COUNTER_IS_ZERO(counter)) {
537536
assert(cframe.use_tracing == 0);
538-
next_instr -= OPSIZE(opcode);
539537
_Py_Specialize_StoreSubscr(container, sub, next_instr);
540538
DISPATCH_SAME_OPARG();
541539
}
@@ -1062,7 +1060,6 @@ dummy_func(
10621060
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
10631061
assert(cframe.use_tracing == 0);
10641062
PyObject *seq = TOP();
1065-
next_instr -= OPSIZE(opcode);
10661063
_Py_Specialize_UnpackSequence(seq, next_instr, oparg);
10671064
DISPATCH_SAME_OPARG();
10681065
}
@@ -1145,7 +1142,6 @@ dummy_func(
11451142
if (ADAPTIVE_COUNTER_IS_ZERO(counter)) {
11461143
assert(cframe.use_tracing == 0);
11471144
PyObject *name = GETITEM(names, oparg);
1148-
next_instr -= OPSIZE(opcode);
11491145
_Py_Specialize_StoreAttr(owner, next_instr, name);
11501146
DISPATCH_SAME_OPARG();
11511147
}
@@ -1257,7 +1253,6 @@ dummy_func(
12571253
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
12581254
assert(cframe.use_tracing == 0);
12591255
PyObject *name = GETITEM(names, oparg>>1);
1260-
next_instr -= OPSIZE(opcode);
12611256
_Py_Specialize_LoadGlobal(GLOBALS(), BUILTINS(), next_instr, name);
12621257
DISPATCH_SAME_OPARG();
12631258
}
@@ -1703,7 +1698,6 @@ dummy_func(
17031698
assert(cframe.use_tracing == 0);
17041699
PyObject *owner = TOP();
17051700
PyObject *name = GETITEM(names, oparg>>1);
1706-
next_instr -= OPSIZE(opcode);
17071701
_Py_Specialize_LoadAttr(owner, next_instr, name);
17081702
DISPATCH_SAME_OPARG();
17091703
}
@@ -2046,7 +2040,6 @@ dummy_func(
20462040
_PyCompareOpCache *cache = (_PyCompareOpCache *)next_instr;
20472041
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
20482042
assert(cframe.use_tracing == 0);
2049-
next_instr -= OPSIZE(opcode);
20502043
_Py_Specialize_CompareOp(left, right, next_instr, oparg);
20512044
DISPATCH_SAME_OPARG();
20522045
}
@@ -2507,7 +2500,6 @@ dummy_func(
25072500
_PyForIterCache *cache = (_PyForIterCache *)next_instr;
25082501
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
25092502
assert(cframe.use_tracing == 0);
2510-
next_instr -= OPSIZE(opcode);
25112503
_Py_Specialize_ForIter(TOP(), next_instr, oparg);
25122504
DISPATCH_SAME_OPARG();
25132505
}
@@ -2857,13 +2849,14 @@ dummy_func(
28572849
inst(CALL) {
28582850
_PyCallCache *cache = (_PyCallCache *)next_instr;
28592851
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
2852+
28602853
assert(cframe.use_tracing == 0);
28612854
int is_meth = is_method(stack_pointer, oparg);
28622855
int nargs = oparg + is_meth;
28632856
PyObject *callable = PEEK(nargs + 1);
2864-
next_instr -= OPSIZE(opcode);
28652857
_Py_Specialize_Call(callable, next_instr, nargs, kwnames);
28662858
DISPATCH_SAME_OPARG();
2859+
28672860
}
28682861
STAT_INC(CALL, deferred);
28692862
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
@@ -3608,7 +3601,6 @@ dummy_func(
36083601
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)next_instr;
36093602
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
36103603
assert(cframe.use_tracing == 0);
3611-
next_instr -= OPSIZE(opcode);
36123604
_Py_Specialize_BinaryOp(lhs, rhs, next_instr, oparg, &GETLOCAL(0));
36133605
DISPATCH_SAME_OPARG();
36143606
}

Python/ceval.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -668,18 +668,13 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
668668
#ifdef Py_STATS
669669
#define INSTRUCTION_START(op) \
670670
do { \
671-
frame->prev_instr = next_instr; \
672-
next_instr += OPSIZE(op); \
671+
frame->prev_instr = next_instr++; \
673672
OPCODE_EXE_INC(op); \
674673
if (_py_stats) _py_stats->opcode_stats[lastopcode].pair_count[op]++; \
675674
lastopcode = op; \
676675
} while (0)
677676
#else
678-
#define INSTRUCTION_START(op) \
679-
do { \
680-
frame->prev_instr = next_instr; \
681-
next_instr += OPSIZE(op); \
682-
} while (0)
677+
#define INSTRUCTION_START(op) (frame->prev_instr = next_instr++)
683678
#endif
684679

685680
#if USE_COMPUTED_GOTOS
@@ -711,6 +706,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
711706

712707
#define DISPATCH_SAME_OPARG() \
713708
{ \
709+
JUMPBY(-OPSIZE(opcode)); \
714710
opcode = _Py_OPCODE(*next_instr); \
715711
PRE_DISPATCH_GOTO(); \
716712
opcode |= cframe.use_tracing OR_DTRACE_LINE; \
@@ -720,7 +716,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
720716
#define DISPATCH_INLINED(NEW_FRAME) \
721717
do { \
722718
_PyFrame_SetStackPointer(frame, stack_pointer); \
723-
frame->prev_instr = next_instr - OPSIZE(-1); \
719+
frame->prev_instr = next_instr - 1; \
724720
(NEW_FRAME)->previous = frame; \
725721
frame = cframe.current_frame = (NEW_FRAME); \
726722
CALL_STAT_INC(inlined_py_calls); \
@@ -750,7 +746,7 @@ GETITEM(PyObject *v, Py_ssize_t i) {
750746

751747
/* Code access macros */
752748

753-
#define VERBOSE 0
749+
#define VERBOSE 1
754750

755751
/* The integer overflow is checked by an assertion below. */
756752
#define INSTR_OFFSET() ((int)(next_instr - _PyCode_CODE(frame->f_code)))
@@ -759,10 +755,16 @@ GETITEM(PyObject *v, Py_ssize_t i) {
759755
opcode = _Py_OPCODE(word); \
760756
oparg1 = oparg = _Py_OPARG(word); \
761757
if (VERBOSE) fprintf(stderr, "[%d] next_instr = %p opcode = %d\n", __LINE__, next_instr, opcode); \
762-
word = *(next_instr +1); \
758+
word = *(next_instr + 1); \
763759
oparg2 = _Py_OPARG2(word); \
764760
oparg3 = _Py_OPARG3(word); \
765-
if (VERBOSE) fprintf(stderr, "%d (%d, %d, %d)\n", opcode, oparg, oparg2, oparg3); \
761+
if (VERBOSE) { \
762+
fprintf(stderr, "%d (%d, %d, %d)\n", opcode, oparg, oparg2, oparg3); \
763+
if(!_PyErr_Occurred(tstate)) PyObject_Print(frame->f_code->co_filename, stderr, 0); \
764+
fprintf(stderr, "\n name = "); \
765+
if(!_PyErr_Occurred(tstate)) PyObject_Print(frame->f_code->co_name, stderr, 0); \
766+
fprintf(stderr, "\n"); \
767+
} \
766768
} while (0)
767769
#define JUMPTO(x) (next_instr = _PyCode_CODE(frame->f_code) + (x))
768770
#define JUMPBY(x) (next_instr += (x))
@@ -896,6 +898,7 @@ GETITEM(PyObject *v, Py_ssize_t i) {
896898
/* This is only a single jump on release builds! */ \
897899
UPDATE_MISS_STATS((INSTNAME)); \
898900
assert(_PyOpcode_Deopt[opcode] == (INSTNAME)); \
901+
JUMPBY(1 - OPSIZE(opcode)); \
899902
GO_TO_INSTRUCTION(INSTNAME); \
900903
}
901904

@@ -1125,7 +1128,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
11251128
#endif
11261129
entry_frame.f_code = tstate->interp->interpreter_trampoline;
11271130
entry_frame.prev_instr =
1128-
_PyCode_CODE(tstate->interp->interpreter_trampoline);
1131+
_PyCode_CODE(tstate->interp->interpreter_trampoline) + 1;
11291132
entry_frame.stacktop = 0;
11301133
entry_frame.owner = FRAME_OWNED_BY_CSTACK;
11311134
entry_frame.yield_offset = 0;
@@ -1169,13 +1172,15 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
11691172
/* Jump back to the last instruction executed... */ \
11701173
if (VERBOSE) { \
11711174
fprintf(stderr, "Jump back to the last instruction executed\n"); \
1175+
fprintf(stderr, "frame->prev_instr = %p\n", frame->prev_instr); \
11721176
fprintf(stderr, "_PyInterpreterFrame_LASTI(frame) = %d\n filename = ", _PyInterpreterFrame_LASTI(frame)); \
11731177
if(!_PyErr_Occurred(tstate)) PyObject_Print(frame->f_code->co_filename, stderr, 0); \
11741178
fprintf(stderr, "\n name = "); \
11751179
if(!_PyErr_Occurred(tstate)) PyObject_Print(frame->f_code->co_name, stderr, 0); \
11761180
fprintf(stderr, "\n"); \
11771181
} \
1178-
next_instr = frame->prev_instr + (_PyInterpreterFrame_LASTI(frame) == -1 ? 1 : OPSIZE(-1)); /* TODO: init frame to -OPSIZE? */ \
1182+
/* next_instr = frame->prev_instr + 1; */ \
1183+
next_instr = frame->prev_instr + 1; \
11791184
stack_pointer = _PyFrame_GetStackPointer(frame); \
11801185
/* Set stackdepth to -1. \
11811186
Update when returning or calling trace function. \

0 commit comments

Comments
 (0)