Hello everyone, I recently worked on some building some C++ modules for micropython and as a result went through a lot of the source code. I found it very enjoyable. Nonetheless, I have merely scratched the surface and I would love to learn more about the architecture and the actual source code.
I would like some suggestions on how I can proceed. I am new to firmware, I am a decent C++ developer and I have a good understanding of computer architecture. Here is what I would like to know:
1. What other knowledge can I acquire to be in a better position to get a deeper understanding of micropython ?
2. How do I go about understanding the source code ? (I am aware it will not happen overnight)
I have ignored open source projects due to the sheer amount of code to go through but I would like to give it a go this time.
Many thanks for any help
Understanding the source code
Re: Understanding the source code
One way is to build the unix version of micropython and then use a debugger to single step through the code as its executing various things in python.
Re: Understanding the source code
Interesting question... I can sympathize having read the source code of programs from beginning to end on a printout in the 70's
Given the amount of code that is here I think your best bet is to first figure out what you're interested in and then go and track down how it works. I mostly end up in the source code 'cause I'm trying to get something to work and it ain't doing it, so I have to go and figure out what the implementation looks like.
Given the amount of code that is here I think your best bet is to first figure out what you're interested in and then go and track down how it works. I mostly end up in the source code 'cause I'm trying to get something to work and it ain't doing it, so I have to go and figure out what the implementation looks like.
Re: Understanding the source code
I was in your position a year ago. I had used MicroPython extensively in projects and had embedded it in a larger project but had never really dived into the internals (i.e. what's an mp_obj_t, when to use MP_OBJ_FROM_PTR, etc).
One huge thing that helped was answering questions here (it's one of the main reasons I first started participating in the forum) -- many of the questions I answered required very specific and targeted spelunking that was highly educational. Often a lot of work to get a very simple answer. (That's not a dig at MicroPython, more that some of the questions involved some really subtle stuff).
I'd also echo Dave's suggestion about using GDB on the Unix port. I do that a lot...many times per week.
Another trick that's really useful is understanding the bytecode. Again using the Unix port, running it with "./micropython -v -v -v program.py" will dump the bytecode in readable form".
Some other things... there are a lot of clever and subtle tricks (often for code size) but for the most part MicroPython works the way you'd expect. The GC is very simple (and gc.c is entirely self-contained), it's a good starting point. (Here's a not very good explanation I gave a few months ago -- https://www.youtube.com/watch?v=H_xq8IYjh2w )
If you treat the NLR stuff like a black box, the VM dispatch is pretty neat too.
I think that as a C++ developer too, I took a while to get used to the macros everywhere, but I understand of course why they're necessary.
Where I get lost easily still though is in anything to do with the native emitters and the glue between them...
One huge thing that helped was answering questions here (it's one of the main reasons I first started participating in the forum) -- many of the questions I answered required very specific and targeted spelunking that was highly educational. Often a lot of work to get a very simple answer. (That's not a dig at MicroPython, more that some of the questions involved some really subtle stuff).
I'd also echo Dave's suggestion about using GDB on the Unix port. I do that a lot...many times per week.
Another trick that's really useful is understanding the bytecode. Again using the Unix port, running it with "./micropython -v -v -v program.py" will dump the bytecode in readable form".
Some other things... there are a lot of clever and subtle tricks (often for code size) but for the most part MicroPython works the way you'd expect. The GC is very simple (and gc.c is entirely self-contained), it's a good starting point. (Here's a not very good explanation I gave a few months ago -- https://www.youtube.com/watch?v=H_xq8IYjh2w )
If you treat the NLR stuff like a black box, the VM dispatch is pretty neat too.
I think that as a C++ developer too, I took a while to get used to the macros everywhere, but I understand of course why they're necessary.
Where I get lost easily still though is in anything to do with the native emitters and the glue between them...
- saiftyfirst
- Posts: 12
- Joined: Wed Mar 04, 2020 4:58 pm
Re: Understanding the source code
Great! Will keep the tips in mind