mlem/
instructions.rs

1use types::*;
2
3#[derive(Serialize, Deserialize, PartialEq, Debug, Copy, Clone)]
4/// Represents a place a value can come from: a register, a memory address, a pointer to memory stored in a register, or a literal value.
5pub enum Address {
6    /// A literal register, like R1.
7    RegAbs(Register),
8    /// A literal memory address, like 0x10.
9    MemAbs(Word),
10    /// A memory address stored in a register. This serves as one level of indirection; 
11    /// for multiple indirection, multiple instructions must be used.
12    MemReg(Register),
13    /// A literal value. Writing to a literal value is a fault.
14    Literal(Word),
15
16}
17
18#[derive(Serialize, Deserialize, PartialEq, Debug, Copy, Clone)]
19/// Specifies a register in the machine. 
20///
21/// This doesn't include the instruction pointer. You have to use  use jump instructions
22/// to mess with that.
23pub enum Register {
24    /// General purpouse register 0
25    R0,
26    /// General purpouse register 1
27    R1,
28    /// General purpouse register 2
29    R2,
30    /// General purpouse register 3
31    R3,
32    /// General purpouse register 4
33    R4,
34    /// General purpouse register 5
35    R5,
36    /// General purpouse register 6
37    R6,
38    /// General purpouse register 7
39    R7,
40    /// Stack position pointer
41    SP,
42    /// Stack base pointer
43    BP,
44}
45
46#[derive(Serialize, Deserialize, PartialEq, Debug, Copy, Clone)]
47/// Possible instructions for the machine to execute.
48/// For each instruction, the first operand is a, second is b, et cetera
49pub enum Instruction {
50    /// Increment IP.
51    NoOp,
52    /// Set a equal to 0
53    Zero(Address),
54    /// Set b equal to a
55    Move(Address, Address),
56    /// Push a onto the output
57    Output(Address),
58    /// Pop from the input into a
59    Input(Address),
60    /// Add the unsigned a to b, storing the result in a
61    Add(Address, Address),
62    /// Subtract the unsigned b from a, storing the result in a
63    Sub(Address, Address),
64    /// Uncontitionally jump to the position given by a
65    Jump(Address),
66    /// Jump to a if the value at b is 0
67    JumpIfZero(Address, Address),
68    /// Jump to a if the value at b is NOT zero
69    JumpNotZero(Address, Address),
70    /// Push a to the stack
71    Push(Address),
72    /// Pop a value from the stack into the given address
73    Pop(Address),
74    /// Gracefully shut down the machine
75    Halt,
76    /// An illegal instruction. Executing this is a Fault.
77    Illegal,
78}