Skip to main content

Universal peephole optimizer for Z80 compilers - pure Z80 assembly input/output

Project description

upeepz80 - Universal Peephole Optimizer for Z80

A language-agnostic peephole optimization library for Z80 compilers that generate pure Z80 assembly.

Overview

upeepz80 provides high-quality optimization passes for compilers targeting the Zilog Z80 processor. Unlike upeep80, this library expects pure Z80 mnemonics in lowercase as input (ld, jp, jr, etc.) and produces optimized Z80 assembly output with lowercase mnemonics.

If your compiler generates 8080 mnemonics (MOV, MVI, LXI, etc.) that need translation to Z80, use upeep80 instead.

Features

Peephole Optimizations

  • Pattern-based optimization on Z80 assembly
  • Redundant load/store elimination
  • Jump optimization (jp to jr conversion, jump threading)
  • djnz conversion (dec b; jr nz → djnz)
  • Stack operation combining (push/pop to ld conversions)
  • Dead store elimination
  • Tail call optimization (call x; ret → jp x)
  • Register copy optimization (push hl; pop de → ld d,h; ld e,l)

Z80-Specific Features

  • Relative jump optimization (jp → jr where in range)
  • djnz loop optimization
  • Z80 block instruction awareness
  • Direct ld de,(addr) usage (Z80-only instruction)

Installation

pip install upeepz80

Or for development:

git clone https://github.com/avwohl/upeepz80.git
cd upeepz80
pip install -e ".[dev]"

Usage

Basic Usage

from upeepz80 import optimize

# Optimize Z80 assembly code
assembly = """
    ld a,0
    push hl
    pop de
    jp LABEL
LABEL:
    ret
"""

optimized = optimize(assembly)
print(optimized)
# Output:
#     xor a          ; ld a,0 → xor a (smaller)
#     ld d,h         ; push/pop → register moves (faster)
#     ld e,l
#     ret            ; jp to next instruction eliminated

Using the Optimizer Class

from upeepz80 import PeepholeOptimizer

# Create optimizer
optimizer = PeepholeOptimizer()

# Optimize assembly code
optimized_asm = optimizer.optimize(assembly_text)

# Check statistics
print(f"xor a conversions: {optimizer.stats.get('xor_a', 0)}")
print(f"Jump threading: {optimizer.stats.get('jump_thread', 0)}")
print(f"djnz conversions: {optimizer.stats.get('djnz', 0)}")

Optimization Phases

The optimizer runs multiple phases:

  1. Pattern Matching - Apply peephole patterns (up to 10 passes)
  2. Z80-Specific Optimizations - Inline patterns for Z80 instructions
  3. Jump Threading - Thread through intermediate jumps
  4. Relative Jump Conversion - Convert jp to jr where possible
  5. djnz Optimization - Convert dec b; jr nz to djnz
  6. Dead Store Elimination - Remove unused stores

Architecture

upeepz80 is designed to be language-agnostic:

  • Works directly on Z80 assembly text
  • No knowledge of source language required
  • Pattern-based transformation engine
  • Zero runtime dependencies

Comparison with upeep80

Feature upeep80 upeepz80
Input 8080 or Z80 mnemonics Z80 mnemonics only
Output Z80 or 8080 (configurable) Z80 only
Translation 8080 → Z80 translation None needed
Use case Compilers generating 8080 code Compilers generating Z80 code

Choose upeepz80 if your compiler already generates Z80 mnemonics. Choose upeep80 if your compiler generates 8080 mnemonics.

Used By

  • uplm80 - PL/M-80 compiler for Z80 (after migration)
  • uada80 - Ada compiler for Z80 (after migration)

Development

Running Tests

pytest

Type Checking

mypy upeepz80

Code Formatting

black upeepz80
ruff check upeepz80

Performance

Benchmarks on typical compiler workloads:

  • Peephole optimization: ~50,000 instructions/second
  • Memory usage: Minimal (pattern-based, no large data structures)

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

This project is licensed under the GNU General Public License v2.0 - see LICENSE for details.

History

upeepz80 is a sibling project to upeep80, designed for compilers that generate native Z80 assembly. It shares the same optimization algorithms but removes the 8080 translation layer for cleaner, more efficient code when 8080 support isn't needed.

See Also

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

upeepz80-0.2.0.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

upeepz80-0.2.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file upeepz80-0.2.0.tar.gz.

File metadata

  • Download URL: upeepz80-0.2.0.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for upeepz80-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ee117af1e3e9596397220e7f9c212372819ffa1b4c5c6fff9c3528f3dd878d9d
MD5 ceedc9b6f709d7ee799037b30c4b1ed9
BLAKE2b-256 a4f95ab2d272e927b29ce04c91d5dd9422c0fbc10dc9fa1fe0d98936c26e37d9

See more details on using hashes here.

File details

Details for the file upeepz80-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: upeepz80-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for upeepz80-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5f7a502c61d4abd3a7d6061fe13aa74397c2a6cb97e3edb5bf02cf9d21bbbd0
MD5 e8575e3c9c178c72253ccbbba8876f2c
BLAKE2b-256 ca3d261f99502e885fd6aad96fc67fd048a42d66140abae1a986e7588b38baca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page