1 / 16

Languages and Compilers (SProg og Oversættere)

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.

mcelwaind
Download Presentation

Languages and Compilers (SProg og Oversættere)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 Source Program Syntax Analysis Error Reports Abstract Syntax Tree Contextual Analysis Error Reports Decorated Abstract Syntax Tree Code Generation Object Code

  4. Multi Pass Compiler input input input output output output Source Text AST Decorated AST Object Code 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 calls calls Syntactic Analyzer Contextual Analyzer Code Generator

  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. Code Generation ~ ~ Source Program Target program 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 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 Triangle RECAP: The mini triangle AST Program ::= Command Program Command ::= V-name := ExpressionAssignCmd | let Declaration in CommandLetCmd ... Expression ::= Integer-LiteralIntegerExp | V-name VnameExp | Operator ExpressionUnaryExp | Expression Op ExpressionBinaryExp Declaration ::= ... 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 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. run P executeC evaluateE fetchV assignV elaborate D Program Command Expres- sion V-name V-name Decla-ration

  9. 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 wherep is address of the routine named I

  10. Code Generation with Code Templates E C1 g: C2 h: Commands: • execute [C1 ;C2] = • execute [C1] • execute [C2] • execute [if E thenC1 elseC2] = • evaluate [E] • JUMPIF(0) g • execute [C1] • JUMP h • g:execute [C2] • h:

  11. Code Generation with Code Templates While command • execute [whileE doC] = • JUMP h • g: execute [C] • h: evaluate[E] • JUMPIF(1) g C E

  12. Developing a Code Generator “Visitor” • execute [C1;C2] = • execute[C1] • execute[C2] public Object visitSequentialCommand( SequentialCommand com,Object arg) { com.C1.visit(this,arg); com.C2.visit(this,arg); return null; } 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; } dummy address backpatch • execute [while E doC] = • JUMP h • g: execute [C] • h: evaluate[E] • JUMPIF(1) g

  14. Constants and Variables • Calculated during generation for • elaborate[D] We have not yet discussed generation of LetCommand. This is the place in MiniTriangle where declarations are. • execute [let D inC] = • elaborate[D] • execute [C] • POP(0) s if s>0where 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

  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*2 in begin n := 9; double() end var n: Integer 0: PUSH 1 1: JUMP 7 2: LOAD 0[SB] 3: LOADL 2 4: CALL mult 5:STORE 0[SB] 6: RETURN(0) 0 7: LOADL 9 8: STORE 0[SB] 9: CALL(SB) 2 10:POP(0) 1 11:HALT proc double() ~ n := n*2 n := n*2 n := 9 double()

More Related