compilerconstructie
play

Compilerconstructie najaar 2012 - PowerPoint PPT Presentation

Compilerconstructie najaar 2012 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs.nl college 6, dinsdag 23 oktober 2012 Intermediate Code Generation 1 6. Intermediate Code


  1. Compilerconstructie najaar 2012 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs.nl college 6, dinsdag 23 oktober 2012 Intermediate Code Generation 1

  2. 6. Intermediate Code Generation • Front end: generates intermediate representation • Back end: generates target code Intermediate Static intermediate Code ✲ ✲ ✲ ✲ ✲ Code Parser Checker Generator code Generator ✲ ✛ front end back end 2

  3. Intermediate Representation • Facilitates efficient compiler suites: m + n instead of m ∗ n • Different types, e.g., – syntax trees – three-address code: x = y op z • High-level vs. low-level • C for C++ High Level Low Level Source ✲ Target ✲ . . . ✲ ✲ Intermediate Intermediate Program Code Representation Representation 3

  4. 6.2 Three-Address Code • Linearized representation of syntax tree / syntax DAG • Sequence of instructions: x = y op z Example: a + a ∗ ( b − c ) + ( b − c ) ∗ d Syntax DAG Three-address code t1 = b - c + ✟ ❍❍❍❍ ✟ ✟ t2 = a * t1 ✟ ✟ ❍ + ∗ t3 = a * t2 ❅ � ❅ ❅ ❅ � ∗ d � t4 = t1 * d � � ❅ � ❅ � a − t5 = t3 + t4 � ❅ � ❅ c b 4

  5. Addresses At most three addresses per instruction • Name: source program name / symbol-table entry • Constant • Compiler-generated temporary: distinct names 5

  6. Three-Address Instructions 1 Assignment instructions x = y op z 2 Assignment instructions x = op y 3 Copy instructions x = y 4 Unconditional jumps goto L 5 Conditional jumps if x goto L / ifFalse x goto L 6 Conditional jumps if x relop y goto L / ifFalse . . . 7 Procedure calls and returns param x 1 param x 2 . . . param x n call p, n return y 8 Indexed copy instructions x = y [ i ] / x [ i ] = y 9 Address and pointer assignments x = & y , x = ∗ y , ∗ x = y Symbolic lable L represents index of instruction 6

  7. Three-Address Instructions (Example) do i = i+1; while (a[i] < v); Syntax tree. . . Two examples of possible translations: Symbolic labels Position numbers L: t1 = i+1 100: t1 = i+1 i = t1 101: i = t1 t2 = i * 8 102: t2 = i * 8 t3 = a [ t2 ] 103: t3 = a [ t2 ] if t3 < v goto L 104: if t3 < v goto 100 7

  8. Implementation of Three-Address Instructions Quadruples: records op , vararg 1 , vararg 2 , result Example: a = b * - c + b * - c Syntax tree. . . vararg 1 vararg 2 op result Three-address code 0 minus c t 1 t1 = minus c 1 ∗ b t 1 t 2 t2 = b * t1 2 minus c t 3 t3 = minus c 3 ∗ b t 3 t 4 t4 = b * t3 4 + t 2 t 4 t 5 t5 = t2 + t4 5 = t 5 a . . . a = t5 8

  9. Implementation of Three-Address Instructions vararg 1 vararg 2 Three-address code op result 0 minus c t 1 t1 = minus c 1 ∗ b t 1 t 2 t2 = b * t1 2 minus c t 3 t3 = minus c 3 ∗ b t 3 t 4 t4 = b * t3 4 + t 2 t 4 t 5 t5 = t2 + t4 5 = t 5 a . . . a = t5 Exceptions 1. minus , = 2. param 3. jumps Field result mainly for temporaries. . . 9

  10. Implementation of Three-Address Instructions Triples: records op , vararg 1 , vararg 2 Example: a = b * - c + b * - c Syntax tree. . . vararg 1 vararg 2 op Three-address code 0 minus c t1 = minus c 1 ∗ (0) b t2 = b * t1 2 minus c t3 = minus c 3 ∗ (2) b t4 = b * t3 4 + (1) (3) t5 = t2 + t4 5 = a t 5 . . . a = t5 10

  11. Implementation of Three-Address Instructions vararg 1 vararg 2 op Three-address code 0 minus c t1 = minus c 1 ∗ (0) b t2 = b * t1 2 minus c t3 = minus c 3 ∗ (2) b t4 = b * t3 4 + (1) (3) t5 = t2 + t4 5 = a t 5 . . . a = t5 Equivalent to DAG Special case: x [ i ] = y or x = y [ i ] Pro: temporaries are implicit Con: difficult to rearrange code 11

  12. Implementation of Three-Address Instructions Indirect triples: pointers to triples Example: a = b * - c + b * - c Syntax tree. . . vararg 1 vararg 2 op instruction Three-address code 0 minus 35 (0) c t1 = minus c 1 ∗ (0) 36 (1) b t2 = b * t1 37 (2) 2 minus c t3 = minus c 38 (3) 3 ∗ (2) b t4 = b * t3 39 (4) 4 + (1) (3) t5 = t2 + t4 40 (5) 5 = (4) a . . . a = t5 . . . 12

  13. 6.3.3 Declarations • Three-address code is simplistic It assumes that names of variables can be easily resolved by the back end in global or local variables • We need symbol tables to record global and local declarations in procedures, blocks, and structs to resolve names • Symbol table contains type and relative adress of names Example: → T id ; D | D ǫ record ′ { ′ D ′ } ′ T → B C | int float B → | [ num ] C C → ǫ | 13

  14. Structure of Types (Example) record ′ { ′ D ′ } ′ T → B C | int float B → | → | [ num ] C C ǫ int[2][3] T ✏ PPPPPPPPPPP ✏ ✏ ✏ ✏ ✏ ✏ ✏ ✏ ✏ ✏ ✏ P B C ✟ ❍❍❍❍❍❍ ✟ ✟ ✟ ✟ ✟ ❍ [ 2 ] int C ✟ ❍❍❍❍❍❍ ✟ ✟ ✟ ✟ ✟ ❍ [ 3 ] C ǫ 14

  15. Storage Layout at Compile Time • Storage comes in blocks of contiguous bytes • Width of type is number of bytes needed → { t = B. type ; w = B. width ; } T B { T. type = C. type ; T. width = C. width ; } C → int { B. type = integer ; B. width = 4; } B float B. type = float ; B. width = 8; } B → { C. type = t ; C. width = w ; } C → ǫ { [ num ] C 1 C. type = array ( num . value , C 1 . type ); C → { C. width = num . value × C 1 . width ; } 15

  16. Types and Their Widths (Example) t = B. type ; w = B. width ; } T → B { { T. type = C. type ; T. width = C. width ; } C → int { B. type = integer ; B. width = 4; } B → float { B. type = float ; B. width = 8; } B → { C. type = t ; C. width = w ; } C ǫ → [ num ] C 1 { C. type = array ( num . value , C 1 . type ); C C. width = num . value × C 1 . width ; } type = array (2 , array (3 , integer )) ❍ ❨ width =24 ❍ . .................. . T . ❍ . . ❍ . . . . . . . type = array (2 , array (3 , integer )) ③ . . t . . . . ❍ ❨ width =24 ❍ B type = integer . C ............ w ❍ . . . ❍ . . . . width =4 . . . type = array (3 , integer ) . . . width =12 ❍ ❨ ❍ [ 2 ] int . C ............ ❍ . . ❍ . . . . . type = integer . . width =4 [ 3 ] C . . ✸ . . ǫ 16

  17. Sequences of Declarations → T id ; D | D ǫ Use offset as next available address → { offset = 0; } P D → T id ; { top . put ( id . lexeme , T. type , offset ); D offset = offset + T. width ; } D 1 → D ǫ 17

  18. Fields in Records and Classes Example float x; record { float x; float y; } p; record { int tag; float x; float y; } q; x = p.x + q.x; → T id ; D | D ǫ record ′ { ′ D ′ } ′ → T • Fields are specified by sequence of declarations – Field names within record must be distinct – Relative address for field is relative to data area for that record 18

  19. Fields in Records and Classes Stored in separate symbol table t Record type has form record ( t ) record ′ { ′ → { Env . push ( top ); top = new Env (); T Stack . push ( offset ); offset = 0; } D ′ } ′ { T. type = record ( top ); T. width = offset ; top = Env . pop (); offset = Stack . pop (); } 19

  20. 6.4 Translation of Expressions • Temporary names are created E → E 1 + E 2 yields t = E 1 + E 2 , e.g., t5 = t2 + t4 a = t5 • If expression is identifier, then no new temporary • Nonterminal E has two attributes: – E. addr – address that will hold value of E – E. code – three-address code sequence 20

  21. Syntax-Directed Definition To produce three-address code for assignments Production Semantic Rules → id = E ; S. code = E. code || S gen ( top . get ( id . lexeme ) ′ = ′ E. addr ) → E 1 + E 2 E. addr = new Temp () E E. code = E 1 . code || E 2 . code || gen ( E. addr ′ = ′ E 1 . addr ′ + ′ E 2 . addr ) E. addr = new Temp () | − E 1 E. code = E 1 . code || gen ( E. addr ′ = ′ ′ minus ′ E 1 . addr ) | ( E 1 ) E. addr = E 1 . addr E. code = E 1 . code | id E. addr = top . get ( id . lexeme ) ′′ E. code = 21

  22. Translation scheme To incrementally produce three-address code for assignments gen ( top . get ( id . lexeme ) ′ = ′ E. addr ); } → id = E ; { S → E 1 + E 2 { E. addr = new Temp (); E gen ( E. addr ′ = ′ E 1 . addr ′ + ′ E 2 . addr ); } | − E 1 { E. addr = new Temp (); gen ( E. addr ′ = ′ ′ minus ′ E 1 . addr ); } | ( E 1 ) { E. addr = E 1 . addr ; } id E. addr = top . get ( id . lexeme ); } | { 22

  23. Addressing Array Elements • Array A [ n ] with elements at positions 0 , 1 , . . . , n − 1 • Let – w be width of array element – base be relative address of storage allocated for A (= A [0]) Element A [ i ] begins in location base + i × w • In two dimensions, let – w 1 be width of row, – w 2 be width of element of row Element A [ i ][ j ] begins in location base + i × w 1 + j × w 2 • In k dimensions base + i 1 ∗ w 1 + i 2 ∗ w 2 + · · · + i k ∗ w k 23

  24. Translation of Array References L generates array name followed by sequence of index expressions L → L [ E ] | id [ E ] Three synthesized attributes • L. addr : temporary used to compute location in array • L. array : pointer to symbol-table entry for array name – L. array . base : base address of array • L. type : type of subarray generated by L – For type t : t. width – For array type t : t. elem 24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend