#include "tclInt.h"
#include "tclCompile.h"
int tclTraceCompile = 0;
static int traceInitialized = 0;
#ifdef TCL_COMPILE_STATS
long tclNumCompilations = 0;
double tclTotalSourceBytes = 0.0;
double tclTotalCodeBytes = 0.0;
double tclTotalInstBytes = 0.0;
double tclTotalObjBytes = 0.0;
double tclTotalExceptBytes = 0.0;
double tclTotalAuxBytes = 0.0;
double tclTotalCmdMapBytes = 0.0;
double tclCurrentSourceBytes = 0.0;
double tclCurrentCodeBytes = 0.0;
int tclSourceCount[32];
int tclByteCodeCount[32];
#endif
InstructionDesc instructionTable[] = {
{"done", 1, 0, {OPERAND_NONE}},
{"push1", 2, 1, {OPERAND_UINT1}},
{"push4", 5, 1, {OPERAND_UINT4}},
{"pop", 1, 0, {OPERAND_NONE}},
{"dup", 1, 0, {OPERAND_NONE}},
{"concat1", 2, 1, {OPERAND_UINT1}},
{"invokeStk1", 2, 1, {OPERAND_UINT1}},
{"invokeStk4", 5, 1, {OPERAND_UINT4}},
{"evalStk", 1, 0, {OPERAND_NONE}},
{"exprStk", 1, 0, {OPERAND_NONE}},
{"loadScalar1", 2, 1, {OPERAND_UINT1}},
{"loadScalar4", 5, 1, {OPERAND_UINT4}},
{"loadScalarStk", 1, 0, {OPERAND_NONE}},
{"loadArray1", 2, 1, {OPERAND_UINT1}},
{"loadArray4", 5, 1, {OPERAND_UINT4}},
{"loadArrayStk", 1, 0, {OPERAND_NONE}},
{"loadStk", 1, 0, {OPERAND_NONE}},
{"storeScalar1", 2, 1, {OPERAND_UINT1}},
{"storeScalar4", 5, 1, {OPERAND_UINT4}},
{"storeScalarStk", 1, 0, {OPERAND_NONE}},
{"storeArray1", 2, 1, {OPERAND_UINT1}},
{"storeArray4", 5, 1, {OPERAND_UINT4}},
{"storeArrayStk", 1, 0, {OPERAND_NONE}},
{"storeStk", 1, 0, {OPERAND_NONE}},
{"incrScalar1", 2, 1, {OPERAND_UINT1}},
{"incrScalarStk", 1, 0, {OPERAND_NONE}},
{"incrArray1", 2, 1, {OPERAND_UINT1}},
{"incrArrayStk", 1, 0, {OPERAND_NONE}},
{"incrStk", 1, 0, {OPERAND_NONE}},
{"incrScalar1Imm", 3, 2, {OPERAND_UINT1, OPERAND_INT1}},
{"incrScalarStkImm", 2, 1, {OPERAND_INT1}},
{"incrArray1Imm", 3, 2, {OPERAND_UINT1, OPERAND_INT1}},
{"incrArrayStkImm", 2, 1, {OPERAND_INT1}},
{"incrStkImm", 2, 1, {OPERAND_INT1}},
{"jump1", 2, 1, {OPERAND_INT1}},
{"jump4", 5, 1, {OPERAND_INT4}},
{"jumpTrue1", 2, 1, {OPERAND_INT1}},
{"jumpTrue4", 5, 1, {OPERAND_INT4}},
{"jumpFalse1", 2, 1, {OPERAND_INT1}},
{"jumpFalse4", 5, 1, {OPERAND_INT4}},
{"lor", 1, 0, {OPERAND_NONE}},
{"land", 1, 0, {OPERAND_NONE}},
{"bitor", 1, 0, {OPERAND_NONE}},
{"bitxor", 1, 0, {OPERAND_NONE}},
{"bitand", 1, 0, {OPERAND_NONE}},
{"eq", 1, 0, {OPERAND_NONE}},
{"neq", 1, 0, {OPERAND_NONE}},
{"lt", 1, 0, {OPERAND_NONE}},
{"gt", 1, 0, {OPERAND_NONE}},
{"le", 1, 0, {OPERAND_NONE}},
{"ge", 1, 0, {OPERAND_NONE}},
{"lshift", 1, 0, {OPERAND_NONE}},
{"rshift", 1, 0, {OPERAND_NONE}},
{"add", 1, 0, {OPERAND_NONE}},
{"sub", 1, 0, {OPERAND_NONE}},
{"mult", 1, 0, {OPERAND_NONE}},
{"div", 1, 0, {OPERAND_NONE}},
{"mod", 1, 0, {OPERAND_NONE}},
{"uplus", 1, 0, {OPERAND_NONE}},
{"uminus", 1, 0, {OPERAND_NONE}},
{"bitnot", 1, 0, {OPERAND_NONE}},
{"not", 1, 0, {OPERAND_NONE}},
{"callBuiltinFunc1", 2, 1, {OPERAND_UINT1}},
{"callFunc1", 2, 1, {OPERAND_UINT1}},
{"tryCvtToNumeric", 1, 0, {OPERAND_NONE}},
{"break", 1, 0, {OPERAND_NONE}},
{"continue", 1, 0, {OPERAND_NONE}},
{"foreach_start4", 5, 1, {OPERAND_UINT4}},
{"foreach_step4", 5, 1, {OPERAND_UINT4}},
{"beginCatch4", 5, 1, {OPERAND_UINT4}},
{"endCatch", 1, 0, {OPERAND_NONE}},
{"pushResult", 1, 0, {OPERAND_NONE}},
{"pushReturnCode", 1, 0, {OPERAND_NONE}},
{0}
};
unsigned char tclTypeTable[] = {
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_SPACE, TCL_COMMAND_END, TCL_SPACE,
TCL_SPACE, TCL_SPACE, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_SPACE, TCL_NORMAL, TCL_QUOTE, TCL_NORMAL,
TCL_DOLLAR, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_COMMAND_END,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_OPEN_BRACKET,
TCL_BACKSLASH, TCL_COMMAND_END, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_OPEN_BRACE,
TCL_NORMAL, TCL_CLOSE_BRACE, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
TCL_NORMAL, TCL_NORMAL, TCL_NORMAL, TCL_NORMAL,
};
static Tcl_HashTable auxDataTypeTable;
static int auxDataTypeTableInitialized = 0;
static void AdvanceToNextWord _ANSI_ARGS_((char *string,
CompileEnv *envPtr));
static int CollectArgInfo _ANSI_ARGS_((Tcl_Interp *interp,
char *string, char *lastChar, int flags,
ArgInfo *argInfoPtr));
static int CompileBraces _ANSI_ARGS_((Tcl_Interp *interp,
char *string, char *lastChar, int flags,
CompileEnv *envPtr));
static int CompileCmdWordInline _ANSI_ARGS_((
Tcl_Interp *interp, char *string,
char *lastChar, int flags, CompileEnv *envPtr));
static int CompileExprWord _ANSI_ARGS_((Tcl_Interp *interp,
char *string, char *lastChar, int flags,
CompileEnv *envPtr));
static int CompileMultipartWord _ANSI_ARGS_((
Tcl_Interp *interp, char *string,
char *lastChar, int flags, CompileEnv *envPtr));
static int CompileWord _ANSI_ARGS_((Tcl_Interp *interp,
char *string, char *lastChar, int flags,
CompileEnv *envPtr));
static int CreateExceptionRange _ANSI_ARGS_((
ExceptionRangeType type, CompileEnv *envPtr));
static void DupByteCodeInternalRep _ANSI_ARGS_((Tcl_Obj *srcPtr,
Tcl_Obj *copyPtr));
static ClientData DupForeachInfo _ANSI_ARGS_((ClientData clientData));
static unsigned char * EncodeCmdLocMap _ANSI_ARGS_((
CompileEnv *envPtr, ByteCode *codePtr,
unsigned char *startPtr));
static void EnterCmdExtentData _ANSI_ARGS_((
CompileEnv *envPtr, int cmdNumber,
int numSrcChars, int numCodeBytes));
static void EnterCmdStartData _ANSI_ARGS_((
CompileEnv *envPtr, int cmdNumber,
int srcOffset, int codeOffset));
static void ExpandObjectArray _ANSI_ARGS_((CompileEnv *envPtr));
static void FreeForeachInfo _ANSI_ARGS_((
ClientData clientData));
static void FreeByteCodeInternalRep _ANSI_ARGS_((
Tcl_Obj *objPtr));
static void FreeArgInfo _ANSI_ARGS_((ArgInfo *argInfoPtr));
static int GetCmdLocEncodingSize _ANSI_ARGS_((
CompileEnv *envPtr));
static void InitArgInfo _ANSI_ARGS_((ArgInfo *argInfoPtr));
static int IsLocalScalar _ANSI_ARGS_((char *name, int len));
static int LookupCompiledLocal _ANSI_ARGS_((
char *name, int nameChars, int createIfNew,
int flagsIfCreated, Proc *procPtr));
static int SetByteCodeFromAny _ANSI_ARGS_((Tcl_Interp *interp,
Tcl_Obj *objPtr));
static void UpdateStringOfByteCode _ANSI_ARGS_((Tcl_Obj *objPtr));
Tcl_ObjType tclByteCodeType = {
"bytecode",
FreeByteCodeInternalRep,
DupByteCodeInternalRep,
UpdateStringOfByteCode,
SetByteCodeFromAny
};
AuxDataType tclForeachInfoType = {
"ForeachInfo",
DupForeachInfo,
FreeForeachInfo
};
void
TclPrintByteCodeObj(interp, objPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
{
ByteCode* codePtr = (ByteCode *) objPtr->internalRep.otherValuePtr;
unsigned char *codeStart, *codeLimit, *pc;
unsigned char *codeDeltaNext, *codeLengthNext;
unsigned char *srcDeltaNext, *srcLengthNext;
int codeOffset, codeLen, srcOffset, srcLen;
int numCmds, numObjs, delta, objBytes, i;
if (codePtr->refCount <= 0) {
return;
}
codeStart = codePtr->codeStart;
codeLimit = (codeStart + codePtr->numCodeBytes);
numCmds = codePtr->numCommands;
numObjs = codePtr->numObjects;
objBytes = (numObjs * sizeof(Tcl_Obj));
for (i = 0; i < numObjs; i++) {
Tcl_Obj *litObjPtr = codePtr->objArrayPtr[i];
if (litObjPtr->bytes != NULL) {
objBytes += litObjPtr->length;
}
}
fprintf(stdout, "\nByteCode 0x%x, ref ct %u, epoch %u, interp 0x%x(epoch %u)\n",
(unsigned int) codePtr, codePtr->refCount,
codePtr->compileEpoch, (unsigned int) codePtr->iPtr,
codePtr->iPtr->compileEpoch);
fprintf(stdout, " Source ");
TclPrintSource(stdout, codePtr->source,
TclMin(codePtr->numSrcChars, 70));
fprintf(stdout, "\n Cmds %d, chars %d, inst %d, objs %u, aux %d, stk depth %u, code/src %.2f\n",
numCmds, codePtr->numSrcChars, codePtr->numCodeBytes, numObjs,
codePtr->numAuxDataItems, codePtr->maxStackDepth,
(codePtr->numSrcChars?
((float)codePtr->totalSize)/((float)codePtr->numSrcChars) : 0.0));
fprintf(stdout, " Code %d = %d(header)+%d(inst)+%d(objs)+%d(exc)+%d(aux)+%d(cmd map)\n",
codePtr->totalSize, sizeof(ByteCode), codePtr->numCodeBytes,
objBytes, (codePtr->numExcRanges * sizeof(ExceptionRange)),
(codePtr->numAuxDataItems * sizeof(AuxData)),
codePtr->numCmdLocBytes);
if (codePtr->procPtr != NULL) {
Proc *procPtr = codePtr->procPtr;
int numCompiledLocals = procPtr->numCompiledLocals;
fprintf(stdout,
" Proc 0x%x, ref ct %d, args %d, compiled locals %d\n",
(unsigned int) procPtr, procPtr->refCount, procPtr->numArgs,
numCompiledLocals);
if (numCompiledLocals > 0) {
CompiledLocal *localPtr = procPtr->firstLocalPtr;
for (i = 0; i < numCompiledLocals; i++) {
fprintf(stdout, " %d: slot %d%s%s%s%s%s%s",
i, localPtr->frameIndex,
((localPtr->flags & VAR_SCALAR)? ", scalar" : ""),
((localPtr->flags & VAR_ARRAY)? ", array" : ""),
((localPtr->flags & VAR_LINK)? ", link" : ""),
((localPtr->flags & VAR_ARGUMENT)? ", arg" : ""),
((localPtr->flags & VAR_TEMPORARY)? ", temp" : ""),
((localPtr->flags & VAR_RESOLVED)? ", resolved" : ""));
if (TclIsVarTemporary(localPtr)) {
fprintf(stdout, "\n");
} else {
fprintf(stdout, ", name=\"%s\"\n", localPtr->name);
}
localPtr = localPtr->nextPtr;
}
}
}
if (codePtr->numExcRanges > 0) {
fprintf(stdout, " Exception ranges %d, depth %d:\n",
codePtr->numExcRanges, codePtr->maxExcRangeDepth);
for (i = 0; i < codePtr->numExcRanges; i++) {
ExceptionRange *rangePtr = &(codePtr->excRangeArrayPtr[i]);
fprintf(stdout, " %d: level %d, %s, pc %d-%d, ",
i, rangePtr->nestingLevel,
((rangePtr->type == LOOP_EXCEPTION_RANGE)? "loop":"catch"),
rangePtr->codeOffset,
(rangePtr->codeOffset + rangePtr->numCodeBytes - 1));
switch (rangePtr->type) {
case LOOP_EXCEPTION_RANGE:
fprintf(stdout, "continue %d, break %d\n",
rangePtr->continueOffset, rangePtr->breakOffset);
break;
case CATCH_EXCEPTION_RANGE:
fprintf(stdout, "catch %d\n", rangePtr->catchOffset);
break;
default:
panic("TclPrintSource: unrecognized ExceptionRange type %d\n",
rangePtr->type);
}
}
}
if (numCmds == 0) {
pc = codeStart;
while (pc < codeLimit) {
fprintf(stdout, " ");
pc += TclPrintInstruction(codePtr, pc);
}
return;
}
fprintf(stdout, " Commands %d:", numCmds);
codeDeltaNext = codePtr->codeDeltaStart;
codeLengthNext = codePtr->codeLengthStart;
srcDeltaNext = codePtr->srcDeltaStart;
srcLengthNext = codePtr->srcLengthStart;
codeOffset = srcOffset = 0;
for (i = 0; i < numCmds; i++) {
if ((unsigned int) (*codeDeltaNext) == (unsigned int) 0xFF) {
codeDeltaNext++;
delta = TclGetInt4AtPtr(codeDeltaNext);
codeDeltaNext += 4;
} else {
delta = TclGetInt1AtPtr(codeDeltaNext);
codeDeltaNext++;
}
codeOffset += delta;
if ((unsigned int) (*codeLengthNext) == (unsigned int) 0xFF) {
codeLengthNext++;
codeLen = TclGetInt4AtPtr(codeLengthNext);
codeLengthNext += 4;
} else {
codeLen = TclGetInt1AtPtr(codeLengthNext);
codeLengthNext++;
}
if ((unsigned int) (*srcDeltaNext) == (unsigned int) 0xFF) {
srcDeltaNext++;
delta = TclGetInt4AtPtr(srcDeltaNext);
srcDeltaNext += 4;
} else {
delta = TclGetInt1AtPtr(srcDeltaNext);
srcDeltaNext++;
}
srcOffset += delta;
if ((unsigned int) (*srcLengthNext) == (unsigned int) 0xFF) {
srcLengthNext++;
srcLen = TclGetInt4AtPtr(srcLengthNext);
srcLengthNext += 4;
} else {
srcLen = TclGetInt1AtPtr(srcLengthNext);
srcLengthNext++;
}
fprintf(stdout, "%s%4d: pc %d-%d, source %d-%d",
((i % 2)? " " : "\n "),
(i+1), codeOffset, (codeOffset + codeLen - 1),
srcOffset, (srcOffset + srcLen - 1));
}
if ((numCmds > 0) && ((numCmds % 2) != 0)) {
fprintf(stdout, "\n");
}
codeDeltaNext = codePtr->codeDeltaStart;
srcDeltaNext = codePtr->srcDeltaStart;
srcLengthNext = codePtr->srcLengthStart;
codeOffset = srcOffset = 0;
pc = codeStart;
for (i = 0; i < numCmds; i++) {
if ((unsigned int) (*codeDeltaNext) == (unsigned int) 0xFF) {
codeDeltaNext++;
delta = TclGetInt4AtPtr(codeDeltaNext);
codeDeltaNext += 4;
} else {
delta = TclGetInt1AtPtr(codeDeltaNext);
codeDeltaNext++;
}
codeOffset += delta;
if ((unsigned int) (*srcDeltaNext) == (unsigned int) 0xFF) {
srcDeltaNext++;
delta = TclGetInt4AtPtr(srcDeltaNext);
srcDeltaNext += 4;
} else {
delta = TclGetInt1AtPtr(srcDeltaNext);
srcDeltaNext++;
}
srcOffset += delta;
if ((unsigned int) (*srcLengthNext) == (unsigned int) 0xFF) {
srcLengthNext++;
srcLen = TclGetInt4AtPtr(srcLengthNext);
srcLengthNext += 4;
} else {
srcLen = TclGetInt1AtPtr(srcLengthNext);
srcLengthNext++;
}
while ((pc-codeStart) < codeOffset) {
fprintf(stdout, " ");
pc += TclPrintInstruction(codePtr, pc);
}
fprintf(stdout, " Command %d: ", (i+1));
TclPrintSource(stdout, (codePtr->source + srcOffset),
TclMin(srcLen, 70));
fprintf(stdout, "\n");
}
if (pc < codeLimit) {
while (pc < codeLimit) {
fprintf(stdout, " ");
pc += TclPrintInstruction(codePtr, pc);
}
}
}
int
TclPrintInstruction(codePtr, pc)
ByteCode* codePtr;
unsigned char *pc;
{
Proc *procPtr = codePtr->procPtr;
unsigned char opCode = *pc;
register InstructionDesc *instDesc = &instructionTable[opCode];
unsigned char *codeStart = codePtr->codeStart;
unsigned int pcOffset = (pc - codeStart);
int opnd, elemLen, i, j;
Tcl_Obj *elemPtr;
char *string;
fprintf(stdout, "(%u) %s ", pcOffset, instDesc->name);
for (i = 0; i < instDesc->numOperands; i++) {
switch (instDesc->opTypes[i]) {
case OPERAND_INT1:
opnd = TclGetInt1AtPtr(pc+1+i);
if ((i == 0) && ((opCode == INST_JUMP1)
|| (opCode == INST_JUMP_TRUE1)
|| (opCode == INST_JUMP_FALSE1))) {
fprintf(stdout, "%d # pc %u", opnd, (pcOffset + opnd));
} else {
fprintf(stdout, "%d", opnd);
}
break;
case OPERAND_INT4:
opnd = TclGetInt4AtPtr(pc+1+i);
if ((i == 0) && ((opCode == INST_JUMP4)
|| (opCode == INST_JUMP_TRUE4)
|| (opCode == INST_JUMP_FALSE4))) {
fprintf(stdout, "%d # pc %u", opnd, (pcOffset + opnd));
} else {
fprintf(stdout, "%d", opnd);
}
break;
case OPERAND_UINT1:
opnd = TclGetUInt1AtPtr(pc+1+i);
if ((i == 0) && (opCode == INST_PUSH1)) {
elemPtr = codePtr->objArrayPtr[opnd];
string = Tcl_GetStringFromObj(elemPtr, &elemLen);
fprintf(stdout, "%u # ", (unsigned int) opnd);
TclPrintSource(stdout, string, TclMin(elemLen, 40));
} else if ((i == 0) && ((opCode == INST_LOAD_SCALAR1)
|| (opCode == INST_LOAD_ARRAY1)
|| (opCode == INST_STORE_SCALAR1)
|| (opCode == INST_STORE_ARRAY1))) {
int localCt = procPtr->numCompiledLocals;
CompiledLocal *localPtr = procPtr->firstLocalPtr;
if (opnd >= localCt) {
panic("TclPrintInstruction: bad local var index %u (%u locals)\n",
(unsigned int) opnd, localCt);
return instDesc->numBytes;
}
for (j = 0; j < opnd; j++) {
localPtr = localPtr->nextPtr;
}
if (TclIsVarTemporary(localPtr)) {
fprintf(stdout, "%u # temp var %u",
(unsigned int) opnd, (unsigned int) opnd);
} else {
fprintf(stdout, "%u # var ", (unsigned int) opnd);
TclPrintSource(stdout, localPtr->name, 40);
}
} else {
fprintf(stdout, "%u ", (unsigned int) opnd);
}
break;
case OPERAND_UINT4:
opnd = TclGetUInt4AtPtr(pc+1+i);
if (opCode == INST_PUSH4) {
elemPtr = codePtr->objArrayPtr[opnd];
string = Tcl_GetStringFromObj(elemPtr, &elemLen);
fprintf(stdout, "%u # ", opnd);
TclPrintSource(stdout, string, TclMin(elemLen, 40));
} else if ((i == 0) && ((opCode == INST_LOAD_SCALAR4)
|| (opCode == INST_LOAD_ARRAY4)
|| (opCode == INST_STORE_SCALAR4)
|| (opCode == INST_STORE_ARRAY4))) {
int localCt = procPtr->numCompiledLocals;
CompiledLocal *localPtr = procPtr->firstLocalPtr;
if (opnd >= localCt) {
panic("TclPrintInstruction: bad local var index %u (%u locals)\n",
(unsigned int) opnd, localCt);
return instDesc->numBytes;
}
for (j = 0; j < opnd; j++) {
localPtr = localPtr->nextPtr;
}
if (TclIsVarTemporary(localPtr)) {
fprintf(stdout, "%u # temp var %u",
(unsigned int) opnd, (unsigned int) opnd);
} else {
fprintf(stdout, "%u # var ", (unsigned int) opnd);
TclPrintSource(stdout, localPtr->name, 40);
}
} else {
fprintf(stdout, "%u ", (unsigned int) opnd);
}
break;
case OPERAND_NONE:
default:
break;
}
}
fprintf(stdout, "\n");
return instDesc->numBytes;
}
void
TclPrintSource(outFile, string, maxChars)
FILE *outFile;
char *string;
int maxChars;
{
register char *p;
register int i = 0;
if (string == NULL) {
fprintf(outFile, "\"\"");
return;
}
fprintf(outFile, "\"");
p = string;
for (; (*p != '\0') && (i < maxChars); p++, i++) {
switch (*p) {
case '"':
fprintf(outFile, "\\\"");
continue;
case '\f':
fprintf(outFile, "\\f");
continue;
case '\n':
fprintf(outFile, "\\n");
continue;
case '\r':
fprintf(outFile, "\\r");
continue;
case '\t':
fprintf(outFile, "\\t");
continue;
case '\v':
fprintf(outFile, "\\v");
continue;
default:
fprintf(outFile, "%c", *p);
continue;
}
}
fprintf(outFile, "\"");
}
static void
FreeByteCodeInternalRep(objPtr)
register Tcl_Obj *objPtr;
{
register ByteCode *codePtr =
(ByteCode *) objPtr->internalRep.otherValuePtr;
codePtr->refCount--;
if (codePtr->refCount <= 0) {
TclCleanupByteCode(codePtr);
}
objPtr->typePtr = NULL;
objPtr->internalRep.otherValuePtr = NULL;
}
void
TclCleanupByteCode(codePtr)
ByteCode *codePtr;
{
Tcl_Obj **objArrayPtr = codePtr->objArrayPtr;
int numObjects = codePtr->numObjects;
int numAuxDataItems = codePtr->numAuxDataItems;
register AuxData *auxDataPtr;
register Tcl_Obj *elemPtr;
register int i;
#ifdef TCL_COMPILE_STATS
tclCurrentSourceBytes -= (double) codePtr->numSrcChars;
tclCurrentCodeBytes -= (double) codePtr->totalSize;
#endif
for (i = 0; i < numObjects; i++) {
elemPtr = objArrayPtr[i];
TclDecrRefCount(elemPtr);
}
auxDataPtr = codePtr->auxDataArrayPtr;
for (i = 0; i < numAuxDataItems; i++) {
if (auxDataPtr->type->freeProc != NULL) {
auxDataPtr->type->freeProc(auxDataPtr->clientData);
}
auxDataPtr++;
}
ckfree((char *) codePtr);
}
static void
DupByteCodeInternalRep(srcPtr, copyPtr)
Tcl_Obj *srcPtr;
Tcl_Obj *copyPtr;
{
return;
}
static int
SetByteCodeFromAny(interp, objPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
{
Interp *iPtr = (Interp *) interp;
char *string;
CompileEnv compEnv;
AuxData *auxDataPtr;
register int i;
int length, result;
if (!traceInitialized) {
if (Tcl_LinkVar(interp, "tcl_traceCompile",
(char *) &tclTraceCompile, TCL_LINK_INT) != TCL_OK) {
panic("SetByteCodeFromAny: unable to create link for tcl_traceCompile variable");
}
traceInitialized = 1;
}
string = Tcl_GetStringFromObj(objPtr, &length);
TclInitCompileEnv(interp, &compEnv, string);
result = TclCompileString(interp, string, string+length,
iPtr->evalFlags, &compEnv);
if (result == TCL_OK) {
TclEmitOpcode(INST_DONE, &compEnv);
TclInitByteCodeObj(objPtr, &compEnv);
} else {
for (i = 0; i < compEnv.objArrayNext; i++) {
Tcl_Obj *elemPtr = compEnv.objArrayPtr[i];
Tcl_DecrRefCount(elemPtr);
}
auxDataPtr = compEnv.auxDataArrayPtr;
for (i = 0; i < compEnv.auxDataArrayNext; i++) {
if (auxDataPtr->type->freeProc != NULL) {
auxDataPtr->type->freeProc(auxDataPtr->clientData);
}
auxDataPtr++;
}
}
TclFreeCompileEnv(&compEnv);
if (result == TCL_OK) {
if (tclTraceCompile == 2) {
TclPrintByteCodeObj(interp, objPtr);
}
}
return result;
}
static void
UpdateStringOfByteCode(objPtr)
register Tcl_Obj *objPtr;
{
panic("UpdateStringOfByteCode should never be called.");
}
void
TclInitCompileEnv(interp, envPtr, string)
Tcl_Interp *interp;
register CompileEnv *envPtr;
char *string;
{
Interp *iPtr = (Interp *) interp;
envPtr->iPtr = iPtr;
envPtr->source = string;
envPtr->procPtr = iPtr->compiledProcPtr;
envPtr->numCommands = 0;
envPtr->excRangeDepth = 0;
envPtr->maxExcRangeDepth = 0;
envPtr->maxStackDepth = 0;
Tcl_InitHashTable(&(envPtr->objTable), TCL_STRING_KEYS);
envPtr->pushSimpleWords = 1;
envPtr->wordIsSimple = 0;
envPtr->numSimpleWordChars = 0;
envPtr->exprIsJustVarRef = 0;
envPtr->exprIsComparison = 0;
envPtr->termOffset = 0;
envPtr->codeStart = envPtr->staticCodeSpace;
envPtr->codeNext = envPtr->codeStart;
envPtr->codeEnd = (envPtr->codeStart + COMPILEENV_INIT_CODE_BYTES);
envPtr->mallocedCodeArray = 0;
envPtr->objArrayPtr = envPtr->staticObjArraySpace;
envPtr->objArrayNext = 0;
envPtr->objArrayEnd = COMPILEENV_INIT_NUM_OBJECTS;
envPtr->mallocedObjArray = 0;
envPtr->excRangeArrayPtr = envPtr->staticExcRangeArraySpace;
envPtr->excRangeArrayNext = 0;
envPtr->excRangeArrayEnd = COMPILEENV_INIT_EXCEPT_RANGES;
envPtr->mallocedExcRangeArray = 0;
envPtr->cmdMapPtr = envPtr->staticCmdMapSpace;
envPtr->cmdMapEnd = COMPILEENV_INIT_CMD_MAP_SIZE;
envPtr->mallocedCmdMap = 0;
envPtr->auxDataArrayPtr = envPtr->staticAuxDataArraySpace;
envPtr->auxDataArrayNext = 0;
envPtr->auxDataArrayEnd = COMPILEENV_INIT_AUX_DATA_SIZE;
envPtr->mallocedAuxDataArray = 0;
}
void
TclFreeCompileEnv(envPtr)
register CompileEnv *envPtr;
{
Tcl_DeleteHashTable(&(envPtr->objTable));
if (envPtr->mallocedCodeArray) {
ckfree((char *) envPtr->codeStart);
}
if (envPtr->mallocedObjArray) {
ckfree((char *) envPtr->objArrayPtr);
}
if (envPtr->mallocedExcRangeArray) {
ckfree((char *) envPtr->excRangeArrayPtr);
}
if (envPtr->mallocedCmdMap) {
ckfree((char *) envPtr->cmdMapPtr);
}
if (envPtr->mallocedAuxDataArray) {
ckfree((char *) envPtr->auxDataArrayPtr);
}
}
void
TclInitByteCodeObj(objPtr, envPtr)
Tcl_Obj *objPtr;
register CompileEnv *envPtr;
{
register ByteCode *codePtr;
size_t codeBytes, objArrayBytes, exceptArrayBytes, cmdLocBytes;
size_t auxDataArrayBytes;
register size_t size, objBytes, totalSize;
register unsigned char *p;
unsigned char *nextPtr;
int srcLen = envPtr->termOffset;
int numObjects, i;
Namespace *namespacePtr;
#ifdef TCL_COMPILE_STATS
int srcLenLog2, sizeLog2;
#endif
codeBytes = (envPtr->codeNext - envPtr->codeStart);
numObjects = envPtr->objArrayNext;
objArrayBytes = (envPtr->objArrayNext * sizeof(Tcl_Obj *));
exceptArrayBytes = (envPtr->excRangeArrayNext * sizeof(ExceptionRange));
auxDataArrayBytes = (envPtr->auxDataArrayNext * sizeof(AuxData));
cmdLocBytes = GetCmdLocEncodingSize(envPtr);
size = sizeof(ByteCode);
size += TCL_ALIGN(codeBytes);
size += TCL_ALIGN(objArrayBytes);
size += TCL_ALIGN(exceptArrayBytes);
size += auxDataArrayBytes;
size += cmdLocBytes;
objBytes = (numObjects * sizeof(Tcl_Obj));
for (i = 0; i < numObjects; i++) {
Tcl_Obj *litObjPtr = envPtr->objArrayPtr[i];
if (litObjPtr->bytes != NULL) {
objBytes += litObjPtr->length;
}
}
totalSize = (size + objBytes);
#ifdef TCL_COMPILE_STATS
tclNumCompilations++;
tclTotalSourceBytes += (double) srcLen;
tclTotalCodeBytes += (double) totalSize;
tclTotalInstBytes += (double) codeBytes;
tclTotalObjBytes += (double) objBytes;
tclTotalExceptBytes += exceptArrayBytes;
tclTotalAuxBytes += (double) auxDataArrayBytes;
tclTotalCmdMapBytes += (double) cmdLocBytes;
tclCurrentSourceBytes += (double) srcLen;
tclCurrentCodeBytes += (double) totalSize;
srcLenLog2 = TclLog2(srcLen);
sizeLog2 = TclLog2((int) totalSize);
if ((srcLenLog2 > 31) || (sizeLog2 > 31)) {
panic("TclInitByteCodeObj: bad source or code sizes\n");
}
tclSourceCount[srcLenLog2]++;
tclByteCodeCount[sizeLog2]++;
#endif
if (envPtr->iPtr->varFramePtr != NULL) {
namespacePtr = envPtr->iPtr->varFramePtr->nsPtr;
} else {
namespacePtr = envPtr->iPtr->globalNsPtr;
}
p = (unsigned char *) ckalloc(size);
codePtr = (ByteCode *) p;
codePtr->iPtr = envPtr->iPtr;
codePtr->compileEpoch = envPtr->iPtr->compileEpoch;
codePtr->nsPtr = namespacePtr;
codePtr->nsEpoch = namespacePtr->resolverEpoch;
codePtr->refCount = 1;
codePtr->flags = 0;
codePtr->source = envPtr->source;
codePtr->procPtr = envPtr->procPtr;
codePtr->totalSize = totalSize;
codePtr->numCommands = envPtr->numCommands;
codePtr->numSrcChars = srcLen;
codePtr->numCodeBytes = codeBytes;
codePtr->numObjects = numObjects;
codePtr->numExcRanges = envPtr->excRangeArrayNext;
codePtr->numAuxDataItems = envPtr->auxDataArrayNext;
codePtr->auxDataArrayPtr = NULL;
codePtr->numCmdLocBytes = cmdLocBytes;
codePtr->maxExcRangeDepth = envPtr->maxExcRangeDepth;
codePtr->maxStackDepth = envPtr->maxStackDepth;
p += sizeof(ByteCode);
codePtr->codeStart = p;
memcpy((VOID *) p, (VOID *) envPtr->codeStart, codeBytes);
p += TCL_ALIGN(codeBytes);
codePtr->objArrayPtr = (Tcl_Obj **) p;
memcpy((VOID *) p, (VOID *) envPtr->objArrayPtr, objArrayBytes);
p += TCL_ALIGN(objArrayBytes);
if (exceptArrayBytes > 0) {
codePtr->excRangeArrayPtr = (ExceptionRange *) p;
memcpy((VOID *) p, (VOID *) envPtr->excRangeArrayPtr,
exceptArrayBytes);
}
p += TCL_ALIGN(exceptArrayBytes);
if (auxDataArrayBytes > 0) {
codePtr->auxDataArrayPtr = (AuxData *) p;
memcpy((VOID *) p, (VOID *) envPtr->auxDataArrayPtr,
auxDataArrayBytes);
}
p += auxDataArrayBytes;
nextPtr = EncodeCmdLocMap(envPtr, codePtr, (unsigned char *) p);
if (((size_t)(nextPtr - p)) != cmdLocBytes) {
panic("TclInitByteCodeObj: encoded cmd location bytes %d != expected size %d\n", (nextPtr - p), cmdLocBytes);
}
if ((objPtr->typePtr != NULL) &&
(objPtr->typePtr->freeIntRepProc != NULL)) {
objPtr->typePtr->freeIntRepProc(objPtr);
}
objPtr->internalRep.otherValuePtr = (VOID *) codePtr;
objPtr->typePtr = &tclByteCodeType;
}
static int
GetCmdLocEncodingSize(envPtr)
CompileEnv *envPtr;
{
register CmdLocation *mapPtr = envPtr->cmdMapPtr;
int numCmds = envPtr->numCommands;
int codeDelta, codeLen, srcDelta, srcLen;
int codeDeltaNext, codeLengthNext, srcDeltaNext, srcLengthNext;
int prevCodeOffset, prevSrcOffset, i;
codeDeltaNext = codeLengthNext = srcDeltaNext = srcLengthNext = 0;
prevCodeOffset = prevSrcOffset = 0;
for (i = 0; i < numCmds; i++) {
codeDelta = (mapPtr[i].codeOffset - prevCodeOffset);
if (codeDelta < 0) {
panic("GetCmdLocEncodingSize: bad code offset");
} else if (codeDelta <= 127) {
codeDeltaNext++;
} else {
codeDeltaNext += 5;
}
prevCodeOffset = mapPtr[i].codeOffset;
codeLen = mapPtr[i].numCodeBytes;
if (codeLen < 0) {
panic("GetCmdLocEncodingSize: bad code length");
} else if (codeLen <= 127) {
codeLengthNext++;
} else {
codeLengthNext += 5;
}
srcDelta = (mapPtr[i].srcOffset - prevSrcOffset);
if ((-127 <= srcDelta) && (srcDelta <= 127)) {
srcDeltaNext++;
} else {
srcDeltaNext += 5;
}
prevSrcOffset = mapPtr[i].srcOffset;
srcLen = mapPtr[i].numSrcChars;
if (srcLen < 0) {
panic("GetCmdLocEncodingSize: bad source length");
} else if (srcLen <= 127) {
srcLengthNext++;
} else {
srcLengthNext += 5;
}
}
return (codeDeltaNext + codeLengthNext + srcDeltaNext + srcLengthNext);
}
static unsigned char *
EncodeCmdLocMap(envPtr, codePtr, startPtr)
CompileEnv *envPtr;
ByteCode *codePtr;
unsigned char *startPtr;
{
register CmdLocation *mapPtr = envPtr->cmdMapPtr;
int numCmds = envPtr->numCommands;
register unsigned char *p = startPtr;
int codeDelta, codeLen, srcDelta, srcLen, prevOffset;
register int i;
codePtr->codeDeltaStart = p;
prevOffset = 0;
for (i = 0; i < numCmds; i++) {
codeDelta = (mapPtr[i].codeOffset - prevOffset);
if (codeDelta < 0) {
panic("EncodeCmdLocMap: bad code offset");
} else if (codeDelta <= 127) {
TclStoreInt1AtPtr(codeDelta, p);
p++;
} else {
TclStoreInt1AtPtr(0xFF, p);
p++;
TclStoreInt4AtPtr(codeDelta, p);
p += 4;
}
prevOffset = mapPtr[i].codeOffset;
}
codePtr->codeLengthStart = p;
for (i = 0; i < numCmds; i++) {
codeLen = mapPtr[i].numCodeBytes;
if (codeLen < 0) {
panic("EncodeCmdLocMap: bad code length");
} else if (codeLen <= 127) {
TclStoreInt1AtPtr(codeLen, p);
p++;
} else {
TclStoreInt1AtPtr(0xFF, p);
p++;
TclStoreInt4AtPtr(codeLen, p);
p += 4;
}
}
codePtr->srcDeltaStart = p;
prevOffset = 0;
for (i = 0; i < numCmds; i++) {
srcDelta = (mapPtr[i].srcOffset - prevOffset);
if ((-127 <= srcDelta) && (srcDelta <= 127)) {
TclStoreInt1AtPtr(srcDelta, p);
p++;
} else {
TclStoreInt1AtPtr(0xFF, p);
p++;
TclStoreInt4AtPtr(srcDelta, p);
p += 4;
}
prevOffset = mapPtr[i].srcOffset;
}
codePtr->srcLengthStart = p;
for (i = 0; i < numCmds; i++) {
srcLen = mapPtr[i].numSrcChars;
if (srcLen < 0) {
panic("EncodeCmdLocMap: bad source length");
} else if (srcLen <= 127) {
TclStoreInt1AtPtr(srcLen, p);
p++;
} else {
TclStoreInt1AtPtr(0xFF, p);
p++;
TclStoreInt4AtPtr(srcLen, p);
p += 4;
}
}
return p;
}
int
TclCompileString(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
Interp *iPtr = (Interp *) interp;
register char *src = string;
register char c = *src;
register int type;
char termChar = (char)((flags & TCL_BRACKET_TERM)? ']' : '\0');
int isFirstCmd = 1;
char *cmdSrcStart = NULL;
int cmdIndex;
int lastTopLevelCmdIndex = -1;
int cmdCodeOffset = -1;
int cmdWords;
Tcl_Command cmd;
Command *cmdPtr;
int maxDepth = 0;
char *termPtr;
char savedChar;
int objIndex = -1;
unsigned char *entryCodeNext = envPtr->codeNext;
char *ellipsis = "";
Tcl_Obj *objPtr;
int numChars;
int result = TCL_OK;
int savePushSimpleWords = envPtr->pushSimpleWords;
while ((src != lastChar) && (c != termChar)) {
type = CHAR_TYPE(src, lastChar);
while ((type & (TCL_SPACE | TCL_BACKSLASH))
|| (c == '\n') || (c == ';')) {
if (type == TCL_BACKSLASH) {
if (src[1] == '\n') {
src += 2;
} else {
break;
}
} else {
src++;
}
c = *src;
type = CHAR_TYPE(src, lastChar);
}
if (c == '#') {
while (src != lastChar) {
if (c == '\\') {
int numRead;
Tcl_Backslash(src, &numRead);
src += numRead;
} else if (c == '\n') {
src++;
c = *src;
envPtr->termOffset = (src - string);
break;
} else {
src++;
}
c = *src;
}
continue;
}
type = CHAR_TYPE(src, lastChar);
if ((type == TCL_COMMAND_END)
&& ((c != ']') || (flags & TCL_BRACKET_TERM))) {
continue;
}
if (!isFirstCmd) {
TclEmitOpcode(INST_POP, envPtr);
if (!(flags & TCL_BRACKET_TERM)) {
(envPtr->cmdMapPtr[lastTopLevelCmdIndex]).numCodeBytes =
(envPtr->codeNext-envPtr->codeStart) - cmdCodeOffset;
}
}
envPtr->numCommands++;
cmdIndex = (envPtr->numCommands - 1);
if (!(flags & TCL_BRACKET_TERM)) {
lastTopLevelCmdIndex = cmdIndex;
}
cmdSrcStart = src;
cmdCodeOffset = (envPtr->codeNext - envPtr->codeStart);
cmdWords = 0;
EnterCmdStartData(envPtr, cmdIndex, src-envPtr->source,
cmdCodeOffset);
if ((!(flags & TCL_BRACKET_TERM))
&& (tclTraceCompile >= 1) && (envPtr->procPtr == NULL)) {
char *p = cmdSrcStart;
int numChars, complete;
while ((CHAR_TYPE(p, lastChar) != TCL_COMMAND_END)
|| ((*p == ']') && !(flags & TCL_BRACKET_TERM))) {
p++;
}
numChars = (p - cmdSrcStart);
complete = 1;
if (numChars > 60) {
numChars = 60;
complete = 0;
} else if ((numChars >= 2) && (*p == '\n') && (*(p-1) == '{')) {
complete = 0;
}
fprintf(stdout, "Compiling: %.*s%s\n",
numChars, cmdSrcStart, (complete? "" : " ..."));
}
while ((type != TCL_COMMAND_END)
|| ((c == ']') && !(flags & TCL_BRACKET_TERM))) {
while (type & (TCL_SPACE | TCL_BACKSLASH)) {
if (type == TCL_BACKSLASH) {
if (src[1] == '\n') {
src += 2;
} else {
break;
}
} else {
src++;
}
c = *src;
type = CHAR_TYPE(src, lastChar);
}
if ((type == TCL_COMMAND_END)
&& ((c != ']') || (flags & TCL_BRACKET_TERM))) {
break;
}
envPtr->pushSimpleWords = 0;
if (type & (TCL_QUOTE | TCL_OPEN_BRACE)) {
src++;
if (type == TCL_QUOTE) {
result = TclCompileQuotes(interp, src, lastChar,
'"', flags, envPtr);
} else {
result = CompileBraces(interp, src, lastChar,
flags, envPtr);
}
termPtr = (src + envPtr->termOffset);
if (result != TCL_OK) {
src = termPtr;
goto done;
}
c = *termPtr;
if ((c == '\\') && (*(termPtr+1) == '\n')) {
} else {
type = CHAR_TYPE(termPtr, lastChar);
if ((type != TCL_SPACE) && (type != TCL_COMMAND_END)) {
Tcl_ResetResult(interp);
if (*(src-1) == '"') {
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"extra characters after close-quote", -1);
} else {
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"extra characters after close-brace", -1);
}
result = TCL_ERROR;
}
}
} else {
result = CompileMultipartWord(interp, src, lastChar,
flags, envPtr);
termPtr = (src + envPtr->termOffset);
}
if (result != TCL_OK) {
ellipsis = "...";
src = termPtr;
goto done;
}
if (envPtr->wordIsSimple) {
numChars = envPtr->numSimpleWordChars;
savedChar = src[numChars];
src[numChars] = '\0';
if ((cmdWords == 0)
&& (!(iPtr->flags & DONT_COMPILE_CMDS_INLINE))) {
Namespace *cmdNsPtr;
if (envPtr->procPtr != NULL) {
cmdNsPtr = envPtr->procPtr->cmdPtr->nsPtr;
} else {
cmdNsPtr = NULL;
}
cmdPtr = NULL;
cmd = Tcl_FindCommand(interp, src,
(Tcl_Namespace *) cmdNsPtr, 0);
if (cmd != (Tcl_Command) NULL) {
cmdPtr = (Command *) cmd;
}
if ((cmdPtr != NULL) && (cmdPtr->compileProc != NULL)) {
char *firstArg = termPtr;
src[numChars] = savedChar;
iPtr->flags &= ~(ERR_ALREADY_LOGGED | ERR_IN_PROGRESS
| ERROR_CODE_SET);
result = (*(cmdPtr->compileProc))(interp,
firstArg, lastChar, flags, envPtr);
if (result == TCL_OK) {
src = (firstArg + envPtr->termOffset);
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
goto finishCommand;
} else if (result == TCL_OUT_LINE_COMPILE) {
result = TCL_OK;
src[numChars] = '\0';
} else {
src = firstArg;
goto done;
}
}
objIndex = TclObjIndexForString(src, numChars,
1, 0, envPtr);
if (cmdPtr != NULL) {
objPtr = envPtr->objArrayPtr[objIndex];
if ((objPtr->typePtr != &tclCmdNameType)
&& (objPtr->bytes != NULL)) {
ResolvedCmdName *resPtr = (ResolvedCmdName *)
ckalloc(sizeof(ResolvedCmdName));
Namespace *nsPtr = (Namespace *)
Tcl_GetCurrentNamespace(interp);
resPtr->cmdPtr = cmdPtr;
resPtr->refNsPtr = nsPtr;
resPtr->refNsId = nsPtr->nsId;
resPtr->refNsCmdEpoch = nsPtr->cmdRefEpoch;
resPtr->cmdEpoch = cmdPtr->cmdEpoch;
resPtr->refCount = 1;
objPtr->internalRep.twoPtrValue.ptr1 =
(VOID *) resPtr;
objPtr->internalRep.twoPtrValue.ptr2 = NULL;
objPtr->typePtr = &tclCmdNameType;
cmdPtr->refCount++;
}
}
} else {
int isCompilableInt = 0;
long n;
char buf[40];
if (TclLooksLikeInt(src)) {
int code = TclGetLong(interp, src, &n);
if (code == TCL_OK) {
TclFormatInt(buf, n);
if (strcmp(src, buf) == 0) {
isCompilableInt = 1;
objIndex = TclObjIndexForString(src,
numChars, 0,
0, envPtr);
objPtr = envPtr->objArrayPtr[objIndex];
Tcl_InvalidateStringRep(objPtr);
objPtr->internalRep.longValue = n;
objPtr->typePtr = &tclIntType;
}
} else {
Tcl_ResetResult(interp);
}
}
if (!isCompilableInt) {
objIndex = TclObjIndexForString(src, numChars,
1, 0, envPtr);
}
}
src[numChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth = TclMax((cmdWords + 1), maxDepth);
} else {
maxDepth = TclMax((cmdWords + envPtr->maxStackDepth),
maxDepth);
}
src = termPtr;
c = *src;
type = CHAR_TYPE(src, lastChar);
cmdWords++;
}
if (cmdWords > 0) {
if (cmdWords <= 255) {
TclEmitInstUInt1(INST_INVOKE_STK1, cmdWords, envPtr);
} else {
TclEmitInstUInt4(INST_INVOKE_STK4, cmdWords, envPtr);
}
}
finishCommand:
EnterCmdExtentData(envPtr, cmdIndex, src-cmdSrcStart,
(envPtr->codeNext-envPtr->codeStart) - cmdCodeOffset);
isFirstCmd = 0;
envPtr->termOffset = (src - string);
c = *src;
}
done:
if (result == TCL_OK) {
if (entryCodeNext == envPtr->codeNext) {
int objIndex = TclObjIndexForString("", 0, 0,
0, envPtr);
TclEmitPush(objIndex, envPtr);
maxDepth = 1;
}
} else {
register char *p;
int numChars;
char buf[200];
iPtr->errorLine = 1;
for (p = string; p != cmdSrcStart; p++) {
if (*p == '\n') {
iPtr->errorLine++;
}
}
for ( ; isspace(UCHAR(*p)) || (*p == ';'); p++) {
if (*p == '\n') {
iPtr->errorLine++;
}
}
p = cmdSrcStart;
while ((CHAR_TYPE(p, lastChar) != TCL_COMMAND_END)
|| ((*p == ']') && !(flags & TCL_BRACKET_TERM))) {
p++;
}
numChars = (p - cmdSrcStart);
if (numChars > 150) {
numChars = 150;
ellipsis = " ...";
} else if ((numChars >= 2) && (*p == '\n') && (*(p-1) == '{')) {
ellipsis = " ...";
}
sprintf(buf, "\n while compiling\n\"%.*s%s\"",
numChars, cmdSrcStart, ellipsis);
Tcl_AddObjErrorInfo(interp, buf, -1);
}
envPtr->termOffset = (src - string);
iPtr->termOffset = envPtr->termOffset;
envPtr->maxStackDepth = maxDepth;
envPtr->pushSimpleWords = savePushSimpleWords;
return result;
}
static int
CompileWord(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register int type = CHAR_TYPE(src, lastChar);
int maxDepth = 0;
char *termPtr = src;
int result = TCL_OK;
while (type & (TCL_SPACE | TCL_BACKSLASH)) {
if (type == TCL_BACKSLASH) {
if (src[1] == '\n') {
src += 2;
} else {
break;
}
} else {
src++;
}
type = CHAR_TYPE(src, lastChar);
}
if (type == TCL_COMMAND_END) {
goto done;
}
if (type & (TCL_QUOTE | TCL_OPEN_BRACE)) {
src++;
if (type == TCL_QUOTE) {
result = TclCompileQuotes(interp, src, lastChar, '"', flags,
envPtr);
} else {
result = CompileBraces(interp, src, lastChar, flags, envPtr);
}
termPtr = (src + envPtr->termOffset);
if (result != TCL_OK) {
goto done;
}
if ((*termPtr == '\\') && (*(termPtr+1) == '\n')) {
} else {
type = CHAR_TYPE(termPtr, lastChar);
if (!(type & (TCL_SPACE | TCL_COMMAND_END))) {
Tcl_ResetResult(interp);
if (*(src-1) == '"') {
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"extra characters after close-quote", -1);
} else {
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"extra characters after close-brace", -1);
}
result = TCL_ERROR;
goto done;
}
}
maxDepth = envPtr->maxStackDepth;
} else {
result = CompileMultipartWord(interp, src, lastChar, flags, envPtr);
termPtr = (src + envPtr->termOffset);
maxDepth = envPtr->maxStackDepth;
}
done:
envPtr->termOffset = (termPtr - string);
envPtr->maxStackDepth = maxDepth;
return result;
}
static int
CompileMultipartWord(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register char c = *src;
register int type;
int bracketNormal = !(flags & TCL_BRACKET_TERM);
int simpleWord = 0;
int numParts = 0;
int maxDepth = 0;
char *start;
int hasBackslash;
int numChars;
char savedChar;
int objIndex;
int savePushSimpleWords = envPtr->pushSimpleWords;
int result = TCL_OK;
int numRead;
type = CHAR_TYPE(src, lastChar);
while (1) {
if ((type & (TCL_NORMAL | TCL_CLOSE_BRACE | TCL_BACKSLASH |
TCL_QUOTE | TCL_OPEN_BRACE)) ||
((c == ']') && bracketNormal)) {
start = src;
hasBackslash = 0;
do {
if (type == TCL_BACKSLASH) {
hasBackslash = 1;
Tcl_Backslash(src, &numRead);
if (src[1] == '\n') {
src += numRead;
type = TCL_SPACE;
break;
}
src += numRead;
} else {
src++;
}
c = *src;
type = CHAR_TYPE(src, lastChar);
} while (type & (TCL_NORMAL | TCL_BACKSLASH | TCL_QUOTE |
TCL_OPEN_BRACE | TCL_CLOSE_BRACE)
|| ((c == ']') && bracketNormal));
if ((numParts == 0) && !hasBackslash
&& (type & (TCL_SPACE | TCL_COMMAND_END))) {
simpleWord = 1;
if (!envPtr->pushSimpleWords) {
envPtr->wordIsSimple = 1;
envPtr->numSimpleWordChars = (src - string);
envPtr->termOffset = envPtr->numSimpleWordChars;
envPtr->pushSimpleWords = savePushSimpleWords;
return TCL_OK;
}
}
numChars = (src - start);
savedChar = start[numChars];
start[numChars] = '\0';
if ((numChars > 0) && (hasBackslash)) {
char *buffer = ckalloc((unsigned) numChars + 1);
register char *dst = buffer;
register char *p = start;
while (p < src) {
if (*p == '\\') {
*dst = Tcl_Backslash(p, &numRead);
if (p[1] == '\n') {
break;
}
p += numRead;
dst++;
} else {
*dst++ = *p++;
}
}
*dst = '\0';
objIndex = TclObjIndexForString(buffer, dst-buffer,
1, 1, envPtr);
} else {
objIndex = TclObjIndexForString(start, numChars,
1, 0, envPtr);
}
start[numChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth = TclMax((numParts + 1), maxDepth);
} else if (type == TCL_DOLLAR) {
result = TclCompileDollarVar(interp, src, lastChar,
flags, envPtr);
src += envPtr->termOffset;
if (result != TCL_OK) {
goto done;
}
maxDepth = TclMax((numParts + envPtr->maxStackDepth), maxDepth);
c = *src;
type = CHAR_TYPE(src, lastChar);
} else if (type == TCL_OPEN_BRACKET) {
char *termPtr;
envPtr->pushSimpleWords = 1;
src++;
result = TclCompileString(interp, src, lastChar,
(flags | TCL_BRACKET_TERM), envPtr);
termPtr = (src + envPtr->termOffset);
if (*termPtr == ']') {
termPtr++;
} else if (*termPtr == '\0') {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"missing close-bracket", -1);
result = TCL_ERROR;
}
src = termPtr;
if (result != TCL_OK) {
goto done;
}
maxDepth = TclMax((numParts + envPtr->maxStackDepth), maxDepth);
c = *src;
type = CHAR_TYPE(src, lastChar);
} else if (type & (TCL_SPACE | TCL_COMMAND_END)) {
goto wordEnd;
}
numParts++;
}
wordEnd:
while (numParts > 255) {
TclEmitInstUInt1(INST_CONCAT1, 255, envPtr);
numParts -= 254;
}
if (numParts > 1) {
TclEmitInstUInt1(INST_CONCAT1, numParts, envPtr);
}
done:
if (simpleWord) {
envPtr->wordIsSimple = 1;
envPtr->numSimpleWordChars = (src - string);
} else {
envPtr->wordIsSimple = 0;
envPtr->numSimpleWordChars = 0;
}
envPtr->termOffset = (src - string);
envPtr->maxStackDepth = maxDepth;
envPtr->pushSimpleWords = savePushSimpleWords;
return result;
}
int
TclCompileQuotes(interp, string, lastChar, termChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int termChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register char c = *src;
int simpleWord = 0;
char *start;
int hasBackslash;
int numRead;
int numParts = 0;
int maxDepth = 0;
char savedChar;
int objIndex;
int numChars;
int savePushSimpleWords = envPtr->pushSimpleWords;
int result = TCL_OK;
while ((src != lastChar) && (c != termChar)) {
if (c == '$') {
result = TclCompileDollarVar(interp, src, lastChar, flags,
envPtr);
src += envPtr->termOffset;
if (result != TCL_OK) {
goto done;
}
maxDepth = TclMax((numParts + envPtr->maxStackDepth), maxDepth);
c = *src;
} else if (c == '[') {
char *termPtr;
envPtr->pushSimpleWords = 1;
src++;
result = TclCompileString(interp, src, lastChar,
(flags | TCL_BRACKET_TERM), envPtr);
termPtr = (src + envPtr->termOffset);
if (*termPtr == ']') {
termPtr++;
}
src = termPtr;
if (result != TCL_OK) {
goto done;
}
if (termPtr == lastChar) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"missing close-bracket", -1);
result = TCL_ERROR;
goto done;
}
maxDepth = TclMax((numParts + envPtr->maxStackDepth), maxDepth);
c = *src;
} else {
start = src;
hasBackslash = 0;
do {
if (c == '\\') {
hasBackslash = 1;
Tcl_Backslash(src, &numRead);
src += numRead;
} else {
src++;
}
c = *src;
} while ((src != lastChar) && (c != '$') && (c != '[')
&& (c != termChar));
if ((numParts == 0) && !hasBackslash
&& ((src == lastChar) && (c == termChar))) {
simpleWord = 1;
if (!envPtr->pushSimpleWords) {
if ((src == lastChar) && (termChar != '\0')) {
char buf[40];
sprintf(buf, "missing %c", termChar);
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
result = TCL_ERROR;
} else {
src++;
}
envPtr->wordIsSimple = 1;
envPtr->numSimpleWordChars = (src - string - 1);
envPtr->termOffset = (src - string);
envPtr->pushSimpleWords = savePushSimpleWords;
return result;
}
}
numChars = (src - start);
savedChar = start[numChars];
start[numChars] = '\0';
if ((numChars > 0) && (hasBackslash)) {
char *buffer = ckalloc((unsigned) numChars + 1);
register char *dst = buffer;
register char *p = start;
while (p < src) {
if (*p == '\\') {
*dst++ = Tcl_Backslash(p, &numRead);
p += numRead;
} else {
*dst++ = *p++;
}
}
*dst = '\0';
objIndex = TclObjIndexForString(buffer, (dst - buffer),
1, 1, envPtr);
} else {
objIndex = TclObjIndexForString(start, numChars,
1, 0, envPtr);
}
start[numChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth = TclMax((numParts + 1), maxDepth);
}
numParts++;
}
if ((src == lastChar) && (termChar != '\0')) {
char buf[40];
sprintf(buf, "missing %c", termChar);
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
result = TCL_ERROR;
goto done;
} else {
src++;
}
if (numParts == 0) {
int objIndex = TclObjIndexForString("", 0, 0,
0, envPtr);
TclEmitPush(objIndex, envPtr);
} else {
while (numParts > 255) {
TclEmitInstUInt1(INST_CONCAT1, 255, envPtr);
numParts -= 254;
}
if (numParts > 1) {
TclEmitInstUInt1(INST_CONCAT1, numParts, envPtr);
}
}
done:
if (simpleWord) {
envPtr->wordIsSimple = 1;
envPtr->numSimpleWordChars = (src - string - 1);
} else {
envPtr->wordIsSimple = 0;
envPtr->numSimpleWordChars = 0;
}
envPtr->termOffset = (src - string);
envPtr->maxStackDepth = maxDepth;
envPtr->pushSimpleWords = savePushSimpleWords;
return result;
}
static int
CompileBraces(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register char c;
int simpleWord = 0;
int level = 1;
int hasBackslashNewline = 0;
char *last;
int numChars;
char savedChar;
int objIndex;
int numRead;
int result = TCL_OK;
while (1) {
c = *src;
if (src == lastChar) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"missing close-brace", -1);
result = TCL_ERROR;
goto done;
}
if (CHAR_TYPE(src, lastChar) != TCL_NORMAL) {
if (c == '{') {
level++;
} else if (c == '}') {
--level;
if (level == 0) {
src++;
last = (src - 2);
break;
}
} else if (c == '\\') {
if (*(src+1) == '\n') {
hasBackslashNewline = 1;
}
(void) Tcl_Backslash(src, &numRead);
src += numRead - 1;
}
}
src++;
}
if (!hasBackslashNewline) {
simpleWord = 1;
if (!envPtr->pushSimpleWords) {
envPtr->wordIsSimple = 1;
envPtr->numSimpleWordChars = (src - string - 1);
envPtr->termOffset = (src - string);
return TCL_OK;
}
}
numChars = (last - string + 1);
savedChar = string[numChars];
string[numChars] = '\0';
if ((numChars > 0) && (hasBackslashNewline)) {
char *buffer = ckalloc((unsigned) numChars + 1);
register char *dst = buffer;
register char *p = string;
while (p <= last) {
c = *dst++ = *p++;
if (c == '\\') {
if (*p == '\n') {
dst[-1] = Tcl_Backslash(p-1, &numRead);
p += numRead - 1;
} else {
(void) Tcl_Backslash(p-1, &numRead);
while (numRead > 1) {
*dst++ = *p++;
numRead--;
}
}
}
}
*dst = '\0';
objIndex = TclObjIndexForString(buffer, (dst - buffer),
1, 1, envPtr);
} else {
objIndex = TclObjIndexForString(string, numChars, 1,
0, envPtr);
}
string[numChars] = savedChar;
TclEmitPush(objIndex, envPtr);
done:
if (simpleWord) {
envPtr->wordIsSimple = 1;
envPtr->numSimpleWordChars = (src - string - 1);
} else {
envPtr->wordIsSimple = 0;
envPtr->numSimpleWordChars = 0;
}
envPtr->termOffset = (src - string);
envPtr->maxStackDepth = 1;
return result;
}
int
TclCompileDollarVar(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register char c;
char *name;
int nameChars;
int nameHasNsSeparators = 0;
char savedChar;
int objIndex;
int isArrayRef = 0;
int localIndex = -1;
int maxDepth = 0;
int savePushSimpleWords = envPtr->pushSimpleWords;
int result = TCL_OK;
src++;
if (*src == '{') {
char *p;
src++;
name = src;
c = *src;
while (c != '}') {
if (src == lastChar) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"missing close-brace for variable name", -1);
result = TCL_ERROR;
goto done;
}
src++;
c = *src;
}
nameChars = (src - name);
for (p = name; p < src; p++) {
if ((*p == ':') && (*(p+1) == ':')) {
nameHasNsSeparators = 1;
break;
}
}
src++;
} else {
name = src;
c = *src;
while (isalnum(UCHAR(c)) || (c == '_') || (c == ':')) {
if (c == ':') {
if (*(src+1) == ':') {
nameHasNsSeparators = 1;
src += 2;
while (*src == ':') {
src++;
}
c = *src;
} else {
break;
}
} else {
src++;
c = *src;
}
}
if (src == name) {
objIndex = TclObjIndexForString("$", 1, 1,
0, envPtr);
TclEmitPush(objIndex, envPtr);
maxDepth = 1;
goto done;
}
nameChars = (src - name);
isArrayRef = (c == '(');
}
if (!isArrayRef) {
if ((envPtr->procPtr == NULL) || nameHasNsSeparators) {
savedChar = name[nameChars];
name[nameChars] = '\0';
objIndex = TclObjIndexForString(name, nameChars,
1, 0, envPtr);
name[nameChars] = savedChar;
TclEmitPush(objIndex, envPtr);
TclEmitOpcode(INST_LOAD_SCALAR_STK, envPtr);
maxDepth = 1;
} else {
localIndex = LookupCompiledLocal(name, nameChars,
0, 0,
envPtr->procPtr);
if (localIndex >= 0) {
if (localIndex <= 255) {
TclEmitInstUInt1(INST_LOAD_SCALAR1, localIndex, envPtr);
} else {
TclEmitInstUInt4(INST_LOAD_SCALAR4, localIndex, envPtr);
}
maxDepth = 0;
} else {
savedChar = name[nameChars];
name[nameChars] = '\0';
objIndex = TclObjIndexForString(name, nameChars,
1, 0, envPtr);
name[nameChars] = savedChar;
TclEmitPush(objIndex, envPtr);
TclEmitOpcode(INST_LOAD_SCALAR_STK, envPtr);
maxDepth = 1;
}
}
} else {
if ((envPtr->procPtr == NULL) || nameHasNsSeparators) {
savedChar = name[nameChars];
name[nameChars] = '\0';
objIndex = TclObjIndexForString(name, nameChars,
1, 0, envPtr);
name[nameChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth = 1;
} else {
localIndex = LookupCompiledLocal(name, nameChars,
0, 0,
envPtr->procPtr);
if (localIndex < 0) {
savedChar = name[nameChars];
name[nameChars] = '\0';
objIndex = TclObjIndexForString(name, nameChars,
1, 0, envPtr);
name[nameChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth = 1;
}
}
src++;
envPtr->pushSimpleWords = 1;
result = TclCompileQuotes(interp, src, lastChar, ')', flags,
envPtr);
src += envPtr->termOffset;
if (result != TCL_OK) {
char msg[200];
sprintf(msg, "\n (parsing index for array \"%.*s\")",
(nameChars > 100? 100 : nameChars), name);
Tcl_AddObjErrorInfo(interp, msg, -1);
goto done;
}
maxDepth += envPtr->maxStackDepth;
if (localIndex < 0) {
TclEmitOpcode(INST_LOAD_ARRAY_STK, envPtr);
} else {
if (localIndex <= 255) {
TclEmitInstUInt1(INST_LOAD_ARRAY1, localIndex, envPtr);
} else {
TclEmitInstUInt4(INST_LOAD_ARRAY4, localIndex, envPtr);
}
}
}
done:
envPtr->termOffset = (src - string);
envPtr->wordIsSimple = 0;
envPtr->numSimpleWordChars = 0;
envPtr->maxStackDepth = maxDepth;
envPtr->pushSimpleWords = savePushSimpleWords;
return result;
}
static int
IsLocalScalar(varName, length)
char *varName;
int length;
{
char *p;
char *lastChar = varName + (length - 1);
for (p = varName; p <= lastChar; p++) {
if ((CHAR_TYPE(p, lastChar) != TCL_NORMAL) &&
(CHAR_TYPE(p, lastChar) != TCL_COMMAND_END)) {
return 0;
}
if (*p == '(') {
if (*lastChar == ')') {
return 0;
}
} else if (*p == ':') {
if ((p != lastChar) && *(p+1) == ':') {
return 0;
}
}
}
return 1;
}
int
TclCompileBreakCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register int type;
int result = TCL_OK;
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: should be \"break\"", -1);
result = TCL_ERROR;
goto done;
}
}
TclEmitOpcode(INST_BREAK, envPtr);
done:
envPtr->termOffset = (src - string);
envPtr->maxStackDepth = 0;
return result;
}
int
TclCompileCatchCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
Proc *procPtr = envPtr->procPtr;
int maxDepth = 0;
ArgInfo argInfo;
int range = -1;
char *name;
int nameChars;
int localIndex = -1;
char savedChar;
JumpFixup jumpFixup;
int numWords, objIndex, jumpDist, result;
char *bodyStart, *bodyEnd;
Tcl_Obj *objPtr;
int savePushSimpleWords = envPtr->pushSimpleWords;
InitArgInfo(&argInfo);
result = CollectArgInfo(interp, string, lastChar, flags, &argInfo);
numWords = argInfo.numArgs;
if (result != TCL_OK) {
goto done;
}
if ((numWords != 1) && (numWords != 2)) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: should be \"catch command ?varName?\"", -1);
result = TCL_ERROR;
goto done;
}
if ((numWords == 2) && (procPtr == NULL)) {
result = TCL_OUT_LINE_COMPILE;
goto done;
}
if (numWords == 2) {
char *firstChar = argInfo.startArray[1];
char *lastChar = argInfo.endArray[1];
if (*firstChar == '{') {
if (*lastChar != '}') {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"extra characters after close-brace", -1);
result = TCL_ERROR;
goto done;
}
firstChar++;
lastChar--;
}
nameChars = (lastChar - firstChar + 1);
if (!IsLocalScalar(firstChar, nameChars)) {
result = TCL_OUT_LINE_COMPILE;
goto done;
}
name = firstChar;
localIndex = LookupCompiledLocal(name, nameChars,
1, VAR_SCALAR,
procPtr);
}
envPtr->excRangeDepth++;
envPtr->maxExcRangeDepth =
TclMax(envPtr->excRangeDepth, envPtr->maxExcRangeDepth);
range = CreateExceptionRange(CATCH_EXCEPTION_RANGE, envPtr);
TclEmitInstUInt4(INST_BEGIN_CATCH4, range, envPtr);
envPtr->excRangeArrayPtr[range].codeOffset = TclCurrCodeOffset();
bodyStart = argInfo.startArray[0];
bodyEnd = argInfo.endArray[0];
savedChar = *(bodyEnd+1);
*(bodyEnd+1) = '\0';
result = CompileCmdWordInline(interp, bodyStart, (bodyEnd+1),
flags, envPtr);
*(bodyEnd+1) = savedChar;
if (result != TCL_OK) {
if (result == TCL_ERROR) {
char msg[60];
sprintf(msg, "\n (\"catch\" body line %d)",
interp->errorLine);
Tcl_AddObjErrorInfo(interp, msg, -1);
}
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
envPtr->excRangeArrayPtr[range].numCodeBytes =
TclCurrCodeOffset() - envPtr->excRangeArrayPtr[range].codeOffset;
if (localIndex != -1) {
if (localIndex <= 255) {
TclEmitInstUInt1(INST_STORE_SCALAR1, localIndex, envPtr);
} else {
TclEmitInstUInt4(INST_STORE_SCALAR4, localIndex, envPtr);
}
}
TclEmitOpcode(INST_POP, envPtr);
objIndex = TclObjIndexForString("0", 1, 0, 0,
envPtr);
objPtr = envPtr->objArrayPtr[objIndex];
Tcl_InvalidateStringRep(objPtr);
objPtr->internalRep.longValue = 0;
objPtr->typePtr = &tclIntType;
TclEmitPush(objIndex, envPtr);
if (maxDepth == 0) {
maxDepth = 1;
}
TclEmitForwardJump(envPtr, TCL_UNCONDITIONAL_JUMP, &jumpFixup);
envPtr->excRangeArrayPtr[range].catchOffset = TclCurrCodeOffset();
if (localIndex != -1) {
TclEmitOpcode(INST_PUSH_RESULT, envPtr);
if (localIndex <= 255) {
TclEmitInstUInt1(INST_STORE_SCALAR1, localIndex, envPtr);
} else {
TclEmitInstUInt4(INST_STORE_SCALAR4, localIndex, envPtr);
}
TclEmitOpcode(INST_POP, envPtr);
}
TclEmitOpcode(INST_PUSH_RETURN_CODE, envPtr);
jumpDist = (TclCurrCodeOffset() - jumpFixup.codeOffset);
if (TclFixupForwardJump(envPtr, &jumpFixup, jumpDist, 127)) {
panic("TclCompileCatchCmd: bad jump distance %d\n", jumpDist);
}
TclEmitOpcode(INST_END_CATCH, envPtr);
done:
if (numWords == 0) {
envPtr->termOffset = 0;
} else {
envPtr->termOffset = (argInfo.endArray[numWords-1] + 1 - string);
}
if (range != -1) {
envPtr->excRangeDepth--;
}
envPtr->pushSimpleWords = savePushSimpleWords;
envPtr->maxStackDepth = maxDepth;
FreeArgInfo(&argInfo);
return result;
}
int
TclCompileContinueCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register int type;
int result = TCL_OK;
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: should be \"continue\"", -1);
result = TCL_ERROR;
goto done;
}
}
TclEmitOpcode(INST_CONTINUE, envPtr);
done:
envPtr->termOffset = (src - string);
envPtr->maxStackDepth = 0;
return result;
}
int
TclCompileExprCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
int maxDepth = 0;
ArgInfo argInfo;
Tcl_DString buffer;
int firstWord;
char *first, *last;
int inlineCode;
int range = -1;
JumpFixup jumpFixup;
char savedChar;
int numWords, objIndex, i, result;
char *wordStart, *wordEnd, *p;
char c;
int savePushSimpleWords = envPtr->pushSimpleWords;
int saveExprIsJustVarRef = envPtr->exprIsJustVarRef;
int saveExprIsComparison = envPtr->exprIsComparison;
InitArgInfo(&argInfo);
result = CollectArgInfo(interp, string, lastChar, flags, &argInfo);
numWords = argInfo.numArgs;
if (result != TCL_OK) {
goto done;
}
if (numWords == 0) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: should be \"expr arg ?arg ...?\"", -1);
result = TCL_ERROR;
goto done;
}
if (numWords == 1) {
wordStart = argInfo.startArray[0];
wordEnd = argInfo.endArray[0];
if ((*wordStart == '{') && (*wordEnd == '}')) {
*wordEnd = '\0';
result = TclCompileExpr(interp, (wordStart + 1), wordEnd,
flags, envPtr);
*wordEnd = '}';
envPtr->termOffset = (wordEnd + 1) - string;
envPtr->pushSimpleWords = savePushSimpleWords;
FreeArgInfo(&argInfo);
return result;
}
}
Tcl_DStringInit(&buffer);
firstWord = 1;
for (i = 0; i < numWords; i++) {
wordStart = argInfo.startArray[i];
wordEnd = argInfo.endArray[i];
if (((*wordStart == '{') && (*wordEnd == '}'))
|| ((*wordStart == '"') && (*wordEnd == '"'))) {
wordStart++;
wordEnd--;
}
if (!firstWord) {
Tcl_DStringAppend(&buffer, " ", 1);
}
firstWord = 0;
if (wordEnd >= wordStart) {
Tcl_DStringAppend(&buffer, wordStart, (wordEnd-wordStart+1));
}
}
inlineCode = 1;
first = Tcl_DStringValue(&buffer);
last = first + (Tcl_DStringLength(&buffer) - 1);
for (p = first; p <= last; p++) {
c = *p;
if ((c == '[') || (c == '\\')) {
inlineCode = 0;
break;
}
}
if (inlineCode) {
int startCodeOffset = (envPtr->codeNext - envPtr->codeStart);
int startRangeNext = envPtr->excRangeArrayNext;
envPtr->excRangeDepth++;
envPtr->maxExcRangeDepth =
TclMax(envPtr->excRangeDepth, envPtr->maxExcRangeDepth);
range = CreateExceptionRange(CATCH_EXCEPTION_RANGE, envPtr);
TclEmitInstUInt4(INST_BEGIN_CATCH4, range, envPtr);
envPtr->excRangeArrayPtr[range].codeOffset = TclCurrCodeOffset();
savedChar = *(last + 1);
*(last + 1) = '\0';
result = TclCompileExpr(interp, first, last + 1, flags, envPtr);
*(last + 1) = savedChar;
maxDepth = envPtr->maxStackDepth;
envPtr->excRangeArrayPtr[range].numCodeBytes =
TclCurrCodeOffset() - envPtr->excRangeArrayPtr[range].codeOffset;
if ((result != TCL_OK) || (envPtr->exprIsJustVarRef)
|| (envPtr->exprIsComparison)) {
envPtr->codeNext = (envPtr->codeStart + startCodeOffset);
envPtr->excRangeArrayNext = startRangeNext;
inlineCode = 0;
} else {
TclEmitOpcode(INST_END_CATCH, envPtr);
TclEmitForwardJump(envPtr, TCL_UNCONDITIONAL_JUMP, &jumpFixup);
envPtr->excRangeArrayPtr[range].catchOffset = TclCurrCodeOffset();
TclEmitOpcode(INST_END_CATCH, envPtr);
}
}
for (i = 0; i < numWords; i++) {
wordStart = argInfo.startArray[i];
wordEnd = argInfo.endArray[i];
savedChar = *(wordEnd + 1);
*(wordEnd + 1) = '\0';
envPtr->pushSimpleWords = 1;
result = CompileWord(interp, wordStart, wordEnd+1, flags, envPtr);
*(wordEnd + 1) = savedChar;
if (result != TCL_OK) {
break;
}
if (i != (numWords - 1)) {
objIndex = TclObjIndexForString(" ", 1, 1,
0, envPtr);
TclEmitPush(objIndex, envPtr);
maxDepth = TclMax((envPtr->maxStackDepth + 1), maxDepth);
} else {
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
}
}
if (result == TCL_OK) {
int concatItems = 2*numWords - 1;
while (concatItems > 255) {
TclEmitInstUInt1(INST_CONCAT1, 255, envPtr);
concatItems -= 254;
}
if (concatItems > 1) {
TclEmitInstUInt1(INST_CONCAT1, concatItems, envPtr);
}
TclEmitOpcode(INST_EXPR_STK, envPtr);
}
if (inlineCode) {
int jumpDist = (TclCurrCodeOffset() - jumpFixup.codeOffset);
if (TclFixupForwardJump(envPtr, &jumpFixup, jumpDist, 127)) {
envPtr->excRangeArrayPtr[range].catchOffset += 3;
}
}
Tcl_DStringFree(&buffer);
done:
if (numWords == 0) {
envPtr->termOffset = 0;
} else {
envPtr->termOffset = (argInfo.endArray[numWords-1] + 1 - string);
}
if (range != -1) {
envPtr->excRangeDepth--;
}
envPtr->pushSimpleWords = savePushSimpleWords;
envPtr->exprIsJustVarRef = saveExprIsJustVarRef;
envPtr->exprIsComparison = saveExprIsComparison;
envPtr->maxStackDepth = maxDepth;
FreeArgInfo(&argInfo);
return result;
}
int
TclCompileForCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
int maxDepth = 0;
ArgInfo argInfo;
int range1 = -1, range2;
JumpFixup jumpFalseFixup;
int jumpBackDist, jumpBackOffset, testCodeOffset, jumpDist, objIndex;
unsigned char *jumpPc;
int savePushSimpleWords = envPtr->pushSimpleWords;
int numWords, result;
InitArgInfo(&argInfo);
result = CollectArgInfo(interp, string, lastChar, flags, &argInfo);
numWords = argInfo.numArgs;
if (result != TCL_OK) {
goto done;
}
if (numWords != 4) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: should be \"for start test next command\"", -1);
result = TCL_ERROR;
goto done;
}
if (*(argInfo.startArray[1]) != '{') {
result = TCL_OUT_LINE_COMPILE;
goto done;
}
envPtr->excRangeDepth++;
envPtr->maxExcRangeDepth =
TclMax(envPtr->excRangeDepth, envPtr->maxExcRangeDepth);
range1 = CreateExceptionRange(LOOP_EXCEPTION_RANGE, envPtr);
range2 = CreateExceptionRange(LOOP_EXCEPTION_RANGE, envPtr);
result = CompileCmdWordInline(interp, argInfo.startArray[0],
(argInfo.endArray[0] + 1), flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
Tcl_AddObjErrorInfo(interp, "\n (\"for\" initial command)", -1);
}
goto done;
}
maxDepth = envPtr->maxStackDepth;
TclEmitOpcode(INST_POP, envPtr);
testCodeOffset = TclCurrCodeOffset();
envPtr->pushSimpleWords = 1;
result = CompileExprWord(interp, argInfo.startArray[1],
(argInfo.endArray[1] + 1), flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
Tcl_AddObjErrorInfo(interp, "\n (\"for\" test expression)", -1);
}
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
TclEmitForwardJump(envPtr, TCL_FALSE_JUMP, &jumpFalseFixup);
envPtr->excRangeArrayPtr[range1].codeOffset = TclCurrCodeOffset();
result = CompileCmdWordInline(interp, argInfo.startArray[3],
(argInfo.endArray[3] + 1), flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
char msg[60];
sprintf(msg, "\n (\"for\" body line %d)", interp->errorLine);
Tcl_AddObjErrorInfo(interp, msg, -1);
}
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
envPtr->excRangeArrayPtr[range1].numCodeBytes =
(TclCurrCodeOffset() - envPtr->excRangeArrayPtr[range1].codeOffset);
TclEmitOpcode(INST_POP, envPtr);
envPtr->excRangeArrayPtr[range1].continueOffset = TclCurrCodeOffset();
envPtr->excRangeArrayPtr[range2].codeOffset = TclCurrCodeOffset();
result = CompileCmdWordInline(interp, argInfo.startArray[2],
(argInfo.endArray[2] + 1), flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
Tcl_AddObjErrorInfo(interp, "\n (\"for\" loop-end command)", -1);
}
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
envPtr->excRangeArrayPtr[range2].numCodeBytes =
TclCurrCodeOffset() - envPtr->excRangeArrayPtr[range2].codeOffset;
TclEmitOpcode(INST_POP, envPtr);
jumpBackOffset = TclCurrCodeOffset();
jumpBackDist = (jumpBackOffset - testCodeOffset);
if (jumpBackDist > 120) {
TclEmitInstInt4(INST_JUMP4, -jumpBackDist, envPtr);
} else {
TclEmitInstInt1(INST_JUMP1, -jumpBackDist, envPtr);
}
jumpDist = (TclCurrCodeOffset() - jumpFalseFixup.codeOffset);
if (TclFixupForwardJump(envPtr, &jumpFalseFixup, jumpDist, 127)) {
envPtr->excRangeArrayPtr[range1].codeOffset += 3;
envPtr->excRangeArrayPtr[range1].continueOffset += 3;
envPtr->excRangeArrayPtr[range2].codeOffset += 3;
jumpBackOffset += 3;
jumpPc = (envPtr->codeStart + jumpBackOffset);
if (jumpBackDist > 120) {
jumpBackDist += 3;
TclUpdateInstInt4AtPc(INST_JUMP4, -jumpBackDist,
jumpPc);
} else {
jumpBackDist += 3;
TclUpdateInstInt1AtPc(INST_JUMP1, -jumpBackDist,
jumpPc);
}
}
envPtr->excRangeArrayPtr[range1].breakOffset =
envPtr->excRangeArrayPtr[range2].breakOffset = TclCurrCodeOffset();
objIndex = TclObjIndexForString("", 0, 0, 0,
envPtr);
TclEmitPush(objIndex, envPtr);
if (maxDepth == 0) {
maxDepth = 1;
}
done:
if (numWords == 0) {
envPtr->termOffset = 0;
} else {
envPtr->termOffset = (argInfo.endArray[numWords-1] + 1 - string);
}
envPtr->pushSimpleWords = savePushSimpleWords;
envPtr->maxStackDepth = maxDepth;
if (range1 != -1) {
envPtr->excRangeDepth--;
}
FreeArgInfo(&argInfo);
return result;
}
int
TclCompileForeachCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
Proc *procPtr = envPtr->procPtr;
int maxDepth = 0;
ArgInfo argInfo;
int numLists = 0;
int range = -1;
ForeachInfo *infoPtr;
JumpFixup jumpFalseFixup;
char savedChar;
int firstListTmp = -1;
int loopIterNumTmp;
char *varListStart, *varListEnd, *valueListStart, *bodyStart, *bodyEnd;
unsigned char *jumpPc;
int jumpDist, jumpBackDist, jumpBackOffset;
int numWords, numVars, infoIndex, tmpIndex, objIndex, i, j, result;
int savePushSimpleWords = envPtr->pushSimpleWords;
#define STATIC_VAR_LIST_SIZE 4
int varcListStaticSpace[STATIC_VAR_LIST_SIZE];
char **varvListStaticSpace[STATIC_VAR_LIST_SIZE];
int *varcList = varcListStaticSpace;
char ***varvList = varvListStaticSpace;
if (procPtr == NULL) {
return TCL_OUT_LINE_COMPILE;
}
InitArgInfo(&argInfo);
result = CollectArgInfo(interp, string, lastChar, flags, &argInfo);
numWords = argInfo.numArgs;
if (result != TCL_OK) {
goto done;
}
if ((numWords < 3) || (numWords%2 != 1)) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: should be \"foreach varList list ?varList list ...? command\"", -1);
result = TCL_ERROR;
goto done;
}
numLists = (numWords - 1)/2;
if (numLists > STATIC_VAR_LIST_SIZE) {
varcList = (int *) ckalloc(numLists * sizeof(int));
varvList = (char ***) ckalloc(numLists * sizeof(char **));
}
for (i = 0; i < numLists; i++) {
varcList[i] = 0;
varvList[i] = (char **) NULL;
}
for (i = 0; i < numLists; i++) {
varListStart = argInfo.startArray[i*2];
varListEnd = argInfo.endArray[i*2];
if ((*varListStart == '{') || (*varListStart == '"')) {
if ((*varListEnd != '}') && (*varListEnd != '"')) {
Tcl_ResetResult(interp);
if (*varListStart == '"') {
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"extra characters after close-quote", -1);
} else {
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"extra characters after close-brace", -1);
}
result = TCL_ERROR;
goto done;
}
varListStart++;
varListEnd--;
}
savedChar = *(varListEnd+1);
*(varListEnd+1) = '\0';
result = Tcl_SplitList(interp, varListStart,
&varcList[i], &varvList[i]);
*(varListEnd+1) = savedChar;
if (result != TCL_OK) {
goto done;
}
numVars = varcList[i];
for (j = 0; j < numVars; j++) {
char *varName = varvList[i][j];
if (!IsLocalScalar(varName, (int) strlen(varName))) {
result = TCL_OUT_LINE_COMPILE;
goto done;
}
}
}
envPtr->excRangeDepth++;
envPtr->maxExcRangeDepth =
TclMax(envPtr->excRangeDepth, envPtr->maxExcRangeDepth);
range = CreateExceptionRange(LOOP_EXCEPTION_RANGE, envPtr);
for (i = 0; i < numLists; i++) {
tmpIndex = LookupCompiledLocal(NULL, 0,
1, VAR_SCALAR, procPtr);
if (i == 0) {
firstListTmp = tmpIndex;
}
}
loopIterNumTmp = LookupCompiledLocal(NULL, 0,
1, VAR_SCALAR, procPtr);
infoPtr = (ForeachInfo *) ckalloc((unsigned)
(sizeof(ForeachInfo) + (numLists * sizeof(ForeachVarList *))));
infoPtr->numLists = numLists;
infoPtr->firstListTmp = firstListTmp;
infoPtr->loopIterNumTmp = loopIterNumTmp;
for (i = 0; i < numLists; i++) {
ForeachVarList *varListPtr;
numVars = varcList[i];
varListPtr = (ForeachVarList *) ckalloc((unsigned)
sizeof(ForeachVarList) + numVars*sizeof(int));
varListPtr->numVars = numVars;
for (j = 0; j < numVars; j++) {
char *varName = varvList[i][j];
int nameChars = strlen(varName);
varListPtr->varIndexes[j] = LookupCompiledLocal(varName,
nameChars, 1,
VAR_SCALAR, procPtr);
}
infoPtr->varLists[i] = varListPtr;
}
infoIndex = TclCreateAuxData((ClientData) infoPtr,
&tclForeachInfoType, envPtr);
for (i = 0; i < numLists; i++) {
valueListStart = argInfo.startArray[2*i + 1];
envPtr->pushSimpleWords = 1;
result = CompileWord(interp, valueListStart, lastChar, flags,
envPtr);
if (result != TCL_OK) {
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
tmpIndex = (firstListTmp + i);
if (tmpIndex <= 255) {
TclEmitInstUInt1(INST_STORE_SCALAR1, tmpIndex, envPtr);
} else {
TclEmitInstUInt4(INST_STORE_SCALAR4, tmpIndex, envPtr);
}
TclEmitOpcode(INST_POP, envPtr);
}
TclEmitInstUInt4(INST_FOREACH_START4, infoIndex, envPtr);
envPtr->excRangeArrayPtr[range].continueOffset = TclCurrCodeOffset();
TclEmitInstUInt4(INST_FOREACH_STEP4, infoIndex, envPtr);
TclEmitForwardJump(envPtr, TCL_FALSE_JUMP, &jumpFalseFixup);
bodyStart = argInfo.startArray[numWords - 1];
bodyEnd = argInfo.endArray[numWords - 1];
savedChar = *(bodyEnd+1);
*(bodyEnd+1) = '\0';
envPtr->excRangeArrayPtr[range].codeOffset = TclCurrCodeOffset();
result = CompileCmdWordInline(interp, bodyStart, bodyEnd+1, flags,
envPtr);
*(bodyEnd+1) = savedChar;
if (result != TCL_OK) {
if (result == TCL_ERROR) {
char msg[60];
sprintf(msg, "\n (\"foreach\" body line %d)",
interp->errorLine);
Tcl_AddObjErrorInfo(interp, msg, -1);
}
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
envPtr->excRangeArrayPtr[range].numCodeBytes =
TclCurrCodeOffset() - envPtr->excRangeArrayPtr[range].codeOffset;
TclEmitOpcode(INST_POP, envPtr);
jumpBackOffset = TclCurrCodeOffset();
jumpBackDist =
(jumpBackOffset - envPtr->excRangeArrayPtr[range].continueOffset);
if (jumpBackDist > 120) {
TclEmitInstInt4(INST_JUMP4, -jumpBackDist, envPtr);
} else {
TclEmitInstInt1(INST_JUMP1, -jumpBackDist, envPtr);
}
jumpDist = (TclCurrCodeOffset() - jumpFalseFixup.codeOffset);
if (TclFixupForwardJump(envPtr, &jumpFalseFixup, jumpDist, 127)) {
envPtr->excRangeArrayPtr[range].codeOffset += 3;
jumpBackOffset += 3;
jumpPc = (envPtr->codeStart + jumpBackOffset);
if (jumpBackDist > 120) {
jumpBackDist += 3;
TclUpdateInstInt4AtPc(INST_JUMP4, -jumpBackDist,
jumpPc);
} else {
jumpBackDist += 3;
TclUpdateInstInt1AtPc(INST_JUMP1, -jumpBackDist,
jumpPc);
}
}
envPtr->excRangeArrayPtr[range].breakOffset = TclCurrCodeOffset();
objIndex = TclObjIndexForString("", 0, 0, 0,
envPtr);
TclEmitPush(objIndex, envPtr);
if (maxDepth == 0) {
maxDepth = 1;
}
done:
for (i = 0; i < numLists; i++) {
if (varvList[i] != (char **) NULL) {
ckfree((char *) varvList[i]);
}
}
if (varcList != varcListStaticSpace) {
ckfree((char *) varcList);
ckfree((char *) varvList);
}
envPtr->termOffset = (argInfo.endArray[numWords-1] + 1 - string);
envPtr->pushSimpleWords = savePushSimpleWords;
envPtr->maxStackDepth = maxDepth;
if (range != -1) {
envPtr->excRangeDepth--;
}
FreeArgInfo(&argInfo);
return result;
}
static ClientData
DupForeachInfo(clientData)
ClientData clientData;
{
register ForeachInfo *srcPtr = (ForeachInfo *) clientData;
ForeachInfo *dupPtr;
register ForeachVarList *srcListPtr, *dupListPtr;
int numLists = srcPtr->numLists;
int numVars, i, j;
dupPtr = (ForeachInfo *) ckalloc((unsigned)
(sizeof(ForeachInfo) + (numLists * sizeof(ForeachVarList *))));
dupPtr->numLists = numLists;
dupPtr->firstListTmp = srcPtr->firstListTmp;
dupPtr->loopIterNumTmp = srcPtr->loopIterNumTmp;
for (i = 0; i < numLists; i++) {
srcListPtr = srcPtr->varLists[i];
numVars = srcListPtr->numVars;
dupListPtr = (ForeachVarList *) ckalloc((unsigned)
sizeof(ForeachVarList) + numVars*sizeof(int));
dupListPtr->numVars = numVars;
for (j = 0; j < numVars; j++) {
dupListPtr->varIndexes[j] = srcListPtr->varIndexes[j];
}
dupPtr->varLists[i] = dupListPtr;
}
return (ClientData) dupPtr;
}
static void
FreeForeachInfo(clientData)
ClientData clientData;
{
register ForeachInfo *infoPtr = (ForeachInfo *) clientData;
register ForeachVarList *listPtr;
int numLists = infoPtr->numLists;
register int i;
for (i = 0; i < numLists; i++) {
listPtr = infoPtr->varLists[i];
ckfree((char *) listPtr);
}
ckfree((char *) infoPtr);
}
int
TclCompileIfCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register int type;
int maxDepth = 0;
JumpFixupArray jumpFalseFixupArray;
JumpFixupArray jumpEndFixupArray;
char *testSrcStart;
int jumpDist, jumpFalseDist, jumpIndex, objIndex, j, result;
unsigned char *ifFalsePc;
unsigned char opCode;
int savePushSimpleWords = envPtr->pushSimpleWords;
TclInitJumpFixupArray(&jumpFalseFixupArray);
TclInitJumpFixupArray(&jumpEndFixupArray);
while (1) {
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type == TCL_COMMAND_END) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: no expression after \"if\" argument", -1);
result = TCL_ERROR;
goto done;
}
testSrcStart = src;
envPtr->pushSimpleWords = 1;
result = CompileExprWord(interp, src, lastChar, flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
Tcl_AddObjErrorInfo(interp,
"\n (\"if\" test expression)", -1);
}
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
src += envPtr->termOffset;
if (jumpFalseFixupArray.next >= jumpFalseFixupArray.end) {
TclExpandJumpFixupArray(&jumpFalseFixupArray);
}
jumpIndex = jumpFalseFixupArray.next;
jumpFalseFixupArray.next++;
TclEmitForwardJump(envPtr, TCL_FALSE_JUMP,
&(jumpFalseFixupArray.fixup[jumpIndex]));
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type == TCL_COMMAND_END) {
char buf[100];
sprintf(buf, "wrong # args: no script following \"%.20s\" argument", testSrcStart);
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
result = TCL_ERROR;
goto done;
}
if ((*src == 't') && (strncmp(src, "then", 4) == 0)) {
type = CHAR_TYPE(src+4, lastChar);
if ((type == TCL_SPACE) || (type == TCL_COMMAND_END)) {
src += 4;
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type == TCL_COMMAND_END) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: no script following \"then\" argument", -1);
result = TCL_ERROR;
goto done;
}
}
}
result = CompileCmdWordInline(interp, src, lastChar, flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
char msg[60];
sprintf(msg, "\n (\"if\" then script line %d)",
interp->errorLine);
Tcl_AddObjErrorInfo(interp, msg, -1);
}
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
src += envPtr->termOffset;
if (jumpEndFixupArray.next >= jumpEndFixupArray.end) {
TclExpandJumpFixupArray(&jumpEndFixupArray);
}
jumpEndFixupArray.next++;
TclEmitForwardJump(envPtr, TCL_UNCONDITIONAL_JUMP,
&(jumpEndFixupArray.fixup[jumpIndex]));
jumpDist = (TclCurrCodeOffset() - jumpFalseFixupArray.fixup[jumpIndex].codeOffset);
if (TclFixupForwardJump(envPtr,
&(jumpFalseFixupArray.fixup[jumpIndex]), jumpDist, 120)) {
jumpEndFixupArray.fixup[jumpIndex].codeOffset += 3;
}
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if ((type != TCL_COMMAND_END)
&& ((*src == 'e') && (strncmp(src, "elseif", 6) == 0))) {
type = CHAR_TYPE(src+6, lastChar);
if ((type == TCL_SPACE) || (type == TCL_COMMAND_END)) {
src += 6;
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type == TCL_COMMAND_END) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: no expression after \"elseif\" argument", -1);
result = TCL_ERROR;
goto done;
}
continue;
}
}
break;
}
if (type != TCL_COMMAND_END) {
if ((*src == 'e') && (strncmp(src, "else", 4) == 0)) {
type = CHAR_TYPE(src+4, lastChar);
if ((type == TCL_SPACE) || (type == TCL_COMMAND_END)) {
src += 4;
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type == TCL_COMMAND_END) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: no script following \"else\" argument", -1);
result = TCL_ERROR;
goto done;
}
}
}
result = CompileCmdWordInline(interp, src, lastChar, flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
char msg[60];
sprintf(msg, "\n (\"if\" else script line %d)",
interp->errorLine);
Tcl_AddObjErrorInfo(interp, msg, -1);
}
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: extra words after \"else\" clause in \"if\" command", -1);
result = TCL_ERROR;
goto done;
}
}
} else {
objIndex = TclObjIndexForString("", 0, 0,
0, envPtr);
TclEmitPush(objIndex, envPtr);
maxDepth = TclMax(1, maxDepth);
}
for (j = jumpEndFixupArray.next; j > 0; j--) {
jumpIndex = (j - 1);
jumpDist = (TclCurrCodeOffset() - jumpEndFixupArray.fixup[jumpIndex].codeOffset);
if (TclFixupForwardJump(envPtr,
&(jumpEndFixupArray.fixup[jumpIndex]), jumpDist, 127)) {
ifFalsePc = (envPtr->codeStart + jumpFalseFixupArray.fixup[jumpIndex].codeOffset);
opCode = *ifFalsePc;
if (opCode == INST_JUMP_FALSE1) {
jumpFalseDist = TclGetInt1AtPtr(ifFalsePc + 1);
jumpFalseDist += 3;
TclStoreInt1AtPtr(jumpFalseDist, (ifFalsePc + 1));
} else if (opCode == INST_JUMP_FALSE4) {
jumpFalseDist = TclGetInt4AtPtr(ifFalsePc + 1);
jumpFalseDist += 3;
TclStoreInt4AtPtr(jumpFalseDist, (ifFalsePc + 1));
} else {
panic("TclCompileIfCmd: unexpected opcode updating ifFalse jump");
}
}
}
done:
TclFreeJumpFixupArray(&jumpFalseFixupArray);
TclFreeJumpFixupArray(&jumpEndFixupArray);
envPtr->termOffset = (src - string);
envPtr->maxStackDepth = maxDepth;
envPtr->pushSimpleWords = savePushSimpleWords;
return result;
}
int
TclCompileIncrCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
Proc *procPtr = envPtr->procPtr;
register char *src = string;
register int type;
int simpleVarName;
char *name = NULL;
char *elName = NULL;
int nameChars = 0;
int elNameChars = 0;
int incrementGiven;
int isImmIncrValue = 0;
int immIncrValue = 0;
int maxDepth = 0;
int localIndex = -1;
char savedChar;
int objIndex;
int savePushSimpleWords = envPtr->pushSimpleWords;
char *p;
int i, result;
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type == TCL_COMMAND_END) {
badArgs:
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: should be \"incr varName ?increment?\"", -1);
result = TCL_ERROR;
goto done;
}
envPtr->pushSimpleWords = 0;
result = CompileWord(interp, src, lastChar, flags, envPtr);
if (result != TCL_OK) {
goto done;
}
simpleVarName = envPtr->wordIsSimple;
if (simpleVarName) {
name = src;
nameChars = envPtr->numSimpleWordChars;
if (type & (TCL_QUOTE | TCL_OPEN_BRACE)) {
name++;
}
elName = NULL;
elNameChars = 0;
p = name;
for (i = 0; i < nameChars; i++) {
if (*p == '(') {
char *openParen = p;
p = (src + nameChars-1);
if (*p == ')') {
nameChars = (openParen - name);
elName = openParen+1;
elNameChars = (p - elName);
}
break;
}
p++;
}
} else {
maxDepth = envPtr->maxStackDepth;
}
src += envPtr->termOffset;
incrementGiven = 0;
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
incrementGiven = (type != TCL_COMMAND_END);
}
if (simpleVarName) {
if (procPtr == NULL) {
savedChar = name[nameChars];
name[nameChars] = '\0';
objIndex = TclObjIndexForString(name, nameChars,
1, 0, envPtr);
name[nameChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth = 1;
} else {
localIndex = LookupCompiledLocal(name, nameChars,
0, 0,
envPtr->procPtr);
if ((localIndex < 0) || (localIndex > 255)) {
if (localIndex > 255) {
localIndex = -1;
}
savedChar = name[nameChars];
name[nameChars] = '\0';
objIndex = TclObjIndexForString(name, nameChars,
1, 0, envPtr);
name[nameChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth = 1;
} else {
maxDepth = 0;
}
}
if (elName != NULL) {
savedChar = elName[elNameChars];
elName[elNameChars] = '\0';
envPtr->pushSimpleWords = 1;
result = TclCompileQuotes(interp, elName, elName+elNameChars,
0, flags, envPtr);
elName[elNameChars] = savedChar;
if (result != TCL_OK) {
char msg[200];
sprintf(msg, "\n (parsing index for array \"%.*s\")",
TclMin(nameChars, 100), name);
Tcl_AddObjErrorInfo(interp, msg, -1);
goto done;
}
maxDepth += envPtr->maxStackDepth;
}
}
if (incrementGiven) {
type = CHAR_TYPE(src, lastChar);
envPtr->pushSimpleWords = 0;
result = CompileWord(interp, src, lastChar, flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
Tcl_AddObjErrorInfo(interp,
"\n (increment expression)", -1);
}
goto done;
}
if (type & (TCL_QUOTE | TCL_OPEN_BRACE)) {
src++;
}
if (envPtr->wordIsSimple) {
int isCompilableInt = 0;
int numChars = envPtr->numSimpleWordChars;
char savedChar = src[numChars];
char buf[40];
Tcl_Obj *objPtr;
long n;
src[numChars] = '\0';
if (TclLooksLikeInt(src)) {
int code = TclGetLong(interp, src, &n);
if (code == TCL_OK) {
if ((-127 <= n) && (n <= 127)) {
isCompilableInt = 1;
isImmIncrValue = 1;
immIncrValue = n;
} else {
TclFormatInt(buf, n);
if (strcmp(src, buf) == 0) {
isCompilableInt = 1;
isImmIncrValue = 0;
objIndex = TclObjIndexForString(src, numChars,
0, 0, envPtr);
objPtr = envPtr->objArrayPtr[objIndex];
Tcl_InvalidateStringRep(objPtr);
objPtr->internalRep.longValue = n;
objPtr->typePtr = &tclIntType;
TclEmitPush(objIndex, envPtr);
maxDepth += 1;
}
}
} else {
Tcl_ResetResult(interp);
}
}
if (!isCompilableInt) {
objIndex = TclObjIndexForString(src, numChars,
1, 0, envPtr);
TclEmitPush(objIndex, envPtr);
maxDepth += 1;
}
src[numChars] = savedChar;
} else {
maxDepth += envPtr->maxStackDepth;
}
if (type & (TCL_QUOTE | TCL_OPEN_BRACE)) {
src += (envPtr->termOffset - 1);
} else {
src += envPtr->termOffset;
}
} else {
isImmIncrValue = 1;
immIncrValue = 1;
}
if (simpleVarName) {
if (elName == NULL) {
if (localIndex >= 0) {
if (isImmIncrValue) {
TclEmitInstUInt1(INST_INCR_SCALAR1_IMM, localIndex,
envPtr);
TclEmitInt1(immIncrValue, envPtr);
} else {
TclEmitInstUInt1(INST_INCR_SCALAR1, localIndex, envPtr);
}
} else {
if (isImmIncrValue) {
TclEmitInstInt1(INST_INCR_SCALAR_STK_IMM, immIncrValue,
envPtr);
} else {
TclEmitOpcode(INST_INCR_SCALAR_STK, envPtr);
}
}
} else {
if (localIndex >= 0) {
if (isImmIncrValue) {
TclEmitInstUInt1(INST_INCR_ARRAY1_IMM, localIndex,
envPtr);
TclEmitInt1(immIncrValue, envPtr);
} else {
TclEmitInstUInt1(INST_INCR_ARRAY1, localIndex, envPtr);
}
} else {
if (isImmIncrValue) {
TclEmitInstInt1(INST_INCR_ARRAY_STK_IMM, immIncrValue,
envPtr);
} else {
TclEmitOpcode(INST_INCR_ARRAY_STK, envPtr);
}
}
}
} else {
if (isImmIncrValue) {
TclEmitInstInt1(INST_INCR_STK_IMM, immIncrValue, envPtr);
} else {
TclEmitOpcode(INST_INCR_STK, envPtr);
}
}
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
goto badArgs;
}
}
done:
envPtr->termOffset = (src - string);
envPtr->maxStackDepth = maxDepth;
envPtr->pushSimpleWords = savePushSimpleWords;
return result;
}
int
TclCompileSetCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
Proc *procPtr = envPtr->procPtr;
ArgInfo argInfo;
int simpleVarName;
char *elName = NULL;
int isAssignment;
int maxDepth = 0;
int localIndex = -1;
char savedChar;
int objIndex = -1;
char *wordStart, *p;
int numWords, isCompilableInt, i, result;
Tcl_Obj *objPtr;
int savePushSimpleWords = envPtr->pushSimpleWords;
InitArgInfo(&argInfo);
result = CollectArgInfo(interp, string, lastChar, flags, &argInfo);
numWords = argInfo.numArgs;
if (result != TCL_OK) {
goto done;
}
if ((numWords < 1) || (numWords > 2)) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: should be \"set varName ?newValue?\"", -1);
result = TCL_ERROR;
goto done;
}
isAssignment = (numWords == 2);
wordStart = argInfo.startArray[0];
if ((*wordStart == '{') || (*wordStart == '"')) {
result = TCL_OUT_LINE_COMPILE;
goto done;
}
envPtr->pushSimpleWords = 0;
result = CompileWord(interp, wordStart, argInfo.endArray[0] + 1,
flags, envPtr);
if (result != TCL_OK) {
goto done;
}
simpleVarName = envPtr->wordIsSimple;
if (!simpleVarName) {
maxDepth = envPtr->maxStackDepth;
} else {
char *name;
int nameChars;
int nameHasNsSeparators = 0;
int elNameChars;
name = wordStart;
nameChars = envPtr->numSimpleWordChars;
elName = NULL;
elNameChars = 0;
p = name;
for (i = 0; i < nameChars; i++) {
if (*p == '(') {
char *openParen = p;
p = (name + nameChars-1);
if (*p == ')') {
nameChars = (openParen - name);
elName = openParen+1;
elNameChars = (p - elName);
}
break;
}
p++;
}
p = name;
for (i = 0; i < nameChars; i++) {
if ((*p == ':') && ((i+1) < nameChars) && (*(p+1) == ':')) {
nameHasNsSeparators = 1;
break;
}
p++;
}
if ((procPtr == NULL) || nameHasNsSeparators) {
savedChar = name[nameChars];
name[nameChars] = '\0';
objIndex = TclObjIndexForString(name, nameChars,
1, 0, envPtr);
name[nameChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth = 1;
} else {
localIndex = LookupCompiledLocal(name, nameChars,
isAssignment,
((elName == NULL)? VAR_SCALAR : VAR_ARRAY),
envPtr->procPtr);
if (localIndex >= 0) {
maxDepth = 0;
} else {
savedChar = name[nameChars];
name[nameChars] = '\0';
objIndex = TclObjIndexForString(name, nameChars,
1, 0, envPtr);
name[nameChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth = 1;
}
}
if (elName != NULL) {
savedChar = elName[elNameChars];
elName[elNameChars] = '\0';
envPtr->pushSimpleWords = 1;
result = TclCompileQuotes(interp, elName, elName+elNameChars,
0, flags, envPtr);
elName[elNameChars] = savedChar;
if (result != TCL_OK) {
char msg[200];
sprintf(msg, "\n (parsing index for array \"%.*s\")",
TclMin(nameChars, 100), name);
Tcl_AddObjErrorInfo(interp, msg, -1);
goto done;
}
maxDepth += envPtr->maxStackDepth;
}
}
if (isAssignment) {
wordStart = argInfo.startArray[1];
envPtr->pushSimpleWords = 0;
result = CompileWord(interp, wordStart, argInfo.endArray[1] + 1,
flags, envPtr);
if (result != TCL_OK) {
goto done;
}
if (!envPtr->wordIsSimple) {
maxDepth += envPtr->maxStackDepth;
} else {
char buf[40];
long n;
p = wordStart;
if ((*wordStart == '"') || (*wordStart == '{')) {
p++;
}
savedChar = p[envPtr->numSimpleWordChars];
p[envPtr->numSimpleWordChars] = '\0';
isCompilableInt = 0;
if (TclLooksLikeInt(p)) {
int code = TclGetLong(interp, p, &n);
if (code == TCL_OK) {
TclFormatInt(buf, n);
if (strcmp(p, buf) == 0) {
isCompilableInt = 1;
objIndex = TclObjIndexForString(p,
envPtr->numSimpleWordChars,
0, 0, envPtr);
objPtr = envPtr->objArrayPtr[objIndex];
Tcl_InvalidateStringRep(objPtr);
objPtr->internalRep.longValue = n;
objPtr->typePtr = &tclIntType;
}
} else {
Tcl_ResetResult(interp);
}
}
if (!isCompilableInt) {
objIndex = TclObjIndexForString(p,
envPtr->numSimpleWordChars, 1,
0, envPtr);
}
p[envPtr->numSimpleWordChars] = savedChar;
TclEmitPush(objIndex, envPtr);
maxDepth += 1;
}
}
if (simpleVarName) {
if (elName == NULL) {
if (localIndex >= 0) {
if (localIndex <= 255) {
TclEmitInstUInt1((isAssignment?
INST_STORE_SCALAR1 : INST_LOAD_SCALAR1),
localIndex, envPtr);
} else {
TclEmitInstUInt4((isAssignment?
INST_STORE_SCALAR4 : INST_LOAD_SCALAR4),
localIndex, envPtr);
}
} else {
TclEmitOpcode((isAssignment?
INST_STORE_SCALAR_STK : INST_LOAD_SCALAR_STK),
envPtr);
}
} else {
if (localIndex >= 0) {
if (localIndex <= 255) {
TclEmitInstUInt1((isAssignment?
INST_STORE_ARRAY1 : INST_LOAD_ARRAY1),
localIndex, envPtr);
} else {
TclEmitInstUInt4((isAssignment?
INST_STORE_ARRAY4 : INST_LOAD_ARRAY4),
localIndex, envPtr);
}
} else {
TclEmitOpcode((isAssignment?
INST_STORE_ARRAY_STK : INST_LOAD_ARRAY_STK),
envPtr);
}
}
} else {
TclEmitOpcode((isAssignment? INST_STORE_STK : INST_LOAD_STK), envPtr);
}
done:
if (numWords == 0) {
envPtr->termOffset = 0;
} else {
envPtr->termOffset = (argInfo.endArray[numWords-1] + 1 - string);
}
envPtr->pushSimpleWords = savePushSimpleWords;
envPtr->maxStackDepth = maxDepth;
FreeArgInfo(&argInfo);
return result;
}
int
TclCompileWhileCmd(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register int type;
int maxDepth = 0;
int range = -1;
JumpFixup jumpFalseFixup;
unsigned char *jumpPc;
int jumpDist, jumpBackDist, jumpBackOffset, objIndex, result;
int savePushSimpleWords = envPtr->pushSimpleWords;
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type == TCL_COMMAND_END) {
badArgs:
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"wrong # args: should be \"while test command\"", -1);
result = TCL_ERROR;
goto done;
}
if (*src != '{') {
result = TCL_OUT_LINE_COMPILE;
goto done;
}
envPtr->excRangeDepth++;
envPtr->maxExcRangeDepth =
TclMax(envPtr->excRangeDepth, envPtr->maxExcRangeDepth);
range = CreateExceptionRange(LOOP_EXCEPTION_RANGE, envPtr);
envPtr->excRangeArrayPtr[range].continueOffset = TclCurrCodeOffset();
envPtr->pushSimpleWords = 1;
result = CompileExprWord(interp, src, lastChar, flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
Tcl_AddObjErrorInfo(interp,
"\n (\"while\" test expression)", -1);
}
goto done;
}
maxDepth = envPtr->maxStackDepth;
src += envPtr->termOffset;
TclEmitForwardJump(envPtr, TCL_FALSE_JUMP, &jumpFalseFixup);
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type == TCL_COMMAND_END) {
goto badArgs;
}
envPtr->excRangeArrayPtr[range].codeOffset = TclCurrCodeOffset();
result = CompileCmdWordInline(interp, src, lastChar,
flags, envPtr);
if (result != TCL_OK) {
if (result == TCL_ERROR) {
char msg[60];
sprintf(msg, "\n (\"while\" body line %d)", interp->errorLine);
Tcl_AddObjErrorInfo(interp, msg, -1);
}
goto done;
}
maxDepth = TclMax(envPtr->maxStackDepth, maxDepth);
src += envPtr->termOffset;
envPtr->excRangeArrayPtr[range].numCodeBytes =
(TclCurrCodeOffset() - envPtr->excRangeArrayPtr[range].codeOffset);
TclEmitOpcode(INST_POP, envPtr);
jumpBackOffset = TclCurrCodeOffset();
jumpBackDist =
(jumpBackOffset - envPtr->excRangeArrayPtr[range].continueOffset);
if (jumpBackDist > 120) {
TclEmitInstInt4(INST_JUMP4, -jumpBackDist, envPtr);
} else {
TclEmitInstInt1(INST_JUMP1, -jumpBackDist, envPtr);
}
jumpDist = (TclCurrCodeOffset() - jumpFalseFixup.codeOffset);
if (TclFixupForwardJump(envPtr, &jumpFalseFixup, jumpDist, 127)) {
envPtr->excRangeArrayPtr[range].codeOffset += 3;
jumpBackOffset += 3;
jumpPc = (envPtr->codeStart + jumpBackOffset);
if (jumpBackDist > 120) {
jumpBackDist += 3;
TclUpdateInstInt4AtPc(INST_JUMP4, -jumpBackDist,
jumpPc);
} else {
jumpBackDist += 3;
TclUpdateInstInt1AtPc(INST_JUMP1, -jumpBackDist,
jumpPc);
}
}
envPtr->excRangeArrayPtr[range].breakOffset = TclCurrCodeOffset();
objIndex = TclObjIndexForString("", 0, 0, 0,
envPtr);
TclEmitPush(objIndex, envPtr);
if (maxDepth == 0) {
maxDepth = 1;
}
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type != TCL_COMMAND_END) {
goto badArgs;
}
}
done:
envPtr->termOffset = (src - string);
envPtr->pushSimpleWords = savePushSimpleWords;
envPtr->maxStackDepth = maxDepth;
if (range != -1) {
envPtr->excRangeDepth--;
}
return result;
}
static int
CompileExprWord(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
register char *src = string;
register int type;
int maxDepth = 0;
int nestedCmd = (flags & TCL_BRACKET_TERM);
char *first, *last;
char savedChar;
int inlineCode;
int range = -1;
JumpFixup jumpFixup;
char *p;
char c;
int savePushSimpleWords = envPtr->pushSimpleWords;
int saveExprIsJustVarRef = envPtr->exprIsJustVarRef;
int saveExprIsComparison = envPtr->exprIsComparison;
int numChars, result;
AdvanceToNextWord(src, envPtr);
src += envPtr->termOffset;
type = CHAR_TYPE(src, lastChar);
if (type == TCL_COMMAND_END) {
badArgs:
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"malformed expression word", -1);
result = TCL_ERROR;
goto done;
}
if (*src == '{') {
first = src+1;
src = TclWordEnd(src, lastChar, nestedCmd, NULL);
if (*src == 0) {
goto badArgs;
}
if (*src != '}') {
goto badArgs;
}
last = (src-1);
numChars = (last - first + 1);
savedChar = first[numChars];
first[numChars] = '\0';
result = TclCompileExpr(interp, first, first+numChars,
flags, envPtr);
first[numChars] = savedChar;
src++;
maxDepth = envPtr->maxStackDepth;
} else {
first = src;
last = TclWordEnd(first, lastChar, nestedCmd, NULL);
if (*last == 0) {
src = last;
goto badArgs;
}
inlineCode = 1;
if ((*first == '"') && (*last == '"')) {
inlineCode = 0;
} else {
for (p = first; p <= last; p++) {
c = *p;
if ((c == '[') || (c == '\\')) {
inlineCode = 0;
break;
}
}
}
if (inlineCode) {
int startCodeOffset = (envPtr->codeNext - envPtr->codeStart);
int startRangeNext = envPtr->excRangeArrayNext;
envPtr->excRangeDepth++;
envPtr->maxExcRangeDepth =
TclMax(envPtr->excRangeDepth, envPtr->maxExcRangeDepth);
range = CreateExceptionRange(CATCH_EXCEPTION_RANGE, envPtr);
TclEmitInstUInt4(INST_BEGIN_CATCH4, range, envPtr);
envPtr->excRangeArrayPtr[range].codeOffset = TclCurrCodeOffset();
numChars = (last - first + 1);
savedChar = first[numChars];
first[numChars] = '\0';
result = TclCompileExpr(interp, first, first + numChars,
flags, envPtr);
first[numChars] = savedChar;
envPtr->excRangeArrayPtr[range].numCodeBytes =
TclCurrCodeOffset() - envPtr->excRangeArrayPtr[range].codeOffset;
if ((result != TCL_OK) || (envPtr->exprIsJustVarRef)
|| (envPtr->exprIsComparison)) {
envPtr->codeNext = (envPtr->codeStart + startCodeOffset);
envPtr->excRangeArrayNext = startRangeNext;
inlineCode = 0;
} else {
TclEmitOpcode(INST_END_CATCH, envPtr);
TclEmitForwardJump(envPtr, TCL_UNCONDITIONAL_JUMP, &jumpFixup);
envPtr->excRangeArrayPtr[range].catchOffset = TclCurrCodeOffset();
}
}
envPtr->pushSimpleWords = 1;
result = CompileWord(interp, first, lastChar, flags, envPtr);
src += envPtr->termOffset;
maxDepth = envPtr->maxStackDepth;
if (result == TCL_OK) {
TclEmitOpcode(INST_EXPR_STK, envPtr);
}
if (inlineCode) {
int jumpDist = (TclCurrCodeOffset() - jumpFixup.codeOffset);
if (TclFixupForwardJump(envPtr, &jumpFixup, jumpDist, 127)) {
envPtr->excRangeArrayPtr[range].catchOffset += 3;
}
}
}
done:
if (range != -1) {
envPtr->excRangeDepth--;
}
envPtr->termOffset = (src - string);
envPtr->maxStackDepth = maxDepth;
envPtr->pushSimpleWords = savePushSimpleWords;
envPtr->exprIsJustVarRef = saveExprIsJustVarRef;
envPtr->exprIsComparison = saveExprIsComparison;
return result;
}
static int
CompileCmdWordInline(interp, string, lastChar, flags, envPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
CompileEnv *envPtr;
{
Interp *iPtr = (Interp *) interp;
register char *src = string;
register int type;
int maxDepth = 0;
char *termPtr;
char savedChar;
int savePushSimpleWords = envPtr->pushSimpleWords;
int objIndex;
int result = TCL_OK;
register char c;
type = CHAR_TYPE(src, lastChar);
if (type & (TCL_QUOTE | TCL_OPEN_BRACE)) {
src++;
envPtr->pushSimpleWords = 0;
if (type == TCL_QUOTE) {
result = TclCompileQuotes(interp, src, lastChar,
'"', flags, envPtr);
} else {
result = CompileBraces(interp, src, lastChar, flags, envPtr);
}
if (result != TCL_OK) {
goto done;
}
termPtr = (src + envPtr->termOffset);
c = *termPtr;
if ((c == '\\') && (*(termPtr+1) == '\n')) {
} else {
type = CHAR_TYPE(termPtr, lastChar);
if ((type != TCL_SPACE) && (type != TCL_COMMAND_END)) {
Tcl_ResetResult(interp);
if (*(src-1) == '"') {
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"extra characters after close-quote", -1);
} else {
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"extra characters after close-brace", -1);
}
result = TCL_ERROR;
goto done;
}
}
if (envPtr->wordIsSimple) {
char *closeCharPos = (termPtr - 1);
savedChar = *closeCharPos;
*closeCharPos = '\0';
result = TclCompileString(interp, src, closeCharPos,
(flags & ~TCL_BRACKET_TERM), envPtr);
*closeCharPos = savedChar;
if (result != TCL_OK) {
goto done;
}
} else {
TclEmitOpcode(INST_EVAL_STK, envPtr);
}
src = termPtr;
maxDepth = envPtr->maxStackDepth;
} else {
char *p = src;
c = *p;
while (isalnum(UCHAR(c)) || (c == '_')) {
p++;
c = *p;
}
type = CHAR_TYPE(p, lastChar);
if ((p > src) && (type == TCL_COMMAND_END)) {
Tcl_Command cmd;
Command *cmdPtr = NULL;
int wasCompiled = 0;
savedChar = *p;
*p = '\0';
cmd = Tcl_FindCommand(interp, src, (Tcl_Namespace *) NULL,
0);
if (cmd != (Tcl_Command) NULL) {
cmdPtr = (Command *) cmd;
}
if (cmdPtr != NULL && cmdPtr->compileProc != NULL) {
*p = savedChar;
src = p;
iPtr->flags &= ~(ERR_ALREADY_LOGGED | ERR_IN_PROGRESS
| ERROR_CODE_SET);
result = (*(cmdPtr->compileProc))(interp, src, lastChar, flags, envPtr);
if (result != TCL_OK) {
goto done;
}
wasCompiled = 1;
src += envPtr->termOffset;
maxDepth = envPtr->maxStackDepth;
}
if (!wasCompiled) {
objIndex = TclObjIndexForString(src, p-src,
1, 0, envPtr);
*p = savedChar;
TclEmitPush(objIndex, envPtr);
TclEmitInstUInt1(INST_INVOKE_STK1, 1, envPtr);
src = p;
maxDepth = 1;
}
} else {
envPtr->pushSimpleWords = 1;
result = CompileWord(interp, src, lastChar, flags, envPtr);
if (result != TCL_OK) {
goto done;
}
TclEmitOpcode(INST_EVAL_STK, envPtr);
src += envPtr->termOffset;
maxDepth = envPtr->maxStackDepth;
}
}
done:
envPtr->termOffset = (src - string);
envPtr->maxStackDepth = maxDepth;
envPtr->pushSimpleWords = savePushSimpleWords;
return result;
}
static int
LookupCompiledLocal(name, nameChars, createIfNew, flagsIfCreated, procPtr)
register char *name;
int nameChars;
int createIfNew;
int flagsIfCreated;
register Proc *procPtr;
{
register CompiledLocal *localPtr;
int localIndex = -1;
register int i;
int localCt;
if (name != NULL) {
localCt = procPtr->numCompiledLocals;
localPtr = procPtr->firstLocalPtr;
for (i = 0; i < localCt; i++) {
if (!TclIsVarTemporary(localPtr)) {
char *localName = localPtr->name;
if ((name[0] == localName[0])
&& (nameChars == localPtr->nameLength)
&& (strncmp(name, localName, (unsigned) nameChars) == 0)) {
return i;
}
}
localPtr = localPtr->nextPtr;
}
}
if (createIfNew || (name == NULL)) {
localIndex = procPtr->numCompiledLocals;
localPtr = (CompiledLocal *) ckalloc((unsigned)
(sizeof(CompiledLocal) - sizeof(localPtr->name)
+ nameChars+1));
if (procPtr->firstLocalPtr == NULL) {
procPtr->firstLocalPtr = procPtr->lastLocalPtr = localPtr;
} else {
procPtr->lastLocalPtr->nextPtr = localPtr;
procPtr->lastLocalPtr = localPtr;
}
localPtr->nextPtr = NULL;
localPtr->nameLength = nameChars;
localPtr->frameIndex = localIndex;
localPtr->flags = flagsIfCreated;
if (name == NULL) {
localPtr->flags |= VAR_TEMPORARY;
}
localPtr->defValuePtr = NULL;
localPtr->resolveInfo = NULL;
if (name != NULL) {
memcpy((VOID *) localPtr->name, (VOID *) name, (size_t) nameChars);
}
localPtr->name[nameChars] = '\0';
procPtr->numCompiledLocals++;
}
return localIndex;
}
void
TclInitCompiledLocals(interp, framePtr, nsPtr)
Tcl_Interp *interp;
CallFrame *framePtr;
Namespace *nsPtr;
{
register CompiledLocal *localPtr;
Interp *iPtr = (Interp*) interp;
Tcl_ResolvedVarInfo *vinfo, *resVarInfo;
Var *varPtr = framePtr->compiledLocals;
Var *resolvedVarPtr;
ResolverScheme *resPtr;
int result;
for (localPtr = framePtr->procPtr->firstLocalPtr;
localPtr != NULL;
localPtr = localPtr->nextPtr) {
if (!(localPtr->flags & (VAR_ARGUMENT|VAR_TEMPORARY|VAR_RESOLVED))
&& (nsPtr->compiledVarResProc || iPtr->resolverPtr)) {
resPtr = iPtr->resolverPtr;
if (nsPtr->compiledVarResProc) {
result = (*nsPtr->compiledVarResProc)(nsPtr->interp,
localPtr->name, localPtr->nameLength,
(Tcl_Namespace *) nsPtr, &vinfo);
} else {
result = TCL_CONTINUE;
}
while ((result == TCL_CONTINUE) && resPtr) {
if (resPtr->compiledVarResProc) {
result = (*resPtr->compiledVarResProc)(nsPtr->interp,
localPtr->name, localPtr->nameLength,
(Tcl_Namespace *) nsPtr, &vinfo);
}
resPtr = resPtr->nextPtr;
}
if (result == TCL_OK) {
localPtr->resolveInfo = vinfo;
localPtr->flags |= VAR_RESOLVED;
}
}
resVarInfo = localPtr->resolveInfo;
resolvedVarPtr = NULL;
if (resVarInfo && resVarInfo->fetchProc) {
resolvedVarPtr = (Var*) (*resVarInfo->fetchProc)(interp,
resVarInfo);
}
if (resolvedVarPtr) {
varPtr->name = localPtr->name;
varPtr->nsPtr = NULL;
varPtr->hPtr = NULL;
varPtr->refCount = 0;
varPtr->tracePtr = NULL;
varPtr->searchPtr = NULL;
varPtr->flags = 0;
TclSetVarLink(varPtr);
varPtr->value.linkPtr = resolvedVarPtr;
resolvedVarPtr->refCount++;
} else {
varPtr->value.objPtr = NULL;
varPtr->name = localPtr->name;
varPtr->nsPtr = NULL;
varPtr->hPtr = NULL;
varPtr->refCount = 0;
varPtr->tracePtr = NULL;
varPtr->searchPtr = NULL;
varPtr->flags = (localPtr->flags | VAR_UNDEFINED);
}
varPtr++;
}
}
static void
AdvanceToNextWord(string, envPtr)
char *string;
CompileEnv *envPtr;
{
register char *src;
register int type;
src = string;
type = CHAR_TYPE(src, src+1);
while (type & (TCL_SPACE | TCL_BACKSLASH)) {
if (type == TCL_BACKSLASH) {
if (src[1] == '\n') {
src += 2;
} else {
break;
}
} else {
src++;
}
type = CHAR_TYPE(src, src+1);
}
envPtr->termOffset = (src - string);
}
char
Tcl_Backslash(src, readPtr)
CONST char *src;
int *readPtr;
{
CONST char *p = src + 1;
char result;
int count;
count = 2;
switch (*p) {
case 'a':
result = 0x7;
break;
case 'b':
result = 0x8;
break;
case 'f':
result = 0xc;
break;
case 'n':
result = 0xa;
break;
case 'r':
result = 0xd;
break;
case 't':
result = 0x9;
break;
case 'v':
result = 0xb;
break;
case 'x':
if (isxdigit(UCHAR(p[1]))) {
char *end;
result = (char) strtoul(p+1, &end, 16);
count = end - src;
} else {
count = 2;
result = 'x';
}
break;
case '\n':
do {
p++;
} while ((*p == ' ') || (*p == '\t'));
result = ' ';
count = p - src;
break;
case 0:
result = '\\';
count = 1;
break;
default:
if (isdigit(UCHAR(*p))) {
result = (char)(*p - '0');
p++;
if (!isdigit(UCHAR(*p))) {
break;
}
count = 3;
result = (char)((result << 3) + (*p - '0'));
p++;
if (!isdigit(UCHAR(*p))) {
break;
}
count = 4;
result = (char)((result << 3) + (*p - '0'));
break;
}
result = *p;
count = 2;
break;
}
if (readPtr != NULL) {
*readPtr = count;
}
return result;
}
int
TclObjIndexForString(string, length, allocStrRep, inHeap, envPtr)
register char *string;
int length;
int allocStrRep;
int inHeap;
CompileEnv *envPtr;
{
register Tcl_Obj *objPtr;
int objIndex;
Tcl_HashEntry *hPtr;
int strLength, new;
strLength = strlen(string);
if (length == -1) {
length = strLength;
}
if (strLength != length) {
hPtr = NULL;
} else {
hPtr = Tcl_CreateHashEntry(&envPtr->objTable, string, &new);
if (!new) {
objIndex = (int) Tcl_GetHashValue(hPtr);
if (inHeap) {
ckfree(string);
}
return objIndex;
}
}
objPtr = Tcl_NewObj();
if (allocStrRep) {
if (inHeap) {
objPtr->bytes = string;
} else {
if (length > 0) {
objPtr->bytes = ckalloc((unsigned) length + 1);
memcpy((VOID *) objPtr->bytes, (VOID *) string,
(size_t) length);
objPtr->bytes[length] = '\0';
}
}
objPtr->length = length;
} else {
if (inHeap) {
ckfree(string);
}
}
if (envPtr->objArrayNext >= envPtr->objArrayEnd) {
ExpandObjectArray(envPtr);
}
objIndex = envPtr->objArrayNext;
envPtr->objArrayPtr[objIndex] = objPtr;
Tcl_IncrRefCount(objPtr);
envPtr->objArrayNext++;
if (hPtr) {
Tcl_SetHashValue(hPtr, objIndex);
}
return objIndex;
}
void
TclExpandCodeArray(envPtr)
CompileEnv *envPtr;
{
size_t currBytes = TclCurrCodeOffset();
size_t newBytes = 2*(envPtr->codeEnd - envPtr->codeStart);
unsigned char *newPtr = (unsigned char *) ckalloc((unsigned) newBytes);
memcpy((VOID *) newPtr, (VOID *) envPtr->codeStart, currBytes);
if (envPtr->mallocedCodeArray) {
ckfree((char *) envPtr->codeStart);
}
envPtr->codeStart = newPtr;
envPtr->codeNext = (newPtr + currBytes);
envPtr->codeEnd = (newPtr + newBytes);
envPtr->mallocedCodeArray = 1;
}
static void
ExpandObjectArray(envPtr)
CompileEnv *envPtr;
{
size_t currBytes = envPtr->objArrayNext * sizeof(Tcl_Obj *);
int newElems = 2*envPtr->objArrayEnd;
size_t newBytes = newElems * sizeof(Tcl_Obj *);
Tcl_Obj **newPtr = (Tcl_Obj **) ckalloc((unsigned) newBytes);
memcpy((VOID *) newPtr, (VOID *) envPtr->objArrayPtr, currBytes);
if (envPtr->mallocedObjArray) {
ckfree((char *) envPtr->objArrayPtr);
}
envPtr->objArrayPtr = (Tcl_Obj **) newPtr;
envPtr->objArrayEnd = newElems;
envPtr->mallocedObjArray = 1;
}
static void
EnterCmdStartData(envPtr, cmdIndex, srcOffset, codeOffset)
CompileEnv *envPtr;
int cmdIndex;
int srcOffset;
int codeOffset;
{
CmdLocation *cmdLocPtr;
if ((cmdIndex < 0) || (cmdIndex >= envPtr->numCommands)) {
panic("EnterCmdStartData: bad command index %d\n", cmdIndex);
}
if (cmdIndex >= envPtr->cmdMapEnd) {
size_t currElems = envPtr->cmdMapEnd;
size_t newElems = 2*currElems;
size_t currBytes = currElems * sizeof(CmdLocation);
size_t newBytes = newElems * sizeof(CmdLocation);
CmdLocation *newPtr = (CmdLocation *) ckalloc((unsigned) newBytes);
memcpy((VOID *) newPtr, (VOID *) envPtr->cmdMapPtr, currBytes);
if (envPtr->mallocedCmdMap) {
ckfree((char *) envPtr->cmdMapPtr);
}
envPtr->cmdMapPtr = (CmdLocation *) newPtr;
envPtr->cmdMapEnd = newElems;
envPtr->mallocedCmdMap = 1;
}
if (cmdIndex > 0) {
if (codeOffset < envPtr->cmdMapPtr[cmdIndex-1].codeOffset) {
panic("EnterCmdStartData: cmd map table not sorted by code offset");
}
}
cmdLocPtr = &(envPtr->cmdMapPtr[cmdIndex]);
cmdLocPtr->codeOffset = codeOffset;
cmdLocPtr->srcOffset = srcOffset;
cmdLocPtr->numSrcChars = -1;
cmdLocPtr->numCodeBytes = -1;
}
static void
EnterCmdExtentData(envPtr, cmdIndex, numSrcChars, numCodeBytes)
CompileEnv *envPtr;
int cmdIndex;
int numSrcChars;
int numCodeBytes;
{
CmdLocation *cmdLocPtr;
if ((cmdIndex < 0) || (cmdIndex >= envPtr->numCommands)) {
panic("EnterCmdStartData: bad command index %d\n", cmdIndex);
}
if (cmdIndex > envPtr->cmdMapEnd) {
panic("EnterCmdStartData: no start data registered for command with index %d\n", cmdIndex);
}
cmdLocPtr = &(envPtr->cmdMapPtr[cmdIndex]);
cmdLocPtr->numSrcChars = numSrcChars;
cmdLocPtr->numCodeBytes = numCodeBytes;
}
static void
InitArgInfo(argInfoPtr)
register ArgInfo *argInfoPtr;
{
argInfoPtr->numArgs = 0;
argInfoPtr->startArray = argInfoPtr->staticStartSpace;
argInfoPtr->endArray = argInfoPtr->staticEndSpace;
argInfoPtr->allocArgs = ARGINFO_INIT_ENTRIES;
argInfoPtr->mallocedArrays = 0;
}
static int
CollectArgInfo(interp, string, lastChar, flags, argInfoPtr)
Tcl_Interp *interp;
char *string;
char *lastChar;
int flags;
register ArgInfo *argInfoPtr;
{
register char *src = string;
register int type;
int nestedCmd = (flags & TCL_BRACKET_TERM);
int scanningArgs;
char *wordStart, *wordEnd;
CompileEnv tempCompEnv;
char *prev;
argInfoPtr->numArgs = 0;
scanningArgs = 1;
while (scanningArgs) {
AdvanceToNextWord(src, &tempCompEnv);
src += tempCompEnv.termOffset;
type = CHAR_TYPE(src, lastChar);
if ((type == TCL_COMMAND_END) && ((*src != ']') || nestedCmd)) {
break;
} else if (*src == '"') {
wordStart = src;
src = TclWordEnd(src, lastChar, nestedCmd, NULL);
if (src == lastChar) {
badStringTermination:
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"quoted string doesn't terminate properly", -1);
return TCL_ERROR;
}
prev = (src-1);
if (*src == '"') {
wordEnd = src;
src++;
} else if ((*src == ';') && (*prev == '"')) {
scanningArgs = 0;
wordEnd = prev;
} else {
goto badStringTermination;
}
} else if (*src == '{') {
wordStart = src;
src = TclWordEnd(src, lastChar, nestedCmd, NULL);
if (src == lastChar) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"missing close-brace", -1);
return TCL_ERROR;
}
prev = (src-1);
if (*src == '}') {
wordEnd = src;
src++;
} else if ((*src == ';') && (*prev == '}')) {
scanningArgs = 0;
wordEnd = prev;
} else {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"argument word in braces doesn't terminate properly", -1);
return TCL_ERROR;
}
} else {
wordStart = src;
src = TclWordEnd(src, lastChar, nestedCmd, NULL);
prev = (src-1);
if (src == lastChar) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"missing close-bracket or close-brace", -1);
return TCL_ERROR;
} else if (*src == ';') {
scanningArgs = 0;
wordEnd = prev;
} else {
wordEnd = src;
src++;
if ((src == lastChar) || (*src == '\n')
|| ((*src == ']') && nestedCmd)) {
scanningArgs = 0;
}
}
}
if (argInfoPtr->numArgs == argInfoPtr->allocArgs) {
int newArgs = 2*argInfoPtr->numArgs;
size_t currBytes = argInfoPtr->numArgs * sizeof(char *);
size_t newBytes = newArgs * sizeof(char *);
char **newStartArrayPtr =
(char **) ckalloc((unsigned) newBytes);
char **newEndArrayPtr =
(char **) ckalloc((unsigned) newBytes);
memcpy((VOID *) newStartArrayPtr,
(VOID *) argInfoPtr->startArray, currBytes);
memcpy((VOID *) newEndArrayPtr,
(VOID *) argInfoPtr->endArray, currBytes);
if (argInfoPtr->mallocedArrays) {
ckfree((char *) argInfoPtr->startArray);
ckfree((char *) argInfoPtr->endArray);
}
argInfoPtr->startArray = newStartArrayPtr;
argInfoPtr->endArray = newEndArrayPtr;
argInfoPtr->allocArgs = newArgs;
argInfoPtr->mallocedArrays = 1;
}
argInfoPtr->startArray[argInfoPtr->numArgs] = wordStart;
argInfoPtr->endArray[argInfoPtr->numArgs] = wordEnd;
argInfoPtr->numArgs++;
}
return TCL_OK;
}
static void
FreeArgInfo(argInfoPtr)
register ArgInfo *argInfoPtr;
{
if (argInfoPtr->mallocedArrays) {
ckfree((char *) argInfoPtr->startArray);
ckfree((char *) argInfoPtr->endArray);
}
}
static int
CreateExceptionRange(type, envPtr)
ExceptionRangeType type;
register CompileEnv *envPtr;
{
int index;
register ExceptionRange *rangePtr;
index = envPtr->excRangeArrayNext;
if (index >= envPtr->excRangeArrayEnd) {
size_t currBytes =
envPtr->excRangeArrayNext * sizeof(ExceptionRange);
int newElems = 2*envPtr->excRangeArrayEnd;
size_t newBytes = newElems * sizeof(ExceptionRange);
ExceptionRange *newPtr = (ExceptionRange *)
ckalloc((unsigned) newBytes);
memcpy((VOID *) newPtr, (VOID *) envPtr->excRangeArrayPtr,
currBytes);
if (envPtr->mallocedExcRangeArray) {
ckfree((char *) envPtr->excRangeArrayPtr);
}
envPtr->excRangeArrayPtr = (ExceptionRange *) newPtr;
envPtr->excRangeArrayEnd = newElems;
envPtr->mallocedExcRangeArray = 1;
}
envPtr->excRangeArrayNext++;
rangePtr = &(envPtr->excRangeArrayPtr[index]);
rangePtr->type = type;
rangePtr->nestingLevel = envPtr->excRangeDepth;
rangePtr->codeOffset = -1;
rangePtr->numCodeBytes = -1;
rangePtr->breakOffset = -1;
rangePtr->continueOffset = -1;
rangePtr->catchOffset = -1;
return index;
}
int
TclCreateAuxData(clientData, typePtr, envPtr)
ClientData clientData;
AuxDataType *typePtr;
register CompileEnv *envPtr;
{
int index;
register AuxData *auxDataPtr;
index = envPtr->auxDataArrayNext;
if (index >= envPtr->auxDataArrayEnd) {
size_t currBytes = envPtr->auxDataArrayNext * sizeof(AuxData);
int newElems = 2*envPtr->auxDataArrayEnd;
size_t newBytes = newElems * sizeof(AuxData);
AuxData *newPtr = (AuxData *) ckalloc((unsigned) newBytes);
memcpy((VOID *) newPtr, (VOID *) envPtr->auxDataArrayPtr,
currBytes);
if (envPtr->mallocedAuxDataArray) {
ckfree((char *) envPtr->auxDataArrayPtr);
}
envPtr->auxDataArrayPtr = newPtr;
envPtr->auxDataArrayEnd = newElems;
envPtr->mallocedAuxDataArray = 1;
}
envPtr->auxDataArrayNext++;
auxDataPtr = &(envPtr->auxDataArrayPtr[index]);
auxDataPtr->type = typePtr;
auxDataPtr->clientData = clientData;
return index;
}
void
TclInitJumpFixupArray(fixupArrayPtr)
register JumpFixupArray *fixupArrayPtr;
{
fixupArrayPtr->fixup = fixupArrayPtr->staticFixupSpace;
fixupArrayPtr->next = 0;
fixupArrayPtr->end = (JUMPFIXUP_INIT_ENTRIES - 1);
fixupArrayPtr->mallocedArray = 0;
}
void
TclExpandJumpFixupArray(fixupArrayPtr)
register JumpFixupArray *fixupArrayPtr;
{
size_t currBytes = fixupArrayPtr->next * sizeof(JumpFixup);
int newElems = 2*(fixupArrayPtr->end + 1);
size_t newBytes = newElems * sizeof(JumpFixup);
JumpFixup *newPtr = (JumpFixup *) ckalloc((unsigned) newBytes);
memcpy((VOID *) newPtr, (VOID *) fixupArrayPtr->fixup, currBytes);
if (fixupArrayPtr->mallocedArray) {
ckfree((char *) fixupArrayPtr->fixup);
}
fixupArrayPtr->fixup = (JumpFixup *) newPtr;
fixupArrayPtr->end = newElems;
fixupArrayPtr->mallocedArray = 1;
}
void
TclFreeJumpFixupArray(fixupArrayPtr)
register JumpFixupArray *fixupArrayPtr;
{
if (fixupArrayPtr->mallocedArray) {
ckfree((char *) fixupArrayPtr->fixup);
}
}
void
TclEmitForwardJump(envPtr, jumpType, jumpFixupPtr)
CompileEnv *envPtr;
TclJumpType jumpType;
JumpFixup *jumpFixupPtr;
{
jumpFixupPtr->jumpType = jumpType;
jumpFixupPtr->codeOffset = TclCurrCodeOffset();
jumpFixupPtr->cmdIndex = envPtr->numCommands;
jumpFixupPtr->excRangeIndex = envPtr->excRangeArrayNext;
switch (jumpType) {
case TCL_UNCONDITIONAL_JUMP:
TclEmitInstInt1(INST_JUMP1, 0, envPtr);
break;
case TCL_TRUE_JUMP:
TclEmitInstInt1(INST_JUMP_TRUE1, 0, envPtr);
break;
default:
TclEmitInstInt1(INST_JUMP_FALSE1, 0, envPtr);
break;
}
}
int
TclFixupForwardJump(envPtr, jumpFixupPtr, jumpDist, distThreshold)
CompileEnv *envPtr;
JumpFixup *jumpFixupPtr;
int jumpDist;
int distThreshold;
{
unsigned char *jumpPc, *p;
int firstCmd, lastCmd, firstRange, lastRange, k;
unsigned int numBytes;
if (jumpDist <= distThreshold) {
jumpPc = (envPtr->codeStart + jumpFixupPtr->codeOffset);
switch (jumpFixupPtr->jumpType) {
case TCL_UNCONDITIONAL_JUMP:
TclUpdateInstInt1AtPc(INST_JUMP1, jumpDist, jumpPc);
break;
case TCL_TRUE_JUMP:
TclUpdateInstInt1AtPc(INST_JUMP_TRUE1, jumpDist, jumpPc);
break;
default:
TclUpdateInstInt1AtPc(INST_JUMP_FALSE1, jumpDist, jumpPc);
break;
}
return 0;
}
TclEnsureCodeSpace(3, envPtr);
jumpPc = (envPtr->codeStart + jumpFixupPtr->codeOffset);
for (numBytes = envPtr->codeNext-jumpPc-2, p = jumpPc+2+numBytes-1;
numBytes > 0; numBytes--, p--) {
p[3] = p[0];
}
envPtr->codeNext += 3;
jumpDist += 3;
switch (jumpFixupPtr->jumpType) {
case TCL_UNCONDITIONAL_JUMP:
TclUpdateInstInt4AtPc(INST_JUMP4, jumpDist, jumpPc);
break;
case TCL_TRUE_JUMP:
TclUpdateInstInt4AtPc(INST_JUMP_TRUE4, jumpDist, jumpPc);
break;
default:
TclUpdateInstInt4AtPc(INST_JUMP_FALSE4, jumpDist, jumpPc);
break;
}
firstCmd = jumpFixupPtr->cmdIndex;
lastCmd = (envPtr->numCommands - 1);
if (firstCmd < lastCmd) {
for (k = firstCmd; k <= lastCmd; k++) {
(envPtr->cmdMapPtr[k]).codeOffset += 3;
}
}
firstRange = jumpFixupPtr->excRangeIndex;
lastRange = (envPtr->excRangeArrayNext - 1);
for (k = firstRange; k <= lastRange; k++) {
ExceptionRange *rangePtr = &(envPtr->excRangeArrayPtr[k]);
rangePtr->codeOffset += 3;
switch (rangePtr->type) {
case LOOP_EXCEPTION_RANGE:
rangePtr->breakOffset += 3;
if (rangePtr->continueOffset != -1) {
rangePtr->continueOffset += 3;
}
break;
case CATCH_EXCEPTION_RANGE:
rangePtr->catchOffset += 3;
break;
default:
panic("TclFixupForwardJump: unrecognized ExceptionRange type %d\n", rangePtr->type);
}
}
return 1;
}
InstructionDesc *
TclGetInstructionTable()
{
return &instructionTable[0];
}
void
TclRegisterAuxDataType(typePtr)
AuxDataType *typePtr;
{
register Tcl_HashEntry *hPtr;
int new;
if (!auxDataTypeTableInitialized) {
TclInitAuxDataTypeTable();
}
hPtr = Tcl_FindHashEntry(&auxDataTypeTable, typePtr->name);
if (hPtr != (Tcl_HashEntry *) NULL) {
Tcl_DeleteHashEntry(hPtr);
}
hPtr = Tcl_CreateHashEntry(&auxDataTypeTable, typePtr->name, &new);
if (new) {
Tcl_SetHashValue(hPtr, typePtr);
}
}
AuxDataType *
TclGetAuxDataType(typeName)
char *typeName;
{
register Tcl_HashEntry *hPtr;
AuxDataType *typePtr = NULL;
if (!auxDataTypeTableInitialized) {
TclInitAuxDataTypeTable();
}
hPtr = Tcl_FindHashEntry(&auxDataTypeTable, typeName);
if (hPtr != (Tcl_HashEntry *) NULL) {
typePtr = (AuxDataType *) Tcl_GetHashValue(hPtr);
}
return typePtr;
}
void
TclInitAuxDataTypeTable()
{
auxDataTypeTableInitialized = 1;
Tcl_InitHashTable(&auxDataTypeTable, TCL_STRING_KEYS);
TclRegisterAuxDataType(&tclForeachInfoType);
}
void
TclFinalizeAuxDataTypeTable()
{
if (auxDataTypeTableInitialized) {
Tcl_DeleteHashTable(&auxDataTypeTable);
auxDataTypeTableInitialized = 0;
}
}