xbintsc 0.7.0 → 0.7.6

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 (323) hide show
  1. package/dist/src/ast/common.d.ts +10 -0
  2. package/dist/src/checker/source.js +4 -0
  3. package/dist/src/checker/source.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/cli/main.js +15 -3
  6. package/dist/src/cli/main.js.map +1 -1
  7. package/dist/src/codegen/generator/calls/access.js +4 -11
  8. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  9. package/dist/src/codegen/generator/calls/element-inline.d.ts +42 -0
  10. package/dist/src/codegen/generator/calls/element-inline.js +388 -0
  11. package/dist/src/codegen/generator/calls/element-inline.js.map +1 -0
  12. package/dist/src/codegen/generator/calls/invocation.js +82 -1
  13. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  14. package/dist/src/codegen/generator/calls.d.ts +3 -1
  15. package/dist/src/codegen/generator/calls.js +3 -1
  16. package/dist/src/codegen/generator/calls.js.map +1 -1
  17. package/dist/src/codegen/generator/debug.js +20 -3
  18. package/dist/src/codegen/generator/debug.js.map +1 -1
  19. package/dist/src/codegen/generator/expressions/assignment.js +10 -2
  20. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  21. package/dist/src/codegen/generator/expressions/global-values.d.ts +19 -0
  22. package/dist/src/codegen/generator/expressions/global-values.js +49 -0
  23. package/dist/src/codegen/generator/expressions/global-values.js.map +1 -0
  24. package/dist/src/codegen/generator/expressions/primary.d.ts +0 -2
  25. package/dist/src/codegen/generator/expressions/primary.js +4 -24
  26. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  27. package/dist/src/codegen/generator/expressions.d.ts +2 -1
  28. package/dist/src/codegen/generator/expressions.js +2 -1
  29. package/dist/src/codegen/generator/expressions.js.map +1 -1
  30. package/dist/src/codegen/generator/layout.d.ts +65 -0
  31. package/dist/src/codegen/generator/layout.js +50 -0
  32. package/dist/src/codegen/generator/layout.js.map +1 -0
  33. package/dist/src/codegen/generator/state.js +4 -0
  34. package/dist/src/codegen/generator/state.js.map +1 -1
  35. package/dist/src/diagnostics/diagnostic.js +1 -1
  36. package/dist/src/diagnostics/diagnostic.js.map +1 -1
  37. package/dist/src/diagnostics/source-text.d.ts +37 -9
  38. package/dist/src/diagnostics/source-text.js +124 -23
  39. package/dist/src/diagnostics/source-text.js.map +1 -1
  40. package/dist/src/diagnostics/source.d.ts +16 -6
  41. package/dist/src/diagnostics/source.js +25 -4
  42. package/dist/src/diagnostics/source.js.map +1 -1
  43. package/dist/src/driver/build-key.d.ts +14 -0
  44. package/dist/src/driver/build-key.js +6 -1
  45. package/dist/src/driver/build-key.js.map +1 -1
  46. package/dist/src/driver/bundler/concatenate.js +11 -4
  47. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  48. package/dist/src/driver/bundler/graph.js +2 -2
  49. package/dist/src/driver/bundler/graph.js.map +1 -1
  50. package/dist/src/driver/bundler/merge.js +11 -5
  51. package/dist/src/driver/bundler/merge.js.map +1 -1
  52. package/dist/src/driver/bundler/reexport-resolution.js +3 -0
  53. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  54. package/dist/src/driver/compiler.js +13 -3
  55. package/dist/src/driver/compiler.js.map +1 -1
  56. package/dist/src/driver/frontend.js +3 -4
  57. package/dist/src/driver/frontend.js.map +1 -1
  58. package/dist/src/driver/runtime-objects.d.ts +1 -1
  59. package/dist/src/driver/runtime-objects.js +4 -0
  60. package/dist/src/driver/runtime-objects.js.map +1 -1
  61. package/dist/src/driver/toolchain.js +28 -5
  62. package/dist/src/driver/toolchain.js.map +1 -1
  63. package/dist/src/driver/version.d.ts +1 -1
  64. package/dist/src/driver/version.js +1 -1
  65. package/dist/src/extensions/gui/html.js +42 -10
  66. package/dist/src/extensions/gui/html.js.map +1 -1
  67. package/dist/src/extensions/gui/index.js +48 -0
  68. package/dist/src/extensions/gui/index.js.map +1 -1
  69. package/dist/src/extensions/registry.d.ts +20 -0
  70. package/dist/src/extensions/registry.js.map +1 -1
  71. package/dist/src/parser/expressions.js +2 -2
  72. package/dist/src/parser/expressions.js.map +1 -1
  73. package/dist/src/parser/helpers.d.ts +8 -0
  74. package/dist/src/parser/helpers.js +21 -0
  75. package/dist/src/parser/helpers.js.map +1 -1
  76. package/dist/tests/cli/main.test.js +9 -0
  77. package/dist/tests/cli/main.test.js.map +1 -1
  78. package/dist/tests/codegen/element-layout.test.d.ts +19 -0
  79. package/dist/tests/codegen/element-layout.test.js +80 -0
  80. package/dist/tests/codegen/element-layout.test.js.map +1 -0
  81. package/dist/tests/codegen/expressions.test.js +25 -0
  82. package/dist/tests/codegen/expressions.test.js.map +1 -1
  83. package/dist/tests/codegen/llvm.test.js +13 -0
  84. package/dist/tests/codegen/llvm.test.js.map +1 -1
  85. package/dist/tests/diagnostics/diagnostic.test.js +9 -0
  86. package/dist/tests/diagnostics/diagnostic.test.js.map +1 -1
  87. package/dist/tests/diagnostics/source-text.test.d.ts +1 -0
  88. package/dist/tests/diagnostics/source-text.test.js +50 -0
  89. package/dist/tests/diagnostics/source-text.test.js.map +1 -0
  90. package/dist/tests/diagnostics/source.test.js +17 -0
  91. package/dist/tests/diagnostics/source.test.js.map +1 -1
  92. package/dist/tests/driver/build-key.test.d.ts +1 -0
  93. package/dist/tests/driver/build-key.test.js +49 -0
  94. package/dist/tests/driver/build-key.test.js.map +1 -0
  95. package/dist/tests/driver/toolchain.test.js +8 -1
  96. package/dist/tests/driver/toolchain.test.js.map +1 -1
  97. package/dist/tests/driver/usage.test.js +2 -1
  98. package/dist/tests/driver/usage.test.js.map +1 -1
  99. package/dist/tests/e2e/async-await.test.js +16 -0
  100. package/dist/tests/e2e/async-await.test.js.map +1 -1
  101. package/dist/tests/e2e/compile-builtins.test.js +29 -0
  102. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  103. package/dist/tests/e2e/compile-modules.test.js +13 -0
  104. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  105. package/dist/tests/e2e/compile-objects-classes.test.js.map +1 -1
  106. package/dist/tests/e2e/compile-prototype-chain.test.d.ts +8 -0
  107. package/dist/tests/e2e/compile-prototype-chain.test.js +24 -0
  108. package/dist/tests/e2e/compile-prototype-chain.test.js.map +1 -0
  109. package/dist/tests/e2e/differential-language.test.js +25 -0
  110. package/dist/tests/e2e/differential-language.test.js.map +1 -1
  111. package/dist/tests/e2e/differential-values.test.js +59 -0
  112. package/dist/tests/e2e/differential-values.test.js.map +1 -1
  113. package/dist/tests/e2e/gc.test.js +22 -0
  114. package/dist/tests/e2e/gc.test.js.map +1 -1
  115. package/dist/tests/e2e/gui-audio-extra.test.d.ts +12 -0
  116. package/dist/tests/e2e/gui-audio-extra.test.js +185 -0
  117. package/dist/tests/e2e/gui-audio-extra.test.js.map +1 -0
  118. package/dist/tests/e2e/gui-browser.test.js +35 -2
  119. package/dist/tests/e2e/gui-browser.test.js.map +1 -1
  120. package/dist/tests/e2e/gui-dom.test.js +88 -0
  121. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  122. package/dist/tests/e2e/gui-webgl-declarations.test.d.ts +13 -0
  123. package/dist/tests/e2e/gui-webgl-declarations.test.js +136 -0
  124. package/dist/tests/e2e/gui-webgl-declarations.test.js.map +1 -0
  125. package/dist/tests/e2e/gui-webgl-texture-units.test.d.ts +12 -0
  126. package/dist/tests/e2e/gui-webgl-texture-units.test.js +86 -0
  127. package/dist/tests/e2e/gui-webgl-texture-units.test.js.map +1 -0
  128. package/dist/tests/e2e/gui-webgl2-blit.test.js +6 -2
  129. package/dist/tests/e2e/gui-webgl2-blit.test.js.map +1 -1
  130. package/dist/tests/e2e/gui-webgl2-msaa-float.test.d.ts +12 -0
  131. package/dist/tests/e2e/gui-webgl2-msaa-float.test.js +157 -0
  132. package/dist/tests/e2e/gui-webgl2-msaa-float.test.js.map +1 -0
  133. package/dist/tests/e2e/gui-webgl2-msaa.test.d.ts +10 -0
  134. package/dist/tests/e2e/gui-webgl2-msaa.test.js +344 -0
  135. package/dist/tests/e2e/gui-webgl2-msaa.test.js.map +1 -0
  136. package/dist/tests/e2e/gui-webgl2-target-orientation.test.d.ts +15 -0
  137. package/dist/tests/e2e/gui-webgl2-target-orientation.test.js +287 -0
  138. package/dist/tests/e2e/gui-webgl2-target-orientation.test.js.map +1 -0
  139. package/dist/tests/e2e/gui-webgl2.test.js +59 -1
  140. package/dist/tests/e2e/gui-webgl2.test.js.map +1 -1
  141. package/dist/tests/e2e/hang-watchdog.test.d.ts +10 -0
  142. package/dist/tests/e2e/hang-watchdog.test.js +50 -0
  143. package/dist/tests/e2e/hang-watchdog.test.js.map +1 -0
  144. package/dist/tests/e2e/method-call-cache.test.d.ts +5 -0
  145. package/dist/tests/e2e/method-call-cache.test.js +65 -0
  146. package/dist/tests/e2e/method-call-cache.test.js.map +1 -0
  147. package/dist/tests/e2e/performance-now.test.d.ts +9 -0
  148. package/dist/tests/e2e/performance-now.test.js +36 -0
  149. package/dist/tests/e2e/performance-now.test.js.map +1 -0
  150. package/dist/tests/e2e/property-ic.test.js +33 -0
  151. package/dist/tests/e2e/property-ic.test.js.map +1 -1
  152. package/dist/tests/e2e/shape-interning.test.d.ts +16 -0
  153. package/dist/tests/e2e/shape-interning.test.js +55 -0
  154. package/dist/tests/e2e/shape-interning.test.js.map +1 -0
  155. package/dist/tests/e2e/string-utf16.test.js +9 -0
  156. package/dist/tests/e2e/string-utf16.test.js.map +1 -1
  157. package/dist/tests/e2e/typed-array-views.test.js +26 -0
  158. package/dist/tests/e2e/typed-array-views.test.js.map +1 -1
  159. package/dist/tests/e2e/url-search-params.test.js +19 -0
  160. package/dist/tests/e2e/url-search-params.test.js.map +1 -1
  161. package/dist/tests/extensions/gui.test.js +16 -1
  162. package/dist/tests/extensions/gui.test.js.map +1 -1
  163. package/dist/tests/helpers.js +1 -0
  164. package/dist/tests/helpers.js.map +1 -1
  165. package/dist/tests/parser/expressions.test.js +36 -0
  166. package/dist/tests/parser/expressions.test.js.map +1 -1
  167. package/doc/ai/contributing.md +8 -5
  168. package/doc/ai/extensions.md +12 -3
  169. package/doc/ai/language-support.md +2 -1
  170. package/doc/ai/troubleshooting.md +14 -0
  171. package/doc/ai/zh-CN/contributing.md +6 -3
  172. package/doc/ai/zh-CN/extensions.md +10 -2
  173. package/doc/ai/zh-CN/language-support.md +2 -1
  174. package/doc/ai/zh-CN/troubleshooting.md +12 -0
  175. package/doc/gui/api.md +26 -4
  176. package/doc/gui/dom.md +8 -0
  177. package/doc/gui/known-issues.md +35 -6
  178. package/doc/gui/runtime.md +16 -1
  179. package/doc/gui/webgl.md +97 -26
  180. package/doc/gui-scripts.md +2 -1
  181. package/doc/gui.md +8 -5
  182. package/doc/implemented/checker.md +9 -0
  183. package/doc/implemented/codegen.md +7 -0
  184. package/doc/implemented/parser.md +2 -1
  185. package/doc/implemented/runtime-core.md +9 -2
  186. package/doc/implemented/stdlib.md +10 -2
  187. package/doc/implemented/typed-arrays.md +18 -2
  188. package/doc/zh-CN/gui/api.md +19 -5
  189. package/doc/zh-CN/gui/dom.md +6 -0
  190. package/doc/zh-CN/gui/known-issues.md +24 -5
  191. package/doc/zh-CN/gui/runtime.md +13 -1
  192. package/doc/zh-CN/gui/webgl.md +20 -7
  193. package/doc/zh-CN/gui-scripts.md +2 -1
  194. package/doc/zh-CN/gui.md +4 -1
  195. package/doc/zh-CN/implemented/checker.md +7 -0
  196. package/doc/zh-CN/implemented/codegen.md +7 -0
  197. package/doc/zh-CN/implemented/parser.md +2 -1
  198. package/doc/zh-CN/implemented/runtime-core.md +9 -2
  199. package/doc/zh-CN/implemented/stdlib.md +6 -2
  200. package/doc/zh-CN/implemented/typed-arrays.md +14 -2
  201. package/package.json +7 -7
  202. package/runtime/ext_gui/audio.cpp +39 -3
  203. package/runtime/ext_gui/audio_extra.cpp +353 -0
  204. package/runtime/ext_gui/audio_extra.h +47 -0
  205. package/runtime/ext_gui/audio_fft.h +76 -0
  206. package/runtime/ext_gui/audio_internal.h +21 -0
  207. package/runtime/ext_gui/audio_mixer.cpp +46 -1
  208. package/runtime/ext_gui/audio_mixer.h +89 -1
  209. package/runtime/ext_gui/audio_mixer_extra.cpp +126 -0
  210. package/runtime/ext_gui/audio_nodes.cpp +242 -0
  211. package/runtime/ext_gui/audio_nodes_internal.h +81 -0
  212. package/runtime/ext_gui/audio_nodes_spatial.cpp +375 -0
  213. package/runtime/ext_gui/browser.cpp +9 -0
  214. package/runtime/ext_gui/browser_screen.cpp +73 -0
  215. package/runtime/ext_gui/dom_api_canvas.cpp +9 -1
  216. package/runtime/ext_gui/dom_api_internal.h +3 -0
  217. package/runtime/ext_gui/dom_api_node.cpp +21 -0
  218. package/runtime/ext_gui/dom_api_node_read.cpp +29 -0
  219. package/runtime/ext_gui/dom_api_proto.cpp +3 -0
  220. package/runtime/ext_gui/dom_api_webgl2.cpp +16 -11
  221. package/runtime/ext_gui/dom_api_webgl2_msaa.cpp +64 -0
  222. package/runtime/ext_gui/dom_api_webgl_compressed.cpp +2 -2
  223. package/runtime/ext_gui/dom_api_webgl_framebuffer.cpp +68 -11
  224. package/runtime/ext_gui/dom_api_webgl_methods.h +3 -0
  225. package/runtime/ext_gui/dom_api_webgl_state.cpp +4 -2
  226. package/runtime/ext_gui/dom_api_webgl_texture.cpp +6 -6
  227. package/runtime/ext_gui/gui.cpp +6 -1
  228. package/runtime/ext_gui/gui_engine.h +2 -0
  229. package/runtime/ext_gui/gui_events.cpp +13 -8
  230. package/runtime/ext_gui/gui_webgl.cpp +3 -2
  231. package/runtime/ext_gui/style_ua.cpp +4 -0
  232. package/runtime/ext_gui/warnings.cpp +1 -1
  233. package/runtime/ext_gui/webgl.h +102 -7
  234. package/runtime/ext_gui/webgl_api.h +3 -2
  235. package/runtime/ext_gui/webgl_blit.cpp +109 -15
  236. package/runtime/ext_gui/webgl_blit_color.cpp +264 -0
  237. package/runtime/ext_gui/webgl_blit_depth.cpp +26 -8
  238. package/runtime/ext_gui/webgl_buffer.cpp +20 -170
  239. package/runtime/ext_gui/webgl_context.cpp +117 -169
  240. package/runtime/ext_gui/webgl_draw.cpp +116 -5
  241. package/runtime/ext_gui/webgl_draw_buffers.cpp +21 -6
  242. package/runtime/ext_gui/webgl_draw_setup.cpp +1 -0
  243. package/runtime/ext_gui/webgl_draw_state.h +2 -0
  244. package/runtime/ext_gui/webgl_feature.cpp +2 -2
  245. package/runtime/ext_gui/webgl_feature.h +4 -3
  246. package/runtime/ext_gui/webgl_framebuffer.cpp +346 -0
  247. package/runtime/ext_gui/webgl_glsl_rewrite.cpp +78 -47
  248. package/runtime/ext_gui/webgl_objects.h +16 -2
  249. package/runtime/ext_gui/webgl_pipeline.cpp +1 -1
  250. package/runtime/ext_gui/webgl_state.cpp +45 -36
  251. package/runtime/ext_gui/webgl_targets.cpp +440 -0
  252. package/runtime/ext_gui/webgl_texture.cpp +81 -25
  253. package/runtime/ext_gui/window.cpp +8 -0
  254. package/runtime/rt.h +27 -27
  255. package/runtime/rt_binary.h +44 -0
  256. package/runtime/rt_internal.h +52 -60
  257. package/runtime/rt_object.h +83 -0
  258. package/runtime/rt_profile.h +45 -0
  259. package/runtime/xt_alloc.c +88 -133
  260. package/runtime/xt_array_buffer.c +99 -71
  261. package/runtime/xt_builtins.c +3 -1
  262. package/runtime/xt_constructors.c +1 -0
  263. package/runtime/xt_containers/access.inc +96 -11
  264. package/runtime/xt_containers/arrays.inc +2 -1
  265. package/runtime/xt_containers/cstr_cache.inc +56 -0
  266. package/runtime/xt_containers/method_cache.inc +121 -0
  267. package/runtime/xt_containers/object_keys.inc +58 -0
  268. package/runtime/xt_containers/objects.inc +47 -79
  269. package/runtime/xt_containers/shapes.inc +153 -8
  270. package/runtime/xt_containers.c +4 -0
  271. package/runtime/xt_gc_release.c +70 -0
  272. package/runtime/xt_gc_stack.c +83 -0
  273. package/runtime/xt_gc_verify.c +1 -0
  274. package/runtime/xt_io/console.inc +66 -0
  275. package/runtime/xt_io/exception.inc +32 -6
  276. package/runtime/xt_io/watchdog.inc +101 -0
  277. package/runtime/xt_io.c +1 -0
  278. package/runtime/xt_loop.c +23 -3
  279. package/runtime/xt_pool.c +151 -0
  280. package/runtime/xt_profile.c +126 -0
  281. package/runtime/xt_stdlib/dispatch.inc +7 -1
  282. package/runtime/xt_stdlib/math.inc +272 -57
  283. package/runtime/xt_stdlib2/json_stringify.inc +1 -2
  284. package/runtime/xt_stdlib2/regexp-match.inc +17 -0
  285. package/runtime/xt_storage/indexed_db.c +4 -1
  286. package/runtime/xt_structured_clone.c +2 -1
  287. package/runtime/xt_typed_array/construction.inc +10 -0
  288. package/runtime/xt_typed_array/elements.inc +43 -23
  289. package/runtime/xt_url.c +50 -0
  290. package/runtime/xt_values/constructors.inc +4 -1
  291. package/runtime/xt_values/strings.inc +3 -1
  292. package/src/ast/common.ts +10 -0
  293. package/src/checker/source.ts +3 -0
  294. package/src/cli/main.ts +14 -3
  295. package/src/codegen/generator/calls/access.ts +5 -10
  296. package/src/codegen/generator/calls/element-inline.ts +433 -0
  297. package/src/codegen/generator/calls/invocation.ts +87 -1
  298. package/src/codegen/generator/calls.ts +5 -0
  299. package/src/codegen/generator/debug.ts +21 -3
  300. package/src/codegen/generator/expressions/assignment.ts +11 -2
  301. package/src/codegen/generator/expressions/global-values.ts +57 -0
  302. package/src/codegen/generator/expressions/primary.ts +4 -27
  303. package/src/codegen/generator/expressions.ts +3 -0
  304. package/src/codegen/generator/layout.ts +54 -0
  305. package/src/codegen/generator/state.ts +5 -0
  306. package/src/diagnostics/diagnostic.ts +1 -1
  307. package/src/diagnostics/source-text.ts +118 -22
  308. package/src/diagnostics/source.ts +29 -7
  309. package/src/driver/build-key.ts +21 -1
  310. package/src/driver/bundler/concatenate.ts +12 -5
  311. package/src/driver/bundler/graph.ts +2 -2
  312. package/src/driver/bundler/merge.ts +11 -4
  313. package/src/driver/bundler/reexport-resolution.ts +2 -0
  314. package/src/driver/compiler.ts +14 -3
  315. package/src/driver/frontend.ts +3 -4
  316. package/src/driver/runtime-objects.ts +4 -0
  317. package/src/driver/toolchain.ts +28 -5
  318. package/src/driver/version.ts +1 -1
  319. package/src/extensions/gui/html.ts +41 -11
  320. package/src/extensions/gui/index.ts +50 -1
  321. package/src/extensions/registry.ts +20 -0
  322. package/src/parser/expressions.ts +2 -2
  323. package/src/parser/helpers.ts +22 -0
