• Stars
    star
    267
  • Rank 148,763 (Top 4 %)
  • Language
    C
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Virtual computer

RSM

RSM is a virtual computer, a form of virtual machine.

  • simple RISC instruction set with plenty of general-purpose registers.
  • virtual memory means programs have no access to host memory addresses and all memory accesses are checked. It also means all RSM programs have the same address space (currently 48 bits; 256 TiB) and could even be migrated across hosts at runtime.
  • embraces the linear memory model -- addressing memory is a simple and easy to-understand way to deal with data. Prefer memory-mapped I/O over custom system calls.
  • multi-threaded Go-style task scheduler
  • portable and embeddable, without dependencies. (RSM can even build without libc.)
  • simple explicit assembly language
    • compiles to very compact, self-contained "ROM" files
    • RSM comes with an integrated assembler, making it possible to compile & run code at runtime. (It can be disabled with a macro if desired in embedding scenarios.)
    • includes an AST API for code generation without an intermediate assembly step, useful if you want to make a compiler that targets RSM.

Project goals:

  1. learn, have fun -- simplicity
  2. substrate, a thing to make other thing on
  3. longevity -- I want to be able to run a (multimedia) program in 10+ years

"RSM" initially stands for "rsms's smol machine" but can also be interpreted as "Really smol machine", or "Raggedy-ass special mumbojumbo", or the recursive acronym "RSM smol machine" (in case you miss the golden days of PHP), or anything you'd like it to mean! Your imagination is really the limit here my friend.

Status of this project: This is a passion project and thus is not "production grade" stuff. The instruction set and semantics are changing. I'd be thrilled and happy if you play with RSM and build stuff on it, but please do keep in mind that stuff will change. Contributions are welcome after an initial discussion

Work in progress, TODO:

  • Finish the scheduler
  • Framebuffer for drawing graphics (maybe even webgpu?)
  • Audio
  • Lots of little small pieces here and there marked with "TODO" in the code

Try it

Download a binary for your platform and try this:

$ cat > hello.rsm <<END
fun main(i32) {
  const STDOUT = 1
  data message = "Hello world\n"
  R0 = message // address of string
  R1 = 12      // length of string
  R0 = write R0 R1 STDOUT
}
END
$ rsm hello.rsm
Hello world
$

Building & running

$ ./build.sh -debug
$ ./out/debug/rsm -d -R0=15 examples/factorial.rsm
# R0 will contain the result 1307674368000

You'll need the following things to build rsm:

  • bash (or a bash-compatible shell like zsh)
  • ninja (or a ninja-compatible program like samurai)
  • C11 compiler with libc (e.g. clang or GCC)

You can use rsm as a really awkward calculator:

$ echo 'fun x() { R0 = R0 * 2; ret; }' | out/debug/rsm -d -R0=123
# R0 will contain the result 246

RSM assembly can be compiled into a ROM file which can later be executed:

$ echo 'fun x() { R0 = R0 * 2; ret; }' | out/debug/rsm -o multiply.rom
$ out/debug/rsm -d -R0=123 multiply.rom
# R0 will contain the result 246

Example

$ cat <<EXAMPLE > example.rsm
fun factorial(i32) i32 {
    R1 = R0        // ACC = n (argument 0)
    R0 = 1         // RES (return value 0)
    ifz R1 end     // if n==0 goto end
  b1:              // <- [b0] b1
    R0 = R1 * R0   // RES = ACC * RES
    R1 = R1 - 1    // ACC = ACC - 1
    if R1 b1       // if n!=0 goto b1
  end:             // <- b0 [b1]
    ret            // RES is at R0
}
EXAMPLE
$ out/debug/rsm -d -R0=15 example.rsm
# R0 will contain the result 1307674368000

See the examples/ directory for more.

Instruction Set Architecture

Instructions are fixed-size, 32 bits wide, little endian. PC and jump- & branch destinations are expressed in #instructions rather than bytes. There is room for 256 operations and 32+32 (int+fp) registers (8 bit OP, 5 bit reg) Most instructions accept reg or immediate (i bit is set) as last argument

        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  bit   โ”‚3 3 2 2 2 2 2 2โ”‚2 2 2 2 1โ”‚1 1 1 1 1โ”‚1 1 1 1  โ”‚ โ”‚               โ”‚
        โ”‚1 0 9 8 7 6 5 4โ”‚3 2 1 0 9โ”‚8 7 6 5 4โ”‚3 2 1 0 9โ”‚8โ”‚7 6 5 4 3 2 1 0โ”‚
        โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  ABCD  โ”‚         D (8) โ”‚  C (5)  โ”‚  B (5)  โ”‚  A (5)  โ”‚iโ”‚     OP (8)    โ”‚
        โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  ABCw  โ”‚                  C (13) โ”‚  B (5)  โ”‚  A (5)  โ”‚iโ”‚     OP (8)    โ”‚
        โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  ABw   โ”‚                            B (18) โ”‚  A (5)  โ”‚iโ”‚     OP (8)    โ”‚
        โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  Aw    โ”‚                                      A (23) โ”‚iโ”‚     OP (8)    โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Registers:

  • 30 general-purpose integer registers R0โ€ฆR29
  • 30 general-purpose floating-point registers F0โ€ฆF29
  • Context register CTX (R30)
  • Stack pointer SP (R31)
  • Floating-point status FPSR (F31)
  • TODO: is a fp control reg needed for stuff like 0div traps? No... no.

Calling convention

  • first 8 integer argument/return values in R0โ€ฆR7, rest on stack
  • first 8 F.P. argument/return values in F0โ€ฆF7, rest on stack
  • anything larger than the register size goes on stack
  • caller saves R0โ€ฆR18, F0โ€ฆF18 (owned by callee)
  • callee saves R19โ€ฆR29, F19โ€ฆF29 (owned by caller)
  • convention inspired by AAPCS64

Callee-owned registers

Callee-owned (caller-saved, temporary) registers. Caller needs to save these before a call (if caller uses them.) Callee can freely use these registers.

R0โ€ฆR7   1stโ€ฆ8th integer argument/return value
F0โ€ฆF7   1stโ€ฆ8th floating-point argument/return value
R8โ€ฆR18  General purpose
F8โ€ฆF18  General purpose

Caller-owned registers

Caller-owned (callee-saved, long-lived) registers. Caller does not need to save these registers. Callee using these must save and later restore their values before returning.

R19โ€ฆR29   General purpose
F19โ€ฆF29   General purpose
SP (R31)  Stack pointer

Special registers

