append bytecodeUnit when append byteCodeFrame tested ok

This commit is contained in:
lyon1998 2022-03-13 12:54:44 +08:00
parent 8d5ba40b14
commit e73a8d2950
6 changed files with 79 additions and 71 deletions

View File

@ -771,21 +771,21 @@ TEST(VM, nag_a) {
TEST(InstructUnit, base) {
__platform_printf((char*)"BEGIN\r\n");
InstructUnit bu;
instructUnit_init(&bu);
instructUnit_setBlockDeepth(&bu, 2);
instructUnit_setIsNewLine(&bu, 1);
instructUnit_setInvokeDeepth(&bu, 3);
instructUnit_setInstruct(&bu, (Instruct)4);
instructUnit_setConstPoolIndex(&bu, 12);
InstructUnit ins_unit;
instructUnit_init(&ins_unit);
instructUnit_setBlockDeepth(&ins_unit, 2);
instructUnit_setIsNewLine(&ins_unit, 1);
instructUnit_setInvokeDeepth(&ins_unit, 3);
instructUnit_setInstruct(&ins_unit, (Instruct)4);
instructUnit_setConstPoolIndex(&ins_unit, 12);
EXPECT_EQ(instructUnit_getBlockDeepth(&bu), 2);
EXPECT_EQ(instructUnit_getIsNewLine(&bu), 1);
EXPECT_EQ(instructUnit_getInvokeDeepth(&bu), 3);
EXPECT_EQ(instructUnit_getInstruct(&bu), 4);
EXPECT_EQ(instructUnit_getConstPoolIndex(&bu), 12);
EXPECT_EQ(instructUnit_getBlockDeepth(&ins_unit), 2);
EXPECT_EQ(instructUnit_getIsNewLine(&ins_unit), 1);
EXPECT_EQ(instructUnit_getInvokeDeepth(&ins_unit), 3);
EXPECT_EQ(instructUnit_getInstruct(&ins_unit), 4);
EXPECT_EQ(instructUnit_getConstPoolIndex(&ins_unit), 12);
instructUnit_print(&bu);
instructUnit_print(&ins_unit);
EXPECT_STREQ(log_buff[2], (char*)"BEGIN\r\n");
EXPECT_STREQ(log_buff[1], (char*)"B2\r\n");
EXPECT_STREQ(log_buff[0], (char*)"3 OUT #12\r\n");
@ -839,19 +839,19 @@ TEST(ConstPool, get) {
TEST(InstructArray, set) {
__platform_printf((char*)"BEGIN\r\n");
InstructArray ia;
InstructUnit bu;
instructUnit_init(&bu);
instructUnit_setBlockDeepth(&bu, 2);
instructUnit_setIsNewLine(&bu, 1);
instructUnit_setInvokeDeepth(&bu, 3);
instructUnit_setInstruct(&bu, (Instruct)4);
instructUnit_setConstPoolIndex(&bu, 12);
InstructUnit ins_unit;
instructUnit_init(&ins_unit);
instructUnit_setBlockDeepth(&ins_unit, 2);
instructUnit_setIsNewLine(&ins_unit, 1);
instructUnit_setInvokeDeepth(&ins_unit, 3);
instructUnit_setInstruct(&ins_unit, (Instruct)4);
instructUnit_setConstPoolIndex(&ins_unit, 12);
instructArray_init(&ia);
instructArray_append(&ia, &bu);
instructArray_print(&ia);
instructArray_deinit(&ia);
InstructArray ins_array;
instructArray_init(&ins_array);
instructArray_append(&ins_array, &ins_unit);
instructArray_print(&ins_array);
instructArray_deinit(&ins_array);
EXPECT_STREQ(log_buff[2], (char*)"BEGIN\r\n");
EXPECT_STREQ(log_buff[1], (char*)"B2\r\n");
EXPECT_STREQ(log_buff[0], (char*)"3 OUT #12\r\n");

View File

@ -2129,17 +2129,19 @@ TEST(asmer, asmer_to_instructUnit) {
"0 RUN test.on\n"
;
Args buffs = {0};
ByteCodeFrame bf;
ByteCodeFrame_init(&bf);
ByteCodeFrame_appendFromAsm(&bf, asm_line);
constPool_print(&(bf.const_pool));
EXPECT_STREQ(constPool_getNext(&(bf.const_pool)), (char*)"2");
EXPECT_STREQ(constPool_getNext(&(bf.const_pool)), (char*)"3");
EXPECT_STREQ(constPool_getNext(&(bf.const_pool)), (char*)"add");
EXPECT_STREQ(constPool_getNext(&(bf.const_pool)), (char*)"test.on");
EXPECT_EQ((uintptr_t)constPool_getNext(&(bf.const_pool)), (uintptr_t)NULL);
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, asm_line);
constPool_print(&(bytecode_frame.const_pool));
EXPECT_STREQ(constPool_getNext(&(bytecode_frame.const_pool)), (char*)"2");
EXPECT_STREQ(constPool_getNext(&(bytecode_frame.const_pool)), (char*)"3");
EXPECT_STREQ(constPool_getNext(&(bytecode_frame.const_pool)), (char*)"add");
EXPECT_STREQ(constPool_getNext(&(bytecode_frame.const_pool)),
(char*)"test.on");
EXPECT_EQ((uintptr_t)constPool_getNext(&(bytecode_frame.const_pool)),
(uintptr_t)NULL);
ByteCodeFrame_deinit(&bf);
byteCodeFrame_deinit(&bytecode_frame);
strsDeinit(&buffs);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -1519,7 +1519,7 @@ int32_t AST_deinit(AST* ast) {
return obj_deinit(ast);
}
ByteCodeFrame* ByteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm) {
ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) {
Asmer asmer = {
.asm_code = pikaAsm,
.block_deepth_now = 0,
@ -1541,25 +1541,28 @@ ByteCodeFrame* ByteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm) {
/* process each ins */
/* load Asm to byte code unit */
InstructUnit bu = {0};
instructUnit_setBlockDeepth(&bu, asmer.block_deepth_now);
instructUnit_setInvokeDeepth(&bu, line[0] - '0');
InstructUnit ins_unit = {0};
instructUnit_setBlockDeepth(&ins_unit, asmer.block_deepth_now);
instructUnit_setInvokeDeepth(&ins_unit, line[0] - '0');
instructUnit_setConstPoolIndex(
&bu, constPool_getLastOffset(&(bf->const_pool)));
instructUnit_setInstruct(&bu, pikaVM_getInstructFromAsm(line));
&ins_unit, constPool_getLastOffset(&(self->const_pool)));
instructUnit_setInstruct(&ins_unit, pikaVM_getInstructFromAsm(line));
if (asmer.is_new_line) {
instructUnit_setIsNewLine(&bu, 1);
instructUnit_setIsNewLine(&ins_unit, 1);
asmer.is_new_line = 0;
}
/* append instructUnit to instructArray */
instructArray_append(&(self->Instruct_array), &ins_unit);
/* load const to const pool buff */
char* data = line + 6;
constPool_append(&(bf->const_pool), data);
constPool_append(&(self->const_pool), data);
next_line:
/* point to next line */
asmer.line_pointer += strGetLineSize(asmer.line_pointer) + 1;
}
return bf;
return self;
}
char* Parser_byteCodeToAsm(Args* outBuffs, char* pikaByteCode) {

View File

@ -41,6 +41,6 @@ typedef struct Asmer_t {
char* Parser_multiLineToAsm(Args* outBuffs, char* multiLine);
char* instructUnit_fromAsmLine(Args* outBuffs, char* pikaAsm);
char* Parser_byteCodeToAsm(Args* outBuffs, char* pikaByteCode);
ByteCodeFrame* ByteCodeFrame_appendFromAsm(ByteCodeFrame *bf, char* pikaAsm);
ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame *bf, char* pikaAsm);
#endif

View File

@ -912,33 +912,36 @@ exit:
return;
}
void ByteCodeFrame_init(ByteCodeFrame* bf) {
constPool_init(&(bf->const_pool));
void byteCodeFrame_init(ByteCodeFrame* self) {
constPool_init(&(self->const_pool));
instructArray_init(&(self->Instruct_array));
}
void ByteCodeFrame_deinit(ByteCodeFrame* bf) {
constPool_deinit(&(bf->const_pool));
void byteCodeFrame_deinit(ByteCodeFrame* self) {
constPool_deinit(&(self->const_pool));
instructArray_deinit(&(self->Instruct_array));
}
void instructArray_init(InstructArray* ia) {
ia->arg_buff = arg_setNull(NULL);
ia->size = 0;
ia->content_offset_now = 0;
void instructArray_init(InstructArray* ins_array) {
ins_array->arg_buff = arg_setNull(NULL);
ins_array->size = 0;
ins_array->content_offset_now = 0;
}
void instructArray_deinit(InstructArray* ia) {
arg_deinit(ia->arg_buff);
void instructArray_deinit(InstructArray* ins_array) {
arg_deinit(ins_array->arg_buff);
}
void instructArray_append(InstructArray* ia, InstructUnit* iu) {
ia->arg_buff = arg_append(ia->arg_buff, iu, sizeof(InstructUnit));
ia->size += sizeof(InstructUnit);
void instructArray_append(InstructArray* ins_array, InstructUnit* ins_unit) {
ins_array->arg_buff =
arg_append(ins_array->arg_buff, ins_unit, sizeof(InstructUnit));
ins_array->size += sizeof(InstructUnit);
}
void instructUnit_init(InstructUnit* iu) {
iu->deepth = 0;
iu->const_pool_index = 0;
iu->isNewLine_instruct = 0;
void instructUnit_init(InstructUnit* ins_unit) {
ins_unit->deepth = 0;
ins_unit->const_pool_index = 0;
ins_unit->isNewLine_instruct = 0;
}
InstructUnit* instructArray_getNow(InstructArray* self) {
@ -982,11 +985,11 @@ void instructArray_print(InstructArray* self) {
uint16_t offset_befor = self->content_offset_now;
self->content_offset_now = 0;
while (1) {
InstructUnit* iu = instructArray_getNow(self);
if (NULL == iu) {
InstructUnit* ins_unit = instructArray_getNow(self);
if (NULL == ins_unit) {
goto exit;
}
instructUnit_print(iu);
instructUnit_print(ins_unit);
instructArray_getNext(self);
}
exit:

View File

@ -128,12 +128,12 @@ void constPool_print(ConstPool* self);
((char*)(arg_getContent((ConstPoll_p_self)->arg_buff) + \
(uintptr_t)(uint16_t_offset)))
void ByteCodeFrame_init(ByteCodeFrame* bf);
void ByteCodeFrame_deinit(ByteCodeFrame* bf);
void instructArray_init(InstructArray* ia);
void instructArray_deinit(InstructArray* ia);
void instructArray_append(InstructArray* ia, InstructUnit* iu);
void instructUnit_init(InstructUnit* iu);
void byteCodeFrame_init(ByteCodeFrame* bf);
void byteCodeFrame_deinit(ByteCodeFrame* bf);
void instructArray_init(InstructArray* ins_array);
void instructArray_deinit(InstructArray* ins_array);
void instructArray_append(InstructArray* ins_array, InstructUnit* ins_unit);
void instructUnit_init(InstructUnit* ins_unit);
void instructUnit_print(InstructUnit* self);
void instructArray_print(InstructArray* self);