Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@
/toolchain/pitfalls/calling-assembler-from-gcc.md @TuKo1982
/toolchain/pitfalls/float-settings-mismatch.md @TuKo1982
/toolchain/pitfalls/gcc-bbb-autoinc.md @TuKo1982
/toolchain/pitfalls/gcc-bbb-shift-wrong-code.md @TuKo1982
/toolchain/pitfalls/gcc-dbra-address-register-loop.md @TuKo1982
/toolchain/pitfalls/gcc-fsincos-ice.md @TuKo1982
/toolchain/pitfalls/gcc-misc-traps.md @TuKo1982
/toolchain/pitfalls/gcc-mrtd-calling-convention.md @TuKo1982
/toolchain/pitfalls/gcc-stackswap-defer-pop.md @TuKo1982
/toolchain/pitfalls/libnix-and-header-traps.md @TuKo1982
/toolchain/pitfalls/vasm-68080-encoding-issues.md @TuKo1982
Expand Down
44 changes: 29 additions & 15 deletions toolchain/compiler-variants.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sources:
- "github.com/WDrijver/ApolloCrossDev README.md"
- "github.com/WDrijver/ApolloCrossDev GCC-6.50-Base-Linux.sh, GCC-6.50-Stable-Linux.sh, GCC-6.50-Dev-Linux.sh, GCC-13.3.sh, GCC-15.2.sh"
- "WDrijver (ApolloCrossDev maintainer), 2026-11-02"
- "github.com/WDrijver/ApolloCrossDev pull requests #5 to #10, #12 and #13 (merged on 2026-11-03)"
---

## Summary
Expand All @@ -30,14 +31,16 @@ 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` | 7 (see below) | Default, latest |
| GCC-6.50-Dev | `GCC-6.50-Dev-Linux.sh`, `-MacOS.sh` | 13 (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-Base | `GCC-6.50-Base-Linux.sh`, `-MacOS.sh` | none | Original baseline (November 2024) |

Each variant installs into its own folder `Compilers/<variant>`, so several
can be installed side by side.
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 `Compilers/_archives/patches/`:
Patches applied by GCC-6.50-Dev on 2026-11-03, from
`Compilers/_archives/patches/`:

| Patch | Author |
|---|---|
Expand All @@ -48,18 +51,29 @@ Patches applied by GCC-6.50-Dev, from `Compilers/_archives/patches/`:
| `opt-shift-lsr-signext-fix.patch` | Morten |
| `postinc-size-fix.patch` | Morten |
| `double-indirect-fix.patch` | Marcel (RedBug) |

GCC-6.50-Stable applies `q2g-stable.patch` and the four patches by Morten.

### Known GCC bugs with pending fixes

| Bug | Fix | State on 2026-11-03 |
|---|---|---|
| bbb pass counts a post-increment twice | ApolloCrossDev pull request #5 | Open: see [gcc-bbb-autoinc.md](pitfalls/gcc-bbb-autoinc.md) |
| 16-bit loop counter in an address register runs away | ApolloCrossDev pull request #7 | Open: see [gcc-dbra-address-register-loop.md](pitfalls/gcc-dbra-address-register-loop.md) |

Until a fix is merged and you rebuild the toolchain, the matching pitfall
applies to every GCC 6.5 variant.
| `bbb-autoinc-direct-add-fix.patch` | TuKo1982 |
| `opt-shift-jump-fix.patch` | TuKo1982 |
| `opt-elim-shift-count-fix.patch` | TuKo1982 |
| `m68k-mrtd-call-pop.patch` | TuKo1982 |
| `m68k-sincos-constraint-fix.patch` | TuKo1982 |
| `m68k-dbra-hi-addrreg-fix.patch` | TuKo1982 |

GCC-6.50-Stable applies `q2g-stable.patch` and the four patches by Morten,
none by TuKo1982.

### Known GCC bugs and which variant fixes them

| Bug | Pitfall | Dev | Stable | Base |
|---|---|---|---|---|
| bbb pass counts a post-increment twice | [gcc-bbb-autoinc.md](pitfalls/gcc-bbb-autoinc.md) | Fixed | Not fixed | Not fixed |
| bbb pass deletes needed instructions around shifts | [gcc-bbb-shift-wrong-code.md](pitfalls/gcc-bbb-shift-wrong-code.md) | Fixed | Not fixed | Not fixed |
| 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 |

"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.

### 68080 instructions the compiler generates

Expand Down
13 changes: 8 additions & 5 deletions toolchain/pitfalls/gcc-bbb-autoinc.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ related:
- toolchain/c-compiler-options.md
sources:
- "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 9.2"
- "github.com/WDrijver/ApolloCrossDev pull request #5 (open on 2026-11-03)"
- "github.com/WDrijver/ApolloCrossDev pull request #5 (merged on 2026-11-03)"
- "github.com/WDrijver/ApolloCrossDev GCC-6.50-Dev-Linux.sh, GCC-6.50-Stable-Linux.sh (after pull request #13, 2026-11-03)"
---

## Symptom
Expand All @@ -27,8 +28,9 @@ at `-O2`.

Bebbo's bbb optimizer pass (`-fbbb=i`, auto-increment) turns it into
`cmp.b #N,(a0)+` / Scc / `pea (3,a0,d0.w)`, which counts the post-increment
twice. The bug is in GCC 6.5 as built by ApolloCrossDev; the fix is
ApolloCrossDev pull request #5, still open on 2026-11-03.
twice. The bug is in GCC 6.5 as built by ApolloCrossDev. The fix,
ApolloCrossDev pull request #5, was merged on 2026-11-03. Only GCC-6.50-Dev
applies it.

