1 / 79

Chapter 8 Intermediate code generation Section 0 Overview

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

kyran
Télécharger la présentation

Chapter 8 Intermediate code generation Section 0 Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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.

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

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

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

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

  13. 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.

  14. Chapter 8 Intermediate code generation Section 1 Intermediate languages 5.Addressing array elements 2)two-dimensional array • 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

  15. Chapter 8 Intermediate code generation Section 1 Intermediate languages 5.Addressing array elements 3)n-dimensional array Array[l1:u1,, l2:u2,… ln:un] Let di=ui-li+1,i=1,2,…n, the width of each dimension is m D=a+((i1-l1)d2d3…dn+ (i2-l2)d3d4…dn + (in-1-ln-1)dn + (in-ln))m Change into D=conspart+varpart conspart=a-C C=((…(l1d2+l2 )d3+ l3) d3…+ ln-1) dn+ ln)m varpart= ((…(i1d2+i2 )d3+ i3) d3…+ in-1) dn+ in)m

  16. 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.

  17. 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

  18. 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

  19. 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

  20. 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)

  21. 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)

  22. 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

  23. 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:

  24. 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

  25. 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.

  26. Chapter 8 Intermediate code generation Section 2 Backpatching 3.Boolean expression 1)Modify the grammar E EAE | E0E | not E | (E) | i | Ea rop Ea EA E and E0 E or 2)Semantic Rules (1) E i {E•TC=NXINSTR; E•FC=NXINSTR+1; GEN(‘if’ i.place ‘<>0’ ‘goto 0’); GEN(‘goto 0’)}

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

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

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

  30. INPUT SYM TC FC TAC A and B or not C# # - - and B or not C# #i -- -- and B or not C# #E -1 -2 1.(jnz,a,-,(3)) B or not C# #E and -1- -2- 2.(j,-,-(5)) B or not C# # EA -- -2 or not C# # EAi --- -2- Translate A and B or not C

  31. INPUT SYM TC FC TAC or not C# # EAE --3 -2 4 3.(jnz,B,-,0) or not C# #E -3 -4 4.(j,-,-(5)) or not C# #E or -3- -4- not C# # E0 -3 -- C# # E0 not -3- --- # # E0 not i -3-- ---- # # E0 notE -3-5 ---6 5.(jnz,C,-,0) # # E0 E -3 6 --5 6.(j,-,-,3) # #E -6 -5 success

  32. Chapter 8 Intermediate code generation Section 2 Backpatching 4.Flow of control statements • 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)

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

  34. 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) (5)(:=,2,_,A) Ca•CHAIN->2 Cb•CHAIN->4

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

  36. Chapter 8 Intermediate code generation Section 2 Backpatching 4.flow of control statements 3) While statement W while {W•LABEL=NXINSTR} Wd W E do {BACKPATCH(E•TC,NXINSTR); Wd•CHAIN=E•FC; Wd•LABEL=W•LABEL;} S  Wd S(1){BACKPATCH(S(1)•CHAIN, Wd•LABEL); GEN(‘goto’ Wd •LABEL); S • CHAIN= Wd•CHAIN}

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

  38. 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

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

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

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

  42. 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

  43. 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 nj of the array be available as we group index expressions into an Elist.

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

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

  46. 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 (V•OFFSET=null) E • PLACE = V•PLACE; else {T=NEWTEMP(); GEN(=[ ], E • PLACE[V•OFFSET],_,T); E • PLACE =T;}}

  47. Chapter 8 Intermediate code generation Section 3 Quadruples (5)V Elist] {if (TYPE[ARRAY]<>1) {T=NEWTEMP(); GEN(*,Elist•PLACE,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}

  48. Chapter 8 Intermediate code generation Section 3 Quadruples (7)Elist Elist(1),E {T=NEWTEMP(); k= Elist(1) •DIM+1; dk=LIMIT(Elist(1)•ARRAY,k); GEN(*,Elist (1)•PLACE, dk,T); T1=NEWTEMP(); GEN(+,T,E •PLACE, T1); Elist•ARRAY= Elist(1)•ARRAY; Elist•PLACE= T1; Elist•DIM=k;

  49. Chapter 8 Intermediate code generation Section 3 Quadruples (8)Elist  i[ E {Elist•PLACE=E•PLACE; Elist•DIM=1; Elist•ARRAY=ENTRY(i)}

More Related