0% found this document useful (0 votes)
8 views27 pages

lect01

Uploaded by

ruhinalmuhit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views27 pages

lect01

Uploaded by

ruhinalmuhit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Compiler Design

Md. Saiful Islam


Associate Professor
Dept. of CSE, RU

2 Dec 2024 1
Introduction
– Any program written in a programming language must be
translated before it can be executed. This translation is
typically accomplished by a software system called
translator.
– Formally, a translator is a program that takes as input a
program written in programming language (i.e. source
code) and produces as output a program in another
language (i.e. object code or target language).
Source program

Translator

Target program
2 Dec 2024 2
Introduction
Source program Source program

Compiler Interpreter

Object Code Intermediate Code


– If the source is a high-level language and the target
language is object code then the translator is called a
compiler.
– Certain other translators transform a programming
language, called intermediate code, which can be
directly executed, called interpreter.
3
2 Dec 2024
Compiler Vs Interpreter
– In the case of interpreter, the source program translated
one line at a time, but the translation process is slow.
– But the case for compiler, the whole source program is
translated one time, hence fast processing.
– The compiled code (i.e. object code) is platform
dependent. On the other hand interpreted code is
platform independent.
– Interpreter is usually for small and simple problems (i.e.
web programs), compiler is for large and complex
problems.

2 Dec 2024 4
Other translators

High-level Language Mid-level Language

Pre-processor Assembler

High-level Language Machine Code

2 Dec 2024 COMP36512 Lecture 1 5


Programming/Language codes
– Source code is the input to a compiler or other code
translator. Source code is normally generated by humans
(and thus hopefully able to be read and modified by
humans)
– Object code contains a sequence of instructions that the
processor can understand but that is difficult for a human
to read or modify.
– [Operating system or application software, it is usually
in the form of compiled object code and the source code
is not included.
– Proprietary software vendors usually don't want you to
try to improve their code (except open source
application)].
Programming/Language codes
– Byte code is the kind of code that can be
executed by a virtual machine. The virtual
machine reads the bytecode and performs the
operations it specifies. Byte code is typically
slower but platform independent.
– Machine code is code that is directly executable
by the computer’s physical processor without
further translation. Load into memory and go.
2 Dec 2024 7
Source program
Examples
compiler
• C is typically compiled
• Lisp is typically interpreted Target program

• Java is compiled to bytecodes, which are then


interpreted
Also:
• C++ to Intel Core 2/.../Assembly
• C++ to C
• High Performance Fortran (HPF) to Fortran
(parallelising compiler)
• C to C (or any language to itself)
2 Dec 2024 8
Execution of a Program
HLL

Pre-processor
Pure HLL

Compiler
Assembly Language

Assembler
M/C Code (Relocatable)

Loader/Linker

Exe/absolute
Machine code
2 Dec 2024 9
Execution of a Program
HLL

Pre-processor
Pure HLL

Compiler
Assembly Language

Assembler
M/C Code (Relocatable)

Loader/Linker

Exe/absolute
Machine code
2 Dec 2024 10
Execution of a Program
HLL

Pre-processor
Pure HLL

Compiler
Assembly Language

Assembler
M/C Code (Relocatable)

Loader/Linker

Exe/absolute
Machine code
2 Dec 2024 11
Execution of a Program
HLL

Pre-processor
Pure HLL

Compiler
Assembly Language

Assembler
M/C Code (Relocatable)

Loader/
Linker
Exe/absolute
Machine code
2 Dec 2024 12
Phases (or structure) of
Compiler

2 Dec 2024 13
HLL

Lexical analysis
Stream of tokens
Syntax analysis
Parse tree

Symbol table Semantic analysis Error


manager Parse tree (semantically)
Handler
Intermediate Code gen
Three address code
Code optimization
Three address code (modified)
Code generation

Assembly
2 Dec 2024 Code 14
HLL

Lexical analysis
Stream of tokens

Syntax analysis
Parse tree

Symbol table Semantic analysis Error


