FLUX bytecode assembler, disassembler, and validator.
Implements the FLUX Bytecode Format Specification v1.1. Compiles FLUX assembly text to bytecode, disassembles bytecode back to human-readable assembly, and validates structural integrity. WASM-ready for browser use.
Part of Working Animal Architecture, where γ + η = C (genome + nurture = capability). The compiler is the forge — it shapes the steel of FLUX bytecode from raw assembly text. The bytecode it produces is the fence that governs working animals. A well-forged fence is invisible to the animal and impenetrable to chaos.
The compiler doesn't interpret policy. It forge-shapes it.
- Assemble — FLUX assembly text → bytecode (
assemble) - Disassemble — bytecode → human-readable assembly (
disassemble) - Validate — structural integrity checks (
validate) - Round-trip — assemble then disassemble for pretty-printing (
round_trip) - WASM-ready — compile to
wasm32-unknown-unknownfor browser use
cargo add si-flux-compilerOr in Cargo.toml:
[dependencies]
si-flux-compiler = "0.1"For WASM builds:
[dependencies]
si-flux-compiler = { version = "0.1", features = ["wasm"] }use si_flux_compiler::{assemble, disassemble, validate};
fn main() {
let source = r#"
MOVI R0, 0
MOVI R1, 10
loop:
IADD R0, R0, R1
DEC R1
JNZ R1, loop
HALT
"#;
let bytecode = assemble(source).unwrap();
validate(&bytecode).unwrap();
println!("{}", disassemble(&bytecode));
}use si_flux_compiler::round_trip;
// Assemble messy source, disassemble to clean output
let clean = round_trip("MOVI R0, 42\nIADD R0,R0,R1\nHALT").unwrap();
println!("{clean}");
// NOP
// MOVI R0, 0x2A
// IADD R0, R0, R1
// HALTuse si_flux_compiler::assemble;
let bc = assemble("MOVI R0, 0xFF\nMOVI R1, 0b1010\nHALT").unwrap();; Comments use ; or //
label_name:
MOVI R0, 42 ; decimal immediate
MOVI R1, 0xFF ; hex immediate
MOVI R2, 0b1010 ; binary immediate
MOVI R3, -10 ; negative immediate
IADD R0, R0, R1 ; R0 = R0 + R1
JNZ R3, label_name ; jump if R3 != 0
HALT
| Register Banks | Range | Use |
|---|---|---|
R0–R15 |
Integer | General-purpose 32-bit integer registers |
F0–F15 |
Float | Floating-point registers |
V0–V15 |
Vector | Vector registers (format G) |
Register names are case-insensitive (r0 works the same as R0).
| Format | Example | Value |
|---|---|---|
| Decimal | 42 |
42 |
| Hexadecimal | 0xFF |
255 |
| Binary | 0b1010 |
10 |
| Negative | -10 |
-10 (stored as i16) |
Labels are defined with name: on their own line or inline before an instruction. Forward and backward references are supported. The assembler resolves labels to relative offsets at emit time.
Commas between operands are optional: IADD R0 R1 R2 and IADD R0, R1, R2 are equivalent.
| Format | Size | Layout | Instructions |
|---|---|---|---|
| A | 1 byte | opcode |
NOP, HALT, YIELD, DUP, SWAP |
| B | 2 bytes | opcode reg |
INC, DEC, PUSH, POP |
| B₂ | 3 bytes | opcode addr16[LE] |
JE, JNE |
| C | 3 bytes | opcode rd rs1 |
MOV, LOAD, STORE, CMP, RET, INEG |
| D | 4 bytes | opcode reg imm16[LE] |
MOVI, JMP, JZ, JNZ, CALL |
| E | 4 bytes | opcode rd rs1 rs2 |
IADD, ISUB, IMUL, FADD, FSUB, ... |
| G | var | opcode len16[LE] data[len] |
TELL, ASK, DELEGATE, BROADCAST |
| Function | Signature | Description |
|---|---|---|
assemble(source) |
&str → Result<Vec<u8>, AssemblyError> |
Assemble FLUX assembly to bytecode |
disassemble(bytecode) |
&[u8] → String |
Disassemble bytecode to assembly text |
validate(bytecode) |
&[u8] → Result<(), ValidationError> |
Validate structural integrity |
round_trip(source) |
&str → Result<String, AssemblyError> |
Assemble + disassemble (pretty-print) |
| Type | Description |
|---|---|
Instruction |
Parsed instruction with format, opcode, operands |
Format |
Enum: A, B, B2, C, D, E, G |
OpcodeInfo |
Opcode metadata (mnemonic, format, size) |
OPCODE_TABLE |
Static lookup table for all opcodes |
lookup_mnemonic(byte) |
Byte → mnemonic string |
lookup_opcode(mnem) |
Mnemonic string → opcode byte |
| Function | Description |
|---|---|
assemble_source(source) |
Full assembly pipeline (parse → resolve → encode) |
encode(instructions) |
Encode parsed instructions to bytecode |
encode_instruction(instr) |
Encode a single instruction |
| Type/Function | Description |
|---|---|
DecodedInstruction |
A decoded instruction with mnemonic and operands |
decode(bytecode) |
Decode bytecode into instruction list |
decode_bytecode(bytecode) |
Full decode with validation |
disassemble(bytecode) |
Decode + format as assembly text |
| Error | When |
|---|---|
AssemblyError |
Parse failure, undefined label, bad register, bad immediate |
ValidationError |
Empty bytecode, unknown opcode, truncated instruction, register overflow |
For browser-based FLUX compilation (e.g., in the FLUX Visual Editor):
rustup target add wasm32-unknown-unknown
cargo build --features wasm --target wasm32-unknown-unknownExposes assemble, disassemble, and validate via wasm-bindgen for JavaScript consumption:
import { assemble, disassemble, validate } from './si_flux_compiler.js';
const bytecode = assemble("MOVI R0, 42\nHALT");
validate(bytecode);
const text = disassemble(bytecode);src/
├── lib.rs # Public API: assemble(), disassemble(), validate(), round_trip()
├── errors.rs # AssemblyError, ValidationError types
├── parser.rs # Lexer + parser: source text → Instruction list
│ # Opcode table, Format enum, register/immediate parsing
├── encoder.rs # Instruction list → bytecode (label resolution + emission)
└── decoder.rs # Bytecode → DecodedInstruction list (for disassembly)
Source text
│
▼
Parser ──── tokens → Instruction list (Format, Opcode, operands)
│
▼
Encoder ──── label resolution → offset computation → byte emission
│
▼
Bytecode (Vec<u8>)
# Run all tests
cargo test
# Run with verbose output
cargo test -- --nocapture
# Run round-trip tests (assemble → disassemble → verify)
cargo test round_trip
# Pretty-print assertion diffs
cargo test -- --nocapture --test-threads=1Uses pretty_assertions for readable diff output on test failures.
| Aspect | Python | Rust |
|---|---|---|
| Package | N/A (part of flux-vm) | cargo add si-flux-compiler |
| Repo | flux-vm (assembler module) | flux-compiler-rs (this) |
| WASM | N/A | ✅ (wasm-bindgen feature) |
| Bytecode compat | ✅ v1.1 spec | ✅ v1.1 spec |
Both implementations produce binary-compatible bytecode per the FLUX Bytecode Format Specification v1.1.
- flux-vm — Python VM with built-in assembler
- flux-core — Rust VM (
cargo add fluxvm) - conservation-enforcer-rs — Conservation enforcement with built-in assembler
- flux-registry-rs — Policy registry CLI
- flux-policy-tester-rs — Policy testing framework with built-in assembler
- FLUX Bytecode Spec — Formal specification
- AI-Writings — Paradigm essays
MIT