Saturday, October 12, 2013

Stacks of JVMs

The JVM uses a stack-based architecture [1]. When stack-based architectures evaluate expressions, they treats them like reverse Polish notation. Take, for instance:

(a + b * c) / (a + d * c - e)

This becomes in reverse Polish:

a b c * + a d c * + e - /

All this is saying is: push each variable on the stack and pop them when we sequentially see an operator, operate on it, and push the result back on the stack.

The downside of stack-based architectures is the potential to push and pop more than is necessary. In the example above, we push a and c twice. The upside is that the instructions set is simple and dense which can increase efficiency.

Of course, the code may be JITed in which case the native machine's architecture is used.

[1] Prof David Wentzlaff of Princeton University via Coursera.

No comments:

Post a Comment