Run-Time Support
Run-Time Support
8. Run-time Support
Laszlo Bszrmenyi
Compilers
Run-time - 1
Run-Time Environment
A compiler needs an abstract model of the runtime environment of the compiled code It must generate code for cooperation with it The run-time environment communicates with the operating system and maybe with the hardware Main tasks
Storage management Handling of run-time errors
Provide information for symbolic debugging Real Time (RTD) resp. Post Mortem Debugger (PMD)
Hardware extensions
Emulated instructions, virtual registers
Laszlo Bszrmenyi Compilers Run-time - 2
Kinds of Storage
Static storage
Code (immutable) Static instance (or module) variables Code Static Data Heap free Stack
Usual subdivison of run-time memory Heap and stack may be co-managed by a VMM
Run-time - 4
De-allocated explicitly
E.g. free in C
Control-Stack
That set of the active procedures of a call-chain Push at call (activation) Pop at return
Laszlo Bszrmenyi
Compilers
Run-time - 5
Sketch of Quicksort
class sort { int Arr[11]; void readarray (); { int i; } int partition (int m, int n); { } void quicksort (int m, int n); { int i; if (n > m) i = partition(m, n); quicksort(m, i-1); quicksort(i+1, n) } } main () { readarray(); Arr[0] := -9999; Arr[10] := 9999; quicksort(1, 9) } // class sort
Laszlo Bszrmenyi
// Array to be sorted: Arr[1].. Arr[9] // Reads 9 integers in Arr[i]; 1 i 9 // Let assume: -9999 < Arr[i] < 9999 // Partitions Arr[m..n] over a separator value V: // Arr[m..p-1] < V and Arr[p+1..n] V; returns p
// As long not sorted (left-right partitions distinct) // Partition // Call quicksort recursively on the left // and on the right partition
// Initialize Arr // Sentinel values (accelerate tests) (- and +) // Initial call of quicksort on 9 elements
Compilers
Run-time - 6
Activation tree
Set by callee push C a ll e r C a ll e e Returned values Actual parameters Saved status Return address static link dynamic link Local variables Temp. variables
PC (program counter) Points to surrounding scope Points to the callers local data
Variable-length data: indirection: A pointer to stack or heap
5. Set access (static) link 6. Set control (dynamic) link 7. Local and temporary variables
Scoping
Names are valid in a certain scope Static scoping
The validity area is defined by the place in program text Nested block can access outer names (via access link)
Dynamic scoping
Validity is defined by actual state of variables Dynamic binding of a variable to a type e.g. class membership
E.g. ((Student)person).matrNum valid, if person is instance of Student
Arr[1] .. Arr[10] x q(1, 9) access link k, v q(1, 3) access link k, v p(1, 3) access link i,j e(1, 3) access link
Run-time - 11
At call of a procedure
Store d[i] in the activation record Let d[i] point to d[0] the new d[1] activation record d[2]
Before returning
Restore d[i]
Laszlo Bszrmenyi
null
Compilers
Call by reference
The address of the actual parameter is passed Assignment to the formal parameter effects the act. par.
(* P (VAR x: INTEGER);*)
a[1]: 10 a[2]: 20
Compilers
Garbage Collection
Explicit de-allocation is error-prone
Memory leaks
Memory never released Bad in server code
p q Tree in use 12 15 20 List unused 7 37 r
Compilers Run-time - 16
59 9
Dangling references
Pointing at released memory very bad!
Difficulties
Careful memory usage Avoid too long pause
Laszlo Bszrmenyi
Reachability of Data
Root set
Data accessed directly, without a pointer E.g. in Java the static field members + stack The root set is always reachable
Compilers
Run-time - 18
Laszlo Bszrmenyi
Compilers
Run-time - 20
Reference counting
Each memory block has a reference counter
If a new reference to the block is set: increment If a reference is deleted: decrement If reference counter == 0, the block is garbage
ref. counter
2 p 1
Run-time - 21
Copying collectors
Memory is partitioned into 2 semispaces A and B
Memory is allocated in A If end reached
Used blocks are copied into B A is now fully free
Disadvantage
Half of the memory remains unused Addresses must be changed at run-time
Especially bad, if memory address is used as a hash value (e.g. in legacy C code)
Run-time - 22
Laszlo Bszrmenyi
Compilers
Partial collection
We collect only a little bit Generational garbage collection
Many generations of memory areas Only the oldest generation is collected Very efficient, if not too much cross-generation references exist
Incremental collection
The reachability analysis is broken into small pieces The collector may oversee garbage
But must never collect non-garbage!