Intermediate Formats
Transcript of Intermediate Formats
![Page 1: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/1.jpg)
Intermediate Formats
for object oriented languages
![Page 2: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/2.jpg)
Program Representation Goals
• Enable Program Analysis and Transformation – Semantic Checks, Correctness Checks, Optimizations
• Structure Translation to Machine Code – Sequence of Steps
Parse Tree
High Level Intermediate Representation
Low Level Intermediate Representation
Machine Code
Semantic Analysis
![Page 3: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/3.jpg)
High Level IR • Preserves Object Structure • Preserves Structured Flow of Control • Primary Goal: Analyze Program
Low Level IR • Moves Data Model to Flat Address Space • Eliminates Structured Control Flow • Suitable for Low Level Compilation Tasks
– Register Allocation – Instruction Selection
![Page 4: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/4.jpg)
Examples of Object Representation and Program Execution
(This happens when program runs)
![Page 5: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/5.jpg)
Example Vector Class class vector {
int v[ ]; …
void add(int x) { int i; i = 0; while (i < v.length) { v[i] = v[i]+x; i = i+1; } }
}
![Page 6: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/6.jpg)
• Items Stored Contiguously In Memory • Length Stored In First Word
• Color Code – Red - generated by compiler automatically – Blue, Yellow, Lavender - program data or code – Magenta - executing code or data
Representing Arrays
3 7 4 8
![Page 7: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/7.jpg)
Representing Vector Objects
• First Word Points to Class Information – Method Table, Garbage Collector Data
• Next Words Have Object Fields – For vectors, Next Word is Reference to Array
Class Info
3 7 4 8
![Page 8: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/8.jpg)
Invoking Vector Add Method
vect.add(1); • Create Activation Record
3 7 4 8
Class Info
![Page 9: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/9.jpg)
Invoking Vector Add Method
vect.add(1); • Create Activation Record
– this onto stack this
3 7 4 8
Class Info
![Page 10: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/10.jpg)
Invoking Vector Add Method
vect.add(1); • Create Activation Record
– this onto stack – parameters onto stack
1 x this
3 7 4 8
Class Info
![Page 11: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/11.jpg)
Invoking Vector Add Method
vect.add(1); • Create Activation Record
– this onto stack – parameters onto stack – space for locals on stack
1 x i
this
3 7 4 8
Class Info
![Page 12: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/12.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 x i
this
3 7 4 8
Class Info
![Page 13: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/13.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 7 4 8
Class Info
![Page 14: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/14.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 7 4 8
Class Info
![Page 15: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/15.jpg)
void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 7 4 8
Executing Vector Add Method
Class Info
![Page 16: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/16.jpg)
void add(int x) { int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 7 4 8
Executing Vector Add Method
Class Info
![Page 17: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/17.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 7 4 8
Class Info
![Page 18: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/18.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 7 4 8
Class Info
![Page 19: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/19.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 7 4 8
Class Info
![Page 20: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/20.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 7 4 8
Class Info
![Page 21: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/21.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 7 4 8
Class Info
![Page 22: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/22.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 8 4 8
Class Info
![Page 23: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/23.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 0
x i
this
3 8 4 8
Class Info
![Page 24: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/24.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 1
x i
this
3 8 4 8
Class Info
![Page 25: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/25.jpg)
Executing Vector Add Method void add(int x) {
int i; i = 0; while (i < v.length) v[i] = v[i]+x; i = i+1; }
1 3
x i
this
3 8 5 9
Class Info
![Page 26: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/26.jpg)
What does the compiler have to do to make all of this work?
![Page 27: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/27.jpg)
Compilation Tasks
• Determine Format of Objects and Arrays • Determine Format of Call Stack • Generate Code to Read Values
– this, parameters, locals, array elements, object fields • Generate Code to Evaluate Expressions • Generate Code to Write Values • Generate Code for Control Constructs
![Page 28: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/28.jpg)
Compilation Tasks
• Determine Object Format in Memory – Fields from Parent Classes – Fields from Current Class
• Generate Code for Methods – Field, Local Variable and Parameter Accesses – Method Invocations
![Page 29: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/29.jpg)
Symbol Tables - Key Concept in Compilation
• Compiler Uses Symbol Tables to Produce – Object Layout in Memory – Code to
• Access Object Fields • Access Local Variables • Access Parameters • Invoke Methods
![Page 30: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/30.jpg)
Symbol Tables During Translation From Parse Tree to IR
• Symbol Tables Map Identifiers (strings) to Descriptors (information about identifiers)
• Basic Operation: Lookup – Given A String, find Descriptor – Typical Implementation: Hash Table
• Examples – Given a class name, find class descriptor – Given variable name, find descriptor
• local descriptor, parameter descriptor, field descriptor
![Page 31: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/31.jpg)
Hierarchy In Symbol Tables
• Hierarchy Comes From – Nested Scopes - Local Scope Inside Field Scope – Inheritance - Child Class Inside Parent Class
• Symbol Table Hierarchy Reflects These Hierarchies
• Lookup Proceeds Up Hierarchy Until Descriptor is Found
![Page 32: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/32.jpg)
Hierarchy in vector add Method
i descriptor for local i
x descriptor for parameter x
v descriptor for field v
Symbol Table for Fields of vector Class
Symbol Table for Parameters of add
Symbol Table for Locals of add
this descriptor for this
![Page 33: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/33.jpg)
Lookup In vector Example • v[i] = v[i]+x;
i descriptor for local i
x descriptor for parameter x
v descriptor for field v
this descriptor for this
![Page 34: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/34.jpg)
Lookup i In vector Example • v[i] = v[i]+x;
i descriptor for local i
x descriptor for parameter x
v descriptor for field v
this descriptor for this
![Page 35: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/35.jpg)
Lookup i In vector Example • v[i] = v[i]+x;
i descriptor for local i
x descriptor for parameter x
v descriptor for field v
this descriptor for this
![Page 36: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/36.jpg)
Lookup x In vector Example • v[i] = v[i]+x;
i descriptor for local i
x descriptor for parameter x
v descriptor for field v
this descriptor for this
![Page 37: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/37.jpg)
Lookup x In vector Example • v[i] = v[i]+x;
i descriptor for local i
x descriptor for parameter x
v descriptor for field v
this descriptor for this
![Page 38: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/38.jpg)
Lookup x In vector Example • v[i] = v[i]+x;
i descriptor for local i
x descriptor for parameter x
v descriptor for field v
this descriptor for this
![Page 39: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/39.jpg)
Descriptors
• What do descriptors contain? • Information used for code generation and
semantic analysis – local descriptors - name, type, stack offset – field descriptors - name, type, object offset – method descriptors
• signature (type of return value, receiver, and parameters) • reference to local symbol table • reference to code for method
![Page 40: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/40.jpg)
Program Symbol Table
• Maps class names to class descriptors • Typical Implementation: Hash Table
vector point
cartesianPoint polarPoint
class descriptor for vector class descriptor for point class descriptor for cartesianPoint class descriptor for polarPoint
![Page 41: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/41.jpg)
Class Descriptor
• Has Two Symbol Tables – Symbol Table for Methods
• Parent Symbol Table is Symbol Table for Methods of Parent Class
– Symbol Table for Fields • Parent Symbol Table is Symbol Table for Fields of
Parent Class
• Reference to Descriptor of Parent Class
![Page 42: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/42.jpg)
Field, Parameter and Local and Type Descriptors
• Field, Parameter and Local Descriptors Refer to Type Descriptors – Base type descriptor: int, boolean – Array type descriptor, which contains reference to
type descriptor for array elements – Class descriptor
• Relatively Simple Type Descriptors • Base Type Descriptors and Array Descriptors
Stored in Type Symbol Table
![Page 43: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/43.jpg)
Example Type Symbol Table
boolean boolean descriptor
int int descriptor int [] array descriptor
boolean [] array descriptor vector [] array descriptor class descriptor
for vector vector
![Page 44: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/44.jpg)
Method Descriptors
• Contain Reference to Code for Method • Contain Reference to Local Symbol Table for
Local Variables of Method • Parent Symbol Table of Local Symbol Table is
Parameter Symbol Table for Parameters of Method
![Page 45: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/45.jpg)
Method Descriptor for add Method
this
i
this descriptor
local descriptor
field symbol table for vector class
local variable symbol table
x
parameter symbol table
parameter descriptor
code for add method
Method descriptor for add
![Page 46: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/46.jpg)
Symbol Table Summary • Program Symbol Table (Class Descriptors) • Class Descriptors
– Field Symbol Table (Field Descriptors) • Field Symbol Table for SuperClass
– Method Symbol Table (Method Descriptors) • Method Symbol Table for Superclass
• Method Descriptors – Local Variable Symbol Table (Local Variable Descriptors)
• Parameter Symbol Table (Parameter Descriptors) – Field Symbol Table of Receiver Class
• Local, Parameter and Field Descriptors – Type Descriptors in Type Symbol Table or Class Descriptors
![Page 47: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/47.jpg)
class_decl
vector field_decl
int v []
class descriptor for vector
![Page 48: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/48.jpg)
v field descriptor
class_decl
vector field_decl
int v []
class descriptor for vector
field symbol table
![Page 49: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/49.jpg)
v field descriptor
this this descriptor x parameter descriptor
class_decl
vector field_decl
int v []
class descriptor for vector
parameter symbol table
field symbol table
![Page 50: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/50.jpg)
v field descriptor
this
i
this descriptor
local descriptor
x parameter descriptor
class_decl
vector field_decl
int v []
class descriptor for vector
local symbol table
parameter symbol table
field symbol table
![Page 51: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/51.jpg)
v field descriptor
add this
i
this descriptor
local descriptor
x parameter descriptor
code for add method
class_decl
vector field_decl
int v []
class descriptor for vector
method descriptor for add local symbol table
parameter symbol table
field symbol table
method symbol table
![Page 52: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/52.jpg)
v field descriptor
add this
i
this descriptor
local descriptor
x parameter descriptor
code for add method
boolean boolean descriptor
int int descriptor int [] array descriptor
boolean [] array descriptor vector [] array descriptor
class_decl
vector field_decl
int v []
class descriptor for vector
method descriptor for add local symbol table
parameter symbol table
field symbol table
method symbol table
![Page 53: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/53.jpg)
v field descriptor
add this
i
this descriptor
local descriptor
x parameter descriptor
code for add method
boolean boolean descriptor
int int descriptor int [] array descriptor
boolean [] array descriptor vector [] array descriptor
class_decl
vector field_decl
int v []
class descriptor for vector
method descriptor for add
type symbol table
local symbol table
parameter symbol table
field symbol table
method symbol table
![Page 54: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/54.jpg)
Representing Code in High-Level Intermediate Representation
![Page 55: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/55.jpg)
Basic Idea
• Move towards assembly language • Preserve high-level structure
– object format – structured control flow – distinction between parameters, locals and fields
• High-level abstractions of assembly language – load and store nodes – access abstract locals, parameters and fields, not
memory locations directly
![Page 56: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/56.jpg)
What is a Parse Tree?
• Parse Tree Records Results of Parse • External nodes are terminals/tokens • Internal nodes are non-terminals class_decl::=‘class’ name ‘{’field_decl method_decl‘}’ field_decl::= ‘int’ name ‘[];’
method_decl::= ‘void’ name ‘(’ param_decl ‘) ’ ‘{‘ var_decl stats ‘}’
![Page 57: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/57.jpg)
Abstract Versus Concrete Trees
• Remember grammar hacks – left factoring, ambuguity elimination, precedence of
binary operators • Hacks lead to a tree that may not reflect
cleanest interpretation of program • May be more convenient to work with abstract
syntax tree (roughly, parse tree from grammar before hacks)
![Page 58: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/58.jpg)
Building IR Alternatives
• Build concrete parse tree in parser, translate to abstract syntax tree, translate to IR
• Build abstract syntax tree in parser, translate to IR
• Roll IR construction into parsing
![Page 59: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/59.jpg)
FromAbstract Syntax Trees to Symbol Tables
• Recursively Traverse Tree • Build Up Symbol Tables As Traversal Visits
Nodes
![Page 60: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/60.jpg)
Representing Expressions • Expression Trees Represent Expressions
– Internal Nodes - Operations like +, -, etc. – Leaves - Load Nodes Represent Variable Accesses
• Load Nodes – ldf node for field accesses - field descriptor
• (implicitly accesses this - could add a reference to accessed object)
– ldl node for local variable accesses - local descriptor – ldp node for parameter accesses - parameter descriptor – lda node for array accesses
• expression tree for array • expression tree for index
![Page 61: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/61.jpg)
Example
x*x + y*y +
ldf
field descriptor for x in field symbol table for cartesianPoint class
*
ldf ldf
*
ldf
field descriptor for y in field symbol table for cartesianPoint class
![Page 62: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/62.jpg)
Example
v[i]+x
lda
+
ldp
field descriptor for v in field symbol table for vector class
local descriptor for i in local symbol table of vector add
parameter descriptor for x in parameter symbol table of vector add
ldl ldf
![Page 63: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/63.jpg)
Special Case: Array Length Operator
• len node represents length of array – expression tree for array
• Example: v.length len
field descriptor for v in field symbol table for vector class
ldf
![Page 64: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/64.jpg)
Representing Assignment Statements
• Store Nodes – stf for stores to fields
• field descriptor • expression tree for stored value
– stl for stores to local variables • local descriptor • expression tree for stored value
– sta for stores to array elements • expression tree for array • expression tree for index • expression tree for stored value
![Page 65: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/65.jpg)
Representing Procedure Calls
• Call statement • Refers to method descriptor for invoked method • Has list of parameters (this is first parameter)
vect.add(1) call
1
method descriptor for add in method symbol table for vector class
ldl
local descriptor for vect in local symbol table of method containing the call statement vect.add(1)
constant
![Page 66: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/66.jpg)
Example v[i]=v[i]+x
lda
+
ldp
field descriptor for v in field symbol table for vector class local descriptor
for i in local symbol table of vector add
parameter descriptor for x in parameter symbol table of vector add
ldl
sta
ldl ldf
![Page 67: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/67.jpg)
Representing Flow of Control • Statement Nodes
– sequence node - first statement, next statement – if node
• expression tree for condition • then statement node and else statement node
– while node • expression tree for condition • statement node for loop body
– return node • expression tree for return value
![Page 68: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/68.jpg)
Example
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
while
<
len lda
+
ldp
ldl
sta
ldl
ldf
ldf
ldf
![Page 69: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/69.jpg)
Translating from Abstract Syntax Trees to Symbol Tables
![Page 70: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/70.jpg)
Example Abstract Syntax Tree
class_decl
vector field_decl
int v
method_decl
add
int x
param_decl var_decl
int i
statements
class vector { int v[]; void add(int x) { int i; i = 0; while (i < v.length) { v[i] = v[i]+x; i = i+1; } }
}
![Page 71: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/71.jpg)
class_decl
vector field_decl
int v
method_decl
add
int x
param_decl var_decl
int i
statements
class symbol table
![Page 72: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/72.jpg)
class descriptor for vector
class_decl
vector field_decl
int v
method_decl
add
int x
param_decl var_decl
int i
statements
class symbol table
vector
![Page 73: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/73.jpg)
v field descriptor
class descriptor for vector
class_decl
vector field_decl
int v
method_decl
add
int x
param_decl var_decl
int i
statements
class symbol table
vector
![Page 74: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/74.jpg)
v field descriptor
add class descriptor for vector
this this descriptor
Method descriptor for add
class_decl
vector field_decl
int v
method_decl
add
int x
param_decl var_decl
int i
statements
class symbol table
vector
![Page 75: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/75.jpg)
v field descriptor
add class descriptor for vector
this this descriptor x parameter descriptor
Method descriptor for add
class_decl
vector field_decl
int v
method_decl
add
int x
param_decl var_decl
int i
statements
class symbol table
vector
![Page 76: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/76.jpg)
v field descriptor
add class descriptor for vector
this
i
this descriptor
local descriptor
x parameter descriptor
Method descriptor for add
class_decl
vector field_decl
int v
method_decl
add
int x
param_decl var_decl
int i
statements
class symbol table
vector
![Page 77: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/77.jpg)
From Abstract Syntax Trees to Intermediate Representation
![Page 78: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/78.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i field descriptor for v parameter descriptor for x
![Page 79: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/79.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
![Page 80: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/80.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
ldf
![Page 81: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/81.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
len
ldf
![Page 82: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/82.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
<
len
ldf
![Page 83: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/83.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
<
len
ldf ldf
![Page 84: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/84.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
<
len
ldl ldf ldf
![Page 85: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/85.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
<
len lda
ldl ldf ldf
![Page 86: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/86.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
<
len lda ldp
ldl ldf ldf
![Page 87: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/87.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
<
len lda
+
ldp
ldl ldf ldf
![Page 88: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/88.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
<
len lda
+
ldp
ldl ldf
ldf
ldf
![Page 89: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/89.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
<
len lda
+
ldp
ldl
ldl
ldf
ldf
ldf
![Page 90: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/90.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
<
len lda
+
ldp
ldl
sta
ldl
ldf
ldf
ldf
![Page 91: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/91.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
while
<
len lda
+
ldp
ldl
sta
ldl
ldf
ldf
ldf
![Page 92: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/92.jpg)
while (i < v.length) v[i] = v[i]+x;
local descriptor for i
ldl
field descriptor for v parameter descriptor for x
while
<
len lda
+
ldp
ldl
sta
ldl
ldf
ldf
ldf
![Page 93: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/93.jpg)
Abbreviated Notation
while (i < v.length) v[i] = v[i]+x;
ldl i
while
<
len lda
+
ldp x
ldl i
sta
ldl i
ldf v
ldf v
ldf v
![Page 94: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/94.jpg)
From Abstract Syntax Trees to IR
• Recursively Traverse Abstract Syntax Tree • Build Up Representation Bottom-Up Manner
– Look Up Variable Identifiers in Symbol Tables – Build Load Nodes to Access Variables – Build Expressions Out of Load Nodes and Operator
Nodes – Build Store Nodes for Assignment Statements – Combine Store Nodes with Flow of Control Nodes
![Page 95: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/95.jpg)
Summary High-Level Intermediate Representation
• Goal: represent program in an intuitive way that supports future compilation tasks
• Representing program data – Symbol tables – Hierarchical organization
• Representing computation – Expression trees – Various types of load and store nodes – Structured flow of control
• Traverse abstract syntax tree to build IR
![Page 96: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/96.jpg)
v field descriptor
add class descriptor for vector
this
i
this descriptor
local descriptor
x parameter descriptor
Method descriptor for add
class_decl
vector field_decl
int v
method_decl
add
int x
param_decl var_decl
int i
statements
code for add method
class symbol table
vector
![Page 97: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/97.jpg)
Eliminating Parse Tree Construction
• Parser actions build symbol tables – Reduce actions build tables in bottom-up fashion – Actions correspond to activities that take place in
top-down fashion in parse tree traversal • Eliminates intermediate construction of parse
tree - improves performance • Also less code to write (but code may be harder
to write than if just traverse parse tree)
![Page 98: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/98.jpg)
class vector { int v[]; void add(int x) { int i; ... }}
class symbol table
![Page 99: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/99.jpg)
field_decl
int v
field descriptor
class vector { int v[]; void add(int x) { int i; ... }}
class symbol table
![Page 100: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/100.jpg)
int x
param_decl
field_decl
int v
field descriptor
class vector { int v[]; void add(int x) { int i; ... }}
parameter descriptor
class symbol table
![Page 101: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/101.jpg)
int x
param_decl
field_decl
int v
field descriptor
class vector { int v[]; void add(int x) { int i; ... }}
parameter descriptor
var_decl
int i
local descriptor
class symbol table
![Page 102: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/102.jpg)
int x
param_decl
field_decl
int v
field descriptor
class vector { int v[]; void add(int x) { int i; ... }}
parameter descriptor
var_decl
int i
local descriptor
statements
code for add method
class symbol table
![Page 103: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/103.jpg)
int x
param_decl
field_decl
int v
field descriptor
class vector { int v[]; void add(int x) { int i; ... }}
parameter descriptor
var_decl
int i
local descriptor
statements
code for add method
this this descriptor x
i Method descriptor for add
method_decl
add class symbol
table
![Page 104: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/104.jpg)
int x
param_decl
field_decl
int v
field descriptor
class vector { int v[]; void add(int x) { int i; ... }}
parameter descriptor
var_decl
int i
local descriptor
statements
code for add method
this this descriptor x
i Method descriptor for add
method_decl
add class symbol
table
add class descriptor for vector
v
![Page 105: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/105.jpg)
Nested Scopes
• So far, have seen several kinds of nesting – Method symbol tables nested inside class symbol
tables – Local symbol tables nesting inside method symbol
tables • Nesting disambiguates potential name clashes
– Same name used for class field and local variable – Name refers to local variable inside method
![Page 106: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/106.jpg)
Nested Code Scopes
• Symbol tables can be nested arbitrarily deeply with code nesting: class bar { baz x;
int foo(int x) { double x = 5.0; { float x = 10.0;
{ int x = 1; ... x ...} ... x ...
} ... x ...
}
Note: Name clashes with nesting can reflect programming error. Compilers often generate warning messages if it occurs.
![Page 107: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/107.jpg)
Traversing Class Declarations • Extract Class Name and Superclass Name • Create Class Descriptor (field and method symbol
tables), Put Descriptor Into Class Symbol Table • Put Array Descriptor Into Type Symbol Table • Lookup Superclass Name in Class Symbol Table,
Make Superclass Link in Class Descriptor Point to Retrieved Class Descriptor
• Traverse Field Declarations to Fill Up Field Symbol Table
• Traverse Method Declarations to Fill Up Method Symbol Table
![Page 108: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/108.jpg)
Further Complication - Inheritance
Object Extension
![Page 109: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/109.jpg)
Inheritance Example - Point Class
class point { int c; int getColor() { return(c); } int distance() { return(0); }
}
![Page 110: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/110.jpg)
Point Subclasses class cartesianPoint extends point{
int x, y; int distance() { return(x*x + y*y); }
} class polarPoint extends point {
int r, t; int distance() { return(r*r); } int angle() { return(t); }
}
![Page 111: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/111.jpg)
Implementing Object Fields
• Each object is a contiguous piece of memory • Fields from inheritance hierarchy allocated
sequentially in piece of memory • Example: polarPoint object
Class Info polarPoint 2
1 2
c r t
![Page 112: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/112.jpg)
Point Objects
1
4
2
1 3
4 6
c
c x y
c r t
Class Info point
Class Info cartesianPoint
Class Info polarPoint
![Page 113: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/113.jpg)
Class Descriptors for point and cartesianPoint
c field descriptor for c
getColor method descriptor for getColor
class descriptor for point
y x field descriptor for x
field descriptor for y
distance method descriptor for distance
class descriptor for cartesianPoint
method descriptor for distance
distance
![Page 114: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/114.jpg)
Dynamic Dispatch if (x == 0) {
p = new point(); } else if (x < 0) {
p = new cartesianPoint(); } else if (x > 0) {
p = new polarPoint(); } y = p.distance();
Which distance method is invoked?
• if p is a point return(0)
• if p is a cartesianPoint return(x*x + y*y)
• if p is a polarPoint return(r*r)
• Invoked Method Depends on Type of Receiver!
![Page 115: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/115.jpg)
Implementing Dynamic Dispatch
• Basic Mechanism: Method Table
getColor method for point distance method for cartesianPoint
getColor method for point distance method for polarPoint
getColor method for point distance method for point
method table for point objects
method table for cartesianPoint objects
method table for polarPoint objects
angle method for polarPoint
![Page 116: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/116.jpg)
Invoking Methods
• Compiler Numbers Methods In Each Inheritance Hierarchy – getColor is Method 0, distance is Method 1,
angle is Method 2 • Method Invocation Sites Access Corresponding
Entry in Method Table • Works For Single Inheritance Only
– not for multiple inheritance, multiple dispatch, or interfaces
![Page 117: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/117.jpg)
Hierarchy in Method Symbol Tables for Points
distance
method descriptor for distance
getColor method descriptor for getColor method descriptor for distance
distance
distance method descriptor for distance method descriptor for angle
angle
![Page 118: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/118.jpg)
Lookup In Method Symbol Tables
• Starts with method table of declared class of receiver object
• Goes up class hierarchy until method found – point p; p = new point(); p.distance();
• finds distance in point method symbol table – point p; p = new cartesianPoint(); p.distance();
• finds distance in point method symbol table – cartesianPoint p; p = new cartesianPoint();
p.getColor(); • finds getColor in point method symbol table
![Page 119: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/119.jpg)
Static Versus Dynamic Lookup
• Static lookup done at compile time for type checking and code generation
• Dynamic lookup done when program runs to dispatch method call
• Static and dynamic lookup results may differ! – point p; p = new cartesianPoint(); p.distance();
• Static lookup finds distance in point method table • Dynamic lookup invokes distance in cartesianPoint class • Dynamic dispatch mechanism used to make this happen
![Page 120: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/120.jpg)
Static and Dynamic Tables • Static Method Symbol Table
– Used to look up method definitions at compile time – Index is method name – Lookup starts at method symbol table determined
by declared type of receiver object – Lookup may traverse multiple symbol tables
• Dynamic Method Table – Used to look up method to invoke at run time – Index is method number – Lookup simply accesses a single table element
![Page 121: Intermediate Formats](https://reader030.fdocuments.in/reader030/viewer/2022012413/616d78a2259a447aa5351961/html5/thumbnails/121.jpg)
getColor method for point
distance method for cartesianPoint
getColor method for point distance method for polarPoint
getColor method for point
distance method for point
angle method for polarPoint
distance
method descriptor for distance
getColor method descriptor for getColor
method descriptor for distance
distance
distance
method descriptor for distance
method descriptor for angle
angle