Commit Graph

16004 Commits

Author SHA1 Message Date
Alessandro Gatti
bb6a4669b2 py/asmrv32: Do not use binary literals.
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-01 22:44:02 +02:00
Jim Mussared
557d31ed2c py/objint: Try to convert big-int back to small-int after binary op.
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-01 13:52:59 +10:00
Alessandro Gatti
0600e4f273 py/asmrv32: Make some code sequences smaller.
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-07-01 11:36:03 +10:00
Phil Howard
0e261443be rp2: Replace CMSIS funcs with Pico SDK equivalents.
Pico SDK defines `__dsb()` and `__sev()` so use those instead of the CMSIS
equivalents.  This matches the use of `__wfi()` in lieu of `__WFI()` and
lowers the dependency on CMSIS headers.

And then, move the include of "RP2040.h" from the widely-included
"mphalport.h" to specific source files that need this header, to keep its
inclusion contained.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
2024-07-01 11:27:42 +10:00
Damien George
0dd25a369e rp2/boards/WEACTSTUDIO: Fix variant names in board.json.
It looks like the variants for this board were never being built properly,
because the auto-build system used the variant name from `board.json` which
did not match the variant names in the original `mpconfigboard.mk`.  Eg
`FLASH_2MB` in `board.json` but `FLASH_2M` in `mpconfigboard.mk`.

This mistake is apparent since 5dff78f38e,
which made it a build error to specify an invalid variant.

Fix this by using the correct variant names in `board.json`.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-28 22:48:46 +10:00
Damien George
95c19e05ff webassembly/objjsproxy: Lookup attributes without testing they exist.
In JavaScript when accessing an attribute such as `obj.attr` a value of
`undefined` is returned if the attribute does not exist.  This is unlike
Python semantics where an `AttributeError` is raised.  Furthermore, in some
cases in JavaScript (eg a Proxy instance) `attr in obj` can return false
yet `obj.attr` is still valid and returns something other than `undefined`.
So the source of truth for whether a JavaScript attribute exists is to just
right away attempt `obj.attr`.

To more closely match these JavaScript semantics when proxying a JavaScript
object through to Python, change the attribute lookup logic on a `JsProxy`
so that it immediately attempts `obj.attr` instead of first testing if the
attribute exists via `attr in obj`.

This allows JavaScript objects which dynamically create attributes to work
correctly on the Python side, with both `obj.attr` and `obj["attr"]`.  Note
that `obj["attr"]` already works in all cases because it immediately does
the subscript access without first testing if the attribute exists.

As a benefit, this new behaviour matches the Pyodide behaviour.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-28 11:40:24 +10:00
Damien George
5dff78f38e rp2: Rework board variant support to require mpconfigvariant file.
Following how the board variants now work in the esp32 port.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-28 11:25:07 +10:00
Damien George
81b1bfcfef stm32: Rework board variant support to require mpconfigvariant file.
Following how the board variants now work in the esp8266 port.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-28 11:24:46 +10:00
Damien George
daa948fe05 esp8266: Rework board variant support to require mpconfigvariant file.
Following how esp32 has been reworked, each variant now has a corresponding
`mpconfigvariant_VARIANT.mk` file associated with it.  The base variant
also has a `mpconfigvariant.mk` file because it has options that none of
the other variants use.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-28 11:24:08 +10:00
Damien George
43ebbec0c5 esp32: Rework board variant support to require mpconfigvariant file.
This commit reworks board variants on the esp32 port.  It's a simple change
that moves the board variant configuration from an "if" statement within
`mpconfigboard.cmake` into separate files for each variant, with the name
of the variant encoded in the filename: `mpconfigvariant_VARIANT.cmake`.

Optionally, the base variant can have its own options in
`mpconfigvariant.cmake` (this is an optional file, but all other variants
of the base must have a corresponding mpconfigvariant file).

There are two benefits to this:
- The build system now gives an error if the variant that you specified
  doesn't exist (because the mpconfigvariant file must exist with the
  variant name you specify).