CTX  (R30)  Context (like AAPCS platform reg and Go's G)
SP   (R31)  Stack pointer
-    (F30)  Reserved (unused)
FPSR (F31)  Floating-point status

Assembly language

Syntax

White-space is ignored

file       = (fundef | constdef | datadef)*

constdef   = "const" name type? "=" expr ";"
datadef    = "data" name (type ("=" expr)? | "=" expr) ";"

fundef     = "fun" name "(" params? ")" result? funbody?
params     = param ("," param)*
result     = param ("," param)*
param      = name type | type
funbody    = "{" block0? block* "}"
block0     = blockstmt*
block      = name ":" blockstmt*
blockstmt  = operation | assignment | binop | constdef | datadef

type       = inttype | arraytype
inttype    = "i1" | "i8" | "i16" | "i32" | "i64"
arraytype  = type "[" intlit "]"

operation  = opcode operand*
           ; brz R1 end
binop      = operand ("-" | "+" | "*" | "/") operand
           ; x + 3
assignment = reg "=" (operation | operand) ";"
operand    = reg | literal | name

literal    = intlit
intlit     = "-"? (binlit | declit | hexlit)
binlit     = "0b" ("0" | "1")+
declit     = (0-9)+
hexlit     = "0x" (0-9A-Fa-f)+

name       = ("_" | A-Za-z | uniprint) ("_" | A-Za-z | 0-9 | uniprint)

uniprint   = <utf8 encoding of printable unicode codepoint>

opcode     = copy | copyv
           | load | load4u | load4s | load2u | load2s | load1u | load1s
           | store | store4 | store2 | store1
           | push | pop
           | add | sub | mul | adds | subs | muls
           | div | mod
           | and | or | xor | shl | shrs | shru | binv
           | not
           | eq | neq | ltu | lts | lteu | ltes | gtu | gts | gteu | gtes
           | if | ifz | call | jump | ret
           | mcopy | mcmp
           | write | read

Comments are ignored and can appear wherever whitespace can appear

comment      = linecomment | blockcomment
linecomment  = "//" <any character except LF> <LF>
blockcomment = "/*" <any character> "*/"

More Repositories

1

inter

The Inter font family
Python
16,727
star
2

peertalk

iOS and Mac Cocoa library for communicating over USB
Objective-C
3,363
star
3

fb-mac-messenger

โšก๏ธ Mac app wrapping Facebook's Messenger for desktop
Objective-C
2,860
star
4

kod

Programmers' editor for OS X [DEAD PROJECT]
Objective-C
2,296
star
5

node-imagemagick

Imagemagick module for NodeJS โ€” NEW MAINTAINER: @yourdeveloper
JavaScript
1,807
star
6

markdown-wasm

Very fast Markdown parser and HTML generator implemented in WebAssembly, based on md4c
C
1,446
star
7

gotalk

Async peer communication protocol & library
Go
1,193
star
8

estrella

Lightweight and versatile build tool based on the esbuild compiler
TypeScript
1,098
star
9

raster

Raster โ€” simple CSS grid system
CSS
806
star
10

js-lru

A fast, simple & universal Least Recently Used (LRU) map for JavaScript
JavaScript
774
star
11

sol

A sunny little virtual machine
C
516
star
12

scrup

Take a screenshot (in OS X) โ€” paste the URL somewhere a second later
Objective-C
405
star
13

chromium-tabs

[historical] Chromium tabs for cocoa applications (project no longer maintained)
Objective-C
388
star
14

wasm-util

WebAssembly utilities
TypeScript
353
star
15

figplug

Figma plugin builder
TypeScript
336
star
16

cocui

Cocoa meets WebKit for more rapid UI development
Objective-C
329
star
17

ec2-webapp

A template I use to quickly set up Node.js-backed web apps on Amazon EC2
324
star
18

llvmbox

Self contained, fully static llvm tools & libs
C
317
star
19

move

A simple, functional-biased, prototypal and powerful programming language that runs on any ES3 (or better) JavaScript platform, aimed toward people new to programming
JavaScript
302
star
20

immutable-cpp

Persistent immutable data structures for C++
C++
281
star
21

compis

Contemporary systems programming language in the spirit of C
C
196
star
22

uilayer

CALayer-style API for building rich, high-performance UI graphics in WebKit
JavaScript
192
star
23

sublime-theme

My Sublime Text theme
Python
187
star
24

scripter

The Scripter Figma plugin
JavaScript
178
star
25

hue

A functional programming language
C++
170
star
26

gitblog

Git-based blog/cms for PHP, meant as a replacement for Wordpress
PHP
164
star
27

co

A programming language in early development
TypeScript
147
star
28

graphviz

Graphviz web app
JavaScript
118
star
29

LazyDispatch

Thin API and concept on top of libdispatch (aka Grand Central Dispatch) for Cocoa Objective-C code.
Objective-C
102
star
30

afcgi

Asynchronous/multiplexing FastCGI for nginx (incl. ref server implementation)
C
101
star
31

rsms-utils

Collection of CLI programs to help with everyday computer life
Shell
99
star
32

colang

Programming language and compiler โ€”WORK IN PROGRESSโ€”
C
71
star
33

xsys

A well-defined system API for abstracting the OS platform
C
68
star
34

figma-plugins

Collection of Figma plugins
TypeScript
67
star
35

mkweb

simple static website generator
JavaScript
63
star
36

fontkit

JS & WASM library for working with fonts
C
62
star
37

js-object-merge

3-way JavaScript Object merging -- Object.merge(v1, v1a, v1b) -> v2
JavaScript
54
star
38

smolmsg

Simple messages
Go
54
star
39

tc

Tokyo Cabinet Python bindings โ€” In need of a new maintainer
C
54
star
40

js-wasmc

Simplifies building of WebAssembly modules in C/C++ and JS
JavaScript
53
star
41

sigi-pixel-font

Sigi pixel fonts [archived]
52
star
42

WebView-OSX-Screensaver

WebKit web view as a screensaver on OS X
Objective-C
52
star
43

Go.tmbundle

TextMate bundle for the Go programming language
51
star
44

oui

Web-app client-server framework developed as part of dropular.net
JavaScript
49
star
45

wlang

Programming language in development
C
47
star
46

smisk

High performance web service framework, written in C but controlled by Python. Used by Spotify infra 2009โ€“2015.
Python
45
star
47

memex

Software for archiving my digital stuff like tweets
Go
45
star
48

ckit

The little C kit
C
44
star
49

workenv

My personal work environment
Emacs Lisp
44
star
50

dropub

DroPub โ€” drop and publish. Simple OS X MenuItem managing secure transfer of files in the background
Objective-C
42
star
51

serve-http

Simple, safe single-file local web server
JavaScript
42
star
52

ghp

Go Hypertext Preprocessor
Go
38
star
53

go-uuid

Binary sortable universally unique identifier
Go
38
star
54

TypoFig

Mac app for assisting type design in Figma
Objective-C
38
star
55

opencv-face-track-basics

Basic code for tracking faces and eyes using OpenCV
C++
37
star
56

qemu-macos-x86-arm64

Run arm64 Linux Alpine virtualized on macOS x86_64 with QEMU
Shell
35
star
57

tspkg

Create small, fast and easily-distributable packages from TypeScript projects
JavaScript
34
star
58

go-immutable

Immutable data structures for Go
Go
34
star
59

webkit-editor

Experimental text editor which runs in the browser
JavaScript
32
star
60

html5-video

Video player in HTML5
JavaScript
30
star
61

jo

Go-style JavaScript ES6 compiler and packager, based on Babel
JavaScript
30
star
62

hovden-stitch

A typeface weekend project from 2002 with a classic "stitching"/"embroidery" look
30
star
63

libcss-osx

Building libcss as Mac OS X universal binary. Developed as part of Kod (rsms/kod)
Objective-C
29
star
64

tumblr-theme-hunch

The theme used on my blog
28
star
65

dawn-lib

Builds Dawn on Linux and macOS as one single easier-to-use library
Shell
28
star
66

browser-require

CommonJS module require() for web browsers
JavaScript
25
star
67

jsont

A minimal and portable JSON tokenizer for building highly effective and strict parsers (in C and C++)
C
25
star
68

prog-lang-tutorial

JavaScript
25
star
69

node-fsdocs

Simple, ACID and versioned file-system based document database
JavaScript
25
star
70

js-fragment

Client-side templating for modern thinkers
JavaScript
24
star
71

node-couchdb-min

Simplistic CouchDB client with a minimal level of abstraction and connection pooling.
JavaScript
24
star
72

fontctrl

Font manager, keeping font files up to date with a distributed repository model
Go
23
star
73

web-clipboard-promise

Demonstrates lazily-evaluated clipboard data on the Web platform
JavaScript
22
star
74

bezier-tangent

Bรฉzier curve toy
JavaScript
22
star
75

twitter-icon

Alternative icon for Twitter.app
21
star
76

dropular-2010

Redacted snapshot of dropular.net, May 2010
JavaScript
21
star
77

dawn-wire-example

[WIP] Demo of a minimal but functional Dawn-based WebGPU client and server
C++
21
star
78

phpab

Abstract Base โ€“ universal PHP runtime library
PHP
18
star
79

NodeCocoa

Embed node.js in Cocoa or write whole Cocoa apps in node.js
Objective-C
17
star
80

ortho-remote

Some code for playing with the Teenage Engineering Ortho Remote
Objective-C
16
star
81

ml-kern

Kerning done by machines (a project to learn more about ML)
JavaScript
16
star
82

asl-logging

Convenience functions and example code for using ASL (Apple System Log facility)
C
14
star
83

js-miniglob

Minimal glob JavaScript implementation ported from Go's path/filepath
JavaScript
13
star
84

hunch-cocoa

An assortment of Cocoa โ€” mixed extensions and additions to Cocoa
Objective-C
13
star
85

wasm-loader

WebAssembly module loader with import resolution
TypeScript
12
star
86

cmdr

Helps writing command-line programs with subcommands in Go
Go
12
star
87

spotifycocoa

Cocoa framework of libspotify
Objective-C
12
star
88

cgui

super duper simple gui for C, wrapping imgui and stb
C
12
star
89

macfusion

Fork of http://svn.macfusionapp.org/macfusion2/trunk/ โ€” With mainly UI changes like menu item icon and OS-standard volume icons) โ€” Download latest release build: http://cloud.github.com/downloads/rsms/Macfusion/Macfusion.zip
Objective-C
12
star
90

hunch-upload

Multiple concurrent files uploads with progress in pure HTML
JavaScript
11
star
91

functional.js

Work in a functional style with JavaScript and TypeScript
JavaScript
11
star
92

coxlang

Programming language w/ subproject that implements the Go scheduler in C++
C++
11
star
93

lolcatos

The lolcat operating system
Assembly
11
star
94

cometps

Simple comet pub/sub
C
9
star
95

node-imgdb

Image fingerprinting
C++
8
star
96

ssl-client-auth-demo

Demonstrates "client-authenticated TLS handshake"
Shell
8
star
97

connect_facebook

Facebook session support for Connect
8
star
98

mode

Node module manager and repository
JavaScript
8
star
99

flup

Drag-and-drop to quickly put images on Flickr
Objective-C
8
star
100

ipvec

Educational persistent vector implementation in C
C
8
star