-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Tool to generate native .mpy files from a .elf file (dynamically loadable native code) #5083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
25fc82f
to
8023e37
Compare
I redid this, changing it so that it builds the .mpy directly from .o files rather than .elf. The new tool is called So now all included examples work on all supported targets (x86-64, ARM Thumb2, xtensa-esp32). |
86d3c70
to
30bfdf7
Compare
I've updated this further. New features include:
|
30bfdf7
to
be0a16d
Compare
a4fa62e
to
1dc3a4b
Compare
43bafff
to
33dc35a
Compare
81cd7a5
to
e7d9532
Compare
@stinos the AppVeyor build is failing here because the compiler doesn't like Do you know how, in MSVC, to declare a function type (using typedef) that does not return? It works under gcc... |
Unless I'm missing something this cannot be done, this is a gcc extension. For msvc (and C's _Noreturn as well), noreturn behaves like inline i.e. it is not part of the type. As such I also cannot think of a usable workaround.. |
OK.... I'll have to think of a way around it. Possibly just put |
e7d9532
to
b0c448d
Compare
So they can be built as dynamic native modules, as well as existing static native modules.
We don't want to add a feature flag to .mpy files that indicate float support because it will get complex and difficult to use. Instead the .mpy is built using whatever precision it chooses (float or double) and the native glue API will convert between this choice and what the host runtime actually uses.
d73784c
to
ac76967
Compare
FINALLY THIS IS DONE AND MERGED! Still to come: docs. |
Question (ESP32): |
The code (text) will be copied to iRAM and executed from there. BSS and rodata are copied to normal RAM.
Yes, but note that not all flash can be executed from, only a limited part of it. And similarly with RAM, only iRAM can be executed from. Also note that the code must be relocated and linked in to the firmware, ie modified, so it must go in to RAM. There is a plan to eventually support putting .mpy files in flash, to be executed from there, saving (i)RAM. The idea is to set aside a region of flash for user modules to be "dynamically frozen" to, and to freeze them at runtime via a call like |
Thank you for the explanation.
This could be very useful, especially for dynamically loading large libraries. |
What version of micropython is this tool (elf2mpy.py) a part of? Thanks for the work @dpgeorge |
It's now called |
Thanks!
Rich
…Sent from my pHone
On Sun, Jan 31, 2021, 7:01 PM Damien George ***@***.***> wrote:
What version of micropython is this tool (elf2mpy.py) a part of?
It's now called tools/mpy_ld.py and was added in aad79ad
<aad79ad>,
so first appeard in v1.12.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#5083 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAVQFMU7O32JAV3BGCEPK5TS4XVMVANCNFSM4IU3ZRVA>
.
|
STM32: add SleepMemory
This PR adds a tool
elf2mpy.py
and associated build scripts/headers to build .mpy files from C source code. Such .mpy files can then be dynamically imported as though they were a normal Python module, even though they are implemented in C.Included in this PR are some examples: ex0, ex1 and ex2 are basic examples, and the qr example shows how to take an existing C library "off the shelf" and wrap it up into a .mpy file (to generate QR codes, work done in collaboration with @mattytrentini ).
Currently the tool supports targets of unix x86-64, ARM Thumb2 (Cortex M3 and above, eg stm32 boards), and partial support for esp32. esp32 support is non trivial and the qr example does not work (at this stage I'm unsure why). esp32 support relies on #5082
To try this PR out do the following:
tools/elf2mpy/ex0
directoryMake 8000 file
to select the architecture (unix, stm32, esp32)make
ex0.mpy
file to the boardimport ex0
ex0.foo(1, 2)
(should return 4)ex0.factorial(5)
(should return 120)Then try out ex1, ex2 and qr. Also use one of these examples as a template to write your own C-based module.
There's still a bit of work to do to clean this up (eg tool/file naming, directory structure, docs). Also freezing the resulting mpy files won't work at the moment because the code relocates itself (writes to itself) when it is imported, and frozen code is read-only. Eventually this can be fixed.
Edit: for past discussion and PR on this idea see #1627