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

compiler construction
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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/

slide-2
SLIDE 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)

slide-3
SLIDE 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 *) AExp : A ::= z | I | A1 + A2 | . . . BExp : B ::= A1 < A2 | not B | B1 and B2 | B1 or B2 Cmd : C ::= I := A | C1;C2 | if B then C1 else C2 | while B do C | I() Dcl : D ::= DC DV DP DC ::= ε | const I1 := z1, . . . ,In := zn; DV ::= ε | var I1, . . . ,In; DP ::= ε | proc I1;K1; . . . ;proc In;Kn; Blk : K ::= D C Pgm : P ::= in/out I1, . . . ,In;K.

3 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-4
SLIDE 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(A1 < A2, st, a, lev) := at(A1, st, a, lev); at(A2, st, a′, lev); a′′ : LT; bt(not B, st, a, lev) := bt(B, st, a, lev); a′ : NOT; bt(B1 and B2, st, a, lev) := bt(B1, st, a, lev); bt(B2, st, a′, lev); a′′ : AND; bt(B1 or B2, st, a, lev) := bt(B1, st, a, lev); bt(B2, 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)

slide-5
SLIDE 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)

slide-6
SLIDE 6

Boolean Expressions with Sequential Semantics Boolean Expressions with Sequential Semantics So far: Boolean expressions with strict semantics (⊥ = nontermination/runtime error) b1 ∧

∨ ⊥ = ⊥ ⊥ ∧ ∨ b2 = ⊥

6 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-7
SLIDE 7

Boolean Expressions with Sequential Semantics Boolean Expressions with Sequential Semantics So far: Boolean expressions with strict semantics (⊥ = nontermination/runtime error) b1 ∧

∨ ⊥ = ⊥ ⊥ ∧ ∨ b2 = ⊥

Now: Boolean expressions with sequential semantics (“short-circuit evaluation”) b1 ∧ b2 ˆ

= if b1 then b2 else false = ⇒

false ∧ ⊥ = false b1 ∨ b2 ˆ

= if b1 then true else b2 = ⇒

true ∨ ⊥ = true (and ⊥ ∧

∨ b = ⊥)

6 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-8
SLIDE 8

Boolean Expressions with Sequential Semantics Boolean Expressions with Sequential Semantics So far: Boolean expressions with strict semantics (⊥ = nontermination/runtime error) b1 ∧

∨ ⊥ = ⊥ ⊥ ∧ ∨ b2 = ⊥

Now: Boolean expressions with sequential semantics (“short-circuit evaluation”) b1 ∧ b2 ˆ

= if b1 then b2 else false = ⇒

false ∧ ⊥ = false b1 ∨ b2 ˆ

= if b1 then true else b2 = ⇒

true ∨ ⊥ = true (and ⊥ ∧

∨ b = ⊥)

Implementation:

  • employ branching instructions rather than Boolean operations (“jumping code”)
  • equip bt and ct with two additional address parameters:

at: target address for true af: target address for false

6 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-9
SLIDE 9

Boolean Expressions with Sequential Semantics Jumping Code for Boolean Expressions Definition 17.1 (Jumping code for Boolean expressions) The mapping

sbt : BExp × Tab × PC3 × Lev AM

(“sequential Boolean expression translation”) is defined by

sbt(A1 < A2, st, a, at, af, l) := at(A1, st, a, l) at(A2, st, a′, l)

a′′ : LT; a′′ + 1 : JFALSE(af); a′′ + 2 : JMP(at);

sbt(not B, st, a, at, af, l) := sbt(B, st, a, af, at, l) sbt(B1 and B2, st, a, at, af, l) := sbt(B1, st, a, a′, af, l) sbt(B2, st, a′, at, af, l) sbt(B1 or B2, st, a, at, af, l) := sbt(B1, st, a, at, a′, l) sbt(B2, st, a′, at, af, l)

7 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-10
SLIDE 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 C1 else C2, st, a, l) := sbt(B, st, a, at, af, l) sct(C1, st, at, l)

