Skip to content

Commit 3ed1402

Browse files
Remove bytecode object for ceval.c
1 parent af9ea57 commit 3ed1402

File tree

8 files changed

+41
-54
lines changed

8 files changed

+41
-54
lines changed

Include/internal/pycore_ceval.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,7 @@ _PyForIter_VirtualIteratorNext(PyThreadState* tstate, struct _PyInterpreterFrame
391391
#define SPECIAL___AEXIT__ 3
392392
#define SPECIAL_MAX 3
393393

394-
struct _PyCode12 _PyCode_DEF(12);
395-
PyAPI_DATA(const struct _PyCode12) _PyEntryFrameCode;
394+
PyAPI_DATA(const _Py_CODEUNIT *) _Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS_PTR;
396395

397396
#ifdef __cplusplus
398397
}

Python/bytecodes.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
#define USE_COMPUTED_GOTOS 0
4646
#include "ceval_macros.h"
47+
#include "../Include/internal/pycore_stackref.h"
4748

4849
/* Flow control macros */
4950

@@ -170,7 +171,6 @@ dummy_func(
170171

171172
op(_QUICKEN_RESUME, (--)) {
172173
#if ENABLE_SPECIALIZATION_FT
173-
PyCodeObject *code = _PyFrame_GetCode(frame);
174174
if (tstate->tracing == 0 && this_instr->op.code == RESUME) {
175175
FT_ATOMIC_STORE_UINT8_RELAXED(this_instr->op.code, RESUME_CHECK);
176176
}
@@ -5266,7 +5266,9 @@ dummy_func(
52665266
tier2 op(_EXIT_TRACE, (exit_p/4 --)) {
52675267
_PyExitData *exit = (_PyExitData *)exit_p;
52685268
#if defined(Py_DEBUG) && !defined(_Py_JIT)
5269-
_Py_CODEUNIT *target = _PyFrame_GetBytecode(frame) + exit->target;
5269+
const _Py_CODEUNIT *target = ((frame->owner >= FRAME_OWNED_BY_INTERPRETER)
5270+
? _Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS_PTR : _PyFrame_GetBytecode(frame))
5271+
+ exit->target;
52705272
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
52715273
if (frame->lltrace >= 2) {
52725274
printf("SIDE EXIT: [UOp ");
@@ -5420,7 +5422,8 @@ dummy_func(
54205422
tier2 op(_COLD_EXIT, ( -- )) {
54215423
_PyExitData *exit = tstate->jit_exit;
54225424
assert(exit != NULL);
5423-
_Py_CODEUNIT *target = _PyFrame_GetBytecode(frame) + exit->target;
5425+
_Py_CODEUNIT *target = ((frame->owner >= FRAME_OWNED_BY_INTERPRETER)
5426+
? (_Py_CODEUNIT *)_Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS_PTR : _PyFrame_GetBytecode(frame)) + exit->target;
54245427
_Py_BackoffCounter temperature = exit->temperature;
54255428
_PyExecutorObject *executor;
54265429
if (target->op.code == ENTER_EXECUTOR) {
@@ -5429,6 +5432,9 @@ dummy_func(
54295432
Py_INCREF(executor);
54305433
}
54315434
else {
5435+
if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
5436+
GOTO_TIER_ONE(target);
5437+
}
54325438
if (!backoff_counter_triggers(temperature)) {
54335439
exit->temperature = advance_backoff_counter(temperature);
54345440
GOTO_TIER_ONE(target);
@@ -5664,14 +5670,14 @@ dummy_func(
56645670
tstate->interp->jit_state.prev_instr = next_instr;
56655671
}
56665672
tstate->interp->jit_state.specialize_counter = 0;
5667-
PyCodeObject *prev_code = (PyCodeObject *)Py_NewRef(_PyFrame_GetCode(frame));
5673+
PyCodeObject *prev_code = (PyCodeObject *)Py_NewRef(PyStackRef_AsPyObjectBorrow(frame->f_executable));
56685674
if (tstate->interp->jit_state.prev_instr_code != prev_code) {
56695675
Py_SETREF(tstate->interp->jit_state.prev_instr_code, prev_code);
56705676
}
56715677

56725678
tstate->interp->jit_state.prev_instr_frame = frame;
56735679
tstate->interp->jit_state.prev_instr_oparg = oparg;
5674-
tstate->interp->jit_state.prev_instr_stacklevel = STACK_LEVEL();
5680+
tstate->interp->jit_state.prev_instr_stacklevel = PyStackRef_IsNone(frame->f_executable) ? 2 : STACK_LEVEL();
56755681
DISPATCH_GOTO_NON_TRACING();
56765682
#else
56775683
Py_FatalError("JIT label executed in non-jit build.");

Python/ceval.c

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -931,44 +931,17 @@ int _Py_CheckRecursiveCallPy(
931931
return 0;
932932
}
933933

934-
static const PyBytesObject no_location = {
935-
PyVarObject_HEAD_INIT(&PyBytes_Type, 1)
936-
.ob_sval = { NO_LOC_4 }
934+
static const _Py_CODEUNIT _Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS[] = {
935+
/* Put a NOP at the start, so that the IP points into
936+
* the code, rather than before it */
937+
{ .op.code = NOP, .op.arg = 0 },
938+
{ .op.code = INTERPRETER_EXIT, .op.arg = 0 }, /* reached on return */
939+
{ .op.code = NOP, .op.arg = 0 },
940+
{ .op.code = INTERPRETER_EXIT, .op.arg = 0 }, /* reached on yield */
941+
{ .op.code = RESUME, .op.arg = RESUME_OPARG_DEPTH1_MASK | RESUME_AT_FUNC_START }
937942
};
938943

939-
#ifdef Py_GIL_DISABLED
940-
static _PyCodeArray emtry_cleanup_tlbc = {
941-
.size = 1,
942-
.entries = {(char*) &_PyEntryFrameCode.co_code_adaptive},
943-
};
944-
#endif
945-
946-
const struct _PyCode12 _PyEntryFrameCode = {
947-
_PyVarObject_HEAD_INIT(&PyCode_Type, 5),
948-
.co_consts = (PyObject *)&_Py_SINGLETON(tuple_empty),
949-
.co_names = (PyObject *)&_Py_SINGLETON(tuple_empty),
950-
.co_exceptiontable = (PyObject *)&_Py_SINGLETON(bytes_empty),
951-
.co_flags = CO_OPTIMIZED | CO_NO_MONITORING_EVENTS,
952-
.co_localsplusnames = (PyObject *)&_Py_SINGLETON(tuple_empty),
953-
.co_localspluskinds = (PyObject *)&_Py_SINGLETON(bytes_empty),
954-
.co_filename = &_Py_ID(_PyEval_EvalFrameDefault),
955-
.co_name = &_Py_ID(_PyEval_EvalFrameDefault),
956-
.co_qualname = &_Py_ID(_PyEval_EvalFrameDefault),
957-
.co_linetable = (PyObject *)&no_location,
958-
._co_firsttraceable = 4,
959-
.co_stacksize = 2,
960-
.co_framesize = 2 + FRAME_SPECIALS_SIZE,
961-
#ifdef Py_GIL_DISABLED
962-
.co_tlbc = &emtry_cleanup_tlbc,
963-
#endif
964-
.co_code_adaptive = {
965-
NOP, 0,
966-
INTERPRETER_EXIT, 0, /* reached on return */
967-
NOP, 0,
968-
INTERPRETER_EXIT, 0, /* reached on yield */
969-
RESUME, RESUME_OPARG_DEPTH1_MASK | RESUME_AT_FUNC_START
970-
}
971-
};
944+
const _Py_CODEUNIT *_Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS_PTR = (_Py_CODEUNIT*)&_Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS;
972945

973946
#ifdef Py_DEBUG
974947
extern void _PyUOpPrint(const _PyUOpInstruction *uop);
@@ -1140,8 +1113,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
11401113
entry.frame.f_globals = (PyObject*)0xaaa3;
11411114
entry.frame.f_builtins = (PyObject*)0xaaa4;
11421115
#endif
1143-
entry.frame.f_executable = PyStackRef_FromPyObjectBorrow((PyObject *)&_PyEntryFrameCode);
1144-
entry.frame.instr_ptr = ((_Py_CODEUNIT *)_PyEntryFrameCode.co_code_adaptive) + 1;
1116+
entry.frame.f_executable = PyStackRef_None;
1117+
entry.frame.instr_ptr = (_Py_CODEUNIT *)_Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS + 1;
11451118
entry.frame.stackpointer = entry.stack;
11461119
entry.frame.owner = FRAME_OWNED_BY_INTERPRETER;
11471120
entry.frame.visited = 0;

Python/executor_cases.c.h

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
static bool
3535
has_space_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
3636
{
37-
if (code == (PyCodeObject *)&_Py_InitCleanup || code == (PyCodeObject *)&_PyEntryFrameCode) {
37+
if (code == (PyCodeObject *)&_Py_InitCleanup) {
3838
return false;
3939
}
4040
if (instr->op.code == ENTER_EXECUTOR) {
@@ -580,7 +580,9 @@ _PyJit_translate_single_bytecode_to_trace(
580580
_Py_CODEUNIT *target_instr = this_instr;
581581
uint32_t target = 0;
582582

583-
target = INSTR_IP(target_instr, old_code);
583+
target = Py_IsNone((PyObject *)old_code)
584+
? (int)(target_instr - _Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS_PTR)
585+
: INSTR_IP(target_instr, old_code);
584586

585587
// Rewind EXTENDED_ARG so that we see the whole thing.
586588
// We must point to the first EXTENDED_ARG when deopting.
@@ -858,7 +860,7 @@ _PyJit_translate_single_bytecode_to_trace(
858860
DPRINTF(2, "Adding %p func to op\n", (void *)operand);
859861
_Py_BloomFilter_Add(dependencies, new_func);
860862
}
861-
else if (new_code != NULL) {
863+
else if (new_code != NULL && !Py_IsNone((PyObject*)new_code)) {
862864
operand = (uintptr_t)new_code | 1;
863865
DPRINTF(2, "Adding %p code to op\n", (void *)operand);
864866
_Py_BloomFilter_Add(dependencies, new_code);
@@ -867,7 +869,7 @@ _PyJit_translate_single_bytecode_to_trace(
867869
operand = 0;
868870
}
869871
ADD_TO_TRACE(uop, oparg, operand, target);
870-
trace[trace_length - 1].operand1 = ((int)(frame->stackpointer - _PyFrame_Stackbase(frame)));
872+
trace[trace_length - 1].operand1 = PyStackRef_IsNone(frame->f_executable) ? 2 : ((int)(frame->stackpointer - _PyFrame_Stackbase(frame)));
871873
break;
872874
}
873875
if (uop == _BINARY_OP_INPLACE_ADD_UNICODE) {

Python/optimizer_bytecodes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ dummy_func(void) {
812812
JitOptRef temp = PyJitRef_StripReferenceInfo(retval);
813813
DEAD(retval);
814814
SAVE_STACK();
815+
ctx->frame->stack_pointer = stack_pointer;
815816
PyCodeObject *returning_code = get_code_with_logging(this_instr);
816817
if (returning_code == NULL) {
817818
ctx->done = true;

Python/optimizer_cases.c.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)