## Wrong

Expand All @@ -41,7 +43,8 @@ const char *skip(const char *p)

## Right

Until PR #5 is merged into the ApolloCrossDev build you use:
Use GCC-6.50-Dev installed on or after 2026-11-03. With GCC-6.50-Stable,
GCC-6.50-Base or an older install:

- check the generated code (see Detection), and
- rewrite the expression so the test and the addition are separate, and
Expand All @@ -67,4 +70,4 @@ Scc.
| Claim | Source | Core | Method |
|---|---|---|---|
| Miscompile as described | (M) | any | Real project code, `-S` output |
| Fix in ApolloCrossDev PR #5 | (SRC) | any | Pull request |
| Fix in ApolloCrossDev PR #5, applied by the Dev scripts only | (SRC) | any | Pull request, install scripts |
79 changes: 79 additions & 0 deletions toolchain/pitfalls/gcc-bbb-shift-wrong-code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: GCC 6.5 bbb pass deletes needed instructions around shifts
type: pitfall
owner: TuKo1982
status: draft
origin: ai-assisted
basis: [measured, source-code]
components: [toolchain]
applies_to:
core: "any"
os: [any]
tags: [gcc, bbb, optimizer, shift, miscompile]
related:
- toolchain/compiler-variants.md
- toolchain/pitfalls/gcc-bbb-autoinc.md
sources:
- "github.com/WDrijver/ApolloCrossDev pull request #10 (merged on 2026-11-03)"
- "github.com/WDrijver/ApolloCrossDev GCC-6.50-Dev-Linux.sh, GCC-6.50-Stable-Linux.sh (after pull request #13, 2026-11-03)"
---

## Symptom

Wrong results, without any warning, at `-O2` and `-Os`, for 68000 to 68080:

- An addition or index that uses a shift count gives a result that depends
on what was left in the register. A typical victim is a `BYTE` or `WORD`
loop counter used both as a shift count and as an array index.
- A table-driven CRC loses its high byte (seen with `-O2 -m68080`).

## Cause

Two backward scans in bebbo's bbb pass (`bbb-opts.c`) delete instructions
that are still needed:

| Pass | What goes wrong |
|---|---|
| `opt_elim_dead_assign` (`-fbbb=e`) | Drops the `ext.l` of a shift count, because the shift only reads the low bits. It does not check that the register is dead after the shift, and its scan crosses jumps |
| `opt_shift` (`-fbbb=h`) | Deletes a `move.w dy,dx` in front of a conditional branch although the other path reads `dx`: the scan stops at labels but not at jumps |

The fixes, `opt-elim-shift-count-fix.patch` and `opt-shift-jump-fix.patch`
from ApolloCrossDev pull request #10, were merged on 2026-11-03.
Only GCC-6.50-Dev applies them.

## Wrong

```c
int f(const short *p, int x)
{
int n = *p;
return (x << n) + n; /* add.l reads a high word that was never set */
}
```

```asm
move.w (a0),d1
move.l (8,sp),d0
lsl.l d1,d0 ; ext.l d1 was deleted before this
add.l d1,d0 ; uses all 32 bits of d1
```

## Right

