compiler construction
play

Compiler Construction Lecture 17: Code Generation III - PowerPoint PPT Presentation

Compiler Construction Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures) Winter Semester 2018/19 Thomas Noll Software Modeling and Verification Group RWTH Aachen University


  1. Compiler Construction Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures) Winter Semester 2018/19 Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ws-1819/cc/

  2. Recap: Syntax of EPL Outline of Lecture 17 Recap: Syntax of EPL Boolean Expressions with Sequential Semantics Implementation of Data Structures Static Data Structures Modifying the Abstract Machine Modifying the Symbol Table Modifying the Translation A Translation Example 2 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  3. Recap: Syntax of EPL Syntax of EPL Definition (Syntax of EPL) The syntax of EPL is defined as follows: Z : z (* z is an integer *) Ide : I (* I is an identifier *) A ::= z | I | A 1 + A 2 | . . . AExp : B ::= A 1 < A 2 | not B | B 1 and B 2 | B 1 or B 2 BExp : Cmd : C ::= I := A | C 1 ; C 2 | if B then C 1 else C 2 | while B do C | I () Dcl : D ::= D C D V D P D C ::= ε | const I 1 := z 1 , . . . , I n := z n ; D V ::= ε | var I 1 , . . . , I n ; D P ::= ε | proc I 1 ; K 1 ; . . . ;proc I n ; K n ; Blk : K ::= D C Pgm : P ::= in/out I 1 , . . . , I n ; K . 3 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  4. Recap: Syntax of EPL Translation of Boolean Expressions Definition (Translation of Boolean expressions) The mapping bt : BExp × Tab × PC × Lev ��� AM (“Boolean expression translation”) is defined by bt ( A 1 < A 2 , st , a , lev ) := at ( A 1 , st , a , lev ); at ( A 2 , st , a ′ , lev ); a ′′ : LT; bt ( not B , st , a , lev ) := bt ( B , st , a , lev ); a ′ : NOT; bt ( B 1 and B 2 , st , a , lev ) := bt ( B 1 , st , a , lev ); bt ( B 2 , st , a ′ , lev ); a ′′ : AND; bt ( B 1 or B 2 , st , a , lev ) := bt ( B 1 , st , a , lev ); bt ( B 2 , st , a ′ , lev ); a ′′ : OR; 4 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  5. Boolean Expressions with Sequential Semantics Outline of Lecture 17 Recap: Syntax of EPL Boolean Expressions with Sequential Semantics Implementation of Data Structures Static Data Structures Modifying the Abstract Machine Modifying the Symbol Table Modifying the Translation A Translation Example 5 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  6. Boolean Expressions with Sequential Semantics Boolean Expressions with Sequential Semantics So far: Boolean expressions with strict semantics ( ⊥ = nontermination/runtime error) b 1 ∧ ∨ ⊥ = ⊥ ⊥ ∧ ∨ b 2 = ⊥ 6 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  7. Boolean Expressions with Sequential Semantics Boolean Expressions with Sequential Semantics So far: Boolean expressions with strict semantics ( ⊥ = nontermination/runtime error) b 1 ∧ ∨ ⊥ = ⊥ ⊥ ∧ ∨ b 2 = ⊥ Now: Boolean expressions with sequential semantics (“short-circuit evaluation”) b 1 ∧ b 2 ˆ = if b 1 then b 2 else false = ⇒ false ∧ ⊥ = false b 1 ∨ b 2 ˆ ⇒ true ∨ ⊥ = true = if b 1 then true else b 2 = (and ⊥ ∧ ∨ b = ⊥ ) 6 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  8. Boolean Expressions with Sequential Semantics Boolean Expressions with Sequential Semantics So far: Boolean expressions with strict semantics ( ⊥ = nontermination/runtime error) b 1 ∧ ∨ ⊥ = ⊥ ⊥ ∧ ∨ b 2 = ⊥ Now: Boolean expressions with sequential semantics (“short-circuit evaluation”) b 1 ∧ b 2 ˆ = if b 1 then b 2 else false = ⇒ false ∧ ⊥ = false b 1 ∨ b 2 ˆ ⇒ true ∨ ⊥ = true = if b 1 then true else b 2 = (and ⊥ ∧ ∨ b = ⊥ ) Implementation: • employ branching instructions rather than Boolean operations (“jumping code”) • equip bt and ct with two additional address parameters: a t : target address for true a f : target address for false 6 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  9. Boolean Expressions with Sequential Semantics Jumping Code for Boolean Expressions Definition 17.1 (Jumping code for Boolean expressions) The mapping sbt : BExp × Tab × PC 3 × Lev ��� AM (“sequential Boolean expression translation”) is defined by sbt ( A 1 < A 2 , st , a , a t , a f , l ) := at ( A 1 , st , a , l ) at ( A 2 , st , a ′ , l ) a ′′ : LT; a ′′ + 1 : JFALSE( a f ); a ′′ + 2 : JMP( a t ); sbt ( not B , st , a , a t , a f , l ) := sbt ( B , st , a , a f , a t , l ) sbt ( B 1 and B 2 , st , a , a t , a f , l ) := sbt ( B 1 , st , a , a ′ , a f , l ) sbt ( B 2 , st , a ′ , a t , a f , l ) sbt ( B 1 or B 2 , st , a , a t , a f , l ) := sbt ( B 1 , st , a , a t , a ′ , l ) sbt ( B 2 , st , a ′ , a t , a f , l ) 7 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  10. Boolean Expressions with Sequential Semantics Jumping Code for Commands Definition 17.2 (Jumping code for commands) The mapping sct : Cmd × Tab × PC × Lev ��� AM (“sequential command translation”) is defined by sct ( if B then C 1 else C 2 , st , a , l ) := sbt ( B , st , a , a t , a f , l ) sct ( C 1 , st , a t , l ) a f − 1 : JMP( a ′ ); sct ( C 2 , st , a f , l ) a ′ : sct ( while B do C , st , a , l ) := sbt ( B , st , a , a t , a f , l ) sct ( C , st , a t , l ) a f − 1 : JMP( a ); a f : (remaining cases analogously) 8 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  11. Boolean Expressions with Sequential Semantics Example: Jumping Code Example 17.3 Translation of while not (x < 1) and (x < y) do C : 9 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  12. Boolean Expressions with Sequential Semantics Example: Jumping Code Example 17.3 Translation of while not (x < 1) and (x < y) do C : Strict: 1 : LOAD(x); LIT(1); LT; NOT; LOAD(x); LOAD(y); LT; AND; JFALSE( a ); ct ( C , . . . ) JMP(1); a : . . . 9 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  13. Boolean Expressions with Sequential Semantics Example: Jumping Code Example 17.3 Translation of while not (x < 1) and (x < y) do C : Strict: Sequential: 1 : LOAD(x); 1 : LOAD(x); LIT(1); LIT(1); LT; LT; NOT; JFALSE(6); LOAD(x); JMP( a ); LOAD(y); 6 : LOAD(x); LT; LOAD(y); AND; LT; JFALSE( a ); JFALSE( a ); ct ( C , . . . ) JMP(11); JMP(1); 11 : sct ( C , . . . ) a : . . . JMP(1); a : . . . 9 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  14. Boolean Expressions with Sequential Semantics Example: Jumping Code Example 17.3 Translation of while not (x < 1) and (x < y) do C : Strict: Sequential: 1 : LOAD(x); 1 : LOAD(x); LIT(1); LIT(1); LT; LT; NOT; JFALSE(6); LOAD(x); JMP( a ); LOAD(y); If x = 0: 6 : LOAD(x); LT; 9 instructions executed LOAD(y); AND; LT; JFALSE( a ); JFALSE( a ); ct ( C , . . . ) JMP(11); JMP(1); 11 : sct ( C , . . . ) a : . . . JMP(1); a : . . . 9 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  15. Boolean Expressions with Sequential Semantics Example: Jumping Code Example 17.3 Translation of while not (x < 1) and (x < y) do C : Strict: Sequential: 1 : LOAD(x); 1 : LOAD(x); LIT(1); LIT(1); LT; LT; NOT; JFALSE(6); LOAD(x); JMP( a ); LOAD(y); If x = 0: 6 : LOAD(x); If x = 0: LT; 9 instructions executed LOAD(y); 5 instructions executed AND; LT; JFALSE( a ); JFALSE( a ); ct ( C , . . . ) JMP(11); JMP(1); 11 : sct ( C , . . . ) a : . . . JMP(1); a : . . . 9 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  16. Boolean Expressions with Sequential Semantics Example: Jumping Code Example 17.3 Translation of while not (x < 1) and (x < y) do C : Strict: Sequential: 1 : LOAD(x); 1 : LOAD(x); LIT(1); LIT(1); LT; LT; NOT; JFALSE(6); LOAD(x); JMP( a ); LOAD(y); If x = 0: 6 : LOAD(x); If x = 0: LT; 9 instructions executed LOAD(y); 5 instructions executed AND; LT; JFALSE( a ); JFALSE( a ); ct ( C , . . . ) JMP(11); JMP(1); 11 : sct ( C , . . . ) a : . . . JMP(1); a : . . . = ⇒ generally: longer code, but shorter executions 9 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

  17. Implementation of Data Structures Outline of Lecture 17 Recap: Syntax of EPL Boolean Expressions with Sequential Semantics Implementation of Data Structures Static Data Structures Modifying the Abstract Machine Modifying the Symbol Table Modifying the Translation A Translation Example 10 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

Recommend


More recommend