eliscript 0.0.1 → 0.0.2

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 (417) hide show
  1. package/README.md +59 -0
  2. package/bootstrap/README.md +26 -0
  3. package/bootstrap/backends/README.md +515 -0
  4. package/bootstrap/backends/async.eli +230 -0
  5. package/bootstrap/backends/binary.eli +173 -0
  6. package/bootstrap/backends/capabilities.eli +74 -0
  7. package/bootstrap/backends/elf.eli +246 -0
  8. package/bootstrap/backends/managed.eli +5976 -0
  9. package/bootstrap/backends/memory.eli +100 -0
  10. package/bootstrap/backends/native.eli +2000 -0
  11. package/bootstrap/backends/numeric.eli +1362 -0
  12. package/bootstrap/backends/optimize.eli +79 -0
  13. package/bootstrap/backends/regexp.eli +2263 -0
  14. package/bootstrap/backends/sha256.eli +138 -0
  15. package/bootstrap/backends/wasm.eli +1161 -0
  16. package/bootstrap/compiler/analyzer.eli +165 -77
  17. package/bootstrap/compiler/compiler.eli +43 -19
  18. package/bootstrap/compiler/emitter.eli +780 -170
  19. package/bootstrap/compiler/expander.eli +136 -45
  20. package/bootstrap/compiler/formatter.eli +8 -1
  21. package/bootstrap/compiler/ir.eli +148 -45
  22. package/bootstrap/compiler/lower.eli +29 -2
  23. package/bootstrap/compiler/project.eli +51 -21
  24. package/bootstrap/compiler/reader.eli +128 -36
  25. package/bootstrap/compiler/source-map.eli +46 -4
  26. package/bootstrap/compiler/symbol.eli +2 -1
  27. package/bootstrap/compiler/syntax.eli +47 -12
  28. package/bootstrap/compiler/transient-analysis.eli +40 -4
  29. package/bootstrap/host/check-cli-legacy.mjs +163 -0
  30. package/bootstrap/host/check-cli.mjs +59 -158
  31. package/bootstrap/host/check-cli.mjs.map +1 -0
  32. package/bootstrap/host/compiler-session.eli +1579 -0
  33. package/bootstrap/host/compiler-tools.eli +513 -0
  34. package/bootstrap/host/eliscript-cli-legacy.mjs +37 -0
  35. package/bootstrap/host/eliscript-cli.mjs +53 -33
  36. package/bootstrap/host/eliscript-cli.mjs.map +1 -0
  37. package/bootstrap/host/project-cli-legacy.mjs +295 -0
  38. package/bootstrap/host/project-cli.mjs +74 -289
  39. package/bootstrap/host/project-cli.mjs.map +1 -0
  40. package/bootstrap/host/project-legacy.mjs +1080 -0
  41. package/bootstrap/host/project.mjs +210 -1060
  42. package/bootstrap/host/project.mjs.map +1 -0
  43. package/bootstrap/runtime/UNICODE-LICENSE.txt +39 -0
  44. package/bootstrap/runtime/memory.eli +30379 -0
  45. package/browser/host.mjs +89 -0
  46. package/compiler/eliscript-analyzer.el +371 -4
  47. package/compiler/eliscript-cli.el +84 -1
  48. package/compiler/eliscript-emitter.el +1189 -256
  49. package/compiler/eliscript-expander.el +147 -10
  50. package/compiler/eliscript-ir-emitter.el +725 -208
  51. package/compiler/eliscript-lower.el +127 -1
  52. package/compiler/eliscript-portable.el +177 -51
  53. package/compiler/eliscript-project.el +20 -2
  54. package/compiler/eliscript-reader.el +141 -57
  55. package/compiler/eliscript-source-map.el +26 -12
  56. package/compiler/eliscript-transient-analysis.el +382 -19
  57. package/dist/backends/async.mjs +561 -0
  58. package/dist/backends/async.mjs.macro-origins.json +1 -0
  59. package/dist/backends/async.mjs.map +1 -0
  60. package/dist/backends/binary.mjs +381 -0
  61. package/dist/backends/binary.mjs.macro-origins.json +1 -0
  62. package/dist/backends/binary.mjs.map +1 -0
  63. package/dist/backends/capabilities.mjs +237 -0
  64. package/dist/backends/capabilities.mjs.macro-origins.json +1 -0
  65. package/dist/backends/capabilities.mjs.map +1 -0
  66. package/dist/backends/elf.mjs +540 -0
  67. package/dist/backends/elf.mjs.macro-origins.json +1 -0
  68. package/dist/backends/elf.mjs.map +1 -0
  69. package/dist/backends/managed.mjs +10616 -0
  70. package/dist/backends/managed.mjs.macro-origins.json +1 -0
  71. package/dist/backends/managed.mjs.map +1 -0
  72. package/dist/backends/memory.mjs +278 -0
  73. package/dist/backends/memory.mjs.macro-origins.json +1 -0
  74. package/dist/backends/memory.mjs.map +1 -0
  75. package/dist/backends/native.mjs +4099 -0
  76. package/dist/backends/native.mjs.macro-origins.json +1 -0
  77. package/dist/backends/native.mjs.map +1 -0
  78. package/dist/backends/numeric.mjs +2458 -0
  79. package/dist/backends/numeric.mjs.macro-origins.json +1 -0
  80. package/dist/backends/numeric.mjs.map +1 -0
  81. package/dist/backends/optimize.mjs +204 -0
  82. package/dist/backends/optimize.mjs.macro-origins.json +1 -0
  83. package/dist/backends/optimize.mjs.map +1 -0
  84. package/dist/backends/regexp.mjs +2033 -0
  85. package/dist/backends/regexp.mjs.macro-origins.json +1 -0
  86. package/dist/backends/regexp.mjs.map +1 -0
  87. package/dist/backends/sha256.mjs +310 -0
  88. package/dist/backends/sha256.mjs.macro-origins.json +1 -0
  89. package/dist/backends/sha256.mjs.map +1 -0
  90. package/dist/backends/wasm.mjs +2258 -0
  91. package/dist/backends/wasm.mjs.macro-origins.json +1 -0
  92. package/dist/backends/wasm.mjs.map +1 -0
  93. package/dist/bootstrap/analyzer.mjs +1908 -776
  94. package/dist/bootstrap/analyzer.mjs.macro-origins.json +1 -0
  95. package/dist/bootstrap/analyzer.mjs.map +1 -1
  96. package/dist/bootstrap/compiler.mjs +450 -142
  97. package/dist/bootstrap/compiler.mjs.macro-origins.json +1 -0
  98. package/dist/bootstrap/compiler.mjs.map +1 -1
  99. package/dist/bootstrap/emitter.mjs +3083 -909
  100. package/dist/bootstrap/emitter.mjs.macro-origins.json +1 -0
  101. package/dist/bootstrap/emitter.mjs.map +1 -1
  102. package/dist/bootstrap/expander.mjs +2887 -1233
  103. package/dist/bootstrap/expander.mjs.macro-origins.json +1 -0
  104. package/dist/bootstrap/expander.mjs.map +1 -1
  105. package/dist/bootstrap/formatter.mjs +582 -218
  106. package/dist/bootstrap/formatter.mjs.macro-origins.json +1 -0
  107. package/dist/bootstrap/formatter.mjs.map +1 -1
  108. package/dist/bootstrap/ir.mjs +670 -116
  109. package/dist/bootstrap/ir.mjs.macro-origins.json +1 -0
  110. package/dist/bootstrap/ir.mjs.map +1 -1
  111. package/dist/bootstrap/lower.mjs +866 -257
  112. package/dist/bootstrap/lower.mjs.macro-origins.json +1 -0
  113. package/dist/bootstrap/lower.mjs.map +1 -1
  114. package/dist/bootstrap/package.json +1 -0
  115. package/dist/bootstrap/project.mjs +1168 -498
  116. package/dist/bootstrap/project.mjs.macro-origins.json +1 -0
  117. package/dist/bootstrap/project.mjs.map +1 -1
  118. package/dist/bootstrap/reader.mjs +674 -175
  119. package/dist/bootstrap/reader.mjs.macro-origins.json +1 -0
  120. package/dist/bootstrap/reader.mjs.map +1 -1
  121. package/dist/bootstrap/runtime/math.mjs +1117 -0
  122. package/dist/bootstrap/runtime/math.mjs.map +1 -0
  123. package/dist/bootstrap/runtime/unicode-case-data.mjs +10125 -0
  124. package/dist/bootstrap/runtime/unicode-case-data.mjs.map +1 -0
  125. package/dist/bootstrap/runtime/unicode-case.mjs +281 -0
  126. package/dist/bootstrap/runtime/unicode-case.mjs.map +1 -0
  127. package/dist/bootstrap/source-map.mjs +430 -119
  128. package/dist/bootstrap/source-map.mjs.macro-origins.json +1 -0
  129. package/dist/bootstrap/source-map.mjs.map +1 -1
  130. package/dist/bootstrap/symbol.mjs +229 -53
  131. package/dist/bootstrap/symbol.mjs.macro-origins.json +1 -0
  132. package/dist/bootstrap/symbol.mjs.map +1 -1
  133. package/dist/bootstrap/syntax.mjs +205 -7
  134. package/dist/bootstrap/syntax.mjs.macro-origins.json +1 -0
  135. package/dist/bootstrap/syntax.mjs.map +1 -1
  136. package/dist/bootstrap/transient-analysis.mjs +1185 -489
  137. package/dist/bootstrap/transient-analysis.mjs.macro-origins.json +1 -0
  138. package/dist/bootstrap/transient-analysis.mjs.map +1 -1
  139. package/dist/browser/compiler.js +199 -6478
  140. package/dist/browser/host.mjs +446 -0
  141. package/dist/browser/loader.js +1 -0
  142. package/dist/browser/loader.mjs +231 -0
  143. package/dist/browser/worker.mjs +60 -0
  144. package/dist/compiler-artifact.json +1 -0
  145. package/dist/host/compiler-session.mjs +2980 -0
  146. package/dist/host/compiler-session.mjs.macro-origins.json +1 -0
  147. package/dist/host/compiler-session.mjs.map +1 -0
  148. package/dist/host/compiler-tools.mjs +1084 -0
  149. package/dist/host/compiler-tools.mjs.macro-origins.json +1 -0
  150. package/dist/host/compiler-tools.mjs.map +1 -0
  151. package/dist/literal-sources/runtime/core/collection-internals.eli +60 -0
  152. package/dist/literal-sources/runtime/core/collection.eli +579 -0
  153. package/dist/literal-sources/runtime/core/data-text.eli +290 -0
  154. package/dist/literal-sources/runtime/core/data.eli +22 -0
  155. package/dist/literal-sources/runtime/core/identifier.eli +44 -0
  156. package/dist/literal-sources/runtime/core/lazy-sequence.eli +131 -0
  157. package/dist/literal-sources/runtime/core/list.eli +19 -0
  158. package/dist/literal-sources/runtime/core/map.eli +9 -0
  159. package/dist/literal-sources/runtime/core/metadata-internals.eli +6 -0
  160. package/dist/literal-sources/runtime/core/metadata.eli +23 -0
  161. package/dist/literal-sources/runtime/core/object.eli +8 -0
  162. package/dist/literal-sources/runtime/core/order.eli +123 -0
  163. package/dist/literal-sources/runtime/core/persistent-kind.eli +28 -0
  164. package/dist/literal-sources/runtime/core/protocol-error.eli +32 -0
  165. package/dist/literal-sources/runtime/core/protocol.eli +28 -0
  166. package/dist/literal-sources/runtime/core/queue.eli +9 -0
  167. package/dist/literal-sources/runtime/core/record.eli +191 -0
  168. package/dist/literal-sources/runtime/core/regex.eli +158 -0
  169. package/dist/literal-sources/runtime/core/sequence.eli +305 -0
  170. package/dist/literal-sources/runtime/core/set-algebra.eli +28 -0
  171. package/dist/literal-sources/runtime/core/set.eli +9 -0
  172. package/dist/literal-sources/runtime/core/sorted.eli +408 -0
  173. package/dist/literal-sources/runtime/core/text.eli +14 -0
  174. package/dist/literal-sources/runtime/core/transducer.eli +268 -0
  175. package/dist/literal-sources/runtime/core/transient.eli +17 -0
  176. package/dist/literal-sources/runtime/core/truth.eli +4 -0
  177. package/dist/literal-sources/runtime/core/type.eli +232 -0
  178. package/dist/literal-sources/runtime/core/value-internals.eli +13 -0
  179. package/dist/literal-sources/runtime/core/value.eli +19 -0
  180. package/dist/literal-sources/runtime/core/vector.eli +20 -0
  181. package/dist/literal-sources/runtime/literals.eli +23 -0
  182. package/dist/literal-sources/runtime/testing/value.eli +6 -0
  183. package/dist/literal-sources/runtime/wire/http-rpc.eli +362 -0
  184. package/dist/literal-sources/runtime/wire/markdown.eli +275 -0
  185. package/dist/literal-sources/runtime/wire/schema.eli +193 -0
  186. package/dist/literal-sources/stdlib/bit.eli +36 -0
  187. package/dist/literal-sources/stdlib/core/data.eli +306 -0
  188. package/dist/literal-sources/stdlib/core/object.eli +89 -0
  189. package/dist/literal-sources/stdlib/core/protocol.eli +451 -0
  190. package/dist/literal-sources/stdlib/core/set.eli +312 -0
  191. package/dist/literal-sources/stdlib/core/text.eli +103 -0
  192. package/dist/literal-sources/stdlib/managed-literal-protocols.eli +483 -0
  193. package/dist/literal-sources/stdlib/managed-literals.eli +1414 -0
  194. package/dist/literal-sources/stdlib/managed-queue-state.eli +65 -0
  195. package/dist/literal-sources/stdlib/managed-sorted-state.eli +233 -0
  196. package/dist/literal-sources/stdlib/managed-transient-state.eli +348 -0
  197. package/dist/literal-sources/stdlib/managed-value-state.eli +153 -0
  198. package/dist/literal-sources/stdlib/managed-vector-view-state.eli +102 -0
  199. package/dist/literal-sources/stdlib/persistent-list.eli +145 -0
  200. package/dist/literal-sources/stdlib/persistent-map.eli +724 -0
  201. package/dist/literal-sources/stdlib/persistent-set.eli +236 -0
  202. package/dist/literal-sources/stdlib/persistent-vector.eli +359 -0
  203. package/dist/literal-sources/stdlib/value.eli +325 -0
  204. package/dist/package.json +10 -0
  205. package/dist/runtime/math.mjs +1116 -0
  206. package/dist/runtime/math.mjs.macro-origins.json +1 -0
  207. package/dist/runtime/math.mjs.map +1 -0
  208. package/dist/runtime/unicode-case-data.mjs +10124 -0
  209. package/dist/runtime/unicode-case-data.mjs.macro-origins.json +1 -0
  210. package/dist/runtime/unicode-case-data.mjs.map +1 -0
  211. package/dist/runtime/unicode-case.mjs +280 -0
  212. package/dist/runtime/unicode-case.mjs.macro-origins.json +1 -0
  213. package/dist/runtime/unicode-case.mjs.map +1 -0
  214. package/dist/runtime.eli +30379 -0
  215. package/dist/stage3-release.json +1 -0
  216. package/editor/README.md +1 -1
  217. package/editor/eliscript-mode.el +1 -1
  218. package/editor/eliscript-repl.el +1 -1
  219. package/package.json +97 -10
  220. package/platform/http.mjs +20 -0
  221. package/runtime/array-text.mjs +159 -0
  222. package/runtime/core/map-internals.mjs +27 -9
  223. package/runtime/core/object-impl.mjs +26 -17
  224. package/runtime/core/object-impl.mjs.map +1 -1
  225. package/runtime/core/protocol-impl.mjs +682 -255
  226. package/runtime/core/protocol-impl.mjs.map +1 -1
  227. package/runtime/core/protocol.mjs +4 -0
  228. package/runtime/core/record.mjs +23 -2
  229. package/runtime/core/regex.mjs +4 -0
  230. package/runtime/core/sorted.mjs +53 -0
  231. package/runtime/core/text-impl.mjs +167 -48
  232. package/runtime/core/text-impl.mjs.map +1 -1
  233. package/runtime/core/type.mjs +75 -3
  234. package/runtime/core/vector.mjs +6 -0
  235. package/runtime/math.eli +429 -0
  236. package/runtime/math.mjs +1116 -0
  237. package/runtime/math.mjs.map +1 -0
  238. package/runtime/number-radix.mjs +105 -0
  239. package/runtime/unicode-case-data.eli +29 -0
  240. package/runtime/unicode-case-data.mjs +10124 -0
  241. package/runtime/unicode-case-data.mjs.map +1 -0
  242. package/runtime/unicode-case.eli +100 -0
  243. package/runtime/unicode-case.mjs +280 -0
  244. package/runtime/unicode-case.mjs.map +1 -0
  245. package/runtime/wire/http-rpc.mjs +168 -0
  246. package/runtime/wire/markdown.mjs +196 -0
  247. package/runtime/wire/schema.mjs +109 -0
  248. package/stdlib/README.md +54 -0
  249. package/stdlib/core/protocol.eli +81 -17
  250. package/stdlib/core/seq.eli +1 -1
  251. package/stdlib/core/zipper.eli +114 -47
  252. package/stdlib/gepo.eli +12 -0
  253. package/stdlib/interop/js.eli +56 -15
  254. package/stdlib/managed-literal-protocols.eli +483 -0
  255. package/stdlib/managed-literals.eli +1414 -0
  256. package/stdlib/managed-protocol-error.eli +32 -0
  257. package/stdlib/managed-queue-state.eli +65 -0
  258. package/stdlib/managed-runtime-collection-internals.eli +60 -0
  259. package/stdlib/managed-runtime-collection.eli +579 -0
  260. package/stdlib/managed-runtime-data-text.eli +290 -0
  261. package/stdlib/managed-runtime-data.eli +22 -0
  262. package/stdlib/managed-runtime-identifier.eli +44 -0
  263. package/stdlib/managed-runtime-lazy-sequence.eli +131 -0
  264. package/stdlib/managed-runtime-list.eli +19 -0
  265. package/stdlib/managed-runtime-literals.eli +23 -0
  266. package/stdlib/managed-runtime-map.eli +9 -0
  267. package/stdlib/managed-runtime-metadata-internals.eli +6 -0
  268. package/stdlib/managed-runtime-metadata.eli +23 -0
  269. package/stdlib/managed-runtime-object.eli +8 -0
  270. package/stdlib/managed-runtime-order.eli +123 -0
  271. package/stdlib/managed-runtime-persistent-kind.eli +28 -0
  272. package/stdlib/managed-runtime-protocol.eli +28 -0
  273. package/stdlib/managed-runtime-queue.eli +9 -0
  274. package/stdlib/managed-runtime-record.eli +191 -0
  275. package/stdlib/managed-runtime-regex.eli +158 -0
  276. package/stdlib/managed-runtime-sequence.eli +305 -0
  277. package/stdlib/managed-runtime-set-algebra.eli +28 -0
  278. package/stdlib/managed-runtime-set.eli +9 -0
  279. package/stdlib/managed-runtime-sorted.eli +408 -0
  280. package/stdlib/managed-runtime-text.eli +14 -0
  281. package/stdlib/managed-runtime-transducer.eli +268 -0
  282. package/stdlib/managed-runtime-transient.eli +17 -0
  283. package/stdlib/managed-runtime-truth.eli +4 -0
  284. package/stdlib/managed-runtime-type.eli +232 -0
  285. package/stdlib/managed-runtime-value-internals.eli +13 -0
  286. package/stdlib/managed-runtime-value.eli +19 -0
  287. package/stdlib/managed-runtime-vector.eli +20 -0
  288. package/stdlib/managed-sorted-state.eli +233 -0
  289. package/stdlib/managed-testing-value.eli +6 -0
  290. package/stdlib/managed-transient-state.eli +348 -0
  291. package/stdlib/managed-value-state.eli +153 -0
  292. package/stdlib/managed-vector-view-state.eli +102 -0
  293. package/stdlib/managed-wire-http-rpc.eli +362 -0
  294. package/stdlib/managed-wire-markdown.eli +275 -0
  295. package/stdlib/managed-wire-schema.eli +193 -0
  296. package/stdlib/mdp.eli +7 -0
  297. package/stdlib/mds.eli +4 -0
  298. package/stdlib/persistent-vector.eli +23 -6
  299. package/tools/acceptance/check-legacy.mjs +687 -0
  300. package/tools/acceptance/check.eli +431 -0
  301. package/tools/acceptance/compile-evidence.el +49 -0
  302. package/tools/acceptance/finalize-legacy.mjs +498 -0
  303. package/tools/acceptance/finalize.eli +270 -0
  304. package/tools/backends/benchmark.mjs +175 -0
  305. package/tools/backends/build.mjs +135 -0
  306. package/tools/backends/compile.mjs +94 -0
  307. package/tools/backends/compiler-bootstrap.mjs +1001 -0
  308. package/tools/backends/compiler-browser.mjs +361 -0
  309. package/tools/backends/compiler-build.mjs +1628 -0
  310. package/tools/backends/compiler-cli-legacy.mjs +334 -0
  311. package/tools/backends/compiler-cli.mjs +29 -0
  312. package/tools/backends/compiler-corpus-bit.mjs +234 -0
  313. package/tools/backends/compiler-corpus-cli-adapters-legacy.mjs +362 -0
  314. package/tools/backends/compiler-corpus-cli-adapters.mjs +247 -0
  315. package/tools/backends/compiler-corpus-debugging.mjs +221 -0
  316. package/tools/backends/compiler-corpus-emitter.mjs +324 -0
  317. package/tools/backends/compiler-corpus-evaluation-adapters.mjs +65 -0
  318. package/tools/backends/compiler-corpus-execution-legacy.mjs +769 -0
  319. package/tools/backends/compiler-corpus-execution.mjs +3324 -0
  320. package/tools/backends/compiler-corpus-fuzz.mjs +229 -0
  321. package/tools/backends/compiler-corpus-generations.mjs +208 -0
  322. package/tools/backends/compiler-corpus-host-context-legacy.mjs +114 -0
  323. package/tools/backends/compiler-corpus-host-context.mjs +550 -0
  324. package/tools/backends/compiler-corpus-identity-host.mjs +553 -0
  325. package/tools/backends/compiler-corpus-identity-value-adapters-legacy.mjs +298 -0
  326. package/tools/backends/compiler-corpus-identity-value-adapters.mjs +69 -0
  327. package/tools/backends/compiler-corpus-isolated-exports.mjs +21 -0
  328. package/tools/backends/compiler-corpus-legacy.mjs +1155 -0
  329. package/tools/backends/compiler-corpus-literal-provider.mjs +282 -0
  330. package/tools/backends/compiler-corpus-managed-adapters-legacy.mjs +334 -0
  331. package/tools/backends/compiler-corpus-managed-adapters.mjs +937 -0
  332. package/tools/backends/compiler-corpus-managed-cases.mjs +179 -0
  333. package/tools/backends/compiler-corpus-managed-fixtures-legacy.mjs +879 -0
  334. package/tools/backends/compiler-corpus-managed-fixtures.mjs +1287 -0
  335. package/tools/backends/compiler-corpus-matchers.mjs +173 -0
  336. package/tools/backends/compiler-corpus-original-program.mjs +612 -0
  337. package/tools/backends/compiler-corpus-process.mjs +765 -0
  338. package/tools/backends/compiler-corpus-program-driver.mjs +306 -0
  339. package/tools/backends/compiler-corpus-project-adapters-legacy.mjs +497 -0
  340. package/tools/backends/compiler-corpus-project-adapters.mjs +340 -0
  341. package/tools/backends/compiler-corpus-project-entry-host.mjs +511 -0
  342. package/tools/backends/compiler-corpus-project-transport.mjs +151 -0
  343. package/tools/backends/compiler-corpus-raw-fixture-observer.mjs +1438 -0
  344. package/tools/backends/compiler-corpus-raw-language-fixtures.mjs +1456 -0
  345. package/tools/backends/compiler-corpus-raw-math-fixture.mjs +346 -0
  346. package/tools/backends/compiler-corpus-raw-special-fixtures.mjs +523 -0
  347. package/tools/backends/compiler-corpus-reference-legacy.mjs +263 -0
  348. package/tools/backends/compiler-corpus-reference.mjs +241 -0
  349. package/tools/backends/compiler-corpus-runtime-adapters-legacy.mjs +843 -0
  350. package/tools/backends/compiler-corpus-runtime-adapters.mjs +663 -0
  351. package/tools/backends/compiler-corpus-source-archive.mjs +206 -0
  352. package/tools/backends/compiler-corpus-tools-artifact-adapters.mjs +410 -0
  353. package/tools/backends/compiler-corpus-tools-cli.mjs +363 -0
  354. package/tools/backends/compiler-corpus-tools-reference.mjs +264 -0
  355. package/tools/backends/compiler-corpus.mjs +2054 -0
  356. package/tools/backends/compiler-debug.mjs +142 -0
  357. package/tools/backends/compiler-matrix.mjs +1955 -0
  358. package/tools/backends/compiler-metadata.mjs +203 -0
  359. package/tools/backends/compiler-package-browser.mjs +120 -0
  360. package/tools/backends/compiler-package-legacy.mjs +654 -0
  361. package/tools/backends/compiler-package.mjs +464 -0
  362. package/tools/backends/compiler-project.mjs +126 -0
  363. package/tools/backends/compiler-session.mjs +843 -0
  364. package/tools/backends/compiler-sha256.mjs +152 -0
  365. package/tools/backends/compiler-unicode-profile-host.mjs +196 -0
  366. package/tools/backends/compiler-worker.mjs +573 -0
  367. package/tools/backends/dist/bootstrap-js-stages.mjs +919 -0
  368. package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -0
  369. package/tools/backends/dist/managed-ir-inventory.mjs +488 -0
  370. package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -0
  371. package/tools/backends/dist/record-grid.mjs +1256 -0
  372. package/tools/backends/dist/record-grid.mjs.map +1 -0
  373. package/tools/backends/dist/release-cli.mjs +460 -0
  374. package/tools/backends/dist/release-cli.mjs.map +1 -0
  375. package/tools/backends/dist/run-artifact.mjs +517 -0
  376. package/tools/backends/dist/run-artifact.mjs.map +1 -0
  377. package/tools/backends/eliscript/compiler-corpus-bit.eli +77 -0
  378. package/tools/backends/eliscript/compiler-corpus-debugging.eli +70 -0
  379. package/tools/backends/eliscript/compiler-corpus-project-entry-host.eli +256 -0
  380. package/tools/backends/managed-foreign-binding.mjs +497 -0
  381. package/tools/backends/managed-host-legacy.mjs +1181 -0
  382. package/tools/backends/managed-host-lifecycle.mjs +310 -0
  383. package/tools/backends/managed-host-object-provider.mjs +155 -0
  384. package/tools/backends/managed-host-object-realm.mjs +797 -0
  385. package/tools/backends/managed-host-proxy.mjs +71 -0
  386. package/tools/backends/managed-host-runtime.mjs +5868 -0
  387. package/tools/backends/managed-host.mjs +716 -0
  388. package/tools/backends/managed-module-graph-legacy.mjs +308 -0
  389. package/tools/backends/managed-module-graph.mjs +694 -0
  390. package/tools/backends/managed-runtime-host-legacy.mjs +195 -0
  391. package/tools/backends/managed-runtime.mjs +939 -0
  392. package/tools/backends/managed-source-view-legacy.mjs +332 -0
  393. package/tools/backends/managed-source-view.mjs +959 -0
  394. package/tools/backends/memory-runtime.mjs +100 -0
  395. package/tools/backends/native-benchmark.mjs +88 -0
  396. package/tools/backends/native-managed-runtime.mjs +677 -0
  397. package/tools/backends/native-memory.mjs +768 -0
  398. package/tools/backends/native-node-memory.mjs +142 -0
  399. package/tools/backends/native-node-runner.mjs +99 -0
  400. package/tools/backends/native-node-watchdog.mjs +19 -0
  401. package/tools/backends/native-node-worker.mjs +75 -0
  402. package/tools/backends/stage3-targets.mjs +486 -0
  403. package/tools/bootstrap-command.el +13 -0
  404. package/tools/bootstrap-runtime.el +101 -0
  405. package/tools/bootstrap-seed.el +55 -0
  406. package/tools/project/check-cli.eli +31 -0
  407. package/tools/project/project-cli.eli +42 -0
  408. package/tools/project/project.eli +122 -0
  409. package/tools/runtime/unicode-case-profiles.mjs +13 -0
  410. package/tools/runtime/unicode-case-runtime.el +57 -0
  411. package/tools/testing/source-debugging-corpus-host.eli +16 -0
  412. package/tools/testing/source-debugging.eli +139 -0
  413. package/tools/testing/stdlib-bit-corpus-host.eli +21 -0
  414. package/tools/testing/stdlib-bit-corpus-host.mjs +118 -0
  415. package/tools/testing/stdlib-bit.eli +104 -0
  416. package/tools/testing/stdlib-bit.mjs +255 -0
  417. package/bin/eliscript-bootstrap +0 -15
