diff --git a/docs/how/cmake_bgfx/CMakeLists.txt b/docs/how/cmake_bgfx/CMakeLists.txt index 4e90792a9..66d75a905 100644 --- a/docs/how/cmake_bgfx/CMakeLists.txt +++ b/docs/how/cmake_bgfx/CMakeLists.txt @@ -80,8 +80,10 @@ if(TSLANG_MEMORY_MODEL STREQUAL "gc") list(APPEND TSLANG_LINK_LIBS gc) endif() +# legacy_stdio_definitions: printf, sprintf_s and the rest of stdio as functions compiled code can +# call (the UCRT has them only as inline wrappers in its headers) if(WIN32) - list(APPEND TSLANG_LINK_LIBS ntdll) + list(APPEND TSLANG_LINK_LIBS ntdll legacy_stdio_definitions) else() find_library(TSLANG_TINFO_LIB NAMES tinfo) list(APPEND TSLANG_LINK_LIBS LLVMDemangle stdc++ m pthread dl rt) diff --git a/docs/how/cmake_dll/CMakeLists.txt b/docs/how/cmake_dll/CMakeLists.txt index 12b1b0525..c039456cf 100644 --- a/docs/how/cmake_dll/CMakeLists.txt +++ b/docs/how/cmake_dll/CMakeLists.txt @@ -64,9 +64,11 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc") list(APPEND TSLANG_LINK_LIBS "gcdll/gc") endif() -# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows +# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows; legacy_stdio_definitions +# printf, sprintf_s and the rest of stdio as functions compiled code can call (the UCRT has them only +# as inline wrappers in its headers) if(WIN32) - list(APPEND TSLANG_LINK_LIBS "ntdll") + list(APPEND TSLANG_LINK_LIBS "ntdll" "legacy_stdio_definitions") else() list(APPEND TSLANG_LINK_LIBS "LLVMDemangle" "stdc++" "m" "pthread" "tinfo" "dl" "rt") endif() diff --git a/docs/how/cmake_tslang/CMakeLists.txt b/docs/how/cmake_tslang/CMakeLists.txt index 6654562c5..7812870d7 100644 --- a/docs/how/cmake_tslang/CMakeLists.txt +++ b/docs/how/cmake_tslang/CMakeLists.txt @@ -82,9 +82,11 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc") endif() endif() -# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows +# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows; legacy_stdio_definitions +# printf, sprintf_s and the rest of stdio as functions compiled code can call (the UCRT has them only +# as inline wrappers in its headers) if(WIN32) - list(APPEND TSLANG_LINK_LIBS "ntdll") + list(APPEND TSLANG_LINK_LIBS "ntdll" "legacy_stdio_definitions") else() list(APPEND TSLANG_LINK_LIBS "LLVMDemangle" "stdc++" "m" "pthread" "tinfo" "dl" "rt") endif() diff --git a/docs/how/cmake_winapp/CMakeLists.txt b/docs/how/cmake_winapp/CMakeLists.txt index 5dc58e0e9..1f467214f 100644 --- a/docs/how/cmake_winapp/CMakeLists.txt +++ b/docs/how/cmake_winapp/CMakeLists.txt @@ -29,7 +29,9 @@ target_link_directories( ${GCLIBPATH} ) -set (LIBS "gcmt-lib") +# legacy_stdio_definitions: printf, sprintf_s and the rest of stdio as functions compiled code can +# call (the UCRT has them only as inline wrappers in its headers) +set (LIBS "gcmt-lib" "legacy_stdio_definitions") target_link_libraries( ${PROJECT_NAME} diff --git a/tslang/include/TypeScript/DOM.h b/tslang/include/TypeScript/DOM.h index 116c31548..84888c8bb 100644 --- a/tslang/include/TypeScript/DOM.h +++ b/tslang/include/TypeScript/DOM.h @@ -192,7 +192,19 @@ class VariableDeclarationDOM invariant = value; }; + // A global that holds the address of the variable rather than the variable: another module's, + // imported from its library (-shared) - reads and writes go through it to the library's own + bool getIndirect() const + { + return indirect; + } + void setIndirect(bool value = true) + { + indirect = value; + } + protected: + bool indirect = false; bool readWrite; bool atomic; int ordering; diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 92913922b..6fc2adf85 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -362,6 +362,17 @@ // a C function pointer - one code pointer, not the code-and-context a TypeScript function value is. // Only a function that captures nothing can be passed there (TSLANG_ISSUES 29). #define CDECL_DECORATOR "cdecl" +// `@stdcall declare function GetCurrentProcessId(): u32`: f is __stdcall, as the Windows API is. On 32-bit +// Windows that is how it is called (the callee pops its arguments) and named (_GetCurrentProcessId@0, +// which is what kernel32.lib exports); on every other target it is the C convention, so the same +// declaration serves both. On a function with a body it is a callback the Windows API calls, as +// `@stdcall function entry(arg: Opaque): u32` for _beginthreadex. It marks the function +// STDCALL_ATTR_NAME, and CxxThrowCallingConvPass gives the function and its calls the convention. +#define STDCALL_DECORATOR "stdcall" +#define STDCALL_ATTR_NAME "tslang.stdcall" +// A library exports a `@stdcall` function, on 32-bit Windows, as a C function under its own name - which an +// importer's calls go through - and as itself under its name with this suffix, which an importer gives to C +#define STDCALL_EXPORT_SUFFIX "$stdcall" // The memory model a module is compiled for, a string literal: `if (__MEMORY_MODEL__ === "gc")` // generates the branch taken only (MLIRGenImpl::foldLiteralCompare) #define MEMORY_MODEL_IDENTIFIER "__MEMORY_MODEL__" diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRDeclarationPrinter.h b/tslang/include/TypeScript/MLIRLogic/MLIRDeclarationPrinter.h index af767cc3c..708826c0f 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRDeclarationPrinter.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRDeclarationPrinter.h @@ -25,7 +25,7 @@ namespace typescript void printVariableDeclaration(StringRef, NamespaceInfo::TypePtr, mlir::Type, bool, StringRef = StringRef()); void print(StringRef, NamespaceInfo::TypePtr, mlir_ts::FunctionType, StringRef = StringRef(), ArrayRef ownKeeps = {}, ArrayRef ownBorrows = {}, ArrayRef ownLends = {}, - bool ownRecord = false, ArrayRef ownImmortalizes = {}); + bool ownRecord = false, ArrayRef ownImmortalizes = {}, bool stdcall = false); void print(ClassInfo::TypePtr); void print(InterfaceInfo::TypePtr); void printGenericClass(NamespaceInfo::TypePtr, StringRef); diff --git a/tslang/include/TypeScript/Pass/CxxThrowCallingConvPass.h b/tslang/include/TypeScript/Pass/CxxThrowCallingConvPass.h index 609c4274e..c610826ce 100644 --- a/tslang/include/TypeScript/Pass/CxxThrowCallingConvPass.h +++ b/tslang/include/TypeScript/Pass/CxxThrowCallingConvPass.h @@ -9,7 +9,8 @@ namespace ts // _CxxThrowException is __stdcall on 32-bit x86 MSVC; its symbol is then __CxxThrowException@8. // Sets that convention on the declaration and on every call and invoke of it. A call whose // convention differs from its callee's is undefined behaviour, which InstCombine turns into - // `unreachable`, so this runs before the optimization pipeline. + // `unreachable`, so this runs before the optimization pipeline. A `@stdcall declare function` + // (the Windows API) is given the same convention, the same way. class CxxThrowCallingConvPass : public llvm::PassInfoMixin { public: diff --git a/tslang/include/TypeScript/VSCodeTemplate/Files.h b/tslang/include/TypeScript/VSCodeTemplate/Files.h index 48199bbfb..565b32bcf 100644 --- a/tslang/include/TypeScript/VSCodeTemplate/Files.h +++ b/tslang/include/TypeScript/VSCodeTemplate/Files.h @@ -352,7 +352,11 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc") list(APPEND TSLANG_LINK_LIBS "gc") endif() -if(NOT WIN32) +# printf, sprintf_s and the rest of stdio as functions compiled code can call: the UCRT has them only +# as inline wrappers in its headers, which code tslang compiles never sees. +if(WIN32) + list(APPEND TSLANG_LINK_LIBS "legacy_stdio_definitions") +else() list(APPEND TSLANG_LINK_LIBS "stdc++" "m" "pthread" "dl" "rt") endif() diff --git a/tslang/lib/TypeScript/DeclarationPrinter.cpp b/tslang/lib/TypeScript/DeclarationPrinter.cpp index dc5f3577e..6858f750c 100644 --- a/tslang/lib/TypeScript/DeclarationPrinter.cpp +++ b/tslang/lib/TypeScript/DeclarationPrinter.cpp @@ -455,7 +455,7 @@ namespace typescript void MLIRDeclarationPrinter::print(StringRef name, NamespaceInfo::TypePtr elementNamespace, mlir_ts::FunctionType funcType, StringRef dllName, ArrayRef ownKeeps, ArrayRef ownBorrows, ArrayRef ownLends, - bool ownRecord, ArrayRef ownImmortalizes) + bool ownRecord, ArrayRef ownImmortalizes, bool stdcall) { printNamespaceBegin(elementNamespace); @@ -467,6 +467,13 @@ namespace typescript newline(); } + // __stdcall on 32-bit Windows: the importer calls it so, and by the name it has there (_f@N) + if (stdcall) + { + os << "@" STDCALL_DECORATOR; + newline(); + } + printOwnMarkers(ownKeeps, ownBorrows, ownLends, ownRecord, /*hasThis=*/false, 0, ownImmortalizes); printFunction( diff --git a/tslang/lib/TypeScript/MLIRGenFunctions.cpp b/tslang/lib/TypeScript/MLIRGenFunctions.cpp index f376e31c9..c8c7cd3dc 100644 --- a/tslang/lib/TypeScript/MLIRGenFunctions.cpp +++ b/tslang/lib/TypeScript/MLIRGenFunctions.cpp @@ -1469,6 +1469,11 @@ namespace mlirgen globalOp->setAttr(name, attr); } } + + if (declared->hasAttr(STDCALL_ATTR_NAME) && compileOptions.targetInfo.stdcallDecoratesCxxThrow) + { + stdcallDynamicImports[globalName] = dllFuncName.str(); + } } } diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index cfa0d4805..6de6893ef 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -2273,6 +2273,17 @@ class MLIRGenImpl varDecl->setUsing(varClass.isUsing); + // another module's variable, imported from its library: the global holds its address + // (mlirGen(VariableDeclaration)'s initFunc), the name is the variable it points to + if (varClass.isDynamicImport) + { + if (auto refType = dyn_cast(type)) + { + varDecl->setType(refType.getElementType()); + varDecl->setIndirect(); + } + } + if (varClass.atomic) { varDecl->setAtomic(varClass.ordering, varClass.syncscope); @@ -3811,11 +3822,17 @@ class MLIRGenImpl addAttr(name, mlir::UnitAttr::get(builder.getContext())); } - if (name == "varargs") + if (name == "varargs") { addAttr("func.varargs", mlir::BoolAttr::get(builder.getContext(), true)); } + // a function of the Windows API, or one it calls back: __stdcall on 32-bit Windows (STDCALL_DECORATOR) + if (name == STDCALL_DECORATOR) + { + addAttr(STDCALL_ATTR_NAME, mlir::UnitAttr::get(builder.getContext())); + } + // a body's facts come from the body: the decorator is a declaration's alone if (name == NO_DROPS_DECORATOR) { @@ -10925,6 +10942,25 @@ class MLIRGenImpl EXIT_IF_FAILED_OR_NO_VALUE(result) auto value = V(result); + // A `@stdcall` function of another library given to C (a parameter of a C function type, @cdecl), on + // 32-bit Windows: C calls it as __stdcall, so it is the function itself, not the C function the + // library exports under its name for calls from TypeScript (stdcallDynamicImports) + if (!stdcallDynamicImports.empty() && isa_and_nonnull(operandsProcessingInfo.getReceiverType())) + { + auto loadOp = value.getDefiningOp(); + auto addressOp = loadOp ? loadOp.getReference().getDefiningOp() : mlir_ts::AddressOfOp(); + auto found = addressOp ? stdcallDynamicImports.find(addressOp.getGlobalName()) : stdcallDynamicImports.end(); + if (found != stdcallDynamicImports.end()) + { + auto location = loc(expression); + auto symbolName = V(mlirGenStringValue(location, found->second + STDCALL_EXPORT_SUFFIX, true)); + auto address = builder.create(location, getOpaqueType(), symbolName); + auto itself = cast(location, value.getType(), address, argGenContext); + EXIT_IF_FAILED_OR_NO_VALUE(itself) + value = V(itself); + } + } + if (expression == SyntaxKind::SpreadElement) { auto location = loc(expression); @@ -13255,8 +13291,20 @@ class MLIRGenImpl return mlir::Value(); } - auto address = builder.create(location, mlir_ts::RefType::get(value->getType()), - value->getName(), ::mlir::IntegerAttr()); + auto refType = mlir_ts::RefType::get(value->getType()); + mlir::Value address; + if (value->getIndirect()) + { + // the global holds the address of the variable: another module's, in its library + auto holder = builder.create(location, mlir_ts::RefType::get(refType), + value->getName(), ::mlir::IntegerAttr()); + address = builder.create(location, refType, holder); + } + else + { + address = builder.create(location, refType, value->getName(), ::mlir::IntegerAttr()); + } + if (asAddess) { return address; @@ -16351,7 +16399,8 @@ class MLIRGenImpl }; dp.print(funcProto->getNameWithoutNamespace(), elementNamespace, funcProto->getFuncType(), dllName, marker(OWN_DECLARED_KEEPS_ATTR_NAME), marker(OWN_DECLARED_BORROWS_ATTR_NAME), marker(OWN_DECLARED_LENDS_ATTR_NAME), - funcOp && funcOp->hasAttr(OWN_DECLARED_RECORD_ATTR_NAME), marker(OWN_DECLARED_IMMORTALIZES_ATTR_NAME)); + funcOp && funcOp->hasAttr(OWN_DECLARED_RECORD_ATTR_NAME), marker(OWN_DECLARED_IMMORTALIZES_ATTR_NAME), + funcOp && funcOp->hasAttr(STDCALL_ATTR_NAME)); declExports << ss.str().str(); } @@ -17541,6 +17590,11 @@ class MLIRGenImpl bool declarationMode; + // 32-bit Windows: the `@stdcall` functions of another library (-shared), by the global their address is + // kept in, and the name the library exports them under - a C function of that name, which calls them + // (STDCALL_EXPORT_SUFFIX). Given to C as a callback, one is looked up as itself instead (mlirGenOperand). + llvm::StringMap stdcallDynamicImports; + std::stringstream declExports; // generic class declarations must be re-imported as plain (non-".d.ts") source: unlike // declExports (parsed with a "partial.d.ts" filename, which makes the parser mark diff --git a/tslang/lib/TypeScript/MLIRGenVariables.cpp b/tslang/lib/TypeScript/MLIRGenVariables.cpp index 7a3337124..f8797ce41 100644 --- a/tslang/lib/TypeScript/MLIRGenVariables.cpp +++ b/tslang/lib/TypeScript/MLIRGenVariables.cpp @@ -878,28 +878,32 @@ namespace mlirgen auto referenceToStaticFieldOpaque = builder.create( location, getOpaqueType(), dllVarName); + // The variable is the library's own: the global holds its address, and every + // read and write goes through it (createVariableDeclaration makes the name + // indirect, globalVariableAccess loads through it). A copy loaded here was a + // snapshot of its value when the program started, which never saw what the + // library assigned to it later. + auto storageType = fieldType; if (varClass.isBoxed) { - // the resolved symbol's storage is a single boxed - // pointer (ObjectType lowers to `ptr`) to the real - // data, not the data inline - load the pointer, - // instead of reading sizeof(fieldType) bytes directly - // at the symbol's own address (which would read past a - // lone 8-byte slot). The variable is that pointer, as - // the exporter's is: a copy of the data it points to - // was a snapshot, which the exporter's object never saw - // a write to, nor a method call, and a cast to an - // interface copied again. See DeclarationPrinter.cpp's + // the library's storage is a single boxed pointer (ObjectType lowers to + // `ptr`) to the real data, not the data inline. See DeclarationPrinter.cpp's // @boxed emission. - auto boxedType = getObjectType(fieldType); - auto refToBoxed = cast(location, mlir_ts::RefType::get(boxedType), referenceToStaticFieldOpaque, genContext); - auto boxedPtr = V(builder.create(location, boxedType, refToBoxed)); - return std::make_tuple(boxedPtr.getType(), boxedPtr, TypeProvided::Yes); + storageType = getObjectType(fieldType); + } + else if (auto funcType = dyn_cast(fieldType); funcType && !funcType.isCVarArg()) + { + // an exported global holds a function as a hybrid one (adjustGlobalVariableType) + storageType = mlir_ts::HybridFunctionType::get(builder.getContext(), funcType); + } + + auto refToStorage = cast(location, mlir_ts::RefType::get(storageType), referenceToStaticFieldOpaque, genContext); + if (refToStorage.failed_or_no_value()) + { + return std::make_tuple(mlir::Type(), mlir::Value(), TypeProvided::No); } - auto refToTyped = cast(location, mlir_ts::RefType::get(fieldType), referenceToStaticFieldOpaque, genContext); - auto valueOfField = builder.create(location, fieldType, refToTyped); - return std::make_tuple(valueOfField.getType(), V(valueOfField), TypeProvided::Yes); + return std::make_tuple(V(refToStorage).getType(), V(refToStorage), TypeProvided::Yes); } } diff --git a/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp b/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp index 279c21795..1f0445bf2 100644 --- a/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp +++ b/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp @@ -1,6 +1,9 @@ #include "TypeScript/Pass/CxxThrowCallingConvPass.h" +#include "TypeScript/Defines.h" #include "TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32Const.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/IR/IRBuilder.h" #include "llvm/IR/InstrTypes.h" #include "llvm/IR/Module.h" #include "llvm/Support/ErrorHandling.h" @@ -11,9 +14,135 @@ using namespace llvm; namespace ts { + // `@stdcall` (STDCALL_ATTR_NAME): the convention on the function - which also gives its symbol the @N + // suffix the import library names - and on every direct call of it. Given to a C function as an argument + // (a callback: `_beginthreadex(..., entry, ...)`), it is the function itself, which C calls as __stdcall. + // Any other use, the function as a TypeScript value, is called as any function value is, the C way: it + // gets a C function that calls this one instead. A variadic function is the C convention, as in MSVC. + static bool applyDeclaredStdcall(Module &M) + { + SmallVector declared; + for (auto &F : M) + { + if (F.hasFnAttribute(STDCALL_ATTR_NAME)) + { + declared.push_back(&F); + } + } + + for (auto *F : declared) + { + F->removeFnAttr(STDCALL_ATTR_NAME); + if (F->isVarArg()) + { + continue; + } + + F->setCallingConv(CallingConv::X86_StdCall); + + // Exported from a library (-shared): an importer finds a function by its name at run time + // (SearchForAddressOfSymbol) and calls it through a pointer, the C way. So the library exports + // a C function that calls it under the plain name (`scale`), and the function itself as + // `scale$stdcall`, which an importer takes to give it to C as a callback. + if (!F->isDeclaration() && (F->hasFnAttribute("export") || F->hasFnAttribute(DLL_EXPORT))) + { + auto name = F->getName().str(); + auto *plain = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage, "\x01_" + name, M); + plain->setAttributes(F->getAttributes()); + plain->removeFnAttr(STDCALL_ATTR_NAME); + plain->addFnAttr("export"); + IRBuilder<> builder(BasicBlock::Create(M.getContext(), "", plain)); + SmallVector args; + for (auto &arg : plain->args()) + { + args.push_back(&arg); + } + + auto *call = builder.CreateCall(F, args); + call->setCallingConv(CallingConv::X86_StdCall); + call->setAttributes(F->getAttributes()); + if (call->getType()->isVoidTy()) + { + builder.CreateRetVoid(); + } + else + { + builder.CreateRet(call); + } + + auto *itself = GlobalAlias::create(F->getValueType(), F->getAddressSpace(), GlobalValue::ExternalLinkage, + "\x01_" + name + STDCALL_EXPORT_SUFFIX, F, &M); + itself->setDLLStorageClass(GlobalValue::DLLExportStorageClass); + } + + // a call of it, or it given to a C function (a declaration): uses that keep it as it is + auto keepsIt = [](Use &use) { + auto *call = dyn_cast(use.getUser()); + if (!call) + { + return false; + } + + if (call->isCallee(&use)) + { + return true; + } + + auto *called = call->getCalledFunction(); + return call->isArgOperand(&use) && called && called->isDeclaration(); + }; + + auto takenAsValue = false; + for (auto &use : F->uses()) + { + if (auto *call = dyn_cast(use.getUser()); call && call->isCallee(&use)) + { + call->setCallingConv(CallingConv::X86_StdCall); + } + else if (!keepsIt(use)) + { + takenAsValue = true; + } + } + + if (!takenAsValue) + { + continue; + } + + auto *callee = Function::Create(F->getFunctionType(), GlobalValue::InternalLinkage, F->getName() + ".cdecl", M); + callee->setAttributes(F->getAttributes()); + // this module's own: what exports the function does not export this + callee->removeFnAttr("export"); + callee->removeFnAttr(DLL_EXPORT); + F->replaceUsesWithIf(callee, [&](Use &use) { return !keepsIt(use); }); + + IRBuilder<> builder(BasicBlock::Create(M.getContext(), "", callee)); + SmallVector args; + for (auto &arg : callee->args()) + { + args.push_back(&arg); + } + + auto *call = builder.CreateCall(F, args); + call->setCallingConv(CallingConv::X86_StdCall); + call->setAttributes(F->getAttributes()); + if (call->getType()->isVoidTy()) + { + builder.CreateRetVoid(); + } + else + { + builder.CreateRet(call); + } + } + + return !declared.empty(); + } + llvm::PreservedAnalyses CxxThrowCallingConvPass::run(llvm::Module &M, llvm::ModuleAnalysisManager &AM) { - auto changed = false; + auto changed = applyDeclaredStdcall(M); // The CRT calls a CatchableType's copy function as a member function - `__thiscall` on // x86: the catch variable in ECX, the thrown object on the stack. Nothing in the module diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 83f07a56a..38208f27c 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1667,6 +1667,7 @@ endforeach() # A library whose root only declares and initializes a variable must not get an entry point of # its own: it is compiled with the same --emit=obj as the program linked beside it, and a `main` # in both fails to link. Only the program half is given --entry-point. +tslang_add_test(NAME test-compile-export-import-vars-live COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars_live.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars_live.ts") tslang_add_test(NAME test-compile-export-import-root-var COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_root_var.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_root_var.ts") tslang_add_test(NAME test-compile-export-import-class-interface COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_interface.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_interface.ts") # @linkname across a static link - the mode it is named for; the second is the live guard for an @@ -1686,6 +1687,13 @@ tslang_add_test(NAME test-compile-export-import-generators COMMAND test-runner " # export_os_suffix.linux.ts: the host's is compiled and linked beside it, and the import's # declarations for each target are those of its file (the OS function that file declares). if (WIN32) + # `@stdcall declare function`: the Windows API, called as it is on 32-bit Windows too (its x86 twin) + tslang_add_test(NAME test-compile-00-stdcall-winapi COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00stdcall_winapi.ts") + tslang_add_test(NAME test-jit-00-stdcall-winapi COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00stdcall_winapi.ts") + # and exported: another module calls them, and gives one to _beginthreadex, by the same convention + tslang_add_test(NAME test-compile-export-import-stdcall COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_stdcall.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_stdcall.ts") + tslang_add_test(NAME test-compile-shared-export-import-stdcall COMMAND test-runner -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_stdcall.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_stdcall.ts") + tslang_add_test(NAME test-jit-shared-export-import-stdcall COMMAND test-runner -jit -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_stdcall.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_stdcall.ts") tslang_add_test(NAME test-compile-export-import-os-suffix COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_os_suffix.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_os_suffix.win32.ts") elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux") tslang_add_test(NAME test-compile-export-import-os-suffix COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/import_os_suffix.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_os_suffix.linux.ts") @@ -2195,6 +2203,7 @@ tslang_add_test(NAME test-compile-shared-export-import-object-literal-structural tslang_add_test(NAME test-compile-shared-export-import-object-literal-structural-typed-extends-interface-triple COMMAND test-runner -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_structural_typed_extends_interface_triple.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_structural_typed_extends_interface_triple.ts") tslang_add_test(NAME test-compile-shared-export-import-object-literal-structural-typed-extends-interface-optional COMMAND test-runner -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_structural_typed_extends_interface_optional.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_structural_typed_extends_interface_optional.ts") tslang_add_test(NAME test-compile-shared-export-import-vars COMMAND test-runner -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars.ts") +tslang_add_test(NAME test-compile-shared-export-import-vars-live COMMAND test-runner -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars_live.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars_live.ts") tslang_add_test(NAME test-compile-shared-export-import-vars-2 COMMAND test-runner -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars2.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars2.ts") tslang_add_test(NAME test-compile-shared-export-import-enum COMMAND test-runner -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_enum.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_enum.ts") @@ -2253,6 +2262,7 @@ tslang_add_test(NAME test-jit-shared-export-import-object-literal-structural-typ tslang_add_test(NAME test-jit-shared-export-import-object-literal-structural-typed-extends-interface-triple COMMAND test-runner -jit -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_structural_typed_extends_interface_triple.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_structural_typed_extends_interface_triple.ts") tslang_add_test(NAME test-jit-shared-export-import-object-literal-structural-typed-extends-interface-optional COMMAND test-runner -jit -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_structural_typed_extends_interface_optional.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_structural_typed_extends_interface_optional.ts") tslang_add_test(NAME test-jit-shared-export-import-vars COMMAND test-runner -jit -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars.ts") +tslang_add_test(NAME test-jit-shared-export-import-vars-live COMMAND test-runner -jit -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars_live.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars_live.ts") tslang_add_test(NAME test-jit-shared-export-import-vars-2 COMMAND test-runner -jit -shared -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars2.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars2.ts") tslang_add_test(NAME test-jit-shared-export-import-enum COMMAND test-runner -jit -shared "${PROJECT_SOURCE_DIR}/test/tester/tests/import_enum.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_enum.ts") @@ -5972,6 +5982,8 @@ tslang_add_test(NAME test-compile-none-shared-export-import-object-literal-struc tslang_add_test(NAME test-compile-rc-shared-export-import-object-literal-structural-typed-extends-interface-optional COMMAND test-runner -shared -mm=rc -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_structural_typed_extends_interface_optional.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_structural_typed_extends_interface_optional.ts") tslang_add_test(NAME test-compile-none-shared-export-import-object-literal-structural-typed-extends-interface-optional COMMAND test-runner -shared -mm=none -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_structural_typed_extends_interface_optional.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_structural_typed_extends_interface_optional.ts") tslang_add_test(NAME test-compile-rc-shared-export-import-vars COMMAND test-runner -shared -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars.ts") +tslang_add_test(NAME test-compile-rc-shared-export-import-vars-live COMMAND test-runner -shared -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars_live.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars_live.ts") +tslang_add_test(NAME test-compile-own-shared-export-import-vars-live COMMAND test-runner -shared -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars_live.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars_live.ts") tslang_add_test(NAME test-compile-none-shared-export-import-vars COMMAND test-runner -shared -mm=none "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars.ts") tslang_add_test(NAME test-compile-rc-shared-export-import-vars-2 COMMAND test-runner -shared -mm=rc -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars2.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars2.ts") tslang_add_test(NAME test-compile-none-shared-export-import-vars-2 COMMAND test-runner -shared -mm=none -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars2.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars2.ts") @@ -6058,6 +6070,8 @@ tslang_add_test(NAME test-jit-none-shared-export-import-object-literal-structura tslang_add_test(NAME test-jit-rc-shared-export-import-object-literal-structural-typed-extends-interface-optional COMMAND test-runner -jit -shared -mm=rc -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_structural_typed_extends_interface_optional.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_structural_typed_extends_interface_optional.ts") tslang_add_test(NAME test-jit-none-shared-export-import-object-literal-structural-typed-extends-interface-optional COMMAND test-runner -jit -shared -mm=none -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_object_literal_structural_typed_extends_interface_optional.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_object_literal_structural_typed_extends_interface_optional.ts") tslang_add_test(NAME test-jit-rc-shared-export-import-vars COMMAND test-runner -jit -shared -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars.ts") +tslang_add_test(NAME test-jit-rc-shared-export-import-vars-live COMMAND test-runner -jit -shared -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars_live.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars_live.ts") +tslang_add_test(NAME test-jit-own-shared-export-import-vars-live COMMAND test-runner -jit -shared -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars_live.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars_live.ts") tslang_add_test(NAME test-jit-none-shared-export-import-vars COMMAND test-runner -jit -shared -mm=none "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars.ts") tslang_add_test(NAME test-jit-rc-shared-export-import-vars-2 COMMAND test-runner -jit -shared -mm=rc -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars2.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars2.ts") tslang_add_test(NAME test-jit-none-shared-export-import-vars-2 COMMAND test-runner -jit -shared -mm=none -gctors-as-method "${PROJECT_SOURCE_DIR}/test/tester/tests/import_vars2.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_vars2.ts") diff --git a/tslang/test/tester/test-runner.cpp b/tslang/test/tester/test-runner.cpp index 686442181..e2d431788 100644 --- a/tslang/test/tester/test-runner.cpp +++ b/tslang/test/tester/test-runner.cpp @@ -13,7 +13,9 @@ #define TYPESCRIPT_LIB "TypeScriptAsyncRuntime.lib " //#define LIBS "msvcrt" _D_ ".lib ucrt" _D_ ".lib " // static CRT (/MT[d]) to match LLVM/TypeScript runtime libs and gc.lib; mixing static+dynamic CRT crashes at startup -#define LIBS "libcmt" _D_ ".lib libvcruntime" _D_ ".lib libucrt" _D_ ".lib " +// legacy_stdio_definitions: printf and the rest of stdio as functions a program can call, which the +// UCRT has only as inline wrappers - x86's libucrt defines none of them (x64's happens to have printf) +#define LIBS "libcmt" _D_ ".lib libvcruntime" _D_ ".lib libucrt" _D_ ".lib legacy_stdio_definitions.lib " #else // for Ubuntu 20.04 add -ldl and optionally -rdynamic #define LIBS "-frtti -fexceptions -lstdc++ -lrt -ldl -lpthread -lm" diff --git a/tslang/test/tester/tests/00stdcall_winapi.ts b/tslang/test/tester/tests/00stdcall_winapi.ts new file mode 100644 index 000000000..21f2bafa3 --- /dev/null +++ b/tslang/test/tester/tests/00stdcall_winapi.ts @@ -0,0 +1,30 @@ +// The Windows API is __stdcall: on 32-bit Windows the callee pops its arguments, and kernel32.lib names +// each function _Name@. `@stdcall` says so of a declaration; on x64 it changes +// nothing. A call with the wrong convention leaves the stack off by the arguments' size every time, +// which a loop of calls turns into a crash. + +@stdcall +declare function GetCurrentProcessId(): u32; + +@stdcall +declare function MulDiv(number: s32, numerator: s32, denominator: s32): s32; + +function main() { + const id = GetCurrentProcessId(); + assert(id != 0, "a call with no arguments"); + + let sum = 0; + for (let i = 0; i < 1000; i++) { + sum += MulDiv(i, 6, 3); + } + + assert(sum == 999000, "calls with arguments, which the callee pops"); + + // taken as a value, it is called as any function value is + const muldiv = MulDiv; + assert(muldiv(10, 3, 2) == 15, "through a function value"); + const processId = GetCurrentProcessId; + assert(processId() == id, "the same process"); + + print("done."); +} diff --git a/tslang/test/tester/tests/00tslang_issue_29.ts b/tslang/test/tester/tests/00tslang_issue_29.ts index 6c99a911c..68b5811f8 100644 --- a/tslang/test/tester/tests/00tslang_issue_29.ts +++ b/tslang/test/tester/tests/00tslang_issue_29.ts @@ -3,7 +3,8 @@ // C function given a callback called garbage. A function declared @cdecl takes each callback // parameter as a plain C function pointer: a function, or an arrow that captures nothing. -@cdecl declare function qsort(base: Opaque, count: u64, size: u64, compare: (a: Opaque, b: Opaque) => s32): void; +// count and size are size_t: `index`, as wide as a pointer (u64 put 8 bytes each where a 32-bit qsort reads 4) +@cdecl declare function qsort(base: Opaque, count: index, size: index, compare: (a: Opaque, b: Opaque) => s32): void; function ascending(a: Opaque, b: Opaque): s32 { return Deref(>a) - Deref(>b); diff --git a/tslang/test/tester/tests/00tslang_issue_29_thread.ts b/tslang/test/tester/tests/00tslang_issue_29_thread.ts index ea1894eb6..0489493d6 100644 --- a/tslang/test/tester/tests/00tslang_issue_29_thread.ts +++ b/tslang/test/tester/tests/00tslang_issue_29_thread.ts @@ -2,11 +2,14 @@ // TypeScript function it was given, which sees the argument passed through to it. @cdecl declare function _beginthreadex(security: Opaque | null, stack: u32, start: (arg: Opaque) => u32, arg: Opaque, flags: u32, id: Opaque | null): Opaque; -declare function WaitForSingleObject(handle: Opaque, ms: u32): u32; -declare function CloseHandle(handle: Opaque): s32; +// the Windows API, and the thread's start, which _beginthreadex calls, are __stdcall (on 32-bit Windows +// that is how they are called and named) +@stdcall declare function WaitForSingleObject(handle: Opaque, ms: u32): u32; +@stdcall declare function CloseHandle(handle: Opaque): s32; let seen: s32 = 0; +@stdcall function entry(arg: Opaque): u32 { seen = Deref(>arg); return 0; diff --git a/tslang/test/tester/tests/export_os_suffix.win32.ts b/tslang/test/tester/tests/export_os_suffix.win32.ts index 6da3ecbbf..c5dc89072 100644 --- a/tslang/test/tester/tests/export_os_suffix.win32.ts +++ b/tslang/test/tester/tests/export_os_suffix.win32.ts @@ -1,6 +1,8 @@ // The Windows file of the module `import './export_os_suffix'` names (import_os_suffix.ts): an // import without an extension takes .win32.ts on Windows, .linux.ts on Linux. +// __stdcall, as the Windows API is: on 32-bit Windows kernel32.lib names it _GetCurrentProcessId@0 +@stdcall declare function GetCurrentProcessId(): u32; namespace O { diff --git a/tslang/test/tester/tests/export_stdcall.ts b/tslang/test/tester/tests/export_stdcall.ts new file mode 100644 index 000000000..442d029ae --- /dev/null +++ b/tslang/test/tester/tests/export_stdcall.ts @@ -0,0 +1,22 @@ +// The library side of import_stdcall.ts: `@stdcall` functions with a body, exported. On 32-bit Windows +// they are __stdcall and named so (_scale@4, _threadStart@4), and an importer has to call them the same +// way and by the same names: its declaration of them says @stdcall too. + +let started: s32 = 0; + +export function startedWith(): s32 { + return started; +} + +// called by the importer, directly +@stdcall +export function scale(value: s32): s32 { + return value * 3; +} + +// given by the importer to _beginthreadex, which calls it as __stdcall +@stdcall +export function threadStart(arg: Opaque): u32 { + started = Deref(>arg); + return 0; +} diff --git a/tslang/test/tester/tests/export_vars_live.ts b/tslang/test/tester/tests/export_vars_live.ts new file mode 100644 index 000000000..f5c2a27ca --- /dev/null +++ b/tslang/test/tester/tests/export_vars_live.ts @@ -0,0 +1,31 @@ +// variables a library assigns after the program starts, read and assigned by the module importing them +export let counter: s32 = 0; +export let title = "first"; +export let point = { x: 1, y: 2 }; +export let twice = (value: number) => value * 2; + +export class Holder { + constructor(public value: number) {} +} + +export let holder = new Holder(1); + +export function changeAll() { + counter = 42; + title = "second"; + point = { x: 10, y: 20 }; + twice = (value: number) => value * 20; + holder = new Holder(2); +} + +export function counterHere(): s32 { + return counter; +} + +export function titleHere(): string { + return title; +} + +export function pointXHere(): number { + return point.x; +} diff --git a/tslang/test/tester/tests/import_stdcall.ts b/tslang/test/tester/tests/import_stdcall.ts new file mode 100644 index 000000000..7252d0a60 --- /dev/null +++ b/tslang/test/tester/tests/import_stdcall.ts @@ -0,0 +1,28 @@ +import './export_stdcall' + +// `@stdcall` functions of another module: called here as __stdcall, by their decorated names on 32-bit +// Windows, and one given to _beginthreadex as a thread's start. + +@cdecl declare function _beginthreadex(security: Opaque | null, stack: u32, start: (arg: Opaque) => u32, arg: Opaque, flags: u32, id: Opaque | null): Opaque; +@stdcall declare function WaitForSingleObject(handle: Opaque, ms: u32): u32; +@stdcall declare function CloseHandle(handle: Opaque): s32; + +function main() { + let sum = 0; + for (let i = 0; i < 1000; i++) { + sum += scale(i); + } + + assert(sum == 1498500, "a direct call, which the callee's convention pops"); + + const viaValue = scale; + assert(viaValue(5) == 15, "through a function value"); + + const values: s32[] = [42]; + const thread = _beginthreadex(null, 0, threadStart, Ref(values[0]), 0, null); + WaitForSingleObject(thread, 0xFFFFFFFF); + CloseHandle(thread); + assert(startedWith() == 42, "a thread started with the other module's callback"); + + print("done."); +} diff --git a/tslang/test/tester/tests/import_vars_live.ts b/tslang/test/tester/tests/import_vars_live.ts new file mode 100644 index 000000000..a9ff66709 --- /dev/null +++ b/tslang/test/tester/tests/import_vars_live.ts @@ -0,0 +1,31 @@ +import './export_vars_live' + +function main() { + assert(counter == 0, "counter before"); + assert(title == "first", "title before"); + assert(point.x == 1, "point before"); + assert(twice(3) == 6, "twice before"); + assert(holder.value == 1, "holder before"); + + // what the library assigns, this module sees + changeAll(); + assert(counter == 42, "counter after"); + assert(title == "second", "title after"); + assert(point.x == 10 && point.y == 20, "point after"); + assert(twice(3) == 60, "twice after"); + assert(holder.value == 2, "holder after"); + + // what this module assigns, the library sees + counter = 7; + counter++; + title = "third"; + point.x = 11; + assert(counterHere() == 8, "counter assigned here"); + assert(titleHere() == "third", "title assigned here"); + assert(pointXHere() == 11, "point field assigned here"); + + point = { x: 30, y: 40 }; + assert(pointXHere() == 30, "point assigned here"); + + print("done."); +} diff --git a/tslang/test/wasm/wasm_skips.txt b/tslang/test/wasm/wasm_skips.txt index e828a2ac6..32b30e1a1 100644 --- a/tslang/test/wasm/wasm_skips.txt +++ b/tslang/test/wasm/wasm_skips.txt @@ -6,6 +6,7 @@ internals.ts # inline_asm with x86 instructions 00any_to_opaque_narrow.ts wasm32 # stores a u64 in an Opaque: a pointer is 4 bytes on wasm32 00tslang_issue_29_pthread.ts # starts threads through pthread_create: the host starts them only through Thread.spawn 00tslang_issue_29_thread.ts # starts threads through _beginthreadex: the host starts them only through Thread.spawn +00stdcall_winapi.ts # calls the Windows API (GetCurrentProcessId): the host has none of it 00cppvtable_hidden_return.ts # a @cpp class returning a struct needs the MSVC or a 64-bit Itanium ABI (excluded on i686 too) fetch.ts # (default library) fetch(): the host has no network, so the request throws json.ts none # (default library) builds a 100000-character string a character at a time: -mm=none frees none of the ~5 GB, more than a wasm memory holds diff --git a/tslang/tslang/exe.cpp b/tslang/tslang/exe.cpp index bb028fbae..141dde9d4 100644 --- a/tslang/tslang/exe.cpp +++ b/tslang/tslang/exe.cpp @@ -1095,6 +1095,12 @@ int linkBinary(const char *argv0, const LinkRequest &request, CompileOptions &co args.push_back("-Wl,-nodefaultlib:libcmt"); } + // printf and the rest of stdio as functions a program can call (a `@varargs declare function + // printf`): the UCRT has them only as inline wrappers in its headers. x86's libucrt defines none, and + // a 32-bit program calling printf failed to link (undefined symbol _printf); x64's libucrt happens + // to define printf, which it still takes, being ahead of this. + args.push_back("-llegacy_stdio_definitions"); + // Windows asks for elevation before starting a 32-bit program that has no // requestedExecutionLevel and whose name contains "setup", "install", "update" or "patch" // (installer detection), so `my_setup.exe` or `dispatch.exe` would never start. Link an