For the expression: (X=X+3)+ 4, The p-code and three-address code Lad x Lod x Ldc 3 Ad t1=x+3 Stn Ldc 4 Adi t2=t1+4
• For the expression: ( x = x +3 ) + 4, • The p-code and three-address code: Lad x Lod x Ldc 3 Adi t1=x+3 Stn x=t1 Ldc 4 Adi t2=t1+4
DArray reference An example:(a[i+1=2)+a[jl Assume that ii and a are local variables declared as int l,J int a[10]; The borland C compiler generates the following assembly code for the above expression(in next page)
1) Array Reference An example: (a [ i + 1 ] = 2 ) + a [ j ] Assume that i j, and a are local variables declared as int i, j; int a[10]; The Borland C compiler generates the following assembly code for the above expression (in next page)
Expression:(a[i+1=2)+aljI (1) mov bx, word ptr [bp-2I (2)shl bx, 1 (3) ea ax, word ptr [bp-22I (4)add bx, ax (5 )mov ax, 2 (6)mov word ptr[bx), ax (7)mov bx, word ptr [bp-4 (8)shl bx, 1 (9)l ea dx, word ptr[bp-241 10)add bx, dx (11)add ax, word ptr [ bx The compiler has applied the algebraic fact to compute address address(a[i+1 D=base address (a)+(i+1)*elem size(a) (base address(a)+ elem size(a))+i*elem size(a)
Expression: (a [ i + 1 ] = 2 ) + a [ j ] ( 1 ) mov bx,word ptr [bp-2] ( 2 ) shl bx , 1 ( 3 ) l ea ax, word ptr [bp-22] ( 4 ) add bx , ax ( 5 ) mov ax , 2 ( 6 ) mov word ptr [bx],ax ( 7 ) mov bx,word ptr [bp-4] ( 8 ) shl bx , 1 ( 9 ) l ea dx,word ptr [bp-24] ( 1 0 ) add bx , dx ( 1 1 ) add ax,word ptr [bx] The compiler has applied the algebraic fact to compute address: address(a [ i + 1 ]) = base _ address (a) + (i + 1)*elem_size (a) = (base _ address (a) + elem_size (a)) + i*elem_size (a)
Array reference generated by a code generation procedure. (ali+1 =2)+aljI Ida a lod i Idc 1 ixa elem size(a) Idc 2 s tn Ida a lod j ixa elem size(a) ind o a
Array reference generated by a code generation procedure. ( a [ i + 1 ] = 2 ) + a [ j ] lda a lod i ldc 1 a d i ixa elem_size(a) ldc 2 s t n lda a lod j ixa elem_size(a) ind 0 adi
2)Pointer and Field references Assume the declarations of previous examples typedef struct rec Int I char c; int j: 3 Rec; typedef struct treenode i int val; struct treeNode Child, rchild; 3 TreeNode; Rec x: TreeNode *p;
2) Pointer and Field References Assume the declarations of previous examples: typedef struct rec { int i; char c; int j; } Rec; typedef struct treeNode { int val; struct treeNode * lchild, * rchild; } TreeNode; … Rec x; TreeNode *p;