1 Lecture 2 Classes and objects, Constructors, Arrays and vectors.
Lecture 20: Using Arrays and Objects
description
Transcript of Lecture 20: Using Arrays and Objects
![Page 1: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/1.jpg)
LECTURE 20:USING ARRAYS AND OBJECTS
Computer Science 313 – Advanced Programming Topics
![Page 2: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/2.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/3.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/4.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/5.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/6.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/7.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/8.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/9.jpg)
Code For a[k] = 2 * x;
ld [sp+x], r0 ! r0 = x
![Page 10: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/10.jpg)
Code For a[k] = 2 * x;
ld [sp+x], r0 ! r0 = xmul r0, 2, r1 ! r1 = 2 * x
![Page 11: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/11.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/12.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/13.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/14.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/15.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/16.jpg)
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: Lecture 20: Using Arrays and Objects](https://reader036.fdocuments.us/reader036/viewer/2022062323/568160c7550346895dcff5d1/html5/thumbnails/17.jpg)
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?