1 / 17

Lecture 20: Using Arrays and Objects

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?

holly
Download Presentation

Lecture 20: Using Arrays and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 313 – Advanced Programming Topics Lecture 20:Using Arrays and Objects

  2. 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?

  3. 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

  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 • 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 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

  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=xmul r0, 2, r1 !r1=2* x

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

  12. 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

  13. 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])

  14. 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])

  15. 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

  16. 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

  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?

More Related