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

This commit is contained in:
Irit Katriel 2023-02-03 14:40:45 +00:00 committed by GitHub
parent 04e06e20ee
commit 433fb3ef08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 54 deletions

View File

@ -2972,36 +2972,39 @@ dummy_func(
CHECK_EVAL_BREAKER(); CHECK_EVAL_BREAKER();
} }
// error: MAKE_FUNCTION has irregular stack effect inst(MAKE_FUNCTION, (defaults if (oparg & 0x01),
inst(MAKE_FUNCTION) { kwdefaults if (oparg & 0x02),
PyObject *codeobj = POP(); annotations if (oparg & 0x04),
PyFunctionObject *func = (PyFunctionObject *) closure if (oparg & 0x08),
codeobj -- func)) {
PyFunctionObject *func_obj = (PyFunctionObject *)
PyFunction_New(codeobj, GLOBALS()); PyFunction_New(codeobj, GLOBALS());
Py_DECREF(codeobj); Py_DECREF(codeobj);
if (func == NULL) { if (func_obj == NULL) {
goto error; goto error;
} }
if (oparg & 0x08) { if (oparg & 0x08) {
assert(PyTuple_CheckExact(TOP())); assert(PyTuple_CheckExact(closure));
func->func_closure = POP(); func_obj->func_closure = closure;
} }
if (oparg & 0x04) { if (oparg & 0x04) {
assert(PyTuple_CheckExact(TOP())); assert(PyTuple_CheckExact(annotations));
func->func_annotations = POP(); func_obj->func_annotations = annotations;
} }
if (oparg & 0x02) { if (oparg & 0x02) {
assert(PyDict_CheckExact(TOP())); assert(PyDict_CheckExact(kwdefaults));
func->func_kwdefaults = POP(); func_obj->func_kwdefaults = kwdefaults;
} }
if (oparg & 0x01) { if (oparg & 0x01) {
assert(PyTuple_CheckExact(TOP())); assert(PyTuple_CheckExact(defaults));
func->func_defaults = POP(); func_obj->func_defaults = defaults;
} }
func->func_version = ((PyCodeObject *)codeobj)->co_version; func_obj->func_version = ((PyCodeObject *)codeobj)->co_version;
PUSH((PyObject *)func); func = (PyObject *)func_obj;
} }
inst(RETURN_GENERATOR, (--)) { inst(RETURN_GENERATOR, (--)) {
@ -3027,22 +3030,12 @@ dummy_func(
goto resume_frame; goto resume_frame;
} }
// error: BUILD_SLICE has irregular stack effect inst(BUILD_SLICE, (start, stop, step if (oparg == 3) -- slice)) {
inst(BUILD_SLICE) {
PyObject *start, *stop, *step, *slice;
if (oparg == 3)
step = POP();
else
step = NULL;
stop = POP();
start = TOP();
slice = PySlice_New(start, stop, step); slice = PySlice_New(start, stop, step);
Py_DECREF(start); Py_DECREF(start);
Py_DECREF(stop); Py_DECREF(stop);
Py_XDECREF(step); Py_XDECREF(step);
SET_TOP(slice); ERROR_IF(slice == NULL, error);
if (slice == NULL)
goto error;
} }
// error: FORMAT_VALUE has irregular stack effect // error: FORMAT_VALUE has irregular stack effect

View File

@ -3584,34 +3584,42 @@
} }
TARGET(MAKE_FUNCTION) { TARGET(MAKE_FUNCTION) {
PyObject *codeobj = POP(); PyObject *codeobj = PEEK(1);
PyFunctionObject *func = (PyFunctionObject *) PyObject *closure = (oparg & 0x08) ? PEEK(1 + ((oparg & 0x08) ? 1 : 0)) : NULL;
PyObject *annotations = (oparg & 0x04) ? PEEK(1 + ((oparg & 0x08) ? 1 : 0) + ((oparg & 0x04) ? 1 : 0)) : NULL;
PyObject *kwdefaults = (oparg & 0x02) ? PEEK(1 + ((oparg & 0x08) ? 1 : 0) + ((oparg & 0x04) ? 1 : 0) + ((oparg & 0x02) ? 1 : 0)) : NULL;
PyObject *defaults = (oparg & 0x01) ? PEEK(1 + ((oparg & 0x08) ? 1 : 0) + ((oparg & 0x04) ? 1 : 0) + ((oparg & 0x02) ? 1 : 0) + ((oparg & 0x01) ? 1 : 0)) : NULL;
PyObject *func;
PyFunctionObject *func_obj = (PyFunctionObject *)
PyFunction_New(codeobj, GLOBALS()); PyFunction_New(codeobj, GLOBALS());
Py_DECREF(codeobj); Py_DECREF(codeobj);
if (func == NULL) { if (func_obj == NULL) {
goto error; goto error;
} }
if (oparg & 0x08) { if (oparg & 0x08) {
assert(PyTuple_CheckExact(TOP())); assert(PyTuple_CheckExact(closure));
func->func_closure = POP(); func_obj->func_closure = closure;
} }
if (oparg & 0x04) { if (oparg & 0x04) {
assert(PyTuple_CheckExact(TOP())); assert(PyTuple_CheckExact(annotations));
func->func_annotations = POP(); func_obj->func_annotations = annotations;
} }
if (oparg & 0x02) { if (oparg & 0x02) {
assert(PyDict_CheckExact(TOP())); assert(PyDict_CheckExact(kwdefaults));
func->func_kwdefaults = POP(); func_obj->func_kwdefaults = kwdefaults;
} }
if (oparg & 0x01) { if (oparg & 0x01) {
assert(PyTuple_CheckExact(TOP())); assert(PyTuple_CheckExact(defaults));
func->func_defaults = POP(); func_obj->func_defaults = defaults;
} }
func->func_version = ((PyCodeObject *)codeobj)->co_version; func_obj->func_version = ((PyCodeObject *)codeobj)->co_version;
PUSH((PyObject *)func); func = (PyObject *)func_obj;
STACK_SHRINK(((oparg & 0x01) ? 1 : 0) + ((oparg & 0x02) ? 1 : 0) + ((oparg & 0x04) ? 1 : 0) + ((oparg & 0x08) ? 1 : 0));
POKE(1, func);
DISPATCH(); DISPATCH();
} }
@ -3639,20 +3647,18 @@
} }
TARGET(BUILD_SLICE) { TARGET(BUILD_SLICE) {
PyObject *start, *stop, *step, *slice; PyObject *step = (oparg == 3) ? PEEK(((oparg == 3) ? 1 : 0)) : NULL;
if (oparg == 3) PyObject *stop = PEEK(1 + ((oparg == 3) ? 1 : 0));
step = POP(); PyObject *start = PEEK(2 + ((oparg == 3) ? 1 : 0));
else PyObject *slice;
step = NULL;
stop = POP();
start = TOP();
slice = PySlice_New(start, stop, step); slice = PySlice_New(start, stop, step);
Py_DECREF(start); Py_DECREF(start);
Py_DECREF(stop); Py_DECREF(stop);
Py_XDECREF(step); Py_XDECREF(step);
SET_TOP(slice); if (slice == NULL) { STACK_SHRINK(((oparg == 3) ? 1 : 0)); goto pop_2_error; }
if (slice == NULL) STACK_SHRINK(((oparg == 3) ? 1 : 0));
goto error; STACK_SHRINK(1);
POKE(1, slice);
DISPATCH(); DISPATCH();
} }

View File

@ -325,11 +325,11 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
case CALL_FUNCTION_EX: case CALL_FUNCTION_EX:
return -1; return -1;
case MAKE_FUNCTION: case MAKE_FUNCTION:
return -1; return ((oparg & 0x01) ? 1 : 0) + ((oparg & 0x02) ? 1 : 0) + ((oparg & 0x04) ? 1 : 0) + ((oparg & 0x08) ? 1 : 0) + 1;
case RETURN_GENERATOR: case RETURN_GENERATOR:
return 0; return 0;
case BUILD_SLICE: case BUILD_SLICE:
return -1; return ((oparg == 3) ? 1 : 0) + 2;
case FORMAT_VALUE: case FORMAT_VALUE:
return -1; return -1;
case COPY: case COPY:
@ -671,11 +671,11 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
case CALL_FUNCTION_EX: case CALL_FUNCTION_EX:
return -1; return -1;
case MAKE_FUNCTION: case MAKE_FUNCTION:
return -1; return 1;
case RETURN_GENERATOR: case RETURN_GENERATOR:
return 0; return 0;
case BUILD_SLICE: case BUILD_SLICE:
return -1; return 1;
case FORMAT_VALUE: case FORMAT_VALUE:
return -1; return -1;
case COPY: case COPY: