CSCE 531 Compiler Construction Ch 7 Code Generation

  • Slides: 80
Download presentation
CSCE 531 Compiler Construction Ch. 7: Code Generation Spring 2007 Marco Valtorta mgv@cse. sc.

CSCE 531 Compiler Construction Ch. 7: Code Generation Spring 2007 Marco Valtorta mgv@cse. sc. edu UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Acknowledgment • The slides are based on the textbook and other sources, including slides

Acknowledgment • The slides are based on the textbook and other sources, including slides from Bent Thomsen’s course at the University of Aalborg in Denmark and several other fine textbooks • The three main other compiler textbooks I considered are: – Aho, Alfred V. , Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, & Tools, 2 nd ed. Addison-Welsey, 2007. (The “dragon book”) – Appel, Andrew W. Modern Compiler Implementation in Java, 2 nd ed. Cambridge, 2002. (Editions in ML and C also available; the “tiger books”) – Grune, Dick, Henri E. Bal, Ceriel J. H. Jacobs, and UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Koen G. Langendoen. Modern Compiler Design.

What This Lecture is About A compiler translates a program from a high-level language

What This Lecture is About A compiler translates a program from a high-level language into an equivalent program in a low-level language. Triangle Program Compile TAM Program Run Result UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Programming Language specification – A Language specification has (at least) three parts: • Syntax

Programming Language specification – A Language specification has (at least) three parts: • Syntax of the language: usually formal: EBNF • Contextual constraints: – scope rules (often written in English, but can be formal) – type rules (formal or informal) • Semantics: – defined by the implementation – informal descriptions in English – formal using operational, axiomatic, or denotational semantics UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

The “Phases” of a Compiler Source Program Syntax Analysis Error Reports Abstract Syntax Tree

The “Phases” of a Compiler Source Program Syntax Analysis Error Reports Abstract Syntax Tree Contextual Analysis Error Reports Decorated Abstract Syntax Tree Code Generation Chapter 7 Object Code UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Multi Pass Compiler A multi pass compiler makes several passes over the program. The

Multi Pass Compiler A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases. Dependency diagram of a typical Multi Pass Compiler: Compiler Driver calls Chapter 7 Syntactic Analyzer Contextual Analyzer Code Generator input output Source Text AST UNIVERSITY OF SOUTH CAROLINA Decorated AST Object Code Department of Computer Science and Engineering

Issues in Code Generation • Code Selection: Deciding which sequence of target machine instructions

Issues in Code Generation • Code Selection: Deciding which sequence of target machine instructions will be used to implement each phrase in the source language. • Storage Allocation Deciding the storage address for each variable in the source program. (static allocation, stack allocation etc. ) • Register Allocation (for register-based machines) How to use registers efficiently to store intermediate results. We use a stack based machine. This is not an issue for us UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Generation Source Program let var n: integer; var c: char in begin c

Code Generation Source Program let var n: integer; var c: char in begin c : = ‘&’; n : = n+1 end Target program PUSH 2 LOADL 38 STORE 1[SB] LOAD 0 LOADL 1 CALL add STORE 0[SB] Source and target program must be POP 2 “semantically equivalent” HALT Semantic specification of the source language is structured in terms of phrases in the SL: expressions, commands, etc. => Code generation follows the same “inductive” structure. ~ Q: Can you see the connection with denotational semantics? UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

“Inductive” Code Generation “Inductive” means: code generation for a “big” structure is defined in

“Inductive” Code Generation “Inductive” means: code generation for a “big” structure is defined in terms of putting together chunks of code that correspond to the substructures. Example: Sequential Command code generation Semantic specification The sequential command C 1 ; C 2 is executed as follows: first C 1 is executed then C 2 is executed. Code generation function: execute : Command -> Instruction* execute [C 1 ; C 2] = instructions for C 1 execute [C 1] instructions for C 2 execute [C 2] UNIVERSITY OF SOUTH CAROLINA instructions for C 1 ; C 2 Department of Computer Science and Engineering

“Inductive” Code Generation Example: Assignment command code generation Code generation function: instructions for E

“Inductive” Code Generation Example: Assignment command code generation Code generation function: instructions for E yield value for E execute [I : = E] = on top of the stack evaluate [E] STORE address [I] instruction to store result into variable These “pictures” of the code layout for a particular source language construct are called code templates. Inductive means: A code template specifies the object code to which a phrase is translated, in terms of the object code to which its subphrases are translated. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

