Understanding the source code

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
saiftyfirst
Posts: 10
Joined: Wed Mar 04, 2020 4:58 pm

Understanding the source code

Post by saiftyfirst » Wed Mar 11, 2020 8:51 pm

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 :)

User avatar
dhylands
Posts: 3300
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Understanding the source code

Post by dhylands » Thu Mar 12, 2020 12:58 am

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.

User avatar
tve
Posts: 104
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: Understanding the source code

Post by tve » Thu Mar 12, 2020 2:28 am

Interesting question... I can sympathize having read the source code of programs from beginning to end on a printout in the 70's :lol:

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.

User avatar
jimmo
Posts: 1440
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Understanding the source code

Post by jimmo » Mon Mar 16, 2020 4:50 am

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... :)

User avatar
saiftyfirst
Posts: 10
Joined: Wed Mar 04, 2020 4:58 pm

Re: Understanding the source code

Post by saiftyfirst » Fri Mar 20, 2020 12:37 pm

Great! Will keep the tips in mind

Post Reply