Yet Another Virtual Machine

This project provides 3 features:

Description

The interpreter works with:

The following structures are needed:

Each byte of the program is an instruction that has to be executed, an instruction is described by the following structure:

1 bit5 bits2 bits
has operandopcodesize

Source code

Instructions that takes parameters can have them on the stack (has operand = 0) or in the bytes following the instruction (has operand = 1). If the instruction produce a result, it will always be pushed on the stack.

The opcode bits identifies the instruction.

The size bits identifies the size of the parameters/result:

Value 00 01 10 11
Size 8 bits 16 bits 32 bits 64 bits

NB: There are some instructions that are size independant, their opcode is then 7 bits long.

PUSH8 42
NOT8
PUSH8 1
INT

The above code push 42 on the stack, then apply a binary not on this value, and calls the interuption number 1 (interuptions are special functions built in the interpreter), which prints the stack head.

It is translated in the following byte array:

PUSH8 (1 00000 00) 42 NOT8 (0 01001 00) PUSH8 (1 00000 00) 1 INT (0 1111110)
NOT8 42
PUSH8 1
INT

The above code behave identically but we pass the parameters directly instead of via the stack.

It is translated in the following byte array:

NOT8 (1 01001 00) 42 INT (1 1111110) 1

Implementation

NB: Using the C headers stdlib.h, stdint.h and stdbool.h, we have access to the following types:

TypeDescriptionBytes
size_tAn (unsigned) integer type capable of storing a memory size (ie. with sizeof())Architecture dependent
(u)int8_tAn (unsigned) integer type capable of storing 8 bits1
(u)int16_tAn (unsigned) integer type capable of storing 16 bits2
(u)int32_tAn (unsigned) integer type capable of storing 32 bits4
(u)int64_tAn (unsigned) integer type capable of storing 64 bits8
(u)intptr_tAn (unsigned) integer type capable of storing any pointerArchitecture dependent
boolboolean type with values false and true defined1

NB: Using the C headers stdlib.h and string.h we are able to use the following functions:

For the complete list of instructions and what they do:

NB: When there is not enough space left on the stack, it must be reallocated with more space:

Source code