Skip to content
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
Use DECREF_INPUTS() more
Also convert END_FOR (the only macro inst) to a regular inst.
  • Loading branch information
gvanrossum committed Mar 13, 2023
commit 778ca94656d461e0880428151408d199c977afd2
94 changes: 39 additions & 55 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ dummy_func(
res = NULL;
}

macro(END_FOR) = POP_TOP + POP_TOP;
inst(END_FOR, (second, first --)) {
DECREF_INPUTS();
}

inst(UNARY_NEGATIVE, (value -- res)) {
res = PyNumber_Negative(value);
Expand Down Expand Up @@ -384,8 +386,7 @@ dummy_func(
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetKeyError(sub);
}
Py_DECREF(dict);
Py_DECREF(sub);
DECREF_INPUTS();
ERROR_IF(true, error);
}
Py_INCREF(res); // Do this before DECREF'ing dict, sub
Expand Down Expand Up @@ -420,7 +421,7 @@ dummy_func(

inst(SET_ADD, (set, unused[oparg-1], v -- set, unused[oparg-1])) {
int err = PySet_Add(set, v);
Py_DECREF(v);
DECREF_INPUTS();
ERROR_IF(err, error);
PREDICT(JUMP_BACKWARD);
}
Expand Down Expand Up @@ -899,7 +900,7 @@ dummy_func(
#endif /* ENABLE_SPECIALIZATION */
PyObject **top = stack_pointer + oparg - 1;
int res = unpack_iterable(tstate, seq, oparg, -1, top);
Py_DECREF(seq);
DECREF_INPUTS();
ERROR_IF(res == 0, error);
}

Expand All @@ -910,7 +911,7 @@ dummy_func(
STAT_INC(UNPACK_SEQUENCE, hit);
values[0] = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
values[1] = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
Py_DECREF(seq);
DECREF_INPUTS();
}

inst(UNPACK_SEQUENCE_TUPLE, (unused/1, seq -- values[oparg])) {
Expand All @@ -921,7 +922,7 @@ dummy_func(
for (int i = oparg; --i >= 0; ) {
*values++ = Py_NewRef(items[i]);
}
Py_DECREF(seq);
DECREF_INPUTS();
}

inst(UNPACK_SEQUENCE_LIST, (unused/1, seq -- values[oparg])) {
Expand All @@ -932,14 +933,14 @@ dummy_func(
for (int i = oparg; --i >= 0; ) {
*values++ = Py_NewRef(items[i]);
}
Py_DECREF(seq);
DECREF_INPUTS();
}

inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
PyObject **top = stack_pointer + totalargs - 1;
int res = unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top);
Py_DECREF(seq);
DECREF_INPUTS();
ERROR_IF(res == 0, error);
}

Expand Down Expand Up @@ -967,22 +968,21 @@ dummy_func(
#endif /* ENABLE_SPECIALIZATION */
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
int err = PyObject_SetAttr(owner, name, v);
Py_DECREF(v);
Py_DECREF(owner);
DECREF_INPUTS();
ERROR_IF(err, error);
}

inst(DELETE_ATTR, (owner --)) {
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
int err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
Py_DECREF(owner);
DECREF_INPUTS();
ERROR_IF(err, error);
}

inst(STORE_GLOBAL, (v --)) {
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
int err = PyDict_SetItem(GLOBALS(), name, v);
Py_DECREF(v);
DECREF_INPUTS();
ERROR_IF(err, error);
}

Expand Down Expand Up @@ -1249,9 +1249,7 @@ dummy_func(

inst(BUILD_STRING, (pieces[oparg] -- str)) {
str = _PyUnicode_JoinArray(&_Py_STR(empty), pieces, oparg);
for (int i = 0; i < oparg; i++) {
Py_DECREF(pieces[i]);
}
DECREF_INPUTS();
ERROR_IF(str == NULL, error);
}

Expand Down Expand Up @@ -1314,10 +1312,7 @@ dummy_func(
if (map == NULL)
goto error;

for (int i = 0; i < oparg; i++) {
Py_DECREF(values[i*2]);
Py_DECREF(values[i*2+1]);
}
DECREF_INPUTS();
ERROR_IF(map == NULL, error);
}

Expand Down Expand Up @@ -1373,10 +1368,7 @@ dummy_func(
map = _PyDict_FromItems(
&PyTuple_GET_ITEM(keys, 0), 1,
values, 1, oparg);
Py_DECREF(keys);
for (int i = 0; i < oparg; i++) {
Py_DECREF(values[i]);
}
DECREF_INPUTS();
ERROR_IF(map == NULL, error);
}

Expand Down Expand Up @@ -1464,7 +1456,7 @@ dummy_func(

NULL | meth | arg1 | ... | argN
*/
Py_DECREF(owner);
DECREF_INPUTS();
ERROR_IF(meth == NULL, error);
res2 = NULL;
res = meth;
Expand All @@ -1473,7 +1465,7 @@ dummy_func(
else {
/* Classic, pushes one value. */
res = PyObject_GetAttr(owner, name);
Py_DECREF(owner);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}
}
Expand All @@ -1492,7 +1484,7 @@ dummy_func(
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(res);
res2 = NULL;
Py_DECREF(owner);
DECREF_INPUTS();
}

inst(LOAD_ATTR_MODULE, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
Expand All @@ -1509,7 +1501,7 @@ dummy_func(
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(res);
res2 = NULL;
Py_DECREF(owner);
DECREF_INPUTS();
}

inst(LOAD_ATTR_WITH_HINT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
Expand Down Expand Up @@ -1540,7 +1532,7 @@ dummy_func(
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(res);
res2 = NULL;
Py_DECREF(owner);
DECREF_INPUTS();
}

inst(LOAD_ATTR_SLOT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
Expand All @@ -1554,7 +1546,7 @@ dummy_func(
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(res);
res2 = NULL;
Py_DECREF(owner);
DECREF_INPUTS();
}

inst(LOAD_ATTR_CLASS, (unused/1, type_version/2, unused/2, descr/4, cls -- res2 if (oparg & 1), res)) {
Expand All @@ -1570,7 +1562,7 @@ dummy_func(
res = descr;
assert(res != NULL);
Py_INCREF(res);
Py_DECREF(cls);
DECREF_INPUTS();
}

inst(LOAD_ATTR_PROPERTY, (unused/1, type_version/2, func_version/2, fget/4, owner -- unused if (oparg & 1), unused)) {
Expand Down Expand Up @@ -1707,8 +1699,7 @@ dummy_func(
STAT_INC(COMPARE_OP, deferred);
assert((oparg >> 4) <= Py_GE);
res = PyObject_RichCompare(left, right, oparg>>4);
Py_DECREF(left);
Py_DECREF(right);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}

Expand All @@ -1734,8 +1725,7 @@ dummy_func(
#endif /* ENABLE_SPECIALIZATION */
assert((oparg >> 4) <= Py_GE);
PyObject *cond = PyObject_RichCompare(left, right, oparg>>4);
Py_DECREF(left);
Py_DECREF(right);
DECREF_INPUTS();
ERROR_IF(cond == NULL, error);
assert(next_instr[1].op.code == POP_JUMP_IF_FALSE ||
next_instr[1].op.code == POP_JUMP_IF_TRUE);
Expand Down Expand Up @@ -1885,7 +1875,7 @@ dummy_func(
}
else {
int err = PyObject_IsTrue(cond);
Py_DECREF(cond);
DECREF_INPUTS();
if (err == 0) {
JUMPBY(oparg);
}
Expand All @@ -1905,7 +1895,7 @@ dummy_func(
}
else {
int err = PyObject_IsTrue(cond);
Py_DECREF(cond);
DECREF_INPUTS();
if (err > 0) {
JUMPBY(oparg);
}
Expand All @@ -1917,7 +1907,7 @@ dummy_func(

inst(POP_JUMP_IF_NOT_NONE, (value -- )) {
if (!Py_IsNone(value)) {
Py_DECREF(value);
DECREF_INPUTS();
JUMPBY(oparg);
}
else {
Expand All @@ -1931,7 +1921,7 @@ dummy_func(
JUMPBY(oparg);
}
else {
Py_DECREF(value);
DECREF_INPUTS();
}
}

Expand All @@ -1948,7 +1938,7 @@ dummy_func(
else {
err = PyObject_IsTrue(cond);
if (err > 0) {
Py_DECREF(cond);
DECREF_INPUTS();
}
else if (err == 0) {
JUMPBY(oparg);
Expand Down Expand Up @@ -1977,7 +1967,7 @@ dummy_func(
jump = true;
}
else if (err == 0) {
Py_DECREF(cond);
DECREF_INPUTS();
}
else {
goto error;
Expand Down Expand Up @@ -2065,7 +2055,7 @@ dummy_func(
if (iter == NULL) {
goto error;
}
Py_DECREF(iterable);
DECREF_INPUTS();
}
PREDICT(LOAD_CONST);
}
Expand Down Expand Up @@ -2110,7 +2100,7 @@ dummy_func(
}
/* iterator ended normally */
assert(next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg].op.code == END_FOR);
Py_DECREF(iter);
DECREF_INPUTS();
STACK_SHRINK(1);
/* Jump forward oparg, then skip following END_FOR instruction */
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
Expand All @@ -2133,7 +2123,7 @@ dummy_func(
it->it_seq = NULL;
Py_DECREF(seq);
}
Py_DECREF(iter);
DECREF_INPUTS();
STACK_SHRINK(1);
/* Jump forward oparg, then skip following END_FOR instruction */
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
Expand All @@ -2156,7 +2146,7 @@ dummy_func(
it->it_seq = NULL;
Py_DECREF(seq);
}
Py_DECREF(iter);
DECREF_INPUTS();
STACK_SHRINK(1);
/* Jump forward oparg, then skip following END_FOR instruction */
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
Expand Down Expand Up @@ -2940,9 +2930,7 @@ dummy_func(
assert(PyTuple_CheckExact(callargs));

result = do_call_core(tstate, func, callargs, kwargs, cframe.use_tracing);
Py_DECREF(func);
Py_DECREF(callargs);
Py_XDECREF(kwargs);
DECREF_INPUTS();

assert(PEEK(3 + (oparg & 1)) == NULL);
ERROR_IF(result == NULL, error);
Expand Down Expand Up @@ -3009,9 +2997,7 @@ dummy_func(

inst(BUILD_SLICE, (start, stop, step if (oparg == 3) -- slice)) {
slice = PySlice_New(start, stop, step);
Py_DECREF(start);
Py_DECREF(stop);
Py_XDECREF(step);
DECREF_INPUTS();
ERROR_IF(slice == NULL, error);
}

Expand Down Expand Up @@ -3057,8 +3043,7 @@ dummy_func(
} else {
/* Actually call format(). */
result = PyObject_Format(value, fmt_spec);
Py_DECREF(value);
Py_XDECREF(fmt_spec);
DECREF_INPUTS();
ERROR_IF(result == NULL, error);
}
}
Expand All @@ -3084,8 +3069,7 @@ dummy_func(
assert((unsigned)oparg < Py_ARRAY_LENGTH(binary_ops));
assert(binary_ops[oparg]);
res = binary_ops[oparg](lhs, rhs);
Py_DECREF(lhs);
Py_DECREF(rhs);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}

Expand Down
Loading