CH1-1 and 1-2
CH1-1 and 1-2
CH1-1 and 1-2
Introduction to Compiling
Run-Time Environment
Code Optimization
Code Generation
1
2
Session objectives
Introduction to Compiling
Grouping
3
Introduction
Compiler design is the field of computer science that deals with the
creation of compilers.
High-level languages are easier for humans to read and write, but
machines don't understand them directly.
4
Introduction
The computer can understand only the Low Level
Language i.e. machine language
7
Introduction
Ultimately, programs written in a high-level language must be
translated into machine language by a compiler.
8
Introduction
Benefits of Compilers
Abstraction: Compilers allow programmers to write code in
languages that are more intuitive and productive compared to
writing directly in machine code.
Portability: Since the compiler translates the code for the specific
machine, the same high-level code can often be compiled to run on
different computer architectures with different machine code.
(Though some modifications might be needed)
Error Detection: Compilers help catch errors early in the
development process, saving programmers time and effort.
9
What Is Compiler
Some times a compiler is written in the same language for which one
is writing a compiler. That is done through Bootstrapping.
11
Terminologies (compiler & interpreter)
Compiler:
A program that translates an executable program in one
language into an executable program in another language
12
Compiler
Outp
ut
➢ Interpreter:
➢ It convert line by line from the total source code.
➢ Error will be displayed at the time of conversion.
14
Interpreter
A program that reads an executable program and produces the
results of running that program
15
Compiler Interpreter
16
Compiler Interpreter
• A compiler always generates • An interpreter does not
an intermediary object code. It generate an intermediary code.
will need further linking. Hence, an interpreter is highly
Hence more memory is efficient in terms of its
needed. memory.
• A compiler generates the error • Keeps translating the program
message only after it scans the continuously till the first error
complete program and hence is confronted. If any error is
debugging is relatively harder spotted, it stops working and
while working with a hence debugging becomes
compiler. easy.
• Compliers are used by • Interpreters are used by
programming languages like C programming languages like
and C++ for example Ruby and Python for example
17
Structure of Compiler
Compiler that maps a source program into a
semantically equivalent target program.
Analysis/front end
Breaks up the source program into constituent pieces & creates an
intermediate representation of source program.
19
Analysis / Front end/
It includes:- Lexical analysis, Syntax analysis , and Semantic
analysis
Semantic Analyzer
IR
Front end
Recognize legal code
Report errors
Shape code for the back end
21
Synthesis / Back end
The synthesis part constructs the desired target program from the
intermediate representation and the information in the symbol table.
Back end
Translate IR into target machine code
23
The Grouping of Phases into Passes
A “pass” is a complete traversal of the source program, or a complete traversal
of some internal representation of the source program (such as an AST(abstract
syntax tree)).
What and how many passes a compiler does over the source program is an
important design decision.
A single pass compiler makes a single pass over the source text, parsing,
analyzing, and generating code all at once.
When we merge all the phases of compiler design in a single module, then
it is called a single pass compiler. In the case of a single pass compiler, the
source code converts into machine code.
25
Single Pass Compiler
26
Single Pass Compiler
Dependency diagram of a typical Single Pass Compiler:
27
Single Pass Compiler
Advantages:
Speed: One-pass compilers can potentially be faster than their multi-pass counterparts
because they avoid the overhead of loading and unloading the code from memory for each
pass.
Simplicity: The design of a one-pass compiler can be simpler as it doesn't need to manage
multiple stages and intermediate code representations.
Disadvantages:
Limited Optimization: Due to the single-pass nature, one-pass compilers often struggle
with complex optimizations.
Less Error Detection: Since all the analysis happens simultaneously, one-pass compilers
might have limitations in catching certain errors that require understanding the context of
the entire program.
28
Two Pass Compiler
The Processor that runs through the program to be translated twice is
considered a two-pass compiler
IR
29
Two Pass Compiler
Advantages:
Better Error Detection: By separating analysis and generation, two-pass compilers can catch
errors that depend on the context of the entire program, which might be missed in a single pass.
Improved Optimization: The second pass can analyze the entire program structure and use this
information for more effective code optimization techniques.
Flexibility: The use of an intermediate representation allows for some decoupling between the
front-end (analysis) and back-end (code generation) stages. This can be useful for targeting
different machine architectures or implementing optimizations specific to the target machine.
Disadvantage:
Slower Compared to One-Pass: Two scans can be slightly slower than a single pass, especially
for smaller programs. This slowdown is often negligible compared to the benefits.
Increased Complexity: The design of a two-pass compiler can be more complex due to
managing separate stages and potentially intermediate code.
30
Multi Pass Compiler
A program’s source code or syntax tree is processed many times by the
multi pass compiler.
It breaks down a huge program into numerous smaller programs and runs
them all at the same time.
It creates a number of intermediate codes, All of these multi passes use the
previous phase’s output as an input.
31
Multi Pass Compiler
32
Multi Pass Compiler
A multi pass compiler makes several passes over the program. The output
of a preceding phase is stored in a data structure and used by subsequent
phases. Dependency diagram of a typical Multi Pass Compiler:
Compiler Driver
calls calls
calls
33
33
Multi Pass Compiler
Advantages:
Improved Optimization: By having separate passes, compilers can perform more sophisticated optimizations
across the entire program. Each pass can take advantage of information gathered in previous passes.
Better Error Detection: Separating analysis from code generation allows for more comprehensive error
checking. Errors that rely on understanding the whole program structure can be effectively identified.
Modular Design: The division into passes promotes a modular design, making the compiler easier to develop,
maintain, and potentially extend with new functionalities in future passes.
Flexibility for Different Targets: The use of intermediate code allows for targeting various machine
architectures by implementing machine-specific optimizations in later passes.
Disadvantage:
Slower Execution: Compared to one-pass compilers, multi-pass compilers can be slower due to the overhead of
loading and unloading the code for each pass. However, modern compilers optimize this process significantly.
Increased Complexity: Designing a multi-pass compiler can be more complex due to managing multiple
stages, intermediate code representations, and ensuring proper information flow between passes.
34
34