feat(pbs): add mutable lists and stable snapshot cursors

Implement SDK list<T> and Cursor<T> with trusted compiler bindings and generic storage publication across IR and PBX. Pin the runtime provider and include specs, conformance coverage, installed examples, and DSC-0090 lessons.
This commit is contained in:
2026-10-23 00:46:39 +01:00
parent 76a8b074e8
commit c410e35001
46 changed files with 2683 additions and 165 deletions
@@ -0,0 +1,234 @@
---
id: LSN-0090
ticket: pbs-sdk-list
title: Mutable Lists and Stable Snapshot Cursors
created: 2026-10-22
tags: [compiler-pbs, collections, snapshot-cursor, atomic-publication, runtime-conformance]
---
## Original problem and final result
Owner: `compiler/pbs`. Transversal owners: `compiler/general` for neutral IR/PBX
and the runtime provider for storage, faults, ownership and resource accounting.
DSC-0090 implemented accepted DEC-0075 through PLN-0153, covering R1–R14.
The workflow control artifacts are disposable; the
[List/Cursor specification](../../../docs/compiler/pbs/specs/sdk-list-and-cursor.md)
is the durable normative reference.
PBS needed a resizable collection whose existing references remain valid, together
with a traversal that survives later collection edits. Both now live in the
executable SDK: ordinary generic `list<T>`, the shared `Cursor<T>` contract and
private snapshot nodes. The compiler provides trusted private bindings and source
projections; generic storage instructions support complete publication in the VM.
Linked, Set and Map remain separate discussions.
## Separate collection identity, logical positions and physical storage
A List is a shared object containing a private fixed-length array and logical size.
Growth replaces the backing reference inside that same object. Existing aliases
therefore continue to observe the collection after growth, reserve or trim.
Size includes holes. Capacity alone does not create valid positions. For example,
`new list<int>(16)` has size zero; `resize(3)` creates three valid empty positions.
Writing position zero then stores a present value without changing size. Zero and
false remain present values. `clearAt` leaves a hole, while `removeAt` removes a
position and shifts the complete suffix, including its holes.
The complete API includes constructors, size/capacity, bracket access, add, insert,
clearAt, removeAt, clear, resize, reserve, trim and cursor. The
[specification](../../../docs/compiler/pbs/specs/sdk-list-and-cursor.md) defines
their exact results, geometric growth, structural limits and fault classifications.
Clear, shrink and removal erase discarded references; later growth cannot resurrect
old values. Directly optional elements are rejected, including through grouping;
an optional List remains valid. Applications are invariant, and argument values
do not infer the element type.
## A snapshot freezes the sequence of references
List capture scans its logical positions backwards and prepends one node per
present occurrence. The resulting chain visits increasing source indices, skips
holes and preserves duplicates. Nodes are private, immutable, singly linked and
non-circular. Capture finishes before the Cursor is returned. There is no second
array, source-slot alias or source collection reference needed for traversal.
Replacing, inserting, removing, clearing or resizing the List cannot change this
captured sequence. A captured object reference still identifies the same object,
so later mutations of its fields remain visible. This distinction lets gameplay
change its collections while a consumer walks a stable sequence.
The private cursor object stores its current-node reference. It can replace that
root as it advances without changing any node link. Once other owners and call
frames release consumed nodes, the prefix becomes collectible. The current node
retains the remaining suffix.
### Successor-based hasNext and a retained last element
`current()` initially reports the first element. `hasNext()` asks whether that
element has a successor. `next()` follows an existing successor and otherwise
does nothing.
| Snapshot/state | hasNext() | current() |
| --- | --- | --- |
| Empty, initially or after repeated next | false | none |
| [A], initially or after repeated next | false | some(A) |
| [A, B], initially | true | some(A) |
| [A, B], after one or any further next | false | some(B) |
Process current, then either advance or stop. A hasNext-only loop omits the last
element. A loop waiting for current to become none never finishes a nonempty
snapshot. The
[distributed example](../../../prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/pbs-knowledge/examples/game/1/src/collections.pbs)
captures 20 and 22 around a hole, clears the List, visits both captured values and
returns 42. Current still contains 22 afterwards.
The outer optional describes whether a node exists. A producer with optional T
must preserve `some(none)` as a present node whose payload is none. A synthetic
non-List producer qualifies that distinction independently of List's element rule.
Future Linked/Set producers use Cursor<T>; Map entries use Cursor<keyval<K,V>>.
Their construction rules and ordering guarantees belong to their own discussions.
## Prepare the whole mutation before publishing
An insert without growth can shift many slots and update size. A remove also
prepares an optional result and clears its old tail. Each operation needs a single
failure boundary for all observable changes.
The generic provider adds four operations, carried through common IR and PBX:
| Operation | Purpose |
| --- | --- |
| INDEX_GUARD | Check a signed-long logical index against an exclusive limit |
| ARRAY_CHECK_LENGTH | Validate a structural length without allocating |
| ARRAY_COPY | Transfer complete stored groups with overlap-safe prestate semantics |
| STORAGE_PUBLISH | Prepare and commit source-selected copy/fill ranges and two typed fields |
The SDK chooses its indices, ranges, fill value, target backing and resulting size.
The generic layer validates those operands and resource charges. It snapshots
complete source groups into charged scratch before any write, then commits the
selected copy, fill and field updates without allocation, callbacks, scheduler or
GC interleaving. Its four immediate operands identify the Array/Struct types and
two distinct fields. There is no exported preparation token or general transaction
syntax.
For growth, a private candidate array is allocated and populated first. For a
no-growth edit, scratch and all argument/result carriers are prepared first.
`removeAt` obtains its previous optional value before publication. Raw transfer
keeps presence tags, full value groups and existing reference/carrier identity.
Per-element public optional unpack/repack would add failure points and allocation
costs while shifting.
Refusal leaves backing identity, capacity, size and complete stored groups
unchanged. Argument effects evaluated before the call remain ordinary source
effects. Unpublished candidates and scratch follow normal ownership and budgets.
The provider has no List/Cursor opcode, category or syscall; collection algorithms
remain in
[SDK source](../../../prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/main.pbs).
## Compiler and tooling integration
Reserved SDK attributes carry trusted definition origin. Copying their names into
a project cannot grant private primitive access, and a user struct named list does
not acquire SDK indexing or constructor rules. The public barrel exports List and
Cursor; backing fields, storage bindings, nodes and bracket adapters stay private.
SDK executable functions and implementations use the ordinary generic pipeline.
Supporting them exposed reusable shape requirements: optional recursive payload
members, concrete contract methods, applicable implementations and native group
field maps must survive specialization. Complete values include vec2, pixel,
tuples, nested arrays, contracts and function references.
List index/length arguments accept localized lossless int-to-long promotion.
Receiver and arguments execute once in source order, including named arguments.
General numeric conversion rules stay unchanged. Common aggregation, optimization,
emission and preload verification retain all four publication operands; malformed
types or fields are rejected even in unreachable instructions.
Editor projections show concrete public List/Cursor methods, optional results,
constructor overloads and SDK definitions. Hover/completion hide private adapters
and backing fields. Installed JAR tests load the actual packaged SDK, normative
spec and example outside the checkout, generate PBX and execute it in the provider.
## Evidence and practical limits
The selected provider is clean commit
`d9b9eeb171d2861ec660d4cf5703b0795eac49a9`. Studio changes were tested over base
`76a8b074e89e17c7425220888eeec38427261d46`. Qualification used macOS 27.0.1 arm64,
Rust 1.98.1/LLVM 22.1.8, the dev runtime profile with resource-diagnostics,
capabilities NONE, Gradle 9.3.1 and the repository's Java 21 toolchain
(Corretto 21.0.4). Ordinary and optimized producer paths were exercised.
| Gate | Observed result |
| --- | --- |
| Provider bytecode and VM libraries | 6 + 302 passed; 2 existing measurement tests ignored |
| Full Studio regression and installed knowledge | Passed; existing skipped tests retained |
| Final frontend/editor, LSP, source generation and installed checks | Passed; both installed tests passed |
| PBS List/Cursor through production verifier/VM | 27 cases in each producer mode: 54 passed |
| Non-PBS common IR | 2 storage fixtures executed through production VM |
| Whole-operation quota refusals | 71 attempts; independently observed prior List state preserved |
| Cursor GC | Nodes/items: 1200/1200 → 1/1 → 1/1 → 0/0 |
| Reexports and installed example execution | Both passed; final captured value 22 retained |
| Provider catalogs and canonical mirrors | Clean pinned provider; catalog parity and all four mirrors match |
The refusal matrix lowers GuestBytes, HandleSlots, AuxiliaryBytes and TemporaryBytes
around candidate allocation, no-growth publication scratch, optional return
preparation, node creation and frame/operand preparation. It compares the typed
List fields and every backing group before and after each refusal, and verifies
resource accounting returns to zero after VM drop. Generic provider tests also
cover overlapping multi-slot transfers, late invalid fields/ranges, immutable
Dynamic-box identity and malformed encodings.
Controlled normal GC safepoints prove consumed-prefix reclamation, retention of
the final value 1199 despite repeated next calls, and release after dropping the
Cursor. No source or snapshot Array remains live for that payload type at those
safepoints. Optional carriers and other legitimate owners still have their normal
allocation and retention costs. Capturing n positions with k present values takes
O(n) time and O(k) additional nodes; next follows one link in O(1).
[Machine-readable evidence](runtime-conformance-evidence.json) records commands,
source and PBX hashes, default ceilings, every lowered refusal quota, GC observations
and tested provenance. New cases are mandatory; missing fixtures fail the harness.
The full harness also reports two pre-existing response-schema gaps, LifecycleRole
and NonFiniteValue, with original payloads preserved. These were not changed by
this delivery. Qualification makes no release-profile or cross-platform claim.
R1–R5 are covered by type/construction/bounds/mutation/capacity fixtures; R6–R9 by
snapshot states, identity, non-List optional production and GC; R10–R11 by direct
provider tests, common IR and the refusal matrix; R12 by editor/import/distribution
gates; R13 by resource accounting and retention observations; R14 by the mandatory
production corpus, full regressions and pinned provenance.
## Pitfalls to preserve in future work
- Treat logical size and capacity separately. An index inside physical capacity
can still require TRAP_OOB; a hole is an optional result only at a valid index.
- Keep faults distinct from absence. Long overflow, invalid bounds and resource
refusal cannot become none or a truncated index.
- Prepare optional returns and temporary groups before publication. Individual
Array instruction atomicity alone cannot prove whole-mutator atomicity.
- Preserve complete stored groups while shifting. Public optional reboxing can
change identity, roots, costs and refusal boundaries.
- Follow the Cursor state table. Never advance a nonempty cursor past its last
node or retain an initial-head root just to support an unrequested reset.
- Account for node and optional-carrier allocation. A linked snapshot has no
promise of lower memory use than an array or immediate quota recovery.
- Carry all publication operands through linking and optimization. A lost second
field identity can invalidate otherwise well-typed SDK calls after imports.
- Keep privileged bindings origin-based and private. Names and copied attributes
are insufficient authority.
## References and reproduction
- [List/Cursor specification](../../../docs/compiler/pbs/specs/sdk-list-and-cursor.md)
and [Array/optional foundation](../../../docs/compiler/pbs/specs/typed-arrays-and-optional-execution.md).
- [Common Registry/PBX specification](../../../docs/compiler/general/specs/typed-registry-and-pbx-v0.md)
and [canonical provider publication code](../../../../runtime/crates/console/prometeu-vm/src/virtual_machine/storage_publication.rs).
- [Source fixture generator](../../../prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsListCursorTest.java),
[neutral IR fixtures](../../../prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/registry/StoragePublicationPipelineTest.java)
and [production harness instructions](../../../tools/pbx-conformance/README.md).
- [Generic structs lesson](../DSC-0094-pbs-generic-structs/LSN-0089-concrete-generic-state-and-bodies.md)
and [generic contracts lesson](../DSC-0089-pbs-generic-contracts/LSN-0088-concrete-generic-contracts-from-source-to-vm.md).
Run `./gradlew test :prometeu-app:installedKnowledgeTest --console=plain` before
`python3 tools/pbx-conformance/run.py --runtime ../runtime` so the complete fixture
corpus, including the installed example, exists. Provider and catalog commands,
their working directories and the tested configuration are in the evidence file.
@@ -0,0 +1,699 @@
{
"schema_version": 1,
"discussion": "DSC-0090",
"historical_decision": "DEC-0075",
"historical_plan": "PLN-0153",
"owner": "compiler/pbs",
"date": "2026-10-22",
"provenance": {
"studio_base": "76a8b074e89e17c7425220888eeec38427261d46",
"studio_changes": "uncommitted implementation",
"runtime": "d9b9eeb171d2861ec660d4cf5703b0795eac49a9",
"runtime_working_tree": "clean",
"platform": "macOS-27.0.1-arm64-arm-64bit-Mach-O",
"java": "Corretto 21.0.4+7-LTS; repository Java toolchain 21",
"gradle": "9.3.1",
"rustc": "1.98.1 (48a229cea 2026-09-01)",
"rust_host": "aarch64-apple-darwin",
"llvm": "22.1.8",
"runtime_profile": "dev",
"features": [
"resource-diagnostics"
],
"capabilities": "NONE",
"producer_modes": [
"ordinary",
"optimized"
]
},
"default_vm_limits": {
"GuestBytes": 268435456,
"HandleSlots": 1048576,
"AuxiliaryBytes": 67108864,
"TemporaryBytes": 67108864,
"StringBytes": 67108864,
"StringAllocations": 262144,
"IndividualStringBytes": 4194304,
"CallDepth": 1024
},
"source_sha256": {
"tools/pbx-conformance/run.py": "9acf64b74241f65fa09ab33fcb7d0daf57b56142d2874d87812d7b9948fe94ef",
"tools/pbx-conformance/main.rs": "c24ade424adfe9edd7168814aabdfc869272c7bcfb816f5e4945b771cca96efd",
"prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsRuntimeRandomTest.java": "ba0b3de2e0aa7177fa71b5a837a56bc262a080717df3276c3a73b67b33dcb384",
"prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/random/main.pbs": "c21c12d99c5a033a1e4026432bbab56393ad09c0fe2d36711504ccca01ce93b5",
"prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsListCursorTest.java": "e9a7b24ca82a00339c0b0c96a53660ef4dcf69a52587a7c9e56f5f28b5c0b94a",
"prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/main.pbs": "fd3fb817a55e25553d4f7bbe8cbad5f306f22fd0a8e0bb6f8bf05bc43bc0794b",
"../runtime/crates/console/prometeu-vm/src/virtual_machine/storage_publication.rs": "6d63bb474d7659a1e256e242fb2f3c9655d80173d51a263bad56b30cae3088b6",
"../runtime/Cargo.lock": "cffad9ed62f38a591538abd81dd39270d6022397342c8189f4ac6c7baaa8e795",
"../runtime/docs/vm-arch/INTRINSICS.csv": "0c1bb78a9b81d1e3480ef5713f3d70f0b06772a7dfab2fd00c1f0a6364f98daa",
"../runtime/docs/vm-arch/BUILTIN_CONSTANTS.csv": "e407e4718127aa7ef7b1097b5b4b67d86dd979235e73a551a7c36b628cddc039",
"../runtime/crates/console/prometeu-vm/tests/fixtures/numeric-conformance.tsv": "5536ee18cadaf32e3719b0e8d541dfcf1a84a49ea978a702101547c5ccdee3fe",
"../runtime/crates/console/prometeu-vm/tests/fixtures/random-conformance.tsv": "0bee1cd0366f1716687aee8c7ac39dd495da5fe2908cce37b1443cb04fac542f"
},
"pbx_sha256": {
"common-storage-optimized.pbx": "2820aa4ab46544e719e73dfce7e2c03b36377399e5f12e44f32d1f3020bbd087",
"common-storage.pbx": "2820aa4ab46544e719e73dfce7e2c03b36377399e5f12e44f32d1f3020bbd087",
"pbs-list-basic-optimized.pbx": "700dd48e3ceb84ad5f3e3f793fc0d2c6057ce1e87e0fdbadf408f82cf637f5fd",
"pbs-list-basic.pbx": "0669143076d8b4e4610c9699b371de539ccd3aedf0a6f2d5dc46cc78f23dd148",
"pbs-list-capacity-optimized.pbx": "a75c1ff31ef7527e1097e5da3e33747834ca0bc278f843edde32193aeb489fcc",
"pbs-list-capacity.pbx": "35d68be7ffefc4dd930351efd083e78f34af1603e16c85eab96f4611fc8e2415",
"pbs-list-construction-optimized.pbx": "21ab588682a00856461f2758cd3135f444fb9477d5dd70f6e48c001f7d54fe36",
"pbs-list-construction.pbx": "b10de1f77f47758ca5a1eb7167e0c5241683a660073ae1241063a40b35098100",
"pbs-list-contract-optimized.pbx": "5c5737ad4d3db9306415d2e9d389ceff8bef752b9beef2d57b15ee368c029eb9",
"pbs-list-contract.pbx": "3c3250138ecaa1235f2bf1351e90a988e440dd34118cc22b1d26ab58c91189b7",
"pbs-list-cursor-empty-optimized.pbx": "8741c4ec6a1736a2521fa414def12684af881561c829f88a96dcf1f24b961451",
"pbs-list-cursor-empty.pbx": "c65d444082b8ceebfc9ec266811833c4dc40260ed47bc85cd0d9851d5be31cd1",
"pbs-list-cursor-many-optimized.pbx": "7f5fa28ad380321f459df27606bc59d7e6d8b0bda7be4acc4305c859ca2af740",
"pbs-list-cursor-many.pbx": "3ef35e99df5800fb5368f198a91350ffbb561d46af465db5a19ba986452693e3",
"pbs-list-cursor-single-optimized.pbx": "8a19dd496746df5aea0208e983bc50bbdbd4596cea1cc79fed2f94b12e62c939",
"pbs-list-cursor-single.pbx": "8ff5fe78d78915e51ad7dce6c1af354b4efc5c4729c9655da2172f0595fbd66a",
"pbs-list-evaluation-optimized.pbx": "6e5e5a7e3085ea2eefa5f7e0ff296444eb633072fa8b301224737390d55965d3",
"pbs-list-evaluation.pbx": "06f2de5f371973b93ee07577224670a2c02d15899b7dbd791be9733a67a5fb2b",
"pbs-list-false-present-optimized.pbx": "48460d05c9dc22c8c3cd7ac87d429ff0e5ecffc17b2ca254196598e965248816",
"pbs-list-false-present.pbx": "be70e9a6e741541f68c0180760bfd73ef81c54175c359f2208b512c4f3656816",
"pbs-list-fault-constructor-optimized.pbx": "676d4c73965dbf4976de01655c4e0f97601dc1cdc01d312957e5be9d4d5135fd",
"pbs-list-fault-constructor.pbx": "89cfeced7967ea96280874591e640eb88c255f88016f16cd69a9b2ed1330e02f",
"pbs-list-fault-insert-optimized.pbx": "1c3ade1229ec4ed3095feaf961ad9a1dcedf9d6e2bca2f217187ca1a06be1668",
"pbs-list-fault-insert.pbx": "fc92df90d591665f12dafa4ae937b44adb62dd7b308dd38d30b4ea30459f4f5f",
"pbs-list-fault-length-optimized.pbx": "c114f0f0bae45d1cc6625319af8c79b158f8e4680448a7343f8899f1ae9e1153",
"pbs-list-fault-length.pbx": "fc06adc9a6b67f25644042147f1f7475d9ac9d25b856b1f0ed3d40e3b9e46482",
"pbs-list-fault-long-index-optimized.pbx": "b098baa781990fa70baf0d6d4671eb5e24978b4491f6317c9f2466f7535447eb",
"pbs-list-fault-long-index.pbx": "10ac9074183cf09ef3e2bff8396bd6451b976a80f3b05458804c20169e353a15",
"pbs-list-fault-negative-optimized.pbx": "1f42b229ed6d8118d61767494bc0e132d2a6e63485642ef6f4d218638c676964",
"pbs-list-fault-negative.pbx": "0614c7b5dc4d5b4d799c179974e9e614dbc1fbdefba688c0acc0c43984090112",
"pbs-list-fault-overflow-optimized.pbx": "c8f9f03f0d4c2d67acff1f094dcce96c129ce9e1abd3b9be04240f780ebe6e46",
"pbs-list-fault-overflow.pbx": "3b66b7da63516af9981e3722997aa477bd0736a882eb0fce1c189bf040cf8554",
"pbs-list-fault-read-optimized.pbx": "208f256e6c97753611edd7d222923a2b1a855c7ff1a7c872d1bc3bb9c1dfe72b",
"pbs-list-fault-read.pbx": "acf039804bc732dc642fea696429bee398c2e6b6b6975e03dc2b2b24e1048c19",
"pbs-list-fault-write-optimized.pbx": "35051291b52de2109e5f7f7d4bd0a8a218fefc31cb167fdda173871cd531be2f",
"pbs-list-fault-write.pbx": "bcd721c64efe44f073ee660daebff72dca8ac9c2f8619be04d018ead799795ca",
"pbs-list-function-optimized.pbx": "1002a115aebb3a7433f3978cbaa300fcadf26ad837da6272fd34260e2a9b7dad",
"pbs-list-function.pbx": "1a047759799eff2982e8212a86d7d5237e8584e74cacff8301c29c4ff2c16b7c",
"pbs-list-gc.pbx": "265bf39e53bd3e521166dc1ac14c99920d83726ebac8a0e0a3a339d49d19bb29",
"pbs-list-holes-optimized.pbx": "ad331fd7f9298eadea06dbc55587d48a964f8b746406b316c108819186633d30",
"pbs-list-holes.pbx": "c8263fb24fa8df2b98d7cb405733329281f386946c9c6ad186b01051fa25752c",
"pbs-list-identity-optimized.pbx": "f8c572e91351cc8ff4901cc6e972bc05ea0e2e696a550526dc8c511919b438a9",
"pbs-list-identity.pbx": "0e20e6c0a1c9d754a3cd9421155debb231b0dbd93d0935cc3e59b1f70dc21eca",
"pbs-list-imports.pbx": "2364fde21bb3db0a86717f904103d5df657248e2b7979d0e17d2fe9c27ea7e1e",
"pbs-list-installed.pbx": "83748e5203837069363e4d01b0c26e6423d7978f18ee5f6438dcf6336faeef0f",
"pbs-list-nested-array-optimized.pbx": "c6d5d630c620589cf72bdad3f4226748c60a64f14d0965d5ee1aa22e89d79e17",
"pbs-list-nested-array.pbx": "7e3b07300b6f03eeba738dec95001b6d9ab583f58c268dddfdf0b876605999db",
"pbs-list-pixel-optimized.pbx": "ba46dd0ed8efacbbae0015cdfdc0cfa1567684df6681ed836f3ef15a5f828e82",
"pbs-list-pixel.pbx": "36ceaa3115e9439ff21550637f62631dbae39ff06c553f459d58fd91e69cea24",
"pbs-list-refusal-cursor.pbx": "0734d3608ff52a2cd9754b319c6d8a471429571e4336b9f2ae0cff729177042b",
"pbs-list-refusal-growth.pbx": "2c453f52469127cc71818c69d7e3b3dd1add99f524f791ed46fb0c507da2f392",
"pbs-list-refusal-insert.pbx": "6786c290f3ed924c6cfc1666afb3624385ca75cf44b397a05071388f951ccb80",
"pbs-list-refusal-remove.pbx": "d7f0460f35a716bd0ef6dca47816d5f76bf8b1a9b32d9e3620479d5e2ace0888",
"pbs-list-shared-optional-optimized.pbx": "bb71e6ffcae4da82bada01aeda5a8941361417c2eb97186b416715b8087d5ab2",
"pbs-list-shared-optional.pbx": "98d12e6ab4eaefdab0c825fc063300feaf74839ad3d695b97187d4b6a510a557",
"pbs-list-snapshot-optimized.pbx": "d673a2bbbcbc1de9c343f295fa15cf0a7e68aa2f694474842116fda3f1ec1787",
"pbs-list-snapshot.pbx": "11c9dbbdc3ad403c55875e28054343b5275a1467a721ccf8fe1265204858d39e",
"pbs-list-structural-growth-optimized.pbx": "d7ef11a773ac5ff179297b5ea89d1e4e7c7abfb9e5ab16d5bdb6860e28550ef9",
"pbs-list-structural-growth.pbx": "a35fa7776ce571ceb8c6e77cbc3bcdac98851396eeb581e2445b4a774591d8a3",
"pbs-list-tuple-optimized.pbx": "84f12aeb2e7686b91cbcbf4f64e584857184fe0f98bcddb72b6637c8f3cfe8bc",
"pbs-list-tuple.pbx": "43d948da3e128f4eaeda52e64cf04c1e3f90df6205c17f847f55365729da1997",
"pbs-list-vec2-optimized.pbx": "a2850a8204adaafc9f2bd0d84e01b53ed0898d7d0a5044f5ea716aa2d4429383",
"pbs-list-vec2.pbx": "5d31b5f46c75992bcd798363def57509b1600e14e3432316196e000f371d77de"
},
"full_production_log_sha256": "f9a92347e491173ed11398257aac8cadd098ba9af175958ae920f4ed5339c051",
"source_execution_passes": [
"basic",
"basic-optimized",
"construction",
"construction-optimized",
"capacity",
"capacity-optimized",
"holes",
"holes-optimized",
"structural-growth",
"structural-growth-optimized",
"cursor-empty",
"cursor-empty-optimized",
"cursor-single",
"cursor-single-optimized",
"cursor-many",
"cursor-many-optimized",
"snapshot",
"snapshot-optimized",
"false-present",
"false-present-optimized",
"shared-optional",
"shared-optional-optimized",
"identity",
"identity-optimized",
"tuple",
"tuple-optimized",
"nested-array",
"nested-array-optimized",
"function",
"function-optimized",
"evaluation",
"evaluation-optimized",
"vec2",
"vec2-optimized",
"pixel",
"pixel-optimized",
"contract",
"contract-optimized",
"fault-read",
"fault-read-optimized",
"fault-write",
"fault-write-optimized",
"fault-negative",
"fault-negative-optimized",
"fault-insert",
"fault-insert-optimized",
"fault-length",
"fault-length-optimized",
"fault-overflow",
"fault-overflow-optimized",
"fault-long-index",
"fault-long-index-optimized",
"fault-constructor",
"fault-constructor-optimized"
],
"common_ir_execution_passes": [
"common-storage",
"common-storage-optimized"
],
"distribution_execution_passes": [
"imports",
"installed"
],
"atomic_refusals": [
{
"operation": "insert",
"resource": "GuestBytes",
"limit": 9736,
"opcode": "StructNew"
},
{
"operation": "insert",
"resource": "GuestBytes",
"limit": 9887,
"opcode": "StructNew"
},
{
"operation": "insert",
"resource": "GuestBytes",
"limit": 10038,
"opcode": "StructNew"
},
{
"operation": "insert",
"resource": "GuestBytes",
"limit": 10339,
"opcode": "StructNew"
},
{
"operation": "insert",
"resource": "HandleSlots",
"limit": 5,
"opcode": "StructNew"
},
{
"operation": "insert",
"resource": "HandleSlots",
"limit": 6,
"opcode": "StructNew"
},
{
"operation": "insert",
"resource": "AuxiliaryBytes",
"limit": 1408,
"opcode": "Call"
},
{
"operation": "insert",
"resource": "AuxiliaryBytes",
"limit": 1640,
"opcode": "GetLocal"
},
{
"operation": "insert",
"resource": "TemporaryBytes",
"limit": 504,
"opcode": "StructNew"
},
{
"operation": "insert",
"resource": "TemporaryBytes",
"limit": 2698,
"opcode": "StoragePublish"
},
{
"operation": "insert",
"resource": "TemporaryBytes",
"limit": 4892,
"opcode": "StoragePublish"
},
{
"operation": "insert",
"resource": "TemporaryBytes",
"limit": 9279,
"opcode": "StoragePublish"
},
{
"operation": "remove",
"resource": "GuestBytes",
"limit": 9736,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "GuestBytes",
"limit": 10127,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "GuestBytes",
"limit": 10518,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "GuestBytes",
"limit": 11299,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "HandleSlots",
"limit": 5,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "HandleSlots",
"limit": 6,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "HandleSlots",
"limit": 7,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "HandleSlots",
"limit": 8,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "HandleSlots",
"limit": 9,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "AuxiliaryBytes",
"limit": 1408,
"opcode": "Call"
},
{
"operation": "remove",
"resource": "AuxiliaryBytes",
"limit": 1760,
"opcode": "StructNew"
},
{
"operation": "remove",
"resource": "AuxiliaryBytes",
"limit": 2112,
"opcode": "GetLocal"
},
{
"operation": "remove",
"resource": "TemporaryBytes",
"limit": 504,
"opcode": "GetLocal"
},
{
"operation": "remove",
"resource": "TemporaryBytes",
"limit": 2678,
"opcode": "StoragePublish"
},
{
"operation": "remove",
"resource": "TemporaryBytes",
"limit": 4852,
"opcode": "StoragePublish"
},
{
"operation": "remove",
"resource": "TemporaryBytes",
"limit": 9199,
"opcode": "StoragePublish"
},
{
"operation": "growth",
"resource": "GuestBytes",
"limit": 5640,
"opcode": "ArrayNew"
},
{
"operation": "growth",
"resource": "GuestBytes",
"limit": 7401,
"opcode": "ArrayNew"
},
{
"operation": "growth",
"resource": "GuestBytes",
"limit": 9162,
"opcode": "ArrayNew"
},
{
"operation": "growth",
"resource": "GuestBytes",
"limit": 12683,
"opcode": "StructNew"
},
{
"operation": "growth",
"resource": "HandleSlots",
"limit": 5,
"opcode": "ArrayNew"
},
{
"operation": "growth",
"resource": "HandleSlots",
"limit": 6,
"opcode": "StructNew"
},
{
"operation": "growth",
"resource": "HandleSlots",
"limit": 7,
"opcode": "StructNew"
},
{
"operation": "growth",
"resource": "AuxiliaryBytes",
"limit": 1408,
"opcode": "Call"
},
{
"operation": "growth",
"resource": "AuxiliaryBytes",
"limit": 1640,
"opcode": "GetLocal"
},
{
"operation": "growth",
"resource": "TemporaryBytes",
"limit": 504,
"opcode": "ArrayCopy"
},
{
"operation": "growth",
"resource": "TemporaryBytes",
"limit": 2938,
"opcode": "ArrayCopy"
},
{
"operation": "growth",
"resource": "TemporaryBytes",
"limit": 5372,
"opcode": "ArrayCopy"
},
{
"operation": "growth",
"resource": "TemporaryBytes",
"limit": 10239,
"opcode": "ArrayCopy"
},
{
"operation": "cursor",
"resource": "GuestBytes",
"limit": 9736,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "GuestBytes",
"limit": 59424,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "GuestBytes",
"limit": 109112,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "GuestBytes",
"limit": 208487,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 5,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 6,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 7,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 8,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 9,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 10,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 11,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 12,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 13,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 14,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 15,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 16,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 17,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 18,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 19,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 20,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 165,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 326,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "HandleSlots",
"limit": 646,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "AuxiliaryBytes",
"limit": 1408,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "AuxiliaryBytes",
"limit": 16648,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "AuxiliaryBytes",
"limit": 31888,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "TemporaryBytes",
"limit": 504,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "TemporaryBytes",
"limit": 8058,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "TemporaryBytes",
"limit": 15612,
"opcode": "StructNew"
},
{
"operation": "cursor",
"resource": "TemporaryBytes",
"limit": 30719,
"opcode": "StructNew"
}
],
"refusal_observation": "Independent typed List fields, backing identity, capacity and all stored groups before/after each refusal; all resource usage zero after VM drop.",
"gc_safepoints": [
{
"round": 0,
"nodes": 1200,
"items": 1200,
"collected": 6003
},
{
"round": 1,
"nodes": 1,
"items": 1,
"collected": 13197
},
{
"round": 2,
"nodes": 1,
"items": 1,
"collected": 15597
},
{
"round": 3,
"nodes": 0,
"items": 0,
"collected": 16803
}
],
"gc_assertions": [
"Consumed prefix reclaimable",
"Final current value 1199 retained after repeated next",
"No source or snapshot Array remains live for Item payload",
"Last node/item released after dropping Cursor"
],
"gates": [
{
"command": "cargo test -p prometeu-bytecode -p prometeu-vm --lib --features prometeu-vm/resource-diagnostics --locked --offline",
"result": "passed",
"working_directory": "../runtime",
"passed": 308,
"ignored": 2,
"environment": {
"CARGO_TARGET_DIR": "/tmp/pln153-provider-final-target"
}
},
{
"command": "./gradlew -q test :prometeu-app:installedKnowledgeTest --console=plain",
"result": "passed",
"notes": "Full repository regression and installed gate; 13 existing skipped tests. Subsequent UI projection and positive construction additions also passed the focused gates below."
},
{
"command": "./gradlew -q :prometeu-compiler:frontends:prometeu-frontend-pbs:test :prometeu-lsp:prometeu-lsp-v1:test --console=plain",
"result": "passed",
"notes": "Final frontend/editor and LSP regression."
},
{
"command": "./gradlew -q :prometeu-architecture-tests:test --tests '*PbsListCursorTest' :prometeu-app:installedKnowledgeTest --console=plain",
"result": "passed",
"notes": "Final source corpus generation and both installed-knowledge tests."
},
{
"command": "python3 tools/pbx-conformance/run.py --runtime ../runtime",
"result": "passed",
"exit_code": 0,
"environment": {
"CARGO_TARGET_DIR": "/tmp/pln153-conformance-target"
},
"notes": "Full mandatory harness, including existing suites, with the final generated and installed List corpus."
},
{
"command": "python3 tools/runtime-catalog/verify.py --runtime ../runtime",
"result": "passed",
"notes": "Clean committed provider; both catalog hashes and actual provider exporter parity match."
}
],
"limitations": [
"Qualification applies to the recorded macOS/arm64 dev runtime profile; no release or cross-platform claim.",
"Two pre-existing full-harness response-schema gaps, LifecycleRole and NonFiniteValue, remain reported with original payloads; no new List/Cursor case is skipped.",
"Quota matrix intentionally lowers only GuestBytes, HandleSlots, AuxiliaryBytes and TemporaryBytes at each recorded limit; other resource ceilings are unchanged."
],
"workflow_cli_gap": "No create-lesson/next-id command. LSN-0090 was read from the framework model without mutation; discussion housekeep registers the lesson and updates the index/counter.",
"workflow_completion": {
"discussion_status": "done",
"plan_status_before_housekeeping": "done",
"lesson": "LSN-0090",
"housekeeping": "discussion housekeep DSC-0090 --lesson discussion/lessons/DSC-0090-pbs-sdk-list/LSN-0090-mutable-lists-and-stable-snapshot-cursors.md",
"removed_control_artifacts": 3,
"validation_after_housekeeping": "passed"
}
}
@@ -1,7 +1,7 @@
# Typed common Registry and PBX v0
Status: normative. Owner: `compiler/general`. Runtime contract reference:
`runtime` revision `f7d7c167531bb662a1af9d73fc326bb476bf8aa7`,
`runtime` revision `d9b9eeb171d2861ec660d4cf5703b0795eac49a9`,
`docs/specs/runtime/02c-program-registry-and-pbx-v0.md` and
`docs/vm-arch/ISA_CORE.md` in that repository.
@@ -186,3 +186,61 @@ Synthetic common IR additionally exercises linking, lowering, optimization and
emission without PBS. Frontend migration and integrated release readiness are
tracked separately in AGD75; passing common conformance does not claim complete
PBS lowering or complete Studio/runtime integration.
## Generic logical guards and atomic storage publication
The core includes the following generic operations, with u16 opcodes and
little-endian u32 immediates. They introduce no new Registry category, syscall,
source-language collection or ambient transaction. Operand order below is bottom
to top; all integer operands are Int64.
| Opcode | Immediate | Stack operands consumed |
| --- | --- | --- |
| INDEX_GUARD `0xA7` | none | index, exclusive limit |
| ARRAY_CHECK_LENGTH `0xA8` | Array TypeId | length |
| ARRAY_COPY `0xA9` | Array TypeId | source, sourceStart, target, targetStart, count |
| STORAGE_PUBLISH `0xAA` | Array TypeId, Struct TypeId, first field, second field | source, sourceStart, target, targetStart, count, fillStart, fillCount, element group, owner, first field group, second field group |
All operations return no values. INDEX_GUARD accepts exactly
`0 <= index < limit`, otherwise TRAP_OOB. It does not use physical storage length.
ARRAY_CHECK_LENGTH accepts zero through u32::MAX without allocating. Negative
lengths produce TRAP_OOB; values above u32::MAX produce the same resource
SizeOverflow failure as ARRAY_NEW. This check does not reserve future allocation.
ARRAY_COPY and STORAGE_PUBLISH require exact Array receiver identities and complete
stored groups. Every range must have nonnegative start/count and fit its physical
array; an empty range may start at length. Invalid ranges produce TRAP_OOB, never
clamping or truncation. Copy reads the pre-attempt source sequence, including for
overlapping source/target ranges. Presence tags, immutable boxes, native owners and
object identities are preserved without payload unpacking or repacking.
STORAGE_PUBLISH first copies the selected source range, then fills the selected
target range with the supplied element group, then writes the two selected fields
on the supplied Struct owner. Fields must be distinct valid indices on that Struct;
their types and widths come from the immutable Registry. Source code/producer owns
selection of every range, value, owner and field. The VM implements these generic
ordered effects and does not select collection algorithms.
Loading validates all immediate identities/categories/fields even in unreachable
code. Verification requires complete typed operands. ARRAY_COPY consumes five
slots; STORAGE_PUBLISH consumes `8 + elementWidth + firstWidth + secondWidth`.
INDEX_GUARD consumes two slots and ARRAY_CHECK_LENGTH consumes one.
Execution validates all receivers, lengths/ranges, supplied groups and source
groups, and reserves the complete charged copy scratch before writing. Validation,
quota or host allocation refusal leaves all input operands and destinations
unchanged, including both fields and native owners. Scratch uses TemporaryBytes,
is refunded on success/refusal and does not relax any resource ceiling. Existing
operands remain roots during preparation. There is no GC, callback, scheduler or
other interleaving inside this single instruction. Commit uses already validated
complete groups, performs no fallible allocation and consumes operands only after
all selected effects have been published. No prepared token escapes the attempt.
Base cycle costs are 2 for either guard, 4 for ARRAY_COPY and 6 for STORAGE_PUBLISH.
Copy adds `2 * count * elementWidth`; publication additionally adds
`fillCount * elementWidth + firstWidth + secondWidth`. Work arithmetic is checked
before mutation. A refused preparation still charges the attempted work. Neither
operation is a safepoint. Consumers must prepare any potentially failing return
carrier and all call operands before publication; a series of ordinary stores is
not a substitute for whole-operation safety.
@@ -0,0 +1,175 @@
# SDK List and snapshot Cursor
Owner: `compiler/pbs`. List and Cursor algorithms belong to the executable PBS
SDK. Logical guards, complete stored-group transfers and atomic publication are
language-neutral compiler/runtime mechanisms. This contract builds on
[arrays and optional](typed-arrays-and-optional-execution.md),
[generic structs](generic-structs-and-free-functions.md) and
[generic contracts](generic-contracts.md).
## Type, identity and construction
The SDK exports the ordinary generic struct `list<T>` and contract `Cursor<T>`
from its collections module. Applications are invariant. A directly optional
element, including through grouping, is invalid; `optional (list<T>)` is valid.
Nested valid arrays, structs, contracts, functions and value groups keep their
normal types and identities. An unrelated user declaration named list does not
acquire SDK behavior.
`new list<T>()` creates size zero and capacity 16. `new list<T>(capacity)` creates
size zero with exactly that capacity, including zero. Contextual `new list()`
and `new list(capacity)` require an expected concrete List type. Constructor
values do not infer T. Only the backing array is allocated; elements are absent.
List has stable reference identity. Assignment and parameter passing share that
identity; aliases observe mutations even when growth replaces its private backing
array. Storage, internal nodes and private compiler bindings are inaccessible to
clients. No public Dynamic or universal element type is introduced.
## Logical positions and operations
Every observable List satisfies `0 <= size <= capacity`. Size includes holes.
Capacity is physical storage and never makes additional indices accessible.
Zero and false are present values, distinct from holes.
| Operation | Result | Behavior |
| --- | --- | --- |
| `size()` | long | Logical positions, including holes |
| `capacity()` | long | Physical backing length |
| `xs[i]` | optional T | Present value or hole at `0 <= i < size` |
| `xs[i] = value` | unit | Replace one valid position without extending size |
| `add(value)` | unit | Append at size; do not fill earlier holes |
| `insert(i, value)` | unit | Insert at `0 <= i <= size`; shift slots and holes right |
| `clearAt(i)` | unit | Clear a valid position without shifting or changing size |
| `removeAt(i)` | optional T | Return old content, shift left, clear old tail, decrement size |
| `clear()` | unit | Clear all contents and size, preserve capacity |
| `resize(n)` | unit | Grow with holes or clear discarded suffix; set logical size |
| `reserve(n)` | unit | Grow exactly to n only when n exceeds capacity |
| `trim()` | unit | Set capacity exactly to size, including zero |
| `cursor()` | Cursor<T> | Complete immediate snapshot of present occurrences |
Removing a hole still removes its position and decrements size. Shrink, removal
and clear discard all hidden suffix references: regrowing must not resurrect old
values. Physical shrinking occurs only through trim. `resize(0)` preserves capacity.
Receiver and arguments are evaluated once in source order. Indices, lengths and
capacities accept int and long with localized lossless promotion; this does not
add general numeric conversion. No long value may be truncated.
Invalid logical indices raise TRAP_OOB, including indices inside physical capacity.
Negative lengths, structural overflow and quota refusals preserve the applicable
Array/provider fault classification. Failures are never translated into none.
## Capacity policy
When required size r exceeds capacity c, automatic growth is:
```text
candidate = max(r, c + max(1, ceil(c / 2)))
new capacity = min(Array structural maximum, candidate)
```
Calculate without overflow. Clamp only if r itself fits the structural maximum;
otherwise raise the applicable provider failure. Explicit reserve uses exactly n,
never shrinks and preserves size. Trim uses exactly size. Old and candidate arrays
may coexist; neither discarded references nor replaced storage promise immediate
GC or immediate quota recovery.
## Complete mutation attempts
Private storage transfer preserves whole existing groups, presence tags and
reference identities. Shifting must not unpack each public Array optional and
repack/rebox every payload. Prepare potentially failing validation, packing,
allocations, return carriers, scratch and publication operands before modifying
observable storage. Then publish the complete selected slot/range and field
changes without a fallible allocation, callback, scheduler or GC boundary.
Any refused attempt leaves size, backing reference, capacity, slots and roots
unchanged. This includes insert/remove without growth and preparation of
removeAt's optional result. Individually atomic Array instructions do not by
themselves establish this guarantee. Previously evaluated argument effects are
not rolled back. Unpublished candidates obey normal ownership and budgets.
## Cursor protocol
```pbs
declare contract Cursor<T> {
fn hasNext() -> bool;
fn next();
fn current() -> optional T;
}
```
The shared SDK implementation captures one private immutable singly linked,
non-circular node per present occurrence. It uses no snapshot array and retains
no source-slot or editable source-node association. List capture scans logical
positions and preserves increasing index order, duplicates and complete values.
Capture finishes before returning; failure exposes no partial Cursor.
A nonempty Cursor starts on its first node. `hasNext()` tests for a successor.
`next()` advances only when a successor exists; otherwise it does nothing.
`current()` reports the current node without advancing. Only a snapshot born
empty reports none. Repeated calls never advance beyond the last node.
| Snapshot and position | hasNext() | current() |
| --- | --- | --- |
| Empty, initially or after any next | false | none |
| [A], initially or after any next | false | some(A) |
| [A, B], initially | true | some(A) |
| [A, B], after one or any further next | false | some(B) |
Consume the current value before deciding whether to advance:
```pbs
while true {
if let value = cursor.current() {
// Consume value.
} else {
break;
}
if cursor.hasNext() {
cursor.next();
} else {
break;
}
}
```
A hasNext-only loop skips the last element. Waiting for current to become none
does not terminate a nonempty snapshot. There is no index, reset, removal, clone
or new for-in protocol. Another complete traversal creates another snapshot.
Captured object references preserve identity and observe subsequent field mutation.
Later List insertions, replacements, removals, clear, resize and backing changes
do not alter the captured sequence. Snapshot links and captured values are not
rewritten. The Cursor retains only its current node and suffix, allowing consumed
prefixes to become unreachable. It keeps the final node/value while reachable.
Other owners remain independent; reclamation happens through normal GC.
The outer optional describes node existence. A future producer with optional T
must distinguish `some(none)` from an empty Cursor. Type/resource faults remain
faults. List itself rejects directly optional elements.
## Reuse, costs and conformance
The shared implementation is independent of List representation. Future Linked<T>
and Set<T> producers use Cursor<T>; Map<K,V> uses Cursor<keyval<K,V>>. Set/Map gain
no ordering guarantee between captures. A captured keyval association is read-only
and does not follow later association replacement; referenced objects stay shared.
Those collections and keyval are separate deliveries.
For n logical List positions and k present occurrences, capture takes O(n) work
and O(k) additional nodes; next follows one link in O(1). Each node costs allocation,
ownership and eventual GC work. Do not promise lower memory than arrays, zero
allocation or allocation-free optional results. All complete groups remain rooted
through allocations and calls; provider quotas remain unchanged.
Conformance must execute real PBS through common IR and PBX in the production VM.
It covers every operation, constructor/type rule, logical bounds, exact growth,
holes and full groups, aliases, the entire Cursor table, snapshot independence,
shared object mutation, non-List producers, full-operation refusals with and without
growth, prefix reclamation, last-node retention, editor projections and installed
SDK/spec/example distribution. Generic publication also requires non-PBS tests.
Value search/removal, sorting, bulk APIs, new equality, public nodes, deep freezing,
general effect/transaction syntax and Linked/Set/Map implementations are out of scope.
@@ -81,7 +81,10 @@ Conformance must compile real PBS through typed IR and production PBX into the p
- [Dynamic semantics](../../../specs/compiler-languages/pbs/9.%20Dynamic%20Semantics%20Specification.md)
- [Existing type runtime alignment](input-and-existing-type-runtime-alignment.md)
List/Set/Map, growth, iteration, equality and hashing policies belong to the separate SDK collections discussion. A future list capacity does not create logical elements; fixed array length does.
[SDK List and Cursor](sdk-list-and-cursor.md) define logical size, growth and
snapshot traversal above private fixed-length arrays. List capacity does not
create logical elements; fixed array length does. Set/Map, equality and hashing
policies remain separate concerns.
See [Runtime Random integration](runtime-random-integration.md) for opaque generator aliases in typed storage and captures.
+66 -47
View File
@@ -2,7 +2,7 @@ Prometeu VM Runtime — Canonical Architecture
Status: canonical
This document is the authoritative architectural reference for the Prometeu VM runtime. It reflects the implementation as it exists today and defines the invariants that govern architectural changes in the VM layer.
This document is the authoritative architectural reference for the Prometeu VM runtime. It defines the invariants that govern architectural changes in the VM layer.
Scope boundary:
@@ -14,8 +14,8 @@ Scope boundary:
Document roles:
- This file is normative for VM/runtime architecture.
- Detailed domain specifications may live under `docs/runtime/specs/`, but they must not contradict this document where VM/runtime invariants are concerned.
- Roadmaps, agendas, and PR proposals may discuss future changes, but they are not authoritative until this document is updated.
- Detailed domain specifications may live under `docs/specs/runtime/`, but they must not contradict this document where VM/runtime invariants are concerned.
- Future architectural changes become authoritative when this document is updated.
- The machine-wide fantasy console framing lives in the runtime specs manual and related domain specs; those documents are complementary, not competing VM architecture sources.
Maintenance rule:
@@ -30,10 +30,10 @@ Maintenance rule:
- Operand stack + call frames; bytecode is fetched from a ROM/program image with a separate constant pool.
- GC‑managed heap
- Non‑compacting mark–sweep collector; stable object handles (`HeapRef`) while live. Sweep invalidates unreachable handles; objects are never moved.
- Closures (Model B)
- First‑class closures with a heap‑allocated environment. The closure object is passed to the callee as a hidden `arg0` when invoking a closure.
- Cooperative coroutines
- Deterministic, cooperative scheduling. Switching and GC occur only at explicit safepoints (`FRAME_SYNC`).
- Typed first-class Fn
- Structural logical parameter/result signatures and heap Closure environments retained by CallFrames, without hidden arg0.
- Single execution context
- VM directly owns PC, stacks and globals; no heap Coroutine or guest scheduler. GC opportunities are FRAME_SYNC and successful last-root RET after result rooting.
- Unified syscall ABI
- PBX pre-load artifacts declare canonical host bindings in `SYSC` and encode call sites as `HOSTCALL <sysc_index>`. The loader resolves and patches them to numeric `SYSCALL <id>` before verification/execution. Capability gating is enforced at load and checked again defensively at runtime. Syscalls are not first‑class values.
@@ -48,29 +48,28 @@ Maintenance rule:
- The VM’s current operand stack and frames are GC roots.
- Heap
- The heap stores runtime objects that require identity and reachability tracking. Handles are `HeapRef` indices into an internal object store.
- The heap stores runtime objects that require identity and reachability tracking. Handles are `HeapRef { index: u32, generation: u32 }` values into an internal object store. Every access checks the live generation; reclaimed maximum-generation slots are retired permanently.
- The collector is mark–sweep, non‑moving: it marks from roots, then reclaims unreachable objects without relocating survivors. Indices for live objects remain stable across collections.
2.2 Heap Object Kinds (as used today)
- Arrays of `Value`
- Variable‑length arrays whose elements may contain further `HeapRef`s.
- Typed arrays
- Variable-length arrays use the Registry element layout and defaults. Primitive/builtin values are stored directly; Struct/Array elements contain generational references. Native String/Dynamic slots retain their ownership rules. Stack/local execution continues to use `Value`.
- Closures
- Carry a function identifier and a captured environment (a slice/vector of `Value`s stored with the closure). Captured `HeapRef`s are traversed by the GC.
- Coroutines
- Heap‑resident coroutine records (state + wake time + suspended operand stack and call frames). These act as GC roots when suspended.
- Structs and Dynamic boxes
- Typed payloads use resolved reference/ownership maps. Private Struct cells can share mutable captured bindings.
Notes:
- Literals like strings and numbers are sourced from the constant pool in the program image; heap allocation is only used for runtime objects (closures, arrays, coroutine records, and any future heap kinds). The constant pool never embeds raw `HeapRef`s.
- Literals like strings and numbers are sourced from the constant pool in the program image; heap allocation is only used for runtime objects (closures, arrays, Structs and Dynamic boxes). The constant pool never embeds raw `HeapRef`s.
2.3 GC Roots
- VM roots
- Current operand stack and call frames of the running coroutine (or main context).
- Suspended coroutines
- All heap‑resident, suspended coroutine objects are treated as roots. Their saved stacks/frames are scanned during marking.
- Root traversal
- The VM exposes a root‑visitor that walks the operand stack, frames, and coroutine records to feed the collector. The collector then follows children from each object kind (e.g., array elements, closure environments, coroutine stacks).
- Roots comprise globals, all live operand values, Closure owners in every active
CallFrame and legally retained native/host roots. Inspection snapshots are not
guest roots. Tracing visits complete typed fields, arrays, captures and cells.
- Root results are transferred/rooted before the last frame is removed or GC runs,
and remain owned until consumed/replaced/reset/unloaded. Unreachable cycles collect.
3. Execution Model
@@ -85,20 +84,22 @@ Notes:
3.2 Safepoints
- `FRAME_SYNC` is the only safepoint.
- At `FRAME_SYNC`, the VM performs two actions in a well‑defined order:
1) Garbage‑collection opportunity: root enumeration + mark–sweep.
2) Scheduler handoff: the currently running coroutine may yield/sleep, and a next ready coroutine is selected deterministically.
- No other opcode constitutes a GC or scheduling safepoint. Syscalls do not implicitly trigger GC or rescheduling.
- Threshold GC opportunities are FRAME_SYNC and successful RET of the last root,
after result rooting. Ordinary callee RET, entry preparation, syscalls and
allocation/quota refusal do not collect. GC adds no guest callback, clock/frame
advance, budget grant or partial sweep on scratch failure.
3.3 Scheduler Behavior (Cooperative Coroutines)
3.3 Single Context and Host Continuation
- Coroutines are cooperative and scheduled deterministically (FIFO among ready coroutines).
- `YIELD` and `SLEEP` take effect at `FRAME_SYNC`:
- `YIELD` places the current coroutine at the end of the ready queue.
- `SLEEP` parks the current coroutine until its exact `wake_tick`, after which it re‑enters the ready queue at the correct point.
- `SPAWN` creates a new coroutine with its own stack/frames recorded in the heap and enqueues it deterministically.
- No preemption: the VM never interrupts a coroutine between safepoints.
- FRAME_SYNC may occur in any body, has no stack effect and resumes next PC with
live frames/locals/environments. It closes the logical frame but does not restart
entry. Budget exhaustion/breakpoints preserve the same open logical frame.
- Last-root RET completes entry. Recurring host prepares FunctionId 0 next frame,
preserving globals/init guard; preparation validates/reserves before replacing
previous owners and allocates no Coroutine. HALT/trap/reset/unload retain their
contracts. Render/asset workers remain independent of guest execution.
- SPAWN/YIELD/SLEEP are retired: numbers 0x54–0x56 stay reserved and all uses reject
at load, including unreachable code. No compatibility scheduler remains.
4. Verification Model
@@ -119,25 +120,33 @@ The verifier statically checks bytecode for structural safety and stack‑shape
- Syscalls
- The verifier runs only on the patched executable image. `HOSTCALL` is invalid at verification time. Final `SYSCALL` IDs must exist per `SyscallMeta`, and arity/declared return slot counts must match metadata.
- Closures
- `CALL_CLOSURE` is only allowed on closure values; the callee function must be known; argument counts for closure calls must match.
- Coroutines
- `YIELD` context must be valid; `SPAWN` argument counts are validated.
- Expected FnTypeId determines complete parameter/result groups even when the concrete FunctionId is unknown. Known mismatch rejects; Unknown retains runtime checks. Capture indices and target metadata are checked, and direct entry into capturing bodies rejects.
- Trait calls
- Exact method signatures and complete views resolve through immutable tables; concrete receiver is the first callee parameter, with no capture environment.
4.2 Runtime vs Verifier Guarantees
- The verifier guarantees structural correctness and stack‑shape invariants. It does not perform full type checking of value contents; dynamic checks (e.g., numeric domain checks, polymorphic comparisons, concrete syscall argument validation) occur at runtime and may trap.
- The Registry verifier checks structure and stack shape and conservatively tracks known types through control flow. It rejects proved incompatibilities; Unknown values retain runtime checks at typed boundaries. Numeric domain checks, polymorphic comparisons and concrete syscall argument validation may still trap at runtime.
- Capability gating for syscalls is enforced at load from cartridge capability flags and checked again at runtime by the VM/native interface.
5. Closures (Model B) — Calling Convention
-------------------------------------------
5. Typed Fn and Environments
----------------------------
- Creation
- `MAKE_CLOSURE` captures N values from the operand stack into a heap‑allocated environment alongside a function identifier. The opcode _pushes a `HeapRef` to the new closure.
- Call
- `CALL_CLOSURE` invokes a closure. The closure object itself is supplied to the callee as a hidden `arg0`. User‑visible arguments follow the function’s declared arity.
- Access to captures
- The callee can access captured values via the closure’s environment. Captured `HeapRef`s are traced by the GC.
- Fn identity is ordered logical parameter/result TypeIds; names, target and captures
are metadata. Pure Array/Fn structural cycles reject; nominal anchors stop expansion.
- MAKE_CLOSURE takes FunctionId/FnTypeId, captures full declared groups and pushes a
distinct one-slot instance. Immutable bindings copy values; mutable bindings can
share private typed Struct cells. Metadata lives in required PBX section 8.
- CALL_CLOSURE takes expected FnTypeId and retains the Closure in CallFrame, without
public arg0. GET_CAPTURE takes an index and copies a complete group. Direct calls
to capturing bodies reject; bound receivers use explicit adapter bodies.
- Fn is valid in Struct/Array/Dynamic, defaults Null and compares instance identity.
It gains no REF_* casts, Trait implementations, fault isolation or separate budget.
- Validate/reserve before consume/publication; faults preserve input owners and expose
no partial frame/object. Costs are 8+C / 6 / 2+W including traps, charged once.
- CALL_TRAIT takes TraitTypeId/method index, consumes explicit args and a complete
view, then passes concrete receiver first. Exact immutable-table dispatch costs 6.
6. Unified Syscall ABI
@@ -160,11 +169,11 @@ The verifier statically checks bytecode for structural safety and stack‑shape
- Collector
- Non‑moving mark–sweep.
- Triggers
- GC runs only at `FRAME_SYNC` safepoints.
- Threshold GC runs at FRAME_SYNC and successful last-root RET after result rooting.
- Liveness
- Roots comprise: the live VM stack/frames and all suspended coroutines. The collector traverses object‑specific children (array elements, closure environments, coroutine stacks).
- Globals, live operand values and active-frame Closures root complete typed object graphs, including cells and legally retained host/native owners.
- Determinism
- GC opportunities and scheduling order are tied to `FRAME_SYNC`, ensuring repeatable execution traces across runs with the same inputs.
- Opportunities depend on execution boundaries and thresholds, preserving deterministic guest execution with the same inputs.
8. Non‑Goals
@@ -180,6 +189,16 @@ The verifier statically checks bytecode for structural safety and stack‑shape
--------------------------
- Keep the public surface minimal and metadata‑driven (e.g., syscalls via `SyscallMeta`).
- Do not assume implicit safepoints; schedule and GC only at `FRAME_SYNC`.
- Preserve explicit GC opportunities and root results before root-return collection.
- When adding new opcodes or object kinds, extend the verifier and GC traversal accordingly (children enumeration, environment scanning, root sets).
- Update this document alongside any architectural change that affects runtime invariants.
## Program Registry
The [Program Registry contract](../specs/runtime/02c-program-registry-and-pbx-v0.md) defines immutable image-owned Type/Function metadata linked by dense IDs. Descriptors are native metadata outside guest GC; nominal identity does not depend on names. Stack/local execution retains Value, while typed storage uses resolved aligned layouts. PBX v0 is the sole target format; publication requires full bounded validation. Fn tag 4 and mandatory captures section 8 extend the Registry; no old-schema fallback is permitted.
`ValidatedProgramImage::load` publishes a shared image only after bounded decoding, native binding and Registry verification; `VirtualMachine::from_image` creates sessions that consume its typed signatures, constants and conversion instructions. Production and internal generators must use this sole sealed-v1 path. Typed Fn integration and legacy removal are implemented together; publication alone is not proof that every consumer has migrated. [Registry Boundaries and Owned Values](../../discussion/lessons/DSC-0058-program-registry-type-and-function-metadata/LSN-0059-registry-boundaries-and-owned-values.md) explains the published mechanisms. The [producer handoff](registry-producer-handoff.md) documents schema, input IDs and named-argument order, while [layout evidence](registry-layout-evidence.md) records reproducible load/access/GC measurements and the current publication boundary.
## VM-owned random core
See the [random contract](../specs/runtime/02e-vm-owned-random-core.md) for Random BuiltinId 0x0016, ObjectKind tag 9, intrinsics 0x4000–0x4003, atomic state mutation, bounded 4+8*N costs and rejection-limit trap 0x00000015. Random is managed by the VM heap and existing GC, without host entropy or PBX changes.
+71 -16
View File
@@ -12,7 +12,7 @@ Machine boundary:
Authority rule:
- This document is normative for bytecode-level encoding and opcode surface.
- Runtime-wide invariants still live in [`../../ARCHITECTURE.md`](../../ARCHITECTURE.md).
- Runtime-wide invariants still live in [`ARCHITECTURE.md`](ARCHITECTURE.md).
- If a bytecode-level rule here conflicts with runtime architecture, the conflict must be resolved explicitly in both documents; neither should drift silently.
#### Encoding Rules
@@ -51,6 +51,7 @@ Authority rule:
- Arithmetic:
- `ADD`, `SUB`, `MUL`, `DIV`, `MOD` — binary numeric ops.
- `NEG` — unary numeric negation.
- Integer arithmetic wraps at the resulting carrier width. For `DIV`, the signed minimum divided by `-1` returns that minimum; for `MOD`, the same operands return zero. Division or modulo by zero still traps. Mixed Int32/Int64 division promotes to Int64 before the operation.
- Comparison and logic:
- `EQ`, `NEQ`, `LT`, `LTE`, `GT`, `GTE` — comparisons → `[bool]`.
@@ -64,13 +65,13 @@ Authority rule:
- Functions and scopes:
- `CALL u32` — call by function index; argument/result arity per function metadata.
- `RET` — return from current function (block terminator).
- `MAKE_CLOSURE u32,u32` — create closure from `(fn_id, capture_count)`.
- `CALL_CLOSURE u32` — invoke closure with `arg_count` user arguments.
- `MAKE_CLOSURE u32,u32` — create typed closure from `(FunctionId, FnTypeId)`; cost 8+C.
- `CALL_CLOSURE u32` — invoke against expected FnTypeId; cost 6.
- `GET_CAPTURE u32` (0x57) — copy indexed complete capture group from current frame; cost 2+W.
- `CALL_TRAIT u32,u32` (0x58) — invoke TraitTypeId/method index through complete view; cost 6.
- Concurrency:
- `SPAWN u32,u32` — create coroutine for `(fn_id, arg_count)`.
- `YIELD` — request cooperative yield at the next safepoint.
- `SLEEP u32` — request suspension for a logical tick duration.
- Retired execution operations:
- 0x54–0x56 (SPAWN/YIELD/SLEEP) remain reserved and reject at load, including unreachable code. Diagnostic decoding may identify them; no execution or assembler emission is permitted.
- System/Timing:
- `HOSTCALL u32` — PBX pre-load host binding call by `SYSC` table index; the loader must resolve and rewrite it before verification or execution.
@@ -97,12 +98,28 @@ Example:
- `canonical_name` is the fully qualified intrinsic identity seen by compiler-side intrinsic pools.
- `arg_slots` and `ret_slots` are the real stack effect contract for verifier/lowering consumers.
- `arg_layout` and `ret_layout` use `|`-separated ABI atoms:
- `int`
- `float`
- `bool`
- `Int32`
- `Int64`
- `Float`
- `Bool`
- `String`
- `builtin:<builtin_name>`
- Rows must remain unique by both `(canonical_name, canonical_version)` and `final_id_hex` / `final_id_dec`.
- Rows must remain deterministically ordered by final ID.
- parameter_names uses ordered | separated names for flattened argument slots.
- cost_model is fixed or random_rejection_v1. cost_cycles is the full fixed
cost or the variable base. attempt_cost_cycles/max_attempts are zero for
fixed entries; random.range publishes base 4, attempt 8, maximum 64.
- Mathematical and random attempts charge upfront, including failure, without
an additional generic INTRINSIC cycle. Random draws retain attempt costs.
- mutates_guest_state distinguishes reproducible stateful calls from pure
computation. It is true for all random entries; may_allocate is true only
for random.new. Consumers must preserve stateful calls and source order.
- Both [INTRINSICS.csv](INTRINSICS.csv) and
[BUILTIN_CONSTANTS.csv](BUILTIN_CONSTANTS.csv) are generated from runtime
descriptors. Float constants publish bits rather than decimal approximations.
- See [numeric semantics and consumer examples](../specs/runtime/02d-gameplay-math-and-numeric-semantics.md)
for math/mathf, scalar IDs 0x3000–0x3019, numeric traps and the revised vec2 v1 contract.
For exact immediates and stack effects, see `CoreOpCode::spec()` which is the single source of truth used by the decoder, disassembler, and verifier.
@@ -110,7 +127,7 @@ For exact immediates and stack effects, see `CoreOpCode::spec()` which is the si
- The canonical decoder is `prometeu_bytecode::decode_next(pc, bytes)`.
- It uses the Core ISA spec to determine immediate size and the canonical `next_pc`.
- Unknown or legacy opcodes must produce a deterministic `UnknownOpcode` error.
- Unknown opcodes produce deterministic UnknownOpcode errors; recognized retired opcodes produce structured load rejection before execution.
#### Module Boundary
@@ -122,24 +139,62 @@ For exact immediates and stack effects, see `CoreOpCode::spec()` which is the si
#### Scope Notes
- "Core ISA" in the current repository means the canonical opcode surface implemented by the runtime today.
- It includes closures, coroutines, `HOSTCALL` patching semantics, `INTRINSIC`, and `FRAME_SYNC`.
- It includes typed closures, trait calls, `HOSTCALL` patching semantics, `INTRINSIC`, and `FRAME_SYNC`.
- It does not, by itself, define higher-level runtime policy such as crash taxonomy, firmware behavior, cartridge lifecycle, or host service organization. Those belong to the canonical runtime architecture and related specs.
#### FRAME_SYNC — Semantics and Placement (Bytecode Level)
- Semantics:
- `FRAME_SYNC` is a zero-operand instruction and does not modify the operand stack.
- It marks a VM safepoint for GC and the cooperative scheduler. In `CoreOpcodeSpec` this is exposed as `spec.is_safepoint == true`.
- On execution, the VM may suspend the current coroutine until the next frame boundary and/or perform GC. After resuming, execution continues at the next instruction.
- It marks a VM safepoint for threshold GC. In `CoreOpcodeSpec` this is exposed as `spec.is_safepoint == true`.
- Execution returns to the host at the logical frame boundary, preserving the single context. Resumption continues at the next instruction with live frames/locals/environments. Successful last-root RET is a separate GC opportunity after result rooting and completes that entry.
- Placement rules (representable and checkable):
- `FRAME_SYNC` may appear anywhere inside a function body where normal instructions can appear. It is NOT a block terminator (`spec.is_terminator == false`).
- Instruction boundaries are canonical: encoders/emitters must only place `FRAME_SYNC` at valid instruction PCs. The verifier already enforces “jump-to-boundary” and end-exclusive `[start, end)` function ranges using the canonical layout routine.
- Entrypoints that represent a render/update loop SHOULD ensure at least one reachable `FRAME_SYNC` along every long-running path to provide deterministic safepoints for GC/scheduling. This policy is semantic and may be enforced by higher-level tooling; at the bytecode level it is representable via `spec.is_safepoint` and can be counted by static analyzers.
- Entrypoints that represent a render/update loop SHOULD ensure at least one reachable `FRAME_SYNC` along every long-running path to provide deterministic safepoints for GC/frame handoff. This policy is semantic and may be enforced by higher-level tooling; at the bytecode level it is representable via `spec.is_safepoint` and can be counted by static analyzers.
- Disassembly:
- Disassemblers must print the mnemonic `FRAME_SYNC` verbatim for this opcode.
- Tools MAY optionally annotate it as a safepoint in comments, e.g., `FRAME_SYNC ; safepoint`.
- Verification notes:
- The bytecode verifier treats `FRAME_SYNC` as a normal instruction with no stack effect and no control-flow targets. It is permitted before `RET`, between basic blocks, and as the last instruction of a function. Jumps targeting the function end (`pc == end`) remain valid under the end-exclusive rule.
- The bytecode verifier treats `FRAME_SYNC` as a normal instruction with no stack effect and no control-flow targets. It is permitted before `RET` and between basic blocks; a reachable path must still terminate explicitly rather than fall through the function range. Jumps targeting the function end (`pc == end`) remain valid under the end-exclusive rule. A taken end branch completes the current call with the same result-height/type checks, frame release and root-return safepoint as `RET`. It consumes only the decoded branch's cycles, without an additional implicit `RET` charge, and never executes the neighboring function or range gap.
## Registry instruction encodings
The [Registry contract](../specs/runtime/02c-program-registry-and-pbx-v0.md#dynamic-and-reference-instructions)
defines opcodes 0x0090–0x0096. Tooling recognizes their u32 TypeId immediates:
```text
DYN_PACK 0
DYN_UNPACK 0
DYN_CAN_UNPACK 0
REF_IS_EXACT source=4, target=5
REF_IMPLEMENTS source=4, target=6
REF_TO_TRAIT source=4, target=6
REF_CAST source=6, target=4
```
Dynamic instructions occupy six bytes; reference instructions occupy ten bytes.
Static opcode metadata must represent Registry-dependent slot effects as unresolved,
never as zero. Resolve the semantic widths and immediate categories from the validated
Registry. Encoding/decoding support alone does not authorize execution: a slot-only
image/verifier must reject these instructions until typed-image integration is present.
#### Registry-backed collections
STRUCT_NEW (0x00A0), STRUCT_GET (0x00A1), STRUCT_SET (0x00A2), ARRAY_NEW (0x00A3), ARRAY_LEN (0x00A4), ARRAY_GET (0x00A5) and ARRAY_SET (0x00A6) require a validated Registry. Their exact immediates, full group effects, attempt costs and failure ordering are normative in the [typed collection instruction table](../specs/runtime/02-vm-instruction-set.md#typed-collection-instructions). These operations have no implicit conversion or safepoint. See the [producer handoff](collection-producer-handoff.md) for emission oracles.
## VM-owned random core
See the [random contract](../specs/runtime/02e-vm-owned-random-core.md) for Random BuiltinId 0x0016, ObjectKind tag 9, intrinsics 0x4000–0x4003, atomic state mutation, bounded 4+8*N costs and rejection-limit trap 0x00000015. Random is managed by the VM heap and existing GC, without host entropy or PBX changes.
## Logical guards and atomic storage publication
INDEX_GUARD (0xA7), ARRAY_CHECK_LENGTH (0xA8), ARRAY_COPY (0xA9) and
STORAGE_PUBLISH (0xAA) provide generic logical validation, complete stored-group
transfer and prepared atomic publication. Their encoding, typed operands, exact
faults, overlap semantics, costs and preflight/commit guarantees are defined by
the [canonical instruction contract](../specs/runtime/02-vm-instruction-set.md#logical-guards-and-atomic-storage-publication). No new Registry category, syscall, collection algorithm or safepoint is introduced.
+1 -1
View File
@@ -1,3 +1,3 @@
runtime.revision=f7d7c167531bb662a1af9d73fc326bb476bf8aa7
runtime.revision=d9b9eeb171d2861ec660d4cf5703b0795eac49a9
INTRINSICS.csv.sha256=0c1bb78a9b81d1e3480ef5713f3d70f0b06772a7dfab2fd00c1f0a6364f98daa
BUILTIN_CONSTANTS.csv.sha256=e407e4718127aa7ef7b1097b5b4b67d86dd979235e73a551a7c36b628cddc039
+2
View File
@@ -85,6 +85,8 @@ tasks.register<Test>("installedKnowledgeTest") {
val distribution = layout.buildDirectory.dir("install/prometeu-app")
inputs.dir(distribution)
systemProperty("studio.test.distribution", distribution.get().asFile.absolutePath)
systemProperty("pbs.registryConformanceDirectory",
project(":prometeu-compiler:prometeu-build-pipeline").layout.buildDirectory.dir("registry-conformance").get().asFile.absolutePath)
}
tasks.register<Test>("headlessProcessTest") {
@@ -47,6 +47,8 @@ class CompilerProjectKnowledgeTest {
assertTrue(result.libraries().getFirst().id().startsWith("sdk:pbs:"));
assertFalse(result.references().isEmpty());
assertTrue(result.references().stream().anyMatch(value -> value.id().equals("pbs:quickstart")));
assertTrue(result.references().stream().anyMatch(value -> value.id().equals("pbs:spec:sdk-list-and-cursor.md")));
assertTrue(result.references().stream().anyMatch(value -> value.id().equals("pbs:example:game:1:src/collections.pbs")));
assertTrue(language.referenceIds().contains("pbs:quickstart"));
assertFalse(result.limitations().stream().anyMatch(value -> value.contains("DOCUMENTATION_UNAVAILABLE")));
assertFalse(Files.exists(root.resolve(".workspace")));
@@ -27,6 +27,8 @@ public final class InstalledKnowledgeProbe {
check(docs.stream().anyMatch(r -> r.id().equals("pbs:example:game:1:src/contracts.pbs")), "Missing installed generic-contract example");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:spec:generic-structs-and-free-functions.md")), "Missing installed generic struct/function specification");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:example:game:1:src/generics.pbs")), "Missing installed generic struct/function example");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:spec:sdk-list-and-cursor.md")), "Missing installed List/Cursor specification");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:example:game:1:src/collections.pbs")), "Missing installed List/Cursor example");
for (var reference : docs) {
if (reference.id().startsWith("pbs:example:game:1:")) {
Path file = root.resolve(reference.id().substring("pbs:example:game:1:".length()));
@@ -58,6 +60,21 @@ public final class InstalledKnowledgeProbe {
BuilderPipelineContext.fromConfig(new BuilderPipelineConfig(false, root.toString())), LogAggregator.stdout());
check(result.diagnostics().stream().noneMatch(issue -> issue.isError()), "Example must compile without errors");
check(result.bytecodeBytes().length > 0, "Example must produce bytecode");
Path listRoot=Files.createDirectory(Path.of("list-example")).toRealPath();
Files.createDirectories(listRoot.resolve("src"));
Files.copy(root.resolve("prometeu.json"),listRoot.resolve("prometeu.json"));
Files.writeString(listRoot.resolve("src/mod.barrel"),"pub fn frame() -> void;");
Files.writeString(listRoot.resolve("src/main.pbs"),Files.readString(root.resolve("src/collections.pbs"))+"\n"+"""
[Frame] fn frame() -> void {
if collectionsExample()!=42 {let failure=1/0;}
}
""");
var listResult=BuilderPipelineService.INSTANCE.compile(
BuilderPipelineContext.fromConfig(new BuilderPipelineConfig(false,listRoot.toString())),LogAggregator.stdout());
check(listResult.diagnostics().stream().noneMatch(issue->issue.isError()),"Installed List/Cursor example must compile");
Path fixtures=Path.of(System.getProperty("pbs.registryConformanceDirectory"));
Files.createDirectories(fixtures);Files.write(fixtures.resolve("pbs-list-installed.pbx"),listResult.bytecodeBytes());
System.out.println("Installed List/Cursor execution fixture generated");
verifyExternalSources(provider.getClass().getClassLoader());
registration.unsubscribe();
System.out.println("Installed knowledge verified: " + docs.size() + " references; example compiled");
@@ -15,6 +15,7 @@ class InstalledKnowledgeTest {
String log = runProbe();
assertTrue(log.contains("references; example compiled"), log);
assertTrue(log.contains("Packaged external sources verified"), log);
assertTrue(log.contains("Installed List/Cursor execution fixture generated"), log);
}
@Test void installedCompilerAndSdkCarryTheSameVerifiedCatalog() throws Exception {
@@ -32,6 +33,7 @@ class InstalledKnowledgeTest {
String probeClasses = Path.of(InstalledKnowledgeProbe.class.getProtectionDomain().getCodeSource().getLocation().toURI()).toString();
Path output = outsideRepository.resolve("probe.log");
var command = new java.util.ArrayList<>(java.util.List.of(distribution.resolve("runtime/bin/java").toString(),
"-Dpbs.registryConformanceDirectory=" + System.getProperty("pbs.registryConformanceDirectory"),
"-cp", jars + java.io.File.pathSeparator + probeClasses, InstalledKnowledgeProbe.class.getName()));
command.addAll(java.util.List.of(arguments));
var process = new ProcessBuilder(command)
@@ -0,0 +1,376 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.models.*;
import p.studio.compiler.pbs.*;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.*;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
class PbsListCursorTest {
private static Path directory() { return Path.of(System.getProperty("pbs.registryConformanceDirectory")); }
static BytecodeModule compile(String name,String source) throws Exception {
var diagnostics=DiagnosticSink.empty();var id=new FileId(0);var sdkId=new FileId(1);
String sdk;
try(var stream=PbsListCursorTest.class.getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
assertNotNull(stream);sdk=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
var ast=PbsParser.parse(PbsLexer.lex(sdk+"\n"+source,id,diagnostics),id,diagnostics,PbsParser.ParseMode.INTERFACE_MODULE);
var file=new PbsFrontendCompiler().compileParsedFile(id,ast,diagnostics,source.contains("[BuiltinType")?SourceKind.SDK_INTERFACE:SourceKind.PROJECT,new ModuleId(0),
ReadOnlyList.empty(),p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),
p.studio.compiler.messages.FESurfaceContext.empty(),new p.studio.compiler.source.tables.NameTable());
assertFalse(diagnostics.hasErrors(),()->name+": "+diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()+" at "+d.getSpan().getStart()+":"+d.getSpan().getEnd()).toList());
var backend=IRBackend.builder().entryPointCallableName("Probe.main").entryPointModuleId(new ModuleId(0))
.executableFunctions(file.executableFunctions()).globals(file.globals()).registry(file.registry())
.intrinsicPool(file.intrinsicPool()).callableSignatures(file.callableSignatures()).build();
var program=new LowerToIRVMService().lower(backend);
var module=new BytecodeEmitter().emit(program.coherentEmissionPlan());
if(name.equals("gc"))module=PbsStructuredValuesTest.withTestSafepoint(module);
new BytecodePreloadVerifierService().verify(module);
Files.createDirectories(directory());Files.write(directory().resolve("pbs-list-"+name+".pbx"),module.serialize());
if(!name.equals("gc")&&!name.startsWith("refusal-")) {
var optimized=new OptimizeIRVMService().optimize(new LowerToIRVMService().lower(backend));
var optimizedModule=new BytecodeEmitter().emit(optimized.coherentEmissionPlan());
new BytecodePreloadVerifierService().verify(optimizedModule);
Files.write(directory().resolve("pbs-list-"+name+"-optimized.pbx"),optimizedModule.serialize());
}
return module;
}
@Test void defaultConstructionAndSourceAlgorithmsCompile() throws Exception {
compile("basic","""
declare service Probe { fn main() -> int {
let values = new list<int>();
values.add(20); values.add(22);
return (values[0] else 0) + (values[1] else 0);
} }
""");
}
private static void probe(String name,String body) throws Exception {
compile(name,"declare service Probe { fn main() -> int { "+body+" } }");
}
@Test void capacityPoliciesAndLogicalCleanup() throws Exception {
probe("construction","""
let values: list<int> = new list();
if (values.size() as int) != 0 { return 0; }
if (values.capacity() as int) != 16 { return 0; }
let maybe:optional (list<int>)=some(new list());
if let owner=maybe {if (owner.size() as int)!=0 {return 0;}} else {return 0;}
let capacity: int = 0;
let empty: list<int> = new list(capacity);
if (empty.capacity() as int) != 0 { return 0; }
empty.add(42);let first:long=0;return empty[first] else 0;
""");
probe("capacity","""
let values = new list<int>(0);
values.add(1); if (values.capacity() as int) != 1 { return 0; }
values.add(2); if (values.capacity() as int) != 2 { return 0; }
values.add(3); if (values.capacity() as int) != 3 { return 0; }
values.add(4); if (values.capacity() as int) != 5 { return 0; }
values.add(5); values.add(6);
if (values.capacity() as int) != 8 { return 0; }
values.reserve(9); if (values.capacity() as int) != 9 { return 0; }
values.reserve(0); if (values.capacity() as int) != 9 { return 0; }
values.resize(20); if (values.capacity() as int) != 20 { return 0; }
values.resize(2); values.trim();
if (values.capacity() as int) != 2 { return 0; }
values.clear(); if (values.capacity() as int) != 2 { return 0; }
values.trim(); if (values.capacity() as int) != 0 { return 0; }
return 42;
""");
probe("holes","""
let values = new list<int>(0);
values.resize(3); values[0] = 0; values[2] = 7;
if values[0].hasNone() { return 0; }
if values[1].hasSome() { return 0; }
values.add(9); values.insert(1, 42);
if (values.size() as int) != 5 { return 0; }
if values[2].hasSome() { return 0; }
if (values[3] else 0) != 7 { return 0; }
if values.removeAt(2).hasSome() { return 0; }
if (values[2] else 0) != 7 { return 0; }
let previous = values.removeAt(1);
values.clearAt(0); if values[0].hasSome() { return 0; }
if (values.size() as int) != 3 { return 0; }
values.resize(1); values.resize(3);
if values[1].hasSome() or values[2].hasSome() { return 0; }
values.clear(); values.resize(3);
if values[0].hasSome() or values[2].hasSome() { return 0; }
return previous else 0;
""");
probe("structural-growth","""
let maximum: long = 4294967295;
if _grownCapacity(maximum - 1, maximum) != maximum { return 0; }
if (_grownCapacity(0, 42) as int) != 42 { return 0; }
return 42;
""");
}
@Test void cursorStatesAndSnapshots() throws Exception {
probe("cursor-empty","""
let values = new list<int>(); values.resize(3);
let cursor: Cursor<int> = values.cursor();
values.add(42);
cursor.next(); cursor.next();
if cursor.hasNext() or cursor.current().hasSome() { return 0; }
return 42;
""");
probe("cursor-single","""
let values = new list<int>(); values.add(42);
let cursor = values.cursor();
if cursor.hasNext() { return 0; }
cursor.next(); cursor.next(); cursor.next();
if cursor.hasNext() { return 0; }
return cursor.current() else 0;
""");
probe("cursor-many","""
let values = new list<int>();
values.add(20); values.add(22);
let cursor = values.cursor();
if (cursor.current() else 0) != 20 { return 0; }
if (cursor.current() else 0) != 20 { return 0; }
if cursor.hasNext() == false { return 0; }
if cursor.hasNext() == false { return 0; }
cursor.next();
if cursor.hasNext() { return 0; }
if (cursor.current() else 0) != 22 { return 0; }
cursor.next(); cursor.next();
if (cursor.current() else 0) != 22 { return 0; }
return 42;
""");
probe("snapshot","""
let values = new list<int>(0); values.resize(4);
values[0]=0; values[2]=20; values[3]=22;
let cursor=values.cursor();
values[0]=9; values.insert(0,9); values.removeAt(1);
values.add(99); values.resize(1); values.clear(); values.trim();
let total:int=0; let count:int=0;
while true {
if let value=cursor.current() { total=total+value; count=count+1; }
else { break; }
if cursor.hasNext() {cursor.next();} else {break;}
}
if count!=3 {return 0;}
return total;
""");
probe("false-present","""
let values=new list<bool>();values.resize(2);values[1]=false;
let cursor=values.cursor();
if cursor.hasNext() {return 0;}
if let value=cursor.current() {if value==false {return 42;}}
return 0;
""");
probe("shared-optional","""
let empty:Cursor<optional int>=new _SnapshotCursor<optional int>(none);
if empty.current().hasSome() {return 0;}
let node=new _SnapshotNode<optional int>(none,none);
let cursor:Cursor<optional int>=new _SnapshotCursor<optional int>(some(node));
if cursor.current().hasNone() {return 0;}
if let payload=cursor.current() {if payload.hasSome() {return 0;}}
else {return 0;}
cursor.next();if cursor.current().hasNone() {return 0;}
return 42;
""");
}
@Test void aliasesAndCompletePayloads() throws Exception {
compile("identity","""
declare struct Item(pub mut value:int);
declare service Probe {fn main()->int {
let values=new list<Item>(0);let alias=values;
let item=new Item(20);let other=new Item(20);
values.add(item);alias.add(other);values.add(item);
if (alias.size() as int)!=3 {return 0;}
let cursor=values.cursor();values.clear();values.trim();item.value=22;
if let first=cursor.current() {if first!=item {return 0;}}
else {return 0;}
cursor.next();if let middle=cursor.current() {if middle==item {return 0;}}
else {return 0;}
cursor.next();if let last=cursor.current() {return last.value+other.value;}
return 0;
}}
""");
probe("tuple","""
let values=new list<(left:int,right:int)>(0);
values.add((left:20,right:22));values.insert(0,(left:1,right:2));
values.removeAt(0);let cursor=values.cursor();values.clear();
if let value=cursor.current() {return value.left+value.right;}
return 0;
""");
probe("nested-array","""
let payload:array<int>=[20];let values=new list<array<int>>();
values.add(payload);values.insert(0,payload);values.removeAt(0);
let cursor=values.cursor();values.clear();payload[0]=42;
if let value=cursor.current() {return value[0] else 0;}
return 0;
""");
compile("function","""
fn answer()->int {return 42;}
declare service Probe {fn main()->int {
let values=new list<Fn()->int>(0);values.add(answer);
values.insert(0,answer);values.removeAt(1);
let cursor=values.cursor();values.clear();
if let call=cursor.current() {return call();}return 0;
}}
""");
compile("evaluation","""
declare struct Effects(pub mut count:int,pub values:list<int>) {
fn receiver()->list<int> {this.count=this.count*10+1;return this.values;}
fn index()->int {this.count=this.count*10+2;return 0;}
fn value()->int {this.count=this.count*10+3;return 42;}
}
declare service Probe {fn main()->int {
let values=new list<int>();values.resize(1);
let effects=new Effects(0,values);
effects.receiver()[effects.index()]=effects.value();
if effects.count!=123 {return 0;}
effects.count=0;
effects.receiver().insert apply (value:effects.value(),index:effects.index());
if effects.count!=132 {return 0;}
let length:int=3;values.resize apply length;
let index:int=2;values.insert apply (index,1);
if (values.size() as int)!=4 {return 0;}
return values[0] else 0;
}}
""");
}
@Test void nativeAndContractGroupsSurviveTransfersAndCapture() throws Exception {
compile("vec2","""
[BuiltinType(name="vec2",version=1)]
declare builtin type Vector(pub x:float,pub y:float) {}
fn nativeProbe()->int {
let values=new list<Vector>(0);values.add(new Vector(20.0,22.0));
values.insert(0,new Vector(1.0,2.0));values.removeAt(0);
let cursor=values.cursor();values.clear();
if let value=cursor.current() {
let projected=(cursor.current() else new Vector(0.0,0.0)).x;
return (projected+value.y) as int;
}
return 0;
}
declare service Probe {fn main()->int {return nativeProbe();}}
""");
String color;
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/core/color/main.pbs")) {
assertNotNull(stream);color=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
compile("pixel",color+"""
[BuiltinType(name="pixel",version=1)]
declare builtin type Pixel(pub x:long,pub y:long,pub color:Color) {}
fn nativeProbe()->int {
let values=new list<Pixel>(0);values.add(new Pixel(20,22,RED));
values.insert(0,new Pixel(1,2,BLUE));values.removeAt(0);
let cursor=values.cursor();values.clear();
if let value=cursor.current() {
if value.color!=RED {return 0;}
return (value.x+value.y) as int;
}return 0;
}
declare service Probe {fn main()->int {return nativeProbe();}}
""");
compile("contract","""
declare contract Reader {fn read()->int;}
declare struct Item(pub value:int);
implements Reader for Item using item {fn read()->int {return item.value;}}
declare service Probe {fn main()->int {
let item=new Item(42);let values=new list<Reader>(0);
values.add(item as Reader);values.insert(0,item as Reader);values.removeAt(0);
let cursor=values.cursor();values.clear();
if let value=cursor.current() {return value.read();}return 0;
}}
""");
}
@Test void faultsPreserveProviderClassification() throws Exception {
probe("fault-read","let values=new list<int>(4);return values[0] else 0;");
probe("fault-write","let values=new list<int>(4);values[0]=42;return 0;");
probe("fault-negative","let values=new list<int>();values.add(1);return values[-1] else 0;");
probe("fault-insert","let values=new list<int>();values.insert(1,42);return 0;");
probe("fault-length","let values=new list<int>();values.resize(-1);return 0;");
probe("fault-overflow","let values=new list<int>();let length:long=4294967296;values.reserve(length);return 0;");
probe("fault-long-index","let values=new list<int>();values.add(42);let index:long=4294967296;return values[index] else 0;");
probe("fault-constructor","let values=new list<int>(-1);return 0;");
}
@Test void resourceRefusalFixturesExposeIndependentMutationBoundaries() throws Exception {
for(var operation:java.util.Map.of(
"insert","values.insert(12,42);",
"remove","let previous=values.removeAt(12);",
"growth","values.add(42);",
"cursor","let snapshot=values.cursor();").entrySet()) {
int capacity=operation.getKey().equals("growth")?128:256;
compile("refusal-"+operation.getKey(),"""
fn mutationMarker() {let marker:int=314160;}
fn completedMarker() {let marker:int=314161;}
fn performSecond(values:list<int>) {OPERATION}
fn perform(values:list<int>) {performSecond(values);}
declare service Probe {fn main()->int {
let values=new list<int>(CAPACITY);
values.resize(128);
for i:int from 0 until 128 {values[i]=i;}
mutationMarker();
perform(values);
completedMarker();return 42;
}}
""".replace("CAPACITY",Integer.toString(capacity)).replace("OPERATION",operation.getValue()));
}
}
@Test void snapshotPrefixAndStickyLastUseNormalSafepoints() throws Exception {
compile("gc","""
declare struct Item(pub value:int);
declare struct Garbage();
fn collect() {let marker:int=314159;}
fn makeCursor()->Cursor<Item> {
let values=new list<Item>(1200);
for i:int from 0 until 1200 {values.add(new Item(i));}
return values.cursor();
}
fn useCursor() {
let cursor=makeCursor();collect();
for i:int from 0 until 1199 {cursor.next();}
collect();
for i:int from 0 until 1200 {cursor.next();}
collect();
if let item=cursor.current() {
if item.value!=1199 {let failure=1/0;}
} else {let failure=1/0;}
}
declare service Probe {fn main()->int {
useCursor();
for i:int from 0 until 1200 {let discarded=new Garbage();}
collect();return 42;
}}
""");
}
@Test void sdkAliasesAndReexportsExecuteThroughTheWorkspace(@org.junit.jupiter.api.io.TempDir Path root) throws Exception {
Files.createDirectories(root.resolve("src/facade"));
Files.writeString(root.resolve("prometeu.json"),"""
{"name":"app","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game","dependencies":[]}
""");
Files.writeString(root.resolve("src/mod.barrel"),"pub fn frame() -> void;");
Files.writeString(root.resolve("src/facade/mod.barrel"),"pub struct Items<T>; pub contract Walk<T>;");
Files.writeString(root.resolve("src/facade/main.pbs"),"import {list as Items,Cursor as Walk} from @sdk:collections;");
Files.writeString(root.resolve("src/main.pbs"),"""
import {Items,Walk} from @app:facade;
[Frame] fn frame() -> void {
let values:Items<int>=new Items(0);values.add(20);values.add(22);
let cursor:Walk<int>=values.cursor();values.clear();
let total:int=0;
while true {
if let value=cursor.current() {total=total+value;} else {break;}
if cursor.hasNext() {cursor.next();} else {break;}
}
if total!=42 {let failure=1/0;}
}
""");
var result=p.studio.compiler.workspaces.BuilderPipelineService.INSTANCE.compile(
p.studio.compiler.models.BuilderPipelineContext.fromConfig(new p.studio.compiler.messages.BuilderPipelineConfig(false,root.toString())),
p.studio.utilities.logs.LogAggregator.stdout());
assertFalse(result.diagnostics().stream().anyMatch(d->d.isError()),result.diagnostics().toString());
assertTrue(result.bytecodeBytes().length>0);
Files.createDirectories(directory());Files.write(directory().resolve("pbs-list-imports.pbx"),result.bytecodeBytes());
}
}
@@ -237,6 +237,10 @@ public final class PbsGenericInstantiation {
error("ARITY", name(template)+" requires "+parameters(template).size()+" explicit type argument(s)", site);
return null;
}
if (PbsSdkCollections.isList(template) && PbsSdkCollections.directOptional(arguments.getFirst())) {
error("LIST_OPTIONAL_ELEMENT", "SDK list<T> does not permit a directly optional element", site);
return null;
}
var application = PbsAst.TypeRef.applied(name(template), arguments, template.span());
var applicationKey = template instanceof PbsAst.FunctionDecl ? PbsContractTypes.nominal(template)+"["+
arguments.stream().map(types::key).map(k -> k.length()+":"+k).collect(Collectors.joining())+"]" : types.key(application);
@@ -357,6 +361,7 @@ public final class PbsGenericInstantiation {
current = probe.concrete.values().stream().flatMap(Collection::stream).toList();
}
var supplemental = new ArrayList<PbsAst.TopDecl>(witnesses);
if(request!=null)probe.addShapeImplementations(request,supplemental);
var visible = new ArrayList<PbsAst.TopDecl>(source.visible().asList());
visible.addAll(source.ast().topDecls().asList());
var shapeKeys = new HashSet<String>();
@@ -374,11 +379,12 @@ public final class PbsGenericInstantiation {
while (!probe.pending.isEmpty()) {
var dependency = probe.pending.removeFirst();
if (dependency == request) continue;
var fullShape = dependency.template() instanceof PbsAst.FunctionDecl || probe.requiredShapes.contains(dependency.name());
var fullShape = dependency.template() instanceof PbsAst.FunctionDecl || dependency.template() instanceof PbsAst.ContractDecl || probe.requiredShapes.contains(dependency.name());
var shape = fullShape ? probe.concreteDeclaration(dependency, probe.shapeRewriter(
probe.environment(dependency.template(), dependency.arguments()), PbsAst.TypeRef.simple(dependency.name(), dependency.template().span())))
: probe.anchors.get(dependency.name());
if (shape != null) { supplemental.add(shape); probe.types.index(shape); }
probe.addShapeImplementations(dependency,supplemental);
}
for (var bindings : probe.argumentBindings.values()) for (var binding : bindings.entrySet())
supplemental.add(new PbsAst.TypeBindingDecl(binding.getKey(), binding.getValue(), template.span()));
@@ -394,6 +400,20 @@ public final class PbsGenericInstantiation {
}
}
private void addShapeImplementations(Request request,List<PbsAst.TopDecl> output) {
if(!(request.template() instanceof PbsAst.StructDecl template))return;
for(var rule:rules)if(!rule.typeParameters().isEmpty() && types.declaration(rule.ownerType())==template) {
var bindings=new LinkedHashMap<String,PbsAst.TypeRef>();
for(int i=0;i<rule.ownerType().arguments().size();i++)
bindings.put(rule.ownerType().arguments().get(i).name(),request.arguments().get(i));
var owner=PbsAst.TypeRef.simple(request.name(),template.span());
var rewrite=shapeRewriter(bindings,owner);
rewrite.implicitLocals=Map.of(rule.binderName(),owner);
output.add(new PbsAst.ImplementsDecl(ReadOnlyList.empty(),rewrite.type(rule.contractType()),owner,
rule.binderName(),PbsAstRewriter.map(rule.methods(),rewrite::function),rule.span()));
}
}
private ConcreteRewriter shapeRewriter(Map<String, PbsAst.TypeRef> bindings, PbsAst.TypeRef owner) {
return new ConcreteRewriter(bindings, owner, List.of()) {
@Override public PbsAst.FunctionDecl function(PbsAst.FunctionDecl f) {
@@ -496,6 +516,21 @@ public final class PbsGenericInstantiation {
locals.put(l.name(), expected == null ? infer(value) : expected);
return new PbsAst.LetStatement(l.isConst(), l.name(), l.nameSpan(), expected, value, l.span());
}
if(s instanceof PbsAst.IfStatement i && i.bindingName()!=null) {
var condition=expression(i.condition());
var optional=infer(condition);
var previous=locals;locals=new HashMap<>(locals);
PbsAst.Block thenBlock;
try {
if(optional!=null && optional.kind()==PbsAst.TypeRefKind.OPTIONAL) {
locals.put(i.bindingName(),optional.inner());
if(symbolicProbe && optional.inner().name()!=null)requiredShapes.add(optional.inner().name());
}
thenBlock=block(i.thenBlock());
} finally {locals=previous;}
return new PbsAst.IfStatement(condition,thenBlock,
i.elseIf()==null?null:(PbsAst.IfStatement)statement(i.elseIf()),block(i.elseBlock()),i.span(),i.bindingName(),i.bindingSpan());
}
if (s instanceof PbsAst.ReturnStatement r) return new PbsAst.ReturnStatement(expression(r.value(), result), r.span());
if (s instanceof PbsAst.AssignStatement a) {
var target = a.target().expression();
@@ -596,11 +631,12 @@ public final class PbsGenericInstantiation {
}
var concreteType = type(applied);
if (symbolicProbe) requiredShapes.add(concreteType.name());
var fields = constructorParameters(concreteType, n.ctorName());
var constructor = PbsSdkCollections.isList(s) && n.ctorName() == null ? "make" : n.ctorName();
var fields = constructorParameters(concreteType, constructor, n.arguments().size());
var arguments = new ArrayList<PbsAst.Expression>();
for (int i = 0; i < n.arguments().size(); i++) arguments.add(expression(n.arguments().get(i),
fields.size() == n.arguments().size() ? fields.get(i).typeRef() : null));
return new PbsAst.NewExpr(concreteType.name(), n.ctorName(), ReadOnlyList.wrap(arguments), null, false, n.span());
return new PbsAst.NewExpr(concreteType.name(), constructor, ReadOnlyList.wrap(arguments), null, false, n.span());
}
}
return super.expression(e, expected);
@@ -611,6 +647,10 @@ public final class PbsGenericInstantiation {
case PbsAst.IdentifierExpr i -> locals.get(i.name());
case PbsAst.ThisExpr t -> self;
case PbsAst.NewExpr n -> n.explicitType() != null ? n.explicitType() : PbsAst.TypeRef.simple(n.typeName(), n.span());
case PbsAst.IndexExpr i -> {
var receiver=infer(i.receiver());
yield receiver!=null && receiver.kind()==PbsAst.TypeRefKind.ARRAY ? PbsAst.TypeRef.optional(receiver.inner(),i.span()) : null;
}
case PbsAst.GroupExpr g -> infer(g.expression());
case PbsAst.CallExpr c -> { var callables = callableSignatures(c.callee()); yield callables.size() == 1 ? callables.getFirst().returnType() : null; }
case PbsAst.MemberExpr m -> {
@@ -669,14 +709,14 @@ public final class PbsGenericInstantiation {
private PbsAst.FunctionSignature signature(PbsAst.FunctionDecl f) {
return new PbsAst.FunctionSignature(f.name(), f.parameters(), f.returnKind(), f.returnType(), f.resultErrorType(), f.attributes(), f.span());
}
private ReadOnlyList<PbsAst.Parameter> constructorParameters(PbsAst.TypeRef owner, String constructor) {
private ReadOnlyList<PbsAst.Parameter> constructorParameters(PbsAst.TypeRef owner, String constructor, int arity) {
var d = types.declaration(owner);
var origin = PbsContractTypes.applicationOrigin(d);
if (origin != null) d = origin.declaration();
if (!(d instanceof PbsAst.StructDecl s)) return ReadOnlyList.empty();
var rewrite = origin == null ? this : new ConcreteRewriter(environment(s, origin.arguments()), owner, chain);
if (constructor == null) return map(s.fields(), f -> new PbsAst.Parameter(f.name(), rewrite.type(f.typeRef()), f.span()));
var candidates = s.ctors().stream().filter(c -> c.name().equals(constructor)).toList();
var candidates = s.ctors().stream().filter(c -> c.name().equals(constructor) && c.parameters().size() == arity).toList();
return candidates.size() == 1 ? map(candidates.getFirst().parameters(), rewrite::parameter) : ReadOnlyList.empty();
}
}
@@ -93,6 +93,9 @@ final class PbsGenericTemplateValidator {
} else if (s instanceof PbsAst.ReturnStatement r) checkCompatible(infer(r.value(), result), result, r.span());
else if (s instanceof PbsAst.AssignStatement a) {
var target = a.target().expression() == null ? locals.get(a.target().rootName()) : infer(a.target().expression(), null);
// Indexed writes accept the element, while indexed reads are optional.
if(a.target().expression() instanceof PbsAst.IndexExpr && target!=null && target.kind()==PbsAst.TypeRefKind.OPTIONAL)
target=target.inner();
checkCompatible(infer(a.value(), target), target, a.span());
if (a.operator() != PbsAst.AssignOperator.ASSIGN && symbolic(target)) capability(a.span());
}
@@ -150,8 +153,9 @@ final class PbsGenericTemplateValidator {
context.error("CONTEXT", "Generic construction requires an expected application or explicit type arguments", n.span());
if (d instanceof PbsAst.StructDecl s) {
var env = bindings(s, result);
var expectedParameters = n.ctorName() == null ? map(s.fields(), f -> new PbsAst.Parameter(f.name(), PbsTypeSubstitution.substitute(f.typeRef(), env), f.span()))
: s.ctors().stream().filter(c -> c.name().equals(n.ctorName())).findFirst().map(c ->
var constructor = PbsSdkCollections.isList(s) && n.ctorName()==null ? "make" : n.ctorName();
var expectedParameters = constructor == null ? map(s.fields(), f -> new PbsAst.Parameter(f.name(), PbsTypeSubstitution.substitute(f.typeRef(), env), f.span()))
: s.ctors().stream().filter(c -> c.name().equals(constructor) && c.parameters().size()==n.arguments().size()).findFirst().map(c ->
map(c.parameters(), p -> new PbsAst.Parameter(p.name(), PbsTypeSubstitution.substitute(p.typeRef(), env), p.span()))).orElse(ReadOnlyList.empty());
checkArguments(n.arguments(), List.of(new Callable(expectedParameters, result)), n.span());
}
@@ -0,0 +1,26 @@
package p.studio.compiler.pbs;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.utilities.structures.ReadOnlyList;
/** Reserved SDK bindings retain parser-established origin through specialization. */
public final class PbsSdkCollections {
private PbsSdkCollections() {}
public static boolean isList(PbsAst.TopDecl declaration) {
return declaration instanceof PbsAst.StructDecl s && attribute(s.attributes(), "SdkList") != null;
}
public static PbsAst.Attribute attribute(ReadOnlyList<PbsAst.Attribute> attributes, String name) {
return attributes.stream().filter(a -> a.trustedSdkOrigin() && a.name().equals(name)).findFirst().orElse(null);
}
public static String argument(PbsAst.Attribute attribute, String name) {
var value=attribute.arguments().stream().filter(a -> a.name().equals(name))
.map(PbsAst.AttributeArgument::value).findFirst().orElse(null);
if (!(value instanceof PbsAst.AttributeStringValue s)) throw new IllegalArgumentException("Missing SDK binding argument: "+name);
var text=s.value();
return text.startsWith("\"") ? text.substring(1,text.length()-1) : text;
}
public static boolean directOptional(PbsAst.TypeRef type) {
while(type!=null && type.kind()==PbsAst.TypeRefKind.GROUP)type=type.inner();
return type!=null && type.kind()==PbsAst.TypeRefKind.OPTIONAL;
}
}
@@ -70,7 +70,11 @@ public final class PbsAst {
public record Attribute(
String name,
ReadOnlyList<AttributeArgument> arguments,
Span span) {
Span span,
boolean trustedSdkOrigin) {
public Attribute(String name, ReadOnlyList<AttributeArgument> arguments, Span span) {
this(name,arguments,span,false);
}
}
public record AttributeArgument(
@@ -60,7 +60,11 @@ final class PbsExecutableBodyLowerer {
}
var binding = functionDecl.attributes().stream().filter(a -> "IntrinsicCall".equals(a.name())).findFirst();
final boolean terminated;
if (binding.isPresent()) {
var storageBinding = p.studio.compiler.pbs.PbsSdkCollections.attribute(functionDecl.attributes(), "StoragePrimitive");
if (storageBinding != null) {
lowerStoragePrimitive(functionDecl,storageBinding,context);
terminated = true;
} else if (binding.isPresent()) {
var attribute = binding.get();
var name = attribute.arguments().stream().filter(a -> "name".equals(a.name()))
.map(a -> ((PbsAst.AttributeStringValue) a.value()).value()).findFirst().orElseThrow();
@@ -664,6 +668,19 @@ final class PbsExecutableBodyLowerer {
offset+=width;
}
}
var nativeFields=receiverType==null?null:context.types().fields.get(receiverType.name());
if(nativeFields!=null) {
lowerExpected(memberExpr.receiver(),receiverType,context);
int slot=save(context.types().slots(receiverType),memberExpr.span(),context),offset=0;
for(var field:nativeFields) {
int width=context.types().slots(field.typeRef());
if(field.name().equals(memberExpr.memberName())) {
emitLoadLocalSlots(slot+offset,width,memberExpr.span(),context);return;
}
offset+=width;
}
reportUnsupportedLowering("Unknown native field",memberExpr.span(),context);return;
}
var struct = receiverType == null ? null : context.types().structs.get(receiverType.name());
if (struct != null) {
for (int i=0;i<struct.fields().size();i++) if (struct.fields().get(i).name().equals(memberExpr.memberName())) {
@@ -1066,6 +1083,7 @@ final class PbsExecutableBodyLowerer {
if (!(argument instanceof PbsAst.TupleExpr tuple) || tuple.items().isEmpty() || tuple.items().getFirst().label() == null) {
lowerCallExpression(call, c); return;
}
if(lowerNamedStructApply(apply,tuple,c))return;
var signature = c.types().signatures.get(PbsRuntimeTypes.path(call.callee())+"#"+call.arguments().size());
if(signature == null) { reportUnsupportedLowering("Named application requires a resolved signature", apply.span(), c); return; }
var slots = new java.util.HashMap<String,Integer>();
@@ -1087,6 +1105,41 @@ final class PbsExecutableBodyLowerer {
callsiteEmitter.emitCallsite(call,c);
}
private boolean lowerNamedStructApply(PbsAst.ApplyExpr apply,PbsAst.TupleExpr tuple,PbsExecutableLoweringContext c) {
if(!(apply.callee() instanceof PbsAst.MemberExpr member))return false;
var owner=PbsRuntimeTypes.unwrap(expressionType(member.receiver(),c));
var declaration=owner==null?null:c.types().structs.get(owner.name());
if(declaration==null)return false;
var methods=declaration.methods().stream().filter(m->m.name().equals(member.memberName())&&m.parameters().size()==tuple.items().size()).toList();
if(methods.size()!=1)return false;
var method=methods.getFirst();
boolean listIndex=p.studio.compiler.pbs.PbsSdkCollections.isList(declaration)&&java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName());
var receiverName=c.nextLabel("named_receiver");
lowerExpected(member.receiver(),owner,c);
int receiver=c.declareLocalSlots(receiverName,1);c.localTypes.put(receiverName,owner);
emitStoreLocalSlots(receiver,1,member.span(),c);
var values=new java.util.HashMap<String,String>();
for(var item:tuple.items()) {
int ordinal=-1;
for(int i=0;i<method.parameters().size();i++)if(method.parameters().get(i).name().equals(item.label()))ordinal=i;
if(ordinal<0||values.containsKey(item.label())){reportUnsupportedLowering("Unknown or duplicate named argument",item.span(),c);return true;}
var type=PbsTypeSubstitution.replaceSelf(method.parameters().get(ordinal).typeRef(),owner.name());
if(listIndex&&ordinal==0)lowerArrayIndex(item.expression(),c);else lowerExpected(item.expression(),type,c);
var name=c.nextLabel("named_argument");int slot=c.declareLocalSlots(name,c.types().slots(type));
c.localTypes.put(name,type);emitStoreLocalSlots(slot,c.types().slots(type),item.span(),c);values.put(item.label(),name);
}
var arguments=new java.util.ArrayList<PbsAst.Expression>();
for(var parameter:method.parameters()) {
var name=values.get(parameter.name());
if(name==null){reportUnsupportedLowering("Missing named argument",apply.span(),c);return true;}
arguments.add(new PbsAst.IdentifierExpr(name,apply.span()));
}
var callee=new PbsAst.MemberExpr(new PbsAst.IdentifierExpr(receiverName,member.span()),member.memberName(),member.span());
if(!lowerStructuredCall(new PbsAst.CallExpr(callee,ReadOnlyList.wrap(arguments),apply.span()),c))
reportUnsupportedLowering("Named method requires its exact signature",apply.span(),c);
return true;
}
private void lowerExpected(PbsAst.Expression expression, PbsAst.TypeRef expected, PbsExecutableLoweringContext context) {
var previous = context.expectedType;
context.expectedType = expected;
@@ -1109,7 +1162,14 @@ final class PbsExecutableBodyLowerer {
if (e instanceof PbsAst.ThisExpr) return c.localTypes.getOrDefault("this", c.serviceOwner == null ? null : PbsRuntimeTypes.type(c.serviceOwner));
if (e instanceof PbsAst.SomeExpr some) return PbsAst.TypeRef.optional(expressionType(some.value(), c), some.span());
if (e instanceof PbsAst.ElseExpr fallback) {var opt = PbsRuntimeTypes.unwrap(expressionType(fallback.optionalExpression(), c)); return opt == null ? null : opt.inner();}
if (e instanceof PbsAst.IndexExpr index) {var array = PbsRuntimeTypes.unwrap(expressionType(index.receiver(), c)); return array == null ? null : PbsAst.TypeRef.optional(array.inner(), index.span());}
if (e instanceof PbsAst.IndexExpr index) {
var array = PbsRuntimeTypes.unwrap(expressionType(index.receiver(), c));
if(array==null)return null;
var declaration=c.types().structs.get(array.name());
return p.studio.compiler.pbs.PbsSdkCollections.isList(declaration)
? declaration.methods().stream().filter(m->m.name().equals("_at")).findFirst().orElseThrow().returnType()
: PbsAst.TypeRef.optional(array.inner(),index.span());
}
if (e instanceof PbsAst.MemberExpr member) {
var serviceType = expressionType(member.receiver(), c);
var service = serviceType == null ? null : c.types().services.get(serviceType.name());
@@ -1123,6 +1183,8 @@ final class PbsExecutableBodyLowerer {
for (var field : receiver.fields()) if (field.label().equals(member.memberName())) return field.typeRef();
var struct = receiver == null ? null : c.types().structs.get(receiver.name());
if (struct != null) for (var field : struct.fields()) if (field.name().equals(member.memberName())) return field.typeRef();
var builtinFields=receiver==null?null:c.types().fields.get(receiver.name());
if(builtinFields!=null)for(var field:builtinFields)if(field.name().equals(member.memberName()))return field.typeRef();
}
if (e instanceof PbsAst.IdentifierExpr i) {
if(c.types().services.containsKey(i.name()))return PbsRuntimeTypes.type(i.name());
@@ -1551,6 +1613,78 @@ final class PbsExecutableBodyLowerer {
span);
}
private void lowerStoragePrimitive(PbsAst.FunctionDecl function, PbsAst.Attribute binding, PbsExecutableLoweringContext c) {
var operation=p.studio.compiler.pbs.PbsSdkCollections.argument(binding,"name");
var span=function.span();
switch(operation) {
case "indexGuard" -> {
emitLoadLocalSlots(0,2,span,c);
registry(INDEX_GUARD,null,0,2,0,span,c);
}
case "checkLength" -> {
emitGetLocal(0,span,c);
registry(ARRAY_CHECK_LENGTH,c.types().arrayStorage(),0,1,0,span,c);
}
case "copy" -> {
emitLoadLocalSlots(0,5,span,c);
registry(ARRAY_COPY,c.types().arrayStorage(),0,5,0,span,c);
}
case "publish" -> {
var parameters=function.parameters();
var optional=PbsRuntimeTypes.unwrap(parameters.get(7).typeRef());
var payload=optional.inner();
var owner=parameters.get(8).typeRef();
var declaration=c.types().structs.get(owner.name());
var firstName=p.studio.compiler.pbs.PbsSdkCollections.argument(binding,"first");
var secondName=p.studio.compiler.pbs.PbsSdkCollections.argument(binding,"second");
int first=-1,second=-1;
for(int i=0;i<declaration.fields().size();i++) {
if(declaration.fields().get(i).name().equals(firstName))first=i;
if(declaration.fields().get(i).name().equals(secondName))second=i;
}
if(first<0||second<0)throw new IllegalArgumentException("Unresolved SDK storage fields");
emitLoadLocalSlots(0,7,span,c);
optionalPresence(7,optional,span,c);
var absent=c.nextLabel("empty_storage_fill");var ready=c.nextLabel("storage_fill_ready");
emitJump(IRBackendExecutableFunction.InstructionKind.JMP_IF_FALSE,absent,span,c);
unwrapOptional(7,optional,span,c);
packStorage(payload,span,c);
registry(DYN_PACK,c.types().registryType(payload),0,storageWidth(payload,c),2,span,c);
emitJump(IRBackendExecutableFunction.InstructionKind.JMP,ready,span,c);
emitLabel(absent,span,c);emitDefault(c.types().dynamic(),2,span,c);
emitLabel(ready,span,c);
int offset=8,pops=9;
for(int i=8;i<parameters.size();i++) {
var type=parameters.get(i).typeRef();int width=c.types().slots(type);
emitLoadLocalSlots(offset,width,span,c);packStorage(type,span,c);
offset+=width;pops+=storageWidth(type,c);
}
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.REGISTRY,
null,"",null,null,null,"","",pops,0,span,
new IRRegistryInstruction(STORAGE_PUBLISH,c.types().arrayStorage(),c.types().registryType(owner),null,first,second)));
}
default -> throw new IllegalArgumentException("Unknown private storage binding: "+operation);
}
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.RET,"",null,null,span));
}
private boolean lowerListIndex(PbsAst.IndexExpr index, PbsAst.Expression value, PbsAst.TypeRef type, PbsExecutableLoweringContext c) {
var declaration=type==null?null:c.types().structs.get(type.name());
if(!p.studio.compiler.pbs.PbsSdkCollections.isList(declaration))return false;
var name=value==null?"_at":"_put";
var method=declaration.methods().stream().filter(m->m.name().equals(name)).findFirst().orElseThrow();
lowerExpected(index.receiver(),type,c);
lowerArrayIndex(index.index(),c);
int pops=2;
if(value!=null) {
var element=method.parameters().get(1).typeRef();
lowerExpected(value,element,c);pops+=c.types().slots(element);
}
emitStructuredFunction(type.name()+"."+name,receiverParameters(type.name(),method.parameters()),
method.returnType(),pops,c.types().slots(method.returnType()),index.span(),c);
return true;
}
private void registry(IRRegistryInstruction.Kind kind, IRRegistry.TypeRef type, int field, int pops, int pushes, Span span, PbsExecutableLoweringContext c) {
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.REGISTRY,
null, "", null, null, null, "", "", pops, pushes, span,
@@ -1589,6 +1723,7 @@ final class PbsExecutableBodyLowerer {
}
private void lowerArrayWrite(PbsAst.IndexExpr index,PbsAst.Expression value,PbsExecutableLoweringContext c) {
var type=PbsRuntimeTypes.unwrap(expressionType(index.receiver(),c));
if(lowerListIndex(index,value,type,c))return;
lowerExpected(index.receiver(),type,c);
lowerArrayIndex(index.index(),c);
lowerExpected(value,type.inner(),c);
@@ -1598,6 +1733,7 @@ final class PbsExecutableBodyLowerer {
}
private void lowerArrayRead(PbsAst.IndexExpr index,PbsExecutableLoweringContext c) {
var type=PbsRuntimeTypes.unwrap(expressionType(index.receiver(),c));
if(lowerListIndex(index,null,type,c))return;
var optional=PbsAst.TypeRef.optional(type.inner(),index.span());
lowerExpected(index.receiver(),type,c);
lowerArrayIndex(index.index(),c);
@@ -1703,10 +1839,11 @@ final class PbsExecutableBodyLowerer {
}
private void lowerStructNew(PbsAst.NewExpr expr,PbsExecutableLoweringContext c) {
var declaration=c.types().structs.get(expr.typeName());var ref=c.types().registryType(PbsRuntimeTypes.type(expr.typeName()));
boolean list=p.studio.compiler.pbs.PbsSdkCollections.isList(declaration);
if(expr.ctorName()!=null) {
var ctor=declaration.ctors().stream().filter(f->f.name().equals(expr.ctorName()) && parametersMatch(f.parameters(),expr.arguments(),c)).findFirst().orElseThrow();
var ctor=declaration.ctors().stream().filter(f->f.name().equals(expr.ctorName()) && parametersMatch(f.parameters(),expr.arguments(),c,list)).findFirst().orElseThrow();
var arguments=new java.util.ArrayList<Integer>();
for(int i=0;i<expr.arguments().size();i++){var type=PbsTypeSubstitution.replaceSelf(ctor.parameters().get(i).typeRef(),expr.typeName());lowerExpected(expr.arguments().get(i),type,c);arguments.add(save(c.types().slots(type),expr.span(),c));}
for(int i=0;i<expr.arguments().size();i++){var type=PbsTypeSubstitution.replaceSelf(ctor.parameters().get(i).typeRef(),expr.typeName());if(list)lowerArrayIndex(expr.arguments().get(i),c);else lowerExpected(expr.arguments().get(i),type,c);arguments.add(save(c.types().slots(type),expr.span(),c));}
registry(STRUCT_NEW,ref,0,0,1,expr.span(),c);int instance=save(1,expr.span(),c);emitGetLocal(instance,expr.span(),c);
for(int i=0;i<arguments.size();i++)emitLoadLocalSlots(arguments.get(i),c.types().slots(ctor.parameters().get(i).typeRef()),expr.span(),c);
emitStructuredFunction(expr.typeName()+".$ctor."+expr.ctorName(),receiverParameters(expr.typeName(),ctor.parameters()),PbsAst.TypeRef.unit(expr.span()),1+ctor.parameters().stream().mapToInt(p->c.types().slots(p.typeRef())).sum(),0,expr.span(),c);
@@ -1779,10 +1916,11 @@ final class PbsExecutableBodyLowerer {
emitStructuredFunction(owner.name()+"."+method.name(),method.parameters().stream().map(PbsAst.Parameter::typeRef).toList(),PbsTypeSubstitution.replaceSelf(method.returnType(),owner.name()),pops,c.types().slots(method.returnType()),call.span(),c);return true;
}
var struct=c.types().structs.get(owner.name());if(struct==null)return false;
var method=struct.methods().stream().filter(m->m.name().equals(member.memberName())&&parametersMatch(m.parameters(),call.arguments(),c) && returnMatches(m.returnType(),owner.name(),c)).findFirst().orElse(null);
boolean listIndex=p.studio.compiler.pbs.PbsSdkCollections.isList(struct) && java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName());
var method=struct.methods().stream().filter(m->m.name().equals(member.memberName())&&parametersMatch(m.parameters(),call.arguments(),c,listIndex) && returnMatches(m.returnType(),owner.name(),c)).findFirst().orElse(null);
if(method==null)return false;
lowerExpected(member.receiver(),owner,c);int pops=1;
for(int i=0;i<call.arguments().size();i++){var type=PbsTypeSubstitution.replaceSelf(method.parameters().get(i).typeRef(),owner.name());lowerExpected(call.arguments().get(i),type,c);pops+=c.types().slots(type);}
for(int i=0;i<call.arguments().size();i++){var type=PbsTypeSubstitution.replaceSelf(method.parameters().get(i).typeRef(),owner.name());if(listIndex&&i==0)lowerArrayIndex(call.arguments().get(i),c);else lowerExpected(call.arguments().get(i),type,c);pops+=c.types().slots(type);}
emitStructuredFunction(owner.name()+"."+method.name(),receiverParameters(owner.name(),method.parameters()),PbsTypeSubstitution.replaceSelf(method.returnType(),owner.name()),pops,c.types().slots(method.returnType()),call.span(),c);return true;
}
private java.util.List<PbsAst.TypeRef> receiverParameters(String owner, ReadOnlyList<PbsAst.Parameter> parameters) {
@@ -1807,6 +1945,10 @@ final class PbsExecutableBodyLowerer {
private boolean parametersMatch(ReadOnlyList<PbsAst.Parameter> parameters,
ReadOnlyList<PbsAst.Expression> arguments, PbsExecutableLoweringContext c) {
return parametersMatch(parameters,arguments,c,false);
}
private boolean parametersMatch(ReadOnlyList<PbsAst.Parameter> parameters,
ReadOnlyList<PbsAst.Expression> arguments, PbsExecutableLoweringContext c, boolean listIndex) {
if (parameters.size() != arguments.size()) return false;
for (int i = 0; i < parameters.size(); i++) {
var actual = PbsRuntimeTypes.unwrap(expressionType(arguments.get(i), c));
@@ -1817,6 +1959,7 @@ final class PbsExecutableBodyLowerer {
|| arguments.get(i) instanceof PbsAst.NoneExpr
|| arguments.get(i) instanceof PbsAst.NewExpr allocation
&& allocation.typeName().equals("array") && allocation.explicitType() == null) continue;
if (listIndex && i==0 && "int".equals(c.types().canonical(actual)) && "long".equals(c.types().canonical(expected))) continue;
if (c.types().shapeKey(actual).equals(c.types().shapeKey(expected))) continue;
if (arguments.get(i) instanceof PbsAst.IntLiteralExpr && "long".equals(c.types().canonical(expected))) continue;
if (c.types().isContract(expected) && c.types().contractTypes.implementers(expected).contains(actual.name())) continue;
@@ -69,6 +69,7 @@ final class PbsRuntimeTypes {
for (var declaration : declarations) if (declaration instanceof PbsAst.TypeBindingDecl binding) {
aliases.put(binding.name(), p.studio.compiler.pbs.PbsGenericInstantiation.name(binding.declaration()));
if (binding.declaration() instanceof PbsAst.StructDecl s) structs.put(binding.name(), s);
if (binding.declaration() instanceof PbsAst.BuiltinTypeDecl b) fields.put(binding.name(), b.fields());
if (binding.declaration() instanceof PbsAst.ServiceDecl s) services.put(binding.name(), s);
if (binding.declaration() instanceof PbsAst.ContractDecl c) contracts.put(binding.name(), c);
}
@@ -51,7 +51,7 @@ final class PbsAttributeParser {
return new PbsAst.Attribute(
name.lexeme(),
ReadOnlyList.wrap(arguments),
context.span(leftBracket.start(), end));
context.span(leftBracket.start(), end), context.parseMode() == PbsParser.ParseMode.INTERFACE_MODULE);
}
private void parseAttributeArguments(final ArrayList<PbsAst.AttributeArgument> arguments) {
@@ -684,7 +684,8 @@ final class PbsDeclarationParser {
final ReadOnlyList<PbsAst.Attribute> attributes,
final String targetSurface) {
for (final var attribute : attributes) {
if ("Doc".equals(attribute.name())) {
if ("Doc".equals(attribute.name()) || attribute.trustedSdkOrigin() &&
"SdkList".equals(attribute.name()) && "struct declarations".equals(targetSurface)) {
continue;
}
p.studio.compiler.source.diagnostics.Diagnostics.error(context.diagnostics(),
@@ -722,7 +723,8 @@ final class PbsDeclarationParser {
PbsAst.LifecycleMarker marker = PbsAst.LifecycleMarker.NONE;
for (final var attribute : attributes) {
final PbsAst.LifecycleMarker nextMarker;
if ("Doc".equals(attribute.name())) {
if ("Doc".equals(attribute.name()) || attribute.trustedSdkOrigin() &&
java.util.Set.of("StoragePrimitive","SdkPrivate").contains(attribute.name())) {
continue;
}
if (!isOrdinaryMode()) {
@@ -65,12 +65,6 @@ public final class PbsDeclarationSemanticsValidator {
final var rules = new PbsDeclarationRuleValidator(nameTable, declarationDiagnostics);
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
validateDocAttributes(functionDecl.attributes(), "function declarations", declarationDiagnostics);
if (interfaceModule) {
reportInterfaceNonDeclarativeDecl(
functionDecl.span(),
"Top-level functions are not allowed in interface modules",
declarationDiagnostics);
}
validateCallable(
PbsCallableScope.topLevelFunctions(),
functionDecl.name(),
@@ -89,12 +83,6 @@ public final class PbsDeclarationSemanticsValidator {
if (topDecl instanceof PbsAst.StructDecl structDecl) {
validateDocAttributes(structDecl.attributes(), "struct declarations", declarationDiagnostics);
binder.registerType(structDecl.name(), structDecl.span(), "struct");
if (interfaceModule && (structDecl.hasBody() || !structDecl.methods().isEmpty() || !structDecl.ctors().isEmpty())) {
reportInterfaceNonDeclarativeDecl(
structDecl.span(),
"Interface modules must not declare executable struct bodies",
declarationDiagnostics);
}
validateStructDeclaration(structDecl, binder, rules, declarationDiagnostics);
continue;
}
@@ -166,12 +154,6 @@ public final class PbsDeclarationSemanticsValidator {
}
if (topDecl instanceof PbsAst.ImplementsDecl implementsDecl) {
if (interfaceModule) {
reportInterfaceNonDeclarativeDecl(
implementsDecl.span(),
"Interface modules must not declare executable 'implements' blocks",
declarationDiagnostics);
}
validateImplementsDeclaration(implementsDecl, binder, rules);
}
}
@@ -759,9 +759,9 @@ public final class PbsEditorialSupportService {
final Map<String, PbsEditorialCompletionCandidate> completions = new LinkedHashMap<>();
var symbolic = symbolicStruct(receiverType);
if (symbolic != null) {
for (var field : symbolic.fields()) completions.put(field.name(), new PbsEditorialCompletionCandidate(
for (var field : symbolic.fields()) if(!p.studio.compiler.pbs.PbsSdkCollections.isList(symbolic)||field.isPublic()) completions.put(field.name(), new PbsEditorialCompletionCandidate(
field.name(), PbsEditorialSymbolKind.FIELD, field.typeRef().surface(), symbolic.name()));
for (var method : symbolic.methods()) completions.put(method.name(), new PbsEditorialCompletionCandidate(
for (var method : symbolic.methods()) if(p.studio.compiler.pbs.PbsSdkCollections.attribute(method.attributes(),"SdkPrivate")==null) completions.put(method.name(), new PbsEditorialCompletionCandidate(
method.name(), PbsEditorialSymbolKind.METHOD, signatureFromFunction(method.name(), method.parameters(),
method.returnKind(), method.returnType(), method.resultErrorType()).label(), symbolic.name()));
return List.copyOf(completions.values());
@@ -778,6 +778,7 @@ public final class PbsEditorialSupportService {
final StructInfo structInfo = model.structs.get(receiverType.name());
if (structInfo != null) {
for (final var field : structInfo.fields().entrySet()) {
if(model.isSdkList(receiverType)&&!field.getValue().isPublic())continue;
completions.put(field.getKey(), new PbsEditorialCompletionCandidate(
field.getKey(),
PbsEditorialSymbolKind.FIELD,
@@ -785,6 +786,7 @@ public final class PbsEditorialSupportService {
receiverType.name()));
}
for (final var method : structInfo.methods().entrySet()) {
if(model.isSdkList(receiverType)&&java.util.Set.of("_at","_put").contains(method.getKey()))continue;
completions.put(method.getKey(), new PbsEditorialCompletionCandidate(
method.getKey(),
PbsEditorialSymbolKind.METHOD,
@@ -1435,6 +1437,7 @@ public final class PbsEditorialSupportService {
return List.of(signatureFromFunction(name, functionDecl.parameters(), functionDecl.returnKind(), functionDecl.returnType(), functionDecl.resultErrorType()));
}
if (topDecl instanceof PbsAst.StructDecl structDecl) return structDecl.methods().stream()
.filter(method -> p.studio.compiler.pbs.PbsSdkCollections.attribute(method.attributes(),"SdkPrivate")==null)
.map(method -> signatureFromFunction(method.name(), method.parameters(), method.returnKind(), method.returnType(), method.resultErrorType())).toList();
if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
@@ -1465,8 +1468,9 @@ public final class PbsEditorialSupportService {
if (structDecl.ctors().isEmpty()) {
return List.of(new PbsEditorialSignature(structDecl.name(), structDecl.name() + "()", List.of(), "unit"));
}
var display=structDecl.applicationOrigin()==null?structDecl.name():structDecl.applicationOrigin().display();
return structDecl.ctors().stream()
.map(ctor -> signatureFromCtor(structDecl.name(), ctor))
.map(ctor -> signatureFromCtor(display, ctor))
.toList();
}
return List.of();
@@ -49,6 +49,9 @@ final class PbsFlowCallableResolutionAnalyzer {
return ExprResult.type(TypeView.unit());
}
}
final boolean listIndexArgument = callExpr.callee() instanceof PbsAst.MemberExpr member
&& java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName())
&& context.model().isSdkList(analyzeValueExpression(member.receiver(),context,null).type());
final var callee = analyzeTargetExpression(callExpr.callee(), context, ExprUse.CALL_TARGET);
final var expectedInputs = callee.callables().size() == 1 ? callee.callables().getFirst().inputTypes() : java.util.List.<TypeView>of();
@@ -56,7 +59,8 @@ final class PbsFlowCallableResolutionAnalyzer {
if (callExpr.arguments().isEmpty()) {
argumentType = TypeView.unit();
} else if (callExpr.arguments().size() == 1) {
argumentType = analyzeValueExpression(callExpr.arguments().getFirst(), context, expectedInputs.size() == 1 ? expectedInputs.getFirst() : null).type();
var actual = analyzeValueExpression(callExpr.arguments().getFirst(), context, expectedInputs.size() == 1 ? expectedInputs.getFirst() : null).type();
argumentType = listIndexArgument && actual.kind()==Kind.INT ? TypeView.longType() : actual;
} else {
final var fields = new ArrayList<TupleField>(callExpr.arguments().size());
int argumentIndex = 0;
@@ -64,7 +68,7 @@ final class PbsFlowCallableResolutionAnalyzer {
final var expected = argumentIndex < expectedInputs.size() ? expectedInputs.get(argumentIndex) : null;
argumentIndex++;
final var itemType = analyzeValueExpression(argument, context, expected).type();
fields.add(new TupleField(null, itemType));
fields.add(new TupleField(null,listIndexArgument && argumentIndex==1 && itemType.kind()==Kind.INT ? TypeView.longType() : itemType));
}
argumentType = TypeView.tuple(fields);
}
@@ -82,6 +86,9 @@ final class PbsFlowCallableResolutionAnalyzer {
final PbsAst.ApplyExpr applyExpr,
final PbsFlowExpressionContext context) {
final var callee = analyzeTargetExpression(applyExpr.callee(), context, ExprUse.APPLY_TARGET);
boolean listIndex=applyExpr.callee() instanceof PbsAst.MemberExpr member
&& java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName())
&& context.model().isSdkList(analyzeValueExpression(member.receiver(),context,null).type());
TypeView argument;
if (applyExpr.argument() instanceof PbsAst.TupleExpr tuple && !tuple.items().isEmpty()
&& tuple.items().getFirst().label() != null && callee.callables().size() == 1) {
@@ -92,7 +99,8 @@ final class PbsFlowCallableResolutionAnalyzer {
if (parameter == null || byName.containsKey(item.label())) {
Diagnostics.error(context.diagnostics(), "E_SEM_NAMED_ARGUMENT", "Unknown or duplicate named argument: " + item.label(), item.span());
}
byName.put(item.label(), analyzeValueExpression(item.expression(), context, parameter == null ? null : parameter.type()).type());
var actual=analyzeValueExpression(item.expression(),context,parameter==null?null:parameter.type()).type();
byName.put(item.label(),listIndex&&parameter!=null&&parameter.order()==0&&actual.kind()==Kind.INT?TypeView.longType():actual);
}
var fields = new ArrayList<TupleField>();
for (var parameter : candidate.parameters()) {
@@ -101,7 +109,14 @@ final class PbsFlowCallableResolutionAnalyzer {
}
argument = TypeView.tuple(fields);
} else {
argument = analyzeValueExpression(applyExpr.argument(), context, callee.callables().size() == 1 && callee.callables().getFirst().inputTypes().size() == 1 ? callee.callables().getFirst().inputTypes().getFirst() : null).type();
var inputs=callee.callables().size()==1?callee.callables().getFirst().inputTypes():java.util.List.<TypeView>of();
var expected=inputs.size()==1?inputs.getFirst():inputs.isEmpty()?null:TypeView.tuple(inputs.stream().map(t->new TupleField(null,t)).toList());
argument=analyzeValueExpression(applyExpr.argument(),context,expected).type();
if(listIndex&&argument.kind()==Kind.INT)argument=TypeView.longType();
else if(listIndex&&argument.kind()==Kind.TUPLE&&!argument.tupleFields().isEmpty()&&argument.tupleFields().getFirst().type().kind()==Kind.INT) {
var fields=new ArrayList<>(argument.tupleFields());
fields.set(0,new TupleField(fields.getFirst().label(),TypeView.longType()));argument=TypeView.tuple(fields);
}
}
return resolveCallableApplication(
@@ -194,6 +209,10 @@ final class PbsFlowCallableResolutionAnalyzer {
}
return ExprResult.type(field.type());
}
if(model.isSdkList(receiver) && java.util.Set.of("_at","_put").contains(memberExpr.memberName())) {
Diagnostics.error(diagnostics,"E_SEM_SDK_PRIVATE_MEMBER","SDK storage adapters are private",memberExpr.span());
return ExprResult.type(TypeView.unknown());
}
final var methods = struct.methods().get(memberExpr.memberName());
if (methods != null && !methods.isEmpty()) {
if (use == ExprUse.VALUE) {
@@ -198,6 +198,10 @@ final class PbsFlowSemanticSupport {
static final class Model {
p.studio.compiler.pbs.PbsContractTypes contractTypes;
boolean isSdkList(TypeView type) {
return type != null && p.studio.compiler.pbs.PbsSdkCollections.isList(contractTypes.declaration(
type.sourceType() == null ? PbsAst.TypeRef.simple(type.name(), Span.none()) : type.sourceType()));
}
final Map<String, List<CallableSymbol>> topLevelCallables = new HashMap<>();
final Map<String, StructInfo> structs = new HashMap<>();
final Map<String, ServiceInfo> services = new HashMap<>();
@@ -53,6 +53,9 @@ final class PbsFlowStructuralExpressionAnalyzer {
if (expression instanceof PbsAst.IndexExpr index) {
final var receiver = analyzeValueExpression(index.receiver(), context, null).type();
checkArrayIndex(index.index(), context);
if (model.isSdkList(receiver)) {
return ExprResult.type(model.structs.get(receiver.name()).methods().get("_at").getFirst().outputType());
}
if (receiver.kind() != Kind.ARRAY) {
Diagnostics.error(diagnostics, "E_SEM_ARRAY_RECEIVER", "Indexing requires an array", index.receiver().span());
return ExprResult.type(TypeView.unknown());
@@ -0,0 +1,156 @@
// Source algorithms own state, ranges, capacity policy and snapshot topology.
// Only these private, origin-checked helpers bind generic storage instructions.
[StoragePrimitive(name = "indexGuard")]
fn _bounds(index: long, limit: long) {}
[StoragePrimitive(name = "checkLength")]
fn _checkLength(length: long) {}
[StoragePrimitive(name = "copy")]
fn _copy<T>(source: array<T>, sourceStart: long, target: array<T>, targetStart: long, count: long) {}
[StoragePrimitive(name = "publish", first = "storage", second = "length")]
fn _publish<T>(source: array<T>, sourceStart: long, target: array<T>, targetStart: long,
count: long, fillStart: long, fillCount: long, fill: optional T,
owner: list<T>, storage: array<T>, length: long) {}
fn _grownCapacity(capacity: long, required: long) -> long {
_checkLength(required);
let maximum: long = 4294967295;
let increment: long = capacity / 2 + capacity % 2;
if increment < (1 as long) { increment = 1; }
let candidate: long = capacity + increment;
if candidate < required { candidate = required; }
if candidate > maximum { candidate = maximum; }
return candidate;
}
[SdkList]
declare struct list<T>(storage: array<T>, length: long) {
ctor make() {
this.storage = new array<T>(16);
this.length = 0;
}
ctor make(capacity: long) {
this.storage = new array<T>(capacity);
this.length = 0;
}
fn size() -> long { return this.length; }
fn capacity() -> long { return this.storage.length; }
[SdkPrivate]
fn _at(index: long) -> optional T {
_bounds(index, this.length);
return this.storage[index];
}
[SdkPrivate]
fn _put(index: long, value: T) {
_bounds(index, this.length);
this.storage[index] = value;
}
fn add(value: T) { this.insert(this.length, value); }
fn insert(index: long, value: T) {
_bounds(index, this.length + 1);
let required: long = this.length + 1;
_checkLength(required);
let target: array<T> = this.storage;
if required > target.length {
target = new array<T>(_grownCapacity(target.length, required));
_copy<T>(this.storage, 0, target, 0, this.length);
}
_publish<T>(target, index, target, index + 1, this.length - index,
index, 1, some(value), this, target, required);
}
fn clearAt(index: long) {
_bounds(index, this.length);
_publish<T>(this.storage, 0, this.storage, 0, 0,
index, 1, none, this, this.storage, this.length);
}
fn removeAt(index: long) -> optional T {
_bounds(index, this.length);
let previous: optional T = this.storage[index];
_publish<T>(this.storage, index + 1, this.storage, index, this.length - index - 1,
this.length - 1, 1, none, this, this.storage, this.length - 1);
return previous;
}
fn clear() {
_publish<T>(this.storage, 0, this.storage, 0, 0,
0, this.length, none, this, this.storage, 0);
}
fn resize(length: long) {
_checkLength(length);
let target: array<T> = this.storage;
if length > target.length {
target = new array<T>(_grownCapacity(target.length, length));
_copy<T>(this.storage, 0, target, 0, this.length);
}
let start: long = this.length;
let count: long = length - this.length;
if length < this.length { start = length; count = this.length - length; }
_publish<T>(target, 0, target, 0, 0, start, count, none, this, target, length);
}
fn reserve(capacity: long) {
_checkLength(capacity);
if capacity > this.storage.length {
let target: array<T> = new array<T>(capacity);
_copy<T>(this.storage, 0, target, 0, this.length);
_publish<T>(target, 0, target, 0, 0, 0, 0, none, this, target, this.length);
}
}
fn trim() {
if this.storage.length != this.length {
let target: array<T> = new array<T>(this.length);
_copy<T>(this.storage, 0, target, 0, this.length);
_publish<T>(target, 0, target, 0, 0, 0, 0, none, this, target, this.length);
}
}
fn cursor() -> Cursor<T> {
let head: optional _SnapshotNode<T> = none;
let index: long = this.length;
while index > (0 as long) {
index = index - 1;
if let value = this.storage[index] {
head = some(new _SnapshotNode<T>(value, head));
}
}
return new _SnapshotCursor<T>(head) as Cursor<T>;
}
}
declare contract Cursor<T> {
fn hasNext() -> bool;
fn next();
fn current() -> optional T;
}
// Read-only links and values; the private Cursor holds only current + suffix.
declare struct _SnapshotNode<T>(pub value: T, pub next: optional _SnapshotNode<T>) {
ctor make(value: T, next: optional _SnapshotNode<T>) {
this.value = value;
this.next = next;
}
}
declare struct _SnapshotCursor<T>(node: optional _SnapshotNode<T>) {
ctor make(node: optional _SnapshotNode<T>) { this.node = node; }
fn hasNext() -> bool {
if let node = this.node { return node.next.hasSome(); }
return false;
}
fn next() {
if let node = this.node {
if let successor = node.next { this.node = some(successor); }
}
}
fn current() -> optional T {
if let node = this.node { return some(node.value); }
return none;
}
}
implements<T> Cursor<T> for _SnapshotCursor<T> using snapshot {
fn hasNext() -> bool { return snapshot.hasNext(); }
fn next() { snapshot.next(); }
fn current() -> optional T { return snapshot.current(); }
}
@@ -0,0 +1,2 @@
pub struct list<T>;
pub contract Cursor<T>;
@@ -0,0 +1,26 @@
import { list, Cursor } from @sdk:collections;
fn collectionsExample() -> int {
let values: list<int> = new list(0);
values.resize(3); // Three logical holes; capacity alone creates no positions.
values[0] = 20;
values[2] = 22;
let snapshot: Cursor<int> = values.cursor();
values.clear(); // The completed snapshot keeps its captured sequence.
let total: int = 0;
while true {
if let value = snapshot.current() {
total = total + value;
} else {
break;
}
if snapshot.hasNext() {
snapshot.next();
} else {
break;
}
}
// current() still holds 22: next() cannot advance past the last node.
return total;
}
@@ -2,3 +2,4 @@ pub fn init() -> void;
pub fn frame() -> void;
pub fn contractExample() -> int;
pub fn genericsExample() -> int;
pub fn collectionsExample() -> int;
@@ -41,6 +41,54 @@ class PbsGateUSdkInterfaceConformanceTest {
@TempDir
Path tempDir;
@Test
void listAndCursorUseDistributedSdkBodiesAndPrivateDefinitionScopes() throws IOException {
var result=compileWorkspaceModule(tempDir.resolve("list-cursor"), """
import { list, Cursor } from @sdk:collections;
fn main() -> int {
let capacity: int = 0;
let values: list<int> = new list(capacity);
values.add(20); values.insert(capacity, 22);
let snapshot: Cursor<int> = values.cursor();
values.clear();
return snapshot.current() else 0;
}
[Frame] fn frame() -> void {let observed=main();}
""", "pub fn main() -> int; pub fn frame() -> void;", 1, null);
assertFalse(result.diagnostics().stream().anyMatch(d->d.getSeverity().isError()),result.diagnostics().stream().map(d->d.getCode()+":"+d.getMessage()).toList().toString());
}
@Test
void listApplicationsAndPrivateAdaptersRejectInvalidProjectUses() throws IOException {
var cases=java.util.Map.of(
"optional","let values:list<optional int>=new list<optional int>();",
"grouped","let values:list<(optional int)>=new list<(optional int)>();",
"arity","let values:list<int,long>=new list<int,long>();",
"context","let values=new list();",
"invariance","let values:list<long>=new list<int>();",
"index","let values=new list<int>();let invalid=values[1.5];",
"storage","let values=new list<int>();let exposed=values.storage;",
"adapter","let values=new list<int>();let exposed=values._at(0);",
"value-promotion","let values=new list<long>();let value:int=1;values.add(value);");
for(var entry:cases.entrySet()) {
var source="import {list} from @sdk:collections; [Frame] fn frame() -> void {"+entry.getValue()+"}";
var result=compileWorkspaceModule(tempDir.resolve("list-negative-"+entry.getKey()),source,"pub fn frame() -> void;",1,null);
assertTrue(result.diagnostics().hasErrors(),entry.getKey());
}
var homonym=new PbsFrontendCompiler();var diagnostics=DiagnosticSink.empty();
homonym.compileFile(new FileId(200),"""
declare struct list<T>(value:T) {}
fn main() {let values=new list<int>(1);let invalid=values[0];}
""",diagnostics,SourceKind.PROJECT);
assertTrue(diagnostics.stream().anyMatch(d->d.getCode().equals("E_SEM_ARRAY_RECEIVER")));
var copied=DiagnosticSink.empty();
homonym.compileFile(new FileId(201),"""
[SdkList] declare struct list<T>(value:T) {}
[StoragePrimitive(name="indexGuard")] fn fake(index:long,limit:long) {}
""",copied,SourceKind.PROJECT);
assertTrue(copied.hasErrors(),"Project attributes cannot acquire SDK bindings");
}
@Test
void removedInputObjectsExplainTheirMigration() throws IOException {
final var result = compileWorkspaceModule(tempDir.resolve("removed-input"), """
@@ -296,9 +344,7 @@ class PbsGateUSdkInterfaceConformanceTest {
"fn run() -> int { return 1; }",
negativeSemanticsDiagnostics,
SourceKind.SDK_INTERFACE);
final var nonDeclarative = firstDiagnostic(negativeSemanticsDiagnostics,
d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name()));
assertStableDiagnosticIdentity(nonDeclarative, PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name(), DiagnosticPhase.STATIC_SEMANTICS);
assertFalse(negativeSemanticsDiagnostics.hasErrors(), "SDK modules retain ordinary executable function bodies");
final var negativeLinkDiagnostics = DiagnosticSink.empty();
final var invalidBarrelAst = PbsBarrelParser.parse(
@@ -6,6 +6,28 @@ import p.studio.compiler.source.identifiers.FileId;
import static org.junit.jupiter.api.Assertions.*;
class PbsGenericStructsTest {
@Test void recursiveOptionalBindingsAndContractViewsKeepTheirDefinitionShapes() {
accepts("""
declare contract Reader<T> {fn read()->optional T;}
declare struct Node<T>(pub value:T,pub next:optional Node<T>);
declare struct Holder<T>(node:optional Node<T>) {
fn read()->optional T {if let node=this.node {return some(node.value);}return none;}
fn view()->Reader<T> {return this as Reader<T>;}
}
implements<T> Reader<T> for Holder<T> using owner {fn read()->optional T {return owner.read();}}
fn main()->int {
let value=new Holder<int>(some(new Node<int>(42,none)));
return value.view().read() else 0;
}
""");
accepts("""
declare struct Cell<T>(values:array<T>) {
fn set(value:T) {this.values[0]=value;}
fn get()->optional T {return this.values[0];}
}
fn main()->int {let cell=new Cell<int>([0]);cell.set(42);return cell.get() else 0;}
""");
}
private static final String BOX = """
declare struct Box<T>(value: T) {
fn get() -> T { return this.value; }
@@ -109,7 +109,7 @@ class PbsInterfaceModuleSemanticsTest {
}
@Test
void shouldRejectExecutableDeclarationsInInterfaceModule() {
void shouldAllowOrdinaryExecutableSdkDeclarations() {
final var source = """
fn run() -> int { return 1; }
@@ -123,10 +123,7 @@ class PbsInterfaceModuleSemanticsTest {
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics, SourceKind.SDK_INTERFACE);
final var nonDeclarativeCount = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name()))
.count();
assertTrue(nonDeclarativeCount >= 2);
assertFalse(diagnostics.hasErrors(), diagnostics.toString());
}
@Test
@@ -0,0 +1,54 @@
package p.studio.compiler.pbs.semantics;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.*;
class PbsListCursorEditorialTest {
@Test void concreteApiHidesStorageAndKeepsSdkDefinitionOrigins() throws Exception {
var diagnostics=DiagnosticSink.empty();var sdkId=new FileId(1);
String sdk;
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
assertNotNull(stream);sdk=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
var library=PbsParser.parse(PbsLexer.lex(sdk,sdkId,diagnostics),sdkId,diagnostics,PbsParser.ParseMode.INTERFACE_MODULE);
var list=library.topDecls().stream().filter(d->d instanceof PbsAst.StructDecl s&&s.name().equals("list")).findFirst().orElseThrow();
var cursor=library.topDecls().stream().filter(d->d instanceof PbsAst.ContractDecl c&&c.name().equals("Cursor")).findFirst().orElseThrow();
var source="""
import {list as Values,Cursor} from @sdk:collections;
fn sample(values:Values<int>,cursor:Cursor<int>)->optional int {
let created:Values<int>=new Values(0);
values.add(42); cursor.hasNext(); return cursor.current();
}
""";
var id=new FileId(2);var ast=PbsParser.parse(PbsLexer.lex(source,id,diagnostics),id,diagnostics);
assertFalse(diagnostics.hasErrors());
var supplemental=ReadOnlyList.<PbsAst.TopDecl>from(new PbsAst.TypeBindingDecl("Values",list,ast.span()),cursor);
var service=new PbsEditorialSupportService();int member=source.indexOf("values.add")+7;
var typeHover=service.hover(source,ast,supplemental,source.indexOf("Values<int>")).orElseThrow();
assertFalse(typeHover.signatures().stream().anyMatch(s->s.label().startsWith("_")));
var constructors=service.signatureHelp(source,ast,supplemental,source.indexOf("new Values(0")+11).orElseThrow().signatures();
assertEquals(2,constructors.size());
assertTrue(constructors.stream().allMatch(s->!s.label().contains("storage")&&!s.label().contains("$generic$")));
var completions=service.completion(source,ast,supplemental,member);
assertTrue(completions.stream().anyMatch(c->c.label().equals("add")&&c.detail().contains("int")));
assertFalse(completions.stream().anyMatch(c->java.util.Set.of("storage","length","_at","_put").contains(c.label())));
var definition=service.definition(source,ast,supplemental,member);
assertEquals(sdkId,definition.getFirst().span().getFileId());
int current=source.indexOf("cursor.current")+7;
assertTrue(service.hover(source,ast,supplemental,current).orElseThrow().signatures().getFirst().label().contains("optional int"));
var methods=service.completion(source,ast,supplemental,current);
assertEquals(java.util.Set.of("hasNext","next","current"),methods.stream().map(c->c.label()).collect(java.util.stream.Collectors.toSet()));
assertFalse(service.definition(source,ast,supplemental,current).isEmpty());
var rename=service.rename(source,ast,supplemental,source.indexOf("values:Values"),"items");
assertTrue(rename.allowed());
assertEquals(2,rename.sites().size());
assertTrue(service.references(source,ast,supplemental,source.indexOf("values:Values")).size()>=2);
}
}
@@ -654,7 +654,7 @@ class PBSFrontendPhaseServiceTest {
}
@Test
void shouldSkipIrLoweringForSourcesMarkedAsSdkInterface() throws IOException {
void shouldLowerOrdinaryExecutableSdkFunctions() throws IOException {
final var projectRoot = tempDir.resolve("project-sdk-interface");
final var sourceRoot = projectRoot.resolve("src");
final var modulePath = sourceRoot.resolve("gfx");
@@ -691,9 +691,9 @@ class PBSFrontendPhaseServiceTest {
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name())));
assertFalse(diagnostics.hasErrors(), diagnostics.stream().map(d -> d.getMessage()).toList().toString());
assertEquals(0, irBackend.getFunctions().size());
assertEquals(1, irBackend.getExecutableFunctions().size());
}
@Test
@@ -65,8 +65,9 @@ public class BytecodeEmitter {
switch (op.kind()) {
case REGISTRY -> {
var instruction = Objects.requireNonNull(op.registryInstruction());
writeOpU32(code, instruction.op().opcode(), instruction.immediate());
if (instruction.secondImmediate() != null) code.writeBytes(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(instruction.secondImmediate()).array());
writeOpNoImm(code,instruction.op().opcode());
for(var value:new Integer[]{instruction.immediate(),instruction.secondImmediate(),instruction.thirdImmediate(),instruction.fourthImmediate()})
if(value!=null)code.writeBytes(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(value).array());
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
}
case HALT -> {
@@ -5,11 +5,12 @@ final class BytecodeOpcodeLayout {
static int sizeForOpcode(int opcode) {
return switch(opcode) {
case 0x00,0x01,0x05,0x11,0x12,0x13,0x20,0x21,0x22,0x23,0x24,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x51,0x80 -> 2;
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x51,0x80,0xa7 -> 2;
case 0x16 -> 3;
case 0x02,0x03,0x04,0x10,0x17,0x18,0x40,0x41,0x42,0x43,0x50,0x53,0x57,0x70,0x71,0x72,
0x90,0x91,0x92,0xa0,0xa3,0xa4,0xa5,0xa6 -> 6;
0x90,0x91,0x92,0xa0,0xa3,0xa4,0xa5,0xa6,0xa8,0xa9 -> 6;
case 0x14,0x15,0x52,0x58,0x93,0x94,0x95,0x96,0xa1,0xa2 -> 10;
case 0xaa -> 18;
default -> throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNKNOWN_OPCODE,
"unknown or retired opcode 0x%04X".formatted(opcode));
};
@@ -29,15 +29,15 @@ public class BytecodePreloadVerifierService {
for(int i=0;i<module.functions().size();i++)verifyFunction(module,registry,i,used);
for(int i=0;i<used.length;i++)check(used[i],"unused syscall declaration "+i);
}
private record Instruction(int pc,int opcode,int first,int second,int next) {}
private record Instruction(int pc,int opcode,int first,int second,int third,int fourth,int next) {}
private void verifyFunction(BytecodeModule m,LinkedRegistry r,int index,boolean[] used) {
var f=m.functions().get(index);byte[] code=m.code();int start=f.codeOffset(),end=start+f.codeLen();
var instructions=new TreeMap<Integer,Instruction>();
for(int pc=start;pc<end;) {
check(pc+2<=end,"truncated opcode");int op=u16(code,pc);
if(op==0x70)throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_LINKAGE_RAW_SYSCALL_IN_PRELOAD,"raw SYSCALL in producer image");int size=BytecodeOpcodeLayout.sizeForOpcode(op);check((long)pc+size<=end,"truncated instruction");
int a=size>=6?i32(code,pc+2):0,b=size==10?i32(code,pc+6):0;
var instruction=new Instruction(pc,op,a,b,pc+size);instructions.put(pc,instruction);
int a=size>=6?i32(code,pc+2):0,b=size>=10?i32(code,pc+6):0;
var instruction=new Instruction(pc,op,a,b,size>=18?i32(code,pc+10):0,size>=18?i32(code,pc+14):0,pc+size);instructions.put(pc,instruction);
switch(op) {
case 0x10 -> check(a>=0&&a<m.constPool().size(),"constant index");
case 0x16 -> check(code[pc+2]==0||code[pc+2]==1,"Boolean immediate");
@@ -58,7 +58,8 @@ public class BytecodePreloadVerifierService {
case 0x93,0x94,0x95,0x96 -> {var src=type(r,a).body();var dst=type(r,b).body();check(src instanceof Struct||src instanceof Array||src instanceof Trait,"reference source category");check(op==0x94||op==0x95?dst instanceof Trait:dst instanceof Struct||dst instanceof Array,"reference target category");}
case 0xa0 -> check(type(r,a).body() instanceof Struct,"Struct category");
case 0xa1,0xa2 -> field(r,a,b);
case 0xa3,0xa4,0xa5,0xa6 -> array(r,a);
case 0xa3,0xa4,0xa5,0xa6,0xa8,0xa9 -> array(r,a);
case 0xaa -> {array(r,a);field(r,b,instruction.third());field(r,b,instruction.fourth());check(instruction.third()!=instruction.fourth(),"publication fields must differ");}
default -> { }
}
pc+=size;
@@ -166,6 +167,21 @@ public class BytecodePreloadVerifierService {
case 0xa4 -> {consume(s,facts(r,type(r,a).identity()),typed);s.stack.add(carrier("i64"));}
case 0xa5 -> {consume(s,List.of(carrier("i64")),typed);consume(s,facts(r,type(r,a).identity()),typed);produce(s,facts(r,array(r,a).element()),i.pc());}
case 0xa6 -> {consume(s,facts(r,array(r,a).element()),typed);consume(s,List.of(carrier("i64")),typed);consume(s,facts(r,type(r,a).identity()),typed);}
case 0xa7 -> consume(s,List.of(carrier("i64"),carrier("i64")),typed);
case 0xa8 -> consume(s,List.of(carrier("i64")),typed);
case 0xa9,0xaa -> {
if(op==0xaa) {
consume(s,facts(r,field(r,b,i.fourth()).type()),typed);
consume(s,facts(r,field(r,b,i.third()).type()),typed);
consume(s,facts(r,type(r,b).identity()),typed);
consume(s,facts(r,array(r,a).element()),typed);
consume(s,List.of(carrier("i64"),carrier("i64")),typed);
}
consume(s,List.of(carrier("i64"),carrier("i64")),typed);
consume(s,facts(r,type(r,a).identity()),typed);
consume(s,List.of(carrier("i64")),typed);
consume(s,facts(r,type(r,a).identity()),typed);
}
case 0x34,0x35 -> {consume(s,List.of(carrier("bool"),carrier("bool")),typed);s.stack.add(carrier("bool"));}
case 0x36 -> {consume(s,List.of(carrier("bool")),typed);s.stack.add(carrier("bool"));}
case 0x3e -> {
@@ -2,34 +2,24 @@ package p.studio.compiler.backend.irvm;
import java.util.Objects;
public record IRVMInstruction(
IRVMOp op,
Integer immediate,
Integer secondImmediate) {
public IRVMInstruction(
IRVMOp op,
Integer immediate) {
this(op, immediate, null);
}
public record IRVMInstruction(IRVMOp op, Integer immediate, Integer secondImmediate,
Integer thirdImmediate, Integer fourthImmediate) {
public IRVMInstruction(IRVMOp op, Integer immediate) { this(op,immediate,null,null,null); }
public IRVMInstruction(IRVMOp op, Integer immediate, Integer secondImmediate) {
this(op,immediate,secondImmediate,null,null);
}
public IRVMInstruction {
Objects.requireNonNull(op, "op");
if (op.name().startsWith("REGISTRY_") && ((op.immediateSize() == 8) != (secondImmediate != null))) {
throw new IllegalArgumentException("registry operand count differs from opcode layout");
}
if (!op.name().startsWith("REGISTRY_") && secondImmediate != null) {
throw new IllegalArgumentException("unexpected second immediate");
}
if (op.immediateSize() > 0 && immediate == null) {
throw new IllegalArgumentException("immediate is required for opcode: " + op.name());
}
if (op.immediateSize() == 0 && immediate != null) {
throw new IllegalArgumentException("immediate is not allowed for opcode: " + op.name());
Objects.requireNonNull(op,"op");
int count=(immediate==null?0:1)+(secondImmediate==null?0:1)+(thirdImmediate==null?0:1)+(fourthImmediate==null?0:1);
if(op.name().startsWith("REGISTRY_")) {
if(count*4!=op.immediateSize() || (thirdImmediate!=null&&secondImmediate==null) || (fourthImmediate!=null&&thirdImmediate==null))
throw new IllegalArgumentException("registry operand count differs from opcode layout");
} else {
if(secondImmediate!=null||thirdImmediate!=null||fourthImmediate!=null)
throw new IllegalArgumentException("unexpected extra immediate");
if((op.immediateSize()>0)!=(immediate!=null))
throw new IllegalArgumentException("immediate differs from opcode layout: "+op.name());
}
}
public int encodedSize() {
return 2 + op.immediateSize();
}
public int encodedSize() { return 2+op.immediateSize(); }
}
@@ -13,7 +13,7 @@ public final class RegistryInstructionLowerer {
public static IRVMInstruction lower(IRRegistryInstruction instruction, LinkedRegistry registry,
IRBackendExecutableFunction current, ReadOnlyList<IRBackendExecutableFunction> functions, Map<CallableId,Integer> functionIds) {
int first=instruction.type()==null?instruction.member():registry.id(instruction.type()).index();
Integer second=null;int pops=0,pushes=0;
Integer second=null,third=null,fourth=null;int pops=0,pushes=0;
var body=instruction.type()==null?null:registry.type(instruction.type()).body();
switch(instruction.kind()) {
case MAKE_CLOSURE -> {
@@ -51,12 +51,25 @@ public final class RegistryInstructionLowerer {
pops=instruction.kind()==IRRegistryInstruction.Kind.STRUCT_SET?1+width:1;pushes=instruction.kind()==IRRegistryInstruction.Kind.STRUCT_GET?width:0;
}
}
case INDEX_GUARD -> pops=2;
case ARRAY_CHECK_LENGTH,ARRAY_COPY,STORAGE_PUBLISH -> {
if(!(body instanceof Array array))throw new IllegalArgumentException("Array category required");
if(instruction.kind()==IRRegistryInstruction.Kind.ARRAY_CHECK_LENGTH)pops=1;
else if(instruction.kind()==IRRegistryInstruction.Kind.ARRAY_COPY)pops=5;
else {
if(!(registry.type(instruction.targetType()).body() instanceof Struct owner))throw new IllegalArgumentException("Struct publication owner required");
if(instruction.member()>=owner.fields().size()||instruction.secondMember()>=owner.fields().size())throw new IllegalArgumentException("publication field index");
second=registry.id(instruction.targetType()).index();third=instruction.member();fourth=instruction.secondMember();
pops=8+registry.width(array.element())+registry.width(owner.fields().get(third).type())+registry.width(owner.fields().get(fourth).type());
}
}
case ARRAY_NEW,ARRAY_LEN,ARRAY_GET,ARRAY_SET -> {
if(!(body instanceof Array array))throw new IllegalArgumentException("Array category required");
int width=registry.width(array.element());
switch(instruction.kind()) {case ARRAY_NEW,ARRAY_LEN->{pops=1;pushes=1;}case ARRAY_GET->{pops=2;pushes=width;}case ARRAY_SET->pops=2+width;default->throw new AssertionError();}
}
}
return new IRVMInstruction(new IRVMOp("REGISTRY_"+instruction.kind(),instruction.kind().opcode(),second==null?4:8,pops,pushes,false,false,false),first,second);
int bytes=instruction.kind()==IRRegistryInstruction.Kind.INDEX_GUARD?0:fourth!=null?16:second==null?4:8;
return new IRVMInstruction(new IRVMOp("REGISTRY_"+instruction.kind(),instruction.kind().opcode(),bytes,pops,pushes,false,false,false),bytes==0?null:first,second,third,fourth);
}
}
@@ -0,0 +1,67 @@
package p.studio.compiler.backend.registry;
import org.junit.jupiter.api.Test;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.backend.irvm.*;
import p.studio.compiler.models.*;
import p.studio.compiler.models.registry.*;
import p.studio.compiler.models.registry.IRRegistry.*;
import p.studio.compiler.source.identifiers.*;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.file.*;
import java.util.ArrayList;
import static p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.*;
import static p.studio.compiler.models.registry.IRRegistryInstruction.Kind.*;
import static org.junit.jupiter.api.Assertions.*;
/** A synthetic frontend qualifies generic storage without a language dependency. */
class StoragePublicationPipelineTest {
private static final TypeRef INTEGER=TypeRef.builtin(0x10),LONG=TypeRef.builtin(0x11),
BUFFER=new TypeRef("synthetic:buffer"),OWNER=new TypeRef("synthetic:state");
private final ArrayList<IRBackendExecutableFunction.Instruction> instructions=new ArrayList<>();
private void literal(IRBackendExecutableFunction.InstructionKind kind,String value) {
instructions.add(new IRBackendExecutableFunction.Instruction(kind,null,null,null,null,null,value,null,null,null,null));
}
private void local(IRBackendExecutableFunction.InstructionKind kind,int index) {
instructions.add(new IRBackendExecutableFunction.Instruction(kind,null,null,null,null,null,null,null,index,null,null));
}
private void typed(IRRegistryInstruction.Kind kind,TypeRef type,int member) {
instructions.add(new IRBackendExecutableFunction.Instruction(REGISTRY,null,null,null,null,null,null,null,null,null,null,
new IRRegistryInstruction(kind,type,null,null,member)));
}
private void number(long value){literal(PUSH_I64,Long.toString(value));}
@Test void guardsTransfersAndPublicationSurviveBothProducerPaths() throws Exception {
var registry=new IRRegistry(ReadOnlyList.from(
new TypeDeclaration(INTEGER,null,new Builtin(0x10)),new TypeDeclaration(LONG,null,new Builtin(0x11)),
new TypeDeclaration(BUFFER,"Buffer",new Array(INTEGER)),
new TypeDeclaration(OWNER,"State",new Struct(ReadOnlyList.from(new NamedType(LONG,"generation"),new NamedType(INTEGER,"answer"))))),null);
number(3);typed(ARRAY_CHECK_LENGTH,BUFFER,0);
number(3);typed(ARRAY_NEW,BUFFER,0);local(SET_LOCAL,0);
typed(STRUCT_NEW,OWNER,0);local(SET_LOCAL,1);
number(0);number(1);typed(INDEX_GUARD,null,0);
for(int i=0;i<2;i++) {local(GET_LOCAL,0);number(i);literal(PUSH_I32,Integer.toString((i+1)*10));typed(ARRAY_SET,BUFFER,0);}
local(GET_LOCAL,0);number(0);local(GET_LOCAL,0);number(1);number(2);typed(ARRAY_COPY,BUFFER,0);
local(GET_LOCAL,0);number(1);local(GET_LOCAL,0);number(0);number(2);number(2);number(1);
literal(PUSH_I32,"7");local(GET_LOCAL,1);number(9);literal(PUSH_I32,"42");
instructions.add(new IRBackendExecutableFunction.Instruction(REGISTRY,null,null,null,null,null,null,null,null,null,null,
new IRRegistryInstruction(STORAGE_PUBLISH,BUFFER,OWNER,null,0,1)));
local(GET_LOCAL,1);typed(STRUCT_GET,OWNER,1);literal(RET,null);
var function=new IRBackendExecutableFunction(new FileId(0),new ModuleId(0),"main",new CallableId(0),0,0,0,2,1,11,
ReadOnlyList.wrap(instructions),null,new FunctionMetadata(new Signature(null,ReadOnlyList.from(INTEGER)),null,Role.NONE));
var backend=IRBackend.builder().entryPointModuleId(new ModuleId(0)).entryPointCallableName("main")
.registry(registry).executableFunctions(ReadOnlyList.from(function)).build();
for(boolean optimize:new boolean[]{false,true}) {
var program=new LowerToIRVMService().lower(backend);
if(optimize)program=new OptimizeIRVMService().optimize(program);
var publication=program.module().functions().getFirst().instructions().stream()
.filter(i->i.op().opcode()==0xaa).findFirst().orElseThrow();
assertEquals(18,publication.encodedSize());assertEquals(0,publication.thirdImmediate());assertEquals(1,publication.fourthImmediate());
var module=new BytecodeEmitter().emit(program.coherentEmissionPlan());
new BytecodePreloadVerifierService().verify(module);
var directory=Path.of("build/registry-conformance");Files.createDirectories(directory);
Files.write(directory.resolve("common-storage"+(optimize?"-optimized":"")+".pbx"),module.serialize());
}
assertThrows(IllegalArgumentException.class,()->new IRRegistryInstruction(STORAGE_PUBLISH,BUFFER,OWNER,null,1,1));
}
}
@@ -220,7 +220,7 @@ public class IRBackend implements IRBackendReader {
instruction.expectedRetSlots(),
instruction.span(), instruction.registryInstruction() == null ? null :
new p.studio.compiler.models.registry.IRRegistryInstruction(instruction.registryInstruction().kind(), instruction.registryInstruction().type(), instruction.registryInstruction().targetType(),
instruction.registryInstruction().function() == null ? null : remapCallableId(instruction.registryInstruction().function(), callableRemap, "Registry instruction"), instruction.registryInstruction().member())));
instruction.registryInstruction().function() == null ? null : remapCallableId(instruction.registryInstruction().function(), callableRemap, "Registry instruction"), instruction.registryInstruction().member(), instruction.registryInstruction().secondMember())));
}
return new IRBackendExecutableFunction(
function.fileId(),
@@ -5,26 +5,32 @@ import java.util.Objects;
/** Semantic Registry operands before image linking. No frontend syntax is exposed. */
public record IRRegistryInstruction(Kind kind, IRRegistry.TypeRef type, IRRegistry.TypeRef targetType,
CallableId function, int member) {
CallableId function, int member, int secondMember) {
public IRRegistryInstruction(Kind kind, IRRegistry.TypeRef type, IRRegistry.TypeRef targetType, CallableId function, int member) {
this(kind,type,targetType,function,member,0);
}
public enum Kind {
MAKE_CLOSURE(0x52), CALL_CLOSURE(0x53), GET_CAPTURE(0x57), CALL_TRAIT(0x58),
DYN_PACK(0x90), DYN_UNPACK(0x91), DYN_CAN_UNPACK(0x92),
REF_IS_EXACT(0x93), REF_IMPLEMENTS(0x94), REF_TO_TRAIT(0x95), REF_CAST(0x96),
STRUCT_NEW(0xa0), STRUCT_GET(0xa1), STRUCT_SET(0xa2),
ARRAY_NEW(0xa3), ARRAY_LEN(0xa4), ARRAY_GET(0xa5), ARRAY_SET(0xa6);
ARRAY_NEW(0xa3), ARRAY_LEN(0xa4), ARRAY_GET(0xa5), ARRAY_SET(0xa6),
INDEX_GUARD(0xa7), ARRAY_CHECK_LENGTH(0xa8), ARRAY_COPY(0xa9), STORAGE_PUBLISH(0xaa);
private final int opcode;
Kind(int opcode) { this.opcode=opcode; }
public int opcode(){return opcode;}
}
public IRRegistryInstruction {
Objects.requireNonNull(kind);
if(member<0)throw new IllegalArgumentException("negative member index");
if(kind!=Kind.GET_CAPTURE)Objects.requireNonNull(type,"type");
if(member<0 || secondMember<0)throw new IllegalArgumentException("negative member index");
if(kind!=Kind.GET_CAPTURE && kind!=Kind.INDEX_GUARD)Objects.requireNonNull(type,"type");
if(kind!=Kind.STORAGE_PUBLISH && secondMember!=0)throw new IllegalArgumentException("unexpected second member");
if(kind==Kind.STORAGE_PUBLISH && member==secondMember)throw new IllegalArgumentException("publication fields must differ");
if(kind==Kind.MAKE_CLOSURE) {Objects.requireNonNull(function,"function");function.getIndex();}
else if(function!=null)throw new IllegalArgumentException("unexpected function operand");
boolean pair=kind.opcode()>=0x93&&kind.opcode()<=0x96;
boolean pair=(kind.opcode()>=0x93&&kind.opcode()<=0x96)||kind==Kind.STORAGE_PUBLISH;
if(pair)Objects.requireNonNull(targetType,"targetType");
else if(targetType!=null)throw new IllegalArgumentException("unexpected target type");
if(kind==Kind.GET_CAPTURE&&type!=null)throw new IllegalArgumentException("capture index is not a TypeId");
if((kind==Kind.GET_CAPTURE||kind==Kind.INDEX_GUARD)&&type!=null)throw new IllegalArgumentException("capture index is not a TypeId");
}
}
+40
View File
@@ -224,3 +224,43 @@ python3 tools/pbx-conformance/run.py --runtime ../runtime --generic-contracts-on
The full harness also requires these fixtures. The driver uses an external
runtime checkout without editing its sources or changing PBX, ISA or quotas.
## SDK List, shared Cursor and generic storage
Generate the source corpus and the language-neutral storage fixtures, then run:
```sh
./gradlew :prometeu-compiler:prometeu-build-pipeline:test :prometeu-architecture-tests:test :prometeu-app:installedKnowledgeTest
python3 tools/pbx-conformance/run.py --runtime ../runtime --list-cursor-only
```
The full harness also requires this corpus. Missing ordinary, optimized, quota,
GC, import/reexport or installed-JAR fixtures fail explicitly. Two synthetic
common-IR programs execute INDEX_GUARD, ARRAY_CHECK_LENGTH, ARRAY_COPY and
STORAGE_PUBLISH through ordinary and optimized producer paths without PBS types.
List source cases cover every mutator, exact growth, logical versus physical
bounds, int/long arguments (including named application), holes, aliases,
vec2/pixel/tuple/Fn/contract/reference groups and every state of a Cursor that remains on its last node.
An independent producer distinguishes some(none) from an empty Cursor.
Four source-generated mutation fixtures expose explicit before/completed markers.
The harness lowers GuestBytes, HandleSlots, AuxiliaryBytes and TemporaryBytes from
measured setup/success peaks. After each refused attempt it independently observes
all List fields and complete backing groups, including reference generations and
presence tags. Coverage gates require refusals in no-growth publication scratch,
removeAt optional return allocation, candidate allocation, node construction and
operand/frame preparation. Read-only resource diagnostics use non-owning storage
observations outside guest execution; no observation introduces a GC root or write.
The controlled safepoint fixture expects 1,200 nodes/objects initially, one after
traversal, one after repeated next on the final node, and zero after releasing the
Cursor. It also requires no live source/snapshot array. Shared payload fields stay
observable; prefix roots and the final payload obey normal reachability and GC.
The installed fixture is compiled by the isolated installDist JVM using the actual
packaged spec/example/SDK, then loaded and executed by this production VM harness.
Evidence prints compiler/provider source hashes, exact provider revision and dirty
state, toolchain, platform/profile, capabilities, every default VM ceiling and each
explicitly lowered refusal limit. Development runs against a dirty provider are
identified as such; final catalog verification requires its clean selected commit.
+201 -1
View File
@@ -36,6 +36,12 @@ impl NativeInterface for NoNative {
fn main() {
let root = std::path::PathBuf::from(std::env::args().nth(1).expect("fixture directory"));
use prometeu_vm::resource_budget::{RuntimeLimits,RuntimeResource as Q};
let limits=RuntimeLimits::default();
for resource in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes,Q::StringBytes,Q::StringAllocations,Q::IndividualStringBytes,Q::CallDepth] {
println!("DEFAULT VM LIMIT {resource:?}={}",limits.get(resource));
}
if std::env::args().any(|arg| arg == "--list-cursor-only") { verify_common_storage_fixtures(&root); verify_pbs_list_cursor_fixtures(&root); return; }
if std::env::args().any(|arg| arg == "--generic-contracts-only") { verify_pbs_generic_contract_fixtures(&root); return; }
if std::env::args().any(|arg| arg == "--generic-structs-only") { verify_pbs_generic_struct_fixtures(&root); return; }
if std::env::args().any(|arg| arg == "--random-only") { verify_pbs_random_fixtures(&root); return; }
@@ -47,6 +53,8 @@ fn main() {
verify_pbs_structured_fixtures(&root);
verify_pbs_generic_contract_fixtures(&root);
verify_pbs_generic_struct_fixtures(&root);
verify_common_storage_fixtures(&root);
verify_pbs_list_cursor_fixtures(&root);
verify_pbs_resource_refusals(&root);
verify_pbs_closure_frames(&root);
for name in [
@@ -483,7 +491,7 @@ fn verify_pbs_fixtures(root: &std::path::Path) {
for entry in std::fs::read_dir(root).unwrap() {
let path = entry.unwrap().path();
let name = path.file_stem().unwrap().to_str().unwrap();
if name.starts_with("pbs-random-") || name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || name.starts_with("pbs-generic-contract-") || name.starts_with("pbs-generic-struct-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
if name.starts_with("pbs-random-") || name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || name.starts_with("pbs-generic-contract-") || name.starts_with("pbs-generic-struct-") || name.starts_with("pbs-list-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
count += 1;
let bytes = std::fs::read(&path).unwrap();
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
@@ -1026,3 +1034,195 @@ fn verify_pbs_random_fixtures(root: &std::path::Path) {
println!("RANDOM PASS real SDK imports and aliases");
}
fn verify_pbs_list_cursor_fixtures(root: &std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End, resource_budget::{RuntimeLimits,RuntimeResource}};
use prometeu_bytecode::{TRAP_OOB,TRAP_RESOURCE_LIMIT};
let successful=["basic","construction","capacity","holes","structural-growth","cursor-empty","cursor-single",
"cursor-many","snapshot","false-present","shared-optional","identity","tuple","nested-array","function","evaluation","vec2","pixel","contract"];
let faults=[("fault-read",TRAP_OOB),("fault-write",TRAP_OOB),("fault-negative",TRAP_OOB),
("fault-insert",TRAP_OOB),("fault-length",TRAP_OOB),("fault-overflow",TRAP_RESOURCE_LIMIT),
("fault-long-index",TRAP_OOB),("fault-constructor",TRAP_OOB)];
let cases=successful.into_iter().map(|n|(n,None)).chain(faults.into_iter().map(|(n,c)|(n,Some(c))))
.flat_map(|(name,fault)|[(name.to_string(),fault),(format!("{name}-optimized"),fault)]);
for (name,fault) in cases {
let bytes=std::fs::read(root.join(format!("pbs-list-{name}.pbx")))
.unwrap_or_else(|e|panic!("missing mandatory SDK List/Cursor fixture {name}: {e}"));
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE)
.unwrap_or_else(|e|panic!("List/Cursor {name}: {e:?}"));
let mut vm=VirtualMachine::from_image(image,RuntimeLimits::default());
let observer=vm.resource_observer();
vm.prepare_boot_call().unwrap();
let report=vm.run_budget(1_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
if let Some(code)=fault {
let End::Trap(info)=report.reason else {panic!("{name}: {report:?}")};
assert_eq!(info.code,code,"{name}: {info:?}");
} else {
assert!(matches!(report.reason,End::Halted|End::EndOfRom),"{name}: {report:?}");
assert_eq!(vm.operand_stack_top(1),vec![Value::Int32(42)],"{name}");
}
drop(vm);
for resource in [RuntimeResource::GuestBytes,RuntimeResource::HandleSlots,RuntimeResource::AuxiliaryBytes,
RuntimeResource::TemporaryBytes,RuntimeResource::StringBytes,RuntimeResource::StringAllocations] {
assert_eq!(observer.used(resource),0,"{name}: final owner release {resource:?}");
}
println!("PBS LIST/CURSOR SOURCE/LOAD/EXEC PASS {name}");
}
verify_list_cursor_refusals(root);
verify_list_cursor_gc(root);
verify_list_cursor_distribution(root);
}
fn list_storage(vm:&VirtualMachine,image:&ValidatedProgramImage)->(prometeu_vm::resource_diagnostics::StorageObject,prometeu_vm::resource_diagnostics::StorageObject) {
use prometeu_vm::resource_diagnostics::StorageValue as V;
let objects=vm.heap().diagnostic_typed_storage();
let object=objects.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("list<int>"))
.expect("one source List remains observable").clone();
let V::Reference(backing)=object.groups[0][0] else {panic!("List storage reference")};
let array=objects.iter().find(|o|o.reference==backing).unwrap().clone();
(object,array)
}
fn verify_list_cursor_refusals(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End, resource_budget::{RuntimeLimits,RuntimeResource as Q}};
use prometeu_vm::resource_diagnostics::StorageValue as V;
use prometeu_bytecode::{decode_next,TRAP_RESOURCE_LIMIT};
use prometeu_bytecode::isa::core::CoreOpCode as Op;
let mut publication_scratch=false;let mut return_carrier=false;let mut nodes=false;let mut candidate=false;let mut operand_preparation=false;
for name in ["insert","remove","growth","cursor"] {
let bytes=std::fs::read(root.join(format!("pbs-list-refusal-{name}.pbx"))).expect("mandatory refusal fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let marker=|value:i32| {
let mut pc=0;let mut found=Vec::new();
while pc<image.code().len() {let instruction=decode_next(pc,image.code()).unwrap();
if instruction.opcode==Op::PushI32&&instruction.imm_i32().unwrap()==value {found.push(pc);}
pc=instruction.next_pc;
}
assert_eq!(found.len(),1,"one explicit test marker");found[0]
};
let before_pc=marker(314160);let after_pc=marker(314161);
let prepare=|limits| {
let mut vm=VirtualMachine::from_image(image.clone(),limits);vm.prepare_boot_call().unwrap();
vm.insert_breakpoint(before_pc);vm.insert_breakpoint(after_pc);vm
};
let mut baseline=prepare(RuntimeLimits::default());
assert!(matches!(baseline.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap().reason,End::Breakpoint));
let before=baseline.resource_snapshot();let original=list_storage(&baseline,&image);
baseline.remove_breakpoint(before_pc);
assert!(matches!(baseline.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap().reason,End::Breakpoint));
let completed=baseline.resource_snapshot();let changed=list_storage(&baseline,&image);
let expected_size=match name {"insert"|"growth"=>129,"remove"=>127,_=>128};
assert_eq!(changed.0.groups[1],[V::Int64(expected_size)],"{name} logical size after success");
if name=="remove" {assert_eq!(changed.1.groups[12][1],V::Int32(13));assert_eq!(changed.1.groups[127],[V::Null,V::Null]);}
if name=="insert" {assert_eq!(changed.1.groups[12][1],V::Int32(42));assert_eq!(changed.1.groups[13][1],V::Int32(12));}
if name=="growth" {assert_eq!(changed.1.groups.len(),192);assert_eq!(changed.1.groups[128][1],V::Int32(42));}
if name=="cursor" {assert_eq!(changed,original);}
let mut refusals=0;
for q in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes] {
let lower=before.budget.peaks[q as usize];let upper=completed.budget.peaks[q as usize];
if lower>=upper {continue;}
let mut limits=vec![lower,lower+(upper-lower)/4,lower+(upper-lower)/2,upper-1];
if q==Q::HandleSlots {limits.extend(lower..upper.min(lower+16));}
limits.sort_unstable();limits.dedup();
for limit in limits {
let mut vm=prepare(RuntimeLimits::default().lowered(q,limit).unwrap());
let observer=vm.resource_observer();
let setup=vm.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(setup.reason,End::Breakpoint),"{name} setup at {q:?}={limit}: {setup:?}");
let state=list_storage(&vm,&image);
vm.remove_breakpoint(before_pc);
let report=vm.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
if let End::Trap(info)=report.reason {
assert_eq!(info.code,TRAP_RESOURCE_LIMIT,"{name} at {q:?}={limit}: {info:?}");
assert_eq!(list_storage(&vm,&image),state,"{name}: no partial slots/backing/size at {q:?}={limit}");
let instruction=decode_next(info.pc as usize,image.code()).unwrap();
publication_scratch|=q==Q::TemporaryBytes&&instruction.opcode==Op::StoragePublish;
operand_preparation|=q==Q::AuxiliaryBytes;
candidate|=name=="growth"&&instruction.opcode==Op::ArrayNew;
if instruction.opcode==Op::StructNew {
let ty=instruction.imm_u32().unwrap();
let declaration=&image.registry().declarations().types[ty as usize];
nodes|=name=="cursor"&&declaration.name.as_deref().is_some_and(|n|n.starts_with("_SnapshotNode<"));
if name=="remove" {
if let prometeu_bytecode::registry::TypeKind::Struct(fields)=&declaration.kind {
return_carrier|=fields.len()==1&&matches!(image.registry().declarations().types[fields[0].type_id.0 as usize].kind,
prometeu_bytecode::registry::TypeKind::Builtin(prometeu_bytecode::registry::BuiltinId::INT32));
}
}
}
refusals+=1;
println!("PBS LIST ATOMIC REFUSAL {name} resource={q:?} limit={limit} opcode={:?}",instruction.opcode);
} else {assert!(matches!(report.reason,End::Breakpoint),"{name}: {report:?}");}
drop(vm);
for resource in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes] {assert_eq!(observer.used(resource),0);}
}
}
assert!(refusals>0,"{name}: mandatory source refusals executed");
}
assert!(publication_scratch,"no-growth publication scratch refusal");
assert!(return_carrier,"removeAt optional return allocation refusal");
assert!(candidate,"growth candidate allocation refusal");
assert!(nodes,"snapshot node allocation refusal");
assert!(operand_preparation,"source operation operand/frame preparation refusal");
}
fn verify_list_cursor_gc(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::RuntimeLimits};
use prometeu_vm::resource_diagnostics::StorageValue as V;
let bytes=std::fs::read(root.join("pbs-list-gc.pbx")).expect("mandatory snapshot GC fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),RuntimeLimits::default());vm.prepare_boot_call().unwrap();
let mut previous=0;
for (round,(nodes,items)) in [(1200,1200),(1,1),(1,1),(0,0)].into_iter().enumerate() {
let report=vm.run_budget(30_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(report.reason,End::FrameSync),"snapshot GC {round}: {report:?}");
let snapshot=vm.resource_snapshot();assert!(snapshot.gc.collections>previous);previous=snapshot.gc.collections;
let objects=vm.heap().diagnostic_typed_storage();
assert!(!objects.iter().any(|o|matches!(image.registry().declarations().types[o.type_id.0 as usize].kind,
prometeu_bytecode::registry::TypeKind::Array(_))),"Cursor retains no source or snapshot array");
let named=|name:&str|objects.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some(name)).collect::<Vec<_>>();
assert_eq!(named("_SnapshotNode<Item>").len(),nodes,"prefix nodes round {round}");
let values=named("Item");assert_eq!(values.len(),items,"captured object roots round {round}");
if items==1 {assert_eq!(values[0].groups[0],[V::Int32(1199)],"sticky last payload remains live");}
println!("PBS CURSOR GC PASS round={round} nodes={nodes} items={items} collected={}",snapshot.gc.reclaimed);
}
let report=vm.run_budget(100_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(report.reason,End::Halted|End::EndOfRom));assert_eq!(vm.operand_stack_top(1),vec![Value::Int32(42)]);
}
fn verify_common_storage_fixtures(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::RuntimeLimits};
for name in ["common-storage","common-storage-optimized"] {
let bytes=std::fs::read(root.join(format!("{name}.pbx"))).expect("mandatory non-PBS storage fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image,RuntimeLimits::default());vm.prepare_boot_call().unwrap();
let report=vm.run_budget(100_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(report.reason,End::Halted|End::EndOfRom),"{name}: {report:?}");
assert_eq!(vm.operand_stack_top(1),vec![Value::Int32(42)]);
println!("COMMON STORAGE SOURCE/LOAD/EXEC PASS {name}");
}
}
fn verify_list_cursor_distribution(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::RuntimeLimits};
for name in ["imports","installed"] {
let bytes=std::fs::read(root.join(format!("pbs-list-{name}.pbx"))).expect("mandatory workspace/installed List fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),RuntimeLimits::default());vm.prepare_boot_call().unwrap();
let report=vm.run_budget(1_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(report.reason,End::FrameSync|End::Halted|End::EndOfRom),"{name}: {report:?}");
use prometeu_vm::resource_diagnostics::StorageValue as V;
let objects=vm.heap().diagnostic_typed_storage();
let cursor=objects.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_SnapshotCursor<int>"))
.expect("distributed frame actually created its shared snapshot Cursor");
let V::Reference(current)=cursor.groups[0][0] else {panic!("nonempty Cursor retains its current node carrier")};
let carrier=objects.iter().find(|o|o.reference==current).unwrap();
let V::Reference(node)=carrier.groups[0][0] else {panic!("current points at a snapshot node")};
let node=objects.iter().find(|o|o.reference==node).unwrap();
assert_eq!(node.groups[0],[V::Int32(22)],"{name}: complete traversal retains the final value");
assert_eq!(node.groups[1],[V::Null],"{name}: final node has no successor");
println!("PBS LIST/CURSOR DISTRIBUTION LOAD/EXEC PASS {name}");
}
}
+7 -1
View File
@@ -15,6 +15,7 @@ parser.add_argument("--release", action="store_true", help="Execute an optimized
parser.add_argument("--math-only", action="store_true", help="Run the complete PBS math corpus only")
parser.add_argument("--random-only", action="store_true", help="Run the complete PBS Random corpus only")
parser.add_argument("--generic-contracts-only", action="store_true", help="Run the mandatory generic-contract source corpus only")
parser.add_argument("--list-cursor-only", action="store_true", help="Run the mandatory SDK List/Cursor corpus only")
parser.add_argument("--generic-structs-only", action="store_true", help="Run the mandatory generic-struct/function source corpus only")
args = parser.parse_args()
runtime = args.runtime.resolve()
@@ -29,6 +30,11 @@ for source in [pathlib.Path(__file__), pathlib.Path(__file__).with_name("main.rs
pathlib.Path("prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/random/main.pbs")]:
print("sha256", source, hashlib.sha256(source.read_bytes()).hexdigest(), flush=True)
print("runtime", subprocess.check_output(["git", "-C", str(runtime), "rev-parse", "HEAD"], text=True).strip(), flush=True)
print("runtime working tree", subprocess.check_output(["git","-C",str(runtime),"status","--short"],text=True).strip() or "clean",flush=True)
for source in [pathlib.Path("prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsListCursorTest.java"),
pathlib.Path("prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/main.pbs"),
runtime / "crates/console/prometeu-vm/src/virtual_machine/storage_publication.rs"]:
print("sha256",source,hashlib.sha256(source.read_bytes()).hexdigest(),flush=True)
print("platform", platform.platform(), "limits=default capabilities=NONE", flush=True)
print("profile", "release" if args.release else "dev", "features=resource-diagnostics", flush=True)
for relative in ["Cargo.lock", "docs/vm-arch/INTRINSICS.csv", "docs/vm-arch/BUILTIN_CONSTANTS.csv", "crates/console/prometeu-vm/tests/fixtures/numeric-conformance.tsv", "crates/console/prometeu-vm/tests/fixtures/random-conformance.tsv"]:
@@ -44,4 +50,4 @@ with tempfile.TemporaryDirectory(prefix="pbx-conformance-") as directory:
(root / "Cargo.lock").write_text((runtime / "Cargo.lock").read_text())
(root / "src").mkdir()
(root / "src/main.rs").write_text(pathlib.Path(__file__).with_name("main.rs").read_text())
subprocess.run(["cargo", "run", *(["--release"] if args.release else []), *([] if args.online else ["--offline"]), "--manifest-path", str(root / "Cargo.toml"), "--", str(args.fixtures.resolve()), str(runtime / "devtools/headless-protocol/response.schema.json"), *(["--math-only"] if args.math_only else []), *(["--random-only"] if args.random_only else []), *(["--generic-contracts-only"] if args.generic_contracts_only else []), *(["--generic-structs-only"] if args.generic_structs_only else [])], check=True)
subprocess.run(["cargo", "run", *(["--release"] if args.release else []), *([] if args.online else ["--offline"]), "--manifest-path", str(root / "Cargo.toml"), "--", str(args.fixtures.resolve()), str(runtime / "devtools/headless-protocol/response.schema.json"), *(["--math-only"] if args.math_only else []), *(["--random-only"] if args.random_only else []), *(["--generic-contracts-only"] if args.generic_contracts_only else []), *(["--generic-structs-only"] if args.generic_structs_only else []), *(["--list-cursor-only"] if args.list_cursor_only else [])], check=True)