af − 1 : JMP(a′);

sct(C2, st, af, l)

a′ :

sct(while B do C, st, a, l) := sbt(B, st, a, at, af, l) sct(C, st, at, l)

af − 1 : JMP(a); af : (remaining cases analogously)

8 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-11
SLIDE 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)

slide-12
SLIDE 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)

slide-13
SLIDE 13

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 : . . . Sequential: 1 : LOAD(x);

LIT(1); LT; JFALSE(6); JMP(a);

6 : LOAD(x);

LOAD(y); LT; JFALSE(a); JMP(11);

11 : sct(C, . . .)

JMP(1);

a : . . .

9 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-14
SLIDE 14

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 : . . . If x = 0: 9 instructions executed Sequential: 1 : LOAD(x);

LIT(1); LT; JFALSE(6); JMP(a);

6 : LOAD(x);

LOAD(y); LT; JFALSE(a); JMP(11);

11 : sct(C, . . .)

JMP(1);

a : . . .

9 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-15
SLIDE 15

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 : . . . If x = 0: 9 instructions executed Sequential: 1 : LOAD(x);

LIT(1); LT; JFALSE(6); JMP(a);

6 : LOAD(x);

LOAD(y); LT; JFALSE(a); JMP(11);

11 : sct(C, . . .)

JMP(1);

a : . . . If x = 0: 5 instructions executed

9 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-16
SLIDE 16

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 : . . . If x = 0: 9 instructions executed Sequential: 1 : LOAD(x);

LIT(1); LT; JFALSE(6); JMP(a);

6 : LOAD(x);

LOAD(y); LT; JFALSE(a); JMP(11);

11 : sct(C, . . .)

JMP(1);

a : . . . If x = 0: 5 instructions executed

= ⇒ 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)

slide-17
SLIDE 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)

slide-18
SLIDE 18

Implementation of Data Structures Implementation of Data Structures Source code: data structures = arrays, records, lists, trees, ...

= ⇒ structured state space, variables with components

11 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-19
SLIDE 19

Implementation of Data Structures 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

11 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-20
SLIDE 20

Implementation of Data Structures 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 requirements known at compile time
  • dynamic data structures: memory requirements runtime dependent

– heap, pointers, garbage collection, ...

11 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-21
SLIDE 21

Implementation of Data Structures 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 requirements known at compile time
  • dynamic data structures: memory requirements runtime dependent

– heap, pointers, garbage collection, ...

First step:

  • static data structures (arrays and records)
  • inductive type definitions
  • no blocks or procedures (only for simplification; “orthogonal” extension)

11 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-22
SLIDE 22

Static 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

12 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-23
SLIDE 23

Static Data Structures Modified Syntax of EPL Definition 17.4 (Modified syntax of EPL)

The modified syntax of EPL is defined as follows (where n ≥ 1):

Z :

z (* z is an integer *)

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, J (* I, J are identifiers *) Type : T ::= bool | int | real | I | array[z1..z2] of T |

record I1:T1; . . . ;In:Tn end

Var : V ::= I | V[E] | V.I Exp : E ::= c | V | E1 + E2 | E1 < E2 | E1 and E2 | . . . Cmd : C ::= V:=E | C1;C2 | if E then C1 else C2 | while E do C Dcl : D ::= DC DT DV DC ::= ε | const I1 := c1; . . . ;In := cn; DT ::= ε | type I1 := T1; . . . ;In := Tn; DV ::= ε | var I1 : T1; . . . ;In : Tn; Pgm : P ::= D C

13 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-24
SLIDE 24

Static Data Structures Static Semantics I

  • All identifiers in a declaration D have to be different.

14 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-25
SLIDE 25

Static Data Structures Static Semantics I

  • All identifiers in a declaration D have to be different.
  • In T = record I1:T1; . . . ;In:Tn end, all selectors Ij must be different.

14 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-26
SLIDE 26

Static Data Structures Static Semantics I

  • All identifiers in a declaration D have to be different.
  • In T = record I1:T1; . . . ;In:Tn end, all selectors Ij must be different.
  • In T = array[z1..z2] of T, z1 ≤ z2.

