androidjvmdalvik

Why Dalvik register-based, but use stack, what is differnce?


Many articles tell about that Dalvik is register-based VM, but also many articles tell that Dalvik use stack for threads where saved methods and cached variables. Maybe I confuse this with keeping classes, variables and methods with executing real operations on processor. I think that this mean VM send operations in stack or registers. But don't find this information. Please help?)


Solution

  • The difference is the use of an "operand stack" or not. With the JVM, variables can only be passed around by pushing/popping their values to/from an operand stack. In this context, values become operands when they exist on the operand stack. To call a method, all the parameters must be on the operand stack. To add two numbers together, both numbers must be on the operand stack, etc.

    With a VM designed without an operand stack, passing variables to a method requires that the variable/register numbers be explicitly encoded in the operations. This generally yields better performance, but the overall size of the encoded operations tends to be larger compared to a design which uses an operand stack.

    If the operations are expected to be compiled to machine code, then whether an operand stack is used or not doesn't really affect performance, because so many transformations are involved.