diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 1b1790165..bcb46aa08 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -18,7 +18,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/../build/src/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - // "--gtest_filter=instructUnit*", + "--gtest_filter=VM.bytecode*", // "--gtest_filter=stack.str", // "--gtest_filter=VM*", // "--gtest_filter=arg*", diff --git a/port/linux/test/VM-test.cpp b/port/linux/test/VM-test.cpp index af82ddd7e..a7ecb27b5 100644 --- a/port/linux/test/VM-test.cpp +++ b/port/linux/test/VM-test.cpp @@ -857,3 +857,27 @@ TEST(InstructArray, set) { EXPECT_STREQ(log_buff[0], (char*)"3 OUT #12\r\n"); EXPECT_EQ(pikaMemNow(), 0); } + +TEST(VM, bytecode_jjcc) { + char* line = (char*)"a = (1 + 1.1) * 3 - 2 /4.0"; + Args* buffs = New_strBuff(); + char* pikaAsm = Parser_LineToAsm(buffs, line, NULL); + printf("%s", pikaAsm); + ByteCodeFrame byte_frame; + byteCodeFrame_init(&byte_frame); + byteCodeFrame_appendFromAsm(&byte_frame, pikaAsm); + byteCodeFrame_print(&byte_frame); + + PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj); + // pikaVM_runAsm(self, pikaAsm); + pikaVM_runByteCodeFrame(self, &byte_frame); + + float res = obj_getFloat(self, (char*)"a"); + + obj_deinit(self); + args_deinit(buffs); + byteCodeFrame_deinit(&byte_frame); + + ASSERT_FLOAT_EQ(res, 5.8); + EXPECT_EQ(pikaMemNow(), 0); +} diff --git a/port/linux/test/parse-test.cpp b/port/linux/test/parse-test.cpp index c26942b8a..1515e4139 100644 --- a/port/linux/test/parse-test.cpp +++ b/port/linux/test/parse-test.cpp @@ -2133,6 +2133,8 @@ TEST(asmer, asmer_to_instructUnit) { byteCodeFrame_init(&bytecode_frame); byteCodeFrame_appendFromAsm(&bytecode_frame, asm_line); byteCodeFrame_print(&bytecode_frame); + InstructUnit* ins_unit = instructArray_getByOffset(&(bytecode_frame.instruct_array), 4); + instructUnit_print(ins_unit); size_t byteCode_size = byteCodeFrame_getSize(&bytecode_frame); EXPECT_EQ(byteCode_size, 33); EXPECT_STREQ(constPool_getNext(&(bytecode_frame.const_pool)), (char*)"2"); diff --git a/src/PikaVM.c b/src/PikaVM.c index e07071b68..1fd1fcc9d 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -768,15 +768,14 @@ int pikaVM_runInstructUnit(PikaObj* self, VMState* vs, InstructUnit* ins_unit) { Arg* resArg; char invode_deepth0_str[2] = {0}; char invode_deepth1_str[2] = {0}; - int ins_addr = 0; /* Found new script Line, clear the queues*/ if (instructUnit_getIsNewLine(ins_unit)) { args_setErrorCode(vs->locals->list, 0); args_setSysOut(vs->locals->list, (char*)""); __clearInvokeQueues(vs->locals); - goto nextLine; } - invode_deepth0_str[0] = instructUnit_getInvokeDeepth(ins_unit); + + invode_deepth0_str[0] = instructUnit_getInvokeDeepth(ins_unit) + '0'; invode_deepth1_str[0] = invode_deepth0_str[0] + 1; uint16_t const_index = instructUnit_getConstPoolIndex(ins_unit); @@ -800,10 +799,10 @@ int pikaVM_runInstructUnit(PikaObj* self, VMState* vs, InstructUnit* ins_unit) { } goto nextLine; nextLine: - // /* exit */ - // if (-999 == vs->jmp) { - // return -99999; - // } + /* exit */ + if (-999 == vs->jmp) { + return -99999; + } // /* break or continue */ // if ((-998 == vs->jmp) || (-997 == vs->jmp)) { // int32_t loop_end_addr = __getAddrOffsetOfJUM(vs->pc); @@ -820,7 +819,7 @@ nextLine: // if (vs.jmp != 0) { // return lineAddr + __getAddrOffsetFromJmp(pikaAsm, vs.pc, vs.jmp); // } - return ins_addr; + return vs->pc_i + sizeof(InstructUnit); } VMParameters* pikaVM_runAsmWithPars(PikaObj* self, @@ -1001,6 +1000,11 @@ InstructUnit* instructArray_getNow(InstructArray* self) { (uintptr_t)(self->content_offset_now)); } +InstructUnit* instructArray_getByOffset(InstructArray* self, int32_t offset) { + return (InstructUnit*)(arg_getContent(self->arg_buff) + + (uintptr_t)(offset)); +} + InstructUnit* instructArray_getNext(InstructArray* self) { self->content_offset_now += sizeof(InstructUnit); return instructArray_getNow(self); @@ -1058,26 +1062,25 @@ VMParameters* pikaVM_runByteCodeWithPars(PikaObj* self, VMParameters* locals, VMParameters* globals, ByteCodeFrame* byteCode_frame) { - int ins_addr = 0; int size = byteCode_frame->instruct_array.size; args_setErrorCode(locals->list, 0); args_setSysOut(locals->list, (char*)""); - VMState vm_state_byteCode = { + VMState vs = { .const_pool = &byteCode_frame->const_pool, .locals = locals, .globals = globals, .jmp = 0, .q0 = NULL, .q1 = NULL, + .pc_i = 0, }; - while (ins_addr < size) { - if (ins_addr == -99999) { + while (vs.pc_i < size) { + if (vs.pc_i == -99999) { break; } - InstructUnit* this_instruct_unit = - instructArray_getNow(&(byteCode_frame->instruct_array)); - ins_addr = pikaVM_runInstructUnit(self, &vm_state_byteCode, - this_instruct_unit); + InstructUnit* this_ins_unit = instructArray_getByOffset( + &(byteCode_frame->instruct_array), vs.pc_i); + vs.pc_i = pikaVM_runInstructUnit(self, &vs, this_ins_unit); char* sysOut = args_getSysOut(locals->list); uint8_t errcode = args_getErrorCode(locals->list); if (!strEqu("", sysOut)) { @@ -1085,7 +1088,7 @@ VMParameters* pikaVM_runByteCodeWithPars(PikaObj* self, } if (0 != errcode) { __platform_printf("[info] input commond: \r\n"); - instructUnit_print(this_instruct_unit); + instructUnit_print(this_ins_unit); } } __clearInvokeQueues(locals); diff --git a/src/PikaVM.h b/src/PikaVM.h index 2039896d1..904a060b4 100644 --- a/src/PikaVM.h +++ b/src/PikaVM.h @@ -67,6 +67,7 @@ typedef struct VMState_t { Queue* q1; int32_t jmp; char* pc; + int32_t pc_i; char* ASM_start; ConstPool* const_pool; } VMState; @@ -154,5 +155,6 @@ void instructUnit_init(InstructUnit* ins_unit); void instructUnit_print(InstructUnit* self); void instructArray_print(InstructArray* self); void byteCodeFrame_print(ByteCodeFrame* self); +InstructUnit* instructArray_getByOffset(InstructArray* self, int32_t offset); #endif