Pid, #Add, #Mult, Etc.): Special Symbols Added to the Grammar to Signal the Need for Code Generation

Pid, #Add, #Mult, Etc.): Special Symbols Added to the Grammar to Signal the Need for Code Generation

Intermediate Code Generation Lecture 6 1 Announcements • PA2 – Due date was extended to (6 July) at 11:59pm • PA3 – Assigned tomorrow! 2 Recommended Reading P. Tremblay, J. Sorenson, Theory and Practice of Compiler Writing, McGraw Hill, Chapter 11. (Not required if you attend this lecture and the next Exercise session) 3 Intermediate Code Generation Parser Lexical Semantic Intermediate … Analyzer Analyzer Code Generator 4 Intermediate Representation • Translating source program into an “intermediate language.” ―Simple ―CPU Independent, ―…yet, close in spirit to machine language. • Three Address Code (quadruples) • Two Address Code, etc. 5 Three Address Codes • Statements of general form x:=y op z here represented as (op, y, z, x) • No built-up arithmetic expressions are allowed. • As a result, x:=y + z * w should be represented as: t1:=z * w t2:=y + t1 x:=t2 • Three-address code is useful: related to machine- language/ simple/ optimizable. 6 Types of Three-Address Statements x:=y op z (op, y, z, x) x:=op z (op, z, x, ) x:=z (:=, z, x, ) goto L (jp, L, ,) if x relop y goto L (relop, x, y, L), or (jpf, A1, A2, ), (jpt, A1, A2, ), etc. 7 Different Addressing Modes (+, 100, 101, 102) location 102 content of 100 + content of 101 (+, #100, 101, 102) location 102 constant 100 + content of 101 (+, @100, 101, 102) location 102 content of content of 100 + content of 101 8 Definitions • Action Symbols (eg., #pid, #add, #mult, etc.): special symbols added to the grammar to signal the need for code generation • Semantic Action (or, Semantic Routine): Each action symbol is associated with a sub-routine to perform • Semantic Stack (here referred to by “ss”): a stack dedicated to the both semantic analyzer and intermediate code generator to store and use the required information • Program Block (here referred to by “PB”): part of run time memory to be filled by the generated code 9 Run Time Memory Organization i 0 Code Program Block 100 Runtime Data Data Block Memory 500 Temporaries 10 Top-Down vs. Bottom-Up Generation • Intermediate Code Generation can be performed in a top-down or bottom-up fashion (depending on the parsing direction) • We first explain the Top-Down Approach • Then we explain the minor differences that exist between the two approaches 11 Top-Down Intermediate Code Generation PRODUCTION Rules with action symbols: 1. S #pid id := E #assign 2. E T E’ 3. E’ 4. E’ + T #add E’ 5. T F T’ 6. T’ 7. T’ * F #mult T’ 8. F ( E ) 9. F #pid id e.g., input: a := b + c * d 12 Code Generator Program Proc codegen(Action) • Function gettemp returns a case (Action) of new temporary variable that we can use. #pid : begin p findaddr(input); • Function findaddr(input) push(p) looks up the current input’s end address from Symbol Table. #add | #mult : begin t gettemp PB[i] (+ | *, ss(top), ss(top-1), t); i i + 1; pop(2); push(t) end #assign : begin PB[i] (:=, ss(top), ss(top-1),); i i + 1; pop(2) end end 13 End codegen Example S #pid id := E #assign E T E’ E’ | + T #add E’ T F T’ T’ | * F #mult T’ F ( E ) F #pid id Recursive Descent Parser Subroutines for S, E, E’, T, T’, and F : procedure S ; { if lookahead = id then {call codegen ( #pid ); call Match ( id ); call Match( ‘:=‘ ); call E; call codegen ( #assign )} else error } 14 Example S #pid id := E #assign procedure E ; E T E’ { if lookahead { id, ( } then E’ | + T #add E’ { call T; call E’ } T F T’ else error T’ | * F #mult T’ F ( E ) } F #pid id procedure E’ ; { if lookahead = ‘+’ then { call Match( ‘+’ ); call T; call codegen ( #add ); call E’ } else error } 15 Example S #pid id := E #assign procedure T ; E T E’ { if lookahead { id, ( } then E’ | + T #add E’ { call F; call T’ } T F T’ else error T’ | * F #mult T’ F ( E ) } F #pid id procedure T’ ; { if lookahead = ‘*’ then { call Match ( ‘*’ ); call F; call codegen ( #mult ); call T’ } else error } 16 Example S #pid id := E #assign E T E’ E’ | + T #add E’ T F T’ T’ | * F #mult T’ F ( E ) F #pid id procedure F ; { if lookahead = id then { call codegen ( #pid ); call Match ( id ) } else if lookahead = ‘(‘ then { call Match ( ‘(‘ ); call E; call Match ( ‘)‘ ) ; } else error } 17 Example (Cont.) • The order of Semantic Routines can be shown by the parse tree of the input sentence: Input: a := b + c * d id1 := id2 + id3 * id4 no lexeme address 1 a 100 2 b 101 3 C 102 4 d 103 Symbol Table 18 Example (Cont.) • Semantic Stack (SS) status during code generation: 102 101 101 100 100 100 103 102 500 101 101 501 100 100 100 • Program Block (PB) : i PB[i] Semantic Action called 0 (*,103,102,500) #mult 1 (+,500,101,501) #add 19 2 (=,501,100, ) #assign Example 2 • Suppose we only had the right hand side expression Input: b + c * d id2 + id3 * id4 no lexeme address 1 a 100 2 b 101 3 C 102 4 d 103 Symbol Table 20 Example 2 (Cont.) • A temporary memory address will remain in SS: i PB[i] Semantic Actions Input: 0 (*,103, 102, 500) #mult b + c * d 1 (+, 500, 101, 501) #add id2 + id3 * id4 2 no lexeme address 1 a 100 2 b 101 3 C 102 4 d 103 102 Symbol Table 101 101 103 102 500 21 101 101 501 Control statements (while) S while E do S end Input Example: while ● (b+c*d) ● do a := a+1 ● end ● • We need to find the appropriate place for inserting Semantic Actions symbols. 22 Control statements (while) S while E do S end 501 Input Example: i PB[i] Semantic Actions while ● (b+c*d) ● do 0 (*,103, 102, 500) #mult 1 (+, 500, 101, 501) #add a := a+1 2 ● end ● • After parsing E, the code for while’s condition has been generated; and the allocated temporary memory address will be on top of the Semantic Stack. 23 Control statements (while) S while E do S end 501 Input Example: i PB[i] Semantic Actions while ● (b+c*d) ● do 0 (*,103, 102, 500) #mult 1 (+, 500, 101, 501) #add a := a+1 2 ● end ● • This is done by the semantic routines of non-terminal E 24 Control statements (while) S while E do S end Input Example: • We need a conditional jump, based on the result of while’s condition, to outside of the loop. while ● (b+c*d) ● do a := a+1 ● end • But, we haven’t yet compiled the loop body and thus don’t know where the ● end of loop is! 25 Control statements (while) #save S while E do S end Input Example: • Solution: BACKPATCHING! while ● (b+c*d) ● do #save: begin a := a+1 push(i) ● end i i + 1 ● end • Reserve a place in the program block for the jump, and generate the code at the end of the loop 26 Control statements (while) #label S while E do #save S end • We also need an unconditional Input Example: jump back to while condition. while ● (b+c*d) ● do • Target of the unconditional jump a := a+1 must be saved in SS ● end • This is done by a semantic ● action; let’s call it #label #label: begin push(i) end 27 Control statements (while) #while S while #label E do #save S end At the end of while, the destination of Input Example: conditional jump is known. So, the place saved by #save can be filled by #while. while ● (b+c*d) ● do An unconditional jump to the start of a := a+1 expression (saved by #label) is ● end generated, too. ● #while: begin PB[ss(top)] (jpf, ss(top-1), i+1, ); PB[i] (jp, ss(top-2), , ); i i + 1; Pop(3) 28 end Control statements (while) S while #label E do #save S #while end i PB[i] Semantic Actions Input Example: 0 1 while ● (b+c*d) ● do 2 a := a+1 3 4 ● end 5 ● 6 29 Control statements (while) S while #label E do #save S #while end i PB[i] Semantic Actions Input Example: 0 1 while ● (b+c*d) ● do 2 a := a+1 3 4 ● end 5 ● 6 After #label 30 0 Control statements (while) S while #label E do #save S #while end i PB[i] Semantic Actions Input Example: 0 (*,103, 102, 500) by E 1 (+, 500, 101, 501) by E while ● (b+c*d) ● do 2 a := a+1 3 4 ● end 5 ● 6 After #label After E 501 31 0 b+c*d 0 Control statements (while) S while #label E do #save S #while end i PB[i] Semantic Actions Input Example: 0 (*,103, 102, 500) by E 1 (+, 500, 101, 501) by E while ● (b+c*d) ● do 2 a := a+1 3 4 ● end 5 ● 6 2 501 After 501 32 0 #save 0 Control statements (while) S while #label E do #save S #while end i PB[i] Semantic Actions Input Example: 0 (*,103, 102, 500) by E 1 (+, 500, 101, 501) by E while ● (b+c*d) ● do 2 a := a+1 3 (+, 100, #1, 503) by S 4 (=, 503, 100, ) by S ● end 5 ● 6 2 2 501 After After S 501 501 33 0 #save 0 0 Control statements (while) S while #label E do #save S #while end i PB[i] Semantic Actions Input Example: 0 (*,103, 102, 500) by E 1 (+, 500, 101, 501) by E while ● (b+c*d) ● do 2 (jpf, 501, 6, ) #while a := a+1 3 (+, 100, #1, 503) by S 4 (=, 503, 100, ) by S ● end 5 (jp, 0, , ) #while ● 6 2 501 After 34 0 #while Conditional Statements (if-then-else) S if E then S S’ S’ else S Input Example: S’ If (a+b) ● then a := a+1 else ●● b:= b+1 ● 35 Conditional Statements (if-then-else) S if E #save then S S’ S’ else S Input Example: S’ If (a+b) ● then a := a+1 Conditional jump: A place for jump else ●● b:= b+1 ● should be saved by #save and to be later filled (by back patching).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    57 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us