Chapter 8 Intermediate code generation Section 0 Overview 1.Position of Intermediate code generator parser Token stream static checker Syntax tree Intermedi.

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Compiler Designs and Constructions
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules 17 and 18 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
CSC 8505 Compiler Construction Intermediate Representations.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
7/15/2015\course\cpeg621-10F\Topic-1a.ppt1 Intermediate Code Generation Reading List: Aho-Sethi-Ullman: Chapter 2.3 Chapter 6.1 ~ 6.2 Chapter 6.3 ~ 6.10.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Compiler Chapter# 5 Intermediate code generation.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
Intermediate Code Generation
1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction to Code Generation and Intermediate Representations
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Chap. 4, Intermediate Code Generation
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Three Address Code Generation of Control Statements continued..
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS 404 Introduction to Compiler Design
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Compiler Construction
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation
Intermediate Code Generation Part II
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part II
Three Address Code Generation - Control Statements
Intermediate Code Generation Part I
Intermediate Code Generation Part II
Intermediate code generation
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
7.4 Boolean Expression and Control Flow Statements
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
Three Address Code Generation – Backpatching
THREE ADDRESS CODE GENERATION
Intermediate Code Generation
Intermediate Code Generation Part II
Review: For array a[2,5], how would the memory for this array looks like using row major layout? What about column major layout? How to compute the address.
Intermediate Code Generation Part I
Compiler Construction
Compiler Design 21. Intermediate Code Generation
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Presentation transcript:

Chapter 8 Intermediate code generation Section 0 Overview 1.Position of Intermediate code generator parser Token stream static checker Syntax tree Intermedi ate code generator Syntax tree Intermediate code Code generator

Chapter 8 Intermediate code generation Section 0 Overview 2.Benefits for using a machine-independent intermediate form Retargeting is facilitated A machine-independent code optimizer can be applied to the intermediate representation.

Chapter 8 Intermediate code generation Section 0 Overview 3.Implementation of Intermediate code generator Syntax-directed translation, folded into parsing –Top-down parsing –Bottom-up parsing

Chapter 8 Intermediate code generation Section 1 Intermediate languages 1.Intermediate representations –Syntax tree (Graphical representation of statements) Abstract Syntax Tree Parsing Tree Directed acyclic graph(DAG) –Postfix notation –Three-address code –Quadruple

Chapter 8 Intermediate code generation Section 1 Intermediate languages 2.Three-address code(TAC) A sequence of statements of the general form x= y op z Notes: 1)Here, x,y,z are names, constants, or compiler- generated temporaries; op stands for any operator 2)There is only one operator on the right side of a statement 3) Three address code is a linearized representation of a syntax tree or a DAG in which explicit names correspond to the interior nodes of the graph 4) Each three-address code statement contains three addresses, two for the operands and one for the result

Chapter 8 Intermediate code generation Section 1 Intermediate languages 3. Types of TAC –X=y op z –X=op y –X=y –goto L –If x relop y goto L –param x call p,n return y

Chapter 8 Intermediate code generation Section 1 Intermediate languages 3. Types of TAC –x=y[i] x[i]=y –x=&y /*the value of x is the location of y*/ x=*y *x=y

Chapter 8 Intermediate code generation Section 1 Intermediate languages 4.Syntax-directed Translation into TAC –We can use S-attributed definition to generate three-address code for assignment statement. –We create a new name every time a temporary is needed.

Production Semantic Rules S  id=E S.code=E.code||gen(id.place ‘=’ E.place) E  E 1 +E 2 E.place=newtemp(); E.code=E 1.code||E 2.code|| gen(E.place,’=’,E 1.place ‘+’ E 2.place) E  id E.place=id.place E.code=‘’

Production Semantic Rules S  while E do S 1 S.begin=newlabel(); S.after=newlabel(); S.code=gen(S.begin ‘:’)||E.code|| gen(‘if’ E.place ‘=‘ ‘0’ ‘goto’ S.after) || S 1.code || gen(‘goto’ S.begin) || gen(S.after ‘:’)

