use exist const, fix RET parse format

This commit is contained in:
lyon1998 2022-03-16 14:40:51 +08:00
parent a868117fbc
commit 12ed23ddd7
7 changed files with 91 additions and 37 deletions

View File

@ -18,6 +18,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/../build/src/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=parser.class_def",
// "--gtest_filter=VM.WHILE_byte",
// "--gtest_filter=pikaMain.task_run_once",
// "--gtest_filter=VM.if_elif*",

View File

@ -2105,6 +2105,12 @@ TEST(parser, class_def) {
"0 RET \n"
"B0\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
byteCodeFrame_print(&bytecode_frame);
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
@ -2133,9 +2139,17 @@ 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* ins_unit =
instructArray_getByOffset(&(bytecode_frame.instruct_array), 4);
instructUnit_print(ins_unit);
size_t byteCode_size = byteCodeFrame_getSize(&bytecode_frame);
uint16_t offset_out =
constPool_getOffsetByData(&(bytecode_frame.const_pool), (char*)"add");
char* data_out =
constPool_getByOffset(&(bytecode_frame.const_pool), offset_out);
/* assert */
EXPECT_STREQ(data_out, (char*)"add");
EXPECT_EQ(byteCode_size, 33);
EXPECT_STREQ(constPool_getNext(&(bytecode_frame.const_pool)), (char*)"2");
EXPECT_STREQ(constPool_getNext(&(bytecode_frame.const_pool)), (char*)"3");
@ -2144,7 +2158,7 @@ TEST(asmer, asmer_to_instructUnit) {
(char*)"test.on");
EXPECT_EQ((uintptr_t)constPool_getNext(&(bytecode_frame.const_pool)),
(uintptr_t)NULL);
/* deinit */
byteCodeFrame_deinit(&bytecode_frame);
strsDeinit(&buffs);
EXPECT_EQ(pikaMemNow(), 0);

View File

@ -1538,12 +1538,32 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) {
/* process each ins */
/* get constPool offset */
uint16_t const_pool_offset = 0;
char* data = line + 6;
uint16_t exist_offset =
constPool_getOffsetByData(&(self->const_pool), data);
/* get const offset */
if (strEqu(data, "")) {
/* not need const value */
const_pool_offset = 0;
} else if (65535 == exist_offset) {
/* push new const value */
const_pool_offset = constPool_getLastOffset(&(self->const_pool));
/* load const to const pool buff */
constPool_append(&(self->const_pool), data);
} else {
/* use exist const value */
const_pool_offset = exist_offset;
}
/* load Asm to byte code unit */
InstructUnit ins_unit = {0};
instructUnit_setBlockDeepth(&ins_unit, asmer.block_deepth_now);
instructUnit_setInvokeDeepth(&ins_unit, line[0] - '0');
instructUnit_setConstPoolIndex(
&ins_unit, constPool_getLastOffset(&(self->const_pool)));
instructUnit_setConstPoolIndex(&ins_unit, const_pool_offset);
instructUnit_setInstruct(&ins_unit, pikaVM_getInstructFromAsm(line));
if (asmer.is_new_line) {
instructUnit_setIsNewLine(&ins_unit, 1);
@ -1553,9 +1573,6 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) {
/* append instructUnit to instructArray */
instructArray_append(&(self->instruct_array), &ins_unit);
/* load const to const pool buff */
char* data = line + 6;
constPool_append(&(self->const_pool), data);
next_line:
/* point to next line */
asmer.line_pointer += strGetLineSize(asmer.line_pointer) + 1;

View File

@ -850,6 +850,27 @@ uint16_t constPool_getLastOffset(ConstPool* self) {
return self->size;
}
uint16_t constPool_getOffsetByData(ConstPool* self, char* data) {
uint16_t ptr_befor = self->content_offset_now;
/* set ptr_now to begin */
self->content_offset_now = 0;
uint16_t offset_out = 65535;
while (1) {
if (NULL == constPool_getNext(self)) {
goto exit;
}
uint16_t offset = self->content_offset_now;
if (strEqu(data, constPool_getNow(self))) {
offset_out = self->content_offset_now;
goto exit;
}
}
exit:
/* retore ptr_now */
self->content_offset_now = ptr_befor;
return offset_out;
}
char* constPool_getNext(ConstPool* self) {
self->content_offset_now += strGetSize(constPool_getNow(self)) + 1;
return constPool_getNow(self);

View File

@ -37,7 +37,6 @@ enum Instruct {
__INSTRCUTION_CNT,
};
typedef struct VMState_t {
VMParameters* locals;
VMParameters* globals;
@ -152,4 +151,6 @@ VMParameters* pikaVM_runByteCodeWithState(PikaObj* self,
ByteCodeFrame* bytecode_frame,
uint16_t pc);
uint16_t constPool_getOffsetByData(ConstPool* self, char* data);
#endif