Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Case Statements and Procedure Calls: Attribute Grammar for Intermediate Code Generation, Slides of Programming Languages

The use of attribute grammar for generating intermediate code during the translation process. It covers various topics such as abstract syntax trees (asts), directed acyclic graphs (dags), attribute grammars, representing attribute grammars, and handling nested procedures/functions. The document also explains how to generate three-address code statements as a side effect of parsing, and provides examples of code generation for while loops and boolean expressions.

Typology: Slides

2012/2013

Uploaded on 05/15/2013

aliya
aliya 🇮🇳

4.4

(12)

96 documents

1 / 83

Toggle sidebar

Related documents


Partial preview of the text

Download Case Statements and Procedure Calls: Attribute Grammar for Intermediate Code Generation and more Slides Programming Languages in PDF only on Docsity! Chap 8: Intermedicate Code Generation Docsity.com Overview  Goal: Generate a Machine Independent Intermediate Form that is Suitable for Optimization and Portability  We’ll Focus on  Revisit AST and DAGs with Attribute Grammars  Introduce and Elaborate on Three Address Coding  Review 3AC for:  Declarations  Assignments and Boolean Expressions  Case Statements and Procedure Calls  Review Prof. Michel’s Approach to Intermediate Code Generation  Concluding Remarks/Looking Ahead Docsity.com Recall ASTs and DAGs  Intermediate Forms of a Program:  ASTs: Abstract Syntax Trees (below left)  DAGs: Directed Acyclic Graphs (below right)  What is the Expression? assign a + * * b c uminus b c uminus assign a + * b c uminus Docsity.com Attribute Grammar  Attribute Grammar for Generating an AST as a side Effect of the Translation Process: Docsity.com Representing Attribute Grammars  Two Different Forms:  Linked Data Structure  Multi-Dimensional Array Docsity.com Temporaries  The machine has an infinite number of temporaries  Call them t0, t1, t2, ....  Temporaries can hold values of any type  The type of the temporary is derived from the generation  Temporaries go out of scope with the function they are in Docsity.com What is Three-Address Coding?  A simple type of instruction  3 / 2 Operands x,y,z  Each operand could be  A literal  A variable  A temporary  Example x := y op z x + y * z t0 := y * z t1 := x + t0 x := op z Docsity.com Types of Three Address Statements  Assignment Statements of Form:  X := Y op Z  op is a Binary Arithmetic or Logical Operation  Assignment Instructions of Form:  X := op Y  op is Unary Operation such as Unary Minus, Logical Negative, Shift/Conversion Operations  Copy Statements of Form:  X := Y where value of Y assigned to X  Unconditional Jump of Form:  goto L which goes to a three address statement labeled with L Docsity.com Attribute Grammar for Assignments  Concepts:  Need to Introduce Temporary Variables as Necessary do Decompose Assignment Statement  Every Generated Line of Code Must have at Most 3 Addresses!  Three Attributes  S.Code : Three Address Code for Non-Term S  E.Place : The Name that Holds Value of E  E.Code : Sequence of 3AC for Expression E  Function:  newtemp: Generate a New temp Variable Docsity.com Attribute Grammar  How would Grammar work for:  A := - B  X := Y + Z  W := B*C + D Docsity.com Three Address Code for While Loops  Grammar Rule:  S → while E do S1  Conceptually:  Check Expression E  If Not True, Skip to First Statement after S1  Execute S1  Goto: Check Expression E  Function:  newlabel : Generates a new label for goto/jump  Generate labels for Check Expression and 1st Statement after S1 Docsity.com Attribute Grammar for Memory Layout  Note: Does not include Temporaries (Need to also Allocate Memory for them as well)  How could this be supported? Docsity.com Handling Nested Procedure/Functions  Recall that in Pascal, Procedures and Functions can be Defined within Other Procedures/Functions  Grammar:  P → D ; S  D → D ; D | id: T | proc id; D ; S  In this Situation, Symbol Table Can’t be Monolithic Structure but Must Support Nested Declarations  Notations:  M, N: Marker Non Terminals so that All S. Actions Occur at End of RHS of Rule  mktable (create ST), enter (ST entry)  addwidth – size of symbol table  enterproc – Add a Procedure Name to ST Docsity.com Nested Symbol Table Q Recall Quicksort from Chapter 7 header a sort nil header a x readarray —> to readarray exchange — to exchange quicksort Rana Sy (ahaa exchange am quicksort | header | header k Vv partition I ] esse | header a a, Docsity.com Revised Attribute Grammar with Emit S>id:=E E->E,+E, E> E,+*E, Bese E> (CE) E > id { p := lookup (id.name); if p # nil then emit(p ':="' E.place) else error } { E.place := newtemp; emit(E.place ':=' E,.place '+' E>.place) } { E.place := newtemp; emit (E.place ':=' E,.place '*' E,.place) } { E.place := newtemp; , emit(E.place ':=' ‘uminus' E,.place) } { E.place := E,.place } { p := lookup (id.name); if p # nil then E.place := p else error } Docsity.com Attribute Grammar for Boolean Expressions E-£, or £; E+E, and £, E= note£, E> (E,} E = id, relop id, E = true E = false { E.place := newremp;: emit(E.place ':=' Ey.place ‘or' E,,place) } { E.place := newtemp; emit(E.place ':=' E,.place ‘and’ E,.place) } { E.place := newtemp; emit(E.place ':=' 'not' E,.place) } { E.place := E,.place } { E.place := newtemp; emit('it' id,.place relop.op id,.place ‘goto’ nextstar +3); emit(E.place ':=' "0'); emit('goto' nextstat +2); emit(E.place ':s''1') } { E.place := newtemp; emit(E.place ‘:=''1') } { E.place := newtemp; emit(E.place ':=''0') } Docsity.com Generated Code  Consider: a < b or c < d and e < f  How Does Attribute Grammar Generate Code?  Do RM Derivation and Create Parse Tree 100: if a < b goto 103 101: t1 := 0 102: goto 104 103: t1 := 1 104: if c < d goto 107 105: t2 := 0 106: goto 108 107: t2 := 1 108: if e < f goto 111 109: t3 := 0 110: goto 112 III: t3 := 1 112: t4 := t2 and t3 113: t5 := t1 or t4 Docsity.com Review Prof. Michel’s Approach  Utilize AST Directly  More Intuitive than Attribute Grammar  Aligns with “emit” Example on Prior Slides  Utilizes C– (see below) C++ C Java C-- Docsity.com C--  The Object Oriented Language For Dummies  C-- supports Classes Inheritance Polymorphism 2 basic types Arrays Simple control primitives – if-then-else – while Docsity.com C-- Example class Foo { int fact(int n) { return 0; } int fib(int x) { return 1; } }; class Main extends Foo { Main() { int x; x = fact(5); } int fact(int n) { if (n==0) return 1; else return n * fact(n-1); } }; Docsity.com Branching Statements  Where to branch ?  Add Labels to instruction  Label = offset of instruction from beginning of array  How to branch ?  Unconditionally  JUMP L  Conditionally  JUMPZ x, L  JUMPNZ x,L  Variant  JUMP x relop y , L with relop in {<,>,<=,>=,==,!=} Docsity.com Function Call Sequence  Fairly abstract here  Calling Conventions  Arguments passed on a stack  Returned value get/set with a special instruction  At Call Site  Push the parameters  call the function  Pass function LABEL  Pass function ARITY  Get result & write in temporary param nk param nk-1 ... param n1 call f,k get return ti Docsity.com Method Call Sequence  Fairly abstract too!  Calling Conventions  Arguments passed on a stack  Pass a reference to object as well  Returned value get/set with a special instruction  At Call Site  Push the parameters  call the method  Pass Object SELF reference  Pass method ID  Pass method ARITY  Get result & write in temporary param nk param nk-1 ... param n1 param obj invoke obj,m,k get return ti Docsity.com Indexed Assigments in C-- IR  In C-- these two are named  mov x := y[i] x[i] := y mov x,y(i) mov x(i),y mov i(x),y mov x,i(y) Docsity.com Classes ?  What is a good representation  A class is  A bunch of attributes like a record/struct  A bunch of methods like an array Docsity.com lelelelels Class Inheritance class T extends S{ ...} Docsity.com Complete Example  On C-- ! (test7.cmm) class Foo { int fact(int n) { return 0; } int fib(int x) { return 1; } }; class Main extends Foo { Main() { int x; x = fact(5); } int fact(int n) { if (n==0) return 1; else return n * fact(n-1); } }; D_0=Foo.vtbl[0] = L_0 Foo.vtbl[1] = L_3 D_1=Main.vtbl[0] = L_13 Main.vtbl[1] = L_3 Main.vtbl[2] = L_6 0 prologue(0) 1 set return 0 2 epilogue 3 prologue(0) 4 set return 1 5 epilogue 6 prologue(0) 7 param 5 8 param self 9 invoke(self,0,1) 10 get return t0 11 mov x,t0 12 epilogue 13 prologue(0) 14 eql t0,n,0 15 jumpz t0 L_18 16 set return 1 17 jump L_25 18 sub t1,n,1 19 param t1 20 param self 21 invoke(self,0,1) 22 get return t2 23 mul t3,n,t2 24 set return t3 25 epilogue 26 param 4 27 call malloc 28 get return t0 29 mov 0(t0),D_1 30 param t0 31 invoke(t0,2,0) 32 get return t1 33 ret Docsity.com Jumps  Consider the factorial code  One conditional jump (n==0)  One inconditional jump (to the exit)  Both are forward Docsity.com Implementation  Straightforward  One Array of “instructions”  Each instruction is polymorphic Docsity.com Location Examples D_0= Foo.vtbl[0] = L_0 Foo.vtbl[1] = L_3 D_1= Main.vtbl[0] = L_13 Main.vtbl[1] = L_3 Main.vtbl[2] = L_6 0 prologue(0) 1 set return 0 2 epilogue ... 13 prologue(0) 14 eql t0,n,0 15 jumpz t0 L_18 16 set return 1 17 jump L_25 18 sub t1,n,1 19 param t1 20 param self 21 invoke(self,0,1) 22 get return t2 23 mul t3,n,t2 24 set return t3 25 epilogue Code Label Code Label Code Label Data Label Symbolic Temporary Docsity.com Dealing With Declarations  It all depends on the nature of the declaration  Class  Subclass  Instance variable  Method  Local Variable Docsity.com Class Declaration  Actually...  Nothing much to do  Except  Setup the VTBL data structure to hold the code labels  Add the VTBL to the data section of the generated code  Sub-class declaration  Same deal! Docsity.com Local Variables  Nothing to do either!  We will just have to reserve space in the frame for the actual code generation Docsity.com All Other Generation  Always based on a template mechanism  (Hence the Grammar style of presentation in the book) Docsity.com Assignments  Easy id := E Location l = lookup(id); Location r = generate(E); emit(mov l,r); Docsity.com Identifier id Location a = newSYMBOLIC(lookup(id)); return c; Docsity.com Method Invocation  Nothing much  Generate code to evaluate the arguments & push Them  Generate code to evaluate the self reference and push it  Generate code to invoke the method  Retrieve #arguments  Retrieve method identifier  Retrieve self reference Docsity.com Method Invocation  Template E0.id(E1,...,En) Location r = newTEMP(); Location obj = generate(E0); Location an = generate(En); emit(param an); ... Location a2 = generate(E2); emit(param a2); Location a1 = generate(E1); emit(param a1); emit(param obj); emit(invoke obj,lookupMethod(id),n); emit(get return r); return r; Docsity.com Array access  Basic Idea  Check bounds [optional step but good practice!]  Remap to 0-based range  Scale offset to match a byte displacement  Do a indexed load based on  Based address  Computed displacement offset = (E - low) * sizeof(T) Docsity.com Array Access Template Location a = generate(E0); Location e = generate(E1); Location t0 = makeTEMP(); Location t1 = makeTEMP(); emit(t0 := E - low); emit(t0 := t0 * sizeof(T)); emit(mov t1, a(t0)); return t1; E0[E1] Docsity.com Multi-Dimentional Array?  Same Idea  With a twist...  Do an induction on the number of dimensions of the array  Use Horner’s rule to evaluate the index.  Choose a striding for the array  2D array Row major vs. Column major  In general (row major) offset = (((E0 - low0) * N0 + E1 - low1)* N1 + E2 .... + EN) * sizeof(T) TYPE []myArray = new TYPE[low0..up0,low1..up1,...,lown..upn]; ... x = myArray[E0,E1,...En]; N0 = 1 Nn-1 = upn-1 - lown-1 + 1 Nn = upn - lown + 1 Docsity.com Array Instantiation  Similar to class instantiation  Generate code to evaluate the bounds for the dimension(s)  Low / Up  Generate code to compute the size for each dimension  Generate code to scale offset  From type-based  To byte-based  Increase the space to account for  Size storage  Low storage  Generate code to call malloc  Generate code to store size/low values Docsity.com Array Instantiation  Template Location l = generate(E1); Location u = generate(E2); Location t0 = makeTEMP(); Location t1 = makeTEMP(); Location t2 = makeTEMP(); emit(t0 := l - u); emit(t0 := t0 + 1); emit(t0 := t0 * sizeof(T)); emit(t1 := t0 + 8); // t1 holds the size in bytes emit(param t0); emit(call malloc); emit(get result t2); emit(mov t2(0),t1); // save the size emit(mov t2(4),l); // save the low (value of E1) return t2; new TYPE[E1..E2]; Docsity.com Field Access  Template  Generate code to get object  Generate code to access field (based on its offset) Location a = generate(E0); Location t0 = makeTEMP(); int fOfs = lookupField(Type(E0),id); emit(mov t0, a(fOfs)); return t0; E0.id Docsity.com Boolean Conjunction  Template Location t0 = makeTEMP(); emit(mov t0, FALSE); Location a = generate(E1); emit(jumpz a,exitLabel); Location b = generate(E2); emit(jumpz b,exitLabel); emit(mov t0, TRUE); emit(exitLabel: NOOP); return t0; E1 && E2 Docsity.com Boolean Disjunction  Template Location t0 = makeTEMP(); emit(mov t0, TRUE); Location a = generate(E1); emit(jumpnz a,exitLabel); Location b = generate(E2); emit(jumpnz b,exitLabel); emit(mov t0, FALSE); emit(exitLabel: NOOP); return t0; E1 || E2 Docsity.com If-Then-Else  Simple Template Location a = generate(E1); emit(jumpz a,elseLabel); generate(S1); emit(jump endLabel); emit(elseLabel:); generate(S2); emit(endLabel:); if E1 then S1 else S2 Docsity.com Switch Statement  The old-fashioned way  As a syntactic rewrite! switch(E1) { case c1: S1; case c2: S2; ... case cn: Sn; default: Sd; } Temporary t = E1; if (t == c1) S1; else if (t == c2) S2; else if (t == c3) S3; .... else if (t == cn) Sn; else Sd; Docsity.com Switch Statement  Table Driven switch(E1) { case c1: S1; case c2: S2; ... case cn: Sn; default: Sd; } Labels[] switchTable= {L1,L2,...,Ln}; Temporary t = E1; jump switchTable[t]; L1: S1; jump end; L2: S2; jump end; ... Ln: Sn; jump end; Ld: Sd end: What have we gained ? Docsity.com Back-patching vs symbolic Labels  Motivation  Sometimes we emit  Backward jumps  Forward jumps  Why are forward jumps “difficult” ?  Example. A while loop ... 125 S1 126 S2 127 JUMPZ ??? 128 S3 129 S4 ... 187 JUMP 125 188 NOOP // exit of loop We don’t know the target of the jump yet! This jump is backward, we already know the target Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved