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:
- Pattern Matching - Apply peephole patterns (up to 10 passes)
- Z80-Specific Optimizations - Inline patterns for Z80 instructions
- Jump Threading - Thread through intermediate jumps
- Relative Jump Conversion - Convert jp to jr where possible
- djnz Optimization - Convert dec b; jr nz to djnz
- 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
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.
Related Projects
- 80un - Unpacker for CP/M compression and archive formats (LBR, ARC, squeeze, crunch, CrLZH)
- cpmdroid - Z80/CP/M emulator for Android with RomWBW HBIOS compatibility and VT100 terminal
- cpmemu - CP/M 2.2 emulator with Z80/8080 CPU emulation and BDOS/BIOS translation to Unix filesystem
- ioscpm - Z80/CP/M emulator for iOS and macOS with RomWBW HBIOS compatibility
- learn-ada-z80 - Ada programming examples for the uada80 compiler targeting Z80/CP/M
- mbasic - Modern MBASIC 5.21 Interpreter & Compilers
- mbasic2025 - MBASIC 5.21 source code reconstruction - byte-for-byte match with original binary
- mbasicc - C++ implementation of MBASIC 5.21
- mbasicc_web - WebAssembly MBASIC 5.21
- mpm2 - MP/M II multi-user CP/M emulator with SSH terminal access and SFTP file transfer
- romwbw_emu - Hardware-level Z80 emulator for RomWBW with 512KB ROM + 512KB RAM banking and HBIOS support
- scelbal - SCELBAL BASIC interpreter - 8008 to 8080 translation
- uada80 - Ada compiler targeting Z80 processor and CP/M 2.2 operating system
- uc80 - ANSI C compiler targeting Z80 processor and CP/M 2.2 operating system
- ucow - Unix/Linux Cowgol to Z80 compiler
- um80_and_friends - Microsoft MACRO-80 compatible toolchain for Linux: assembler, linker, librarian, disassembler
- uplm80 - PL/M-80 compiler targeting Intel 8080 and Zilog Z80 assembly language
- z80cpmw - Z80 CP/M emulator for Windows (RomWBW)
See Also
- upeep80 - Optimizer with 8080 input support
- Z80 CPU User Manual
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file upeepz80-0.2.2.tar.gz.
File metadata
- Download URL: upeepz80-0.2.2.tar.gz
- Upload date:
- Size: 33.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d60db2f109d9fe0e85c3feec05a90386ad4e957c8c2fdca700e1974bc1c2d271
|
|
| MD5 |
15be5cfdc0c46048f6f36f0e8571df60
|
|
| BLAKE2b-256 |
9c3d0a2e47d8a0b4a75db9d1b02a3753a2fb4a534f864f49c20112cff5df1b63
|
Provenance
The following attestation bundles were made for upeepz80-0.2.2.tar.gz:
Publisher:
publish.yml on avwohl/upeepz80
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
upeepz80-0.2.2.tar.gz -
Subject digest:
d60db2f109d9fe0e85c3feec05a90386ad4e957c8c2fdca700e1974bc1c2d271 - Sigstore transparency entry: 1194650778
- Sigstore integration time:
-
Permalink:
avwohl/upeepz80@b1c59adfb240659befc46d987f5fd53fb3aec2ef -
Branch / Tag:
refs/heads/master - Owner: https://github.com/avwohl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b1c59adfb240659befc46d987f5fd53fb3aec2ef -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file upeepz80-0.2.2-py3-none-any.whl.
File metadata
- Download URL: upeepz80-0.2.2-py3-none-any.whl
- Upload date:
- Size: 26.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02024b5523a54ab7c2cae57e30b261c7f6c23a8900ffda23df5824ec40e6d464
|
|
| MD5 |
8c0d15f3ffa6a8bdf7e09f48287d99b3
|
|
| BLAKE2b-256 |
687ca24c43d3f9fa949a1ef69f16979702e1843ae56a090b24f8ea4ad0c32dd9
|
Provenance
The following attestation bundles were made for upeepz80-0.2.2-py3-none-any.whl:
Publisher:
publish.yml on avwohl/upeepz80
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
upeepz80-0.2.2-py3-none-any.whl -
Subject digest:
02024b5523a54ab7c2cae57e30b261c7f6c23a8900ffda23df5824ec40e6d464 - Sigstore transparency entry: 1194650782
- Sigstore integration time:
-
Permalink:
avwohl/upeepz80@b1c59adfb240659befc46d987f5fd53fb3aec2ef -
Branch / Tag:
refs/heads/master - Owner: https://github.com/avwohl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b1c59adfb240659befc46d987f5fd53fb3aec2ef -
Trigger Event:
workflow_dispatch
-
Statement type: