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-96461: clarify the meaning of the oparg for CACHE and COPY opcode #96462

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

Conversation

MatthieuDartiailh
Copy link
Contributor

@MatthieuDartiailh MatthieuDartiailh commented Aug 31, 2022

@bedevere-bot bedevere-bot added docs Documentation in the Doc dir skip news awaiting review labels Aug 31, 2022
@kumaraditya303 kumaraditya303 requested a review from iritkatriel Aug 31, 2022
@iritkatriel iritkatriel requested a review from markshannon Sep 1, 2022
@@ -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.
Copy link
Member

@iritkatriel iritkatriel Sep 1, 2022

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.

Copy link
Contributor Author

@MatthieuDartiailh MatthieuDartiailh Sep 1, 2022

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.

@@ -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.
Copy link
Member

@iritkatriel iritkatriel Sep 1, 2022

Choose a reason for hiding this comment

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

Suggested change
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.

@@ -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.
Copy link
Member

@iritkatriel iritkatriel Sep 1, 2022

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?

Copy link
Contributor Author

@MatthieuDartiailh MatthieuDartiailh Sep 1, 2022

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.

Copy link
Member

@markshannon markshannon Oct 3, 2022

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.

@@ -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
Copy link
Member

@markshannon markshannon Oct 3, 2022

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 ith item (counting from 1) to the top of the stack ..."

original location.

.. versionadded:: 3.11


.. opcode:: SWAP (i)

Swap TOS with the item at position *i*.
Swap TOS with TOS(i).
Copy link
Member

@markshannon markshannon Oct 3, 2022

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 ith item on the stack (counting from 1).

@MatthieuDartiailh
Copy link
Contributor Author

MatthieuDartiailh commented Oct 5, 2022

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 ?

@iritkatriel
Copy link
Member

iritkatriel commented Oct 5, 2022

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?

@MatthieuDartiailh
Copy link
Contributor Author

MatthieuDartiailh commented Oct 5, 2022

Removing the TOSi should be a smaller change yes and would not entail any large duplication.

@MatthieuDartiailh
Copy link
Contributor Author

MatthieuDartiailh commented Oct 6, 2022

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.

@sweeneyde
Copy link
Member

sweeneyde commented Oct 7, 2022

I don't know if this has already been considered, but one approach might be to eliminate "TOS" terminology altogether and instead refer to stack[-1], stack[-2], stack.pop(), etc., borrowing the well-understood semantics of lists.

  • POP_TOP becomes "Remove stack[-1], i.e., do stack.pop()"
  • COPY becomes stack.append(stack[-i]).
  • SWAP becomes stack[-1], stack[-i] = stack[-i], stack[-1]
  • UNARY_POSITIVE becomes stack[-1] = +stack[-1]
  • BINARY_SUBSCR becomes right = stack.pop(); left = stack.pop(); stack.append(left[right]), or similar.

What do you think?

@MatthieuDartiailh
Copy link
Contributor Author

MatthieuDartiailh commented Oct 7, 2022

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.

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
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants