SlideShare una empresa de Scribd logo
1 de 39
CSE 420
Lecture 12
Compiler Architecture
Parser
Static
Checker
Intermediate
Code generator
Code
Generator
Intermediate
Code
Target
language
Front End Back End
• m  n compliers can be built by writing m front ends
and n back ends – save considerable amount of effort
• We assume parsing, static checking and IC
generation is done sequentially
– These can be combined and done during parsing
• Static checking
– Operator operand compatibility
– Proper placement of break/continue keywords etc.
2
Intermediate Code (IC)
• The given program in a source language is converted
to an equivalent program in an intermediate language
by the IC generator.
• Ties the front and back ends together
• Language and Machine neutral
• Many forms
• Level depends on how being processed
• More than one intermediate language may be used by
a compiler
3
• Intermediate language can be many different languages, and the
designer of the compiler decides this intermediate language.
– syntax trees can be used as an intermediate language.
– postfix notation can be used as an intermediate language.
– three-address code (Quadraples) can be used as an
intermediate language
• we will use quadraples to discuss intermediate code
generation
• quadraples are close to machine instructions, but they are
not actual machine instructions.
– some programming languages have well defined
intermediate languages.
• java – java virtual machine
• prolog – warren abstract machine
• In fact, there are byte-code emulators to execute instructions
in these intermediate languages.
Intermediate Code (IC)
4
Intermediate language levels
5
Intermediate Languages Types
• Graphical IRs:
– Abstract Syntax trees
– Directed Acyclic Graphs (DAGs)
– Control Flow Graphs
• Linear IRs:
– Stack based (postfix)
– Three address code (quadruples)
6
Graphical IRs
• Abstract Syntax Trees (AST) – retain essential
structure of the parse tree, eliminating unneeded
nodes.
• Directed Acyclic Graphs (DAG) – compacted AST to
avoid duplication – smaller footprint as well
• Control flow graphs (CFG) – explicitly model control
flow
7
ASTs and DAGs:
:=
a +
**
b - (uni)
c
:=
a +
b - (uni) b
*
- (uni)
c c
a := b *-c + b*-c
AST DAG
8
Implementation of DAG/AST: Value Number
Method
9
Three-Address Code
• A three-address code is:
x := y op z
where x, y and z are names, constants or compiler-
generated temporaries; op is any operator.
• But we may also the following notation for three-address
code (it looks like a machine code instruction)
op y,z,x
apply operator op to y and z, and store the result in x.
• We use the term “three-address code” because each
statement usually contains three addresses (two for
operands, one for the result).
10
Linearized Representation of DAG/AST
• Source Code
– a = b * -c + b * -c
• Three address code
• Tree Representation
11
Three-Address Statements
Binary Operator: op y,z,result or
result := y op z
where op is a binary arithmetic or logical operator. This binary
operator is applied to y and z, and the result of the operation is
stored in result.
Ex:
add a,b,c
gt a,b,c
addr a,b,c
addi a,b,c
Unary Operator: op y,,result or
result := op y
where op is a unary arithmetic or logical operator. This unary
operator is applied to y, and the result of the operation is stored
in result.
Ex: uminus a,,c
12
Three-Address Code
• Two concepts
– Address
– Instruction
• Address
– Name: source-program names to appear as addresses
– Constant: Different types of constants
– Compiler Generated temporary:
13
Three-Address Instruction
Assignment Type 1: x := y op z
op is a binary arithmetic or logical operation
x, y and z are addresses
Assignment Type 2: x := op z
op is a unary arithmetic or logical operation
x and z are addresses
Copy Instruction:x := y
x and z are addresses and x is assigned the value of y
14
Three-Address Instructions
Unconditional Jump: goto L
We will jump to the three-address code with the label L, and the
execution continues from that statement.
Ex: goto L1
jmp 7
Conditional Jump 1: if
// jump to L1
// jump to the statement 7
x goto L and if False x goto L
We will jump to the three-address code with the label L if x is TRUE
and FALSE, respectively. Otherwise, the following three-address
instruction in sequence is executed next.
Conditional Jump 2: if x relop y goto L
We will jump to the three-address code with the label L if the result of y
relop z is true, and the execution continues from that statement. If the result is
false, the execution continues from the statement following this conditional
jump statement.
15
Three-Address Statements (cont.)
Procedure Parameters: param x
Procedure Calls: call p,n
where x is an actual parameter, we invoke the procedure p with
n parameters.
16
Three-Address Statements (cont.)
Indexed Assignments:
x := y[i]
sets x to the value in location i memory units beyond location y
y[i] := x
sets contents of the location i memory units beyond location y to
the value of x
Address and Pointer Assignments:
x := &y
sets the r-value of x to l-value of y
x := *y where y is a pointer whose r-value is a location
sets the r-value of x equal to the contents of that location
*x := y
sets the r-value of the object pointed by x to the r-value of y
17
Three address code example
do i=i+1; while (a[i] < v)
L: t1=i+1 100: t1 = i +1
i=t1 101: i = t1
t2=i*8 102: t2=i*8
t3=a[t2] 103: t3=a[t2]
if t3 < v goto L 104: if t3 < v goto 100
(A) Symbolic Labels (B) Position Numbers
18
Representing 3-Address Statements
op, arg1, arg2, result
• x = minus y
– Does not use arg2
• x = y
– Op is =
• param a1
– Uses neither arg2 nor result
• Conditional/Unconditional jumps
– Put the target label in result
19
Quadruples
• a = b * -c + b * -c
Quadruples
• Store each fields directly
21
Triples
op arg1 arg2
[ ]= x i
:= 0 y
0
1
22
Indirect Triples
23
Translating Expression
24
Goal
25
Synthesized Code and Place Attributes
26
Evaluating the attributes for code generation
27
Three-address code for expression
28
Incremental Translation
• ‘code’ attribute can be long string
• Instead of building up ‘E.code’
– We arrange to generate new three-address instructions
– ‘code’ attribute is not used
– ‘gen’ method is used instead of ‘IR’
• ‘gen’ constructs a three address instruction and appends
it to the sequence of instructions generated so far
29
Syntax-Directed Translation into Three-Address Code
S  id := E
E  E1 + E2
E  E1 * E2
E  - E1
E  ( E1 )
{gen(ID.svalue ‘:=’ E.place)}
{E.place := NewTemp();
gen(E.place ‘:=’ E1.place ‘+’ E2.place )}
{E.place = NewTemp();
gen(E.place ‘:=’ E1.place ‘*’ E2.place ‘,’)}
{E.place = NewTemp();
gen(E.place ‘:=’ ‘minus’ E1.place)}
{E.place = E1.place;}
E  id {E.place = ID.svalue;}
30
Addressing Array Elements
• Elements of arrays can be accessed quickly if the elements are
stored in a block of consecutive locations.
A one-dimensional array A:
baseA low i width
baseA is the address of the first location of the array A,
width is the width of each array element.
low is the index of the first array element
… …
31
Addressing Array Elements (cont.)
baseA+(i-low)*width
can be re-written as i*width + (baseA-low*width)
should be computed
at run-time
can be computed
at compile-time
• So, the location of A[i] can be computed at the run-time by
evaluating the formula i*width+c where c is (baseA-
low*width) which is evaluated at compile-time.
• Intermediate code generator should produce the code to
evaluate this formula i*width+c (one multiplication and
one addition operation).
32
Two-Dimensional Arrays
• A two-dimensional array can be stored in
– either row-major (row-by-row) or
– column-major (column-by-column).
• Most of the programming languages use row-major method.
• Row-major representation of a two-dimensional array:
row1 row2 rown
33
Two-Dimensional Arrays (cont.)
• The location of A[i1,i2] is
baseA+ ((i1-low1)*n2+i2-low2)*width
baseA is the location of the array A.
low1
low2
is the index of the first row
is the index of the first column
n2 is the number of elements in each row
width is the width of each array element
• Again, this formula can be re-written as
((i1*n2)+i2)*width + (baseA-((low1*n1)+low2)*width)
should be computed
at run-time
can be computed
at compile-time
34
Multi-Dimensional Arrays
• In general, the location of A[i1,i2,...,ik] is
(( ... ((i1*n2)+i2) ...)*nk+ik)*width + (baseA-
((...((low1*n1)+low2)...)*nk+lowk)*width)
• So, the intermediate code generator should produce the codes
to evaluate the following formula (to find the location of
A[i1,i2,...,ik]) :
(( ... ((i1*n2)+i2) ...)*nk+ik)*width + c
• To evaluate the (( ... ((i1*n2)+i2) ...)*nk+ik portion of this formula,
we can use the recurrence equation:
e1 = i1
em = em-1 * nm + im
35
Translation of Array Elements
• One dimensional
base + i  w
w: width of each array element
• Two Dimensional
base + i1  w1 + i2  w2
w1: width of a row
w2: width of an element in a row
• k dimensional (generalized)
base + i1  w1 + i2  w2 + … + ik  wk
36
Translation of Array References
• Need to relate the address calculation formulas to a
grammar for array references
• Consider the non-terminal L to generate an array
L € L [E] | id [E]
• Nonterminal L has three synthesized attributes
– L.addr denotes a temporary used to compute the offset
for array reference ij x wj
– L.array is a pointer to the symbol-table entry
• L.array.base is used to determine the actual l-value of it
– L.type is the type of sub-array generated by L
• L.type.width gives the width of the type
37
Syntax-Directed Translation into Three-Address Code
S  id := E;
| L := E;
{gen(ID.svalue ‘:=’ E.place)}
{gen(L.array.base ‘[’ L.addr ‘]’ ‘:=’ E.place)}
E  E1 + E2
| id
| L
{E.place := NewTemp();
gen(E.place ‘:=’ E1.place ‘+’ E2.place )}
{E.place = ID.svalue;}
{E.place = NewTemp();
gen(E.place ‘:=’ L.array.base ‘[’ L.addr ‘]’);}
L  id [E]
| L1 [E]
{L.array = ID.svalue;
L.type = L.array.type.elem;
L.addr = NewTemp();
gen(L.addr ‘:=’ E.place ‘*’ L.type.width)}
{L.array = L1.array;
L.type = L1.type.elem;
t = NewTemp();
L.addr = NewTemp();
gen(t ‘:=’ E.place ‘*’ L.type.width);
gen(L.addr ‘:=’ L1.addr ‘+’ t)}
38
Thank You
39

Más contenido relacionado

La actualidad más candente

Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Time space trade off
Time space trade offTime space trade off
Time space trade offanisha talwar
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSARASWATHI S
 
Issues in design_of_code_generator
Issues in design_of_code_generatorIssues in design_of_code_generator
Issues in design_of_code_generatorvinithapanneer
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Codesanchi29
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler designRajkumar R
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler DesignKuppusamy P
 

La actualidad más candente (20)

Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Assembler
AssemblerAssembler
Assembler
 
Time space trade off
Time space trade offTime space trade off
Time space trade off
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Three Address code
Three Address code Three Address code
Three Address code
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 
Issues in design_of_code_generator
Issues in design_of_code_generatorIssues in design_of_code_generator
Issues in design_of_code_generator
 
System Programming Overview
System Programming OverviewSystem Programming Overview
System Programming Overview
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Run time storage
Run time storageRun time storage
Run time storage
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Code Generation
Code GenerationCode Generation
Code Generation
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 

Similar a CSE 420 Lecture 12 - Compiler Architecture and Intermediate Code Generation

Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...venkatapranaykumarGa
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12Rabia Khalid
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdfSHUJEHASSAN
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)Sourov Kumar Ron
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants Hemantha Kulathilake
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationAkshaya Arunan
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoreambikavenkatesh2
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++NUST Stuff
 
Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfRAnwarpasha
 