Production Semantic Rules S  if E then S 1 S.after=newlabel(); S.code=E.code|| gen(‘if’ E.place ‘=‘ ‘0’ ‘goto’ S.after) || S 1.code || gen(S.after ‘:’)

Production Semantic Rules S  if E then S 1 S.after=newlabel(); else S 2 E.false=newlabel(); S.code=E.code|| gen(‘if’ E.place ‘=‘ ‘0’ ‘goto’ E.false) || S 1.code || gen(‘goto’ S.after) || gen(E.false ‘:’) || S 2.code || gen(S.after ‘:’)

Chapter 8 Intermediate code generation Section 1 Intermediate languages 5.Addressing array elements 1)One-dimensional array Addr(A[i])=base+(i-low)*w=i*w+(base-low*w) Notes:1)Here, we assume the width of each array element is w and the start address of the array block is base. 2)The array is defined as array[low..upper] of type 3)The sub-expression c=base-low*w can be evaluated when the declaration of the array is seen and we assume that c is saved in the symbol table entry for the array.

Chapter 8 Intermediate code generation Section 1 Intermediate languages 5.Addressing array elements 2)two-dimensional array (1)row-major form Addr(A[i1, i2])= base+((i1-low1)*n2+i2-low2)*w =(i1*n2+i2)*w+base-(low1*n2+low2)*w Where n2=upper2-low2+1 t1=low1*n2 t2=t1+low2 t3=t2*w t4=base-t3 t5=i1*n2 t6=t5+i2 t7=t6*w t4[t7]=x x=t4[t7] (2) column-major form

Chapter 8 Intermediate code generation Section 1 Intermediate languages 5.Addressing array elements 3)n-dimensional array Array[l 1 :u 1,, l 2 :u 2,… l n :u n ] Let d i =u i -l i +1,i=1,2,…n, the width of each dimension is m D=a+((i 1 -l 1 )d 2 d 3 …d n + (i 2 -l 2 )d 3 d 4 …d n + (i n-1 -l n-1 )d n + (i n -l n ))m Change into D=conspart+varpart conspart=a-C C=((…(l 1 d 2 +l 2 )d 3 + l 3 ) d 3 …+ l n-1 ) d n + l n )m varpart= ((…(i 1 d 2 +i 2 )d 3 + i 3 ) d 3 …+ i n-1 ) d n + i n )m

Chapter 8 Intermediate code generation Section 1 Intermediate languages 6.Short-circuit code of Boolean expressions Translate a boolean expression into intermediate code without evaluating the entire expression.

Chapter 8 Intermediate code generation Section 1 Intermediate languages 7. Translation methods of Flow of control statements in Short-circuit code 1)Associate E with two labels E.true –The label to which control flows if E is true E.false –The label to which control flows if E is false

Chapter 8 Intermediate code generation Section 1 Intermediate languages 7. Translation methods of Flow of control statements in Short-circuit code 2)Associate S with a label S.next –Following S.code is a jump to some label

Production Semantic Rules S  if E then S1 E.true=newlabel(); E.false=S.next; S1.next=S.next; S.code=E.code ||gen(E.true ‘:’) ||S1.code S  if E then S1 else S2 E.true=newlabel(); E.false=newlabel(); S1.next=S.next S2.next=S.next S.code=E.code ||gen(E.true ‘:’) ||S1.code||gen(‘goto’ S.next)|| gen(E.false ‘:’)||S2.code

Production Semantic Rules S  while E do S1 S.begin=newlabel(); E.true=newlabel(); E.false=S.next; S1.next=S.begin S.code=gen(S.begin ‘:’)||E.code ||gen(E.true ‘:’) ||S1.code||gen(‘goto’ S.begin)

Production Semantic Rules E  E1 or E2 E1.true=E.true; E1.false=newlabel(); E2.true=E.true; E2.false=E.false E.code=E1.code ||gen(E1.false ‘:’) ||E2.code E  E1 and E2 E1.true=newlabel(); E1.false=E.false; E2.true=E.true; E2.false=E.false E.code=E1.code ||gen(E1.true ‘:’) ||E2.code E  id1 relop id2 E.code=gen(‘if’ id1.place relop.op id2.place ‘goto’ E.true)||gen(‘goto’ E.false)

