Upload
whitney-soto
View
16
Download
1
Embed Size (px)
DESCRIPTION
Languages and Compilers (SProg og Oversættere). Code Generation. Code Generation. Describe the purpose of the code generator Code templates Back patching. The “Phases” of a Compiler. Source Program. Syntax Analysis. Error Reports. Abstract Syntax Tree. Contextual Analysis. - PowerPoint PPT Presentation
Citation preview
1
Languages and Compilers(SProg og Oversættere)
Code Generation
2
Code Generation
– Describe the purpose of the code generator
– Code templates
– Back patching
3
The “Phases” of a Compiler
Syntax Analysis
Contextual Analysis
Code Generation
Source Program
Abstract Syntax Tree
Decorated Abstract Syntax Tree
Object Code
Error Reports
Error Reports
4
Multi Pass Compiler
Compiler Driver
Syntactic Analyzer
callscalls
Contextual Analyzer Code Generator
calls
Dependency diagram of a typical 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.
input
Source Text
output
AST
input output
Decorated AST
input output
Object Code
5
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 AllocationDeciding 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.
6
Code Generation
Source Program
let var n: integer; var c: charin begin c := ‘&’; n := n+1end
let var n: integer; var c: charin begin c := ‘&’; n := n+1end
PUSH 2LOADL 38STORE 1[SB]LOAD 0LOADL 1CALL addSTORE 0[SB]POP 2HALT
PUSH 2LOADL 38STORE 1[SB]LOAD 0LOADL 1CALL addSTORE 0[SB]POP 2HALT
Target program
~~
Source and target program must be“semantically equivalent”
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 formal semantics?
7
Specifying Code Generation with Code Templates
Example: Code templates specification for Mini TriangleRECAP: The mini triangle AST
Program ::= Command ProgramCommand ::= V-name := Expression AssignCmd | let Declaration in Command LetCmd ...Expression ::= Integer-Literal IntegerExp | V-name VnameExp | Operator Expression UnaryExp | Expression Op Expression BinaryExpDeclaration ::= ...V-name::= IdentifierSimpleVName
Program ::= Command ProgramCommand ::= V-name := Expression AssignCmd | let Declaration in Command LetCmd ...Expression ::= Integer-Literal IntegerExp | V-name VnameExp | Operator Expression UnaryExp | Expression Op Expression BinaryExpDeclaration ::= ...V-name::= IdentifierSimpleVName
8
Specifying Code Generation with Code Templates
The code generation functions for Mini Triangle
Phrase Class Function Effect of the generated code
Program
Command
Expres-sionV-name
V-nameDecla-ration
run P
execute C
evaluate E
fetch V
assign Velaborate D
Run program P then halt. Starting and finishing with empty stackExecute Command C. May update variables but does not shrink or grow the stack!Evaluate E, net result is pushing the value of E on the stack.Push value of constant or variable on the stack.Pop value from stack and store in variable VElaborate declaration, make space on the stack for constants and variables in the decl.
9
Code Generation with Code Templates
run [C] =execute [C]HALT
The code generation functions for Mini Triangle
Programs:
Commands:
execute [V := E] =evaluate [E]assign [V]
execute [I ( E )] =evaluate [E]CALL p where p is address of the routine named I
10
Code Generation with Code Templates
Commands:
execute [C1 ; C2] =execute [C1]execute [C2]
execute [if E then C1 else C2] =evaluate [E]JUMPIF(0) gexecute [C1]JUMP h
g: execute [C2]h:
C1
C2
E
g:
h:
11
Code Generation with Code Templates
execute [while E do C] =
JUMP hg: execute [C]h: evaluate[E]
JUMPIF(1) g
C
E
While command
12
Developing a Code Generator “Visitor”
public Object visitSequentialCommand(SequentialCommand com,Object arg) {
com.C1.visit(this,arg);com.C2.visit(this,arg);return null;
}
public Object visitSequentialCommand(SequentialCommand com,Object arg) {
com.C1.visit(this,arg);com.C2.visit(this,arg);return null;
}
execute [C1 ; C2] =execute[C1]execute[C2]
LetCommand, IfCommand, WhileCommand => later. - LetCommand is more complex: memory allocation and addresses - IfCommand and WhileCommand: complications with jumps
13
Backpatching Example
public Object WhileCommand (WhileCommand com,Object arg) {
short j = nextInstrAddr;emit(Instruction.JUMPop, 0,
Instruction.CBr,0);short g = nextInstrAddr;com.C.visit(this,arg);short h = nextInstrAddr;code[j].d = h; com.E.visit(this,arg);emit(Instruction.JUMPIFop, 1,
Instruction.CBr,g);return null;
}
public Object WhileCommand (WhileCommand com,Object arg) {
short j = nextInstrAddr;emit(Instruction.JUMPop, 0,
Instruction.CBr,0);short g = nextInstrAddr;com.C.visit(this,arg);short h = nextInstrAddr;code[j].d = h; com.E.visit(this,arg);emit(Instruction.JUMPIFop, 1,
Instruction.CBr,g);return null;
}
execute [while E do C] =JUMP h
g: execute [C]h: evaluate[E]
JUMPIF(1) g
dummy address
backpatch
14
Constants and Variables
We have not yet discussed generation of LetCommand. This is the place in MiniTriangle where declarations are.
execute [let D in C] =elaborate[D]execute [C]POP(0) s if s>0
where s = amount of storage allocated by D
How to know these?
fetch [V] =LOAD d[SB] where d = address of V relative to SB
assign [V] =STORE d[SB] where d = address of V relative to SB
Calculated during generation forelaborate[D]
15
Code Template: Global Procedure
elaborate [proc I () ~ C] =JUMP g
e: execute [C]RETURN(0) 0
g:
C
execute [I ()] =CALL(SB) e
16
Code Template: Global Procedure
Example: let var n: Integer; proc double() ~ n := n*2in begin n := 9; double() end
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()