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
7 changes: 7 additions & 0 deletions tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ class CastLogicHelper
return castIntToString(in, inLLVMType.getIntOrFloatBitWidth(), inType.isSignedInteger());
}

// a bigint is a signed i64; without this case it fell through to a bare inttoptr, and
// printing one passed its value to puts as an address
if (isa<mlir_ts::BigIntType>(inType) && isResString)
{
return castIntToString(in, inLLVMType.getIntOrFloatBitWidth(), true);
}

if ((inLLVMType.isF16() || inLLVMType.isF32() || inLLVMType.isF64() || inLLVMType.isF128()) && isResString)
{
if (inLLVMType.isF16())
Expand Down
6 changes: 6 additions & 0 deletions tslang/include/TypeScript/LowerToLLVM/CodeLogicHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class CodeLogicHelper
{
ftype = rewriter.getF16Type();
}
else if (width == 32)
{
// not the default: with NUMBER_F64 that is f64, and an f32 compared against it (an
// f32 cast to boolean, e.g. in the ___unbox<boolean> helper) is invalid IR
ftype = rewriter.getF32Type();
}
else if (width == 64)
{
ftype = rewriter.getF64Type();
Expand Down
16 changes: 16 additions & 0 deletions tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ class LLVMRTTIHelperVCLinux
types.push_back({linux::I32Type::typeName});
}

void setBoolAsCatchType()
{
types.push_back({linux::BoolType::typeName});
}

void setI64AsCatchType()
{
types.push_back({linux::I64Type::typeName});
}

void setStringTypeAsCatchType()
{
types.push_back({linux::StringType::typeName});
Expand Down Expand Up @@ -108,6 +118,10 @@ class LLVMRTTIHelperVCLinux
{
setI32AsCatchType();
}
else if (intType.getIntOrFloatBitWidth() == 64)
{
setI64AsCatchType();
}
else
{
LLVM_DEBUG(llvm::dbgs() << "...unsupported throw/catch integer width: " << intType << "\n";);
Expand Down Expand Up @@ -135,6 +149,8 @@ class LLVMRTTIHelperVCLinux
setF32AsCatchType();
#endif
})
.Case<mlir_ts::BooleanType>([&](auto boolType) { setBoolAsCatchType(); })
.Case<mlir_ts::BigIntType>([&](auto bigIntType) { setI64AsCatchType(); })
.Case<mlir_ts::StringType>([&](auto stringType) { setStringTypeAsCatchType(); })
.Case<mlir_ts::ClassType>([&](auto classType) { setClassTypeAsCatchType(classType.getName().getValue()); })
.Case<mlir_ts::AnyType>([&](auto anyType) { setI8PtrAsCatchType(); })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ namespace I32Type
constexpr const auto *typeName = "_ZTIi";
} // namespace I32Type

namespace BoolType
{
constexpr const auto *typeName = "_ZTIb";
} // namespace BoolType

// also a bigint, which is an i64
namespace I64Type
{
constexpr const auto *typeName = "_ZTIx";
Expand Down
23 changes: 23 additions & 0 deletions tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ class LLVMRTTIHelperVCWin32
throwInfoRef = I32Type::throwInfoRef;
}

void setBoolAsCatchType()
{
types.push_back({BoolType::typeName, BoolType::typeInfoRef, BoolType::catchableTypeInfoRef, BoolType::catchableTypeSize});

catchableTypeInfoArrayRef = BoolType::catchableTypeInfoArrayRef;
throwInfoRef = BoolType::throwInfoRef;
}

void setBigIntAsCatchType()
{
types.push_back(
{BigIntType::typeName, BigIntType::typeInfoRef, BigIntType::catchableTypeInfoRef, BigIntType::catchableTypeSize});

catchableTypeInfoArrayRef = BigIntType::catchableTypeInfoArrayRef;
throwInfoRef = BigIntType::throwInfoRef;
}

void setStringTypeAsCatchType()
{
types.push_back({StringType::typeName, StringType::typeInfoRef, StringType::catchableTypeInfoRef, pointerSize()});
Expand Down Expand Up @@ -162,6 +179,10 @@ class LLVMRTTIHelperVCWin32
{
setI32AsCatchType();
}
else if (intType.getIntOrFloatBitWidth() == 64)
{
setBigIntAsCatchType();
}
else
{
LLVM_DEBUG(llvm::dbgs() << "...unsupported throw/catch integer width: " << intType << "\n";);
Expand Down Expand Up @@ -189,6 +210,8 @@ class LLVMRTTIHelperVCWin32
setF64AsCatchType();
#endif
})
.Case<mlir_ts::BooleanType>([&](auto boolType) { setBoolAsCatchType(); })
.Case<mlir_ts::BigIntType>([&](auto bigIntType) { setBigIntAsCatchType(); })
.Case<mlir_ts::StringType>([&](auto stringType) { setStringTypeAsCatchType(); })
.Case<mlir_ts::ClassType>([&](auto classType) { setClassTypeAsCatchType(classType.getName().getValue()); })
.Case<mlir_ts::AnyType>([&](auto anyType) { setI8PtrAsCatchType(); })
Expand Down
26 changes: 26 additions & 0 deletions tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32Const.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,32 @@ constexpr const auto *throwInfoRef = "_TI3H";
constexpr int catchableTypeSize = 4;
} // namespace I32Type

// `bool` - a boolean is an i1, stored as a byte
namespace BoolType
{
constexpr const auto *typeName = "._N";
constexpr const auto *typeInfoRef = "??_R0_N@8";
constexpr const auto *catchableTypeInfoRef = "_CT??_R0_N@81";
constexpr const auto *catchableTypeInfoRef2 = "_CT??_R0PEAX@_N81.box";
constexpr const auto *copyThunk2 = ".eh.copy.box._N";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA2_N.box";
constexpr const auto *throwInfoRef = "_TI2_N.box";
constexpr int catchableTypeSize = 1;
} // namespace BoolType

// `__int64` - a bigint is an i64
namespace BigIntType
{
constexpr const auto *typeName = "._J";
constexpr const auto *typeInfoRef = "??_R0_J@8";
constexpr const auto *catchableTypeInfoRef = "_CT??_R0_J@88";
constexpr const auto *catchableTypeInfoRef2 = "_CT??_R0PEAX@_J88.box";
constexpr const auto *copyThunk2 = ".eh.copy.box._J";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA2_J.box";
constexpr const auto *throwInfoRef = "_TI2_J.box";
constexpr int catchableTypeSize = 8;
} // namespace BigIntType

namespace StringType
{
constexpr const auto *typeName = ".PEAD";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ mlir::Value LogicOp(Operation *binOp, SyntaxKind op, mlir::Value left, mlir::Typ
{
return UndefTypeLogicalOp<StdIOpTy, V1, v1, StdFOpTy, V2, v2>(binOp, op, builder, typeConverter, compileOptions);
}
else if (leftType.isIntOrIndex() || isa<mlir_ts::BooleanType>(leftType) || isa<mlir_ts::CharType>(leftType))
// a bigint is an i64; without it here a comparison of two bigints fell to the "not
// applicable" warning at the bottom and was constant false - `u == v` of equal values
// included (comparing against a literal went through the literal's own integer type)
else if (leftType.isIntOrIndex() || isa<mlir_ts::BooleanType>(leftType) || isa<mlir_ts::CharType>(leftType) ||
isa<mlir_ts::BigIntType>(leftType))
{
auto value = builder.create<StdIOpTy>(loc, v1, left, right);
return value;
Expand Down
11 changes: 11 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class MLIRRTTIHelperVCLinux
types.push_back({linux::I32Type::typeName, TypeInfo::Value, -1});
}

void setBoolAsCatchType()
{
types.push_back({linux::BoolType::typeName, TypeInfo::Value, -1});
}

void setI64AsCatchType()
{
types.push_back({linux::I64Type::typeName, TypeInfo::Value, -1});
Expand Down Expand Up @@ -174,6 +179,8 @@ class MLIRRTTIHelperVCLinux
// compares against the int type_info - see TryOpLowering, SaveCatchVarOpLowering
setI32AsCatchType();
})
.Case<mlir_ts::BooleanType>([&](auto boolType) { setBoolAsCatchType(); })
.Case<mlir_ts::BigIntType>([&](auto bigIntType) { setI64AsCatchType(); })
.Case<mlir_ts::StringType>([&](auto stringType) { setStringTypeAsCatchType(); })
.Case<mlir_ts::ClassType>([&](auto classType) {
// we need all bases as well
Expand All @@ -192,6 +199,8 @@ class MLIRRTTIHelperVCLinux
// caught value throws again.
setI32AsCatchType();
setF64AsCatchType();
setBoolAsCatchType();
setI64AsCatchType();
setStringTypeAsCatchType();
setI8PtrAsCatchType();
})
Expand Down Expand Up @@ -253,6 +262,8 @@ class MLIRRTTIHelperVCLinux
setF32AsCatchType();
#endif
})
.Case<mlir_ts::BooleanType>([&](auto boolType) { setBoolAsCatchType(); })
.Case<mlir_ts::BigIntType>([&](auto bigIntType) { setI64AsCatchType(); })
.Case<mlir_ts::StringType>([&](auto stringType) { setStringTypeAsCatchType(); })
.Case<mlir_ts::ClassType>([&](auto classType) { setClassTypeAsCatchType(classType.getName().getValue()); })
// An untyped `catch (e)` / `catch (e: any)` has to catch everything, and on the
Expand Down
36 changes: 36 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ class MLIRRTTIHelperVCWin32
throwInfoRef = windows::I32Type::throwInfoRef;
}

