1 Intermediate Code Generation Part II Chapter 8




![5 Addressing Array Elements: One. Dimensional Arrays A : array [10. . 20] of 5 Addressing Array Elements: One. Dimensional Arrays A : array [10. . 20] of](https://slidetodoc.com/presentation_image/ab253f1db6cf2ce50ff8fd625e06b5b9/image-5.jpg)




![10 Addressing Array Elements L Elist ] { L. place : = newtemp(); L. 10 Addressing Array Elements L Elist ] { L. place : = newtemp(); L.](https://slidetodoc.com/presentation_image/ab253f1db6cf2ce50ff8fd625e06b5b9/image-10.jpg)











- Slides: 21
1 Intermediate Code Generation Part II Chapter 8 COP 5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005
2 Advanced Intermediate Code Generation Techniques • Reusing temporary names • Addressing array elements • Translating logical and relational expressions • Translating short-circuit Boolean expressions and flow-of-control statements with backpatching lists • Translating procedure calls
3 Reusing Temporary Names generate E 1 + E 2 Evaluate E 1 into t 1 Evaluate E 2 into t 2 t 3 : = t 1 + t 2 If t 1 no longer used, can reuse t 1 instead of using new temp t 3 Modify newtemp() to use a “stack”: Keep a counter c, initialized to 0 newtemp() increments c and returns temporary $c Decrement counter on each use of a $i in a three-address statement
4 Reusing Temporary Names (cont’d) x : = a * b + c * d - e * f Statement c $0 $1 $0 x 0 1 2 1 0 : = : = : = a * b c * d $0 + $1 e * f $0 - $1 $0
5 Addressing Array Elements: One. Dimensional Arrays A : array [10. . 20] of integer; … : = A[i] = base. A + (i - low) * w =i*w+c where c = base. A - low * w with low = 10; w = 4 t 1 t 2 t 3 … : = : = c // c = i * 4 t 1[t 2] t 3 base. A - 10 * 4
6 Addressing Array Elements: Multi-Dimensional Arrays A : array [1. . 2, 1. . 3] of integer; low 1 = 1, low 2 = 1, n 1 = 2, n 2 = 3, w = 4 base. A A[1, 1] A[1, 2] A[2, 1] A[1, 3] A[1, 2] A[2, 1] A[2, 2] A[1, 3] A[2, 3] Row-major Column-major
7 Addressing Array Elements: Multi-Dimensional Arrays A : array [1. . 2, 1. . 3] of integer; (Row-major) … : = A[i, j] = base. A + ((i 1 - low 1) * n 2 + i 2 - low 2) * w = ((i 1 * n 2) + i 2) * w + c where c = base. A - ((low 1 * n 2) + low 2) * w with low 1 = 1; low 2 = 1; n 2 = 3; w = 4 t 1 t 2 t 3 t 4 … : = : = : = i * 3 t 1 + j c // c = t 1 * 4 t 2[t 3] t 4 base. A - (1 * 3 + 1) * 4
8 Addressing Array Elements: Grammar S L : = E E E+E |(E) |L L Elist ] | id Elist , E | id [ E Synthesized attributes: E. place name of temp holding value of E Elist. array name Elist. place name of temp holding index value Elist. ndim number of array dimensions L. place lvalue (=name of temp) L. offset index into array (=name of temp) null indicates non-array simple id
9 Addressing Array Elements S L : = E E E 1 + E 2 E ( E 1 ) E L { if L. offset = null then emit(L. place ‘: =’ E. place) else emit(L. place[L. offset] ‘: =’ E. place) } { E. place : = newtemp(); emit(E. place ‘: =’ E 1. place ‘+’ E 2. place) } { E. place : = E 1. place } { if L. offset = null then E. place : = L. place else E. place : = newtemp(); emit(E. place ‘: =’ L. place[L. offset] }
10 Addressing Array Elements L Elist ] { L. place : = newtemp(); L. offset : = newtemp(); emit(L. place ‘: =’ c(Elist. array); emit(L. offset ‘: =’ Elist. place ‘*’ width(Elist. array)) } L id { L. place : = id. place; L. offset : = null } Elist 1 , E { t : = newtemp(); m : = Elist 1. ndim + 1; emit(t ‘: =’ Elist 1. place ‘*’ limit(Elist 1. array, m)); emit(t ‘: =’ t ‘+’ E. place); Elist. array : = Elist 1. array; Elist. place : = t; Elist. ndim : = m } Elist id [ E { Elist. array : = id. place; Elist. place : = E. place; Elist. ndim : = 1 }
11 Translating Logical and Relational Expressions a or b and not c a<b t 1 : = not c t 2 : = b and t 1 t 3 : = a or t 2 if a < b goto L 1 t 1 : = 0 goto L 2 L 1: t 1 : = 1 L 2:
12 Translating Short-Circuit Expressions Using Backpatching E E or M E | E and M E | not E |(E) | id relop id | true | false M Synthesized attributes: E. code three-address code E. truelist backpatch list for jumps on true E. falselist backpatch list for jumps on false M. quad location of current three-address quad
13 Backpatch Operations with Lists • makelist(i) creates a new list containing threeaddress location i, returns a pointer to the list • merge(p 1, p 2) concatenates lists pointed to by p 1 and p 2, returns a pointer to the concatenates list • backpatch(p, i) inserts i as the target label for each of the statements in the list pointed to by p
14 Backpatching with Lists: Example a < b or c < d and e < f 100: 101: 102: 103: 104: 105: if a goto if c goto if e goto < b goto _ _ < d goto _ _ < f goto _ _ backpatch 100: 101: 102: 103: 104: 105: if a goto if c goto if e goto < b goto TRUE 102 < d goto 104 FALSE < f goto TRUE FALSE
15 Backpatching with Lists: Translation Scheme M { M. quad : = nextquad() } E E 1 or M E 2 { backpatch(E 1. falselist, M. quad); E. truelist : = merge(E 1. truelist, E 2. truelist); E. falselist : = E 2. falselist } E E 1 and M E 2 { backpatch(E 1. truelist, M. quad); E. truelist : = E 2. truelist; E. falselist : = merge(E 1. falselist, E 2. falselist); } E not E 1 { E. truelist : = E 1. falselist; E. falselist : = E 1. truelist } E ( E 1 ) { E. truelist : = E 1. truelist; E. falselist : = E 1. falselist }
16 Backpatching with Lists: Translation Scheme (cont’d) E id 1 relop id 2 { E. truelist : = makelist(nextquad()); E. falselist : = makelist(nextquad() + 1); emit(‘if’ id 1. place relop. op id 2. place ‘goto _’); emit(‘goto _’) } E true { E. truelist : = makelist(nextquad()); E. falselist : = nil; emit(‘goto _’) } E false { E. falselist : = makelist(nextquad()); E. truelist : = nil; emit(‘goto _’) }
17 Flow-of-Control Statements and Backpatching: Grammar S if E then S | if E then S else S | while E do S | begin L end |A L L; S |S S 1 ; S 2 ; S 3 ; S 4 … Synthesized attributes: S. nextlist backpatch list for jumps to the next statement after S (or nil) L. nextlist backpatch list for jumps to the next statement after L (or nil) 100: 200: 300: 400: 500: Code Code Jumps for S 1 out of S 1 backpatch(S. nextlist, 1 for S 2 backpatch(S 2. nextlist, for S 3 backpatch(S 3. nextlist, for S 4 for S 5 backpatch(S 4. nextlist, 200) 300) 400) 500)
18 Flow-of-Control Statements and Backpatching S A { S. nextlist : = nil } S begin L end { S. nextlist : = L. nextlist } S if E then M S 1 { backpatch(E. truelist, M. quad); S. nextlist : = merge(E. falselist, S 1. nextlist) } L L 1 ; M S { backpatch(L 1. nextlist, M. quad); L. nextlist : = S. nextlist; } L S { L. nextlist : = S. nextlist; } M { M. quad : = nextquad() }
19 Flow-of-Control Statements and Backpatching (cont’d) S if E then M 1 S 1 N else M 2 S 2 { backpatch(E. truelist, M 1. quad); backpatch(E. falselist, M 2. quad); S. nextlist : = merge(S 1. nextlist, merge(N. nextlist, S 2. nextlist)) } S while M 1 E do M 2 S 1 { backpatch(S 1, nextlist, M 1. quad); backpatch(E. truelist, M 2. quad); S. nextlist : = E. falselist; emit(‘goto _’) } N { N. nextlist : = makelist(nextquad()); emit(‘goto _’) }
20 Translating Procedure Calls S call id ( Elist ) Elist , E |E foo(a+1, b, 7) t 1 : = a + 1 t 2 : = 7 param t 1 param b param t 2 call foo 3
21 Translating Procedure Calls S call id ( Elist ) Elist , E Elist E { for each item p on queue do emit(‘param’ p); emit(‘call’ id. place |queue|) } { append E. place to the end of queue } { initialize queue to contain only E. place }