I am currently building a simple Virtual Machine as a side project with the end goal of implementing my own programming language from scratch. Anyway, I am trying to loosely copy many techniques seen in the JVM.
I noticed that when a function is called in the JVM, a stack frame is pushed onto the call stack containing three sections... the local variable array, the operand stack, and the frame data. My question is how does the JVM know how much space to allocate for the operand stack section. Is there a simple rule for determining how large the operand stack for a given function call should be?
Lastly, what happens if the operand stack fills up? Should the stack frame expand itself to compensate? Or should an error be thrown?
how does the JVM know how much space to allocate for the operand stack section
"The maximum depth of the operand stack of a frame is determined at compile-time and is supplied along with the code for the method associated with the frame." JVMS §2.6.2, §4.7.3.
what happens if the operand stack fills up?
As cited above, the size of the operand stack for each individual frame is known beforehand. The computation cannot use more operand stack than specified in the class file for a particular method, otherwise class verification would fail.
"At no point during execution can the operand stack grow to a depth greater than that implied by the max_stack item." JVMS §4.9.2.