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
or b
push hl
pop de
jp LABEL
LABEL:
ret
"""
optimized = optimize(assembly)
print(optimized)
# Output:
# xor a ; ld a,0 → xor a (smaller): or b sets the flags xor a changes
# or b
# ld d,h ; push/pop → register moves (faster)
# ld e,l
# LABEL:
# ret ; jp to next instruction eliminated
Without the or b, ld a,0 stays: xor a also sets the flags, and the
caller this code returns to might read them.
For the PeepholeOptimizer class and its statistics, see docs/INTEGRATION.md.
Documentation
- docs/INTEGRATION.md: integration guide, the
PeepholeOptimizerclass, statistics, input format, migrating from upeep80 - docs/design.md: optimization phases, correctness, architecture, performance, comparison with upeep80, history
- docs/development.md: running the tests, type checking, code formatting, contributing
- docs/noopt.md:
;#noopt beginand;#noopt endregions, anddisable=to stop named rewrites - CHANGELOG.md: release history
License
This project is licensed under the GNU General Public License v2.0 - see LICENSE for details.
Related Projects
- 80un - Unpacker for the CP/M archive and compression formats LBR, ARC, squeeze, crunch, and CrLZH.
- cpmdroid - Z80/CP/M emulator for Android phones and tablets. It emulates the RomWBW HBIOS interface and a VT100 terminal.
- cpmemu - Z80/CP/M emulator for Linux and Windows, with Z80 and 8080 CPU cores. It translates the BDOS and BIOS calls of CP/M 2.2 programs to the host file system.
- ioscpm - Z80/CP/M emulator for iOS and macOS. It emulates the RomWBW HBIOS interface and runs CP/M 2.2 and CP/M 3.
- learn-ada-z80 - Collection of more than 90 Ada example programs for uada80, the Ada compiler for the Z80 processor and CP/M.
- mbasic - Python interpreter for MBASIC 5.21, the Microsoft BASIC-80 for CP/M. Two compiler backends compile the programs to CP/M .COM files or to JavaScript.
- mbasic2025 - Reconstruction of the lost source code of MBASIC 5.21, the Microsoft BASIC-80 for CP/M. The MACRO-80 source code assembles to a binary that matches mbasic.com byte for byte.
- mbasicc - C++17 interpreter for MBASIC 5.21, the Microsoft BASIC-80 for CP/M. It runs on Linux and macOS.
- mbasicc_web - Web browser interpreter for MBASIC 5.21, the Microsoft BASIC-80 for CP/M. Emscripten compiles the mbasicc interpreter to WebAssembly.
- mpm2 - Z80 emulator for MP/M II, the multi-user CP/M operating system. Users connect over SSH, and SFTP clients transfer files.
- romwbw_emu - Hardware-level Z80/CP/M emulator for Linux and macOS. It emulates the RomWBW HBIOS interface and switches banks in 512 KB of ROM and 512 KB of RAM.
- scelbal - Floating-point BASIC interpreter for the 8080 processor and CP/M. A translator converts the original 8008 source code to 8080 source code.
- uada80 - Ada compiler for the Z80 processor and CP/M 2.2. It compiles a subset of Ada 2012 to CP/M .COM files.
- uc80 - C compiler for the Z80 processor and CP/M. It optimizes for small code size.
- ucow - Cowgol compiler for the Z80 processor and CP/M. It runs on Linux in Python.
- um80_and_friends - Linux toolchain that is compatible with Microsoft MACRO-80. It has an assembler, a linker, a librarian, and a disassembler.
- uplm80 - PL/M-80 compiler for the Z80 processor and CP/M. It writes Intel 8080 and Zilog Z80 assembly language.
- z80cpmw - Z80/CP/M emulator for Windows. It emulates the RomWBW HBIOS interface and boots CP/M from disk images.
See Also
- upeep80 - Optimizer with 8080 input support
- Z80 CPU User Manual
Metadata
Release files for upeepz80 0.2.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| upeepz80-0.2.8.tar.gz | 90.4 kB | With provenance | Details for upeepz80-0.2.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| upeepz80-0.2.8-py3-none-any.whl | Python 3 | none | any | Details for upeepz80-0.2.8-py3-none-any.whl |
Total release size: 149.5 kB
Release files / upeepz80-0.2.8.tar.gz
| Download URL | upeepz80-0.2.8.tar.gz |
|---|---|
| Size | 90.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5330e5102731a3d2d3750ffe6d1fc2a5412dee3a94b7de6759d57bd1c89609b7
|
|
BLAKE2b-256 checksum How to use checksums |
dabd8ed40e1e034233d17c7aac99253d8eb0988f81ca89e47a5b6869bc21c67d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logRelease files / upeepz80-0.2.8-py3-none-any.whl
| Download URL | upeepz80-0.2.8-py3-none-any.whl |
|---|---|
| Size | 59.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7f761b52daa549bc90fd3c237f4c53813fd00f697545d33b50d74d79e018e49e
|
|
BLAKE2b-256 checksum How to use checksums |
0a01f3248c3c38fbbb4b9724cd6f7da0f50c0c1965adeb4e1fd6417fa2658b4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logSecurity for upeepz80 0.2.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for upeepz80-0.2.8.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.