@@ -0,0 +1,1362 @@
1
+ (module eliscript.backend.numeric
2
+ (import "../bootstrap/emitter.mjs" emit-module-numeric)
3
+ (import "./optimize.mjs" optimize-program)
4
+ ;; Explicit numeric-f64 ABI: required Number arguments and Number results.
5
+ ;; This is a typed vertical slice, not a replacement for the dynamic runtime.
6
+ (defun backend-fail (node message)
7
+ (let* ((record (or node (js-object)))
8
+ (span (or (get record :span nil) (js-object))))
9
+ (throw
10
+ (js-object
11
+ :message message
12
+ :eliscriptDiagnostic
13
+ (js-object :format "eliscript-diagnostic" :version 1
14
+ :code "ELI-B0001" :severity "error" :phase "numeric-backend"
15
+ :message message
16
+ :location (js-object :file (get span :filename (get record :filename "<ir>"))
17
+ :start (js-object :line (get span :line 1)
18
+ :column (get span :column 1))))))))
19
+
20
+ (defun find-local (scope name)
21
+ (let ((index (1- (js-length scope))) (found nil))
22
+ (while (and (>= index 0) (not found))
23
+ (let ((entry (js-nth index scope)))
24
+ (when (= (get entry :name) name) (setq found entry)))
25
+ (setq index (1- index)))
26
+ found))
27
+
28
+ (defun find-function (functions name)
29
+ (let ((index 0) (found nil))
30
+ (while (and (< index (js-length functions)) (not found))
31
+ (when (= (get (js-nth index functions) :name) name)
32
+ (setq found (js-nth index functions)))
33
+ (setq index (1+ index)))
34
+ found))
35
+
36
+ (defun typed (kind type children value)
37
+ (js-object :kind kind :type type :children children :value value))
38
+
39
+ (defun require-type (original expression type)
40
+ (unless (= (get expression :type) type)
41
+ (backend-fail original (str (if (= type "u32") "memory-u32" "numeric-f64") " ABI requires " type " operands")))
42
+ expression)
43
+
44
+ (defun typed-sequence (nodes scope state functions &optional discarded)
45
+ (let ((result (js-array)) (index 0) (type "i32"))
46
+ (while (< index (js-length nodes))
47
+ (let ((expression (lower-expression (js-nth index nodes) scope state functions
48
+ (or discarded (< index (1- (js-length nodes)))))))
49
+ (setq type (get expression :type))
50
+ (js-call result :push expression))
51
+ (setq index (1+ index)))
52
+ (typed "sequence" type result nil)))
53
+
54
+ (defun new-local (state name type)
55
+ (let* ((locals (get state :locals))
56
+ (entry (js-object :name name :type type :index (js-length locals))))
57
+ (js-call locals :push entry)
58
+ entry))
59
+
60
+ (defun lower-bindings (node scope state functions)
61
+ (let* ((properties (get node :properties))
62
+ (count (get properties :bindingCount))
63
+ (children (get node :children))
64
+ (inner (js-call scope :slice))
65
+ (initializers (js-array))
66
+ (pending (js-array))
67
+ (index 0))
68
+ (while (< index count)
69
+ (let ((binding (js-nth index children)))
70
+ (unless (and (= (get binding :kind) "lexical-binding")
71
+ (= (js-length (get binding :children)) 1))
72
+ (backend-fail binding "numeric-f64 ABI requires simple initialized bindings"))
73
+ (when (find-local pending (get binding :value))
74
+ (backend-fail binding "duplicate lexical binding"))
75
+ (let* ((init (lower-expression (js-nth 0 (get binding :children))
76
+ (if (get properties :sequential) inner scope) state functions))
77
+ (local (new-local state (get binding :value) (get init :type))))
78
+ (js-call initializers :push (typed "initialize" (get init :type) (js-array init)
79
+ (get local :index)))
80
+ (js-call pending :push local)
81
+ (when (get properties :sequential) (js-call inner :push local))))
82
+ (setq index (1+ index)))
83
+ (unless (get properties :sequential)
84
+ (let ((index 0))
85
+ (while (< index (js-length pending))
86
+ (js-call inner :push (js-nth index pending))
87
+ (setq index (1+ index)))))
88
+ (let ((body (typed-sequence (js-call children :slice count) inner state functions
89
+ (get state :discarded nil))))
90
+ (js-call initializers :push body)
91
+ (typed "sequence" (get body :type) initializers nil))))
92
+
93
+ (defun lower-expression (node scope state functions &optional discarded)
94
+ (let ((previous (get state :discarded nil)))
95
+ (put state :discarded discarded)
96
+ (let ((lowered (lower-expression-body node scope state functions))
97
+ (span (get node :span nil)))
98
+ (put state :discarded previous)
99
+ (when span (put lowered :span span))
100
+ lowered)))
101
+
102
+ (defun lower-conditional-chain (node scope state functions)
103
+ (let ((clauses (get node :children)) (lowered (js-array))
104
+ (discarded (get state :discarded nil)) (index 0)
105
+ (zero (typed "literal" "i32" (js-array) 0)))
106
+ (while (< index (js-length clauses))
107
+ (let* ((clause (js-nth index clauses)) (parts (get clause :children)))
108
+ (unless (and (= (get clause :kind) "conditional-clause") (> (js-length parts) 0))
109
+ (backend-fail clause "cond requires nonempty clauses"))
110
+ (let* ((test (js-nth 0 parts))
111
+ (condition (lower-expression test scope state functions))
112
+ (single (= (js-length parts) 1))
113
+ (saved (and single (new-local state "cond-test" (get condition :type))))
114
+ (body (if single
115
+ (typed "local" (get condition :type) (js-array) (get saved :index))
116
+ (typed-sequence (js-call parts :slice 1) scope state functions discarded))))
117
+ (when discarded (setq body (typed "sequence" "i32" (js-array body zero) nil)))
118
+ (js-call lowered :push
119
+ (js-object :source clause :test test :condition condition :saved saved :body body))))
120
+ (setq index (1+ index)))
121
+ (let ((result zero) (index (1- (js-length lowered))))
122
+ (while (>= index 0)
123
+ (let* ((entry (js-nth index lowered)) (test (get entry :test))
124
+ (condition (get entry :condition)) (saved (get entry :saved)) (body (get entry :body))
125
+ (always (and (= (get test :kind) "literal")
126
+ (= (value-type (get test :value)) "boolean") (= (get test :value) t))))
127
+ (if always (setq result body)
128
+ (progn
129
+ (require-type (get entry :source) result (get body :type))
130
+ (setq result (typed "if" (get body :type)
131
+ (js-array (if saved (typed "local" (get condition :type) (js-array) (get saved :index)) condition)
132
+ body result) nil))))
133
+ (when saved
134
+ (setq result (typed "sequence" (get result :type)
135
+ (js-array (typed "initialize" (get condition :type) (js-array condition) (get saved :index)) result) nil))))
136
+ (setq index (1- index)))
137
+ result)))
138
+
139
+ (defun lower-expression-body (node scope state functions)
140
+ (let ((kind (get node :kind)) (value (get node :value))
141
+ (children (get node :children)))
142
+ (cond
143
+ ((and (= kind "literal") (not (get node :properties nil)))
144
+ (cond ((= (value-type value) "number")
145
+ (let ((type (get state :scalarType "f64")))
146
+ (when (and (= type "u32")
147
+ (or (/= value (uint32 value)) (< value 0) (> value 4294967295)
148
+ (and (= value 0) (< (/ 1 value) 0))))
149
+ (backend-fail node "memory-u32 literals must be unsigned integers, excluding negative zero"))
150
+ (typed "literal" type (js-array) value)))
151
+ ((or (= (value-type value) "boolean") (nil? value))
152
+ (typed "literal" "i32" (js-array) (if value 1 0)))
153
+ (t (backend-fail node "numeric-f64 ABI does not support this literal"))))
154
+ ((= kind "reference")
155
+ (let ((local (find-local scope value)))
156
+ (unless local (backend-fail node (str "unknown numeric local: " value)))
157
+ (typed "local" (get local :type) (js-array) (get local :index))))
158
+ ((= kind "lexical-bindings") (lower-bindings node scope state functions))
159
+ ((= kind "sequence") (typed-sequence children scope state functions (get state :discarded nil)))
160
+ ((= kind "conditional-chain") (lower-conditional-chain node scope state functions))
161
+ ((= kind "assignment")
162
+ (let ((assignments (js-array)) (index 0) (type "i32"))
163
+ (while (< index (js-length children))
164
+ (let* ((pair (js-nth index children))
165
+ (local (find-local scope (get pair :value))))
166
+ (unless (and (= (get pair :kind) "assignment-pair") local)
167
+ (backend-fail pair "assignment must address an existing numeric local"))
168
+ (let ((rhs (lower-expression (js-nth 0 (get pair :children)) scope state functions)))
169
+ (require-type pair rhs (get local :type))
170
+ (setq type (get rhs :type))
171
+ (js-call assignments :push
172
+ (typed "set" type (js-array rhs) (get local :index)))))
173
+ (setq index (1+ index)))
174
+ (typed "sequence" type assignments nil)))
175
+ ((and (= kind "loop") (= value "while"))
176
+ (typed "while" "i32"
177
+ (js-array (lower-expression (js-nth 0 children) scope state functions)
178
+ (typed-sequence (js-call children :slice 1) scope state functions t)) nil))
179
+ ((and (= (get state :scalarType "f64") "u32") (= kind "conditional-sugar")
180
+ (or (= value "when") (= value "unless")))
181
+ (let* ((condition (lower-expression (js-nth 0 children) scope state functions))
182
+ (body (typed-sequence (js-call children :slice 1) scope state functions t))
183
+ (zero (typed "literal" "i32" (js-array) 0))
184
+ (yes (typed "sequence" "i32" (js-array body zero) nil)))
185
+ (typed "if" "i32" (if (= value "when") (js-array condition yes zero)
186
+ (js-array condition zero yes)) nil)))
187
+ ((and (= kind "conditional") (= (js-length children) 3))
188
+ (let* ((condition (lower-expression (js-nth 0 children) scope state functions))
189
+ (yes (lower-expression (js-nth 1 children) scope state functions (get state :discarded nil)))
190
+ (no (lower-expression (js-nth 2 children) scope state functions (get state :discarded nil))))
191
+ (when (get state :discarded nil)
192
+ (let ((zero (typed "literal" "i32" (js-array) 0)))
193
+ (setq yes (typed "sequence" "i32" (js-array yes zero) nil))
194
+ (setq no (typed "sequence" "i32" (js-array no zero) nil))))
195
+ (require-type node no (get yes :type))
196
+ (typed "if" (get yes :type) (js-array condition yes no) nil)))
197
+ ((and (= kind "short-circuit")
198
+ (or (= value "and") (= value "or"))
199
+ (> (js-length children) 0))
200
+ (let ((operands (js-array)) (index 0) (type nil))
201
+ (while (< index (js-length children))
202
+ (let ((operand (lower-expression (js-nth index children) scope state functions)))
203
+ (if type (require-type node operand type) (setq type (get operand :type)))
204
+ (js-call operands :push operand))
205
+ (setq index (1+ index)))
206
+ (typed value type operands nil)))
207
+ ((= kind "intrinsic")
208
+ (let ((operands (js-array)) (index 0) (operand-type (get state :scalarType "f64")))
209
+ (while (< index (js-length children))
210
+ (js-call operands :push (lower-expression (js-nth index children) scope state functions))
211
+ (setq index (1+ index)))
212
+ (when (and (> (js-length operands) 0) (= (get (js-nth 0 operands) :type) "f64"))
213
+ (setq operand-type "f64"))
214
+ (cond
215
+ ((and (= value "not") (= (js-length operands) 1))
216
+ (typed "not" "i32" operands nil))
217
+ ((and (or (= value "+") (= value "-") (= value "*") (= value "/"))
218
+ (<= (js-length operands) 2)
219
+ (or (> (js-length operands) 0) (= value "+") (= value "*")))
220
+ (let ((index 0))
221
+ (while (< index (js-length operands))
222
+ (require-type node (js-nth index operands) operand-type)
223
+ (setq index (1+ index))))
224
+ (when (and (= operand-type "u32") (= value "/"))
225
+ (backend-fail node "memory-u32 division requires explicit word-div"))
226
+ (typed (if (= operand-type "u32") "word-arithmetic" "arithmetic")
227
+ operand-type operands value))
228
+ ((and (or (= value "1+") (= value "1-")) (= (js-length operands) 1))
229
+ (require-type node (js-nth 0 operands) operand-type)
230
+ (typed (if (= operand-type "u32") "word-arithmetic" "arithmetic")
231
+ operand-type operands value))
232
+ ((and (or (= value "=") (and (= (get state :scalarType "f64") "u32") (= value "/=")) (= value "<") (= value "<=")
233
+ (= value ">") (= value ">=")) (= (js-length operands) 2))
234
+ (require-type node (js-nth 0 operands) operand-type)
235
+ (require-type node (js-nth 1 operands) operand-type)
236
+ (typed (if (= operand-type "u32") "word-compare" "compare") "i32" operands value))
237
+ (t (backend-fail node (str "unsupported numeric intrinsic: " value))))))
238
+ ((and (= kind "call") (> (js-length children) 0)
239
+ (= (get (js-nth 0 children) :kind) "reference"))
240
+ (let* ((name (get (js-nth 0 children) :value))
241
+ (capability (find-function (get state :capabilities (js-array)) name))
242
+ (function (or capability (find-function functions name)))
243
+ (arguments (js-array)) (index 1))
244
+ (when (find-local scope name)
245
+ (backend-fail node "numeric calls cannot resolve a shadowed function"))
246
+ (unless function (backend-fail node (str "unknown numeric function: " name)))
247
+ (unless (= (1- (js-length children)) (get function :arity))
248
+ (backend-fail node "numeric call arity mismatch"))
249
+ (if (and capability (get capability :staticLiteral nil))
250
+ (let ((literal (js-nth 1 children)))
251
+ (unless (and (= (get literal :kind) "literal") (nil? (get literal :properties nil))
252
+ (= (value-type (get literal :value)) "number"))
253
+ (backend-fail node "f64-literal requires one static Number literal"))
254
+ (typed "literal" "f64" (js-array) (get literal :value)))
255
+ (if (and capability (get capability :staticData nil))
256
+ (let ((literal (js-nth 2 children))
257
+ (destination (require-type node
258
+ (lower-expression (js-nth 1 children) scope state functions) "u32")))
259
+ (unless (and (= (get literal :kind) "literal") (nil? (get literal :properties nil))
260
+ (= (value-type (get literal :value)) "string"))
261
+ (backend-fail node "copy-static-u16 requires a static UTF16 string"))
262
+ (typed "memory-data-copy" "u32" (js-array destination) (get literal :value)))
263
+ (progn
264
+ (while (< index (js-length children))
265
+ (js-call arguments :push
266
+ (require-type node (lower-expression (js-nth index children) scope state functions)
267
+ (if (and capability (get capability :parameterTypes nil))
268
+ (js-nth (1- index) (get capability :parameterTypes)) (get state :scalarType "f64"))))
269
+ (setq index (1+ index)))
270
+ (typed (if capability "memory-op" "call")
271
+ (if capability (get capability :resultType (get state :scalarType "f64")) (get state :scalarType "f64"))
272
+ arguments (if capability name (get function :index))))))))
273
+ (t (backend-fail node (str "unsupported numeric IR: " kind ":" value))))))
274
+
275
+ (defun collect-declarations (nodes declarations)
276
+ (let ((index 0))
277
+ (while (< index (js-length nodes))
278
+ (let ((node (js-nth index nodes)))
279
+ (if (= (get node :kind) "module-declaration")
280
+ (collect-declarations (get node :children) declarations)
281
+ (js-call declarations :push node)))
282
+ (setq index (1+ index))))
283
+ declarations)
284
+
285
+ (defun lower-scalar-program (program abi allowed-capabilities)
286
+ (unless (or (= abi "numeric-f64") (= abi "memory-u32"))
287
+ (backend-fail program "unsupported scalar ABI"))
288
+ (let ((scalar-type (if (= abi "memory-u32") "u32" "f64"))
289
+ (capabilities (js-array))
290
+ (functions (js-array)) (exports (js-array))
291
+ (nodes (collect-declarations (get program :body) (js-array)))
292
+ (index 0))
293
+ (while (< index (js-length nodes))
294
+ (let ((node (js-nth index nodes)))
295
+ (cond
296
+ ((and (= abi "memory-u32") (= (get node :kind) "import-declaration"))
297
+ (unless (= (get node :value) "eliscript:memory32")
298
+ (backend-fail node "memory-u32 accepts only the memory32 capability import"))
299
+ (let ((position 0))
300
+ (while (< position (js-length (get node :children)))
301
+ (let* ((binding (js-nth position (get node :children)))
302
+ (capability (find-function allowed-capabilities (get binding :value))))
303
+ (unless (and (= (get binding :kind) "import-named") capability
304
+ (not (find-function capabilities (get binding :value))))
305
+ (backend-fail binding "unknown, renamed or duplicate memory32 capability"))
306
+ (js-call capabilities :push capability))
307
+ (setq position (1+ position)))))
308
+ ((= (get node :kind) "export-declaration")
309
+ (let ((position 0))
310
+ (while (< position (js-length (get node :children)))
311
+ (let ((child (js-nth position (get node :children))))
312
+ (unless (= (get child :kind) "reference")
313
+ (backend-fail child "numeric exports require named functions"))
314
+ (js-call exports :push (get child :value)))
315
+ (setq position (1+ position)))))
316
+ ((and (= (get node :kind) "function-declaration")
317
+ (not (get (get node :properties) :async)))
318
+ (when (find-function functions (get node :value))
319
+ (backend-fail node "duplicate numeric function"))
320
+ (js-call functions :push
321
+ (js-object :name (get node :value) :index (js-length functions)
322
+ :arity (get (get node :properties) :parameterCount) :source node)))
323
+ (t (backend-fail node "numeric-f64 ABI requires synchronous functions and exports"))))
324
+ (setq index (1+ index)))
325
+ (when (= (js-length functions) 0)
326
+ (backend-fail program "numeric-f64 ABI requires at least one function"))
327
+ (setq index 0)
328
+ (while (< index (js-length functions))
329
+ (when (find-function capabilities (get (js-nth index functions) :name))
330
+ (backend-fail (get (js-nth index functions) :source) "function shadows memory32 capability"))
331
+ (let* ((function (js-nth index functions))
332
+ (node (get function :source))
333
+ (children (get node :children))
334
+ (arity (get function :arity))
335
+ (state (js-object :locals (js-array) :scalarType scalar-type :capabilities capabilities))
336
+ (scope (js-array)) (position 0))
337
+ (while (< position arity)
338
+ (let ((parameter (js-nth position children)))
339
+ (unless (and (= (get parameter :kind) "parameter-binding")
340
+ (= (get (get parameter :properties) :parameterKind) "required")
341
+ (not (find-local scope (get parameter :value))))
342
+ (backend-fail parameter "numeric-f64 ABI requires distinct required parameters"))
343
+ (js-call scope :push (new-local state (get parameter :value) scalar-type)))
344
+ (setq position (1+ position)))
345
+ (let ((body (typed-sequence (js-call children :slice arity) scope state functions)))
346
+ (require-type node body scalar-type)
347
+ (put function :body body)
348
+ (put function :locals (get state :locals))))
349
+ (setq index (1+ index)))
350
+ (setq index 0)
351
+ (while (< index (js-length exports))
352
+ (unless (find-function functions (js-nth index exports))
353
+ (backend-fail program (str "unknown numeric export: " (js-nth index exports))))
354
+ (let ((prior 0))
355
+ (while (< prior index)
356
+ (when (= (js-nth prior exports) (js-nth index exports))
357
+ (backend-fail program "duplicate numeric export"))
358
+ (setq prior (1+ prior))))
359
+ (setq index (1+ index)))
360
+ (js-object :abi abi :filename (get program :filename)
361
+ :functions functions :exports exports)))
362
+
363
+ (defun lower-numeric-program (program)
364
+ (lower-scalar-program program "numeric-f64" (js-array)))
365
+
366
+ (defun tree-size (node)
367
+ (let ((size 1) (children (get node :children)) (index 0))
368
+ (while (< index (js-length children))
369
+ (setq size (+ size (tree-size (js-nth index children))))
370
+ (setq index (1+ index)))
371
+ size))
372
+
373
+ (defun contains-kind (node kind)
374
+ (let ((found (or (= (get node :kind) kind)
375
+ (and (= kind "while") (or (= (get node :kind) "remainder-while")
376
+ (= (get node :kind) "guarded-while"))))) (index 0))
377
+ (while (and (not found) (< index (js-length (get node :children))))
378
+ (setq found (contains-kind (js-nth index (get node :children)) kind))
379
+ (setq index (1+ index)))
380
+ found))
381
+
382
+ (defun writes-local (node local)
383
+ (let ((count (if (and (or (= (get node :kind) "set") (= (get node :kind) "initialize"))
384
+ (= (get node :value) local)) 1 0))
385
+ (index 0))
386
+ (while (< index (js-length (get node :children)))
387
+ (setq count (+ count (writes-local (js-nth index (get node :children)) local)))
388
+ (setq index (1+ index)))
389
+ count))
390
+
391
+ (defun numeric-pure (node)
392
+ (let ((kind (get node :kind)) (index 0) (pure t))
393
+ (unless (or (= kind "literal") (= kind "local") (= kind "arithmetic")
394
+ (= kind "compare") (= kind "not") (= kind "int-number")
395
+ (= kind "int-literal") (= kind "int-add") (= kind "int-sub") (= kind "int-and")
396
+ (= kind "int-compare"))
397
+ (setq pure nil))
398
+ (while (and pure (< index (js-length (get node :children))))
399
+ (setq pure (numeric-pure (js-nth index (get node :children))))
400
+ (setq index (1+ index)))
401
+ pure))
402
+
403
+ (defun copy-offset (node offset aliases)
404
+ (let* ((kind (get node :kind)) (value (get node :value))
405
+ (children (js-array)) (index 0))
406
+ (while (< index (js-length (get node :children)))
407
+ (js-call children :push (copy-offset (js-nth index (get node :children)) offset aliases))
408
+ (setq index (1+ index)))
409
+ (if (and (= kind "local") (< value (js-length aliases)) (js-nth value aliases))
410
+ (js-nth value aliases)
411
+ (typed kind (get node :type) children
412
+ (if (or (= kind "local") (= kind "set") (= kind "initialize")) (+ offset value) value)))))
413
+
414
+ (defun inline-numeric-node (node functions locals)
415
+ (let ((children (js-array)) (index 0))
416
+ (while (< index (js-length (get node :children)))
417
+ (js-call children :push (inline-numeric-node (js-nth index (get node :children)) functions locals))
418
+ (setq index (1+ index)))
419
+ (if (= (get node :kind) "call")
420
+ (let* ((callee (js-nth (get node :value) functions))
421
+ (body (get callee :body)))
422
+ ;; Leaf-only expansion excludes both direct and mutual recursion.
423
+ (if (and (not (contains-kind body "call"))
424
+ (<= (tree-size body) (get callee :inlineLimit 128)))
425
+ (let ((offset (js-length locals)) (aliases (js-array))
426
+ (sequence (js-array)) (pure t) (index 0))
427
+ (while (< index (js-length children))
428
+ (unless (numeric-pure (js-nth index children)) (setq pure nil))
429
+ (setq index (1+ index)))
430
+ (setq index 0)
431
+ (while (< index (js-length (get callee :locals)))
432
+ (let ((entry (js-nth index (get callee :locals))))
433
+ (js-call locals :push (js-object :name (get entry :name)
434
+ :index (js-length locals) :type (get entry :type)
435
+ :boundedInteger (get entry :boundedInteger nil))))
436
+ (setq index (1+ index)))
437
+ (setq index 0)
438
+ (while (< index (get callee :arity))
439
+ (let* ((argument (js-nth index children))
440
+ (alias (and pure (= (writes-local body index) 0)
441
+ (or (= (get argument :kind) "local") (= (get argument :kind) "literal")))))
442
+ (js-call aliases :push (if alias argument nil))
443
+ (unless alias (js-call sequence :push
444
+ (typed "initialize" "f64" (js-array argument) (+ offset index)))))
445
+ (setq index (1+ index)))
446
+ (js-call sequence :push (copy-offset body offset aliases))
447
+ (typed "sequence" "f64" sequence nil))
448
+ (typed "call" "f64" children (get node :value))))
449
+ (typed (get node :kind) (get node :type) children (get node :value)))))
450
+
451
+ (defun loop-invariant (node loop)
452
+ (let ((kind (get node :kind)) (index 0) (invariant (numeric-pure node)))
453
+ (when (and (= kind "local") (> (writes-local loop (get node :value)) 0))
454
+ (setq invariant nil))
455
+ (while (and invariant (< index (js-length (get node :children))))
456
+ (setq invariant (loop-invariant (js-nth index (get node :children)) loop))
457
+ (setq index (1+ index)))
458
+ invariant))
459
+
460
+ (defun lift-invariants (node loop prelude)
461
+ (if (and (= (get node :kind) "initialize")
462
+ (= (writes-local loop (get node :value)) 1)
463
+ (loop-invariant (js-nth 0 (get node :children)) loop))
464
+ (progn
465
+ ;; Lexical locals are initialized before reads. Only pure RHSs
466
+ ;; assigned at one site may move; calls and nontermination stay put.
467
+ (js-call prelude :push node)
468
+ (typed "local" (get node :type) (js-array) (get node :value)))
469
+ (let ((children (js-array)) (index 0))
470
+ (while (< index (js-length (get node :children)))
471
+ (js-call children :push (lift-invariants (js-nth index (get node :children)) loop prelude))
472
+ (setq index (1+ index)))
473
+ (typed (get node :kind) (get node :type) children (get node :value)))))
474
+
475
+ (defun optimize-numeric-loops (node)
476
+ (let ((children (js-array)) (index 0))
477
+ (while (< index (js-length (get node :children)))
478
+ (js-call children :push (optimize-numeric-loops (js-nth index (get node :children))))
479
+ (setq index (1+ index)))
480
+ (let ((copy (typed (get node :kind) (get node :type) children (get node :value))))
481
+ (if (= (get copy :kind) "while")
482
+ (let* ((prelude (js-array)) (lifted (lift-invariants copy copy prelude)))
483
+ (js-call prelude :push lifted)
484
+ (typed "sequence" (get copy :type) prelude nil))
485
+ copy))))
486
+
487
+ (defun square-local (node)
488
+ (if (and (= (get node :kind) "arithmetic") (= (get node :value) "*")
489
+ (= (js-length (get node :children)) 2)
490
+ (= (get (js-nth 0 (get node :children)) :kind) "local")
491
+ (= (get (js-nth 1 (get node :children)) :kind) "local")
492
+ (= (get (js-nth 0 (get node :children)) :value)
493
+ (get (js-nth 1 (get node :children)) :value)))
494
+ (get (js-nth 0 (get node :children)) :value) -1))
495
+
496
+ (defun square-uses (node local)
497
+ (let ((count (if (= (square-local node) local) 1 0)) (index 0))
498
+ (while (< index (js-length (get node :children)))
499
+ (setq count (+ count (square-uses (js-nth index (get node :children)) local)))
500
+ (setq index (1+ index)))
501
+ count))
502
+
503
+ (defun pure-conjunction (node)
504
+ (if (= (get node :kind) "and")
505
+ (let ((pure t) (index 0))
506
+ (while (and pure (< index (js-length (get node :children))))
507
+ (setq pure (pure-conjunction (js-nth index (get node :children))))
508
+ (setq index (1+ index)))
509
+ pure)
510
+ (numeric-pure node)))
511
+
512
+ (defun collect-squares (node body caches locals)
513
+ (let ((local (square-local node)) (index 0))
514
+ (when (and (>= local 0) (> (square-uses body local) 0)
515
+ (not (find-local caches (str local))))
516
+ (js-call caches :push (js-object :name (str local) :operand local
517
+ :storage (js-length locals) :ready nil))
518
+ (js-call locals :push (js-object :name "cached-square" :index (js-length locals) :type "f64"
519
+ :pureGuardCache t)))
520
+ (while (< index (js-length (get node :children)))
521
+ (collect-squares (js-nth index (get node :children)) body caches locals)
522
+ (setq index (1+ index)))))
523
+
524
+ (defun clear-square-caches (caches local)
525
+ (let ((index 0))
526
+ (while (< index (js-length caches))
527
+ (when (or (< local 0) (= (get (js-nth index caches) :operand) local))
528
+ (put (js-nth index caches) :ready nil))
529
+ (setq index (1+ index)))))
530
+
531
+ (defun reuse-squares (node caches condition)
532
+ (let* ((kind (get node :kind)) (value (get node :value))
533
+ (local (square-local node))
534
+ (cache (if (>= local 0) (find-local caches (str local)) nil)))
535
+ (if (and cache (get cache :ready))
536
+ (typed "local" "f64" (js-array) (get cache :storage))
537
+ (let ((children (js-array)) (index 0))
538
+ ;; Only a pure conjunction's successful path dominates the body.
539
+ ;; Branches, nested loops and calls conservatively end availability.
540
+ (when (or (= kind "if") (= kind "while") (= kind "remainder-while") (= kind "or"))
541
+ (clear-square-caches caches -1))
542
+ (while (< index (js-length (get node :children)))
543
+ (js-call children :push (reuse-squares (js-nth index (get node :children)) caches condition))
544
+ (setq index (1+ index)))
545
+ (let ((copy (typed kind (get node :type) children value)))
546
+ (when (or (= kind "set") (= kind "initialize")) (clear-square-caches caches value))
547
+ (when (or (= kind "if") (= kind "while") (= kind "remainder-while") (= kind "or") (= kind "call"))
548
+ (clear-square-caches caches -1))
549
+ (if (and cache condition)
550
+ (progn (put cache :ready t)
551
+ (typed "set" "f64" (js-array copy) (get cache :storage)))
552
+ copy))))))
553
+
554
+ (defun optimize-loop-squares (node locals)
555
+ (let ((children (js-array)) (index 0))
556
+ (while (< index (js-length (get node :children)))
557
+ (js-call children :push (optimize-loop-squares (js-nth index (get node :children)) locals))
558
+ (setq index (1+ index)))
559
+ (if (and (or (= (get node :kind) "while") (= (get node :kind) "remainder-while"))
560
+ (pure-conjunction (js-nth 0 children)))
561
+ (let ((caches (js-array)))
562
+ (collect-squares (js-nth 0 children) (js-nth 1 children) caches locals)
563
+ (let ((condition (reuse-squares (js-nth 0 children) caches t)))
564
+ (typed (get node :kind) "i32" (js-array condition
565
+ (reuse-squares (js-nth 1 children) caches nil)) nil)))
566
+ (typed (get node :kind) (get node :type) children (get node :value)))))
567
+
568
+ (defun optimize-numeric-program (program &optional options)
569
+ (unless (= (get program :abi nil) "numeric-f64")
570
+ (backend-fail program "numeric optimization requires validated numeric-f64 input"))
571
+ (let* ((functions (get program :functions))
572
+ (inline-functions (prepare-nested-callees functions options))
573
+ (output (js-array)) (index 0))
574
+ (while (< index (js-length functions))
575
+ (let* ((function (js-nth index functions))
576
+ (locals (js-call (get function :locals) :slice))
577
+ (body (get function :body)))
578
+ (when (get (or options (js-object)) :inlining t)
579
+ (setq body (inline-numeric-node body inline-functions locals)))
580
+ (when (get (or options (js-object)) :loops t)
581
+ (setq body (optimize-loop-squares (optimize-numeric-loops body) locals)))
582
+ (when (get (or options (js-object)) :integers t)
583
+ (setq body (specialize-integer-locals body locals (get function :arity))))
584
+ (when (and (get (or options (js-object)) :inlining t)
585
+ (get (or options (js-object)) :parallel t))
586
+ (setq body (parallelize-call-loop (get function :body) functions locals
587
+ (get function :arity) body (get (or options (js-object)) :parallelSingle nil))))
588
+ (when (get (or options (js-object)) :cyclicMultiple nil)
589
+ (setq body (specialize-cyclic-locals body locals)))
590
+ (when (get (or options (js-object)) :guardedUnroll t)
591
+ (setq body (expand-compound-loops body locals)))
592
+ (when (get (or options (js-object)) :constantFlow nil)
593
+ (let ((reads (js-array)))
594
+ (setq body (fold-constant-flow body (js-array)))
595
+ (collect-flow-reads body reads)
596
+ (setq body (remove-unread-stores body reads))))
597
+ (when (get (or options (js-object)) :unswitch nil)
598
+ (setq body (unswitch-numeric-loops body)))
599
+ (when (get (or options (js-object)) :countedGuards nil)
600
+ (setq body (counted-compound-loops body locals)))
601
+ (js-call output :push (js-object :name (get function :name)
602
+ :index (get function :index) :arity (get function :arity)
603
+ :source (get function :source) :locals locals :body body)))
604
+ (setq index (1+ index)))
605
+ (js-object :abi "numeric-f64" :filename (get program :filename)
606
+ :exports (get program :exports) :functions output)))
607
+
608
+ (defun loop-depth (node depth)
609
+ (let ((best depth) (index 0)
610
+ (next (if (or (= (get node :kind) "while") (= (get node :kind) "remainder-while")
611
+ (= (get node :kind) "guarded-while")) (1+ depth) depth)))
612
+ (while (< index (js-length (get node :children)))
613
+ (let ((candidate (loop-depth (js-nth index (get node :children)) next)))
614
+ (when (> candidate best) (setq best candidate)))
615
+ (setq index (1+ index)))
616
+ best))
617
+
618
+ (defun prepare-nested-callees (functions options)
619
+ (let ((prepared (js-array)) (index 0))
620
+ (while (< index (js-length functions))
621
+ (let* ((function (js-nth index functions)) (body (get function :body)))
622
+ (if (and (get (or options (js-object)) :inlining t)
623
+ (not (contains-kind body "call")) (>= (loop-depth body 0) 2)
624
+ (<= (tree-size body) 128))
625
+ (let ((locals (js-call (get function :locals) :slice)))
626
+ ;; Narrow invariant callee parameters before caller expansion;
627
+ ;; a caller may supply a computed bound that changes between
628
+ ;; calls but is invariant throughout each nested callee loop.
629
+ (when (get (or options (js-object)) :loops t)
630
+ (setq body (optimize-loop-squares (optimize-numeric-loops body) locals)))
631
+ (when (get (or options (js-object)) :integers t)
632
+ (setq body (specialize-integer-locals body locals (get function :arity))))
633
+ (js-call prepared :push (js-object :name (get function :name)
634
+ :arity (get function :arity) :index (get function :index)
635
+ :locals locals :body body :inlineLimit 512)))
636
+ (js-call prepared :push function)))
637
+ (setq index (1+ index)))
638
+ prepared))
639
+
640
+ (defun flatten-statements (node output)
641
+ (if (= (get node :kind) "sequence")
642
+ (let ((index 0))
643
+ (while (< index (js-length (get node :children)))
644
+ (flatten-statements (js-nth index (get node :children)) output)
645
+ (setq index (1+ index))))
646
+ (js-call output :push node))
647
+ output)
648
+
649
+ (defun scalar-pure-value (node)
650
+ (if (or (= (get node :kind) "if") (= (get node :kind) "and")
651
+ (= (get node :kind) "or") (= (get node :kind) "sequence"))
652
+ (let ((pure t) (index 0))
653
+ (while (and pure (< index (js-length (get node :children))))
654
+ (setq pure (scalar-pure-value (js-nth index (get node :children))))
655
+ (setq index (1+ index)))
656
+ pure)
657
+ (numeric-pure node)))
658
+
659
+ (defun reads-local (node local)
660
+ (let ((found (local-node-is node local)) (index 0))
661
+ (while (and (not found) (< index (js-length (get node :children))))
662
+ (setq found (reads-local (js-nth index (get node :children)) local))
663
+ (setq index (1+ index)))
664
+ found))
665
+
666
+ (defun single-loop-shape (body)
667
+ (let ((statements (flatten-statements body (js-array)))
668
+ (position 0) (loop nil) (valid t))
669
+ (while (< position (js-length statements))
670
+ (let ((statement (js-nth position statements)))
671
+ (cond
672
+ ((= (get statement :kind) "while")
673
+ (if loop (setq valid nil) (setq loop (js-object :node statement :position position))))
674
+ ((not loop)
675
+ (unless (and (= (get statement :kind) "initialize")
676
+ (scalar-pure-value (js-nth 0 (get statement :children)))) (setq valid nil)))
677
+ ((not (scalar-pure-value statement)) (setq valid nil))))
678
+ (setq position (1+ position)))
679
+ (when (and valid loop)
680
+ (let* ((condition (js-nth 0 (get (get loop :node) :children)))
681
+ (tail (last-statement (js-nth 1 (get (get loop :node) :children)))))
682
+ (when (and (= (get condition :kind) "compare") (= (get condition :value) "<")
683
+ (= (get (js-nth 0 (get condition :children)) :kind) "local")
684
+ (= (get tail :kind) "set")
685
+ (local-node-is (js-nth 0 (get condition :children)) (get tail :value))
686
+ (increment-is (js-nth 0 (get tail :children)) (get tail :value)))
687
+ (let* ((counter (get tail :value)) (initializer (find-write body counter "initialize")))
688
+ (when (and initializer (= (writes-local body counter) 2)
689
+ (positive-integer-literal (js-nth 0 (get initializer :children)))
690
+ (= (get (js-nth 0 (get initializer :children)) :value) 0))
691
+ (put loop :counter counter)
692
+ (put loop :bound (js-nth 1 (get condition :children)))
693
+ (put loop :statements statements)
694
+ (setq valid t)
695
+ loop)))))))
696
+
697
+ (defun append-numeric-frame (locals callee)
698
+ (let ((offset (js-length locals)) (index 0))
699
+ (while (< index (js-length (get callee :locals)))
700
+ (let ((entry (js-nth index (get callee :locals))))
701
+ (js-call locals :push (js-object :name (get entry :name)
702
+ :index (js-length locals) :type (get entry :type)
703
+ :boundedInteger (get entry :boundedInteger nil))))
704
+ (setq index (1+ index)))
705
+ offset))
706
+
707
+ (defun parallelize-call-loop (original functions locals arity fallback single)
708
+ (let ((outer (single-loop-shape original)))
709
+ (if (not outer) fallback
710
+ (let* ((loop (get outer :node)) (bound (get outer :bound))
711
+ (statements (flatten-statements (js-nth 1 (get loop :children)) (js-array)))
712
+ (first (js-nth 0 statements))
713
+ (addition (if (= (get first :kind) "set") (js-nth 0 (get first :children)) nil))
714
+ (call (if (and addition (= (get addition :kind) "arithmetic")
715
+ (= (get addition :value) "+")) (js-nth 1 (get addition :children)) nil))
716
+ (accumulator (get first :value))
717
+ (valid (and call (= (get call :kind) "call")
718
+ (local-node-is (js-nth 0 (get addition :children)) accumulator)
719
+ (= (get bound :kind) "local") (< (get bound :value) arity)
720
+ (= (writes-local original (get bound :value)) 0))))
721
+ (if (not valid) fallback
722
+ (let* ((callee (js-nth (get call :value) functions))
723
+ (inner (single-loop-shape (get callee :body)))
724
+ (index 1))
725
+ (while (< index (js-length statements))
726
+ (let ((statement (js-nth index statements)))
727
+ (unless (and (= (get statement :kind) "set")
728
+ (/= (get statement :value) accumulator)
729
+ (scalar-pure-value (js-nth 0 (get statement :children)))
730
+ (not (reads-local (js-nth 0 (get statement :children)) accumulator)))
731
+ (setq valid nil)))
732
+ (setq index (1+ index)))
733
+ (setq index 0)
734
+ (while (< index (js-length (get call :children)))
735
+ (let ((argument (js-nth index (get call :children))))
736
+ (unless (and (scalar-pure-value argument) (not (reads-local argument accumulator)))
737
+ (setq valid nil)))
738
+ (setq index (1+ index)))
739
+ (when inner
740
+ (let* ((limit (get inner :bound))
741
+ (kernel-body (flatten-statements (js-nth 1 (get (get inner :node) :children)) (js-array)))
742
+ (index 0))
743
+ (unless (and (= (get limit :kind) "local") (< (get limit :value) (get callee :arity))
744
+ (= (writes-local (get callee :body) (get limit :value)) 0))
745
+ (setq valid nil))
746
+ (while (< index (js-length kernel-body))
747
+ (let ((statement (js-nth index kernel-body)))
748
+ (unless (and (or (= (get statement :kind) "set") (= (get statement :kind) "initialize"))
749
+ (scalar-pure-value (js-nth 0 (get statement :children))))
750
+ (setq valid nil)))
751
+ (setq index (1+ index)))))
752
+ (if (or (not valid) (not inner)) fallback
753
+ (let ((input (js-nth (get (get inner :bound) :value) (get call :children))))
754
+ (if (not (or (= (get input :kind) "literal")
755
+ (and (= (get input :kind) "local") (< (get input :value) arity)
756
+ (= (writes-local original (get input :value)) 0)))) fallback
757
+ (emit-parallel-call-loop outer inner callee call statements input bound
758
+ accumulator locals fallback single))))))))))
759
+
760
+ (defun emit-parallel-call-loop (outer inner callee call statements input bound accumulator locals fallback single)
761
+ (let* ((offset0 (append-numeric-frame locals callee))
762
+ (offset1 (append-numeric-frame locals callee))
763
+ (aliases0 (js-array)) (aliases1 (js-array))
764
+ (pair (js-array)) (work (js-array)) (index 0) (lane-loop nil)
765
+ (kernel-statements (get inner :statements))
766
+ (kernel-body (flatten-statements (js-nth 1 (get (get inner :node) :children)) (js-array)))
767
+ (counter (get inner :counter)) (limit-parameter (get (get inner :bound) :value)))
768
+ (while (< index (js-length (get callee :locals)))
769
+ (js-call aliases0 :push (if (= index limit-parameter) input nil))
770
+ (js-call aliases1 :push (cond ((= index limit-parameter) input)
771
+ ((= index counter) (typed "local" "f64" (js-array) (+ offset0 counter))) (t nil)))
772
+ (setq index (1+ index)))
773
+ (let ((lane 0))
774
+ (while (< lane 2)
775
+ (let ((offset (if (= lane 0) offset0 offset1))
776
+ (aliases (if (= lane 0) aliases0 aliases1)) (index 0))
777
+ (while (< index (get callee :arity))
778
+ (js-call pair :push (typed "initialize" "f64"
779
+ (js-array (js-nth index (get call :children))) (+ offset index)))
780
+ (setq index (1+ index)))
781
+ (setq index 0)
782
+ (while (< index (get inner :position))
783
+ (js-call pair :push (copy-offset (js-nth index kernel-statements) offset aliases))
784
+ (setq index (1+ index)))
785
+ (setq index 1)
786
+ (while (< index (js-length statements))
787
+ (js-call pair :push (js-nth index statements))
788
+ (setq index (1+ index)))
789
+ (setq index 0)
790
+ (while (< index (1- (js-length kernel-body)))
791
+ (js-call work :push (copy-offset (js-nth index kernel-body) offset aliases))
792
+ (setq index (1+ index))))
793
+ (setq lane (1+ lane))))
794
+ (js-call work :push (copy-offset (js-nth (1- (js-length kernel-body)) kernel-body) offset0 aliases0))
795
+ (setq lane-loop (typed "while" "i32" (js-array
796
+ (copy-offset (js-nth 0 (get (get inner :node) :children)) offset0 aliases0)
797
+ (typed "sequence" "f64" work nil)) nil))
798
+ (js-call pair :push lane-loop)
799
+ (let ((lane 0))
800
+ (while (< lane 2)
801
+ (let* ((offset (if (= lane 0) offset0 offset1))
802
+ (aliases (if (= lane 0) aliases0 aliases1))
803
+ (suffix (js-array)) (index (1+ (get inner :position))))
804
+ (while (< index (js-length kernel-statements))
805
+ (js-call suffix :push (copy-offset (js-nth index kernel-statements) offset aliases))
806
+ (setq index (1+ index)))
807
+ (js-call pair :push (typed "set" "f64" (js-array
808
+ (typed "arithmetic" "f64" (js-array
809
+ (typed "local" "f64" (js-array) accumulator)
810
+ (typed "sequence" "f64" suffix nil)) "+")) accumulator)))
811
+ (setq lane (1+ lane))))
812
+ (let ((body (js-array)) (index 0) (outer-statements (get outer :statements)))
813
+ (while (< index (get outer :position))
814
+ (js-call body :push (js-nth index outer-statements))
815
+ (setq index (1+ index)))
816
+ (js-call body :push (typed "while" "i32" (js-array
817
+ (typed "compare" "i32" (js-array
818
+ (typed "arithmetic" "f64" (js-array (typed "local" "f64" (js-array) (get outer :counter))) "1+")
819
+ bound) "<") (typed "sequence" "f64" pair nil)) nil))
820
+ (js-call body :push (get outer :node))
821
+ (setq index (1+ (get outer :position)))
822
+ (while (< index (js-length outer-statements))
823
+ (js-call body :push (js-nth index outer-statements))
824
+ (setq index (1+ index)))
825
+ ;; Lane state is private and arithmetic order is unchanged per call.
826
+ ;; The final reductions execute in original call order. Bounds exclude
827
+ ;; nontermination and counter rounding; all other inputs use fallback.
828
+ (typed "if" "f64" (js-array (typed "and" "i32" (js-array
829
+ (typed "compare" "i32" (js-array input (typed "literal" "f64" (js-array) (if single 1 2))) ">=")
830
+ (typed "compare" "i32" (js-array input (typed "literal" "f64" (js-array) 2147483647)) "<=")
831
+ (typed "compare" "i32" (js-array bound (typed "literal" "f64" (js-array) 2)) ">=")
832
+ (typed "compare" "i32" (js-array bound (typed "literal" "f64" (js-array) 2147483647)) "<=")) nil)
833
+ (if single
834
+ ;; The enclosing finite input >= 1 guard and this <= 1 test
835
+ ;; prove exactly one iteration. Dispatch once before the batch;
836
+ ;; preserve both lane bodies and their ordered reductions.
837
+ (typed "if" "f64" (js-array
838
+ (typed "compare" "i32" (js-array input (typed "literal" "f64" (js-array) 1)) "<=")
839
+ (replace-numeric-node (typed "sequence" "f64" body nil) lane-loop
840
+ (typed "sequence" "i32" (js-array (typed "sequence" "f64" work nil)
841
+ (typed "literal" "i32" (js-array) 0)) nil))
842
+ (typed "sequence" "f64" body nil)) nil)
843
+ (typed "sequence" "f64" body nil)) fallback) nil))))
844
+
845
+ (defun replace-numeric-node (node selected replacement)
846
+ (if (= node selected) replacement
847
+ (let ((children (js-array)) (i 0))
848
+ (while (< i (js-length (get node :children)))
849
+ (js-call children :push (replace-numeric-node (js-nth i (get node :children)) selected replacement))
850
+ (setq i (1+ i)))
851
+ (typed (get node :kind) (get node :type) children (get node :value)))))
852
+
853
+ (defun positive-integer-literal (node)
854
+ (and (= (get node :kind) "literal") (= (get node :type) "f64")
855
+ (>= (get node :value) 0) (<= (get node :value) 2147483647)
856
+ (= (get node :value) (uint32 (get node :value)))
857
+ (not (and (= (get node :value) 0) (< (/ 1 (get node :value)) 0)))))
858
+
859
+ (defun local-node-is (node index)
860
+ (and (= (get node :kind) "local") (= (get node :value) index)))
861
+
862
+ (defun increment-is (node index)
863
+ (and (= (get node :kind) "arithmetic") (= (get node :value) "1+")
864
+ (local-node-is (js-nth 0 (get node :children)) index)))
865
+
866
+ (defun find-write (node index kind)
867
+ (let ((found nil) (position 0))
868
+ (if (and (= (get node :kind) kind) (= (get node :value) index))
869
+ node
870
+ (progn
871
+ (while (and (not found) (< position (js-length (get node :children))))
872
+ (setq found (find-write (js-nth position (get node :children)) index kind))
873
+ (setq position (1+ position)))
874
+ found))))
875
+
876
+ (defun last-statement (node)
877
+ (if (and (= (get node :kind) "sequence") (> (js-length (get node :children)) 0))
878
+ (last-statement (js-nth (1- (js-length (get node :children))) (get node :children)))
879
+ node))
880
+
881
+ (defun counter-bound (node index root arity)
882
+ (let ((found nil) (position 0))
883
+ (when (= (get node :kind) "while")
884
+ (let* ((condition (js-nth 0 (get node :children)))
885
+ (first (if (= (get condition :kind) "and")
886
+ (js-nth 0 (get condition :children)) condition))
887
+ (tail (last-statement (js-nth 1 (get node :children)))))
888
+ (when (and (= (get first :kind) "compare") (= (get first :value) "<")
889
+ (local-node-is (js-nth 0 (get first :children)) index)
890
+ (= (get tail :kind) "set") (= (get tail :value) index)
891
+ (increment-is (js-nth 0 (get tail :children)) index))
892
+ (let ((bound (js-nth 1 (get first :children))))
893
+ (when (or (positive-integer-literal bound)
894
+ (and (= (get bound :kind) "local") (< (get bound :value) arity)
895
+ (= (writes-local root (get bound :value)) 0)))
896
+ (setq found bound))))))
897
+ (while (and (not found) (< position (js-length (get node :children))))
898
+ (setq found (counter-bound (js-nth position (get node :children)) index root arity))
899
+ (setq position (1+ position)))
900
+ found))
901
+
902
+ (defun cyclic-integer (node index)
903
+ (when (= (get node :kind) "if")
904
+ (let* ((children (get node :children)) (condition (js-nth 0 children))
905
+ (yes (js-nth 1 children)) (no (js-nth 2 children)))
906
+ (and (= (get condition :kind) "compare") (= (get condition :value) "<")
907
+ (local-node-is (js-nth 0 (get condition :children)) index)
908
+ (positive-integer-literal (js-nth 1 (get condition :children)))
909
+ (<= (get (js-nth 1 (get condition :children)) :value) 2147483646)
910
+ (increment-is yes index) (positive-integer-literal no) (= (get no :value) 0)))))
911
+
912
+ (defun cycle-writes-proven (node index)
913
+ (let ((kind (get node :kind)) (valid t) (i 0))
914
+ (when (= (get node :value) index)
915
+ (cond
916
+ ((= kind "initialize")
917
+ (setq valid (and (positive-integer-literal (js-nth 0 (get node :children)))
918
+ (= (get (js-nth 0 (get node :children)) :value) 0))))
919
+ ((= kind "set") (setq valid (cyclic-integer (js-nth 0 (get node :children)) index)))))
920
+ (while (and valid (< i (js-length (get node :children))))
921
+ (setq valid (cycle-writes-proven (js-nth i (get node :children)) index)) (setq i (1+ i)))
922
+ valid))
923
+
924
+ (defun specialize-cyclic-locals (body locals)
925
+ ;; Every initialization and update must satisfy the range proof. Lane
926
+ ;; expansion may introduce several updates, resets and branch copies;
927
+ ;; accepting only the first update would freeze an unproven later write.
928
+ (let ((descriptors (js-array)) (count (js-length locals)) (i 0) (selected nil))
929
+ (while (< i count)
930
+ (let ((entry nil))
931
+ (when (and (= (get (js-nth i locals) :type) "f64")
932
+ (find-write body i "initialize") (find-write body i "set")
933
+ (cycle-writes-proven body i))
934
+ (setq entry (js-object :storage (js-length locals)))
935
+ (js-call locals :push (js-object :name "integer-cycle" :index (js-length locals) :type "i32"))
936
+ (setq selected t))
937
+ (js-call descriptors :push entry))
938
+ (setq i (1+ i)))
939
+ (if selected (rewrite-integers body descriptors) body)))
940
+
941
+ (defun counter-depth (node index depth)
942
+ (let* ((kind (get node :kind)) (children (get node :children))
943
+ (best -1) (position 0))
944
+ (when (= kind "while")
945
+ (let* ((condition (js-nth 0 children))
946
+ (first (if (= (get condition :kind) "and") (js-nth 0 (get condition :children)) condition)))
947
+ (when (and (= (get first :kind) "compare")
948
+ (local-node-is (js-nth 0 (get first :children)) index))
949
+ (setq best depth))))
950
+ (while (< position (js-length children))
951
+ (let ((candidate (counter-depth (js-nth position children) index
952
+ (if (= kind "while") (1+ depth) depth))))
953
+ (when (> candidate best) (setq best candidate)))
954
+ (setq position (1+ position)))
955
+ best))
956
+
957
+ (defun integer-value (node)
958
+ (cond
959
+ ((= (get node :kind) "int-number") (js-nth 0 (get node :children)))
960
+ ((positive-integer-literal node) (typed "int-literal" "i32" (js-array) (get node :value)))
961
+ ((and (= (get node :kind) "arithmetic") (= (get node :value) "1+"))
962
+ (typed "int-add" "i32" (js-array (integer-value (js-nth 0 (get node :children)))
963
+ (typed "int-literal" "i32" (js-array) 1)) nil))
964
+ ((= (get node :kind) "if")
965
+ (let* ((condition (js-nth 0 (get node :children)))
966
+ (yes (integer-value (js-nth 1 (get node :children))))
967
+ (no (integer-value (js-nth 2 (get node :children)))))
968
+ (if (and (= (get condition :kind) "int-compare")
969
+ (= (get condition :value) "<") (= (get yes :kind) "int-add")
970
+ (= (get no :kind) "int-literal") (= (get no :value) 0)
971
+ (= (get (js-nth 1 (get condition :children)) :kind) "int-literal")
972
+ (= (get (js-nth 0 (get condition :children)) :kind) "local")
973
+ (local-node-is (js-nth 0 (get yes :children))
974
+ (get (js-nth 0 (get condition :children)) :value))
975
+ (= (bit-and (1+ (get (js-nth 1 (get condition :children)) :value))
976
+ (get (js-nth 1 (get condition :children)) :value)) 0))
977
+ ;; The cyclic-local proof establishes 0 <= value <= mask.
978
+ ;; Addition cannot overflow, and & is exact modulo mask + 1.
979
+ (typed "int-and" "i32" (js-array yes (js-nth 1 (get condition :children))) nil)
980
+ (typed "if" "i32" (js-array condition yes no) nil))))
981
+ (t (backend-fail node "integer specialization lacks a range proof"))))
982
+
983
+ (defun integer-operand (node)
984
+ (or (= (get node :kind) "int-number") (positive-integer-literal node)))
985
+
986
+ (defun rewrite-integers (node descriptors)
987
+ (let* ((kind (get node :kind)) (value (get node :value))
988
+ (descriptor (if (or (= kind "local") (= kind "set") (= kind "initialize"))
989
+ (js-nth value descriptors) nil))
990
+ (children (js-array)) (position 0))
991
+ (while (< position (js-length (get node :children)))
992
+ (js-call children :push (rewrite-integers (js-nth position (get node :children)) descriptors))
993
+ (setq position (1+ position)))
994
+ (cond
995
+ ((and descriptor (= kind "local"))
996
+ (typed "int-number" "f64" (js-array
997
+ (typed "local" "i32" (js-array) (get descriptor :storage))) nil))
998
+ ((and descriptor (or (= kind "set") (= kind "initialize")))
999
+ (typed "int-number" "f64" (js-array
1000
+ (typed kind "i32" (js-array (integer-value (js-nth 0 children)))
1001
+ (get descriptor :storage))) nil))
1002
+ ((and (= kind "compare") (= value "<")
1003
+ (= (get (js-nth 0 (get node :children)) :kind) "local"))
1004
+ (let* ((entry (js-nth (get (js-nth 0 (get node :children)) :value) descriptors))
1005
+ (right (js-nth 1 (get node :children)))
1006
+ (bound (if entry (get entry :bound nil) nil)))
1007
+ (cond
1008
+ ((and bound (= (get right :kind) (get bound :kind))
1009
+ (= (get right :value) (get bound :value)))
1010
+ (typed "int-compare" "i32" (js-array (integer-value (js-nth 0 children))
1011
+ (typed "local" "i32" (js-array) (get entry :limit))) value))
1012
+ ((and (integer-operand (js-nth 0 children)) (integer-operand (js-nth 1 children)))
1013
+ (typed "int-compare" "i32" (js-array (integer-value (js-nth 0 children))
1014
+ (integer-value (js-nth 1 children))) value))
1015
+ (t (typed kind (get node :type) children value)))))
1016
+ (t (typed kind (get node :type) children value)))))
1017
+
1018
+ (defun specialize-integer-locals (body locals arity)
1019
+ (let ((descriptors (js-array)) (guards (js-array)) (prelude (js-array))
1020
+ (count (js-length locals)) (index 0) (selected 0) (small nil))
1021
+ (while (< index count)
1022
+ (let ((initializer (find-write body index "initialize"))
1023
+ (update (find-write body index "set")) (entry nil))
1024
+ (when (and initializer update (= (writes-local body index) 2)
1025
+ (positive-integer-literal (js-nth 0 (get initializer :children)))
1026
+ (= (get (js-nth 0 (get initializer :children)) :value) 0))
1027
+ (let* ((rhs (js-nth 0 (get update :children)))
1028
+ (bound (if (increment-is rhs index) (counter-bound body index body arity) nil)))
1029
+ (when (or bound (cyclic-integer rhs index))
1030
+ (setq entry (js-object :storage (js-length locals) :bound bound))
1031
+ (js-call locals :push (js-object :name "integer-counter" :index (js-length locals) :type "i32" :boundedInteger t))
1032
+ (when bound
1033
+ (put entry :limit (js-length locals))
1034
+ (js-call locals :push (js-object :name "integer-limit" :index (js-length locals) :type "i32" :boundedInteger t))
1035
+ (unless (positive-integer-literal bound)
1036
+ (js-call guards :push (typed "compare" "i32" (js-array bound
1037
+ (typed "literal" "f64" (js-array) 0)) ">="))
1038
+ (js-call guards :push (typed "compare" "i32" (js-array bound
1039
+ (typed "literal" "f64" (js-array) 2147483647)) "<=")))
1040
+ (js-call prelude :push (typed "initialize" "i32"
1041
+ (js-array (typed "ceil-int" "i32" (js-array bound) nil)) (get entry :limit)))
1042
+ (put entry :depth (counter-depth body index 0))
1043
+ (when (and (= (get bound :kind) "local")
1044
+ (or (not small) (> (get entry :depth) (get small :depth))))
1045
+ (setq small entry)))
1046
+ (setq selected (1+ selected)))))
1047
+ (js-call descriptors :push entry))
1048
+ (setq index (1+ index)))
1049
+ (if (= selected 0) body
1050
+ (progn
1051
+ (let ((rewritten (rewrite-integers body descriptors)))
1052
+ (js-call prelude :push
1053
+ (if small
1054
+ ;; Version a proven counter for zero/one iterations. The
1055
+ ;; bound test stays outside surrounding batch loops.
1056
+ (typed "if" "f64" (js-array
1057
+ (typed "int-compare" "i32" (js-array
1058
+ (typed "local" "i32" (js-array) (get small :limit))
1059
+ (typed "int-literal" "i32" (js-array) 2)) "<")
1060
+ (unroll-integer-loops (single-iteration-loops rewritten small))
1061
+ (unroll-integer-loops rewritten)) nil)
1062
+ (unroll-integer-loops rewritten))))
1063
+ (let ((fast (typed "sequence" "f64" prelude nil)))
1064
+ (if (= (js-length guards) 0) fast
1065
+ ;; Conversion executes only behind finite nonnegative bounds.
1066
+ ;; Fractional bounds use ceil, preserving the original i < n.
1067
+ (typed "if" "f64" (js-array (typed "and" "i32" guards nil) fast body) nil)))))))
1068
+
1069
+ (defun unroll-integer-loops (node)
1070
+ (let ((children (js-array)) (index 0))
1071
+ (while (< index (js-length (get node :children)))
1072
+ (js-call children :push (unroll-integer-loops (js-nth index (get node :children))))
1073
+ (setq index (1+ index)))
1074
+ (let ((copy (typed (get node :kind) (get node :type) children (get node :value))))
1075
+ (if (= (get copy :kind) "while")
1076
+ (let* ((condition (js-nth 0 children)) (body (js-nth 1 children))
1077
+ (operands (get condition :children)) (tail (last-statement body)))
1078
+ (when (= (get tail :kind) "int-number") (setq tail (js-nth 0 (get tail :children))))
1079
+ (if (and (= (get condition :kind) "int-compare")
1080
+ (local-node-is (js-nth 0 operands) (get tail :value))
1081
+ (= (get (js-nth 1 operands) :kind) "local")
1082
+ (= (get tail :kind) "set")
1083
+ (= (get (js-nth 0 (get tail :children)) :kind) "int-add")
1084
+ (= (writes-local body (get (js-nth 0 operands) :value)) 1)
1085
+ (= (writes-local body (get (js-nth 1 operands) :value)) 0)
1086
+ (not (contains-kind body "while")) (<= (tree-size body) 128))
1087
+ (typed "sequence" "i32" (js-array
1088
+ (typed "while" "i32" (js-array
1089
+ (typed "int-compare" "i32" (js-array
1090
+ (typed "int-literal" "i32" (js-array) 3)
1091
+ (typed "int-sub" "i32" (js-array (js-nth 1 operands) (js-nth 0 operands)) nil)) "<")
1092
+ ;; Four complete iterations, with every f64 operation
1093
+ ;; and effect in its original order; no reassociation.
1094
+ (typed "sequence" (get body :type) (js-array body body body body) nil)) nil)
1095
+ (typed "remainder-while" "i32" children nil)) nil)
1096
+ copy))
1097
+ copy))))
1098
+
1099
+ (defun single-iteration-loops (node entry)
1100
+ (let ((children (js-array)) (index 0))
1101
+ (while (< index (js-length (get node :children)))
1102
+ (js-call children :push (single-iteration-loops (js-nth index (get node :children)) entry))
1103
+ (setq index (1+ index)))
1104
+ (if (= (get node :kind) "while")
1105
+ (let* ((condition (js-nth 0 children))
1106
+ (first (if (= (get condition :kind) "and") (js-nth 0 (get condition :children)) condition)))
1107
+ (if (and (= (get first :kind) "int-compare")
1108
+ (local-node-is (js-nth 0 (get first :children)) (get entry :storage))
1109
+ (local-node-is (js-nth 1 (get first :children)) (get entry :limit)))
1110
+ (typed "if" "i32" (js-array condition
1111
+ (typed "sequence" "i32" (js-array (js-nth 1 children)
1112
+ (typed "literal" "i32" (js-array) 0)) nil)
1113
+ (typed "literal" "i32" (js-array) 0)) nil)
1114
+ (typed "while" "i32" children nil)))
1115
+ (typed (get node :kind) (get node :type) children (get node :value)))))
1116
+
1117
+ (defun expandable-guard (node locals)
1118
+ (let ((kind (get node :kind)))
1119
+ (cond
1120
+ ((= kind "and")
1121
+ (let ((index 0) (pure t))
1122
+ (while (and pure (< index (js-length (get node :children))))
1123
+ (setq pure (expandable-guard (js-nth index (get node :children)) locals))
1124
+ (setq index (1+ index)))
1125
+ pure))
1126
+ ((= kind "set")
1127
+ (and (get (js-nth (get node :value) locals) :pureGuardCache nil)
1128
+ (numeric-pure (js-nth 0 (get node :children)))))
1129
+ ((or (= kind "arithmetic") (= kind "compare") (= kind "not"))
1130
+ (let ((index 0) (pure t))
1131
+ (while (and pure (< index (js-length (get node :children))))
1132
+ (setq pure (expandable-guard (js-nth index (get node :children)) locals))
1133
+ (setq index (1+ index)))
1134
+ pure))
1135
+ (t (numeric-pure node)))))
1136
+
1137
+ (defun expand-compound-loops (node locals)
1138
+ (let ((children (js-array)) (index 0))
1139
+ (while (< index (js-length (get node :children)))
1140
+ (js-call children :push (expand-compound-loops (js-nth index (get node :children)) locals))
1141
+ (setq index (1+ index)))
1142
+ ;; The emitter repeats a pure compound guard after every complete body.
1143
+ ;; It never moves floating operations or executes a failing iteration.
1144
+ ;; Keep effectful guards and nested/large bodies on their normal path.
1145
+ (typed
1146
+ (if (and (= (get node :kind) "while")
1147
+ (= (get (js-nth 0 children) :kind) "and")
1148
+ (expandable-guard (js-nth 0 children) locals)
1149
+ (not (contains-kind (js-nth 1 children) "while"))
1150
+ (<= (tree-size (js-nth 1 children)) 128))
1151
+ "guarded-while" (get node :kind))
1152
+ (get node :type) children (get node :value))))
1153
+
1154
+ (defun counted-compound-loops (node locals)
1155
+ (let ((children (js-array)) (index 0))
1156
+ (while (< index (js-length (get node :children)))
1157
+ (js-call children :push (counted-compound-loops (js-nth index (get node :children)) locals))
1158
+ (setq index (1+ index)))
1159
+ (let ((copy (typed (get node :kind) (get node :type) children (get node :value))))
1160
+ (if (= (get node :kind) "guarded-while")
1161
+ (let* ((condition (js-nth 0 children)) (body (js-nth 1 children))
1162
+ (guards (get condition :children)) (counter (js-nth 0 guards))
1163
+ (a (js-nth 0 (get counter :children))) (b (js-nth 1 (get counter :children)))
1164
+ (tail (last-statement body)))
1165
+ (when (= (get tail :kind) "int-number") (setq tail (js-nth 0 (get tail :children))))
1166
+ (if (and (= (get counter :kind) "int-compare") (= (get counter :value) "<")
1167
+ (= (get a :kind) "local") (= (get b :kind) "local")
1168
+ (get (js-nth (get a :value) locals) :boundedInteger nil)
1169
+ (get (js-nth (get b :value) locals) :boundedInteger nil)
1170
+ (= (get tail :kind) "set") (= (get tail :value) (get a :value))
1171
+ (= (writes-local body (get a :value)) 1) (= (writes-local body (get b :value)) 0)
1172
+ (= (get (js-nth 0 (get tail :children)) :kind) "int-add")
1173
+ (local-node-is (js-nth 0 (get (js-nth 0 (get tail :children)) :children)) (get a :value))
1174
+ (= (get (js-nth 1 (get (js-nth 0 (get tail :children)) :children)) :kind) "int-literal")
1175
+ (= (get (js-nth 1 (get (js-nth 0 (get tail :children)) :children)) :value) 1))
1176
+ ;; Both operands were range-proven in [0, 2^31-1]. Their
1177
+ ;; subtraction cannot overflow. Four remaining steps allow us
1178
+ ;; to omit only the redundant counter tests, never IEEE guards.
1179
+ (typed "guarded-counter-while" (get node :type)
1180
+ (js-array condition body
1181
+ (typed "int-compare" "i32" (js-array (typed "int-literal" "i32" (js-array) 3)
1182
+ (typed "int-sub" "i32" (js-array b a) nil)) "<")
1183
+ (if (= (js-length guards) 2) (js-nth 1 guards)
1184
+ (typed "and" "i32" (js-call guards :slice 1) nil))) nil)
1185
+ copy)) copy))))
1186
+
1187
+ (defun flow-literal (node)
1188
+ (or (= (get node :kind) "literal") (= (get node :kind) "int-literal")))
1189
+
1190
+ (defun invalidate-flow-writes (node known)
1191
+ (when (and (or (= (get node :kind) "set") (= (get node :kind) "initialize"))
1192
+ (< (get node :value) (js-length known)))
1193
+ (js-call known :splice (get node :value) 1 nil))
1194
+ (let ((i 0)) (while (< i (js-length (get node :children)))
1195
+ (invalidate-flow-writes (js-nth i (get node :children)) known) (setq i (1+ i)))))
1196
+
1197
+ (defun same-flow-literal (left right)
1198
+ ;; Join constants by bits: +0 and -0 are distinct IEEE values.
1199
+ (and left right (= (get left :type) (get right :type))
1200
+ (or (/= (get left :value) 0) (= (/ 1 (get left :value)) (/ 1 (get right :value))))
1201
+ (let ((a (number-float64-words (get left :value)))
1202
+ (b (number-float64-words (get right :value))))
1203
+ (and (= (js-nth 0 a) (js-nth 0 b)) (= (js-nth 1 a) (js-nth 1 b))))))
1204
+
1205
+ (defun fold-literal-operation (node)
1206
+ (let* ((kind (get node :kind)) (children (get node :children))
1207
+ (op (get node :value)) (i 0) (all t))
1208
+ (while (< i (js-length children))
1209
+ (unless (flow-literal (js-nth i children)) (setq all nil)) (setq i (1+ i)))
1210
+ (if (not all) node
1211
+ (let ((a (if (> (js-length children) 0) (get (js-nth 0 children) :value) 0))
1212
+ (b (if (> (js-length children) 1) (get (js-nth 1 children) :value) 1))
1213
+ (value nil) (valid t))
1214
+ (cond
1215
+ ((= kind "arithmetic")
1216
+ (setq value (cond
1217
+ ((= (js-length children) 0) (if (= op "*") 1 0))
1218
+ ((= op "1+") (+ a 1)) ((= op "1-") (- a 1))
1219
+ ((= (js-length children) 1)
1220
+ (cond ((= op "-") (- a)) ((= op "/") (/ 1 a)) (t a)))
1221
+ ((= op "+") (+ a b)) ((= op "-") (- a b))
1222
+ ((= op "*") (* a b)) (t (/ a b)))))
1223
+ ((or (= kind "compare") (= kind "int-compare"))
1224
+ (setq value (if (cond ((= op "=") (= a b)) ((= op "<") (< a b))
1225
+ ((= op "<=") (<= a b)) ((= op ">") (> a b)) (t (>= a b))) 1 0)))
1226
+ ((= kind "int-number") (setq value a))
1227
+ ((= kind "int-add") (setq value (bit-or (+ a b) 0)))
1228
+ ((= kind "int-sub") (setq value (bit-or (- a b) 0)))
1229
+ ((= kind "int-and") (setq value (bit-and a b)))
1230
+ ((= kind "not") (setq value (if (and (= (get (js-nth 0 children) :type) "i32") (= a 0)) 1 0)))
1231
+ (t (setq valid nil)))
1232
+ (if valid (typed "literal" (get node :type) (js-array) value) node)))))
1233
+
1234
+ (defun fold-constant-flow (node known)
1235
+ (let ((kind (get node :kind)) (value (get node :value)) (children (get node :children)))
1236
+ (cond
1237
+ ((= kind "local") (or (js-nth value known) node))
1238
+ ((= kind "if")
1239
+ (let ((guard (fold-constant-flow (js-nth 0 children) known)))
1240
+ (if (flow-literal guard)
1241
+ (fold-constant-flow (js-nth (if (or (= (get guard :type) "f64")
1242
+ (/= (get guard :value) 0)) 1 2) children) known)
1243
+ (let* ((yes-known (js-call known :slice)) (no-known (js-call known :slice))
1244
+ (yes (fold-constant-flow (js-nth 1 children) yes-known))
1245
+ (no (fold-constant-flow (js-nth 2 children) no-known)) (i 0))
1246
+ (while (< i (js-length known))
1247
+ (js-call known :splice i 1
1248
+ (if (same-flow-literal (js-nth i yes-known) (js-nth i no-known))
1249
+ (js-nth i yes-known) nil))
1250
+ (setq i (1+ i)))
1251
+ (typed kind (get node :type) (js-array guard yes no) value)))))
1252
+ ((or (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
1253
+ ;; Invalidate before folding the first guard, including guard writes.
1254
+ ;; Initial values cannot stand in for values of later iterations.
1255
+ (invalidate-flow-writes node known)
1256
+ (let* ((inner (js-call known :slice))
1257
+ (guard (fold-constant-flow (js-nth 0 children) inner))
1258
+ (body (fold-constant-flow (js-nth 1 children) inner)))
1259
+ (typed kind (get node :type) (js-array guard body) value)))
1260
+ ((or (= kind "and") (= kind "or"))
1261
+ (invalidate-flow-writes node known)
1262
+ (let ((output (js-array)) (i 0))
1263
+ (while (< i (js-length children))
1264
+ (js-call output :push (fold-constant-flow (js-nth i children) (js-call known :slice)))
1265
+ (setq i (1+ i)))
1266
+ (typed kind (get node :type) output value)))
1267
+ (t
1268
+ (let ((output (js-array)) (i 0))
1269
+ (while (< i (js-length children))
1270
+ (js-call output :push (fold-constant-flow (js-nth i children) known)) (setq i (1+ i)))
1271
+ (let ((copy (fold-literal-operation (typed kind (get node :type) output value))))
1272
+ (when (or (= kind "set") (= kind "initialize"))
1273
+ (while (<= (js-length known) value) (js-call known :push nil))
1274
+ (js-call known :splice value 1 (if (flow-literal (js-nth 0 output)) (js-nth 0 output) nil)))
1275
+ copy))))))
1276
+
1277
+ (defun collect-flow-reads (node reads)
1278
+ (when (= (get node :kind) "local") (js-call reads :push (get node :value)))
1279
+ (let ((i 0)) (while (< i (js-length (get node :children)))
1280
+ (collect-flow-reads (js-nth i (get node :children)) reads) (setq i (1+ i)))))
1281
+
1282
+ (defun remove-unread-stores (node reads)
1283
+ (let ((children (js-array)) (i 0) (kind (get node :kind)))
1284
+ (while (< i (js-length (get node :children)))
1285
+ (js-call children :push (remove-unread-stores (js-nth i (get node :children)) reads))
1286
+ (setq i (1+ i)))
1287
+ ;; Keep the RHS as an expression: its effects and assignment result
1288
+ ;; remain observable even when the storage itself is never read.
1289
+ (if (and (or (= kind "set") (= kind "initialize"))
1290
+ (not (js-call reads :includes (get node :value))))
1291
+ (js-nth 0 children)
1292
+ (typed kind (get node :type) children (get node :value)))))
1293
+
1294
+ (defun invariant-guard (node body)
1295
+ (let ((kind (get node :kind)) (children (get node :children)) (i 0) (valid t))
1296
+ (cond
1297
+ ((= kind "local") (setq valid (= (writes-local body (get node :value)) 0)))
1298
+ ((or (= kind "literal") (= kind "int-literal")))
1299
+ ((or (= kind "arithmetic") (= kind "compare") (= kind "and") (= kind "or") (= kind "not")
1300
+ (= kind "int-add") (= kind "int-sub") (= kind "int-and") (= kind "int-compare")
1301
+ (= kind "int-number") (= kind "ceil-int")))
1302
+ (t (setq valid nil)))
1303
+ (while (and valid (< i (js-length children)))
1304
+ (setq valid (invariant-guard (js-nth i children) body)) (setq i (1+ i)))
1305
+ valid))
1306
+
1307
+ (defun invariant-if-path (node body path)
1308
+ (let ((kind (get node :kind)) (children (get node :children)) (i 0) (found nil))
1309
+ (if (and (= kind "if") (invariant-guard (js-nth 0 children) body))
1310
+ (js-object :path path :condition (js-nth 0 children))
1311
+ (progn
1312
+ (unless (or (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
1313
+ (while (and (not found) (< i (js-length children)))
1314
+ (let ((next (js-call path :slice)))
1315
+ (js-call next :push i) (setq found (invariant-if-path (js-nth i children) body next)))
1316
+ (setq i (1+ i))))
1317
+ found))))
1318
+
1319
+ (defun same-numeric-tree (left right)
1320
+ (let ((a (get left :children)) (b (get right :children)) (i 0)
1321
+ (same (and (= (get left :kind) (get right :kind)) (= (get left :type) (get right :type))
1322
+ (= (js-length (get left :children)) (js-length (get right :children))))))
1323
+ (when same
1324
+ (setq same (if (flow-literal left) (same-flow-literal left right)
1325
+ (= (get left :value) (get right :value)))))
1326
+ (while (and same (< i (js-length a)))
1327
+ (setq same (same-numeric-tree (js-nth i a) (js-nth i b))) (setq i (1+ i)))
1328
+ same))
1329
+
1330
+ (defun select-invariant-copies (node guard arm)
1331
+ (if (and (= (get node :kind) "if") (same-numeric-tree (js-nth 0 (get node :children)) guard))
1332
+ (select-invariant-copies (js-nth arm (get node :children)) guard arm)
1333
+ (let ((children (js-array)) (i 0))
1334
+ (while (< i (js-length (get node :children)))
1335
+ (js-call children :push (select-invariant-copies (js-nth i (get node :children)) guard arm))
1336
+ (setq i (1+ i)))
1337
+ (typed (get node :kind) (get node :type) children (get node :value)))))
1338
+
1339
+ (defun unswitch-numeric-loops (node)
1340
+ (let ((children (js-array)) (kind (get node :kind)) (i 0))
1341
+ (while (< i (js-length (get node :children)))
1342
+ (js-call children :push (unswitch-numeric-loops (js-nth i (get node :children)))) (setq i (1+ i)))
1343
+ (let ((copy (typed kind (get node :type) children (get node :value))))
1344
+ (if (and (or (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
1345
+ (<= (tree-size (js-nth 1 children)) 512))
1346
+ (let* ((body (js-nth 1 children)) (choice (invariant-if-path body copy (js-array))))
1347
+ (if choice
1348
+ (typed "if" (get copy :type) (js-array (get choice :condition)
1349
+ (typed kind (get copy :type) (js-array (js-nth 0 children)
1350
+ (select-invariant-copies body (get choice :condition) 1)) (get copy :value))
1351
+ (typed kind (get copy :type) (js-array (js-nth 0 children)
1352
+ (select-invariant-copies body (get choice :condition) 2)) (get copy :value))) nil)
1353
+ copy))
1354
+ copy))))
1355
+
1356
+ (defun emit-numeric-javascript (program)
1357
+ ;; Validate unreachable code in the original IR before optimization can
1358
+ ;; remove it. Required Number arguments/results justify raw JS operators.
1359
+ (lower-numeric-program program)
1360
+ (emit-module-numeric (optimize-program program)))
1361
+
1362
+ (export emit-numeric-javascript lower-scalar-program lower-numeric-program optimize-numeric-program numeric-pure tree-size backend-fail find-function))