eliscript 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/bootstrap/README.md +26 -0
- package/bootstrap/backends/README.md +515 -0
- package/bootstrap/backends/async.eli +230 -0
- package/bootstrap/backends/binary.eli +173 -0
- package/bootstrap/backends/capabilities.eli +74 -0
- package/bootstrap/backends/elf.eli +246 -0
- package/bootstrap/backends/managed.eli +5976 -0
- package/bootstrap/backends/memory.eli +100 -0
- package/bootstrap/backends/native.eli +2000 -0
- package/bootstrap/backends/numeric.eli +1362 -0
- package/bootstrap/backends/optimize.eli +79 -0
- package/bootstrap/backends/regexp.eli +2263 -0
- package/bootstrap/backends/sha256.eli +138 -0
- package/bootstrap/backends/wasm.eli +1161 -0
- package/bootstrap/compiler/analyzer.eli +165 -77
- package/bootstrap/compiler/compiler.eli +43 -19
- package/bootstrap/compiler/emitter.eli +780 -170
- package/bootstrap/compiler/expander.eli +136 -45
- package/bootstrap/compiler/formatter.eli +8 -1
- package/bootstrap/compiler/ir.eli +148 -45
- package/bootstrap/compiler/lower.eli +29 -2
- package/bootstrap/compiler/project.eli +51 -21
- package/bootstrap/compiler/reader.eli +128 -36
- package/bootstrap/compiler/source-map.eli +46 -4
- package/bootstrap/compiler/symbol.eli +2 -1
- package/bootstrap/compiler/syntax.eli +47 -12
- package/bootstrap/compiler/transient-analysis.eli +40 -4
- package/bootstrap/host/check-cli-legacy.mjs +163 -0
- package/bootstrap/host/check-cli.mjs +59 -158
- package/bootstrap/host/check-cli.mjs.map +1 -0
- package/bootstrap/host/compiler-session.eli +1579 -0
- package/bootstrap/host/compiler-tools.eli +513 -0
- package/bootstrap/host/eliscript-cli.mjs +53 -33
- package/bootstrap/host/eliscript-cli.mjs.map +1 -0
- package/bootstrap/host/project-cli-legacy.mjs +295 -0
- package/bootstrap/host/project-cli.mjs +74 -289
- package/bootstrap/host/project-cli.mjs.map +1 -0
- package/bootstrap/host/project-legacy.mjs +1080 -0
- package/bootstrap/host/project.mjs +210 -1060
- package/bootstrap/host/project.mjs.map +1 -0
- package/bootstrap/runtime/UNICODE-LICENSE.txt +39 -0
- package/bootstrap/runtime/memory.eli +30379 -0
- package/browser/host.mjs +732 -11
- package/browser/worker.mjs +6 -3
- package/compiler/eliscript-analyzer.el +371 -4
- package/compiler/eliscript-cli.el +84 -1
- package/compiler/eliscript-emitter.el +1189 -256
- package/compiler/eliscript-expander.el +147 -10
- package/compiler/eliscript-ir-emitter.el +725 -208
- package/compiler/eliscript-lower.el +127 -1
- package/compiler/eliscript-portable.el +177 -51
- package/compiler/eliscript-project.el +20 -2
- package/compiler/eliscript-reader.el +141 -57
- package/compiler/eliscript-source-map.el +26 -12
- package/compiler/eliscript-transient-analysis.el +382 -19
- package/dist/backends/async.mjs +561 -0
- package/dist/backends/async.mjs.macro-origins.json +1 -0
- package/dist/backends/async.mjs.map +1 -0
- package/dist/backends/binary.mjs +381 -0
- package/dist/backends/binary.mjs.macro-origins.json +1 -0
- package/dist/backends/binary.mjs.map +1 -0
- package/dist/backends/capabilities.mjs +237 -0
- package/dist/backends/capabilities.mjs.macro-origins.json +1 -0
- package/dist/backends/capabilities.mjs.map +1 -0
- package/dist/backends/elf.mjs +540 -0
- package/dist/backends/elf.mjs.macro-origins.json +1 -0
- package/dist/backends/elf.mjs.map +1 -0
- package/dist/backends/managed.mjs +10616 -0
- package/dist/backends/managed.mjs.macro-origins.json +1 -0
- package/dist/backends/managed.mjs.map +1 -0
- package/dist/backends/memory.mjs +278 -0
- package/dist/backends/memory.mjs.macro-origins.json +1 -0
- package/dist/backends/memory.mjs.map +1 -0
- package/dist/backends/native.mjs +4099 -0
- package/dist/backends/native.mjs.macro-origins.json +1 -0
- package/dist/backends/native.mjs.map +1 -0
- package/dist/backends/numeric.mjs +2458 -0
- package/dist/backends/numeric.mjs.macro-origins.json +1 -0
- package/dist/backends/numeric.mjs.map +1 -0
- package/dist/backends/optimize.mjs +204 -0
- package/dist/backends/optimize.mjs.macro-origins.json +1 -0
- package/dist/backends/optimize.mjs.map +1 -0
- package/dist/backends/regexp.mjs +2033 -0
- package/dist/backends/regexp.mjs.macro-origins.json +1 -0
- package/dist/backends/regexp.mjs.map +1 -0
- package/dist/backends/sha256.mjs +310 -0
- package/dist/backends/sha256.mjs.macro-origins.json +1 -0
- package/dist/backends/sha256.mjs.map +1 -0
- package/dist/backends/wasm.mjs +2258 -0
- package/dist/backends/wasm.mjs.macro-origins.json +1 -0
- package/dist/backends/wasm.mjs.map +1 -0
- package/dist/bootstrap/analyzer.mjs +1908 -776
- package/dist/bootstrap/analyzer.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -1
- package/dist/bootstrap/compiler.mjs +450 -142
- package/dist/bootstrap/compiler.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/compiler.mjs.map +1 -1
- package/dist/bootstrap/emitter.mjs +3083 -909
- package/dist/bootstrap/emitter.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/emitter.mjs.map +1 -1
- package/dist/bootstrap/expander.mjs +2887 -1233
- package/dist/bootstrap/expander.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/expander.mjs.map +1 -1
- package/dist/bootstrap/formatter.mjs +582 -218
- package/dist/bootstrap/formatter.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/formatter.mjs.map +1 -1
- package/dist/bootstrap/ir.mjs +670 -116
- package/dist/bootstrap/ir.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/ir.mjs.map +1 -1
- package/dist/bootstrap/lower.mjs +866 -257
- package/dist/bootstrap/lower.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/lower.mjs.map +1 -1
- package/dist/bootstrap/package.json +1 -0
- package/dist/bootstrap/project.mjs +1168 -498
- package/dist/bootstrap/project.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/project.mjs.map +1 -1
- package/dist/bootstrap/reader.mjs +674 -175
- package/dist/bootstrap/reader.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/reader.mjs.map +1 -1
- package/dist/bootstrap/runtime/math.mjs +1117 -0
- package/dist/bootstrap/runtime/math.mjs.map +1 -0
- package/dist/bootstrap/runtime/unicode-case-data.mjs +10125 -0
- package/dist/bootstrap/runtime/unicode-case-data.mjs.map +1 -0
- package/dist/bootstrap/runtime/unicode-case.mjs +281 -0
- package/dist/bootstrap/runtime/unicode-case.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +430 -119
- package/dist/bootstrap/source-map.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/source-map.mjs.map +1 -1
- package/dist/bootstrap/symbol.mjs +229 -53
- package/dist/bootstrap/symbol.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/symbol.mjs.map +1 -1
- package/dist/bootstrap/syntax.mjs +205 -7
- package/dist/bootstrap/syntax.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/syntax.mjs.map +1 -1
- package/dist/bootstrap/transient-analysis.mjs +1185 -489
- package/dist/bootstrap/transient-analysis.mjs.macro-origins.json +1 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -1
- package/dist/browser/compiler.js +199 -6478
- package/dist/browser/host.mjs +1078 -0
- package/dist/browser/loader.js +1 -0
- package/dist/browser/loader.mjs +231 -0
- package/dist/browser/worker.mjs +63 -0
- package/dist/compiler-artifact.json +1 -0
- package/dist/host/compiler-session.mjs +2980 -0
- package/dist/host/compiler-session.mjs.macro-origins.json +1 -0
- package/dist/host/compiler-session.mjs.map +1 -0
- package/dist/host/compiler-tools.mjs +1084 -0
- package/dist/host/compiler-tools.mjs.macro-origins.json +1 -0
- package/dist/host/compiler-tools.mjs.map +1 -0
- package/dist/literal-sources/runtime/core/collection-internals.eli +60 -0
- package/dist/literal-sources/runtime/core/collection.eli +639 -0
- package/dist/literal-sources/runtime/core/data-text.eli +290 -0
- package/dist/literal-sources/runtime/core/data.eli +22 -0
- package/dist/literal-sources/runtime/core/identifier.eli +44 -0
- package/dist/literal-sources/runtime/core/lazy-sequence.eli +131 -0
- package/dist/literal-sources/runtime/core/list.eli +19 -0
- package/dist/literal-sources/runtime/core/map.eli +9 -0
- package/dist/literal-sources/runtime/core/metadata-internals.eli +6 -0
- package/dist/literal-sources/runtime/core/metadata.eli +40 -0
- package/dist/literal-sources/runtime/core/object.eli +8 -0
- package/dist/literal-sources/runtime/core/order.eli +123 -0
- package/dist/literal-sources/runtime/core/persistent-kind.eli +28 -0
- package/dist/literal-sources/runtime/core/protocol-error.eli +32 -0
- package/dist/literal-sources/runtime/core/protocol.eli +28 -0
- package/dist/literal-sources/runtime/core/queue.eli +9 -0
- package/dist/literal-sources/runtime/core/record.eli +191 -0
- package/dist/literal-sources/runtime/core/regex.eli +158 -0
- package/dist/literal-sources/runtime/core/sequence.eli +305 -0
- package/dist/literal-sources/runtime/core/set-algebra.eli +28 -0
- package/dist/literal-sources/runtime/core/set.eli +9 -0
- package/dist/literal-sources/runtime/core/sorted.eli +408 -0
- package/dist/literal-sources/runtime/core/text.eli +14 -0
- package/dist/literal-sources/runtime/core/transducer.eli +268 -0
- package/dist/literal-sources/runtime/core/transient.eli +17 -0
- package/dist/literal-sources/runtime/core/truth.eli +4 -0
- package/dist/literal-sources/runtime/core/type.eli +232 -0
- package/dist/literal-sources/runtime/core/value-internals.eli +13 -0
- package/dist/literal-sources/runtime/core/value.eli +19 -0
- package/dist/literal-sources/runtime/core/vector.eli +25 -0
- package/dist/literal-sources/runtime/literals.eli +23 -0
- package/dist/literal-sources/runtime/testing/value.eli +6 -0
- package/dist/literal-sources/runtime/wire/http-rpc.eli +362 -0
- package/dist/literal-sources/runtime/wire/markdown.eli +275 -0
- package/dist/literal-sources/runtime/wire/schema.eli +193 -0
- package/dist/literal-sources/stdlib/bit.eli +36 -0
- package/dist/literal-sources/stdlib/core/data.eli +306 -0
- package/dist/literal-sources/stdlib/core/object.eli +89 -0
- package/dist/literal-sources/stdlib/core/protocol.eli +451 -0
- package/dist/literal-sources/stdlib/core/set.eli +312 -0
- package/dist/literal-sources/stdlib/core/text.eli +103 -0
- package/dist/literal-sources/stdlib/managed-literal-protocols.eli +494 -0
- package/dist/literal-sources/stdlib/managed-literals.eli +1469 -0
- package/dist/literal-sources/stdlib/managed-queue-state.eli +65 -0
- package/dist/literal-sources/stdlib/managed-sorted-state.eli +233 -0
- package/dist/literal-sources/stdlib/managed-transient-state.eli +348 -0
- package/dist/literal-sources/stdlib/managed-value-state.eli +153 -0
- package/dist/literal-sources/stdlib/managed-vector-view-state.eli +102 -0
- package/dist/literal-sources/stdlib/persistent-list.eli +145 -0
- package/dist/literal-sources/stdlib/persistent-map.eli +724 -0
- package/dist/literal-sources/stdlib/persistent-set.eli +236 -0
- package/dist/literal-sources/stdlib/persistent-vector.eli +359 -0
- package/dist/literal-sources/stdlib/value.eli +325 -0
- package/dist/package.json +10 -0
- package/dist/runtime/math.mjs +1116 -0
- package/dist/runtime/math.mjs.macro-origins.json +1 -0
- package/dist/runtime/math.mjs.map +1 -0
- package/dist/runtime/unicode-case-data.mjs +10124 -0
- package/dist/runtime/unicode-case-data.mjs.macro-origins.json +1 -0
- package/dist/runtime/unicode-case-data.mjs.map +1 -0
- package/dist/runtime/unicode-case.mjs +280 -0
- package/dist/runtime/unicode-case.mjs.macro-origins.json +1 -0
- package/dist/runtime/unicode-case.mjs.map +1 -0
- package/dist/runtime.eli +30379 -0
- package/dist/stage3-release.json +1 -0
- package/editor/README.md +1 -1
- package/editor/eliscript-mode.el +1 -1
- package/editor/eliscript-repl.el +1 -1
- package/package.json +106 -11
- package/platform/http.mjs +20 -0
- package/runtime/array-text.mjs +159 -0
- package/runtime/core/map-internals.mjs +27 -9
- package/runtime/core/object-impl.mjs +26 -17
- package/runtime/core/object-impl.mjs.map +1 -1
- package/runtime/core/protocol-impl.mjs +682 -255
- package/runtime/core/protocol-impl.mjs.map +1 -1
- package/runtime/core/protocol.mjs +4 -0
- package/runtime/core/record.mjs +23 -2
- package/runtime/core/regex.mjs +4 -0
- package/runtime/core/sorted.mjs +53 -0
- package/runtime/core/text-impl.mjs +167 -48
- package/runtime/core/text-impl.mjs.map +1 -1
- package/runtime/core/type.mjs +75 -3
- package/runtime/core/vector.mjs +6 -0
- package/runtime/math.eli +429 -0
- package/runtime/math.mjs +1116 -0
- package/runtime/math.mjs.map +1 -0
- package/runtime/number-radix.mjs +105 -0
- package/runtime/unicode-case-data.eli +29 -0
- package/runtime/unicode-case-data.mjs +10124 -0
- package/runtime/unicode-case-data.mjs.map +1 -0
- package/runtime/unicode-case.eli +100 -0
- package/runtime/unicode-case.mjs +280 -0
- package/runtime/unicode-case.mjs.map +1 -0
- package/runtime/wire/http-rpc.mjs +168 -0
- package/runtime/wire/markdown.mjs +196 -0
- package/runtime/wire/schema.mjs +109 -0
- package/stdlib/README.md +54 -0
- package/stdlib/core/protocol.eli +81 -17
- package/stdlib/core/seq.eli +1 -1
- package/stdlib/core/zipper.eli +172 -72
- package/stdlib/gepo.eli +12 -0
- package/stdlib/interop/js.eli +56 -15
- package/stdlib/managed-literal-protocols.eli +494 -0
- package/stdlib/managed-literals.eli +1469 -0
- package/stdlib/managed-protocol-error.eli +32 -0
- package/stdlib/managed-queue-state.eli +65 -0
- package/stdlib/managed-runtime-collection-internals.eli +60 -0
- package/stdlib/managed-runtime-collection.eli +639 -0
- package/stdlib/managed-runtime-data-text.eli +290 -0
- package/stdlib/managed-runtime-data.eli +22 -0
- package/stdlib/managed-runtime-identifier.eli +44 -0
- package/stdlib/managed-runtime-lazy-sequence.eli +131 -0
- package/stdlib/managed-runtime-list.eli +19 -0
- package/stdlib/managed-runtime-literals.eli +23 -0
- package/stdlib/managed-runtime-map.eli +9 -0
- package/stdlib/managed-runtime-metadata-internals.eli +6 -0
- package/stdlib/managed-runtime-metadata.eli +40 -0
- package/stdlib/managed-runtime-object.eli +8 -0
- package/stdlib/managed-runtime-order.eli +123 -0
- package/stdlib/managed-runtime-persistent-kind.eli +28 -0
- package/stdlib/managed-runtime-protocol.eli +28 -0
- package/stdlib/managed-runtime-queue.eli +9 -0
- package/stdlib/managed-runtime-record.eli +191 -0
- package/stdlib/managed-runtime-regex.eli +158 -0
- package/stdlib/managed-runtime-sequence.eli +305 -0
- package/stdlib/managed-runtime-set-algebra.eli +28 -0
- package/stdlib/managed-runtime-set.eli +9 -0
- package/stdlib/managed-runtime-sorted.eli +408 -0
- package/stdlib/managed-runtime-text.eli +14 -0
- package/stdlib/managed-runtime-transducer.eli +268 -0
- package/stdlib/managed-runtime-transient.eli +17 -0
- package/stdlib/managed-runtime-truth.eli +4 -0
- package/stdlib/managed-runtime-type.eli +232 -0
- package/stdlib/managed-runtime-value-internals.eli +13 -0
- package/stdlib/managed-runtime-value.eli +19 -0
- package/stdlib/managed-runtime-vector.eli +25 -0
- package/stdlib/managed-sorted-state.eli +233 -0
- package/stdlib/managed-testing-value.eli +6 -0
- package/stdlib/managed-transient-state.eli +348 -0
- package/stdlib/managed-value-state.eli +153 -0
- package/stdlib/managed-vector-view-state.eli +102 -0
- package/stdlib/managed-wire-http-rpc.eli +362 -0
- package/stdlib/managed-wire-markdown.eli +275 -0
- package/stdlib/managed-wire-schema.eli +193 -0
- package/stdlib/mdp.eli +7 -0
- package/stdlib/mds.eli +4 -0
- package/stdlib/persistent-vector.eli +23 -6
- package/tools/acceptance/check-legacy.mjs +687 -0
- package/tools/acceptance/check.eli +431 -0
- package/tools/acceptance/compile-evidence.el +49 -0
- package/tools/acceptance/finalize-legacy.mjs +498 -0
- package/tools/acceptance/finalize.eli +270 -0
- package/tools/backends/benchmark.mjs +175 -0
- package/tools/backends/build.mjs +135 -0
- package/tools/backends/compile.mjs +94 -0
- package/tools/backends/compiler-bootstrap.mjs +1001 -0
- package/tools/backends/compiler-browser.mjs +361 -0
- package/tools/backends/compiler-build.mjs +1628 -0
- package/tools/backends/compiler-cli-legacy.mjs +334 -0
- package/tools/backends/compiler-cli.mjs +29 -0
- package/tools/backends/compiler-corpus-bit.mjs +234 -0
- package/tools/backends/compiler-corpus-cli-adapters-legacy.mjs +362 -0
- package/tools/backends/compiler-corpus-cli-adapters.mjs +247 -0
- package/tools/backends/compiler-corpus-debugging.mjs +221 -0
- package/tools/backends/compiler-corpus-emitter.mjs +324 -0
- package/tools/backends/compiler-corpus-evaluation-adapters.mjs +65 -0
- package/tools/backends/compiler-corpus-execution-legacy.mjs +769 -0
- package/tools/backends/compiler-corpus-execution.mjs +3324 -0
- package/tools/backends/compiler-corpus-fuzz.mjs +229 -0
- package/tools/backends/compiler-corpus-generations.mjs +208 -0
- package/tools/backends/compiler-corpus-host-context-legacy.mjs +114 -0
- package/tools/backends/compiler-corpus-host-context.mjs +550 -0
- package/tools/backends/compiler-corpus-identity-host.mjs +553 -0
- package/tools/backends/compiler-corpus-identity-value-adapters-legacy.mjs +298 -0
- package/tools/backends/compiler-corpus-identity-value-adapters.mjs +69 -0
- package/tools/backends/compiler-corpus-isolated-exports.mjs +21 -0
- package/tools/backends/compiler-corpus-legacy.mjs +1155 -0
- package/tools/backends/compiler-corpus-literal-provider.mjs +282 -0
- package/tools/backends/compiler-corpus-managed-adapters-legacy.mjs +334 -0
- package/tools/backends/compiler-corpus-managed-adapters.mjs +937 -0
- package/tools/backends/compiler-corpus-managed-cases.mjs +179 -0
- package/tools/backends/compiler-corpus-managed-fixtures-legacy.mjs +879 -0
- package/tools/backends/compiler-corpus-managed-fixtures.mjs +1287 -0
- package/tools/backends/compiler-corpus-matchers.mjs +173 -0
- package/tools/backends/compiler-corpus-original-program.mjs +612 -0
- package/tools/backends/compiler-corpus-process.mjs +765 -0
- package/tools/backends/compiler-corpus-program-driver.mjs +306 -0
- package/tools/backends/compiler-corpus-project-adapters-legacy.mjs +497 -0
- package/tools/backends/compiler-corpus-project-adapters.mjs +340 -0
- package/tools/backends/compiler-corpus-project-entry-host.mjs +511 -0
- package/tools/backends/compiler-corpus-project-transport.mjs +151 -0
- package/tools/backends/compiler-corpus-raw-fixture-observer.mjs +1438 -0
- package/tools/backends/compiler-corpus-raw-language-fixtures.mjs +1456 -0
- package/tools/backends/compiler-corpus-raw-math-fixture.mjs +346 -0
- package/tools/backends/compiler-corpus-raw-special-fixtures.mjs +523 -0
- package/tools/backends/compiler-corpus-reference-legacy.mjs +263 -0
- package/tools/backends/compiler-corpus-reference.mjs +241 -0
- package/tools/backends/compiler-corpus-runtime-adapters-legacy.mjs +843 -0
- package/tools/backends/compiler-corpus-runtime-adapters.mjs +663 -0
- package/tools/backends/compiler-corpus-source-archive.mjs +206 -0
- package/tools/backends/compiler-corpus-tools-artifact-adapters.mjs +410 -0
- package/tools/backends/compiler-corpus-tools-cli.mjs +363 -0
- package/tools/backends/compiler-corpus-tools-reference.mjs +264 -0
- package/tools/backends/compiler-corpus.mjs +2054 -0
- package/tools/backends/compiler-debug.mjs +142 -0
- package/tools/backends/compiler-matrix.mjs +1955 -0
- package/tools/backends/compiler-metadata.mjs +203 -0
- package/tools/backends/compiler-package-browser.mjs +120 -0
- package/tools/backends/compiler-package-legacy.mjs +654 -0
- package/tools/backends/compiler-package.mjs +464 -0
- package/tools/backends/compiler-project.mjs +126 -0
- package/tools/backends/compiler-session.mjs +843 -0
- package/tools/backends/compiler-sha256.mjs +152 -0
- package/tools/backends/compiler-unicode-profile-host.mjs +196 -0
- package/tools/backends/compiler-worker.mjs +573 -0
- package/tools/backends/dist/bootstrap-js-stages.mjs +919 -0
- package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -0
- package/tools/backends/dist/managed-ir-inventory.mjs +488 -0
- package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -0
- package/tools/backends/dist/record-grid.mjs +1256 -0
- package/tools/backends/dist/record-grid.mjs.map +1 -0
- package/tools/backends/dist/release-cli.mjs +462 -0
- package/tools/backends/dist/release-cli.mjs.map +1 -0
- package/tools/backends/dist/run-artifact.mjs +517 -0
- package/tools/backends/dist/run-artifact.mjs.map +1 -0
- package/tools/backends/eliscript/compiler-corpus-bit.eli +77 -0
- package/tools/backends/eliscript/compiler-corpus-debugging.eli +70 -0
- package/tools/backends/eliscript/compiler-corpus-project-entry-host.eli +256 -0
- package/tools/backends/managed-foreign-binding.mjs +497 -0
- package/tools/backends/managed-host-legacy.mjs +1181 -0
- package/tools/backends/managed-host-lifecycle.mjs +310 -0
- package/tools/backends/managed-host-object-provider.mjs +155 -0
- package/tools/backends/managed-host-object-realm.mjs +797 -0
- package/tools/backends/managed-host-proxy.mjs +71 -0
- package/tools/backends/managed-host-runtime.mjs +5868 -0
- package/tools/backends/managed-host.mjs +716 -0
- package/tools/backends/managed-module-graph-legacy.mjs +308 -0
- package/tools/backends/managed-module-graph.mjs +694 -0
- package/tools/backends/managed-runtime-host-legacy.mjs +195 -0
- package/tools/backends/managed-runtime.mjs +939 -0
- package/tools/backends/managed-source-view-legacy.mjs +332 -0
- package/tools/backends/managed-source-view.mjs +959 -0
- package/tools/backends/memory-runtime.mjs +100 -0
- package/tools/backends/native-benchmark.mjs +88 -0
- package/tools/backends/native-managed-runtime.mjs +677 -0
- package/tools/backends/native-memory.mjs +768 -0
- package/tools/backends/native-node-memory.mjs +142 -0
- package/tools/backends/native-node-runner.mjs +99 -0
- package/tools/backends/native-node-watchdog.mjs +19 -0
- package/tools/backends/native-node-worker.mjs +75 -0
- package/tools/backends/stage3-targets.mjs +493 -0
- package/tools/bootstrap-command.el +13 -0
- package/tools/bootstrap-runtime.el +101 -0
- package/tools/bootstrap-seed.el +55 -0
- package/tools/project/check-cli.eli +31 -0
- package/tools/project/project-cli.eli +42 -0
- package/tools/project/project.eli +122 -0
- package/tools/runtime/unicode-case-profiles.mjs +13 -0
- package/tools/runtime/unicode-case-runtime.el +57 -0
- package/tools/testing/source-debugging-corpus-host.eli +16 -0
- package/tools/testing/source-debugging.eli +139 -0
- package/tools/testing/stdlib-bit-corpus-host.eli +21 -0
- package/tools/testing/stdlib-bit-corpus-host.mjs +118 -0
- package/tools/testing/stdlib-bit.eli +104 -0
- package/tools/testing/stdlib-bit.mjs +255 -0
- package/bin/eliscript-bootstrap +0 -15
|
@@ -0,0 +1,2000 @@
|
|
|
1
|
+
(module eliscript.backend.native
|
|
2
|
+
(import "./optimize.mjs" optimize-program)
|
|
3
|
+
(import "./numeric.mjs" lower-numeric-program optimize-numeric-program numeric-pure backend-fail find-function)
|
|
4
|
+
(import "./sha256.mjs" sha256-bytes sha256-words-le)
|
|
5
|
+
(import "./memory.mjs" lower-memory-program)
|
|
6
|
+
(import "./elf.mjs" emit-elf-object emit-elf-library)
|
|
7
|
+
(import "./capabilities.mjs" prepare-host-capabilities prepare-fresh-host-capabilities host-capability-index)
|
|
8
|
+
(import "./binary.mjs" binary-output binary-byte binary-u32 binary-append binary-append-owned binary-slice binary-finish)
|
|
9
|
+
|
|
10
|
+
;; ARM64 numeric-f64 instruction and Mach-O image emission. Selection,
|
|
11
|
+
;; encoding, linking metadata and ad-hoc page signatures are independent.
|
|
12
|
+
;; A memory image owns immutable 1024-word UTF16 blocks and one mutable
|
|
13
|
+
;; block. Relocations may revisit any block, but never unpack the image.
|
|
14
|
+
;; Numeric assembly helpers keep their existing ordinary-array contract.
|
|
15
|
+
(defun native-word-buffer ()
|
|
16
|
+
(js-object :length 0 :chunks (js-array) :cache (js-array) :units (js-array) :cacheIndex -1 :dirty false))
|
|
17
|
+
|
|
18
|
+
(defun native-word-flush (words)
|
|
19
|
+
(when (get words :dirty)
|
|
20
|
+
(let ((cache (get words :cache)) (units (get words :units nil)) (index 0))
|
|
21
|
+
(unless units (setq units (js-array)) (put words :units units))
|
|
22
|
+
(put units :length 0)
|
|
23
|
+
(while (< index (js-length cache))
|
|
24
|
+
(unless (Object/hasOwn cache index) (backend-fail nil "native word buffer contains an unwritten word"))
|
|
25
|
+
(let ((value (js-nth index cache)))
|
|
26
|
+
(js-call units :push (bit-and value 65535) (unsigned-bit-shift-right value 16)))
|
|
27
|
+
(setq index (1+ index)))
|
|
28
|
+
(put (get words :chunks) (get words :cacheIndex)
|
|
29
|
+
(Reflect/apply (get globalThis.String :fromCharCode) nil units))
|
|
30
|
+
(put words :dirty false))) words)
|
|
31
|
+
|
|
32
|
+
(defun native-word-cache (words block)
|
|
33
|
+
(unless (= block (get words :cacheIndex))
|
|
34
|
+
(native-word-flush words)
|
|
35
|
+
(let ((text (js-nth block (get words :chunks))) (cache (get words :cache)) (index 0))
|
|
36
|
+
;; The preceding flush published an immutable string. Both numeric
|
|
37
|
+
;; scratch arrays are private to this buffer and bounded by one block.
|
|
38
|
+
(put cache :length 0)
|
|
39
|
+
(when (= (value-type text) "string")
|
|
40
|
+
(while (< index (js-length text))
|
|
41
|
+
(js-call cache :push (+ (string-code-unit-at text index)
|
|
42
|
+
(* (string-code-unit-at text (1+ index)) 65536)))
|
|
43
|
+
(setq index (+ index 2))))
|
|
44
|
+
(put words :cacheIndex block)))
|
|
45
|
+
(get words :cache))
|
|
46
|
+
|
|
47
|
+
(defun native-word-at (words index)
|
|
48
|
+
(if (Array/isArray words) (js-nth index words)
|
|
49
|
+
(progn
|
|
50
|
+
(unless (and (Number/isSafeInteger index) (>= index 0) (< index (js-length words)))
|
|
51
|
+
(backend-fail nil "native word buffer index is out of range"))
|
|
52
|
+
(let ((block (js-call globalThis.Math :floor (/ index 1024))) (offset (% index 1024)))
|
|
53
|
+
(if (= block (get words :cacheIndex))
|
|
54
|
+
(let ((cache (get words :cache)))
|
|
55
|
+
(unless (Object/hasOwn cache offset) (backend-fail nil "native word buffer contains an unwritten word"))
|
|
56
|
+
(js-nth offset cache))
|
|
57
|
+
(let ((text (js-nth block (get words :chunks))))
|
|
58
|
+
(unless (and (= (value-type text) "string") (< (1+ (* offset 2)) (js-length text)))
|
|
59
|
+
(backend-fail nil "native word buffer contains an unwritten word"))
|
|
60
|
+
(+ (string-code-unit-at text (* offset 2))
|
|
61
|
+
(* (string-code-unit-at text (1+ (* offset 2))) 65536))))))))
|
|
62
|
+
|
|
63
|
+
(defun native-word-set (words index value)
|
|
64
|
+
(if (Array/isArray words) (put words index value)
|
|
65
|
+
(progn
|
|
66
|
+
(unless (and (Number/isSafeInteger index) (>= index 0) (< index (js-length words)))
|
|
67
|
+
(backend-fail nil "native word buffer index is out of range"))
|
|
68
|
+
(put (native-word-cache words (js-call globalThis.Math :floor (/ index 1024))) (% index 1024) (uint32 value))
|
|
69
|
+
(put words :dirty t))) value)
|
|
70
|
+
|
|
71
|
+
(defun native-word-resize (words length)
|
|
72
|
+
(if (Array/isArray words) (put words :length length)
|
|
73
|
+
(progn
|
|
74
|
+
(unless (and (Number/isSafeInteger length) (>= length 0))
|
|
75
|
+
(backend-fail nil "native word buffer length is invalid"))
|
|
76
|
+
(when (< length (js-length words))
|
|
77
|
+
(let ((block (js-call globalThis.Math :floor (/ length 1024))) (tail (% length 1024))
|
|
78
|
+
(cached (get words :cacheIndex)) (chunks (get words :chunks)))
|
|
79
|
+
(when (< (* cached 1024) length)
|
|
80
|
+
(when (= cached block)
|
|
81
|
+
(let ((cache (get words :cache)))
|
|
82
|
+
(when (> (js-length cache) tail) (put cache :length tail))))
|
|
83
|
+
(native-word-flush words))
|
|
84
|
+
(put (get words :cache) :length 0) (put words :cacheIndex -1) (put words :dirty false)
|
|
85
|
+
(put chunks :length (+ block (if (> tail 0) 1 0)))
|
|
86
|
+
(when (and (> tail 0) (= (value-type (js-nth block chunks)) "string"))
|
|
87
|
+
(put chunks block (js-call (js-nth block chunks) :slice 0 (* tail 2))))))
|
|
88
|
+
(put words :length length))) length)
|
|
89
|
+
|
|
90
|
+
(defun native-word-push (words value)
|
|
91
|
+
(if (Array/isArray words) (js-call words :push (uint32 value))
|
|
92
|
+
(let ((index (js-length words)))
|
|
93
|
+
(native-word-resize words (1+ index)) (native-word-set words index value) (js-length words))))
|
|
94
|
+
|
|
95
|
+
(defun word (state value)
|
|
96
|
+
(native-word-push (get state :words) value))
|
|
97
|
+
|
|
98
|
+
(defun native-append-words (output words)
|
|
99
|
+
(if (or (Array/isArray output) (Array/isArray words))
|
|
100
|
+
(let ((index 0))
|
|
101
|
+
(while (< index (js-length words))
|
|
102
|
+
(binary-u32 output (native-word-at words index)) (setq index (1+ index))))
|
|
103
|
+
(progn
|
|
104
|
+
;; Both are emitter-owned writers. Publish the final mutable word block
|
|
105
|
+
;; before sharing its immutable UTF16 strings. The temporary byte view
|
|
106
|
+
;; owns only its fields: consuming it never clears words.chunks/cache.
|
|
107
|
+
;; Aligned Mach-O text transfers whole strings; the object prefix uses
|
|
108
|
+
;; binary's bounded carry without constructing a complete byte copy.
|
|
109
|
+
(native-word-flush words)
|
|
110
|
+
(binary-append-owned output (js-object :length (* (js-length words) 4)
|
|
111
|
+
:chunks (get words :chunks) :pending (js-array) :low -1)))) output)
|
|
112
|
+
|
|
113
|
+
(defun u32 (bytes value)
|
|
114
|
+
(binary-u32 bytes value))
|
|
115
|
+
|
|
116
|
+
(defun u64 (bytes value)
|
|
117
|
+
(u32 bytes (uint32 value))
|
|
118
|
+
(u32 bytes (uint32 (/ value 4294967296))))
|
|
119
|
+
|
|
120
|
+
(defun zeros (bytes count)
|
|
121
|
+
(let ((i 0)) (while (< i count) (binary-byte bytes 0) (setq i (1+ i)))))
|
|
122
|
+
|
|
123
|
+
(defun ascii-field (bytes name width)
|
|
124
|
+
(let ((i 0))
|
|
125
|
+
(while (< i width)
|
|
126
|
+
(binary-byte bytes (if (< i (js-length name)) (string-code-unit-at name i) 0))
|
|
127
|
+
(setq i (1+ i)))))
|
|
128
|
+
|
|
129
|
+
(defun string-bytes (bytes name)
|
|
130
|
+
(let ((i 0))
|
|
131
|
+
(while (< i (js-length name))
|
|
132
|
+
(let ((code (string-code-unit-at name i)))
|
|
133
|
+
(when (= code 0) (backend-fail nil "native symbol contains NUL"))
|
|
134
|
+
(when (and (>= code 55296) (<= code 56319))
|
|
135
|
+
(let ((next (if (< (1+ i) (js-length name)) (string-code-unit-at name (1+ i)) 0)))
|
|
136
|
+
(unless (and (>= next 56320) (<= next 57343))
|
|
137
|
+
(backend-fail nil "native symbol contains an unpaired surrogate"))
|
|
138
|
+
(setq code (+ 65536 (* (- code 55296) 1024) (- next 56320)))
|
|
139
|
+
(setq i (1+ i))))
|
|
140
|
+
(when (and (>= code 56320) (<= code 57343))
|
|
141
|
+
(backend-fail nil "native symbol contains an unpaired surrogate"))
|
|
142
|
+
(cond
|
|
143
|
+
((< code 128) (js-call bytes :push code))
|
|
144
|
+
((< code 2048) (js-call bytes :push
|
|
145
|
+
(bit-or 192 (unsigned-bit-shift-right code 6)) (bit-or 128 (bit-and code 63))))
|
|
146
|
+
((< code 65536) (js-call bytes :push
|
|
147
|
+
(bit-or 224 (unsigned-bit-shift-right code 12))
|
|
148
|
+
(bit-or 128 (bit-and (unsigned-bit-shift-right code 6) 63))
|
|
149
|
+
(bit-or 128 (bit-and code 63))))
|
|
150
|
+
(t (js-call bytes :push
|
|
151
|
+
(bit-or 240 (unsigned-bit-shift-right code 18))
|
|
152
|
+
(bit-or 128 (bit-and (unsigned-bit-shift-right code 12) 63))
|
|
153
|
+
(bit-or 128 (bit-and (unsigned-bit-shift-right code 6) 63))
|
|
154
|
+
(bit-or 128 (bit-and code 63))))))
|
|
155
|
+
(setq i (1+ i)))
|
|
156
|
+
(js-call bytes :push 0)))
|
|
157
|
+
|
|
158
|
+
(defun integer-register (state register low high)
|
|
159
|
+
(word state (+ 3531603968 (* (bit-and low 65535) 32) register))
|
|
160
|
+
(when (or (not (get state :compact nil)) (/= (unsigned-bit-shift-right low 16) 0))
|
|
161
|
+
(word state (+ 4070572032 (* (unsigned-bit-shift-right low 16) 32) register)))
|
|
162
|
+
(when (or (not (get state :compact nil)) (/= (bit-and high 65535) 0))
|
|
163
|
+
(word state (+ 4072669184 (* (bit-and high 65535) 32) register)))
|
|
164
|
+
(when (or (not (get state :compact nil)) (/= (unsigned-bit-shift-right high 16) 0))
|
|
165
|
+
(word state (+ 4074766336 (* (unsigned-bit-shift-right high 16) 32) register))))
|
|
166
|
+
|
|
167
|
+
(defun memory-integer-register (state register value)
|
|
168
|
+
;; Word values need only W-register writes, which also zero the upper half.
|
|
169
|
+
;; Keep integer-register's fixed-width sequences for relocatable placeholders.
|
|
170
|
+
(let ((low (bit-and value 65535)) (high (unsigned-bit-shift-right value 16)))
|
|
171
|
+
(if (and (= low 0) (/= high 0))
|
|
172
|
+
(word state (+ 1386217472 (* high 32) register)) ;; movz Wd,#high,lsl #16
|
|
173
|
+
(progn
|
|
174
|
+
(word state (+ 1384120320 (* low 32) register)) ;; movz Wd,#low
|
|
175
|
+
(when (/= high 0)
|
|
176
|
+
(word state (+ 1923088384 (* high 32) register))))))) ;; movk Wd,#high,lsl #16
|
|
177
|
+
|
|
178
|
+
(defun float-literal (state value)
|
|
179
|
+
(let* ((bits (number-float64-words value)) (low (js-nth 0 bits)) (high (js-nth 1 bits)))
|
|
180
|
+
(when (and (= value 0) (< (/ 1 value) 0)) (setq high 2147483648))
|
|
181
|
+
(if (and (get state :compact nil) (= low 0) (= high 0))
|
|
182
|
+
(word state 2657551328)
|
|
183
|
+
(if (and (get state :compact nil) (= low 0) (= (bit-and high 65535) 0)
|
|
184
|
+
(>= (bit-and (unsigned-bit-shift-right high 20) 2047) 1020)
|
|
185
|
+
(<= (bit-and (unsigned-bit-shift-right high 20) 2047) 1027))
|
|
186
|
+
(let* ((exponent (- (bit-and (unsigned-bit-shift-right high 20) 2047) 1023))
|
|
187
|
+
(immediate (+ (* (unsigned-bit-shift-right high 31) 128)
|
|
188
|
+
(if (< exponent 1) (+ 64 (* (+ exponent 3) 16)) (* (- exponent 1) 16))
|
|
189
|
+
(bit-and (unsigned-bit-shift-right high 16) 15))))
|
|
190
|
+
(word state (+ 509612032 (* immediate 8192))))
|
|
191
|
+
(if (get state :compact nil)
|
|
192
|
+
(let ((constants (get state :constants)) (index 0) (found nil))
|
|
193
|
+
(while (and (< index (js-length constants)) (not found))
|
|
194
|
+
(let ((entry (js-nth index constants)))
|
|
195
|
+
(when (and (= low (get entry :low)) (= high (get entry :high))) (setq found entry)))
|
|
196
|
+
(setq index (1+ index)))
|
|
197
|
+
(unless found
|
|
198
|
+
(setq found (js-object :low low :high high :uses (js-array)))
|
|
199
|
+
(js-call constants :push found))
|
|
200
|
+
(js-call (get found :uses) :push (js-length (get state :words)))
|
|
201
|
+
(word state 1543503872))
|
|
202
|
+
(progn (integer-register state 9 low high) (word state 2657550624)))))))
|
|
203
|
+
|
|
204
|
+
(defun literal-register (state value)
|
|
205
|
+
(let* ((bits (number-float64-words value)) (entries (get state :literalRegisters (js-array)))
|
|
206
|
+
(i 0) (register -1))
|
|
207
|
+
(while (< i (js-length entries))
|
|
208
|
+
(let ((entry (js-nth i entries)))
|
|
209
|
+
(when (and (= (js-nth 0 bits) (get entry :low)) (= (js-nth 1 bits) (get entry :high)))
|
|
210
|
+
(setq register (get entry :register))))
|
|
211
|
+
(setq i (1+ i)))
|
|
212
|
+
register))
|
|
213
|
+
|
|
214
|
+
(defun collect-loop-literals (node depth entries)
|
|
215
|
+
(let ((kind (get node :kind)) (children (get node :children)) (i 0))
|
|
216
|
+
(when (and (> depth 0) (= kind "literal") (= (get node :type) "f64") (/= (get node :value) 0))
|
|
217
|
+
(let* ((bits (number-float64-words (get node :value))) (found nil) (j 0))
|
|
218
|
+
(while (< j (js-length entries))
|
|
219
|
+
(let ((entry (js-nth j entries)))
|
|
220
|
+
(when (and (= (js-nth 0 bits) (get entry :low)) (= (js-nth 1 bits) (get entry :high)))
|
|
221
|
+
(setq found entry)))
|
|
222
|
+
(setq j (1+ j)))
|
|
223
|
+
(if found (put found :weight (1+ (get found :weight)))
|
|
224
|
+
(js-call entries :push (js-object :low (js-nth 0 bits) :high (js-nth 1 bits)
|
|
225
|
+
:value (get node :value) :weight 1)))))
|
|
226
|
+
(while (< i (js-length children))
|
|
227
|
+
(collect-loop-literals (js-nth i children)
|
|
228
|
+
(if (or (= kind "while") (= kind "remainder-while") (= kind "guarded-while")) (1+ depth) depth) entries)
|
|
229
|
+
(setq i (1+ i)))))
|
|
230
|
+
|
|
231
|
+
(defun allocate-literal-registers (state body)
|
|
232
|
+
;; Only unused allocated-class registers are available. Loop constants are
|
|
233
|
+
;; immutable, bit-exact and initialized after all incoming arguments.
|
|
234
|
+
(let ((entries (js-array)) (selected (js-array)) (registers (get state :registers))
|
|
235
|
+
(r 31))
|
|
236
|
+
(collect-loop-literals body 0 entries)
|
|
237
|
+
(while (and (>= r 2) (> (js-length entries) 0))
|
|
238
|
+
(when (and (or (< r 16) (>= r 24)) (not (js-call registers :includes r)))
|
|
239
|
+
(let ((best 0) (j 1))
|
|
240
|
+
(while (< j (js-length entries))
|
|
241
|
+
(when (> (get (js-nth j entries) :weight) (get (js-nth best entries) :weight)) (setq best j))
|
|
242
|
+
(setq j (1+ j)))
|
|
243
|
+
(let ((entry (js-nth best entries)))
|
|
244
|
+
(put entry :register r) (js-call selected :push entry) (js-call registers :push r)
|
|
245
|
+
(js-call entries :splice best 1))))
|
|
246
|
+
(setq r (1- r)))
|
|
247
|
+
selected))
|
|
248
|
+
|
|
249
|
+
(defun finish-literals (state)
|
|
250
|
+
(let ((constants (get state :constants)) (words (get state :words)) (i 0))
|
|
251
|
+
(when (and (> (js-length constants) 0) (/= (bit-and (js-length words) 1) 0))
|
|
252
|
+
(word state 3573751839))
|
|
253
|
+
(while (< i (js-length constants))
|
|
254
|
+
(let* ((entry (js-nth i constants)) (target (js-length words)) (uses (get entry :uses)) (j 0))
|
|
255
|
+
(while (< j (js-length uses))
|
|
256
|
+
(let* ((at (js-nth j uses)) (distance (- target at)))
|
|
257
|
+
(unless (and (>= distance -262144) (< distance 262144))
|
|
258
|
+
(backend-fail nil "native literal exceeds ARM64 displacement range"))
|
|
259
|
+
(native-word-set words at (bit-or (+ 1543503872 (bit-and (native-word-at words at) 31))
|
|
260
|
+
(* (bit-and distance 524287) 32))))
|
|
261
|
+
(setq j (1+ j)))
|
|
262
|
+
(word state (get entry :low)) (word state (get entry :high)))
|
|
263
|
+
(setq i (1+ i)))))
|
|
264
|
+
|
|
265
|
+
(defun stack-adjust (state amount subtract)
|
|
266
|
+
(while (> amount 0)
|
|
267
|
+
(let ((part (if (< amount 4080) amount 4080)))
|
|
268
|
+
(word state (+ (if subtract 3506439167 2432697343) (* part 1024)))
|
|
269
|
+
(setq amount (- amount part)))))
|
|
270
|
+
|
|
271
|
+
(defun push-value (state type)
|
|
272
|
+
(let ((depth (get state :depth 0)))
|
|
273
|
+
(if (and (get state :leaf nil) (< depth 8))
|
|
274
|
+
(word state (+ (if (= type "f64") 509624320 505872384) (+ 16 depth)))
|
|
275
|
+
(progn (stack-adjust state 16 t)
|
|
276
|
+
(word state (if (= type "f64") 4244636640 3103785952))))
|
|
277
|
+
(put state :depth (1+ depth))))
|
|
278
|
+
|
|
279
|
+
(defun pop-value (state type)
|
|
280
|
+
(let ((depth (1- (get state :depth 1))))
|
|
281
|
+
(put state :depth depth)
|
|
282
|
+
(if (and (get state :leaf nil) (< depth 8))
|
|
283
|
+
(word state (+ (if (= type "f64") 509624321 505806849) (* (+ 16 depth) 32)))
|
|
284
|
+
(progn (word state (if (= type "f64") 4248830945 3107980257))
|
|
285
|
+
(stack-adjust state 16 nil)))))
|
|
286
|
+
|
|
287
|
+
(defun local-access (state index type store)
|
|
288
|
+
(let* ((registers (get state :registers (js-array)))
|
|
289
|
+
(register (if (< index (js-length registers)) (js-nth index registers) -1)))
|
|
290
|
+
(if (<= register -2)
|
|
291
|
+
(let ((physical (- (- register) 2)))
|
|
292
|
+
(word state (+ 704644064 (if store physical (* physical 65536)))))
|
|
293
|
+
(if (>= register 0)
|
|
294
|
+
(word state (if (= type "f64")
|
|
295
|
+
(+ 509624320 (if store register (* register 32)))
|
|
296
|
+
(if store (+ 505872384 register) (+ 505806848 (* register 32)))))
|
|
297
|
+
(let ((offset (* (1+ index) 8)))
|
|
298
|
+
(if (and (get state :memory nil) (<= offset 256))
|
|
299
|
+
;; LDUR/STUR use a signed nine-bit byte offset from FP (x29).
|
|
300
|
+
(word state (+ (if (= type "f64") (if store 4227858432 4232052736)
|
|
301
|
+
(if store 3087007744 3091202048))
|
|
302
|
+
(* (bit-and (- offset) 511) 4096) 928))
|
|
303
|
+
(progn
|
|
304
|
+
(if (and (get state :memory nil) (<= offset 4095))
|
|
305
|
+
(word state (+ 3506439081 (* offset 1024))) ;; sub x9,x29,#offset
|
|
306
|
+
(progn
|
|
307
|
+
(integer-register state 9 offset 0)
|
|
308
|
+
(word state 3406365609))) ;; sub x9,x29,x9
|
|
309
|
+
(word state (if (= type "f64") (if store 4244635936 4248830240)
|
|
310
|
+
(if store 3103785248 3107979552))))))))))
|
|
311
|
+
|
|
312
|
+
(defun register-weights (node counts)
|
|
313
|
+
(let ((kind (get node :kind)) (children (get node :children)) (i 0) (call (= (get node :kind) "call")))
|
|
314
|
+
(when (or (= kind "local") (= kind "set") (= kind "initialize"))
|
|
315
|
+
(let ((index (get node :value)))
|
|
316
|
+
(js-call counts :splice index 1 (1+ (js-nth index counts)))))
|
|
317
|
+
(while (< i (js-length children))
|
|
318
|
+
(when (register-weights (js-nth i children) counts) (setq call t))
|
|
319
|
+
(setq i (1+ i)))
|
|
320
|
+
call))
|
|
321
|
+
|
|
322
|
+
(defun live-union (left right)
|
|
323
|
+
(let ((result (js-call left :slice)) (i 0))
|
|
324
|
+
(while (< i (js-length right))
|
|
325
|
+
(unless (js-call result :includes (js-nth i right)) (js-call result :push (js-nth i right)))
|
|
326
|
+
(setq i (1+ i)))
|
|
327
|
+
result))
|
|
328
|
+
|
|
329
|
+
(defun interference (graph index live)
|
|
330
|
+
(let ((i 0)) (while (< i (js-length live))
|
|
331
|
+
(let ((other (js-nth i live)))
|
|
332
|
+
(unless (= index other)
|
|
333
|
+
(unless (js-call (js-nth index graph) :includes other) (js-call (js-nth index graph) :push other))
|
|
334
|
+
(unless (js-call (js-nth other graph) :includes index) (js-call (js-nth other graph) :push index))))
|
|
335
|
+
(setq i (1+ i)))))
|
|
336
|
+
|
|
337
|
+
(defun live-before (node after graph)
|
|
338
|
+
(let ((kind (get node :kind)) (children (get node :children)) (value (get node :value))
|
|
339
|
+
(live (js-call after :slice)))
|
|
340
|
+
(when (= kind "call")
|
|
341
|
+
(put node :callLive (live-union (get node :callLive (js-array)) after)))
|
|
342
|
+
(cond
|
|
343
|
+
((= kind "local") (interference graph value live)
|
|
344
|
+
(unless (js-call live :includes value) (js-call live :push value)))
|
|
345
|
+
((or (= kind "set") (= kind "initialize"))
|
|
346
|
+
(put node :storeLive (or (get node :storeLive nil) (js-call live :includes value)))
|
|
347
|
+
(interference graph value live)
|
|
348
|
+
(let ((at (js-call live :indexOf value))) (when (>= at 0) (js-call live :splice at 1)))
|
|
349
|
+
(setq live (live-before (js-nth 0 children) live graph)))
|
|
350
|
+
((= kind "if")
|
|
351
|
+
(setq live (live-before (js-nth 0 children)
|
|
352
|
+
(live-union (live-before (js-nth 1 children) live graph)
|
|
353
|
+
(live-before (js-nth 2 children) live graph)) graph)))
|
|
354
|
+
((or (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
|
|
355
|
+
(let ((changed t))
|
|
356
|
+
(while changed
|
|
357
|
+
(let* ((next (live-before (js-nth 0 children)
|
|
358
|
+
(live-union after (live-before (js-nth 1 children) live graph)) graph))
|
|
359
|
+
(joined (live-union live next)))
|
|
360
|
+
(setq changed (/= (js-length joined) (js-length live)))
|
|
361
|
+
(setq live joined)))))
|
|
362
|
+
(t
|
|
363
|
+
(let ((i (1- (js-length children))))
|
|
364
|
+
(while (>= i 0)
|
|
365
|
+
(setq live (live-before (js-nth i children)
|
|
366
|
+
(if (or (= kind "and") (= kind "or")) (live-union after live) live) graph))
|
|
367
|
+
(setq i (1- i))))))
|
|
368
|
+
live))
|
|
369
|
+
|
|
370
|
+
(defun allocate-registers (body locals arity)
|
|
371
|
+
(let ((count (js-length locals)) (counts (js-array)) (registers (js-array))
|
|
372
|
+
(available (js-array)) (integers (js-array)) (graph (js-array)) (i 0))
|
|
373
|
+
(while (< i count) (js-call counts :push 0) (js-call registers :push -1)
|
|
374
|
+
(js-call graph :push (js-array)) (setq i (1+ i)))
|
|
375
|
+
(register-weights body counts)
|
|
376
|
+
(live-before body (js-array) graph)
|
|
377
|
+
;; Entry argument capture materializes all live incoming parameters.
|
|
378
|
+
(setq i 0)
|
|
379
|
+
(while (< i arity) (let ((j 0)) (while (< j arity)
|
|
380
|
+
(when (and (/= i j) (> (js-nth i counts) 0) (> (js-nth j counts) 0))
|
|
381
|
+
(interference graph i (js-array j))) (setq j (1+ j)))) (setq i (1+ i)))
|
|
382
|
+
(setq i 2)
|
|
383
|
+
(while (< i 32)
|
|
384
|
+
;; d0/d1 and d16-d23 are expression temporaries. Actual calls
|
|
385
|
+
;; explicitly preserve allocated caller registers.
|
|
386
|
+
(when (or (< i 16) (>= i 24))
|
|
387
|
+
(js-call available :push i))
|
|
388
|
+
(setq i (1+ i)))
|
|
389
|
+
(setq i 10)
|
|
390
|
+
(while (< i 29)
|
|
391
|
+
(when (or (< i 16) (>= i 19)) (js-call integers :push i))
|
|
392
|
+
(setq i (1+ i)))
|
|
393
|
+
(assign-register-class counts locals registers available "f64" graph)
|
|
394
|
+
(assign-register-class counts locals registers integers "i32" graph)
|
|
395
|
+
(js-object :registers registers :leaf t)))
|
|
396
|
+
|
|
397
|
+
(defun assign-register-class (counts locals registers available type graph)
|
|
398
|
+
(let ((i 0) (count (js-length locals)) (processed (js-array)))
|
|
399
|
+
(while (< i count)
|
|
400
|
+
(let ((best -1) (weight 0) (j 0))
|
|
401
|
+
(while (< j count)
|
|
402
|
+
(when (and (= (get (js-nth j locals) :type) type) (not (js-call processed :includes j))
|
|
403
|
+
(> (js-nth j counts) weight))
|
|
404
|
+
(setq best j) (setq weight (js-nth j counts)))
|
|
405
|
+
(setq j (1+ j)))
|
|
406
|
+
(when (>= best 0)
|
|
407
|
+
(js-call processed :push best)
|
|
408
|
+
(setq j 0)
|
|
409
|
+
(while (and (< j (js-length available)) (= (js-nth best registers) -1))
|
|
410
|
+
(let ((r (if (= type "f64") (js-nth j available) (- -2 (js-nth j available))))
|
|
411
|
+
(neighbors (js-nth best graph)) (k 0) (conflict nil))
|
|
412
|
+
(while (< k (js-length neighbors))
|
|
413
|
+
(when (= (js-nth (js-nth k neighbors) registers) r) (setq conflict t))
|
|
414
|
+
(setq k (1+ k)))
|
|
415
|
+
(unless conflict (js-call registers :splice best 1 r)))
|
|
416
|
+
(setq j (1+ j)))))
|
|
417
|
+
(setq i (1+ i)))
|
|
418
|
+
registers))
|
|
419
|
+
|
|
420
|
+
(defun preserve-registers (state count restore)
|
|
421
|
+
(let ((register 8) (registers (get state :registers)))
|
|
422
|
+
(while (< register 16)
|
|
423
|
+
(when (js-call registers :includes register)
|
|
424
|
+
(integer-register state 9 (* (+ count (- register 8) 1) 8) 0)
|
|
425
|
+
(word state 3406365609)
|
|
426
|
+
(word state (+ (if restore 4248830240 4244635936) register)))
|
|
427
|
+
(setq register (1+ register)))
|
|
428
|
+
(setq register 19)
|
|
429
|
+
(while (< register 29)
|
|
430
|
+
(when (js-call registers :includes (- -2 register))
|
|
431
|
+
(integer-register state 9 (* (+ count 8 (- register 19) 1) 8) 0)
|
|
432
|
+
(word state 3406365609)
|
|
433
|
+
(word state (+ (if restore 4181721376 4177527072) register)))
|
|
434
|
+
(setq register (1+ register)))))
|
|
435
|
+
|
|
436
|
+
(defun local-register (state node type)
|
|
437
|
+
(if (= (get node :kind) "local")
|
|
438
|
+
(let* ((registers (get state :registers)) (index (get node :value))
|
|
439
|
+
(register (if (< index (js-length registers)) (js-nth index registers) -1)))
|
|
440
|
+
(if (= type "f64") register (if (<= register -2) (- (- register) 2) -1)))
|
|
441
|
+
-1))
|
|
442
|
+
|
|
443
|
+
(defun operands (state left right type)
|
|
444
|
+
(if (and (= type "f64") (direct-value state left) (direct-value state right)
|
|
445
|
+
(<= (+ (pure-depth left) (pure-depth right)) (- 7 (get state :depth 0))))
|
|
446
|
+
(let* ((depth (get state :depth 0)) (l (capture-before state (pure-value state left) right))
|
|
447
|
+
(r (pure-value state right)))
|
|
448
|
+
(put state :depth depth) (js-array l r))
|
|
449
|
+
(if (and (get state :compact nil)
|
|
450
|
+
(or (= (get right :kind) "literal") (= (get right :kind) "local") (= (get right :kind) "int-literal")))
|
|
451
|
+
(let ((l (local-register state left type)) (r (local-register state right type)))
|
|
452
|
+
(when (< l 0)
|
|
453
|
+
(expression state left)
|
|
454
|
+
(if (< r 0)
|
|
455
|
+
(progn (word state (if (= type "f64") 509624321 704644065)) (setq l 1))
|
|
456
|
+
(setq l 0)))
|
|
457
|
+
(when (< r 0) (expression state right) (setq r 0))
|
|
458
|
+
(js-array l r))
|
|
459
|
+
(progn (expression state left) (push-value state type) (expression state right) (pop-value state type)
|
|
460
|
+
(js-array 1 0)))))
|
|
461
|
+
|
|
462
|
+
(defun binary-word (state opcode registers)
|
|
463
|
+
(word state (+ (- opcode 32) (* (js-nth 0 registers) 32) (* (js-nth 1 registers) 65536))))
|
|
464
|
+
|
|
465
|
+
(defun pure-depth (node)
|
|
466
|
+
(let ((depth 0) (i 0) (children (get node :children)))
|
|
467
|
+
;; Sequence prefixes execute and release their scratch registers before
|
|
468
|
+
;; the final value. Their tree nesting does not increase value pressure.
|
|
469
|
+
(when (and (= (get node :kind) "sequence") (> (js-length children) 0))
|
|
470
|
+
(setq i (1- (js-length children))))
|
|
471
|
+
(while (< i (js-length children))
|
|
472
|
+
(let ((child (pure-depth (js-nth i children)))) (when (> child depth) (setq depth child)))
|
|
473
|
+
(setq i (1+ i)))
|
|
474
|
+
(if (= (get node :kind) "sequence") depth (1+ depth))))
|
|
475
|
+
|
|
476
|
+
(defun direct-value-tree (node)
|
|
477
|
+
;; Direct value emission permits ordered effects, but calls and loops
|
|
478
|
+
;; still use the full fallback and its call/loop lifetime machinery.
|
|
479
|
+
(let ((kind (get node :kind)) (children (get node :children)) (i 0)
|
|
480
|
+
(valid t))
|
|
481
|
+
(when (or (= kind "call") (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
|
|
482
|
+
(setq valid nil))
|
|
483
|
+
(while (and valid (< i (js-length children)))
|
|
484
|
+
(setq valid (direct-value-tree (js-nth i children))) (setq i (1+ i)))
|
|
485
|
+
valid))
|
|
486
|
+
|
|
487
|
+
(defun direct-value (state node)
|
|
488
|
+
(and (get state :compact nil) (= (get node :type) "f64")
|
|
489
|
+
(direct-value-tree node)
|
|
490
|
+
(<= (pure-depth node) (- 7 (get state :depth 0)))))
|
|
491
|
+
|
|
492
|
+
(defun retarget-last (state register)
|
|
493
|
+
(let* ((words (get state :words)) (at (1- (js-length words))))
|
|
494
|
+
(native-word-set words at (bit-or (bit-and (native-word-at words at) 4294967264) register))))
|
|
495
|
+
|
|
496
|
+
(defun writes-physical (state node register)
|
|
497
|
+
(let ((kind (get node :kind)) (children (get node :children)) (i 0) (found nil))
|
|
498
|
+
(when (or (= kind "set") (= kind "initialize"))
|
|
499
|
+
(setq found (= register (js-nth (get node :value) (get state :registers)))))
|
|
500
|
+
(while (and (not found) (< i (js-length children)))
|
|
501
|
+
(setq found (writes-physical state (js-nth i children) register)) (setq i (1+ i)))
|
|
502
|
+
found))
|
|
503
|
+
|
|
504
|
+
(defun capture-before (state register right)
|
|
505
|
+
(if (writes-physical state right register)
|
|
506
|
+
(let* ((depth (get state :depth 0)) (temp (+ 16 depth)))
|
|
507
|
+
(word state (+ 509624320 (* register 32) temp))
|
|
508
|
+
(put state :depth (1+ depth)) temp)
|
|
509
|
+
register))
|
|
510
|
+
|
|
511
|
+
(defun store-float-value (state index register)
|
|
512
|
+
(let ((target (js-nth index (get state :registers))))
|
|
513
|
+
(if (>= target 0)
|
|
514
|
+
(progn (unless (= target register) (word state (+ 509624320 (* register 32) target))) target)
|
|
515
|
+
(progn
|
|
516
|
+
(unless (= register 0) (word state (+ 509624320 (* register 32))))
|
|
517
|
+
(local-access state index "f64" t) register))))
|
|
518
|
+
|
|
519
|
+
(defun pure-value (state node)
|
|
520
|
+
(let ((kind (get node :kind)) (children (get node :children)) (value (get node :value))
|
|
521
|
+
(depth (get state :depth 0)) (local (local-register state node "f64")))
|
|
522
|
+
(if (>= local 0) local
|
|
523
|
+
(let ((register (+ 16 depth)))
|
|
524
|
+
(cond
|
|
525
|
+
((= kind "literal")
|
|
526
|
+
(let ((constant (literal-register state value)))
|
|
527
|
+
(if (>= constant 0) (setq register constant)
|
|
528
|
+
(progn (float-literal state value) (retarget-last state register)))))
|
|
529
|
+
((or (= kind "set") (= kind "initialize"))
|
|
530
|
+
(let* ((rhs (js-nth 0 children)) (r (pure-value state rhs))
|
|
531
|
+
(target (js-nth value (get state :registers))))
|
|
532
|
+
(if (and (>= target 0) (= r register) (= (bit-and
|
|
533
|
+
(native-word-at (get state :words) (1- (js-length (get state :words)))) 31) r)
|
|
534
|
+
(or (= (get rhs :kind) "arithmetic") (= (get rhs :kind) "literal")))
|
|
535
|
+
(progn (retarget-last state target) (setq register target))
|
|
536
|
+
(setq register (store-float-value state value r)))))
|
|
537
|
+
((= kind "sequence")
|
|
538
|
+
(let ((i 0))
|
|
539
|
+
(while (< i (1- (js-length children)))
|
|
540
|
+
(discarded state (js-nth i children)) (setq i (1+ i)))
|
|
541
|
+
(setq register (pure-value state (js-nth i children)))))
|
|
542
|
+
((= kind "int-number")
|
|
543
|
+
(let ((source (local-register state (js-nth 0 children) "i32")))
|
|
544
|
+
(when (< source 0) (expression state (js-nth 0 children)) (setq source 0))
|
|
545
|
+
(word state (+ 509739008 (* source 32) register))))
|
|
546
|
+
((= kind "arithmetic")
|
|
547
|
+
(let ((length (js-length children)))
|
|
548
|
+
(cond
|
|
549
|
+
((= length 0)
|
|
550
|
+
(float-literal state (if (= value "*") 1 0)) (retarget-last state register))
|
|
551
|
+
((and (= length 1) (or (= value "+") (= value "*")))
|
|
552
|
+
(setq register (pure-value state (js-nth 0 children))))
|
|
553
|
+
((and (= length 1) (= value "-"))
|
|
554
|
+
(word state (+ 509689856 (* (pure-value state (js-nth 0 children)) 32) register)))
|
|
555
|
+
(t
|
|
556
|
+
(let* ((left (if (and (= length 1) (= value "/"))
|
|
557
|
+
(js-object :kind "literal" :type "f64" :value 1) (js-nth 0 children)))
|
|
558
|
+
(right (if (= length 2) (js-nth 1 children)
|
|
559
|
+
(if (= value "/") (js-nth 0 children)
|
|
560
|
+
(js-object :kind "literal" :type "f64" :value 1))))
|
|
561
|
+
(l (capture-before state (pure-value state left) right)) (r (pure-value state right)))
|
|
562
|
+
(binary-word state (cond ((or (= value "+") (= value "1+")) 509618208)
|
|
563
|
+
((or (= value "-") (= value "1-")) 509622304)
|
|
564
|
+
((= value "*") 509610016) (t 509614112)) (js-array l r))
|
|
565
|
+
(retarget-last state register))))))
|
|
566
|
+
(t (expression state node) (word state (+ 509624320 register))))
|
|
567
|
+
(put state :depth (1+ depth))
|
|
568
|
+
register))))
|
|
569
|
+
|
|
570
|
+
(defun discarded-pure (node)
|
|
571
|
+
;; Discarded numeric branches include a sequence ending in an i32 zero.
|
|
572
|
+
;; That wrapper is effect-free when all its children are effect-free;
|
|
573
|
+
;; numeric-pure deliberately excludes sequences for its other callers.
|
|
574
|
+
(if (or (= (get node :kind) "sequence") (= (get node :kind) "if"))
|
|
575
|
+
(let ((children (get node :children)) (i 0) (pure t))
|
|
576
|
+
(while (and pure (< i (js-length children)))
|
|
577
|
+
(setq pure (discarded-pure (js-nth i children)))
|
|
578
|
+
(setq i (1+ i)))
|
|
579
|
+
pure)
|
|
580
|
+
(numeric-pure node)))
|
|
581
|
+
|
|
582
|
+
(defun discarded (state node)
|
|
583
|
+
(let ((kind (get node :kind)) (children (get node :children)) (i 0))
|
|
584
|
+
(cond
|
|
585
|
+
((or (= kind "literal") (= kind "int-literal") (= kind "local")))
|
|
586
|
+
((and (get state :compact nil) (or (= kind "set") (= kind "initialize"))
|
|
587
|
+
(not (get node :storeLive nil)))
|
|
588
|
+
(discarded state (js-nth 0 children)))
|
|
589
|
+
((or (= kind "sequence") (= kind "arithmetic") (= kind "compare") (= kind "not")
|
|
590
|
+
(= kind "int-number") (= kind "ceil-int") (= kind "int-add") (= kind "int-sub")
|
|
591
|
+
(= kind "int-and") (= kind "int-compare"))
|
|
592
|
+
(while (< i (js-length children)) (discarded state (js-nth i children)) (setq i (1+ i))))
|
|
593
|
+
((= kind "if")
|
|
594
|
+
(if (and (discarded-pure (js-nth 1 children)) (discarded-pure (js-nth 2 children)))
|
|
595
|
+
(discarded state (js-nth 0 children))
|
|
596
|
+
(let ((otherwise (condition-jumps state (js-nth 0 children) nil)))
|
|
597
|
+
(discarded state (js-nth 1 children))
|
|
598
|
+
(let ((end (jump state "jump")))
|
|
599
|
+
(patch-conditions state otherwise (js-length (get state :words)))
|
|
600
|
+
(discarded state (js-nth 2 children))
|
|
601
|
+
(patch-jump state end (js-length (get state :words)))))))
|
|
602
|
+
((or (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
|
|
603
|
+
(emit-loop state children nil))
|
|
604
|
+
((and (or (= kind "set") (= kind "initialize")) (= (get node :type) "i32")
|
|
605
|
+
(get state :compact nil)
|
|
606
|
+
(<= (js-nth (get node :value) (get state :registers)) -2)
|
|
607
|
+
(or (= (get (js-nth 0 children) :kind) "int-add")
|
|
608
|
+
(= (get (js-nth 0 children) :kind) "int-sub")
|
|
609
|
+
(= (get (js-nth 0 children) :kind) "int-and")))
|
|
610
|
+
(integer-operation state (js-nth 0 children)
|
|
611
|
+
(- (- (js-nth (get node :value) (get state :registers))) 2)))
|
|
612
|
+
((and (or (= kind "set") (= kind "initialize")) (direct-value state (js-nth 0 children)))
|
|
613
|
+
(let* ((depth (get state :depth 0)) (rhs (js-nth 0 children))
|
|
614
|
+
(r (pure-value state rhs)) (registers (get state :registers))
|
|
615
|
+
(target (js-nth (get node :value) registers)))
|
|
616
|
+
(if (and (>= target 0) (= r (+ 16 depth))
|
|
617
|
+
(= (bit-and (native-word-at (get state :words) (1- (js-length (get state :words)))) 31) r)
|
|
618
|
+
(or (= (get rhs :kind) "arithmetic") (= (get rhs :kind) "literal")))
|
|
619
|
+
(retarget-last state target)
|
|
620
|
+
(store-float-value state (get node :value) r))
|
|
621
|
+
(put state :depth depth)))
|
|
622
|
+
(t (expression state node)))))
|
|
623
|
+
|
|
624
|
+
(defun stack-float (state offset register store)
|
|
625
|
+
(word state 2432697321) ;; mov x9, sp
|
|
626
|
+
(while (> offset 0)
|
|
627
|
+
(let ((part (if (< offset 4095) offset 4095)))
|
|
628
|
+
(word state (+ 2432696617 (* part 1024)))
|
|
629
|
+
(setq offset (- offset part))))
|
|
630
|
+
(word state (+ (if store 4244635936 4248830240) register)))
|
|
631
|
+
|
|
632
|
+
(defun jump (state kind)
|
|
633
|
+
(let ((at (js-length (get state :words))))
|
|
634
|
+
(word state (cond ((= kind "zero") 872415232)
|
|
635
|
+
((= kind "nonzero") 889192448) (t 335544320)))
|
|
636
|
+
at))
|
|
637
|
+
|
|
638
|
+
(defun branch-kind (opcode)
|
|
639
|
+
(cond
|
|
640
|
+
((= (bit-and opcode 2080374784) 335544320) 0) ;; B / BL: imm26
|
|
641
|
+
((= (bit-and opcode 4278190096) 1409286144) 1) ;; B.cond: imm19
|
|
642
|
+
((= (bit-and opcode 2113929216) 872415232) 2) ;; CBZ / CBNZ: imm19
|
|
643
|
+
((= (bit-and opcode 2113929216) 905969664) 3) ;; TBZ / TBNZ: imm14
|
|
644
|
+
(t (backend-fail nil "unsupported native branch instruction"))))
|
|
645
|
+
|
|
646
|
+
(defun branch-limit (kind)
|
|
647
|
+
(if (= kind 0) 33554432 (if (= kind 3) 8192 262144)))
|
|
648
|
+
|
|
649
|
+
(defun branch-fits (distance limit)
|
|
650
|
+
(and (>= distance (- limit)) (< distance limit)))
|
|
651
|
+
|
|
652
|
+
(defun branch-word (opcode distance)
|
|
653
|
+
(let* ((kind (branch-kind opcode)) (limit (branch-limit kind)))
|
|
654
|
+
(unless (branch-fits distance limit)
|
|
655
|
+
(backend-fail nil "native branch exceeds ARM64 displacement range"))
|
|
656
|
+
(if (= kind 0) (bit-or (bit-and opcode 4227858432) (bit-and distance 67108863))
|
|
657
|
+
(bit-or (bit-and opcode (if (= kind 3) 4294443039 4278190111))
|
|
658
|
+
(* (bit-and distance (if (= kind 3) 16383 524287)) 32)))))
|
|
659
|
+
|
|
660
|
+
(defun patch-jump-record (state at target record descriptor)
|
|
661
|
+
(let* ((words (get state :words)) (opcode (native-word-at words at))
|
|
662
|
+
(branches (or (get state :branches nil)
|
|
663
|
+
(let ((created (js-array))) (put state :branches created) created)))
|
|
664
|
+
(kind (branch-kind opcode)) (distance (- target at)))
|
|
665
|
+
;; Keep source positions stable until every local label and call is known.
|
|
666
|
+
;; A direct call's private pending record becomes its final relocation.
|
|
667
|
+
;; Do not retain a second object for the same instruction until linking.
|
|
668
|
+
(let ((entry (or record (js-object :at at :target target :opcode opcode :kind kind :size 1))))
|
|
669
|
+
(when record
|
|
670
|
+
(put entry :at at) (put entry :target target)
|
|
671
|
+
;; The pending shape stays two fields during code generation. Define
|
|
672
|
+
;; the remaining own data fields only now, without inherited setters.
|
|
673
|
+
;; One null-prototype descriptor is reused for this whole transfer.
|
|
674
|
+
(put descriptor :value opcode) (Object/defineProperty entry "opcode" descriptor)
|
|
675
|
+
(put descriptor :value kind) (Object/defineProperty entry "kind" descriptor)
|
|
676
|
+
(put descriptor :value 1) (Object/defineProperty entry "size" descriptor))
|
|
677
|
+
(js-call branches :push entry))
|
|
678
|
+
(when (branch-fits distance (branch-limit kind))
|
|
679
|
+
(native-word-set words at (branch-word opcode distance)))))
|
|
680
|
+
|
|
681
|
+
(defun patch-jump (state at target)
|
|
682
|
+
(patch-jump-record state at target nil nil))
|
|
683
|
+
|
|
684
|
+
(defun finish-calls (state offsets)
|
|
685
|
+
(let* ((calls (get state :calls)) (count (js-length calls)) (index 0)
|
|
686
|
+
(descriptor (Object/create nil)))
|
|
687
|
+
(put descriptor :value 0) (put descriptor :writable t)
|
|
688
|
+
(put descriptor :enumerable t) (put descriptor :configurable t)
|
|
689
|
+
(while (< index count)
|
|
690
|
+
(let* ((call (js-nth index calls)) (at (get call :at))
|
|
691
|
+
(target (/ (js-nth (get call :target) offsets) 4)))
|
|
692
|
+
;; Pending :target is the function index; after validation it becomes
|
|
693
|
+
;; the word offset on this same emitter-owned record. Clear only a
|
|
694
|
+
;; successfully transferred slot, preserving the first patch error.
|
|
695
|
+
(patch-jump-record state at target call descriptor)
|
|
696
|
+
(put calls index nil))
|
|
697
|
+
(setq index (1+ index)))
|
|
698
|
+
(put calls :length 0) count))
|
|
699
|
+
|
|
700
|
+
(defun branch-position (branches position)
|
|
701
|
+
(let ((low 0) (high (js-length branches)))
|
|
702
|
+
(while (< low high)
|
|
703
|
+
(let ((middle (js-call globalThis.Math :floor (/ (+ low high) 2))))
|
|
704
|
+
(if (< (get (js-nth middle branches) :at) position) (setq low (1+ middle)) (setq high middle))))
|
|
705
|
+
(if (= low 0) position
|
|
706
|
+
(let ((entry (js-nth (1- low) branches)))
|
|
707
|
+
(+ position (get entry :before) (1- (get entry :size)))))))
|
|
708
|
+
|
|
709
|
+
(defun long-jump (state distance link)
|
|
710
|
+
;; Position-independent ADR + signed 64-bit delta; x16/x17 are ABI scratch.
|
|
711
|
+
;; BLR returns after the whole sequence, preserving the ordinary call ABI.
|
|
712
|
+
(word state 268435472) ;; adr x16, .
|
|
713
|
+
(integer-register state 17 (uint32 (* distance 4))
|
|
714
|
+
(uint32 (js-call globalThis.Math :floor (/ (* distance 4) 4294967296))))
|
|
715
|
+
(word state 2333147664) ;; add x16,x16,x17
|
|
716
|
+
(word state (if link 3594453504 3592356352))) ;; blr / br x16
|
|
717
|
+
|
|
718
|
+
(defun relaxed-branch (state entry at target)
|
|
719
|
+
(let ((opcode (get entry :opcode)) (kind (get entry :kind)) (size (get entry :size)))
|
|
720
|
+
(if (branch-fits (- target at) (branch-limit kind))
|
|
721
|
+
(word state (branch-word opcode (- target at)))
|
|
722
|
+
(if (or (= kind 0) (and (= kind 1) (>= (bit-and opcode 15) 14)))
|
|
723
|
+
(if (= size 1) (word state (branch-word 335544320 (- target at)))
|
|
724
|
+
(long-jump state (- target at) (/= (bit-and opcode 2147483648) 0)))
|
|
725
|
+
(progn
|
|
726
|
+
(word state (branch-word (bit-xor opcode (if (= kind 1) 1 16777216)) size))
|
|
727
|
+
(if (= size 2) (word state (branch-word 335544320 (- target at 1)))
|
|
728
|
+
(long-jump state (- target at 1) nil)))))))
|
|
729
|
+
|
|
730
|
+
(defun finish-branches (state offsets)
|
|
731
|
+
(let ((branches (get state :branches (js-array))) (changed t))
|
|
732
|
+
(js-call branches :sort (lambda (left right) (- (get left :at) (get right :at))))
|
|
733
|
+
;; Sizes only grow. Revisit all labels after each growth, including branches
|
|
734
|
+
;; which were in range before another expansion shifted their destination.
|
|
735
|
+
(while changed
|
|
736
|
+
(setq changed false)
|
|
737
|
+
(let ((growth 0))
|
|
738
|
+
(js-call branches :forEach (lambda (entry)
|
|
739
|
+
(put entry :before growth) (setq growth (+ growth (1- (get entry :size)))))))
|
|
740
|
+
(js-call branches :forEach (lambda (entry)
|
|
741
|
+
(let* ((at (branch-position branches (get entry :at)))
|
|
742
|
+
(target (branch-position branches (get entry :target)))
|
|
743
|
+
(kind (get entry :kind)) (opcode (get entry :opcode))
|
|
744
|
+
(always (and (= kind 1) (>= (bit-and opcode 15) 14)))
|
|
745
|
+
(size (if (branch-fits (- target at) (branch-limit kind)) 1
|
|
746
|
+
(if (or (= kind 0) always)
|
|
747
|
+
(if (branch-fits (- target at) 33554432) 1 7)
|
|
748
|
+
(if (branch-fits (- target at 1) 33554432) 2 8)))))
|
|
749
|
+
(when (> size (get entry :size)) (put entry :size size) (setq changed t))))))
|
|
750
|
+
(let* ((words (get state :words)) (original-size (js-length words))
|
|
751
|
+
(count (js-length branches))
|
|
752
|
+
(last (if (> count 0) (js-nth (1- count) branches) nil))
|
|
753
|
+
(final-size (+ original-size (if last (+ (get last :before) (1- (get last :size))) 0)))
|
|
754
|
+
(scratch (js-array)) (out (js-object :words scratch :compact nil))
|
|
755
|
+
(index (1- original-size)) (next (1- count)) (write (1- final-size)))
|
|
756
|
+
;; Sizes have converged and only grow. Every destination is at or above
|
|
757
|
+
;; its old position, so reverse traversal preserves the unread prefix.
|
|
758
|
+
;; Reuse at most eight words for one relaxed branch, never a code copy.
|
|
759
|
+
(native-word-resize words final-size)
|
|
760
|
+
(while (>= index 0)
|
|
761
|
+
(let ((entry (if (>= next 0) (js-nth next branches) nil)))
|
|
762
|
+
(if (and entry (= (get entry :at) index))
|
|
763
|
+
(let ((at (- write (1- (get entry :size))))
|
|
764
|
+
(target (branch-position branches (get entry :target))) (j 0))
|
|
765
|
+
(put scratch :length 0)
|
|
766
|
+
(relaxed-branch out entry at target)
|
|
767
|
+
(setq j (1- (js-length scratch)))
|
|
768
|
+
(while (>= j 0)
|
|
769
|
+
(let ((value (js-nth j scratch)))
|
|
770
|
+
;; patch-jump already wrote every fitting branch. When no
|
|
771
|
+
;; earlier relaxation moved it, do not repack that same
|
|
772
|
+
;; immutable word block merely to write the same opcode.
|
|
773
|
+
(unless (and (= write index) (= (native-word-at words index) value))
|
|
774
|
+
(native-word-set words write value)))
|
|
775
|
+
(setq write (1- write)) (setq j (1- j)))
|
|
776
|
+
(setq next (1- next)))
|
|
777
|
+
(let ((value (native-word-at words index)))
|
|
778
|
+
;; Still read every source word in the original reverse order,
|
|
779
|
+
;; including hole validation. Only moved words need a write.
|
|
780
|
+
(unless (= write index) (native-word-set words write value))
|
|
781
|
+
(setq write (1- write)))))
|
|
782
|
+
(setq index (1- index)))
|
|
783
|
+
(setq index 0)
|
|
784
|
+
(while (< index (js-length offsets))
|
|
785
|
+
(put offsets index (* (branch-position branches (/ (js-nth index offsets) 4)) 4))
|
|
786
|
+
(setq index (1+ index)))
|
|
787
|
+
(js-call (get state :constants (js-array)) :forEach (lambda (entry)
|
|
788
|
+
(let ((uses (get entry :uses)) (i 0))
|
|
789
|
+
(while (< i (js-length uses)) (put uses i (branch-position branches (js-nth i uses))) (setq i (1+ i))))))
|
|
790
|
+
(let ((uses (get state :tableUses (js-array))) (i 0))
|
|
791
|
+
(while (< i (js-length uses)) (put uses i (branch-position branches (js-nth i uses))) (setq i (1+ i))))
|
|
792
|
+
(let ((ranges (get state :debugRanges nil)) (i 0))
|
|
793
|
+
(when ranges
|
|
794
|
+
(while (< i (js-length ranges))
|
|
795
|
+
(let ((range (js-nth i ranges)))
|
|
796
|
+
(put range :start (* (branch-position branches (/ (get range :start) 4)) 4))
|
|
797
|
+
(put range :end (* (branch-position branches (/ (get range :end) 4)) 4)))
|
|
798
|
+
(setq i (1+ i)))))
|
|
799
|
+
(put state :branches (js-array)))))
|
|
800
|
+
|
|
801
|
+
(defun condition (state node)
|
|
802
|
+
(expression state node)
|
|
803
|
+
(when (or (= (get node :type) "f64") (= (get node :type) "u32"))
|
|
804
|
+
(if (get state :memory nil) (memory-integer-register state 0 1) (integer-register state 0 1 0))))
|
|
805
|
+
|
|
806
|
+
(defun comparison-code (operator integer)
|
|
807
|
+
;; Ordered f64 '<' uses MI: signed LT would accept unordered NaN.
|
|
808
|
+
(cond ((= operator "=") 0) ((= operator "/=") 1) ((= operator "<") (if integer 11 4))
|
|
809
|
+
((= operator ">") 12) ((= operator "<=") (if integer 13 9)) (t 10)))
|
|
810
|
+
|
|
811
|
+
(defun compare-result (state operator integer)
|
|
812
|
+
(word state (+ 446629856 (* (bit-xor (comparison-code operator integer) 1) 4096))))
|
|
813
|
+
|
|
814
|
+
(defun patch-conditions (state jumps target)
|
|
815
|
+
(let ((i 0)) (while (< i (js-length jumps))
|
|
816
|
+
(patch-jump state (js-nth i jumps) target) (setq i (1+ i)))))
|
|
817
|
+
|
|
818
|
+
(defun condition-jumps (state node when-true)
|
|
819
|
+
(let ((kind (get node :kind)) (children (get node :children)) (jumps (js-array)))
|
|
820
|
+
(cond
|
|
821
|
+
((and (or (= kind "and") (= kind "or")) (= (get node :type) "i32"))
|
|
822
|
+
(let ((skip (js-array)) (i 0)
|
|
823
|
+
(every (if (= kind "and") (not when-true) when-true)))
|
|
824
|
+
(while (< i (js-length children))
|
|
825
|
+
(let ((last (= i (1- (js-length children)))))
|
|
826
|
+
(let ((output (if (or every last) jumps skip))
|
|
827
|
+
(positions (condition-jumps state (js-nth i children)
|
|
828
|
+
(if (or every last) when-true (not when-true)))) (j 0))
|
|
829
|
+
;; These are full word positions, not container bytes.
|
|
830
|
+
(while (< j (js-length positions))
|
|
831
|
+
(js-call output :push (js-nth j positions))
|
|
832
|
+
(setq j (1+ j)))))
|
|
833
|
+
(setq i (1+ i)))
|
|
834
|
+
(when (= (js-length children) 0)
|
|
835
|
+
(when (= when-true (= kind "and")) (js-call jumps :push (jump state "jump"))))
|
|
836
|
+
(patch-conditions state skip (js-length (get state :words)))))
|
|
837
|
+
((= kind "not") (setq jumps (condition-jumps state (js-nth 0 children) (not when-true))))
|
|
838
|
+
((and (get state :compact nil) (= kind "int-compare")
|
|
839
|
+
(or (>= (integer-immediate (js-nth 0 children)) 0)
|
|
840
|
+
(>= (integer-immediate (js-nth 1 children)) 0)))
|
|
841
|
+
(let* ((reverse (< (integer-immediate (js-nth 1 children)) 0))
|
|
842
|
+
(operand (js-nth (if reverse 1 0) children))
|
|
843
|
+
(immediate (integer-immediate (js-nth (if reverse 0 1) children)))
|
|
844
|
+
(r (local-register state operand "i32")) (op (get node :value)))
|
|
845
|
+
(when (< r 0) (expression state operand) (setq r 0))
|
|
846
|
+
(word state (+ 1895825439 (* immediate 1024) (* r 32)))
|
|
847
|
+
(when reverse (setq op (cond ((= op "<") ">") ((= op ">") "<")
|
|
848
|
+
((= op "<=") ">=") ((= op ">=") "<=") (t op))))
|
|
849
|
+
(let ((code (comparison-code op t)))
|
|
850
|
+
(js-call jumps :push (js-length (get state :words)))
|
|
851
|
+
(word state (+ 1409286144 (if when-true code (bit-xor code 1)))))))
|
|
852
|
+
((or (= kind "compare") (= kind "int-compare"))
|
|
853
|
+
(let* ((integer (= kind "int-compare"))
|
|
854
|
+
(registers (operands state (js-nth 0 children) (js-nth 1 children) (if integer "i32" "f64")))
|
|
855
|
+
(code (comparison-code (get node :value) integer)))
|
|
856
|
+
(binary-word state (if integer 1795162175 509616160) registers)
|
|
857
|
+
(js-call jumps :push (js-length (get state :words)))
|
|
858
|
+
(word state (+ 1409286144 (if when-true code (bit-xor code 1))))))
|
|
859
|
+
(t (condition state node) (js-call jumps :push (jump state (if when-true "nonzero" "zero")))))
|
|
860
|
+
jumps))
|
|
861
|
+
|
|
862
|
+
(defun emit-call (state node)
|
|
863
|
+
(let* ((args (get node :children)) (count (js-length args))
|
|
864
|
+
(extra (* (js-call globalThis.Math :ceil (/ (if (> count 8) (- count 8) 0) 2)) 16)) (i 0)
|
|
865
|
+
(depth (get state :depth 0)) (leaf (get state :leaf nil)) (saved (js-array)))
|
|
866
|
+
;; Arguments execute first: restoring a pre-argument snapshot would undo
|
|
867
|
+
;; their assignments. Only actual calls pay caller-register preservation;
|
|
868
|
+
;; conservative fallback calls must not spill an entire optimized loop.
|
|
869
|
+
(put state :leaf nil)
|
|
870
|
+
(while (< i count) (expression state (js-nth i args)) (push-value state "f64") (setq i (1+ i)))
|
|
871
|
+
(when (get state :compact nil)
|
|
872
|
+
(let ((registers (get state :registers)) (live (get node :callLive (js-array))) (j 0))
|
|
873
|
+
(while (< j (js-length live))
|
|
874
|
+
(let ((r (js-nth (js-nth j live) registers)))
|
|
875
|
+
(when (or (and (>= r 2) (or (< r 8) (>= r 24))) (and (<= r -12) (>= r -17)))
|
|
876
|
+
(unless (js-call saved :includes r) (js-call saved :push r))))
|
|
877
|
+
(setq j (1+ j)))
|
|
878
|
+
(setq j 0)
|
|
879
|
+
(let ((constants (get state :literalRegisters (js-array))))
|
|
880
|
+
(while (< j (js-length constants))
|
|
881
|
+
(let ((r (get (js-nth j constants) :register)))
|
|
882
|
+
(when (and (or (< r 8) (>= r 24)) (not (js-call saved :includes r)))
|
|
883
|
+
(js-call saved :push r)))
|
|
884
|
+
(setq j (1+ j))))
|
|
885
|
+
(setq j 0)
|
|
886
|
+
(while (< j (if (< depth 8) depth 8)) (js-call saved :push (+ 16 j)) (setq j (1+ j))))
|
|
887
|
+
(setq i 0)
|
|
888
|
+
(while (< i (js-length saved))
|
|
889
|
+
(let ((r (js-nth i saved)))
|
|
890
|
+
(stack-adjust state 16 t)
|
|
891
|
+
(word state (+ (if (>= r 0) 4244636640 4177527776) (if (>= r 0) r (- (- r) 2)))))
|
|
892
|
+
(setq i (1+ i))))
|
|
893
|
+
(stack-adjust state extra t)
|
|
894
|
+
(setq i 0)
|
|
895
|
+
(while (< i count)
|
|
896
|
+
(stack-float state (+ extra (* (js-length saved) 16) (* (- count i 1) 16)) (if (< i 8) i 16) nil)
|
|
897
|
+
(when (>= i 8) (stack-float state (* (- i 8) 8) 16 t))
|
|
898
|
+
(setq i (1+ i)))
|
|
899
|
+
(let ((at (js-length (get state :words))))
|
|
900
|
+
(word state 2483027968)
|
|
901
|
+
(js-call (get state :calls) :push (js-object :at at :target (get node :value))))
|
|
902
|
+
(stack-adjust state extra nil)
|
|
903
|
+
(setq i (1- (js-length saved)))
|
|
904
|
+
(while (>= i 0)
|
|
905
|
+
(let ((r (js-nth i saved)))
|
|
906
|
+
(word state (+ (if (>= r 0) 4248830944 4181722080) (if (>= r 0) r (- (- r) 2))))
|
|
907
|
+
(stack-adjust state 16 nil))
|
|
908
|
+
(setq i (1- i)))
|
|
909
|
+
(stack-adjust state (* count 16) nil)
|
|
910
|
+
(put state :leaf leaf) (put state :depth depth)))
|
|
911
|
+
|
|
912
|
+
(defun emit-loop (state children used)
|
|
913
|
+
(if (get state :compact nil)
|
|
914
|
+
(let* ((initial (jump state "jump")) (start (js-length (get state :words))))
|
|
915
|
+
(discarded state (js-nth 1 children))
|
|
916
|
+
(patch-jump state initial (js-length (get state :words)))
|
|
917
|
+
(patch-conditions state (condition-jumps state (js-nth 0 children) t) start)
|
|
918
|
+
(when used (integer-register state 0 0 0)))
|
|
919
|
+
(let ((start (js-length (get state :words))))
|
|
920
|
+
(condition state (js-nth 0 children))
|
|
921
|
+
(let ((end (jump state "zero")))
|
|
922
|
+
(if (get state :memory nil) (memory-discarded state (js-nth 1 children))
|
|
923
|
+
(expression state (js-nth 1 children)))
|
|
924
|
+
(let ((back (jump state "jump"))) (patch-jump state back start))
|
|
925
|
+
(patch-jump state end (js-length (get state :words)))
|
|
926
|
+
(if (get state :memory nil)
|
|
927
|
+
(when used (memory-integer-register state 0 0))
|
|
928
|
+
(integer-register state 0 0 0))))))
|
|
929
|
+
|
|
930
|
+
(defun integer-immediate (node)
|
|
931
|
+
(if (and (or (= (get node :kind) "int-literal")
|
|
932
|
+
(and (= (get node :kind) "literal") (= (get node :type) "i32")))
|
|
933
|
+
(>= (get node :value) 0) (< (get node :value) 4096)) (get node :value) -1))
|
|
934
|
+
|
|
935
|
+
(defun integer-mask-width (node)
|
|
936
|
+
(let ((value (if (or (= (get node :kind) "int-literal")
|
|
937
|
+
(and (= (get node :kind) "literal") (= (get node :type) "i32")))
|
|
938
|
+
(get node :value) 0)) (bits 0) (mask 0))
|
|
939
|
+
(while (and (< bits 31) (< mask value)) (setq mask (+ (* mask 2) 1)) (setq bits (1+ bits)))
|
|
940
|
+
(if (and (> bits 0) (= mask value)) bits 0)))
|
|
941
|
+
|
|
942
|
+
(defun integer-operation (state node destination)
|
|
943
|
+
(let* ((kind (get node :kind)) (children (get node :children))
|
|
944
|
+
(left (js-nth 0 children)) (right (js-nth 1 children))
|
|
945
|
+
(immediate (integer-immediate right)) (mask (if (= kind "int-and") (integer-mask-width right) 0)))
|
|
946
|
+
(if (and (get state :compact nil) (or (> mask 0)
|
|
947
|
+
(and (or (= kind "int-add") (= kind "int-sub")) (>= immediate 0))))
|
|
948
|
+
(let ((register (local-register state left "i32")))
|
|
949
|
+
(when (< register 0)
|
|
950
|
+
(if (or (= (get left :kind) "int-add") (= (get left :kind) "int-sub") (= (get left :kind) "int-and"))
|
|
951
|
+
(progn (integer-operation state left destination) (setq register destination))
|
|
952
|
+
(progn (expression state left) (setq register 0))))
|
|
953
|
+
(word state (+ (cond ((> mask 0) 301989888) ((= kind "int-add") 285212672) (t 1358954496))
|
|
954
|
+
(* (if (> mask 0) (1- mask) immediate) 1024) (* register 32) destination)))
|
|
955
|
+
(let ((registers (operands state left right "i32")))
|
|
956
|
+
(binary-word state (cond ((= kind "int-add") 184549408) ((= kind "int-sub") 1258291232)
|
|
957
|
+
((= kind "int-and") 167772192) (t 1795162175)) registers)
|
|
958
|
+
(unless (or (= kind "int-compare") (= destination 0)) (retarget-last state destination))))))
|
|
959
|
+
|
|
960
|
+
(defun expression (state node &optional unused)
|
|
961
|
+
(let ((ranges (get state :debugRanges nil)))
|
|
962
|
+
(if ranges
|
|
963
|
+
(let* ((depth (get state :debugDepth 0))
|
|
964
|
+
(range (js-object :kind (get node :kind) :function (get state :debugFunction)
|
|
965
|
+
:functionIndex (get state :debugFunctionIndex) :depth depth
|
|
966
|
+
:start (* 4 (js-length (get state :words))) :end 0
|
|
967
|
+
:span (get node :span nil))))
|
|
968
|
+
(js-call ranges :push range)
|
|
969
|
+
(put state :debugDepth (1+ depth))
|
|
970
|
+
(let ((result (if (and unused (get state :memory nil))
|
|
971
|
+
(memory-discarded-body state node) (expression-body state node))))
|
|
972
|
+
(put state :debugDepth depth)
|
|
973
|
+
(put range :end (* 4 (js-length (get state :words)))) result))
|
|
974
|
+
(if (and unused (get state :memory nil))
|
|
975
|
+
(memory-discarded-body state node) (expression-body state node)))))
|
|
976
|
+
|
|
977
|
+
(defun memory-discarded (state node)
|
|
978
|
+
;; Reuse expression's debug ranges even when a pure result emits no words.
|
|
979
|
+
(expression state node t))
|
|
980
|
+
|
|
981
|
+
(defun memory-discarded-body (state node)
|
|
982
|
+
(let ((kind (get node :kind)) (children (get node :children)) (i 0))
|
|
983
|
+
(cond
|
|
984
|
+
((or (= kind "literal") (= kind "int-literal") (= kind "local")) nil)
|
|
985
|
+
((or (= kind "sequence")
|
|
986
|
+
(and (= kind "word-compare") (= (js-length children) 2))
|
|
987
|
+
(and (= kind "word-arithmetic") (<= (js-length children) 2)
|
|
988
|
+
(js-call (js-array "+" "-" "*" "1+" "1-") :includes (get node :value))))
|
|
989
|
+
;; Pure word arithmetic cannot trap. Retain every evaluated operand's
|
|
990
|
+
;; writes, calls and checked memory access in its original order.
|
|
991
|
+
(while (< i (js-length children))
|
|
992
|
+
(memory-discarded state (js-nth i children)) (setq i (1+ i))))
|
|
993
|
+
((= kind "if")
|
|
994
|
+
(condition state (js-nth 0 children))
|
|
995
|
+
(let ((otherwise (jump state "zero")))
|
|
996
|
+
(memory-discarded state (js-nth 1 children))
|
|
997
|
+
(let ((end (jump state "jump")))
|
|
998
|
+
(patch-jump state otherwise (js-length (get state :words)))
|
|
999
|
+
(memory-discarded state (js-nth 2 children))
|
|
1000
|
+
(patch-jump state end (js-length (get state :words))))))
|
|
1001
|
+
((or (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
|
|
1002
|
+
(emit-loop state children nil))
|
|
1003
|
+
;; In particular memory-op (including loads/division), calls, stores,
|
|
1004
|
+
;; and short-circuit nodes keep their complete existing emission path.
|
|
1005
|
+
(t (expression-body state node)))))
|
|
1006
|
+
|
|
1007
|
+
(defun expression-body (state node)
|
|
1008
|
+
(let ((kind (get node :kind)) (value (get node :value)) (children (get node :children)))
|
|
1009
|
+
(cond
|
|
1010
|
+
((and (get state :memory nil) (= kind "memory-op")) (memory-operation state node))
|
|
1011
|
+
((and (get state :memory nil) (= kind "memory-data-copy")) (memory-data-copy state node))
|
|
1012
|
+
((and (get state :memory nil) (or (= kind "call") (= kind "call-indirect"))) (memory-call state node))
|
|
1013
|
+
((and (get state :memory nil) (= kind "host-call")) (memory-host-call state node))
|
|
1014
|
+
((or (= kind "word-arithmetic") (= kind "word-compare")) (memory-word state node))
|
|
1015
|
+
((= kind "literal")
|
|
1016
|
+
(if (= (get node :type) "f64")
|
|
1017
|
+
(float-literal state value)
|
|
1018
|
+
(if (and (get state :memory nil) (memory-word-type (get node :type)))
|
|
1019
|
+
(memory-integer-register state 0 value)
|
|
1020
|
+
(integer-register state 0 value 0))))
|
|
1021
|
+
((= kind "int-literal")
|
|
1022
|
+
(if (and (get state :memory nil) (memory-word-type (get node :type)))
|
|
1023
|
+
(memory-integer-register state 0 value)
|
|
1024
|
+
(integer-register state 0 value 0)))
|
|
1025
|
+
((= kind "local") (local-access state value (get node :type) nil))
|
|
1026
|
+
((or (= kind "set") (= kind "initialize"))
|
|
1027
|
+
(expression state (js-nth 0 children))
|
|
1028
|
+
(local-access state value (get node :type) t))
|
|
1029
|
+
((= kind "sequence")
|
|
1030
|
+
(if (= (js-length children) 0)
|
|
1031
|
+
(if (get state :memory nil) (memory-integer-register state 0 0) (integer-register state 0 0 0))
|
|
1032
|
+
(let ((i 0)) (while (< i (js-length children))
|
|
1033
|
+
(if (< i (1- (js-length children)))
|
|
1034
|
+
(if (get state :memory nil) (memory-discarded state (js-nth i children))
|
|
1035
|
+
(if (get state :compact nil) (discarded state (js-nth i children))
|
|
1036
|
+
(expression state (js-nth i children))))
|
|
1037
|
+
(expression state (js-nth i children)))
|
|
1038
|
+
(setq i (1+ i))))))
|
|
1039
|
+
((= kind "int-number") (expression state (js-nth 0 children)) (word state 509739008))
|
|
1040
|
+
((= kind "ceil-int") (expression state (js-nth 0 children)) (word state 509919232) (word state 511180800))
|
|
1041
|
+
((or (= kind "int-add") (= kind "int-sub") (= kind "int-and") (= kind "int-compare"))
|
|
1042
|
+
(integer-operation state node 0)
|
|
1043
|
+
(when (= kind "int-compare") (compare-result state value t)))
|
|
1044
|
+
((= kind "arithmetic")
|
|
1045
|
+
(if (direct-value state node)
|
|
1046
|
+
(let* ((depth (get state :depth 0)) (r (pure-value state node)))
|
|
1047
|
+
(word state (+ 509624320 (* r 32))) (put state :depth depth))
|
|
1048
|
+
(cond
|
|
1049
|
+
((= (js-length children) 0) (expression state (js-object :kind "literal" :type "f64"
|
|
1050
|
+
:value (if (= value "*") 1 0))))
|
|
1051
|
+
((and (= (js-length children) 1) (= value "-"))
|
|
1052
|
+
(expression state (js-nth 0 children)) (word state 509689856))
|
|
1053
|
+
((and (= (js-length children) 1) (= value "/"))
|
|
1054
|
+
(expression state (js-object :kind "literal" :type "f64" :value 1))
|
|
1055
|
+
(push-value state "f64") (expression state (js-nth 0 children)) (pop-value state "f64")
|
|
1056
|
+
(word state 509614112))
|
|
1057
|
+
(t
|
|
1058
|
+
(if (or (= (js-length children) 2) (= value "1+") (= value "1-"))
|
|
1059
|
+
(progn
|
|
1060
|
+
(let ((registers (operands state (js-nth 0 children)
|
|
1061
|
+
(if (= (js-length children) 2) (js-nth 1 children)
|
|
1062
|
+
(js-object :kind "literal" :type "f64" :value 1)) "f64")))
|
|
1063
|
+
(binary-word state (cond ((or (= value "+") (= value "1+")) 509618208)
|
|
1064
|
+
((or (= value "-") (= value "1-")) 509622304)
|
|
1065
|
+
((= value "*") 509610016) (t 509614112)) registers)))
|
|
1066
|
+
(expression state (js-nth 0 children)))))))
|
|
1067
|
+
((= kind "compare")
|
|
1068
|
+
(binary-word state 509616160 (operands state (js-nth 0 children) (js-nth 1 children) "f64"))
|
|
1069
|
+
(compare-result state value nil))
|
|
1070
|
+
((= kind "not") (condition state (js-nth 0 children)) (word state 1375731712))
|
|
1071
|
+
((= kind "if")
|
|
1072
|
+
(let ((otherwise (if (get state :compact nil) (condition-jumps state (js-nth 0 children) nil)
|
|
1073
|
+
(progn (condition state (js-nth 0 children)) (js-array (jump state "zero"))))))
|
|
1074
|
+
(expression state (js-nth 1 children))
|
|
1075
|
+
(let ((end (jump state "jump")))
|
|
1076
|
+
(patch-conditions state otherwise (js-length (get state :words)))
|
|
1077
|
+
(expression state (js-nth 2 children))
|
|
1078
|
+
(patch-jump state end (js-length (get state :words))))))
|
|
1079
|
+
((or (= kind "while") (= kind "remainder-while") (= kind "guarded-while"))
|
|
1080
|
+
(emit-loop state children t))
|
|
1081
|
+
((or (= kind "and") (= kind "or"))
|
|
1082
|
+
(let ((i 0) (jumps (js-array)))
|
|
1083
|
+
(while (< i (js-length children))
|
|
1084
|
+
(expression state (js-nth i children))
|
|
1085
|
+
(when (< i (1- (js-length children)))
|
|
1086
|
+
(if (or (= (get node :type) "f64") (= (get node :type) "u32"))
|
|
1087
|
+
(when (= kind "or") (setq i (js-length children)))
|
|
1088
|
+
(js-call jumps :push (jump state (if (= kind "and") "zero" "nonzero")))))
|
|
1089
|
+
(setq i (1+ i)))
|
|
1090
|
+
(setq i 0)
|
|
1091
|
+
(while (< i (js-length jumps))
|
|
1092
|
+
(patch-jump state (js-nth i jumps) (js-length (get state :words))) (setq i (1+ i)))))
|
|
1093
|
+
((= kind "call") (emit-call state node))
|
|
1094
|
+
(t (backend-fail node (str "unknown typed native expression: " kind))))))
|
|
1095
|
+
|
|
1096
|
+
(defun typed-code (program &optional options)
|
|
1097
|
+
(let* ((compact (get (or options (js-object)) :registerAllocation t))
|
|
1098
|
+
(state (js-object :words (js-array) :calls (js-array) :constants (js-array) :compact compact))
|
|
1099
|
+
(functions (get program :functions)) (offsets (js-array)) (i 0))
|
|
1100
|
+
(while (< i (js-length functions))
|
|
1101
|
+
(let* ((function (js-nth i functions)) (locals (get function :locals))
|
|
1102
|
+
(arity (get function :arity)) (j 0))
|
|
1103
|
+
(js-call offsets :push (* (js-length (get state :words)) 4))
|
|
1104
|
+
(put state :locals locals)
|
|
1105
|
+
(let ((allocation (if compact (allocate-registers (get function :body) locals arity) (js-object))))
|
|
1106
|
+
(put state :registers (get allocation :registers (js-array)))
|
|
1107
|
+
(put state :leaf (get allocation :leaf nil)) (put state :depth 0))
|
|
1108
|
+
(put state :literalRegisters (js-array))
|
|
1109
|
+
(let ((constants (if compact (allocate-literal-registers state (get function :body)) (js-array))))
|
|
1110
|
+
(word state 2847898621) (word state 2432697341)
|
|
1111
|
+
(stack-adjust state (* (js-call globalThis.Math :ceil (/ (+ (js-length locals) (if compact 18 0)) 2)) 16) t)
|
|
1112
|
+
(when compact (preserve-registers state (js-length locals) nil))
|
|
1113
|
+
(let ((registers (get state :registers)) (capture nil) (k 0))
|
|
1114
|
+
(when compact
|
|
1115
|
+
(while (< k arity)
|
|
1116
|
+
(let ((r (js-nth k registers)))
|
|
1117
|
+
(when (and (>= r 0) (< r arity) (> r k)) (setq capture t)))
|
|
1118
|
+
(setq k (1+ k))))
|
|
1119
|
+
;; Capture every ABI argument before assigning physical registers:
|
|
1120
|
+
;; a leaf's allocated d2-d7 may still hold a later incoming argument.
|
|
1121
|
+
(when capture (put state :registers (js-array)))
|
|
1122
|
+
(while (< j arity)
|
|
1123
|
+
(if (< j 8) (word state (+ 509624320 (* j 32)))
|
|
1124
|
+
(progn
|
|
1125
|
+
(integer-register state 9 (+ 16 (* (- j 8) 8)) 0)
|
|
1126
|
+
(word state 2332623785) (word state 4248830240)))
|
|
1127
|
+
(local-access state j "f64" t)
|
|
1128
|
+
(setq j (1+ j)))
|
|
1129
|
+
(when capture
|
|
1130
|
+
(setq j 0)
|
|
1131
|
+
(while (< j arity)
|
|
1132
|
+
(put state :registers (js-array)) (local-access state j "f64" nil)
|
|
1133
|
+
(put state :registers registers) (local-access state j "f64" t)
|
|
1134
|
+
(setq j (1+ j))))
|
|
1135
|
+
(put state :registers registers))
|
|
1136
|
+
(let ((k 0))
|
|
1137
|
+
(while (< k (js-length constants))
|
|
1138
|
+
(let ((entry (js-nth k constants)))
|
|
1139
|
+
(float-literal state (get entry :value)) (retarget-last state (get entry :register)))
|
|
1140
|
+
(setq k (1+ k))))
|
|
1141
|
+
(put state :literalRegisters constants)
|
|
1142
|
+
(expression state (get function :body))
|
|
1143
|
+
(when compact (preserve-registers state (js-length locals) t))
|
|
1144
|
+
(word state 2432697279) (word state 2831252477) (word state 3596551104)))
|
|
1145
|
+
(setq i (1+ i)))
|
|
1146
|
+
(finish-calls state offsets)
|
|
1147
|
+
(finish-branches state offsets)
|
|
1148
|
+
(finish-literals state)
|
|
1149
|
+
(js-object :words (get state :words) :offsets offsets)))
|
|
1150
|
+
|
|
1151
|
+
(defun macho-object (program code &optional options)
|
|
1152
|
+
(let* ((words (get code :words)) (offsets (get code :offsets))
|
|
1153
|
+
(functions (get program :functions)) (size (* (js-length words) 4))
|
|
1154
|
+
(symbol-offset (* (js-call globalThis.Math :ceil (/ (+ 208 size) 8)) 8))
|
|
1155
|
+
(strings (js-array 0)) (names (js-array)) (output (binary-output options)) (i 0))
|
|
1156
|
+
(while (< i (js-length functions))
|
|
1157
|
+
(js-call names :push (js-length strings))
|
|
1158
|
+
(string-bytes strings (str "_" (get (js-nth i functions) :name)))
|
|
1159
|
+
(setq i (1+ i)))
|
|
1160
|
+
;; mach_header_64; one segment/section and one symbol table.
|
|
1161
|
+
(u32 output 4277009103) (u32 output 16777228) (u32 output 0)
|
|
1162
|
+
(u32 output 1) (u32 output 2) (u32 output 176) (u32 output 0) (u32 output 0)
|
|
1163
|
+
(u32 output 25) (u32 output 152) (zeros output 16)
|
|
1164
|
+
(u64 output 0) (u64 output size) (u64 output 208) (u64 output size)
|
|
1165
|
+
(u32 output 7) (u32 output 7) (u32 output 1) (u32 output 0)
|
|
1166
|
+
(ascii-field output "__text" 16) (ascii-field output "__TEXT" 16)
|
|
1167
|
+
(u64 output 0) (u64 output size) (u32 output 208) (u32 output 2)
|
|
1168
|
+
(u32 output 0) (u32 output 0) (u32 output 2147484672) (zeros output 12)
|
|
1169
|
+
(u32 output 2) (u32 output 24) (u32 output symbol-offset)
|
|
1170
|
+
(u32 output (js-length functions)) (u32 output (+ symbol-offset (* (js-length functions) 16)))
|
|
1171
|
+
(u32 output (js-length strings))
|
|
1172
|
+
(native-append-words output words)
|
|
1173
|
+
(zeros output (- symbol-offset (js-length output)))
|
|
1174
|
+
(setq i 0)
|
|
1175
|
+
(while (< i (js-length functions))
|
|
1176
|
+
(u32 output (js-nth i names))
|
|
1177
|
+
(binary-byte output (if (js-call (get program :exports) :includes (get (js-nth i functions) :name)) 15 14))
|
|
1178
|
+
(binary-byte output 1) (binary-byte output 0) (binary-byte output 0)
|
|
1179
|
+
(u64 output (js-nth i offsets))
|
|
1180
|
+
(setq i (1+ i)))
|
|
1181
|
+
(setq i 0)
|
|
1182
|
+
(while (< i (js-length strings)) (binary-byte output (js-nth i strings)) (setq i (1+ i)))
|
|
1183
|
+
(put code :section (js-object :name "__text" :fileOffset 208 :virtualAddress 0 :size size))
|
|
1184
|
+
(binary-finish output)))
|
|
1185
|
+
|
|
1186
|
+
(defun emit-native-numeric-object (program target &optional options)
|
|
1187
|
+
(unless (or (= target "arm64-macos") (= target "arm64-linux"))
|
|
1188
|
+
(backend-fail program "native object target must be explicit arm64-macos or arm64-linux"))
|
|
1189
|
+
(lower-numeric-program program)
|
|
1190
|
+
(let ((typed-program (optimize-numeric-program (lower-numeric-program (optimize-program program))
|
|
1191
|
+
(js-object :unswitch (get (or options (js-object)) :registerAllocation t)
|
|
1192
|
+
:constantFlow (get (or options (js-object)) :registerAllocation t)
|
|
1193
|
+
:parallelSingle (get (or options (js-object)) :registerAllocation t)
|
|
1194
|
+
:cyclicMultiple (get (or options (js-object)) :registerAllocation t)))))
|
|
1195
|
+
(if (= target "arm64-linux")
|
|
1196
|
+
(emit-elf-object typed-program (typed-code typed-program options) options)
|
|
1197
|
+
(macho-object typed-program (typed-code typed-program options) options))))
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
(defun append (output bytes)
|
|
1201
|
+
(binary-append output bytes))
|
|
1202
|
+
|
|
1203
|
+
(defun leb (output value)
|
|
1204
|
+
(while (>= value 128)
|
|
1205
|
+
(js-call output :push (bit-or 128 (bit-and value 127)))
|
|
1206
|
+
(setq value (unsigned-bit-shift-right value 7)))
|
|
1207
|
+
(js-call output :push value))
|
|
1208
|
+
|
|
1209
|
+
(defun align-size (value alignment)
|
|
1210
|
+
(* (js-call globalThis.Math :ceil (/ value alignment)) alignment))
|
|
1211
|
+
|
|
1212
|
+
(defun export-trie (program offsets)
|
|
1213
|
+
(let ((root (js-object :children (js-array) :address -1 :offset 0))
|
|
1214
|
+
(functions (get program :functions)) (nodes (js-array)) (i 0))
|
|
1215
|
+
(while (< i (js-length functions))
|
|
1216
|
+
(when (js-call (get program :exports) :includes (get (js-nth i functions) :name))
|
|
1217
|
+
(let ((bytes (js-array)) (node root) (j 0))
|
|
1218
|
+
(string-bytes bytes (str "_" (get (js-nth i functions) :name)))
|
|
1219
|
+
(while (< j (1- (js-length bytes)))
|
|
1220
|
+
(let* ((edges (get node :children)) (k 0) (next nil))
|
|
1221
|
+
(while (< k (js-length edges))
|
|
1222
|
+
(when (= (get (js-nth k edges) :byte) (js-nth j bytes))
|
|
1223
|
+
(setq next (get (js-nth k edges) :node)))
|
|
1224
|
+
(setq k (1+ k)))
|
|
1225
|
+
(unless next
|
|
1226
|
+
(setq next (js-object :children (js-array) :address -1 :offset 0))
|
|
1227
|
+
(js-call edges :push (js-object :byte (js-nth j bytes) :node next)))
|
|
1228
|
+
(setq node next))
|
|
1229
|
+
(setq j (1+ j)))
|
|
1230
|
+
(put node :address (+ 4096 (js-nth i offsets)))))
|
|
1231
|
+
(setq i (1+ i)))
|
|
1232
|
+
;; Breadth-first layout avoids call-stack depth dependence for names.
|
|
1233
|
+
(js-call nodes :push root)
|
|
1234
|
+
(setq i 0)
|
|
1235
|
+
(while (< i (js-length nodes))
|
|
1236
|
+
(let ((children (get (js-nth i nodes) :children)) (j 0))
|
|
1237
|
+
(while (< j (js-length children))
|
|
1238
|
+
(js-call nodes :push (get (js-nth j children) :node)) (setq j (1+ j))))
|
|
1239
|
+
(setq i (1+ i)))
|
|
1240
|
+
(let ((changed t) (round 0) (output (js-array)))
|
|
1241
|
+
(while changed
|
|
1242
|
+
(setq changed nil) (setq output (js-array)) (setq i 0)
|
|
1243
|
+
(while (< i (js-length nodes))
|
|
1244
|
+
(let ((node (js-nth i nodes)))
|
|
1245
|
+
(unless (= (get node :offset) (js-length output)) (setq changed t))
|
|
1246
|
+
(put node :offset (js-length output))
|
|
1247
|
+
(trie-node output node))
|
|
1248
|
+
(setq i (1+ i)))
|
|
1249
|
+
(setq round (1+ round))
|
|
1250
|
+
(when (> round (+ 1 (* 5 (js-length nodes))))
|
|
1251
|
+
(backend-fail program "native export trie offset convergence failed")))
|
|
1252
|
+
output)))
|
|
1253
|
+
|
|
1254
|
+
(defun trie-node (output node)
|
|
1255
|
+
(let ((terminal (js-array)) (children (get node :children)) (i 0))
|
|
1256
|
+
(when (>= (get node :address) 0) (leb terminal 0) (leb terminal (get node :address)))
|
|
1257
|
+
(leb output (js-length terminal)) (append output terminal)
|
|
1258
|
+
(when (> (js-length children) 255) (backend-fail nil "native trie has too many edges"))
|
|
1259
|
+
(js-call output :push (js-length children))
|
|
1260
|
+
(while (< i (js-length children))
|
|
1261
|
+
(let ((edge (js-nth i children)))
|
|
1262
|
+
(js-call output :push (get edge :byte) 0)
|
|
1263
|
+
(leb output (get (get edge :node) :offset)))
|
|
1264
|
+
(setq i (1+ i)))))
|
|
1265
|
+
|
|
1266
|
+
(defun dylib-command (output command name)
|
|
1267
|
+
(let ((bytes (js-array)))
|
|
1268
|
+
(string-bytes bytes name)
|
|
1269
|
+
(let ((size (align-size (+ 24 (js-length bytes)) 8)))
|
|
1270
|
+
(u32 output command) (u32 output size) (u32 output 24)
|
|
1271
|
+
(u32 output 0) (u32 output 65536) (u32 output 65536)
|
|
1272
|
+
(append output bytes) (zeros output (- size 24 (js-length bytes))))))
|
|
1273
|
+
|
|
1274
|
+
(defun be32 (output value)
|
|
1275
|
+
(js-call output :push (unsigned-bit-shift-right value 24)
|
|
1276
|
+
(bit-and (unsigned-bit-shift-right value 16) 255)
|
|
1277
|
+
(bit-and (unsigned-bit-shift-right value 8) 255) (bit-and value 255)))
|
|
1278
|
+
|
|
1279
|
+
(defun be64 (output value)
|
|
1280
|
+
(be32 output (uint32 (/ value 4294967296))) (be32 output (uint32 value)))
|
|
1281
|
+
|
|
1282
|
+
(defun page-signature (image executable-size main)
|
|
1283
|
+
(let* ((identifier (js-array)) (limit (js-length image))
|
|
1284
|
+
(pages (js-call globalThis.Math :ceil (/ limit 4096)))
|
|
1285
|
+
(directory (js-array)) (output (js-array)))
|
|
1286
|
+
(string-bytes identifier "eliscript-native")
|
|
1287
|
+
(let ((hash-offset (+ 88 (js-length identifier))))
|
|
1288
|
+
(be32 directory 4208856066) (be32 directory (+ hash-offset (* pages 32)))
|
|
1289
|
+
(be32 directory 132096) (be32 directory 131074)
|
|
1290
|
+
(be32 directory hash-offset) (be32 directory 88)
|
|
1291
|
+
(be32 directory 0) (be32 directory pages) (be32 directory limit)
|
|
1292
|
+
(js-call directory :push 32 2 0 12)
|
|
1293
|
+
(be32 directory 0) (be32 directory 0) (be32 directory 0) (be32 directory 0)
|
|
1294
|
+
(be64 directory 0) (be64 directory 0) (be64 directory executable-size) (be64 directory (if main 1 0))
|
|
1295
|
+
(append directory identifier)
|
|
1296
|
+
(let ((i 0))
|
|
1297
|
+
(while (< i pages)
|
|
1298
|
+
(append directory (sha256-bytes (binary-slice image (* i 4096) (* (1+ i) 4096))))
|
|
1299
|
+
(setq i (1+ i))))
|
|
1300
|
+
(be32 output 4208856256) (be32 output (+ 20 (js-length directory)))
|
|
1301
|
+
(be32 output 1) (be32 output 0) (be32 output 20)
|
|
1302
|
+
(append output directory)
|
|
1303
|
+
output)))
|
|
1304
|
+
|
|
1305
|
+
(defun macho-library (program code &optional entry options)
|
|
1306
|
+
(let* ((functions (get program :functions)) (offsets (get code :offsets))
|
|
1307
|
+
(words (get code :words)) (size (* 4 (js-length words)))
|
|
1308
|
+
(text-size (align-size (+ 4096 size) 16384))
|
|
1309
|
+
(trie (export-trie program offsets))
|
|
1310
|
+
(symbols (js-array)) (strings (js-array 0)) (local-count 0)
|
|
1311
|
+
(commands (js-array)) (image (binary-output options)) (i 0))
|
|
1312
|
+
;; Local symbols precede external definitions; public surface is exact.
|
|
1313
|
+
(let ((pass 0))
|
|
1314
|
+
(while (< pass 2)
|
|
1315
|
+
(let ((external (= pass 1)))
|
|
1316
|
+
(setq i 0)
|
|
1317
|
+
(while (< i (js-length functions))
|
|
1318
|
+
(let* ((function (js-nth i functions))
|
|
1319
|
+
(public (js-call (get program :exports) :includes (get function :name))))
|
|
1320
|
+
(when (if public external (not external))
|
|
1321
|
+
(u32 symbols (js-length strings))
|
|
1322
|
+
(js-call symbols :push (if public 15 14) 1 0 0)
|
|
1323
|
+
(u64 symbols (+ (if entry 4294967296 0) 4096 (js-nth i offsets)))
|
|
1324
|
+
(string-bytes strings (str "_" (get function :name)))
|
|
1325
|
+
(unless public (setq local-count (1+ local-count)))))
|
|
1326
|
+
(setq i (1+ i))))
|
|
1327
|
+
(setq pass (1+ pass))))
|
|
1328
|
+
(let* ((string-offset (+ text-size (js-length symbols)))
|
|
1329
|
+
(trie-offset (align-size (+ string-offset (js-length strings)) 8))
|
|
1330
|
+
(signature-offset (align-size (+ trie-offset (js-length trie)) 16))
|
|
1331
|
+
(signature-size (+ 108 17 (* (js-call globalThis.Math :ceil (/ signature-offset 4096)) 32)))
|
|
1332
|
+
(link-size (+ (- signature-offset text-size) signature-size)))
|
|
1333
|
+
(when entry
|
|
1334
|
+
(u32 commands 25) (u32 commands 72) (ascii-field commands "__PAGEZERO" 16)
|
|
1335
|
+
(u64 commands 0) (u64 commands 4294967296) (u64 commands 0) (u64 commands 0)
|
|
1336
|
+
(u32 commands 0) (u32 commands 0) (u32 commands 0) (u32 commands 0))
|
|
1337
|
+
(u32 commands 25) (u32 commands 152) (ascii-field commands "__TEXT" 16)
|
|
1338
|
+
(u64 commands (if entry 4294967296 0)) (u64 commands text-size) (u64 commands 0) (u64 commands text-size)
|
|
1339
|
+
(u32 commands 5) (u32 commands 5) (u32 commands 1) (u32 commands 0)
|
|
1340
|
+
(ascii-field commands "__text" 16) (ascii-field commands "__TEXT" 16)
|
|
1341
|
+
(u64 commands (+ (if entry 4294967296 0) 4096)) (u64 commands size) (u32 commands 4096) (u32 commands 2)
|
|
1342
|
+
(u32 commands 0) (u32 commands 0) (u32 commands 2147484672) (zeros commands 12)
|
|
1343
|
+
(u32 commands 25) (u32 commands 72) (ascii-field commands "__LINKEDIT" 16)
|
|
1344
|
+
(u64 commands (+ (if entry 4294967296 0) text-size)) (u64 commands (align-size link-size 16384))
|
|
1345
|
+
(u64 commands text-size) (u64 commands link-size)
|
|
1346
|
+
(u32 commands 1) (u32 commands 1) (u32 commands 0) (u32 commands 0)
|
|
1347
|
+
(if entry
|
|
1348
|
+
(progn
|
|
1349
|
+
(u32 commands 14) (u32 commands 32) (u32 commands 12)
|
|
1350
|
+
(ascii-field commands "/usr/lib/dyld" 20)
|
|
1351
|
+
(u32 commands 2147483688) (u32 commands 24)
|
|
1352
|
+
(u64 commands (+ 4096 entry)) (u64 commands 0))
|
|
1353
|
+
(dylib-command commands 13 "@rpath/eliscript-native.dylib"))
|
|
1354
|
+
(dylib-command commands 12 "/usr/lib/libSystem.B.dylib")
|
|
1355
|
+
(u32 commands 27) (u32 commands 24)
|
|
1356
|
+
(append commands (js-call (sha256-words-le words (if (Array/isArray words) nil native-word-at)) :slice 0 16))
|
|
1357
|
+
(u32 commands 50) (u32 commands 24) (u32 commands 1)
|
|
1358
|
+
(u32 commands 983040) (u32 commands 983040) (u32 commands 0)
|
|
1359
|
+
(u32 commands 2) (u32 commands 24) (u32 commands text-size)
|
|
1360
|
+
(u32 commands (js-length functions)) (u32 commands string-offset) (u32 commands (js-length strings))
|
|
1361
|
+
(u32 commands 11) (u32 commands 80)
|
|
1362
|
+
(u32 commands 0) (u32 commands local-count)
|
|
1363
|
+
(u32 commands local-count) (u32 commands (- (js-length functions) local-count))
|
|
1364
|
+
(u32 commands (js-length functions)) (zeros commands 52)
|
|
1365
|
+
(u32 commands 2147483682) (u32 commands 48) (zeros commands 32)
|
|
1366
|
+
(u32 commands trie-offset) (u32 commands (js-length trie))
|
|
1367
|
+
(u32 commands 29) (u32 commands 16) (u32 commands signature-offset) (u32 commands signature-size)
|
|
1368
|
+
(u32 image 4277009103) (u32 image 16777228) (u32 image 0)
|
|
1369
|
+
(u32 image (if entry 2 6)) (u32 image (if entry 12 10)) (u32 image (js-length commands))
|
|
1370
|
+
(u32 image (if entry 2097285 133)) (u32 image 0)
|
|
1371
|
+
(append image commands)
|
|
1372
|
+
(when (> (js-length image) 4096) (backend-fail program "native load commands exceed text prefix"))
|
|
1373
|
+
(zeros image (- 4096 (js-length image)))
|
|
1374
|
+
(native-append-words image words)
|
|
1375
|
+
(zeros image (- text-size (js-length image)))
|
|
1376
|
+
(append image symbols) (append image strings)
|
|
1377
|
+
(zeros image (- trie-offset (js-length image))) (append image trie)
|
|
1378
|
+
(zeros image (- signature-offset (js-length image)))
|
|
1379
|
+
(let ((signature (page-signature image text-size entry)))
|
|
1380
|
+
(unless (= (js-length signature) signature-size) (backend-fail program "native signature layout mismatch"))
|
|
1381
|
+
(append image signature))
|
|
1382
|
+
(put code :section (js-object :name "__text" :fileOffset 4096
|
|
1383
|
+
:virtualAddress (+ (if entry 4294967296 0) 4096) :size size))
|
|
1384
|
+
(binary-finish image))))
|
|
1385
|
+
|
|
1386
|
+
(defun emit-native-numeric-library (program target &optional options)
|
|
1387
|
+
(unless (or (= target "arm64-macos") (= target "arm64-linux"))
|
|
1388
|
+
(backend-fail program "native library target must be explicit arm64-macos or arm64-linux"))
|
|
1389
|
+
(lower-numeric-program program)
|
|
1390
|
+
(let ((typed-program (optimize-numeric-program (lower-numeric-program (optimize-program program))
|
|
1391
|
+
(js-object :unswitch (get (or options (js-object)) :registerAllocation t)
|
|
1392
|
+
:constantFlow (get (or options (js-object)) :registerAllocation t)
|
|
1393
|
+
:parallelSingle (get (or options (js-object)) :registerAllocation t)
|
|
1394
|
+
:cyclicMultiple (get (or options (js-object)) :registerAllocation t)))))
|
|
1395
|
+
(if (= target "arm64-linux")
|
|
1396
|
+
(emit-elf-library typed-program (typed-code typed-program options) options)
|
|
1397
|
+
(macho-library typed-program (typed-code typed-program options) nil options))))
|
|
1398
|
+
|
|
1399
|
+
(defun emit-native-numeric-executable (program target entry &optional options)
|
|
1400
|
+
(unless (= target "arm64-macos") (backend-fail program "native target must be explicit arm64-macos"))
|
|
1401
|
+
(lower-numeric-program program)
|
|
1402
|
+
(let* ((typed-program (optimize-numeric-program (lower-numeric-program (optimize-program program))
|
|
1403
|
+
(js-object :unswitch (get (or options (js-object)) :registerAllocation t)
|
|
1404
|
+
:constantFlow (get (or options (js-object)) :registerAllocation t)
|
|
1405
|
+
:parallelSingle (get (or options (js-object)) :registerAllocation t)
|
|
1406
|
+
:cyclicMultiple (get (or options (js-object)) :registerAllocation t))))
|
|
1407
|
+
(function (find-function (get typed-program :functions) entry)))
|
|
1408
|
+
(unless (and function (= (get function :arity) 0)
|
|
1409
|
+
(js-call (get typed-program :exports) :includes entry))
|
|
1410
|
+
(backend-fail program "native executable entry must be an exported zero-argument f64 function"))
|
|
1411
|
+
(let* ((code (typed-code typed-program options)) (words (get code :words))
|
|
1412
|
+
(start (* (js-length words) 4)) (state (js-object :words words)))
|
|
1413
|
+
(let ((call (js-length words)))
|
|
1414
|
+
(word state 2483027968)
|
|
1415
|
+
(patch-jump state call (/ (js-nth (get function :index) (get code :offsets)) 4)))
|
|
1416
|
+
(stack-adjust state 16 t) (word state 4244636640)
|
|
1417
|
+
(integer-register state 0 1 0) (word state 2432697313)
|
|
1418
|
+
(integer-register state 2 8 0) (integer-register state 16 4 0)
|
|
1419
|
+
(word state 3556773889)
|
|
1420
|
+
;; A short or failed write is observable as a nonzero process status.
|
|
1421
|
+
(word state 4043317279)
|
|
1422
|
+
(let ((failed (js-length words)))
|
|
1423
|
+
(word state 1409286145)
|
|
1424
|
+
(integer-register state 0 0 0) (integer-register state 16 1 0)
|
|
1425
|
+
(word state 3556773889)
|
|
1426
|
+
(patch-jump state failed (js-length words))
|
|
1427
|
+
(integer-register state 0 1 0) (integer-register state 16 1 0)
|
|
1428
|
+
(word state 3556773889))
|
|
1429
|
+
(finish-branches state (js-array))
|
|
1430
|
+
(put code :words (get state :words))
|
|
1431
|
+
(macho-library typed-program code start options))))
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
;; memory-u32 native ABI: x0 is a caller-owned, nonmoving context:
|
|
1435
|
+
;; [0] byte pointer, [8] current byte length, [16] capacity, [24] error u32.
|
|
1436
|
+
;; Explicit u32 arguments use w1..w7, then Darwin's packed 4-byte slots or
|
|
1437
|
+
;; AAPCS64's 8-byte stack slots selected explicitly by the Linux target.
|
|
1438
|
+
;; Programs with explicit host capabilities additionally read callback@32.
|
|
1439
|
+
;; Debug artifacts use the reserved padding word@28 for trap site ID + 1.
|
|
1440
|
+
;; A zero padding word means that no precise native site was recorded.
|
|
1441
|
+
;; Pure programs retain the original 32-byte context and no host dependency.
|
|
1442
|
+
;; Every nested call receives the same context; growth zeroes exposed bytes.
|
|
1443
|
+
(defun memory-error (state code)
|
|
1444
|
+
(memory-integer-register state 9 code)
|
|
1445
|
+
(word state 3103791721) ;; str w9, [x19, #24]
|
|
1446
|
+
(let ((sites (get state :trapSites nil)))
|
|
1447
|
+
(when sites
|
|
1448
|
+
(memory-integer-register state 9 (1+ (js-length sites)))
|
|
1449
|
+
(let ((range (js-object :kind "trap-site" :function (get state :debugFunction)
|
|
1450
|
+
:functionIndex (get state :debugFunctionIndex) :depth (get state :debugDepth 0)
|
|
1451
|
+
:start (* 4 (js-length (get state :words))) :end (+ 4 (* 4 (js-length (get state :words))))
|
|
1452
|
+
:span nil :code code :site (1+ (js-length sites)))))
|
|
1453
|
+
(js-call sites :push range) (js-call (get state :debugRanges) :push range))
|
|
1454
|
+
(word state 3103792745))) ;; str w9, [x19, #28]
|
|
1455
|
+
(js-call (get state :memoryErrors) :push (jump state "jump")))
|
|
1456
|
+
|
|
1457
|
+
(defun memory-conditional-error (state opcode code)
|
|
1458
|
+
(let ((safe (js-length (get state :words))))
|
|
1459
|
+
(word state opcode)
|
|
1460
|
+
(memory-error state code)
|
|
1461
|
+
(patch-jump state safe (js-length (get state :words)))))
|
|
1462
|
+
|
|
1463
|
+
(defun memory-width (name)
|
|
1464
|
+
(cond ((or (= name "load-u8") (= name "store-u8")) 1)
|
|
1465
|
+
((or (= name "load-u16") (= name "store-u16")) 2)
|
|
1466
|
+
((or (= name "load-u32") (= name "store-u32")) 4)
|
|
1467
|
+
((or (= name "load-f64") (= name "store-f64")) 8) (t 0)))
|
|
1468
|
+
|
|
1469
|
+
(defun memory-data-copy (state node)
|
|
1470
|
+
(let* ((text (get node :value)) (length (js-length text)) (index 0))
|
|
1471
|
+
;; The destination may call, trap or grow memory. Evaluate it once before
|
|
1472
|
+
;; loading the current base/length, and check the complete byte range in
|
|
1473
|
+
;; x registers before the first store. Even an empty copy checks its end.
|
|
1474
|
+
(expression state (js-nth 0 (get node :children)))
|
|
1475
|
+
(memory-integer-register state 10 (* length 2))
|
|
1476
|
+
(word state 2332688394) ;; add x10,x0,x10; no u32 end-address wrap
|
|
1477
|
+
(word state 4181722731) ;; ldr x11,[x19,#8]
|
|
1478
|
+
(word state 3943367007) ;; cmp x10,x11
|
|
1479
|
+
(memory-conditional-error state 1409286153 1) ;; b.ls
|
|
1480
|
+
(when (> length 0)
|
|
1481
|
+
(word state 4181721705) ;; ldr x9,[x19]
|
|
1482
|
+
(word state 2332033321) ;; add x9,x9,x0
|
|
1483
|
+
(while (< index length)
|
|
1484
|
+
(let* ((count (js-call globalThis.Math :min 16384 (- length index)))
|
|
1485
|
+
(end (+ index count)) (cursor index) (skip nil)
|
|
1486
|
+
(data-start 0))
|
|
1487
|
+
;; Each immutable UTF16LE block is at most 32 KiB. Its adjacent ADR
|
|
1488
|
+
;; and unconditional branch stay short regardless of total source
|
|
1489
|
+
;; size or relaxation elsewhere. No pointer relocation or writable
|
|
1490
|
+
;; linear-memory constant pool can corrupt the next invocation.
|
|
1491
|
+
(word state 268435530) ;; adr x10,.+8
|
|
1492
|
+
(setq skip (jump state "jump"))
|
|
1493
|
+
(setq data-start (* 4 (js-length (get state :words))))
|
|
1494
|
+
(while (< cursor end)
|
|
1495
|
+
(word state (bit-or (string-code-unit-at text cursor)
|
|
1496
|
+
(if (< (1+ cursor) end) (* (string-code-unit-at text (1+ cursor)) 65536) 0)))
|
|
1497
|
+
(setq cursor (+ cursor 2)))
|
|
1498
|
+
(when (get state :debugRanges nil)
|
|
1499
|
+
(js-call (get state :debugRanges) :push
|
|
1500
|
+
(js-object :kind "static-data" :dataKind "utf16le"
|
|
1501
|
+
:function (get state :debugFunction) :functionIndex (get state :debugFunctionIndex)
|
|
1502
|
+
:depth (get state :debugDepth 0) :start data-start
|
|
1503
|
+
:end (* 4 (js-length (get state :words))) :span nil)))
|
|
1504
|
+
(patch-jump state skip (js-length (get state :words)))
|
|
1505
|
+
(memory-integer-register state 11 count)
|
|
1506
|
+
(let ((loop (js-length (get state :words))) (again nil))
|
|
1507
|
+
;; Unaligned halfword accesses have the same normal-memory
|
|
1508
|
+
;; contract as load/store-u16, including lone surrogate units.
|
|
1509
|
+
(word state 2034237772) ;; ldrh w12,[x10]
|
|
1510
|
+
(word state 2030043436) ;; strh w12,[x9]
|
|
1511
|
+
(word state 2432698698) ;; add x10,x10,#2
|
|
1512
|
+
(word state 2432698665) ;; add x9,x9,#2
|
|
1513
|
+
(word state 4043310443) ;; subs x11,x11,#1
|
|
1514
|
+
(setq again (js-length (get state :words)))
|
|
1515
|
+
(word state 1409286145) ;; b.ne loop
|
|
1516
|
+
(patch-jump state again loop))
|
|
1517
|
+
(setq index end))))
|
|
1518
|
+
(memory-integer-register state 0 0)))
|
|
1519
|
+
|
|
1520
|
+
(defun memory-word (state node)
|
|
1521
|
+
(let* ((children (get node :children)) (count (js-length children)) (name (get node :value)))
|
|
1522
|
+
(if (= count 0)
|
|
1523
|
+
(if (get state :memory nil) (memory-integer-register state 0 (if (= name "*") 1 0))
|
|
1524
|
+
(integer-register state 0 (if (= name "*") 1 0) 0))
|
|
1525
|
+
(progn
|
|
1526
|
+
(expression state (js-nth 0 children))
|
|
1527
|
+
(cond
|
|
1528
|
+
((= count 2)
|
|
1529
|
+
(let ((right (js-nth 1 children)))
|
|
1530
|
+
(if (and (get state :memory nil) (not (get state :compact nil))
|
|
1531
|
+
(not (get state :leaf nil)) (= (js-length (get state :registers)) 0)
|
|
1532
|
+
(memory-word-type (get right :type))
|
|
1533
|
+
(or (= (get right :kind) "literal") (= (get right :kind) "local")))
|
|
1534
|
+
;; These RHS forms touch only x0/x9 in the memory backend.
|
|
1535
|
+
;; Capture the evaluated LHS without changing evaluation order.
|
|
1536
|
+
(progn (word state 704644065) (expression state right)) ;; mov w1,w0
|
|
1537
|
+
(progn
|
|
1538
|
+
(push-value state "u32") (expression state right) (pop-value state "u32"))))
|
|
1539
|
+
(if (= (get node :kind) "word-compare")
|
|
1540
|
+
(let ((code (cond ((= name "=") 0) ((= name "/=") 1) ((= name "<") 3)
|
|
1541
|
+
((= name "<=") 9) ((= name ">") 8) (t 2))))
|
|
1542
|
+
(word state 1795162175)
|
|
1543
|
+
(word state (+ 446629856 (* (bit-xor code 1) 4096))))
|
|
1544
|
+
(word state (cond ((= name "+") 184549408) ((= name "-") 1258291232) (t 453016608)))))
|
|
1545
|
+
((= name "-") (word state 1258292192))
|
|
1546
|
+
((= name "1+") (word state 285213696))
|
|
1547
|
+
((= name "1-") (word state 1358955520)))))))
|
|
1548
|
+
|
|
1549
|
+
(defun memory-grow (state node)
|
|
1550
|
+
(expression state (js-nth 0 (get node :children)))
|
|
1551
|
+
(word state 4181722731) ;; ldr x11, [x19, #8]
|
|
1552
|
+
(word state 4181723756) ;; ldr x12, [x19, #16]
|
|
1553
|
+
(word state 2332049770) ;; add x10, x11, x0, lsl #16
|
|
1554
|
+
(word state 3943432543) ;; cmp x10, x12
|
|
1555
|
+
(let ((failed (js-length (get state :words))))
|
|
1556
|
+
(word state 1409286152) ;; b.hi failure
|
|
1557
|
+
(word state 3545300320) ;; lsr x0, x11, #16 (old page count)
|
|
1558
|
+
(word state 4181721705) ;; ldr x9, [x19]
|
|
1559
|
+
(word state 2332754217) ;; add x9, x9, x11
|
|
1560
|
+
(word state 3406496076) ;; sub x12, x10, x11
|
|
1561
|
+
(let ((done (js-length (get state :words))))
|
|
1562
|
+
(word state 3019898892) ;; cbz x12, done
|
|
1563
|
+
(let ((loop (js-length (get state :words))))
|
|
1564
|
+
(word state 939529535) ;; strb wzr, [x9], #1
|
|
1565
|
+
(word state 4043310476) ;; subs x12, x12, #1
|
|
1566
|
+
(let ((back (js-length (get state :words))))
|
|
1567
|
+
(word state 1409286145) (patch-jump state back loop)))
|
|
1568
|
+
(patch-jump state done (js-length (get state :words))))
|
|
1569
|
+
(word state 4177528426) ;; str x10, [x19, #8]
|
|
1570
|
+
(let ((end (jump state "jump")))
|
|
1571
|
+
(patch-jump state failed (js-length (get state :words)))
|
|
1572
|
+
(memory-integer-register state 0 4294967295)
|
|
1573
|
+
(patch-jump state end (js-length (get state :words))))))
|
|
1574
|
+
|
|
1575
|
+
(defun memory-f64-unary-instruction (name)
|
|
1576
|
+
;; Arm A64 scalar FP one-source encoding, ftype=01, Rn=Rd=0.
|
|
1577
|
+
(cond ((= name "f64-abs") 509657088) ;; fabs d0,d0
|
|
1578
|
+
((= name "f64-neg") 509689856) ;; fneg d0,d0
|
|
1579
|
+
((= name "f64-ceil") 509919232) ;; frintp d0,d0
|
|
1580
|
+
((= name "f64-floor") 509952000) ;; frintm d0,d0
|
|
1581
|
+
((= name "f64-trunc") 509984768) ;; frintz d0,d0
|
|
1582
|
+
((= name "f64-nearest") 509886464) ;; frintn d0,d0: ties to even
|
|
1583
|
+
((= name "f64-sqrt") 509722624) ;; fsqrt d0,d0
|
|
1584
|
+
(t nil)))
|
|
1585
|
+
|
|
1586
|
+
(defun memory-f64-unary (state node)
|
|
1587
|
+
(expression state (js-nth 0 (get node :children)))
|
|
1588
|
+
;; The child completes before this instruction-local environment begins.
|
|
1589
|
+
;; x9 is the existing scratch register; no calls, loads or branches can
|
|
1590
|
+
;; leave this window without restoring the caller's exact FPCR. Zero
|
|
1591
|
+
;; selects nearest-even sqrt, gradual underflow, ordinary NaN propagation
|
|
1592
|
+
;; and disabled FP exception traps, independently of the embedding host.
|
|
1593
|
+
(word state 3577431049) ;; mrs x9,FPCR (0xd53b4409)
|
|
1594
|
+
(word state 3575333919) ;; msr FPCR,xzr (0xd51b441f)
|
|
1595
|
+
(word state (memory-f64-unary-instruction (get node :value)))
|
|
1596
|
+
(word state 3575333897)) ;; msr FPCR,x9 (0xd51b4409)
|
|
1597
|
+
|
|
1598
|
+
(defun memory-operation (state node)
|
|
1599
|
+
(let* ((name (get node :value)) (children (get node :children)) (width (memory-width name))
|
|
1600
|
+
(store (= (js-length children) 2)))
|
|
1601
|
+
(cond
|
|
1602
|
+
((memory-f64-unary-instruction name) (memory-f64-unary state node))
|
|
1603
|
+
((> width 0)
|
|
1604
|
+
(expression state (js-nth 0 children))
|
|
1605
|
+
(when store
|
|
1606
|
+
(push-value state "u32")
|
|
1607
|
+
(expression state (js-nth 1 children))
|
|
1608
|
+
(push-value state (if (= name "store-f64") "f64" "u32"))
|
|
1609
|
+
(pop-value state (if (= name "store-f64") "f64" "u32"))
|
|
1610
|
+
(word state (if (= name "store-f64") 509624354 704709602)) ;; mov d2,d1 / w2,w1
|
|
1611
|
+
(pop-value state "u32") (word state 704709600))
|
|
1612
|
+
;; w0 zero-extends address; compare address+width in 64 bits before
|
|
1613
|
+
;; adding the host pointer, including near-4GiB wrapping addresses.
|
|
1614
|
+
(word state (+ 2432696330 (* width 1024))) ;; add x10,x0,#width
|
|
1615
|
+
(word state 4181722731) (word state 3943367007)
|
|
1616
|
+
(memory-conditional-error state 1409286153 1) ;; b.ls safe
|
|
1617
|
+
(word state 4181721705) (word state 2332033321)
|
|
1618
|
+
(word state (cond ((= name "load-u8") 960495904) ((= name "load-u16") 2034237728)
|
|
1619
|
+
((= name "load-u32") 3107979552) ((= name "load-f64") 4248830240)
|
|
1620
|
+
((= name "store-u8") 956301602) ((= name "store-u16") 2030043426)
|
|
1621
|
+
((= name "store-u32") 3103785250) (t 4244635938)))
|
|
1622
|
+
(when store (memory-integer-register state 0 0)))
|
|
1623
|
+
((= name "memory-size") (word state 4181722720) (word state 3545299968))
|
|
1624
|
+
((= name "memory-grow") (memory-grow state node))
|
|
1625
|
+
(t
|
|
1626
|
+
(expression state (js-nth 0 children)) (push-value state "u32")
|
|
1627
|
+
(expression state (js-nth 1 children)) (pop-value state "u32")
|
|
1628
|
+
(when (= name "word-div") (memory-conditional-error state 889192448 2))
|
|
1629
|
+
(word state (cond ((= name "word-div") 448792608) ((= name "word-shr") 448799776)
|
|
1630
|
+
((= name "word-shl") 448798752) ((= name "word-and") 167772192)
|
|
1631
|
+
((= name "word-or") 704643104) ((= name "word-xor") 1241514016)
|
|
1632
|
+
(t (backend-fail node "unsupported native memory32 opcode"))))))))
|
|
1633
|
+
|
|
1634
|
+
(defun memory-call (state node)
|
|
1635
|
+
(let* ((args (get node :children)) (indirect (= (get node :kind) "call-indirect"))
|
|
1636
|
+
(count (- (js-length args) (if indirect 1 0))) (i 0)
|
|
1637
|
+
(slot-size (if (get state :linux nil) 8 4))
|
|
1638
|
+
(extra (* (js-call globalThis.Math :ceil (/ (if (> count 7) (* (- count 7) slot-size) 0) 16)) 16)))
|
|
1639
|
+
(while (< i (js-length args)) (expression state (js-nth i args)) (push-value state "u32") (setq i (1+ i)))
|
|
1640
|
+
(when indirect (pop-value state "u32") (memory-indirect-target state node))
|
|
1641
|
+
(stack-adjust state extra t)
|
|
1642
|
+
(setq i 0)
|
|
1643
|
+
(while (< i count)
|
|
1644
|
+
(word state 2432697321) ;; mov x9, sp
|
|
1645
|
+
(let ((offset (+ extra (* (- count i 1) 16))))
|
|
1646
|
+
(while (> offset 0)
|
|
1647
|
+
(let ((part (if (< offset 4095) offset 4095)))
|
|
1648
|
+
(word state (+ 2432696617 (* part 1024))) (setq offset (- offset part)))))
|
|
1649
|
+
(word state (+ 3107979552 (if (< i 7) (1+ i) 10)))
|
|
1650
|
+
(when (>= i 7)
|
|
1651
|
+
(word state 2432697321)
|
|
1652
|
+
(let ((offset (* (- i 7) slot-size)))
|
|
1653
|
+
(while (> offset 0)
|
|
1654
|
+
(let ((part (if (< offset 4095) offset 4095)))
|
|
1655
|
+
(word state (+ 2432696617 (* part 1024))) (setq offset (- offset part)))))
|
|
1656
|
+
(word state 3103785258))
|
|
1657
|
+
(setq i (1+ i)))
|
|
1658
|
+
(word state 2853372896) ;; mov x0, x19
|
|
1659
|
+
(if indirect (word state 3594453504) ;; blr x16
|
|
1660
|
+
(let ((at (js-length (get state :words))))
|
|
1661
|
+
(word state 2483027968) (js-call (get state :calls) :push (js-object :at at :target (get node :value)))))
|
|
1662
|
+
(stack-adjust state (+ extra (* count 16)) nil)
|
|
1663
|
+
(put state :depth (- (get state :depth) count))
|
|
1664
|
+
(word state 3107986025)
|
|
1665
|
+
(let ((at (js-length (get state :words))))
|
|
1666
|
+
(word state 889192457) (js-call (get state :memoryErrors) :push at))))
|
|
1667
|
+
|
|
1668
|
+
(defun memory-host-call (state node)
|
|
1669
|
+
(let ((children (get node :children))
|
|
1670
|
+
(index (host-capability-index (get state :hostCapabilities) node)))
|
|
1671
|
+
(expression state (js-nth 0 children)) (push-value state "u32")
|
|
1672
|
+
(expression state (js-nth 1 children))
|
|
1673
|
+
(word state 704644067) ;; mov w3,w0: complete rooted argument list
|
|
1674
|
+
(pop-value state "u32")
|
|
1675
|
+
(word state 704709602) ;; mov w2,w1: heap
|
|
1676
|
+
(memory-integer-register state 1 index)
|
|
1677
|
+
(word state 2853372896) ;; mov x0,x19: native context
|
|
1678
|
+
(word state 4181725808) ;; ldr x16,[x19,#32]
|
|
1679
|
+
(memory-conditional-error state 3036676112 6) ;; cbnz x16, ready
|
|
1680
|
+
(word state 3594453504) ;; blr x16
|
|
1681
|
+
;; FFI callbacks return normally; the loader retains the original thrown
|
|
1682
|
+
;; host value and sets the trap latch before this epilogue boundary.
|
|
1683
|
+
(word state 3107986025)
|
|
1684
|
+
(let ((at (js-length (get state :words))))
|
|
1685
|
+
(word state 889192457) (js-call (get state :memoryErrors) :push at))))
|
|
1686
|
+
|
|
1687
|
+
(defun native-phase (observer phase count words branches calls)
|
|
1688
|
+
(when (= (value-type observer) "function")
|
|
1689
|
+
(try
|
|
1690
|
+
(funcall observer phase (js-object :count count :words words :branches branches :calls calls))
|
|
1691
|
+
(catch ignored nil))) nil)
|
|
1692
|
+
|
|
1693
|
+
(defun native-memory-prepare (program observer &optional fresh)
|
|
1694
|
+
(native-phase observer "native-prepare-start" 0 0 0 0)
|
|
1695
|
+
(let ((prepared (if fresh (memory-prepare-fresh-program program) (memory-prepare-program program))))
|
|
1696
|
+
(native-phase observer "native-prepare-complete" (js-length (get prepared :functions)) 0 0 0)
|
|
1697
|
+
prepared))
|
|
1698
|
+
|
|
1699
|
+
(defun native-memory-container (program code target format options observer)
|
|
1700
|
+
(let ((word-count (js-length (get code :words))))
|
|
1701
|
+
(native-phase observer "native-container-start" (js-length (get program :functions)) word-count 0 0)
|
|
1702
|
+
(let ((bytes (if (= target "arm64-linux")
|
|
1703
|
+
(if (= format "object") (emit-elf-object program code options) (emit-elf-library program code options))
|
|
1704
|
+
(if (= format "object") (macho-object program code options) (macho-library program code nil options)))))
|
|
1705
|
+
(native-phase observer "native-container-complete" (js-length (get program :functions)) word-count 0 0)
|
|
1706
|
+
;; All consumers of this private code image have finished. Public typed
|
|
1707
|
+
;; input, completed bytes and exact source-map ranges retain their own
|
|
1708
|
+
;; data; building the final debug document no longer needs this root.
|
|
1709
|
+
(put code :words nil) bytes)))
|
|
1710
|
+
|
|
1711
|
+
(defun memory-code (program &optional target options observer)
|
|
1712
|
+
(let* ((linux (= target "arm64-linux")) (slot-size (if linux 8 4))
|
|
1713
|
+
(debug (get (or options (js-object)) :debug nil))
|
|
1714
|
+
(state (js-object :words (if (get (or options (js-object)) :packedWords nil) (native-word-buffer) (js-array)) :calls (js-array) :constants (js-array)
|
|
1715
|
+
:compact nil :leaf nil :memory t :linux linux :registers (js-array)
|
|
1716
|
+
:debugRanges (if debug (js-array) nil)
|
|
1717
|
+
:trapSites (if debug (js-array) nil)
|
|
1718
|
+
:hostCapabilities (get program :hostCapabilities (js-array))
|
|
1719
|
+
:table (get program :table nil) :tableUses (js-array)))
|
|
1720
|
+
(functions (get program :functions)) (offsets (js-array)) (i 0))
|
|
1721
|
+
(native-phase observer "native-code-start" 0 0 0 0)
|
|
1722
|
+
(while (< i (js-length functions))
|
|
1723
|
+
(let* ((function (js-nth i functions)) (locals (get function :locals)) (arity (get function :arity))
|
|
1724
|
+
(frame (* (js-call globalThis.Math :ceil (/ (1+ (js-length locals)) 2)) 16)) (j 0))
|
|
1725
|
+
(js-call offsets :push (* (js-length (get state :words)) 4))
|
|
1726
|
+
(put state :depth 0) (put state :memoryErrors (js-array))
|
|
1727
|
+
(when debug
|
|
1728
|
+
(put state :debugFunction (get function :name)) (put state :debugFunctionIndex i)
|
|
1729
|
+
(put state :debugDepth 0)
|
|
1730
|
+
(let* ((source (get function :source nil))
|
|
1731
|
+
(range (js-object :kind "function" :function (get function :name) :functionIndex i :depth -1
|
|
1732
|
+
:start (* 4 (js-length (get state :words))) :end 0
|
|
1733
|
+
:span (get function :span (if source (get source :span nil) nil)))))
|
|
1734
|
+
(put state :debugFunctionRange range) (js-call (get state :debugRanges) :push range)))
|
|
1735
|
+
(word state 2847898621) (word state 2432697341)
|
|
1736
|
+
(stack-adjust state frame t) (word state 4177527795) ;; save caller x19
|
|
1737
|
+
(word state 2852127731) ;; mov x19,x0
|
|
1738
|
+
(while (< j arity)
|
|
1739
|
+
(if (< j 7) (word state (+ 704644064 (* (1+ j) 65536)))
|
|
1740
|
+
(progn (integer-register state 9 (+ 16 (* (- j 7) slot-size)) 0)
|
|
1741
|
+
(word state 2332623785) (word state 3107979552)))
|
|
1742
|
+
(local-access state j "u32" t) (setq j (1+ j)))
|
|
1743
|
+
;; Typed memory IR follows WASM's zero-initialized local contract.
|
|
1744
|
+
;; Managed error/frame slots can be read before any explicit store.
|
|
1745
|
+
(while (< j (js-length locals))
|
|
1746
|
+
(let ((type (get (js-nth j locals) :type)))
|
|
1747
|
+
(if (= type "f64") (word state 2657551328) ;; fmov d0,xzr: positive zero
|
|
1748
|
+
(memory-integer-register state 0 0))
|
|
1749
|
+
(local-access state j type t)) (setq j (1+ j)))
|
|
1750
|
+
(expression state (get function :body))
|
|
1751
|
+
(let ((normal (jump state "jump")))
|
|
1752
|
+
(patch-conditions state (get state :memoryErrors) (js-length (get state :words)))
|
|
1753
|
+
(memory-integer-register state 0 0)
|
|
1754
|
+
(patch-jump state normal (js-length (get state :words))))
|
|
1755
|
+
(integer-register state 9 frame 0) (word state 3406365609) ;; sub x9,fp,x9
|
|
1756
|
+
(word state 4181721395) ;; restore x19 independently of temporary stack depth
|
|
1757
|
+
(word state 2432697279) (word state 2831252477) (word state 3596551104)
|
|
1758
|
+
(when debug
|
|
1759
|
+
(put (get state :debugFunctionRange) :end (* 4 (js-length (get state :words))))))
|
|
1760
|
+
(setq i (1+ i))
|
|
1761
|
+
(when (and (= (value-type observer) "function") (= (% i 128) 0))
|
|
1762
|
+
(native-phase observer "native-code-progress" i (js-length (get state :words))
|
|
1763
|
+
(js-length (get state :branches (js-array))) (js-length (get state :calls)))))
|
|
1764
|
+
(let ((call-count (finish-calls state offsets)))
|
|
1765
|
+
(finish-branches state offsets)
|
|
1766
|
+
(memory-finish-table state program offsets)
|
|
1767
|
+
(native-phase observer "native-code-complete" (js-length functions) (js-length (get state :words))
|
|
1768
|
+
(js-length (get state :branches (js-array))) call-count)
|
|
1769
|
+
(let ((code (js-object :words (get state :words) :offsets offsets :debugRanges (get state :debugRanges nil)
|
|
1770
|
+
:trapSites (get state :trapSites nil))))
|
|
1771
|
+
(unless (Array/isArray (get code :words)) (put code :wordReader native-word-at)) code))))
|
|
1772
|
+
|
|
1773
|
+
(defun emit-native-memory-object (program target &optional options)
|
|
1774
|
+
(unless (or (= target "arm64-macos") (= target "arm64-linux"))
|
|
1775
|
+
(backend-fail program "native memory target must be explicit arm64-macos or arm64-linux"))
|
|
1776
|
+
(let ((typed-program (lower-memory-program program)))
|
|
1777
|
+
(emit-typed-native-memory-object typed-program target options)))
|
|
1778
|
+
|
|
1779
|
+
(defun emit-native-memory-library (program target &optional options)
|
|
1780
|
+
(unless (or (= target "arm64-macos") (= target "arm64-linux"))
|
|
1781
|
+
(backend-fail program "native memory target must be explicit arm64-macos or arm64-linux"))
|
|
1782
|
+
(let ((typed-program (lower-memory-program program)))
|
|
1783
|
+
(emit-typed-native-memory-library typed-program target options)))
|
|
1784
|
+
|
|
1785
|
+
|
|
1786
|
+
(defun memory-word-type (type) (or (= type "u32") (= type "i32")))
|
|
1787
|
+
(defun memory-valid-index (value limit)
|
|
1788
|
+
(and (= (value-type value) "number") (= value (uint32 value)) (< value limit)))
|
|
1789
|
+
|
|
1790
|
+
(defun memory-validate-node (node function program arities capabilities &optional completed)
|
|
1791
|
+
(unless (and completed (js-call completed :has node))
|
|
1792
|
+
(let ((kind (get node :kind)) (children (get node :children)) (value (get node :value)) (i 0))
|
|
1793
|
+
(unless (Array/isArray children) (backend-fail node "native typed node requires children"))
|
|
1794
|
+
(when (or (= kind "local") (= kind "set") (= kind "initialize"))
|
|
1795
|
+
(unless (and (memory-valid-index value (js-length (get function :locals)))
|
|
1796
|
+
(= (get node :type) (get (js-nth value (get function :locals)) :type)))
|
|
1797
|
+
(backend-fail node "native typed local index or type is invalid")))
|
|
1798
|
+
(when (= kind "call")
|
|
1799
|
+
(unless (and (memory-valid-index value (js-length (get program :functions)))
|
|
1800
|
+
(= (js-length children) (get (js-nth value (get program :functions)) :arity)))
|
|
1801
|
+
(backend-fail node "native direct call requires an exact declared signature")))
|
|
1802
|
+
(when (= kind "call-indirect")
|
|
1803
|
+
(unless (and (Array/isArray (get program :table nil)) (memory-valid-index value 65536)
|
|
1804
|
+
(= (js-length children) (1+ value)) (js-call arities :includes value)
|
|
1805
|
+
(memory-word-type (get node :type)))
|
|
1806
|
+
(backend-fail node "indirect calls require a memory-u32 table and an exact signature")))
|
|
1807
|
+
(when (= kind "host-call")
|
|
1808
|
+
(host-capability-index capabilities node))
|
|
1809
|
+
(when (and (= kind "memory-op") (memory-f64-unary-instruction value))
|
|
1810
|
+
(unless (and (= (get node :type) "f64") (= (js-length children) 1)
|
|
1811
|
+
(not (nil? (js-nth 0 children)))
|
|
1812
|
+
(= (value-type (js-nth 0 children)) "object")
|
|
1813
|
+
(not (Array/isArray (js-nth 0 children)))
|
|
1814
|
+
(Array/isArray (get (js-nth 0 children) :children))
|
|
1815
|
+
(= (get (js-nth 0 children) :type) "f64"))
|
|
1816
|
+
(backend-fail node "f64 unary memory opcode requires one f64 operand and result")))
|
|
1817
|
+
(when (= kind "memory-data-copy")
|
|
1818
|
+
(unless (and (= (get node :type) "u32") (= (value-type value) "string")
|
|
1819
|
+
(<= (js-length value) 2147483647) (= (js-length children) 1)
|
|
1820
|
+
(= (get (js-nth 0 children) :type) "u32"))
|
|
1821
|
+
(backend-fail node "static UTF16 copy requires memory-u32, one word address and string data")))
|
|
1822
|
+
(while (< i (js-length children))
|
|
1823
|
+
(when (and (or (= kind "call") (= kind "call-indirect") (= kind "host-call"))
|
|
1824
|
+
(not (memory-word-type (get (js-nth i children) :type))))
|
|
1825
|
+
(backend-fail node "native memory calls require word arguments"))
|
|
1826
|
+
(memory-validate-node (js-nth i children) function program arities capabilities completed) (setq i (1+ i))))
|
|
1827
|
+
;; Each memo belongs to one function and one preparation only. Do not
|
|
1828
|
+
;; remember a node until every check and every descendant has succeeded.
|
|
1829
|
+
(when completed (js-call completed :set node t))))
|
|
1830
|
+
|
|
1831
|
+
(defun memory-prepare-program-core (program fresh)
|
|
1832
|
+
(unless (= (get program :abi) "memory-u32") (backend-fail program "native memory emitter requires memory-u32 typed input"))
|
|
1833
|
+
(let* ((functions (get program :functions)) (exports (get program :exports))
|
|
1834
|
+
(capabilities (if fresh (prepare-fresh-host-capabilities program) (prepare-host-capabilities program)))
|
|
1835
|
+
(table (get program :table nil)) (declared (get program :indirectArities (js-array)))
|
|
1836
|
+
(arities (js-array)) (i 0))
|
|
1837
|
+
(unless (and (Array/isArray functions) (Array/isArray exports) (Array/isArray declared)
|
|
1838
|
+
(or (nil? table) (Array/isArray table))
|
|
1839
|
+
(or (= (js-length declared) 0) (Array/isArray table)))
|
|
1840
|
+
(backend-fail program "invalid native memory declarations"))
|
|
1841
|
+
(while (< i (js-length functions))
|
|
1842
|
+
(let* ((function (js-nth i functions)) (arity (get function :arity)) (locals (get function :locals)) (j 0))
|
|
1843
|
+
(unless (and (= (get function :index) i) (memory-valid-index arity 65536)
|
|
1844
|
+
(Array/isArray locals) (<= arity (js-length locals))
|
|
1845
|
+
(memory-word-type (get (get function :body) :type)))
|
|
1846
|
+
(backend-fail function "native memory function requires a word signature"))
|
|
1847
|
+
(while (< j (js-length locals))
|
|
1848
|
+
(let* ((local (js-nth j locals)) (type (get local :type)))
|
|
1849
|
+
(unless (and (= (get local :index) j) (or (memory-word-type type) (= type "f64"))
|
|
1850
|
+
(or (>= j arity) (memory-word-type type)))
|
|
1851
|
+
(backend-fail function "invalid native local layout or parameter type"))) (setq j (1+ j)))
|
|
1852
|
+
(unless (js-call arities :includes arity) (js-call arities :push arity))) (setq i (1+ i)))
|
|
1853
|
+
(setq i 0)
|
|
1854
|
+
(while (< i (js-length declared))
|
|
1855
|
+
(let ((arity (js-nth i declared)))
|
|
1856
|
+
(unless (memory-valid-index arity 65536) (backend-fail program "invalid indirect signature arity"))
|
|
1857
|
+
(unless (js-call arities :includes arity) (js-call arities :push arity))) (setq i (1+ i)))
|
|
1858
|
+
(when table
|
|
1859
|
+
(setq i 0)
|
|
1860
|
+
(while (< i (js-length table))
|
|
1861
|
+
(unless (memory-valid-index (js-nth i table) (js-length functions))
|
|
1862
|
+
(backend-fail program "native table entry is not a declared function index")) (setq i (1+ i))))
|
|
1863
|
+
(setq i 0)
|
|
1864
|
+
(while (< i (js-length functions))
|
|
1865
|
+
(let ((function (js-nth i functions)))
|
|
1866
|
+
(memory-validate-node (get function :body) function program arities capabilities
|
|
1867
|
+
(when fresh (new globalThis.WeakMap)))) (setq i (1+ i)))
|
|
1868
|
+
;; Existing managed aliases become direct word wrappers; source and table
|
|
1869
|
+
;; indices remain unchanged, and no public canonical/typed IR is mutated.
|
|
1870
|
+
(let ((combined (js-call functions :slice)) (public (js-call exports :slice))
|
|
1871
|
+
(aliases (get program :aliases (js-array))) (seen (js-array)))
|
|
1872
|
+
(unless (Array/isArray aliases) (backend-fail program "invalid native aliases"))
|
|
1873
|
+
(setq i 0)
|
|
1874
|
+
(while (< i (js-length public))
|
|
1875
|
+
(unless (and (find-function functions (js-nth i public)) (not (js-call seen :includes (js-nth i public))))
|
|
1876
|
+
(backend-fail program "invalid or duplicate native export"))
|
|
1877
|
+
(js-call seen :push (js-nth i public)) (setq i (1+ i)))
|
|
1878
|
+
(setq i 0)
|
|
1879
|
+
(while (< i (js-length aliases))
|
|
1880
|
+
(let* ((alias (js-nth i aliases)) (name (get alias :name))
|
|
1881
|
+
(target (find-function functions (get alias :function))))
|
|
1882
|
+
(unless (and target (= (value-type name) "string") (not (js-call seen :includes name)))
|
|
1883
|
+
(backend-fail program "invalid or duplicate native alias"))
|
|
1884
|
+
(js-call seen :push name) (js-call public :push name)
|
|
1885
|
+
(if (= name (get target :name)) nil
|
|
1886
|
+
(progn
|
|
1887
|
+
(when (find-function functions name) (backend-fail program "native alias shadows a function"))
|
|
1888
|
+
(let ((locals (js-array)) (arguments (js-array)) (j 0))
|
|
1889
|
+
(while (< j (get target :arity))
|
|
1890
|
+
(js-call locals :push (js-object :name (str "argument-" j) :type "u32" :index j))
|
|
1891
|
+
(js-call arguments :push (js-object :kind "local" :type "u32" :value j :children (js-array))) (setq j (1+ j)))
|
|
1892
|
+
(js-call combined :push (js-object :name name :index (js-length combined) :arity (get target :arity) :locals locals
|
|
1893
|
+
:body (js-object :kind "call" :type "u32" :value (get target :index) :children arguments)))))))
|
|
1894
|
+
(setq i (1+ i)))
|
|
1895
|
+
(let ((prepared (js-object :abi "memory-u32" :filename (get program :filename) :functions combined :exports public :table table)))
|
|
1896
|
+
(when (> (js-length capabilities) 0) (put prepared :hostCapabilities capabilities))
|
|
1897
|
+
prepared))))
|
|
1898
|
+
|
|
1899
|
+
(defun memory-prepare-program (program)
|
|
1900
|
+
(memory-prepare-program-core program nil))
|
|
1901
|
+
(defun memory-prepare-fresh-program (program)
|
|
1902
|
+
(memory-prepare-program-core program t))
|
|
1903
|
+
|
|
1904
|
+
(defun memory-indirect-target (state node)
|
|
1905
|
+
(word state 704709612) ;; mov w12,w1: evaluated table slot
|
|
1906
|
+
(memory-integer-register state 0 (js-length (get state :table)))
|
|
1907
|
+
(word state 1795162527) ;; cmp w12,w0
|
|
1908
|
+
(memory-conditional-error state 1409286147 3) ;; b.lo
|
|
1909
|
+
(let ((at (js-length (get state :words))))
|
|
1910
|
+
(word state 268435465) ;; adr x9, .
|
|
1911
|
+
(js-call (get state :tableUses) :push at)
|
|
1912
|
+
(integer-register state 10 0 0)) ;; patched positive byte distance to table
|
|
1913
|
+
(word state 2332688681)
|
|
1914
|
+
(word state 2332822825) ;; add x9,x9,x12,lsl #3
|
|
1915
|
+
(word state 3107980586) ;; table entry arity
|
|
1916
|
+
(memory-integer-register state 11 (get node :value))
|
|
1917
|
+
(word state 1795883359)
|
|
1918
|
+
(memory-conditional-error state 1409286144 4) ;; b.eq
|
|
1919
|
+
(word state 3112173866) ;; ldrsw x10,[x9]: signed target relative to entry
|
|
1920
|
+
(word state 2332688688)) ;; add x16,x9,x10
|
|
1921
|
+
|
|
1922
|
+
(defun memory-finish-table (state program offsets)
|
|
1923
|
+
(let ((table (get state :table)) (uses (get state :tableUses)) (i 0))
|
|
1924
|
+
(when table
|
|
1925
|
+
(when (/= (bit-and (js-length (get state :words)) 1) 0) (word state 3573751839))
|
|
1926
|
+
(let ((start (js-length (get state :words))))
|
|
1927
|
+
(while (< i (js-length table))
|
|
1928
|
+
(let* ((target (js-nth i table)) (distance (- (js-nth target offsets) (* (js-length (get state :words)) 4))))
|
|
1929
|
+
(unless (and (>= distance -2147483648) (< distance 2147483648))
|
|
1930
|
+
(backend-fail program "native table target exceeds relative range"))
|
|
1931
|
+
(word state distance) (word state (get (js-nth target (get program :functions)) :arity)))
|
|
1932
|
+
(setq i (1+ i)))
|
|
1933
|
+
(setq i 0)
|
|
1934
|
+
(while (< i (js-length uses))
|
|
1935
|
+
(let* ((at (js-nth i uses)) (distance (* (- start at) 4)))
|
|
1936
|
+
(unless (memory-valid-index distance 4294967296) (backend-fail program "native table exceeds relative range"))
|
|
1937
|
+
(native-word-set (get state :words) (1+ at) (+ 3531603978 (* (bit-and distance 65535) 32)))
|
|
1938
|
+
(native-word-set (get state :words) (+ at 2) (+ 4070572042 (* (unsigned-bit-shift-right distance 16) 32))))
|
|
1939
|
+
(setq i (1+ i)))))))
|
|
1940
|
+
|
|
1941
|
+
(defun emit-observed-typed-native-memory-object (program target options observer &optional fresh)
|
|
1942
|
+
(unless (or (= target "arm64-macos") (= target "arm64-linux"))
|
|
1943
|
+
(backend-fail program "native memory target must be explicit arm64-macos or arm64-linux"))
|
|
1944
|
+
(let* ((prepared (native-memory-prepare program observer fresh))
|
|
1945
|
+
(code (memory-code prepared target (js-object :packedWords t) observer)))
|
|
1946
|
+
(native-memory-container prepared code target "object" options observer)))
|
|
1947
|
+
|
|
1948
|
+
(defun emit-observed-typed-native-memory-library (program target options observer &optional fresh)
|
|
1949
|
+
(unless (or (= target "arm64-macos") (= target "arm64-linux"))
|
|
1950
|
+
(backend-fail program "native memory target must be explicit arm64-macos or arm64-linux"))
|
|
1951
|
+
(let* ((prepared (native-memory-prepare program observer fresh))
|
|
1952
|
+
(code (memory-code prepared target (js-object :packedWords t) observer)))
|
|
1953
|
+
(native-memory-container prepared code target "library" options observer)))
|
|
1954
|
+
|
|
1955
|
+
(defun emit-observed-typed-native-memory-artifact (program target options observer &optional fresh)
|
|
1956
|
+
(unless (or (= target "arm64-macos") (= target "arm64-linux"))
|
|
1957
|
+
(backend-fail program "native memory target must be explicit arm64-macos or arm64-linux"))
|
|
1958
|
+
(let* ((format (get (or options (js-object)) :format "library"))
|
|
1959
|
+
(prepared (native-memory-prepare program observer fresh))
|
|
1960
|
+
(code (memory-code prepared target (js-object :debug t :packedWords t) observer))
|
|
1961
|
+
(bytes nil) (functions (js-array)) (ranges (get code :debugRanges)) (i 0))
|
|
1962
|
+
(unless (or (= format "object") (= format "library"))
|
|
1963
|
+
(backend-fail program "native memory artifact format must be object or library"))
|
|
1964
|
+
(setq bytes (native-memory-container prepared code target format options observer))
|
|
1965
|
+
(while (< i (js-length ranges))
|
|
1966
|
+
(let ((range (js-nth i ranges)))
|
|
1967
|
+
(when (= (get range :kind) "function")
|
|
1968
|
+
(js-call functions :push (js-object :name (get range :function) :index (get range :functionIndex)
|
|
1969
|
+
:start (get range :start) :end (get range :end) :span (get range :span)))))
|
|
1970
|
+
(setq i (1+ i)))
|
|
1971
|
+
(js-object :bytes bytes
|
|
1972
|
+
:debug (js-object :format "eliscript-native-source-map" :version 1 :target target :abi "memory-u32"
|
|
1973
|
+
:imageFormat (if (= target "arm64-linux") "elf64" "mach-o64") :artifactFormat format
|
|
1974
|
+
:offsetUnit "byte" :offsetBase "code-section" :relocatable t
|
|
1975
|
+
:trapSites (js-call (get code :trapSites (js-array)) :map (lambda (site)
|
|
1976
|
+
(js-object :site (get site :site) :address (get site :start) :offsetBase "code-section"
|
|
1977
|
+
:functionIndex (get site :functionIndex) :returnAddress false :code (get site :code))))
|
|
1978
|
+
:codeSection (get code :section) :functions functions :ranges ranges))))
|
|
1979
|
+
|
|
1980
|
+
;; Preserve the existing public argument list and options observations. Only
|
|
1981
|
+
;; the in-process session imports the explicit-observer variants above.
|
|
1982
|
+
(defun emit-typed-native-memory-object (program target &optional options)
|
|
1983
|
+
(emit-observed-typed-native-memory-object program target options nil))
|
|
1984
|
+
(defun emit-typed-native-memory-library (program target &optional options)
|
|
1985
|
+
(emit-observed-typed-native-memory-library program target options nil))
|
|
1986
|
+
(defun emit-typed-native-memory-artifact (program target &optional options)
|
|
1987
|
+
(emit-observed-typed-native-memory-artifact program target options nil))
|
|
1988
|
+
|
|
1989
|
+
;; These entries are the session's already lowered, privately created data
|
|
1990
|
+
;; handoff. No program marker or public options property enables its memos.
|
|
1991
|
+
(defun emit-fresh-observed-typed-native-memory-object (program target options observer)
|
|
1992
|
+
(emit-observed-typed-native-memory-object program target options observer t))
|
|
1993
|
+
(defun emit-fresh-observed-typed-native-memory-library (program target options observer)
|
|
1994
|
+
(emit-observed-typed-native-memory-library program target options observer t))
|
|
1995
|
+
(defun emit-fresh-observed-typed-native-memory-artifact (program target options observer)
|
|
1996
|
+
(emit-observed-typed-native-memory-artifact program target options observer t))
|
|
1997
|
+
|
|
1998
|
+
(export emit-typed-native-memory-artifact emit-typed-native-memory-object emit-typed-native-memory-library emit-native-memory-object emit-native-memory-library emit-native-numeric-object emit-native-numeric-library emit-native-numeric-executable
|
|
1999
|
+
emit-observed-typed-native-memory-object emit-observed-typed-native-memory-library emit-observed-typed-native-memory-artifact
|
|
2000
|
+
emit-fresh-observed-typed-native-memory-object emit-fresh-observed-typed-native-memory-library emit-fresh-observed-typed-native-memory-artifact))
|