“Inductive” Code Generation Example: code generation for a larger phrase in terms of its

“Inductive” Code Generation Example: code generation for a larger phrase in terms of its subphrases. execute [f : = f*n] LOAD CALL STORE execute [n : = n-1] LOAD n CALL pred STORE n execute [f : = f*n; n : = n-1] UNIVERSITY OF SOUTH CAROLINA f n mult f Department of Computer Science and Engineering

Specifying Code Generation with Code Templates For each “phrase class” P in the abstract

Specifying Code Generation with Code Templates For each “phrase class” P in the abstract syntax of the source language: Define code function f. P : P -> Instruction* that translate each phrase in class P to object code. We specify the function f. P by code templates. Typically they look like: f. P […Q…R…] = … f. Q [Q] note: A “phrase class” typically corresponds … to a non-terminal of the abstract syntax. f. R [R] … This in turn corresponds to an abstract class in the Java classes that implement the AST nodes. (for example Expression, Command, Declaration) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Specifying Code Generation with Code Templates Example: Code templates specification for Mini Triangle RECAP:

Specifying Code Generation with Code Templates Example: Code templates specification for Mini Triangle RECAP: The mini triangle AST Program : : = Command Program Command : : = V-name : = Expression Assign. Cmd | let Declaration in Command Let. Cmd. . . Expression : : = Integer-Literal Integer. Exp | V-name Vname. Exp | Operator Expression Unary. Exp | Expression Op Expression Binary. Exp Declaration : : =. . . UNIVERSITYIdentifier OF SOUTH CAROLINA V-name: : = Department of Computer Science and Engineering

Specifying Code Generation with Code Templates The code generation functions for Mini Triangle Phrase

Specifying Code Generation with Code Templates The code generation functions for Mini Triangle Phrase Class Function Effect of the generated code Program run P Run program P then halt. Starting and finishing with empty stack Command execute C Execute Command C. May update variables but does not shrink or grow the stack. Expres- evaluate E Evaluate E, net result is pushing the value of sion E on the stack. V-name Push value of constant or variable on the fetch V stack. V-name assign V Pop value from stack and store in variable V Declaelaborate Elaborate declaration, make space on the ration stack for constants and variables in the decl. D UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Generation with Code Templates The code generation functions for Mini Triangle Programs: run

Code Generation with Code Templates The code generation functions for Mini Triangle Programs: run [C] = execute [C] HALT Commands: execute [V : = E] = evaluate [E] assign [V] execute [I ( E )] = evaluate [E] CALL p where p is address of the routine named I UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Generation with Code Templates Commands: execute [C 1 ; C 2] = execute

Code Generation with Code Templates Commands: execute [C 1 ; C 2] = execute [C 1] execute [C 2] execute [if E then C 1 else C 2] = evaluate [E] E JUMPIF(0) g C 1 execute [C 1] JUMP h g: g: execute [C 2] C 2 h: h: UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Generation with Code Templates Commands: execute [while E do C] = JUMP h

Code Generation with Code Templates Commands: execute [while E do C] = JUMP h g: execute [C] C h: evaluate[E] E JUMPIF(1) g Alternative While Command code template: execute [while E do C] = g: evaluate [E] E JUMPIF(0) h C execute[C] JUMP g h: UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Generation with Code Templates Repeat Command code template: execute [repeat C until E]

Code Generation with Code Templates Repeat Command code template: execute [repeat C until E] = g: execute [C] h: evaluate[E] JUMPIF(0) g C E execute [let D in C] = elaborate[D] execute [C] POP(0) s if s>0 where s = amount of storage allocated by D UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Generation with Code Templates Expressions: evaluate [IL] = LOADL v note: IL is

