Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intermediate Code Generation

Similar presentations


Presentation on theme: "Intermediate Code Generation"— Presentation transcript:

1 Intermediate Code Generation
Part II Chapter 6

2

3

4

5 Addressing Array Elements
In C and Java, A[i] i=0,1,…,n-1 … =A[i] =base+i*w; Where base is the relative address of the storage allocated for the array ,i.e.,base is the relative address of A[0] Two dimensions, base +i1 * w1 +i2 * w2 k dimensions, base +i1 * w1 +i2 * w2 +…+ik * wk

6 Addressing Array Elements
In C and Java, int A[2][3]; BaseA A[0][0] A[0][0] A[0][1] A[1][0] A[0][2] A[0][1] A[1][0] A[1][1] A[1][1] A[0][2] A[1][2] A[1][2] Row-major Column-major

7 Translation of Array References
S  id= E; {gen(top.get(id.lexeme)’=‘ E.addr } |L=E; {gen(L.array.base’[‘L.addr’]’’=‘ E.addr } E E1+ E2 { E.addr := new Temp(); gen (E.addr’=‘ E1.addr’+’ E2.addr);} |id {E.addr =top.get(id.lexeme);} |L {E.addr = new Temp(); gen (E.addr’=‘L.array.base’[’L.addr’]’);} L  id[E] {L.array=top.get(id.lexeme); L.type=L.array.type.elem; L.addr=new Temp(); gen(L.addr’=‘E.addr’*’L.type.width);} |L1[E] {L.array=L1.array; L.type=L1.type.elem; t=new Temp(); gen(t’=‘E.addr’*’L.type.width);} gen(L.addr’=‘L1.addr’+’t);}

8 Translation of Array References
L.addr denotes a temporary that is used while computing the offset for the array reference by summing the terms ij * wj L.array is a pointer to the symbol-table entry for the array name. The base address of the array ,say ,L.array.base. L.type is the type of the subarray generated by L. For any type t, we assume that its width is given by t.width.

9 Translation of Array References
Example 6.12, let a denote a 2*3 array of integers, and let c,i and j all denote integers. Then the type of a is array(2,array(3,integer)).Its width w is 24,assuming that the width of an integer is 4. the type of a[i] is array (3,integer),of width w1=12. the type of a[i][j] is integer. Three address code of c+a[i][j] is t1=i*12 t2=j*4 t3=t1+t2 t4=a[t3] t5=c+t4

10

11

12

13

14

15

16

17

18

19

20


Download ppt "Intermediate Code Generation"

Similar presentations


Ads by Google