Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz.

Similar presentations


Presentation on theme: "Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz."— Presentation transcript:

1 Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz

2 Chapter 9: Translation2 Bottom-up evaluation of S-attributed: Definitions: Syntax-directed definition with only synthesized attributes is called S-attributed Use LR Parser Implementation: Stack to hold info about subtrees that have been parsed

3 Chapter 9: Translation3 Example: A.a:= F(X.x, Y.y, Z.z) for production A ---> XYZ StateValue XX.x YY.y ZZ.z AA.a <--- Top After Parse ===> Value of Symbol Table Pointer to a State in LR(1) Parse Table

4 Chapter 9: Translation4 Example: ProductionSemantic Values (Code) L ---> E$Print (Val[Top]) E ---> E + TVal[nTop]:= Val[Top – 2] + Val[Top] E ---> T T ---> T * FVal[nTop]:= Val[Top – 2] * Val[Top] T ---> F F ---> (E)Val[nTop]:=Val[Top – 1] F ---> digit

5 Chapter 9: Translation5 Cont. Using LR Parser: Parse 3 * 5 + 4 $ T.Val + E.Val <--- Top Top – 1 Top - 2 <--- nTop =

6 Chapter 9: Translation6 InputStackAttributeProduction Used 3 * 5 + 4 $-- * 5 + 4 $33 F3F ---> digit * 5 + 4 $T3T ---> F 5 + 4 $T *3 + 4 $T * 53 * 5 + 4 $T * F3 * 5F -  digit + 4 $T15T ---> T * F + 4 $E15E ---> T 4 $E +15 $E + 415 + 4 $E + F15 + 4F ---> digit $E + T15 4T ---> F $E19E ---> E + T E19 L L ---> E $

7 Chapter 9: Translation7 Parse 3 * 5 + 4 $ L E$ E +T FT * FT 5 F 3 Show Stack 4

8 Chapter 9: Translation8 Intermediate Code Generation: Type of intermediate code generation: 1. Graphical Representation a := b * c + b * c Syntax Tree := a + * * b c b c

9 Chapter 9: Translation9 Dag := a + * b c

10 Chapter 9: Translation10 Advantages? Disadvantages?

11 Chapter 9: Translation11 Representation of Syntax Tree: := ida + ** c b b c <--- b * c a-

12 Chapter 9: Translation12 b.Using array 0idb 1 c 2*01 3 b 4 c 5*34 6+25 7 a 8:=76 b * c

13 Chapter 9: Translation13 2. Postfix notation ( a:= ((b * c) + (b * c))) a b c * b c * + := Advantages/Disadvantages? -No need for () -No need for Temp -Use Run-Time Stack -Easy to reconstruct Parse Tree

14 Chapter 9: Translation14 Three Address Code: 3. Three address Code: General Form X := A op B X, A, B are Const, Name, or Temp Example: a := b * c + d T 0 := b * c T 1 := T 0 + d a := T 1

15 Chapter 9: Translation15 Three Address Code: Advantages? Disadvantages?

16 Chapter 9: Translation16 Three Address Code: Assignment Operand:= Operand1 op Operand2 Unary Operation Operand:= Op Operand1 Copy Statement Operand:= Operand1 Procedure/Function Call Parm A,B Call P,n

17 Chapter 9: Translation17 Three Address Code Indexed Assignment Operand[I]:= B Pointer A:= Address B Unconditional Jump GOTO L Conditional Jump If A RelOp B GOTO L

18 Chapter 9: Translation18 Types of Three Address Codes: 1. Quadruples (Quad/Three Address Code) Record structure with 4 fields Arg1, Arg3, op, Result Example: a:= -b+c*d

19 Chapter 9: Translation19 Quadruples: opArg1Arg2Result 0U_MinusbT1T1 1*cdT2T2 2+T1T1 T2T2 T3T3 3:=T3T3 a 4 Code:U_MinusbT 1 MulcdT 2 AddT 1 T 2 T 3 AssignT 3 a

20 Chapter 9: Translation20 Types of Three Address Codes: 2. Triples: (3 – Tuples/Triplest) Record with 3 fields Arg1, Arg2, op Example: a := -b + c * d opArg1Arg2 (0)U_Minusb (1)*cd (2)+(0)(1) (3):=(2)a

21 Chapter 9: Translation21 Triples: Arg1, Arg2 are either a pointer to Symbol Table (Name, Constant) or a pointer to Triple Structure This is a Two Address Instruction Most Assembly Languages are made up of Triples

22 Chapter 9: Translation22 3. Indirect Tuples: We use pointers to Triples Example: a:= -b + c * d TripleAddressopArg1Arg2 014  U_Minusb 115  *cd 216  +(14)(15) 317  :=(15)a

23 Chapter 9: Translation23 Indirect Tuples: Save Space: Why? Not good for optimizing code: Why? Not good for memory assignment prior to code generation


Download ppt "Chapter 9: Translation1 Compiler Designs and Constructions Chapter 9: Translation Objectives: Translation Method Three Address Code Dr. Mohsen Chitsaz."

Similar presentations


Ads by Google