Code Generation with Code Templates Expressions: evaluate [IL] = LOADL v note: IL is an integer literal where v = the integer value of IL evaluate [V] = fetch[V] note: V is variable name evaluate [O E] = evaluate[E] CALL p note: O is a unary operator where p = address of routine for O evaluate [E 1 O E 2] = note: O is a binary operator evaluate[E 1] evaluate[E 2] CALL p where p = address of routine for O UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Generation with Code Templates Variables: note: Mini triangle only needs static allocation (Q:

Code Generation with Code Templates Variables: note: Mini triangle only needs static allocation (Q: why is that? ) fetch [V] = LOAD d[SB] assign [V] = STORE d[SB] UNIVERSITY OF SOUTH CAROLINA where d = address of V relative to SB Department of Computer Science and Engineering

Code Generation with Code Templates Declarations: elaborate [const I ~ E] = evaluate[E] elaborate

Code Generation with Code Templates Declarations: elaborate [const I ~ E] = evaluate[E] elaborate [var I : T] = PUSH s where s = size of T elaborate [D 1 ; D 2] = elaborate [D 1] elaborate [D 2] THE END: these are all the code templates for Mini Triangle. Now let’s put them to use in an example. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Example of Mini Triangle Code Generation execute [while i>0 do i: =i+2] = JUMP

Example of Mini Triangle Code Generation execute [while i>0 do i: =i+2] = JUMP h g: LOAD i evaluate [i+2] LOADL 2 CALL add STORE i h: LOAD i LOADL 0 evaluate [i>0] CALL gt JUMPIF(1) g execute [i: =i+2] Note: Picture shows a few steps but not all UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Special Case Code Templates There are often several ways to generate code for an

Special Case Code Templates There are often several ways to generate code for an expression, command, etc. The templates we defined work, but sometimes we can get more efficient code for special cases => special case code templates. Example: evaluate [i+1] = LOAD i LOADL 1 CALL add what we get with the “general” code templates UNIVERSITY OF SOUTH CAROLINA evaluate [i+1] = LOAD i CALL succ more efficient code for the special case “+1” Department of Computer Science and Engineering

Special Case Code Templates Example: some special case code template for “+1”, “-1”, …

Special Case Code Templates Example: some special case code template for “+1”, “-1”, … evaluate [E + 1] = evaluate [1 + E] = evaluate [E] CALL succ evaluate [E - 1] = evaluate [E] CALL pred A special-case code template is one that is applicable to phrase of a special form. Such phrases are also covered by a more general form. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Special Case Code Templates Example: “Inlining” known constants. execute [let const n~7; var i:

Special Case Code Templates Example: “Inlining” known constants. execute [let const n~7; var i: Integer in i: =n*n] = elaborate [const n~7] LOADL 7 elaborate [var i: Integer] PUSH 1 LOAD n execute [i: =n*n] CALL mult STORE i POP(0) 2 This is how the code looks like with no special case templates UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Special Case Code Templates Example: “Inlining” known constants. Special case templates for inlining literals.

Special Case Code Templates Example: “Inlining” known constants. Special case templates for inlining literals. elaborate [const I ~ IL] = no code fetch [I] = LOADL v special case if I is a known literal constant where v is the known value of I execute [let const n~7; var i: Integer in i: =n*n] = PUSH 1 LOADL 7 CALL mult STORE i POP(0) 1 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Generation Algorithm The code templates specify how code is to be generated =>

Code Generation Algorithm The code templates specify how code is to be generated => determines code generation algorithm. Generating code: traversal of the AST emitting instructions one by one. The code templates determine the order of the traversal and the instructions to be emitted. We will now look at how to implement a Mini Triangle code generator in Java. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Representation of Object Program: Instructions public class Instruction { public byte op; // op-code

Representation of Object Program: Instructions public class Instruction { public byte op; // op-code 0. . 15 public byte r; // register field (0. . 15) public byte n; // length field (0. . 255) public short d; // operand f. (-32767. . +32767) public static final byte // op-codes LOADop = 0, LOADAop = 1, . . . public static final byte // register numbers CBr = 0, CTr = 1, … SBr = 4, STr = 5, … } public Instruction(byte op, byte n, byte r, short d) {. . . } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Representation of Object Program: Emitting Code public class Encoder { private Instruction[] code =

Representation of Object Program: Emitting Code public class Encoder { private Instruction[] code = new Instruction[1024]; private short next. Instr. Addr = 0; private void emit(byte op, byte n, byte r, short d) { code[next. Instr. Addr++]=new Instruction( op, n, r, d); }. . . lots of other stuff in here of course. . . } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Developing a Code Generator “Visitor” Phrase visitor method Behavior of the visitor method Class

Developing a Code Generator “Visitor” Phrase visitor method Behavior of the visitor method Class Program visit. Program generate code as specified by run[P] Command visit…Command generate code as specified by execute[C] Expression visit…Expression generate code as specified by evaluate[E] V-name visit…Vname Return “entity description” for the visited variable or constant name. Declaration visit…Declaration generate code as specified by elaborate[D] Type-Den visit…Type. Den return the size of the type UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Developing a Code Generator “Visitor” For variables we have two distinct code generation functions:

Developing a Code Generator “Visitor” For variables we have two distinct code generation functions: fetch and assign. => Not implemented as visitor methods but as separate methods. public void encode. Fetch(Vname) {. . . as specified by fetch template. . . } public void encode. Assign(Vname) {. . . as specified by assign template. . . } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Developing a Code Generator “Visitor” public class Encoder implements Visitor {. . . /*

Developing a Code Generator “Visitor” public class Encoder implements Visitor {. . . /* Generating code for entire Program */ public Object visit. Program(Program prog, Object arg ) { prog. C. visit(this, arg); emit a halt instruction return null; } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Developing a Code Generator “Visitor” RECAP: execute [V : = E] = evaluate [E]

Developing a Code Generator “Visitor” RECAP: execute [V : = E] = evaluate [E] assign [V] /* Generating code for commands */ public Object visit. Assign. Command( Assign. Command com, Object arg) { com. E. visit(this, arg); encode. Assign(com. V); return null; } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Developing a Code Generator “Visitor” execute [I ( E )] = evaluate [E] CALL

Developing a Code Generator “Visitor” execute [I ( E )] = evaluate [E] CALL p where p is address of the routine named I public Object visit. Call. Command( Call. Command com, Object arg) { com. E. visit(this, arg); short p = address of primitive routine for name com. I emit(Instruction. CALLop, Instruction. SBr, Instruction. PBr, p); return null; } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Developing a Code Generator “Visitor” execute [C 1 ; C 2] = execute[C 1]

Developing a Code Generator “Visitor” execute [C 1 ; C 2] = execute[C 1] execute[C 2] public Object visit. Sequential. Command( Sequential. Command com, Object arg) { com. C 1. visit(this, arg); com. C 2. visit(this, arg); return null; } Let. Command, If. Command, While. Command => later. - Let. Command is more complex: memory allocation and addresses - If. Command While. Command: complications with jumps UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Developing a Code Generator “Visitor” evaluate [IL] = LOADL v where v is the

Developing a Code Generator “Visitor” evaluate [IL] = LOADL v where v is the integer value of IL /* Expressions */ public Object visit. Integer. Expression ( Integer. Expression expr, Object arg) { short v = valuation(expr. IL. spelling); emit(Instruction. LOADLop, 0, 0, v); return null; } public short valuation(String s) {. . . convert string to integer value. . . } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Developing a Code Generator evaluate [E 1 O E 2] = “Visitor” evaluate [E

Developing a Code Generator evaluate [E 1 O E 2] = “Visitor” evaluate [E 1] evaluate [E 2] CALL p where p is the address of routine for O public Object visit. Binary. Expression ( Binary. Expression expr, Object arg) { expr. E 1. visit(this, arg); expr. E 2. visit(this, arg); short p = address for expr. O operation emit(Instruction. CALLop, Instruction. SBr, Instruction. PBr, p); return null; } Remaining expression visitors are developed in a similar way. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Controls Structures We have yet to discuss generation for If. Command While. Command execute

Controls Structures We have yet to discuss generation for If. Command While. Command execute [while E do C] = JUMP h g: execute [C] h: evaluate[E] JUMPIF(1) g C E A complication is the generation of the correct addresses for the jump instructions. We can determine the address of the instructions by incrementing a counter while emitting instructions. Backwards jumps are easy but forward jumps are harder. Q: why? UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Control Structures Backwards jumps are easy: The “address” of the target has already been

Control Structures Backwards jumps are easy: The “address” of the target has already been generated and is known Forward jumps are harder: When the jump is generated the target is not yet generated so its address is not (yet) known. There is a solution which is known as backpatching 1) Emit jump with “dummy” address (e. g. simply 0). 2) Remember the address where the jump instruction occurred. 3) When the target label is reached, go back and patch the jump instruction. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Backpatching Example public Object While. Command ( While. Command com, Object arg) { short

Backpatching Example public Object While. Command ( While. Command com, Object arg) { short j = next. Instr. Addr; emit(Instruction. JUMPop, 0, Instruction. CBr, 0); dummy address short g = next. Instr. Addr; com. C. visit(this, arg); short h = next. Instr. Addr; backpatch code[j]. d = h; com. E. visit(this, arg); emit(Instruction. JUMPIFop, 1, Instruction. CBr, g); execute [while E do C] = return null; JUMP h } g: execute [C] h: evaluate[E] UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering JUMPIF(1) g

Constants and Variables We have not yet discussed generation of Let. Command. This is

Constants and Variables We have not yet discussed generation of Let. Command. This is the place in Mini. Triangle where declarations are. Calculated during generation for execute [let D in C] = elaborate[D] execute [C] How to know these? POP(0) s if s>0 where s = amount of storage allocated by D fetch [V] = LOAD d[SB] assign [V] = STORE d[SB] where d = address of V relative to SB UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Constants and Variables Example Accessing known values and known addresses let const b ~

Constants and Variables Example Accessing known values and known addresses let const b ~ 10; var i: Integer; in i : = i*b PUSH 1 LOAD 4[SB] LOADL 10 CALL mult STORE 4[SB] elaborate[const … ; var …] execute [i: =i*b] UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Constants and Variables Example Accessing an unknown value. Not all constants have values known

Constants and Variables Example Accessing an unknown value. Not all constants have values known (at compile time). let var x: Integer; in let const y ~ 365 + x in putint(y) Depends on variable x: value not known at compile time. When visiting declarations the code generator must decide whether to represent constants in memory or as a literal value => We have to remember the address or the value somehow. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Constants and Variables Example Accessing an unknown value. let var x: Integer; in let

Constants and Variables Example Accessing an unknown value. let var x: Integer; in let const y ~ 365 + x in putint(y) PUSH 1 LOADL 365 LOAD 4[SB] CALL add STORE 5[SB] LOAD 5[SB] CALL putint elaborate[var x: Integer] elaborate[const y ~ 365 + x] execute [putint(y)] UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Constants and Variables Entity descriptions: When the code generator visits a declaration: 1) it

Constants and Variables Entity descriptions: When the code generator visits a declaration: 1) it decides whether to represent it as a known value or a known address 2) if its an address then emit code to reserve space. 3) make an entity description: an object that describes the variable or constant: its value or address, its size. 4) put a link in the AST that points to the entity description Example and picture on next slide UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Constants and Variables let const b ~ 10; var i: Integer; in i :

