Bytecode Comparison Skill
Compares the bytecode produced by our compiler pipeline (parser → lowering →
IR → CFG → codegen) against tclsh reference disassembly (8.4, 8.5, 8.6, 9.0)
for the 72 fixtures in the Rust codegen corpus.
Where the two sides come from
Ours is the <name>.golden beside each <name>.tcl in
rust/tcl-compiler/tests/fixtures/codegen/matching/. Those goldens hold the
label-stripped disassembly the Rust emitter produces, byte-identity gated by
rust/tcl-compiler/tests/codegen_golden.rs — so whenever that test passes, the
goldens are our current output. Nothing is regenerated by this skill.
Reference is captured from a real tclsh into
tests/bytecode_reference/<version>/ by scripts/capture/bytecode.sh (and
bytecode_84.sh for 8.4, which needs tcl_traceCompile 2 rather than
tcl::unsupported::disassemble). That directory is generated and git-ignored;
capture it before comparing, or every snippet reports 0 reference instructions:
./scripts/capture/bytecode.sh # 8.5 / 8.6 / 9.0, skipping absent ones
./scripts/capture/bytecode_84.sh # 8.4
Usage
Run from the project root:
python3 .claude/skills/bytecode-compare/bytecode_compare.py [-v VERSION] <subcommand> [args...]
Options
| Flag | Description |
|---|---|
| -v, --version | Tcl version to compare against: 8.4, 8.5, 8.6, 9.0 (default: 9.0) |
Subcommands
| Subcommand | Arguments | What it does |
|---|---|---|
| all | | Compare all 72 fixtures: the summary table, then a detailed diff per mismatch |
| diff | <snippet> | Detailed instruction-by-instruction diff for one snippet (e.g. while-simple) |
| summary | | One-line per snippet: match/mismatch with instruction counts |
| instructions | <snippet> | Side-by-side normalised instruction listing |
| refresh | | Verify the goldens still match live codegen (codegen_golden), then re-compare. Re-bless an intentional codegen change with BLESS_GOLDEN=1 cargo test -p tcl-compiler --test codegen_golden. |
| categories | | Group differences by category (variable access, jumps, opcodes, etc.) |
| versions | <snippet> | Compare one snippet against all Tcl versions (8.4, 8.5, 8.6, 9.0) |
Interpreting Output
Summary mode
append-scalar MATCH ours: 9 instrs 8.6: 9 instrs
array-for MISMATCH ours: 12 instrs 8.6: 14 instrs (-2)
foreach-simple MISMATCH ours: 11 instrs 8.6: 6 instrs (+5)
MATCH means identical normalised instruction sequences. MISMATCH means differences exist.
Diff mode
=== while-simple ===
9.0: (0) push1 0 # "i"
9.0: (2) push1 1 # "0"
9.0: (4) storeStk
ours: (0) push1 0 # "0"
ours: (2) storeScalar1 %v0 # var "i"
--- variable access: storeStk (name-based) vs storeScalar1 (LVT-indexed)
Categories mode
Groups all divergences into categories:
- Variable access: storeStk/loadStk vs storeScalar1/loadScalar1
- Jump encoding: relative offsets vs absolute PC
- Missing opcodes: opcodes tclsh uses that we don't emit
- Extra instructions: instructions we emit that tclsh doesn't
- Dead code: unreachable code differences
When to use
- After modifying the bytecode emitter (
rust/tcl-compiler/src/codegen/emitter/,
rust/tcl-bytecode/) — verify instruction sequences
- After modifying lowering or CFG — check for regressions
- When investigating why a specific snippet diverges from tclsh
- Before committing codegen changes — run
summaryas a gate check
$ARGUMENTS