• This repository has been archived on 19/Jan/2021
  • Stars
    star
    976
  • Rank 45,035 (Top 1.0 %)
  • Language
    C
  • License
    GNU General Publi...
  • Created about 10 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

"interesting" VM in C. Let's see how this goes.

THIS PROJECT IS UNSTABLE AND DEPRECATED

I have since started slow work on a more stable, better thought-out project called RabbitVM. It doesn't quite have the same level of documentation but it should work much better.

CarpVM

This is a project I've been slowly working on for about half a year now. The goal is to try and build a small (and decently reliable) VM from the ground up, learning more and more C as I go.

Right now there are instructions, registers, a stack, data memory, and calls. Not sure if recursion works... it should in theory. Haven't tested.

CONSIDER THIS PRE-ALPHA SOFTWARE. Things change more than they do in Rust!

Build Status     Flattr this

Installation

Getting Carp

If you already have a local copy (cloned before submodule was added)

  • git fetch
  • git rebase
  • git submodule init
  • git submodule update

If you don't already have a local copy

  • git clone --recursive

NOTE: If git submodule status doesn't return a SHA1 hash and repository name, you have a problem. NOTE: Recursive clone is for testing library. If not included, tests will not build.

Building Carp

  1. make
  2. make test (optional)
  3. make install
  4. make clean (optional)

Tested Machines

Arch (bit) OS (with version, name) Compiler (with version)
64-bit Ubuntu 12.04 (precise) gcc 4.6.3
64-bit Ubuntu 14.04 (trusty) gcc 4.8.2
32-bit Ubuntu 14.04 (trusty) gcc 4.8.2
64-bit OS X 10.9.4 (Mavericks) clang 3.2
32-bit OS X 10.9.4 (Mavericks) gcc 4.2.1
64-bit Arch Linux 3.13.6-1 clang 3.4.2
64-bit? Arch Linux 3.13.6-1 gcc 4.9.0 20140604

Use

NOTE: See carp -h for help with command-line options.

Interpreter

  1. Write a Carp file - see SYNTAX.md. More formal spec coming.
  2. Run ./carp.out -f your_file.carp.

API

  1. Include carp/carp_machine.h in your program.
  2. Run gcc program.c /usr/local/lib/libcarp.a -o program.out.

Instruction set

Opcode Arguments Description
HALT exit code Sets ext to given code, halts, and attempts to clean up stack, data memory, and label memory.
NOP Does nothing. Seriously.
LOADR reg, val Loads given integer value into given register.
LOAD diff Loads value at location fp + diff in the stack.
STORE diff, val Stores value at location fp + diff.
MOV dst, src Copies contents of src register into dst register.
ADD Pops the top two integers from the stack and pushes their sum.
SUB Pops the top two integers from the stack and pushes the difference (lower minus upper).
MUL Pops the top two integers from the stack and pushes their product.
MOD Pops the top two integers from the stack and pushes lower % upper.
SHR Pops the top two integers from the stack and pushes lower >> upper.
SHL Pops the top two integers from the stack and pushes lower << upper.
NOT Pops top integer from stack and pushes bitwise NOT of that integer.
XOR Pops the top two integers from the stack and pushes bitwise XOR..
OR Pops the top two integers from the stack and pushes bitwise OR.
AND Pops the top two integers from the stack and pushes bitwise AND.
INCR reg Increments value in given register.
DECR reg Decrements value in given register.
INC Increments the value at the top of the stack.
DEC Decrements the value at the top of the stack.
PUSHR reg Pushes value in given register.
PUSH val Pushes given value.
POP reg Pops an integer from the stack and dumps it into given register.
CMP Pops the top two integers from the stack and checks if equal. 0 means equal. Pushes result.
LT Pops the top two integers from the stack and checks if lower < upper. Pushes result.
GT Pops the top two integers from the stack and checks if lower > upper. Pushes result.
JZ addr Jumps to given absolute address if top of the stack is 0.
RJZ diff Adds differential to ip (relative jump) if top of the stack is 0.
JNZ addr Jumps to given absolute address if top of the stack is not 0.
RJNZ diff Adds differential to ip (relative jump) if top of the stack is not 0.
JMP addr Jumps to given absolute address unconditionally.
RJMP diff Adds differential to ip (relative jump) unconditionally.
CALL key/addr Save state and set IP to value in data memory at key. Function may return value in ax.
RET Put top of the stack into ax and load previous state.
PREG reg Prints contents of given register.
PTOP Peeks top of stack and prints top value.

