Accessing and Storing Variables
Local Variables
Review instructions iload and istore from JVM Spec.
Assigning indices (called slots) to local variables:
- in which compiler phase. What information about the symbol do we need to know?
- how to compute the indices?
Function
- maps variable ocurrence into an index (need the symbol table)
Translation Rules for Local Variables
If is local variable and we translate expression, then
For assignment to local variable , we have
Static Variables
Instance Variables (Fields)
To access these we use
- getfield
- putfield
Note: in a lower-level model x.f is often evaluated as mem[x + offset(f)]
- with inheritance, offset(f) must be consistent despite the fact that we do not know exactly what the run-time class of x will be
- this can be very tricky to implement efficiently with multiple inheritance
- general approach: do a run-time test on the dynamic type of an object to decide how to interpret field or method access