Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 7f1f89abf16315a93795a6d54e4f084a5a09658d
Ticket: 716b427f76f8f97a8d9a06043903c53bb2b592c2
Suboptimal bytecode for $a[unset a]
User & Date: dgp on 2017-06-06 19:56:48
Changes

  1. closedate changed to: "2457911.33111293"
  2. closer changed to: "dgp"
  3. icomment:
    
    Here is why Tcl's optimizer is correct that it
    cannot optimize those instructions to no-ops
    (at least absent some deep and fundamental changes
    to the bytecode execution engine):
    
    Define two variables to report different outcomes:
    
    % set v wut?
    wut?
    % set v1 yay!
    yay!
    
    Define a self-modifying proc and examine its bytecode:
    
    % proc demo a {proc unset x {return 1}; upvar 1 $a[unset a] b; puts $b}
    % tcl::unsupported::disassemble proc demo
    ByteCode 0x2519c60, refCt 1, epoch 18, interp 0x24b75b0 (epoch 18)
      Source "proc unset x {return 1}; upvar 1 $a[unset a] b; puts $b"
      Cmds 4, src 55, inst 59, litObjs 7, aux 0, stkDepth 4, code/src 0.00
      Proc 0x255c9a0, refCt 1, args 1, compiled locals 2
          slot 0, scalar, arg, "a"
          slot 1, scalar, "b"
      Commands 4:
          1: pc 0-10, src 0-22        2: pc 11-51, src 25-45
          3: pc 24-40, src 36-42        4: pc 52-57, src 48-54
      Command 1: "proc unset x {return 1}..."
        (0) push1 0         # "proc"
        (2) push1 1         # "unset"
        (4) push1 2         # "x"
        (6) push1 3         # "return 1"
        (8) invokeStk1 4 
        (10) pop 
      Command 2: "upvar 1 $a[unset a] b..."
        (11) startCommand +40 1     # next cmd at pc 51, 1 cmds start here
        (20) push1 4        # "1"
        (22) loadScalar1 %v0        # var "a"
      Command 3: "unset a..."
        (24) startCommand +17 1     # next cmd at pc 41, 1 cmds start here
        (33) unsetScalar 1 %v0      # var "a"
        (39) push1 5        # ""
        (41) strcat 2 
        (43) upvar %v1      # var "b"
        (48) pop 
        (49) push1 5        # ""
        (51) pop 
      Command 4: "puts $b"
        (52) push1 6        # "puts"
        (54) loadScalar1 %v1        # var "b"
        (56) invokeStk1 2 
        (58) done 
    
    Note that pc 24-40 are the bytecode of the [unset a].
    It was produced by compiling the original meaning of [::unset].
    However, at run time when that bytecode is reached,
    [::unset] has a new definition. The "startCommand" instruction
    detects this and effectively overwrites pc 24-40 with a new
    set of bytecode:
    
    push "unset a"
    evalStk
    
    This makes the bytecode do a proper Tcl evaluation, but
    then it returns to pc 41 to continue operations from
    there.
    
    Now, if we allowed some optimizer to turn pc 39-42
    into no-ops, then after the replacement and recovery
    to pc 41, we'd have a no-op instead of a proper strcat
    to attach the "1" returned by the replacement [unset]
    to the varname, and we'd link to the wrong caller variable.
    
    So we run it and see proper functioning:
    
    % demo v
    yay!
    
    Given how all the pieces currently fit together and function
    this is an optimization we cannot do.
    
    But! But! If we just... and ... 
    
    Yes, by all means take a stab at it, and seek out the
    proper boundaries of reform that might let us make the
    optimization; just know it's not a simple matter.
    
  4. login: "dgp"
  5. mimetype: "text/plain"
  6. status changed to: "Closed"