manager Parse tree (semantically)
Handler
Intermediate Code gen
Three address code
Code optimization
Three address code (modified)
Code generation

Assembly
2 Dec 2024 code 15
HLL

Lexical analysis
Stream of tokens
Syntax analysis
Parse tree

Symbol table Semantic analysis Error


manager Parse tree (semantically)
Handler
Intermediate Code gen
Three address code
Code optimization
Three address code (modified)
Code generation

Assembly
2 Dec 2024 code 16
HLL

Lexical analysis
Stream of tokens
Syntax analysis
Parse tree

Symbol table Semantic analysis Error


manager Parse tree (semantically)
Handler
Intermediate Code gen
Three address code
Code optimization
Three address code (modified)
Code generation

Exe/absolute
2 Dec 2024 Machine code 17
HLL

Lexical analysis
Stream of tokens
Syntax analysis
Parse tree

Symbol table Semantic analysis Error


manager Parse tree (semantically)
Handler
Intermediate Code gen
Three address code
Code optimization
Three address code (modified)
Code generation

Assembly
2 Dec 2024 code 18
HLL

Lexical analysis
Stream of tokens
Syntax analysis
Parse tree

Symbol table Semantic analysis Error


manager Parse tree (semantically)
Handler
Intermediate Code gen
Three address code
Code optimization
Three address code (modified)
Code generation

Assembly
2 Dec 2024 Code 19
HLL

Lexical analysis
Stream of tokens
Syntax analysis
Parse tree

Symbol table Semantic analysis Error


manager Parse tree (semantically)
Handler
Intermediate Code gen
Three address code
Code optimization

Code generation

Exe/absolute
2 Dec 2024 Machine code 20
HLL

Lexical analysis
Stream of tokens
Syntax analysis
Parse tree

Symbol table Semantic analysis Error


manager Parse tree (semantically)
Handler
Intermediate Code gen
Three address code
Code optimization

Code generation

Exe/absolute
2 Dec 2024 Machine code 21
Phases with example

2 Dec 2024 22
2 Dec 2024 23
Qualities of a Good Compiler
What qualities would you want in a compiler?
– generates correct code (first and foremost!)
– generates fast code
– conforms to the specifications of the input language
– copes with essentially arbitrary input size, variables, etc.
– compilation time (linearly)proportional to size of source
– good diagnostics
– consistent optimisations
– works well with the debugger

2 Dec 2024 COMP36512 Lecture 1 24


Principles of Compilation
The compiler must:
• preserve the meaning of the program being compiled.
• “improve” the source code in some way.
Other issues (depending on the setting):
• Speed (of compiled code)
• Space (size of compiled code)
• Feedback (information provided to the user)
• Debugging (transformations obscure the relationship source code vs target)
• Compilation time efficiency (fast or slow compiler?)

2 Dec 2024 COMP36512 Lecture 1 25


Uses of Compiler Technology
• Most common use: translate a high-level program to object code
– Program Translation: binary translation, hardware synthesis, …
• Optimizations for computer architectures:
– Improve program performance, take into account hardware parallelism, etc…
• Automatic parallelisation or vectorisation
• Performance instrumentation: e.g., -pg option of cc or gcc
• Interpreters: e.g., Python, Ruby, Perl, Matlab, sh, …
• Software productivity tools
– Debugging aids: e.g, purify
• Security: Java VM uses compiler analysis to prove “safety” of Java
code.
• Text formatters, just-in-time compilation for Java, power
management, global distributed computing, …
Key: Ability to extract properties of a source program (analysis) and
transform it to construct a target program (synthesis)
2 Dec 2024 COMP36512 Lecture 1 26
Summary
• A compiler is a program that converts some input
text in a source language to output in a target
language.
• Compiler construction poses some of the most
challenging problems in computer science.
• Reading:
– Aho2, 1.1, 1.5; Aho1 1.1; Cooper1 1.1-1.3;
– Grune 1.1, 1.5

• Next lecture: structure of a typical compiler.

2 Dec 2024 COMP36512 Lecture 1 27

You might also like