Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Languages and Compilers (SProg og Oversættere) Code Generation.

Similar presentations


Presentation on theme: "1 Languages and Compilers (SProg og Oversættere) Code Generation."— Presentation transcript:

1 1 Languages and Compilers (SProg og Oversættere) Code Generation

2 2 –Describe the purpose of the code generator –Code templates –Back patching

3 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

4 4 Multi Pass Compiler Compiler Driver Syntactic Analyzer calls Contextual AnalyzerCode 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 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 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.

6 6 Code Generation Source Program let var n: integer; var c: char in begin c := ‘&’; n := n+1 end PUSH 2 LOADL 38 STORE 1[SB] LOAD 0 LOADL 1 CALL add STORE 0[SB] POP 2 HALT 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 7 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 AssignCmd | let Declaration in Command LetCmd... Expression ::= Integer-Literal IntegerExp | V-name VnameExp | Operator Expression UnaryExp | Expression Op Expression BinaryExp Declaration ::=... V-name::= Identifier SimpleVName Program ::= Command Program Command ::= V-name := Expression AssignCmd | let Declaration in Command LetCmd... Expression ::= Integer-Literal IntegerExp | V-name VnameExp | Operator Expression UnaryExp | Expression Op Expression BinaryExp Declaration ::=... V-name::= Identifier SimpleVName

8 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- sion V-name Decla- ration run P execute C evaluate E fetch V assign V elaborate D Run program P then halt. Starting and finishing with empty stack Execute 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 V Elaborate declaration, make space on the stack for constants and variables in the decl.

9 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 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) g execute [ C1 ] JUMP h g: execute [ C2 ] h: C1 C2 E g:g: h:h:

11 11 Code Generation with Code Templates execute [ while E do C ] = JUMP h g: execute [ C ] h: evaluate[ E ] JUMPIF(1) g C E While command

12 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 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 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 for elaborate[ D ]

15 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 16 Code Template: Global Procedure Example: let var n: Integer; proc double() ~ n := n*2 in begin n := 9; double() end let var n: Integer; proc double() ~ n := n*2 in begin n := 9; double() end 0:PUSH1 1:JUMP7 2:LOAD0[SB] 3:LOADL2 4:CALLmult 5:STORE0[SB] 6:RETURN(0)0 7:LOADL9 8:STORE0[SB] 9:CALL(SB)2 10:POP(0)1 11:HALT n := n*2 var n: Integer proc double() ~ n := n*2 n := 9 double()


Download ppt "1 Languages and Compilers (SProg og Oversættere) Code Generation."

Similar presentations


Ads by Google