Skip to content

Commit 53208be

Browse files
committed
Added constant declarations
1 parent 586d3df commit 53208be

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

src/stackvm.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require "./stackvm/**"
2+
3+
module StackVM
4+
puts "Welcome to the stack machine"
5+
end

src/stackvm/semantic/errors.cr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module StackVM::Semantic
2+
3+
# Error codes the machine will use on exit
4+
enum Err : UInt8
5+
REGULAR_EXIT = 0x00 # Regular exit
6+
STACKOVERFLOW = 0x00 # Operation would overflow the stack
7+
STACKUNDERFLOW = 0x01 # Operation would underflow the stack
8+
ILLEGAL_MEMORY_ACCESS = 0x02 # Memory read and write is out-of-bounds
9+
INVALID_INSTRUCTION = 0x03 # Unknown instruction
10+
INVALID_REGISTER = 0x04 # Unknown register
11+
INVALID_JUMP = 0x05 # Jump out of bounds
12+
OUT_OF_MEMORY = 0x06 # Not enough memory
13+
end
14+
15+
end

src/stackvm/semantic/opcodes.cr

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
module StackVM::Semantic
2+
3+
# Opcodes
4+
enum OP : UInt16
5+
6+
# Registers
7+
RPUSH = 0x00 # Push the value of a register onto the stack
8+
RPOP = 0x01 # Pop the top of the stack into a register
9+
INCR = 0x02 # Increment the value inside a register by 1
10+
DECR = 0x03 # Decrement the value inside a register by 1
11+
MOV = 0x04 # Copies the contents of the source register into the target register
12+
13+
# Arithmetic
14+
ADD = 0x05 # Push the sum of the top two values
15+
SUB = 0x06 # Push the difference of the top two values (lower - upper)
16+
MUL = 0x07 # Push the product of the top two values
17+
DIV = 0x08 # Push the quotient of the top two values
18+
REM = 0x09 # Push the remainder of the top two values (lower % upper)
19+
EXP = 0x0A # Push the power of the top two values (lower ** upper)
20+
21+
# Comparisons
22+
CMP = 0x0B # Push 0 if the top two values are equal
23+
LT = 0x0C # Push 0 if the second-highest value is less than the top
24+
GT = 0x0D # Push 0 if the second-highest value is greater than the top
25+
LTE = 0x0E # Push 0 if the second-highest value is less or equal than the top
26+
GTE = 0x0F # Push 0 if the second-highest value is greater or equal than the top
27+
28+
# Bitwise operations
29+
SHR = 0x10 # Shift the bits of the top value to the right n times (lower >> upper)
30+
SHL = 0x11 # Shift the bits of the top value to the left n times (lower << upper)
31+
AND = 0x12 # Push bitwise AND of the top two values
32+
XOR = 0x13 # Push bitwise OR of the top two values
33+
NAND = 0x14 # Push bitwise NAND of the top two values
34+
OR = 0x15 # Push bitwise OR of the top two values
35+
NOT = 0x16 # Push bitwise NOT on the top two values
36+
37+
# Casting instructions
38+
TRUNC = 0x17 # Truncate a value from type1 to type2
39+
SE = 0x18 # Sign-extend a value from type1 to type2
40+
ZE = 0x19 # Zero-extend a value from type1 to type2
41+
42+
# Stack instructions
43+
LOAD = 0x1A # Load a type value located at (fp + offset)
44+
LOADR = 0x1B # Load a type value located at (fp + [reg])
45+
LOADI = 0x1C # Load an immediate type value
46+
STORE = 0x1D # Pop a type value and save at (fp + offset)
47+
STORER = 0x1E # Pop a type value and save at (fp + [reg])
48+
INC = 0x1F # Increment a type value at (fp + offset)
49+
DEC = 0x20 # Decrement a type value at (fp + offset)
50+
51+
# Memory
52+
READ = 0x21 # Read a type value from address and push it onto the stack
53+
READR = 0x22 # Read a type value from [reg] and push it onto the stack
54+
WRITE = 0x23 # Read a type value from the stack and write it to address
55+
WRITER = 0x24 # Read a type value from the stack and write it to [reg]
56+
COPY = 0x25 # Read a type value from source and write it to the given target
57+
COPYR = 0x26 # Read a type value from source and write it to [reg]
58+
59+
# Jumps
60+
JZ = 0x27 # Relative or absolute jump to given offset if top of the stack is 0
61+
JZR = 0x28 # Relative or absolute jump to [reg] if top of the stack is 0
62+
JNZ = 0x29 # Relative or absolute jump to given offset if top of the stack is not 0
63+
JNZR = 0x2A # Relative or absolute jump to [reg] if top of the stack is not 0
64+
JMP = 0x2B # Unconditional relative or absolute jump to given offset
65+
JMPR = 0x2C # Unconditional relative or absolute jump to [reg]
66+
CALL = 0x2D # Relative or absolute jump to given offset, pushing a stack frame
67+
CALLR = 0x2E # Relative or absolute jump to [reg], pushing a stack frame
68+
RET = 0x2F # Return from the current stack frame
69+
70+
# Miscellaneous
71+
NOP = 0x30 # Does nothing
72+
PUTS = 0x31 # Copy a type value from the stack into stdout
73+
HALT = 0x32 # Halts the machine with a given 1 byte exit code from the stack
74+
75+
# Bitmasks
76+
M_S = 0b1000000000000000 # Signed / Unsigned
77+
M_T = 0b0100000000000000 # Integer / Floating-point
78+
M_B = 0b0010000000000000 # 32-bit / 64-bit
79+
M_O = 0b0001111111111111 # Instruction opcode
80+
end
81+
82+
end

src/stackvm/semantic/registers.cr

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module StackVM::Semantic
2+
3+
# Registers of the machine
4+
enum Reg : UInt8
5+
6+
# General purpose registers
7+
R0 = 0x00
8+
R1 = 0x01
9+
R2 = 0x02
10+
R3 = 0x03
11+
R4 = 0x04
12+
R5 = 0x05
13+
R6 = 0x06
14+
R7 = 0x07
15+
R8 = 0x08
16+
R9 = 0x09
17+
R10 = 0x0A
18+
R11 = 0x0B
19+
R12 = 0x0C
20+
R13 = 0x0D
21+
R14 = 0x0E
22+
R15 = 0x0F
23+
24+
# Machine managed
25+
IP = 0x10 # Instruction pointer
26+
SP = 0x11 # Stack pointer
27+
FP = 0x12 # Frame pointer
28+
29+
# Bitmasks
30+
M_C = 0b10000000 # Complete or sub-portion
31+
M_H = 0b01000000 # Left or right half
32+
M_R = 0b00111111 # Register code
33+
end
34+
35+
end

src/stackvm/semantic/size.cr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module StackVM::Semantic
2+
3+
# Each size value denotes a specific amount of bytes
4+
enum Size : UInt64
5+
BYTE = 1
6+
WORD = 2
7+
DWORD = 4
8+
QWORD = 8
9+
LWORD = 16
10+
end
11+
12+
end

src/stackvm/version.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module StackVM
2+
VERSION = "0.1.0"
3+
end

0 commit comments

Comments
 (0)