4 * This file contains procedures that execute byte-compiled Tcl
7 * Copyright (c) 1996-1997 Sun Microsystems, Inc.
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 #include "tclCompile.h"
19 # include "../compat/float.h"
28 * The stuff below is a bit of a hack so that this file can be used
29 * in environments that include no UNIX, i.e. no errno. Just define
33 #ifndef TCL_GENERIC_ONLY
46 * Boolean flag indicating whether the Tcl bytecode interpreter has been
50 static int execInitialized = 0;
51 TCL_DECLARE_MUTEX(execMutex)
54 * Variable that controls whether execution tracing is enabled and, if so,
55 * what level of tracing is desired:
56 * 0: no execution tracing
57 * 1: trace invocations of Tcl procs only
58 * 2: trace invocations of all (not compiled away) commands
59 * 3: display each instruction executed
60 * This variable is linked to the Tcl variable "tcl_traceExec".
65 typedef struct ThreadSpecificData {
67 * The following global variable is use to signal matherr that Tcl
68 * is responsible for the arithmetic, so errors can be handled in a
69 * fashion appropriate for Tcl. Zero means no Tcl math is in
70 * progress; non-zero means Tcl is doing math.
77 static Tcl_ThreadDataKey dataKey;
80 * The variable below serves no useful purpose except to generate
81 * a reference to matherr, so that the Tcl version of matherr is
82 * linked in rather than the system version. Without this reference
83 * the need for matherr won't be discovered during linking until after
84 * libtcl.a has been processed, so Tcl's version won't be used.
89 int (*tclMatherrPtr)() = matherr;
93 * Mapping from expression instruction opcodes to strings; used for error
94 * messages. Note that these entries must match the order and number of the
95 * expression opcodes (e.g., INST_LOR) in tclCompile.h.
98 static char *operatorStrings[] = {
99 "||", "&&", "|", "^", "&", "==", "!=", "<", ">", "<=", ">=", "<<", ">>",
100 "+", "-", "*", "/", "%", "+", "-", "~", "!",
101 "BUILTIN FUNCTION", "FUNCTION"
105 * Mapping from Tcl result codes to strings; used for error and debugging
109 #ifdef TCL_COMPILE_DEBUG
110 static char *resultStrings[] = {
111 "TCL_OK", "TCL_ERROR", "TCL_RETURN", "TCL_BREAK", "TCL_CONTINUE"
116 * Macros for testing floating-point values for certain special cases. Test
117 * for not-a-number by comparing a value against itself; test for infinity
118 * by comparing against the largest floating-point value.
121 #define IS_NAN(v) ((v) != (v))
123 # define IS_INF(v) (((v) > DBL_MAX) || ((v) < -DBL_MAX))
129 * Macro to adjust the program counter and restart the instruction execution
130 * loop after each instruction is executed.
133 #define ADJUST_PC(instBytes) \
138 * Macros used to cache often-referenced Tcl evaluation stack information
139 * in local variables. Note that a DECACHE_STACK_INFO()-CACHE_STACK_INFO()
140 * pair must surround any call inside TclExecuteByteCode (and a few other
141 * procedures that use this scheme) that could result in a recursive call
142 * to TclExecuteByteCode.
145 #define CACHE_STACK_INFO() \
146 stackPtr = eePtr->stackPtr; \
147 stackTop = eePtr->stackTop
149 #define DECACHE_STACK_INFO() \
150 eePtr->stackTop = stackTop
153 * Macros used to access items on the Tcl evaluation stack. PUSH_OBJECT
154 * increments the object's ref count since it makes the stack have another
155 * reference pointing to the object. However, POP_OBJECT does not decrement
156 * the ref count. This is because the stack may hold the only reference to
157 * the object, so the object would be destroyed if its ref count were
158 * decremented before the caller had a chance to, e.g., store it in a
159 * variable. It is the caller's responsibility to decrement the ref count
160 * when it is finished with an object.
162 * WARNING! It is essential that objPtr only appear once in the PUSH_OBJECT
163 * macro. The actual parameter might be an expression with side effects,
164 * and this ensures that it will be executed only once.
167 #define PUSH_OBJECT(objPtr) \
168 Tcl_IncrRefCount(stackPtr[++stackTop] = (objPtr))
170 #define POP_OBJECT() \
171 (stackPtr[stackTop--])
174 * Macros used to trace instruction execution. The macros TRACE,
175 * TRACE_WITH_OBJ, and O2S are only used inside TclExecuteByteCode.
176 * O2S is only used in TRACE* calls to get a string from an object.
179 #ifdef TCL_COMPILE_DEBUG
181 if (traceInstructions) { \
182 fprintf(stdout, "%2d: %2d (%u) %s ", iPtr->numLevels, stackTop, \
183 (unsigned int)(pc - codePtr->codeStart), \
184 GetOpcodeName(pc)); \
187 #define TRACE_WITH_OBJ(a, objPtr) \
188 if (traceInstructions) { \
189 fprintf(stdout, "%2d: %2d (%u) %s ", iPtr->numLevels, stackTop, \
190 (unsigned int)(pc - codePtr->codeStart), \
191 GetOpcodeName(pc)); \
193 TclPrintObject(stdout, (objPtr), 30); \
194 fprintf(stdout, "\n"); \
196 #define O2S(objPtr) \
197 Tcl_GetString(objPtr)
200 #define TRACE_WITH_OBJ(a, objPtr)
202 #endif /* TCL_COMPILE_DEBUG */
205 * Declarations for local procedures to this file:
208 static void CallTraceProcedure _ANSI_ARGS_((Tcl_Interp *interp,
209 Trace *tracePtr, Command *cmdPtr,
210 char *command, int numChars,
211 int objc, Tcl_Obj *objv[]));
212 static void DupCmdNameInternalRep _ANSI_ARGS_((Tcl_Obj *objPtr,
214 static int ExprAbsFunc _ANSI_ARGS_((Tcl_Interp *interp,
215 ExecEnv *eePtr, ClientData clientData));
216 static int ExprBinaryFunc _ANSI_ARGS_((Tcl_Interp *interp,
217 ExecEnv *eePtr, ClientData clientData));
218 static int ExprCallMathFunc _ANSI_ARGS_((Tcl_Interp *interp,
219 ExecEnv *eePtr, int objc, Tcl_Obj **objv));
220 static int ExprDoubleFunc _ANSI_ARGS_((Tcl_Interp *interp,
221 ExecEnv *eePtr, ClientData clientData));
222 static int ExprIntFunc _ANSI_ARGS_((Tcl_Interp *interp,
223 ExecEnv *eePtr, ClientData clientData));
224 static int ExprRandFunc _ANSI_ARGS_((Tcl_Interp *interp,
225 ExecEnv *eePtr, ClientData clientData));
226 static int ExprRoundFunc _ANSI_ARGS_((Tcl_Interp *interp,
227 ExecEnv *eePtr, ClientData clientData));
228 static int ExprSrandFunc _ANSI_ARGS_((Tcl_Interp *interp,
229 ExecEnv *eePtr, ClientData clientData));
230 static int ExprUnaryFunc _ANSI_ARGS_((Tcl_Interp *interp,
231 ExecEnv *eePtr, ClientData clientData));
232 #ifdef TCL_COMPILE_STATS
233 static int EvalStatsCmd _ANSI_ARGS_((ClientData clientData,
234 Tcl_Interp *interp, int argc, char **argv));
236 static void FreeCmdNameInternalRep _ANSI_ARGS_((
238 #ifdef TCL_COMPILE_DEBUG
239 static char * GetOpcodeName _ANSI_ARGS_((unsigned char *pc));
241 static ExceptionRange * GetExceptRangeForPc _ANSI_ARGS_((unsigned char *pc,
242 int catchOnly, ByteCode* codePtr));
243 static char * GetSrcInfoForPc _ANSI_ARGS_((unsigned char *pc,
244 ByteCode* codePtr, int *lengthPtr));
245 static void GrowEvaluationStack _ANSI_ARGS_((ExecEnv *eePtr));
246 static void IllegalExprOperandType _ANSI_ARGS_((
247 Tcl_Interp *interp, unsigned char *pc,
249 static void InitByteCodeExecution _ANSI_ARGS_((
250 Tcl_Interp *interp));
251 #ifdef TCL_COMPILE_DEBUG
252 static void PrintByteCodeInfo _ANSI_ARGS_((ByteCode *codePtr));
254 static int SetCmdNameFromAny _ANSI_ARGS_((Tcl_Interp *interp,
256 #ifdef TCL_COMPILE_DEBUG
257 static char * StringForResultCode _ANSI_ARGS_((int result));
258 static void ValidatePcAndStackTop _ANSI_ARGS_((
259 ByteCode *codePtr, unsigned char *pc,
260 int stackTop, int stackLowerBound,
261 int stackUpperBound));
263 static int VerifyExprObjType _ANSI_ARGS_((Tcl_Interp *interp,
267 * Table describing the built-in math functions. Entries in this table are
268 * indexed by the values of the INST_CALL_BUILTIN_FUNC instruction's
272 BuiltinFunc builtinFuncTable[] = {
274 {"acos", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) acos},
275 {"asin", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) asin},
276 {"atan", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) atan},
277 {"atan2", 2, {TCL_DOUBLE, TCL_DOUBLE}, ExprBinaryFunc, (ClientData) atan2},
278 {"ceil", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) ceil},
279 {"cos", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) cos},
280 {"cosh", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) cosh},
281 {"exp", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) exp},
282 {"floor", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) floor},
283 {"fmod", 2, {TCL_DOUBLE, TCL_DOUBLE}, ExprBinaryFunc, (ClientData) fmod},
284 {"hypot", 2, {TCL_DOUBLE, TCL_DOUBLE}, ExprBinaryFunc, (ClientData) hypot},
285 {"log", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) log},
286 {"log10", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) log10},
287 {"pow", 2, {TCL_DOUBLE, TCL_DOUBLE}, ExprBinaryFunc, (ClientData) pow},
288 {"sin", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) sin},
289 {"sinh", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) sinh},
290 {"sqrt", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) sqrt},
291 {"tan", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) tan},
292 {"tanh", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) tanh},
294 {"abs", 1, {TCL_EITHER}, ExprAbsFunc, 0},
295 {"double", 1, {TCL_EITHER}, ExprDoubleFunc, 0},
296 {"int", 1, {TCL_EITHER}, ExprIntFunc, 0},
297 {"rand", 0, {TCL_EITHER}, ExprRandFunc, 0}, /* NOTE: rand takes no args. */
298 {"round", 1, {TCL_EITHER}, ExprRoundFunc, 0},
299 {"srand", 1, {TCL_INT}, ExprSrandFunc, 0},
304 * The structure below defines the command name Tcl object type by means of
305 * procedures that can be invoked by generic object code. Objects of this
306 * type cache the Command pointer that results from looking up command names
307 * in the command hashtable. Such objects appear as the zeroth ("command
308 * name") argument in a Tcl command.
311 Tcl_ObjType tclCmdNameType = {
312 "cmdName", /* name */
313 FreeCmdNameInternalRep, /* freeIntRepProc */
314 DupCmdNameInternalRep, /* dupIntRepProc */
315 (Tcl_UpdateStringProc *) NULL, /* updateStringProc */
316 SetCmdNameFromAny /* setFromAnyProc */
320 *----------------------------------------------------------------------
322 * InitByteCodeExecution --
324 * This procedure is called once to initialize the Tcl bytecode
331 * This procedure initializes the array of instruction names. If
332 * compiling with the TCL_COMPILE_STATS flag, it initializes the
333 * array that counts the executions of each instruction and it
334 * creates the "evalstats" command. It also registers the command name
335 * Tcl_ObjType. It also establishes the link between the Tcl
336 * "tcl_traceExec" and C "tclTraceExec" variables.
338 *----------------------------------------------------------------------
342 InitByteCodeExecution(interp)
343 Tcl_Interp *interp; /* Interpreter for which the Tcl variable
344 * "tcl_traceExec" is linked to control
345 * instruction tracing. */
347 Tcl_RegisterObjType(&tclCmdNameType);
348 if (Tcl_LinkVar(interp, "tcl_traceExec", (char *) &tclTraceExec,
349 TCL_LINK_INT) != TCL_OK) {
350 panic("InitByteCodeExecution: can't create link for tcl_traceExec variable");
353 #ifdef TCL_COMPILE_STATS
354 Tcl_CreateCommand(interp, "evalstats", EvalStatsCmd,
355 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
356 #endif /* TCL_COMPILE_STATS */
360 *----------------------------------------------------------------------
362 * TclCreateExecEnv --
364 * This procedure creates a new execution environment for Tcl bytecode
365 * execution. An ExecEnv points to a Tcl evaluation stack. An ExecEnv
366 * is typically created once for each Tcl interpreter (Interp
367 * structure) and recursively passed to TclExecuteByteCode to execute
368 * ByteCode sequences for nested commands.
371 * A newly allocated ExecEnv is returned. This points to an empty
372 * evaluation stack of the standard initial size.
375 * The bytecode interpreter is also initialized here, as this
376 * procedure will be called before any call to TclExecuteByteCode.
378 *----------------------------------------------------------------------
381 #define TCL_STACK_INITIAL_SIZE 2000
384 TclCreateExecEnv(interp)
385 Tcl_Interp *interp; /* Interpreter for which the execution
386 * environment is being created. */
388 ExecEnv *eePtr = (ExecEnv *) ckalloc(sizeof(ExecEnv));
390 eePtr->stackPtr = (Tcl_Obj **)
391 ckalloc((unsigned) (TCL_STACK_INITIAL_SIZE * sizeof(Tcl_Obj *)));
392 eePtr->stackTop = -1;
393 eePtr->stackEnd = (TCL_STACK_INITIAL_SIZE - 1);
395 Tcl_MutexLock(&execMutex);
396 if (!execInitialized) {
397 TclInitAuxDataTypeTable();
398 InitByteCodeExecution(interp);
401 Tcl_MutexUnlock(&execMutex);
405 #undef TCL_STACK_INITIAL_SIZE
408 *----------------------------------------------------------------------
410 * TclDeleteExecEnv --
412 * Frees the storage for an ExecEnv.
418 * Storage for an ExecEnv and its contained storage (e.g. the
419 * evaluation stack) is freed.
421 *----------------------------------------------------------------------
425 TclDeleteExecEnv(eePtr)
426 ExecEnv *eePtr; /* Execution environment to free. */
428 ckfree((char *) eePtr->stackPtr);
429 ckfree((char *) eePtr);
433 *----------------------------------------------------------------------
435 * TclFinalizeExecution --
437 * Finalizes the execution environment setup so that it can be
438 * later reinitialized.
444 * After this call, the next time TclCreateExecEnv will be called
445 * it will call InitByteCodeExecution.
447 *----------------------------------------------------------------------
451 TclFinalizeExecution()
453 Tcl_MutexLock(&execMutex);
455 Tcl_MutexUnlock(&execMutex);
456 TclFinalizeAuxDataTypeTable();
460 *----------------------------------------------------------------------
462 * GrowEvaluationStack --
464 * This procedure grows a Tcl evaluation stack stored in an ExecEnv.
470 * The size of the evaluation stack is doubled.
472 *----------------------------------------------------------------------
476 GrowEvaluationStack(eePtr)
477 register ExecEnv *eePtr; /* Points to the ExecEnv with an evaluation
478 * stack to enlarge. */
481 * The current Tcl stack elements are stored from eePtr->stackPtr[0]
482 * to eePtr->stackPtr[eePtr->stackEnd] (inclusive).
485 int currElems = (eePtr->stackEnd + 1);
486 int newElems = 2*currElems;
487 int currBytes = currElems * sizeof(Tcl_Obj *);
488 int newBytes = 2*currBytes;
489 Tcl_Obj **newStackPtr = (Tcl_Obj **) ckalloc((unsigned) newBytes);
492 * Copy the existing stack items to the new stack space, free the old
493 * storage if appropriate, and mark new space as malloc'ed.
496 memcpy((VOID *) newStackPtr, (VOID *) eePtr->stackPtr,
498 ckfree((char *) eePtr->stackPtr);
499 eePtr->stackPtr = newStackPtr;
500 eePtr->stackEnd = (newElems - 1); /* i.e. index of last usable item */
504 *----------------------------------------------------------------------
506 * TclExecuteByteCode --
508 * This procedure executes the instructions of a ByteCode structure.
509 * It returns when a "done" instruction is executed or an error occurs.
512 * The return value is one of the return codes defined in tcl.h
513 * (such as TCL_OK), and interp->objResultPtr refers to a Tcl object
514 * that either contains the result of executing the code or an
518 * Almost certainly, depending on the ByteCode's instructions.
520 *----------------------------------------------------------------------
524 TclExecuteByteCode(interp, codePtr)
525 Tcl_Interp *interp; /* Token for command interpreter. */
526 ByteCode *codePtr; /* The bytecode sequence to interpret. */
528 Interp *iPtr = (Interp *) interp;
529 ExecEnv *eePtr = iPtr->execEnvPtr;
530 /* Points to the execution environment. */
531 register Tcl_Obj **stackPtr = eePtr->stackPtr;
532 /* Cached evaluation stack base pointer. */
533 register int stackTop = eePtr->stackTop;
534 /* Cached top index of evaluation stack. */
535 register unsigned char *pc = codePtr->codeStart;
536 /* The current program counter. */
537 int opnd; /* Current instruction's operand byte. */
538 int pcAdjustment; /* Hold pc adjustment after instruction. */
539 int initStackTop = stackTop;/* Stack top at start of execution. */
540 ExceptionRange *rangePtr; /* Points to closest loop or catch exception
541 * range enclosing the pc. Used by various
542 * instructions and processCatch to
543 * process break, continue, and errors. */
544 int result = TCL_OK; /* Return code returned after execution. */
545 int traceInstructions = (tclTraceExec == 3);
546 Tcl_Obj *valuePtr, *value2Ptr, *objPtr;
552 * This procedure uses a stack to hold information about catch commands.
553 * This information is the current operand stack top when starting to
554 * execute the code for each catch command. It starts out with stack-
555 * allocated space but uses dynamically-allocated storage if needed.
558 #define STATIC_CATCH_STACK_SIZE 4
559 int (catchStackStorage[STATIC_CATCH_STACK_SIZE]);
560 int *catchStackPtr = catchStackStorage;
563 #ifdef TCL_COMPILE_DEBUG
564 if (tclTraceExec >= 2) {
565 PrintByteCodeInfo(codePtr);
566 fprintf(stdout, " Starting stack top=%d\n", eePtr->stackTop);
571 #ifdef TCL_COMPILE_STATS
572 iPtr->stats.numExecutions++;
576 * Make sure the catch stack is large enough to hold the maximum number
577 * of catch commands that could ever be executing at the same time. This
578 * will be no more than the exception range array's depth.
581 if (codePtr->maxExceptDepth > STATIC_CATCH_STACK_SIZE) {
582 catchStackPtr = (int *)
583 ckalloc(codePtr->maxExceptDepth * sizeof(int));
587 * Make sure the stack has enough room to execute this ByteCode.
590 while ((stackTop + codePtr->maxStackDepth) > eePtr->stackEnd) {
591 GrowEvaluationStack(eePtr);
592 stackPtr = eePtr->stackPtr;
596 * Loop executing instructions until a "done" instruction, a TCL_RETURN,
601 #ifdef TCL_COMPILE_DEBUG
602 ValidatePcAndStackTop(codePtr, pc, stackTop, initStackTop,
604 #else /* not TCL_COMPILE_DEBUG */
605 if (traceInstructions) {
606 fprintf(stdout, "%2d: %2d ", iPtr->numLevels, stackTop);
607 TclPrintInstruction(codePtr, pc);
610 #endif /* TCL_COMPILE_DEBUG */
612 #ifdef TCL_COMPILE_STATS
613 iPtr->stats.instructionCount[*pc]++;
618 * Pop the topmost object from the stack, set the interpreter's
619 * object result to point to it, and return.
621 valuePtr = POP_OBJECT();
622 Tcl_SetObjResult(interp, valuePtr);
623 TclDecrRefCount(valuePtr);
624 if (stackTop != initStackTop) {
625 fprintf(stderr, "\nTclExecuteByteCode: done instruction at pc %u: stack top %d != entry stack top %d\n",
626 (unsigned int)(pc - codePtr->codeStart),
627 (unsigned int) stackTop,
628 (unsigned int) initStackTop);
629 panic("TclExecuteByteCode execution failure: end stack top != start stack top");
631 TRACE_WITH_OBJ(("=> return code=%d, result=", result),
633 #ifdef TCL_COMPILE_DEBUG
634 if (traceInstructions) {
635 fprintf(stdout, "\n");
641 #ifdef TCL_COMPILE_DEBUG
642 valuePtr = codePtr->objArrayPtr[TclGetUInt1AtPtr(pc+1)];
643 PUSH_OBJECT(valuePtr);
644 TRACE_WITH_OBJ(("%u => ", TclGetInt1AtPtr(pc+1)), valuePtr);
646 PUSH_OBJECT(codePtr->objArrayPtr[TclGetUInt1AtPtr(pc+1)]);
647 #endif /* TCL_COMPILE_DEBUG */
651 valuePtr = codePtr->objArrayPtr[TclGetUInt4AtPtr(pc+1)];
652 PUSH_OBJECT(valuePtr);
653 TRACE_WITH_OBJ(("%u => ", TclGetUInt4AtPtr(pc+1)), valuePtr);
657 valuePtr = POP_OBJECT();
658 TRACE_WITH_OBJ(("=> discarding "), valuePtr);
659 TclDecrRefCount(valuePtr); /* finished with pop'ed object. */
663 valuePtr = stackPtr[stackTop];
664 PUSH_OBJECT(Tcl_DuplicateObj(valuePtr));
665 TRACE_WITH_OBJ(("=> "), valuePtr);
669 opnd = TclGetUInt1AtPtr(pc+1);
671 Tcl_Obj *concatObjPtr;
675 * Concatenate strings (with no separators) from the top
676 * opnd items on the stack starting with the deepest item.
677 * First, determine how many characters are needed.
680 for (i = (stackTop - (opnd-1)); i <= stackTop; i++) {
681 bytes = Tcl_GetStringFromObj(stackPtr[i], &length);
688 * Initialize the new append string object by appending the
689 * strings of the opnd stack objects. Also pop the objects.
692 TclNewObj(concatObjPtr);
694 char *p = (char *) ckalloc((unsigned) (totalLen + 1));
695 concatObjPtr->bytes = p;
696 concatObjPtr->length = totalLen;
697 for (i = (stackTop - (opnd-1)); i <= stackTop; i++) {
698 valuePtr = stackPtr[i];
699 bytes = Tcl_GetStringFromObj(valuePtr, &length);
701 memcpy((VOID *) p, (VOID *) bytes,
705 TclDecrRefCount(valuePtr);
709 for (i = (stackTop - (opnd-1)); i <= stackTop; i++) {
710 Tcl_DecrRefCount(stackPtr[i]);
715 PUSH_OBJECT(concatObjPtr);
716 TRACE_WITH_OBJ(("%u => ", opnd), concatObjPtr);
720 case INST_INVOKE_STK4:
721 opnd = TclGetUInt4AtPtr(pc+1);
725 case INST_INVOKE_STK1:
726 opnd = TclGetUInt1AtPtr(pc+1);
731 int objc = opnd; /* The number of arguments. */
732 Tcl_Obj **objv; /* The array of argument objects. */
733 Command *cmdPtr; /* Points to command's Command struct. */
734 int newPcOffset; /* New inst offset for break, continue. */
735 #ifdef TCL_COMPILE_DEBUG
736 int isUnknownCmd = 0;
738 #endif /* TCL_COMPILE_DEBUG */
741 * If the interpreter was deleted, return an error.
744 if (iPtr->flags & DELETED) {
745 Tcl_ResetResult(interp);
746 Tcl_AppendToObj(Tcl_GetObjResult(interp),
747 "attempt to call eval in deleted interpreter", -1);
748 Tcl_SetErrorCode(interp, "CORE", "IDELETE",
749 "attempt to call eval in deleted interpreter",
756 * Find the procedure to execute this command. If the
757 * command is not found, handle it with the "unknown" proc.
760 objv = &(stackPtr[stackTop - (objc-1)]);
761 cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, objv[0]);
762 if (cmdPtr == NULL) {
763 cmdPtr = (Command *) Tcl_FindCommand(interp, "unknown",
764 (Tcl_Namespace *) NULL, TCL_GLOBAL_ONLY);
765 if (cmdPtr == NULL) {
766 Tcl_ResetResult(interp);
767 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
768 "invalid command name \"",
769 Tcl_GetString(objv[0]), "\"",
771 TRACE(("%u => unknown proc not found: ", objc));
775 #ifdef TCL_COMPILE_DEBUG
777 #endif /*TCL_COMPILE_DEBUG*/
778 stackTop++; /* need room for new inserted objv[0] */
779 for (i = objc-1; i >= 0; i--) {
783 objv[0] = Tcl_NewStringObj("unknown", -1);
784 Tcl_IncrRefCount(objv[0]);
788 * Call any trace procedures.
791 if (iPtr->tracePtr != NULL) {
792 Trace *tracePtr, *nextTracePtr;
794 for (tracePtr = iPtr->tracePtr; tracePtr != NULL;
795 tracePtr = nextTracePtr) {
796 nextTracePtr = tracePtr->nextPtr;
797 if (iPtr->numLevels <= tracePtr->level) {
799 char *cmd = GetSrcInfoForPc(pc, codePtr,
802 DECACHE_STACK_INFO();
803 CallTraceProcedure(interp, tracePtr, cmdPtr,
804 cmd, numChars, objc, objv);
812 * Finally, invoke the command's Tcl_ObjCmdProc. First reset
813 * the interpreter's string and object results to their
814 * default empty values since they could have gotten changed
815 * by earlier invocations.
818 Tcl_ResetResult(interp);
819 if (tclTraceExec >= 2) {
820 #ifdef TCL_COMPILE_DEBUG
821 if (traceInstructions) {
822 strncpy(cmdNameBuf, Tcl_GetString(objv[0]), 20);
823 TRACE(("%u => call ", (isUnknownCmd? objc-1:objc)));
825 fprintf(stdout, "%d: (%u) invoking ",
827 (unsigned int)(pc - codePtr->codeStart));
829 for (i = 0; i < objc; i++) {
830 TclPrintObject(stdout, objv[i], 15);
831 fprintf(stdout, " ");
833 fprintf(stdout, "\n");
835 #else /* TCL_COMPILE_DEBUG */
836 fprintf(stdout, "%d: (%u) invoking %s\n",
838 (unsigned int)(pc - codePtr->codeStart),
839 Tcl_GetString(objv[0]));
840 #endif /*TCL_COMPILE_DEBUG*/
844 DECACHE_STACK_INFO();
845 result = (*cmdPtr->objProc)(cmdPtr->objClientData, interp,
847 if (Tcl_AsyncReady()) {
848 result = Tcl_AsyncInvoke(interp, result);
853 * If the interpreter has a non-empty string result, the
854 * result object is either empty or stale because some
855 * procedure set interp->result directly. If so, move the
856 * string result to the result object, then reset the
860 if (*(iPtr->result) != 0) {
861 (void) Tcl_GetObjResult(interp);
865 * Pop the objc top stack elements and decrement their ref
869 for (i = 0; i < objc; i++) {
870 valuePtr = stackPtr[stackTop];
871 TclDecrRefCount(valuePtr);
876 * Process the result of the Tcl_ObjCmdProc call.
882 * Push the call's object result and continue execution
883 * with the next instruction.
885 PUSH_OBJECT(Tcl_GetObjResult(interp));
886 TRACE_WITH_OBJ(("%u => ...after \"%.20s\", result=",
887 objc, cmdNameBuf), Tcl_GetObjResult(interp));
888 ADJUST_PC(pcAdjustment);
893 * The invoked command requested a break or continue.
894 * Find the closest enclosing loop or catch exception
895 * range, if any. If a loop is found, terminate its
896 * execution or skip to its next iteration. If the
897 * closest is a catch exception range, jump to its
898 * catchOffset. If no enclosing range is found, stop
899 * execution and return the TCL_BREAK or TCL_CONTINUE.
901 rangePtr = GetExceptRangeForPc(pc, /*catchOnly*/ 0,
903 if (rangePtr == NULL) {
904 TRACE(("%u => ... after \"%.20s\", no encl. loop or catch, returning %s\n",
906 StringForResultCode(result)));
907 goto abnormalReturn; /* no catch exists to check */
910 switch (rangePtr->type) {
911 case LOOP_EXCEPTION_RANGE:
912 if (result == TCL_BREAK) {
913 newPcOffset = rangePtr->breakOffset;
914 } else if (rangePtr->continueOffset == -1) {
915 TRACE(("%u => ... after \"%.20s\", %s, loop w/o continue, checking for catch\n",
917 StringForResultCode(result)));
920 newPcOffset = rangePtr->continueOffset;
922 TRACE(("%u => ... after \"%.20s\", %s, range at %d, new pc %d\n",
924 StringForResultCode(result),
925 rangePtr->codeOffset, newPcOffset));
927 case CATCH_EXCEPTION_RANGE:
928 TRACE(("%u => ... after \"%.20s\", %s...\n",
930 StringForResultCode(result)));
931 goto processCatch; /* it will use rangePtr */
933 panic("TclExecuteByteCode: bad ExceptionRange type\n");
936 pc = (codePtr->codeStart + newPcOffset);
937 continue; /* restart outer instruction loop at pc */
941 * The invoked command returned an error. Look for an
942 * enclosing catch exception range, if any.
944 TRACE_WITH_OBJ(("%u => ... after \"%.20s\", TCL_ERROR ",
945 objc, cmdNameBuf), Tcl_GetObjResult(interp));
950 * The invoked command requested that the current
951 * procedure stop execution and return. First check
952 * for an enclosing catch exception range, if any.
954 TRACE(("%u => ... after \"%.20s\", TCL_RETURN\n",
959 TRACE_WITH_OBJ(("%u => ... after \"%.20s\", OTHER RETURN CODE %d ",
960 objc, cmdNameBuf, result),
961 Tcl_GetObjResult(interp));
967 objPtr = POP_OBJECT();
968 DECACHE_STACK_INFO();
969 result = Tcl_EvalObjEx(interp, objPtr, 0);
971 if (result == TCL_OK) {
973 * Normal return; push the eval's object result.
975 PUSH_OBJECT(Tcl_GetObjResult(interp));
976 TRACE_WITH_OBJ(("\"%.30s\" => ", O2S(objPtr)),
977 Tcl_GetObjResult(interp));
978 TclDecrRefCount(objPtr);
980 } else if ((result == TCL_BREAK) || (result == TCL_CONTINUE)) {
982 * Find the closest enclosing loop or catch exception range,
983 * if any. If a loop is found, terminate its execution or
984 * skip to its next iteration. If the closest is a catch
985 * exception range, jump to its catchOffset. If no enclosing
986 * range is found, stop execution and return that same
987 * TCL_BREAK or TCL_CONTINUE.
990 int newPcOffset = 0; /* Pc offset computed during break,
991 * continue, error processing. Init.
992 * to avoid compiler warning. */
994 rangePtr = GetExceptRangeForPc(pc, /*catchOnly*/ 0,
996 if (rangePtr == NULL) {
997 TRACE(("\"%.30s\" => no encl. loop or catch, returning %s\n",
998 O2S(objPtr), StringForResultCode(result)));
999 Tcl_DecrRefCount(objPtr);
1000 goto abnormalReturn; /* no catch exists to check */
1002 switch (rangePtr->type) {
1003 case LOOP_EXCEPTION_RANGE:
1004 if (result == TCL_BREAK) {
1005 newPcOffset = rangePtr->breakOffset;
1006 } else if (rangePtr->continueOffset == -1) {
1007 TRACE(("\"%.30s\" => %s, loop w/o continue, checking for catch\n",
1008 O2S(objPtr), StringForResultCode(result)));
1009 Tcl_DecrRefCount(objPtr);
1012 newPcOffset = rangePtr->continueOffset;
1015 TRACE_WITH_OBJ(("\"%.30s\" => %s, range at %d, new pc %d ",
1016 O2S(objPtr), StringForResultCode(result),
1017 rangePtr->codeOffset, newPcOffset), valuePtr);
1019 case CATCH_EXCEPTION_RANGE:
1020 TRACE_WITH_OBJ(("\"%.30s\" => %s ",
1021 O2S(objPtr), StringForResultCode(result)),
1023 Tcl_DecrRefCount(objPtr);
1024 goto processCatch; /* it will use rangePtr */
1026 panic("TclExecuteByteCode: unrecognized ExceptionRange type %d\n", rangePtr->type);
1028 Tcl_DecrRefCount(objPtr);
1029 pc = (codePtr->codeStart + newPcOffset);
1030 continue; /* restart outer instruction loop at pc */
1031 } else { /* eval returned TCL_ERROR, TCL_RETURN, unknown code */
1032 TRACE_WITH_OBJ(("\"%.30s\" => ERROR: ", O2S(objPtr)),
1033 Tcl_GetObjResult(interp));
1034 Tcl_DecrRefCount(objPtr);
1039 objPtr = POP_OBJECT();
1040 Tcl_ResetResult(interp);
1041 DECACHE_STACK_INFO();
1042 result = Tcl_ExprObj(interp, objPtr, &valuePtr);
1044 if (result != TCL_OK) {
1045 TRACE_WITH_OBJ(("\"%.30s\" => ERROR: ",
1046 O2S(objPtr)), Tcl_GetObjResult(interp));
1047 Tcl_DecrRefCount(objPtr);
1050 stackPtr[++stackTop] = valuePtr; /* already has right refct */
1051 TRACE_WITH_OBJ(("\"%.30s\" => ", O2S(objPtr)), valuePtr);
1052 TclDecrRefCount(objPtr);
1055 case INST_LOAD_SCALAR1:
1056 #ifdef TCL_COMPILE_DEBUG
1057 opnd = TclGetUInt1AtPtr(pc+1);
1058 DECACHE_STACK_INFO();
1059 valuePtr = TclGetIndexedScalar(interp, opnd,
1060 /*leaveErrorMsg*/ 1);
1062 if (valuePtr == NULL) {
1063 TRACE_WITH_OBJ(("%u => ERROR: ", opnd),
1064 Tcl_GetObjResult(interp));
1068 PUSH_OBJECT(valuePtr);
1069 TRACE_WITH_OBJ(("%u => ", opnd), valuePtr);
1070 #else /* TCL_COMPILE_DEBUG */
1071 DECACHE_STACK_INFO();
1072 opnd = TclGetUInt1AtPtr(pc+1);
1073 valuePtr = TclGetIndexedScalar(interp, opnd, /*leaveErrorMsg*/ 1);
1075 if (valuePtr == NULL) {
1079 PUSH_OBJECT(valuePtr);
1080 #endif /* TCL_COMPILE_DEBUG */
1083 case INST_LOAD_SCALAR4:
1084 opnd = TclGetUInt4AtPtr(pc+1);
1085 DECACHE_STACK_INFO();
1086 valuePtr = TclGetIndexedScalar(interp, opnd,
1087 /*leaveErrorMsg*/ 1);
1089 if (valuePtr == NULL) {
1090 TRACE_WITH_OBJ(("%u => ERROR: ", opnd),
1091 Tcl_GetObjResult(interp));
1095 PUSH_OBJECT(valuePtr);
1096 TRACE_WITH_OBJ(("%u => ", opnd), valuePtr);
1099 case INST_LOAD_SCALAR_STK:
1100 objPtr = POP_OBJECT(); /* scalar name */
1101 DECACHE_STACK_INFO();
1102 valuePtr = Tcl_ObjGetVar2(interp, objPtr, NULL, TCL_LEAVE_ERR_MSG);
1104 if (valuePtr == NULL) {
1105 TRACE_WITH_OBJ(("\"%.30s\" => ERROR: ", O2S(objPtr)),
1106 Tcl_GetObjResult(interp));
1107 Tcl_DecrRefCount(objPtr);
1111 PUSH_OBJECT(valuePtr);
1112 TRACE_WITH_OBJ(("\"%.30s\" => ", O2S(objPtr)), valuePtr);
1113 TclDecrRefCount(objPtr);
1116 case INST_LOAD_ARRAY4:
1117 opnd = TclGetUInt4AtPtr(pc+1);
1121 case INST_LOAD_ARRAY1:
1122 opnd = TclGetUInt1AtPtr(pc+1);
1127 Tcl_Obj *elemPtr = POP_OBJECT();
1129 DECACHE_STACK_INFO();
1130 valuePtr = TclGetElementOfIndexedArray(interp, opnd,
1131 elemPtr, /*leaveErrorMsg*/ 1);
1133 if (valuePtr == NULL) {
1134 TRACE_WITH_OBJ(("%u \"%.30s\" => ERROR: ",
1135 opnd, O2S(elemPtr)), Tcl_GetObjResult(interp));
1136 Tcl_DecrRefCount(elemPtr);
1140 PUSH_OBJECT(valuePtr);
1141 TRACE_WITH_OBJ(("%u \"%.30s\" => ",
1142 opnd, O2S(elemPtr)),valuePtr);
1143 TclDecrRefCount(elemPtr);
1145 ADJUST_PC(pcAdjustment);
1147 case INST_LOAD_ARRAY_STK:
1149 Tcl_Obj *elemPtr = POP_OBJECT();
1151 objPtr = POP_OBJECT(); /* array name */
1152 DECACHE_STACK_INFO();
1153 valuePtr = Tcl_ObjGetVar2(interp, objPtr, elemPtr,
1156 if (valuePtr == NULL) {
1157 TRACE_WITH_OBJ(("\"%.30s(%.30s)\" => ERROR: ",
1158 O2S(objPtr), O2S(elemPtr)),
1159 Tcl_GetObjResult(interp));
1160 Tcl_DecrRefCount(objPtr);
1161 Tcl_DecrRefCount(elemPtr);
1165 PUSH_OBJECT(valuePtr);
1166 TRACE_WITH_OBJ(("\"%.30s(%.30s)\" => ",
1167 O2S(objPtr), O2S(elemPtr)), valuePtr);
1168 TclDecrRefCount(objPtr);
1169 TclDecrRefCount(elemPtr);
1174 objPtr = POP_OBJECT(); /* variable name */
1175 DECACHE_STACK_INFO();
1176 valuePtr = Tcl_ObjGetVar2(interp, objPtr, NULL, TCL_LEAVE_ERR_MSG);
1178 if (valuePtr == NULL) {
1179 TRACE_WITH_OBJ(("\"%.30s\" => ERROR: ",
1180 O2S(objPtr)), Tcl_GetObjResult(interp));
1181 Tcl_DecrRefCount(objPtr);
1185 PUSH_OBJECT(valuePtr);
1186 TRACE_WITH_OBJ(("\"%.30s\" => ", O2S(objPtr)), valuePtr);
1187 TclDecrRefCount(objPtr);
1190 case INST_STORE_SCALAR4:
1191 opnd = TclGetUInt4AtPtr(pc+1);
1195 case INST_STORE_SCALAR1:
1196 opnd = TclGetUInt1AtPtr(pc+1);
1200 valuePtr = POP_OBJECT();
1201 DECACHE_STACK_INFO();
1202 value2Ptr = TclSetIndexedScalar(interp, opnd, valuePtr,
1203 /*leaveErrorMsg*/ 1);
1205 if (value2Ptr == NULL) {
1206 TRACE_WITH_OBJ(("%u <- \"%.30s\" => ERROR: ",
1207 opnd, O2S(valuePtr)), Tcl_GetObjResult(interp));
1208 Tcl_DecrRefCount(valuePtr);
1212 PUSH_OBJECT(value2Ptr);
1213 TRACE_WITH_OBJ(("%u <- \"%.30s\" => ",
1214 opnd, O2S(valuePtr)), value2Ptr);
1215 TclDecrRefCount(valuePtr);
1216 ADJUST_PC(pcAdjustment);
1218 case INST_STORE_SCALAR_STK:
1219 valuePtr = POP_OBJECT();
1220 objPtr = POP_OBJECT(); /* scalar name */
1221 DECACHE_STACK_INFO();
1222 value2Ptr = Tcl_ObjSetVar2(interp, objPtr, NULL, valuePtr,
1225 if (value2Ptr == NULL) {
1226 TRACE_WITH_OBJ(("\"%.30s\" <- \"%.30s\" => ERROR: ",
1227 O2S(objPtr), O2S(valuePtr)),
1228 Tcl_GetObjResult(interp));
1229 Tcl_DecrRefCount(objPtr);
1230 Tcl_DecrRefCount(valuePtr);
1234 PUSH_OBJECT(value2Ptr);
1235 TRACE_WITH_OBJ(("\"%.30s\" <- \"%.30s\" => ",
1236 O2S(objPtr), O2S(valuePtr)), value2Ptr);
1237 TclDecrRefCount(objPtr);
1238 TclDecrRefCount(valuePtr);
1241 case INST_STORE_ARRAY4:
1242 opnd = TclGetUInt4AtPtr(pc+1);
1246 case INST_STORE_ARRAY1:
1247 opnd = TclGetUInt1AtPtr(pc+1);
1254 valuePtr = POP_OBJECT();
1255 elemPtr = POP_OBJECT();
1256 DECACHE_STACK_INFO();
1257 value2Ptr = TclSetElementOfIndexedArray(interp, opnd,
1258 elemPtr, valuePtr, TCL_LEAVE_ERR_MSG);
1260 if (value2Ptr == NULL) {
1261 TRACE_WITH_OBJ(("%u \"%.30s\" <- \"%.30s\" => ERROR: ",
1262 opnd, O2S(elemPtr), O2S(valuePtr)),
1263 Tcl_GetObjResult(interp));
1264 Tcl_DecrRefCount(elemPtr);
1265 Tcl_DecrRefCount(valuePtr);
1269 PUSH_OBJECT(value2Ptr);
1270 TRACE_WITH_OBJ(("%u \"%.30s\" <- \"%.30s\" => ",
1271 opnd, O2S(elemPtr), O2S(valuePtr)), value2Ptr);
1272 TclDecrRefCount(elemPtr);
1273 TclDecrRefCount(valuePtr);
1275 ADJUST_PC(pcAdjustment);
1277 case INST_STORE_ARRAY_STK:
1281 valuePtr = POP_OBJECT();
1282 elemPtr = POP_OBJECT();
1283 objPtr = POP_OBJECT(); /* array name */
1284 DECACHE_STACK_INFO();
1285 value2Ptr = Tcl_ObjSetVar2(interp, objPtr, elemPtr, valuePtr,
1288 if (value2Ptr == NULL) {
1289 TRACE_WITH_OBJ(("\"%.30s(%.30s)\" <- \"%.30s\" => ERROR: ",
1290 O2S(objPtr), O2S(elemPtr), O2S(valuePtr)),
1291 Tcl_GetObjResult(interp));
1292 Tcl_DecrRefCount(objPtr);
1293 Tcl_DecrRefCount(elemPtr);
1294 Tcl_DecrRefCount(valuePtr);
1298 PUSH_OBJECT(value2Ptr);
1299 TRACE_WITH_OBJ(("\"%.30s(%.30s)\" <- \"%.30s\" => ",
1300 O2S(objPtr), O2S(elemPtr), O2S(valuePtr)),
1302 TclDecrRefCount(objPtr);
1303 TclDecrRefCount(elemPtr);
1304 TclDecrRefCount(valuePtr);
1308 case INST_STORE_STK:
1309 valuePtr = POP_OBJECT();
1310 objPtr = POP_OBJECT(); /* variable name */
1311 DECACHE_STACK_INFO();
1312 value2Ptr = Tcl_ObjSetVar2(interp, objPtr, NULL, valuePtr,
1315 if (value2Ptr == NULL) {
1316 TRACE_WITH_OBJ(("\"%.30s\" <- \"%.30s\" => ERROR: ",
1317 O2S(objPtr), O2S(valuePtr)),
1318 Tcl_GetObjResult(interp));
1319 Tcl_DecrRefCount(objPtr);
1320 Tcl_DecrRefCount(valuePtr);
1324 PUSH_OBJECT(value2Ptr);
1325 TRACE_WITH_OBJ(("\"%.30s\" <- \"%.30s\" => ",
1326 O2S(objPtr), O2S(valuePtr)), value2Ptr);
1327 TclDecrRefCount(objPtr);
1328 TclDecrRefCount(valuePtr);
1331 case INST_INCR_SCALAR1:
1332 opnd = TclGetUInt1AtPtr(pc+1);
1333 valuePtr = POP_OBJECT();
1334 if (valuePtr->typePtr != &tclIntType) {
1335 result = tclIntType.setFromAnyProc(interp, valuePtr);
1336 if (result != TCL_OK) {
1337 TRACE_WITH_OBJ(("%u (by %s) => ERROR converting increment amount to int: ",
1338 opnd, O2S(valuePtr)), Tcl_GetObjResult(interp));
1339 Tcl_DecrRefCount(valuePtr);
1343 i = valuePtr->internalRep.longValue;
1344 DECACHE_STACK_INFO();
1345 value2Ptr = TclIncrIndexedScalar(interp, opnd, i);
1347 if (value2Ptr == NULL) {
1348 TRACE_WITH_OBJ(("%u (by %ld) => ERROR: ", opnd, i),
1349 Tcl_GetObjResult(interp));
1350 Tcl_DecrRefCount(valuePtr);
1354 PUSH_OBJECT(value2Ptr);
1355 TRACE_WITH_OBJ(("%u (by %ld) => ", opnd, i), value2Ptr);
1356 TclDecrRefCount(valuePtr);
1359 case INST_INCR_SCALAR_STK:
1361 valuePtr = POP_OBJECT();
1362 objPtr = POP_OBJECT(); /* scalar name */
1363 if (valuePtr->typePtr != &tclIntType) {
1364 result = tclIntType.setFromAnyProc(interp, valuePtr);
1365 if (result != TCL_OK) {
1366 TRACE_WITH_OBJ(("\"%.30s\" (by %s) => ERROR converting increment amount to int: ",
1367 O2S(objPtr), O2S(valuePtr)),
1368 Tcl_GetObjResult(interp));
1369 Tcl_DecrRefCount(objPtr);
1370 Tcl_DecrRefCount(valuePtr);
1374 i = valuePtr->internalRep.longValue;
1375 DECACHE_STACK_INFO();
1376 value2Ptr = TclIncrVar2(interp, objPtr, (Tcl_Obj *) NULL, i,
1379 if (value2Ptr == NULL) {
1380 TRACE_WITH_OBJ(("\"%.30s\" (by %ld) => ERROR: ",
1381 O2S(objPtr), i), Tcl_GetObjResult(interp));
1382 Tcl_DecrRefCount(objPtr);
1383 Tcl_DecrRefCount(valuePtr);
1387 PUSH_OBJECT(value2Ptr);
1388 TRACE_WITH_OBJ(("\"%.30s\" (by %ld) => ", O2S(objPtr), i),
1390 Tcl_DecrRefCount(objPtr);
1391 Tcl_DecrRefCount(valuePtr);
1394 case INST_INCR_ARRAY1:
1398 opnd = TclGetUInt1AtPtr(pc+1);
1399 valuePtr = POP_OBJECT();
1400 elemPtr = POP_OBJECT();
1401 if (valuePtr->typePtr != &tclIntType) {
1402 result = tclIntType.setFromAnyProc(interp, valuePtr);
1403 if (result != TCL_OK) {
1404 TRACE_WITH_OBJ(("%u \"%.30s\" (by %s) => ERROR converting increment amount to int: ",
1405 opnd, O2S(elemPtr), O2S(valuePtr)),
1406 Tcl_GetObjResult(interp));
1407 Tcl_DecrRefCount(elemPtr);
1408 Tcl_DecrRefCount(valuePtr);
1412 i = valuePtr->internalRep.longValue;
1413 DECACHE_STACK_INFO();
1414 value2Ptr = TclIncrElementOfIndexedArray(interp, opnd,
1417 if (value2Ptr == NULL) {
1418 TRACE_WITH_OBJ(("%u \"%.30s\" (by %ld) => ERROR: ",
1419 opnd, O2S(elemPtr), i),
1420 Tcl_GetObjResult(interp));
1421 Tcl_DecrRefCount(elemPtr);
1422 Tcl_DecrRefCount(valuePtr);
1426 PUSH_OBJECT(value2Ptr);
1427 TRACE_WITH_OBJ(("%u \"%.30s\" (by %ld) => ",
1428 opnd, O2S(elemPtr), i), value2Ptr);
1429 Tcl_DecrRefCount(elemPtr);
1430 Tcl_DecrRefCount(valuePtr);
1434 case INST_INCR_ARRAY_STK:
1438 valuePtr = POP_OBJECT();
1439 elemPtr = POP_OBJECT();
1440 objPtr = POP_OBJECT(); /* array name */
1441 if (valuePtr->typePtr != &tclIntType) {
1442 result = tclIntType.setFromAnyProc(interp, valuePtr);
1443 if (result != TCL_OK) {
1444 TRACE_WITH_OBJ(("\"%.30s(%.30s)\" (by %s) => ERROR converting increment amount to int: ",
1445 O2S(objPtr), O2S(elemPtr), O2S(valuePtr)),
1446 Tcl_GetObjResult(interp));
1447 Tcl_DecrRefCount(objPtr);
1448 Tcl_DecrRefCount(elemPtr);
1449 Tcl_DecrRefCount(valuePtr);
1453 i = valuePtr->internalRep.longValue;
1454 DECACHE_STACK_INFO();
1455 value2Ptr = TclIncrVar2(interp, objPtr, elemPtr, i,
1458 if (value2Ptr == NULL) {
1459 TRACE_WITH_OBJ(("\"%.30s(%.30s)\" (by %ld) => ERROR: ",
1460 O2S(objPtr), O2S(elemPtr), i),
1461 Tcl_GetObjResult(interp));
1462 Tcl_DecrRefCount(objPtr);
1463 Tcl_DecrRefCount(elemPtr);
1464 Tcl_DecrRefCount(valuePtr);
1468 PUSH_OBJECT(value2Ptr);
1469 TRACE_WITH_OBJ(("\"%.30s(%.30s)\" (by %ld) => ",
1470 O2S(objPtr), O2S(elemPtr), i), value2Ptr);
1471 Tcl_DecrRefCount(objPtr);
1472 Tcl_DecrRefCount(elemPtr);
1473 Tcl_DecrRefCount(valuePtr);
1477 case INST_INCR_SCALAR1_IMM:
1478 opnd = TclGetUInt1AtPtr(pc+1);
1479 i = TclGetInt1AtPtr(pc+2);
1480 DECACHE_STACK_INFO();
1481 value2Ptr = TclIncrIndexedScalar(interp, opnd, i);
1483 if (value2Ptr == NULL) {
1484 TRACE_WITH_OBJ(("%u %ld => ERROR: ", opnd, i),
1485 Tcl_GetObjResult(interp));
1489 PUSH_OBJECT(value2Ptr);
1490 TRACE_WITH_OBJ(("%u %ld => ", opnd, i), value2Ptr);
1493 case INST_INCR_SCALAR_STK_IMM:
1494 case INST_INCR_STK_IMM:
1495 objPtr = POP_OBJECT(); /* variable name */
1496 i = TclGetInt1AtPtr(pc+1);
1497 DECACHE_STACK_INFO();
1498 value2Ptr = TclIncrVar2(interp, objPtr, (Tcl_Obj *) NULL, i,
1501 if (value2Ptr == NULL) {
1502 TRACE_WITH_OBJ(("\"%.30s\" %ld => ERROR: ",
1503 O2S(objPtr), i), Tcl_GetObjResult(interp));
1505 Tcl_DecrRefCount(objPtr);
1508 PUSH_OBJECT(value2Ptr);
1509 TRACE_WITH_OBJ(("\"%.30s\" %ld => ", O2S(objPtr), i),
1511 TclDecrRefCount(objPtr);
1514 case INST_INCR_ARRAY1_IMM:
1518 opnd = TclGetUInt1AtPtr(pc+1);
1519 i = TclGetInt1AtPtr(pc+2);
1520 elemPtr = POP_OBJECT();
1521 DECACHE_STACK_INFO();
1522 value2Ptr = TclIncrElementOfIndexedArray(interp, opnd,
1525 if (value2Ptr == NULL) {
1526 TRACE_WITH_OBJ(("%u \"%.30s\" (by %ld) => ERROR: ",
1527 opnd, O2S(elemPtr), i),
1528 Tcl_GetObjResult(interp));
1529 Tcl_DecrRefCount(elemPtr);
1533 PUSH_OBJECT(value2Ptr);
1534 TRACE_WITH_OBJ(("%u \"%.30s\" (by %ld) => ",
1535 opnd, O2S(elemPtr), i), value2Ptr);
1536 Tcl_DecrRefCount(elemPtr);
1540 case INST_INCR_ARRAY_STK_IMM:
1544 i = TclGetInt1AtPtr(pc+1);
1545 elemPtr = POP_OBJECT();
1546 objPtr = POP_OBJECT(); /* array name */
1547 DECACHE_STACK_INFO();
1548 value2Ptr = TclIncrVar2(interp, objPtr, elemPtr, i,
1551 if (value2Ptr == NULL) {
1552 TRACE_WITH_OBJ(("\"%.30s(%.30s)\" (by %ld) => ERROR: ",
1553 O2S(objPtr), O2S(elemPtr), i),
1554 Tcl_GetObjResult(interp));
1555 Tcl_DecrRefCount(objPtr);
1556 Tcl_DecrRefCount(elemPtr);
1560 PUSH_OBJECT(value2Ptr);
1561 TRACE_WITH_OBJ(("\"%.30s(%.30s)\" (by %ld) => ",
1562 O2S(objPtr), O2S(elemPtr), i), value2Ptr);
1563 Tcl_DecrRefCount(objPtr);
1564 Tcl_DecrRefCount(elemPtr);
1569 #ifdef TCL_COMPILE_DEBUG
1570 opnd = TclGetInt1AtPtr(pc+1);
1571 TRACE(("%d => new pc %u\n", opnd,
1572 (unsigned int)(pc + opnd - codePtr->codeStart)));
1575 pc += TclGetInt1AtPtr(pc+1);
1576 #endif /* TCL_COMPILE_DEBUG */
1580 opnd = TclGetInt4AtPtr(pc+1);
1581 TRACE(("%d => new pc %u\n", opnd,
1582 (unsigned int)(pc + opnd - codePtr->codeStart)));
1585 case INST_JUMP_TRUE4:
1586 opnd = TclGetInt4AtPtr(pc+1);
1590 case INST_JUMP_TRUE1:
1591 opnd = TclGetInt1AtPtr(pc+1);
1598 valuePtr = POP_OBJECT();
1599 if (valuePtr->typePtr == &tclIntType) {
1600 b = (valuePtr->internalRep.longValue != 0);
1601 } else if (valuePtr->typePtr == &tclDoubleType) {
1602 b = (valuePtr->internalRep.doubleValue != 0.0);
1604 result = Tcl_GetBooleanFromObj(interp, valuePtr, &b);
1605 if (result != TCL_OK) {
1606 TRACE_WITH_OBJ(("%d => ERROR: ", opnd),
1607 Tcl_GetObjResult(interp));
1608 Tcl_DecrRefCount(valuePtr);
1613 TRACE(("%d => %.20s true, new pc %u\n",
1614 opnd, O2S(valuePtr),
1615 (unsigned int)(pc+opnd - codePtr->codeStart)));
1616 TclDecrRefCount(valuePtr);
1619 TRACE(("%d => %.20s false\n", opnd, O2S(valuePtr)));
1620 TclDecrRefCount(valuePtr);
1621 ADJUST_PC(pcAdjustment);
1625 case INST_JUMP_FALSE4:
1626 opnd = TclGetInt4AtPtr(pc+1);
1630 case INST_JUMP_FALSE1:
1631 opnd = TclGetInt1AtPtr(pc+1);
1638 valuePtr = POP_OBJECT();
1639 if (valuePtr->typePtr == &tclIntType) {
1640 b = (valuePtr->internalRep.longValue != 0);
1641 } else if (valuePtr->typePtr == &tclDoubleType) {
1642 b = (valuePtr->internalRep.doubleValue != 0.0);
1644 result = Tcl_GetBooleanFromObj(interp, valuePtr, &b);
1645 if (result != TCL_OK) {
1646 TRACE_WITH_OBJ(("%d => ERROR: ", opnd),
1647 Tcl_GetObjResult(interp));
1648 Tcl_DecrRefCount(valuePtr);
1653 TRACE(("%d => %.20s true\n", opnd, O2S(valuePtr)));
1654 TclDecrRefCount(valuePtr);
1655 ADJUST_PC(pcAdjustment);
1657 TRACE(("%d => %.20s false, new pc %u\n",
1658 opnd, O2S(valuePtr),
1659 (unsigned int)(pc + opnd - codePtr->codeStart)));
1660 TclDecrRefCount(valuePtr);
1669 * Operands must be boolean or numeric. No int->double
1670 * conversions are performed.
1676 Tcl_ObjType *t1Ptr, *t2Ptr;
1678 value2Ptr = POP_OBJECT();
1679 valuePtr = POP_OBJECT();
1680 t1Ptr = valuePtr->typePtr;
1681 t2Ptr = value2Ptr->typePtr;
1683 if ((t1Ptr == &tclIntType) || (t1Ptr == &tclBooleanType)) {
1684 i1 = (valuePtr->internalRep.longValue != 0);
1685 } else if (t1Ptr == &tclDoubleType) {
1686 i1 = (valuePtr->internalRep.doubleValue != 0.0);
1688 s = Tcl_GetStringFromObj(valuePtr, &length);
1689 if (TclLooksLikeInt(s, length)) {
1690 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
1694 result = Tcl_GetBooleanFromObj((Tcl_Interp *) NULL,
1698 if (result != TCL_OK) {
1699 TRACE(("\"%.20s\" => ILLEGAL TYPE %s \n",
1701 (t1Ptr? t1Ptr->name : "null")));
1702 IllegalExprOperandType(interp, pc, valuePtr);
1703 Tcl_DecrRefCount(valuePtr);
1704 Tcl_DecrRefCount(value2Ptr);
1709 if ((t2Ptr == &tclIntType) || (t2Ptr == &tclBooleanType)) {
1710 i2 = (value2Ptr->internalRep.longValue != 0);
1711 } else if (t2Ptr == &tclDoubleType) {
1712 i2 = (value2Ptr->internalRep.doubleValue != 0.0);
1714 s = Tcl_GetStringFromObj(value2Ptr, &length);
1715 if (TclLooksLikeInt(s, length)) {
1716 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
1720 result = Tcl_GetBooleanFromObj((Tcl_Interp *) NULL,
1723 if (result != TCL_OK) {
1724 TRACE(("\"%.20s\" => ILLEGAL TYPE %s \n",
1726 (t2Ptr? t2Ptr->name : "null")));
1727 IllegalExprOperandType(interp, pc, value2Ptr);
1728 Tcl_DecrRefCount(valuePtr);
1729 Tcl_DecrRefCount(value2Ptr);
1735 * Reuse the valuePtr object already on stack if possible.
1738 if (*pc == INST_LOR) {
1739 iResult = (i1 || i2);
1741 iResult = (i1 && i2);
1743 if (Tcl_IsShared(valuePtr)) {
1744 PUSH_OBJECT(Tcl_NewLongObj(iResult));
1745 TRACE(("%.20s %.20s => %d\n",
1746 O2S(valuePtr), O2S(value2Ptr), iResult));
1747 TclDecrRefCount(valuePtr);
1748 } else { /* reuse the valuePtr object */
1749 TRACE(("%.20s %.20s => %d\n",
1750 O2S(valuePtr), O2S(value2Ptr), iResult));
1751 Tcl_SetLongObj(valuePtr, iResult);
1752 ++stackTop; /* valuePtr now on stk top has right r.c. */
1754 TclDecrRefCount(value2Ptr);
1766 * Any type is allowed but the two operands must have the
1767 * same type. We will compute value op value2.
1770 Tcl_ObjType *t1Ptr, *t2Ptr;
1771 char *s1 = NULL; /* Init. avoids compiler warning. */
1772 char *s2 = NULL; /* Init. avoids compiler warning. */
1773 long i2 = 0; /* Init. avoids compiler warning. */
1774 double d1 = 0.0; /* Init. avoids compiler warning. */
1775 double d2 = 0.0; /* Init. avoids compiler warning. */
1776 long iResult = 0; /* Init. avoids compiler warning. */
1778 value2Ptr = POP_OBJECT();
1779 valuePtr = POP_OBJECT();
1780 t1Ptr = valuePtr->typePtr;
1781 t2Ptr = value2Ptr->typePtr;
1784 * We only want to coerce numeric validation if
1785 * neither type is NULL. A NULL type means the arg is
1786 * essentially an empty object ("", {} or [list]).
1788 if (!((((t1Ptr == NULL) && (valuePtr->bytes == NULL))
1789 || (valuePtr->bytes && (valuePtr->length == 0)))
1790 || (((t2Ptr == NULL) && (value2Ptr->bytes == NULL))
1791 || (value2Ptr->bytes && (value2Ptr->length == 0))))) {
1792 if ((t1Ptr != &tclIntType) && (t1Ptr != &tclDoubleType)) {
1793 s1 = Tcl_GetStringFromObj(valuePtr, &length);
1794 if (TclLooksLikeInt(s1, length)) {
1795 (void) Tcl_GetLongFromObj((Tcl_Interp *) NULL,
1798 (void) Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
1801 t1Ptr = valuePtr->typePtr;
1803 if ((t2Ptr != &tclIntType) && (t2Ptr != &tclDoubleType)) {
1804 s2 = Tcl_GetStringFromObj(value2Ptr, &length);
1805 if (TclLooksLikeInt(s2, length)) {
1806 (void) Tcl_GetLongFromObj((Tcl_Interp *) NULL,
1809 (void) Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
1812 t2Ptr = value2Ptr->typePtr;
1815 if (((t1Ptr != &tclIntType) && (t1Ptr != &tclDoubleType))
1816 || ((t2Ptr != &tclIntType) && (t2Ptr != &tclDoubleType))) {
1818 * One operand is not numeric. Compare as strings.
1821 s1 = Tcl_GetString(valuePtr);
1822 s2 = Tcl_GetString(value2Ptr);
1823 cmpValue = strcmp(s1, s2);
1826 iResult = (cmpValue == 0);
1829 iResult = (cmpValue != 0);
1832 iResult = (cmpValue < 0);
1835 iResult = (cmpValue > 0);
1838 iResult = (cmpValue <= 0);
1841 iResult = (cmpValue >= 0);
1844 } else if ((t1Ptr == &tclDoubleType)
1845 || (t2Ptr == &tclDoubleType)) {
1847 * Compare as doubles.
1849 if (t1Ptr == &tclDoubleType) {
1850 d1 = valuePtr->internalRep.doubleValue;
1851 if (t2Ptr == &tclIntType) {
1852 d2 = value2Ptr->internalRep.longValue;
1854 d2 = value2Ptr->internalRep.doubleValue;
1856 } else { /* t1Ptr is int, t2Ptr is double */
1857 d1 = valuePtr->internalRep.longValue;
1858 d2 = value2Ptr->internalRep.doubleValue;
1884 i = valuePtr->internalRep.longValue;
1885 i2 = value2Ptr->internalRep.longValue;
1909 * Reuse the valuePtr object already on stack if possible.
1912 if (Tcl_IsShared(valuePtr)) {
1913 PUSH_OBJECT(Tcl_NewLongObj(iResult));
1914 TRACE(("%.20s %.20s => %ld\n",
1915 O2S(valuePtr), O2S(value2Ptr), iResult));
1916 TclDecrRefCount(valuePtr);
1917 } else { /* reuse the valuePtr object */
1918 TRACE(("%.20s %.20s => %ld\n",
1919 O2S(valuePtr), O2S(value2Ptr), iResult));
1920 Tcl_SetLongObj(valuePtr, iResult);
1921 ++stackTop; /* valuePtr now on stk top has right r.c. */
1923 TclDecrRefCount(value2Ptr);
1935 * Only integers are allowed. We compute value op value2.
1938 long i2, rem, negative;
1939 long iResult = 0; /* Init. avoids compiler warning. */
1941 value2Ptr = POP_OBJECT();
1942 valuePtr = POP_OBJECT();
1943 if (valuePtr->typePtr == &tclIntType) {
1944 i = valuePtr->internalRep.longValue;
1945 } else { /* try to convert to int */
1946 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
1948 if (result != TCL_OK) {
1949 TRACE(("%.20s %.20s => ILLEGAL 1st TYPE %s\n",
1950 O2S(valuePtr), O2S(value2Ptr),
1952 valuePtr->typePtr->name : "null")));
1953 IllegalExprOperandType(interp, pc, valuePtr);
1954 Tcl_DecrRefCount(valuePtr);
1955 Tcl_DecrRefCount(value2Ptr);
1959 if (value2Ptr->typePtr == &tclIntType) {
1960 i2 = value2Ptr->internalRep.longValue;
1962 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
1964 if (result != TCL_OK) {
1965 TRACE(("%.20s %.20s => ILLEGAL 2nd TYPE %s\n",
1966 O2S(valuePtr), O2S(value2Ptr),
1967 (value2Ptr->typePtr?
1968 value2Ptr->typePtr->name : "null")));
1969 IllegalExprOperandType(interp, pc, value2Ptr);
1970 Tcl_DecrRefCount(valuePtr);
1971 Tcl_DecrRefCount(value2Ptr);
1979 * This code is tricky: C doesn't guarantee much about
1980 * the quotient or remainder, but Tcl does. The
1981 * remainder always has the same sign as the divisor and
1982 * a smaller absolute value.
1985 TRACE(("%ld %ld => DIVIDE BY ZERO\n", i, i2));
1986 Tcl_DecrRefCount(valuePtr);
1987 Tcl_DecrRefCount(value2Ptr);
2010 * The following code is a bit tricky: it ensures that
2011 * right shifts propagate the sign bit even on machines
2012 * where ">>" won't do it by default.
2015 iResult = ~((~i) >> i2);
2032 * Reuse the valuePtr object already on stack if possible.
2035 if (Tcl_IsShared(valuePtr)) {
2036 PUSH_OBJECT(Tcl_NewLongObj(iResult));
2037 TRACE(("%ld %ld => %ld\n", i, i2, iResult));
2038 TclDecrRefCount(valuePtr);
2039 } else { /* reuse the valuePtr object */
2040 TRACE(("%ld %ld => %ld\n", i, i2, iResult));
2041 Tcl_SetLongObj(valuePtr, iResult);
2042 ++stackTop; /* valuePtr now on stk top has right r.c. */
2044 TclDecrRefCount(value2Ptr);
2054 * Operands must be numeric and ints get converted to floats
2055 * if necessary. We compute value op value2.
2058 Tcl_ObjType *t1Ptr, *t2Ptr;
2061 long iResult = 0; /* Init. avoids compiler warning. */
2062 double dResult = 0.0; /* Init. avoids compiler warning. */
2063 int doDouble = 0; /* 1 if doing floating arithmetic */
2065 value2Ptr = POP_OBJECT();
2066 valuePtr = POP_OBJECT();
2067 t1Ptr = valuePtr->typePtr;
2068 t2Ptr = value2Ptr->typePtr;
2070 if (t1Ptr == &tclIntType) {
2071 i = valuePtr->internalRep.longValue;
2072 } else if ((t1Ptr == &tclDoubleType)
2073 && (valuePtr->bytes == NULL)) {
2075 * We can only use the internal rep directly if there is
2076 * no string rep. Otherwise the string rep might actually
2077 * look like an integer, which is preferred.
2080 d1 = valuePtr->internalRep.doubleValue;
2082 char *s = Tcl_GetStringFromObj(valuePtr, &length);
2083 if (TclLooksLikeInt(s, length)) {
2084 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
2087 result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
2090 if (result != TCL_OK) {
2091 TRACE(("%.20s %.20s => ILLEGAL 1st TYPE %s\n",
2094 valuePtr->typePtr->name : "null")));
2095 IllegalExprOperandType(interp, pc, valuePtr);
2096 Tcl_DecrRefCount(valuePtr);
2097 Tcl_DecrRefCount(value2Ptr);
2100 t1Ptr = valuePtr->typePtr;
2103 if (t2Ptr == &tclIntType) {
2104 i2 = value2Ptr->internalRep.longValue;
2105 } else if ((t2Ptr == &tclDoubleType)
2106 && (value2Ptr->bytes == NULL)) {
2108 * We can only use the internal rep directly if there is
2109 * no string rep. Otherwise the string rep might actually
2110 * look like an integer, which is preferred.
2113 d2 = value2Ptr->internalRep.doubleValue;
2115 char *s = Tcl_GetStringFromObj(value2Ptr, &length);
2116 if (TclLooksLikeInt(s, length)) {
2117 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
2120 result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
2123 if (result != TCL_OK) {
2124 TRACE(("%.20s %.20s => ILLEGAL 2nd TYPE %s\n",
2126 (value2Ptr->typePtr?
2127 value2Ptr->typePtr->name : "null")));
2128 IllegalExprOperandType(interp, pc, value2Ptr);
2129 Tcl_DecrRefCount(valuePtr);
2130 Tcl_DecrRefCount(value2Ptr);
2133 t2Ptr = value2Ptr->typePtr;
2136 if ((t1Ptr == &tclDoubleType) || (t2Ptr == &tclDoubleType)) {
2138 * Do double arithmetic.
2141 if (t1Ptr == &tclIntType) {
2142 d1 = i; /* promote value 1 to double */
2143 } else if (t2Ptr == &tclIntType) {
2144 d2 = i2; /* promote value 2 to double */
2158 TRACE(("%.6g %.6g => DIVIDE BY ZERO\n", d1, d2));
2159 Tcl_DecrRefCount(valuePtr);
2160 Tcl_DecrRefCount(value2Ptr);
2168 * Check now for IEEE floating-point error.
2171 if (IS_NAN(dResult) || IS_INF(dResult)) {
2172 TRACE(("%.20s %.20s => IEEE FLOATING PT ERROR\n",
2173 O2S(valuePtr), O2S(value2Ptr)));
2174 TclExprFloatError(interp, dResult);
2176 Tcl_DecrRefCount(valuePtr);
2177 Tcl_DecrRefCount(value2Ptr);
2182 * Do integer arithmetic.
2196 * This code is tricky: C doesn't guarantee much
2197 * about the quotient or remainder, but Tcl does.
2198 * The remainder always has the same sign as the
2199 * divisor and a smaller absolute value.
2202 TRACE(("%ld %ld => DIVIDE BY ZERO\n", i, i2));
2203 Tcl_DecrRefCount(valuePtr);
2204 Tcl_DecrRefCount(value2Ptr);
2222 * Reuse the valuePtr object already on stack if possible.
2225 if (Tcl_IsShared(valuePtr)) {
2227 PUSH_OBJECT(Tcl_NewDoubleObj(dResult));
2228 TRACE(("%.6g %.6g => %.6g\n", d1, d2, dResult));
2230 PUSH_OBJECT(Tcl_NewLongObj(iResult));
2231 TRACE(("%ld %ld => %ld\n", i, i2, iResult));
2233 TclDecrRefCount(valuePtr);
2234 } else { /* reuse the valuePtr object */
2235 if (doDouble) { /* NB: stack top is off by 1 */
2236 TRACE(("%.6g %.6g => %.6g\n", d1, d2, dResult));
2237 Tcl_SetDoubleObj(valuePtr, dResult);
2239 TRACE(("%ld %ld => %ld\n", i, i2, iResult));
2240 Tcl_SetLongObj(valuePtr, iResult);
2242 ++stackTop; /* valuePtr now on stk top has right r.c. */
2244 TclDecrRefCount(value2Ptr);
2251 * Operand must be numeric.
2257 valuePtr = stackPtr[stackTop];
2258 tPtr = valuePtr->typePtr;
2259 if ((tPtr != &tclIntType) && ((tPtr != &tclDoubleType)
2260 || (valuePtr->bytes != NULL))) {
2261 char *s = Tcl_GetStringFromObj(valuePtr, &length);
2262 if (TclLooksLikeInt(s, length)) {
2263 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
2266 result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
2269 if (result != TCL_OK) {
2270 TRACE(("\"%.20s\" => ILLEGAL TYPE %s \n",
2271 s, (tPtr? tPtr->name : "null")));
2272 IllegalExprOperandType(interp, pc, valuePtr);
2275 tPtr = valuePtr->typePtr;
2279 * Ensure that the operand's string rep is the same as the
2280 * formatted version of its internal rep. This makes sure
2281 * that "expr +000123" yields "83", not "000123". We
2282 * implement this by _discarding_ the string rep since we
2283 * know it will be regenerated, if needed later, by
2284 * formatting the internal rep's value.
2287 if (Tcl_IsShared(valuePtr)) {
2288 if (tPtr == &tclIntType) {
2289 i = valuePtr->internalRep.longValue;
2290 objPtr = Tcl_NewLongObj(i);
2292 d = valuePtr->internalRep.doubleValue;
2293 objPtr = Tcl_NewDoubleObj(d);
2295 Tcl_IncrRefCount(objPtr);
2296 Tcl_DecrRefCount(valuePtr);
2298 stackPtr[stackTop] = valuePtr;
2300 Tcl_InvalidateStringRep(valuePtr);
2302 TRACE_WITH_OBJ(("%s => ", O2S(valuePtr)), valuePtr);
2310 * The operand must be numeric. If the operand object is
2311 * unshared modify it directly, otherwise create a copy to
2312 * modify: this is "copy on write". free any old string
2313 * representation since it is now invalid.
2319 valuePtr = POP_OBJECT();
2320 tPtr = valuePtr->typePtr;
2321 if ((tPtr != &tclIntType) && ((tPtr != &tclDoubleType)
2322 || (valuePtr->bytes != NULL))) {
2323 if ((tPtr == &tclBooleanType)
2324 && (valuePtr->bytes == NULL)) {
2325 valuePtr->typePtr = &tclIntType;
2327 char *s = Tcl_GetStringFromObj(valuePtr, &length);
2328 if (TclLooksLikeInt(s, length)) {
2329 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
2332 result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
2335 if (result != TCL_OK) {
2336 TRACE(("\"%.20s\" => ILLEGAL TYPE %s\n",
2337 s, (tPtr? tPtr->name : "null")));
2338 IllegalExprOperandType(interp, pc, valuePtr);
2339 Tcl_DecrRefCount(valuePtr);
2343 tPtr = valuePtr->typePtr;
2346 if (Tcl_IsShared(valuePtr)) {
2348 * Create a new object.
2350 if (tPtr == &tclIntType) {
2351 i = valuePtr->internalRep.longValue;
2352 objPtr = Tcl_NewLongObj(
2353 (*pc == INST_UMINUS)? -i : !i);
2354 TRACE_WITH_OBJ(("%ld => ", i), objPtr);
2356 d = valuePtr->internalRep.doubleValue;
2357 if (*pc == INST_UMINUS) {
2358 objPtr = Tcl_NewDoubleObj(-d);
2361 * Should be able to use "!d", but apparently
2362 * some compilers can't handle it.
2364 objPtr = Tcl_NewLongObj((d==0.0)? 1 : 0);
2366 TRACE_WITH_OBJ(("%.6g => ", d), objPtr);
2368 PUSH_OBJECT(objPtr);
2369 TclDecrRefCount(valuePtr);
2372 * valuePtr is unshared. Modify it directly.
2374 if (tPtr == &tclIntType) {
2375 i = valuePtr->internalRep.longValue;
2376 Tcl_SetLongObj(valuePtr,
2377 (*pc == INST_UMINUS)? -i : !i);
2378 TRACE_WITH_OBJ(("%ld => ", i), valuePtr);
2380 d = valuePtr->internalRep.doubleValue;
2381 if (*pc == INST_UMINUS) {
2382 Tcl_SetDoubleObj(valuePtr, -d);
2385 * Should be able to use "!d", but apparently
2386 * some compilers can't handle it.
2388 Tcl_SetLongObj(valuePtr, (d==0.0)? 1 : 0);
2390 TRACE_WITH_OBJ(("%.6g => ", d), valuePtr);
2392 ++stackTop; /* valuePtr now on stk top has right r.c. */
2400 * The operand must be an integer. If the operand object is
2401 * unshared modify it directly, otherwise modify a copy.
2402 * Free any old string representation since it is now
2408 valuePtr = POP_OBJECT();
2409 tPtr = valuePtr->typePtr;
2410 if (tPtr != &tclIntType) {
2411 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
2413 if (result != TCL_OK) { /* try to convert to double */
2414 TRACE(("\"%.20s\" => ILLEGAL TYPE %s\n",
2415 O2S(valuePtr), (tPtr? tPtr->name : "null")));
2416 IllegalExprOperandType(interp, pc, valuePtr);
2417 Tcl_DecrRefCount(valuePtr);
2422 i = valuePtr->internalRep.longValue;
2423 if (Tcl_IsShared(valuePtr)) {
2424 PUSH_OBJECT(Tcl_NewLongObj(~i));
2425 TRACE(("0x%lx => (%lu)\n", i, ~i));
2426 TclDecrRefCount(valuePtr);
2429 * valuePtr is unshared. Modify it directly.
2431 Tcl_SetLongObj(valuePtr, ~i);
2432 ++stackTop; /* valuePtr now on stk top has right r.c. */
2433 TRACE(("0x%lx => (%lu)\n", i, ~i));
2438 case INST_CALL_BUILTIN_FUNC1:
2439 opnd = TclGetUInt1AtPtr(pc+1);
2442 * Call one of the built-in Tcl math functions.
2445 BuiltinFunc *mathFuncPtr;
2446 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
2448 if ((opnd < 0) || (opnd > LAST_BUILTIN_FUNC)) {
2449 TRACE(("UNRECOGNIZED BUILTIN FUNC CODE %d\n", opnd));
2450 panic("TclExecuteByteCode: unrecognized builtin function code %d", opnd);
2452 mathFuncPtr = &(builtinFuncTable[opnd]);
2453 DECACHE_STACK_INFO();
2454 tsdPtr->mathInProgress++;
2455 result = (*mathFuncPtr->proc)(interp, eePtr,
2456 mathFuncPtr->clientData);
2457 tsdPtr->mathInProgress--;
2459 if (result != TCL_OK) {
2462 TRACE_WITH_OBJ(("%d => ", opnd), stackPtr[stackTop]);
2466 case INST_CALL_FUNC1:
2467 opnd = TclGetUInt1AtPtr(pc+1);
2470 * Call a non-builtin Tcl math function previously
2471 * registered by a call to Tcl_CreateMathFunc.
2474 int objc = opnd; /* Number of arguments. The function name
2475 * is the 0-th argument. */
2476 Tcl_Obj **objv; /* The array of arguments. The function
2477 * name is objv[0]. */
2478 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
2480 objv = &(stackPtr[stackTop - (objc-1)]); /* "objv[0]" */
2481 DECACHE_STACK_INFO();
2482 tsdPtr->mathInProgress++;
2483 result = ExprCallMathFunc(interp, eePtr, objc, objv);
2484 tsdPtr->mathInProgress--;
2486 if (result != TCL_OK) {
2489 TRACE_WITH_OBJ(("%d => ", objc), stackPtr[stackTop]);
2493 case INST_TRY_CVT_TO_NUMERIC:
2496 * Try to convert the topmost stack object to an int or
2497 * double object. This is done in order to support Tcl's
2498 * policy of interpreting operands if at all possible as
2499 * first integers, else floating-point numbers.
2505 int converted, shared;
2507 valuePtr = stackPtr[stackTop];
2508 tPtr = valuePtr->typePtr;
2510 if ((tPtr != &tclIntType) && ((tPtr != &tclDoubleType)
2511 || (valuePtr->bytes != NULL))) {
2512 if ((tPtr == &tclBooleanType)
2513 && (valuePtr->bytes == NULL)) {
2514 valuePtr->typePtr = &tclIntType;
2517 s = Tcl_GetStringFromObj(valuePtr, &length);
2518 if (TclLooksLikeInt(s, length)) {
2519 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL,
2522 result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL,
2525 if (result == TCL_OK) {
2528 result = TCL_OK; /* reset the result variable */
2530 tPtr = valuePtr->typePtr;
2534 * Ensure that the topmost stack object, if numeric, has a
2535 * string rep the same as the formatted version of its
2536 * internal rep. This is used, e.g., to make sure that "expr
2537 * {0001}" yields "1", not "0001". We implement this by
2538 * _discarding_ the string rep since we know it will be
2539 * regenerated, if needed later, by formatting the internal
2540 * rep's value. Also check if there has been an IEEE
2541 * floating point error.
2544 if ((tPtr == &tclIntType) || (tPtr == &tclDoubleType)) {
2546 if (Tcl_IsShared(valuePtr)) {
2548 if (valuePtr->bytes != NULL) {
2550 * We only need to make a copy of the object
2551 * when it already had a string rep
2553 if (tPtr == &tclIntType) {
2554 i = valuePtr->internalRep.longValue;
2555 objPtr = Tcl_NewLongObj(i);
2557 d = valuePtr->internalRep.doubleValue;
2558 objPtr = Tcl_NewDoubleObj(d);
2560 Tcl_IncrRefCount(objPtr);
2561 TclDecrRefCount(valuePtr);
2563 stackPtr[stackTop] = valuePtr;
2564 tPtr = valuePtr->typePtr;
2567 Tcl_InvalidateStringRep(valuePtr);
2570 if (tPtr == &tclDoubleType) {
2571 d = valuePtr->internalRep.doubleValue;
2572 if (IS_NAN(d) || IS_INF(d)) {
2573 TRACE(("\"%.20s\" => IEEE FLOATING PT ERROR\n",
2575 TclExprFloatError(interp, d);
2580 shared = shared; /* lint, shared not used. */
2581 converted = converted; /* lint, converted not used. */
2582 TRACE(("\"%.20s\" => numeric, %s, %s\n", O2S(valuePtr),
2583 (converted? "converted" : "not converted"),
2584 (shared? "shared" : "not shared")));
2586 TRACE(("\"%.20s\" => not numeric\n", O2S(valuePtr)));
2593 * First reset the interpreter's result. Then find the closest
2594 * enclosing loop or catch exception range, if any. If a loop is
2595 * found, terminate its execution. If the closest is a catch
2596 * exception range, jump to its catchOffset. If no enclosing
2597 * range is found, stop execution and return TCL_BREAK.
2600 Tcl_ResetResult(interp);
2601 rangePtr = GetExceptRangeForPc(pc, /*catchOnly*/ 0, codePtr);
2602 if (rangePtr == NULL) {
2603 TRACE(("=> no encl. loop or catch, returning TCL_BREAK\n"));
2605 goto abnormalReturn; /* no catch exists to check */
2607 switch (rangePtr->type) {
2608 case LOOP_EXCEPTION_RANGE:
2610 TRACE(("=> range at %d, new pc %d\n",
2611 rangePtr->codeOffset, rangePtr->breakOffset));
2613 case CATCH_EXCEPTION_RANGE:
2615 TRACE(("=> ...\n"));
2616 goto processCatch; /* it will use rangePtr */
2618 panic("TclExecuteByteCode: unrecognized ExceptionRange type %d\n", rangePtr->type);
2620 pc = (codePtr->codeStart + rangePtr->breakOffset);
2621 continue; /* restart outer instruction loop at pc */
2625 * Find the closest enclosing loop or catch exception range,
2626 * if any. If a loop is found, skip to its next iteration.
2627 * If the closest is a catch exception range, jump to its
2628 * catchOffset. If no enclosing range is found, stop
2629 * execution and return TCL_CONTINUE.
2632 Tcl_ResetResult(interp);
2633 rangePtr = GetExceptRangeForPc(pc, /*catchOnly*/ 0, codePtr);
2634 if (rangePtr == NULL) {
2635 TRACE(("=> no encl. loop or catch, returning TCL_CONTINUE\n"));
2636 result = TCL_CONTINUE;
2637 goto abnormalReturn;
2639 switch (rangePtr->type) {
2640 case LOOP_EXCEPTION_RANGE:
2641 if (rangePtr->continueOffset == -1) {
2642 TRACE(("=> loop w/o continue, checking for catch\n"));
2646 TRACE(("=> range at %d, new pc %d\n",
2647 rangePtr->codeOffset, rangePtr->continueOffset));
2650 case CATCH_EXCEPTION_RANGE:
2651 result = TCL_CONTINUE;
2652 TRACE(("=> ...\n"));
2653 goto processCatch; /* it will use rangePtr */
2655 panic("TclExecuteByteCode: unrecognized ExceptionRange type %d\n", rangePtr->type);
2657 pc = (codePtr->codeStart + rangePtr->continueOffset);
2658 continue; /* restart outer instruction loop at pc */
2660 case INST_FOREACH_START4:
2661 opnd = TclGetUInt4AtPtr(pc+1);
2664 * Initialize the temporary local var that holds the count
2665 * of the number of iterations of the loop body to -1.
2668 ForeachInfo *infoPtr = (ForeachInfo *)
2669 codePtr->auxDataArrayPtr[opnd].clientData;
2670 int iterTmpIndex = infoPtr->loopCtTemp;
2671 Var *compiledLocals = iPtr->varFramePtr->compiledLocals;
2672 Var *iterVarPtr = &(compiledLocals[iterTmpIndex]);
2673 Tcl_Obj *oldValuePtr = iterVarPtr->value.objPtr;
2675 if (oldValuePtr == NULL) {
2676 iterVarPtr->value.objPtr = Tcl_NewLongObj(-1);
2677 Tcl_IncrRefCount(iterVarPtr->value.objPtr);
2679 Tcl_SetLongObj(oldValuePtr, -1);
2681 TclSetVarScalar(iterVarPtr);
2682 TclClearVarUndefined(iterVarPtr);
2683 TRACE(("%u => loop iter count temp %d\n",
2684 opnd, iterTmpIndex));
2688 case INST_FOREACH_STEP4:
2689 opnd = TclGetUInt4AtPtr(pc+1);
2692 * "Step" a foreach loop (i.e., begin its next iteration) by
2693 * assigning the next value list element to each loop var.
2696 ForeachInfo *infoPtr = (ForeachInfo *)
2697 codePtr->auxDataArrayPtr[opnd].clientData;
2698 ForeachVarList *varListPtr;
2699 int numLists = infoPtr->numLists;
2700 Var *compiledLocals = iPtr->varFramePtr->compiledLocals;
2703 Var *iterVarPtr, *listVarPtr;
2704 int iterNum, listTmpIndex, listLen, numVars;
2705 int varIndex, valIndex, continueLoop, j;
2708 * Increment the temp holding the loop iteration number.
2711 iterVarPtr = &(compiledLocals[infoPtr->loopCtTemp]);
2712 valuePtr = iterVarPtr->value.objPtr;
2713 iterNum = (valuePtr->internalRep.longValue + 1);
2714 Tcl_SetLongObj(valuePtr, iterNum);
2717 * Check whether all value lists are exhausted and we should
2722 listTmpIndex = infoPtr->firstValueTemp;
2723 for (i = 0; i < numLists; i++) {
2724 varListPtr = infoPtr->varLists[i];
2725 numVars = varListPtr->numVars;
2727 listVarPtr = &(compiledLocals[listTmpIndex]);
2728 listPtr = listVarPtr->value.objPtr;
2729 result = Tcl_ListObjLength(interp, listPtr, &listLen);
2730 if (result != TCL_OK) {
2731 TRACE_WITH_OBJ(("%u => ERROR converting list %ld, \"%s\": ",
2732 opnd, i, O2S(listPtr)),
2733 Tcl_GetObjResult(interp));
2736 if (listLen > (iterNum * numVars)) {
2743 * If some var in some var list still has a remaining list
2744 * element iterate one more time. Assign to var the next
2745 * element from its value list. We already checked above
2746 * that each list temp holds a valid list object.
2750 listTmpIndex = infoPtr->firstValueTemp;
2751 for (i = 0; i < numLists; i++) {
2752 varListPtr = infoPtr->varLists[i];
2753 numVars = varListPtr->numVars;
2755 listVarPtr = &(compiledLocals[listTmpIndex]);
2756 listPtr = listVarPtr->value.objPtr;
2757 listRepPtr = (List *) listPtr->internalRep.otherValuePtr;
2758 listLen = listRepPtr->elemCount;
2760 valIndex = (iterNum * numVars);
2761 for (j = 0; j < numVars; j++) {
2762 int setEmptyStr = 0;
2763 if (valIndex >= listLen) {
2765 valuePtr = Tcl_NewObj();
2767 valuePtr = listRepPtr->elements[valIndex];
2770 varIndex = varListPtr->varIndexes[j];
2771 DECACHE_STACK_INFO();
2772 value2Ptr = TclSetIndexedScalar(interp,
2773 varIndex, valuePtr, /*leaveErrorMsg*/ 1);
2775 if (value2Ptr == NULL) {
2776 TRACE_WITH_OBJ(("%u => ERROR init. index temp %d: ",
2778 Tcl_GetObjResult(interp));
2780 Tcl_DecrRefCount(valuePtr);
2792 * Push 1 if at least one value list had a remaining element
2793 * and the loop should continue. Otherwise push 0.
2796 PUSH_OBJECT(Tcl_NewLongObj(continueLoop));
2797 TRACE(("%u => %d lists, iter %d, %s loop\n",
2798 opnd, numLists, iterNum,
2799 (continueLoop? "continue" : "exit")));
2803 case INST_BEGIN_CATCH4:
2805 * Record start of the catch command with exception range index
2806 * equal to the operand. Push the current stack depth onto the
2807 * special catch stack.
2809 catchStackPtr[++catchTop] = stackTop;
2810 TRACE(("%u => catchTop=%d, stackTop=%d\n",
2811 TclGetUInt4AtPtr(pc+1), catchTop, stackTop));
2814 case INST_END_CATCH:
2817 TRACE(("=> catchTop=%d\n", catchTop));
2820 case INST_PUSH_RESULT:
2821 PUSH_OBJECT(Tcl_GetObjResult(interp));
2822 TRACE_WITH_OBJ(("=> "), Tcl_GetObjResult(interp));
2825 case INST_PUSH_RETURN_CODE:
2826 PUSH_OBJECT(Tcl_NewLongObj(result));
2827 TRACE(("=> %u\n", result));
2831 panic("TclExecuteByteCode: unrecognized opCode %u", *pc);
2832 } /* end of switch on opCode */
2835 * Division by zero in an expression. Control only reaches this
2836 * point by "goto divideByZero".
2840 Tcl_ResetResult(interp);
2841 Tcl_AppendToObj(Tcl_GetObjResult(interp), "divide by zero", -1);
2842 Tcl_SetErrorCode(interp, "ARITH", "DIVZERO", "divide by zero",
2847 * Execution has generated an "exception" such as TCL_ERROR. If the
2848 * exception is an error, record information about what was being
2849 * executed when the error occurred. Find the closest enclosing
2850 * catch range, if any. If no enclosing catch range is found, stop
2851 * execution and return the "exception" code.
2855 if ((result == TCL_ERROR) && !(iPtr->flags & ERR_ALREADY_LOGGED)) {
2856 bytes = GetSrcInfoForPc(pc, codePtr, &length);
2857 if (bytes != NULL) {
2858 Tcl_LogCommandInfo(interp, codePtr->source, bytes, length);
2859 iPtr->flags |= ERR_ALREADY_LOGGED;
2862 rangePtr = GetExceptRangeForPc(pc, /*catchOnly*/ 1, codePtr);
2863 if (rangePtr == NULL) {
2864 #ifdef TCL_COMPILE_DEBUG
2865 if (traceInstructions) {
2866 fprintf(stdout, " ... no enclosing catch, returning %s\n",
2867 StringForResultCode(result));
2870 goto abnormalReturn;
2874 * A catch exception range (rangePtr) was found to handle an
2875 * "exception". It was found either by checkForCatch just above or
2876 * by an instruction during break, continue, or error processing.
2877 * Jump to its catchOffset after unwinding the operand stack to
2878 * the depth it had when starting to execute the range's catch
2883 while (stackTop > catchStackPtr[catchTop]) {
2884 valuePtr = POP_OBJECT();
2885 TclDecrRefCount(valuePtr);
2887 #ifdef TCL_COMPILE_DEBUG
2888 if (traceInstructions) {
2889 fprintf(stdout, " ... found catch at %d, catchTop=%d, unwound to %d, new pc %u\n",
2890 rangePtr->codeOffset, catchTop, catchStackPtr[catchTop],
2891 (unsigned int)(rangePtr->catchOffset));
2894 pc = (codePtr->codeStart + rangePtr->catchOffset);
2895 continue; /* restart the execution loop at pc */
2896 } /* end of infinite loop dispatching on instructions */
2899 * Abnormal return code. Restore the stack to state it had when starting
2900 * to execute the ByteCode.
2904 while (stackTop > initStackTop) {
2905 valuePtr = POP_OBJECT();
2906 Tcl_DecrRefCount(valuePtr);
2910 * Free the catch stack array if malloc'ed storage was used.
2914 if (catchStackPtr != catchStackStorage) {
2915 ckfree((char *) catchStackPtr);
2917 eePtr->stackTop = initStackTop;
2919 #undef STATIC_CATCH_STACK_SIZE
2922 #ifdef TCL_COMPILE_DEBUG
2924 *----------------------------------------------------------------------
2926 * PrintByteCodeInfo --
2928 * This procedure prints a summary about a bytecode object to stdout.
2929 * It is called by TclExecuteByteCode when starting to execute the
2930 * bytecode object if tclTraceExec has the value 2 or more.
2938 *----------------------------------------------------------------------
2942 PrintByteCodeInfo(codePtr)
2943 register ByteCode *codePtr; /* The bytecode whose summary is printed
2946 Proc *procPtr = codePtr->procPtr;
2947 Interp *iPtr = (Interp *) *codePtr->interpHandle;
2949 fprintf(stdout, "\nExecuting ByteCode 0x%x, refCt %u, epoch %u, interp 0x%x (epoch %u)\n",
2950 (unsigned int) codePtr, codePtr->refCount,
2951 codePtr->compileEpoch, (unsigned int) iPtr,
2952 iPtr->compileEpoch);
2954 fprintf(stdout, " Source: ");
2955 TclPrintSource(stdout, codePtr->source, 60);
2957 fprintf(stdout, "\n Cmds %d, src %d, inst %u, litObjs %u, aux %d, stkDepth %u, code/src %.2f\n",
2958 codePtr->numCommands, codePtr->numSrcBytes,
2959 codePtr->numCodeBytes, codePtr->numLitObjects,
2960 codePtr->numAuxDataItems, codePtr->maxStackDepth,
2961 #ifdef TCL_COMPILE_STATS
2962 (codePtr->numSrcBytes?
2963 ((float)codePtr->structureSize)/((float)codePtr->numSrcBytes) : 0.0));
2967 #ifdef TCL_COMPILE_STATS
2968 fprintf(stdout, " Code %d = header %d+inst %d+litObj %d+exc %d+aux %d+cmdMap %d\n",
2969 codePtr->structureSize,
2970 (sizeof(ByteCode) - (sizeof(size_t) + sizeof(Tcl_Time))),
2971 codePtr->numCodeBytes,
2972 (codePtr->numLitObjects * sizeof(Tcl_Obj *)),
2973 (codePtr->numExceptRanges * sizeof(ExceptionRange)),
2974 (codePtr->numAuxDataItems * sizeof(AuxData)),
2975 codePtr->numCmdLocBytes);
2976 #endif /* TCL_COMPILE_STATS */
2977 if (procPtr != NULL) {
2979 " Proc 0x%x, refCt %d, args %d, compiled locals %d\n",
2980 (unsigned int) procPtr, procPtr->refCount,
2981 procPtr->numArgs, procPtr->numCompiledLocals);
2984 #endif /* TCL_COMPILE_DEBUG */
2987 *----------------------------------------------------------------------
2989 * ValidatePcAndStackTop --
2991 * This procedure is called by TclExecuteByteCode when debugging to
2992 * verify that the program counter and stack top are valid during
2999 * Prints a message to stderr and panics if either the pc or stack
3002 *----------------------------------------------------------------------
3005 #ifdef TCL_COMPILE_DEBUG
3007 ValidatePcAndStackTop(codePtr, pc, stackTop, stackLowerBound,
3009 register ByteCode *codePtr; /* The bytecode whose summary is printed
3011 unsigned char *pc; /* Points to first byte of a bytecode
3012 * instruction. The program counter. */
3013 int stackTop; /* Current stack top. Must be between
3014 * stackLowerBound and stackUpperBound
3016 int stackLowerBound; /* Smallest legal value for stackTop. */
3017 int stackUpperBound; /* Greatest legal value for stackTop. */
3019 unsigned int relativePc = (unsigned int) (pc - codePtr->codeStart);
3020 unsigned int codeStart = (unsigned int) codePtr->codeStart;
3021 unsigned int codeEnd = (unsigned int)
3022 (codePtr->codeStart + codePtr->numCodeBytes);
3023 unsigned char opCode = *pc;
3025 if (((unsigned int) pc < codeStart) || ((unsigned int) pc > codeEnd)) {
3026 fprintf(stderr, "\nBad instruction pc 0x%x in TclExecuteByteCode\n",
3028 panic("TclExecuteByteCode execution failure: bad pc");
3030 if ((unsigned int) opCode > LAST_INST_OPCODE) {
3031 fprintf(stderr, "\nBad opcode %d at pc %u in TclExecuteByteCode\n",
3032 (unsigned int) opCode, relativePc);
3033 panic("TclExecuteByteCode execution failure: bad opcode");
3035 if ((stackTop < stackLowerBound) || (stackTop > stackUpperBound)) {
3037 char *cmd = GetSrcInfoForPc(pc, codePtr, &numChars);
3038 char *ellipsis = "";
3040 fprintf(stderr, "\nBad stack top %d at pc %u in TclExecuteByteCode",
3041 stackTop, relativePc);
3043 if (numChars > 100) {
3047 fprintf(stderr, "\n executing %.*s%s\n", numChars, cmd,
3050 fprintf(stderr, "\n");
3052 panic("TclExecuteByteCode execution failure: bad stack top");
3055 #endif /* TCL_COMPILE_DEBUG */
3058 *----------------------------------------------------------------------
3060 * IllegalExprOperandType --
3062 * Used by TclExecuteByteCode to add an error message to errorInfo
3063 * when an illegal operand type is detected by an expression
3064 * instruction. The argument opndPtr holds the operand object in error.
3070 * An error message is appended to errorInfo.
3072 *----------------------------------------------------------------------
3076 IllegalExprOperandType(interp, pc, opndPtr)
3077 Tcl_Interp *interp; /* Interpreter to which error information
3079 unsigned char *pc; /* Points to the instruction being executed
3080 * when the illegal type was found. */
3081 Tcl_Obj *opndPtr; /* Points to the operand holding the value
3082 * with the illegal type. */
3084 unsigned char opCode = *pc;
3086 Tcl_ResetResult(interp);
3087 if ((opndPtr->bytes == NULL) || (opndPtr->length == 0)) {
3088 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
3089 "can't use empty string as operand of \"",
3090 operatorStrings[opCode - INST_LOR], "\"", (char *) NULL);
3092 char *msg = "non-numeric string";
3093 if (opndPtr->typePtr != &tclDoubleType) {
3095 * See if the operand can be interpreted as a double in order to
3096 * improve the error message.
3099 char *s = Tcl_GetString(opndPtr);
3102 if (Tcl_GetDouble((Tcl_Interp *) NULL, s, &d) == TCL_OK) {
3104 * Make sure that what appears to be a double
3105 * (ie 08) isn't really a bad octal
3107 if (TclCheckBadOctal(NULL, Tcl_GetString(opndPtr))) {
3108 msg = "invalid octal number";
3110 msg = "floating-point value";
3114 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "can't use ",
3115 msg, " as operand of \"", operatorStrings[opCode - INST_LOR],
3116 "\"", (char *) NULL);
3121 *----------------------------------------------------------------------
3123 * CallTraceProcedure --
3125 * Invokes a trace procedure registered with an interpreter. These
3126 * procedures trace command execution. Currently this trace procedure
3127 * is called with the address of the string-based Tcl_CmdProc for the
3128 * command, not the Tcl_ObjCmdProc.
3134 * Those side effects made by the trace procedure.
3136 *----------------------------------------------------------------------
3140 CallTraceProcedure(interp, tracePtr, cmdPtr, command, numChars, objc, objv)
3141 Tcl_Interp *interp; /* The current interpreter. */
3142 register Trace *tracePtr; /* Describes the trace procedure to call. */
3143 Command *cmdPtr; /* Points to command's Command struct. */
3144 char *command; /* Points to the first character of the
3145 * command's source before substitutions. */
3146 int numChars; /* The number of characters in the
3147 * command's source. */
3148 register int objc; /* Number of arguments for the command. */
3149 Tcl_Obj *objv[]; /* Pointers to Tcl_Obj of each argument. */
3151 Interp *iPtr = (Interp *) interp;
3152 register char **argv;
3158 * Get the string rep from the objv argument objects and place their
3159 * pointers in argv. First make sure argv is large enough to hold the
3160 * objc args plus 1 extra word for the zero end-of-argv word.
3163 argv = (char **) ckalloc((unsigned)(objc + 1) * sizeof(char *));
3164 for (i = 0; i < objc; i++) {
3165 argv[i] = Tcl_GetStringFromObj(objv[i], &length);
3170 * Copy the command characters into a new string.
3173 p = (char *) ckalloc((unsigned) (numChars + 1));
3174 memcpy((VOID *) p, (VOID *) command, (size_t) numChars);
3178 * Call the trace procedure then free allocated storage.
3181 (*tracePtr->proc)(tracePtr->clientData, interp, iPtr->numLevels,
3182 p, cmdPtr->proc, cmdPtr->clientData, objc, argv);
3184 ckfree((char *) argv);
3189 *----------------------------------------------------------------------
3191 * GetSrcInfoForPc --
3193 * Given a program counter value, finds the closest command in the
3194 * bytecode code unit's CmdLocation array and returns information about
3195 * that command's source: a pointer to its first byte and the number of
3199 * If a command is found that encloses the program counter value, a
3200 * pointer to the command's source is returned and the length of the
3201 * source is stored at *lengthPtr. If multiple commands resulted in
3202 * code at pc, information about the closest enclosing command is
3203 * returned. If no matching command is found, NULL is returned and
3204 * *lengthPtr is unchanged.
3209 *----------------------------------------------------------------------
3213 GetSrcInfoForPc(pc, codePtr, lengthPtr)
3214 unsigned char *pc; /* The program counter value for which to
3215 * return the closest command's source info.
3216 * This points to a bytecode instruction
3217 * in codePtr's code. */
3218 ByteCode *codePtr; /* The bytecode sequence in which to look
3219 * up the command source for the pc. */
3220 int *lengthPtr; /* If non-NULL, the location where the
3221 * length of the command's source should be
3222 * stored. If NULL, no length is stored. */
3224 register int pcOffset = (pc - codePtr->codeStart);
3225 int numCmds = codePtr->numCommands;
3226 unsigned char *codeDeltaNext, *codeLengthNext;
3227 unsigned char *srcDeltaNext, *srcLengthNext;
3228 int codeOffset, codeLen, codeEnd, srcOffset, srcLen, delta, i;
3229 int bestDist = INT_MAX; /* Distance of pc to best cmd's start pc. */
3230 int bestSrcOffset = -1; /* Initialized to avoid compiler warning. */
3231 int bestSrcLength = -1; /* Initialized to avoid compiler warning. */
3233 if ((pcOffset < 0) || (pcOffset >= codePtr->numCodeBytes)) {
3238 * Decode the code and source offset and length for each command. The
3239 * closest enclosing command is the last one whose code started before
3243 codeDeltaNext = codePtr->codeDeltaStart;
3244 codeLengthNext = codePtr->codeLengthStart;
3245 srcDeltaNext = codePtr->srcDeltaStart;
3246 srcLengthNext = codePtr->srcLengthStart;
3247 codeOffset = srcOffset = 0;
3248 for (i = 0; i < numCmds; i++) {
3249 if ((unsigned int) (*codeDeltaNext) == (unsigned int) 0xFF) {
3251 delta = TclGetInt4AtPtr(codeDeltaNext);
3254 delta = TclGetInt1AtPtr(codeDeltaNext);
3257 codeOffset += delta;
3259 if ((unsigned int) (*codeLengthNext) == (unsigned int) 0xFF) {
3261 codeLen = TclGetInt4AtPtr(codeLengthNext);
3262 codeLengthNext += 4;
3264 codeLen = TclGetInt1AtPtr(codeLengthNext);
3267 codeEnd = (codeOffset + codeLen - 1);
3269 if ((unsigned int) (*srcDeltaNext) == (unsigned int) 0xFF) {
3271 delta = TclGetInt4AtPtr(srcDeltaNext);
3274 delta = TclGetInt1AtPtr(srcDeltaNext);
3279 if ((unsigned int) (*srcLengthNext) == (unsigned int) 0xFF) {
3281 srcLen = TclGetInt4AtPtr(srcLengthNext);
3284 srcLen = TclGetInt1AtPtr(srcLengthNext);
3288 if (codeOffset > pcOffset) { /* best cmd already found */
3290 } else if (pcOffset <= codeEnd) { /* this cmd's code encloses pc */
3291 int dist = (pcOffset - codeOffset);
3292 if (dist <= bestDist) {
3294 bestSrcOffset = srcOffset;
3295 bestSrcLength = srcLen;
3300 if (bestDist == INT_MAX) {
3304 if (lengthPtr != NULL) {
3305 *lengthPtr = bestSrcLength;
3307 return (codePtr->source + bestSrcOffset);
3311 *----------------------------------------------------------------------
3313 * GetExceptRangeForPc --
3315 * Given a program counter value, return the closest enclosing
3319 * In the normal case, catchOnly is 0 (false) and this procedure
3320 * returns a pointer to the most closely enclosing ExceptionRange
3321 * structure regardless of whether it is a loop or catch exception
3322 * range. This is appropriate when processing a TCL_BREAK or
3323 * TCL_CONTINUE, which will be "handled" either by a loop exception
3324 * range or a closer catch range. If catchOnly is nonzero, this
3325 * procedure ignores loop exception ranges and returns a pointer to the
3326 * closest catch range. If no matching ExceptionRange is found that
3327 * encloses pc, a NULL is returned.
3332 *----------------------------------------------------------------------
3335 static ExceptionRange *
3336 GetExceptRangeForPc(pc, catchOnly, codePtr)
3337 unsigned char *pc; /* The program counter value for which to
3338 * search for a closest enclosing exception
3339 * range. This points to a bytecode
3340 * instruction in codePtr's code. */
3341 int catchOnly; /* If 0, consider either loop or catch
3342 * ExceptionRanges in search. If nonzero
3343 * consider only catch ranges (and ignore
3344 * any closer loop ranges). */
3345 ByteCode* codePtr; /* Points to the ByteCode in which to search
3346 * for the enclosing ExceptionRange. */
3348 ExceptionRange *rangeArrayPtr;
3349 int numRanges = codePtr->numExceptRanges;
3350 register ExceptionRange *rangePtr;
3351 int pcOffset = (pc - codePtr->codeStart);
3352 register int i, level;
3354 if (numRanges == 0) {
3357 rangeArrayPtr = codePtr->exceptArrayPtr;
3359 for (level = codePtr->maxExceptDepth; level >= 0; level--) {
3360 for (i = 0; i < numRanges; i++) {
3361 rangePtr = &(rangeArrayPtr[i]);
3362 if (rangePtr->nestingLevel == level) {
3363 int start = rangePtr->codeOffset;
3364 int end = (start + rangePtr->numCodeBytes);
3365 if ((start <= pcOffset) && (pcOffset < end)) {
3367 || (rangePtr->type == CATCH_EXCEPTION_RANGE)) {
3378 *----------------------------------------------------------------------
3382 * This procedure is called by the TRACE and TRACE_WITH_OBJ macros
3383 * used in TclExecuteByteCode when debugging. It returns the name of
3384 * the bytecode instruction at a specified instruction pc.
3387 * A character string for the instruction.
3392 *----------------------------------------------------------------------
3395 #ifdef TCL_COMPILE_DEBUG
3398 unsigned char *pc; /* Points to the instruction whose name
3399 * should be returned. */
3401 unsigned char opCode = *pc;
3403 return instructionTable[opCode].name;
3405 #endif /* TCL_COMPILE_DEBUG */
3408 *----------------------------------------------------------------------
3410 * VerifyExprObjType --
3412 * This procedure is called by the math functions to verify that
3413 * the object is either an int or double, coercing it if necessary.
3414 * If an error occurs during conversion, an error message is left
3415 * in the interpreter's result unless "interp" is NULL.
3418 * TCL_OK if it was int or double, TCL_ERROR otherwise
3421 * objPtr is ensured to be either tclIntType of tclDoubleType.
3423 *----------------------------------------------------------------------
3427 VerifyExprObjType(interp, objPtr)
3428 Tcl_Interp *interp; /* The interpreter in which to execute the
3430 Tcl_Obj *objPtr; /* Points to the object to type check. */
3432 if ((objPtr->typePtr == &tclIntType) ||
3433 (objPtr->typePtr == &tclDoubleType)) {
3436 int length, result = TCL_OK;
3437 char *s = Tcl_GetStringFromObj(objPtr, &length);
3439 if (TclLooksLikeInt(s, length)) {
3441 result = Tcl_GetLongFromObj((Tcl_Interp *) NULL, objPtr, &i);
3444 result = Tcl_GetDoubleFromObj((Tcl_Interp *) NULL, objPtr, &d);
3446 if ((result != TCL_OK) && (interp != NULL)) {
3447 Tcl_ResetResult(interp);
3448 if (TclCheckBadOctal((Tcl_Interp *) NULL, s)) {
3449 Tcl_AppendToObj(Tcl_GetObjResult(interp),
3450 "argument to math function was an invalid octal number",
3453 Tcl_AppendToObj(Tcl_GetObjResult(interp),
3454 "argument to math function didn't have numeric value",
3463 *----------------------------------------------------------------------
3467 * This page contains the procedures that implement all of the
3468 * built-in math functions for expressions.
3471 * Each procedure returns TCL_OK if it succeeds and pushes an
3472 * Tcl object holding the result. If it fails it returns TCL_ERROR
3473 * and leaves an error message in the interpreter's result.
3478 *----------------------------------------------------------------------
3482 ExprUnaryFunc(interp, eePtr, clientData)
3483 Tcl_Interp *interp; /* The interpreter in which to execute the
3485 ExecEnv *eePtr; /* Points to the environment for executing
3487 ClientData clientData; /* Contains the address of a procedure that
3488 * takes one double argument and returns a
3491 Tcl_Obj **stackPtr; /* Cached evaluation stack base pointer. */
3492 register int stackTop; /* Cached top index of evaluation stack. */
3493 register Tcl_Obj *valuePtr;
3497 double (*func) _ANSI_ARGS_((double)) =
3498 (double (*)_ANSI_ARGS_((double))) clientData;
3501 * Set stackPtr and stackTop from eePtr.
3508 * Pop the function's argument from the evaluation stack. Convert it
3509 * to a double if necessary.
3512 valuePtr = POP_OBJECT();
3514 if (VerifyExprObjType(interp, valuePtr) != TCL_OK) {
3519 if (valuePtr->typePtr == &tclIntType) {
3520 d = (double) valuePtr->internalRep.longValue;
3522 d = valuePtr->internalRep.doubleValue;
3526 dResult = (*func)(d);
3527 if ((errno != 0) || IS_NAN(dResult) || IS_INF(dResult)) {
3528 TclExprFloatError(interp, dResult);
3534 * Push a Tcl object holding the result.
3537 PUSH_OBJECT(Tcl_NewDoubleObj(dResult));
3540 * Reflect the change to stackTop back in eePtr.
3544 Tcl_DecrRefCount(valuePtr);
3545 DECACHE_STACK_INFO();
3550 ExprBinaryFunc(interp, eePtr, clientData)
3551 Tcl_Interp *interp; /* The interpreter in which to execute the
3553 ExecEnv *eePtr; /* Points to the environment for executing
3555 ClientData clientData; /* Contains the address of a procedure that
3556 * takes two double arguments and
3557 * returns a double result. */
3559 Tcl_Obj **stackPtr; /* Cached evaluation stack base pointer. */
3560 register int stackTop; /* Cached top index of evaluation stack. */
3561 register Tcl_Obj *valuePtr, *value2Ptr;
3562 double d1, d2, dResult;
3565 double (*func) _ANSI_ARGS_((double, double))
3566 = (double (*)_ANSI_ARGS_((double, double))) clientData;
3569 * Set stackPtr and stackTop from eePtr.
3576 * Pop the function's two arguments from the evaluation stack. Convert
3577 * them to doubles if necessary.
3580 value2Ptr = POP_OBJECT();
3581 valuePtr = POP_OBJECT();
3583 if ((VerifyExprObjType(interp, valuePtr) != TCL_OK) ||
3584 (VerifyExprObjType(interp, value2Ptr) != TCL_OK)) {
3589 if (valuePtr->typePtr == &tclIntType) {
3590 d1 = (double) valuePtr->internalRep.longValue;
3592 d1 = valuePtr->internalRep.doubleValue;
3595 if (value2Ptr->typePtr == &tclIntType) {
3596 d2 = (double) value2Ptr->internalRep.longValue;
3598 d2 = value2Ptr->internalRep.doubleValue;
3602 dResult = (*func)(d1, d2);
3603 if ((errno != 0) || IS_NAN(dResult) || IS_INF(dResult)) {
3604 TclExprFloatError(interp, dResult);
3610 * Push a Tcl object holding the result.
3613 PUSH_OBJECT(Tcl_NewDoubleObj(dResult));
3616 * Reflect the change to stackTop back in eePtr.
3620 Tcl_DecrRefCount(valuePtr);
3621 Tcl_DecrRefCount(value2Ptr);
3622 DECACHE_STACK_INFO();
3627 ExprAbsFunc(interp, eePtr, clientData)
3628 Tcl_Interp *interp; /* The interpreter in which to execute the
3630 ExecEnv *eePtr; /* Points to the environment for executing
3632 ClientData clientData; /* Ignored. */
3634 Tcl_Obj **stackPtr; /* Cached evaluation stack base pointer. */
3635 register int stackTop; /* Cached top index of evaluation stack. */
3636 register Tcl_Obj *valuePtr;
3642 * Set stackPtr and stackTop from eePtr.
3649 * Pop the argument from the evaluation stack.
3652 valuePtr = POP_OBJECT();
3654 if (VerifyExprObjType(interp, valuePtr) != TCL_OK) {
3660 * Push a Tcl object with the result.
3662 if (valuePtr->typePtr == &tclIntType) {
3663 i = valuePtr->internalRep.longValue;
3667 Tcl_ResetResult(interp);
3668 Tcl_AppendToObj(Tcl_GetObjResult(interp),
3669 "integer value too large to represent", -1);
3670 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
3671 "integer value too large to represent", (char *) NULL);
3678 PUSH_OBJECT(Tcl_NewLongObj(iResult));
3680 d = valuePtr->internalRep.doubleValue;
3686 if (IS_NAN(dResult) || IS_INF(dResult)) {
3687 TclExprFloatError(interp, dResult);
3691 PUSH_OBJECT(Tcl_NewDoubleObj(dResult));
3695 * Reflect the change to stackTop back in eePtr.
3699 Tcl_DecrRefCount(valuePtr);
3700 DECACHE_STACK_INFO();
3705 ExprDoubleFunc(interp, eePtr, clientData)
3706 Tcl_Interp *interp; /* The interpreter in which to execute the
3708 ExecEnv *eePtr; /* Points to the environment for executing
3710 ClientData clientData; /* Ignored. */
3712 Tcl_Obj **stackPtr; /* Cached evaluation stack base pointer. */
3713 register int stackTop; /* Cached top index of evaluation stack. */
3714 register Tcl_Obj *valuePtr;
3719 * Set stackPtr and stackTop from eePtr.
3726 * Pop the argument from the evaluation stack.
3729 valuePtr = POP_OBJECT();
3731 if (VerifyExprObjType(interp, valuePtr) != TCL_OK) {
3736 if (valuePtr->typePtr == &tclIntType) {
3737 dResult = (double) valuePtr->internalRep.longValue;
3739 dResult = valuePtr->internalRep.doubleValue;
3743 * Push a Tcl object with the result.
3746 PUSH_OBJECT(Tcl_NewDoubleObj(dResult));
3749 * Reflect the change to stackTop back in eePtr.
3753 Tcl_DecrRefCount(valuePtr);
3754 DECACHE_STACK_INFO();
3759 ExprIntFunc(interp, eePtr, clientData)
3760 Tcl_Interp *interp; /* The interpreter in which to execute the
3762 ExecEnv *eePtr; /* Points to the environment for executing
3764 ClientData clientData; /* Ignored. */
3766 Tcl_Obj **stackPtr; /* Cached evaluation stack base pointer. */
3767 register int stackTop; /* Cached top index of evaluation stack. */
3768 register Tcl_Obj *valuePtr;
3774 * Set stackPtr and stackTop from eePtr.
3781 * Pop the argument from the evaluation stack.
3784 valuePtr = POP_OBJECT();
3786 if (VerifyExprObjType(interp, valuePtr) != TCL_OK) {
3791 if (valuePtr->typePtr == &tclIntType) {
3792 iResult = valuePtr->internalRep.longValue;
3794 d = valuePtr->internalRep.doubleValue;
3796 if (d < (double) (long) LONG_MIN) {
3798 Tcl_ResetResult(interp);
3799 Tcl_AppendToObj(Tcl_GetObjResult(interp),
3800 "integer value too large to represent", -1);
3801 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
3802 "integer value too large to represent", (char *) NULL);
3807 if (d > (double) LONG_MAX) {
3811 if (IS_NAN(d) || IS_INF(d)) {
3812 TclExprFloatError(interp, d);
3820 * Push a Tcl object with the result.
3823 PUSH_OBJECT(Tcl_NewLongObj(iResult));
3826 * Reflect the change to stackTop back in eePtr.
3830 Tcl_DecrRefCount(valuePtr);
3831 DECACHE_STACK_INFO();
3836 ExprRandFunc(interp, eePtr, clientData)
3837 Tcl_Interp *interp; /* The interpreter in which to execute the
3839 ExecEnv *eePtr; /* Points to the environment for executing
3841 ClientData clientData; /* Ignored. */
3843 Tcl_Obj **stackPtr; /* Cached evaluation stack base pointer. */
3844 register int stackTop; /* Cached top index of evaluation stack. */
3845 Interp *iPtr = (Interp *) interp;
3849 if (!(iPtr->flags & RAND_SEED_INITIALIZED)) {
3850 iPtr->flags |= RAND_SEED_INITIALIZED;
3851 iPtr->randSeed = TclpGetClicks();
3855 * Set stackPtr and stackTop from eePtr.
3861 * Generate the random number using the linear congruential
3862 * generator defined by the following recurrence:
3863 * seed = ( IA * seed ) mod IM
3864 * where IA is 16807 and IM is (2^31) - 1. In order to avoid
3865 * potential problems with integer overflow, the code uses
3866 * additional constants IQ and IR such that
3868 * For details on how this algorithm works, refer to the following
3871 * S.K. Park & K.W. Miller, "Random number generators: good ones
3872 * are hard to find," Comm ACM 31(10):1192-1201, Oct 1988
3874 * W.H. Press & S.A. Teukolsky, "Portable random number
3875 * generators," Computers in Physics 6(5):522-524, Sep/Oct 1992.
3878 #define RAND_IA 16807
3879 #define RAND_IM 2147483647
3880 #define RAND_IQ 127773
3881 #define RAND_IR 2836
3882 #define RAND_MASK 123459876
3884 if (iPtr->randSeed == 0) {
3886 * Don't allow a 0 seed, since it breaks the generator. Shift
3887 * it to some other value.
3890 iPtr->randSeed = 123459876;
3892 tmp = iPtr->randSeed/RAND_IQ;
3893 iPtr->randSeed = RAND_IA*(iPtr->randSeed - tmp*RAND_IQ) - RAND_IR*tmp;
3894 if (iPtr->randSeed < 0) {
3895 iPtr->randSeed += RAND_IM;
3899 * On 64-bit architectures we need to mask off the upper bits to
3900 * ensure we only have a 32-bit range. The constant has the
3901 * bizarre form below in order to make sure that it doesn't
3902 * get sign-extended (the rules for sign extension are very
3903 * concat, particularly on 64-bit machines).
3906 iPtr->randSeed &= ((((unsigned long) 0xfffffff) << 4) | 0xf);
3907 dResult = iPtr->randSeed * (1.0/RAND_IM);
3910 * Push a Tcl object with the result.
3913 PUSH_OBJECT(Tcl_NewDoubleObj(dResult));
3916 * Reflect the change to stackTop back in eePtr.
3919 DECACHE_STACK_INFO();
3924 ExprRoundFunc(interp, eePtr, clientData)
3925 Tcl_Interp *interp; /* The interpreter in which to execute the
3927 ExecEnv *eePtr; /* Points to the environment for executing
3929 ClientData clientData; /* Ignored. */
3931 Tcl_Obj **stackPtr; /* Cached evaluation stack base pointer. */
3932 register int stackTop; /* Cached top index of evaluation stack. */
3939 * Set stackPtr and stackTop from eePtr.
3946 * Pop the argument from the evaluation stack.
3949 valuePtr = POP_OBJECT();
3951 if (VerifyExprObjType(interp, valuePtr) != TCL_OK) {
3956 if (valuePtr->typePtr == &tclIntType) {
3957 iResult = valuePtr->internalRep.longValue;
3959 d = valuePtr->internalRep.doubleValue;
3961 if (d <= (((double) (long) LONG_MIN) - 0.5)) {
3963 Tcl_ResetResult(interp);
3964 Tcl_AppendToObj(Tcl_GetObjResult(interp),
3965 "integer value too large to represent", -1);
3966 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
3967 "integer value too large to represent",
3972 temp = (long) (d - 0.5);
3974 if (d >= (((double) LONG_MAX + 0.5))) {
3977 temp = (long) (d + 0.5);
3979 if (IS_NAN(temp) || IS_INF(temp)) {
3980 TclExprFloatError(interp, temp);
3984 iResult = (long) temp;
3988 * Push a Tcl object with the result.
3991 PUSH_OBJECT(Tcl_NewLongObj(iResult));
3994 * Reflect the change to stackTop back in eePtr.
3998 Tcl_DecrRefCount(valuePtr);
3999 DECACHE_STACK_INFO();
4004 ExprSrandFunc(interp, eePtr, clientData)
4005 Tcl_Interp *interp; /* The interpreter in which to execute the
4007 ExecEnv *eePtr; /* Points to the environment for executing
4009 ClientData clientData; /* Ignored. */
4011 Tcl_Obj **stackPtr; /* Cached evaluation stack base pointer. */
4012 register int stackTop; /* Cached top index of evaluation stack. */
4013 Interp *iPtr = (Interp *) interp;
4015 long i = 0; /* Initialized to avoid compiler warning. */
4019 * Set stackPtr and stackTop from eePtr.
4025 * Pop the argument from the evaluation stack. Use the value
4026 * to reset the random number seed.
4029 valuePtr = POP_OBJECT();
4031 if (VerifyExprObjType(interp, valuePtr) != TCL_OK) {
4036 if (valuePtr->typePtr == &tclIntType) {
4037 i = valuePtr->internalRep.longValue;
4040 * At this point, the only other possible type is double
4042 Tcl_ResetResult(interp);
4043 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
4044 "can't use floating-point value as argument to srand",
4047 Tcl_DecrRefCount(valuePtr);
4048 DECACHE_STACK_INFO();
4056 iPtr->flags |= RAND_SEED_INITIALIZED;
4060 * To avoid duplicating the random number generation code we simply
4061 * clean up our state and call the real random number function. That
4062 * function will always succeed.
4065 Tcl_DecrRefCount(valuePtr);
4066 DECACHE_STACK_INFO();
4068 ExprRandFunc(interp, eePtr, clientData);
4073 *----------------------------------------------------------------------
4075 * ExprCallMathFunc --
4077 * This procedure is invoked to call a non-builtin math function
4078 * during the execution of an expression.
4081 * TCL_OK is returned if all went well and the function's value
4082 * was computed successfully. If an error occurred, TCL_ERROR
4083 * is returned and an error message is left in the interpreter's
4084 * result. After a successful return this procedure pushes a Tcl object
4085 * holding the result.
4088 * None, unless the called math function has side effects.
4090 *----------------------------------------------------------------------
4094 ExprCallMathFunc(interp, eePtr, objc, objv)
4095 Tcl_Interp *interp; /* The interpreter in which to execute the
4097 ExecEnv *eePtr; /* Points to the environment for executing
4099 int objc; /* Number of arguments. The function name is
4100 * the 0-th argument. */
4101 Tcl_Obj **objv; /* The array of arguments. The function name
4104 Interp *iPtr = (Interp *) interp;
4105 Tcl_Obj **stackPtr; /* Cached evaluation stack base pointer. */
4106 register int stackTop; /* Cached top index of evaluation stack. */
4108 Tcl_HashEntry *hPtr;
4109 MathFunc *mathFuncPtr; /* Information about math function. */
4110 Tcl_Value args[MAX_MATH_ARGS]; /* Arguments for function call. */
4111 Tcl_Value funcResult; /* Result of function call as Tcl_Value. */
4112 register Tcl_Obj *valuePtr;
4116 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
4118 Tcl_ResetResult(interp);
4121 * Set stackPtr and stackTop from eePtr.
4127 * Look up the MathFunc record for the function.
4130 funcName = Tcl_GetString(objv[0]);
4131 hPtr = Tcl_FindHashEntry(&iPtr->mathFuncTable, funcName);
4133 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
4134 "unknown math function \"", funcName, "\"", (char *) NULL);
4138 mathFuncPtr = (MathFunc *) Tcl_GetHashValue(hPtr);
4139 if (mathFuncPtr->numArgs != (objc-1)) {
4140 panic("ExprCallMathFunc: expected number of args %d != actual number %d",
4141 mathFuncPtr->numArgs, objc);
4147 * Collect the arguments for the function, if there are any, into the
4148 * array "args". Note that args[0] will have the Tcl_Value that
4149 * corresponds to objv[1].
4152 for (j = 1, k = 0; j < objc; j++, k++) {
4155 if (VerifyExprObjType(interp, valuePtr) != TCL_OK) {
4161 * Copy the object's numeric value to the argument record,
4162 * converting it if necessary.
4165 if (valuePtr->typePtr == &tclIntType) {
4166 i = valuePtr->internalRep.longValue;
4167 if (mathFuncPtr->argTypes[k] == TCL_DOUBLE) {
4168 args[k].type = TCL_DOUBLE;
4169 args[k].doubleValue = i;
4171 args[k].type = TCL_INT;
4172 args[k].intValue = i;
4175 d = valuePtr->internalRep.doubleValue;
4176 if (mathFuncPtr->argTypes[k] == TCL_INT) {
4177 args[k].type = TCL_INT;
4178 args[k].intValue = (long) d;
4180 args[k].type = TCL_DOUBLE;
4181 args[k].doubleValue = d;
4187 * Invoke the function and copy its result back into valuePtr.
4190 tsdPtr->mathInProgress++;
4191 result = (*mathFuncPtr->proc)(mathFuncPtr->clientData, interp, args,
4193 tsdPtr->mathInProgress--;
4194 if (result != TCL_OK) {
4199 * Pop the objc top stack elements and decrement their ref counts.
4202 i = (stackTop - (objc-1));
4203 while (i <= stackTop) {
4204 valuePtr = stackPtr[i];
4205 Tcl_DecrRefCount(valuePtr);
4211 * Push the call's object result.
4214 if (funcResult.type == TCL_INT) {
4215 PUSH_OBJECT(Tcl_NewLongObj(funcResult.intValue));
4217 d = funcResult.doubleValue;
4218 if (IS_NAN(d) || IS_INF(d)) {
4219 TclExprFloatError(interp, d);
4223 PUSH_OBJECT(Tcl_NewDoubleObj(d));
4227 * Reflect the change to stackTop back in eePtr.
4231 DECACHE_STACK_INFO();
4236 *----------------------------------------------------------------------
4238 * TclExprFloatError --
4240 * This procedure is called when an error occurs during a
4241 * floating-point operation. It reads errno and sets
4242 * interp->objResultPtr accordingly.
4245 * interp->objResultPtr is set to hold an error message.
4250 *----------------------------------------------------------------------
4254 TclExprFloatError(interp, value)
4255 Tcl_Interp *interp; /* Where to store error message. */
4256 double value; /* Value returned after error; used to
4257 * distinguish underflows from overflows. */
4261 Tcl_ResetResult(interp);
4262 if ((errno == EDOM) || (value != value)) {
4263 s = "domain error: argument not in valid range";
4264 Tcl_AppendToObj(Tcl_GetObjResult(interp), s, -1);
4265 Tcl_SetErrorCode(interp, "ARITH", "DOMAIN", s, (char *) NULL);
4266 } else if ((errno == ERANGE) || IS_INF(value)) {
4268 s = "floating-point value too small to represent";
4269 Tcl_AppendToObj(Tcl_GetObjResult(interp), s, -1);
4270 Tcl_SetErrorCode(interp, "ARITH", "UNDERFLOW", s, (char *) NULL);
4272 s = "floating-point value too large to represent";
4273 Tcl_AppendToObj(Tcl_GetObjResult(interp), s, -1);
4274 Tcl_SetErrorCode(interp, "ARITH", "OVERFLOW", s, (char *) NULL);
4277 char msg[64 + TCL_INTEGER_SPACE];
4279 sprintf(msg, "unknown floating-point error, errno = %d", errno);
4280 Tcl_AppendToObj(Tcl_GetObjResult(interp), msg, -1);
4281 Tcl_SetErrorCode(interp, "ARITH", "UNKNOWN", msg, (char *) NULL);
4286 *----------------------------------------------------------------------
4288 * TclMathInProgress --
4290 * This procedure is called to find out if Tcl is doing math
4299 *----------------------------------------------------------------------
4305 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
4306 return tsdPtr->mathInProgress;
4309 #ifdef TCL_COMPILE_STATS
4311 *----------------------------------------------------------------------
4315 * Procedure used while collecting compilation statistics to determine
4316 * the log base 2 of an integer.
4319 * Returns the log base 2 of the operand. If the argument is less
4320 * than or equal to zero, a zero is returned.
4325 *----------------------------------------------------------------------
4330 register int value; /* The integer for which to compute the
4333 register int n = value;
4334 register int result = 0;
4344 *----------------------------------------------------------------------
4348 * Implements the "evalstats" command that prints instruction execution
4352 * Standard Tcl results.
4357 *----------------------------------------------------------------------
4361 EvalStatsCmd(unused, interp, argc, argv)
4362 ClientData unused; /* Unused. */
4363 Tcl_Interp *interp; /* The current interpreter. */
4364 int argc; /* The number of arguments. */
4365 char **argv; /* The argument strings. */
4367 Interp *iPtr = (Interp *) interp;
4368 LiteralTable *globalTablePtr = &(iPtr->literalTable);
4369 ByteCodeStats *statsPtr = &(iPtr->stats);
4370 double totalCodeBytes, currentCodeBytes;
4371 double totalLiteralBytes, currentLiteralBytes;
4372 double objBytesIfUnshared, strBytesIfUnshared, sharingBytesSaved;
4373 double strBytesSharedMultX, strBytesSharedOnce;
4374 double numInstructions, currentHeaderBytes;
4375 long numCurrentByteCodes, numByteCodeLits;
4376 long refCountSum, literalMgmtBytes, sum;
4377 int numSharedMultX, numSharedOnce;
4378 int decadeHigh, minSizeDecade, maxSizeDecade, length, i;
4379 char *litTableStats;
4380 LiteralEntry *entryPtr;
4382 numInstructions = 0.0;
4383 for (i = 0; i < 256; i++) {
4384 if (statsPtr->instructionCount[i] != 0) {
4385 numInstructions += statsPtr->instructionCount[i];
4389 totalLiteralBytes = sizeof(LiteralTable)
4390 + iPtr->literalTable.numBuckets * sizeof(LiteralEntry *)
4391 + (statsPtr->numLiteralsCreated * sizeof(LiteralEntry))
4392 + (statsPtr->numLiteralsCreated * sizeof(Tcl_Obj))
4393 + statsPtr->totalLitStringBytes;
4394 totalCodeBytes = statsPtr->totalByteCodeBytes + totalLiteralBytes;
4396 numCurrentByteCodes =
4397 statsPtr->numCompilations - statsPtr->numByteCodesFreed;
4398 currentHeaderBytes = numCurrentByteCodes
4399 * (sizeof(ByteCode) - (sizeof(size_t) + sizeof(Tcl_Time)));
4400 literalMgmtBytes = sizeof(LiteralTable)
4401 + (iPtr->literalTable.numBuckets * sizeof(LiteralEntry *))
4402 + (iPtr->literalTable.numEntries * sizeof(LiteralEntry));
4403 currentLiteralBytes = literalMgmtBytes
4404 + iPtr->literalTable.numEntries * sizeof(Tcl_Obj)
4405 + statsPtr->currentLitStringBytes;
4406 currentCodeBytes = statsPtr->currentByteCodeBytes + currentLiteralBytes;
4409 * Summary statistics, total and current source and ByteCode sizes.
4412 fprintf(stdout, "\n----------------------------------------------------------------\n");
4414 "Compilation and execution statistics for interpreter 0x%x\n",
4415 (unsigned int) iPtr);
4417 fprintf(stdout, "\nNumber ByteCodes executed %ld\n",
4418 statsPtr->numExecutions);
4419 fprintf(stdout, "Number ByteCodes compiled %ld\n",
4420 statsPtr->numCompilations);
4421 fprintf(stdout, " Mean executions/compile %.1f\n",
4422 ((float)statsPtr->numExecutions) / ((float)statsPtr->numCompilations));
4424 fprintf(stdout, "\nInstructions executed %.0f\n",
4426 fprintf(stdout, " Mean inst/compile %.0f\n",
4427 numInstructions / statsPtr->numCompilations);
4428 fprintf(stdout, " Mean inst/execution %.0f\n",
4429 numInstructions / statsPtr->numExecutions);
4431 fprintf(stdout, "\nTotal ByteCodes %ld\n",
4432 statsPtr->numCompilations);
4433 fprintf(stdout, " Source bytes %.6g\n",
4434 statsPtr->totalSrcBytes);
4435 fprintf(stdout, " Code bytes %.6g\n",
4437 fprintf(stdout, " ByteCode bytes %.6g\n",
4438 statsPtr->totalByteCodeBytes);
4439 fprintf(stdout, " Literal bytes %.6g\n",
4441 fprintf(stdout, " table %d + bkts %d + entries %ld + objects %ld + strings %.6g\n",
4442 sizeof(LiteralTable),
4443 iPtr->literalTable.numBuckets * sizeof(LiteralEntry *),
4444 statsPtr->numLiteralsCreated * sizeof(LiteralEntry),
4445 statsPtr->numLiteralsCreated * sizeof(Tcl_Obj),
4446 statsPtr->totalLitStringBytes);
4447 fprintf(stdout, " Mean code/compile %.1f\n",
4448 totalCodeBytes / statsPtr->numCompilations);
4449 fprintf(stdout, " Mean code/source %.1f\n",
4450 totalCodeBytes / statsPtr->totalSrcBytes);
4452 fprintf(stdout, "\nCurrent ByteCodes %ld\n",
4453 numCurrentByteCodes);
4454 fprintf(stdout, " Source bytes %.6g\n",
4455 statsPtr->currentSrcBytes);
4456 fprintf(stdout, " Code bytes %.6g\n",
4458 fprintf(stdout, " ByteCode bytes %.6g\n",
4459 statsPtr->currentByteCodeBytes);
4460 fprintf(stdout, " Literal bytes %.6g\n",
4461 currentLiteralBytes);
4462 fprintf(stdout, " table %d + bkts %d + entries %d + objects %d + strings %.6g\n",
4463 sizeof(LiteralTable),
4464 iPtr->literalTable.numBuckets * sizeof(LiteralEntry *),
4465 iPtr->literalTable.numEntries * sizeof(LiteralEntry),
4466 iPtr->literalTable.numEntries * sizeof(Tcl_Obj),
4467 statsPtr->currentLitStringBytes);
4468 fprintf(stdout, " Mean code/source %.1f\n",
4469 currentCodeBytes / statsPtr->currentSrcBytes);
4470 fprintf(stdout, " Code + source bytes %.6g (%0.1f mean code/src)\n",
4471 (currentCodeBytes + statsPtr->currentSrcBytes),
4472 (currentCodeBytes / statsPtr->currentSrcBytes) + 1.0);
4475 * Literal table statistics.
4478 numByteCodeLits = 0;
4482 objBytesIfUnshared = 0.0;
4483 strBytesIfUnshared = 0.0;
4484 strBytesSharedMultX = 0.0;
4485 strBytesSharedOnce = 0.0;
4486 for (i = 0; i < globalTablePtr->numBuckets; i++) {
4487 for (entryPtr = globalTablePtr->buckets[i]; entryPtr != NULL;
4488 entryPtr = entryPtr->nextPtr) {
4489 if (entryPtr->objPtr->typePtr == &tclByteCodeType) {
4492 (void) Tcl_GetStringFromObj(entryPtr->objPtr, &length);
4493 refCountSum += entryPtr->refCount;
4494 objBytesIfUnshared += (entryPtr->refCount * sizeof(Tcl_Obj));
4495 strBytesIfUnshared += (entryPtr->refCount * (length+1));
4496 if (entryPtr->refCount > 1) {
4498 strBytesSharedMultX += (length+1);
4501 strBytesSharedOnce += (length+1);
4505 sharingBytesSaved = (objBytesIfUnshared + strBytesIfUnshared)
4506 - currentLiteralBytes;
4508 fprintf(stdout, "\nTotal objects (all interps) %ld\n",
4510 fprintf(stdout, "Current objects %ld\n",
4511 (tclObjsAlloced - tclObjsFreed));
4512 fprintf(stdout, "Total literal objects %ld\n",
4513 statsPtr->numLiteralsCreated);
4515 fprintf(stdout, "\nCurrent literal objects %d (%0.1f%% of current objects)\n",
4516 globalTablePtr->numEntries,
4517 (globalTablePtr->numEntries * 100.0) / (tclObjsAlloced-tclObjsFreed));
4518 fprintf(stdout, " ByteCode literals %ld (%0.1f%% of current literals)\n",
4520 (numByteCodeLits * 100.0) / globalTablePtr->numEntries);
4521 fprintf(stdout, " Literals reused > 1x %d\n",
4523 fprintf(stdout, " Mean reference count %.2f\n",
4524 ((double) refCountSum) / globalTablePtr->numEntries);
4525 fprintf(stdout, " Mean len, str reused >1x %.2f\n",
4526 (numSharedMultX? (strBytesSharedMultX/numSharedMultX) : 0.0));
4527 fprintf(stdout, " Mean len, str used 1x %.2f\n",
4528 (numSharedOnce? (strBytesSharedOnce/numSharedOnce) : 0.0));
4529 fprintf(stdout, " Total sharing savings %.6g (%0.1f%% of bytes if no sharing)\n",
4531 (sharingBytesSaved * 100.0) / (objBytesIfUnshared + strBytesIfUnshared));
4532 fprintf(stdout, " Bytes with sharing %.6g\n",
4533 currentLiteralBytes);
4534 fprintf(stdout, " table %d + bkts %d + entries %d + objects %d + strings %.6g\n",
4535 sizeof(LiteralTable),
4536 iPtr->literalTable.numBuckets * sizeof(LiteralEntry *),
4537 iPtr->literalTable.numEntries * sizeof(LiteralEntry),
4538 iPtr->literalTable.numEntries * sizeof(Tcl_Obj),
4539 statsPtr->currentLitStringBytes);
4540 fprintf(stdout, " Bytes if no sharing %.6g = objects %.6g + strings %.6g\n",
4541 (objBytesIfUnshared + strBytesIfUnshared),
4542 objBytesIfUnshared, strBytesIfUnshared);
4543 fprintf(stdout, " String sharing savings %.6g = unshared %.6g - shared %.6g\n",
4544 (strBytesIfUnshared - statsPtr->currentLitStringBytes),
4545 strBytesIfUnshared, statsPtr->currentLitStringBytes);
4546 fprintf(stdout, " Literal mgmt overhead %ld (%0.1f%% of bytes with sharing)\n",
4548 (literalMgmtBytes * 100.0) / currentLiteralBytes);
4549 fprintf(stdout, " table %d + buckets %d + entries %d\n",
4550 sizeof(LiteralTable),
4551 iPtr->literalTable.numBuckets * sizeof(LiteralEntry *),
4552 iPtr->literalTable.numEntries * sizeof(LiteralEntry));
4555 * Breakdown of current ByteCode space requirements.
4558 fprintf(stdout, "\nBreakdown of current ByteCode requirements:\n");
4559 fprintf(stdout, " Bytes Pct of Avg per\n");
4560 fprintf(stdout, " total ByteCode\n");
4561 fprintf(stdout, "Total %12.6g 100.00%% %8.1f\n",
4562 statsPtr->currentByteCodeBytes,
4563 statsPtr->currentByteCodeBytes / numCurrentByteCodes);
4564 fprintf(stdout, "Header %12.6g %8.1f%% %8.1f\n",
4566 ((currentHeaderBytes * 100.0) / statsPtr->currentByteCodeBytes),
4567 currentHeaderBytes / numCurrentByteCodes);
4568 fprintf(stdout, "Instructions %12.6g %8.1f%% %8.1f\n",
4569 statsPtr->currentInstBytes,
4570 ((statsPtr->currentInstBytes * 100.0) / statsPtr->currentByteCodeBytes),
4571 statsPtr->currentInstBytes / numCurrentByteCodes);
4572 fprintf(stdout, "Literal ptr array %12.6g %8.1f%% %8.1f\n",
4573 statsPtr->currentLitBytes,
4574 ((statsPtr->currentLitBytes * 100.0) / statsPtr->currentByteCodeBytes),
4575 statsPtr->currentLitBytes / numCurrentByteCodes);
4576 fprintf(stdout, "Exception table %12.6g %8.1f%% %8.1f\n",
4577 statsPtr->currentExceptBytes,
4578 ((statsPtr->currentExceptBytes * 100.0) / statsPtr->currentByteCodeBytes),
4579 statsPtr->currentExceptBytes / numCurrentByteCodes);
4580 fprintf(stdout, "Auxiliary data %12.6g %8.1f%% %8.1f\n",
4581 statsPtr->currentAuxBytes,
4582 ((statsPtr->currentAuxBytes * 100.0) / statsPtr->currentByteCodeBytes),
4583 statsPtr->currentAuxBytes / numCurrentByteCodes);
4584 fprintf(stdout, "Command map %12.6g %8.1f%% %8.1f\n",
4585 statsPtr->currentCmdMapBytes,
4586 ((statsPtr->currentCmdMapBytes * 100.0) / statsPtr->currentByteCodeBytes),
4587 statsPtr->currentCmdMapBytes / numCurrentByteCodes);
4590 * Detailed literal statistics.
4593 fprintf(stdout, "\nLiteral string sizes:\n");
4594 fprintf(stdout, " Up to length Percentage\n");
4596 for (i = 31; i >= 0; i--) {
4597 if (statsPtr->literalCount[i] > 0) {
4603 for (i = 0; i <= maxSizeDecade; i++) {
4604 decadeHigh = (1 << (i+1)) - 1;
4605 sum += statsPtr->literalCount[i];
4606 fprintf(stdout, " %10d %8.0f%%\n",
4607 decadeHigh, (sum * 100.0) / statsPtr->numLiteralsCreated);
4610 litTableStats = TclLiteralStats(globalTablePtr);
4611 fprintf(stdout, "\nCurrent literal table statistics:\n%s\n",
4613 ckfree((char *) litTableStats);
4616 * Source and ByteCode size distributions.
4619 fprintf(stdout, "\nSource sizes:\n");
4620 fprintf(stdout, " Up to size Percentage\n");
4621 minSizeDecade = maxSizeDecade = 0;
4622 for (i = 0; i < 31; i++) {
4623 if (statsPtr->srcCount[i] > 0) {
4628 for (i = 31; i >= 0; i--) {
4629 if (statsPtr->srcCount[i] > 0) {
4635 for (i = minSizeDecade; i <= maxSizeDecade; i++) {
4636 decadeHigh = (1 << (i+1)) - 1;
4637 sum += statsPtr->srcCount[i];
4638 fprintf(stdout, " %10d %8.0f%%\n",
4639 decadeHigh, (sum * 100.0) / statsPtr->numCompilations);
4642 fprintf(stdout, "\nByteCode sizes:\n");
4643 fprintf(stdout, " Up to size Percentage\n");
4644 minSizeDecade = maxSizeDecade = 0;
4645 for (i = 0; i < 31; i++) {
4646 if (statsPtr->byteCodeCount[i] > 0) {
4651 for (i = 31; i >= 0; i--) {
4652 if (statsPtr->byteCodeCount[i] > 0) {
4658 for (i = minSizeDecade; i <= maxSizeDecade; i++) {
4659 decadeHigh = (1 << (i+1)) - 1;
4660 sum += statsPtr->byteCodeCount[i];
4661 fprintf(stdout, " %10d %8.0f%%\n",
4662 decadeHigh, (sum * 100.0) / statsPtr->numCompilations);
4665 fprintf(stdout, "\nByteCode longevity (excludes current ByteCodes):\n");
4666 fprintf(stdout, " Up to ms Percentage\n");
4667 minSizeDecade = maxSizeDecade = 0;
4668 for (i = 0; i < 31; i++) {
4669 if (statsPtr->lifetimeCount[i] > 0) {
4674 for (i = 31; i >= 0; i--) {
4675 if (statsPtr->lifetimeCount[i] > 0) {
4681 for (i = minSizeDecade; i <= maxSizeDecade; i++) {
4682 decadeHigh = (1 << (i+1)) - 1;
4683 sum += statsPtr->lifetimeCount[i];
4684 fprintf(stdout, " %12.3f %8.0f%%\n",
4685 decadeHigh / 1000.0,
4686 (sum * 100.0) / statsPtr->numByteCodesFreed);
4690 * Instruction counts.
4693 fprintf(stdout, "\nInstruction counts:\n");
4694 for (i = 0; i <= LAST_INST_OPCODE; i++) {
4695 if (statsPtr->instructionCount[i]) {
4696 fprintf(stdout, "%20s %8ld %6.1f%%\n",
4697 instructionTable[i].name,
4698 statsPtr->instructionCount[i],
4699 (statsPtr->instructionCount[i]*100.0) / numInstructions);
4703 fprintf(stdout, "\nInstructions NEVER executed:\n");
4704 for (i = 0; i <= LAST_INST_OPCODE; i++) {
4705 if (statsPtr->instructionCount[i] == 0) {
4706 fprintf(stdout, "%20s\n",
4707 instructionTable[i].name);
4711 #ifdef TCL_MEM_DEBUG
4712 fprintf(stdout, "\nHeap Statistics:\n");
4713 TclDumpMemoryInfo(stdout);
4715 fprintf(stdout, "\n----------------------------------------------------------------\n");
4718 #endif /* TCL_COMPILE_STATS */
4721 *----------------------------------------------------------------------
4723 * Tcl_GetCommandFromObj --
4725 * Returns the command specified by the name in a Tcl_Obj.
4728 * Returns a token for the command if it is found. Otherwise, if it
4729 * can't be found or there is an error, returns NULL.
4732 * May update the internal representation for the object, caching
4733 * the command reference so that the next time this procedure is
4734 * called with the same object, the command can be found quickly.
4736 *----------------------------------------------------------------------
4740 Tcl_GetCommandFromObj(interp, objPtr)
4741 Tcl_Interp *interp; /* The interpreter in which to resolve the
4742 * command and to report errors. */
4743 register Tcl_Obj *objPtr; /* The object containing the command's
4744 * name. If the name starts with "::", will
4745 * be looked up in global namespace. Else,
4746 * looked up first in the current namespace
4747 * if contextNsPtr is NULL, then in global
4750 Interp *iPtr = (Interp *) interp;
4751 register ResolvedCmdName *resPtr;
4752 register Command *cmdPtr;
4753 Namespace *currNsPtr;
4757 * Get the internal representation, converting to a command type if
4758 * needed. The internal representation is a ResolvedCmdName that points
4759 * to the actual command.
4762 if (objPtr->typePtr != &tclCmdNameType) {
4763 result = tclCmdNameType.setFromAnyProc(interp, objPtr);
4764 if (result != TCL_OK) {
4765 return (Tcl_Command) NULL;
4768 resPtr = (ResolvedCmdName *) objPtr->internalRep.otherValuePtr;
4771 * Get the current namespace.
4774 if (iPtr->varFramePtr != NULL) {
4775 currNsPtr = iPtr->varFramePtr->nsPtr;
4777 currNsPtr = iPtr->globalNsPtr;
4781 * Check the context namespace and the namespace epoch of the resolved
4782 * symbol to make sure that it is fresh. If not, then force another
4783 * conversion to the command type, to discard the old rep and create a
4784 * new one. Note that we verify that the namespace id of the context
4785 * namespace is the same as the one we cached; this insures that the
4786 * namespace wasn't deleted and a new one created at the same address
4787 * with the same command epoch.
4791 if ((resPtr != NULL)
4792 && (resPtr->refNsPtr == currNsPtr)
4793 && (resPtr->refNsId == currNsPtr->nsId)
4794 && (resPtr->refNsCmdEpoch == currNsPtr->cmdRefEpoch)) {
4795 cmdPtr = resPtr->cmdPtr;
4796 if (cmdPtr->cmdEpoch != resPtr->cmdEpoch) {
4801 if (cmdPtr == NULL) {
4802 result = tclCmdNameType.setFromAnyProc(interp, objPtr);
4803 if (result != TCL_OK) {
4804 return (Tcl_Command) NULL;
4806 resPtr = (ResolvedCmdName *) objPtr->internalRep.otherValuePtr;
4807 if (resPtr != NULL) {
4808 cmdPtr = resPtr->cmdPtr;
4811 return (Tcl_Command) cmdPtr;
4815 *----------------------------------------------------------------------
4817 * TclSetCmdNameObj --
4819 * Modify an object to be an CmdName object that refers to the argument
4820 * Command structure.
4826 * The object's old internal rep is freed. It's string rep is not
4827 * changed. The refcount in the Command structure is incremented to
4828 * keep it from being freed if the command is later deleted until
4829 * TclExecuteByteCode has a chance to recognize that it was deleted.
4831 *----------------------------------------------------------------------
4835 TclSetCmdNameObj(interp, objPtr, cmdPtr)
4836 Tcl_Interp *interp; /* Points to interpreter containing command
4837 * that should be cached in objPtr. */
4838 register Tcl_Obj *objPtr; /* Points to Tcl object to be changed to
4839 * a CmdName object. */
4840 Command *cmdPtr; /* Points to Command structure that the
4841 * CmdName object should refer to. */
4843 Interp *iPtr = (Interp *) interp;
4844 register ResolvedCmdName *resPtr;
4845 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
4846 register Namespace *currNsPtr;
4848 if (oldTypePtr == &tclCmdNameType) {
4853 * Get the current namespace.
4856 if (iPtr->varFramePtr != NULL) {
4857 currNsPtr = iPtr->varFramePtr->nsPtr;
4859 currNsPtr = iPtr->globalNsPtr;
4863 resPtr = (ResolvedCmdName *) ckalloc(sizeof(ResolvedCmdName));
4864 resPtr->cmdPtr = cmdPtr;
4865 resPtr->refNsPtr = currNsPtr;
4866 resPtr->refNsId = currNsPtr->nsId;
4867 resPtr->refNsCmdEpoch = currNsPtr->cmdRefEpoch;
4868 resPtr->cmdEpoch = cmdPtr->cmdEpoch;
4869 resPtr->refCount = 1;
4871 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
4872 oldTypePtr->freeIntRepProc(objPtr);
4874 objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) resPtr;
4875 objPtr->internalRep.twoPtrValue.ptr2 = NULL;
4876 objPtr->typePtr = &tclCmdNameType;
4880 *----------------------------------------------------------------------
4882 * FreeCmdNameInternalRep --
4884 * Frees the resources associated with a cmdName object's internal
4891 * Decrements the ref count of any cached ResolvedCmdName structure
4892 * pointed to by the cmdName's internal representation. If this is
4893 * the last use of the ResolvedCmdName, it is freed. This in turn
4894 * decrements the ref count of the Command structure pointed to by
4895 * the ResolvedSymbol, which may free the Command structure.
4897 *----------------------------------------------------------------------
4901 FreeCmdNameInternalRep(objPtr)
4902 register Tcl_Obj *objPtr; /* CmdName object with internal
4903 * representation to free. */
4905 register ResolvedCmdName *resPtr =
4906 (ResolvedCmdName *) objPtr->internalRep.otherValuePtr;
4908 if (resPtr != NULL) {
4910 * Decrement the reference count of the ResolvedCmdName structure.
4911 * If there are no more uses, free the ResolvedCmdName structure.
4915 if (resPtr->refCount == 0) {
4917 * Now free the cached command, unless it is still in its
4918 * hash table or if there are other references to it
4919 * from other cmdName objects.
4922 Command *cmdPtr = resPtr->cmdPtr;
4923 TclCleanupCommand(cmdPtr);
4924 ckfree((char *) resPtr);
4930 *----------------------------------------------------------------------
4932 * DupCmdNameInternalRep --
4934 * Initialize the internal representation of an cmdName Tcl_Obj to a
4935 * copy of the internal representation of an existing cmdName object.
4941 * "copyPtr"s internal rep is set to point to the ResolvedCmdName
4942 * structure corresponding to "srcPtr"s internal rep. Increments the
4943 * ref count of the ResolvedCmdName structure pointed to by the
4944 * cmdName's internal representation.
4946 *----------------------------------------------------------------------
4950 DupCmdNameInternalRep(srcPtr, copyPtr)
4951 Tcl_Obj *srcPtr; /* Object with internal rep to copy. */
4952 register Tcl_Obj *copyPtr; /* Object with internal rep to set. */
4954 register ResolvedCmdName *resPtr =
4955 (ResolvedCmdName *) srcPtr->internalRep.otherValuePtr;
4957 copyPtr->internalRep.twoPtrValue.ptr1 = (VOID *) resPtr;
4958 copyPtr->internalRep.twoPtrValue.ptr2 = NULL;
4959 if (resPtr != NULL) {
4962 copyPtr->typePtr = &tclCmdNameType;
4966 *----------------------------------------------------------------------
4968 * SetCmdNameFromAny --
4970 * Generate an cmdName internal form for the Tcl object "objPtr".
4973 * The return value is a standard Tcl result. The conversion always
4974 * succeeds and TCL_OK is returned.
4977 * A pointer to a ResolvedCmdName structure that holds a cached pointer
4978 * to the command with a name that matches objPtr's string rep is
4979 * stored as objPtr's internal representation. This ResolvedCmdName
4980 * pointer will be NULL if no matching command was found. The ref count
4981 * of the cached Command's structure (if any) is also incremented.
4983 *----------------------------------------------------------------------
4987 SetCmdNameFromAny(interp, objPtr)
4988 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
4989 register Tcl_Obj *objPtr; /* The object to convert. */
4991 Interp *iPtr = (Interp *) interp;
4994 register Command *cmdPtr;
4995 Namespace *currNsPtr;
4996 register ResolvedCmdName *resPtr;
4999 * Get "objPtr"s string representation. Make it up-to-date if necessary.
5002 name = objPtr->bytes;
5004 name = Tcl_GetString(objPtr);
5008 * Find the Command structure, if any, that describes the command called
5009 * "name". Build a ResolvedCmdName that holds a cached pointer to this
5010 * Command, and bump the reference count in the referenced Command
5011 * structure. A Command structure will not be deleted as long as it is
5012 * referenced from a CmdName object.
5015 cmd = Tcl_FindCommand(interp, name, (Tcl_Namespace *) NULL,
5017 cmdPtr = (Command *) cmd;
5018 if (cmdPtr != NULL) {
5020 * Get the current namespace.
5023 if (iPtr->varFramePtr != NULL) {
5024 currNsPtr = iPtr->varFramePtr->nsPtr;
5026 currNsPtr = iPtr->globalNsPtr;
5030 resPtr = (ResolvedCmdName *) ckalloc(sizeof(ResolvedCmdName));
5031 resPtr->cmdPtr = cmdPtr;
5032 resPtr->refNsPtr = currNsPtr;
5033 resPtr->refNsId = currNsPtr->nsId;
5034 resPtr->refNsCmdEpoch = currNsPtr->cmdRefEpoch;
5035 resPtr->cmdEpoch = cmdPtr->cmdEpoch;
5036 resPtr->refCount = 1;
5038 resPtr = NULL; /* no command named "name" was found */
5042 * Free the old internalRep before setting the new one. We do this as
5043 * late as possible to allow the conversion code, in particular
5044 * GetStringFromObj, to use that old internalRep. If no Command
5045 * structure was found, leave NULL as the cached value.
5048 if ((objPtr->typePtr != NULL)
5049 && (objPtr->typePtr->freeIntRepProc != NULL)) {
5050 objPtr->typePtr->freeIntRepProc(objPtr);
5053 objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) resPtr;
5054 objPtr->internalRep.twoPtrValue.ptr2 = NULL;
5055 objPtr->typePtr = &tclCmdNameType;
5059 #ifdef TCL_COMPILE_DEBUG
5061 *----------------------------------------------------------------------
5063 * StringForResultCode --
5065 * Procedure that returns a human-readable string representing a
5066 * Tcl result code such as TCL_ERROR.
5069 * If the result code is one of the standard Tcl return codes, the
5070 * result is a string representing that code such as "TCL_ERROR".
5071 * Otherwise, the result string is that code formatted as a
5072 * sequence of decimal digit characters. Note that the resulting
5073 * string must not be modified by the caller.
5078 *----------------------------------------------------------------------
5082 StringForResultCode(result)
5083 int result; /* The Tcl result code for which to
5084 * generate a string. */
5086 static char buf[TCL_INTEGER_SPACE];
5088 if ((result >= TCL_OK) && (result <= TCL_CONTINUE)) {
5089 return resultStrings[result];
5091 TclFormatInt(buf, result);
5094 #endif /* TCL_COMPILE_DEBUG */