All Flashcards
What are static links used for
They are used to implement local variables when nested functions are allowed in a language (stack based machine)
How are static links implemented
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
When can static linking fail? What’s the solution?
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
Why do we need calling conventions for registers?
Otherwise we may overwrite registers or use values from the wrong registers - caller and callee code may use overlapping sets of registers
Caller saved register
When a caller saves a register value for later before making a call
Callee saved register
The caller can be assured that the callee will leave the register intact
When do we use register spilling?
When all the registers are in use
What is register spilling?
- Move some register values to the stack
- Use registers for computation
- Restore registers to their original value
Register pros/cons
Pro: fast execution
Con: explicit argument location so instructions are longer
Stack pros/cons
Pro: implicit argument locations so shorter instructions
Con: slower execution
What is inline expansion?
Replace a function call site with the body of the function
Inline expansion pros/cons
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
What is constant propagation/folding?
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
What is peephole optimisation?
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
Explain how “e handle f” and “raise e” work
“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”