An MVP stack based bytecode VM

Overview

TinyVM

An MVP stack-based bytecode VM

This VM runs a simplistic, Turing complete instruction set.

In ~250 LOC with extensive comments, it's meant to be simple to understand and easily reproducible in languages other than Rust.

It also includes a rudimentary compiler which can execute programs in the test_files folder.

Instructions

Instruction Description
Push (isize) Pushes the argument to the top of the stack
Pop Removes the value on top of the stack
Add Pops the top two values and pushes their sum
Sub Pops the top two values and pushes their difference
Mul Pops the top two values and pushes their product
Div Pops the top two values and pushes their quotient
Jump (label) Sets the instruction pointer to the label
JNE (label) Jumps if the top of the stack is not zero
JE (label) Jumps if the top of the stack is zero
JGT (label) Jumps if the top of the stack is greater than zero
JLT (label) Jumps if the top of the stack is less than zero
JGE (label) Jumps if the top of the stack is greater than or equal to zero
JLE (label) Jumps if the top of the stack is less than or equal to zero
Call (procedure) Calls a procedure, setting the stack offset to the current s stack length
Get (usize) Gets index of the stack and copies it to the top
Set (usize) Copies value at the top of the stack to the index
GetArg (usize) Gets nth argument from top of callstack stack offset, used for procedures
SetArg (usize) Sets nth argument from top of callstack stack offset, used for procedures
Noop Doesn't do anything, used by comments to keep instruction pointers correspondent to lines
Print Prints value at the top of the stack as an integer
PrintC Prints value at the top of the stack as an ASCII character
PrintStack Prints the whole stack, used mostly for debugging

You can also set a label with the line label $name, and you can declare a procedure by using Proc $name, Ret, and End. See test_files/procedure.bytecode or test_files/fib_recurse.bytecode for more details.

Examples

In test_files/:

hello_world.bytecode prints "Hello World"

sum.bytecode prints the sum of all integers from 0 to 100

fib.bytecode prints the first 40 fibonacci numbers

fib_recurse.bytecode recursively calculates 35th fibonacci number

Sum

Push 0
Push 0

label loop
    -- [accumulator, index]
    Get 0
    Get 1
    -- [accumulator, index, accumulator, index]
    Add
    -- [accumulator, index, accumulator + index]
    Set 0
    Pop
    -- [accumulator + index, index]

    -- [accumulator, index]
    Incr
    -- [accumulator, index + 1]

    -- [accumulator, index]
    Get 1
    Push 100
    Sub
    -- [accumulator, index, index - 100]
    JNE loop
Pop

Get 0
Print
Push 10
PrintC
> cargo run --release sum.bytecode
4950
>
You might also like...
An embedded key-value storage for learning purpose, which is based on the idea of SSTable / LSM-tree.

Nouzdb An embedded key-value storage for learning purpose, which is based on the idea of SSTable / LSM-tree. Plan Implement a memtable. Implement the

Tools to feature more lenient Polonius-based borrow-checker patterns in stable Rust
Tools to feature more lenient Polonius-based borrow-checker patterns in stable Rust

Though this be madness, yet there is method in 't. More context Hamlet: For yourself, sir, shall grow old as I am – if, like a crab, you could go back

A simplistic functional programming language based around Lisp syntax.

Orchid A simplistic functional programming language based around Lisp syntax. Short taste # function to return the larger list (fn larger-list (as bs)

lelang programming language is a toy language based on LLVM.

lelang leang是一门使用Rust编写,基于LLVM(inkwell llvm safe binding library)实现的编程语言,起初作为课程实验项目,现在为个人长期维护项目。 Target Features 支持8至64位的整形类型和32/64位浮点 基本的函数定义,调用,声明外部

API for the creation character based games in Linux.
API for the creation character based games in Linux.

Console Game Engine for Linux. API for the creation of character based games in Linux. The inspiration came from the olcConsoleGameEngine. This is my

Diosic is an open source web-based music collection server and streamer

Diosic is an open source web-based music collection server and streamer. Mainly suitable for users who need to deploy on servers with low hardware specifications.

Rust crate implementing short & stable ids based on timestamps

Lexicoid Short & stable IDs based on timestamps. Heavily inspired by Short, friendly base32 slugs from timestamps by @brandur. Install Install with ca

A real-time data backend for browser-based applications.

DriftDB DriftDB is a real-time data backend for browser-based applications. For more information, see driftdb.com. Structure of this repo docs/: main

Operating system based off of blog_os, with the goal of running wasm modules as executables

yavkOS - A OS that attempts at running WASM modules as userspace programs Recommended Development Environment You need nix with the flakes, and nix-co

Comments
  • chore: update the comment which cause misleading

    chore: update the comment which cause misleading

    Issue: Some of the comment will misleading others, below is the comments for Sub.

                // Before:
                // [.., a, b]
                //
                // After:
                // [.., b - a]
    

    the code:

    let (a, b) = (stack.pop(), stack.pop());
    

    but actually the the first pop element is a, so the expect comments should be:

                // Before:
                // [.., b, a]
    
    opened by 0753Ljuc 1
Owner
Mikail Khan
¯\_(ツ)_/¯
Mikail Khan
OOLANG - an esoteric stack-based programming language where all instructions/commands are differnet unicode O characters

OOLANG is an esoteric stack-based programming language where all instructions/commands are differnet unicode O characters

RNM Enterprises 2 Mar 20, 2022
A stack based, virtual machine language written in Rust

Stackyy A stack based, virtual machine language written in Rust Description: Stackyy is a stack based, virtual machine language inspired by Forth and

FlawlessCode 2 May 2, 2022
Stack-based programming language which emulates the look and feel of the 60s

Cauchemar Cauchemar is a stack-based programming language inspired by FORTH but more arcane. Emulates the look and feel of a programming language from

Yuki Langley 4 Dec 29, 2022
Stack heap flexible string designed to improve performance for Rust

flexible-string A stack heap flexible string designed to improve performance. FlexibleString was first implemented in spdlog-rs crate, which improved

Sprite 6 Feb 9, 2022
How to be a full stack Rust Developer

How to be a full stack Rust Developer Read Rust API guideline. Test code here and refer to the Rust blog posts at Steadylearner. If you need or know s

Steadylearner 1.2k Jan 8, 2023
A rocksdb.rs wrapper bringing stack and queue functionalities

RocksDB_sq (Stack & Queue) A Rust crate that adds stack and queue functionality to RocksDB. This crate provide a wrapper around a RocksDB database and

Nathan GD 5 May 16, 2023
OP-Up is a hive tool for testing OP-Stack-compatible software modules

op-up Warning This is a work in progress. OP-Up is a hive tool for testing OP-Stack-compatible software modules. This project was born out of the need

nicolas 20 Jun 13, 2023
An expression based data notation, aimed at transpiling itself to any cascaded data notation.

Lala An expression oriented data notation, aimed at transpiling itself to any cascaded data notation. Lala is separated into three components: Nana, L

null 37 Mar 9, 2022
a frontier based evm compatible chain template

Substrate Frontier Node Template A FRAME-based Substrate node with the Ethereum RPC support, ready for hacking ?? Generation & Upstream This template

zero network 2 Oct 6, 2021
A Rust no-std (de)compressor based on PAQ

MASHI まし A 100% no-std compatible Rust implementation of a PAQ style arithmetic coding, context mixing compressor. Its intended use case is compressin

null 7 Dec 14, 2022