Storing and executing compiled code in ROM
Posted: Mon Sep 12, 2016 9:15 pm
(After originally posting this, I found this post from almost two years ago, but I think that with mpy_cross and persistent bytecode, the situation has changed since then.)
I'm working on a port of MicroPython to a platform without a filesystem. I'm currently considering a process of uploading a .py file via serial, compiling it on the device with MicroPython, and then storing the compiled (frozen?) bytecode to a reserved area of memory-mapped flash for automatic execution on every boot.
My question is whether I can just copy a portion of the struct generated by mp_builtin_compile() into flash, and then execute it directly from flash using code_execute(). Maybe I would store the mp_raw_code_t (or the bytecode of it) in flash and create a stub mp_obj_code_t (or mp_raw_code_t structure) to point into it?
My goal is to reserve as much RAM as possible for execution by keeping bytecode in ROM, but I'm still diving through the various structures looking for the "right" place to do so. I see the code in emitglue.c related to saving off .mpy files (mp_raw_code_save()), but that doesn't seem like the correct path to follow given that .mpy files get loaded into RAM for execution.
Is there a platform already doing this that I should review (I'm currently looking at pyboard and esp8266 platforms)? Should I be trying to mimic the way MicroPython stores/loads frozen modules?
I'm working on a port of MicroPython to a platform without a filesystem. I'm currently considering a process of uploading a .py file via serial, compiling it on the device with MicroPython, and then storing the compiled (frozen?) bytecode to a reserved area of memory-mapped flash for automatic execution on every boot.
My question is whether I can just copy a portion of the struct generated by mp_builtin_compile() into flash, and then execute it directly from flash using code_execute(). Maybe I would store the mp_raw_code_t (or the bytecode of it) in flash and create a stub mp_obj_code_t (or mp_raw_code_t structure) to point into it?
My goal is to reserve as much RAM as possible for execution by keeping bytecode in ROM, but I'm still diving through the various structures looking for the "right" place to do so. I see the code in emitglue.c related to saving off .mpy files (mp_raw_code_save()), but that doesn't seem like the correct path to follow given that .mpy files get loaded into RAM for execution.
Is there a platform already doing this that I should review (I'm currently looking at pyboard and esp8266 platforms)? Should I be trying to mimic the way MicroPython stores/loads frozen modules?