Chapter 8 Intermediate code generation Section 1 Intermediate languages 7. Translation methods of Flow of control statements in Short-circuit code 3)Examples (1)a<b or c<d and e<f if a<b goto Ltrue goto L1 L1:if c<d goto L2 goto Lfalse L2:if e<f goto Ltrue goto Lfalse Here, we assume that the true and false exits for the entire expression are Ltrue and Lfalse respectively

Chapter 8 Intermediate code generation Section 1 Intermediate languages 7. Translation methods of Flow of control statements in Short-circuit code 3)Examples (2)while a<b do if c<d then x=y+z else x=y-z L1: if a<b goto L2 goto Lnext L2: if c<d goto L3 goto L4 L3:t1=y+z x=t1 goto L1 L4:t2=y-z x=t2 goto L1 Lnext:

Chapter 8 Intermediate code generation Section 2 Backpatching 1.Why and what is backpatching? When generating code for boolean expressions and flow- of-control statements, we may not know the labels that control must go to. We can get around this problem by generating a series of branching statement with the targets of the jumps temporarily left unspecified. Each such statement will be put on a list of goto statements whose labels will be filled in when the proper label can be determined. This subsequent filling in of labels is called backpatching

Chapter 8 Intermediate code generation Section 2 Backpatching 2.Functions to manipulate lists of labels related to backpatching Makelist(i) –Creates a new list containing only i, an index into the array of TAC instructions; makelist returns a pointer to the list it has made. Merge(p1,p2) –Concatenates the lists pointed to by p1 and p2, and returns a pointer to the concatenated list. Backpatch(p,i) –Inserts i as the target label for each of the statements on the list pointed to by p.

Chapter 8 Intermediate code generation Section 2 Backpatching 3.Boolean expression 1)Modify the grammar E  E A E | E 0 E | not E | (E) | i | E a rop E a E A  E and E 0  E or 2)Semantic Rules (1) E  i {ETC=NXINSTR; EFC=NXINSTR+1; GEN( ‘if’ i.place ‘<>0’ ‘goto 0’); GEN(‘goto 0’)}

Chapter 8 Intermediate code generation Section 2 Backpatching 3.Boolean expression 2)Semantic Rules (2) E  E a rop E b {ETC=NXINSTR; EFC=NXINSTR+1; GEN( ‘if’ E a.place rop.op E b.place ‘goto 0’ ); GEN(‘goto 0’)} (3) E  (E (1) ) {ETC= E (1)TC; EFC= E (1)FC} (4) E  not E (1) {ETC= E (1)FC; EFC= E (1)TC}

Chapter 8 Intermediate code generation Section 2 Backpatching 3.Boolean expression 2)Semantic Rules (5)E A  E (1) and {BACKPATCH(E (1)TC,NXINSTR); E A FC= E (1)FC;} (6) E  E A E (2) {ETC= E (2) TC; EFC=MERG(E A FC,E (2) FC}

Chapter 8 Intermediate code generation Section 2 Backpatching 3.Boolean expression 2)Semantic Rules (7)E 0  E (1) or {BACKPATCH(E (1) FC,NXINSTR); E 0 TC= E (1) TC;} (8) E  E 0 E (2) {EFC= E (2) FC; ETC=MERG(E 0 TC,E (2) TC}

Translate A and B or not C # E A i or not C# -2--# E A B or not C# 2.(j,-,-(5))-2--1-#E and B or not C# 1.(jnz,a,-,(3))-2#E and B or not C# -- #i and B or not C# --# A and B or not C# TACFCTCSYMINPUT

TACFCTCSYMINPUT 3.(jnz,B, -,0) - 2 4 -- 3 # E A E or not C # success -5-5 -6-6 #E # 6.(j, -, -,3) -- 5 - 3 6 # E 0 E # 5.(jnz,C, -,0) --- 6 -3-5-3-5 # E 0 not E # -----3---3-- # E 0 not i # ----3--3- # E 0 not C # ---3-3 # E 0 not C # -4--4--3--3- #E or or not C # 4.(j, -, - (5)) -4-4 -3-3 #E or not C #

Chapter 8 Intermediate code generation Section 2 Backpatching 4.Flow of control statements 1)modify the grammar S  if E then S (1) else S (2)  C  if E then T  C S (1) else S  T S (2) S  if E then S (1)  C  if E then S  C S (1)

