mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-106812: Small stack effect fixes (#107759)
- Generalize the syntax for the type of a stack effect to allow a trailing `*`, so we can declare something as e.g. `PyCodeObject *`. - When generating assignments for stack effects, the type of the value on the stack should be the default (i.e., `PyObject *`) even when the variable copied to/from it has a different type, so that an appropriate cast is generated However, not when the variable is an array -- then the type is taken from the variable (as it is always `PyObject **`).
This commit is contained in:
parent
707018cc75
commit
2df58dcd50
3 changed files with 15 additions and 20 deletions
|
@ -108,7 +108,7 @@ and a piece of C code describing its semantics::
|
||||||
NAME [":" type] [ "if" "(" C-expression ")" ]
|
NAME [":" type] [ "if" "(" C-expression ")" ]
|
||||||
|
|
||||||
type:
|
type:
|
||||||
NAME
|
NAME ["*"]
|
||||||
|
|
||||||
stream:
|
stream:
|
||||||
NAME "/" size
|
NAME "/" size
|
||||||
|
|
|
@ -252,12 +252,14 @@ class Parser(PLexer):
|
||||||
|
|
||||||
@contextual
|
@contextual
|
||||||
def stack_effect(self) -> StackEffect | None:
|
def stack_effect(self) -> StackEffect | None:
|
||||||
# IDENTIFIER [':' IDENTIFIER] ['if' '(' expression ')']
|
# IDENTIFIER [':' IDENTIFIER [TIMES]] ['if' '(' expression ')']
|
||||||
# | IDENTIFIER '[' expression ']'
|
# | IDENTIFIER '[' expression ']'
|
||||||
if tkn := self.expect(lx.IDENTIFIER):
|
if tkn := self.expect(lx.IDENTIFIER):
|
||||||
type_text = ""
|
type_text = ""
|
||||||
if self.expect(lx.COLON):
|
if self.expect(lx.COLON):
|
||||||
type_text = self.require(lx.IDENTIFIER).text.strip()
|
type_text = self.require(lx.IDENTIFIER).text.strip()
|
||||||
|
if self.expect(lx.TIMES):
|
||||||
|
type_text += " *"
|
||||||
cond_text = ""
|
cond_text = ""
|
||||||
if self.expect(lx.IF):
|
if self.expect(lx.IF):
|
||||||
self.require(lx.LPAREN)
|
self.require(lx.LPAREN)
|
||||||
|
|
|
@ -120,6 +120,14 @@ class StackItem:
|
||||||
), f"Push or pop above current stack level: {res}"
|
), f"Push or pop above current stack level: {res}"
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def as_stack_effect(self, lax: bool = False) -> StackEffect:
|
||||||
|
return StackEffect(
|
||||||
|
self.as_variable(lax=lax),
|
||||||
|
self.effect.type if self.effect.size else "",
|
||||||
|
self.effect.cond,
|
||||||
|
self.effect.size,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class CopyEffect:
|
class CopyEffect:
|
||||||
|
@ -356,24 +364,14 @@ def write_components(
|
||||||
for peek in mgr.peeks:
|
for peek in mgr.peeks:
|
||||||
out.assign(
|
out.assign(
|
||||||
peek.effect,
|
peek.effect,
|
||||||
StackEffect(
|
peek.as_stack_effect(),
|
||||||
peek.as_variable(),
|
|
||||||
peek.effect.type,
|
|
||||||
peek.effect.cond,
|
|
||||||
peek.effect.size,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
# Initialize array outputs
|
# Initialize array outputs
|
||||||
for poke in mgr.pokes:
|
for poke in mgr.pokes:
|
||||||
if poke.effect.size and poke.effect.name not in mgr.instr.unmoved_names:
|
if poke.effect.size and poke.effect.name not in mgr.instr.unmoved_names:
|
||||||
out.assign(
|
out.assign(
|
||||||
poke.effect,
|
poke.effect,
|
||||||
StackEffect(
|
poke.as_stack_effect(lax=True),
|
||||||
poke.as_variable(lax=True),
|
|
||||||
poke.effect.type,
|
|
||||||
poke.effect.cond,
|
|
||||||
poke.effect.size,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(parts) == 1:
|
if len(parts) == 1:
|
||||||
|
@ -390,11 +388,6 @@ def write_components(
|
||||||
for poke in mgr.pokes:
|
for poke in mgr.pokes:
|
||||||
if not poke.effect.size and poke.effect.name not in mgr.instr.unmoved_names:
|
if not poke.effect.size and poke.effect.name not in mgr.instr.unmoved_names:
|
||||||
out.assign(
|
out.assign(
|
||||||
StackEffect(
|
poke.as_stack_effect(),
|
||||||
poke.as_variable(),
|
|
||||||
poke.effect.type,
|
|
||||||
poke.effect.cond,
|
|
||||||
poke.effect.size,
|
|
||||||
),
|
|
||||||
poke.effect,
|
poke.effect,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue