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
2 changes: 2 additions & 0 deletions .github/workflows/kb-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,5 @@ jobs:
- run: python tools/extract_compile_blocks.py
- name: Compile kb:compile blocks
run: sh tools/compile-snippets.sh
- name: Check emitted code (kb:expect)
run: python tools/check_emitted_code.py
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ don't use.
first line: `; kb:compile` (assembly) or `// kb:compile` (C, C++).
CI compiles each block on its own to an object file (no linking), so a
`kb:compile` block must be a complete source file with its own includes.
- A claim about the code the toolchain emits gets a check in its
`kb:compile` block: comment lines right after the first line, in the
block's comment style.
- `kb:expect <text>` / `kb:expect-not <text>`: for C and C++ the text
must, or must not, occur in GCC's `-S` output (runs of spaces count as
one). For assembly the text is hex bytes (`71f1`, `7104 4a46`) that
must, or must not, occur in vasm's `-Fbin` output, so the block must not
reference outside symbols.
- `kb:opts <flags>`: build with these code-generation flags instead of
the standard ones (for example `-O2 -m68080` without `-m68881`).
- A check that fails means the text no longer matches the toolchain:
update the text, not only the check.

---

Expand Down Expand Up @@ -351,6 +363,8 @@ Every pull request runs (scripts in `tools/`):
- Compilation of all `kb:compile` code blocks with ApolloCrossDev
(GCC-6.50-Dev, flags as in its Makefile template; see
`tools/compile-snippets.sh`)
- `check_emitted_code.py`: the `kb:expect` and `kb:expect-not` checks of
those blocks (section 6)

On every merge to `main`, `generate_codeowners.py` regenerates
`.github/CODEOWNERS`. Fixed owners for files without frontmatter go in
Expand Down
36 changes: 34 additions & 2 deletions toolchain/compiler-variants.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,40 @@ bug.

### 68080 instructions the compiler generates

According to the ApolloCrossDev README, the patched GCC 6.5 generates these
68080 instructions: `cmpiw.l`, `addiw.l`, `dbra.l`, `moviw.l`, `bcc.s`.
The ApolloCrossDev README lists `cmpiw.l`, `addiw.l`, `dbra.l`, `moviw.l`
and `bcc.s+`. The GCC source has output patterns for three of them (SRC,
WDrijver/gcc branches `gcc-stable` and `68080regs`, 2026-11-08):

| Instruction | GCC emits it for | Where |
|---|---|---|
| `cmpiw.l` | comparing a long with a 16-bit immediate | `m68k.md` |
| `addiw.l` | adding a 16-bit immediate to a long | `m68k.c` |
| `dbra.l` (`dbral`) | a loop with a 32-bit counter | `m68k.md` |

DON'T expect `moviw.l` or `bcc.s+` from GCC. Neither the GCC branches nor
the patches in ApolloCrossDev's `Compilers/_archives/patches/` have a
pattern for them; GCC-6.50-Dev emits a 6-byte `move.l #imm` for a 16-bit
constant. Write them in assembly when you need them. CI checks this:

```c
// kb:compile
// kb:expect cmpiw.l #1000,
// kb:expect addiw.l #1000,
// kb:expect dbral
// kb:expect move.l #1000,d0
// kb:expect-not moviw
int is_k(long x) { return x == 1000; }
void add_k(long *p) { *p += 1000; }
long k(void) { return 1000; }
long sum(const long *p, long n)
{
long s = 0, i;
for (i = 0; i < n; i++)
s += p[i];
return s;
}
```

Planned, not yet supported: `mov3q`, `clr.q`, `move2.b`, `movs.b/w`,
`movz.b/w`.

Expand Down
4 changes: 3 additions & 1 deletion toolchain/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ The `Docs/` folder of ApolloCrossDev contains `AC68080PRM.pdf`,
### In this knowledge base's CI

`kb:compile` code blocks are built with GCC-6.50-Dev and the standard flags,
see `tools/compile-snippets.sh`.
see `tools/compile-snippets.sh`. Their `kb:expect` lines check the code GCC
and vasm emit, see `tools/check_emitted_code.py` and `CONTRIBUTING.md`
section 6.

## Differences from AmigaOS / AROS / 68060

