Compiler Construction Week 4
Compiler Construction Week 4
Zulfiqar Ali
UIT University
Week 4 - Contents
• Science of Building a Compiler
• Modeling in Compiler Design &
Implementation
• Science of Code Optimization
The Science of Building a compiler
• Real-world problems are solved by abstracting the essence
of the problem mathematically.
• A compiler must accept all source programs that conform
to the specification of the language; the set of source
programs is infinite and any program can b every large,
consisting of possibly millions of lines of code.
• Any transformation performed by the compiler while
translating a source program must preserve the meaning of
the program being compiled.
The Science of Building a compiler
• Compiler writers thus have influence over not
just the compilers they create, but all the
programs that their compilers compile.
• This leverage makes writing compilers
particularly rewarding; however, it also makes
compiler development challenging.
Modeling in Compiler Design &
Implementation
• The study of compilers is mainly a study of how we design
the right mathematical models and choose the right
algorithms, while balancing the need for generality and
power against simplicity and efficiency.
• Some of most fundamental models are finite-state
machines and regular expressions.
• These models are useful for describing the lexical units of
programs (keywords, identifiers, and such) and for
describing the algorithms used by the compiler to recognize
those units.
Modeling in Compiler Design &
Implementation
• The most fundamental models are context-free
grammars, used to describe the syntactic
structure of programming languages such as the
nesting of parentheses or control constructs.
• Similarly, trees are an important model for
representing the structure of programs
• and their translation into object code.
Science of Code Optimization
• The term "optimization" in compiler design refers to the attempts
that a compiler makes to produce code that is more efficient than
the obvious code.
• "Optimization" is thus a misnomer, since there is no way that the
code produced by a compiler can be guaranteed to be as fast or
faster than any other code that performs the same task.
• In modern times, the optimization of code that a compiler performs
has become both more important and more complex. It is more
complex because processor architectures have become more
complex, yielding more opportunities to improve the way code
executes.
Science of Code Optimization
• With the likely prevalence of multicore machines
(computers with chips that have large numbers of
processors on them), all compilers will have to face the
problem of taking advantage of multiprocessor machines.
• The use of a rigorous mathematical foundation allows us to
show that an optimization is correct and that it produces
the desirable effect for all possible inputs.
• You shall see, starting in Chapter 9 Models such as graphs,
matrices, and linear programs are necessary if the compiler
is to produce well optimized code.
Science of Code Optimization
• Compiler optimizations must meet the following
design objectives:
– The optimization must be correct, that is, preserve the
meaning of the compiled program,
– The optimization must improve the performance of
many programs.
– The compilation time must be kept reasonable
– The engineering effort required must be manageable.
Types of Code Optimization:
• The optimization process can be broadly
classified into two types :
– Machine Independent Optimization
– Machine Dependent Optimization
Applications of Compiler Technology
• Compiler design is not only about compilers, and
many people use the technology learned by
studying compilers in school, yet have never,
strictly speaking, written (even part of) a
compiler for a major programming language.
• Compiler technology has other important uses as
well. Additionally, compiler design impacts
several other areas of computer science.
Applications of Compiler Technology
• Implementation of High-Level Programming Languages .
• Optimizations for Computer Architectures
– Parallelism
– Memory Hierarchies
• Design of New Computer Architectures
– RISC (Reduced Instruction-Set Computer) architecture.
• Program Translations
– Binary Translation , Hardware Synthesis, Database Query Interpreters
• Hardware Synthesis
– Tools for optimizing the circuits
• Database Query Interpreters
• Compiled Simulation
• Type Checking
• Bounds Checking
• Memory-Management Tools
Reference
• Compilers: Principles, Techniques, and Tools, A. V.
Aho, R. Sethi and J. D. Ullman, Addison-Wesley, 2nd
ed., 2006.
THANK YOU