Similar a CSE 420 Lecture 12 - Compiler Architecture and Intermediate Code Generation (20)

Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
1.2 matlab numerical data
1.2  matlab numerical data1.2  matlab numerical data
1.2 matlab numerical data
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)
 
MATLABgraphPlotting.pptx
MATLABgraphPlotting.pptxMATLABgraphPlotting.pptx
MATLABgraphPlotting.pptx
 
Ch8a
Ch8aCh8a
Ch8a
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdf
 

Más de Iffat Anjum

Fog computing ( foggy cloud)
Fog computing  ( foggy cloud)Fog computing  ( foggy cloud)
Fog computing ( foggy cloud)Iffat Anjum
 
Cognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationCognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationIffat Anjum
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Iffat Anjum
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generationIffat Anjum
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environmentIffat Anjum
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxIffat Anjum
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Iffat Anjum
 
Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Iffat Anjum
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Iffat Anjum
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Iffat Anjum
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Iffat Anjum
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Iffat Anjum
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysisIffat Anjum
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysisIffat Anjum
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysisIffat Anjum
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 
Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerIffat Anjum
 
Distributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioDistributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioIffat Anjum
 
On qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareOn qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareIffat Anjum
 
Data link control
Data link controlData link control
Data link controlIffat Anjum
 

