see code emitted by bytecode, native, viper decorators?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
pbljung
Posts: 1
Joined: Mon Apr 23, 2018 9:35 pm

see code emitted by bytecode, native, viper decorators?

Post by pbljung » Mon Apr 23, 2018 9:45 pm

How do I see the code emitted by decorators bytecode, native or viper?

In "normal" python I can see the bytecode with foo.func_code.co_code, or import dis and use dis.dis(foo).
I would like to see the dis-like output that D. George shows on https://gotocon.com/dl/goto-amsterdam-2 ... Things.pdf
How do I do that?

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

Re: see code emitted by bytecode, native, viper decorators?

Post by dhylands » Mon Apr 23, 2018 10:28 pm

If you add this line to ports/stm32/mpconfigport.h:

Code: Select all

#define MICROPY_DEBUG_PRINTERS (1)
and then edit py/emitglue.c and change the initial value of mp_verbose_flag to 2: https://github.com/micropython/micropyt ... glue.c#L49

Then when code is compiled it will show the bytecode:

Code: Select all

>>> def foo():
...     print('test')
... 
File <stdin>, code block '<module>' (descriptor: 20003080, bytecode @20003260 21 bytes)
Raw bytecode (code_info_size=7, bytecode_size=14):
 01 00 00 00 00 00 07 46 00 4d 00 00 00 ff 60 00
 24 8d 05 11 5b
arg names:
(N_STATE 1)
(N_EXC_STACK 0)
  bc=-1 line=1
00 MAKE_FUNCTION 20003090
02 STORE_NAME foo
05 LOAD_CONST_NONE
06 RETURN_VALUE
File <stdin>, code block 'foo' (descriptor: 20003090, bytecode @20003280 26 bytes)
Raw bytecode (code_info_size=8, bytecode_size=18):
 02 00 00 00 00 00 08 8d 05 4d 00 21 00 00 ff 1c
 04 04 16 8e 05 64 01 32 11 5b
arg names:
(N_STATE 2)
(N_EXC_STACK 0)
  bc=-1 line=1
  bc=0 line=2
00 LOAD_GLOBAL print
03 LOAD_CONST_STRING 'test'
06 CALL_FUNCTION n=1 nkw=0
08 POP_TOP
09 LOAD_CONST_NONE
10 RETURN_VALUE
>>> 
The first bit of bytecode is the REPL storing the function foo and the second bit is the foo function itself.

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

Re: see code emitted by bytecode, native, viper decorators?

Post by dhylands » Mon Apr 23, 2018 10:45 pm

I'm not sure how you see native or viper code.

Jongy
Posts: 9
Joined: Fri Jun 08, 2018 11:50 am

Re: see code emitted by bytecode, native, viper decorators?

Post by Jongy » Tue Nov 12, 2019 11:27 pm

I found this topic after I watched https://www.youtube.com/watch?v=hHec4qL00x0, played around with "dis" in cpython and thought "hey that's quite cool"

Any plans on adding a real "dis"-like module? I don't think it's a long way from the current "mp_bytecode_print" function.

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

Re: see code emitted by bytecode, native, viper decorators?

Post by jimmo » Tue Nov 12, 2019 11:48 pm

Not what you're looking for but a very cool feature that was added recently is sys.settrace. You can use it to extract the bytecode from a function. Disassembly still left as an exercise for the reader, but could potentially be done in a separate tool. Unfortunately doesn't work for native/viper/asm though.

Code: Select all

import sys

def foo(a, b):
    x = a + b
    return x

def trace(frame, event, arg):
    print(event, frame.f_code.co_name, frame.f_code.co_code)

sys.settrace(trace)
foo(1, 2)
sys.settrace(None)
prints

Code: Select all

call foo b'\xb0\xb1\xf2\xc2\xb2c'

Post Reply