Constants and Variables let const b ~ 10; var i: Integer; in i : = i*b RECAP: Applied occurrences of Identifiers point to their declaration Let. Command Sequential. Declaration Const. Decl Var. Decl Ident Int. Exp Ident b 10 i known value size = 1 value = 10 known address = 4 size = 1 UNIVERSITY OF SOUTH CAROLINA Ident i i b Department of Computer Science and Engineering

Constants and Variables let var x: Integer; in let const y ~ 365 +

Constants and Variables let var x: Integer; in let const y ~ 365 + x in putint(y) Let. Command Var. Decl Const. Decl Ident x y known address = 4 size = 1 Note: There also unknown addresses. More about these later. unknown value address = 5 size = 1 UNIVERSITY OF SOUTH CAROLINA Q: When do unknown addresses occur? Department of Computer Science and Engineering

Static Storage Allocation Example 1: Global variables let var var in. . . a:

Static Storage Allocation Example 1: Global variables let var var in. . . a: b: c: d: Integer; Boolean; Integer; Tam Address: a 0[SB] b 1[SB] c 2[SB] d 3[SB] Note: In this example all globals have the same size: 1. This is not always the case. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Static Storage Allocation Example 2: Static allocation with nested blocks: overlays let var a:

Static Storage Allocation Example 2: Static allocation with nested blocks: overlays let var a: Integer; in begin. . . let var b: Boolean; var c: Integer; in begin. . . end; . . . let var d: Integer; in begin. . . end; . . . end UNIVERSITY OF SOUTH CAROLINA Tam Address: a 0[SB] b c 1[SB] 2[SB] Same address! d 1[SB] Q: Why can b and d share the same address? Department of Computer Science and Engineering

