Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tslang/lib/TypeScript/MLIRGenAccessCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,26 @@ namespace mlirgen
}
})
.Case<mlir_ts::ClassType>([&](auto classType) {
// `Number(x)`, `BigInt(x)`, `String(x)` and `Boolean(x)` called without `new` convert `x` to the
// primitive, as in JavaScript: made the class's object, `BigInt(42) + 1n` was an invalid cast
// of the bigint to the class
if (operands.size() == 1)
{
auto name = classType.getName().getValue();
mlir::Type primitiveType = name == "Number" ? mlir::Type(getNumberType())
: name == "BigInt" ? mlir::Type(getBigIntType())
: name == "String" ? mlir::Type(getStringType())
: name == "Boolean" ? mlir::Type(getBooleanType())
: mlir::Type();
if (primitiveType)
{
value = operands.front().getType() == primitiveType
? ValueOrLogicalResult(operands.front())
: cast(location, primitiveType, operands.front(), genContext);
return;
}
}

value = NewClassInstance(location, classType, operands, genContext);
})
.Case<mlir_ts::ClassStorageType>([&](auto classStorageType) {
Expand Down
16 changes: 13 additions & 3 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2178,11 +2178,21 @@ namespace mlirgen
*(bigIntLiteral->text.end() - 1) == S('n')
? bigIntLiteral->text.substr(0, bigIntLiteral->text.length() - 1)
: bigIntLiteral->text.c_str()));
// 64-bit whatever the target: a `bigint` literal is modeled as the language's own 64-bit
// integer type; its width is fixed by the language, not by the pointer width.
// 64-bit whatever the target: a `bigint` is the language's own 64-bit integer; its width is fixed
// by the language, not by the pointer width. A literal that does not fit was wrapped silently
// (12345678901234567890n printed -6101065172474983726).
if (newVal.getActiveBits() > 63)
{
emitError(loc(bigIntLiteral)) << "bigint literal '" << convertWideToUTF8(bigIntLiteral->text)
<< "' is out of range: a bigint is a 64-bit signed integer";
return mlir::failure();
}

// the literal is a `bigint`: widened, it is one (`x ?? 7n` of an optional bigint), and a union
// with bigint takes it (`5n` given for `bigint | string`); typed by its i64 it was neither
auto type = builder.getI64Type();
auto attrVal = mlir::IntegerAttr::get(type, newVal.getExtValue());
auto literalType = mlir_ts::LiteralType::get(attrVal, type);
auto literalType = mlir_ts::LiteralType::get(attrVal, getBigIntType());
return V(builder.create<mlir_ts::ConstantOp>(loc(bigIntLiteral), literalType, attrVal));
}

Expand Down
38 changes: 36 additions & 2 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5802,7 +5802,8 @@ class MLIRGenImpl
return llvm::all_of(unionType.getTypes(), [&](mlir::Type subType) { return isNeverNullish(subType); });
}

return mth.isValueType(type);
// a bigint is a 64-bit integer: compared with null it was an i64 against a pointer
return mth.isValueType(type) || isa<mlir_ts::BigIntType>(mth.stripLiteralType(type));
}

// the type without null and undefined: what is left once `?? b` did not take `b`
Expand Down Expand Up @@ -7853,9 +7854,42 @@ class MLIRGenImpl
CAST(rightExpressionValue, location, type, rightExpressionValue, genContext);
}

// cast step
// A bigint is a 64-bit integer, and so is what `/`, `%`, a shift or a bitwise operator makes of
// two of them: `10n / 3n` is 3n, not 3.33333 through a number, and `1n << 40n` is not shifted by
// 40 mod 32 through an i32.
auto isBigInt = [&](mlir::Value value) { return isa<mlir_ts::BigIntType>(mth.stripLiteralType(value.getType())); };
auto bigIntOperation = false;
switch (opCode)
{
case SyntaxKind::SlashToken:
case SyntaxKind::PercentToken:
case SyntaxKind::LessThanLessThanToken:
case SyntaxKind::GreaterThanGreaterThanToken:
case SyntaxKind::AmpersandToken:
case SyntaxKind::BarToken:
case SyntaxKind::CaretToken:
bigIntOperation = isBigInt(leftExpressionValue) || isBigInt(rightExpressionValue);
break;
default:
break;
}

if (bigIntOperation)
{
if (leftExpressionValue.getType() != getBigIntType())
{
CAST(leftExpressionValue, location, getBigIntType(), leftExpressionValue, genContext);
}

if (rightExpressionValue.getType() != getBigIntType())
{
CAST(rightExpressionValue, location, getBigIntType(), rightExpressionValue, genContext);
}
}

