GH-106008: Make implicit boolean conversions explicit (GH-106003)

This commit is contained in:
Brandt Bucher 2023-06-29 13:49:54 -07:00 committed by GitHub
parent 6e9f83d9ae
commit 7b2d94d875
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 1728 additions and 1152 deletions

3
.gitattributes vendored
View File

@ -85,8 +85,9 @@ Parser/parser.c generated
Parser/token.c generated Parser/token.c generated
Programs/test_frozenmain.h generated Programs/test_frozenmain.h generated
Python/Python-ast.c generated Python/Python-ast.c generated
Python/generated_cases.c.h generated
Python/executor_cases.c.h generated Python/executor_cases.c.h generated
Python/generated_cases.c.h generated
Python/opcode_metadata.h generated
Python/opcode_targets.h generated Python/opcode_targets.h generated
Python/stdlib_module_names.h generated Python/stdlib_module_names.h generated
Tools/peg_generator/pegen/grammar_parser.py generated Tools/peg_generator/pegen/grammar_parser.py generated

View File

@ -529,6 +529,9 @@ result back on the stack.
Implements ``STACK[-1] = not STACK[-1]``. Implements ``STACK[-1] = not STACK[-1]``.
.. versionchanged:: 3.13
This instruction now requires an exact :class:`bool` operand.
.. opcode:: UNARY_INVERT .. opcode:: UNARY_INVERT
@ -548,6 +551,13 @@ result back on the stack.
.. versionadded:: 3.5 .. versionadded:: 3.5
.. opcode:: TO_BOOL
Implements ``STACK[-1] = bool(STACK[-1])``.
.. versionadded:: 3.13
**Binary and in-place operations** **Binary and in-place operations**
Binary operations remove the top two items from the stack (``STACK[-1]`` and Binary operations remove the top two items from the stack (``STACK[-1]`` and
@ -1127,7 +1137,12 @@ iterations of the loop.
.. opcode:: COMPARE_OP (opname) .. opcode:: COMPARE_OP (opname)
Performs a Boolean operation. The operation name can be found in Performs a Boolean operation. The operation name can be found in
``cmp_op[opname]``. ``cmp_op[opname >> 5]``. If the fifth-lowest bit of ``opname`` is set
(``opname & 16``), the result should be coerced to ``bool``.
.. versionchanged:: 3.13
The fifth-lowest bit of the oparg now indicates a forced conversion to
:class:`bool`.
.. opcode:: IS_OP (invert) .. opcode:: IS_OP (invert)
@ -1191,6 +1206,9 @@ iterations of the loop.
.. versionchanged:: 3.12 .. versionchanged:: 3.12
This is no longer a pseudo-instruction. This is no longer a pseudo-instruction.
.. versionchanged:: 3.13
This instruction now requires an exact :class:`bool` operand.
.. opcode:: POP_JUMP_IF_FALSE (delta) .. opcode:: POP_JUMP_IF_FALSE (delta)
If ``STACK[-1]`` is false, increments the bytecode counter by *delta*. If ``STACK[-1]`` is false, increments the bytecode counter by *delta*.
@ -1204,6 +1222,9 @@ iterations of the loop.
.. versionchanged:: 3.12 .. versionchanged:: 3.12
This is no longer a pseudo-instruction. This is no longer a pseudo-instruction.
.. versionchanged:: 3.13
This instruction now requires an exact :class:`bool` operand.
.. opcode:: POP_JUMP_IF_NOT_NONE (delta) .. opcode:: POP_JUMP_IF_NOT_NONE (delta)
If ``STACK[-1]`` is not ``None``, increments the bytecode counter by *delta*. If ``STACK[-1]`` is not ``None``, increments the bytecode counter by *delta*.

View File

@ -101,6 +101,13 @@ typedef struct {
#define INLINE_CACHE_ENTRIES_SEND CACHE_ENTRIES(_PySendCache) #define INLINE_CACHE_ENTRIES_SEND CACHE_ENTRIES(_PySendCache)
typedef struct {
uint16_t counter;
uint16_t version[2];
} _PyToBoolCache;
#define INLINE_CACHE_ENTRIES_TO_BOOL CACHE_ENTRIES(_PyToBoolCache)
// Borrowed references to common callables: // Borrowed references to common callables:
struct callable_cache { struct callable_cache {
PyObject *isinstance; PyObject *isinstance;
@ -246,6 +253,7 @@ extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
int oparg); int oparg);
extern void _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr, int oparg); extern void _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr, int oparg);
extern void _Py_Specialize_Send(PyObject *receiver, _Py_CODEUNIT *instr); extern void _Py_Specialize_Send(PyObject *receiver, _Py_CODEUNIT *instr);
extern void _Py_Specialize_ToBool(PyObject *value, _Py_CODEUNIT *instr);
/* Finalizer function for static codeobjects used in deepfreeze.py */ /* Finalizer function for static codeobjects used in deepfreeze.py */
extern void _PyStaticCode_Fini(PyCodeObject *co); extern void _PyStaticCode_Fini(PyCodeObject *co);

View File

@ -19,6 +19,7 @@ extern const uint8_t _PyOpcode_Deopt[256];
#ifdef NEED_OPCODE_TABLES #ifdef NEED_OPCODE_TABLES
const uint8_t _PyOpcode_Caches[256] = { const uint8_t _PyOpcode_Caches[256] = {
[TO_BOOL] = 3,
[BINARY_SUBSCR] = 1, [BINARY_SUBSCR] = 1,
[STORE_SUBSCR] = 1, [STORE_SUBSCR] = 1,
[UNPACK_SEQUENCE] = 1, [UNPACK_SEQUENCE] = 1,
@ -221,6 +222,13 @@ const uint8_t _PyOpcode_Deopt[256] = {
[STORE_SUBSCR_DICT] = STORE_SUBSCR, [STORE_SUBSCR_DICT] = STORE_SUBSCR,
[STORE_SUBSCR_LIST_INT] = STORE_SUBSCR, [STORE_SUBSCR_LIST_INT] = STORE_SUBSCR,
[SWAP] = SWAP, [SWAP] = SWAP,
[TO_BOOL] = TO_BOOL,
[TO_BOOL_ALWAYS_TRUE] = TO_BOOL,
[TO_BOOL_BOOL] = TO_BOOL,
[TO_BOOL_INT] = TO_BOOL,
[TO_BOOL_LIST] = TO_BOOL,
[TO_BOOL_NONE] = TO_BOOL,
[TO_BOOL_STR] = TO_BOOL,
[UNARY_INVERT] = UNARY_INVERT, [UNARY_INVERT] = UNARY_INVERT,
[UNARY_NEGATIVE] = UNARY_NEGATIVE, [UNARY_NEGATIVE] = UNARY_NEGATIVE,
[UNARY_NOT] = UNARY_NOT, [UNARY_NOT] = UNARY_NOT,
@ -242,49 +250,49 @@ static const char *const _PyOpcode_OpName[268] = {
[INTERPRETER_EXIT] = "INTERPRETER_EXIT", [INTERPRETER_EXIT] = "INTERPRETER_EXIT",
[END_FOR] = "END_FOR", [END_FOR] = "END_FOR",
[END_SEND] = "END_SEND", [END_SEND] = "END_SEND",
[BINARY_OP_MULTIPLY_INT] = "BINARY_OP_MULTIPLY_INT", [TO_BOOL] = "TO_BOOL",
[BINARY_OP_ADD_INT] = "BINARY_OP_ADD_INT", [TO_BOOL_ALWAYS_TRUE] = "TO_BOOL_ALWAYS_TRUE",
[BINARY_OP_SUBTRACT_INT] = "BINARY_OP_SUBTRACT_INT", [TO_BOOL_BOOL] = "TO_BOOL_BOOL",
[NOP] = "NOP", [NOP] = "NOP",
[BINARY_OP_MULTIPLY_FLOAT] = "BINARY_OP_MULTIPLY_FLOAT", [TO_BOOL_INT] = "TO_BOOL_INT",
[UNARY_NEGATIVE] = "UNARY_NEGATIVE", [UNARY_NEGATIVE] = "UNARY_NEGATIVE",
[UNARY_NOT] = "UNARY_NOT", [UNARY_NOT] = "UNARY_NOT",
[BINARY_OP_ADD_FLOAT] = "BINARY_OP_ADD_FLOAT", [TO_BOOL_LIST] = "TO_BOOL_LIST",
[BINARY_OP_SUBTRACT_FLOAT] = "BINARY_OP_SUBTRACT_FLOAT", [TO_BOOL_NONE] = "TO_BOOL_NONE",
[UNARY_INVERT] = "UNARY_INVERT", [UNARY_INVERT] = "UNARY_INVERT",
[EXIT_INIT_CHECK] = "EXIT_INIT_CHECK", [EXIT_INIT_CHECK] = "EXIT_INIT_CHECK",
[RESERVED] = "RESERVED", [RESERVED] = "RESERVED",
[BINARY_OP_ADD_UNICODE] = "BINARY_OP_ADD_UNICODE", [TO_BOOL_STR] = "TO_BOOL_STR",
[BINARY_OP_INPLACE_ADD_UNICODE] = "BINARY_OP_INPLACE_ADD_UNICODE", [BINARY_OP_MULTIPLY_INT] = "BINARY_OP_MULTIPLY_INT",
[BINARY_SUBSCR_DICT] = "BINARY_SUBSCR_DICT", [BINARY_OP_ADD_INT] = "BINARY_OP_ADD_INT",
[BINARY_SUBSCR_GETITEM] = "BINARY_SUBSCR_GETITEM", [BINARY_OP_SUBTRACT_INT] = "BINARY_OP_SUBTRACT_INT",
[BINARY_SUBSCR_LIST_INT] = "BINARY_SUBSCR_LIST_INT", [BINARY_OP_MULTIPLY_FLOAT] = "BINARY_OP_MULTIPLY_FLOAT",
[BINARY_SUBSCR_TUPLE_INT] = "BINARY_SUBSCR_TUPLE_INT", [BINARY_OP_ADD_FLOAT] = "BINARY_OP_ADD_FLOAT",
[MAKE_FUNCTION] = "MAKE_FUNCTION", [MAKE_FUNCTION] = "MAKE_FUNCTION",
[BINARY_SUBSCR] = "BINARY_SUBSCR", [BINARY_SUBSCR] = "BINARY_SUBSCR",
[BINARY_SLICE] = "BINARY_SLICE", [BINARY_SLICE] = "BINARY_SLICE",
[STORE_SLICE] = "STORE_SLICE", [STORE_SLICE] = "STORE_SLICE",
[STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT", [BINARY_OP_SUBTRACT_FLOAT] = "BINARY_OP_SUBTRACT_FLOAT",
[STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT", [BINARY_OP_ADD_UNICODE] = "BINARY_OP_ADD_UNICODE",
[GET_LEN] = "GET_LEN", [GET_LEN] = "GET_LEN",
[MATCH_MAPPING] = "MATCH_MAPPING", [MATCH_MAPPING] = "MATCH_MAPPING",
[MATCH_SEQUENCE] = "MATCH_SEQUENCE", [MATCH_SEQUENCE] = "MATCH_SEQUENCE",
[MATCH_KEYS] = "MATCH_KEYS", [MATCH_KEYS] = "MATCH_KEYS",
[SEND_GEN] = "SEND_GEN", [BINARY_OP_INPLACE_ADD_UNICODE] = "BINARY_OP_INPLACE_ADD_UNICODE",
[PUSH_EXC_INFO] = "PUSH_EXC_INFO", [PUSH_EXC_INFO] = "PUSH_EXC_INFO",
[CHECK_EXC_MATCH] = "CHECK_EXC_MATCH", [CHECK_EXC_MATCH] = "CHECK_EXC_MATCH",
[CHECK_EG_MATCH] = "CHECK_EG_MATCH", [CHECK_EG_MATCH] = "CHECK_EG_MATCH",
[UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE", [BINARY_SUBSCR_DICT] = "BINARY_SUBSCR_DICT",
[UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE", [BINARY_SUBSCR_GETITEM] = "BINARY_SUBSCR_GETITEM",
[FORMAT_SIMPLE] = "FORMAT_SIMPLE", [FORMAT_SIMPLE] = "FORMAT_SIMPLE",
[FORMAT_WITH_SPEC] = "FORMAT_WITH_SPEC", [FORMAT_WITH_SPEC] = "FORMAT_WITH_SPEC",
[UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST", [BINARY_SUBSCR_LIST_INT] = "BINARY_SUBSCR_LIST_INT",
[STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE", [BINARY_SUBSCR_TUPLE_INT] = "BINARY_SUBSCR_TUPLE_INT",
[STORE_ATTR_SLOT] = "STORE_ATTR_SLOT", [STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT",
[STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT", [STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT",
[LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE", [SEND_GEN] = "SEND_GEN",
[LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", [UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE",
[LOAD_SUPER_ATTR_ATTR] = "LOAD_SUPER_ATTR_ATTR", [UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE",
[WITH_EXCEPT_START] = "WITH_EXCEPT_START", [WITH_EXCEPT_START] = "WITH_EXCEPT_START",
[GET_AITER] = "GET_AITER", [GET_AITER] = "GET_AITER",
[GET_ANEXT] = "GET_ANEXT", [GET_ANEXT] = "GET_ANEXT",
@ -292,39 +300,39 @@ static const char *const _PyOpcode_OpName[268] = {
[BEFORE_WITH] = "BEFORE_WITH", [BEFORE_WITH] = "BEFORE_WITH",
[END_ASYNC_FOR] = "END_ASYNC_FOR", [END_ASYNC_FOR] = "END_ASYNC_FOR",
[CLEANUP_THROW] = "CLEANUP_THROW", [CLEANUP_THROW] = "CLEANUP_THROW",
[UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST",
[STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE",
[STORE_ATTR_SLOT] = "STORE_ATTR_SLOT",
[STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT",
[STORE_SUBSCR] = "STORE_SUBSCR",
[DELETE_SUBSCR] = "DELETE_SUBSCR",
[LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE",
[LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN",
[LOAD_SUPER_ATTR_ATTR] = "LOAD_SUPER_ATTR_ATTR",
[LOAD_SUPER_ATTR_METHOD] = "LOAD_SUPER_ATTR_METHOD", [LOAD_SUPER_ATTR_METHOD] = "LOAD_SUPER_ATTR_METHOD",
[LOAD_ATTR_INSTANCE_VALUE] = "LOAD_ATTR_INSTANCE_VALUE", [LOAD_ATTR_INSTANCE_VALUE] = "LOAD_ATTR_INSTANCE_VALUE",
[LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE", [LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE",
[GET_ITER] = "GET_ITER",
[GET_YIELD_FROM_ITER] = "GET_YIELD_FROM_ITER",
[LOAD_ATTR_WITH_HINT] = "LOAD_ATTR_WITH_HINT", [LOAD_ATTR_WITH_HINT] = "LOAD_ATTR_WITH_HINT",
[STORE_SUBSCR] = "STORE_SUBSCR", [LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS",
[DELETE_SUBSCR] = "DELETE_SUBSCR",
[LOAD_ATTR_SLOT] = "LOAD_ATTR_SLOT", [LOAD_ATTR_SLOT] = "LOAD_ATTR_SLOT",
[LOAD_ATTR_CLASS] = "LOAD_ATTR_CLASS", [LOAD_ATTR_CLASS] = "LOAD_ATTR_CLASS",
[LOAD_ASSERTION_ERROR] = "LOAD_ASSERTION_ERROR",
[RETURN_GENERATOR] = "RETURN_GENERATOR",
[LOAD_ATTR_PROPERTY] = "LOAD_ATTR_PROPERTY", [LOAD_ATTR_PROPERTY] = "LOAD_ATTR_PROPERTY",
[LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN", [LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN",
[LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES", [LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES",
[LOAD_ATTR_METHOD_NO_DICT] = "LOAD_ATTR_METHOD_NO_DICT", [LOAD_ATTR_METHOD_NO_DICT] = "LOAD_ATTR_METHOD_NO_DICT",
[GET_ITER] = "GET_ITER",
[GET_YIELD_FROM_ITER] = "GET_YIELD_FROM_ITER",
[LOAD_ATTR_METHOD_LAZY_DICT] = "LOAD_ATTR_METHOD_LAZY_DICT", [LOAD_ATTR_METHOD_LAZY_DICT] = "LOAD_ATTR_METHOD_LAZY_DICT",
[LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS",
[COMPARE_OP_FLOAT] = "COMPARE_OP_FLOAT", [COMPARE_OP_FLOAT] = "COMPARE_OP_FLOAT",
[COMPARE_OP_INT] = "COMPARE_OP_INT", [COMPARE_OP_INT] = "COMPARE_OP_INT",
[LOAD_ASSERTION_ERROR] = "LOAD_ASSERTION_ERROR",
[RETURN_GENERATOR] = "RETURN_GENERATOR",
[COMPARE_OP_STR] = "COMPARE_OP_STR",
[FOR_ITER_LIST] = "FOR_ITER_LIST",
[FOR_ITER_TUPLE] = "FOR_ITER_TUPLE",
[FOR_ITER_RANGE] = "FOR_ITER_RANGE",
[FOR_ITER_GEN] = "FOR_ITER_GEN",
[CALL_BOUND_METHOD_EXACT_ARGS] = "CALL_BOUND_METHOD_EXACT_ARGS",
[CALL_PY_EXACT_ARGS] = "CALL_PY_EXACT_ARGS",
[RETURN_VALUE] = "RETURN_VALUE", [RETURN_VALUE] = "RETURN_VALUE",
[CALL_PY_WITH_DEFAULTS] = "CALL_PY_WITH_DEFAULTS", [COMPARE_OP_STR] = "COMPARE_OP_STR",
[SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS", [SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS",
[CALL_NO_KW_TYPE_1] = "CALL_NO_KW_TYPE_1", [FOR_ITER_LIST] = "FOR_ITER_LIST",
[LOAD_LOCALS] = "LOAD_LOCALS", [LOAD_LOCALS] = "LOAD_LOCALS",
[CALL_NO_KW_STR_1] = "CALL_NO_KW_STR_1", [FOR_ITER_TUPLE] = "FOR_ITER_TUPLE",
[POP_EXCEPT] = "POP_EXCEPT", [POP_EXCEPT] = "POP_EXCEPT",
[STORE_NAME] = "STORE_NAME", [STORE_NAME] = "STORE_NAME",
[DELETE_NAME] = "DELETE_NAME", [DELETE_NAME] = "DELETE_NAME",
@ -347,9 +355,9 @@ static const char *const _PyOpcode_OpName[268] = {
[IMPORT_NAME] = "IMPORT_NAME", [IMPORT_NAME] = "IMPORT_NAME",
[IMPORT_FROM] = "IMPORT_FROM", [IMPORT_FROM] = "IMPORT_FROM",
[JUMP_FORWARD] = "JUMP_FORWARD", [JUMP_FORWARD] = "JUMP_FORWARD",
[CALL_NO_KW_TUPLE_1] = "CALL_NO_KW_TUPLE_1", [FOR_ITER_RANGE] = "FOR_ITER_RANGE",
[CALL_BUILTIN_CLASS] = "CALL_BUILTIN_CLASS", [FOR_ITER_GEN] = "FOR_ITER_GEN",
[CALL_NO_KW_BUILTIN_O] = "CALL_NO_KW_BUILTIN_O", [CALL_BOUND_METHOD_EXACT_ARGS] = "CALL_BOUND_METHOD_EXACT_ARGS",
[POP_JUMP_IF_FALSE] = "POP_JUMP_IF_FALSE", [POP_JUMP_IF_FALSE] = "POP_JUMP_IF_FALSE",
[POP_JUMP_IF_TRUE] = "POP_JUMP_IF_TRUE", [POP_JUMP_IF_TRUE] = "POP_JUMP_IF_TRUE",
[LOAD_GLOBAL] = "LOAD_GLOBAL", [LOAD_GLOBAL] = "LOAD_GLOBAL",
@ -368,11 +376,11 @@ static const char *const _PyOpcode_OpName[268] = {
[POP_JUMP_IF_NONE] = "POP_JUMP_IF_NONE", [POP_JUMP_IF_NONE] = "POP_JUMP_IF_NONE",
[RAISE_VARARGS] = "RAISE_VARARGS", [RAISE_VARARGS] = "RAISE_VARARGS",
[GET_AWAITABLE] = "GET_AWAITABLE", [GET_AWAITABLE] = "GET_AWAITABLE",
[CALL_NO_KW_BUILTIN_FAST] = "CALL_NO_KW_BUILTIN_FAST", [CALL_PY_EXACT_ARGS] = "CALL_PY_EXACT_ARGS",
[BUILD_SLICE] = "BUILD_SLICE", [BUILD_SLICE] = "BUILD_SLICE",
[JUMP_BACKWARD_NO_INTERRUPT] = "JUMP_BACKWARD_NO_INTERRUPT", [JUMP_BACKWARD_NO_INTERRUPT] = "JUMP_BACKWARD_NO_INTERRUPT",
[MAKE_CELL] = "MAKE_CELL", [MAKE_CELL] = "MAKE_CELL",
[CALL_BUILTIN_FAST_WITH_KEYWORDS] = "CALL_BUILTIN_FAST_WITH_KEYWORDS", [CALL_PY_WITH_DEFAULTS] = "CALL_PY_WITH_DEFAULTS",
[LOAD_DEREF] = "LOAD_DEREF", [LOAD_DEREF] = "LOAD_DEREF",
[STORE_DEREF] = "STORE_DEREF", [STORE_DEREF] = "STORE_DEREF",
[DELETE_DEREF] = "DELETE_DEREF", [DELETE_DEREF] = "DELETE_DEREF",
@ -384,26 +392,26 @@ static const char *const _PyOpcode_OpName[268] = {
[LIST_APPEND] = "LIST_APPEND", [LIST_APPEND] = "LIST_APPEND",
[SET_ADD] = "SET_ADD", [SET_ADD] = "SET_ADD",
[MAP_ADD] = "MAP_ADD", [MAP_ADD] = "MAP_ADD",
[CALL_NO_KW_LEN] = "CALL_NO_KW_LEN", [CALL_NO_KW_TYPE_1] = "CALL_NO_KW_TYPE_1",
[COPY_FREE_VARS] = "COPY_FREE_VARS", [COPY_FREE_VARS] = "COPY_FREE_VARS",
[YIELD_VALUE] = "YIELD_VALUE", [YIELD_VALUE] = "YIELD_VALUE",
[RESUME] = "RESUME", [RESUME] = "RESUME",
[MATCH_CLASS] = "MATCH_CLASS", [MATCH_CLASS] = "MATCH_CLASS",
[CALL_NO_KW_ISINSTANCE] = "CALL_NO_KW_ISINSTANCE", [CALL_NO_KW_STR_1] = "CALL_NO_KW_STR_1",
[CALL_NO_KW_LIST_APPEND] = "CALL_NO_KW_LIST_APPEND", [CALL_NO_KW_TUPLE_1] = "CALL_NO_KW_TUPLE_1",
[CALL_NO_KW_METHOD_DESCRIPTOR_O] = "CALL_NO_KW_METHOD_DESCRIPTOR_O", [CALL_BUILTIN_CLASS] = "CALL_BUILTIN_CLASS",
[BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP", [BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP",
[BUILD_STRING] = "BUILD_STRING", [BUILD_STRING] = "BUILD_STRING",
[CONVERT_VALUE] = "CONVERT_VALUE", [CONVERT_VALUE] = "CONVERT_VALUE",
[CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS] = "CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS", [CALL_NO_KW_BUILTIN_O] = "CALL_NO_KW_BUILTIN_O",
[CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS] = "CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS", [CALL_NO_KW_BUILTIN_FAST] = "CALL_NO_KW_BUILTIN_FAST",
[CALL_NO_KW_METHOD_DESCRIPTOR_FAST] = "CALL_NO_KW_METHOD_DESCRIPTOR_FAST", [CALL_BUILTIN_FAST_WITH_KEYWORDS] = "CALL_BUILTIN_FAST_WITH_KEYWORDS",
[LIST_EXTEND] = "LIST_EXTEND", [LIST_EXTEND] = "LIST_EXTEND",
[SET_UPDATE] = "SET_UPDATE", [SET_UPDATE] = "SET_UPDATE",
[DICT_MERGE] = "DICT_MERGE", [DICT_MERGE] = "DICT_MERGE",
[DICT_UPDATE] = "DICT_UPDATE", [DICT_UPDATE] = "DICT_UPDATE",
[CALL_NO_KW_ALLOC_AND_ENTER_INIT] = "CALL_NO_KW_ALLOC_AND_ENTER_INIT", [CALL_NO_KW_LEN] = "CALL_NO_KW_LEN",
[167] = "<167>", [CALL_NO_KW_ISINSTANCE] = "CALL_NO_KW_ISINSTANCE",
[LOAD_FAST_LOAD_FAST] = "LOAD_FAST_LOAD_FAST", [LOAD_FAST_LOAD_FAST] = "LOAD_FAST_LOAD_FAST",
[STORE_FAST_LOAD_FAST] = "STORE_FAST_LOAD_FAST", [STORE_FAST_LOAD_FAST] = "STORE_FAST_LOAD_FAST",
[STORE_FAST_STORE_FAST] = "STORE_FAST_STORE_FAST", [STORE_FAST_STORE_FAST] = "STORE_FAST_STORE_FAST",
@ -414,12 +422,12 @@ static const char *const _PyOpcode_OpName[268] = {
[LOAD_FROM_DICT_OR_GLOBALS] = "LOAD_FROM_DICT_OR_GLOBALS", [LOAD_FROM_DICT_OR_GLOBALS] = "LOAD_FROM_DICT_OR_GLOBALS",
[LOAD_FROM_DICT_OR_DEREF] = "LOAD_FROM_DICT_OR_DEREF", [LOAD_FROM_DICT_OR_DEREF] = "LOAD_FROM_DICT_OR_DEREF",
[SET_FUNCTION_ATTRIBUTE] = "SET_FUNCTION_ATTRIBUTE", [SET_FUNCTION_ATTRIBUTE] = "SET_FUNCTION_ATTRIBUTE",
[178] = "<178>", [CALL_NO_KW_LIST_APPEND] = "CALL_NO_KW_LIST_APPEND",
[179] = "<179>", [CALL_NO_KW_METHOD_DESCRIPTOR_O] = "CALL_NO_KW_METHOD_DESCRIPTOR_O",
[180] = "<180>", [CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS] = "CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS",
[181] = "<181>", [CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS] = "CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS",
[182] = "<182>", [CALL_NO_KW_METHOD_DESCRIPTOR_FAST] = "CALL_NO_KW_METHOD_DESCRIPTOR_FAST",
[183] = "<183>", [CALL_NO_KW_ALLOC_AND_ENTER_INIT] = "CALL_NO_KW_ALLOC_AND_ENTER_INIT",
[184] = "<184>", [184] = "<184>",
[185] = "<185>", [185] = "<185>",
[186] = "<186>", [186] = "<186>",
@ -508,13 +516,6 @@ static const char *const _PyOpcode_OpName[268] = {
#endif #endif
#define EXTRA_CASES \ #define EXTRA_CASES \
case 167: \
case 178: \
case 179: \
case 180: \
case 181: \
case 182: \
case 183: \
case 184: \ case 184: \
case 185: \ case 185: \
case 186: \ case 186: \

127
Include/opcode.h generated
View File

@ -14,6 +14,7 @@ extern "C" {
#define INTERPRETER_EXIT 3 #define INTERPRETER_EXIT 3
#define END_FOR 4 #define END_FOR 4
#define END_SEND 5 #define END_SEND 5
#define TO_BOOL 6
#define NOP 9 #define NOP 9
#define UNARY_NEGATIVE 11 #define UNARY_NEGATIVE 11
#define UNARY_NOT 12 #define UNARY_NOT 12
@ -159,66 +160,72 @@ extern "C" {
#define STORE_FAST_MAYBE_NULL 266 #define STORE_FAST_MAYBE_NULL 266
#define LOAD_CLOSURE 267 #define LOAD_CLOSURE 267
#define MAX_PSEUDO_OPCODE 267 #define MAX_PSEUDO_OPCODE 267
#define BINARY_OP_MULTIPLY_INT 6 #define TO_BOOL_ALWAYS_TRUE 7
#define BINARY_OP_ADD_INT 7 #define TO_BOOL_BOOL 8
#define BINARY_OP_SUBTRACT_INT 8 #define TO_BOOL_INT 10
#define BINARY_OP_MULTIPLY_FLOAT 10 #define TO_BOOL_LIST 13
#define BINARY_OP_ADD_FLOAT 13 #define TO_BOOL_NONE 14
#define BINARY_OP_SUBTRACT_FLOAT 14 #define TO_BOOL_STR 18
#define BINARY_OP_ADD_UNICODE 18 #define BINARY_OP_MULTIPLY_INT 19
#define BINARY_OP_INPLACE_ADD_UNICODE 19 #define BINARY_OP_ADD_INT 20
#define BINARY_SUBSCR_DICT 20 #define BINARY_OP_SUBTRACT_INT 21
#define BINARY_SUBSCR_GETITEM 21 #define BINARY_OP_MULTIPLY_FLOAT 22
#define BINARY_SUBSCR_LIST_INT 22 #define BINARY_OP_ADD_FLOAT 23
#define BINARY_SUBSCR_TUPLE_INT 23 #define BINARY_OP_SUBTRACT_FLOAT 28
#define STORE_SUBSCR_DICT 28 #define BINARY_OP_ADD_UNICODE 29
#define STORE_SUBSCR_LIST_INT 29 #define BINARY_OP_INPLACE_ADD_UNICODE 34
#define SEND_GEN 34 #define BINARY_SUBSCR_DICT 38
#define UNPACK_SEQUENCE_TWO_TUPLE 38 #define BINARY_SUBSCR_GETITEM 39
#define UNPACK_SEQUENCE_TUPLE 39 #define BINARY_SUBSCR_LIST_INT 42
#define UNPACK_SEQUENCE_LIST 42 #define BINARY_SUBSCR_TUPLE_INT 43
#define STORE_ATTR_INSTANCE_VALUE 43 #define STORE_SUBSCR_DICT 44
#define STORE_ATTR_SLOT 44 #define STORE_SUBSCR_LIST_INT 45
#define STORE_ATTR_WITH_HINT 45 #define SEND_GEN 46
#define LOAD_GLOBAL_MODULE 46 #define UNPACK_SEQUENCE_TWO_TUPLE 47
#define LOAD_GLOBAL_BUILTIN 47 #define UNPACK_SEQUENCE_TUPLE 48
#define LOAD_SUPER_ATTR_ATTR 48 #define UNPACK_SEQUENCE_LIST 56
#define LOAD_SUPER_ATTR_METHOD 56 #define STORE_ATTR_INSTANCE_VALUE 57
#define LOAD_ATTR_INSTANCE_VALUE 57 #define STORE_ATTR_SLOT 58
#define LOAD_ATTR_MODULE 58 #define STORE_ATTR_WITH_HINT 59
#define LOAD_ATTR_WITH_HINT 59 #define LOAD_GLOBAL_MODULE 62
#define LOAD_ATTR_SLOT 62 #define LOAD_GLOBAL_BUILTIN 63
#define LOAD_ATTR_CLASS 63 #define LOAD_SUPER_ATTR_ATTR 64
#define LOAD_ATTR_PROPERTY 64 #define LOAD_SUPER_ATTR_METHOD 65
#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 65 #define LOAD_ATTR_INSTANCE_VALUE 66
#define LOAD_ATTR_METHOD_WITH_VALUES 66 #define LOAD_ATTR_MODULE 67
#define LOAD_ATTR_METHOD_NO_DICT 67 #define LOAD_ATTR_WITH_HINT 70
#define LOAD_ATTR_METHOD_LAZY_DICT 70 #define LOAD_ATTR_SLOT 72
#define COMPARE_OP_FLOAT 72 #define LOAD_ATTR_CLASS 73
#define COMPARE_OP_INT 73 #define LOAD_ATTR_PROPERTY 76
#define COMPARE_OP_STR 76 #define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 77
#define FOR_ITER_LIST 77 #define LOAD_ATTR_METHOD_WITH_VALUES 78
#define FOR_ITER_TUPLE 78 #define LOAD_ATTR_METHOD_NO_DICT 79
#define FOR_ITER_RANGE 79 #define LOAD_ATTR_METHOD_LAZY_DICT 80
#define FOR_ITER_GEN 80 #define COMPARE_OP_FLOAT 81
#define CALL_BOUND_METHOD_EXACT_ARGS 81 #define COMPARE_OP_INT 82
#define CALL_PY_EXACT_ARGS 82 #define COMPARE_OP_STR 84
#define CALL_PY_WITH_DEFAULTS 84 #define FOR_ITER_LIST 86
#define CALL_NO_KW_TYPE_1 86 #define FOR_ITER_TUPLE 88
#define CALL_NO_KW_STR_1 88 #define FOR_ITER_RANGE 111
#define CALL_NO_KW_TUPLE_1 111 #define FOR_ITER_GEN 112
#define CALL_BUILTIN_CLASS 112 #define CALL_BOUND_METHOD_EXACT_ARGS 113
#define CALL_NO_KW_BUILTIN_O 113 #define CALL_PY_EXACT_ARGS 132
#define CALL_NO_KW_BUILTIN_FAST 132 #define CALL_PY_WITH_DEFAULTS 136
#define CALL_BUILTIN_FAST_WITH_KEYWORDS 136 #define CALL_NO_KW_TYPE_1 148
#define CALL_NO_KW_LEN 148 #define CALL_NO_KW_STR_1 153
#define CALL_NO_KW_ISINSTANCE 153 #define CALL_NO_KW_TUPLE_1 154
#define CALL_NO_KW_LIST_APPEND 154 #define CALL_BUILTIN_CLASS 155
#define CALL_NO_KW_METHOD_DESCRIPTOR_O 155 #define CALL_NO_KW_BUILTIN_O 159
#define CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS 159 #define CALL_NO_KW_BUILTIN_FAST 160
#define CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS 160 #define CALL_BUILTIN_FAST_WITH_KEYWORDS 161
#define CALL_NO_KW_METHOD_DESCRIPTOR_FAST 161 #define CALL_NO_KW_LEN 166
#define CALL_NO_KW_ALLOC_AND_ENTER_INIT 166 #define CALL_NO_KW_ISINSTANCE 167
#define CALL_NO_KW_LIST_APPEND 178
#define CALL_NO_KW_METHOD_DESCRIPTOR_O 179
#define CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS 180
#define CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS 181
#define CALL_NO_KW_METHOD_DESCRIPTOR_FAST 182
#define CALL_NO_KW_ALLOC_AND_ENTER_INIT 183
#define NB_ADD 0 #define NB_ADD 0
#define NB_AND 1 #define NB_AND 1

View File

@ -4,6 +4,14 @@
# Do not edit! # Do not edit!
_specializations = { _specializations = {
"TO_BOOL": [
"TO_BOOL_ALWAYS_TRUE",
"TO_BOOL_BOOL",
"TO_BOOL_INT",
"TO_BOOL_LIST",
"TO_BOOL_NONE",
"TO_BOOL_STR",
],
"BINARY_OP": [ "BINARY_OP": [
"BINARY_OP_MULTIPLY_INT", "BINARY_OP_MULTIPLY_INT",
"BINARY_OP_ADD_INT", "BINARY_OP_ADD_INT",

View File

@ -572,8 +572,10 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
elif deop in haslocal or deop in hasfree: elif deop in haslocal or deop in hasfree:
argval, argrepr = _get_name_info(arg, varname_from_oparg) argval, argrepr = _get_name_info(arg, varname_from_oparg)
elif deop in hascompare: elif deop in hascompare:
argval = cmp_op[arg>>4] argval = cmp_op[arg >> 5]
argrepr = argval argrepr = argval
if arg & 16:
argrepr = f"bool({argrepr})"
elif deop == CONVERT_VALUE: elif deop == CONVERT_VALUE:
argval = (None, str, repr, ascii)[arg] argval = (None, str, repr, ascii)[arg]
argrepr = ('', 'str', 'repr', 'ascii')[arg] argrepr = ('', 'str', 'repr', 'ascii')[arg]

View File

@ -452,6 +452,7 @@ _code_type = type(_write_atomic.__code__)
# Python 3.13a1 3554 (more efficient bytecodes for f-strings) # Python 3.13a1 3554 (more efficient bytecodes for f-strings)
# Python 3.13a1 3555 (generate specialized opcodes metadata from bytecodes.c) # Python 3.13a1 3555 (generate specialized opcodes metadata from bytecodes.c)
# Python 3.13a1 3556 (Convert LOAD_CLOSURE to a pseudo-op) # Python 3.13a1 3556 (Convert LOAD_CLOSURE to a pseudo-op)
# Python 3.13a1 3557 (Make the conversion to boolean in jumps explicit)
# Python 3.14 will start with 3600 # Python 3.14 will start with 3600
@ -468,7 +469,7 @@ _code_type = type(_write_atomic.__code__)
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array # Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated. # in PC/launcher.c must also be updated.
MAGIC_NUMBER = (3556).to_bytes(2, 'little') + b'\r\n' MAGIC_NUMBER = (3557).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

View File

@ -87,9 +87,9 @@ def_op('CACHE', 0)
def_op('POP_TOP', 1) def_op('POP_TOP', 1)
def_op('PUSH_NULL', 2) def_op('PUSH_NULL', 2)
def_op('INTERPRETER_EXIT', 3) def_op('INTERPRETER_EXIT', 3)
def_op('END_FOR', 4) def_op('END_FOR', 4)
def_op('END_SEND', 5) def_op('END_SEND', 5)
def_op('TO_BOOL', 6)
def_op('NOP', 9) def_op('NOP', 9)
@ -142,6 +142,7 @@ def_op('RETURN_GENERATOR', 75)
def_op('RETURN_VALUE', 83) def_op('RETURN_VALUE', 83)
def_op('SETUP_ANNOTATIONS', 85) def_op('SETUP_ANNOTATIONS', 85)
def_op('LOAD_LOCALS', 87) def_op('LOAD_LOCALS', 87)
def_op('POP_EXCEPT', 89) def_op('POP_EXCEPT', 89)
@ -171,6 +172,7 @@ hascompare.append(107)
name_op('IMPORT_NAME', 108) # Index in name list name_op('IMPORT_NAME', 108) # Index in name list
name_op('IMPORT_FROM', 109) # Index in name list name_op('IMPORT_FROM', 109) # Index in name list
jrel_op('JUMP_FORWARD', 110) # Number of words to skip jrel_op('JUMP_FORWARD', 110) # Number of words to skip
jrel_op('POP_JUMP_IF_FALSE', 114) jrel_op('POP_JUMP_IF_FALSE', 114)
jrel_op('POP_JUMP_IF_TRUE', 115) jrel_op('POP_JUMP_IF_TRUE', 115)
name_op('LOAD_GLOBAL', 116) # Index in name list name_op('LOAD_GLOBAL', 116) # Index in name list
@ -209,7 +211,6 @@ name_op('LOAD_SUPER_ATTR', 141)
def_op('CALL_FUNCTION_EX', 142) # Flags def_op('CALL_FUNCTION_EX', 142) # Flags
def_op('LOAD_FAST_AND_CLEAR', 143) # Local variable number def_op('LOAD_FAST_AND_CLEAR', 143) # Local variable number
haslocal.append(143) haslocal.append(143)
def_op('EXTENDED_ARG', 144) def_op('EXTENDED_ARG', 144)
EXTENDED_ARG = 144 EXTENDED_ARG = 144
def_op('LIST_APPEND', 145) def_op('LIST_APPEND', 145)
@ -238,7 +239,6 @@ def_op('KW_NAMES', 172)
hasconst.append(172) hasconst.append(172)
def_op('CALL_INTRINSIC_1', 173) def_op('CALL_INTRINSIC_1', 173)
def_op('CALL_INTRINSIC_2', 174) def_op('CALL_INTRINSIC_2', 174)
name_op('LOAD_FROM_DICT_OR_GLOBALS', 175) name_op('LOAD_FROM_DICT_OR_GLOBALS', 175)
def_op('LOAD_FROM_DICT_OR_DEREF', 176) def_op('LOAD_FROM_DICT_OR_DEREF', 176)
hasfree.append(176) hasfree.append(176)
@ -404,6 +404,10 @@ _cache_format = {
"JUMP_BACKWARD": { "JUMP_BACKWARD": {
"counter": 1, "counter": 1,
}, },
"TO_BOOL": {
"counter": 1,
"version": 2,
},
} }
_inline_cache_entries = [ _inline_cache_entries = [

View File

@ -18,6 +18,7 @@ class IsolatedCodeGenTests(CodegenTestCase):
expected = [ expected = [
('RESUME', 0, 0), ('RESUME', 0, 0),
('LOAD_CONST', 0, 1), ('LOAD_CONST', 0, 1),
('TO_BOOL', 0, 1),
('POP_JUMP_IF_FALSE', false_lbl := self.Label(), 1), ('POP_JUMP_IF_FALSE', false_lbl := self.Label(), 1),
('LOAD_CONST', 1, 1), ('LOAD_CONST', 1, 1),
('JUMP', exit_lbl := self.Label()), ('JUMP', exit_lbl := self.Label()),

View File

@ -46,7 +46,7 @@ dis_c_instance_method = """\
%3d LOAD_FAST 1 (x) %3d LOAD_FAST 1 (x)
LOAD_CONST 1 (1) LOAD_CONST 1 (1)
COMPARE_OP 40 (==) COMPARE_OP 72 (==)
LOAD_FAST 0 (self) LOAD_FAST 0 (self)
STORE_ATTR 0 (x) STORE_ATTR 0 (x)
RETURN_CONST 0 (None) RETURN_CONST 0 (None)
@ -56,7 +56,7 @@ dis_c_instance_method_bytes = """\
RESUME 0 RESUME 0
LOAD_FAST 1 LOAD_FAST 1
LOAD_CONST 1 LOAD_CONST 1
COMPARE_OP 40 (==) COMPARE_OP 72 (==)
LOAD_FAST 0 LOAD_FAST 0
STORE_ATTR 0 STORE_ATTR 0
RETURN_CONST 0 RETURN_CONST 0
@ -67,7 +67,7 @@ dis_c_class_method = """\
%3d LOAD_FAST 1 (x) %3d LOAD_FAST 1 (x)
LOAD_CONST 1 (1) LOAD_CONST 1 (1)
COMPARE_OP 40 (==) COMPARE_OP 72 (==)
LOAD_FAST 0 (cls) LOAD_FAST 0 (cls)
STORE_ATTR 0 (x) STORE_ATTR 0 (x)
RETURN_CONST 0 (None) RETURN_CONST 0 (None)
@ -78,7 +78,7 @@ dis_c_static_method = """\
%3d LOAD_FAST 0 (x) %3d LOAD_FAST 0 (x)
LOAD_CONST 1 (1) LOAD_CONST 1 (1)
COMPARE_OP 40 (==) COMPARE_OP 72 (==)
STORE_FAST 0 (x) STORE_FAST 0 (x)
RETURN_CONST 0 (None) RETURN_CONST 0 (None)
""" % (_C.sm.__code__.co_firstlineno, _C.sm.__code__.co_firstlineno + 2,) """ % (_C.sm.__code__.co_firstlineno, _C.sm.__code__.co_firstlineno + 2,)
@ -488,7 +488,8 @@ dis_with = """\
%3d >> PUSH_EXC_INFO %3d >> PUSH_EXC_INFO
WITH_EXCEPT_START WITH_EXCEPT_START
POP_JUMP_IF_TRUE 1 (to 42) TO_BOOL
POP_JUMP_IF_TRUE 1 (to 50)
RERAISE 2 RERAISE 2
>> POP_TOP >> POP_TOP
POP_EXCEPT POP_EXCEPT
@ -567,7 +568,8 @@ dis_asyncwith = """\
JUMP_BACKWARD_NO_INTERRUPT 5 (to 90) JUMP_BACKWARD_NO_INTERRUPT 5 (to 90)
>> CLEANUP_THROW >> CLEANUP_THROW
>> END_SEND >> END_SEND
POP_JUMP_IF_TRUE 1 (to 108) TO_BOOL
POP_JUMP_IF_TRUE 1 (to 116)
RERAISE 2 RERAISE 2
>> POP_TOP >> POP_TOP
POP_EXCEPT POP_EXCEPT
@ -1570,22 +1572,41 @@ expected_jumpy_line = 1
def _stringify_instruction(instr): def _stringify_instruction(instr):
# Since line numbers and other offsets change a lot for these # Since line numbers and other offsets change a lot for these
# test cases, ignore them. # test cases, ignore them.
return repr(instr._replace(positions=None)) return f" {instr._replace(positions=None)!r},"
def _prepare_test_cases(): def _prepare_test_cases():
_instructions = dis.get_instructions(outer, first_line=expected_outer_line) ignore = io.StringIO()
print('expected_opinfo_outer = [\n ', with contextlib.redirect_stdout(ignore):
',\n '.join(map(_stringify_instruction, _instructions)), ',\n]', sep='') f = outer()
_instructions = dis.get_instructions(outer(), first_line=expected_f_line) inner = f()
print('expected_opinfo_f = [\n ', _instructions_outer = dis.get_instructions(outer, first_line=expected_outer_line)
',\n '.join(map(_stringify_instruction, _instructions)), ',\n]', sep='') _instructions_f = dis.get_instructions(f, first_line=expected_f_line)
_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line) _instructions_inner = dis.get_instructions(inner, first_line=expected_inner_line)
print('expected_opinfo_inner = [\n ', _instructions_jumpy = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
',\n '.join(map(_stringify_instruction, _instructions)), ',\n]', sep='') result = "\n".join(
_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line) [
print('expected_opinfo_jumpy = [\n ', "expected_opinfo_outer = [",
',\n '.join(map(_stringify_instruction, _instructions)), ',\n]', sep='') *map(_stringify_instruction, _instructions_outer),
dis.dis(outer) "]",
"",
"expected_opinfo_f = [",
*map(_stringify_instruction, _instructions_f),
"]",
"",
"expected_opinfo_inner = [",
*map(_stringify_instruction, _instructions_inner),
"]",
"",
"expected_opinfo_jumpy = [",
*map(_stringify_instruction, _instructions_jumpy),
"]",
]
)
result = result.replace(repr(repr(code_object_f)), "repr(code_object_f)")
result = result.replace(repr(code_object_f), "code_object_f")
result = result.replace(repr(repr(code_object_inner)), "repr(code_object_inner)")
result = result.replace(repr(code_object_inner), "code_object_inner")
print(result)
# _prepare_test_cases() # _prepare_test_cases()
@ -1659,7 +1680,6 @@ expected_opinfo_inner = [
Instruction(opname='RETURN_CONST', opcode=121, arg=0, argval=None, argrepr='None', offset=34, start_offset=34, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='RETURN_CONST', opcode=121, arg=0, argval=None, argrepr='None', offset=34, start_offset=34, starts_line=None, is_jump_target=False, positions=None),
] ]
expected_opinfo_jumpy = [ expected_opinfo_jumpy = [
Instruction(opname='RESUME', opcode=151, arg=0, argval=0, argrepr='', offset=0, start_offset=0, starts_line=1, is_jump_target=False, positions=None), Instruction(opname='RESUME', opcode=151, arg=0, argval=0, argrepr='', offset=0, start_offset=0, starts_line=1, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='range', argrepr='NULL + range', offset=2, start_offset=2, starts_line=3, is_jump_target=False, positions=None), Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='range', argrepr='NULL + range', offset=2, start_offset=2, starts_line=3, is_jump_target=False, positions=None),
@ -1674,12 +1694,12 @@ expected_opinfo_jumpy = [
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=50, start_offset=50, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=50, start_offset=50, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=52, start_offset=52, starts_line=5, is_jump_target=False, positions=None), Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=52, start_offset=52, starts_line=5, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=54, start_offset=54, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=54, start_offset=54, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_OP', opcode=107, arg=2, argval='<', argrepr='<', offset=56, start_offset=56, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='COMPARE_OP', opcode=107, arg=18, argval='<', argrepr='bool(<)', offset=56, start_offset=56, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=2, argval=66, argrepr='to 66', offset=60, start_offset=60, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=2, argval=66, argrepr='to 66', offset=60, start_offset=60, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=21, argval=24, argrepr='to 24', offset=62, start_offset=62, starts_line=6, is_jump_target=False, positions=None), Instruction(opname='JUMP_BACKWARD', opcode=140, arg=21, argval=24, argrepr='to 24', offset=62, start_offset=62, starts_line=6, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=66, start_offset=66, starts_line=7, is_jump_target=True, positions=None), Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=66, start_offset=66, starts_line=7, is_jump_target=True, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=68, start_offset=68, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=68, start_offset=68, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_OP', opcode=107, arg=68, argval='>', argrepr='>', offset=70, start_offset=70, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='COMPARE_OP', opcode=107, arg=148, argval='>', argrepr='bool(>)', offset=70, start_offset=70, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=2, argval=80, argrepr='to 80', offset=74, start_offset=74, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=2, argval=80, argrepr='to 80', offset=74, start_offset=74, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=28, argval=24, argrepr='to 24', offset=76, start_offset=76, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='JUMP_BACKWARD', opcode=140, arg=28, argval=24, argrepr='to 24', offset=76, start_offset=76, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=80, start_offset=80, starts_line=8, is_jump_target=True, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=80, start_offset=80, starts_line=8, is_jump_target=True, positions=None),
@ -1690,90 +1710,93 @@ expected_opinfo_jumpy = [
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=98, start_offset=98, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=98, start_offset=98, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=106, start_offset=106, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=106, start_offset=106, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST_CHECK', opcode=127, arg=0, argval='i', argrepr='i', offset=108, start_offset=108, starts_line=11, is_jump_target=True, positions=None), Instruction(opname='LOAD_FAST_CHECK', opcode=127, arg=0, argval='i', argrepr='i', offset=108, start_offset=108, starts_line=11, is_jump_target=True, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=33, argval=178, argrepr='to 178', offset=110, start_offset=110, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='TO_BOOL', opcode=6, arg=None, argval=None, argrepr='', offset=110, start_offset=110, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=112, start_offset=112, starts_line=12, is_jump_target=True, positions=None), Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=37, argval=194, argrepr='to 194', offset=118, start_offset=118, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=122, start_offset=122, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=120, start_offset=120, starts_line=12, is_jump_target=True, positions=None),
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=124, start_offset=124, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=130, start_offset=130, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=132, start_offset=132, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=132, start_offset=132, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=134, start_offset=134, starts_line=13, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=140, start_offset=140, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=136, start_offset=136, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=142, start_offset=142, starts_line=13, is_jump_target=False, positions=None),
Instruction(opname='BINARY_OP', opcode=122, arg=23, argval=23, argrepr='-=', offset=138, start_offset=138, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=144, start_offset=144, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=142, start_offset=142, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='BINARY_OP', opcode=122, arg=23, argval=23, argrepr='-=', offset=146, start_offset=146, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=144, start_offset=144, starts_line=14, is_jump_target=False, positions=None), Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=150, start_offset=150, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=146, start_offset=146, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=152, start_offset=152, starts_line=14, is_jump_target=False, positions=None),
Instruction(opname='COMPARE_OP', opcode=107, arg=68, argval='>', argrepr='>', offset=148, start_offset=148, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=154, start_offset=154, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=2, argval=158, argrepr='to 158', offset=152, start_offset=152, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='COMPARE_OP', opcode=107, arg=148, argval='>', argrepr='bool(>)', offset=156, start_offset=156, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=25, argval=108, argrepr='to 108', offset=154, start_offset=154, starts_line=15, is_jump_target=False, positions=None), Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=2, argval=166, argrepr='to 166', offset=160, start_offset=160, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=158, start_offset=158, starts_line=16, is_jump_target=True, positions=None), Instruction(opname='JUMP_BACKWARD', opcode=140, arg=29, argval=108, argrepr='to 108', offset=162, start_offset=162, starts_line=15, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=160, start_offset=160, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=166, start_offset=166, starts_line=16, is_jump_target=True, positions=None),
Instruction(opname='COMPARE_OP', opcode=107, arg=2, argval='<', argrepr='<', offset=162, start_offset=162, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=168, start_offset=168, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=1, argval=170, argrepr='to 170', offset=166, start_offset=166, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='COMPARE_OP', opcode=107, arg=18, argval='<', argrepr='bool(<)', offset=170, start_offset=170, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_FORWARD', opcode=110, arg=15, argval=200, argrepr='to 200', offset=168, start_offset=168, starts_line=17, is_jump_target=False, positions=None), Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=1, argval=178, argrepr='to 178', offset=174, start_offset=174, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=170, start_offset=170, starts_line=11, is_jump_target=True, positions=None), Instruction(opname='JUMP_FORWARD', opcode=110, arg=19, argval=216, argrepr='to 216', offset=176, start_offset=176, starts_line=17, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=2, argval=178, argrepr='to 178', offset=172, start_offset=172, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=178, start_offset=178, starts_line=11, is_jump_target=True, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=33, argval=112, argrepr='to 112', offset=174, start_offset=174, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='TO_BOOL', opcode=6, arg=None, argval=None, argrepr='', offset=180, start_offset=180, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=178, start_offset=178, starts_line=19, is_jump_target=True, positions=None), Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=2, argval=194, argrepr='to 194', offset=188, start_offset=188, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=188, start_offset=188, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='JUMP_BACKWARD', opcode=140, arg=37, argval=120, argrepr='to 120', offset=190, start_offset=190, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=190, start_offset=190, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=194, start_offset=194, starts_line=19, is_jump_target=True, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=198, start_offset=198, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=204, start_offset=204, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=200, start_offset=200, starts_line=20, is_jump_target=True, positions=None), Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=206, start_offset=206, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=202, start_offset=202, starts_line=21, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=214, start_offset=214, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=204, start_offset=204, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='NOP', opcode=9, arg=None, argval=None, argrepr='', offset=216, start_offset=216, starts_line=20, is_jump_target=True, positions=None),
Instruction(opname='BINARY_OP', opcode=122, arg=11, argval=11, argrepr='/', offset=206, start_offset=206, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=218, start_offset=218, starts_line=21, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=210, start_offset=210, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=220, start_offset=220, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=212, start_offset=212, starts_line=25, is_jump_target=False, positions=None), Instruction(opname='BINARY_OP', opcode=122, arg=11, argval=11, argrepr='/', offset=222, start_offset=222, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='BEFORE_WITH', opcode=53, arg=None, argval=None, argrepr='', offset=214, start_offset=214, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=226, start_offset=226, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=216, start_offset=216, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=228, start_offset=228, starts_line=25, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=218, start_offset=218, starts_line=26, is_jump_target=False, positions=None), Instruction(opname='BEFORE_WITH', opcode=53, arg=None, argval=None, argrepr='', offset=230, start_offset=230, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Never reach this', argrepr="'Never reach this'", offset=228, start_offset=228, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=232, start_offset=232, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=230, start_offset=230, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=234, start_offset=234, starts_line=26, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=238, start_offset=238, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Never reach this', argrepr="'Never reach this'", offset=244, start_offset=244, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=240, start_offset=240, starts_line=25, is_jump_target=False, positions=None), Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=246, start_offset=246, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=242, start_offset=242, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=244, start_offset=244, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='CALL', opcode=171, arg=2, argval=2, argrepr='', offset=246, start_offset=246, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=254, start_offset=254, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=254, start_offset=254, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=256, start_offset=256, starts_line=28, is_jump_target=True, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=256, start_offset=256, starts_line=25, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=266, start_offset=266, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=258, start_offset=258, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=268, start_offset=268, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=260, start_offset=260, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=276, start_offset=276, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='CALL', opcode=171, arg=2, argval=2, argrepr='', offset=262, start_offset=262, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='RETURN_CONST', opcode=121, arg=0, argval=None, argrepr='None', offset=278, start_offset=278, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=270, start_offset=270, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=280, start_offset=280, starts_line=25, is_jump_target=False, positions=None), Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=272, start_offset=272, starts_line=28, is_jump_target=True, positions=None),
Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=282, start_offset=282, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=282, start_offset=282, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=1, argval=288, argrepr='to 288', offset=284, start_offset=284, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=284, start_offset=284, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='RERAISE', opcode=119, arg=2, argval=2, argrepr='', offset=286, start_offset=286, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=288, start_offset=288, starts_line=None, is_jump_target=True, positions=None),
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=290, start_offset=290, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=292, start_offset=292, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=292, start_offset=292, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=294, start_offset=294, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='RETURN_CONST', opcode=121, arg=0, argval=None, argrepr='None', offset=294, start_offset=294, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=22, argval=256, argrepr='to 256', offset=296, start_offset=296, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=296, start_offset=296, starts_line=25, is_jump_target=False, positions=None),
Instruction(opname='COPY', opcode=120, arg=3, argval=3, argrepr='', offset=300, start_offset=300, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=298, start_offset=298, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=302, start_offset=302, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='TO_BOOL', opcode=6, arg=None, argval=None, argrepr='', offset=300, start_offset=300, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='RERAISE', opcode=119, arg=1, argval=1, argrepr='', offset=304, start_offset=304, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=1, argval=312, argrepr='to 312', offset=308, start_offset=308, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=306, start_offset=306, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='RERAISE', opcode=119, arg=2, argval=2, argrepr='', offset=310, start_offset=310, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=4, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=308, start_offset=308, starts_line=22, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=312, start_offset=312, starts_line=None, is_jump_target=True, positions=None),
Instruction(opname='CHECK_EXC_MATCH', opcode=36, arg=None, argval=None, argrepr='', offset=318, start_offset=318, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=314, start_offset=314, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=15, argval=352, argrepr='to 352', offset=320, start_offset=320, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=316, start_offset=316, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=322, start_offset=322, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=318, start_offset=318, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=324, start_offset=324, starts_line=23, is_jump_target=False, positions=None), Instruction(opname='JUMP_BACKWARD', opcode=140, arg=26, argval=272, argrepr='to 272', offset=320, start_offset=320, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=334, start_offset=334, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='COPY', opcode=120, arg=3, argval=3, argrepr='', offset=324, start_offset=324, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=336, start_offset=336, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=326, start_offset=326, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=344, start_offset=344, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='RERAISE', opcode=119, arg=1, argval=1, argrepr='', offset=328, start_offset=328, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=346, start_offset=346, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=330, start_offset=330, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=48, argval=256, argrepr='to 256', offset=348, start_offset=348, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_GLOBAL', opcode=116, arg=4, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=332, start_offset=332, starts_line=22, is_jump_target=False, positions=None),
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=352, start_offset=352, starts_line=22, is_jump_target=True, positions=None), Instruction(opname='CHECK_EXC_MATCH', opcode=36, arg=None, argval=None, argrepr='', offset=342, start_offset=342, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COPY', opcode=120, arg=3, argval=3, argrepr='', offset=354, start_offset=354, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=15, argval=376, argrepr='to 376', offset=344, start_offset=344, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=356, start_offset=356, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=346, start_offset=346, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='RERAISE', opcode=119, arg=1, argval=1, argrepr='', offset=358, start_offset=358, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=348, start_offset=348, starts_line=23, is_jump_target=False, positions=None),
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=360, start_offset=360, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=358, start_offset=358, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=362, start_offset=362, starts_line=28, is_jump_target=False, positions=None), Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=360, start_offset=360, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=372, start_offset=372, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=368, start_offset=368, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=374, start_offset=374, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=370, start_offset=370, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=382, start_offset=382, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='JUMP_BACKWARD', opcode=140, arg=52, argval=272, argrepr='to 272', offset=372, start_offset=372, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=384, start_offset=384, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=376, start_offset=376, starts_line=22, is_jump_target=True, positions=None),
Instruction(opname='COPY', opcode=120, arg=3, argval=3, argrepr='', offset=386, start_offset=386, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='COPY', opcode=120, arg=3, argval=3, argrepr='', offset=378, start_offset=378, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=388, start_offset=388, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=380, start_offset=380, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='RERAISE', opcode=119, arg=1, argval=1, argrepr='', offset=390, start_offset=390, starts_line=None, is_jump_target=False, positions=None), Instruction(opname='RERAISE', opcode=119, arg=1, argval=1, argrepr='', offset=382, start_offset=382, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='PUSH_EXC_INFO', opcode=35, arg=None, argval=None, argrepr='', offset=384, start_offset=384, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=386, start_offset=386, starts_line=28, is_jump_target=False, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=396, start_offset=396, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=398, start_offset=398, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=406, start_offset=406, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='RERAISE', opcode=119, arg=0, argval=0, argrepr='', offset=408, start_offset=408, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='COPY', opcode=120, arg=3, argval=3, argrepr='', offset=410, start_offset=410, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=412, start_offset=412, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='RERAISE', opcode=119, arg=1, argval=1, argrepr='', offset=414, start_offset=414, starts_line=None, is_jump_target=False, positions=None),
] ]
# One last piece of inspect fodder to check the default line number handling # One last piece of inspect fodder to check the default line number handling

View File

@ -0,0 +1,5 @@
Add a new :opcode:`TO_BOOL` instruction, which performs boolean conversions
for :opcode:`POP_JUMP_IF_TRUE`, :opcode:`POP_JUMP_IF_FALSE`, and
:opcode:`UNARY_NOT` (which all expect exact :class:`bool` values now). Also,
modify the oparg of :opcode:`COMPARE_OP` to include an optional "boolean
conversion" flag.

View File

@ -279,15 +279,90 @@ dummy_func(
} }
inst(UNARY_NOT, (value -- res)) { 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); int err = PyObject_IsTrue(value);
DECREF_INPUTS(); DECREF_INPUTS();
ERROR_IF(err < 0, error); ERROR_IF(err < 0, error);
if (err == 0) { res = err ? Py_True : Py_False;
res = Py_True;
} }
else {
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; 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)) { inst(UNARY_INVERT, (value -- res)) {
@ -2024,10 +2099,16 @@ dummy_func(
STAT_INC(COMPARE_OP, deferred); STAT_INC(COMPARE_OP, deferred);
DECREMENT_ADAPTIVE_COUNTER(cache->counter); DECREMENT_ADAPTIVE_COUNTER(cache->counter);
#endif /* ENABLE_SPECIALIZATION */ #endif /* ENABLE_SPECIALIZATION */
assert((oparg >> 4) <= Py_GE); assert((oparg >> 5) <= Py_GE);
res = PyObject_RichCompare(left, right, oparg>>4); res = PyObject_RichCompare(left, right, oparg >> 5);
DECREF_INPUTS(); DECREF_INPUTS();
ERROR_IF(res == NULL, error); 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)) { inst(COMPARE_OP_FLOAT, (unused/1, left, right -- res)) {
@ -2041,6 +2122,7 @@ dummy_func(
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc); _Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc); _Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
res = (sign_ish & oparg) ? Py_True : Py_False; 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 // Similar to COMPARE_OP_FLOAT
@ -2059,6 +2141,7 @@ dummy_func(
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free); _Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free); _Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
res = (sign_ish & oparg) ? Py_True : Py_False; 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 // Similar to COMPARE_OP_FLOAT, but for ==, != only
@ -2067,13 +2150,14 @@ dummy_func(
DEOPT_IF(!PyUnicode_CheckExact(right), COMPARE_OP); DEOPT_IF(!PyUnicode_CheckExact(right), COMPARE_OP);
STAT_INC(COMPARE_OP, hit); STAT_INC(COMPARE_OP, hit);
int eq = _PyUnicode_Equal(left, right); 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(left, _PyUnicode_ExactDealloc);
_Py_DECREF_SPECIALIZED(right, _PyUnicode_ExactDealloc); _Py_DECREF_SPECIALIZED(right, _PyUnicode_ExactDealloc);
assert(eq == 0 || eq == 1); assert(eq == 0 || eq == 1);
assert((oparg & 0xf) == COMPARISON_NOT_EQUALS || (oparg & 0xf) == COMPARISON_EQUALS); assert((oparg & 0xf) == COMPARISON_NOT_EQUALS || (oparg & 0xf) == COMPARISON_EQUALS);
assert(COMPARISON_NOT_EQUALS + 1 == COMPARISON_EQUALS); assert(COMPARISON_NOT_EQUALS + 1 == COMPARISON_EQUALS);
res = ((COMPARISON_NOT_EQUALS + eq) & oparg) ? Py_True : Py_False; 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)) { inst(IS_OP, (left, right -- b)) {
@ -2183,35 +2267,13 @@ dummy_func(
} }
inst(POP_JUMP_IF_FALSE, (cond -- )) { inst(POP_JUMP_IF_FALSE, (cond -- )) {
if (Py_IsFalse(cond)) { assert(PyBool_Check(cond));
JUMPBY(oparg); JUMPBY(oparg * Py_IsFalse(cond));
}
else if (!Py_IsTrue(cond)) {
int err = PyObject_IsTrue(cond);
DECREF_INPUTS();
if (err == 0) {
JUMPBY(oparg);
}
else {
ERROR_IF(err < 0, error);
}
}
} }
inst(POP_JUMP_IF_TRUE, (cond -- )) { inst(POP_JUMP_IF_TRUE, (cond -- )) {
if (Py_IsTrue(cond)) { assert(PyBool_Check(cond));
JUMPBY(oparg); JUMPBY(oparg * Py_IsTrue(cond));
}
else if (!Py_IsFalse(cond)) {
int err = PyObject_IsTrue(cond);
DECREF_INPUTS();
if (err > 0) {
JUMPBY(oparg);
}
else {
ERROR_IF(err < 0, error);
}
}
} }
inst(POP_JUMP_IF_NOT_NONE, (value -- )) { inst(POP_JUMP_IF_NOT_NONE, (value -- )) {
@ -3515,23 +3577,17 @@ dummy_func(
inst(INSTRUMENTED_POP_JUMP_IF_TRUE, ( -- )) { inst(INSTRUMENTED_POP_JUMP_IF_TRUE, ( -- )) {
PyObject *cond = POP(); PyObject *cond = POP();
int err = PyObject_IsTrue(cond); assert(PyBool_Check(cond));
Py_DECREF(cond);
ERROR_IF(err < 0, error);
_Py_CODEUNIT *here = next_instr - 1; _Py_CODEUNIT *here = next_instr - 1;
assert(err == 0 || err == 1); int offset = Py_IsTrue(cond) * oparg;
int offset = err*oparg;
INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH); INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
} }
inst(INSTRUMENTED_POP_JUMP_IF_FALSE, ( -- )) { inst(INSTRUMENTED_POP_JUMP_IF_FALSE, ( -- )) {
PyObject *cond = POP(); PyObject *cond = POP();
int err = PyObject_IsTrue(cond); assert(PyBool_Check(cond));
Py_DECREF(cond);
ERROR_IF(err < 0, error);
_Py_CODEUNIT *here = next_instr - 1; _Py_CODEUNIT *here = next_instr - 1;
assert(err == 0 || err == 1); int offset = Py_IsFalse(cond) * oparg;
int offset = (1-err)*oparg;
INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH); INSTRUMENTED_JUMP(here, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
} }

View File

@ -2790,9 +2790,11 @@ static int compiler_addcompare(struct compiler *c, location loc,
default: default:
Py_UNREACHABLE(); Py_UNREACHABLE();
} }
/* cmp goes in top bits of the oparg, while the low bits are used by quickened // cmp goes in top three bits of the oparg, while the low four bits are used
* versions of this opcode to store the comparison mask. */ // by quickened versions of this opcode to store the comparison mask. The
ADDOP_I(c, loc, COMPARE_OP, (cmp << 4) | compare_masks[cmp]); // 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; 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), SWAP, 2);
ADDOP_I(c, LOC(e), COPY, 2); ADDOP_I(c, LOC(e), COPY, 2);
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, i)); 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); ADDOP_JUMP(c, LOC(e), POP_JUMP_IF_FALSE, cleanup);
} }
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); 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_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); ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
NEW_JUMP_TARGET_LABEL(c, end); NEW_JUMP_TARGET_LABEL(c, end);
ADDOP_JUMP(c, NO_LOCATION, JUMP, end); ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
@ -2885,6 +2889,7 @@ compiler_jump_if(struct compiler *c, location loc,
/* general implementation */ /* general implementation */
VISIT(c, expr, e); 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); ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
return SUCCESS; return SUCCESS;
} }
@ -4016,8 +4021,6 @@ unaryop(unaryop_ty op)
switch (op) { switch (op) {
case Invert: case Invert:
return UNARY_INVERT; return UNARY_INVERT;
case Not:
return UNARY_NOT;
case USub: case USub:
return UNARY_NEGATIVE; return UNARY_NEGATIVE;
default: default:
@ -4247,6 +4250,7 @@ compiler_boolop(struct compiler *c, expr_ty e)
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
ADDOP_I(c, loc, COPY, 1); ADDOP_I(c, loc, COPY, 1);
ADDOP(c, loc, TO_BOOL);
ADDOP_JUMP(c, loc, jumpi, end); ADDOP_JUMP(c, loc, jumpi, end);
ADDOP(c, loc, POP_TOP); ADDOP(c, loc, POP_TOP);
} }
@ -4554,6 +4558,7 @@ compiler_compare(struct compiler *c, expr_ty e)
ADDOP_I(c, loc, COPY, 2); ADDOP_I(c, loc, COPY, 2);
ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, i)); ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, i));
ADDOP_I(c, loc, COPY, 1); ADDOP_I(c, loc, COPY, 1);
ADDOP(c, loc, TO_BOOL);
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, cleanup); ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, cleanup);
ADDOP(c, loc, POP_TOP); ADDOP(c, loc, POP_TOP);
} }
@ -5789,6 +5794,7 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
static int static int
compiler_with_except_finish(struct compiler *c, jump_target_label cleanup) { compiler_with_except_finish(struct compiler *c, jump_target_label cleanup) {
NEW_JUMP_TARGET_LABEL(c, suppress); NEW_JUMP_TARGET_LABEL(c, suppress);
ADDOP(c, NO_LOCATION, TO_BOOL);
ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress); ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress);
ADDOP_I(c, NO_LOCATION, RERAISE, 2); 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) { if (e->v.UnaryOp.op == UAdd) {
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_UNARY_POSITIVE); 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 { else {
ADDOP(c, loc, unaryop(e->v.UnaryOp.op)); 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); VISIT(c, expr, value);
ADDOP_COMPARE(c, LOC(p), Eq); 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_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
return SUCCESS; return SUCCESS;
} }

File diff suppressed because it is too large Load Diff

View File

@ -1044,6 +1044,36 @@ get_const_value(int opcode, int oparg, PyObject *co_consts)
return Py_NewRef(constant); 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 /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
with LOAD_CONST (c1, c2, ... cn). with LOAD_CONST (c1, c2, ... cn).
The consts table must still be in list form so that the 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); PyTuple_SET_ITEM(newconst, i, constant);
} }
if (_PyCompile_ConstCacheMergeOne(const_cache, &newconst) < 0) { int index = add_const(newconst, consts, const_cache);
Py_DECREF(newconst); if (index < 0) {
return ERROR; 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++) { for (int i = 0; i < n; i++) {
INSTR_SET_OP0(&inst[i], NOP); 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; return SUCCESS;
} }
@ -1361,24 +1372,71 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
} }
break; break;
case IS_OP: 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); cnt = get_const_value(opcode, oparg, consts);
if (cnt == NULL) { if (cnt == NULL) {
goto error; goto error;
} }
int jump_op = i+2 < bb->b_iused ? bb->b_instr[i+2].i_opcode : 0; if (!Py_IsNone(cnt)) {
if (Py_IsNone(cnt) && (jump_op == POP_JUMP_IF_FALSE || jump_op == POP_JUMP_IF_TRUE)) { break;
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;
} }
Py_DECREF(cnt); 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; break;
case RETURN_VALUE: case RETURN_VALUE:
INSTR_SET_OP0(inst, NOP); INSTR_SET_OP0(inst, NOP);
INSTR_SET_OP1(&bb->b_instr[++i], RETURN_CONST, oparg); INSTR_SET_OP1(&bb->b_instr[++i], RETURN_CONST, oparg);
break; 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; break;
} }
@ -1464,6 +1522,39 @@ optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
inst[1].i_oparg |= 1; inst[1].i_oparg |= 1;
} }
break; 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: default:
/* All OPCODE_HAS_CONST opcodes should be handled with LOAD_CONST */ /* All OPCODE_HAS_CONST opcodes should be handled with LOAD_CONST */
assert (!OPCODE_HAS_CONST(inst->i_opcode)); assert (!OPCODE_HAS_CONST(inst->i_opcode));

File diff suppressed because it is too large Load Diff

View File

@ -82,6 +82,20 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
return 1; return 1;
case UNARY_NOT: case UNARY_NOT:
return 1; 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: case UNARY_INVERT:
return 1; return 1;
case BINARY_OP_MULTIPLY_INT: case BINARY_OP_MULTIPLY_INT:
@ -508,6 +522,20 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
return 1; return 1;
case UNARY_NOT: case UNARY_NOT:
return 1; 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: case UNARY_INVERT:
return 1; return 1;
case BINARY_OP_MULTIPLY_INT: case BINARY_OP_MULTIPLY_INT:
@ -886,7 +914,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
} }
#endif #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_ARG_FLAG (1)
#define HAS_CONST_FLAG (2) #define HAS_CONST_FLAG (2)
#define HAS_NAME_FLAG (4) #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 }, [INSTRUMENTED_END_SEND] = { true, INSTR_FMT_IX, 0 },
[UNARY_NEGATIVE] = { true, INSTR_FMT_IX, 0 }, [UNARY_NEGATIVE] = { true, INSTR_FMT_IX, 0 },
[UNARY_NOT] = { 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 }, [UNARY_INVERT] = { true, INSTR_FMT_IX, 0 },
[BINARY_OP_MULTIPLY_INT] = { true, INSTR_FMT_IBC, 0 }, [BINARY_OP_MULTIPLY_INT] = { true, INSTR_FMT_IBC, 0 },
[BINARY_OP_ADD_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 } } }, [END_SEND] = { .nuops = 1, .uops = { { END_SEND, 0, 0 } } },
[UNARY_NEGATIVE] = { .nuops = 1, .uops = { { UNARY_NEGATIVE, 0, 0 } } }, [UNARY_NEGATIVE] = { .nuops = 1, .uops = { { UNARY_NEGATIVE, 0, 0 } } },
[UNARY_NOT] = { .nuops = 1, .uops = { { UNARY_NOT, 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 } } }, [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_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 } } }, [BINARY_OP_ADD_INT] = { .nuops = 2, .uops = { { _GUARD_BOTH_INT, 0, 0 }, { _BINARY_OP_ADD_INT, 0, 0 } } },

