-
Notifications
You must be signed in to change notification settings - Fork 201
Port the DSL to Rust #342
Description
Cretonne's Python-based DSL allows target ISAs to be described in a concise manner, and can generate tables, code, and documentation from them. It's common for compilers to use DSLs for such purposes; see also LLVM's tablegen, GCC's machine description language, libfirm's perl (example) and python (example), and others.
Python compares pretty favorably to obvious alternatives, in particular by being a real programming language, and a fairly friendly one at that. However, using Python in Cretonne has some downsides too:
- There's extra overhead for developers that don't work with Python on a regular basis. They may not have mypy, flake8, or python-aware or even mypy-aware IDE plugins installed, they may not remember how variable scoping works, and so on.
- The current Python code makes use of some "magical" features, such as
__getitem__
,isinstance
, etc. which may be fine for people who work with Python regularly, but which make it harder to follow the code for people who aren't. - The current Python code also behaves in "magical" ways sometimes, such as
PUT_OP
in the x86 target. It's used in Rust code, but it's not a Rust construct; it's edited out byreplace_put_op
. In some cases, it's replaced byput_mp2
, but it's not easy to determine when that name is used, as there are no other occurrences ofput_mp2
ormp2
in the repository. As another example, the DSL infers instruction formats from instruction fields implicitly, in a way that makes it confusing when one is adding a new format and accidentally colliding with an existing format. - Cretonne has ambitions of being able to compile Rust code in the future, so it would be cooler if it could compile all of itself.
None of these are show-stoppers, and one option is to just keep the current code and refactor it to help out with some of these issues. But we should also consider where we want to be in the long term; it'd be easier to do a large architectural change now, while the code is (relatively) simple, as it'll grow more complex in the future as we add more features. One option is Rust's procedural macros, except that they're not yet stable. A possible intermediate step could be to use a build.rs.
The instruction descriptions are fairly elaborate, so one way to start experimenting with this would be to start by rewriting some of the simper parts, such as just gen_types.py
, to get started setting up the infrastructure. gen_types.py
's output file is target/debug/build/cretonne-codegen-*/out/types.rs, which is pretty simple and demonstrates what it does.
If anyone's interested in working on this, I'd be happy to mentor.