MicroGoose: how feasible is Goose on a microcontroller?¶
Short answer: feasible on 32-bit MCUs, and more practical than expected. Stock Goose v2026.4 programs, compiled by the unmodified Goose compiler, run unchanged on an ESP32-S3 (Xtensa, ESP-IDF/FreeRTOS) and an STM32H743 (Cortex-M7, FreeRTOS). Every stock sample that fits a board prints exactly what the PC prints: 24 of 24 on the S3, 21 of 21 on the WROOM-32 and 22 of 22 on the H7 (see Conformance). Getting there took a few hundred lines of C shim and HAL per port, a small build script, two source-level fixes for 32-bit targets, and a workaround for a real Xtensa GCC calling-convention bug that this project found. An 8-bit Arduino Nano is out of reach (see below).
Tested on:
- ESP32-S3 (QFN56) rev v0.2, 16 MB flash, 8 MB octal PSRAM, CH343 USB-UART, ESP-IDF 6.1.0 (picolibc), xtensa-esp-elf-gcc 15.2.0.
- STM32H743VIT6 (DevEBox), 2 MB flash, ST-Link V2 (SWD), STM32Cube HAL, newlib, arm-none-eabi-gcc via PlatformIO ststm32.
- ATmega328P (Arduino Nano clone): build probe only, avr-gcc 7.3.0.
All builds go through PlatformIO 6.2.
Why Goose suits embedded work¶
| Goose property | What it means on an MCU |
|---|---|
| No heap, no GC, no RC | No allocator pauses and no fragmentation. All dynamic data lives on compiler-managed bump-pointer data stacks. |
| Compiler counts data stacks statically | Memory per program is known at build time (gs_rt_init(.., stacks, regions, ..)). microgoose prints it. |
| Whole-program compile to one C file | Any C toolchain can be a backend. No LLVM target, no Goose port needed. |
extern fn with plain C shapes |
HAL bindings are one line each, with direct calls and no marshalling. |
| Threads share nothing, typed queues | Maps cleanly onto FreeRTOS tasks and pthreads on the S3's two cores. |
What it took¶
1. The runtime compiles almost unchanged¶
goose --emit-runtime writes about 200 KB of C. Its non-Windows path targets
POSIX. Against ESP-IDF's libc, only these were missing:
| Runtime needs | MicroGoose provides (compat/, mg_compat.c) |
|---|---|
mmap/mprotect/munmap/madvise for data stacks |
Heap allocation (internal RAM, or PSRAM via Kconfig) plus a guard zone |
sigaction, siginfo_t.si_addr, sigaltstack |
Stand-in types; the handler is kept but never delivered |
dlopen/dlsym (extern "lib" blocks) |
Stubs that report "not supported" |
_longjmp, newlib's log2 macro |
Two #defines in a force-included prefix |
pause, _exit/_Exit |
Park the task instead of rebooting; print the exit code |
The x86 parts (cpuid, SSE byte search) are already behind #ifdefs. The
FreeRTOS pthread layer, clock_gettime, nanosleep, stdio and 64-bit atomics
all come from ESP-IDF.
2. Data stacks vs. no MMU: the real design problem¶
On a desktop, a Goose push is an unchecked bump-pointer write
(*(gs_i64 *)t25 = v; t25 += 8;). Overflow protection is entirely the OS
guard page behind each data stack, reserved as 2 GB of address space by
default. The S3 has no page protection over internal SRAM. MicroGoose:
- shrinks each reservation to fit RAM (default 16 KB,
--reserve), which is safe because the compiler's elided checks assume at most the reservation; - puts a 64-byte guard zone after each stack, filled with a canary and covered by one of the CPU's two hardware data watchpoints while they last, so the first store past the end traps at once;
- re-checks every canary every 100 ms and at exit, to catch overruns of stacks without a watchpoint.
This is a best-effort replacement, not an equivalent. Stacks beyond the first two per core only get the after-the-fact canary check. A real fix would need either (a) a bounds check per push, which Goose would have to emit as an option, or (b) data stacks in PSRAM, where the S3's MMU does map 64 KB pages, so unmapped guard pages are possible.
3. A compiler bug: Xtensa GCC passes some 64-bit arguments in the wrong registers¶
The first board run printed pushed 4596609819930001408 squares instead of
100. Root cause, reduced to a few lines:
typedef long long i64u __attribute__((aligned(1))); // how Goose declares gs_i64
extern void take(int a, long long v); // callee reads v from a4:a5
void bad(void) { i64u x = get(); take(1, (long long)x); }
// xtensa-esp-elf-gcc -O2: mov a12,a11 ; mov a11,a10 -> v passed in a11:a12 (wrong)
// with plain `long long x`: mov a12,a10 ; mov a13,a11 -> v passed in a12:a13 (right)
The windowed Xtensa ABI passes 64-bit arguments in an even register pair. GCC's
Xtensa backend chooses the pair from TYPE_ALIGN of the argument's type. At
-O1 and above the cast to long long is dropped as a "useless conversion",
so an aligned(1) typedef (and also a packed-struct field after SRA) shifts
the argument by one register. The callee reads garbage. double is affected
the same way. -O0/-Og hide it. The same class of bug was fixed for ARM long ago
by using the type's main variant. The RISC-V ESP32s (C3/C6/P4) are not
affected, because RV32 doesn't pair-align arguments.
Goose declares every scalar this way, to allow unaligned packed data, so nearly any 64-bit value crossing a non-inlined call was at risk.
Workaround (microgoose/core.py:xtensa_abi_fix, component CMake flags):
- 64-bit value typedefs (
gs_i64,gs_u64,gs_f64) get natural alignment. Only pointers to them keepaligned(1)(gs_i64u *), so loads and stores of unaligned packed data still use byte accesses. This is safe because every Goose struct is under#pragma pack(1). -fno-tree-sra -fno-ipa-sra, so SRA can't turn packed 64-bit fields back into under-aligned scalars.
This is worth reporting to Espressif (esp-gcc) and upstream GCC (xtensa
backend, xtensa_function_arg_1). A one-line TYPE_MAIN_VARIANT fix would
remove the need for the rewrite.
Results¶
Four boards were run, using two ports. The ESP32-S3 and the classic ESP32 (WROOM-32 and WROVER-E) run on ESP-IDF/FreeRTOS, and the STM32H743 runs on FreeRTOS from STM32Cube. All four use the same Goose compiler output and the same generated runtime, after the same two source fixes.
| ESP32-S3 | ESP32 WROOM-32 | ESP32 WROVER-E | STM32H743 | |
|---|---|---|---|---|
| CPU | 2× LX7 @ 240 MHz | 2× LX6 @ 240 MHz | 2× LX6 @ 240 MHz | 1× Cortex-M7 @ 400 MHz |
| Free heap at start | 8.4 MB (incl. 8 MB PSRAM) | 295 KB | 4.4 MB (incl. 4 MB PSRAM) | 487 KB |
| Stock samples identical to PC (of those that fit) | 24 / 24 | 21 / 21 | 21 / 21³ | 22 / 22 |
| App image (program + runtime + OS) | 214–279 KB | 200–253 KB | 203–260 KB³ | 38–78 KB |
| Data stack overflow caught at 16384 B | yes (watchpoint) | yes (watchpoint) | (same image as WROOM) | yes (MPU fault) |
| Sieve, primes < 200000 | 38 ms (octal PSRAM) | doesn't fit² | 720 ms (quad PSRAM) | 12 ms (SRAM) |
| Goose threads | yes, both cores (Mandelbrot 1.95× faster on 2) | yes, both cores | yes | yes, one core |
| Console | UART0 (CH343) | UART0 (CP2102) | UART0 (CH340) | SEGGER RTT over ST-Link |
¹ 03_strings needs 16 data stacks. 16 × 16 KB doesn't fit beside ESP-IDF in
the WROOM's 295 KB, so it passes with --reserve 8K. Everything else passes
at the default 16 KB.
² The sieve needs one 200 KB stack, and the runtime reserves two of that size.
³ The WROVER-E runs the WROOM's images, and tests/conformance.py skips the
same four samples there. Its run is on main at e38087e, later than the
WROOM-32's, so its images are a little larger
(log).
The sieve shows what memory placement costs. Its scattered byte writes over 200 KB miss the cache constantly. The classic ESP32's PSRAM is on a 40 MHz quad SPI bus, so it is 60× slower than the H7's SRAM and 19× slower than the S3's 80 MHz octal PSRAM. Programs whose working set fits in internal RAM don't pay this.
Conformance¶
tests/conformance.py [--target s3|esp32|h7] runs a stock sample with goose run
on the PC, runs the same source on the board, and compares stdout byte for
byte. The samples print their timings on stderr, which the boards show in
red and the comparison leaves out. Samples that read files get them through
the board's file tree (--data); 17_calc gets its input through --stdin.
goose run can't run two kinds of sample: ones that use threads, and
24_call_c, which links C. Their reference is the output of a native 64-bit
build, kept in tests/expected/.
Goose ships 32 samples. Six can't run on any of these boards:
| Sample | Why not |
|---|---|
| 07_sieve | one 10 MB array; the largest board has 8 MB of PSRAM |
| 09_sorting | sorts a million numbers through 4 MB arrays, over 8 MB in all |
| 27_gfx_cube, 28_physics_boxes, 29_ui_todo, 30_mini_minecraft, 31_mini_doom | draw in a desktop window (OpenGL, through Goose's gfx package) |
That leaves 26, which between them cover strings and UTF-8, error handling, bignums, recursion, trees, a VM, float formatting, files, stdin, JSON, serialization, C interop and threads. Some need more than a board has:
- Equal-sized data stacks. Goose gives every data stack the same reservation. On a PC that costs nothing until it's touched, because virtual memory commits pages on use. These boards have no MMU over their RAM, so a program with one 1.2 MB array among ten data stacks (02_memory) needs ten stacks of that size. On the H7's 500 KB this rules out 02_memory, 21_raytrace and 22_image_filters. The WROOM-32 (300 KB, no PSRAM) can't run those three either, nor 25_serialize, whose 26 data stacks don't fit at any size its largest stack survives. With 8 MB of PSRAM, the S3 runs all but 02_memory.
Conformance turned up these problems, all fixed:
- Xtensa ABI bug (above): wrong 64-bit arguments, S3 only.
- 64-bit layout assumption: 16_records crashed on the S3. The Goose C
backend computes every stride and offset with 8-byte pointers
(implementation.md §6.1: it "targets 64-bit hosts"), so a struct holding a
slice is 28 bytes to Goose but 24 to a 32-bit C compiler.
microgoose/core.py:ptr64_layoutpads every pointer member inside Goose's packed type regions to 8 bytes (union { T *p; uint64_t pad; }). Member access and initializers are untouched. This affects every 32-bit target, not just Xtensa. With the fix, the H7 passed 16_records on its first run. - Too few guards (H7): 03_strings uses 16 data stacks, but the MPU has only 14 free regions. Stacks beyond the 14th now get the same canary check as the S3's stacks beyond its watchpoints.
- 64 KB stack buffers:
read_fileand the stdin readers copy through a 64 KB buffer on the native stack, and the Goose task on ESP-IDF has 24 KB.small_stack_ioshrinks those buffers to 1 KB (each is used in a loop), and path buffers to 256 bytes. intvsint32_t: on Xtensa and Arm,int32_tislong, so Goose's prototype for a C library function with anintin it clashes with the C header's. That coversldexpand anextern fnbound to"abs"(24_call_c). The fix drops those prototypes, since both types are 32-bit there.- stdio locks (H7): newlib-nano has no
flockfile, whichread_linetakes. Only the program's task reads stdin, so the stubs still do nothing. Printing from several tasks needed a lock of its own (see How it works). fflush(NULL)(ESP-IDF 6): the runtime flushes all streams withfflush(NULL)when a program ends with an error. ESP-IDF 6's picolibc (tinystdio) dereferences that NULL. As a result, every Goose runtime error (such ascannot reserve data stack address space) became aLoadProhibitedpanic. A linker wrap offflushhandles NULL.- Stale link flags: PlatformIO doesn't re-run CMake when only a
component's
CMakeLists.txtchanges, so the fix above was first linked without its--wrap. MicroGoose now rebuilds from clean whenever the ESP-IDF build configuration is newer than the build.
Overflow protection¶
examples/overflow.goose pushes until the 16 KB data stack is full. Both
boards stop it at the first store past the end and print the same error as
the PC build (goose runtime error: data stack overflow ... raise
--reserve):
| First guards | Remaining stacks | |
|---|---|---|
| ESP32-S3 | 2 hardware watchpoints per core, trap at once | 64-byte canary, checked every 100 ms and at exit |
| STM32H743 | 14 MPU no-access regions of 32 B, MemManage fault at once | 32-byte canary, checked every 100 ms (SysTick) and at exit |
The H7 also has an MPU guard below the native call stack, so deep recursion reports "native call stack overflow" instead of silently corrupting RAM.
Memory¶
The compiler knows statically how many data stacks a program needs
(gs_rt_init(argc, argv, stacks, regions, per_worker)), and microgoose prints
it. Memory is therefore stacks × --reserve, known before flashing, plus
each worker's stacks × --worker-reserve. Typical samples need 2–16 stacks. The runtime does not unmap the main data stacks at
exit (on a PC the process dies), which is why about 33–53 KB still shows as in
use after hello. That is not a leak.
Classic ESP32 (WROOM-32, WROVER-E)¶
Same ESP-IDF project, env:esp32 (microgoose ... --target esp32). The HAL now
selects the temperature sensor (absent on the ESP32) and ADC calibration
(line fitting, not curve fitting) per chip, through soc_caps.h. The ESP32
is also Xtensa (LX6), so both fixes apply. hello builds to 160 KB.
One image covers both modules. On the WROVER, ESP-IDF finds the 8 MB PSRAM,
maps 4 MB of it, and data stacks spill there once internal RAM runs short. On
the WROOM, SPIRAM_IGNORE_NOTFOUND lets the same binary boot without PSRAM.
Both boards were chip rev 3.x (ESP32-D0WD-V3). Every sample passed on the
first hardware run. The S3 work (ABI fix, layout fix, watchpoint guards,
pthreads) carried over with no code changes. Only the two chip-capability
#ifs in the HAL and an sdkconfig split (sdkconfig.defaults.<chip>) were
needed.
Arduino Nano (ATmega328P): not feasible¶
The Nano has 2 KB of SRAM and 32 KB of flash (30 KB after the bootloader).
ports/nano/ builds the same Goose output with avr-gcc 7.3 to see where it
breaks. Three things rule it out for stock Goose:
doubleis 32 bits on avr-gcc/avr-libc (__SIZEOF_DOUBLE__ 4). Goose'sf64cannot be represented, so float results would silently differ. avr-gcc ≥ 10 has-mdouble=64, but PlatformIO's toolchain is 7.3.- Flash.
hello's program alone, not counting the runtime, links to 17 KB of AVR code after dead-code removal. That is 5× its ARM size, because every Goose integer is 64-bit and the AVR does 64-bit arithmetic in library calls. The runtime pieces it needs (formatting, region reserve, panics) come on top. They are most of the H7's 28 KBhelloimage, in Thumb-2 code that is denser than AVR code. That is well past 30 KB. - RAM and pointers. The ABI assumes 8-byte pointers. On a 16-bit-pointer
chip
ptr64_layoutpads every 2-byte pointer to 8 bytes. With 2 KB of SRAM, even two 256-byte data stacks leave almost nothing for the C stack and stdio.
An 8-bit AVR would need a Goose backend that emits 16/32-bit code. That is a different project. The realistic floor for stock Goose is a 32-bit MCU with roughly 64 KB of flash and 64 KB of RAM. The H7 build suggests a Cortex-M4 such as an STM32F4 or RP2040 class part would do.
Verdict¶
| Target class | Stock Goose? | Notes |
|---|---|---|
| ESP32-S3 / ESP32 (Xtensa) | Yes | Needs the ABI workaround until GCC is fixed. Threads work on both cores. Keep hot data in internal RAM on the classic ESP32. |
| STM32H7 / Cortex-M7 | Yes | Smallest images (41–81 KB), real MPU guards. Threads share its one core. |
| Other 32-bit MCUs (Cortex-M4, RISC-V ESP32-C3/C6) | Very likely | Only the layout fix is needed. RISC-V doesn't have the Xtensa ABI bug. |
| 8-bit AVR (Arduino Nano) | No | 32-bit double, 16-bit pointers, 2 KB RAM. |
What would make this first-class¶
Changes to Goose, in order of value:
- A pointer-size option in the C backend (
--target-ptr 4). This would removeptr64_layoutand roughly halve slice and reference sizes on 32-bit MCUs. It is the one real portability gap. - Opt-in bounds checks on data-stack pushes (
--checked-stacks), for MCUs with no MMU, MPU or watchpoints to spare. This would make overflow detection exact rather than best-effort. - A hook to free all regions at program exit. This matters only on systems where "exit" doesn't reset the chip.
Outside Goose: report the Xtensa xtensa_function_arg TYPE_ALIGN bug to
GCC and esp-gcc (ARM precedent: PR target/65956). The fix is to use
TYPE_MAIN_VARIANT.