Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.

Similar presentations


Presentation on theme: "Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction."— Presentation transcript:

1 Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction

2 – 2 – CSCE 531 Spring 2006 Overview Last Time – Lec21 slides 1-15, 29-35 Array indexing Test 2 - review Test 2 - (7:15 PM 300Main B213) Today’s Lecture Finishing touches on Arrays in expressions Project 5 - Code Generation References: Chapter 9

3 – 3 – CSCE 531 Spring 2006 Optimization levels man gcc man gcc Page of optimization options Page of optimization options E.g. -funroll-loops Optimization Levels Optimization Levels O1 – optimize to make code smaller and faster O2 – optimize more O3 – optimize even more Optimizer rewrites code for inner loop to use pointers Optimizer rewrites code for inner loop to use pointers gcc –O2 –S array.c -o array.O2.s gcc –O2 –S array.c -o array.O2.s

4 – 4 – CSCE 531 Spring 2006 gcc –O2 –S array.c –o arrayO2.s func: pushl %ebp pushl %ebp movl %esp, %ebp movl %esp, %ebp pushl %edi pushl %edi pushl %esi pushl %esi pushl %ebx pushl %ebx subl $92, %esp subl $92, %esp movl 12(%ebp), %edx movl 12(%ebp), %edx movl $0, -92(%ebp) movl $0, -92(%ebp) cmpl %edx, -92(%ebp) cmpl %edx, -92(%ebp) movl 16(%ebp), %ebx movl 16(%ebp), %ebx jge.L11 jge.L11 movl $0, -76(%ebp) movl $0, -76(%ebp).p2align 4,,15.p2align 4,,15.L9:.L9: xorl %ecx, %ecx xorl %ecx, %ecx cmpl %ebx, %ecx cmpl %ebx, %ecx jge.L13 jge.L13 movl -76(%ebp), %esi movl -76(%ebp), %esiNotes:  Compiling using the optimizer  Callee-save registers %edi, %esi, %ebx  92 bytes of local storage  12(%ebp) = arg 2  edx (outer loop limit)  -92(%ebp)

5 – 5 – CSCE 531 Spring 2006 arrayO2.s Page2 Notes  %edx = %edi + %esi * 4  Inner loop %ecx contain for index “n”  x movl 8(%ebp), %edi movl 8(%ebp), %edi leal (%edi,%esi,4), %edx leal (%edi,%esi,4), %edx.p2align 4,,15.p2align 4,,15.L8: incl %ecx incl %ecx addl (%edx), %eax addl (%edx), %eax addl $4, %edx addl $4, %edx cmpl %ebx, %ecx cmpl %ebx, %ecx jl.L8 jl.L8.L13: incl -92(%ebp) incl -92(%ebp) movl 12(%ebp), %edx movl 12(%ebp), %edx addl $7, -76(%ebp) addl $7, -76(%ebp) cmpl %edx, -92(%ebp) cmpl %edx, -92(%ebp) jl.L9 jl.L9.L11:

6 – 6 – CSCE 531 Spring 2006 arrayO2.s Page2 Notes  Inner loop %ecx contain for index “n”  x.L11: addl $92, %esp addl $92, %esp popl %ebx popl %ebx popl %esi popl %esi popl %edi popl %edi popl %ebp popl %ebp ret ret

7 – 7 – CSCE 531 Spring 2006 Array references in Source Code sum = sum + d[i][j]; A right hand side value (rval) refers to the value in d[i][j] a[i][j] = 32; A left hand side value (lval) refers to the address of the memory location whose value should be updated by the assignment Note the variable sum above has lval and an rval also.

