From c2a37faf47b6ee24b12e7e5cd4c7d06ab55f2427 Mon Sep 17 00:00:00 2001 From: lyon1998 <645275593@qq.com> Date: Sun, 17 Oct 2021 20:34:40 +0800 Subject: [PATCH] test JMP for asm line is ok --- port/linux/test/VM-test.cpp | 19 ++++++++++++++ src/PikaVM.c | 49 ++++++++++++++++++++----------------- src/PikaVM.h | 5 ++++ 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/port/linux/test/VM-test.cpp b/port/linux/test/VM-test.cpp index f97113229..318a18981 100644 --- a/port/linux/test/VM-test.cpp +++ b/port/linux/test/VM-test.cpp @@ -140,4 +140,23 @@ TEST(VM, Run_add_1_2_3) { ASSERT_EQ(a, 6); obj_deinit(self); EXPECT_EQ(pikaMemNow(), 0); +} + +TEST(VM, JMP) { + char* pikaAsm = (char*) + "B0\n" + "0 JMP 2\n"; + PikaObj* self = New_TinyObj(NULL); + int lineAddr = 0; + int size = strGetSize(pikaAsm); + Args* sysRes = New_args(NULL); + args_setErrorCode(sysRes, 0); + args_setSysOut(sysRes, (char*)""); + lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes); + lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes); + __clearInvokeQueues(self); + obj_deinit(self); + args_deinit(sysRes); + EXPECT_EQ(lineAddr, 2); + EXPECT_EQ(pikaMemNow(), 0); } \ No newline at end of file diff --git a/src/PikaVM.c b/src/PikaVM.c index 4e246a668..797443c0c 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -100,8 +100,8 @@ int fast_atoi(char* src) { Arg* pikaVM_runAsmInstruct(PikaObj* self, enum Instruct instruct, char* data, - Queue* q0, - Queue* q1, + Queue* invokeQuene0, + Queue* invokeQuene1, int32_t* jmp, Args* sysRes) { if (instruct == NUM) { @@ -116,7 +116,7 @@ Arg* pikaVM_runAsmInstruct(PikaObj* self, return arg_setStr(strArg, "", data); } if (instruct == OUT) { - Arg* outArg = arg_copy(queue_popArg(q0)); + Arg* outArg = arg_copy(queue_popArg(invokeQuene0)); outArg = arg_setName(outArg, data); obj_setArg(self, data, outArg); arg_deinit(outArg); @@ -135,7 +135,7 @@ Arg* pikaVM_runAsmInstruct(PikaObj* self, *jmp = fast_atoi(data); } if (instruct == JEZ) { - Arg* assertArg = arg_copy(queue_popArg(q0)); + Arg* assertArg = arg_copy(queue_popArg(invokeQuene0)); int assert = arg_getInt(assertArg); arg_deinit(assertArg); if (0 == assert) { @@ -190,7 +190,7 @@ Arg* pikaVM_runAsmInstruct(PikaObj* self, methodArgs = New_args(NULL); while (1) { - Arg* methodArg = arg_copy(queue_popArg(q1)); + Arg* methodArg = arg_copy(queue_popArg(invokeQuene1)); if (NULL == methodArg) { break; } @@ -232,7 +232,7 @@ Arg* pikaVM_runAsmInstruct(PikaObj* self, return NULL; } -static int32_t clearQueues(PikaObj* self) { +int32_t __clearInvokeQueues(PikaObj* self) { for (char deepthChar = '0'; deepthChar < '9'; deepthChar++) { char deepth[2] = {0}; deepth[0] = deepthChar; @@ -252,40 +252,43 @@ int32_t pikaVM_runAsmLine(PikaObj* self, char* code = pikaAsm + lineAddr; char* line = strs_getLine(buffs, code); int32_t nextAddr = lineAddr + strGetSize(line) + 1; + int32_t jmp = 0; /* Found new script Line, clear the queues*/ if ('B' == line[0]) { - clearQueues(self); + __clearInvokeQueues(self); uint8_t blockDeepth = line[1] - '0'; goto nextLine; } - char d0[2] = {0}, d1[2] = {0}; - d0[0] = line[0]; - d1[0] = line[0] + 1; + char invokeDeepth0[2] = {0}, invokeDeepth1[2] = {0}; + invokeDeepth0[0] = line[0]; + invokeDeepth1[0] = line[0] + 1; enum Instruct instruct = getInstruct(line); char* data = line + 6; - Queue* q0 = obj_getPtr(self, d0); - Queue* q1 = obj_getPtr(self, d1); - if (NULL == q0) { - q0 = New_queue(); - obj_setPtr(self, d0, q0); + Queue* invokeQuene0 = obj_getPtr(self, invokeDeepth0); + Queue* invokeQuene1 = obj_getPtr(self, invokeDeepth1); + if (NULL == invokeQuene0) { + invokeQuene0 = New_queue(); + obj_setPtr(self, invokeDeepth0, invokeQuene0); } - if (NULL == q1) { - q1 = New_queue(); - obj_setPtr(self, d1, q1); + if (NULL == invokeQuene1) { + invokeQuene1 = New_queue(); + obj_setPtr(self, invokeDeepth1, invokeQuene1); } - int32_t jmp = 0; - Arg* resArg = - pikaVM_runAsmInstruct(self, instruct, data, q0, q1, &jmp, sysRes); + Arg* resArg = pikaVM_runAsmInstruct(self, instruct, data, invokeQuene0, + invokeQuene1, &jmp, sysRes); if (NULL != resArg) { - queue_pushArg(q0, resArg); + queue_pushArg(invokeQuene0, resArg); } goto nextLine; nextLine: args_deinit(buffs); + if (jmp != 0) { + return jmp; + } return nextAddr; } @@ -305,7 +308,7 @@ Args* pikaVM_runAsm(PikaObj* self, char* pikaAsm) { while (lineAddr < size) { lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes); } - clearQueues(self); + __clearInvokeQueues(self); return sysRes; } \ No newline at end of file diff --git a/src/PikaVM.h b/src/PikaVM.h index a9e467d6b..df6865551 100644 --- a/src/PikaVM.h +++ b/src/PikaVM.h @@ -4,5 +4,10 @@ Args* pikaVM_run(PikaObj* self, char* pyLine); Args* pikaVM_runAsm(PikaObj* self, char* pikaAsm); +int32_t pikaVM_runAsmLine(PikaObj* self, + char* pikaAsm, + int32_t lineAddr, + Args* sysRes); +int32_t __clearInvokeQueues(PikaObj* self); #endif \ No newline at end of file