react-nitro-hermes 0.0.1-alpha.8 → 0.0.1-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/hermes/CMakeLists.txt +874 -0
  2. package/hermes/android/HostObjectJniTest.cpp +178 -0
  3. package/hermes/android/build-logic/build.gradle.kts +31 -0
  4. package/hermes/android/build-logic/settings.gradle.kts +17 -0
  5. package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/helpers/internal/SerializeHermesCompilerPackageJson.kt +33 -0
  6. package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/plugins/internal/TestUtilsPlugin.kt +80 -0
  7. package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/tasks/internal/CustomExecTask.kt +46 -0
  8. package/hermes/android/build-logic/src/main/kotlin/publish.gradle.kts +82 -0
  9. package/hermes/android/build.gradle.kts +332 -0
  10. package/hermes/android/cppruntime/build.gradle +72 -0
  11. package/hermes/android/cppruntime/src/main/AndroidManifest.xml +2 -0
  12. package/hermes/android/cppruntime/src/main/cpp/CMakeLists.txt +8 -0
  13. package/hermes/android/cppruntime/src/main/cpp/stub.cpp +12 -0
  14. package/hermes/android/cross-compile/swift-libicu.patch +13 -0
  15. package/hermes/android/cross-compile/toolchain-android-arm-api13-gnustl.cmake +13 -0
  16. package/hermes/android/cross-compile/toolchain-android-arm-api15-gnustl.cmake +14 -0
  17. package/hermes/android/gradle/libs.versions.toml +30 -0
  18. package/hermes/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  19. package/hermes/android/gradle/wrapper/gradle-wrapper.properties +7 -0
  20. package/hermes/android/gradle.properties +3 -0
  21. package/hermes/android/gradlew +248 -0
  22. package/hermes/android/gradlew.bat +93 -0
  23. package/hermes/android/intltest/build.gradle +147 -0
  24. package/hermes/android/intltest/java/com/facebook/hermes/test/CMakeLists.txt +45 -0
  25. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.cpp +68 -0
  26. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.h +44 -0
  27. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.java +45 -0
  28. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogueTestData.java +32 -0
  29. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java +150 -0
  30. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java +147 -0
  31. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java +470 -0
  32. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesRuntimeFactory.cpp +73 -0
  33. package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.cpp +90 -0
  34. package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.h +62 -0
  35. package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.java +155 -0
  36. package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.cpp +48 -0
  37. package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.h +36 -0
  38. package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoad.cpp +13 -0
  39. package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoadEpilogue.cpp +13 -0
  40. package/hermes/android/intltest/src/main/AndroidManifest.xml +2 -0
  41. package/hermes/android/ios-artifacts/build.gradle.kts +65 -0
  42. package/hermes/android/settings.gradle.kts +27 -0
  43. package/hermes/cmake/modules/Hermes.cmake +481 -0
  44. package/hermes/cmake/modules/Lit.cmake +96 -0
  45. package/hermes/doc/Branches.md +38 -0
  46. package/hermes/doc/BuildingAndRunning.md +121 -0
  47. package/hermes/doc/CodingStandards.md +112 -0
  48. package/hermes/doc/CrossCompilation.md +79 -0
  49. package/hermes/doc/Design.md +217 -0
  50. package/hermes/doc/Emscripten.md +156 -0
  51. package/hermes/doc/Features.md +143 -0
  52. package/hermes/doc/GCSafeCoding.md +563 -0
  53. package/hermes/doc/GenGC.md +300 -0
  54. package/hermes/doc/Hades.md +344 -0
  55. package/hermes/doc/HighLevelOptimizations.md +37 -0
  56. package/hermes/doc/IR.md +1306 -0
  57. package/hermes/doc/IntlAPIs.md +196 -0
  58. package/hermes/doc/JIT.md +999 -0
  59. package/hermes/doc/LazyEvalCompilation.md +84 -0
  60. package/hermes/doc/MemoryProfilers.md +316 -0
  61. package/hermes/doc/Modules.md +144 -0
  62. package/hermes/doc/Optimizer.md +83 -0
  63. package/hermes/doc/PerfProfiling.md +29 -0
  64. package/hermes/doc/ProhibitInvoke.md +329 -0
  65. package/hermes/doc/ReactNative.md +40 -0
  66. package/hermes/doc/ReactNativeIntegration.md +94 -0
  67. package/hermes/doc/RegExp.md +24 -0
  68. package/hermes/doc/SpecIncompat.md +85 -0
  69. package/hermes/doc/Strings.md +140 -0
  70. package/hermes/doc/TypedLanguage.md +432 -0
  71. package/hermes/doc/VM.md +174 -0
  72. package/hermes/doc/blog/2024-11-09-octane-benchmark-jit-progress.md +17 -0
  73. package/hermes/doc/blog/2024-12-19-static-hermes-update-dec-2024.md +47 -0
  74. package/hermes/doc/blog/2024-12-23-compiling-javascript-to-wasm.md +267 -0
  75. package/hermes/doc/blog/2025-06-09-jsi-runtime-data-apis.md +23 -0
  76. package/hermes/doc/blog/2025-07-15-static-h-performance-june-2025.md +18 -0
  77. package/hermes/doc/blog/2025-11-02-hermes-compilation-runtime-modes.md +23 -0
  78. package/hermes/doc/blog/2025-11-06-hermes-memory-modes.md +35 -0
  79. package/hermes/doc/blog/2025-11-24-json-parsing-performance.md +17 -0
  80. package/hermes/doc/blog/2025-12-02-iserialization.md +11 -0
  81. package/hermes/doc/blog/2025-12-12-new-way-to-contribute.md +68 -0
  82. package/hermes/doc/blog/2026-06-05-new-hermes-stable-release.md +51 -0
  83. package/hermes/doc/blog/README.md +25 -0
  84. package/hermes/doc/blog/images/2024-11-09-octane-pi3.jpeg +0 -0
  85. package/hermes/doc/blog/images/2024-11-09-octane-pi5.jpeg +0 -0
  86. package/hermes/doc/blog/images/2025-07-15-perf-hv32-interpreter.jpeg +0 -0
  87. package/hermes/doc/blog/images/2025-07-15-perf-hv32-jit.jpeg +0 -0
  88. package/hermes/doc/icu_license.txt +420 -0
  89. package/hermes/doc/img/heap_snapshot_retainers.png +0 -0
  90. package/hermes/doc/img/logo.svg +1 -0
  91. package/hermes/doc/img/object_captured_by_environment.png +0 -0
  92. package/hermes/doc/img/root_categories.png +0 -0
  93. package/hermes/doc/plans/ir-type/flow-type-system.md +324 -0
  94. package/hermes/doc/plans/ir-type/ir-type-system-v2-design.md +652 -0
  95. package/hermes/doc/plans/ir-type/ir-type-system.md +220 -0
  96. package/hermes/doc/plans/ir-type/ir-type-v2-implementation.md +1187 -0
  97. package/hermes/doc/plans/ir-type/memory.md +48 -0
  98. package/hermes/doc/plans/ir-type/phase1-steps.md +512 -0
  99. package/hermes/doc/plans/ir-type/progress.md +239 -0
  100. package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-design.md +203 -0
  101. package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-results.md +71 -0
  102. package/hermes/doc/superpowers/plans/2026-07-28-worker-arraybuffer-input.md +597 -0
  103. package/hermes/doc/superpowers/plans/2026-08-18-jitemitter-split.md +1149 -0
  104. package/hermes/doc/superpowers/plans/2026-08-20-jit-type-asserts.md +969 -0
  105. package/hermes/doc/superpowers/plans/2026-08-23-jit-shared-compiler-driver.md +478 -0
  106. package/hermes/doc/superpowers/plans/2026-08-23-x86-64-jit-skeleton.md +797 -0
  107. package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-arith.md +444 -0
  108. package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-calls.md +261 -0
  109. package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-props.md +240 -0
  110. package/hermes/doc/superpowers/plans/2026-10-26-regexp-bytecode-sharing.md +2197 -0
  111. package/hermes/doc/superpowers/plans/2026-10-28-regexp-start-filter-and-exec-fast-paths.md +3981 -0
  112. package/hermes/doc/superpowers/specs/2026-07-28-worker-arraybuffer-input-design.md +206 -0
  113. package/hermes/doc/superpowers/specs/2026-08-18-jitemitter-split-design.md +530 -0
  114. package/hermes/doc/superpowers/specs/2026-08-20-jit-type-asserts-design.md +360 -0
  115. package/hermes/doc/superpowers/specs/2026-08-23-x86-64-jit-design.md +246 -0
  116. package/hermes/doc/superpowers/specs/2026-08-26-icu-free-normalize-design.md +355 -0
  117. package/hermes/doc/superpowers/specs/2026-08-27-icu-free-convert-to-case-design.md +305 -0
  118. package/hermes/doc/superpowers/specs/2026-08-27-icu-free-locale-compare-design.md +429 -0
  119. package/hermes/doc/superpowers/specs/2026-08-29-icu-free-date-format-design.md +297 -0
  120. package/hermes/doc/superpowers/specs/2026-10-26-regexp-bytecode-sharing-design.md +605 -0
  121. package/hermes/doc/superpowers/specs/2026-10-28-regexp-start-filter-and-exec-fast-paths-design.md +551 -0
  122. package/hermes/doc/typescript-stripping.md +90 -0
  123. package/hermes/external/CMakeLists.txt +20 -0
  124. package/hermes/utils/IntlGen/IntlGen.java +531 -0
  125. package/hermes/utils/bench_with_stats.py +827 -0
  126. package/hermes/utils/build-apple-framework.sh +323 -0
  127. package/hermes/utils/chrome_snapshot_parser.py +210 -0
  128. package/hermes/utils/cmake-dev-build.sh +18 -0
  129. package/hermes/utils/commons.sh +55 -0
  130. package/hermes/utils/dump-cpp-funcs.sh +3 -0
  131. package/hermes/utils/es5objects.js +198 -0
  132. package/hermes/utils/format.sh +145 -0
  133. package/hermes/utils/gen-promise-internal-bc.sh +66 -0
  134. package/hermes/utils/genUnicodeTable.py +2503 -0
  135. package/hermes/utils/hermes-lit/CMakeLists.txt +22 -0
  136. package/hermes/utils/hermes-lit/hermes-lit.in +15 -0
  137. package/hermes/utils/jit/README.md +134 -0
  138. package/hermes/utils/jit/jit-diff.sh +160 -0
  139. package/hermes/utils/jit/jit-dump.sh +193 -0
  140. package/hermes/utils/lit-expect-gen/generate.py +217 -0
  141. package/hermes/utils/promise/index.js +15 -0
  142. package/hermes/utils/promise/package.json +13 -0
  143. package/hermes/utils/promise/rollup.config.js +19 -0
  144. package/hermes/utils/promise/yarn.lock +247 -0
  145. package/hermes/utils/scripts/hermes/consts.js +26 -0
  146. package/hermes/utils/scripts/hermes/create-tarball.js +75 -0
  147. package/hermes/utils/scripts/hermes/get-hermes-version.js +57 -0
  148. package/hermes/utils/scripts/hermes/hermes-utils.js +122 -0
  149. package/hermes/utils/scripts/hermes/package.json +6 -0
  150. package/hermes/utils/scripts/hermes/prepare-artifacts.js +113 -0
  151. package/hermes/utils/scripts/hermes/publish-artifacts.js +109 -0
  152. package/hermes/utils/scripts/hermes/publish-npm.js +96 -0
  153. package/hermes/utils/scripts/hermes/scm-utils.js +41 -0
  154. package/hermes/utils/scripts/hermes/set-artifacts-version.js +64 -0
  155. package/hermes/utils/scripts/hermes/version-utils.js +98 -0
  156. package/hermes/utils/scripts/hermes/yarn.lock +240 -0
  157. package/hermes/utils/summarize_sizes.py +184 -0
  158. package/hermes/utils/symbolicate-nativecall-stats.py +112 -0
  159. package/hermes/utils/test_runner.py +13 -0
  160. package/hermes/utils/testsuite/README.facebook +3 -0
  161. package/hermes/utils/testsuite/README.md +69 -0
  162. package/hermes/utils/testsuite/cli.py +547 -0
  163. package/hermes/utils/testsuite/es_ast.py +337 -0
  164. package/hermes/utils/testsuite/external/LICENSE +28 -0
  165. package/hermes/utils/testsuite/external/README.facebook +8 -0
  166. package/hermes/utils/testsuite/external/__init__.py +0 -0
  167. package/hermes/utils/testsuite/external/_monkeyYaml.py +164 -0
  168. package/hermes/utils/testsuite/external/parse_test262.py +141 -0
  169. package/hermes/utils/testsuite/hermes.py +522 -0
  170. package/hermes/utils/testsuite/preprocess.py +365 -0
  171. package/hermes/utils/testsuite/progress.py +377 -0
  172. package/hermes/utils/testsuite/skiplist.json +4356 -0
  173. package/hermes/utils/testsuite/skiplist.py +270 -0
  174. package/hermes/utils/testsuite/test_run_defs.py +435 -0
  175. package/hermes/utils/testsuite/typing_defs.py +52 -0
  176. package/hermes/utils/testsuite/utils.py +214 -0
  177. package/hermes/utils/tidy.sh +22 -0
  178. package/hermes/utils/update-fbcode-hermes.sh +53 -0
  179. package/hermes/utils/updateErrorTest.py +193 -0
  180. package/hermes/utils/wasm-compile.sh +65 -0
  181. package/package.json +1 -1
