Optimizations & Limitations
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 andPHX/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, andSTZfor constant-zero stores, directly instead of relying on the peephole optimizer. This mainly benefits unoptimized builds; at-Othe peepholes already produced these forms. - Jump-table switches
-
Dense
char-typedswitchstatements with enough cases are compiled to aJMP (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,
memcpyandmemsetuse 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 fuseda !* b !/ cthat 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--codesizeabove 100) the smallincsp1/incsp2C-stack drops are inlined when the model shows the inline body beats thejsr/rtscall 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 65c02would 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,STZandBITinstructions 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 ofBBRx/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. Seedesign/LYNX_CODEGEN_DESIGN.mdsection 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.