Skip to content

inst defintion and its optional stack_effect #133412

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

Closed
nybblista opened this issue May 5, 2025 · 6 comments
Closed

inst defintion and its optional stack_effect #133412

nybblista opened this issue May 5, 2025 · 6 comments
Labels
docs Documentation in the Doc dir easy

Comments

@nybblista
Copy link
Contributor

nybblista commented May 5, 2025

The DSL specification syntax definition of the inst shows that the stack_effect is optional,
including its parentheses:

definition:
    "inst" "(" NAME ["," stack_effect] ")" "{" C-code "}"
...
stack_effect:
    "(" [inputs] "--" [outputs] ")"

But if we defined a new instruction, let's say, MY_INSTRUCTION as:

inst(MY_INSTRUCTION) {
  /* implementation */
}

We will get the following error:

SyntaxError: Extra stuff at the end of ./Python/bytecodes.c

However, if we defined MY_INSTRUCTION with an empty stack-effect, it will work!

inst(MY_INSTRUCTION, ( -- )) {
  /* implementation */
}

Linked PRs

@nybblista
Copy link
Contributor Author

@brandtbucher

@picnixz picnixz added the docs Documentation in the Doc dir label May 5, 2025
@picnixz
Copy link
Member

picnixz commented May 5, 2025

I think it's easier to change the docs than to change the parser. And it's better, IMO, to keep an explicit stack effect as it helps readability and avoids having special cases during parsing.

@nybblista
Copy link
Contributor Author

@picnixz I agree. Clearly defining the stack effect and enforcing it at the parser level will help ensure consistency in the inst definition across all instructions.

@nybblista
Copy link
Contributor Author

The inst_header method of the parser shows that it is not dealing with the optional path. However, it will be enough to update the doc.

@contextual
def inst_header(self) -> InstHeader | None:
    # annotation* inst(NAME, (inputs -- outputs))
    # | annotation* op(NAME, (inputs -- outputs))
    annotations = []
    while anno := self.expect(lx.ANNOTATION):
        if anno.text == "replicate":
            self.require(lx.LPAREN)
            times = self.require(lx.NUMBER)
            self.require(lx.RPAREN)
            annotations.append(f"replicate({times.text})")
        else:
            annotations.append(anno.text)
    tkn = self.expect(lx.INST)
    if not tkn:
        tkn = self.expect(lx.OP)
    if tkn:
        kind = cast(Literal["inst", "op"], tkn.text)
        if self.expect(lx.LPAREN) and (tkn := self.expect(lx.IDENTIFIER)):
            name = tkn.text
            if self.expect(lx.COMMA):
                inp, outp = self.io_effect()
                if self.expect(lx.RPAREN):
                    if (tkn := self.peek()) and tkn.kind == lx.LBRACE:
                        return InstHeader(annotations, kind, name, inp, outp)
    return None

@picnixz @tomasr8

@tomasr8
Copy link
Member

tomasr8 commented May 6, 2025

I think it should be enough to update the internal docs. I don't think there's any urgent need to support the optional syntax (imo, it's better to be explicit)

@picnixz
Copy link
Member

picnixz commented May 8, 2025

I haven't merged your PR yet :). I'm only merging PRs when I'm on my Linux session and before 8PM (Paris time)

picnixz pushed a commit that referenced this issue May 9, 2025
The `stack_effect` is incorrectly documented as being allowed to be optional.
@picnixz picnixz closed this as completed May 9, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 9, 2025
The `stack_effect` is incorrectly documented as being allowed to be optional.
(cherry picked from commit f77dac6)

Co-authored-by: Nybblista <170842536+nybblista@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 9, 2025
The `stack_effect` is incorrectly documented as being allowed to be optional.
(cherry picked from commit f77dac6)

Co-authored-by: Nybblista <170842536+nybblista@users.noreply.github.com>
picnixz pushed a commit that referenced this issue May 9, 2025
…133729)

gh-133412: amend docs for the `inst` definition (GH-133708)

The `stack_effect` is incorrectly documented as being allowed to be optional.
(cherry picked from commit f77dac6)

Co-authored-by: Nybblista <170842536+nybblista@users.noreply.github.com>
picnixz pushed a commit that referenced this issue May 9, 2025
…133730)

gh-133412: amend docs for the `inst` definition (GH-133708)

The `stack_effect` is incorrectly documented as being allowed to be optional.
(cherry picked from commit f77dac6)

Co-authored-by: Nybblista <170842536+nybblista@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir easy
Projects
Status: Todo
Development

No branches or pull requests

3 participants