Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Dalvik VM save and restore its registers between method calls?

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?

like image 764
Pteromys Avatar asked Jan 04 '12 03:01

Pteromys


1 Answers

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.).

like image 198
JesusFreke Avatar answered Oct 28 '22 13:10

JesusFreke