14 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-27
SLIDE 27

Static Data Structures Static Semantics I

  • All identifiers in a declaration D have to be different.
  • In T = record I1:T1; . . . ;In:Tn end, all selectors Ij must be different.
  • In T = array[z1..z2] of T, z1 ≤ z2.
  • Type definitions must not be recursive:

if DT = type I1 := T1; . . . ;In := Tn and type identifier I occurs in Tj, then I ∈ {I1, . . . , Ij−1}.

14 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-28
SLIDE 28

Static Data Structures Static Semantics I

  • All identifiers in a declaration D have to be different.
  • In T = record I1:T1; . . . ;In:Tn end, all selectors Ij must be different.
  • In T = array[z1..z2] of T, z1 ≤ z2.
  • Type definitions must not be recursive:

if DT = type I1 := T1; . . . ;In := Tn and type identifier I occurs in Tj, then I ∈ {I1, . . . , Ij−1}.

  • All type identifiers used in in a variable declaration DV must be declared in DT.

14 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-29
SLIDE 29

Static Data Structures Static Semantics I

  • All identifiers in a declaration D have to be different.
  • In T = record I1:T1; . . . ;In:Tn end, all selectors Ij must be different.
  • In T = array[z1..z2] of T, z1 ≤ z2.
  • Type definitions must not be recursive:

if DT = type I1 := T1; . . . ;In := Tn and type identifier I occurs in Tj, then I ∈ {I1, . . . , Ij−1}.

  • All type identifiers used in in a variable declaration DV must be declared in DT.
  • Every identifier used in a command C must be declared in D (as a constant or variable).

14 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-30
SLIDE 30

Static Data Structures Static Semantics I

  • All identifiers in a declaration D have to be different.
  • In T = record I1:T1; . . . ;In:Tn end, all selectors Ij must be different.
  • In T = array[z1..z2] of T, z1 ≤ z2.
  • Type definitions must not be recursive:

if DT = type I1 := T1; . . . ;In := Tn and type identifier I occurs in Tj, then I ∈ {I1, . . . , Ij−1}.

  • All type identifiers used in in a variable declaration DV must be declared in DT.
  • 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).

14 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-31
SLIDE 31

Static Data Structures Static Semantics II

  • Array indices must have type int.

15 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-32
SLIDE 32

Static Data Structures Static Semantics II

  • Array indices must have type int.
  • Execution conditions (while) and branching expressions (if) must have type bool.

15 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-33
SLIDE 33

Static Data Structures 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.

15 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-34
SLIDE 34

Static Data Structures 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")

– risk of undetected errors; for programming-in-the-small (scripting languages)

strong typing: explicit casting by programmer

– enhanced software reliability; for programming-in-the-large

15 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-35
SLIDE 35

Static Data Structures 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")