Use GCC-6.50-Dev installed on or after 2026-11-03. With GCC-6.50-Stable,
GCC-6.50-Base or an older install, compile the affected file with `-fbbb=-`,
or leave the letter of the pass (`e` or `h`) out of `-fbbb=`.

## Detection

Compile with `-S` and look for a register that is loaded with `move.w`,
used as a shift count and then used as a 32-bit value with no `ext.l` in
between. Compare the results with the same code built with `-fbbb=-`.

## Evidence

| Claim | Source | Core | Method |
|---|---|---|---|
| Shift-count case: up to 20000 wrong results per function before the fix, 0 after | (M) | any | 20000 calls with garbage in the scratch registers, `-O2`/`-Os` 68020 and `-O2` 68080, on a V4 |
| CRC case: 99612 of 100000 calls wrong before the fix, 0 after | (M) | any | `-O2 -m68080` and `-O2 -m68020 -mtune=68080`, on a V4 |
| `-fbbb=-` or leaving out the pass letter hides each bug | (SRC) | any | ApolloCrossDev PR #10 |
17 changes: 11 additions & 6 deletions toolchain/pitfalls/gcc-dbra-address-register-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ related:
- toolchain/compiler-variants.md
sources:
- "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 9.2"
- "github.com/WDrijver/ApolloCrossDev pull request #7 and github.com/WDrijver/gcc issue #1 (open on 2026-11-03)"
- "github.com/WDrijver/ApolloCrossDev pull requests #7 and #12 (merged on 2026-11-03)"
- "github.com/WDrijver/gcc issue #1 (open on 2026-11-03)"
---

## Symptom
Expand All @@ -30,9 +31,11 @@ which is a 32-bit operation on an address register. After ivopts builds a
UWORD counter from a pointer, the high word never reaches −1. The bug is
GCC's, not the core's: the same loop also runs away on a real 68060.

