2314 Commits

Author SHA1 Message Date
Damian Gryski f31b39775f reflect: add string <-> []rune conversions 2026-06-30 09:49:02 -07:00
Damian Gryski 6c30583062 reflect: add some more complex conversion tests 2026-06-30 09:49:02 -07:00
Damian Gryski d29e8e5f22 reflect: add converting complex 2026-06-30 09:49:02 -07:00
Damian Gryski a93640662e reflect: add Type.ConvertibleTo 2026-06-30 09:49:02 -07:00
deadprogram 3fb0a90854 machine: add I2C support for ESP32-C6
Generalize the shared esp32xx I2C implementation to also cover the
ESP32-C6 and add the chip-specific code.

Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-06-26 01:20:21 +02:00
Ayke van Laethem 134de98ba5 compiler, runtime: optimize zero-sized allocations
Instead of referring to an unused global, use a constant value. This is
safe even when using `-gc=none` (since no actual memory gets allocated)
which wasn't the case before. It should also reduce binary size by a few
bytes for most programs.
2026-06-25 21:09:43 +02:00
Ayke van Laethem 221ea6a54c runtime: move alloc_noheap call (pure refactor)
See the next commit, where this makes more sense.
2026-06-25 21:09:43 +02:00
deadprogram 5f66260c3a machine,targets: add minimal esp32c6 implementation
This adds a minimal esp32c6 implementation, currently only
supporting the examples/serial and examples/blinky1 programs.
It does correctly output the expected "Hello, World" via the
serial port, as well as blink the onboard LED.

In addition, it adds support for the PLIC based IRQ handling
as used on the ESP32C6 processor.

Some parts of this code are loosely based on PR #5252 and #5248

Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-06-25 19:30:24 +02:00
Ayke van Laethem cb311ed25e machine: optimize RTT initialization
This converts the heap allocation to a statically allocated string.
This is useful for me, since it reduces binary size and makes it
possible to use `-gc=none` (which would previously result in a linker
error).
2026-06-24 12:43:55 +02:00
Damian Gryski 6f686d12af runtime: don't access timer queue outside of lock
This was causing a flake on testing `context`.

Fixes #5469
2026-06-19 11:55:32 -07:00
Nia Waldvogel 11c2b76e29 runtime (gc.blocks): move objHeader to the end
This moves the objHeader from before an object body to after it.
On 32-bit systems with 16-byte alignment requirements (x86, ARM, RISC-V), we previously padded the header to a whole block.
This wastes up to 12 bytes, as on -gc=conservative the header is a single pointer.
With this change, no padding is required (beyond that from rounding the size up).

The "head" block in the metadata was moved to the end of the range to match the header location.
This changed the block loop directions throughout the GC logic.
The bit hacks used by sweep no longer work because there is no equivalent of addition that carries downwards.
However it is now possible to merge the sweep and free range list rebuild passes because their loop directions match.