// cast step
switch (bigIntOperation ? SyntaxKind::CommaToken : opCode)
{
case SyntaxKind::CommaToken:
// no cast needed
break;
Expand Down
6 changes: 5 additions & 1 deletion tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ tslang_add_test(NAME test-compile-00-spread COMMAND test-runner "${PROJECT_SOURC
tslang_add_test(NAME test-compile-01-spread COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01spread.ts")
tslang_add_test(NAME test-compile-00-types-unknown1 COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_unknown1.ts")
tslang_add_test(NAME test-compile-00-any-to-tuple COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_tuple.ts")
tslang_add_test(NAME test-compile-00-bigint-ops COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00bigint_ops.ts")
tslang_add_test(NAME test-compile-00-any-literal-to-interface COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_literal_to_interface.ts")
tslang_add_test(NAME test-compile-00-unknown-round-trip COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00unknown_round_trip.ts")
tslang_add_test(NAME test-compile-00-types-indexed-access-type COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_indexedaccesstype.ts")
Expand Down Expand Up @@ -1377,6 +1378,7 @@ tslang_add_test(NAME test-jit-00-spread COMMAND test-runner -jit "${PROJECT_SOUR
tslang_add_test(NAME test-jit-01-spread COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01spread.ts")
tslang_add_test(NAME test-jit-00-types-unknown1 COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_unknown1.ts")
tslang_add_test(NAME test-jit-00-any-to-tuple COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_tuple.ts")
tslang_add_test(NAME test-jit-00-bigint-ops COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00bigint_ops.ts")
tslang_add_test(NAME test-jit-00-any-literal-to-interface COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_literal_to_interface.ts")
tslang_add_test(NAME test-jit-00-unknown-round-trip COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00unknown_round_trip.ts")
tslang_add_test(NAME test-jit-00-types-indexed-access-type COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00types_indexedaccesstype.ts")
Expand Down Expand Up @@ -2479,6 +2481,7 @@ set(TSLANG_CORPUS
00template_literal_escapes.ts
00types_unknown1.ts
00any_to_tuple.ts
00bigint_ops.ts
00any_literal_to_interface.ts
00unknown_round_trip.ts
00types_utility.ts
Expand Down Expand Up @@ -3768,7 +3771,8 @@ set_tests_properties(test-compile-diagnostic-location-enum-member-initializer
set(diagnostic_location_statement_cases
"for_of|for_of\\.ts:6:5: error: Can't resolve property 'length' of type s32"
"for_in|for_in\\.ts:6:5: error: Can't resolve property 'length' of type s32"
"using|using\\.ts:5:11: error: Can't resolve property 'dispose' of type s32")
"using|using\\.ts:5:11: error: Can't resolve property 'dispose' of type s32"
"bigint_literal_out_of_range|bigint_literal_out_of_range\\.ts:3:15: error: bigint literal '12345678901234567890n' is out of range")
foreach(diagnostic_location_case ${diagnostic_location_statement_cases})
string(REPLACE "|" ";" diagnostic_location_parts "${diagnostic_location_case}")
list(GET diagnostic_location_parts 0 diagnostic_location_name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// A bigint is a 64-bit signed integer: a literal that does not fit is an error, not wrapped.
function main() {
const a = 12345678901234567890n;
print(a);
}
43 changes: 43 additions & 0 deletions tslang/test/tester/tests/00bigint_ops.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// A bigint is the language's 64-bit integer. Its literal is a `bigint` (it was an i64): widened it is one,
// so `x ?? 7n` of an optional bigint is a bigint (it was `bigint | i64`, and the null test an i64 against a
// pointer), and `5n` given for `bigint | string` is its bigint member (an invalid cast). `/`, `%`, shifts
// and bitwise operators stay 64-bit integers: `10n / 3n` was 3.33333 through a number, and `1n << 40n`
// shifted by 40 mod 32 through an i32.
function orSeven(x?: bigint) {
return x ?? 7n;
}

function next(u: bigint | string) {
if (typeof u == "bigint") {
return u + 1n;
}

return 0n;
}

function main() {
const big = 1234567890123456789n;
assert(`${big}` == "1234567890123456789", "a literal near the top of the range");

assert(orSeven() == 7n, "?? of no value");
assert(orSeven(3n) == 3n, "?? of a value");

assert(next(5n) == 6n, "a literal given for bigint | string");
assert(next("s") == 0n, "a string given for bigint | string");

const a = 10n;
const b = 3n;
assert(a / b == 3n, "division truncates");
assert(-a / b == -3n, "toward zero");
assert(a % b == 1n && -a % b == -1n, "remainder");
assert(1n << 40n == 1099511627776n, "a shift past 32 bits");
assert((1n << 62n) >> 60n == 4n, "a right shift");
assert((a & 6n) == 2n && (a | 5n) == 15n && (a ^ 3n) == 9n, "bitwise");

let c = 100n;
c /= 7n;
c <<= 33n;
assert(c == 120259084288n, "compound assignments");

print("done.");
}
Loading