CASM Overview
Open as a notebook (.crush-nb, how to run it)
CASM (Crush Assembly) is the stack-machine instruction set that Crush programs compile to. The name covers three closely related things, and most confusion about it comes from mixing them up:
| Form | What it is | Extension | Defined in | Who produces / runs it |
|---|---|---|---|---|
| CASM IR (JSON) | The compiler’s structured output: functions of JSON instructions, {"op": "push_int", "value": 42} | .casm JSON, or .casmb (MessagePack) | the casm crate (casm::Program) | compile_crush_to_casm produces it; embedders load it with casm::Program::load |
| CASM text (CVM1 assembly) | A line-oriented assembly you can write by hand: .func main / PUSH 42 / CAP_CALL "io.print" 1 | .cvms | crush-vm’s assembler | crushc --emit cvms prints it; crush-run run x.cvms and crush-compile consume it |
| CVM1 (binary) | The bytecode the VM actually executes: magic CVM1, constant pool, flat code | .cvm1 | crush-vm (bytecode.rs) | crushc writes it; crush-run run x.cvm1 runs it |
(Up to crush-ast v0.9.0 the extension .casm was shared by the JSON IR and the
text assembly. Since v0.10.0 (#365) .casm means the JSON IR and the text is
.cvms; crush-run still runs text in a .casm file, with a note asking you to
rename it. This guide’s text examples are labelled casm but are CVM1 assembly.)
They fit together like this:
Crush source ──parse──▶ CAST (JSON AST) ──compile──▶ CASM IR (JSON) ──lower──▶ CASM text ──assemble──▶ CVM1 bytes ──▶ VM
(crush_lang_sdk::compile::casm_to_vm)
crushc runs the whole chain; crush-run run FILE.crush compiles in memory and
runs. The CASM IR is the interchange point: every front-end (the Crush parser,
the polyglot walkers, an agent writing CAST directly) ends up
there, and every back-end (the VM, the AOT compilers crush-aot / crush-aotc,
the FastVM lowering) starts there.
What is real and what is not
- The VM is stack-based, with a flat operand stack, per-frame local slots, a
call stack, and a
try/throwhandler stack. - Host access is capability-only. There is no syscall instruction. A program
talks to the outside world with
CAP_CALL "name" argc, and the VM refuses a capability that the host did not register or that the program’s manifest does not list. - The IR has more instructions than the VM bytecode. The
casmcrate’sOpCodeenum is a superset;casm_to_vmlowers only part of it (see the Instruction Reference). A few IR instructions lower to aNOP; a few are rejected withUnsupported CVM1 opcode. - The
ai_*anddom_*instructions are hooks, not features. AnAI_QUERY(and the other AI opcodes) calls anai_native.querycapability that no CLI provides; an embedder’s stub returns{kind: query, echo: [...], ok: true}and does nothing else. Thedom_*IR instructions lower toNOP. See AI-Native CAST. (spawn/yield/awaitare real: Concurrency.) - Source mapping is carried in each IR instruction’s
meta(line,col); the CVM1 binary does not store it (the text assembler keeps a line→offset map for debuggers only).
Seeing it for yourself
Compile a small program to text assembly:
fn add(a, b) {
return a + b
}
let x = add(2, 3)
io.print("x = ", x)
x = 5
crushc add.crush --emit casm
.func main
PUSH 3
PUSH 2
CALL add
STORE 0
PUSH_STR "x = "
LOAD 0
CAP_CALL "io.print" 2
PUSH_NULL
RET
.func add
STORE 0
STORE 1
LOAD 0
LOAD 1
ADD
RET
Two things to notice, both explained in Program Structure: arguments
are pushed in reverse order (3 then 2), so the callee’s first STORE pops the
first argument into slot 0, the second STORE the next one, and so on; and all top-level statements live in a
function called main.
Next steps
- Program Structure — the JSON IR, the text assembly and the CVM1 binary layout.
- Instruction Reference — every opcode, with stack effects.
- Examples — hand-written programs, all run by the guide’s checker.
- Serialization —
.casm,.casmb,.cvm1and the version gates. - Crush Language Guide — the source language that compiles to it.