(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?
Storing and executing compiled code in ROM
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Storing and executing compiled code in ROM
Might it be easier to cross compile your code and upload the bytecode via serial?
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Storing and executing compiled code in ROM
If I cross-compile and upload bytecode via serial, can I then execute that from its location in flash without having to load it into RAM?pythoncoder wrote:Might it be easier to cross compile your code and upload the bytecode via serial?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Storing and executing compiled code in ROM
As I understand it persistent bytecode executes from flash on most platforms, the notable exception being the CC3200 where code is executed from RAM.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Storing and executing compiled code in ROM
How big is your ROM? Most ports place a filesystem in ROM if theres enough space. It doesn't need external flash necessarily.
Re: Storing and executing compiled code in ROM
If you upload via serial, then presumably, the bytecode is being stored as a .mpy file in a flash filesystem, which means that the bytecode will be loaded into RAM.tomlogic wrote:If I cross-compile and upload bytecode via serial, can I then execute that from its location in flash without having to load it into RAM?pythoncoder wrote:Might it be easier to cross compile your code and upload the bytecode via serial?
Currently, in order to have the bytecode not be loaded into RAM, it has to be linked into the MicroPython firmware.