| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
🤖 New build scheduled with the buildbot fleet by @gvanrossum for commit b92e879 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
Sorry, something went wrong.
This once again refactors a lot of the code generator. There are still cleanups to be done, and I'd like things to be more compact, but I think most of the refactoring is now done.
sup: SuperInstruction mac: MacroInstruction super: Super macro: Macro
I tried to split it into InstDef and OpDef, removing kind, but that caused problems because the Instruction class inherits from InstHeader.
|
@brandtbucher If you want larger diffs I can send you a later version that also updates a bunch of instructions (BINARY_OP_INPLACE_ADD_UNICODE and COMPARE_OP*) and adds typed stack effects. |
Sorry, something went wrong.
|
As before, a later version passed all the buildbot tests and I am confident this one would too. |
Sorry, something went wrong.
There was a problem hiding this comment.
Here are a few hints about the refactorings.
This PR intentionally doesn't contain any changes to instruction definitions, to highlight that the generated output is unchanged (except for one detail).
Sorry, something went wrong.
There was a problem hiding this comment.
Thanks for your patience! Excited to see these in action. :)
Do we have any plans to desugar superinstructions into macros? Could help unify some of the repeated logic in the generator.
Sorry, something went wrong.
| if dedent != 0 and tkn.kind == 'COMMENT' and '\n' in text: | ||
| if dedent < 0: | ||
| text = text.replace('\n', '\n' + ' '*-dedent) | ||
| # TODO: dedent > 0 |
There was a problem hiding this comment.
Leaving this TODO for future work?
As a minor nit: the double-negative of a "negative dedent" is sort of strange to me. I'd personally find it easier to reason about "indents" and "negative indents"... but I'm not sure if that makes things more difficult elsewhere.
Sorry, something went wrong.
There was a problem hiding this comment.
Yeah, I inherited that from the lexer. I'll eventually fix it. For a while it was inconvenient because there was also a variable named indent in a lot of places. That's now self.indent. (But still, it's a string of spaces rather than an int.)
Sorry, something went wrong.
| while self.expect(lx.PLUS): | ||
| if tkn := self.require(lx.IDENTIFIER): | ||
| ops.append(tkn.text) | ||
| self.require(lx.SEMI) | ||
| if op := self.op(): | ||
| ops.append(op) |
There was a problem hiding this comment.
Does this allow a single op followed by a bunch of +s? It looks like it might...
Sorry, something went wrong.
| if (tkn := self.expect(lx.IDENTIFIER)) and tkn.text == "macro": | ||
| if self.expect(lx.LPAREN): | ||
| if tkn := self.expect(lx.IDENTIFIER): | ||
| if self.expect(lx.RPAREN): | ||
| if self.expect(lx.EQUALS): | ||
| if uops := self.uops(): |
There was a problem hiding this comment.
Hmmmm. I know you prefer one test per if, but I think six nested ifs is pushing it for generated code, let alone human code. Maybe one test per line?
| if (tkn := self.expect(lx.IDENTIFIER)) and tkn.text == "macro": | |
| if self.expect(lx.LPAREN): | |
| if tkn := self.expect(lx.IDENTIFIER): | |
| if self.expect(lx.RPAREN): | |
| if self.expect(lx.EQUALS): | |
| if uops := self.uops(): | |
| if ( | |
| (tkn := self.expect(lx.IDENTIFIER)) | |
| and tkn.text == "macro" | |
| and self.expect(lx.LPAREN) | |
| and (tkn := self.expect(lx.IDENTIFIER)) | |
| and self.expect(lx.RPAREN) | |
| and self.expect(lx.EQUALS) | |
| and (uops := self.uops()) | |
| ): |
But consistency within the file is probably more important.
Sorry, something went wrong.
| else: | ||
| return CacheEffect(tkn.text, size) | ||
| raise self.make_syntax_error("Expected integer") | ||
| else: |
There was a problem hiding this comment.
This is another case where the deep nesting impairs readability: with the naked eye, it's pretty hard to tell which if this else corresponds to.
Sorry, something went wrong.
| DEFAULT_INPUT = os.path.relpath( | ||
| os.path.join(os.path.dirname(__file__), "../../Python/bytecodes.c") | ||
| ) | ||
| DEFAULT_OUTPUT = os.path.relpath( | ||
| os.path.join(os.path.dirname(__file__), "../../Python/generated_cases.c.h") | ||
| ) |
There was a problem hiding this comment.
Sorta defeats the purpose of os.path.join... ;)
| DEFAULT_INPUT = os.path.relpath( | |
| os.path.join(os.path.dirname(__file__), "../../Python/bytecodes.c") | |
| ) | |
| DEFAULT_OUTPUT = os.path.relpath( | |
| os.path.join(os.path.dirname(__file__), "../../Python/generated_cases.c.h") | |
| ) | |
| DEFAULT_INPUT = os.path.relpath( | |
| os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, "Python", "bytecodes.c") | |
| ) | |
| DEFAULT_OUTPUT = os.path.relpath( | |
| os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, "Python", "generated_cases.c.h") | |
| ) |
Sorry, something went wrong.
| for i, var in enumerate(reversed(up.stack[: up.final_sp]), 1): | ||
| self.out.emit(f"POKE({i}, {var});") | ||
|
|
||
| self.out.emit(f"DISPATCH();") |
There was a problem hiding this comment.
| self.out.emit(f"DISPATCH();") | |
| self.out.emit("DISPATCH();") |
Sorry, something went wrong.
Yeah, it would be nice if instead of super(LOAD_FAST__LOAD_CONST) = LOAD_FAST + LOAD_CONST; we could write macro(LOAD_FAST__LOAD_CONST) = LOAD_FAST + JOIN + LOAD_CONST; I had originally thought that JOIN could be defined like this: op(JOIN, (--)) {
NEXTOPARG();
next_instr++;
}
But next_instr isn't pointing where we expect it to be pointing, and bumping it will make things worse. Instead we could define op(JOIN, (word/1 --)) {
oparg = _Py_OPARG(word);
}
I only came up with that while writing this reply, I'll have to play with it to see if it'll work. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Note: this doesn't yet support cache effects in the macro definition itself (e.g. macro(X) = counter/1 + FOO + BAR + unused/2). That seems something for a follow-up (we don't even have a use case for the current thing yet).
Details
(From two original commits that were since merged.)
Super-instructions can now have cache effects.
To do this I mostly just had to move the cache effects code into
Instr*.write_body(), reducing the responsibilities of Instr*.write().
(I also had to fiddle a bit with indents.)
For macros the same approach would almost work, except that
next_instr might point in the middle of the cache when we encounter
DEOPT_IF() or ERROR_IF() in a second or further component.
I have to think more about that.
NOTES:
super-instruction).
This shouldn't matter.
Macro instructions can now also have cache effects.
We pass the initial cache offset into write_body().
This is a little fiddly because everything is different
for super-instructions vs macros:
bump next_instr after each op.
and we bump next_instr at the end.
Also, I had to move the bump of next_instr back into Instr*.write().
It is better placed there anyway because that function avoids the bump
if the C code already ends in a goto, return or DISPATCH*() call.
(The previous commit emitted one unreachable bump, which is now fixed.)
Tested manually.
NOTES
There's more refactoring coming.
Also included: