8d8f83bc05
F-strings, new machine.I2S class, ESP32-C3 support and LEGO_HUB_NO6 board This release of MicroPython adds support for f-strings (PEP-498), with a few limitations compared to normal Python. F-strings are essentially syntactic sugar for "".format() and make formatting strings a lot more convenient. Other improvements to the core runtime include pretty printing OSError when it has two arguments (an errno code and a string), scheduling of KeyboardInterrupt on the main thread, and support for a single argument to the optimised form of StopIteration. In the machine module a new I2S class has been added, with support for esp32 and stm32 ports. This provides a consistent API for transmit and receive of audio data in blocking, non-blocking and asyncio-based operation. Also, the json module has support for the "separators" argument in the dump and dumps functions, and framebuf now includes a way to blit between frame buffers of different formats using a palette. A new, portable machine.bitstream function is also added which can output a stream of bits with configurable timing, and is used as the basis for driving WS2812 LEDs in a common way across ports. There has been some restructuring of the repository directory layout, with all third-party code now in the lib/ directory. And a new top-level directory shared/ has been added with first-party code that was previously in lib/ moved there. The docs have seen further improvement with enhancements and additions to the rp2 parts, as well as a new quick reference for the zephyr port. The terms master/slave have been replaced with controller/peripheral, mainly relating to I2C and SPI usage. And u-module references have been replaced with just the module name without the u-prefix to help clear up the intended usage of modules in MicroPython. For the esp8266 and esp32 ports, hidden networks are now included in WLAN scan results. On the esp32 the RMT class is enhanced with idle_level and write_pulses modes. There is initial support for ESP32-C3 chips with GENERIC_C3 and GENERIC_C3_USB boards. The javascript port has had its Makefile and garbage collector implementation reworked so it compiles and runs with latest the Emscripten using asyncify. The mimxrt port sees the addition of hardware I2C and SPI support, as well as some additional methods to the machine module. There is also support for Hyperflash chips. The nrf port now has full VFS storage support, enables source-line on traceback, and has .mpy features consistent with other ports. For the rp2 port there is now more configurability for boards, and more boards added. The stm32 port has a new LEGO_HUB_NO6 board definition with detailed information how to get this LEGO Hub running stock MicroPython. There is also now support to change the CPU frequency on STM32WB MCUs. And USBD_xxx descriptor options have been renamed to MICROPY_HW_USB_xxx. Thanks to everyone who contributed to this release: Amir Gonnen, Andrew Scheller, Bryan Tong Minh, Chris Wilson, Damien George, Daniel Mizyrycki, David Lechner, David P, Fernando, finefoot, Frank Pilhofer, Glenn Ruben Bakke, iabdalkader, Jeff Epler, Jim Mussared, Jonathan Hogg, Josh Klar, Josh Lloyd, Julia Hathaway, Krzysztof Adamski, Matúš Olekšák, Michael Weiss, Michel Bouwmans, Mike Causer, Mike Teachman, Ned Konz, NitiKaur, oclyke, Patrick Van Oosterwijck, Peter Hinch, Peter Züger, Philipp Ebensberger, robert-hh, Roberto Colistete Jr, Sashkoiv, Seon Rozenblum, Tobias Thyrrestrup, Tom McDermott, Will Sowerbutts, Yonatan Goldschmidt. What follows is a detailed list of changes, generated from the git commit history, and organised into sections. Main components =============== all: - fix signed shifts and NULL access errors from -fsanitize=undefined - update to point to files in new shared/ directory py core: - mpstate: make exceptions thread-local - mpstate: schedule KeyboardInterrupt on main thread - mperrno: add MP_ECANCELED error code - makeqstrdefs.py: don't include .h files explicitly in preprocessing - mark unused arguments from bytecode decoding macros - objexcept: pretty print OSError also when it has 2 arguments - makeversionhdr: add --tags arg to git describe - vm: simplify handling of MP_OBJ_STOP_ITERATION in yield-from opcode - objexcept: make mp_obj_exception_get_value support subclassed excs - support single argument to optimised MP_OBJ_STOP_ITERATION - introduce and use mp_raise_type_arg helper - modsys: optimise sys.exit for code size by using exception helpers - objexcept: make mp_obj_new_exception_arg1 inline - obj: fix formatting of comment for mp_obj_is_integer - emitnative: reuse need_reg_all func in need_stack_settled - emitnative: ensure stack settling is safe mid-branch - runtime: fix bool unary op for subclasses of native types - builtinimport: fix condition for including do_execute_raw_code() - mkrules: automatically build mpy-cross if it doesn't exist - implement partial PEP-498 (f-string) support - lexer: clear fstring_args vstr on lexer free - mkrules.mk: do submodule sync in "make submodules" extmod: - btstack: add missing call to mp_bluetooth_hci_uart_deinit - btstack: check that BLE is active before performing operations - uasyncio: get addr and bind server socket before creating task - axtls-include: add axtls_os_port.h to customise axTLS - update for move of crypto-algorithms, re1.5, uzlib to lib - moduselect: conditionally compile select() - nimble: fix leak in l2cap_send if send-while-stalled - btstack/btstack.mk: use -Wno-implicit-fallthrough, not =0 - utime: always invoke mp_hal_delay_ms when >= to 0ms - modbluetooth: clamp MTU values to 32->UINT16_MAX - nimble: allow modbluetooth binding to hook "sent HCI packet" - nimble: add "memory stalling" mechanism for l2cap_send - uasyncio: in open_connection use address info in socket creation - modujson: add support for dump/dumps separators keyword-argument - modlwip: fix close and clean up of UDP and raw sockets - modbluetooth: add send_update arg to gatts_write - add machine.bitstream - modframebuf: enable blit between different formats via a palette lib: - tinyusb: update to version 0.10.1 - pico-sdk: update to version 1.2.0 - utils/stdout_helpers: make mp_hal_stdout_tx_strn_cooked efficient - axtls: switch to repo at micropython/axtls - axtls: update to latest axtls 2.1.5 wih additional commits - re1.5: move re1.5 code from extmod to lib - uzlib: move uzlib code from extmod to lib - crypto-algorithms: move crypto-algorithms code from extmod to lib - update README's based on contents of these dirs drivers: - neopixel: add common machine.bitstream-based neopixel module - neopixel: optimize fill() for speed - neopixel: reduce code size of driver - cyw43: fix cyw43_deinit so it can be called many times in a row - cyw43: make wifi join fail if interface is not active mpy-cross: - disable stack check when building with Emscripten Support components ================== docs: - library: document new esp32.RMT features and fix wait_done - library: warn that ustruct doesn't handle spaces in format strings - esp8266/tutorial: change flash mode from dio to dout - replace master/slave with controller/peripheral in I2C and SPI - rp2: enhance quickref and change image to Pico pinout - rp2: update general section to give a brief technical overview - library/utime.rst: clarify behaviour and precision of sleep ms/us - library/uasyncio.rst: document stream readexactly() method - library/machine.I2S.rst: fix use of sd pin in examples - zephyr: add quick reference for the Zephyr port - library/zephyr: add libraries specific to the Zephyr port - templates: add unix and zephyr quickref links to top-index - rename ufoo.rst to foo.rst - replace ufoo with foo in all docs - library/index.rst: clarify module naming and purpose - library/builtins.rst: add module title - library/network.rst: simplify socket import - add docs for machine.bitstream and neopixel module - library: fix usage of :term: for frozen module reference - esp8266: use monospace for software tools - reference: mention that slicing a memoryview causes allocation examples: no changes specific to this component/port tests: - extmod: make uasyncio_heaplock test more deterministic - cpydiff/modules_struct_whitespace_in_format: run black - extmod/ujson: add tests for dump/dumps separators argument - run-multitests.py: add broadcast and wait facility - multi_bluetooth/ble_subscribe.py: add test for subscription - extmod/vfs_fat_finaliser.py: ensure alloc at never-used GC blocks - basics: split f-string debug printing to separate file with .exp - pybnative: make while.py test run on boards without pyb.delay tools: - autobuild: add scripts to build release firmware - remove obsolete build-stm-latest.sh script - ci.sh: run apt-get update in ci_powerpc_setup - makemanifest.py: allow passing flags to mpy-tool.py - autobuild: add mimxrt port to build scripts for nightly builds - pyboard.py: add cmd-line option to make soft reset configurable - mpremote: swap order of PID and VID in connect-list output - ci.sh: build unix dev variant as part of macOS CI - ci.sh: build GENERIC_C3 board as part of esp32 CI - autobuild: use separate IDF version to build newer esp32 SoCs - autobuild: add FeatherS2 and TinyS2 to esp32 auto builds - mpremote: add seek whence for mounted files - mpremote: raise OSError on unsupported RemoteFile.seek - autobuild: add the MIMXRT1050_EVKB board to the daily builds - ci.sh: add mpy-cross build to nrf port - codeformat.py: include ports/nrf/modules/nrf in code formatting - gen-cpydiff.py: don't rename foo to ufoo in diff output - autobuild: add auto build for Silicognition wESP32 - mpremote: fix connect-list in case VID/PID are None - mpremote: add "devs" shortcut for "connect list" - mpremote: remove support for pyb.USB_VCP in/out specialisation - autobuild: don't use "-B" for make, it's already a fresh build - pyboard.py: move --no-exclusive/--soft-reset out of mutex group - pyboard.py: make --no-follow use same variable as --follow - pyboard.py: add --exclusive to match --no-exclusive - pyboard.py: make --no-soft-reset consistent with other args - uncrustify: force 1 newline at end of file - mpremote: bump version to 0.0.6 CI: - workflows: add workflow to build and test javascript port - workflows: switch from Coveralls to Codecov - workflows: switch from lcov to gcov - workflows: add workflow to build and test unix dev variant The ports ========= all ports: - use common mp_hal_stdout_tx_strn_cooked instead of custom one - update for move of crypto-algorithms, uzlib to lib - rename USBD_VID/PID config macros to MICROPY_HW_USB_VID/PID bare-arm port: no changes specific to this component/port cc3200 port: no changes specific to this component/port esp8266 port: - add __len__ to NeoPixel driver to support iterating - Makefile: add more libm files to build - include hidden networks in WLAN.scan results - replace esp.neopixel with machine.bitstream - remove dead code for end_ticks in machine_bitstream esp32 port: - boards/sdkconfig.base: disable MEMPROT_FEATURE to alloc from IRAM - add __len__ to NeoPixel driver to support iterating - main: allow MICROPY_DIR to be overridden - esp32_rmt: fix RMT looping in newer IDF versions - esp32_rmt: enhance RMT with idle_level and write_pulses modes - add new machine.I2S class for I2S protocol support - machine_spi: calculate actual attained baudrate - machine_hw_spi: use a 2 item SPI queue for long transfers - machine_dac: add MICROPY_PY_MACHINE_DAC option, enable by default - machine_i2s: add MICROPY_PY_MACHINE_I2S option, enable by default - fix use of mp_int_t, size_t and uintptr_t - add initial support for ESP32C3 SoCs - boards/GENERIC_C3: add generic C3-based board - modmachine: release the GIL in machine.idle() - mphalport: always yield at least once in delay_ms - machine_uart: add flow kw-arg to enable hardware flow control - boards: add Silicognition wESP32 board configuration - mpconfigport.h: enable reverse and inplace special methods - include hidden networks in WLAN.scan results - makeimg.py: get bootloader and partition offset from sdkconfig - enable MICROPY_PY_FSTRINGS by default - machine_hw_spi: release GIL during transfers - machine_pin: make check for non-output pins respect chip variant - replace esp.neopixel with machine.bitstream - remove dead code for end_ticks in machine_bitstream - boards: add GENERIC_C3_USB board with USB serial/JTAG support javascript port: - rework Makefile and GC so it works with latest Emscripten - Makefile: suppress compiler errors from array bounds - Makefile: change variable to EXPORTED_RUNTIME_METHODS mimxrt port: - move calc_weekday helper function to timeutils - machine_spi: add the SPI class to the machine module - moduos: seed the PRNG on boot using the TRNG - boards: set vfs partition start to 1 MBbyte - main: skip running main.py if boot.py failed - main: extend the information returned by help() - mimxrt_flash: remove commented-out code - modmachine: add a few minor methods to the machine module - machine_led: use mp_raise_msg_varg helper - machine_i2c: add hardware-based machine.I2C to machine module - add support for Hyperflash chips - boards: add support for the MIMXRT1050_EVKB board - machine_pin: implement ioctl for Pin minimal port: - Makefile: add support for building with user C modules nrf port: - modules: replace master/slave with controller/peripheral in SPI - boards/common.ld: calculate unused flash region - modules/nrf: add new nrf module with flash block device - drivers: add support for using flash block device with SoftDevice - mpconfigport.h: expose nrf module when MICROPY_PY_NRF is set - README: update README.md to reflect internal file systems - mpconfigport.h: tune FAT FS configuration - Makefile: add _fs_size linker script override from make - modules/uos: allow a board to configure MICROPY_VFS_FAT/LFS1/LFS2 - mpconfigport.h: enable MICROPY_PY_IO_FILEIO when an FS is enabled - qstrdefsport.h: add entries for in-built FS mount points - main: add auto mount and auto format hook for internal flash FS - boards: enable needed features for FAT/LFS1/LFS2 - facilitate use of freeze manifest - boards: set FROZEN_MANIFEST blank when SD present on nrf51 targets - modules/scripts: add file system formatting script - Makefile: set default manifest file for all targets - mphalport: add dummy function for mp_hal_time_ns() - boards: enable MICROPY_VFS_LFS2 for all target boards - modules/uos: add ilistdir to uos module - modules/nrf: add function to enable/disable DCDC - enable source line on tracebacks - set .mpy features consistent with documentation and other ports pic16bit port: no changes specific to this component/port powerpc port: no changes specific to this component/port qemu-arm port: no changes specific to this component/port rp2 port: - use 0=Monday datetime convention in RTC - machine_rtc: in RTC.datetime, compute weekday automatically - CMakeLists.txt: suppress compiler errors for pico-sdk and tinyusb - tusb_config.h: set CFG_TUD_CDC_EP_BUFSIZE to 256 - machine_uart: add hardware flow control support - machine_uart: allow overriding default machine UART pins - machine_i2c: allow boards to configure I2C pins using new macros - machine_spi: allow boards to configure SPI pins using new macros - machine_uart: fix poll ioctl to also check hardware FIFO - machine_uart: fix read when FIFO has chars but ringbuf doesn't - tusb_port: allow boards to configure USB VID and PID - boards/ADAFRUIT_FEATHER_RP2040: configure custom VID/PID - boards/ADAFRUIT_FEATHER_RP2040: configure I2C/SPI default pins - boards/SPARKFUN_PROMICRO: configure UART/I2C/SPI default pins - boards/SPARKFUN_THINGPLUS: configure I2C/SPI default pins - boards: add Adafruit ItsyBitsy RP2040 - boards: add Adafruit QT Py RP2040 - boards: add Pimoroni Pico LiPo 4MB - boards: add Pimoroni Pico LiPo 16MB - boards: add Pimoroni Tiny 2040 - CMakeLists.txt: allow a board's cmake to set the manifest path - enable MICROPY_PY_FSTRINGS by default - Makefile: add "submodules" target, to match other ports - rp2_flash: disable IRQs while calling flash_erase/program - CMakeLists.txt: add option to enable double tap reset to bootrom - mpconfigport.h: allow boards to add root pointers samd port: - add support for building with user C modules stm32 port: - softtimer: add soft_timer_reinsert() helper function - mpbthciport: change from systick to soft-timer for BT scheduling - provide a custom BTstack runloop that integrates with soft timer - usb: make irq's default trigger enable all events - boardctrl: skip running main.py if boot.py had an error - sdio: fix undefined reference to DMA stream on H7 - dma: add DMAMUX configuration for H7 to fix dma_nohal_init - main: call mp_deinit() at end of main - adc: allow using ADC12 and ADC3 for H7 - adc: define the ADC instance used for internal channels - adc: simplify and generalise how pin_adcX table is defined - add new machine.I2S class for I2S protocol support - boards/NUCLEO_F446RE: fix I2C1 pin assignment to match datasheet - replace master/slave with controller/peripheral in I2C and SPI - systick: always POLL_HOOK when delaying for milliseconds - sdram: make SDRAM test cache aware, and optional failure with msg - boards/NUCLEO_F446RE: enable CAN bus support - boards: add support for SparkFun STM32 MicroMod Processor board - uart: fix LPUART1 baudrate set/get - uart: support low baudrates on LPUART1 - boards/STM32F429DISC: set correct UART2 pins and add UART3/6 - boards/NUCLEO_F439ZI: add board definition for NUCLEO_F439ZI - boards/LEGO_HUB_NO6: add board definition for LEGO_HUB_NO6 - Makefile: update to only pull in used Bluetooth library - README.md: update supported MCUs, and submodule and mboot use - usbd_desc: rename USBD_xxx descriptor opts to MICROPY_HW_USB_xxx - usbd_cdc_interface: rename USBD_CDC_xx opts to MICROPY_HW_USB_xx - powerctrl: support changing frequency on WB MCUs - boards/NUCLEO_H743ZI2: add modified version of NUCLEO_H743ZI - mbedtls: fix compile warning about uninitialized val - enable MICROPY_PY_FSTRINGS by default - add implementation of machine.bitstream - Makefile: allow GIT_SUBMODULES and LIBS to be extended - stm32_it: support TIM17 IRQs on WB MCUs - disable computed goto on constrained boards - storage: make extended-block-device more configurable - boards/LEGO_HUB_NO6: change SPI flash storage to use hardware SPI - boards/LEGO_HUB_NO6: skip first 1MiB of SPI flash for storage - boards/LEGO_HUB_NO6: add make commands to backup/restore firmware teensy port: no changes specific to this component/port unix port: - modffi: add option to lock GC in callback, and cfun access - Makefile: add back LIB_SRC_C to list of object files - variants: enable help and help("modules") on standard and dev - Makefile: disable error compression on arm-linux-gnueabi-gcc windows port: - Makefile: add .exe extension to executables name - appveyor: update to VS 2017 and use Python 3.8 for build/test zephyr port: - machine_spi: add support for hardware SPI
983 lines
31 KiB
C
983 lines
31 KiB
C
/*
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) SatoshiLabs
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include "py/profile.h"
|
|
#include "py/bc0.h"
|
|
#include "py/gc.h"
|
|
|
|
#if MICROPY_PY_SYS_SETTRACE
|
|
|
|
#define prof_trace_cb MP_STATE_THREAD(prof_trace_callback)
|
|
|
|
STATIC uint mp_prof_bytecode_lineno(const mp_raw_code_t *rc, size_t bc) {
|
|
const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
return mp_bytecode_get_source_line(prelude->line_info, bc);
|
|
}
|
|
|
|
void mp_prof_extract_prelude(const byte *bytecode, mp_bytecode_prelude_t *prelude) {
|
|
const byte *ip = bytecode;
|
|
|
|
MP_BC_PRELUDE_SIG_DECODE(ip);
|
|
prelude->n_state = n_state;
|
|
prelude->n_exc_stack = n_exc_stack;
|
|
prelude->scope_flags = scope_flags;
|
|
prelude->n_pos_args = n_pos_args;
|
|
prelude->n_kwonly_args = n_kwonly_args;
|
|
prelude->n_def_pos_args = n_def_pos_args;
|
|
|
|
MP_BC_PRELUDE_SIZE_DECODE(ip);
|
|
|
|
prelude->line_info = ip + 4;
|
|
prelude->opcodes = ip + n_info + n_cell;
|
|
|
|
qstr block_name = ip[0] | (ip[1] << 8);
|
|
qstr source_file = ip[2] | (ip[3] << 8);
|
|
prelude->qstr_block_name = block_name;
|
|
prelude->qstr_source_file = source_file;
|
|
}
|
|
|
|
/******************************************************************************/
|
|
// code object
|
|
|
|
STATIC void code_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
|
|
(void)kind;
|
|
mp_obj_code_t *o = MP_OBJ_TO_PTR(o_in);
|
|
const mp_raw_code_t *rc = o->rc;
|
|
const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
mp_printf(print,
|
|
"<code object %q at %p, file \"%q\", line %d>",
|
|
prelude->qstr_block_name,
|
|
o,
|
|
prelude->qstr_source_file,
|
|
rc->line_of_definition
|
|
);
|
|
}
|
|
|
|
STATIC mp_obj_tuple_t *code_consts(const mp_raw_code_t *rc) {
|
|
const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
int start = prelude->n_pos_args + prelude->n_kwonly_args + rc->n_obj;
|
|
int stop = prelude->n_pos_args + prelude->n_kwonly_args + rc->n_obj + rc->n_raw_code;
|
|
mp_obj_tuple_t *consts = MP_OBJ_TO_PTR(mp_obj_new_tuple(stop - start + 1, NULL));
|
|
|
|
size_t const_no = 0;
|
|
for (int i = start; i < stop; ++i) {
|
|
mp_obj_t code = mp_obj_new_code((const mp_raw_code_t *)MP_OBJ_TO_PTR(rc->const_table[i]));
|
|
if (code == MP_OBJ_NULL) {
|
|
m_malloc_fail(sizeof(mp_obj_code_t));
|
|
}
|
|
consts->items[const_no++] = code;
|
|
}
|
|
consts->items[const_no++] = mp_const_none;
|
|
|
|
return consts;
|
|
}
|
|
|
|
STATIC mp_obj_t raw_code_lnotab(const mp_raw_code_t *rc) {
|
|
// const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
uint start = 0;
|
|
uint stop = rc->fun_data_len - start;
|
|
|
|
uint last_lineno = mp_prof_bytecode_lineno(rc, start);
|
|
uint lasti = 0;
|
|
|
|
const uint buffer_chunk_size = (stop - start) >> 2; // heuristic magic
|
|
uint buffer_size = buffer_chunk_size;
|
|
byte *buffer = m_new(byte, buffer_size);
|
|
uint buffer_index = 0;
|
|
|
|
for (uint i = start; i < stop; ++i) {
|
|
uint lineno = mp_prof_bytecode_lineno(rc, i);
|
|
size_t line_diff = lineno - last_lineno;
|
|
if (line_diff > 0) {
|
|
uint instr_diff = (i - start) - lasti;
|
|
|
|
assert(instr_diff < 256);
|
|
assert(line_diff < 256);
|
|
|
|
if (buffer_index + 2 > buffer_size) {
|
|
buffer = m_renew(byte, buffer, buffer_size, buffer_size + buffer_chunk_size);
|
|
buffer_size = buffer_size + buffer_chunk_size;
|
|
}
|
|
last_lineno = lineno;
|
|
lasti = i - start;
|
|
buffer[buffer_index++] = instr_diff;
|
|
buffer[buffer_index++] = line_diff;
|
|
}
|
|
}
|
|
|
|
mp_obj_t o = mp_obj_new_bytes(buffer, buffer_index);
|
|
m_del(byte, buffer, buffer_size);
|
|
return o;
|
|
}
|
|
|
|
STATIC void code_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
|
if (dest[0] != MP_OBJ_NULL) {
|
|
// not load attribute
|
|
return;
|
|
}
|
|
mp_obj_code_t *o = MP_OBJ_TO_PTR(self_in);
|
|
const mp_raw_code_t *rc = o->rc;
|
|
const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
switch (attr) {
|
|
case MP_QSTR_co_code:
|
|
dest[0] = mp_obj_new_bytes(
|
|
(void *)prelude->opcodes,
|
|
rc->fun_data_len - (prelude->opcodes - (const byte *)rc->fun_data)
|
|
);
|
|
break;
|
|
case MP_QSTR_co_consts:
|
|
dest[0] = MP_OBJ_FROM_PTR(code_consts(rc));
|
|
break;
|
|
case MP_QSTR_co_filename:
|
|
dest[0] = MP_OBJ_NEW_QSTR(prelude->qstr_source_file);
|
|
break;
|
|
case MP_QSTR_co_firstlineno:
|
|
dest[0] = MP_OBJ_NEW_SMALL_INT(mp_prof_bytecode_lineno(rc, 0));
|
|
break;
|
|
case MP_QSTR_co_name:
|
|
dest[0] = MP_OBJ_NEW_QSTR(prelude->qstr_block_name);
|
|
break;
|
|
case MP_QSTR_co_names:
|
|
dest[0] = MP_OBJ_FROM_PTR(o->dict_locals);
|
|
break;
|
|
case MP_QSTR_co_lnotab:
|
|
if (!o->lnotab) {
|
|
o->lnotab = raw_code_lnotab(rc);
|
|
}
|
|
dest[0] = o->lnotab;
|
|
break;
|
|
}
|
|
}
|
|
|
|
const mp_obj_type_t mp_type_settrace_codeobj = {
|
|
{ &mp_type_type },
|
|
.name = MP_QSTR_code,
|
|
.print = code_print,
|
|
.unary_op = mp_generic_unary_op,
|
|
.attr = code_attr,
|
|
};
|
|
|
|
mp_obj_t mp_obj_new_code(const mp_raw_code_t *rc) {
|
|
mp_obj_code_t *o = m_new_obj_maybe(mp_obj_code_t);
|
|
if (o == NULL) {
|
|
return MP_OBJ_NULL;
|
|
}
|
|
o->base.type = &mp_type_settrace_codeobj;
|
|
o->rc = rc;
|
|
o->dict_locals = mp_locals_get(); // this is a wrong! how to do this properly?
|
|
o->lnotab = MP_OBJ_NULL;
|
|
return MP_OBJ_FROM_PTR(o);
|
|
}
|
|
|
|
/******************************************************************************/
|
|
// frame object
|
|
|
|
STATIC void frame_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
|
|
(void)kind;
|
|
mp_obj_frame_t *frame = MP_OBJ_TO_PTR(o_in);
|
|
mp_obj_code_t *code = frame->code;
|
|
const mp_raw_code_t *rc = code->rc;
|
|
const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
mp_printf(print,
|
|
"<frame at %p, file '%q', line %d, code %q>",
|
|
frame,
|
|
prelude->qstr_source_file,
|
|
frame->lineno,
|
|
prelude->qstr_block_name
|
|
);
|
|
}
|
|
|
|
STATIC void frame_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
|
if (dest[0] != MP_OBJ_NULL) {
|
|
// not load attribute
|
|
return;
|
|
}
|
|
|
|
mp_obj_frame_t *o = MP_OBJ_TO_PTR(self_in);
|
|
|
|
switch (attr) {
|
|
case MP_QSTR_f_back:
|
|
dest[0] = mp_const_none;
|
|
if (o->code_state->prev_state) {
|
|
dest[0] = MP_OBJ_FROM_PTR(o->code_state->prev_state->frame);
|
|
}
|
|
break;
|
|
case MP_QSTR_f_code:
|
|
dest[0] = MP_OBJ_FROM_PTR(o->code);
|
|
break;
|
|
case MP_QSTR_f_globals:
|
|
dest[0] = MP_OBJ_FROM_PTR(o->code_state->fun_bc->globals);
|
|
break;
|
|
case MP_QSTR_f_lasti:
|
|
dest[0] = MP_OBJ_NEW_SMALL_INT(o->lasti);
|
|
break;
|
|
case MP_QSTR_f_lineno:
|
|
dest[0] = MP_OBJ_NEW_SMALL_INT(o->lineno);
|
|
break;
|
|
}
|
|
}
|
|
|
|
const mp_obj_type_t mp_type_frame = {
|
|
{ &mp_type_type },
|
|
.name = MP_QSTR_frame,
|
|
.print = frame_print,
|
|
.unary_op = mp_generic_unary_op,
|
|
.attr = frame_attr,
|
|
};
|
|
|
|
mp_obj_t mp_obj_new_frame(const mp_code_state_t *code_state) {
|
|
if (gc_is_locked()) {
|
|
return MP_OBJ_NULL;
|
|
}
|
|
|
|
mp_obj_frame_t *o = m_new_obj_maybe(mp_obj_frame_t);
|
|
if (o == NULL) {
|
|
return MP_OBJ_NULL;
|
|
}
|
|
|
|
mp_obj_code_t *code = o->code = MP_OBJ_TO_PTR(mp_obj_new_code(code_state->fun_bc->rc));
|
|
if (code == NULL) {
|
|
return MP_OBJ_NULL;
|
|
}
|
|
|
|
const mp_raw_code_t *rc = code->rc;
|
|
const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
o->code_state = code_state;
|
|
o->base.type = &mp_type_frame;
|
|
o->back = NULL;
|
|
o->code = code;
|
|
o->lasti = code_state->ip - prelude->opcodes;
|
|
o->lineno = mp_prof_bytecode_lineno(rc, o->lasti);
|
|
o->trace_opcodes = false;
|
|
o->callback = MP_OBJ_NULL;
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
}
|
|
|
|
|
|
/******************************************************************************/
|
|
// Trace logic
|
|
|
|
typedef struct {
|
|
struct _mp_obj_frame_t *frame;
|
|
mp_obj_t event;
|
|
mp_obj_t arg;
|
|
} prof_callback_args_t;
|
|
|
|
STATIC mp_obj_t mp_prof_callback_invoke(mp_obj_t callback, prof_callback_args_t *args) {
|
|
assert(mp_obj_is_callable(callback));
|
|
|
|
mp_prof_is_executing = true;
|
|
|
|
mp_obj_t a[3] = {MP_OBJ_FROM_PTR(args->frame), args->event, args->arg};
|
|
mp_obj_t top = mp_call_function_n_kw(callback, 3, 0, a);
|
|
|
|
mp_prof_is_executing = false;
|
|
|
|
if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) {
|
|
mp_handle_pending(true);
|
|
}
|
|
return top;
|
|
}
|
|
|
|
mp_obj_t mp_prof_settrace(mp_obj_t callback) {
|
|
if (mp_obj_is_callable(callback)) {
|
|
prof_trace_cb = callback;
|
|
} else {
|
|
prof_trace_cb = MP_OBJ_NULL;
|
|
}
|
|
return mp_const_none;
|
|
}
|
|
|
|
mp_obj_t mp_prof_frame_enter(mp_code_state_t *code_state) {
|
|
assert(!mp_prof_is_executing);
|
|
|
|
mp_obj_frame_t *frame = MP_OBJ_TO_PTR(mp_obj_new_frame(code_state));
|
|
if (frame == NULL) {
|
|
// Couldn't allocate a frame object
|
|
return MP_OBJ_NULL;
|
|
}
|
|
|
|
if (code_state->prev_state && code_state->frame == NULL) {
|
|
// We are entering not-yet-traced frame
|
|
// which means it's a CALL event (not a GENERATOR)
|
|
// so set the function definition line.
|
|
const mp_raw_code_t *rc = code_state->fun_bc->rc;
|
|
frame->lineno = rc->line_of_definition;
|
|
if (!rc->line_of_definition) {
|
|
frame->lineno = mp_prof_bytecode_lineno(rc, 0);
|
|
}
|
|
}
|
|
code_state->frame = frame;
|
|
|
|
if (!prof_trace_cb) {
|
|
return MP_OBJ_NULL;
|
|
}
|
|
|
|
mp_obj_t top;
|
|
prof_callback_args_t _args, *args = &_args;
|
|
args->frame = code_state->frame;
|
|
|
|
// SETTRACE event CALL
|
|
args->event = MP_OBJ_NEW_QSTR(MP_QSTR_call);
|
|
args->arg = mp_const_none;
|
|
top = mp_prof_callback_invoke(prof_trace_cb, args);
|
|
|
|
code_state->frame->callback = mp_obj_is_callable(top) ? top : MP_OBJ_NULL;
|
|
|
|
// Invalidate the last executed line number so the LINE trace can trigger after this CALL.
|
|
frame->lineno = 0;
|
|
|
|
return top;
|
|
}
|
|
|
|
mp_obj_t mp_prof_frame_update(const mp_code_state_t *code_state) {
|
|
mp_obj_frame_t *frame = code_state->frame;
|
|
if (frame == NULL) {
|
|
// Frame was not allocated (eg because there was no memory available)
|
|
return MP_OBJ_NULL;
|
|
}
|
|
|
|
mp_obj_frame_t *o = frame;
|
|
mp_obj_code_t *code = o->code;
|
|
const mp_raw_code_t *rc = code->rc;
|
|
const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
|
|
assert(o->code_state == code_state);
|
|
|
|
o->lasti = code_state->ip - prelude->opcodes;
|
|
o->lineno = mp_prof_bytecode_lineno(rc, o->lasti);
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
}
|
|
|
|
mp_obj_t mp_prof_instr_tick(mp_code_state_t *code_state, bool is_exception) {
|
|
// Detect execution recursion
|
|
assert(!mp_prof_is_executing);
|
|
assert(code_state->frame);
|
|
assert(mp_obj_get_type(code_state->frame) == &mp_type_frame);
|
|
|
|
// Detect data recursion
|
|
assert(code_state != code_state->prev_state);
|
|
|
|
mp_obj_t top = mp_const_none;
|
|
mp_obj_t callback = code_state->frame->callback;
|
|
|
|
prof_callback_args_t _args, *args = &_args;
|
|
args->frame = code_state->frame;
|
|
args->event = mp_const_none;
|
|
args->arg = mp_const_none;
|
|
|
|
// Call event's are handled inside mp_prof_frame_enter
|
|
|
|
// SETTRACE event EXCEPTION
|
|
if (is_exception) {
|
|
args->event = MP_OBJ_NEW_QSTR(MP_QSTR_exception);
|
|
top = mp_prof_callback_invoke(callback, args);
|
|
return top;
|
|
}
|
|
|
|
// SETTRACE event LINE
|
|
const mp_raw_code_t *rc = code_state->fun_bc->rc;
|
|
const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
size_t prev_line_no = args->frame->lineno;
|
|
size_t current_line_no = mp_prof_bytecode_lineno(rc, code_state->ip - prelude->opcodes);
|
|
if (prev_line_no != current_line_no) {
|
|
args->frame->lineno = current_line_no;
|
|
args->event = MP_OBJ_NEW_QSTR(MP_QSTR_line);
|
|
top = mp_prof_callback_invoke(callback, args);
|
|
}
|
|
|
|
// SETTRACE event RETURN
|
|
const byte *ip = code_state->ip;
|
|
if (*ip == MP_BC_RETURN_VALUE || *ip == MP_BC_YIELD_VALUE) {
|
|
args->event = MP_OBJ_NEW_QSTR(MP_QSTR_return);
|
|
top = mp_prof_callback_invoke(callback, args);
|
|
if (code_state->prev_state && *ip == MP_BC_RETURN_VALUE) {
|
|
code_state->frame->callback = MP_OBJ_NULL;
|
|
}
|
|
}
|
|
|
|
// SETTRACE event OPCODE
|
|
// TODO: frame.f_trace_opcodes=True
|
|
if (false) {
|
|
args->event = MP_OBJ_NEW_QSTR(MP_QSTR_opcode);
|
|
}
|
|
|
|
return top;
|
|
}
|
|
|
|
/******************************************************************************/
|
|
// DEBUG
|
|
|
|
// This section is for debugging the settrace feature itself, and is not intended
|
|
// to be included in production/release builds. The code structure for this block
|
|
// was taken from py/showbc.c and should not be used as a reference. To enable
|
|
// this debug feature enable MICROPY_PROF_INSTR_DEBUG_PRINT_ENABLE in py/profile.h.
|
|
#if MICROPY_PROF_INSTR_DEBUG_PRINT_ENABLE
|
|
|
|
#include "runtime0.h"
|
|
|
|
#define DECODE_UINT { \
|
|
unum = 0; \
|
|
do { \
|
|
unum = (unum << 7) + (*ip & 0x7f); \
|
|
} while ((*ip++ & 0x80) != 0); \
|
|
}
|
|
#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
|
|
#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
|
|
|
|
#define DECODE_QSTR \
|
|
qst = ip[0] | ip[1] << 8; \
|
|
ip += 2;
|
|
#define DECODE_PTR \
|
|
DECODE_UINT; \
|
|
ptr = (const byte *)const_table[unum]
|
|
#define DECODE_OBJ \
|
|
DECODE_UINT; \
|
|
obj = (mp_obj_t)const_table[unum]
|
|
|
|
typedef struct _mp_dis_instruction_t {
|
|
mp_uint_t qstr_opname;
|
|
mp_uint_t arg;
|
|
mp_obj_t argobj;
|
|
mp_obj_t argobjex_cache;
|
|
} mp_dis_instruction_t;
|
|
|
|
STATIC const byte *mp_prof_opcode_decode(const byte *ip, const mp_uint_t *const_table, mp_dis_instruction_t *instruction) {
|
|
mp_uint_t unum;
|
|
const byte *ptr;
|
|
mp_obj_t obj;
|
|
qstr qst;
|
|
|
|
instruction->qstr_opname = MP_QSTR_;
|
|
instruction->arg = 0;
|
|
instruction->argobj = mp_const_none;
|
|
instruction->argobjex_cache = mp_const_none;
|
|
|
|
switch (*ip++) {
|
|
case MP_BC_LOAD_CONST_FALSE:
|
|
instruction->qstr_opname = MP_QSTR_LOAD_CONST_FALSE;
|
|
break;
|
|
|
|
case MP_BC_LOAD_CONST_NONE:
|
|
instruction->qstr_opname = MP_QSTR_LOAD_CONST_NONE;
|
|
break;
|
|
|
|
case MP_BC_LOAD_CONST_TRUE:
|
|
instruction->qstr_opname = MP_QSTR_LOAD_CONST_TRUE;
|
|
break;
|
|
|
|
case MP_BC_LOAD_CONST_SMALL_INT: {
|
|
mp_int_t num = 0;
|
|
if ((ip[0] & 0x40) != 0) {
|
|
// Number is negative
|
|
num--;
|
|
}
|
|
do {
|
|
num = (num << 7) | (*ip & 0x7f);
|
|
} while ((*ip++ & 0x80) != 0);
|
|
instruction->qstr_opname = MP_QSTR_LOAD_CONST_SMALL_INT;
|
|
instruction->arg = num;
|
|
break;
|
|
}
|
|
|
|
case MP_BC_LOAD_CONST_STRING:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_LOAD_CONST_STRING;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
break;
|
|
|
|
case MP_BC_LOAD_CONST_OBJ:
|
|
DECODE_OBJ;
|
|
instruction->qstr_opname = MP_QSTR_LOAD_CONST_OBJ;
|
|
instruction->arg = unum;
|
|
instruction->argobj = obj;
|
|
break;
|
|
|
|
case MP_BC_LOAD_NULL:
|
|
instruction->qstr_opname = MP_QSTR_LOAD_NULL;
|
|
break;
|
|
|
|
case MP_BC_LOAD_FAST_N:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_LOAD_FAST_N;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_LOAD_DEREF:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_LOAD_DEREF;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_LOAD_NAME:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_LOAD_NAME;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT(*ip++);
|
|
}
|
|
break;
|
|
|
|
case MP_BC_LOAD_GLOBAL:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_LOAD_GLOBAL;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT(*ip++);
|
|
}
|
|
break;
|
|
|
|
case MP_BC_LOAD_ATTR:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_LOAD_ATTR;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT(*ip++);
|
|
}
|
|
break;
|
|
|
|
case MP_BC_LOAD_METHOD:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_LOAD_METHOD;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
break;
|
|
|
|
case MP_BC_LOAD_SUPER_METHOD:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_LOAD_SUPER_METHOD;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
break;
|
|
|
|
case MP_BC_LOAD_BUILD_CLASS:
|
|
instruction->qstr_opname = MP_QSTR_LOAD_BUILD_CLASS;
|
|
break;
|
|
|
|
case MP_BC_LOAD_SUBSCR:
|
|
instruction->qstr_opname = MP_QSTR_LOAD_SUBSCR;
|
|
break;
|
|
|
|
case MP_BC_STORE_FAST_N:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_STORE_FAST_N;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_STORE_DEREF:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_STORE_DEREF;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_STORE_NAME:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_STORE_NAME;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
break;
|
|
|
|
case MP_BC_STORE_GLOBAL:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_STORE_GLOBAL;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
break;
|
|
|
|
case MP_BC_STORE_ATTR:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_STORE_ATTR;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT(*ip++);
|
|
}
|
|
break;
|
|
|
|
case MP_BC_STORE_SUBSCR:
|
|
instruction->qstr_opname = MP_QSTR_STORE_SUBSCR;
|
|
break;
|
|
|
|
case MP_BC_DELETE_FAST:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_DELETE_FAST;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_DELETE_DEREF:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_DELETE_DEREF;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_DELETE_NAME:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_DELETE_NAME;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
break;
|
|
|
|
case MP_BC_DELETE_GLOBAL:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_DELETE_GLOBAL;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
break;
|
|
|
|
case MP_BC_DUP_TOP:
|
|
instruction->qstr_opname = MP_QSTR_DUP_TOP;
|
|
break;
|
|
|
|
case MP_BC_DUP_TOP_TWO:
|
|
instruction->qstr_opname = MP_QSTR_DUP_TOP_TWO;
|
|
break;
|
|
|
|
case MP_BC_POP_TOP:
|
|
instruction->qstr_opname = MP_QSTR_POP_TOP;
|
|
break;
|
|
|
|
case MP_BC_ROT_TWO:
|
|
instruction->qstr_opname = MP_QSTR_ROT_TWO;
|
|
break;
|
|
|
|
case MP_BC_ROT_THREE:
|
|
instruction->qstr_opname = MP_QSTR_ROT_THREE;
|
|
break;
|
|
|
|
case MP_BC_JUMP:
|
|
DECODE_SLABEL;
|
|
instruction->qstr_opname = MP_QSTR_JUMP;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_POP_JUMP_IF_TRUE:
|
|
DECODE_SLABEL;
|
|
instruction->qstr_opname = MP_QSTR_POP_JUMP_IF_TRUE;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_POP_JUMP_IF_FALSE:
|
|
DECODE_SLABEL;
|
|
instruction->qstr_opname = MP_QSTR_POP_JUMP_IF_FALSE;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_JUMP_IF_TRUE_OR_POP:
|
|
DECODE_SLABEL;
|
|
instruction->qstr_opname = MP_QSTR_JUMP_IF_TRUE_OR_POP;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_JUMP_IF_FALSE_OR_POP:
|
|
DECODE_SLABEL;
|
|
instruction->qstr_opname = MP_QSTR_JUMP_IF_FALSE_OR_POP;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_SETUP_WITH:
|
|
DECODE_ULABEL; // loop-like labels are always forward
|
|
instruction->qstr_opname = MP_QSTR_SETUP_WITH;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_WITH_CLEANUP:
|
|
instruction->qstr_opname = MP_QSTR_WITH_CLEANUP;
|
|
break;
|
|
|
|
case MP_BC_UNWIND_JUMP:
|
|
DECODE_SLABEL;
|
|
instruction->qstr_opname = MP_QSTR_UNWIND_JUMP;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_SETUP_EXCEPT:
|
|
DECODE_ULABEL; // except labels are always forward
|
|
instruction->qstr_opname = MP_QSTR_SETUP_EXCEPT;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_SETUP_FINALLY:
|
|
DECODE_ULABEL; // except labels are always forward
|
|
instruction->qstr_opname = MP_QSTR_SETUP_FINALLY;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_END_FINALLY:
|
|
// if TOS is an exception, reraises the exception (3 values on TOS)
|
|
// if TOS is an integer, does something else
|
|
// if TOS is None, just pops it and continues
|
|
// else error
|
|
instruction->qstr_opname = MP_QSTR_END_FINALLY;
|
|
break;
|
|
|
|
case MP_BC_GET_ITER:
|
|
instruction->qstr_opname = MP_QSTR_GET_ITER;
|
|
break;
|
|
|
|
case MP_BC_GET_ITER_STACK:
|
|
instruction->qstr_opname = MP_QSTR_GET_ITER_STACK;
|
|
break;
|
|
|
|
case MP_BC_FOR_ITER:
|
|
DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
|
|
instruction->qstr_opname = MP_QSTR_FOR_ITER;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_BUILD_TUPLE:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_BUILD_TUPLE;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_BUILD_LIST:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_BUILD_LIST;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_BUILD_MAP:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_BUILD_MAP;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_STORE_MAP:
|
|
instruction->qstr_opname = MP_QSTR_STORE_MAP;
|
|
break;
|
|
|
|
case MP_BC_BUILD_SET:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_BUILD_SET;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
#if MICROPY_PY_BUILTINS_SLICE
|
|
case MP_BC_BUILD_SLICE:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_BUILD_SLICE;
|
|
instruction->arg = unum;
|
|
break;
|
|
#endif
|
|
|
|
case MP_BC_STORE_COMP:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_STORE_COMP;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_UNPACK_SEQUENCE:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_UNPACK_SEQUENCE;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_UNPACK_EX:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_UNPACK_EX;
|
|
instruction->arg = unum;
|
|
break;
|
|
|
|
case MP_BC_MAKE_FUNCTION:
|
|
DECODE_PTR;
|
|
instruction->qstr_opname = MP_QSTR_MAKE_FUNCTION;
|
|
instruction->arg = unum;
|
|
instruction->argobj = mp_obj_new_int_from_ull((uint64_t)ptr);
|
|
break;
|
|
|
|
case MP_BC_MAKE_FUNCTION_DEFARGS:
|
|
DECODE_PTR;
|
|
instruction->qstr_opname = MP_QSTR_MAKE_FUNCTION_DEFARGS;
|
|
instruction->arg = unum;
|
|
instruction->argobj = mp_obj_new_int_from_ull((uint64_t)ptr);
|
|
break;
|
|
|
|
case MP_BC_MAKE_CLOSURE: {
|
|
DECODE_PTR;
|
|
mp_uint_t n_closed_over = *ip++;
|
|
instruction->qstr_opname = MP_QSTR_MAKE_CLOSURE;
|
|
instruction->arg = unum;
|
|
instruction->argobj = mp_obj_new_int_from_ull((uint64_t)ptr);
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT(n_closed_over);
|
|
break;
|
|
}
|
|
|
|
case MP_BC_MAKE_CLOSURE_DEFARGS: {
|
|
DECODE_PTR;
|
|
mp_uint_t n_closed_over = *ip++;
|
|
instruction->qstr_opname = MP_QSTR_MAKE_CLOSURE_DEFARGS;
|
|
instruction->arg = unum;
|
|
instruction->argobj = mp_obj_new_int_from_ull((uint64_t)ptr);
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT(n_closed_over);
|
|
break;
|
|
}
|
|
|
|
case MP_BC_CALL_FUNCTION:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_CALL_FUNCTION;
|
|
instruction->arg = unum & 0xff;
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT((unum >> 8) & 0xff);
|
|
break;
|
|
|
|
case MP_BC_CALL_FUNCTION_VAR_KW:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_CALL_FUNCTION_VAR_KW;
|
|
instruction->arg = unum & 0xff;
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT((unum >> 8) & 0xff);
|
|
break;
|
|
|
|
case MP_BC_CALL_METHOD:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_CALL_METHOD;
|
|
instruction->arg = unum & 0xff;
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT((unum >> 8) & 0xff);
|
|
break;
|
|
|
|
case MP_BC_CALL_METHOD_VAR_KW:
|
|
DECODE_UINT;
|
|
instruction->qstr_opname = MP_QSTR_CALL_METHOD_VAR_KW;
|
|
instruction->arg = unum & 0xff;
|
|
instruction->argobjex_cache = MP_OBJ_NEW_SMALL_INT((unum >> 8) & 0xff);
|
|
break;
|
|
|
|
case MP_BC_RETURN_VALUE:
|
|
instruction->qstr_opname = MP_QSTR_RETURN_VALUE;
|
|
break;
|
|
|
|
case MP_BC_RAISE_LAST:
|
|
instruction->qstr_opname = MP_QSTR_RAISE_LAST;
|
|
break;
|
|
|
|
case MP_BC_RAISE_OBJ:
|
|
instruction->qstr_opname = MP_QSTR_RAISE_OBJ;
|
|
break;
|
|
|
|
case MP_BC_RAISE_FROM:
|
|
instruction->qstr_opname = MP_QSTR_RAISE_FROM;
|
|
break;
|
|
|
|
case MP_BC_YIELD_VALUE:
|
|
instruction->qstr_opname = MP_QSTR_YIELD_VALUE;
|
|
break;
|
|
|
|
case MP_BC_YIELD_FROM:
|
|
instruction->qstr_opname = MP_QSTR_YIELD_FROM;
|
|
break;
|
|
|
|
case MP_BC_IMPORT_NAME:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_IMPORT_NAME;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
break;
|
|
|
|
case MP_BC_IMPORT_FROM:
|
|
DECODE_QSTR;
|
|
instruction->qstr_opname = MP_QSTR_IMPORT_FROM;
|
|
instruction->arg = qst;
|
|
instruction->argobj = MP_OBJ_NEW_QSTR(qst);
|
|
break;
|
|
|
|
case MP_BC_IMPORT_STAR:
|
|
instruction->qstr_opname = MP_QSTR_IMPORT_STAR;
|
|
break;
|
|
|
|
default:
|
|
if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
|
|
instruction->qstr_opname = MP_QSTR_LOAD_CONST_SMALL_INT;
|
|
instruction->arg = (mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16;
|
|
} else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
|
|
instruction->qstr_opname = MP_QSTR_LOAD_FAST;
|
|
instruction->arg = (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI;
|
|
} else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
|
|
instruction->qstr_opname = MP_QSTR_STORE_FAST;
|
|
instruction->arg = (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI;
|
|
} else if (ip[-1] < MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NUM_BYTECODE) {
|
|
instruction->qstr_opname = MP_QSTR_UNARY_OP;
|
|
instruction->arg = (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI;
|
|
} else if (ip[-1] < MP_BC_BINARY_OP_MULTI + MP_BINARY_OP_NUM_BYTECODE) {
|
|
mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
|
|
instruction->qstr_opname = MP_QSTR_BINARY_OP;
|
|
instruction->arg = op;
|
|
} else {
|
|
mp_printf(&mp_plat_print, "code %p, opcode 0x%02x not implemented\n", ip - 1, ip[-1]);
|
|
assert(0);
|
|
return ip;
|
|
}
|
|
break;
|
|
}
|
|
|
|
return ip;
|
|
}
|
|
|
|
void mp_prof_print_instr(const byte *ip, mp_code_state_t *code_state) {
|
|
mp_dis_instruction_t _instruction, *instruction = &_instruction;
|
|
mp_prof_opcode_decode(ip, code_state->fun_bc->rc->const_table, instruction);
|
|
const mp_raw_code_t *rc = code_state->fun_bc->rc;
|
|
const mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
|
|
mp_uint_t offset = ip - prelude->opcodes;
|
|
mp_printf(&mp_plat_print, "instr");
|
|
|
|
/* long path */ if (1) {
|
|
mp_printf(&mp_plat_print,
|
|
"@%p:%q:%q+0x%04x:%d",
|
|
ip,
|
|
prelude->qstr_source_file,
|
|
prelude->qstr_block_name,
|
|
offset,
|
|
mp_prof_bytecode_lineno(rc, offset)
|
|
);
|
|
}
|
|
|
|
/* bytecode */ if (0) {
|
|
mp_printf(&mp_plat_print, " %02x %02x %02x %02x", ip[0], ip[1], ip[2], ip[3]);
|
|
}
|
|
|
|
mp_printf(&mp_plat_print, " 0x%02x %q [%d]", *ip, instruction->qstr_opname, instruction->arg);
|
|
|
|
if (instruction->argobj != mp_const_none) {
|
|
mp_printf(&mp_plat_print, " $");
|
|
mp_obj_print_helper(&mp_plat_print, instruction->argobj, PRINT_REPR);
|
|
}
|
|
if (instruction->argobjex_cache != mp_const_none) {
|
|
mp_printf(&mp_plat_print, " #");
|
|
mp_obj_print_helper(&mp_plat_print, instruction->argobjex_cache, PRINT_REPR);
|
|
}
|
|
|
|
mp_printf(&mp_plat_print, "\n");
|
|
}
|
|
|
|
#endif // MICROPY_PROF_INSTR_DEBUG_PRINT_ENABLE
|
|
|
|
#endif // MICROPY_PY_SYS_SETTRACE
|