eliscript 0.1.3 → 0.1.4

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 (526) hide show
  1. package/COPYING.RUNTIME +93 -0
  2. package/README.md +15 -3
  3. package/bin/eliscript-test +29 -0
  4. package/bootstrap/backends/README.md +5 -4
  5. package/bootstrap/backends/binary.eli +89 -22
  6. package/bootstrap/backends/capabilities.eli +9 -4
  7. package/bootstrap/backends/managed-layout.eli +93 -81
  8. package/bootstrap/backends/managed.eli +651 -125
  9. package/bootstrap/backends/native.eli +1791 -324
  10. package/bootstrap/backends/numeric.eli +71 -7
  11. package/bootstrap/backends/sha256.eli +165 -68
  12. package/bootstrap/backends/wasm.eli +461 -52
  13. package/bootstrap/compiler/analyzer.eli +692 -2
  14. package/bootstrap/compiler/compiler.eli +16 -3
  15. package/bootstrap/compiler/emitter.eli +80 -35
  16. package/bootstrap/compiler/project.eli +175 -1
  17. package/bootstrap/compiler/transient-analysis.eli +2 -2
  18. package/bootstrap/host/build.eli +1 -0
  19. package/bootstrap/host/build.mjs +1 -1
  20. package/bootstrap/host/build.mjs.map +1 -1
  21. package/bootstrap/host/bun.mjs +7 -9
  22. package/bootstrap/host/bun.mjs.map +1 -1
  23. package/bootstrap/host/check-cli-ground.eli +47 -5
  24. package/bootstrap/host/check-cli-ground.mjs +60 -19
  25. package/bootstrap/host/check-cli-ground.mjs.map +1 -1
  26. package/bootstrap/host/check-cli.mjs +41 -1
  27. package/bootstrap/host/check-cli.mjs.map +1 -1
  28. package/bootstrap/host/compiler-session.eli +136 -22
  29. package/bootstrap/host/eliscript-cli.mjs +1 -1
  30. package/bootstrap/host/eliscript-cli.mjs.map +1 -1
  31. package/bootstrap/host/evaluation-cli.eli +33 -1
  32. package/bootstrap/host/evaluation-cli.mjs +65 -28
  33. package/bootstrap/host/evaluation-cli.mjs.map +1 -1
  34. package/bootstrap/host/evaluation.eli +651 -42
  35. package/bootstrap/host/evaluation.mjs +884 -72
  36. package/bootstrap/host/evaluation.mjs.map +1 -1
  37. package/bootstrap/host/project-cli-ground.eli +6 -1
  38. package/bootstrap/host/project-cli-ground.mjs +33 -32
  39. package/bootstrap/host/project-cli-ground.mjs.map +1 -1
  40. package/bootstrap/host/project-cli.mjs +1 -1
  41. package/bootstrap/host/project-cli.mjs.map +1 -1
  42. package/bootstrap/host/project-ground.eli +266 -45
  43. package/bootstrap/host/project-ground.mjs +429 -159
  44. package/bootstrap/host/project-ground.mjs.map +1 -1
  45. package/bootstrap/host/project.mjs +30 -40
  46. package/bootstrap/host/project.mjs.map +1 -1
  47. package/bootstrap/host/test-cli.eli +379 -0
  48. package/bootstrap/host/test-cli.mjs +494 -0
  49. package/bootstrap/host/test-cli.mjs.map +1 -0
  50. package/browser/README.md +7 -0
  51. package/browser/host.mjs +21 -29
  52. package/browser/host.mjs.map +1 -1
  53. package/browser/worker.mjs +7 -9
  54. package/browser/worker.mjs.map +1 -1
  55. package/compiler/eliscript-analyzer.el +694 -0
  56. package/compiler/eliscript-cli.el +66 -22
  57. package/compiler/eliscript-emitter.el +67 -43
  58. package/compiler/eliscript-ir-emitter.el +71 -36
  59. package/compiler/eliscript-project.el +360 -80
  60. package/compiler/eliscript-transient-analysis.el +5 -5
  61. package/compiler/eliscript.el +6 -0
  62. package/dist/backends/binary.mjs +148 -26
  63. package/dist/backends/binary.mjs.map +1 -1
  64. package/dist/backends/capabilities.mjs +11 -4
  65. package/dist/backends/capabilities.mjs.map +1 -1
  66. package/dist/backends/elf.mjs +4 -4
  67. package/dist/backends/elf.mjs.map +1 -1
  68. package/dist/backends/managed-layout.mjs +53 -56
  69. package/dist/backends/managed-layout.mjs.map +1 -1
  70. package/dist/backends/managed.mjs +1116 -375
  71. package/dist/backends/managed.mjs.map +1 -1
  72. package/dist/backends/memory.mjs +24 -26
  73. package/dist/backends/memory.mjs.map +1 -1
  74. package/dist/backends/native.mjs +2529 -434
  75. package/dist/backends/native.mjs.map +1 -1
  76. package/dist/backends/numeric.mjs +140 -33
  77. package/dist/backends/numeric.mjs.map +1 -1
  78. package/dist/backends/optimize.mjs +2 -2
  79. package/dist/backends/optimize.mjs.map +1 -1
  80. package/dist/backends/regexp.mjs +33 -35
  81. package/dist/backends/regexp.mjs.map +1 -1
  82. package/dist/backends/sha256.mjs +159 -59
  83. package/dist/backends/sha256.mjs.map +1 -1
  84. package/dist/backends/wasm.mjs +609 -73
  85. package/dist/backends/wasm.mjs.map +1 -1
  86. package/dist/bootstrap/analyzer.mjs +1018 -2
  87. package/dist/bootstrap/analyzer.mjs.map +1 -1
  88. package/dist/bootstrap/compiler.mjs +30 -23
  89. package/dist/bootstrap/compiler.mjs.map +1 -1
  90. package/dist/bootstrap/emitter.mjs +206 -161
  91. package/dist/bootstrap/emitter.mjs.macro-origins.json +16 -16
  92. package/dist/bootstrap/emitter.mjs.map +1 -1
  93. package/dist/bootstrap/expander.mjs +27 -31
  94. package/dist/bootstrap/expander.mjs.map +1 -1
  95. package/dist/bootstrap/ir.mjs +22 -24
  96. package/dist/bootstrap/ir.mjs.map +1 -1
  97. package/dist/bootstrap/project.mjs +246 -1
  98. package/dist/bootstrap/project.mjs.map +1 -1
  99. package/dist/bootstrap/reader.mjs +2 -2
  100. package/dist/bootstrap/reader.mjs.map +1 -1
  101. package/dist/bootstrap/source-map.mjs +34 -48
  102. package/dist/bootstrap/source-map.mjs.map +1 -1
  103. package/dist/bootstrap/transient-analysis.mjs +2 -2
  104. package/dist/bootstrap/transient-analysis.mjs.map +1 -1
  105. package/dist/browser/compiler.js +106 -106
  106. package/dist/browser/host.mjs +21 -29
  107. package/dist/browser/loader.js +1 -1
  108. package/dist/browser/worker.mjs +95 -95
  109. package/dist/compiler-artifact.json +1 -1
  110. package/dist/host/compiler-session.mjs +405 -228
  111. package/dist/host/compiler-session.mjs.map +1 -1
  112. package/dist/host/compiler-tools.mjs +44 -46
  113. package/dist/host/compiler-tools.mjs.map +1 -1
  114. package/dist/literal-sources/runtime/core/array-map.eli +45 -0
  115. package/dist/literal-sources/runtime/core/collection-internals.eli +22 -2
  116. package/dist/literal-sources/runtime/core/collection.eli +282 -23
  117. package/dist/literal-sources/runtime/core/data-text.eli +196 -13
  118. package/dist/literal-sources/runtime/core/data.eli +1 -1
  119. package/dist/literal-sources/runtime/core/identifier.eli +1 -1
  120. package/dist/literal-sources/runtime/core/lazy-sequence.eli +822 -44
  121. package/dist/literal-sources/runtime/core/list.eli +1 -1
  122. package/dist/literal-sources/runtime/core/map.eli +1 -1
  123. package/dist/literal-sources/runtime/core/metadata-internals.eli +2 -2
  124. package/dist/literal-sources/runtime/core/metadata.eli +2 -2
  125. package/dist/literal-sources/{stdlib/core/object.eli → runtime/core/object-impl.eli} +6 -6
  126. package/dist/literal-sources/runtime/core/object.eli +1 -1
  127. package/dist/literal-sources/runtime/core/order.eli +3 -3
  128. package/dist/literal-sources/runtime/core/persistent-kind.eli +1 -1
  129. package/dist/literal-sources/{stdlib/core/protocol.eli → runtime/core/protocol-impl.eli} +2 -2
  130. package/dist/literal-sources/runtime/core/protocol.eli +5 -3
  131. package/dist/literal-sources/runtime/core/queue.eli +1 -1
  132. package/dist/literal-sources/runtime/core/record.eli +3 -3
  133. package/dist/literal-sources/runtime/core/regex.eli +2 -2
  134. package/dist/literal-sources/runtime/core/sequence.eli +1 -1
  135. package/dist/literal-sources/runtime/core/set-algebra.eli +1 -1
  136. package/dist/literal-sources/runtime/core/set.eli +1 -1
  137. package/dist/literal-sources/runtime/core/sorted.eli +4 -4
  138. package/dist/literal-sources/{stdlib/core/text.eli → runtime/core/text-impl.eli} +2 -2
  139. package/dist/literal-sources/runtime/core/text.eli +1 -1
  140. package/dist/literal-sources/runtime/core/transducer.eli +24 -2
  141. package/dist/literal-sources/runtime/core/transient.eli +7 -5
  142. package/dist/literal-sources/runtime/core/type.eli +1 -1
  143. package/dist/literal-sources/runtime/core/value-internals.eli +3 -3
  144. package/dist/literal-sources/runtime/core/value.eli +2 -2
  145. package/dist/literal-sources/runtime/core/vector.eli +5 -2
  146. package/dist/literal-sources/{stdlib → runtime/managed}/managed-literal-protocols.eli +100 -34
  147. package/dist/literal-sources/{stdlib → runtime/managed}/managed-literals.eli +406 -81
  148. package/{stdlib → dist/literal-sources/runtime/managed}/managed-queue-state.eli +1 -1
  149. package/{stdlib → dist/literal-sources/runtime/managed}/managed-sorted-state.eli +1 -1
  150. package/dist/literal-sources/runtime/managed/managed-transient-state.eli +1206 -0
  151. package/{stdlib → dist/literal-sources/runtime/managed}/managed-value-state.eli +30 -6
  152. package/dist/literal-sources/{stdlib → runtime/managed}/managed-vector-view-state.eli +1 -1
  153. package/dist/literal-sources/runtime/wire/http-rpc.eli +1 -1
  154. package/dist/literal-sources/runtime/wire/markdown.eli +1 -1
  155. package/dist/literal-sources/runtime/wire/schema.eli +1 -1
  156. package/dist/literal-sources/stdlib/bit.eli +89 -1
  157. package/dist/literal-sources/stdlib/data.eli +522 -0
  158. package/dist/literal-sources/stdlib/interop/object.eli +39 -0
  159. package/dist/literal-sources/stdlib/persistent-list.eli +60 -21
  160. package/dist/literal-sources/stdlib/persistent-map.eli +335 -324
  161. package/dist/literal-sources/stdlib/persistent-set.eli +8 -6
  162. package/dist/literal-sources/stdlib/persistent-vector.eli +18 -18
  163. package/dist/literal-sources/stdlib/protocol.eli +63 -0
  164. package/{stdlib/core → dist/literal-sources/stdlib}/set.eli +8 -8
  165. package/dist/literal-sources/stdlib/string.eli +362 -0
  166. package/dist/runtime/lisp/async-runtime.mjs +55 -59
  167. package/dist/runtime/lisp/async-runtime.mjs.map +1 -1
  168. package/dist/runtime/lisp/builtins.mjs +232 -280
  169. package/dist/runtime/lisp/builtins.mjs.map +1 -1
  170. package/dist/runtime/lisp/cli.mjs +159 -167
  171. package/dist/runtime/lisp/cli.mjs.map +1 -1
  172. package/dist/runtime/lisp/compiler.mjs +359 -419
  173. package/dist/runtime/lisp/compiler.mjs.map +1 -1
  174. package/dist/runtime/lisp/core.mjs +24 -24
  175. package/dist/runtime/lisp/core.mjs.map +1 -1
  176. package/dist/runtime/lisp/evaluator.mjs +315 -385
  177. package/dist/runtime/lisp/evaluator.mjs.map +1 -1
  178. package/dist/runtime/lisp/extensions.mjs +85 -103
  179. package/dist/runtime/lisp/extensions.mjs.map +1 -1
  180. package/dist/runtime/lisp/host-io.mjs +106 -122
  181. package/dist/runtime/lisp/host-io.mjs.map +1 -1
  182. package/dist/runtime/lisp/io-worker.mjs +117 -135
  183. package/dist/runtime/lisp/io-worker.mjs.map +1 -1
  184. package/dist/runtime/lisp/keymap.mjs +5 -5
  185. package/dist/runtime/lisp/keymap.mjs.map +1 -1
  186. package/dist/runtime/lisp/macros-forms.mjs +49 -51
  187. package/dist/runtime/lisp/macros-forms.mjs.map +1 -1
  188. package/dist/runtime/lisp/reader-format.mjs +31 -31
  189. package/dist/runtime/lisp/reader-format.mjs.map +1 -1
  190. package/dist/runtime/lisp/regexp-buffer.mjs +9 -9
  191. package/dist/runtime/lisp/regexp-buffer.mjs.map +1 -1
  192. package/dist/runtime/math.mjs +32 -38
  193. package/dist/runtime/math.mjs.map +1 -1
  194. package/dist/runtime/number-radix.mjs +5 -7
  195. package/dist/runtime/number-radix.mjs.map +1 -1
  196. package/dist/runtime/primitives.mjs +81 -46
  197. package/dist/runtime/primitives.mjs.macro-origins.json +294 -44
  198. package/dist/runtime/primitives.mjs.map +1 -1
  199. package/dist/runtime/unicode-case.mjs +6 -6
  200. package/dist/runtime/unicode-case.mjs.map +1 -1
  201. package/dist/runtime.eli +3068 -1882
  202. package/dist/stage3-release.json +1 -1
  203. package/dist/stdlib/async.mjs +1838 -0
  204. package/dist/stdlib/bit.mjs +116 -0
  205. package/dist/stdlib/collection.mjs +193 -0
  206. package/dist/stdlib/core.mjs +28 -0
  207. package/dist/stdlib/data-text.mjs +54 -0
  208. package/dist/stdlib/data.mjs +601 -0
  209. package/dist/stdlib/devtools.mjs +565 -0
  210. package/dist/stdlib/diff.mjs +180 -0
  211. package/dist/stdlib/fn.mjs +164 -0
  212. package/dist/stdlib/hierarchy.mjs +189 -0
  213. package/dist/stdlib/identifier.mjs +33 -0
  214. package/dist/stdlib/interop/js.mjs +706 -0
  215. package/dist/stdlib/interop/object.mjs +40 -0
  216. package/dist/stdlib/json.mjs +843 -0
  217. package/dist/stdlib/lazy-sequence.mjs +117 -0
  218. package/dist/stdlib/math.mjs +386 -0
  219. package/dist/stdlib/meta.mjs +21 -0
  220. package/dist/stdlib/multimethod.mjs +345 -0
  221. package/dist/stdlib/numeric.mjs +327 -0
  222. package/dist/stdlib/order.mjs +194 -0
  223. package/dist/stdlib/persistent-list.mjs +171 -0
  224. package/dist/stdlib/persistent-map.mjs +781 -0
  225. package/dist/stdlib/persistent-queue.mjs +44 -0
  226. package/dist/stdlib/persistent-set.mjs +225 -0
  227. package/dist/stdlib/persistent-vector.mjs +316 -0
  228. package/dist/stdlib/portable/data-text.mjs +1094 -0
  229. package/dist/stdlib/portable/data.mjs +62 -0
  230. package/dist/stdlib/portable/identifier.mjs +179 -0
  231. package/dist/stdlib/portable/metadata.mjs +63 -0
  232. package/dist/stdlib/portable/object.mjs +140 -0
  233. package/dist/stdlib/portable/sequence.mjs +142 -0
  234. package/dist/stdlib/portable/text.mjs +111 -0
  235. package/dist/stdlib/pprint.mjs +807 -0
  236. package/dist/stdlib/protocol.mjs +61 -0
  237. package/dist/stdlib/record.mjs +25 -0
  238. package/dist/stdlib/regex.mjs +115 -0
  239. package/dist/stdlib/result.mjs +198 -0
  240. package/dist/stdlib/schema.mjs +2828 -0
  241. package/dist/stdlib/seq.mjs +913 -0
  242. package/dist/stdlib/set.mjs +317 -0
  243. package/dist/stdlib/sorted.mjs +56 -0
  244. package/dist/stdlib/state.mjs +451 -0
  245. package/dist/stdlib/string.mjs +494 -0
  246. package/dist/stdlib/tap.mjs +187 -0
  247. package/dist/stdlib/test.mjs +669 -0
  248. package/dist/stdlib/time.mjs +522 -0
  249. package/dist/stdlib/transducer.mjs +85 -0
  250. package/dist/stdlib/transient.mjs +32 -0
  251. package/dist/stdlib/type.mjs +28 -0
  252. package/dist/stdlib/uuid.mjs +377 -0
  253. package/dist/stdlib/value.mjs +334 -0
  254. package/dist/stdlib/walk.mjs +243 -0
  255. package/dist/stdlib/wire/gepo.mjs +28 -0
  256. package/dist/stdlib/wire/mdp.mjs +13 -0
  257. package/dist/stdlib/wire/mds.mjs +10 -0
  258. package/dist/stdlib/zipper.mjs +1025 -0
  259. package/editor/README.md +15 -0
  260. package/editor/eliscript-data.el +221 -8
  261. package/editor/eliscript-mode.el +207 -48
  262. package/editor/eliscript-runtime.el +6 -3
  263. package/package.json +49 -15
  264. package/platform/README.md +6 -0
  265. package/platform/browser.mjs +10 -12
  266. package/platform/browser.mjs.map +1 -1
  267. package/runtime/README.md +12 -5
  268. package/runtime/core/array-map.eli +538 -0
  269. package/runtime/core/array-map.mjs +651 -0
  270. package/runtime/core/array-map.mjs.map +1 -0
  271. package/runtime/core/collection-internals.eli +85 -33
  272. package/runtime/core/collection-internals.mjs +245 -193
  273. package/runtime/core/collection-internals.mjs.map +1 -1
  274. package/runtime/core/collection.eli +105 -3
  275. package/runtime/core/collection.mjs +190 -53
  276. package/runtime/core/collection.mjs.map +1 -1
  277. package/runtime/core/data-text.eli +224 -16
  278. package/runtime/core/data-text.mjs +345 -83
  279. package/runtime/core/data-text.mjs.map +1 -1
  280. package/runtime/core/data.eli +50 -33
  281. package/runtime/core/data.mjs +72 -36
  282. package/runtime/core/data.mjs.map +1 -1
  283. package/runtime/core/identifier.mjs +15 -19
  284. package/runtime/core/identifier.mjs.map +1 -1
  285. package/runtime/core/lazy-sequence.eli +885 -15
  286. package/runtime/core/lazy-sequence.mjs +1265 -38
  287. package/runtime/core/lazy-sequence.mjs.map +1 -1
  288. package/runtime/core/map-internals.eli +209 -1
  289. package/runtime/core/map-internals.mjs +359 -71
  290. package/runtime/core/map-internals.mjs.map +1 -1
  291. package/runtime/core/map.eli +75 -7
  292. package/runtime/core/map.mjs +122 -47
  293. package/runtime/core/map.mjs.map +1 -1
  294. package/{stdlib/core/object.eli → runtime/core/object-impl.eli} +6 -6
  295. package/runtime/core/object-impl.mjs +5 -5
  296. package/runtime/core/object-impl.mjs.map +1 -1
  297. package/runtime/core/order.eli +15 -3
  298. package/runtime/core/order.mjs +23 -8
  299. package/runtime/core/order.mjs.map +1 -1
  300. package/runtime/core/persistent-kind.mjs +9 -13
  301. package/runtime/core/persistent-kind.mjs.map +1 -1
  302. package/runtime/core/protocol-error.mjs +9 -11
  303. package/runtime/core/protocol-error.mjs.map +1 -1
  304. package/{stdlib/core/protocol.eli → runtime/core/protocol-impl.eli} +2 -2
  305. package/runtime/core/protocol-impl.mjs +10 -10
  306. package/runtime/core/protocol-impl.mjs.map +1 -1
  307. package/runtime/core/queue.mjs +14 -16
  308. package/runtime/core/queue.mjs.map +1 -1
  309. package/runtime/core/record.eli +2 -2
  310. package/runtime/core/record.mjs +13 -17
  311. package/runtime/core/record.mjs.map +1 -1
  312. package/runtime/core/regex.mjs +18 -22
  313. package/runtime/core/regex.mjs.map +1 -1
  314. package/runtime/core/sequence.eli +33 -31
  315. package/runtime/core/sequence.mjs +60 -93
  316. package/runtime/core/sequence.mjs.map +1 -1
  317. package/runtime/core/set.eli +53 -27
  318. package/runtime/core/set.mjs +113 -82
  319. package/runtime/core/set.mjs.map +1 -1
  320. package/runtime/core/sorted.mjs +91 -108
  321. package/runtime/core/sorted.mjs.map +1 -1
  322. package/{stdlib/core/text.eli → runtime/core/text-impl.eli} +2 -2
  323. package/runtime/core/text-impl.mjs +1 -1
  324. package/runtime/core/text-impl.mjs.map +1 -1
  325. package/runtime/core/transducer.eli +24 -0
  326. package/runtime/core/transducer.mjs +27 -1
  327. package/runtime/core/transducer.mjs.map +1 -1
  328. package/runtime/core/transient-internals.eli +12 -4
  329. package/runtime/core/transient-internals.mjs +19 -6
  330. package/runtime/core/transient-internals.mjs.map +1 -1
  331. package/runtime/core/transient.eli +31 -0
  332. package/runtime/core/transient.mjs +33 -2
  333. package/runtime/core/transient.mjs.map +1 -1
  334. package/runtime/core/value-internals.mjs +29 -35
  335. package/runtime/core/value-internals.mjs.map +1 -1
  336. package/runtime/core/vector.eli +73 -20
  337. package/runtime/core/vector.mjs +159 -156
  338. package/runtime/core/vector.mjs.map +1 -1
  339. package/runtime/literals.eli +16 -9
  340. package/runtime/literals.mjs +25 -9
  341. package/runtime/literals.mjs.map +1 -1
  342. package/{stdlib → runtime/managed}/managed-literal-protocols.eli +100 -34
  343. package/{stdlib → runtime/managed}/managed-literals.eli +406 -81
  344. package/{dist/literal-sources/stdlib → runtime/managed}/managed-queue-state.eli +1 -1
  345. package/runtime/managed/managed-runtime-array-map.eli +45 -0
  346. package/{stdlib → runtime/managed}/managed-runtime-collection-internals.eli +22 -2
  347. package/{stdlib → runtime/managed}/managed-runtime-collection.eli +282 -23
  348. package/{stdlib → runtime/managed}/managed-runtime-data-text.eli +196 -13
  349. package/{stdlib → runtime/managed}/managed-runtime-data.eli +1 -1
  350. package/{stdlib → runtime/managed}/managed-runtime-identifier.eli +1 -1
  351. package/runtime/managed/managed-runtime-lazy-sequence.eli +935 -0
  352. package/{stdlib → runtime/managed}/managed-runtime-list.eli +1 -1
  353. package/{stdlib → runtime/managed}/managed-runtime-map.eli +1 -1
  354. package/{stdlib → runtime/managed}/managed-runtime-metadata-internals.eli +2 -2
  355. package/{stdlib → runtime/managed}/managed-runtime-metadata.eli +2 -2
  356. package/{stdlib → runtime/managed}/managed-runtime-object.eli +1 -1
  357. package/{stdlib → runtime/managed}/managed-runtime-order.eli +3 -3
  358. package/{stdlib → runtime/managed}/managed-runtime-persistent-kind.eli +1 -1
  359. package/{stdlib → runtime/managed}/managed-runtime-protocol.eli +5 -3
  360. package/{stdlib → runtime/managed}/managed-runtime-queue.eli +1 -1
  361. package/{stdlib → runtime/managed}/managed-runtime-record.eli +3 -3
  362. package/{stdlib → runtime/managed}/managed-runtime-regex.eli +2 -2
  363. package/{stdlib → runtime/managed}/managed-runtime-sequence.eli +1 -1
  364. package/{stdlib → runtime/managed}/managed-runtime-set-algebra.eli +1 -1
  365. package/{stdlib → runtime/managed}/managed-runtime-set.eli +1 -1
  366. package/{stdlib → runtime/managed}/managed-runtime-sorted.eli +4 -4
  367. package/{stdlib → runtime/managed}/managed-runtime-text.eli +1 -1
  368. package/{stdlib → runtime/managed}/managed-runtime-transducer.eli +24 -2
  369. package/{stdlib → runtime/managed}/managed-runtime-transient.eli +7 -5
  370. package/{stdlib → runtime/managed}/managed-runtime-type.eli +1 -1
  371. package/{stdlib → runtime/managed}/managed-runtime-value-internals.eli +3 -3
  372. package/{stdlib → runtime/managed}/managed-runtime-value.eli +2 -2
  373. package/{stdlib → runtime/managed}/managed-runtime-vector.eli +5 -2
  374. package/{dist/literal-sources/stdlib → runtime/managed}/managed-sorted-state.eli +1 -1
  375. package/runtime/managed/managed-transient-state.eli +1206 -0
  376. package/{dist/literal-sources/stdlib → runtime/managed}/managed-value-state.eli +30 -6
  377. package/{stdlib → runtime/managed}/managed-vector-view-state.eli +1 -1
  378. package/{stdlib → runtime/managed}/managed-wire-http-rpc.eli +1 -1
  379. package/{stdlib → runtime/managed}/managed-wire-markdown.eli +1 -1
  380. package/{stdlib → runtime/managed}/managed-wire-schema.eli +1 -1
  381. package/runtime/math.mjs +32 -38
  382. package/runtime/math.mjs.map +1 -1
  383. package/runtime/number-radix.mjs +5 -7
  384. package/runtime/number-radix.mjs.map +1 -1
  385. package/runtime/primitives.eli +19 -6
  386. package/runtime/primitives.mjs +81 -46
  387. package/runtime/primitives.mjs.map +1 -1
  388. package/runtime/source-mapping.mjs +17 -23
  389. package/runtime/source-mapping.mjs.map +1 -1
  390. package/runtime/testing/map.eli +70 -41
  391. package/runtime/testing/map.mjs +57 -34
  392. package/runtime/testing/map.mjs.map +1 -1
  393. package/runtime/unicode-case.mjs +6 -6
  394. package/runtime/unicode-case.mjs.map +1 -1
  395. package/runtime/wire/http-rpc.mjs +12 -18
  396. package/runtime/wire/http-rpc.mjs.map +1 -1
  397. package/runtime/wire/markdown.mjs +42 -42
  398. package/runtime/wire/markdown.mjs.map +1 -1
  399. package/runtime/wire/schema.mjs +13 -13
  400. package/runtime/wire/schema.mjs.map +1 -1
  401. package/runtime/worker-value-codec.mjs +5 -7
  402. package/runtime/worker-value-codec.mjs.map +1 -1
  403. package/runtime/worker-value-stream.mjs +128 -135
  404. package/runtime/worker-value-stream.mjs.map +1 -1
  405. package/runtime/worker.mjs +75 -89
  406. package/runtime/worker.mjs.map +1 -1
  407. package/stdlib/README.md +25 -18
  408. package/stdlib/async.eli +1245 -0
  409. package/stdlib/bit.eli +89 -1
  410. package/stdlib/bit.mjs +89 -2
  411. package/stdlib/bit.mjs.map +1 -1
  412. package/stdlib/{core/collection.eli → collection.eli} +39 -4
  413. package/stdlib/core.eli +956 -0
  414. package/stdlib/data-text.eli +62 -1091
  415. package/stdlib/data.eli +517 -37
  416. package/stdlib/devtools.eli +399 -0
  417. package/stdlib/diff.eli +134 -0
  418. package/stdlib/{function.eli → fn.eli} +1 -1
  419. package/stdlib/identifier.eli +21 -136
  420. package/stdlib/interop/object.eli +39 -0
  421. package/stdlib/{core/lazy-sequence.eli → lazy-sequence.eli} +28 -2
  422. package/stdlib/math.eli +267 -0
  423. package/stdlib/{core/metadata.eli → meta.eli} +2 -2
  424. package/stdlib/{core/order.eli → order.eli} +6 -6
  425. package/stdlib/persistent-list.eli +60 -21
  426. package/stdlib/persistent-map.eli +335 -324
  427. package/stdlib/persistent-set.eli +8 -6
  428. package/stdlib/persistent-vector.eli +18 -18
  429. package/stdlib/portable/data-text.eli +1092 -0
  430. package/stdlib/portable/data.eli +42 -0
  431. package/stdlib/portable/identifier.eli +147 -0
  432. package/stdlib/{metadata.eli → portable/metadata.eli} +5 -5
  433. package/stdlib/{object.eli → portable/object.eli} +1 -1
  434. package/stdlib/{sequence.eli → portable/sequence.eli} +1 -1
  435. package/stdlib/{text.eli → portable/text.eli} +1 -1
  436. package/stdlib/pprint.eli +588 -0
  437. package/stdlib/protocol.eli +63 -0
  438. package/stdlib/{core/record.eli → record.eli} +2 -2
  439. package/stdlib/regex.eli +70 -0
  440. package/stdlib/result.eli +53 -44
  441. package/stdlib/schema.eli +1886 -0
  442. package/stdlib/{core/seq.eli → seq.eli} +97 -90
  443. package/{dist/literal-sources/stdlib/core → stdlib}/set.eli +8 -8
  444. package/stdlib/{core/sorted.eli → sorted.eli} +2 -2
  445. package/stdlib/{state/atom.eli → state.eli} +137 -7
  446. package/stdlib/string.eli +362 -0
  447. package/stdlib/tap.eli +135 -0
  448. package/stdlib/test.eli +566 -0
  449. package/stdlib/time.eli +419 -0
  450. package/stdlib/{core/transducer.eli → transducer.eli} +4 -2
  451. package/stdlib/{core/transient.eli → transient.eli} +11 -5
  452. package/stdlib/{core/type.eli → type.eli} +2 -2
  453. package/stdlib/uuid.eli +288 -0
  454. package/stdlib/{core/walk.eli → walk.eli} +6 -6
  455. package/stdlib/{gepo.eli → wire/gepo.eli} +2 -2
  456. package/stdlib/{mdp.eli → wire/mdp.eli} +2 -2
  457. package/stdlib/{mds.eli → wire/mds.eli} +2 -2
  458. package/stdlib/{core/zipper.eli → zipper.eli} +179 -138
  459. package/tests/helpers/wasm-managed-math-node.mjs +495 -582
  460. package/tools/acceptance/check-ground.mjs +106 -132
  461. package/tools/acceptance/check.mjs +16 -16
  462. package/tools/acceptance/retained-performance-snapshot.mjs +265 -149
  463. package/tools/acceptance/retained-soak.mjs +5 -7
  464. package/tools/acceptance/retained-transient.mjs +5 -7
  465. package/tools/backends/build.mjs +373 -95
  466. package/tools/backends/compiler-bootstrap.mjs +271 -317
  467. package/tools/backends/compiler-browser.mjs +7 -9
  468. package/tools/backends/compiler-build.mjs +77 -97
  469. package/tools/backends/compiler-cli-ground.mjs +47 -47
  470. package/tools/backends/compiler-data-transport.mjs +27 -31
  471. package/tools/backends/compiler-host-iteration.mjs +13 -21
  472. package/tools/backends/compiler-host-record.mjs +28 -34
  473. package/tools/backends/compiler-metadata.mjs +145 -53
  474. package/tools/backends/compiler-package-ground.mjs +39 -47
  475. package/tools/backends/compiler-package-loader.mjs +164 -206
  476. package/tools/backends/compiler-session.mjs +432 -324
  477. package/tools/backends/compiler-worker.mjs +79 -89
  478. package/tools/backends/dist/bootstrap-js-stages.mjs +17 -19
  479. package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -1
  480. package/tools/backends/dist/managed-ir-inventory.mjs +1 -1
  481. package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -1
  482. package/tools/backends/dist/release-cli.mjs +151 -22
  483. package/tools/backends/dist/release-cli.mjs.map +1 -1
  484. package/tools/backends/dist/run-artifact.mjs +54 -76
  485. package/tools/backends/dist/run-artifact.mjs.map +1 -1
  486. package/tools/backends/managed-foreign-binding.mjs +370 -114
  487. package/tools/backends/managed-host-ground.mjs +14 -17
  488. package/tools/backends/managed-host-lifecycle.mjs +42 -53
  489. package/tools/backends/managed-host-object-realm.mjs +121 -111
  490. package/tools/backends/managed-host-proxy.mjs +5 -7
  491. package/tools/backends/managed-host-runtime.mjs +6295 -4350
  492. package/tools/backends/managed-host.mjs +234 -188
  493. package/tools/backends/managed-layout.mjs +53 -56
  494. package/tools/backends/managed-module-graph-ground.mjs +488 -568
  495. package/tools/backends/managed-module-graph.mjs +62 -76
  496. package/tools/backends/managed-runtime-host-ground.mjs +352 -400
  497. package/tools/backends/managed-runtime.mjs +227 -266
  498. package/tools/backends/managed-source-view-ground.mjs +165 -161
  499. package/tools/backends/managed-source-view.mjs +415 -154
  500. package/tools/backends/native-managed-runtime.mjs +96 -138
  501. package/tools/backends/native-memory.mjs +1122 -742
  502. package/tools/backends/native-node-ffi.mjs +2439 -0
  503. package/tools/backends/native-node-memory.mjs +793 -383
  504. package/tools/backends/native-node-runner.mjs +1458 -579
  505. package/tools/backends/native-node-watchdog.mjs +12 -14
  506. package/tools/backends/native-node-worker.mjs +46 -50
  507. package/tools/backends/stage3-targets.mjs +2 -2
  508. package/tools/bootstrap-runtime.el +4 -4
  509. package/tools/collections/transient-builder-benchmark.mjs +17 -17
  510. package/tools/project/check-cli.eli +27 -3
  511. package/tools/project/project-cli.eli +1 -1
  512. package/tools/testing/maintained-suite-source.mjs +5 -7
  513. package/tools/testing/maintained-suite-source.mjs.map +1 -1
  514. package/dist/literal-sources/stdlib/core/data.eli +0 -306
  515. package/dist/literal-sources/stdlib/managed-transient-state.eli +0 -353
  516. package/stdlib/core/data-text.eli +0 -16
  517. package/stdlib/core/data.eli +0 -306
  518. package/stdlib/core/identifier.eli +0 -32
  519. package/stdlib/core/regex.eli +0 -17
  520. package/stdlib/deferred.eli +0 -70
  521. package/stdlib/managed-runtime-lazy-sequence.eli +0 -157
  522. package/stdlib/managed-transient-state.eli +0 -353
  523. /package/{stdlib → runtime/managed}/managed-protocol-error.eli +0 -0
  524. /package/{stdlib → runtime/managed}/managed-runtime-literals.eli +0 -0
  525. /package/{stdlib → runtime/managed}/managed-runtime-truth.eli +0 -0
  526. /package/{stdlib → runtime/managed}/managed-testing-value.eli +0 -0
