Skip to content

frogfishio/zasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

121 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

zasm
A Deterministic Virtual CPU + IR Toolchain (WASM, native, and more)

Version License Targets


What is zasm?

zasm is a compiler-and-runtime stack for deterministic, auditable, and sandboxed execution.

At the bottom, it defines a stable virtual CPU (the β€œZASM64 / ZX64” model) with a concrete opcode encoding that can be:

  • Interpreted (reference execution)
  • JIT-compiled to native machine code (arm64 / x86_64; more planned)
  • AOT-lowered to WebAssembly (WAT/WASM)
  • Packaged as a portable opcode container for shipping and replay

But zasm is no longer β€œjust a cute ISA”: it has grown into a multi-IR toolchain where:

  • Higher-level languages can compile into a stable, streaming IR (SIR) and then lower through zasm layers.
  • The host boundary is defined by a normative, capability-gated ABI (zABI 2.x, env.zi_*).
  • Complex data lives in a controlled arena/record system (Hopper), enabling β€œmemory-safe by default” frontends.

Product lines (execution models)

zasm supports three closely-related runtime profiles:

  • ZX64 β€” classic register-machine virtual CPU (the stable baseline).
  • Z64+S β€” ZX64 plus the optional stacker coprocessor (explicit stack substrate for mixed-work loads).
  • ZX64S β€” a stack-machine runtime (stacker as the whole CPU; stacker-only control flow).

These are compile-time/lowering-time choices (no runtime auto-switch). If a target/runtime doesn’t support a requested profile, lowering fails.

Status: v1.0.5 β€” core contracts are stable (ISA/IR/ABI/opcode encoding). Breaking changes trigger major version bumps.


Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         FRONTENDS (many languages)                           β”‚
β”‚              Oberon / C89 / DSLs / etc. β†’ SIR (streaming typed IR)           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         zasm layer (ZIR / zasm IR)                           β”‚
β”‚               Lowering, tooling, analysis, minimization, replay              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        SOURCE (.asm) / zas (Assembler)                       β”‚
β”‚         Human-readable mnemonics β†’ Emits versioned JSONL IR / opcode stream  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                            zlnt (Analyzer)                                  β”‚
β”‚          Static analysis on JSONL IR (recommended safety gate)               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚                       β”‚                       β”‚
            β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   zld (Linker)      β”‚   β”‚   zop (Packer)      β”‚   β”‚  Third-Party Compilers  β”‚
β”‚   JSONL β†’ WAT/WASM  β”‚   β”‚   JSONL β†’ .zasm.bin β”‚   β”‚  (Your DSL β†’ JSONL IR)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                         β”‚
        β–Ό                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   WebAssembly       β”‚   β”‚              libzxc (Cross-Compiler)             β”‚
β”‚   (Any WASM host)   β”‚   β”‚   Opcode bytes β†’ Native machine code             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
                          β”‚   β”‚   arm64     β”‚   x86_64    β”‚  RV64I (soon) β”‚  β”‚
                          β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
                          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                          β”‚
                                          β–Ό
                          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                          β”‚           Cloak Runtime (Sandbox)               β”‚
                          β”‚   Capability-gated execution with zABI 2.x      β”‚
                          β”‚   β€’ zrun (WASM via wasmtime)                    β”‚
                          β”‚   β€’ zcloak (Pure C interpreter)                 β”‚
                          β”‚   β€’ zcloak-jit (JIT via libzxc)                 β”‚
                          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Differentiators

🎯 Custom Virtual ISA (Not an Emulator)

zasm defines a 64-bit virtual CPU (ZX64) with:

  • Fixed 32-bit base instruction word with extension words for large immediates
  • 16-register file (5 currently mapped: HL, DE, A, BC, IX)
  • Explicit opcode encoding (documented in docs/spec/opcode_encoding.md)
  • Both 32-bit and 64-bit arithmetic with zero-extension semantics
  • Memory operations with bounds checking built into the ABI

This is not a Z80 emulatorβ€”the mnemonics are merely a UX choice for readability. The underlying architecture is closer to RISC-V in philosophy.

πŸ”’ Deterministic by Design

Every aspect of zasm is built for byte-for-byte reproducibility:

  • No timestamps, random IDs, or host-dependent formatting
  • Deterministic memory layout with fixed base offsets and 4-byte alignment
  • Deterministic allocator behavior (_alloc results are reproducible for identical call sequences)
  • Identical inputs β†’ identical outputs across runs, machines, and platforms

This makes zasm ideal for content-addressable systems, blockchain execution, and reproducible builds.

