Writing a stack based vm

Most VMs are stack based but this is mainly because they are conceptually easier to understand.

Stack Based vs Register Based Virtual Machine Architecture, and the Dalvik VM

The compiler puts independent steps in between. And not only is it easier to write compilers and jitters that target simple stack machines, it is easier to write other code analysis tools as well.

Each process in Android has its own Dalvik VM instance. How are instruction operands encoded?

Creating a Virtual Machine/Register VM in C

A great blog article I came across at this linkcontains an explanatory and simple C implementation of a register based virtual machine. Code generation is trivial and independent of prior or subsequent code. This can easily fit in writing a stack based vm bits or less.

The IL verifier, for example, can quickly determine when there is a code path through a method that, say, misaligns the stack, or passes the wrong types of arguments to a method.

For example, MuP21 relies on a register called "A", while the more recent GreenArrays processors relies on two registers: This is in essence, how the Dalvik virtual machine is created and used in the Android system.

High cost of factoring out common subexpressions[ edit ] In register machines, a common subexpression a subexpression which is used multiple times with the same result value can be evaluated just once and its result saved in a fast register. Commercial stack machines[ edit ] Examples of stack instruction sets directly executed in hardware include The F18A architecture of the processor GA chip from GreenArrays, Inc.

In addition, most stack-machine instruction is very simple, made from only one opcode field or one operand field. In contrast, register machines require two or three register-number fields per ALU instruction to select operands; the densest register machines average about 16 bits per instruction.

On register based cpus with 64k of storage the code density benefits of a stack based VM are usually sufficient to cancel out the storage overhead of the VM interpreter and support a more feature rich application provided speed of execution is not crucial.

The next three digits will be used for the operands. These can be used as three 1-digit operands, two operands of 1 and 2 digits, or a single 3-digit operand.

Arithmetic instructions directly fetch or store local variables via 4-bit or larger instruction fields. Addressing modes[ edit ] In any assembly language numbers serve multiple uses. The compiler and the CPU do not need special cases for instructions that have separate calculation paths for addresses.

There has to be some data structure that describes the program, so that it can be interpreted and JIT compiled, and "human readable source code" is an awful choice of data structure for reasons outlined above. Microprogrammed stack machines are an example of this.

While licensing issues may have been a consideration there are a number of technical advantages to this decision: This can lead to more data cache traffic than in an advanced stack machine implementation. The top-of-stack address and the next-instruction address.

So excellent prefetching is accomplished easily by keeping operands on the top of stack in fast storage. That superset comes at a complexity cost in some areas, sure, but being able to address more different data items without having to manipulate the stack also turns out to be very handy.

Stack and register machines are extremely simple. Writing a JIT with two-address ops is also trivial. What model should we choose for our VM? The ALU will access this with an index. Interpreters for virtual stack machines are easier to build than interpreters for register or memory-to-memory machines; the logic for handling memory address modes is in just one place rather than repeated in many instructions.

A program has to execute more instructions when compiled to a stack machine than when compiled to a register machine or memory-to-memory machine. Spare registers were used to factor out repeated address calculations.

In a complex machine like Athlon that completes two or more instructions per cycle, the register file allows reading of four or more independent registers and writing of two others, all in one ALU cycle with one-cycle latency. In CLR, for example, local variables are referenced by index on the execution stack, but you cannot dynamically push new variables onto it - you have to predeclare the locals at the beginning of your execution frame, and the only way to push a new execution frame is by calling another function.

The instruction set carries out most ALU actions with postfix reverse Polish notation operations that work only on the expression stack, not on data registers or main memory cells.

The first digit of a machine word will be the instruction number. For example, the UCSD p-System supported a complete student programming environment on early 8-bit microprocessors with poor instruction sets and little RAM, by compiling to a virtual stack machine rather than to the actual hardware.

Here, we are talking about process virtual machines and not system virtual machines. Jitting is not free - an increase in jit complexity is paid by lower performance of the code being jitted.

In a stack machine, most parameters are already on a stack. There is an interesting research paper where the authors have re-implemented the traditional JVM as a register based VM, and recorded some significant performance gains. The execution unit will read each instruction in order and execute it.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.


I'll be writing the compiler in Haskell, so a good interface with that is a plus. compiler-construction.

Stack machine

share. The stack based virtual machine represents instructions as two field structs that are essentially predecoded (and larger in memory) while the register VM uses what boils down to array of words which are then presumably decoded at run time (the paper even contains some kind of argument why the code is represented as array of single-field structs.

I do wish I was writing faster. My life is going through a challenging patch right now, so it's hard to put as much time into the book as I'd like. From then, purely on the quench of performance, I've gone from tree walk interpreter to stack based bytecode vm to register based vm, and now an optionally typed JITed language.

Thank you. Some stack machine instruction sets are intended for interpretive execution of a virtual machine, rather than driving hardware directly. Interpreters for virtual stack machines are easier to build than interpreters for register or memory-to-memory machines; the logic for handling memory address modes is in just one place rather than repeated in.

13 days ago · What is a stack-based virtual machine? There are three main types of Von Neumann machines: due to the fact that they're a little easier to write and you don't have to keep track of your register usage when writing your assembly. Implementing a Virtual Machine in C (mint-body.com) points by freefouran on May 9, I am starting to sound like a broken record, but here it goes.

If you want a more complete tutorial on writing stack based virtual machines, > Parrot is a register-based process virtual machine designed to run dynamic languages efficiently.


Writing a stack based vm
Rated 5/5 based on 50 review