1 /* 2 * tclCompile.h -- 3 * 4 * Copyright (c) 1996-1998 Sun Microsystems, Inc. 5 * Copyright (c) 1998-2000 by Scriptics Corporation. 6 * Copyright (c) 2001 by Kevin B. Kenny. All rights reserved. 7 * Copyright (c) 2007 Daniel A. Steffen <das@users.sourceforge.net> 8 * 9 * See the file "license.terms" for information on usage and redistribution of 10 * this file, and for a DISCLAIMER OF ALL WARRANTIES. 11 */ 12 13 #ifndef _TCLCOMPILATION 14 #define _TCLCOMPILATION 1 15 16 #include "tclInt.h" 17 18 struct ByteCode; /* Forward declaration. */ 19 20 /* 21 *------------------------------------------------------------------------ 22 * Variables related to compilation. These are used in tclCompile.c, 23 * tclExecute.c, tclBasic.c, and their clients. 24 *------------------------------------------------------------------------ 25 */ 26 27 #ifdef TCL_COMPILE_DEBUG 28 /* 29 * Variable that controls whether compilation tracing is enabled and, if so, 30 * what level of tracing is desired: 31 * 0: no compilation tracing 32 * 1: summarize compilation of top level cmds and proc bodies 33 * 2: display all instructions of each ByteCode compiled 34 * This variable is linked to the Tcl variable "tcl_traceCompile". 35 */ 36 37 MODULE_SCOPE int tclTraceCompile; 38 39 /* 40 * Variable that controls whether execution tracing is enabled and, if so, 41 * what level of tracing is desired: 42 * 0: no execution tracing 43 * 1: trace invocations of Tcl procs only 44 * 2: trace invocations of all (not compiled away) commands 45 * 3: display each instruction executed 46 * This variable is linked to the Tcl variable "tcl_traceExec". 47 */ 48 49 MODULE_SCOPE int tclTraceExec; 50 #endif 51 52 /* 53 * The type of lambda expressions. Note that every lambda will *always* have a 54 * string representation. 55 */ 56 57 MODULE_SCOPE const Tcl_ObjType tclLambdaType; 58 59 /* 60 *------------------------------------------------------------------------ 61 * Data structures related to compilation. 62 *------------------------------------------------------------------------ 63 */ 64 65 /* 66 * The structure used to implement Tcl "exceptions" (exceptional returns): for 67 * example, those generated in loops by the break and continue commands, and 68 * those generated by scripts and caught by the catch command. This 69 * ExceptionRange structure describes a range of code (e.g., a loop body), the 70 * kind of exceptions (e.g., a break or continue) that might occur, and the PC 71 * offsets to jump to if a matching exception does occur. Exception ranges can 72 * nest so this structure includes a nesting level that is used at runtime to 73 * find the closest exception range surrounding a PC. For example, when a 74 * break command is executed, the ExceptionRange structure for the most deeply 75 * nested loop, if any, is found and used. These structures are also generated 76 * for the "next" subcommands of for loops since a break there terminates the 77 * for command. This means a for command actually generates two LoopInfo 78 * structures. 79 */ 80 81 typedef enum { 82 LOOP_EXCEPTION_RANGE, /* Exception's range is part of a loop. Break 83 * and continue "exceptions" cause jumps to 84 * appropriate PC offsets. */ 85 CATCH_EXCEPTION_RANGE /* Exception's range is controlled by a catch 86 * command. Errors in the range cause a jump 87 * to a catch PC offset. */ 88 } ExceptionRangeType; 89 90 typedef struct ExceptionRange { 91 ExceptionRangeType type; /* The kind of ExceptionRange. */ 92 int nestingLevel; /* Static depth of the exception range. Used 93 * to find the most deeply-nested range 94 * surrounding a PC at runtime. */ 95 int codeOffset; /* Offset of the first instruction byte of the 96 * code range. */ 97 int numCodeBytes; /* Number of bytes in the code range. */ 98 int breakOffset; /* If LOOP_EXCEPTION_RANGE, the target PC 99 * offset for a break command in the range. */ 100 int continueOffset; /* If LOOP_EXCEPTION_RANGE and not -1, the 101 * target PC offset for a continue command in 102 * the code range. Otherwise, ignore this 103 * range when processing a continue 104 * command. */ 105 int catchOffset; /* If a CATCH_EXCEPTION_RANGE, the target PC 106 * offset for any "exception" in range. */ 107 } ExceptionRange; 108 109 /* 110 * Auxiliary data used when issuing (currently just loop) exception ranges, 111 * but which is not required during execution. 112 */ 113 114 typedef struct ExceptionAux { 115 int supportsContinue; /* Whether this exception range will have a 116 * continueOffset created for it; if it is a 117 * loop exception range that *doesn't* have 118 * one (see [for] next-clause) then we must 119 * not pick up the range when scanning for a 120 * target to continue to. */ 121 int stackDepth; /* The stack depth at the point where the 122 * exception range was created. This is used 123 * to calculate the number of POPs required to 124 * restore the stack to its prior state. */ 125 int expandTarget; /* The number of expansions expected on the 126 * auxData stack at the time the loop starts; 127 * we can't currently discard them except by 128 * doing INST_INVOKE_EXPANDED; this is a known 129 * problem. */ 130 int expandTargetDepth; /* The stack depth expected at the outermost 131 * expansion within the loop. Not meaningful 132 * if there are no open expansions between the 133 * looping level and the point of jump 134 * issue. */ 135 int numBreakTargets; /* The number of [break]s that want to be 136 * targeted to the place where this loop 137 * exception will be bound to. */ 138 unsigned int *breakTargets; /* The offsets of the INST_JUMP4 instructions 139 * issued by the [break]s that we must 140 * update. Note that resizing a jump (via 141 * TclFixupForwardJump) can cause the contents 142 * of this array to be updated. When 143 * numBreakTargets==0, this is NULL. */ 144 int allocBreakTargets; /* The size of the breakTargets array. */ 145 int numContinueTargets; /* The number of [continue]s that want to be 146 * targeted to the place where this loop 147 * exception will be bound to. */ 148 unsigned int *continueTargets; /* The offsets of the INST_JUMP4 instructions 149 * issued by the [continue]s that we must 150 * update. Note that resizing a jump (via 151 * TclFixupForwardJump) can cause the contents 152 * of this array to be updated. When 153 * numContinueTargets==0, this is NULL. */ 154 int allocContinueTargets; /* The size of the continueTargets array. */ 155 } ExceptionAux; 156 157 /* 158 * Structure used to map between instruction pc and source locations. It 159 * defines for each compiled Tcl command its code's starting offset and its 160 * source's starting offset and length. Note that the code offset increases 161 * monotonically: that is, the table is sorted in code offset order. The 162 * source offset is not monotonic. 163 */ 164 165 typedef struct CmdLocation { 166 int codeOffset; /* Offset of first byte of command code. */ 167 int numCodeBytes; /* Number of bytes for command's code. */ 168 int srcOffset; /* Offset of first char of the command. */ 169 int numSrcBytes; /* Number of command source chars. */ 170 } CmdLocation; 171 172 /* 173 * TIP #280 174 * Structure to record additional location information for byte code. This 175 * information is internal and not saved. i.e. tbcload'ed code will not have 176 * this information. It records the lines for all words of all commands found 177 * in the byte code. The association with a ByteCode structure BC is done 178 * through the 'lineBCPtr' HashTable in Interp, keyed by the address of BC. 179 * Also recorded is information coming from the context, i.e. type of the 180 * frame and associated information, like the path of a sourced file. 181 */ 182 183 typedef struct ECL { 184 int srcOffset; /* Command location to find the entry. */ 185 int nline; /* Number of words in the command */ 186 int *line; /* Line information for all words in the 187 * command. */ 188 int **next; /* Transient information used by the compiler 189 * for tracking of hidden continuation 190 * lines. */ 191 } ECL; 192 193 typedef struct ExtCmdLoc { 194 int type; /* Context type. */ 195 int start; /* Starting line for compiled script. Needed 196 * for the extended recompile check in 197 * tclCompileObj. */ 198 Tcl_Obj *path; /* Path of the sourced file the command is 199 * in. */ 200 ECL *loc; /* Command word locations (lines). */ 201 int nloc; /* Number of allocated entries in 'loc'. */ 202 int nuloc; /* Number of used entries in 'loc'. */ 203 } ExtCmdLoc; 204 205 /* 206 * CompileProcs need the ability to record information during compilation that 207 * can be used by bytecode instructions during execution. The AuxData 208 * structure provides this "auxiliary data" mechanism. An arbitrary number of 209 * these structures can be stored in the ByteCode record (during compilation 210 * they are stored in a CompileEnv structure). Each AuxData record holds one 211 * word of client-specified data (often a pointer) and is given an index that 212 * instructions can later use to look up the structure and its data. 213 * 214 * The following definitions declare the types of procedures that are called 215 * to duplicate or free this auxiliary data when the containing ByteCode 216 * objects are duplicated and freed. Pointers to these procedures are kept in 217 * the AuxData structure. 218 */ 219 220 typedef ClientData (AuxDataDupProc) (ClientData clientData); 221 typedef void (AuxDataFreeProc) (ClientData clientData); 222 typedef void (AuxDataPrintProc)(ClientData clientData, 223 Tcl_Obj *appendObj, struct ByteCode *codePtr, 224 unsigned int pcOffset); 225 226 /* 227 * We define a separate AuxDataType struct to hold type-related information 228 * for the AuxData structure. This separation makes it possible for clients 229 * outside of the TCL core to manipulate (in a limited fashion!) AuxData; for 230 * example, it makes it possible to pickle and unpickle AuxData structs. 231 */ 232 233 typedef struct AuxDataType { 234 const char *name; /* The name of the type. Types can be 235 * registered and found by name */ 236 AuxDataDupProc *dupProc; /* Callback procedure to invoke when the aux 237 * data is duplicated (e.g., when the ByteCode 238 * structure containing the aux data is 239 * duplicated). NULL means just copy the 240 * source clientData bits; no proc need be 241 * called. */ 242 AuxDataFreeProc *freeProc; /* Callback procedure to invoke when the aux 243 * data is freed. NULL means no proc need be 244 * called. */ 245 AuxDataPrintProc *printProc;/* Callback function to invoke when printing 246 * the aux data as part of debugging. NULL 247 * means that the data can't be printed. */ 248 AuxDataPrintProc *disassembleProc; 249 /* Callback function to invoke when doing a 250 * disassembly of the aux data (like the 251 * printProc, except that the output is 252 * intended to be script-readable). The 253 * appendObj argument should be filled in with 254 * a descriptive dictionary; it will start out 255 * with "name" mapped to the content of the 256 * name field. NULL means that the printProc 257 * should be used instead. */ 258 } AuxDataType; 259 260 /* 261 * The definition of the AuxData structure that holds information created 262 * during compilation by CompileProcs and used by instructions during 263 * execution. 264 */ 265 266 typedef struct AuxData { 267 const AuxDataType *type; /* Pointer to the AuxData type associated with 268 * this ClientData. */ 269 void *clientData; /* The compilation data itself. */ 270 } AuxData; 271 272 /* 273 * Structure defining the compilation environment. After compilation, fields 274 * describing bytecode instructions are copied out into the more compact 275 * ByteCode structure defined below. 276 */ 277 278 #define COMPILEENV_INIT_CODE_BYTES 250 279 #define COMPILEENV_INIT_NUM_OBJECTS 60 280 #define COMPILEENV_INIT_EXCEPT_RANGES 5 281 #define COMPILEENV_INIT_CMD_MAP_SIZE 40 282 #define COMPILEENV_INIT_AUX_DATA_SIZE 5 283 284 typedef struct CompileEnv { 285 Interp *iPtr; /* Interpreter containing the code being 286 * compiled. Commands and their compile procs 287 * are specific to an interpreter so the code 288 * emitted will depend on the interpreter. */ 289 const char *source; /* The source string being compiled by 290 * SetByteCodeFromAny. This pointer is not 291 * owned by the CompileEnv and must not be 292 * freed or changed by it. */ 293 int numSrcBytes; /* Number of bytes in source. */ 294 Proc *procPtr; /* If a procedure is being compiled, a pointer 295 * to its Proc structure; otherwise NULL. Used 296 * to compile local variables. Set from 297 * information provided by ObjInterpProc in 298 * tclProc.c. */ 299 int numCommands; /* Number of commands compiled. */ 300 int exceptDepth; /* Current exception range nesting level; -1 301 * if not in any range currently. */ 302 int maxExceptDepth; /* Max nesting level of exception ranges; -1 303 * if no ranges have been compiled. */ 304 int maxStackDepth; /* Maximum number of stack elements needed to 305 * execute the code. Set by compilation 306 * procedures before returning. */ 307 int currStackDepth; /* Current stack depth. */ 308 LiteralTable localLitTable; /* Contains LiteralEntry's describing all Tcl 309 * objects referenced by this compiled code. 310 * Indexed by the string representations of 311 * the literals. Used to avoid creating 312 * duplicate objects. */ 313 unsigned char *codeStart; /* Points to the first byte of the code. */ 314 unsigned char *codeNext; /* Points to next code array byte to use. */ 315 unsigned char *codeEnd; /* Points just after the last allocated code 316 * array byte. */ 317 int mallocedCodeArray; /* Set 1 if code array was expanded and 318 * codeStart points into the heap.*/ 319 LiteralEntry *literalArrayPtr; 320 /* Points to start of LiteralEntry array. */ 321 int literalArrayNext; /* Index of next free object array entry. */ 322 int literalArrayEnd; /* Index just after last obj array entry. */ 323 int mallocedLiteralArray; /* 1 if object array was expanded and objArray 324 * points into the heap, else 0. */ 325 ExceptionRange *exceptArrayPtr; 326 /* Points to start of the ExceptionRange 327 * array. */ 328 int exceptArrayNext; /* Next free ExceptionRange array index. 329 * exceptArrayNext is the number of ranges and 330 * (exceptArrayNext-1) is the index of the 331 * current range's array entry. */ 332 int exceptArrayEnd; /* Index after the last ExceptionRange array 333 * entry. */ 334 int mallocedExceptArray; /* 1 if ExceptionRange array was expanded and 335 * exceptArrayPtr points in heap, else 0. */ 336 ExceptionAux *exceptAuxArrayPtr; 337 /* Array of information used to restore the 338 * state when processing BREAK/CONTINUE 339 * exceptions. Must be the same size as the 340 * exceptArrayPtr. */ 341 CmdLocation *cmdMapPtr; /* Points to start of CmdLocation array. 342 * numCommands is the index of the next entry 343 * to use; (numCommands-1) is the entry index 344 * for the last command. */ 345 int cmdMapEnd; /* Index after last CmdLocation entry. */ 346 int mallocedCmdMap; /* 1 if command map array was expanded and 347 * cmdMapPtr points in the heap, else 0. */ 348 AuxData *auxDataArrayPtr; /* Points to auxiliary data array start. */ 349 int auxDataArrayNext; /* Next free compile aux data array index. 350 * auxDataArrayNext is the number of aux data 351 * items and (auxDataArrayNext-1) is index of 352 * current aux data array entry. */ 353 int auxDataArrayEnd; /* Index after last aux data array entry. */ 354 int mallocedAuxDataArray; /* 1 if aux data array was expanded and 355 * auxDataArrayPtr points in heap else 0. */ 356 unsigned char staticCodeSpace[COMPILEENV_INIT_CODE_BYTES]; 357 /* Initial storage for code. */ 358 LiteralEntry staticLiteralSpace[COMPILEENV_INIT_NUM_OBJECTS]; 359 /* Initial storage of LiteralEntry array. */ 360 ExceptionRange staticExceptArraySpace[COMPILEENV_INIT_EXCEPT_RANGES]; 361 /* Initial ExceptionRange array storage. */ 362 ExceptionAux staticExAuxArraySpace[COMPILEENV_INIT_EXCEPT_RANGES]; 363 /* Initial static except auxiliary info array 364 * storage. */ 365 CmdLocation staticCmdMapSpace[COMPILEENV_INIT_CMD_MAP_SIZE]; 366 /* Initial storage for cmd location map. */ 367 AuxData staticAuxDataArraySpace[COMPILEENV_INIT_AUX_DATA_SIZE]; 368 /* Initial storage for aux data array. */ 369 /* TIP #280 */ 370 ExtCmdLoc *extCmdMapPtr; /* Extended command location information for 371 * 'info frame'. */ 372 int line; /* First line of the script, based on the 373 * invoking context, then the line of the 374 * command currently compiled. */ 375 int atCmdStart; /* Flag to say whether an INST_START_CMD 376 * should be issued; they should never be 377 * issued repeatedly, as that is significantly 378 * inefficient. If set to 2, that instruction 379 * should not be issued at all (by the generic 380 * part of the command compiler). */ 381 int expandCount; /* Number of INST_EXPAND_START instructions 382 * encountered that have not yet been paired 383 * with a corresponding 384 * INST_INVOKE_EXPANDED. */ 385 int *clNext; /* If not NULL, it refers to the next slot in 386 * clLoc to check for an invisible 387 * continuation line. */ 388 } CompileEnv; 389 390 /* 391 * The structure defining the bytecode instructions resulting from compiling a 392 * Tcl script. Note that this structure is variable length: a single heap 393 * object is allocated to hold the ByteCode structure immediately followed by 394 * the code bytes, the literal object array, the ExceptionRange array, the 395 * CmdLocation map, and the compilation AuxData array. 396 */ 397 398 /* 399 * A PRECOMPILED bytecode struct is one that was generated from a compiled 400 * image rather than implicitly compiled from source 401 */ 402 403 #define TCL_BYTECODE_PRECOMPILED 0x0001 404 405 /* 406 * When a bytecode is compiled, interp or namespace resolvers have not been 407 * applied yet: this is indicated by the TCL_BYTECODE_RESOLVE_VARS flag. 408 */ 409 410 #define TCL_BYTECODE_RESOLVE_VARS 0x0002 411 412 #define TCL_BYTECODE_RECOMPILE 0x0004 413 414 typedef struct ByteCode { 415 TclHandle interpHandle; /* Handle for interpreter containing the 416 * compiled code. Commands and their compile 417 * procs are specific to an interpreter so the 418 * code emitted will depend on the 419 * interpreter. */ 420 unsigned int compileEpoch; /* Value of iPtr->compileEpoch when this 421 * ByteCode was compiled. Used to invalidate 422 * code when, e.g., commands with compile 423 * procs are redefined. */ 424 Namespace *nsPtr; /* Namespace context in which this code was 425 * compiled. If the code is executed if a 426 * different namespace, it must be 427 * recompiled. */ 428 unsigned int nsEpoch; /* Value of nsPtr->resolverEpoch when this 429 * ByteCode was compiled. Used to invalidate 430 * code when new namespace resolution rules 431 * are put into effect. */ 432 unsigned int refCount; /* Reference count: set 1 when created plus 1 433 * for each execution of the code currently 434 * active. This structure can be freed when 435 * refCount becomes zero. */ 436 unsigned int flags; /* flags describing state for the codebyte. 437 * this variable holds ORed values from the 438 * TCL_BYTECODE_ masks defined above */ 439 const char *source; /* The source string from which this ByteCode 440 * was compiled. Note that this pointer is not 441 * owned by the ByteCode and must not be freed 442 * or modified by it. */ 443 Proc *procPtr; /* If the ByteCode was compiled from a 444 * procedure body, this is a pointer to its 445 * Proc structure; otherwise NULL. This 446 * pointer is also not owned by the ByteCode 447 * and must not be freed by it. */ 448 size_t structureSize; /* Number of bytes in the ByteCode structure 449 * itself. Does not include heap space for 450 * literal Tcl objects or storage referenced 451 * by AuxData entries. */ 452 int numCommands; /* Number of commands compiled. */ 453 int numSrcBytes; /* Number of source bytes compiled. */ 454 int numCodeBytes; /* Number of code bytes. */ 455 int numLitObjects; /* Number of objects in literal array. */ 456 int numExceptRanges; /* Number of ExceptionRange array elems. */ 457 int numAuxDataItems; /* Number of AuxData items. */ 458 int numCmdLocBytes; /* Number of bytes needed for encoded command 459 * location information. */ 460 int maxExceptDepth; /* Maximum nesting level of ExceptionRanges; 461 * -1 if no ranges were compiled. */ 462 int maxStackDepth; /* Maximum number of stack elements needed to 463 * execute the code. */ 464 unsigned char *codeStart; /* Points to the first byte of the code. This 465 * is just after the final ByteCode member 466 * cmdMapPtr. */ 467 Tcl_Obj **objArrayPtr; /* Points to the start of the literal object 468 * array. This is just after the last code 469 * byte. */ 470 ExceptionRange *exceptArrayPtr; 471 /* Points to the start of the ExceptionRange 472 * array. This is just after the last object 473 * in the object array. */ 474 AuxData *auxDataArrayPtr; /* Points to the start of the auxiliary data 475 * array. This is just after the last entry in 476 * the ExceptionRange array. */ 477 unsigned char *codeDeltaStart; 478 /* Points to the first of a sequence of bytes 479 * that encode the change in the starting 480 * offset of each command's code. If -127 <= 481 * delta <= 127, it is encoded as 1 byte, 482 * otherwise 0xFF (128) appears and the delta 483 * is encoded by the next 4 bytes. Code deltas 484 * are always positive. This sequence is just 485 * after the last entry in the AuxData 486 * array. */ 487 unsigned char *codeLengthStart; 488 /* Points to the first of a sequence of bytes 489 * that encode the length of each command's 490 * code. The encoding is the same as for code 491 * deltas. Code lengths are always positive. 492 * This sequence is just after the last entry 493 * in the code delta sequence. */ 494 unsigned char *srcDeltaStart; 495 /* Points to the first of a sequence of bytes 496 * that encode the change in the starting 497 * offset of each command's source. The 498 * encoding is the same as for code deltas. 499 * Source deltas can be negative. This 500 * sequence is just after the last byte in the 501 * code length sequence. */ 502 unsigned char *srcLengthStart; 503 /* Points to the first of a sequence of bytes 504 * that encode the length of each command's 505 * source. The encoding is the same as for 506 * code deltas. Source lengths are always 507 * positive. This sequence is just after the 508 * last byte in the source delta sequence. */ 509 LocalCache *localCachePtr; /* Pointer to the start of the cached variable 510 * names and initialisation data for local 511 * variables. */ 512 #ifdef TCL_COMPILE_STATS 513 Tcl_Time createTime; /* Absolute time when the ByteCode was 514 * created. */ 515 #endif /* TCL_COMPILE_STATS */ 516 } ByteCode; 517 518 #define ByteCodeSetIntRep(objPtr, typePtr, codePtr) \ 519 do { \ 520 Tcl_ObjIntRep ir; \ 521 ir.twoPtrValue.ptr1 = (codePtr); \ 522 ir.twoPtrValue.ptr2 = NULL; \ 523 Tcl_StoreIntRep((objPtr), (typePtr), &ir); \ 524 } while (0) 525 526 527 528 #define ByteCodeGetIntRep(objPtr, typePtr, codePtr) \ 529 do { \ 530 const Tcl_ObjIntRep *irPtr; \ 531 irPtr = TclFetchIntRep((objPtr), (typePtr)); \ 532 (codePtr) = irPtr ? (ByteCode*)irPtr->twoPtrValue.ptr1 : NULL; \ 533 } while (0) 534 535 /* 536 * Opcodes for the Tcl bytecode instructions. These must correspond to the 537 * entries in the table of instruction descriptions, tclInstructionTable, in 538 * tclCompile.c. Also, the order and number of the expression opcodes (e.g., 539 * INST_LOR) must match the entries in the array operatorStrings in 540 * tclExecute.c. 541 */ 542 543 /* Opcodes 0 to 9 */ 544 #define INST_DONE 0 545 #define INST_PUSH1 1 546 #define INST_PUSH4 2 547 #define INST_POP 3 548 #define INST_DUP 4 549 #define INST_STR_CONCAT1 5 550 #define INST_INVOKE_STK1 6 551 #define INST_INVOKE_STK4 7 552 #define INST_EVAL_STK 8 553 #define INST_EXPR_STK 9 554 555 /* Opcodes 10 to 23 */ 556 #define INST_LOAD_SCALAR1 10 557 #define INST_LOAD_SCALAR4 11 558 #define INST_LOAD_SCALAR_STK 12 559 #define INST_LOAD_ARRAY1 13 560 #define INST_LOAD_ARRAY4 14 561 #define INST_LOAD_ARRAY_STK 15 562 #define INST_LOAD_STK 16 563 #define INST_STORE_SCALAR1 17 564 #define INST_STORE_SCALAR4 18 565 #define INST_STORE_SCALAR_STK 19 566 #define INST_STORE_ARRAY1 20 567 #define INST_STORE_ARRAY4 21 568 #define INST_STORE_ARRAY_STK 22 569 #define INST_STORE_STK 23 570 571 /* Opcodes 24 to 33 */ 572 #define INST_INCR_SCALAR1 24 573 #define INST_INCR_SCALAR_STK 25 574 #define INST_INCR_ARRAY1 26 575 #define INST_INCR_ARRAY_STK 27 576 #define INST_INCR_STK 28 577 #define INST_INCR_SCALAR1_IMM 29 578 #define INST_INCR_SCALAR_STK_IMM 30 579 #define INST_INCR_ARRAY1_IMM 31 580 #define INST_INCR_ARRAY_STK_IMM 32 581 #define INST_INCR_STK_IMM 33 582 583 /* Opcodes 34 to 39 */ 584 #define INST_JUMP1 34 585 #define INST_JUMP4 35 586 #define INST_JUMP_TRUE1 36 587 #define INST_JUMP_TRUE4 37 588 #define INST_JUMP_FALSE1 38 589 #define INST_JUMP_FALSE4 39 590 591 /* Opcodes 40 to 64 */ 592 #define INST_LOR 40 593 #define INST_LAND 41 594 #define INST_BITOR 42 595 #define INST_BITXOR 43 596 #define INST_BITAND 44 597 #define INST_EQ 45 598 #define INST_NEQ 46 599 #define INST_LT 47 600 #define INST_GT 48 601 #define INST_LE 49 602 #define INST_GE 50 603 #define INST_LSHIFT 51 604 #define INST_RSHIFT 52 605 #define INST_ADD 53 606 #define INST_SUB 54 607 #define INST_MULT 55 608 #define INST_DIV 56 609 #define INST_MOD 57 610 #define INST_UPLUS 58 611 #define INST_UMINUS 59 612 #define INST_BITNOT 60 613 #define INST_LNOT 61 614 #define INST_CALL_BUILTIN_FUNC1 62 615 #define INST_CALL_FUNC1 63 616 #define INST_TRY_CVT_TO_NUMERIC 64 617 618 /* Opcodes 65 to 66 */ 619 #define INST_BREAK 65 620 #define INST_CONTINUE 66 621 622 /* Opcodes 67 to 68 */ 623 #define INST_FOREACH_START4 67 /* DEPRECATED */ 624 #define INST_FOREACH_STEP4 68 /* DEPRECATED */ 625 626 /* Opcodes 69 to 72 */ 627 #define INST_BEGIN_CATCH4 69 628 #define INST_END_CATCH 70 629 #define INST_PUSH_RESULT 71 630 #define INST_PUSH_RETURN_CODE 72 631 632 /* Opcodes 73 to 78 */ 633 #define INST_STR_EQ 73 634 #define INST_STR_NEQ 74 635 #define INST_STR_CMP 75 636 #define INST_STR_LEN 76 637 #define INST_STR_INDEX 77 638 #define INST_STR_MATCH 78 639 640 /* Opcodes 78 to 81 */ 641 #define INST_LIST 79 642 #define INST_LIST_INDEX 80 643 #define INST_LIST_LENGTH 81 644 645 /* Opcodes 82 to 87 */ 646 #define INST_APPEND_SCALAR1 82 647 #define INST_APPEND_SCALAR4 83 648 #define INST_APPEND_ARRAY1 84 649 #define INST_APPEND_ARRAY4 85 650 #define INST_APPEND_ARRAY_STK 86 651 #define INST_APPEND_STK 87 652 653 /* Opcodes 88 to 93 */ 654 #define INST_LAPPEND_SCALAR1 88 655 #define INST_LAPPEND_SCALAR4 89 656 #define INST_LAPPEND_ARRAY1 90 657 #define INST_LAPPEND_ARRAY4 91 658 #define INST_LAPPEND_ARRAY_STK 92 659 #define INST_LAPPEND_STK 93 660 661 /* TIP #22 - LINDEX operator with flat arg list */ 662 663 #define INST_LIST_INDEX_MULTI 94 664 665 /* 666 * TIP #33 - 'lset' command. Code gen also required a Forth-like 667 * OVER operation. 668 */ 669 670 #define INST_OVER 95 671 #define INST_LSET_LIST 96 672 #define INST_LSET_FLAT 97 673 674 /* TIP#90 - 'return' command. */ 675 676 #define INST_RETURN_IMM 98 677 678 /* TIP#123 - exponentiation operator. */ 679 680 #define INST_EXPON 99 681 682 /* TIP #157 - {*}... (word expansion) language syntax support. */ 683 684 #define INST_EXPAND_START 100 685 #define INST_EXPAND_STKTOP 101 686 #define INST_INVOKE_EXPANDED 102 687 688 /* 689 * TIP #57 - 'lassign' command. Code generation requires immediate 690 * LINDEX and LRANGE operators. 691 */ 692 693 #define INST_LIST_INDEX_IMM 103 694 #define INST_LIST_RANGE_IMM 104 695 696 #define INST_START_CMD 105 697 698 #define INST_LIST_IN 106 699 #define INST_LIST_NOT_IN 107 700 701 #define INST_PUSH_RETURN_OPTIONS 108 702 #define INST_RETURN_STK 109 703 704 /* 705 * Dictionary (TIP#111) related commands. 706 */ 707 708 #define INST_DICT_GET 110 709 #define INST_DICT_SET 111 710 #define INST_DICT_UNSET 112 711 #define INST_DICT_INCR_IMM 113 712 #define INST_DICT_APPEND 114 713 #define INST_DICT_LAPPEND 115 714 #define INST_DICT_FIRST 116 715 #define INST_DICT_NEXT 117 716 #define INST_DICT_DONE 118 717 #define INST_DICT_UPDATE_START 119 718 #define INST_DICT_UPDATE_END 120 719 720 /* 721 * Instruction to support jumps defined by tables (instead of the classic 722 * [switch] technique of chained comparisons). 723 */ 724 725 #define INST_JUMP_TABLE 121 726 727 /* 728 * Instructions to support compilation of global, variable, upvar and 729 * [namespace upvar]. 730 */ 731 732 #define INST_UPVAR 122 733 #define INST_NSUPVAR 123 734 #define INST_VARIABLE 124 735 736 /* Instruction to support compiling syntax error to bytecode */ 737 738 #define INST_SYNTAX 125 739 740 /* Instruction to reverse N items on top of stack */ 741 742 #define INST_REVERSE 126 743 744 /* regexp instruction */ 745 746 #define INST_REGEXP 127 747 748 /* For [info exists] compilation */ 749 #define INST_EXIST_SCALAR 128 750 #define INST_EXIST_ARRAY 129 751 #define INST_EXIST_ARRAY_STK 130 752 #define INST_EXIST_STK 131 753 754 /* For [subst] compilation */ 755 #define INST_NOP 132 756 #define INST_RETURN_CODE_BRANCH 133 757 758 /* For [unset] compilation */ 759 #define INST_UNSET_SCALAR 134 760 #define INST_UNSET_ARRAY 135 761 #define INST_UNSET_ARRAY_STK 136 762 #define INST_UNSET_STK 137 763 764 /* For [dict with], [dict exists], [dict create] and [dict merge] */ 765 #define INST_DICT_EXPAND 138 766 #define INST_DICT_RECOMBINE_STK 139 767 #define INST_DICT_RECOMBINE_IMM 140 768 #define INST_DICT_EXISTS 141 769 #define INST_DICT_VERIFY 142 770 771 /* For [string map] and [regsub] compilation */ 772 #define INST_STR_MAP 143 773 #define INST_STR_FIND 144 774 #define INST_STR_FIND_LAST 145 775 #define INST_STR_RANGE_IMM 146 776 #define INST_STR_RANGE 147 777 778 /* For operations to do with coroutines and other NRE-manipulators */ 779 #define INST_YIELD 148 780 #define INST_COROUTINE_NAME 149 781 #define INST_TAILCALL 150 782 783 /* For compilation of basic information operations */ 784 #define INST_NS_CURRENT 151 785 #define INST_INFO_LEVEL_NUM 152 786 #define INST_INFO_LEVEL_ARGS 153 787 #define INST_RESOLVE_COMMAND 154 788 789 /* For compilation relating to TclOO */ 790 #define INST_TCLOO_SELF 155 791 #define INST_TCLOO_CLASS 156 792 #define INST_TCLOO_NS 157 793 #define INST_TCLOO_IS_OBJECT 158 794 795 /* For compilation of [array] subcommands */ 796 #define INST_ARRAY_EXISTS_STK 159 797 #define INST_ARRAY_EXISTS_IMM 160 798 #define INST_ARRAY_MAKE_STK 161 799 #define INST_ARRAY_MAKE_IMM 162 800 801 #define INST_INVOKE_REPLACE 163 802 803 #define INST_LIST_CONCAT 164 804 805 #define INST_EXPAND_DROP 165 806 807 /* New foreach implementation */ 808 #define INST_FOREACH_START 166 809 #define INST_FOREACH_STEP 167 810 #define INST_FOREACH_END 168 811 #define INST_LMAP_COLLECT 169 812 813 /* For compilation of [string trim] and related */ 814 #define INST_STR_TRIM 170 815 #define INST_STR_TRIM_LEFT 171 816 #define INST_STR_TRIM_RIGHT 172 817 818 #define INST_CONCAT_STK 173 819 820 #define INST_STR_UPPER 174 821 #define INST_STR_LOWER 175 822 #define INST_STR_TITLE 176 823 #define INST_STR_REPLACE 177 824 825 #define INST_ORIGIN_COMMAND 178 826 827 #define INST_TCLOO_NEXT 179 828 #define INST_TCLOO_NEXT_CLASS 180 829 830 #define INST_YIELD_TO_INVOKE 181 831 832 #define INST_NUM_TYPE 182 833 #define INST_TRY_CVT_TO_BOOLEAN 183 834 #define INST_STR_CLASS 184 835 836 #define INST_LAPPEND_LIST 185 837 #define INST_LAPPEND_LIST_ARRAY 186 838 #define INST_LAPPEND_LIST_ARRAY_STK 187 839 #define INST_LAPPEND_LIST_STK 188 840 841 #define INST_CLOCK_READ 189 842 843 #define INST_DICT_GET_DEF 190 844 845 /* TIP 461 */ 846 #define INST_STR_LT 191 847 #define INST_STR_GT 192 848 #define INST_STR_LE 193 849 #define INST_STR_GE 194 850 851 /* The last opcode */ 852 #define LAST_INST_OPCODE 194 853 854 /* 855 * Table describing the Tcl bytecode instructions: their name (for displaying 856 * code), total number of code bytes required (including operand bytes), and a 857 * description of the type of each operand. These operand types include signed 858 * and unsigned integers of length one and four bytes. The unsigned integers 859 * are used for indexes or for, e.g., the count of objects to push in a "push" 860 * instruction. 861 */ 862 863 #define MAX_INSTRUCTION_OPERANDS 2 864 865 typedef enum InstOperandType { 866 OPERAND_NONE, 867 OPERAND_INT1, /* One byte signed integer. */ 868 OPERAND_INT4, /* Four byte signed integer. */ 869 OPERAND_UINT1, /* One byte unsigned integer. */ 870 OPERAND_UINT4, /* Four byte unsigned integer. */ 871 OPERAND_IDX4, /* Four byte signed index (actually an 872 * integer, but displayed differently.) */ 873 OPERAND_LVT1, /* One byte unsigned index into the local 874 * variable table. */ 875 OPERAND_LVT4, /* Four byte unsigned index into the local 876 * variable table. */ 877 OPERAND_AUX4, /* Four byte unsigned index into the aux data 878 * table. */ 879 OPERAND_OFFSET1, /* One byte signed jump offset. */ 880 OPERAND_OFFSET4, /* Four byte signed jump offset. */ 881 OPERAND_LIT1, /* One byte unsigned index into table of 882 * literals. */ 883 OPERAND_LIT4, /* Four byte unsigned index into table of 884 * literals. */ 885 OPERAND_SCLS1 /* Index into tclStringClassTable. */ 886 } InstOperandType; 887 888 typedef struct InstructionDesc { 889 const char *name; /* Name of instruction. */ 890 int numBytes; /* Total number of bytes for instruction. */ 891 int stackEffect; /* The worst-case balance stack effect of the 892 * instruction, used for stack requirements 893 * computations. The value INT_MIN signals 894 * that the instruction's worst case effect is 895 * (1-opnd1). */ 896 int numOperands; /* Number of operands. */ 897 InstOperandType opTypes[MAX_INSTRUCTION_OPERANDS]; 898 /* The type of each operand. */ 899 } InstructionDesc; 900 901 MODULE_SCOPE InstructionDesc const tclInstructionTable[]; 902 903 /* 904 * Constants used by INST_STRING_CLASS to indicate character classes. These 905 * correspond closely by name with what [string is] can support, but there is 906 * no requirement to keep the values the same. 907 */ 908 909 typedef enum InstStringClassType { 910 STR_CLASS_ALNUM, /* Unicode alphabet or digit characters. */ 911 STR_CLASS_ALPHA, /* Unicode alphabet characters. */ 912 STR_CLASS_ASCII, /* Characters in range U+000000..U+00007F. */ 913 STR_CLASS_CONTROL, /* Unicode control characters. */ 914 STR_CLASS_DIGIT, /* Unicode digit characters. */ 915 STR_CLASS_GRAPH, /* Unicode printing characters, excluding 916 * space. */ 917 STR_CLASS_LOWER, /* Unicode lower-case alphabet characters. */ 918 STR_CLASS_PRINT, /* Unicode printing characters, including 919 * spaces. */ 920 STR_CLASS_PUNCT, /* Unicode punctuation characters. */ 921 STR_CLASS_SPACE, /* Unicode space characters. */ 922 STR_CLASS_UPPER, /* Unicode upper-case alphabet characters. */ 923 STR_CLASS_WORD, /* Unicode word (alphabetic, digit, connector 924 * punctuation) characters. */ 925 STR_CLASS_XDIGIT, /* Characters that can be used as digits in 926 * hexadecimal numbers ([0-9A-Fa-f]). */ 927 STR_CLASS_UNICODE /* Unicode characters. */ 928 } InstStringClassType; 929 930 typedef struct StringClassDesc { 931 char name[8]; /* Name of the class. */ 932 int (*comparator)(int); /* Function to test if a single unicode 933 * character is a member of the class. */ 934 } StringClassDesc; 935 936 MODULE_SCOPE StringClassDesc const tclStringClassTable[]; 937 938 /* 939 * Compilation of some Tcl constructs such as if commands and the logical or 940 * (||) and logical and (&&) operators in expressions requires the generation 941 * of forward jumps. Since the PC target of these jumps isn't known when the 942 * jumps are emitted, we record the offset of each jump in an array of 943 * JumpFixup structures. There is one array for each sequence of jumps to one 944 * target PC. When we learn the target PC, we update the jumps with the 945 * correct distance. Also, if the distance is too great (> 127 bytes), we 946 * replace the single-byte jump with a four byte jump instruction, move the 947 * instructions after the jump down, and update the code offsets for any 948 * commands between the jump and the target. 949 */ 950 951 typedef enum { 952 TCL_UNCONDITIONAL_JUMP, 953 TCL_TRUE_JUMP, 954 TCL_FALSE_JUMP 955 } TclJumpType; 956 957 typedef struct JumpFixup { 958 TclJumpType jumpType; /* Indicates the kind of jump. */ 959 unsigned int codeOffset; /* Offset of the first byte of the one-byte 960 * forward jump's code. */ 961 int cmdIndex; /* Index of the first command after the one 962 * for which the jump was emitted. Used to 963 * update the code offsets for subsequent 964 * commands if the two-byte jump at jumpPc 965 * must be replaced with a five-byte one. */ 966 int exceptIndex; /* Index of the first range entry in the 967 * ExceptionRange array after the current one. 968 * This field is used to adjust the code 969 * offsets in subsequent ExceptionRange 970 * records when a jump is grown from 2 bytes 971 * to 5 bytes. */ 972 } JumpFixup; 973 974 #define JUMPFIXUP_INIT_ENTRIES 10 975 976 typedef struct JumpFixupArray { 977 JumpFixup *fixup; /* Points to start of jump fixup array. */ 978 int next; /* Index of next free array entry. */ 979 int end; /* Index of last usable entry in array. */ 980 int mallocedArray; /* 1 if array was expanded and fixups points 981 * into the heap, else 0. */ 982 JumpFixup staticFixupSpace[JUMPFIXUP_INIT_ENTRIES]; 983 /* Initial storage for jump fixup array. */ 984 } JumpFixupArray; 985 986 /* 987 * The structure describing one variable list of a foreach command. Note that 988 * only foreach commands inside procedure bodies are compiled inline so a 989 * ForeachVarList structure always describes local variables. Furthermore, 990 * only scalar variables are supported for inline-compiled foreach loops. 991 */ 992 993 typedef struct ForeachVarList { 994 int numVars; /* The number of variables in the list. */ 995 int varIndexes[TCLFLEXARRAY];/* An array of the indexes ("slot numbers") 996 * for each variable in the procedure's array 997 * of local variables. Only scalar variables 998 * are supported. The actual size of this 999 * field will be large enough to numVars 1000 * indexes. THIS MUST BE THE LAST FIELD IN THE 1001 * STRUCTURE! */ 1002 } ForeachVarList; 1003 1004 /* 1005 * Structure used to hold information about a foreach command that is needed 1006 * during program execution. These structures are stored in CompileEnv and 1007 * ByteCode structures as auxiliary data. 1008 */ 1009 1010 typedef struct ForeachInfo { 1011 int numLists; /* The number of both the variable and value 1012 * lists of the foreach command. */ 1013 int firstValueTemp; /* Index of the first temp var in a proc frame 1014 * used to point to a value list. */ 1015 int loopCtTemp; /* Index of temp var in a proc frame holding 1016 * the loop's iteration count. Used to 1017 * determine next value list element to assign 1018 * each loop var. */ 1019 ForeachVarList *varLists[TCLFLEXARRAY];/* An array of pointers to ForeachVarList 1020 * structures describing each var list. The 1021 * actual size of this field will be large 1022 * enough to numVars indexes. THIS MUST BE THE 1023 * LAST FIELD IN THE STRUCTURE! */ 1024 } ForeachInfo; 1025 1026 /* 1027 * Structure used to hold information about a switch command that is needed 1028 * during program execution. These structures are stored in CompileEnv and 1029 * ByteCode structures as auxiliary data. 1030 */ 1031 1032 typedef struct JumptableInfo { 1033 Tcl_HashTable hashTable; /* Hash that maps strings to signed ints (PC 1034 * offsets). */ 1035 } JumptableInfo; 1036 1037 MODULE_SCOPE const AuxDataType tclJumptableInfoType; 1038 1039 #define JUMPTABLEINFO(envPtr, index) \ 1040 ((JumptableInfo*)((envPtr)->auxDataArrayPtr[TclGetUInt4AtPtr(index)].clientData)) 1041 1042 /* 1043 * Structure used to hold information about a [dict update] command that is 1044 * needed during program execution. These structures are stored in CompileEnv 1045 * and ByteCode structures as auxiliary data. 1046 */ 1047 1048 typedef struct { 1049 int length; /* Size of array */ 1050 int varIndices[TCLFLEXARRAY]; /* Array of variable indices to manage when 1051 * processing the start and end of a [dict 1052 * update]. There is really more than one 1053 * entry, and the structure is allocated to 1054 * take account of this. MUST BE LAST FIELD IN 1055 * STRUCTURE. */ 1056 } DictUpdateInfo; 1057 1058 /* 1059 * ClientData type used by the math operator commands. 1060 */ 1061 1062 typedef struct { 1063 const char *op; /* Do not call it 'operator': C++ reserved */ 1064 const char *expected; 1065 union { 1066 int numArgs; 1067 int identity; 1068 } i; 1069 } TclOpCmdClientData; 1070 1071 /* 1072 *---------------------------------------------------------------- 1073 * Procedures exported by tclBasic.c to be used within the engine. 1074 *---------------------------------------------------------------- 1075 */ 1076 1077 MODULE_SCOPE Tcl_ObjCmdProc TclNRInterpCoroutine; 1078 1079 /* 1080 *---------------------------------------------------------------- 1081 * Procedures exported by the engine to be used by tclBasic.c 1082 *---------------------------------------------------------------- 1083 */ 1084 1085 MODULE_SCOPE ByteCode * TclCompileObj(Tcl_Interp *interp, Tcl_Obj *objPtr, 1086 const CmdFrame *invoker, int word); 1087 1088 /* 1089 *---------------------------------------------------------------- 1090 * Procedures shared among Tcl bytecode compilation and execution modules but 1091 * not used outside: 1092 *---------------------------------------------------------------- 1093 */ 1094 1095 MODULE_SCOPE int TclAttemptCompileProc(Tcl_Interp *interp, 1096 Tcl_Parse *parsePtr, int depth, Command *cmdPtr, 1097 CompileEnv *envPtr); 1098 MODULE_SCOPE void TclCleanupStackForBreakContinue(CompileEnv *envPtr, 1099 ExceptionAux *auxPtr); 1100 MODULE_SCOPE void TclCompileCmdWord(Tcl_Interp *interp, 1101 Tcl_Token *tokenPtr, int count, 1102 CompileEnv *envPtr); 1103 MODULE_SCOPE void TclCompileExpr(Tcl_Interp *interp, const char *script, 1104 int numBytes, CompileEnv *envPtr, int optimize); 1105 MODULE_SCOPE void TclCompileExprWords(Tcl_Interp *interp, 1106 Tcl_Token *tokenPtr, int numWords, 1107 CompileEnv *envPtr); 1108 MODULE_SCOPE void TclCompileInvocation(Tcl_Interp *interp, 1109 Tcl_Token *tokenPtr, Tcl_Obj *cmdObj, int numWords, 1110 CompileEnv *envPtr); 1111 MODULE_SCOPE void TclCompileScript(Tcl_Interp *interp, 1112 const char *script, int numBytes, 1113 CompileEnv *envPtr); 1114 MODULE_SCOPE void TclCompileSyntaxError(Tcl_Interp *interp, 1115 CompileEnv *envPtr); 1116 MODULE_SCOPE void TclCompileTokens(Tcl_Interp *interp, 1117 Tcl_Token *tokenPtr, int count, 1118 CompileEnv *envPtr); 1119 MODULE_SCOPE void TclCompileVarSubst(Tcl_Interp *interp, 1120 Tcl_Token *tokenPtr, CompileEnv *envPtr); 1121 MODULE_SCOPE int TclCreateAuxData(ClientData clientData, 1122 const AuxDataType *typePtr, CompileEnv *envPtr); 1123 MODULE_SCOPE int TclCreateExceptRange(ExceptionRangeType type, 1124 CompileEnv *envPtr); 1125 MODULE_SCOPE ExecEnv * TclCreateExecEnv(Tcl_Interp *interp, int size); 1126 MODULE_SCOPE Tcl_Obj * TclCreateLiteral(Interp *iPtr, const char *bytes, 1127 int length, unsigned int hash, int *newPtr, 1128 Namespace *nsPtr, int flags, 1129 LiteralEntry **globalPtrPtr); 1130 MODULE_SCOPE void TclDeleteExecEnv(ExecEnv *eePtr); 1131 MODULE_SCOPE void TclDeleteLiteralTable(Tcl_Interp *interp, 1132 LiteralTable *tablePtr); 1133 MODULE_SCOPE void TclEmitForwardJump(CompileEnv *envPtr, 1134 TclJumpType jumpType, JumpFixup *jumpFixupPtr); 1135 MODULE_SCOPE void TclEmitInvoke(CompileEnv *envPtr, int opcode, ...); 1136 MODULE_SCOPE ExceptionRange * TclGetExceptionRangeForPc(unsigned char *pc, 1137 int catchOnly, ByteCode *codePtr); 1138 MODULE_SCOPE void TclExpandJumpFixupArray(JumpFixupArray *fixupArrayPtr); 1139 MODULE_SCOPE int TclNRExecuteByteCode(Tcl_Interp *interp, 1140 ByteCode *codePtr); 1141 MODULE_SCOPE Tcl_Obj * TclFetchLiteral(CompileEnv *envPtr, unsigned int index); 1142 MODULE_SCOPE int TclFindCompiledLocal(const char *name, int nameChars, 1143 int create, CompileEnv *envPtr); 1144 MODULE_SCOPE int TclFixupForwardJump(CompileEnv *envPtr, 1145 JumpFixup *jumpFixupPtr, int jumpDist, 1146 int distThreshold); 1147 MODULE_SCOPE void TclFreeCompileEnv(CompileEnv *envPtr); 1148 MODULE_SCOPE void TclFreeJumpFixupArray(JumpFixupArray *fixupArrayPtr); 1149 MODULE_SCOPE int TclGetIndexFromToken(Tcl_Token *tokenPtr, 1150 int before, int after, int *indexPtr); 1151 MODULE_SCOPE ByteCode * TclInitByteCode(CompileEnv *envPtr); 1152 MODULE_SCOPE ByteCode * TclInitByteCodeObj(Tcl_Obj *objPtr, 1153 const Tcl_ObjType *typePtr, CompileEnv *envPtr); 1154 MODULE_SCOPE void TclInitCompileEnv(Tcl_Interp *interp, 1155 CompileEnv *envPtr, const char *string, 1156 int numBytes, const CmdFrame *invoker, int word); 1157 MODULE_SCOPE void TclInitJumpFixupArray(JumpFixupArray *fixupArrayPtr); 1158 MODULE_SCOPE void TclInitLiteralTable(LiteralTable *tablePtr); 1159 MODULE_SCOPE ExceptionRange *TclGetInnermostExceptionRange(CompileEnv *envPtr, 1160 int returnCode, ExceptionAux **auxPtrPtr); 1161 MODULE_SCOPE void TclAddLoopBreakFixup(CompileEnv *envPtr, 1162 ExceptionAux *auxPtr); 1163 MODULE_SCOPE void TclAddLoopContinueFixup(CompileEnv *envPtr, 1164 ExceptionAux *auxPtr); 1165 MODULE_SCOPE void TclFinalizeLoopExceptionRange(CompileEnv *envPtr, 1166 int range); 1167 #ifdef TCL_COMPILE_STATS 1168 MODULE_SCOPE char * TclLiteralStats(LiteralTable *tablePtr); 1169 MODULE_SCOPE int TclLog2(int value); 1170 #endif 1171 MODULE_SCOPE int TclLocalScalar(const char *bytes, int numBytes, 1172 CompileEnv *envPtr); 1173 MODULE_SCOPE int TclLocalScalarFromToken(Tcl_Token *tokenPtr, 1174 CompileEnv *envPtr); 1175 MODULE_SCOPE void TclOptimizeBytecode(void *envPtr); 1176 #ifdef TCL_COMPILE_DEBUG 1177 MODULE_SCOPE void TclPrintByteCodeObj(Tcl_Interp *interp, 1178 Tcl_Obj *objPtr); 1179 #endif 1180 MODULE_SCOPE int TclPrintInstruction(ByteCode *codePtr, 1181 const unsigned char *pc); 1182 MODULE_SCOPE void TclPrintObject(FILE *outFile, 1183 Tcl_Obj *objPtr, int maxChars); 1184 MODULE_SCOPE void TclPrintSource(FILE *outFile, 1185 const char *string, int maxChars); 1186 MODULE_SCOPE void TclPushVarName(Tcl_Interp *interp, 1187 Tcl_Token *varTokenPtr, CompileEnv *envPtr, 1188 int flags, int *localIndexPtr, 1189 int *isScalarPtr); 1190 MODULE_SCOPE void TclPreserveByteCode(ByteCode *codePtr); 1191 MODULE_SCOPE void TclReleaseByteCode(ByteCode *codePtr); 1192 MODULE_SCOPE void TclReleaseLiteral(Tcl_Interp *interp, Tcl_Obj *objPtr); 1193 MODULE_SCOPE void TclInvalidateCmdLiteral(Tcl_Interp *interp, 1194 const char *name, Namespace *nsPtr); 1195 MODULE_SCOPE int TclSingleOpCmd(ClientData clientData, 1196 Tcl_Interp *interp, int objc, 1197 Tcl_Obj *const objv[]); 1198 MODULE_SCOPE int TclSortingOpCmd(ClientData clientData, 1199 Tcl_Interp *interp, int objc, 1200 Tcl_Obj *const objv[]); 1201 MODULE_SCOPE int TclVariadicOpCmd(ClientData clientData, 1202 Tcl_Interp *interp, int objc, 1203 Tcl_Obj *const objv[]); 1204 MODULE_SCOPE int TclNoIdentOpCmd(ClientData clientData, 1205 Tcl_Interp *interp, int objc, 1206 Tcl_Obj *const objv[]); 1207 #ifdef TCL_COMPILE_DEBUG 1208 MODULE_SCOPE void TclVerifyGlobalLiteralTable(Interp *iPtr); 1209 MODULE_SCOPE void TclVerifyLocalLiteralTable(CompileEnv *envPtr); 1210 #endif 1211 MODULE_SCOPE int TclWordKnownAtCompileTime(Tcl_Token *tokenPtr, 1212 Tcl_Obj *valuePtr); 1213 MODULE_SCOPE void TclLogCommandInfo(Tcl_Interp *interp, 1214 const char *script, const char *command, 1215 int length, const unsigned char *pc, 1216 Tcl_Obj **tosPtr); 1217 MODULE_SCOPE Tcl_Obj *TclGetInnerContext(Tcl_Interp *interp, 1218 const unsigned char *pc, Tcl_Obj **tosPtr); 1219 MODULE_SCOPE Tcl_Obj *TclNewInstNameObj(unsigned char inst); 1220 MODULE_SCOPE int TclPushProcCallFrame(ClientData clientData, 1221 Tcl_Interp *interp, int objc, 1222 Tcl_Obj *const objv[], int isLambda); 1223 1224 1225 /* 1226 *---------------------------------------------------------------- 1227 * Macros and flag values used by Tcl bytecode compilation and execution 1228 * modules inside the Tcl core but not used outside. 1229 *---------------------------------------------------------------- 1230 */ 1231 1232 /* 1233 * Simplified form to access AuxData. 1234 * 1235 * ClientData TclFetchAuxData(CompileEng *envPtr, int index); 1236 */ 1237 1238 #define TclFetchAuxData(envPtr, index) \ 1239 (envPtr)->auxDataArrayPtr[(index)].clientData 1240 1241 #define LITERAL_ON_HEAP 0x01 1242 #define LITERAL_CMD_NAME 0x02 1243 #define LITERAL_UNSHARED 0x04 1244 1245 /* 1246 * Macro used to manually adjust the stack requirements; used in cases where 1247 * the stack effect cannot be computed from the opcode and its operands, but 1248 * is still known at compile time. 1249 * 1250 * void TclAdjustStackDepth(int delta, CompileEnv *envPtr); 1251 */ 1252 1253 #define TclAdjustStackDepth(delta, envPtr) \ 1254 do { \ 1255 if ((delta) < 0) { \ 1256 if ((envPtr)->maxStackDepth < (envPtr)->currStackDepth) { \ 1257 (envPtr)->maxStackDepth = (envPtr)->currStackDepth; \ 1258 } \ 1259 } \ 1260 (envPtr)->currStackDepth += (delta); \ 1261 } while (0) 1262 1263 #define TclGetStackDepth(envPtr) \ 1264 ((envPtr)->currStackDepth) 1265 1266 #define TclSetStackDepth(depth, envPtr) \ 1267 (envPtr)->currStackDepth = (depth) 1268 1269 #define TclCheckStackDepth(depth, envPtr) \ 1270 do { \ 1271 int _dd = (depth); \ 1272 if (_dd != (envPtr)->currStackDepth) { \ 1273 Tcl_Panic("bad stack depth computations: is %i, should be %i", \ 1274 (envPtr)->currStackDepth, _dd); \ 1275 } \ 1276 } while (0) 1277 1278 /* 1279 * Macro used to update the stack requirements. It is called by the macros 1280 * TclEmitOpCode, TclEmitInst1 and TclEmitInst4. 1281 * Remark that the very last instruction of a bytecode always reduces the 1282 * stack level: INST_DONE or INST_POP, so that the maxStackdepth is always 1283 * updated. 1284 * 1285 * void TclUpdateStackReqs(unsigned char op, int i, CompileEnv *envPtr); 1286 */ 1287 1288 #define TclUpdateStackReqs(op, i, envPtr) \ 1289 do { \ 1290 int _delta = tclInstructionTable[(op)].stackEffect; \ 1291 if (_delta) { \ 1292 if (_delta == INT_MIN) { \ 1293 _delta = 1 - (i); \ 1294 } \ 1295 TclAdjustStackDepth(_delta, envPtr); \ 1296 } \ 1297 } while (0) 1298 1299 /* 1300 * Macros used to update the flag that indicates if we are at the start of a 1301 * command, based on whether the opcode is INST_START_COMMAND. 1302 * 1303 * void TclUpdateAtCmdStart(unsigned char op, CompileEnv *envPtr); 1304 */ 1305 1306 #define TclUpdateAtCmdStart(op, envPtr) \ 1307 if ((envPtr)->atCmdStart < 2) { \ 1308 (envPtr)->atCmdStart = ((op) == INST_START_CMD ? 1 : 0); \ 1309 } 1310 1311 /* 1312 * Macro to emit an opcode byte into a CompileEnv's code array. The ANSI C 1313 * "prototype" for this macro is: 1314 * 1315 * void TclEmitOpcode(unsigned char op, CompileEnv *envPtr); 1316 */ 1317 1318 #define TclEmitOpcode(op, envPtr) \ 1319 do { \ 1320 if ((envPtr)->codeNext == (envPtr)->codeEnd) { \ 1321 TclExpandCodeArray(envPtr); \ 1322 } \ 1323 *(envPtr)->codeNext++ = (unsigned char) (op); \ 1324 TclUpdateAtCmdStart(op, envPtr); \ 1325 TclUpdateStackReqs(op, 0, envPtr); \ 1326 } while (0) 1327 1328 /* 1329 * Macros to emit an integer operand. The ANSI C "prototype" for these macros 1330 * are: 1331 * 1332 * void TclEmitInt1(int i, CompileEnv *envPtr); 1333 * void TclEmitInt4(int i, CompileEnv *envPtr); 1334 */ 1335 1336 #define TclEmitInt1(i, envPtr) \ 1337 do { \ 1338 if ((envPtr)->codeNext == (envPtr)->codeEnd) { \ 1339 TclExpandCodeArray(envPtr); \ 1340 } \ 1341 *(envPtr)->codeNext++ = (unsigned char) ((unsigned int) (i)); \ 1342 } while (0) 1343 1344 #define TclEmitInt4(i, envPtr) \ 1345 do { \ 1346 if (((envPtr)->codeNext + 4) > (envPtr)->codeEnd) { \ 1347 TclExpandCodeArray(envPtr); \ 1348 } \ 1349 *(envPtr)->codeNext++ = \ 1350 (unsigned char) ((unsigned int) (i) >> 24); \ 1351 *(envPtr)->codeNext++ = \ 1352 (unsigned char) ((unsigned int) (i) >> 16); \ 1353 *(envPtr)->codeNext++ = \ 1354 (unsigned char) ((unsigned int) (i) >> 8); \ 1355 *(envPtr)->codeNext++ = \ 1356 (unsigned char) ((unsigned int) (i) ); \ 1357 } while (0) 1358 1359 /* 1360 * Macros to emit an instruction with signed or unsigned integer operands. 1361 * Four byte integers are stored in "big-endian" order with the high order 1362 * byte stored at the lowest address. The ANSI C "prototypes" for these macros 1363 * are: 1364 * 1365 * void TclEmitInstInt1(unsigned char op, int i, CompileEnv *envPtr); 1366 * void TclEmitInstInt4(unsigned char op, int i, CompileEnv *envPtr); 1367 */ 1368 1369 #define TclEmitInstInt1(op, i, envPtr) \ 1370 do { \ 1371 if (((envPtr)->codeNext + 2) > (envPtr)->codeEnd) { \ 1372 TclExpandCodeArray(envPtr); \ 1373 } \ 1374 *(envPtr)->codeNext++ = (unsigned char) (op); \ 1375 *(envPtr)->codeNext++ = (unsigned char) ((unsigned int) (i)); \ 1376 TclUpdateAtCmdStart(op, envPtr); \ 1377 TclUpdateStackReqs(op, i, envPtr); \ 1378 } while (0) 1379 1380 #define TclEmitInstInt4(op, i, envPtr) \ 1381 do { \ 1382 if (((envPtr)->codeNext + 5) > (envPtr)->codeEnd) { \ 1383 TclExpandCodeArray(envPtr); \ 1384 } \ 1385 *(envPtr)->codeNext++ = (unsigned char) (op); \ 1386 *(envPtr)->codeNext++ = \ 1387 (unsigned char) ((unsigned int) (i) >> 24); \ 1388 *(envPtr)->codeNext++ = \ 1389 (unsigned char) ((unsigned int) (i) >> 16); \ 1390 *(envPtr)->codeNext++ = \ 1391 (unsigned char) ((unsigned int) (i) >> 8); \ 1392 *(envPtr)->codeNext++ = \ 1393 (unsigned char) ((unsigned int) (i) ); \ 1394 TclUpdateAtCmdStart(op, envPtr); \ 1395 TclUpdateStackReqs(op, i, envPtr); \ 1396 } while (0) 1397 1398 /* 1399 * Macro to push a Tcl object onto the Tcl evaluation stack. It emits the 1400 * object's one or four byte array index into the CompileEnv's code array. 1401 * These support, respectively, a maximum of 256 (2**8) and 2**32 objects in a 1402 * CompileEnv. The ANSI C "prototype" for this macro is: 1403 * 1404 * void TclEmitPush(int objIndex, CompileEnv *envPtr); 1405 */ 1406 1407 #define TclEmitPush(objIndex, envPtr) \ 1408 do { \ 1409 int _objIndexCopy = (objIndex); \ 1410 if (_objIndexCopy <= 255) { \ 1411 TclEmitInstInt1(INST_PUSH1, _objIndexCopy, (envPtr)); \ 1412 } else { \ 1413 TclEmitInstInt4(INST_PUSH4, _objIndexCopy, (envPtr)); \ 1414 } \ 1415 } while (0) 1416 1417 /* 1418 * Macros to update a (signed or unsigned) integer starting at a pointer. The 1419 * two variants depend on the number of bytes. The ANSI C "prototypes" for 1420 * these macros are: 1421 * 1422 * void TclStoreInt1AtPtr(int i, unsigned char *p); 1423 * void TclStoreInt4AtPtr(int i, unsigned char *p); 1424 */ 1425 1426 #define TclStoreInt1AtPtr(i, p) \ 1427 *(p) = (unsigned char) ((unsigned int) (i)) 1428 1429 #define TclStoreInt4AtPtr(i, p) \ 1430 do { \ 1431 *(p) = (unsigned char) ((unsigned int) (i) >> 24); \ 1432 *(p+1) = (unsigned char) ((unsigned int) (i) >> 16); \ 1433 *(p+2) = (unsigned char) ((unsigned int) (i) >> 8); \ 1434 *(p+3) = (unsigned char) ((unsigned int) (i) ); \ 1435 } while (0) 1436 1437 /* 1438 * Macros to update instructions at a particular pc with a new op code and a 1439 * (signed or unsigned) int operand. The ANSI C "prototypes" for these macros 1440 * are: 1441 * 1442 * void TclUpdateInstInt1AtPc(unsigned char op, int i, unsigned char *pc); 1443 * void TclUpdateInstInt4AtPc(unsigned char op, int i, unsigned char *pc); 1444 */ 1445 1446 #define TclUpdateInstInt1AtPc(op, i, pc) \ 1447 do { \ 1448 *(pc) = (unsigned char) (op); \ 1449 TclStoreInt1AtPtr((i), ((pc)+1)); \ 1450 } while (0) 1451 1452 #define TclUpdateInstInt4AtPc(op, i, pc) \ 1453 do { \ 1454 *(pc) = (unsigned char) (op); \ 1455 TclStoreInt4AtPtr((i), ((pc)+1)); \ 1456 } while (0) 1457 1458 /* 1459 * Macro to fix up a forward jump to point to the current code-generation 1460 * position in the bytecode being created (the most common case). The ANSI C 1461 * "prototypes" for this macro is: 1462 * 1463 * int TclFixupForwardJumpToHere(CompileEnv *envPtr, JumpFixup *fixupPtr, 1464 * int threshold); 1465 */ 1466 1467 #define TclFixupForwardJumpToHere(envPtr, fixupPtr, threshold) \ 1468 TclFixupForwardJump((envPtr), (fixupPtr), \ 1469 (envPtr)->codeNext-(envPtr)->codeStart-(fixupPtr)->codeOffset, \ 1470 (threshold)) 1471 1472 /* 1473 * Macros to get a signed integer (GET_INT{1,2}) or an unsigned int 1474 * (GET_UINT{1,2}) from a pointer. There are two variants for each return type 1475 * that depend on the number of bytes fetched. The ANSI C "prototypes" for 1476 * these macros are: 1477 * 1478 * int TclGetInt1AtPtr(unsigned char *p); 1479 * int TclGetInt4AtPtr(unsigned char *p); 1480 * unsigned int TclGetUInt1AtPtr(unsigned char *p); 1481 * unsigned int TclGetUInt4AtPtr(unsigned char *p); 1482 */ 1483 1484 /* 1485 * The TclGetInt1AtPtr macro is tricky because we want to do sign extension on 1486 * the 1-byte value. Unfortunately the "char" type isn't signed on all 1487 * platforms so sign-extension doesn't always happen automatically. Sometimes 1488 * we can explicitly declare the pointer to be signed, but other times we have 1489 * to explicitly sign-extend the value in software. 1490 */ 1491 1492 #ifndef __CHAR_UNSIGNED__ 1493 # define TclGetInt1AtPtr(p) ((int) *((char *) p)) 1494 #elif defined(HAVE_SIGNED_CHAR) 1495 # define TclGetInt1AtPtr(p) ((int) *((signed char *) p)) 1496 #else 1497 # define TclGetInt1AtPtr(p) \ 1498 (((int) *((char *) p)) | ((*(p) & 0200) ? (-256) : 0)) 1499 #endif 1500 1501 #define TclGetInt4AtPtr(p) \ 1502 (((int) (TclGetUInt1AtPtr(p) << 24)) | \ 1503 (*((p)+1) << 16) | \ 1504 (*((p)+2) << 8) | \ 1505 (*((p)+3))) 1506 1507 #define TclGetUInt1AtPtr(p) \ 1508 ((unsigned int) *(p)) 1509 #define TclGetUInt4AtPtr(p) \ 1510 ((unsigned int) (*(p) << 24) | \ 1511 (*((p)+1) << 16) | \ 1512 (*((p)+2) << 8) | \ 1513 (*((p)+3))) 1514 1515 /* 1516 * Macros used to compute the minimum and maximum of two integers. The ANSI C 1517 * "prototypes" for these macros are: 1518 * 1519 * int TclMin(int i, int j); 1520 * int TclMax(int i, int j); 1521 */ 1522 1523 #define TclMin(i, j) ((((int) i) < ((int) j))? (i) : (j)) 1524 #define TclMax(i, j) ((((int) i) > ((int) j))? (i) : (j)) 1525 1526 /* 1527 * Convenience macros for use when compiling bodies of commands. The ANSI C 1528 * "prototype" for these macros are: 1529 * 1530 * static void BODY(Tcl_Token *tokenPtr, int word); 1531 */ 1532 1533 #define BODY(tokenPtr, word) \ 1534 SetLineInformation((word)); \ 1535 TclCompileCmdWord(interp, (tokenPtr)+1, (tokenPtr)->numComponents, \ 1536 envPtr) 1537 1538 /* 1539 * Convenience macro for use when compiling tokens to be pushed. The ANSI C 1540 * "prototype" for this macro is: 1541 * 1542 * static void CompileTokens(CompileEnv *envPtr, Tcl_Token *tokenPtr, 1543 * Tcl_Interp *interp); 1544 */ 1545 1546 #define CompileTokens(envPtr, tokenPtr, interp) \ 1547 TclCompileTokens((interp), (tokenPtr)+1, (tokenPtr)->numComponents, \ 1548 (envPtr)); 1549 /* 1550 * Convenience macros for use when pushing literals. The ANSI C "prototype" for 1551 * these macros are: 1552 * 1553 * static void PushLiteral(CompileEnv *envPtr, 1554 * const char *string, int length); 1555 * static void PushStringLiteral(CompileEnv *envPtr, 1556 * const char *string); 1557 */ 1558 1559 #define PushLiteral(envPtr, string, length) \ 1560 TclEmitPush(TclRegisterLiteral(envPtr, string, length, 0), (envPtr)) 1561 #define PushStringLiteral(envPtr, string) \ 1562 PushLiteral(envPtr, string, (int) (sizeof(string "") - 1)) 1563 1564 /* 1565 * Macro to advance to the next token; it is more mnemonic than the address 1566 * arithmetic that it replaces. The ANSI C "prototype" for this macro is: 1567 * 1568 * static Tcl_Token * TokenAfter(Tcl_Token *tokenPtr); 1569 */ 1570 1571 #define TokenAfter(tokenPtr) \ 1572 ((tokenPtr) + ((tokenPtr)->numComponents + 1)) 1573 1574 /* 1575 * Macro to get the offset to the next instruction to be issued. The ANSI C 1576 * "prototype" for this macro is: 1577 * 1578 * static int CurrentOffset(CompileEnv *envPtr); 1579 */ 1580 1581 #define CurrentOffset(envPtr) \ 1582 ((envPtr)->codeNext - (envPtr)->codeStart) 1583 1584 /* 1585 * Note: the exceptDepth is a bit of a misnomer: TEBC only needs the 1586 * maximal depth of nested CATCH ranges in order to alloc runtime 1587 * memory. These macros should compute precisely that? OTOH, the nesting depth 1588 * of LOOP ranges is an interesting datum for debugging purposes, and that is 1589 * what we compute now. 1590 * 1591 * static int ExceptionRangeStarts(CompileEnv *envPtr, int index); 1592 * static void ExceptionRangeEnds(CompileEnv *envPtr, int index); 1593 * static void ExceptionRangeTarget(CompileEnv *envPtr, int index, LABEL); 1594 */ 1595 1596 #define ExceptionRangeStarts(envPtr, index) \ 1597 (((envPtr)->exceptDepth++), \ 1598 ((envPtr)->maxExceptDepth = \ 1599 TclMax((envPtr)->exceptDepth, (envPtr)->maxExceptDepth)), \ 1600 ((envPtr)->exceptArrayPtr[(index)].codeOffset = CurrentOffset(envPtr))) 1601 #define ExceptionRangeEnds(envPtr, index) \ 1602 (((envPtr)->exceptDepth--), \ 1603 ((envPtr)->exceptArrayPtr[(index)].numCodeBytes = \ 1604 CurrentOffset(envPtr) - (envPtr)->exceptArrayPtr[(index)].codeOffset)) 1605 #define ExceptionRangeTarget(envPtr, index, targetType) \ 1606 ((envPtr)->exceptArrayPtr[(index)].targetType = CurrentOffset(envPtr)) 1607 1608 /* 1609 * Check if there is an LVT for compiled locals 1610 */ 1611 1612 #define EnvHasLVT(envPtr) \ 1613 (envPtr->procPtr || envPtr->iPtr->varFramePtr->localCachePtr) 1614 1615 /* 1616 * Macros for making it easier to deal with tokens and DStrings. 1617 */ 1618 1619 #define TclDStringAppendToken(dsPtr, tokenPtr) \ 1620 Tcl_DStringAppend((dsPtr), (tokenPtr)->start, (tokenPtr)->size) 1621 #define TclRegisterDStringLiteral(envPtr, dsPtr) \ 1622 TclRegisterLiteral(envPtr, Tcl_DStringValue(dsPtr), \ 1623 Tcl_DStringLength(dsPtr), /*flags*/ 0) 1624 1625 /* 1626 * Macro that encapsulates an efficiency trick that avoids a function call for 1627 * the simplest of compiles. The ANSI C "prototype" for this macro is: 1628 * 1629 * static void CompileWord(CompileEnv *envPtr, Tcl_Token *tokenPtr, 1630 * Tcl_Interp *interp, int word); 1631 */ 1632 1633 #define CompileWord(envPtr, tokenPtr, interp, word) \ 1634 if ((tokenPtr)->type == TCL_TOKEN_SIMPLE_WORD) { \ 1635 PushLiteral((envPtr), (tokenPtr)[1].start, (tokenPtr)[1].size); \ 1636 } else { \ 1637 SetLineInformation((word)); \ 1638 CompileTokens((envPtr), (tokenPtr), (interp)); \ 1639 } 1640 1641 /* 1642 * TIP #280: Remember the per-word line information of the current command. An 1643 * index is used instead of a pointer as recursive compilation may reallocate, 1644 * i.e. move, the array. This is also the reason to save the nuloc now, it may 1645 * change during the course of the function. 1646 * 1647 * Macro to encapsulate the variable definition and setup. 1648 */ 1649 1650 #define DefineLineInformation \ 1651 ExtCmdLoc *mapPtr = envPtr->extCmdMapPtr; \ 1652 int eclIndex = mapPtr->nuloc - 1 1653 1654 #define SetLineInformation(word) \ 1655 envPtr->line = mapPtr->loc[eclIndex].line[(word)]; \ 1656 envPtr->clNext = mapPtr->loc[eclIndex].next[(word)] 1657 1658 #define PushVarNameWord(i,v,e,f,l,sc,word) \ 1659 SetLineInformation(word); \ 1660 TclPushVarName(i,v,e,f,l,sc) 1661 1662 /* 1663 * Often want to issue one of two versions of an instruction based on whether 1664 * the argument will fit in a single byte or not. This makes it much clearer. 1665 */ 1666 1667 #define Emit14Inst(nm,idx,envPtr) \ 1668 if (idx <= 255) { \ 1669 TclEmitInstInt1(nm##1,idx,envPtr); \ 1670 } else { \ 1671 TclEmitInstInt4(nm##4,idx,envPtr); \ 1672 } 1673 1674 /* 1675 * How to get an anonymous local variable (used for holding temporary values 1676 * off the stack) or a local simple scalar. 1677 */ 1678 1679 #define AnonymousLocal(envPtr) \ 1680 (TclFindCompiledLocal(NULL, /*nameChars*/ 0, /*create*/ 1, (envPtr))) 1681 #define LocalScalar(chars,len,envPtr) \ 1682 TclLocalScalar(chars, len, envPtr) 1683 #define LocalScalarFromToken(tokenPtr,envPtr) \ 1684 TclLocalScalarFromToken(tokenPtr, envPtr) 1685 1686 /* 1687 * Flags bits used by TclPushVarName. 1688 */ 1689 1690 #define TCL_NO_LARGE_INDEX 1 /* Do not return localIndex value > 255 */ 1691 #define TCL_NO_ELEMENT 2 /* Do not push the array element. */ 1692 1693 /* 1694 * DTrace probe macros (NOPs if DTrace support is not enabled). 1695 */ 1696 1697 /* 1698 * Define the following macros to enable debug logging of the DTrace proc, 1699 * cmd, and inst probes. Note that this does _not_ require a platform with 1700 * DTrace, it simply logs all probe output to /tmp/tclDTraceDebug-[pid].log. 1701 * 1702 * If the second macro is defined, logging to file starts immediately, 1703 * otherwise only after the first call to [tcl::dtrace]. Note that the debug 1704 * probe data is always computed, even when it is not logged to file. 1705 * 1706 * Defining the third macro enables debug logging of inst probes (disabled 1707 * by default due to the significant performance impact). 1708 */ 1709 1710 /* 1711 #define TCL_DTRACE_DEBUG 1 1712 #define TCL_DTRACE_DEBUG_LOG_ENABLED 1 1713 #define TCL_DTRACE_DEBUG_INST_PROBES 1 1714 */ 1715 1716 #if !(defined(TCL_DTRACE_DEBUG) && defined(__GNUC__)) 1717 1718 #ifdef USE_DTRACE 1719 1720 #if defined(__GNUC__) && __GNUC__ > 2 1721 /* 1722 * Use gcc branch prediction hint to minimize cost of DTrace ENABLED checks. 1723 */ 1724 #define unlikely(x) (__builtin_expect((x), 0)) 1725 #else 1726 #define unlikely(x) (x) 1727 #endif 1728 1729 #define TCL_DTRACE_PROC_ENTRY_ENABLED() unlikely(TCL_PROC_ENTRY_ENABLED()) 1730 #define TCL_DTRACE_PROC_RETURN_ENABLED() unlikely(TCL_PROC_RETURN_ENABLED()) 1731 #define TCL_DTRACE_PROC_RESULT_ENABLED() unlikely(TCL_PROC_RESULT_ENABLED()) 1732 #define TCL_DTRACE_PROC_ARGS_ENABLED() unlikely(TCL_PROC_ARGS_ENABLED()) 1733 #define TCL_DTRACE_PROC_INFO_ENABLED() unlikely(TCL_PROC_INFO_ENABLED()) 1734 #define TCL_DTRACE_PROC_ENTRY(a0, a1, a2) TCL_PROC_ENTRY(a0, a1, a2) 1735 #define TCL_DTRACE_PROC_RETURN(a0, a1) TCL_PROC_RETURN(a0, a1) 1736 #define TCL_DTRACE_PROC_RESULT(a0, a1, a2, a3) TCL_PROC_RESULT(a0, a1, a2, a3) 1737 #define TCL_DTRACE_PROC_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \ 1738 TCL_PROC_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) 1739 #define TCL_DTRACE_PROC_INFO(a0, a1, a2, a3, a4, a5, a6, a7) \ 1740 TCL_PROC_INFO(a0, a1, a2, a3, a4, a5, a6, a7) 1741 1742 #define TCL_DTRACE_CMD_ENTRY_ENABLED() unlikely(TCL_CMD_ENTRY_ENABLED()) 1743 #define TCL_DTRACE_CMD_RETURN_ENABLED() unlikely(TCL_CMD_RETURN_ENABLED()) 1744 #define TCL_DTRACE_CMD_RESULT_ENABLED() unlikely(TCL_CMD_RESULT_ENABLED()) 1745 #define TCL_DTRACE_CMD_ARGS_ENABLED() unlikely(TCL_CMD_ARGS_ENABLED()) 1746 #define TCL_DTRACE_CMD_INFO_ENABLED() unlikely(TCL_CMD_INFO_ENABLED()) 1747 #define TCL_DTRACE_CMD_ENTRY(a0, a1, a2) TCL_CMD_ENTRY(a0, a1, a2) 1748 #define TCL_DTRACE_CMD_RETURN(a0, a1) TCL_CMD_RETURN(a0, a1) 1749 #define TCL_DTRACE_CMD_RESULT(a0, a1, a2, a3) TCL_CMD_RESULT(a0, a1, a2, a3) 1750 #define TCL_DTRACE_CMD_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \ 1751 TCL_CMD_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) 1752 #define TCL_DTRACE_CMD_INFO(a0, a1, a2, a3, a4, a5, a6, a7) \ 1753 TCL_CMD_INFO(a0, a1, a2, a3, a4, a5, a6, a7) 1754 1755 #define TCL_DTRACE_INST_START_ENABLED() unlikely(TCL_INST_START_ENABLED()) 1756 #define TCL_DTRACE_INST_DONE_ENABLED() unlikely(TCL_INST_DONE_ENABLED()) 1757 #define TCL_DTRACE_INST_START(a0, a1, a2) TCL_INST_START(a0, a1, a2) 1758 #define TCL_DTRACE_INST_DONE(a0, a1, a2) TCL_INST_DONE(a0, a1, a2) 1759 1760 #define TCL_DTRACE_TCL_PROBE_ENABLED() unlikely(TCL_TCL_PROBE_ENABLED()) 1761 #define TCL_DTRACE_TCL_PROBE(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \ 1762 TCL_TCL_PROBE(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) 1763 1764 #define TCL_DTRACE_DEBUG_LOG() 1765 1766 MODULE_SCOPE void TclDTraceInfo(Tcl_Obj *info, const char **args, 1767 int *argsi); 1768 1769 #else /* USE_DTRACE */ 1770 1771 #define TCL_DTRACE_PROC_ENTRY_ENABLED() 0 1772 #define TCL_DTRACE_PROC_RETURN_ENABLED() 0 1773 #define TCL_DTRACE_PROC_RESULT_ENABLED() 0 1774 #define TCL_DTRACE_PROC_ARGS_ENABLED() 0 1775 #define TCL_DTRACE_PROC_INFO_ENABLED() 0 1776 #define TCL_DTRACE_PROC_ENTRY(a0, a1, a2) {if (a0) {}} 1777 #define TCL_DTRACE_PROC_RETURN(a0, a1) {if (a0) {}} 1778 #define TCL_DTRACE_PROC_RESULT(a0, a1, a2, a3) {if (a0) {}; if (a3) {}} 1779 #define TCL_DTRACE_PROC_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {} 1780 #define TCL_DTRACE_PROC_INFO(a0, a1, a2, a3, a4, a5, a6, a7) {} 1781 1782 #define TCL_DTRACE_CMD_ENTRY_ENABLED() 0 1783 #define TCL_DTRACE_CMD_RETURN_ENABLED() 0 1784 #define TCL_DTRACE_CMD_RESULT_ENABLED() 0 1785 #define TCL_DTRACE_CMD_ARGS_ENABLED() 0 1786 #define TCL_DTRACE_CMD_INFO_ENABLED() 0 1787 #define TCL_DTRACE_CMD_ENTRY(a0, a1, a2) {} 1788 #define TCL_DTRACE_CMD_RETURN(a0, a1) {} 1789 #define TCL_DTRACE_CMD_RESULT(a0, a1, a2, a3) {} 1790 #define TCL_DTRACE_CMD_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {} 1791 #define TCL_DTRACE_CMD_INFO(a0, a1, a2, a3, a4, a5, a6, a7) {} 1792 1793 #define TCL_DTRACE_INST_START_ENABLED() 0 1794 #define TCL_DTRACE_INST_DONE_ENABLED() 0 1795 #define TCL_DTRACE_INST_START(a0, a1, a2) {} 1796 #define TCL_DTRACE_INST_DONE(a0, a1, a2) {} 1797 1798 #define TCL_DTRACE_TCL_PROBE_ENABLED() 0 1799 #define TCL_DTRACE_TCL_PROBE(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {} 1800 1801 #define TclDTraceInfo(info, args, argsi) {*args = ""; *argsi = 0;} 1802 1803 #endif /* USE_DTRACE */ 1804 1805 #else /* TCL_DTRACE_DEBUG */ 1806 1807 #define USE_DTRACE 1 1808 1809 #if !defined(TCL_DTRACE_DEBUG_LOG_ENABLED) || !(TCL_DTRACE_DEBUG_LOG_ENABLED) 1810 #undef TCL_DTRACE_DEBUG_LOG_ENABLED 1811 #define TCL_DTRACE_DEBUG_LOG_ENABLED 0 1812 #endif 1813 1814 #if !defined(TCL_DTRACE_DEBUG_INST_PROBES) || !(TCL_DTRACE_DEBUG_INST_PROBES) 1815 #undef TCL_DTRACE_DEBUG_INST_PROBES 1816 #define TCL_DTRACE_DEBUG_INST_PROBES 0 1817 #endif 1818 1819 MODULE_SCOPE int tclDTraceDebugEnabled, tclDTraceDebugIndent; 1820 MODULE_SCOPE FILE *tclDTraceDebugLog; 1821 MODULE_SCOPE void TclDTraceOpenDebugLog(void); 1822 MODULE_SCOPE void TclDTraceInfo(Tcl_Obj *info, const char **args, int *argsi); 1823 1824 #define TCL_DTRACE_DEBUG_LOG() \ 1825 int tclDTraceDebugEnabled = TCL_DTRACE_DEBUG_LOG_ENABLED; \ 1826 int tclDTraceDebugIndent = 0; \ 1827 FILE *tclDTraceDebugLog = NULL; \ 1828 void TclDTraceOpenDebugLog(void) { \ 1829 char n[35]; \ 1830 sprintf(n, "/tmp/tclDTraceDebug-%lu.log", \ 1831 (unsigned long) getpid()); \ 1832 tclDTraceDebugLog = fopen(n, "a"); \ 1833 } 1834 1835 #define TclDTraceDbgMsg(p, m, ...) \ 1836 do { \ 1837 if (tclDTraceDebugEnabled) { \ 1838 int _l, _t = 0; \ 1839 if (!tclDTraceDebugLog) { TclDTraceOpenDebugLog(); } \ 1840 fprintf(tclDTraceDebugLog, "%.12s:%.4d:%n", \ 1841 strrchr(__FILE__, '/')+1, __LINE__, &_l); _t += _l; \ 1842 fprintf(tclDTraceDebugLog, " %.*s():%n", \ 1843 (_t < 18 ? 18 - _t : 0) + 18, __func__, &_l); _t += _l; \ 1844 fprintf(tclDTraceDebugLog, "%*s" p "%n", \ 1845 (_t < 40 ? 40 - _t : 0) + 2 * tclDTraceDebugIndent, \ 1846 "", &_l); _t += _l; \ 1847 fprintf(tclDTraceDebugLog, "%*s" m "\n", \ 1848 (_t < 64 ? 64 - _t : 1), "", ##__VA_ARGS__); \ 1849 fflush(tclDTraceDebugLog); \ 1850 } \ 1851 } while (0) 1852 1853 #define TCL_DTRACE_PROC_ENTRY_ENABLED() 1 1854 #define TCL_DTRACE_PROC_RETURN_ENABLED() 1 1855 #define TCL_DTRACE_PROC_RESULT_ENABLED() 1 1856 #define TCL_DTRACE_PROC_ARGS_ENABLED() 1 1857 #define TCL_DTRACE_PROC_INFO_ENABLED() 1 1858 #define TCL_DTRACE_PROC_ENTRY(a0, a1, a2) \ 1859 tclDTraceDebugIndent++; \ 1860 TclDTraceDbgMsg("-> proc-entry", "%s %d %p", a0, a1, a2) 1861 #define TCL_DTRACE_PROC_RETURN(a0, a1) \ 1862 TclDTraceDbgMsg("<- proc-return", "%s %d", a0, a1); \ 1863 tclDTraceDebugIndent-- 1864 #define TCL_DTRACE_PROC_RESULT(a0, a1, a2, a3) \ 1865 TclDTraceDbgMsg(" | proc-result", "%s %d %s %p", a0, a1, a2, a3) 1866 #define TCL_DTRACE_PROC_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \ 1867 TclDTraceDbgMsg(" | proc-args", "%s %s %s %s %s %s %s %s %s %s", a0, \ 1868 a1, a2, a3, a4, a5, a6, a7, a8, a9) 1869 #define TCL_DTRACE_PROC_INFO(a0, a1, a2, a3, a4, a5, a6, a7) \ 1870 TclDTraceDbgMsg(" | proc-info", "%s %s %s %s %d %d %s %s", a0, a1, \ 1871 a2, a3, a4, a5, a6, a7) 1872 1873 #define TCL_DTRACE_CMD_ENTRY_ENABLED() 1 1874 #define TCL_DTRACE_CMD_RETURN_ENABLED() 1 1875 #define TCL_DTRACE_CMD_RESULT_ENABLED() 1 1876 #define TCL_DTRACE_CMD_ARGS_ENABLED() 1 1877 #define TCL_DTRACE_CMD_INFO_ENABLED() 1 1878 #define TCL_DTRACE_CMD_ENTRY(a0, a1, a2) \ 1879 tclDTraceDebugIndent++; \ 1880 TclDTraceDbgMsg("-> cmd-entry", "%s %d %p", a0, a1, a2) 1881 #define TCL_DTRACE_CMD_RETURN(a0, a1) \ 1882 TclDTraceDbgMsg("<- cmd-return", "%s %d", a0, a1); \ 1883 tclDTraceDebugIndent-- 1884 #define TCL_DTRACE_CMD_RESULT(a0, a1, a2, a3) \ 1885 TclDTraceDbgMsg(" | cmd-result", "%s %d %s %p", a0, a1, a2, a3) 1886 #define TCL_DTRACE_CMD_ARGS(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \ 1887 TclDTraceDbgMsg(" | cmd-args", "%s %s %s %s %s %s %s %s %s %s", a0, \ 1888 a1, a2, a3, a4, a5, a6, a7, a8, a9) 1889 #define TCL_DTRACE_CMD_INFO(a0, a1, a2, a3, a4, a5, a6, a7) \ 1890 TclDTraceDbgMsg(" | cmd-info", "%s %s %s %s %d %d %s %s", a0, a1, \ 1891 a2, a3, a4, a5, a6, a7) 1892 1893 #define TCL_DTRACE_INST_START_ENABLED() TCL_DTRACE_DEBUG_INST_PROBES 1894 #define TCL_DTRACE_INST_DONE_ENABLED() TCL_DTRACE_DEBUG_INST_PROBES 1895 #define TCL_DTRACE_INST_START(a0, a1, a2) \ 1896 TclDTraceDbgMsg(" | inst-start", "%s %d %p", a0, a1, a2) 1897 #define TCL_DTRACE_INST_DONE(a0, a1, a2) \ 1898 TclDTraceDbgMsg(" | inst-end", "%s %d %p", a0, a1, a2) 1899 1900 #define TCL_DTRACE_TCL_PROBE_ENABLED() 1 1901 #define TCL_DTRACE_TCL_PROBE(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) \ 1902 do { \ 1903 tclDTraceDebugEnabled = 1; \ 1904 TclDTraceDbgMsg(" | tcl-probe", "%s %s %s %s %s %s %s %s %s %s", a0, \ 1905 a1, a2, a3, a4, a5, a6, a7, a8, a9); \ 1906 } while (0) 1907 1908 #endif /* TCL_DTRACE_DEBUG */ 1909 1910 #endif /* _TCLCOMPILATION */ 1911 1912 /* 1913 * Local Variables: 1914 * mode: c 1915 * c-basic-offset: 4 1916 * fill-column: 78 1917 * End: 1918 */ 1919