Chapter Eight(3)


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Chapter Eight(3)

  1. 1. COMPILER CONSTRUCTION Principles and Practice Kenneth C. Louden
  2. 2. 8. Code Generation
  3. 3. Contents <ul><li>Part One </li></ul><ul><li>8.1 Intermediate Code and Data Structure for code Generation </li></ul><ul><li>8.2 Basic Code Generation Techniques </li></ul><ul><li>Part Two </li></ul><ul><li>8.3 Code Generation of Data Structure Reference </li></ul><ul><li>8.4 Code Generation of Control Statements and Logical Expression </li></ul><ul><li>8.5 Code Generation of Procedure and Function calls </li></ul><ul><li>Part Three </li></ul><ul><li>8.6 Code Generation on Commercial Compilers: Two Case Studies </li></ul><ul><li>8.7 TM: A Simple Target Machine </li></ul><ul><li>8.8 A Code Generator for the TINY Language </li></ul><ul><li>8.9 A Survey of Code Optimization Techniques </li></ul><ul><li>8.10 Simple Optimizations for TINY Code Generator </li></ul>
  4. 4. 8.6 Code Generation in Commercial Compilers: Two Case Studies Borland’s C Compiler for 80X86 Sun’s C Compiler for SparcStations
  5. 5. <ul><li>For example, </li></ul><ul><li>Consider the C procedure </li></ul><ul><li>Void f ( int x, char c) </li></ul><ul><li>{ int a[10]; </li></ul><ul><li> double y; </li></ul><ul><li> … </li></ul><ul><li>} </li></ul>
  6. 6. Offset of x fp Offset of c Offset of a Offset of y The activation record for a call to f would appear as y a[0] a[1] … a[9] Return address Control link c x
  7. 7. Assuming two bytes for integers, four bytes for addresses, one byte for character and eight bytes for double-precision floating point, we would have the following offset values: Now, an access of a[i], would require the computation of the address: (-24+2*i)(fp) -32 y -24 a +4 c +5 x Offset Name
  8. 8. <ul><li>For the expression: ( x = x +3 ) + 4, the p-code and three-address code: </li></ul><ul><li>Lad x </li></ul><ul><li>Lod x </li></ul><ul><li>Ldc 3 </li></ul><ul><li>Adi t1=x+3 </li></ul><ul><li>Stn x=t1 </li></ul><ul><li>Ldc 4 </li></ul><ul><li>Adi t2=t1+4 </li></ul>
  9. 9. 8.6.1 The Borland 3.0 C Compiler for the 80X86
  10. 10. <ul><li>Consider the examples of the output of this compiler with the following assignment </li></ul><ul><ul><li>(x = x +3 ) + 4 </li></ul></ul><ul><li>The assembly code for this expression as produced by the Borland 3.0 compiler for the Intel 80x86 is as follows: </li></ul><ul><ul><li>mov ax, word ptr [bp-2] </li></ul></ul><ul><ul><li>add ax, 3 </li></ul></ul><ul><ul><li>mov word ptr [bp-2], ax </li></ul></ul><ul><ul><li>add ax, 4 </li></ul></ul><ul><li>Notes: </li></ul><ul><ul><li>The bp is used as the frame pointer. </li></ul></ul><ul><ul><li>The static simulation method is used to convert the intermediate code into the target code. </li></ul></ul>
  11. 11. <ul><li>For the expression: ( x = x +3 ) + 4, </li></ul><ul><li>The p-code and three-address code: </li></ul><ul><li>Lad x </li></ul><ul><li>Lod x </li></ul><ul><li>Ldc 3 </li></ul><ul><li>Adi t1=x+3 </li></ul><ul><li>Stn x=t1 </li></ul><ul><li>Ldc 4 </li></ul><ul><li>Adi t2=t1+4 </li></ul>
  12. 12. <ul><li>1) Array Reference </li></ul><ul><ul><li>An example: (a [ i + 1 ] = 2 ) + a [ j ] </li></ul></ul><ul><ul><li>Assume that i j, and a are local variables declared as </li></ul></ul><ul><ul><ul><li>int i, j; </li></ul></ul></ul><ul><ul><ul><li>int a[10]; </li></ul></ul></ul><ul><ul><li>The Borland C compiler generates the following assembly code for the above expression (in next page) </li></ul></ul>
  13. 13. <ul><li>Expression: (a [ i + 1 ] = 2 ) + a [ j ] </li></ul><ul><ul><li>( 1 ) mov bx,word ptr [bp-2] </li></ul></ul><ul><ul><li>( 2 ) shl bx , 1 </li></ul></ul><ul><ul><li>( 3 ) l ea ax, word ptr [bp-22] </li></ul></ul><ul><ul><li>( 4 ) add bx , ax </li></ul></ul><ul><ul><li>( 5 ) mov ax , 2 </li></ul></ul><ul><ul><li>( 6 ) mov word ptr [bx],ax </li></ul></ul><ul><ul><li>( 7 ) mov bx,word ptr [bp-4] </li></ul></ul><ul><ul><li>( 8 ) shl bx , 1 </li></ul></ul><ul><ul><li>( 9 ) l ea dx,word ptr [bp-24] </li></ul></ul><ul><ul><li>( 1 0 ) add bx , dx </li></ul></ul><ul><ul><li>( 1 1 ) add ax,word ptr [bx] </li></ul></ul><ul><li>The compiler has applied the algebraic fact to compute address : </li></ul><ul><li>address(a [ i + 1 ]) = base _ address (a) + (i + 1)*elem_size (a) </li></ul><ul><li>= (base _ address (a) + elem_size (a)) + i*elem_size (a) </li></ul>
  14. 14. <ul><li>Array reference generated by a code generation procedure. </li></ul><ul><li>( a [ i + 1 ] = 2 ) + a [ j ] </li></ul><ul><ul><li>lda a </li></ul></ul><ul><ul><li>lod i </li></ul></ul><ul><ul><li>ldc 1 </li></ul></ul><ul><ul><li>a d i </li></ul></ul><ul><ul><li>ixa elem_size(a) </li></ul></ul><ul><ul><li>ldc 2 </li></ul></ul><ul><ul><li>s t n </li></ul></ul><ul><ul><li>lda a </li></ul></ul><ul><ul><li>lod j </li></ul></ul><ul><ul><li>ixa elem_size(a) </li></ul></ul><ul><ul><li>ind 0 </li></ul></ul><ul><ul><li>adi </li></ul></ul>
  15. 15. <ul><li>2) Pointer and Field References </li></ul><ul><li>Assume the declarations of previous examples: </li></ul><ul><ul><li>typedef struct rec </li></ul></ul><ul><ul><li>{ int i; </li></ul></ul><ul><ul><li>char c; </li></ul></ul><ul><ul><li>int j; </li></ul></ul><ul><ul><li>} Rec; </li></ul></ul><ul><ul><li>typedef struct treeNode </li></ul></ul><ul><ul><li>{ int val; </li></ul></ul><ul><ul><li>struct treeNode * lchild, * rchild; </li></ul></ul><ul><ul><li>} TreeNode; </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>Rec x; </li></ul></ul><ul><ul><li>TreeNode *p; </li></ul></ul>
  16. 16. <ul><li>Assume that X and P are declared as local variables and that appropriate allocation of pointers has been done. </li></ul><ul><li>Consider, first, the sizes of the data types involved. </li></ul><ul><ul><li>Integer variable has size 2 bytes; </li></ul></ul><ul><ul><li>Character variable has size 1 bytes; </li></ul></ul><ul><ul><li>The pointer has size 2 bytes. </li></ul></ul><ul><li>The code generated for the statement </li></ul><ul><ul><li>x.j =x.i; </li></ul></ul><ul><li> is </li></ul><ul><ul><li>mov ax, word ptr [ bp-6 ] </li></ul></ul><ul><ul><li>mov word ptr [bp-3],ax </li></ul></ul><ul><li>Notes: </li></ul><ul><ul><li>Local variables are allocated only on even-byte boundaries; </li></ul></ul><ul><ul><li>The offset computation for j (-6 + 3 = -3 ) is performed statically by the compiler . </li></ul></ul>
  17. 17. <ul><li>The code generated for the statement </li></ul><ul><ul><li>p->lchild = p; </li></ul></ul><ul><li>is </li></ul><ul><ul><li>mov word ptr [ si+2 ], si </li></ul></ul><ul><li>And the statement </li></ul><ul><ul><li>p = p->rchild; </li></ul></ul><ul><li>is </li></ul><ul><ul><li>mov si, word ptr [ si+ 4] </li></ul></ul>
  18. 18. <ul><li>3) If and While-Statement </li></ul><ul><li>The statements we use are </li></ul><ul><ul><li>if (x>y) y++; else x--; </li></ul></ul><ul><ul><li>and </li></ul></ul><ul><ul><li>while (x<y) y -= x; </li></ul></ul><ul><li>The Borland compiler generates the following 80x86 code for the given if-statement: </li></ul><ul><ul><li>cmp bx , dx </li></ul></ul><ul><ul><li>jle short @1@86 </li></ul></ul><ul><ul><li>inc dx </li></ul></ul><ul><ul><li>jmp short @1@114 </li></ul></ul><ul><ul><li>@1@86 : </li></ul></ul><ul><ul><li>dec bx </li></ul></ul><ul><ul><li>@1@114 : </li></ul></ul>
  19. 19. <ul><li>For the given while-statement: </li></ul><ul><ul><li>jmp short @1@170 </li></ul></ul><ul><ul><li>@1@142 : </li></ul></ul><ul><ul><li>sub dx , bx </li></ul></ul><ul><ul><li>@1@170 : </li></ul></ul><ul><ul><li>cmp bx , dx </li></ul></ul><ul><ul><li>jl short @1@142 </li></ul></ul>
  20. 20. <ul><li>4) Function definition and call </li></ul><ul><li>The examples are the C function definition: </li></ul><ul><ul><li>int f( int x, int y) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>return x+y+1 ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>And a corresponding call </li></ul><ul><ul><li>f (2+3, 4) </li></ul></ul>
  21. 21. <ul><li>The Borland compiler for the call f (2+3, 4): </li></ul><ul><ul><ul><li>mov ax,4 </li></ul></ul></ul><ul><ul><ul><li>push ax </li></ul></ul></ul><ul><ul><ul><li>mov ax,5 </li></ul></ul></ul><ul><ul><ul><li>push ax </li></ul></ul></ul><ul><ul><ul><li>call near ptr _f </li></ul></ul></ul><ul><ul><ul><li>pop cx </li></ul></ul></ul><ul><ul><ul><li>pop cx </li></ul></ul></ul><ul><li>Notes: </li></ul><ul><ul><ul><li>The arguments are pushed on the stack in reverse order ; </li></ul></ul></ul><ul><ul><ul><li>The caller is responsible for removing the arguments from the stack after the call . </li></ul></ul></ul><ul><ul><ul><li>The call instruction on the 80x86 automatically pushes the return address onto the stack. </li></ul></ul></ul>
  22. 22. <ul><li>Now, consider the code generated by the Borland compiler for the definition of f: </li></ul><ul><ul><ul><li>_ f proc near </li></ul></ul></ul><ul><ul><ul><li> push bp </li></ul></ul></ul><ul><ul><ul><li> mov bp , sp </li></ul></ul></ul><ul><ul><ul><li> mov ax, word ptr [bp+4] </li></ul></ul></ul><ul><ul><ul><li> add ax, word ptr [bp+6] </li></ul></ul></ul><ul><ul><ul><li> inc ax </li></ul></ul></ul><ul><ul><ul><li> jmp short @1@58 </li></ul></ul></ul><ul><ul><ul><li>@1@58 : </li></ul></ul></ul><ul><ul><ul><li> pop bp </li></ul></ul></ul><ul><ul><ul><li> ret </li></ul></ul></ul><ul><ul><ul><li>_ f endp </li></ul></ul></ul>
  23. 23. After these operations, the stack looks as follows: The body of f then corresponds to the code that comes next mov ax, word ptr [bp+4] add ax, word ptr [bp+6] inc ax Finally, the code executes a jump, restores the old bp from the stack, and returns to the caller.
  24. 24. 8.6.2 The Sun 2.0 C Compiler for Sun SPARCstation
  25. 25. <ul><li>Consider again with the assignment </li></ul><ul><ul><ul><li>(x = x + 3 ) + 4 </li></ul></ul></ul><ul><li>The Sun C compiler produces assembly code: </li></ul><ul><ul><ul><li>ld [ %fp + - 0x4 ] , % o1 </li></ul></ul></ul><ul><ul><ul><li>add %o1 , 0x3 , %o1 </li></ul></ul></ul><ul><ul><ul><li>st %o1 , [ %fp + - 0x4 ] </li></ul></ul></ul><ul><ul><ul><li>ld [ %fp + - 0x4 ] , %o2 </li></ul></ul></ul><ul><ul><ul><li>add %o2 , 0x4 , %o3 </li></ul></ul></ul>
  26. 26. <ul><li>1) Array Reference </li></ul><ul><ul><li>( a [ i + 1 ] = 2 ) + a [ j ] </li></ul></ul><ul><li>translated to the following assembly code by the Sun compiler: </li></ul><ul><ul><li>( 1 ) add %fp , - 0x2c , %o1 /*fp-44 </li></ul></ul><ul><ul><li>( 2 ) l d [ %f p + - 0x4 ] , %o2 </li></ul></ul><ul><ul><li>( 3 ) sll %o2 , 0x2 , %o3 </li></ul></ul><ul><ul><li>( 4 ) mov 0x2 , %o4 </li></ul></ul><ul><ul><li>( 5 ) st %o4 , [ %o1 + %o3 ] </li></ul></ul><ul><ul><li>( 6 ) add %f p , - 0x30 , %o5 /*fp-48 </li></ul></ul><ul><ul><li>( 7 ) ld [ %fp + - 0 x 8 ] , % o 7 </li></ul></ul><ul><ul><li>( 8 ) sll %o7 , 0 x 2 , % l 0 </li></ul></ul><ul><ul><li>( 9 ) ld [ %o5 + % l 0 ] , %l1 </li></ul></ul><ul><ul><li>( 1 0 ) mov 0x2 , %l2 </li></ul></ul><ul><ul><li>( 1 1 ) add %l2 , %l1 , %l3 </li></ul></ul>
  27. 27. <ul><li>2 ) Pointer and Field References </li></ul><ul><ul><li>typedef struct rec </li></ul></ul><ul><ul><li>{ int i; </li></ul></ul><ul><ul><li>char c; </li></ul></ul><ul><ul><li>int j; </li></ul></ul><ul><ul><li>} Rec; </li></ul></ul><ul><ul><li>typedef struct treeNode </li></ul></ul><ul><ul><li>{ int val; </li></ul></ul><ul><ul><li>struct treeNode * lchild, * rchild; </li></ul></ul><ul><ul><li>} TreeNode; </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>Rec x; /*allocated only on 4-bytes boundaries. </li></ul></ul><ul><ul><li>TreeNode p; /*4 bytes sizes. </li></ul></ul>
  28. 28. <ul><li>The code generated for the assignment x.j =x.i; is </li></ul><ul><ul><ul><li>ld [%fp+-0xc], %o1 </li></ul></ul></ul><ul><ul><ul><li>st %o1, [%fp+-0x4] </li></ul></ul></ul><ul><li>The pointer assignment p = p - > r c h i l d; results in the target code: </li></ul><ul><ul><ul><li>ld [ % f p + - 0x10 ] , %o4 </li></ul></ul></ul><ul><ul><ul><li>ld [%o4 +0x8],%o5 </li></ul></ul></ul><ul><ul><ul><li>st %o5 , [ %f p + - 0x10 ] </li></ul></ul></ul>
  29. 29. <ul><li>3) If- and while-statements </li></ul><ul><ul><li>if (x>y) y++; else x--; </li></ul></ul><ul><li>The Sun SPARCstation compiler generates the following code: </li></ul><ul><ul><ul><li>ld [ % f p + - 0x4 ] , %o2 </li></ul></ul></ul><ul><ul><ul><li>ld [ % f p + - 0x8 ] , %o3 </li></ul></ul></ul><ul><ul><ul><li>cmp %o2 , %o3 </li></ul></ul></ul><ul><ul><ul><li>bg L16 </li></ul></ul></ul><ul><ul><ul><li>nop </li></ul></ul></ul><ul><ul><ul><li>b L15 </li></ul></ul></ul><ul><ul><ul><li>nop </li></ul></ul></ul>
  30. 30. <ul><li>L16 : </li></ul><ul><ul><ul><li>ld [ %f p + - 0x8 ] , %o4 </li></ul></ul></ul><ul><ul><ul><li>add %o4 , 0x1 , %o4 </li></ul></ul></ul><ul><ul><ul><li>st %o4,[ %fp+-0x8] </li></ul></ul></ul><ul><ul><ul><li>b L17 </li></ul></ul></ul><ul><ul><ul><li>nop </li></ul></ul></ul><ul><li>L15 : </li></ul><ul><ul><ul><li>ld [ %fp + - 0x4 ] , %o5 </li></ul></ul></ul><ul><ul><ul><li>sub % o 5 , 0 x 1 , % o 5 </li></ul></ul></ul><ul><ul><ul><li>st %o5,[ %fp+-0x4] </li></ul></ul></ul><ul><li>L17 : </li></ul>
  31. 31. <ul><li>and while (x<y) y -= x ; </li></ul><ul><li>The code generated by the Sun compiler for the while-loop is </li></ul><ul><ul><ul><li>ld [ % f p + - 0x4 ] , %o7 </li></ul></ul></ul><ul><ul><ul><li>ld [ % f p + - 0x8 ] , %10 </li></ul></ul></ul><ul><ul><ul><li>cmp %o7 , %10 </li></ul></ul></ul><ul><ul><ul><li>bl L21 </li></ul></ul></ul><ul><ul><ul><li>nop </li></ul></ul></ul><ul><ul><ul><li>b L20 </li></ul></ul></ul><ul><ul><ul><li>nop </li></ul></ul></ul><ul><ul><li>L21 : </li></ul></ul>
  32. 32. <ul><li>L18 : </li></ul><ul><ul><li>ld [ % f p + - 0 x 4 ] , % 1 1 </li></ul></ul><ul><ul><li>ld [ % f p + - 0 x 8 ] , % 1 2 </li></ul></ul><ul><ul><li>sub % 1 2 , % 1 1 , % 1 2 </li></ul></ul><ul><ul><li>st %12,[ %fp+-0x8] </li></ul></ul><ul><ul><li>ld [ % f p + - 0 x 4 ] , % 1 3 </li></ul></ul><ul><ul><li>ld [ % f p + - 0 x 8 ] , % 1 4 </li></ul></ul><ul><ul><li>cmp % 1 3 , % 1 4 </li></ul></ul><ul><ul><li>bl L18 </li></ul></ul><ul><ul><li>nop </li></ul></ul><ul><ul><li>b L22 </li></ul></ul><ul><ul><li>nop </li></ul></ul><ul><li>L22 : </li></ul><ul><li>L20 : </li></ul>
  33. 33. <ul><li>4) Function Definition and Call </li></ul><ul><li>We use the same function definition as previously </li></ul><ul><li>the C function definition: </li></ul><ul><ul><li>int f( int x, int y) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>return x+y+1 ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>and a corresponding call </li></ul><ul><ul><li>f (2+3, 4) </li></ul></ul><ul><li>Sun compiler generates the following code: </li></ul><ul><ul><li>mov 0x5, %o0 </li></ul></ul><ul><ul><li>mov 0x4, %o1 </li></ul></ul><ul><ul><li>call _f, 2 </li></ul></ul>
  34. 34. <ul><li>And the code generated for the definition f is </li></ul><ul><li>_ f : </li></ul><ul><ul><li>!#PROLOGUE# 0 </li></ul></ul><ul><ul><li>sethi %hi ( LF62) , %gl </li></ul></ul><ul><ul><li>add %gl , %lo ( L F 6 2 ) , %gl </li></ul></ul><ul><ul><li>save %sp , %gl , %sp </li></ul></ul><ul><ul><li>!#PROLOGUE# 1 </li></ul></ul><ul><ul><li>st %i0 , [ %fp + 0x44 ] </li></ul></ul><ul><ul><li>st %i1, [%fp+0x48] </li></ul></ul><ul><li>L64 : </li></ul><ul><ul><li>. seg &quot; text &quot; </li></ul></ul><ul><ul><li>ld [ %fp + 0x44 ] , %o0 </li></ul></ul><ul><ul><li>ld [%fp+0x48] ,%o1 </li></ul></ul>
  35. 35. <ul><ul><li>add %o0 , %o1 , %o0 </li></ul></ul><ul><ul><li>add %o0 , 0x1 , %o0 </li></ul></ul><ul><ul><li>b LE62 </li></ul></ul><ul><ul><li>nop </li></ul></ul><ul><li>LF62 : </li></ul><ul><ul><li>mov %o0 , %i0 </li></ul></ul><ul><ul><li>ret </li></ul></ul><ul><ul><li>Restore </li></ul></ul><ul><li>Notes: </li></ul><ul><ul><li>The call passes the arguments in register O0 and O1 , rather than on the stack; </li></ul></ul><ul><ul><li>The call indicates with number 2 how many registers are used for this purpose; </li></ul></ul><ul><ul><li>The “o” registers become the “i” registers after call. </li></ul></ul>
  36. 36. 8.7 TM: A Simple Target Machine
  37. 37. <ul><li>In the section following this one will present a code generator for the TINY language </li></ul><ul><li>Generate target code directly for a very simple machine that can be easily simulated </li></ul><ul><li>This machine is called TM (for Tiny Machine). </li></ul>
  38. 38. 8.7.1 Basic Architecture of the Tiny Machine
  39. 39. <ul><li>TM consists of a read-only instruction memory , a data memory , and a set of eight general-purpose registers . </li></ul><ul><li>These all use nonnegative integer addresses beginning at 0. </li></ul><ul><li>Register 7 is the program counter and is the only special register, as described below. </li></ul><ul><li>The C declarations </li></ul><ul><ul><li>#define IADDR_SIZE ... </li></ul></ul><ul><ul><li>/* size of instruction memory */ </li></ul></ul><ul><ul><li>#define DADDR_SIZE... </li></ul></ul><ul><ul><li>/* size of data memory */ </li></ul></ul><ul><ul><li>#define NO_REGS 8 /* number of registers */ </li></ul></ul><ul><ul><li>#define PC_REG 7 </li></ul></ul><ul><ul><li>Instruction iMem[IADDR_SIZE]; </li></ul></ul><ul><ul><li>int dMem[DADDR_SIZE]; </li></ul></ul><ul><ul><li>int reg[NO_REGS]; </li></ul></ul>
  40. 40. <ul><li>TM performs a conventional fetch-execute cycle: </li></ul><ul><ul><li>d o </li></ul></ul><ul><ul><li>/* fetch */ </li></ul></ul><ul><ul><li>Current Instruction = iMem [reg[pcRegNo]++]; </li></ul></ul><ul><ul><li>/* execute current instruction */ </li></ul></ul><ul><ul><li>. . . </li></ul></ul><ul><ul><li>while (!(halt||error)); </li></ul></ul><ul><li>A register-only instruction has the format </li></ul><ul><ul><li>opcode r, s, t </li></ul></ul><ul><li>There are two basic instruction formats: </li></ul><ul><ul><li>Register only ------ RO instruction ; </li></ul></ul><ul><ul><li>Register-memory ------ RM instruction . </li></ul></ul><ul><li>The complete instruction set of the Tiny Machine is listed in the next page. </li></ul>
  41. 41. <ul><li>RO Instructions </li></ul><ul><ul><li>Format opcode r, s, t </li></ul></ul><ul><ul><li>Opcode Effect </li></ul></ul><ul><ul><li>HALT stop execution (operands ignored) </li></ul></ul><ul><ul><li>IN reg [r] ← integer value read from the standard input (s and t ignored) </li></ul></ul><ul><ul><li>OUT reg [r] -> the standard output (s and t ignored) </li></ul></ul><ul><ul><li>ADD reg [r] = reg[s] + reg[t] </li></ul></ul><ul><ul><li>SUB reg [r] = reg[s] - reg[t] </li></ul></ul><ul><ul><li>MUL reg [r] = reg[s] * reg[t] </li></ul></ul><ul><ul><li>DIV reg [r] = reg[s] / reg[t](may generate ZERO _ DIV) </li></ul></ul>
  42. 42. <ul><li>RM Instructions Format opcode r, d(s) </li></ul><ul><ul><li>(a=d+ r e g [s]; any reference to DMem [a] generates DEME_ERR if a<0 or a≥DADDR – SIZE ) </li></ul></ul><ul><li>Opcode Effect </li></ul><ul><ul><li>LD reg [r] = dMem[a] (load r with memory value at a) </li></ul></ul><ul><ul><li>LDA reg [r] = a (load address a directly into r) </li></ul></ul><ul><ul><li>LDC reg [r] = d (load constant d directly into r – s is ignored) </li></ul></ul><ul><ul><li>ST dMem[a] = reg[r] (store value in r to memory location a) </li></ul></ul><ul><ul><li>JLT if (reg [r]<0) reg [PC_REG] = a </li></ul></ul><ul><ul><li>(jump to instruction a if r is negative, similarly for the following) </li></ul></ul><ul><ul><li>JLE if (reg [r]<=0 reg [PC_REG] = a </li></ul></ul><ul><ul><li>JGE if (reg [r]>0) reg [PC_REG] = a </li></ul></ul><ul><ul><li>JGT if (reg [r]>0) reg [PC_REG] = a </li></ul></ul><ul><ul><li>TEQ if (reg [r]==0) reg [PC_REG] = a </li></ul></ul><ul><ul><li>JNE if (reg [r]!=0) reg [PC_REG] = a </li></ul></ul>
  43. 43. <ul><li>A register-memory instruction has the format </li></ul><ul><ul><li>opcode r,d(s) </li></ul></ul><ul><li>RM instructions include three different load instructions corresponding to the three addressing modes </li></ul><ul><ul><li>“ load constant” (LDC), </li></ul></ul><ul><ul><li>“ load address” (LDA), </li></ul></ul><ul><ul><li>and “load memory” (LD). </li></ul></ul><ul><li>Since the instruction set is minimal, some comments are in order about how they can be used to achieve almost all standard programming language operations . </li></ul><ul><ul><li>(More detail in the page P456-457) </li></ul></ul><ul><ul><li>(1) The target register in the arithmetic, IN, and load operations comes first, and the source register(s) come second. </li></ul></ul><ul><ul><li>(2) All arithmetic operations are restricted to registers. </li></ul></ul>
  44. 44. <ul><li>(3) There are no floating-point operations or floating-point registers. </li></ul><ul><li>(4) There are no addressing modes specifiable in the operands as in some assembly code. </li></ul><ul><li>(5) There is no restriction on the use of the pc in any of the instructions. </li></ul><ul><li>LDA 7, d(s) </li></ul><ul><li>(6) There is also no indirect jump instruction. </li></ul><ul><li>LD 7, 0(1) </li></ul><ul><li>(7) The conditional jump instructions (JLT, etc.) can be made relative to the current position in the program by using the pc as the second register. </li></ul><ul><li>JEQ 0, 4(7) </li></ul><ul><li>(8) There is no procedure call or JSUB instruction. </li></ul><ul><li>LD 7, d(s) </li></ul>
  45. 45. 8.7.2 The TM Simulator
  46. 46. <ul><li>The machine simulator accepts text files containing TM instructions as described above, with the following conventions: </li></ul><ul><ul><li>An entirely blank line is ignored. </li></ul></ul><ul><ul><li>A line beginning with an asterisk is considered a comment and is ignored. </li></ul></ul><ul><ul><li>Any other line must contain an integer instruction location followed by a colon followed by a legal instruction. Any text occurring after the instruction is considered a comment and is ignored . </li></ul></ul><ul><li>Figure 8.16 A TM program showing format conventions </li></ul>
  47. 47. <ul><li>* This program inputs an integer, computes </li></ul><ul><li>* its factorial if it is positive, </li></ul><ul><li>* and prints the result </li></ul><ul><ul><li>0 : IN 0, 0, 0 r0 = read </li></ul></ul><ul><ul><li>1 : JLE 0, 6 (7) if 0 < r0 then </li></ul></ul><ul><ul><li>2 : LDC 1,1,0 r1 = 1 </li></ul></ul><ul><ul><li>3 : LDC 2, 1, 0 r2 = 1 </li></ul></ul><ul><ul><li>* repeat </li></ul></ul><ul><ul><li>4 : MUL 1, 1, 0 r1 = r1*r0 </li></ul></ul><ul><ul><li>5 : SUB 0, 0, 2 r0 = r0-r2 </li></ul></ul><ul><ul><li>6 : JNE 0, -3 (7) until r0 == 0 </li></ul></ul><ul><ul><li>7 : OUT 1, 0, 0 write r1 </li></ul></ul><ul><ul><li>8 : HALT 0, 0, 0 halt </li></ul></ul><ul><ul><li>* end of program </li></ul></ul><ul><li>Note: there is no need for location to appear in ascending sequence as they do above. </li></ul>
  48. 48. <ul><li>For example, a code generator is likely to generate the code of Figure 8.16 in the following sequence : </li></ul><ul><ul><li>0 : IN 0,0,0 </li></ul></ul><ul><ul><li>2 : LDC 1,1,0 </li></ul></ul><ul><ul><li>3 : LDC 2,1,0 </li></ul></ul><ul><ul><li>4 : MUL 1,1,0 </li></ul></ul><ul><ul><li>5 : SUB 0,0,2 </li></ul></ul><ul><ul><li>6 : JNE 0,-3(7) </li></ul></ul><ul><ul><li>7 : OUT 1,0,0 </li></ul></ul><ul><ul><li>1 : JLE 0,6(7) </li></ul></ul><ul><ul><li>8 : HALT 0,0,0 </li></ul></ul>
  49. 49. 8.8 Code Generation for the Tiny Language
  50. 50. 8.8.1 The TM Interface of the TINY Code Generator
  51. 51. <ul><li>Encapsulate some of the information the code generator needs to know about the TM in files code.h and code.c </li></ul><ul><ul><li>which are listed in Appendix b </li></ul></ul><ul><li>Review here some of the features of the constant and function definitions in the code.h file </li></ul><ul><li>If a program has two variables x and y, and there are two temporary values currently stored in memory, then dMem would look as following page </li></ul>
  52. 52. There are seven code emitting functions: EmitComment, TranceCode, emitRO, emitRM, emitSkip, emitRestore, emitBackup, emitRM_Abs
  53. 53. 8.8.2 The TINY Code Generator
  54. 54. <ul><li>The TINY code generator is contained in file cgen.c, with its only interface to the TINY compiler the function codeGen, with prototype </li></ul><ul><ul><li>void codeGen(void); </li></ul></ul><ul><li>The codeGen function does: </li></ul><ul><ul><li>It generates a few comments and instructions that set up the runtime environment on startup, </li></ul></ul><ul><ul><li>The calls the cGen function on the syntax tree, </li></ul></ul><ul><ul><li>And finally generates a HALT instruction to end the program. </li></ul></ul>
  55. 55. <ul><li>A TINY syntax tree has a form given by the declarations </li></ul><ul><ul><li>typedef enum { StmtK, ExpK } NodeKind; </li></ul></ul><ul><ul><li>typedef enum { IfK, RepeatK, AssignK, ReadK, WriteK } StmtKind; </li></ul></ul><ul><ul><li>typedef enum { OpK, ConstK, IdK } ExpKind; </li></ul></ul><ul><ul><li>#define MAXCHILDREN 3 </li></ul></ul><ul><ul><li>typedef struct treeNode </li></ul></ul><ul><ul><li>{ struct treeNode * child[MAXCHILDREN] ; </li></ul></ul><ul><ul><ul><li>struct treeNode * sibling; </li></ul></ul></ul><ul><ul><ul><li>int lineno; </li></ul></ul></ul><ul><ul><ul><li>NodeKind nodekind; </li></ul></ul></ul><ul><ul><ul><li>union { StmtKind stmt; ExpKind exp; } kind; </li></ul></ul></ul><ul><ul><ul><li>union { TokenType op; </li></ul></ul></ul><ul><ul><ul><li>Int val; </li></ul></ul></ul><ul><ul><ul><li>char * name; } attr; </li></ul></ul></ul><ul><ul><ul><li>ExpType type; </li></ul></ul></ul><ul><ul><li>} TreeNode; </li></ul></ul>
  56. 56. <ul><li>The cGen function tests only whether a node is a statement or expression node(or null), </li></ul><ul><li>calling the appropriate function genStmt or genExp, </li></ul><ul><li>and then calling itself recursively on siblings. </li></ul>
  57. 57. 8.8.3 Generating and Using TM Code Files with the TINY Compiler
  58. 58. 8.8.4 A Sample TM Code File Generated by the TINY Compiler
  59. 59. 8.9 A Survey of Code Optimizations Techniques
  60. 60. 8.9.1 Principal Sources of Code Optimizations
  61. 61. <ul><li>(1) Register Allocation </li></ul><ul><li>Good use of registers is the most important feature of efficient code. </li></ul><ul><li>(2) Unnecessary Operations </li></ul><ul><li>The second major source of code improvement is to avoid generating code for operations that are redundant or unnecessary . </li></ul><ul><li>(3) Costly Operations </li></ul><ul><li>A code generator should not only look for unnecessary operations, but should take advantage of opportunities to reduce the cost of operations that are necessary, </li></ul><ul><li>but may be implemented in cheaper ways than the source code or a simple implementation might indicate. </li></ul>
  62. 62. <ul><li>(4) Prediction Program Behavior </li></ul><ul><li>To perform some of the previously described optimizations, a compiler must collect information about the uses of variables, values and procedures in programs : whether expressions are reused, whether or when variables change their values or remain constant, and whether procedures are called or not. </li></ul><ul><li>A different approach is taken by some compilers in that statistical behavior about a program is gathered from actual executions and the used to predict which paths are most likely to be taken, which procedures are most likely to be called often, and which sections of code are likely to be executed the most frequently. </li></ul>
  63. 63. 8.9.2 Classification of Optimizations
  64. 64. <ul><li>Two useful classifications are the time during the compilation process when an optimization can be applied and the area of the program over which the optimization applies: </li></ul><ul><ul><li>The time of application during compilation. Optimizations can be performed at practically every stage of compilation. </li></ul></ul><ul><ul><ul><li>For example, constant folding…. </li></ul></ul></ul><ul><ul><li>Some optimizations can be delayed until after target code has been generated - the target code is examined and rewritten to reflect the optimization. </li></ul></ul><ul><ul><ul><li>For example, jump optimization…. </li></ul></ul></ul>
  65. 65. <ul><li>The majority of optimizations are performed either during intermediate code generation , just after intermediate code generation, or during target code generation . </li></ul><ul><li>To the extent that an optimization does not depend on the characteristics of the target machine (called source-level optimizations ) </li></ul><ul><li>They can be performed earlier than those that do depend on the target architecture ( target-level optimizations ). </li></ul><ul><li>Sometimes both optimizations do. </li></ul>
  66. 66. <ul><li>Consider the effect that one optimization may have on another. </li></ul><ul><li>For instance, propagate constants before performing unreachable code elimination . Occasionally, a phase problem may arise in that each of two optimizations may uncover further opportunities for the other. </li></ul><ul><li>For example, consider the code </li></ul><ul><ul><li>x = 1; </li></ul></ul><ul><ul><li>. . . </li></ul></ul><ul><ul><li>y = 0; </li></ul></ul><ul><ul><li>. . . </li></ul></ul><ul><ul><li>if (y) x = 0; </li></ul></ul><ul><ul><li>. . . </li></ul></ul><ul><ul><li>if (x) y = 1; </li></ul></ul>
  67. 67. <ul><li>A first pass at constant propagation might result in the code </li></ul><ul><ul><ul><li>x = 1; </li></ul></ul></ul><ul><ul><ul><li>. . . </li></ul></ul></ul><ul><ul><ul><li>y = 0; </li></ul></ul></ul><ul><ul><ul><li>. . . </li></ul></ul></ul><ul><ul><ul><li>if (0) x = 0; </li></ul></ul></ul><ul><ul><ul><li>. . . </li></ul></ul></ul><ul><ul><ul><li>if (x) y = 1; </li></ul></ul></ul><ul><li>Now, the body of the first if is unreachable code; eliminating it yields: </li></ul><ul><ul><ul><li>x = 1; </li></ul></ul></ul><ul><ul><ul><li>. . . </li></ul></ul></ul><ul><ul><ul><li>y = 0; </li></ul></ul></ul><ul><ul><ul><li>. . . </li></ul></ul></ul><ul><ul><ul><li>if (x) y = 1; </li></ul></ul></ul>
  68. 68. <ul><li>The second classification scheme for optimizations that we consider is by the area of the program over which the optimization applies </li></ul><ul><li>The categories for this classification are called local , global and inter-procedural optimizations </li></ul><ul><ul><li>( 1 ) Local optimizations: applied to s traight-line segments of code , or basic blocks . </li></ul></ul><ul><ul><li>( 2 ) Global optimizations: applied to an individual procedure. </li></ul></ul><ul><ul><li>( 3 ) Inter-procedural optimizations: beyond the boundaries of procedures to the entire program. </li></ul></ul>
  69. 70. 8.9.3 Data Structures and Implementation Techniques for Optimizations
  70. 71. <ul><li>Some optimizations can be made by transformations on the syntax tree itself </li></ul><ul><ul><li>Including constant folding and unreachable code elimination. </li></ul></ul><ul><ul><li>However the syntax tree is an unwieldy or unsuitable structure for collecting information and performing optimizations </li></ul></ul><ul><li>An optimizer that performs global optimizations will construct from the intermediate code of each procedure </li></ul><ul><ul><li>A graphical representation of the code called a flow graph . </li></ul></ul><ul><ul><li>The nodes of a flow graph are the basic blocks , and the edges are formed from the conditional and unconditional jumps. </li></ul></ul><ul><ul><li>Each basic block node contains the sequence of intermediate code instructions of the block. </li></ul></ul>
  71. 73. <ul><li>A single pass can construct a flow graph, together with each of its basic blocks, over the intermediate code </li></ul><ul><li>Each new basic block is identified as follows : </li></ul><ul><ul><li>The first instruction begins a new basic block; </li></ul></ul><ul><ul><li>Each label that is the target of a jump begin a new basic block; </li></ul></ul><ul><ul><li>Each instruction that follows a jump begins a new basic block; </li></ul></ul>
  72. 74. <ul><li>A standard data flow analysis problem is to compute, for each variable, the set of so-called reaching definitions of that variable at the beginning of each basic block. </li></ul><ul><ul><li>Here a definition is an intermediate code instruction that can set the value of the variable, such as an assignment or a read </li></ul></ul><ul><li>Another data structure is frequently constructed for each block, called the DAG of a basic block . </li></ul><ul><ul><li>DAG traces the computation and reassignment of values and variables in a basic block as follows. </li></ul></ul><ul><ul><li>Values that are used in the block that come from elsewhere are represented as leaf nodes . </li></ul></ul>
  73. 75. <ul><li>Operations on those and other values are represented by interior nodes . </li></ul><ul><ul><li>Assignment of a new value is represented by attaching the name of target variable or temporary to the node representing the value assigned </li></ul></ul><ul><li>For example: </li></ul>
  74. 76. <ul><li>Repeated use of the same value also is represented in the DAG structure. </li></ul><ul><li>For example, the C assignment x = (x+1)*(x+1) translates into the three-address instructions: </li></ul><ul><ul><ul><li>t1 = x + 1 </li></ul></ul></ul><ul><ul><ul><li>t2 = x + 1 </li></ul></ul></ul><ul><ul><ul><li>t3 = t1 * t2 </li></ul></ul></ul><ul><ul><ul><li>x = t3 </li></ul></ul></ul><ul><li>DAG for this sequence of instructions is given, showing the repeated use of the expression x+1 </li></ul>
  75. 77. <ul><li>The DAG of a basic block can be constructed by maintaining two dictionaries. </li></ul><ul><ul><li>A table containing variable names and constants , with a lookup operation that returns the DAG node to which a variable name is currently assigned. </li></ul></ul><ul><ul><li>A table of DAG nodes , with a lookup operation that, given an operation and child node </li></ul></ul><ul><li>Target code , or a revised version of intermediate code, can be generated from a DAG by a traversal according to any of the possible topological sorts of the nonleaf nodes. </li></ul>
  76. 78. t3 = x - 1 t2 = fact * x x = t3 t4 = x == 0 fact = t2 Of course, wish to avoid the unnecessary use of temporaries, and so would want to generate the following equivalent three-address code, whose order must remain fixed: fact = fact * x x = x - 1 t4 = x == 0
  77. 79. A similar traversal of the DAG of above Figure results in the following revised three-address code: t1 = x + 1 x = t1 * t1 Using DAG to generate target code for a basic block, we automatically get local common sub expression elimination The DAG representation also makes it possible to eliminate redundant stores and tells us how many references to each value there are
  78. 80. <ul><li>A final method that is often used to assist register allocation as code generator proceeds </li></ul><ul><ul><li>Involves the maintenance of data called register descriptors and address descriptors . </li></ul></ul><ul><ul><li>Register descriptors associate with each register a list of the variable names whose value is currently in the register. </li></ul></ul><ul><ul><li>Address descriptors associate with each variable name the locations in memory where its value is to be found. </li></ul></ul><ul><li>For example, take the basic block DAG of Figure 8.19 and consider the generation of TM code according to a left-to-right traversal of the interior nodes , </li></ul><ul><ul><li>Using the three registers 0, 1, and 2. </li></ul></ul><ul><ul><li>Assume that there are four address descriptors: </li></ul></ul><ul><ul><li>inReg(reg_no) , </li></ul></ul><ul><ul><li>isGlobal(global_offset) , </li></ul></ul><ul><ul><li>isTemp(temp_offset ), </li></ul></ul><ul><ul><li>and isCounst(value) . </li></ul></ul>
  79. 81. Assume further that x is in global location 0 , that fact is in global location 1 , that global locations are accessed via the gp register, and that temporary locations are accessed via the mp register. Finally, assume also that none of the registers begin with any values in them. Then, before code generation for the basic block begins, the address descriptors for the variables and constants would be as follows:
  80. 82. Now assume that the following code is generated: LD 0,1(gp) load fact into reg 0 LD 1,0(gp) load x into reg 1 MUL 0,0,1 The address descriptors would now be Variable/Constant Address Descriptors
  81. 83. And the register descriptors would be Register Variables Contained
  82. 84. Now, given the subsequent code LDC 2,1(0) load constant 1 into reg 2 ADD 1,1,2 The address descriptors would become: Variable/Constant Address Descriptors And the register descriptors would become: Register Variables Contained
  83. 85. 8.10 Simple Optimizations for the TINY Code Generator
  84. 86. <ul><li>Primarily, the inefficiencies are due to two sources: </li></ul><ul><ul><li>The TINY code generator makes very poor use of the registers of the TM machine. </li></ul></ul><ul><ul><li>The TINY code generator unnecessarily generates logical values 0 and 1 for tests, when these tests only appear in if-statements and while-statements, where simpler code will do. </li></ul></ul><ul><ul><li>In this section, indicate how even relatively crude techniques can substantially improve the code generated by the TINY compiler. </li></ul></ul>
  85. 87. 8.10.1 Keeping Temporaries in Registers
  86. 88. <ul><li>The first optimization is an easy method for keeping temporaries in registers rather than constantly storing and reloading them from memory. </li></ul><ul><li>In the TINY code generator temporaries were always stored at the location: tmpoffset(mp) where tmpoffset is a static variable initialized to 0, decremented each time a temporary is stored, and incremented each time it is reloaded. </li></ul><ul><li>A simple way to use registers as temporary locations is to interpret tmpoffset as initially referring to registers, and only after the available registers are exhausted, to use it as an actual offset into memory. </li></ul>
  87. 89. With this improvement, the TINY code generator now generates the TM code sequence given in Figure 8.21.
  88. 90. 8.10.2 Keeping Variables in Registers
  89. 91. <ul><li>A further improvement can be made to the use of the TM registers by reserving some of the registers for use as variable locations. </li></ul><ul><li>A basic scheme is to simply pick a few registers and allocate these as the locations for the most used variables of the program </li></ul><ul><li>With these modifications, the code of the sample program might now use register 3 for the variable x and register 4 for the variable fact, assuming that register 0 through 2 are still reserved for temporaries. </li></ul>
  90. 93. 8.10.3 Optimizing Test Expressions
  91. 94. <ul><li>The final optimization is to simplify the code generated for tests in if-statement and while-statements. </li></ul><ul><li>This improvement depends on the fact that a comparison operator must appear as the root node of the test expression. </li></ul><ul><li>The genExp code for this operator will simply generate code to subtract the value of the right-hand operand from the left-hand operand, leaving the result in register 0. </li></ul><ul><li>The code for the if-statement or while-statement will then test for which comparison operator is applied and generate appropriate conditional jump code. </li></ul><ul><ul><li>if 0<x then. </li></ul></ul>
  92. 95. <ul><li>Corresponds to the TM code: </li></ul><ul><ul><li>4: SUB 0,0,3 </li></ul></ul><ul><ul><li>5: JLT 0,2(7) </li></ul></ul><ul><ul><li>6: LDC 0,0(0) </li></ul></ul><ul><ul><li>7: LDA 7,1(7) </li></ul></ul><ul><ul><li>8: LDC 0,1(0) </li></ul></ul><ul><ul><li>9: JEQ 0,15(7) </li></ul></ul><ul><li>will generate instead the simpler TM code </li></ul><ul><ul><li>4: SUB 0,0,3 </li></ul></ul><ul><ul><li>5: JGE 0,10(7) </li></ul></ul>
  93. 96. With this optimization, the code generated for the test program becomes that given in Figure 8.23.
  94. 97. End of Part Three THANKS