Explorar el Código

test ins_unit_print ok

lyon1998 hace 4 años
padre
commit
87c30b2cb3

+ 1 - 1
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=ByteCodeUnit*",
+                // "--gtest_filter=instructUnit*",
                 // "--gtest_filter=stack.str",
                 // "--gtest_filter=VM*",
                 // "--gtest_filter=arg*",

+ 42 - 26
port/linux/test/VM-test.cpp

@@ -769,33 +769,32 @@ TEST(VM, nag_a) {
     EXPECT_EQ(pikaMemNow(), 0);
 }
 
-TEST(ByteCodeUnit, base) {
-    ByteCodeUnit bu = {
-        .deepth = 0,
-        .isNewLine_instruct = 0,
-        .const_pool_index = 0,
-    };
-    byteCodeUnit_setBlockDeepth(&bu, 2);
-    byteCodeUnit_setIsNewLine(&bu, 1);
-    byteCodeUnit_setInvokeDeepth(&bu, 3);
-    byteCodeUnit_setInstruct(&bu, (Instruct)4);
-    byteCodeUnit_setConstPoolIndex(&bu, 12);
-
-    EXPECT_EQ(byteCodeUnit_getBlockDeepth(&bu), 2);
-    EXPECT_EQ(byteCodeUnit_getIsNewLine(&bu), 1);
-    EXPECT_EQ(byteCodeUnit_getInvokeDeepth(&bu), 3);
-    EXPECT_EQ(byteCodeUnit_getInstruct(&bu), 4);
-    EXPECT_EQ(byteCodeUnit_getConstPoolIndex(&bu), 12);
-}
-
-// TEST(ByteCodeUnit, new_) {
+TEST(InstructUnit, base) {
+    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);
+
+    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);
+
+    instructUnit_print(&bu);
+}
+
+// TEST(InstructUnit, new_) {
 //     char data[] = "test";
-//     ByteCodeUnit* bu_p = New_byteCodeUnit(strGetSize(data));
-//     byteCodeUnit_setData(bu_p, data);
-//     EXPECT_STREQ(byteCodeUnit_getData(bu_p), (char*)"test");
-//     EXPECT_EQ(byteCodeUnit_getTotleSize(bu_p), 8);
-//     EXPECT_EQ(byteCodeUnit_getTotleSize_withDataSize(strGetSize(data)), 8);
-//     byteCodeUnit_deinit(bu_p);
+//     InstructUnit* bu_p = New_instructUnit(strGetSize(data));
+//     instructUnit_setData(bu_p, data);
+//     EXPECT_STREQ(instructUnit_getData(bu_p), (char*)"test");
+//     EXPECT_EQ(instructUnit_getTotleSize(bu_p), 8);
+//     EXPECT_EQ(instructUnit_getTotleSize_withDataSize(strGetSize(data)), 8);
+//     instructUnit_deinit(bu_p);
 //     EXPECT_EQ(pikaMemNow(), 0);
 // }
 
@@ -832,3 +831,20 @@ TEST(ConstPool, get) {
     constPool_deinit(&cp);
     EXPECT_EQ(pikaMemNow(), 0);
 }
+
+TEST(InstructArray, set) {
+    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);
+
+    instructArray_init(&ia);
+    instructArray_append(&ia, &bu);
+    instructArray_deinit(&ia);
+
+    EXPECT_EQ(pikaMemNow(), 0);
+}

+ 1 - 1
port/linux/test/parse-test.cpp

@@ -2120,7 +2120,7 @@ TEST(parser, nag_a) {
     EXPECT_EQ(pikaMemNow(), 0);
 }
 
