Intermediate Code Generation Part II Chapter 6
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
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
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);}
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.
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