Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Latest commit

History

38 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Bytecode instruction set and stack based virtual machine.

Usage

Usage: bytecode <COMMAND>
Commands:
b2r ByteCode source code file -> RawBytes file [ByteCode -> RawBytes]
run Interpret RawBytes file [RawBytes -> Execution]
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-V, --version Print version

RawBytes is data consisting of raw bytecode instructions and data.

$ bytecode b2r -i examples/example1 -o examples/example1.raw
$ bytecode run -i examples/example1.raw

Bytecode file

A small language exists to facilitate bytecode representation and is very simple to use.

opcode [operand] ; comment
push 2 ; Push 2 onto the stack.
push 0xFF ; Push 255 onto the stack.

Opcodes

OpcodeHexOperandStackDescription
Noop0x00Does nothing.
Push0x01value[.., value]Push value onto the stack.
Add0x02[.., A, B] -> [.., R]Pop two values, add them. (A + B = R)
Sub0x03[.., A, B] -> [.., R]Pop two values, subtract them. (A + B = R)
Mul0x04[.., A, B] -> [.., R]Pop two values, multiply them. (A + B = R)
Div0x05[.., A, B] -> [.., R]Pop two values, divide them. (A + B = R)
Mod0x06[.., A, B] -> [.., R]Pop two values, modulo them. (A + B = R)
Jump0x07offset[.., ptr] -> [..]Jump to offset (pointer).
JumpIfFalse0x08offset[.., ptr] -> [..]If top of stack is 0, jump to offset.
GT0x09[.., A, B] -> [.., R]Pop two values, push true(1) if first is greater, else false(0).
LT0x0A[.., A, B] -> [.., R]Pop two values, push true(1) if first is less.
GTE0x0B[.., A, B] -> [.., R]Pop two values, push true(1) if first is greater or equal.
LTE0x0C[.., A, B] -> [.., R]Pop two values, push true(1) if first is less or equal.
EQ0x0D[.., A, B] -> [.., R]Pop two values, push true(1) if equal.
Proc0x0Elength[.., ptr] -> [..]Instruction to delimit a procedure. instructions after length are omitted.
Call0x0Foffset[.., ptr] -> [..]Call procedure at offset.
Ret0x10Return from procedure. See procedures for more details.
Store0x11index[.., value] -> [..]Pop value and store it at index.
Load0x12index[..] -> [.., value]Load value at index and push it onto the stack.
0x13 ~ 0xFDNot used.
Exit0xFEExit the program.
Debug0xFFvalueDescriptionPrint the stack. -1 = pops all, value = pops value items.

Procedures

Proc opcode takes a length operand, which is the number of instructions to skip after the Proc opcode.

Then, after executing the procedure in the CallStack using the Call instruction, the offset(pointer) to return to is stored. After that, Call jump to the first instruction in the procedure and executes the procedure.

Return jumps to the return pointer stored in the CallStack.

proc 2 ; 0, The 2 instructions below are omitted.
noop ; 1
return ; 2, Jump to the pointer (4) stored in the CallStack.
call 1 ; 3, After saving the pointer to return (4) to the CallStack, jump to the pointer 1 of the first instruction of the procedure.
noop ; 4, Executed after the procedure runs.

Example

See examples for more examples.

About

Simple bytecode instruction set and stack based virtual machine

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages