compiler construction
play

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

Compiler Construction Lecture 17: Code Generation III (Implementation of Static Data Structures) Thomas Noll Lehrstuhl f ur Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de


  1. Compiler Construction Lecture 17: Code Generation III (Implementation of Static Data Structures) Thomas Noll Lehrstuhl f¨ ur Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de http://moves.rwth-aachen.de/teaching/ss-14/cc14/ Summer Semester 2014

  2. Outline Recap: Syntax of EPL 1 Implementation of Data Structures 2 Static Data Structures 3 Modifying the Abstract Machine 4 Translation of Static Data Structures into AM Programs 5 A Translation Example 6 Compiler Construction Summer Semester 2014 17.3

  3. Syntax of EPL Definition (Syntax of EPL) The syntax of EPL is defined as follows: Z : (* z is an integer *) z Ide : (* I is an identifier *) I AExp : A ::= z | I | A 1 + A 2 | . . . BExp : B ::= A 1 < A 2 | not B | B 1 and B 2 | B 1 or B 2 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 . Compiler Construction Summer Semester 2014 17.4

  4. Outline Recap: Syntax of EPL 1 Implementation of Data Structures 2 Static Data Structures 3 Modifying the Abstract Machine 4 Translation of Static Data Structures into AM Programs 5 A Translation Example 6 Compiler Construction Summer Semester 2014 17.5

  5. Implementation of Data Structures Source code: data structures = arrays, records, lists, trees, ... = ⇒ structured state space, variables with components Abstract machine: linear memory structure, cells for storing atomic data Translation: mapping of structured state space to linear memory ( = ⇒ address computation) static data structures: memory reqirements known at compile time dynamic data structures: memory reqirements runtime dependent = ⇒ heap, pointers, garbage collection, ... First step: static data structures (arrays and records) inductive type definitions no procedures (for simplification; “orthogonal” extension) Compiler Construction Summer Semester 2014 17.6

  6. Outline Recap: Syntax of EPL 1 Implementation of Data Structures 2 Static Data Structures 3 Modifying the Abstract Machine 4 Translation of Static Data Structures into AM Programs 5 A Translation Example 6 Compiler Construction Summer Semester 2014 17.7

  7. Modified Syntax of EPL Definition 17.1 (Modified syntax of EPL) The modified syntax of EPL is defined as follows (where n ≥ 1): Z : (* z is an integer *) z B : b ::= true | false (* b is a Boolean *) R : r (* r is a real number *) Con : c ::= z | b | r (* c is a constant *) Ide : I (* I is an identifier *) Type : T ::= bool | int | real | I | array[ z 1 .. z 2 ] of T | record I 1 : T 1 ; . . . ; I n : T n end Var : V ::= I | V [ E ] | V . I Exp : E ::= c | V | E 1 + E 2 | E 1 < E 2 | E 1 and E 2 | . . . Cmd : C ::= V := E | C 1 ; C 2 | if E then C 1 else C 2 | while E do C Dcl : D ::= D C D T D V D C ::= ε | const I 1 := c 1 ; . . . ; I n := c n ; D T ::= ε | type I 1 := T 1 ; . . . ; I n := T n ; D V ::= ε | var I 1 : T 1 ; . . . ; I n : T n ; Pgm : P ::= D C Compiler Construction Summer Semester 2014 17.8

  8. Static Semantics I All identifiers in a declaration D have to be different. In T = record I 1 : T 1 ; . . . ; I n : T n end , all selectors I j must be different. In T = array[ z 1 .. z 2 ] of T , z 1 ≤ z 2 . Type definitions must not be recursive: if D T = type I 1 := T 1 ; . . . ; I n := T n ; and type identifier I occurs in T j , then I ∈ { I 1 , . . . , I j − 1 } . All type identifiers used in in a variable declaration D V must be declared in D T . Every identifier used in a command C must be declared in D (as a constant or variable). Variables in expressions and assignments have a base type ( bool / int / real ; possibly via type identifiers). Compiler Construction Summer Semester 2014 17.9

  9. Static Semantics II Array indices must have type int . Execution conditions ( while ) and branching expressions ( if ) must have type bool . The types of the left-hand side and of the right-hand side types of an assignment must be compatible. Type compatibility: Z ⊆ R in mathematics, but not on computers (different representation) = ⇒ type casts weak typing: implicit casting by compiler ( 2.5 + 1 , 1 + "42" ) = ⇒ risc of undetected “real” errors; for programming-in-the-small (script languages) strong typing: explicit casting by programmer = ⇒ enhanced software reliability; for programming-in-the-large Instantiation of operators/functions/procedures/... for different parameter types: polymorphism or overloading + : int × int → int + : real × real → real Compiler Construction Summer Semester 2014 17.10

  10. Outline Recap: Syntax of EPL 1 Implementation of Data Structures 2 Static Data Structures 3 Modifying the Abstract Machine 4 Translation of Static Data Structures into AM Programs 5 A Translation Example 6 Compiler Construction Summer Semester 2014 17.11

  11. The Modified Abstract Machine AM Additional main storage for keeping data values Procedure stack not required anymore (as procedures no longer supported) Definition 17.2 (Modified abstract machine for EPL) The modified abstract machine for EPL (AM) is defined by the state space S := PC × DS × MS with the program counter PC := N , the data stack DS := R ∗ (top to the right), and the main storage MS := { σ | σ : N → R } . Compiler Construction Summer Semester 2014 17.12

  12. New AM Instructions Definition 17.3 (New AM instructions) Procedure instructions are no longer needed. Transfer instructions ( LOAD( dif , off ) , STORE( dif , off ) ) are replaced by the following instructions with the respective semantics � O � : S ��� S : � LOAD � ( a , d : n , σ ) := ( a + 1 , d : σ ( n ) , σ ) if n ∈ N � STORE � ( a , d : n : r , σ ) := ( a + 1 , d , σ [ n �→ r ]) if n ∈ N Moreover the following instruction for checking array bounds is introduced: � ( a + 1 , d : z , σ ) if z ∈ { z 1 , . . . , z 2 } (0 , d : , σ ) otherwise � CAB( z 1 , z 2 ) � ( a , d : z , σ ) := RTE ���� runtime error Compiler Construction Summer Semester 2014 17.13

  13. Outline Recap: Syntax of EPL 1 Implementation of Data Structures 2 Static Data Structures 3 Modifying the Abstract Machine 4 Translation of Static Data Structures into AM Programs 5 A Translation Example 6 Compiler Construction Summer Semester 2014 17.14

  14. Modifying the Symbol Table Tab := { st | st : Ide ��� ( { const } × ( B ∪ Z ∪ R )) ∪ ( { var } × Ide × N ) ∪ ( { type } × { bool , int , real } × { 1 } ) ∪ ( { type } × { array } × Z 2 × Ide × N ) ∪ ( { type } × { record } × ( Ide 2 × N ) ∗ × N ) } Remarks: Variable descriptor ( var , I , n ): type I , memory address n Last component of type entry: memory requirement (base types: 1 “cell”) Array descriptor ( type , array , z 1 , z 2 , I , n ): bounds z 1 , z 2 , component type I Record descriptor ( type , record , I 1 , J 1 , o 1 , . . . , I l , J l , o l , n ): selector I k , component type J k , memory offset o k “Indexed” table lookup: st ( I . I k ) := ( J k , o k ) if st ( I ) = ( type , record , . . . , I k , J k , o k , . . . , n ) Compiler Construction Summer Semester 2014 17.15

  15. Maintaining the Symbol Table I The symbol table is again maintained by the function update ( D , st ) which specifies the update of symbol table st according to declaration D . For the sake of simplicity we assume that D = D C D T D V ∈ Dcl is flattened, i.e., that every subtype is named by an identifier: If D T = type I 1 := T 1 ; . . . ; I n := T n ; , then for every k ∈ [ n ] T k ∈ { bool , int , real } or T k ∈ { I 1 , . . . , I k − 1 } or T k = array[ z 1 .. z 2 ] of I j where j ∈ [ k − 1] or T k = record J 1 : I j 1 ; . . . ; J l : I j l end where j 1 , . . . , j l ∈ [ k − 1] For D T as above, D V must be of the form D V = var J 1 : I j 1 ; . . . ; J k : I j k ; where j 1 , . . . , j k ∈ [ n ] Compiler Construction Summer Semester 2014 17.16

  16. Maintaining the Symbol Table II Definition 17.1 (Modified update function) update : Dcl × Tab ��� Tab is defined by update ( D C D T D V , st ) := update ( D V , update ( D T , update ( D C , st ))) update ( ε, st ) := st update ( const I 1 := c 1 ; . . . ; I n := c n ; , st ) := st [ I 1 �→ ( const , c 1 ) , . . . , I n �→ ( const , c n )] update ( type I :=bool; D ′ T , st ) := update ( type D ′ T , st [ I �→ ( type , bool , 1)]) update ( type I :=int; D ′ T , st ) := update ( type D ′ T , st [ I �→ ( type , int , 1)]) update ( type I :=real; D ′ T , st ) := update ( type D ′ T , st [ I �→ ( type , real , 1)]) update ( type I := J ; D ′ T , st ) := update ( type D ′ T , st [ I �→ st ( J )]) update ( type I :=array[ z 1 .. z 2 ] of J ; D ′ T , st ) := update ( type D ′ T , st [ I �→ ( type , array , z 1 , z 2 , J , k · n )]) if st ( J ) = ( type , . . . , n ) and k = z 2 − z 1 + 1 update ( type I :=record I 1 : J 1 ; . . . ; I l : J l end; D ′ T , st ) := update ( type D ′ T , st [ I �→ ( type , record , I 1 , J 1 , 0 , I 2 , J 2 , n 1 , . . . , I l , J l , � l − 1 i =1 n i , � l i =1 n i )]) if st ( J i ) = ( type , . . . , n i ) for i ∈ [ l ] update ( var I 1 : J 1 ; . . . ; I n : J n ; , st ) := st [ I 1 �→ ( var , J 1 , 0) , I 2 �→ ( var , J 2 , n 1 ) , . . . , I n �→ ( var , J n , � n − 1 i =1 n i )] if st ( J i ) = ( type , . . . , n i ) for i ∈ [ l ] Compiler Construction Summer Semester 2014 17.17

Recommend


More recommend