Page 1 of 1

Python objects in ROM?

Posted: Mon Oct 06, 2014 5:55 am
by andershedberg
Hi, we will have about 6000 lines of python code ported from another project, which we would like to move to the micro-controller. It will run out of RAM memory during compilation. We could write it as C libraries, but then we would have no benefit of Python. What is the best approach to pre-compile python (any micro python variant of compiled code will do) and put it into ROM (I mean flash, but not the file system /flash as I don't believe it supports Execute In Place?)

1. How do we get pre-compiled python? (Can we trick the board to emit compiled bytes on to the serial port? Can the unix version emit such code?)
2. How do we put it in ROM? (Should we make an assembler-file with just databytes out of it and link as the C-libraries)
3. How do we get micro python to find it for our real .py files when they use import?
4. Can this be done for immutable datastructures as well?

Is there a high level description of the compile/emit/import stages of micropython? The parser is described here in the link below, and the article ends with an "next is compiler and code emitters.." but I can't see that those articles were written?!? https://www.kickstarter.com/projects/21 ... sts/669549

Thanks for any help to put me in the right direction /Anders H.

Re: Python objects in ROM?

Posted: Mon Oct 06, 2014 2:28 pm
by dhylands
andershedberg wrote:Hi, we will have about 6000 lines of python code ported from another project, which we would like to move to the micro-controller. It will run out of RAM memory during compilation. We could write it as C libraries, but then we would have no benefit of Python. What is the best approach to pre-compile python (any micro python variant of compiled code will do) and put it into ROM (I mean flash, but not the file system /flash as I don't believe it supports Execute In Place?)

1. How do we get pre-compiled python? (Can we trick the board to emit compiled bytes on to the serial port? Can the unix version emit such code?)
Currently, it isn't possible to store the python bytecodes in ROM.
https://github.com/micropython/micropython/issues/222
andershedberg wrote: 2. How do we put it in ROM? (Should we make an assembler-file with just databytes out of it and link as the C-libraries)
3. How do we get micro python to find it for our real .py files when they use import?
4. Can this be done for immutable datastructures as well?
It is possible to put many python data structures in ROM, but currently these are coded in C. To make this work today, I think you'd need to write a python script which converts the python data structure into some C code and compie that.
andershedberg wrote: Is there a high level description of the compile/emit/import stages of micropython? The parser is described here in the link below, and the article ends with an "next is compiler and code emitters.." but I can't see that those articles were written?!? https://www.kickstarter.com/projects/21 ... sts/669549

Thanks for any help to put me in the right direction /Anders H.
I don't believe that there is. import is handled by looking in an dict of already loaded modules, and if a given module is found there it's used, otherwise sys.path is searched for module.py

Re: Python objects in ROM?

Posted: Mon Oct 06, 2014 4:21 pm
by andershedberg
dhylands wrote:
andershedberg wrote: 2. How do we put it in ROM? (Should we make an assembler-file with just databytes out of it and link as the C-libraries)
3. How do we get micro python to find it for our real .py files when they use import?
4. Can this be done for immutable datastructures as well?
It is possible to put many python data structures in ROM, but currently these are coded in C. To make this work today, I think you'd need to write a python script which converts the python data structure into some C code and compie that.
Thanks for the answer, I'll look into how to make C programs, as https://github.com/micropython/micropython/issues/222 was a bit over my head.

Re: Python objects in ROM?

Posted: Tue Oct 07, 2014 5:24 am
by dhylands
You'll probably want to start looking at this thread: http://forum.micropython.org/viewtopic.php?f=3&t=153