Upload
sabina-matthews
View
216
Download
0
Embed Size (px)
DESCRIPTION
Code Generation (Chapter 7) 3 Static Storage Allocation in the Code Generator public abstract class AST { public RuntimeEntity entity; // mostly used for Decls... } public abstract class AST { public RuntimeEntity entity; // mostly used for Decls... } Entity Descriptions:
Citation preview
1Code Generation (Chapter 7)
Course Overview
PART I: overview material1 Introduction2 Language processors (tombstone diagrams, bootstrapping)3 Architecture of a compiler
PART II: inside a compiler4 Syntax analysis 5 Contextual analysis6 Runtime organization7 Code generation
PART III: conclusion8 Interpretation9 Review
2Code Generation (Chapter 7)
Static Storage Allocation in the Code Generator
public abstract class RuntimeEntity {public short size;...
}public class KnownValue extends RuntimeEntity {
public short value;...
}public class UnknownValue extends RuntimeEntity {
public short address;...
}public class KnownAddress extends RuntimeEntity {
public short address;...
}
Entitity Descriptions:
3Code Generation (Chapter 7)
Static Storage Allocation in the Code Generator
public abstract class AST {public RuntimeEntity entity; // mostly used for Decls...
}
Entity Descriptions:
4Code Generation (Chapter 7)
Static Storage Allocation in the Code Generatorpublic Object visit...Command( ...Command com, Object arg) { short gs = shortValueOf(arg); // global storage in use generate code as specified by execute[com] return null;}public Object visit...Expression( ...Expression expr, Object arg) { short gs = shortValueOf(arg); // global storage in use generate code as specified by evaluate[expr] return new Short(size of expr result);}public Object visit...Declaration( ...Declaration dec, Object arg) { short gs = shortValueOf(arg); // global storage in use generate code as specified by elaborate[dec] return new Short(amount of memory allocated by dec);}
5Code Generation (Chapter 7)
Static Storage Allocation in the Code Generator
public void encode(Program prog) { prog.visit(this, new Short(0));}
Amount of global storage previously allocated is initially = 0
The visitor is started …
6Code Generation (Chapter 7)
Static Storage Allocation in the Code Generator
public Object visitVarDeclaration( VarDeclaration decl, Object arg) { short gs = shortValueOf(arg); short s = shortValueOf(decl.T.visit(this, null)); decl.entity = new KnownAddress(s, gs); emit(Instruction.PUSHop, 0, 0, s); return new Short(s);}
Some specific examples of visitor methods
elaborate [var I : T] =PUSH s where s = size of T
Remember the size and address of the variable
7Code Generation (Chapter 7)
Static Storage Allocation in the Code Generator
public Object visitSequentialDeclaration( SequentialDeclaration decl, Object arg) { short gs = shortValueOf(arg); short s1 = shortValueOf(decl.D1.visit(this,arg)); short s2 = shortValueOf(decl.D2.visit(this,
new Short(gs+s1))); return new Short(s1+s2);}
elaborate [D1 ; D2] =elaborate [D1]elaborate [D2]
8Code Generation (Chapter 7)
Static Storage Allocation in the Code Generator
public Object visitLetCommand(LetCommand com, Object arg) {
short gs = shortValueOf(arg); short s = shortValueOf(com.D.visit(this,arg)); com.C.visit(this,new Short(gs+s)); if (s > 0) emit(Instruction.POPop, 0, 0, s); return null;}
execute [let D in C] =elaborate[D]execute [C]POP(0) s if s > 0
where s = amount of storage allocated by D
9Code Generation (Chapter 7)
Static Storage Allocation in the Code Generator
public void encodeFetch(Vname name, short s) { RuntimeEntity ent = VName.I.decl.entity; if (ent instanceof KnownValue) { short v = ((KnownValue)ent).value; emit(Instruction.LOADLop, 0, 0, v); } else { short d = (ent instanceof UnknownValue) ?
((UnknownValue)ent).address :((KnownAddress)ent).address ;
emit(Instruction.LOADop, 0, Instruction.SBr, d); }}
fetch [I] = special case if I is a known literal constant LOADL v where v is the known value of I
fetch [V] = LOAD d[SB] where d = address of V relative
to SB
10Code Generation (Chapter 7)
Stack Allocation, Procedures and Functions
So far we have seen how code generation works for Mini Triangle.
Next we will extend code generation to handle remainder of Triangle:
1) How procedures and functions are compiled.
2) How to modify the code generator to compute addresses when we use a stack allocation model (instead of static allocation).
11Code Generation (Chapter 7)
Recap: TAM Frame Layout Summary
LB
ST
local variablesand intermediate
results
dynamic linkstatic link
return address
Local data (grows and shrinksduring execution)
Link data
arguments Arguments for current procedure(these were put here by the caller)
12Code Generation (Chapter 7)
Recap: Accessing global/local variables
Example: Compute the addresses of the variables in this program
let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y( ) ~ let var d: Integer; var e: ... in ... ; proc Z( ) ~ let var f: Integer; in begin ...; Y( ); ... endin begin ...; Y( ); ...; Z( ); end
Var Size Addressabc
de
f
311
[0]SB[3]SB[4]SB
1?
1
[3]LB[4]LB
[3]LB
13Code Generation (Chapter 7)
Recap: TAM addressing schemas overview
We now have a complete picture of the different kinds of addresses that are used for accessing variables and formal parameters stored on the stack.
Type of variable
Global
Local
Parameter
Non-local, 1 level up
Non-local, 2 levels up
...
Load instruction
LOAD +offset[SB]
LOAD +offset[LB]
LOAD -offset[LB]
LOAD +offset[L1]
LOAD +offset[L2]
14Code Generation (Chapter 7)
How To Characterize Addresses now?
When we have a static allocation model only, an address can be characterized by a single positive integer (i.e. its offset from SB).
Now we generalize this to stack allocation (for nested procedures).
Q: How should we characterize an address for a variable/constant now?
A: Each address can be characterized by two numbers:- offset (similar to static allocation)- nesting level
Q: How do we compute the addresses to use in an instruction that loads a value from a variable, or stores a value to a variable?
15Code Generation (Chapter 7)
How To Characterize Addresses of Vars/ConstantsExample: Compute the addresses of the variables in this program
let var a: array 3 of Integer; var b: Boolean; proc foo( ) ~ let var d: array 2 of Integer; var e: ... proc bar( ) ~ let var f: Integer; in ...bar body... in ...foo body... ;in ... global code ...
Var Size Addr Accessing31
2?
1
(0, 0)(3, 0)
(3, 1)(5, 1)
(3, 2)
0[SB]3[SB]
3[LB]
?
How to access these depends on … where are you accessing from!accessing e from foo body =>accessing e from bar body =>
5[LB]5[L1]
ab
de
f
16Code Generation (Chapter 7)
New Fetch and Assign Code Templates
fetch [I] =LOAD(s) d[r]
s = size of the type of I d from address of I is (d, l)r determined by l and cl (current level)
How to determine r:
l = 0 ==> r = SBl = cl ==> r = LBotherwise ==> r = L(cl-l)
17Code Generation (Chapter 7)
How To Modify the Code Generator
public class Frame { public byte level; public short size;}
An info structure to pass as argument in the visitor (instead of “gs”)
18Code Generation (Chapter 7)
How To Modify the Code Generator
public class EntityAddress { public byte level; public short displacement;}
Different kind of “address” in entity descriptors
public class UnknownValue extends RuntimeEntity {public EntityAddress address;...
}public class KnownAddress extends RuntimeEntity {
public EntityAddress address;...
}
19Code Generation (Chapter 7)
How To Modify the Code Generator
Changes to code generator (visitor)
Example:
public Object visitVarDeclaration( VarDeclaration decl, Object arg) { Frame frame = (Frame)arg; short s = shortValueOf(decl.T.visit(this,null)); decl.entity = new KnownAddress(s,frame); emit(Instruction.PUSHop, 0, 0, s); return new Short(s);}
Q: When will the level of a frame be changed? Q: When will the size of a frame be changed?
20Code Generation (Chapter 7)
Procedures and Functions
We extend Mini Triangle with procedures:
Declaration ::= ... | proc Identifier ( ) ~ CommandCommand ::= ... | Identifier ( )
First , we will only consider global procedures (with no arguments).
21Code Generation (Chapter 7)
Code Template: Global Procedure
elaborate [proc I ( ) ~ C] =JUMP g
e: execute [C]RETURN(0) 0
g:
C
execute [I ( )] =CALL(SB) e
22Code Generation (Chapter 7)
Code Template: Global Procedure
Example: let var n: Integer; proc double( ) ~ n := n*2in begin n := 9; double( ) end
0: PUSH 11: JUMP 72: LOAD 0[SB]3: LOADL 24: CALL mult5: STORE 0[SB]6: RETURN(0) 07: LOADL 98: STORE 0[SB]9: CALL(SB) 210:POP(0) 111:HALT
n := n*2
var n: Integer
proc double( ) ~ n := n*2
n := 9double( )
23Code Generation (Chapter 7)
Procedures and Functions
We extend Mini Triangle with functions:
Declaration ::= ... | func Identifier ( ) : TypeDenoter ~ ExpressionExpression ::= ... | Identifier ( )
First , we will only consider global functions (with no arguments).
This works nearly the same as procedures (except for the RETURN).
24Code Generation (Chapter 7)
Code Template: Global Function
elaborate [func I ( ) : T ~ E] =JUMP g
e: evaluate [E]RETURN(s) 0
g:
E
evaluate [I ( )] =CALL(SB) e
where s is the size of T
25Code Generation (Chapter 7)
Nested Procedures and Functions
We extend Mini Triangle with nested procedures and functions.Again, this works nearly the same except for static links.
Recall the way this works from chapter 6. When calling a (nested) procedure we must tell the CALL where to find the static link.
Revised code template:
execute [I ( )] =CALL(r) e
evaluate [I ( )] =CALL(r) e
where address of I is (e, l)r determined by l and cl (current level)
26Code Generation (Chapter 7)
Procedures and Functions: Parameters
Parameters are pushed right before calling a procedure or function.They are addressed like locals, but with negative offsets (in TAM).
LB
ST
local variablesand intermediate
results
dynamic linkstatic link
return address
arguments
let proc double(var n:Integer) ~ n := n*2in ...
UnknownAddressaddress = (1, -1)
27Code Generation (Chapter 7)
Procedures and Functions: Parameters
We extend Mini Triangle with ...
Declaration ::= ... | proc Identifier (Formal) ~ CommandCommand ::= ... | Identifier (Actual) Formal ::= Identifier : TypeDenoter | var Identifier : TypeDenoterActual ::= Expression | var VName
28Code Generation (Chapter 7)
Code Templates: Parameters
elaborate [proc I (FP) ~ C] =JUMP g
e: execute [C]RETURN(0) d
g:
execute [I (AP)] =passArgument [AP]
CALL(r) e
passArgument [E] =evaluate [E]
passArgument [var V] =fetch [V]
where d is the size of FP
29Code Generation (Chapter 7)
Code Templates: Parameters
An “UnknownAddress” extra case for fetch and assign
fetch [V] = if V bound to unknown address LOAD d[r] where (d,l) = address where the LOADI(s) unknown address will be stored at runtime.
r determined by l and cl (current level).s is the size of the type of V.
assign [V] =LOAD d[r] STOREI(s)
30Code Generation (Chapter 7)
Runtime Entities Overview
Known Unknown
Value
Address
Routine
const lucky ~ 888 const foo ~ x + 10value: 888 address: (offset, level)
address where the valuewill be stored
proc double( ) ~ ...address: (offset, level)address of the routine(label e in template)
Proc or func parameteraddress: (offset, level)address where closureobject will be stored
var counter : Integeraddress: (offset, level) address of the variable
Pass-by-ref (var) parameteraddress: (offset, level) address where the pointer will be stored