Static Storage Allocation: In the Code Generator Entity Descriptions: public abstract class Runtime. Entity

Static Storage Allocation: In the Code Generator Entity Descriptions: public abstract class Runtime. Entity { public short size; . . . } public class Known. Value extends Runtime. Entity { public short value; . . . } public class Unknown. Value extends Runtime. Entity { public short address; . . . } public class Known. Address extends Runtime. Entity { public short address; . . . } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Static Storage Allocation: In the Code Generator Entity Descriptions: public abstract class AST {

Static Storage Allocation: In the Code Generator Entity Descriptions: public abstract class AST { public Runtime. Entity entity; // mostly used for Decls. . . } Note: This is an addition to the AST class and requires recompilation of a lot of code if added late in the compiler implementation, but there seems to be no way around it! UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Static Storage Allocation: In the Code Generator public Object visit. . . Command(. .

Static Storage Allocation: In the Code Generator public Object visit. . . Command(. . . Command com, Object arg) { short gs = short. Value. Of(arg); generate code as specified by execute[com] return null; } public Object visit. . . Expression(. . . Expression expr, Object arg) { short gs = short. Value. Of(arg); generate code as specified by evaluate[com] return new Short(size of expr result); } public Object visit. . . Declaration(. . . Declaration dec, Object arg) { short gs = short. Value. Of(arg); generate code as specified by evaluate[com] return new Short(amount of extra allocated by dec); } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Static Storage Allocation: In the Code Generator The visitor is started … public void

Static Storage Allocation: In the Code Generator The visitor is started … public void encode(Program prog) { prog. visit(this, new Short(0)); } Amount of global storage already allocated. Initially = 0 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Static Storage Allocation: In the Code Generator Some concrete examples of visit methods elaborate

Static Storage Allocation: In the Code Generator Some concrete examples of visit methods elaborate [var I : T] = PUSH s where s = size of T public Object visit. Var. Declaration( Var. Declaration decl, Object arg) { short gs = short. Value. Of(arg); short s = short. Value. Of(decl. T. visit(this, null)); decl. entity = new Known. Address(s, gs); emit(Instruction. PUSHop, 0, 0, s); return new Short(s); } Remember the address/size of the variable UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Static Storage Allocation: In the Code Generator elaborate [D 1 ; D 2] =

Static Storage Allocation: In the Code Generator elaborate [D 1 ; D 2] = elaborate [D 1] elaborate [D 2] public Object visit. Sequential. Declaration( Sequential. Declaration decl, Object arg) { short gs = short. Value. Of(arg); short s 1 = short. Value. Of(decl. D 1. visit(this, arg)); short s 2 = short. Value. Of(decl. D 2. visit(this, new Short(gs+s 1))); return new Short(s 1+s 2); } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Static Storage Allocation: In the Code Generator execute [let D in C] = elaborate[D]

Static Storage Allocation: In the Code Generator execute [let D in C] = elaborate[D] execute [C] POP(0) s if s>0 where s = amount of storage allocated by D public Object visit. Let. Command( Let. Command com, Object arg) { short gs = short. Value. Of(arg); short s = short. Value. Of(com. D. visit(this, arg)); com. C. visit(this, new Short(gs+s)); if (s > 0) emit(Instruction. POPop, 0, 0, s) return null; } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Static Storage Allocation: In the Code Generator fetch [I] = special case if I

Static Storage Allocation: In the Code Generator 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 public void encode. Fetch(Vname, short s) { Runtime. Entity ent = VName. I. decl. entity; if (ent instanceof Known. Value) { short v = ((Known. Value)ent). value; emit(Instruction. LOADLop, 0, 0, v); } else { short d = (entity instanceof Unknown. Value) ? ((Unknown. Value)ent). address : ((Known. Address)ent). address ; emit(Instruction. LOADop, 0, Instruction. SBr, d); } } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Stack Allocation, Procedures and Functions Now we will consider: 1) how procedures and functions

Stack Allocation, Procedures and Functions Now we will consider: 1) how procedures and functions are compiled 2) how to modify code generator to compute addresses when we use a stack allocation model (instead of static allocation) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

