mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
GH-106008: Make implicit boolean conversions explicit (GH-106003)
This commit is contained in:
parent
6e9f83d9ae
commit
7b2d94d875
20 changed files with 1728 additions and 1152 deletions
|
@ -279,15 +279,90 @@ dummy_func(
|
|||
}
|
||||
|
||||
inst(UNARY_NOT, (value -- res)) {
|
||||
assert(PyBool_Check(value));
|
||||
res = Py_IsFalse(value) ? Py_True : Py_False;
|
||||
}
|
||||
|
||||
family(to_bool, INLINE_CACHE_ENTRIES_TO_BOOL) = {
|
||||
TO_BOOL,
|
||||
TO_BOOL_ALWAYS_TRUE,
|
||||
TO_BOOL_BOOL,
|
||||
TO_BOOL_INT,
|
||||
TO_BOOL_LIST,
|
||||
TO_BOOL_NONE,
|
||||
TO_BOOL_STR,
|
||||
};
|
||||
|
||||
inst(TO_BOOL, (unused/1, unused/2, value -- res)) {
|
||||
#if ENABLE_SPECIALIZATION
|
||||
_PyToBoolCache *cache = (_PyToBoolCache *)next_instr;
|
||||
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
|
||||
next_instr--;
|
||||
_Py_Specialize_ToBool(value, next_instr);
|
||||
DISPATCH_SAME_OPARG();
|
||||
}
|
||||
STAT_INC(TO_BOOL, deferred);
|
||||
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
|
||||
#endif /* ENABLE_SPECIALIZATION */
|
||||
int err = PyObject_IsTrue(value);
|
||||
DECREF_INPUTS();
|
||||
ERROR_IF(err < 0, error);
|
||||
if (err == 0) {
|
||||
res = Py_True;
|
||||
}
|
||||
else {
|
||||
res = err ? Py_True : Py_False;
|
||||
}
|
||||
|
||||
inst(TO_BOOL_BOOL, (unused/1, unused/2, value -- value)) {
|
||||
DEOPT_IF(!PyBool_Check(value), TO_BOOL);
|
||||
STAT_INC(TO_BOOL, hit);
|
||||
}
|
||||
|
||||
inst(TO_BOOL_INT, (unused/1, unused/2, value -- res)) {
|
||||
DEOPT_IF(!PyLong_CheckExact(value), TO_BOOL);
|
||||
STAT_INC(TO_BOOL, hit);
|
||||
if (_PyLong_IsZero((PyLongObject *)value)) {
|
||||
assert(_Py_IsImmortal(value));
|
||||
res = Py_False;
|
||||
}
|
||||
else {
|
||||
DECREF_INPUTS();
|
||||
res = Py_True;
|
||||
}
|
||||
}
|
||||
|
||||
inst(TO_BOOL_LIST, (unused/1, unused/2, value -- res)) {
|
||||
DEOPT_IF(!PyList_CheckExact(value), TO_BOOL);
|
||||
STAT_INC(TO_BOOL, hit);
|
||||
res = Py_SIZE(value) ? Py_True : Py_False;
|
||||
DECREF_INPUTS();
|
||||
}
|
||||
|
||||
inst(TO_BOOL_NONE, (unused/1, unused/2, value -- res)) {
|
||||
// This one is a bit weird, because we expect *some* failures:
|
||||
DEOPT_IF(!Py_IsNone(value), TO_BOOL);
|
||||
STAT_INC(TO_BOOL, hit);
|
||||
res = Py_False;
|
||||
}
|
||||
|
||||
inst(TO_BOOL_STR, (unused/1, unused/2, value -- res)) {
|
||||
DEOPT_IF(!PyUnicode_CheckExact(value), TO_BOOL);
|
||||
STAT_INC(TO_BOOL, hit);
|
||||
if (value == &_Py_STR(empty)) {
|
||||
assert(_Py_IsImmortal(value));
|
||||
res = Py_False;
|
||||
}
|
||||
else {
|
||||
assert(Py_SIZE(value));
|
||||
DECREF_INPUTS();
|
||||
res = Py_True;
|
||||
}
|
||||
}
|
||||
|
||||
inst(TO_BOOL_ALWAYS_TRUE, (unused/1, version/2, value -- res)) {
|
||||
// This one is a bit weird, because we expect *some* failures:
|
||||
assert(version);
|
||||
DEOPT_IF(Py_TYPE(value)->tp_version_tag != version, TO_BOOL);
|
||||
STAT_INC(TO_BOOL, hit);
|
||||
DECREF_INPUTS();
|
||||
res = Py_True;
|
||||
}
|
||||
|
||||
inst(UNARY_INVERT, (value -- res)) {
|
||||
|
@ -2024,10 +2099,16 @@ dummy_func(
|
|||
STAT_INC(COMPARE_OP, deferred);
|
||||
DECREMENT_ADAPTIVE_COUNTER(cache->counter);
|
||||
#endif /* ENABLE_SPECIALIZATION */
|
||||
assert((oparg >> 4) <= Py_GE);
|
||||
res = PyObject_RichCompare(left, right, oparg>>4);
|
||||
assert((oparg >> 5) <= Py_GE);
|
||||
res = PyObject_RichCompare(left, right, oparg >> 5);
|
||||
DECREF_INPUTS();
|
||||
ERROR_IF(res == NULL, error);
|
||||
if (oparg & 16) {
|
||||
int res_bool = PyObject_IsTrue(res);
|
||||
Py_DECREF(res);
|
||||
ERROR_IF(res_bool < 0, error);
|
||||
res = res_bool ? Py_True : Py_False;
|
||||
}
|
||||
}
|
||||
|
||||
inst(COMPARE_OP_FLOAT, (unused/1, left, right -- res)) {
|
||||
|
@ -2041,6 +2122,7 @@ dummy_func(
|
|||
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
|
||||
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
|
||||
res = (sign_ish & oparg) ? Py_True : Py_False;
|
||||
// It's always a bool, so we don't care about oparg & 16.
|
||||
}
|
||||
|
||||
// Similar to COMPARE_OP_FLOAT
|
||||
|
@ -2059,6 +2141,7 @@ dummy_func(
|
|||
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
|
||||
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
|
||||
res = (sign_ish & oparg) ? Py_True : Py_False;
|
||||
// It's always a bool, so we don't care about oparg & 16.
|
||||
}
|
||||
|
||||
// Similar to COMPARE_OP_FLOAT, but for ==, != only
|
||||
|
@ -2067,13 +2150,14 @@ dummy_func(
|
|||
DEOPT_IF(!PyUnicode_CheckExact(right), COMPARE_OP);
|
||||
STAT_INC(COMPARE_OP, hit);
|
||||
int eq = _PyUnicode_Equal(left, right);
|
||||
assert((oparg >>4) == Py_EQ || (oparg >>4) == Py_NE);
|
||||
assert((oparg >> 5) == Py_EQ || (oparg >> 5) == Py_NE);
|
||||
_Py_DECREF_SPECIALIZED(left, _PyUnicode_ExactDealloc);
|
||||
_Py_DECREF_SPECIALIZED(right, _PyUnicode_ExactDealloc);
|
||||
assert(eq == 0 || eq == 1);
|
||||
assert((oparg & 0xf) == COMPARISON_NOT_EQUALS || (oparg & 0xf) == COMPARISON_EQUALS);
|
||||
assert(COMPARISON_NOT_EQUALS + 1 == COMPARISON_EQUALS);
|
||||
res = ((COMPARISON_NOT_EQUALS + eq) & oparg) ? Py_True : Py_False;
|
||||
// It's always a bool, so we don't care about oparg & 16.
|
||||
}
|
||||
|
||||
inst(IS_OP, (left, right -- b)) {
|
||||
|
@ -2183,35 +2267,13 @@ dummy_func(
|
|||
}
|
||||
|
||||
inst(POP_JUMP_IF_FALSE, (cond -- )) {
|
||||
if (Py_IsFalse(cond)) {
|
||||
JUMPBY(oparg);
|
||||
}
|
||||
else if (!Py_IsTrue(cond)) {
|
||||
int err = PyObject_IsTrue(cond);
|
||||
DECREF_INPUTS();
|
||||
if (err == 0) {
|
||||
JUMPBY(oparg);
|
||||
}
|
||||
else {
|
||||
ERROR_IF(err < 0, error);
|
||||
}
|
||||
}
|
||||
assert(PyBool_Check(cond));
|
||||
JUMPBY(oparg * Py_IsFalse(cond));
|
||||
}
|
||||
|
||||
inst(POP_JUMP_IF_TRUE, (cond -- )) {
|
||||
if (Py_IsTrue(cond)) {
|
||||
JUMPBY(oparg);
|
||||
}
|
||||
else if (!Py_IsFalse(cond)) {
|
||||
int err = PyObject_IsTrue(cond);
|
||||
DECREF_INPUTS();
|
||||
if (err > 0) {
|
||||
JUMPBY(oparg);
|
||||
}
|
||||
else {
|
||||
ERROR_IF(err < 0, error);
|
||||
}
|
||||
}
|
||||
assert(PyBool_Check(cond));
|
||||
JUMPBY(oparg * Py_IsTrue(cond));
|
||||
}
|
||||
|
||||
inst(POP_JUMP_IF_NOT_NONE, (value -- )) {
|
||||
|
@ -3515,23 +3577,17 @@ dummy_func(
|
|||
|
||||
inst(INSTRUMENTED_POP_JUMP_IF_TRUE, ( -- )) {
|
||||
PyObject *cond = POP();
|
||||
int err = PyObject_IsTrue(cond);
|
||||
Py_DECREF(cond);
|
||||
ERROR_IF(err < 0, error);
|
||||
_Py_CODEUNIT *here = next_instr-1;
|
||||
assert(err == 0 || err == 1);
|
||||
int offset = err*oparg;
|
||||
assert(PyBool_Check(cond));
|
||||
_Py_CODEUNIT *here = next_instr - 1;
|
||||
int offset = Py_IsTrue(cond) * oparg;
|
||||
INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
|
||||
}
|
||||
|
||||
inst(INSTRUMENTED_POP_JUMP_IF_FALSE, ( -- )) {
|
||||
PyObject *cond = POP();
|
||||
int err = PyObject_IsTrue(cond);
|
||||
Py_DECREF(cond);
|
||||
ERROR_IF(err < 0, error);
|
||||
_Py_CODEUNIT *here = next_instr-1;
|
||||
assert(err == 0 || err == 1);
|
||||
int offset = (1-err)*oparg;
|
||||
assert(PyBool_Check(cond));
|
||||
_Py_CODEUNIT *here = next_instr - 1;
|
||||
int offset = Py_IsFalse(cond) * oparg;
|
||||
INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
|
||||
}
|
||||
|
||||
|
@ -3558,7 +3614,7 @@ dummy_func(
|
|||
}
|
||||
else {
|
||||
Py_DECREF(value);
|
||||
offset = oparg;
|
||||
offset = oparg;
|
||||
}
|
||||
INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
|
||||
}
|
||||
|
|
|
@ -2790,9 +2790,11 @@ static int compiler_addcompare(struct compiler *c, location loc,
|
|||
default:
|
||||
Py_UNREACHABLE();
|
||||
}
|
||||
/* cmp goes in top bits of the oparg, while the low bits are used by quickened
|
||||
* versions of this opcode to store the comparison mask. */
|
||||
ADDOP_I(c, loc, COMPARE_OP, (cmp << 4) | compare_masks[cmp]);
|
||||
// cmp goes in top three bits of the oparg, while the low four bits are used
|
||||
// by quickened versions of this opcode to store the comparison mask. The
|
||||
// fifth-lowest bit indicates whether the result should be converted to bool
|
||||
// and is set later):
|
||||
ADDOP_I(c, loc, COMPARE_OP, (cmp << 5) | compare_masks[cmp]);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -2858,10 +2860,12 @@ compiler_jump_if(struct compiler *c, location loc,
|
|||
ADDOP_I(c, LOC(e), SWAP, 2);
|
||||
ADDOP_I(c, LOC(e), COPY, 2);
|
||||
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, i));
|
||||
ADDOP(c, LOC(e), TO_BOOL);
|
||||
ADDOP_JUMP(c, LOC(e), POP_JUMP_IF_FALSE, cleanup);
|
||||
}
|
||||
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
|
||||
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, n));
|
||||
ADDOP(c, LOC(e), TO_BOOL);
|
||||
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
|
||||
NEW_JUMP_TARGET_LABEL(c, end);
|
||||
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
|
||||
|
@ -2885,6 +2889,7 @@ compiler_jump_if(struct compiler *c, location loc,
|
|||
|
||||
/* general implementation */
|
||||
VISIT(c, expr, e);
|
||||
ADDOP(c, LOC(e), TO_BOOL);
|
||||
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
@ -4016,8 +4021,6 @@ unaryop(unaryop_ty op)
|
|||
switch (op) {
|
||||
case Invert:
|
||||
return UNARY_INVERT;
|
||||
case Not:
|
||||
return UNARY_NOT;
|
||||
case USub:
|
||||
return UNARY_NEGATIVE;
|
||||
default:
|
||||
|
@ -4247,6 +4250,7 @@ compiler_boolop(struct compiler *c, expr_ty e)
|
|||
for (i = 0; i < n; ++i) {
|
||||
VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
|
||||
ADDOP_I(c, loc, COPY, 1);
|
||||
ADDOP(c, loc, TO_BOOL);
|
||||
ADDOP_JUMP(c, loc, jumpi, end);
|
||||
ADDOP(c, loc, POP_TOP);
|
||||
}
|
||||
|
@ -4554,6 +4558,7 @@ compiler_compare(struct compiler *c, expr_ty e)
|
|||
ADDOP_I(c, loc, COPY, 2);
|
||||
ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, i));
|
||||
ADDOP_I(c, loc, COPY, 1);
|
||||
ADDOP(c, loc, TO_BOOL);
|
||||
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, cleanup);
|
||||
ADDOP(c, loc, POP_TOP);
|
||||
}
|
||||
|
@ -5789,6 +5794,7 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
|
|||
static int
|
||||
compiler_with_except_finish(struct compiler *c, jump_target_label cleanup) {
|
||||
NEW_JUMP_TARGET_LABEL(c, suppress);
|
||||
ADDOP(c, NO_LOCATION, TO_BOOL);
|
||||
ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress);
|
||||
ADDOP_I(c, NO_LOCATION, RERAISE, 2);
|
||||
|
||||
|
@ -6022,6 +6028,10 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
|
|||
if (e->v.UnaryOp.op == UAdd) {
|
||||
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_UNARY_POSITIVE);
|
||||
}
|
||||
else if (e->v.UnaryOp.op == Not) {
|
||||
ADDOP(c, loc, TO_BOOL);
|
||||
ADDOP(c, loc, UNARY_NOT);
|
||||
}
|
||||
else {
|
||||
ADDOP(c, loc, unaryop(e->v.UnaryOp.op));
|
||||
}
|
||||
|
@ -7197,6 +7207,7 @@ compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
|
|||
}
|
||||
VISIT(c, expr, value);
|
||||
ADDOP_COMPARE(c, LOC(p), Eq);
|
||||
ADDOP(c, LOC(p), TO_BOOL);
|
||||
RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
return SUCCESS;
|
||||
}
|
||||
|
|
576
Python/executor_cases.c.h
generated
576
Python/executor_cases.c.h
generated
File diff suppressed because it is too large
Load diff
|
@ -1044,6 +1044,36 @@ get_const_value(int opcode, int oparg, PyObject *co_consts)
|
|||
return Py_NewRef(constant);
|
||||
}
|
||||
|
||||
// Steals a reference to newconst.
|
||||
static int
|
||||
add_const(PyObject *newconst, PyObject *consts, PyObject *const_cache)
|
||||
{
|
||||
if (_PyCompile_ConstCacheMergeOne(const_cache, &newconst) < 0) {
|
||||
Py_DECREF(newconst);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Py_ssize_t index;
|
||||
for (index = 0; index < PyList_GET_SIZE(consts); index++) {
|
||||
if (PyList_GET_ITEM(consts, index) == newconst) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index == PyList_GET_SIZE(consts)) {
|
||||
if ((size_t)index >= (size_t)INT_MAX - 1) {
|
||||
PyErr_SetString(PyExc_OverflowError, "too many constants");
|
||||
Py_DECREF(newconst);
|
||||
return -1;
|
||||
}
|
||||
if (PyList_Append(consts, newconst)) {
|
||||
Py_DECREF(newconst);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
Py_DECREF(newconst);
|
||||
return (int)index;
|
||||
}
|
||||
|
||||
/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
|
||||
with LOAD_CONST (c1, c2, ... cn).
|
||||
The consts table must still be in list form so that the
|
||||
|
@ -1081,33 +1111,14 @@ fold_tuple_on_constants(PyObject *const_cache,
|
|||
}
|
||||
PyTuple_SET_ITEM(newconst, i, constant);
|
||||
}
|
||||
if (_PyCompile_ConstCacheMergeOne(const_cache, &newconst) < 0) {
|
||||
Py_DECREF(newconst);
|
||||
int index = add_const(newconst, consts, const_cache);
|
||||
if (index < 0) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
Py_ssize_t index;
|
||||
for (index = 0; index < PyList_GET_SIZE(consts); index++) {
|
||||
if (PyList_GET_ITEM(consts, index) == newconst) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index == PyList_GET_SIZE(consts)) {
|
||||
if ((size_t)index >= (size_t)INT_MAX - 1) {
|
||||
Py_DECREF(newconst);
|
||||
PyErr_SetString(PyExc_OverflowError, "too many constants");
|
||||
return ERROR;
|
||||
}
|
||||
if (PyList_Append(consts, newconst)) {
|
||||
Py_DECREF(newconst);
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
Py_DECREF(newconst);
|
||||
for (int i = 0; i < n; i++) {
|
||||
INSTR_SET_OP0(&inst[i], NOP);
|
||||
}
|
||||
INSTR_SET_OP1(&inst[n], LOAD_CONST, (int)index);
|
||||
INSTR_SET_OP1(&inst[n], LOAD_CONST, index);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -1361,24 +1372,71 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
|
|||
}
|
||||
break;
|
||||
case IS_OP:
|
||||
// Fold to POP_JUMP_IF_NONE:
|
||||
// - LOAD_CONST(None) IS_OP(0) POP_JUMP_IF_TRUE
|
||||
// - LOAD_CONST(None) IS_OP(1) POP_JUMP_IF_FALSE
|
||||
// - LOAD_CONST(None) IS_OP(0) TO_BOOL POP_JUMP_IF_TRUE
|
||||
// - LOAD_CONST(None) IS_OP(1) TO_BOOL POP_JUMP_IF_FALSE
|
||||
// Fold to POP_JUMP_IF_NOT_NONE:
|
||||
// - LOAD_CONST(None) IS_OP(0) POP_JUMP_IF_FALSE
|
||||
// - LOAD_CONST(None) IS_OP(1) POP_JUMP_IF_TRUE
|
||||
// - LOAD_CONST(None) IS_OP(0) TO_BOOL POP_JUMP_IF_FALSE
|
||||
// - LOAD_CONST(None) IS_OP(1) TO_BOOL POP_JUMP_IF_TRUE
|
||||
cnt = get_const_value(opcode, oparg, consts);
|
||||
if (cnt == NULL) {
|
||||
goto error;
|
||||
}
|
||||
int jump_op = i+2 < bb->b_iused ? bb->b_instr[i+2].i_opcode : 0;
|
||||
if (Py_IsNone(cnt) && (jump_op == POP_JUMP_IF_FALSE || jump_op == POP_JUMP_IF_TRUE)) {
|
||||
unsigned char nextarg = bb->b_instr[i+1].i_oparg;
|
||||
INSTR_SET_OP0(inst, NOP);
|
||||
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
|
||||
bb->b_instr[i+2].i_opcode = nextarg ^ (jump_op == POP_JUMP_IF_FALSE) ?
|
||||
POP_JUMP_IF_NOT_NONE : POP_JUMP_IF_NONE;
|
||||
if (!Py_IsNone(cnt)) {
|
||||
break;
|
||||
}
|
||||
Py_DECREF(cnt);
|
||||
if (bb->b_iused <= i + 2) {
|
||||
break;
|
||||
}
|
||||
cfg_instr *is_instr = &bb->b_instr[i + 1];
|
||||
cfg_instr *jump_instr = &bb->b_instr[i + 2];
|
||||
// Get rid of TO_BOOL regardless:
|
||||
if (jump_instr->i_opcode == TO_BOOL) {
|
||||
INSTR_SET_OP0(jump_instr, NOP);
|
||||
if (bb->b_iused <= i + 3) {
|
||||
break;
|
||||
}
|
||||
jump_instr = &bb->b_instr[i + 3];
|
||||
}
|
||||
bool invert = is_instr->i_oparg;
|
||||
if (jump_instr->i_opcode == POP_JUMP_IF_FALSE) {
|
||||
invert = !invert;
|
||||
}
|
||||
else if (jump_instr->i_opcode != POP_JUMP_IF_TRUE) {
|
||||
break;
|
||||
}
|
||||
INSTR_SET_OP0(inst, NOP);
|
||||
INSTR_SET_OP0(is_instr, NOP);
|
||||
jump_instr->i_opcode = invert ? POP_JUMP_IF_NOT_NONE
|
||||
: POP_JUMP_IF_NONE;
|
||||
break;
|
||||
case RETURN_VALUE:
|
||||
INSTR_SET_OP0(inst, NOP);
|
||||
INSTR_SET_OP1(&bb->b_instr[++i], RETURN_CONST, oparg);
|
||||
break;
|
||||
case TO_BOOL:
|
||||
cnt = get_const_value(opcode, oparg, consts);
|
||||
if (cnt == NULL) {
|
||||
goto error;
|
||||
}
|
||||
is_true = PyObject_IsTrue(cnt);
|
||||
Py_DECREF(cnt);
|
||||
if (is_true == -1) {
|
||||
goto error;
|
||||
}
|
||||
cnt = PyBool_FromLong(is_true);
|
||||
int index = add_const(cnt, consts, const_cache);
|
||||
if (index < 0) {
|
||||
return ERROR;
|
||||
}
|
||||
INSTR_SET_OP0(inst, NOP);
|
||||
INSTR_SET_OP1(&bb->b_instr[i + 1], LOAD_CONST, index);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -1464,6 +1522,39 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
|
|||
inst[1].i_oparg |= 1;
|
||||
}
|
||||
break;
|
||||
case COMPARE_OP:
|
||||
if (nextop == TO_BOOL) {
|
||||
INSTR_SET_OP0(inst, NOP);
|
||||
INSTR_SET_OP1(&bb->b_instr[i + 1], COMPARE_OP, oparg | 16);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case CONTAINS_OP:
|
||||
case IS_OP:
|
||||
if (nextop == TO_BOOL) {
|
||||
INSTR_SET_OP0(inst, NOP);
|
||||
INSTR_SET_OP1(&bb->b_instr[i + 1], opcode, oparg);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case TO_BOOL:
|
||||
if (nextop == TO_BOOL) {
|
||||
INSTR_SET_OP0(inst, NOP);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case UNARY_NOT:
|
||||
if (nextop == TO_BOOL) {
|
||||
INSTR_SET_OP0(inst, NOP);
|
||||
INSTR_SET_OP0(&bb->b_instr[i + 1], UNARY_NOT);
|
||||
continue;
|
||||
}
|
||||
if (nextop == UNARY_NOT) {
|
||||
INSTR_SET_OP0(inst, NOP);
|
||||
INSTR_SET_OP0(&bb->b_instr[i + 1], NOP);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/* All OPCODE_HAS_CONST opcodes should be handled with LOAD_CONST */
|
||||
assert (!OPCODE_HAS_CONST(inst->i_opcode));
|
||||
|
|
1144
Python/generated_cases.c.h
generated
1144
Python/generated_cases.c.h
generated
File diff suppressed because it is too large
Load diff
43
Python/opcode_metadata.h
generated
43
Python/opcode_metadata.h
generated
|
@ -82,6 +82,20 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
|
|||
return 1;
|
||||
case UNARY_NOT:
|
||||
return 1;
|
||||
case TO_BOOL:
|
||||
return 1;
|
||||
case TO_BOOL_BOOL:
|
||||
return 1;
|
||||
case TO_BOOL_INT:
|
||||
return 1;
|
||||
case TO_BOOL_LIST:
|
||||
return 1;
|
||||
case TO_BOOL_NONE:
|
||||
return 1;
|
||||
case TO_BOOL_STR:
|
||||
return 1;
|
||||
case TO_BOOL_ALWAYS_TRUE:
|
||||
return 1;
|
||||
case UNARY_INVERT:
|
||||
return 1;
|
||||
case BINARY_OP_MULTIPLY_INT:
|
||||
|
@ -508,6 +522,20 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
|
|||
return 1;
|
||||
case UNARY_NOT:
|
||||
return 1;
|
||||
case TO_BOOL:
|
||||
return 1;
|
||||
case TO_BOOL_BOOL:
|
||||
return 1;
|
||||
case TO_BOOL_INT:
|
||||
return 1;
|
||||
case TO_BOOL_LIST:
|
||||
return 1;
|
||||
case TO_BOOL_NONE:
|
||||
return 1;
|
||||
case TO_BOOL_STR:
|
||||
return 1;
|
||||
case TO_BOOL_ALWAYS_TRUE:
|
||||
return 1;
|
||||
case UNARY_INVERT:
|
||||
return 1;
|
||||
case BINARY_OP_MULTIPLY_INT:
|
||||
|
@ -886,7 +914,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
|
|||
}
|
||||
#endif
|
||||
|
||||
enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC00, INSTR_FMT_IBC000, INSTR_FMT_IBC00000000, INSTR_FMT_IX, INSTR_FMT_IXC, INSTR_FMT_IXC000 };
|
||||
enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC00, INSTR_FMT_IBC000, INSTR_FMT_IBC00000000, INSTR_FMT_IX, INSTR_FMT_IXC, INSTR_FMT_IXC00, INSTR_FMT_IXC000 };
|
||||
#define HAS_ARG_FLAG (1)
|
||||
#define HAS_CONST_FLAG (2)
|
||||
#define HAS_NAME_FLAG (4)
|
||||
|
@ -940,6 +968,13 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[512] = {
|
|||
[INSTRUMENTED_END_SEND] = { true, INSTR_FMT_IX, 0 },
|
||||
[UNARY_NEGATIVE] = { true, INSTR_FMT_IX, 0 },
|
||||
[UNARY_NOT] = { true, INSTR_FMT_IX, 0 },
|
||||
[TO_BOOL] = { true, INSTR_FMT_IXC00, 0 },
|
||||
[TO_BOOL_BOOL] = { true, INSTR_FMT_IXC00, 0 },
|
||||
[TO_BOOL_INT] = { true, INSTR_FMT_IXC00, 0 },
|
||||
[TO_BOOL_LIST] = { true, INSTR_FMT_IXC00, 0 },
|
||||
[TO_BOOL_NONE] = { true, INSTR_FMT_IXC00, 0 },
|
||||
[TO_BOOL_STR] = { true, INSTR_FMT_IXC00, 0 },
|
||||
[TO_BOOL_ALWAYS_TRUE] = { true, INSTR_FMT_IXC00, 0 },
|
||||
[UNARY_INVERT] = { true, INSTR_FMT_IX, 0 },
|
||||
[BINARY_OP_MULTIPLY_INT] = { true, INSTR_FMT_IBC, 0 },
|
||||
[BINARY_OP_ADD_INT] = { true, INSTR_FMT_IBC, 0 },
|
||||
|
@ -1139,6 +1174,12 @@ const struct opcode_macro_expansion _PyOpcode_macro_expansion[256] = {
|
|||
[END_SEND] = { .nuops = 1, .uops = { { END_SEND, 0, 0 } } },
|
||||
[UNARY_NEGATIVE] = { .nuops = 1, .uops = { { UNARY_NEGATIVE, 0, 0 } } },
|
||||
[UNARY_NOT] = { .nuops = 1, .uops = { { UNARY_NOT, 0, 0 } } },
|
||||
[TO_BOOL_BOOL] = { .nuops = 1, .uops = { { TO_BOOL_BOOL, 0, 0 } } },
|
||||
[TO_BOOL_INT] = { .nuops = 1, .uops = { { TO_BOOL_INT, 0, 0 } } },
|
||||
[TO_BOOL_LIST] = { .nuops = 1, .uops = { { TO_BOOL_LIST, 0, 0 } } },
|
||||
[TO_BOOL_NONE] = { .nuops = 1, .uops = { { TO_BOOL_NONE, 0, 0 } } },
|
||||
[TO_BOOL_STR] = { .nuops = 1, .uops = { { TO_BOOL_STR, 0, 0 } } },
|
||||
[TO_BOOL_ALWAYS_TRUE] = { .nuops = 1, .uops = { { TO_BOOL_ALWAYS_TRUE, 2, 1 } } },
|
||||
[UNARY_INVERT] = { .nuops = 1, .uops = { { UNARY_INVERT, 0, 0 } } },
|
||||
[BINARY_OP_MULTIPLY_INT] = { .nuops = 2, .uops = { { _GUARD_BOTH_INT, 0, 0 }, { _BINARY_OP_MULTIPLY_INT, 0, 0 } } },
|
||||
[BINARY_OP_ADD_INT] = { .nuops = 2, .uops = { { _GUARD_BOTH_INT, 0, 0 }, { _BINARY_OP_ADD_INT, 0, 0 } } },
|
||||
|
|
122
Python/opcode_targets.h
generated
122
Python/opcode_targets.h
generated
|
@ -5,49 +5,49 @@ static void *opcode_targets[256] = {
|
|||
&&TARGET_INTERPRETER_EXIT,
|
||||
&&TARGET_END_FOR,
|
||||
&&TARGET_END_SEND,
|
||||
&&TARGET_BINARY_OP_MULTIPLY_INT,
|
||||
&&TARGET_BINARY_OP_ADD_INT,
|
||||
&&TARGET_BINARY_OP_SUBTRACT_INT,
|
||||
&&TARGET_TO_BOOL,
|
||||
&&TARGET_TO_BOOL_ALWAYS_TRUE,
|
||||
&&TARGET_TO_BOOL_BOOL,
|
||||
&&TARGET_NOP,
|
||||
&&TARGET_BINARY_OP_MULTIPLY_FLOAT,
|
||||
&&TARGET_TO_BOOL_INT,
|
||||
&&TARGET_UNARY_NEGATIVE,
|
||||
&&TARGET_UNARY_NOT,
|
||||
&&TARGET_BINARY_OP_ADD_FLOAT,
|
||||
&&TARGET_BINARY_OP_SUBTRACT_FLOAT,
|
||||
&&TARGET_TO_BOOL_LIST,
|
||||
&&TARGET_TO_BOOL_NONE,
|
||||
&&TARGET_UNARY_INVERT,
|
||||
&&TARGET_EXIT_INIT_CHECK,
|
||||
&&TARGET_RESERVED,
|
||||
&&TARGET_BINARY_OP_ADD_UNICODE,
|
||||
&&TARGET_BINARY_OP_INPLACE_ADD_UNICODE,
|
||||
&&TARGET_BINARY_SUBSCR_DICT,
|
||||
&&TARGET_BINARY_SUBSCR_GETITEM,
|
||||
&&TARGET_BINARY_SUBSCR_LIST_INT,
|
||||
&&TARGET_BINARY_SUBSCR_TUPLE_INT,
|
||||
&&TARGET_TO_BOOL_STR,
|
||||
&&TARGET_BINARY_OP_MULTIPLY_INT,
|
||||
&&TARGET_BINARY_OP_ADD_INT,
|
||||
&&TARGET_BINARY_OP_SUBTRACT_INT,
|
||||
&&TARGET_BINARY_OP_MULTIPLY_FLOAT,
|
||||
&&TARGET_BINARY_OP_ADD_FLOAT,
|
||||
&&TARGET_MAKE_FUNCTION,
|
||||
&&TARGET_BINARY_SUBSCR,
|
||||
&&TARGET_BINARY_SLICE,
|
||||
&&TARGET_STORE_SLICE,
|
||||
&&TARGET_STORE_SUBSCR_DICT,
|
||||
&&TARGET_STORE_SUBSCR_LIST_INT,
|
||||
&&TARGET_BINARY_OP_SUBTRACT_FLOAT,
|
||||
&&TARGET_BINARY_OP_ADD_UNICODE,
|
||||
&&TARGET_GET_LEN,
|
||||
&&TARGET_MATCH_MAPPING,
|
||||
&&TARGET_MATCH_SEQUENCE,
|
||||
&&TARGET_MATCH_KEYS,
|
||||
&&TARGET_SEND_GEN,
|
||||
&&TARGET_BINARY_OP_INPLACE_ADD_UNICODE,
|
||||
&&TARGET_PUSH_EXC_INFO,
|
||||
&&TARGET_CHECK_EXC_MATCH,
|
||||
&&TARGET_CHECK_EG_MATCH,
|
||||
&&TARGET_UNPACK_SEQUENCE_TWO_TUPLE,
|
||||
&&TARGET_UNPACK_SEQUENCE_TUPLE,
|
||||
&&TARGET_BINARY_SUBSCR_DICT,
|
||||
&&TARGET_BINARY_SUBSCR_GETITEM,
|
||||
&&TARGET_FORMAT_SIMPLE,
|
||||
&&TARGET_FORMAT_WITH_SPEC,
|
||||
&&TARGET_UNPACK_SEQUENCE_LIST,
|
||||
&&TARGET_STORE_ATTR_INSTANCE_VALUE,
|
||||
&&TARGET_STORE_ATTR_SLOT,
|
||||
&&TARGET_STORE_ATTR_WITH_HINT,
|
||||
&&TARGET_LOAD_GLOBAL_MODULE,
|
||||
&&TARGET_LOAD_GLOBAL_BUILTIN,
|
||||
&&TARGET_LOAD_SUPER_ATTR_ATTR,
|
||||
&&TARGET_BINARY_SUBSCR_LIST_INT,
|
||||
&&TARGET_BINARY_SUBSCR_TUPLE_INT,
|
||||
&&TARGET_STORE_SUBSCR_DICT,
|
||||
&&TARGET_STORE_SUBSCR_LIST_INT,
|
||||
&&TARGET_SEND_GEN,
|
||||
&&TARGET_UNPACK_SEQUENCE_TWO_TUPLE,
|
||||
&&TARGET_UNPACK_SEQUENCE_TUPLE,
|
||||
&&TARGET_WITH_EXCEPT_START,
|
||||
&&TARGET_GET_AITER,
|
||||
&&TARGET_GET_ANEXT,
|
||||
|
@ -55,39 +55,39 @@ static void *opcode_targets[256] = {
|
|||
&&TARGET_BEFORE_WITH,
|
||||
&&TARGET_END_ASYNC_FOR,
|
||||
&&TARGET_CLEANUP_THROW,
|
||||
&&TARGET_UNPACK_SEQUENCE_LIST,
|
||||
&&TARGET_STORE_ATTR_INSTANCE_VALUE,
|
||||
&&TARGET_STORE_ATTR_SLOT,
|
||||
&&TARGET_STORE_ATTR_WITH_HINT,
|
||||
&&TARGET_STORE_SUBSCR,
|
||||
&&TARGET_DELETE_SUBSCR,
|
||||
&&TARGET_LOAD_GLOBAL_MODULE,
|
||||
&&TARGET_LOAD_GLOBAL_BUILTIN,
|
||||
&&TARGET_LOAD_SUPER_ATTR_ATTR,
|
||||
&&TARGET_LOAD_SUPER_ATTR_METHOD,
|
||||
&&TARGET_LOAD_ATTR_INSTANCE_VALUE,
|
||||
&&TARGET_LOAD_ATTR_MODULE,
|
||||
&&TARGET_GET_ITER,
|
||||
&&TARGET_GET_YIELD_FROM_ITER,
|
||||
&&TARGET_LOAD_ATTR_WITH_HINT,
|
||||
&&TARGET_STORE_SUBSCR,
|
||||
&&TARGET_DELETE_SUBSCR,
|
||||
&&TARGET_LOAD_BUILD_CLASS,
|
||||
&&TARGET_LOAD_ATTR_SLOT,
|
||||
&&TARGET_LOAD_ATTR_CLASS,
|
||||
&&TARGET_LOAD_ASSERTION_ERROR,
|
||||
&&TARGET_RETURN_GENERATOR,
|
||||
&&TARGET_LOAD_ATTR_PROPERTY,
|
||||
&&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN,
|
||||
&&TARGET_LOAD_ATTR_METHOD_WITH_VALUES,
|
||||
&&TARGET_LOAD_ATTR_METHOD_NO_DICT,
|
||||
&&TARGET_GET_ITER,
|
||||
&&TARGET_GET_YIELD_FROM_ITER,
|
||||
&&TARGET_LOAD_ATTR_METHOD_LAZY_DICT,
|
||||
&&TARGET_LOAD_BUILD_CLASS,
|
||||
&&TARGET_COMPARE_OP_FLOAT,
|
||||
&&TARGET_COMPARE_OP_INT,
|
||||
&&TARGET_LOAD_ASSERTION_ERROR,
|
||||
&&TARGET_RETURN_GENERATOR,
|
||||
&&TARGET_COMPARE_OP_STR,
|
||||
&&TARGET_FOR_ITER_LIST,
|
||||
&&TARGET_FOR_ITER_TUPLE,
|
||||
&&TARGET_FOR_ITER_RANGE,
|
||||
&&TARGET_FOR_ITER_GEN,
|
||||
&&TARGET_CALL_BOUND_METHOD_EXACT_ARGS,
|
||||
&&TARGET_CALL_PY_EXACT_ARGS,
|
||||
&&TARGET_RETURN_VALUE,
|
||||
&&TARGET_CALL_PY_WITH_DEFAULTS,
|
||||
&&TARGET_COMPARE_OP_STR,
|
||||
&&TARGET_SETUP_ANNOTATIONS,
|
||||
&&TARGET_CALL_NO_KW_TYPE_1,
|
||||
&&TARGET_FOR_ITER_LIST,
|
||||
&&TARGET_LOAD_LOCALS,
|
||||
&&TARGET_CALL_NO_KW_STR_1,
|
||||
&&TARGET_FOR_ITER_TUPLE,
|
||||
&&TARGET_POP_EXCEPT,
|
||||
&&TARGET_STORE_NAME,
|
||||
&&TARGET_DELETE_NAME,
|
||||
|
@ -110,9 +110,9 @@ static void *opcode_targets[256] = {
|
|||
&&TARGET_IMPORT_NAME,
|
||||
&&TARGET_IMPORT_FROM,
|
||||
&&TARGET_JUMP_FORWARD,
|
||||
&&TARGET_CALL_NO_KW_TUPLE_1,
|
||||
&&TARGET_CALL_BUILTIN_CLASS,
|
||||
&&TARGET_CALL_NO_KW_BUILTIN_O,
|
||||
&&TARGET_FOR_ITER_RANGE,
|
||||
&&TARGET_FOR_ITER_GEN,
|
||||
&&TARGET_CALL_BOUND_METHOD_EXACT_ARGS,
|
||||
&&TARGET_POP_JUMP_IF_FALSE,
|
||||
&&TARGET_POP_JUMP_IF_TRUE,
|
||||
&&TARGET_LOAD_GLOBAL,
|
||||
|
@ -131,11 +131,11 @@ static void *opcode_targets[256] = {
|
|||
&&TARGET_POP_JUMP_IF_NONE,
|
||||
&&TARGET_RAISE_VARARGS,
|
||||
&&TARGET_GET_AWAITABLE,
|
||||
&&TARGET_CALL_NO_KW_BUILTIN_FAST,
|
||||
&&TARGET_CALL_PY_EXACT_ARGS,
|
||||
&&TARGET_BUILD_SLICE,
|
||||
&&TARGET_JUMP_BACKWARD_NO_INTERRUPT,
|
||||
&&TARGET_MAKE_CELL,
|
||||
&&TARGET_CALL_BUILTIN_FAST_WITH_KEYWORDS,
|
||||
&&TARGET_CALL_PY_WITH_DEFAULTS,
|
||||
&&TARGET_LOAD_DEREF,
|
||||
&&TARGET_STORE_DEREF,
|
||||
&&TARGET_DELETE_DEREF,
|
||||
|
@ -147,26 +147,26 @@ static void *opcode_targets[256] = {
|
|||
&&TARGET_LIST_APPEND,
|
||||
&&TARGET_SET_ADD,
|
||||
&&TARGET_MAP_ADD,
|
||||
&&TARGET_CALL_NO_KW_LEN,
|
||||
&&TARGET_CALL_NO_KW_TYPE_1,
|
||||
&&TARGET_COPY_FREE_VARS,
|
||||
&&TARGET_YIELD_VALUE,
|
||||
&&TARGET_RESUME,
|
||||
&&TARGET_MATCH_CLASS,
|
||||
&&TARGET_CALL_NO_KW_ISINSTANCE,
|
||||
&&TARGET_CALL_NO_KW_LIST_APPEND,
|
||||
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_O,
|
||||
&&TARGET_CALL_NO_KW_STR_1,
|
||||
&&TARGET_CALL_NO_KW_TUPLE_1,
|
||||
&&TARGET_CALL_BUILTIN_CLASS,
|
||||
&&TARGET_BUILD_CONST_KEY_MAP,
|
||||
&&TARGET_BUILD_STRING,
|
||||
&&TARGET_CONVERT_VALUE,
|
||||
&&TARGET_CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS,
|
||||
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS,
|
||||
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_FAST,
|
||||
&&TARGET_CALL_NO_KW_BUILTIN_O,
|
||||
&&TARGET_CALL_NO_KW_BUILTIN_FAST,
|
||||
&&TARGET_CALL_BUILTIN_FAST_WITH_KEYWORDS,
|
||||
&&TARGET_LIST_EXTEND,
|
||||
&&TARGET_SET_UPDATE,
|
||||
&&TARGET_DICT_MERGE,
|
||||
&&TARGET_DICT_UPDATE,
|
||||
&&TARGET_CALL_NO_KW_ALLOC_AND_ENTER_INIT,
|
||||
&&_unknown_opcode,
|
||||
&&TARGET_CALL_NO_KW_LEN,
|
||||
&&TARGET_CALL_NO_KW_ISINSTANCE,
|
||||
&&TARGET_LOAD_FAST_LOAD_FAST,
|
||||
&&TARGET_STORE_FAST_LOAD_FAST,
|
||||
&&TARGET_STORE_FAST_STORE_FAST,
|
||||
|
@ -177,12 +177,12 @@ static void *opcode_targets[256] = {
|
|||
&&TARGET_LOAD_FROM_DICT_OR_GLOBALS,
|
||||
&&TARGET_LOAD_FROM_DICT_OR_DEREF,
|
||||
&&TARGET_SET_FUNCTION_ATTRIBUTE,
|
||||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
&&TARGET_CALL_NO_KW_LIST_APPEND,
|
||||
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_O,
|
||||
&&TARGET_CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS,
|
||||
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS,
|
||||
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_FAST,
|
||||
&&TARGET_CALL_NO_KW_ALLOC_AND_ENTER_INIT,
|
||||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
&&_unknown_opcode,
|
||||
|
|
|
@ -107,6 +107,8 @@ _Py_GetSpecializationStats(void) {
|
|||
err += add_stat_dict(stats, COMPARE_OP, "compare_op");
|
||||
err += add_stat_dict(stats, UNPACK_SEQUENCE, "unpack_sequence");
|
||||
err += add_stat_dict(stats, FOR_ITER, "for_iter");
|
||||
err += add_stat_dict(stats, TO_BOOL, "to_bool");
|
||||
err += add_stat_dict(stats, SEND, "send");
|
||||
if (err < 0) {
|
||||
Py_DECREF(stats);
|
||||
return NULL;
|
||||
|
@ -127,9 +129,7 @@ print_spec_stats(FILE *out, OpcodeStats *stats)
|
|||
/* Mark some opcodes as specializable for stats,
|
||||
* even though we don't specialize them yet. */
|
||||
fprintf(out, "opcode[%d].specializable : 1\n", BINARY_SLICE);
|
||||
fprintf(out, "opcode[%d].specializable : 1\n", COMPARE_OP);
|
||||
fprintf(out, "opcode[%d].specializable : 1\n", STORE_SLICE);
|
||||
fprintf(out, "opcode[%d].specializable : 1\n", SEND);
|
||||
for (int i = 0; i < 256; i++) {
|
||||
if (_PyOpcode_Caches[i]) {
|
||||
fprintf(out, "opcode[%d].specializable : 1\n", i);
|
||||
|
@ -447,6 +447,18 @@ _PyCode_Quicken(PyCodeObject *code)
|
|||
#define SPEC_FAIL_UNPACK_SEQUENCE_ITERATOR 9
|
||||
#define SPEC_FAIL_UNPACK_SEQUENCE_SEQUENCE 10
|
||||
|
||||
// TO_BOOL
|
||||
#define SPEC_FAIL_TO_BOOL_BYTEARRAY 9
|
||||
#define SPEC_FAIL_TO_BOOL_BYTES 10
|
||||
#define SPEC_FAIL_TO_BOOL_DICT 11
|
||||
#define SPEC_FAIL_TO_BOOL_FLOAT 12
|
||||
#define SPEC_FAIL_TO_BOOL_MAPPING 13
|
||||
#define SPEC_FAIL_TO_BOOL_MEMORY_VIEW 14
|
||||
#define SPEC_FAIL_TO_BOOL_NUMBER 15
|
||||
#define SPEC_FAIL_TO_BOOL_SEQUENCE 16
|
||||
#define SPEC_FAIL_TO_BOOL_SET 17
|
||||
#define SPEC_FAIL_TO_BOOL_TUPLE 18
|
||||
|
||||
static int function_kind(PyCodeObject *code);
|
||||
static bool function_check_args(PyObject *o, int expected_argcount, int opcode);
|
||||
static uint32_t function_get_version(PyObject *o, int opcode);
|
||||
|
@ -2047,6 +2059,8 @@ _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
|
|||
{
|
||||
assert(ENABLE_SPECIALIZATION);
|
||||
assert(_PyOpcode_Caches[COMPARE_OP] == INLINE_CACHE_ENTRIES_COMPARE_OP);
|
||||
// All of these specializations compute boolean values, so they're all valid
|
||||
// regardless of the fifth-lowest oparg bit.
|
||||
_PyCompareOpCache *cache = (_PyCompareOpCache *)(instr + 1);
|
||||
if (Py_TYPE(lhs) != Py_TYPE(rhs)) {
|
||||
SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
|
||||
|
@ -2067,7 +2081,7 @@ _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
|
|||
}
|
||||
}
|
||||
if (PyUnicode_CheckExact(lhs)) {
|
||||
int cmp = oparg >> 4;
|
||||
int cmp = oparg >> 5;
|
||||
if (cmp != Py_EQ && cmp != Py_NE) {
|
||||
SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_OP_STRING);
|
||||
goto failure;
|
||||
|
@ -2284,6 +2298,99 @@ success:
|
|||
cache->counter = adaptive_counter_cooldown();
|
||||
}
|
||||
|
||||
void
|
||||
_Py_Specialize_ToBool(PyObject *value, _Py_CODEUNIT *instr)
|
||||
{
|
||||
assert(ENABLE_SPECIALIZATION);
|
||||
assert(_PyOpcode_Caches[TO_BOOL] == INLINE_CACHE_ENTRIES_TO_BOOL);
|
||||
_PyToBoolCache *cache = (_PyToBoolCache *)(instr + 1);
|
||||
if (PyBool_Check(value)) {
|
||||
instr->op.code = TO_BOOL_BOOL;
|
||||
goto success;
|
||||
}
|
||||
if (PyLong_CheckExact(value)) {
|
||||
instr->op.code = TO_BOOL_INT;
|
||||
goto success;
|
||||
}
|
||||
if (PyList_CheckExact(value)) {
|
||||
instr->op.code = TO_BOOL_LIST;
|
||||
goto success;
|
||||
}
|
||||
if (Py_IsNone(value)) {
|
||||
instr->op.code = TO_BOOL_NONE;
|
||||
goto success;
|
||||
}
|
||||
if (PyUnicode_CheckExact(value)) {
|
||||
instr->op.code = TO_BOOL_STR;
|
||||
goto success;
|
||||
}
|
||||
if (PyType_HasFeature(Py_TYPE(value), Py_TPFLAGS_HEAPTYPE)) {
|
||||
PyNumberMethods *nb = Py_TYPE(value)->tp_as_number;
|
||||
if (nb && nb->nb_bool) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_NUMBER);
|
||||
goto failure;
|
||||
}
|
||||
PyMappingMethods *mp = Py_TYPE(value)->tp_as_mapping;
|
||||
if (mp && mp->mp_length) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_MAPPING);
|
||||
goto failure;
|
||||
}
|
||||
PySequenceMethods *sq = Py_TYPE(value)->tp_as_sequence;
|
||||
if (sq && sq->sq_length) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_SEQUENCE);
|
||||
goto failure;
|
||||
}
|
||||
if (!PyUnstable_Type_AssignVersionTag(Py_TYPE(value))) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_OUT_OF_VERSIONS);
|
||||
goto failure;
|
||||
}
|
||||
uint32_t version = Py_TYPE(value)->tp_version_tag;
|
||||
instr->op.code = TO_BOOL_ALWAYS_TRUE;
|
||||
write_u32(cache->version, version);
|
||||
assert(version);
|
||||
goto success;
|
||||
}
|
||||
#ifdef Py_STATS
|
||||
if (PyByteArray_CheckExact(value)) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_BYTEARRAY);
|
||||
goto failure;
|
||||
}
|
||||
if (PyBytes_CheckExact(value)) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_BYTES);
|
||||
goto failure;
|
||||
}
|
||||
if (PyDict_CheckExact(value)) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_DICT);
|
||||
goto failure;
|
||||
}
|
||||
if (PyFloat_CheckExact(value)) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_FLOAT);
|
||||
goto failure;
|
||||
}
|
||||
if (PyMemoryView_Check(value)) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_MEMORY_VIEW);
|
||||
goto failure;
|
||||
}
|
||||
if (PyAnySet_CheckExact(value)) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_SET);
|
||||
goto failure;
|
||||
}
|
||||
if (PyTuple_CheckExact(value)) {
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_TO_BOOL_TUPLE);
|
||||
goto failure;
|
||||
}
|
||||
SPECIALIZATION_FAIL(TO_BOOL, SPEC_FAIL_OTHER);
|
||||
#endif
|
||||
failure:
|
||||
STAT_INC(TO_BOOL, failure);
|
||||
instr->op.code = TO_BOOL;
|
||||
cache->counter = adaptive_counter_backoff(cache->counter);
|
||||
return;
|
||||
success:
|
||||
STAT_INC(TO_BOOL, success);
|
||||
cache->counter = adaptive_counter_cooldown();
|
||||
}
|
||||
|
||||
/* Code init cleanup.
|
||||
* CALL_NO_KW_ALLOC_AND_ENTER_INIT will set up
|
||||
* the frame to execute the EXIT_INIT_CHECK
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue