From e04d5d54f819971d5d1a202f02f66d72944d6b61 Mon Sep 17 00:00:00 2001 From: WDrijver Date: Wed, 25 Nov 2026 12:25:41 +0100 Subject: [PATCH] Sync toolchain with ApolloCrossDev of 2026-11-25: bbb pitfalls, patch tables, NDK paths - Five new pitfalls for the bbb fixes in ApolloCrossDev PRs #29, #30, #34, #35 and #37, with reproductions on GCC-6.50-Dev. - compiler-variants.md: Dev applies 19 patches, Stable 7; bug table gains the btst, stack-slot and five bbb rows; how to tell which fixes an install has (the version string never changes). - NDK paths after ApolloCrossDev PR #38: ndk-include, not ndk32-include/Include_H; no NDK 3.9 or clib2 install; lower-case mui. Also in the apollo-new-project skill, compile-snippets.sh and v4test.py. Co-Authored-By: Claude Opus 5.5 --- skills/apollo-new-project/SKILL.md | 2 +- toolchain/c-compiler-options.md | 12 +- toolchain/compiler-variants.md | 43 ++++-- .../gcc-bbb-autoinc-converted-insn.md | 107 +++++++++++++++ .../pitfalls/gcc-bbb-autoinc-fixup-ice.md | 102 ++++++++++++++ .../pitfalls/gcc-bbb-propagate-moves-ice.md | 129 ++++++++++++++++++ .../gcc-bbb-propagate-moves-loop-exit.md | 113 +++++++++++++++ .../pitfalls/gcc-bbb-stale-memory-value.md | 88 ++++++++++++ toolchain/target-os-headers.md | 29 ++-- tools/compile-snippets.sh | 2 +- tools/v4/v4test.py | 2 +- 11 files changed, 604 insertions(+), 25 deletions(-) create mode 100644 toolchain/pitfalls/gcc-bbb-autoinc-converted-insn.md create mode 100644 toolchain/pitfalls/gcc-bbb-autoinc-fixup-ice.md create mode 100644 toolchain/pitfalls/gcc-bbb-propagate-moves-ice.md create mode 100644 toolchain/pitfalls/gcc-bbb-propagate-moves-loop-exit.md create mode 100644 toolchain/pitfalls/gcc-bbb-stale-memory-value.md diff --git a/skills/apollo-new-project/SKILL.md b/skills/apollo-new-project/SKILL.md index c30c248..9e08e52 100644 --- a/skills/apollo-new-project/SKILL.md +++ b/skills/apollo-new-project/SKILL.md @@ -151,7 +151,7 @@ Copy `/Projects/_makefiles/Makefile` and change only: | Line | Set to | |---|---| | `PROJECT_NAME` | the name | -| `C_INCL_NDK` | the `ndk39-include` line: AmigaOS 3.9 is a target, so a function that 3.9 lacks gives a compile error instead of a crash on 3.9 (`KB toolchain/target-os-headers.md`) | +| `C_INCL_NDK` | keep the `ndk-include` line (NDK 3.2). ApolloCrossDev installs no NDK 3.9: when AmigaOS 3.9 is a target, check every OS function the program calls against `KB os/apolloos-and-amigaos-on-v4.md` instead (`KB toolchain/target-os-headers.md`) | | `C_COMPILER` | the `m68k-amigaos-g++` line for C++; the template does not collect `.cpp` files, so add them as `KB toolchain/project-makefile.md` says | Keep `APOLLOLIB`, `C_OPTIONS` and `S_FLAGS` as they are. diff --git a/toolchain/c-compiler-options.md b/toolchain/c-compiler-options.md index 3e82ce2..7df8020 100644 --- a/toolchain/c-compiler-options.md +++ b/toolchain/c-compiler-options.md @@ -49,7 +49,7 @@ more on code generation, [gcc-code-generation-for-68080.md](best-practices/gcc-c | Runtime | Option | Include path | |---|---|---| | libnix (default) | `-noixemul` | `/m68k-amigaos/libnix/include` | -| clib2 | `-mcrt=clib2` | `/m68k-amigaos/clib2/include` | +| clib2 | `-mcrt=clib2` | `/m68k-amigaos/clib2/include`, not installed by the GCC-6.50 scripts | Pass the runtime option both when compiling and when linking. @@ -62,12 +62,18 @@ and NDK 3.2: -I. \ -I/m68k-amigaos/libnix/include \ -I/m68k-amigaos/sys-include \ --I/m68k-amigaos/ndk32-include/Include_H +-I/m68k-amigaos/ndk-include ``` For other target operating systems, see `toolchain/target-os-headers.md`. Optional include folders: `/m68k-amigaos/include/GL`, `.../SDL`, -`.../MUI`. +`.../mui` (lower case: case matters on Linux). + +Until ApolloCrossDev pull request #38 (2026-11-25) the template pointed at +`ndk32-include/Include_H`, which the GCC-6.50 scripts never create. Builds +still worked because GCC's built-in include path already holds +`ndk-include`. A Makefile copied before that date has the wrong path; the +generated code is the same. ### Extra libraries diff --git a/toolchain/compiler-variants.md b/toolchain/compiler-variants.md index 1893c4e..5370a24 100644 --- a/toolchain/compiler-variants.md +++ b/toolchain/compiler-variants.md @@ -20,6 +20,7 @@ sources: - "github.com/WDrijver/ApolloCrossDev pull request #15 (removes double-indirect-fix.patch) and issue #14" - "WDrijver (ApolloCrossDev maintainer), 2026-11-04" - "WDrijver (maintainer), 2026-11-11: LineA instructions are not yet implemented; only a few unreleased test cores had them" + - "github.com/WDrijver/ApolloCrossDev pull requests #26, #28, #29, #30, #34, #35 and #37 (merged 2026-11-16 to 2026-11-25), install scripts at commit c1fba26a4" --- ## Summary @@ -34,17 +35,17 @@ and differ mainly in which Apollo 68080 patches they apply. Use GCC-6.50-Dev. | Variant | Script | Apollo patches applied | Use | |---|---|---|---| -| GCC-6.50-Dev | `GCC-6.50-Dev-Linux.sh`, `-MacOS.sh` | 12 (see below) | Default, latest | -| GCC-6.50-Stable | `GCC-6.50-Stable-Linux.sh`, `-MacOS.sh` | 5 | Fallback if GCC-6.50-Dev gives problems | +| GCC-6.50-Dev | `GCC-6.50-Dev-Linux.sh`, `-MacOS.sh` | 19 (see below) | Default, latest | +| GCC-6.50-Stable | `GCC-6.50-Stable-Linux.sh`, `-MacOS.sh` | 7 | Fallback if GCC-6.50-Dev gives problems | | GCC-6.50-Base | `GCC-6.50-Base-Linux.sh`, `-MacOS.sh` | none | Original baseline (November 2024) | Each variant installs into its own folder `Compilers/`, so several can be installed side by side. All three set `MUIMASTER_VMIN` to 19 in `mui.h`, so MUI programs also open on MUI 3.8 (from 2026-11-03). -Patches applied by GCC-6.50-Dev from ApolloCrossDev pull request #15 -(2026-11-04), from -`Compilers/_archives/patches/`: +Patches applied by GCC-6.50-Dev (install scripts at ApolloCrossDev commit +`c1fba26a4`, 2026-11-25), from `Compilers/_archives/patches/`. The first +twelve date from ApolloCrossDev pull request #15 (2026-11-04): | Patch | Author | |---|---| @@ -60,6 +61,13 @@ Patches applied by GCC-6.50-Dev from ApolloCrossDev pull request #15 | `m68k-mrtd-call-pop.patch` | TuKo1982 | | `m68k-sincos-constraint-fix.patch` | TuKo1982 | | `m68k-dbra-hi-addrreg-fix.patch` | TuKo1982 | +| `m68k-btst-mem-offset-fix.patch` (PR #26, 2026-11-16) | WDrijver | +| `reload-ind-equiv-offset-fix.patch` (PR #28, 2026-11-17) | WDrijver | +| `bbb-autoinc-converted-insn-fix.patch` (PR #29, 2026-11-24) | delwall | +| `bbb-invalidate-mem-fix.patch` (PR #30, 2026-11-24) | delwall | +| `bbb-autoinc-fixup-constraints-fix.patch` (PR #34, 2026-11-24) | WDrijver | +| `bbb-propagate-moves-bb-boundary-fix.patch` (PR #35, 2026-11-25) | WDrijver | +| `bbb-propagate-moves-addr-exit-fix.patch` (PR #37, 2026-11-25) | WDrijver | Until then GCC-6.50-Dev also applied `double-indirect-fix.patch` by Marcel (r3dbug). It stopped GCC from generating memory indirect addressing, and @@ -67,8 +75,9 @@ made GCC crash on computed gotos through a table (`jmp ([table,dN.l*4])`, for example in Lua's `lvm.c`). It was removed because the 68080 has full double indirect addressing (WDrijver, 2026-11-04). -GCC-6.50-Stable applies `q2g-stable.patch` and the four patches by Morten, -none by TuKo1982. +GCC-6.50-Stable applies `q2g-stable.patch`, the four patches by Morten, +`m68k-btst-mem-offset-fix.patch` and `reload-ind-equiv-offset-fix.patch`. +It applies none by TuKo1982 and none of the bbb fixes. ### Known GCC bugs and which variant fixes them @@ -79,11 +88,25 @@ none by TuKo1982. | ICE with `-mrtd` | [gcc-mrtd-calling-convention.md](pitfalls/gcc-mrtd-calling-convention.md) | Fixed | Not fixed | Not fixed | | ICE on `sinf`/`cosf` with `-ffast-math` | [gcc-fsincos-ice.md](pitfalls/gcc-fsincos-ice.md) | Fixed | Not fixed | Not fixed | | 16-bit loop counter in an address register runs away | [gcc-dbra-address-register-loop.md](pitfalls/gcc-dbra-address-register-loop.md) | Fixed | Not fixed | Not fixed | +| `btst` tests the wrong byte of a bit field in memory | [gcc-btst-memory-bit-number.md](pitfalls/gcc-btst-memory-bit-number.md) | Fixed | Fixed | Not fixed | +| Memory indirect operand on the wrong stack slot after a call | [gcc-double-indirect-stack-slot.md](pitfalls/gcc-double-indirect-stack-slot.md) | Fixed | Fixed | Not fixed | +| bbb pass fixes up a post-increment it just made; pointer stride too small | [gcc-bbb-autoinc-converted-insn.md](pitfalls/gcc-bbb-autoinc-converted-insn.md) | Fixed | Not fixed | Not fixed | +| bbb pass keeps a stale memory value after a store; wrong 64-bit results | [gcc-bbb-stale-memory-value.md](pitfalls/gcc-bbb-stale-memory-value.md) | Fixed | Not fixed | Not fixed | +| ICE "insn does not satisfy its constraints" after an auto-increment | [gcc-bbb-autoinc-fixup-ice.md](pitfalls/gcc-bbb-autoinc-fixup-ice.md) | Fixed | Not fixed | Not fixed | +| ICE in `schedule_region` when bbb moves a copy out of a loop | [gcc-bbb-propagate-moves-ice.md](pitfalls/gcc-bbb-propagate-moves-ice.md) | Fixed | Not fixed | Not fixed | +| bbb pass gives an early loop exit the old pointer value | [gcc-bbb-propagate-moves-loop-exit.md](pitfalls/gcc-bbb-propagate-moves-loop-exit.md) | Fixed | Not fixed | Not fixed | | ICE in `print_operand_address2` on computed gotos (Dev builds of 2026-11-03 only) | [double-memory-indirect.md](../core/pitfalls/double-memory-indirect.md) | Fixed (patch removed) | Not affected | Not affected | -"Fixed" means the install script applies the patch on 2026-11-03. A fix only -reaches you when you reinstall the variant; an older install still has the -bug. +"Fixed" means the install script applies the patch at ApolloCrossDev commit +`c1fba26a4` (2026-11-25). A fix only reaches you when you reinstall the +variant; an older install still has the bug. Each pitfall gives the date from +which an install has the fix. + +`m68k-amigaos-gcc --version` prints `6.5.0b 240104120137` for every install, +so it does not tell which patches you have. Check the date of the install +(`ls -l /libexec/gcc/m68k-amigaos/6.5.0b/cc1`) against the merge dates +above. When a bbb bug is suspected, compile the file again with `-fbbb=-`: if +the problem goes away, it is in the bbb pass. ### 68080 instructions the compiler generates diff --git a/toolchain/pitfalls/gcc-bbb-autoinc-converted-insn.md b/toolchain/pitfalls/gcc-bbb-autoinc-converted-insn.md new file mode 100644 index 0000000..33e48ce --- /dev/null +++ b/toolchain/pitfalls/gcc-bbb-autoinc-converted-insn.md @@ -0,0 +1,107 @@ +--- +title: GCC 6.5 bbb pass fixes up a post-increment it just made, and a pointer advances too little +type: pitfall +owner: WDrijver +status: draft +origin: ai-assisted +basis: [measured, source-code] +components: [toolchain] +applies_to: + core: "any" + os: [any] +tags: [gcc, bbb, optimizer, autoincrement, miscompile, struct-stride] +related: + - toolchain/compiler-variants.md + - toolchain/pitfalls/gcc-bbb-autoinc.md +sources: + - "github.com/WDrijver/ApolloCrossDev pull request #29 by delwall (bbb-autoinc-converted-insn-fix.patch, merged 2026-11-24)" + - "Reproduction with GCC-6.50-Dev 6.5.0b, installed 2026-11-25 (this file, Evidence)" +--- + +## Symptom + +A loop over packed records (for example 12-byte `float[3]` vertices) reads +the wrong fields and advances its pointer too little. The result is silently +wrong. There is no warning and no crash. `-fbbb=-` gives correct code. + +It needs a loop body that reads the same record in two groups, such as two +dot products on the same vertex. The original case was a 68080 renderer. +`float`, `double`, and 3, 4 or 5 element records were all affected. + +## Cause + +Bebbo's bbb pass `opt_autoinc` (`-fbbb=i`) turns `(a0)`, `(4,a0)`, `(8,a0)` +into post-increments one instruction at a time. For each one it lowers the +offsets of the other uses of `a0` and the closing `add`/`lea`. After an +instruction is converted, its bookkeeping still shows the old offset until +the next pass. A later search in the same pass sees that instruction as a +plain `(-4,a0)` access and lowers the `add` a second time. Offsets shift too +far and a `lea (4,a0),a0` that must stay is deleted. + +In the reproducer below, `a0` advanced 8 bytes per vertex instead of 12, and +`v[2]` was read from `(a0)` instead of `(4,a0)`. + +The fix makes `try_auto_inc` stop at an instruction that already +auto-modifies the register. Only GCC-6.50-Dev applies it. + +## Wrong + +```c +typedef float vec3_t[3]; +void uv(const vec3_t *pos, float (*out)[2], int n, + const float *vx, const float *vy, + float ox, float oy, float sx, float sy, float k) +{ + int i; + for (i = 0; i < n; i++) { + const float *v = pos[i]; /* read twice: two groups of loads */ + out[i][0] = (v[0]*vx[0] + v[1]*vx[1] + v[2]*vx[2] + ox - sx) * k; + out[i][1] = (v[0]*vy[0] + v[1]*vy[1] + v[2]*vy[2] + oy - sy) * k; + } +} +``` + +With `-m68080 -m68881 -ffast-math -fomit-frame-pointer -Ofast +-fexcess-precision=fast -fno-unsafe-math-optimizations`, before the fix: + +```text + fsmove.s (a0)+,fp0 + fsmove.s (a0),fp3 ; v[1] and v[2] both read at (a0) + fsmove.s (a0),fp2 + ... + fsmove.s (-8,a0),fp0 + fsmove.s (-4,a0),fp3 + fsmove.s (a0)+,fp2 ; a0 advanced 8 bytes, not 12 +``` + +## Right + +Use GCC-6.50-Dev installed on or after 2026-11-24. The same loop then +compiles to: + +```text + fsmove.s (a0)+,fp0 + fsmove.s (a0)+,fp3 + fsmove.s (a0)+,fp2 + fsmove.s (-12,a0),fp0 + fsmove.s (-8,a0),fp3 + fsmove.s (-4,a0),fp2 +``` + +With an older install, GCC-6.50-Stable or GCC-6.50-Base, compile the +affected file with `-fbbb=-`, or reinstall GCC-6.50-Dev. + +## Detection + +Compile with `-S` and look for an address register that is post-incremented +in one group of loads while other loads of the same record use `(aN)` +without an offset. Compare with the output of `-fbbb=-`: the number of +bytes the pointer advances per iteration must be the record size. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| Two groups of loads on one record, converted in one pass, give wrong offsets and too small a stride; `-fbbb=-` is correct | (SRC) ApolloCrossDev PR #29 | any (compiler output) | Patch description with the reproducer and the wrong `-S` output | +| With the fix, the reproducer gives three post-increments, then `(-12,a0)`, `(-8,a0)`, `(-4,a0)` | (M) 2026-11-25 | any (compiler output) | GCC-6.50-Dev 6.5.0b installed 2026-11-25 08:34, flags above, `-S` | +| Only GCC-6.50-Dev applies the patch | (SRC) | any | `GCC-6.50-Dev-*.sh` and `GCC-6.50-Stable-*.sh`, ApolloCrossDev `c1fba26a4` | diff --git a/toolchain/pitfalls/gcc-bbb-autoinc-fixup-ice.md b/toolchain/pitfalls/gcc-bbb-autoinc-fixup-ice.md new file mode 100644 index 0000000..be36100 --- /dev/null +++ b/toolchain/pitfalls/gcc-bbb-autoinc-fixup-ice.md @@ -0,0 +1,102 @@ +--- +title: "GCC 6.5 bbb pass crashes with \"insn does not satisfy its constraints\" after an auto-increment" +type: pitfall +owner: WDrijver +status: draft +origin: ai-assisted +basis: [measured, source-code] +components: [toolchain] +applies_to: + core: "any" + os: [any] +tags: [gcc, bbb, optimizer, autoincrement, ice, volatile] +related: + - toolchain/compiler-variants.md + - toolchain/pitfalls/gcc-bbb-autoinc-converted-insn.md +sources: + - "github.com/WDrijver/ApolloCrossDev issue #33 and pull request #34 (bbb-autoinc-fixup-constraints-fix.patch, merged 2026-11-24)" + - "ApolloTeam-dev/V4Net, source/v4net/netdev.c, drv_dma_alloc (where it was found)" + - "Reproduction with GCC-6.50-Dev 6.5.0b, installed 2026-11-25 (this file, Evidence)" +--- + +## Symptom + +The compiler stops with an internal compiler error: + +```text +error: insn does not satisfy its constraints: +(insn 84 ... (set (reg:SI 8 a0) + (plus:SI (mem/v:SI (plus:SI (reg/v/f:SI 11 a3) (const_int 11054))) + (const_int 1)))) {*addsi3_internal} +internal compiler error: in final_scan_insn, at final.c:2943 +``` + +It was found building `v4net.device` at `-O2` with `-m68020` or `-m68080`. +The trigger was a `volatile ULONG` increment at a large struct offset between +`Forbid()` and `Permit()`, with `SysBase` read through a struct pointer. +`-fbbb=-` compiles the file. + +## Cause + +Bebbo's bbb pass `opt_autoinc` (`-fbbb=i`) turns `move.l (a3),a2` into +`move.l (a3)+,a2` and lowers the offsets of the later uses of `a3`. Each +changed instruction is emitted again and recognized anew, and GCC takes the +first pattern whose operand types match. That need not be the pattern the +instruction had. `lea ([11054,a3],1),a0` came back with offset 11050 as +`*addsi3_internal`, which does not allow a memory source with an address +register as destination. Nothing checked this, so `final` crashed. + +The fix tries each fixup on a copy first and gives up the auto-increment +when the copy is not a valid instruction. That spot then compiles as with +`-fbbb=-`. Only GCC-6.50-Dev applies it. + +## Wrong + +The reduced case from ApolloCrossDev issue #33: + +```c +#include +#include +struct Db { UBYTE pad[34]; struct Library *db_SysBase; }; +#include +#define SysBase db->db_SysBase +struct Nd { struct Db *nd_Db; UBYTE pad[11050]; volatile ULONG cnt; }; + +APTR f(APTR ctx, ULONG size, ULONG align) +{ + struct Nd *nd = ctx; + struct Db *db = nd->nd_Db; + UBYTE *raw; + APTR *p; + if (align < 4) align = 4; + raw = AllocVec(size + align + 4, MEMF_PUBLIC); + if (raw == NULL) return NULL; + p = (APTR *)(((ULONG)raw + 4 + align - 1) & ~(align - 1)); + p[-1] = raw; + Forbid(); + nd->cnt++; /* volatile increment at offset 11054 */ + Permit(); + return p; +} +``` + +## Right + +Use GCC-6.50-Dev installed on or after 2026-11-25 (pull request #34 was +merged late on 2026-11-24). With an older install, compile the affected +file with `-fbbb=-`, or reinstall GCC-6.50-Dev. The code needs no change. + +## Detection + +The error message itself: `insn does not satisfy its constraints` with a +`mem` operand and an address register destination, in `final_scan_insn`. +If `-fbbb=-` makes it go away, it is this bug or another bbb bug. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| ICE with default flags and with `-fbbb=i`; `-fbbb=-` compiles | (M) 2026-11-24 | any (compiler output) | ApolloCrossDev issue #33, GCC-6.50-Dev built 2026-11-24 15:15 UTC, `-noixemul -O2 -m68020 -fomit-frame-pointer -msoft-float` | +| With the fix the reproducer compiles with `-m68020` and with `-m68080` | (M) 2026-11-25 | any (compiler output) | GCC-6.50-Dev 6.5.0b installed 2026-11-25 08:34, same flags, NDK 3.2 headers | +| Cause: the re-emitted insn is recognized as `*addsi3_internal` without a constraint check | (SRC) | any | `bbb-autoinc-fixup-constraints-fix.patch`, `bbb-opts.c` | +| Only GCC-6.50-Dev applies the patch | (SRC) | any | `GCC-6.50-Dev-*.sh` and `GCC-6.50-Stable-*.sh`, ApolloCrossDev `c1fba26a4` | diff --git a/toolchain/pitfalls/gcc-bbb-propagate-moves-ice.md b/toolchain/pitfalls/gcc-bbb-propagate-moves-ice.md new file mode 100644 index 0000000..b477776 --- /dev/null +++ b/toolchain/pitfalls/gcc-bbb-propagate-moves-ice.md @@ -0,0 +1,129 @@ +--- +title: "GCC 6.5 bbb pass crashes in schedule_region when it moves a copy out of a loop" +type: pitfall +owner: WDrijver +status: draft +origin: ai-assisted +basis: [measured, source-code] +components: [toolchain] +applies_to: + core: "any" + os: [any] +tags: [gcc, bbb, optimizer, propagate-moves, ice, sched2, lua] +related: + - toolchain/compiler-variants.md + - toolchain/pitfalls/gcc-bbb-propagate-moves-loop-exit.md +sources: + - "github.com/WDrijver/ApolloCrossDev pull request #35 (bbb-propagate-moves-bb-boundary-fix.patch, merged 2026-11-25)" + - "Lua 5.4.6 lobject.c, luaO_str2num (where it was found)" + - "Reproduction with GCC-6.50-Dev 6.5.0b, installed 2026-11-25 (this file, Evidence)" +--- + +## Symptom + +The compiler stops with: + +```text +internal compiler error: in schedule_region, at sched-rgn.c:3213 +``` + +It was found compiling Lua 5.4.6 `lobject.c` at `-O3 -m68080` and at +`-Ofast -m68080 -m68881`. `-fbbb=-`, and `-fbbb` without `p`, compile the +file. + +## Cause + +Bebbo's bbb pass `opt_propagate_moves` (`-fbbb=p`) takes a register copy +pair `move src,x ... move x,src` out of a loop. One copy goes in front of +the loop label, the other behind the loop's jump. The pass chose those +places from its own filtered list of instructions, so the place in front of +the loop could be a conditional jump, and the place behind it could be a +label. The copy then landed behind the jump that ends a basic block: + +```text + beq .L221 ; end of the block + move.l d0,a4 ; the moved copy, still in that block +.L154: ; loop label +``` + +The code would still run correctly, but the scheduler (`sched2`) cannot +place an instruction behind a block's jump, and crashed. + +The fix leaves such loops alone, as with `-fbbb=-`. Only GCC-6.50-Dev +applies it. + +## Wrong + +The reduced case from the patch, with the string-to-number code of Lua +inlined into one function at `-O3`: + +```c +#include +#include +extern const unsigned char ctype_[257]; +typedef struct { double n; unsigned char tt; } TValue; + +static const char *str2dloc(const char *s, double *result, int mode) +{ + char *endptr; + *result = (mode == 'x') ? strtod(s, &endptr) : strtod(s, &endptr); + if (endptr == s) return NULL; + while (ctype_[(unsigned char)*endptr + 1] & 8) endptr++; + return *endptr == '\0' ? endptr : NULL; +} + +static const char *str2d(const char *s, double *result) +{ + const char *endptr; + const char *pmode = strpbrk(s, ".xXnN"); + int mode = pmode ? ((unsigned char)*pmode | ('A' ^ 'a')) : 0; + if (mode == 'n') return NULL; + endptr = str2dloc(s, result, mode); + if (endptr == NULL) { + char buff[201]; + strcpy(buff, s); + endptr = str2dloc(buff, result, mode); + if (endptr != NULL) endptr = s + (endptr - buff); + } + return endptr; +} + +static const char *str2int(const char *s, long long *result) +{ + if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) + for (s += 2; ctype_[(unsigned char)*s + 1] & 16; s++) ; + while (ctype_[(unsigned char)*s + 1] & 8) s++; + if (*s != '\0') return NULL; + *result = 0; + return s; +} + +size_t str2num(const char *s, TValue *o) +{ + long long i; double n; const char *e; + if ((e = str2int(s, &i)) != NULL) o->tt = 3; + else if ((e = str2d(s, &n)) != NULL) { o->n = n; o->tt = 19; } + else return 0; + return (e - s) + 1; +} +``` + +## Right + +Use GCC-6.50-Dev installed on or after 2026-11-25. With an older install, +compile the affected file with `-fbbb=-`, or reinstall GCC-6.50-Dev. The +code needs no change. + +## Detection + +The error message itself: an ICE in `schedule_region` at +`sched-rgn.c:3213`. If `-fbbb=-` makes it go away, it is this bug or another +bbb bug. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| ICE at `-O3 -m68080` and `-Ofast -m68080 -m68881`; `-fbbb=-` and `-fbbb` without `p` compile it | (SRC) ApolloCrossDev PR #35 | any (compiler output) | Patch description, Lua 5.4.6 `lobject.c` and the reduced case | +| With the fix the reduced case compiles at `-O3 -m68080` and `-Ofast -m68080 -m68881` | (M) 2026-11-25 | any (compiler output) | GCC-6.50-Dev 6.5.0b installed 2026-11-25 08:34, `-noixemul -S` | +| Only GCC-6.50-Dev applies the patch | (SRC) | any | `GCC-6.50-Dev-*.sh` and `GCC-6.50-Stable-*.sh`, ApolloCrossDev `c1fba26a4` | diff --git a/toolchain/pitfalls/gcc-bbb-propagate-moves-loop-exit.md b/toolchain/pitfalls/gcc-bbb-propagate-moves-loop-exit.md new file mode 100644 index 0000000..6264a33 --- /dev/null +++ b/toolchain/pitfalls/gcc-bbb-propagate-moves-loop-exit.md @@ -0,0 +1,113 @@ +--- +title: GCC 6.5 bbb pass gives an early loop exit the pointer value from before the loop +type: pitfall +owner: WDrijver +status: draft +origin: ai-assisted +basis: [measured, source-code] +components: [toolchain] +applies_to: + core: "any" + os: [any] +tags: [gcc, bbb, optimizer, propagate-moves, autoincrement, miscompile, loop] +related: + - toolchain/compiler-variants.md + - toolchain/pitfalls/gcc-bbb-propagate-moves-ice.md +sources: + - "github.com/WDrijver/ApolloCrossDev issue #36 and pull request #37 (bbb-propagate-moves-addr-exit-fix.patch, merged 2026-11-25)" + - "Reproduction with GCC-6.50-Dev 6.5.0b, installed 2026-11-25 before pull request #37 (this file, Evidence)" +--- + +## Symptom + +A loop that walks a pointer through a buffer and leaves early, with `break` +or `goto`, sees the pointer's value from before the loop after the exit. +Code after the loop that uses the pointer, such as `p - s`, gets the wrong +result. Leaving the loop at its normal end is correct. There is no warning. +It happens at `-O2` with the default `-fbbb`, for 68020 to 68080. + +## Cause + +Bebbo's bbb pass `opt_propagate_moves` (`-fbbb=p`) takes a loop with a +register copy pair + +```text +.L1: move.l src,dst ... (exit: jcc .Lout) ... move.l dst,src jcc .L1 +``` + +and moves the first copy in front of the loop and the second behind it. +Inside the loop `src` is then no longer updated, so every exit between the +two copies must set `src` from `dst` again. Two bugs broke that: + +1. For a post-increment in the source of an instruction, the size was taken + from the destination. After bbb pass 0 a byte load is a + `strict_low_part`, which has no size, so the increment counted as 0. +2. With two address registers and no offset to undo, nothing at all was + emitted at the exit, so `src` kept its value from before the loop. + +The fix uses the size of the memory read, and emits `src = dst` when the +offset is 0. Only GCC-6.50-Dev applies it. + +## Wrong + +```c +long f(const unsigned char *s, unsigned char *d, unsigned char c, + unsigned char m, const unsigned char *lim, int n, unsigned char *x0, + unsigned char *x1, unsigned char *x2, unsigned char *x3) +{ + const unsigned char *p = s; long acc = 0; int k = 0; + while (k < n) + { + const unsigned char *q = p; + if ((*q & m) != 0) goto out; /* early exit */ + acc += *q++; + p = q; k++; + } +out: + return (long)(p - s) * 3 + acc; +} +``` + +At `-O2 -m68020`, before the fix, the loop advances `a0`, but the early exit +computes `p - s` from `a1`, which still holds `s`: + +```text + move.l d5,a1 ; a1 = s + ... + move.l a1,a0 +.L4: + clr.l d1 + move.b (a0)+,d1 ; p advances in a0 only + move.b d1,d4 + and.b d3,d4 + jne .L9 ; early exit + ... +.L9: + sub.l d5,a1 ; p - s from a1 = s: always 0 +``` + +Every early exit after the first iteration returns `acc` instead of +`3 * (p - s) + acc`. + +## Right + +Use GCC-6.50-Dev installed on or after 2026-11-25, after pull request #37 +(merged at 09:46 UTC). An install from earlier that day still has this bug. +With an older install, GCC-6.50-Stable or GCC-6.50-Base, compile the +affected file with `-fbbb=-`, or reinstall GCC-6.50-Dev. + +## Detection + +In `-S` output, look for a loop that post-increments one address register +and has an exit label that reads another address register which the loop +never writes. Compare with `-fbbb=-`, where the loop copies the pointer +back (`move.l a0,a1`) inside the loop. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| The early exit at `.L9` computes `p - s` from `a1`, which still holds `s`; `-fbbb=-` copies `a0` to `a1` inside the loop | (M) 2026-11-25 | any (compiler output) | GCC-6.50-Dev 6.5.0b installed 2026-11-25 08:34 (before PR #37), `-O2 -m68020 -S`, with and without `-fbbb=-` | +| Cause: increment size taken from a `strict_low_part` (VOIDmode) destination; no copy emitted for two address registers with offset 0 | (SRC) | any | `bbb-propagate-moves-addr-exit-fix.patch`, ApolloCrossDev issue #36 | +| With the fix, `.L9` uses `a0 - 1`, the same as `-fbbb=-` | (SRC) ApolloCrossDev PR #37 | any (compiler output) | Patch description | +| Only GCC-6.50-Dev applies the patch | (SRC) | any | `GCC-6.50-Dev-*.sh` and `GCC-6.50-Stable-*.sh`, ApolloCrossDev `c1fba26a4` | diff --git a/toolchain/pitfalls/gcc-bbb-stale-memory-value.md b/toolchain/pitfalls/gcc-bbb-stale-memory-value.md new file mode 100644 index 0000000..340ef9c --- /dev/null +++ b/toolchain/pitfalls/gcc-bbb-stale-memory-value.md @@ -0,0 +1,88 @@ +--- +title: GCC 6.5 bbb pass keeps a stale memory value after a store and deletes the reload +type: pitfall +owner: WDrijver +status: draft +origin: ai-assisted +basis: [measured, source-code] +components: [toolchain] +applies_to: + core: "any" + os: [any] +tags: [gcc, bbb, optimizer, dead-assign, miscompile, int64, spill] +related: + - toolchain/compiler-variants.md + - toolchain/pitfalls/gcc-bbb-shift-wrong-code.md +sources: + - "github.com/WDrijver/ApolloCrossDev pull request #30 by delwall (bbb-invalidate-mem-fix.patch, merged 2026-11-24)" +--- + +## Symptom + +64-bit arithmetic gives wrong results at `-O2 -m68080`, for example +`int64_t a = ...; a * i / j`. There is no warning. `-fbbb=-`, `-fbbb` +without `e`, and `-O1` give correct results. + +## Cause + +Bebbo's bbb pass remembers which register holds which memory value. A +store must make it forget every cached value the store may overwrite. The +old `track_var::invalidate_mem` kept the cached value when the store had +another mode or other attributes, or lay within 3 bytes of it: its offset +test was inverted. + +When a 64-bit (`DImode`) store overwrote a 32-bit spill slot, the pass still +believed a register held the slot's old value. `opt_elim_dead_assign` +(`-fbbb=e`) then deleted the reload of that slot: + +```text + move.l (-460,a5),d0 + move.l d0,(-456,a5) + smi d4 + extb.l d4 + move.l d4,(-460,a5) ; overwrites the slot + muls.l d0,d1 + move.l (-460,a5),d0 ; deleted by -fbbb=e: d0 keeps the old value + muls.l (-480,a5),d0 +``` + +The fix forgets a cached value on every store that may overlap it: always +for the same base register, and otherwise unless the alias sets or +`nonoverlapping_memrefs_p` prove the two apart. Instructions with several +stores are handled too. Only GCC-6.50-Dev applies it. + +## Wrong + +```c +#include + +int64_t scale(int64_t a, int mul, int div) +{ + return a * mul / div; /* can be miscompiled at -O2 -m68080 */ +} +``` + +The function above shows the pattern, not a confirmed reproducer: the bug +needs register pressure that spills a value next to a 64-bit temporary. + +## Right + +Use GCC-6.50-Dev installed on or after 2026-11-24. With an older install, +GCC-6.50-Stable or GCC-6.50-Base, compile files with 64-bit arithmetic with +`-fbbb=-`, or reinstall GCC-6.50-Dev. + +## Detection + +Run 64-bit arithmetic against host results in a unit test. In `-S` output, +look for a store to a stack slot followed by a use of a register that held +that slot before the store, with no reload in between. Compare with +`-fbbb=-`. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| A `DImode` store over an `SImode` spill slot kept the stale value; `opt_elim_dead_assign` deleted the reload; wrong 64-bit products | (SRC) ApolloCrossDev PR #30 | any (compiler output) | Pull request description with the `-S` excerpt above | +| With the fix the reduced case matches the host | (M) delwall, PR #30 | V4 (core not stated) | Run on a Vampire V4 against host results | +| 703 of 720 compilations (373 C files at `-m68080 -O2/-O3/-Os`) unchanged; the rest only keep loads the old pass had replaced | (M) delwall, PR #30 | any (compiler output) | Comparison of compiler output before and after the patch | +| Only GCC-6.50-Dev applies the patch | (SRC) | any | `GCC-6.50-Dev-*.sh` and `GCC-6.50-Stable-*.sh`, ApolloCrossDev `c1fba26a4` | diff --git a/toolchain/target-os-headers.md b/toolchain/target-os-headers.md index a271cf0..dc064aa 100644 --- a/toolchain/target-os-headers.md +++ b/toolchain/target-os-headers.md @@ -16,13 +16,16 @@ sources: - "github.com/WDrijver/ApolloCrossDev Projects/_makefiles/Makefile" - "github.com/WDrijver/ApolloCrossDev GCC-6.50-Dev-Linux.sh" - "WDrijver (ApolloCrossDev maintainer), 2026-11-02" + - "github.com/WDrijver/ApolloCrossDev pull request #38 (2026-11-25, fixes issue #32): the GCC-6.50 scripts install ndk-include (NDK 3.2) and ndk13-include, not ndk32-include or ndk39-include" + - "lessons/2026-11-lide-device.md (found the missing folders, 2026-11-24)" --- ## Summary -ApolloCrossDev installs the Amiga NDK headers for versions 1.3, 3.9 and 3.2. -NDK 3.2 is the default. Choose the NDK that matches the oldest target OS of -the project. +The GCC-6.50 scripts of ApolloCrossDev install the Amiga NDK headers for +versions 3.2 (the default) and 1.3. They do not install NDK 3.9. For a +project that also targets AmigaOS 3.9, check by hand that every OS function +it calls exists on 3.9. ## Details @@ -30,10 +33,15 @@ the project. | NDK | Include path | Target OS | |---|---|---| -| 3.2 (default) | `/m68k-amigaos/ndk32-include/Include_H` | `amigaos-3.2`, `apolloos-r10` | -| 3.9 | `/m68k-amigaos/ndk39-include` | `amigaos-3.9` | +| 3.2 (default) | `/m68k-amigaos/ndk-include` | `amigaos-3.2`, `apolloos-r10`, and `amigaos-3.9` with a manual check | | 1.3 | `/m68k-amigaos/ndk13-include` | none of the supported targets | +`ndk-include` is also on GCC's built-in include path, so a wrong or missing +`-I` for the NDK does not give an error: GCC silently uses NDK 3.2. The +Makefile template up to ApolloCrossDev pull request #38 (2026-11-25) pointed +at `ndk32-include/Include_H` and an `ndk39-include` line; neither folder is +installed. + ApolloCrossDev builds its runtime libraries against NDK 3.2 (`make all NDK=3.2`). @@ -41,9 +49,10 @@ ApolloCrossDev builds its runtime libraries against NDK 3.2 - Targets `amigaos-3.2` and/or `apolloos-r10`: NDK 3.2. ApolloOS R10 aims to be compatible with AmigaOS 3.2.x. -- Target includes `amigaos-3.9`: NDK 3.9, so functions that AmigaOS 3.9 does - not have are not declared, and calling them gives a compiler warning or - error instead of a crash at run time. +- Target includes `amigaos-3.9`: NDK 3.2, as there is no NDK 3.9 install. + The compiler then does not catch functions that AmigaOS 3.9 lacks, so + check each OS function the program calls against + `os/apolloos-and-amigaos-on-v4.md`. ### Why it matters @@ -58,6 +67,8 @@ run time when it calls a function that 3.9 does not have. ## Do / Don't -- DO pick the NDK from the project's target OS list. +- DO use `ndk-include` (NDK 3.2) for every supported target. +- DON'T point `-I` at `ndk32-include` or `ndk39-include`: they are not + installed, and GCC falls back to NDK 3.2 without a warning. - DON'T call a function that is missing in one of the project's target operating systems. diff --git a/tools/compile-snippets.sh b/tools/compile-snippets.sh index ded45c7..06b3b0b 100755 --- a/tools/compile-snippets.sh +++ b/tools/compile-snippets.sh @@ -27,7 +27,7 @@ AS="${AS:-$PREFIX/bin/vasmm68k_mot}" OPTS="-O2 -m68080 -m68881 -ffast-math -fomit-frame-pointer" AOPTS="-m68080 -m68881 -opt-speed" -INCL="-I$PREFIX/$TARGET/libnix/include -I$PREFIX/$TARGET/sys-include -I$PREFIX/$TARGET/ndk32-include/Include_H" +INCL="-I$PREFIX/$TARGET/libnix/include -I$PREFIX/$TARGET/sys-include -I$PREFIX/$TARGET/ndk-include" [ -n "$APOLLOLIB" ] && INCL="$INCL -I$APOLLOLIB" AINCL="-I$PREFIX/$TARGET/DevPac/Include" diff --git a/tools/v4/v4test.py b/tools/v4/v4test.py index a06d0c9..b7e56d4 100644 --- a/tools/v4/v4test.py +++ b/tools/v4/v4test.py @@ -262,7 +262,7 @@ def build(test_dir, name, variant, opts, out_dir): prefix = acd_prefix() tgt = os.path.join(prefix, "m68k-amigaos") incl = ["-I" + os.path.join(tgt, d) for d in - ("libnix/include", "sys-include", "ndk32-include/Include_H")] + ("libnix/include", "sys-include", "ndk-include")] objs = [] for s in sorted(glob.glob(os.path.join(test_dir, "*.s"))): o = os.path.join(out_dir, os.path.basename(s)[:-2] + ".o")