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.1

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.1
File Size Uploaded
upeep386-0.2.1.tar.gz 262.6 kB Details

Built distribution (wheel)

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

Total release size: 447.0 kB

Release files / upeep386-0.2.1.tar.gz

Download URL upeep386-0.2.1.tar.gz
Size 262.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7dff4a453522e734e5b82f2941ef3438d377e81365d0d0bcdb0289fcc1f858e6
BLAKE2b-256 checksum
How to use checksums
b32636b7eae8848b014d9a4c2f2737340c9c084b66086ec99a593105fda055a5
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 20, 2026.

Transparency log

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

Download URL upeep386-0.2.1-py3-none-any.whl
Size 184.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
24dd13e774bcf273002f125de3dbf0ed9dff3f19e9b49c12ce8e0e5528afbb2b
BLAKE2b-256 checksum
How to use checksums
a2d1714cc781403b29f3ba914902c4d431cfa66ca0ad87f126903969ea137b05
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 20, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

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