65816 IIgs Backend for LLVM-MOS.
Find a file
2026-05-26 19:00:55 -05:00
benchmarks Optimizations 2026-05-25 21:00:32 -05:00
compare Optimizations 2026-05-25 21:00:32 -05:00
demos Optimizations 2026-05-25 21:00:32 -05:00
docs Install cleaned up. 2026-05-26 19:00:55 -05:00
patches Checkpoint 2026-05-13 15:48:34 -05:00
runtime Optimizations 2026-05-25 21:00:32 -05:00
screenshots Optimizations 2026-05-25 21:00:32 -05:00
scripts Install cleaned up. 2026-05-26 19:00:55 -05:00
src Install cleaned up. 2026-05-26 19:00:55 -05:00
tests Install cleaned up. 2026-05-26 19:00:55 -05:00
.gitattributes ORCA/C demos seem to be working! 2026-05-18 14:43:35 -05:00
.gitignore Optimizations 2026-05-25 21:00:32 -05:00
bench_simple.s Checkpoint 2026-05-02 18:30:15 -05:00
LLVM_65816_DESIGN.md Checkpoint. 2026-04-25 17:07:28 -05:00
mame.ini Checkpoint 2026-05-13 15:48:34 -05:00
plugin.ini Checkpoint 2026-05-13 15:48:34 -05:00
README.md Install cleaned up. 2026-05-26 19:00:55 -05:00
SESSION_RECOVERY.md Optimizations 2026-05-25 21:00:32 -05:00
SESSION_STATE.md Initial check in. Lots of work yet to do. 2026-04-28 16:49:41 -05:00
setup.sh Install cleaned up. 2026-05-26 19:00:55 -05:00
STATUS.md Optimizations 2026-05-25 21:00:32 -05:00
ui.ini Checkpoint 2026-05-13 15:48:34 -05:00

llvm816

LLVM/Clang C compiler for the WDC 65816 / Apple IIgs.

Compiles C (and a minimal subset of C++) to native 65816 machine code, links to a relocatable OMF binary, and runs under MAME's apple2gs. Speed-tuned: matches or beats hand-written 65816 assembly on the tight loops in benchmarks like sumOfSquares, popcount, and strcpy.

What you get

  • clang --target=w65816 — full C99 + parts of C11, optimized at -O2 by default. Soft-float and soft-double included.
  • C standard library subsetstdio.h, stdlib.h, string.h, math.h, time.h, setjmp.h, etc. See runtime/include/ for the complete list.
  • link816 — relocating linker producing GS/OS-loadable OMF binaries (single- or multi-segment).
  • MAME integration scripts — compile, link, and run a program under MAME's apple2gs with one command.
  • Apple IIgs Toolbox bindings<iigs/toolbox_full.h> exposes ~1300 toolbox routines from 35 tool sets.

Quick start

After installation (see docs/INSTALL.md):

# Write a tiny C program that computes 1+2+...+10 = 55 and stores it.
cat > hello.c <<'EOF'
int main(void) {
    unsigned short x = 0;
    for (int i = 1; i <= 10; i++) x += i;        // x = 55 = 0x37
    // Write to a known 24-bit absolute address.  The compiler lowers
    // this to `sta long $025000` — no bank switching needed.  The MAME
    // test harness reads this cell to verify the program ran.
    *(volatile unsigned short *)0x025000 = x;
    while (1) {}
}
EOF

# Compile, link, run under MAME, check the result.
./tools/llvm-mos-build/bin/clang --target=w65816 -O2 -c hello.c -o hello.o
./tools/link816 -o hello.bin --text-base 0x1000 \
    runtime/crt0.o runtime/libc.o runtime/libgcc.o hello.o
bash scripts/runInMame.sh hello.bin --check 0x025000=0037

See docs/USAGE.md for a full walkthrough including multi-segment builds and the Apple IIgs Toolbox.

Project layout

runtime/         C standard library + crt0 startup
  src/           sources (C and .s)
  include/       headers
  *.o            built object files
src/             our LLVM/Clang sources (W65816 target backend)
  clang/         clang patches
  llvm/          LLVM patches + W65816 target
  link816/       relocating linker
patches/         patches against vanilla llvm-mos
scripts/         install scripts, MAME runners, benchmarks
tools/           installed compilers, MAME, ROMs, Calypsi (reference)
benchmarks/      cycle-count and instruction-count benchmarks
compare/         side-by-side asm vs Calypsi
docs/            this directory — INSTALL.md, USAGE.md, design notes

Status

Stable enough to build real programs. Per-call cycle measurements against commercial Calypsi 5.16, measured under MAME via emu.time() (IIgs slow-mode 1.023 MHz, -mllvm -w65816-dbr-safe-ptrs enabled):

Benchmark Ours Calypsi Ratio
bsearch 682 2,387 0.29×
dotProduct 1,534 5,712 0.27×
sumOfSquares 6,820 16,368 0.42×
bubbleSort 11,594 17,050 0.68×
djb2Hash 2,387 2,643 0.90×
memcmp 716 716 1.00×
strcpy 1,279 1,194 1.07×
popcount 1,705 1,534 1.11×
fib 12,106 10,912 1.11×
strLen 1,876 1,023 1.83×

Geomean: 0.74× Calypsi across this suite. Six of ten benches beat Calypsi outright; one ties exactly. Run scripts/benchCyclesPrecise.sh (ours) and scripts/benchCyclesCalypsi.sh (Calypsi) to reproduce.

On real programs:

  • Lua 5.1.5 (17K LoC, 24 source files) compiles + links clean. Object total 0.93× Calypsi.
  • CoreMark 1.0 (EEMBC standard benchmark) compiles + links clean. Object total 0.80× Calypsi.

See STATUS.md for full language and runtime feature coverage, and LLVM_65816_DESIGN.md for backend internals.

Documentation

License

Apache 2.0 (matching the LLVM project's license). See tools/llvm-mos/LICENSE.TXT after install.