The fix (`tst.w An; subq.w #1,An; jne`, also faster) is ApolloCrossDev pull
request #7, still open on 2026-11-03. The pattern is latent in GCC 15.2 too
(WDrijver/gcc issue #1).
The fix (`tst.w An; subq.w #1,An; jne`, also faster) is
`m68k-dbra-hi-addrreg-fix.patch` from ApolloCrossDev pull request #7, merged
on 2026-11-03. A later merge dropped the step that applies it; it was put
back in GCC-6.50-Dev the same day. GCC-6.50-Stable and GCC-6.50-Base do not
apply it. The pattern is latent in GCC 15.2 too (WDrijver/gcc issue #1).

## Wrong

Expand All @@ -49,7 +52,9 @@ this pattern:

## Right

Until PR #7 is in your ApolloCrossDev build:
Use GCC-6.50-Dev installed on or after 2026-11-03 (its install log shows
"Applying the m68k dbra HI address-register fix"). With another variant or
an older install:

- compile with `-fno-ivopts` (3–6 % slower on the tested code), or
- mark the affected function `__attribute__((noinline))`.
Expand All @@ -65,4 +70,4 @@ Compile with `-S` and look for `subq.w #1,aN` followed by `cmp.w #-1,aN`.
| Loop runs away on Fast RAM | (M) | any | Real project, V4 |
| `tst.w An` tests only bits 15–0; `subq` to An leaves the CCR alone | (M) | Core 12841 | Self-checking test tstan, 252 checks x 3 passes; 0 failures on the 080 and on a real 68060 |
| The old GCC loop also runs away on a real 68060 | (M) | 68060 | Same test |
| Fix in ApolloCrossDev PR #7 | (SRC) | any | Pull request |
| Fix in ApolloCrossDev PR #7, applied by the Dev scripts | (SRC) | any | Pull requests, install scripts |
66 changes: 66 additions & 0 deletions toolchain/pitfalls/gcc-fsincos-ice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: GCC 6.5 crashes on sinf and cosf of the same value with -ffast-math
type: pitfall
owner: TuKo1982
status: draft
origin: ai-assisted
basis: [measured, source-code]
components: [toolchain]
applies_to:
core: "any"
os: [any]
tags: [gcc, fpu, fsincos, fast-math, ice]
related:
- toolchain/compiler-variants.md
- toolchain/c-compiler-options.md
sources:
- "github.com/WDrijver/ApolloCrossDev pull request #8 (merged on 2026-11-03)"
- "github.com/WDrijver/ApolloCrossDev GCC-6.50-Dev-Linux.sh (2026-11-03)"
---

## Symptom

The compiler stops with:

```text
internal compiler error: in print_operand_address2, at config/m68k/m68k.c:5457
```

It shows with `-m68881 -ffast-math` on 68020, 68030 and 68080 targets, when
a function computes `sinf(x)` and `cosf(x)` of the same value under some
register pressure. 68040 and 68060 targets don't hit it: GCC does not use
`fsincos` for them.

## Cause

In `m68k.md`, the input operand of `sincos<mode>2_68881` has an empty
constraint. When the pointer in its address gets no hard register, the
operand becomes a memory-indirect address that the backend rejects.

The fix, `m68k-sincos-constraint-fix.patch` from ApolloCrossDev pull
request #8, was merged on 2026-11-03. Only GCC-6.50-Dev applies it. It
changes no code that compiled before: 69 C files with 3 flag sets gave
byte-identical assembly.

## Wrong

```c
float fx = sinf(w->heading), fy = -cosf(w->heading); /* ICE with -ffast-math */
```

## Right

Use GCC-6.50-Dev installed on or after 2026-11-03. With another variant,
compiling the affected file without `-ffast-math` should avoid the `fsincos`
pattern (not yet tested).

## Detection

The ICE above, with `-m68881 -ffast-math` in the flags.

## Evidence

| Claim | Source | Core | Method |
|---|---|---|---|
| ICE before the fix, builds with it | (M) | any | Failing project file and a 30-line reproducer, rebuilt cc1 |
| No other code change | (M) | any | 69 C files x 3 flag sets, byte-identical assembly |
68 changes: 68 additions & 0 deletions toolchain/pitfalls/gcc-mrtd-calling-convention.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: GCC 6.5 crashes or miscompiles with -mrtd
type: pitfall
owner: TuKo1982
status: draft
origin: ai-assisted
basis: [measured, source-code]
components: [toolchain]
applies_to:
core: "any"
os: [any]
tags: [gcc, mrtd, rtd, calling-convention, ice, miscompile]
related:
- toolchain/compiler-variants.md
- toolchain/pitfalls/calling-assembler-from-gcc.md
sources:
- "github.com/WDrijver/ApolloCrossDev pull request #9 (merged on 2026-11-03)"
- "github.com/WDrijver/ApolloCrossDev GCC-6.50-Dev-Linux.sh (2026-11-03)"
---

## Symptom

With `-mrtd` (the callee pops its stack arguments with `rtd #n`), the first
call to any function with stack arguments stops the compiler, at every `-O`
level:

```text
internal compiler error: in invalid_rtx_rtx_rtx_rtx_rtx, at ./insn-target-def.h:130
```

## Cause

The m68k backend of GCC 6.5 had no `call_pop` patterns. Two more defects
showed once those were added: bebbo's bbb pass did not count the stack
adjustment inside such a call (stack pointer wrong at `rts` in functions
without a frame pointer), and the cc0 tracking dropped a `tst.l d0` after a
call (seen at `-Os`).

The fix, `m68k-mrtd-call-pop.patch` from ApolloCrossDev pull request #9, was
merged on 2026-11-03. Only GCC-6.50-Dev applies it.

## Wrong

```sh
m68k-amigaos-gcc -O2 -m68080 -mrtd ... # GCC-6.50-Base or -Stable: ICE
```

## Right

- Use `-mrtd` only with GCC-6.50-Dev installed on or after 2026-11-03.
- `-mrtd` changes the calling convention: build everything you link with it
(startup code, libnix or clib2, static libraries). GCC may call
`memcpy`/`memset` for block copies, so they must come from a C library
built with `-mrtd`. libgcc helpers such as `__udivdi3` keep the normal
convention, so an unmodified libgcc works.

## Detection

The ICE above, or a link of `-mrtd` objects with a C library that was not
built with `-mrtd`.

## Evidence

| Claim | Source | Core | Method |
|---|---|---|---|
| ICE on every call before the fix | (M) | any | Test case in ApolloCrossDev PR #9 |
| Correct with the fix | (M) | any | Test program built entirely with `-mrtd`, 10 tests, `-O0` to `-O3` and `-Os`, 68020/68040/68080, under vamos and on a V4 (3 rounds) |
| No change in code built without `-mrtd` | (M) | any | 400 C and 38 C++ files, byte-identical assembly |
Loading
Loading