@@ -1,11 +1,11 @@
1
1
  (module eliscript.backend.native
2
2
  (import "./optimize.mjs" optimize-program)
3
- (import "./numeric.mjs" lower-numeric-program optimize-numeric-program numeric-pure backend-fail find-function)
4
- (import "./sha256.mjs" sha256-bytes sha256-words-le)
3
+ (import "./numeric.mjs" lower-numeric-program optimize-numeric-program numeric-pure backend-fail find-function runtime-body-cache-get)
4
+ (import "./sha256.mjs" sha256-bytes sha256-words-le sha256-string-utf16le)
5
5
  (import "./memory.mjs" lower-memory-program)
6
6
  (import "./elf.mjs" emit-elf-object emit-elf-library)
7
7
  (import "./capabilities.mjs" prepare-host-capabilities prepare-fresh-host-capabilities host-capability-index)
8
- (import "./binary.mjs" binary-output binary-byte binary-u32 binary-append binary-append-owned binary-slice binary-finish)
8
+ (import "./binary.mjs" binary-output binary-byte binary-u32 binary-append binary-append-owned binary-slice binary-to-array binary-finish)
9
9
 
10
10
  ;; ARM64 numeric-f64 instruction and Mach-O image emission. Selection,
11
11
  ;; encoding, linking metadata and ad-hoc page signatures are independent.
@@ -13,7 +13,7 @@
13
13
  ;; block. Relocations may revisit any block, but never unpack the image.
14
14
  ;; Numeric assembly helpers keep their existing ordinary-array contract.
15
15
  (defun native-word-buffer ()
16
- (js-object :length 0 :chunks (js-array) :cache (js-array) :units (js-array) :cacheIndex -1 :dirty false))
16
+ (js-object :length 0 :chunks (js-array) :cache (js-array) :units (js-array) :cacheIndex -1 :dirty false :hasHole false))
17
17
 
