SlideShare a Scribd company logo
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

More Related Content

PPT
Chapter 6 intermediate code generation
PPTX
Three Address code
PPT
Intermediate code generation
PPTX
Intermediate code generator
PPTX
Lexical analyzer generator lex
PPT
Intermediate code generation
PDF
Dataflow Analysis
PPTX
Phases of Compiler
Chapter 6 intermediate code generation
Three Address code
Intermediate code generation
Intermediate code generator
Lexical analyzer generator lex
Intermediate code generation
Dataflow Analysis
Phases of Compiler

What's hot (20)

PPT
Lexical Analysis
PDF
Intermediate code generation
PPTX
Bootstrapping in Compiler
PPT
Intermediate code generation (Compiler Design)
PDF
Symbol table in compiler Design
PPTX
Phases of compiler
PPTX
Basic blocks - compiler design
PPT
Symbol table management and error handling in compiler design
PPT
phases of a compiler
PPT
Lecture 1 - Lexical Analysis.ppt
PPTX
System Programming Overview
PPT
Message authentication and hash function
PPT
1.Role lexical Analyzer
PPTX
Code optimization
PPTX
Lexical Analysis - Compiler Design
PPTX
Recognition-of-tokens
PPT
Top down parsing
PPT
Language translator
PPTX
Types of Compilers
PPTX
compiler ppt on symbol table
Lexical Analysis
Intermediate code generation
Bootstrapping in Compiler
Intermediate code generation (Compiler Design)
Symbol table in compiler Design
Phases of compiler
Basic blocks - compiler design
Symbol table management and error handling in compiler design
phases of a compiler
Lecture 1 - Lexical Analysis.ppt
System Programming Overview
Message authentication and hash function
1.Role lexical Analyzer
Code optimization
Lexical Analysis - Compiler Design
Recognition-of-tokens
Top down parsing
Language translator
Types of Compilers
compiler ppt on symbol table
Ad

Similar to Lecture 12 intermediate code generation (20)

PDF
Intermediate code generation in Compiler Design
PPTX
Intermediate code generation1
PPT
Chapter 6 Intermediate Code Generation
PPTX
Three address code In Compiler Design
PPTX
Compiler Design_Intermediate code generation new ppt.pptx
PPT
458237.-Compiler-Design-Intermediate-code-generation.ppt
PDF
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
PPTX
Compiler Design_Code generation techniques.pptx
DOC
Compiler notes--unit-iii
PPTX
Syntax directed definition and intermediate code generation
PPTX
UNIT - III Compiler.pptx power point presentation
PDF
Project presentation PPT.pdf this is help for student who doing this complier...
PDF
Chapter 11 - Intermediate Code Generation.pdf
PPT
Code generator
PPT
Chapter Eight(2)
PPTX
Assignment statements
PDF
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
PPT
Interm codegen
PDF
Presentation1.pdf
Intermediate code generation in Compiler Design
Intermediate code generation1
Chapter 6 Intermediate Code Generation
Three address code In Compiler Design
Compiler Design_Intermediate code generation new ppt.pptx
458237.-Compiler-Design-Intermediate-code-generation.ppt
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
Compiler Design_Code generation techniques.pptx
Compiler notes--unit-iii
Syntax directed definition and intermediate code generation
UNIT - III Compiler.pptx power point presentation
Project presentation PPT.pdf this is help for student who doing this complier...
Chapter 11 - Intermediate Code Generation.pdf
Code generator
Chapter Eight(2)
Assignment statements
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
Interm codegen
Presentation1.pdf
Ad

More from Iffat Anjum (20)

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

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
RMMM.pdf make it easy to upload and study
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Complications of Minimal Access Surgery at WLH
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Classroom Observation Tools for Teachers
PPTX
master seminar digital applications in india
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Anesthesia in Laparoscopic Surgery in India
RMMM.pdf make it easy to upload and study
O5-L3 Freight Transport Ops (International) V1.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
GDM (1) (1).pptx small presentation for students
Classroom Observation Tools for Teachers
master seminar digital applications in india
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
O7-L3 Supply Chain Operations - ICLT Program
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Presentation on HIE in infants and its manifestations
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
01-Introduction-to-Information-Management.pdf
Institutional Correction lecture only . . .
Abdominal Access Techniques with Prof. Dr. R K Mishra

Lecture 12 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