Chapter 8 Intermediate code generation Section 2 Backpatching 4.Flow of control statements 2) Semantic Rules C  if E then {BACKPATCH(ETC,NXINSTR); CCHAIN=EFC;} T  C S (1) else {q=NXINSTR; GEN(‘goto 0’); BACKPATCH(CCHAIN,NXINSTR); T CHAIN=MERG(S (1)CHAIN,q)} S  T S (2) {SCHAIN=MERG(TCHAIN,S (2)CHAIN)} S  C S (1) {SCHAIN=MERG(CCHAIN,S (1)CHAIN)}

If a then if b then A:=2 else A:=3 Else if c then A=4 Else a=5 (1)(jnz,a,_,(3)) (2)(j,_,_,0) (3)(jnz,b,_,(5)) (4)(j,_,_,0) CaCHAIN->2 CbCHAIN->4 (5)(:=,2,_,A)

Chapter 8 Intermediate code generation Section 2 Backpatching 4.Flow of control statements 3) While statement S  while E do S (1)  W  while W d  W E do S  W d S (1)

Chapter 8 Intermediate code generation Section 2 Backpatching 4.flow of control statements 3) While statement W  while {WLABEL=NXINSTR} W d  W E do {BACKPATCH(ETC,NXINSTR); W dCHAIN=EFC; W dLABEL=WLABEL;} S  W d S (1) {BACKPATCH(S (1)CHAIN, W dLABEL); GEN(‘goto’ W dLABEL); S CHAIN= W dCHAIN}

Chapter 8 Intermediate code generation Section 2 Backpatching 4.flow of control statements 3) While statement Code of E Code of S (1) S.CHAIN

Chapter 8 Intermediate code generation Section 3 Quadruples 1.Implementations of three-address statements Quadruples –(op, arg1,arg2,result) Triples –(n) (op,arg1,arg2) –(m) (op,(n),arg) Notes: A three-address statement is an abstract form of intermediate codes

Chapter 8 Intermediate code generation Section 3 Quadruples 2.Advantages of quadruples Easy to generate target code Good for optimizing

Chapter 8 Intermediate code generation Section 3 Quadruples 3 、 Assignment statements with only id 1) functions NEWTEMP() GEN(OP,ARG 1,ARG 2,RESULT) 2)Semantic rules for quadruple code generation

(1)A  i=E {GEN(=, EPLACE,_, i.entry} (2)E  -E (1) {T=NEWTEMP(); E (1)PLACE,_,T); EPLACE =T } (3)E  E (1) *E (2) {T=NEWTEMP(); GEN(*, E (1)PLACE, E (2)PLACE,T); EPLACE =T } (4)E  E (1) + E (2) {T=NEWTEMP(); GEN(+, E (1)PLACE, E (2)PLACE,T); EPLACE =T } (5)E  (E (1) ) {EPLACE =E (1)PLACE } (6)E  i {EPLACE = i.entry}

iputSYMPLACEquadruples A=-B*(C+D)# =-B*(C+D)#i - -B*(C+D)#i= -- B*(C+D)#i=- --- *(C+D)#i=-i --- *(C+D)#i=-E --- B *(C+D)#i=E -- T 1 -,T 1 ) (C+D)#i=E* -- T 1 - C+D)#i=E*( -- T 1 -- +D)#i=E*(i -- T 1 -- C +D)#i=E*(E -- T 1 -- C

Chapter 8 Intermediate code generation Section 3 Quadruples 4.The translation scheme for addressing array elements 1) grammar A  V:=E V  i[Elist] | i Elist  Elist,E | E E  E op E | (E) | V

