Optimizations & Limitations

The Lynx-specific code-generation optimizations this SDK adds over stock cc65 2.19, and the deliberate hardware limitations it works within.

1. Lynx-specific code-generation optimizations

This tree contains a set of 65SC02/Lynx code-generation improvements over stock cc65 2.19. The full design, measurements, and verification plan are in design/LYNX_CODEGEN_DESIGN.md at the root of the source tree. Summary:

Runtime library fast paths

The hottest runtime routines (pushax, ldaxsp, staxsp, staspidx, pusha, enter) have 65C02 variants using (zp) addressing and PHX/PLX-style instructions, saving 1-4 cycles per call. These routines run on every C stack or pointer access, so the savings apply to nearly all compiled code.

Direct 65C02 emission in the compiler

The code generator emits (zp) indirect addressing for zero-index pointer loads and stores, and STZ for constant-zero stores, directly instead of relying on the peephole optimizer. This mainly benefits unoptimized builds; at -O the peepholes already produced these forms.

Jump-table switches

Dense char-typed switch statements with enough cases are compiled to a JMP (table,x) dispatch instead of a compare/branch cascade. Dispatch cost is constant regardless of the number of cases (measured 76 vs 93 average cycles on a 6-case switch). Word-sized switches keep the cascade.

Self-modifying-code runtime variants

Because the Lynx executes from RAM, memcpy and memset use self-modifying code (guarded by __LYNX__), measured 8-12 percent faster for sizes of 256/512 bytes and up. The SMC macros used are available to your own code as well, see the SMC macro documentation. Note that SMC routines are not reentrant; the same contract as the rest of the zero-page runtime applies.

Suzy hardware multiply and divide

Integer multiply and divide can run on Suzy's hardware math unit instead of software shift-add/shift-subtract loops. The standard *, / and % operators keep the stock software routines; Suzy math is requested per call site with the fork-specific opt-in operators !*, !/ and !% (same precedence, associativity and type rules as their standard counterparts), plus a fused a !* b !/ c that keeps a 32-bit intermediate product to avoid overflow. Small divisors are normalized to cut divide latency. A non-blocking start/poll/harvest variant of the same unit is available through <lynx/suzymath.h>; see How the Suzy math functions work in the function reference.

Cycle-cost model

The optimizer carries a per-instruction 65SC02 cycle model alongside the existing size model, so passes can prefer the faster of two encodings instead of guessing. At a speed-biased setting (-Oi, or --codesize above 100) the small incsp1/incsp2 C-stack drops are inlined when the model shows the inline body beats the jsr/rts call overhead, trading a few bytes for speed. At the default size setting nothing changes. This is rarely triggered in practice and is purely an internal optimization; see the design document, section 2.7.

2. Limitations

No Rockwell bit instructions (RMBx/SMBx/BBRx/BBSx)

The compiler and runtime never emit the Rockwell single-bit instructions, even though --cpu 65c02 would assemble them. They are not portable across the Lynx hardware family: the original Lynx (Lynx I) uses a 65SC02 core that omits them — the opcodes decode as NOP-like no-ops, so code relying on them silently misbehaves — whereas the later Lynx II Mikey revision implements the full Rockwell bit set and executes them as intended. Using them would mean either dropping Lynx I support or carrying two execution paths (runtime CPU detection plus duplicated, separately tested runtime routines and peepholes).

The payoff does not justify that cost. The instructions only help single-bit set/clear and test-and-branch on zero-page bytes, a thin slice of the instruction mix, and most C variables are not in zero page. Where the pattern does occur, the TRB/TSB, STZ and BIT instructions available on the shared 65SC02 baseline (and already used by the optimizer) capture the realistic benefit; the only thing they cannot replicate is the fused test-and-branch of BBRx/BBSx, worth roughly 2–3 cycles and 3 bytes per site — a sub-1–2 percent whole-program effect. A single 65SC02 build that runs unmodified on both Lynx I and Lynx II is worth more than that margin. See design/LYNX_CODEGEN_DESIGN.md section 3 for the full rationale.

3. Known compiler bugs (under audit)

A systematic correctness audit of the compiler (design/LYNX_COMPILER_AUDIT_DESIGN.md, harness in tests/compiler/) tracks miscompiles inherited from cc65 2.19. The authoritative list — every bug fixed in this fork and the known bugs still open, each with its workaround and its execution-checked regression test — is the Compiler bugs page.