🧱 More than an ISA: streaming IR + tooling

zasm is built around line-oriented, versioned IR streams (JSONL) so tools can operate on real programs:

  • interpret and debug (reference semantics)
  • trace execution and memory events
  • compute and merge coverage
  • delta-minimize and triage failures
  • strip unreachable code and dead regions
  • analyze repetition (n-grams) and bloat

This is the β€œcompiler stack” part: a stable representation you can pipe through real toolchains.

πŸ›‘οΈ Capability-Gated Sandboxing

The runtime boundary is defined by a normative host ABI (zABI 2.x):

  • All host interactions go through explicit env.zi_* syscalls (e.g. zi_read, zi_write, zi_alloc, zi_ctl).
  • No ambient authority β€” if it’s not in the ABI surface, it doesn’t exist.
  • Fail-closed security β€” missing capabilities are rejected via discovery/negotiation (zi_ctl).
  • Auditability β€” host effects are explicit and can be traced, replayed, minimized, and certified.

This keeps zasm modules portable across hosts while preserving a strict sandbox contract.

πŸ”Œ Multi-Target Cross-Compilation

A single source compiles to multiple backends:

Target Output Use Case
--target wasm WebAssembly (WAT/WASM) Browser, edge, serverless
--target zasm Native opcode bytes JIT/AOT via libzxc
--target rv64i RISC-V RV64I Embedded, FPGA, future hardware

The libzxc library provides embeddable C APIs for translating opcode streams to native machine code:

zxc_result_t zxc_arm64_translate(const uint8_t* in, size_t in_len,
                                 uint8_t* out, size_t out_cap,
                                 uint64_t mem_base, uint64_t mem_size,
                                 const struct zi_host_v1* host);

zxc_result_t zxc_x86_64_translate(const uint8_t* in, size_t in_len,
                                  uint8_t* out, size_t out_cap,
                                  uint64_t mem_base, uint64_t mem_size);

πŸ“œ Stable, Versioned Contracts

zasm ships with normative specifications that third-party tools can rely on:

  • ISA Spec (docs/spec/isa.md) β€” Registers, instructions, directives
  • | docs/spec/stacker.md | Stacker profile (coprocessor) and ZX64S addendum (stack CPU) |
  • ABI Spec (docs/spec/abi.md) β€” Host primitives, memory model, handle semantics
  • IR Spec (docs/spec/ir.md) β€” JSONL intermediate representation
  • Opcode Spec (docs/spec/opcode_encoding.md) β€” Binary encoding for native backends

The historical Integrator Pack was ABI1-based and is now largely defunct; we will produce a new zABI 2.5 integrator pack later.


Use Cases

πŸ—οΈ DSL Backend / Compiler Target

Building a domain-specific language? Emit JSONL IR and let zasm handle the hard parts:

{"ir":"zasm-v1.1","kind":"instr","op":"LD","ops":[{"t":"reg","v":"DE"},{"t":"sym","v":"msg"}],"loc":{"line":5}}
{"ir":"zasm-v1.1","kind":"instr","op":"LD","ops":[{"t":"reg","v":"BC"},{"t":"sym","v":"msg_len"}],"loc":{"line":6}}
{"ir":"zasm-v1.1","kind":"instr","op":"LD","ops":[{"t":"reg","v":"HL"},{"t":"num","v":1}],"loc":{"line":7}}
{"ir":"zasm-v1.1","kind":"instr","op":"CALL","ops":[{"t":"sym","v":"zi_write"}],"loc":{"line":8}}

No need to handle WASM's structured control flowβ€”zld converts labels and jumps automatically.

οΏ½οΏ½ Zero-Trust Plugin Execution

Run untrusted code with full auditability:

  • Every host call is explicit (CALL zi_write, CALL zi_alloc)
  • No hidden syscalls or ambient capabilities
  • Memory bounds checked at the ABI level
  • Deterministic execution for replay/audit

πŸ—„οΈ Content-Addressable / Immutable Systems

Perfect for systems where the hash is the identity:

  • IPFS / Filecoin compute
  • Blockchain smart contracts
  • Build caches and artifact stores
  • Reproducible research

⚑ High-Performance Stream Processing

The (DE, BC) = (ptr, len) slice convention (with HL as a handle for zi_read/zi_write) enables:

  • O(1) length lookups (no NUL scanning)
  • Zero-copy buffer passing
  • UNIX-style filter composition

Quick Start

Deliverables

