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: 2755
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, 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: 2755
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, 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.

Post Reply