Explorar el Código

use bytecode in stdtask for all

lyon1998 hace 4 años
padre
commit
ac511d6f34

+ 68 - 4
package/PikaStdLib/PikaStdTask_Task.c

@@ -3,26 +3,63 @@
 
 extern PikaObj* __pikaMain;
 void PikaStdTask_Task___init__(PikaObj* self) {
+    /*
     obj_run(self,
             "calls.__init__()\n"
             "is_period = 0\n");
+    */
+    const uint8_t bytes[] = {
+        0x0c, 0x00, /* instruct array size */
+        0x00, 0x82, 0x01, 0x00, 0x00, 0x85, 0x10, 0x00, 0x00, 0x04, 0x12, 0x00,
+        /* instruct array */
+        0x1c, 0x00, /* const pool size */
+        0x00, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x5f, 0x5f, 0x69, 0x6e, 0x69,
+        0x74, 0x5f, 0x5f, 0x00, 0x30, 0x00, 0x69, 0x73, 0x5f, 0x70, 0x65, 0x72,
+        0x69, 0x6f, 0x64, 0x00, /* const pool */
+    };
+    pikaVM_runByteCode(self, (uint8_t*)bytes);
     obj_setPtr(__pikaMain, "__calls", obj_getPtr(self, "calls"));
 }
 
 void PikaStdTask_Task_call_always(PikaObj* self, Arg* fun_todo) {
     obj_setArg(self, "fun_todo", fun_todo);
-    obj_run(self,
-            "calls.append('always')\n"
-            "calls.append(fun_todo)\n");
+    // obj_run(self,
+    //         "calls.append('always')\n"
+    //         "calls.append(fun_todo)\n");
+    const uint8_t bytes[] = {
+        0x10, 0x00, /* instruct array size */
+        0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x08, 0x00, 0x10, 0x81, 0x15,
+        0x00, 0x00, 0x02, 0x08, 0x00, /* instruct array */
+        0x1e, 0x00,                   /* const pool size */
+        0x00, 0x61, 0x6c, 0x77, 0x61, 0x79, 0x73, 0x00, 0x63, 0x61, 0x6c,
+        0x6c, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, 0x66,
+        0x75, 0x6e, 0x5f, 0x74, 0x6f, 0x64, 0x6f, 0x00, /* const pool */
+    };
+    pikaVM_runByteCode(self, (uint8_t*)bytes);
 }
 
 void PikaStdTask_Task_call_when(PikaObj* self, Arg* fun_todo, Arg* fun_when) {
     obj_setArg(self, "fun_todo", fun_todo);
     obj_setArg(self, "fun_when", fun_when);
+    /*
     obj_run(self,
             "calls.append('when')\n"
             "calls.append(fun_when)\n"
             "calls.append(fun_todo)\n");
+            */
+    const uint8_t bytes[] = {
+        0x18, 0x00, /* instruct array size */
+        0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x06, 0x00, 0x10, 0x81, 0x13, 0x00,
+        0x00, 0x02, 0x06, 0x00, 0x10, 0x81, 0x1c, 0x00, 0x00, 0x02, 0x06, 0x00,
+        /* instruct array */
+        0x25, 0x00, /* const pool size */
+        0x00, 0x77, 0x68, 0x65, 0x6e, 0x00, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e,
+        0x61, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, 0x66, 0x75, 0x6e, 0x5f, 0x77,
+        0x68, 0x65, 0x6e, 0x00, 0x66, 0x75, 0x6e, 0x5f, 0x74, 0x6f, 0x64, 0x6f,
+        0x00,
+        /* const pool */
+    };
+    pikaVM_runByteCode(self, (uint8_t*)bytes);
 }
 
 void PikaStdTask_Task_call_period_ms(PikaObj* self,
@@ -30,12 +67,29 @@ void PikaStdTask_Task_call_period_ms(PikaObj* self,
                                      int period_ms) {
     obj_setArg(self, "fun_todo", fun_todo);
     obj_setInt(self, "period_ms", period_ms);
+    /*
     obj_run(self,
             "calls.append('period_ms')\n"
             "calls.append(period_ms)\n"
             "calls.append(fun_todo)\n"
             "calls.append(0)\n"
             "is_period = 1\n");
+    */
+    const uint8_t bytes[] =
+        {
+            0x28, 0x00, /* instruct array size */
+            0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x0b, 0x00, 0x10, 0x81, 0x01,
+            0x00, 0x00, 0x02, 0x0b, 0x00, 0x10, 0x81, 0x18, 0x00, 0x00, 0x02,
+            0x0b, 0x00, 0x10, 0x85, 0x21, 0x00, 0x00, 0x02, 0x0b, 0x00, 0x00,
+            0x85, 0x23, 0x00, 0x00, 0x04, 0x25, 0x00, /* instruct array */
+            0x2f, 0x00,                               /* const pool size */
+            0x00, 0x70, 0x65, 0x72, 0x69, 0x6f, 0x64, 0x5f, 0x6d, 0x73, 0x00,
+            0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x65, 0x6e,
+            0x64, 0x00, 0x66, 0x75, 0x6e, 0x5f, 0x74, 0x6f, 0x64, 0x6f, 0x00,
+            0x30, 0x00, 0x31, 0x00, 0x69, 0x73, 0x5f, 0x70, 0x65, 0x72, 0x69,
+            0x6f, 0x64, 0x00, /* const pool */
+        };
+    pikaVM_runByteCode(self, (uint8_t*)bytes);
 }
 
 void PikaStdTask_Task_run_once(PikaObj* self) {
@@ -148,7 +202,17 @@ void PikaStdTask_Task_run_once(PikaObj* self) {
 
 void __Task_update_tick(PikaObj* self) {
     if (obj_getInt(self, "is_perod")) {
-        pikaVM_runAsm(self, "B0\n0 RUN platformGetTick\n");
+        /*
+        pikaVM_run(self, "platformGetTick()");
+        */
+        const uint8_t bytes[] = {
+            0x04, 0x00,             /* instruct array size */
+            0x00, 0x82, 0x01, 0x00, /* instruct array */
+            0x11, 0x00,             /* const pool size */
+            0x00, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d,
+            0x47, 0x65, 0x74, 0x54, 0x69, 0x63, 0x6b, 0x00, /* const pool */
+        };
+        pikaVM_runByteCode(self, (uint8_t*)bytes);
     }
 }
 

+ 68 - 4
port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdTask_Task.c

@@ -3,26 +3,63 @@
 
 extern PikaObj* __pikaMain;
 void PikaStdTask_Task___init__(PikaObj* self) {
+    /*
     obj_run(self,
             "calls.__init__()\n"
             "is_period = 0\n");
+    */
+    const uint8_t bytes[] = {
+        0x0c, 0x00, /* instruct array size */
+        0x00, 0x82, 0x01, 0x00, 0x00, 0x85, 0x10, 0x00, 0x00, 0x04, 0x12, 0x00,
+        /* instruct array */
+        0x1c, 0x00, /* const pool size */
+        0x00, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x5f, 0x5f, 0x69, 0x6e, 0x69,
+        0x74, 0x5f, 0x5f, 0x00, 0x30, 0x00, 0x69, 0x73, 0x5f, 0x70, 0x65, 0x72,
+        0x69, 0x6f, 0x64, 0x00, /* const pool */
+    };
+    pikaVM_runByteCode(self, (uint8_t*)bytes);
     obj_setPtr(__pikaMain, "__calls", obj_getPtr(self, "calls"));
 }
 
 void PikaStdTask_Task_call_always(PikaObj* self, Arg* fun_todo) {
     obj_setArg(self, "fun_todo", fun_todo);
-    obj_run(self,
-            "calls.append('always')\n"
-            "calls.append(fun_todo)\n");
+    // obj_run(self,
+    //         "calls.append('always')\n"
+    //         "calls.append(fun_todo)\n");
+    const uint8_t bytes[] = {
+        0x10, 0x00, /* instruct array size */
+        0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x08, 0x00, 0x10, 0x81, 0x15,
+        0x00, 0x00, 0x02, 0x08, 0x00, /* instruct array */
+        0x1e, 0x00,                   /* const pool size */
+        0x00, 0x61, 0x6c, 0x77, 0x61, 0x79, 0x73, 0x00, 0x63, 0x61, 0x6c,
+        0x6c, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, 0x66,
+        0x75, 0x6e, 0x5f, 0x74, 0x6f, 0x64, 0x6f, 0x00, /* const pool */
+    };
+    pikaVM_runByteCode(self, (uint8_t*)bytes);
 }
 
 void PikaStdTask_Task_call_when(PikaObj* self, Arg* fun_todo, Arg* fun_when) {
     obj_setArg(self, "fun_todo", fun_todo);
     obj_setArg(self, "fun_when", fun_when);
+    /*
     obj_run(self,
             "calls.append('when')\n"
             "calls.append(fun_when)\n"
             "calls.append(fun_todo)\n");
+            */
+    const uint8_t bytes[] = {
+        0x18, 0x00, /* instruct array size */
+        0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x06, 0x00, 0x10, 0x81, 0x13, 0x00,
+        0x00, 0x02, 0x06, 0x00, 0x10, 0x81, 0x1c, 0x00, 0x00, 0x02, 0x06, 0x00,
+        /* instruct array */
+        0x25, 0x00, /* const pool size */
+        0x00, 0x77, 0x68, 0x65, 0x6e, 0x00, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e,
+        0x61, 0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, 0x66, 0x75, 0x6e, 0x5f, 0x77,
+        0x68, 0x65, 0x6e, 0x00, 0x66, 0x75, 0x6e, 0x5f, 0x74, 0x6f, 0x64, 0x6f,
+        0x00,
+        /* const pool */
+    };
+    pikaVM_runByteCode(self, (uint8_t*)bytes);
 }
 
 void PikaStdTask_Task_call_period_ms(PikaObj* self,
@@ -30,12 +67,29 @@ void PikaStdTask_Task_call_period_ms(PikaObj* self,
                                      int period_ms) {
     obj_setArg(self, "fun_todo", fun_todo);
     obj_setInt(self, "period_ms", period_ms);
+    /*
     obj_run(self,
             "calls.append('period_ms')\n"
             "calls.append(period_ms)\n"
             "calls.append(fun_todo)\n"
             "calls.append(0)\n"
             "is_period = 1\n");
+    */
+    const uint8_t bytes[] =
+        {
+            0x28, 0x00, /* instruct array size */
+            0x10, 0x83, 0x01, 0x00, 0x00, 0x02, 0x0b, 0x00, 0x10, 0x81, 0x01,
+            0x00, 0x00, 0x02, 0x0b, 0x00, 0x10, 0x81, 0x18, 0x00, 0x00, 0x02,
+            0x0b, 0x00, 0x10, 0x85, 0x21, 0x00, 0x00, 0x02, 0x0b, 0x00, 0x00,
+            0x85, 0x23, 0x00, 0x00, 0x04, 0x25, 0x00, /* instruct array */
+            0x2f, 0x00,                               /* const pool size */
+            0x00, 0x70, 0x65, 0x72, 0x69, 0x6f, 0x64, 0x5f, 0x6d, 0x73, 0x00,
+            0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x61, 0x70, 0x70, 0x65, 0x6e,
+            0x64, 0x00, 0x66, 0x75, 0x6e, 0x5f, 0x74, 0x6f, 0x64, 0x6f, 0x00,
+            0x30, 0x00, 0x31, 0x00, 0x69, 0x73, 0x5f, 0x70, 0x65, 0x72, 0x69,
+            0x6f, 0x64, 0x00, /* const pool */
+        };
+    pikaVM_runByteCode(self, (uint8_t*)bytes);
 }
 
 void PikaStdTask_Task_run_once(PikaObj* self) {
@@ -148,7 +202,17 @@ void PikaStdTask_Task_run_once(PikaObj* self) {
 
 void __Task_update_tick(PikaObj* self) {
     if (obj_getInt(self, "is_perod")) {
-        pikaVM_runAsm(self, "B0\n0 RUN platformGetTick\n");
+        /*
+        pikaVM_run(self, "platformGetTick()");
+        */
+        const uint8_t bytes[] = {
+            0x04, 0x00,             /* instruct array size */
+            0x00, 0x82, 0x01, 0x00, /* instruct array */
+            0x11, 0x00,             /* const pool size */
+            0x00, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d,
+            0x47, 0x65, 0x74, 0x54, 0x69, 0x63, 0x6b, 0x00, /* const pool */
+        };
+        pikaVM_runByteCode(self, (uint8_t*)bytes);
     }
 }
 

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

@@ -2228,4 +2228,9 @@ TEST(compiler, task) {
     EXPECT_EQ(pikaMemNow(), 0);
 }
 
-
+TEST(compiler, demo1) {
+    char* lines = (char*)
+        "platformGetTick()";
+    Parser_compilePyToBytecodeArray(lines);
+    EXPECT_EQ(pikaMemNow(), 0);
+}

+ 11 - 0
src/PikaParser.c

@@ -1583,3 +1583,14 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) {
 char* Parser_byteCodeToAsm(Args* outBuffs, char* pikaByteCode) {
     return NULL;
 }
+
+void Parser_compilePyToBytecodeArray(char* lines) {
+    ByteCodeFrame bytecode_frame;
+    byteCodeFrame_init(&bytecode_frame);
+    bytecodeFrame_fromMultiLine(&bytecode_frame, lines);
+    /* do something */
+    byteCodeFrame_print(&bytecode_frame);
+    byteCodeFrame_printAsArray(&bytecode_frame);
+    /* deinit */
+    byteCodeFrame_deinit(&bytecode_frame);
+}

+ 1 - 0
src/PikaParser.h

@@ -44,5 +44,6 @@ char* Parser_byteCodeToAsm(Args* outBuffs, char* pikaByteCode);
 ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm);
 int bytecodeFrame_fromMultiLine(ByteCodeFrame* bytecode_frame,
                                             char* multi_line);
+void Parser_compilePyToBytecodeArray(char* lines);
 
 #endif

+ 1 - 0
src/PikaVM.c

@@ -1204,4 +1204,5 @@ void byteCodeFrame_printAsArray(ByteCodeFrame* self) {
     instructArray_printAsArray(&(self->instruct_array));
     constPool_printAsArray(&(self->const_pool));
     __platform_printf("};\n");
+    __platform_printf("pikaVM_runByteCode(self, (uint8_t*)bytes);\n");
 }