Computer Science 313 – Advanced Programming Topics.

17
LECTURE 20: USING ARRAYS AND OBJECTS Computer Science 313 – Advanced Programming Topics

Transcript of Computer Science 313 – Advanced Programming Topics.

Page 1: Computer Science 313 – Advanced Programming Topics.

LECTURE 20:USING ARRAYS AND OBJECTS

Computer Science 313 – Advanced Programming Topics

Page 2: Computer Science 313 – Advanced Programming Topics.

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?

Page 3: Computer Science 313 – Advanced Programming Topics.

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

Page 4: Computer Science 313 – Advanced Programming Topics.

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(?)

Page 5: Computer Science 313 – Advanced Programming Topics.

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(?)

Page 6: Computer Science 313 – Advanced Programming Topics.

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

Page 7: Computer Science 313 – Advanced Programming Topics.

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

Page 8: Computer Science 313 – Advanced Programming Topics.

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

Page 9: Computer Science 313 – Advanced Programming Topics.

Code For a[k] = 2 * x;

ld [sp+x], r0 ! r0 = x

Page 10: Computer Science 313 – Advanced Programming Topics.

Code For a[k] = 2 * x;

ld [sp+x], r0 ! r0 = xmul r0, 2, r1 ! r1 = 2 * x

Page 11: Computer Science 313 – Advanced Programming Topics.

Code For a[k] = 2 * x;

ld [sp+x], r0 ! r0 = xmul r0, 2, r1 ! r1 = 2 * x ld [sp+k],r2 ! r2 = k

Page 12: Computer Science 313 – Advanced Programming Topics.

Code For a[k] = 2 * x;

ld [sp+x], r0 ! r0 = xmul r0, 2, r1 ! r1 = 2 * x ld [sp+k],r2 ! r2 = kmul r2, 4, r3 ! r3 = k * 4

Page 13: Computer Science 313 – Advanced Programming Topics.

Code For a[k] = 2 * x;

ld [sp+x], r0 ! r0 = xmul r0, 2, r1 ! r1 = 2 * x ld [sp+k],r2 ! r2 = kmul r2, 4, r3 ! r3 = k * 4ld [sp+a], r4 ! r4 = &(a[0])

Page 14: Computer Science 313 – Advanced Programming Topics.

Code For a[k] = 2 * x;

ld [sp+x], r0 ! r0 = xmul r0, 2, r1 ! r1 = 2 * x ld [sp+k],r2 ! r2 = kmul r2, 4, r3 ! r3 = k * 4ld [sp+a], r4 ! r4 = &(a[0]) add r4, r3, r5 ! r5 = &(a[k])

Page 15: Computer Science 313 – Advanced Programming Topics.

Code For a[k] = 2 * x;

ld [sp+x], r0 ! r0 = xmul r0, 2, r1 ! r1 = 2 * x ld [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

Page 16: Computer Science 313 – Advanced Programming Topics.

Remember the Peephole!

ld [sp+x], r0 ! r0 = xshl r0, 1, r1 ! r1 = 2 * x ld [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

Page 17: Computer Science 313 – Advanced Programming Topics.

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?