@@ -0,0 +1,969 @@
1
+ # JIT FR Type Assertions Implementation Plan
2
+
3
+ > **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.
4
+
5
+ **Goal:** Emit runtime checks into arm64 JIT'ed code, under a new default-off
6
+ flag, that verify the FR type facts the emitters rely on when they elide tag
7
+ checks and when they place values in class-restricted callee-saved registers.
8
+
9
+ **Architecture:** One check primitive (`Emitter::emitTypeAssert`) that uses
10
+ only the two reserved scratch registers and never touches `TempRegAlloc`,
11
+ plus one out-of-line fail stub per site riding the existing `slowPaths_`
12
+ deque, plus one shared per-function tail that calls a noreturn C++ handler.
13
+ Consumption-side checks (Classes A and B) go where the fast path has already
14
+ materialized its operand and before that fast path's own compare. The
15
+ producer-side check (Class C) runs from a hook at the bytecode instruction
16
+ boundary in `compileBB`, not from inside the register-file helpers.
17
+
18
+ **Tech Stack:** C++17 (no exceptions, no RTTI), asmjit, buck2 (the build and
19
+ test path for all work), CMake + Ninja (must stay green, out-of-tree only),
20
+ LLVM lit + FileCheck, Sapling.
21
+
22
+ **Design:** `doc/superpowers/specs/2026-08-20-jit-type-asserts-design.md`.
23
+ Read it before Task 1; this plan does not restate its motivation or its
24
+ justification for any of the choices below.
25
+
26
+ ## Global Constraints
27
+
28
+ - **Emitted code must be byte-identical with the flag off.** After every
29
+ task, `utils/jit/jit-diff.sh <before> <after>` must report
30
+ `dumps are identical`. This is the single most important constraint: a
31
+ debugging feature that perturbs production codegen is worse than none.
32
+ - **The flag is a runtime flag, not a build-time one.** Gate everything on
33
+ `emitTypeAsserts_`. Never gate emitted-code paths on `#ifndef NDEBUG` —
34
+ a release build with `-Xjit-emit-type-asserts` must emit the checks.
35
+ (`assert()` on compile-time bookkeeping is still fine and encouraged.)
36
+ - **Check sequences use `x16`/`x17` only.** Never call `allocTempGpX`,
37
+ `allocTempVecD`, `getOrAllocFRIn*`, `syncToFrame`, or `freeReg` from
38
+ check-emission code. No new spills, no LRU state changes.
39
+ - **Every insertion point must be verified flags-dead by the author.** This
40
+ is not a property arm64 emitters have in general; `selectObject` holds
41
+ NZCV across `getOrAllocFRInGpX` and consumes it in `csel`. State the
42
+ verification for each site in the task report.
43
+ - 80-column lines, 2-space indent, doc comment on every declaration, Meta
44
+ copyright header on every new file, trailing newline, `\n` line endings.
45
+ - **Build and test with buck2. Never create a CMake build directory inside
46
+ `fbsource`.** This checkout is on EdenFS, which does not support CMake
47
+ build trees (`facebook/CLAUDE-meta.md`). CMake must stay green, but it is
48
+ verified from a build directory outside `fbsource` — the controller
49
+ supplies the path in each dispatch — and never by configuring one here.
50
+ - `arc f` and `arc lint` clean.
51
+
52
+ ## Design points this plan resolves
53
+
54
+ The design doc leaves four things implicit that turn into concrete decisions
55
+ here. They are listed once, up front, rather than argued inside each task.
56
+
57
+ 1. **The Class C hook is a sibling of `assertPostInstructionInvariants`, not
58
+ an addition to its body.** That function is `{}` under `NDEBUG`
59
+ (`JitEmitter.h:473`), so putting emitted-code generation inside it would
60
+ silently disable Class C in release builds where the flag is on.
61
+ 2. **The leaked object is the `std::vector`, not its buffer.** The design
62
+ says to leak a `TypeAssertSite[]` and store its address in RO data, but
63
+ the array's address is not known until every site is registered, which
64
+ is after the RO-data constant must be emitted. Leaking the vector
65
+ *object* gives a stable address at first use and needs no back-patching.
66
+ 3. **The handler does not take `SHRuntime *`.** It formats from the site
67
+ record alone (`CodeBlock::getFunctionID`, `getNameString`), so the fail
68
+ tail is one instruction shorter and the stubs pass the site index in
69
+ `w0` rather than `w1`.
70
+ 4. **`TypeAssertSite` stores a bytecode offset, not an `inst::Inst *`.**
71
+ Computed at emit time with `codeBlock_->getOffsetOf(emittingIP)`, the
72
+ same call `getBytecodeIP` makes.
73
+
74
+ ## Verification harness
75
+
76
+ Everything here is buck2. `jit-diff.sh` and `jit-dump.sh` take **binaries**,
77
+ not a build directory, so they work with buck2 output unchanged — resolve
78
+ the binary once and reuse it:
79
+
80
+ ```bash
81
+ HERMES=$(buck2 build //xplat/static_h:hermes --show-full-output | awk '{print $2}')
82
+ ```
83
+
84
+ Capture the baseline binary once, in Task 1, from a pristine tree before any
85
+ edit. Both sides of every later comparison must come from the same build
86
+ system, so a baseline built any other way is not usable:
87
+
88
+ ```bash
89
+ cp "$HERMES" /tmp/hermes-typeassert-base
90
+ ```
91
+
92
+ Then after each task:
93
+
94
+ ```bash
95
+ HERMES=$(buck2 build //xplat/static_h:hermes --show-full-output | awk '{print $2}')
96
+ utils/jit/jit-diff.sh /tmp/hermes-typeassert-base "$HERMES"
97
+ buck2 test //xplat/static_h:lit
98
+ arc lint
99
+ ```
100
+
101
+ `jit-diff.sh` defaults to running without the new flag, so
102
+ `dumps are identical` is the expected result for every task including the
103
+ last one. To see what a task *added*, capture with the flag explicitly:
104
+
105
+ ```bash
106
+ utils/jit/jit-dump.sh --raw -c test/jit/type-asserts.js "$HERMES" | less
107
+ ```
108
+
109
+ Note that `jit-dump.sh` does not pass `-Xjit-emit-type-asserts`; add it to
110
+ the invocation by hand when inspecting, or run the binary directly.
111
+
112
+ **CMake must also stay green**, but from a build directory *outside*
113
+ `fbsource`; the controller supplies the path in each dispatch. Never
114
+ configure one inside the repository — EdenFS does not support CMake build
115
+ trees, and a 4 GB build directory in the source tree is a real cost even
116
+ though `.gitignore` hides it.
117
+
118
+ `buck2 test //xplat/static_h:testsuite_tests` (and `:testsuite_tests_jit`)
119
+ are the heavyweight suites. Do not run them per task; they are for the final
120
+ review, and only when the human asks.
121
+
122
+ ---
123
+
124
+ ### Task 1: Add the `-Xjit-emit-type-asserts` flag
125
+
126
+ Plumbing only. No emitted-code change, which makes this the task that
127
+ establishes the byte-identical baseline.
128
+
129
+ The chain mirrors `jitEmitAsserts` exactly, at five sites. Do not reuse
130
+ `emitAsserts_`: it defaults to **true** under `!NDEBUG`
131
+ (`include/hermes/VM/RuntimeFlags.h:286`), so the checks would be emitted
132
+ in every debug lit run.
133
+
134
+ **Files:**
135
+ - Modify: `include/hermes/VM/RuntimeFlags.h:286` (add after `JITEmitAsserts`)
136
+ - Modify: `include/hermes/ConsoleHost/ConsoleHost.h:171`
137
+ - Modify: `tools/hermes/hermes.cpp:150`
138
+ - Modify: `lib/ConsoleHost/ConsoleHost.cpp:969`
139
+ - Modify: `include/hermes/VM/JIT/JIT.h:127` (no-op stubs for the non-JIT build)
140
+ - Modify: `include/hermes/VM/JIT/arm64/JIT.h:143,161,191`
141
+ - Modify: `lib/VM/JIT/arm64/JIT.cpp:117` (`Compiler` ctor init list)
142
+ - Modify: `lib/VM/JIT/arm64/JitEmitter.h:307,460` (member + ctor param)
143
+ - Modify: `lib/VM/JIT/arm64/JitEmitter.cpp:64` (ctor init list)
144
+
145
+ **Interfaces:**
146
+ - Consumes: nothing.
147
+ - Produces: `Emitter::emitTypeAsserts_` (`bool const`, private),
148
+ `JITContext::setEmitTypeAsserts(bool)`,
149
+ `JITContext::getEmitTypeAsserts()`, and the
150
+ `-Xjit-emit-type-asserts` command-line flag.
151
+
152
+ - [ ] **Step 1: Capture the baseline binary**
153
+
154
+ ```bash
155
+ HERMES=$(buck2 build //xplat/static_h:hermes --show-full-output | awk '{print $2}')
156
+ cp "$HERMES" /tmp/hermes-typeassert-base
157
+ ```
158
+
159
+ - [ ] **Step 2: Add the command-line flag**
160
+
161
+ In `include/hermes/VM/RuntimeFlags.h`, immediately after the
162
+ `JITEmitAsserts` option. Unlike that one, it is unconditionally
163
+ `init(false)`:
164
+
165
+ ```cpp
166
+ llvh::cl::opt<bool> JITEmitTypeAsserts{
167
+ "Xjit-emit-type-asserts",
168
+ llvh::cl::Hidden,
169
+ llvh::cl::cat(RuntimeCategory),
170
+ llvh::cl::desc(
171
+ "(default false) Whether to verify FR type assumptions in JIT "
172
+ "compiled code"),
173
+ llvh::cl::init(false)};
174
+ ```
175
+
176
+ - [ ] **Step 3: Thread it to the JIT context**
177
+
178
+ `include/hermes/ConsoleHost/ConsoleHost.h`, after `jitEmitAsserts`:
179
+
180
+ ```cpp
181
+ /// Verify FR type assumptions in JIT'ed code.
182
+ bool jitEmitTypeAsserts{false};
183
+ ```
184
+
185
+ `tools/hermes/hermes.cpp`, after line 150:
186
+
187
+ ```cpp
188
+ options.jitEmitTypeAsserts = flags.JITEmitTypeAsserts;
189
+ ```
190
+
191
+ `lib/ConsoleHost/ConsoleHost.cpp`, after line 969:
192
+
193
+ ```cpp
194
+ runtime->getJITContext().setEmitTypeAsserts(options.jitEmitTypeAsserts);
195
+ ```
196
+
197
+ - [ ] **Step 4: Add the JITContext accessors**
198
+
199
+ `include/hermes/VM/JIT/arm64/JIT.h`, beside `setEmitAsserts`/`getEmitAsserts`
200
+ and the `emitAsserts_` member:
201
+
202
+ ```cpp
203
+ /// Set the flag to verify FR type assumptions in the JIT'ed code.
204
+ void setEmitTypeAsserts(bool emitTypeAsserts) {
205
+ emitTypeAsserts_ = emitTypeAsserts;
206
+ }
207
+ ```
208
+
209
+ ```cpp
210
+ /// \return true if we should verify FR type assumptions in JIT'ed code.
211
+ bool getEmitTypeAsserts() {
212
+ return emitTypeAsserts_;
213
+ }
214
+ ```
215
+
216
+ ```cpp
217
+ /// Whether to verify FR type assumptions in the JIT'ed code.
218
+ bool emitTypeAsserts_{false};
219
+ ```
220
+
221
+ `include/hermes/VM/JIT/JIT.h` gets the matching no-ops, beside the existing
222
+ ones at lines 127 and 136:
223
+
224
+ ```cpp
225
+ void setEmitTypeAsserts(bool emitTypeAsserts) {}
226
+ ```
227
+
228
+ ```cpp
229
+ bool getEmitTypeAsserts() {
230
+ return false;
231
+ }
232
+ ```
233
+
234
+ - [ ] **Step 5: Thread it into the Emitter**
235
+
236
+ `JitEmitter.h`, after the `emitAsserts_` member at line 307:
237
+
238
+ ```cpp
239
+ /// Whether to verify FR type assumptions in the JIT'ed code.
240
+ bool const emitTypeAsserts_;
241
+ ```
242
+
243
+ Add a `bool emitTypeAsserts` parameter to the `Emitter` constructor
244
+ declaration (line 460) directly after `emitAsserts`, mirror it in the
245
+ definition's init list (`JitEmitter.cpp:64`), and pass
246
+ `jc.getEmitTypeAsserts()` from the `Compiler` ctor (`JIT.cpp:117`).
247
+
248
+ - [ ] **Step 6: Verify the flag exists and changes nothing**
249
+
250
+ ```bash
251
+ HERMES=$(buck2 build //xplat/static_h:hermes --show-full-output | awk '{print $2}')
252
+ "$HERMES" -Xjit=force -Xjit-emit-type-asserts test/jit/binops.js
253
+ utils/jit/jit-diff.sh /tmp/hermes-typeassert-base "$HERMES"
254
+ ```
255
+
256
+ Expected: the script exits 0 with `jit-diff: dumps are identical`, and the
257
+ run with the flag produces identical program output to a run without it.
258
+
259
+ - [ ] **Step 7: Commit**
260
+
261
+ ```bash
262
+ sl commit include/hermes/VM/RuntimeFlags.h \
263
+ include/hermes/ConsoleHost/ConsoleHost.h tools/hermes/hermes.cpp \
264
+ lib/ConsoleHost/ConsoleHost.cpp include/hermes/VM/JIT/JIT.h \
265
+ include/hermes/VM/JIT/arm64/JIT.h lib/VM/JIT/arm64/JIT.cpp \
266
+ lib/VM/JIT/arm64/JitEmitter.h lib/VM/JIT/arm64/JitEmitter.cpp \
267
+ --reason "commit type assert flag - sl help commit" \
268
+ -m "..."
269
+ ```
270
+
271
+ ---
272
+
273
+ ### Task 2: The check primitive, the failure path, and the first site
274
+
275
+ The primitive is not independently testable, so this task also converts one
276
+ Class A site — `arithBinOp` under `forceNumber` — which makes the whole
277
+ chain observable end to end.
278
+
279
+ **Files:**
280
+ - Modify: `lib/VM/JIT/arm64/JitEmitter.h`
281
+ - Modify: `lib/VM/JIT/arm64/JitEmitter.cpp`
282
+ - Modify: `lib/VM/JIT/arm64/JitEmitter-arith.cpp`
283
+ - Create: `test/jit/type-asserts.js`
284
+
285
+ **Interfaces:**
286
+ - Consumes: `Emitter::emitTypeAsserts_` from Task 1.
287
+ - Produces, in `namespace hermes::vm::arm64`:
288
+ - `enum class TypePred : uint8_t` with `IsNumber`, `IsBool`,
289
+ `NotPointer`, `BitComparable`, `IsObject`
290
+ - `const char *typePredName(TypePred)`
291
+ - `struct TypeAssertSite { CodeBlock *codeBlock; uint32_t bytecodeOfs;
292
+ uint16_t frIndex; TypePred pred; }`
293
+ - `void Emitter::emitTypeAssert(FR fr, HWReg hwVal, TypePred pred)`
294
+ - `void Emitter::emitTypeAssertGpX(FR fr, const a64::GpX &xVal,
295
+ TypePred pred)` (private)
296
+ - `void Emitter::emitTypeAssertFailTail()` (private)
297
+ - `[[noreturn]] void _jit_type_assert_failed(uint32_t siteIdx,
298
+ const std::vector<TypeAssertSite> *sites)`
299
+ - `static constexpr auto xScratch2 = a64::x17;`
300
+
301
+ - [ ] **Step 1: Declare the second scratch register**
302
+
303
+ `JitEmitter.h`, at the `xScratch` definition (line 203). Replace the
304
+ existing comment and add the second register:
305
+
306
+ ```cpp
307
+ /// Scratch registers. x16/x17 sit outside the register allocator and are
308
+ /// used as scratch (call targets, IP materialization, type assert check
309
+ /// sequences); nothing holds a value in them across an emitter call.
310
+ static constexpr auto xScratch = a64::x16;
311
+ static constexpr auto xScratch2 = a64::x17;
312
+ ```
313
+
314
+ - [ ] **Step 2: Add the predicate enum and the site record**
315
+
316
+ `JitEmitter.h`, above `class Emitter`:
317
+
318
+ ```cpp
319
+ /// A property of an FR's value that a fast path relies on. These are the
320
+ /// predicates the emitters actually exploit, which is narrower and more
321
+ /// useful than the declared FRType.
322
+ enum class TypePred : uint8_t {
323
+ /// Unsigned-below (HVTag_First << kHV_NumDataBits).
324
+ IsNumber,
325
+ /// ETag == HVETag_Bool.
326
+ IsBool,
327
+ /// Tag unsigned-below the pointer range. This is the GC-safety predicate.
328
+ NotPointer,
329
+ /// NotPointer && !IsNumber: comparable by raw bits.
330
+ BitComparable,
331
+ /// Tag == HVTag_Object.
332
+ IsObject,
333
+ };
334
+
335
+ /// \return a human-readable name for \p pred, for diagnostics.
336
+ const char *typePredName(TypePred pred);
337
+
338
+ /// One emitted type check, recorded so the failure handler can name it.
339
+ struct TypeAssertSite {
340
+ CodeBlock *codeBlock;
341
+ uint32_t bytecodeOfs;
342
+ uint16_t frIndex;
343
+ TypePred pred;
344
+ };
345
+
346
+ /// Report a failed JIT type assertion and abort. Called only from JIT'ed
347
+ /// code, and never returns, so it needs no register or frame preservation.
348
+ [[noreturn]] void _jit_type_assert_failed(
349
+ uint32_t siteIdx,
350
+ const std::vector<TypeAssertSite> *sites);
351
+ ```
352
+
353
+ - [ ] **Step 3: Add the Emitter members**
354
+
355
+ `JitEmitter.h`, in the private data of `class Emitter`, near `slowPaths_`:
356
+
357
+ ```cpp
358
+ /// Records for every emitted type check, in site-index order. Allocated
359
+ /// on first use and deliberately leaked: its address is baked into the
360
+ /// emitted code, the handler that reads it never returns, and JIT'ed
361
+ /// code is never freed.
362
+ std::vector<TypeAssertSite> *typeAssertSites_ = nullptr;
363
+ /// The shared failure tail, bound only if there is at least one site.
364
+ asmjit::Label typeAssertFailLab_{};
365
+ ```
366
+
367
+ And the public/private methods:
368
+
369
+ ```cpp
370
+ /// Emit, only when emitTypeAsserts_ is set, a trap-on-violation check
371
+ /// that the value of \p fr, currently held in \p hwVal, satisfies
372
+ /// \p pred.
373
+ ///
374
+ /// Uses only xScratch/xScratch2 and never touches the register
375
+ /// allocator, so it is a pure insertion. It clobbers NZCV, so the caller
376
+ /// must have verified that flags are dead at the insertion point. That
377
+ /// is an obligation, not a property emitters have in general: see
378
+ /// selectObject, which holds flags across getOrAllocFRInGpX.
379
+ void emitTypeAssert(FR fr, HWReg hwVal, TypePred pred);
380
+ ```
381
+
382
+ - [ ] **Step 4: Implement the primitive**
383
+
384
+ `JitEmitter.cpp`:
385
+
386
+ ```cpp
387
+ const char *typePredName(TypePred pred) {
388
+ switch (pred) {
389
+ case TypePred::IsNumber:
390
+ return "number";
391
+ case TypePred::IsBool:
392
+ return "bool";
393
+ case TypePred::NotPointer:
394
+ return "non-pointer";
395
+ case TypePred::BitComparable:
396
+ return "non-pointer non-number";
397
+ case TypePred::IsObject:
398
+ return "object";
399
+ }
400
+ return "<invalid>";
401
+ }
402
+
403
+ void Emitter::emitTypeAssert(FR fr, HWReg hwVal, TypePred pred) {
404
+ if (LLVM_LIKELY(!emitTypeAsserts_))
405
+ return;
406
+ if (hwVal.isVecD()) {
407
+ a.fmov(xScratch, hwVal.a64VecD());
408
+ emitTypeAssertGpX(fr, xScratch, pred);
409
+ } else {
410
+ emitTypeAssertGpX(fr, hwVal.a64GpX(), pred);
411
+ }
412
+ }
413
+
414
+ void Emitter::emitTypeAssertGpX(
415
+ FR fr,
416
+ const a64::GpX &xVal,
417
+ TypePred pred) {
418
+ assert(emitTypeAsserts_ && "caller must check emitTypeAsserts_");
419
+ if (!typeAssertSites_) {
420
+ typeAssertSites_ = new std::vector<TypeAssertSite>();
421
+ typeAssertFailLab_ = newPrefLabel("TYPEASSERT_FAIL", 0);
422
+ }
423
+
424
+ uint32_t idx = (uint32_t)typeAssertSites_->size();
425
+ typeAssertSites_->push_back(TypeAssertSite{
426
+ codeBlock_,
427
+ codeBlock_->getOffsetOf(emittingIP),
428
+ (uint16_t)fr.index(),
429
+ pred});
430
+
431
+ comment("// type assert r%u is %s", fr.index(), typePredName(pred));
432
+ asmjit::Label failLab = newPrefLabel("TYPEASSERT_", idx);
433
+
434
+ // The helpers below tolerate xTemp == xVal; every such use is the last
435
+ // read of xVal in the sequence.
436
+ switch (pred) {
437
+ case TypePred::IsNumber:
438
+ emit_sh_ljs_is_double(a, xVal, xScratch2);
439
+ a.b_hs(failLab);
440
+ break;
441
+ case TypePred::IsBool:
442
+ emit_sh_ljs_is_bool(a, xScratch, xVal);
443
+ a.b_ne(failLab);
444
+ break;
445
+ case TypePred::NotPointer:
446
+ emit_sh_ljs_get_tag(a, xScratch, xVal);
447
+ emit_sh_ljs_tag_is_pointer(a, xScratch);
448
+ a.b_hs(failLab);
449
+ break;
450
+ case TypePred::BitComparable:
451
+ emit_sh_ljs_is_double(a, xVal, xScratch2);
452
+ a.b_lo(failLab);
453
+ emit_sh_ljs_get_tag(a, xScratch, xVal);
454
+ emit_sh_ljs_tag_is_pointer(a, xScratch);
455
+ a.b_hs(failLab);
456
+ break;
457
+ case TypePred::IsObject:
458
+ emit_sh_ljs_is_object(a, xScratch, xVal);
459
+ a.b_ne(failLab);
460
+ break;
461
+ }
462
+
463
+ slowPaths_.emplace_back(
464
+ failLab, emittingIP, [idx](Emitter &em, SlowPath &sp) {
465
+ em.comment("// Type assert failure %u", idx);
466
+ em.a.bind(sp.slowPathLab);
467
+ em.a.mov(a64::w0, idx);
468
+ em.a.b(em.typeAssertFailLab_);
469
+ });
470
+ }
471
+ ```
472
+
473
+ Condition codes, for review: `emit_sh_ljs_is_double` leaves number ⟺ LO, so
474
+ not-a-number is HS. `emit_sh_ljs_tag_is_pointer` leaves pointer ⟺ HS.
475
+ `emit_sh_ljs_is_bool` and `emit_sh_ljs_is_object` leave the property ⟺ EQ.
476
+
477
+ - [ ] **Step 5: Implement the shared tail and the handler**
478
+
479
+ `JitEmitter.cpp`:
480
+
481
+ ```cpp
482
+ void Emitter::emitTypeAssertFailTail() {
483
+ if (!typeAssertSites_)
484
+ return;
485
+ comment("// Type assert failure tail");
486
+ a.bind(typeAssertFailLab_);
487
+ // w0 already holds the site index, set by the per-site stub.
488
+ a.ldr(
489
+ a64::x1,
490
+ a64::Mem(
491
+ roDataLabel_,
492
+ uint64Const(
493
+ (uint64_t)typeAssertSites_, "type assert site table")));
494
+ // Not EMIT_RUNTIME_CALL: that saves the current IP, and emitSlowPaths()
495
+ // has already cleared emittingIP by the time this runs. The handler does
496
+ // not need the IP anyway — the site record carries the bytecode offset.
497
+ EMIT_RUNTIME_CALL_WITHOUT_SAVED_IP(
498
+ *this,
499
+ void (*)(uint32_t, const std::vector<TypeAssertSite> *),
500
+ _jit_type_assert_failed);
501
+ }
502
+
503
+ void _jit_type_assert_failed(
504
+ uint32_t siteIdx,
505
+ const std::vector<TypeAssertSite> *sites) {
506
+ const TypeAssertSite &site = (*sites)[siteIdx];
507
+ std::string message;
508
+ llvh::raw_string_ostream os(message);
509
+ os << "JIT type assert failed: function "
510
+ << site.codeBlock->getFunctionID() << "("
511
+ << site.codeBlock->getNameString() << "), bytecode offset "
512
+ << site.bytecodeOfs << ", r" << site.frIndex << ", expected "
513
+ << typePredName(site.pred);
514
+ os.flush();
515
+ hermes_fatal(message);
516
+ }
517
+ ```
518
+
519
+ Call the tail between the two existing calls at the end of the function
520
+ emission (`JitEmitter.cpp:575`), because it allocates an RO-data constant
521
+ and must therefore precede `emitROData`, and it is a branch target of slow
522
+ paths so it must follow `emitSlowPaths`:
523
+
524
+ ```cpp
525
+ emitSlowPaths();
526
+ emitTypeAssertFailTail();
527
+ emitROData();
528
+ ```
529
+
530
+ - [ ] **Step 6: Convert the first site — `arithBinOp` under `forceNumber`**
531
+
532
+ In `lib/VM/JIT/arm64/JitEmitter-arith.cpp`, in `arithBinOp`, after both
533
+ operands have been materialized by `getOrAllocFRInVecD` and before the
534
+ fast path's own `fcmp`:
535
+
536
+ ```cpp
537
+ if (forceNumber) {
538
+ emitTypeAssert(frLeft, hwLeft, TypePred::IsNumber);
539
+ emitTypeAssert(frRight, hwRight, TypePred::IsNumber);
540
+ }
541
+ ```
542
+
543
+ Flags-dead verification for this site: the two `getOrAllocFRInVecD` calls
544
+ are the first flag-relevant emissions in the fast path, and the first
545
+ consumer of NZCV (`fcmp`/`b.vs`) is emitted strictly after. Record this in
546
+ the task report.
547
+
548
+ - [ ] **Step 7: Write the lit test**
549
+
550
+ `test/jit/type-asserts.js`. It runs the same code twice, once with the flag
551
+ and once without, and checks that behaviour is unchanged. It deliberately
552
+ does **not** FileCheck emitted code: pinning the check sequences line by
553
+ line would turn every future emitter change into a test edit, and the
554
+ property under test is that behaviour and flag-off codegen do not move.
555
+
556
+ ```js
557
+ /**
558
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
559
+ *
560
+ * This source code is licensed under the MIT license found in the
561
+ * LICENSE file in the root directory of this source tree.
562
+ */
563
+ // RUN: %hermes -Xjit=force %s | %FileCheck --match-full-lines %s
564
+ // RUN: %hermes -Xjit=force -Xjit-emit-type-asserts %s | %FileCheck --match-full-lines %s
565
+
566
+ function addNumbers(a, b) {
567
+ var sum = 0;
568
+ for (var i = 0; i < 100; ++i)
569
+ sum += a * b - i;
570
+ return sum;
571
+ }
572
+
573
+ print(addNumbers(3, 4));
574
+ // CHECK: -3750
575
+ ```
576
+
577
+ - [ ] **Step 8: Verify**
578
+
579
+ ```bash
580
+ HERMES=$(buck2 build //xplat/static_h:hermes --show-full-output | awk '{print $2}')
581
+ utils/jit/jit-diff.sh /tmp/hermes-typeassert-base "$HERMES"
582
+ buck2 test //xplat/static_h:lit
583
+ ```
584
+
585
+ Expected: `dumps are identical`, all jit tests pass. Then confirm the checks
586
+ really are emitted:
587
+
588
+ ```bash
589
+ "$HERMES" -Xjit=force -Xjit-emit-type-asserts \
590
+ -Xdump-jitcode=1 test/jit/type-asserts.js | grep -c "type assert"
591
+ ```
592
+
593
+ Expected: a nonzero count.
594
+
595
+ - [ ] **Step 9: Commit**
596
+
597
+ ---
598
+
599
+ ### Task 3: The remaining Class A sites
600
+
601
+ `forceNumber` promises number operands and the emitters skip both the check
602
+ and the slow path, so these are the sites where a front-end type-inference
603
+ bug is invisible today.
604
+
605
+ **Files:**
606
+ - Modify: `lib/VM/JIT/arm64/JitEmitter-arith.cpp` (`arithUnop`, `mod`)
607
+ - Modify: `lib/VM/JIT/arm64/JitEmitter-control.cpp` (`jCond`)
608
+ - Modify: `lib/VM/JIT/arm64/JitEmitter-call.cpp` (`callWithNewTargetLong`)
609
+
610
+ **Interfaces:**
611
+ - Consumes: `emitTypeAssert`, `TypePred` from Task 2.
612
+ - Produces: nothing new.
613
+
614
+ - [ ] **Step 1: `arithUnop`**
615
+
616
+ After `hwInput = getOrAllocFRInVecD(frInput, true);` and before the
617
+ `if (!inputIsNum)` block's `fcmp`:
618
+
619
+ ```cpp
620
+ if (forceNumber)
621
+ emitTypeAssert(frInput, hwInput, TypePred::IsNumber);
622
+ ```
623
+
624
+ - [ ] **Step 2: `mod`, `jCond`, `callWithNewTargetLong`**
625
+
626
+ Same shape in each: immediately after the last operand is materialized,
627
+ before that emitter's first flag-setting instruction. For
628
+ `callWithNewTargetLong` the operand is `frArgc` and the reliance is the
629
+ `fcvtzu` that assumes a number, so the check goes immediately before it.
630
+
631
+ For each site, verify flags-dead and record the verification.
632
+
633
+ - [ ] **Step 3: Verify and commit**
634
+
635
+ Run the harness. `dumps are identical` is still required.
636
+
637
+ ---
638
+
639
+ ### Task 4: `readFRForAssert` and the Class B sites
640
+
641
+ Class B covers the fast paths that skip a check because `isFRKnownNumber`,
642
+ `isFRKnownBool` or `isFRKnownOtherNonPtr` returned true. Note that those
643
+ predicates consult `globalType` as well as `localType`
644
+ (`JitEmitter.h:1157`), so they test the bytecode header's classification,
645
+ not only the JIT's own bookkeeping.
646
+
647
+ `jmpUndefined` needs a read helper: when the operand is known Number or
648
+ Bool it emits *nothing*, so there is no materialized register to check.
649
+
650
+ **Files:**
651
+ - Modify: `lib/VM/JIT/arm64/JitEmitter.h`
652
+ - Modify: `lib/VM/JIT/arm64/JitEmitter.cpp`
653
+ - Modify: `lib/VM/JIT/arm64/JitEmitter-arith.cpp` (`arithBinOp`,
654
+ `arithUnop`, `compareImpl`, `strictEqualImpl`, `toNumber`, `toNumeric`)
655
+ - Modify: `lib/VM/JIT/arm64/JitEmitter-control.cpp` (`jCond`,
656
+ `jmpTrueFalse`, `jmpUndefined`, `jStrictEqual`)
657
+
658
+ **Interfaces:**
659
+ - Consumes: `emitTypeAssert`, `TypePred`.
660
+ - Produces: `void Emitter::readFRForAssert(FR fr)`.
661
+
662
+ - [ ] **Step 1: Implement `readFRForAssert`**
663
+
664
+ The location priority must honour the `FRState` up-to-date invariants, not
665
+ just the location order. Reading a stale location would make the assert
666
+ report phantom violations, which is the worst failure mode a checking
667
+ feature can have.
668
+
669
+ ```cpp
670
+ void Emitter::readFRForAssert(FR fr) {
671
+ assert(emitTypeAsserts_ && "caller must check emitTypeAsserts_");
672
+ FRState &frState = frameRegs_[fr.index()];
673
+ assert(!frState.regIsDirty && "reading an FR that is about to be written");
674
+
675
+ // Locals always hold the latest value; a global reg holds it only if
676
+ // globalRegUpToDate; otherwise the frame must be up to date.
677
+ if (frState.localGpX) {
678
+ a.mov(xScratch, frState.localGpX.a64GpX());
679
+ } else if (frState.localVecD) {
680
+ a.fmov(xScratch, frState.localVecD.a64VecD());
681
+ } else if (frState.globalReg && frState.globalRegUpToDate) {
682
+ if (frState.globalReg.isGpX())
683
+ a.mov(xScratch, frState.globalReg.a64GpX());
684
+ else
685
+ a.fmov(xScratch, frState.globalReg.a64VecD());
686
+ } else {
687
+ assert(frState.frameUpToDate && "FR has no up-to-date location");
688
+ // The encoding fallback in _loadFrame also uses xScratch, but sequences
689
+ // that use before xScratch receives the loaded value.
690
+ _loadFrame(HWReg(xScratch), fr);
691
+ }
692
+ }
693
+ ```
694
+
695
+ Declare it in `JitEmitter.h` beside `emitTypeAssert`, private, with the doc
696
+ comment from the design.
697
+
698
+ - [ ] **Step 2: The materialized-operand sites**
699
+
700
+ For each row of the design's Class B table except `jmpUndefined`, insert the
701
+ check right after the operand the elision applies to is materialized:
702
+
703
+ | Emitter | Guard | Check |
704
+ |---|---|---|
705
+ | `arithBinOp`, `arithUnop` | `isFRKnownNumber(fr)` | `IsNumber` on that operand |
706
+ | `compareImpl`, `jCond` | both known Number | `IsNumber` on both |
707
+ | `jmpTrueFalse` | known Number / known Bool | `IsNumber` / `IsBool` |
708
+ | `strictEqualImpl` (`arith.cpp:692`) | known Bool or OtherNonPtr / known Number | `BitComparable` / `IsNumber` on the known side |
709
+ | `jStrictEqual` (`control.cpp:858`) | same two elisions, separate emitter | `BitComparable` / `IsNumber` on the known side |
710
+
711
+ `jStrictEqual` is a distinct emitter from `strictEqualImpl`, not a caller
712
+ of it, and carries its own copy of both tiers. Convert both.
713
+
714
+ Note that `arithBinOp`'s `forceNumber` path from Task 2 already sets
715
+ `localType = FRType::Number`, so guard the Class B insertion on
716
+ `!forceNumber` to avoid emitting the same check twice.
717
+
718
+ - [ ] **Step 3: The early-return elisions**
719
+
720
+ Three sites elide by returning before anything is materialized, so there is
721
+ no operand register to check and `readFRForAssert` is required. Flags are
722
+ trivially dead at all three: the insertion point is an otherwise empty
723
+ emission.
724
+
725
+ `jmpUndefined` (`control.cpp:685`) returns early when the input is known
726
+ Number **or** known Bool. Assert the fact that was actually relied on, in
727
+ the same order the emitter tests it:
728
+
729
+ ```cpp
730
+ if (emitTypeAsserts_) {
731
+ readFRForAssert(frInput);
732
+ emitTypeAssertGpX(
733
+ frInput,
734
+ xScratch,
735
+ isFRKnownType(frInput, FRType::Number) ? TypePred::IsNumber
736
+ : TypePred::IsBool);
737
+ }
738
+ ```
739
+
740
+ Do **not** use `BitComparable` here. It is `NotPointer && !IsNumber`, so it
741
+ would trap on every legitimate number reaching this path, and it accepts
742
+ `undefined` — inverted in both directions.
743
+
744
+ `toNumber` (`arith.cpp:20`) and `toNumeric` (`arith.cpp:68`) elide with
745
+ `return mov(frRes, frInput, false)` when the input is known Number. The
746
+ check goes immediately before that early return, on `frInput`:
747
+
748
+ ```cpp
749
+ if (isFRKnownNumber(frInput)) {
750
+ if (emitTypeAsserts_) {
751
+ readFRForAssert(frInput);
752
+ emitTypeAssertGpX(frInput, xScratch, TypePred::IsNumber);
753
+ }
754
+ return mov(frRes, frInput, false);
755
+ }
756
+ ```
757
+
758
+ `toInt32` is **not** a Class B site and must not be converted: it has no
759
+ `isFRKnownNumber` elision at all — it always emits its check and slow
760
+ path.
761
+
762
+ - [ ] **Step 4: Verify and commit**
763
+
764
+ ---
765
+
766
+ ### Task 5: Class C — producer-side global-class checks
767
+
768
+ A value stored into an FR that owns a callee-saved global register must
769
+ match that register's class, or the GC will either miss a pointer or
770
+ misread one. Consumption checks cannot cover this, because the GC's
771
+ "consumption" is register residency rather than an emitted instruction.
772
+
773
+ The hook runs at the bytecode instruction boundary. Do not hook the tail of
774
+ `frUpdatedWithHW`: many emitters call it *before* emitting the write
775
+ (`catchInst`, `callImpl`'s SavedCodeBlock, `bitNot`, `booleanNot`,
776
+ `loadThisNS`, `selectObject`), so it would check a register the value has
777
+ not reached — and because `frUpdatedWithHW` clears `regIsDirty`, it could
778
+ not even detect that it was in that window. A third write path,
779
+ `syncFrameOutParam`, reaches neither `frUpdatedWithHW` nor `movFRFromHW`.
780
+
781
+ **Files:**
782
+ - Modify: `lib/VM/JIT/arm64/JitEmitter.h`
783
+ - Modify: `lib/VM/JIT/arm64/JitEmitter.cpp`
784
+ - Modify: `lib/VM/JIT/arm64/JitEmitter-regalloc.cpp`
785
+ - Modify: `lib/VM/JIT/arm64/JIT.cpp:157`
786
+
787
+ **Interfaces:**
788
+ - Consumes: `emitTypeAssertGpX`, `readFRForAssert`, `TypePred`.
789
+ - Produces: `void Emitter::recordFRWriteForAssert(FR fr)` (private),
790
+ `void Emitter::emitPendingTypeAsserts()` (public, called from
791
+ `compileBB`).
792
+
793
+ - [ ] **Step 1: Add the per-instruction written set**
794
+
795
+ `JitEmitter.h`, in the private data:
796
+
797
+ ```cpp
798
+ /// FRs written by the bytecode instruction currently being emitted whose
799
+ /// global register class requires a check. Drained at each instruction
800
+ /// boundary by emitPendingTypeAsserts().
801
+ llvh::SmallVector<FR, 4> typeAssertPendingWrites_{};
802
+ ```
803
+
804
+ ```cpp
805
+ /// Record that \p fr was written, so that the instruction boundary can
806
+ /// check the value against its global register class. Records nothing
807
+ /// unless the FR owns a global register. The callers gate on
808
+ /// emitTypeAsserts_.
809
+ void recordFRWriteForAssert(FR fr);
810
+ ```
811
+
812
+ - [ ] **Step 2: Record from all three write paths**
813
+
814
+ `JitEmitter-regalloc.cpp`. In `frUpdatedWithHW`, at the top (before it
815
+ clears `regIsDirty`, though the position does not matter — only the
816
+ recording does); in `movFRFromHW`'s direct-to-frame `else` branch; and in
817
+ `syncFrameOutParam`:
818
+
819
+ ```cpp
820
+ if (LLVM_UNLIKELY(emitTypeAsserts_))
821
+ recordFRWriteForAssert(fr);
822
+ ```
823
+
824
+ ```cpp
825
+ void Emitter::recordFRWriteForAssert(FR fr) {
826
+ FRState &frState = frameRegs_[fr.index()];
827
+ // The two conditions coincide today only because enter()'s allocation
828
+ // loops set globalReg and globalType together and stop together. Pin it,
829
+ // because the check below relies on it.
830
+ assert(
831
+ (frState.globalType != FRType::UnknownPtr) ==
832
+ frState.globalReg.isValid() &&
833
+ "globalType and globalReg must agree");
834
+ if (frState.globalType == FRType::UnknownPtr)
835
+ return;
836
+ if (llvh::is_contained(typeAssertPendingWrites_, fr))
837
+ return;
838
+ typeAssertPendingWrites_.push_back(fr);
839
+ }
840
+ ```
841
+
842
+ - [ ] **Step 3: Emit at the instruction boundary**
843
+
844
+ `JitEmitter.cpp`:
845
+
846
+ ```cpp
847
+ void Emitter::emitPendingTypeAsserts() {
848
+ if (LLVM_LIKELY(typeAssertPendingWrites_.empty()))
849
+ return;
850
+ for (FR fr : typeAssertPendingWrites_) {
851
+ FRState &frState = frameRegs_[fr.index()];
852
+ TypePred pred = frState.globalType == FRType::Number
853
+ ? TypePred::IsNumber
854
+ : TypePred::NotPointer;
855
+ readFRForAssert(fr);
856
+ emitTypeAssertGpX(fr, xScratch, pred);
857
+ }
858
+ typeAssertPendingWrites_.clear();
859
+ }
860
+ ```
861
+
862
+ `JIT.cpp`, in `compileBB`, as a sibling of the existing call — **not**
863
+ inside `assertPostInstructionInvariants`, whose body is `{}` under
864
+ `NDEBUG`:
865
+
866
+ ```cpp
867
+ while (ip != to) {
868
+ em_.emittingIP = ip;
869
+ ip = dispatch(ip);
870
+ em_.assertPostInstructionInvariants();
871
+ em_.emitPendingTypeAsserts();
872
+ }
873
+ ```
874
+
875
+ `emittingIP` still names the instruction just emitted, which is what the
876
+ site record should carry.
877
+
878
+ Add to `newBasicBlock`:
879
+
880
+ ```cpp
881
+ assert(
882
+ typeAssertPendingWrites_.empty() &&
883
+ "pending type asserts must be drained at each instruction");
884
+ ```
885
+
886
+ - [ ] **Step 4: Verify and commit**
887
+
888
+ Beyond the standard harness, run a program that exercises number-classed
889
+ global registers under the flag and confirm no trap:
890
+
891
+ ```bash
892
+ "$HERMES" -Xjit=force -Xjit-emit-type-asserts test/hermes/flow/nbody.js
893
+ ```
894
+
895
+ ---
896
+
897
+ ### Task 6: Prove the checks can fail
898
+
899
+ A green run proves only that the suite ran. Each mutation below is applied
900
+ to a throwaway working copy, observed, and reverted; none of them is
901
+ committed.
902
+
903
+ **This task produces no commit.** Its deliverable is the three recorded
904
+ failure messages, which go into the task report and into the Test Plan of
905
+ Task 5's commit. There is no `doc/JIT.md` or any other user-facing document
906
+ describing the JIT's `-X` flags in this repository — the `cl::desc` string
907
+ added in Task 1 is the documentation. Do not create one as part of this
908
+ feature.
909
+
910
+ **Files:**
911
+ - None. Mutations are applied and reverted, never committed.
912
+
913
+ **Interfaces:**
914
+ - Consumes: everything from Tasks 1-5.
915
+ - Produces: nothing.
916
+
917
+ - [ ] **Step 1: Class C — a lying emitter**
918
+
919
+ Change `loadConstString` to declare `FRType::Number`, rebuild, and run any
920
+ test that stores a string into a number-classed FR under the flag. Expect a
921
+ fatal naming that FR and `expected number`. Record the exact message.
922
+
923
+ - [ ] **Step 2: Class B — a weakened elision**
924
+
925
+ Force `isFRKnownNumber` to return true for one FR in `arithBinOp`, rebuild,
926
+ run `test/jit/binops.js` under the flag. Expect a trap at the consumption
927
+ site. Record the message.
928
+
929
+ - [ ] **Step 3: Class A — the `*N` contract**
930
+
931
+ Apply the Step 2 mutation to the `jLessN` path and run a typed test that
932
+ reaches it. Record the message.
933
+
934
+ - [ ] **Step 4: Revert all mutations and confirm the tree is clean**
935
+
936
+ ```bash
937
+ sl status --reason "confirm mutations reverted - sl help status"
938
+ ```
939
+
940
+ - [ ] **Step 5: Final verification**
941
+
942
+ ```bash
943
+ buck2 test //xplat/static_h:lit
944
+ utils/jit/jit-diff.sh /tmp/hermes-typeassert-base "$HERMES"
945
+ ```
946
+
947
+ `buck2 test //xplat/static_h:testsuite_tests_jit` is the run that would
948
+ actually certify the existing type plumbing, and the design's rollout order
949
+ asks for it here. It is heavyweight, so ask the human before running it
950
+ rather than starting it unprompted.
951
+
952
+ ---
953
+
954
+ ## Out of scope
955
+
956
+ - **Class D (typed-mode invariants).** `typedLoadParent`, the `fastArray*`
957
+ element assumptions, and `AddS`'s both-strings contract. Cheap once the
958
+ primitive exists, but they validate the typed front-end rather than the
959
+ JIT, and typed mode has its own checking story. Revisit after Class A/B/C
960
+ have run clean against test262 and the benchmark suite.
961
+ - **The x86-64 side.** The design's register-convention requirement — a
962
+ reserved caller-saved scratch (r11, and a second for the `IsNumber`
963
+ bound, which genuinely cannot be done with one register) — belongs in the
964
+ port plan's register-convention section, which must be written before the
965
+ temp allocator's range is fixed.
966
+ - **Sharing Class C across backends.** It lives in
967
+ `JitEmitter-regalloc.cpp`, the piece proposed for cross-arch extraction
968
+ (cleanup item A3). If that extraction lands first, Class C is written
969
+ once for both backends; this plan does not depend on it either way.