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.
- package/hermes/CMakeLists.txt +874 -0
- package/hermes/android/HostObjectJniTest.cpp +178 -0
- package/hermes/android/build-logic/build.gradle.kts +31 -0
- package/hermes/android/build-logic/settings.gradle.kts +17 -0
- package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/helpers/internal/SerializeHermesCompilerPackageJson.kt +33 -0
- package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/plugins/internal/TestUtilsPlugin.kt +80 -0
- package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/tasks/internal/CustomExecTask.kt +46 -0
- package/hermes/android/build-logic/src/main/kotlin/publish.gradle.kts +82 -0
- package/hermes/android/build.gradle.kts +332 -0
- package/hermes/android/cppruntime/build.gradle +72 -0
- package/hermes/android/cppruntime/src/main/AndroidManifest.xml +2 -0
- package/hermes/android/cppruntime/src/main/cpp/CMakeLists.txt +8 -0
- package/hermes/android/cppruntime/src/main/cpp/stub.cpp +12 -0
- package/hermes/android/cross-compile/swift-libicu.patch +13 -0
- package/hermes/android/cross-compile/toolchain-android-arm-api13-gnustl.cmake +13 -0
- package/hermes/android/cross-compile/toolchain-android-arm-api15-gnustl.cmake +14 -0
- package/hermes/android/gradle/libs.versions.toml +30 -0
- package/hermes/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/hermes/android/gradle/wrapper/gradle-wrapper.properties +7 -0
- package/hermes/android/gradle.properties +3 -0
- package/hermes/android/gradlew +248 -0
- package/hermes/android/gradlew.bat +93 -0
- package/hermes/android/intltest/build.gradle +147 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/CMakeLists.txt +45 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.cpp +68 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.h +44 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.java +45 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogueTestData.java +32 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java +150 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java +147 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java +470 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesRuntimeFactory.cpp +73 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.cpp +90 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.h +62 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.java +155 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.cpp +48 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.h +36 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoad.cpp +13 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoadEpilogue.cpp +13 -0
- package/hermes/android/intltest/src/main/AndroidManifest.xml +2 -0
- package/hermes/android/ios-artifacts/build.gradle.kts +65 -0
- package/hermes/android/settings.gradle.kts +27 -0
- package/hermes/cmake/modules/Hermes.cmake +481 -0
- package/hermes/cmake/modules/Lit.cmake +96 -0
- package/hermes/doc/Branches.md +38 -0
- package/hermes/doc/BuildingAndRunning.md +121 -0
- package/hermes/doc/CodingStandards.md +112 -0
- package/hermes/doc/CrossCompilation.md +79 -0
- package/hermes/doc/Design.md +217 -0
- package/hermes/doc/Emscripten.md +156 -0
- package/hermes/doc/Features.md +143 -0
- package/hermes/doc/GCSafeCoding.md +563 -0
- package/hermes/doc/GenGC.md +300 -0
- package/hermes/doc/Hades.md +344 -0
- package/hermes/doc/HighLevelOptimizations.md +37 -0
- package/hermes/doc/IR.md +1306 -0
- package/hermes/doc/IntlAPIs.md +196 -0
- package/hermes/doc/JIT.md +999 -0
- package/hermes/doc/LazyEvalCompilation.md +84 -0
- package/hermes/doc/MemoryProfilers.md +316 -0
- package/hermes/doc/Modules.md +144 -0
- package/hermes/doc/Optimizer.md +83 -0
- package/hermes/doc/PerfProfiling.md +29 -0
- package/hermes/doc/ProhibitInvoke.md +329 -0
- package/hermes/doc/ReactNative.md +40 -0
- package/hermes/doc/ReactNativeIntegration.md +94 -0
- package/hermes/doc/RegExp.md +24 -0
- package/hermes/doc/SpecIncompat.md +85 -0
- package/hermes/doc/Strings.md +140 -0
- package/hermes/doc/TypedLanguage.md +432 -0
- package/hermes/doc/VM.md +174 -0
- package/hermes/doc/blog/2024-11-09-octane-benchmark-jit-progress.md +17 -0
- package/hermes/doc/blog/2024-12-19-static-hermes-update-dec-2024.md +47 -0
- package/hermes/doc/blog/2024-12-23-compiling-javascript-to-wasm.md +267 -0
- package/hermes/doc/blog/2025-06-09-jsi-runtime-data-apis.md +23 -0
- package/hermes/doc/blog/2025-07-15-static-h-performance-june-2025.md +18 -0
- package/hermes/doc/blog/2025-11-02-hermes-compilation-runtime-modes.md +23 -0
- package/hermes/doc/blog/2025-11-06-hermes-memory-modes.md +35 -0
- package/hermes/doc/blog/2025-11-24-json-parsing-performance.md +17 -0
- package/hermes/doc/blog/2025-12-02-iserialization.md +11 -0
- package/hermes/doc/blog/2025-12-12-new-way-to-contribute.md +68 -0
- package/hermes/doc/blog/2026-06-05-new-hermes-stable-release.md +51 -0
- package/hermes/doc/blog/README.md +25 -0
- package/hermes/doc/blog/images/2024-11-09-octane-pi3.jpeg +0 -0
- package/hermes/doc/blog/images/2024-11-09-octane-pi5.jpeg +0 -0
- package/hermes/doc/blog/images/2025-07-15-perf-hv32-interpreter.jpeg +0 -0
- package/hermes/doc/blog/images/2025-07-15-perf-hv32-jit.jpeg +0 -0
- package/hermes/doc/icu_license.txt +420 -0
- package/hermes/doc/img/heap_snapshot_retainers.png +0 -0
- package/hermes/doc/img/logo.svg +1 -0
- package/hermes/doc/img/object_captured_by_environment.png +0 -0
- package/hermes/doc/img/root_categories.png +0 -0
- package/hermes/doc/plans/ir-type/flow-type-system.md +324 -0
- package/hermes/doc/plans/ir-type/ir-type-system-v2-design.md +652 -0
- package/hermes/doc/plans/ir-type/ir-type-system.md +220 -0
- package/hermes/doc/plans/ir-type/ir-type-v2-implementation.md +1187 -0
- package/hermes/doc/plans/ir-type/memory.md +48 -0
- package/hermes/doc/plans/ir-type/phase1-steps.md +512 -0
- package/hermes/doc/plans/ir-type/progress.md +239 -0
- package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-design.md +203 -0
- package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-results.md +71 -0
- package/hermes/doc/superpowers/plans/2026-07-28-worker-arraybuffer-input.md +597 -0
- package/hermes/doc/superpowers/plans/2026-08-18-jitemitter-split.md +1149 -0
- package/hermes/doc/superpowers/plans/2026-08-20-jit-type-asserts.md +969 -0
- package/hermes/doc/superpowers/plans/2026-08-23-jit-shared-compiler-driver.md +478 -0
- package/hermes/doc/superpowers/plans/2026-08-23-x86-64-jit-skeleton.md +797 -0
- package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-arith.md +444 -0
- package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-calls.md +261 -0
- package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-props.md +240 -0
- package/hermes/doc/superpowers/plans/2026-10-26-regexp-bytecode-sharing.md +2197 -0
- package/hermes/doc/superpowers/plans/2026-10-28-regexp-start-filter-and-exec-fast-paths.md +3981 -0
- package/hermes/doc/superpowers/specs/2026-07-28-worker-arraybuffer-input-design.md +206 -0
- package/hermes/doc/superpowers/specs/2026-08-18-jitemitter-split-design.md +530 -0
- package/hermes/doc/superpowers/specs/2026-08-20-jit-type-asserts-design.md +360 -0
- package/hermes/doc/superpowers/specs/2026-08-23-x86-64-jit-design.md +246 -0
- package/hermes/doc/superpowers/specs/2026-08-26-icu-free-normalize-design.md +355 -0
- package/hermes/doc/superpowers/specs/2026-08-27-icu-free-convert-to-case-design.md +305 -0
- package/hermes/doc/superpowers/specs/2026-08-27-icu-free-locale-compare-design.md +429 -0
- package/hermes/doc/superpowers/specs/2026-08-29-icu-free-date-format-design.md +297 -0
- package/hermes/doc/superpowers/specs/2026-10-26-regexp-bytecode-sharing-design.md +605 -0
- package/hermes/doc/superpowers/specs/2026-10-28-regexp-start-filter-and-exec-fast-paths-design.md +551 -0
- package/hermes/doc/typescript-stripping.md +90 -0
- package/hermes/external/CMakeLists.txt +20 -0
- package/hermes/utils/IntlGen/IntlGen.java +531 -0
- package/hermes/utils/bench_with_stats.py +827 -0
- package/hermes/utils/build-apple-framework.sh +323 -0
- package/hermes/utils/chrome_snapshot_parser.py +210 -0
- package/hermes/utils/cmake-dev-build.sh +18 -0
- package/hermes/utils/commons.sh +55 -0
- package/hermes/utils/dump-cpp-funcs.sh +3 -0
- package/hermes/utils/es5objects.js +198 -0
- package/hermes/utils/format.sh +145 -0
- package/hermes/utils/gen-promise-internal-bc.sh +66 -0
- package/hermes/utils/genUnicodeTable.py +2503 -0
- package/hermes/utils/hermes-lit/CMakeLists.txt +22 -0
- package/hermes/utils/hermes-lit/hermes-lit.in +15 -0
- package/hermes/utils/jit/README.md +134 -0
- package/hermes/utils/jit/jit-diff.sh +160 -0
- package/hermes/utils/jit/jit-dump.sh +193 -0
- package/hermes/utils/lit-expect-gen/generate.py +217 -0
- package/hermes/utils/promise/index.js +15 -0
- package/hermes/utils/promise/package.json +13 -0
- package/hermes/utils/promise/rollup.config.js +19 -0
- package/hermes/utils/promise/yarn.lock +247 -0
- package/hermes/utils/scripts/hermes/consts.js +26 -0
- package/hermes/utils/scripts/hermes/create-tarball.js +75 -0
- package/hermes/utils/scripts/hermes/get-hermes-version.js +57 -0
- package/hermes/utils/scripts/hermes/hermes-utils.js +122 -0
- package/hermes/utils/scripts/hermes/package.json +6 -0
- package/hermes/utils/scripts/hermes/prepare-artifacts.js +113 -0
- package/hermes/utils/scripts/hermes/publish-artifacts.js +109 -0
- package/hermes/utils/scripts/hermes/publish-npm.js +96 -0
- package/hermes/utils/scripts/hermes/scm-utils.js +41 -0
- package/hermes/utils/scripts/hermes/set-artifacts-version.js +64 -0
- package/hermes/utils/scripts/hermes/version-utils.js +98 -0
- package/hermes/utils/scripts/hermes/yarn.lock +240 -0
- package/hermes/utils/summarize_sizes.py +184 -0
- package/hermes/utils/symbolicate-nativecall-stats.py +112 -0
- package/hermes/utils/test_runner.py +13 -0
- package/hermes/utils/testsuite/README.facebook +3 -0
- package/hermes/utils/testsuite/README.md +69 -0
- package/hermes/utils/testsuite/cli.py +547 -0
- package/hermes/utils/testsuite/es_ast.py +337 -0
- package/hermes/utils/testsuite/external/LICENSE +28 -0
- package/hermes/utils/testsuite/external/README.facebook +8 -0
- package/hermes/utils/testsuite/external/__init__.py +0 -0
- package/hermes/utils/testsuite/external/_monkeyYaml.py +164 -0
- package/hermes/utils/testsuite/external/parse_test262.py +141 -0
- package/hermes/utils/testsuite/hermes.py +522 -0
- package/hermes/utils/testsuite/preprocess.py +365 -0
- package/hermes/utils/testsuite/progress.py +377 -0
- package/hermes/utils/testsuite/skiplist.json +4356 -0
- package/hermes/utils/testsuite/skiplist.py +270 -0
- package/hermes/utils/testsuite/test_run_defs.py +435 -0
- package/hermes/utils/testsuite/typing_defs.py +52 -0
- package/hermes/utils/testsuite/utils.py +214 -0
- package/hermes/utils/tidy.sh +22 -0
- package/hermes/utils/update-fbcode-hermes.sh +53 -0
- package/hermes/utils/updateErrorTest.py +193 -0
- package/hermes/utils/wasm-compile.sh +65 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1187 @@
|
|
|
1
|
+
# IR Type System v2 — Implementation Design
|
|
2
|
+
|
|
3
|
+
## References
|
|
4
|
+
|
|
5
|
+
- [ir-type-system-v2-design.md](ir-type-system-v2-design.md) — conceptual type
|
|
6
|
+
hierarchy, type algebra, design goals, interning algorithm, migration phases.
|
|
7
|
+
- [ir-type-system.md](ir-type-system.md) — current 2-byte bitmask type system.
|
|
8
|
+
- [flow-type-system.md](flow-type-system.md) — Flow type system that feeds
|
|
9
|
+
into IR types during lowering.
|
|
10
|
+
|
|
11
|
+
This document covers the concrete C++ implementation: data structures, class
|
|
12
|
+
layouts, APIs, integration points, and invariants.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 1. Type Representation
|
|
17
|
+
|
|
18
|
+
### Current: 2-byte bitmask
|
|
19
|
+
|
|
20
|
+
```cpp
|
|
21
|
+
class Type {
|
|
22
|
+
uint16_t bitmask_; // 13 primitive kinds as bit flags
|
|
23
|
+
};
|
|
24
|
+
static_assert(sizeof(Type) == 2);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### New: 4-byte opaque index
|
|
28
|
+
|
|
29
|
+
```cpp
|
|
30
|
+
class Type {
|
|
31
|
+
uint32_t id_; // Index into TypeContext type table
|
|
32
|
+
};
|
|
33
|
+
static_assert(sizeof(Type) == 4);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`Type` is a pure opaque handle. It carries no type information itself — all
|
|
37
|
+
type data lives in the `TypeContext` type table, indexed by `id_`. Two
|
|
38
|
+
types are equal iff their `id_` values are equal.
|
|
39
|
+
|
|
40
|
+
The size increase from 2 to 4 bytes fits within the existing `Value` layout
|
|
41
|
+
constraint (`sizeof(valueType) <= 4`, enforced at `IR.h:737`).
|
|
42
|
+
|
|
43
|
+
### Well-Known Type IDs
|
|
44
|
+
|
|
45
|
+
The `TypeContext` constructor pre-allocates entries for all primitive types
|
|
46
|
+
and commonly used unions. These have fixed `id_` values:
|
|
47
|
+
|
|
48
|
+
```cpp
|
|
49
|
+
// Pre-allocated type IDs (assigned by TypeContext constructor).
|
|
50
|
+
// These are constants, valid for any TypeContext instance.
|
|
51
|
+
static constexpr uint32_t kNoTypeId = 0;
|
|
52
|
+
static constexpr uint32_t kEmptyId = 1;
|
|
53
|
+
static constexpr uint32_t kUninitId = 2;
|
|
54
|
+
static constexpr uint32_t kUndefinedId = 3;
|
|
55
|
+
static constexpr uint32_t kNullId = 4;
|
|
56
|
+
static constexpr uint32_t kBooleanId = 5;
|
|
57
|
+
static constexpr uint32_t kStringId = 6;
|
|
58
|
+
static constexpr uint32_t kNumberId = 7;
|
|
59
|
+
static constexpr uint32_t kBigIntId = 8;
|
|
60
|
+
static constexpr uint32_t kSymbolId = 9;
|
|
61
|
+
static constexpr uint32_t kEnvironmentId = 10;
|
|
62
|
+
static constexpr uint32_t kPrivateNameId = 11;
|
|
63
|
+
static constexpr uint32_t kFunctionCodeId = 12;
|
|
64
|
+
static constexpr uint32_t kObjectId = 13;
|
|
65
|
+
static constexpr uint32_t kBits32Id = 14;
|
|
66
|
+
static constexpr uint32_t kAnyTypeId = 15; // Union of all JS-observable types
|
|
67
|
+
static constexpr uint32_t kNumericId = 16; // Number | BigInt
|
|
68
|
+
static constexpr uint32_t kAnyEmptyUninitId = 17; // any | Empty | Uninit
|
|
69
|
+
// ... other common unions as needed ...
|
|
70
|
+
static constexpr uint32_t kFirstDynamicId = 32; // IDs below this are reserved
|
|
71
|
+
// The well-known ID assignment is frozen: IDs are never removed or reordered.
|
|
72
|
+
// New well-known IDs are only appended before kFirstDynamicId.
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The `Type::createX()` static methods return these well-known IDs:
|
|
76
|
+
|
|
77
|
+
```cpp
|
|
78
|
+
static constexpr Type createNoType() { return Type(kNoTypeId); }
|
|
79
|
+
static constexpr Type createNumber() { return Type(kNumberId); }
|
|
80
|
+
static constexpr Type createString() { return Type(kStringId); }
|
|
81
|
+
static constexpr Type createObject() { return Type(kObjectId); }
|
|
82
|
+
static constexpr Type createAnyType() { return Type(kAnyTypeId); }
|
|
83
|
+
static constexpr Type createBits32() { return Type(kBits32Id); }
|
|
84
|
+
// ... etc.
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
These remain `static constexpr` — no context needed to create primitive types.
|
|
88
|
+
|
|
89
|
+
### Encoding Examples
|
|
90
|
+
|
|
91
|
+
| Type | id_ | Table entry |
|
|
92
|
+
|------|-----|-------------|
|
|
93
|
+
| `NoType` | `0` | `{kind: NoType}` |
|
|
94
|
+
| `Number` | `7` | `{kind: Number}` |
|
|
95
|
+
| `Object` (unrefined) | `13` | `{kind: Object}` |
|
|
96
|
+
| `Number \| String` | `N` | `{kind: Union, arms: [Number, String]}` |
|
|
97
|
+
| `ClassA` | `N` | `{kind: ClassInstance, classId: A}` |
|
|
98
|
+
| `Array(Number)` | `N` | `{kind: Array, elemType: Number}` |
|
|
99
|
+
| `Number \| ClassA` | `N` | `{kind: Union, arms: [Number, ClassA]}` |
|
|
100
|
+
| `ClassA \| ClassB` | `N` | `{kind: Union, arms: [ClassA, ClassB]}` |
|
|
101
|
+
| `AnyType` | `15` | `{kind: Union, arms: [all JS-observable types]}` |
|
|
102
|
+
| `bits32` | `14` | `{kind: Bits32}` |
|
|
103
|
+
|
|
104
|
+
**Note on `bits32`**: `bits32` is not yet used in the current codebase. It is
|
|
105
|
+
reserved for future use (physical 32-bit integer, signless). Its well-known
|
|
106
|
+
ID and `TypeKind` enum value are defined from the start so they are stable.
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## 2. Type Table Entries
|
|
111
|
+
|
|
112
|
+
Each entry in the type table describes a type. All entries are immutable once
|
|
113
|
+
created.
|
|
114
|
+
|
|
115
|
+
The `TypeKind` enum is defined in full from the start, but refined kinds
|
|
116
|
+
(`ClassInstance`, `Array`, `Tuple`, `Function`, `ExactObject`, `Int32`,
|
|
117
|
+
`Uint32`) are dead code until their respective migration phase activates
|
|
118
|
+
them. In Phase 1, only the leaf kinds and `Union` are populated.
|
|
119
|
+
|
|
120
|
+
### TypeKind
|
|
121
|
+
|
|
122
|
+
```cpp
|
|
123
|
+
enum class TypeKind : uint8_t {
|
|
124
|
+
// --- Leaf kinds (no sub-type data) ---
|
|
125
|
+
NoType, // Empty set, bottom
|
|
126
|
+
Undefined,
|
|
127
|
+
Null,
|
|
128
|
+
Boolean,
|
|
129
|
+
Number, // Unrefined fp64
|
|
130
|
+
BigInt,
|
|
131
|
+
String,
|
|
132
|
+
Symbol,
|
|
133
|
+
Empty, // TDZ
|
|
134
|
+
Uninit, // Declared, not initialized
|
|
135
|
+
Environment,
|
|
136
|
+
PrivateName,
|
|
137
|
+
FunctionCode,
|
|
138
|
+
Object, // Unrefined JS object
|
|
139
|
+
Bits32, // Physical 32-bit integer, signless
|
|
140
|
+
|
|
141
|
+
// --- Number subtypes (still fp64, value constrained) ---
|
|
142
|
+
Int32, // Integer in [-2^31, 2^31 - 1]
|
|
143
|
+
Uint32, // Integer in [0, 2^32 - 1]
|
|
144
|
+
|
|
145
|
+
// --- Refined object types ---
|
|
146
|
+
ClassInstance, // Nominal class instance
|
|
147
|
+
Array, // Array with known element type
|
|
148
|
+
Tuple, // Fixed-length positional types
|
|
149
|
+
Function, // Callable with known signature
|
|
150
|
+
ExactObject, // Known exact field set
|
|
151
|
+
|
|
152
|
+
// --- Composite ---
|
|
153
|
+
Union, // Union of two or more types
|
|
154
|
+
};
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### TypeEntry
|
|
158
|
+
|
|
159
|
+
```cpp
|
|
160
|
+
struct TypeEntry {
|
|
161
|
+
TypeKind kind;
|
|
162
|
+
|
|
163
|
+
union {
|
|
164
|
+
// ClassInstance
|
|
165
|
+
struct {
|
|
166
|
+
uint32_t superClassTypeId; // type table ID of parent ClassInstance,
|
|
167
|
+
// or kNoTypeId if no parent
|
|
168
|
+
Identifier className; // for runtime type descriptors / errors
|
|
169
|
+
uint32_t fieldOffset; // into fieldArrays_
|
|
170
|
+
uint16_t fieldCount;
|
|
171
|
+
} classInstance;
|
|
172
|
+
|
|
173
|
+
// Array
|
|
174
|
+
struct {
|
|
175
|
+
Type elemType;
|
|
176
|
+
} array;
|
|
177
|
+
|
|
178
|
+
// Tuple
|
|
179
|
+
struct {
|
|
180
|
+
uint32_t elemOffset; // into typeArrays_
|
|
181
|
+
uint16_t elemCount;
|
|
182
|
+
} tuple;
|
|
183
|
+
|
|
184
|
+
// Function
|
|
185
|
+
struct {
|
|
186
|
+
Type returnType;
|
|
187
|
+
Type thisType; // NoType if no `this` parameter
|
|
188
|
+
Type restType; // NoType if no rest parameter
|
|
189
|
+
uint32_t paramOffset; // into paramArrays_
|
|
190
|
+
uint16_t paramCount;
|
|
191
|
+
bool isAsync;
|
|
192
|
+
bool isGenerator;
|
|
193
|
+
} function;
|
|
194
|
+
|
|
195
|
+
// ExactObject
|
|
196
|
+
struct {
|
|
197
|
+
uint32_t fieldOffset; // into fieldArrays_
|
|
198
|
+
uint16_t fieldCount;
|
|
199
|
+
} exactObject;
|
|
200
|
+
|
|
201
|
+
// Union
|
|
202
|
+
struct {
|
|
203
|
+
uint32_t armOffset; // into typeArrays_
|
|
204
|
+
uint16_t armCount; // >= 2
|
|
205
|
+
} union_;
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Side Arrays
|
|
211
|
+
|
|
212
|
+
Variable-length data is stored in side arrays within `TypeContext`,
|
|
213
|
+
referenced by (offset, count) pairs in the `TypeEntry`:
|
|
214
|
+
|
|
215
|
+
| Side array | Element type | Used by |
|
|
216
|
+
|-----------|-------------|---------|
|
|
217
|
+
| `typeArrays_` | `Type` | Tuple elements, Union arms |
|
|
218
|
+
| `paramArrays_` | `FunctionParam` | Function parameters |
|
|
219
|
+
| `fieldArrays_` | `ExactObjectField` | ExactObject fields |
|
|
220
|
+
|
|
221
|
+
```cpp
|
|
222
|
+
struct FunctionParam {
|
|
223
|
+
Type type;
|
|
224
|
+
bool isOptional;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
struct ExactObjectField {
|
|
228
|
+
Identifier name;
|
|
229
|
+
Type type;
|
|
230
|
+
};
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
**ExactObject field ordering**: field order is preserved and significant.
|
|
234
|
+
Two ExactObjects with the same fields in different orders are different types.
|
|
235
|
+
The intern key hashes the fields in order, so `{x: number, y: string}` and
|
|
236
|
+
`{y: string, x: number}` produce different type table entries.
|
|
237
|
+
|
|
238
|
+
**C++17 note**: the `TypeEntry` pseudo-code above uses C++20 designated
|
|
239
|
+
initializers (`{.array = {elemType}}`). The actual C++17 implementation will
|
|
240
|
+
use explicit construction or a tagged-union helper.
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
## 3. TypeContext
|
|
245
|
+
|
|
246
|
+
### Responsibilities
|
|
247
|
+
|
|
248
|
+
- Owns the type table (all `TypeEntry` values).
|
|
249
|
+
- Provides all type operations (union, intersect, subtract, isSubsetOf).
|
|
250
|
+
- Stores the class inheritance hierarchy.
|
|
251
|
+
- Interns types via hash-consing.
|
|
252
|
+
|
|
253
|
+
### Ownership
|
|
254
|
+
|
|
255
|
+
One `TypeContext` per `Module`:
|
|
256
|
+
|
|
257
|
+
```cpp
|
|
258
|
+
class Module : public Value {
|
|
259
|
+
// ...
|
|
260
|
+
TypeContext typeContext_;
|
|
261
|
+
// ...
|
|
262
|
+
public:
|
|
263
|
+
TypeContext &getTypeContext() { return typeContext_; }
|
|
264
|
+
};
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
**Header dependency**: `Type` is defined in `IR.h`. `TypeContext` uses
|
|
268
|
+
`Type` and is defined in `TypeContext.h`. `Module` (in `IR.h`) embeds
|
|
269
|
+
`TypeContext` by value. The include order is:
|
|
270
|
+
1. `IR.h` forward-declares `class TypeContext;`
|
|
271
|
+
2. `IR.h` defines `Type` (before `Module`).
|
|
272
|
+
3. `IR.h` defines `Module` with `TypeContext typeContext_;` — this
|
|
273
|
+
requires the full definition, so `IR.h` includes `TypeContext.h`
|
|
274
|
+
above the `Module` class definition (after `Type` is defined).
|
|
275
|
+
4. `TypeContext.h` includes only the `Type` definition (not `Module`).
|
|
276
|
+
|
|
277
|
+
### Storage Layout
|
|
278
|
+
|
|
279
|
+
```cpp
|
|
280
|
+
class TypeContext {
|
|
281
|
+
public:
|
|
282
|
+
// --- Phase 1: thread-local "current" context ---
|
|
283
|
+
static TypeContext ¤t();
|
|
284
|
+
|
|
285
|
+
// --- Type operations ---
|
|
286
|
+
Type unionTy(Type A, Type B);
|
|
287
|
+
Type intersectTy(Type A, Type B);
|
|
288
|
+
Type subtractTy(Type A, Type B);
|
|
289
|
+
bool isSubsetOf(Type A, Type B);
|
|
290
|
+
|
|
291
|
+
// --- Type queries ---
|
|
292
|
+
TypeKind getKind(Type t) const;
|
|
293
|
+
bool canBeNumber(Type t) const;
|
|
294
|
+
bool canBeString(Type t) const;
|
|
295
|
+
bool canBeObject(Type t) const;
|
|
296
|
+
bool canBeNull(Type t) const;
|
|
297
|
+
bool canBeUndefined(Type t) const;
|
|
298
|
+
bool isPrimitive(Type t) const;
|
|
299
|
+
bool isNoType(Type t) const;
|
|
300
|
+
// ... etc.
|
|
301
|
+
|
|
302
|
+
// --- Refined type construction ---
|
|
303
|
+
Type createClassInstance(
|
|
304
|
+
Type superClassType, Identifier className,
|
|
305
|
+
llvh::ArrayRef<ExactObjectField> fields);
|
|
306
|
+
Type createArray(Type elemType);
|
|
307
|
+
Type createTuple(llvh::ArrayRef<Type> elemTypes);
|
|
308
|
+
Type createFunction(
|
|
309
|
+
Type returnType,
|
|
310
|
+
Type thisType,
|
|
311
|
+
Type restType,
|
|
312
|
+
llvh::ArrayRef<FunctionParam> params,
|
|
313
|
+
bool isAsync,
|
|
314
|
+
bool isGenerator);
|
|
315
|
+
Type createExactObject(llvh::ArrayRef<ExactObjectField> fields);
|
|
316
|
+
|
|
317
|
+
// --- Refined type accessors ---
|
|
318
|
+
Type getClassSuperType(Type t) const;
|
|
319
|
+
Identifier getClassName(Type t) const;
|
|
320
|
+
Type getArrayElementType(Type t) const;
|
|
321
|
+
const FunctionParam *getFunctionParams(Type t, uint16_t &count) const;
|
|
322
|
+
// ... etc.
|
|
323
|
+
|
|
324
|
+
// --- Class hierarchy ---
|
|
325
|
+
bool isSubclass(Type a, Type b) const;
|
|
326
|
+
|
|
327
|
+
private:
|
|
328
|
+
static thread_local TypeContext *current_;
|
|
329
|
+
|
|
330
|
+
// Type table. Index 0 = NoType. Pre-allocated entries for primitives.
|
|
331
|
+
std::vector<TypeEntry> entries_;
|
|
332
|
+
|
|
333
|
+
// Side arrays for variable-length data.
|
|
334
|
+
std::vector<Type> typeArrays_;
|
|
335
|
+
std::vector<FunctionParam> paramArrays_;
|
|
336
|
+
std::vector<ExactObjectField> fieldArrays_;
|
|
337
|
+
|
|
338
|
+
// Interning table: structural hash → type ID.
|
|
339
|
+
llvh::DenseMap<TypeEntryKey, uint32_t> internTable_;
|
|
340
|
+
};
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Construction
|
|
344
|
+
|
|
345
|
+
The `TypeContext` constructor pre-allocates all well-known types:
|
|
346
|
+
|
|
347
|
+
```cpp
|
|
348
|
+
TypeContext::TypeContext() {
|
|
349
|
+
// Index 0: NoType
|
|
350
|
+
entries_.push_back({TypeKind::NoType, {}});
|
|
351
|
+
// Index 1: Empty
|
|
352
|
+
entries_.push_back({TypeKind::Empty, {}});
|
|
353
|
+
// Index 2: Uninit
|
|
354
|
+
entries_.push_back({TypeKind::Uninit, {}});
|
|
355
|
+
// ... all primitives and well-known unions ...
|
|
356
|
+
// Index kAnyTypeId: Union of all JS-observable types
|
|
357
|
+
// Index kNumericId: Union(Number, BigInt)
|
|
358
|
+
// ...
|
|
359
|
+
assert(entries_.size() == kFirstDynamicId);
|
|
360
|
+
}
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### Interning
|
|
364
|
+
|
|
365
|
+
Every type construction method interns the result. Since sub-types are already
|
|
366
|
+
interned (they are `Type` values = indices), the intern key is a shallow hash
|
|
367
|
+
of the `TypeEntry` contents:
|
|
368
|
+
|
|
369
|
+
```
|
|
370
|
+
createArray(elemType):
|
|
371
|
+
key = {kind: Array, elemType.id_}
|
|
372
|
+
if internTable_.count(key):
|
|
373
|
+
return Type(internTable_[key])
|
|
374
|
+
id = entries_.size()
|
|
375
|
+
entries_.push_back({TypeKind::Array, {.array = {elemType}}})
|
|
376
|
+
internTable_[key] = id
|
|
377
|
+
return Type(id)
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
No deep recursive hashing is needed because sub-types are indices.
|
|
381
|
+
|
|
382
|
+
---
|
|
383
|
+
|
|
384
|
+
## 4. Type Operations
|
|
385
|
+
|
|
386
|
+
All type operations are methods on `TypeContext`. They look up the
|
|
387
|
+
`TypeEntry` for each operand and dispatch based on `TypeKind`.
|
|
388
|
+
|
|
389
|
+
### unionTy(A, B)
|
|
390
|
+
|
|
391
|
+
```cpp
|
|
392
|
+
Type TypeContext::unionTy(Type A, Type B) {
|
|
393
|
+
if (A == B) return A;
|
|
394
|
+
if (isNoType(A)) return B;
|
|
395
|
+
if (isNoType(B)) return A;
|
|
396
|
+
if (isSubsetOf(A, B)) return B;
|
|
397
|
+
if (isSubsetOf(B, A)) return A;
|
|
398
|
+
return createUnionImpl(A, B);
|
|
399
|
+
}
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
`createUnionImpl` constructs a canonical `Union` entry:
|
|
403
|
+
1. Flatten: if A or B is already a Union, extract its arms.
|
|
404
|
+
2. Collect all arms into a single list.
|
|
405
|
+
3. Deduplicate: remove identical arms (same `id_`).
|
|
406
|
+
4. Subsume: if arm X is a subtype of arm Y, drop X.
|
|
407
|
+
5. Sort arms by `id_` for canonical order.
|
|
408
|
+
6. If one arm remains, return it directly (no Union entry).
|
|
409
|
+
7. Intern the Union entry.
|
|
410
|
+
|
|
411
|
+
Special cases for object refinements:
|
|
412
|
+
- `union(ClassA, ClassB)` where A extends B → return B.
|
|
413
|
+
- `union(refined, Object)` → return Object (unrefined subsumes all).
|
|
414
|
+
- `union(Array(S), Array(T))` where S ≠ T → Union (arrays are invariant).
|
|
415
|
+
|
|
416
|
+
### intersectTy(A, B)
|
|
417
|
+
|
|
418
|
+
```cpp
|
|
419
|
+
Type TypeContext::intersectTy(Type A, Type B) {
|
|
420
|
+
if (A == B) return A;
|
|
421
|
+
if (isSubsetOf(A, B)) return A;
|
|
422
|
+
if (isSubsetOf(B, A)) return B;
|
|
423
|
+
return intersectImpl(A, B);
|
|
424
|
+
}
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
`intersectImpl` rules:
|
|
428
|
+
- Two disjoint leaf types → NoType.
|
|
429
|
+
- `intersect(Object, ClassA)` → ClassA.
|
|
430
|
+
- `intersect(ClassA, ClassB)` unrelated → NoType.
|
|
431
|
+
- Distribute over unions on either side:
|
|
432
|
+
`intersect(Union(A₁,...,Aₙ), B)` = `union(intersect(A₁,B), ..., intersect(Aₙ,B))`.
|
|
433
|
+
`intersect(A, Union(B₁,...,Bₙ))` = `union(intersect(A,B₁), ..., intersect(A,Bₙ))`.
|
|
434
|
+
|
|
435
|
+
Note: cases like `intersect(Number, Int32)` and `intersect(ClassA, ClassB)`
|
|
436
|
+
where one is a subtype of the other are handled by the `isSubsetOf` checks
|
|
437
|
+
in `intersectTy` before `intersectImpl` is reached.
|
|
438
|
+
|
|
439
|
+
### subtractTy(A, B)
|
|
440
|
+
|
|
441
|
+
```cpp
|
|
442
|
+
Type TypeContext::subtractTy(Type A, Type B) {
|
|
443
|
+
if (isSubsetOf(A, B)) return createNoType();
|
|
444
|
+
if (areDisjoint(A, B)) return A;
|
|
445
|
+
return subtractImpl(A, B);
|
|
446
|
+
}
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
`subtractImpl` rules:
|
|
450
|
+
- Distribute over unions in A:
|
|
451
|
+
`subtract(Union(A₁,...,Aₙ), B)` = `union(subtract(A₁,B), ..., subtract(Aₙ,B))`.
|
|
452
|
+
- `subtract(Number, Int32)` → Number (conservative).
|
|
453
|
+
- `subtract(Object, ClassA)` → Object (conservative).
|
|
454
|
+
|
|
455
|
+
### isSubsetOf(A, B)
|
|
456
|
+
|
|
457
|
+
```cpp
|
|
458
|
+
bool TypeContext::isSubsetOf(Type A, Type B) {
|
|
459
|
+
if (A == B) return true;
|
|
460
|
+
if (isNoType(A)) return true;
|
|
461
|
+
|
|
462
|
+
TypeKind akind = getKind(A);
|
|
463
|
+
TypeKind bkind = getKind(B);
|
|
464
|
+
|
|
465
|
+
// Union on left: every arm must be a subset.
|
|
466
|
+
if (akind == TypeKind::Union) {
|
|
467
|
+
for (Type arm : getUnionArms(A))
|
|
468
|
+
if (!isSubsetOf(arm, B))
|
|
469
|
+
return false;
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Union on right: A must be a subset of some arm.
|
|
474
|
+
if (bkind == TypeKind::Union) {
|
|
475
|
+
for (Type arm : getUnionArms(B))
|
|
476
|
+
if (isSubsetOf(A, arm))
|
|
477
|
+
return true;
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// Both are non-union.
|
|
482
|
+
return isBaseSubsetOf(A, akind, B, bkind);
|
|
483
|
+
}
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
`isBaseSubsetOf` handles non-union cases:
|
|
487
|
+
- `Int32 <: Number`, `Uint32 <: Number`.
|
|
488
|
+
- `ClassA <: Object` (any refined object is a subset of unrefined Object).
|
|
489
|
+
- `ClassA <: ClassB` iff A is a subclass of B (walked via
|
|
490
|
+
`superClassTypeId` in the type table entries).
|
|
491
|
+
- All other cross-kind pairs → false.
|
|
492
|
+
|
|
493
|
+
### areDisjoint(A, B)
|
|
494
|
+
|
|
495
|
+
Direct kind-based check without materializing an intersection:
|
|
496
|
+
|
|
497
|
+
```cpp
|
|
498
|
+
bool TypeContext::areDisjoint(Type A, Type B) {
|
|
499
|
+
if (A == B) return isNoType(A);
|
|
500
|
+
|
|
501
|
+
TypeKind ak = getKind(A);
|
|
502
|
+
TypeKind bk = getKind(B);
|
|
503
|
+
|
|
504
|
+
// Distribute over unions.
|
|
505
|
+
if (ak == TypeKind::Union) {
|
|
506
|
+
for (Type arm : getUnionArms(A))
|
|
507
|
+
if (!areDisjoint(arm, B))
|
|
508
|
+
return false;
|
|
509
|
+
return true;
|
|
510
|
+
}
|
|
511
|
+
if (bk == TypeKind::Union) {
|
|
512
|
+
for (Type arm : getUnionArms(B))
|
|
513
|
+
if (!areDisjoint(A, arm))
|
|
514
|
+
return false;
|
|
515
|
+
return true;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// Two non-union types: disjoint unless one is a subtype of the other
|
|
519
|
+
// or they share a kind with overlap (e.g., ClassA/ClassB with inheritance).
|
|
520
|
+
return !isBaseSubsetOf(A, ak, B, bk) && !isBaseSubsetOf(B, bk, A, ak)
|
|
521
|
+
&& !haveKindOverlap(ak, bk);
|
|
522
|
+
}
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
This avoids interning throwaway intersection results.
|
|
526
|
+
|
|
527
|
+
---
|
|
528
|
+
|
|
529
|
+
## 5. API Surface
|
|
530
|
+
|
|
531
|
+
### Type Class (Thin Handle)
|
|
532
|
+
|
|
533
|
+
`Type` is a thin 4-byte handle. It provides only operations that don't
|
|
534
|
+
require the type table: identity comparison, hashing, and well-known
|
|
535
|
+
constructors.
|
|
536
|
+
|
|
537
|
+
```cpp
|
|
538
|
+
class Type {
|
|
539
|
+
uint32_t id_;
|
|
540
|
+
|
|
541
|
+
constexpr explicit Type(uint32_t id) : id_(id) {}
|
|
542
|
+
|
|
543
|
+
public:
|
|
544
|
+
// --- Identity ---
|
|
545
|
+
constexpr bool operator==(Type RHS) const { return id_ == RHS.id_; }
|
|
546
|
+
constexpr bool operator!=(Type RHS) const { return id_ != RHS.id_; }
|
|
547
|
+
llvh::hash_code hash() const { return llvh::hash_value(id_); }
|
|
548
|
+
|
|
549
|
+
// --- Well-known constructors (constexpr, no context needed) ---
|
|
550
|
+
static constexpr Type createNoType() { return Type(kNoTypeId); }
|
|
551
|
+
static constexpr Type createUndefined() { return Type(kUndefinedId); }
|
|
552
|
+
static constexpr Type createNull() { return Type(kNullId); }
|
|
553
|
+
static constexpr Type createBoolean() { return Type(kBooleanId); }
|
|
554
|
+
static constexpr Type createString() { return Type(kStringId); }
|
|
555
|
+
static constexpr Type createNumber() { return Type(kNumberId); }
|
|
556
|
+
static constexpr Type createBigInt() { return Type(kBigIntId); }
|
|
557
|
+
static constexpr Type createSymbol() { return Type(kSymbolId); }
|
|
558
|
+
static constexpr Type createObject() { return Type(kObjectId); }
|
|
559
|
+
static constexpr Type createEmpty() { return Type(kEmptyId); }
|
|
560
|
+
static constexpr Type createUninit() { return Type(kUninitId); }
|
|
561
|
+
static constexpr Type createEnvironment() { return Type(kEnvironmentId); }
|
|
562
|
+
static constexpr Type createPrivateName() { return Type(kPrivateNameId); }
|
|
563
|
+
static constexpr Type createFunctionCode() { return Type(kFunctionCodeId); }
|
|
564
|
+
static constexpr Type createBits32() { return Type(kBits32Id); }
|
|
565
|
+
static constexpr Type createAnyType() { return Type(kAnyTypeId); }
|
|
566
|
+
static constexpr Type createNumeric() { return Type(kNumericId); }
|
|
567
|
+
|
|
568
|
+
// Aliases (backwards compatibility, see ir-type-system.md).
|
|
569
|
+
static constexpr Type createInt32() { return createNumber(); }
|
|
570
|
+
static constexpr Type createUint32() { return createNumber(); }
|
|
571
|
+
|
|
572
|
+
// Pre-allocated well-known union (backwards compatibility).
|
|
573
|
+
static constexpr Type createAnyEmptyUninit() {
|
|
574
|
+
return Type(kAnyEmptyUninitId);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// --- Context-dependent operations (Phase 1: use thread-local) ---
|
|
578
|
+
// These delegate to TypeContext::current().
|
|
579
|
+
|
|
580
|
+
static Type unionTy(Type A, Type B);
|
|
581
|
+
static Type intersectTy(Type A, Type B);
|
|
582
|
+
static Type subtractTy(Type A, Type B);
|
|
583
|
+
bool isSubsetOf(Type t) const;
|
|
584
|
+
bool canBeType(Type t) const;
|
|
585
|
+
|
|
586
|
+
// --- Context-dependent queries (Phase 1: use thread-local) ---
|
|
587
|
+
bool isNoType() const;
|
|
588
|
+
bool isAnyType() const;
|
|
589
|
+
bool canBeNumber() const;
|
|
590
|
+
bool canBeString() const;
|
|
591
|
+
bool canBeObject() const;
|
|
592
|
+
bool canBeNull() const;
|
|
593
|
+
bool canBeUndefined() const;
|
|
594
|
+
bool canBeEmpty() const;
|
|
595
|
+
bool canBeUninit() const;
|
|
596
|
+
bool canBeBigInt() const;
|
|
597
|
+
bool canBeBoolean() const;
|
|
598
|
+
bool canBeSymbol() const;
|
|
599
|
+
bool isPrimitive() const;
|
|
600
|
+
bool canBePrimitive() const;
|
|
601
|
+
bool canBeAny() const;
|
|
602
|
+
bool isNonPtr() const;
|
|
603
|
+
bool isNumberType() const;
|
|
604
|
+
bool isStringType() const;
|
|
605
|
+
bool isObjectType() const;
|
|
606
|
+
bool isAnyEmptyUninitType() const;
|
|
607
|
+
|
|
608
|
+
// Reimplemented on top of the new representation (delegate to
|
|
609
|
+
// TypeContext::current()):
|
|
610
|
+
//
|
|
611
|
+
// countTypes(): returns 0 for NoType, arm count for Union, 1 otherwise.
|
|
612
|
+
// getFirstTypeKind(): returns the TypeKind from the table entry
|
|
613
|
+
// (or first arm's kind for Union).
|
|
614
|
+
// Type::iterator: iterates over union arms (or yields the single kind
|
|
615
|
+
// for non-union types). Delegates to TypeContext::getUnionArms()
|
|
616
|
+
// for unions.
|
|
617
|
+
|
|
618
|
+
void print(llvh::raw_ostream &OS) const;
|
|
619
|
+
void Profile(llvh::FoldingSetNodeID &ID) const { ID.AddInteger(id_); }
|
|
620
|
+
|
|
621
|
+
friend class TypeContext;
|
|
622
|
+
};
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
### Phase 1: Delegation to Thread-Local
|
|
626
|
+
|
|
627
|
+
All context-dependent methods delegate to the thread-local context:
|
|
628
|
+
|
|
629
|
+
```cpp
|
|
630
|
+
// Static operations:
|
|
631
|
+
Type Type::unionTy(Type A, Type B) {
|
|
632
|
+
return TypeContext::current().unionTy(A, B);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// Instance queries:
|
|
636
|
+
bool Type::canBeNumber() const {
|
|
637
|
+
return TypeContext::current().canBeNumber(*this);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
bool Type::isNoType() const {
|
|
641
|
+
return id_ == kNoTypeId;
|
|
642
|
+
}
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
Note: some queries like `isNoType()` and `isAnyType()` can remain constexpr
|
|
646
|
+
by comparing against well-known IDs. Others like `canBeNumber()` must consult
|
|
647
|
+
the context because a Union containing Number should also return true.
|
|
648
|
+
|
|
649
|
+
### canBeX() Implementation in TypeContext
|
|
650
|
+
|
|
651
|
+
```cpp
|
|
652
|
+
bool TypeContext::canBeNumber(Type t) const {
|
|
653
|
+
// Fast path: well-known IDs.
|
|
654
|
+
if (t.id_ == kAnyTypeId || t.id_ == kNumberId || t.id_ == kNumericId)
|
|
655
|
+
return true;
|
|
656
|
+
|
|
657
|
+
TypeKind kind = getKind(t);
|
|
658
|
+
if (kind == TypeKind::Number || kind == TypeKind::Int32 ||
|
|
659
|
+
kind == TypeKind::Uint32)
|
|
660
|
+
return true;
|
|
661
|
+
if (kind == TypeKind::Union) {
|
|
662
|
+
for (Type arm : getUnionArms(t))
|
|
663
|
+
if (canBeNumber(arm))
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
666
|
+
return false;
|
|
667
|
+
}
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
The well-known ID checks avoid table lookups and union arm iteration for
|
|
671
|
+
the most common types (`AnyType`, `Number`, `Numeric`). Each `canBeX()`
|
|
672
|
+
method has its own set of well-known fast paths. This prevents the
|
|
673
|
+
performance regression that would otherwise occur from `AnyType` being
|
|
674
|
+
represented as a multi-arm union.
|
|
675
|
+
|
|
676
|
+
Union arm recursion terminates because unions are always flattened (arms
|
|
677
|
+
are never themselves unions).
|
|
678
|
+
|
|
679
|
+
---
|
|
680
|
+
|
|
681
|
+
## 6. Class Hierarchy
|
|
682
|
+
|
|
683
|
+
There is no separate class registry. Class identity is the type table
|
|
684
|
+
`id_`, and the inheritance chain is stored directly in the `TypeEntry`
|
|
685
|
+
payload (`classInstance.superClassTypeId`).
|
|
686
|
+
|
|
687
|
+
### Registration
|
|
688
|
+
|
|
689
|
+
```cpp
|
|
690
|
+
Type TypeContext::createClassInstance(
|
|
691
|
+
Type superClassType,
|
|
692
|
+
Identifier className,
|
|
693
|
+
llvh::ArrayRef<ExactObjectField> fields) {
|
|
694
|
+
// ClassInstance entries are NOT interned — each class declaration
|
|
695
|
+
// produces a unique entry, even if structurally identical to another.
|
|
696
|
+
uint32_t id = entries_.size();
|
|
697
|
+
TypeEntry entry{};
|
|
698
|
+
entry.kind = TypeKind::ClassInstance;
|
|
699
|
+
entry.classInstance.superClassTypeId = superClassType.id_;
|
|
700
|
+
entry.classInstance.className = className;
|
|
701
|
+
entry.classInstance.fieldOffset = fieldArrays_.size();
|
|
702
|
+
entry.classInstance.fieldCount = fields.size();
|
|
703
|
+
fieldArrays_.insert(fieldArrays_.end(), fields.begin(), fields.end());
|
|
704
|
+
entries_.push_back(entry);
|
|
705
|
+
return Type(id);
|
|
706
|
+
}
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
### Subtype Check
|
|
710
|
+
|
|
711
|
+
```cpp
|
|
712
|
+
bool TypeContext::isSubclass(Type a, Type b) const {
|
|
713
|
+
assert(getKind(a) == TypeKind::ClassInstance);
|
|
714
|
+
assert(getKind(b) == TypeKind::ClassInstance);
|
|
715
|
+
uint32_t cur = a.id_;
|
|
716
|
+
while (cur != kNoTypeId) {
|
|
717
|
+
if (cur == b.id_)
|
|
718
|
+
return true;
|
|
719
|
+
cur = entries_[cur].classInstance.superClassTypeId;
|
|
720
|
+
}
|
|
721
|
+
return false;
|
|
722
|
+
}
|
|
723
|
+
```
|
|
724
|
+
|
|
725
|
+
O(depth), typically small. Can be optimized later with DFS numbering if
|
|
726
|
+
needed.
|
|
727
|
+
|
|
728
|
+
---
|
|
729
|
+
|
|
730
|
+
## 7. Interning
|
|
731
|
+
|
|
732
|
+
### Within TypeContext (Acyclic Path)
|
|
733
|
+
|
|
734
|
+
For non-looping types, all type construction methods intern their results.
|
|
735
|
+
Since sub-types are `Type` values (= integer indices) and are already
|
|
736
|
+
interned before their parent, the intern key is a shallow hash of the
|
|
737
|
+
entry's contents — no recursive hashing needed.
|
|
738
|
+
|
|
739
|
+
For looping (recursive) types, a separate batch allocation protocol is
|
|
740
|
+
used — see "Batch Allocation for Looping Types" below.
|
|
741
|
+
|
|
742
|
+
```cpp
|
|
743
|
+
struct TypeEntryKey {
|
|
744
|
+
TypeKind kind;
|
|
745
|
+
// Kind-specific content, using Type id_ values for sub-types.
|
|
746
|
+
// Structurally hashed and compared.
|
|
747
|
+
unsigned hash() const;
|
|
748
|
+
bool operator==(const TypeEntryKey &other) const;
|
|
749
|
+
};
|
|
750
|
+
```
|
|
751
|
+
|
|
752
|
+
Example: `Array{elemType: Number}` hashes as `hash(Array, kNumberId)`.
|
|
753
|
+
|
|
754
|
+
The `internTable_` maps `TypeEntryKey → uint32_t` (type table index).
|
|
755
|
+
Duplicate entries are never created.
|
|
756
|
+
|
|
757
|
+
### From Flow Types
|
|
758
|
+
|
|
759
|
+
When lowering Flow types to IR types, the three-table interning algorithm
|
|
760
|
+
from [ir-type-system-v2-design.md §Interning Algorithm](ir-type-system-v2-design.md#interning-algorithm-flow-types-to-ir-types)
|
|
761
|
+
is used:
|
|
762
|
+
|
|
763
|
+
1. **Identity table**: `flow::Type *` → `ir::Type` (pointer-keyed fast path).
|
|
764
|
+
2. **Non-looping dedup table**: `const TypeInfo *` → `ir::Type`, keyed by
|
|
765
|
+
Flow's `TypeInfo::hash()` (intentionally shallow — kind + arity, not
|
|
766
|
+
deep structural) and `TypeInfo::equals()` (deep structural for
|
|
767
|
+
non-looping types). The shallow hash means more collisions, but
|
|
768
|
+
`equals()` resolves them correctly.
|
|
769
|
+
3. **Looping dedup table**: `const TypeInfo *` → `ir::Type`, keyed by
|
|
770
|
+
shallow hash + co-inductive equals.
|
|
771
|
+
|
|
772
|
+
**Important**: the identity table is keyed by `flow::Type *` (the wrapper),
|
|
773
|
+
not `TypeInfo *`. The `isLooping` flag that controls routing between tables
|
|
774
|
+
2 and 3 lives on the `Type` wrapper (`FlowContext.h:175`), not on `TypeInfo`.
|
|
775
|
+
Two different `Type *` wrappers can point to the same `TypeInfo *` with
|
|
776
|
+
different `isLooping` flags. The dedup tables (2 and 3) are keyed by
|
|
777
|
+
`TypeInfo *` using structural equality, which produces correct results
|
|
778
|
+
regardless of `isLooping`.
|
|
779
|
+
|
|
780
|
+
This interning algorithm *replaces* the current `flowTypeToIRType` static
|
|
781
|
+
method (`ESTreeIRGen-typed-class.cpp:410–454`), which does not handle
|
|
782
|
+
cycles.
|
|
783
|
+
|
|
784
|
+
See the design document for the full algorithm and rationale.
|
|
785
|
+
|
|
786
|
+
### Batch Allocation for Looping Types
|
|
787
|
+
|
|
788
|
+
The acyclic interning path (§7 "Within TypeContext") assumes sub-types
|
|
789
|
+
are already interned before their parent. For looping (recursive) types,
|
|
790
|
+
this assumption breaks: the sub-types include the type itself.
|
|
791
|
+
|
|
792
|
+
Consider `type Tree = null | {left: Tree, right: Tree}`. The cycle is:
|
|
793
|
+
|
|
794
|
+
```
|
|
795
|
+
Union(Null, ExactObject) → ExactObject → field "left" → Union → ...
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
The Union, the ExactObject, and the field reference to Union all need IDs,
|
|
799
|
+
but they refer to each other.
|
|
800
|
+
|
|
801
|
+
#### Construction Protocol
|
|
802
|
+
|
|
803
|
+
Looping types are constructed in three phases within `TypeContext`:
|
|
804
|
+
|
|
805
|
+
**Phase A — Reserve IDs.** Walk the cycle cluster (all reachable looping
|
|
806
|
+
types not yet in the identity table). For each node, allocate an `entries_`
|
|
807
|
+
slot and assign an ID, but leave the entry contents uninitialized
|
|
808
|
+
(placeholder):
|
|
809
|
+
|
|
810
|
+
```cpp
|
|
811
|
+
// Reserve an ID without filling in the entry.
|
|
812
|
+
uint32_t TypeContext::reserveEntry() {
|
|
813
|
+
uint32_t id = entries_.size();
|
|
814
|
+
entries_.push_back(TypeEntry{}); // placeholder, kind = unset
|
|
815
|
+
return id;
|
|
816
|
+
}
|
|
817
|
+
```
|
|
818
|
+
|
|
819
|
+
Record the mapping from each `flow::Type *` to its reserved ID in the
|
|
820
|
+
identity table immediately. This ensures that when we encounter the same
|
|
821
|
+
type again during the fill phase (the cycle), we find the reserved ID
|
|
822
|
+
instead of recursing infinitely.
|
|
823
|
+
|
|
824
|
+
**Phase B — Fill entries.** Walk the cluster again. For each node, construct
|
|
825
|
+
the `TypeEntry` using the reserved IDs for cross-references:
|
|
826
|
+
|
|
827
|
+
```cpp
|
|
828
|
+
// For the ExactObject node in the Tree example:
|
|
829
|
+
TypeEntry &entry = entries_[reservedExactObjectId];
|
|
830
|
+
entry.kind = TypeKind::ExactObject;
|
|
831
|
+
// The field "left" references the Union, whose ID was reserved in Phase A.
|
|
832
|
+
entry.exactObject.fieldOffset = fieldArrays_.size();
|
|
833
|
+
entry.exactObject.fieldCount = 2;
|
|
834
|
+
fieldArrays_.push_back({leftName, Type(reservedUnionId)});
|
|
835
|
+
fieldArrays_.push_back({rightName, Type(reservedUnionId)});
|
|
836
|
+
```
|
|
837
|
+
|
|
838
|
+
The key insight: sub-type references use the **reserved** IDs, which are
|
|
839
|
+
final — they won't change. The entries are incomplete only in that their
|
|
840
|
+
contents haven't been written yet, but their IDs are stable.
|
|
841
|
+
|
|
842
|
+
Union canonicalization is partially performed during this phase: arms are
|
|
843
|
+
deduplicated and sorted by `id_` (all arms have assigned IDs from Phase A).
|
|
844
|
+
Subsumption is **skipped** because it would need to inspect entry contents
|
|
845
|
+
that may still be placeholders. This is safe because Flow has already
|
|
846
|
+
canonicalized its unions — see §8 for details.
|
|
847
|
+
|
|
848
|
+
**Phase C — Intern.** After all entries are filled, insert each into the
|
|
849
|
+
`internTable_`. The key is computed from the now-complete entry contents.
|
|
850
|
+
If a structurally identical entry already exists (detected via the looping
|
|
851
|
+
dedup table using co-inductive equality in the Flow lowering algorithm),
|
|
852
|
+
the reserved entry is wasted — it remains in `entries_` but is never
|
|
853
|
+
referenced. This is a minor space cost.
|
|
854
|
+
|
|
855
|
+
#### Invariants During Construction
|
|
856
|
+
|
|
857
|
+
- `entries_` may contain **placeholder entries** (kind = unset) between
|
|
858
|
+
Phase A and Phase B. These must never be accessed by type operations.
|
|
859
|
+
Since the looping cluster is being constructed by the lowering pass (not
|
|
860
|
+
by an optimization pass), no concurrent type operations reference these
|
|
861
|
+
IDs until construction completes.
|
|
862
|
+
|
|
863
|
+
- The `internTable_` does not contain entries for placeholder types. It is
|
|
864
|
+
populated only in Phase C after entries are finalized. This means
|
|
865
|
+
`createArray()`, `createExactObject()`, etc. (the normal acyclic intern
|
|
866
|
+
path) will not find or collide with partially-constructed entries.
|
|
867
|
+
|
|
868
|
+
- The identity table (`flow::Type *` → `ir::Type`) IS populated during
|
|
869
|
+
Phase A. This is what breaks the cycle: when the fill phase encounters a
|
|
870
|
+
`flow::Type *` that is part of the cluster, it finds the reserved ID in
|
|
871
|
+
the identity table and uses it directly.
|
|
872
|
+
|
|
873
|
+
#### Example: Interning `Tree = null | {left: Tree, right: Tree}`
|
|
874
|
+
|
|
875
|
+
```
|
|
876
|
+
Phase A (reserve):
|
|
877
|
+
Union → reserve ID #40, identity[flowUnion] = Type(40)
|
|
878
|
+
ExactObj → reserve ID #41, identity[flowExact] = Type(41)
|
|
879
|
+
|
|
880
|
+
Phase B (fill):
|
|
881
|
+
Intern non-looping sub-types:
|
|
882
|
+
Null → already interned (well-known kNullId)
|
|
883
|
+
"left" field type → lookup flowUnion in identity table → Type(40)
|
|
884
|
+
"right" field type → lookup flowUnion in identity table → Type(40)
|
|
885
|
+
|
|
886
|
+
Fill entries_[41] (ExactObject):
|
|
887
|
+
kind = ExactObject
|
|
888
|
+
fields = [{left, Type(40)}, {right, Type(40)}]
|
|
889
|
+
|
|
890
|
+
Fill entries_[40] (Union):
|
|
891
|
+
arms = [Type(kNullId), Type(41)]
|
|
892
|
+
sort by id_ → [Type(kNullId=4), Type(41)]
|
|
893
|
+
kind = Union
|
|
894
|
+
|
|
895
|
+
Phase C (intern):
|
|
896
|
+
Insert entries_[40] and entries_[41] into internTable_.
|
|
897
|
+
```
|
|
898
|
+
|
|
899
|
+
---
|
|
900
|
+
|
|
901
|
+
## 8. Union Canonicalization
|
|
902
|
+
|
|
903
|
+
Union arms must be in canonical order for interning to produce unique entries.
|
|
904
|
+
|
|
905
|
+
### Rules
|
|
906
|
+
|
|
907
|
+
1. **Flatten**: nested unions are expanded into a flat arm list.
|
|
908
|
+
2. **Deduplicate**: arms with the same `id_` are removed.
|
|
909
|
+
3. **Subsume** (acyclic path only): if arm A is a subtype of arm B, drop A.
|
|
910
|
+
In particular, if unrefined `Object` is an arm, all object refinements
|
|
911
|
+
are dropped. Subsumption requires inspecting the full type entries of
|
|
912
|
+
each arm, so it is only performed when all arms are fully finalized.
|
|
913
|
+
During looping-type batch construction (Phase B in §7), subsumption is
|
|
914
|
+
**skipped** — arms are deduplicated and sorted, but not
|
|
915
|
+
subsumption-reduced. This is safe because looping types come from Flow,
|
|
916
|
+
which has already canonicalized its unions.
|
|
917
|
+
4. **Sort**: arms are sorted by `id_`. This is a deterministic total order
|
|
918
|
+
(arbitrary but consistent within a single `TypeContext`).
|
|
919
|
+
5. **Collapse**: if one arm remains, return it directly (no Union entry).
|
|
920
|
+
|
|
921
|
+
### Sorting and Looping Types
|
|
922
|
+
|
|
923
|
+
Sorting by `id_` sidesteps the looping-type sorting problem: looping types
|
|
924
|
+
cannot be sorted by structural comparison (co-inductive comparison doesn't
|
|
925
|
+
give a total order), but they CAN be sorted by their interned index. The
|
|
926
|
+
interning algorithm assigns IDs before union canonicalization, so all arms
|
|
927
|
+
have stable IDs.
|
|
928
|
+
|
|
929
|
+
---
|
|
930
|
+
|
|
931
|
+
## 9. Thread-Local Context (Phase 1)
|
|
932
|
+
|
|
933
|
+
### Motivation
|
|
934
|
+
|
|
935
|
+
Phase 1 must be API-compatible: existing call sites like
|
|
936
|
+
`Type::unionTy(a, b)` and `t.canBeNumber()` must work without passing a
|
|
937
|
+
context parameter. The thread-local provides the context implicitly.
|
|
938
|
+
|
|
939
|
+
### Implementation
|
|
940
|
+
|
|
941
|
+
```cpp
|
|
942
|
+
class TypeContext {
|
|
943
|
+
static thread_local TypeContext *current_;
|
|
944
|
+
|
|
945
|
+
public:
|
|
946
|
+
static TypeContext ¤t() {
|
|
947
|
+
assert(current_ && "No TypeContext set on this thread");
|
|
948
|
+
return *current_;
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
/// RAII guard that sets the thread-local context for the duration of a scope.
|
|
953
|
+
class TypeContextRAII {
|
|
954
|
+
TypeContext *prev_;
|
|
955
|
+
|
|
956
|
+
public:
|
|
957
|
+
explicit TypeContextRAII(TypeContext &ctx)
|
|
958
|
+
: prev_(TypeContext::current_) {
|
|
959
|
+
TypeContext::current_ = &ctx;
|
|
960
|
+
}
|
|
961
|
+
~TypeContextRAII() {
|
|
962
|
+
TypeContext::current_ = prev_;
|
|
963
|
+
}
|
|
964
|
+
};
|
|
965
|
+
```
|
|
966
|
+
|
|
967
|
+
### Context Lifetime
|
|
968
|
+
|
|
969
|
+
The TLS context must be set whenever any `Type` operation is used — not just
|
|
970
|
+
in optimization passes. Instruction constructors call `unionTy` (e.g.,
|
|
971
|
+
`ToPropertyKeyInst` at `Instrs.h:225`), IR construction helpers call it
|
|
972
|
+
(e.g., `functionNewTargetType` at `IR.cpp:177`), and Flow type lowering
|
|
973
|
+
calls it (e.g., `flowTypeToIRType` at `ESTreeIRGen-typed-class.cpp:432`).
|
|
974
|
+
|
|
975
|
+
The guard must therefore be installed at Module creation time, so that
|
|
976
|
+
all subsequent operations on the Module (IR construction, passes, codegen)
|
|
977
|
+
are covered:
|
|
978
|
+
|
|
979
|
+
```cpp
|
|
980
|
+
// At Module creation:
|
|
981
|
+
TypeContextRAII guard(module->getTypeContext());
|
|
982
|
+
// All Type operations for the lifetime of the Module are now covered.
|
|
983
|
+
```
|
|
984
|
+
|
|
985
|
+
Unit tests that use `Type` algebra directly (e.g.,
|
|
986
|
+
`unittests/IR/BuilderTest.cpp:209`) must also install the guard.
|
|
987
|
+
|
|
988
|
+
**TLS overhead**: on some platforms (notably older Android), `thread_local`
|
|
989
|
+
access involves a function call through `__tls_get_addr`. Since every
|
|
990
|
+
`canBeX()` and type operation goes through `TypeContext::current()`, code
|
|
991
|
+
in tight loops should cache the context reference locally:
|
|
992
|
+
|
|
993
|
+
```cpp
|
|
994
|
+
TypeContext &ctx = TypeContext::current();
|
|
995
|
+
for (auto &inst : block) {
|
|
996
|
+
// Use ctx directly, not TypeContext::current() each time.
|
|
997
|
+
if (ctx.canBeNumber(inst.getType())) { ... }
|
|
998
|
+
}
|
|
999
|
+
```
|
|
1000
|
+
|
|
1001
|
+
### Phase 2: Explicit Parameter
|
|
1002
|
+
|
|
1003
|
+
All call sites are mechanically transformed to accept an explicit
|
|
1004
|
+
`TypeContext &`:
|
|
1005
|
+
|
|
1006
|
+
```cpp
|
|
1007
|
+
// Phase 1 (thread-local):
|
|
1008
|
+
Type result = Type::unionTy(a, b);
|
|
1009
|
+
|
|
1010
|
+
// Phase 2 (explicit):
|
|
1011
|
+
Type result = ctx.unionTy(a, b);
|
|
1012
|
+
```
|
|
1013
|
+
|
|
1014
|
+
Phase 2 is incremental: during the transition, both the explicit-parameter
|
|
1015
|
+
API and the thread-local API coexist. Call sites are converted one at a
|
|
1016
|
+
time. The thread-local is removed only at the END of Phase 2, after all
|
|
1017
|
+
call sites have been converted.
|
|
1018
|
+
|
|
1019
|
+
---
|
|
1020
|
+
|
|
1021
|
+
## 10. Integration Points
|
|
1022
|
+
|
|
1023
|
+
### Value::getType() / setType()
|
|
1024
|
+
|
|
1025
|
+
No API changes. `Value` stores a `Type` member (now 4 bytes instead of 2).
|
|
1026
|
+
The existing size assertion `sizeof(valueType) <= 4` already allows this.
|
|
1027
|
+
|
|
1028
|
+
### Inherent Types (Instrs.h)
|
|
1029
|
+
|
|
1030
|
+
Instructions with inherent types set them in constructors. Current pattern:
|
|
1031
|
+
|
|
1032
|
+
```cpp
|
|
1033
|
+
// ToPropertyKeyInst constructor:
|
|
1034
|
+
setType(Type::unionTy(Type::createString(), Type::createSymbol()));
|
|
1035
|
+
```
|
|
1036
|
+
|
|
1037
|
+
This continues to work. `unionTy` now delegates to the thread-local context,
|
|
1038
|
+
which looks up the pre-allocated entries for `String` and `Symbol` and creates
|
|
1039
|
+
(or finds) the `Union(String, Symbol)` entry.
|
|
1040
|
+
|
|
1041
|
+
**Constraint**: instruction constructors that call `unionTy` or similar must
|
|
1042
|
+
execute within an `TypeContextRAII` scope. This is ensured by installing
|
|
1043
|
+
the guard at Module creation time (see §9), which covers all subsequent
|
|
1044
|
+
instruction creation.
|
|
1045
|
+
|
|
1046
|
+
### TypeInference Pass
|
|
1047
|
+
|
|
1048
|
+
The TypeInference pass (~76 calls to `unionTy`, ~6 to `subtractTy`, ~30 to
|
|
1049
|
+
`isSubsetOf`) works unchanged via the thread-local context. The
|
|
1050
|
+
`TypeContextRAII` guard is installed at the top of `runOnModule`.
|
|
1051
|
+
|
|
1052
|
+
### LiteralIRType
|
|
1053
|
+
|
|
1054
|
+
`Module` owns a `ValueOFS<LiteralIRType>` folding set that deduplicates
|
|
1055
|
+
`Type` values used as literal operands. The `Profile` method now uses the
|
|
1056
|
+
single `id_` field:
|
|
1057
|
+
|
|
1058
|
+
```cpp
|
|
1059
|
+
void Profile(llvh::FoldingSetNodeID &ID) const {
|
|
1060
|
+
ID.AddInteger(id_);
|
|
1061
|
+
}
|
|
1062
|
+
```
|
|
1063
|
+
|
|
1064
|
+
### FlowChecker Lowering
|
|
1065
|
+
|
|
1066
|
+
The lowering from Flow types to IR types occurs in the Flow checker / IR
|
|
1067
|
+
generator:
|
|
1068
|
+
|
|
1069
|
+
1. The `Module`'s `TypeContext` is created.
|
|
1070
|
+
2. The interning algorithm (§7) processes all reachable Flow types.
|
|
1071
|
+
3. The resulting IR `Type` values are attached to instructions and values.
|
|
1072
|
+
|
|
1073
|
+
### Flow Type Lowering Notes
|
|
1074
|
+
|
|
1075
|
+
- **ClassConstructorType**: a class constructor is a callable with a known
|
|
1076
|
+
signature. Lowered to `Function(constructorSignature)`. Until now lowered
|
|
1077
|
+
to unrefined `Object` due to limitations of the old type system.
|
|
1078
|
+
- **NativeFunction**: native functions are called via specialized
|
|
1079
|
+
instructions, not through JS closures. They don't need an IR type
|
|
1080
|
+
representation. Currently hardcoded to `Type::createNumber()`
|
|
1081
|
+
(`ESTreeIRGen-typed-class.cpp:437`).
|
|
1082
|
+
- **CPtr**: Flow's internal C pointer type for native interop. Currently
|
|
1083
|
+
lowered to `Number`. May warrant its own internal type (like `bits32`)
|
|
1084
|
+
in a future phase.
|
|
1085
|
+
- **UntypedFunction**: functions with no type information. Lowered to
|
|
1086
|
+
unrefined `Object` in the IR.
|
|
1087
|
+
|
|
1088
|
+
---
|
|
1089
|
+
|
|
1090
|
+
## 11. Invariants and Assertions
|
|
1091
|
+
|
|
1092
|
+
### Type Invariants
|
|
1093
|
+
|
|
1094
|
+
1. **Uniqueness**: two types with the same `id_` are identical. Two types with
|
|
1095
|
+
different `id_` values are different. Guaranteed by hash-consing.
|
|
1096
|
+
|
|
1097
|
+
2. **Union canonicality**: Union arms are sorted by `id_`, deduplicated,
|
|
1098
|
+
subsumption-reduced, and contain at least 2 arms.
|
|
1099
|
+
|
|
1100
|
+
3. **No nested unions**: Union arms are never themselves unions (flattened).
|
|
1101
|
+
|
|
1102
|
+
4. **Well-known IDs**: IDs below `kFirstDynamicId` are reserved and have
|
|
1103
|
+
fixed meanings across all `TypeContext` instances.
|
|
1104
|
+
|
|
1105
|
+
5. **Context affinity**: a `Type` value is only meaningful within the
|
|
1106
|
+
`TypeContext` that created it. Mixing types from different contexts is
|
|
1107
|
+
undefined behavior. Since `Type` is a bare `uint32_t` with no room for a
|
|
1108
|
+
context pointer, this invariant cannot be enforced inside `Type` itself.
|
|
1109
|
+
It is enforced indirectly: the type table bounds-check
|
|
1110
|
+
(`id < entries_.size()`) will catch most violations, and in practice each
|
|
1111
|
+
compilation unit uses a single Module/context.
|
|
1112
|
+
|
|
1113
|
+
### Debug Assertions
|
|
1114
|
+
|
|
1115
|
+
```cpp
|
|
1116
|
+
// In type table access:
|
|
1117
|
+
assert(id < entries_.size() && "invalid type ID");
|
|
1118
|
+
|
|
1119
|
+
// In union construction:
|
|
1120
|
+
assert(armCount >= 2 && "union must have at least 2 arms");
|
|
1121
|
+
|
|
1122
|
+
// In class subtype check:
|
|
1123
|
+
assert(getKind(a) == TypeKind::ClassInstance
|
|
1124
|
+
&& "not a class type");
|
|
1125
|
+
```
|
|
1126
|
+
|
|
1127
|
+
---
|
|
1128
|
+
|
|
1129
|
+
## 12. Performance Considerations
|
|
1130
|
+
|
|
1131
|
+
### Cost Model
|
|
1132
|
+
|
|
1133
|
+
With the pure-index approach, every `canBeNumber()`, `isSubsetOf()`, etc.
|
|
1134
|
+
requires a table lookup instead of a bitmask test. The table is a
|
|
1135
|
+
`std::vector` — lookups are a pointer dereference + offset, typically in L1
|
|
1136
|
+
cache for types used frequently.
|
|
1137
|
+
|
|
1138
|
+
For union queries like `canBeNumber()` on `Union(Number, String)`, the
|
|
1139
|
+
implementation must check each arm. Unions are typically small (2–4 arms), so
|
|
1140
|
+
this is a short linear scan.
|
|
1141
|
+
|
|
1142
|
+
### Future Optimization: Bitmask Cache
|
|
1143
|
+
|
|
1144
|
+
If profiling reveals that table lookups are a bottleneck, a bitmask cache can
|
|
1145
|
+
be added without changing the type algebra:
|
|
1146
|
+
|
|
1147
|
+
- Each `TypeEntry` stores a precomputed `uint16_t kindMask` — the set of
|
|
1148
|
+
primitive kinds reachable from this type.
|
|
1149
|
+
- `canBeNumber(t)` becomes `entries_[t.id_].kindMask & NumberBit`.
|
|
1150
|
+
- This is an implementation optimization layered on top of the clean design.
|
|
1151
|
+
It does not affect the public API or the type algebra.
|
|
1152
|
+
|
|
1153
|
+
This optimization is explicitly deferred. The clean design comes first;
|
|
1154
|
+
performance tuning comes after profiling on real workloads.
|
|
1155
|
+
|
|
1156
|
+
---
|
|
1157
|
+
|
|
1158
|
+
## 13. Summary of Changes by Phase
|
|
1159
|
+
|
|
1160
|
+
### Phase 1: Type representation and context (API-compatible)
|
|
1161
|
+
|
|
1162
|
+
**Files modified:**
|
|
1163
|
+
- `include/hermes/IR/IR.h`: `Type` class rewritten (pure index).
|
|
1164
|
+
- `include/hermes/IR/IR.h`: `Module` class (add `TypeContext` member).
|
|
1165
|
+
- New: `include/hermes/IR/TypeContext.h`.
|
|
1166
|
+
- New: `lib/IR/TypeContext.cpp`.
|
|
1167
|
+
- `Module` constructor: install `TypeContextRAII` guard.
|
|
1168
|
+
- Unit tests using `Type` algebra: install `TypeContextRAII` guard.
|
|
1169
|
+
|
|
1170
|
+
**Semantic changes:** None. All types are well-known primitives. The context
|
|
1171
|
+
produces identical results to the old bitmask operations. The change is
|
|
1172
|
+
that operations now go through the type table instead of being direct
|
|
1173
|
+
bitmask arithmetic, but the results are identical.
|
|
1174
|
+
|
|
1175
|
+
### Phase 2: Explicit context parameter (mechanical)
|
|
1176
|
+
|
|
1177
|
+
**Files modified:** Every file that calls `unionTy`, `intersectTy`,
|
|
1178
|
+
`subtractTy`, `isSubsetOf`, `canBeX()`, etc. (~128+ call sites across
|
|
1179
|
+
~23 files).
|
|
1180
|
+
|
|
1181
|
+
**Semantic changes:** None. Thread-local removed, explicit parameter added.
|
|
1182
|
+
|
|
1183
|
+
### Phases 3–8: Incremental type enrichment
|
|
1184
|
+
|
|
1185
|
+
Each phase adds new `TypeKind` values, extends the type operations, and
|
|
1186
|
+
adds new query/construction methods to `TypeContext`. Existing methods
|
|
1187
|
+
continue to work.
|