Page 1 of 1

Storing and executing compiled code in ROM

Posted: Mon Sep 12, 2016 9:15 pm
by tomlogic
(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?

Re: Storing and executing compiled code in ROM

Posted: Tue Sep 13, 2016 8:18 am
by pythoncoder
Might it be easier to cross compile your code and upload the bytecode via serial?

Re: Storing and executing compiled code in ROM

Posted: Wed Sep 14, 2016 12:49 am
by tomlogic
pythoncoder wrote:Might it be easier to cross compile your code and upload the bytecode via serial?
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?

Re: Storing and executing compiled code in ROM

Posted: Wed Sep 14, 2016 5:39 am
by pythoncoder
As I understand it persistent bytecode executes from flash on most platforms, the notable exception being the CC3200 where code is executed from RAM.

Re: Storing and executing compiled code in ROM

Posted: Sat Oct 22, 2016 1:43 am
by tannewt
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

Posted: Sat Oct 22, 2016 5:20 am
by dhylands
tomlogic wrote:
pythoncoder wrote:Might it be easier to cross compile your code and upload the bytecode via serial?
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?
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.

Currently, in order to have the bytecode not be loaded into RAM, it has to be linked into the MicroPython firmware.