



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




This course is a thorough introduction to compiler design, focusing on more low-level and systems aspects rather than high-level questions such as polymorphic type inference or separate compilation. You will be build- ing several complete end-to-end compilers for successively more complex languages, culminating in a mildly optimizing compiler for a (subset of a) safe variant of the C programming language to x86-64 assembly language. For the last project of this course you will have the opportunity to opti- mize more aggressively, to implement a garbage collector, or retarget the compiler to an abstract machine. The safe variant of the C programming language which we compile in this course is called C0 and has been de- veloped at Carnegie Mellon University by Frank Pfenning et al. based on the reference compiler from the 2009 Compiler Design course at Carnegie Mellon.
In this overview we review the goals for this class and give a general description of the structure of a compiler. Additional material can be found in the optional textbook [App98, Chapter 1].
∗These course notes are by Frank Pfenning from previous versions of the Compiler De- sign course. This version of the notes are minor edits for the course 15-411 on Compiler Design in Fall’10+11 taught by Andr´e Platzer.
L1.2 Compiler Design: Overview
2 Goals
After this course you should know how a compiler works in some depth. In particular, you should understand the structure of a compiler, and how the source and target languages influence various choices in its design. It will give you a new appreciation for programming language features and the implementation challenges they pose, as well as for the actual hardware ar- chitecture and the runtime system in which your generated code executes. Understanding the details of typical compilation models will also make you a more discerning programmer. You will also understand some specific components of compiler tech- nology, such as lexical analysis, grammars and parsing, type-checking, in- termediate representations, static analysis, common optimizations, instruc- tion selection, register allocation, code generation, and runtime organiza- tion. The knowledge gained should be broad enough that if you are con- fronted with the task of contributing to the implementation of a real com- piler in the field, you should be able to do so confidently and quickly. For many of you, this will be the first time you have to write, maintain, and evolve a complex piece of software. You will have to program for correct- ness, while keeping an eye on efficiency, both for the compiler itself and for the code it generates. Because you will have to rewrite the compiler from lab to lab, and also because you will be collaborating with a partner, you will have to pay close attention to issues of modularity and interfaces. Developing these software engineering and system building skills are an important goal of this class, although we will rarely talk about them explic- itly.
3 Compiler Requirements
As we will be implementing several compilers, it is important to under- stand which requirement compilers should satisfy. We discuss in each case to what extent it is relevant to this course.
Correctness. Correctness is absolutely paramount. A buggy compiler is next to useless in practice. Since we cannot formally prove the correctness of your compilers, we use extensive testing. This testing is end-to-end, ver- ifying the correctness of the generated code on sample inputs. We also ver- ify that your compiler rejects programs as expected when the input is not well-formed (lexically, syntactically, or with respect to the static semantics),
L1.4 Compiler Design: Overview
with executable code in order help users debug runtime errors in their pro- gram. In this course, we will not formally evaluate the quality or detail of your error messages, although you should strive to achieve at least a minimum standard so that you can use your own compiler effectively.
Retargetability. At the outset, we think of a compiler of going from one source language to one target language. In practice, compilers may be re- quired to generate more than one target from a given source (for example, x86-64 and ARM code), sometimes at very different levels of abstraction (for example, x86-64 assembly or LLVM intermediate code). In this course we will deemphasize retargetability, although if you struc- ture your compiler following the general outline presented in the next sec- tion, it should not be too difficult to retrofit another code generator. One of the options for the last lab in this course is to retarget your compiler to pro- duce code in a low-level virtual machine (LLVM). Using LLVM tools this means you will be able to produce efficient binaries for a variety of concrete machine architectures.
4 The Structure of a Compiler
Certain general common structures have arisen over decades of develop- ment of compilers. Many of these are based on experience and sound engi- neering principles rather than any formal theory, although some parts, such as parsers, are very well understood from the theoretical side. The struc- ture of a compiler is shown in Figure 1. Smaller variations, of this overall structure are not uncommon. The structure of a typical classical compiler that separates register allocation and code generation is shown in Figure 2. In this course, we will begin by giving you the front and middle ends of a simple compiler for a very small language, and you have to write the back end, that is, perform instruction selection and register allocation. Con- sequently, Lectures 2 and 3 will be concerned with instruction selection and register allocation, respectively, so that you can write your own. We then turn to the front end and follow through the phases of a com- piler in order to complete the picture, while incrementally complicating the language features you have to compile. Roughly, we will proceed as follows, subject to adjustment throughout the course:
Compiler Design: Overview L1.
Lex
Parse
Sem.Analysis
tokens
AST
Reg. Alloc & Codegen
ASM+Link ASM
Source Exec
Translate
Optimize
IL
attr. AST IL
Figure 1: Structure of a compiler
The last lab is somewhat open-ended and allows either to implement fur- ther optimizations, a garbage collector, or a new back end which uses the low-level virtual machine (LLVM)^2. Note that the compilers you write for a lab will build on the compiler you have designed for your previous lab submissions! You will, thus, benefit from a clean and flexible design of your compiler.
5 Bibliographical Notes
Additional material about compiler design can be found in the optional textbook [App98]. Other standard references on compiler construction in- clude the “dragon book” [ALSU06] and Muchnik’s book on optimizing compilers [Muc97]. Another classical book that is available for educational purposes is the “silver book” [WG83]. A book that also covers functional, logic-based, and object-oriented languages is by Wilhelm and Maurer [WM95].
(^1) Thanks to David Koes for this diagram. (^2) See http://llvm.org
Compiler Design: Overview L1.
ftp://i44ftp.info.uni-karlsruhe.de/public_ html/papers/ggoos/CompilerConstruction.ps.gz.
[WM95] Reinhard Wilhelm and Dieter Maurer. Compiler Design. Addison-Wesley, 1995.