Compilers
Compilers
1
Overview and History (1)
Cause
Software for early computers was written in assembly language
The benefits of reusing software on different CPUs started to become
significantly greater than the cost of writing a compiler
2
Overview and History (2)
Compiler technology
is more broadly applicable and has been employed in rather
unexpected areas.
Text-formatting languages
Silicon compiler for the creation of VLSI circuits
Command languages of OS
Query languages of Database systems
3
What Do Compilers Do (1)
Programming Machine
Language Compiler Language
(Source) (Target)
4
What Do Compilers Do (2)
Compilers may generate three types of code:
Pure Machine Code
Machine instruction set without assuming the existence of any
operating system or library.
Mostly being OS or embedded applications.
Augmented Machine Code
Code with OS routines and runtime support routines.
More often
Virtual Machine Code
Virtual instructions, can be run on any architecture with a virtual
machine interpreter or a just-in-time compiler
Ex. Java
5
What Do Compilers Do (3)
Another way that compilers differ from one another is in
the format of the target machine code they generate:
Assembly or other source format
Relocatable binary
Relative address
A linkage step is required
Absolute binary
Absolute address
Can be executed directly
6
The Structure of a Compiler (1)
Compiler
Analysis Synthesis
Source
Program Tokens Syntactic Semantic
Scanner Parser Routines
(Character Stream) Structure
Intermediate
Representation
Code
Generator
8
Target machine code
The Structure of a Compiler (3)
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Scanner Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Parser Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Semantic Routines Representation
Perform two functions
Check the static semantics of each construct
Symbol and
Do the actual translation Optimizer
The heart of a compilerAttribute
Tables
Syntax
Syntax Directed
Directed Translation
Translation
Semantic
Semantic Processing Techniques
Processing Techniques
IR (Intermediate
(Used
by all
Representation)
IR (Intermediate Representation)
Phases of
The Compiler) Code
Generator
11
Target machine code
The Structure of a Compiler (6)
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Optimizer Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Code Generator Representation
Interpretive
Interpretive Code
Code Generation
Generation
Generating
Generating Code from Tree
Code from Tree
Grammar-Based
Grammar-Based Code
Code Generator
Generator Optimizer
Code
Generator
13 Target machine code
The Structure of a Compiler (8)
Code
Code Generator
Generator
[Intermediate Code
[Intermediate Code Generator]
Generator]
Tokens
Code
Code Optimizer
Optimizer
Parser
Parser
[Syntax
[Syntax Analyzer]
Analyzer]
Optimized Intermediate Code
Parse tree
Code
Code Generator
Generator
Semantic
Semantic Process
Process
[Semantic
[Semantic analyzer]
analyzer] Target machine code
14
The Syntax and Semantics of
Programming Language (1)
A programming language must include the specification
of syntax (structure) and semantics (meaning).
Syntax typically means the context-free syntax because of
the almost universal use of context-free-grammar (CFGs)
Ex.
a = b + c is syntactically legal
b + c = a is illegal
15
The Syntax and Semantics of
Programming Language (2)
The semantics of a programming language are commonly
divided into two classes:
Static semantics
Semantics rules that can be checked at compiled time.
Ex. The type and number of a function’s arguments
Runtime semantics
Semantics rules that can be checked only at run time
16
Computer Architecture and Compiler
Design
Compilers should exploit the hardware-specific feature
and computing capability to optimize code.
The problems encountered in modern computing
platforms:
Instruction sets for some popular architectures are highly
nonuniform.
High-level programming language operations are not always
easy to support.
Ex. exceptions, threads, dynamic heap access …
Exploiting architectural features such as cache, distributed
processors and memory
Effective use of a large number of processors
17
Compiler Design Considerations
Debugging Compilers
Designed to aid in the development and debugging of
programs.
Optimizing Compilers
Designed to produce efficient target code
Retargetable Compilers
A compiler whose target architecture can be changed without
its machine-independent components having to be rewritten.
18