Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 313 – Advanced Programming Topics.

Similar presentations


Presentation on theme: "Computer Science 313 – Advanced Programming Topics."— Presentation transcript:

1 Computer Science 313 – Advanced Programming Topics

2 Today’s Goals  Discuss what array accesses compiled into  How many instructions would each of these cost?  Are all instructions necessary & why do we need them?  Why so expensive? Could we make it cheaper?  Lab #3 reflection & review semester so far  What mistake did I see in most projects to date?  What are design patterns & optimizations?  How to use them when we write our code?

3 Unoptimized Compiler

4 How Can We Do This?  Byte codes provide 256 possible operations  Not all used at moment, but saved for future use  Review on term so far: how can we do this  Design patterns provide tools to use  Keep in mind lessons of effect of Amdahl’s law  Peephole optimizations and others possible(?)

5 How Can We Do This?  Byte codes provide 256 possible operations  Not all used at moment, but saved for future use  Review on term so far: how can we do this  Design patterns provide tools to use effect of Amdahl’s law  Keep in mind lessons of effect of Amdahl’s law  Peephole optimizations and others possible(?)

6 Speed Is Critical  Normally done via HUGE switch statement  Violates every design rule discussed so far  Method is way too big, violates all SE rules too  Could we reuse this code? Where? Why?  It is all about the tradeoffs we willing to make

7 Array Code Generation int x, k ; int[] a ; a [ k ] = 2 * x ;  Problems we face compiling this code:  Have no idea what is a ’s address  i & k unknown values at this time  But we do have some knowledge to use  Frame’s address held in stack pointer register  Fixed size for int & arrays start at 0

8 Accessing Array or Object  Access array (or object) using base+offset  Base is address at start of the array/object  Each field found at fixed offset in object  Compute offset in array via multiplication  We should require a null-pointer check, but…  … we will cheat by playing with page protections

9 Code For a [ k ] = 2 * x ; ld [sp+ x ], r0 ! r0 = x

10 Code For a [ k ] = 2 * x ; ld [sp+ x ], r0 ! r0 = x mul r0, 2, r1 ! r1 = 2 * x

11 Code For a [ k ] = 2 * x ; ld [sp+ x ], r0 ! r0 = x mul r0, 2, r1 ! r1 = 2 * x ld [sp+ k ],r2 ! r2 = k

12 Code For a [ k ] = 2 * x ; ld [sp+ x ], r0 ! r0 = x mul r0, 2, r1 ! r1 = 2 * x ld [sp+ k ],r2 ! r2 = k mul r2, 4, r3 ! r3 = k * 4

13 Code For a [ k ] = 2 * x ; ld [sp+ x ], r0 ! r0 = x mul r0, 2, r1 ! r1 = 2 * x ld [sp+ k ],r2 ! r2 = k mul r2, 4, r3 ! r3 = k * 4 ld [sp+ a ], r4 ! r4 = &(a[0])

14 Code For a [ k ] = 2 * x ; ld [sp+ x ], r0 ! r0 = x mul r0, 2, r1 ! r1 = 2 * x ld [sp+ k ],r2 ! r2 = k mul r2, 4, r3 ! r3 = k * 4 ld [sp+ a ], r4 ! r4 = &(a[0]) add r4, r3, r5 ! r5 = &(a[k])

15 Code For a [ k ] = 2 * x ; ld [sp+ x ], r0 ! r0 = x mul r0, 2, r1 ! r1 = 2 * x ld [sp+ k ],r2 ! r2 = k mul r2, 4, r3 ! r3 = k * 4 ld [sp+ a ], r4 ! r4 = &(a[0]) add r4, r3, r5 ! r5 = &(a[k]) st r1, [r5] ! a[k] = r1 = 2 * x

16 Remember the Peephole! ld [sp+ x ], r0 ! r0 = x shl r0, 1, r1 ! r1 = 2 * x ld [sp+ k ],r2 ! r2 = k shl r2, 2, r3 ! r3 = k * 4 ld [sp+ a ], r4 ! r4 = &(a[0]) add r4, r3, r5 ! r5 = &(a[k]) st r1, [r5] ! a[k] = r1 = 2 * x

17 For Next Class  Lab #4 on Angel  Have until Friday to complete this lab  Do not delay, it will take time to complete  For class on Wednesday, readings on Angel  Can we optimize all these repeated accesses?  What is CSE and how does it work?  Relation to value numbering & how they work?


Download ppt "Computer Science 313 – Advanced Programming Topics."

Similar presentations


Ads by Google