Chapter 8 Intermediate code generation Section 3 Quadruples 4.The translation scheme for addressing array elements 2) Rewriting of the grammar A  V:=E V  Elist] | i Elist  Elist (1),E | i[ E E  E op E | (E) | V Notes: This rewriting aims that the various dimensional limits n j of the array be available as we group index expressions into an Elist.

Chapter 8 Intermediate code generation Section 3 Quadruples 4.The translation scheme for addressing array elements 3) semantic variables ARRAY DIM PLACE OFFSET

Chapter 8 Intermediate code generation Section 3 Quadruples 4.The translation scheme for addressing array elements 4) Translation code (1)A  V=E {if (VOFFSET=null) GEN(=,E PLACE,_,VPLACE); else GEN([ ]=,EPLACE,_,VPLACE[VOFFSET])}

Chapter 8 Intermediate code generation Section 3 Quadruples (2)E  E (1) op E (2) {T=NEWTEMP(); GEN(op, E (1) PLACE, E (2) PLACE,T); E PLACE =T} (3)E  (E (1) ) {E PLACE = E (1) PLACE} (4)E  V {if (VOFFSET=null) E PLACE = V PLACE; else {T=NEWTEMP(); GEN(=[ ], E PLACE[V OFFSET],_,T); E PLACE =T;}}

Chapter 8 Intermediate code generation Section 3 Quadruples (5)V  Elist] {if (TYPE[ARRAY]<>1) {T=NEWTEMP(); GEN(*,ElistPLACE,TYPE[ARRAY],T); Elist PLACE=T;} V OFFSET=Elist PLACE; T=NEWTEMP(); GEN(-,HEAD[ARRAY],CONS[ARRAY],T); V PLACE=T} (6)V  i {V PLACE=ENTRY[i]; V OFFSET=null}

Chapter 8 Intermediate code generation Section 3 Quadruples (7)Elist  Elist (1),E {T=NEWTEMP(); k= Elist (1) DIM+1; d k =LIMIT(Elist (1) ARRAY,k); GEN(*,Elist (1) PLACE, d k,T); T 1 =NEWTEMP(); GEN(+,T,E PLACE, T 1 ); ElistARRAY= Elist (1) ARRAY; ElistPLACE= T 1 ; ElistDIM=k;

Chapter 8 Intermediate code generation Section 3 Quadruples (8)Elist  i[ E {ElistPLACE=EPLACE; ElistDIM=1; ElistARRAY=ENTRY(i)}

Chapter 8 Intermediate code generation Section 3 Quadruples E.g. Let A be an array:ARRAY[1:10,1:20]; the address of the beginning of the array is a, m=1. We can get C by the computing: (low1*n2+low2)*m=(1*20+1)*1=21 The quadruples for X=A[I,J] are: (1) (*,I,20,T 1 ) (2) (+, T 1,J, T 2 ) (3) (-,a,21, T 3 ) (4) (=[ ], T 3 [T 2 ],_, T 4 ) (5) (=, T 4,_,X)

Chapter 8 Intermediate code generation Section 3 Quadruples 5.Boolean expressions 1)Primary purposes of boolean expressions –Compute logical values –Used as conditional expressions in statements that alter the flow of control,such as if or while statements. 2)Grammar –E  E and E | E or E | not E | (E) | i | E a rop E a

Chapter 8 Intermediate code generation Section 3 Quadruples 5.Boolean expressions 3).Numerical representation (1)E  E a (1) rop E a (2) {T=NEWTEMP(); GEN(rop, E a (1)PLACE, E a (2)PLACE,T); EPLACE =T } (2)E  E (1) bop E (2) {T=NEWTEMP(); GEN(bop, E (1)PLACE, E (2)PLACE,T); EPLACE =T }

Chapter 8 Intermediate code generation Section 3 Quadruples 5.Boolean expressions 3).Numerical representation (3)E  not E (1) {T=NEWTEMP; GEN( not, E (1)PLACE, _,T); EPLACE =T } (4)E  (E (1) ) {EPLACE =E (1)PLACE } (5)E  i {EPLACE = ENTRY(i)}

