Writing a stack based vm

The question and OP made that clear is how you criticize.

Writing a stack based vm

Here, we are talking about process virtual machines and not system virtual machines. A virtual machine enables the same platform to run on multiple operating systems and hardware architectures.

The Interpreters for Java and Python can be taken as examples, where the code is compiled into their VM specific bytecode. The same can be seen in the Microsoft. What should a virtual machine generally implement? It should emulate the operations carried out by a physical CPU and thus should ideally encompass the following concepts: Stack based, and Register based.

Net CLR, and is the widely used method for implementing virtual machines. The difference between the two approaches is in the mechanism used for storing and retrieving operands and their results. Stack Based Virtual Machines A stack based virtual machine implements the general features described as needed by a virtual machine in the points above, but the memory structure where the operands are stored is a stack data structure.

Operations are carried out by popping data from the stack, processing them and pushing in back the results in LIFO Last in First Out fashion. An advantage of the stack based model is that the operands are addressed implicitly by the stack pointer SP in above image. This means that the Virtual machine does not need to know the operand addresses explicitly, as calling the stack pointer will give Pop the next operand.

Register Based Virtual Machines In the register based implementation of a virtual machine, the data structure where the operands are stored is based on the registers of the CPU. That is, the operands for the instructions are explicitly addressed in the instruction, unlike the stack based model where we had a stack pointer to point to the operand.

For example, if an addition operation is to be carried out in a register based virtual machine, the instruction would more or less be as follows: But unlike the stack, we need to explicitly mention the addresses of the operands as R1, R2, and R3. The advantage here is that the overhead of pushing to and popping from a stack is non-existent, and instructions in a register based VM execute faster within the instruction dispatch loop.

Another advantage of the register based model is that it allows for some optimizations that cannot be done in the stack based approach.

One such instance is when there are common sub expressions in the code, the register model can calculate it once and store the result in a register for future use when the sub expression comes up again, which reduces the cost of recalculating the expression. The problem with a register based model is that the average register instruction is larger than an average stack instruction, as we need to specify the operand addresses explicitly.

Whereas the instructions for a stack machine is short due to the stack pointer, the respective register machine instructions need to contain operand locations, and results in larger register code compared to stack code.

A great blog article I came across At this linkcontains an explanatory and simple C implementation of a register based virtual machine.

A very very simple register-based virtual machine. : coding

It is a process virtual machine, whereby the the underlying Linux kernel of the Android OS spawns a new Dalvik VM instance for every process. Each process in Android has its own Dalvik VM instance. This reduces the chances of multi-application failure if one Dalvik VM crashes. Dalvik implements the register machine model, and unlike standard Java bytecode which executes 8 bit stack instructions on the stack based JVMuses a 16 bit instruction set.

The registers are implemented in Dalvik as 4 bit fields. If we want to dive a bit deep into the internals of how each process gets an instance of the Dalvik VM, we have to go back to the beginning… back to where the Linux kernel of the Android OS boots up:Stack-based VMs always pull from the top of the stack.

In fact, in Lua's implementation the set of registers is the stack. The instruction operands basically give you a way to read values from farther down the stack. In order to do this I wrote an even simpler VM that runs inside of useless called even-more-useless VM (emuvm). EmuVM is a very simple stack-based virtual machine .

Jul 15,  · Examples of stack based VM’s are the Java Virtual Machine, schwenkreis.com CLR, and is the widely used method for implementing virtual machines. Examples of register based virtual machines are the Lua VM, and the Dalvik VM (which we will discuss shortly).

Writing a compiler; which VM? Ask Question. up vote 8 down vote favorite. what pros and cons are there to stack-based vs register-based VMs? Performance and tool support would be important.

writing a stack based vm

I'll be writing the compiler in Haskell, so a good interface with that is a plus. compiler-construction. share. About Us Learn more about Stack Overflow the company How would I go about writing a Virtual Machine [closed] How does a register based virtual machine work? How would you improve Dalvik?

Android's Virtual Machine. Stack based vs Register based Virtual Machine Architecture, and the Dalvik VM A virtual machine (VM) is a high level abstraction on top of the native operating system, that emulates a physical machine.

Writing a compiler; which VM? - Stack Overflow