Skip to content

Commit 770036a

Browse files
committed
Modernize CALL_PY_WITH_DEFAULTS
1 parent 2a71001 commit 770036a

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

Python/bytecodes.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ dummy_func(
23582358
CALL,
23592359
CALL_BOUND_METHOD_EXACT_ARGS,
23602360
CALL_PY_EXACT_ARGS,
2361-
// CALL_PY_WITH_DEFAULTS,
2361+
CALL_PY_WITH_DEFAULTS,
23622362
// CALL_NO_KW_TYPE_1,
23632363
// CALL_NO_KW_STR_1,
23642364
// CALL_NO_KW_TUPLE_1,
@@ -2505,34 +2505,31 @@ dummy_func(
25052505
DISPATCH_INLINED(new_frame);
25062506
}
25072507

2508-
// stack effect: (__0, __array[oparg] -- )
2509-
inst(CALL_PY_WITH_DEFAULTS) {
2508+
inst(CALL_PY_WITH_DEFAULTS, (unused/1, func_version/2, min_args/1, thing1, thing2, unused[oparg] -- unused)) {
25102509
assert(kwnames == NULL);
25112510
DEOPT_IF(tstate->interp->eval_frame, CALL);
2512-
_PyCallCache *cache = (_PyCallCache *)next_instr;
2513-
int is_meth = is_method(stack_pointer, oparg);
2511+
int is_meth = thing1 != NULL;
25142512
int argcount = oparg + is_meth;
2515-
PyObject *callable = PEEK(argcount + 1);
2513+
PyObject *callable = is_meth ? thing1 : thing2;
25162514
DEOPT_IF(!PyFunction_Check(callable), CALL);
25172515
PyFunctionObject *func = (PyFunctionObject *)callable;
2518-
DEOPT_IF(func->func_version != read_u32(cache->func_version), CALL);
2516+
DEOPT_IF(func->func_version != func_version, CALL);
25192517
PyCodeObject *code = (PyCodeObject *)func->func_code;
25202518
DEOPT_IF(argcount > code->co_argcount, CALL);
2521-
int minargs = cache->min_args;
2522-
DEOPT_IF(argcount < minargs, CALL);
2519+
DEOPT_IF(argcount < min_args, CALL);
25232520
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), CALL);
25242521
STAT_INC(CALL, hit);
25252522
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, func, code->co_argcount);
2523+
// Manipulate stack directly since we leave using DISPATCH_INLINED().
25262524
STACK_SHRINK(argcount);
25272525
for (int i = 0; i < argcount; i++) {
25282526
new_frame->localsplus[i] = stack_pointer[i];
25292527
}
25302528
for (int i = argcount; i < code->co_argcount; i++) {
2531-
PyObject *def = PyTuple_GET_ITEM(func->func_defaults,
2532-
i - minargs);
2529+
PyObject *def = PyTuple_GET_ITEM(func->func_defaults, i - min_args);
25332530
new_frame->localsplus[i] = Py_NewRef(def);
25342531
}
2535-
STACK_SHRINK(2-is_meth);
2532+
STACK_SHRINK(2 - is_meth);
25362533
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
25372534
DISPATCH_INLINED(new_frame);
25382535
}

Python/generated_cases.c.h

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

Python/opcode_metadata.h

Lines changed: 3 additions & 3 deletions
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)