8 – 8 – CSCE 531 Spring 2006 Extending Grammar for Expressions S  L := E E  E * E | E + E | ( E ) E  L L  Elist ] L  id Elist  Elist, E Elist  id [ E

9 – 9 – CSCE 531 Spring 2006 Generalizing the Array Address Indexing Formula to 3 rd and Higher Dimensions In this discussion of Addressing a[i 1, i 2, … i m ] let: n k = the number of elements in the kth dimension After factoring out width, the width of the element type Address of a[i 1, i 2, … i p ] is f * width Dimension = 1 Dimension = 1 i 1 e 1 = i 1 ; Dimension = 2 Dimension = 2 (i 1 * n 2 + i 2 ) e 2 = e 1 * n 2 + i 2 ; Dimension = 3 Dimension = 3 ((i 1 * n 2 + i 2 ) * n 3 + i 3 ) e 3 = e 2 * n 3 + i 3 ((i 1 * n 2 + i 2 ) * n 3 + i 3 ) e 3 = e 2 * n 3 + i 3. Dimension = p Dimension = p (…((i 1 * n 2 + i 2 ) … * n p + i p )e p = e p-1 * n p + i p (…((i 1 * n 2 + i 2 ) … * n p + i p )e p = e p-1 * n p + i p

10 – 10 – CSCE 531 Spring 2006 Attributes for Elist - Index Expr List For Elist  Elist, E For Elist  Elist, E we need to be able to evaluate the recursion e m = e m-1 * n m + i m ( For Elist For Elist Elist.array - pointer to the symbol table for the array Elist.ndim - number of dimensions Elist.place - place for offset calculation (pointer to the symbol table) For L For L L.place L.offset - place for offset calculation

11 – 11 – CSCE 531 Spring 2006 Elist – Semantic Actions Elist  id [ E { Elist.array = id.place; Elist.place = E.place; Elist.place = E.place; Elist.ndim = 1; Elist.ndim = 1;} Elist  Elist 1, E{ t = newtemp(); m = Elist 1.ndim + 1; emit(t := Elist 1.place * numcols(Elist 1.array, m); ); emit(t := t + E.place;); Elist.array = Elist 1.array Elist.place = t; Elist.ndim = m; }

12 – 12 – CSCE 531 Spring 2006 L – Semantic Actions L  id{ L.place = id.place; L.offset = null; /* non-array */ L.offset = null; /* non-array */} L  Elist ‘]’{ L.offset = newtemp(); L.offset = newtemp(); L.offset = newtemp(); emit(L.place := Elist.array->base; emit(L.offset := Elist.place *width(Elist.array); }

13 – 13 – CSCE 531 Spring 2006 E  L { if L.offset = null then if L.offset = null then E.place = L.place; else{ else{ E.place = newtemp(); emit(E.place := L.place[L/offset]); }}

14 – 14 – CSCE 531 Spring 2006 Fig 8.18 Annotated Parse Tree A[y,z]

15 – 15 – CSCE 531 Spring 2006 One more thing about Arrays I have been telling you that C started subscripts at zero for simplicity and efficiency. I have been telling you that C started subscripts at zero for simplicity and efficiency. Actually that is true but compilers can generate code that is just as efficient for more general array references. Its just more work on the compiler! Actually that is true but compilers can generate code that is just as efficient for more general array references. Its just more work on the compiler! E.g. a[low1..high1][low2..high2][low3..high3] Static (compile time) versus Dynamic (run time) Static (compile time) versus Dynamic (run time) (…(( (i 1 - low) * n 2 + (i 2 - low)) … * n p + (i p - low p )) + base =(…((i 1 * n 2 + i 2 ) … * n p + i p ) =(…((i 1 * n 2 + i 2 ) … * n p + i p ) - (…((low 1 * n 2 + low 2 ) … * n p + low p ) - (…((low 1 * n 2 + low 2 ) … * n p + low p ) +base +base So what portion of this computation is dynamic(run- time) and what is static( compile-time)? So what portion of this computation is dynamic(run- time) and what is static( compile-time)? Static computation (at compile time) Same dynamic computation (at run time)

16 – 16 – CSCE 531 Spring 2006 Project 5 - Functions Add Functions to core+ Non-nested functions FuncDefList inserted … Parameters passed By value for ugrads By value or ref for grads Symbol Tables Current symbol table; current offset; no global variables Allocate new table when parsing of function definition starts.Types int, float, level of indirection Functions?

17 – 17 – CSCE 531 Spring 2006 Functions as First Class Objects Functions in C – pointer to the start of the code You can pass pointers to functions as argument. Pointer to a function returning an int Pointer to a function returning an int int (*currentRoundToInt) ( float);... ival = *currentRoundToInt(f+2.3); Signal handlers is one of the places that this is commonly used Signal handlers is one of the places that this is commonly used #include #include typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);

18 – 18 – CSCE 531 Spring 2006 Code Generation Chapter 9 Issues in Code Generation Input to code generator Input to code generator Target programs Target programs Memory management Memory management Instruction selection Instruction selection Register allocation Register allocation

19 – 19 – CSCE 531 Spring 2006 Target Machine Architecture RISC vs CISC Byte addressable, word addressable? Byte order? Big Endian vs little Address Modes supported by architecture Absolute Register Indexedd(R)d + contents(R) Indirect register *d(R)contents(d + contents(R)) Cost of address modes in references to memory Cost of address modes in references to memory

20 – 20 – CSCE 531 Spring 2006 Instruction Costs


Download ppt "Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction."

Similar presentations


Ads by Google