void setBoolAsCatchType()
{
types.push_back({windows::BoolType::typeName, windows::BoolType::typeInfoRef, windows::BoolType::catchableTypeInfoRef,
windows::BoolType::catchableTypeSize});
types.push_back(boxEntry(windows::BoolType::catchableTypeInfoRef2, windows::BoolType::copyThunk2));

catchableTypeInfoArrayRef = windows::BoolType::catchableTypeInfoArrayRef;
throwInfoRef = windows::BoolType::throwInfoRef;
}

void setBigIntAsCatchType()
{
types.push_back({windows::BigIntType::typeName, windows::BigIntType::typeInfoRef, windows::BigIntType::catchableTypeInfoRef,
windows::BigIntType::catchableTypeSize});
types.push_back(boxEntry(windows::BigIntType::catchableTypeInfoRef2, windows::BigIntType::copyThunk2));

catchableTypeInfoArrayRef = windows::BigIntType::catchableTypeInfoArrayRef;
throwInfoRef = windows::BigIntType::throwInfoRef;
}

void setStringTypeAsCatchType()
{
types.push_back({windows::StringType::typeName, windows::StringType::typeInfoRef, windows::StringType::catchableTypeInfoRef,
Expand Down Expand Up @@ -231,6 +251,12 @@ class MLIRRTTIHelperVCWin32
{
setI32AsCatchType();
}
else if (intType.getIntOrFloatBitWidth() == 64)
{
setBigIntAsCatchType();
// a bigint literal is a plain i64; box it as the bigint it is
thrownType = mlir_ts::BigIntType::get(rewriter.getContext());
}
else
{
result = false;
Expand Down Expand Up @@ -258,6 +284,8 @@ class MLIRRTTIHelperVCWin32
setF32AsCatchType();
#endif
})
.Case<mlir_ts::BooleanType>([&](auto boolType) { setBoolAsCatchType(); })
.Case<mlir_ts::BigIntType>([&](auto bigIntType) { setBigIntAsCatchType(); })
.Case<mlir_ts::StringType>([&](auto stringType) { setStringTypeAsCatchType(); })
.Case<mlir_ts::ClassType>([&](auto classType) {
// we need all bases as well
Expand Down Expand Up @@ -291,6 +319,12 @@ class MLIRRTTIHelperVCWin32
{
setI32AsCatchType();
}
else if (intType.getIntOrFloatBitWidth() == 64)
{
setBigIntAsCatchType();
// a bigint literal is a plain i64; box it as the bigint it is
thrownType = mlir_ts::BigIntType::get(rewriter.getContext());
}
else
{
result = false;
Expand All @@ -313,6 +347,8 @@ class MLIRRTTIHelperVCWin32
setF32AsCatchType();
#endif
})
.Case<mlir_ts::BooleanType>([&](auto boolType) { setBoolAsCatchType(); })
.Case<mlir_ts::BigIntType>([&](auto bigIntType) { setBigIntAsCatchType(); })
.Case<mlir_ts::StringType>([&](auto stringType) { setStringTypeAsCatchType(); })
.Case<mlir_ts::ClassType>([&](auto classType) { setClassTypeAsCatchType(classType.getName().getValue()); })
.Case<mlir_ts::AnyType>([&](auto anyType) { setI8PtrAsCatchType(); })
Expand Down
7 changes: 7 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/TypeOfOpHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ class TypeOfOpHelper
return "boolean";
}

// without it a bigint had no descriptor, and boxing one into an `any` handed a null
// tag to BoxOp - a silent compiler crash (and ___unbox already tests for "bigint")
if (isa<mlir_ts::BigIntType>(type))
{
return "bigint";
}

// special case
if (isa<mlir_ts::TypePredicateType>(type))
{
Expand Down
14 changes: 11 additions & 3 deletions tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,27 @@ class ParseIntOpLowering : public TsLlvmPattern<mlir_ts::ParseIntOp>
TypeHelper th(rewriter);
LLVMCodeHelper ch(op, rewriter, getTypeConverter(), tsLlvmContext->compileOptions);

// A result wider than 32 bits (a bigint is an i64) needs the `long long` functions: `atoi`
// returns an int, and `strtol` a `long`, which is also 32 bits on Windows.
auto resultType = getTypeConverter()->convertType(op.getType());
auto wide = resultType.isIntOrFloat() && resultType.getIntOrFloatBitWidth() > 32;
auto parsedType = wide ? rewriter.getI64Type() : rewriter.getI32Type();

// Insert the `atoi` declaration if necessary.
auto i8PtrTy = th.getPtrType();
LLVM::LLVMFuncOp parseIntFuncOp;
if (transformed.getBase())
{
parseIntFuncOp = ch.getOrInsertFunction(
"strtol",
th.getFunctionType(rewriter.getI32Type(), {i8PtrTy, th.getPtrType(), rewriter.getI32Type()}));
wide ? "strtoll" : "strtol",
th.getFunctionType(parsedType, {i8PtrTy, th.getPtrType(), rewriter.getI32Type()}));
auto nullOp = rewriter.create<LLVM::ZeroOp>(op->getLoc(), th.getPtrType());
rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, parseIntFuncOp,
ValueRange{transformed.getArg(), nullOp, transformed.getBase()});
}
else
{
parseIntFuncOp = ch.getOrInsertFunction("atoi", th.getFunctionType(rewriter.getI32Type(), {i8PtrTy}));
parseIntFuncOp = ch.getOrInsertFunction(wide ? "atoll" : "atoi", th.getFunctionType(parsedType, {i8PtrTy}));
rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, parseIntFuncOp, ValueRange{transformed.getArg()});
}

