4 ms·
Something overlooked in this (and many other articles about stack based VMs): if the stack based VM requires a well balanced stack (as is the case for JVM), the
by wokkel 6y ago
Something overlooked in this (and many other articles about stack based VMs): if the stack based VM requires a well balanced stack (as is the case for JVM), the bytecode can be trivially rewritten to a register based VM: number each stack location and trace all basic-blocks to see which stackslot they hit. Then just replace all the push 10, pop with "move 10 to slot/register-1", "load slot/register-1" etc. The converse is much harder, so I really prefer to work with stack based VMs as they are more condense and if speed is needed a linear (in function size) scan can rewrite it to make the mapping to cpu registers much easier.
- yodsanklai 6y ago> a well balanced stack what is a well-balanced stack?
- abecedarius 6y agoThey mean the stack depth is guaranteed to be statically apparent at each point in the control flow. In the JVM the class loader checks this with an analysis phase during loading. (The JVM design makes this harder than it really needs to be, since it doesn't insist on reducible control flow, iirc. You could say OTOH that irreducible control flow is more valuable than trivial analyzability, which is a reasonable take, but conflicts with the above comment's design point of a stack machine being both very simple for an interpreter and not an obstacle to quickly compiling to decent register-machine code. Needing a fancier stack-depth analysis would be that sort of obstacle.)
- RodgerTheGreat 6y agoThe stack is balanced if everything pushed at a given level of scope is popped as that scope closes. This precludes variable-stack-effect sequences as one might have in a Forth definition like: : ?drop dup if drop then ;