SLD 1
SLD 1
Introduction
1
Textbook and Other Classroom
Material
Class textbook
Compilers: Principles, Techniques, and Tools,
Aho, Sethi, Ullman (red dragon book)
Crafting a Compiler with C,
Other useful books
Lex & Yacc, Levine, Mason and Brown
Advanced Compiler Design & Implementation,
Steven Muchnick
2
Course Grading
Components
Exam 50%
Project 40%
Homeworks 10%
3
Why Compilers?
Compiler
A program that translates from 1 language to
another
It must preserve semantics of the source
It should create an efficient version of the target
language
4
Why Compilers?
5
Compilers are Translators
Fortran, C, C++, Java
Text processing
language
Command
Language
Natural
language
6
Compiler Structure
Object
Source
Program Loader, Linker
Program Translator and Run-time Output
System
Source language
Fortran, Pascal, C, C++
VHDL, Tex, Html
Target language
Machine code, assembly
High-level languages, simply actions
7
General Structure of a Modern
Source
Compiler
Program Lexical Analysis Scanner
8
Assembly code and Assemblers
Assembly
Code
Compiler Assembler Machine code
9
Interpreters
"Execute" the source language directly.
Interpreters directly produce the result of a
computation, whereas compilers produce
executable code that can produce this result.
Each language construct executes by
invoking a subroutine of the interpreter, rather
than a machine instruction.
execution is immediate
elaborate error checking is possible
Disadvantage: is slow; space overhead
10
Lexical Analysis (Scanner)
11
Lexical Analysis (Scanner)
12
Lex/Flex
13
Parser
14
Parser
15
Static Semantic Analysis
16
Static Semantic Analysis
17
Backend
Frontend
Statements, loops, etc
These broken down into multiple assembly
statements
Machine independent assembly code
3-address code, RTL
Infinite virtual registers, infinite resources
Standard opcode repetoire
load/store architecture
18
Backend
Goals
Optimize code quality
Map application to real hardware
19
Dataflow and Control Flow
Analysis
Provide the necessary information about variable
usage and execution behavior to determine when a
transformation is legal/illegal
Dataflow analysis
Identify when variables contain interesting
values
Which instructions created values or consume
values
DEF, USE, GEN, KILL
20
Dataflow and Control Flow Analysis
21
Optimization
How to make the code go faster
Classical optimizations
Dead code elimination remove useless code
Common subexpression elimimation recomputing the same
thing multiple times
Machine independent (classical)
Focus of this class
Useful for almost all architectures
Machine dependent
Depends on processor architecture
Memory system, branches, dependences
22
Code Generation
Mapping machine independent assembly code to the target
architecture
Virtual to physical binding
Instruction selection best machine opcodes to implement
generic opcodes
Register allocation infinite virtual registers to N physical
registers
Scheduling binding to resources (ie adder1)
Assembly emission
Machine assembly is our output, assembler, linker take over to
create binary
23
Compiler Writing Tools
Other terms: compiler generators, compiler compilers
scanner generators, example: lex
parser generators, example: yacc
symbol table routines,
code generation aids,
(optimizer generators, still a research topic)
These tools are useful, but bulk of work for
compiler writer is in semantic routines and
optimizations .
24
Sequence of Compiler Passes
In general, all compiler passes are run in
sequence.
They read the internal program representation,
process the information, and
generate the output representation.
25
Sequence of Compiler Passes
26
Language Syntax and Semantics
An important distinction:
Syntax defines the structure of a language
E.g., an IF clause has the structure:
IF ( expression ) THEN statements
Semantics defines its meaning
E.g., an IF clause means:
test the expression; if it evaluates to true, execute the
statements.
27
Context-free and Context-sensitive
Syntax
28
Compiler and Language Design
There is a strong mutual influence:
hard to compile languages are hard to read
easy to compile language lead to quality
compilers, better code, smaller compiler, more
reliable, cheaper, wider use, better diagnostics.
Example. Dynamic typing seems convenient because
type declaration is not needed. However, such
languages are
hard to read because the type of an identifier is not known
hard to compile because the compiler cannot make
assumptions about the identifier's type
29
Compiler and Architecture Design
30