1 year ago
#356297
Solaxun
Stack Based Virtual Machine - do compiled functions have their own "space"?
I'm a complete novice with bytecode VM's so forgive the basic question.
While digging into some resources to learn more about this stuff, I came across Terrence Parr's great video which goes over a stack VM example from his book "Language Implementation Patterns". I follow everything for the most part, but once the video gets to interpreting function calls I got confused about one specific thing. When we "jump" to a function definition within the code memory array, that compiled instruction set (from the function) is in the same "code" memory array as the rest of the code. Is the implication there that functions compile their bytecode to a separate location of the code array (like maybe after everything else)? If not, wouldn't you end up "running into" compiled function bytecode while incrementing through the code array?
For example, imagine the first few lines of the program are something like:
x = 10
define somefunc(x,y){dostuff}
y = 9
...more code ...
If during compilation we simply step through the source-code sequentially, we would end up compiling the function's bytecode in the middle of our "normal" code. Then while interpreting, the VM would "run into" that code.
The video doesn't cover compilation as that is handled by ANTLR, so I'm taking the VM at face value and trying to infer how the compilation part works (for this particular VM). I'm sure at some point further along in my learning I will understand the compilation piece better, but for now I'm trying to understand this specific example.
I could see it working two ways:
- compiled function bytecode lives within each function itself, which is a VM level primitive. We look up the address in the constant pool and the function object which lives there has the compiled bytecode for that particular function. This would be different than what's in the video since the function bytecode would not be part of the main "code" array. I don't know if this is a common pattern, it's just one way I could imagine it working.
- compiled function bytecode has it's own "space" in the code array, either before or after everything else. That code would never be reached while normally stepping through the code array, only if we explicitly jump to it.
java
virtual-machine
programming-languages
bytecode
stack-based
0 Answers
Your Answer