Home
Engineering a Compiler, 2012, Cooper, Torczon
1 - Overview
-
Compiler: a computer program that translates other computer programs to prepare them for execution
-
Front end to deal with the source language, back end to deal with the target language
-
Intermediate form to translate between the two
-
Intermediate form is often optimized
-
Some compilers produce source code as an output, for example, c
-
Known as source-to-source compiler
-
Interpreter is different from a compiler, but many of the same concepts apply
1.2
-
There are two fundamental principles
-
The compiler must preserve the meaning of the program being compiled
-
The compiler must improve the input program in some discernible way
-
Front end converts source to Intermediate representation (IR)
-
Can (in theory) take IR and convert to multiple different back end targets
-
Or add in more phases between front end and backend, such as optimization
-
Optimization problems usually don't have a closed form solution
-
Will produce "optimized" code but not "optimal" code
1.3
-
Source form is "syntax"
-
Source meaning is "semantics"
-
Front end checks the syntax and semantics
-
Syntax is defined by a grammar
-
Two passes in the front end, for the scanner and the parser
-
Scanner converts stream of characters into words
-
The parser then checks that the words follow the rules of the grammar
-
Most optimization consists of analysis and transformation
-
The backend will decide what goes into registers and what goes into memory
-
Rewriting to machine operations is "instruction selection"
-
Will make another pass to translate "virtual registers" into actual registers on the target hardware, and update any required code as needed
-
Cost of instructions is considered during instruction scheduling