Expand Down
20 changes: 20 additions & 0 deletions toolchain/pitfalls/float-settings-mismatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ m68k-amigaos-gcc lib.o app.o -noixemul -o app # mixed
- Compile one float function with `-S` and check for `fmul`/`fadd` instead
of calls.

CI checks both cases on every run. With the standard flags
(`-m68080 -m68881`) GCC emits an FPU multiply:

```c
// kb:compile
// kb:expect fsmul.s
// kb:expect-not ___mulsf3
float scale(float a, float b) { return a * b; }
```

With `-m68080` alone it calls the soft-float routine:

```c
// kb:compile
// kb:opts -O2 -m68080 -ffast-math -fomit-frame-pointer
// kb:expect ___mulsf3
// kb:expect-not fsmul
float scale(float a, float b) { return a * b; }
```

## Evidence

| Claim | Source | Core | Method |
Expand Down
24 changes: 24 additions & 0 deletions toolchain/pitfalls/vasm-68080-encoding-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ Assemble with `-L file.lst` and check:
- odd displacement bytes in `.s` branches (the `.s+` form);
- `(pc)` source operands on AMMX instructions.

CI checks three rows of the table with the vasm that ApolloCrossDev ships
and the standard flags (`-m68080 -m68881 -opt-speed`). When a vasm update
changes one of these encodings, the check fails and this table needs
updating:

```asm
; kb:compile
; add.l b1,d0: a lone BANK word $71F1 (SS not 0).
; tst e6: BANK $7104 (AA only), then tst.w d6.
; add.l #imm,En: BANK $7101 (BB only).
; Out-of-range bra.s: bra.s+ with odd byte $49 (+200).
; kb:expect 71f1
; kb:expect 7104 4a46
; kb:expect 7101
; kb:expect 6049
section code,code
add.l b1,d0
tst e6
add.l #5,e1
bra.s far
ds.b 200
far: rts
```

## Evidence

| Claim | Source | Core | Method |
Expand Down
91 changes: 91 additions & 0 deletions tools/check_emitted_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python3
"""Check what ApolloCrossDev emitted for kb:compile snippets that carry
`kb:expect` or `kb:expect-not` lines (see CONTRIBUTING.md section 6).

Run after tools/compile-snippets.sh, which writes <name>.asm (the `-S` output
of a C or C++ snippet) or <name>.bin (the `-Fbin` output of an assembly
snippet) next to each snippet with expectations.

- C, C++: the text after `kb:expect` must occur in the assembly text; for
`kb:expect-not` it must not. Runs of spaces and tabs count as one space.
- Assembly: the text is hex bytes (spaces allowed, `$` and `0x` ignored) that
must, or must not, occur in the assembled bytes.

check_emitted_code.py [dir] default: build/snippets
"""
import os
import re
import sys

from kblib import ROOT

LINE_RE = re.compile(r"^\s*(?://|;)\s*kb:(expect|expect-not)\s+(.+?)\s*$")


def norm_text(s):
return re.sub(r"[ \t]+", " ", s)


def norm_hex(s):
h = re.sub(r"\$|0x|\s", "", s).lower()
if not re.fullmatch(r"(?:[0-9a-f]{2})+", h):
return None
return h


def check(src, out_dir):
with open(os.path.join(out_dir, src), encoding="utf-8") as f:
rules = [m.groups() for m in map(LINE_RE.match, f) if m]
if not rules:
return 0, 0
base, ext = os.path.splitext(src)
is_asm = ext == ".s"
out = os.path.join(out_dir, base + (".bin" if is_asm else ".asm"))
if not os.path.exists(out):
print(f"::error::{src}: no {os.path.basename(out)} (did it compile?)")
return len(rules), len(rules)
if is_asm:
with open(out, "rb") as f:
hay = f.read().hex()
else:
with open(out, encoding="utf-8") as f:
hay = norm_text(f.read())
failed = 0
for kind, text in rules:
if is_asm:
needle = norm_hex(text)
if needle is None:
print(f"::error::{src}: kb:{kind} '{text}' is not hex bytes")
failed += 1
continue
# Match on byte boundaries only.
found = any(i % 2 == 0 for i in
(m.start() for m in re.finditer(f"(?={needle})", hay)))
else:
found = norm_text(text) in hay
if found != (kind == "expect"):
what = "missing" if kind == "expect" else "present"
print(f"::error::{src}: kb:{kind} '{text}': {what} in "
f"{os.path.basename(out)}")
failed += 1
return len(rules), failed


