|
| 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 |
0 commit comments