Version 4 of disassemble

Updated 2013-01-31 22:26:52 by dkf

Bytecode disassemby command, introduced in Tcl 8.5.

tcl::unsupported::disassemble proc procName
tcl::unsupported::disassemble lambda lambdaTerm
tcl::unsupported::disassemble script script
tcl::unsupported::disassemble method class method
tcl::unsupported::disassemble objmethod object method

Usage

Disassembles the bytecode of a procedure, lambda/apply term, general script, etc. and returns the disassembly. The format is formally undefined, but is really an exposure of the disassembler built in since 8.0 when compiling with the correct options and can probably be read by anyone who has ever read any assembly code without much difficulty. Understanding the bytecodes requires reading tclCompile.c and (possibly) tclExecute.c. Will not disassemble anything created by tbcload.

Notes

It's unfortunately not at all compatible with the assemble command; the formats are quite different (though the opcodes themselves are the same).

Not present in safe interpreters. This command was developed during the Fourteenth Annual Tcl/Tk Conference (2007).

Example Output/Use

% proc foo {x} {
   for {set y 0} {$y < $x} {incr y} {
      puts $x,$y
   }
}
% tcl::unsupported::disassemble proc foo
ByteCode 0x0x9a2ec58, refCt 1, epoch 1, interp 0x0x9a377d8 (epoch 1)
  Source "\n   for {set y 0} {$y < $x} {incr y} {\n      puts $x,$y"
  Cmds 4, src 61, inst 43, litObjs 4, aux 0, stkDepth 4, code/src 0.00
  Proc 0x0x9a48f70, refCt 1, args 1, compiled locals 2
      slot 0, scalar, arg, "x"
      slot 1, scalar, "y"
  Exception ranges 2, depth 1:
      0: level 0, loop, pc 7-18, continue 20, break 40
      1: level 0, loop, pc 20-31, continue -1, break 40
  Commands 4:
      1: pc 0-41, src 4-59         2: pc 0-3, src 9-15
      3: pc 7-18, src 45-54        4: pc 20-31, src 29-34
  Command 1: "for {set y 0} {$y < $x} {incr y} {\n      puts $x,$y\n   "
  Command 2: "set y 0"
    (0) push1 0         # "0"
    (2) storeScalar1 %v1        # var "y"
    (4) pop
    (5) jump1 +28       # pc 33
  Command 3: "puts $x,$y"
    (7) push1 1         # "puts"
    (9) loadScalar1 %v0         # var "x"
    (11) push1 2        # ","
    (13) loadScalar1 %v1        # var "y"
    (15) concat1 3
    (17) invokeStk1 2
    (19) pop
  Command 4: "incr y"
    (20) startCommand +12 1     # next cmd at pc 32
    (29) incrScalar1Imm %v1 +1  # var "y"
    (32) pop
    (33) loadScalar1 %v1        # var "y"
    (35) loadScalar1 %v0        # var "x"
    (37) lt
    (38) jumpTrue1 -31  # pc 7
    (40) push1 3        # ""
    (42) done