RECAP: TAM Frame Layout Summary arguments LB ST dynamic link static link return address

RECAP: TAM Frame Layout Summary arguments LB ST dynamic link static link return address local variables and intermediate results UNIVERSITY OF SOUTH CAROLINA Arguments for current procedure they were put here by the caller. Link data Local data, grows and shrinks during execution. Department of Computer Science and Engineering

RECAP: Accessing Global & Local Variables Example: Compute the addresses of the variables in

RECAP: Accessing Global & Local Variables Example: Compute the addresses of the variables in this program Var Size Address 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(); . . . end a b c 3 1 1 [0]SB [3]SB [4]SB d e 1 ? [2]LB [3]LB f 1 [2]LB in begin. . . ; Y(); . . . ; Z(); end UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

RECAP: TAM addressing schemas overview We now have a complete picture of the different

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 Load instruction Global LOAD +offset[SB] Local LOAD +offset[LB] Parameter LOAD -offset[LB] Non-local, 1 level up LOAD +offset[L 1] LOAD +offset[L 2] Non-local, 2 levels up UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering. . .

How To Characterize Addresses now? When we have a static allocation model only, an

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. the offset from SB) Now we generalize this to stack allocation (for nested procedures) Q: How do we characterize an address for a variable/constant now? A: We need two numbers. - nesting level - offset (similar to static allocation) Q: How do we compute the addresses to use in an instruction that loads or stores a value from/to a variable? UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