Más de Iffat Anjum (20)

Fog computing ( foggy cloud)
Fog computing  ( foggy cloud)Fog computing  ( foggy cloud)
Fog computing ( foggy cloud)
 
Cognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationCognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentation
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptx
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to Compiler
 
Distributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioDistributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radio
 
On qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareOn qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcare
 
Data link control
Data link controlData link control
Data link control
 

Último

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 

Último (20)

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 

CSE 420 Lecture 12 - Compiler Architecture and Intermediate Code Generation

  • 2. Compiler Architecture Parser Static Checker Intermediate Code generator Code Generator Intermediate Code Target language Front End Back End • m  n compliers can be built by writing m front ends and n back ends – save considerable amount of effort • We assume parsing, static checking and IC generation is done sequentially – These can be combined and done during parsing • Static checking – Operator operand compatibility – Proper placement of break/continue keywords etc. 2
  • 3. Intermediate Code (IC) • The given program in a source language is converted to an equivalent program in an intermediate language by the IC generator. • Ties the front and back ends together • Language and Machine neutral • Many forms • Level depends on how being processed • More than one intermediate language may be used by a compiler 3
  • 4. • Intermediate language can be many different languages, and the designer of the compiler decides this intermediate language. – syntax trees can be used as an intermediate language. – postfix notation can be used as an intermediate language. – three-address code (Quadraples) can be used as an intermediate language • we will use quadraples to discuss intermediate code generation • quadraples are close to machine instructions, but they are not actual machine instructions. – some programming languages have well defined intermediate languages. • java – java virtual machine • prolog – warren abstract machine • In fact, there are byte-code emulators to execute instructions in these intermediate languages. Intermediate Code (IC) 4
  • 6. Intermediate Languages Types • Graphical IRs: – Abstract Syntax trees – Directed Acyclic Graphs (DAGs) – Control Flow Graphs • Linear IRs: – Stack based (postfix) – Three address code (quadruples) 6
  • 7. Graphical IRs • Abstract Syntax Trees (AST) – retain essential structure of the parse tree, eliminating unneeded nodes. • Directed Acyclic Graphs (DAG) – compacted AST to avoid duplication – smaller footprint as well • Control flow graphs (CFG) – explicitly model control flow 7
  • 8. ASTs and DAGs: := a + ** b - (uni) c := a + b - (uni) b * - (uni) c c a := b *-c + b*-c AST DAG 8
  • 9. Implementation of DAG/AST: Value Number Method 9
  • 10. Three-Address Code • A three-address code is: x := y op z where x, y and z are names, constants or compiler- generated temporaries; op is any operator. • But we may also the following notation for three-address code (it looks like a machine code instruction) op y,z,x apply operator op to y and z, and store the result in x. • We use the term “three-address code” because each statement usually contains three addresses (two for operands, one for the result). 10
  • 11. Linearized Representation of DAG/AST • Source Code – a = b * -c + b * -c • Three address code • Tree Representation 11
  • 12. Three-Address Statements Binary Operator: op y,z,result or result := y op z where op is a binary arithmetic or logical operator. This binary operator is applied to y and z, and the result of the operation is stored in result. Ex: add a,b,c gt a,b,c addr a,b,c addi a,b,c Unary Operator: op y,,result or result := op y where op is a unary arithmetic or logical operator. This unary operator is applied to y, and the result of the operation is stored in result. Ex: uminus a,,c 12
  • 13. Three-Address Code • Two concepts – Address – Instruction • Address – Name: source-program names to appear as addresses – Constant: Different types of constants – Compiler Generated temporary: 13
  • 14. Three-Address Instruction Assignment Type 1: x := y op z op is a binary arithmetic or logical operation x, y and z are addresses Assignment Type 2: x := op z op is a unary arithmetic or logical operation x and z are addresses Copy Instruction:x := y x and z are addresses and x is assigned the value of y 14
  • 15. Three-Address Instructions Unconditional Jump: goto L We will jump to the three-address code with the label L, and the execution continues from that statement. Ex: goto L1 jmp 7 Conditional Jump 1: if // jump to L1 // jump to the statement 7 x goto L and if False x goto L We will jump to the three-address code with the label L if x is TRUE and FALSE, respectively. Otherwise, the following three-address instruction in sequence is executed next. Conditional Jump 2: if x relop y goto L We will jump to the three-address code with the label L if the result of y relop z is true, and the execution continues from that statement. If the result is false, the execution continues from the statement following this conditional jump statement. 15
  • 16. Three-Address Statements (cont.) Procedure Parameters: param x Procedure Calls: call p,n where x is an actual parameter, we invoke the procedure p with n parameters. 16
  • 17. Three-Address Statements (cont.) Indexed Assignments: x := y[i] sets x to the value in location i memory units beyond location y y[i] := x sets contents of the location i memory units beyond location y to the value of x Address and Pointer Assignments: x := &y sets the r-value of x to l-value of y x := *y where y is a pointer whose r-value is a location sets the r-value of x equal to the contents of that location *x := y sets the r-value of the object pointed by x to the r-value of y 17
  • 18. Three address code example do i=i+1; while (a[i] < v) L: t1=i+1 100: t1 = i +1 i=t1 101: i = t1 t2=i*8 102: t2=i*8 t3=a[t2] 103: t3=a[t2] if t3 < v goto L 104: if t3 < v goto 100 (A) Symbolic Labels (B) Position Numbers 18
  • 19. Representing 3-Address Statements op, arg1, arg2, result • x = minus y – Does not use arg2 • x = y – Op is = • param a1 – Uses neither arg2 nor result • Conditional/Unconditional jumps – Put the target label in result 19
  • 20. Quadruples • a = b * -c + b * -c
  • 21. Quadruples • Store each fields directly 21
  • 22. Triples op arg1 arg2 [ ]= x i := 0 y 0 1 22
  • 26. Synthesized Code and Place Attributes 26
  • 27. Evaluating the attributes for code generation 27
  • 28. Three-address code for expression 28
  • 29. Incremental Translation • ‘code’ attribute can be long string • Instead of building up ‘E.code’ – We arrange to generate new three-address instructions – ‘code’ attribute is not used – ‘gen’ method is used instead of ‘IR’ • ‘gen’ constructs a three address instruction and appends it to the sequence of instructions generated so far 29
  • 30. Syntax-Directed Translation into Three-Address Code S  id := E E  E1 + E2 E  E1 * E2 E  - E1 E  ( E1 ) {gen(ID.svalue ‘:=’ E.place)} {E.place := NewTemp(); gen(E.place ‘:=’ E1.place ‘+’ E2.place )} {E.place = NewTemp(); gen(E.place ‘:=’ E1.place ‘*’ E2.place ‘,’)} {E.place = NewTemp(); gen(E.place ‘:=’ ‘minus’ E1.place)} {E.place = E1.place;} E  id {E.place = ID.svalue;} 30
  • 31. Addressing Array Elements • Elements of arrays can be accessed quickly if the elements are stored in a block of consecutive locations. A one-dimensional array A: baseA low i width baseA is the address of the first location of the array A, width is the width of each array element. low is the index of the first array element … … 31
  • 32. Addressing Array Elements (cont.) baseA+(i-low)*width can be re-written as i*width + (baseA-low*width) should be computed at run-time can be computed at compile-time • So, the location of A[i] can be computed at the run-time by evaluating the formula i*width+c where c is (baseA- low*width) which is evaluated at compile-time. • Intermediate code generator should produce the code to evaluate this formula i*width+c (one multiplication and one addition operation). 32
  • 33. Two-Dimensional Arrays • A two-dimensional array can be stored in – either row-major (row-by-row) or – column-major (column-by-column). • Most of the programming languages use row-major method. • Row-major representation of a two-dimensional array: row1 row2 rown 33
  • 34. Two-Dimensional Arrays (cont.) • The location of A[i1,i2] is baseA+ ((i1-low1)*n2+i2-low2)*width baseA is the location of the array A. low1 low2 is the index of the first row is the index of the first column n2 is the number of elements in each row width is the width of each array element • Again, this formula can be re-written as ((i1*n2)+i2)*width + (baseA-((low1*n1)+low2)*width) should be computed at run-time can be computed at compile-time 34
  • 35. Multi-Dimensional Arrays • In general, the location of A[i1,i2,...,ik] is (( ... ((i1*n2)+i2) ...)*nk+ik)*width + (baseA- ((...((low1*n1)+low2)...)*nk+lowk)*width) • So, the intermediate code generator should produce the codes to evaluate the following formula (to find the location of A[i1,i2,...,ik]) : (( ... ((i1*n2)+i2) ...)*nk+ik)*width + c • To evaluate the (( ... ((i1*n2)+i2) ...)*nk+ik portion of this formula, we can use the recurrence equation: e1 = i1 em = em-1 * nm + im 35
  • 36. Translation of Array Elements • One dimensional base + i  w w: width of each array element • Two Dimensional base + i1  w1 + i2  w2 w1: width of a row w2: width of an element in a row • k dimensional (generalized) base + i1  w1 + i2  w2 + … + ik  wk 36
  • 37. Translation of Array References • Need to relate the address calculation formulas to a grammar for array references • Consider the non-terminal L to generate an array L € L [E] | id [E] • Nonterminal L has three synthesized attributes – L.addr denotes a temporary used to compute the offset for array reference ij x wj – L.array is a pointer to the symbol-table entry • L.array.base is used to determine the actual l-value of it – L.type is the type of sub-array generated by L • L.type.width gives the width of the type 37
  • 38. Syntax-Directed Translation into Three-Address Code S  id := E; | L := E; {gen(ID.svalue ‘:=’ E.place)} {gen(L.array.base ‘[’ L.addr ‘]’ ‘:=’ E.place)} E  E1 + E2 | id | L {E.place := NewTemp(); gen(E.place ‘:=’ E1.place ‘+’ E2.place )} {E.place = ID.svalue;} {E.place = NewTemp(); gen(E.place ‘:=’ L.array.base ‘[’ L.addr ‘]’);} L  id [E] | L1 [E] {L.array = ID.svalue; L.type = L.array.type.elem; L.addr = NewTemp(); gen(L.addr ‘:=’ E.place ‘*’ L.type.width)} {L.array = L1.array; L.type = L1.type.elem; t = NewTemp(); L.addr = NewTemp(); gen(t ‘:=’ E.place ‘*’ L.type.width); gen(L.addr ‘:=’ L1.addr ‘+’ t)} 38