Peephole Optimizer

Post-compilation optimizer that applies target-specific sliding-window pattern matching to reduce instruction count. Operates on the generated assembly before it reaches the assembler.

Location: hcbcomp/peep.c / hcbcomp/peep.h

Z80 Optimizations

PatternBeforeAfterSaved
Local variable load
ld hl, -N
ex de, hl
push ix
pop hl
add hl, de
ld a, [hl]
inc hl
ld h, [hl]
ld l, a
ld l, [ix+N]
ld h, [ix+N+1]
7
Local store (immediate)
ld hl, -N / ex de, hl / push ix
pop hl / add hl, de / push hl
ld hl, IMM / pop de / ex de, hl
ld [hl], e / inc hl / ld [hl], d
ld hl, IMM
ld [ix+N], l
ld [ix+N+1], h
9

8086 Optimizations

PatternBeforeAfterSaved
LEA + deref chainlea ax,[bp-N]; mov bx,ax; mov ax,[bx]mov ax, [bp-N]2
Global dereferencemov ax,label; mov bx,ax; mov ax,[bx]mov ax, [label]2
Store immediate to locallea ax,[bp-N]; push ax; mov ax,IMM; pop bx; mov [bx],axmov word [bp-N], IMM4
Push/Pop copypush ax; pop bxmov bx, ax1

Typical Results

TargetReduction
Z80~32% fewer instruction lines
8086~28% fewer instruction lines