122
Python/opcode_targets.h generated
View File

@ -5,49 +5,49 @@ static void *opcode_targets[256] = {
&&TARGET_INTERPRETER_EXIT, &&TARGET_INTERPRETER_EXIT,
&&TARGET_END_FOR, &&TARGET_END_FOR,
&&TARGET_END_SEND, &&TARGET_END_SEND,
&&TARGET_BINARY_OP_MULTIPLY_INT, &&TARGET_TO_BOOL,
&&TARGET_BINARY_OP_ADD_INT, &&TARGET_TO_BOOL_ALWAYS_TRUE,
&&TARGET_BINARY_OP_SUBTRACT_INT, &&TARGET_TO_BOOL_BOOL,
&&TARGET_NOP, &&TARGET_NOP,
&&TARGET_BINARY_OP_MULTIPLY_FLOAT, &&TARGET_TO_BOOL_INT,
&&TARGET_UNARY_NEGATIVE, &&TARGET_UNARY_NEGATIVE,
&&TARGET_UNARY_NOT, &&TARGET_UNARY_NOT,
&&TARGET_BINARY_OP_ADD_FLOAT, &&TARGET_TO_BOOL_LIST,
&&TARGET_BINARY_OP_SUBTRACT_FLOAT, &&TARGET_TO_BOOL_NONE,
&&TARGET_UNARY_INVERT, &&TARGET_UNARY_INVERT,
&&TARGET_EXIT_INIT_CHECK, &&TARGET_EXIT_INIT_CHECK,
&&TARGET_RESERVED, &&TARGET_RESERVED,
&&TARGET_BINARY_OP_ADD_UNICODE, &&TARGET_TO_BOOL_STR,
&&TARGET_BINARY_OP_INPLACE_ADD_UNICODE, &&TARGET_BINARY_OP_MULTIPLY_INT,
&&TARGET_BINARY_SUBSCR_DICT, &&TARGET_BINARY_OP_ADD_INT,
&&TARGET_BINARY_SUBSCR_GETITEM, &&TARGET_BINARY_OP_SUBTRACT_INT,
&&TARGET_BINARY_SUBSCR_LIST_INT, &&TARGET_BINARY_OP_MULTIPLY_FLOAT,
&&TARGET_BINARY_SUBSCR_TUPLE_INT, &&TARGET_BINARY_OP_ADD_FLOAT,
&&TARGET_MAKE_FUNCTION, &&TARGET_MAKE_FUNCTION,
&&TARGET_BINARY_SUBSCR, &&TARGET_BINARY_SUBSCR,
&&TARGET_BINARY_SLICE, &&TARGET_BINARY_SLICE,
&&TARGET_STORE_SLICE, &&TARGET_STORE_SLICE,
&&TARGET_STORE_SUBSCR_DICT, &&TARGET_BINARY_OP_SUBTRACT_FLOAT,
&&TARGET_STORE_SUBSCR_LIST_INT, &&TARGET_BINARY_OP_ADD_UNICODE,
&&TARGET_GET_LEN, &&TARGET_GET_LEN,
&&TARGET_MATCH_MAPPING, &&TARGET_MATCH_MAPPING,
&&TARGET_MATCH_SEQUENCE, &&TARGET_MATCH_SEQUENCE,
&&TARGET_MATCH_KEYS, &&TARGET_MATCH_KEYS,
&&TARGET_SEND_GEN, &&TARGET_BINARY_OP_INPLACE_ADD_UNICODE,
&&TARGET_PUSH_EXC_INFO, &&TARGET_PUSH_EXC_INFO,
&&TARGET_CHECK_EXC_MATCH, &&TARGET_CHECK_EXC_MATCH,
&&TARGET_CHECK_EG_MATCH, &&TARGET_CHECK_EG_MATCH,
&&TARGET_UNPACK_SEQUENCE_TWO_TUPLE, &&TARGET_BINARY_SUBSCR_DICT,
&&TARGET_UNPACK_SEQUENCE_TUPLE, &&TARGET_BINARY_SUBSCR_GETITEM,
&&TARGET_FORMAT_SIMPLE, &&TARGET_FORMAT_SIMPLE,
&&TARGET_FORMAT_WITH_SPEC, &&TARGET_FORMAT_WITH_SPEC,
&&TARGET_UNPACK_SEQUENCE_LIST, &&TARGET_BINARY_SUBSCR_LIST_INT,
&&TARGET_STORE_ATTR_INSTANCE_VALUE, &&TARGET_BINARY_SUBSCR_TUPLE_INT,
&&TARGET_STORE_ATTR_SLOT, &&TARGET_STORE_SUBSCR_DICT,
&&TARGET_STORE_ATTR_WITH_HINT, &&TARGET_STORE_SUBSCR_LIST_INT,
&&TARGET_LOAD_GLOBAL_MODULE, &&TARGET_SEND_GEN,
&&TARGET_LOAD_GLOBAL_BUILTIN, &&TARGET_UNPACK_SEQUENCE_TWO_TUPLE,
&&TARGET_LOAD_SUPER_ATTR_ATTR, &&TARGET_UNPACK_SEQUENCE_TUPLE,
&&TARGET_WITH_EXCEPT_START, &&TARGET_WITH_EXCEPT_START,
&&TARGET_GET_AITER, &&TARGET_GET_AITER,
&&TARGET_GET_ANEXT, &&TARGET_GET_ANEXT,
@ -55,39 +55,39 @@ static void *opcode_targets[256] = {
&&TARGET_BEFORE_WITH, &&TARGET_BEFORE_WITH,
&&TARGET_END_ASYNC_FOR, &&TARGET_END_ASYNC_FOR,
&&TARGET_CLEANUP_THROW, &&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_SUPER_ATTR_METHOD,
&&TARGET_LOAD_ATTR_INSTANCE_VALUE, &&TARGET_LOAD_ATTR_INSTANCE_VALUE,
&&TARGET_LOAD_ATTR_MODULE, &&TARGET_LOAD_ATTR_MODULE,
&&TARGET_GET_ITER,
&&TARGET_GET_YIELD_FROM_ITER,
&&TARGET_LOAD_ATTR_WITH_HINT, &&TARGET_LOAD_ATTR_WITH_HINT,
&&TARGET_STORE_SUBSCR, &&TARGET_LOAD_BUILD_CLASS,
&&TARGET_DELETE_SUBSCR,
&&TARGET_LOAD_ATTR_SLOT, &&TARGET_LOAD_ATTR_SLOT,
&&TARGET_LOAD_ATTR_CLASS, &&TARGET_LOAD_ATTR_CLASS,
&&TARGET_LOAD_ASSERTION_ERROR,
&&TARGET_RETURN_GENERATOR,
&&TARGET_LOAD_ATTR_PROPERTY, &&TARGET_LOAD_ATTR_PROPERTY,
&&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, &&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN,
&&TARGET_LOAD_ATTR_METHOD_WITH_VALUES, &&TARGET_LOAD_ATTR_METHOD_WITH_VALUES,
&&TARGET_LOAD_ATTR_METHOD_NO_DICT, &&TARGET_LOAD_ATTR_METHOD_NO_DICT,
&&TARGET_GET_ITER,
&&TARGET_GET_YIELD_FROM_ITER,
&&TARGET_LOAD_ATTR_METHOD_LAZY_DICT, &&TARGET_LOAD_ATTR_METHOD_LAZY_DICT,
&&TARGET_LOAD_BUILD_CLASS,
&&TARGET_COMPARE_OP_FLOAT, &&TARGET_COMPARE_OP_FLOAT,
&&TARGET_COMPARE_OP_INT, &&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_RETURN_VALUE,
&&TARGET_CALL_PY_WITH_DEFAULTS, &&TARGET_COMPARE_OP_STR,
&&TARGET_SETUP_ANNOTATIONS, &&TARGET_SETUP_ANNOTATIONS,
&&TARGET_CALL_NO_KW_TYPE_1, &&TARGET_FOR_ITER_LIST,
&&TARGET_LOAD_LOCALS, &&TARGET_LOAD_LOCALS,
&&TARGET_CALL_NO_KW_STR_1, &&TARGET_FOR_ITER_TUPLE,
&&TARGET_POP_EXCEPT, &&TARGET_POP_EXCEPT,
&&TARGET_STORE_NAME, &&TARGET_STORE_NAME,
&&TARGET_DELETE_NAME, &&TARGET_DELETE_NAME,
@ -110,9 +110,9 @@ static void *opcode_targets[256] = {
&&TARGET_IMPORT_NAME, &&TARGET_IMPORT_NAME,
&&TARGET_IMPORT_FROM, &&TARGET_IMPORT_FROM,
&&TARGET_JUMP_FORWARD, &&TARGET_JUMP_FORWARD,
&&TARGET_CALL_NO_KW_TUPLE_1, &&TARGET_FOR_ITER_RANGE,
&&TARGET_CALL_BUILTIN_CLASS, &&TARGET_FOR_ITER_GEN,
&&TARGET_CALL_NO_KW_BUILTIN_O, &&TARGET_CALL_BOUND_METHOD_EXACT_ARGS,
&&TARGET_POP_JUMP_IF_FALSE, &&TARGET_POP_JUMP_IF_FALSE,
&&TARGET_POP_JUMP_IF_TRUE, &&TARGET_POP_JUMP_IF_TRUE,
&&TARGET_LOAD_GLOBAL, &&TARGET_LOAD_GLOBAL,
@ -131,11 +131,11 @@ static void *opcode_targets[256] = {
&&TARGET_POP_JUMP_IF_NONE, &&TARGET_POP_JUMP_IF_NONE,
&&TARGET_RAISE_VARARGS, &&TARGET_RAISE_VARARGS,
&&TARGET_GET_AWAITABLE, &&TARGET_GET_AWAITABLE,
&&TARGET_CALL_NO_KW_BUILTIN_FAST, &&TARGET_CALL_PY_EXACT_ARGS,
&&TARGET_BUILD_SLICE, &&TARGET_BUILD_SLICE,
&&TARGET_JUMP_BACKWARD_NO_INTERRUPT, &&TARGET_JUMP_BACKWARD_NO_INTERRUPT,
&&TARGET_MAKE_CELL, &&TARGET_MAKE_CELL,
&&TARGET_CALL_BUILTIN_FAST_WITH_KEYWORDS, &&TARGET_CALL_PY_WITH_DEFAULTS,
&&TARGET_LOAD_DEREF, &&TARGET_LOAD_DEREF,
&&TARGET_STORE_DEREF, &&TARGET_STORE_DEREF,
&&TARGET_DELETE_DEREF, &&TARGET_DELETE_DEREF,
@ -147,26 +147,26 @@ static void *opcode_targets[256] = {
&&TARGET_LIST_APPEND, &&TARGET_LIST_APPEND,
&&TARGET_SET_ADD, &&TARGET_SET_ADD,
&&TARGET_MAP_ADD, &&TARGET_MAP_ADD,
&&TARGET_CALL_NO_KW_LEN, &&TARGET_CALL_NO_KW_TYPE_1,
&&TARGET_COPY_FREE_VARS, &&TARGET_COPY_FREE_VARS,
&&TARGET_YIELD_VALUE, &&TARGET_YIELD_VALUE,
&&TARGET_RESUME, &&TARGET_RESUME,
&&TARGET_MATCH_CLASS, &&TARGET_MATCH_CLASS,
&&TARGET_CALL_NO_KW_ISINSTANCE, &&TARGET_CALL_NO_KW_STR_1,
&&TARGET_CALL_NO_KW_LIST_APPEND, &&TARGET_CALL_NO_KW_TUPLE_1,
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_O, &&TARGET_CALL_BUILTIN_CLASS,
&&TARGET_BUILD_CONST_KEY_MAP, &&TARGET_BUILD_CONST_KEY_MAP,
&&TARGET_BUILD_STRING, &&TARGET_BUILD_STRING,
&&TARGET_CONVERT_VALUE, &&TARGET_CONVERT_VALUE,
&&TARGET_CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS, &&TARGET_CALL_NO_KW_BUILTIN_O,
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS, &&TARGET_CALL_NO_KW_BUILTIN_FAST,
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_FAST, &&TARGET_CALL_BUILTIN_FAST_WITH_KEYWORDS,
&&TARGET_LIST_EXTEND, &&TARGET_LIST_EXTEND,
&&TARGET_SET_UPDATE, &&TARGET_SET_UPDATE,
&&TARGET_DICT_MERGE, &&TARGET_DICT_MERGE,
&&TARGET_DICT_UPDATE, &&TARGET_DICT_UPDATE,
&&TARGET_CALL_NO_KW_ALLOC_AND_ENTER_INIT, &&TARGET_CALL_NO_KW_LEN,
&&_unknown_opcode, &&TARGET_CALL_NO_KW_ISINSTANCE,
&&TARGET_LOAD_FAST_LOAD_FAST, &&TARGET_LOAD_FAST_LOAD_FAST,
&&TARGET_STORE_FAST_LOAD_FAST, &&TARGET_STORE_FAST_LOAD_FAST,
&&TARGET_STORE_FAST_STORE_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_GLOBALS,
&&TARGET_LOAD_FROM_DICT_OR_DEREF, &&TARGET_LOAD_FROM_DICT_OR_DEREF,
&&TARGET_SET_FUNCTION_ATTRIBUTE, &&TARGET_SET_FUNCTION_ATTRIBUTE,
&&_unknown_opcode, &&TARGET_CALL_NO_KW_LIST_APPEND,
&&_unknown_opcode, &&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_O,
&&_unknown_opcode, &&TARGET_CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS,
&&_unknown_opcode, &&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS,
&&_unknown_opcode, &&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_FAST,
&&_unknown_opcode, &&TARGET_CALL_NO_KW_ALLOC_AND_ENTER_INIT,
&&_unknown_opcode, &&_unknown_opcode,
&&_unknown_opcode, &&_unknown_opcode,
&&_unknown_opcode, &&_unknown_opcode,

View File

@ -107,6 +107,8 @@ _Py_GetSpecializationStats(void) {
err += add_stat_dict(stats, COMPARE_OP, "compare_op"); err += add_stat_dict(stats, COMPARE_OP, "compare_op");
err += add_stat_dict(stats, UNPACK_SEQUENCE, "unpack_sequence"); err += add_stat_dict(stats, UNPACK_SEQUENCE, "unpack_sequence");
err += add_stat_dict(stats, FOR_ITER, "for_iter"); 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) { if (err < 0) {
Py_DECREF(stats); Py_DECREF(stats);
return NULL; return NULL;
@ -127,9 +129,7 @@ print_spec_stats(FILE *out, OpcodeStats *stats)
/* Mark some opcodes as specializable for stats, /* Mark some opcodes as specializable for stats,
* even though we don't specialize them yet. */ * even though we don't specialize them yet. */
fprintf(out, "opcode[%d].specializable : 1\n", BINARY_SLICE); 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", STORE_SLICE);
fprintf(out, "opcode[%d].specializable : 1\n", SEND);
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
if (_PyOpcode_Caches[i]) { if (_PyOpcode_Caches[i]) {
fprintf(out, "opcode[%d].specializable : 1\n", 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_ITERATOR 9
#define SPEC_FAIL_UNPACK_SEQUENCE_SEQUENCE 10 #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 int function_kind(PyCodeObject *code);
static bool function_check_args(PyObject *o, int expected_argcount, int opcode); static bool function_check_args(PyObject *o, int expected_argcount, int opcode);
static uint32_t function_get_version(PyObject *o, 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(ENABLE_SPECIALIZATION);
assert(_PyOpcode_Caches[COMPARE_OP] == INLINE_CACHE_ENTRIES_COMPARE_OP); 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); _PyCompareOpCache *cache = (_PyCompareOpCache *)(instr + 1);
if (Py_TYPE(lhs) != Py_TYPE(rhs)) { if (Py_TYPE(lhs) != Py_TYPE(rhs)) {
SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, 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)) { if (PyUnicode_CheckExact(lhs)) {
int cmp = oparg >> 4; int cmp = oparg >> 5;
if (cmp != Py_EQ && cmp != Py_NE) { if (cmp != Py_EQ && cmp != Py_NE) {
SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_OP_STRING); SPECIALIZATION_FAIL(COMPARE_OP, SPEC_FAIL_COMPARE_OP_STRING);
goto failure; goto failure;
@ -2284,6 +2298,99 @@ success:
cache->counter = adaptive_counter_cooldown(); 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. /* Code init cleanup.
* CALL_NO_KW_ALLOC_AND_ENTER_INIT will set up * CALL_NO_KW_ALLOC_AND_ENTER_INIT will set up
* the frame to execute the EXIT_INIT_CHECK * the frame to execute the EXIT_INIT_CHECK