4792 Commits

Author SHA1 Message Date
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
Jake Bailey 3a0d72457b interp: fix partial store aliasing with previously loaded values
The optimization to skip cloning the destination object on partial
stores when the buffer is already owned by the current memory view
mutated obj.buffer.buf in place. The previous v.buf clone only handled
the case where the store value itself aliased that buffer; it did not
cover the more common case where an earlier partial load had returned
a slice into the same buffer. The in-place store would then corrupt
the previously loaded value, breaking code such as cloudflare/bn256
where the gfP arithmetic loads, computes, and stores within the same
global during package initialization.

Fix this in load instead: when the source object is owned by the
current memory view, copy the loaded slice so the returned value is
independent of the live buffer. The v.buf clone in store is no longer
needed and is removed.

Updates the regression test added in the previous commit to reflect
the corrected behavior.
2026-06-01 11:27:56 +02:00
Jake Bailey c980e48ad4 interp: add regression test for partial store aliasing
Adds a third init function to the store testdata: an initial partial
store puts the buffer into the current memory view, then a partial load
is followed by another partial store at the same offset, and finally the
loaded value is written to a separate global.

The expected output captures the current (incorrect) behavior of the
in-place partial store optimization: the loaded value gets corrupted by
the subsequent in-place store. The next commit fixes this.
2026-06-01 11:27:56 +02:00
Jake Bailey f5bd750a4d interp: avoid repeated object clones on partial stores
memoryView.store cloned the entire destination object on every partial
store. Compiling a program that pulls in golang.org/x/text/collate hit
this hard: its generated tables are initialized one element at a time,
and each element store copied the whole global, making interp quadratic
in the table size.

Switch to copy-on-first-write per memory view: clone the object the
first time this view writes to it, then mutate that private copy in
place on later partial stores. Rollback is unaffected because revert
still discards the view's objects wholesale.

Two extra safety tweaks fall out of this:

  - Full overwrites now clone the incoming value, so the stored buffer
    can never alias state held by the caller.
  - Partial in-place stores snapshot the source buffer, so a store
    whose source is a load from the same object (overlapping or not)
    still sees the pre-store bytes.

Add an interp golden test exercising both the overlapping load/store
case and a cross-object aliased load/store case.
2026-06-01 11:27:56 +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
Ayke van Laethem 54ecf74261 sizediff: look for actual 'tinygo build' line 2026-05-29 10:32:27 +02:00
deadprogram 9b83acffe6 ci: increase runner size for compat build
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-05-29 09:10:14 +02:00
deadprogram 67aa15e92e fix: move esp32s3 based device to inside of XTENSA build section
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-05-29 09:10:14 +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 1bd3ade825 ci: increase size of runners for Linux, Windows, and macOS (Intel)
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-05-28 20:55:18 +02:00
Ayke van Laethem fb8ed8fde8 all: update golang.org/x/tools
This has the following effects:

  * It fixes issue #5414.
  * It bumps the minimum Go version to Go 1.24 (from 1.23).

I looked, and there doesn't seem to be a version of golang.org/x/tools
that supports the improved `new` builtin without also bumping the
minimum Go version.
2026-05-28 19:32:08 +02:00
Ayke van Laethem c698eeacba cli: add basic probe-rs support
OpenOCD can be somewhat outdated. `probe-rs` is a more modern tool
written in Rust with many interesting features, like built-in RTT
logging support.

