bytecodeFrame print as array is ok

This commit is contained in:
lyon1998 2022-03-17 15:43:26 +08:00
parent 1d1ac244ec
commit 494f61a562
4 changed files with 144 additions and 5 deletions

View File

@ -814,3 +814,88 @@ TEST(VM, for_break_byte) {
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(VM, load_static_bytes) {
const uint8_t bytes[] = {
0x08, 0x02, /* instruct array size */
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0d, 0x00, 0x00, 0x83, 0x11, 0x00,
0x00, 0x04, 0x16, 0x00, 0x00, 0x85, 0x1b, 0x00, 0x00, 0x04, 0x1d, 0x00,
0x20, 0x85, 0x1b, 0x00, 0x20, 0x01, 0x0d, 0x00, 0x10, 0x02, 0x28, 0x00,
0x00, 0x02, 0x2e, 0x00, 0x00, 0x04, 0x33, 0x00, 0x00, 0x01, 0x37, 0x00,
0x00, 0x01, 0x3b, 0x00, 0x00, 0x01, 0x3f, 0x00, 0x00, 0x04, 0x43, 0x00,
0x00, 0x04, 0x4a, 0x00, 0x00, 0x04, 0x51, 0x00, 0x00, 0x82, 0x58, 0x00,
0x00, 0x04, 0x65, 0x00, 0x00, 0x0d, 0x65, 0x00, 0x00, 0x07, 0x67, 0x00,
0x11, 0x81, 0x0d, 0x00, 0x11, 0x05, 0x1b, 0x00, 0x01, 0x08, 0x69, 0x00,
0x01, 0x07, 0x6c, 0x00, 0x02, 0x8e, 0x6c, 0x00, 0x11, 0x81, 0x1d, 0x00,
0x11, 0x05, 0x1b, 0x00, 0x01, 0x08, 0x69, 0x00, 0x01, 0x07, 0x6c, 0x00,
0x12, 0x81, 0x6e, 0x00, 0x12, 0x01, 0x65, 0x00, 0x02, 0x02, 0x76, 0x00,
0x02, 0x04, 0x16, 0x00, 0x02, 0x85, 0x6c, 0x00, 0x02, 0x04, 0x1d, 0x00,
0x01, 0x8b, 0x6c, 0x00, 0x11, 0x01, 0x1d, 0x00, 0x11, 0x05, 0x6c, 0x00,
0x01, 0x08, 0x69, 0x00, 0x01, 0x07, 0x6c, 0x00, 0x12, 0x81, 0x16, 0x00,
0x12, 0x03, 0x7e, 0x00, 0x02, 0x08, 0x69, 0x00, 0x02, 0x07, 0x6c, 0x00,
0x13, 0x81, 0x6e, 0x00, 0x13, 0x01, 0x65, 0x00, 0x03, 0x02, 0x76, 0x00,
0x03, 0x04, 0x85, 0x00, 0x03, 0x82, 0x85, 0x00, 0x03, 0x85, 0x1b, 0x00,
0x03, 0x04, 0x1d, 0x00, 0x02, 0x8b, 0x6c, 0x00, 0x12, 0x01, 0x16, 0x00,
0x12, 0x03, 0x8a, 0x00, 0x02, 0x08, 0x69, 0x00, 0x02, 0x07, 0x6c, 0x00,
0x13, 0x81, 0x6e, 0x00, 0x13, 0x01, 0x65, 0x00, 0x03, 0x02, 0x76, 0x00,
0x03, 0x04, 0x8a, 0x00, 0x03, 0x85, 0x67, 0x00, 0x03, 0x04, 0x1d, 0x00,
0x02, 0x8b, 0x6c, 0x00, 0x12, 0x01, 0x16, 0x00, 0x12, 0x03, 0x8f, 0x00,
0x02, 0x08, 0x69, 0x00, 0x02, 0x07, 0x6c, 0x00, 0x13, 0x81, 0x6e, 0x00,
0x13, 0x01, 0x65, 0x00, 0x03, 0x02, 0x76, 0x00, 0x03, 0x04, 0x8f, 0x00,
0x03, 0x85, 0x67, 0x00, 0x03, 0x04, 0x1d, 0x00, 0x01, 0x8b, 0x6c, 0x00,
0x11, 0x01, 0x1d, 0x00, 0x11, 0x05, 0x67, 0x00, 0x01, 0x08, 0x69, 0x00,
0x01, 0x07, 0x6c, 0x00, 0x12, 0x81, 0x16, 0x00, 0x12, 0x03, 0x8a, 0x00,
0x02, 0x08, 0x69, 0x00, 0x02, 0x07, 0x6c, 0x00, 0x03, 0x82, 0x8a, 0x00,
0x03, 0x07, 0x6c, 0x00, 0x14, 0x81, 0x6e, 0x00, 0x14, 0x01, 0x65, 0x00,
0x04, 0x02, 0x76, 0x00, 0x04, 0x04, 0x85, 0x00, 0x04, 0x82, 0x85, 0x00,
0x03, 0x85, 0x1b, 0x00, 0x03, 0x04, 0x1d, 0x00, 0x02, 0x8b, 0x6c, 0x00,
0x12, 0x01, 0x16, 0x00, 0x12, 0x03, 0x8f, 0x00, 0x02, 0x08, 0x69, 0x00,
0x02, 0x07, 0x6c, 0x00, 0x13, 0x81, 0x6e, 0x00, 0x13, 0x01, 0x65, 0x00,
0x03, 0x02, 0x76, 0x00, 0x03, 0x04, 0x85, 0x00, 0x03, 0x85, 0x99, 0x00,
0x03, 0x04, 0x1d, 0x00, 0x01, 0x8b, 0x6c, 0x00, 0x11, 0x01, 0x1d, 0x00,
0x11, 0x05, 0x99, 0x00, 0x01, 0x08, 0x69, 0x00, 0x01, 0x07, 0x6c, 0x00,
0x12, 0x81, 0x16, 0x00, 0x12, 0x03, 0x8f, 0x00, 0x02, 0x08, 0x69, 0x00,
0x02, 0x07, 0x6c, 0x00, 0x13, 0x81, 0x9b, 0x00, 0x23, 0x01, 0x6e, 0x00,
0x23, 0x01, 0x65, 0x00, 0x13, 0x02, 0x76, 0x00, 0x03, 0x08, 0xa2, 0x00,
0x03, 0x07, 0x6c, 0x00, 0x04, 0x82, 0x85, 0x00, 0x14, 0x81, 0x6e, 0x00,
0x14, 0x01, 0x65, 0x00, 0x24, 0x01, 0x9b, 0x00, 0x24, 0x01, 0x8f, 0x00,
0x14, 0x08, 0xa4, 0x00, 0x14, 0x03, 0x6e, 0x00, 0x04, 0x02, 0xa6, 0x00,
0x03, 0x85, 0x1b, 0x00, 0x03, 0x04, 0x1d, 0x00, 0x00, 0x86, 0xae, 0x00,
0x00, 0x8c, 0x33, 0x00, /* instruct array */
0xb1, 0x00, /* const pool size */
0x5f, 0x5f, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x6c, 0x65, 0x6e, 0x00,
0x6c, 0x65, 0x6e, 0x00, 0x6e, 0x6f, 0x6e, 0x65, 0x00, 0x6d, 0x6f, 0x64,
0x65, 0x00, 0x30, 0x00, 0x69, 0x6e, 0x66, 0x6f, 0x5f, 0x69, 0x6e, 0x64,
0x65, 0x78, 0x00, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x69, 0x74, 0x65,
0x72, 0x00, 0x5f, 0x6c, 0x30, 0x00, 0x5f, 0x72, 0x31, 0x00, 0x5f, 0x72,
0x32, 0x00, 0x5f, 0x72, 0x33, 0x00, 0x5f, 0x6c, 0x30, 0x2e, 0x61, 0x31,
0x00, 0x5f, 0x6c, 0x30, 0x2e, 0x61, 0x32, 0x00, 0x5f, 0x6c, 0x30, 0x2e,
0x61, 0x33, 0x00, 0x5f, 0x6c, 0x30, 0x2e, 0x5f, 0x5f, 0x6e, 0x65, 0x78,
0x74, 0x5f, 0x5f, 0x00, 0x69, 0x00, 0x32, 0x00, 0x3d, 0x3d, 0x00, 0x31,
0x00, 0x5f, 0x5f, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x00, 0x5f, 0x5f, 0x67,
0x65, 0x74, 0x5f, 0x5f, 0x00, 0x61, 0x6c, 0x77, 0x61, 0x79, 0x73, 0x00,
0x74, 0x6f, 0x64, 0x6f, 0x00, 0x77, 0x68, 0x65, 0x6e, 0x00, 0x70, 0x65,
0x72, 0x69, 0x6f, 0x64, 0x5f, 0x6d, 0x73, 0x00, 0x33, 0x00, 0x5f, 0x5f,
0x74, 0x69, 0x63, 0x6b, 0x00, 0x3e, 0x00, 0x2b, 0x00, 0x5f, 0x5f, 0x73,
0x65, 0x74, 0x5f, 0x5f, 0x00, 0x2d, 0x31, 0x00, /* const pool */
};
ByteCodeFrame bytecode_frame = {.const_pool =
{
.arg_buff = NULL,
.content_offset_now = 0,
.size = 0,
.content_start = NULL,
},
.instruct_array = {
.arg_buff = NULL,
.content_offset_now = 0,
.size = 0,
.content_start = NULL,
}};
byteCodeFrame_loadBytes(&bytecode_frame, (uint8_t*)bytes);
EXPECT_EQ(instructArray_getSize(&(bytecode_frame.instruct_array)), 520);
EXPECT_EQ(bytecode_frame.const_pool.size, 177);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -2220,7 +2220,7 @@ TEST(compiler, task) {
byteCodeFrame_print(&bytecode_frame);
printf("Asm size: %d\r\n", strGetSize(pikaAsm));
constPool_printAsArray(&(bytecode_frame.const_pool));
byteCodeFrame_printAsArray(&bytecode_frame);
/* deinit */
byteCodeFrame_deinit(&bytecode_frame);
@ -2228,3 +2228,4 @@ TEST(compiler, task) {
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -926,7 +926,15 @@ void byteCodeFrame_init(ByteCodeFrame* self) {
instructArray_init(&(self->instruct_array));
}
void byteCodeFrame_fromFlash(ByteCodeFrame* self) {}
void byteCodeFrame_loadBytes(ByteCodeFrame* self, uint8_t* bytes) {
uint16_t* ins_size_p = (uint16_t*)bytes;
void* ins_start_p = (uint16_t*)(bytes + 2);
uint16_t* const_size_p = ins_start_p + *ins_size_p;
self->instruct_array.size = *ins_size_p;
self->instruct_array.content_start = ins_start_p;
self->const_pool.size = *const_size_p;
self->const_pool.content_start = const_size_p + 2;
}
void byteCodeFrame_deinit(ByteCodeFrame* self) {
constPool_deinit(&(self->const_pool));
@ -1040,6 +1048,35 @@ exit:
return;
}
void instructArray_printAsArray(InstructArray* self) {
uint16_t offset_befor = self->content_offset_now;
self->content_offset_now = 0;
uint8_t line_num = 12;
uint16_t g_i = 0;
uint8_t* ins_size_p = (uint8_t*)&self->size;
__platform_printf("0x%02x, ", *(ins_size_p));
__platform_printf("0x%02x, ", *(ins_size_p + (uintptr_t)1));
__platform_printf("/* instruct array size */\n");
while (1) {
InstructUnit* ins_unit = instructArray_getNow(self);
if (NULL == ins_unit) {
goto exit;
}
for (int i = 0; i < (int)instructUnit_getSize(ins_unit); i++) {
g_i++;
__platform_printf("0x%02x, ", *((uint8_t*)ins_unit + (uintptr_t)i));
if (g_i % line_num == 0) {
__platform_printf("\n");
}
}
instructArray_getNext(self);
}
exit:
__platform_printf("/* instruct array */\n");
self->content_offset_now = offset_befor;
return;
}
size_t byteCodeFrame_getSize(ByteCodeFrame* bf) {
return bf->const_pool.size + bf->instruct_array.size;
}
@ -1105,11 +1142,13 @@ InstructUnit* instructArray_getByOffset(InstructArray* self, int32_t offset) {
}
void constPool_printAsArray(ConstPool* self) {
__platform_printf("char constArray[] = {\n");
uint8_t* const_size_str = (uint8_t*)&(self->size);
__platform_printf("0x%02x, ", *(const_size_str + (uintptr_t)1));
__platform_printf("0x%02x, ", *(const_size_str));
__platform_printf("0x%02x, ", *(const_size_str + (uintptr_t)1));
__platform_printf("/* const pool size */\n");
uint16_t ptr_befor = self->content_offset_now;
uint8_t line_num = 12;
uint16_t g_i = 0;
/* set ptr_now to begin */
self->content_offset_now = 0;
while (1) {
@ -1120,12 +1159,23 @@ void constPool_printAsArray(ConstPool* self) {
/* todo start */
for (uint32_t i = 0; i < strGetSize(data_each) + 1; i++) {
__platform_printf("0x%02x, ", *(data_each + (uintptr_t)i));
g_i++;
if (g_i % line_num == 0) {
__platform_printf("\n");
}
}
/* todo end */
}
exit:
__platform_printf("};\n");
/* retore ptr_now */
__platform_printf("/* const pool */\n");
self->content_offset_now = ptr_befor;
return;
}
void byteCodeFrame_printAsArray(ByteCodeFrame* self) {
__platform_printf("const uint8_t bytes[] = {\n");
instructArray_printAsArray(&(self->instruct_array));
constPool_printAsArray(&(self->const_pool));
__platform_printf("};\n");
}

View File

@ -132,5 +132,8 @@ void instructArray_printWithConst(InstructArray* self, ConstPool* const_pool);
void constPool_update(ConstPool* self);
void instructArray_update(InstructArray* self);
void constPool_printAsArray(ConstPool* self);
void instructArray_printAsArray(InstructArray* self);
void byteCodeFrame_loadBytes(ByteCodeFrame* self, uint8_t* bytes);
void byteCodeFrame_printAsArray(ByteCodeFrame* self);
#endif