3.6. Run the bytecode directly

3.6.1. Parse bytecode on PC

The runtime architecture of PikaScript is shown in the figure below. By default, the process of parsing Python scripts into Pika bytecodes is executed on the MCU, which enables the MCU to directly run Python scripts, including support for interactive running.

In the case of limited resources, the process of parsing the Python script into bytecode can be completed in advance on the PC, so that the Python script can not be parsed in the MCU, and the Pika bytecode can be directly executed, so that the Python script can be fully parsed. The code of the script can be cropped out.

_images/1639281281608-011ffd89-5851-47d8-9dca-438ed963f5d4-164649975346225.png

Using the pikaVM_runAsm API, you can directly run a multi-line bytecode string. You can refer to the usage of starting from bytecode in g030.

https://github.com/pikastech/pikascript/blob/master/bsp/stm32g030c8/Booter/main.c

Avoid using obj_run to execute python scripts, and instead use pikaVM_runAsm to run bytecode directly, and the compiler will automatically optimize the code parsed by Python to reduce code size.

Tools for parsing Python to bytecode on a PC: https://github.com/pikastech/pikascript/tree/master/tools/pikaByteCodeGen