@@ -0,0 +1,433 @@
1
+ /**
2
+ * Inline element access (`obj[key]`) in generated IR.
3
+ *
4
+ * `xt_get_element` / `xt_set_element` are runtime calls, and without LTO clang
5
+ * cannot inline them into generated code, so every element access paid a call
6
+ * plus the whole generic dispatch (tag checks, index canonicalisation, a
7
+ * property-bag fallback). Geometry code performs millions of these per level,
8
+ * which made that call overhead the dominant cost of element access.
9
+ *
10
+ * This module emits the hot path *in the IR*:
11
+ *
12
+ * 1. guard the key to a canonical array index (`0 .. 2^32-2`, integral) —
13
+ * `ta[1.5]` and `ta[-1]` are ordinary property accesses and must not be
14
+ * truncated into element 1 / 0;
15
+ * 2. for a plain array (`XT_TAG_ARRAY`), bounds-check against the live
16
+ * `length` and load/store the NaN-boxed element straight from `items`;
17
+ * 3. for a typed-array view (`XT_TAG_OBJECT` with a resolved `view_bytes`),
18
+ * test the memoized element kind and load the element with that kind's own
19
+ * load + conversion. Writes inline only for the float kinds: integer
20
+ * stores need the runtime's `ToUintN`/`ToIntN` wrapping (NaN → 0, modulo),
21
+ * which is not a plain truncation, so those keep the runtime call.
22
+ *
23
+ * Everything else (strings, `Map`s, host views, out-of-range indices, a view
24
+ * whose kind is not memoized yet, a non-numeric stored value) branches to the
25
+ * ordinary `xt_get_element` / `xt_set_element`, so semantics are exactly the
26
+ * runtime's. Two properties make the direct field reads safe:
27
+ *
28
+ * - the object pointer is reloaded from the value on every access, so an
29
+ * array whose `items` was reallocated by `push` is seen through its current
30
+ * header (no pointer is cached across mutations);
31
+ * - nothing allocates between the field load and the element load, so the
32
+ * collector cannot run in between; it never moves objects, and the receiver
33
+ * is a live value the conservative stack scan already roots.
34
+ */
35
+
36
+ import type { Generator } from "../generator.js";
37
+ import { XT_INLINE_TAGS, XT_LAYOUT } from "../layout.js";
38
+ import { i64 } from "../../values.js";
39
+
40
+ /** Kinds inlined on the read path, in test order (most common first). */
41
+ const INLINE_READ_KINDS = [
42
+ { kind: 7, width: 4 }, // Float32Array
43
+ { kind: 8, width: 8 }, // Float64Array
44
+ { kind: 0, width: 1 }, // Uint8Array
45
+ { kind: 3, width: 2 }, // Uint16Array
46
+ { kind: 5, width: 4 }, // Uint32Array
47
+ ] as const;
48
+
49
+ export interface ElementInlineMethods {
50
+ /** Read `object[key]` with the inline fast path. */
51
+ emitElementRead(this: Generator, objectValue: string, keyValue: string): string;
52
+ /** Write `object[key] = value` with the inline fast path. */
53
+ emitElementWrite(this: Generator, objectValue: string, keyValue: string, value: string): void;
54
+ }
55
+
56
+ /* -- small IR helpers ------------------------------------------------------ */
57
+
58
+ function reg(self: Generator): string {
59
+ return self.reg();
60
+ }
61
+
62
+ function emit(self: Generator, line: string): void {
63
+ self.emit(line);
64
+ }
65
+
66
+ /** `value` is a number (not undefined/null/string/object/...). */
67
+ function emitIsNumber(self: Generator, value: string): string {
68
+ const masked = reg(self);
69
+ emit(self, ` ${masked} = and i64 ${value}, ${i64(XT_INLINE_TAGS.numberMask)}`);
70
+ const isNumber = reg(self);
71
+ emit(self, ` ${isNumber} = icmp ne i64 ${masked}, ${i64(XT_INLINE_TAGS.numberMask)}`);
72
+ return isNumber;
73
+ }
74
+
75
+ /** `value` carries `tag` in its high 16 bits. */
76
+ function emitHasTag(self: Generator, value: string, tag: bigint): string {
77
+ const masked = reg(self);
78
+ emit(self, ` ${masked} = and i64 ${value}, ${i64(XT_INLINE_TAGS.tagMask)}`);
79
+ const matches = reg(self);
80
+ emit(self, ` ${matches} = icmp eq i64 ${masked}, ${i64(tag)}`);
81
+ return matches;
82
+ }
83
+
84
+ /** `value` is an integral array index in `0 .. 2^32-2`; safe to `fptosi`. */
85
+ function emitIsIndex(self: Generator, value: string): string {
86
+ const asDouble = reg(self);
87
+ emit(self, ` ${asDouble} = bitcast i64 ${value} to double`);
88
+ const truncated = reg(self);
89
+ emit(self, ` ${truncated} = call double @llvm.trunc.f64(double ${asDouble})`);
90
+ const integral = reg(self);
91
+ emit(self, ` ${integral} = fcmp oeq double ${truncated}, ${asDouble}`);
92
+ const nonNegative = reg(self);
93
+ emit(self, ` ${nonNegative} = fcmp oge double ${asDouble}, 0.000000e+00`);
94
+ const bounded = reg(self);
95
+ emit(self, ` ${bounded} = fcmp olt double ${asDouble}, 4.294967294000000e+09`);
96
+ const partial = reg(self);
97
+ emit(self, ` ${partial} = and i1 ${integral}, ${nonNegative}`);
98
+ const isIndex = reg(self);
99
+ emit(self, ` ${isIndex} = and i1 ${partial}, ${bounded}`);
100
+ return isIndex;
101
+ }
102
+
103
+ /** Truncate a key already proven to be an index. */
104
+ function emitIndexOf(self: Generator, value: string): string {
105
+ const asDouble = reg(self);
106
+ emit(self, ` ${asDouble} = bitcast i64 ${value} to double`);
107
+ const index = reg(self);
108
+ emit(self, ` ${index} = fptosi double ${asDouble} to i64`);
109
+ return index;
110
+ }
111
+
112
+ /** An `i8*` onto the heap payload of a tagged value. */
113
+ function emitPayload(self: Generator, value: string): string {
114
+ const pointer = reg(self);
115
+ emit(self, ` ${pointer} = and i64 ${value}, ${i64(0x0000ffffffffffffn)}`);
116
+ const base = reg(self);
117
+ emit(self, ` ${base} = inttoptr i64 ${pointer} to i8*`);
118
+ return base;
119
+ }
120
+
121
+ function addressAt(self: Generator, base: string, offset: number): string {
122
+ const address = reg(self);
123
+ emit(self, ` ${address} = getelementptr i8, i8* ${base}, i64 ${offset}`);
124
+ return address;
125
+ }
126
+
127
+ function loadU32(self: Generator, base: string, offset: number): string {
128
+ const pointer = reg(self);
129
+ emit(self, ` ${pointer} = bitcast i8* ${addressAt(self, base, offset)} to i32*`);
130
+ const value = reg(self);
131
+ emit(self, ` ${value} = load i32, i32* ${pointer}`);
132
+ return value;
133
+ }
134
+
135
+ function loadI64(self: Generator, base: string, offset: number): string {
136
+ const pointer = reg(self);
137
+ emit(self, ` ${pointer} = bitcast i8* ${addressAt(self, base, offset)} to i64*`);
138
+ const value = reg(self);
139
+ emit(self, ` ${value} = load i64, i64* ${pointer}`);
140
+ return value;
141
+ }
142
+
143
+ function loadI16(self: Generator, base: string, offset: number): string {
144
+ const pointer = reg(self);
145
+ emit(self, ` ${pointer} = bitcast i8* ${addressAt(self, base, offset)} to i16*`);
146
+ const value = reg(self);
147
+ emit(self, ` ${value} = load i16, i16* ${pointer}`);
148
+ return value;
149
+ }
150
+
151
+ /** `items[index]` as an `i64*` slot of the array whose header starts at `base`. */
152
+ function arraySlot(self: Generator, base: string, index: string): string {
153
+ const raw = loadI64(self, base, XT_LAYOUT.arrayItems);
154
+ const items = reg(self);
155
+ emit(self, ` ${items} = inttoptr i64 ${raw} to i64*`);
156
+ const slot = reg(self);
157
+ emit(self, ` ${slot} = getelementptr i64, i64* ${items}, i64 ${index}`);
158
+ return slot;
159
+ }
160
+
161
+ /** `view_bytes + index * width`, plus the exclusive end of that element. */
162
+ function viewOffset(
163
+ self: Generator,
164
+ view: string,
165
+ index: string,
166
+ width: number,
167
+ ): { address: string; end: string } {
168
+ const base = reg(self);
169
+ emit(self, ` ${base} = inttoptr i64 ${view} to i8*`);
170
+ const offset = reg(self);
171
+ emit(self, ` ${offset} = mul i64 ${index}, ${width}`);
172
+ const address = reg(self);
173
+ emit(self, ` ${address} = getelementptr i8, i8* ${base}, i64 ${offset}`);
174
+ const end = reg(self);
175
+ emit(self, ` ${end} = add i64 ${offset}, ${width}`);
176
+ return { address, end };
177
+ }
178
+
179
+ /** Load one typed-array element and box it as a NaN-boxed double. */
180
+ function loadTypedElement(self: Generator, address: string, kind: number): string {
181
+ const converted = reg(self);
182
+ switch (kind) {
183
+ case 7: {
184
+ const pointer = reg(self);
185
+ emit(self, ` ${pointer} = bitcast i8* ${address} to float*`);
186
+ const raw = reg(self);
187
+ emit(self, ` ${raw} = load float, float* ${pointer}`);
188
+ emit(self, ` ${converted} = fpext float ${raw} to double`);
189
+ break;
190
+ }
191
+ case 8: {
192
+ const pointer = reg(self);
193
+ emit(self, ` ${pointer} = bitcast i8* ${address} to double*`);
194
+ emit(self, ` ${converted} = load double, double* ${pointer}`);
195
+ break;
196
+ }
197
+ case 0: {
198
+ const raw = reg(self);
199
+ emit(self, ` ${raw} = load i8, i8* ${address}`);
200
+ const wide = reg(self);
201
+ emit(self, ` ${wide} = zext i8 ${raw} to i32`);
202
+ emit(self, ` ${converted} = uitofp i32 ${wide} to double`);
203
+ break;
204
+ }
205
+ case 3: {
206
+ const pointer = reg(self);
207
+ emit(self, ` ${pointer} = bitcast i8* ${address} to i16*`);
208
+ const raw = reg(self);
209
+ emit(self, ` ${raw} = load i16, i16* ${pointer}`);
210
+ const wide = reg(self);
211
+ emit(self, ` ${wide} = zext i16 ${raw} to i32`);
212
+ emit(self, ` ${converted} = uitofp i32 ${wide} to double`);
213
+ break;
214
+ }
215
+ default: {
216
+ const pointer = reg(self);
217
+ emit(self, ` ${pointer} = bitcast i8* ${address} to i32*`);
218
+ const raw = reg(self);
219
+ emit(self, ` ${raw} = load i32, i32* ${pointer}`);
220
+ emit(self, ` ${converted} = uitofp i32 ${raw} to double`);
221
+ break;
222
+ }
223
+ }
224
+ const boxed = reg(self);
225
+ emit(self, ` ${boxed} = bitcast double ${converted} to i64`);
226
+ return boxed;
227
+ }
228
+
229
+ export const elementInlineMethods: ElementInlineMethods = {
230
+ emitElementRead(objectValue: string, keyValue: string): string {
231
+ const result = this.alloca();
232
+ const done = this.label("elem.done");
233
+ const generic = this.label("elem.generic");
234
+ const numeric = emitIsNumber(this, keyValue);
235
+ const indexPath = this.label("elem.index");
236
+ this.terminate(`br i1 ${numeric}, label %${indexPath}, label %${generic}`);
237
+
238
+ this.startBlock(indexPath);
239
+ const isIndex = emitIsIndex(this, keyValue);
240
+ const keyIndex = this.label("elem.index.ok");
241
+ this.terminate(`br i1 ${isIndex}, label %${keyIndex}, label %${generic}`);
242
+ this.startBlock(keyIndex);
243
+ const index = emitIndexOf(this, keyValue);
244
+
245
+ /* -- plain array ------------------------------------------------------- */
246
+ const isArray = emitHasTag(this, objectValue, XT_INLINE_TAGS.array);
247
+ const arrayHit = this.label("elem.array.hit");
248
+ const notArray = this.label("elem.notarray");
249
+ this.terminate(`br i1 ${isArray}, label %${arrayHit}, label %${notArray}`);
250
+ this.startBlock(arrayHit);
251
+ const base = emitPayload(this, objectValue);
252
+ const length32 = loadU32(this, base, XT_LAYOUT.arrayLength);
253
+ const length = reg(this);
254
+ this.emit(` ${length} = zext i32 ${length32} to i64`);
255
+ const inBounds = reg(this);
256
+ this.emit(` ${inBounds} = icmp ult i64 ${index}, ${length}`);
257
+ const arrayStore = this.label("elem.array.store");
258
+ this.terminate(`br i1 ${inBounds}, label %${arrayStore}, label %${generic}`);
259
+ this.startBlock(arrayStore);
260
+ const slot = arraySlot(this, base, index);
261
+ const loaded = reg(this);
262
+ this.emit(` ${loaded} = load i64, i64* ${slot}`);
263
+ this.emit(` store i64 ${loaded}, i64* ${result}`);
264
+ this.terminate(`br label %${done}`);
265
+
266
+ /* -- typed-array view -------------------------------------------------- */
267
+ this.startBlock(notArray);
268
+ const isObject = emitHasTag(this, objectValue, XT_INLINE_TAGS.object);
269
+ const viewCheck = this.label("elem.view.check");
270
+ this.terminate(`br i1 ${isObject}, label %${viewCheck}, label %${generic}`);
271
+ this.startBlock(viewCheck);
272
+ const viewBase = emitPayload(this, objectValue);
273
+ const view = loadI64(this, viewBase, XT_LAYOUT.objectViewBytes);
274
+ const hasView = reg(this);
275
+ this.emit(` ${hasView} = icmp ne i64 ${view}, 0`);
276
+ const kindCheck = this.label("elem.view.kind");
277
+ this.terminate(`br i1 ${hasView}, label %${kindCheck}, label %${generic}`);
278
+ this.startBlock(kindCheck);
279
+ const viewLength32 = loadU32(this, viewBase, XT_LAYOUT.objectViewLength);
280
+ const viewLength = reg(this);
281
+ this.emit(` ${viewLength} = zext i32 ${viewLength32} to i64`);
282
+ const kindRaw = loadI16(this, viewBase, XT_LAYOUT.objectTypedKind);
283
+ const kindSet = reg(this);
284
+ this.emit(` ${kindSet} = icmp ne i16 ${kindRaw}, 0`);
285
+ const kind = reg(this);
286
+ this.emit(` ${kind} = add i16 ${kindRaw}, -1`);
287
+ const dispatch = this.label("elem.view.dispatch");
288
+ this.terminate(`br i1 ${kindSet}, label %${dispatch}, label %${generic}`);
289
+
290
+ this.startBlock(dispatch);
291
+ for (const entry of INLINE_READ_KINDS) {
292
+ const matches = reg(this);
293
+ this.emit(` ${matches} = icmp eq i16 ${kind}, ${entry.kind}`);
294
+ const hit = this.label(`elem.k${entry.kind}`);
295
+ const next = this.label("elem.nextkind");
296
+ this.terminate(`br i1 ${matches}, label %${hit}, label %${next}`);
297
+ this.startBlock(hit);
298
+ const { address, end } = viewOffset(this, view, index, entry.width);
299
+ const fits = reg(this);
300
+ this.emit(` ${fits} = icmp ule i64 ${end}, ${viewLength}`);
301
+ const load = this.label("elem.load");
302
+ this.terminate(`br i1 ${fits}, label %${load}, label %${generic}`);
303
+ this.startBlock(load);
304
+ const element = loadTypedElement(this, address, entry.kind);
305
+ this.emit(` store i64 ${element}, i64* ${result}`);
306
+ this.terminate(`br label %${done}`);
307
+ this.startBlock(next);
308
+ }
309
+ this.terminate(`br label %${generic}`);
310
+
311
+ /* -- fallback ---------------------------------------------------------- */
312
+ this.startBlock(generic);
313
+ const fallback = reg(this);
314
+ this.emit(` ${fallback} = call i64 @xt_get_element(i64 ${objectValue}, i64 ${keyValue}, i64* null)`);
315
+ this.emit(` store i64 ${fallback}, i64* ${result}`);
316
+ this.terminate(`br label %${done}`);
317
+ this.startBlock(done);
318
+ const merged = reg(this);
319
+ this.emit(` ${merged} = load i64, i64* ${result}`);
320
+ return merged;
321
+ },
322
+
323
+ emitElementWrite(objectValue: string, keyValue: string, value: string): void {
324
+ const done = this.label("setelem.done");
325
+ const generic = this.label("setelem.generic");
326
+ const numeric = emitIsNumber(this, keyValue);
327
+ const indexPath = this.label("setelem.index");
328
+ this.terminate(`br i1 ${numeric}, label %${indexPath}, label %${generic}`);
329
+
330
+ this.startBlock(indexPath);
331
+ const isIndex = emitIsIndex(this, keyValue);
332
+ const keyIndex = this.label("setelem.index.ok");
333
+ this.terminate(`br i1 ${isIndex}, label %${keyIndex}, label %${generic}`);
334
+ this.startBlock(keyIndex);
335
+ const index = emitIndexOf(this, keyValue);
336
+
337
+ /* -- plain array: an in-range store never changes the length ----------- */
338
+ const isArray = emitHasTag(this, objectValue, XT_INLINE_TAGS.array);
339
+ const arrayHit = this.label("setelem.array.hit");
340
+ const notArray = this.label("setelem.notarray");
341
+ this.terminate(`br i1 ${isArray}, label %${arrayHit}, label %${notArray}`);
342
+ this.startBlock(arrayHit);
343
+ const base = emitPayload(this, objectValue);
344
+ const length32 = loadU32(this, base, XT_LAYOUT.arrayLength);
345
+ const length = reg(this);
346
+ this.emit(` ${length} = zext i32 ${length32} to i64`);
347
+ const inBounds = reg(this);
348
+ this.emit(` ${inBounds} = icmp ult i64 ${index}, ${length}`);
349
+ const arrayStore = this.label("setelem.array.store");
350
+ this.terminate(`br i1 ${inBounds}, label %${arrayStore}, label %${generic}`);
351
+ this.startBlock(arrayStore);
352
+ const slot = arraySlot(this, base, index);
353
+ this.emit(` store i64 ${value}, i64* ${slot}`);
354
+ this.terminate(`br label %${done}`);
355
+
356
+ /* -- typed-array view: float kinds only (integer stores coerce) -------- */
357
+ this.startBlock(notArray);
358
+ const isObject = emitHasTag(this, objectValue, XT_INLINE_TAGS.object);
359
+ const viewCheck = this.label("setelem.view.check");
360
+ this.terminate(`br i1 ${isObject}, label %${viewCheck}, label %${generic}`);
361
+ this.startBlock(viewCheck);
362
+ const viewBase = emitPayload(this, objectValue);
363
+ const view = loadI64(this, viewBase, XT_LAYOUT.objectViewBytes);
364
+ const hasView = reg(this);
365
+ this.emit(` ${hasView} = icmp ne i64 ${view}, 0`);
366
+ const kindCheck = this.label("setelem.kind");
367
+ this.terminate(`br i1 ${hasView}, label %${kindCheck}, label %${generic}`);
368
+ this.startBlock(kindCheck);
369
+ const viewLength32 = loadU32(this, viewBase, XT_LAYOUT.objectViewLength);
370
+ const viewLength = reg(this);
371
+ this.emit(` ${viewLength} = zext i32 ${viewLength32} to i64`);
372
+ const kindRaw = loadI16(this, viewBase, XT_LAYOUT.objectTypedKind);
373
+ const kind = reg(this);
374
+ this.emit(` ${kind} = add i16 ${kindRaw}, -1`);
375
+ /* A non-number value needs the runtime's ToNumber conversion. */
376
+ const valueIsNumber = emitIsNumber(this, value);
377
+ const f32Test = this.label("setelem.f32.test");
378
+ this.terminate(`br i1 ${valueIsNumber}, label %${f32Test}, label %${generic}`);
379
+
380
+ this.startBlock(f32Test);
381
+ const isF32 = reg(this);
382
+ this.emit(` ${isF32} = icmp eq i16 ${kind}, 7`);
383
+ const f32Store = this.label("setelem.f32");
384
+ const f64Test = this.label("setelem.f64.test");
385
+ this.terminate(`br i1 ${isF32}, label %${f32Store}, label %${f64Test}`);
386
+
387
+ this.startBlock(f32Store);
388
+ {
389
+ const { address, end } = viewOffset(this, view, index, 4);
390
+ const fits = reg(this);
391
+ this.emit(` ${fits} = icmp ule i64 ${end}, ${viewLength}`);
392
+ const ok = this.label("setelem.f32.ok");
393
+ this.terminate(`br i1 ${fits}, label %${ok}, label %${generic}`);
394
+ this.startBlock(ok);
395
+ const pointer = reg(this);
396
+ const wide = reg(this);
397
+ const narrowed = reg(this);
398
+ this.emit(` ${pointer} = bitcast i8* ${address} to float*`);
399
+ this.emit(` ${wide} = bitcast i64 ${value} to double`);
400
+ this.emit(` ${narrowed} = fptrunc double ${wide} to float`);
401
+ this.emit(` store float ${narrowed}, float* ${pointer}`);
402
+ this.terminate(`br label %${done}`);
403
+ }
404
+
405
+ this.startBlock(f64Test);
406
+ const isF64 = reg(this);
407
+ this.emit(` ${isF64} = icmp eq i16 ${kind}, 8`);
408
+ const f64Store = this.label("setelem.f64");
409
+ this.terminate(`br i1 ${isF64}, label %${f64Store}, label %${generic}`);
410
+
411
+ this.startBlock(f64Store);
412
+ {
413
+ const { address, end } = viewOffset(this, view, index, 8);
414
+ const fits = reg(this);
415
+ this.emit(` ${fits} = icmp ule i64 ${end}, ${viewLength}`);
416
+ const ok = this.label("setelem.f64.ok");
417
+ this.terminate(`br i1 ${fits}, label %${ok}, label %${generic}`);
418
+ this.startBlock(ok);
419
+ const pointer = reg(this);
420
+ const wide = reg(this);
421
+ this.emit(` ${pointer} = bitcast i8* ${address} to double*`);
422
+ this.emit(` ${wide} = bitcast i64 ${value} to double`);
423
+ this.emit(` store double ${wide}, double* ${pointer}`);
424
+ this.terminate(`br label %${done}`);
425
+ }
426
+
427
+ this.startBlock(generic);
428
+ this.emit(` call i64 @xt_set_element(i64 ${objectValue}, i64 ${keyValue}, i64 ${value})`);
429
+ this.terminate(`br label %${done}`);
430
+ this.startBlock(done);
431
+ },
432
+ };
433
+
@@ -36,6 +36,51 @@ export interface InvocationCallMethods {
36
36
  emitArguments(this: Generator, args: readonly Expression[]): { argc: string; ptr: string };
37
37
  }
