Nanopass compiler infrastructure
R. Kent Dybvig, Dipanwita Sarkar · 2008
A compiler that is structured as a small number of monolithic passes is difficult to understand and difficult to maintain. Finding compiler bugs in such a compiler is often difficult, and adding new optimizations and analyses sometimes requires major restructuring that can result in subtle and tenacious bugs. A natural solution to this problem is to structure the compiler as a series of correctness-preserving transformations, each of which performs a small part of the compilation process. Unfortunately, as each pass of the compiler becomes simpler, the number of passes required to accomplish the entire task becomes greater. Even though a pass may make significant changes to only a few of the intermediate-language forms, it must still handle the remaining forms. This coding overhead can more than offset the benefits of having a fine-grained structure, as the sheer volume of essentially repetitive code can obscure the meaningful transformations performed by the compiler. This dissertation describes a new “nanopass” infrastructure that eliminates most of the repetitive coding overhead, making the development of such compilers simpler, less tedious, and less error-prone. The nanopass infrastructure formalizes the declaration of intermediate languages, and a compiler written using the infrastructure rejects intermediate-language programs that are not well-formed according to these declarations, increasing reliability. The infrastructure also represents intermediate-language code internally using efficient lowlevel data structures while allowing the compiler writer to interact with the code at a higher, more readable level. The infrastructure provides additional tools to support rapid development of new compilers, as well as experimentation with new language features and code improvement strategies in existing compilers. The modular nature of the compilers developed with the infrastructure should also support rapid adaptation of general-purpose compilers to domain-specific purposes.