def main():
out_dir = sys.argv[1] if len(sys.argv) > 1 else os.path.join(
ROOT, "build", "snippets")
total = failed = files = 0
for src in sorted(os.listdir(out_dir)):
if os.path.splitext(src)[1] not in (".c", ".cpp", ".s"):
continue
n, bad = check(src, out_dir)
if n:
files += 1
total += n
failed += bad
print(f"{total} expectation(s) in {files} snippet(s), {failed} failed")
sys.exit(1 if failed else 0)


if __name__ == "__main__":
main()
31 changes: 25 additions & 6 deletions tools/compile-snippets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
# ApolloLib folder (ApolloCrossDev Projects/ApolloLib), snippets can include
# "ApolloCrossDev_Lib.h".
#
# A snippet with a `kb:opts <flags>` line is built with those code-generation
# flags instead of the default OPTS (C, C++) or AOPTS (assembly). A snippet
# with `kb:expect` or `kb:expect-not` lines is also turned into the output
# that tools/check_emitted_code.py checks: assembly text (`-S`, <name>.asm)
# for C and C++, the assembled bytes (`-Fbin`, <name>.bin) for assembly.
#
# compile-snippets.sh [dir] compile all .c/.cpp/.s files in dir
# (default: build/snippets)
set -e
Expand All @@ -20,22 +26,35 @@ CXX="${CXX:-$PREFIX/bin/m68k-amigaos-g++}"
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"
[ -n "$APOLLOLIB" ] && INCL="$INCL -I$APOLLOLIB"
CFLAGS="${CFLAGS:--noixemul $OPTS -Wall $INCL}"
CXXFLAGS="${CXXFLAGS:--noixemul $OPTS -Wall $INCL}"
ASFLAGS="${ASFLAGS:--quiet -Fhunk -m68080 -m68881 -opt-speed -I$PREFIX/$TARGET/DevPac/Include}"
AINCL="-I$PREFIX/$TARGET/DevPac/Include"

cd "${1:-$(dirname "$0")/../build/snippets}"
fail=0
count=0
for f in *.c *.cpp *.s; do
[ -e "$f" ] || continue
count=$((count + 1))
opts=$(sed -n 's/^[[:space:];/]*kb:opts[[:space:]]*//p' "$f" | head -n 1)
expect=$(grep -c 'kb:expect' "$f" || true)
case "$f" in
*.c) $CC $CFLAGS -c "$f" -o "${f%.c}.o" || { echo "::error::$f failed"; fail=1; } ;;
*.cpp) $CXX $CXXFLAGS -c "$f" -o "${f%.cpp}.o" || { echo "::error::$f failed"; fail=1; } ;;
*.s) $AS $ASFLAGS "$f" -o "${f%.s}.o" || { echo "::error::$f failed"; fail=1; } ;;
*.c|*.cpp)
if [ "${f%.cpp}" != "$f" ]; then cc=$CXX; base=${f%.cpp}; flags=$CXXFLAGS
else cc=$CC; base=${f%.c}; flags=$CFLAGS; fi
flags="${flags:--noixemul ${opts:-$OPTS} -Wall $INCL}"
$cc $flags -c "$f" -o "$base.o" || { echo "::error::$f failed"; fail=1; continue; }
if [ "$expect" -gt 0 ]; then
$cc $flags -S "$f" -o "$base.asm" || { echo "::error::$f: -S failed"; fail=1; }
fi ;;
*.s)
base=${f%.s}
flags="${ASFLAGS:--quiet ${opts:-$AOPTS} $AINCL}"
$AS $flags -Fhunk "$f" -o "$base.o" || { echo "::error::$f failed"; fail=1; continue; }
if [ "$expect" -gt 0 ]; then
$AS $flags -Fbin "$f" -o "$base.bin" || { echo "::error::$f: -Fbin failed"; fail=1; }
fi ;;
esac
done
echo "Compiled $count file(s) in $(pwd)"
Expand Down
Loading