8000 gh-135447: Document new bytecodes in 3.14 by Yzi-Li · Pull Request #135803 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-135447: Document new bytecodes in 3.14 #135803
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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

Yzi-Li
Copy link
Contributor
@Yzi-Li Yzi-Li commented Jun 22, 2025

Document:

  • BUILD_TEMPLATE
  • BUILD_INTERPOLATION
  • NOT_TAKEN

📚 Documentation preview 📚: https://cpython-previews--135803.org.readthedocs.build/

@Yzi-Li Yzi-Li marked this pull request as ready for review June 22, 2025 03:42
@Yzi-Li
Copy link
Contributor Author
Yzi-Li commented Jun 22, 2025

@Fidget-Spinner I did a PR to fix the issue. I'm not sure if my expression is correct, so could you take a look? Thanks!

@Fidget-Spinner
Copy link
Member
Fidget-Spinner commented Jun 22, 2025

I think there's some misconception happening here, but no worries, I'll explain how to interpret the bytecode.

To document a new bytecode, you need to go to this file https://github.com/python/cpython/blob/main/Python/bytecodes.c . Then, find the bytecode definition (for example, inst(BUILD_INTERPOLATION). Everything after the comma and to the left of the -- are the stack inputs, everything to the right are stack outputs. So for example, inst(BUILD_INTERPOLATION, (value, str, format[oparg & 1] -- interpolation)) should be documented as:

``BUILD_INTERPOLATION`` expects as stack inputs:
* ``STACK[oparg & 1]`` should be ``format``
* ``STACK[-1 - (oparg & 1)]`` should be ``str``
* ``STACK[-2 - (oparg & 1)]`` should be ``value``

``BUILD_INTERPOLATION`` outputs the interpolation object to TOS after consuming its inputs

Based on this, are you able to infer the behavior of BUILD_TEMPLATE :) ?

@Yzi-Li
Copy link
Contributor Author
Yzi-Li commented Jun 23, 2025

Based on this, are you able to infer the behavior of BUILD_TEMPLATE :) ?

I have seen what you wrote. And I think I am able to express it.

But I've been a little busy recently so I might need a couple of days — sorry for the delay.


.. versionadded:: 3.14


.. opcode:: BUILD_TEMPLATE

Create a Template object consuming interpolations and a string from the stack,
and pushes it onto the stack.
It consumes ``STACK[-1]``(string) and ``STACK[-2]``(interpolations) and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is pretty close. One small error: it's the other way around, interpolations is STACK[-1], string is STACK[-2]. This is because the bytecode definition file models a stack (LIFO), which means the rightmost element is the top of the stack.

Co-authored-by: Ken Jin <kenjin4096@gmail.com>
Comment on lines +589 to +590
Do nothing code. Used as a hint to the interpreter that a branch was predicted
as not taken.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot what NOT_TAKEN does, but the rest look good. Maybe @iritkatriel knows?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It indeed does nothing. Not sure it's related to prediction though. It think it's to give tracing applications like code coverage a way to distinguish between the taken/not taken branches.

CC @markshannon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pure inst(NOP, (--)) {
}

family(RESUME, 0) = {
    RESUME_CHECK,
};

macro(NOT_TAKEN) = NOP;

Can we consider its behavior to be the same as NOP?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes its a NOP but I forgot if its a hint or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

3 participants
0