From d085cff9f50461cbf37e5e019a7ee8216f35ecbb Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 15:58:17 +0000 Subject: [PATCH 1/6] x86: printf links, and @stdcall declares the Windows API Two link failures of 32-bit Windows programs: - printf (a `@varargs declare function printf`): the UCRT has printf only as an inline wrapper in its headers. x64's libucrt happens to define it, x86's does not (undefined symbol _printf). exe.cpp and test-runner link legacy_stdio_definitions.lib after libucrt; x64 still takes libucrt's. - The Windows API is __stdcall, and kernel32.lib names it so on x86 (_GetCurrentProcessId@0); a declaration had no way to say it. `@stdcall declare function` marks the function (tslang.stdcall), and on i686 Windows CxxThrowCallingConvPass gives it and its direct calls X86_StdCall - LLVM then names it with the @N - before the optimizations, which treat a mismatched convention as UB. A use as a value goes through a C function that calls it, so a function value is called as any other. On other targets it changes nothing; on a function with a body it is ignored with a warning; a variadic function stays cdecl, as in MSVC. Tests: 00stdcall_winapi.ts (x64, JIT, x86 twin) - GetCurrentProcessId and MulDiv, 1000 calls in a loop and calls through function values; export_os_suffix.win32.ts declares GetCurrentProcessId with @stdcall. ctest with TSLANG_TEST_X86: no new failures, the x86 c-varargs (5) and os-suffix twins fixed; Linux 7057/7057. Co-Authored-By: Claude Opus 5.5 --- tslang/include/TypeScript/Defines.h | 7 ++ .../TypeScript/Pass/CxxThrowCallingConvPass.h | 3 +- tslang/lib/TypeScript/MLIRGenImpl.h | 16 +++- .../CxxThrowCallingConvPass.cpp | 80 ++++++++++++++++++- tslang/test/tester/CMakeLists.txt | 3 + tslang/test/tester/test-runner.cpp | 4 +- tslang/test/tester/tests/00stdcall_winapi.ts | 30 +++++++ .../tester/tests/export_os_suffix.win32.ts | 2 + tslang/tslang/exe.cpp | 6 ++ 9 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 tslang/test/tester/tests/00stdcall_winapi.ts diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 92913922b..97d96c457 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -362,6 +362,13 @@ // 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. A declaration's alone: 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" // 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/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/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index cfa0d4805..f86fb6217 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -3811,11 +3811,25 @@ 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: __stdcall on 32-bit Windows (STDCALL_DECORATOR) + if (name == STDCALL_DECORATOR) + { + if (functionLikeDeclarationBaseAST->body) + { + emitWarning(location, "'@" STDCALL_DECORATOR "' is ignored on a function with a body: it is for a declared " + "function, one defined outside TypeScript"); + } + else + { + 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) { diff --git a/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp b/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp index 279c21795..41d8b37b2 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,84 @@ using namespace llvm; namespace ts { + // `@stdcall declare function` (STDCALL_ATTR_NAME): the convention on the declaration - which also + // gives its symbol the @N suffix the import library names - and on every direct call of it. Any other + // use, the function taken as a 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); + + auto isCallOfIt = [](Use &use) { + auto *call = dyn_cast(use.getUser()); + return call && call->isCallee(&use); + }; + + 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 + { + takenAsValue = true; + } + } + + if (!takenAsValue) + { + continue; + } + + auto *callee = Function::Create(F->getFunctionType(), GlobalValue::InternalLinkage, F->getName() + ".cdecl", M); + callee->setAttributes(F->getAttributes()); + F->replaceUsesWithIf(callee, [&](Use &use) { return !isCallOfIt(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..3ff99666d 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1686,6 +1686,9 @@ 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") 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") 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/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/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 From 79235500db4f51d5f945b7c22bd6e771985991ce Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 16:09:56 +0000 Subject: [PATCH 2/6] x86: issue-29 tests - size_t is index, and @stdcall on a callback The x86 twins of 00tslang_issue_29 (qsort) and 00tslang_issue_29_thread failed under every model: - qsort's count and size are size_t, declared u64: a 32-bit qsort read the wrong count and size, and the sort's assert failed. They are `index`, as wide as a pointer. - The thread test called WaitForSingleObject and CloseHandle with no @stdcall (an x86 link error), and _beginthreadex calls its start as __stdcall on 32-bit Windows. `@stdcall` now goes on a function with a body too: a callback the Windows API calls (`@stdcall function entry(arg: Opaque): u32` for _beginthreadex). It is defined with the convention (_entry@4); given to a C function as an argument it is the function itself, and any other use as a value goes through the C function that calls it, as for a declaration. It replaces the warning that ignored the decorator on a function with a body. ctest with TSLANG_TEST_X86: no new failures, the 8 issue-29 x86 twins fixed (17 x86 failures left, all @cpp classes); Linux 7057/7057. Co-Authored-By: Claude Opus 5.5 --- tslang/include/TypeScript/Defines.h | 5 ++-- tslang/lib/TypeScript/MLIRGenImpl.h | 12 ++------ .../CxxThrowCallingConvPass.cpp | 29 ++++++++++++++----- tslang/test/tester/tests/00tslang_issue_29.ts | 3 +- .../tester/tests/00tslang_issue_29_thread.ts | 7 +++-- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 97d96c457..bcbbe9180 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -365,8 +365,9 @@ // `@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. A declaration's alone: it marks the function STDCALL_ATTR_NAME, and -// CxxThrowCallingConvPass gives the function and its calls the convention. +// 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" // The memory model a module is compiled for, a string literal: `if (__MEMORY_MODEL__ === "gc")` diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index f86fb6217..bc80508dc 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -3816,18 +3816,10 @@ class MLIRGenImpl addAttr("func.varargs", mlir::BoolAttr::get(builder.getContext(), true)); } - // a function of the Windows API: __stdcall on 32-bit Windows (STDCALL_DECORATOR) + // a function of the Windows API, or one it calls back: __stdcall on 32-bit Windows (STDCALL_DECORATOR) if (name == STDCALL_DECORATOR) { - if (functionLikeDeclarationBaseAST->body) - { - emitWarning(location, "'@" STDCALL_DECORATOR "' is ignored on a function with a body: it is for a declared " - "function, one defined outside TypeScript"); - } - else - { - addAttr(STDCALL_ATTR_NAME, mlir::UnitAttr::get(builder.getContext())); - } + addAttr(STDCALL_ATTR_NAME, mlir::UnitAttr::get(builder.getContext())); } // a body's facts come from the body: the decorator is a declaration's alone diff --git a/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp b/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp index 41d8b37b2..01cfa28f0 100644 --- a/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp +++ b/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp @@ -14,10 +14,11 @@ using namespace llvm; namespace ts { - // `@stdcall declare function` (STDCALL_ATTR_NAME): the convention on the declaration - which also - // gives its symbol the @N suffix the import library names - and on every direct call of it. Any other - // use, the function taken as a 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. + // `@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; @@ -39,9 +40,21 @@ namespace ts F->setCallingConv(CallingConv::X86_StdCall); - auto isCallOfIt = [](Use &use) { + // 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()); - return call && call->isCallee(&use); + if (!call) + { + return false; + } + + if (call->isCallee(&use)) + { + return true; + } + + auto *called = call->getCalledFunction(); + return call->isArgOperand(&use) && called && called->isDeclaration(); }; auto takenAsValue = false; @@ -51,7 +64,7 @@ namespace ts { call->setCallingConv(CallingConv::X86_StdCall); } - else + else if (!keepsIt(use)) { takenAsValue = true; } @@ -64,7 +77,7 @@ namespace ts auto *callee = Function::Create(F->getFunctionType(), GlobalValue::InternalLinkage, F->getName() + ".cdecl", M); callee->setAttributes(F->getAttributes()); - F->replaceUsesWithIf(callee, [&](Use &use) { return !isCallOfIt(use); }); + F->replaceUsesWithIf(callee, [&](Use &use) { return !keepsIt(use); }); IRBuilder<> builder(BasicBlock::Create(M.getContext(), "", callee)); SmallVector args; 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; From 528e262b12f73260780efa91ae67b1efd577df70 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 16:24:51 +0000 Subject: [PATCH 3/6] x86: a @stdcall function with a body exported from one module and imported by another The exporting library keeps @stdcall on the declaration it prints into __decls and exports, besides the __stdcall function itself (_name@N), a C function under the plain name that an importer finds at run time and calls through a pointer, and the function itself as `name$stdcall`, which an importer looks up when it hands the function to C as a callback. Co-Authored-By: Claude Opus 5.5 --- tslang/include/TypeScript/Defines.h | 3 ++ .../MLIRLogic/MLIRDeclarationPrinter.h | 2 +- tslang/lib/TypeScript/DeclarationPrinter.cpp | 9 ++++- tslang/lib/TypeScript/MLIRGenFunctions.cpp | 5 +++ tslang/lib/TypeScript/MLIRGenImpl.h | 27 ++++++++++++- .../CxxThrowCallingConvPass.cpp | 38 +++++++++++++++++++ tslang/test/tester/CMakeLists.txt | 4 ++ tslang/test/tester/tests/export_stdcall.ts | 22 +++++++++++ tslang/test/tester/tests/import_stdcall.ts | 28 ++++++++++++++ 9 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 tslang/test/tester/tests/export_stdcall.ts create mode 100644 tslang/test/tester/tests/import_stdcall.ts diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index bcbbe9180..6fc2adf85 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -370,6 +370,9 @@ // 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/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 bc80508dc..701974b5f 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -10931,6 +10931,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); @@ -16357,7 +16376,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(); } @@ -17547,6 +17567,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/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp b/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp index 01cfa28f0..1f0445bf2 100644 --- a/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp +++ b/tslang/lib/TypeScriptExceptionPass/CxxThrowCallingConvPass.cpp @@ -40,6 +40,41 @@ namespace ts 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()); @@ -77,6 +112,9 @@ namespace ts 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)); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 3ff99666d..cbf2e0e67 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1689,6 +1689,10 @@ 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") 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/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."); +} From 86d4e3e3a0e7ccf2f393bd7efcba7755a165100c Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 16:36:04 +0000 Subject: [PATCH 4/6] -shared: an imported variable is the library's own, not a copy A module importing another's exported variable from its library loaded the value once, at start-up, into a global of its own: what the library assigned later it never saw, and what it assigned the library never saw. The global now holds the address of the library's variable, and reads and writes go through it (VariableDeclarationDOM::indirect, globalVariableAccess). A @boxed object is the library's slot as well, and a function-typed variable is read as the hybrid function the library stores. Co-Authored-By: Claude Opus 5.5 --- tslang/include/TypeScript/DOM.h | 12 +++++++ tslang/lib/TypeScript/MLIRGenImpl.h | 27 ++++++++++++-- tslang/lib/TypeScript/MLIRGenVariables.cpp | 38 +++++++++++--------- tslang/test/tester/CMakeLists.txt | 7 ++++ tslang/test/tester/tests/export_vars_live.ts | 31 ++++++++++++++++ tslang/test/tester/tests/import_vars_live.ts | 31 ++++++++++++++++ 6 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 tslang/test/tester/tests/export_vars_live.ts create mode 100644 tslang/test/tester/tests/import_vars_live.ts 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/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 701974b5f..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); @@ -13280,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; 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/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index cbf2e0e67..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 @@ -2202,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") @@ -2260,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") @@ -5979,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") @@ -6065,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/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_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."); +} From 5c1b5721db0d365f6f8375d41e147351f5ce02cc Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 16:41:03 +0000 Subject: [PATCH 5/6] cmake template: link legacy_stdio_definitions on Windows The UCRT has printf, sprintf_s and the rest of stdio only as inline wrappers in its headers, which code tslang compiles never sees; its import library defines none of them, on x64 or x86. A generated project whose TypeScript called printf linked only because main.cpp called std::printf too, which compiled a copy in; without that it failed with "unresolved external symbol printf". The template and the cmake samples in docs/how now link legacy_stdio_definitions on Windows, as tslang's own link does. Co-Authored-By: Claude Opus 5.5 --- docs/how/cmake_bgfx/CMakeLists.txt | 4 +++- docs/how/cmake_dll/CMakeLists.txt | 6 ++++-- docs/how/cmake_tslang/CMakeLists.txt | 6 ++++-- docs/how/cmake_winapp/CMakeLists.txt | 4 +++- tslang/include/TypeScript/VSCodeTemplate/Files.h | 6 +++++- 5 files changed, 19 insertions(+), 7 deletions(-) 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/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() From 511f0136c74c1cb1c760a01622f70bb0c36f898f Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 13 Nov 2026 17:10:15 +0000 Subject: [PATCH 6/6] wasm tests: skip 00stdcall_winapi.ts It calls the Windows API (GetCurrentProcessId), which the wasm host does not provide: every wasm leg failed with "the wasm host has no import env.GetCurrentProcessId". Co-Authored-By: Claude Opus 5.5 --- tslang/test/wasm/wasm_skips.txt | 1 + 1 file changed, 1 insertion(+) 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