Skip to content

Commit 2b105e8

Browse files
committed
Modernize CALL_PY_WITH_DEFAULTS
1 parent 569c1d7 commit 2b105e8

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

Python/bytecodes.c

+9-12
Original file line numberDiff line numberDiff line change
@@ -2370,7 +2370,7 @@ dummy_func(
23702370
CALL,
23712371
CALL_BOUND_METHOD_EXACT_ARGS,
23722372
CALL_PY_EXACT_ARGS,
2373-
// CALL_PY_WITH_DEFAULTS,
2373+
CALL_PY_WITH_DEFAULTS,
23742374
// CALL_NO_KW_TYPE_1,
23752375
// CALL_NO_KW_STR_1,
23762376
// CALL_NO_KW_TUPLE_1,
@@ -2517,34 +2517,31 @@ dummy_func(
25172517
DISPATCH_INLINED(new_frame);
25182518
}
25192519

2520-
// stack effect: (__0, __array[oparg] -- )
2521-
inst(CALL_PY_WITH_DEFAULTS) {
2520+
inst(CALL_PY_WITH_DEFAULTS, (unused/1, func_version/2, min_args/1, thing1, thing2, unused[oparg] -- unused)) {
25222521
assert(kwnames == NULL);
25232522
DEOPT_IF(tstate->interp->eval_frame, CALL);
2524-
_PyCallCache *cache = (_PyCallCache *)next_instr;
2525-
int is_meth = is_method(stack_pointer, oparg);
2523+
int is_meth = thing1 != NULL;
25262524
int argcount = oparg + is_meth;
2527-
PyObject *callable = PEEK(argcount + 1);
2525+
PyObject *callable = is_meth ? thing1 : thing2;
25282526
DEOPT_IF(!PyFunction_Check(callable), CALL);
25292527
PyFunctionObject *func = (PyFunctionObject *)callable;
2530-
DEOPT_IF(func->func_version != read_u32(cache->func_version), CALL);
2528+
DEOPT_IF(func->func_version != func_version, CALL);
25312529
PyCodeObject *code = (PyCodeObject *)func->func_code;
25322530
DEOPT_IF(argcount > code->co_argcount, CALL);
2533-
int minargs = cache->min_args;
2534-
DEOPT_IF(argcount < minargs, CALL);
2531+
DEOPT_IF(argcount < min_args, CALL);
25352532
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), CALL);
25362533
STAT_INC(CALL, hit);
25372534
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, func, code->co_argcount);
2535+
// Manipulate stack directly since we leave using DISPATCH_INLINED().
25382536
STACK_SHRINK(argcount);
25392537
for (int i = 0; i < argcount; i++) {
25402538
new_frame->localsplus[i] = stack_pointer[i];
25412539
}
25422540
for (int i = argcount; i < code->co_argcount; i++) {
2543-
PyObject *def = PyTuple_GET_ITEM(func->func_defaults,
2544-
i - minargs);
2541+
PyObject *def = PyTuple_GET_ITEM(func->func_defaults, i - min_args);
25452542
new_frame->localsplus[i] = Py_NewRef(def);
25462543
}
2547-
STACK_SHRINK(2-is_meth);
2544+
STACK_SHRINK(2 - is_meth);
25482545
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
25492546
DISPATCH_INLINED(new_frame);
25502547
}

Python/generated_cases.c.h

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

Python/opcode_metadata.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
293293
case CALL_PY_EXACT_ARGS:
294294
return oparg + 2;
295295
case CALL_PY_WITH_DEFAULTS:
296-
return -1;
296+
return oparg + 2;
297297
case CALL_NO_KW_TYPE_1:
298298
return -1;
299299
case CALL_NO_KW_STR_1:
@@ -639,7 +639,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
639639
case CALL_PY_EXACT_ARGS:
640640
return 1;
641641
case CALL_PY_WITH_DEFAULTS:
642-
return -1;
642+
return 1;
643643
case CALL_NO_KW_TYPE_1:
644644
return -1;
645645
case CALL_NO_KW_STR_1:
@@ -845,7 +845,7 @@ struct opcode_metadata {
845845
[CALL] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
846846
[CALL_BOUND_METHOD_EXACT_ARGS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
847847
[CALL_PY_EXACT_ARGS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
848-
[CALL_PY_WITH_DEFAULTS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
848+
[CALL_PY_WITH_DEFAULTS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
849849
[CALL_NO_KW_TYPE_1] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
850850
[CALL_NO_KW_STR_1] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
851851
[CALL_NO_KW_TUPLE_1] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },

0 commit comments

Comments
 (0)