All Flashcards

1
Q

What are static links used for

A

They are used to implement local variables when nested functions are allowed in a language (stack based machine)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are static links implemented

A

We add a pointer s to a functions stack frame which points to the stack frame of the definer. This allows us to access non-local values that can be found elsewhere in the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When can static linking fail? What’s the solution?

A

When a function is returned as a value as we can lose the “environment” of its free variables from the stack. Instead we need to use closures

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why do we need calling conventions for registers?

A

Otherwise we may overwrite registers or use values from the wrong registers - caller and callee code may use overlapping sets of registers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Caller saved register

A

When a caller saves a register value for later before making a call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Callee saved register

A

The caller can be assured that the callee will leave the register intact

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When do we use register spilling?

A

When all the registers are in use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is register spilling?

A
  1. Move some register values to the stack
  2. Use registers for computation
  3. Restore registers to their original value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Register pros/cons

A

Pro: fast execution
Con: explicit argument location so instructions are longer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Stack pros/cons

A

Pro: implicit argument locations so shorter instructions
Con: slower execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is inline expansion?

A

Replace a function call site with the body of the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Inline expansion pros/cons

A

Pros:
- avoid building activation records at runtime
- may allow further optimisations (constant propagation)
Cons:
- may lead to code bloat
- need to be careful with variable scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is constant propagation/folding?

A

Propagate constants (i.e. if int y = 2; replace y with 2 where it appears in the code) and evaluate simple expressions AT COMPILE TIME

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is peephole optimisation?

A

Sweep a window over the code sequence looking for instances of simple code patterns that can be rewritten to better code e.g. eliminate useless combinations (push0; pop), improve control flow goto L1 … L1: goto L2 => goto L2 … L1: goto L2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain how “e handle f” and “raise e” work

A

“e handle f”
If e evaluates to a value v “normally” then v is the result of the entire expression
Otherwise if the value v’ is “raised” during evaluation of e the result of the expression is f v’

“raise e”
evaluate expression e to value v then raise v as an exceptional value which can only be “handled”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the stack look like when we encounter “e handle f” and later “raise v”

A
  1. push a special frame for the handler
  2. execute code
  3. current frame contains “raise v”
  4. “unwind” call stack and replace handle frame with frame for f on top of v
17
Q

What is the root set?

A

Collection of registers and stack currently in use by program

18
Q

What is reference counting?

A

Keep track of the number of pointers to each object. When the object is created set the count to 1. Every time a new pointer to the object is created, increment the count. Every time an existing pointer to the object is destroyed, decrement the count. When the reference count goes to 0 the object is unreachable garbage.

19
Q

What is the idea behind tracing and what schemes are tracing schemes?

A

Find all objects reachable from root set e.g. mark and sweep; copying collection; generational garbage collection.

20
Q

Reference counting pros/cons

A

Pros: incremental - no long pauses to collect
Cons:
can’t detect cycle => memory leaks
space/time overhead to maintain count

21
Q

What is mark and sweep?

A

Mark phase: depth first traversal of object graph from root to mark live data
Sweep phase: iterate over entire heap adding unmarked data back onto the free list

22
Q

What is copying collection?

A

Use two heaps: one used by program, the other unused until GC time

At GC:

  1. start at the roots and traverse the reachable data
  2. copy reachable data from the active heap to the other heap
  3. dead objects are left behind
  4. heaps switch roles
23
Q

Copying collection pros/cons

A

Pros:
simple and collects cycles
run-time proportional to number of live objects
automatic compaction eliminates fragmentation
Cons:
twice as much memory used as program requires
long GC pauses - bad for real-time apps

24
Q

What is generational copy collection?

A

First we observe that most new objects “die” very quickly.
Therefore we introduce heaps for different generations of objects:
Old generations are collected less frequently than newer ones.