From 6f2f5583c4b45b845b99504cb0bd4accdafbbd98 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 8 Nov 2026 15:57:44 +0000 Subject: [PATCH] An integer literal past 64 bits is a number; an uncaught number reads as JS writes it An integer literal that no 64-bit integer holds was cut to its low 64 bits (123456789012345680000 read -5670419503621181000), and one past 128 bits was an error. It is now a `number`, the nearest double, as in JS: 123456789012345680000, 2^64 -> 18446744073709552000, 1e313's digits -> Infinity. A literal in [2^63, 2^64) stays a u64, now exactly: the signed string parse made it 65 bits wide, which took a 128-bit type and read back only its low word. Under the Windows JIT, an uncaught thrown number was described with `%.17g` (`9.9999999999999995e-08`). It now uses the shortest round-trip digits in JS layout, as `"" + x` does since #783 (`uncaught exception: 1e-7`). Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenExpressions.cpp | 5 -- tslang/lib/TypeScript/MLIRGenImpl.h | 33 ++++++--- tslang/test/tester/CMakeLists.txt | 5 +- .../tests/00integer_literal_past_64_bits.ts | 33 +++++++++ .../tester/uncaught/throw_number_shortest.ts | 7 ++ tslang/tslang/jit.cpp | 69 ++++++++++++++++++- 6 files changed, 135 insertions(+), 17 deletions(-) create mode 100644 tslang/test/tester/tests/00integer_literal_past_64_bits.ts create mode 100644 tslang/test/tester/uncaught/throw_number_shortest.ts diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index 82191a919..61724bb2b 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -2299,11 +2299,6 @@ namespace mlirgen ValueOrLogicalResult MLIRGenImpl::mlirGen(NumericLiteral numericLiteral, const GenContext &genContext) { auto attrVal = getNumericLiteralAttribute(numericLiteral); - if (!attrVal) - { - emitError(loc(numericLiteral), "integer literal is too large to represent (more than 128 bits)"); - return mlir::failure(); - } auto attrType = mlir::cast(attrVal).getType(); auto valueType = isa(attrType) ? getNumberType() : attrType; diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index e9ca4621f..318aeaf3e 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -11161,6 +11161,28 @@ class MLIRGenImpl { APSInt newVal(wstos(text)); + // a literal past 64 bits is a `number`, the nearest double, as in JS: taken as an integer it + // was cut to its low 64 bits (123456789012345680000 read -5670419503621181000), and past 128 + // bits it was an error + if (newVal.getActiveBits() > 64) + { +#ifdef NUMBER_F64 + auto floatType = builder.getF64Type(); +#else + auto floatType = builder.getF32Type(); +#endif + llvm::APFloat val(floatType.getFloatSemantics()); + val.convertFromAPInt(newVal, newVal.isSigned(), llvm::APFloat::rmNearestTiesToEven); + return builder.getFloatAttr(floatType, val); + } + + // [2^63, 2^64): a u64, exactly (`let max: u64 = 18446744073709551615`). The signed string + // parse makes it 65 bits wide, which took a 128-bit type and read back only its low word. + if (newVal.getActiveBits() == 64) + { + return mlir::IntegerAttr::get(builder.getIntegerType(64, false), llvm::APInt(newVal.trunc(64))); + } + auto unsignedVal = false; auto width = newVal.getBitWidth(); switch (width) @@ -11170,22 +11192,13 @@ class MLIRGenImpl // a literal in [2^31, 2^32) does not fit s32; it takes s64 (default branch), not u32, // so arithmetic with other integer literals does not wrap case 64: - case 128: unsignedVal = true; break; default: //if (width < 8) width = 8; else //if (width < 16) width = 16; else if (width < 32) width = 32; else - if (width < 64) width = 64; else - if (width < 128) width = 128; - else - { - // no mlir::Location available at this layer to emitError with; - // return a null Attribute and let the caller (which has the - // literal's location) report it cleanly instead of crashing. - return mlir::Attribute(); - } + width = 64; } auto type = builder.getIntegerType(width, !unsignedVal); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 48cbf9163..843abb2b7 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -453,6 +453,7 @@ tslang_add_test(NAME test-compile-00-switch COMMAND test-runner "${PROJECT_SOURC tslang_add_test(NAME test-compile-01-switch COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01switch.ts") tslang_add_test(NAME test-compile-00-strings COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00strings.ts") tslang_add_test(NAME test-compile-00-number-to-string COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00number_to_string.ts") +tslang_add_test(NAME test-compile-00-integer-literal-past-64-bits COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00integer_literal_past_64_bits.ts") tslang_add_test(NAME test-compile-00-tuple COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00tuple.ts") tslang_add_test(NAME test-compile-01-tuple COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01tuple.ts") tslang_add_test(NAME test-compile-00-tuple-named COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00tuple_named.ts") @@ -1122,6 +1123,7 @@ tslang_add_test(NAME test-jit-00-switch COMMAND test-runner -jit "${PROJECT_SOUR tslang_add_test(NAME test-jit-01-switch COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01switch.ts") tslang_add_test(NAME test-jit-00-strings COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00strings.ts") tslang_add_test(NAME test-jit-00-number-to-string COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00number_to_string.ts") +tslang_add_test(NAME test-jit-00-integer-literal-past-64-bits COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00integer_literal_past_64_bits.ts") tslang_add_test(NAME test-jit-00-tuple COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00tuple.ts") tslang_add_test(NAME test-jit-01-tuple COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01tuple.ts") tslang_add_test(NAME test-jit-00-tuple-named COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00tuple_named.ts") @@ -5819,7 +5821,8 @@ if (WIN32) set(uncaught_expected_throw_string "error: uncaught exception: not caught") set(uncaught_expected_throw_class "error: uncaught exception: Failure") set(uncaught_expected_throw_number "error: uncaught exception: 2.5") - foreach(uncaught_name throw_string throw_class throw_number) + set(uncaught_expected_throw_number_shortest "error: uncaught exception: 1e-7") + foreach(uncaught_name throw_string throw_class throw_number throw_number_shortest) foreach(uncaught_model gc rc none) add_test(NAME test-jit-${uncaught_model}-uncaught-${uncaught_name} COMMAND $ --emit=jit --no-default-lib -mm=${uncaught_model} diff --git a/tslang/test/tester/tests/00integer_literal_past_64_bits.ts b/tslang/test/tester/tests/00integer_literal_past_64_bits.ts new file mode 100644 index 000000000..7b23522f7 --- /dev/null +++ b/tslang/test/tester/tests/00integer_literal_past_64_bits.ts @@ -0,0 +1,33 @@ +// An integer literal past 64 bits is a `number`, the nearest double, as in JS: it was cut to its +// low 64 bits (123456789012345680000 read -5670419503621181000), and past 128 bits it was an error. +function main() { + const big = 123456789012345680000; + assert(`${big}` == "123456789012345680000", "past 64 bits"); + assert(big > 0, "past 64 bits: positive"); + assert(big / 1e20 == 1.234567890123457, "past 64 bits: arithmetic as a number"); + + // up to 2^64 - 1 a literal is still a u64, exactly + const twoTo63 = 9223372036854775808; + assert(`${twoTo63}` == "9223372036854775808", "2^63: u64"); + const maxU64 = 18446744073709551615; + assert(`${maxU64}` == "18446744073709551615", "2^64 - 1: u64"); + + const twoTo64 = 18446744073709551616; + assert(`${twoTo64}` == "18446744073709552000", "2^64"); + + const negative = -123456789012345680000; + assert(`${negative}` == "-123456789012345680000", "negative past 64 bits"); + + const past128 = 1000000000000000000000000000000000000000000; + assert(`${past128}` == "1e+42", "past 128 bits"); + + const pastDouble = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000; + assert(`${pastDouble}` == "Infinity", "past the largest double"); + + // still an integer below 2^63 + const fits = 9007199254740993; + assert(`${fits}` == "9007199254740993", "fits s64: stays an integer"); + + print(big, twoTo63); + print("done."); +} diff --git a/tslang/test/tester/uncaught/throw_number_shortest.ts b/tslang/test/tester/uncaught/throw_number_shortest.ts new file mode 100644 index 000000000..e039ae854 --- /dev/null +++ b/tslang/test/tester/uncaught/throw_number_shortest.ts @@ -0,0 +1,7 @@ +// nothing catches it: the error says the number as the program prints it, `1e-7`, not `%.17g`'s +// 9.9999999999999995e-08 +function main() { + print("before"); + const tenth: number = 0.1; + throw tenth * 1e-6; +} diff --git a/tslang/tslang/jit.cpp b/tslang/tslang/jit.cpp index 7b68489ff..c6699d3de 100644 --- a/tslang/tslang/jit.cpp +++ b/tslang/tslang/jit.cpp @@ -23,7 +23,9 @@ #include "llvm/Support/Process.h" #include +#include #include +#include #include #include #ifdef _WIN32 @@ -660,6 +662,71 @@ using JitEntryThunkFn = int (*)(int, char **); // What an uncaught exception threw, said after "uncaught exception: ", or empty static char uncaughtDescription[512]; +// A thrown number as the program would print it (Number::toString, as ConvertLogic's +// __ts_number_to_string): the fewest digits that read back as the same double, plain decimal for +// a decimal exponent n in -6 < n <= 21, `d.ddde+N` / `e-N` otherwise. `%.17g` said `0.1` as +// 0.10000000000000001. +static void numberText(char *out, size_t size, double value) +{ + if (value != value) + { + snprintf(out, size, "NaN"); + return; + } + + if (value == 0) + { + snprintf(out, size, "0"); + return; + } + + auto *sign = value < 0 ? "-" : ""; + auto magnitude = value < 0 ? -value : value; + if (magnitude > DBL_MAX) + { + snprintf(out, size, "%sInfinity", sign); + return; + } + + char scientific[32]; + int decimals = 0; + for (; decimals < 16; decimals++) + { + snprintf(scientific, sizeof(scientific), "%.*e", decimals, magnitude); + if (strtod(scientific, nullptr) == magnitude) + { + break; + } + } + + if (decimals == 16) + { + snprintf(scientific, sizeof(scientific), "%.*e", decimals, magnitude); + } + + char digits[32]; + snprintf(digits, sizeof(digits), "%c%.*s", scientific[0], decimals, scientific + 2); + int k = decimals + 1; + int n = atoi(strchr(scientific, 'e') + 1) + 1; + static const char zeros[] = "000000000000000000000"; + if (k <= n && n <= 21) + { + snprintf(out, size, "%s%s%.*s", sign, digits, n - k, zeros); + } + else if (0 < n && n <= 21) + { + snprintf(out, size, "%s%.*s.%s", sign, n, digits, digits + n); + } + else if (-6 < n && n <= 0) + { + snprintf(out, size, "%s0.%.*s%s", sign, -n, zeros, digits); + } + else + { + snprintf(out, size, "%s%c%s%se%+d", sign, digits[0], k > 1 ? "." : "", digits + 1, n - 1); + } +} + // The thrown value, read from the exception record of a throw (_CxxThrowException's): the object, and its // ThrowInfo, whose first catchable type names its type (`.N` a number, `.PEAD` a string, `.PEAVName@@` a class). // Read while the object is alive, in the filter; a record not shaped so is left undescribed. @@ -704,7 +771,7 @@ static void describeUncaught(EXCEPTION_POINTERS *pointers) } else if (strcmp(name, ".N") == 0) { - snprintf(uncaughtDescription, size, "%.17g", *reinterpret_cast(object)); + numberText(uncaughtDescription, size, *reinterpret_cast(object)); } else if (strcmp(name, ".H") == 0) {