micropython/py/scope.c

158 lines
5.7 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* 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.
*/
2013-10-04 19:53:11 +01:00
#include <assert.h>
#include "py/scope.h"
2013-10-04 19:53:11 +01:00
#if MICROPY_ENABLE_COMPILER
// these low numbered qstrs should fit in 8 bits
STATIC const uint8_t scope_simple_name_table[] = {
[SCOPE_MODULE] = MP_QSTR__lt_module_gt_,
[SCOPE_LAMBDA] = MP_QSTR__lt_lambda_gt_,
[SCOPE_LIST_COMP] = MP_QSTR__lt_listcomp_gt_,
[SCOPE_DICT_COMP] = MP_QSTR__lt_dictcomp_gt_,
[SCOPE_SET_COMP] = MP_QSTR__lt_setcomp_gt_,
[SCOPE_GEN_EXPR] = MP_QSTR__lt_genexpr_gt_,
};
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t emit_options) {
scope_t *scope = m_new0(scope_t, 1);
2013-10-04 19:53:11 +01:00
scope->kind = kind;
scope->pn = pn;
scope->source_file = source_file;
if (kind == SCOPE_FUNCTION || kind == SCOPE_CLASS) {
#if MICROPY_USE_SMALL_HEAP_COMPILER
Merge tag 'v1.8.5' into parse-bytecode New port to Zephyr, upip runs on baremetal, and reduction in code size This release adds a new port of MicroPython to run on top of the Zephyr real-time operating system. As part of this there is now basic support for using mbedTLS as the ussl module. The release also brings initial support for running the package manager upip on baremetal systems with low heap memory (such as esp8266), through a Python module interface. Work has been done in this release to clean up the core, removing redundant and/or unreachable code, and factoring existing duplicated code patterns. This brings a reduction of 828 bytes in code size to the bare-arm port, and 1368 bytes to the minimal port. There is also improved coverage through the addition of new tests for corner cases. The "micropython" module sees the addition of the "const" identity function which should be used as "from micropython import const" in scripts that want to use the MicroPython constant optimisations in the compile stage. There is also the addition of the "opt_level" function to change the parser/compiler optimisation level at runtime. The behaviour of "sys.exit" (and "raise SystemExit") on baremetal is now changed: this function no longer does a soft-reset of the board, rather it just stops the running script and drops to the REPL. In order to do an actual soft reset the "machine.soft_reset" function has been added (to the stmhal port only, for the time being). Following CPython, KeyError exceptions for dictionary lookups now have the failed key stored as the argument of the exception instance, accessible as exc.args[0]. The "ujson.load" function has also been added, to load JSON data from an arbitrary stream. The I2C support in the machine module now has clock stretching, the addition of the "addrsize" parameter in memory transfer methods, and I2C scanning now uses dummy writes instead of dummy reads to make the scanning more reliable. The CMSIS library has been upgrade to v4.30, and the boards section of the stmhal port has been refactored to use a common.ld script. The stmhal port now has a full implementation of the machine.SPI class, with support for both hardware SPI peripherals and fast software SPI. The USB HID driver in stmhal has added support to receive USB HID messages from the host. py core: - asmthumb: flush D-cache, and invalidate I-cache on STM32F7 - makeqstrdefs.py: use python 2.6 syntax for set creation - objnone: remove unnecessary handling of MP_UNARY_OP_BOOL - move frozen modules rules from esp8266 port for reuse across ports - combine 3 comprehension emit functions (list/dict/set) into 1 - combine 3 comprehension opcodes (list/dict/set) into 1 - vstr: remove vstr.had_error flag and inline basic vstr functions - objnone: use mp_generic_unary_op instead of custom one - showbc: make printf's go to the platform print stream - remove 'name' member from mp_obj_module_t struct - builtinimport: fix nanbox build after change to better handle -m modules - stream: remove unnecessary check for NULL return from vstr_extend - py.mk: suppress some compiler warnings when building berkeley-db - shrink mp_arg_t struct by using reduced-size integer members - update opcode format table because 3 opcodes were removed, 1 added - parse: only replace constants that are standalone identifiers - py.mk: add support for building modussl_mbedtls - only store the exception instance on Py stack in bytecode try block - vm: use MP_OBJ_FROM_PTR to cast a type to an object - modmicropython: add micropython.const, alias for identity function - objstr: remove unreachable function used only for terse error msgs - emitbc: remove/refactor unreachable code, to improve coverage - objfun: remove unnecessary check for viper fun with 5 or more args - objfun: use if instead of switch to check return value of VM execute - objset: use mp_check_self() to check args of set/frozenset methods - objset: ensure that use of frozenset.update raises an exception - compile: fix async-for/async-with to work with simpler exc on stack - scope: use lookup-table to determine a scope's simple name - scope: shrink scope_t struct by 1 machine word - scope: factor common code to find locals and close over them - compile: fix typo when checking for parse-node kind - argcheck: simplify if-chain so that the last one is the default - objbool: defer bool's unary op implementation to small int - objbool: make a slight simplification of bool constructor - modstruct: remove unreachable code, and add comment about CPy diff - add mp_raise_OSError(errno) helper function - objstringio: add readinto() method - modmicropython: add micropython.opt_level([value]) function - compile: remove unreachable code - mpz: in divmod, replace check for rhs!=0 with assert - mpz: use assert to verify mpz does not have a fixed digit buffer - factor duplicated function to calculate size of formatted int - objint: use size_t for arguments that measure bytes/sizes - compile: remove debugging code for compiler dispatch - lexer: remove unnecessary code, and unreachable code - vstr: combine vstr_new_size with vstr_new since they are rarely used - objdict: fix optimisation for allocating result in fromkeys - objdict: actually provide the key that failed in KeyError exception - use mp_raise_msg helper function where appropriate - add the module specified by -m to sys.modules as '__main__' extmod: - modussl_mbedtls: initial implementation of mbedTLS ussl module - uctypes: allow full 32-bit address range - modubinascii: fix crc32() function on 32-bit platforms - modussl_mbedtls: implement key= and cert= args to wrap_socket() - modussl_mbedtls: use 2-component include paths - machine_i2c: add clock stretching support - modussl_mbedtls: add server_hostname param for wrap_socket() - uzlib: add tinfgzip.c (gzip header parsing) from upstream - moduzlib: DecompIO: Add support for gzip-formatted streams - uzlib/: update uzlib to v2.0.3 - vfs_fat: add fat_vfs_statvfs(), reused from stmhal - machine_i2c: add support for the addrsize parameter in mem xfers - machine_spi: simplify SPI xfer function to only take one buf len - machine_spi: factor out software SPI code from esp8266 to extmod - machine_spi: use delay_half, not baudrate, for internal timing - machine_spi: add optional support for fast software SPI - vfs_fat: use mp_raise_OSError helper function - modlwip: use mp_raise_OSError helper function - use mp_raise_OSError helper function - vfs_fat_file: use MP_Exxx errno constants - uzlib: update to upstream v2.1 - machine_i2c: use writes not reads in i2c.scan() - vfs_fat: add file and directory checks for remove and rmdir - modujson: implement ujson.load() to load JSON from a stream - modujson: fix nanbox build - utime_mphal: factor out implementations in terms of mp_hal_* for reuse - utime_mphal: sleep_us/ms(): Don't wait on negative argument - modussl_mbedtls: add dummy setblocking() method lib: - interrupt_char: factor out typical Ctrl+C handling from esp8266 port - cmsis: move CMSIS headers to lib/ - cmsis: remove CMSIS-DSP headers, they are unused - cmsis: upgrade CMSIS-CORE to V4.30 - utils/pyexec: don't treat SystemExit as "forced exit" - utils/pyexec: allow behaviour of SystemExit to be configurable drivers: - dht: use mp_raise_OSError helper function tools: - update upip to 0.8, fixes IPv6 support - upgrade upip to 1.0, fully self-hosted release (without fallbacks), and uses stream gzip decompression (step towards support for limited-heap baremetal systems) - upgrade upip to 1.1.3, initial support for running on a baremetal, low-heap systems (like esp8266), using Python module interface tests: - pyb: add test for ExtInt when doing swint while disabled - pyb: update exp file for previously updated extint test - extmod/urandom: add urandom tests for error cases - basics: add errno1 test, to check basics of uerrno module - extmod: add test for machine.time_pulse_us() - struct1: test "l" specifier to improve coverage - array1: add tests for "l", "L" array types to improve coverage - get cmdline verbose tests running again - run-tests: add --via-mpy option to run test from precompiled code - uzlib_decompio_gz: test for DecompIO with gzip bitstream - basics: add test case for overflowing Py stack in try-finally - micropython: add tests for const names being replaced in parser - cmdline/cmd_showbc: fix test now that 1 value is stored on stack - extmod/vfs_fat_ramdisk: add test for VFS.statvfs() - float: add test for parsing a float from an empty string - basics: add test for set.difference_update with arg being itself - basics: add further tests for nonlocal scoping and closures - import: add test for compiling "import a.b as c" - basics: add test constructing a set from a non-trivial expression - basics: add test for printing OSError when errno is unknown - run-tests: disable cmdline/cmd_showbc test on Windows - extmod/btree1: checks for put, seq, string print and unsupported binary op - fix expected output of verbose cmdline test - extmod/uzlib: test adaptive huffman tree for tinflate coverage - improve coverage of struct with test for non-compliant behaviour - io/write_ext: add description comment - io/bytesio_ext: add test for readinto() - micropython: add test for micropython.opt_level() function - improve test coverage of py/compile.c - extmod/vfs_fat: test coverage for remove() and rmdir() - extmod: add test for ujson.load() - extmod/vfs_fat: replace asserts with prints and expected outputs - micropython: add tests for heap_lock, and emergency exceptions - cmdline: improve coverage test for printing bytecode - improve coverage of array, range, dict, slice, exc, unicode - add test to print full KeyError exc from failed dict lookup - run-tests: enable extmod/machine1.py on pyboard unix port: - fix build for when MICROPY_PY_SOCKET=0 - modjni: implement subscription for object arrays - modjni: add array() top-level function to create Java array - modjni: array(): Support creation of object arrays - enable btree module for coverage build - use mp_raise_OSError helper function - use common RAISE_ERRNO macro from mphalport.h windows port: - enable MICROPY_PY_UERRNO qemu-arm port: - enable lots of extmods and enable tests for them stmhal port: - lcd: de-assert chip select after completing SPI transmission - {accel,lcd}: use GPIO_{set,clear}_pin - extint: force 0 to 1 transition on swint() - boards: add pllvalues.py script to compute PLL values for sysclk - boards: for OLIMEX_E407, enable UART1 and fix I2C1 mapping - use attribute to avoid inlining - put common definitions from linker files to common.ld - remove STM32CubeF2 HAL files, they are unused/unsupported - modmachine: fix clearing of reset-cause flags - add virtual com port support for STM32L476DISC - remove CMSIS STM32F2xx files, they are unused/unsupported - spi: simplify spi_transfer function to take only one buf len arg - mphalport: implement mp_hal_pin_{input,output,write} - spi: make machine.SPI class conform to correct API - mphalport: fix mp_hal_pin_write to use correct pin_mask - spi: use software SPI if no periph id given, even if pins given - spi: enable use of fast software SPI - fix linker map for STM32L476 chips - usbdev: add OUT endpoint to HID interface - usb: add support to receive USB HID messages from host - usb: use correct ClassData structure for HID receive - usb: use real packet size (not maximum) in HID receive - fix ESPRUINO_PICO by adding ld scripts with correct flash size - mphalport: change pin obj type to const pointer, to avoid casts - moduos: implement total-number-of-blocks field in statvfs - disable network and usocket for ESPRUINO_PICO - enable machine.time_pulse_us() function - use mp_raise_OSError helper function - pybstdio: use size_t instead of mp_uint_t - modutime: refactor to use extmod/utime_mphal.c - implement machine.soft_reset() - enable str.center(), str.[r]partition() and builtin compile() cc3200 port: - add ssl_version argument to ssl.wrap_socket() esp8266 port: - Makefile: rename SCRIPTDIR to FROZEN_DIR for consistency with FROZEN_MPY_DIR - ets_alt_task: ets_post: Should return 0 on success, !0 - failure - esp_mphal: add tentative change to mp_hal_stdin_rx_chr() to wait IRQ - extend system microsecond counter to 64-bits; use in ticks_ms - add uos.statvfs() to get filesystem status - moduos: move stat/statvfs funcs to sit within #if VFS guard - modmachine: idle(): Return number of CPU cycles spent idling - main: put /lib before / in sys.path - modpybrtc.c: implement machine.RTC.alarm_left() - make PY_UHASHLIB_SHA1 config depend on PY_USSL and SSL_AXTLS - add FLASH_MODE,FLASH_SIZE options for make deploy target - use mp_raise_OSError helper function - make neopixel support configurable - mpconfigport: enable MICROPY_PY_BUILTINS_SLICE_ATTRS - enable sys.{stdin,stdout,stderr}.buffer for raw serial access - enable importing of precompiled .mpy files - enable micropython.alloc_emergency_exception_buf() zephyr port: - initial Zephyr RTOS port, MicroPython part - initial Zephyr RTOS port, Zephyr part - add zephyr_getchar module to handle console input - switch to microkernel, required for network to work in background - automatically derive ARCH - support extra make targets - Makefile: automatically derive target-specific CFLAGS - use recently added "make outputexports" Zephyr target - add README - enable stack checking and micropython.mem_info() - enable frozen modules support - main: execute main.py frozen module on boot, if available - zephyr_getchar: add support for Ctrl+C handling - add Ctrl+C handling - implement the help() function - add copyright blurbs README: - remove issue-stats badges, the service is no longer available - mention _thread module availability in select ports docs: - library/pyb.SPI: init(): describe "bits" argument - library/machine: update description of disable/enable IRQ funcs - uos: add uos.statvfs() documentation - wipy: correct deep sleep current figure - wipy: small doc fixes - reference: add constrained.rst doc travis: - abandon mingw32 in favour of mingw-w64 - run feature and coverage test for precompiled mpy files examples: - network/http_client*: use \r\n line-endings in request
2017-08-14 03:47:47 +01:00
qstr id;
pt_extract_id(pn, &id); // function name
scope->simple_name = id;
#else
scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]);
#endif
} else {
scope->simple_name = scope_simple_name_table[kind];
2013-10-04 19:53:11 +01:00
}
#if !MICROPY_USE_SMALL_HEAP_COMPILER
scope->raw_code = mp_emit_glue_new_raw_code();
#endif
scope->emit_options = emit_options;
scope->id_info_alloc = MICROPY_ALLOC_SCOPE_ID_INIT;
scope->id_info = m_new(id_info_t, scope->id_info_alloc);
2013-10-04 19:53:11 +01:00
return scope;
}
void scope_free(scope_t *scope) {
m_del(id_info_t, scope->id_info, scope->id_info_alloc);
m_del(scope_t, scope, 1);
}
id_info_t *scope_find_or_add_id(scope_t *scope, qstr qst, bool *added) {
id_info_t *id_info = scope_find(scope, qst);
if (id_info != NULL) {
*added = false;
return id_info;
2013-10-04 19:53:11 +01:00
}
// make sure we have enough memory
if (scope->id_info_len >= scope->id_info_alloc) {
scope->id_info = m_renew(id_info_t, scope->id_info, scope->id_info_alloc, scope->id_info_alloc + MICROPY_ALLOC_SCOPE_ID_INC);
scope->id_info_alloc += MICROPY_ALLOC_SCOPE_ID_INC;
2013-10-04 19:53:11 +01:00
}
// add new id to end of array of all ids; this seems to match CPython
// important thing is that function arguments are first, but that is
// handled by the compiler because it adds arguments before compiling the body
id_info = &scope->id_info[scope->id_info_len++];
2013-10-04 19:53:11 +01:00
id_info->kind = 0;
id_info->flags = 0;
id_info->local_num = 0;
id_info->qst = qst;
2013-10-04 19:53:11 +01:00
*added = true;
return id_info;
}
id_info_t *scope_find(scope_t *scope, qstr qst) {
for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
if (scope->id_info[i].qst == qst) {
2013-10-04 19:53:11 +01:00
return &scope->id_info[i];
}
}
return NULL;
}
id_info_t *scope_find_global(scope_t *scope, qstr qst) {
2013-10-04 19:53:11 +01:00
while (scope->parent != NULL) {
scope = scope->parent;
}
return scope_find(scope, qst);
2013-10-04 19:53:11 +01:00
}
STATIC void scope_close_over_in_parents(scope_t *scope, qstr qst) {
2013-10-04 19:53:11 +01:00
assert(scope->parent != NULL); // we should have at least 1 parent
for (scope_t *s = scope->parent;; s = s->parent) {
assert(s->parent != NULL); // we should not get to the outer scope
bool added;
id_info_t *id = scope_find_or_add_id(s, qst, &added);
if (added) {
// variable not previously declared in this scope, so declare it as free and keep searching parents
2013-10-04 19:53:11 +01:00
id->kind = ID_INFO_KIND_FREE;
} else {
// variable is declared in this scope, so finish
if (id->kind == ID_INFO_KIND_LOCAL) {
// variable local to this scope, close it over
id->kind = ID_INFO_KIND_CELL;
} else {
// ID_INFO_KIND_FREE: variable already closed over in a parent scope
// ID_INFO_KIND_CELL: variable already closed over in this scope
assert(id->kind == ID_INFO_KIND_FREE || id->kind == ID_INFO_KIND_CELL);
2013-10-04 19:53:11 +01:00
}
return;
}
}
}
void scope_find_local_and_close_over(scope_t *scope, id_info_t *id, qstr qst) {
if (scope->parent != NULL) {
for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
id_info_t *id2 = scope_find(s, qst);
if (id2 != NULL) {
if (id2->kind == ID_INFO_KIND_LOCAL || id2->kind == ID_INFO_KIND_CELL || id2->kind == ID_INFO_KIND_FREE) {
id->kind = ID_INFO_KIND_FREE;
scope_close_over_in_parents(scope, qst);
return;
}
break;
}
}
}
id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
2013-10-04 19:53:11 +01:00
}
#endif // MICROPY_ENABLE_COMPILER