Structure of Compiler
Structure of Compiler
2
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
3
Compiler technology
◦ is more broadly applicable and has been
employed in rather unexpected areas.
Text-formatting languages,
like nroff and troff; preprocessor packages like eqn,
tbl, pic
Silicon compiler for the creation of VLSI circuits
Command languages of OS
Query languages of Database systems
4
A compiler acts as a translator,
transforming human-oriented programming
languages
into computer-oriented machine languages.
◦ Ignore machine-dependent details for
programmer
Programming Machine
Language Compiler Language
(Source) (Target)
5
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
6
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
7
Any compiler must perform two major tasks
Compiler
Analysis Synthesis
8
Source
Program Tokens SyntacticSemantic
Scanner Parser
(Character StructureRoutines
Stream)
Intermediate
Representation
Code
Generator
Target machine code
14
Code
Code Generator
Generator
[Intermediate Code
[Intermediate Code Generator]
Generator]
Non-optimized
Scanner
Scanner
[Lexical Intermediate Code
[Lexical Analyzer]
Analyzer]
Tokens
Code
Code Optimizer
Optimizer
Parser
Parser
[Syntax
[Syntax Analyzer]
Analyzer]
Optimized Intermediate Cod
Parse
tree
Code
Code Optimizer
Optimizer
Semantic
Semantic Process
Process
[Semantic
[Semantic analyzer]
analyzer] Target machine code
15
Compiler writing tools
Compiler generators or compiler-
compilers
E.g. scanner and parser
generators
Examples : Yacc, Lex
16
A programming language must include the
specification of syntax (structure) and
semantics (meaning).
Syntax typically means the context-free
◦ a = b + c is syntactically legal
◦ b + c = a is illegal
17
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
18
An interesting aspect is how programming
language design and compiler design
influence one another.
19
Languages such as Snobol and APL are
usually considered noncompilable
20
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
21
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.
22