From 6fb37b2588d69609097109c0dce88fba46087618 Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 20 Jun 2022 14:58:16 +0200 Subject: [PATCH] spec --- README.md | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ src/interpret.rs | 5 ++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f439b7..067e8c3 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,100 @@ jmp false exit: ``` + +# Specification + +The crapderive virtual machine has 16 registers, `r0` to `r15`. + +It has 100MB (1024^3) bytes of memory, addressed by the addresses `0x0` to `0x40000000` (not included). + +The machine word (register) size is 64 bit. + +Arithmetic is defined as wrappings, and division by zero traps (cannot be caught). + +The VM has a `flag` that is used in some operations. + +## Definitions + +### Place + +A place is a location in the VM. A place can either be a register (`r0`-`r15`) +or a memory address (`[X]` where `X` is an `index`). + +### Index + +An index is used to dereference memory and can be one of the following: + +* A register +* A literal number +* A symbol (TODO) + +### Value + +A value is a value that can be obtained from the VM. A value can either be a place, a literal number or a symbol. + +### Location + +A location is a location in the VM code. It must contain a symbol with the label to redirect execution to. + +## Instructions + +### Mov + +`mov A, B` + +Mov moves the `value` `B` to the `place` `A`. + +### Add + +`add A, B` + +Add adds the `value` `B` to the `place` `A`. +The result is stored in the `place` `A`. + +### Sub + +`sub A, B` + +Sub subtracts the `value` `B` from* the `place` `A`. +The result is stored in the `place` `A`. + +### Mul + +`mul A, B` + +Mul multiplies the `place` `A` with the `value` `B`. +The result is stored in the `place` `A`. + +### Div + +`div A, B` + +Div divides the `place` `A` by the `value` `B`. +The result is stored in the `place` `A`. + +### Cmp + +`cmp A, B` + +Cmp compares the `value` `A` with the `value` `B`. +If they are equal, the `flag` is set, otherwise the `flag` is cleared. + +### Jmp + +`jmp L` + +Jmp jumps to the `location` `L`. The next instruction executed will be the instruction located at `L`. + +### Je + +`je L` + +Je jumps to the `location` `L` if the `flag` is set. If the jump happens, +the next instruction executed will be the instruction located at `L`. + +### Label +`:` + +A label is an identifier (regex: `[a-zA-Z]\w+`) followed by a colon (`:`). Jumps (like `jmp` or `je`) will be able +to jump to this label. A label is applied to the *next* instruction in the code. \ No newline at end of file diff --git a/src/interpret.rs b/src/interpret.rs index d95a971..c90bd45 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -22,6 +22,8 @@ impl Register { } } +const MEMORY_SIZE = 1024 * 1024 * 1024; + struct InterpretCtx { memory: Vec, registers: [u64; 16], @@ -151,9 +153,10 @@ impl InterpretCtx { } } + pub fn interpret(stmts: Vec, spans: Vec) -> Result<()> { let mut ctx = InterpretCtx { - memory: vec![0; 100_000], + memory: vec![0; MEMORY_SIZE], registers: [0; 16], flag: false, stmts,