8000 gh-98831: rewrite MAKE_FUNCTION and BUILD_SLICE in the instruction definition DSL by iritkatriel · Pull Request #101529 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-98831: rewrite MAKE_FUNCTION and BUILD_SLICE in the instruction definition DSL #101529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
MAKE_FUNCTION
  • Loading branch information
iritkatriel committed Feb 3, 2023
commit 6a05302de4961732c3026da148884bd8b99fc941
33 changes: 18 additions & 15 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2972,36 +2972,39 @@ dummy_func(
CHECK_EVAL_BREAKER();
}

// error: MAKE_FUNCTION has irregular stack effect
inst(MAKE_FUNCTION) {
PyObject *codeobj = POP();
PyFunctionObject *func = (PyFunctionObject *)
inst(MAKE_FUNCTION, (defaults if (oparg & 0x01),
kwdefaults if (oparg & 0x02),
annotations if (oparg & 0x04),
closure if (oparg & 0x08),
codeobj -- func)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too bad we can't write func: PyFunctionObject * yet -- I've gotta add that to the grammar, it would add the casts and save the ugly extra func_obj variable.


PyFunctionObject *func_obj = (PyFunctionObject *)
PyFunction_New(codeobj, GLOBALS());

Py_DECREF(codeobj);
if (func == NULL) {
if (func_obj == NULL) {
goto error;
}

if (oparg & 0x08) {
assert(PyTuple_CheckExact(TOP()));
func->func_closure = POP();
assert(PyTuple_CheckExact(closure));
func_obj->func_closure = closure;
}
if (oparg & 0x04) {
assert(PyTuple_CheckExact(TOP()));
func->func_annotations = POP();
assert(PyTuple_CheckExact(annotations));
func_obj->func_annotations = annotations;
}
if (oparg & 0x02) {
assert(PyDict_CheckExact(TOP()));
func->func_kwdefaults = POP();
assert(PyDict_CheckExact(kwdefaults));
func_obj->func_kwdefaults = kwdefaults;
}
if (oparg & 0x01) {
assert(PyTuple_CheckExact(TOP()));
func->func_defaults = POP();
assert(PyTuple_CheckExact(defaults));
func_obj->func_defaults = defaults;
}

func->func_version = ((PyCodeObject *)codeobj)->co_version;
PUSH((PyObject *)func);
func_obj->func_version = ((PyCodeObject *)codeobj)->co_version;
func = (PyObject *)func_obj;
}

inst(RETURN_GENERATOR, (--)) {
Expand Down
34 changes: 21 additions & 13 deletions Python/generated_cases.c.h

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

4 changes: 2 additions & 2 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
case CALL_FUNCTION_EX:
return -1;
case MAKE_FUNCTION:
return -1;
return ((oparg & 0x01) ? 1 : 0) + ((oparg & 0x02) ? 1 : 0) + ((oparg & 0x04) ? 1 : 0) + ((oparg & 0 6A47 x08) ? 1 : 0) + 1;
case RETURN_GENERATOR:
return 0;
case BUILD_SLICE:
Expand Down Expand Up @@ -671,7 +671,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
case CALL_FUNCTION_EX:
return -1;
case MAKE_FUNCTION:
return -1;
return 1;
case RETURN_GENERATOR:
return 0;
case BUILD_SLICE:
Expand Down
0