– risk of undetected errors; for programming-in-the-small (scripting 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

15 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-36
SLIDE 36

Modifying the Abstract Machine 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

16 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-37
SLIDE 37

Modifying the Abstract Machine The Modified Abstract Machine AM

  • Additional main storage for keeping data values
  • Procedure stack not required anymore (as procedures ignored)

Definition 17.5 (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∗ (true ˆ

= 1, false ˆ = 0; top to the right), and

  • the main storage MS := {σ | σ : N → R}.

17 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-38
SLIDE 38

Modifying the Abstract Machine New AM Instructions Definition 17.6 (New AM instructions)

  • Procedure instructions are no longer needed.

18 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-39
SLIDE 39

Modifying the Abstract Machine New AM Instructions Definition 17.6 (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 . : S S:

LOAD(pc, d : m, σ) := (pc + 1, d : σ(m), σ)

if m ∈ N

STORE(pc, d : m : r, σ) := (pc + 1, d, σ[m → r])

if m ∈ N

18 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-40
SLIDE 40

Modifying the Abstract Machine New AM Instructions Definition 17.6 (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 . : S S:

LOAD(pc, d : m, σ) := (pc + 1, d : σ(m), σ)

if m ∈ N

STORE(pc, d : m : r, σ) := (pc + 1, d, σ[m → r])

if m ∈ N

  • Moreover the following instruction for checking array bounds is introduced:

CAB(z1,z2)(pc, d : r, σ) :=

(pc + 1, d : r, σ)

if r ∈ {z1, . . . , z2}

(0, d : RTE

  • runtime error

, σ) otherwise

18 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-41
SLIDE 41

Modifying the Symbol Table 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

19 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-42
SLIDE 42

Modifying the Symbol Table Modifying the Symbol Table Tab := {st | st : Ide ({const} × (B ∪ Z ∪ R))

∪ ({var} × Ide × N) ∪ ({type} × {bool, int, real} × {1}) ∪ ({type} × {array} × Z2 × Ide × N) ∪ ({type} × {record} × (Ide2 × N)∗ × N)}

Remarks:

  • Variable descriptor (var, I, m): type I, memory address m
  • Last component m of type entry: memory requirement (base types: 1 “cell”)
  • Array descriptor (type, array, z1, z2, I, m):

– bounds z1, z2 – component type identifier I

  • Record descriptor (type, record, I1, J1, o1, . . . , In, Jn, on, m):

– selector Ik – component type identifier Jk – memory offset ok

  • “Indexed” table lookup: st(I.Ik) := (Jk, ok) if st(I) = (type, record, . . . , Ik, Jk, ok, . . . , m)

20 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-43
SLIDE 43

Modifying the Symbol Table 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.

21 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-44
SLIDE 44

Modifying the Symbol Table 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 = DC DT DV ∈ Dcl is flattened, i.e., that every subtype is named by an identifier:

  • If DT = type I1:=T1;. . .;In:=Tn;, then for every k ∈ [n]

– Tk ∈ {bool, int, real} or – Tk ∈ {I1, . . . , Ik−1} or – Tk = array[z1..z2] of Ij where j ∈ [k − 1] or – Tk = record J1:Ij1;. . .;Jl:Ijl end where j1, . . . , jl ∈ [k − 1]

  • For DT as above, DV must be of the form DV = var J1:Ij1;. . .;Jk:Ijk; where j1, . . . , jk ∈ [n]

21 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-45
SLIDE 45

Modifying the Symbol Table Maintaining the Symbol Table II Definition 17.7 (Modified update function)

update : Dcl × Tab Tab is defined by update(DC DT DV, st) := update(DV, update(DT, update(DC, st))) update(ε, st) := st

22 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-46
SLIDE 46

Modifying the Symbol Table Maintaining the Symbol Table II Definition 17.7 (Modified update function)

update : Dcl × Tab Tab is defined by update(DC DT DV, st) := update(DV, update(DT, update(DC, st))) update(ε, st) := st update(const I1:=c1;. . .;In:=cn;, st) := st[I1 → (const, c1), . . . , In → (const, cn)]

22 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-47
SLIDE 47

Modifying the Symbol Table Maintaining the Symbol Table II Definition 17.7 (Modified update function)

update : Dcl × Tab Tab is defined by update(DC DT DV, st) := update(DV, update(DT, update(DC, st))) update(ε, st) := st update(const I1:=c1;. . .;In:=cn;, st) := st[I1 → (const, c1), . . . , In → (const, cn)] update(var I1:J1;. . .;In:Jn;, st) := st[I1 → (var, J1, o1), . . . , In → (var, Jn, on)]

if st(Jk) = (type, . . . , mk) and ok := k−1

i=1 mi for k ∈ [n]

22 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-48
SLIDE 48

Modifying the Symbol Table Maintaining the Symbol Table III Definition 17.7 (Modified update function; continued)

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[z1..z2] of J;D′

T, st)

:= update(type D′

T, st[I → (type, array, z1, z2, J, n · m)])

if st(J) = (type, . . . , m) and n := z2 − z1 + 1

update(type I:=record I1:J1;. . .;In:Jn end;D′

T, st)

:= update(type D′

T, st[I → (type, record, I1, J1, o1, . . . , In, Jn, on, on+1)])

if st(Jk) = (type, . . . , mk) and ok := k−1

i=1 mi for k ∈ [n]

23 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-49
SLIDE 49

Modifying the Symbol Table Maintaining the Symbol Table IV Example 17.8 (Modified update function) Let D := type Bool = bool; Int = int;

Array = array [1..20] of Bool; Record = record S: Array; T: Int end; var x: Int; y: Array; z: Record;

24 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-50
SLIDE 50

Modifying the Symbol Table Maintaining the Symbol Table IV Example 17.8 (Modified update function) Let D := type Bool = bool; Int = int;

Array = array [1..20] of Bool; Record = record S: Array; T: Int end; var x: Int; y: Array; z: Record;

Then

update(D, st) = st[ Bool → (type, bool, 1), Int → (type, int, 1), Array → (type, array, 1, 20, Bool, 20), Record → (type, record, S, Array, 0, T, Int, 20, 21), x → (var, Int, 0), y → (var, Array, 1), z → (var, Record, 21)]

24 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-51
SLIDE 51

Modifying the Translation 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

25 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-52
SLIDE 52

Modifying the Translation Translation of Variables I The translation employs the following auxiliary function to determine the type identifier of a given variable: Definition 17.9 (vtype function) The mapping

vtype : Var × Tab Ide

is given by

vtype(I, st) := J

if st(I) = (var, J, m)

vtype(V[E], st) := J

if vtype(V, st) = I and st(I) = (type, array, z1, z2, J, m)

vtype(V.I, st) := J

if vtype(V, st) = I′ and st(I′.I) = (J, o)

26 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-53
SLIDE 53

Modifying the Translation Translation of Variables II

Function vt generates code for computing the memory address of a variable on the data stack:

Definition 17.10 (Translation of variables)

The mapping vt : Var × Tab AM is given by

vt(I, st) := LIT(m);

if st(I) = (var, J, m)

vt(V[E], st) := vt(V, st)

% base address of V

et(E, st)

% array index

CAB(z1,z2);

% bounds checking

LIT(z1); SUB;

% index difference

LIT(n); MULT;

% relative address

ADD;

% address of V[E] if vtype(V, st) = I, st(I) = (type, array, z1, z2, J, m), st(J) = (type, . . . , n)

vt(V.I, st) := vt(V, st)

% base address of V

LIT(o);

% offset

ADD;

% address of V.I if vtype(V, st) = I′, st(I′.I) = (J, o)

27 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-54
SLIDE 54

Modifying the Translation Translation of Expressions Definition 17.11 (Translation of expressions) The mapping

et : Exp × Tab AM

is given by

et(c, st) := LIT(c); et(V, st) :=

  

LIT(c); if V ∈ Ide and st(V) = (const, c) vt(V, st) LOAD;

  • therwise

et(E1+E2, st) := et(E1, st) et(E2, st) ADD;

. . .

28 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-55
SLIDE 55

Modifying the Translation Translation of Commands and Programs Definition 17.12 (Translation of commands) For the mapping

ct : Cmd × Tab AM

  • nly the handling of assignments needs to be adapted:

ct(V:=E, st) := vt(V, st)

% address of left-hand side

et(E, st)

% value of right-hand side

STORE;

29 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-56
SLIDE 56

Modifying the Translation Translation of Commands and Programs Definition 17.12 (Translation of commands) For the mapping

ct : Cmd × Tab AM

  • nly the handling of assignments needs to be adapted:

ct(V:=E, st) := vt(V, st)

% address of left-hand side

et(E, st)

% value of right-hand side

STORE;

Definition 17.13 (Translation of programs) The mapping

trans : Pgm AM

is defined by

trans(D C) := ct(C, update(D, st∅))

where st∅(I) is undefined for every I ∈ Ide

29 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-57
SLIDE 57

A Translation Example 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

30 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-58
SLIDE 58

A Translation Example Translation Example I Example 17.14

P = type Int=int; Array=array[1..10] of Int;

var a:Array; i:Int;

  • D

i:=1; while i<=10 do a[i]:=i; i:=i+1;

  • C

31 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-59
SLIDE 59

A Translation Example Translation Example I Example 17.14

P = type Int=int; Array=array[1..10] of Int;

var a:Array; i:Int;

  • D

i:=1; while i<=10 do a[i]:=i; i:=i+1;

  • C

trans(P) = ct(C, update(D, st∅))

31 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-60
SLIDE 60

A Translation Example Translation Example I Example 17.14

P = type Int=int; Array=array[1..10] of Int;

var a:Array; i:Int;

  • D

i:=1; while i<=10 do a[i]:=i; i:=i+1;

  • C

trans(P) = ct(C, update(D, st∅)) st := update(D, st∅) = st∅[ Int → (type, int, 1), Array → (type, array, 1, 10, Int, 10), a → (var, Array, 0), i → (var, Int, 10)]

31 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-61
SLIDE 61

A Translation Example Translation Example I Example 17.14

P = type Int=int; Array=array[1..10] of Int;

var a:Array; i:Int;

  • D

i:=1; while i<=10 do a[i]:=i; i:=i+1;

  • C

trans(P) = ct(C, update(D, st∅)) st := update(D, st∅) = st∅[ Int → (type, int, 1), Array → (type, array, 1, 10, Int, 10), a → (var, Array, 0), i → (var, Int, 10)] ct(C, st) = ct(i:=1, st) ct(while i<=10 do a[i]:=i; i:=i+1, st)

31 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-62
SLIDE 62

A Translation Example Translation Example I Example 17.14

P = type Int=int; Array=array[1..10] of Int;

var a:Array; i:Int;

  • D

i:=1; while i<=10 do a[i]:=i; i:=i+1;

  • C

trans(P) = ct(C, update(D, st∅)) st := update(D, st∅) = st∅[ Int → (type, int, 1), Array → (type, array, 1, 10, Int, 10), a → (var, Array, 0), i → (var, Int, 10)] ct(C, st) = ct(i:=1, st) ct(while i<=10 do a[i]:=i; i:=i+1, st) ct(i:=1, st) = vt(i, st) % address of i et(1, st) % value of 1 STORE; = LIT(10); LIT(1); STORE;

31 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-63
SLIDE 63

A Translation Example Translation Example II Example 17.14 (continued)

ct(while i<=10 do a[i]:=i; i:=i+1, st) = a′ : et(i<=10, st) JFALSE(a′′); ct(a[i]:=i; i:=i+1, st) JMP(a′); a′′ :

32 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-64
SLIDE 64

A Translation Example Translation Example II Example 17.14 (continued)

ct(while i<=10 do a[i]:=i; i:=i+1, st) = a′ : et(i<=10, st) JFALSE(a′′); ct(a[i]:=i; i:=i+1, st) JMP(a′); a′′ : et(i<=10, st) = LIT(10); LOAD; LIT(10); LE;

32 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-65
SLIDE 65

A Translation Example Translation Example II Example 17.14 (continued)

ct(while i<=10 do a[i]:=i; i:=i+1, st) = a′ : et(i<=10, st) JFALSE(a′′); ct(a[i]:=i; i:=i+1, st) JMP(a′); a′′ : et(i<=10, st) = LIT(10); LOAD; LIT(10); LE; ct(a[i]:=i; i:=i+1, st) = ct(a[i]:=i, st) ct(i:=i+1, st)

32 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-66
SLIDE 66

A Translation Example Translation Example II Example 17.14 (continued)

ct(while i<=10 do a[i]:=i; i:=i+1, st) = a′ : et(i<=10, st) JFALSE(a′′); ct(a[i]:=i; i:=i+1, st) JMP(a′); a′′ : et(i<=10, st) = LIT(10); LOAD; LIT(10); LE; ct(a[i]:=i; i:=i+1, st) = ct(a[i]:=i, st) ct(i:=i+1, st) ct(a[i]:=i, st) = vt(a[i], st) % address of a[i] et(i, st)

% value of i

STORE;

32 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-67
SLIDE 67

A Translation Example Translation Example II Example 17.14 (continued)

ct(while i<=10 do a[i]:=i; i:=i+1, st) = a′ : et(i<=10, st) JFALSE(a′′); ct(a[i]:=i; i:=i+1, st) JMP(a′); a′′ : et(i<=10, st) = LIT(10); LOAD; LIT(10); LE; ct(a[i]:=i; i:=i+1, st) = ct(a[i]:=i, st) ct(i:=i+1, st) ct(a[i]:=i, st) = vt(a[i], st) % address of a[i] et(i, st)

% value of i

STORE; vt(a[i], st) = vt(a, st)

% address of a

et(i, st)

% value of i

CAB(1,10);

% bounds checking

LIT(1); SUB;

% index difference

LIT(1); MULT; % relative address ADD;

% address of a[i]

32 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-68
SLIDE 68

A Translation Example Translation Example II Example 17.14 (continued)

ct(while i<=10 do a[i]:=i; i:=i+1, st) = a′ : et(i<=10, st) JFALSE(a′′); ct(a[i]:=i; i:=i+1, st) JMP(a′); a′′ : et(i<=10, st) = LIT(10); LOAD; LIT(10); LE; ct(a[i]:=i; i:=i+1, st) = ct(a[i]:=i, st) ct(i:=i+1, st) ct(a[i]:=i, st) = vt(a[i], st) % address of a[i] et(i, st)

% value of i

STORE; vt(a[i], st) = vt(a, st)

% address of a

et(i, st)

% value of i

CAB(1,10);

% bounds checking

LIT(1); SUB;

% index difference

LIT(1); MULT; % relative address ADD;

% address of a[i]

vt(a, st) = LIT(0);

32 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-69
SLIDE 69

A Translation Example Translation Example II Example 17.14 (continued)

ct(while i<=10 do a[i]:=i; i:=i+1, st) = a′ : et(i<=10, st) JFALSE(a′′); ct(a[i]:=i; i:=i+1, st) JMP(a′); a′′ : et(i<=10, st) = LIT(10); LOAD; LIT(10); LE; ct(a[i]:=i; i:=i+1, st) = ct(a[i]:=i, st) ct(i:=i+1, st) ct(a[i]:=i, st) = vt(a[i], st) % address of a[i] et(i, st)

% value of i

STORE; vt(a[i], st) = vt(a, st)

% address of a

et(i, st)

% value of i

CAB(1,10);

% bounds checking

LIT(1); SUB;

% index difference

LIT(1); MULT; % relative address ADD;

% address of a[i]

vt(a, st) = LIT(0); et(i, st) = LIT(10); LOAD;

32 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)

slide-70
SLIDE 70

A Translation Example Translation Example II Example 17.14 (continued)

ct(while i<=10 do a[i]:=i; i:=i+1, st) = a′ : et(i<=10, st) JFALSE(a′′); ct(a[i]:=i; i:=i+1, st) JMP(a′); a′′ : et(i<=10, st) = LIT(10); LOAD; LIT(10); LE; ct(a[i]:=i; i:=i+1, st) = ct(a[i]:=i, st) ct(i:=i+1, st) ct(a[i]:=i, st) = vt(a[i], st) % address of a[i] et(i, st)

% value of i

STORE; vt(a[i], st) = vt(a, st)

% address of a

et(i, st)

% value of i

CAB(1,10);

% bounds checking

LIT(1); SUB;

% index difference

LIT(1); MULT; % relative address ADD;

% address of a[i]

vt(a, st) = LIT(0); et(i, st) = LIT(10); LOAD; ct(i:=i+1, st) = LIT(10); LIT(10); LOAD; LIT(1); ADD; STORE;

32 of 32 Compiler Construction Winter Semester 2018/19 Lecture 17: Code Generation III (Short-Circuit Evaluation & Static Data Structures)