18
18
  (defun native-word-flush (words)
19
19
  (when (get words :dirty)
@@ -73,6 +73,8 @@
73
73
  (progn
74
74
  (unless (and (Number/isSafeInteger length) (>= length 0))
75
75
  (backend-fail nil "native word buffer length is invalid"))
76
+ (when (> length (js-length words))
77
+ (put words :hasHole t))
76
78
  (when (< length (js-length words))
77
79
  (let ((block (js-call globalThis.Math :floor (/ length 1024))) (tail (% length 1024))
78
80
  (cached (get words :cacheIndex)) (chunks (get words :chunks)))
@@ -90,7 +92,32 @@
90
92
  (defun native-word-push (words value)
91
93
  (if (Array/isArray words) (js-call words :push (uint32 value))
92
94
  (let ((index (js-length words)))
93
- (native-word-resize words (1+ index)) (native-word-set words index value) (js-length words))))
95
+ (put words :length (1+ index))
96
+ (native-word-set words index value)
97
+ (js-length words))))
98
+
99
+ (defun native-word-append-u32 (words u32arr)
100
+ (let ((len (js-length u32arr)) (src 0))
101
+ (if (Array/isArray words)
102
+ (while (< src len)
103
+ (js-call words :push (uint32 (aref u32arr src)))
104
+ (setq src (1+ src)))
105
+ (let ((pos (js-length words)))
106
+ (while (< src len)
107
+ (let* ((block (js-call globalThis.Math :floor (/ pos 1024)))
108
+ (offset (% pos 1024))
109
+ (room (- 1024 offset))
110
+ (take (if (< (- len src) room) (- len src) room))
111
+ (cache (native-word-cache words block))
112
+ (end (+ offset take))
113
+ (dst offset))
114
+ (while (< dst end)
115
+ (put cache dst (aref u32arr src))
116
+ (setq dst (1+ dst) src (1+ src)))
117
+ (put words :dirty t)
118
+ (setq pos (+ pos take))))
119
+ (put words :length pos)))
120
+ (js-length words)))
94
121
 
95
122
  (defun word (state value)
96
123
  (native-word-push (get state :words) value))
@@ -316,14 +343,20 @@
316
343
  (data (if (nullish? operand) 0 operand)))
317
344
  (if (<= register -2)
318
345
  (let ((physical (- (- register) 2)))
319
- (word state (+ (if wide 2852127712 704644064) (if store physical (* physical 65536)))))
346
+ (word state (+ (if wide 2852127712 704644064)
347
+ (if store physical data)
348
+ (* (if store data physical) 65536))))
320
349
  (if (>= register 0)
321
350
  (word state (if (= type "f64")
322
351
  (+ 509624320 (if store register (* register 32)))
323
352
  (if wide
324
353
  (if store (+ 2657550336 register) (+ 2657484800 (* register 32)))
325
354
  (if store (+ 505872384 register) (+ 505806848 (* register 32))))))
326
- (let ((offset (* (1+ index) 8)))
355
+ (let* ((local-offsets (get state :localOffsets nil))
356
+ (offset (if (and (get state :memory nil) local-offsets (< index (js-length local-offsets))
357
+ (> (js-nth index local-offsets) 0))
358
+ (js-nth index local-offsets)
359
+ (* (1+ index) 8))))
327
360
  (if (and (get state :memory nil) (<= offset 256))
328
361
  ;; LDUR/STUR use a signed nine-bit byte offset from FP (x29).
329
362
  ;; Rt is the low 5 bits; 31 is WZR/XZR.
@@ -351,6 +384,17 @@
351
384
  (setq i (1+ i)))
352
385
  call))
353
386
 
387
+ (defun memory-register-weights (node counts weight)
388
+ (let* ((kind (get node :kind)) (children (get node :children)) (i 0)
389
+ (loop (or (= kind "while") (= kind "remainder-while") (= kind "guarded-while")))
390
+ (child-weight (if loop (Math/min 1024 (* weight 8)) weight)))
391
+ (when (or (= kind "local") (= kind "set") (= kind "initialize"))
392
+ (let ((index (get node :value)))
393
+ (js-call counts :splice index 1 (+ (js-nth index counts) weight))))
394
+ (while (< i (js-length children))
395
+ (memory-register-weights (js-nth i children) counts child-weight)
396
+ (setq i (1+ i)))))
397
+
354
398
  (defun live-union (left right)
355
399
  (let ((result (js-call left :slice)) (i 0))
356
400
  (while (< i (js-length right))
@@ -426,6 +470,220 @@
426
470
  (assign-register-class counts locals registers integers "i32" graph)
427
471
  (js-object :registers registers :leaf t)))
428
472
 
473
+ (defun memory-bitset-copy (bits)
474
+ (js-call bits :slice))
475
+
476
+ (defun memory-bitset-union (left right words)
477
+ (let ((out (js-call left :slice)) (w 0))
478
+ (while (< w words)
479
+ (put out w (bit-or (aref left w) (aref right w)))
480
+ (setq w (1+ w)))
481
+ out))
482
+
483
+ (defun memory-bitset-disjoint-p (left right words)
484
+ (let ((w 0) (disjoint t))
485
+ (while (and disjoint (< w words))
486
+ (when (/= (bit-and (aref left w) (aref right w)) 0)
487
+ (setq disjoint nil))
488
+ (setq w (1+ w)))
489
+ disjoint))
490
+
491
+ (defun memory-bitset-interfere (graph index live words)
492
+ (let ((idx-word (unsigned-bit-shift-right index 5))
493
+ (idx-bit (bit-shift-left 1 (bit-and index 31)))
494
+ (row (js-nth index graph))
495
+ (w 0))
496
+ (while (< w words)
497
+ (let* ((lw (aref live w))
498
+ (masked (if (= w idx-word) (bit-and lw (bit-not idx-bit)) lw)))
499
+ (when (/= masked 0)
500
+ (put row w (bit-or (aref row w) masked))
501
+ (let ((rem (bit-or masked 0)) (base-idx (* w 32)))
502
+ (while (/= rem 0)
503
+ (let* ((lsb (bit-and rem (- rem)))
504
+ (bit (- 31 (Math/clz32 lsb)))
505
+ (other (+ base-idx bit))
506
+ (orow (js-nth other graph)))
507
+ (put orow idx-word (bit-or (aref orow idx-word) idx-bit))
508
+ (setq rem (bit-xor rem lsb)))))))
509
+ (setq w (1+ w)))))
510
+
511
+ (defun memory-addr-base-local (node)
512
+ (let ((kind (get node :kind)) (val (get node :value)) (children (get node :children)))
513
+ (cond
514
+ ((and (= kind "local") (memory-word-type (get node :type))) val)
515
+ ((and (= kind "word-arithmetic") (= val "1+") (= (js-length children) 1)
516
+ (memory-word-type (get node :type))
517
+ (= (get (js-nth 0 children) :kind) "local")
518
+ (memory-word-type (get (js-nth 0 children) :type)))
519
+ (get (js-nth 0 children) :value))
520
+ ((and (= kind "word-arithmetic") (= val "+") (= (js-length children) 2)
521
+ (memory-word-type (get node :type)))
522
+ (let ((left (js-nth 0 children)) (right (js-nth 1 children)))
523
+ (cond
524
+ ((and (= (get left :kind) "local") (memory-word-type (get left :type)) (>= (memory-word-imm12 right) 0))
525
+ (get left :value))
526
+ ((and (= (get right :kind) "local") (memory-word-type (get right :type)) (>= (memory-word-imm12 left) 0))
527
+ (get right :value))
528
+ (t -1))))
529
+ (t -1))))
530
+
531
+ (defun memory-live-before (node after graph words)
532
+ (let ((kind (get node :kind)) (children (get node :children)) (value (get node :value))
533
+ (live (memory-bitset-copy after)))
534
+ (cond
535
+ ((= kind "local")
536
+ (memory-bitset-interfere graph value live words)
537
+ (let ((w (unsigned-bit-shift-right value 5)) (b (bit-shift-left 1 (bit-and value 31))))
538
+ (put live w (bit-or (aref live w) b))))
539
+ ((or (= kind "set") (= kind "initialize"))
540
+ (memory-bitset-interfere graph value live words)
541
+ (let ((w (unsigned-bit-shift-right value 5)) (b (bit-shift-left 1 (bit-and value 31))))
542
+ (put live w (bit-and (aref live w) (bit-not b))))
543
+ (setq live (memory-live-before (js-nth 0 children) live graph words)))
544
+ ((= kind "if")
545
+ (let ((t-live (memory-live-before (js-nth 1 children) live graph words))
546
+ (e-live (memory-live-before (js-nth 2 children) live graph words)))
547
+ (setq live (memory-live-before (js-nth 0 children) (memory-bitset-union t-live e-live words) graph words))))
548
+ ((or (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
549
+ (let ((changed t))
550
+ (while changed
551
+ (setq changed nil)
552
+ (let* ((body-in (memory-live-before (js-nth 1 children) live graph words))
553
+ (cond-after (memory-bitset-union after body-in words))
554
+ (next (memory-live-before (js-nth 0 children) cond-after graph words))
555
+ (w 0))
556
+ (while (< w words)
557
+ (let* ((lw (aref live w)) (nw (bit-or lw (aref next w))))
558
+ (when (/= (bit-and nw (bit-not lw)) 0)
559
+ (put live w nw)
560
+ (setq changed t)))
561
+ (setq w (1+ w)))))))
562
+ ((and (= kind "memory-op") (= (js-length children) 2) (> (memory-width value) 0))
563
+ (let* ((c0 (js-nth 0 children))
564
+ (addr-idx (memory-addr-base-local c0)))
565
+ (when (>= addr-idx 0)
566
+ (let ((aw (unsigned-bit-shift-right addr-idx 5))
567
+ (ab (bit-shift-left 1 (bit-and addr-idx 31))))
568
+ (put live aw (bit-or (aref live aw) ab))
569
+ (memory-bitset-interfere graph addr-idx live words)))
570
+ (setq live (memory-live-before (js-nth 1 children) live graph words))
571
+ (setq live (memory-live-before c0 live graph words))))
572
+ ((and (= (js-length children) 2)
573
+ (or (= kind "word-compare") (= kind "v64-compare") (= kind "word-arithmetic") (= kind "memory-op"))
574
+ (= (get (js-nth 0 children) :kind) "local")
575
+ (not (memory-expr-writes-local-p (js-nth 1 children) (get (js-nth 0 children) :value))))
576
+ (let* ((c0 (js-nth 0 children))
577
+ (lidx (get c0 :value))
578
+ (lw (unsigned-bit-shift-right lidx 5))
579
+ (lb (bit-shift-left 1 (bit-and lidx 31))))
580
+ (put live lw (bit-or (aref live lw) lb))
581
+ (memory-bitset-interfere graph lidx live words)
582
+ (setq live (memory-live-before (js-nth 1 children) live graph words))
583
+ (setq live (memory-live-before c0 live graph words))))
584
+ (t
585
+ (let ((i (1- (js-length children)))
586
+ (short (or (= kind "and") (= kind "or"))))
587
+ (while (>= i 0)
588
+ (setq live (memory-live-before (js-nth i children)
589
+ (if short (memory-bitset-union after live words) live) graph words))
590
+ (setq i (1- i))))))
591
+ live))
592
+
593
+ (defun allocate-memory-registers (body locals &optional pin-memory arity)
594
+ ;; Liveness-based graph coloring + weight ordering: hot loop locals win
595
+ ;; callee-saved registers x20-x28, non-interfering lifetimes share
596
+ ;; registers and stack spill slots, and definitely-initialized locals skip
597
+ ;; redundant entry zero-initialization.
598
+ (let ((count (js-length locals)))
599
+ (if (= count 0)
600
+ (js-object :registers (js-array) :saved 0 :offsets (js-array) :spilled 0 :noInit (js-array))
601
+ (let* ((counts (js-call (new globalThis.Array count) :fill 0))
602
+ (registers (js-call (new globalThis.Array count) :fill -1))
603
+ (offsets (js-call (new globalThis.Array count) :fill 0))
604
+ (no-init (js-call (new globalThis.Array count) :fill false))
605
+ (words (Math/max 1 (Math/ceil (/ count 32))))
606
+ (graph (new globalThis.Array count))
607
+ (param-count (if (nullish? arity) 0 arity))
608
+ (candidates (js-array))
609
+ (index 0))
610
+ (while (< index count)
611
+ (put graph index (new globalThis.Uint32Array words))
612
+ (setq index (1+ index)))
613
+ (memory-register-weights body counts 1)
614
+ (let* ((empty (new globalThis.Uint32Array words))
615
+ (entry-live (memory-live-before body empty graph words))
616
+ (entry-mask (memory-bitset-copy entry-live)))
617
+ (setq index 0)
618
+ (while (< index param-count)
619
+ (let ((w (unsigned-bit-shift-right index 5))
620
+ (b (bit-shift-left 1 (bit-and index 31))))
621
+ (put entry-mask w (bit-or (aref entry-mask w) b)))
622
+ (setq index (1+ index)))
623
+ (setq index 0)
624
+ (while (< index count)
625
+ (when (/= (bit-and (aref entry-mask (unsigned-bit-shift-right index 5))
626
+ (bit-shift-left 1 (bit-and index 31))) 0)
627
+ (memory-bitset-interfere graph index entry-mask words))
628
+ (when (and (>= index param-count)
629
+ (= (bit-and (aref entry-live (unsigned-bit-shift-right index 5))
630
+ (bit-shift-left 1 (bit-and index 31))) 0))
631
+ (put no-init index t))
632
+ (let ((type (get (js-nth index locals) :type)))
633
+ (when (and (> (js-nth index counts) 1)
634
+ (or (memory-word-type type) (memory-wide-type type)))
635
+ (js-call candidates :push index)))
636
+ (setq index (1+ index)))
637
+ (js-call candidates :sort (lambda (left right)
638
+ (- (js-nth right counts) (js-nth left counts))))
639
+ (let* ((max-regs (if pin-memory 7 9))
640
+ (reg-masks (new globalThis.Array max-regs))
641
+ (max-reg -1)
642
+ (c-len (js-length candidates))
643
+ (ci 0))
644
+ (setq index 0)
645
+ (while (< index max-regs)
646
+ (put reg-masks index (new globalThis.Uint32Array words))
647
+ (setq index (1+ index)))
648
+ (while (< ci c-len)
649
+ (let ((cand (js-nth ci candidates)) (r 0) (placed nil))
650
+ (while (and (not placed) (< r max-regs))
651
+ (let ((rm (js-nth r reg-masks)))
652
+ (when (memory-bitset-disjoint-p (js-nth cand graph) rm words)
653
+ (put registers cand (- -22 r))
654
+ (let ((w (unsigned-bit-shift-right cand 5))
655
+ (b (bit-shift-left 1 (bit-and cand 31))))
656
+ (put rm w (bit-or (aref rm w) b)))
657
+ (when (> r max-reg) (setq max-reg r))
658
+ (setq placed t)))
659
+ (setq r (1+ r))))
660
+ (setq ci (1+ ci)))
661
+ (let ((slot-masks (js-array)))
662
+ (setq index 0)
663
+ (while (< index count)
664
+ (when (and (= (js-nth index registers) -1)
665
+ (or (< index param-count) (> (js-nth index counts) 0)))
666
+ (let ((s 0) (s-len (js-length slot-masks)) (placed nil)
667
+ (irow (js-nth index graph)))
668
+ (while (and (not placed) (< s s-len))
669
+ (let ((sm (js-nth s slot-masks)))
670
+ (when (memory-bitset-disjoint-p irow sm words)
671
+ (let ((w (unsigned-bit-shift-right index 5))
672
+ (b (bit-shift-left 1 (bit-and index 31))))
673
+ (put sm w (bit-or (aref sm w) b)))
674
+ (put offsets index (* (1+ s) 8))
675
+ (setq placed t)))
676
+ (setq s (1+ s)))
677
+ (unless placed
678
+ (let ((sm (new globalThis.Uint32Array words)))
679
+ (put sm (unsigned-bit-shift-right index 5)
680
+ (bit-shift-left 1 (bit-and index 31)))
681
+ (js-call slot-masks :push sm)
682
+ (put offsets index (* (js-length slot-masks) 8))))))
683
+ (setq index (1+ index)))
684
+ (js-object :registers registers :saved (if (>= max-reg 0) (1+ max-reg) 0)
685
+ :offsets offsets :spilled (js-length slot-masks) :noInit no-init))))))))
686
+
429
687
  (defun assign-register-class (counts locals registers available type graph)
430
688
  (let ((i 0) (count (js-length locals)) (processed (js-array)))
431
689
  (while (< i count)
@@ -760,75 +1018,106 @@
760
1018
  (long-jump state (- target at 1) nil)))))))
761
1019
 
762
1020
  (defun finish-branches (state offsets)
763
- (let ((branches (get state :branches (js-array))) (changed t))
764
- (js-call branches :sort (lambda (left right) (- (get left :at) (get right :at))))
765
- ;; Sizes only grow. Revisit all labels after each growth, including branches
766
- ;; which were in range before another expansion shifted their destination.
767
- (while changed
768
- (setq changed false)
769
- (let ((growth 0))
770
- (js-call branches :forEach (lambda (entry)
771
- (put entry :before growth) (setq growth (+ growth (1- (get entry :size)))))))
772
- (js-call branches :forEach (lambda (entry)
773
- (let* ((at (branch-position branches (get entry :at)))
774
- (target (branch-position branches (get entry :target)))
775
- (kind (get entry :kind)) (opcode (get entry :opcode))
776
- (always (and (= kind 1) (>= (bit-and opcode 15) 14)))
777
- (size (if (branch-fits (- target at) (branch-limit kind)) 1
778
- (if (or (= kind 0) always)
779
- (if (branch-fits (- target at) 33554432) 1 7)
780
- (if (branch-fits (- target at 1) 33554432) 2 8)))))
781
- (when (> size (get entry :size)) (put entry :size size) (setq changed t))))))
782
- (let* ((words (get state :words)) (original-size (js-length words))
783
- (count (js-length branches))
784
- (last (if (> count 0) (js-nth (1- count) branches) nil))
785
- (final-size (+ original-size (if last (+ (get last :before) (1- (get last :size))) 0)))
786
- (scratch (js-array)) (out (js-object :words scratch :compact nil))
787
- (index (1- original-size)) (next (1- count)) (write (1- final-size)))
788
- ;; Sizes have converged and only grow. Every destination is at or above
789
- ;; its old position, so reverse traversal preserves the unread prefix.
790
- ;; Reuse at most eight words for one relaxed branch, never a code copy.
791
- (native-word-resize words final-size)
792
- (while (>= index 0)
793
- (let ((entry (if (>= next 0) (js-nth next branches) nil)))
794
- (if (and entry (= (get entry :at) index))
795
- (let ((at (- write (1- (get entry :size))))
796
- (target (branch-position branches (get entry :target))) (j 0))
797
- (put scratch :length 0)
798
- (relaxed-branch out entry at target)
799
- (setq j (1- (js-length scratch)))
800
- (while (>= j 0)
801
- (let ((value (js-nth j scratch)))
802
- ;; patch-jump already wrote every fitting branch. When no
803
- ;; earlier relaxation moved it, do not repack that same
804
- ;; immutable word block merely to write the same opcode.
805
- (unless (and (= write index) (= (native-word-at words index) value))
806
- (native-word-set words write value)))
807
- (setq write (1- write)) (setq j (1- j)))
808
- (setq next (1- next)))
809
- (let ((value (native-word-at words index)))
810
- ;; Still read every source word in the original reverse order,
811
- ;; including hole validation. Only moved words need a write.
812
- (unless (= write index) (native-word-set words write value))
813
- (setq write (1- write)))))
814
- (setq index (1- index)))
815
- (setq index 0)
816
- (while (< index (js-length offsets))
817
- (put offsets index (* (branch-position branches (/ (js-nth index offsets) 4)) 4))
818
- (setq index (1+ index)))
819
- (js-call (get state :constants (js-array)) :forEach (lambda (entry)
820
- (let ((uses (get entry :uses)) (i 0))
821
- (while (< i (js-length uses)) (put uses i (branch-position branches (js-nth i uses))) (setq i (1+ i))))))
822
- (let ((uses (get state :tableUses (js-array))) (i 0))
823
- (while (< i (js-length uses)) (put uses i (branch-position branches (js-nth i uses))) (setq i (1+ i))))
824
- (let ((ranges (get state :debugRanges nil)) (i 0))
825
- (when ranges
826
- (while (< i (js-length ranges))
827
- (let ((range (js-nth i ranges)))
828
- (put range :start (* (branch-position branches (/ (get range :start) 4)) 4))
829
- (put range :end (* (branch-position branches (/ (get range :end) 4)) 4)))
830
- (setq i (1+ i)))))
831
- (put state :branches (js-array)))))
1021
+ (let* ((branches (get state :branches (js-array)))
1022
+ (branch-count (js-length branches))
1023
+ (fast t)
1024
+ (k 0))
1025
+ (while (and fast (< k branch-count))
1026
+ (let ((entry (js-nth k branches)))
1027
+ (unless (branch-fits (- (get entry :target) (get entry :at)) (branch-limit (get entry :kind)))
1028
+ (setq fast nil)))
1029
+ (setq k (1+ k)))
1030
+ (if (and fast (or (Array/isArray (get state :words)) (not (get (get state :words) :hasHole nil))))
1031
+ (progn
1032
+ (put state :branches (js-array))
1033
+ (put state :cachedBranches (js-array)))
1034
+ (let ((changed t)
1035
+ (cached-groups (get state :cachedBranches (js-array)))
1036
+ (gi 0))
1037
+ (while (< gi (js-length cached-groups))
1038
+ (let* ((group (js-nth gi cached-groups))
1039
+ (base (get group :base))
1040
+ (arr (get group :branches))
1041
+ (bi 0)
1042
+ (blen (js-length arr)))
1043
+ (while (< bi blen)
1044
+ (js-call branches :push (js-object
1045
+ :at (+ base (aref arr bi))
1046
+ :target (+ base (aref arr (1+ bi)))
1047
+ :opcode (aref arr (+ bi 2))
1048
+ :kind (aref arr (+ bi 3))
1049
+ :size 1))
1050
+ (setq bi (+ bi 4))))
1051
+ (setq gi (1+ gi)))
1052
+ (put state :cachedBranches (js-array))
1053
+ (js-call branches :sort (lambda (left right) (- (get left :at) (get right :at))))
1054
+ ;; Sizes only grow. Revisit all labels after each growth, including branches
1055
+ ;; which were in range before another expansion shifted their destination.
1056
+ (while changed
1057
+ (setq changed false)
1058
+ (let ((growth 0))
1059
+ (js-call branches :forEach (lambda (entry)
1060
+ (put entry :before growth) (setq growth (+ growth (1- (get entry :size)))))))
1061
+ (js-call branches :forEach (lambda (entry)
1062
+ (let* ((at (branch-position branches (get entry :at)))
1063
+ (target (branch-position branches (get entry :target)))
1064
+ (kind (get entry :kind)) (opcode (get entry :opcode))
1065
+ (always (and (= kind 1) (>= (bit-and opcode 15) 14)))
1066
+ (size (if (branch-fits (- target at) (branch-limit kind)) 1
1067
+ (if (or (= kind 0) always)
1068
+ (if (branch-fits (- target at) 33554432) 1 7)
1069
+ (if (branch-fits (- target at 1) 33554432) 2 8)))))
1070
+ (when (> size (get entry :size)) (put entry :size size) (setq changed t))))))
1071
+ (let* ((words (get state :words)) (original-size (js-length words))
1072
+ (count (js-length branches))
1073
+ (last (if (> count 0) (js-nth (1- count) branches) nil))
1074
+ (final-size (+ original-size (if last (+ (get last :before) (1- (get last :size))) 0)))
1075
+ (scratch (js-array)) (out (js-object :words scratch :compact nil))
1076
+ (index (1- original-size)) (next (1- count)) (write (1- final-size)))
1077
+ ;; Sizes have converged and only grow. Every destination is at or above
1078
+ ;; its old position, so reverse traversal preserves the unread prefix.
1079
+ ;; Reuse at most eight words for one relaxed branch, never a code copy.
1080
+ (native-word-resize words final-size)
1081
+ (while (>= index 0)
1082
+ (let ((entry (if (>= next 0) (js-nth next branches) nil)))
1083
+ (if (and entry (= (get entry :at) index))
1084
+ (let ((at (- write (1- (get entry :size))))
1085
+ (target (branch-position branches (get entry :target))) (j 0))
1086
+ (put scratch :length 0)
1087
+ (relaxed-branch out entry at target)
1088
+ (setq j (1- (js-length scratch)))
1089
+ (while (>= j 0)
1090
+ (let ((value (js-nth j scratch)))
1091
+ ;; patch-jump already wrote every fitting branch. When no
1092
+ ;; earlier relaxation moved it, do not repack that same
1093
+ ;; immutable word block merely to write the same opcode.
1094
+ (unless (and (= write index) (= (native-word-at words index) value))
1095
+ (native-word-set words write value)))
1096
+ (setq write (1- write)) (setq j (1- j)))
1097
+ (setq next (1- next)))
1098
+ (let ((value (native-word-at words index)))
1099
+ ;; Still read every source word in the original reverse order,
1100
+ ;; including hole validation. Only moved words need a write.
1101
+ (unless (= write index) (native-word-set words write value))
1102
+ (setq write (1- write)))))
1103
+ (setq index (1- index)))
1104
+ (setq index 0)
1105
+ (while (< index (js-length offsets))
1106
+ (put offsets index (* (branch-position branches (/ (js-nth index offsets) 4)) 4))
1107
+ (setq index (1+ index)))
1108
+ (js-call (get state :constants (js-array)) :forEach (lambda (entry)
1109
+ (let ((uses (get entry :uses)) (i 0))
1110
+ (while (< i (js-length uses)) (put uses i (branch-position branches (js-nth i uses))) (setq i (1+ i))))))
1111
+ (let ((uses (get state :tableUses (js-array))) (i 0))
1112
+ (while (< i (js-length uses)) (put uses i (branch-position branches (js-nth i uses))) (setq i (1+ i))))
1113
+ (let ((ranges (get state :debugRanges nil)) (i 0))
1114
+ (when ranges
1115
+ (while (< i (js-length ranges))
1116
+ (let ((range (js-nth i ranges)))
1117
+ (put range :start (* (branch-position branches (/ (get range :start) 4)) 4))
1118
+ (put range :end (* (branch-position branches (/ (get range :end) 4)) 4)))
1119
+ (setq i (1+ i)))))
1120
+ (put state :branches (js-array)))))))
832
1121
 
833
1122
  (defun condition (state node)
834
1123
  (expression state node)
@@ -941,6 +1230,227 @@
941
1230
  (stack-adjust state (* count 16) nil)
942
1231
  (put state :leaf leaf) (put state :depth depth)))
943
1232
 
