WIP more stack ops
This commit is contained in:
parent
1b77649e36
commit
9f8575e5a2
44
vm/vm.c
44
vm/vm.c
|
|
@ -49,8 +49,6 @@ bool step_vm() {
|
|||
lc = 0;
|
||||
/* push parents frame value to reset the heap to */
|
||||
(*header++) = fp;
|
||||
/* push return address to child frame */
|
||||
globals[gmp + 1] = pc;
|
||||
/* increase the mp to new size */
|
||||
mp += FRAME_HEADER_SIZE;
|
||||
/* now set the frame pointer, where the locals start */
|
||||
|
|
@ -102,6 +100,16 @@ bool step_vm() {
|
|||
status = syscall(id, size, rd);
|
||||
return true;
|
||||
}
|
||||
case OP_PUSH: {
|
||||
return false;
|
||||
}
|
||||
case OP_POP: {
|
||||
--sp;
|
||||
return true;
|
||||
}
|
||||
case OP_SET: {
|
||||
return false;
|
||||
}
|
||||
case OP_MEM_ALLOC: {
|
||||
u32 size = stack[--sp];
|
||||
stack[sp++] = mp;
|
||||
|
|
@ -221,6 +229,38 @@ bool step_vm() {
|
|||
status = 0;
|
||||
return true;
|
||||
}
|
||||
case OP_DUP: {
|
||||
u32 a = stack[--sp];
|
||||
stack[sp++] = a;
|
||||
stack[sp++] = a;
|
||||
return true;
|
||||
}
|
||||
case OP_EXCH: {
|
||||
u32 a = stack[--sp];
|
||||
u32 b = stack[--sp];
|
||||
stack[sp++] = b;
|
||||
stack[sp++] = a;
|
||||
return true;
|
||||
}
|
||||
case OP_OVER: {
|
||||
u32 a = stack[sp - 1];
|
||||
stack[sp++] = a;
|
||||
return true;
|
||||
}
|
||||
case OP_PICK: {
|
||||
u32 n = stack[--sp];
|
||||
u32 b = stack[sp - n];
|
||||
stack[sp++] = b;
|
||||
return true;
|
||||
}
|
||||
case OP_ROT: {
|
||||
return false;
|
||||
}
|
||||
case OP_DEPTH: {
|
||||
u32 a = sp;
|
||||
stack[sp++] = a;
|
||||
return true;
|
||||
}
|
||||
case OP_ADD_INT: {
|
||||
MATH_OP(i32, +);
|
||||
}
|
||||
|
|
|
|||
29
vm/vm.h
29
vm/vm.h
|
|
@ -4,10 +4,10 @@
|
|||
#include "libc.h"
|
||||
|
||||
typedef enum {
|
||||
OP_HALT, /* `halt` | halt execution */
|
||||
OP_HALT, /* - `halt` | halt execution */
|
||||
OP_CALL, /* ptr `call` | creates a new frame */
|
||||
OP_RETURN, /* `return` | returns from a frame to the parent frame */
|
||||
OP_SYSCALL, /* `syscall` | id args mem_ptr : does a system call based on id with args */
|
||||
OP_RETURN, /* - `return` | returns from a frame to the parent frame */
|
||||
OP_SYSCALL, /* id args mem_ptr `syscall` - | id args mem_ptr : does a system call based on id with args */
|
||||
OP_LOAD_8, /* &dest `load-8` u8 | push memory[obj1] onto stack as u8 */
|
||||
OP_LOAD_16, /* &dest `load-16` u16 | push memory[obj1] onto stack as u16 */
|
||||
OP_LOAD_32, /* &dest `load` u32 | push memory[obj1] onto stack as u32 */
|
||||
|
|
@ -15,15 +15,22 @@ typedef enum {
|
|||
OP_STORE_16, /* &dest obj1 `store-16`- | memory[dest] = obj1 << 16 */
|
||||
OP_STORE_32, /* &dest obj1 `store` - | memory[dest] = obj1 */
|
||||
OP_MALLOC, /* size `malloc` ptr | allocate 'size + 4' of memory and push ptr to memory on stack */
|
||||
OP_GET, /* &local `get` obj1 | get the value from the local slot */
|
||||
OP_PUSH, /* &local `get` obj1 | get the value from the local slot */
|
||||
OP_POP, /* - `pop` - | removes top item from the stack */
|
||||
OP_SET, /* obj1 &local `set` - | set the value of the local slot */
|
||||
OP_DUP, /* obj1 `dup` obj1 obj1 | duplicates the top of the stack */
|
||||
OP_EXCH, /* obj2 obj1 `exch` obj1 obj2 | swaps the top two values on the stack */
|
||||
OP_OVER, /* obj2 obj1 `over` obj2 | copys the 2nd to the top element and pushes to the stack */
|
||||
OP_PICK, /* N `pick` objN | gets the nth element on the stack and pushes it on top */
|
||||
OP_ROT, /* obj3 obj2 obj1 `rot` obj2 obj1 obj3 | takes the 3rd element and moves it to the top of the stack */
|
||||
OP_DEPTH, /* - `depth` heap_count | pushes the number of elements on the stack to the stack*/
|
||||
OP_MEM_ALLOC, /* size `alloc` ptr | allocate 'size + 4' of memory and push ptr to memory on stack */
|
||||
OP_MEM_CPY_8, /* `memcpy_8` | memory[src1..src1+src2] = memory[dest..dest+src2] */
|
||||
OP_MEM_CPY_16, /* `memcpy_16` | memory[src1..src1+src2] = memory[dest..dest+src2] */
|
||||
OP_MEM_CPY_32, /* `memcpy_32` | memory[src1..src1+src2] = memory[dest..dest+src2] */
|
||||
OP_MEM_SET_8, /* `memset_8` | memory[dest..dest+src2] = local[src1] as u8 */
|
||||
OP_MEM_SET_16, /* `memset_16` | memory[dest..dest+src2] = local[src1] as u16 */
|
||||
OP_MEM_SET_32, /* `memset_32` | memory[dest..dest+src2] = local[src1] as u32 */
|
||||
OP_MEM_CPY_8, /* size src dest `memcpy_8` - | memory[src..src+size] = memory[dest..dest+size] */
|
||||
OP_MEM_CPY_16, /* size src dest `memcpy_16` - | memory[src..src+size] = memory[dest..dest+size] */
|
||||
OP_MEM_CPY_32, /* size src dest `memcpy_32` - | memory[src..src+size] = memory[dest..dest+size] */
|
||||
OP_MEM_SET_8, /* size src dest `memset_8` - | memory[dest..dest+size] = local[src] as u8 */
|
||||
OP_MEM_SET_16, /* size src dest `memset_16` - | memory[dest..dest+size] = local[src] as u16 */
|
||||
OP_MEM_SET_32, /* size src dest `memset_32` - | memory[dest..dest+size] = local[src] as u32 */
|
||||
OP_ADD_INT, /* obj2 obj1 `add_int` obj | obj1 + obj2 then push result on stack */
|
||||
OP_SUB_INT, /* obj2 obj1 `sub_int` obj | obj1 - obj2 then push result on stack */
|
||||
OP_MUL_INT, /* obj2 obj1 `mul_int` obj | obj1 * obj2 then push result on stack */
|
||||
|
|
@ -149,8 +156,8 @@ extern u8 *mem; /* memory */
|
|||
|
||||
#define MATH_OP_NO_CAST(op) \
|
||||
do { \
|
||||
u32 a = stack[--sp]; \
|
||||
u32 b = stack[--sp]; \
|
||||
u32 a = stack[--sp]; \
|
||||
stack[sp++] = a op b; \
|
||||
return true; \
|
||||
} while (0)
|
||||
|
|
|
|||
Loading…
Reference in New Issue