Chapter 8 Intermediate code generation Section 3 Quadruples 5.Boolean expressions 3).Numerical representation E.g. X+Y>Z or A and (not B or C) (+,X,Y,T 1 ) ;E+E (>, T 1,Z, T 2 ) ; E >E (not,B,_, T 3 ) ; not E (or, T 3,C, T 4 ) ; E or E (and,A, T 4,T 5 ) ; E and E (or, T 2, T 5, T 6 ) ; E or E

Chapter 8 Intermediate code generation Section 3 Quadruples 5.Boolean expressions 4).Short-circuit code Translate a boolean expression into intermediate code without evaluating the entire expression. Represent the value of an expression by a position in the code sequence.

E.g. if A or B<D then S 1 else S 2 (1)(jnz,A,_,(5)) ;E.true, to S 1 (2)(j,__,(3)) ;E.false, look at the right of or (3)(j<,B,D,(5)) ;E a.true, to S 1 (4)(j,_,_,(P+1)) ; E a.false, to S 2 (5) S 1 …… (P)(j,_,_,(q)) ;jump over S 2 (p+1) S 2 …… (q)the code after S 2

Chapter 8 Intermediate code generation Section 3 Quadruples 6.BackPatching 1).Functions to manipulate lists of labels related to backpatching Makelist(i) –Creates a new list containing only i, an index into the array of quadruples; makelist returns a pointer to the list it has made. Merge(p1,p2) Backpatch(p,i)

Chapter 8 Intermediate code generation Section 3 Quadruples 6.Backpatching 2).Boolean expression (1)Modify the grammar E  E A E | E 0 E | not E | (E) | i | E a rop E a E A  E and E 0  E or (2)Semantic Rules (1) E  i {ETC=NXQ; EFC=NXQ+1; GEN(jnz,ENTRY(i),_,0); GEN(j,_,_,0)}

Chapter 8 Intermediate code generation Section 3 Quadruples 6.Backpatching 2).Boolean expression (2)Semantic Rules (2) E  E a rop E a {ETC=NXQ; EFC=NXQ+1; GEN(jrop, E a (1)PLACE, E a (2)PLACE,0); GEN(j,_,_,0)} (3) E  (E (1) ) {ETC= E (1)TC; EFC= E (1)FC} (4) E  not E (1) {ETC= E (1)FC; EFC= E (1)TC}

Chapter 8 Intermediate code generation Section 3 Quadruples 6.Backpatching 2).Boolean expression (2)Semantic Rules (5)E A  E (1) and {BACKPATCH(E (1)TC,NXQ); E A FC= E (1)FC;} (6) E  E A E (2) {ETC= E (2) TC; EFC=MERG(E A FC,E (2) FC}

Chapter 8 Intermediate code generation Section 3 Quadruples 6.Backpatching 2).Boolean expression (2)Semantic Rules (7)E 0  E (1) or {BACKPATCH(E (1) FC,NXQ); E 0 TC= E (1) TC;} (8) E  E 0 E (2) {EFC= E (2) FC; ETC=MERG(E 0 TC,E (2) TC}

Translate A and B or not C # E A i or not C# -2--# E A B or not C# 2.(j,-,-(5))-2--1-#E and B or not C# 1.(jnz,a,-,(3))-2#E and B or not C# -- #i and B or not C# --# A and B or not C# quadrupleFCTCSYMINPUT

quadrupleFCTCSYMINPUT 3.(jnz,B, -,0) - 2 4 -- 3 # E A E or not C # success -5-5 -6-6 #E # 6.(j, -, -,3) -- 5 - 3 6 # E 0 E # 5.(jnz,C, -,0) --- 6 -3-5-3-5 # E 0 not E # -----3---3-- # E 0 not i # ----3--3- # E 0 not C # ---3-3 # E 0 not C # -4--4--3--3- #E or or not C # 4.(j, -, - (5)) -4-4 -3-3 #E or not C #

Chapter 8 Intermediate code generation Section 3 Quadruples 6.Backpatching 3) Flow of control statements (1) modify the grammar S  if E then S (1) else S (2)  C  if E then T  C S (1) else S  T S (2) S  if E then S (1)  C  if E then S  C S (1)

