Skip to content
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

gh-98831: rewrite GET_LEN, GET_ITER, BEFORE_WITH and a few simple opcodes in the instruction definition DSL #101443

Merged
merged 5 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
57 changes: 21 additions & 36 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2053,8 +2053,7 @@ dummy_func(
}
}

// stack effect: ( -- )
inst(JUMP_BACKWARD_NO_INTERRUPT) {
inst(JUMP_BACKWARD_NO_INTERRUPT, (--)) {
/* This bytecode is used in the `yield from` or `await` loop.
* If there is an interrupt, we want it handled in the innermost
* generator or coroutine, so we deliberately do not check it here.
Expand All @@ -2063,18 +2062,12 @@ dummy_func(
JUMPBY(-oparg);
}

// stack effect: ( -- __0)
inst(GET_LEN) {
inst(GET_LEN, (obj -- obj, len_o)) {
// PUSH(len(TOS))
Py_ssize_t len_i = PyObject_Length(TOP());
if (len_i < 0) {
goto error;
}
PyObject *len_o = PyLong_FromSsize_t(len_i);
if (len_o == NULL) {
goto error;
}
PUSH(len_o);
Py_ssize_t len_i = PyObject_Length(obj);
ERROR_IF(len_i < 0, error);
len_o = PyLong_FromSsize_t(len_i);
ERROR_IF(len_o == NULL, error);
}

inst(MATCH_CLASS, (subject, type, names -- attrs)) {
Expand Down Expand Up @@ -2110,15 +2103,11 @@ dummy_func(
ERROR_IF(values_or_none == NULL, error);
}

// stack effect: ( -- )
inst(GET_ITER) {
inst(GET_ITER, (iterable -- iter)) {
/* before: [obj]; after [getiter(obj)] */
PyObject *iterable = TOP();
PyObject *iter = PyObject_GetIter(iterable);
Py_DECREF(iterable);
SET_TOP(iter);
if (iter == NULL)
goto error;
iter = PyObject_GetIter(iterable);
DECREF_INPUTS();
ERROR_IF(iter == NULL, error);
}

// stack effect: ( -- )
Expand Down Expand Up @@ -2313,10 +2302,10 @@ dummy_func(
PREDICT(GET_AWAITABLE);
}

// stack effect: ( -- __0)
inst(BEFORE_WITH) {
PyObject *mgr = TOP();
PyObject *res;
inst(BEFORE_WITH, (mgr -- exit, res)) {
/* pop the context manager, push its __exit__ and the
* value returned from calling its __enter__
*/
PyObject *enter = _PyObject_LookupSpecial(mgr, &_Py_ID(__enter__));
if (enter == NULL) {
if (!_PyErr_Occurred(tstate)) {
Expand All @@ -2327,7 +2316,7 @@ dummy_func(
}
goto error;
}
PyObject *exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__exit__));
exit = _PyObject_LookupSpecial(mgr, &_Py_ID(__exit__));
if (exit == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
Expand All @@ -2339,14 +2328,13 @@ dummy_func(
Py_DECREF(enter);
goto error;
}
SET_TOP(exit);
Py_DECREF(mgr);
DECREF_INPUTS();
res = _PyObject_CallNoArgs(enter);
Py_DECREF(enter);
if (res == NULL) {
goto error;
Py_DECREF(exit);
ERROR_IF(true, error);
}
PUSH(res);
}

inst(WITH_EXCEPT_START, (exit_func, lasti, unused, val -- exit_func, lasti, unused, val, res)) {
Expand Down Expand Up @@ -2469,8 +2457,7 @@ dummy_func(
GO_TO_INSTRUCTION(CALL_PY_EXACT_ARGS);
}

// stack effect: ( -- )
inst(KW_NAMES) {
inst(KW_NAMES, (--)) {
assert(kwnames == NULL);
assert(oparg < PyTuple_GET_SIZE(consts));
kwnames = GETITEM(consts, oparg);
Expand Down Expand Up @@ -3252,8 +3239,7 @@ dummy_func(
PEEK(oparg) = top;
}

// stack effect: ( -- )
inst(EXTENDED_ARG) {
inst(EXTENDED_ARG, (--)) {
assert(oparg);
assert(cframe.use_tracing == 0);
opcode = _Py_OPCODE(*next_instr);
Expand All @@ -3262,8 +3248,7 @@ dummy_func(
DISPATCH_GOTO();
}

// stack effect: ( -- )
inst(CACHE) {
inst(CACHE, (--)) {
Py_UNREACHABLE();
}

Expand Down
43 changes: 24 additions & 19 deletions Python/generated_cases.c.h

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

28 changes: 14 additions & 14 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ _PyOpcode_num_popped(int opcode, int oparg) {
case JUMP_IF_TRUE_OR_POP:
return -1;
case JUMP_BACKWARD_NO_INTERRUPT:
return -1;
return 0;
case GET_LEN:
return -1;
return 1;
case MATCH_CLASS:
return 3;
case MATCH_MAPPING:
Expand All @@ -257,7 +257,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
case MATCH_KEYS:
return 2;
case GET_ITER:
return -1;
return 1;
case GET_YIELD_FROM_ITER:
return -1;
case FOR_ITER:
Expand All @@ -273,7 +273,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
case BEFORE_ASYNC_WITH:
return -1;
case BEFORE_WITH:
return -1;
return 1;
case WITH_EXCEPT_START:
return 4;
case PUSH_EXC_INFO:
Expand All @@ -287,7 +287,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
case CALL_BOUND_METHOD_EXACT_ARGS:
return -1;
case KW_NAMES:
return -1;
return 0;
case CALL:
return -1;
case CALL_PY_EXACT_ARGS:
Expand Down Expand Up @@ -339,9 +339,9 @@ _PyOpcode_num_popped(int opcode, int oparg) {
case SWAP:
return -1;
case EXTENDED_ARG:
return -1;
return 0;
case CACHE:
return -1;
return 0;
default:
Py_UNREACHABLE();
}
Expand Down Expand Up @@ -591,9 +591,9 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
case JUMP_IF_TRUE_OR_POP:
return -1;
case JUMP_BACKWARD_NO_INTERRUPT:
return -1;
return 0;
case GET_LEN:
return -1;
return 2;
case MATCH_CLASS:
return 1;
case MATCH_MAPPING:
Expand All @@ -603,7 +603,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
case MATCH_KEYS:
return 3;
case GET_ITER:
return -1;
return 1;
case GET_YIELD_FROM_ITER:
return -1;
case FOR_ITER:
Expand All @@ -619,7 +619,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
case BEFORE_ASYNC_WITH:
return -1;
case BEFORE_WITH:
return -1;
return 2;
case WITH_EXCEPT_START:
return 5;
case PUSH_EXC_INFO:
Expand All @@ -633,7 +633,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
case CALL_BOUND_METHOD_EXACT_ARGS:
return -1;
case KW_NAMES:
return -1;
return 0;
case CALL:
return -1;
case CALL_PY_EXACT_ARGS:
Expand Down Expand Up @@ -685,9 +685,9 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
case SWAP:
return -1;
case EXTENDED_ARG:
return -1;
return 0;
case CACHE:
return -1;
return 0;
default:
Py_UNREACHABLE();
}
Expand Down