diff options
| author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2019-02-04 22:46:56 (GMT) |
|---|---|---|
| committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2019-02-04 22:46:56 (GMT) |
| commit | d50da922b1c1a3043e6ee9f24282a638ee143b48 (patch) | |
| tree | 937d8b4d10f30a85b1657a2af519b72b243bd63e /generic/tclExecute.c | |
| parent | 795fcf4f08882df1123a1ab6228a6cdf31fbb3eb (diff) | |
| parent | 73b6b4eab6a4b0a4ecf0f0c6bcf00bd815c34dd5 (diff) | |
| download | tcl-d50da922b1c1a3043e6ee9f24282a638ee143b48.zip tcl-d50da922b1c1a3043e6ee9f24282a638ee143b48.tar.gz tcl-d50da922b1c1a3043e6ee9f24282a638ee143b48.tar.bz2 | |
merge 8.7
Diffstat (limited to 'generic/tclExecute.c')
| -rw-r--r-- | generic/tclExecute.c | 455 |
1 files changed, 151 insertions, 304 deletions
diff --git a/generic/tclExecute.c b/generic/tclExecute.c index f38f7cd..17ad0bb 100644 --- a/generic/tclExecute.c +++ b/generic/tclExecute.c @@ -750,7 +750,7 @@ ReleaseDictIterator( Tcl_Obj *dictPtr; const Tcl_ObjIntRep *irPtr; - irPtr = Tcl_FetchIntRep(objPtr, &dictIteratorType); + irPtr = TclFetchIntRep(objPtr, &dictIteratorType); assert(irPtr != NULL); /* @@ -1454,7 +1454,7 @@ CompileExprObj( */ ByteCodeGetIntRep(objPtr, &exprCodeType, codePtr); - + if (codePtr != NULL) { Namespace *namespacePtr = iPtr->varFramePtr->nsPtr; @@ -1830,11 +1830,11 @@ TclIncrObj( return TCL_ERROR; } - if ((type1 != TCL_NUMBER_BIG) && (type2 != TCL_NUMBER_BIG)) { + if ((type1 == TCL_NUMBER_INT) && (type2 == TCL_NUMBER_INT)) { Tcl_WideInt w1, w2, sum; - TclGetWideIntFromObj(NULL, valuePtr, &w1); - TclGetWideIntFromObj(NULL, incrPtr, &w2); + w1 = *((const Tcl_WideInt *)ptr1); + w2 = *((const Tcl_WideInt *)ptr2); sum = w1 + w2; /* @@ -1909,7 +1909,7 @@ ArgumentBCEnter( *---------------------------------------------------------------------- */ #define bcFramePtr (&TD->cmdFrame) -#define initCatchTop ((ptrdiff_t *) (&TD->stack[-1])) +#define initCatchTop ((ptrdiff_t *) (TD->stack-1)) #define initTosPtr ((Tcl_Obj **) (initCatchTop+codePtr->maxExceptDepth)) #define esPtr (iPtr->execEnvPtr->execStackPtr) @@ -4768,7 +4768,7 @@ TEBCresume( */ if ((TclListObjGetElements(interp, valuePtr, &objc, &objv) == TCL_OK) - && (NULL == Tcl_FetchIntRep(value2Ptr, &tclListType)) + && (value2Ptr->typePtr != &tclListType) && (TclGetIntForIndexM(NULL, value2Ptr, objc-1, &index) == TCL_OK)) { TclDecrRefCount(value2Ptr); @@ -4963,22 +4963,17 @@ TEBCresume( /* Every range of an empty list is an empty list */ if (objc == 0) { - TRACE_APPEND(("\n")); - NEXT_INST_F(9, 0, 0); + /* avoid return of not canonical list (e. g. spaces in string repr.) */ + if (!valuePtr->bytes || !valuePtr->length) { + TRACE_APPEND(("\n")); + NEXT_INST_F(9, 0, 0); + } + goto emptyList; } /* Decode index value operands. */ - /* - assert ( toIdx != TCL_INDEX_AFTER); - * - * Extra safety for legacy bytecodes: - */ - if (toIdx == TCL_INDEX_AFTER) { - toIdx = TCL_INDEX_END; - } - - if ((toIdx == TCL_INDEX_BEFORE) || (fromIdx == TCL_INDEX_AFTER)) { + if (toIdx == TCL_INDEX_NONE) { emptyList: objResultPtr = Tcl_NewObj(); TRACE_APPEND(("\"%.30s\"", O2S(objResultPtr))); @@ -4993,11 +4988,11 @@ TEBCresume( assert ( toIdx >= 0 && toIdx < objc); /* - assert ( fromIdx != TCL_INDEX_BEFORE ); + assert ( fromIdx != TCL_INDEX_NONE ); * * Extra safety for legacy bytecodes: */ - if (fromIdx == TCL_INDEX_BEFORE) { + if (fromIdx == TCL_INDEX_NONE) { fromIdx = TCL_INDEX_START; } @@ -5284,17 +5279,13 @@ TEBCresume( /* Decode index operands. */ /* - assert ( toIdx != TCL_INDEX_BEFORE ); - assert ( toIdx != TCL_INDEX_AFTER); + assert ( toIdx != TCL_INDEX_NONE ); * * Extra safety for legacy bytecodes: */ - if (toIdx == TCL_INDEX_BEFORE) { + if (toIdx == TCL_INDEX_NONE) { goto emptyRange; } - if (toIdx == TCL_INDEX_AFTER) { - toIdx = TCL_INDEX_END; - } toIdx = TclIndexDecode(toIdx, length - 1); if (toIdx < 0) { @@ -5306,17 +5297,13 @@ TEBCresume( assert ( toIdx >= 0 && toIdx < length ); /* - assert ( fromIdx != TCL_INDEX_BEFORE ); - assert ( fromIdx != TCL_INDEX_AFTER); + assert ( fromIdx != TCL_INDEX_NONE ); * * Extra safety for legacy bytecodes: */ - if (fromIdx == TCL_INDEX_BEFORE) { + if (fromIdx == TCL_INDEX_NONE) { fromIdx = TCL_INDEX_START; } - if (fromIdx == TCL_INDEX_AFTER) { - goto emptyRange; - } fromIdx = TclIndexDecode(fromIdx, length - 1); if (fromIdx < 0) { @@ -7121,7 +7108,7 @@ TEBCresume( } varPtr = LOCAL(opnd); if (varPtr->value.objPtr) { - if (Tcl_FetchIntRep(varPtr->value.objPtr, &dictIteratorType)) { + if (varPtr->value.objPtr->typePtr == &dictIteratorType) { Tcl_Panic("mis-issued dictFirst!"); } TclDecrRefCount(varPtr->value.objPtr); @@ -7138,7 +7125,7 @@ TEBCresume( const Tcl_ObjIntRep *irPtr; if (statePtr && - (irPtr = Tcl_FetchIntRep(statePtr, &dictIteratorType))) { + (irPtr = TclFetchIntRep(statePtr, &dictIteratorType))) { searchPtr = irPtr->twoPtrValue.ptr1; Tcl_DictObjNext(searchPtr, &keyPtr, &valuePtr, &done); } else { @@ -7806,6 +7793,91 @@ FinalizeOONextFilter( } /* + * WidePwrSmallExpon -- + * + * Helper to calculate small powers of integers whose result is wide. + */ +static inline Tcl_WideInt +WidePwrSmallExpon(Tcl_WideInt w1, long exponent) { + + Tcl_WideInt wResult; + + wResult = w1 * w1; /* b**2 */ + switch (exponent) { + case 2: + break; + case 3: + wResult *= w1; /* b**3 */ + break; + case 4: + wResult *= wResult; /* b**4 */ + break; + case 5: + wResult *= wResult; /* b**4 */ + wResult *= w1; /* b**5 */ + break; + case 6: + wResult *= w1; /* b**3 */ + wResult *= wResult; /* b**6 */ + break; + case 7: + wResult *= w1; /* b**3 */ + wResult *= wResult; /* b**6 */ + wResult *= w1; /* b**7 */ + break; + case 8: + wResult *= wResult; /* b**4 */ + wResult *= wResult; /* b**8 */ + break; + case 9: + wResult *= wResult; /* b**4 */ + wResult *= wResult; /* b**8 */ + wResult *= w1; /* b**9 */ + break; + case 10: + wResult *= wResult; /* b**4 */ + wResult *= w1; /* b**5 */ + wResult *= wResult; /* b**10 */ + break; + case 11: + wResult *= wResult; /* b**4 */ + wResult *= w1; /* b**5 */ + wResult *= wResult; /* b**10 */ + wResult *= w1; /* b**11 */ + break; + case 12: + wResult *= w1; /* b**3 */ + wResult *= wResult; /* b**6 */ + wResult *= wResult; /* b**12 */ + break; + case 13: + wResult *= w1; /* b**3 */ + wResult *= wResult; /* b**6 */ + wResult *= wResult; /* b**12 */ + wResult *= w1; /* b**13 */ + break; + case 14: + wResult *= w1; /* b**3 */ + wResult *= wResult; /* b**6 */ + wResult *= w1; /* b**7 */ + wResult *= wResult; /* b**14 */ + break; + case 15: + wResult *= w1; /* b**3 */ + wResult *= wResult; /* b**6 */ + wResult *= w1; /* b**7 */ + wResult *= wResult; /* b**14 */ + wResult *= w1; /* b**15 */ + break; + case 16: + wResult *= wResult; /* b**4 */ + wResult *= wResult; /* b**8 */ + wResult *= wResult; /* b**16 */ + break; + } + return wResult; +} +/* *---------------------------------------------------------------------- * * ExecuteExtendedBinaryMathOp, ExecuteExtendedUnaryMathOp -- @@ -7867,7 +7939,7 @@ ExecuteExtendedBinaryMathOp( Tcl_WideInt w1, w2, wResult; mp_int big1, big2, bigResult, bigRemainder; Tcl_Obj *objResultPtr; - int invalid, numPos, zero; + int invalid, zero; long shift; (void) GetNumberFromObj(NULL, valuePtr, &ptr1, &type1); @@ -7901,9 +7973,9 @@ ExecuteExtendedBinaryMathOp( return constants[0]; } - if (type2 != TCL_NUMBER_BIG) { + if (type2 == TCL_NUMBER_INT) { Tcl_WideInt wQuotient, wRemainder; - Tcl_GetWideIntFromObj(NULL, value2Ptr, &w2); + w2 = *((const Tcl_WideInt *)ptr2); wQuotient = w1 / w2; /* @@ -8023,9 +8095,9 @@ ExecuteExtendedBinaryMathOp( * Handle shifts within the native wide range. */ - if ((type1 != TCL_NUMBER_BIG) + if ((type1 == TCL_NUMBER_INT) && ((size_t)shift < CHAR_BIT*sizeof(Tcl_WideInt))) { - TclGetWideIntFromObj(NULL, valuePtr, &w1); + w1 = *((const Tcl_WideInt *)ptr1); if (!((w1>0 ? w1 : ~w1) & -(((Tcl_WideInt)1) << (CHAR_BIT*sizeof(Tcl_WideInt) - 1 - shift)))) { @@ -8089,16 +8161,7 @@ ExecuteExtendedBinaryMathOp( if (opcode == INST_LSHIFT) { mp_mul_2d(&big1, shift, &bigResult); } else { - mp_init(&bigRemainder); - mp_div_2d(&big1, shift, &bigResult, &bigRemainder); - if (mp_isneg(&bigRemainder)) { - /* - * Convert to Tcl's integer division rules. - */ - - mp_sub_d(&bigResult, 1, &bigResult); - } - mp_clear(&bigRemainder); + mp_tc_div_2d(&big1, shift, &bigResult); } mp_clear(&big1); BIG_RESULT(&bigResult); @@ -8107,139 +8170,23 @@ ExecuteExtendedBinaryMathOp( case INST_BITOR: case INST_BITXOR: case INST_BITAND: - if ((type1 == TCL_NUMBER_BIG) || (type2 == TCL_NUMBER_BIG)) { - mp_int *First, *Second; - + if ((type1 != TCL_NUMBER_INT) || (type2 != TCL_NUMBER_INT)) { Tcl_TakeBignumFromObj(NULL, valuePtr, &big1); Tcl_TakeBignumFromObj(NULL, value2Ptr, &big2); - /* - * Count how many positive arguments we have. If only one of the - * arguments is negative, store it in 'Second'. - */ - - if (!mp_isneg(&big1)) { - numPos = 1 + !mp_isneg(&big2); - First = &big1; - Second = &big2; - } else { - First = &big2; - Second = &big1; - numPos = (!mp_isneg(First)); - } mp_init(&bigResult); switch (opcode) { case INST_BITAND: - switch (numPos) { - case 2: - /* - * Both arguments positive, base case. - */ - - mp_and(First, Second, &bigResult); - break; - case 1: - /* - * First is positive; second negative: - * P & N = P & ~~N = P&~(-N-1) = P & (P ^ (-N-1)) - */ - - mp_neg(Second, Second); - mp_sub_d(Second, 1, Second); - mp_xor(First, Second, &bigResult); - mp_and(First, &bigResult, &bigResult); - break; - case 0: - /* - * Both arguments negative: - * a & b = ~ (~a | ~b) = -(-a-1|-b-1)-1 - */ - - mp_neg(First, First); - mp_sub_d(First, 1, First); - mp_neg(Second, Second); - mp_sub_d(Second, 1, Second); - mp_or(First, Second, &bigResult); - mp_neg(&bigResult, &bigResult); - mp_sub_d(&bigResult, 1, &bigResult); - break; - } + mp_tc_and(&big1, &big2, &bigResult); break; case INST_BITOR: - switch (numPos) { - case 2: - /* - * Both arguments positive, base case. - */ - - mp_or(First, Second, &bigResult); - break; - case 1: - /* - * First is positive; second negative: - * N|P = ~(~N&~P) = ~((-N-1)&~P) = -((-N-1)&((-N-1)^P))-1 - */ - - mp_neg(Second, Second); - mp_sub_d(Second, 1, Second); - mp_xor(First, Second, &bigResult); - mp_and(Second, &bigResult, &bigResult); - mp_neg(&bigResult, &bigResult); - mp_sub_d(&bigResult, 1, &bigResult); - break; - case 0: - /* - * Both arguments negative: - * a | b = ~ (~a & ~b) = -(-a-1&-b-1)-1 - */ - - mp_neg(First, First); - mp_sub_d(First, 1, First); - mp_neg(Second, Second); - mp_sub_d(Second, 1, Second); - mp_and(First, Second, &bigResult); - mp_neg(&bigResult, &bigResult); - mp_sub_d(&bigResult, 1, &bigResult); - break; - } + mp_tc_or(&big1, &big2, &bigResult); break; case INST_BITXOR: - switch (numPos) { - case 2: - /* - * Both arguments positive, base case. - */ - - mp_xor(First, Second, &bigResult); - break; - case 1: - /* - * First is positive; second negative: - * P^N = ~(P^~N) = -(P^(-N-1))-1 - */ - - mp_neg(Second, Second); - mp_sub_d(Second, 1, Second); - mp_xor(First, Second, &bigResult); - mp_neg(&bigResult, &bigResult); - mp_sub_d(&bigResult, 1, &bigResult); - break; - case 0: - /* - * Both arguments negative: - * a ^ b = (~a ^ ~b) = (-a-1^-b-1) - */ - - mp_neg(First, First); - mp_sub_d(First, 1, First); - mp_neg(Second, Second); - mp_sub_d(Second, 1, Second); - mp_xor(First, Second, &bigResult); - break; - } + mp_tc_xor(&big1, &big2, &bigResult); break; } @@ -8248,26 +8195,6 @@ ExecuteExtendedBinaryMathOp( BIG_RESULT(&bigResult); } - if ((type1 == TCL_NUMBER_INT) || (type2 == TCL_NUMBER_INT)) { - TclGetWideIntFromObj(NULL, valuePtr, &w1); - TclGetWideIntFromObj(NULL, value2Ptr, &w2); - - switch (opcode) { - case INST_BITAND: - wResult = w1 & w2; - break; - case INST_BITOR: - wResult = w1 | w2; - break; - case INST_BITXOR: - wResult = w1 ^ w2; - break; - default: - /* Unused, here to silence compiler warning. */ - wResult = 0; - } - WIDE_RESULT(wResult); - } w1 = *((const Tcl_WideInt *)ptr1); w2 = *((const Tcl_WideInt *)ptr2); @@ -8301,7 +8228,7 @@ ExecuteExtendedBinaryMathOp( dResult = pow(d1, d2); goto doubleResult; } - w2 = 0; + w1 = w2 = 0; /* to silence compiler warning (maybe-uninitialized) */ if (type2 == TCL_NUMBER_INT) { w2 = *((const Tcl_WideInt *) ptr2); if (w2 == 0) { @@ -8317,28 +8244,21 @@ ExecuteExtendedBinaryMathOp( return NULL; } - } - switch (type2) { - case TCL_NUMBER_INT: - w2 = *((const Tcl_WideInt *)ptr2); negativeExponent = (w2 < 0); oddExponent = (int) (w2 & (Tcl_WideInt)1); - break; - case TCL_NUMBER_BIG: + } else { Tcl_TakeBignumFromObj(NULL, value2Ptr, &big2); negativeExponent = mp_isneg(&big2); mp_mod_2d(&big2, 1, &big2); oddExponent = !mp_iszero(&big2); mp_clear(&big2); - break; } if (type1 == TCL_NUMBER_INT) { w1 = *((const Tcl_WideInt *)ptr1); - } - if (negativeExponent) { - if (type1 == TCL_NUMBER_INT) { + + if (negativeExponent) { switch (w1) { case 0: /* @@ -8359,17 +8279,21 @@ ExecuteExtendedBinaryMathOp( return constants[1]; } } + } + if (negativeExponent) { /* * Integers with magnitude greater than 1 raise to a negative * power yield the answer zero (see TIP 123). */ - return constants[0]; } - if (type1 == TCL_NUMBER_INT) { - switch (w1) { + if (type1 != TCL_NUMBER_INT) { + goto overflowExpon; + } + + switch (w1) { case 0: /* * Zero to a positive power is zero. @@ -8387,7 +8311,6 @@ ExecuteExtendedBinaryMathOp( return constants[1]; } WIDE_RESULT(-1); - } } /* @@ -8405,33 +8328,29 @@ ExecuteExtendedBinaryMathOp( return GENERAL_ARITHMETIC_ERROR; } - if (type1 == TCL_NUMBER_INT) { - if (w1 == 2) { - /* - * Reduce small powers of 2 to shifts. - */ + /* From here (up to overflowExpon) w1 and exponent w2 are wide-int's. */ + assert(type1 == TCL_NUMBER_INT && type2 == TCL_NUMBER_INT); - if ((Tcl_WideUInt) w2 < (Tcl_WideUInt) CHAR_BIT*sizeof(Tcl_WideInt) - 1) { - WIDE_RESULT(((Tcl_WideInt) 1) << (int)w2); - } - goto overflowExpon; + if (w1 == 2) { + /* + * Reduce small powers of 2 to shifts. + */ + + if ((Tcl_WideUInt) w2 < (Tcl_WideUInt) CHAR_BIT*sizeof(Tcl_WideInt) - 1) { + WIDE_RESULT(((Tcl_WideInt) 1) << (int)w2); } - if (w1 == -2) { - int signum = oddExponent ? -1 : 1; + goto overflowExpon; + } + if (w1 == -2) { + int signum = oddExponent ? -1 : 1; - /* - * Reduce small powers of 2 to shifts. - */ + /* + * Reduce small powers of 2 to shifts. + */ - if ((Tcl_WideUInt)w2 < CHAR_BIT*sizeof(Tcl_WideInt) - 1){ - WIDE_RESULT(signum * (((Tcl_WideInt) 1) << (int) w2)); - } - goto overflowExpon; + if ((Tcl_WideUInt)w2 < CHAR_BIT*sizeof(Tcl_WideInt) - 1){ + WIDE_RESULT(signum * (((Tcl_WideInt) 1) << (int) w2)); } - } - if (type1 == TCL_NUMBER_INT) { - w1 = *((const Tcl_WideInt *) ptr1); - } else { goto overflowExpon; } if (w2 - 2 < (long)MaxBase64Size @@ -8440,80 +8359,8 @@ ExecuteExtendedBinaryMathOp( /* * Small powers of integers whose result is wide. */ + wResult = WidePwrSmallExpon(w1, (long)w2); - wResult = w1 * w1; /* b**2 */ - switch (w2) { - case 2: - break; - case 3: - wResult *= w1; /* b**3 */ - break; - case 4: - wResult *= wResult; /* b**4 */ - break; - case 5: - wResult *= wResult; /* b**4 */ - wResult *= w1; /* b**5 */ - break; - case 6: - wResult *= w1; /* b**3 */ - wResult *= wResult; /* b**6 */ - break; - case 7: - wResult *= w1; /* b**3 */ - wResult *= wResult; /* b**6 */ - wResult *= w1; /* b**7 */ - break; - case 8: - wResult *= wResult; /* b**4 */ - wResult *= wResult; /* b**8 */ - break; - case 9: - wResult *= wResult; /* b**4 */ - wResult *= wResult; /* b**8 */ - wResult *= w1; /* b**9 */ - break; - case 10: - wResult *= wResult; /* b**4 */ - wResult *= w1; /* b**5 */ - wResult *= wResult; /* b**10 */ - break; - case 11: - wResult *= wResult; /* b**4 */ - wResult *= w1; /* b**5 */ - wResult *= wResult; /* b**10 */ - wResult *= w1; /* b**11 */ - break; - case 12: - wResult *= w1; /* b**3 */ - wResult *= wResult; /* b**6 */ - wResult *= wResult; /* b**12 */ - break; - case 13: - wResult *= w1; /* b**3 */ - wResult *= wResult; /* b**6 */ - wResult *= wResult; /* b**12 */ - wResult *= w1; /* b**13 */ - break; - case 14: - wResult *= w1; /* b**3 */ - wResult *= wResult; /* b**6 */ - wResult *= w1; /* b**7 */ - wResult *= wResult; /* b**14 */ - break; - case 15: - wResult *= w1; /* b**3 */ - wResult *= wResult; /* b**6 */ - wResult *= w1; /* b**7 */ - wResult *= wResult; /* b**14 */ - wResult *= w1; /* b**15 */ - break; - case 16: - wResult *= wResult; /* b**4 */ - wResult *= wResult; /* b**8 */ - wResult *= wResult; /* b**16 */ - break; - } WIDE_RESULT(wResult); } @@ -8622,9 +8469,9 @@ ExecuteExtendedBinaryMathOp( #endif DOUBLE_RESULT(dResult); } - if ((type1 != TCL_NUMBER_BIG) && (type2 != TCL_NUMBER_BIG)) { - TclGetWideIntFromObj(NULL, valuePtr, &w1); - TclGetWideIntFromObj(NULL, value2Ptr, &w2); + if ((type1 == TCL_NUMBER_INT) && (type2 == TCL_NUMBER_INT)) { + w1 = *((const Tcl_WideInt *)ptr1); + w2 = *((const Tcl_WideInt *)ptr2); switch (opcode) { case INST_ADD: @@ -9775,7 +9622,7 @@ EvalStatsCmd( for (i = 0; i < globalTablePtr->numBuckets; i++) { for (entryPtr = globalTablePtr->buckets[i]; entryPtr != NULL; entryPtr = entryPtr->nextPtr) { - if (NULL != Tcl_FetchIntRep(entryPtr->objPtr, &tclByteCodeType)) { + if (entryPtr->objPtr->typePtr == &tclByteCodeType) { numByteCodeLits++; } (void) TclGetStringFromObj(entryPtr->objPtr, &length); |
