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

@ -724,7 +724,7 @@ TEST(parser, def_add) {
"1 REF a\n"
"1 REF b\n"
"0 OPT +\n"
"0 RET\n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);
@ -750,8 +750,8 @@ TEST(parser, def_add_return) {
"1 REF a\n"
"1 REF b\n"
"0 OPT +\n"
"0 RET\n"
"0 RET\n"
"0 RET \n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);
@ -781,10 +781,10 @@ TEST(parser, def_while_return) {
"1 REF a\n"
"1 REF b\n"
"0 OPT +\n"
"0 RET\n"
"0 RET \n"
"B1\n"
"0 JMP -1\n"
"0 RET\n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);
@ -811,10 +811,10 @@ TEST(parser, def_while_return_void) {
"0 REF True\n"
"0 JEZ 2\n"
"B2\n"
"0 RET\n"
"0 RET \n"
"B1\n"
"0 JMP -1\n"
"0 RET\n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);

View File

@ -274,9 +274,9 @@ TEST(VM, RET_instruct) {
char* pikaAsm = (char*)
"B1\n"
"0 NUM 13\n"
"0 RET\n"
"0 RET \n"
"0 NUM 2\n"
"0 RET\n"
"0 RET \n"
"B0\n";
PikaObj* self = New_TinyObj(NULL);
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
@ -295,7 +295,7 @@ TEST(VM, RUN_DEF) {
"0 JMP 1\n"
"B1\n"
"0 NUM 1\n"
"0 RET\n"
"0 RET \n"
"B0\n"
"0 RUN test\n"
"0 OUT a\n";
@ -318,7 +318,7 @@ TEST(VM, RUN_global) {
"0 JMP 1\n"
"B1\n"
"0 REF a\n"
"0 RET\n"
"0 RET \n"
"B0\n"
"0 RUN test\n"
"0 OUT b\n";
@ -347,7 +347,7 @@ TEST(VM, RUN_local_b) {
"1 REF b\n"
"1 REF a\n"
"0 OPT +\n"
"0 RET\n"
"0 RET \n"
"B0\n"
"0 RUN test\n"
"0 OUT c\n";
@ -374,7 +374,7 @@ TEST(VM, RUN_DEF_add) {
"1 REF b\n"
"1 REF a\n"
"0 OPT +\n"
"0 RET\n"
"0 RET \n"
"B0\n"
"1 NUM 1\n"
"1 NUM 2\n"
@ -597,9 +597,9 @@ TEST(VM, RUN_DEF_NEW) {
"0 OUT newObj.x\n"
"B1\n"
"0 NEW newObj\n"
"0 RET\n"
"0 RET \n"
"B1\n"
"0 RET\n"
"0 RET \n"
"B0\n"
"0 RUN testnew\n"
"0 OUT outobj\n"

View File

@ -755,7 +755,7 @@ TEST(parser, def_add) {
"1 REF b\n"
"0 OPT +\n"
"B1\n"
"0 RET\n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);
@ -781,9 +781,9 @@ TEST(parser, def_add_return) {
"1 REF a\n"
"1 REF b\n"
"0 OPT +\n"
"0 RET\n"
"0 RET \n"
"B1\n"
"0 RET\n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);
@ -813,11 +813,11 @@ TEST(parser, def_while_return) {
"1 REF a\n"
"1 REF b\n"
"0 OPT +\n"
"0 RET\n"
"0 RET \n"
"B1\n"
"0 JMP -1\n"
"B1\n"
"0 RET\n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);
@ -844,11 +844,11 @@ TEST(parser, def_while_return_void) {
"0 REF True\n"
"0 JEZ 2\n"
"B2\n"
"0 RET\n"
"0 RET \n"
"B1\n"
"0 JMP -1\n"
"B1\n"
"0 RET\n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);
@ -1998,7 +1998,7 @@ TEST(parser, global) {
"B1\n"
"0 GLB y,z\n"
"B1\n"
"0 RET\n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);
@ -2057,7 +2057,7 @@ TEST(parser, class_) {
"0 RAS $origin\n"
"B1\n"
"0 NEW self\n"
"0 RET\n"
"0 RET \n"
"B0\n"
);
args_deinit(buffs);
@ -2097,14 +2097,20 @@ TEST(parser, class_def) {
"1 STR hello\n"
"0 RUN print\n"
"B2\n"
"0 RET\n"
"0 RET \n"
"B1\n"
"0 RAS $origin\n"
"B1\n"
"0 NEW self\n"
"0 RET\n"
"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

@ -1322,7 +1322,7 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) {
if (strEqu(blockType, "def")) {
pikaAsm = ASM_addBlockDeepth(ast, outBuffs, pikaAsm,
blockTypeNum + 1);
pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 RET\n");
pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 RET \n");
}
/* return when exit class */
if (strEqu(blockType, "class")) {
@ -1332,7 +1332,7 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) {
strsAppend(outBuffs, pikaAsm, (char*)"0 RAS $origin\n");
pikaAsm = ASM_addBlockDeepth(ast, outBuffs, pikaAsm, 1);
pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 NEW self\n");
pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 RET\n");
pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 RET \n");
}
}
}
@ -1474,7 +1474,7 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) {
if (obj_isArgExist(ast, "return")) {
/* parse stmt ast */
pikaAsm = AST_appandPikaASM(ast, ast, &buffs, pikaAsm);
pikaAsm = strsAppend(&buffs, pikaAsm, "0 RET\n");
pikaAsm = strsAppend(&buffs, pikaAsm, "0 RET \n");
is_block_matched = 1;
goto exit;
}
@ -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