For end users, zasm is delivered as self-contained compiled binaries (e.g. bin/<platform>/*, with convenience symlinks under bin/).

The repository may contain scripts used for development, testing, or internal workflows; they are not part of the user-facing product surface.

Build

make              # Build core tools (zas, zld, zlnt, zop)
make zrun         # Build WASM runner (requires wasmtime-c-api)
make zcloak       # Build pure-C cloak runtime
make zcloak-jit   # Build JIT runner (via libzxc)

Hello World (WASM Target)

; hello.asm
CALL print_hello
RET

print_hello:
  LD HL, msg
  LD DE, msg_len
  LD BC, DE
  LD DE, HL
  LD HL, #1
  CALL zi_write
  RET

msg:      STR "Hello from zasm!"
cat hello.asm | bin/zas | bin/zlnt | bin/zld > hello.wat
wat2wasm hello.wat -o hello.wasm
bin/zrun hello.wat

Native JIT Execution

cat hello.asm | bin/zas --target opcodes | bin/zop --container > hello.zasm.bin
bin/zcloak-jit --mem 2mb hello.zasm.bin

JIT error semantics:

  • Bounds violations and div0 traps raise a signal and zcloak-jit exits non-zero with a diagnostic.
  • Host ABI faults (invalid pointers/handles) are reported as zcloak faults after execution.

Toolchain

Tool Description
zas Assembler: zASM source β†’ JSONL IR
zld Linker: JSONL IR β†’ WAT/WASM
zop Packer: JSONL opcode stream β†’ .zasm.bin
zlnt Linter: Static analysis for JSONL IR
zrun Runner: Execute WAT/WASM via wasmtime
zem Emulator + debugger: Execute JSONL IR directly (trace/debug/events)
zcloak Pure-C cloak runtime (interpreter)
zcloak-jit JIT runner via libzxc
libzxc Embeddable cross-compiler library (C API)

Documentation

Document Description
docs/architecture.md System design and pipeline overview
docs/spec/stacker.md Stacker profile (coprocessor) and ZX64S addendum (stack CPU)
docs/developers.md Getting started guide
docs/spec/isa.md Normative instruction set specification
docs/spec/abi.md Normative host ABI and capability model
docs/spec/ir.md Normative JSONL IR format
docs/spec/opcode_encoding.md Normative binary opcode encoding
docs/spec/zasm_bin.md Normative .zasm.bin container format
docs/tools/zem.md zem usage (debugger + JSONL stop events)
docs/spec/accelerator.md Accelerator profile (CUDA/Vulkan/Metal; draft)
docs/spec/fpga.md FPGA profile (HLS/RTL; draft)
docs/integrator_pack/jit/README.md JIT codepack snapshot (integrator pack)
docs/integrator_pack/integrator_pack.md Third-party compiler integration guide
docs/integrator_pack/integrator_pack/CLOAK_INTEGRATOR_GUIDE.md Legacy cloak integration guide (retired)

Project Status

βœ… Stable (v1.x)

  • JSONL IR schema and versioning
  • Host ABI (zABI 2.0: env.zi_* syscalls)
  • Core ISA (arithmetic, logic, shifts, loads, stores, branches, calls)
  • WASM backend via zld
  • Pure-C cloak runtime
  • Opcode binary encoding

🚧 In Progress

  • libzxc arm64 backend (partial coverage)
  • libzxc x86_64 backend (Group A only)
  • zcloak-jit native execution

πŸ“‹ Planned

  • RV64I backend
  • Additional target architectures
  • VS Code extension (syntax highlighting, diagnostics)
  • Expanded conformance test suite

Contributing

Contributions are welcome. Areas of interest:

  • Backend coverage: Expanding opcode support in libzxc (arm64, x86_64)
  • New targets: RV64I, additional architectures
  • Tooling: Editor integrations, debugging support
  • Conformance tests: Golden tests for all mnemonic/operand combinations
  • Documentation: Examples, tutorials, integration guides

See CONTRIBUTING.md for guidelines.


License


zasm: Write once, run deterministically everywhere.

Tools

  • zas: assembler β†’ JSONL/opcodes (docs/tools/zas.md)
  • lower: JSON IR (zasm-v1.0/v1.1) β†’ macOS arm64 Mach-O with rich dump/LLDB helper modes (docs/tools/lower.md)
  • zxc: experimental cross-compilers (docs/tools/zxc.md)
  • zir: IR utilities

About

64-bit register-based virtual processor for the WebAssembly age. Deterministic, modular, and tiny.

Topics

Resources

License

GPL-3.0, MIT licenses found

Licenses found

GPL-3.0
LICENSE
MIT
LICENSE-ASM

Contributing

Stars

2 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors