Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-99005: Add CALL_INTRINSIC_1 instruction #100771

Merged
merged 14 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -607,15 +607,6 @@ the original TOS1.
.. versionadded:: 3.12


.. opcode:: STOPITERATION_ERROR

Handles a StopIteration raised in a generator or coroutine.
If TOS is an instance of :exc:`StopIteration`, or :exc:`StopAsyncIteration`
replace it with a :exc:`RuntimeError`.

.. versionadded:: 3.12


.. opcode:: BEFORE_ASYNC_WITH

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

**Miscellaneous opcodes**

.. opcode:: PRINT_EXPR

Implements the expression statement for the interactive mode. TOS is removed
from the stack and printed. In non-interactive mode, an expression statement
is terminated with :opcode:`POP_TOP`.


.. opcode:: SET_ADD (i)

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


.. opcode:: IMPORT_STAR

Loads all symbols not starting with ``'_'`` directly from the module TOS to
the local namespace. The module is popped after loading all names. This
opcode implements ``from module import *``.


.. opcode:: POP_EXCEPT

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


.. opcode:: CALL_INTRINSIC_1

Calls an intrinsic function with one argument. Passes the TOS as the argument
and sets TOS to the result. Used to implement functionality that is necessary
but not performance critical.
markshannon marked this conversation as resolved.
Show resolved Hide resolved

The operand determines which intrinsic function is called:

* ``0`` Not valid
* ``1`` Prints the argument to standard out. Used in the REPL.
* ``2`` Performs ``import *`` for the named module.
* ``3`` Extracts the return value from a ``StopIteration`` exception.

.. versionadded:: 3.12


**Pseudo-instructions**

These opcodes do not appear in python bytecode, they are used by the compiler
Expand Down
10 changes: 10 additions & 0 deletions Include/internal/pycore_intrinsics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#define INTRINSIC_PRINT 1
#define INTRINSIC_IMPORT_STAR 2
#define INTRINSIC_STOPITERATION_ERROR 3

#define MAX_INTRINSIC_1 3

typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value);

extern instrinsic_func1 _PyIntrinsics_UnaryFunctions[];
50 changes: 25 additions & 25 deletions Include/internal/pycore_opcode.h

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

58 changes: 28 additions & 30 deletions Include/opcode.h

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

3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.12a1 3510 (FOR_ITER leaves iterator on the stack)
# Python 3.12a1 3511 (Add STOPITERATION_ERROR instruction)
# Python 3.12a1 3512 (Remove all unused consts from code objects)
# Python 3.12a1 3513 (Add CALL_INTRINSIC_1 instruction, removed STOPITERATION_ERROR, PRINT_EXPR, IMPORT_STAR)

# Python 3.13 will start with 3550

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

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

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

Expand Down
8 changes: 3 additions & 5 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,17 @@ def pseudo_op(name, op, real_ops):
def_op('STORE_SUBSCR', 60)
def_op('DELETE_SUBSCR', 61)

def_op('STOPITERATION_ERROR', 63)

def_op('GET_ITER', 68)
def_op('GET_YIELD_FROM_ITER', 69)
def_op('PRINT_EXPR', 70)

def_op('LOAD_BUILD_CLASS', 71)

def_op('LOAD_ASSERTION_ERROR', 74)
def_op('RETURN_GENERATOR', 75)

def_op('LIST_TO_TUPLE', 82)
def_op('RETURN_VALUE', 83)
def_op('IMPORT_STAR', 84)

def_op('SETUP_ANNOTATIONS', 85)

def_op('ASYNC_GEN_WRAP', 87)
Expand Down Expand Up @@ -220,7 +218,7 @@ def pseudo_op(name, op, real_ops):
def_op('CALL', 171)
def_op('KW_NAMES', 172)
hasconst.append(172)

def_op('CALL_INTRINSIC_1', 173)

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

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ async def _asyncwith(c):
>> COPY 3
POP_EXCEPT
RERAISE 1
>> STOPITERATION_ERROR
>> CALL_INTRINSIC_1 3
RERAISE 1
ExceptionTable:
12 rows
Expand Down
2 changes: 2 additions & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ PYTHON_OBJS= \
Python/import.o \
Python/importdl.o \
Python/initconfig.o \
Python/intrinsics.o \
Python/marshal.o \
Python/modsupport.o \
Python/mysnprintf.o \
Expand Down Expand Up @@ -1650,6 +1651,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_initconfig.h \
$(srcdir)/Include/internal/pycore_interp.h \
$(srcdir)/Include/internal/pycore_interpreteridobject.h \
$(srcdir)/Include/internal/pycore_intrinsics.h \
$(srcdir)/Include/internal/pycore_list.h \
$(srcdir)/Include/internal/pycore_long.h \
$(srcdir)/Include/internal/pycore_moduleobject.h \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Add new :opcode:`CALL_INSTRINSIC_1` instruction. Remove
:opcode:`IMPORT_STAR`, :opcode:`PRINT_EXPR` and
:opcode:`STOPITERATION_ERROR`, replacing them with the
:opcode:`CALL_INSTRINSIC_1` instruction.
1 change: 1 addition & 0 deletions PCbuild/_freeze_module.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
<ClCompile Include="..\Python\import.c" />
<ClCompile Include="..\Python\importdl.c" />
<ClCompile Include="..\Python\initconfig.c" />
<ClCompile Include="..\Python\intrinsics.c" />
<ClCompile Include="..\Python\marshal.c" />
<ClCompile Include="..\Python\modsupport.c" />
<ClCompile Include="..\Python\mysnprintf.c" />
Expand Down
Loading