Stack Machine – How Global Variables are Stored

designglobalsstack

How exactly do stack machines (both real and virtual stack machines) store global variables?

I know that C(++) just compile it to the .data segment of a program's memory segmentation.

Then there's Java's JVM which uses a stack-heap system (both data-stack and heap are stacks that grow towards one another) and stores static locals to the heap.

The reason for this question is I'm developing a stack-based VM and I'm trying to decide whether I implement the stack-heap system or use C's .data segment system?

Are there other ways of storing global data besides these two?

Best Answer

I believe your question is fundamentally conflating (a) the executable (file) format and instruction set of a virtual machine — how that format supports the declaration & accesses of globals/statics — with (b) the underlying implementation of such virtual machine, which may be in C or even in another VM-based language, like Java or C#.

Regarding the former (the executable format & instruction set), for instance in the JVM executable (class file) format, is that this format allows declaring fields for classes, and the field can be tagged as static vs. instance. The instruction set, like real machines, provides an addressing mode that is applicable to the access of global or static variables. Since this is a stack machine, these addressing modes are offered within push and pop/store instructions that target statics instead of local variables or instance variables.

Regarding the latter, since the virtual machine itself almost certainly dynamically loads the code to execute/interpret, it does not know in advance about the globals/statics in the code it will be loading. Thus, it is highly impractical for the virtual machine itself to map the globals/statics into the implementation language's concept of globals/statics, and instead it will map the globals/statics into heap objects in the implementation language, translating access instructions in the vm-loaded code into pointer accesses (or object references) during execution or interpretation.

Let's also be clear that the same design for a virtual machine file format and instruction set can be implemented by a variety of widely different ways and in different implementation languages. So, there is the abstraction that represents the virtual machine, namely by defining its file format and instruction set, and then there is the implementation of a virtual machine that observes/executes that format & instruction set.

Related Topic