- No more error-prone if-logic needed in the .cmake files.

The way to build a variant is unchanged, still via:

    $ make BOARD_VARIANT=VARIANT

Signed-off-by: Damien George <damien@micropython.org>
2024-06-28 11:23:55 +10:00
Jos Verlinde
3af1425be7 tools/mpremote: Fix mpremote mip install with multiple lib in sys.path.
This is a fix for an algorithmic error in mpremote mip, that throws an
error due to a '\n' used in the concatenation and split when there is more
than one lib path in `sys.path`.

Signed-off-by: Jos Verlinde <Jos.Verlinde@microsoft.com>
2024-06-28 11:07:01 +10:00
Yoctopuce
b4213c9c92 tools/mpy-tool.py: Implement freezing of long-long ints.
Allow inclusion of large integer constants in frozen files using long-long
representation (mpy-cross option -mlongint-impl=longlong).

Signed-off-by: Yoctopuce <dev@yoctopuce.com>
2024-06-27 17:53:31 +10:00
Damien George
706a4b4477 tools/ci.sh: Build an stm32 board with -O2 enabled.
To test building with full optimisations.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-27 12:00:20 +10:00
Damien George
096adca0c8 stm32/pin: Decrease machine_pin_obj_t.pin width from 5 to 4 bits.
Compiling using arm-none-eabi-gcc 14.1.0 with -O2 will give warnings about
possible overflow indexing extint arrays, such as `pyb_extint_callback`.
This is due to `machine_pin_obj_t.pin` having a bit-width of 5, and so a
possible value up to 31, which is usually larger than
`PYB_EXTI_NUM_VECTORS`.

To fix this, change `machine_pin_obj_t.pin` to a bit-width of 4.  Only 4
bits are needed for ST MCUs, which have up to 16 pins per port.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-27 12:00:20 +10:00
Damien George
74f52374e4 extmod/extmod.mk: Disable maybe-uninitialized warnings in libm_dbl.
These warnings are emitted by arm-none-eabi-gcc 14.1.0 with -O2 enabled.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-27 11:17:59 +10:00
Damien George
880f7bc040 shared/tinyusb/mp_usbd_cdc: Fix short CDC TX timeouts.
The `mp_event_wait_ms()` function may return earlier than the requested
timeout, and if that happens repeatedly (eg due to lots of USB data and
IRQs) then the loop waiting for CDC TX FIFO space to become available may
exit much earlier than MICROPY_HW_USB_CDC_TX_TIMEOUT, even when there is
no space.

Fix this by using `mp_hal_ticks_ms()` to compute a more accurate timeout.

The `basics/int_big_mul.py` test fails on RPI_PICO without this fix.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-26 14:50:42 +10:00
Angus Gratton
0c28a5ab06 github: Add Pull Request template.
Provides pull request submitters with contributor documentation, and
prompts them to provide relevant information about testing, and how they
came to implement this change.

Sections are deliberately small so they don't crowd out the GitHub Pull
Request description text field.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-06-26 14:47:13 +10:00
tharuka
206dc09456 README: Clean up a few bits of grammar.
The word "Select" could be confusing in this context, eg it could be
misunderstood as the `select` module.

Signed-off-by: tharuka <78165134+tharuka-pavith@users.noreply.github.com>
2024-06-26 14:40:39 +10:00
Yoctopuce
3d93fed0aa py/objarray: Fix buffer overflow in case of memory allocation failure.
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-26 14:12:57 +10:00
Peter Harper
9111fa5831 shared/tinyusb/mp_usbd_runtime: Fix pointer comparison in assert.
Addresses build warning "comparison of distinct pointer types lacks a
cast".

Fixes issue #15276.

Signed-off-by: Peter Harper <peter.harper@raspberrypi.com>
2024-06-26 13:57:45 +10:00
Peter Harper
e35f13a22d rp2/pendsv: Fix variable typo in assert so it compiles.
Fixes issue #15276.

