Skip to content

x86: printf links, and @stdcall declares the Windows API - #842

Merged
ASDAlexander77 merged 6 commits into
mainfrom
x86-stdcall-printf
Nov 13, 2026
Merged

ASDAlexander77 merged 6 commits into
mainfrom
x86-stdcall-printf

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

This fixes two link failures in 32-bit Windows programs.

printf

A @varargs declare function printf failed to link on x86 with undefined symbol: _printf. The UCRT provides printf only as an inline wrapper in its headers. The x64 libucrt.lib happens to contain a callable printf; the x86 one doesn't.

exe.cpp and test-runner now link legacy_stdio_definitions.lib, Microsoft's library for this case, after libucrt. On x64, printf still resolves from libucrt first, so x64 links don't change.

@stdcall

The Windows API uses __stdcall, and on x86 kernel32.lib exports names such as _GetCurrentProcessId@0. A declare function couldn't say that, so the x86 link failed. The new decorator marks a declaration as __stdcall:

@stdcall
declare function GetCurrentProcessId(): u32;
  • 32-bit Windows: CxxThrowCallingConvPass sets X86_StdCall on the declaration and on every direct call to it, and LLVM adds the @N to the symbol. The pass runs before optimization, because the optimizer treats a mismatched calling convention as undefined behavior.
  • Function values: a use as a value (const f = MulDiv) goes through a small cdecl wrapper, so calls through a function value stay correct.
  • Other targets: the decorator changes nothing.
  • Callbacks (second commit): @stdcall also works on a function with a body, for a callback the Windows API calls, such as a _beginthreadex thread 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.
  • Limits: a variadic function stays cdecl, as in MSVC. A @stdcall function 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.ts declared qsort's size_t parameters as u64, so a 32-bit qsort read the wrong count and size. They're now index, the pointer-sized type.
  • 00tslang_issue_29_thread.ts now uses @stdcall on WaitForSingleObject, CloseHandle and the thread start entry.

Tests

  • New test: 00stdcall_winapi.ts, run on x64, in the JIT and on x86. It calls GetCurrentProcessId and makes 1000 MulDiv calls in a loop, where a wrong convention would unbalance the stack. It also calls both through function values.
  • Updated test: export_os_suffix.win32.ts now declares GetCurrentProcessId with @stdcall.
  • Windows full suite with 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 @cpp class tests, which are an error on i686 by design.
  • Linux: 7057/7057.

tsbindgen still skips __stdcall functions; it could generate @stdcall declarations for them instead. That isn't part of this PR.

🤖 Generated with Claude Code

ASDAlexander77 and others added 6 commits November 13, 2026 15:58
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>
@ASDAlexander77
ASDAlexander77 merged commit 2ae1f69 into main Nov 13, 2026
8 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant