From 9db20255a19e2ae1674d61ecee7f665d68a46676 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Tue, 22 Sep 2026 01:02:49 +0100 Subject: [PATCH 1/3] Add implementation plan for the 32-bit x86 default library Co-Authored-By: Claude Opus 5 --- .../2026-09-22-32-bit-x86-default-lib.md | 225 ++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 docs/superpowers/plans/2026-09-22-32-bit-x86-default-lib.md diff --git a/docs/superpowers/plans/2026-09-22-32-bit-x86-default-lib.md b/docs/superpowers/plans/2026-09-22-32-bit-x86-default-lib.md new file mode 100644 index 000000000..16d341edf --- /dev/null +++ b/docs/superpowers/plans/2026-09-22-32-bit-x86-default-lib.md @@ -0,0 +1,225 @@ +# 32-bit x86 Default Library Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** A 32-bit Windows program (`-mtriple=i686-pc-windows-msvc`) links and runs against the default library, for every model (gc, rc, none), debug and release, static and DLL. It no longer needs `--no-default-lib`. + +**Architecture:** Two repos. The compiler (`I:\TypeScriptCompiler`) gains an arch segment in `getDefaultLibSubDir`. That gives `defaultlib/{lib,dll}/x86/{debug,release}/{gc,rc,none}/` for Windows x86, while every other target keeps today's path. The compiler also checks the COFF machine of the default library it links. The default-library repo (`I:\TypeScriptCompilerDefaultLib`) gets `TSLANG_ARCH=x86`: its build scripts produce that tree, and `tests.ps1` runs the repo's 150 tests as 32-bit exes. One x86-only bug the probe found (`string_replaceAll`) is root-caused and fixed. + +**Tech Stack:** C++17 (tslang driver), Windows batch, PowerShell, CMake/CTest, bash check scripts. + +**Spec:** `docs/superpowers/specs/2026-09-20-32-bit-compilation-design.md`: "Arch in the library layout", "Phase 4 — Default library and the suite", and the open issue "The x86 default library (deferred from phase 4)". + +## Global Constraints + +- Layout (spec, verbatim): `defaultlib/{lib,dll}/x86/{debug,release}/{gc,rc,none}/`. The x64 paths keep their shape. There is no `x64` segment. +- Strict, no fallback (spec): a missing arch tree is an error that names the arch and the script that builds it. It never falls back to another arch. +- `getDefaultLibSubDir` stays the ONE place the path is composed. Every consumer calls it and never spells the path out. +- The arch segment applies to Windows x86 only, the same rule as `getTargetLibDir` / `resolveWindowsLibPath` in `tslang/tslang/exe.cpp`. The JIT refuses x86, so `jit.cpp` always passes the host (no segment). +- x64 behaviour must be byte-for-byte unchanged. Proof: the default suite `ctest -j 16 -C Release` stays green (2771), and `tslang/test/compare-ir.sh` shows noise only. +- Commit trailer: `Co-Authored-By: Claude Opus 5 `. GPG signing: retry, pause, `gpgconf --kill scdaemon`/`gpg-agent`, then hand the staged commit to the user with a message file. Never bypass signing. +- Branches: compiler `x86-default-lib`, from `origin/main` with `--no-track`. For DefaultLib, use `x86-default-lib` from `origin/main` if PR #7 (`debug-crt-for-native-wrappers`) has merged; otherwise branch from `debug-crt-for-native-wrappers` and say so in the PR (it edits the same `build_core.bat`). +- Use Edit/Write for any file containing backslashes (batch, PowerShell). The Bash heredoc mangles `\1` etc. Scan new files for control characters. + +## Probe results (2026-09-22, compiler main b277622c) + +- `lib.ts` and `lib.win32.ts` compile for i686 under gc, rc and none with no source change. +- The wrappers compile with `vcvarsall.bat x86` + `cl /MT`. `tslang --emit=dll -mtriple=i686-pc-windows-msvc --gc-lib-path=3rdParty\gcdll\x64\release\lib` links (the compiler appends `x86`). `lib.exe` archives the static lib. +- The 150 DefaultLib tests, release/gc, as x86 exes against that static lib: 149 pass. `string_replaceAll` segfaults (exit 139) after its first line; x64 passes. The failing call is `paragraph.replaceAll(/Dog/gi, "ferret")`. The string-pattern `replaceAll` works. +- Not probed: debug, rc, none, DLL linking, jit (refused, by design). + +## File map + +Compiler repo: +- `tslang/include/TypeScript/Defines.h`: `getDefaultLibSubDir` gains a required `arch` parameter, plus `DEFAULT_LIB_ARCH_X86`. +- `tslang/tslang/exe.cpp`: passes the arch, gives an x86-specific missing-tree error, and checks the machine of `TypeScriptDefaultLib.lib`. +- `tslang/tslang/jit.cpp`, `tslang/tslang/defaultlib.cpp`: pass `""` (host / x64). +- `tslang/test/check-x86-libs.sh`: default-lib error-path cases. +- `tslang/test/check-x86-run.sh`: default-lib run cases (skipped if no x86 default lib). +- `tslang/test/tester/CMakeLists.txt`: x86 twin of `test-compile-gc-defaultlib-collector` under `TSLANG_TEST_X86`. +- `docs/superpowers/specs/2026-09-20-32-bit-compilation-design.md`: record as built, close the open issue. +- The `string_replaceAll` fix location is decided by Task 4's root cause. + +DefaultLib repo: +- `scripts/build_core.bat`: `TSLANG_ARCH` (x64 default | x86), vcvarsall arch, `-mtriple`, output and staging paths. +- `scripts/build_llvm.bat`: `-m32` for clang-cl under x86. +- `build.bat`: documents `TSLANG_ARCH`. +- `tests.ps1`: `TSLANG_ARCH=x86` runs compile mode only, as 32-bit exes. +- `README.md`: how to build and test x86. + +--- + +### Task 1: Compiler looks for the default library in the x86 tree + +**Files:** +- Modify: `tslang/include/TypeScript/Defines.h:240-265` +- Modify: `tslang/tslang/exe.cpp` (default-lib block around line 660-715; `isWindowsX86Target`/`checkWindowsBinaryMachine` exist at ~135 and ~340) +- Modify: `tslang/tslang/jit.cpp:~400`, `tslang/tslang/defaultlib.cpp:~146` +- Test: `tslang/test/check-x86-libs.sh` + +**Interfaces:** +- Produces: `std::string getDefaultLibSubDir(bool shared, bool debugBuild, const char *memoryModel, const char *arch)`. `arch` is `""` for no segment or `DEFAULT_LIB_ARCH_X86` (`"x86"`). The result is `defaultlib//[x86/]/`. +- Produces: the error text `no x86 default library built for -mm=: does not exist` (Task 5 greps it). + +- [ ] **Step 1: Write the failing checks.** Add these to `check-x86-libs.sh`, following its existing case style (read the script first; it builds fake lib trees in a temp dir). Compile `tslang/test/x86/await_order.ts` or any hello-world `--emit=exe -mtriple=i686-pc-windows-msvc` WITHOUT `--no-default-lib`, and pass a `--default-lib-path` pointing at: + 1. a tree that has only x64 `defaultlib/lib/release/gc/TypeScriptDefaultLib.lib` (copy from `I:\TypeScriptCompilerDefaultLib\__build\defaultlib`). Expect a nonzero exit and stderr containing `no x86 default library built for -mm=gc` and the path `defaultlib/lib/x86/release/gc`. + 2. a tree with that x64 `.lib` copied into `defaultlib/lib/x86/release/gc/`. Expect a nonzero exit and stderr containing `is built for x64` (the `checkWindowsBinaryMachine` wording; confirm it against `Dump::coffMachineName`). + 3. x64 control: the same program without `-mtriple`, against tree 1, still compiles (exit 0). + +- [ ] **Step 2: Run to verify it fails.** Run `bash tslang/test/check-x86-libs.sh`. Expected: cases 1 and 2 FAIL (today the compiler takes `defaultlib/lib/release/gc` and the link fails with LNK4272/unresolved symbols, not our message). Case 3 passes. + +- [ ] **Step 3: Implement.** In `Defines.h`: + +```cpp +// ...and, for a Windows x86 target, per arch, outermost under lib/ or dll/ like every other x86 +// library (spec "Arch in the library layout"). Every other target, x64 included, has no arch +// segment, so existing trees keep their shape. There is no fallback to the x64 tree: an x64 +// library links into an x86 program only to fail on every symbol. +#define DEFAULT_LIB_ARCH_X86 "x86" + +inline std::string getDefaultLibSubDir(bool shared, bool debugBuild, const char *memoryModel, const char *arch) +{ + std::string subDir = std::string(DEFAULT_LIB_DIR "/") + (shared ? DEFAULT_LIB_KIND_SHARED : DEFAULT_LIB_KIND_STATIC) + "/"; + if (arch && *arch) + { + subDir += std::string(arch) + "/"; + } + + return subDir + (debugBuild ? DEFAULT_LIB_BUILD_DIR_DEBUG : DEFAULT_LIB_BUILD_DIR_RELEASE) + "/" + memoryModel; +} +``` + +Update the layout comment above it to `defaultlib/{lib,dll}/[x86/]{debug,release}/{gc,rc,none}/`. + +In `exe.cpp`, in the `!compileOptions.noDefaultLib` block, compute the arch as `win && arch == llvm::Triple::x86 ? DEFAULT_LIB_ARCH_X86 : ""` (the local `arch`/`win` variables already exist in that function; rename locally if they shadow). Pass it to `getDefaultLibSubDir`. The missing-directory error becomes arch-aware: + +```cpp +llvm::errs() << "error: no " << (x86DefaultLib ? "x86 " : "") << "default library built for -mm=" + << memoryModelName(compileOptions.memoryModel) << ": " << defaultLibDir << " does not exist. " + << (x86DefaultLib ? "Build it in TypeScriptCompilerDefaultLib with TSLANG_ARCH=x86 (see its build.bat), " + : "Build it (see the default-lib build scripts), ") + << "or compile with --no-default-lib.\n"; +``` + +After the existence check and before the collector check, add the machine check for Windows x86/x64 (`winX86OrX64` exists in the function): + +```cpp +// Refused here for the reason resolveWindowsLibPath refuses the other libraries: an x64 default +// library in an x86 program (or the reverse) is a linker warning and then an unresolved symbol +// for every call into it, which names neither the library nor the fix. +if (winX86OrX64 && !defaultLibDir.empty()) +{ + llvm::SmallString<256> defaultLibFile(defaultLibDir); + llvm::sys::path::append(defaultLibFile, DEFAULT_LIB_NAME ".lib"); + if (llvm::sys::fs::exists(defaultLibFile) && !checkWindowsBinaryMachine(TheTriple, defaultLibFile)) + { + return 1; + } +} +``` + +(`dll/` trees hold the DLL's import library `TypeScriptDefaultLib.lib` next to the DLL. Verify with `ls`. If it is absent there, check the `.dll` instead.) + +In `jit.cpp` and `defaultlib.cpp`, add the argument `/*arch=*/""` with a one-line comment: JIT is host-only / install checks the host build. + +- [ ] **Step 4: Run to verify it passes.** Build the compiler (the Release build dir the suite uses; see memory `build-and-test-invocation-gotchas`). Then run `bash tslang/test/check-x86-libs.sh`: all ok, including the existing cases. + +- [ ] **Step 5: x64 unchanged.** Run `ctest -j 16 -C Release` in the build dir (2771/2771) and `bash tslang/test/compare-ir.sh` (noise only). Also compile one DefaultLib test at x64 WITH the default lib (`--default-lib-path=I:\TypeScriptCompilerDefaultLib\__build`) and run it. + +- [ ] **Step 6: Commit.** Message: "Look for the Windows x86 default library in its own arch tree". + +--- + +### Task 2: DefaultLib builds the x86 tree + +**Files:** (DefaultLib repo) +- Modify: `scripts/build_core.bat`, `scripts/build_llvm.bat`, `build.bat`, `README.md` + +**Interfaces:** +- Consumes: the Task 1 layout. +- Produces: `set TSLANG_ARCH=x86 && build.bat [release|debug] [model]` stages `__build\defaultlib\{lib,dll}\x86\\\` (`TypeScriptDefaultLib.lib`, and under `dll\` also `TypeScriptDefaultLib.dll` + its import `.lib`). With `TSLANG_ARCH` unset or `x64`, the output is identical to today. + +- [ ] **Step 1: Implement `build_core.bat`.** Read the whole file first. Changes: + - `set ARCH=x64` becomes `if "%TSLANG_ARCH%"=="" set TSLANG_ARCH=x64` then `set ARCH=%TSLANG_ARCH%`. Reject anything but `x64`/`x86` with the script's XXX banner and `exit /b 1`. + - `set ARCH_DIR=` for x64 and `set ARCH_DIR=\x86` for x86. Also `set TRIPLE_OPT=` for x64 and `set TRIPLE_OPT=-mtriple=i686-pc-windows-msvc` for x86. + - Every output path `lib\%BUILD%\%MM%` / `dll\%BUILD%\%MM%`, and the `%BUILD_LIB_PATH%\{dll,lib}\...` staging paths, become `lib%ARCH_DIR%\%BUILD%\%MM%` etc. Do this as ONE variable per tree to avoid missing one: `set LIB_OUT=lib%ARCH_DIR%\%BUILD%\%MM%` and `set DLL_OUT=dll%ARCH_DIR%\%BUILD%\%MM%`, then use those everywhere (rd, md, /Fo, -o, --obj, del, xcopy, the final exist check). + - Every `tslang.exe` invocation gets `%TRIPLE_OPT%`. + - The vcvars line: `vcvars64.bat` becomes `vcvarsall.bat` with the argument `x64` or `x86` (both live in `VC\Auxiliary\Build`). Keep the existing quoting pattern. + - The `GC_SHARED_LIB_PATH` default stays `..\TypeScriptCompiler\3rdParty\gcdll\x64\%BUILD%\lib`: the compiler appends `x86` itself. Add a comment saying so. + - `COMPILER_VERSION.txt`: append `arch: %ARCH%`. +- [ ] **Step 2: `build_llvm.bat`.** clang-cl targets x64 regardless of vcvars. For x86, `TSLANG_CC` must be `clang-cl -m32`. Set it in `build_core.bat` after the arch is known (`if "%TSLANG_TOOLCHAIN%"=="llvm" if "%ARCH%"=="x86" set "TSLANG_CC=%TSLANG_CC% -m32"`), and make the `where %TSLANG_CC%` check use the bare tool name (set a `TSLANG_CC_TOOL` before appending). `llvm-lib` infers the machine from its inputs. +- [ ] **Step 3: `build.bat`** header comment documents `set TSLANG_ARCH=x86`. It is not a positional argument: the positional pair `mode model` stays as is, in the same idiom as `TSLANG_TOOLCHAIN`. `README.md` gets the x86 build line plus the prerequisite (x86 Boehm: `prepare_3rdParty.bat x86` in the compiler repo). +- [ ] **Step 4: Build and inspect.** Run `set TSLANG_ARCH=x86 && build.bat` via `cmd //c` from the repo root (all 6 combos). Then run `set TSLANG_ARCH=x64 && build.bat release gc`, and `build.bat release gc` with the variable unset. Verify: + - every `__build\defaultlib\{lib,dll}\x86\{debug,release}\{gc,rc,none}\TypeScriptDefaultLib.lib` is machine `0x14c` (use `dumpbin /headers` or `llvm-objdump --file-headers`, whichever is on PATH); + - `dll\x86\...\TypeScriptDefaultLib.dll` is PE machine `0x14c`, and under gc imports `gc.dll` (`dumpbin /imports`); + - the x64 tree is still machine `0x8664`, with the same file list as before (`git status` in `__build` is irrelevant because it's untracked; compare `ls -R` before/after). +- [ ] **Step 5: Commit** in DefaultLib. Message: "Build the default library for 32-bit Windows with TSLANG_ARCH=x86". + +--- + +### Task 3: DefaultLib tests run as 32-bit exes + +**Files:** (DefaultLib repo) +- Modify: `tests.ps1`, `README.md` + +**Interfaces:** +- Consumes: the Task 2 tree and the Task 1 compiler. +- Produces: `$Env:TSLANG_ARCH="x86"; .\tests.ps1` runs release/compile and debug/compile for 32 bits, and skips jit with a printed note ("jit is host-only; skipped for x86"). + +- [ ] **Step 1: Implement.** In `Test`, when `$Env:TSLANG_ARCH -eq "x86"`: + - add `-mtriple=i686-pc-windows-msvc` to the compile arguments; + - drop `--shared-libs=...TypeScriptRuntime.dll` (an x64 JIT DLL; exe linking doesn't need it — verify by reading `exe.cpp`'s use of shared libs, and keep it for x64); + - point `--gc-lib-path` at `..\TypeScriptCompiler\3rdParty\gc\x64\\lib` and `--tslang-lib-path` at `..\TypeScriptCompiler\__build\tslang-runtime\`. The compiler appends `x86` to both. Pass them as explicit flags, not via the `$Env:` defaults, so an x64 value already in the environment can't leak in. + + The run step is unchanged: the x86 exe runs under WOW64. At the bottom, x86 runs only the two compile passes. +- [ ] **Step 2: Run** `$Env:TSLANG_ARCH="x86"; powershell -File tests.ps1` (PowerShell tool). Expected: 149/150 per config, with `string_replaceAll` failing (the probe). Record the exact debug/compile results; debug was not probed. Any failure besides `string_replaceAll` gets root-caused in Task 4 (same procedure), not papered over. +- [ ] **Step 3: x64 unchanged.** Run `tests.ps1` with `TSLANG_ARCH` unset: same results as before the change (run it once on the unchanged script first to have the baseline). +- [ ] **Step 4: Commit.** Message: "Run the default-library tests as 32-bit programs with TSLANG_ARCH=x86". + +--- + +### Task 4: Root-cause and fix `string_replaceAll` at i686 + +**REQUIRED SUB-SKILL:** superpowers:systematic-debugging. No fix before the root cause is stated with evidence. + +**Files:** determined by the root cause. The regression test goes in the compiler repo's suite if the bug is in the compiler, so every x86 twin covers it. If the bug is in `lib.ts`/wrappers, the DefaultLib test already covers it. + +- [ ] **Step 1: Reproduce minimal.** Start from `tests/string_replaceAll.ts`. Cut it to the smallest program that still crashes at x86 and passes at x64, e.g. just `console.log("I think Ruth's dog".replaceAll(/Dog/gi, "ferret"))`. Try release and debug, and gc/rc/none. Build commands as in the probe: + +``` +tslang --opt --opt_level=3 --nowarn -mm=gc -mtriple=i686-pc-windows-msvc --emit=exe --default-lib-path=I:\TypeScriptCompilerDefaultLib\__build --gc-lib-path=I:\TypeScriptCompiler\3rdParty\gc\x64\release\lib --tslang-lib-path=I:\TypeScriptCompiler\__build\tslang-runtime\release t.ts +``` + +- [ ] **Step 2: Locate.** Read `replaceAll`'s regex path in `src/lib.ts` and the regex wrapper (`src/wrappers/regex.cpp`). Look first for width assumptions: `index`/`i64`/`long` parameters crossing into C++ `size_t`/`int`, and struct layouts shared between TS and C++ (e.g. match-result arrays), which differ at 32 bits. If reading doesn't settle it, get a crash dump (memory `windbg-available-for-debugging`: `procdump -ma -e`, DbgX.Shell with a `$$<` script, `!wow64exts.sw` for the 32-bit stack). +- [ ] **Step 3: Failing test first.** If the bug is in the compiler, add a minimal compiler-suite test (`tslang/test/tester/tests/`, registered like its neighbours; x86 twin via `tslang_add_test`) that does NOT need the default library, i.e. reproduces the width bug in pure TS. If it is in the default library, the DefaultLib test is the failing test. +- [ ] **Step 4: Fix, then verify:** + - the x86 DefaultLib tests are 150/150 in both configs; + - x64 `tests.ps1` is unchanged; + - if the compiler changed: `ctest -C Release` is green, and the x86 twins are green with `-DTSLANG_TEST_X86=ON` (`ctest -C Release -L x86`; re-configure OFF afterwards). +- [ ] **Step 5: Commit** in whichever repo holds the fix, with a message naming the root cause. + +--- + +### Task 5: Compiler-side run coverage and docs + +**Files:** +- Modify: `tslang/test/check-x86-run.sh`, `tslang/test/tester/CMakeLists.txt` (~line 2089-2120), `docs/superpowers/specs/2026-09-20-32-bit-compilation-design.md` + +- [ ] **Step 1: `check-x86-run.sh`.** Add a default-lib section. It finds an x86 default library the way `defaultlib-collector.cmake` finds one (the `DEFAULT_LIB_CANDIDATES` pair `../TypeScriptCompilerDefaultLib/__build`), and SKIPS with a message if `defaultlib/lib/x86` is absent. It builds and runs as 32-bit exes, checking output: + - one program using Array/Map/string/Date/RegExp, including the `replaceAll(regex, …)` from Task 4, under gc, rc and none (release); + - the gc one again with `--di` (debug tree); + - a gc `--emit=dll` library that uses the default library, plus an exe that imports it. Copy the shape from the existing shared-library cases in the script, and copy `TypeScriptDefaultLib.dll` and x86 `gc.dll` next to the exe. +- [ ] **Step 2: x86 collector twin.** In `CMakeLists.txt`, under `if(TSLANG_TEST_X86)`, register `test-x86-compile-gc-defaultlib-collector`, labelled `x86`. It uses the same script with an x86 flag, read `defaultlib-collector.cmake` first. It passes `-mtriple=i686-pc-windows-msvc` through `OPT`, and the gc lib path whose `x86` subdir exists, and it keeps `SKIP_REGULAR_EXPRESSION "SKIPPED:"`. Extend the script's skip test so that when the x86 flag is on it checks the `x86` tree. With `TSLANG_TEST_X86=OFF`, `CTestTestfile.cmake` must be identical to before. +- [ ] **Step 3: Run.** `bash tslang/test/check-x86-run.sh` (all ok, default-lib section ran, not skipped). Then `cmake -DTSLANG_TEST_X86=ON .` + `ctest -C Release -L x86`: 1395 pass + the collector twin passes, 3 disabled. Then re-configure OFF, and run `ctest -j 16 -C Release`: 2771/2771. +- [ ] **Step 4: Spec.** + - Status line: phases 1-4 implemented, no exception. + - Phase 4 gets an "As built" note for the default library: the layout, `TSLANG_ARCH`, the machine check, test counts, and the Task 4 root cause. + - Close the open issue "The x86 default library", and update the 4c bullet's "(the default library has no x86 build)" parenthetical. + - Note what is still not covered: CI and the release workflows build x64 only, and the x86 `test-runner` twins still pass `--no-default-lib` like x64. +- [ ] **Step 5: Commit.** Message: "Cover the x86 default library in the x86 checks and record it in the spec". + +--- + +## Finish + +Two PRs, compiler first: its layout is what the DefaultLib output must match, and the DefaultLib tests need the Task 1 compiler. Each description states the pairing and the counts. Neither CI builds x86, so each PR's test plan lists what was run locally. From b11f2cc62f104da10d091ec6da5e2d749d24d4de Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Tue, 22 Sep 2026 17:16:34 +0100 Subject: [PATCH 2/3] Look for the Windows x86 default library in its own arch tree 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 --- tslang/include/TypeScript/Defines.h | 19 +++++-- tslang/test/check-x86-libs.sh | 81 ++++++++++++++++++++++++++++- tslang/tslang/defaultlib.cpp | 3 +- tslang/tslang/exe.cpp | 25 +++++++-- tslang/tslang/jit.cpp | 2 +- 5 files changed, 119 insertions(+), 11 deletions(-) diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 9aae092b3..baf19af8f 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -253,15 +253,26 @@ // warns about, and it is the largest instance of it, since every program that does not pass // `--no-default-lib` links this one. // -// Layout: defaultlib/{lib,dll}/{debug,release}/{gc,rc,none}/. The model name is exactly +// Layout: defaultlib/{lib,dll}/[x86/]{debug,release}/{gc,rc,none}/. The model name is exactly // `memoryModelName()`, so the directory and the `-mm=` flag cannot drift apart. #define DEFAULT_LIB_KIND_STATIC "lib" #define DEFAULT_LIB_KIND_SHARED "dll" -inline std::string getDefaultLibSubDir(bool shared, bool debugBuild, const char *memoryModel) +// ...and, for a Windows x86 target, per arch, outermost under lib/ or dll/ like every other x86 +// library (spec "Arch in the library layout"). Every other target, x64 included, has no arch +// segment, so existing trees keep their shape. There is no fallback to the x64 tree: an x64 +// library links into an x86 program only to fail on every symbol. +#define DEFAULT_LIB_ARCH_X86 "x86" + +inline std::string getDefaultLibSubDir(bool shared, bool debugBuild, const char *memoryModel, const char *arch) { - return std::string(DEFAULT_LIB_DIR "/") + (shared ? DEFAULT_LIB_KIND_SHARED : DEFAULT_LIB_KIND_STATIC) + "/" + - (debugBuild ? DEFAULT_LIB_BUILD_DIR_DEBUG : DEFAULT_LIB_BUILD_DIR_RELEASE) + "/" + memoryModel; + std::string subDir = std::string(DEFAULT_LIB_DIR "/") + (shared ? DEFAULT_LIB_KIND_SHARED : DEFAULT_LIB_KIND_STATIC) + "/"; + if (arch && *arch) + { + subDir += std::string(arch) + "/"; + } + + return subDir + (debugBuild ? DEFAULT_LIB_BUILD_DIR_DEBUG : DEFAULT_LIB_BUILD_DIR_RELEASE) + "/" + memoryModel; } #define DEBUG_SCOPE "current" diff --git a/tslang/test/check-x86-libs.sh b/tslang/test/check-x86-libs.sh index 5171b86bb..a7d8bd848 100755 --- a/tslang/test/check-x86-libs.sh +++ b/tslang/test/check-x86-libs.sh @@ -15,8 +15,12 @@ GCDLL_X86_LIB="$REPO/3rdParty/gcdll/x86/release/lib/gc.lib" GCDLL_X86_DLL="$REPO/3rdParty/gcdll/x86/release/bin/gc.dll" GCDLL_X64_DLL="$REPO/3rdParty/gcdll/x64/release/bin/gc.dll" RT_X86="$REPO/__build/tslang-runtime/release/x86/TypeScriptAsyncRuntime.lib" +# The default-lib repo is a sibling checkout; its x64 release/gc static lib stands in for "a +# default library, but the wrong one" in the cases below. Only the x64 tree is real; there is no +# x86 default library staged here (that is Task 5's job), so this file has no positive x86 case. +DEFAULTLIB_X64_LIB="$REPO/../TypeScriptCompilerDefaultLib/__build/defaultlib/lib/release/gc/TypeScriptDefaultLib.lib" -for f in "$GC_X64" "$GC_X86" "$GCDLL_X86_LIB" "$GCDLL_X86_DLL" "$GCDLL_X64_DLL" "$RT_X64" "$RT_X86"; do +for f in "$GC_X64" "$GC_X86" "$GCDLL_X86_LIB" "$GCDLL_X86_DLL" "$GCDLL_X64_DLL" "$RT_X64" "$RT_X86" "$DEFAULTLIB_X64_LIB"; do if [ ! -f "$f" ]; then echo "FAIL missing prerequisite: $f" exit 1 @@ -32,7 +36,9 @@ mkdir -p "$work/empty" \ "$work/gc-good/x86" "$work/gc-x64-in-x86/x86" "$work/gc-x86-flat" \ "$work/gcdll-good/x86" "$work/gcdll-x64-dll/x86" \ "$work/rt-good/x86" "$work/rt-x64-in-x86/x86" \ - "$work/gcdll-x86-flat" "$work/rt-x86-flat" + "$work/gcdll-x86-flat" "$work/rt-x86-flat" \ + "$work/defaultlib-x64-only/defaultlib/lib/release/gc" \ + "$work/defaultlib-x64-in-x86/defaultlib/lib/x86/release/gc" cp "$GC_X86" "$work/gc-good/x86/gc.lib" cp "$GC_X64" "$work/gc-x64-in-x86/x86/gc.lib" cp "$GC_X86" "$work/gc-x86-flat/gc.lib" @@ -45,6 +51,11 @@ cp "$RT_X64" "$work/rt-x64-in-x86/x86/TypeScriptAsyncRuntime.lib" cp "$GCDLL_X86_LIB" "$work/gcdll-x86-flat/gc.lib" cp "$GCDLL_X86_DLL" "$work/gcdll-x86-flat/gc.dll" cp "$RT_X86" "$work/rt-x86-flat/TypeScriptAsyncRuntime.lib" +# defaultlib-x64-only: a real x64 default library, staged with no x86 tree at all. +# defaultlib-x64-in-x86: that same x64 library, staged where the x86 tree is expected to be - +# present, but the wrong machine. +cp "$DEFAULTLIB_X64_LIB" "$work/defaultlib-x64-only/defaultlib/lib/release/gc/TypeScriptDefaultLib.lib" +cp "$DEFAULTLIB_X64_LIB" "$work/defaultlib-x64-in-x86/defaultlib/lib/x86/release/gc/TypeScriptDefaultLib.lib" X86="i686-pc-windows-msvc" X64="x86_64-pc-windows-msvc" @@ -99,6 +110,60 @@ expect_no_flat_error() { fi } +# The default library resolution below only runs without --no-default-lib, unlike every case +# above, so it is a separate pair of helpers rather than a reuse of expect_error/expect_no_flat_error. +# A distinct -o per call: unlike the --no-default-lib cases above, these can produce a real linked +# .exe, and Windows (AV scan) can briefly hold a lock on one just written, which would otherwise +# spuriously fail the next case's read of the shared work dir. +dl_case_n=0 + +# expect_default_lib_error ... -- ... +expect_default_lib_error() { + local name="$1" triple="$2" dlpath="$3" + shift 3 + local needles=() + while [ "$1" != "--" ]; do + needles+=("$1") + shift + done + shift + dl_case_n=$((dl_case_n + 1)) + local err status + err="$("$TSLANG" --emit=exe --opt -mm=gc -mtriple="$triple" --default-lib-path="$dlpath" "$@" \ + "$work/x.ts" -o "$work/out-dl-$dl_case_n.exe" 2>&1 >/dev/null)" + status=$? + if [ "$status" -eq 0 ]; then + echo "FAIL $name: expected a non-zero exit, got 0" + fail=1 + return + fi + for needle in "${needles[@]}"; do + if ! printf '%s' "$err" | grep -q -F -- "$needle"; then + echo "FAIL $name: expected the error to contain '$needle', got: $err" + fail=1 + return + fi + done + echo "ok $name" +} + +# expect_default_lib_ok ... +expect_default_lib_ok() { + local name="$1" triple="$2" dlpath="$3" + shift 3 + dl_case_n=$((dl_case_n + 1)) + local err status + err="$("$TSLANG" --emit=exe --opt -mm=gc -mtriple="$triple" --default-lib-path="$dlpath" "$@" \ + "$work/x.ts" -o "$work/out-dl-$dl_case_n.exe" 2>&1 >/dev/null)" + status=$? + if [ "$status" -ne 0 ]; then + echo "FAIL $name: expected a zero exit, got $status: $err" + fail=1 + else + echo "ok $name" + fi +} + GOOD_RT="--tslang-lib-path=$work/rt-good" GOOD_GC="--gc-lib-path=$work/gc-good" @@ -124,4 +189,16 @@ expect_error "x64: x86 runtime lib in flat path" "$X64" exe "TypeScriptAsyncRunt expect_no_flat_error "exe: correct x86 layout" exe "$GOOD_GC" "$GOOD_RT" expect_no_flat_error "dll: correct x86 layout" dll "--gc-shared-lib-path=$work/gcdll-good" "$GOOD_GC" "$GOOD_RT" +# Default library, without --no-default-lib: it takes the same x86-tree/machine-check treatment +# as the collector and the runtime above. There is no positive x86 case here (no x86 default +# library is staged for this script to find); Task 5 adds run coverage once one is built. +expect_default_lib_error "default lib: no x86 tree" "$X86" "$work/defaultlib-x64-only" \ + "no x86 default library built for -mm=gc" "defaultlib" "x86" "release" "gc" -- \ + "$GOOD_GC" "$GOOD_RT" +expect_default_lib_error "default lib: x64 lib in x86 tree" "$X86" "$work/defaultlib-x64-in-x86" \ + "is built for x64" -- \ + "$GOOD_GC" "$GOOD_RT" +expect_default_lib_ok "default lib: x64 control" "$X64" "$work/defaultlib-x64-only" \ + "--gc-lib-path=$(dirname "$GC_X64")" "--tslang-lib-path=$(dirname "$RT_X64")" + exit "$fail" diff --git a/tslang/tslang/defaultlib.cpp b/tslang/tslang/defaultlib.cpp index c9b5cf32e..e30e0f820 100644 --- a/tslang/tslang/defaultlib.cpp +++ b/tslang/tslang/defaultlib.cpp @@ -143,7 +143,8 @@ int installDefaultLib(int argc, char **argv) // jit.cpp resolve (defaultlib/lib/release/gc/...). auto result = checkFileExistsAtPath( builtPath, - getDefaultLibSubDir(/*shared=*/false, /*debugBuild=*/false, memoryModelName(MemoryModelGC)), + getDefaultLibSubDir(/*shared=*/false, /*debugBuild=*/false, memoryModelName(MemoryModelGC), + /*arch=*/""), // install checks the host build #ifdef WIN32 DEFAULT_LIB_NAME ".lib" #else diff --git a/tslang/tslang/exe.cpp b/tslang/tslang/exe.cpp index 9a6fff740..3c8c78031 100644 --- a/tslang/tslang/exe.cpp +++ b/tslang/tslang/exe.cpp @@ -598,6 +598,9 @@ int buildExe(int argc, char **argv, std::string objFileName, std::string additio auto shared = emitAction == BuildDll; // Windows x86 and x64 link libraries whose machine tslang checks; see resolveWindowsLibPath. auto winX86OrX64 = win && (arch == llvm::Triple::x86 || arch == llvm::Triple::x86_64); + // The default library keeps a separate x86 tree, the same rule getTargetLibDir applies to + // the collector and the runtime; see getDefaultLibSubDir. + auto x86DefaultLib = win && arch == llvm::Triple::x86; // the same debug/release choice as the CRT below std::string libConfig = enableOpt ? "release" : "debug"; @@ -670,7 +673,8 @@ int buildExe(int argc, char **argv, std::string objFileName, std::string additio // allocates, so a `gc` build linked into an `-mm=rc` program would drag Boehm in and hand // back objects this program's ownership rules do not describe. See getDefaultLibSubDir. auto defaultLibSubDir = getDefaultLibSubDir(shared, compileOptions.generateDebugInfo, - memoryModelName(compileOptions.memoryModel)); + memoryModelName(compileOptions.memoryModel), + x86DefaultLib ? DEFAULT_LIB_ARCH_X86 : ""); auto defaultLibDir = mergeWithDefaultLibPath(getDefaultLibPath(), defaultLibSubDir); // Checked here rather than left to the linker. mergeWithDefaultLibPath only joins the @@ -681,13 +685,28 @@ int buildExe(int argc, char **argv, std::string objFileName, std::string additio // far harder to diagnose than a directory that is not there. if (!defaultLibDir.empty() && !llvm::sys::fs::is_directory(defaultLibDir)) { - llvm::errs() << "error: no default library built for -mm=" + llvm::errs() << "error: no " << (x86DefaultLib ? "x86 " : "") << "default library built for -mm=" << memoryModelName(compileOptions.memoryModel) << ": " << defaultLibDir - << " does not exist. Build it (see the default-lib build scripts), " + << " does not exist. " + << (x86DefaultLib ? "Build it in TypeScriptCompilerDefaultLib with TSLANG_ARCH=x86 (see its build.bat), " + : "Build it (see the default-lib build scripts), ") << "or compile with --no-default-lib.\n"; return 1; } + // Refused here for the reason resolveWindowsLibPath refuses the other libraries: an x64 + // default library in an x86 program (or the reverse) is a linker warning and then an + // unresolved symbol for every call into it, which names neither the library nor the fix. + if (winX86OrX64 && !defaultLibDir.empty()) + { + llvm::SmallString<256> defaultLibFile(defaultLibDir); + llvm::sys::path::append(defaultLibFile, DEFAULT_LIB_NAME ".lib"); + if (llvm::sys::fs::exists(defaultLibFile) && !checkWindowsBinaryMachine(TheTriple, defaultLibFile)) + { + return 1; + } + } + // A shared library links the default library's DLL and shares a process with it. If that // DLL predates linking gc.dll it brings a second collector, which frees what the library // and its host hold - the case step 4's import check cannot see, since the library does diff --git a/tslang/tslang/jit.cpp b/tslang/tslang/jit.cpp index f9faee537..247eb232c 100644 --- a/tslang/tslang/jit.cpp +++ b/tslang/tslang/jit.cpp @@ -399,7 +399,7 @@ int runJit(int argc, char **argv, mlir::ModuleOp module, CompileOptions &compile // allocates the way the model it was built for allocates. See getDefaultLibSubDir. auto defaultLibSubDir = getDefaultLibSubDir(/*shared=*/true, compileOptions.generateDebugInfo, - memoryModelName(compileOptions.memoryModel)); + memoryModelName(compileOptions.memoryModel), /*arch=*/""); // JIT is host-only auto defaultLibFile = mergeWithDefaultLibPath(getDefaultLibPath(), #ifdef WIN32 defaultLibSubDir + "/" DEFAULT_LIB_NAME ".dll" From fd697ebc82532b982e79ead1b58ec3a75795ed9c Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Tue, 22 Sep 2026 17:16:47 +0100 Subject: [PATCH 3/3] Cover the x86 default library in the x86 checks and record it in the 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 --- .../2026-09-20-32-bit-compilation-design.md | 96 ++++++++++++-- tslang/test/check-x86-libs.sh | 42 ++++-- tslang/test/check-x86-run.sh | 122 +++++++++++++++++- tslang/test/tester/CMakeLists.txt | 40 +++++- tslang/test/tester/defaultlib-collector.cmake | 18 ++- tslang/test/x86/defaultlib_smoke.ts | 50 +++++++ 6 files changed, 335 insertions(+), 33 deletions(-) create mode 100644 tslang/test/x86/defaultlib_smoke.ts diff --git a/docs/superpowers/specs/2026-09-20-32-bit-compilation-design.md b/docs/superpowers/specs/2026-09-20-32-bit-compilation-design.md index c0762db9e..511c529a0 100644 --- a/docs/superpowers/specs/2026-09-20-32-bit-compilation-design.md +++ b/docs/superpowers/specs/2026-09-20-32-bit-compilation-design.md @@ -3,8 +3,7 @@ Design for compiling TypeScript to 32-bit targets, with `i686-pc-windows-msvc` as the proof target and width-correctness that generalizes to any 32-bit triple. -Status: approved design. Phases 1-4 implemented, except the x86 default library -(deferred; see Open issues). +Status: approved design. Phases 1-4 implemented. ## Why now @@ -301,10 +300,10 @@ only, so phase 4 is three PRs: because `--emit=jit` runs in the x64 compiler process and refuses a foreign arch. Only one test file, `02funcs_vararg.ts`, is JIT-only at the file level; every other file has a compile registration and so a twin. The cmake-script - tests have no twin either. There were 12 of them: `rc-debug-info`, - `gc-shared-auto`, the two default-library collector tests (the default - library has no x86 build) and the 8 ownership-verifier shards. The two - foreign-target import tests below make 14. + tests get no twin via `tslang_add_test` either. There were 12 of them: + `rc-debug-info`, `gc-shared-auto`, the two default-library collector tests + and the 8 ownership-verifier shards. The two foreign-target import tests + below make 14. 1395 twins pass; 3 are registered `DISABLED` in `tslang/test/tester/x86-exclusions.cmake`: `internals` under gc, rc and none, because its `inline_asm` with an `=r` constraint needs a 64-bit @@ -329,6 +328,57 @@ only, so phase 4 is three PRs: `/manifest:embed /manifestuac:...` for x86 exes. `check-x86-run.sh` builds hello as `my_setup_patch.exe` and runs it. +**As built — the x86 default library (done; was deferred from 4c).** +`getDefaultLibSubDir` (`include/TypeScript/Defines.h`) gained a required `arch` +parameter and `DEFAULT_LIB_ARCH_X86`, giving the layout named above: +`defaultlib/{lib,dll}/x86/{debug,release}/{gc,rc,none}/`, with no `x86` +segment (and no change) for every other target, x64 included. `exe.cpp` +passes `DEFAULT_LIB_ARCH_X86` only for a Windows x86 target, names the arch +and the `TSLANG_ARCH=x86` build step in the missing-directory error, and +checks the COFF machine of `TypeScriptDefaultLib.lib` the same way it already +checked the collector and the runtime (an x64 default library in an x86 +program is refused before the link, not left to become unresolved symbols). +`jit.cpp` and `defaultlib.cpp` pass `""` (host-only paths). In the separate +`TypeScriptCompilerDefaultLib` repository, `TSLANG_ARCH=x86` (set before +`build.bat`) switches `vcvarsall.bat` to `x86`, adds +`-mtriple=i686-pc-windows-msvc` to every `tslang.exe` invocation, and routes +every build and staging path through the same layout; `TSLANG_ARCH` unset or +`x64` produces the same output tree as before. That repository's own 150-test +suite passes at 150/150, release and debug, as i686 executables against the +static x86 tree (`tests.ps1`). + +One probe-only failure needed a real fix: `paragraph.replaceAll(/Dog/gi, +"ferret")` segfaulted after its first `console.log` at i686 (149/150; x64 +passed). Root cause: `RegExp.replaceAll` in `lib.ts` called the native +`declare function regexp_replace` with 3 of its 4 parameters; tslang does not +check declare-function call arity (TypeScript's TS2554), so the omitted +argument read whatever a register or a stack slot happened to hold - luck +that held at x64 and did not at i686. Fixed in the default library, not in +the compiler: `RegExp.replaceAll`'s regex branch in `lib.ts` now passes the +replacement string as the native call's 4th argument instead of omitting it. + +Compiler-side coverage: `check-x86-libs.sh` gained the default-library +error-path cases (no x86 tree; an x64 `TypeScriptDefaultLib.lib` staged where +the x86 one belongs; an x64 control case). `check-x86-run.sh` gained a +default-library section, skipped (not failed) when +`TypeScriptCompilerDefaultLib`'s x86 tree is absent: `defaultlib_smoke.ts` +(Array, Map, string, Date and RegExp, including `replaceAll(regex, ...)`) +under gc, rc and none release, the gc case again against the debug tree +(`--di`), and a gc `--emit=dll` library linking the default library plus an +exe that imports it (the shape of `defaultlib-collector.cmake`'s compile +mode, reused in bash), with `TypeScriptDefaultLib.dll` and the x86 `gc.dll` +copied beside 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, extending +`defaultlib-collector.cmake`'s own skip test to look under the `x86` tree +when an `X86` flag is passed; there is still no JIT twin (the JIT refuses an +x86 target, unrelated to the default library). With `-DTSLANG_TEST_X86=ON`: +1396 of the 1399 x86-labelled tests pass (the 1395 `tslang_add_test` twins +plus the new collector twin), 3 stay `DISABLED`. With it `OFF`, +`CTestTestfile.cmake` is unchanged except for `_BACKTRACE_TRIPLES` line +numbers (this file added lines above existing `add_test` calls), and the +default suite stays 2771/2771. + Remaining i686-only probe failures after 4b: `internals` (an `inline_asm` with an `=r` constraint, which no single i686 register can satisfy); `02funcs_vararg` (unresolved `_printf`; it is commented out of the suite). @@ -360,12 +410,34 @@ confusion: `MLIRGenImpl::mlirGenImportSharedLib` loaded the DLL into the x64 compiler to read its `__decls`. For a foreign target (arch or OS) it now reads the string from the file (`Dump::readExportedCString`); the x86 `-shared` twins pass. -- **The x86 default library (deferred from phase 4).** `getDefaultLibSubDir` - in `include/TypeScript/Defines.h` still has no arch segment, and the default - library, built in the separate `TypeScriptCompilerDefaultLib` repository, has - no x86 build. Both are needed before an x86 program can link the default - library; until then x86 programs use `--no-default-lib`, and the collector - test that links the real default library has no x86 twin. +- **The x86 default library — closed (phase 4, "As built" note above).** + `getDefaultLibSubDir` has the arch segment, the `TypeScriptCompilerDefaultLib` + repository builds the x86 tree under `TSLANG_ARCH=x86`, and + `test-x86-compile-gc-defaultlib-collector` gives the compile-mode collector + test its x86 twin. x86 programs no longer need `--no-default-lib`. +- **tslang does not check declare-function call arity (TypeScript's TS2554).** + Found while root-causing the x86 `replaceAll(regex, ...)` segfault above: a + call to a `declare function` with fewer arguments than it declares compiles + regardless, and the callee reads whatever a register or stack slot happens + to hold for the missing ones. That hid the `RegExp.replaceAll` bug at x64 + (the garbage argument happened to be usable) until the x86 build read a + garbage stack slot instead. No arity check exists for `declare function` + calls generally, only for the specific case this one bug surfaced. +- **`"xxx".replaceAll("", "_")` gives `___` instead of `_x_x_x_` (pre-existing, + both architectures).** An empty-pattern `replaceAll` should insert the + replacement between every character; it currently inserts once per + distinct-looking match instead. Confirmed unchanged at x64 and i686; not + investigated further here. +- **Neither CI nor the release workflows build an x86 default library.** Both + build x64 only, so `-DTSLANG_TEST_X86=ON` and `check-x86-run.sh`'s + default-library section are local-only checks today; the x86-labelled ctest + run and the default suite's x64-only default-library collector tests never + run together in the same CI job. The `test-runner -x86` twins (the 1395 + `tslang_add_test` twins) still pass `--no-default-lib` unconditionally, the + same as their x64 counterparts - `test-runner.cpp` was not changed by this + work - so they cover the arch/ABI matrix, not the default library; only + `test-x86-compile-gc-defaultlib-collector` and `check-x86-run.sh` exercise + the x86 default library itself. - **Per-test flags miss the entry file of multi-file tests (not 32-bit; pre-existing).** Found in phase 4c: in `test-runner`, `-mm`, `-fast-math` and `--gctors-as-method` reach only the non-entry files of a multi-file test, diff --git a/tslang/test/check-x86-libs.sh b/tslang/test/check-x86-libs.sh index a7d8bd848..d6223522d 100755 --- a/tslang/test/check-x86-libs.sh +++ b/tslang/test/check-x86-libs.sh @@ -15,12 +15,17 @@ GCDLL_X86_LIB="$REPO/3rdParty/gcdll/x86/release/lib/gc.lib" GCDLL_X86_DLL="$REPO/3rdParty/gcdll/x86/release/bin/gc.dll" GCDLL_X64_DLL="$REPO/3rdParty/gcdll/x64/release/bin/gc.dll" RT_X86="$REPO/__build/tslang-runtime/release/x86/TypeScriptAsyncRuntime.lib" -# The default-lib repo is a sibling checkout; its x64 release/gc static lib stands in for "a -# default library, but the wrong one" in the cases below. Only the x64 tree is real; there is no -# x86 default library staged here (that is Task 5's job), so this file has no positive x86 case. +# The default-lib repo is a sibling checkout; when its x64 build exists, its release/gc static lib +# stands in for "a default library, but the wrong one" below and is the only thing the x64-control +# case can run against. It is optional, unlike the libraries in the prerequisite loop below: a +# machine with no TypeScriptCompilerDefaultLib x64 build still runs every other case in this file +# (the "x64 lib in x86 tree" case below needs only some x64 COFF archive, not a real default +# library); only the x64-control case is SKIPPED. There is no x86 default library staged here - +# the x86 default library (spec Phase 4, As built) has its own run coverage in check-x86-run.sh's +# default-library section - so this file has no positive x86 case. DEFAULTLIB_X64_LIB="$REPO/../TypeScriptCompilerDefaultLib/__build/defaultlib/lib/release/gc/TypeScriptDefaultLib.lib" -for f in "$GC_X64" "$GC_X86" "$GCDLL_X86_LIB" "$GCDLL_X86_DLL" "$GCDLL_X64_DLL" "$RT_X64" "$RT_X86" "$DEFAULTLIB_X64_LIB"; do +for f in "$GC_X64" "$GC_X86" "$GCDLL_X86_LIB" "$GCDLL_X86_DLL" "$GCDLL_X64_DLL" "$RT_X64" "$RT_X86"; do if [ ! -f "$f" ]; then echo "FAIL missing prerequisite: $f" exit 1 @@ -51,11 +56,14 @@ cp "$RT_X64" "$work/rt-x64-in-x86/x86/TypeScriptAsyncRuntime.lib" cp "$GCDLL_X86_LIB" "$work/gcdll-x86-flat/gc.lib" cp "$GCDLL_X86_DLL" "$work/gcdll-x86-flat/gc.dll" cp "$RT_X86" "$work/rt-x86-flat/TypeScriptAsyncRuntime.lib" -# defaultlib-x64-only: a real x64 default library, staged with no x86 tree at all. -# defaultlib-x64-in-x86: that same x64 library, staged where the x86 tree is expected to be - -# present, but the wrong machine. -cp "$DEFAULTLIB_X64_LIB" "$work/defaultlib-x64-only/defaultlib/lib/release/gc/TypeScriptDefaultLib.lib" -cp "$DEFAULTLIB_X64_LIB" "$work/defaultlib-x64-in-x86/defaultlib/lib/x86/release/gc/TypeScriptDefaultLib.lib" +# defaultlib-x64-only/defaultlib/lib/release/gc: a stand-in file. The "no x86 tree" case below +# never opens it - it fails on the missing x86 subdirectory before reading any file - and the +# "x64 control" case only runs against this tree, with the real x64 default library copied over +# it, when one is staged (see below). defaultlib-x64-in-x86 needs only *some* x64 COFF archive: +# the machine check reads the file's COFF header before ever trying to link it as a default +# library, so $GC_X64 stands in without this file depending on a TypeScriptCompilerDefaultLib build. +cp "$GC_X64" "$work/defaultlib-x64-only/defaultlib/lib/release/gc/TypeScriptDefaultLib.lib" +cp "$GC_X64" "$work/defaultlib-x64-in-x86/defaultlib/lib/x86/release/gc/TypeScriptDefaultLib.lib" X86="i686-pc-windows-msvc" X64="x86_64-pc-windows-msvc" @@ -191,14 +199,24 @@ expect_no_flat_error "dll: correct x86 layout" dll "--gc-shared-lib-path=$work/g # Default library, without --no-default-lib: it takes the same x86-tree/machine-check treatment # as the collector and the runtime above. There is no positive x86 case here (no x86 default -# library is staged for this script to find); Task 5 adds run coverage once one is built. +# library is staged for this script to find); check-x86-run.sh's default-library section adds run +# coverage once one is built. expect_default_lib_error "default lib: no x86 tree" "$X86" "$work/defaultlib-x64-only" \ "no x86 default library built for -mm=gc" "defaultlib" "x86" "release" "gc" -- \ "$GOOD_GC" "$GOOD_RT" expect_default_lib_error "default lib: x64 lib in x86 tree" "$X86" "$work/defaultlib-x64-in-x86" \ "is built for x64" -- \ "$GOOD_GC" "$GOOD_RT" -expect_default_lib_ok "default lib: x64 control" "$X64" "$work/defaultlib-x64-only" \ - "--gc-lib-path=$(dirname "$GC_X64")" "--tslang-lib-path=$(dirname "$RT_X64")" +# Only this case needs a real, linkable x64 default library (an actual TypeScriptCompilerDefaultLib +# build); the two error cases above are satisfied by the stand-in files staged above. SKIPPED, not +# FAILed, when none is staged, matching check-x86-run.sh's skip style for the same situation. +if [ -f "$DEFAULTLIB_X64_LIB" ]; then + cp "$DEFAULTLIB_X64_LIB" "$work/defaultlib-x64-only/defaultlib/lib/release/gc/TypeScriptDefaultLib.lib" + expect_default_lib_ok "default lib: x64 control" "$X64" "$work/defaultlib-x64-only" \ + "--gc-lib-path=$(dirname "$GC_X64")" "--tslang-lib-path=$(dirname "$RT_X64")" +else + echo "SKIPPED: default lib: x64 control (no x64 default library built at $DEFAULTLIB_X64_LIB;" \ + "build it in TypeScriptCompilerDefaultLib)" +fi exit "$fail" diff --git a/tslang/test/check-x86-run.sh b/tslang/test/check-x86-run.sh index c8cae56fc..7076c95ce 100755 --- a/tslang/test/check-x86-run.sh +++ b/tslang/test/check-x86-run.sh @@ -16,6 +16,12 @@ # scope). Each corpus file asserts internally, so exiting 0 is the check, as in check-x86-eh.sh's # exception corpus loop. await_order.ts is the minimal repro from the phase 4b plan and gets an # exact-output check because its whole point is the print ORDER around the await. +# +# The x86 default library (spec Phase 4, As built) gate: defaultlib_smoke.ts, WITHOUT --no-default-lib, under +# gc/rc/none release and gc again with --di, plus a gc --emit=dll library linking the default +# library and an exe that imports it - the shape of defaultlib-collector.cmake's compile mode, +# copied here in bash because that mode is a separate (x64-only today) ctest. SKIPPED, not FAILed, +# when TypeScriptCompilerDefaultLib has no x86 tree yet (build it there with TSLANG_ARCH=x86). set -u TSLANG="${1:?usage: check-x86-run.sh }" REPO="$(cd "$(dirname "$0")/../.." && pwd)" @@ -32,6 +38,17 @@ GC_DIR="$REPO/3rdParty/gc/x64/release/lib" GC_X86="$GC_DIR/x86/gc.lib" RT_DIR="$REPO/__build/tslang-runtime/release" RT_X86="$RT_DIR/x86/TypeScriptAsyncRuntime.lib" +# Debug counterparts, for the --di case only (everything else in this script is --opt/release). +GC_DIR_DEBUG="$REPO/3rdParty/gc/x64/debug/lib" +RT_DIR_DEBUG="$REPO/__build/tslang-runtime/debug" +# The shared (DLL) Boehm build: --gc-shared-lib-path root, x86 gc.lib/gc.dll under its x86/. +GCDLL_DIR="$REPO/3rdParty/gcdll/x64/release/lib" + +# The x86 default library (spec Phase 4, As built) is a separate build (TypeScriptCompilerDefaultLib, TSLANG_ARCH=x86; +# see its build.bat). DEFAULT_LIB_PATH is unset above for hermeticity, so this is the one candidate +# defaultlib-collector.cmake's DEFAULT_LIB_CANDIDATES pair resolves to from this repo's location. +DEFAULTLIB_DIR="$REPO/../TypeScriptCompilerDefaultLib/__build" +DEFAULTLIB_TESTS="$REPO/tslang/test/tester/defaultlib" # The async corpus cases (gc only), from test/tester/tests. They are built with --entry-point to # mirror the suite; for --emit=exe it changes nothing, it matters only to the --emit=llvm IR checks. @@ -39,7 +56,8 @@ ASYNC_CORPUS=(00async_await 00async_gc_threading 00async_result_types 00owned_as 00for_await 00for_await_yield) for f in "$TSLANG" "$READOBJ" "$GC_X86" "$RT_X86" "$PROGRAMS/hello.ts" "$PROGRAMS/gc_stress.ts" \ - "$PROGRAMS/await_order.ts"; do + "$PROGRAMS/await_order.ts" "$PROGRAMS/defaultlib_smoke.ts" \ + "$DEFAULTLIB_TESTS/export_defaultlib_collector.ts" "$DEFAULTLIB_TESTS/import_defaultlib_collector.ts"; do if [ ! -f "$f" ]; then echo "FAIL missing prerequisite: $f" exit 1 @@ -234,4 +252,106 @@ if out="$(emit_await_order_ir none)"; then fi fi +# --- The x86 default library (spec Phase 4, As built) --------------------------------------- +# Skipped, not failed, the same rule defaultlib-collector.cmake applies for the x64 collector +# tests: this is a build product from a sibling repository, not something this script +# can build. `defaultlib/lib/x86` is the layout Defines.h's getDefaultLibSubDir names for any +# model/build, so its presence (regardless of model) is the gate. +if [ ! -d "$DEFAULTLIB_DIR/defaultlib/lib/x86" ]; then + echo "SKIPPED: no x86 default library built at $DEFAULTLIB_DIR/defaultlib/lib/x86" \ + "(build it in TypeScriptCompilerDefaultLib with TSLANG_ARCH=x86; see its build.bat)" +else + DL_FLAG="--default-lib-path=$DEFAULTLIB_DIR" + + # run_defaultlib_case