diff --git a/lib/include/lbox.h b/lib/include/lbox.h index 3779558..80060b0 100644 --- a/lib/include/lbox.h +++ b/lib/include/lbox.h @@ -27,13 +27,18 @@ struct sLaikaB_box { }; LAIKA_FORCEINLINE void* laikaB_unlock(struct sLaikaB_box *box, void *data) { - struct sLaikaV_vm vm = {.pc = 0}; + struct sLaikaV_vm vm = { + /* boxes have 2 reserved constants, [0] for the output, [1] for the input */ + .constList = { + LAIKA_MAKE_VM_PTR(box->unlockedData), + LAIKA_MAKE_VM_PTR(data), + }, + .code = {0}, + .stack = {0}, + .pc = 0 + }; + memcpy(vm.code, box->code, LAIKA_VM_CODESIZE); - - /* boxes have 2 reserved constants, 0 for the output, 1 for the input */ - vm.constList[0].ptr = box->unlockedData; - vm.constList[1].ptr = data; - laikaV_execute(&vm); return (void*)box->unlockedData; } diff --git a/lib/include/lvm.h b/lib/include/lvm.h index e530f37..de63e3e 100644 --- a/lib/include/lvm.h +++ b/lib/include/lvm.h @@ -32,11 +32,11 @@ struct sLaikaV_vm { int pc; }; -#define LAIKA_MAKE_VM(_consts, _code) (struct sLaikaV_vm)({.constList = _consts, .code = _code, .pc = 0, .stack = {}}) +#define LAIKA_MAKE_VM(_consts, _code) {.constList = _consts, .code = _code, .pc = 0, .stack = {0}} /* constants */ -#define LAIKA_MAKE_VM_INT(_i) (struct sLaikaV_vm_val)({.i = _i}) -#define LAIKA_MAKE_VM_PTR(_ptr) (struct sLaikaV_vm_val)({.ptr = _ptr}) +#define LAIKA_MAKE_VM_INT(_i) {.i = _i} +#define LAIKA_MAKE_VM_PTR(_ptr) {.ptr = _ptr} /* instructions */ #define LAIKA_MAKE_VM_IA(opcode, a) opcode, a #define LAIKA_MAKE_VM_IAB(opcode, a, b) opcode, a, b @@ -49,6 +49,7 @@ enum { OP_READ, /* stk_indx[uint8_t].i = *(int8_t*)stk_indx[uint8_t] */ OP_WRITE, /* *(uint8_t*)stk_indx[uint8_t].ptr = stk_indx[uint8_t].i */ OP_INCPTR, /* stk_indx[uint8_t].ptr++ */ + OP_DECPTR, /* stk_indx[uint8_t].ptr-- */ /* arithmetic */ OP_ADD, /* stk_indx[uint8_t] = stk_indx[uint8_t] + stk_indx[uint8_t] */ @@ -110,6 +111,11 @@ LAIKA_FORCEINLINE void laikaV_execute(struct sLaikaV_vm *vm) { vm->stack[ptr].ptr++; break; } + case OP_DECPTR: { + uint8_t ptr = READBYTE; + vm->stack[ptr].ptr--; + break; + } case OP_ADD: BINOP(+); case OP_SUB: BINOP(-); case OP_MUL: BINOP(*); diff --git a/tools/vmtest/src/main.c b/tools/vmtest/src/main.c index 11f3cab..ede8134 100644 --- a/tools/vmtest/src/main.c +++ b/tools/vmtest/src/main.c @@ -18,13 +18,13 @@ int main(int argv, char **argc) { }; struct sLaikaB_box box = { - {0}, /* reserved */ - { /* stack layout: + .unlockedData = {0}, /* reserved */ + .code = { /* stack layout: [0] - unlockedData (ptr) [1] - data (ptr) [2] - key (uint8_t) [3] - working data (uint8_t) - */ + */ LAIKA_MAKE_VM_IAB(OP_LOADCONST, 0, 0), LAIKA_MAKE_VM_IAB(OP_LOADCONST, 1, 1), LAIKA_MAKE_VM_IAB(OP_PUSHLIT, 2, 0xDE),