38
38
 
39
+ /**
40
+ * Fixed-arity `Math.<fn>` calls lower to their own runtime entry point instead
41
+ * of `xt_math_call(name, argc, argv)`. The name and arity are known here, so
42
+ * the generated call passes the arguments in registers and the runtime skips
43
+ * the name dispatch and the argument array. A numeric kernel calls these
44
+ * hundreds of times per pixel (the Saltline terrain generator alone executes
45
+ * ~125 million `Math` calls), which made the generic ABI the dominant cost.
46
+ * `min`/`max` stay variadic and keep `xt_math_call`.
47
+ */
48
+ const MATH_DIRECT: Record<string, { symbol: string; arity: number }> = {
49
+ abs: { symbol: "xt_math1_abs", arity: 1 },
50
+ floor: { symbol: "xt_math1_floor", arity: 1 },
51
+ ceil: { symbol: "xt_math1_ceil", arity: 1 },
52
+ round: { symbol: "xt_math1_round", arity: 1 },
53
+ trunc: { symbol: "xt_math1_trunc", arity: 1 },
54
+ sqrt: { symbol: "xt_math1_sqrt", arity: 1 },
55
+ cbrt: { symbol: "xt_math1_cbrt", arity: 1 },
56
+ exp: { symbol: "xt_math1_exp", arity: 1 },
57
+ log: { symbol: "xt_math1_log", arity: 1 },
58
+ log2: { symbol: "xt_math1_log2", arity: 1 },
59
+ log10: { symbol: "xt_math1_log10", arity: 1 },
60
+ log1p: { symbol: "xt_math1_log1p", arity: 1 },
61
+ expm1: { symbol: "xt_math1_expm1", arity: 1 },
62
+ sinh: { symbol: "xt_math1_sinh", arity: 1 },
63
+ cosh: { symbol: "xt_math1_cosh", arity: 1 },
64
+ tanh: { symbol: "xt_math1_tanh", arity: 1 },
65
+ asinh: { symbol: "xt_math1_asinh", arity: 1 },
66
+ acosh: { symbol: "xt_math1_acosh", arity: 1 },
67
+ atanh: { symbol: "xt_math1_atanh", arity: 1 },
68
+ fround: { symbol: "xt_math1_fround", arity: 1 },
69
+ clz32: { symbol: "xt_math1_clz32", arity: 1 },
70
+ sin: { symbol: "xt_math1_sin", arity: 1 },
71
+ cos: { symbol: "xt_math1_cos", arity: 1 },
72
+ tan: { symbol: "xt_math1_tan", arity: 1 },
73
+ asin: { symbol: "xt_math1_asin", arity: 1 },
74
+ acos: { symbol: "xt_math1_acos", arity: 1 },
75
+ atan: { symbol: "xt_math1_atan", arity: 1 },
76
+ sign: { symbol: "xt_math1_sign", arity: 1 },
77
+ pow: { symbol: "xt_math2_pow", arity: 2 },
78
+ atan2: { symbol: "xt_math2_atan2", arity: 2 },
79
+ hypot: { symbol: "xt_math2_hypot", arity: 2 },
80
+ imul: { symbol: "xt_math2_imul", arity: 2 },
81
+ random: { symbol: "xt_math0_random", arity: 0 },
82
+ };
83
+
39
84
  export const invocationCallMethods: InvocationCallMethods = {
40
85
  emitCall(node: CallExpression): string {
41
86
  const callee = node.expression;
@@ -87,7 +132,18 @@ export const invocationCallMethods: InvocationCallMethods = {
87
132
  const name = this.stringValue(access.name.text);
88
133
  const args = this.emitArguments(node.arguments);
89
134
  const result = this.reg();
90
- this.emit(` ${result} = call i64 @xt_call_method(i64 ${object}, i64 ${name}, i32 ${args.argc}, i64* ${args.ptr})`);
135
+ /* Method dispatch is monomorphic at most sites, so the call carries a
136
+ * three-word inline cache: a shape/slot hit calls the cached method value
137
+ * directly instead of walking the dispatch tables and the prototype
138
+ * chain. See `runtime/xt_containers/access.inc`. */
139
+ this.extraDeclarations.add("declare i64 @xt_call_method_ic(i64, i64, i32, i64*, i64*)");
140
+ const cache = `@mt.${this.icCounter++}`;
141
+ this.globals.push(`${cache} = internal global [3 x i64] zeroinitializer`);
142
+ const slot = this.reg();
143
+ this.emit(` ${slot} = getelementptr [3 x i64], [3 x i64]* ${cache}, i64 0, i64 0`);
144
+ this.emit(
145
+ ` ${result} = call i64 @xt_call_method_ic(i64 ${object}, i64 ${name}, i32 ${args.argc}, i64* ${args.ptr}, i64* ${slot})`,
146
+ );
91
147
  return result;
92
148
  }
93
149
  if (callee.kind === SyntaxKind.ElementAccessExpression) {
@@ -139,7 +195,23 @@ export const invocationCallMethods: InvocationCallMethods = {
139
195
  this.emit(` ${result} = call i64 @${globalFunction}(i32 ${args.argc}, i64* ${args.ptr})`);
140
196
  return result;
141
197
  }
198
+ /* A browser global that is really a window method (`addEventListener`):
199
+ * dispatch through the host object so `this` is the window. The builtin
200
+ * table is consulted first — a host that needs to do extra work for a
201
+ * global callable (`requestAnimationFrame` also arms the frame pump)
202
+ * declares a builtin, and that must win over the plain member call. */
203
+ const windowMember = !symbol ? this.globalValues[identifier.text] : undefined;
142
204
  const builtin = this.builtins[(callee as Identifier).text];
205
+ if (windowMember?.member !== undefined && !builtin) {
206
+ const host = this.emitGlobalValue({ symbol: windowMember.symbol });
207
+ const name = this.stringValue(windowMember.member);
208
+ const args = this.emitArguments(node.arguments);
209
+ const result = this.reg();
210
+ this.emit(
211
+ ` ${result} = call i64 @xt_call_method(i64 ${host}, i64 ${name}, i32 ${args.argc}, i64* ${args.ptr})`,
212
+ );
213
+ return result;
214
+ }
143
215
  if (!symbol && builtin) {
144
216
  this.extraDeclarations.add(
145
217
  builtin.returnVoid ? `declare void @${builtin.symbol}(i32, i64*)` : `declare i64 @${builtin.symbol}(i32, i64*)`,
@@ -190,6 +262,20 @@ export const invocationCallMethods: InvocationCallMethods = {
190
262
 
191
263
  if (targetIdentifier && targetIdentifier.text === "Math" && !targetSymbol) {
192
264
  if (!MATH_FUNCTIONS.has(method)) return undefined;
265
+ const direct = MATH_DIRECT[method];
266
+ const hasSpread = node.arguments.some((argument) => argument.kind === SyntaxKind.SpreadElement);
267
+ if (direct && !hasSpread && node.arguments.length === direct.arity) {
268
+ const values = node.arguments.map((argument) => this.emitExpression(argument));
269
+ const parameterList = values.map(() => "i64").join(", ");
270
+ /* Numeric literals come back as bare bit patterns (`values.ts` leaves
271
+ * the type to the instruction that consumes them), so every operand is
272
+ * typed here explicitly. */
273
+ const operands = values.map((value) => `i64 ${value}`).join(", ");
274
+ this.extraDeclarations.add(`declare i64 @${direct.symbol}(${parameterList})`);
275
+ const result = this.reg();
276
+ this.emit(` ${result} = call i64 @${direct.symbol}(${operands})`);
277
+ return result;
278
+ }
193
279
  const name = this.stringValue(method);
194
280
  const args = this.emitArguments(node.arguments);
195
281
  const result = this.reg();
@@ -5,6 +5,7 @@
5
5
  * The implementation is split by functional area under `calls/`:
6
6
  * - `invocation` — direct calls, built-ins, imports and argument marshalling
7
7
  * - `access` — property/element reads, `delete` and optional chaining
8
+ * - `element-inline` — the inlined `obj[key]` fast path (plain arrays and typed-array views)
8
9
  * - `literals` — array and object literals
9
10
  * - `closures` — arrow/function expressions and captured environments
10
11
  * - `super` — parent constructor/method delegation
@@ -12,6 +13,7 @@
12
13
 
13
14
  import { invocationCallMethods, type InvocationCallMethods } from "./calls/invocation.js";
14
15
  import { accessCallMethods, type AccessCallMethods } from "./calls/access.js";
16
+ import { elementInlineMethods, type ElementInlineMethods } from "./calls/element-inline.js";
15
17
  import { literalCallMethods, type LiteralCallMethods } from "./calls/literals.js";
16
18
  import { closureCallMethods, type ClosureCallMethods } from "./calls/closures.js";
17
19
  import { superCallMethods, type SuperCallMethods } from "./calls/super.js";
@@ -19,6 +21,7 @@ import { superCallMethods, type SuperCallMethods } from "./calls/super.js";
19
21
  export interface CallMethods
20
22
  extends InvocationCallMethods,
21
23
  AccessCallMethods,
24
+ ElementInlineMethods,
22
25
  LiteralCallMethods,
23
26
  ClosureCallMethods,
24
27
  SuperCallMethods {}
@@ -27,7 +30,9 @@ export const callMethods: CallMethods = Object.assign(
27
30
  {},
28
31
  invocationCallMethods,
29
32
  accessCallMethods,
33
+ elementInlineMethods,
30
34
  literalCallMethods,
31
35
  closureCallMethods,
32
36
  superCallMethods,
33
37
  );
38
+
@@ -39,6 +39,21 @@ function escapeMetadata(text: string): string {
39
39
  return escapeBytes(utf8Bytes(text));
40
40
  }
41
41
 
42
+ /**
43
+ * DWARF/Verifier limits for `!DILocation`. The column is a 16-bit field, so a
44
+ * minified source line (`<script>` bundles are routinely 60–80k columns wide)
45
+ * makes the verifier reject the whole module. The line has its own, larger
46
+ * limit; clamp both rather than failing the build: a debug location is a
47
+ * best-effort hint, and a wrong-but-valid one still points at the statement.
48
+ */
49
+ const MAX_DEBUG_LINE = 0xffffff;
50
+ const MAX_DEBUG_COLUMN = 0xffff;
51
+
52
+ function clampDebugPosition(value: number, limit: number): number {
53
+ if (!Number.isFinite(value) || value < 0) return 0;
54
+ return Math.min(Math.floor(value), limit);
55
+ }
56
+
42
57
  /**
43
58
  * Create the compile unit and module flags once. LLVM requires the `Debug Info
44
59
  * Version` flag for any `!dbg` attachment, and clang only emits DWARF/CodeView
@@ -97,10 +112,11 @@ export const debugMethods: DebugMethods = {
97
112
  : fn.isConstructor && fn.classInfo
98
113
  ? `new ${this.displayNameOf(fn.classInfo.name)}`
99
114
  : this.displayNameOf(fn.name.length > 0 ? fn.name : "anonymous");
115
+ const line = clampDebugPosition(position.line, MAX_DEBUG_LINE);
100
116
  const subprogram = this.addMetadata(
101
117
  'distinct !DISubprogram(name: ' +
102
118
  `"${escapeMetadata(displayName)}", scope: ${this.debugUnit}, file: ${file}, ` +
103
- `line: ${position.line}, type: ${this.debugSubroutineType}, scopeLine: ${position.line}, ` +
119
+ `line: ${line}, type: ${this.debugSubroutineType}, scopeLine: ${line}, ` +
104
120
  `spFlags: DISPFlagDefinition, unit: ${this.debugUnit})`,
105
121
  );
106
122
  this.debugSubprograms.set(fn.id, subprogram);
@@ -110,11 +126,13 @@ export const debugMethods: DebugMethods = {
110
126
  setDebugLocation(node) {
111
127
  if (!this.debug || this.current.debugSubprogram === undefined) return;
112
128
  const position = this.positionOf(node);
113
- const key = `${this.current.debugSubprogram}:${position.line}:${position.column}`;
129
+ const line = clampDebugPosition(position.line, MAX_DEBUG_LINE);
130
+ const column = clampDebugPosition(position.column, MAX_DEBUG_COLUMN);
131
+ const key = `${this.current.debugSubprogram}:${line}:${column}`;
114
132
  let location = this.debugLocations.get(key);
115
133
  if (!location) {
116
134
  location = this.addMetadata(
117
- `!DILocation(line: ${position.line}, column: ${position.column}, scope: ${this.current.debugSubprogram})`,
135
+ `!DILocation(line: ${line}, column: ${column}, scope: ${this.current.debugSubprogram})`,
118
136
  );
119
137
  this.debugLocations.set(key, location);
120
138
  }
@@ -144,14 +144,22 @@ export const assignmentExpressionMethods: AssignmentExpressionMethods = {
144
144
  }
145
145
  const object = this.emitExpression(access.expression);
146
146
  const key = this.stringValue(access.name.text);
147
- this.emit(` call i64 @xt_set(i64 ${object}, i64 ${key}, i64 ${value})`);
147
+ /* A store site gets the same monomorphic inline cache a read site has:
148
+ * `obj.x = v` in a hot loop otherwise resolves the property (own scan,
149
+ * prototype-chain walk for setters) on every assignment. */
150
+ this.extraDeclarations.add("declare i64 @xt_set_ic(i64, i64, i64, i64*)");
151
+ const cache = `@st.${this.icCounter++}`;
152
+ this.globals.push(`${cache} = internal global [2 x i64] zeroinitializer`);
153
+ const slot = this.reg();
154
+ this.emit(` ${slot} = getelementptr [2 x i64], [2 x i64]* ${cache}, i64 0, i64 0`);
155
+ this.emit(` call i64 @xt_set_ic(i64 ${object}, i64 ${key}, i64 ${value}, i64* ${slot})`);
148
156
  return;
149
157
  }
150
158
  case SyntaxKind.ElementAccessExpression: {
151
159
  const access = target as ElementAccessExpression;
152
160
  const object = this.emitExpression(access.expression);
153
161
  const key = this.emitExpression(access.argumentExpression);
154
- this.emit(` call i64 @xt_set(i64 ${object}, i64 ${key}, i64 ${value})`);
162
+ this.emitElementWrite(object, key, value);
155
163
  return;
156
164
  }
157
165
  case SyntaxKind.ParenthesizedExpression:
@@ -244,3 +252,4 @@ export const assignmentExpressionMethods: AssignmentExpressionMethods = {
244
252
  }
245
253
  },
246
254
  };
255
+