170 likes | 247 Views
Computer Science 313 – Advanced Programming Topics. Lecture 20: Using Arrays and Objects. 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?
E N D
Computer Science 313 – Advanced Programming Topics Lecture 20:Using Arrays and Objects
Today’s Goals • Discuss what array accesses compiled into • How many instructionswould 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?
Unoptimized Compiler • Earliest JIT pass translates to machine code • Not worth optimizing (yet), so can be simple • Savings real but not significant, so must be fast • Simplest approach possible in most systems • Already out of code & in low-level representation • Working with byte codes when writing a JVM
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(?)
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(?)
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
Array Code Generation intx, k;int[] a;a[k] = 2 * x; • Problems we face compiling this code: • Have no idea what isa’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
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
Code For a[k] = 2 * x; ld [sp+x], r0 !r0=x
Code For a[k] = 2 * x; ld [sp+x], r0 !r0=xmul r0, 2, r1 !r1=2* x
Code For a[k] = 2 * x; ld [sp+x], r0 !r0=xmul r0, 2, r1 !r1=2* xld [sp+k],r2 !r2=k
Code For a[k] = 2 * x; ld [sp+x], r0 !r0=xmul r0, 2, r1 !r1=2* xld [sp+k],r2 !r2=kmul r2, 4, r3 !r3=k * 4
Code For a[k] = 2 * x; ld [sp+x], r0 !r0=xmul r0, 2, r1 !r1=2* xld [sp+k],r2 !r2=kmul r2, 4, r3 !r3=k * 4ld [sp+a], r4 !r4=&(a[0])
Code For a[k] = 2 * x; ld [sp+x], r0 !r0=xmul r0, 2, r1 !r1=2* xld [sp+k],r2 !r2=kmul r2, 4, r3 !r3=k * 4ld [sp+a], r4 !r4=&(a[0])add r4, r3, r5 !r5=&(a[k])
Code For a[k] = 2 * x; ld [sp+x], r0 !r0=xmul r0, 2, r1 !r1=2* xld [sp+k],r2 !r2=kmul r2, 4, r3 !r3=k * 4ld [sp+a], r4 !r4=&(a[0])add r4, r3, r5 !r5=&(a[k])st r1, [r5] !a[k] =r1=2* x
Remember the Peephole! ld [sp+x], r0 !r0=xshl r0, 1, r1 !r1=2* xld [sp+k],r2 !r2=kshl r2, 2, r3 !r3=k * 4ld [sp+a], r4 !r4=&(a[0])add r4, r3, r5 !r5=&(a[k])st r1, [r5] !a[k] =r1=2* x
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?