Nanopass compilers in Rust?

Is there anything (crate, tutorial, prototype code) for doing nanopass style compiler in Rust ?

I'm getting nothing useful from Google / crates.io.

My limited understanding of nanopass is that we define a large # of Instruction Set, where adjacent Instruction Sets are very similar (and can be transpiled in a 'nanopass'). Then, the compiler is just a chain of these nanopasses.

Is there any good example of such implementations in Rust? (If there is a good reason why nanopass & Rust are not a good fit, I am interested in hearing that too.)

On a theoretical level, based on my own experience, there is one thing that could potentially make this somewhat painful in Rust. You'd need to define a similar but not identical set of types for each level of the IR/instruction set. This would basically be unavoidable boilerplate, unless you are willing to sacrifice some amount of type safety by defining neighboring IR layers as different subsets of the same set of node types and ensuring subset boundaries by means of runtime assertions.

Another solution could be something like MLIR is to LLVM. You'd have to be very principled and careful, but you could lay out a generic (both in the broad and the type-theoretical sense) framework for defining IRs in a way that different layers are just specializations of the same structure, or maybe a few structures. For example, you could have a kind of layer that explicitly represents control flow and data flow, but earlier passes might be closer to an AST, only implicitly representing concepts of the source language. You could have types and untyped representations as well, etc.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.