Registers

Name Purpose
r0 ... r9 General purpose.
ax Return value for user-defined function.
bx, cx, dx, rx ... something in the future. Just taking up space for now.
ip Instruction pointer. Used for keeping place in code, gotos, calling, etc.
sp Stack pointer.
fp Frame pointer. Used to keep state for function calls.
gbg Garbage register mainly used for popping.
run Boolean - is machine running?
ext Exit code.

How to contribute

One way:

  1. Check out the TODO.md file to see if anything needs doing.
  2. Write some code (following existing implicit style) and submit a pull request.

Another way:

  1. Write some code for a feature you want to exist and submit a pull request.

License

GPLv3. See LICENSE.txt.

More Repositories

1

scrapscript

A functional, content-addressable programming language.
Python
140
star
2

ghuloum

Following [An Incremental Approach to Compiler Construction](http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf)
C
57
star
3

tekknolagi.github.com

My personal website
C
52
star
4

indexer

Torrent indexer written in Ruby/Sinatra. Called Brightswipe.
Ruby
32
star
5

ddcg

Implementation of destination-driven code generation with control destinations. See [post.md](post.md)
C++
21
star
6

gecho

Simple stack language in C. Has a few basic commands; working to implement a `while` loop and user-defined functions. If statement would be nice. HC SVNT DRACONES.
C
16
star
7

icdemo

A small inline caching demo.
C
14
star
8

distlisp

Distributed Lisp interpreter in Erlang.
Erlang
11
star
9

doge_api

Minimal Ruby wrapper for the DogeAPI
Ruby
8
star
10

tuftscs-tips

A resource for Tufts Computer Science students who are interested in learning more about the classes they are currently taking.
7
star
11

stoneknifecpp

C++ port of [stoneknifeforth](https://github.com/kragen/stoneknifeforth)
Forth
6
star
12

isdt

The website for the Introduction to Software Development Tooling course.
HTML
6
star
13

pyddcg

A small implementation of destination-driven code generation in Python
Python
5
star
14

dcheck

C++ dynamic assertion library
C
4
star
15

droplet

very simple drag/drop image sharing
PHP
4
star
16

condensation

Ruby
4
star
17

resume-pub

TeX
3
star
18

katecount

Erlang
3
star
19

dotfiles

Shell
3
star
20

kefir

C
2
star
21

weechat-nostr

Python
2
star
22

shortener

Small URL shortener with Ruby/Sinatra
Ruby
2
star
23

glug

A tiny message board. http://glug.bernsteinbear.com
Ruby
2
star
24

blog-lisp

Source code for the blog series Writing a Lisp.
OCaml
2
star
25

100prisoners

Python
2
star
26

stackx

OCaml
2
star
27

tablam

Rust
2
star
28

lispcompiler.com

Following [An Incremental Approach to Compiler Construction](http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf)
2
star
29

wakey

Python
2
star
30

receipt-manager

Ruby
2
star
31

valgrind

C
1
star
32

jelatine

C
1
star
33

option

OCaml
1
star
34

mastercollection

Python
1
star
35

opendata

Ruby
1
star
36

code

Tidbits
C
1
star
37

i3-config

Shell
1
star
38

fmt

Small C string formatting library
C
1
star
39

styx

Styx programming language and its compiler
D
1
star
40

kamui

Go
1
star
41

ninja-demo

Python
1
star