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

Change the interpreter definition DSL to be legal C syntax #477

Closed
markshannon opened this issue Oct 12, 2022 · 4 comments
Closed

Change the interpreter definition DSL to be legal C syntax #477

markshannon opened this issue Oct 12, 2022 · 4 comments
Labels
epic-generator DSL-based code generator for the interpreter(s)

Comments

@markshannon
Copy link
Member

markshannon commented Oct 12, 2022

The proposed DSL for defining the interpreter has sound semantics, but the syntax is problematic due to lack of tooling.
We want to be able to edit and view the code in standard tools.

The 3.11 (and earlier) instructions definitions are understood by tools that understand C, we want to retain that.

To do that we need to change the DSL to be a subset of C, as follows:

  • The definition file will start with macro and function declarations to enable C tooling to understand the following definition. Our tooling will ignore that section.
  • Definitions will change from kind NAME ... to kind "(" NAME ")" ...
  • The stack effect will be embedded in a C comment ( -- value) becomes /* -- value */
  • Sequences will be need to become C expressions, so CHECK_OBJECT_TYPE LOAD_SLOT becomes CHECK_OBJECT_TYPE + LOAD_SLOT
  • Stream values will be indicated by multiplication, #index becomes index*1 and ##version becomes version*2.

From the examples,

inst LOAD_ATTR_SLOT = #counter CHECK_OBJECT_TYPE LOAD_SLOT ####unused;

becomes

inst(LOAD_ATTR_SLOT) = counter*1 + CHECK_OBJECT_TYPE + LOAD_SLOT  + unused*4;

Alternatively we could define S1, S2 and S4 for values in the instruction stream:

inst(LOAD_ATTR_SLOT) = S1(counter) + CHECK_OBJECT_TYPE + LOAD_SLOT  + S4(unused);

The definition of LOAD_FAST changes from:

inst LOAD_FAST ( -- value ) {
        value = frame->f_localsplus[oparg];
        Py_INCREF(value);
    }

to:

inst(LOAD_FAST)
/* -- value */
{
    value = frame->f_localsplus[oparg];
    Py_INCREF(value);
}
@gvanrossum
Copy link
Collaborator

Looks good, although I'd prefer to come up with a solution where the stack effect (both what it takes from the stack and what it leaves behind) are arguments to the macro. Maybe this would work?

inst(LOAD_FAST, (), (value)) {
    value = frame->f_localsplus[oparg];
    Py_INCREF(value);
}

When preceded by e.g.

#define inst(x, y, z) // nothing

VS Code has no problem with that.

@markshannon
Copy link
Member Author

Maybe use a two argument macro, that way we can keep the stack comment format

#define inst(x, y) void inst_ ## x(void)

inst (LOAD_FAST, -- value)
{
    value = frame->localsplus[oparg];
    Py_INCREF(value);
}

@gvanrossum
Copy link
Collaborator

Sure, that'll work, assuming the DSL lives in a separate file. I'll try to update the definition file.

@gvanrossum
Copy link
Collaborator

For streams, I propose NAME/1, NAME/2, and NAME/4. Valid C and feels vaguely more intuitive than *4.

gvanrossum added a commit that referenced this issue Oct 12, 2022
gvanrossum added a commit that referenced this issue Oct 31, 2022
switch from
```
inst NAME (inputs -- outputs) {
    C-code
}
```
to
```
inst(NAME, (inputs -- outputs)) {
    C-code
}
```
Also from
```
inst NAME = op1 op2;
```
to
```
inst(NAME) = op1 + op2;
```
Change stream syntax from
```
#NAME
##NAME
####NAME
```
to
```
NAME/1
NAME/2
NAME/4
```
Finally change
```
family NAME = OP1, OP2, ..., OPN;
```
to
```
family(NAME) = OP1, OP2, ..., OPN;
```
@gvanrossum gvanrossum added the epic-generator DSL-based code generator for the interpreter(s) label Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
epic-generator DSL-based code generator for the interpreter(s)
Projects
None yet
Development

No branches or pull requests

2 participants