1 / 26

Course Overview

Course Overview. PART I: overview material 1 Introduction 2 Language processors (tombstone diagrams, bootstrapping) 3 Architecture of a compiler PART II: inside a compiler 4 Syntax analysis 5 Contextual analysis 6 Runtime organization 7 Code generation PART III: conclusion

Download Presentation

Course Overview

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. Course Overview PART I: overview material 1 Introduction 2 Language processors (tombstone diagrams, bootstrapping) 3 Architecture of a compiler PART II: inside a compiler 4 Syntax analysis 5 Contextual analysis 6 Runtime organization 7 Code generation PART III: conclusion • Interpretation 9 Review

  2. 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

  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 This chapter Object Code

  4. input input input output output output Source Text AST Decorated AST Object Code 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 calls This chapter 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. (Phrases are syntactic constructs such as commands, expressions, etc.) • Storage Allocation Deciding the storage address for each variable in the source program. (Static allocation, stack, heap, etc.) • Register Allocation (for register-based machines) How to use registers efficiently to store intermediate results. We’ll use a stack-based machine, so this is not an issue for us.

  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 0[SB]LOADL 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 the phrases in the source language. => Code generation follows the same “inductive” structure.

  7. “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 sub-structures. Example: Sequential Command code generation • Semantic specification • The sequential commandC1;C2is executed as follows: first C1 is executed then C2is executed. • Code generation function: • execute : Command -> Instruction* • execute [C1;C2] = • execute [C1] • execute [C2] instructions for C1 instructions for C1;C2 instructions for C2

  8. “Inductive” Code Generation Example: Assignment Command code generation • Code generation function: • execute [I:=E] = • evaluate [E] • STOREaddress [I] instructions for Eyield value for E on top of the stack 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 sub-phrases are translated.

  9. “Inductive” Code Generation Example: code generation for a larger phrase in terms of its subphrases. LOAD f LOAD n CALL mult STORE f execute [f := f*n] execute [f := f*n; n := n-1] LOAD n CALL pred STORE n execute [n := n-1]

  10. Specifying Code Generation with Code Templates • For each “phrase class” P in the abstract syntax of the source language: • Define code function fP : P -> Instruction* • that translates each phrase in class P to object code. We specify the function fP by code templates. Typically they look like: fP […Q…R…] = … fQ [Q] … fR [R] … Note: A “phrase class” typically corresponds to a non-terminal of the abstract syntax. This in turn corresponds to an abstract class in the Java/C++ classes that implement the AST nodes. (For example: Expression, Command, Declaration, etc.)

  11. 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

  12. 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. Start and finish with empty stack. Execute command C. May update variables but does not shrink or grow the stack! Evaluate expression E. Net result is pushing the value of E onto the stack. Push the value of constant or variable onto the stack. Pop value from stack and store in variable V. Elaborate declaration D. Make space on the stack for constants and variables in D. run P executeC evaluateE fetchV assignV elaborate D Program Command Expres- sion V-name V-name Decla-ration

  13. Code Generation with Code Templates 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

  14. E C1 g: C2 h: Code Generation with Code Templates 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:

  15. C E Code Generation with Code Templates Commands: • execute [whileE doC] = • JUMP h • g: execute [C] • h: evaluate[E] • JUMPIF(1) g • execute [let D inC] = • elaborate[D] • execute [C] • POP(0) s if s > 0where s= amount of storage allocated by D

  16. Code Generation with Code Templates Expressions: • evaluate [IL] = note: IL is an integer literal • LOADL vwhere v= the integer value of IL • evaluate [V] = note: V is variable name • fetch[V] • evaluate [O E] = note: O is a unary operator • evaluate[E] • CALL pwhere p= address of routine for O • evaluate [E1O E2] = note: O is a binary operator • evaluate[E1] • evaluate[E2] • CALL pwhere p= address of routine for O

  17. Code Generation with Code Templates Variables: Note: Mini Triangle only needs static allocation. Q: Why not stack allocation? Q: Why not heap allocation? 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

  18. Code Generation with Code Templates Declarations: elaborate [const I ~E] = evaluate[E] elaborate [var I :T] = PUSH swhere s= size of T elaborate [D1 ;D2] = elaborate [D1] elaborate [D2] THE END: these are all the code templates for Mini Triangle. Now let’s put them to use in an example.

  19. Example of Mini Triangle Code Generation • execute [while i>0 do i:=i–2] = • JUMP h • g: LOAD i • LOADL 2 • CALL sub • STORE i • h: LOAD i • LOADL 0 • CALL gt • JUMPIF(1) g evaluate [i–2] execute [i:=i–2] evaluate [i>0] Q: Show how we get to this end result by successively applying the code templates we defined. Picture shows a few steps but not all. Fill in the missing details.

  20. Alternative Code for While Loop • execute [while i>0 do i:=i–2] = • g: LOAD i • LOADL 0 • CALL gt • JUMPIF(0) h • LOAD i • LOADL 2 • CALL sub • STORE i • JUMP g • h: evaluate [i>0] evaluate [i–2] execute [i:=i–2] Q: Which is better and why?

  21. Example of Mini Triangle Code Generation • execute [let var i: Integer in i:=i+3] = • PUSH 1 • LOAD i • LOADL 3 • CALL add • STORE i • POP(0) 1 Note: This only shows the “end result”. Q: Show how we get to this end result by successively applying the code templates defined previously.

  22. 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 => use special case code templates. Example: • evaluate [i+1] = • LOAD i • LOADL 1 • CALL add • evaluate [i+1] = • LOAD i • CALL succ more efficient code for the special case “+1” what we get with the “general” code templates

  23. Special Case Code Templates Example: some special case code template for “+1”, “-1”, … evaluate [E+ 1] = evaluate [E] CALL succ 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 phrases of a special form. Such phrases are also covered by a more general form.

  24. Special Case Code Templates Example: “In-lining” known constants. • execute [let const n~7; • var i:Integer • in i:=n*n] = • LOADL 7 • PUSH 1 • LOAD n • LOAD n • CALL mult • STORE i • POP(0) 2 elaborate [const n~7] elaborate [var i:Integer] execute [i:=n*n] This is how the code looks like with no special case templates.

  25. Special Case Code Templates Example: “In-lining” known constants. Special case templates for inlining literals. elaborate [const I ~IL] = no code • fetch [I] = special case if Iis a known literal constant • LOADL vwhere v is the known value of I • execute[let const n~7; var i:Integer in i:=n*n] = • PUSH 1 • LOADL 7 • LOADL 7 • CALL mult • STORE i • POP(0) 1

  26. 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 next look at how to implement a Mini Triangle code generator in Java (or C++). Use the visitor design pattern!

More Related