Lecture 1: Introduction
1 What is a Compiler?
1.1 A Bit of History
1.2 What does a Compiler look like?
1.3 What is EECS 483?
2 Course Outline
3 What will we do?
4 What will you learn?
8.7

Lecture 1: Introduction

1 What is a Compiler?

We all have an intuitive understanding of what a program is: it’s some thing that instructs a computer to do something. But the language in which we tend to write our programs is nothing like the language that the computer understands natively. Something must translate the source code of our programs into a form the computer understands.

Conceptually, there are two ways this could happen: first, we could write a program that takes the source code of our program and interprets it on the fly, translating as it goes. This is roughly how languages like Python and JavaScript run programs in it. In practice JavaScript uses a combination of an interpreter and "just-in-time" compilation for performance reasons The downside to this approach is that we must keep both the source code and the interpreter around whenever we want to run the program. In addition, the interpreter adds a level of runtime overhead because it adds a layer of indirection: the interpreter has to figure out what the code means in order to run.

Second, we could write a program that translates the source code of our program into a form the machine can understand directly. Once this translation step has completed, we no longer need the source or the translator; we can just execute the resulting program directly.

In other words, a compiler is simply a function that maps an input string to an output string,

compiler : String -> String

where we typically consider the input and output strings to be programs in two different languages:

compiler : SourceProgram -> TargetProgram

For example, here are some well-known compilers

gcc, clang   : C/C++      -> Binary          /* a.out, .exe */
emcc         : C/C++      -> WebAssembly
rustc        : Rust       -> Binary
javac        : Java       -> JvmByteCode     /* .class */
scalac       : Scala      -> JvmByteCode
gwt          : Java       -> JavaScript      /* .js */
v8           : JavaScript -> Binary
nasm         : X64        -> Binary
pdftex       : LaTeX      -> PDF
pandoc       : Markdown   -> PDF or Html or Doc

Key Requirements on output program:

  1. Has the same meaning (“semantics”) as input,

  2. Is executable in relevant context (VM, microprocessor, web browser).

1.1 A Bit of History

Compilers were invented to avoid writing machine code by hand.

Richard Hamming – The Art of Doing Science and Engineering, p25:

In the beginning we programmed in absolute binary... Finally, a Symbolic Assembly Program was devised – after more years than you are apt to believe during which most programmers continued their heroic absolute binary programming. At the time [the assembler] first appeared I would guess about 1% of the older programmers were interested in it...a real programmer would not stoop to wasting machine capacity to do the assembly.

John A.N. Lee, Dept of Computer Science, Virginia Polytechnical Institute:

One of von Neumann’s students at Princeton recalled that graduate students were being used to hand assemble programs into binary for their early machine. This student took time out to build an assembler, but when von Neumann found out about it he was very angry, saying that it was a waste of a valuable scientific computing instrument to use it to do clerical work.

The term compiler was coined by Grace Hopper, who also wrote the first "compiler" (more accurately the first linker in today’s terminology). She was an early advocate for and designer of high-level programming languages.

1.2 What does a Compiler look like?

An input source program is converted to an executable binary in many stages:

1.3 What is EECS 483?

2 Course Outline

3 What will we do?

Writing a compiler directly from a high-level language to x64 isn’t easy to build in a single step. So we will write many compilers, each adding new features:

At this point you will have a language akin to Javascript without objects, but there are many more extensions we can try:

We may not get to implementing all of these, but we will see where they each fit into the architecture of a compiler. And as the final project of the course you will implement an extension of your own design!

4 What will you learn?