From 6af4cb7b66819be9de2aab7eb501092d26ac0b96 Mon Sep 17 00:00:00 2001 From: WDrijver Date: Sun, 8 Nov 2026 10:35:50 +0100 Subject: [PATCH] CI: check the code GCC and vasm emit (kb:expect) kb:compile blocks can now carry kb:expect / kb:expect-not lines (text in GCC's -S output, or hex bytes in vasm's -Fbin output) and kb:opts (other code-generation flags). tools/check_emitted_code.py runs after compiling in the compile-examples job. First checks: hard vs soft float (float-settings-mismatch), the 68080 instructions GCC emits (compiler-variants), four vasm encodings (vasm-68080-encoding-issues). compiler-variants: GCC has patterns for cmpiw.l, addiw.l and dbra.l only; moviw.l and bcc.s+ from the ACD README are not in the GCC source or the ACD patches. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/kb-checks.yml | 2 + CONTRIBUTING.md | 14 +++ toolchain/compiler-variants.md | 36 +++++++- toolchain/index.md | 4 +- toolchain/pitfalls/float-settings-mismatch.md | 20 ++++ .../pitfalls/vasm-68080-encoding-issues.md | 24 +++++ tools/check_emitted_code.py | 91 +++++++++++++++++++ tools/compile-snippets.sh | 31 +++++-- 8 files changed, 213 insertions(+), 9 deletions(-) create mode 100644 tools/check_emitted_code.py diff --git a/.github/workflows/kb-checks.yml b/.github/workflows/kb-checks.yml index 589cc2c..32638c0 100644 --- a/.github/workflows/kb-checks.yml +++ b/.github/workflows/kb-checks.yml @@ -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 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fe85268..169de6d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 ` / `kb:expect-not `: 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 `: 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. --- @@ -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 diff --git a/toolchain/compiler-variants.md b/toolchain/compiler-variants.md index 4426500..8c477fb 100644 --- a/toolchain/compiler-variants.md +++ b/toolchain/compiler-variants.md @@ -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`. diff --git a/toolchain/index.md b/toolchain/index.md index 277e2cf..d2d7bcd 100644 --- a/toolchain/index.md +++ b/toolchain/index.md @@ -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 diff --git a/toolchain/pitfalls/float-settings-mismatch.md b/toolchain/pitfalls/float-settings-mismatch.md index 32731eb..462b918 100644 --- a/toolchain/pitfalls/float-settings-mismatch.md +++ b/toolchain/pitfalls/float-settings-mismatch.md @@ -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 | diff --git a/toolchain/pitfalls/vasm-68080-encoding-issues.md b/toolchain/pitfalls/vasm-68080-encoding-issues.md index 85f9314..e11acf7 100644 --- a/toolchain/pitfalls/vasm-68080-encoding-issues.md +++ b/toolchain/pitfalls/vasm-68080-encoding-issues.md @@ -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 | diff --git a/tools/check_emitted_code.py b/tools/check_emitted_code.py new file mode 100644 index 0000000..2a2530b --- /dev/null +++ b/tools/check_emitted_code.py @@ -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 .asm (the `-S` output +of a C or C++ snippet) or .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() diff --git a/tools/compile-snippets.sh b/tools/compile-snippets.sh index c44e254..ded45c7 100755 --- a/tools/compile-snippets.sh +++ b/tools/compile-snippets.sh @@ -8,6 +8,12 @@ # ApolloLib folder (ApolloCrossDev Projects/ApolloLib), snippets can include # "ApolloCrossDev_Lib.h". # +# A snippet with a `kb:opts ` 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`, .asm) +# for C and C++, the assembled bytes (`-Fbin`, .bin) for assembly. +# # compile-snippets.sh [dir] compile all .c/.cpp/.s files in dir # (default: build/snippets) set -e @@ -20,11 +26,10 @@ 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 @@ -32,10 +37,24 @@ 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)"