ch2-2
ch2-2
•
Dynamic data: heap
–
E.g., malloc in C, new in Java
•
Stack: automatic storage
Addresses
2.10 LEGv8 Addressing for 32-Bit Immediates and
32-bit Constants
• Most constants are small
– 12-bit immediate is sufficient
• For the occasional 32-bit constant
MOVZ: mov wide with zeros
MOVK: e with with keep
mov
e
•
Use with flexible second operand (shift)
MOV X9,255,LSL 16
Z
0000 0000 0000 0000 0000 0000 0000 0000 1111 0000 0000 1111
0000 0000 0000
0000 0000 0000 0000
0000 0000 0000 1111
1111 0000 0000 0000
1111
0000 0000 1111 0000
MOV X9,255,LSL 0
K
Branch Addressing
• B-type
– B 1000 // go to location 10000ten
5 10000ten
6 bits 26 bits
• CB-type
– CBNZ X19, Exit // go to Exit if X19 != 0
181 Exit 19
8 bits 19 5 bits
bits
• Example 2: lock
ADDI
X11,XZR,#1 // copy locked value
again: LDXR
X10,[X20,#0] // read lock
CBNZ
X10, again // check if it is 0 yet
STXR
X11, X9, [X20] // attempt to store
BNEZ
X9,again // branch if fails
– Unlock:
STUR XZR, [X20,#0] // free lock
Translation and Startup
Many compilers
produce object
modules directly
Static linking
Producing an Object Module
• Assembler (or compiler) translates program
into machine instructions
• Provides information for building a
complete program from the pieces
– Header: described contents of object module
– Text segment: translated instructions
– Static data segment: data allocated for the life of
the program
– Relocation info: for contents that depend on
absolute location of loaded program
– Symbol table: global definitions and external refs
– Debug info: for associating with source code
Linking Object Modules
• Produces an executable image
1.Merges segments
2.Resolve labels (determine their addresses)
3.Patch location-dependent and external refs
• Could leave location dependencies for
fixing by a relocating loader
– But with virtual memory, no need to do this
– Program can be loaded into absolute location
in virtual memory space
Loading a Program
• Load from image file on disk into memory
1. Read header to determine segment sizes
2. Create virtual address space
3. Copy text and initialized data into memory
•
Or set page table entries so they can be faulted in
4. Set up arguments on stack
5. Initialize registers (including SP, FP)
6. Jump to startup routine
•
Copies arguments to X0, … and calls main
•
When main returns, do exit syscall
Dynamically Linked Libraries (DLLs)
• Only link/load library procedure when it
is called
– Requires procedure code to be relocatable
– Avoids image bloat caused by static linking of
all (transitively) referenced libraries
– Automatically picks up new library versions
Lazy Linkage
Indirection
table
Linker/loader code
Dynamically
mapped code
FIGURE 2.23 Dynamically linked
library via lazy procedure linkage.
Starting Java Applications
Simple
portable
instruction set
for the JVM
Compiles
Interpret
bytecodes
s
of “hot”
bytecode
methods
into native
code for
host
Together
§
C Sort Example
MOV X19,XZR // i = 0
for1tst: CMP X19, X1 // compare X19 to X1 (i to n) B.GE exit1 //
go to exit1 if X19 ≥ X1 (i≥n)
ADDI X19,X19,#1 // i += 1
B for1tst // branch to test of outer loop
exit1:
The Inner Loop
• Skeleton of inner loop:
– for (j = i − 1; j >= 0 && v[j] > v[j + 1]; j − = 1) {
• Procedure return
BR LR // return to calling routine
Lessons Learnt
• Instruction count and CPI are not
good performance indicators in
isolation
• Compiler optimizations are sensitive to
the algorithm
• Java/JIT compiled code is significantly
faster than JVM interpreted
– Comparable to optimized C in some cases
• Nothing can fix a dumb algorithm!
Pointers
§