summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2024-08-01py/py.mk: Add SRC_USERMOD_LIB_ASM to include assembly files.George Hopkins
Introduce SRC_USERMOD_LIB_ASM to allow users to include assembly files as part of their user modules. It could be used to include optimized functions or outputs of other programming languages. Signed-off-by: George Hopkins <george-hopkins@null.net>
2024-07-25py/runtime: Fix self arg passed to classmethod when accessed via super.Damien George
Thanks to @AJMansfield for the original test case. Signed-off-by: Damien George <damien@micropython.org>
2024-07-25py/misc: Fix msvc and C++ compatibility.stijn
Use explicit casts to suppress warnings about implicit conversions, add a workaround for constant expression conditional, and make functions static inline (as is done in the rest of the codebase) to suppress 'warning C4505: unreferenced function with internal linkage has been removed'. (Follow up to fix commit 908ab1ceca15ee6fd0ef82ca4cba770a3ec41894) Signed-off-by: stijn <stijn@ignitron.net>
2024-07-25py/objtype: Validate super() arguments.stijn
This fixes various null dereferencing and out-of-bounds access because super_attr assumes the held obj is effectively an object of the held type, which is now verified. Fixes issue #12830. Signed-off-by: stijn <stijn@ignitron.net>
2024-07-25py/objtype: Avoid crash on calling members of uninitialized native type.Laurens Valk
When subclassing a native type, calling native members in `__init__` before `super().__init__()` has been called could cause a crash. In this situation, `self` in `mp_convert_member_lookup` is the `native_base_init_wrapper_obj`. The check added in this commit ensures that an `AttributeError` is raised before this happens, which is consistent with other failed lookups. Also fix a typo in a related comment. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2024-07-20extmod/modmachine: Use sys.exit as implementation of machine.soft_reset.Damien George
It does the same thing, raising `SystemExit`. Signed-off-by: Damien George <damien@micropython.org>
2024-07-19py/gc: Remove commented-out functions.Damien George
These are old, unused, and most of them no longer compile. The `gc_test()` function is superseded by the test suite. Signed-off-by: Damien George <damien@micropython.org>
2024-07-18py/sequence: Remove unused len argument from mp_seq_extract_slice.Damien George
Also put this function inside the `MICROPY_PY_BUILTINS_SLICE` guard, because it's only usable when that option is enabled. Signed-off-by: Damien George <damien@micropython.org>
2024-07-18py/lexer: Add static assert that token enum values all fit in a byte.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2024-07-18py: Remove 5 TODOs in emitbc, objrange and repl.Damien George
These TODOs don't need to be done: - Calling functions with keyword arguments is less common than without them, so adding an extra byte overhead to all calls regardless of whether they use keywords or not would overall increase generated bytecode size. - Restricting `range` objects to machine-sized ints has been adequate for a long time now, so no need to change that and make it more complicated and slower. - Printing spaces in tab completion does not need to be optimised. Signed-off-by: Damien George <damien@micropython.org>
2024-07-12extmod/machine_spi: Support firstbit=LSB for machine.SoftSPI.robert-hh
Being able to send data out in LSB format can be useful, and having support in the low-level driver is much better than requiring Python code to reorder the bits before sending them / after receiving them. In particular if the hardware does not support the LSB format (eg RP2040) then one needs to use the SoftSPI in LSB mode. For this change a default definition of `MICROPY_PY_MACHINE_SPI_MSB/_LSB` was added to `py/mpconfig.h`, making them available to all ports. The identical defines in `esp32/mpconfigport.h` were deleted. Resolves issues #5340, #11404. Signed-off-by: robert-hh <robert@hammelrath.com>
2024-07-11extmod/modmachine: Allow more than one argument to machine.freq().robert-hh
The limit is set by a `MICROPY_PY_MACHINE_FREQ_NUM_ARGS_MAX` define, which defaults to 1 and is set for stm32 to 4. For stm32 this fixes a regression introduced in commit e1ec6af654b1c5c4a973b6c6b029ee68bb92eb89 where the maximum number of arguments was changed from 4 to 1. Signed-off-by: robert-hh <robert@hammelrath.com>
2024-07-04all: Use new mp_obj_new_str_from_cstr() function.Jon Foster
Use new function mp_obj_new_str_from_cstr() where appropriate. It simplifies the code, and makes it smaller too. Signed-off-by: Jon Foster <jon@jon-foster.co.uk>
2024-07-04py/objstr: Add new mp_obj_new_str_from_cstr() helper function.Jon Foster
There were lots of places where this pattern was duplicated, to convert a standard C string to a MicroPython string: x = mp_obj_new_str(s, strlen(s)); This commit provides a simpler method that removes this code duplication: x = mp_obj_new_str_from_cstr(s); This gives clearer, and probably smaller, code. Signed-off-by: Jon Foster <jon@jon-foster.co.uk>
2024-07-01py/asmrv32: Do not use binary literals.Alessandro Gatti
As per discussion in #15347, non-standard binary literals have been removed in favour of their hexadecimal counterparts. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-07-01py/objint: Try to convert big-int back to small-int after binary op.Jim Mussared
Before this change, long/mpz ints propagated into all future calculations, even if their value could fit in a small-int object. With this change, the result of a big-int binary op will now be converted to a small-int object if the value fits in a small-int. For example, a relatively common operation like `x = a * b // c` where a,b,c all small ints would always result in a long/mpz int, even if it didn't need to, and then this would impact all future calculations with x. This adds +24 bytes on PYBV11 but avoids heap allocations and potential surprises (e.g. `big-big` is now a small `0`, and can safely be accessed with MP_OBJ_SMALL_INT_VALUE). Performance tests are unchanged on PYBV10, except for `bm_pidigits.py` which makes heavy use of big-ints and gains about 8% in speed. Unix coverage tests have been updated to cover mpz code that is now unreachable by normal Python code (removing the unreachable code would lead to some surprising gaps in the internal C functions and the functionality may be needed in the future, so it is kept because it has minimal overhead). This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2024-07-01py/asmrv32: Make some code sequences smaller.Alessandro Gatti
This commit changes a few code sequences to use more compressed opcodes where possible. The sequences in question are the ones that show up the most in the test suite and require the least amount of code changes, namely short offset loads from memory to RET/ARG registers, indirect calls through the function table, register-based jumps, locals' offset calculation, reg-is-null jumps, and register comparisons. There are no speed losses or gains from these changes, but there is an average 15-20% generated code size reduction. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-06-26py/objarray: Fix buffer overflow in case of memory allocation failure.Yoctopuce
If `array.append()` fails with an exception due to heap exhaustion, the next attempt to grow the buffer will cause a buffer overflow because the free slot count is increased before performing the allocation, and will stay as if the allocation succeeded. Signed-off-by: Yoctopuce <dev@yoctopuce.com>
2024-06-24py/objint: Fix int.to_bytes() buffer size checks.Angus Gratton
Fixes and improvements to `int.to_bytes()` are: - No longer overflows if byte size is 0 (closes #13041). - Raises OverflowError in any case where number won't fit into byte length (now matches CPython, previously MicroPython would return a truncated bytes object). - Document that `micropython int.to_bytes()` doesn't implement the optional signed kwarg, but will behave as if `signed=True` when the integer is negative (this is the current behaviour). Add tests for this also. Requires changes for small ints, MPZ large ints, and "long long" large ints. Adds a new set of unit tests for ints between 32 and 64 bits to increase coverage of "long long" large ints, which are otherwise untested. Tested on unix port (64 bit small ints, MPZ long ints) and Zephyr STM32WB board (32 bit small ints, long long large ints). This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-06-24py/misc: Move mp_clz and mp_ctz intrinsics into misc.h.Angus Gratton
Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-06-21py/emitnative: Fix native async with.Damien George
The code generating the entry to the finally handler of an async-with statement was simply wrong for the case of the native emitter. Among other things the layout of the stack was incorrect. This is fixed by this commit. The setup of the async-with finally handler is now put in a dedicated emit function, for both the bytecode and native emitters to implement in their own way (the bytecode emitter is unchanged, just factored to a function). With this fix all of the async-with tests now work when using the native emitter. Signed-off-by: Damien George <damien@micropython.org>
2024-06-21py/emitnative: Place thrown value in dedicated local variable.Damien George
A value thrown/injected into a native generator needs to be stored in a dedicated variable outside `nlr_buf_t`, following the `inject_exc` variable in `py/vm.c`. Signed-off-by: Damien George <damien@micropython.org>
2024-06-21py/emitndebug: Add native debug emitter.Damien George
This emitter prints out pseudo-machine instructions, instead of the usual output of the native emitter. It can be enabled on any port via `MICROPY_EMIT_NATIVE_DEBUG` (make sure other native emitters are disabled) but the easiest way to use it is with mpy-cross: $ mpy-cross -march=debug file.py Signed-off-by: Damien George <damien@micropython.org>
2024-06-21py/emitnative: Add more DEBUG_printf statements.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2024-06-21py/emitnative: Emit better load/store sequences for RISC-V RV32IMC.Alessandro Gatti
Selected load/store code sequences have been optimised for RV32IMC when the chance to use fewer and smaller opcodes was possible. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-06-21mpy-cross: Add RISC-V RV32IMC support in MPY files.Alessandro Gatti
MPY files can now hold generated RV32IMC native code. This can be accomplished by passing the `-march=rv32imc` flag to mpy-cross. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-06-21py/asmrv32: Add RISC-V RV32IMC native code emitter.Alessandro Gatti
This adds a native code generation backend for RISC-V RV32I CPUs, currently limited to the I, M, and C instruction sets. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-06-21py/objdeque: Fix deque type flags based on option settings.Dan Halbert
This fixes a minor issue in the changes made by 7dff38fdc190d7b731fad8319d2ae8aa13fde18a: the type flags for deque were meant to be conditionalized based on MICROPY_PY_COLLECTIONS_DEQUE_ITER, but the computed conditionalized value wasn't used. Signed-off-by: Dan Halbert <halbert@halwitz.org>
2024-06-20extmod/modasyncio: Add support for a callback on TaskQueue push.Damien George
Allows passing in a callback to `TaskQueue()` that is called when something is pushed on to the queue. Signed-off-by: Damien George <damien@micropython.org>
2024-06-06py/lexer: Support raw f-strings.Damien George
Support for raw str/bytes already exists, and extending that to raw f-strings is easy. It also reduces code size because it eliminates an error message. Signed-off-by: Damien George <damien@micropython.org>
2024-06-06py/lexer: Support concatenation of adjacent f-strings.Damien George
This is quite a simple and small change to support concatenation of adjacent f-strings, and improve compatibility with CPython. Signed-off-by: Damien George <damien@micropython.org>
2024-06-06py/nlrrv32: Add RISC-V RV32I NLR implementation.Alessandro Gatti
Add custom NLR support for 32 bits RISC-V RV32I targets. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-05-31all: Bump version to 1.24.0-preview.v1.24.0-previewDamien George
Signed-off-by: Damien George <damien@micropython.org>
2024-05-31all: Bump version to 1.23.0.v1.23.0Damien George
Signed-off-by: Damien George <damien@micropython.org>
2024-05-24py/dynruntime: Export mp_load_method_maybe and mp_arg_parse_all* funcs.Brian Pugh
Also define `mp_type_bytearray`. These all help to write native modules. Signed-off-by: Brian Pugh <bnp117@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
2024-05-23py/dynruntime: Add mp_obj_exception_init function to create C exception.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2024-04-25py/nlrthumb: Make non-Thumb2 long-jump workaround opt-in.J. Neuschäfer
Although the original motivation given for the workaround[1] is correct, nlr.o and nlrthumb.o are linked with a small enough distance that the problem does not occur, and the workaround isn't necessary. The distance between the b instruction and its target (nlr_push_tail) is just 64 bytes[2], well within the ±2046 byte range addressable by an unconditional branch instruction in Thumb mode. The workaround induces a relocation in the text section (textrel), which isn't supported everywhere, notably not on musl-libc[3], where it causes a crash on start-up. With the workaround removed, micropython works on an ARMv5T Linux system built with musl-libc. This commit changes nlrthumb.c to use a direct jump by default, but leaves the long jump workaround as an option for those cases where it's actually needed. [1]: commit dd376a239dc4f47b0ee7866810fcda151f3cf6dd Author: Damien George <damien.p.george@gmail.com> Date: Fri Sep 1 15:25:29 2017 +1000 py/nlrthumb: Get working again on standard Thumb arch (ie not Thumb2). "b" on Thumb might not be long enough for the jump to nlr_push_tail so it must be done indirectly. [2]: Excerpt from objdump -d micropython: 000095c4 <nlr_push_tail>: 95c4: b510 push {r4, lr} 95c6: 0004 movs r4, r0 95c8: f02d fd42 bl 37050 <mp_thread_get_state> 95cc: 6943 ldr r3, [r0, #20] 95ce: 6023 str r3, [r4, #0] 95d0: 6144 str r4, [r0, #20] 95d2: 2000 movs r0, #0 95d4: bd10 pop {r4, pc} 000095d6 <nlr_pop>: 95d6: b510 push {r4, lr} 95d8: f02d fd3a bl 37050 <mp_thread_get_state> 95dc: 6943 ldr r3, [r0, #20] 95de: 681b ldr r3, [r3, #0] 95e0: 6143 str r3, [r0, #20] 95e2: bd10 pop {r4, pc} 000095e4 <nlr_push>: 95e4: 60c4 str r4, [r0, #12] 95e6: 6105 str r5, [r0, #16] 95e8: 6146 str r6, [r0, #20] 95ea: 6187 str r7, [r0, #24] 95ec: 4641 mov r1, r8 95ee: 61c1 str r1, [r0, #28] 95f0: 4649 mov r1, r9 95f2: 6201 str r1, [r0, #32] 95f4: 4651 mov r1, sl 95f6: 6241 str r1, [r0, #36] @ 0x24 95f8: 4659 mov r1, fp 95fa: 6281 str r1, [r0, #40] @ 0x28 95fc: 4669 mov r1, sp 95fe: 62c1 str r1, [r0, #44] @ 0x2c 9600: 4671 mov r1, lr 9602: 6081 str r1, [r0, #8] 9604: e7de b.n 95c4 <nlr_push_tail> [3]: https://www.openwall.com/lists/musl/2020/09/25/4 Signed-off-by: J. Neuschäfer <j.ne@posteo.net>
2024-04-22py/objarray: Fix use-after-free if extending a bytearray from itself.Angus Gratton
Two cases, one assigning to a slice. Closes https://github.com/micropython/micropython/issues/13283 Second is extending a slice from itself, similar logic. In both cases the problem occurs when m_renew causes realloc to move the buffer, leaving a dangling pointer behind. There are more complex and hard to fix cases when either argument is a memoryview into the buffer, currently resizing to a new address breaks memoryviews into that object. Reproducing this bug and confirming the fix was done by running the unix port under valgrind with GC-aware extensions. Note in default configurations with GIL this bug exists but has no impact (the free buffer won't be reused while the function is still executing, and is no longer referenced after it returns). Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-04-22py/obj: Fix initialiser order in MP_DEFINE_CONST_OBJ_TYPE_NARGS_ macros.Vonasmic
This commit swaps the order of the `flags` and `name` struct initialisers for `mp_obj_type_t`, to fix an incompatibility with C++. The original order of the initialiser didn't match the definition of the type, and although that's still legal C, it's not legal C++. Signed-off-by: Vonasmic <kasarkal123@gmail.com>
2024-04-22py/objfun: Fix C++ compatibility with casting in inline functions.stijn
Explicit casts are needed. Fixes recent changes from 648a7578da21cc7ddb4046fc59891144e797b983 and 9400229766624e80db6a6f95af287a5542dc1b43. Signed-off-by: stijn <stijn@ignitron.net>
2024-03-28py/persistentcode: Bump .mpy sub-version to 6.3.Damien George
This is required because the .mpy native ABI was changed by the introduction of `mp_proto_fun_t`, see commits: - 416465d81e911b088836f4e7c37fac2bc0f67917 - 5e3006f1172d0eabbbefeb3268dfb942ec7cf9cd - e2ff00e81113d7a3f32f860652017644b5d68bf1 And three `mp_binary` functions were added to `mp_fun_table` in commit d2276f0d41c2fa66a224725fdb2411846c91cf1a. Signed-off-by: Damien George <damien@micropython.org>
2024-03-28py/dynruntime: Add mp_binary_get_size/get_val_array/set_val_array.Damien George
These are needed to read/write array.array objects, which is useful in native code to provide fast extensions that work with big arrays of data. Signed-off-by: Damien George <damien@micropython.org>
2024-03-26py/makeqstrdata.py: Ensure that scope names get low qstr values.Jim Mussared
Originally implemented in a patch file provided by @ironss-iotec. Fixes issue #14093. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2024-03-25py/nlr: Add "memory" to asm clobbers list in nlr_jump.Damien George
Newer versions of gcc (14 and up) have more sophisticated dead-code detection, and the asm clobbers list needs to contain "memory" to inform the compiler that the asm code actually does something. Tested that adding this "memory" line does not change the generated code on ARM Thumb2, x86-64 and Xtensa targets (using gcc 13.2). Fixes issue #14115. Signed-off-by: Damien George <damien@micropython.org>
2024-03-22py/compile: Add option to allow compiling top-level await.Damien George
Enabled by MICROPY_COMPILE_ALLOW_TOP_LEVEL_AWAIT. When enabled, this means that scope such as module-level functions and REPL statements can yield. The outer C code must then handle this yielded generator. Signed-off-by: Damien George <damien@micropython.org>
2024-03-22py/parse: Zero out dangling parse tree pointer to fix potential GC leak.Angus Gratton
This fixes a bug where a random Python object may become un-garbage-collectable until an enclosing Python file (compiled on device) finishes executing. Details: The mp_parse_tree_t structure is stored on the stack in top-level functions such as parse_compile_execute() in pyexec.c (and others). Although it quickly falls out of scope in these functions, it is usually still in the current stack frame when the compiled code executes. (Compiler dependent, but usually it's one stack push per function.) This means if any Python object happens to allocate at the same address as the (freed) root parse tree chunk, it's un-garbage-collectable as there's a (dangling) pointer up the stack referencing this same address. As reported by @GitHubsSilverBullet here: https://github.com/orgs/micropython/discussions/14116#discussioncomment-8837214 This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-03-20py/binary: Support half-float 'e' format in struct pack/unpack.Matthias Urlichs
This commit implements the 'e' half-float format: 10-bit mantissa, 5-bit exponent. It uses native _Float16 if supported by the compiler, otherwise uses custom bitshifting encoding/decoding routines. Signed-off-by: Matthias Urlichs <matthias@urlichs.de> Signed-off-by: Damien George <damien@micropython.org>
2024-03-19py/emitnative: Implement viper unary ops positive, negative and invert.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2024-03-19py/asmxtensa: Optimise asm_xtensa_mov_reg_i32_optimised() for tiny ints.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2024-03-19py/asm: Add ASM_NOT_REG and ASM_NEG_REG macros for unary ops.Damien George
ASM_NOT_REG is optional, it can be synthesised by xor(reg, -1). ASM_NEG_REG can also be synthesised with a subtraction, but most architectures have a dedicated instruction for it. Signed-off-by: Damien George <damien@micropython.org>