1233
+ (defun memory-word-imm12 (node)
1234
+ (let ((kind (get node :kind)) (value (get node :value)))
1235
+ (if (and (memory-word-type (get node :type))
1236
+ (or (= kind "literal") (= kind "int-literal"))
1237
+ (= (value-type value) "number")
1238
+ (Number/isInteger value)
1239
+ (>= value 0)
1240
+ (<= value 4095))
1241
+ value -1)))
1242
+
1243
+ (defun memory-word-compare-code (name)
1244
+ (cond ((= name "=") 0) ((= name "/=") 1) ((= name "<") 3)
1245
+ ((= name "<=") 9) ((= name ">") 8) (t 2)))
1246
+
1247
+ (defun memory-wide-imm12 (node)
1248
+ (when (and (memory-wide-type (get node :type)) (= (get node :kind) "literal"))
1249
+ (let ((value (get node :value)))
1250
+ (when (and (not (nil? value)) (= (value-type value) "object")
1251
+ (= (get value :high) 0)
1252
+ (>= (get value :low) 0) (<= (get value :low) 4095))
1253
+ (get value :low)))))
1254
+
1255
+ (defun memory-word-pow2-shift (node)
1256
+ (let ((kind (get node :kind)) (value (get node :value)))
1257
+ (if (and (memory-word-type (get node :type))
1258
+ (or (= kind "literal") (= kind "int-literal"))
1259
+ (= (value-type value) "number")
1260
+ (Number/isInteger value)
1261
+ (> value 1)
1262
+ (<= value 1073741824)
1263
+ (= (bit-and value (1- value)) 0))
1264
+ (let ((k 0) (v value))
1265
+ (while (> v 1) (setq v (unsigned-bit-shift-right v 1) k (1+ k)))
1266
+ k)
1267
+ 0)))
1268
+
1269
+ (defun memory-dynamic-ref-local (children is-or)
1270
+ (when (= (js-length children) 2)
1271
+ (let* ((c0 (js-nth 0 children)) (c1 (js-nth 1 children)))
1272
+ (when (and (= (get c0 :kind) "word-compare") (= (get c1 :kind) "word-compare")
1273
+ (= (get c0 :value) (if is-or "/=" "="))
1274
+ (= (get c1 :value) (if is-or ">" "<=")))
1275
+ (let* ((c0-ch (get c0 :children)) (c1-ch (get c1 :children))
1276
+ (c0-0 (js-nth 0 c0-ch)) (c0-1 (js-nth 1 c0-ch))
1277
+ (c1-0 (js-nth 0 c1-ch)) (c1-1 (js-nth 1 c1-ch)))
1278
+ (when (and (= (get c0-0 :kind) "memory-op") (= (get c0-0 :value) "v64-high")
1279
+ (= (memory-word-imm12 c0-1) 0)
1280
+ (= (get c1-0 :kind) "memory-op") (= (get c1-0 :value) "v64-low")
1281
+ (= (memory-word-imm12 c1-1) 3))
1282
+ (let ((arg0 (js-nth 0 (get c0-0 :children)))
1283
+ (arg1 (js-nth 0 (get c1-0 :children))))
1284
+ (when (and (= (get arg0 :kind) "local") (= (get arg1 :kind) "local")
1285
+ (= (get arg0 :value) (get arg1 :value)))
1286
+ arg0))))))))
1287
+
1288
+ (defun memory-word-lit-val (node)
1289
+ (let ((kind (get node :kind)) (value (get node :value)))
1290
+ (if (and (memory-word-type (get node :type))
1291
+ (or (= kind "literal") (= kind "int-literal"))
1292
+ (= (value-type value) "number")
1293
+ (Number/isInteger value))
1294
+ value -1)))
1295
+
1296
+ (defun memory-bool01-p (node)
1297
+ (let ((kind (get node :kind)) (children (get node :children)))
1298
+ (cond
1299
+ ((or (= kind "word-compare") (= kind "v64-compare"))
1300
+ (= (js-length children) 2))
1301
+ ((= kind "not") (= (js-length children) 1))
1302
+ ((= kind "if")
1303
+ (and (= (js-length children) 3)
1304
+ (let ((tv (memory-word-lit-val (js-nth 1 children)))
1305
+ (ev (memory-word-lit-val (js-nth 2 children))))
1306
+ (or (and (= tv 1) (= ev 0)) (and (= tv 0) (= ev 1))))))
1307
+ ((= kind "sequence")
1308
+ (and (> (js-length children) 0)
1309
+ (memory-bool01-p (js-nth (1- (js-length children)) children))))
1310
+ (t nil))))
1311
+
1312
+ (defun memory-bool01-jumps (state node when-true)
1313
+ ;; Branch on whether a memory-bool01-p expression evaluates to 1 (when-true)
1314
+ ;; or 0 (not when-true), regardless of whether its static type is i32 or u32.
1315
+ (let ((kind (get node :kind)) (children (get node :children)))
1316
+ (cond
1317
+ ((= kind "sequence")
1318
+ (let ((clen (js-length children)) (si 0))
1319
+ (while (< si (1- clen))
1320
+ (memory-discarded state (js-nth si children))
1321
+ (setq si (1+ si)))
1322
+ (memory-bool01-jumps state (js-nth (1- clen) children) when-true)))
1323
+ ((= kind "if")
1324
+ (let ((tv (memory-word-lit-val (js-nth 1 children))))
1325
+ (memory-condition-jumps state (js-nth 0 children)
1326
+ (if (= tv 1) when-true (not when-true)))))
1327
+ (t
1328
+ ;; word-compare, v64-compare, and not all have type i32 and return 0/1.
1329
+ (memory-condition-jumps state node when-true)))))
1330
+
1331
+ (defun memory-condition-jumps (state node when-true)
1332
+ (let ((kind (get node :kind)) (children (get node :children)) (jumps (js-array))
1333
+ (opt (and (get state :memory nil) (get state :optimizeMemory nil) (not (get state :debugRanges nil)))))
1334
+ (cond
1335
+ ((and opt (= kind "sequence") (= (get node :type) "i32"))
1336
+ (let ((clen (js-length children)))
1337
+ (if (= clen 0)
1338
+ (when (not when-true) (js-call jumps :push (jump state "jump")))
1339
+ (let ((si 0))
1340
+ (while (< si (1- clen))
1341
+ (memory-discarded state (js-nth si children))
1342
+ (setq si (1+ si)))
1343
+ (let ((last (js-nth (1- clen) children)))
1344
+ (cond
1345
+ ((= (get last :type) "i32")
1346
+ (setq jumps (memory-condition-jumps state last when-true)))
1347
+ ((memory-bool01-p last)
1348
+ (setq jumps (memory-bool01-jumps state last when-true)))
1349
+ ((= (get last :type) "u32")
1350
+ (expression state last)
1351
+ (js-call jumps :push (jump state (if when-true "nonzero" "zero"))))
1352
+ (t
1353
+ (condition state last)
1354
+ (js-call jumps :push (jump state (if when-true "nonzero" "zero"))))))))))
1355
+ ((and opt (= kind "if") (= (get node :type) "i32") (= (js-length children) 3)
1356
+ (let ((tv (memory-word-lit-val (js-nth 1 children)))
1357
+ (ev (memory-word-lit-val (js-nth 2 children))))
1358
+ (or (and (= tv 1) (= ev 0)) (and (= tv 0) (= ev 1)))))
1359
+ (let ((tv (memory-word-lit-val (js-nth 1 children))))
1360
+ (setq jumps (memory-condition-jumps state (js-nth 0 children)
1361
+ (if (= tv 1) when-true (not when-true))))))
1362
+ ((and opt (= kind "not") (= (get node :type) "i32") (= (js-length children) 1))
1363
+ (setq jumps (memory-condition-jumps state (js-nth 0 children) (not when-true))))
1364
+ ((and opt (or (= kind "and") (= kind "or")) (= (get node :type) "i32"))
1365
+ (let ((dyn (memory-dynamic-ref-local children (= kind "or"))))
1366
+ (if (not (nil? dyn))
1367
+ (let ((code (if (= kind "or") 8 9))
1368
+ (dr (local-register state dyn "v64")))
1369
+ (if (>= dr 0)
1370
+ (word state (+ 4043312159 (* dr 32))) ;; cmp xR, #3
1371
+ (progn
1372
+ (expression state dyn)
1373
+ (word state 4043312159))) ;; cmp x0, #3
1374
+ (js-call jumps :push (js-length (get state :words)))
1375
+ (word state (+ 1409286144 (if when-true code (bit-xor code 1)))))
1376
+ (let ((skip (js-array)) (i 0)
1377
+ (every (if (= kind "and") (not when-true) when-true)))
1378
+ (while (< i (js-length children))
1379
+ (let* ((last (= i (1- (js-length children))))
1380
+ (output (if (or every last) jumps skip))
1381
+ (positions (memory-condition-jumps state (js-nth i children)
1382
+ (if (or every last) when-true (not when-true)))) (j 0))
1383
+ (while (< j (js-length positions))
1384
+ (js-call output :push (js-nth j positions))
1385
+ (setq j (1+ j))))
1386
+ (setq i (1+ i)))
1387
+ (when (= (js-length children) 0)
1388
+ (when (= when-true (= kind "and")) (js-call jumps :push (jump state "jump"))))
1389
+ (patch-conditions state skip (js-length (get state :words)))))))
1390
+ ((and opt (or (= kind "word-compare") (= kind "v64-compare")) (= (js-length children) 2))
1391
+ (let* ((name (get node :value)) (code (memory-word-compare-code name))
1392
+ (left (js-nth 0 children)) (right (js-nth 1 children))
1393
+ (lv (if (= kind "word-compare") (memory-word-lit-val left) -1))
1394
+ (rv (if (= kind "word-compare") (memory-word-lit-val right) -1)))
1395
+ (cond
1396
+ ((and (= kind "word-compare") (or (= name "=") (= name "/="))
1397
+ (or (= rv 0) (= rv 1)) (memory-bool01-p left))
1398
+ (let* ((want-one (= rv 1))
1399
+ (flip (if (= name "=") (not want-one) want-one)))
1400
+ (setq jumps (memory-bool01-jumps state left (if flip (not when-true) when-true)))))
1401
+ ((and (= kind "word-compare") (or (= name "=") (= name "/="))
1402
+ (or (= lv 0) (= lv 1)) (memory-bool01-p right))
1403
+ (let* ((want-one (= lv 1))
1404
+ (flip (if (= name "=") (not want-one) want-one)))
1405
+ (setq jumps (memory-bool01-jumps state right (if flip (not when-true) when-true)))))
1406
+ (t
1407
+ (if (= kind "word-compare")
1408
+ (let ((imm (memory-word-imm12 right))
1409
+ (rL (if (memory-word-type (get left :type)) (local-register state left "u32") -1))
1410
+ (rR (if (memory-word-type (get right :type)) (local-register state right "u32") -1)))
1411
+ (if (>= imm 0)
1412
+ (progn
1413
+ (when (< rL 0) (expression state left))
1414
+ (word state (+ 1895825439 (* imm 1024) (if (>= rL 0) (* rL 32) 0)))) ;; cmp wRL, #imm12
1415
+ (if (>= rR 0)
1416
+ (progn
1417
+ (when (< rL 0) (expression state left))
1418
+ (word state (+ 1795162143 (* rR 65536) (if (>= rL 0) (* rL 32) 0)))) ;; cmp wRL, wRR
1419
+ (if (and (>= rL 0) (not (memory-expr-writes-reg-p state right rL)))
1420
+ (progn
1421
+ (expression state right)
1422
+ (word state (+ 1795162143 (* rL 32)))) ;; cmp wRL, w0
1423
+ (progn
1424
+ (expression state left)
1425
+ (if (and (get state :memory nil) (not (get state :compact nil))
1426
+ (not (get state :leaf nil))
1427
+ (memory-word-type (get right :type))
1428
+ (or (= (get right :kind) "literal") (= (get right :kind) "int-literal") (= (get right :kind) "local")))
1429
+ (progn (word state 704644065) (expression state right)) ;; mov w1, w0
1430
+ (progn (push-value state "u32") (expression state right) (pop-value state "u32")))
1431
+ (word state 1795162175)))))) ;; cmp w1, w0
1432
+ (let ((wimm (memory-wide-imm12 right))
1433
+ (rL (if (memory-wide-type (get left :type)) (local-register state left "v64") -1))
1434
+ (rR (if (memory-wide-type (get right :type)) (local-register state right "v64") -1)))
1435
+ (if (not (nil? wimm))
1436
+ (progn
1437
+ (when (< rL 0) (expression state left))
1438
+ (word state (+ 4043309087 (* wimm 1024) (if (>= rL 0) (* rL 32) 0)))) ;; cmp xRL, #wimm
1439
+ (if (>= rR 0)
1440
+ (progn
1441
+ (when (< rL 0) (expression state left))
1442
+ (word state (+ 3942645791 (* rR 65536) (if (>= rL 0) (* rL 32) 0)))) ;; cmp xRL, xRR
1443
+ (if (and (>= rL 0) (not (memory-expr-writes-reg-p state right rL)))
1444
+ (progn
1445
+ (expression state right)
1446
+ (word state (+ 3942645791 (* rL 32)))) ;; cmp xRL, x0
1447
+ (progn (memory-wide-operands state left right)
1448
+ (word state 3942645823))))))) ;; cmp x1, x0
1449
+ (js-call jumps :push (js-length (get state :words)))
1450
+ (word state (+ 1409286144 (if when-true code (bit-xor code 1))))))))
1451
+ (t (condition state node) (js-call jumps :push (jump state (if when-true "nonzero" "zero")))))
1452
+ jumps))
1453
+
944
1454
  (defun emit-loop (state children used)
945
1455
  (if (get state :compact nil)
946
1456
  (let* ((initial (jump state "jump")) (start (js-length (get state :words))))
@@ -949,12 +1459,13 @@
949
1459
  (patch-conditions state (condition-jumps state (js-nth 0 children) t) start)
950
1460
  (when used (integer-register state 0 0 0)))
951
1461
  (let ((start (js-length (get state :words))))
952
- (condition state (js-nth 0 children))
953
- (let ((end (jump state "zero")))
1462
+ (let ((end (if (get state :memory nil)
1463
+ (memory-condition-jumps state (js-nth 0 children) nil)
1464
+ (progn (condition state (js-nth 0 children)) (js-array (jump state "zero"))))))
954
1465
  (if (get state :memory nil) (memory-discarded state (js-nth 1 children))
955
1466
  (expression state (js-nth 1 children)))
956
1467
  (let ((back (jump state "jump"))) (patch-jump state back start))
957
- (patch-jump state end (js-length (get state :words)))
1468
+ (patch-conditions state end (js-length (get state :words)))
958
1469
  (if (get state :memory nil)
959
1470
  (when used (memory-integer-register state 0 0))
960
1471
  (integer-register state 0 0 0))))))
@@ -1010,10 +1521,86 @@
1010
1521
  ;; Reuse expression's debug ranges even when a pure result emits no words.
1011
1522
  (expression state node t))
1012
1523
 
