Skip to content

32-bit: link the default library into i686 programs - #340

Merged
ASDAlexander77 merged 3 commits into
mainfrom
x86-default-lib
Sep 22, 2026
Merged

ASDAlexander77 merged 3 commits into
mainfrom
x86-default-lib

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

This finishes 32-bit support. A 32-bit Windows program (-mtriple=i686-pc-windows-msvc) can now link and run against the default library, for gc, rc and none, debug and release, static and DLL. It no longer needs --no-default-lib. This was the one piece deferred from Phase 4; the spec's status line now says Phases 1-4 are implemented. The plan is in docs/superpowers/plans/2026-09-22-32-bit-x86-default-lib.md.

Merge this first. It is paired with a TypeScriptCompilerDefaultLib PR (branch x86-default-lib, stacked on DefaultLib PR #7) that builds and tests the x86 libraries.

Compiler changes

  • Where the library is looked for. getDefaultLibSubDir gains a required arch parameter. For a Windows x86 target the default library is taken from defaultlib/{lib,dll}/x86/{debug,release}/{gc,rc,none}/, the layout the spec gives. This is the same Windows-x86-only rule the gc and runtime libraries already follow. Every other target, x64 included, keeps its current path. jit.cpp and the --install-default-lib check pass the host, because the JIT refuses x86 anyway.
  • No fallback to the x64 tree. A missing x86 tree is an error that names the arch and the DefaultLib build command (TSLANG_ARCH=x86).
  • Machine check. On Windows x86 and x64, the compiler checks the COFF machine of the TypeScriptDefaultLib.lib it links, using the check it already applies to the gc and runtime libraries. A library built for the wrong CPU is now a named error rather than LNK4272 followed by a page of unresolved symbols.

Tests

  • check-x86-libs.sh gains three cases:
    • no x86 tree;
    • an x64 library where the x86 tree should be;
    • an x64 control, which prints SKIPPED if no x64 default library is built.
  • check-x86-run.sh gains a default-library section, which is skipped when the DefaultLib repo has no x86 tree. It runs tslang/test/x86/defaultlib_smoke.ts (Array, Map, string, Date, RegExp including replaceAll(regex, ...)) as a 32-bit exe under gc, rc and none, then the gc case again with --di. It also runs a gc --emit=dll that links the default library, plus an exe that imports it.
  • test-x86-compile-gc-defaultlib-collector is a new test, registered only under TSLANG_TEST_X86.

Found on the way

  • The replaceAll crash was a library bug. RegExp.replaceAll crashed at i686. lib.ts called the native regexp_replace with three of its four arguments. On x64 the missing argument happened to still be in a register; on x86 it was an uninitialised stack slot. It is fixed in the DefaultLib PR.
  • The compiler gap behind it is recorded as an open issue in the spec: tslang does not check the argument count of calls to declare functions (TypeScript's TS2554).
  • Another open issue recorded in the spec: "xxx".replaceAll("", "_") returns ___ where JavaScript gives _x_x_x_. This happens on both archs and predates this change.

Not covered

Neither CI nor the release workflows build x86 libraries, so the x86 test-runner twins still pass --no-default-lib, like the x64 ones.

Test plan

  • ctest -j 16 -C Release: 2771/2771. With TSLANG_TEST_X86=OFF, CTestTestfile.cmake changes only in _BACKTRACE_TRIPLES line numbers.
  • -DTSLANG_TEST_X86=ON, ctest -C Release -L x86: 1396 pass (the 1395 existing twins plus the new collector twin), 3 disabled.
  • check-x86-libs.sh 14/14. It also passes (exit 0) with the x64 default library made unfindable, and the x64 control case then prints SKIPPED.
  • check-x86-run.sh 21/21, with the default-library section running rather than skipped.
    • One run failed once on the rc smoke case (exit 127) and did not reproduce in three reruns.
  • x64 IR (compare-ir.sh): unchanged apart from the known nondeterministic files.
  • A DefaultLib test compiled for x64 and run against the real default library.
  • DefaultLib repo, TSLANG_ARCH=x86 tests.ps1: 150/150 release, 150/150 debug.
  • Linux CI. The change is limited to Windows x86, and x64 paths are unchanged.

🤖 Generated with Claude Code

ASDAlexander77 and others added 3 commits September 22, 2026 01:02
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
getDefaultLibSubDir gains a required arch parameter: for a Windows x86
target it inserts an "x86" segment outermost under lib/ or dll/, giving
defaultlib/{lib,dll}/x86/{debug,release}/{gc,rc,none}/, the same rule
getTargetLibDir already applies to the collector and the runtime. Every
other target, x64 included, keeps its existing flat layout - there is no
x64 segment and no fallback from x86 to the x64 tree.

exe.cpp computes x86DefaultLib from the triple already resolved for the
link, passes it through, and reports an x86-specific message (naming the
TSLANG_ARCH=x86 build script) when the tree is missing. It also checks
the linked TypeScriptDefaultLib.lib's COFF machine against the target,
the same protection resolveWindowsLibPath gives the collector and
runtime, so a wrong-arch default library fails with a named reason
instead of a page of unresolved symbols. jit.cpp and defaultlib.cpp pass
an empty arch: the JIT is host-only and the install check verifies the
host build.

check-x86-libs.sh gains three cases for this path: a missing x86 tree,
an x64 library staged where the x86 tree is expected, and an x64 control
that must keep working unchanged. Verified: check-x86-libs.sh 14/14,
ctest 2771/2771, compare-ir.sh noise only (confirmed by rerunning the
same binary twice - the diffing file set changes between runs), and an
x64 DefaultLib test compiled and run against the real default library.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…spec

check-x86-run.sh gains a default-library section, skipped (not failed)
when TypeScriptCompilerDefaultLib has no x86 tree yet. It builds
defaultlib_smoke.ts (Array, Map, string, Date and RegExp, including the
replaceAll(regex, ...) overload whose missing native argument crashed i686) without
--no-default-lib, under gc, rc and none release, then the gc case again
against the debug tree with --di. It also builds a gc --emit=dll library
that links the default library and an exe that imports it - the shape
of defaultlib-collector.cmake's compile mode, reused in bash - copying
TypeScriptDefaultLib.dll and the x86 gc.dll next to the importing exe.

tester/CMakeLists.txt hand-registers test-x86-compile-gc-defaultlib-
collector (labelled x86) as the x86 twin of the existing compile-mode
default-library collector test, since that test uses plain add_test
rather than tslang_add_test and so gets no twin automatically.
defaultlib-collector.cmake's own skip test now checks the x86 tree when
an X86 flag is passed, so the twin skips correctly when the x86 default
library is absent. There is still no JIT twin: the JIT refuses an x86
target regardless of the default library.

Verified: check-x86-run.sh all ok, including the new section (not
skipped). With -DTSLANG_TEST_X86=ON, ctest -C Release -L x86: 1396/1399
pass (the 1395 existing twins plus the new collector twin), 3 disabled.
With it OFF, CTestTestfile.cmake is unchanged except for
_BACKTRACE_TRIPLES line numbers (this CMakeLists.txt now has more lines
above existing add_test calls), and ctest -j 16 -C Release: 2771/2771.

Spec updated: status line drops the x86-default-library exception,
Phase 4 gets an "As built" note (layout, TSLANG_ARCH, the machine
check, test counts, the replaceAll root cause), the "x86 default library"
open issue is closed, and the 4c bullet's stale "(the default library
has no x86 build)" parenthetical is removed. New open issues record
what stays uncovered: tslang has no arity check on declare-function
calls (TS2554) - what hid the replaceAll bug - the pre-existing
"xxx".replaceAll("", "_") bug, and that neither CI nor the release
workflows build an x86 default library, so the x86 test-runner twins
still pass --no-default-lib like their x64 counterparts.

check-x86-libs.sh no longer needs an x64 default-library build: the
two error-path cases use the x64 gc.lib as a stand-in (the directory
and machine checks run before any link), and only the x64 control case
needs the real library - it prints SKIPPED when that is absent.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 6900ee6 into main Sep 22, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the x86-default-lib branch September 22, 2026 17:07
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