Repository navigation
x86: printf links, and @stdcall declares the Windows API - #842
Merged
Merged
Conversation
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
…orted 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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
This was referenced Nov 13, 2026
ASDAlexander77
added a commit
that referenced
this pull request
Nov 13, 2026
The test #842 added declares GetCurrentProcessId and MulDiv from kernel32; ctest runs it on Windows only and the wasm runner skips it, but the Android runner tried to build it for every tester leg, and the four failed links failed the release's "Build Android tests" step. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes two link failures in 32-bit Windows programs.
printf
A
@varargs declare function printffailed to link on x86 withundefined symbol: _printf. The UCRT providesprintfonly as an inline wrapper in its headers. The x64libucrt.libhappens to contain a callableprintf; the x86 one doesn't.exe.cppandtest-runnernow linklegacy_stdio_definitions.lib, Microsoft's library for this case, afterlibucrt. On x64,printfstill resolves fromlibucrtfirst, so x64 links don't change.@stdcallThe Windows API uses
__stdcall, and on x86kernel32.libexports names such as_GetCurrentProcessId@0. Adeclare functioncouldn't say that, so the x86 link failed. The new decorator marks a declaration as__stdcall:CxxThrowCallingConvPasssetsX86_StdCallon the declaration and on every direct call to it, and LLVM adds the@Nto the symbol. The pass runs before optimization, because the optimizer treats a mismatched calling convention as undefined behavior.const f = MulDiv) goes through a small cdecl wrapper, so calls through a function value stay correct.@stdcallalso works on a function with a body, for a callback the Windows API calls, such as a_beginthreadexthread start:@stdcall function entry(arg: Opaque): u32. The function is compiled__stdcall(_entry@4). Passed directly as an argument to a C function, it goes in as itself; any other use as a TypeScript value goes through the cdecl wrapper.@stdcallfunction with a body that is exported from one module and imported by another doesn't work on x86 yet: the importer's declaration doesn't carry@stdcall.issue-29 x86 tests (second commit)
00tslang_issue_29.tsdeclared qsort'ssize_tparameters asu64, so a 32-bit qsort read the wrong count and size. They're nowindex, the pointer-sized type.00tslang_issue_29_thread.tsnow uses@stdcallonWaitForSingleObject,CloseHandleand the thread startentry.Tests
00stdcall_winapi.ts, run on x64, in the JIT and on x86. It callsGetCurrentProcessIdand makes 1000MulDivcalls in a loop, where a wrong convention would unbalance the stack. It also calls both through function values.export_os_suffix.win32.tsnow declaresGetCurrentProcessIdwith@stdcall.TSLANG_TEST_X86: no new failures. Fourteen x86 tests now pass: the five c-varargs tests, os-suffix, and the eight issue-29 tests. The 17 x86 failures left are all@cppclass tests, which are an error on i686 by design.tsbindgen still skips
__stdcallfunctions; it could generate@stdcalldeclarations for them instead. That isn't part of this PR.🤖 Generated with Claude Code