◐ Shell
reader mode source ↗
Skip to content

gh-98831: Support conditional effects; use for LOAD_ATTR#101333

Merged
gvanrossum merged 15 commits into
python:mainfrom
gvanrossum:cond-cases
Jan 30, 2023
Merged

gh-98831: Support conditional effects; use for LOAD_ATTR#101333
gvanrossum merged 15 commits into
python:mainfrom
gvanrossum:cond-cases

Conversation

@gvanrossum

@gvanrossum gvanrossum commented Jan 26, 2023

Copy link
Copy Markdown
Member

We can now write

inst(OP, (foo if (oparg & 1) -- bar if (oparg & 2)) {
    ...
}

which pops foo off the stack if oparg & 1, otherwise setting it to NULL, and pushes bar onto the stack if oparg & 2 (otherwise ignoring it).

This syntax cannot be combined with an array size on the same effect, but it can be combined with a type (untested). In addition, it is incompatible with an array size closer to the stack top, e.g. foo if (oparg&1), bar[oparg>>1] is not supported (I don't think it's used, and generating code for it would be slightly awkward, but could be done if needed).

The implementation switches to using POP() and PUSH() (from PEEK() and POKE()) for those variables that are either conditional or whose position relative to the bottom (!) of the stack depends on a condition. (See the added test for details.)

To demonstrate this, I converted LOAD_ATTR (but not its specializations).

- UndefinedLocalError when generating metadata for an 'op'
- Accidental newline inserted in test_generator.py

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hide comment

Notes to self.

@iritkatriel

Copy link
Copy Markdown
Member

I don't understand what this warning is about:

comparison of constant ‘0’ with boolean expression is always true [-Wbool-compare]

@gvanrossum

gvanrossum commented Jan 28, 2023

Copy link
Copy Markdown
Member Author

I don't understand what this warning is about:

comparison of constant ‘0’ with boolean expression is always true [-Wbool-compare]

I assume it's about this line (L1797):

STACK_GROW(((oparg & 1) != 0));

The compiler probably reasons that x >= 0 where x has type bool is always true and is trying to warn you that you've got a trivial condition (the expansion of STACK_GROW(n) uses assert(n >= 0) and presumably assert(x) expands to something like if (!x) ...).

That's silly since it doesn't warn us about the equally trivial 1 >= 0 that appears in the expansion of STACK_GROW(1) -- I'm guessing it's more likely to warn based on types rather than values.

I'm not sure what to do about it.

PS. I'm still looking into your suggestion to assign the condition to a flag variable, it's a bit messy.

@gvanrossum

Copy link
Copy Markdown
Member Author

Here's the new version. Let me know if you like it enough to go ahead -- if not, I can easily roll it back. Note that I also fixed a bunch of typing issues (which caught one genuine bug).

@iritkatriel

Copy link
Copy Markdown
Member

Here's the new version. Let me know if you like it enough to go ahead -- if not, I can easily roll it back. Note that I also fixed a bunch of typing issues (which caught one genuine bug).

I think this is easier to read. The warning about the bool comparison is gone too. There's just the one about res being potentially unassigned.

This hopefully avoids a silly compiler warning.
@gvanrossum

Copy link
Copy Markdown
Member Author

Finally this has no compiler warnings on GitHub. I prefer this version, the changes to generate_cases.py are simpler than the version with flags.

@gvanrossum gvanrossum merged commit f5a3d91 into python:main Jan 30, 2023
@gvanrossum gvanrossum deleted the cond-cases branch January 30, 2023 01:28
mdboom pushed a commit to mdboom/cpython that referenced this pull request Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants