Home

Awesome

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)

If you don't already have a local copy

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-bitUbuntu 12.04 (precise)gcc 4.6.3
64-bitUbuntu 14.04 (trusty)gcc 4.8.2
32-bitUbuntu 14.04 (trusty)gcc 4.8.2
64-bitOS X 10.9.4 (Mavericks)clang 3.2
32-bitOS X 10.9.4 (Mavericks)gcc 4.2.1
64-bitArch Linux 3.13.6-1clang 3.4.2
64-bit?Arch Linux 3.13.6-1gcc 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

OpcodeArgumentsDescription
HALTexit codeSets ext to given code, halts, and attempts to clean up stack, data memory, and label memory.
NOPDoes nothing. Seriously.
LOADRreg, valLoads given integer value into given register.
LOADdiffLoads value at location fp + diff in the stack.
STOREdiff, valStores value at location fp + diff.
MOVdst, srcCopies contents of src register into dst register.
ADDPops the top two integers from the stack and pushes their sum.
SUBPops the top two integers from the stack and pushes the difference (lower minus upper).
MULPops the top two integers from the stack and pushes their product.
MODPops the top two integers from the stack and pushes lower % upper.
SHRPops the top two integers from the stack and pushes lower >> upper.
SHLPops the top two integers from the stack and pushes lower << upper.
NOTPops top integer from stack and pushes bitwise NOT of that integer.
XORPops the top two integers from the stack and pushes bitwise XOR..
ORPops the top two integers from the stack and pushes bitwise OR.
ANDPops the top two integers from the stack and pushes bitwise AND.
INCRregIncrements value in given register.
DECRregDecrements value in given register.
INCIncrements the value at the top of the stack.
DECDecrements the value at the top of the stack.
PUSHRregPushes value in given register.
PUSHvalPushes given value.
POPregPops an integer from the stack and dumps it into given register.
CMPPops the top two integers from the stack and checks if equal. 0 means equal. Pushes result.
LTPops the top two integers from the stack and checks if lower < upper. Pushes result.
GTPops the top two integers from the stack and checks if lower > upper. Pushes result.
JZaddrJumps to given absolute address if top of the stack is 0.
RJZdiffAdds differential to ip (relative jump) if top of the stack is 0.
JNZaddrJumps to given absolute address if top of the stack is not 0.
RJNZdiffAdds differential to ip (relative jump) if top of the stack is not 0.
JMPaddrJumps to given absolute address unconditionally.
RJMPdiffAdds differential to ip (relative jump) unconditionally.
CALLkey/addrSave state and set IP to value in data memory at key. Function may return value in ax.
RETPut top of the stack into ax and load previous state.
PREGregPrints contents of given register.
PTOPPeeks top of stack and prints top value.

Registers

NamePurpose
r0 ... r9General purpose.
axReturn value for user-defined function.
bx, cx, dx, rx... something in the future. Just taking up space for now.
ipInstruction pointer. Used for keeping place in code, gotos, calling, etc.
spStack pointer.
fpFrame pointer. Used to keep state for function calls.
gbgGarbage register mainly used for popping.
runBoolean - is machine running?
extExit 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.