Signed-off-by: Peter Harper <peter.harper@raspberrypi.com>
2024-06-26 13:57:39 +10:00
Angus Gratton
f60c71d131 rp2: Don't disable USB if going to DORMANT mode.
In this mode, XOSC is stopped so can't really keep
the USB PLL enabled.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-06-25 16:48:39 +10:00
Angus Gratton
068d9bf2cf rp2: Fix USB PLL glitch during wake from light sleep.
Follow-up to a84c7a0ed9, this commit works most of the time but has an
intermittent bug where USB doesn't resume as expected after waking from
light sleep.

Turns out waking calls clocks_init() which will re-initialise the USB PLL.
Most of the time this is OK but occasionally it seems like the clock
glitches the USB peripheral and it stops working until the next hard reset.

Adds a machine.lightsleep() test that consistently hangs in the first
two dozen iterations on rp2 without this fix. Passed over 100 times in a
row with this fix.

The test is currently rp2-only as it seems similar lightsleep USB issues
exist on other ports (both pyboard and ESP32-S3 native USB don't send any
data to the host after waking, until they receive something from the host
first.)

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-06-25 16:25:19 +10:00
Angus Gratton
5dcffb53ab rp2/clocks_extra: Implement custom clocks_init function.
Adapts pico-sdk clocks_init() into clocks_init_optional_usb() which takes
an argument to initialise USB clocks or not.

To avoid a code size increase the SDK clocks_init() function is linker
wrapped to become clocks_init_optional_usb(true).

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-06-25 16:25:06 +10:00
Angus Gratton
cfa55b4ca1 rp2: Fix recursive atomic sections when core1 is active.
mp_thread_begin_atomic_section() is expected to be recursive (i.e. for
nested machine.disable_irq() calls, or if Python code calls disable_irq()
and then the Python runtime calls mp_handle_pending() which also enters an
atomic section to check the scheduler state).

On rp2 when not using core1 the atomic sections are recursive.

However when core1 was active (i.e. _thread) then there was a bug that
caused the core to live-lock if an atomic section recursed.

Adds a test case specifically for mutual exclusion and recursive atomic
sections when using two threads. Without this fix the test immediately
hangs on rp2.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-06-25 11:01:25 +10:00
Angus Gratton
908ab1ceca py/objint: Fix int.to_bytes() buffer size checks.
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-24 14:07:00 +10:00
Angus Gratton
d933210d96 py/misc: Move mp_clz and mp_ctz intrinsics into misc.h.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-06-24 14:06:54 +10:00
Damien George
cebc9b0ae2 tools/mpremote: Fix absolute path usage in remote mounted VFS.
Prior to this fix the current working path in the remote VFS would always
be prepended to the requested path to get the full path, even if the
requested path was already absolute, ie starting with "/".

So `os.chdir("/remote/dir1")` would set the working path to "/dir1/", and
a subsequent call with an absolute path like `os.listdir("/remote/dir2")`
would try to list the directory "/dir1/dir2/".

