This is a proposal for the stack based vm. It is somewhat based on my previous StackVM. The only difference being that all operations happen on the stack and not in registers.
Registers
ip Points to the current instructions, adjusted after the instruction finishes
sp Points to the current top of the stack.
fp Points to the current stack frame.
Instructions
Instructions can (when it makes sense) dereference their argument.
TODO: Define instruction encoding.
Arithmetic
add
sub
mul
div unsigned division
sdiv signed divison
rem unsigned remainder
srem signed remainder
Floating-point arithmetic
fadd
fsub
fmul
fdiv
frem
fexp
Comparison
lt unsigned less than
slt signed less than
lteq unsigned less than or equal
slteq signed less than or equal
gt unsigned greater than
sgt signed greater than
gteq unsigned greater than or equal
sgteq signed greater than or equal
flt
fgt
Bitwise instructions
shr Shift right
shl Shift left
and
xor
or
not
Type conversions
inttofp Integer to floating-point
sinttofp Signed integer to floating-point
fptoint Floating-point to signed integer.
Stack instructions
asp Add a given value signed int to the stack pointer
load Load a value from fp + offset onto the stack
store Pop a value from the stack to fp + offset
push Push an immediate value onto the stack
Memory IO
read Read a value from an address and push it onto the stack
write Write a value from the stack to an address
copy Copy a value from an address to another address
move Move a value from an address to another address
malloc Allocate memory, pushing the address onto the stack
free Free memory
Control-flow
jmp Unconditional jump to address
jmpz Jump to address if top of the stack is 0
jmpnz Jump to address if top of the stack is not 0
call Unconditional jump to address. Pushes a stack frame
This is a proposal for the stack based vm. It is somewhat based on my previous StackVM. The only difference being that all operations happen on the stack and not in registers.
Registers
ip
Points to the current instructions, adjusted after the instruction finishessp
Points to the current top of the stack.fp
Points to the current stack frame.Instructions
Instructions can (when it makes sense) dereference their argument.
TODO: Define instruction encoding.
Arithmetic
add
sub
mul
div
unsigned divisionsdiv
signed divisonrem
unsigned remaindersrem
signed remainderFloating-point arithmetic
fadd
fsub
fmul
fdiv
frem
fexp
Comparison
lt
unsigned less thanslt
signed less thanlteq
unsigned less than or equalslteq
signed less than or equalgt
unsigned greater thansgt
signed greater thangteq
unsigned greater than or equalsgteq
signed greater than or equalflt
fgt
Bitwise instructions
shr
Shift rightshl
Shift leftand
xor
or
not
Type conversions
inttofp
Integer to floating-pointsinttofp
Signed integer to floating-pointfptoint
Floating-point to signed integer.Stack instructions
asp
Add a given valuesigned int
to the stack pointerload
Load a value fromfp + offset
onto the stackstore
Pop a value from the stack tofp + offset
push
Push an immediate value onto the stackMemory IO
read
Read a value from an address and push it onto the stackwrite
Write a value from the stack to an addresscopy
Copy a value from an address to another addressmove
Move a value from an address to another addressmalloc
Allocate memory, pushing the address onto the stackfree
Free memoryControl-flow
jmp
Unconditional jump to addressjmpz
Jump to address if top of the stack is0
jmpnz
Jump to address if top of the stack is not0
call
Unconditional jump to address. Pushes a stack frameret
Return from the current stack frame