Semantically, the Dalvik VM has a fresh set of registers for each method, and does not have instructions to access the call stack. But in terms of its implementation, the registers should be saved somehow on method calls and restored on method returns. How does the (Google's implementation of) Dalvik do this?
The registers that dalvik bytecode refers to are not machine registers at all, but they are actually locations on the call stack. Whenever you call into a method, dalvik allocates enough memory on that method's stack frame to hold all the registers that that method needs.
Note that not all calculations will modify the value on the stack immediately, the vm obviously has to load the values into a machine register in order to do the calculations. The results may be kept in a machine register to be used later without immediately writing it back to the corresponding stack location, at the discretion of the VM. The values will be flushed back to the call stack if and when it is needed (i.e. when you call into another method, use various sync constructs, or it needs the register for another calculation, etc.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With