Virtual machine

From IFWiki

A Virtual Machine or VM is a computer program which acts as a model of a computer itself. A major advantage of using VMs is that programs targeting the VM can be compiled once and run on any computer with an implementation of that VM, rather than having to be compiled for each different computer architecture and operating system. Other advantages include making it possible to use different programing styles due to the VM's abstraction of the physical system, and providing domain-specific interfaces which are not present in physical systems.

Interactive fiction has had a long association with virtual machines, as Infocom invented one of the earliest VMs, the Z-machine. They wrote implementations of the Z-machine for several computer systems of their day, and many others have written new implementations since then. Because of their use of the Z-machine it is still possible to run their decades-old games on modern computers without them needing to be recompiled.

Interactive fiction virtual machines

Several virtual machines have been invented specially for interactive fiction. These include the Z-machine, Glulx and the TADS VMs.

Implementation strategies

Programs for VMs are usually compiled to bytecode, the format of which is an important part of each VM's specification. The bytecode will contain information about the type of code and which version of the VM it has been compiled for, the initial memory state the VM must be initalised with, and the actual code to run.

Most IF VM implementations consist of a loop which runs through the byte code, executing each instruction in turn. Such implementations are fairly straight forward to make, but may suffer from poor performance, a problem sometimes occurring for works written in Inform 7.

A rarer strategy for implementations is to use a JIT, a "just-in-time" compiler. No IF VMs currently use a JIT to compile directly to the hardware architecture, but several implementations written in dynamic languages do compile the bytecode to the same language or system which the interpreter itself is written in. However as these languages are frequently implemented in VMs which use JIT technology themselves, such implementations have the potential to run at near-native speeds. JIT compiling implementations include Gnusto, Quixe and ZLR.