Fixes issue #15308.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-21 16:46:49 +10:00
Damien George
0619f261a8 tests/basics: Add tests to test repeated throw into the same generator.
Signed-off-by: Damien George <damien@micropython.org>
2024-06-21 16:23:08 +10:00
Damien George
038125be79 py/emitnative: Fix native async with.
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-21 16:21:33 +10:00
Damien George
a19214d897 py/emitnative: Place thrown value in dedicated local variable.
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-21 16:21:29 +10:00
Damien George
9dbc787ce8 py/emitndebug: Add native debug emitter.
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-21 16:21:27 +10:00
Damien George
e2ae03e979 py/emitnative: Add more DEBUG_printf statements.
Signed-off-by: Damien George <damien@micropython.org>
2024-06-21 16:06:02 +10:00
Alessandro Gatti
c1882e4866 qemu-riscv: Enable native code generation by default.
This turns on the native RV32IMC code generator for the QEMU-based
RISC-V port, and removes tests that relies on native code generation
from the exclusion list (ie enables these tests).

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-06-21 15:07:03 +10:00
Alessandro Gatti
3dd1130f6d py/emitnative: Emit better load/store sequences for RISC-V RV32IMC.
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-21 15:06:52 +10:00
Alessandro Gatti
99f5659cf5 mpy-cross: Add RISC-V RV32IMC support in MPY files.
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-21 15:06:29 +10:00
Alessandro Gatti
8338f66352 py/asmrv32: Add RISC-V RV32IMC native code emitter.
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-21 15:06:07 +10:00
Angus Gratton
5a778ebc37 tests/thread: Re-enable GC before stress_schedule test ends.
Otherwise GC stays disabled (not re-enabled by soft reset) and later test
runs fail with MemoryError.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-06-21 14:45:59 +10:00
Damien George
407464348d tests/cpydiff: Remove deque difference test.
Because `collections.deque` is now a built-in type in MicroPython.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-21 14:35:43 +10:00
Dan Halbert
5040b13dd4 py/objdeque: Fix deque type flags based on option settings.
This fixes a minor issue in the changes made by
7dff38fdc1: 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-21 14:27:33 +10:00
Damien George
88513d1226 webassembly/api: Allow specifying the pystack size.
This allows increasing the Python recursion depth if needed.

Also increase the default to 2k words.  There is enough RAM in the
browser/node context for this to be increased, and having a larger pystack
allows more complex code to run without hitting the limit.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-20 00:26:08 +10:00
Damien George
13195a678d webassembly/asyncio: Schedule run loop when tasks are pushed to queue.
In the webassembly port there is no asyncio run loop running at the top
level.  Instead the Python asyncio run loop is scheduled through setTimeout
and run by the outer JavaScript event loop.  Because tasks can become
runable from an external (to Python) event (eg a JavaScript callback), the
run loop must be scheduled whenever a task is pushed to the asyncio task
queue, otherwise tasks may be waiting forever on the queue.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-20 00:11:54 +10:00
Damien George
8ac9c8f392 extmod/modasyncio: Add support for a callback on TaskQueue push.
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-20 00:11:05 +10:00
Damien George
e9c898cb33 webassembly/asyncio: Support top-level await of asyncio Task and Event.
This change allows doing a top-level await on an asyncio primitive like
Task and Event.

This feature enables a better interaction and synchronisation between
JavaScript and Python, because `api.runPythonAsync` can now be used (called
from JavaScript) to await on the completion of asyncio primitives.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-18 22:23:16 +10:00
Damien George
a053e63914 webassembly/objjsproxy: Implement proxying of JS iterable protocol.
This allows Python to iterate over JavaScript objects that provide
Symbol.iterator.

Signed-off-by: Damien George <damien@micropython.org>
2024-06-18 22:14:34 +10:00
Maureen Helm
2b0e64beaf zephyr: Link MicroPython with the Zephyr kernel library.
Unlike most other Zephyr libraries, libkernel.a is not built as a
whole-archive.

This change also fixes a linker error observed on nucleo_wb55rg while
preparing an upgrade to Zephyr v3.5.0, caused by an undefined reference to
`z_impl_k_busy_wait`.

Signed-off-by: Maureen Helm <maureen.helm@analog.com>
2024-06-18 16:19:07 +10:00
Jason Kridner
c211c05291 zephyr/modsocket: Add socket.recvfrom method.
Signed-off-by: Jason Kridner <jkridner@beagleboard.org>
2024-06-18 16:00:21 +10:00
Jason Kridner
0466560b12 zephyr/modzsensor: Add additional sensor type constants.
Signed-off-by: Jason Kridner <jkridner@beagleboard.org>
2024-06-18 15:54:25 +10:00
Alessandro Gatti
411d66586c extmod/modplatform: Add picolibc to the recognised libcs list.
This adds picolibc to the list of the recognised libc options.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-06-17 13:12:54 +10:00
Alessandro Gatti
1b10cb843c github/workflows: Add qemu-riscv port to CI.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-06-17 12:06:09 +10:00