summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2024-02-20py/builtinevex: Fix setting globals for native functions in compile().Damien George
Signed-off-by: Damien George <damien@micropython.org>
2024-02-20py/emitglue: Remove n_pos_args from DEBUG_printf.Damien George
This argument was renamed in 39bf055d23be4b0f761af115773c3db1074fc2dd. Signed-off-by: Damien George <damien@micropython.org>
2024-02-20py/objfun: Inline mp_obj_code_get_name() into mp_obj_fun_get_name().Damien George
The former is static and does not need to be a separate function. Signed-off-by: Damien George <damien@micropython.org>
2024-02-20py/objfun: Support __name__ on native functions and generators.Damien George
This is now easy to support, since the first machine-word of a native function tells how to find the prelude, from which the function name can be extracted in the same way as for bytecode. Signed-off-by: Damien George <damien@micropython.org>
2024-02-20py/emitnative: Simplify layout and loading of native function prelude.Damien George
Now native functions and native generators have similar behaviour: the first machine-word of their code is an index to get to the prelude. This simplifies the handling of these types of functions, and also reduces the size of the emitted native machine code by no longer requiring special code at the start of the function to load a pointer to the prelude. Signed-off-by: Damien George <damien@micropython.org>
2024-02-20py/objfun: Split viper fun type out to separate mp_type_fun_viper type.Damien George
Viper functions are quite different to native functions and benefit from being a separate type. For example, viper functions don't have a bytecode- style prelude, and don't support generators or default arguments. Signed-off-by: Damien George <damien@micropython.org>
2024-02-20py/objfun: Make mp_obj_new_fun_native/mp_obj_new_fun_asm static-inline.Damien George
To reduce code size, since they are only used once by py/emitglue.c. Signed-off-by: Damien George <damien@micropython.org>
2024-02-20py/misc: Remove m_new_obj[_var]_with_finaliser macros.Damien George
They are no longer used. The new `mp_obj_malloc_with_finaliser()` macros should be used instead, which force the setting of the `base.type` field. And there's always `m_malloc_with_finaliser()` if needed. Signed-off-by: Damien George <damien@micropython.org>
2024-02-20py/obj: Introduce mp_obj_malloc_with_finaliser to allocate and set type.Damien George
Following 709e8328d9812a2e02da6fba65a03f9a873d60a2. Using this helps to reduce code size. And it ensure that the type is always set as soon as the object is allocated, which is important for the GC to function correctly. Signed-off-by: Damien George <damien@micropython.org>
2024-02-19py/obj: Change sizeof to offsetof in mp_obj_malloc_var macro.Damien George
Following b6a977848407a4ced45d118cf926bd915cc89dfb, to properly calculate the size of the variable-length allocation. Signed-off-by: Damien George <damien@micropython.org>
2024-02-19py/makeversionhdr.py: Reinstate MICROPY_GIT_HASH in mpversion.h.Damien George
MICROPY_GIT_HASH was removed in 69e34b6b6bdf45bc1111777c46839a8b5fcb30bd but it is useful for, and used by, third-party code to tell which hash of MicroPython is used. Signed-off-by: Damien George <damien@micropython.org>
2024-02-16py/emitglue: Include fun_data_len in mp_raw_code_t only when saving.Damien George
Reduces the size of mp_raw_code_t in the case when MICROPY_DEBUG_PRINTERS is enabled. Signed-off-by: Damien George <damien@micropython.org>
2024-02-16tools/mpy-tool.py: Skip generating frozen mp_raw_code_t when possible.Damien George
This reduces frozen code size by using the bytecode directly as the `mp_proto_fun_t`. Signed-off-by: Damien George <damien@micropython.org>
2024-02-16py/emitglue: Introduce mp_proto_fun_t as a more general mp_raw_code_t.Damien George
Allows bytecode itself to be used instead of an mp_raw_code_t in the simple and common cases of a bytecode function without any children. This can be used to further reduce frozen code size, and has the potential to optimise other areas like importing. Signed-off-by: Damien George <damien@micropython.org>
2024-02-16py/emitglue: Simplify mp_raw_code_t's kind and scope_flags members.Damien George
To simplify their access and reduce code size. The `scope_flags` member is only ever used to determine if a function is a generator or not, so make it reflect that fact as a bool type. Signed-off-by: Damien George <damien@micropython.org>
2024-02-16py/emitglue: Provide a truncated mp_raw_code_t for non-asm code.Damien George
The `asm_n_pos_args` and `asm_type_sig` members of `mp_raw_code_t` are only used for raw codes of type MP_CODE_NATIVE_ASM, which are rare, for example in frozen code. So using a truncated `mp_raw_code_t` in these cases helps to reduce frozen code size on targets that have MICROPY_EMIT_INLINE_ASM enabled. With this, change in firmware size of RPI_PICO builds is -648. Signed-off-by: Damien George <damien@micropython.org>
2024-02-16py/emitglue: Reorder and resize members of mp_raw_code_t.Damien George
The mp_raw_code_t struct has been reordered and some members resized. The `n_pos_args` member is renamed to `asm_n_pos_args`, and `type_sig` renamed to `asm_type_sig` to indicate that these are used only for the inline-asm emitters. These two members are also grouped together in the struct. The justifications for resizing the members are: - `fun_data_len` can be 32-bits without issue - `n_children` is already limited to 16-bits by `mp_emit_common_t::ct_cur_child` - `scope_flags` is already limited to 16-bits by `scope_t::scope_flags` - `prelude_offset` is already limited to 16-bits by the argument to `mp_emit_glue_assign_native()` - it's reasonable to limit the maximim number of inline-asm arguments to 12 (24 bits for `asm_type_sig` divided by 2) This change helps to reduce frozen code size (and in some cases RAM usage) in the following cases: - 64-bit targets - builds with MICROPY_PY_SYS_SETTRACE enabled - builds with MICROPY_EMIT_MACHINE_CODE enabled but MICROPY_EMIT_INLINE_ASM disabled With this change, unix 64-bit builds are -4080 bytes in size. Bare-metal ports like rp2 are unchanged (because mp_raw_code_t is still 32 bytes on those 32-bit targets). Signed-off-by: Damien George <damien@micropython.org>
2024-02-07extmod/modvfs: Add new "vfs" module with mount/umount and Vfs classes.Damien George
They have been moved from the "os" module. Signed-off-by: Damien George <damien@micropython.org>
2024-01-31py/compile: Fix potential Py-stack overflow in try-finally with return.Damien George
If a return is executed within the try block of a try-finally then the return value is stored on the top of the Python stack during the execution of the finally block. In this case the Python stack is one larger than it normally would be in the finally block. Prior to this commit, the compiler was not taking this case into account and could have a Python stack overflow if the Python stack used by the finally block was more than that used elsewhere in the function. In such a scenario the last argument of the function would be clobbered by the top-most temporary value used in the deepest Python expression/statement. This commit fixes that case by making sure enough Python stack is allocated to the function. Fixes issue #13562. Signed-off-by: Damien George <damien@micropython.org>
2024-01-30py/builtinimport: Simplify calls to stat_path().Matthias Urlichs
stat_path is only called with stringified vstr_t objects. Thus, pulling the stringification into the function replaces three function calls with one, saving a few bytes. Signed-off-by: Matthias Urlichs <matthias@urlichs.de>
2024-01-25py/mpconfig: Disable qstr hashing at minimum feature level.Jim Mussared
This will apply to bare-arm and minimal, as well as the minimal unix variant. Change the default to MICROPY_QSTR_BYTES_IN_HASH=1 for the CORE,BASIC levels, 2 for >=EXTRA. Removes explicit setting of MICROPY_QSTR_BYTES_IN_HASH==1 in ports that don't set the feature level (because 1 is implied by the default level, CORE). Applies to cc3200, pic16bt, powerpc. Removes explicit setting for nRF (which sets feature level). Also for samd, which sets CORE for d21 and FULL for d51. This means that d21 is unchanged with MICROPY_QSTR_BYTES_IN_HASH==1, but d51 now moves from 1 to 2 (roughly adds 1kiB). The only remaining port which explicitly set bytes-in-hash is rp2 because it's high-flash (hence CORE level) but lowish-SRAM, so it's worthwhile saving the RAM for runtime qstrs. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2024-01-25py/qstr: Add support for MICROPY_QSTR_BYTES_IN_HASH=0.Jim Mussared
This disables using qstr hashes altogether, which saves RAM and flash (two bytes per interned string on a typical build) as well as code size. On PYBV11 this is worth over 3k flash. qstr comparison will now be done just by length then data. This affects qstr_find_strn although this has a negligible performance impact as, for a given comparison, the length and first character will ~usually be different anyway. String hashing (e.g. builtin `hash()` and map.c) now need to compute the hash dynamically, and for the map case this does come at a performance cost. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2024-01-24ports: Fix handling of paths containing spaces in Makefiles.Iksas
Make can't handle paths with spaces, see https://savannah.gnu.org/bugs/?712 The following workarounds exist: - When using make's built-in functions: - Use relative paths wherever possible to avoid spaces in the first place. - All spaces in paths can be escaped with backslashes; quotes don't work. - Some users use the shell to temporarily rename directories, or to create symlinks without spaces. - When using make to pass commands to the system's shell, enclose paths in quotes. While make will still interpret quoted strings with spaces as multiple words, the system's shell will correctly parse the resulting command. This commit contains the following fixes: - In ports/stm32/mboot/Makefile: Use relative paths to avoid spaces when using built-in functions. - In all other files: Use quotes to enclose paths when make is used to call shell functions. All changes have been tested with a directory containing spaces. Signed-off-by: Iksas <iksas@mailbox.org>
2024-01-17py/py.mk: Remove extra build dir created for frozen_content.iabdalkader
This was originally needed because the .c --> .o rule is: $(BUILD)/%.o: %.c and because the generated frozen_content.c is inside build-FOO, it must therefore generate build-FOO/build-FOO/frozen_content.o. But 2eda5138701d6a7d36f8d8e3700d136b3c1161b7 added a new build rule for pins.c that can also be used for frozen_content.c. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
2024-01-02all: Bump version to 1.23.0-preview.v1.23.0-previewDamien George
Signed-off-by: Damien George <damien@micropython.org>
2023-12-27all: Bump version to 1.22.0.v1.22.0Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-12-22py/mkrules.mk: Fix dependency file generation for compiler wrappers.Peter Züger
When compiling with distcc, it does not understand the -MD flag on its own. This fixes the interaction by explicitly adding the -MF option. The error in distcc is described here under "Problems with gcc -MD": https://www.distcc.org/faq.html Signed-off-by: Peter Züger <zueger.peter@icloud.com>
2023-12-22ports: Fix sys.stdout.buffer.write() return value.Maarten van der Schrieck
MicroPython code may rely on the return value of sys.stdout.buffer.write() to reflect the number of bytes actually written. While in most scenarios a write() operation is successful, there are cases where it fails, leading to data loss. This problem arises because, currently, write() merely returns the number of bytes it was supposed to write, without indication of failure. One scenario where write() might fail, is where USB is used and the receiving end doesn't read quickly enough to empty the receive buffer. In that case, write() on the MicroPython side can timeout, resulting in the loss of data without any indication, a behavior observed notably in communication between a Pi Pico as a client and a Linux host using the ACM driver. A complex issue arises with mp_hal_stdout_tx_strn() when it involves multiple outputs, such as USB, dupterm and hardware UART. The challenge is in handling cases where writing to one output is successful, but another fails, either fully or partially. This patch implements the following solution: mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible destinations for that data, and returns the minimum successful write length. The implementation of this is complicated by several factors: - multiple outputs may be enabled or disabled at compiled time - multiple outputs may be enabled or disabled at runtime - mp_os_dupterm_tx_strn() is one such output, optionally containing multiple additional outputs - each of these outputs may or may not be able to report success - each of these outputs may or may not be able to report partial writes As a result, there's no single strategy that fits all ports, necessitating unique logic for each instance of mp_hal_stdout_tx_strn(). Note that addressing sys.stdout.write() is more complex due to its data modification process ("cooked" output), and it remains unchanged in this patch. Developers who are concerned about accurate return values from write operations should use sys.stdout.buffer.write(). This patch might disrupt some existing code, but it's also expected to resolve issues, considering that the peculiar return value behavior of sys.stdout.buffer.write() is not well-documented and likely not widely known. Therefore, it's improbable that much existing code relies on the previous behavior. Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
2023-12-19py/gc: Improve calculation of new heap size in split-heap-auto mode.Damien George
There are two main changes here to improve the calculation of the size of the next heap area when automatically expanding the heap: - Compute the existing total size by counting the total number of GC blocks, and then using that to compute the corresponding number of bytes. - Round the bytes value up to the nearest multiple of BYTES_PER_BLOCK. This makes the calculation slightly simpler and more accurate, and makes sure that, in the case of growing from one area to two areas, the number of bytes allocated from the system for the second area is the same as the first. For example on esp32 with an initial area size of 65536 bytes, the subsequent allocation is also 65536 bytes. Previously it was a number that was not even a multiple of 2. Signed-off-by: Damien George <damien@micropython.org>
2023-12-14py/makeqstrdefs.py: Stop generating temporary intermediate file.Trent Piepho
In "cat" mode, output was written to a file named "out", then moved to the location of the real output file. There was no reason for this. While makeqstrdefs.py does make an effort to not update the timestamp on an existing output file that has not changed, the intermediate "out" file isn't part of the that process. Signed-off-by: Trent Piepho <tpiepho@gmail.com>
2023-12-15py/makeqstrdefs.py: Don't skip output for stale hash file.Trent Piepho
In "cat" mode a "$output_file.hash" file is checked to see if the hash of the new output is the same as the existing, and if so the output file isn't updated. However, it's possible that the output file has been deleted but the hash file has not. In this case the output file is not created. Change the logic so that a hash file is considered stale if there is no output file and still create the output. Signed-off-by: Trent Piepho <tpiepho@gmail.com>
2023-12-15py/mkrules.mk: List hash files as byproducts.Trent Piepho
These are produced by the "cat" command to makeqstrdefs.py, to allow it to not update unchanged files. cmake doesn't know about them and so they are not removed on a "clean". This triggered a bug in makeqstrdefs.py where it would not recreate a deleted output file (which is removed by clean) if a stale hash file with a valid hash still existed. Listing them as byproducts will cause them to be deleted on clean. Signed-off-by: Trent Piepho <tpiepho@gmail.com>
2023-12-15py/modsys: Implement optional sys.intern.stijn
Signed-off-by: stijn <stijn@ignitron.net>
2023-12-08py: Add port-agnostic inline functions for event handling.Angus Gratton
These are intended to replace MICROPY_EVENT_POLL_HOOK and MICROPY_EVENT_POLL_HOOK_FAST, which are insufficient for tickless ports. This implementation is along the lines suggested here: https://github.com/micropython/micropython/issues/12925#issuecomment-1803038430 Currently any usage of these functions expands to use the existing hook macros, but this can be switched over port by port. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2023-12-01py/mphal: Move configuration of ATOMIC_SECTION macros to mphal.h.Damien George
MICROPY_BEGIN_ATOMIC_SECTION/MICROPY_END_ATOMIC_SECTION belong more to the MicroPython HAL rather than build configuration settings, so move their default configuration to py/mphal.h, and require all users of these macros to include py/mphal.h (here, py/objexcept.c and py/scheduler.c). This helps ports separate configuration from their HAL implementations, and can improve build times (because mpconfig.h is included everywhere, whereas mphal.h is not). Signed-off-by: Damien George <damien@micropython.org>
2023-11-28py/modbuiltins: Share vstr_add_char's implementation of utf8 encoding.Jeff Epler
This saves ~84 bytes on trinket m0, and saves 112 bytes on PYBV10. Signed-off-by: Jeff Epler <jepler@gmail.com>
2023-11-28py/mkrules: Add support for custom manifest variables.Jim Mussared
This allows e.g. a board (or make command line) to set MICROPY_MANIFEST_MY_VARIABLE = path/to/somewhere set(MICROPY_MANIFEST_MY_VARIABLE path/to/somewhere) and then in the manifest.py they can query this, e.g. via include("$(MY_VARIABLE)/path/manifest.py") Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-11-21py/objslice: Validate that the argument to indices() is an integer.Damien George
Otherwise passing in a non-integer can lead to an invalid memory access. Thanks to Junwha Hong and Wonil Jang @S2Lab, UNIST for finding the issue. Fixes issue #13007. Signed-off-by: Damien George <damien@micropython.org>
2023-11-17py/obj: Fix mp_obj_is_type compilation with C++.stijn
Fixes issue #12951. Signed-off-by: stijn <stijn@ignitron.net>
2023-11-09extmod/vfs_reader: Add file ioctl to set read buffer size.Andrew Leech
Can be used to speed up importing a file from a vfs based filesystem. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
2023-11-07windows: Use the MicroPython logo as application icon.stijn
Add a .ico file with common icon image size, created from vector-logo-2.png, and embed it into the resulting executable. Signed-off-by: stijn <stijn@ignitron.net>
2023-11-07py/qstr: Special case qstr_find_strn for empty string.Jim Mussared
This handles the case where an empty bytes/bytearray/str could pass in NULL as the str argument (with length zero). This would result in UB in strncmp. Even though our bare-metal implementation of strncmp handles this, best to avoid it for when we're using system strncmp. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-11-03py/misc: Change sizeof to offsetof for variable-length alloc.Jim Mussared
This fixes the case where e.g. struct foo_t { mp_obj_t x; uint16_t y; char buf[]; }; will have `sizeof(struct foo_t)==8`, but `offsetof(struct foo_t, buf)==6`. When computing the size to allocate for `m_new_obj_var` we need to use offsetof to avoid over-allocating. This is important especially when it might cause it to spill over into another GC block. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-11-03py/lexer: Change token position for new lines.Mathieu Serandour
Set the position of new line tokens as the end of the preceding line instead of the beginning of the next line. This is done by first moving the pointer to the end of the current line to skip any whitespace, record the position for the token, then finaly skip any other line and whitespace. The previous behavior was to skip every new line and whitespace, including the indent of the next line, before recording the token position. (Note that both lex->emit_dent and lex->nested_bracket_level equal 0 if had_physical_newline == true, which allows simplifying the if-logic for MP_TOKEN_NEWLINE.) And update the cmd_parsetree.py test expected output, because the position of the new-line token has changed. Fixes issue #12792. Signed-off-by: Mathieu Serandour <mathieu.serandour@numworks.fr>
2023-11-03py/runtime: Remove declaration of function from inside function.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-11-03py/mkrules.mk: Add rule for compiling auto-generated source files.Jim Mussared
This prevents each port Makefile from having to add an explicit rule for `build-BOARD/pins_BOARD.c`. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-11-01esp32: Use better build settings for ESP32-C3.Alessandro Gatti
ESP32-C3 is not Xtensa-based, so build settings are now tailored a bit better following that fact. ESP-IDF 5.x already adds architecture-specific modules by itself so there is no need to specify either the `xtensa` or the `riscv` module in the build settings. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2023-10-30py/qstr: Add support for sorted qstr pools.Jim Mussared
This provides a significant performance boost for qstr_find_strn, which is called a lot during parsing and loading of .mpy files, as well as interning of string objects (which happens in most string methods that return new strings). Also adds comments to explain the "static" qstrs. These are part of the .mpy ABI and avoid needing to duplicate string data for QSTRs known to already be in the firmware. The static pool isn't currently sorted, but in the future we could either split the static pool into the sorted regions, or in the next .mpy version just sort them. Based on initial work done by @amirgon in #6896. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-27py/asm{arm,thumb,x64,x86,xtensa}: Remove unused macros.Alessandro Gatti
`ASM_MOV_REG_IMM_FIX_U16` and `ASM_MOV_REG_IMM_FIX_WORD` are no longer used anywhere in the code. See discussion in #12771. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2023-10-27py/mkrules.mk: Add MICROPY_PREVIEW_VERSION_2.Jim Mussared
This provides a way to enable features and changes slated for MicroPython 2.x, by running `make MICROPY_PREVIEW_VERSION_2=1`. Also supported for the cmake ports (except Zephyr). This is an alternative to having a 2.x development branch (or equivalently, keeping a 1.x release branch). Any feature or change that needs to be "hidden" until 2.x can use this flag (either in the Makefile or the preprocessor). A good example is changing function arguments or other public API features, in particular to aid in improving consistency between ports. When `MICROPY_PREVIEW_VERSION_2` is enabled, the REPL banner is amended to say "MicroPython (with v2.0 preview) vX.Y.Z", and sys.implementation gets a new field `_v2` set to `True`. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>