Skip to content

Commit

Permalink
Modernize CALL_PY_WITH_DEFAULTS
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Feb 1, 2023
1 parent 569c1d7 commit 2b105e8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
21 changes: 9 additions & 12 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2370,7 +2370,7 @@ dummy_func(
CALL,
CALL_BOUND_METHOD_EXACT_ARGS,
CALL_PY_EXACT_ARGS,
// CALL_PY_WITH_DEFAULTS,
CALL_PY_WITH_DEFAULTS,
// CALL_NO_KW_TYPE_1,
// CALL_NO_KW_STR_1,
// CALL_NO_KW_TUPLE_1,
Expand Down Expand Up @@ -2517,34 +2517,31 @@ dummy_func(
DISPATCH_INLINED(new_frame);
}

// stack effect: (__0, __array[oparg] -- )
inst(CALL_PY_WITH_DEFAULTS) {
inst(CALL_PY_WITH_DEFAULTS, (unused/1, func_version/2, min_args/1, thing1, thing2, unused[oparg] -- unused)) {
assert(kwnames == NULL);
DEOPT_IF(tstate->interp->eval_frame, CALL);
_PyCallCache *cache = (_PyCallCache *)next_instr;
int is_meth = is_method(stack_pointer, oparg);
int is_meth = thing1 != NULL;
int argcount = oparg + is_meth;
PyObject *callable = PEEK(argcount + 1);
PyObject *callable = is_meth ? thing1 : thing2;
DEOPT_IF(!PyFunction_Check(callable), CALL);
PyFunctionObject *func = (PyFunctionObject *)callable;
DEOPT_IF(func->func_version != read_u32(cache->func_version), CALL);
DEOPT_IF(func->func_version != func_version, CALL);
PyCodeObject *code = (PyCodeObject *)func->func_code;
DEOPT_IF(argcount > code->co_argcount, CALL);
int minargs = cache->min_args;
DEOPT_IF(argcount < minargs, CALL);
DEOPT_IF(argcount < min_args, CALL);
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), CALL);
STAT_INC(CALL, hit);
_PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, func, code->co_argcount);
// Manipulate stack directly since we leave using DISPATCH_INLINED().
STACK_SHRINK(argcount);
for (int i = 0; i < argcount; i++) {
new_frame->localsplus[i] = stack_pointer[i];
}
for (int i = argcount; i < code->co_argcount; i++) {
PyObject *def = PyTuple_GET_ITEM(func->func_defaults,
i - minargs);
PyObject *def = PyTuple_GET_ITEM(func->func_defaults, i - min_args);
new_frame->localsplus[i] = Py_NewRef(def);
}
STACK_SHRINK(2-is_meth);
STACK_SHRINK(2 - is_meth);
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
DISPATCH_INLINED(new_frame);
}
Expand Down
20 changes: 11 additions & 9 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
case CALL_PY_EXACT_ARGS:
return oparg + 2;
case CALL_PY_WITH_DEFAULTS:
return -1;
return oparg + 2;
case CALL_NO_KW_TYPE_1:
return -1;
case CALL_NO_KW_STR_1:
Expand Down Expand Up @@ -639,7 +639,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
case CALL_PY_EXACT_ARGS:
return 1;
case CALL_PY_WITH_DEFAULTS:
return -1;
return 1;
case CALL_NO_KW_TYPE_1:
return -1;
case CALL_NO_KW_STR_1:
Expand Down Expand Up @@ -845,7 +845,7 @@ struct opcode_metadata {
[CALL] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
[CALL_BOUND_METHOD_EXACT_ARGS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
[CALL_PY_EXACT_ARGS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
[CALL_PY_WITH_DEFAULTS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[CALL_PY_WITH_DEFAULTS] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC000 },
[CALL_NO_KW_TYPE_1] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[CALL_NO_KW_STR_1] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[CALL_NO_KW_TUPLE_1] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
Expand Down

0 comments on commit 2b105e8

Please sign in to comment.