-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-96461: clarify the meaning of the oparg for CACHE and COPY opcode #96462
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
Conversation
Doc/library/dis.rst
Outdated
@@ -398,13 +401,17 @@ The Python compiler currently generates the following bytecode instructions. | |||
Push the *i*-th item to the top of the stack. The item is not removed from its | |||
original location. | |||
|
|||
The stack is indexed from 1, so COPY 1 will copy TOS. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe instead of adding this, we could define TOS(i)
at the top along with TOS1, TOS2 etc, and then reword the definition here to use those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried something along those lines in my last commit let me know if you like it better this way.
Doc/library/dis.rst
Outdated
@@ -382,6 +382,10 @@ The Python compiler currently generates the following bytecode instructions. | |||
|
|||
**General instructions** | |||
|
|||
In the following, TOS(i) refer to the *i*-th item on the stack which is index from 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the following, TOS(i) refer to the *i*-th item on the stack which is index from 1. | |
In the following, TOS(i) refers to the *i*-th item on the stack which is indexed from 1. |
Doc/library/dis.rst
Outdated
@@ -382,6 +382,10 @@ The Python compiler currently generates the following bytecode instructions. | |||
|
|||
**General instructions** | |||
|
|||
In the following, TOS(i) refer to the *i*-th item on the stack which is index from 1. | |||
We also use as shorthand TOS = TOS(1) which is the top-of-stack. | |||
TOS1, TOS2, TOS3 are the second, third and fourth items on the stack, respectively. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit unfortunate that TOS1 = TOS(2), TOS2 = TOS(3), etc.
@markshannon - would you like to change this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was another reason for my previous formulation that avoided ro shed too much light on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally we count from one for stack items, so I would like to change it. Ideally drop TOS1
etc, all together.
The current explanation is not explicit about what is popped or not, so the shorthand is not helpful.
Doc/library/dis.rst
Outdated
@@ -395,15 +399,15 @@ The Python compiler currently generates the following bytecode instructions. | |||
|
|||
.. opcode:: COPY (i) | |||
|
|||
Push the *i*-th item to the top of the stack. The item is not removed from its | |||
Push TOS(i) to the top of the stack. The item is not removed from its |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "Push a copy of i
th item (counting from 1) to the top of the stack ..."
Doc/library/dis.rst
Outdated
original location. | ||
|
||
.. versionadded:: 3.11 | ||
|
||
|
||
.. opcode:: SWAP (i) | ||
|
||
Swap TOS with the item at position *i*. | ||
Swap TOS with TOS(i). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "Swap the first and i
th item on the stack (counting from 1).
I can rephrase everything to not use TOS(i) and repeat that the stack is indexed from 1 in each relevant place however they are quite a few occurrences (many more than what this PR currently does). @iritkatriel do you agree with @markshannon on this ? |
I agree that we don't need to have both TOS(i) and TOSi, I think removing the TOSis is the smaller change, amirite? @markshannon - were you suggesting to add "(counting from 1)" on each place where TOS(i) is used? Or just once where it's defined or used for the first time? |
Removing the TOSi should be a smaller change yes and would not entail any large duplication. |
I went ahead and replaced TOS by TOS(1) I am not sure this a clear win for the reader TBH. Let me know what you think. |
I don't know if this has already been considered, but one approach might be to eliminate "
What do you think? |
That may lead to clearer description indeed. I must say I first tried to make the changes minimal but I can undertake a larger changes if it is generallyvthough to be useful. |
I went ahead with @sweeneyde suggestion and also fixed a bunch of small issues in separate commits (let me know if they should be split into their own PR). I will wait to get some feedback before dealing with the merge conflict. |
I think @sweeneyde 's suggestion is good. |
Thanks @iritkatriel. I will incorporate your changes and work on fixing the conflicts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm honestly not sure why you hate the term "TOS" so much. It's short, and doesn't go into the (irrelevant) issue whether the stack grows up or down.
Doc/library/dis.rst
Outdated
Push the i-th item to the top of the stack without removing it from its original | ||
location.:: | ||
|
||
STACK.append(STACK[-i]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add that i
should not be zero.
I have no issue with the term TOS. I am simply looking for the best way to document opcode operating on the i-th element of the stack. Ideally I would like whatever is picked to be easy to remember and ideally consistent across the documentation. I like |
8209c15
to
84176ea
Compare
I rebased this and forced pushed. There are places where using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like describing the stack as a Python list, it is unambiguous and clear to any Python developer.
However, you need to be careful to accounts for pops, pushes and sequencing.
E.g. LIST_APPEND
is
list.append(STACK[-i-1], STACK.pop())
not
list.append(STACK[-i], STACK[-1])
Doc/library/dis.rst
Outdated
|
||
|
||
.. opcode:: BINARY_OP (op) | ||
|
||
Implements the binary and in-place operators (depending on the value of | ||
*op*). | ||
``TOS = TOS1 op TOS``. | ||
``STACK[-1] = op STACK[-1]``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are missing the left operand.
STACK[-1] = STACK[-2] op STACK.pop()
or
rhs = STACK.pop()
lhs = STACK.pop()
STACK.push(lhs op rhs)
Doc/library/dis.rst
Outdated
|
||
.. versionadded:: 3.11 | ||
|
||
|
||
.. opcode:: BINARY_SUBSCR | ||
|
||
Implements ``TOS = TOS1[TOS]``. | ||
Implements ``STACK[-1] = STACK[-2][STACK[-1]]``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is incorrect, although no more than the old version.
See my comments on BINARY_OP
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
aaa0ba3
to
b84a775
Compare
I rebased to address the conflicts and updated the description of CALL_INSTRINSIC_1 ping @markshannon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent, thanks for this.
I've one comment on BUILD_TUPLE
And there is still @gvanrossum's comment on COPY
to address.
Doc/library/dis.rst
Outdated
resulting tuple onto the stack. | ||
resulting tuple onto the stack.:: | ||
|
||
STACK.append(tuple(STACK[-count:])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite correct. The colon should be before the -count
and the values need to be popped from the stack.
Also state that count
should not be zero (it can be, but the compiler emits LOAD_CONST
in that case, so we might change that it the future.
assert(count > 0)
STACK, values = STACK[-count:], STACK[:-count]
STACK.append(tuple(values))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am confused. BUILD_TUPLE
consume the items on top of the stack so if count is 2 that would be STACK[-2],STACK[-1] == STACK[-2:]
so shouldn't we write:
assert count > 0
STACK, values = STACK[:-count], STACK[-count:]
STACK.append(tuple(values))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I updated I have made the requested changes; please review again |
Thanks for making the requested changes! @markshannon: please review the changes made to this pull request. |
Thanks |
This could be backported to Python 3.11