Tcl/Tk Insight: Tcl中的字节码(ByteCode)

风行水上 @ 2014-11-06 12:32:34
标签:

    为了加速脚本的执行,Tcl引入了字节码(ByteCode)。

    ByteCode实际上像是一种汇编指令。通过把Tcl脚本源代码转换为字节码,Tcl解释器不再需要频繁调用命令对应的函数,而是执行一条条的指令。

    tcl::unsupported::disassemble

    Tcl 8.5开始引入了命令tcl::unsupported::disassemble可以用来观察代码编译成字节码之后的结果。

    参见:disassemble @ wiki.tcl.tk

    下面的例子可以大致说明其用法和输出结果。

    proc t-append {s} {
      append s "abc" 
      append s2 "def$s" 
      append [set s2] "hij"
    }
    
    tcl::unsupported::disassemble proc t-append
    
    ByteCode 0x02E58658, refCt 1, epoch 3, interp 0x009B40F8 (epoch 3)
      Source "\n  append s \"abc\" \n  append s2 \"def$s\" \n  append [set s"
      Cmds 4, src 64, inst 38, litObjs 3, aux 0, stkDepth 2, code/src 0.00
      Proc 0x02E76898, refCt 1, args 1, compiled locals 2
          slot 0, scalar, arg, "s"
          slot 1, scalar, "s2"
      Commands 4:
          1: pc 0-4, src 3-17   	   2: pc 5-22, src 21-38
          3: pc 23-36, src 42-62   	   4: pc 32-33, src 50-55
      Command 1: "append s \"abc\" "
        (0) push1 0 	# "abc"
        (2) appendScalar1 %v0 	# var "s"
        (4) pop 
      Command 2: "append s2 \"def$s\" "
        (5) startCommand +17 1 	# next cmd at pc 22
        (14) push1 1 	# "def"
        (16) loadScalar1 %v0 	# var "s"
        (18) concat1 2 
        (20) appendScalar1 %v1 	# var "s2"
        (22) pop 
      Command 3: "append [set s2] \"hij\""
        (23) startCommand +14 2 	# next cmd at pc 37, 2 cmds start here
      Command 4: "set s2"
        (32) loadScalar1 %v1 	# var "s2"
        (34) push1 2 	# "hij"
        (36) appendStk 
        (37) done 
    

    tcl::unsupported::assemble

    TODO

    标签:

      分享到:
      comments powered by Disqus

      29/32ms