Bytecode compare

From bitwisecook/tcl-lsp · View interactive version

Bytecode compare is an AI agent skill from the GitHub repository bitwisecook/tcl-lsp. Compare our compiler's bytecode output against tclsh reference disassembly (8.4, 8.5, 8.6, 9.0). Use when verifying bytecode correctness, investigating instruction sequence differences, or checking that our codegen matches C Tcl.

How to use this skill

Skill file

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:

When to use

rust/tcl-bytecode/) — verify instruction sequences

$ARGUMENTS

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.