Skip to main content

upeep386 — i386 NASM optimizer toolkit

Language-agnostic NASM-text optimizer for compilers targeting the Intel 386 (x86-32) processor. Three composable passes:

  • peephole — pattern-based instruction rewriting to fixed point (binop_collapse, setcc_jcc_collapse, mov_zero_to_xor, …).
  • asm_dce — call/reference-graph dead-code elimination, walking reachability from _start / _main over the call graph of top-level functions and data labels.
  • libc_split — parser for a monolithic libc.asm with per- function dep tracking, so a host compiler can include only the transitive closure of routines the user code references.

Sibling to upeepz80 (Z80 peephole) and upeep80 (8080 peephole); same split pattern — once the optimizer's pattern set crystallized inside a host compiler, it became a standalone package shared across compilers for the same target.

Origin

Extracted from uc386 (the C23 → i386/DOS compiler) for shared use by ucpp386 (the C++ sibling). Both compilers emit NASM in the same dialect and benefit from the same rewrites — vtables and templates in ucpp386 make the asm-DCE cascade and the inline-load collapses more important, not less, than they are for C.

Install

pip install upeep386

Or from source:

pip install -e .

Quick start

from upeep386 import optimize, dce, parse_libc

asm = compiler.emit(...)
asm = optimize(asm)          # peephole patterns to fixed point
asm = dce(asm)               # drop unreachable functions / labels
# parse_libc(libc_asm) lets the host include only the libc
# routines the user actually references.

Pattern list (current)

Runs to fixed point. Patterns the optimizer applies:

  • dead_after_terminator — drop instructions between unconditional jmp/ret and the next label/directive.
  • jmp_to_next_label — drop jmp X immediately followed by X:.
  • binop_collapse — replace 4-line stack-machine right-operand transfer with a single mov ecx, src.
  • store_collapse — drop push/pop pair around a store when src is a single-instruction load.
  • leave_collapsemov esp, ebp; pop ebpleave.
  • imm_store_collapsemov eax, IMM; mov [addr], eax; mov eax, Xmov dword [addr], IMM; mov eax, X.
  • setcc_jcc_collapse — drop the setCC al; movzx eax, al; test eax, eax; jz/jnz boolean-then-branch sequence into a single conditional jump.
  • push_immediatemov eax, IMM; push eaxpush IMM when the next instruction overwrites EAX.
  • ecx_binop_collapsemov ecx, src; OP eax, ecxOP eax, src.
  • mov_zero_to_xormov reg, 0xor reg, reg (when flag-safe).
  • store_load_collapse — drop the redundant load after a store of the same register to the same address.

See upeep386/peephole.py docstring for the complete pattern description and bytes saved per pattern.

Input contract

Input is exactly what uc386 / ucpp386 emit. The optimizer assumes:

  • Section directives at column 0: section .text / section .data / section .bss.
  • Top-level labels at column 0: _name: (multi-underscore prefixes allowed for __start, ___builtin_*).
  • Local labels inside functions: .local:.
  • cdecl calling convention; EAX is the return register; EBP is the frame pointer.
  • No macro expansion required at this layer — input is post- preprocessor literal NASM.

License

GPL-3.0-or-later. See LICENSE.

Release files for upeep386 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for upeep386 0.2.0
File Size Uploaded
upeep386-0.2.0.tar.gz 260.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for upeep386 0.2.0
File Interpreter ABI Platform
upeep386-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 443.0 kB

Release files / upeep386-0.2.0.tar.gz

Download URL upeep386-0.2.0.tar.gz
Size 260.2 kB
Tags Source
SHA-256 checksum
How to use checksums
512c2951ccc15063f2a32804dd80fc9dcfe6c16885e60998d2948e3407c761fa
BLAKE2b-256 checksum
How to use checksums
d10d54008b09df6bf5b911d53c389fa5f54e377aad0cab93e069ad3b87548214
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Aug 6, 2026.

Transparency log

Release files / upeep386-0.2.0-py3-none-any.whl

Download URL upeep386-0.2.0-py3-none-any.whl
Size 182.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ea7e509d49390fe416864b379fac0d61c3021eda5794bb86c3ece6db5e12818
BLAKE2b-256 checksum
How to use checksums
6b21658c48b523167a28f15f3ff6f8ce3dae711cb81e643b0a524f3a582dcea6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Aug 6, 2026.

Transparency log

Release history Release notifications | RSS feed

0.2.1

2 release files

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page