| 1 |
/* |
| 2 |
* tclCompile.c -- |
| 3 |
* |
| 4 |
* This file contains procedures that compile Tcl commands or parts of |
| 5 |
* commands (like quoted strings or nested sub-commands) into a sequence |
| 6 |
* of instructions ("bytecodes"). |
| 7 |
* |
| 8 |
* Copyright (c) 1996-1998 Sun Microsystems, Inc. |
| 9 |
* Copyright (c) 2001 Kevin B. Kenny. All rights reserved. |
| 10 |
* |
| 11 |
* See the file "license.terms" for information on usage and redistribution of |
| 12 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "tclInt.h" |
| 16 |
#include "tclCompile.h" |
| 17 |
#include <assert.h> |
| 18 |
|
| 19 |
/* |
| 20 |
* Variable that controls whether compilation tracing is enabled and, if so, |
| 21 |
* what level of tracing is desired: |
| 22 |
* 0: no compilation tracing |
| 23 |
* 1: summarize compilation of top level cmds and proc bodies |
| 24 |
* 2: display all instructions of each ByteCode compiled |
| 25 |
* This variable is linked to the Tcl variable "tcl_traceCompile". |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifdef TCL_COMPILE_DEBUG |
| 29 |
int tclTraceCompile = 0; |
| 30 |
static int traceInitialized = 0; |
| 31 |
#endif |
| 32 |
|
| 33 |
/* |
| 34 |
* A table describing the Tcl bytecode instructions. Entries in this table |
| 35 |
* must correspond to the instruction opcode definitions in tclCompile.h. The |
| 36 |
* names "op1" and "op4" refer to an instruction's one or four byte first |
| 37 |
* operand. Similarly, "stktop" and "stknext" refer to the topmost and next to |
| 38 |
* topmost stack elements. |
| 39 |
* |
| 40 |
* Note that the load, store, and incr instructions do not distinguish local |
| 41 |
* from global variables; the bytecode interpreter at runtime uses the |
| 42 |
* existence of a procedure call frame to distinguish these. |
| 43 |
*/ |
| 44 |
|
| 45 |
InstructionDesc const tclInstructionTable[] = { |
| 46 |
/* Name Bytes stackEffect #Opnds Operand types */ |
| 47 |
{"done", 1, -1, 0, {OPERAND_NONE}}, |
| 48 |
/* Finish ByteCode execution and return stktop (top stack item) */ |
| 49 |
{"push1", 2, +1, 1, {OPERAND_LIT1}}, |
| 50 |
/* Push object at ByteCode objArray[op1] */ |
| 51 |
{"push4", 5, +1, 1, {OPERAND_LIT4}}, |
| 52 |
/* Push object at ByteCode objArray[op4] */ |
| 53 |
{"pop", 1, -1, 0, {OPERAND_NONE}}, |
| 54 |
/* Pop the topmost stack object */ |
| 55 |
{"dup", 1, +1, 0, {OPERAND_NONE}}, |
| 56 |
/* Duplicate the topmost stack object and push the result */ |
| 57 |
{"strcat", 2, INT_MIN, 1, {OPERAND_UINT1}}, |
| 58 |
/* Concatenate the top op1 items and push result */ |
| 59 |
{"invokeStk1", 2, INT_MIN, 1, {OPERAND_UINT1}}, |
| 60 |
/* Invoke command named objv[0]; <objc,objv> = <op1,top op1> */ |
| 61 |
{"invokeStk4", 5, INT_MIN, 1, {OPERAND_UINT4}}, |
| 62 |
/* Invoke command named objv[0]; <objc,objv> = <op4,top op4> */ |
| 63 |
{"evalStk", 1, 0, 0, {OPERAND_NONE}}, |
| 64 |
/* Evaluate command in stktop using Tcl_EvalObj. */ |
| 65 |
{"exprStk", 1, 0, 0, {OPERAND_NONE}}, |
| 66 |
/* Execute expression in stktop using Tcl_ExprStringObj. */ |
| 67 |
|
| 68 |
{"loadScalar1", 2, 1, 1, {OPERAND_LVT1}}, |
| 69 |
/* Load scalar variable at index op1 <= 255 in call frame */ |
| 70 |
{"loadScalar4", 5, 1, 1, {OPERAND_LVT4}}, |
| 71 |
/* Load scalar variable at index op1 >= 256 in call frame */ |
| 72 |
{"loadScalarStk", 1, 0, 0, {OPERAND_NONE}}, |
| 73 |
/* Load scalar variable; scalar's name is stktop */ |
| 74 |
{"loadArray1", 2, 0, 1, {OPERAND_LVT1}}, |
| 75 |
/* Load array element; array at slot op1<=255, element is stktop */ |
| 76 |
{"loadArray4", 5, 0, 1, {OPERAND_LVT4}}, |
| 77 |
/* Load array element; array at slot op1 > 255, element is stktop */ |
| 78 |
{"loadArrayStk", 1, -1, 0, {OPERAND_NONE}}, |
| 79 |
/* Load array element; element is stktop, array name is stknext */ |
| 80 |
{"loadStk", 1, 0, 0, {OPERAND_NONE}}, |
| 81 |
/* Load general variable; unparsed variable name is stktop */ |
| 82 |
{"storeScalar1", 2, 0, 1, {OPERAND_LVT1}}, |
| 83 |
/* Store scalar variable at op1<=255 in frame; value is stktop */ |
| 84 |
{"storeScalar4", 5, 0, 1, {OPERAND_LVT4}}, |
| 85 |
/* Store scalar variable at op1 > 255 in frame; value is stktop */ |
| 86 |
{"storeScalarStk", 1, -1, 0, {OPERAND_NONE}}, |
| 87 |
/* Store scalar; value is stktop, scalar name is stknext */ |
| 88 |
{"storeArray1", 2, -1, 1, {OPERAND_LVT1}}, |
| 89 |
/* Store array element; array at op1<=255, value is top then elem */ |
| 90 |
{"storeArray4", 5, -1, 1, {OPERAND_LVT4}}, |
| 91 |
/* Store array element; array at op1>=256, value is top then elem */ |
| 92 |
{"storeArrayStk", 1, -2, 0, {OPERAND_NONE}}, |
| 93 |
/* Store array element; value is stktop, then elem, array names */ |
| 94 |
{"storeStk", 1, -1, 0, {OPERAND_NONE}}, |
| 95 |
/* Store general variable; value is stktop, then unparsed name */ |
| 96 |
|
| 97 |
{"incrScalar1", 2, 0, 1, {OPERAND_LVT1}}, |
| 98 |
/* Incr scalar at index op1<=255 in frame; incr amount is stktop */ |
| 99 |
{"incrScalarStk", 1, -1, 0, {OPERAND_NONE}}, |
| 100 |
/* Incr scalar; incr amount is stktop, scalar's name is stknext */ |
| 101 |
{"incrArray1", 2, -1, 1, {OPERAND_LVT1}}, |
| 102 |
/* Incr array elem; arr at slot op1<=255, amount is top then elem */ |
| 103 |
{"incrArrayStk", 1, -2, 0, {OPERAND_NONE}}, |
| 104 |
/* Incr array element; amount is top then elem then array names */ |
| 105 |
{"incrStk", 1, -1, 0, {OPERAND_NONE}}, |
| 106 |
/* Incr general variable; amount is stktop then unparsed var name */ |
| 107 |
{"incrScalar1Imm", 3, +1, 2, {OPERAND_LVT1, OPERAND_INT1}}, |
| 108 |
/* Incr scalar at slot op1 <= 255; amount is 2nd operand byte */ |
| 109 |
{"incrScalarStkImm", 2, 0, 1, {OPERAND_INT1}}, |
| 110 |
/* Incr scalar; scalar name is stktop; incr amount is op1 */ |
| 111 |
{"incrArray1Imm", 3, 0, 2, {OPERAND_LVT1, OPERAND_INT1}}, |
| 112 |
/* Incr array elem; array at slot op1 <= 255, elem is stktop, |
| 113 |
* amount is 2nd operand byte */ |
| 114 |
{"incrArrayStkImm", 2, -1, 1, {OPERAND_INT1}}, |
| 115 |
/* Incr array element; elem is top then array name, amount is op1 */ |
| 116 |
{"incrStkImm", 2, 0, 1, {OPERAND_INT1}}, |
| 117 |
/* Incr general variable; unparsed name is top, amount is op1 */ |
| 118 |
|
| 119 |
{"jump1", 2, 0, 1, {OPERAND_OFFSET1}}, |
| 120 |
/* Jump relative to (pc + op1) */ |
| 121 |
{"jump4", 5, 0, 1, {OPERAND_OFFSET4}}, |
| 122 |
/* Jump relative to (pc + op4) */ |
| 123 |
{"jumpTrue1", 2, -1, 1, {OPERAND_OFFSET1}}, |
| 124 |
/* Jump relative to (pc + op1) if stktop expr object is true */ |
| 125 |
{"jumpTrue4", 5, -1, 1, {OPERAND_OFFSET4}}, |
| 126 |
/* Jump relative to (pc + op4) if stktop expr object is true */ |
| 127 |
{"jumpFalse1", 2, -1, 1, {OPERAND_OFFSET1}}, |
| 128 |
/* Jump relative to (pc + op1) if stktop expr object is false */ |
| 129 |
{"jumpFalse4", 5, -1, 1, {OPERAND_OFFSET4}}, |
| 130 |
/* Jump relative to (pc + op4) if stktop expr object is false */ |
| 131 |
|
| 132 |
{"lor", 1, -1, 0, {OPERAND_NONE}}, |
| 133 |
/* Logical or: push (stknext || stktop) */ |
| 134 |
{"land", 1, -1, 0, {OPERAND_NONE}}, |
| 135 |
/* Logical and: push (stknext && stktop) */ |
| 136 |
{"bitor", 1, -1, 0, {OPERAND_NONE}}, |
| 137 |
/* Bitwise or: push (stknext | stktop) */ |
| 138 |
{"bitxor", 1, -1, 0, {OPERAND_NONE}}, |
| 139 |
/* Bitwise xor push (stknext ^ stktop) */ |
| 140 |
{"bitand", 1, -1, 0, {OPERAND_NONE}}, |
| 141 |
/* Bitwise and: push (stknext & stktop) */ |
| 142 |
{"eq", 1, -1, 0, {OPERAND_NONE}}, |
| 143 |
/* Equal: push (stknext == stktop) */ |
| 144 |
{"neq", 1, -1, 0, {OPERAND_NONE}}, |
| 145 |
/* Not equal: push (stknext != stktop) */ |
| 146 |
{"lt", 1, -1, 0, {OPERAND_NONE}}, |
| 147 |
/* Less: push (stknext < stktop) */ |
| 148 |
{"gt", 1, -1, 0, {OPERAND_NONE}}, |
| 149 |
/* Greater: push (stknext > stktop) */ |
| 150 |
{"le", 1, -1, 0, {OPERAND_NONE}}, |
| 151 |
/* Less or equal: push (stknext <= stktop) */ |
| 152 |
{"ge", 1, -1, 0, {OPERAND_NONE}}, |
| 153 |
/* Greater or equal: push (stknext >= stktop) */ |
| 154 |
{"lshift", 1, -1, 0, {OPERAND_NONE}}, |
| 155 |
/* Left shift: push (stknext << stktop) */ |
| 156 |
{"rshift", 1, -1, 0, {OPERAND_NONE}}, |
| 157 |
/* Right shift: push (stknext >> stktop) */ |
| 158 |
{"add", 1, -1, 0, {OPERAND_NONE}}, |
| 159 |
/* Add: push (stknext + stktop) */ |
| 160 |
{"sub", 1, -1, 0, {OPERAND_NONE}}, |
| 161 |
/* Sub: push (stkext - stktop) */ |
| 162 |
{"mult", 1, -1, 0, {OPERAND_NONE}}, |
| 163 |
/* Multiply: push (stknext * stktop) */ |
| 164 |
{"div", 1, -1, 0, {OPERAND_NONE}}, |
| 165 |
/* Divide: push (stknext / stktop) */ |
| 166 |
{"mod", 1, -1, 0, {OPERAND_NONE}}, |
| 167 |
/* Mod: push (stknext % stktop) */ |
| 168 |
{"uplus", 1, 0, 0, {OPERAND_NONE}}, |
| 169 |
/* Unary plus: push +stktop */ |
| 170 |
{"uminus", 1, 0, 0, {OPERAND_NONE}}, |
| 171 |
/* Unary minus: push -stktop */ |
| 172 |
{"bitnot", 1, 0, 0, {OPERAND_NONE}}, |
| 173 |
/* Bitwise not: push ~stktop */ |
| 174 |
{"not", 1, 0, 0, {OPERAND_NONE}}, |
| 175 |
/* Logical not: push !stktop */ |
| 176 |
{"callBuiltinFunc1", 2, 1, 1, {OPERAND_UINT1}}, |
| 177 |
/* Call builtin math function with index op1; any args are on stk */ |
| 178 |
{"callFunc1", 2, INT_MIN, 1, {OPERAND_UINT1}}, |
| 179 |
/* Call non-builtin func objv[0]; <objc,objv>=<op1,top op1> */ |
| 180 |
{"tryCvtToNumeric", 1, 0, 0, {OPERAND_NONE}}, |
| 181 |
/* Try converting stktop to first int then double if possible. */ |
| 182 |
|
| 183 |
{"break", 1, 0, 0, {OPERAND_NONE}}, |
| 184 |
/* Abort closest enclosing loop; if none, return TCL_BREAK code. */ |
| 185 |
{"continue", 1, 0, 0, {OPERAND_NONE}}, |
| 186 |
/* Skip to next iteration of closest enclosing loop; if none, return |
| 187 |
* TCL_CONTINUE code. */ |
| 188 |
|
| 189 |
{"foreach_start4", 5, 0, 1, {OPERAND_AUX4}}, |
| 190 |
/* Initialize execution of a foreach loop. Operand is aux data index |
| 191 |
* of the ForeachInfo structure for the foreach command. */ |
| 192 |
{"foreach_step4", 5, +1, 1, {OPERAND_AUX4}}, |
| 193 |
/* "Step" or begin next iteration of foreach loop. Push 0 if to |
| 194 |
* terminate loop, else push 1. */ |
| 195 |
|
| 196 |
{"beginCatch4", 5, 0, 1, {OPERAND_UINT4}}, |
| 197 |
/* Record start of catch with the operand's exception index. Push the |
| 198 |
* current stack depth onto a special catch stack. */ |
| 199 |
{"endCatch", 1, 0, 0, {OPERAND_NONE}}, |
| 200 |
/* End of last catch. Pop the bytecode interpreter's catch stack. */ |
| 201 |
{"pushResult", 1, +1, 0, {OPERAND_NONE}}, |
| 202 |
/* Push the interpreter's object result onto the stack. */ |
| 203 |
{"pushReturnCode", 1, +1, 0, {OPERAND_NONE}}, |
| 204 |
/* Push interpreter's return code (e.g. TCL_OK or TCL_ERROR) as a new |
| 205 |
* object onto the stack. */ |
| 206 |
|
| 207 |
{"streq", 1, -1, 0, {OPERAND_NONE}}, |
| 208 |
/* Str Equal: push (stknext eq stktop) */ |
| 209 |
{"strneq", 1, -1, 0, {OPERAND_NONE}}, |
| 210 |
/* Str !Equal: push (stknext neq stktop) */ |
| 211 |
{"strcmp", 1, -1, 0, {OPERAND_NONE}}, |
| 212 |
/* Str Compare: push (stknext cmp stktop) */ |
| 213 |
{"strlen", 1, 0, 0, {OPERAND_NONE}}, |
| 214 |
/* Str Length: push (strlen stktop) */ |
| 215 |
{"strindex", 1, -1, 0, {OPERAND_NONE}}, |
| 216 |
/* Str Index: push (strindex stknext stktop) */ |
| 217 |
{"strmatch", 2, -1, 1, {OPERAND_INT1}}, |
| 218 |
/* Str Match: push (strmatch stknext stktop) opnd == nocase */ |
| 219 |
|
| 220 |
{"list", 5, INT_MIN, 1, {OPERAND_UINT4}}, |
| 221 |
/* List: push (stk1 stk2 ... stktop) */ |
| 222 |
{"listIndex", 1, -1, 0, {OPERAND_NONE}}, |
| 223 |
/* List Index: push (listindex stknext stktop) */ |
| 224 |
{"listLength", 1, 0, 0, {OPERAND_NONE}}, |
| 225 |
/* List Len: push (listlength stktop) */ |
| 226 |
|
| 227 |
{"appendScalar1", 2, 0, 1, {OPERAND_LVT1}}, |
| 228 |
/* Append scalar variable at op1<=255 in frame; value is stktop */ |
| 229 |
{"appendScalar4", 5, 0, 1, {OPERAND_LVT4}}, |
| 230 |
/* Append scalar variable at op1 > 255 in frame; value is stktop */ |
| 231 |
{"appendArray1", 2, -1, 1, {OPERAND_LVT1}}, |
| 232 |
/* Append array element; array at op1<=255, value is top then elem */ |
| 233 |
{"appendArray4", 5, -1, 1, {OPERAND_LVT4}}, |
| 234 |
/* Append array element; array at op1>=256, value is top then elem */ |
| 235 |
{"appendArrayStk", 1, -2, 0, {OPERAND_NONE}}, |
| 236 |
/* Append array element; value is stktop, then elem, array names */ |
| 237 |
{"appendStk", 1, -1, 0, {OPERAND_NONE}}, |
| 238 |
/* Append general variable; value is stktop, then unparsed name */ |
| 239 |
{"lappendScalar1", 2, 0, 1, {OPERAND_LVT1}}, |
| 240 |
/* Lappend scalar variable at op1<=255 in frame; value is stktop */ |
| 241 |
{"lappendScalar4", 5, 0, 1, {OPERAND_LVT4}}, |
| 242 |
/* Lappend scalar variable at op1 > 255 in frame; value is stktop */ |
| 243 |
{"lappendArray1", 2, -1, 1, {OPERAND_LVT1}}, |
| 244 |
/* Lappend array element; array at op1<=255, value is top then elem */ |
| 245 |
{"lappendArray4", 5, -1, 1, {OPERAND_LVT4}}, |
| 246 |
/* Lappend array element; array at op1>=256, value is top then elem */ |
| 247 |
{"lappendArrayStk", 1, -2, 0, {OPERAND_NONE}}, |
| 248 |
/* Lappend array element; value is stktop, then elem, array names */ |
| 249 |
{"lappendStk", 1, -1, 0, {OPERAND_NONE}}, |
| 250 |
/* Lappend general variable; value is stktop, then unparsed name */ |
| 251 |
|
| 252 |
{"lindexMulti", 5, INT_MIN, 1, {OPERAND_UINT4}}, |
| 253 |
/* Lindex with generalized args, operand is number of stacked objs |
| 254 |
* used: (operand-1) entries from stktop are the indices; then list to |
| 255 |
* process. */ |
| 256 |
{"over", 5, +1, 1, {OPERAND_UINT4}}, |
| 257 |
/* Duplicate the arg-th element from top of stack (TOS=0) */ |
| 258 |
{"lsetList", 1, -2, 0, {OPERAND_NONE}}, |
| 259 |
/* Four-arg version of 'lset'. stktop is old value; next is new |
| 260 |
* element value, next is the index list; pushes new value */ |
| 261 |
{"lsetFlat", 5, INT_MIN, 1, {OPERAND_UINT4}}, |
| 262 |
/* Three- or >=5-arg version of 'lset', operand is number of stacked |
| 263 |
* objs: stktop is old value, next is new element value, next come |
| 264 |
* (operand-2) indices; pushes the new value. |
| 265 |
*/ |
| 266 |
|
| 267 |
{"returnImm", 9, -1, 2, {OPERAND_INT4, OPERAND_UINT4}}, |
| 268 |
/* Compiled [return], code, level are operands; options and result |
| 269 |
* are on the stack. */ |
| 270 |
{"expon", 1, -1, 0, {OPERAND_NONE}}, |
| 271 |
/* Binary exponentiation operator: push (stknext ** stktop) */ |
| 272 |
|
| 273 |
/* |
| 274 |
* NOTE: the stack effects of expandStkTop and invokeExpanded are wrong - |
| 275 |
* but it cannot be done right at compile time, the stack effect is only |
| 276 |
* known at run time. The value for invokeExpanded is estimated better at |
| 277 |
* compile time. |
| 278 |
* See the comments further down in this file, where INST_INVOKE_EXPANDED |
| 279 |
* is emitted. |
| 280 |
*/ |
| 281 |
{"expandStart", 1, 0, 0, {OPERAND_NONE}}, |
| 282 |
/* Start of command with {*} (expanded) arguments */ |
| 283 |
{"expandStkTop", 5, 0, 1, {OPERAND_UINT4}}, |
| 284 |
/* Expand the list at stacktop: push its elements on the stack */ |
| 285 |
{"invokeExpanded", 1, 0, 0, {OPERAND_NONE}}, |
| 286 |
/* Invoke the command marked by the last 'expandStart' */ |
| 287 |
|
| 288 |
{"listIndexImm", 5, 0, 1, {OPERAND_IDX4}}, |
| 289 |
/* List Index: push (lindex stktop op4) */ |
| 290 |
{"listRangeImm", 9, 0, 2, {OPERAND_IDX4, OPERAND_IDX4}}, |
| 291 |
/* List Range: push (lrange stktop op4 op4) */ |
| 292 |
{"startCommand", 9, 0, 2, {OPERAND_OFFSET4, OPERAND_UINT4}}, |
| 293 |
/* Start of bytecoded command: op is the length of the cmd's code, op2 |
| 294 |
* is number of commands here */ |
| 295 |
|
| 296 |
{"listIn", 1, -1, 0, {OPERAND_NONE}}, |
| 297 |
/* List containment: push [lsearch stktop stknext]>=0) */ |
| 298 |
{"listNotIn", 1, -1, 0, {OPERAND_NONE}}, |
| 299 |
/* List negated containment: push [lsearch stktop stknext]<0) */ |
| 300 |
|
| 301 |
{"pushReturnOpts", 1, +1, 0, {OPERAND_NONE}}, |
| 302 |
/* Push the interpreter's return option dictionary as an object on the |
| 303 |
* stack. */ |
| 304 |
{"returnStk", 1, -1, 0, {OPERAND_NONE}}, |
| 305 |
/* Compiled [return]; options and result are on the stack, code and |
| 306 |
* level are in the options. */ |
| 307 |
|
| 308 |
{"dictGet", 5, INT_MIN, 1, {OPERAND_UINT4}}, |
| 309 |
/* The top op4 words (min 1) are a key path into the dictionary just |
| 310 |
* below the keys on the stack, and all those values are replaced by |
| 311 |
* the value read out of that key-path (like [dict get]). |
| 312 |
* Stack: ... dict key1 ... keyN => ... value */ |
| 313 |
{"dictSet", 9, INT_MIN, 2, {OPERAND_UINT4, OPERAND_LVT4}}, |
| 314 |
/* Update a dictionary value such that the keys are a path pointing to |
| 315 |
* the value. op4#1 = numKeys, op4#2 = LVTindex |
| 316 |
* Stack: ... key1 ... keyN value => ... newDict */ |
| 317 |
{"dictUnset", 9, INT_MIN, 2, {OPERAND_UINT4, OPERAND_LVT4}}, |
| 318 |
/* Update a dictionary value such that the keys are not a path pointing |
| 319 |
* to any value. op4#1 = numKeys, op4#2 = LVTindex |
| 320 |
* Stack: ... key1 ... keyN => ... newDict */ |
| 321 |
{"dictIncrImm", 9, 0, 2, {OPERAND_INT4, OPERAND_LVT4}}, |
| 322 |
/* Update a dictionary value such that the value pointed to by key is |
| 323 |
* incremented by some value (or set to it if the key isn't in the |
| 324 |
* dictionary at all). op4#1 = incrAmount, op4#2 = LVTindex |
| 325 |
* Stack: ... key => ... newDict */ |
| 326 |
{"dictAppend", 5, -1, 1, {OPERAND_LVT4}}, |
| 327 |
/* Update a dictionary value such that the value pointed to by key has |
| 328 |
* some value string-concatenated onto it. op4 = LVTindex |
| 329 |
* Stack: ... key valueToAppend => ... newDict */ |
| 330 |
{"dictLappend", 5, -1, 1, {OPERAND_LVT4}}, |
| 331 |
/* Update a dictionary value such that the value pointed to by key has |
| 332 |
* some value list-appended onto it. op4 = LVTindex |
| 333 |
* Stack: ... key valueToAppend => ... newDict */ |
| 334 |
{"dictFirst", 5, +2, 1, {OPERAND_LVT4}}, |
| 335 |
/* Begin iterating over the dictionary, using the local scalar |
| 336 |
* indicated by op4 to hold the iterator state. The local scalar |
| 337 |
* should not refer to a named variable as the value is not wholly |
| 338 |
* managed correctly. |
| 339 |
* Stack: ... dict => ... value key doneBool */ |
| 340 |
{"dictNext", 5, +3, 1, {OPERAND_LVT4}}, |
| 341 |
/* Get the next iteration from the iterator in op4's local scalar. |
| 342 |
* Stack: ... => ... value key doneBool */ |
| 343 |
{"dictDone", 5, 0, 1, {OPERAND_LVT4}}, |
| 344 |
/* Terminate the iterator in op4's local scalar. Use unsetScalar |
| 345 |
* instead (with 0 for flags). */ |
| 346 |
{"dictUpdateStart", 9, 0, 2, {OPERAND_LVT4, OPERAND_AUX4}}, |
| 347 |
/* Create the variables (described in the aux data referred to by the |
| 348 |
* second immediate argument) to mirror the state of the dictionary in |
| 349 |
* the variable referred to by the first immediate argument. The list |
| 350 |
* of keys (top of the stack, not popped) must be the same length as |
| 351 |
* the list of variables. |
| 352 |
* Stack: ... keyList => ... keyList */ |
| 353 |
{"dictUpdateEnd", 9, -1, 2, {OPERAND_LVT4, OPERAND_AUX4}}, |
| 354 |
/* Reflect the state of local variables (described in the aux data |
| 355 |
* referred to by the second immediate argument) back to the state of |
| 356 |
* the dictionary in the variable referred to by the first immediate |
| 357 |
* argument. The list of keys (popped from the stack) must be the same |
| 358 |
* length as the list of variables. |
| 359 |
* Stack: ... keyList => ... */ |
| 360 |
{"jumpTable", 5, -1, 1, {OPERAND_AUX4}}, |
| 361 |
/* Jump according to the jump-table (in AuxData as indicated by the |
| 362 |
* operand) and the argument popped from the list. Always executes the |
| 363 |
* next instruction if no match against the table's entries was found. |
| 364 |
* Stack: ... value => ... |
| 365 |
* Note that the jump table contains offsets relative to the PC when |
| 366 |
* it points to this instruction; the code is relocatable. */ |
| 367 |
{"upvar", 5, -1, 1, {OPERAND_LVT4}}, |
| 368 |
/* finds level and otherName in stack, links to local variable at |
| 369 |
* index op1. Leaves the level on stack. */ |
| 370 |
{"nsupvar", 5, -1, 1, {OPERAND_LVT4}}, |
| 371 |
/* finds namespace and otherName in stack, links to local variable at |
| 372 |
* index op1. Leaves the namespace on stack. */ |
| 373 |
{"variable", 5, -1, 1, {OPERAND_LVT4}}, |
| 374 |
/* finds namespace and otherName in stack, links to local variable at |
| 375 |
* index op1. Leaves the namespace on stack. */ |
| 376 |
{"syntax", 9, -1, 2, {OPERAND_INT4, OPERAND_UINT4}}, |
| 377 |
/* Compiled bytecodes to signal syntax error. Equivalent to returnImm |
| 378 |
* except for the ERR_ALREADY_LOGGED flag in the interpreter. */ |
| 379 |
{"reverse", 5, 0, 1, {OPERAND_UINT4}}, |
| 380 |
/* Reverse the order of the arg elements at the top of stack */ |
| 381 |
|
| 382 |
{"regexp", 2, -1, 1, {OPERAND_INT1}}, |
| 383 |
/* Regexp: push (regexp stknext stktop) opnd == nocase */ |
| 384 |
|
| 385 |
{"existScalar", 5, 1, 1, {OPERAND_LVT4}}, |
| 386 |
/* Test if scalar variable at index op1 in call frame exists */ |
| 387 |
{"existArray", 5, 0, 1, {OPERAND_LVT4}}, |
| 388 |
/* Test if array element exists; array at slot op1, element is |
| 389 |
* stktop */ |
| 390 |
{"existArrayStk", 1, -1, 0, {OPERAND_NONE}}, |
| 391 |
/* Test if array element exists; element is stktop, array name is |
| 392 |
* stknext */ |
| 393 |
{"existStk", 1, 0, 0, {OPERAND_NONE}}, |
| 394 |
/* Test if general variable exists; unparsed variable name is stktop*/ |
| 395 |
|
| 396 |
{"nop", 1, 0, 0, {OPERAND_NONE}}, |
| 397 |
/* Do nothing */ |
| 398 |
{"returnCodeBranch", 1, -1, 0, {OPERAND_NONE}}, |
| 399 |
/* Jump to next instruction based on the return code on top of stack |
| 400 |
* ERROR: +1; RETURN: +3; BREAK: +5; CONTINUE: +7; |
| 401 |
* Other non-OK: +9 |
| 402 |
*/ |
| 403 |
|
| 404 |
{"unsetScalar", 6, 0, 2, {OPERAND_UINT1, OPERAND_LVT4}}, |
| 405 |
/* Make scalar variable at index op2 in call frame cease to exist; |
| 406 |
* op1 is 1 for errors on problems, 0 otherwise */ |
| 407 |
{"unsetArray", 6, -1, 2, {OPERAND_UINT1, OPERAND_LVT4}}, |
| 408 |
/* Make array element cease to exist; array at slot op2, element is |
| 409 |
* stktop; op1 is 1 for errors on problems, 0 otherwise */ |
| 410 |
{"unsetArrayStk", 2, -2, 1, {OPERAND_UINT1}}, |
| 411 |
/* Make array element cease to exist; element is stktop, array name is |
| 412 |
* stknext; op1 is 1 for errors on problems, 0 otherwise */ |
| 413 |
{"unsetStk", 2, -1, 1, {OPERAND_UINT1}}, |
| 414 |
/* Make general variable cease to exist; unparsed variable name is |
| 415 |
* stktop; op1 is 1 for errors on problems, 0 otherwise */ |
| 416 |
|
| 417 |
{"dictExpand", 1, -1, 0, {OPERAND_NONE}}, |
| 418 |
/* Probe into a dict and extract it (or a subdict of it) into |
| 419 |
* variables with matched names. Produces list of keys bound as |
| 420 |
* result. Part of [dict with]. |
| 421 |
* Stack: ... dict path => ... keyList */ |
| 422 |
{"dictRecombineStk", 1, -3, 0, {OPERAND_NONE}}, |
| 423 |
/* Map variable contents back into a dictionary in a variable. Part of |
| 424 |
* [dict with]. |
| 425 |
* Stack: ... dictVarName path keyList => ... */ |
| 426 |
{"dictRecombineImm", 5, -2, 1, {OPERAND_LVT4}}, |
| 427 |
/* Map variable contents back into a dictionary in the local variable |
| 428 |
* indicated by the LVT index. Part of [dict with]. |
| 429 |
* Stack: ... path keyList => ... */ |
| 430 |
{"dictExists", 5, INT_MIN, 1, {OPERAND_UINT4}}, |
| 431 |
/* The top op4 words (min 1) are a key path into the dictionary just |
| 432 |
* below the keys on the stack, and all those values are replaced by a |
| 433 |
* boolean indicating whether it is possible to read out a value from |
| 434 |
* that key-path (like [dict exists]). |
| 435 |
* Stack: ... dict key1 ... keyN => ... boolean */ |
| 436 |
{"verifyDict", 1, -1, 0, {OPERAND_NONE}}, |
| 437 |
/* Verifies that the word on the top of the stack is a dictionary, |
| 438 |
* popping it if it is and throwing an error if it is not. |
| 439 |
* Stack: ... value => ... */ |
| 440 |
|
| 441 |
{"strmap", 1, -2, 0, {OPERAND_NONE}}, |
| 442 |
/* Simplified version of [string map] that only applies one change |
| 443 |
* string, and only case-sensitively. |
| 444 |
* Stack: ... from to string => ... changedString */ |
| 445 |
{"strfind", 1, -1, 0, {OPERAND_NONE}}, |
| 446 |
/* Find the first index of a needle string in a haystack string, |
| 447 |
* producing the index (integer) or -1 if nothing found. |
| 448 |
* Stack: ... needle haystack => ... index */ |
| 449 |
{"strrfind", 1, -1, 0, {OPERAND_NONE}}, |
| 450 |
/* Find the last index of a needle string in a haystack string, |
| 451 |
* producing the index (integer) or -1 if nothing found. |
| 452 |
* Stack: ... needle haystack => ... index */ |
| 453 |
{"strrangeImm", 9, 0, 2, {OPERAND_IDX4, OPERAND_IDX4}}, |
| 454 |
/* String Range: push (string range stktop op4 op4) */ |
| 455 |
{"strrange", 1, -2, 0, {OPERAND_NONE}}, |
| 456 |
/* String Range with non-constant arguments. |
| 457 |
* Stack: ... string idxA idxB => ... substring */ |
| 458 |
|
| 459 |
{"yield", 1, 0, 0, {OPERAND_NONE}}, |
| 460 |
/* Makes the current coroutine yield the value at the top of the |
| 461 |
* stack, and places the response back on top of the stack when it |
| 462 |
* resumes. |
| 463 |
* Stack: ... valueToYield => ... resumeValue */ |
| 464 |
{"coroName", 1, +1, 0, {OPERAND_NONE}}, |
| 465 |
/* Push the name of the interpreter's current coroutine as an object |
| 466 |
* on the stack. */ |
| 467 |
{"tailcall", 2, INT_MIN, 1, {OPERAND_UINT1}}, |
| 468 |
/* Do a tailcall with the opnd items on the stack as the thing to |
| 469 |
* tailcall to; opnd must be greater than 0 for the semantics to work |
| 470 |
* right. */ |
| 471 |
|
| 472 |
{"currentNamespace", 1, +1, 0, {OPERAND_NONE}}, |
| 473 |
/* Push the name of the interpreter's current namespace as an object |
| 474 |
* on the stack. */ |
| 475 |
{"infoLevelNumber", 1, +1, 0, {OPERAND_NONE}}, |
| 476 |
/* Push the stack depth (i.e., [info level]) of the interpreter as an |
| 477 |
* object on the stack. */ |
| 478 |
{"infoLevelArgs", 1, 0, 0, {OPERAND_NONE}}, |
| 479 |
/* Push the argument words to a stack depth (i.e., [info level <n>]) |
| 480 |
* of the interpreter as an object on the stack. |
| 481 |
* Stack: ... depth => ... argList */ |
| 482 |
{"resolveCmd", 1, 0, 0, {OPERAND_NONE}}, |
| 483 |
/* Resolves the command named on the top of the stack to its fully |
| 484 |
* qualified version, or produces the empty string if no such command |
| 485 |
* exists. Never generates errors. |
| 486 |
* Stack: ... cmdName => ... fullCmdName */ |
| 487 |
|
| 488 |
{"tclooSelf", 1, +1, 0, {OPERAND_NONE}}, |
| 489 |
/* Push the identity of the current TclOO object (i.e., the name of |
| 490 |
* its current public access command) on the stack. */ |
| 491 |
{"tclooClass", 1, 0, 0, {OPERAND_NONE}}, |
| 492 |
/* Push the class of the TclOO object named at the top of the stack |
| 493 |
* onto the stack. |
| 494 |
* Stack: ... object => ... class */ |
| 495 |
{"tclooNamespace", 1, 0, 0, {OPERAND_NONE}}, |
| 496 |
/* Push the namespace of the TclOO object named at the top of the |
| 497 |
* stack onto the stack. |
| 498 |
* Stack: ... object => ... namespace */ |
| 499 |
{"tclooIsObject", 1, 0, 0, {OPERAND_NONE}}, |
| 500 |
/* Push whether the value named at the top of the stack is a TclOO |
| 501 |
* object (i.e., a boolean). Can corrupt the interpreter result |
| 502 |
* despite not throwing, so not safe for use in a post-exception |
| 503 |
* context. |
| 504 |
* Stack: ... value => ... boolean */ |
| 505 |
|
| 506 |
{"arrayExistsStk", 1, 0, 0, {OPERAND_NONE}}, |
| 507 |
/* Looks up the element on the top of the stack and tests whether it |
| 508 |
* is an array. Pushes a boolean describing whether this is the |
| 509 |
* case. Also runs the whole-array trace on the named variable, so can |
| 510 |
* throw anything. |
| 511 |
* Stack: ... varName => ... boolean */ |
| 512 |
{"arrayExistsImm", 5, +1, 1, {OPERAND_LVT4}}, |
| 513 |
/* Looks up the variable indexed by opnd and tests whether it is an |
| 514 |
* array. Pushes a boolean describing whether this is the case. Also |
| 515 |
* runs the whole-array trace on the named variable, so can throw |
| 516 |
* anything. |
| 517 |
* Stack: ... => ... boolean */ |
| 518 |
{"arrayMakeStk", 1, -1, 0, {OPERAND_NONE}}, |
| 519 |
/* Forces the element on the top of the stack to be the name of an |
| 520 |
* array. |
| 521 |
* Stack: ... varName => ... */ |
| 522 |
{"arrayMakeImm", 5, 0, 1, {OPERAND_LVT4}}, |
| 523 |
/* Forces the variable indexed by opnd to be an array. Does not touch |
| 524 |
* the stack. */ |
| 525 |
|
| 526 |
{"invokeReplace", 6, INT_MIN, 2, {OPERAND_UINT4,OPERAND_UINT1}}, |
| 527 |
/* Invoke command named objv[0], replacing the first two words with |
| 528 |
* the word at the top of the stack; |
| 529 |
* <objc,objv> = <op4,top op4 after popping 1> */ |
| 530 |
|
| 531 |
{"listConcat", 1, -1, 0, {OPERAND_NONE}}, |
| 532 |
/* Concatenates the two lists at the top of the stack into a single |
| 533 |
* list and pushes that resulting list onto the stack. |
| 534 |
* Stack: ... list1 list2 => ... [lconcat list1 list2] */ |
| 535 |
|
| 536 |
{"expandDrop", 1, 0, 0, {OPERAND_NONE}}, |
| 537 |
/* Drops an element from the auxiliary stack, popping stack elements |
| 538 |
* until the matching stack depth is reached. */ |
| 539 |
|
| 540 |
/* New foreach implementation */ |
| 541 |
{"foreach_start", 5, +2, 1, {OPERAND_AUX4}}, |
| 542 |
/* Initialize execution of a foreach loop. Operand is aux data index |
| 543 |
* of the ForeachInfo structure for the foreach command. It pushes 2 |
| 544 |
* elements which hold runtime params for foreach_step, they are later |
| 545 |
* dropped by foreach_end together with the value lists. NOTE that the |
| 546 |
* iterator-tracker and info reference must not be passed to bytecodes |
| 547 |
* that handle normal Tcl values. NOTE that this instruction jumps to |
| 548 |
* the foreach_step instruction paired with it; the stack info below |
| 549 |
* is only nominal. |
| 550 |
* Stack: ... listObjs... => ... listObjs... iterTracker info */ |
| 551 |
{"foreach_step", 1, 0, 0, {OPERAND_NONE}}, |
| 552 |
/* "Step" or begin next iteration of foreach loop. Assigns to foreach |
| 553 |
* iteration variables. May jump to straight after the foreach_start |
| 554 |
* that pushed the iterTracker and info values. MUST be followed |
| 555 |
* immediately by a foreach_end. |
| 556 |
* Stack: ... listObjs... iterTracker info => |
| 557 |
* ... listObjs... iterTracker info */ |
| 558 |
{"foreach_end", 1, 0, 0, {OPERAND_NONE}}, |
| 559 |
/* Clean up a foreach loop by dropping the info value, the tracker |
| 560 |
* value and the lists that were being iterated over. |
| 561 |
* Stack: ... listObjs... iterTracker info => ... */ |
| 562 |
{"lmap_collect", 1, -1, 0, {OPERAND_NONE}}, |
| 563 |
/* Appends the value at the top of the stack to the list located on |
| 564 |
* the stack the "other side" of the foreach-related values. |
| 565 |
* Stack: ... collector listObjs... iterTracker info value => |
| 566 |
* ... collector listObjs... iterTracker info */ |
| 567 |
|
| 568 |
{"strtrim", 1, -1, 0, {OPERAND_NONE}}, |
| 569 |
/* [string trim] core: removes the characters (designated by the value |
| 570 |
* at the top of the stack) from both ends of the string and pushes |
| 571 |
* the resulting string. |
| 572 |
* Stack: ... string charset => ... trimmedString */ |
| 573 |
{"strtrimLeft", 1, -1, 0, {OPERAND_NONE}}, |
| 574 |
/* [string trimleft] core: removes the characters (designated by the |
| 575 |
* value at the top of the stack) from the left of the string and |
| 576 |
* pushes the resulting string. |
| 577 |
* Stack: ... string charset => ... trimmedString */ |
| 578 |
{"strtrimRight", 1, -1, 0, {OPERAND_NONE}}, |
| 579 |
/* [string trimright] core: removes the characters (designated by the |
| 580 |
* value at the top of the stack) from the right of the string and |
| 581 |
* pushes the resulting string. |
| 582 |
* Stack: ... string charset => ... trimmedString */ |
| 583 |
|
| 584 |
{"concatStk", 5, INT_MIN, 1, {OPERAND_UINT4}}, |
| 585 |
/* Wrapper round Tcl_ConcatObj(), used for [concat] and [eval]. opnd |
| 586 |
* is number of values to concatenate. |
| 587 |
* Operation: push concat(stk1 stk2 ... stktop) */ |
| 588 |
|
| 589 |
{"strcaseUpper", 1, 0, 0, {OPERAND_NONE}}, |
| 590 |
/* [string toupper] core: converts whole string to upper case using |
| 591 |
* the default (extended "C" locale) rules. |
| 592 |
* Stack: ... string => ... newString */ |
| 593 |
{"strcaseLower", 1, 0, 0, {OPERAND_NONE}}, |
| 594 |
/* [string tolower] core: converts whole string to upper case using |
| 595 |
* the default (extended "C" locale) rules. |
| 596 |
* Stack: ... string => ... newString */ |
| 597 |
{"strcaseTitle", 1, 0, 0, {OPERAND_NONE}}, |
| 598 |
/* [string totitle] core: converts whole string to upper case using |
| 599 |
* the default (extended "C" locale) rules. |
| 600 |
* Stack: ... string => ... newString */ |
| 601 |
{"strreplace", 1, -3, 0, {OPERAND_NONE}}, |
| 602 |
/* [string replace] core: replaces a non-empty range of one string |
| 603 |
* with the contents of another. |
| 604 |
* Stack: ... string fromIdx toIdx replacement => ... newString */ |
| 605 |
|
| 606 |
{"originCmd", 1, 0, 0, {OPERAND_NONE}}, |
| 607 |
/* Reports which command was the origin (via namespace import chain) |
| 608 |
* of the command named on the top of the stack. |
| 609 |
* Stack: ... cmdName => ... fullOriginalCmdName */ |
| 610 |
|
| 611 |
{"tclooNext", 2, INT_MIN, 1, {OPERAND_UINT1}}, |
| 612 |
/* Call the next item on the TclOO call chain, passing opnd arguments |
| 613 |
* (min 1, max 255, *includes* "next"). The result of the invoked |
| 614 |
* method implementation will be pushed on the stack in place of the |
| 615 |
* arguments (similar to invokeStk). |
| 616 |
* Stack: ... "next" arg2 arg3 -- argN => ... result */ |
| 617 |
{"tclooNextClass", 2, INT_MIN, 1, {OPERAND_UINT1}}, |
| 618 |
/* Call the following item on the TclOO call chain defined by class |
| 619 |
* className, passing opnd arguments (min 2, max 255, *includes* |
| 620 |
* "nextto" and the class name). The result of the invoked method |
| 621 |
* implementation will be pushed on the stack in place of the |
| 622 |
* arguments (similar to invokeStk). |
| 623 |
* Stack: ... "nextto" className arg3 arg4 -- argN => ... result */ |
| 624 |
|
| 625 |
{"yieldToInvoke", 1, 0, 0, {OPERAND_NONE}}, |
| 626 |
/* Makes the current coroutine yield the value at the top of the |
| 627 |
* stack, invoking the given command/args with resolution in the given |
| 628 |
* namespace (all packed into a list), and places the list of values |
| 629 |
* that are the response back on top of the stack when it resumes. |
| 630 |
* Stack: ... [list ns cmd arg1 ... argN] => ... resumeList */ |
| 631 |
|
| 632 |
{"numericType", 1, 0, 0, {OPERAND_NONE}}, |
| 633 |
/* Pushes the numeric type code of the word at the top of the stack. |
| 634 |
* Stack: ... value => ... typeCode */ |
| 635 |
{"tryCvtToBoolean", 1, +1, 0, {OPERAND_NONE}}, |
| 636 |
/* Try converting stktop to boolean if possible. No errors. |
| 637 |
* Stack: ... value => ... value isStrictBool */ |
| 638 |
{"strclass", 2, 0, 1, {OPERAND_SCLS1}}, |
| 639 |
/* See if all the characters of the given string are a member of the |
| 640 |
* specified (by opnd) character class. Note that an empty string will |
| 641 |
* satisfy the class check (standard definition of "all"). |
| 642 |
* Stack: ... stringValue => ... boolean */ |
| 643 |
|
| 644 |
{"lappendList", 5, 0, 1, {OPERAND_LVT4}}, |
| 645 |
/* Lappend list to scalar variable at op4 in frame. |
| 646 |
* Stack: ... list => ... listVarContents */ |
| 647 |
{"lappendListArray", 5, -1, 1, {OPERAND_LVT4}}, |
| 648 |
/* Lappend list to array element; array at op4. |
| 649 |
* Stack: ... elem list => ... listVarContents */ |
| 650 |
{"lappendListArrayStk", 1, -2, 0, {OPERAND_NONE}}, |
| 651 |
/* Lappend list to array element. |
| 652 |
* Stack: ... arrayName elem list => ... listVarContents */ |
| 653 |
{"lappendListStk", 1, -1, 0, {OPERAND_NONE}}, |
| 654 |
/* Lappend list to general variable. |
| 655 |
* Stack: ... varName list => ... listVarContents */ |
| 656 |
|
| 657 |
{"clockRead", 2, +1, 1, {OPERAND_UINT1}}, |
| 658 |
/* Read clock out to the stack. Operand is which clock to read |
| 659 |
* 0=clicks, 1=microseconds, 2=milliseconds, 3=seconds. |
| 660 |
* Stack: ... => ... time */ |
| 661 |
|
| 662 |
{NULL, 0, 0, 0, {OPERAND_NONE}} |
| 663 |
}; |
| 664 |
|
| 665 |
/* |
| 666 |
* Prototypes for procedures defined later in this file: |
| 667 |
*/ |
| 668 |
|
| 669 |
static ByteCode * CompileSubstObj(Tcl_Interp *interp, Tcl_Obj *objPtr, |
| 670 |
int flags); |
| 671 |
static void DupByteCodeInternalRep(Tcl_Obj *srcPtr, |
| 672 |
Tcl_Obj *copyPtr); |
| 673 |
static unsigned char * EncodeCmdLocMap(CompileEnv *envPtr, |
| 674 |
ByteCode *codePtr, unsigned char *startPtr); |
| 675 |
static void EnterCmdExtentData(CompileEnv *envPtr, |
| 676 |
int cmdNumber, int numSrcBytes, int numCodeBytes); |
| 677 |
static void EnterCmdStartData(CompileEnv *envPtr, |
| 678 |
int cmdNumber, int srcOffset, int codeOffset); |
| 679 |
static void FreeByteCodeInternalRep(Tcl_Obj *objPtr); |
| 680 |
static void FreeSubstCodeInternalRep(Tcl_Obj *objPtr); |
| 681 |
static int GetCmdLocEncodingSize(CompileEnv *envPtr); |
| 682 |
static int IsCompactibleCompileEnv(Tcl_Interp *interp, |
| 683 |
CompileEnv *envPtr); |
| 684 |
#ifdef TCL_COMPILE_STATS |
| 685 |
static void RecordByteCodeStats(ByteCode *codePtr); |
| 686 |
#endif /* TCL_COMPILE_STATS */ |
| 687 |
static int SetByteCodeFromAny(Tcl_Interp *interp, |
| 688 |
Tcl_Obj *objPtr); |
| 689 |
static void StartExpanding(CompileEnv *envPtr); |
| 690 |
|
| 691 |
/* |
| 692 |
* TIP #280: Helper for building the per-word line information of all compiled |
| 693 |
* commands. |
| 694 |
*/ |
| 695 |
static void EnterCmdWordData(ExtCmdLoc *eclPtr, int srcOffset, |
| 696 |
Tcl_Token *tokenPtr, const char *cmd, int len, |
| 697 |
int numWords, int line, int *clNext, int **lines, |
| 698 |
CompileEnv *envPtr); |
| 699 |
static void ReleaseCmdWordData(ExtCmdLoc *eclPtr); |
| 700 |
|
| 701 |
/* |
| 702 |
* The structure below defines the bytecode Tcl object type by means of |
| 703 |
* procedures that can be invoked by generic object code. |
| 704 |
*/ |
| 705 |
|
| 706 |
const Tcl_ObjType tclByteCodeType = { |
| 707 |
"bytecode", /* name */ |
| 708 |
FreeByteCodeInternalRep, /* freeIntRepProc */ |
| 709 |
DupByteCodeInternalRep, /* dupIntRepProc */ |
| 710 |
NULL, /* updateStringProc */ |
| 711 |
SetByteCodeFromAny /* setFromAnyProc */ |
| 712 |
}; |
| 713 |
|
| 714 |
/* |
| 715 |
* The structure below defines a bytecode Tcl object type to hold the |
| 716 |
* compiled bytecode for the [subst]itution of Tcl values. |
| 717 |
*/ |
| 718 |
|
| 719 |
static const Tcl_ObjType substCodeType = { |
| 720 |
"substcode", /* name */ |
| 721 |
FreeSubstCodeInternalRep, /* freeIntRepProc */ |
| 722 |
DupByteCodeInternalRep, /* dupIntRepProc - shared with bytecode */ |
| 723 |
NULL, /* updateStringProc */ |
| 724 |
NULL, /* setFromAnyProc */ |
| 725 |
}; |
| 726 |
|
| 727 |
/* |
| 728 |
* Helper macros. |
| 729 |
*/ |
| 730 |
|
| 731 |
#define TclIncrUInt4AtPtr(ptr, delta) \ |
| 732 |
TclStoreInt4AtPtr(TclGetUInt4AtPtr(ptr)+(delta), (ptr)); |
| 733 |
|
| 734 |
/* |
| 735 |
*---------------------------------------------------------------------- |
| 736 |
* |
| 737 |
* TclSetByteCodeFromAny -- |
| 738 |
* |
| 739 |
* Part of the bytecode Tcl object type implementation. Attempts to |
| 740 |
* generate an byte code internal form for the Tcl object "objPtr" by |
| 741 |
* compiling its string representation. This function also takes a hook |
| 742 |
* procedure that will be invoked to perform any needed post processing |
| 743 |
* on the compilation results before generating byte codes. interp is |
| 744 |
* compilation context and may not be NULL. |
| 745 |
* |
| 746 |
* Results: |
| 747 |
* The return value is a standard Tcl object result. If an error occurs |
| 748 |
* during compilation, an error message is left in the interpreter's |
| 749 |
* result. |
| 750 |
* |
| 751 |
* Side effects: |
| 752 |
* Frees the old internal representation. If no error occurs, then the |
| 753 |
* compiled code is stored as "objPtr"s bytecode representation. Also, if |
| 754 |
* debugging, initializes the "tcl_traceCompile" Tcl variable used to |
| 755 |
* trace compilations. |
| 756 |
* |
| 757 |
*---------------------------------------------------------------------- |
| 758 |
*/ |
| 759 |
|
| 760 |
int |
| 761 |
TclSetByteCodeFromAny( |
| 762 |
Tcl_Interp *interp, /* The interpreter for which the code is being |
| 763 |
* compiled. Must not be NULL. */ |
| 764 |
Tcl_Obj *objPtr, /* The object to make a ByteCode object. */ |
| 765 |
CompileHookProc *hookProc, /* Procedure to invoke after compilation. */ |
| 766 |
ClientData clientData) /* Hook procedure private data. */ |
| 767 |
{ |
| 768 |
Interp *iPtr = (Interp *) interp; |
| 769 |
CompileEnv compEnv; /* Compilation environment structure allocated |
| 770 |
* in frame. */ |
| 771 |
int length, result = TCL_OK; |
| 772 |
const char *stringPtr; |
| 773 |
Proc *procPtr = iPtr->compiledProcPtr; |
| 774 |
ContLineLoc *clLocPtr; |
| 775 |
|
| 776 |
#ifdef TCL_COMPILE_DEBUG |
| 777 |
if (!traceInitialized) { |
| 778 |
if (Tcl_LinkVar(interp, "tcl_traceCompile", |
| 779 |
(char *) &tclTraceCompile, TCL_LINK_INT) != TCL_OK) { |
| 780 |
Tcl_Panic("SetByteCodeFromAny: unable to create link for tcl_traceCompile variable"); |
| 781 |
} |
| 782 |
traceInitialized = 1; |
| 783 |
} |
| 784 |
#endif |
| 785 |
|
| 786 |
stringPtr = TclGetStringFromObj(objPtr, &length); |
| 787 |
|
| 788 |
/* |
| 789 |
* TIP #280: Pick up the CmdFrame in which the BC compiler was invoked and |
| 790 |
* use to initialize the tracking in the compiler. This information was |
| 791 |
* stored by TclCompEvalObj and ProcCompileProc. |
| 792 |
*/ |
| 793 |
|
| 794 |
TclInitCompileEnv(interp, &compEnv, stringPtr, length, |
| 795 |
iPtr->invokeCmdFramePtr, iPtr->invokeWord); |
| 796 |
|
| 797 |
/* |
| 798 |
* Now we check if we have data about invisible continuation lines for the |
| 799 |
* script, and make it available to the compile environment, if so. |
| 800 |
* |
| 801 |
* It is not clear if the script Tcl_Obj* can be free'd while the compiler |
| 802 |
* is using it, leading to the release of the associated ContLineLoc |
| 803 |
* structure as well. To ensure that the latter doesn't happen we set a |
| 804 |
* lock on it. We release this lock in the function TclFreeCompileEnv(), |
| 805 |
* found in this file. The "lineCLPtr" hashtable is managed in the file |
| 806 |
* "tclObj.c". |
| 807 |
*/ |
| 808 |
|
| 809 |
clLocPtr = TclContinuationsGet(objPtr); |
| 810 |
if (clLocPtr) { |
| 811 |
compEnv.clNext = &clLocPtr->loc[0]; |
| 812 |
} |
| 813 |
|
| 814 |
TclCompileScript(interp, stringPtr, length, &compEnv); |
| 815 |
|
| 816 |
/* |
| 817 |
* Successful compilation. Add a "done" instruction at the end. |
| 818 |
*/ |
| 819 |
|
| 820 |
TclEmitOpcode(INST_DONE, &compEnv); |
| 821 |
|
| 822 |
/* |
| 823 |
* Check for optimizations! |
| 824 |
* |
| 825 |
* Test if the generated code is free of most hazards; if so, recompile |
| 826 |
* but with generation of INST_START_CMD disabled. This produces somewhat |
| 827 |
* faster code in some cases, and more compact code in more. |
| 828 |
*/ |
| 829 |
|
| 830 |
if (Tcl_GetParent(interp) == NULL && |
| 831 |
!Tcl_LimitTypeEnabled(interp, TCL_LIMIT_COMMANDS|TCL_LIMIT_TIME) |
| 832 |
&& IsCompactibleCompileEnv(interp, &compEnv)) { |
| 833 |
TclFreeCompileEnv(&compEnv); |
| 834 |
iPtr->compiledProcPtr = procPtr; |
| 835 |
TclInitCompileEnv(interp, &compEnv, stringPtr, length, |
| 836 |
iPtr->invokeCmdFramePtr, iPtr->invokeWord); |
| 837 |
if (clLocPtr) { |
| 838 |
compEnv.clNext = &clLocPtr->loc[0]; |
| 839 |
} |
| 840 |
compEnv.atCmdStart = 2; /* The disabling magic. */ |
| 841 |
TclCompileScript(interp, stringPtr, length, &compEnv); |
| 842 |
assert (compEnv.atCmdStart > 1); |
| 843 |
TclEmitOpcode(INST_DONE, &compEnv); |
| 844 |
assert (compEnv.atCmdStart > 1); |
| 845 |
} |
| 846 |
|
| 847 |
/* |
| 848 |
* Apply some peephole optimizations that can cross specific/generic |
| 849 |
* instruction generator boundaries. |
| 850 |
*/ |
| 851 |
|
| 852 |
if (iPtr->extra.optimizer) { |
| 853 |
(iPtr->extra.optimizer)(&compEnv); |
| 854 |
} |
| 855 |
|
| 856 |
/* |
| 857 |
* Invoke the compilation hook procedure if one exists. |
| 858 |
*/ |
| 859 |
|
| 860 |
if (hookProc) { |
| 861 |
result = hookProc(interp, &compEnv, clientData); |
| 862 |
} |
| 863 |
|
| 864 |
/* |
| 865 |
* Change the object into a ByteCode object. Ownership of the literal |
| 866 |
* objects and aux data items is given to the ByteCode object. |
| 867 |
*/ |
| 868 |
|
| 869 |
#ifdef TCL_COMPILE_DEBUG |
| 870 |
TclVerifyLocalLiteralTable(&compEnv); |
| 871 |
#endif /*TCL_COMPILE_DEBUG*/ |
| 872 |
|
| 873 |
if (result == TCL_OK) { |
| 874 |
TclInitByteCodeObj(objPtr, &compEnv); |
| 875 |
TclDebugPrintByteCodeObj(objPtr); |
| 876 |
} |
| 877 |
|
| 878 |
TclFreeCompileEnv(&compEnv); |
| 879 |
return result; |
| 880 |
} |
| 881 |
|
| 882 |
/* |
| 883 |
*----------------------------------------------------------------------- |
| 884 |
* |
| 885 |
* SetByteCodeFromAny -- |
| 886 |
* |
| 887 |
* Part of the bytecode Tcl object type implementation. Attempts to |
| 888 |
* generate an byte code internal form for the Tcl object "objPtr" by |
| 889 |
* compiling its string representation. |
| 890 |
* |
| 891 |
* Results: |
| 892 |
* The return value is a standard Tcl object result. If an error occurs |
| 893 |
* during compilation, an error message is left in the interpreter's |
| 894 |
* result unless "interp" is NULL. |
| 895 |
* |
| 896 |
* Side effects: |
| 897 |
* Frees the old internal representation. If no error occurs, then the |
| 898 |
* compiled code is stored as "objPtr"s bytecode representation. Also, if |
| 899 |
* debugging, initializes the "tcl_traceCompile" Tcl variable used to |
| 900 |
* trace compilations. |
| 901 |
* |
| 902 |
*---------------------------------------------------------------------- |
| 903 |
*/ |
| 904 |
|
| 905 |
static int |
| 906 |
SetByteCodeFromAny( |
| 907 |
Tcl_Interp *interp, /* The interpreter for which the code is being |
| 908 |
* compiled. Must not be NULL. */ |
| 909 |
Tcl_Obj *objPtr) /* The object to make a ByteCode object. */ |
| 910 |
{ |
| 911 |
if (interp == NULL) { |
| 912 |
return TCL_ERROR; |
| 913 |
} |
| 914 |
return TclSetByteCodeFromAny(interp, objPtr, NULL, NULL); |
| 915 |
} |
| 916 |
|
| 917 |
/* |
| 918 |
*---------------------------------------------------------------------- |
| 919 |
* |
| 920 |
* DupByteCodeInternalRep -- |
| 921 |
* |
| 922 |
* Part of the bytecode Tcl object type implementation. However, it does |
| 923 |
* not copy the internal representation of a bytecode Tcl_Obj, but |
| 924 |
* instead leaves the new object untyped (with a NULL type pointer). |
| 925 |
* Code will be compiled for the new object only if necessary. |
| 926 |
* |
| 927 |
* Results: |
| 928 |
* None. |
| 929 |
* |
| 930 |
* Side effects: |
| 931 |
* None. |
| 932 |
* |
| 933 |
*---------------------------------------------------------------------- |
| 934 |
*/ |
| 935 |
|
| 936 |
static void |
| 937 |
DupByteCodeInternalRep( |
| 938 |
Tcl_Obj *srcPtr, /* Object with internal rep to copy. */ |
| 939 |
Tcl_Obj *copyPtr) /* Object with internal rep to set. */ |
| 940 |
{ |
| 941 |
return; |
| 942 |
} |
| 943 |
|
| 944 |
/* |
| 945 |
*---------------------------------------------------------------------- |
| 946 |
* |
| 947 |
* FreeByteCodeInternalRep -- |
| 948 |
* |
| 949 |
* Part of the bytecode Tcl object type implementation. Frees the storage |
| 950 |
* associated with a bytecode object's internal representation unless its |
| 951 |
* code is actively being executed. |
| 952 |
* |
| 953 |
* Results: |
| 954 |
* None. |
| 955 |
* |
| 956 |
* Side effects: |
| 957 |
* The bytecode object's internal rep is marked invalid and its code gets |
| 958 |
* freed unless the code is actively being executed. In that case the |
| 959 |
* cleanup is delayed until the last execution of the code completes. |
| 960 |
* |
| 961 |
*---------------------------------------------------------------------- |
| 962 |
*/ |
| 963 |
|
| 964 |
static void |
| 965 |
FreeByteCodeInternalRep( |
| 966 |
Tcl_Obj *objPtr) /* Object whose internal rep to free. */ |
| 967 |
{ |
| 968 |
ByteCode *codePtr = (ByteCode *)objPtr->internalRep.twoPtrValue.ptr1; |
| 969 |
|
| 970 |
objPtr->typePtr = NULL; |
| 971 |
if (codePtr->refCount-- <= 1) { |
| 972 |
TclCleanupByteCode(codePtr); |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
/* |
| 977 |
*---------------------------------------------------------------------- |
| 978 |
* |
| 979 |
* TclCleanupByteCode -- |
| 980 |
* |
| 981 |
* This procedure does all the real work of freeing up a bytecode |
| 982 |
* object's ByteCode structure. It's called only when the structure's |
| 983 |
* reference count becomes zero. |
| 984 |
* |
| 985 |
* Results: |
| 986 |
* None. |
| 987 |
* |
| 988 |
* Side effects: |
| 989 |
* Frees objPtr's bytecode internal representation and sets its type NULL |
| 990 |
* Also releases its literals and frees its auxiliary data items. |
| 991 |
* |
| 992 |
*---------------------------------------------------------------------- |
| 993 |
*/ |
| 994 |
|
| 995 |
void |
| 996 |
TclCleanupByteCode( |
| 997 |
ByteCode *codePtr) /* Points to the ByteCode to free. */ |
| 998 |
{ |
| 999 |
Tcl_Interp *interp = (Tcl_Interp *) *codePtr->interpHandle; |
| 1000 |
Interp *iPtr = (Interp *) interp; |
| 1001 |
int numLitObjects = codePtr->numLitObjects; |
| 1002 |
int numAuxDataItems = codePtr->numAuxDataItems; |
| 1003 |
Tcl_Obj **objArrayPtr, *objPtr; |
| 1004 |
const AuxData *auxDataPtr; |
| 1005 |
int i; |
| 1006 |
#ifdef TCL_COMPILE_STATS |
| 1007 |
|
| 1008 |
if (interp != NULL) { |
| 1009 |
ByteCodeStats *statsPtr; |
| 1010 |
Tcl_Time destroyTime; |
| 1011 |
int lifetimeSec, lifetimeMicroSec, log2; |
| 1012 |
|
| 1013 |
statsPtr = &iPtr->stats; |
| 1014 |
|
| 1015 |
statsPtr->numByteCodesFreed++; |
| 1016 |
statsPtr->currentSrcBytes -= (double) codePtr->numSrcBytes; |
| 1017 |
statsPtr->currentByteCodeBytes -= (double) codePtr->structureSize; |
| 1018 |
|
| 1019 |
statsPtr->currentInstBytes -= (double) codePtr->numCodeBytes; |
| 1020 |
statsPtr->currentLitBytes -= (double) |
| 1021 |
codePtr->numLitObjects * sizeof(Tcl_Obj *); |
| 1022 |
statsPtr->currentExceptBytes -= (double) |
| 1023 |
codePtr->numExceptRanges * sizeof(ExceptionRange); |
| 1024 |
statsPtr->currentAuxBytes -= (double) |
| 1025 |
codePtr->numAuxDataItems * sizeof(AuxData); |
| 1026 |
statsPtr->currentCmdMapBytes -= (double) codePtr->numCmdLocBytes; |
| 1027 |
|
| 1028 |
Tcl_GetTime(&destroyTime); |
| 1029 |
lifetimeSec = destroyTime.sec - codePtr->createTime.sec; |
| 1030 |
if (lifetimeSec > 2000) { /* avoid overflow */ |
| 1031 |
lifetimeSec = 2000; |
| 1032 |
} |
| 1033 |
lifetimeMicroSec = 1000000 * lifetimeSec + |
| 1034 |
(destroyTime.usec - codePtr->createTime.usec); |
| 1035 |
|
| 1036 |
log2 = TclLog2(lifetimeMicroSec); |
| 1037 |
if (log2 > 31) { |
| 1038 |
log2 = 31; |
| 1039 |
} |
| 1040 |
statsPtr->lifetimeCount[log2]++; |
| 1041 |
} |
| 1042 |
#endif /* TCL_COMPILE_STATS */ |
| 1043 |
|
| 1044 |
/* |
| 1045 |
* A single heap object holds the ByteCode structure and its code, object, |
| 1046 |
* command location, and auxiliary data arrays. This means we only need to |
| 1047 |
* 1) decrement the ref counts of the LiteralEntry's in its literal array, |
| 1048 |
* 2) call the free procs for the auxiliary data items, 3) free the |
| 1049 |
* localCache if it is unused, and finally 4) free the ByteCode |
| 1050 |
* structure's heap object. |
| 1051 |
* |
| 1052 |
* The case for TCL_BYTECODE_PRECOMPILED (precompiled ByteCodes, like |
| 1053 |
* those generated from tbcload) is special, as they doesn't make use of |
| 1054 |
* the global literal table. They instead maintain private references to |
| 1055 |
* their literals which must be decremented. |
| 1056 |
* |
| 1057 |
* In order to insure a proper and efficient cleanup of the literal array |
| 1058 |
* when it contains non-shared literals [Bug 983660], we also distinguish |
| 1059 |
* the case of an interpreter being deleted (signaled by interp == NULL). |
| 1060 |
* Also, as the interp deletion will remove the global literal table |
| 1061 |
* anyway, we avoid the extra cost of updating it for each literal being |
| 1062 |
* released. |
| 1063 |
*/ |
| 1064 |
|
| 1065 |
if (codePtr->flags & TCL_BYTECODE_PRECOMPILED) { |
| 1066 |
|
| 1067 |
objArrayPtr = codePtr->objArrayPtr; |
| 1068 |
for (i = 0; i < numLitObjects; i++) { |
| 1069 |
objPtr = *objArrayPtr; |
| 1070 |
if (objPtr) { |
| 1071 |
Tcl_DecrRefCount(objPtr); |
| 1072 |
} |
| 1073 |
objArrayPtr++; |
| 1074 |
} |
| 1075 |
codePtr->numLitObjects = 0; |
| 1076 |
} else { |
| 1077 |
objArrayPtr = codePtr->objArrayPtr; |
| 1078 |
while (numLitObjects--) { |
| 1079 |
/* TclReleaseLiteral calls Tcl_DecrRefCount() for us */ |
| 1080 |
TclReleaseLiteral(interp, *objArrayPtr++); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
auxDataPtr = codePtr->auxDataArrayPtr; |
| 1085 |
for (i = 0; i < numAuxDataItems; i++) { |
| 1086 |
if (auxDataPtr->type->freeProc != NULL) { |
| 1087 |
auxDataPtr->type->freeProc(auxDataPtr->clientData); |
| 1088 |
} |
| 1089 |
auxDataPtr++; |
| 1090 |
} |
| 1091 |
|
| 1092 |
/* |
| 1093 |
* TIP #280. Release the location data associated with this byte code |
| 1094 |
* structure, if any. NOTE: The interp we belong to may be gone already, |
| 1095 |
* and the data with it. |
| 1096 |
* |
| 1097 |
* See also tclBasic.c, DeleteInterpProc |
| 1098 |
*/ |
| 1099 |
|
| 1100 |
if (iPtr) { |
| 1101 |
Tcl_HashEntry *hePtr = Tcl_FindHashEntry(iPtr->lineBCPtr, |
| 1102 |
(char *) codePtr); |
| 1103 |
|
| 1104 |
if (hePtr) { |
| 1105 |
ReleaseCmdWordData((ExtCmdLoc *)Tcl_GetHashValue(hePtr)); |
| 1106 |
Tcl_DeleteHashEntry(hePtr); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (codePtr->localCachePtr && (--codePtr->localCachePtr->refCount == 0)) { |
| 1111 |
TclFreeLocalCache(interp, codePtr->localCachePtr); |
| 1112 |
} |
| 1113 |
|
| 1114 |
TclHandleRelease(codePtr->interpHandle); |
| 1115 |
ckfree(codePtr); |
| 1116 |
} |
| 1117 |
|
| 1118 |
/* |
| 1119 |
* --------------------------------------------------------------------- |
| 1120 |
* |
| 1121 |
* IsCompactibleCompileEnv -- |
| 1122 |
* |
| 1123 |
* Checks to see if we may apply some basic compaction optimizations to a |
| 1124 |
* piece of bytecode. Idempotent. |
| 1125 |
* |
| 1126 |
* --------------------------------------------------------------------- |
| 1127 |
*/ |
| 1128 |
|
| 1129 |
static int |
| 1130 |
IsCompactibleCompileEnv( |
| 1131 |
Tcl_Interp *interp, |
| 1132 |
CompileEnv *envPtr) |
| 1133 |
{ |
| 1134 |
unsigned char *pc; |
| 1135 |
int size; |
| 1136 |
|
| 1137 |
/* |
| 1138 |
* Special: procedures in the '::tcl' namespace (or its children) are |
| 1139 |
* considered to be well-behaved and so can have compaction applied even |
| 1140 |
* if it would otherwise be invalid. |
| 1141 |
*/ |
| 1142 |
|
| 1143 |
if (envPtr->procPtr != NULL && envPtr->procPtr->cmdPtr != NULL |
| 1144 |
&& envPtr->procPtr->cmdPtr->nsPtr != NULL) { |
| 1145 |
Namespace *nsPtr = envPtr->procPtr->cmdPtr->nsPtr; |
| 1146 |
|
| 1147 |
if (strcmp(nsPtr->fullName, "::tcl") == 0 |
| 1148 |
|| strncmp(nsPtr->fullName, "::tcl::", 7) == 0) { |
| 1149 |
return 1; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* |
| 1154 |
* Go through and ensure that no operation involved can cause a desired |
| 1155 |
* change of bytecode sequence during running. This comes down to ensuring |
| 1156 |
* that there are no mapped variables (due to traces) or calls to external |
| 1157 |
* commands (traces, [uplevel] trickery). This is actually a very |
| 1158 |
* conservative check; it turns down a lot of code that is OK in practice. |
| 1159 |
*/ |
| 1160 |
|
| 1161 |
for (pc = envPtr->codeStart ; pc < envPtr->codeNext ; pc += size) { |
| 1162 |
switch (*pc) { |
| 1163 |
/* Invokes */ |
| 1164 |
case INST_INVOKE_STK1: |
| 1165 |
case INST_INVOKE_STK4: |
| 1166 |
case INST_INVOKE_EXPANDED: |
| 1167 |
case INST_INVOKE_REPLACE: |
| 1168 |
return 0; |
| 1169 |
/* Runtime evals */ |
| 1170 |
case INST_EVAL_STK: |
| 1171 |
case INST_EXPR_STK: |
| 1172 |
case INST_YIELD: |
| 1173 |
return 0; |
| 1174 |
/* Upvars */ |
| 1175 |
case INST_UPVAR: |
| 1176 |
case INST_NSUPVAR: |
| 1177 |
case INST_VARIABLE: |
| 1178 |
return 0; |
| 1179 |
default: |
| 1180 |
size = tclInstructionTable[*pc].numBytes; |
| 1181 |
assert (size > 0); |
| 1182 |
break; |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
return 1; |
| 1187 |
} |
| 1188 |
|
| 1189 |
/* |
| 1190 |
*---------------------------------------------------------------------- |
| 1191 |
* |
| 1192 |
* Tcl_SubstObj -- |
| 1193 |
* |
| 1194 |
* This function performs the substitutions specified on the given string |
| 1195 |
* as described in the user documentation for the "subst" Tcl command. |
| 1196 |
* |
| 1197 |
* Results: |
| 1198 |
* A Tcl_Obj* containing the substituted string, or NULL to indicate that |
| 1199 |
* an error occurred. |
| 1200 |
* |
| 1201 |
* Side effects: |
| 1202 |
* See the user documentation. |
| 1203 |
* |
| 1204 |
*---------------------------------------------------------------------- |
| 1205 |
*/ |
| 1206 |
|
| 1207 |
Tcl_Obj * |
| 1208 |
Tcl_SubstObj( |
| 1209 |
Tcl_Interp *interp, /* Interpreter in which substitution occurs */ |
| 1210 |
Tcl_Obj *objPtr, /* The value to be substituted. */ |
| 1211 |
int flags) /* What substitutions to do. */ |
| 1212 |
{ |
| 1213 |
NRE_callback *rootPtr = TOP_CB(interp); |
| 1214 |
|
| 1215 |
if (TclNRRunCallbacks(interp, Tcl_NRSubstObj(interp, objPtr, flags), |
| 1216 |
rootPtr) != TCL_OK) { |
| 1217 |
return NULL; |
| 1218 |
} |
| 1219 |
return Tcl_GetObjResult(interp); |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* |
| 1223 |
*---------------------------------------------------------------------- |
| 1224 |
* |
| 1225 |
* Tcl_NRSubstObj -- |
| 1226 |
* |
| 1227 |
* Request substitution of a Tcl value by the NR stack. |
| 1228 |
* |
| 1229 |
* Results: |
| 1230 |
* Returns TCL_OK. |
| 1231 |
* |
| 1232 |
* Side effects: |
| 1233 |
* Compiles objPtr into bytecode that performs the substitutions as |
| 1234 |
* governed by flags and places callbacks on the NR stack to execute |
| 1235 |
* the bytecode and store the result in the interp. |
| 1236 |
* |
| 1237 |
*---------------------------------------------------------------------- |
| 1238 |
*/ |
| 1239 |
|
| 1240 |
int |
| 1241 |
Tcl_NRSubstObj( |
| 1242 |
Tcl_Interp *interp, |
| 1243 |
Tcl_Obj *objPtr, |
| 1244 |
int flags) |
| 1245 |
{ |
| 1246 |
ByteCode *codePtr = CompileSubstObj(interp, objPtr, flags); |
| 1247 |
|
| 1248 |
/* TODO: Confirm we do not need this. */ |
| 1249 |
/* Tcl_ResetResult(interp); */ |
| 1250 |
return TclNRExecuteByteCode(interp, codePtr); |
| 1251 |
} |
| 1252 |
|
| 1253 |
/* |
| 1254 |
*---------------------------------------------------------------------- |
| 1255 |
* |
| 1256 |
* CompileSubstObj -- |
| 1257 |
* |
| 1258 |
* Compile a Tcl value into ByteCode implementing its substitution, as |
| 1259 |
* governed by flags. |
| 1260 |
* |
| 1261 |
* Results: |
| 1262 |
* A (ByteCode *) is returned pointing to the resulting ByteCode. |
| 1263 |
* The caller must manage its refCount and arrange for a call to |
| 1264 |
* TclCleanupByteCode() when the last reference disappears. |
| 1265 |
* |
| 1266 |
* Side effects: |
| 1267 |
* The Tcl_ObjType of objPtr is changed to the "substcode" type, and the |
| 1268 |
* ByteCode and governing flags value are kept in the internal rep for |
| 1269 |
* faster operations the next time CompileSubstObj is called on the same |
| 1270 |
* value. |
| 1271 |
* |
| 1272 |
*---------------------------------------------------------------------- |
| 1273 |
*/ |
| 1274 |
|
| 1275 |
static ByteCode * |
| 1276 |
CompileSubstObj( |
| 1277 |
Tcl_Interp *interp, |
| 1278 |
Tcl_Obj *objPtr, |
| 1279 |
int flags) |
| 1280 |
{ |
| 1281 |
Interp *iPtr = (Interp *) interp; |
| 1282 |
ByteCode *codePtr = NULL; |
| 1283 |
|
| 1284 |
if (objPtr->typePtr == &substCodeType) { |
| 1285 |
Namespace *nsPtr = iPtr->varFramePtr->nsPtr; |
| 1286 |
|
| 1287 |
codePtr = (ByteCode *)objPtr->internalRep.twoPtrValue.ptr1; |
| 1288 |
if (flags != PTR2INT(objPtr->internalRep.twoPtrValue.ptr2) |
| 1289 |
|| ((Interp *) *codePtr->interpHandle != iPtr) |
| 1290 |
|| (codePtr->compileEpoch != iPtr->compileEpoch) |
| 1291 |
|| (codePtr->nsPtr != nsPtr) |
| 1292 |
|| (codePtr->nsEpoch != nsPtr->resolverEpoch) |
| 1293 |
|| (codePtr->localCachePtr != |
| 1294 |
iPtr->varFramePtr->localCachePtr)) { |
| 1295 |
FreeSubstCodeInternalRep(objPtr); |
| 1296 |
} |
| 1297 |
} |
| 1298 |
if (objPtr->typePtr != &substCodeType) { |
| 1299 |
CompileEnv compEnv; |
| 1300 |
int numBytes; |
| 1301 |
const char *bytes = Tcl_GetStringFromObj(objPtr, &numBytes); |
| 1302 |
|
| 1303 |
/* TODO: Check for more TIP 280 */ |
| 1304 |
TclInitCompileEnv(interp, &compEnv, bytes, numBytes, NULL, 0); |
| 1305 |
|
| 1306 |
TclSubstCompile(interp, bytes, numBytes, flags, 1, &compEnv); |
| 1307 |
|
| 1308 |
TclEmitOpcode(INST_DONE, &compEnv); |
| 1309 |
TclInitByteCodeObj(objPtr, &compEnv); |
| 1310 |
objPtr->typePtr = &substCodeType; |
| 1311 |
TclFreeCompileEnv(&compEnv); |
| 1312 |
|
| 1313 |
codePtr = (ByteCode *)objPtr->internalRep.twoPtrValue.ptr1; |
| 1314 |
objPtr->internalRep.twoPtrValue.ptr1 = codePtr; |
| 1315 |
objPtr->internalRep.twoPtrValue.ptr2 = INT2PTR(flags); |
| 1316 |
if (iPtr->varFramePtr->localCachePtr) { |
| 1317 |
codePtr->localCachePtr = iPtr->varFramePtr->localCachePtr; |
| 1318 |
codePtr->localCachePtr->refCount++; |
| 1319 |
} |
| 1320 |
TclDebugPrintByteCodeObj(objPtr); |
| 1321 |
} |
| 1322 |
return codePtr; |
| 1323 |
} |
| 1324 |
|
| 1325 |
/* |
| 1326 |
*---------------------------------------------------------------------- |
| 1327 |
* |
| 1328 |
* FreeSubstCodeInternalRep -- |
| 1329 |
* |
| 1330 |
* Part of the substcode Tcl object type implementation. Frees the |
| 1331 |
* storage associated with a substcode object's internal representation |
| 1332 |
* unless its code is actively being executed. |
| 1333 |
* |
| 1334 |
* Results: |
| 1335 |
* None. |
| 1336 |
* |
| 1337 |
* Side effects: |
| 1338 |
* The substcode object's internal rep is marked invalid and its code |
| 1339 |
* gets freed unless the code is actively being executed. In that case |
| 1340 |
* the cleanup is delayed until the last execution of the code completes. |
| 1341 |
* |
| 1342 |
*---------------------------------------------------------------------- |
| 1343 |
*/ |
| 1344 |
|
| 1345 |
static void |
| 1346 |
FreeSubstCodeInternalRep( |
| 1347 |
Tcl_Obj *objPtr) /* Object whose internal rep to free. */ |
| 1348 |
{ |
| 1349 |
ByteCode *codePtr = (ByteCode *)objPtr->internalRep.twoPtrValue.ptr1; |
| 1350 |
|
| 1351 |
objPtr->typePtr = NULL; |
| 1352 |
if (codePtr->refCount-- <= 1) { |
| 1353 |
TclCleanupByteCode(codePtr); |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
static void |
| 1358 |
ReleaseCmdWordData( |
| 1359 |
ExtCmdLoc *eclPtr) |
| 1360 |
{ |
| 1361 |
int i; |
| 1362 |
|
| 1363 |
if (eclPtr->type == TCL_LOCATION_SOURCE) { |
| 1364 |
Tcl_DecrRefCount(eclPtr->path); |
| 1365 |
} |
| 1366 |
for (i=0 ; i<eclPtr->nuloc ; i++) { |
| 1367 |
ckfree((char *) eclPtr->loc[i].line); |
| 1368 |
} |
| 1369 |
|
| 1370 |
if (eclPtr->loc != NULL) { |
| 1371 |
ckfree((char *) eclPtr->loc); |
| 1372 |
} |
| 1373 |
|
| 1374 |
ckfree((char *) eclPtr); |
| 1375 |
} |
| 1376 |
|
| 1377 |
/* |
| 1378 |
*---------------------------------------------------------------------- |
| 1379 |
* |
| 1380 |
* TclInitCompileEnv -- |
| 1381 |
* |
| 1382 |
* Initializes a CompileEnv compilation environment structure for the |
| 1383 |
* compilation of a string in an interpreter. |
| 1384 |
* |
| 1385 |
* Results: |
| 1386 |
* None. |
| 1387 |
* |
| 1388 |
* Side effects: |
| 1389 |
* The CompileEnv structure is initialized. |
| 1390 |
* |
| 1391 |
*---------------------------------------------------------------------- |
| 1392 |
*/ |
| 1393 |
|
| 1394 |
void |
| 1395 |
TclInitCompileEnv( |
| 1396 |
Tcl_Interp *interp, /* The interpreter for which a CompileEnv |
| 1397 |
* structure is initialized. */ |
| 1398 |
CompileEnv *envPtr,/* Points to the CompileEnv structure to |
| 1399 |
* initialize. */ |
| 1400 |
const char *stringPtr, /* The source string to be compiled. */ |
| 1401 |
int numBytes, /* Number of bytes in source string. */ |
| 1402 |
const CmdFrame *invoker, /* Location context invoking the bcc */ |
| 1403 |
int word) /* Index of the word in that context getting |
| 1404 |
* compiled */ |
| 1405 |
{ |
| 1406 |
Interp *iPtr = (Interp *) interp; |
| 1407 |
|
| 1408 |
assert(tclInstructionTable[LAST_INST_OPCODE+1].name == NULL); |
| 1409 |
|
| 1410 |
envPtr->iPtr = iPtr; |
| 1411 |
envPtr->source = stringPtr; |
| 1412 |
envPtr->numSrcBytes = numBytes; |
| 1413 |
envPtr->procPtr = iPtr->compiledProcPtr; |
| 1414 |
iPtr->compiledProcPtr = NULL; |
| 1415 |
envPtr->numCommands = 0; |
| 1416 |
envPtr->exceptDepth = 0; |
| 1417 |
envPtr->maxExceptDepth = 0; |
| 1418 |
envPtr->maxStackDepth = 0; |
| 1419 |
envPtr->currStackDepth = 0; |
| 1420 |
TclInitLiteralTable(&envPtr->localLitTable); |
| 1421 |
|
| 1422 |
envPtr->codeStart = envPtr->staticCodeSpace; |
| 1423 |
envPtr->codeNext = envPtr->codeStart; |
| 1424 |
envPtr->codeEnd = envPtr->codeStart + COMPILEENV_INIT_CODE_BYTES; |
| 1425 |
envPtr->mallocedCodeArray = 0; |
| 1426 |
|
| 1427 |
envPtr->literalArrayPtr = envPtr->staticLiteralSpace; |
| 1428 |
envPtr->literalArrayNext = 0; |
| 1429 |
envPtr->literalArrayEnd = COMPILEENV_INIT_NUM_OBJECTS; |
| 1430 |
envPtr->mallocedLiteralArray = 0; |
| 1431 |
|
| 1432 |
envPtr->exceptArrayPtr = envPtr->staticExceptArraySpace; |
| 1433 |
envPtr->exceptAuxArrayPtr = envPtr->staticExAuxArraySpace; |
| 1434 |
envPtr->exceptArrayNext = 0; |
| 1435 |
envPtr->exceptArrayEnd = COMPILEENV_INIT_EXCEPT_RANGES; |
| 1436 |
envPtr->mallocedExceptArray = 0; |
| 1437 |
|
| 1438 |
envPtr->cmdMapPtr = envPtr->staticCmdMapSpace; |
| 1439 |
envPtr->cmdMapEnd = COMPILEENV_INIT_CMD_MAP_SIZE; |
| 1440 |
envPtr->mallocedCmdMap = 0; |
| 1441 |
envPtr->atCmdStart = 1; |
| 1442 |
envPtr->expandCount = 0; |
| 1443 |
|
| 1444 |
/* |
| 1445 |
* TIP #280: Set up the extended command location information, based on |
| 1446 |
* the context invoking the byte code compiler. This structure is used to |
| 1447 |
* keep the per-word line information for all compiled commands. |
| 1448 |
* |
| 1449 |
* See also tclBasic.c, TclEvalObjEx, for the equivalent code in the |
| 1450 |
* non-compiling evaluator |
| 1451 |
*/ |
| 1452 |
|
| 1453 |
envPtr->extCmdMapPtr = (ExtCmdLoc *)ckalloc(sizeof(ExtCmdLoc)); |
| 1454 |
envPtr->extCmdMapPtr->loc = NULL; |
| 1455 |
envPtr->extCmdMapPtr->nloc = 0; |
| 1456 |
envPtr->extCmdMapPtr->nuloc = 0; |
| 1457 |
envPtr->extCmdMapPtr->path = NULL; |
| 1458 |
|
| 1459 |
if (invoker == NULL) { |
| 1460 |
/* |
| 1461 |
* Initialize the compiler for relative counting in case of a |
| 1462 |
* dynamic context. |
| 1463 |
*/ |
| 1464 |
|
| 1465 |
envPtr->line = 1; |
| 1466 |
if (iPtr->evalFlags & TCL_EVAL_FILE) { |
| 1467 |
iPtr->evalFlags &= ~TCL_EVAL_FILE; |
| 1468 |
envPtr->extCmdMapPtr->type = TCL_LOCATION_SOURCE; |
| 1469 |
|
| 1470 |
if (iPtr->scriptFile) { |
| 1471 |
/* |
| 1472 |
* Normalization here, to have the correct pwd. Should have |
| 1473 |
* negligible impact on performance, as the norm should have |
| 1474 |
* been done already by the 'source' invoking us, and it |
| 1475 |
* caches the result. |
| 1476 |
*/ |
| 1477 |
|
| 1478 |
Tcl_Obj *norm = |
| 1479 |
Tcl_FSGetNormalizedPath(interp, iPtr->scriptFile); |
| 1480 |
|
| 1481 |
if (norm == NULL) { |
| 1482 |
/* |
| 1483 |
* Error message in the interp result. No place to put it. |
| 1484 |
* And no place to serve the error itself to either. Fake |
| 1485 |
* a path, empty string. |
| 1486 |
*/ |
| 1487 |
|
| 1488 |
TclNewLiteralStringObj(envPtr->extCmdMapPtr->path, ""); |
| 1489 |
} else { |
| 1490 |
envPtr->extCmdMapPtr->path = norm; |
| 1491 |
} |
| 1492 |
} else { |
| 1493 |
TclNewLiteralStringObj(envPtr->extCmdMapPtr->path, ""); |
| 1494 |
} |
| 1495 |
|
| 1496 |
Tcl_IncrRefCount(envPtr->extCmdMapPtr->path); |
| 1497 |
} else { |
| 1498 |
envPtr->extCmdMapPtr->type = |
| 1499 |
(envPtr->procPtr ? TCL_LOCATION_PROC : TCL_LOCATION_BC); |
| 1500 |
} |
| 1501 |
} else { |
| 1502 |
/* |
| 1503 |
* Initialize the compiler using the context, making counting absolute |
| 1504 |
* to that context. Note that the context can be byte code execution. |
| 1505 |
* In that case we have to fill out the missing pieces (line, path, |
| 1506 |
* ...) which may make change the type as well. |
| 1507 |
*/ |
| 1508 |
|
| 1509 |
CmdFrame *ctxPtr = (CmdFrame *)TclStackAlloc(interp, sizeof(CmdFrame)); |
| 1510 |
int pc = 0; |
| 1511 |
|
| 1512 |
*ctxPtr = *invoker; |
| 1513 |
if (invoker->type == TCL_LOCATION_BC) { |
| 1514 |
/* |
| 1515 |
* Note: Type BC => ctx.data.eval.path is not used. |
| 1516 |
* ctx.data.tebc.codePtr is used instead. |
| 1517 |
*/ |
| 1518 |
|
| 1519 |
TclGetSrcInfoForPc(ctxPtr); |
| 1520 |
pc = 1; |
| 1521 |
} |
| 1522 |
|
| 1523 |
if ((ctxPtr->nline <= word) || (ctxPtr->line[word] < 0)) { |
| 1524 |
/* |
| 1525 |
* Word is not a literal, relative counting. |
| 1526 |
*/ |
| 1527 |
|
| 1528 |
envPtr->line = 1; |
| 1529 |
envPtr->extCmdMapPtr->type = |
| 1530 |
(envPtr->procPtr ? TCL_LOCATION_PROC : TCL_LOCATION_BC); |
| 1531 |
|
| 1532 |
if (pc && (ctxPtr->type == TCL_LOCATION_SOURCE)) { |
| 1533 |
/* |
| 1534 |
* The reference made by 'TclGetSrcInfoForPc' is dead. |
| 1535 |
*/ |
| 1536 |
|
| 1537 |
Tcl_DecrRefCount(ctxPtr->data.eval.path); |
| 1538 |
} |
| 1539 |
} else { |
| 1540 |
envPtr->line = ctxPtr->line[word]; |
| 1541 |
envPtr->extCmdMapPtr->type = ctxPtr->type; |
| 1542 |
|
| 1543 |
if (ctxPtr->type == TCL_LOCATION_SOURCE) { |
| 1544 |
envPtr->extCmdMapPtr->path = ctxPtr->data.eval.path; |
| 1545 |
|
| 1546 |
if (pc) { |
| 1547 |
/* |
| 1548 |
* The reference 'TclGetSrcInfoForPc' made is transfered. |
| 1549 |
*/ |
| 1550 |
|
| 1551 |
ctxPtr->data.eval.path = NULL; |
| 1552 |
} else { |
| 1553 |
/* |
| 1554 |
* We have a new reference here. |
| 1555 |
*/ |
| 1556 |
|
| 1557 |
Tcl_IncrRefCount(envPtr->extCmdMapPtr->path); |
| 1558 |
} |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
TclStackFree(interp, ctxPtr); |
| 1563 |
} |
| 1564 |
|
| 1565 |
envPtr->extCmdMapPtr->start = envPtr->line; |
| 1566 |
|
| 1567 |
/* |
| 1568 |
* Initialize the data about invisible continuation lines as empty, i.e. |
| 1569 |
* not used. The caller (TclSetByteCodeFromAny) will set this up, if such |
| 1570 |
* data is available. |
| 1571 |
*/ |
| 1572 |
|
| 1573 |
envPtr->clNext = NULL; |
| 1574 |
|
| 1575 |
envPtr->auxDataArrayPtr = envPtr->staticAuxDataArraySpace; |
| 1576 |
envPtr->auxDataArrayNext = 0; |
| 1577 |
envPtr->auxDataArrayEnd = COMPILEENV_INIT_AUX_DATA_SIZE; |
| 1578 |
envPtr->mallocedAuxDataArray = 0; |
| 1579 |
} |
| 1580 |
|
| 1581 |
/* |
| 1582 |
*---------------------------------------------------------------------- |
| 1583 |
* |
| 1584 |
* TclFreeCompileEnv -- |
| 1585 |
* |
| 1586 |
* Free the storage allocated in a CompileEnv compilation environment |
| 1587 |
* structure. |
| 1588 |
* |
| 1589 |
* Results: |
| 1590 |
* None. |
| 1591 |
* |
| 1592 |
* Side effects: |
| 1593 |
* Allocated storage in the CompileEnv structure is freed. Note that its |
| 1594 |
* local literal table is not deleted and its literal objects are not |
| 1595 |
* released. In addition, storage referenced by its auxiliary data items |
| 1596 |
* is not freed. This is done so that, when compilation is successful, |
| 1597 |
* "ownership" of these objects and aux data items is handed over to the |
| 1598 |
* corresponding ByteCode structure. |
| 1599 |
* |
| 1600 |
*---------------------------------------------------------------------- |
| 1601 |
*/ |
| 1602 |
|
| 1603 |
void |
| 1604 |
TclFreeCompileEnv( |
| 1605 |
CompileEnv *envPtr)/* Points to the CompileEnv structure. */ |
| 1606 |
{ |
| 1607 |
if (envPtr->localLitTable.buckets != envPtr->localLitTable.staticBuckets){ |
| 1608 |
ckfree(envPtr->localLitTable.buckets); |
| 1609 |
envPtr->localLitTable.buckets = envPtr->localLitTable.staticBuckets; |
| 1610 |
} |
| 1611 |
if (envPtr->iPtr) { |
| 1612 |
/* |
| 1613 |
* We never converted to Bytecode, so free the things we would |
| 1614 |
* have transferred to it. |
| 1615 |
*/ |
| 1616 |
|
| 1617 |
int i; |
| 1618 |
LiteralEntry *entryPtr = envPtr->literalArrayPtr; |
| 1619 |
AuxData *auxDataPtr = envPtr->auxDataArrayPtr; |
| 1620 |
|
| 1621 |
for (i = 0; i < envPtr->literalArrayNext; i++) { |
| 1622 |
TclReleaseLiteral((Tcl_Interp *)envPtr->iPtr, entryPtr->objPtr); |
| 1623 |
entryPtr++; |
| 1624 |
} |
| 1625 |
|
| 1626 |
#ifdef TCL_COMPILE_DEBUG |
| 1627 |
TclVerifyGlobalLiteralTable(envPtr->iPtr); |
| 1628 |
#endif /*TCL_COMPILE_DEBUG*/ |
| 1629 |
|
| 1630 |
for (i = 0; i < envPtr->auxDataArrayNext; i++) { |
| 1631 |
if (auxDataPtr->type->freeProc != NULL) { |
| 1632 |
auxDataPtr->type->freeProc(auxDataPtr->clientData); |
| 1633 |
} |
| 1634 |
auxDataPtr++; |
| 1635 |
} |
| 1636 |
} |
| 1637 |
if (envPtr->mallocedCodeArray) { |
| 1638 |
ckfree(envPtr->codeStart); |
| 1639 |
} |
| 1640 |
if (envPtr->mallocedLiteralArray) { |
| 1641 |
ckfree(envPtr->literalArrayPtr); |
| 1642 |
} |
| 1643 |
if (envPtr->mallocedExceptArray) { |
| 1644 |
ckfree(envPtr->exceptArrayPtr); |
| 1645 |
ckfree(envPtr->exceptAuxArrayPtr); |
| 1646 |
} |
| 1647 |
if (envPtr->mallocedCmdMap) { |
| 1648 |
ckfree(envPtr->cmdMapPtr); |
| 1649 |
} |
| 1650 |
if (envPtr->mallocedAuxDataArray) { |
| 1651 |
ckfree(envPtr->auxDataArrayPtr); |
| 1652 |
} |
| 1653 |
if (envPtr->extCmdMapPtr) { |
| 1654 |
ReleaseCmdWordData(envPtr->extCmdMapPtr); |
| 1655 |
envPtr->extCmdMapPtr = NULL; |
| 1656 |
} |
| 1657 |
} |
| 1658 |
|
| 1659 |
/* |
| 1660 |
*---------------------------------------------------------------------- |
| 1661 |
* |
| 1662 |
* TclWordKnownAtCompileTime -- |
| 1663 |
* |
| 1664 |
* Test whether the value of a token is completely known at compile time. |
| 1665 |
* |
| 1666 |
* Results: |
| 1667 |
* Returns true if the tokenPtr argument points to a word value that is |
| 1668 |
* completely known at compile time. Generally, values that are known at |
| 1669 |
* compile time can be compiled to their values, while values that cannot |
| 1670 |
* be known until substitution at runtime must be compiled to bytecode |
| 1671 |
* instructions that perform that substitution. For several commands, |
| 1672 |
* whether or not arguments are known at compile time determine whether |
| 1673 |
* it is worthwhile to compile at all. |
| 1674 |
* |
| 1675 |
* Side effects: |
| 1676 |
* When returning true, appends the known value of the word to the |
| 1677 |
* unshared Tcl_Obj (*valuePtr), unless valuePtr is NULL. |
| 1678 |
* |
| 1679 |
*---------------------------------------------------------------------- |
| 1680 |
*/ |
| 1681 |
|
| 1682 |
int |
| 1683 |
TclWordKnownAtCompileTime( |
| 1684 |
Tcl_Token *tokenPtr, /* Points to Tcl_Token we should check */ |
| 1685 |
Tcl_Obj *valuePtr) /* If not NULL, points to an unshared Tcl_Obj |
| 1686 |
* to which we should append the known value |
| 1687 |
* of the word. */ |
| 1688 |
{ |
| 1689 |
int numComponents = tokenPtr->numComponents; |
| 1690 |
Tcl_Obj *tempPtr = NULL; |
| 1691 |
|
| 1692 |
if (tokenPtr->type == TCL_TOKEN_SIMPLE_WORD) { |
| 1693 |
if (valuePtr != NULL) { |
| 1694 |
Tcl_AppendToObj(valuePtr, tokenPtr[1].start, tokenPtr[1].size); |
| 1695 |
} |
| 1696 |
return 1; |
| 1697 |
} |
| 1698 |
if (tokenPtr->type != TCL_TOKEN_WORD) { |
| 1699 |
return 0; |
| 1700 |
} |
| 1701 |
tokenPtr++; |
| 1702 |
if (valuePtr != NULL) { |
| 1703 |
TclNewObj(tempPtr); |
| 1704 |
Tcl_IncrRefCount(tempPtr); |
| 1705 |
} |
| 1706 |
while (numComponents--) { |
| 1707 |
switch (tokenPtr->type) { |
| 1708 |
case TCL_TOKEN_TEXT: |
| 1709 |
if (tempPtr != NULL) { |
| 1710 |
Tcl_AppendToObj(tempPtr, tokenPtr->start, tokenPtr->size); |
| 1711 |
} |
| 1712 |
break; |
| 1713 |
|
| 1714 |
case TCL_TOKEN_BS: |
| 1715 |
if (tempPtr != NULL) { |
| 1716 |
char utfBuf[TCL_UTF_MAX] = ""; |
| 1717 |
int length = TclParseBackslash(tokenPtr->start, |
| 1718 |
tokenPtr->size, NULL, utfBuf); |
| 1719 |
|
| 1720 |
Tcl_AppendToObj(tempPtr, utfBuf, length); |
| 1721 |
} |
| 1722 |
break; |
| 1723 |
|
| 1724 |
default: |
| 1725 |
if (tempPtr != NULL) { |
| 1726 |
Tcl_DecrRefCount(tempPtr); |
| 1727 |
} |
| 1728 |
return 0; |
| 1729 |
} |
| 1730 |
tokenPtr++; |
| 1731 |
} |
| 1732 |
if (valuePtr != NULL) { |
| 1733 |
Tcl_AppendObjToObj(valuePtr, tempPtr); |
| 1734 |
Tcl_DecrRefCount(tempPtr); |
| 1735 |
} |
| 1736 |
return 1; |
| 1737 |
} |
| 1738 |
|
| 1739 |
/* |
| 1740 |
*---------------------------------------------------------------------- |
| 1741 |
* |
| 1742 |
* TclCompileScript -- |
| 1743 |
* |
| 1744 |
* Compile a Tcl script in a string. |
| 1745 |
* |
| 1746 |
* Results: |
| 1747 |
* The return value is TCL_OK on a successful compilation and TCL_ERROR |
| 1748 |
* on failure. If TCL_ERROR is returned, then the interpreter's result |
| 1749 |
* contains an error message. |
| 1750 |
* |
| 1751 |
* Side effects: |
| 1752 |
* Adds instructions to envPtr to evaluate the script at runtime. |
| 1753 |
* |
| 1754 |
*---------------------------------------------------------------------- |
| 1755 |
*/ |
| 1756 |
|
| 1757 |
static int |
| 1758 |
ExpandRequested( |
| 1759 |
Tcl_Token *tokenPtr, |
| 1760 |
int numWords) |
| 1761 |
{ |
| 1762 |
/* Determine whether any words of the command require expansion */ |
| 1763 |
while (numWords--) { |
| 1764 |
if (tokenPtr->type == TCL_TOKEN_EXPAND_WORD) { |
| 1765 |
return 1; |
| 1766 |
} |
| 1767 |
tokenPtr = TokenAfter(tokenPtr); |
| 1768 |
} |
| 1769 |
return 0; |
| 1770 |
} |
| 1771 |
|
| 1772 |
static void |
| 1773 |
CompileCmdLiteral( |
| 1774 |
Tcl_Interp *interp, |
| 1775 |
Tcl_Obj *cmdObj, |
| 1776 |
CompileEnv *envPtr) |
| 1777 |
{ |
| 1778 |
int numBytes; |
| 1779 |
const char *bytes; |
| 1780 |
Command *cmdPtr; |
| 1781 |
int cmdLitIdx, extraLiteralFlags = LITERAL_CMD_NAME; |
| 1782 |
|
| 1783 |
cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, cmdObj); |
| 1784 |
if ((cmdPtr != NULL) && (cmdPtr->flags & CMD_VIA_RESOLVER)) { |
| 1785 |
extraLiteralFlags |= LITERAL_UNSHARED; |
| 1786 |
} |
| 1787 |
|
| 1788 |
bytes = Tcl_GetStringFromObj(cmdObj, &numBytes); |
| 1789 |
cmdLitIdx = TclRegisterLiteral(envPtr, (char *)bytes, numBytes, extraLiteralFlags); |
| 1790 |
|
| 1791 |
if (cmdPtr) { |
| 1792 |
TclSetCmdNameObj(interp, TclFetchLiteral(envPtr, cmdLitIdx), cmdPtr); |
| 1793 |
} |
| 1794 |
TclEmitPush(cmdLitIdx, envPtr); |
| 1795 |
} |
| 1796 |
|
| 1797 |
void |
| 1798 |
TclCompileInvocation( |
| 1799 |
Tcl_Interp *interp, |
| 1800 |
Tcl_Token *tokenPtr, |
| 1801 |
Tcl_Obj *cmdObj, |
| 1802 |
int numWords, |
| 1803 |
CompileEnv *envPtr) |
| 1804 |
{ |
| 1805 |
DefineLineInformation; |
| 1806 |
int wordIdx = 0, depth = TclGetStackDepth(envPtr); |
| 1807 |
|
| 1808 |
if (cmdObj) { |
| 1809 |
CompileCmdLiteral(interp, cmdObj, envPtr); |
| 1810 |
wordIdx = 1; |
| 1811 |
tokenPtr = TokenAfter(tokenPtr); |
| 1812 |
} |
| 1813 |
|
| 1814 |
for (; wordIdx < numWords; wordIdx++, tokenPtr = TokenAfter(tokenPtr)) { |
| 1815 |
int objIdx; |
| 1816 |
|
| 1817 |
SetLineInformation(wordIdx); |
| 1818 |
|
| 1819 |
if (tokenPtr->type != TCL_TOKEN_SIMPLE_WORD) { |
| 1820 |
CompileTokens(envPtr, tokenPtr, interp); |
| 1821 |
continue; |
| 1822 |
} |
| 1823 |
|
| 1824 |
objIdx = TclRegisterNewLiteral(envPtr, |
| 1825 |
tokenPtr[1].start, tokenPtr[1].size); |
| 1826 |
if (envPtr->clNext) { |
| 1827 |
TclContinuationsEnterDerived(TclFetchLiteral(envPtr, objIdx), |
| 1828 |
tokenPtr[1].start - envPtr->source, envPtr->clNext); |
| 1829 |
} |
| 1830 |
TclEmitPush(objIdx, envPtr); |
| 1831 |
} |
| 1832 |
|
| 1833 |
if (wordIdx <= 255) { |
| 1834 |
TclEmitInvoke(envPtr, INST_INVOKE_STK1, wordIdx); |
| 1835 |
} else { |
| 1836 |
TclEmitInvoke(envPtr, INST_INVOKE_STK4, wordIdx); |
| 1837 |
} |
| 1838 |
TclCheckStackDepth(depth+1, envPtr); |
| 1839 |
} |
| 1840 |
|
| 1841 |
static void |
| 1842 |
CompileExpanded( |
| 1843 |
Tcl_Interp *interp, |
| 1844 |
Tcl_Token *tokenPtr, |
| 1845 |
Tcl_Obj *cmdObj, |
| 1846 |
int numWords, |
| 1847 |
CompileEnv *envPtr) |
| 1848 |
{ |
| 1849 |
DefineLineInformation; |
| 1850 |
int wordIdx = 0; |
| 1851 |
int depth = TclGetStackDepth(envPtr); |
| 1852 |
|
| 1853 |
StartExpanding(envPtr); |
| 1854 |
if (cmdObj) { |
| 1855 |
CompileCmdLiteral(interp, cmdObj, envPtr); |
| 1856 |
wordIdx = 1; |
| 1857 |
tokenPtr = TokenAfter(tokenPtr); |
| 1858 |
} |
| 1859 |
|
| 1860 |
for (; wordIdx < numWords; wordIdx++, tokenPtr = TokenAfter(tokenPtr)) { |
| 1861 |
int objIdx; |
| 1862 |
|
| 1863 |
SetLineInformation(wordIdx); |
| 1864 |
|
| 1865 |
if (tokenPtr->type != TCL_TOKEN_SIMPLE_WORD) { |
| 1866 |
CompileTokens(envPtr, tokenPtr, interp); |
| 1867 |
if (tokenPtr->type == TCL_TOKEN_EXPAND_WORD) { |
| 1868 |
TclEmitInstInt4(INST_EXPAND_STKTOP, |
| 1869 |
envPtr->currStackDepth, envPtr); |
| 1870 |
} |
| 1871 |
continue; |
| 1872 |
} |
| 1873 |
|
| 1874 |
objIdx = TclRegisterNewLiteral(envPtr, |
| 1875 |
tokenPtr[1].start, tokenPtr[1].size); |
| 1876 |
if (envPtr->clNext) { |
| 1877 |
TclContinuationsEnterDerived(TclFetchLiteral(envPtr, objIdx), |
| 1878 |
tokenPtr[1].start - envPtr->source, envPtr->clNext); |
| 1879 |
} |
| 1880 |
TclEmitPush(objIdx, envPtr); |
| 1881 |
} |
| 1882 |
|
| 1883 |
/* |
| 1884 |
* The stack depth during argument expansion can only be managed at |
| 1885 |
* runtime, as the number of elements in the expanded lists is not known |
| 1886 |
* at compile time. We adjust here the stack depth estimate so that it is |
| 1887 |
* correct after the command with expanded arguments returns. |
| 1888 |
* |
| 1889 |
* The end effect of this command's invocation is that all the words of |
| 1890 |
* the command are popped from the stack, and the result is pushed: the |
| 1891 |
* stack top changes by (1-wordIdx). |
| 1892 |
* |
| 1893 |
* Note that the estimates are not correct while the command is being |
| 1894 |
* prepared and run, INST_EXPAND_STKTOP is not stack-neutral in general. |
| 1895 |
*/ |
| 1896 |
|
| 1897 |
TclEmitInvoke(envPtr, INST_INVOKE_EXPANDED, wordIdx); |
| 1898 |
TclCheckStackDepth(depth+1, envPtr); |
| 1899 |
} |
| 1900 |
|
| 1901 |
static int |
| 1902 |
CompileCmdCompileProc( |
| 1903 |
Tcl_Interp *interp, |
| 1904 |
Tcl_Parse *parsePtr, |
| 1905 |
Command *cmdPtr, |
| 1906 |
CompileEnv *envPtr) |
| 1907 |
{ |
| 1908 |
DefineLineInformation; |
| 1909 |
int unwind = 0, incrOffset = -1; |
| 1910 |
int depth = TclGetStackDepth(envPtr); |
| 1911 |
|
| 1912 |
/* |
| 1913 |
* Emit of the INST_START_CMD instruction is controlled by the value of |
| 1914 |
* envPtr->atCmdStart: |
| 1915 |
* |
| 1916 |
* atCmdStart == 2 : We are not using the INST_START_CMD instruction. |
| 1917 |
* atCmdStart == 1 : INST_START_CMD was the last instruction emitted. |
| 1918 |
* : We do not need to emit another. Instead we |
| 1919 |
* : increment the number of cmds started at it (except |
| 1920 |
* : for the special case at the start of a script.) |
| 1921 |
* atCmdStart == 0 : The last instruction was something else. We need |
| 1922 |
* : to emit INST_START_CMD here. |
| 1923 |
*/ |
| 1924 |
|
| 1925 |
switch (envPtr->atCmdStart) { |
| 1926 |
case 0: |
| 1927 |
unwind = tclInstructionTable[INST_START_CMD].numBytes; |
| 1928 |
TclEmitInstInt4(INST_START_CMD, 0, envPtr); |
| 1929 |
incrOffset = envPtr->codeNext - envPtr->codeStart; |
| 1930 |
TclEmitInt4(0, envPtr); |
| 1931 |
break; |
| 1932 |
case 1: |
| 1933 |
if (envPtr->codeNext > envPtr->codeStart) { |
| 1934 |
incrOffset = envPtr->codeNext - 4 - envPtr->codeStart; |
| 1935 |
} |
| 1936 |
break; |
| 1937 |
case 2: |
| 1938 |
/* Nothing to do */ |
| 1939 |
; |
| 1940 |
} |
| 1941 |
|
| 1942 |
if (TCL_OK == TclAttemptCompileProc(interp, parsePtr, 1, cmdPtr, envPtr)) { |
| 1943 |
if (incrOffset >= 0) { |
| 1944 |
/* |
| 1945 |
* We successfully compiled a command. Increment the number of |
| 1946 |
* commands that start at the currently active INST_START_CMD. |
| 1947 |
*/ |
| 1948 |
|
| 1949 |
unsigned char *incrPtr = envPtr->codeStart + incrOffset; |
| 1950 |
unsigned char *startPtr = incrPtr - 5; |
| 1951 |
|
| 1952 |
TclIncrUInt4AtPtr(incrPtr, 1); |
| 1953 |
if (unwind) { |
| 1954 |
/* We started the INST_START_CMD. Record the code length. */ |
| 1955 |
TclStoreInt4AtPtr(envPtr->codeNext - startPtr, startPtr + 1); |
| 1956 |
} |
| 1957 |
} |
| 1958 |
TclCheckStackDepth(depth+1, envPtr); |
| 1959 |
return TCL_OK; |
| 1960 |
} |
| 1961 |
|
| 1962 |
envPtr->codeNext -= unwind; /* Unwind INST_START_CMD */ |
| 1963 |
|
| 1964 |
/* |
| 1965 |
* Throw out any line information generated by the failed compile attempt. |
| 1966 |
*/ |
| 1967 |
|
| 1968 |
while (mapPtr->nuloc - 1 > eclIndex) { |
| 1969 |
mapPtr->nuloc--; |
| 1970 |
ckfree(mapPtr->loc[mapPtr->nuloc].line); |
| 1971 |
mapPtr->loc[mapPtr->nuloc].line = NULL; |
| 1972 |
} |
| 1973 |
|
| 1974 |
/* |
| 1975 |
* Reset the index of next command. Toss out any from failed nested |
| 1976 |
* partial compiles. |
| 1977 |
*/ |
| 1978 |
|
| 1979 |
envPtr->numCommands = mapPtr->nuloc; |
| 1980 |
return TCL_ERROR; |
| 1981 |
} |
| 1982 |
|
| 1983 |
static int |
| 1984 |
CompileCommandTokens( |
| 1985 |
Tcl_Interp *interp, |
| 1986 |
Tcl_Parse *parsePtr, |
| 1987 |
CompileEnv *envPtr) |
| 1988 |
{ |
| 1989 |
Interp *iPtr = (Interp *) interp; |
| 1990 |
Tcl_Token *tokenPtr = parsePtr->tokenPtr; |
| 1991 |
ExtCmdLoc *eclPtr = envPtr->extCmdMapPtr; |
| 1992 |
Tcl_Obj *cmdObj; |
| 1993 |
Command *cmdPtr = NULL; |
| 1994 |
int code = TCL_ERROR; |
| 1995 |
int cmdKnown, expand = -1; |
| 1996 |
int *wlines, wlineat; |
| 1997 |
int cmdLine = envPtr->line; |
| 1998 |
int *clNext = envPtr->clNext; |
| 1999 |
int cmdIdx = envPtr->numCommands; |
| 2000 |
int startCodeOffset = envPtr->codeNext - envPtr->codeStart; |
| 2001 |
int depth = TclGetStackDepth(envPtr); |
| 2002 |
|
| 2003 |
TclNewObj(cmdObj); |
| 2004 |
assert (parsePtr->numWords > 0); |
| 2005 |
|
| 2006 |
/* Precompile */ |
| 2007 |
|
| 2008 |
envPtr->numCommands++; |
| 2009 |
EnterCmdStartData(envPtr, cmdIdx, |
| 2010 |
parsePtr->commandStart - envPtr->source, startCodeOffset); |
| 2011 |
|
| 2012 |
/* |
| 2013 |
* TIP #280. Scan the words and compute the extended location information. |
| 2014 |
* At first the map first contains full per-word line information for use by the |
| 2015 |
* compiler. This is later replaced by a reduced form which signals |
| 2016 |
* non-literal words, stored in 'wlines'. |
| 2017 |
*/ |
| 2018 |
|
| 2019 |
EnterCmdWordData(eclPtr, parsePtr->commandStart - envPtr->source, |
| 2020 |
parsePtr->tokenPtr, parsePtr->commandStart, |
| 2021 |
parsePtr->commandSize, parsePtr->numWords, cmdLine, |
| 2022 |
clNext, &wlines, envPtr); |
| 2023 |
wlineat = eclPtr->nuloc - 1; |
| 2024 |
|
| 2025 |
envPtr->line = eclPtr->loc[wlineat].line[0]; |
| 2026 |
envPtr->clNext = eclPtr->loc[wlineat].next[0]; |
| 2027 |
|
| 2028 |
/* Do we know the command word? */ |
| 2029 |
Tcl_IncrRefCount(cmdObj); |
| 2030 |
tokenPtr = parsePtr->tokenPtr; |
| 2031 |
cmdKnown = TclWordKnownAtCompileTime(tokenPtr, cmdObj); |
| 2032 |
|
| 2033 |
/* Is this a command we should (try to) compile with a compileProc ? */ |
| 2034 |
if (cmdKnown && !(iPtr->flags & DONT_COMPILE_CMDS_INLINE)) { |
| 2035 |
cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, cmdObj); |
| 2036 |
if (cmdPtr) { |
| 2037 |
/* |
| 2038 |
* Found a command. Test the ways we can be told not to attempt |
| 2039 |
* to compile it. |
| 2040 |
*/ |
| 2041 |
if ((cmdPtr->compileProc == NULL) |
| 2042 |
|| (cmdPtr->nsPtr->flags & NS_SUPPRESS_COMPILATION) |
| 2043 |
|| (cmdPtr->flags & CMD_HAS_EXEC_TRACES)) { |
| 2044 |
cmdPtr = NULL; |
| 2045 |
} |
| 2046 |
} |
| 2047 |
if (cmdPtr && !(cmdPtr->flags & CMD_COMPILES_EXPANDED)) { |
| 2048 |
expand = ExpandRequested(parsePtr->tokenPtr, parsePtr->numWords); |
| 2049 |
if (expand) { |
| 2050 |
/* We need to expand, but compileProc cannot. */ |
| 2051 |
cmdPtr = NULL; |
| 2052 |
} |
| 2053 |
} |
| 2054 |
} |
| 2055 |
|
| 2056 |
/* If cmdPtr != NULL, try to call cmdPtr->compileProc */ |
| 2057 |
if (cmdPtr) { |
| 2058 |
code = CompileCmdCompileProc(interp, parsePtr, cmdPtr, envPtr); |
| 2059 |
} |
| 2060 |
|
| 2061 |
if (code == TCL_ERROR) { |
| 2062 |
if (expand < 0) { |
| 2063 |
expand = ExpandRequested(parsePtr->tokenPtr, parsePtr->numWords); |
| 2064 |
} |
| 2065 |
|
| 2066 |
if (expand) { |
| 2067 |
CompileExpanded(interp, parsePtr->tokenPtr, |
| 2068 |
cmdKnown ? cmdObj : NULL, parsePtr->numWords, envPtr); |
| 2069 |
} else { |
| 2070 |
TclCompileInvocation(interp, parsePtr->tokenPtr, |
| 2071 |
cmdKnown ? cmdObj : NULL, parsePtr->numWords, envPtr); |
| 2072 |
} |
| 2073 |
} |
| 2074 |
|
| 2075 |
Tcl_DecrRefCount(cmdObj); |
| 2076 |
|
| 2077 |
TclEmitOpcode(INST_POP, envPtr); |
| 2078 |
EnterCmdExtentData(envPtr, cmdIdx, |
| 2079 |
parsePtr->term - parsePtr->commandStart, |
| 2080 |
(envPtr->codeNext-envPtr->codeStart) - startCodeOffset); |
| 2081 |
|
| 2082 |
/* |
| 2083 |
* TIP #280: Free full form of per-word line data and insert the reduced |
| 2084 |
* form now |
| 2085 |
*/ |
| 2086 |
|
| 2087 |
envPtr->line = cmdLine; |
| 2088 |
envPtr->clNext = clNext; |
| 2089 |
ckfree(eclPtr->loc[wlineat].line); |
| 2090 |
ckfree(eclPtr->loc[wlineat].next); |
| 2091 |
eclPtr->loc[wlineat].line = wlines; |
| 2092 |
eclPtr->loc[wlineat].next = NULL; |
| 2093 |
|
| 2094 |
TclCheckStackDepth(depth, envPtr); |
| 2095 |
return cmdIdx; |
| 2096 |
} |
| 2097 |
|
| 2098 |
void |
| 2099 |
TclCompileScript( |
| 2100 |
Tcl_Interp *interp, /* Used for error and status reporting. Also |
| 2101 |
* serves as context for finding and compiling |
| 2102 |
* commands. May not be NULL. */ |
| 2103 |
const char *script, /* The source script to compile. */ |
| 2104 |
int numBytes, /* Number of bytes in script. If < 0, the |
| 2105 |
* script consists of all bytes up to the |
| 2106 |
* first null character. */ |
| 2107 |
CompileEnv *envPtr) /* Holds resulting instructions. */ |
| 2108 |
{ |
| 2109 |
int lastCmdIdx = -1; /* Index into envPtr->cmdMapPtr of the last |
| 2110 |
* command this routine compiles into bytecode. |
| 2111 |
* Initial value of -1 indicates this routine |
| 2112 |
* has not yet generated any bytecode. */ |
| 2113 |
const char *p = script; /* Where we are in our compile. */ |
| 2114 |
int depth = TclGetStackDepth(envPtr); |
| 2115 |
Interp *iPtr = (Interp *) interp; |
| 2116 |
|
| 2117 |
if (envPtr->iPtr == NULL) { |
| 2118 |
Tcl_Panic("TclCompileScript() called on uninitialized CompileEnv"); |
| 2119 |
} |
| 2120 |
/* |
| 2121 |
* Check depth to avoid overflow of the C execution stack by too many |
| 2122 |
* nested calls of TclCompileScript, considering interp recursionlimit. |
| 2123 |
* Use factor 5/4 (1.25) to avoid being too mistaken when recognizing the |
| 2124 |
* limit during "mixed" evaluation and compilation process (nested |
| 2125 |
* eval+compile) and is good enough for default recursionlimit (1000). |
| 2126 |
*/ |
| 2127 |
if (iPtr->numLevels / 5 > iPtr->maxNestingDepth / 4) { |
| 2128 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2129 |
"too many nested compilations (infinite loop?)", -1)); |
| 2130 |
Tcl_SetErrorCode(interp, "TCL", "LIMIT", "STACK", (char *)NULL); |
| 2131 |
TclCompileSyntaxError(interp, envPtr); |
| 2132 |
return; |
| 2133 |
} |
| 2134 |
|
| 2135 |
/* Each iteration compiles one command from the script. */ |
| 2136 |
|
| 2137 |
if (numBytes > 0) { |
| 2138 |
/* |
| 2139 |
* Don't use system stack (size of Tcl_Parse is ca. 400 bytes), so |
| 2140 |
* many nested compilations (body enclosed in body) can cause abnormal |
| 2141 |
* program termination with a stack overflow exception, bug [fec0c17d39]. |
| 2142 |
*/ |
| 2143 |
Tcl_Parse *parsePtr = (Tcl_Parse *)ckalloc(sizeof(Tcl_Parse)); |
| 2144 |
|
| 2145 |
do { |
| 2146 |
const char *next; |
| 2147 |
|
| 2148 |
if (TCL_OK != Tcl_ParseCommand(interp, p, numBytes, 0, parsePtr)) { |
| 2149 |
/* |
| 2150 |
* Compile bytecodes to report the parsePtr error at runtime. |
| 2151 |
*/ |
| 2152 |
|
| 2153 |
Tcl_LogCommandInfo(interp, script, parsePtr->commandStart, |
| 2154 |
parsePtr->term + 1 - parsePtr->commandStart); |
| 2155 |
TclCompileSyntaxError(interp, envPtr); |
| 2156 |
ckfree(parsePtr); |
| 2157 |
return; |
| 2158 |
} |
| 2159 |
|
| 2160 |
#ifdef TCL_COMPILE_DEBUG |
| 2161 |
/* |
| 2162 |
* If tracing, print a line for each top level command compiled. |
| 2163 |
* TODO: Suppress when numWords == 0 ? |
| 2164 |
*/ |
| 2165 |
|
| 2166 |
if ((tclTraceCompile >= 1) && (envPtr->procPtr == NULL)) { |
| 2167 |
int commandLength = parsePtr->term - parsePtr->commandStart; |
| 2168 |
fprintf(stdout, " Compiling: "); |
| 2169 |
TclPrintSource(stdout, parsePtr->commandStart, |
| 2170 |
TclMin(commandLength, 55)); |
| 2171 |
fprintf(stdout, "\n"); |
| 2172 |
} |
| 2173 |
#endif |
| 2174 |
|
| 2175 |
/* |
| 2176 |
* TIP #280: Count newlines before the command start. |
| 2177 |
* (See test info-30.33). |
| 2178 |
*/ |
| 2179 |
|
| 2180 |
TclAdvanceLines(&envPtr->line, p, parsePtr->commandStart); |
| 2181 |
TclAdvanceContinuations(&envPtr->line, &envPtr->clNext, |
| 2182 |
parsePtr->commandStart - envPtr->source); |
| 2183 |
|
| 2184 |
/* |
| 2185 |
* Advance parser to the next command in the script. |
| 2186 |
*/ |
| 2187 |
|
| 2188 |
next = parsePtr->commandStart + parsePtr->commandSize; |
| 2189 |
numBytes -= next - p; |
| 2190 |
p = next; |
| 2191 |
|
| 2192 |
if (parsePtr->numWords == 0) { |
| 2193 |
/* |
| 2194 |
* The "command" parsed has no words. In this case we can skip |
| 2195 |
* the rest of the loop body. With no words, clearly |
| 2196 |
* CompileCommandTokens() has nothing to do. Since the parser |
| 2197 |
* aggressively sucks up leading comment and white space, |
| 2198 |
* including newlines, parsePtr->commandStart must be pointing at |
| 2199 |
* either the end of script, or a command-terminating semi-colon. |
| 2200 |
* In either case, the TclAdvance*() calls have nothing to do. |
| 2201 |
* Finally, when no words are parsed, no tokens have been |
| 2202 |
* allocated at parsePtr->tokenPtr so there's also nothing for |
| 2203 |
* Tcl_FreeParse() to do. |
| 2204 |
* |
| 2205 |
* The advantage of this shortcut is that CompileCommandTokens() |
| 2206 |
* can be written with an assumption that parsePtr->numWords > 0, with |
| 2207 |
* the implication the CCT() always generates bytecode. |
| 2208 |
*/ |
| 2209 |
continue; |
| 2210 |
} |
| 2211 |
|
| 2212 |
/* |
| 2213 |
* Avoid stack exhaustion by too many nested calls of TclCompileScript |
| 2214 |
* (considering interp recursionlimit). |
| 2215 |
*/ |
| 2216 |
iPtr->numLevels++; |
| 2217 |
|
| 2218 |
lastCmdIdx = CompileCommandTokens(interp, parsePtr, envPtr); |
| 2219 |
|
| 2220 |
iPtr->numLevels--; |
| 2221 |
|
| 2222 |
/* |
| 2223 |
* TIP #280: Track lines in the just compiled command. |
| 2224 |
*/ |
| 2225 |
|
| 2226 |
TclAdvanceLines(&envPtr->line, parsePtr->commandStart, p); |
| 2227 |
TclAdvanceContinuations(&envPtr->line, &envPtr->clNext, |
| 2228 |
p - envPtr->source); |
| 2229 |
Tcl_FreeParse(parsePtr); |
| 2230 |
} while (numBytes > 0); |
| 2231 |
|
| 2232 |
ckfree(parsePtr); |
| 2233 |
} |
| 2234 |
|
| 2235 |
if (lastCmdIdx == -1) { |
| 2236 |
/* |
| 2237 |
* Compiling the script yielded no bytecode. The script must be all |
| 2238 |
* whitespace, comments, and empty commands. Such scripts are defined |
| 2239 |
* to successfully produce the empty string result, so we emit the |
| 2240 |
* simple bytecode that makes that happen. |
| 2241 |
*/ |
| 2242 |
|
| 2243 |
PushStringLiteral(envPtr, ""); |
| 2244 |
} else { |
| 2245 |
/* |
| 2246 |
* We compiled at least one command to bytecode. The routine |
| 2247 |
* CompileCommandTokens() follows the bytecode of each compiled |
| 2248 |
* command with an INST_POP, so that stack balance is maintained when |
| 2249 |
* several commands are in sequence. (The result of each command is |
| 2250 |
* thrown away before moving on to the next command). For the last |
| 2251 |
* command compiled, we need to undo that INST_POP so that the result |
| 2252 |
* of the last command becomes the result of the script. The code |
| 2253 |
* here removes that trailing INST_POP. |
| 2254 |
*/ |
| 2255 |
|
| 2256 |
envPtr->cmdMapPtr[lastCmdIdx].numCodeBytes--; |
| 2257 |
envPtr->codeNext--; |
| 2258 |
envPtr->currStackDepth++; |
| 2259 |
} |
| 2260 |
TclCheckStackDepth(depth+1, envPtr); |
| 2261 |
} |
| 2262 |
|
| 2263 |
/* |
| 2264 |
*---------------------------------------------------------------------- |
| 2265 |
* |
| 2266 |
* TclCompileTokens -- |
| 2267 |
* |
| 2268 |
* Given an array of tokens parsed from a Tcl command, e.g. the tokens |
| 2269 |
* that make up a word, emits instructions to evaluate the |
| 2270 |
* tokens and concatenate their values to form a single result value on |
| 2271 |
* the interpreter's runtime evaluation stack. |
| 2272 |
* |
| 2273 |
* Results: |
| 2274 |
* The return value is a standard Tcl result. If an error occurs, an |
| 2275 |
* error message is left in the interpreter's result. |
| 2276 |
* |
| 2277 |
* Side effects: |
| 2278 |
* Instructions are added to envPtr to push and evaluate the tokens at |
| 2279 |
* runtime. |
| 2280 |
* |
| 2281 |
*---------------------------------------------------------------------- |
| 2282 |
*/ |
| 2283 |
|
| 2284 |
void |
| 2285 |
TclCompileVarSubst( |
| 2286 |
Tcl_Interp *interp, |
| 2287 |
Tcl_Token *tokenPtr, |
| 2288 |
CompileEnv *envPtr) |
| 2289 |
{ |
| 2290 |
const char *p, *name = tokenPtr[1].start; |
| 2291 |
int i, localVar, nameBytes = tokenPtr[1].size; |
| 2292 |
int localVarName = 1; |
| 2293 |
|
| 2294 |
/* |
| 2295 |
* Determine how the variable name should be handled: if it contains any |
| 2296 |
* namespace qualifiers it is not a local variable (localVarName=-1); if |
| 2297 |
* it looks like an array element and the token has a single component, it |
| 2298 |
* should not be created here [Bug 569438] (localVarName=0); otherwise, |
| 2299 |
* the local variable can safely be created (localVarName=1). |
| 2300 |
*/ |
| 2301 |
|
| 2302 |
for (i = 0, p = name; i < nameBytes; i++, p++) { |
| 2303 |
if ((*p == ':') && (i < nameBytes-1) && (*(p+1) == ':')) { |
| 2304 |
localVarName = -1; |
| 2305 |
break; |
| 2306 |
} else if ((*p == '(') |
| 2307 |
&& (tokenPtr->numComponents == 1) |
| 2308 |
&& (*(name + nameBytes - 1) == ')')) { |
| 2309 |
localVarName = 0; |
| 2310 |
break; |
| 2311 |
} |
| 2312 |
} |
| 2313 |
|
| 2314 |
/* |
| 2315 |
* Either push the variable's name, or find its index in the array |
| 2316 |
* of local variables in a procedure frame. |
| 2317 |
*/ |
| 2318 |
|
| 2319 |
localVar = -1; |
| 2320 |
if (localVarName != -1) { |
| 2321 |
localVar = TclFindCompiledLocal(name, nameBytes, localVarName, envPtr); |
| 2322 |
} |
| 2323 |
if (localVar < 0) { |
| 2324 |
PushLiteral(envPtr, name, nameBytes); |
| 2325 |
} |
| 2326 |
|
| 2327 |
/* |
| 2328 |
* Emit instructions to load the variable. |
| 2329 |
*/ |
| 2330 |
|
| 2331 |
TclAdvanceLines(&envPtr->line, tokenPtr[1].start, |
| 2332 |
tokenPtr[1].start + tokenPtr[1].size); |
| 2333 |
|
| 2334 |
if (tokenPtr->numComponents == 1) { |
| 2335 |
if (localVar < 0) { |
| 2336 |
TclEmitOpcode(INST_LOAD_STK, envPtr); |
| 2337 |
} else if (localVar <= 255) { |
| 2338 |
TclEmitInstInt1(INST_LOAD_SCALAR1, localVar, envPtr); |
| 2339 |
} else { |
| 2340 |
TclEmitInstInt4(INST_LOAD_SCALAR4, localVar, envPtr); |
| 2341 |
} |
| 2342 |
} else { |
| 2343 |
TclCompileTokens(interp, tokenPtr+2, tokenPtr->numComponents-1, envPtr); |
| 2344 |
if (localVar < 0) { |
| 2345 |
TclEmitOpcode(INST_LOAD_ARRAY_STK, envPtr); |
| 2346 |
} else if (localVar <= 255) { |
| 2347 |
TclEmitInstInt1(INST_LOAD_ARRAY1, localVar, envPtr); |
| 2348 |
} else { |
| 2349 |
TclEmitInstInt4(INST_LOAD_ARRAY4, localVar, envPtr); |
| 2350 |
} |
| 2351 |
} |
| 2352 |
} |
| 2353 |
|
| 2354 |
void |
| 2355 |
TclCompileTokens( |
| 2356 |
Tcl_Interp *interp, /* Used for error and status reporting. */ |
| 2357 |
Tcl_Token *tokenPtr, /* Pointer to first in an array of tokens to |
| 2358 |
* compile. */ |
| 2359 |
int count, /* Number of tokens to consider at tokenPtr. |
| 2360 |
* Must be at least 1. */ |
| 2361 |
CompileEnv *envPtr) /* Holds the resulting instructions. */ |
| 2362 |
{ |
| 2363 |
Tcl_DString textBuffer; /* Holds concatenated chars from adjacent |
| 2364 |
* TCL_TOKEN_TEXT, TCL_TOKEN_BS tokens. */ |
| 2365 |
char buffer[TCL_UTF_MAX] = ""; |
| 2366 |
int i, numObjsToConcat, length, adjust; |
| 2367 |
unsigned char *entryCodeNext = envPtr->codeNext; |
| 2368 |
#define NUM_STATIC_POS 20 |
| 2369 |
int isLiteral, maxNumCL, numCL; |
| 2370 |
int *clPosition = NULL; |
| 2371 |
int depth = TclGetStackDepth(envPtr); |
| 2372 |
|
| 2373 |
/* |
| 2374 |
* For the handling of continuation lines in literals, first check if |
| 2375 |
* this is actually a literal. For if not we can forego the additional |
| 2376 |
* processing. Otherwise preallocate a small table to store the |
| 2377 |
* locations of all continuation lines found in this literal, if any. |
| 2378 |
* The table is extended if needed. |
| 2379 |
* |
| 2380 |
* Note: Different to the equivalent code in function 'TclSubstTokens()' |
| 2381 |
* (see file "tclParse.c") there seem to be no need the 'adjust' variable. |
| 2382 |
* There also seems to be no need for code which merges continuation line |
| 2383 |
* information of multiple words which concat'd at runtime. Either that or |
| 2384 |
* I have not managed to find a test case for these two possibilities yet. |
| 2385 |
* It might be a difference between compile- versus run-time processing. |
| 2386 |
*/ |
| 2387 |
|
| 2388 |
numCL = 0; |
| 2389 |
maxNumCL = 0; |
| 2390 |
isLiteral = 1; |
| 2391 |
for (i=0 ; i < count; i++) { |
| 2392 |
if ((tokenPtr[i].type != TCL_TOKEN_TEXT) |
| 2393 |
&& (tokenPtr[i].type != TCL_TOKEN_BS)) { |
| 2394 |
isLiteral = 0; |
| 2395 |
break; |
| 2396 |
} |
| 2397 |
} |
| 2398 |
|
| 2399 |
if (isLiteral) { |
| 2400 |
maxNumCL = NUM_STATIC_POS; |
| 2401 |
clPosition = (int *)ckalloc(maxNumCL * sizeof(int)); |
| 2402 |
} |
| 2403 |
|
| 2404 |
adjust = 0; |
| 2405 |
Tcl_DStringInit(&textBuffer); |
| 2406 |
numObjsToConcat = 0; |
| 2407 |
for ( ; count > 0; count--, tokenPtr++) { |
| 2408 |
switch (tokenPtr->type) { |
| 2409 |
case TCL_TOKEN_TEXT: |
| 2410 |
TclDStringAppendToken(&textBuffer, tokenPtr); |
| 2411 |
TclAdvanceLines(&envPtr->line, tokenPtr->start, |
| 2412 |
tokenPtr->start + tokenPtr->size); |
| 2413 |
break; |
| 2414 |
|
| 2415 |
case TCL_TOKEN_BS: |
| 2416 |
length = TclParseBackslash(tokenPtr->start, tokenPtr->size, |
| 2417 |
NULL, buffer); |
| 2418 |
Tcl_DStringAppend(&textBuffer, buffer, length); |
| 2419 |
|
| 2420 |
/* |
| 2421 |
* If the backslash sequence we found is in a literal, and |
| 2422 |
* represented a continuation line, we compute and store its |
| 2423 |
* location (as char offset to the beginning of the _result_ |
| 2424 |
* script). We may have to extend the table of locations. |
| 2425 |
* |
| 2426 |
* Note that the continuation line information is relevant even if |
| 2427 |
* the word we are processing is not a literal, as it can affect |
| 2428 |
* nested commands. See the branch for TCL_TOKEN_COMMAND below, |
| 2429 |
* where the adjustment we are tracking here is taken into |
| 2430 |
* account. The good thing is that we do not need a table of |
| 2431 |
* everything, just the number of lines we have to add as |
| 2432 |
* correction. |
| 2433 |
*/ |
| 2434 |
|
| 2435 |
if ((length == 1) && (buffer[0] == ' ') && |
| 2436 |
(tokenPtr->start[1] == '\n')) { |
| 2437 |
if (isLiteral) { |
| 2438 |
int clPos = Tcl_DStringLength(&textBuffer); |
| 2439 |
|
| 2440 |
if (numCL >= maxNumCL) { |
| 2441 |
maxNumCL *= 2; |
| 2442 |
clPosition = (int *)ckrealloc(clPosition, |
| 2443 |
maxNumCL * sizeof(int)); |
| 2444 |
} |
| 2445 |
clPosition[numCL] = clPos; |
| 2446 |
numCL ++; |
| 2447 |
} |
| 2448 |
adjust++; |
| 2449 |
} |
| 2450 |
break; |
| 2451 |
|
| 2452 |
case TCL_TOKEN_COMMAND: |
| 2453 |
/* |
| 2454 |
* Push any accumulated chars appearing before the command. |
| 2455 |
*/ |
| 2456 |
|
| 2457 |
if (Tcl_DStringLength(&textBuffer) > 0) { |
| 2458 |
int literal = TclRegisterDStringLiteral(envPtr, &textBuffer); |
| 2459 |
|
| 2460 |
TclEmitPush(literal, envPtr); |
| 2461 |
numObjsToConcat++; |
| 2462 |
Tcl_DStringFree(&textBuffer); |
| 2463 |
|
| 2464 |
if (numCL) { |
| 2465 |
TclContinuationsEnter(TclFetchLiteral(envPtr, literal), |
| 2466 |
numCL, clPosition); |
| 2467 |
} |
| 2468 |
numCL = 0; |
| 2469 |
} |
| 2470 |
|
| 2471 |
envPtr->line += adjust; |
| 2472 |
TclCompileScript(interp, tokenPtr->start+1, |
| 2473 |
tokenPtr->size-2, envPtr); |
| 2474 |
envPtr->line -= adjust; |
| 2475 |
numObjsToConcat++; |
| 2476 |
break; |
| 2477 |
|
| 2478 |
case TCL_TOKEN_VARIABLE: |
| 2479 |
/* |
| 2480 |
* Push any accumulated chars appearing before the $<var>. |
| 2481 |
*/ |
| 2482 |
|
| 2483 |
if (Tcl_DStringLength(&textBuffer) > 0) { |
| 2484 |
int literal; |
| 2485 |
|
| 2486 |
literal = TclRegisterDStringLiteral(envPtr, &textBuffer); |
| 2487 |
TclEmitPush(literal, envPtr); |
| 2488 |
numObjsToConcat++; |
| 2489 |
Tcl_DStringFree(&textBuffer); |
| 2490 |
} |
| 2491 |
|
| 2492 |
TclCompileVarSubst(interp, tokenPtr, envPtr); |
| 2493 |
numObjsToConcat++; |
| 2494 |
count -= tokenPtr->numComponents; |
| 2495 |
tokenPtr += tokenPtr->numComponents; |
| 2496 |
break; |
| 2497 |
|
| 2498 |
default: |
| 2499 |
Tcl_Panic("Unexpected token type in TclCompileTokens: %d; %.*s", |
| 2500 |
tokenPtr->type, tokenPtr->size, tokenPtr->start); |
| 2501 |
} |
| 2502 |
} |
| 2503 |
|
| 2504 |
/* |
| 2505 |
* Push any accumulated characters appearing at the end. |
| 2506 |
*/ |
| 2507 |
|
| 2508 |
if (Tcl_DStringLength(&textBuffer) > 0) { |
| 2509 |
int literal = TclRegisterDStringLiteral(envPtr, &textBuffer); |
| 2510 |
|
| 2511 |
TclEmitPush(literal, envPtr); |
| 2512 |
numObjsToConcat++; |
| 2513 |
if (numCL) { |
| 2514 |
TclContinuationsEnter(TclFetchLiteral(envPtr, literal), |
| 2515 |
numCL, clPosition); |
| 2516 |
} |
| 2517 |
numCL = 0; |
| 2518 |
} |
| 2519 |
|
| 2520 |
/* |
| 2521 |
* If necessary, concatenate the parts of the word. |
| 2522 |
*/ |
| 2523 |
|
| 2524 |
while (numObjsToConcat > 255) { |
| 2525 |
TclEmitInstInt1(INST_STR_CONCAT1, 255, envPtr); |
| 2526 |
numObjsToConcat -= 254; /* concat pushes 1 obj, the result */ |
| 2527 |
} |
| 2528 |
if (numObjsToConcat > 1) { |
| 2529 |
TclEmitInstInt1(INST_STR_CONCAT1, numObjsToConcat, envPtr); |
| 2530 |
} |
| 2531 |
|
| 2532 |
/* |
| 2533 |
* If the tokens yielded no instructions, push an empty string. |
| 2534 |
*/ |
| 2535 |
|
| 2536 |
if (envPtr->codeNext == entryCodeNext) { |
| 2537 |
PushStringLiteral(envPtr, ""); |
| 2538 |
} |
| 2539 |
Tcl_DStringFree(&textBuffer); |
| 2540 |
|
| 2541 |
/* |
| 2542 |
* Release the temp table we used to collect the locations of continuation |
| 2543 |
* lines, if any. |
| 2544 |
*/ |
| 2545 |
|
| 2546 |
if (maxNumCL) { |
| 2547 |
ckfree(clPosition); |
| 2548 |
} |
| 2549 |
TclCheckStackDepth(depth+1, envPtr); |
| 2550 |
} |
| 2551 |
|
| 2552 |
/* |
| 2553 |
*---------------------------------------------------------------------- |
| 2554 |
* |
| 2555 |
* TclCompileCmdWord -- |
| 2556 |
* |
| 2557 |
* Given an array of parse tokens for a word containing one or more Tcl |
| 2558 |
* commands, emit inline instructions to execute them. This procedure |
| 2559 |
* differs from TclCompileTokens in that a simple word such as a loop |
| 2560 |
* body enclosed in braces is not just pushed as a string, but is itself |
| 2561 |
* parsed into tokens and compiled. |
| 2562 |
* |
| 2563 |
* Results: |
| 2564 |
* The return value is a standard Tcl result. If an error occurs, an |
| 2565 |
* error message is left in the interpreter's result. |
| 2566 |
* |
| 2567 |
* Side effects: |
| 2568 |
* Instructions are added to envPtr to execute the tokens at runtime. |
| 2569 |
* |
| 2570 |
*---------------------------------------------------------------------- |
| 2571 |
*/ |
| 2572 |
|
| 2573 |
void |
| 2574 |
TclCompileCmdWord( |
| 2575 |
Tcl_Interp *interp, /* Used for error and status reporting. */ |
| 2576 |
Tcl_Token *tokenPtr, /* Pointer to first in an array of tokens for |
| 2577 |
* a command word to compile inline. */ |
| 2578 |
int count, /* Number of tokens to consider at tokenPtr. |
| 2579 |
* Must be at least 1. */ |
| 2580 |
CompileEnv *envPtr) /* Holds the resulting instructions. */ |
| 2581 |
{ |
| 2582 |
if ((count == 1) && (tokenPtr->type == TCL_TOKEN_TEXT)) { |
| 2583 |
/* |
| 2584 |
* Handle the common case: if there is a single text token, compile it |
| 2585 |
* into an inline sequence of instructions. |
| 2586 |
*/ |
| 2587 |
|
| 2588 |
TclCompileScript(interp, tokenPtr->start, tokenPtr->size, envPtr); |
| 2589 |
} else { |
| 2590 |
/* |
| 2591 |
* Multiple tokens or the single token involves substitutions. Emit |
| 2592 |
* instructions to invoke the eval command procedure at runtime on the |
| 2593 |
* result of evaluating the tokens. |
| 2594 |
*/ |
| 2595 |
|
| 2596 |
TclCompileTokens(interp, tokenPtr, count, envPtr); |
| 2597 |
TclEmitInvoke(envPtr, INST_EVAL_STK); |
| 2598 |
} |
| 2599 |
} |
| 2600 |
|
| 2601 |
/* |
| 2602 |
*---------------------------------------------------------------------- |
| 2603 |
* |
| 2604 |
* TclCompileExprWords -- |
| 2605 |
* |
| 2606 |
* Given an array of parse tokens representing one or more words that |
| 2607 |
* contain a Tcl expression, emit inline instructions to execute the |
| 2608 |
* expression. This procedure differs from TclCompileExpr in that it |
| 2609 |
* supports Tcl's two-level substitution semantics for expressions that |
| 2610 |
* appear as command words. |
| 2611 |
* |
| 2612 |
* Results: |
| 2613 |
* The return value is a standard Tcl result. If an error occurs, an |
| 2614 |
* error message is left in the interpreter's result. |
| 2615 |
* |
| 2616 |
* Side effects: |
| 2617 |
* Instructions are added to envPtr to execute the expression. |
| 2618 |
* |
| 2619 |
*---------------------------------------------------------------------- |
| 2620 |
*/ |
| 2621 |
|
| 2622 |
void |
| 2623 |
TclCompileExprWords( |
| 2624 |
Tcl_Interp *interp, /* Used for error and status reporting. */ |
| 2625 |
Tcl_Token *tokenPtr, /* Points to first in an array of word tokens |
| 2626 |
* tokens for the expression to compile |
| 2627 |
* inline. */ |
| 2628 |
int numWords, /* Number of word tokens starting at tokenPtr. |
| 2629 |
* Must be at least 1. Each word token |
| 2630 |
* contains one or more subtokens. */ |
| 2631 |
CompileEnv *envPtr) /* Holds the resulting instructions. */ |
| 2632 |
{ |
| 2633 |
Tcl_Token *wordPtr; |
| 2634 |
int i, concatItems; |
| 2635 |
|
| 2636 |
/* |
| 2637 |
* If the expression is a single word that doesn't require substitutions, |
| 2638 |
* just compile its string into inline instructions. |
| 2639 |
*/ |
| 2640 |
|
| 2641 |
if ((numWords == 1) && (tokenPtr->type == TCL_TOKEN_SIMPLE_WORD)) { |
| 2642 |
TclCompileExpr(interp, tokenPtr[1].start,tokenPtr[1].size, envPtr, 1); |
| 2643 |
return; |
| 2644 |
} |
| 2645 |
|
| 2646 |
/* |
| 2647 |
* Emit code to call the expr command proc at runtime. Concatenate the |
| 2648 |
* (already substituted once) expr tokens with a space between each. |
| 2649 |
*/ |
| 2650 |
|
| 2651 |
wordPtr = tokenPtr; |
| 2652 |
for (i = 0; i < numWords; i++) { |
| 2653 |
CompileTokens(envPtr, wordPtr, interp); |
| 2654 |
if (i < (numWords - 1)) { |
| 2655 |
PushStringLiteral(envPtr, " "); |
| 2656 |
} |
| 2657 |
wordPtr += wordPtr->numComponents + 1; |
| 2658 |
} |
| 2659 |
concatItems = 2*numWords - 1; |
| 2660 |
while (concatItems > 255) { |
| 2661 |
TclEmitInstInt1(INST_STR_CONCAT1, 255, envPtr); |
| 2662 |
concatItems -= 254; |
| 2663 |
} |
| 2664 |
if (concatItems > 1) { |
| 2665 |
TclEmitInstInt1(INST_STR_CONCAT1, concatItems, envPtr); |
| 2666 |
} |
| 2667 |
TclEmitOpcode(INST_EXPR_STK, envPtr); |
| 2668 |
} |
| 2669 |
|
| 2670 |
/* |
| 2671 |
*---------------------------------------------------------------------- |
| 2672 |
* |
| 2673 |
* TclCompileNoOp -- |
| 2674 |
* |
| 2675 |
* Function called to compile no-op's |
| 2676 |
* |
| 2677 |
* Results: |
| 2678 |
* The return value is TCL_OK, indicating successful compilation. |
| 2679 |
* |
| 2680 |
* Side effects: |
| 2681 |
* Instructions are added to envPtr to execute a no-op at runtime. No |
| 2682 |
* result is pushed onto the stack: the compiler has to take care of this |
| 2683 |
* itself if the last compiled command is a NoOp. |
| 2684 |
* |
| 2685 |
*---------------------------------------------------------------------- |
| 2686 |
*/ |
| 2687 |
|
| 2688 |
int |
| 2689 |
TclCompileNoOp( |
| 2690 |
Tcl_Interp *interp, /* Used for error reporting. */ |
| 2691 |
Tcl_Parse *parsePtr, /* Points to a parse structure for the command |
| 2692 |
* created by Tcl_ParseCommand. */ |
| 2693 |
Command *cmdPtr, /* Points to definition of command being |
| 2694 |
* compiled. */ |
| 2695 |
CompileEnv *envPtr) /* Holds resulting instructions. */ |
| 2696 |
{ |
| 2697 |
Tcl_Token *tokenPtr; |
| 2698 |
int i; |
| 2699 |
|
| 2700 |
tokenPtr = parsePtr->tokenPtr; |
| 2701 |
for (i = 1; i < parsePtr->numWords; i++) { |
| 2702 |
tokenPtr = tokenPtr + tokenPtr->numComponents + 1; |
| 2703 |
|
| 2704 |
if (tokenPtr->type != TCL_TOKEN_SIMPLE_WORD) { |
| 2705 |
CompileTokens(envPtr, tokenPtr, interp); |
| 2706 |
TclEmitOpcode(INST_POP, envPtr); |
| 2707 |
} |
| 2708 |
} |
| 2709 |
PushStringLiteral(envPtr, ""); |
| 2710 |
return TCL_OK; |
| 2711 |
} |
| 2712 |
|
| 2713 |
/* |
| 2714 |
*---------------------------------------------------------------------- |
| 2715 |
* |
| 2716 |
* TclInitByteCodeObj -- |
| 2717 |
* |
| 2718 |
* Creates a ByteCode structure and initializes it from a CompileEnv |
| 2719 |
* compilation environment structure. The ByteCode structure is smaller |
| 2720 |
* and contains just that information needed to execute the bytecode |
| 2721 |
* instructions resulting from compiling a Tcl script. The resulting |
| 2722 |
* structure is placed in the specified object. |
| 2723 |
* |
| 2724 |
* Results: |
| 2725 |
* A newly-constructed ByteCode object is stored in the internal |
| 2726 |
* representation of the objPtr. |
| 2727 |
* |
| 2728 |
* Side effects: |
| 2729 |
* A single heap object is allocated to hold the new ByteCode structure |
| 2730 |
* and its code, object, command location, and aux data arrays. Note that |
| 2731 |
* "ownership" (i.e., the pointers to) the Tcl objects and aux data items |
| 2732 |
* will be handed over to the new ByteCode structure from the CompileEnv |
| 2733 |
* structure. |
| 2734 |
* |
| 2735 |
*---------------------------------------------------------------------- |
| 2736 |
*/ |
| 2737 |
|
| 2738 |
void |
| 2739 |
TclInitByteCodeObj( |
| 2740 |
Tcl_Obj *objPtr, /* Points object that should be initialized, |
| 2741 |
* and whose string rep contains the source |
| 2742 |
* code. */ |
| 2743 |
CompileEnv *envPtr)/* Points to the CompileEnv structure from |
| 2744 |
* which to create a ByteCode structure. */ |
| 2745 |
{ |
| 2746 |
ByteCode *codePtr; |
| 2747 |
size_t codeBytes, objArrayBytes, exceptArrayBytes, cmdLocBytes; |
| 2748 |
size_t auxDataArrayBytes, structureSize; |
| 2749 |
unsigned char *p; |
| 2750 |
#ifdef TCL_COMPILE_DEBUG |
| 2751 |
unsigned char *nextPtr; |
| 2752 |
#endif |
| 2753 |
int numLitObjects = envPtr->literalArrayNext; |
| 2754 |
Namespace *namespacePtr; |
| 2755 |
int i, isNew; |
| 2756 |
Interp *iPtr; |
| 2757 |
|
| 2758 |
if (envPtr->iPtr == NULL) { |
| 2759 |
Tcl_Panic("TclInitByteCodeObj() called on uninitialized CompileEnv"); |
| 2760 |
} |
| 2761 |
|
| 2762 |
iPtr = envPtr->iPtr; |
| 2763 |
|
| 2764 |
codeBytes = envPtr->codeNext - envPtr->codeStart; |
| 2765 |
objArrayBytes = envPtr->literalArrayNext * sizeof(Tcl_Obj *); |
| 2766 |
exceptArrayBytes = envPtr->exceptArrayNext * sizeof(ExceptionRange); |
| 2767 |
auxDataArrayBytes = envPtr->auxDataArrayNext * sizeof(AuxData); |
| 2768 |
cmdLocBytes = GetCmdLocEncodingSize(envPtr); |
| 2769 |
|
| 2770 |
/* |
| 2771 |
* Compute the total number of bytes needed for this bytecode. |
| 2772 |
*/ |
| 2773 |
|
| 2774 |
structureSize = sizeof(ByteCode); |
| 2775 |
structureSize += TCL_ALIGN(codeBytes); /* align object array */ |
| 2776 |
structureSize += TCL_ALIGN(objArrayBytes); /* align exc range arr */ |
| 2777 |
structureSize += TCL_ALIGN(exceptArrayBytes); /* align AuxData array */ |
| 2778 |
structureSize += auxDataArrayBytes; |
| 2779 |
structureSize += cmdLocBytes; |
| 2780 |
|
| 2781 |
if (envPtr->iPtr->varFramePtr != NULL) { |
| 2782 |
namespacePtr = envPtr->iPtr->varFramePtr->nsPtr; |
| 2783 |
} else { |
| 2784 |
namespacePtr = envPtr->iPtr->globalNsPtr; |
| 2785 |
} |
| 2786 |
|
| 2787 |
p = (unsigned char *)ckalloc(structureSize); |
| 2788 |
codePtr = (ByteCode *) p; |
| 2789 |
codePtr->interpHandle = TclHandlePreserve(iPtr->handle); |
| 2790 |
codePtr->compileEpoch = iPtr->compileEpoch; |
| 2791 |
codePtr->nsPtr = namespacePtr; |
| 2792 |
codePtr->nsEpoch = namespacePtr->resolverEpoch; |
| 2793 |
codePtr->refCount = 1; |
| 2794 |
if (namespacePtr->compiledVarResProc || iPtr->resolverPtr) { |
| 2795 |
codePtr->flags = TCL_BYTECODE_RESOLVE_VARS; |
| 2796 |
} else { |
| 2797 |
codePtr->flags = 0; |
| 2798 |
} |
| 2799 |
codePtr->source = envPtr->source; |
| 2800 |
codePtr->procPtr = envPtr->procPtr; |
| 2801 |
|
| 2802 |
codePtr->numCommands = envPtr->numCommands; |
| 2803 |
codePtr->numSrcBytes = envPtr->numSrcBytes; |
| 2804 |
codePtr->numCodeBytes = codeBytes; |
| 2805 |
codePtr->numLitObjects = numLitObjects; |
| 2806 |
codePtr->numExceptRanges = envPtr->exceptArrayNext; |
| 2807 |
codePtr->numAuxDataItems = envPtr->auxDataArrayNext; |
| 2808 |
codePtr->numCmdLocBytes = cmdLocBytes; |
| 2809 |
codePtr->maxExceptDepth = envPtr->maxExceptDepth; |
| 2810 |
codePtr->maxStackDepth = envPtr->maxStackDepth; |
| 2811 |
|
| 2812 |
p += sizeof(ByteCode); |
| 2813 |
codePtr->codeStart = p; |
| 2814 |
memcpy(p, envPtr->codeStart, codeBytes); |
| 2815 |
|
| 2816 |
p += TCL_ALIGN(codeBytes); /* align object array */ |
| 2817 |
codePtr->objArrayPtr = (Tcl_Obj **) p; |
| 2818 |
for (i = 0; i < numLitObjects; i++) { |
| 2819 |
Tcl_Obj *fetched = TclFetchLiteral(envPtr, i); |
| 2820 |
|
| 2821 |
if (objPtr == fetched) { |
| 2822 |
/* |
| 2823 |
* Prevent circular reference where the bytecode internalrep of |
| 2824 |
* a value contains a literal which is that same value. |
| 2825 |
* If this is allowed to happen, refcount decrements may not |
| 2826 |
* reach zero, and memory may leak. Bugs 467523, 3357771 |
| 2827 |
* |
| 2828 |
* NOTE: [Bugs 3392070, 3389764] We make a copy based completely |
| 2829 |
* on the string value, and do not call Tcl_DuplicateObj() so we |
| 2830 |
* can be sure we do not have any lingering cycles hiding in |
| 2831 |
* the internalrep. |
| 2832 |
*/ |
| 2833 |
int numBytes; |
| 2834 |
const char *bytes = Tcl_GetStringFromObj(objPtr, &numBytes); |
| 2835 |
|
| 2836 |
codePtr->objArrayPtr[i] = Tcl_NewStringObj(bytes, numBytes); |
| 2837 |
Tcl_IncrRefCount(codePtr->objArrayPtr[i]); |
| 2838 |
TclReleaseLiteral((Tcl_Interp *)iPtr, objPtr); |
| 2839 |
} else { |
| 2840 |
codePtr->objArrayPtr[i] = fetched; |
| 2841 |
} |
| 2842 |
} |
| 2843 |
|
| 2844 |
p += TCL_ALIGN(objArrayBytes); /* align exception range array */ |
| 2845 |
if (exceptArrayBytes > 0) { |
| 2846 |
codePtr->exceptArrayPtr = (ExceptionRange *) p; |
| 2847 |
memcpy(p, envPtr->exceptArrayPtr, exceptArrayBytes); |
| 2848 |
} else { |
| 2849 |
codePtr->exceptArrayPtr = NULL; |
| 2850 |
} |
| 2851 |
|
| 2852 |
p += TCL_ALIGN(exceptArrayBytes); /* align AuxData array */ |
| 2853 |
if (auxDataArrayBytes > 0) { |
| 2854 |
codePtr->auxDataArrayPtr = (AuxData *) p; |
| 2855 |
memcpy(p, envPtr->auxDataArrayPtr, auxDataArrayBytes); |
| 2856 |
} else { |
| 2857 |
codePtr->auxDataArrayPtr = NULL; |
| 2858 |
} |
| 2859 |
|
| 2860 |
p += auxDataArrayBytes; |
| 2861 |
#ifndef TCL_COMPILE_DEBUG |
| 2862 |
EncodeCmdLocMap(envPtr, codePtr, (unsigned char *) p); |
| 2863 |
#else |
| 2864 |
nextPtr = EncodeCmdLocMap(envPtr, codePtr, (unsigned char *) p); |
| 2865 |
if (((size_t)(nextPtr - p)) != cmdLocBytes) { |
| 2866 |
Tcl_Panic("TclInitByteCodeObj: encoded cmd location bytes %lu != expected size %lu", (unsigned long)(nextPtr - p), (unsigned long)cmdLocBytes); |
| 2867 |
} |
| 2868 |
#endif |
| 2869 |
|
| 2870 |
/* |
| 2871 |
* Record various compilation-related statistics about the new ByteCode |
| 2872 |
* structure. Don't include overhead for statistics-related fields. |
| 2873 |
*/ |
| 2874 |
|
| 2875 |
#ifdef TCL_COMPILE_STATS |
| 2876 |
codePtr->structureSize = structureSize |
| 2877 |
- (sizeof(size_t) + sizeof(Tcl_Time)); |
| 2878 |
Tcl_GetTime(&codePtr->createTime); |
| 2879 |
|
| 2880 |
RecordByteCodeStats(codePtr); |
| 2881 |
#endif /* TCL_COMPILE_STATS */ |
| 2882 |
|
| 2883 |
/* |
| 2884 |
* Free the old internal rep then convert the object to a bytecode object |
| 2885 |
* by making its internal rep point to the just compiled ByteCode. |
| 2886 |
*/ |
| 2887 |
|
| 2888 |
TclFreeIntRep(objPtr); |
| 2889 |
objPtr->internalRep.twoPtrValue.ptr1 = codePtr; |
| 2890 |
objPtr->typePtr = &tclByteCodeType; |
| 2891 |
|
| 2892 |
/* |
| 2893 |
* TIP #280. Associate the extended per-word line information with the |
| 2894 |
* byte code object (internal rep), for use with the bc compiler. |
| 2895 |
*/ |
| 2896 |
|
| 2897 |
Tcl_SetHashValue(Tcl_CreateHashEntry(iPtr->lineBCPtr, codePtr, |
| 2898 |
&isNew), envPtr->extCmdMapPtr); |
| 2899 |
envPtr->extCmdMapPtr = NULL; |
| 2900 |
|
| 2901 |
/* We've used up the CompileEnv. Mark as uninitialized. */ |
| 2902 |
envPtr->iPtr = NULL; |
| 2903 |
|
| 2904 |
codePtr->localCachePtr = NULL; |
| 2905 |
} |
| 2906 |
|
| 2907 |
/* |
| 2908 |
*---------------------------------------------------------------------- |
| 2909 |
* |
| 2910 |
* TclFindCompiledLocal -- |
| 2911 |
* |
| 2912 |
* This procedure is called at compile time to look up and optionally |
| 2913 |
* allocate an entry ("slot") for a variable in a procedure's array of |
| 2914 |
* local variables. If the variable's name is NULL, a new temporary |
| 2915 |
* variable is always created. (Such temporary variables can only be |
| 2916 |
* referenced using their slot index.) |
| 2917 |
* |
| 2918 |
* Results: |
| 2919 |
* If create is 0 and the name is non-NULL, then if the variable is |
| 2920 |
* found, the index of its entry in the procedure's array of local |
| 2921 |
* variables is returned; otherwise -1 is returned. If name is NULL, the |
| 2922 |
* index of a new temporary variable is returned. Finally, if create is 1 |
| 2923 |
* and name is non-NULL, the index of a new entry is returned. |
| 2924 |
* |
| 2925 |
* Side effects: |
| 2926 |
* Creates and registers a new local variable if create is 1 and the |
| 2927 |
* variable is unknown, or if the name is NULL. |
| 2928 |
* |
| 2929 |
*---------------------------------------------------------------------- |
| 2930 |
*/ |
| 2931 |
|
| 2932 |
int |
| 2933 |
TclFindCompiledLocal( |
| 2934 |
const char *name, /* Points to first character of the name of a |
| 2935 |
* scalar or array variable. If NULL, a |
| 2936 |
* temporary var should be created. */ |
| 2937 |
int nameBytes, /* Number of bytes in the name. */ |
| 2938 |
int create, /* If 1, allocate a local frame entry for the |
| 2939 |
* variable if it is new. */ |
| 2940 |
CompileEnv *envPtr) /* Points to the current compile environment*/ |
| 2941 |
{ |
| 2942 |
CompiledLocal *localPtr; |
| 2943 |
int localVar = -1; |
| 2944 |
int i; |
| 2945 |
Proc *procPtr; |
| 2946 |
|
| 2947 |
/* |
| 2948 |
* If not creating a temporary, does a local variable of the specified |
| 2949 |
* name already exist? |
| 2950 |
*/ |
| 2951 |
|
| 2952 |
procPtr = envPtr->procPtr; |
| 2953 |
|
| 2954 |
if (procPtr == NULL) { |
| 2955 |
/* |
| 2956 |
* Compiling a non-body script: give it read access to the LVT in the |
| 2957 |
* current localCache |
| 2958 |
*/ |
| 2959 |
|
| 2960 |
LocalCache *cachePtr = envPtr->iPtr->varFramePtr->localCachePtr; |
| 2961 |
const char *localName; |
| 2962 |
Tcl_Obj **varNamePtr; |
| 2963 |
int len; |
| 2964 |
|
| 2965 |
if (!cachePtr || !name) { |
| 2966 |
return -1; |
| 2967 |
} |
| 2968 |
|
| 2969 |
varNamePtr = &cachePtr->varName0; |
| 2970 |
for (i=0; i < cachePtr->numVars; varNamePtr++, i++) { |
| 2971 |
if (*varNamePtr) { |
| 2972 |
localName = Tcl_GetStringFromObj(*varNamePtr, &len); |
| 2973 |
if ((len == nameBytes) && !strncmp(name, localName, len)) { |
| 2974 |
return i; |
| 2975 |
} |
| 2976 |
} |
| 2977 |
} |
| 2978 |
return -1; |
| 2979 |
} |
| 2980 |
|
| 2981 |
if (name != NULL) { |
| 2982 |
int localCt = procPtr->numCompiledLocals; |
| 2983 |
|
| 2984 |
localPtr = procPtr->firstLocalPtr; |
| 2985 |
for (i = 0; i < localCt; i++) { |
| 2986 |
if (!TclIsVarTemporary(localPtr)) { |
| 2987 |
char *localName = localPtr->name; |
| 2988 |
|
| 2989 |
if ((nameBytes == localPtr->nameLength) && |
| 2990 |
(strncmp(name, localName, nameBytes) == 0)) { |
| 2991 |
return i; |
| 2992 |
} |
| 2993 |
} |
| 2994 |
localPtr = localPtr->nextPtr; |
| 2995 |
} |
| 2996 |
} |
| 2997 |
|
| 2998 |
/* |
| 2999 |
* Create a new variable if appropriate. |
| 3000 |
*/ |
| 3001 |
|
| 3002 |
if (create || (name == NULL)) { |
| 3003 |
localVar = procPtr->numCompiledLocals; |
| 3004 |
localPtr = (CompiledLocal *)ckalloc(TclOffset(CompiledLocal, name) + 1U + nameBytes); |
| 3005 |
if (procPtr->firstLocalPtr == NULL) { |
| 3006 |
procPtr->firstLocalPtr = procPtr->lastLocalPtr = localPtr; |
| 3007 |
} else { |
| 3008 |
procPtr->lastLocalPtr->nextPtr = localPtr; |
| 3009 |
procPtr->lastLocalPtr = localPtr; |
| 3010 |
} |
| 3011 |
localPtr->nextPtr = NULL; |
| 3012 |
localPtr->nameLength = nameBytes; |
| 3013 |
localPtr->frameIndex = localVar; |
| 3014 |
localPtr->flags = 0; |
| 3015 |
if (name == NULL) { |
| 3016 |
localPtr->flags |= VAR_TEMPORARY; |
| 3017 |
} |
| 3018 |
localPtr->defValuePtr = NULL; |
| 3019 |
localPtr->resolveInfo = NULL; |
| 3020 |
|
| 3021 |
if (name != NULL) { |
| 3022 |
memcpy(localPtr->name, name, nameBytes); |
| 3023 |
} |
| 3024 |
localPtr->name[nameBytes] = '\0'; |
| 3025 |
procPtr->numCompiledLocals++; |
| 3026 |
} |
| 3027 |
return localVar; |
| 3028 |
} |
| 3029 |
|
| 3030 |
/* |
| 3031 |
*---------------------------------------------------------------------- |
| 3032 |
* |
| 3033 |
* TclExpandCodeArray -- |
| 3034 |
* |
| 3035 |
* Procedure that uses malloc to allocate more storage for a CompileEnv's |
| 3036 |
* code array. |
| 3037 |
* |
| 3038 |
* Results: |
| 3039 |
* None. |
| 3040 |
* |
| 3041 |
* Side effects: |
| 3042 |
* The byte code array in *envPtr is reallocated to a new array of double |
| 3043 |
* the size, and if envPtr->mallocedCodeArray is non-zero the old array |
| 3044 |
* is freed. Byte codes are copied from the old array to the new one. |
| 3045 |
* |
| 3046 |
*---------------------------------------------------------------------- |
| 3047 |
*/ |
| 3048 |
|
| 3049 |
void |
| 3050 |
TclExpandCodeArray( |
| 3051 |
void *envArgPtr) /* Points to the CompileEnv whose code array |
| 3052 |
* must be enlarged. */ |
| 3053 |
{ |
| 3054 |
CompileEnv *envPtr = (CompileEnv *)envArgPtr; |
| 3055 |
/* The CompileEnv containing the code array to |
| 3056 |
* be doubled in size. */ |
| 3057 |
|
| 3058 |
/* |
| 3059 |
* envPtr->codeNext is equal to envPtr->codeEnd. The currently defined |
| 3060 |
* code bytes are stored between envPtr->codeStart and envPtr->codeNext-1 |
| 3061 |
* [inclusive]. |
| 3062 |
*/ |
| 3063 |
|
| 3064 |
size_t currBytes = envPtr->codeNext - envPtr->codeStart; |
| 3065 |
size_t newBytes = 2 * (envPtr->codeEnd - envPtr->codeStart); |
| 3066 |
|
| 3067 |
if (envPtr->mallocedCodeArray) { |
| 3068 |
envPtr->codeStart = (unsigned char *)ckrealloc(envPtr->codeStart, newBytes); |
| 3069 |
} else { |
| 3070 |
/* |
| 3071 |
* envPtr->codeStart isn't a ckalloc'd pointer, so we must code a |
| 3072 |
* ckrealloc equivalent for ourselves. |
| 3073 |
*/ |
| 3074 |
|
| 3075 |
unsigned char *newPtr = (unsigned char *)ckalloc(newBytes); |
| 3076 |
|
| 3077 |
memcpy(newPtr, envPtr->codeStart, currBytes); |
| 3078 |
envPtr->codeStart = newPtr; |
| 3079 |
envPtr->mallocedCodeArray = 1; |
| 3080 |
} |
| 3081 |
|
| 3082 |
envPtr->codeNext = envPtr->codeStart + currBytes; |
| 3083 |
envPtr->codeEnd = envPtr->codeStart + newBytes; |
| 3084 |
} |
| 3085 |
|
| 3086 |
/* |
| 3087 |
*---------------------------------------------------------------------- |
| 3088 |
* |
| 3089 |
* EnterCmdStartData -- |
| 3090 |
* |
| 3091 |
* Registers the starting source and bytecode location of a command. This |
| 3092 |
* information is used at runtime to map between instruction pc and |
| 3093 |
* source locations. |
| 3094 |
* |
| 3095 |
* Results: |
| 3096 |
* None. |
| 3097 |
* |
| 3098 |
* Side effects: |
| 3099 |
* Inserts source and code location information into the compilation |
| 3100 |
* environment envPtr for the command at index cmdIndex. The compilation |
| 3101 |
* environment's CmdLocation array is grown if necessary. |
| 3102 |
* |
| 3103 |
*---------------------------------------------------------------------- |
| 3104 |
*/ |
| 3105 |
|
| 3106 |
static void |
| 3107 |
EnterCmdStartData( |
| 3108 |
CompileEnv *envPtr, /* Points to the compilation environment |
| 3109 |
* structure in which to enter command |
| 3110 |
* location information. */ |
| 3111 |
int cmdIndex, /* Index of the command whose start data is |
| 3112 |
* being set. */ |
| 3113 |
int srcOffset, /* Offset of first char of the command. */ |
| 3114 |
int codeOffset) /* Offset of first byte of command code. */ |
| 3115 |
{ |
| 3116 |
CmdLocation *cmdLocPtr; |
| 3117 |
|
| 3118 |
if ((cmdIndex < 0) || (cmdIndex >= envPtr->numCommands)) { |
| 3119 |
Tcl_Panic("EnterCmdStartData: bad command index %d", cmdIndex); |
| 3120 |
} |
| 3121 |
|
| 3122 |
if (cmdIndex >= envPtr->cmdMapEnd) { |
| 3123 |
/* |
| 3124 |
* Expand the command location array by allocating more storage from |
| 3125 |
* the heap. The currently allocated CmdLocation entries are stored |
| 3126 |
* from cmdMapPtr[0] up to cmdMapPtr[envPtr->cmdMapEnd] (inclusive). |
| 3127 |
*/ |
| 3128 |
|
| 3129 |
size_t currElems = envPtr->cmdMapEnd; |
| 3130 |
size_t newElems = 2 * currElems; |
| 3131 |
size_t currBytes = currElems * sizeof(CmdLocation); |
| 3132 |
size_t newBytes = newElems * sizeof(CmdLocation); |
| 3133 |
|
| 3134 |
if (envPtr->mallocedCmdMap) { |
| 3135 |
envPtr->cmdMapPtr = (CmdLocation *)ckrealloc(envPtr->cmdMapPtr, newBytes); |
| 3136 |
} else { |
| 3137 |
/* |
| 3138 |
* envPtr->cmdMapPtr isn't a ckalloc'd pointer, so we must code a |
| 3139 |
* ckrealloc equivalent for ourselves. |
| 3140 |
*/ |
| 3141 |
|
| 3142 |
CmdLocation *newPtr = (CmdLocation *)ckalloc(newBytes); |
| 3143 |
|
| 3144 |
memcpy(newPtr, envPtr->cmdMapPtr, currBytes); |
| 3145 |
envPtr->cmdMapPtr = newPtr; |
| 3146 |
envPtr->mallocedCmdMap = 1; |
| 3147 |
} |
| 3148 |
envPtr->cmdMapEnd = newElems; |
| 3149 |
} |
| 3150 |
|
| 3151 |
if (cmdIndex > 0) { |
| 3152 |
if (codeOffset < envPtr->cmdMapPtr[cmdIndex-1].codeOffset) { |
| 3153 |
Tcl_Panic("EnterCmdStartData: cmd map not sorted by code offset"); |
| 3154 |
} |
| 3155 |
} |
| 3156 |
|
| 3157 |
cmdLocPtr = &envPtr->cmdMapPtr[cmdIndex]; |
| 3158 |
cmdLocPtr->codeOffset = codeOffset; |
| 3159 |
cmdLocPtr->srcOffset = srcOffset; |
| 3160 |
cmdLocPtr->numSrcBytes = -1; |
| 3161 |
cmdLocPtr->numCodeBytes = -1; |
| 3162 |
} |
| 3163 |
|
| 3164 |
/* |
| 3165 |
*---------------------------------------------------------------------- |
| 3166 |
* |
| 3167 |
* EnterCmdExtentData -- |
| 3168 |
* |
| 3169 |
* Registers the source and bytecode length for a command. This |
| 3170 |
* information is used at runtime to map between instruction pc and |
| 3171 |
* source locations. |
| 3172 |
* |
| 3173 |
* Results: |
| 3174 |
* None. |
| 3175 |
* |
| 3176 |
* Side effects: |
| 3177 |
* Inserts source and code length information into the compilation |
| 3178 |
* environment envPtr for the command at index cmdIndex. Starting source |
| 3179 |
* and bytecode information for the command must already have been |
| 3180 |
* registered. |
| 3181 |
* |
| 3182 |
*---------------------------------------------------------------------- |
| 3183 |
*/ |
| 3184 |
|
| 3185 |
static void |
| 3186 |
EnterCmdExtentData( |
| 3187 |
CompileEnv *envPtr, /* Points to the compilation environment |
| 3188 |
* structure in which to enter command |
| 3189 |
* location information. */ |
| 3190 |
int cmdIndex, /* Index of the command whose source and code |
| 3191 |
* length data is being set. */ |
| 3192 |
int numSrcBytes, /* Number of command source chars. */ |
| 3193 |
int numCodeBytes) /* Offset of last byte of command code. */ |
| 3194 |
{ |
| 3195 |
CmdLocation *cmdLocPtr; |
| 3196 |
|
| 3197 |
if ((cmdIndex < 0) || (cmdIndex >= envPtr->numCommands)) { |
| 3198 |
Tcl_Panic("EnterCmdExtentData: bad command index %d", cmdIndex); |
| 3199 |
} |
| 3200 |
|
| 3201 |
if (cmdIndex > envPtr->cmdMapEnd) { |
| 3202 |
Tcl_Panic("EnterCmdExtentData: missing start data for command %d", |
| 3203 |
cmdIndex); |
| 3204 |
} |
| 3205 |
|
| 3206 |
cmdLocPtr = &envPtr->cmdMapPtr[cmdIndex]; |
| 3207 |
cmdLocPtr->numSrcBytes = numSrcBytes; |
| 3208 |
cmdLocPtr->numCodeBytes = numCodeBytes; |
| 3209 |
} |
| 3210 |
|
| 3211 |
/* |
| 3212 |
*---------------------------------------------------------------------- |
| 3213 |
* TIP #280 |
| 3214 |
* |
| 3215 |
* EnterCmdWordData -- |
| 3216 |
* |
| 3217 |
* Registers the lines for the words of a command. This information is |
| 3218 |
* used at runtime by 'info frame'. |
| 3219 |
* |
| 3220 |
* Results: |
| 3221 |
* None. |
| 3222 |
* |
| 3223 |
* Side effects: |
| 3224 |
* Inserts word location information into the compilation environment |
| 3225 |
* envPtr for the command at index cmdIndex. The compilation |
| 3226 |
* environment's ExtCmdLoc.ECL array is grown if necessary. |
| 3227 |
* |
| 3228 |
*---------------------------------------------------------------------- |
| 3229 |
*/ |
| 3230 |
|
| 3231 |
static void |
| 3232 |
EnterCmdWordData( |
| 3233 |
ExtCmdLoc *eclPtr, /* Points to the map environment structure in |
| 3234 |
* which to enter command location |
| 3235 |
* information. */ |
| 3236 |
int srcOffset, /* Offset of first char of the command. */ |
| 3237 |
Tcl_Token *tokenPtr, |
| 3238 |
const char *cmd, |
| 3239 |
int len, |
| 3240 |
int numWords, |
| 3241 |
int line, |
| 3242 |
int *clNext, |
| 3243 |
int **wlines, |
| 3244 |
CompileEnv *envPtr) |
| 3245 |
{ |
| 3246 |
ECL *ePtr; |
| 3247 |
const char *last; |
| 3248 |
int wordIdx, wordLine, *wwlines, *wordNext; |
| 3249 |
|
| 3250 |
if (eclPtr->nuloc >= eclPtr->nloc) { |
| 3251 |
/* |
| 3252 |
* Expand the ECL array by allocating more storage from the heap. The |
| 3253 |
* currently allocated ECL entries are stored from eclPtr->loc[0] up |
| 3254 |
* to eclPtr->loc[eclPtr->nuloc-1] (inclusive). |
| 3255 |
*/ |
| 3256 |
|
| 3257 |
size_t currElems = eclPtr->nloc; |
| 3258 |
size_t newElems = (currElems ? 2*currElems : 1); |
| 3259 |
size_t newBytes = newElems * sizeof(ECL); |
| 3260 |
|
| 3261 |
eclPtr->loc = (ECL *)ckrealloc(eclPtr->loc, newBytes); |
| 3262 |
eclPtr->nloc = newElems; |
| 3263 |
} |
| 3264 |
|
| 3265 |
ePtr = &eclPtr->loc[eclPtr->nuloc]; |
| 3266 |
ePtr->srcOffset = srcOffset; |
| 3267 |
ePtr->line = (int *)ckalloc(numWords * sizeof(int)); |
| 3268 |
ePtr->next = (int **)ckalloc(numWords * sizeof(int *)); |
| 3269 |
ePtr->nline = numWords; |
| 3270 |
wwlines = (int *)ckalloc(numWords * sizeof(int)); |
| 3271 |
|
| 3272 |
last = cmd; |
| 3273 |
wordLine = line; |
| 3274 |
wordNext = clNext; |
| 3275 |
for (wordIdx=0 ; wordIdx<numWords; |
| 3276 |
wordIdx++, tokenPtr += tokenPtr->numComponents + 1) { |
| 3277 |
TclAdvanceLines(&wordLine, last, tokenPtr->start); |
| 3278 |
TclAdvanceContinuations(&wordLine, &wordNext, |
| 3279 |
tokenPtr->start - envPtr->source); |
| 3280 |
/* See Ticket 4b61afd660 */ |
| 3281 |
wwlines[wordIdx] = |
| 3282 |
((wordIdx == 0) || TclWordKnownAtCompileTime(tokenPtr, NULL)) |
| 3283 |
? wordLine : -1; |
| 3284 |
ePtr->line[wordIdx] = wordLine; |
| 3285 |
ePtr->next[wordIdx] = wordNext; |
| 3286 |
last = tokenPtr->start; |
| 3287 |
} |
| 3288 |
|
| 3289 |
*wlines = wwlines; |
| 3290 |
eclPtr->nuloc ++; |
| 3291 |
} |
| 3292 |
|
| 3293 |
/* |
| 3294 |
*---------------------------------------------------------------------- |
| 3295 |
* |
| 3296 |
* TclCreateExceptRange -- |
| 3297 |
* |
| 3298 |
* Procedure that allocates and initializes a new ExceptionRange |
| 3299 |
* structure of the specified kind in a CompileEnv. |
| 3300 |
* |
| 3301 |
* Results: |
| 3302 |
* Returns the index for the newly created ExceptionRange. |
| 3303 |
* |
| 3304 |
* Side effects: |
| 3305 |
* If there is not enough room in the CompileEnv's ExceptionRange array, |
| 3306 |
* the array in expanded: a new array of double the size is allocated, if |
| 3307 |
* envPtr->mallocedExceptArray is non-zero the old array is freed, and |
| 3308 |
* ExceptionRange entries are copied from the old array to the new one. |
| 3309 |
* |
| 3310 |
*---------------------------------------------------------------------- |
| 3311 |
*/ |
| 3312 |
|
| 3313 |
int |
| 3314 |
TclCreateExceptRange( |
| 3315 |
ExceptionRangeType type, /* The kind of ExceptionRange desired. */ |
| 3316 |
CompileEnv *envPtr)/* Points to CompileEnv for which to create a |
| 3317 |
* new ExceptionRange structure. */ |
| 3318 |
{ |
| 3319 |
ExceptionRange *rangePtr; |
| 3320 |
ExceptionAux *auxPtr; |
| 3321 |
int index = envPtr->exceptArrayNext; |
| 3322 |
|
| 3323 |
if (index >= envPtr->exceptArrayEnd) { |
| 3324 |
/* |
| 3325 |
* Expand the ExceptionRange array. The currently allocated entries |
| 3326 |
* are stored between elements 0 and (envPtr->exceptArrayNext - 1) |
| 3327 |
* [inclusive]. |
| 3328 |
*/ |
| 3329 |
|
| 3330 |
size_t currBytes = |
| 3331 |
envPtr->exceptArrayNext * sizeof(ExceptionRange); |
| 3332 |
size_t currBytes2 = envPtr->exceptArrayNext * sizeof(ExceptionAux); |
| 3333 |
int newElems = 2*envPtr->exceptArrayEnd; |
| 3334 |
size_t newBytes = newElems * sizeof(ExceptionRange); |
| 3335 |
size_t newBytes2 = newElems * sizeof(ExceptionAux); |
| 3336 |
|
| 3337 |
if (envPtr->mallocedExceptArray) { |
| 3338 |
envPtr->exceptArrayPtr = |
| 3339 |
(ExceptionRange *)ckrealloc(envPtr->exceptArrayPtr, newBytes); |
| 3340 |
envPtr->exceptAuxArrayPtr = |
| 3341 |
(ExceptionAux *)ckrealloc(envPtr->exceptAuxArrayPtr, newBytes2); |
| 3342 |
} else { |
| 3343 |
/* |
| 3344 |
* envPtr->exceptArrayPtr isn't a ckalloc'd pointer, so we must |
| 3345 |
* code a ckrealloc equivalent for ourselves. |
| 3346 |
*/ |
| 3347 |
|
| 3348 |
ExceptionRange *newPtr = (ExceptionRange *)ckalloc(newBytes); |
| 3349 |
ExceptionAux *newPtr2 = (ExceptionAux *)ckalloc(newBytes2); |
| 3350 |
|
| 3351 |
memcpy(newPtr, envPtr->exceptArrayPtr, currBytes); |
| 3352 |
memcpy(newPtr2, envPtr->exceptAuxArrayPtr, currBytes2); |
| 3353 |
envPtr->exceptArrayPtr = newPtr; |
| 3354 |
envPtr->exceptAuxArrayPtr = newPtr2; |
| 3355 |
envPtr->mallocedExceptArray = 1; |
| 3356 |
} |
| 3357 |
envPtr->exceptArrayEnd = newElems; |
| 3358 |
} |
| 3359 |
envPtr->exceptArrayNext++; |
| 3360 |
|
| 3361 |
rangePtr = &envPtr->exceptArrayPtr[index]; |
| 3362 |
rangePtr->type = type; |
| 3363 |
rangePtr->nestingLevel = envPtr->exceptDepth; |
| 3364 |
rangePtr->codeOffset = -1; |
| 3365 |
rangePtr->numCodeBytes = -1; |
| 3366 |
rangePtr->breakOffset = -1; |
| 3367 |
rangePtr->continueOffset = -1; |
| 3368 |
rangePtr->catchOffset = -1; |
| 3369 |
auxPtr = &envPtr->exceptAuxArrayPtr[index]; |
| 3370 |
auxPtr->supportsContinue = 1; |
| 3371 |
auxPtr->stackDepth = envPtr->currStackDepth; |
| 3372 |
auxPtr->expandTarget = envPtr->expandCount; |
| 3373 |
auxPtr->expandTargetDepth = -1; |
| 3374 |
auxPtr->numBreakTargets = 0; |
| 3375 |
auxPtr->breakTargets = NULL; |
| 3376 |
auxPtr->allocBreakTargets = 0; |
| 3377 |
auxPtr->numContinueTargets = 0; |
| 3378 |
auxPtr->continueTargets = NULL; |
| 3379 |
auxPtr->allocContinueTargets = 0; |
| 3380 |
return index; |
| 3381 |
} |
| 3382 |
|
| 3383 |
/* |
| 3384 |
* --------------------------------------------------------------------- |
| 3385 |
* |
| 3386 |
* TclGetInnermostExceptionRange -- |
| 3387 |
* |
| 3388 |
* Returns the innermost exception range that covers the current code |
| 3389 |
* creation point, and (optionally) the stack depth that is expected at |
| 3390 |
* that point. Relies on the fact that the range has a numCodeBytes = -1 |
| 3391 |
* when it is being populated and that inner ranges come after outer |
| 3392 |
* ranges. |
| 3393 |
* |
| 3394 |
* --------------------------------------------------------------------- |
| 3395 |
*/ |
| 3396 |
|
| 3397 |
ExceptionRange * |
| 3398 |
TclGetInnermostExceptionRange( |
| 3399 |
CompileEnv *envPtr, |
| 3400 |
int returnCode, |
| 3401 |
ExceptionAux **auxPtrPtr) |
| 3402 |
{ |
| 3403 |
int i = envPtr->exceptArrayNext; |
| 3404 |
ExceptionRange *rangePtr = envPtr->exceptArrayPtr + i; |
| 3405 |
|
| 3406 |
while (i > 0) { |
| 3407 |
rangePtr--; i--; |
| 3408 |
|
| 3409 |
if (CurrentOffset(envPtr) >= rangePtr->codeOffset && |
| 3410 |
(rangePtr->numCodeBytes == -1 || CurrentOffset(envPtr) < |
| 3411 |
rangePtr->codeOffset+rangePtr->numCodeBytes) && |
| 3412 |
(returnCode != TCL_CONTINUE || |
| 3413 |
envPtr->exceptAuxArrayPtr[i].supportsContinue)) { |
| 3414 |
|
| 3415 |
if (auxPtrPtr) { |
| 3416 |
*auxPtrPtr = envPtr->exceptAuxArrayPtr + i; |
| 3417 |
} |
| 3418 |
return rangePtr; |
| 3419 |
} |
| 3420 |
} |
| 3421 |
return NULL; |
| 3422 |
} |
| 3423 |
|
| 3424 |
/* |
| 3425 |
* --------------------------------------------------------------------- |
| 3426 |
* |
| 3427 |
* TclAddLoopBreakFixup, TclAddLoopContinueFixup -- |
| 3428 |
* |
| 3429 |
* Adds a place that wants to break/continue to the loop exception range |
| 3430 |
* tracking that will be fixed up once the loop can be finalized. These |
| 3431 |
* functions generate an INST_JUMP4 that is fixed up during the |
| 3432 |
* loop finalization. |
| 3433 |
* |
| 3434 |
* --------------------------------------------------------------------- |
| 3435 |
*/ |
| 3436 |
|
| 3437 |
void |
| 3438 |
TclAddLoopBreakFixup( |
| 3439 |
CompileEnv *envPtr, |
| 3440 |
ExceptionAux *auxPtr) |
| 3441 |
{ |
| 3442 |
int range = auxPtr - envPtr->exceptAuxArrayPtr; |
| 3443 |
|
| 3444 |
if (envPtr->exceptArrayPtr[range].type != LOOP_EXCEPTION_RANGE) { |
| 3445 |
Tcl_Panic("trying to add 'break' fixup to full exception range"); |
| 3446 |
} |
| 3447 |
|
| 3448 |
if (++auxPtr->numBreakTargets > auxPtr->allocBreakTargets) { |
| 3449 |
auxPtr->allocBreakTargets *= 2; |
| 3450 |
auxPtr->allocBreakTargets += 2; |
| 3451 |
if (auxPtr->breakTargets) { |
| 3452 |
auxPtr->breakTargets = (unsigned int *)ckrealloc(auxPtr->breakTargets, |
| 3453 |
sizeof(int) * auxPtr->allocBreakTargets); |
| 3454 |
} else { |
| 3455 |
auxPtr->breakTargets = |
| 3456 |
(unsigned int *)ckalloc(sizeof(int) * auxPtr->allocBreakTargets); |
| 3457 |
} |
| 3458 |
} |
| 3459 |
auxPtr->breakTargets[auxPtr->numBreakTargets - 1] = CurrentOffset(envPtr); |
| 3460 |
TclEmitInstInt4(INST_JUMP4, 0, envPtr); |
| 3461 |
} |
| 3462 |
|
| 3463 |
void |
| 3464 |
TclAddLoopContinueFixup( |
| 3465 |
CompileEnv *envPtr, |
| 3466 |
ExceptionAux *auxPtr) |
| 3467 |
{ |
| 3468 |
int range = auxPtr - envPtr->exceptAuxArrayPtr; |
| 3469 |
|
| 3470 |
if (envPtr->exceptArrayPtr[range].type != LOOP_EXCEPTION_RANGE) { |
| 3471 |
Tcl_Panic("trying to add 'continue' fixup to full exception range"); |
| 3472 |
} |
| 3473 |
|
| 3474 |
if (++auxPtr->numContinueTargets > auxPtr->allocContinueTargets) { |
| 3475 |
auxPtr->allocContinueTargets *= 2; |
| 3476 |
auxPtr->allocContinueTargets += 2; |
| 3477 |
if (auxPtr->continueTargets) { |
| 3478 |
auxPtr->continueTargets = (unsigned int *)ckrealloc(auxPtr->continueTargets, |
| 3479 |
sizeof(int) * auxPtr->allocContinueTargets); |
| 3480 |
} else { |
| 3481 |
auxPtr->continueTargets = |
| 3482 |
(unsigned int *)ckalloc(sizeof(int) * auxPtr->allocContinueTargets); |
| 3483 |
} |
| 3484 |
} |
| 3485 |
auxPtr->continueTargets[auxPtr->numContinueTargets - 1] = |
| 3486 |
CurrentOffset(envPtr); |
| 3487 |
TclEmitInstInt4(INST_JUMP4, 0, envPtr); |
| 3488 |
} |
| 3489 |
|
| 3490 |
/* |
| 3491 |
* --------------------------------------------------------------------- |
| 3492 |
* |
| 3493 |
* TclCleanupStackForBreakContinue -- |
| 3494 |
* |
| 3495 |
* Ditch the extra elements from the auxiliary stack and the main stack. |
| 3496 |
* How to do this exactly depends on whether there are any elements on |
| 3497 |
* the auxiliary stack to pop. |
| 3498 |
* |
| 3499 |
* --------------------------------------------------------------------- |
| 3500 |
*/ |
| 3501 |
|
| 3502 |
void |
| 3503 |
TclCleanupStackForBreakContinue( |
| 3504 |
CompileEnv *envPtr, |
| 3505 |
ExceptionAux *auxPtr) |
| 3506 |
{ |
| 3507 |
int savedStackDepth = envPtr->currStackDepth; |
| 3508 |
int toPop = envPtr->expandCount - auxPtr->expandTarget; |
| 3509 |
|
| 3510 |
if (toPop > 0) { |
| 3511 |
while (toPop --> 0) { |
| 3512 |
TclEmitOpcode(INST_EXPAND_DROP, envPtr); |
| 3513 |
} |
| 3514 |
TclAdjustStackDepth(auxPtr->expandTargetDepth - envPtr->currStackDepth, |
| 3515 |
envPtr); |
| 3516 |
envPtr->currStackDepth = auxPtr->expandTargetDepth; |
| 3517 |
} |
| 3518 |
toPop = envPtr->currStackDepth - auxPtr->stackDepth; |
| 3519 |
while (toPop --> 0) { |
| 3520 |
TclEmitOpcode(INST_POP, envPtr); |
| 3521 |
} |
| 3522 |
envPtr->currStackDepth = savedStackDepth; |
| 3523 |
} |
| 3524 |
|
| 3525 |
/* |
| 3526 |
* --------------------------------------------------------------------- |
| 3527 |
* |
| 3528 |
* StartExpanding -- |
| 3529 |
* |
| 3530 |
* Pushes an INST_EXPAND_START and does some additional housekeeping so |
| 3531 |
* that the [break] and [continue] compilers can use an exception-free |
| 3532 |
* issue to discard it. |
| 3533 |
* |
| 3534 |
* --------------------------------------------------------------------- |
| 3535 |
*/ |
| 3536 |
|
| 3537 |
static void |
| 3538 |
StartExpanding( |
| 3539 |
CompileEnv *envPtr) |
| 3540 |
{ |
| 3541 |
int i; |
| 3542 |
|
| 3543 |
TclEmitOpcode(INST_EXPAND_START, envPtr); |
| 3544 |
|
| 3545 |
/* |
| 3546 |
* Update inner exception ranges with information about the environment |
| 3547 |
* where this expansion started. |
| 3548 |
*/ |
| 3549 |
|
| 3550 |
for (i=0 ; i<envPtr->exceptArrayNext ; i++) { |
| 3551 |
ExceptionRange *rangePtr = &envPtr->exceptArrayPtr[i]; |
| 3552 |
ExceptionAux *auxPtr = &envPtr->exceptAuxArrayPtr[i]; |
| 3553 |
|
| 3554 |
/* |
| 3555 |
* Ignore loops unless they're still being built. |
| 3556 |
*/ |
| 3557 |
|
| 3558 |
if (rangePtr->codeOffset > CurrentOffset(envPtr)) { |
| 3559 |
continue; |
| 3560 |
} |
| 3561 |
if (rangePtr->numCodeBytes != -1) { |
| 3562 |
continue; |
| 3563 |
} |
| 3564 |
|
| 3565 |
/* |
| 3566 |
* Adequate condition: further out loops and further in exceptions |
| 3567 |
* don't actually need this information. |
| 3568 |
*/ |
| 3569 |
|
| 3570 |
if (auxPtr->expandTarget == envPtr->expandCount) { |
| 3571 |
auxPtr->expandTargetDepth = envPtr->currStackDepth; |
| 3572 |
} |
| 3573 |
} |
| 3574 |
|
| 3575 |
/* |
| 3576 |
* There's now one more expansion being processed on the auxiliary stack. |
| 3577 |
*/ |
| 3578 |
|
| 3579 |
envPtr->expandCount++; |
| 3580 |
} |
| 3581 |
|
| 3582 |
/* |
| 3583 |
* --------------------------------------------------------------------- |
| 3584 |
* |
| 3585 |
* TclFinalizeLoopExceptionRange -- |
| 3586 |
* |
| 3587 |
* Finalizes a loop exception range, binding the registered [break] and |
| 3588 |
* [continue] implementations so that they jump to the correct place. |
| 3589 |
* Note that this must only be called after *all* the exception range |
| 3590 |
* target offsets have been set. |
| 3591 |
* |
| 3592 |
* --------------------------------------------------------------------- |
| 3593 |
*/ |
| 3594 |
|
| 3595 |
void |
| 3596 |
TclFinalizeLoopExceptionRange( |
| 3597 |
CompileEnv *envPtr, |
| 3598 |
int range) |
| 3599 |
{ |
| 3600 |
ExceptionRange *rangePtr = &envPtr->exceptArrayPtr[range]; |
| 3601 |
ExceptionAux *auxPtr = &envPtr->exceptAuxArrayPtr[range]; |
| 3602 |
int i, offset; |
| 3603 |
unsigned char *site; |
| 3604 |
|
| 3605 |
if (rangePtr->type != LOOP_EXCEPTION_RANGE) { |
| 3606 |
Tcl_Panic("trying to finalize a loop exception range"); |
| 3607 |
} |
| 3608 |
|
| 3609 |
/* |
| 3610 |
* Do the jump fixups. Note that these are always issued as INST_JUMP4 so |
| 3611 |
* there is no need to fuss around with updating code offsets. |
| 3612 |
*/ |
| 3613 |
|
| 3614 |
for (i=0 ; i<auxPtr->numBreakTargets ; i++) { |
| 3615 |
site = envPtr->codeStart + auxPtr->breakTargets[i]; |
| 3616 |
offset = rangePtr->breakOffset - auxPtr->breakTargets[i]; |
| 3617 |
TclUpdateInstInt4AtPc(INST_JUMP4, offset, site); |
| 3618 |
} |
| 3619 |
for (i=0 ; i<auxPtr->numContinueTargets ; i++) { |
| 3620 |
site = envPtr->codeStart + auxPtr->continueTargets[i]; |
| 3621 |
if (rangePtr->continueOffset == -1) { |
| 3622 |
int j; |
| 3623 |
|
| 3624 |
/* |
| 3625 |
* WTF? Can't bind, so revert to an INST_CONTINUE. Not enough |
| 3626 |
* space to do anything else. |
| 3627 |
*/ |
| 3628 |
|
| 3629 |
*site = INST_CONTINUE; |
| 3630 |
for (j=0 ; j<4 ; j++) { |
| 3631 |
*++site = INST_NOP; |
| 3632 |
} |
| 3633 |
} else { |
| 3634 |
offset = rangePtr->continueOffset - auxPtr->continueTargets[i]; |
| 3635 |
TclUpdateInstInt4AtPc(INST_JUMP4, offset, site); |
| 3636 |
} |
| 3637 |
} |
| 3638 |
|
| 3639 |
/* |
| 3640 |
* Drop the arrays we were holding the only reference to. |
| 3641 |
*/ |
| 3642 |
|
| 3643 |
if (auxPtr->breakTargets) { |
| 3644 |
ckfree(auxPtr->breakTargets); |
| 3645 |
auxPtr->breakTargets = NULL; |
| 3646 |
auxPtr->numBreakTargets = 0; |
| 3647 |
} |
| 3648 |
if (auxPtr->continueTargets) { |
| 3649 |
ckfree(auxPtr->continueTargets); |
| 3650 |
auxPtr->continueTargets = NULL; |
| 3651 |
auxPtr->numContinueTargets = 0; |
| 3652 |
} |
| 3653 |
} |
| 3654 |
|
| 3655 |
/* |
| 3656 |
*---------------------------------------------------------------------- |
| 3657 |
* |
| 3658 |
* TclCreateAuxData -- |
| 3659 |
* |
| 3660 |
* Procedure that allocates and initializes a new AuxData structure in a |
| 3661 |
* CompileEnv's array of compilation auxiliary data records. These |
| 3662 |
* AuxData records hold information created during compilation by |
| 3663 |
* CompileProcs and used by instructions during execution. |
| 3664 |
* |
| 3665 |
* Results: |
| 3666 |
* Returns the index for the newly created AuxData structure. |
| 3667 |
* |
| 3668 |
* Side effects: |
| 3669 |
* If there is not enough room in the CompileEnv's AuxData array, the |
| 3670 |
* AuxData array in expanded: a new array of double the size is |
| 3671 |
* allocated, if envPtr->mallocedAuxDataArray is non-zero the old array |
| 3672 |
* is freed, and AuxData entries are copied from the old array to the new |
| 3673 |
* one. |
| 3674 |
* |
| 3675 |
*---------------------------------------------------------------------- |
| 3676 |
*/ |
| 3677 |
|
| 3678 |
int |
| 3679 |
TclCreateAuxData( |
| 3680 |
ClientData clientData, /* The compilation auxiliary data to store in |
| 3681 |
* the new aux data record. */ |
| 3682 |
const AuxDataType *typePtr, /* Pointer to the type to attach to this |
| 3683 |
* AuxData */ |
| 3684 |
CompileEnv *envPtr)/* Points to the CompileEnv for which a new |
| 3685 |
* aux data structure is to be allocated. */ |
| 3686 |
{ |
| 3687 |
int index; /* Index for the new AuxData structure. */ |
| 3688 |
AuxData *auxDataPtr; |
| 3689 |
/* Points to the new AuxData structure */ |
| 3690 |
|
| 3691 |
index = envPtr->auxDataArrayNext; |
| 3692 |
if (index >= envPtr->auxDataArrayEnd) { |
| 3693 |
/* |
| 3694 |
* Expand the AuxData array. The currently allocated entries are |
| 3695 |
* stored between elements 0 and (envPtr->auxDataArrayNext - 1) |
| 3696 |
* [inclusive]. |
| 3697 |
*/ |
| 3698 |
|
| 3699 |
size_t currBytes = envPtr->auxDataArrayNext * sizeof(AuxData); |
| 3700 |
int newElems = 2*envPtr->auxDataArrayEnd; |
| 3701 |
size_t newBytes = newElems * sizeof(AuxData); |
| 3702 |
|
| 3703 |
if (envPtr->mallocedAuxDataArray) { |
| 3704 |
envPtr->auxDataArrayPtr = |
| 3705 |
(AuxData *)ckrealloc(envPtr->auxDataArrayPtr, newBytes); |
| 3706 |
} else { |
| 3707 |
/* |
| 3708 |
* envPtr->auxDataArrayPtr isn't a ckalloc'd pointer, so we must |
| 3709 |
* code a ckrealloc equivalent for ourselves. |
| 3710 |
*/ |
| 3711 |
|
| 3712 |
AuxData *newPtr = (AuxData *)ckalloc(newBytes); |
| 3713 |
|
| 3714 |
memcpy(newPtr, envPtr->auxDataArrayPtr, currBytes); |
| 3715 |
envPtr->auxDataArrayPtr = newPtr; |
| 3716 |
envPtr->mallocedAuxDataArray = 1; |
| 3717 |
} |
| 3718 |
envPtr->auxDataArrayEnd = newElems; |
| 3719 |
} |
| 3720 |
envPtr->auxDataArrayNext++; |
| 3721 |
|
| 3722 |
auxDataPtr = &envPtr->auxDataArrayPtr[index]; |
| 3723 |
auxDataPtr->clientData = clientData; |
| 3724 |
auxDataPtr->type = typePtr; |
| 3725 |
return index; |
| 3726 |
} |
| 3727 |
|
| 3728 |
/* |
| 3729 |
*---------------------------------------------------------------------- |
| 3730 |
* |
| 3731 |
* TclInitJumpFixupArray -- |
| 3732 |
* |
| 3733 |
* Initializes a JumpFixupArray structure to hold some number of jump |
| 3734 |
* fixup entries. |
| 3735 |
* |
| 3736 |
* Results: |
| 3737 |
* None. |
| 3738 |
* |
| 3739 |
* Side effects: |
| 3740 |
* The JumpFixupArray structure is initialized. |
| 3741 |
* |
| 3742 |
*---------------------------------------------------------------------- |
| 3743 |
*/ |
| 3744 |
|
| 3745 |
void |
| 3746 |
TclInitJumpFixupArray( |
| 3747 |
JumpFixupArray *fixupArrayPtr) |
| 3748 |
/* Points to the JumpFixupArray structure to |
| 3749 |
* initialize. */ |
| 3750 |
{ |
| 3751 |
fixupArrayPtr->fixup = fixupArrayPtr->staticFixupSpace; |
| 3752 |
fixupArrayPtr->next = 0; |
| 3753 |
fixupArrayPtr->end = JUMPFIXUP_INIT_ENTRIES - 1; |
| 3754 |
fixupArrayPtr->mallocedArray = 0; |
| 3755 |
} |
| 3756 |
|
| 3757 |
/* |
| 3758 |
*---------------------------------------------------------------------- |
| 3759 |
* |
| 3760 |
* TclExpandJumpFixupArray -- |
| 3761 |
* |
| 3762 |
* Procedure that uses malloc to allocate more storage for a jump fixup |
| 3763 |
* array. |
| 3764 |
* |
| 3765 |
* Results: |
| 3766 |
* None. |
| 3767 |
* |
| 3768 |
* Side effects: |
| 3769 |
* The jump fixup array in *fixupArrayPtr is reallocated to a new array |
| 3770 |
* of double the size, and if fixupArrayPtr->mallocedArray is non-zero |
| 3771 |
* the old array is freed. Jump fixup structures are copied from the old |
| 3772 |
* array to the new one. |
| 3773 |
* |
| 3774 |
*---------------------------------------------------------------------- |
| 3775 |
*/ |
| 3776 |
|
| 3777 |
void |
| 3778 |
TclExpandJumpFixupArray( |
| 3779 |
JumpFixupArray *fixupArrayPtr) |
| 3780 |
/* Points to the JumpFixupArray structure to |
| 3781 |
* enlarge. */ |
| 3782 |
{ |
| 3783 |
/* |
| 3784 |
* The currently allocated jump fixup entries are stored from fixup[0] up |
| 3785 |
* to fixup[fixupArrayPtr->fixupNext] (*not* inclusive). We assume |
| 3786 |
* fixupArrayPtr->fixupNext is equal to fixupArrayPtr->fixupEnd. |
| 3787 |
*/ |
| 3788 |
|
| 3789 |
size_t currBytes = fixupArrayPtr->next * sizeof(JumpFixup); |
| 3790 |
int newElems = 2*(fixupArrayPtr->end + 1); |
| 3791 |
size_t newBytes = newElems * sizeof(JumpFixup); |
| 3792 |
|
| 3793 |
if (fixupArrayPtr->mallocedArray) { |
| 3794 |
fixupArrayPtr->fixup = (JumpFixup *)ckrealloc(fixupArrayPtr->fixup, newBytes); |
| 3795 |
} else { |
| 3796 |
/* |
| 3797 |
* fixupArrayPtr->fixup isn't a ckalloc'd pointer, so we must code a |
| 3798 |
* ckrealloc equivalent for ourselves. |
| 3799 |
*/ |
| 3800 |
|
| 3801 |
JumpFixup *newPtr = (JumpFixup *)ckalloc(newBytes); |
| 3802 |
|
| 3803 |
memcpy(newPtr, fixupArrayPtr->fixup, currBytes); |
| 3804 |
fixupArrayPtr->fixup = newPtr; |
| 3805 |
fixupArrayPtr->mallocedArray = 1; |
| 3806 |
} |
| 3807 |
fixupArrayPtr->end = newElems; |
| 3808 |
} |
| 3809 |
|
| 3810 |
/* |
| 3811 |
*---------------------------------------------------------------------- |
| 3812 |
* |
| 3813 |
* TclFreeJumpFixupArray -- |
| 3814 |
* |
| 3815 |
* Free any storage allocated in a jump fixup array structure. |
| 3816 |
* |
| 3817 |
* Results: |
| 3818 |
* None. |
| 3819 |
* |
| 3820 |
* Side effects: |
| 3821 |
* Allocated storage in the JumpFixupArray structure is freed. |
| 3822 |
* |
| 3823 |
*---------------------------------------------------------------------- |
| 3824 |
*/ |
| 3825 |
|
| 3826 |
void |
| 3827 |
TclFreeJumpFixupArray( |
| 3828 |
JumpFixupArray *fixupArrayPtr) |
| 3829 |
/* Points to the JumpFixupArray structure to |
| 3830 |
* free. */ |
| 3831 |
{ |
| 3832 |
if (fixupArrayPtr->mallocedArray) { |
| 3833 |
ckfree(fixupArrayPtr->fixup); |
| 3834 |
} |
| 3835 |
} |
| 3836 |
|
| 3837 |
/* |
| 3838 |
*---------------------------------------------------------------------- |
| 3839 |
* |
| 3840 |
* TclEmitForwardJump -- |
| 3841 |
* |
| 3842 |
* Procedure to emit a two-byte forward jump of kind "jumpType". Since |
| 3843 |
* the jump may later have to be grown to five bytes if the jump target |
| 3844 |
* is more than, say, 127 bytes away, this procedure also initializes a |
| 3845 |
* JumpFixup record with information about the jump. |
| 3846 |
* |
| 3847 |
* Results: |
| 3848 |
* None. |
| 3849 |
* |
| 3850 |
* Side effects: |
| 3851 |
* The JumpFixup record pointed to by "jumpFixupPtr" is initialized with |
| 3852 |
* information needed later if the jump is to be grown. Also, a two byte |
| 3853 |
* jump of the designated type is emitted at the current point in the |
| 3854 |
* bytecode stream. |
| 3855 |
* |
| 3856 |
*---------------------------------------------------------------------- |
| 3857 |
*/ |
| 3858 |
|
| 3859 |
void |
| 3860 |
TclEmitForwardJump( |
| 3861 |
CompileEnv *envPtr, /* Points to the CompileEnv structure that |
| 3862 |
* holds the resulting instruction. */ |
| 3863 |
TclJumpType jumpType, /* Indicates the kind of jump: if true or |
| 3864 |
* false or unconditional. */ |
| 3865 |
JumpFixup *jumpFixupPtr) /* Points to the JumpFixup structure to |
| 3866 |
* initialize with information about this |
| 3867 |
* forward jump. */ |
| 3868 |
{ |
| 3869 |
/* |
| 3870 |
* Initialize the JumpFixup structure: |
| 3871 |
* - codeOffset is offset of first byte of jump below |
| 3872 |
* - cmdIndex is index of the command after the current one |
| 3873 |
* - exceptIndex is the index of the first ExceptionRange after the |
| 3874 |
* current one. |
| 3875 |
*/ |
| 3876 |
|
| 3877 |
jumpFixupPtr->jumpType = jumpType; |
| 3878 |
jumpFixupPtr->codeOffset = envPtr->codeNext - envPtr->codeStart; |
| 3879 |
jumpFixupPtr->cmdIndex = envPtr->numCommands; |
| 3880 |
jumpFixupPtr->exceptIndex = envPtr->exceptArrayNext; |
| 3881 |
|
| 3882 |
switch (jumpType) { |
| 3883 |
case TCL_UNCONDITIONAL_JUMP: |
| 3884 |
TclEmitInstInt1(INST_JUMP1, 0, envPtr); |
| 3885 |
break; |
| 3886 |
case TCL_TRUE_JUMP: |
| 3887 |
TclEmitInstInt1(INST_JUMP_TRUE1, 0, envPtr); |
| 3888 |
break; |
| 3889 |
default: |
| 3890 |
TclEmitInstInt1(INST_JUMP_FALSE1, 0, envPtr); |
| 3891 |
break; |
| 3892 |
} |
| 3893 |
} |
| 3894 |
|
| 3895 |
/* |
| 3896 |
*---------------------------------------------------------------------- |
| 3897 |
* |
| 3898 |
* TclFixupForwardJump -- |
| 3899 |
* |
| 3900 |
* Procedure that updates a previously-emitted forward jump to jump a |
| 3901 |
* specified number of bytes, "jumpDist". If necessary, the jump is grown |
| 3902 |
* from two to five bytes; this is done if the jump distance is greater |
| 3903 |
* than "distThreshold" (normally 127 bytes). The jump is described by a |
| 3904 |
* JumpFixup record previously initialized by TclEmitForwardJump. |
| 3905 |
* |
| 3906 |
* Results: |
| 3907 |
* 1 if the jump was grown and subsequent instructions had to be moved; |
| 3908 |
* otherwise 0. This result is returned to allow callers to update any |
| 3909 |
* additional code offsets they may hold. |
| 3910 |
* |
| 3911 |
* Side effects: |
| 3912 |
* The jump may be grown and subsequent instructions moved. If this |
| 3913 |
* happens, the code offsets for any commands and any ExceptionRange |
| 3914 |
* records between the jump and the current code address will be updated |
| 3915 |
* to reflect the moved code. Also, the bytecode instruction array in the |
| 3916 |
* CompileEnv structure may be grown and reallocated. |
| 3917 |
* |
| 3918 |
*---------------------------------------------------------------------- |
| 3919 |
*/ |
| 3920 |
|
| 3921 |
int |
| 3922 |
TclFixupForwardJump( |
| 3923 |
CompileEnv *envPtr, /* Points to the CompileEnv structure that |
| 3924 |
* holds the resulting instruction. */ |
| 3925 |
JumpFixup *jumpFixupPtr, /* Points to the JumpFixup structure that |
| 3926 |
* describes the forward jump. */ |
| 3927 |
int jumpDist, /* Jump distance to set in jump instr. */ |
| 3928 |
int distThreshold) /* Maximum distance before the two byte jump |
| 3929 |
* is grown to five bytes. */ |
| 3930 |
{ |
| 3931 |
unsigned char *jumpPc, *p; |
| 3932 |
int firstCmd, lastCmd, firstRange, lastRange, k; |
| 3933 |
unsigned numBytes; |
| 3934 |
|
| 3935 |
if (jumpDist <= distThreshold) { |
| 3936 |
jumpPc = envPtr->codeStart + jumpFixupPtr->codeOffset; |
| 3937 |
switch (jumpFixupPtr->jumpType) { |
| 3938 |
case TCL_UNCONDITIONAL_JUMP: |
| 3939 |
TclUpdateInstInt1AtPc(INST_JUMP1, jumpDist, jumpPc); |
| 3940 |
break; |
| 3941 |
case TCL_TRUE_JUMP: |
| 3942 |
TclUpdateInstInt1AtPc(INST_JUMP_TRUE1, jumpDist, jumpPc); |
| 3943 |
break; |
| 3944 |
default: |
| 3945 |
TclUpdateInstInt1AtPc(INST_JUMP_FALSE1, jumpDist, jumpPc); |
| 3946 |
break; |
| 3947 |
} |
| 3948 |
return 0; |
| 3949 |
} |
| 3950 |
|
| 3951 |
/* |
| 3952 |
* We must grow the jump then move subsequent instructions down. Note that |
| 3953 |
* if we expand the space for generated instructions, code addresses might |
| 3954 |
* change; be careful about updating any of these addresses held in |
| 3955 |
* variables. |
| 3956 |
*/ |
| 3957 |
|
| 3958 |
if ((envPtr->codeNext + 3) > envPtr->codeEnd) { |
| 3959 |
TclExpandCodeArray(envPtr); |
| 3960 |
} |
| 3961 |
jumpPc = envPtr->codeStart + jumpFixupPtr->codeOffset; |
| 3962 |
numBytes = envPtr->codeNext-jumpPc-2; |
| 3963 |
p = jumpPc+2; |
| 3964 |
memmove(p+3, p, numBytes); |
| 3965 |
|
| 3966 |
envPtr->codeNext += 3; |
| 3967 |
jumpDist += 3; |
| 3968 |
switch (jumpFixupPtr->jumpType) { |
| 3969 |
case TCL_UNCONDITIONAL_JUMP: |
| 3970 |
TclUpdateInstInt4AtPc(INST_JUMP4, jumpDist, jumpPc); |
| 3971 |
break; |
| 3972 |
case TCL_TRUE_JUMP: |
| 3973 |
TclUpdateInstInt4AtPc(INST_JUMP_TRUE4, jumpDist, jumpPc); |
| 3974 |
break; |
| 3975 |
default: |
| 3976 |
TclUpdateInstInt4AtPc(INST_JUMP_FALSE4, jumpDist, jumpPc); |
| 3977 |
break; |
| 3978 |
} |
| 3979 |
|
| 3980 |
/* |
| 3981 |
* Adjust the code offsets for any commands and any ExceptionRange records |
| 3982 |
* between the jump and the current code address. |
| 3983 |
*/ |
| 3984 |
|
| 3985 |
firstCmd = jumpFixupPtr->cmdIndex; |
| 3986 |
lastCmd = envPtr->numCommands - 1; |
| 3987 |
if (firstCmd < lastCmd) { |
| 3988 |
for (k = firstCmd; k <= lastCmd; k++) { |
| 3989 |
envPtr->cmdMapPtr[k].codeOffset += 3; |
| 3990 |
} |
| 3991 |
} |
| 3992 |
|
| 3993 |
firstRange = jumpFixupPtr->exceptIndex; |
| 3994 |
lastRange = envPtr->exceptArrayNext - 1; |
| 3995 |
for (k = firstRange; k <= lastRange; k++) { |
| 3996 |
ExceptionRange *rangePtr = &envPtr->exceptArrayPtr[k]; |
| 3997 |
|
| 3998 |
rangePtr->codeOffset += 3; |
| 3999 |
switch (rangePtr->type) { |
| 4000 |
case LOOP_EXCEPTION_RANGE: |
| 4001 |
rangePtr->breakOffset += 3; |
| 4002 |
if (rangePtr->continueOffset != -1) { |
| 4003 |
rangePtr->continueOffset += 3; |
| 4004 |
} |
| 4005 |
break; |
| 4006 |
case CATCH_EXCEPTION_RANGE: |
| 4007 |
rangePtr->catchOffset += 3; |
| 4008 |
break; |
| 4009 |
default: |
| 4010 |
Tcl_Panic("TclFixupForwardJump: bad ExceptionRange type %d", |
| 4011 |
rangePtr->type); |
| 4012 |
} |
| 4013 |
} |
| 4014 |
|
| 4015 |
for (k = 0 ; k < envPtr->exceptArrayNext ; k++) { |
| 4016 |
ExceptionAux *auxPtr = &envPtr->exceptAuxArrayPtr[k]; |
| 4017 |
int i; |
| 4018 |
|
| 4019 |
for (i=0 ; i<auxPtr->numBreakTargets ; i++) { |
| 4020 |
if (jumpFixupPtr->codeOffset < auxPtr->breakTargets[i]) { |
| 4021 |
auxPtr->breakTargets[i] += 3; |
| 4022 |
} |
| 4023 |
} |
| 4024 |
for (i=0 ; i<auxPtr->numContinueTargets ; i++) { |
| 4025 |
if (jumpFixupPtr->codeOffset < auxPtr->continueTargets[i]) { |
| 4026 |
auxPtr->continueTargets[i] += 3; |
| 4027 |
} |
| 4028 |
} |
| 4029 |
} |
| 4030 |
|
| 4031 |
return 1; /* the jump was grown */ |
| 4032 |
} |
| 4033 |
|
| 4034 |
/* |
| 4035 |
*---------------------------------------------------------------------- |
| 4036 |
* |
| 4037 |
* TclEmitInvoke -- |
| 4038 |
* |
| 4039 |
* Emit one of the invoke-related instructions, wrapping it if necessary |
| 4040 |
* in code that ensures that any break or continue operation passing |
| 4041 |
* through it gets the stack unwinding correct, converting it into an |
| 4042 |
* internal jump if in an appropriate context. |
| 4043 |
* |
| 4044 |
* Results: |
| 4045 |
* None |
| 4046 |
* |
| 4047 |
* Side effects: |
| 4048 |
* Issues the jump with all correct stack management. May create another |
| 4049 |
* loop exception range; pointers to ExceptionRange and ExceptionAux |
| 4050 |
* structures should not be held across this call. |
| 4051 |
* |
| 4052 |
*---------------------------------------------------------------------- |
| 4053 |
*/ |
| 4054 |
|
| 4055 |
void |
| 4056 |
TclEmitInvoke( |
| 4057 |
CompileEnv *envPtr, |
| 4058 |
int opcode, |
| 4059 |
...) |
| 4060 |
{ |
| 4061 |
va_list argList; |
| 4062 |
ExceptionRange *rangePtr; |
| 4063 |
ExceptionAux *auxBreakPtr, *auxContinuePtr; |
| 4064 |
int arg1, arg2, wordCount = 0, expandCount = 0; |
| 4065 |
int loopRange = 0, breakRange = 0, continueRange = 0; |
| 4066 |
int cleanup, depth = TclGetStackDepth(envPtr); |
| 4067 |
|
| 4068 |
/* |
| 4069 |
* Parse the arguments. |
| 4070 |
*/ |
| 4071 |
|
| 4072 |
va_start(argList, opcode); |
| 4073 |
switch (opcode) { |
| 4074 |
case INST_INVOKE_STK1: |
| 4075 |
wordCount = arg1 = cleanup = va_arg(argList, int); |
| 4076 |
arg2 = 0; |
| 4077 |
break; |
| 4078 |
case INST_INVOKE_STK4: |
| 4079 |
wordCount = arg1 = cleanup = va_arg(argList, int); |
| 4080 |
arg2 = 0; |
| 4081 |
break; |
| 4082 |
case INST_INVOKE_REPLACE: |
| 4083 |
arg1 = va_arg(argList, int); |
| 4084 |
arg2 = va_arg(argList, int); |
| 4085 |
wordCount = arg1 + arg2 - 1; |
| 4086 |
cleanup = arg1 + 1; |
| 4087 |
break; |
| 4088 |
default: |
| 4089 |
Tcl_Panic("unexpected opcode"); |
| 4090 |
case INST_EVAL_STK: |
| 4091 |
wordCount = cleanup = 1; |
| 4092 |
arg1 = arg2 = 0; |
| 4093 |
break; |
| 4094 |
case INST_RETURN_STK: |
| 4095 |
wordCount = cleanup = 2; |
| 4096 |
arg1 = arg2 = 0; |
| 4097 |
break; |
| 4098 |
case INST_INVOKE_EXPANDED: |
| 4099 |
wordCount = arg1 = cleanup = va_arg(argList, int); |
| 4100 |
arg2 = 0; |
| 4101 |
expandCount = 1; |
| 4102 |
break; |
| 4103 |
} |
| 4104 |
va_end(argList); |
| 4105 |
|
| 4106 |
/* |
| 4107 |
* Determine if we need to handle break and continue exceptions with a |
| 4108 |
* special handling exception range (so that we can correctly unwind the |
| 4109 |
* stack). |
| 4110 |
* |
| 4111 |
* These must be done separately; they can be different (especially for |
| 4112 |
* calls from inside a [for] increment clause). |
| 4113 |
*/ |
| 4114 |
|
| 4115 |
rangePtr = TclGetInnermostExceptionRange(envPtr, TCL_CONTINUE, |
| 4116 |
&auxContinuePtr); |
| 4117 |
if (rangePtr == NULL || rangePtr->type != LOOP_EXCEPTION_RANGE) { |
| 4118 |
auxContinuePtr = NULL; |
| 4119 |
} else if (auxContinuePtr->stackDepth == envPtr->currStackDepth-wordCount |
| 4120 |
&& auxContinuePtr->expandTarget == envPtr->expandCount-expandCount) { |
| 4121 |
auxContinuePtr = NULL; |
| 4122 |
} else { |
| 4123 |
continueRange = auxContinuePtr - envPtr->exceptAuxArrayPtr; |
| 4124 |
} |
| 4125 |
|
| 4126 |
rangePtr = TclGetInnermostExceptionRange(envPtr, TCL_BREAK, &auxBreakPtr); |
| 4127 |
if (rangePtr == NULL || rangePtr->type != LOOP_EXCEPTION_RANGE) { |
| 4128 |
auxBreakPtr = NULL; |
| 4129 |
} else if (auxContinuePtr == NULL |
| 4130 |
&& auxBreakPtr->stackDepth == envPtr->currStackDepth-wordCount |
| 4131 |
&& auxBreakPtr->expandTarget == envPtr->expandCount-expandCount) { |
| 4132 |
auxBreakPtr = NULL; |
| 4133 |
} else { |
| 4134 |
breakRange = auxBreakPtr - envPtr->exceptAuxArrayPtr; |
| 4135 |
} |
| 4136 |
|
| 4137 |
if (auxBreakPtr != NULL || auxContinuePtr != NULL) { |
| 4138 |
loopRange = TclCreateExceptRange(LOOP_EXCEPTION_RANGE, envPtr); |
| 4139 |
ExceptionRangeStarts(envPtr, loopRange); |
| 4140 |
} |
| 4141 |
|
| 4142 |
/* |
| 4143 |
* Issue the invoke itself. |
| 4144 |
*/ |
| 4145 |
|
| 4146 |
switch (opcode) { |
| 4147 |
case INST_INVOKE_STK1: |
| 4148 |
TclEmitInstInt1(INST_INVOKE_STK1, arg1, envPtr); |
| 4149 |
break; |
| 4150 |
case INST_INVOKE_STK4: |
| 4151 |
TclEmitInstInt4(INST_INVOKE_STK4, arg1, envPtr); |
| 4152 |
break; |
| 4153 |
case INST_INVOKE_EXPANDED: |
| 4154 |
TclEmitOpcode(INST_INVOKE_EXPANDED, envPtr); |
| 4155 |
envPtr->expandCount--; |
| 4156 |
TclAdjustStackDepth(1 - arg1, envPtr); |
| 4157 |
break; |
| 4158 |
case INST_EVAL_STK: |
| 4159 |
TclEmitOpcode(INST_EVAL_STK, envPtr); |
| 4160 |
break; |
| 4161 |
case INST_RETURN_STK: |
| 4162 |
TclEmitOpcode(INST_RETURN_STK, envPtr); |
| 4163 |
break; |
| 4164 |
case INST_INVOKE_REPLACE: |
| 4165 |
TclEmitInstInt4(INST_INVOKE_REPLACE, arg1, envPtr); |
| 4166 |
TclEmitInt1(arg2, envPtr); |
| 4167 |
TclAdjustStackDepth(-1, envPtr); /* Correction to stack depth calcs */ |
| 4168 |
break; |
| 4169 |
} |
| 4170 |
|
| 4171 |
/* |
| 4172 |
* If we're generating a special wrapper exception range, we need to |
| 4173 |
* finish that up now. |
| 4174 |
*/ |
| 4175 |
|
| 4176 |
if (auxBreakPtr != NULL || auxContinuePtr != NULL) { |
| 4177 |
int savedStackDepth = envPtr->currStackDepth; |
| 4178 |
int savedExpandCount = envPtr->expandCount; |
| 4179 |
JumpFixup nonTrapFixup; |
| 4180 |
|
| 4181 |
if (auxBreakPtr != NULL) { |
| 4182 |
auxBreakPtr = envPtr->exceptAuxArrayPtr + breakRange; |
| 4183 |
} |
| 4184 |
if (auxContinuePtr != NULL) { |
| 4185 |
auxContinuePtr = envPtr->exceptAuxArrayPtr + continueRange; |
| 4186 |
} |
| 4187 |
|
| 4188 |
ExceptionRangeEnds(envPtr, loopRange); |
| 4189 |
TclEmitForwardJump(envPtr, TCL_UNCONDITIONAL_JUMP, &nonTrapFixup); |
| 4190 |
|
| 4191 |
/* |
| 4192 |
* Careful! When generating these stack unwinding sequences, the depth |
| 4193 |
* of stack in the cases where they are taken is not the same as if |
| 4194 |
* the exception is not taken. |
| 4195 |
*/ |
| 4196 |
|
| 4197 |
if (auxBreakPtr != NULL) { |
| 4198 |
TclAdjustStackDepth(-1, envPtr); |
| 4199 |
|
| 4200 |
ExceptionRangeTarget(envPtr, loopRange, breakOffset); |
| 4201 |
TclCleanupStackForBreakContinue(envPtr, auxBreakPtr); |
| 4202 |
TclAddLoopBreakFixup(envPtr, auxBreakPtr); |
| 4203 |
TclAdjustStackDepth(1, envPtr); |
| 4204 |
|
| 4205 |
envPtr->currStackDepth = savedStackDepth; |
| 4206 |
envPtr->expandCount = savedExpandCount; |
| 4207 |
} |
| 4208 |
|
| 4209 |
if (auxContinuePtr != NULL) { |
| 4210 |
TclAdjustStackDepth(-1, envPtr); |
| 4211 |
|
| 4212 |
ExceptionRangeTarget(envPtr, loopRange, continueOffset); |
| 4213 |
TclCleanupStackForBreakContinue(envPtr, auxContinuePtr); |
| 4214 |
TclAddLoopContinueFixup(envPtr, auxContinuePtr); |
| 4215 |
TclAdjustStackDepth(1, envPtr); |
| 4216 |
|
| 4217 |
envPtr->currStackDepth = savedStackDepth; |
| 4218 |
envPtr->expandCount = savedExpandCount; |
| 4219 |
} |
| 4220 |
|
| 4221 |
TclFinalizeLoopExceptionRange(envPtr, loopRange); |
| 4222 |
TclFixupForwardJumpToHere(envPtr, &nonTrapFixup, 127); |
| 4223 |
} |
| 4224 |
TclCheckStackDepth(depth+1-cleanup, envPtr); |
| 4225 |
} |
| 4226 |
|
| 4227 |
/* |
| 4228 |
*---------------------------------------------------------------------- |
| 4229 |
* |
| 4230 |
* TclGetInstructionTable -- |
| 4231 |
* |
| 4232 |
* Returns a pointer to the table describing Tcl bytecode instructions. |
| 4233 |
* This procedure is defined so that clients can access the pointer from |
| 4234 |
* outside the TCL DLLs. |
| 4235 |
* |
| 4236 |
* Results: |
| 4237 |
* Returns a pointer to the global instruction table, same as the |
| 4238 |
* expression (&tclInstructionTable[0]). |
| 4239 |
* |
| 4240 |
* Side effects: |
| 4241 |
* None. |
| 4242 |
* |
| 4243 |
*---------------------------------------------------------------------- |
| 4244 |
*/ |
| 4245 |
|
| 4246 |
const void * /* == InstructionDesc* == */ |
| 4247 |
TclGetInstructionTable(void) |
| 4248 |
{ |
| 4249 |
return &tclInstructionTable[0]; |
| 4250 |
} |
| 4251 |
|
| 4252 |
/* |
| 4253 |
*---------------------------------------------------------------------- |
| 4254 |
* |
| 4255 |
* GetCmdLocEncodingSize -- |
| 4256 |
* |
| 4257 |
* Computes the total number of bytes needed to encode the command |
| 4258 |
* location information for some compiled code. |
| 4259 |
* |
| 4260 |
* Results: |
| 4261 |
* The byte count needed to encode the compiled location information. |
| 4262 |
* |
| 4263 |
* Side effects: |
| 4264 |
* None. |
| 4265 |
* |
| 4266 |
*---------------------------------------------------------------------- |
| 4267 |
*/ |
| 4268 |
|
| 4269 |
static int |
| 4270 |
GetCmdLocEncodingSize( |
| 4271 |
CompileEnv *envPtr) /* Points to compilation environment structure |
| 4272 |
* containing the CmdLocation structure to |
| 4273 |
* encode. */ |
| 4274 |
{ |
| 4275 |
CmdLocation *mapPtr = envPtr->cmdMapPtr; |
| 4276 |
int numCmds = envPtr->numCommands; |
| 4277 |
int codeDelta, codeLen, srcDelta, srcLen; |
| 4278 |
int codeDeltaNext, codeLengthNext, srcDeltaNext, srcLengthNext; |
| 4279 |
/* The offsets in their respective byte |
| 4280 |
* sequences where the next encoded offset or |
| 4281 |
* length should go. */ |
| 4282 |
int prevCodeOffset, prevSrcOffset, i; |
| 4283 |
|
| 4284 |
codeDeltaNext = codeLengthNext = srcDeltaNext = srcLengthNext = 0; |
| 4285 |
prevCodeOffset = prevSrcOffset = 0; |
| 4286 |
for (i = 0; i < numCmds; i++) { |
| 4287 |
codeDelta = mapPtr[i].codeOffset - prevCodeOffset; |
| 4288 |
if (codeDelta < 0) { |
| 4289 |
Tcl_Panic("GetCmdLocEncodingSize: bad code offset"); |
| 4290 |
} else if (codeDelta <= 127) { |
| 4291 |
codeDeltaNext++; |
| 4292 |
} else { |
| 4293 |
codeDeltaNext += 5; /* 1 byte for 0xFF, 4 for positive delta */ |
| 4294 |
} |
| 4295 |
prevCodeOffset = mapPtr[i].codeOffset; |
| 4296 |
|
| 4297 |
codeLen = mapPtr[i].numCodeBytes; |
| 4298 |
if (codeLen < 0) { |
| 4299 |
Tcl_Panic("GetCmdLocEncodingSize: bad code length"); |
| 4300 |
} else if (codeLen <= 127) { |
| 4301 |
codeLengthNext++; |
| 4302 |
} else { |
| 4303 |
codeLengthNext += 5;/* 1 byte for 0xFF, 4 for length */ |
| 4304 |
} |
| 4305 |
|
| 4306 |
srcDelta = mapPtr[i].srcOffset - prevSrcOffset; |
| 4307 |
if ((-127 <= srcDelta) && (srcDelta <= 127) && (srcDelta != -1)) { |
| 4308 |
srcDeltaNext++; |
| 4309 |
} else { |
| 4310 |
srcDeltaNext += 5; /* 1 byte for 0xFF, 4 for delta */ |
| 4311 |
} |
| 4312 |
prevSrcOffset = mapPtr[i].srcOffset; |
| 4313 |
|
| 4314 |
srcLen = mapPtr[i].numSrcBytes; |
| 4315 |
if (srcLen < 0) { |
| 4316 |
Tcl_Panic("GetCmdLocEncodingSize: bad source length"); |
| 4317 |
} else if (srcLen <= 127) { |
| 4318 |
srcLengthNext++; |
| 4319 |
} else { |
| 4320 |
srcLengthNext += 5; /* 1 byte for 0xFF, 4 for length */ |
| 4321 |
} |
| 4322 |
} |
| 4323 |
|
| 4324 |
return (codeDeltaNext + codeLengthNext + srcDeltaNext + srcLengthNext); |
| 4325 |
} |
| 4326 |
|
| 4327 |
/* |
| 4328 |
*---------------------------------------------------------------------- |
| 4329 |
* |
| 4330 |
* EncodeCmdLocMap -- |
| 4331 |
* |
| 4332 |
* Encode the command location information for some compiled code into a |
| 4333 |
* ByteCode structure. The encoded command location map is stored as |
| 4334 |
* three adjacent byte sequences. |
| 4335 |
* |
| 4336 |
* Results: |
| 4337 |
* Pointer to the first byte after the encoded command location |
| 4338 |
* information. |
| 4339 |
* |
| 4340 |
* Side effects: |
| 4341 |
* The encoded information is stored into the block of memory headed by |
| 4342 |
* codePtr. Also records pointers to the start of the four byte sequences |
| 4343 |
* in fields in codePtr's ByteCode header structure. |
| 4344 |
* |
| 4345 |
*---------------------------------------------------------------------- |
| 4346 |
*/ |
| 4347 |
|
| 4348 |
static unsigned char * |
| 4349 |
EncodeCmdLocMap( |
| 4350 |
CompileEnv *envPtr, /* Points to compilation environment structure |
| 4351 |
* containing the CmdLocation structure to |
| 4352 |
* encode. */ |
| 4353 |
ByteCode *codePtr, /* ByteCode in which to encode envPtr's |
| 4354 |
* command location information. */ |
| 4355 |
unsigned char *startPtr) /* Points to the first byte in codePtr's |
| 4356 |
* memory block where the location information |
| 4357 |
* is to be stored. */ |
| 4358 |
{ |
| 4359 |
CmdLocation *mapPtr = envPtr->cmdMapPtr; |
| 4360 |
int numCmds = envPtr->numCommands; |
| 4361 |
unsigned char *p = startPtr; |
| 4362 |
int codeDelta, codeLen, srcDelta, srcLen, prevOffset; |
| 4363 |
int i; |
| 4364 |
|
| 4365 |
/* |
| 4366 |
* Encode the code offset for each command as a sequence of deltas. |
| 4367 |
*/ |
| 4368 |
|
| 4369 |
codePtr->codeDeltaStart = p; |
| 4370 |
prevOffset = 0; |
| 4371 |
for (i = 0; i < numCmds; i++) { |
| 4372 |
codeDelta = mapPtr[i].codeOffset - prevOffset; |
| 4373 |
if (codeDelta < 0) { |
| 4374 |
Tcl_Panic("EncodeCmdLocMap: bad code offset"); |
| 4375 |
} else if (codeDelta <= 127) { |
| 4376 |
TclStoreInt1AtPtr(codeDelta, p); |
| 4377 |
p++; |
| 4378 |
} else { |
| 4379 |
TclStoreInt1AtPtr(0xFF, p); |
| 4380 |
p++; |
| 4381 |
TclStoreInt4AtPtr(codeDelta, p); |
| 4382 |
p += 4; |
| 4383 |
} |
| 4384 |
prevOffset = mapPtr[i].codeOffset; |
| 4385 |
} |
| 4386 |
|
| 4387 |
/* |
| 4388 |
* Encode the code length for each command. |
| 4389 |
*/ |
| 4390 |
|
| 4391 |
codePtr->codeLengthStart = p; |
| 4392 |
for (i = 0; i < numCmds; i++) { |
| 4393 |
codeLen = mapPtr[i].numCodeBytes; |
| 4394 |
if (codeLen < 0) { |
| 4395 |
Tcl_Panic("EncodeCmdLocMap: bad code length"); |
| 4396 |
} else if (codeLen <= 127) { |
| 4397 |
TclStoreInt1AtPtr(codeLen, p); |
| 4398 |
p++; |
| 4399 |
} else { |
| 4400 |
TclStoreInt1AtPtr(0xFF, p); |
| 4401 |
p++; |
| 4402 |
TclStoreInt4AtPtr(codeLen, p); |
| 4403 |
p += 4; |
| 4404 |
} |
| 4405 |
} |
| 4406 |
|
| 4407 |
/* |
| 4408 |
* Encode the source offset for each command as a sequence of deltas. |
| 4409 |
*/ |
| 4410 |
|
| 4411 |
codePtr->srcDeltaStart = p; |
| 4412 |
prevOffset = 0; |
| 4413 |
for (i = 0; i < numCmds; i++) { |
| 4414 |
srcDelta = mapPtr[i].srcOffset - prevOffset; |
| 4415 |
if ((-127 <= srcDelta) && (srcDelta <= 127) && (srcDelta != -1)) { |
| 4416 |
TclStoreInt1AtPtr(srcDelta, p); |
| 4417 |
p++; |
| 4418 |
} else { |
| 4419 |
TclStoreInt1AtPtr(0xFF, p); |
| 4420 |
p++; |
| 4421 |
TclStoreInt4AtPtr(srcDelta, p); |
| 4422 |
p += 4; |
| 4423 |
} |
| 4424 |
prevOffset = mapPtr[i].srcOffset; |
| 4425 |
} |
| 4426 |
|
| 4427 |
/* |
| 4428 |
* Encode the source length for each command. |
| 4429 |
*/ |
| 4430 |
|
| 4431 |
codePtr->srcLengthStart = p; |
| 4432 |
for (i = 0; i < numCmds; i++) { |
| 4433 |
srcLen = mapPtr[i].numSrcBytes; |
| 4434 |
if (srcLen < 0) { |
| 4435 |
Tcl_Panic("EncodeCmdLocMap: bad source length"); |
| 4436 |
} else if (srcLen <= 127) { |
| 4437 |
TclStoreInt1AtPtr(srcLen, p); |
| 4438 |
p++; |
| 4439 |
} else { |
| 4440 |
TclStoreInt1AtPtr(0xFF, p); |
| 4441 |
p++; |
| 4442 |
TclStoreInt4AtPtr(srcLen, p); |
| 4443 |
p += 4; |
| 4444 |
} |
| 4445 |
} |
| 4446 |
|
| 4447 |
return p; |
| 4448 |
} |
| 4449 |
|
| 4450 |
#ifdef TCL_COMPILE_STATS |
| 4451 |
/* |
| 4452 |
*---------------------------------------------------------------------- |
| 4453 |
* |
| 4454 |
* RecordByteCodeStats -- |
| 4455 |
* |
| 4456 |
* Accumulates various compilation-related statistics for each newly |
| 4457 |
* compiled ByteCode. Called by the TclInitByteCodeObj when Tcl is |
| 4458 |
* compiled with the -DTCL_COMPILE_STATS flag |
| 4459 |
* |
| 4460 |
* Results: |
| 4461 |
* None. |
| 4462 |
* |
| 4463 |
* Side effects: |
| 4464 |
* Accumulates aggregate code-related statistics in the interpreter's |
| 4465 |
* ByteCodeStats structure. Records statistics specific to a ByteCode in |
| 4466 |
* its ByteCode structure. |
| 4467 |
* |
| 4468 |
*---------------------------------------------------------------------- |
| 4469 |
*/ |
| 4470 |
|
| 4471 |
void |
| 4472 |
RecordByteCodeStats( |
| 4473 |
ByteCode *codePtr) /* Points to ByteCode structure with info |
| 4474 |
* to add to accumulated statistics. */ |
| 4475 |
{ |
| 4476 |
Interp *iPtr = (Interp *) *codePtr->interpHandle; |
| 4477 |
ByteCodeStats *statsPtr; |
| 4478 |
|
| 4479 |
if (iPtr == NULL) { |
| 4480 |
/* Avoid segfaulting in case we're called in a deleted interp */ |
| 4481 |
return; |
| 4482 |
} |
| 4483 |
statsPtr = &(iPtr->stats); |
| 4484 |
|
| 4485 |
statsPtr->numCompilations++; |
| 4486 |
statsPtr->totalSrcBytes += (double) codePtr->numSrcBytes; |
| 4487 |
statsPtr->totalByteCodeBytes += (double) codePtr->structureSize; |
| 4488 |
statsPtr->currentSrcBytes += (double) codePtr->numSrcBytes; |
| 4489 |
statsPtr->currentByteCodeBytes += (double) codePtr->structureSize; |
| 4490 |
|
| 4491 |
statsPtr->srcCount[TclLog2(codePtr->numSrcBytes)]++; |
| 4492 |
statsPtr->byteCodeCount[TclLog2((int) codePtr->structureSize)]++; |
| 4493 |
|
| 4494 |
statsPtr->currentInstBytes += (double) codePtr->numCodeBytes; |
| 4495 |
statsPtr->currentLitBytes += (double) |
| 4496 |
codePtr->numLitObjects * sizeof(Tcl_Obj *); |
| 4497 |
statsPtr->currentExceptBytes += (double) |
| 4498 |
codePtr->numExceptRanges * sizeof(ExceptionRange); |
| 4499 |
statsPtr->currentAuxBytes += (double) |
| 4500 |
codePtr->numAuxDataItems * sizeof(AuxData); |
| 4501 |
statsPtr->currentCmdMapBytes += (double) codePtr->numCmdLocBytes; |
| 4502 |
} |
| 4503 |
#endif /* TCL_COMPILE_STATS */ |
| 4504 |
|
| 4505 |
/* |
| 4506 |
* Local Variables: |
| 4507 |
* mode: c |
| 4508 |
* c-basic-offset: 4 |
| 4509 |
* fill-column: 78 |
| 4510 |
* tab-width: 8 |
| 4511 |
* End: |
| 4512 |
*/ |