Skip to content

Commit

Permalink
remove prev_instr and return_offset from frame
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Sep 30, 2023
1 parent c9a9601 commit 764bd37
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 161 deletions.
39 changes: 6 additions & 33 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,15 @@ typedef struct _PyInterpreterFrame {
// frame. Rather, it is the code unit *prior to* the *next* instruction. For
// example, it may be an inline CACHE entry, an instruction we just jumped
// over, or (in the case of a newly-created frame) a totally invalid value:
_Py_CODEUNIT *prev_instr;
_Py_CODEUNIT *prev_traced_instr;
/* The instruction that is currently executing (possibly not started yet). */
_Py_CODEUNIT *instr_ptr;
int stacktop; /* Offset of TOS from localsplus */
/* The return_offset determines where a `RETURN` should go in the caller,
* relative to `prev_instr`.
* It is only meaningful to the callee,
* so it needs to be set in any CALL (to a Python function)
* or SEND (to a coroutine or generator).
* If there is no callee, then it is meaningless. */
/* The yield_offset determines where a `YIELD_VALUE` should go in the caller,
* relative to `instr_ptr`.
* It must be set by SEND, SEND_GEN, FOR_ITER_GEN and used by YIELD_VALUE.
*/
uint16_t yield_offset;
uint16_t return_offset;
/* The new_return_offset determines where a `RETURN` should go in the caller,
* relative to `instr_ptr`.
* It is only meaningful to the callee,
Expand All @@ -90,40 +86,21 @@ typedef struct _PyInterpreterFrame {
#define _PyInterpreterFrame_LASTI(IF) \
((int)(((IF)->instr_ptr) - _PyCode_CODE(_PyFrame_GetCode(IF))))

#define _OldPyInterpreterFrame_LASTI(IF) \
((int)((IF)->prev_instr - _PyCode_CODE(_PyFrame_GetCode(IF))))

static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) {
assert(PyCode_Check(f->f_executable));
return (PyCodeObject *)f->f_executable;
}


static void
dump_frame_ip(const char* title, _PyInterpreterFrame *frame) {
if (frame) {
fprintf(stderr, "%s: frame=%p frame->prev_instr=%p frame->instr_ptr=%p ",
title, frame, frame->prev_instr, frame->instr_ptr);
fprintf(stderr, "%s: frame=%p frame->instr_ptr=%p ",
title, frame, frame->instr_ptr);
fprintf(stderr, "new_return_offset=%d yield_offset=%d \n",
frame->new_return_offset, frame->yield_offset);
}
}


static void
check_lasti_values(_PyInterpreterFrame *f, bool raise, const char* filename, int line) {
int new_addr = _PyInterpreterFrame_LASTI(f) * sizeof(_Py_CODEUNIT);
int addr = _OldPyInterpreterFrame_LASTI(f) * sizeof(_Py_CODEUNIT);
int new = PyCode_Addr2Line(_PyFrame_GetCode(f), new_addr);
int old = PyCode_Addr2Line(_PyFrame_GetCode(f), addr);

if (old != new) {
fprintf(stderr, "f=%p f->prev_instr=%p f->instr_ptr=%p old=%d new=%d\n", f, f->prev_instr, f->instr_ptr, old, new);
fprintf(stderr, "%s : %d\n", filename, line);
}
if (true || raise) assert(old == new);
}

static inline PyObject **_PyFrame_Stackbase(_PyInterpreterFrame *f) {
return f->localsplus + _PyFrame_GetCode(f)->co_nlocalsplus;
}
Expand Down Expand Up @@ -175,9 +152,7 @@ _PyFrame_Initialize(
frame->stacktop = code->co_nlocalsplus;
frame->frame_obj = NULL;
frame->prev_traced_instr = NULL;
frame->prev_instr = _PyCode_CODE(code) - 1;
frame->instr_ptr = _PyCode_CODE(code);
frame->return_offset = 0;
frame->new_return_offset = 0;
frame->yield_offset = 0;
frame->owner = FRAME_OWNED_BY_THREAD;
Expand Down Expand Up @@ -342,10 +317,8 @@ _PyFrame_PushTrampolineUnchecked(PyThreadState *tstate, PyCodeObject *code, int
frame->stacktop = code->co_nlocalsplus + stackdepth;
frame->frame_obj = NULL;
frame->prev_traced_instr = NULL;
frame->prev_instr = _PyCode_CODE(code) + previous_instr;
frame->instr_ptr = _PyCode_CODE(code) + previous_instr + 1;
frame->owner = FRAME_OWNED_BY_THREAD;
frame->return_offset = 0;
frame->new_return_offset = 0;
frame->yield_offset = 0;
return frame;
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ typedef struct _Py_DebugOffsets {
struct _interpreter_frame {
off_t previous;
off_t executable;
off_t prev_instr;
off_t instr_ptr;
off_t localsplus;
off_t owner;
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ extern PyTypeObject _PyExc_MemoryError;
.interpreter_frame = { \
.previous = offsetof(_PyInterpreterFrame, previous), \
.executable = offsetof(_PyInterpreterFrame, f_executable), \
.prev_instr = offsetof(_PyInterpreterFrame, prev_instr), \
.instr_ptr = offsetof(_PyInterpreterFrame, instr_ptr), \
.localsplus = offsetof(_PyInterpreterFrame, localsplus), \
.owner = offsetof(_PyInterpreterFrame, owner), \
Expand Down
7 changes: 0 additions & 7 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ frame_getlineno(PyFrameObject *f, void *closure)
static PyObject *
frame_getlasti(PyFrameObject *f, void *closure)
{
check_lasti_values(f->f_frame, false, __FILE__, __LINE__);
int lasti = _PyInterpreterFrame_LASTI(f->f_frame);
if (lasti < 0) {
return PyLong_FromLong(-1);
Expand Down Expand Up @@ -739,7 +738,6 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore

int64_t best_stack = OVERFLOWED;
int best_addr = -1;
check_lasti_values(f->f_frame, false, __FILE__, __LINE__);
int64_t start_stack = stacks[_PyInterpreterFrame_LASTI(f->f_frame)];
int err = -1;
const char *msg = "cannot find bytecode for specified line";
Expand Down Expand Up @@ -821,7 +819,6 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore
}
/* Finally set the new lasti and return OK. */
f->f_lineno = 0;
f->f_frame->prev_instr = _PyCode_CODE(code) + best_addr;
f->f_frame->instr_ptr = _PyCode_CODE(code) + best_addr;
return 0;
}
Expand Down Expand Up @@ -1080,7 +1077,6 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data;
f->f_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
// This frame needs to be "complete", so pretend that the first RESUME ran:
f->f_frame->prev_instr = _PyCode_CODE(code) + code->_co_firsttraceable;
f->f_frame->instr_ptr = _PyCode_CODE(code) + code->_co_firsttraceable + 1;
assert(!_PyFrame_IsIncomplete(f->f_frame));
Py_DECREF(func);
Expand Down Expand Up @@ -1122,7 +1118,6 @@ frame_init_get_vars(_PyInterpreterFrame *frame)
// COPY_FREE_VARS has no quickened forms, so no need to use _PyOpcode_Deopt
// here:
PyCodeObject *co = _PyFrame_GetCode(frame);
check_lasti_values(frame, false, __FILE__, __LINE__);
int lasti = _PyInterpreterFrame_LASTI(frame);
if (!(lasti < 0 && _PyCode_CODE(co)->op.code == COPY_FREE_VARS
&& PyFunction_Check(frame->f_funcobj)))
Expand All @@ -1139,7 +1134,6 @@ frame_init_get_vars(_PyInterpreterFrame *frame)
frame->localsplus[offset + i] = Py_NewRef(o);
}
// COPY_FREE_VARS doesn't have inline CACHEs, either:
frame->prev_instr = _PyCode_CODE(_PyFrame_GetCode(frame));
frame->instr_ptr = _PyCode_CODE(_PyFrame_GetCode(frame));
}

Expand Down Expand Up @@ -1514,7 +1508,6 @@ int
PyFrame_GetLasti(PyFrameObject *frame)
{
assert(!_PyFrame_IsIncomplete(frame->f_frame));
check_lasti_values(frame->f_frame, false, __FILE__, __LINE__);
int lasti = _PyInterpreterFrame_LASTI(frame->f_frame);
if (lasti < 0) {
return -1;
Expand Down
1 change: 0 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10466,7 +10466,6 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyCodeObject *co,
if (firstarg != NULL && (_PyLocals_GetKind(co->co_localspluskinds, 0) & CO_FAST_CELL)) {
// "firstarg" is a cell here unless (very unlikely) super()
// was called from the C-API before the first MAKE_CELL op.
check_lasti_values(cframe, false, __FILE__, __LINE__);
if (_PyInterpreterFrame_LASTI(cframe) >= 0) {
// MAKE_CELL and COPY_FREE_VARS have no quickened forms, so no need
// to use _PyOpcode_Deopt here:
Expand Down
Loading

0 comments on commit 764bd37

Please sign in to comment.