Chapter 8 Intermediate code generation Section 3 Quadruples 6.Backpatching 3) Flow of control statements (2) Semantic Rules C  if E then {BACKPATCH(ETC,NXQ); CCHAIN=EFC;} T  C S (1) else {q=NXQ; GEN(j, -, - 0); BACKPATCH(CCHAIN,NXQ); T CHAIN=MERG(S (1)CHAIN,q)} S  T S (2) {SCHAIN=MERG(TCHAIN,S (2)CHAIN)} S  C S (1) {SCHAIN=MERG(CCHAIN,S (1)CHAIN)}

e.g. If a then if b then A:=2 else A:=3 Else if c then A=4 Else a=5 (1) (jnz,a,_,0) (2) (j,_,_,0)

If a then if b then A:=2 else A:=3 Else if c then A=4 Else a=5 (1)(jnz,a,_,(3)) (2)(j,_,_,0) (3)(jnz,b,_,0) (4)(j,_,_,0) CaCHAIN->2

If a then if b then A:=2 else A:=3 Else if c then A=4 Else a=5 (1)(jnz,a,_,(3)) (2)(j,_,_,0) (3)(jnz,b,_,(5)) (4)(j,_,_,0) CaCHAIN->2 CbCHAIN->4 (5)(:=,2,_,A)

If a then if b then A:=2 else A:=3 Else if c then A=4 Else a=5 (1)(jnz,a,_,(3)) (2)(j,_,_,0) (3)(jnz,b,_,(5)) (4)(j,_,_,(7)) (5)(:=,2,_,A) CaCHAIN->2 CbCHAIN->6 (6)(j,_,_,0)

Answer (1)(jnz,a,_,(3)) (8)(j,_,_,6) (2)(j,_,_,(9)) (9)(jnz,c,_,(11)) (3)(jnz,b,_,(5)) (10)(j,_,_,(13)) (4)(j,_,_,(7)) (11)(:=,4,_,A) (5)(:=,2,_,A) (12)(j,_,_,8) (6)(j,_,_,0) (13)(:=,5,_,A) (7)(:=,3,_,A) SCHAIN->6->8->12

a b S1(A:=2) S2(A:=3) c S3(A:=4) S4(A:=5) TRUE FALSE 1,2 3, ,

Chapter 8 Intermediate code generation Section 3 Quadruples 6.Backpatching 4) While statement S  while E do S (1)  W  while W d  W E do S  W d S (1)

Chapter 8 Intermediate code generation Section 3 Quadruples 6.Backpatching 4) While statement W  while {WQUAD=NXQ} W d  W E do {BACKPATCH(ETC,NXQ); W dCHAIN=EFC; W dQUAD=WQUAD;} S  W d S (1) {BACKPATCH(S (1)CHAIN, W dQUAD); GEN(j,_,_, W dQUAD); S CHAIN= W dCHAIN}

Chapter 8 Intermediate code generation Section 3 Quadruples 6.Backpatching 4) While statement Code of E Code of S (1) S.CHAIN

e.g. While (A<B) do if (C<D) then X:=Y+Z;  (100) (j<,A,B,0) (101)(j,_,_,0)

e.g. While (A<B) do if (C<D) then X:=Y+Z;  : (100) (j<,A,B,(102)) (101)(j,_,_,0) (102)(j<,C,D,0) (103)(j,_,_,(100))

e.g. While (A<B) do if (C<D) then X:=Y+Z;  : (100) (j<,A,B,(102)) (101)(j,_,_,0) (102)(j<,C,D,(104)) (103)(j,_,_,(100)) (104)(+,Y,Z,T 1 ) (105)(:=, T 1,_,X)

e.g. While (A<B) do if (C<D) then X:=Y+Z;  : (100) (j<,A,B,(102)) (106)(j,_,_,(100)) (101)(j,_,_,(107)) (102)(j<,C,D,(104)) (103)(j,_,_,(100)) (104)(+,Y,Z,T 1 ) (105)(:=, T 1,_,X)