LLVM-MOS v0.1.0: Clang C and C++ for the Atari 8-bit
At the end of February 2022, mysterymath announced on AtariAge the first official release (v0.1.0) of the LLVM-MOS toolchain. It is a 6502 back end for the Clang/LLVM compiler framework, and its SDK supports the Atari 8-bit computers from day one, alongside the Commodore 64 and a built-in 6502 simulator.
What's in the box
LLVM-MOS is a full modern toolchain rather than a single compiler:
- C and C++ compilers (Clang), with broad C99 and C++11 freestanding compatibility, including templates, virtual functions,
new/delete, RTTI and static constructors and destructors; - a GNU-assembler-compatible 6502 assembler with a complete macro system;
- the LLD linker, with linker scripts, link-time garbage collection, symbol maps and GNU ld compatibility;
- the ELF object format and the usual tools (
readelf,nmand so on); - an SDK with a small standard library: simple
printf,malloc/free,exit/atexit.
The announcement said the project also runs the GCC end-to-end regression suite on a simulated 6502, in addition to its unit tests, and noted that someone had already ported Rust to it.
How it generates 6502 code
The code generator works with A, X and Y plus 16 two-byte "imaginary registers" in zero page, which can be placed anywhere and do not need to be contiguous. The calling convention passes arguments in registers where it can. A whole-program "static stack" optimization finds functions that are never re-entered and gives them static frames, so programs without recursion or complex function pointers may not need a software stack at all. Link-time inlining works across the whole program, and library calls can sometimes disappear entirely.
The author was clear about the limits: no float/double, no C++ exceptions, no hosted C library, and no support for C99's 64 KiB locals requirement. He said much optimization work was still to come. Asked about newer CPUs, he said 65C02 support existed but did not yet use every CMOS improvement, and that the 65C816 is a much bigger job.
Tooling around it
Developers get Clang's error messages and IDE integration through a bundled clangd language server. A build can be a single command in the style of mos-<target>-clang++ -Os -o game game.cc. Later, Altirra 4.10 (January 2023) added initial support for DWARF5 debug information from LLVM-MOS .elf files, so you can debug at source level in the emulator.
Getting it
Releases and documentation are at github.com/llvm-mos/llvm-mos-sdk (v0.1.0 was the first tagged release; use the latest).
Credits
- AtariAge — LLVM-MOS Toolchain v0.1.0 — AtariAge forum members
- virtualdub.org — Altirra 4.10 change log (DWARF5 support for LLVM-MOS) — Screenshot from Avery Lee's Altirra page, virtualdub.org
