Skip to content

Commit 2818714

Browse files
authored
GH-99005: Add CALL_INTRINSIC_1 instruction (GH-100771)
* Remove PRINT_EXPR instruction * Remove STOPITERATION_ERROR instruction * Remove IMPORT_STAR instruction
1 parent f20c553 commit 2818714

19 files changed

+336
-361
lines changed

Doc/library/dis.rst

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -607,15 +607,6 @@ the original TOS1.
607607
.. versionadded:: 3.12
608608

609609

610-
.. opcode:: STOPITERATION_ERROR
611-
612-
Handles a StopIteration raised in a generator or coroutine.
613-
If TOS is an instance of :exc:`StopIteration`, or :exc:`StopAsyncIteration`
614-
replace it with a :exc:`RuntimeError`.
615-
616-
.. versionadded:: 3.12
617-
618-
619610
.. opcode:: BEFORE_ASYNC_WITH
620611

621612
Resolves ``__aenter__`` and ``__aexit__`` from the object on top of the
@@ -627,13 +618,6 @@ the original TOS1.
627618

628619
**Miscellaneous opcodes**
629620

630-
.. opcode:: PRINT_EXPR
631-
632-
Implements the expression statement for the interactive mode. TOS is removed
633-
from the stack and printed. In non-interactive mode, an expression statement
634-
is terminated with :opcode:`POP_TOP`.
635-
636-
637621
.. opcode:: SET_ADD (i)
638622

639623
Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
@@ -682,13 +666,6 @@ iterations of the loop.
682666
.. versionadded:: 3.6
683667

684668

685-
.. opcode:: IMPORT_STAR
686-
687-
Loads all symbols not starting with ``'_'`` directly from the module TOS to
688-
the local namespace. The module is popped after loading all names. This
689-
opcode implements ``from module import *``.
690-
691-
692669
.. opcode:: POP_EXCEPT
693670

694671
Pops a value from the stack, which is used to restore the exception state.
@@ -1422,6 +1399,22 @@ iterations of the loop.
14221399
they use their arg.
14231400

14241401

1402+
.. opcode:: CALL_INTRINSIC_1
1403+
1404+
Calls an intrinsic function with one argument. Passes the TOS as the argument
1405+
and sets TOS to the result. Used to implement functionality that is necessary
1406+
but not performance critical.
1407+
1408+
The operand determines which intrinsic function is called:
1409+
1410+
* ``0`` Not valid
1411+
* ``1`` Prints the argument to standard out. Used in the REPL.
1412+
* ``2`` Performs ``import *`` for the named module.
1413+
* ``3`` Extracts the return value from a ``StopIteration`` exception.
1414+
1415+
.. versionadded:: 3.12
1416+
1417+
14251418
**Pseudo-instructions**
14261419

14271420
These opcodes do not appear in python bytecode, they are used by the compiler

Include/internal/pycore_intrinsics.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
#define INTRINSIC_PRINT 1
3+
#define INTRINSIC_IMPORT_STAR 2
4+
#define INTRINSIC_STOPITERATION_ERROR 3
5+
6+
#define MAX_INTRINSIC_1 3
7+
8+
typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value);
9+
10+
extern instrinsic_func1 _PyIntrinsics_UnaryFunctions[];

Include/internal/pycore_opcode.h

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/opcode.h

Lines changed: 28 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/importlib/_bootstrap_external.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ def _write_atomic(path, data, mode=0o666):
426426
# Python 3.12a1 3510 (FOR_ITER leaves iterator on the stack)
427427
# Python 3.12a1 3511 (Add STOPITERATION_ERROR instruction)
428428
# Python 3.12a1 3512 (Remove all unused consts from code objects)
429+
# Python 3.12a1 3513 (Add CALL_INTRINSIC_1 instruction, removed STOPITERATION_ERROR, PRINT_EXPR, IMPORT_STAR)
429430

430431
# Python 3.13 will start with 3550
431432

@@ -438,7 +439,7 @@ def _write_atomic(path, data, mode=0o666):
438439
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
439440
# in PC/launcher.c must also be updated.
440441

441-
MAGIC_NUMBER = (3512).to_bytes(2, 'little') + b'\r\n'
442+
MAGIC_NUMBER = (3513).to_bytes(2, 'little') + b'\r\n'
442443

443444
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
444445

Lib/opcode.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,17 @@ def pseudo_op(name, op, real_ops):
112112
def_op('STORE_SUBSCR', 60)
113113
def_op('DELETE_SUBSCR', 61)
114114

115-
def_op('STOPITERATION_ERROR', 63)
116-
117115
def_op('GET_ITER', 68)
118116
def_op('GET_YIELD_FROM_ITER', 69)
119-
def_op('PRINT_EXPR', 70)
117+
120118
def_op('LOAD_BUILD_CLASS', 71)
121119

122120
def_op('LOAD_ASSERTION_ERROR', 74)
123121
def_op('RETURN_GENERATOR', 75)
124122

125123
def_op('LIST_TO_TUPLE', 82)
126124
def_op('RETURN_VALUE', 83)
127-
def_op('IMPORT_STAR', 84)
125+
128126
def_op('SETUP_ANNOTATIONS', 85)
129127

130128
def_op('ASYNC_GEN_WRAP', 87)
@@ -220,7 +218,7 @@ def pseudo_op(name, op, real_ops):
220218
def_op('CALL', 171)
221219
def_op('KW_NAMES', 172)
222220
hasconst.append(172)
223-
221+
def_op('CALL_INTRINSIC_1', 173)
224222

225223
hasarg.extend([op for op in opmap.values() if op >= HAVE_ARGUMENT])
226224

Lib/test/test_dis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ async def _asyncwith(c):
543543
>> COPY 3
544544
POP_EXCEPT
545545
RERAISE 1
546-
>> STOPITERATION_ERROR
546+
>> CALL_INTRINSIC_1 3
547547
RERAISE 1
548548
ExceptionTable:
549549
12 rows

Makefile.pre.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ PYTHON_OBJS= \
395395
Python/import.o \
396396
Python/importdl.o \
397397
Python/initconfig.o \
398+
Python/intrinsics.o \
398399
Python/marshal.o \
399400
Python/modsupport.o \
400401
Python/mysnprintf.o \
@@ -1650,6 +1651,7 @@ PYTHON_HEADERS= \
16501651
$(srcdir)/Include/internal/pycore_initconfig.h \
16511652
$(srcdir)/Include/internal/pycore_interp.h \
16521653
$(srcdir)/Include/internal/pycore_interpreteridobject.h \
1654+
$(srcdir)/Include/internal/pycore_intrinsics.h \
16531655
$(srcdir)/Include/internal/pycore_list.h \
16541656
$(srcdir)/Include/internal/pycore_long.h \
16551657
$(srcdir)/Include/internal/pycore_moduleobject.h \
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add new :opcode:`CALL_INSTRINSIC_1` instruction. Remove
2+
:opcode:`IMPORT_STAR`, :opcode:`PRINT_EXPR` and
3+
:opcode:`STOPITERATION_ERROR`, replacing them with the
4+
:opcode:`CALL_INSTRINSIC_1` instruction.

PCbuild/_freeze_module.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
<ClCompile Include="..\Python\import.c" />
207207
<ClCompile Include="..\Python\importdl.c" />
208208
<ClCompile Include="..\Python\initconfig.c" />
209+
<ClCompile Include="..\Python\intrinsics.c" />
209210
<ClCompile Include="..\Python\marshal.c" />
210211
<ClCompile Include="..\Python\modsupport.c" />
211212
<ClCompile Include="..\Python\mysnprintf.c" />

0 commit comments

Comments
 (0)