There are two other places where we rebuilt the free ranges list: when initializing or growing the heap.
The former can be easily replaced with a single hardcoded range containing the entire heap.
In the latter case, I opted to only add the new space to the existing list.
These replacements allowed me to fully remove the buildFreeRanges function.
2026-06-16 13:37:35 -04:00
Marco Feltmann 6605b6e43f New Boards: Pimoroni Blinky 2350 and Badger 2350 (#5434)
* Prepares target for Piromoni Blinky 2350

* Adds target for Piromoni Badger 2350

* Updates submodules

* Removes redundant csv mention

* Revert "Updates submodules"

This reverts commit f05c2e1f2b.
2026-06-13 15:00:29 +02:00
rdon ec18e89365 machine: add GP aliases for waveshare-rp2040-zero 2026-06-13 13:51:46 +02:00
Konstantin Sharlaimov 2747027ef2 machine/rp2350: fix the ROM CS control for RP2350 ROM code
Remove support for A0 & A1 versions of RP2350
2026-06-12 21:07:20 +02:00
Konstantin Sharlaimov c33113ab5f fix(usb): implement endpoint stall for nRF52840.
Implement SetStallEPIn, SetStallEPOut, ClearStallEPIn, and ClearStallEPOut methods on the nRF52840 USBDevice struct using the hardware EPSTALL register to support MSC driver requirements. Also, correct the handleUSBIRQ loops to iterate over physical endpoint numbers rather than dynamic configuration entries to prevent missed or incorrectly routed endpoint interrupts.
2026-06-12 18:17:25 +02:00
Konstantin Sharlaimov eab43851ac fix(usb): support bidirectional endpoints on RP2 and SAMD21/51.
Remap CDC and MSC endpoints to share physical endpoint numbers. This change separates IN/OUT directional states for RP2 endpoints to prevent cross-talk, ensures SAMD21 and SAMD51 endpoint configuration merging does not overwrite bidirectionally shared registers, implements missing SAMD endpoint stall and clear methods, and corrects SAMD interrupt loop bounds to iterate over physical endpoint numbers rather than dynamic configuration entries.
2026-06-12 18:17:25 +02:00
Konstantin Sharlaimov c3f1832d9a refactor(usb): dynamic endpoint descriptor generation
Replace static endpoint variables with EndpointIN and EndpointOUT
functions. Allows flexible endpoint remapping across USB configs.

Map CDC, HID, MIDI and MSC USB devices to bidirectional endpoints consistently across different configurations.
2026-06-12 18:17:25 +02:00
Jake Bailey 2328d1e799 syscall: remove sliceHeader, use unsafe.SliceData more 2026-06-09 13:06:17 -04:00
あーるどん d9d19e812e usb/cdc: fix RP2 USB CDC TX race with cores scheduler (#5391)
* usb/cdc: serialize TX pump across cores

* usb/cdc: apply review suggestion

* Clarify TX pump loop comment

* clarify txActive comments

* usb/cdc: document txActive ownership and lost-wakeup recheck

* builder: update wioterminal size expectation

---------

Co-authored-by: rdon <you@example.com>
2026-06-08 13:58:19 +02:00
zowhoey cd364a9315 gba: add bios interrupt flags (#5445)
* gba: add bios interrupt flags

Adds a new GBA register for interrupt flags, the register is equivalent
to the IF register, but is intended for BIOS functions.

Acknowledging the interrupts with this register allows us to use BIOS
halt functions such as VBlankIntrWait and IntrWait, which we can use to
get rid of busy loops in the GBA code.

* fix formatting

---------

Co-authored-by: zoey <git@zoey.si>
2026-06-07 22:41:40 +02:00
sago35 ca584de84a machine/usb: support bidirectional endpoints by dynamic registration (#5447)
* machine/usb: support bidirectional endpoints by dynamic registration
2026-06-07 20:33:18 +02:00
sago35 aef70a5839 machine/usb: fix truncated USB string descriptor when host requests short maxLen (#5449)
* machine/usb: fix truncated USB string descriptor when host requests short maxLen
* fix binary size
2026-06-07 13:32:52 +02:00
Matthew Hiles e6e7250a47 UEFI Target groundwork: runtime: extract Windows PE globals scan helper (#5361)
* runtime: extract Windows PE globals scan helper
* Update src/runtime/os_windows_pe.go
* run goimports
2026-06-07 08:45:16 +02:00
Matthew Hiles 7b3dc19445 UEFI target groundwork: runtime: split baremetal memory setup (#5360)
* runtime: split baremetal memory setup
* add missing unsafe import
2026-06-06 16:11:20 +02:00
Konstantin Sharlaimov 8e36d2758b runtime: improve hardfault handler and stack reporting on Cortex-M
- Add reference to the current goroutine stack (PSP) when showing the hardfault info on Cortex-M.
2026-06-05 21:05:36 +02:00
zoey 7cbbfd6fc5 gba: add support for mGBA debugging
Implements `putchar` when `mgbadebug` build tag is set which outputs
text through the mGBA debugging output interface.

mGBA allows the games running in the emulator to output debug text the
process to do so is:
1. set 0x4FFF780 to value of 0xC0DE
2. write text in memory 0x4FFF600 to 0x4FFF700
3. set 0x4FFF700 to the desired log level value from 0x100 (fatal) to
0x104 (debug)

Once this is done mGBA will output the text in its logs view as well as
through stdout. (Setting the log level to fatal also produces a dialog
box with the text)

The text output in the CLI is prefixed with `[DEBUG] GBA Debug: ` so I
modified the regex used for address matching in panic messages to be
able to read the address when the output line doesn't start with
`panic`.
2026-06-05 17:55:03 +02:00
あーるどん e8bb38f7c9 rp2: allow SPI transmit-only without SDI (#5437)
* rp2: allow SPI transmit-only without SDI

* ci: rerun

---------

Co-authored-by: rdon <you@example.com>
2026-06-05 12:10:16 +02:00
Jake Bailey 469e2434fd compiler: document defer frame field dependency 2026-06-04 19:25:47 +02:00
Jake Bailey 8ffabbea64 runtime: add Windows vectored exception handler for recoverable panics
Register a vectored exception handler at startup so Windows hardware
exceptions can be translated into Go panics. Access violations become
nil pointer panics, and integer divide-by-zero exceptions become divide
by zero panics, allowing defer/recover to handle them like ordinary
runtime panics.
2026-06-04 19:25:47 +02:00
Jake Bailey 29b4c6723f runtime: make divide-by-zero and nil dereference panics recoverable
Modify the Unix signal handler to redirect execution to a Go sigpanic
function instead of printing an error and re-raising the signal.
The C signal handler modifies the ucontext to make the faulting
instruction appear to have called tinygo_sigpanic, which then calls
runtimePanic with the appropriate message.

Supported on all architectures TinyGo targets on Linux and Darwin:
x86_64, i386, aarch64, ARM, and MIPS. Also registers SIGFPE, which
was previously not handled at all.
2026-06-04 19:25:47 +02:00
Jake Bailey eed4afda63 compiler, runtime: make runtime panics recoverable
Emit fault checkpoints around compiler-generated runtime assertions so
runtimePanicAt can unwind through the existing defer/recover machinery
instead of aborting. This lets panics from bounds checks, type checks,
and other compiler-inserted runtime checks be recovered by deferred
functions.

Mark functions that call recover as noinline. Inlining such a function
into a deferred closure can make recover observe the wrong call context
and report success when it should return nil.

Addresses tinygo-org/tinygo issues 2759 and 3510.
2026-06-04 19:25:47 +02:00
Jake Bailey ce888e1042 compiler: store defer list head in defer frame 2026-06-04 19:25:47 +02:00
Kenneth Bell f5d0ec93ef esp32: configure uarts to specified pins and yield waiting for buffer 2026-06-01 14:37:09 +02:00
deadprogram 0a5875ab2e gba: add some const values for sound register configs to avoid magic numbers
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-05-29 16:02:52 +02:00
Juraj Michálek 0d6efc6429 Add new targets/esp32s3-box-3 (#5388)
* targets: add esp32s3-box-3

* add esp32s3-box3

* register esp32-s3-box-3 in tests
2026-05-28 22:47:08 +02:00
deadprogram 906a20d3bf stm32u5: configure system clock to 160 MHz
- Uses PLL1 to boost the system clock from the 4 MHz MSIS default to 160 MHz.
- Sets VOS to Range 1 (1.2V) and enables the EPOD booster for higher frequency support.
- Configures flash latency (4 wait states) and enables prefetch for 160 MHz operation.
- Updates CPU and APB timer frequencies in the machine package accordingly.
- Fixes LPUART baud rate divisor computation by using 64-bit arithmetic to prevent
overflow with the newly increased 160 MHz clock.

Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-05-28 15:18:31 +02:00
Ayke van Laethem 4dbb5dd4da nrf: avoid too high priority interrupt on SoftDevice
The highest priority interrupts are reserved by the SoftDevice, the
SoftDevice will refuse to start if any of them are used for other
purposes.
2026-05-28 11:36:38 +02:00
deadprogram 7d51044892 machine/stm32: fix UART transmission, baud rate, and interrupt handling
This fixes several issues in the STM32 UART implementation:
- `writeByte` now waits for the transmit register to be empty before writing, preventing data loss by avoiding overwriting the shift register.
- `flush` now correctly waits for the Transmission Complete (TC) flag.
- The interrupt handler now clears all error flags (ORE, NE, FE, PE) to prevent interrupt storms, rather than just ORE.
- Extracted `SetBaudRate` so it can be cleanly overridden by specific MCU families.

It also introduces specific fixes for the STM32U5 family:
- Enforces a minimum BRR divisor of 16 in `getBaudRateDivisor` to prevent undefined hardware behavior and CPU starvation.
- Overrides `SetBaudRate` for STM32U585 to momentarily disable the USART (UE=0) before updating the BRR register, which is read-only when enabled.
- Adds a readback after enabling the USART1 clock to ensure the clock is active before register access.

Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-05-28 08:53:40 +02:00
Ayke van Laethem 70a0dee8c8 all: use unsafe.SliceData where appropriate 2026-05-27 19:44:10 +02:00
Ayke van Laethem 7a5a50d949 runntime/interrupt.Checkpoint: add warning that this is hard to use
Realized this while looking through the code: there is no way to use
this safely when the scheduler is involved. It can only be used safely
with a `wfe` or similar (part of scheduler code).
2026-05-26 15:30:18 +02:00
Pat Whittingslow d348d8b4cf rp2040: fix -gc=leaking/none 2026-05-26 15:27:32 +02:00
Achille d01a932201 runtime,syscall,internal/poll,os: wasip1 poll_oneoff scheduler integration + net.FileListener (#5386)
* runtime,syscall,internal/poll,os: wasip1 poll_oneoff scheduler integration + net.FileListener

On wasip1 today every syscall.Read/Write blocks the entire wasm module — the
cooperative scheduler invokes poll_oneoff only for sleep/timer wakeups, and
there's no path from the net package to a working TCP server. This change
fixes both: it threads poll_oneoff through the scheduler's idle path so a
goroutine doing FD I/O parks instead of blocking the module, and it provides
enough internal/poll / os / syscall surface that upstream Go's
net.FileListener / net.FileConn works on a host-pre-opened TCP socket.

* runtime: keep scheduler_cooperative idle-wait calls direct

TestBinarySize/hifive1b/examples/echo regressed by 32 bytes after the
previous commit routed the scheduler's idle wait through a
schedulerIdleWait helper. The extra call frame + branch landed on every
non-wasip1 cooperative target, where the original direct sleepTicks /
waitForEvents calls compile to a single inlined call.
2026-05-26 10:12:55 +02:00
Ayke van Laethem 85d223c5e2 compiler: add //go:noheap pragma 2026-05-25 16:58:01 +02:00
Ayke van Laethem 6682e41ab4 reflectlite: remove stringHeader
It's usually a better idea to use unsafe.String and such instead.
2026-05-20 14:33:41 +02:00
Jake Bailey 18033ebc36 compiler, runtime, reflect: generate type-specific hash/equal (#5359)
* compiler, runtime, reflect: generate type-specific hash/equal for composite map keys

For map keys that are not trivially binary-comparable, the compiler now
generates type-specific hash and equal functions as LLVM IR instead of
going through the interface+reflection path. This covers comparable
types: strings, floats, complex numbers, interfaces, channels, and
composites containing any mix of these.

Previously, maps with composite keys containing strings or floats
converted the key to interface{}, hashed via reflection, and compared
through interface equality. Now the compiler walks struct fields and
array elements directly, dispatching to the right runtime helper for
each field type and storing keys at their actual type.

Struct keys are always handled field-by-field so padding bytes do not
affect equality or hashing. Blank fields are ignored, matching Go
equality. Generated hash/equal function names use canonical underlying
type structure so structurally identical key types can share generated
functions. Padding zeroing before map operations is no longer needed
because structs no longer use the binary key path.

Also fix reflect map iteration for interface-keyed maps: MapIter.Key
returns an interface Value for map[interface{}] keys instead of
unpacking to the concrete key kind.

* compiler: generate loops for array map key hash/equal

Previously, array key hash and equal functions were unrolled at compile
time, generating one block of IR per element. For large arrays like
[1000]int inside a struct with non-binary fields, this caused code
explosion.

Now, binary-element arrays dispatch directly to hash32/memequal for the
whole array. Non-binary-element arrays generate an LLVM IR loop. The
equal loop short-circuits on the first mismatch.

Small arrays are still unrolled instead of looping, keeping the simple
cases compact.

* reflect: fix at-runtime map issues from review, and more found locally

Maps created through reflect.MakeMap need hash/equal behavior that
matches compiler-created maps. Add hashmapMakeReflect for composite key
types, using runtime closures that reconstruct interface{} values from
raw key bytes and delegate to the interface hash and equality paths.

Interface-keyed maps are already stored as interface values, so use the
existing interface hash/equal helpers directly for those. This keeps
reflect insert, lookup, delete, and compiled lookup paths consistent.

Also fix addressable small values used as interface map keys or
interface map values. loadSmallValue puts small indirect values back in
the pointer-sized interface data field the same way valueInterfaceUnsafe
does.

* compiler, interp, reflect: fix pointer map literals; remove interface fallback

Package-level map literals with pointer keys (both *T and
unsafe.Pointer) crash the compiler: the interp pass panics when trying
to hash pointer data as raw bytes, because pointer values in the interp
memory model are symbolic identities that do not fit in a byte.

Fix this by setting a recoverable error flag instead of panicking. The
interp detects the error after each instruction and defers the map
insert to runtime init code, where real addresses are available for
hashing. This matches how the interp already handles other operations
it cannot evaluate at compile time.

With this fix, unsafe.Pointer can also be classified as a binary map
key, which was the last type requiring the interface-based fallback.
Since all comparable types now use either the binary or the
compiler-generated hash/equal path, remove the interface fallback from
the compiler and reflect packages.

* compiler, transform: always pass hash/equal function pointers to hashmapMakeGeneric

The compiler now always resolves the hash and equal functions at compile
time and passes them directly to hashmapMakeGeneric, instead of passing
an algorithm enum to hashmapMake and resolving at runtime. For string
keys, the runtime hashmapStringPtrHash/hashmapStringEqual functions are
referenced directly. For binary keys, hash32/memequal are referenced.

The old hashmapMake with alg enum is retained for reflect, which still
needs runtime resolution when creating maps dynamically.

The OptimizeMaps transform pass is updated to handle both hashmapMake
and hashmapMakeGeneric, and to recognize hashmapGenericSet in addition
to hashmapBinarySet and hashmapStringSet. The now-unused
hashmapCanGenerateHashEqual helper is removed.

* runtime: store large map keys and values indirectly

When a map key or value exceeds 128 bytes, the bucket now stores a
pointer to separately allocated memory instead of the data inline. This
matches Go's MapMaxKeyBytes/MapMaxElemBytes threshold and prevents
bucket sizes from exploding for large key/value types.

For example, map[[256]byte]int previously used 2128 bytes per bucket
(16 header + 256*8 keys + 8*8 values); now it uses 144 bytes per bucket
(16 header + 8*8 pointers + 8*8 values).

The indirection is fully encapsulated in the runtime via helper
functions. Store the computed key and value slot sizes on the hashmap so
all runtime and reflect paths use the same bucket layout, including
non-indirect keys and values.

Add big-key golden coverage and benchmarks. Make the benchmark vary
enough key bytes to exercise hashing.
2026-05-18 13:31:27 +02:00
Jake Bailey 4204f3d065 sync: add Map.CompareAndSwap and Map.CompareAndDelete
These methods were added in Go 1.20 but were missing from TinyGo's
sync.Map implementation, causing compilation failures for code that
uses them.

The implementation follows the same lock-based approach as the rest
of TinyGo's sync.Map.
2026-05-11 14:13:10 -07:00
Jake Bailey ddbd65beec builder, cgo, syscall: replace fixed-size array casts with unsafe.Slice
Several places used the (*[1 << N]T)(ptr)[:len:len] pattern to convert
C pointers to Go slices. This has a hardcoded size limit that can panic
if exceeded; RunTool hit this with >1024 linker arguments when many
files are embedded.

Replace all instances with unsafe.Slice, which handles any size.
2026-05-11 09:35:04 +02:00
deadprogram 8793dc37fa modules: update to 'net' package with roundtrip fix for js/wasm
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-04-27 12:32:43 +01:00
Jake Bailey 7ec9cfc61c testdata: re-add reflect.DeepEqual test case 2026-04-23 12:02:44 +01:00
deadprogram aa10b682e4 modules: update net to version that is backwards compatible with Go 1.25.x to fix #5332
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-04-22 16:45:20 +01:00