Page 1 of 2

how to decompile bytecode into human readable format

Posted: Mon Oct 02, 2017 8:54 pm
by jickster
I used the mpy-cross utility to turn a .py into a bytecode .mpy file.

Is it possible to decompile the .mpy into human readable format?

Re: how to decompile bytecode into human readable format

Posted: Tue Oct 03, 2017 6:16 am
by pythoncoder
I don't think anyone has written a tool to do this. I imagine it would be a straightforward task if there were any documentation for the bytecode. Alas (as far as I know) there is not. It's also worth noting that the bytecode is something of a moving target in that Damien occasionally adds opcodes. Hence it's wise to recompile mpy-cross when you download MicroPython sourcecode.

Re: how to decompile bytecode into human readable format

Posted: Tue Oct 03, 2017 6:39 am
by Roberthh
@jickster What is your intention?
If you are asking whether bytecode is something like a protected format: It is not! The intention is efficiency: split the compile and execute phase and use a machine with a lot of resources (your PC) for compile.
If you are simply interested how the bytecode looks like? There may be information available by Damien or at least represented by the source code.
If you have bytecode from somewhere and want to look into it: just go ahead and do it. You may end up with your own tool and a lot of understanding of the bytecode.

Re: how to decompile bytecode into human readable format

Posted: Tue Oct 03, 2017 7:00 am
by pythoncoder
@Roberthh You're describing a process of reverse engineering here - writing short MicroPython scripts, examining the bytecode and deducing its purpose. While possible and instructive it would be hard (understatement?) to guarantee a complete set of bytecodes.

A decompiler would be a useful tool, notably when trying to optimise code. If there's much interest perhaps you or I might consider writing one but I think we'd need information from Damien. To some extent its feasibility would depend on the degree to which existing bytecodes are set in stone or whether their functionality is subject to change.

Re: how to decompile bytecode into human readable format

Posted: Tue Oct 03, 2017 9:03 am
by mwm

Re: how to decompile bytecode into human readable format

Posted: Tue Oct 03, 2017 3:35 pm
by dhylands
The extract_byte_code.py was intended to extract native code and then I ran a disassembler on the binary blob to compare to the inline assembler.

There is actually a byte-code annotator in the source code. You can turn it on and get the byte code dumped in a human readable form while its compiling.

It looks like there are some functions for displaying bytecodes in human readable form in the py/showbc.c file.

Re: how to decompile bytecode into human readable format

Posted: Sat Nov 14, 2020 7:06 am
by Pytri
jickster wrote:
Mon Oct 02, 2017 8:54 pm
I used the mpy-cross utility to turn a .py into a bytecode .mpy file.

Is it possible to decompile the .mpy into human readable format?
Give a try to Python Decompiler Online. It works for .pyc files.

Re: how to decompile bytecode into human readable format

Posted: Sat Nov 14, 2020 9:15 am
by jimmo
Pytri wrote:
Sat Nov 14, 2020 7:06 am
Give a try to Python Decompiler Online. It works for .pyc files.
MicroPython does not use pyc files, and the bytecode is completely different to CPython.

Re: how to decompile bytecode into human readable format

Posted: Sun Nov 29, 2020 8:44 pm
by MasterOfGizmo
Any news on this?

The Lego Spike hub runs micropython and the source code doesn't seem to be public. Its default UI comes in form of a mpy and can easily be downloaded from the device. It would be nice to be able to decompile and modify this.

Re: how to decompile bytecode into human readable format

Posted: Sun Dec 20, 2020 5:53 pm
by jcase
While today is my first day playing with micropython, I'm pretty versed in writing disassemblers and decompilers from scratch. I'll give it a shot at writing one. Can anyone share a source for some precompiled bins that don't have source available? Obviously I'll start with my own examples, but would like some unknowns for validation.