Home Contact Projects

SilvIR: Introduction2021-02-10

I'm planning to work on an IR for Silver, an attribute-grammar based language for compiler construction. This'll probably form the core of my MS thesis work, so my advisor recommended I blog about it as a way to communicate everything properly to the rest of the group, with the side benefits of getting a head start on laying everything out for writing the actual thesis.

Background

(If you're a MELT group member I'm making read this, feel free to skip this section.)

Attribute grammars are a formalism for describing computations on trees. The core idea is that you describe the syntax (concrete or abstract) of your language with a grammar, and then can perform transformations on it by describing them in terms of attributes.

Generally, attributes can be divided into two classes, inherited and synthesized. Inherited attributes (informally, inhs) are passed from the parent node down to its children. Synthesized attributes (informally, syns) are computed from inherited attributes and children, and typically are passed from children up to their parents.

Typical examples of inherited attributes include the environment and other information about the context of a term. Typical examples of synthesized attributes include the type of a term, the errors present on a term and its subterms, and the translation of a term to an IR or target language.

Silver implements one particular evaluation strategy for attribute grammars, demand-driven evaluation. This looks approximately like a lazy functional language, and many idioms are shared between Haskell and Silver as a result.

Motivation

Silver is super convenient to use for writing compilers with, but its performance is a lot worse than I wish it was. Furthermore, lots of optimizations that would be really nice to have implemented are pretty dang tricky. This is mostly for historical reasons relating to the implementation of Silver, rather than its properties or expressiveness as a language -- Silver best-practices didn't exist when Silver was written, so it doesn't follow them.

Currently, Silver compiles directly (i.e., without an IR!) to a huge amount of Java source code, which is unideal from an aesthetic perspective, and makes implementing additional passes pretty tricky.

The big goals of SilvIR as an IR for Silver are:

Initially I plan to compile Silver to SilvIR, and write an interpreter for SilvIR with the Truffle Language Implementation Framework in order to get access to a fast runtime and high-throughput GC easily.

Furthermore, as "stretch goals," it'd be kinda nice to have:

Design Considerations

Silver supports a large number of extensions to the basic idea of attribute grammars, and supporting all of them is necessarily a goal. Notable extensions include:

It would also be worthwhile to design SilvIR in such a way that features other attribute grammar systems support can be added to Silver without having to overhaul SilvIR. In a later post, we'll explore how ordered attributes and circular attributes can be translated to SilvIR.

All of these are used within the implementation of Silver, so must be supported before Silver can be bootstrapped on SilvIR. In the next post, we'll look at (the current draft of) the actual definition of the IR.