This commit just adds basic support, to be able to flash devices with
probe-rs and debug them.
2026-05-28 18:05:24 +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 1be377c47d ci: add check for compatibility with older Go/LLVM versions 2026-05-28 11:46:25 +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
Ayke van Laethem 997974376d ci: re-add fmt-check in CI
This was removed in https://github.com/tinygo-org/tinygo/pull/5406.
2026-05-28 11:36:05 +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
Patricio Whittingslow 37c080083b upgrade espflasher 2026-05-28 00:30:46 +02:00
Ayke van Laethem 70a0dee8c8 all: use unsafe.SliceData where appropriate 2026-05-27 19:44:10 +02:00
Ayke van Laethem de83d67364 loader: skip object resolution
This is deprecated, see: https://pkg.go.dev/go/ast#Object
Removing it might speed up TinyGo a little bit.
2026-05-27 10:30:27 +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
Patricio Whittingslow 443de9fe1c skip flag on windows :( 2026-05-26 15:21:00 +02:00
Patricio Whittingslow c72f1d5e62 GNUmakefile: disable DWARF compression for CGo objects 2026-05-26 15:21:00 +02:00
Ayke van Laethem ca8fcc9479 cli: clarify runtime.alloc linker error with -gc=none
This should help people figure out the actual problem if they run into
this.
2026-05-26 14:28:52 +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 7b486e398a compiler: clean up pointer pack code
This means runtime.alloc isn't allocated every time but instead only
when it is actually needed. It also just cleans up the compiler code a
little bit, and makes the next commit a bit nicer.
2026-05-25 16:58:01 +02:00
felipegenef da69f18e91 fix targets/wasm_exec.js: move unsigned coercions from helpers to top-level import functions 2026-05-21 14:39:17 -07:00
felipegenef 6971b8a69e fix: coerce memory offsets to unsigned before typed array and DataView access 2026-05-21 14:39:17 -07: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 89d9e33bca interp: bail out of loops that iterate too many times (#5395)
* interp: bail out of loops that iterate too many times

The existing loop guard (errLoopUnrolled) only fires when a loop body
emits runtime instructions. Loops that are fully evaluable at compile
time, such as inserting thousands of entries into a map, were not
caught and could hang the compiler.

Add a per-basic-block iteration counter that triggers a recoverable
error (errLoopTooLong) when any block is entered more than 1000 times
in a single function call. This defers the init function to runtime,
which is the same behavior as other interp bailouts.

Profiling showed that 83% of CPU time was spent in GC, caused by
allocation pressure from the interp memory cloning on each map
mutation. The iteration limit avoids this entirely by bailing out
before the quadratic cost becomes significant.

Performance on the reproducer from #2090 (map init with strconv.Itoa):

    entries  before       after
    5,000    7.4s         2.1s
    10,000   17.5s        2.1s
    20,000   48.0s        2.8s
    65,536   >180s (OOM)  3.2s

* Add -interp-loop-limit
2026-05-17 20:42:27 +02:00
deadprogram e80e7e5c10 ci: remove CircleCI configuration
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-05-17 14:36:32 +01:00
pedramktb 6233915ecf targets/wasm_exec: add runtime.getRandomData to gojs imports 2026-05-17 10:25:05 +01:00
Joel Wetzell fd8e0bd70b monitor flag on flash now respects port 2026-05-17 07:14:56 +01:00
Ayke van Laethem 45e2863e9f loader: avoid race condition when loading package list
I was seeing bad error messages... but only some of the time. So I went
digging. Eventually I traced it to this one bug, probably introduced in
https://github.com/tinygo-org/tinygo/pull/5290.

This is not just a visual issue, this is actually a bug in TinyGo that
will result in non-reproducible binaries and random behavior
differences.
2026-05-16 17:24:58 +01: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 1a1506ef79 builder,loader: fix -ldflags -X not overriding variables with default values
When a string variable already had a source-level default value, the -ldflags
"-X" flag was silently ignored and the variable remained the default value
at runtime.

Fix by stripping the InitOrder entry for each -X variable before
LoadSSA() is called. With no entry in InitOrder, go/ssa emits no init
store, so the global stays zero-valued in the IR. makeGlobalsModule
still injects the actual -X values at final link time.

The -X values remain out of the per-package build cache with only the
variable names appearing in the cache key.

Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-04-28 11:06:48 +01: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
Ayke van Laethem e9d78a78ea compiler: re-enable generics test
This seems to have been removed by accident, but it still seems useful
to me.
2026-04-24 17:48:48 +01:00
Jake Bailey 7ec9cfc61c testdata: re-add reflect.DeepEqual test case 2026-04-23 12:02:44 +01:00
Ayke van Laethem 294cf28e43 all: support LLVM 19 and LLVM 20 on Fedora 43
The system LLVM paths on Fedora 43 are slightly different from other
systems like Debian, so adding those paths in this patch.
2026-04-23 10:22:56 +01:00
deadprogram 2f7a66e6d3 version: update to 0.42.0-dev for next dev cycle
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-04-23 09:22:47 +01:00
deadprogram a4f9c9d1b6 Release 0.41.1
Signed-off-by: deadprogram <ron@hybridgroup.com>
v0.41.1
2026-04-22 18:22:49 +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
deadprogram eafbe4ee69 machine/esp32c3: correct pin interrupt setup call that was overlooked from #5320
Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-04-22 15:53:17 +01:00