How To Characterize Addresses Example: Compute the addresses of the variables in this program

How To Characterize Addresses Example: Compute the addresses of the variables in this program Var Size Addr Accessing let var a: array 3 of Integer; var b: Boolean; proc foo() ~ let var d: Integer; var e: . . . proc bar() ~ let var f: Integer; in. . . bar body. . . in. . . foo body. . . ; in. . . global code. . . a b 3 1 (0, 0) (0, 3) 0[SB] 3[SB] d e 1 ? (1, 0) (1, 1) ? f 1 (3, 0) 0[LB] How to access depends on … where are you accessing from! accessing e from foo body => 1[LB] accessing e from bar body => 1[L 1] UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

New Fetch / Assign Code Templates fetch [I] = LOADL(s) d[r] s = Size

New Fetch / Assign Code Templates fetch [I] = LOADL(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 ==> l = cl ==> otherwise ==> r = SB r = L(cl-l) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

How To Modify The Code Generator An info structure to pass as argument in

How To Modify The Code Generator An info structure to pass as argument in the visitor (instead of “gs”) public class Frame { public byte level; public short size; } Before it was sufficient to pass the current size (gs) of the global frame since without procedures all storage is allocated at level 0. With subprograms we need to know the current level and the size of the frame. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

How To Modify The Code Generator Different kind of “address” in entity descriptors public

How To Modify The Code Generator Different kind of “address” in entity descriptors public class Entity. Address { public byte level; public short displacement; } public class public. . . } Unknown. Value extends Runtime. Entity { Entity. Address address; Known. Address extends Runtime. Entity { Entity. Address address; UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

How To Modify The Code Generator Changes to code generator (visitor) Example: public Object

How To Modify The Code Generator Changes to code generator (visitor) Example: public Object visit. Var. Declaration( Var. Declaration decl, Object arg) { Frame frame = (Frame)arg; short s = short. Value. Of(decl. T. visit(this, null)); decl. entity = new Known. Address(s, frame); emit(Instruction. PUSHop, 0, 0, s); return new Short(s); } etc. Q: When will the level of a frame be changed? Q: When will the size be changed? UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Change of frame level and size • In the visitor/encoding method for translating a

Change of frame level and size • In the visitor/encoding method for translating a procedure body, the frame level must be incremented by one and the frame size set to 3 (space for link data) Frame outer. Frame … Frame local. Frame = new Frame(outer. Frame. level +1, 3); • The encoder starts at frame level 0 and with no storage allocated: public void encoder(Program prog) { Frame global. Frame = new Frame(0, 0); prog. visit(this, global. Frame); } UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Procedures and Functions We extend Mini Triangle with procedures: Declaration : : =. .

Procedures and Functions We extend Mini Triangle with procedures: Declaration : : =. . . | proc Identifier ( ) ~ Command : : =. . . | Identifier ( ) First , we will only consider global procedures (with no arguments). UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Template: Global Procedure elaborate [proc I () ~ C] = JUMP g e:

Code Template: Global Procedure elaborate [proc I () ~ C] = JUMP g e: execute [C] C RETURN(0) 0 g: execute [I ()] = CALL(SB) e UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Template: Global Procedure let var n: Integer; Example: proc double() ~ n :

Code Template: Global Procedure let var n: Integer; Example: proc double() ~ n : = n*2 in begin n : = 9; double() end 0: PUSH 1: JUMP 2: LOAD 3: LOADL 4: CALL 5: STORE 6: RETURN(0) 7: LOADL 8: STORE 9: CALL(SB) 10: POP(0) 11: HALT 1 7 0[SB] 2 mult 0[SB] 0 9 0[SB] 2 1 var n: Integer n : = n*2 proc double() ~ n : = n*2 n : = 9 double() UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Procedures and Functions We extend Mini Triangle with functions: Declaration : : =. .

Procedures and Functions We extend Mini Triangle with functions: Declaration : : =. . . | func Identifier ( ) : Type. Denoter ~ Expression : : =. . . | Identifier ( ) First , we will only consider global functions (with no arguments). This is all pretty much the same as procedures (except for the RETURN) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Template: Global Function elaborate [func I () : T ~ E] = JUMP

Code Template: Global Function elaborate [func I () : T ~ E] = JUMP g e: evaluate [E] RETURN(s) 0 g: where s is the size of T C evaluate [I ()] = CALL(SB) e UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Nested Procedures and Functions Again, this is all pretty much the same except for

Nested Procedures and Functions Again, this is all pretty much the same except for static links. When calling a (nested) procedure we must tell the CALL where to find the static link. Revised code template: execute [I ()] = CALL(r) e e from address of I is (d, l) r determined by l and cl (current level) evaluate [I ()] = CALL(r) e UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Procedures and Functions: Parameters We extend Mini Triangle with. . . Declaration : :

Procedures and Functions: Parameters We extend Mini Triangle with. . . Declaration : : =. . . | proc Identifier (Formal) : Type. Denoter ~ Expression : : =. . . | Identifier (Actual) Formal : : = Identifier : Type. Denoter | var Identifier : Type. Denoter Actual : : = Expression | var VName UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Procedures and Functions: Parameters are pushed right before calling a proc/func. They are addressed

Procedures and Functions: Parameters are pushed right before calling a proc/func. They are addressed like locals, but with negative offsets (in TAM). let proc double(var n: Integer) ~ n : = n*2 in. . . arguments Unknown. Address address = (1, -1) UNIVERSITY OF SOUTH CAROLINA LB ST dynamic link static link return addres local variables and intermediate results Department of Computer Science and Engineering

Code Templates Parameters elaborate [proc I(FP) ~ C] = JUMP g e: execute [C]

Code Templates Parameters elaborate [proc I(FP) ~ C] = JUMP g e: execute [C] where d is the size of FP RETURN(0) d g: execute [I (AP)] = pass. Argument [AP] CALL(r) e Where (l, e) = address of routine bound to I, Cl = current routine level pass. Argument [E] = R = display-register(cl, l) evaluate [E] pass. Argument [var V] = fetch. Address [V] UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Code Templates Parameters An “Unknown. Address” extra case for fetch and assign fetch [V]

Code Templates Parameters An “Unknown. Address” extra case for fetch and assign fetch [V] = LOAD d[r] LOADI(s) assign [V] = LOAD d[r] STOREI(s) if V bound to unknown address where (d, l) = address where the unknown address will be stored at runtime. s is the size of the type of V where d = address where the unknow address will be stored a runtime. UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Runtime Entities Overview Known Value const lucky ~ 888 Unknown const foo ~ x

Runtime Entities Overview Known Value const lucky ~ 888 Unknown const foo ~ x + 10 address: (level, offset) the address where value will be stored A var parameter Address var counter : Integer address: (level, offset) of the address where the pointer the variable. will be stored. Routine proc double() ~. . . A procedure or function address: (level, offset) of parameter. address: (level, offset) the routine (label e in address where closure object template) will be stored. UNIVERSITY OF SOUTH CAROLINA value: 888 Department of Computer Science and Engineering

Code generation summary • Create code templates inductively – There may be special case

Code generation summary • Create code templates inductively – There may be special case templates generating equivalent, but more efficient code • Use visitors pattern to walk the AST recursively emitting code as you go along – Back patching is needed forward jumps – It is necessary to keep track of frame level and allocated space • That’s it folks! UNIVERSITY OF SOUTH CAROLINA – At least for Mini. Triangle on TAM Department of Computer Science and Engineering