Expand Down Expand Up @@ -5109,6 +5115,8 @@ struct SaveCatchVarOpLowering : public TsLlvmPattern<mlir_ts::SaveCatchVarOp>
SmallVector<std::pair<std::string, mlir::Type>> candidates{
{::typescript::linux::I32Type::typeName, mlir::IntegerType::get(ctx, 32, mlir::IntegerType::Signed)},
{::typescript::linux::F64Type::typeName, mlir_ts::NumberType::get(ctx)},
{::typescript::linux::BoolType::typeName, mlir_ts::BooleanType::get(ctx)},
{::typescript::linux::I64Type::typeName, mlir_ts::BigIntType::get(ctx)},
{::typescript::linux::StringType::typeName, mlir_ts::StringType::get(ctx)},
// a thrown `any` is already a box
{::typescript::linux::I8PtrType::typeName, mlir_ts::AnyType::get(ctx)}};
Expand Down
3 changes: 3 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ tslang_add_test(NAME test-compile-00-throw-inlined COMMAND test-runner "${PROJEC
tslang_add_test(NAME test-compile-00-try-catch-sequence-and-nesting COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_sequence_and_nesting.ts")
tslang_add_test(NAME test-compile-00-throw-number-untyped-catch COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_number_untyped_catch.ts")
tslang_add_test(NAME test-compile-00-catch-untyped-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_untyped_value.ts")
tslang_add_test(NAME test-compile-00-throw-bool-bigint COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_bool_bigint.ts")
tslang_add_test(NAME test-compile-00-using-nested-scopes COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00using_nested_scopes.ts")
tslang_add_test(NAME test-compile-00-using-unwind-progress COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00using_unwind_progress.ts")
tslang_add_test(NAME test-compile-00-break-continue-scope-exit COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00break_continue_scope_exit.ts")
Expand Down Expand Up @@ -815,6 +816,7 @@ tslang_add_test(NAME test-jit-00-throw-inlined COMMAND test-runner -jit "${PROJE
tslang_add_test(NAME test-jit-00-try-catch-sequence-and-nesting COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_catch_sequence_and_nesting.ts")
tslang_add_test(NAME test-jit-00-throw-number-untyped-catch COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_number_untyped_catch.ts")
tslang_add_test(NAME test-jit-00-catch-untyped-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_untyped_value.ts")
tslang_add_test(NAME test-jit-00-throw-bool-bigint COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_bool_bigint.ts")
tslang_add_test(NAME test-jit-00-using-nested-scopes COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00using_nested_scopes.ts")
tslang_add_test(NAME test-jit-00-using-unwind-progress COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00using_unwind_progress.ts")
tslang_add_test(NAME test-jit-00-break-continue-scope-exit COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00break_continue_scope_exit.ts")
Expand Down Expand Up @@ -1705,6 +1707,7 @@ set(TSLANG_CORPUS
00switch.ts
00symbol.ts
00throw_in_catch.ts
00throw_bool_bigint.ts
00throw_inlined.ts
00throw_number_untyped_catch.ts
00to_primitive_ops.ts
Expand Down
Loading
Loading