-TEST(asmer, asmer_to_byteCodeUnit) {
+TEST(asmer, asmer_to_instructUnit) {
     char* asm_line = (char*)
         "B2\n"
         "2 NUM 2\n"

+ 6 - 6
src/PikaParser.c

@@ -1541,14 +1541,14 @@ ByteCodeFrame* ByteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm) {
         /* process each ins */
 
         /* load Asm to byte code unit */
-        ByteCodeUnit bu = {0};
-        byteCodeUnit_setBlockDeepth(&bu, asmer.block_deepth_now);
-        byteCodeUnit_setInvokeDeepth(&bu, line[0] - '0');
-        byteCodeUnit_setConstPoolIndex(
+        InstructUnit bu = {0};
+        instructUnit_setBlockDeepth(&bu, asmer.block_deepth_now);
+        instructUnit_setInvokeDeepth(&bu, line[0] - '0');
+        instructUnit_setConstPoolIndex(
             &bu, constPool_getLastOffset(&(bf->const_pool)));
-        byteCodeUnit_setInstruct(&bu, pikaVM_getInstructFromAsm(line));
+        instructUnit_setInstruct(&bu, pikaVM_getInstructFromAsm(line));
         if (asmer.is_new_line) {
-            byteCodeUnit_setIsNewLine(&bu, 1);
+            instructUnit_setIsNewLine(&bu, 1);
             asmer.is_new_line = 0;
         }
 

+ 1 - 1
src/PikaParser.h

@@ -39,7 +39,7 @@ typedef struct Asmer_t {
 } Asmer;
 
 char* Parser_multiLineToAsm(Args* outBuffs, char* multiLine);
-char* ByteCodeUnit_fromAsmLine(Args* outBuffs, char* pikaAsm);
+char* instructUnit_fromAsmLine(Args* outBuffs, char* pikaAsm);
 char* Parser_byteCodeToAsm(Args* outBuffs, char* pikaByteCode);
 ByteCodeFrame* ByteCodeFrame_appendFromAsm(ByteCodeFrame *bf, char* pikaAsm);
 

+ 61 - 7
src/PikaVM.c

@@ -838,20 +838,20 @@ exit:
     return globals;
 }
 
-// ByteCodeUnit* New_byteCodeUnit(uint8_t data_size) {
-//     ByteCodeUnit* self =
-//         pikaMalloc(byteCodeUnit_getTotleSize_withDataSize(data_size));
+// InstructUnit* New_instructUnit(uint8_t data_size) {
+//     InstructUnit* self =
+//         pikaMalloc(instructUnit_getTotleSize_withDataSize(data_size));
 //     return self;
 // }
 
-// void byteCodeUnit_deinit(ByteCodeUnit* self) {
-//     pikaFree(self, (byteCodeUnit_getTotleSize(self)));
+// void instructUnit_deinit(InstructUnit* self) {
+//     pikaFree(self, (instructUnit_getTotleSize(self)));
 // }
 
 void constPool_init(ConstPool* self) {
     self->arg_buff = arg_setStr(NULL, "", "");
     self->content_offset_now = 0;
-    self->size = strGetSize(arg_getContent(self->arg_buff)) + 1;
+    self->size = strGetSize((char*)arg_getContent(self->arg_buff)) + 1;
 }
 
 void constPool_deinit(ConstPool* self) {
@@ -877,7 +877,6 @@ uint16_t constPool_getLastOffset(ConstPool* self) {
     return self->size;
 }
 
-
 char* constPool_getNext(ConstPool* self) {
     self->content_offset_now += strGetSize(constPool_getNow(self)) + 1;
     return constPool_getNow(self);
@@ -920,3 +919,58 @@ void ByteCodeFrame_init(ByteCodeFrame* bf) {
 void ByteCodeFrame_deinit(ByteCodeFrame* bf) {
     constPool_deinit(&(bf->const_pool));
 }
+
+void instructArray_init(InstructArray* ia) {
+    ia->arg_buff = arg_setNull(NULL);
+}
+
+void instructArray_deinit(InstructArray* ia) {
+    arg_deinit(ia->arg_buff);
+}
+
+void instructArray_append(InstructArray* ia, InstructUnit* iu) {
+    ia->arg_buff = arg_append(ia->arg_buff, iu, sizeof(InstructUnit));
+}
+
+void instructUnit_init(InstructUnit* iu) {
+    iu->deepth = 0;
+    iu->const_pool_index = 0;
+    iu->isNewLine_instruct = 0;
+}
+
+InstructUnit* instructArray_getNow(InstructArray* self) {
+    if (self->content_offset_now >= self->size) {
+        /* is the end */
+        return NULL;
+    }
+    return (InstructUnit*)(arg_getContent(self->arg_buff) +
+                           (uintptr_t)(self->content_offset_now));
+}
+
+InstructUnit* instructArray_getNext(InstructArray* self) {
+    self->content_offset_now += sizeof(InstructUnit);
+    return instructArray_getNow(self);
+}
+
+InstructUnit* instructArray_getLast(InstructArray* self) {
+    if (0 == self->content_offset_now) {
+        return NULL;
+    }
+    self->content_offset_now -= sizeof(InstructUnit);
+    return instructArray_getNow(self);
+}
+
+char* instructUnit_getInstructStr(InstructUnit* self) {
+#define __INS_GET_INS_STR
+#include "__instruction_table.cfg"
+    return "NON";
+}
+
+void instructUnit_print(InstructUnit* self) {
+    if (instructUnit_getIsNewLine(self)) {
+        __platform_printf("B%d\n", instructUnit_getBlockDeepth(self));
+    }
+    __platform_printf("%d %s #%d\n ", instructUnit_getInvokeDeepth(self),
+                      instructUnit_getInstructStr(self),
+                      self->const_pool_index);
+}

+ 30 - 23
src/PikaVM.h

@@ -35,11 +35,11 @@ enum Instruct {
     __INSTRCUTION_CNT,
 };
 
-typedef struct InsturctUnit_t {
+typedef struct InstructUnit_t {
     uint8_t deepth;
     uint8_t isNewLine_instruct;
     uint16_t const_pool_index;
-} ByteCodeUnit;
+} InstructUnit;
 
 typedef struct ConstPool_t {
     Arg* arg_buff;
@@ -61,55 +61,56 @@ typedef struct ByteCodeFrame_t {
 VMParameters* pikaVM_run(PikaObj* self, char* pyLine);
 VMParameters* pikaVM_runAsm(PikaObj* self, char* pikaAsm);
 
-#define byteCodeUnit_getBlockDeepth(self) (((self)->deepth) & 0x0F)
-#define byteCodeUnit_getInvokeDeepth(self) (((self)->deepth) >> 4)
-// #define byteCodeUnit_getDataSize(self) (strGetSize((char*)(self)->data))
-// #define byteCodeUnit_getData(self) (char*)((self)->data)
-#define byteCodeUnit_getInstruct(self) ((self)->isNewLine_instruct & 0x7F)
-#define byteCodeUnit_getConstPoolIndex(self) ((self)->const_pool_index)
-#define byteCodeUnit_getIsNewLine(self) ((self)->isNewLine_instruct >> 7)
+#define instructUnit_getBlockDeepth(self) (((self)->deepth) & 0x0F)
+#define instructUnit_getInvokeDeepth(self) (((self)->deepth) >> 4)
+// #define instructUnit_getDataSize(self) (strGetSize((char*)(self)->data))
+// #define instructUnit_getData(self) (char*)((self)->data)
+#define instructUnit_getInstruct(self) \
+    ((enum Instruct)((self)->isNewLine_instruct & 0x7F))
+#define instructUnit_getConstPoolIndex(self) ((self)->const_pool_index)
+#define instructUnit_getIsNewLine(self) ((self)->isNewLine_instruct >> 7)
 
-#define byteCodeUnit_setBlockDeepth(self, val) \
+#define instructUnit_setBlockDeepth(self, val) \
     do {                                       \
         ((self)->deepth) |= (0x0F & val);      \
     } while (0)
 
-#define byteCodeUnit_setConstPoolIndex(self, val) \
+#define instructUnit_setConstPoolIndex(self, val) \
     do {                                          \
         ((self)->const_pool_index = val);         \
     } while (0)
 
-#define byteCodeUnit_setInvokeDeepth(self, val)  \
+#define instructUnit_setInvokeDeepth(self, val)  \
     do {                                         \
         ((self)->deepth) |= ((0x0F & val) << 4); \
     } while (0)
 
 /*
- #define byteCodeUnit_setData(self, val)                            \
+ #define instructUnit_setData(self, val)                            \
      do {                                                           \
          __platform_memcpy((self)->data, val, strGetSize(val) + 1); \
      } while (0)
 */
 
-#define byteCodeUnit_setInstruct(self, val)           \
+#define instructUnit_setInstruct(self, val)           \
     do {                                              \
         ((self)->isNewLine_instruct) |= (0x7F & val); \
     } while (0)
 
-#define byteCodeUnit_setIsNewLine(self, val)                 \
+#define instructUnit_setIsNewLine(self, val)                 \
     do {                                                     \
         ((self)->isNewLine_instruct) |= ((0x01 & val) << 7); \
     } while (0)
 
-ByteCodeUnit* New_byteCodeUnit(uint8_t data_size);
-void byteCodeUnit_deinit(ByteCodeUnit* self);
+InstructUnit* New_instructUnit(uint8_t data_size);
+void instructUnit_deinit(InstructUnit* self);
 
 /*
  #define __get_alined_size(size) (((((size)-1) / 4) + 1) * 4)
- #define byteCodeUnit_getTotleSize_withDataSize(data_size) \
-  (__get_alined_size(sizeof(ByteCodeUnit) + data_size + 1))
- #define byteCodeUnit_getTotleSize(self) \
-  (byteCodeUnit_getTotleSize_withDataSize(byteCodeUnit_getDataSize(self)))
+ #define instructUnit_getTotleSize_withDataSize(data_size) \
+  (__get_alined_size(sizeof(InstructUnit) + data_size + 1))
+ #define instructUnit_getTotleSize(self) \
+  (instructUnit_getTotleSize_withDataSize(instructUnit_getDataSize(self)))
 */
 
 enum Instruct pikaVM_getInstructFromAsm(char* line);
@@ -123,10 +124,16 @@ char* constPool_getByIndex(ConstPool* self, uint16_t index);
 uint16_t constPool_getLastOffset(ConstPool* self);
 void constPool_print(ConstPool* self);
 
-#define constPool_getByOffset(self, offset) \
-    ((char*)(arg_getContent((self)->arg_buff) + (uintptr_t)(offset)))
+#define constPool_getByOffset(ConstPoll_p_self, uint16_t_offset) \
+    ((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 instructUnit_print(InstructUnit* self);
 
 #endif

+ 11 - 4
src/__instruction_def.h

@@ -36,10 +36,17 @@
 #endif
 
 #if defined(__INS_COMPIRE)
-#define def_ins(__INS_NAME) \
-    if (0 == strncmp(line + 2, ""#__INS_NAME"", 3)) { \
-        return __INS_NAME; \
-    } 
+#define def_ins(__INS_NAME)                             \
+    if (0 == strncmp(line + 2, "" #__INS_NAME "", 3)) { \
+        return __INS_NAME;                              \
+    }
+#endif
+
+#if defined(__INS_GET_INS_STR)
+#define def_ins(__INS_NAME)                              \
+    if (__INS_NAME ==  instructUnit_getInstruct(self)){ \
+        return ""#__INS_NAME""; \
+    }
 #endif
 
 #undef __INS_ENUM