1524
+ (defconst native-single-sink-rd0-ops (new globalThis.Set (js-array
1525
+ "load-u8" "load-u16" "load-u32" "load-v64" "load-bits64"
1526
+ "v64-low" "bits64-low" "v64-high" "bits64-high"
1527
+ "v64-from-u32" "v64-pack" "bits64-pack"
1528
+ "word-and" "word-or" "word-xor" "word-div"
1529
+ "v64-add" "v64-sub" "v64-and" "v64-or" "v64-xor" "v64-shl" "v64-shr"
1530
+ "bits64-add" "bits64-sub" "bits64-and" "bits64-or" "bits64-xor" "bits64-shl" "bits64-shr")))
1531
+
1532
+ (defun memory-single-sink-rd0-p (node)
1533
+ (let ((kind (get node :kind)) (val (get node :value)) (children (get node :children)))
1534
+ (cond
1535
+ ((= kind "word-arithmetic") (> (js-length children) 0))
1536
+ ((= kind "memory-op")
1537
+ (if (js-call native-single-sink-rd0-ops :has val)
1538
+ t
1539
+ (if (and (or (= val "word-shl") (= val "word-shr")) (= (js-length children) 2))
1540
+ (let* ((c1 (js-nth 1 children))
1541
+ (ck (get c1 :kind))
1542
+ (cv (get c1 :value)))
1543
+ (not (and (or (= ck "literal") (= ck "int-literal"))
1544
+ (= (value-type cv) "number")
1545
+ (= (uint32 cv) 0))))
1546
+ nil)))
1547
+ (t nil))))
1548
+
1013
1549
  (defun memory-discarded-body (state node)
1014
1550
  (let ((kind (get node :kind)) (children (get node :children)) (i 0))
1015
1551
  (cond
1016
1552
  ((or (= kind "literal") (= kind "int-literal") (= kind "local")) nil)
1553
+ ((and (= kind "memory-op") (> (memory-width (get node :value)) 0) (= (js-length children) 2))
1554
+ (memory-operation state node t))
1555
+ ((and (or (= kind "set") (= kind "initialize"))
1556
+ (not (get state :debugRanges nil))
1557
+ (not (= (get node :type) "f64")))
1558
+ (let* ((rhs (js-nth 0 children))
1559
+ (ltype (get node :type))
1560
+ (dst-raw (let ((regs (get state :registers)) (idx (get node :value)))
1561
+ (if (< idx (js-length regs)) (js-nth idx regs) -1)))
1562
+ (dst-reg (if (<= dst-raw -2) (- (- dst-raw) 2) -1))
1563
+ (rk (get rhs :kind))
1564
+ (rv (get rhs :value)))
1565
+ (cond
1566
+ ((and (= rk "local") (memory-same-abi-type ltype (get rhs :type)))
1567
+ (let ((src-reg (local-register state rhs ltype)))
1568
+ (if (>= src-reg 0)
1569
+ (unless (= src-reg dst-reg)
1570
+ (local-access state (get node :value) ltype t src-reg))
1571
+ (expression-body state node))))
1572
+ ((and (>= dst-reg 0) (or (= rk "literal") (= rk "int-literal"))
1573
+ (if (memory-wide-type ltype)
1574
+ (and (memory-wide-type (get rhs :type))
1575
+ (or (= (value-type rv) "number")
1576
+ (and (not (nil? rv)) (= (value-type rv) "object"))))
1577
+ (and (memory-word-type ltype)
1578
+ (memory-word-type (get rhs :type))
1579
+ (= (value-type rv) "number")
1580
+ (Number/isInteger rv))))
1581
+ (if (memory-wide-type ltype)
1582
+ (if (and (not (nil? rv)) (= (value-type rv) "object"))
1583
+ (memory-wide-register state dst-reg (uint32 (get rv :low)) (uint32 (get rv :high)))
1584
+ (memory-wide-register state dst-reg (uint32 rv) 0))
1585
+ (memory-integer-register state dst-reg rv)))
1586
+ ((and (= dst-reg -1)
1587
+ (or (= rk "literal") (= rk "int-literal"))
1588
+ (if (memory-wide-type ltype)
1589
+ (if (and (not (nil? rv)) (= (value-type rv) "object"))
1590
+ (and (= (get rv :low) 0) (= (get rv :high) 0))
1591
+ (= rv 0))
1592
+ (= rv 0)))
1593
+ (local-access state (get node :value) ltype t 31))
1594
+ ((and (>= dst-reg 0)
1595
+ (memory-same-abi-type ltype (get rhs :type))
1596
+ (memory-single-sink-rd0-p rhs))
1597
+ (expression state rhs)
1598
+ (let* ((words (get state :words))
1599
+ (wlen (js-length words)))
1600
+ (if (and (> wlen 0) (= (bit-and (native-word-at words (1- wlen)) 31) 0))
1601
+ (retarget-last state dst-reg)
1602
+ (local-access state (get node :value) ltype t))))
1603
+ (t (expression-body state node)))))
1017
1604
  ((or (= kind "sequence")
1018
1605
  (and (or (= kind "word-compare") (= kind "v64-compare")) (= (js-length children) 2))
1019
1606
  (and (= kind "word-arithmetic") (<= (js-length children) 2)
@@ -1023,11 +1610,10 @@
1023
1610
  (while (< i (js-length children))
1024
1611
  (memory-discarded state (js-nth i children)) (setq i (1+ i))))
1025
1612
  ((= kind "if")
1026
- (condition state (js-nth 0 children))
1027
- (let ((otherwise (jump state "zero")))
1613
+ (let ((otherwise (memory-condition-jumps state (js-nth 0 children) nil)))
1028
1614
  (memory-discarded state (js-nth 1 children))
1029
1615
  (let ((end (jump state "jump")))
1030
- (patch-jump state otherwise (js-length (get state :words)))
1616
+ (patch-conditions state otherwise (js-length (get state :words)))
1031
1617
  (memory-discarded state (js-nth 2 children))
1032
1618
  (patch-jump state end (js-length (get state :words))))))
1033
1619
  ((or (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
@@ -1111,7 +1697,8 @@
1111
1697
  ((= kind "not") (condition state (js-nth 0 children)) (word state 1375731712))
1112
1698
  ((= kind "if")
1113
1699
  (let ((otherwise (if (get state :compact nil) (condition-jumps state (js-nth 0 children) nil)
1114
- (progn (condition state (js-nth 0 children)) (js-array (jump state "zero"))))))
1700
+ (if (get state :memory nil) (memory-condition-jumps state (js-nth 0 children) nil)
1701
+ (progn (condition state (js-nth 0 children)) (js-array (jump state "zero")))))))
1115
1702
  (expression state (js-nth 1 children))
1116
1703
  (let ((end (jump state "jump")))
1117
1704
  (patch-conditions state otherwise (js-length (get state :words)))
@@ -1275,11 +1862,13 @@
1275
1862
 
1276
1863
  (defun export-trie (program offsets)
1277
1864
  (let ((root (js-object :children (js-array) :address -1 :offset 0))
1278
- (functions (get program :functions)) (nodes (js-array)) (i 0))
1865
+ (functions (get program :functions))
1866
+ (export-set (new globalThis.Set (get program :exports)))
1867
+ (nodes (js-array)) (i 0))
1279
1868
  (while (< i (js-length functions))
1280
- (when (js-call (get program :exports) :includes (get (js-nth i functions) :name))
1869
+ (when (js-call export-set :has (get (aref functions i) :name))
1281
1870
  (let ((bytes (js-array)) (node root) (j 0))
1282
- (string-bytes bytes (str "_" (get (js-nth i functions) :name)))
1871
+ (string-bytes bytes (str "_" (get (aref functions i) :name)))
1283
1872
  (while (< j (1- (js-length bytes)))
1284
1873
  (let* ((edges (get node :children)) (k 0) (next nil))
1285
1874
  (while (< k (js-length edges))
@@ -1343,10 +1932,14 @@
1343
1932
  (defun be64 (output value)
1344
1933
  (be32 output (uint32 (/ value 4294967296))) (be32 output (uint32 value)))
1345
1934
 
1935
+ (defconst page-sha256-cache (new globalThis.Map))
1936
+
1346
1937
  (defun page-signature (image executable-size main)
1347
1938
  (let* ((identifier (js-array)) (limit (js-length image))
1348
1939
  (pages (js-call globalThis.Math :ceil (/ limit 4096)))
1349
- (directory (js-array)) (output (js-array)))
1940
+ (directory (js-array)) (output (js-array))
1941
+ (chunks (if (Array/isArray image) nil (get image :chunks nil)))
1942
+ (chunk-count (if (Array/isArray chunks) (js-length chunks) 0)))
1350
1943
  (string-bytes identifier "eliscript-native")
1351
1944
  (let ((hash-offset (+ 88 (js-length identifier))))
1352
1945
  (be32 directory 4208856066) (be32 directory (+ hash-offset (* pages 32)))
@@ -1359,7 +1952,19 @@
1359
1952
  (append directory identifier)
1360
1953
  (let ((i 0))
1361
1954
  (while (< i pages)
1362
- (append directory (sha256-bytes (binary-slice image (* i 4096) (* (1+ i) 4096))))
1955
+ (let* ((chunk (if (< i chunk-count) (aref chunks i) nil))
1956
+ (digest (if (and (= (value-type chunk) "string") (= (js-length chunk) 2048))
1957
+ (let ((cached-digest (js-call page-sha256-cache :get chunk)))
1958
+ (if cached-digest cached-digest
1959
+ (let ((computed (sha256-string-utf16le chunk)))
1960
+ (when (< (get page-sha256-cache :size) 4096)
1961
+ (js-call page-sha256-cache :set chunk computed))
1962
+ computed)))
1963
+ (sha256-bytes (binary-slice image (* i 4096) (* (1+ i) 4096)))))
1964
+ (k 0))
1965
+ (while (< k 32)
1966
+ (js-call directory :push (aref digest k))
1967
+ (setq k (1+ k))))
1363
1968
  (setq i (1+ i))))
1364
1969
  (be32 output 4208856256) (be32 output (+ 20 (js-length directory)))
1365
1970
  (be32 output 1) (be32 output 0) (be32 output 20)
@@ -1376,20 +1981,24 @@
1376
1981
  (text-end (if has-ro (+ ro-file-offset ro-len) (+ 4096 size)))
1377
1982
  (text-size (align-size text-end 16384))
1378
1983
  (trie (export-trie program offsets))
1984
+ (export-set (new globalThis.Set (get program :exports)))
1379
1985
  (symbols (js-array)) (strings (js-array 0)) (local-count 0)
1380
- (commands (js-array)) (image (binary-output options)) (i 0))
1986
+ (want-packed (and (not (nil? options)) (= (get options :binaryEncoding nil) "u16le-chunks")))
1987
+ (_check-encoding (when (and (not (nil? options)) (Object/hasOwn options "binaryEncoding") (not want-packed))
1988
+ (binary-output options)))
1989
+ (commands (js-array)) (image (binary-output (js-object :binaryEncoding "u16le-chunks"))) (i 0))
1381
1990
  ;; Local symbols precede external definitions; public surface is exact.
1382
1991
  (let ((pass 0))
1383
1992
  (while (< pass 2)
1384
1993
  (let ((external (= pass 1)))
1385
1994
  (setq i 0)
1386
1995
  (while (< i (js-length functions))
1387
- (let* ((function (js-nth i functions))
1388
- (public (js-call (get program :exports) :includes (get function :name))))
1996
+ (let* ((function (aref functions i))
1997
+ (public (js-call export-set :has (get function :name))))
1389
1998
  (when (if public external (not external))
1390
1999
  (u32 symbols (js-length strings))
1391
2000
  (js-call symbols :push (if public 15 14) 1 0 0)
1392
- (u64 symbols (+ (if entry 4294967296 0) 4096 (js-nth i offsets)))
2001
+ (u64 symbols (+ (if entry 4294967296 0) 4096 (aref offsets i)))
1393
2002
  (string-bytes strings (str "_" (get function :name)))
1394
2003
  (unless public (setq local-count (1+ local-count)))))
1395
2004
  (setq i (1+ i))))
@@ -1427,7 +2036,13 @@
1427
2036
  (dylib-command commands 13 "@rpath/eliscript-native.dylib"))
1428
2037
  (dylib-command commands 12 "/usr/lib/libSystem.B.dylib")
1429
2038
  (u32 commands 27) (u32 commands 24)
1430
- (let ((word-digest (sha256-words-le words (if (Array/isArray words) nil native-word-at))))
2039
+ (unless (Array/isArray words) (native-word-flush words))
2040
+ (let ((word-digest (sha256-words-le words
2041
+ (if (or (Array/isArray words)
2042
+ (and (Array/isArray (get words :chunks nil))
2043
+ (not (get words :dirty nil))
2044
+ (not (get words :hasHole nil))))
2045
+ nil native-word-at))))
1431
2046
  (append commands (js-call (if has-ro
1432
2047
  (let ((combined (js-call word-digest :slice)))
1433
2048
  (append combined (sha256-bytes ro-bytes))
@@ -1467,7 +2082,7 @@
1467
2082
  (put code :roDataSection (js-object :name "__const" :segment "__TEXT"
1468
2083
  :fileOffset ro-file-offset :virtualAddress (+ (if entry 4294967296 0) ro-file-offset)
1469
2084
  :size ro-len :align 8)))
1470
- (binary-finish image))))
2085
+ (if want-packed (binary-finish image) (binary-to-array image)))))
1471
2086
 
1472
2087
  (defun emit-native-numeric-library (program target &optional options)
1473
2088
  (unless (or (= target "arm64-macos") (= target "arm64-linux"))
@@ -1531,12 +2146,13 @@
1531
2146
  (word state 3103791721) ;; str w9, [x19, #24]
1532
2147
  (let ((sites (get state :trapSites nil)))
1533
2148
  (when sites
1534
- (memory-integer-register state 9 (1+ (js-length sites)))
1535
- (let ((range (js-object :kind "trap-site" :function (get state :debugFunction)
1536
- :functionIndex (get state :debugFunctionIndex) :depth (get state :debugDepth 0)
1537
- :start (* 4 (js-length (get state :words))) :end (+ 4 (* 4 (js-length (get state :words))))
1538
- :span nil :code code :site (1+ (js-length sites)))))
1539
- (js-call sites :push range) (js-call (get state :debugRanges) :push range))
2149
+ (let ((site-id (+ (get state :trapSiteBase 0) (1+ (js-length sites)))))
2150
+ (memory-integer-register state 9 site-id)
2151
+ (let ((range (js-object :kind "trap-site" :function (get state :debugFunction)
2152
+ :functionIndex (get state :debugFunctionIndex) :depth (get state :debugDepth 0)
2153
+ :start (* 4 (js-length (get state :words))) :end (+ 4 (* 4 (js-length (get state :words))))
2154
+ :span nil :code code :site site-id)))
2155
+ (js-call sites :push range) (js-call (get state :debugRanges) :push range)))
1540
2156
  (word state 3103792745))) ;; str w9, [x19, #28]
1541
2157
  (js-call (get state :memoryErrors) :push (jump state "jump")))
1542
2158
 
@@ -1607,33 +2223,81 @@
1607
2223
  (memory-integer-register state 0 0)))
1608
2224
 
1609
2225
  (defun memory-word (state node)
1610
- (let* ((children (get node :children)) (count (js-length children)) (name (get node :value)))
2226
+ (let* ((children (get node :children)) (count (js-length children)) (name (get node :value))
2227
+ (opt (and (get state :memory nil) (get state :optimizeMemory nil)))
2228
+ (no-debug (not (get state :debugRanges nil))))
1611
2229
  (if (= count 0)
1612
2230
  (if (get state :memory nil) (memory-integer-register state 0 (if (= name "*") 1 0))
1613
2231
  (integer-register state 0 (if (= name "*") 1 0) 0))
1614
- (progn
1615
- (expression state (js-nth 0 children))
2232
+ (let* ((left (js-nth 0 children))
2233
+ (rL (if (and opt no-debug (memory-word-type (get left :type)))
2234
+ (local-register state left "u32") -1)))
1616
2235
  (cond
1617
2236
  ((= count 2)
1618
- (let ((right (js-nth 1 children)))
1619
- (if (and (get state :memory nil) (not (get state :compact nil))
1620
- (not (get state :leaf nil)) (= (js-length (get state :registers)) 0)
1621
- (memory-word-type (get right :type))
1622
- (or (= (get right :kind) "literal") (= (get right :kind) "int-literal") (= (get right :kind) "local")))
1623
- ;; These RHS forms touch only x0/x9 in the memory backend.
1624
- ;; Capture the evaluated LHS without changing evaluation order.
1625
- (progn (word state 704644065) (expression state right)) ;; mov w1,w0
1626
- (progn
1627
- (push-value state "u32") (expression state right) (pop-value state "u32"))))
1628
- (if (= (get node :kind) "word-compare")
1629
- (let ((code (cond ((= name "=") 0) ((= name "/=") 1) ((= name "<") 3)
1630
- ((= name "<=") 9) ((= name ">") 8) (t 2))))
1631
- (word state 1795162175)
1632
- (word state (+ 446629856 (* (bit-xor code 1) 4096))))
1633
- (word state (cond ((= name "+") 184549408) ((= name "-") 1258291232) (t 453016608)))))
1634
- ((= name "-") (word state 1258292192))
1635
- ((= name "1+") (word state 285213696))
1636
- ((= name "1-") (word state 1358955520)))))))
2237
+ (let* ((right (js-nth 1 children))
2238
+ (imm (if opt (memory-word-imm12 right) -1))
2239
+ (shift (if (and opt (= (get node :kind) "word-arithmetic") (= name "*"))
2240
+ (memory-word-pow2-shift right) 0))
2241
+ (rR (if (and opt no-debug (memory-word-type (get right :type)))
2242
+ (local-register state right "u32") -1)))
2243
+ (cond
2244
+ ((and (= (get node :kind) "word-arithmetic") (or (= name "+") (= name "-")) (>= imm 0))
2245
+ (when (< rL 0) (expression state left))
2246
+ (word state (+ (if (= name "+") 285212672 1358954496) (* imm 1024) (if (>= rL 0) (* rL 32) 0))))
2247
+ ((> shift 0)
2248
+ (when (< rL 0) (expression state left))
2249
+ (word state (+ 1392508928 (* (- 32 shift) 65536) (* (- 31 shift) 1024) (if (>= rL 0) (* rL 32) 0))))
2250
+ ((and (= (get node :kind) "word-compare") (>= imm 0))
2251
+ (when (< rL 0) (expression state left))
2252
+ (word state (+ 1895825439 (* imm 1024) (if (>= rL 0) (* rL 32) 0)))
2253
+ (let ((code (memory-word-compare-code name)))
2254
+ (word state (+ 446629856 (* (bit-xor code 1) 4096)))))
2255
+ ((>= rR 0)
2256
+ (when (< rL 0) (expression state left))
2257
+ (if (= (get node :kind) "word-compare")
2258
+ (let ((code (memory-word-compare-code name)))
2259
+ (word state (+ 1795162143 (* rR 65536) (if (>= rL 0) (* rL 32) 0)))
2260
+ (word state (+ 446629856 (* (bit-xor code 1) 4096))))
2261
+ (word state (+ (cond ((= name "+") 184549376) ((= name "-") 1258291200) (t 453016576))
2262
+ (* rR 65536) (if (>= rL 0) (* rL 32) 0)))))
2263
+ (t
2264
+ (if (and opt no-debug (>= rL 0)
2265
+ (not (memory-expr-writes-reg-p state right rL)))
2266
+ (progn
2267
+ (expression state right)
2268
+ (if (= (get node :kind) "word-compare")
2269
+ (let ((code (memory-word-compare-code name)))
2270
+ (word state (+ 1795162143 (* rL 32))) ;; cmp wRL, w0
2271
+ (word state (+ 446629856 (* (bit-xor code 1) 4096))))
2272
+ (word state (+ (cond ((= name "+") 184549376) ((= name "-") 1258291200) (t 453016576))
2273
+ (* rL 32))))) ;; add/sub/mul w0, wRL, w0
2274
+ (progn
2275
+ (expression state left)
2276
+ (if (and (get state :memory nil) (not (get state :compact nil))
2277
+ (not (get state :leaf nil))
2278
+ (memory-word-type (get right :type))
2279
+ (or (= (get right :kind) "literal") (= (get right :kind) "int-literal") (= (get right :kind) "local")))
2280
+ ;; These forms write only x0/x9 even when the local is
2281
+ ;; read from a callee-saved register.
2282
+ ;; Capture the evaluated LHS without changing evaluation order.
2283
+ (progn (word state 704644065) (expression state right)) ;; mov w1,w0
2284
+ (progn
2285
+ (push-value state "u32") (expression state right) (pop-value state "u32")))
2286
+ (if (= (get node :kind) "word-compare")
2287
+ (let ((code (memory-word-compare-code name)))
2288
+ (word state 1795162175)
2289
+ (word state (+ 446629856 (* (bit-xor code 1) 4096))))
2290
+ (word state (cond ((= name "+") 184549408) ((= name "-") 1258291232) (t 453016608))))))))))
2291
+ ((= name "-")
2292
+ (expression state left) (word state 1258292192))
2293
+ ((= name "1+")
2294
+ (if (>= rL 0)
2295
+ (word state (+ 285213696 (* rL 32)))
2296
+ (progn (expression state left) (word state 285213696))))
2297
+ (t
2298
+ (if (>= rL 0)
2299
+ (word state (+ 1358955520 (* rL 32)))
2300
+ (progn (expression state left) (word state 1358955520)))))))))
1637
2301
 
1638
2302
  (defun memory-grow (state node)
1639
2303
  (expression state (js-nth 0 (get node :children)))
@@ -1656,6 +2320,8 @@
1656
2320
  (word state 1409286145) (patch-jump state back loop)))
1657
2321
  (patch-jump state done (js-length (get state :words))))
1658
2322
  (word state 4177528426) ;; str x10, [x19, #8]
2323
+ (when (get state :pinMemory nil)
2324
+ (word state 2852783100)) ;; mov x28, x10
1659
2325
  (let ((end (jump state "jump")))
1660
2326
  (patch-jump state failed (js-length (get state :words)))
1661
2327
  (memory-integer-register state 0 4294967295)
@@ -1686,7 +2352,18 @@
1686
2352
  (word state 3943301439) ;; cmp x9,x10
1687
2353
  (let ((backward (js-length (get state :words))))
1688
2354
  (word state 1409286152) ;; b.hi backward (dst_ptr > src_ptr)
1689
- ;; Forward 8B + 1B loop (memmove-exact when dst_ptr <= src_ptr)
2355
+ ;; Forward 32B + 8B + 1B loop (memmove-exact when dst_ptr <= src_ptr)
2356
+ (let ((fwd32 (js-length (get state :words))))
2357
+ (word state 4043342271) ;; cmp x13,#32
2358
+ (let ((fwd8-skip (js-length (get state :words))))
2359
+ (word state 1409286147) ;; b.lo fwd8
2360
+ (word state 2831236428) ;; ldp x12,x15,[x10],#16
2361
+ (word state 2827042092) ;; stp x12,x15,[x9],#16
2362
+ (word state 2831236428) ;; ldp x12,x15,[x10],#16
2363
+ (word state 2827042092) ;; stp x12,x15,[x9],#16
2364
+ (word state 3506471341) ;; sub x13,x13,#32
2365
+ (let ((back32 (jump state "jump"))) (patch-jump state back32 fwd32))
2366
+ (patch-jump state fwd8-skip (js-length (get state :words)))))
1690
2367
  (let ((fwd8 (js-length (get state :words))))
1691
2368
  (word state 4043317695) ;; cmp x13,#8
1692
2369
  (let ((fwd1 (js-length (get state :words))))
@@ -1733,43 +2410,98 @@
1733
2410
  (memory-integer-register state 0 0)))
1734
2411
 
1735
2412
  (defun memory-fill-op (state node)
1736
- (let ((children (get node :children)))
1737
- (expression state (js-nth 0 children)) (push-value state "u32")
1738
- (expression state (js-nth 1 children)) (push-value state "u32")
1739
- (expression state (js-nth 2 children))
1740
- (word state 704644077) ;; mov w13,w0 (len, zero-extended in x13)
1741
- (pop-value state "u32")
1742
- (word state 704709612) ;; mov w12,w1 (byte val)
1743
- (pop-value state "u32")
1744
- (word state 704709600) ;; mov w0,w1 (dst, zero-extended in x0)
1745
- (word state 2332885002) ;; add x10,x0,x13
1746
- (word state 4181722731) ;; ldr x11,[x19,#8]
1747
- (word state 3943367007) ;; cmp x10,x11
1748
- (memory-conditional-error state 1409286153 1) ;; b.ls
1749
- (let ((done (js-length (get state :words))))
1750
- (word state 3019898893) ;; cbz x13, done
1751
- (word state 4181721705) ;; ldr x9,[x19]
1752
- (word state 2332033321) ;; add x9,x9,x0
1753
- (word state 3544194444) ;; and x12,x12,#0xff
1754
- (integer-register state 10 16843009 16843009) ;; x10 = 0x0101010101010101
1755
- (word state 2601155980) ;; mul x12,x12,x10
1756
- (let ((fill8 (js-length (get state :words))))
1757
- (word state 4043317695) ;; cmp x13,#8
1758
- (let ((fill1 (js-length (get state :words))))
1759
- (word state 1409286147) ;; b.lo fill1
1760
- (word state 4160783660) ;; str x12,[x9],#8
1761
- (word state 3506446765) ;; sub x13,x13,#8
1762
- (let ((back8 (jump state "jump"))) (patch-jump state back8 fill8))
1763
- (patch-jump state fill1 (js-length (get state :words)))))
1764
- (let ((fill-done (js-length (get state :words))))
1765
- (word state 3019898893) ;; cbz x13, fill-done
1766
- (let ((fill1-loop (js-length (get state :words))))
1767
- (word state 939529516) ;; strb w12,[x9],#1
1768
- (word state 4043310509) ;; subs x13,x13,#1
1769
- (let ((back1 (js-length (get state :words))))
1770
- (word state 1409286145) (patch-jump state back1 fill1-loop)))
1771
- (patch-jump state fill-done (js-length (get state :words))))
1772
- (patch-jump state done (js-length (get state :words))))
2413
+ (let* ((children (get node :children))
2414
+ (val-node (js-nth 1 children))
2415
+ (val0 (and (or (= (get val-node :kind) "literal") (= (get val-node :kind) "int-literal"))
2416
+ (= (value-type (get val-node :value)) "number")
2417
+ (= (bit-and (uint32 (get val-node :value)) 255) 0))))
2418
+ (if val0
2419
+ (progn
2420
+ (expression state (js-nth 0 children)) (push-value state "u32")
2421
+ (expression state (js-nth 2 children))
2422
+ (word state 704644077) ;; mov w13,w0 (len, zero-extended in x13)
2423
+ (pop-value state "u32")
2424
+ (word state 704709600) ;; mov w0,w1 (dst, zero-extended in x0)
2425
+ (word state 2332885002) ;; add x10,x0,x13
2426
+ (word state 4181722731) ;; ldr x11,[x19,#8]
2427
+ (word state 3943367007) ;; cmp x10,x11
2428
+ (memory-conditional-error state 1409286153 1) ;; b.ls
2429
+ (let ((done (js-length (get state :words))))
2430
+ (word state 3019898893) ;; cbz x13, done
2431
+ (word state 4181721705) ;; ldr x9,[x19]
2432
+ (word state 2332033321) ;; add x9,x9,x0
2433
+ (let ((fill32 (js-length (get state :words))))
2434
+ (word state 4043342271) ;; cmp x13,#32
2435
+ (let ((fill8 (js-length (get state :words))))
2436
+ (word state 1409286147) ;; b.lo fill8
2437
+ (word state 2827058495) ;; stp xzr,xzr,[x9],#16
2438
+ (word state 2827058495) ;; stp xzr,xzr,[x9],#16
2439
+ (word state 3506471341) ;; sub x13,x13,#32
2440
+ (let ((back32 (jump state "jump"))) (patch-jump state back32 fill32))
2441
+ (patch-jump state fill8 (js-length (get state :words)))))
2442
+ (let ((fill8 (js-length (get state :words))))
2443
+ (word state 4043317695) ;; cmp x13,#8
2444
+ (let ((fill1 (js-length (get state :words))))
2445
+ (word state 1409286147) ;; b.lo fill1
2446
+ (word state 4160783679) ;; str xzr,[x9],#8
2447
+ (word state 3506446765) ;; sub x13,x13,#8
2448
+ (let ((back8 (jump state "jump"))) (patch-jump state back8 fill8))
2449
+ (patch-jump state fill1 (js-length (get state :words)))))
2450
+ (let ((fill-done (js-length (get state :words))))
2451
+ (word state 3019898893) ;; cbz x13, fill-done
2452
+ (let ((fill1-loop (js-length (get state :words))))
2453
+ (word state 939529535) ;; strb wzr,[x9],#1
2454
+ (word state 4043310509) ;; subs x13,x13,#1
2455
+ (let ((back1 (js-length (get state :words))))
2456
+ (word state 1409286145) (patch-jump state back1 fill1-loop)))
2457
+ (patch-jump state fill-done (js-length (get state :words))))
2458
+ (patch-jump state done (js-length (get state :words)))))
2459
+ (progn
2460
+ (expression state (js-nth 0 children)) (push-value state "u32")
2461
+ (expression state (js-nth 1 children)) (push-value state "u32")
2462
+ (expression state (js-nth 2 children))
2463
+ (word state 704644077) ;; mov w13,w0 (len, zero-extended in x13)
2464
+ (pop-value state "u32")
2465
+ (word state 704709612) ;; mov w12,w1 (byte val)
2466
+ (pop-value state "u32")
2467
+ (word state 704709600) ;; mov w0,w1 (dst, zero-extended in x0)
2468
+ (word state 2332885002) ;; add x10,x0,x13
2469
+ (word state 4181722731) ;; ldr x11,[x19,#8]
2470
+ (word state 3943367007) ;; cmp x10,x11
2471
+ (memory-conditional-error state 1409286153 1) ;; b.ls
2472
+ (let ((done (js-length (get state :words))))
2473
+ (word state 3019898893) ;; cbz x13, done
2474
+ (word state 4181721705) ;; ldr x9,[x19]
2475
+ (word state 2332033321) ;; add x9,x9,x0
2476
+ (word state 3544194444) ;; and x12,x12,#0xff
2477
+ (integer-register state 10 16843009 16843009) ;; x10 = 0x0101010101010101
2478
+ (word state 2601155980) ;; mul x12,x12,x10
2479
+ (let ((fill32 (js-length (get state :words))))
2480
+ (word state 4043342271) ;; cmp x13,#32
2481
+ (let ((fill8 (js-length (get state :words))))
2482
+ (word state 1409286147) ;; b.lo fill8
2483
+ (word state 2827039020) ;; stp x12,x12,[x9],#16
2484
+ (word state 2827039020) ;; stp x12,x12,[x9],#16
2485
+ (word state 3506471341) ;; sub x13,x13,#32
2486
+ (let ((back32 (jump state "jump"))) (patch-jump state back32 fill32))
2487
+ (patch-jump state fill8 (js-length (get state :words)))))
2488
+ (let ((fill8 (js-length (get state :words))))
2489
+ (word state 4043317695) ;; cmp x13,#8
2490
+ (let ((fill1 (js-length (get state :words))))
2491
+ (word state 1409286147) ;; b.lo fill1
2492
+ (word state 4160783660) ;; str x12,[x9],#8
2493
+ (word state 3506446765) ;; sub x13,x13,#8
2494
+ (let ((back8 (jump state "jump"))) (patch-jump state back8 fill8))
2495
+ (patch-jump state fill1 (js-length (get state :words)))))
2496
+ (let ((fill-done (js-length (get state :words))))
2497
+ (word state 3019898893) ;; cbz x13, fill-done
2498
+ (let ((fill1-loop (js-length (get state :words))))
2499
+ (word state 939529516) ;; strb w12,[x9],#1
2500
+ (word state 4043310509) ;; subs x13,x13,#1
2501
+ (let ((back1 (js-length (get state :words))))
2502
+ (word state 1409286145) (patch-jump state back1 fill1-loop)))
2503
+ (patch-jump state fill-done (js-length (get state :words))))
2504
+ (patch-jump state done (js-length (get state :words))))))
1773
2505
  (memory-integer-register state 0 0)))
1774
2506
 
1775
2507
  (defun memory-wide-operands (state left right)
@@ -1780,12 +2512,31 @@
1780
2512
  (progn (push-value state "v64") (expression state right) (pop-value state "v64"))))
1781
2513
 
1782
2514
  (defun memory-v64-compare (state node)
1783
- (let ((children (get node :children)) (name (get node :value)))
1784
- (memory-wide-operands state (js-nth 0 children) (js-nth 1 children))
1785
- (let ((code (cond ((= name "=") 0) ((= name "/=") 1) ((= name "<") 3)
1786
- ((= name "<=") 9) ((= name ">") 8) (t 2))))
1787
- (word state 3942645823) ;; cmp x1,x0
1788
- (word state (+ 446629856 (* (bit-xor code 1) 4096))))))
2515
+ (let* ((children (get node :children)) (name (get node :value))
2516
+ (left (js-nth 0 children))
2517
+ (right (js-nth 1 children))
2518
+ (opt (and (get state :memory nil) (get state :optimizeMemory nil)))
2519
+ (no-debug (not (get state :debugRanges nil)))
2520
+ (wimm (when opt (memory-wide-imm12 right)))
2521
+ (rL (if (and opt no-debug (memory-wide-type (get left :type))) (local-register state left "v64") -1))
2522
+ (rR (if (and opt no-debug (memory-wide-type (get right :type))) (local-register state right "v64") -1))
2523
+ (code (cond ((= name "=") 0) ((= name "/=") 1) ((= name "<") 3)
2524
+ ((= name "<=") 9) ((= name ">") 8) (t 2))))
2525
+ (if (not (nil? wimm))
2526
+ (progn
2527
+ (when (< rL 0) (expression state left))
2528
+ (word state (+ 4043309087 (* wimm 1024) (if (>= rL 0) (* rL 32) 0)))) ;; cmp xRL, #imm
2529
+ (if (>= rR 0)
2530
+ (progn
2531
+ (when (< rL 0) (expression state left))
2532
+ (word state (+ 3942645791 (* rR 65536) (if (>= rL 0) (* rL 32) 0)))) ;; cmp xRL, xRR
2533
+ (if (and (>= rL 0) (not (memory-expr-writes-reg-p state right rL)))
2534
+ (progn
2535
+ (expression state right)
2536
+ (word state (+ 3942645791 (* rL 32)))) ;; cmp xRL, x0
2537
+ (progn (memory-wide-operands state left right)
2538
+ (word state 3942645823))))) ;; cmp x1, x0
2539
+ (word state (+ 446629856 (* (bit-xor code 1) 4096)))))
1789
2540
 
1790
2541
  (defun memory-f64-unary-instruction (name)
1791
2542
  ;; Arm A64 scalar FP one-source encoding, ftype=01, Rn=Rd=0.
@@ -1810,7 +2561,71 @@
1810
2561
  (word state (memory-f64-unary-instruction (get node :value)))
1811
2562
  (word state 3575333897)) ;; msr FPCR,x9 (0xd51b4409)
1812
2563
 
1813
- (defun memory-operation (state node)
2564
+ (defun memory-expr-writes-local-p (node target)
2565
+ (let ((kind (get node :kind)) (children (get node :children)) (i 0) (found nil))
2566
+ (if (and (or (= kind "set") (= kind "initialize")) (= (get node :value) target))
2567
+ t
2568
+ (progn
2569
+ (when children
2570
+ (while (and (not found) (< i (js-length children)))
2571
+ (when (memory-expr-writes-local-p (js-nth i children) target)
2572
+ (setq found t))
2573
+ (setq i (1+ i))))
2574
+ found))))
2575
+
2576
+ (defun memory-expr-writes-reg-p (state node target-reg)
2577
+ (let ((kind (get node :kind)) (children (get node :children)) (i 0) (found nil))
2578
+ (if (and (or (= kind "set") (= kind "initialize"))
2579
+ (let* ((regs (get state :registers)) (idx (get node :value))
2580
+ (raw (if (< idx (js-length regs)) (js-nth idx regs) -1)))
2581
+ (and (<= raw -2) (= (- (- raw) 2) target-reg))))
2582
+ t
2583
+ (progn
2584
+ (when children
2585
+ (while (and (not found) (< i (js-length children)))
2586
+ (when (memory-expr-writes-reg-p state (js-nth i children) target-reg)
2587
+ (setq found t))
2588
+ (setq i (1+ i))))
2589
+ found))))
2590
+
2591
+ (defun memory-addr-base-imm (state addr)
2592
+ (let ((kind (get addr :kind)) (val (get addr :value)) (children (get addr :children)))
2593
+ (when (and (= kind "word-arithmetic") (memory-word-type (get addr :type)))
2594
+ (cond
2595
+ ((and (= val "+") (= (js-length children) 2))
2596
+ (let* ((left (js-nth 0 children))
2597
+ (right (js-nth 1 children))
2598
+ (rL (if (memory-word-type (get left :type)) (local-register state left "u32") -1))
2599
+ (immR (memory-word-imm12 right)))
2600
+ (if (and (>= rL 0) (>= immR 0))
2601
+ (js-object :reg rL :imm immR :local (get left :value))
2602
+ (let ((rR (if (memory-word-type (get right :type)) (local-register state right "u32") -1))
2603
+ (immL (memory-word-imm12 left)))
2604
+ (when (and (>= rR 0) (>= immL 0))
2605
+ (js-object :reg rR :imm immL :local (get right :value)))))))
2606
+ ((and (= val "1+") (= (js-length children) 1))
2607
+ (let* ((arg (js-nth 0 children))
2608
+ (rA (if (memory-word-type (get arg :type)) (local-register state arg "u32") -1)))
2609
+ (when (>= rA 0)
2610
+ (js-object :reg rA :imm 1 :local (get arg :value)))))))))
2611
+
2612
+ (defun memory-emit-pinned-bounds (state width idx-reg)
2613
+ (word state (+ 2432696330 (* width 1024) (* idx-reg 32))) ;; add x10, xIdx, #width
2614
+ (word state 3944481119) ;; cmp x10, x28
2615
+ (if (get state :trapSites nil)
2616
+ (memory-conditional-error state 1409286153 1) ;; b.ls safe
2617
+ (progn
2618
+ (js-call (get state :boundsErrors) :push (js-length (get state :words)))
2619
+ (word state 1409286152)))) ;; b.hi bounds_trap
2620
+
2621
+ (defun memory-emit-pinned-store (state name width idx-reg rV &optional discard)
2622
+ (memory-emit-pinned-bounds state width idx-reg)
2623
+ (word state (+ (cond ((= name "store-u8") 941648736) ((= name "store-u16") 2015390560)
2624
+ ((= name "store-u32") 3089132384) (t 4162874208))
2625
+ (* idx-reg 65536) rV))
2626
+ (unless discard (memory-integer-register state 0 0)))
2627
+
2628
+ (defun memory-operation (state node &optional discard)
1814
2629
  (let* ((name (get node :value)) (children (get node :children)) (width (memory-width name))
1815
2630
  (store (and (> width 0) (= (js-length children) 2))))
1816
2631
  (cond
@@ -1818,43 +2633,151 @@
1818
2633
  ((= name "memory-copy") (memory-copy-op state node))
1819
2634
  ((= name "memory-fill") (memory-fill-op state node))
1820
2635
  ((> width 0)
1821
- (expression state (js-nth 0 children))
1822
- (when store
1823
- (let ((right (js-nth 1 children))
1824
- (move-temp (if (= name "store-f64") 509624322
1825
- (if (or (= name "store-v64") (= name "store-bits64")) 2852127714 704644066))))
1826
- (if (or (= (get right :kind) "literal") (= (get right :kind) "int-literal") (= (get right :kind) "local"))
1827
- (progn
1828
- (word state 704644065) ;; mov w1,w0
1829
- (expression state right)
1830
- (word state move-temp) ;; fmov d2,d0 / mov x2,x0 / mov w2,w0
1831
- (word state 704709600)) ;; mov w0,w1
1832
- (progn
1833
- (push-value state "u32")
1834
- (expression state right)
1835
- (word state move-temp)
1836
- (pop-value state "u32") (word state 704709600)))))
1837
- ;; w0 zero-extends address; compare address+width in 64 bits before
1838
- ;; adding the host pointer, including near-4GiB wrapping addresses.
1839
- (word state (+ 2432696330 (* width 1024))) ;; add x10,x0,#width
1840
- (word state 4181722731) (word state 3943367007)
1841
- (memory-conditional-error state 1409286153 1) ;; b.ls safe
1842
- (word state 4181721705) (word state 2332033321)
1843
- (word state (cond ((= name "load-u8") 960495904) ((= name "load-u16") 2034237728)
1844
- ((= name "load-u32") 3107979552) ((= name "load-f64") 4248830240)
1845
- ((or (= name "load-v64") (= name "load-bits64")) 4181721376)
1846
- ((= name "store-u8") 956301602) ((= name "store-u16") 2030043426)
1847
- ((= name "store-u32") 3103785250) ((= name "store-f64") 4244635938)
1848
- (t 4177527074)))
1849
- (when store (memory-integer-register state 0 0)))
2636
+ (let* ((addr (js-nth 0 children))
2637
+ (no-debug (not (get state :debugRanges nil)))
2638
+ (pin (get state :pinMemory nil))
2639
+ (right (when store (js-nth 1 children)))
2640
+ (rV (if (and pin store no-debug (/= name "store-f64"))
2641
+ (let* ((rtype (get right :type))
2642
+ (rk (get right :kind))
2643
+ (rv (get right :value))
2644
+ (reg (if (or (memory-word-type rtype) (memory-wide-type rtype))
2645
+ (local-register state right rtype) -1)))
2646
+ (if (>= reg 0) reg
2647
+ (if (and (or (= rk "literal") (= rk "int-literal"))
2648
+ (if (memory-wide-type rtype)
2649
+ (if (and (not (nil? rv)) (= (value-type rv) "object"))
2650
+ (and (= (get rv :low) 0) (= (get rv :high) 0))
2651
+ (= rv 0))
2652
+ (= rv 0)))
2653
+ 31 -1)))
2654
+ -1))
2655
+ (constAddr (if (and pin no-debug (memory-word-type (get addr :type)))
2656
+ (memory-word-imm12 addr) -1))
2657
+ (rA (if (and pin no-debug (< constAddr 0) (memory-word-type (get addr :type)))
2658
+ (local-register state addr "u32") -1))
2659
+ (baseImmRaw (if (and pin no-debug (< constAddr 0) (< rA 0)) (memory-addr-base-imm state addr) nil))
2660
+ (baseImm (when baseImmRaw
2661
+ (if (= (get baseImmRaw :imm) 0)
2662
+ (progn (setq rA (get baseImmRaw :reg)) nil)
2663
+ baseImmRaw)))
2664
+ (addrReg (if (>= rA 0) rA (if baseImm (get baseImm :reg) -1))))
2665
+ (cond
2666
+ ((and (>= constAddr 0) (<= (+ constAddr width) 4095) (= (% constAddr width) 0) (/= name "store-f64"))
2667
+ (when (and store (< rV 0)) (expression state right))
2668
+ (word state (+ 4043309983 (* (+ constAddr width) 1024))) ;; cmp x28, #(constAddr+width)
2669
+ (if (get state :trapSites nil)
2670
+ (memory-conditional-error state 1409286146 1) ;; b.hs safe
2671
+ (progn
2672
+ (js-call (get state :boundsErrors) :push (js-length (get state :words)))
2673
+ (word state 1409286147))) ;; b.lo bounds_trap
2674
+ (let ((scaled (/ constAddr width)))
2675
+ (if store
2676
+ (progn
2677
+ (word state (+ (cond ((= name "store-u8") 956302176)
2678
+ ((= name "store-u16") 2030044000)
2679
+ ((= name "store-u32") 3103785824)
2680
+ (t 4177527648))
2681
+ (* scaled 1024)
2682
+ (if (>= rV 0) rV 0)))
2683
+ (unless discard (memory-integer-register state 0 0)))
2684
+ (word state (+ (cond ((= name "load-u8") 960496480)
2685
+ ((= name "load-u16") 2034238304)
2686
+ ((= name "load-u32") 3107980128)
2687
+ ((= name "load-f64") 4248830816)
2688
+ (t 4181721952))
2689
+ (* scaled 1024))))))
2690
+ ((and pin (not store) (or (>= rA 0) baseImm))
2691
+ (let ((idx-reg (if (>= rA 0) rA
2692
+ (progn
2693
+ (word state (+ 285212681 (* (get baseImm :imm) 1024) (* (get baseImm :reg) 32))) ;; add w9, wBase, #imm
2694
+ 9))))
2695
+ (memory-emit-pinned-bounds state width idx-reg)
2696
+ (word state (+ (cond ((= name "load-u8") 945843040) ((= name "load-u16") 2019584864)
2697
+ ((= name "load-u32") 3093326688) ((= name "load-f64") 4234177376)
2698
+ (t 4167068512)) (* idx-reg 65536)))))
2699
+ ((and pin store (>= rV 0))
2700
+ (let ((idx-reg (cond
2701
+ ((>= rA 0) rA)
2702
+ (baseImm
2703
+ (word state (+ 285212681 (* (get baseImm :imm) 1024) (* (get baseImm :reg) 32))) ;; add w9, wBase, #imm
2704
+ 9)
2705
+ (t (expression state addr) 0))))
2706
+ (memory-emit-pinned-store state name width idx-reg rV discard)))
2707
+ ((and pin store no-debug (/= name "store-f64") (< rV 0)
2708
+ (or (>= rA 0) baseImm)
2709
+ (and (>= addrReg 0) (not (memory-expr-writes-reg-p state right addrReg))))
2710
+ (expression state right)
2711
+ (let ((idx-reg (if (>= rA 0) rA
2712
+ (progn
2713
+ (word state (+ 285212681 (* (get baseImm :imm) 1024) (* (get baseImm :reg) 32))) ;; add w9, wBase, #imm
2714
+ 9))))
2715
+ (memory-emit-pinned-store state name width idx-reg 0 discard)))
2716
+ (t
2717
+ (expression state addr)
2718
+ (when store
2719
+ (let ((move-temp (if (= name "store-f64") 509624322
2720
+ (if (or (= name "store-v64") (= name "store-bits64")) 2852127714 704644066))))
2721
+ (if (or (= (get right :kind) "literal") (= (get right :kind) "int-literal") (= (get right :kind) "local"))
2722
+ (progn
2723
+ (word state 704644065) ;; mov w1,w0
2724
+ (expression state right)
2725
+ (word state move-temp) ;; fmov d2,d0 / mov x2,x0 / mov w2,w0
2726
+ (word state 704709600)) ;; mov w0,w1
2727
+ (progn
2728
+ (push-value state "u32")
2729
+ (expression state right)
2730
+ (word state move-temp)
2731
+ (pop-value state "u32") (word state 704709600)))))
2732
+ ;; w0 zero-extends address; compare address+width in 64 bits before
2733
+ ;; adding the host pointer, including near-4GiB wrapping addresses.
2734
+ (word state (+ 2432696330 (* width 1024))) ;; add x10,x0,#width
2735
+ (if pin
2736
+ (progn
2737
+ (word state 3944481119) ;; cmp x10, x28
2738
+ (if (get state :trapSites nil)
2739
+ (memory-conditional-error state 1409286153 1) ;; b.ls safe
2740
+ (progn
2741
+ (js-call (get state :boundsErrors) :push (js-length (get state :words)))
2742
+ (word state 1409286152))) ;; b.hi bounds_trap
2743
+ (word state (cond ((= name "load-u8") 945843040) ((= name "load-u16") 2019584864)
2744
+ ((= name "load-u32") 3093326688) ((= name "load-f64") 4234177376)
2745
+ ((or (= name "load-v64") (= name "load-bits64")) 4167068512)
2746
+ ((= name "store-u8") 941648738) ((= name "store-u16") 2015390562)
2747
+ ((= name "store-u32") 3089132386) ((= name "store-f64") 4229983074)
2748
+ (t 4162874210))))
2749
+ (progn
2750
+ (word state 4181722731) (word state 3943367007)
2751
+ (memory-conditional-error state 1409286153 1) ;; b.ls safe
2752
+ (word state 4181721705) (word state 2332033321)
2753
+ (word state (cond ((= name "load-u8") 960495904) ((= name "load-u16") 2034237728)
2754
+ ((= name "load-u32") 3107979552) ((= name "load-f64") 4248830240)
2755
+ ((or (= name "load-v64") (= name "load-bits64")) 4181721376)
2756
+ ((= name "store-u8") 956301602) ((= name "store-u16") 2030043426)
2757
+ ((= name "store-u32") 3103785250) ((= name "store-f64") 4244635938)
2758
+ (t 4177527074)))))
2759
+ (when (and store (not discard)) (memory-integer-register state 0 0))))))
1850
2760
  ((= name "f64-from-bits64")
1851
2761
  (expression state (js-nth 0 children)) (word state 2657550336)) ;; fmov d0,x0
1852
2762
  ((= name "bits64-from-f64")
1853
2763
  (expression state (js-nth 0 children)) (word state 2657484800)) ;; fmov x0,d0
1854
2764
  ((or (= name "v64-from-u32") (= name "v64-low") (= name "bits64-low"))
1855
- (expression state (js-nth 0 children)) (word state 704644064)) ;; mov w0,w0
2765
+ (let* ((arg (js-nth 0 children))
2766
+ (atype (get arg :type))
2767
+ (r (if (and (not (get state :debugRanges nil))
2768
+ (or (memory-word-type atype) (memory-wide-type atype)))
2769
+ (local-register state arg atype) -1)))
2770
+ (if (>= r 0)
2771
+ (word state (+ 704644064 (* r 65536))) ;; mov w0, wR
2772
+ (progn (expression state arg) (word state 704644064))))) ;; mov w0,w0
1856
2773
  ((or (= name "v64-high") (= name "bits64-high"))
1857
- (expression state (js-nth 0 children)) (word state 3546348544)) ;; lsr x0,x0,#32
2774
+ (let* ((arg (js-nth 0 children))
2775
+ (r (if (and (not (get state :debugRanges nil))
2776
+ (memory-wide-type (get arg :type)))
2777
+ (local-register state arg "v64") -1)))
2778
+ (if (>= r 0)
2779
+ (word state (+ 3546348544 (* r 32))) ;; lsr x0, xR, #32
2780
+ (progn (expression state arg) (word state 3546348544))))) ;; lsr x0,x0,#32
1858
2781
  ((= name "v64-encode-bits64")
1859
2782
  (expression state (js-nth 0 children))
1860
2783
  (word state 3537895465) ;; movz x9,#1,lsl #48
@@ -1890,17 +2813,63 @@
1890
2813
  ((= name "memory-size") (word state 4181722720) (word state 3545299968))
1891
2814
  ((= name "memory-grow") (memory-grow state node))
1892
2815
  (t
1893
- (expression state (js-nth 0 children))
1894
- (let ((right (js-nth 1 children)))
1895
- (if (and (memory-word-type (get right :type))
1896
- (or (= (get right :kind) "literal") (= (get right :kind) "int-literal") (= (get right :kind) "local")))
1897
- (progn (word state 704644065) (expression state right)) ;; mov w1,w0
1898
- (progn (push-value state "u32") (expression state right) (pop-value state "u32"))))
1899
- (when (= name "word-div") (memory-conditional-error state 889192448 2))
1900
- (word state (cond ((= name "word-div") 448792608) ((= name "word-shr") 448799776)
1901
- ((= name "word-shl") 448798752) ((= name "word-and") 167772192)
1902
- ((= name "word-or") 704643104) ((= name "word-xor") 1241514016)
1903
- (t (backend-fail node "unsupported native memory32 opcode"))))))))
2816
+ (let* ((left (js-nth 0 children))
2817
+ (right (js-nth 1 children))
2818
+ (rkind (get right :kind))
2819
+ (rlit (and (memory-word-type (get right :type))
2820
+ (or (= rkind "literal") (= rkind "int-literal"))
2821
+ (= (value-type (get right :value)) "number")
2822
+ (Number/isInteger (get right :value))))
2823
+ (rval (when rlit (uint32 (get right :value))))
2824
+ (shift-k (if (and rlit (or (= name "word-shr") (= name "word-shl"))
2825
+ (>= rval 0) (< rval 32)) rval -1))
2826
+ (mask-bits (if (and rlit (= name "word-and"))
2827
+ (let ((bits 0) (mask 0))
2828
+ (while (and (< bits 31) (< mask rval))
2829
+ (setq mask (+ (* mask 2) 1) bits (1+ bits)))
2830
+ (if (= mask rval) bits 0))
2831
+ 0))
2832
+ (rL (if (and (not (get state :debugRanges nil))
2833
+ (memory-word-type (get left :type)))
2834
+ (local-register state left "u32") -1)))
2835
+ (cond
2836
+ ((and (= name "word-shr") (> shift-k 0)
2837
+ (= (get left :kind) "memory-op")
2838
+ (or (= (get left :value) "v64-high") (= (get left :value) "bits64-high")))
2839
+ (let* ((inner (js-nth 0 (get left :children)))
2840
+ (rIn (if (and (not (get state :debugRanges nil))
2841
+ (memory-wide-type (get inner :type)))
2842
+ (local-register state inner "v64") -1)))
2843
+ (when (< rIn 0) (expression state inner))
2844
+ (word state (+ 3544251392 (* (+ 32 shift-k) 65536) (if (>= rIn 0) (* rIn 32) 0)))))
2845
+ ((>= shift-k 0)
2846
+ (when (or (= shift-k 0) (< rL 0)) (expression state left))
2847
+ (when (> shift-k 0)
2848
+ (word state (if (= name "word-shr")
2849
+ (+ 1392540672 (* shift-k 65536) (if (>= rL 0) (* rL 32) 0))
2850
+ (+ 1392508928 (* (- 32 shift-k) 65536) (* (- 31 shift-k) 1024) (if (>= rL 0) (* rL 32) 0))))))
2851
+ ((> mask-bits 0)
2852
+ (when (< rL 0) (expression state left))
2853
+ (word state (+ 301989888 (* (1- mask-bits) 1024) (if (>= rL 0) (* rL 32) 0))))
2854
+ ((and (>= rL 0) (not (memory-expr-writes-reg-p state right rL)))
2855
+ (expression state right)
2856
+ (when (= name "word-div") (memory-conditional-error state 889192448 2))
2857
+ (word state (+ (cond ((= name "word-div") 448792576) ((= name "word-shr") 448799744)
2858
+ ((= name "word-shl") 448798720) ((= name "word-and") 167772160)
2859
+ ((= name "word-or") 704643072) ((= name "word-xor") 1241513984)
2860
+ (t (backend-fail node "unsupported native memory32 opcode")))
2861
+ (* rL 32))))
2862
+ (t
2863
+ (expression state left)
2864
+ (if (and (memory-word-type (get right :type))
2865
+ (or (= rkind "literal") (= rkind "int-literal") (= rkind "local")))
2866
+ (progn (word state 704644065) (expression state right)) ;; mov w1,w0
2867
+ (progn (push-value state "u32") (expression state right) (pop-value state "u32")))
2868
+ (when (= name "word-div") (memory-conditional-error state 889192448 2))
2869
+ (word state (cond ((= name "word-div") 448792608) ((= name "word-shr") 448799776)
2870
+ ((= name "word-shl") 448798752) ((= name "word-and") 167772192)
2871
+ ((= name "word-or") 704643104) ((= name "word-xor") 1241514016)
2872
+ (t (backend-fail node "unsupported native memory32 opcode")))))))))))
1904
2873
 
1905
2874
  (defun memory-word-type (type) (or (= type "u32") (= type "i32")))
1906
2875
  (defun memory-wide-type (type) (or (= type "v64") (= type "bits64")))
@@ -2009,15 +2978,21 @@
2009
2978
  (progn (emit-sp-address-x9 state offset) (word state (+ 3103785248 reg)))))))
2010
2979
 
2011
2980
  (defun memory-move-to-reg (state node reg wide)
2012
- ;; Memory locals live in frame slots. Load directly into the call's GPR
2013
- ;; while keeping the expression's normal debug range.
2014
- (let ((type (get node :type)))
2015
- (if (and (= (get node :kind) "local")
2016
- (or (memory-word-type type) (memory-wide-type type)))
2017
- (expression state node nil reg)
2018
- (progn
2019
- (expression state node)
2020
- (word state (+ (if wide 2852127712 704644064) reg))))))
2981
+ ;; Move locals and small constants directly into the call's GPR.
2982
+ (let ((kind (get node :kind)) (type (get node :type)))
2983
+ (cond
2984
+ ((and (= kind "local") (or (memory-word-type type) (memory-wide-type type)))
2985
+ (expression state node nil reg))
2986
+ ((and (not (get state :debugRanges nil)) (or (= kind "literal") (= kind "int-literal")) (memory-wide-type type))
2987
+ (let ((val (get node :value)))
2988
+ (if (and (not (nil? val)) (= (value-type val) "object"))
2989
+ (memory-wide-register state reg (uint32 (get val :low)) (uint32 (get val :high)))
2990
+ (memory-wide-register state reg (uint32 val) 0))))
2991
+ ((and (not (get state :debugRanges nil)) (or (= kind "literal") (= kind "int-literal")) (memory-word-type type))
2992
+ (memory-integer-register state reg (get node :value)))
2993
+ (t
2994
+ (expression state node)
2995
+ (word state (+ (if wide 2852127712 704644064) reg))))))
2021
2996
  (defun memory-call (state node)
2022
2997
  (let* ((args (get node :children)) (indirect (= (get node :kind) "call-indirect"))
2023
2998
  (count (- (js-length args) (if indirect 1 0)))
@@ -2088,7 +3063,10 @@
2088
3063
  (put state :depth (- (get state :depth) count)))
2089
3064
  (word state 3107986025)
2090
3065
  (let ((at (js-length (get state :words))))
2091
- (word state 889192457) (js-call (get state :memoryErrors) :push at))))
3066
+ (word state 889192457) (js-call (get state :memoryErrors) :push at))
3067
+ (when (and (get state :pinMemory nil)
3068
+ (or indirect (not (get state :mayGrow nil)) (js-nth (get node :value) (get state :mayGrow))))
3069
+ (word state 4181722748))))
2092
3070
 
2093
3071
  (defun memory-host-call (state node)
2094
3072
  (let* ((children (get node :children))
@@ -2109,7 +3087,9 @@
2109
3087
  ;; host value and sets the trap latch before this epilogue boundary.
2110
3088
  (word state 3107986025)
2111
3089
  (let ((at (js-length (get state :words))))
2112
- (word state 889192457) (js-call (get state :memoryErrors) :push at))))
3090
+ (word state 889192457) (js-call (get state :memoryErrors) :push at))
3091
+ (when (get state :pinMemory nil)
3092
+ (word state 4181722748))))
2113
3093
 
2114
3094
  (defun native-phase (observer phase count words branches calls)
2115
3095
  (when (= (value-type observer) "function")
@@ -2138,12 +3118,472 @@
2138
3118
  ;; data; building the final debug document no longer needs this root.
2139
3119
  (put code :words nil) bytes)))
2140
3120
 
3121
+ (defconst native-hot-leaf-names (new globalThis.Set (js-array
3122
+ "heap-block" "rt-root-scope-set-abs" "rt-root-scope-get-abs"
3123
+ "heap-ro-block" "heap-value-valid" "value-number-bits"
3124
+ "heap-number-from-word" "heap-number-word" "heap-number-uint" "heap-check"
3125
+ "heap-object-init-cell" "heap-string-cached-hash" "heap-object-builtin" "heap-key-equal"
3126
+ "heap-slot-index" "heap-handle" "heap-note-allocation"
3127
+ "val64-dynamic-p" "val64-inline-number-p" "val64-singleton-p"
3128
+ "heap-free-tree-maximum" "heap-free-tree-height"
3129
+ "heap-value-check" "heap-kind-check" "heap-base")))
3130
+
3131
+ (defun memory-leaf-candidate (function self-index)
3132
+ (let* ((body (get function :body))
3133
+ (locals (get function :locals))
3134
+ (fname (get function :name nil))
3135
+ (hot-leaf (and (= (value-type fname) "string") (js-call native-hot-leaf-names :has fname)))
3136
+ (max-nodes (if hot-leaf 120 52))
3137
+ (max-calls (if hot-leaf 4 3))
3138
+ (pending (js-array body))
3139
+ (count 0)
3140
+ (calls 0)
3141
+ (valid (<= (js-length locals) 12)))
3142
+ (while (and valid (> (js-length pending) 0))
3143
+ (let* ((node (js-call pending :pop))
3144
+ (kind (get node :kind))
3145
+ (children (get node :children))
3146
+ (i 0))
3147
+ (setq count (1+ count))
3148
+ (when (= kind "call")
3149
+ (setq calls (1+ calls))
3150
+ (when (or (= (get node :value) self-index) (> calls max-calls))
3151
+ (setq valid nil)))
3152
+ (if (or (> count max-nodes)
3153
+ (not valid)
3154
+ (= kind "call-indirect") (= kind "host-call")
3155
+ (= kind "while") (= kind "remainder-while") (= kind "guarded-while")
3156
+ (= kind "memory-data-copy"))
3157
+ (setq valid nil)
3158
+ (while (< i (js-length children))
3159
+ (js-call pending :push (js-nth i children))
3160
+ (setq i (1+ i))))))
3161
+ (when valid (js-object :function function :size count))))
3162
+
3163
+ (defun memory-uses-linear-p (node)
3164
+ (let ((pending (js-array node)) (found nil))
3165
+ (while (and (not found) (> (js-length pending) 0))
3166
+ (let* ((item (js-call pending :pop))
3167
+ (kind (get item :kind)))
3168
+ (if (and (= kind "memory-op")
3169
+ (let ((op (get item :value)))
3170
+ (or (> (memory-width op) 0) (= op "memory-grow"))))
3171
+ (setq found t)
3172
+ (let ((children (get item :children)) (i 0))
3173
+ (when (Array/isArray children)
3174
+ (while (< i (js-length children))
3175
+ (js-call pending :push (js-nth i children))
3176
+ (setq i (1+ i))))))))
3177
+ found))
3178
+
3179
+ (defun memory-has-inline-site (node candidates self-index)
3180
+ (let ((pending (js-array node)) (found nil))
3181
+ (while (and (not found) (> (js-length pending) 0))
3182
+ (let* ((item (js-call pending :pop))
3183
+ (kind (get item :kind))
3184
+ (children (get item :children))
3185
+ (i 0))
3186
+ (if (and (= kind "call")
3187
+ (/= (get item :value) self-index)
3188
+ (not (nil? (js-nth (get item :value) candidates))))
3189
+ (setq found t)
3190
+ (while (< i (js-length children))
3191
+ (js-call pending :push (js-nth i children))
3192
+ (setq i (1+ i))))))
3193
+ found))
3194
+
3195
+ (defun memory-shift-clone (node slot-map)
3196
+ (let* ((kind (get node :kind))
3197
+ (children (get node :children))
3198
+ (clen (js-length children))
3199
+ (is-local (or (= kind "local") (= kind "set") (= kind "initialize"))))
3200
+ (if (and (= clen 0) (not is-local))
3201
+ node
3202
+ (let ((new-children nil) (changed false) (i 0))
3203
+ (while (< i clen)
3204
+ (let* ((orig (js-nth i children))
3205
+ (cloned (memory-shift-clone orig slot-map)))
3206
+ (when (not (eq cloned orig))
3207
+ (unless new-children (setq new-children (js-call children :slice)))
3208
+ (put new-children i cloned)
3209
+ (setq changed t)))
3210
+ (setq i (1+ i)))
3211
+ (if (and (not changed) (not is-local))
3212
+ node
3213
+ (let ((copy (Object/assign (js-object) node)))
3214
+ (put copy :children (or new-children children))
3215
+ (when is-local
3216
+ (put copy :value (js-nth (get node :value) slot-map)))
3217
+ copy))))))
3218
+
3219
+ (defun memory-rewrite-inline (node candidates self-index locals budget pools cursors)
3220
+ (let* ((kind (get node :kind))
3221
+ (children (get node :children))
3222
+ (clen (js-length children))
3223
+ (control-flow (or (= kind "sequence") (= kind "if") (= kind "while")
3224
+ (= kind "remainder-while") (= kind "guarded-while")))
3225
+ (new-children nil)
3226
+ (changed false)
3227
+ (i 0))
3228
+ (if control-flow
3229
+ (let ((peak (Object/assign (js-object) cursors)))
3230
+ (while (< i clen)
3231
+ (let* ((orig (js-nth i children))
3232
+ (child-cursors (Object/assign (js-object) cursors))
3233
+ (rewritten (memory-rewrite-inline orig candidates self-index locals budget pools child-cursors))
3234
+ (pkeys (Object/keys child-cursors))
3235
+ (pi 0))
3236
+ (while (< pi (js-length pkeys))
3237
+ (let* ((pk (js-nth pi pkeys))
3238
+ (pv (get child-cursors pk 0)))
3239
+ (when (> pv (get peak pk 0)) (put peak pk pv)))
3240
+ (setq pi (1+ pi)))
3241
+ (when (not (eq rewritten orig))
3242
+ (unless new-children (setq new-children (js-call children :slice)))
3243
+ (put new-children i rewritten)
3244
+ (setq changed t)))
3245
+ (setq i (1+ i)))
3246
+ (let ((pkeys (Object/keys peak)) (pi 0))
3247
+ (while (< pi (js-length pkeys))
3248
+ (let ((pk (js-nth pi pkeys)))
3249
+ (put cursors pk (get peak pk)))
3250
+ (setq pi (1+ pi)))))
3251
+ (while (< i clen)
3252
+ (let* ((orig (js-nth i children))
3253
+ (rewritten (memory-rewrite-inline orig candidates self-index locals budget pools cursors)))
3254
+ (when (not (eq rewritten orig))
3255
+ (unless new-children (setq new-children (js-call children :slice)))
3256
+ (put new-children i rewritten)
3257
+ (setq changed t)))
3258
+ (setq i (1+ i))))
3259
+ (let* ((target-index (if (= kind "call") (get node :value) -1))
3260
+ (cand (when (and (= kind "call") (/= target-index self-index))
3261
+ (js-nth target-index candidates)))
3262
+ (callee (when cand (get cand :function)))
3263
+ (callee-locals (when callee (get callee :locals))))
3264
+ (if (and cand
3265
+ (<= (+ (get budget :added) (get cand :size)) (get budget :maxAdded)))
3266
+ (let ((tmp-cursors (Object/assign (js-object) cursors))
3267
+ (new-needed 0)
3268
+ (k 0))
3269
+ (while (< k (js-length callee-locals))
3270
+ (let* ((ltype (get (js-nth k callee-locals) :type))
3271
+ (pool (get pools ltype))
3272
+ (pos (get tmp-cursors ltype 0)))
3273
+ (put tmp-cursors ltype (1+ pos))
3274
+ (when (or (nil? pool) (>= pos (js-length pool)))
3275
+ (setq new-needed (1+ new-needed))))
3276
+ (setq k (1+ k)))
3277
+ (if (> (+ (js-length locals) new-needed) (get budget :maxLocals))
3278
+ (if changed
3279
+ (let ((copy (Object/assign (js-object) node)))
3280
+ (put copy :children new-children)
3281
+ copy)
3282
+ node)
3283
+ (let ((callee-arity (get callee :arity))
3284
+ (args (or new-children children))
3285
+ (slot-map (js-array))
3286
+ (seq-children (js-array)))
3287
+ (setq k 0)
3288
+ (while (< k (js-length callee-locals))
3289
+ (let* ((ltype (get (js-nth k callee-locals) :type))
3290
+ (pool (get pools ltype))
3291
+ (pos (get cursors ltype 0))
3292
+ (slot nil))
3293
+ (unless pool
3294
+ (setq pool (js-array))
3295
+ (put pools ltype pool))
3296
+ (if (< pos (js-length pool))
3297
+ (setq slot (js-nth pos pool))
3298
+ (progn
3299
+ (setq slot (js-length locals))
3300
+ (js-call locals :push (js-object :name (str "inline-" slot) :type ltype :index slot :noEntryInit t))
3301
+ (js-call pool :push slot)))
3302
+ (put cursors ltype (1+ pos))
3303
+ (js-call slot-map :push slot)
3304
+ (if (< k callee-arity)
3305
+ (js-call seq-children :push
3306
+ (js-object :kind "set" :type ltype :value slot
3307
+ :children (js-array (js-nth k args))))
3308
+ (let ((zero-node (js-object :kind "literal" :type ltype
3309
+ :value (if (memory-wide-type ltype) (js-object :low 0 :high 0) 0)
3310
+ :children (js-array))))
3311
+ (js-call seq-children :push
3312
+ (js-object :kind "set" :type ltype :value slot
3313
+ :children (js-array zero-node))))))
3314
+ (setq k (1+ k)))
3315
+ (js-call seq-children :push (memory-shift-clone (get callee :body) slot-map))
3316
+ (put budget :added (+ (get budget :added) (get cand :size)))
3317
+ (js-object :kind "sequence" :type (get node :type) :children seq-children))))
3318
+ (if changed
3319
+ (let ((copy (Object/assign (js-object) node)))
3320
+ (put copy :children new-children)
3321
+ copy)
3322
+ node)))))
3323
+
3324
+ (defun inline-memory-function (self-index raw-locals raw-body candidates)
3325
+ (when (memory-has-inline-site raw-body candidates self-index)
3326
+ (let* ((locals (js-call raw-locals :slice))
3327
+ (budget (js-object :added 0 :maxAdded 320 :maxLocals 80))
3328
+ (pools (js-object))
3329
+ (cursors (js-object))
3330
+ (body (memory-rewrite-inline raw-body candidates self-index locals budget pools cursors)))
3331
+ (js-object :locals locals :body body))))
3332
+
3333
+ (defun emit-memory-function-into (state function i raw-locals raw-body inline-candidates linux debug offsets)
3334
+ (let* ((inlined (when inline-candidates
3335
+ (inline-memory-function i raw-locals raw-body inline-candidates)))
3336
+ (locals (if inlined (get inlined :locals) raw-locals))
3337
+ (body (if inlined (get inlined :body) raw-body))
3338
+ (arity (get function :arity))
3339
+ (param-types (function-param-types function))
3340
+ (layout (compute-native-param-layout param-types linux))
3341
+ (slots (get layout :slots))
3342
+ (return-type (function-return-type function))
3343
+ (pin-memory (memory-uses-linear-p body))
3344
+ (allocation (allocate-memory-registers body locals pin-memory arity))
3345
+ (saved (get allocation :saved))
3346
+ (spilled (get allocation :spilled))
3347
+ (no-init (get allocation :noInit))
3348
+ (frame (* (js-call globalThis.Math :ceil (/ (+ (if pin-memory 3 1) saved spilled) 2)) 16)) (j 0)
3349
+ (save-regs (let ((regs (js-array 19)) (si 0))
3350
+ (while (< si saved) (js-call regs :push (+ 20 si)) (setq si (1+ si)))
3351
+ (when pin-memory (js-call regs :push 27) (js-call regs :push 28))
3352
+ regs)))
3353
+ (when offsets (js-call offsets :push (* (js-length (get state :words)) 4)))
3354
+ (put state :registers (get allocation :registers))
3355
+ (put state :localOffsets (get allocation :offsets))
3356
+ (put state :depth 0) (put state :memoryErrors (js-array))
3357
+ (put state :pinMemory pin-memory) (put state :boundsErrors (js-array))
3358
+ (when debug
3359
+ (put state :debugFunction (get function :name)) (put state :debugFunctionIndex i)
3360
+ (put state :debugDepth 0)
3361
+ (let* ((source (get function :source nil))
3362
+ (range (js-object :kind "function" :function (get function :name) :functionIndex i :depth -1
3363
+ :start (* 4 (js-length (get state :words))) :end 0
3364
+ :span (get function :span (if source (get source :span nil) nil)))))
3365
+ (put state :debugFunctionRange range) (js-call (get state :debugRanges) :push range)))
3366
+ (word state 2847898621) (word state 2432697341)
3367
+ (stack-adjust state frame t)
3368
+ (let ((si 0) (slen (js-length save-regs)))
3369
+ (while (< si slen)
3370
+ (if (< (1+ si) slen)
3371
+ (progn
3372
+ (word state (+ 2835350496 (* si 32768) (* (js-nth (1+ si) save-regs) 1024) (js-nth si save-regs)))
3373
+ (setq si (+ si 2)))
3374
+ (progn
3375
+ (word state (+ 4177527776 (* si 1024) (js-nth si save-regs)))
3376
+ (setq si (1+ si))))))
3377
+ (word state 2852127731) ;; mov x19,x0
3378
+ (when pin-memory
3379
+ (word state 4181721723) ;; ldr x27,[x19,#0]
3380
+ (word state 4181722748)) ;; ldr x28,[x19,#8]
3381
+ (while (< j arity)
3382
+ (let* ((slot (js-nth j slots))
3383
+ (kind (get slot :kind))
3384
+ (local-type (get (js-nth j locals) :type)))
3385
+ (cond
3386
+ ((= kind "gpr")
3387
+ ;; The incoming GPR is killed only by the spill. Store it
3388
+ ;; directly instead of COPY into w0/x0 first.
3389
+ nil)
3390
+ ((= kind "fpr")
3391
+ (unless (= (get slot :reg) 0)
3392
+ (word state (+ 509624320 (* (get slot :reg) 32)))))
3393
+ (t
3394
+ (integer-register state 9 (+ 16 (get slot :offset)) 0)
3395
+ (word state 2332623785) ;; add x9, x29, x9
3396
+ (word state (if (= local-type "f64") 4248830240
3397
+ (if (memory-wide-type local-type) 4181721376 3107979552)))))
3398
+ (local-access state j local-type t (if (= kind "gpr") (get slot :reg) 0))
3399
+ (setq j (1+ j))))
3400
+ ;; Typed memory IR follows WASM's zero-initialized local contract.
3401
+ ;; Managed error/frame slots can be read before any explicit store.
3402
+ (while (< j (js-length locals))
3403
+ (let* ((local (js-nth j locals))
3404
+ (type (get local :type)))
3405
+ (unless (or (get local :noEntryInit nil) (and no-init (js-nth j no-init)))
3406
+ (if (= type "f64")
3407
+ (progn (word state 2657551328) ;; fmov d0,xzr: positive zero
3408
+ (local-access state j type t))
3409
+ ;; Initialize the register or frame slot directly from WZR/XZR.
3410
+ (local-access state j type t 31))))
3411
+ (setq j (1+ j)))
3412
+ (expression state body)
3413
+ (let ((normal (jump state "jump")))
3414
+ (when (> (js-length (get state :boundsErrors)) 0)
3415
+ (patch-conditions state (get state :boundsErrors) (js-length (get state :words)))
3416
+ (memory-integer-register state 9 1)
3417
+ (word state 3103791721))
3418
+ (patch-conditions state (get state :memoryErrors) (js-length (get state :words)))
3419
+ (if (= return-type "f64")
3420
+ (word state 2657551328)
3421
+ (memory-integer-register state 0 0))
3422
+ (patch-jump state normal (js-length (get state :words))))
3423
+ (if (<= frame 4095)
3424
+ (word state (+ 3506439081 (* frame 1024))) ;; sub x9,fp,#frame
3425
+ (progn (integer-register state 9 frame 0) (word state 3406365609))) ;; sub x9,fp,x9
3426
+ (let ((si 0) (slen (js-length save-regs)))
3427
+ (while (< si slen)
3428
+ (if (< (1+ si) slen)
3429
+ (progn
3430
+ (word state (+ 2839544096 (* si 32768) (* (js-nth (1+ si) save-regs) 1024) (js-nth si save-regs)))
3431
+ (setq si (+ si 2)))
3432
+ (progn
3433
+ (word state (+ 4181721376 (* si 1024) (js-nth si save-regs)))
3434
+ (setq si (1+ si))))))
3435
+ (word state 2432697279) (word state 2831252477) (word state 3596551104)
3436
+ (when debug
3437
+ (put (get state :debugFunctionRange) :end (* 4 (js-length (get state :words)))))))
3438
+
3439
+ (defun pack-native-function-cache (fn-state &optional fn-name fn-arity fn-locals-len)
3440
+ (when (and (= (js-length (get fn-state :constants)) 0)
3441
+ (= (js-length (get fn-state :tableUses)) 0))
3442
+ (let* ((fn-branches (get fn-state :branches (js-array)))
3443
+ (bcount (js-length fn-branches))
3444
+ (all-fit t) (bi 0))
3445
+ (while (and all-fit (< bi bcount))
3446
+ (let ((b (js-nth bi fn-branches)))
3447
+ (unless (branch-fits (- (get b :target) (get b :at)) (branch-limit (get b :kind)))
3448
+ (setq all-fit false)))
3449
+ (setq bi (1+ bi)))
3450
+ (when all-fit
3451
+ (let* ((fn-calls (get fn-state :calls))
3452
+ (ccount (js-length fn-calls))
3453
+ (packed-calls (new globalThis.Uint32Array (* ccount 2)))
3454
+ (packed-branches (new globalThis.Uint32Array (* bcount 4)))
3455
+ (ci 0)
3456
+ (fn-ranges (get fn-state :debugRanges nil)))
3457
+ (while (< ci ccount)
3458
+ (let ((c (js-nth ci fn-calls))
3459
+ (off (* ci 2)))
3460
+ (put packed-calls off (get c :at))
3461
+ (put packed-calls (1+ off) (get c :target)))
3462
+ (setq ci (1+ ci)))
3463
+ (setq bi 0)
3464
+ (while (< bi bcount)
3465
+ (let ((b (js-nth bi fn-branches))
3466
+ (off (* bi 4)))
3467
+ (put packed-branches off (get b :at))
3468
+ (put packed-branches (1+ off) (get b :target))
3469
+ (put packed-branches (+ off 2) (get b :opcode))
3470
+ (put packed-branches (+ off 3) (get b :kind)))
3471
+ (setq bi (1+ bi)))
3472
+ (let ((entry (js-object
3473
+ :words (new globalThis.Uint32Array (get fn-state :words))
3474
+ :calls packed-calls
3475
+ :branches packed-branches)))
3476
+ (when fn-ranges
3477
+ (put entry :debugRanges fn-ranges)
3478
+ (put entry :functionName fn-name)
3479
+ (put entry :arity fn-arity)
3480
+ (put entry :localsLength fn-locals-len))
3481
+ entry))))))
3482
+
3483
+ (defun replay-native-function-cache (state entry offsets &optional function)
3484
+ (let* ((base (js-length (get state :words)))
3485
+ (packed-calls (get entry :calls))
3486
+ (call-len (js-length packed-calls))
3487
+ (calls (get state :calls))
3488
+ (ci 0)
3489
+ (packed-branches (get entry :branches))
3490
+ (cached-ranges (get entry :debugRanges nil))
3491
+ (dst-ranges (get state :debugRanges nil)))
3492
+ (js-call offsets :push (* base 4))
3493
+ (native-word-append-u32 (get state :words) (get entry :words))
3494
+ (while (< ci call-len)
3495
+ (js-call calls :push (js-object :at (+ base (aref packed-calls ci)) :target (aref packed-calls (1+ ci))))
3496
+ (setq ci (+ ci 2)))
3497
+ (when (> (js-length packed-branches) 0)
3498
+ (js-call (get state :cachedBranches) :push (js-object :base base :branches packed-branches)))
3499
+ (when (and cached-ranges dst-ranges)
3500
+ (let* ((base-bytes (* base 4))
3501
+ (dst-sites (get state :trapSites nil))
3502
+ (source (when function (get function :source nil)))
3503
+ (fn-span (when function (get function :span (if source (get source :span nil) nil))))
3504
+ (ri 0)
3505
+ (rlen (js-length cached-ranges)))
3506
+ (while (< ri rlen)
3507
+ (let* ((r (js-nth ri cached-ranges))
3508
+ (cloned (if (and (= ri 0) (= (get r :kind) "function") function)
3509
+ (Object/assign (js-object) r
3510
+ (js-object :start (+ base-bytes (get r :start))
3511
+ :end (+ base-bytes (get r :end))
3512
+ :span fn-span))
3513
+ (Object/assign (js-object) r
3514
+ (js-object :start (+ base-bytes (get r :start))
3515
+ :end (+ base-bytes (get r :end)))))))
3516
+ (js-call dst-ranges :push cloned)
3517
+ (when (and dst-sites (= (get cloned :kind) "trap-site"))
3518
+ (js-call dst-sites :push cloned)))
3519
+ (setq ri (1+ ri)))))))
3520
+
3521
+ (defun compute-memory-may-grow (functions)
3522
+ (let* ((count (js-length functions))
3523
+ (grow (js-call (new globalThis.Array count) :fill false))
3524
+ (edges (new globalThis.Array count))
3525
+ (i 0))
3526
+ (while (< i count)
3527
+ (let ((deps (js-array)) (seen (new globalThis.Set))
3528
+ (pending (js-array (get (js-nth i functions) :body))))
3529
+ (while (> (js-length pending) 0)
3530
+ (let* ((item (js-call pending :pop))
3531
+ (kind (get item :kind))
3532
+ (children (get item :children))
3533
+ (j 0))
3534
+ (cond
3535
+ ((or (= kind "host-call") (= kind "call-indirect")
3536
+ (and (= kind "memory-op") (= (get item :value) "memory-grow")))
3537
+ (put grow i t))
3538
+ ((= kind "call")
3539
+ (let ((callee (get item :value)))
3540
+ (unless (or (= callee i) (js-call seen :has callee))
3541
+ (js-call seen :add callee)
3542
+ (js-call deps :push callee)))))
3543
+ (when (Array/isArray children)
3544
+ (while (< j (js-length children))
3545
+ (js-call pending :push (js-nth j children))
3546
+ (setq j (1+ j))))))
3547
+ (put edges i deps))
3548
+ (setq i (1+ i)))
3549
+ (let ((changed t))
3550
+ (while changed
3551
+ (setq changed false i 0)
3552
+ (while (< i count)
3553
+ (unless (js-nth i grow)
3554
+ (let ((deps (js-nth i edges)) (k 0))
3555
+ (while (< k (js-length deps))
3556
+ (when (js-nth (js-nth k deps) grow)
3557
+ (put grow i t)
3558
+ (setq changed t k (js-length deps)))
3559
+ (setq k (1+ k)))))
3560
+ (setq i (1+ i)))))
3561
+ grow))
3562
+
2141
3563
  (defun memory-code (program &optional target options observer)
2142
3564
  (let* ((linux (= target "arm64-linux"))
2143
- (debug (get (or options (js-object)) :debug nil))
3565
+ (opts (or options (js-object)))
3566
+ (debug (get opts :debug nil))
2144
3567
  (functions (get program :functions))
2145
- (state (js-object :words (if (get (or options (js-object)) :packedWords nil) (native-word-buffer) (js-array)) :calls (js-array) :constants (js-array)
2146
- :compact nil :leaf nil :memory t :linux linux :registers (js-array)
3568
+ (rt-prefix-count (get program :rtPrefixCount 0))
3569
+ (optimize-memory (and (not debug) (>= (js-length functions) 16)))
3570
+ (may-grow (when optimize-memory (compute-memory-may-grow functions)))
3571
+ (mode-key (str (if linux "L" "M") ":" (if optimize-memory "O" "N")))
3572
+ (packed-words (or (get opts :packedWords nil) (= (get opts :binaryEncoding nil) "u16le-chunks")))
3573
+ (inline-candidates (when optimize-memory
3574
+ (let ((arr (new globalThis.Array (js-length functions))) (k 0))
3575
+ (while (< k (js-length functions))
3576
+ (let ((fn (js-nth k functions)))
3577
+ (if (< k rt-prefix-count)
3578
+ (let* ((rc (runtime-body-cache-get (get fn :body)))
3579
+ (leaf (if (and rc (get rc :frozen)) (get rc :leafCandidate) -1)))
3580
+ (put arr k (if (>= leaf 0) (js-object :function fn :size leaf) nil)))
3581
+ (put arr k (memory-leaf-candidate fn k))))
3582
+ (setq k (1+ k)))
3583
+ arr)))
3584
+ (state (js-object :words (if packed-words (native-word-buffer) (js-array)) :calls (js-array) :constants (js-array)
3585
+ :branches (js-array) :cachedBranches (js-array)
3586
+ :compact nil :leaf nil :memory t :optimizeMemory optimize-memory :mayGrow may-grow :linux linux :registers (js-array) :localOffsets nil
2147
3587
  :functions functions
2148
3588
  :signatureMap (get program :signatureMap nil)
2149
3589
  :debugRanges (if debug (js-array) nil)
@@ -2153,66 +3593,48 @@
2153
3593
  (offsets (js-array)) (i 0))
2154
3594
  (native-phase observer "native-code-start" 0 0 0 0)
2155
3595
  (while (< i (js-length functions))
2156
- (let* ((function (js-nth i functions)) (locals (get function :locals)) (arity (get function :arity))
2157
- (param-types (function-param-types function))
2158
- (layout (compute-native-param-layout param-types linux))
2159
- (slots (get layout :slots))
2160
- (return-type (function-return-type function))
2161
- (frame (* (js-call globalThis.Math :ceil (/ (1+ (js-length locals)) 2)) 16)) (j 0))
2162
- (js-call offsets :push (* (js-length (get state :words)) 4))
2163
- (put state :depth 0) (put state :memoryErrors (js-array))
2164
- (when debug
2165
- (put state :debugFunction (get function :name)) (put state :debugFunctionIndex i)
2166
- (put state :debugDepth 0)
2167
- (let* ((source (get function :source nil))
2168
- (range (js-object :kind "function" :function (get function :name) :functionIndex i :depth -1
2169
- :start (* 4 (js-length (get state :words))) :end 0
2170
- :span (get function :span (if source (get source :span nil) nil)))))
2171
- (put state :debugFunctionRange range) (js-call (get state :debugRanges) :push range)))
2172
- (word state 2847898621) (word state 2432697341)
2173
- (stack-adjust state frame t) (word state 4177527795) ;; save caller x19
2174
- (word state 2852127731) ;; mov x19,x0
2175
- (while (< j arity)
2176
- (let* ((slot (js-nth j slots))
2177
- (kind (get slot :kind))
2178
- (local-type (get (js-nth j locals) :type)))
2179
- (cond
2180
- ((= kind "gpr")
2181
- ;; The incoming GPR is killed only by the spill. Store it
2182
- ;; directly instead of COPY into w0/x0 first.
2183
- nil)
2184
- ((= kind "fpr")
2185
- (unless (= (get slot :reg) 0)
2186
- (word state (+ 509624320 (* (get slot :reg) 32)))))
2187
- (t
2188
- (integer-register state 9 (+ 16 (get slot :offset)) 0)
2189
- (word state 2332623785) ;; add x9, x29, x9
2190
- (word state (if (= local-type "f64") 4248830240
2191
- (if (memory-wide-type local-type) 4181721376 3107979552)))))
2192
- (local-access state j local-type t (if (= kind "gpr") (get slot :reg) 0))
2193
- (setq j (1+ j))))
2194
- ;; Typed memory IR follows WASM's zero-initialized local contract.
2195
- ;; Managed error/frame slots can be read before any explicit store.
2196
- (while (< j (js-length locals))
2197
- (let ((type (get (js-nth j locals) :type)))
2198
- (if (= type "f64")
2199
- (progn (word state 2657551328) ;; fmov d0,xzr: positive zero
2200
- (local-access state j type t))
2201
- ;; Integer and wide zeros are WZR/XZR stores. MOVZ w0,#0 only
2202
- ;; existed to feed the following STUR, and that COPY is dead.
2203
- (local-access state j type t 31))) (setq j (1+ j)))
2204
- (expression state (get function :body))
2205
- (let ((normal (jump state "jump")))
2206
- (patch-conditions state (get state :memoryErrors) (js-length (get state :words)))
2207
- (if (= return-type "f64")
2208
- (word state 2657551328)
2209
- (memory-integer-register state 0 0))
2210
- (patch-jump state normal (js-length (get state :words))))
2211
- (integer-register state 9 frame 0) (word state 3406365609) ;; sub x9,fp,x9
2212
- (word state 4181721395) ;; restore x19 independently of temporary stack depth
2213
- (word state 2432697279) (word state 2831252477) (word state 3596551104)
2214
- (when debug
2215
- (put (get state :debugFunctionRange) :end (* 4 (js-length (get state :words))))))
3596
+ (let* ((function (js-nth i functions))
3597
+ (raw-locals (get function :locals))
3598
+ (raw-body (get function :body))
3599
+ (cache-rec (when (< i rt-prefix-count)
3600
+ (runtime-body-cache-get raw-body)))
3601
+ (can-cache (and cache-rec (get cache-rec :frozen)
3602
+ (not (get cache-rec :hasIndirect))
3603
+ (not (get cache-rec :hasHostCall))
3604
+ (< (get cache-rec :maxCallTarget) rt-prefix-count))))
3605
+ (if can-cache
3606
+ (let* ((native-bodies (get cache-rec :nativeBodies))
3607
+ (fn-key (if debug
3608
+ (str (if linux "L" "M") ":N:D:" i ":" (js-length (get state :trapSites)))
3609
+ mode-key))
3610
+ (cached-entry (js-call native-bodies :get fn-key)))
3611
+ (if (and cached-entry
3612
+ (or (not debug)
3613
+ (and (= (get cached-entry :functionName) (get function :name))
3614
+ (= (get cached-entry :arity) (get function :arity))
3615
+ (= (get cached-entry :localsLength) (js-length raw-locals)))))
3616
+ (replay-native-function-cache state cached-entry offsets function)
3617
+ (let ((fn-state (js-object :words (js-array) :calls (js-array) :branches (js-array) :constants (js-array)
3618
+ :compact nil :leaf nil :memory t :optimizeMemory optimize-memory :mayGrow may-grow :linux linux
3619
+ :registers (js-array) :localOffsets nil
3620
+ :functions functions
3621
+ :signatureMap (get program :signatureMap nil)
3622
+ :debugRanges (if debug (js-array) nil)
3623
+ :trapSites (if debug (js-array) nil)
3624
+ :trapSiteBase (if debug (js-length (get state :trapSites)) 0)
3625
+ :hostCapabilities (get program :hostCapabilities (js-array))
3626
+ :table (get program :table nil) :tableUses (js-array))))
3627
+ (emit-memory-function-into fn-state function i raw-locals raw-body inline-candidates linux debug nil)
3628
+ (let ((packed (if debug
3629
+ (pack-native-function-cache fn-state (get function :name) (get function :arity) (js-length raw-locals))
3630
+ (pack-native-function-cache fn-state))))
3631
+ (if packed
3632
+ (progn
3633
+ (unless cached-entry
3634
+ (js-call native-bodies :set fn-key packed))
3635
+ (replay-native-function-cache state packed offsets function))
3636
+ (emit-memory-function-into state function i raw-locals raw-body inline-candidates linux debug offsets))))))
3637
+ (emit-memory-function-into state function i raw-locals raw-body inline-candidates linux debug offsets)))
2216
3638
  (setq i (1+ i))
2217
3639
  (when (and (= (value-type observer) "function") (= (% i 128) 0))
2218
3640
  (native-phase observer "native-code-progress" i (js-length (get state :words))
@@ -2329,6 +3751,13 @@
2329
3751
  ;; remember a node until every check and every descendant has succeeded.
2330
3752
  (when completed (js-call completed :set node t))))
2331
3753
 
3754
+ (defun native-locals-sig (locals)
3755
+ (let ((parts (js-array)) (len (js-length locals)) (i 0))
3756
+ (while (< i len)
3757
+ (js-call parts :push (get (js-nth i locals) :type))
3758
+ (setq i (1+ i)))
3759
+ (js-call parts :join ",")))
3760
+
2332
3761
  (defun memory-prepare-program-core (program fresh)
2333
3762
  (unless (= (get program :abi) "memory-u32") (backend-fail program "native memory emitter requires memory-u32 typed input"))
2334
3763
  (let* ((functions (get program :functions)) (exports (get program :exports))
@@ -2400,49 +3829,87 @@
2400
3829
  (while (< i (js-length table))
2401
3830
  (unless (memory-valid-index (js-nth i table) (js-length functions))
2402
3831
  (backend-fail program "native table entry is not a declared function index")) (setq i (1+ i))))
2403
- (setq i 0)
2404
- (while (< i (js-length functions))
2405
- (let ((function (js-nth i functions)))
2406
- (memory-validate-node (get function :body) function program allowed-indirect capabilities
2407
- (when fresh (new globalThis.WeakMap)))) (setq i (1+ i)))
2408
- ;; Existing managed aliases become direct typed wrappers; source and table
2409
- ;; indices remain unchanged, and no public canonical/typed IR is mutated.
2410
- (let ((combined (js-call functions :slice)) (public (js-call exports :slice))
2411
- (aliases (get program :aliases (js-array))) (seen (js-array)))
2412
- (unless (Array/isArray aliases) (backend-fail program "invalid native aliases"))
3832
+ (let* ((first-cached (when (> (js-length functions) 0)
3833
+ (runtime-body-cache-get (get (js-nth 0 functions) :body nil))))
3834
+ (rt-id (if first-cached (get first-cached :runtimeId 0) 0))
3835
+ (rt-count (if first-cached (get first-cached :runtimeFnCount 0) 0))
3836
+ (rt-prefix-ok (and (> rt-id 0) (> rt-count 0) (<= rt-count (js-length functions)))))
3837
+ (when rt-prefix-ok
3838
+ (let ((ri 0))
3839
+ (while (and rt-prefix-ok (< ri rt-count))
3840
+ (let* ((rfn (js-nth ri functions))
3841
+ (rc (runtime-body-cache-get (get rfn :body nil))))
3842
+ (unless (and rc
3843
+ (= (get rc :runtimeId) rt-id)
3844
+ (= (get rc :runtimeFnCount) rt-count)
3845
+ (= (get rc :fnIndex) ri)
3846
+ (= (get rc :arity) (get rfn :arity))
3847
+ (= (get rc :localsSig) (native-locals-sig (get rfn :locals))))
3848
+ (setq rt-prefix-ok false)))
3849
+ (setq ri (1+ ri)))))
2413
3850
  (setq i 0)
2414
- (while (< i (js-length public))
2415
- (unless (and (find-function functions (js-nth i public)) (not (js-call seen :includes (js-nth i public))))
2416
- (backend-fail program "invalid or duplicate native export"))
2417
- (js-call seen :push (js-nth i public)) (setq i (1+ i)))
2418
- (setq i 0)
2419
- (while (< i (js-length aliases))
2420
- (let* ((alias (js-nth i aliases)) (name (get alias :name))
2421
- (target (find-function functions (get alias :function))))
2422
- (unless (and target (= (value-type name) "string") (not (js-call seen :includes name)))
2423
- (backend-fail program "invalid or duplicate native alias"))
2424
- (js-call seen :push name) (js-call public :push name)
2425
- (if (= name (get target :name)) nil
2426
- (progn
2427
- (when (find-function functions name) (backend-fail program "native alias shadows a function"))
2428
- (let ((locals (js-array)) (arguments (js-array))
2429
- (target-locals (get target :locals))
2430
- (target-ret (get (get target :body) :type))
2431
- (j 0))
2432
- (while (< j (get target :arity))
2433
- (let ((param-type (get (js-nth j target-locals) :type)))
2434
- (js-call locals :push (js-object :name (str "argument-" j) :type param-type :index j))
2435
- (js-call arguments :push (js-object :kind "local" :type param-type :value j :children (js-array))))
2436
- (setq j (1+ j)))
2437
- (js-call combined :push (js-object :name name :index (js-length combined) :arity (get target :arity) :locals locals
2438
- :parameterTypes (function-param-types target) :resultType (function-return-type target)
2439
- :body (js-object :kind "call" :type target-ret :value (get target :index) :children arguments)))))))
3851
+ (while (< i (js-length functions))
3852
+ (let* ((function (js-nth i functions))
3853
+ (fn-body (get function :body))
3854
+ (c (when (and rt-prefix-ok (< i rt-count)) (runtime-body-cache-get fn-body)))
3855
+ (can-cache (and c (get c :frozen) (not (get c :hasIndirect))
3856
+ (not (get c :hasHostCall)) (< (get c :maxCallTarget) rt-count))))
3857
+ (if can-cache
3858
+ (unless (get c :nativeValidated)
3859
+ (memory-validate-node fn-body function program allowed-indirect capabilities
3860
+ (when fresh (new globalThis.WeakMap)))
3861
+ (put c :nativeValidated t))
3862
+ (memory-validate-node fn-body function program allowed-indirect capabilities
3863
+ (when fresh (new globalThis.WeakMap)))))
2440
3864
  (setq i (1+ i)))
2441
- (let ((prepared (js-object :abi "memory-u32" :filename (get program :filename) :functions combined :exports public :table table
2442
- :signatureMap signature-map)))
2443
- (when (> (js-length capabilities) 0) (put prepared :hostCapabilities capabilities))
2444
- (when (get program :roData nil) (put prepared :roData (get program :roData)))
2445
- prepared))))
3865
+ ;; Existing managed aliases become direct typed wrappers; source and table
3866
+ ;; indices remain unchanged, and no public canonical/typed IR is mutated.
3867
+ (let ((combined (js-call functions :slice)) (public (js-call exports :slice))
3868
+ (aliases (get program :aliases (js-array))) (seen (new globalThis.Set))
3869
+ (fn-by-name (new globalThis.Map)))
3870
+ (setq i 0)
3871
+ (while (< i (js-length functions))
3872
+ (let ((fn (js-nth i functions)))
3873
+ (unless (js-call fn-by-name :has (get fn :name))
3874
+ (js-call fn-by-name :set (get fn :name) fn)))
3875
+ (setq i (1+ i)))
3876
+ (unless (Array/isArray aliases) (backend-fail program "invalid native aliases"))
3877
+ (setq i 0)
3878
+ (while (< i (js-length public))
3879
+ (let ((pub-name (js-nth i public)))
3880
+ (unless (and (js-call fn-by-name :get pub-name) (not (js-call seen :has pub-name)))
3881
+ (backend-fail program "invalid or duplicate native export"))
3882
+ (js-call seen :add pub-name))
3883
+ (setq i (1+ i)))
3884
+ (setq i 0)
3885
+ (while (< i (js-length aliases))
3886
+ (let* ((alias (js-nth i aliases)) (name (get alias :name))
3887
+ (target (js-call fn-by-name :get (get alias :function))))
3888
+ (unless (and target (= (value-type name) "string") (not (js-call seen :has name)))
3889
+ (backend-fail program "invalid or duplicate native alias"))
3890
+ (js-call seen :add name) (js-call public :push name)
3891
+ (if (= name (get target :name)) nil
3892
+ (progn
3893
+ (when (js-call fn-by-name :get name) (backend-fail program "native alias shadows a function"))
3894
+ (let ((locals (js-array)) (arguments (js-array))
3895
+ (target-locals (get target :locals))
3896
+ (target-ret (get (get target :body) :type))
3897
+ (j 0))
3898
+ (while (< j (get target :arity))
3899
+ (let ((param-type (get (js-nth j target-locals) :type)))
3900
+ (js-call locals :push (js-object :name (str "argument-" j) :type param-type :index j))
3901
+ (js-call arguments :push (js-object :kind "local" :type param-type :value j :children (js-array))))
3902
+ (setq j (1+ j)))
3903
+ (js-call combined :push (js-object :name name :index (js-length combined) :arity (get target :arity) :locals locals
3904
+ :parameterTypes (function-param-types target) :resultType (function-return-type target)
3905
+ :body (js-object :kind "call" :type target-ret :value (get target :index) :children arguments)))))))
3906
+ (setq i (1+ i)))
3907
+ (let ((prepared (js-object :abi "memory-u32" :filename (get program :filename) :functions combined :exports public :table table
3908
+ :signatureMap signature-map)))
3909
+ (when rt-prefix-ok (put prepared :rtPrefixCount rt-count))
3910
+ (when (> (js-length capabilities) 0) (put prepared :hostCapabilities capabilities))
3911
+ (when (get program :roData nil) (put prepared :roData (get program :roData)))
3912
+ prepared)))))
2446
3913
 
2447
3914
  (defun memory-prepare-program (program)
2448
3915
  (memory-prepare-program-core program nil))