Compare commits

..
3 Commits
Author SHA1 Message Date
bquarkz 13c0ababbb feat(pbs): align typed values and Pad input facade
Test / Build occurred while executing withChecks step.
Intrepid/Prometeu/Studio/pipeline/pr-master There was a failure building this commit
Intrepid/Prometeu/Studio/pipeline/head There was a failure building this commit
2026-10-15 15:48:50 +01:00
bquarkz 603ed01227 feat(compiler): port runtime catalogs and enforce target ABI (AGD77)
Import both runtime catalogs and provenance, port all intrinsic metadata and exact constants, and validate target signatures and carriers through the common pipeline. Package matching compiler/SDK resources and add active parity plus production runtime conformance checks.

Validation: 420 compiler/PBS tests passed, with 12 existing skips; provider export, runtime execution and installed catalog checks passed. The pre-existing Gfx.clear typed-metadata failure remains active and assigned to AGD78 under the explicitly accepted scope revision recorded in LSN-0080.
2026-10-15 13:07:49 +01:00
bquarkz bd5694dbdf feat(compiler): implement typed registry and production PBX conformance (AGD76) 2026-10-14 21:04:04 +01:00
119 changed files with 4890 additions and 1153 deletions
+2 -2
View File
@@ -189,8 +189,8 @@ acknowledge completed cleanup; control stays locked until cleanup is confirmed.
The following findings are recorded for investigation after implementation; they
are not considered fixed by the passing integration tests:
- **PBS/LSP SDK navigation:** definition lookup for the bundled SDK's `Input.pad()`
currently returns no location. Project-source navigation and indexed SDK reference
- **PBS/LSP SDK navigation:** the historical probe of the former `Input.pad()`
API returned no location; the current input API is `Pad`/`Button` and `Touch`. Project-source navigation and indexed SDK reference
reads work. Investigate SDK symbol resolution separately; an empty definition
result is not evidence of missing runtime support or permission to edit the SDK.
- **Build tooling:** the validation environment's JDK 25 triggers Kotlin's target-24
@@ -0,0 +1,145 @@
---
id: LSN-0079
ticket: compiler-pbx-registry-conformance
title: Typed common IR through production PBX admission
created: 2026-10-14
tags: [runtime-integration]
---
## Original problem and ownership
Owner: compiler/general; interfaces: compiler/pbs and studio.
The old producer carried parameter/result slot counts without complete semantic
types and emitted a retired PBX function layout. A writer-only repair could not
recover signatures, distinguish nominal identities or describe closures and
trait implementations. The fix had to cross the entire common producer chain.
This lesson consolidates AGD-0076, accepted DEC-0063 and completed PLN-0142
(DSC-0074). Those workflow artifacts were removed by housekeeping after their
normative content and evidence were propagated. The active integration umbrella
is [AGD75](../../workflow/agendas/AGD-0075-studio-runtime-readiness.md).
## Consolidated decisions
Use stable declaration references before assigning deterministic dense image
IDs. Preserve Struct/Trait nominal identity; canonicalize Builtin imports,
Array elements and Fn signatures. Names are descriptive, not nominal keys.
Parameters, results, captures, implementations and lifecycle roles are explicit
common values, preserved through IRVM, optimization and emission.
The common IR remains serializable by design, but a concrete codec was
explicitly deferred. Complete representation does not require selecting a
transport format. References avoid recursive Java object graphs and the public
contract audit rejects mutable or frontend-specific shapes.
PBS is one frontend. Optional and Result already belong to PBS's normative
language contract and have parser/semantic implementations. Defects in their
executable lowering do not justify new backend categories or rejection rules.
Their implementation and carrier choices remain frontend-owned. The user
explicitly scoped consumer migration to subsequent tickets and a single AGD75
integrated delivery. No PBS semantics were added to this backend change.
## Final implementation
- IRRegistry and FunctionMetadata represent all five type categories, ordered
signatures/captures, roles and implementations. Aggregation remaps callable
references while preserving stable type identity.
- LinkedRegistry canonicalizes imports/structural declarations deterministically
with an iterative dependency traversal. Nominal anchors permit recursion;
purely structural cycles reject.
- Lowering derives widths from semantic signatures, links typed operands and
remaps implementation targets. IRVM coherence and optimization preserve the
Registry and function metadata.
- The PBX writer emits the 32-byte header and all eight mandatory sections,
optional debug, strict UTF-8, exact function records and explicit empty capture
environments. Width caches are not serialized. Constant deduplication now
relocates PUSH_CONST indices rather than silently changing their meanings.
- Local verification decodes all function ranges, validates references and
operand categories, forbids retired/raw opcodes and analyzes reachable stack
and type facts to convergence. Conflicts become Unknown; exact semantic groups
retain provenance when available. Unknown remains subject to runtime checks.
- Production loading remains authoritative for native identity/capability/ABI,
runtime budgets and executable admission.
- Studio previously discarded error.details when a headless response rejected
boot. AutomationFailure now preserves the entire runtime error envelope,
including initialization, binding and resource details, through MCP output.
## Examples and pitfalls
A `vec2 -> vec2` function has logical arity one/one and derived slot widths two/two.
The function record writes the two TypeId lists, not the numbers two/two. A test
compares that record with independent fixed bytes from the runtime contract.
A capturing body is not a direct-call target. MAKE_CLOSURE names the body and
expected Fn type, consumes its explicit capture groups and produces a callable.
CALL_CLOSURE consumes that callable after the explicit arguments. The synthetic
frontend test proves this path without PBS source, SDK or language ASTs.
Jumps contain absolute offsets from the current function start. They are not
relative to the next PC. A taken jump to function end is a return boundary;
ordinary fallthrough is invalid. These details were checked against the actual
runtime verifier, not inferred from older emitter behavior.
Do not derive type identity from slot width, spelling or display names. Do not
claim a self-round-trip proves external conformance. Do not treat parser tests
as execution evidence or a load-only argument-taking entry as a boot test.
Initialization diagnostics have null outer executed PC. A verifier PC remains
nested in initError.load.pc; file offsets remain offsets. Preserve the original
JSON structure rather than flattening it into human-readable messages.
## Qualification evidence
Runtime: `f7d7c167531bb662a1af9d73fc326bb476bf8aa7`.
Platform: macOS 27.0.1 arm64. Rust harness uses the runtime Cargo.lock, default
load limits and NONE capabilities except the positive native case (SYSTEM).
Resource-negative cases explicitly lower Types, TemporaryBytes or Work.
Runtime checkout remained unchanged.
| Gate | Result |
| --- | --- |
| Build-pipeline suite | 178 tests: 163 passed, 15 skipped, no failures |
| Frontend-api suite | 28 passed |
| PBS suite | 245 passed; not a claim of complete executable migration |
| Headless process suite | 9 passed, including six observed runtime fault payloads |
| Architecture suite | 17 passed |
| Independent binary oracle | Exact 194-byte minimal image and fixed vec2 function bytes |
| Production loader positives | 12 admitted images, including native binding |
| Genuine execution | 11 programs: direct/common path, closures/captures, trait, Array/Dynamic, joins, roles, intrinsic and native; vec2 signature is load-only |
| Production negative corpus | 32 rejected malformed/binding/code/resource fixtures |
| Workflow and patch integrity | discussion validate and git diff --check passed |
The 15 skipped tests are older conditional integration adapters. The dedicated
production harness above ran explicitly; their skip state is not substituted
for runtime evidence. The intrinsic/native catalogue goldens are serialization
fixtures and do not claim current PBS catalogue compatibility.
Reproduce with the commands in
[the harness README](../../../tools/pbx-conformance/README.md). The harness
creates its Cargo build outside the runtime checkout and saves generated PBX and
observed JSON under the build-pipeline build directory.
## Remaining integration work
The runtime LoadReason enum has four values missing from the published headless
schema: LifecycleRole, LifecycleSignature, DuplicateLifecycleRole and
NonFiniteValue. The corpus observes the first and last. Both payloads fail the
original schema solely because of the missing enum value. The harness explicitly
reports this upstream gap, validates the original payload with that single enum
addition, and leaves the payload unchanged. It does not claim those two cases
conform to the unmodified schema. Runtime must correct the schema under AGD75.
PBS typed producer migration, Optional/Result executable lowering, catalogue
alignment, installed runtime delivery, hook selection, memcard gating and
persistence remain with their consumer tickets. The common schema and synthetic
producer work without those features. There is no legacy PBX fallback and no
slot-to-type inference to conceal an incomplete handoff.
## References
- [Typed common Registry/PBX specification](../../../docs/compiler/general/specs/typed-registry-and-pbx-v0.md)
- [Backend conformance matrix](../../../docs/specs/compiler/22.%20Backend%20Spec-to-Test%20Conformance%20Matrix.md)
- [Runtime qualification harness](../../../tools/pbx-conformance/README.md)
- Runtime sources: registry_v0.rs, program_image.rs, registry_verifier, and
devtools/headless-protocol/response.schema.json at the revision above.
@@ -0,0 +1,182 @@
---
id: LSN-0080
ticket: compiler-runtime-catalog-parity
title: Manual runtime catalog port and independent conformance
created: 2026-10-15
tags: [runtime-integration]
---
## Original problem and ownership
Owner: `compiler/general`; consumers: `compiler/pbs`, `vm-arch` and `studio`.
This lesson consolidates AGD-0077, accepted DEC-0064 and completed PLN-0143
(DSC-0075), including the user's explicit acceptance revision on 2026-10-15.
Their workflow files were removed after propagation and CLI housekeeping.
The runtime published 105 intrinsics with 18 fields and 15 builtin constants
occupying 16 slots. Studio had 42 intrinsic records with 12 fields, no constant
mirror and a disabled parity test. Copying the CSV could not update the compiler's
manual ABI representation, reject retired endpoints or preserve new effects.
Against the previous Studio catalog, the port added 82 identities, removed 19
and updated 23 retained identities.
## Consolidated decision
The runtime owns identities, layouts, constant values, effects and costs.
Studio imports both CSVs unchanged at an explicit target revision, inventories
the differences, then manually ports the compiler representation and consumers.
`IRVMIntrinsicDefinition` represents the runtime supported by this compiler
version. Normal compilation uses local versioned code/resources; it neither
queries the latest runtime nor depends on a sibling checkout.
Manual maintenance keeps compiler adaptations explicit and reviewable, at the
cost of maintaining a port. Active comparisons against independently exported
CSV data detect drift. Enum generation, a shared implementation library and
moving the backend were not required. An explicit provider check uses runtime
source/tests to establish behavior and export provenance. A different runtime
commit is not itself proof of incompatibility; revision and hashes identify
the evidence target without introducing version negotiation.
Read dispatch, verifier and execution tests as well as CSVs. CSV fields describe
the published shape but cannot explain all null handling, native-reference
validation, traps or charging behavior.
## Final implementation
- `IRVMIntrinsicDefinition` and `IRVMIntrinsicRegistry` cover all 105 entries and
18 published fields. Exact name/version resolution and ID lookup reject
retired identities; old IDs are not compatibility aliases.
- `IRVMBuiltinConstantDefinition` independently groups all 15 constants by
target/name/version, preserving ordered slots and exact values/bits. Its
materialization supplies common bytecode constant values.
- Lowering checks caller counts against the selected definition. IRVM validation,
emission and bytecode precheck reject unknown IDs and inconsistent signature
caches, including unknown IDs in unreachable code. Module-only IRVM emission
derives intrinsic signatures from the same target definitions.
- Explicit carrier mappings distinguish Int32, Int64, Float, Bool, String,
color and Random. Unsupported tokens fail. Static facts do not claim to prove
native handle identity or session validity; the runtime remains authoritative.
- Compiler and PBS SDK JARs package both CSVs and provenance from checked-in
mirrors at `runtime-catalog/compiler/` and `runtime-catalog/pbs-sdk/`.
Installed-resource tests validate actual JAR bytes/hashes outside the checkout.
- Active parity tests, synthetic common-IR behavior and the production runtime
harness provide separate evidence. No new PBS facade or numeric/random
algorithm was implemented in this common backend work.
## Examples and pitfalls
**A retained name can have a different ABI.** `input.touch.x/y` moved to
`0x2134/0x2135`, lost their receivers and return Int64. Resolving the name alone
is insufficient; stale caller counts must fail. Input metadata coverage does
not mean the existing PBS input syntax already lowers to these direct queries.
**Constants are not generic zero-argument intrinsics.** `mathf.pi` and
`mathf.tau` materialize Float bits `0x400921FB54442D18` and
`0x401921FB54442D18`. `vec2.zero` materializes two Float slots and has no
intrinsic ID. Some color names independently have both mechanisms, which does
not establish a rule for other constants. Pool deduplication may share the
zero value but must preserve both emitted vec2 components.
**Determinism is not purity.** All four Random intrinsics mutate guest state;
only `random.new` allocates. The tested common program aliases one generator,
discards a `next` result, then mixes `nextf`, `range` and `next`. Removing the
discarded call changes the sequence. Optimized and unoptimized executions
matched the independent seed-42 vector and each used 100 cycles/20 steps.
`random.range` preserves base cost 4, cost 8 per attempt and limit 64. The VM
performs charging; storing metadata does not create a second Java scheduler.
**Static and dispatch rules can differ.** Runtime `abi_mask` permits String
only for intrinsic String arguments, while `value_matches_abi_type` accepts
String or null in dispatch. The Studio precheck therefore rejects a proven
null at load time. The production loader confirmed that rejection with
`LOAD_INVALID_CODE/TypeMismatch`; a String `#AABBCCDD` executed to color
`0xAABBCCDD`. Random's static reference mask includes null, leaving native
handle validity to runtime checks.
Do not use a serializer of the Java enum as its own expected catalog, infer
carrier identity from slot width, disable stale fixtures to hide regressions,
or equate JAR resource parity with successful PBS or installed-host execution.
## Qualification evidence
Selected runtime: `f7d7c167531bb662a1af9d73fc326bb476bf8aa7`, clean checkout.
Host: macOS 27.0.1 arm64; JDK 25 for Studio build and JDK 21 for the installed
launcher. Provider exporter used Cargo test profile/default features; the
production VM harness used dev profile/default features and default load
limits. New catalog fixtures used NONE capabilities. Runtime was not modified.
| Artifact | SHA-256 |
| --- | --- |
| INTRINSICS.csv | `0c1bb78a9b81d1e3480ef5713f3d70f0b06772a7dfab2fd00c1f0a6364f98daa` |
| BUILTIN_CONSTANTS.csv | `e407e4718127aa7ef7b1097b5b4b67d86dd979235e73a551a7c36b628cddc039` |
| Check | Result |
| --- | --- |
| Build-pipeline suite | 187 tests: 175 passed, 12 existing skips, zero failures |
| PBS suite | 245 passed, zero failures; no claim of executable PBS migration |
| Normal catalog parity | All fields, identities, constant bits/groups and packaged hashes passed |
| Explicit provider check | Committed-source/mirror/hash comparison and real runtime exporter test passed; missing provider failed as required |
| Catalog execution corpus | Random with alias/discarded mutation, exact constants, vec2.zero and String conversion passed on the actual VM |
| Admission/availability negatives | Removed unreachable ID and proven-null String rejected; known input loaded then trapped UNAVAILABLE without hardware |
| Installed catalog gate | Passed using actual compiler/SDK JAR resources and bundled launcher outside the checkout |
| Full installed example gate | Failed before and after the port: missing typed metadata for Gfx.clear; explicit consumer handoff and acceptance revision below |
| Workflow/patch integrity | `discussion validate` and `git diff --check` passed |
The complete existing PBX harness corpus also passed with its already documented
runtime schema exceptions: LifecycleRole and NonFiniteValue remain absent from
the published fault-schema enum. The harness reports these gaps and preserves
payloads; this port does not claim the upstream schema was corrected.
Reproduce the catalog and consumer checks from the repository root:
```sh
./gradlew :prometeu-compiler:prometeu-build-pipeline:test :prometeu-compiler:frontends:prometeu-frontend-pbs:test --console=plain
./gradlew :prometeu-app:installedKnowledgeTest --tests '*installedCompilerAndSdkCarryTheSameVerifiedCatalog' --console=plain
python3 tools/runtime-catalog/verify.py --runtime ../runtime
python3 tools/pbx-conformance/run.py --runtime ../runtime
```
## Accepted boundary and remaining consumer work
The full `:prometeu-app:installedKnowledgeTest` failed with
`LOWER_IRVM_INVALID_TYPED_REGISTRY: typed function metadata required: Gfx.clear`.
The same failure was reproduced on an independent archive of previous Studio
HEAD `bd5694dbdf5b214b4295fc8e01ddbe0ba5479599`; only diagnostic stdout was
enabled in its test probe to reveal the exact existing error.
On 2026-10-15 the user explicitly approved concluding catalog acceptance and
leaving that correction with AGD-0078. DEC-0064 and PLN-0143 recorded this precise
revision before housekeeping: the installed catalog probe is this delivery's
mandatory installed gate; the full example test remains active and failing.
All catalog, common behavior, affected module and provider-execution checks
remain required and passed. No general waiver for future failures was granted.
- AGD-0078 owns PBS typed metadata, existing input/types and vec2 source
materialization. Correct the Gfx.clear producer path and rerun the full
installed example gate; do not weaken the common typed Registry contract.
- AGD-0083 owns math/mathf facades, overloads and source constant exposure.
`PbsExecutableBodyLowerer.emitBuiltinConstFetch` still assumes an intrinsic
with one return; pi/tau require the independent constant materialization path.
- AGD-0084 owns PBS Random handles, storage, aliasing and source replay tests.
Common-IR execution provides a base, not proof of the future PBS facade.
- AGD-0080 owns actual installed-runtime Build/Play/headless qualification.
Catalog resources identify the compiler target without proving that flow.
These handoffs and the explicit acceptance revision are recorded in the active
agendas and [integration umbrella](../../workflow/agendas/AGD-0075-studio-runtime-readiness.md).
Future catalog updates must review source, imports, manual consumers, provenance
and behavior together. Roll back that set together if necessary.
## References
- [Normative catalog support](../../../docs/compiler/general/specs/runtime-catalog-support.md)
- [Typed common Registry/PBX contract](../../../docs/compiler/general/specs/typed-registry-and-pbx-v0.md)
- [VM mirror and provider verification policy](../../../docs/vm-arch/README.md)
- [Production runtime harness](../../../tools/pbx-conformance/README.md)
- Runtime sources at the recorded revision: catalog.rs, builtins.rs,
input_intrinsics.rs, math_intrinsics.rs, random_intrinsics.rs, random.rs,
registry_verifier/{heights,types}.rs and tests/numeric_contract.rs.
Workflow tooling note: the installed CLI registers an existing lesson but has
no lesson-allocation command. LSN-0080 was selected by reading the next counter;
`discussion housekeep` performed all index mutations and counter advancement.
@@ -0,0 +1,83 @@
---
id: LSN-0081
ticket: pbs-existing-types-input-alignment
title: PBS Typed Values and Direct Input Queries
created: 2026-10-15
tags: [compiler/pbs, runtime-integration]
---
## Context and result
Owner: **compiler/pbs**, with shared compiler IR and installed Studio consumer propagation. DSC-0076 (AGD-0078, accepted DEC-0065, completed PLN-0144) aligned existing PBS values and input with the published runtime. The original object chain exposed intermediate input objects that had no useful runtime representation. PBS also omitted exact executable function metadata, causing the full installed example to fail at `Gfx.clear` under the shared typed registry validator.
The delivered implementation replaces those chains, produces typed function metadata, preserves complete value groups, and passes both actual-runtime execution and the full installed knowledge gate. The completed agenda, decision and plan were removed through framework housekeeping; this lesson preserves their implementation evidence.
## Public selectors and direct runtime endpoints
Use a nominal selector value and a public query:
```pbs
let button = Button.A;
let pressed = Pad.pressed(button);
let frames: long = Pad.hold(button);
let x: long = Touch.x();
```
`Button` has twelve cases: Up, Down, Left, Right, A, B, X, Y, L, R, Start, Select. The four Pad operations are pressed, released, down and hold. Each public function dispatches through an exhaustive enum switch to one of the 48 zero-argument LowPad endpoints. For example, `Pad.pressed(Button.A)` reaches `LowPad.a_pressed()`, bound to `input.pad.a.pressed`. The selector is evaluated once and only the selected endpoint reads input. Obtaining or passing a Button has no input effect. Constant specialization is unnecessary for correctness.
Touch exposes pressed/released/down as Bool and hold/x/y as Int64. The implementation uses existing runtime endpoints, with no ABI or lifecycle redesign. `Input.pad().a()` and aliases of its intermediate object are removed; import diagnostics explain the replacement. Strings, integers and unrelated enums cannot substitute for Button. The source type remains nominal even though its executable carrier is Int32.
## Exact values must survive frontend lowering
The explicitly accepted integer mapping is `int` to Int32 and `long` to Int64. Integer literals default to int; an explicit long context admits Int64 literals. Conversions use existing `as` syntax. Variables of different numeric widths require explicit conversion. Pad.hold and Touch.hold/x/y therefore return long.
`PbsRuntimeTypes` connects source types and declarations to canonical registry types and exact parameter/result groups. Callable lookup includes arity so overloaded SDK functions do not overwrite each other's signatures. Existing unsupported source types retain their previous executable boundary; shared strict metadata validation was not relaxed to admit them.
Locals, parameters, calls and returns carry whole groups: vec2 uses two Float slots and pixel uses two Int64 coordinates plus color. Slot count alone never establishes type identity. Builtin constants are resolved through the shared constant catalog: vec2.zero materializes two Float zero values, without inventing a one-result intrinsic. The backend validates the declared group width against the catalog materialization.
Named application first evaluates expressions in source order, stores their complete groups, and then loads those values in parameter order. The runtime proof calls `combine apply (y: Probe.y(), x: Probe.x())`: effects record Y then X while combine receives x then y. Its final oracle is 211020.
## Qualification evidence
Qualification date: 2026-10-15; platform: Darwin arm64. Studio base revision: `603ed012272b137361ba618f6887f015f7317803` plus this delivery's working-tree changes. Runtime revision: `f7d7c167531bb662a1af9d73fc326bb476bf8aa7`. The runtime checkout was not modified. Default Gradle configuration; controlled input and no-platform configurations in the real runtime harness.
```sh
./gradlew :prometeu-compiler:frontends:prometeu-frontend-pbs:test :prometeu-compiler:prometeu-build-pipeline:test :prometeu-compiler:prometeu-frontend-api:test :prometeu-lsp:prometeu-lsp-v1:test :prometeu-app:installedKnowledgeTest --console=plain
CARGO_TARGET_DIR=/tmp/pln144-cargo-target python3 tools/pbx-conformance/run.py --runtime ../runtime
git diff --check
discussion validate
```
| Suite | Passed | Existing skips | Failures/errors |
| --- | ---: | ---: | ---: |
| PBS frontend | 246 | 0 | 0 |
| Build pipeline | 179 | 12 | 0 |
| Frontend API | 28 | 0 | 0 |
| LSP | 38 | 1 | 0 |
| Full installed knowledge | 2 | 0 | 0 |
`PbsRuntimeAlignmentTest` generates 114 PBX programs through the production compiler, lowerer, emitter and preload verifier. The actual runtime harness loads and executes all of them:
- 48 Pad combinations using a local Button passed through a typed function, plus 48 direct constant combinations; controlled input asserts the exact returned value and exactly one selected read.
- All six Touch queries, including signed x/y values. All 102 input programs are also executed without a platform and must trap with TRAP_UNAVAILABLE.
- Twelve value/fault programs cover Int32, Int64, Float, String, color, vec2, pixel, tuple returns, named evaluation order, Int32 wrapping, checked narrowing and division by zero. Bool is covered by input queries. Registry default layouts are checked for all eight existing canonical value types.
Frontend negatives cover invalid Button arguments, mixed int/long variables, literal overflow and migration diagnostics. Existing shared tests continue to cover wrong typed signatures, stale touch signatures, unreachable removed IDs, and lifecycle assembly. The original full installed failure at `Gfx.clear` is fixed. This does not claim completion of the separate Studio desktop integration agenda. Existing upstream fault-schema gaps (LifecycleRole/NonFiniteValue) remain explicitly reported by the shared harness, without suppressing this delivery's oracles.
## Pitfalls and reusable patterns
- A dotted builtin target is already a complete catalog name: use `input.pad.a.pressed`, not a partial `a.pressed` that silently loses its owner.
- A numeric literal's context must reach calls, returns and constructors; parsing an integer into a wide host value is insufficient to give it an Int64 guest type.
- Handle the signed minimum literal before rejecting its positive magnitude. A positive value above Int64 maximum must produce a diagnostic, not become zero.
- Preserve catalog overflow behavior: Int32 addition wraps; out-of-range narrowing traps with TRAP_NUMERIC_RANGE. Do not assume all numeric operations trap on overflow.
- A switch can omit its final comparison only when exhaustive coverage is proven. Non-exhaustive statement switches must not execute an unmatched final arm.
- A catalog or structural test alone cannot prove a frontend consumer. Generate PBS programs, run the real VM, and keep the full installed example as an independent gate.
- Publish the focused canonical spec in installed knowledge as well as in the checkout. SDK exports, diagnostics, language services and examples must migrate together.
## References
- [Normative PBS input and existing-value contract](../../../docs/compiler/pbs/specs/input-and-existing-type-runtime-alignment.md)
- [Shared runtime catalog contract](../../../docs/compiler/general/specs/runtime-catalog-support.md)
- [Typed registry and PBX contract](../../../docs/compiler/general/specs/typed-registry-and-pbx-v0.md)
- [PBS source-to-PBX qualification](../../../prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/PbsRuntimeAlignmentTest.java)
- [Actual-runtime harness instructions](../../../tools/pbx-conformance/README.md)
@@ -0,0 +1,106 @@
# Runtime catalog support
Status: normative. Owner: `compiler/general`.
## Authority and update boundary
The runtime owns intrinsic identities, ABI layouts, effects, costs and builtin
constants. Studio MUST keep byte-identical copies of the selected provider's
`INTRINSICS.csv` and `BUILTIN_CONSTANTS.csv` under `docs/vm-arch`. The sibling
`runtime-catalog.properties` records the provider revision and SHA-256 of both
files. These mirrors are reference data, not an independent runtime contract.
An update MUST import both catalogs, inventory additions/removals and field
changes, manually port the compiler consumers, verify behavior and update the
packaged provenance together. A CSV-only change is not a completed integration.
Read the selected runtime's implementation and tests when porting behavior;
CSV fields alone do not explain dispatch, reference validation or charging.
Normal builds MUST use local versioned sources and resources. They MUST NOT
query the latest runtime or depend on a sibling runtime checkout. An explicit
provider verification command requires the recorded revision and fails when
the source is missing, modified or different. That evidence requirement does
not make a different runtime revision inherently incompatible.
## Manual intrinsic representation
`IRVMIntrinsicDefinition` is the explicit compiler representation of its runtime
target. It MUST preserve every published record and field: IDs, canonical
identity/version, owner/name, argument/result counts and layouts, determinism,
allocation, parameter names, guest-state mutation, base/fixed cycles, cost model,
attempt cycles and maximum attempts. `IRVMIntrinsicRegistry` resolves exact
identities and IDs. Retired entries MUST NOT survive as aliases.
Lowering MUST validate caller-supplied counts against the selected definition.
Validation and emission MUST reject unknown IDs and inconsistent signatures,
including unknown IDs in unreachable bytecode. A name retained by the provider
does not imply an unchanged ABI; input touch coordinates illustrate a change
from receiver-based calls to zero-argument queries.
Carrier mappings MUST be explicit. Int32 and Int64 remain distinct despite equal
slot widths. Layout order and complete groups MUST be preserved. Native color
and Random contracts MUST NOT be inferred from arbitrary one-slot integers or
references. Producer checks validate proven carrier facts; runtime admission
and execution remain authoritative for reference identity, session ownership,
platform availability and traps. Unsupported carrier tokens MUST fail explicitly.
Static intrinsic String facts MUST follow the provider verifier's String-only
mask: a proven null is rejected at load time. The dispatcher's acceptance of
null for the String ABI does not broaden that static rule. Random's static
reference mask includes null; native handle validity is still checked at runtime.
Determinism MUST NOT be interpreted as purity. In particular, all Random calls
mutate guest state. Reachable calls cannot be removed, merged or reordered in a
way that changes effects, traps or charged work, even when their results are
discarded. Variable-cost descriptors MUST remain variable: `random.range` has
base cost 4, cost 8 per attempt and a maximum of 64 attempts in the selected
contract. The runtime performs charging; Java metadata does not implement a
second scheduler or random algorithm.
## Constants
Builtin constants have their own lookup identity `(target, name, version)` and
ordered slot values. They MUST NOT be resolved by inventing an intrinsic with
the same name. Some color names have both a constant and a separately published
intrinsic; this does not establish a generic equivalence.
The manual constant representation MUST preserve all carriers and exact values.
Float constants are materialized from published IEEE-754 bits. `vec2.zero` is
two ordered Float slots and has no intrinsic ID. Materialization and emission
MUST preserve the whole group; source-language exposure remains frontend-owned.
## Distribution and conformance
The compiler JAR packages the two catalogs and provenance at
`runtime-catalog/compiler/`; the PBS SDK JAR packages the same three files at
`runtime-catalog/pbs-sdk/`. Both copies MUST derive from the checked-in mirrors
and agree byte-for-byte. The metadata identifies the represented target contract;
it does not assert that every catalog entry already has a PBS facade.
Normal tests MUST compare every manually maintained field with the independently
published CSV, verify constant bits/groups and check packaged hashes. They MUST
run without a runtime checkout and MUST NOT be disabled or silently skip parity.
Explicit provider checks also run the real runtime catalog exporter test;
parsing only one Rust table is insufficient because the catalog spans several
implementation modules.
Behavioral tests MUST cover changed signatures, retired IDs, scalar carriers,
complete groups, stateful call order and emitted values. Evidence from the
selected runtime MUST identify revision, platform and configuration and distinguish
catalog comparison, image loading and execution. PBS facade delivery and
installed-runtime qualification remain separate consumer claims.
See [typed common Registry and PBX v0](typed-registry-and-pbx-v0.md) for the
frontend/backend boundary and [mirror policy](../../../vm-arch/README.md) for
explicit verification commands.
## PBS consumer
The [PBS input and value alignment contract](../../pbs/specs/input-and-existing-type-runtime-alignment.md)
requires typed Pad/Button dispatch, direct LowPad endpoints and complete builtin
value groups. Frontend numeric literal and builtin-constant operations retain
explicit identity and width; common lowering resolves constant groups against
`IRVMBuiltinConstantDefinition` and checks the declared result width. PBS does
not introduce another catalog. Source execution and the full installed example
are separate consumer gates beyond CSV parity.
@@ -0,0 +1,182 @@
# Typed common Registry and PBX v0
Status: normative. Owner: `compiler/general`. Runtime contract reference:
`runtime` revision `f7d7c167531bb662a1af9d73fc326bb476bf8aa7`,
`docs/specs/runtime/02c-program-registry-and-pbx-v0.md` and
`docs/vm-arch/ISA_CORE.md` in that repository.
## Ownership and handoff
Every executable frontend MUST provide its semantic types through the common
`IRRegistry` model and typed `FunctionMetadata`. The backend MUST NOT inspect
language ASTs, PBS names, Optional/Result syntax, or frontend-specific classes.
Language carriers and their lowering remain owned by the frontend. A carrier
can use ordinary Struct, Array, Trait, Fn and Builtin declarations without
introducing a language category in the backend.
The public model consists of immutable values and ordered `ReadOnlyList`
collections. References use explicit declaration keys and callable IDs, not
object identity or recursive object graphs. It is serializable by design;
no concrete IR codec, wire format or persistence protocol is selected.
A function declares ordered named parameters, ordered logical results, ordered
captures, a lifecycle role and an optional name. Empty names differ from absent
names. Captures are an environment, never hidden parameters. Slot counts are
derived from types; existing count fields are checked caches, not semantic
input. Missing nonempty type metadata is an invalid handoff, not a request to
infer types from counts. Optimizers MUST preserve metadata and typed operands.
## Identity and linking
TypeId, FunctionId and ImplementationId are separate dense image-local index
domains. Index zero is valid. Trait references use TypeIds; field and method
indices are local to their owner. Source declaration keys MUST identify the
same declaration across repeated module imports and distinguish unrelated
nominal declarations, even when names and shapes coincide.
Linking is deterministic. The selected physical entry is FunctionId zero;
remaining functions follow canonical module/callable/source ordering. Struct
and Trait retain nominal identity. Builtin imports deduplicate by BuiltinId,
Array by canonical element identity, and Fn by ordered parameter/result type
identities, excluding names. Forward references and recursion through nominal
anchors are valid. Pure Array/Fn cycles are invalid. Every reference must
resolve before emission. Frontends should supply only used imports.
| BuiltinId | Type | Slots |
| --- | --- | --- |
| `0x0001` | color | 1 |
| `0x0002` | vec2 | 2 |
| `0x0003` | pixel | 3 |
| `0x0010` | Int32 | 1 |
| `0x0011` | Int64 | 1 |
| `0x0012` | Float | 1 |
| `0x0013` | Bool | 1 |
| `0x0014` | String | 1 |
| `0x0015` | Dynamic | 2 |
| `0x0016` | Random | 1 |
The retired input IDs `0x0100`–`0x0102` have no aliases. Struct, Array and Fn
values occupy one reference slot; Trait values occupy two slots. Width does
not establish type identity or authorize conversion.
## Binary contract
All fields are little-endian. The header is 32 bytes: `PBX\0`, u16 version zero,
zero endian/reserved bytes, u32 section count and twenty zero reserved bytes.
Directory entries are u32 kind, file-relative offset and length. Ranges must
not overlap or overflow. The version remains zero; no legacy fallback exists.
Mandatory sections are constants (0), functions (1), Code (2), exports (4),
syscalls (5), types (6), implementations (7) and captures (8). Debug (3) is
optional. Empty tables contain a u32 zero count. Code is raw bytes, not a
counted table. An empty table set is serializable but is not an executable
program: production loading requires a valid ordinary entry.
Lists have u32 counts. Optional names use u8 presence 0/1, then u32 UTF-8 byte
length and bytes when present. UTF-8 is strict. Named types contain TypeId then
optional name; results and captures contain TypeIds without names.
| Record | Ordered payload |
| --- | --- |
| Type | u8 tag, optional name, category payload |
| Builtin (tag 0) | u32 BuiltinId |
| Struct (tag 1) | named fields |
| Array (tag 2) | u32 element TypeId |
| Trait (tag 3) | methods: optional name, parameters, results |
| Fn (tag 4) | parameters, results |
| Function | u8 role, optional name, parameters, results, u32 Code offset, length, extra locals, max temporaries |
| Implementation | u32 owner TypeId, trait TypeId, method count, ordered FunctionIds |
| Captures | function count and one ordered TypeId list per function, including empty lists |
Function Code offsets are Code-relative. Parameter/result slot counts MUST NOT
be serialized. Max temporaries zero means zero. Signature names are independent
of debug names. Constants retain tags Null 0, Int64 1, Float64 2, Boolean 3,
String 4 and Int32 5. Float payloads must be finite. Constant deduplication
requires relocating every PUSH_CONST index; changing only the table is invalid.
Roles are None 0, BeforePause 1, BeforeResume 2 and MemcardBoot 3. Nonzero roles
are unique, cannot target entry zero and cannot capture. Pause/resume have empty
signatures; MemcardBoot has `(Int64) -> Int32`. This metadata does not implement
frontend hook selection, mem.bootstrap gating or persistence.
Implementations are unique per Struct/Trait pair, complete and ordered. Each
method target has an exact signature with a concrete Struct receiver first,
followed by the trait method's explicit parameters. Targets cannot capture.
## Instructions and verification
Instructions use u16 opcodes. Jump operands are absolute u32 byte offsets from
the current function start, not displacements from the next instruction.
A taken jump to the current function end is a return boundary; ordinary
fallthrough past the end is invalid.
| Opcode | Typed operands |
| --- | --- |
| MAKE_CLOSURE `0x52` | FunctionId, Fn TypeId |
| CALL_CLOSURE `0x53` | expected Fn TypeId |
| GET_CAPTURE `0x57` | capture group index |
| CALL_TRAIT `0x58` | Trait TypeId, method index |
| DYN_PACK/UNPACK/CAN_UNPACK `0x90`–`0x92` | TypeId |
| Reference operations `0x93`–`0x96` | source TypeId, target TypeId |
| STRUCT_NEW `0xa0` | Struct TypeId |
| STRUCT_GET/SET `0xa1`–`0xa2` | Struct TypeId, field index |
| Array operations `0xa3`–`0xa6` | Array TypeId |
Direct CALL cannot target a capturing body. Closure signatures match exactly.
Retired SPAWN/YIELD/SLEEP (`0x54`–`0x56`) and raw producer SYSCALL are invalid,
even when unreachable. All function ranges must decode completely; references,
categories, operand boundaries, finite immediates and branch destinations are
checked even in unreachable instructions.
Reachable analysis uses a bounded worklist. Parameters initialize local facts;
extra locals start Null and may change type. Joins require equal temporary
heights. Equal facts survive; conflicting facts widen to Unknown. Type errors
are diagnosed after convergence. Unknown alone neither proves compatibility
nor justifies rejection. Runtime group checks remain authoritative. RET and
jump-to-end require exact result width and compatible known facts. GET/SET_LOCAL
address only parameters plus extra locals. No implicit numeric, trait or Dynamic
conversion is introduced by linking, writing or verification.
HOSTCALL references a declared canonical `(module, name, version)` identity
with exact argument/result slot ABI. Duplicate identities with conflicting ABI,
unused declarations and invalid indices are rejected. Only the runtime loader
binds capabilities and rewrites HOSTCALL to SYSCALL. Intrinsic signatures are
explicit compiler metadata; their IDs and definitive ABI remain runtime-owned.
## Bounds and diagnostics
Product ceilings: PBX 64 MiB, Code 32 MiB, 65,536 types and functions each,
262,144 implementations, 1,048,576 nested records, names 4,096 bytes individually
and 4 MiB aggregate, frame 16,384 slots and results 1,024 slots. Runtime admission
also owns retained Registry 64 MiB, scratch 128 MiB and work 268,435,456. A guest
cannot raise these limits. Java validation is not a substitute for runtime
allocation accounting or final `ValidatedProgramImage::load` admission.
Compilation failures use common LOWER_IRVM/MARSHAL stage diagnostics. Load,
binding and resource failures remain distinct from executed traps. Studio
preserves the full runtime error envelope in `AutomationFailure.runtimeError`,
including null fields and native binding details. The outer initialization
fault PC remains null. A verifier PC stays in `initError.load.pc`; a file offset
MUST NOT become an executed PC.
## Reproducible conformance
Run the focused common tests, then the production harness:
```sh
./gradlew :prometeu-compiler:prometeu-build-pipeline:test --tests '*LinkedRegistryTest' --tests '*PbxRegistryConformanceTest' --tests '*TypedCommonPipelineTest'
python3 tools/pbx-conformance/run.py --runtime ../runtime
```
The Java tests write `build/registry-conformance` inside the build-pipeline
module. The harness builds outside the runtime checkout using its Cargo.lock,
loads Java-produced bytes, executes admitted zero-argument fixtures, validates
structured failures against the published schema and records the runtime hash,
platform, capabilities and limits. Parsing, loading and execution are separate
claims. The vec2 signature fixture is load-only; it is not a zero-argument boot.
The fixed 194-byte minimal image comes from the independent runtime test oracle.
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.
@@ -0,0 +1,136 @@
# PBS input and existing value runtime alignment
Status: normative. Owner: `compiler/pbs`. Common ABI authority:
[typed Registry/PBX](../../general/specs/typed-registry-and-pbx-v0.md) and
[runtime catalogs](../../general/specs/runtime-catalog-support.md).
## Source types and exact carriers
| PBS source | Runtime identity | Ordered slots |
| --- | --- | --- |
| `int` | Int32 | Int32 |
| `long` | Int64 | Int64 |
| `float` | Float | Float (binary64) |
| `bool` | Bool | Bool |
| `str` | String | String |
| builtin `color` shell | color | Int64 constrained to RGBA8888 |
| builtin `vec2` shell | vec2 | Float x, Float y |
| builtin `pixel` shell | pixel | Int64 x, Int64 y, Int64 color |
The canonical builtin metadata determines a shell's identity; its source name
is not an ABI lookup rule. Equal widths do not permit substitution. Function
metadata MUST declare ordered parameter and result identities. Slot counts are
checked caches derived from those identities. Calls, locals and returns MUST
preserve entire value groups. A vec2 is one logical value with two slots;
a named output tuple is a fixed sequence of logical results, not a heap object.
`int` and `long` are distinct. Integer literals default to `int`; explicit
`long` contexts (annotations, parameters and results) validate literals in the
Int64 range and materialize them with that carrier. Out-of-range literals MUST
be diagnosed, never replaced by zero. A typed variable does not change type
because it appears in another expected context. Mixed int/long operations
require an explicit conversion. Existing `as` syntax expresses conversion:
```pbs
let frames: long = 2147483648;
let offset: int = 10;
let wide = offset as long;
let narrow = frames as int;
```
The final conversion faults if its value is outside Int32 range. Numeric
conversions use published math/mathf conversion intrinsics, without adding
math service facades. Int32/Int64 arithmetic preserves runtime wrapping;
Float overflow and range-invalid conversions preserve runtime faults.
Defaults are owned by the runtime storage contract: numeric zero, Float +0,
Bool false, nullable String null, transparent color, zero vec2 and zero pixel.
This does not introduce implicit source-variable initialization: VM locals
start null and PBS definite-assignment rules still apply. NaN/infinity are not
valid guest numeric defaults.
## Public input API
Import `Pad`, `Button` and `Touch` from `@sdk:input`. Button is a closed nominal
PBS enum with cases Up, Down, Left, Right, A, B, X, Y, L, R, Start and Select.
It identifies a button; it does not contain input state or perform a query.
Locals and typed function parameters may carry Button values, including values
selected during execution. Strings, integer indices and unrelated enums are
not valid Button arguments.
| Public operation | Result |
| --- | --- |
| `Pad.pressed(button: Button)` | bool |
| `Pad.released(button: Button)` | bool |
| `Pad.down(button: Button)` | bool |
| `Pad.hold(button: Button)` | long, held logical frames |
| `Touch.pressed()`, `Touch.released()`, `Touch.down()` | bool |
| `Touch.hold()` | long, held logical frames |
| `Touch.x()`, `Touch.y()` | long |
```pbs
import { Pad, Button, Touch } from @sdk:input;
fn jump(button: Button) -> bool {
return Pad.pressed(button);
}
fn held() -> long {
let jump_button = Button.A;
return Pad.hold(jump_button);
}
```
Pad dispatches to `LowPad.<button>_<query>()`, for example
`LowPad.a_pressed()`. All 48 combinations map to the published direct endpoint.
Each call MUST evaluate its selector once and execute exactly one chosen
query. It MUST NOT read unselected buttons, speculate input faults or collect
all button states. Constant specialization is optional; dynamic dispatch must
work without optimization.
All 54 VM queries take zero arguments and no receiver. Button never crosses
the intrinsic boundary as a selector. Touch x/y use current IDs 0x2134/0x2135
and Int64 results. No new device, button-state object, snapshot or multi-pad
contract is introduced. Known input without an available platform faults with
TRAP_UNAVAILABLE; it never fabricates false or zero. Unknown and retired IDs
are invalid even in unreachable code.
## Migration and diagnostics
The old Input root and intermediate InputPad/InputTouch/InputButton objects
are removed. Replace `Input.pad().a().pressed()` with
`Pad.pressed(Button.A)` and `Input.touch().x()` with `Touch.x()`.
`let button = Input.pad().a();` is invalid; `let button = Button.A;` is valid.
Migration diagnostics must point to the replacement surface. Hover, completion,
exports and examples must advertise the same API and exact return types.
When a consumer requires int coordinates, explicitly narrow a Touch result
with `as int`; do not silently change the query's ABI.
## Lowering and observable order
PBS owns source enum dispatch and typed metadata. Common code owns catalog
resolution, PBX validation and emission. The frontend MUST NOT maintain a
second intrinsic or constant catalog. SDK executable methods admit the existing
switch expression with value arms, apply and explicit numeric conversions
needed by this facade; other unsupported SDK constructs remain diagnosed.
For named application such as `f apply (y: effect_y(), x: effect_x())`, evaluate
Y then X exactly once, store complete groups in temporary locals, and reload
in x,y parameter order. Reordering expressions instead of values is invalid.
Builtin constants are groups resolved by the shared constant catalog.
`vec2.zero` materializes two Float slots; no intrinsic endpoint is invented.
Colors may use a published constant or an independently published intrinsic.
Function metadata must cover SDK wrappers such as Gfx.clear. Validation must
not infer missing identities from widths or omit the full installed example.
Preserve the existing lifecycle assembly and physical boot FunctionId zero.
## Conformance
The PBS source-to-production-PBX corpus and production runtime harness must
cover all 54 input queries with controlled input and exactly-one-query checks,
known-unavailable behavior, typed selectors, int/long boundaries and explicit
conversions, Float/String/Bool, color/vec2/pixel groups, multiple returns and
named-argument effects. Keep common registry wrong-carrier, retired-ID and stale
signature tests active. Record runtime hash, Studio revision, platform and
configuration. A catalog-only pass does not replace successful PBS execution
or `:prometeu-app:installedKnowledgeTest`.
@@ -296,3 +296,7 @@ This document is ready to move beyond temporary status only when:
2. diagnostic identity, metadata, and attribution rules are explicit enough for conformance, including locale-agnostic token/template identity,
3. the backend-originated failure boundary is explicit enough that PBS-facing diagnostics can be distinguished from internal tool failures,
4. and cost/warning expectations are explicit where they are intended to be normative for v1.
## Existing values and input runtime alignment
The [PBS input and value alignment contract](../../../compiler/pbs/specs/input-and-existing-type-runtime-alignment.md) governs int/Int32, long/Int64, exact builtin carriers, the Pad/Button/LowPad and Touch API, named-argument evaluation order, migration diagnostics and executable conformance. It supersedes older input-object and untyped executable-lowering descriptions.
@@ -238,3 +238,7 @@ At frontend `IRBackend` emission time:
4. module order in the declaration MUST propagate the explicit dependency order already derived from resolved project/module data, without sorting by `FileId`, textual module name, or synthetic callable name,
5. PBS MUST NOT emit module init, project-init wrapper, published frame wrapper, physical entrypoint, or boot guard,
6. and common lifecycle assembly remains the owner of derived artifacts, including the published wrapper that becomes the physical entrypoint and owns the final frame return.
## Existing values and input runtime alignment
The [PBS input and value alignment contract](../../../compiler/pbs/specs/input-and-existing-type-runtime-alignment.md) governs int/Int32, long/Int64, exact builtin carriers, the Pad/Button/LowPad and Touch API, named-argument evaluation order, migration diagnostics and executable conformance. It supersedes older input-object and untyped executable-lowering descriptions.
@@ -48,7 +48,7 @@ Rules:
- `declare host` declarations are reserved to SDK/toolchain-controlled modules and are not formable in ordinary user-authored PBS source.
- `declare builtin type` declarations are reserved to SDK/toolchain-controlled modules and are not formable in ordinary user-authored PBS source.
- A host owner name is not an ordinary value expression in v1 core; it is used only as a qualified host-call owner.
- The builtin simple types `int`, `float`, `bool`, and `str` are always present in the type namespace.
- The builtin simple types `int`, `long`, `float`, `bool`, and `str` are always present in the type namespace.
- Builtin simple types are resolved without import or declaration.
- User-authored type declarations must not reuse builtin simple type names.
@@ -874,3 +874,7 @@ At minimum, deterministic static diagnostics are required for:
- invalid optional intrinsic member call,
- invalid builtin intrinsic member call,
- member projection on a missing named output label.
## Existing values and input runtime alignment
The [PBS input and value alignment contract](../../../compiler/pbs/specs/input-and-existing-type-runtime-alignment.md) governs int/Int32, long/Int64, exact builtin carriers, the Pad/Button/LowPad and Touch API, named-argument evaluation order, migration diagnostics and executable conformance. It supersedes older input-object and untyped executable-lowering descriptions.
@@ -263,13 +263,13 @@ Example:
```pbs
[BuiltinType(name="color", version=1)]
declare builtin type Color(
pub raw: int,
pub raw: long,
);
[BuiltinType(name="pixel", version=1)]
declare builtin type Pixel(
pub x: int,
pub y: int,
pub x: long,
pub y: long,
pub color: Color,
);
```
@@ -277,13 +277,13 @@ declare builtin type Pixel(
Canonical semantic layout:
```text
Pixel -> [int, int, Color]
Pixel -> [long, long, Color]
```
Canonical flattened slot layout:
```text
Pixel -> [int, int, int]
Pixel -> [long, long, long]
```
## 13. Member Categories
@@ -560,3 +560,7 @@ The current temporary contract is:
7. `BuiltinConst(target, name, version)` exposes VM-owned builtin constants through reserved top-level `declare const` shells.
8. Builtin projections, constants, and intrinsic calls must not lower through `SYSC`, `HOSTCALL`, or `SYSCALL`.
9. Runtime-facing intrinsic artifacts are distinct from host-binding artifacts.
## Existing values and input runtime alignment
The [PBS input and value alignment contract](../../../compiler/pbs/specs/input-and-existing-type-runtime-alignment.md) governs int/Int32, long/Int64, exact builtin carriers, the Pad/Button/LowPad and Touch API, named-argument evaluation order, migration diagnostics and executable conformance. It supersedes older input-object and untyped executable-lowering descriptions.
@@ -387,3 +387,7 @@ This document is ready to move beyond temporary draft status only when:
3. host-call interaction rules are stated without relying on informal prose elsewhere,
4. the document can drive runtime conformance tests without decision backfill,
5. the remaining TODO items are reduced to editorial integration rather than semantic uncertainty.
## Existing values and input runtime alignment
The [PBS input and value alignment contract](../../../compiler/pbs/specs/input-and-existing-type-runtime-alignment.md) governs int/Int32, long/Int64, exact builtin carriers, the Pad/Button/LowPad and Touch API, named-argument evaluation order, migration diagnostics and executable conformance. It supersedes older input-object and untyped executable-lowering descriptions.
@@ -1,5 +1,10 @@
# Bytecode and PBX Mapping Specification
The current typed Registry/PBX v0 contract is normative in
[Typed common Registry and PBX v0](../../compiler/general/specs/typed-registry-and-pbx-v0.md).
Its complete typed function records, mandatory sections and verification rules
replace the earlier slot-only artifact baseline.
Status: Draft v1 (Backend Baseline)
Applies to: mapping from lowered/optimized backend programs into PBX sections, bytecode-facing artifacts, and source-to-artifact invariants required by loader/verifier/runtime
@@ -74,7 +79,8 @@ Emitter output must map to a `BytecodeModule` shape containing:
3. `code`,
4. `exports`,
5. `syscalls`,
6. optional `debug_info` that still satisfies v1 minimum debug obligations.
6. typed Registry declarations, implementations and per-function capture lists,
7. optional `debug_info` that still satisfies v1 minimum debug obligations.
### 6.2 Function ordering and IDs
@@ -168,7 +174,6 @@ At minimum, artifact conformance checks should assert:
The following remain deferred:
- richer optional debug/source-map formats,
- additional PBX section-level contracts beyond current baseline,
- and profile-specific binary compatibility policy details beyond current v1 baseline.
## 11. Non-Goals
@@ -1,5 +1,10 @@
# Compiler Verification and Safety Checks Specification
The current typed Registry/PBX v0 contract is normative in
[Typed common Registry and PBX v0](../../compiler/general/specs/typed-registry-and-pbx-v0.md).
Its complete typed function records, mandatory sections and verification rules
replace the earlier slot-only artifact baseline.
Status: Draft v1 (Quality-Gate Baseline)
Applies to: test-backed safety gates for common compiler stages, validation ownership across executable frontends, and integration regression safety checks against runtime
@@ -1,5 +1,10 @@
# IRBackend to IRVM Lowering Specification
The current typed Registry/PBX v0 contract is normative in
[Typed common Registry and PBX v0](../../compiler/general/specs/typed-registry-and-pbx-v0.md).
Its complete typed function records, mandatory sections and verification rules
replace the earlier slot-only artifact baseline.
Status: Draft v1 (Backend Baseline)
Applies to: executable-backend lowering from the common `IRBackend` handoff into `IRVM`
@@ -1,5 +1,10 @@
# Backend Spec-to-Test Conformance Matrix
The current typed Registry/PBX v0 contract is normative in
[Typed common Registry and PBX v0](../../compiler/general/specs/typed-registry-and-pbx-v0.md).
Its complete typed function records, mandatory sections and verification rules
replace the earlier slot-only artifact baseline.
Status: Draft v1 (Traceability Baseline)
Applies to: compiler/backend conformance traceability for canonical stage order and entrypoint-specific contracts
@@ -140,3 +145,19 @@ Matrix integrity is enforced by automated document lint:
- if `PROMETEU_MATRIX_HARD_GATE=true` and `PROMETEU_CHANGED_FILES` contains a path under conformance-relevant FE/BE prefixes,
- then this matrix file MUST be present in `PROMETEU_CHANGED_FILES`, otherwise the gate fails.
- CI policy uses strict Gate I mode via `PROMETEU_GATE_I_STRICT=true` (or `CI=true`) and requires `PROMETEU_RUNTIME_CHECK_CMD` for runtime-backed evidence.
## 7. Typed Registry/PBX v0 qualification (2026-10-14)
Owner: compiler/general. These rows supersede slot-only PBX coverage claims;
frontend syntax and lowering remain consumer-owned.
| Requirement | Evidence | Status | Scope |
| --- | --- | --- | --- |
| Stable declarations, nominal identity, structural deduplication and cycles | LinkedRegistryTest; IRBackendExecutableContractTest | pass | Common API and linking |
| Complete typed signatures, captures, roles and implementations | PbxRegistryConformanceTest; TypedCommonPipelineTest | pass | Common IR through emission |
| Exact PBX mandatory tables and multi-slot signatures | Independent 194-byte oracle; fixed vec2 function bytes | pass | Wire format, not a self round-trip |
| Runtime final admission and execution | tools/pbx-conformance/run.py | pass | Closure, trait, array, Dynamic, intrinsic, roles, joins, native binding; vec2 load-only |
| Invalid structures, references, code and resource refusal | tools/pbx-conformance/main.rs mutation corpus | pass | Production loader, default/lowered limits |
| Structured initialization and binding diagnostics | HeadlessProcessSessionTest; observed runtime JSON fixtures | pass | Preserves full payload through MCP, including null outer PC |
| Published schema covers current runtime reasons | Actual LifecycleRole/NonFiniteValue payloads against runtime schema | partial | Runtime enum/schema mismatch recorded in AGD75; no payload rewriting |
| PBS migration and integrated delivery | AGD75 and consumer tickets | partial | Existing PBS suite passing is not complete executable lowering evidence |
@@ -559,8 +559,8 @@ are also checked. Rebuilding changes pixels/build identity and visibly marks the
old capture stale. Images remain in memory; window screenshots are not evidence.
The fixture uses supported project-source LSP definition/symbol navigation and
indexed SDK reads. A probe of the installed SDK's `Input.pad()` definition returns
an empty result with the current language service; the gate does not claim SDK
indexed SDK reads. The historical probe of the former SDK `Input.pad()` definition returned
an empty result (the current facade is Pad/Button and Touch); the gate does not claim SDK
symbol navigation where no location is returned. This is a language tooling
limitation, not a headless or MCP execution fallback.
+17
View File
@@ -0,0 +1,17 @@
target,name,version,slot,carrier,bits_or_value
color,black,1,0,Int64,255
color,blue,1,0,Int64,65535
color,cyan,1,0,Int64,16777215
color,gray,1,0,Int64,2155905279
color,green,1,0,Int64,16711935
color,indigo,1,0,Int64,1258324735
color,magenta,1,0,Int64,4278255615
color,orange,1,0,Int64,4286578943
color,red,1,0,Int64,4278190335
color,transparent,1,0,Int64,0
color,white,1,0,Int64,4294967295
color,yellow,1,0,Int64,4294902015
mathf,pi,1,0,Float,0x400921FB54442D18
mathf,tau,1,0,Float,0x401921FB54442D18
vec2,zero,1,0,Float,0x0000000000000000
vec2,zero,1,1,Float,0x0000000000000000
1 target name version slot carrier bits_or_value
2 color black 1 0 Int64 255
3 color blue 1 0 Int64 65535
4 color cyan 1 0 Int64 16777215
5 color gray 1 0 Int64 2155905279
6 color green 1 0 Int64 16711935
7 color indigo 1 0 Int64 1258324735
8 color magenta 1 0 Int64 4278255615
9 color orange 1 0 Int64 4286578943
10 color red 1 0 Int64 4278190335
11 color transparent 1 0 Int64 0
12 color white 1 0 Int64 4294967295
13 color yellow 1 0 Int64 4294902015
14 mathf pi 1 0 Float 0x400921FB54442D18
15 mathf tau 1 0 Float 0x401921FB54442D18
16 vec2 zero 1 0 Float 0x0000000000000000
17 vec2 zero 1 1 Float 0x0000000000000000
+106 -43
View File
@@ -1,43 +1,106 @@
final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate
0x1000,4096,vec2.dot,1,vec2,dot,4,1,float|float|float|float,float,true,false
0x1001,4097,vec2.length,1,vec2,length,2,1,float|float,float,true,false
0x1100,4352,color.from_raw,1,color,from_raw,1,1,int,builtin:color,true,false
0x1101,4353,color.rgb,1,color,rgb,3,1,int|int|int,builtin:color,true,false
0x1102,4354,color.rgba,1,color,rgba,4,1,int|int|int|int,builtin:color,true,false
0x1103,4355,color.html_rgba,1,color,html_rgba,1,1,str,builtin:color,true,false
0x1104,4356,color.gray_scale,1,color,gray_scale,1,1,int,builtin:color,true,false
0x1105,4357,color.hex,1,color,hex,1,1,builtin:color,int,true,false
0x1106,4358,color.alpha,1,color,alpha,1,1,builtin:color,int,true,false
0x1120,4384,color.black,1,color,black,0,1,,builtin:color,true,false
0x1121,4385,color.white,1,color,white,0,1,,builtin:color,true,false
0x1122,4386,color.red,1,color,red,0,1,,builtin:color,true,false
0x1123,4387,color.green,1,color,green,0,1,,builtin:color,true,false
0x1124,4388,color.blue,1,color,blue,0,1,,builtin:color,true,false
0x1125,4389,color.yellow,1,color,yellow,0,1,,builtin:color,true,false
0x1126,4390,color.orange,1,color,orange,0,1,,builtin:color,true,false
0x1127,4391,color.indigo,1,color,indigo,0,1,,builtin:color,true,false
0x1128,4392,color.gray,1,color,gray,0,1,,builtin:color,true,false
0x1129,4393,color.cyan,1,color,cyan,0,1,,builtin:color,true,false
0x112A,4394,color.magenta,1,color,magenta,0,1,,builtin:color,true,false
0x112B,4395,color.transparent,1,color,transparent,0,1,,builtin:color,true,false
0x2000,8192,input.pad,1,input,pad,0,1,,builtin:input.pad,true,false
0x2001,8193,input.touch,1,input,touch,0,1,,builtin:input.touch,true,false
0x2010,8208,input.pad.up,1,input.pad,up,1,1,builtin:input.pad,builtin:input.button,true,false
0x2011,8209,input.pad.down,1,input.pad,down,1,1,builtin:input.pad,builtin:input.button,true,false
0x2012,8210,input.pad.left,1,input.pad,left,1,1,builtin:input.pad,builtin:input.button,true,false
0x2013,8211,input.pad.right,1,input.pad,right,1,1,builtin:input.pad,builtin:input.button,true,false
0x2014,8212,input.pad.a,1,input.pad,a,1,1,builtin:input.pad,builtin:input.button,true,false
0x2015,8213,input.pad.b,1,input.pad,b,1,1,builtin:input.pad,builtin:input.button,true,false
0x2016,8214,input.pad.x,1,input.pad,x,1,1,builtin:input.pad,builtin:input.button,true,false
0x2017,8215,input.pad.y,1,input.pad,y,1,1,builtin:input.pad,builtin:input.button,true,false
0x2018,8216,input.pad.l,1,input.pad,l,1,1,builtin:input.pad,builtin:input.button,true,false
0x2019,8217,input.pad.r,1,input.pad,r,1,1,builtin:input.pad,builtin:input.button,true,false
0x201A,8218,input.pad.start,1,input.pad,start,1,1,builtin:input.pad,builtin:input.button,true,false
0x201B,8219,input.pad.select,1,input.pad,select,1,1,builtin:input.pad,builtin:input.button,true,false
0x2020,8224,input.touch.button,1,input.touch,button,1,1,builtin:input.touch,builtin:input.button,true,false
0x2021,8225,input.touch.x,1,input.touch,x,1,1,builtin:input.touch,int,true,false
0x2022,8226,input.touch.y,1,input.touch,y,1,1,builtin:input.touch,int,true,false
0x2030,8240,input.button.pressed,1,input.button,pressed,1,1,builtin:input.button,bool,true,false
0x2031,8241,input.button.released,1,input.button,released,1,1,builtin:input.button,bool,true,false
0x2032,8242,input.button.down,1,input.button,down,1,1,builtin:input.button,bool,true,false
0x2033,8243,input.button.hold,1,input.button,hold,1,1,builtin:input.button,int,true,false
final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate,parameter_names,cost_cycles,mutates_guest_state,cost_model,attempt_cost_cycles,max_attempts
0x1000,4096,vec2.dot,1,vec2,dot,4,1,Float|Float|Float|Float,Float,true,false,ax|ay|bx|by,10,false,fixed,0,0
0x1001,4097,vec2.length,1,vec2,length,2,1,Float|Float,Float,true,false,x|y,40,false,fixed,0,0
0x1100,4352,color.from_raw,1,color,from_raw,1,1,Int64,builtin:color,true,false,raw,1,false,fixed,0,0
0x1101,4353,color.rgb,1,color,rgb,3,1,Int64|Int64|Int64,builtin:color,true,false,r|g|b,1,false,fixed,0,0
0x1102,4354,color.rgba,1,color,rgba,4,1,Int64|Int64|Int64|Int64,builtin:color,true,false,r|g|b|a,1,false,fixed,0,0
0x1103,4355,color.html_rgba,1,color,html_rgba,1,1,String,builtin:color,true,false,value,1,false,fixed,0,0
0x1104,4356,color.gray_scale,1,color,gray_scale,1,1,Int64,builtin:color,true,false,value,1,false,fixed,0,0
0x1105,4357,color.hex,1,color,hex,1,1,builtin:color,Int64,true,false,color,1,false,fixed,0,0
0x1106,4358,color.alpha,1,color,alpha,1,1,builtin:color,Int64,true,false,color,1,false,fixed,0,0
0x1120,4384,color.black,1,color,black,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x1121,4385,color.white,1,color,white,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x1122,4386,color.red,1,color,red,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x1123,4387,color.green,1,color,green,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x1124,4388,color.blue,1,color,blue,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x1125,4389,color.yellow,1,color,yellow,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x1126,4390,color.orange,1,color,orange,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x1127,4391,color.indigo,1,color,indigo,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x1128,4392,color.gray,1,color,gray,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x1129,4393,color.cyan,1,color,cyan,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x112A,4394,color.magenta,1,color,magenta,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x112B,4395,color.transparent,1,color,transparent,0,1,,builtin:color,true,false,,1,false,fixed,0,0
0x2100,8448,input.pad.up.pressed,1,input.pad.up,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2101,8449,input.pad.up.released,1,input.pad.up,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2102,8450,input.pad.up.down,1,input.pad.up,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2103,8451,input.pad.up.hold,1,input.pad.up,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2104,8452,input.pad.down.pressed,1,input.pad.down,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2105,8453,input.pad.down.released,1,input.pad.down,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2106,8454,input.pad.down.down,1,input.pad.down,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2107,8455,input.pad.down.hold,1,input.pad.down,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2108,8456,input.pad.left.pressed,1,input.pad.left,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2109,8457,input.pad.left.released,1,input.pad.left,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x210A,8458,input.pad.left.down,1,input.pad.left,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x210B,8459,input.pad.left.hold,1,input.pad.left,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x210C,8460,input.pad.right.pressed,1,input.pad.right,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x210D,8461,input.pad.right.released,1,input.pad.right,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x210E,8462,input.pad.right.down,1,input.pad.right,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x210F,8463,input.pad.right.hold,1,input.pad.right,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2110,8464,input.pad.a.pressed,1,input.pad.a,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2111,8465,input.pad.a.released,1,input.pad.a,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2112,8466,input.pad.a.down,1,input.pad.a,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2113,8467,input.pad.a.hold,1,input.pad.a,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2114,8468,input.pad.b.pressed,1,input.pad.b,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2115,8469,input.pad.b.released,1,input.pad.b,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2116,8470,input.pad.b.down,1,input.pad.b,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2117,8471,input.pad.b.hold,1,input.pad.b,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2118,8472,input.pad.x.pressed,1,input.pad.x,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2119,8473,input.pad.x.released,1,input.pad.x,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x211A,8474,input.pad.x.down,1,input.pad.x,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x211B,8475,input.pad.x.hold,1,input.pad.x,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x211C,8476,input.pad.y.pressed,1,input.pad.y,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x211D,8477,input.pad.y.released,1,input.pad.y,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x211E,8478,input.pad.y.down,1,input.pad.y,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x211F,8479,input.pad.y.hold,1,input.pad.y,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2120,8480,input.pad.l.pressed,1,input.pad.l,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2121,8481,input.pad.l.released,1,input.pad.l,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2122,8482,input.pad.l.down,1,input.pad.l,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2123,8483,input.pad.l.hold,1,input.pad.l,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2124,8484,input.pad.r.pressed,1,input.pad.r,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2125,8485,input.pad.r.released,1,input.pad.r,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2126,8486,input.pad.r.down,1,input.pad.r,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2127,8487,input.pad.r.hold,1,input.pad.r,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2128,8488,input.pad.start.pressed,1,input.pad.start,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2129,8489,input.pad.start.released,1,input.pad.start,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x212A,8490,input.pad.start.down,1,input.pad.start,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x212B,8491,input.pad.start.hold,1,input.pad.start,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x212C,8492,input.pad.select.pressed,1,input.pad.select,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x212D,8493,input.pad.select.released,1,input.pad.select,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x212E,8494,input.pad.select.down,1,input.pad.select,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x212F,8495,input.pad.select.hold,1,input.pad.select,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2130,8496,input.touch.button.pressed,1,input.touch.button,pressed,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2131,8497,input.touch.button.released,1,input.touch.button,released,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2132,8498,input.touch.button.down,1,input.touch.button,down,0,1,,Bool,true,false,,1,false,fixed,0,0
0x2133,8499,input.touch.button.hold,1,input.touch.button,hold,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2134,8500,input.touch.x,1,input.touch,x,0,1,,Int64,true,false,,1,false,fixed,0,0
0x2135,8501,input.touch.y,1,input.touch,y,0,1,,Int64,true,false,,1,false,fixed,0,0
0x3000,12288,math.i32.min,1,math.i32,min,2,1,Int32|Int32,Int32,true,false,a|b,2,false,fixed,0,0
0x3001,12289,math.i32.max,1,math.i32,max,2,1,Int32|Int32,Int32,true,false,a|b,2,false,fixed,0,0
0x3002,12290,math.i32.clamp,1,math.i32,clamp,3,1,Int32|Int32|Int32,Int32,true,false,value|min|max,4,false,fixed,0,0
0x3003,12291,math.i32.abs,1,math.i32,abs,1,1,Int32,Int32,true,false,value,2,false,fixed,0,0
0x3004,12292,math.i64.min,1,math.i64,min,2,1,Int64|Int64,Int64,true,false,a|b,2,false,fixed,0,0
0x3005,12293,math.i64.max,1,math.i64,max,2,1,Int64|Int64,Int64,true,false,a|b,2,false,fixed,0,0
0x3006,12294,math.i64.clamp,1,math.i64,clamp,3,1,Int64|Int64|Int64,Int64,true,false,value|min|max,4,false,fixed,0,0
0x3007,12295,math.i64.abs,1,math.i64,abs,1,1,Int64,Int64,true,false,value,2,false,fixed,0,0
0x3008,12296,mathf.min,1,mathf,min,2,1,Float|Float,Float,true,false,a|b,2,false,fixed,0,0
0x3009,12297,mathf.max,1,mathf,max,2,1,Float|Float,Float,true,false,a|b,2,false,fixed,0,0
0x300A,12298,mathf.clamp,1,mathf,clamp,3,1,Float|Float|Float,Float,true,false,value|min|max,4,false,fixed,0,0
0x300B,12299,mathf.abs,1,mathf,abs,1,1,Float,Float,true,false,value,2,false,fixed,0,0
0x300C,12300,mathf.floor,1,mathf,floor,1,1,Float,Float,true,false,value,4,false,fixed,0,0
0x300D,12301,mathf.ceil,1,mathf,ceil,1,1,Float,Float,true,false,value,4,false,fixed,0,0
0x300E,12302,mathf.trunc,1,mathf,trunc,1,1,Float,Float,true,false,value,4,false,fixed,0,0
0x300F,12303,mathf.round,1,mathf,round,1,1,Float,Float,true,false,value,4,false,fixed,0,0
0x3010,12304,mathf.sqrt,1,mathf,sqrt,1,1,Float,Float,true,false,value,16,false,fixed,0,0
0x3011,12305,mathf.sin,1,mathf,sin,1,1,Float,Float,true,false,angle,32,false,fixed,0,0
0x3012,12306,mathf.cos,1,mathf,cos,1,1,Float,Float,true,false,angle,32,false,fixed,0,0
0x3013,12307,mathf.atan2,1,mathf,atan2,2,1,Float|Float,Float,true,false,y|x,48,false,fixed,0,0
0x3014,12308,math.i32.to_i64,1,math.i32,to_i64,1,1,Int32,Int64,true,false,value,2,false,fixed,0,0
0x3015,12309,math.i64.to_i32,1,math.i64,to_i32,1,1,Int64,Int32,true,false,value,2,false,fixed,0,0
0x3016,12310,math.i32.to_float,1,math.i32,to_float,1,1,Int32,Float,true,false,value,2,false,fixed,0,0
0x3017,12311,math.i64.to_float,1,math.i64,to_float,1,1,Int64,Float,true,false,value,2,false,fixed,0,0
0x3018,12312,mathf.to_i32,1,mathf,to_i32,1,1,Float,Int32,true,false,value,2,false,fixed,0,0
0x3019,12313,mathf.to_i64,1,mathf,to_i64,1,1,Float,Int64,true,false,value,2,false,fixed,0,0
0x4000,16384,random.new,1,random,new,1,1,Int64,builtin:Random,true,true,seed,32,true,fixed,0,0
0x4001,16385,random.next,1,random,next,1,1,builtin:Random,Int64,true,false,rng,8,true,fixed,0,0
0x4002,16386,random.nextf,1,random,nextf,1,1,builtin:Random,Float,true,false,rng,12,true,fixed,0,0
0x4003,16387,random.range,1,random,range,3,1,builtin:Random|Int64|Int64,Int64,true,false,rng|min|max,4,true,random_rejection_v1,8,64
1 final_id_hex final_id_dec canonical_name canonical_version owner name arg_slots ret_slots arg_layout ret_layout deterministic may_allocate parameter_names cost_cycles mutates_guest_state cost_model attempt_cost_cycles max_attempts
2 0x1000 4096 vec2.dot 1 vec2 dot 4 1 float|float|float|float Float|Float|Float|Float float Float true false ax|ay|bx|by 10 false fixed 0 0
3 0x1001 4097 vec2.length 1 vec2 length 2 1 float|float Float|Float float Float true false x|y 40 false fixed 0 0
4 0x1100 4352 color.from_raw 1 color from_raw 1 1 int Int64 builtin:color true false raw 1 false fixed 0 0
5 0x1101 4353 color.rgb 1 color rgb 3 1 int|int|int Int64|Int64|Int64 builtin:color true false r|g|b 1 false fixed 0 0
6 0x1102 4354 color.rgba 1 color rgba 4 1 int|int|int|int Int64|Int64|Int64|Int64 builtin:color true false r|g|b|a 1 false fixed 0 0
7 0x1103 4355 color.html_rgba 1 color html_rgba 1 1 str String builtin:color true false value 1 false fixed 0 0
8 0x1104 4356 color.gray_scale 1 color gray_scale 1 1 int Int64 builtin:color true false value 1 false fixed 0 0
9 0x1105 4357 color.hex 1 color hex 1 1 builtin:color int Int64 true false color 1 false fixed 0 0
10 0x1106 4358 color.alpha 1 color alpha 1 1 builtin:color int Int64 true false color 1 false fixed 0 0
11 0x1120 4384 color.black 1 color black 0 1 builtin:color true false 1 false fixed 0 0
12 0x1121 4385 color.white 1 color white 0 1 builtin:color true false 1 false fixed 0 0
13 0x1122 4386 color.red 1 color red 0 1 builtin:color true false 1 false fixed 0 0
14 0x1123 4387 color.green 1 color green 0 1 builtin:color true false 1 false fixed 0 0
15 0x1124 4388 color.blue 1 color blue 0 1 builtin:color true false 1 false fixed 0 0
16 0x1125 4389 color.yellow 1 color yellow 0 1 builtin:color true false 1 false fixed 0 0
17 0x1126 4390 color.orange 1 color orange 0 1 builtin:color true false 1 false fixed 0 0
18 0x1127 4391 color.indigo 1 color indigo 0 1 builtin:color true false 1 false fixed 0 0
19 0x1128 4392 color.gray 1 color gray 0 1 builtin:color true false 1 false fixed 0 0
20 0x1129 4393 color.cyan 1 color cyan 0 1 builtin:color true false 1 false fixed 0 0
21 0x112A 4394 color.magenta 1 color magenta 0 1 builtin:color true false 1 false fixed 0 0
22 0x112B 4395 color.transparent 1 color transparent 0 1 builtin:color true false 1 false fixed 0 0
23 0x2000 0x2100 8192 8448 input.pad input.pad.up.pressed 1 input input.pad.up pad pressed 0 1 builtin:input.pad Bool true false 1 false fixed 0 0
24 0x2001 0x2101 8193 8449 input.touch input.pad.up.released 1 input input.pad.up touch released 0 1 builtin:input.touch Bool true false 1 false fixed 0 0
25 0x2010 0x2102 8208 8450 input.pad.up input.pad.up.down 1 input.pad input.pad.up up down 1 0 1 builtin:input.pad builtin:input.button Bool true false 1 false fixed 0 0
26 0x2011 0x2103 8209 8451 input.pad.down input.pad.up.hold 1 input.pad input.pad.up down hold 1 0 1 builtin:input.pad builtin:input.button Int64 true false 1 false fixed 0 0
27 0x2012 0x2104 8210 8452 input.pad.left input.pad.down.pressed 1 input.pad input.pad.down left pressed 1 0 1 builtin:input.pad builtin:input.button Bool true false 1 false fixed 0 0
28 0x2013 0x2105 8211 8453 input.pad.right input.pad.down.released 1 input.pad input.pad.down right released 1 0 1 builtin:input.pad builtin:input.button Bool true false 1 false fixed 0 0
29 0x2014 0x2106 8212 8454 input.pad.a input.pad.down.down 1 input.pad input.pad.down a down 1 0 1 builtin:input.pad builtin:input.button Bool true false 1 false fixed 0 0
30 0x2015 0x2107 8213 8455 input.pad.b input.pad.down.hold 1 input.pad input.pad.down b hold 1 0 1 builtin:input.pad builtin:input.button Int64 true false 1 false fixed 0 0
31 0x2016 0x2108 8214 8456 input.pad.x input.pad.left.pressed 1 input.pad input.pad.left x pressed 1 0 1 builtin:input.pad builtin:input.button Bool true false 1 false fixed 0 0
32 0x2017 0x2109 8215 8457 input.pad.y input.pad.left.released 1 input.pad input.pad.left y released 1 0 1 builtin:input.pad builtin:input.button Bool true false 1 false fixed 0 0
33 0x2018 0x210A 8216 8458 input.pad.l input.pad.left.down 1 input.pad input.pad.left l down 1 0 1 builtin:input.pad builtin:input.button Bool true false 1 false fixed 0 0
34 0x2019 0x210B 8217 8459 input.pad.r input.pad.left.hold 1 input.pad input.pad.left r hold 1 0 1 builtin:input.pad builtin:input.button Int64 true false 1 false fixed 0 0
35 0x201A 0x210C 8218 8460 input.pad.start input.pad.right.pressed 1 input.pad input.pad.right start pressed 1 0 1 builtin:input.pad builtin:input.button Bool true false 1 false fixed 0 0
36 0x201B 0x210D 8219 8461 input.pad.select input.pad.right.released 1 input.pad input.pad.right select released 1 0 1 builtin:input.pad builtin:input.button Bool true false 1 false fixed 0 0
37 0x2020 0x210E 8224 8462 input.touch.button input.pad.right.down 1 input.touch input.pad.right button down 1 0 1 builtin:input.touch builtin:input.button Bool true false 1 false fixed 0 0
38 0x2021 0x210F 8225 8463 input.touch.x input.pad.right.hold 1 input.touch input.pad.right x hold 1 0 1 builtin:input.touch int Int64 true false 1 false fixed 0 0
39 0x2022 0x2110 8226 8464 input.touch.y input.pad.a.pressed 1 input.touch input.pad.a y pressed 1 0 1 builtin:input.touch int Bool true false 1 false fixed 0 0
40 0x2030 0x2111 8240 8465 input.button.pressed input.pad.a.released 1 input.button input.pad.a pressed released 1 0 1 builtin:input.button bool Bool true false 1 false fixed 0 0
41 0x2031 0x2112 8241 8466 input.button.released input.pad.a.down 1 input.button input.pad.a released down 1 0 1 builtin:input.button bool Bool true false 1 false fixed 0 0
42 0x2032 0x2113 8242 8467 input.button.down input.pad.a.hold 1 input.button input.pad.a down hold 1 0 1 builtin:input.button bool Int64 true false 1 false fixed 0 0
43 0x2033 0x2114 8243 8468 input.button.hold input.pad.b.pressed 1 input.button input.pad.b hold pressed 1 0 1 builtin:input.button int Bool true false 1 false fixed 0 0
44 0x2115 8469 input.pad.b.released 1 input.pad.b released 0 1 Bool true false 1 false fixed 0 0
45 0x2116 8470 input.pad.b.down 1 input.pad.b down 0 1 Bool true false 1 false fixed 0 0
46 0x2117 8471 input.pad.b.hold 1 input.pad.b hold 0 1 Int64 true false 1 false fixed 0 0
47 0x2118 8472 input.pad.x.pressed 1 input.pad.x pressed 0 1 Bool true false 1 false fixed 0 0
48 0x2119 8473 input.pad.x.released 1 input.pad.x released 0 1 Bool true false 1 false fixed 0 0
49 0x211A 8474 input.pad.x.down 1 input.pad.x down 0 1 Bool true false 1 false fixed 0 0
50 0x211B 8475 input.pad.x.hold 1 input.pad.x hold 0 1 Int64 true false 1 false fixed 0 0
51 0x211C 8476 input.pad.y.pressed 1 input.pad.y pressed 0 1 Bool true false 1 false fixed 0 0
52 0x211D 8477 input.pad.y.released 1 input.pad.y released 0 1 Bool true false 1 false fixed 0 0
53 0x211E 8478 input.pad.y.down 1 input.pad.y down 0 1 Bool true false 1 false fixed 0 0
54 0x211F 8479 input.pad.y.hold 1 input.pad.y hold 0 1 Int64 true false 1 false fixed 0 0
55 0x2120 8480 input.pad.l.pressed 1 input.pad.l pressed 0 1 Bool true false 1 false fixed 0 0
56 0x2121 8481 input.pad.l.released 1 input.pad.l released 0 1 Bool true false 1 false fixed 0 0
57 0x2122 8482 input.pad.l.down 1 input.pad.l down 0 1 Bool true false 1 false fixed 0 0
58 0x2123 8483 input.pad.l.hold 1 input.pad.l hold 0 1 Int64 true false 1 false fixed 0 0
59 0x2124 8484 input.pad.r.pressed 1 input.pad.r pressed 0 1 Bool true false 1 false fixed 0 0
60 0x2125 8485 input.pad.r.released 1 input.pad.r released 0 1 Bool true false 1 false fixed 0 0
61 0x2126 8486 input.pad.r.down 1 input.pad.r down 0 1 Bool true false 1 false fixed 0 0
62 0x2127 8487 input.pad.r.hold 1 input.pad.r hold 0 1 Int64 true false 1 false fixed 0 0
63 0x2128 8488 input.pad.start.pressed 1 input.pad.start pressed 0 1 Bool true false 1 false fixed 0 0
64 0x2129 8489 input.pad.start.released 1 input.pad.start released 0 1 Bool true false 1 false fixed 0 0
65 0x212A 8490 input.pad.start.down 1 input.pad.start down 0 1 Bool true false 1 false fixed 0 0
66 0x212B 8491 input.pad.start.hold 1 input.pad.start hold 0 1 Int64 true false 1 false fixed 0 0
67 0x212C 8492 input.pad.select.pressed 1 input.pad.select pressed 0 1 Bool true false 1 false fixed 0 0
68 0x212D 8493 input.pad.select.released 1 input.pad.select released 0 1 Bool true false 1 false fixed 0 0
69 0x212E 8494 input.pad.select.down 1 input.pad.select down 0 1 Bool true false 1 false fixed 0 0
70 0x212F 8495 input.pad.select.hold 1 input.pad.select hold 0 1 Int64 true false 1 false fixed 0 0
71 0x2130 8496 input.touch.button.pressed 1 input.touch.button pressed 0 1 Bool true false 1 false fixed 0 0
72 0x2131 8497 input.touch.button.released 1 input.touch.button released 0 1 Bool true false 1 false fixed 0 0
73 0x2132 8498 input.touch.button.down 1 input.touch.button down 0 1 Bool true false 1 false fixed 0 0
74 0x2133 8499 input.touch.button.hold 1 input.touch.button hold 0 1 Int64 true false 1 false fixed 0 0
75 0x2134 8500 input.touch.x 1 input.touch x 0 1 Int64 true false 1 false fixed 0 0
76 0x2135 8501 input.touch.y 1 input.touch y 0 1 Int64 true false 1 false fixed 0 0
77 0x3000 12288 math.i32.min 1 math.i32 min 2 1 Int32|Int32 Int32 true false a|b 2 false fixed 0 0
78 0x3001 12289 math.i32.max 1 math.i32 max 2 1 Int32|Int32 Int32 true false a|b 2 false fixed 0 0
79 0x3002 12290 math.i32.clamp 1 math.i32 clamp 3 1 Int32|Int32|Int32 Int32 true false value|min|max 4 false fixed 0 0
80 0x3003 12291 math.i32.abs 1 math.i32 abs 1 1 Int32 Int32 true false value 2 false fixed 0 0
81 0x3004 12292 math.i64.min 1 math.i64 min 2 1 Int64|Int64 Int64 true false a|b 2 false fixed 0 0
82 0x3005 12293 math.i64.max 1 math.i64 max 2 1 Int64|Int64 Int64 true false a|b 2 false fixed 0 0
83 0x3006 12294 math.i64.clamp 1 math.i64 clamp 3 1 Int64|Int64|Int64 Int64 true false value|min|max 4 false fixed 0 0
84 0x3007 12295 math.i64.abs 1 math.i64 abs 1 1 Int64 Int64 true false value 2 false fixed 0 0
85 0x3008 12296 mathf.min 1 mathf min 2 1 Float|Float Float true false a|b 2 false fixed 0 0
86 0x3009 12297 mathf.max 1 mathf max 2 1 Float|Float Float true false a|b 2 false fixed 0 0
87 0x300A 12298 mathf.clamp 1 mathf clamp 3 1 Float|Float|Float Float true false value|min|max 4 false fixed 0 0
88 0x300B 12299 mathf.abs 1 mathf abs 1 1 Float Float true false value 2 false fixed 0 0
89 0x300C 12300 mathf.floor 1 mathf floor 1 1 Float Float true false value 4 false fixed 0 0
90 0x300D 12301 mathf.ceil 1 mathf ceil 1 1 Float Float true false value 4 false fixed 0 0
91 0x300E 12302 mathf.trunc 1 mathf trunc 1 1 Float Float true false value 4 false fixed 0 0
92 0x300F 12303 mathf.round 1 mathf round 1 1 Float Float true false value 4 false fixed 0 0
93 0x3010 12304 mathf.sqrt 1 mathf sqrt 1 1 Float Float true false value 16 false fixed 0 0
94 0x3011 12305 mathf.sin 1 mathf sin 1 1 Float Float true false angle 32 false fixed 0 0
95 0x3012 12306 mathf.cos 1 mathf cos 1 1 Float Float true false angle 32 false fixed 0 0
96 0x3013 12307 mathf.atan2 1 mathf atan2 2 1 Float|Float Float true false y|x 48 false fixed 0 0
97 0x3014 12308 math.i32.to_i64 1 math.i32 to_i64 1 1 Int32 Int64 true false value 2 false fixed 0 0
98 0x3015 12309 math.i64.to_i32 1 math.i64 to_i32 1 1 Int64 Int32 true false value 2 false fixed 0 0
99 0x3016 12310 math.i32.to_float 1 math.i32 to_float 1 1 Int32 Float true false value 2 false fixed 0 0
100 0x3017 12311 math.i64.to_float 1 math.i64 to_float 1 1 Int64 Float true false value 2 false fixed 0 0
101 0x3018 12312 mathf.to_i32 1 mathf to_i32 1 1 Float Int32 true false value 2 false fixed 0 0
102 0x3019 12313 mathf.to_i64 1 mathf to_i64 1 1 Float Int64 true false value 2 false fixed 0 0
103 0x4000 16384 random.new 1 random new 1 1 Int64 builtin:Random true true seed 32 true fixed 0 0
104 0x4001 16385 random.next 1 random next 1 1 builtin:Random Int64 true false rng 8 true fixed 0 0
105 0x4002 16386 random.nextf 1 random nextf 1 1 builtin:Random Float true false rng 12 true fixed 0 0
106 0x4003 16387 random.range 1 random range 3 1 builtin:Random|Int64|Int64 Int64 true false rng|min|max 4 true random_rejection_v1 8 64
+24 -1
View File
@@ -10,6 +10,7 @@ Canonical runtime sources:
- `../runtime/docs/vm-arch/ARCHITECTURE.md`
- `../runtime/docs/vm-arch/ISA_CORE.md`
- `../runtime/docs/vm-arch/INTRINSICS.csv`
- `../runtime/docs/vm-arch/BUILTIN_CONSTANTS.csv`
When these mirrors are retained, an integration change that updates a canonical
runtime source MUST update the corresponding Studio mirror in the same logical
@@ -19,9 +20,10 @@ change. Verify synchronization from the Studio repository root with:
diff -u docs/vm-arch/ARCHITECTURE.md ../runtime/docs/vm-arch/ARCHITECTURE.md
diff -u docs/vm-arch/ISA_CORE.md ../runtime/docs/vm-arch/ISA_CORE.md
diff -u docs/vm-arch/INTRINSICS.csv ../runtime/docs/vm-arch/INTRINSICS.csv
diff -u docs/vm-arch/BUILTIN_CONSTANTS.csv ../runtime/docs/vm-arch/BUILTIN_CONSTANTS.csv
```
All three commands MUST exit successfully with no output before a change claims
All four commands MUST exit successfully with no output before a change claims
that the mirrors are synchronized. If the runtime repository is not available
in the same checkout, synchronization evidence must be produced against the
canonical runtime revision before merging.
@@ -30,3 +32,24 @@ Compiler-owned transformation contracts remain under `docs/specs/compiler`.
Those specifications define `IRBackend -> IRVM -> PBX` emission, but they MUST
conform to the runtime-owned PBX and ISA contracts and MUST NOT redefine runtime
execution semantics.
## Catalog provenance
`runtime-catalog.properties` identifies the selected provider revision and hashes
of both CSVs. The compiler and PBS SDK package these artifacts with their own
resource namespaces. Catalog synchronization alone does not claim the other
architecture mirrors are synchronized or that all PBS facades are implemented.
Run the explicit source/exporter verification from the Studio root:
```sh
python3 tools/runtime-catalog/verify.py --runtime ../runtime
```
This command requires a clean checkout of the recorded provider revision, compares
committed bytes and hashes, and runs its real catalog exporter parity test with
build output in a temporary directory. Missing or different evidence sources fail
explicitly. Ordinary builds and local catalog tests never invoke this command.
The manual port and consumer requirements are defined in
[Runtime catalog support](../compiler/general/specs/runtime-catalog-support.md).
+3
View File
@@ -0,0 +1,3 @@
runtime.revision=f7d7c167531bb662a1af9d73fc326bb476bf8aa7
INTRINSICS.csv.sha256=0c1bb78a9b81d1e3480ef5713f3d70f0b06772a7dfab2fd00c1f0a6364f98daa
BUILTIN_CONSTANTS.csv.sha256=e407e4718127aa7ef7b1097b5b4b67d86dd979235e73a551a7c36b628cddc039
@@ -245,7 +245,9 @@ final class HeadlessProcessSession implements HeadlessGateway.Session {
default -> AutomationFailure.Code.INFRASTRUCTURE_FAILURE;
};
pending.remove(item.id);
item.result.completeExceptionally(new AutomationException(code, "Runtime " + error.path("code").asText() + ": " + error.path("message").asText()));
Map<String, Object> runtimeError = mapper.convertValue(error, new com.fasterxml.jackson.core.type.TypeReference<>() {});
item.result.completeExceptionally(new AutomationException(new AutomationFailure(code,
"Runtime " + error.path("code").asText() + ": " + error.path("message").asText(), runtimeError)));
} else {
var result = message.get("result");
if (item.command.equals("cancel") && (!result.path("requested").isBoolean() || !result.path("requested").booleanValue()
@@ -19,6 +19,8 @@ public final class InstalledKnowledgeProbe {
check(provider.getClass().getProtectionDomain().getCodeSource().getLocation().getPath().endsWith(".jar"), "Frontend must come from installed JAR");
check(CompilerProjectKnowledge.class.getProtectionDomain().getCodeSource().getLocation().getPath().endsWith(".jar"), "App must come from installed JAR");
check(!Files.exists(Path.of("settings.gradle.kts")) && !Files.exists(Path.of("docs")), "No repository in working directory");
verifyRuntimeCatalogs(provider.getClass().getClassLoader());
if (arguments.length == 1 && arguments[0].equals("--catalog-only")) return;
Path root = Files.createDirectory(Path.of("example")).toRealPath();
var docs = provider.documentation().orElseThrow().references(1, p.studio.AppMode.Game);
for (var reference : docs) {
@@ -49,7 +51,7 @@ public final class InstalledKnowledgeProbe {
check(actual.toString().equals(reference.text()), "Incorrect installed reference: " + reference.id());
}
var result = BuilderPipelineService.INSTANCE.compile(
BuilderPipelineContext.fromConfig(new BuilderPipelineConfig(false, root.toString())), LogAggregator.empty());
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");
verifyExternalSources(provider.getClass().getClassLoader());
@@ -61,6 +63,30 @@ public final class InstalledKnowledgeProbe {
if (!condition) throw new AssertionError(message);
}
private static void verifyRuntimeCatalogs(ClassLoader loader) throws Exception {
var names = java.util.List.of("INTRINSICS.csv", "BUILTIN_CONSTANTS.csv", "runtime-catalog.properties");
var compiler = new java.util.HashMap<String, byte[]>();
for (var name : names) {
for (var owner : java.util.List.of("compiler", "pbs-sdk")) {
var resource = loader.getResource("runtime-catalog/" + owner + "/" + name);
check(resource != null && resource.getProtocol().equals("jar"), "Missing installed catalog: " + owner + "/" + name);
try (var stream = resource.openStream()) {
var bytes = stream.readAllBytes();
if (owner.equals("compiler")) compiler.put(name, bytes);
else check(java.util.Arrays.equals(compiler.get(name), bytes), "Compiler/SDK contract mismatch: " + name);
}
}
}
var properties = new java.util.Properties();
properties.load(new java.io.ByteArrayInputStream(compiler.get("runtime-catalog.properties")));
check(properties.getProperty("runtime.revision").matches("[0-9a-f]{40}"), "Missing target revision");
for (var name : names.subList(0, 2)) {
var digest = java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(compiler.get(name)));
check(digest.equals(properties.getProperty(name + ".sha256")), "Packaged catalog hash mismatch: " + name);
}
System.out.println("Installed compiler/SDK catalog identity verified: " + properties.getProperty("runtime.revision"));
}
private static void verifyExternalSources(ClassLoader loader) throws Exception {
var resource = loader.getResource("game/stdlib/1/sdk/gfx/main.pbs");
check(resource != null && resource.getProtocol().equals("jar"), "SDK must be read from a JAR URL");
@@ -12,6 +12,17 @@ class InstalledKnowledgeTest {
@TempDir Path outsideRepository;
@Test void installedJarsProvideAllReferencesAndCompileTheExampleOutsideTheCheckout() throws Exception {
String log = runProbe();
assertTrue(log.contains("references; example compiled"), log);
assertTrue(log.contains("Packaged external sources verified"), log);
}
@Test void installedCompilerAndSdkCarryTheSameVerifiedCatalog() throws Exception {
String log = runProbe("--catalog-only");
assertTrue(log.contains("Installed compiler/SDK catalog identity verified"), log);
}
private String runProbe(String... arguments) throws Exception {
Path distribution = Path.of(System.getProperty("studio.test.distribution"));
String jars;
try (var files = Files.list(distribution.resolve("lib"))) {
@@ -20,15 +31,16 @@ class InstalledKnowledgeTest {
}
String probeClasses = Path.of(InstalledKnowledgeProbe.class.getProtectionDomain().getCodeSource().getLocation().toURI()).toString();
Path output = outsideRepository.resolve("probe.log");
var process = new ProcessBuilder(distribution.resolve("runtime/bin/java").toString(),
"-cp", jars + java.io.File.pathSeparator + probeClasses, InstalledKnowledgeProbe.class.getName())
var command = new java.util.ArrayList<>(java.util.List.of(distribution.resolve("runtime/bin/java").toString(),
"-cp", jars + java.io.File.pathSeparator + probeClasses, InstalledKnowledgeProbe.class.getName()));
command.addAll(java.util.List.of(arguments));
var process = new ProcessBuilder(command)
.directory(outsideRepository.toFile()).redirectErrorStream(true).redirectOutput(output.toFile()).start();
try {
assertTrue(process.waitFor(45, TimeUnit.SECONDS), "Installed knowledge probe timed out");
String log = Files.readString(output);
assertEquals(0, process.exitValue(), log);
assertTrue(log.contains("references; example compiled"), log);
assertTrue(log.contains("Packaged external sources verified"), log);
return log;
} finally { if (process.isAlive()) process.destroyForcibly().waitFor(); }
}
}
@@ -50,7 +50,15 @@ public final class HeadlessFixture {
var request = MAPPER.readTree(line); String id = request.path("requestId").asText(), command = request.path("command").asText();
switch (command) {
case "initialize" -> response(id, MAPPER.createObjectNode().setAll(Map.of("protocolVersion", hello.path("protocolVersion"), "capabilities", hello.path("capabilities"))));
case "boot", "reset" -> response(id, state());
case "boot", "reset" -> {
if (mode.startsWith("load-fault-")) {
var resource = "/runtime/fault-" + mode.substring("load-fault-".length()) + ".json";
var fault = MAPPER.readTree(HeadlessFixture.class.getResourceAsStream(resource));
var error = MAPPER.createObjectNode().put("code", "boot_failed").put("message", "Runtime rejected cartridge");
error.putObject("details").set("fault", fault);
emit(MAPPER.createObjectNode().put("type", "response").put("sessionId", "fixture").put("requestId", id).set("error", error));
} else response(id, state());
}
case "get_state" -> { if (!mode.equals("hang-call")) response(id, state()); }
case "get_fault" -> response(id, MAPPER.nullNode());
case "advance" -> {
@@ -284,14 +284,14 @@ class HeadlessMcpDesktopTest {
assertTrue(value(call("studio_project_context", args())).path("languages").toString().contains("pbs"));
value(call("studio_reference_read", args().put("referenceId", "pbs:quickstart").put("limit", 100)));
var sdk = value(call("studio_reference_read", args().put("referenceId", "pbs:sdk:game:1:sdk/input/main.pbs").put("limit", 1000)));
assertTrue(sdk.toString().contains("InputPad"), "Consult installed SDK source through the indexed read-only reference");
assertTrue(sdk.toString().contains("LowPad"), "Consult installed SDK source through the indexed read-only reference");
assertEquals(ControlState.WAITING_FOR_AGENT, project.automation().snapshot().state());
task = value(call("studio_control_acquire", args())).path("taskId").asText();
assertFalse(task.isBlank());
// Agent-native edit of the installed example; SDK remains unchanged.
String edited = Files.readString(source).replace("import { Gfx } from @sdk:gfx;",
"import { Gfx } from @sdk:gfx;\nimport { Input } from @sdk:input;")
.replace("x = x + 1;", "if (Input.pad().right().down()) { x = x + 3; } else { x = x + 1; }");
"import { Gfx } from @sdk:gfx;\nimport { Pad, Button, Touch } from @sdk:input;")
.replace("x = x + 1;", "if (Pad.down(Button.Right)) { x = x + 3; } else { x = x + 1; }");
Files.writeString(source, edited);
var query = args().put("operation", "diagnostics"); query.putObject("parameters").put("uri", source.toUri().toString());
assertTrue(value(call("studio_language_query", query)).path("result").isEmpty());
@@ -18,6 +18,20 @@ class HeadlessProcessSessionTest {
var session = new HeadlessProcessSession(process, new ObjectMapper(), HeadlessFixture.limits(mode), "build"); sessions.add(session); return session;
}
@AfterEach void cleanup() { sessions.forEach(session -> session.abort("test cleanup")); }
@Test void productionLoadDiagnosticsSurviveBootFailureAndMcpSerialization() throws Exception {
var mapper = new ObjectMapper();
for (String fixture : List.of("resource", "invalid-role", "retired-opcode", "native", "native-abi", "unknown-native")) {
var session = start("load-fault-" + fixture);
var failure = assertThrows(AutomationException.class, () -> session.initializeAndBoot(root)).failure();
var fault = mapper.readTree(getClass().getResourceAsStream("/runtime/fault-" + fixture + ".json"));
var error = mapper.valueToTree(failure.runtimeError());
assertEquals(fault, error.path("details").path("fault"));
assertTrue(error.path("details").path("fault").path("pc").isNull());
var wire = p.studio.app.mcp.McpToolResult.failure(mapper, failure).wire();
assertEquals(fault, wire.path("structuredContent").path("error").path("runtimeError").path("details").path("fault"));
assertThrows(UnsupportedOperationException.class, () -> failure.runtimeError().put("code", "changed"));
}
}
@Test void fragmentedProtocolPreservesUnsignedFrameIdentityLayoutAndSeparateStderr() throws Exception {
var session = start("normal"); session.initializeAndBoot(root);
var outcome = session.advance(3, ignored -> {});
@@ -0,0 +1,24 @@
{
"initError": {
"code": "registry_load_failed",
"load": {
"code": "LOAD_INVALID_REGISTRY",
"function": 0,
"id_domain": null,
"limit": null,
"member": null,
"offset": 136,
"pc": null,
"reason": "LifecycleRole",
"record": 0,
"referenced_id": null,
"requested": null,
"resource": null,
"section": 1
},
"message": "RegistryLoadFailed(LoadFailure { code: InvalidRegistry, reason: LifecycleRole, section: Some(1), offset: Some(136), record: Some(0), member: None, id_domain: None, referenced_id: None, function: Some(0), pc: None, resource: None, requested: None, limit: None })"
},
"kind": "vm_init",
"pc": null,
"summary": "Conformance fixture"
}
@@ -0,0 +1,19 @@
{
"initError": {
"binding": {
"declared_arg_slots": 1,
"declared_ret_slots": 1,
"expected_arg_slots": 0,
"expected_ret_slots": 1,
"module": "system",
"name": "has_cart",
"reason": "abi_mismatch",
"version": 1
},
"code": "loader_patch_failed",
"message": "LoaderPatchFailed(ResolveFailed(AbiMismatch { module: \"system\", name: \"has_cart\", version: 1, declared_arg_slots: 1, declared_ret_slots: 1, expected_arg_slots: 0, expected_ret_slots: 1 }))"
},
"kind": "vm_init",
"pc": null,
"summary": "Conformance fixture"
}
@@ -0,0 +1,17 @@
{
"initError": {
"binding": {
"module": "system",
"name": "has_cart",
"provided": 0,
"reason": "missing_capability",
"required": 1,
"version": 1
},
"code": "loader_patch_failed",
"message": "LoaderPatchFailed(ResolveFailed(MissingCapability { required: 1, provided: 0, module: \"system\", name: \"has_cart\", version: 1 }))"
},
"kind": "vm_init",
"pc": null,
"summary": "Conformance fixture"
}
@@ -0,0 +1,24 @@
{
"initError": {
"code": "registry_load_failed",
"load": {
"code": "LOAD_RESOURCE_LIMIT",
"function": null,
"id_domain": null,
"limit": 0,
"member": null,
"offset": null,
"pc": null,
"reason": "Limit",
"record": null,
"referenced_id": null,
"requested": 32,
"resource": "Work",
"section": null
},
"message": "RegistryLoadFailed(LoadFailure { code: ResourceLimit, reason: Limit, section: None, offset: None, record: None, member: None, id_domain: None, referenced_id: None, function: None, pc: None, resource: Some(Work), requested: Some(32), limit: Some(0) })"
},
"kind": "vm_init",
"pc": null,
"summary": "Conformance fixture"
}
@@ -0,0 +1,24 @@
{
"initError": {
"code": "registry_load_failed",
"load": {
"code": "LOAD_INVALID_CODE",
"function": 0,
"id_domain": null,
"limit": null,
"member": null,
"offset": null,
"pc": 0,
"reason": "Truncated",
"record": null,
"referenced_id": null,
"requested": null,
"resource": null,
"section": 2
},
"message": "RegistryLoadFailed(LoadFailure { code: InvalidCode, reason: Truncated, section: Some(2), offset: None, record: None, member: None, id_domain: None, referenced_id: None, function: Some(0), pc: Some(0), resource: None, requested: None, limit: None })"
},
"kind": "vm_init",
"pc": null,
"summary": "Conformance fixture"
}
@@ -0,0 +1,15 @@
{
"initError": {
"binding": {
"module": "system",
"name": "missing",
"reason": "unknown_syscall",
"version": 1
},
"code": "loader_patch_failed",
"message": "LoaderPatchFailed(ResolveFailed(UnknownSyscall { module: \"system\", name: \"missing\", version: 1 }))"
},
"kind": "vm_init",
"pc": null,
"summary": "Conformance fixture"
}
@@ -13,9 +13,11 @@ dependencies {
// Build-time projection of owner-maintained sources; no second normative copy is edited by Studio.
val pbsSpecs = rootProject.layout.projectDirectory.dir("docs/specs/compiler-languages/pbs")
val pbsAlignmentSpecs = rootProject.layout.projectDirectory.dir("docs/compiler/pbs/specs")
val pbsResources = layout.projectDirectory.dir("src/main/resources")
val generatePbsKnowledge by tasks.registering(Sync::class) {
from(pbsSpecs) { include("*.md"); into("pbs-knowledge/specs") }
from(pbsAlignmentSpecs) { include("*.md"); into("pbs-knowledge/specs") }
into(layout.buildDirectory.dir("generated/pbsKnowledge"))
inputs.files(fileTree(pbsResources) { include("game/stdlib/**/*.pbs", "game/stdlib/**/*.barrel", "pbs-knowledge/examples/**") })
doLast {
@@ -27,7 +29,7 @@ val generatePbsKnowledge by tasks.registering(Sync::class) {
entries.add(linkedMapOf("id" to id, "title" to title, "kind" to kind,
"resource" to resource, "sha256" to digest, "sdkVersion" to sdk, "target" to target))
}
pbsSpecs.asFile.listFiles()!!.filter { it.extension == "md" }.sortedBy { it.name }.forEach { source ->
(pbsSpecs.asFile.listFiles()!!.toList() + (pbsAlignmentSpecs.asFile.listFiles()?.toList() ?: emptyList())).filter { it.extension == "md" }.sortedBy { it.name }.forEach { source ->
val chapter = source.name.substringBefore(". ")
val quickstart = source.name == "README.md"
entry(if (quickstart) "pbs:quickstart" else "pbs:spec:$chapter",
@@ -54,3 +56,10 @@ val generatePbsKnowledge by tasks.registering(Sync::class) {
}
}
sourceSets.main { resources.srcDir(generatePbsKnowledge) }
tasks.named<ProcessResources>("processResources") {
from(rootProject.layout.projectDirectory.dir("docs/vm-arch")) {
include("INTRINSICS.csv", "BUILTIN_CONSTANTS.csv", "runtime-catalog.properties")
into("runtime-catalog/pbs-sdk")
}
}
@@ -25,7 +25,7 @@ import java.util.Map;
import java.util.Set;
public final class PBSSemanticTokenProvider implements FrontendSemanticTokenProvider {
private static final Set<String> BUILTIN_TYPE_NAMES = Set.of("int", "float", "bool", "string", "i32", "i64", "f32", "f64");
private static final Set<String> BUILTIN_TYPE_NAMES = Set.of("int", "long", "float", "bool", "string", "i32", "i64", "f32", "f64");
private static final int DEFAULT_STDLIB_VERSION = 1;
private static final AppMode DEFAULT_TARGET = AppMode.Game;
private static final ResourceStdlibEnvironmentResolver STDLIB_RESOLVER = new ResourceStdlibEnvironmentResolver();
@@ -234,7 +234,7 @@ public final class PbsFrontendCompiler {
effectiveModulePool,
ReadOnlyList.wrap(callableSignatures),
executableLowering.intrinsicPool(),
lifecycleDeclaration);
lifecycleDeclaration, executableLowering.registry());
}
public String callableShapeSurfaceOf(final PbsAst.FunctionDecl functionDecl) {
@@ -43,6 +43,12 @@ public final class PbsModuleVisibilityValidator {
}
}
private static String inputMigrationHint(String module, String name) {
return "@sdk:input".equals(module) && java.util.Set.of("Input", "InputType", "InputPad", "InputTouch", "InputButton").contains(name)
? "; migrate to Pad.pressed/released/down/hold(Button.A) or Touch queries. Intermediate input objects are no longer values."
: "";
}
private ModuleExports validateModule(
final ModuleUnit module,
final NameTable nameTable,
@@ -219,7 +225,7 @@ public final class PbsModuleVisibilityValidator {
PbsLinkErrors.E_LINK_IMPORT_SYMBOL_UNRESOLVED.name(),
"Symbol '%s' does not exist in module %s".formatted(
importItem.name(),
displayModule(targetModule)),
displayModule(targetModule)) + inputMigrationHint(displayModule(targetModule), importItem.name()),
importItem.span());
continue;
}
@@ -28,7 +28,7 @@ final class PbsExecutableBodyLowerer {
final PbsExecutableMetadataIndex metadataIndex,
final PbsExecutableCallableRegistry callableRegistry,
final IntrinsicTable intrinsicIdTable) {
final var localSlotByNameId = initialLocalSlots(functionDecl, nameTable);
final var localSlotByNameId = initialLocalSlots(functionDecl, nameTable, metadataIndex.types());
final var localOwnerByNameId = initialLocalOwners(functionDecl, nameTable, metadataIndex);
final var context = new PbsExecutableLoweringContext(
diagnostics,
@@ -38,11 +38,13 @@ final class PbsExecutableBodyLowerer {
intrinsicIdTable,
localSlotByNameId,
localOwnerByNameId,
functionDecl.parameters().size());
functionDecl.parameters().stream().mapToInt(p -> metadataIndex.types().slots(p.typeRef())).sum());
for (var parameter : functionDecl.parameters()) context.localTypes.put(parameter.name(), parameter.typeRef());
context.returnType = functionDecl.returnType();
final var terminated = lowerBlock(functionDecl.body(), context);
finalizeFunctionInstructions(functionDecl, context, terminated);
final var returnSlots = returnSlotsFor(functionDecl);
final var returnSlots = functionDecl.returnKind() == PbsAst.ReturnKind.PLAIN ? context.types().slots(functionDecl.returnType()) : 0;
final int maxStackSlots;
try {
maxStackSlots = stackAnalyzer.analyzeMaxStackSlots(ReadOnlyList.wrap(context.instructions()), returnSlots);
@@ -56,7 +58,7 @@ final class PbsExecutableBodyLowerer {
functionDecl.span());
return null;
}
final var localSlots = Math.max(0, context.nextLocalSlot() - functionDecl.parameters().size());
final var localSlots = Math.max(0, context.nextLocalSlot() - functionDecl.parameters().stream().mapToInt(p -> context.types().slots(p.typeRef())).sum());
return new PbsLoweredExecutableBody(
ReadOnlyList.wrap(context.instructions()),
localSlots,
@@ -65,10 +67,12 @@ final class PbsExecutableBodyLowerer {
private Map<NameId, Integer> initialLocalSlots(
final PbsAst.FunctionDecl functionDecl,
final NameTable nameTable) {
final NameTable nameTable, final PbsRuntimeTypes types) {
final var localSlotByNameId = new HashMap<NameId, Integer>();
for (var paramIndex = 0; paramIndex < functionDecl.parameters().size(); paramIndex++) {
localSlotByNameId.put(nameTable.register(functionDecl.parameters().get(paramIndex).name()), paramIndex);
int slot = 0;
for (var parameter : functionDecl.parameters()) {
localSlotByNameId.put(nameTable.register(parameter.name()), slot);
slot += types.slots(parameter.typeRef());
}
return localSlotByNameId;
}
@@ -155,7 +159,9 @@ final class PbsExecutableBodyLowerer {
final var localOwner = callsiteEmitter.resolveExpressionOwnerForLowering(letStatement.initializer(), context);
final var tupleProjection = tupleProjectionOf(letStatement.initializer(), context);
final var valueSlots = Math.max(1, materializedValueSlots(letStatement.initializer(), context));
lowerExpression(letStatement.initializer(), context);
final var localType = letStatement.explicitType() != null ? letStatement.explicitType() : expressionType(letStatement.initializer(), context);
lowerExpected(letStatement.initializer(), localType, context);
context.localTypes.put(letStatement.name(), localType);
final var localSlot = context.declareLocalSlots(letStatement.name(), valueSlots);
context.bindLocalOwner(letStatement.name(), localOwner);
context.bindLocalTupleProjection(letStatement.name(), tupleProjection);
@@ -185,7 +191,7 @@ final class PbsExecutableBodyLowerer {
final var localOwner = callsiteEmitter.resolveExpressionOwnerForLowering(assignStatement.value(), context);
final var tupleProjection = tupleProjectionOf(assignStatement.value(), context);
final var valueSlots = Math.max(1, materializedValueSlots(assignStatement.value(), context));
lowerExpression(assignStatement.value(), context);
lowerExpected(assignStatement.value(), context.localTypes.getOrDefault(target.rootName(), context.types().values.get(target.rootName())), context);
if (targetLocalSlot != null) {
context.bindLocalOwner(target.rootName(), localOwner);
context.bindLocalTupleProjection(target.rootName(), tupleProjection);
@@ -200,7 +206,7 @@ final class PbsExecutableBodyLowerer {
} else {
emitGetGlobal(targetGlobalSlot, assignStatement.span(), context);
}
lowerExpression(assignStatement.value(), context);
lowerExpected(assignStatement.value(), context.localTypes.getOrDefault(target.rootName(), context.types().values.get(target.rootName())), context);
emitBinaryOperatorInstruction(compoundAssignBinaryOperator(assignStatement.operator()), assignStatement.span(), context);
if (targetLocalSlot != null) {
context.bindLocalOwner(target.rootName(), "");
@@ -235,7 +241,7 @@ final class PbsExecutableBodyLowerer {
final PbsAst.ReturnStatement returnStatement,
final PbsExecutableLoweringContext context) {
if (returnStatement.value() != null) {
lowerExpression(returnStatement.value(), context);
lowerExpected(returnStatement.value(), context.returnType, context);
}
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.RET,
@@ -363,10 +369,7 @@ final class PbsExecutableBodyLowerer {
}
switch (expression) {
case PbsAst.CallExpr callExpr -> lowerCallExpression(callExpr, context);
case PbsAst.ApplyExpr applyExpr -> {
lowerExpression(applyExpr.callee(), context);
lowerExpression(applyExpr.argument(), context);
}
case PbsAst.ApplyExpr applyExpr -> lowerApply(applyExpr, context);
case PbsAst.BinaryExpr binaryExpr -> {
lowerExpression(binaryExpr.left(), context);
lowerExpression(binaryExpr.right(), context);
@@ -381,36 +384,36 @@ final class PbsExecutableBodyLowerer {
lowerExpression(elseExpr.fallbackExpression(), context);
}
case PbsAst.IfExpr ifExpr -> lowerIfExpression(ifExpr, context);
case PbsAst.SwitchExpr switchExpr -> lowerUnsupportedExpression(
switchExpr.selector(),
"switch expression is not lowerable in executable lowering v1",
switchExpr.span(),
context);
case PbsAst.SwitchExpr switchExpr -> lowerSwitch(switchExpr, context);
case PbsAst.HandleExpr handleExpr -> lowerUnsupportedExpression(
handleExpr.value(),
"handle expression is not lowerable in executable lowering v1",
handleExpr.span(),
context);
case PbsAst.AsExpr asExpr -> lowerExpression(asExpr.expression(), context);
case PbsAst.AsExpr asExpr -> lowerConversion(asExpr, context);
case PbsAst.MemberExpr memberExpr -> lowerMemberExpression(memberExpr, context);
case PbsAst.PropagateExpr propagateExpr -> lowerExpression(propagateExpr.expression(), context);
case PbsAst.GroupExpr groupExpr -> lowerExpression(groupExpr.expression(), context);
case PbsAst.NewExpr newExpr -> lowerExpressionList(newExpr.arguments(), context);
case PbsAst.NewExpr newExpr -> {
var fields = context.types().fields.get(newExpr.typeName());
for(int i=0; i<newExpr.arguments().size(); i++) lowerExpected(newExpr.arguments().get(i), fields != null && i < fields.size() ? fields.get(i).typeRef() : null, context);
}
case PbsAst.BindExpr bindExpr -> lowerExpression(bindExpr.contextExpression(), context);
case PbsAst.SomeExpr someExpr -> lowerExpression(someExpr.value(), context);
case PbsAst.OkExpr okExpr -> lowerExpression(okExpr.value(), context);
case PbsAst.TupleExpr tupleExpr -> {
for (final var item : tupleExpr.items()) {
lowerExpression(item.expression(), context);
var expected = context.expectedType;
for (int index=0; index<tupleExpr.items().size(); index++) {
var item=tupleExpr.items().get(index);
var fieldType=expected != null && expected.kind()==PbsAst.TypeRefKind.NAMED_TUPLE && index<expected.fields().size() ? expected.fields().get(index).typeRef() : null;
lowerExpected(item.expression(), fieldType, context);
}
}
case PbsAst.BlockExpr blockExpr -> lowerBlock(blockExpr.block(), context);
case PbsAst.IdentifierExpr identifierExpr -> lowerIdentifierExpression(identifierExpr, context);
case PbsAst.IntLiteralExpr intLiteralExpr -> lowerIntLiteral(intLiteralExpr, context);
case PbsAst.FloatLiteralExpr ignored -> {
}
case PbsAst.BoundedLiteralExpr ignored -> {
}
case PbsAst.FloatLiteralExpr literal -> emitLiteral(IRBackendExecutableFunction.InstructionKind.PUSH_F64, Double.toString(literal.value()), literal.span(), context);
case PbsAst.BoundedLiteralExpr literal -> emitPushI32(literal.value(), literal.span(), context);
case PbsAst.StringLiteralExpr stringLiteralExpr -> emitPushConst(stringLiteralExpr.value(), stringLiteralExpr.span(), context);
case PbsAst.BoolLiteralExpr boolLiteralExpr -> emitPushBool(boolLiteralExpr.value(), boolLiteralExpr.span(), context);
case PbsAst.ThisExpr ignored -> {
@@ -437,7 +440,10 @@ final class PbsExecutableBodyLowerer {
final PbsExecutableLoweringContext context) {
final var hostBinding = callsiteEmitter.resolveUniqueHostBinding(callExpr, context);
if (hostBinding == null || hostBinding.assetLoweringParam() == null) {
lowerExpressionList(callExpr.arguments(), context);
final var signature = context.types().signatures.get(PbsRuntimeTypes.path(callExpr.callee())+"#"+callExpr.arguments().size());
for (int i=0; i<callExpr.arguments().size(); i++) {
lowerExpected(callExpr.arguments().get(i), signature != null && i < signature.parameters().size() ? signature.parameters().get(i).typeRef() : null, context);
}
return;
}
for (int i = 0; i < callExpr.arguments().size(); i++) {
@@ -476,6 +482,10 @@ final class PbsExecutableBodyLowerer {
private void lowerMemberExpression(
final PbsAst.MemberExpr memberExpr,
final PbsExecutableLoweringContext context) {
if (memberExpr.receiver() instanceof PbsAst.IdentifierExpr owner) {
final var enumValue = context.types().enumValue(owner.name(), memberExpr.memberName());
if (enumValue != null) { emitPushI32(enumValue, memberExpr.span(), context); return; }
}
final var assetReference = flattenAssetReference(memberExpr);
if (assetReference != null) {
final var assetId = context.resolveAssetId(assetReference);
@@ -492,6 +502,15 @@ final class PbsExecutableBodyLowerer {
if (memberExpr.receiver() instanceof PbsAst.IdentifierExpr identifierExpr) {
final var tupleProjection = context.resolveLocalTupleProjection(identifierExpr.name());
final var localSlot = context.resolveLocalSlot(identifierExpr.name());
var type = context.localTypes.get(identifierExpr.name());
var fields = type == null ? null : context.types().fields.get(type.name());
if (fields != null && localSlot != null) {
int offset = 0;
for (var field : fields) {
if (field.name().equals(memberExpr.memberName())) { emitLoadLocalSlots(localSlot + offset, context.types().slots(field.typeRef()), memberExpr.span(), context); return; }
offset += context.types().slots(field.typeRef());
}
}
if (tupleProjection != null && localSlot != null) {
final var fieldProjection = tupleProjection.fieldsByLabel().get(memberExpr.memberName());
if (fieldProjection != null) {
@@ -562,7 +581,7 @@ final class PbsExecutableBodyLowerer {
if (tupleProjection != null) {
emitLoadLocalSlots(slot, tupleProjection.slotCount(), identifierExpr.span(), context);
} else {
emitGetLocal(slot, identifierExpr.span(), context);
emitLoadLocalSlots(slot, Math.max(1, context.types().slots(context.localTypes.get(identifierExpr.name()))), identifierExpr.span(), context);
}
return;
}
@@ -586,18 +605,10 @@ final class PbsExecutableBodyLowerer {
final p.studio.compiler.models.IRReservedMetadata.BuiltinConstSurface builtinConst,
final p.studio.compiler.source.Span span,
final PbsExecutableLoweringContext context) {
final var canonicalName = builtinConst.canonicalTarget() + "." + builtinConst.canonicalName();
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC,
"",
null,
new IRBackendExecutableFunction.IntrinsicCallMetadata(
canonicalName,
builtinConst.canonicalVersion(),
context.intrinsicIdTable().register(canonicalName, builtinConst.canonicalVersion())),
0,
1,
span));
context.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.BUILTIN_CONST,
ModuleId.none(), "", null, null, null,
builtinConst.canonicalTarget() + "." + builtinConst.canonicalName() + "@" + builtinConst.canonicalVersion(), "", null,
context.types().slots(context.types().values.get(builtinConst.sourceConstName())), span));
}
private String flattenAssetReference(final PbsAst.Expression expression) {
@@ -622,7 +633,7 @@ final class PbsExecutableBodyLowerer {
}
return switch (expression) {
case PbsAst.CallExpr callExpr -> callsiteEmitter.returnSlotsOf(callExpr, context);
case PbsAst.ApplyExpr ignored -> 0;
case PbsAst.ApplyExpr apply -> callsiteEmitter.returnSlotsOf(applyCall(apply), context);
case PbsAst.BinaryExpr ignored -> 1;
case PbsAst.UnaryExpr ignored -> 1;
case PbsAst.ElseExpr elseExpr ->
@@ -633,7 +644,7 @@ final class PbsExecutableBodyLowerer {
Math.max(
blockValueSlots(ifExpr.thenBlock(), context),
materializedValueSlots(ifExpr.elseExpression(), context));
case PbsAst.SwitchExpr ignored -> 0;
case PbsAst.SwitchExpr sw -> sw.arms().isEmpty() ? 0 : blockValueSlots(sw.arms().getFirst().block(), context);
case PbsAst.HandleExpr ignored -> 0;
case PbsAst.AsExpr asExpr -> materializedValueSlots(asExpr.expression(), context);
case PbsAst.MemberExpr memberExpr -> {
@@ -642,6 +653,9 @@ final class PbsExecutableBodyLowerer {
yield 1;
}
if (memberExpr.receiver() instanceof PbsAst.IdentifierExpr identifierExpr) {
var type = context.localTypes.get(identifierExpr.name());
var fields = type == null ? null : context.types().fields.get(type.name());
if (fields != null) for (var field : fields) if (field.name().equals(memberExpr.memberName())) yield context.types().slots(field.typeRef());
final var tupleProjection = context.resolveLocalTupleProjection(identifierExpr.name());
if (tupleProjection != null) {
final var fieldProjection = tupleProjection.fieldsByLabel().get(memberExpr.memberName());
@@ -650,6 +664,7 @@ final class PbsExecutableBodyLowerer {
}
}
}
if (memberExpr.receiver() instanceof PbsAst.IdentifierExpr owner && context.types().enumValue(owner.name(), memberExpr.memberName()) != null) yield 1;
yield materializedValueSlots(memberExpr.receiver(), context);
}
case PbsAst.PropagateExpr propagateExpr -> materializedValueSlots(propagateExpr.expression(), context);
@@ -670,8 +685,8 @@ final class PbsExecutableBodyLowerer {
case PbsAst.IntLiteralExpr ignored -> 1;
case PbsAst.StringLiteralExpr ignoredString -> 1;
case PbsAst.BoolLiteralExpr ignoredBool -> 1;
case PbsAst.FloatLiteralExpr ignoredFloat -> 0;
case PbsAst.BoundedLiteralExpr ignoredBounded -> 0;
case PbsAst.FloatLiteralExpr ignoredFloat -> 1;
case PbsAst.BoundedLiteralExpr ignoredBounded -> 1;
case PbsAst.ThisExpr ignoredThis -> 0;
case PbsAst.NoneExpr ignoredNone -> 0;
case PbsAst.ErrExpr ignoredErr -> 0;
@@ -712,11 +727,11 @@ final class PbsExecutableBodyLowerer {
private int identifierValueSlots(
final PbsAst.IdentifierExpr identifierExpr,
final PbsExecutableLoweringContext context) {
if (!producesIdentifierValue(identifierExpr, context)) {
return 0;
}
var constant = context.builtinConstByNameId().get(context.nameTable().register(identifierExpr.name()));
if (constant != null) return context.types().slots(context.types().values.get(identifierExpr.name()));
if (!producesIdentifierValue(identifierExpr, context)) return 0;
final var tupleProjection = context.resolveLocalTupleProjection(identifierExpr.name());
return tupleProjection == null ? 1 : tupleProjection.slotCount();
return tupleProjection == null ? Math.max(1, context.types().slots(expressionType(identifierExpr, context))) : tupleProjection.slotCount();
}
private PbsTupleProjection tupleProjectionOf(
@@ -774,6 +789,10 @@ final class PbsExecutableBodyLowerer {
private void lowerIntLiteral(
final PbsAst.IntLiteralExpr intLiteralExpr,
final PbsExecutableLoweringContext context) {
if (context.expectedType != null && "long".equals(context.types().canonical(context.expectedType))) {
emitLiteral(IRBackendExecutableFunction.InstructionKind.PUSH_I64, Long.toString(intLiteralExpr.value()), intLiteralExpr.span(), context);
return;
}
if (intLiteralExpr.value() < Integer.MIN_VALUE || intLiteralExpr.value() > Integer.MAX_VALUE) {
reportUnsupportedLowering(
"int literal exceeds i32 lowering range: " + intLiteralExpr.value(),
@@ -784,6 +803,132 @@ final class PbsExecutableBodyLowerer {
emitPushI32((int) intLiteralExpr.value(), intLiteralExpr.span(), context);
}
private PbsAst.CallExpr applyCall(PbsAst.ApplyExpr apply) {
var argument = apply.argument();
while (argument instanceof PbsAst.GroupExpr group) argument = group.expression();
var values = argument instanceof PbsAst.TupleExpr tuple ? tuple.items().stream().map(PbsAst.TupleItem::expression).toList()
: argument instanceof PbsAst.UnitExpr ? java.util.List.<PbsAst.Expression>of() : java.util.List.of(argument);
return new PbsAst.CallExpr(apply.callee(), ReadOnlyList.wrap(values), apply.span());
}
private void lowerApply(PbsAst.ApplyExpr apply, PbsExecutableLoweringContext c) {
var call = applyCall(apply);
var argument = apply.argument();
while (argument instanceof PbsAst.GroupExpr group) argument = group.expression();
if (!(argument instanceof PbsAst.TupleExpr tuple) || tuple.items().isEmpty() || tuple.items().getFirst().label() == null) {
lowerCallExpression(call, 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>();
for(var item:tuple.items()) {
var parameter = signature.parameters().stream().filter(p -> p.name().equals(item.label())).findFirst().orElse(null);
if(parameter == null || slots.containsKey(item.label())) { reportUnsupportedLowering("Unknown or duplicate named argument: "+item.label(), item.span(), c); return; }
// Evaluate source order once, then reorder complete value groups, never expressions.
lowerExpected(item.expression(), parameter.typeRef(), c);
int width = c.types().slots(parameter.typeRef());
int slot = c.declareLocalSlots(c.nextLabel("argument"), width);
emitStoreLocalSlots(slot, width, item.span(), c);
slots.put(item.label(), slot);
}
for(var parameter:signature.parameters()) {
var slot=slots.get(parameter.name());
if(slot==null) { reportUnsupportedLowering("Missing named argument: "+parameter.name(), apply.span(), c); return; }
emitLoadLocalSlots(slot,c.types().slots(parameter.typeRef()),apply.span(),c);
}
callsiteEmitter.emitCallsite(call,c);
}
private void lowerExpected(PbsAst.Expression expression, PbsAst.TypeRef expected, PbsExecutableLoweringContext context) {
var previous = context.expectedType;
context.expectedType = expected;
try { lowerExpression(expression, context); } finally { context.expectedType = previous; }
}
private PbsAst.TypeRef expressionType(PbsAst.Expression e, PbsExecutableLoweringContext c) {
if (e instanceof PbsAst.IdentifierExpr i) return c.localTypes.getOrDefault(i.name(), c.types().values.get(i.name()));
if (e instanceof PbsAst.GroupExpr g) return expressionType(g.expression(), c);
if (e instanceof PbsAst.AsExpr a) return PbsRuntimeTypes.type(a.contractName());
if (e instanceof PbsAst.UnaryExpr u) return java.util.Set.of("!", "not").contains(u.operator()) ? PbsRuntimeTypes.type("bool") : expressionType(u.expression(), c);
if (e instanceof PbsAst.BinaryExpr b) return java.util.Set.of("==", "!=", "<", "<=", ">", ">=", "&&", "||", "and", "or").contains(b.operator()) ? PbsRuntimeTypes.type("bool") : expressionType(b.left(), c);
if (e instanceof PbsAst.IfExpr branch) return expressionType(branch.thenBlock().tailExpression(), c);
if (e instanceof PbsAst.SwitchExpr branch && !branch.arms().isEmpty()) return expressionType(branch.arms().getFirst().block().tailExpression(), c);
if (e instanceof PbsAst.ApplyExpr apply) return expressionType(applyCall(apply), c);
if (e instanceof PbsAst.NewExpr value) return PbsRuntimeTypes.type(value.typeName());
if (e instanceof PbsAst.CallExpr call) {
var sig = c.types().signatures.get(PbsRuntimeTypes.path(call.callee())+"#"+call.arguments().size());
return sig == null ? null : sig.returnType();
}
if (e instanceof PbsAst.MemberExpr m && m.receiver() instanceof PbsAst.IdentifierExpr i) {
if (c.types().enums.containsKey(i.name())) return PbsRuntimeTypes.type(i.name());
var owner = c.localTypes.get(i.name());
var fields = owner == null ? null : c.types().fields.get(owner.name());
if (fields != null) for (var field : fields) if (field.name().equals(m.memberName())) return field.typeRef();
if (owner != null && owner.kind() == PbsAst.TypeRefKind.NAMED_TUPLE)
for (var field : owner.fields()) if (field.label().equals(m.memberName())) return field.typeRef();
}
if (e instanceof PbsAst.IntLiteralExpr || e instanceof PbsAst.BoundedLiteralExpr) return PbsRuntimeTypes.type("int");
if (e instanceof PbsAst.FloatLiteralExpr) return PbsRuntimeTypes.type("float");
if (e instanceof PbsAst.BoolLiteralExpr) return PbsRuntimeTypes.type("bool");
if (e instanceof PbsAst.StringLiteralExpr) return PbsRuntimeTypes.type("str");
return null;
}
private void lowerConversion(PbsAst.AsExpr expr, PbsExecutableLoweringContext c) {
var source = c.types().canonical(expressionType(expr.expression(), c));
var target = expr.contractName();
lowerExpected(expr.expression(), null, c);
if (source.equals(target)) return;
String name = switch(source + "->" + target) {
case "int->long" -> "math.i32.to_i64"; case "long->int" -> "math.i64.to_i32";
case "int->float" -> "math.i32.to_float"; case "long->float" -> "math.i64.to_float";
case "float->int" -> "mathf.to_i32"; case "float->long" -> "mathf.to_i64";
default -> null;
};
if (name == null) { reportUnsupportedLowering("Unsupported conversion " + source + " -> " + target, expr.span(), c); return; }
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC,
"", null, new IRBackendExecutableFunction.IntrinsicCallMetadata(name, 1, c.intrinsicIdTable().register(name, 1)), 1, 1, expr.span()));
}
private void lowerSwitch(PbsAst.SwitchExpr expr, PbsExecutableLoweringContext c) {
var slot = c.declareLocalSlot(c.nextLabel("switch_value"));
lowerExpected(expr.selector(), null, c);
emitSetLocal(slot, expr.span(), c);
var end = c.nextLabel("switch_end");
var selectorType = expressionType(expr.selector(), c);
var enumDecl = selectorType == null ? null : c.types().enums.get(selectorType.name());
var coveredCases = new java.util.HashSet<String>();
var coveredBooleans = new java.util.HashSet<Boolean>();
for (var arm : expr.arms()) {
if (arm.pattern() instanceof PbsAst.EnumCaseSwitchPattern pattern) coveredCases.add(pattern.path().segments().get(1));
if (arm.pattern() instanceof PbsAst.LiteralSwitchPattern pattern && pattern.literal() instanceof PbsAst.BoolLiteralExpr literal) coveredBooleans.add(literal.value());
}
boolean exhaustive = enumDecl != null && enumDecl.cases().stream().allMatch(item -> coveredCases.contains(item.name())) || coveredBooleans.size() == 2;
for (int i=0; i<expr.arms().size(); i++) {
var arm = expr.arms().get(i);
var next = c.nextLabel("switch_next");
boolean last = i == expr.arms().size()-1;
boolean checked = !(arm.pattern() instanceof PbsAst.WildcardSwitchPattern) && !(last && exhaustive);
if (checked) {
emitGetLocal(slot, expr.span(), c);
if (arm.pattern() instanceof PbsAst.EnumCaseSwitchPattern pattern) {
var value = c.types().enumValue(pattern.path().segments().get(0), pattern.path().segments().get(1));
emitPushI32(value, pattern.span(), c);
} else if (arm.pattern() instanceof PbsAst.LiteralSwitchPattern literal) lowerExpected(literal.literal(), expressionType(expr.selector(), c), c);
emitBinaryOperatorInstruction("==", expr.span(), c);
emitJump(IRBackendExecutableFunction.InstructionKind.JMP_IF_FALSE, next, expr.span(), c);
}
lowerBlock(arm.block(), c);
if (!last) emitJump(IRBackendExecutableFunction.InstructionKind.JMP, end, arm.span(), c);
if (checked) emitLabel(next, arm.span(), c);
}
emitLabel(end, expr.span(), c);
}
private void emitLiteral(IRBackendExecutableFunction.InstructionKind kind, String value, p.studio.compiler.source.Span span, PbsExecutableLoweringContext c) {
c.instructions().add(new IRBackendExecutableFunction.Instruction(kind, ModuleId.none(), "", null, null, null, value, "", null, null, span));
}
private void lowerCallsiteReceiver(
final PbsAst.Expression callee,
final PbsExecutableLoweringContext context) {
@@ -201,7 +201,7 @@ final class PbsExecutableCallsiteEmitter {
final PbsAst.CallExpr callExpr,
final PbsExecutableLoweringContext context,
final IRReservedMetadata.HostMethodBinding host) {
final var effectiveArgSlots = callExpr.arguments().size() + implicitReceiverArgSlots(callExpr.callee(), context);
final var effectiveArgSlots = argumentSlots(callExpr, context) + implicitReceiverArgSlots(callExpr.callee(), context);
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_HOST,
"",
@@ -251,7 +251,7 @@ final class PbsExecutableCallsiteEmitter {
reportUnsupportedLowering("executable lowering resolved callable without signature metadata", callExpr.span(), context);
return;
}
final var effectiveArgSlots = callExpr.arguments().size() + implicitReceiverArgSlots(callExpr.callee(), context);
final var effectiveArgSlots = argumentSlots(callExpr, context) + implicitReceiverArgSlots(callExpr.callee(), context);
context.instructions().add(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
calleeSignature.moduleId(),
@@ -411,13 +411,23 @@ final class PbsExecutableCallsiteEmitter {
};
}
private int argumentSlots(PbsAst.CallExpr call, PbsExecutableLoweringContext context) {
var signature = context.types().signatures.get(PbsRuntimeTypes.path(call.callee())+"#"+call.arguments().size());
return signature == null ? call.arguments().size() : signature.parameters().stream().mapToInt(p -> context.types().slots(p.typeRef())).sum();
}
private int implicitReceiverArgSlots(
final PbsAst.Expression callee,
final PbsExecutableLoweringContext context) {
if (!(callee instanceof PbsAst.MemberExpr memberExpr)) {
return 0;
}
return receiverProducesRuntimeValue(memberExpr.receiver(), context) ? 1 : 0;
if (!receiverProducesRuntimeValue(memberExpr.receiver(), context)) return 0;
return switch (resolveExpressionOwner(memberExpr.receiver(), context)) {
case "vec2" -> 2;
case "pixel" -> 3;
default -> 1;
};
}
private boolean receiverProducesRuntimeValue(
@@ -22,6 +22,10 @@ final class PbsExecutableLoweringContext {
private final Map<NameId, String> localOwnerByNameId;
private final ArrayList<IRBackendExecutableFunction.Instruction> instructions = new ArrayList<>();
private final ArrayDeque<PbsLoopTargets> loopTargets = new ArrayDeque<>();
final Map<String, p.studio.compiler.pbs.ast.PbsAst.TypeRef> localTypes = new HashMap<>();
p.studio.compiler.pbs.ast.PbsAst.TypeRef expectedType;
p.studio.compiler.pbs.ast.PbsAst.TypeRef returnType;
PbsRuntimeTypes types() { return metadataIndex.types(); }
private int nextLabelId = 0;
private int nextLocalSlot;
@@ -18,7 +18,8 @@ public class PbsExecutableLoweringModels {
public record PbsExecutableLoweringResult(
ReadOnlyList<IRBackendExecutableFunction> executableFunctions,
ReadOnlyList<CallableSignatureRef> callableSignatures,
ReadOnlyList<IntrinsicReference> intrinsicPool) {
ReadOnlyList<IntrinsicReference> intrinsicPool,
p.studio.compiler.models.registry.IRRegistry registry) {
}
record PbsLowerableCallable(
@@ -42,6 +43,7 @@ public class PbsExecutableLoweringModels {
}
record PbsExecutableMetadataIndex(
PbsRuntimeTypes types,
Map<NameId, List<IRReservedMetadata.HostMethodBinding>> hostByMethodName,
Map<String, String> builtinCanonicalBySourceType,
Map<NameId, String> builtinConstOwnerByNameId,
@@ -47,6 +47,10 @@ public final class PbsExecutableLoweringService {
nameTable,
metadataIndex.builtinCanonicalBySourceType(),
importedCallables);
for (var entry : callableRegistry.callableSignatureByCallableId().entrySet()) {
var signature = metadataIndex.types().signatures.get(entry.getValue().callableName()+"#"+entry.getValue().arity());
if (signature != null) callableRegistry.returnSlotsByCallableId().put(entry.getKey(), signature.returnKind() == PbsAst.ReturnKind.PLAIN ? metadataIndex.types().slots(signature.returnType()) : 0);
}
final var intrinsicIdTable = new IntrinsicTable();
final var executableFunctions = new ArrayList<IRBackendExecutableFunction>(callableRegistry.localCallables().size());
@@ -72,7 +76,7 @@ public final class PbsExecutableLoweringService {
return new PbsExecutableLoweringResult(
ReadOnlyList.wrap(executableFunctions),
callableRegistry.callableSignatures(),
ReadOnlyList.wrap(intrinsicPool));
ReadOnlyList.wrap(intrinsicPool), metadataIndex.types().registry());
}
public String callableShapeSurfaceOf(final PbsAst.FunctionDecl functionDecl) {
@@ -110,12 +114,13 @@ public final class PbsExecutableLoweringService {
functionCallableId,
safeToInt(functionDecl.span().getStart()),
safeToInt(functionDecl.span().getEnd()),
functionDecl.parameters().size(),
functionDecl.parameters().stream().mapToInt(p -> metadataIndex.types().slots(p.typeRef())).sum(),
loweredBody.localSlots(),
callableRegistry.returnSlotsByCallableId().getOrDefault(functionCallableId, 0),
functionDecl.returnKind() == PbsAst.ReturnKind.PLAIN ? metadataIndex.types().slots(functionDecl.returnType()) : 0,
loweredBody.maxStackSlots(),
loweredBody.instructions(),
functionDecl.span());
functionDecl.span(),
metadataIndex.types().metadata(functionDecl));
}
private int safeToInt(final long value) {
@@ -48,6 +48,7 @@ final class PbsExecutableMetadataIndexFactory {
intrinsicReturnOwnerByCanonical,
diagnostics);
return new PbsExecutableMetadataIndex(
new PbsRuntimeTypes(ast, supplementalTopDecls, builtinCanonicalBySourceType),
hostByMethodName,
builtinCanonicalBySourceType,
builtinConstOwnerByNameId,
@@ -38,7 +38,8 @@ final class PbsExecutableStackAnalyzer {
var outHeight = incomingHeightByInstruction.get(index);
switch (instruction.kind()) {
case PUSH_I32, PUSH_BOOL, PUSH_CONST, GET_LOCAL, GET_GLOBAL -> outHeight += 1;
case PUSH_I32, PUSH_I64, PUSH_F64, PUSH_BOOL, PUSH_CONST, GET_LOCAL, GET_GLOBAL -> outHeight += 1;
case BUILTIN_CONST -> outHeight += instruction.expectedRetSlots();
case POP, SET_LOCAL, SET_GLOBAL -> {
if (outHeight <= 0) {
throw new ExecutableLoweringAnalysisException(
@@ -0,0 +1,92 @@
package p.studio.compiler.pbs.lowering;
import p.studio.compiler.models.registry.IRRegistry;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.source.Span;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.*;
/** PBS-owned source identities and layouts; never inferred from slot widths. */
final class PbsRuntimeTypes {
final Map<String, String> builtinNames;
final Map<String, ReadOnlyList<PbsAst.BuiltinFieldDecl>> fields = new HashMap<>();
final Map<String, PbsAst.EnumDecl> enums = new HashMap<>();
final Map<String, PbsAst.FunctionSignature> signatures = new HashMap<>();
final Map<String, PbsAst.TypeRef> values = new HashMap<>();
private final Map<Integer, IRRegistry.TypeDeclaration> used = new TreeMap<>();
PbsRuntimeTypes(PbsAst.File ast, ReadOnlyList<PbsAst.TopDecl> supplemental, Map<String,String> builtins) {
builtinNames = builtins;
var declarations = new ArrayList<>(ast.topDecls().asList());
declarations.addAll(supplemental.asList());
for (var decl : declarations) {
if (decl instanceof PbsAst.EnumDecl e) enums.put(e.name(), e);
if (decl instanceof PbsAst.ConstDecl c) values.put(c.name(), c.explicitType());
if (decl instanceof PbsAst.GlobalDecl g) values.put(g.name(), g.explicitType());
if (decl instanceof PbsAst.FunctionDecl f) addSignature(f.name(), f);
if (decl instanceof PbsAst.ServiceDecl s) for (var f : s.methods()) addSignature(s.name()+"."+f.name(), f);
if (decl instanceof PbsAst.BuiltinTypeDecl b) {
fields.put(b.name(), b.fields());
for(var f:b.signatures()) signatures.put(b.name()+"."+f.name()+"#"+f.parameters().size(), f);
}
if (decl instanceof PbsAst.HostDecl h) for(var f:h.signatures()) signatures.put(h.name()+"."+f.name()+"#"+f.parameters().size(), f);
}
}
private void addSignature(String name, PbsAst.FunctionDecl f) {
signatures.put(name+"#"+f.parameters().size(), new PbsAst.FunctionSignature(f.name(), f.parameters(), f.returnKind(), f.returnType(), f.resultErrorType(), f.attributes(), f.span()));
}
static PbsAst.TypeRef type(String name) { return PbsAst.TypeRef.simple(name, Span.none()); }
String canonical(PbsAst.TypeRef t) {
if(t==null) return "void";
if(t.kind()==PbsAst.TypeRefKind.GROUP) return canonical(t.inner());
return builtinNames.getOrDefault(t.name(), t.name());
}
int slots(PbsAst.TypeRef t) {
if(t==null || t.kind()==PbsAst.TypeRefKind.UNIT) return 0;
if(t.kind()==PbsAst.TypeRefKind.GROUP) return slots(t.inner());
if(t.kind()==PbsAst.TypeRefKind.NAMED_TUPLE) return t.fields().stream().mapToInt(f->slots(f.typeRef())).sum();
return switch(canonical(t)) { case "vec2" -> 2; case "pixel" -> 3; default -> 1; };
}
IRRegistry.TypeRef registryType(PbsAst.TypeRef t) {
int id = switch(canonical(t)) {
case "int" -> 0x10; case "long" -> 0x11; case "float" -> 0x12;
case "bool" -> 0x13; case "str" -> 0x14;
case "color" -> 1; case "vec2" -> 2; case "pixel" -> 3;
default -> { if(enums.containsKey(t.name())) yield 0x10;
throw new UnsupportedOperationException("Unsupported PBS runtime type: "+t.name()); }
};
var ref=IRRegistry.TypeRef.builtin(id);
used.putIfAbsent(id,new IRRegistry.TypeDeclaration(ref,null,new IRRegistry.Builtin(id)));
return ref;
}
List<IRRegistry.TypeRef> results(PbsAst.TypeRef t) {
if(t==null || t.kind()==PbsAst.TypeRefKind.UNIT) return List.of();
if(t.kind()==PbsAst.TypeRefKind.GROUP) return results(t.inner());
if(t.kind()==PbsAst.TypeRefKind.NAMED_TUPLE) return t.fields().stream().flatMap(f->results(f.typeRef()).stream()).toList();
return List.of(registryType(t));
}
IRRegistry.FunctionMetadata metadata(PbsAst.FunctionDecl f) {
// Preserve the pre-existing unsupported executable boundary for types outside this port.
// The common backend still rejects missing typed metadata; never infer a carrier by width.
try { return supportedMetadata(f); } catch (UnsupportedOperationException unsupported) { return null; }
}
private IRRegistry.FunctionMetadata supportedMetadata(PbsAst.FunctionDecl f) {
var params=new ArrayList<IRRegistry.NamedType>();
for(var p:f.parameters()) for(var type:results(p.typeRef())) params.add(new IRRegistry.NamedType(type,p.name()));
var returns=f.returnKind()==PbsAst.ReturnKind.PLAIN ? results(f.returnType()) : List.<IRRegistry.TypeRef>of();
return IRRegistry.FunctionMetadata.ordinary(new IRRegistry.Signature(ReadOnlyList.wrap(params),ReadOnlyList.wrap(returns)));
}
IRRegistry registry() { return new IRRegistry(ReadOnlyList.wrap(new ArrayList<>(used.values())),ReadOnlyList.empty()); }
Integer enumValue(String owner,String label) {
var e=enums.get(owner); if(e==null) return null;
int index=0;
for(var c:e.cases()) { if(c.name().equals(label)) return c.explicitValue()==null?index:Math.toIntExact(c.explicitValue()); index++; }
return null;
}
static String path(PbsAst.Expression e) {
if(e instanceof PbsAst.IdentifierExpr i) return i.name();
if(e instanceof PbsAst.MemberExpr m) {var owner=path(m.receiver());return owner==null?null:owner+"."+m.memberName();}
if(e instanceof PbsAst.GroupExpr g) return path(g.expression());
return null;
}
}
@@ -45,6 +45,7 @@ final class PbsExprLiteralSupport {
try {
return Long.parseLong(text);
} catch (NumberFormatException ignored) {
context.report(cursor.previous(), ParseErrors.E_PARSE_EXPECTED_TOKEN, "Integer literal is outside the Int64 range");
return 0L;
}
}
@@ -159,6 +159,11 @@ final class PbsExprParser {
private PbsAst.Expression parseUnary() {
if (cursor.match(PbsTokenKind.BANG, PbsTokenKind.MINUS, PbsTokenKind.NOT)) {
final var operator = cursor.previous();
if (operator.kind() == PbsTokenKind.MINUS && cursor.check(PbsTokenKind.INT_LITERAL)
&& (cursor.peek().lexeme().equals("9223372036854775808") || cursor.peek().lexeme().equals("2147483648"))) {
final var literal = cursor.advance();
return new PbsAst.IntLiteralExpr(Long.parseLong("-" + literal.lexeme()), span(operator.start(), literal.end()));
}
final var right = parseUnary();
return new PbsAst.UnaryExpr(operator.lexeme(), right, span(operator.start(), right.span().getEnd()));
}
@@ -8,7 +8,7 @@ import p.studio.utilities.structures.ReadOnlyList;
import java.util.*;
public final class PbsBuiltinLayoutResolver {
private static final Set<String> BUILTIN_SCALAR_TYPES = Set.of("int", "float", "bool", "str");
private static final Set<String> BUILTIN_SCALAR_TYPES = Set.of("int", "long", "float", "bool", "str");
private enum ResolveState {
UNVISITED,
@@ -357,6 +357,18 @@ public final class PbsDeclarationSemanticsValidator {
validateSdkExecutableExpression(argument, diagnostics);
}
}
case PbsAst.ApplyExpr apply -> {
validateSdkExecutableExpression(apply.callee(), diagnostics);
validateSdkExecutableExpression(apply.argument(), diagnostics);
}
case PbsAst.AsExpr conversion -> validateSdkExecutableExpression(conversion.expression(), diagnostics);
case PbsAst.SwitchExpr switchExpr -> {
validateSdkExecutableExpression(switchExpr.selector(), diagnostics);
for (var arm : switchExpr.arms()) {
for (var statement : arm.block().statements()) validateSdkExecutableStatement(statement, diagnostics);
validateSdkExecutableExpression(arm.block().tailExpression(), diagnostics);
}
}
case PbsAst.MemberExpr memberExpr ->
validateSdkExecutableExpression(memberExpr.receiver(), diagnostics);
case PbsAst.GroupExpr groupExpr ->
@@ -16,7 +16,7 @@ import java.util.*;
import static p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.*;
public final class PbsEditorialSupportService {
private static final Set<String> BUILTIN_TYPE_NAMES = Set.of("int", "float", "bool", "str", "string", "i32", "i64", "f32", "f64");
private static final Set<String> BUILTIN_TYPE_NAMES = Set.of("int", "long", "float", "bool", "str", "string", "i32", "i64", "f32", "f64");
private static final List<String> KEYWORDS = List.of(
"fn", "declare", "let", "const", "global", "struct", "service", "host", "contract", "enum", "error", "callback",
"if", "else", "switch", "for", "until", "step", "while", "break", "continue", "return",
@@ -29,15 +29,19 @@ final class PbsFlowCallableResolutionAnalyzer {
final PbsFlowExpressionContext context) {
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();
final TypeView argumentType;
if (callExpr.arguments().isEmpty()) {
argumentType = TypeView.unit();
} else if (callExpr.arguments().size() == 1) {
argumentType = analyzeValueExpression(callExpr.arguments().getFirst(), context, null).type();
argumentType = analyzeValueExpression(callExpr.arguments().getFirst(), context, expectedInputs.size() == 1 ? expectedInputs.getFirst() : null).type();
} else {
final var fields = new ArrayList<TupleField>(callExpr.arguments().size());
int argumentIndex = 0;
for (final var argument : callExpr.arguments()) {
final var itemType = analyzeValueExpression(argument, context, null).type();
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));
}
argumentType = TypeView.tuple(fields);
@@ -56,7 +60,27 @@ final class PbsFlowCallableResolutionAnalyzer {
final PbsAst.ApplyExpr applyExpr,
final PbsFlowExpressionContext context) {
final var callee = analyzeTargetExpression(applyExpr.callee(), context, ExprUse.APPLY_TARGET);
final var argument = analyzeValueExpression(applyExpr.argument(), context, null).type();
TypeView argument;
if (applyExpr.argument() instanceof PbsAst.TupleExpr tuple && !tuple.items().isEmpty()
&& tuple.items().getFirst().label() != null && callee.callables().size() == 1) {
var candidate = callee.callables().getFirst();
var byName = new java.util.HashMap<String, TypeView>();
for (var item : tuple.items()) {
var parameter = candidate.parameters().stream().filter(p -> p.name().equals(item.label())).findFirst().orElse(null);
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 fields = new ArrayList<TupleField>();
for (var parameter : candidate.parameters()) {
if (!byName.containsKey(parameter.name())) Diagnostics.error(context.diagnostics(), "E_SEM_NAMED_ARGUMENT", "Missing named argument: " + parameter.name(), applyExpr.span());
fields.add(new TupleField(parameter.name(), byName.getOrDefault(parameter.name(), TypeView.unknown())));
}
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();
}
return resolveCallableApplication(
applyExpr.span(),
@@ -35,6 +35,7 @@ final class PbsFlowSemanticSupport {
UNKNOWN,
UNIT,
INT,
LONG,
FLOAT,
BOOL,
STR,
@@ -77,6 +78,10 @@ final class PbsFlowSemanticSupport {
return new TypeView(Kind.INT, "int", List.of(), null, null, List.of(), null);
}
static TypeView longType() {
return new TypeView(Kind.LONG, "long", List.of(), null, null, List.of(), null);
}
static TypeView floatType() {
return new TypeView(Kind.FLOAT, "float", List.of(), null, null, List.of(), null);
}
@@ -543,6 +548,9 @@ final class PbsFlowSemanticSupport {
case UNIT -> TypeView.unit();
case SELF, ERROR -> TypeView.unknown();
case SIMPLE -> {
if ("long".equals(typeRef.name())) {
yield TypeView.longType();
}
if ("int".equals(typeRef.name())) {
yield TypeView.intType();
}
@@ -41,7 +41,11 @@ final class PbsFlowStructuralExpressionAnalyzer {
final var model = context.model();
final var diagnostics = context.diagnostics();
if (expression instanceof PbsAst.IntLiteralExpr) {
if (expression instanceof PbsAst.IntLiteralExpr literal) {
if (expectedType != null && expectedType.kind() == Kind.LONG) return ExprResult.type(TypeView.longType());
if (literal.value() < Integer.MIN_VALUE || literal.value() > Integer.MAX_VALUE) {
Diagnostics.error(diagnostics, "E_SEM_INTEGER_RANGE", "Integer literal exceeds int range; use an explicit long context", literal.span());
}
return ExprResult.type(TypeView.intType());
}
if (expression instanceof PbsAst.FloatLiteralExpr) {
@@ -119,8 +123,11 @@ final class PbsFlowStructuralExpressionAnalyzer {
}
if (expression instanceof PbsAst.TupleExpr tupleExpr) {
final var fields = new ArrayList<TupleField>(tupleExpr.items().size());
int itemIndex=0;
for (final var item : tupleExpr.items()) {
final var value = analyzeValueExpression(item.expression(), context, null).type();
var fieldExpected=expectedType != null && expectedType.kind()==Kind.TUPLE && itemIndex<expectedType.tupleFields().size() ? expectedType.tupleFields().get(itemIndex).type() : null;
itemIndex++;
final var value = analyzeValueExpression(item.expression(), context, fieldExpected).type();
fields.add(new TupleField(item.label(), value));
}
return ExprResult.type(TypeView.tuple(fields));
@@ -129,18 +136,22 @@ final class PbsFlowStructuralExpressionAnalyzer {
return ExprResult.type(blockAnalysisDelegate.analyze(blockExpr.block(), context, context.valueContext()));
}
if (expression instanceof PbsAst.UnaryExpr unaryExpr) {
final var operand = analyzeValueExpression(unaryExpr.expression(), context, null).type();
final var operand = analyzeValueExpression(unaryExpr.expression(), context, expectedType).type();
if ("!".equals(unaryExpr.operator()) || "not".equals(unaryExpr.operator())) {
return ExprResult.type(TypeView.bool());
}
if ("-".equals(unaryExpr.operator()) && (typeOps.isInt(operand) || typeOps.isFloat(operand))) {
if ("-".equals(unaryExpr.operator()) && (typeOps.isInt(operand) || operand.kind() == Kind.LONG || typeOps.isFloat(operand))) {
return ExprResult.type(operand);
}
return ExprResult.type(TypeView.unknown());
}
if (expression instanceof PbsAst.BinaryExpr binaryExpr) {
final var left = analyzeValueExpression(binaryExpr.left(), context, null).type();
final var right = analyzeValueExpression(binaryExpr.right(), context, null).type();
final var left = analyzeValueExpression(binaryExpr.left(), context, expectedType != null && expectedType.kind()==Kind.LONG ? expectedType : null).type();
final var right = analyzeValueExpression(binaryExpr.right(), context, expectedType != null && expectedType.kind()==Kind.LONG ? expectedType : null).type();
if ((left.kind() == Kind.LONG || right.kind() == Kind.LONG) && left.kind() != right.kind()
&& left.kind() != Kind.UNKNOWN && right.kind() != Kind.UNKNOWN) {
Diagnostics.error(diagnostics, "E_SEM_NUMERIC_TYPE_MISMATCH", "Mixed numeric types require an explicit as conversion", binaryExpr.span());
}
return ExprResult.type(typeOps.inferBinaryResult(binaryExpr.operator(), left, right));
}
if (expression instanceof PbsAst.NoneExpr noneExpr) {
@@ -167,7 +178,13 @@ final class PbsFlowStructuralExpressionAnalyzer {
return ExprResult.type(TypeView.unknown());
}
if (expression instanceof PbsAst.AsExpr asExpr) {
analyzeValueExpression(asExpr.expression(), context, null);
final var source = analyzeValueExpression(asExpr.expression(), context, null).type();
if ("int".equals(asExpr.contractName()) || "long".equals(asExpr.contractName()) || "float".equals(asExpr.contractName())) {
if (source.kind() != Kind.INT && source.kind() != Kind.LONG && source.kind() != Kind.FLOAT && source.kind() != Kind.UNKNOWN) {
Diagnostics.error(diagnostics, "E_SEM_NUMERIC_TYPE_MISMATCH", "Numeric conversion requires a numeric operand", asExpr.span());
}
return ExprResult.type(typeOps.typeFromTypeRef(PbsAst.TypeRef.simple(asExpr.contractName(), asExpr.span()), model, null));
}
if (model.contracts.containsKey(asExpr.contractName())) {
return ExprResult.type(TypeView.contract(asExpr.contractName()));
}
@@ -15,6 +15,9 @@ final class PbsFlowTypeOps {
if (isFloat(left) || isFloat(right)) {
return TypeView.floatType();
}
if (left.kind() == Kind.LONG && right.kind() == Kind.LONG) {
return TypeView.longType();
}
if (isInt(left) && isInt(right)) {
return TypeView.intType();
}
@@ -46,7 +49,7 @@ final class PbsFlowTypeOps {
return false;
}
return switch (actual.kind()) {
case UNIT, INT, FLOAT, BOOL, STR -> true;
case UNIT, INT, LONG, FLOAT, BOOL, STR -> true;
case STRUCT, SERVICE, CONTRACT, CALLBACK, ENUM, ERROR, TYPE_REF, ASSET_NAMESPACE -> actual.name().equals(expected.name());
case ADDRESSABLE -> true;
case OPTIONAL -> compatible(actual.inner(), expected.inner());
@@ -116,7 +119,7 @@ final class PbsFlowTypeOps {
}
boolean isScalarComparable(final TypeView type) {
return isBool(type) || isInt(type) || isFloat(type) || isStr(type);
return type.kind() == Kind.LONG || isBool(type) || isInt(type) || isFloat(type) || isStr(type);
}
boolean isUnit(final TypeView type) {
@@ -172,6 +175,9 @@ final class PbsFlowTypeOps {
}
private TypeView simpleType(final String name, final Model model) {
if ("long".equals(name)) {
return TypeView.longType();
}
if ("int".equals(name)) {
return TypeView.intType();
}
@@ -11,7 +11,7 @@ final class PbsInlineHintSurfaceFormatter {
return switch (type.kind()) {
case UNKNOWN, ASSET_NAMESPACE -> null;
case UNIT -> "void";
case INT, FLOAT, BOOL, STR -> type.name();
case INT, LONG, FLOAT, BOOL, STR -> type.name();
case STRUCT,
SERVICE,
CONTRACT,
@@ -315,7 +315,7 @@ final class PbsImportedSemanticContextService {
}
private boolean isPrimitiveTypeName(final String typeName) {
return "int".equals(typeName)
return "long".equals(typeName) || "int".equals(typeName)
|| "float".equals(typeName)
|| "bool".equals(typeName)
|| "str".equals(typeName)
@@ -3,7 +3,7 @@ Represents a packed RGBA color.
""")]
[BuiltinType(name = "color", version = 1)]
declare builtin type Color(
pub raw: int
pub raw: long
) {
[Doc(markdown = """
Builds a color from its packed integer representation.
@@ -11,7 +11,7 @@ declare builtin type Color(
- `raw`: packed color value.
""")]
[IntrinsicCall(name = "from_raw", version = 1)]
fn from_raw(raw: int) -> Color;
fn from_raw(raw: long) -> Color;
[Doc(markdown = """
Builds an opaque color from red, green, and blue channels.
@@ -21,7 +21,7 @@ declare builtin type Color(
- `b`: blue channel.
""")]
[IntrinsicCall(name = "rgb", version = 1)]
fn rgb(r: int, g: int, b: int) -> Color;
fn rgb(r: long, g: long, b: long) -> Color;
[Doc(markdown = """
Builds a color from red, green, blue, and alpha channels.
@@ -32,7 +32,7 @@ declare builtin type Color(
- `a`: alpha channel.
""")]
[IntrinsicCall(name = "rgba", version = 1)]
fn rgba(r: int, g: int, b: int, a: int) -> Color;
fn rgba(r: long, g: long, b: long, a: long) -> Color;
[Doc(markdown = """
Parses a CSS-style RGBA color string.
@@ -48,19 +48,19 @@ declare builtin type Color(
- `value`: grayscale channel value.
""")]
[IntrinsicCall(name = "gray_scale", version = 1)]
fn gray_scale(value: int) -> Color;
fn gray_scale(value: long) -> Color;
[Doc(markdown = """
Returns the packed RGB portion of this color.
""")]
[IntrinsicCall(name = "hex", version = 1)]
fn hex() -> int;
fn hex() -> long;
[Doc(markdown = """
Returns the alpha channel of this color.
""")]
[IntrinsicCall(name = "alpha", version = 1)]
fn alpha() -> int;
fn alpha() -> long;
}
[Doc(markdown = """
@@ -1,171 +1,205 @@
[Doc(markdown = """
Root input device provider.
""")]
[BuiltinType(name = "input", version = 1)]
declare builtin type InputType() {
[Doc(markdown = """
Returns the gamepad input state.
""")]
[IntrinsicCall(name = "pad", version = 1)]
fn pad() -> InputPad;
[Doc(markdown = """
Returns the touch input state.
""")]
[IntrinsicCall(name = "touch", version = 1)]
fn touch() -> InputTouch;
}
declare enum Button(Up, Down, Left, Right, A, B, X, Y, L, R, Start, Select);
[Doc(markdown = """
Represents the current gamepad state.
Direct zero-argument gamepad queries. Prefer Pad with a typed Button in game code.
""")]
[BuiltinType(name = "input.pad", version = 1)]
declare builtin type InputPad() {
[Doc(markdown = """
Returns the up directional button.
""")]
[IntrinsicCall(name = "up", version = 1)]
fn up() -> InputButton;
[Doc(markdown = """
Returns the down directional button.
""")]
[IntrinsicCall(name = "down", version = 1)]
fn down() -> InputButton;
[Doc(markdown = """
Returns the left directional button.
""")]
[IntrinsicCall(name = "left", version = 1)]
fn left() -> InputButton;
[Doc(markdown = """
Returns the right directional button.
""")]
[IntrinsicCall(name = "right", version = 1)]
fn right() -> InputButton;
[Doc(markdown = """
Returns the A action button.
""")]
[IntrinsicCall(name = "a", version = 1)]
fn a() -> InputButton;
[Doc(markdown = """
Returns the B action button.
""")]
[IntrinsicCall(name = "b", version = 1)]
fn b() -> InputButton;
[Doc(markdown = """
Returns the X action button.
""")]
[IntrinsicCall(name = "x", version = 1)]
fn x() -> InputButton;
[Doc(markdown = """
Returns the Y action button.
""")]
[IntrinsicCall(name = "y", version = 1)]
fn y() -> InputButton;
[Doc(markdown = """
Returns the left shoulder button.
""")]
[IntrinsicCall(name = "l", version = 1)]
fn l() -> InputButton;
[Doc(markdown = """
Returns the right shoulder button.
""")]
[IntrinsicCall(name = "r", version = 1)]
fn r() -> InputButton;
[Doc(markdown = """
Returns the start button.
""")]
[IntrinsicCall(name = "start", version = 1)]
fn start() -> InputButton;
[Doc(markdown = """
Returns the select button.
""")]
[IntrinsicCall(name = "select", version = 1)]
fn select() -> InputButton;
declare builtin type LowPad() {
[IntrinsicCall(name = "input.pad.up.pressed", version = 1)]
fn up_pressed() -> bool;
[IntrinsicCall(name = "input.pad.up.released", version = 1)]
fn up_released() -> bool;
[IntrinsicCall(name = "input.pad.up.down", version = 1)]
fn up_down() -> bool;
[IntrinsicCall(name = "input.pad.up.hold", version = 1)]
fn up_hold() -> long;
[IntrinsicCall(name = "input.pad.down.pressed", version = 1)]
fn down_pressed() -> bool;
[IntrinsicCall(name = "input.pad.down.released", version = 1)]
fn down_released() -> bool;
[IntrinsicCall(name = "input.pad.down.down", version = 1)]
fn down_down() -> bool;
[IntrinsicCall(name = "input.pad.down.hold", version = 1)]
fn down_hold() -> long;
[IntrinsicCall(name = "input.pad.left.pressed", version = 1)]
fn left_pressed() -> bool;
[IntrinsicCall(name = "input.pad.left.released", version = 1)]
fn left_released() -> bool;
[IntrinsicCall(name = "input.pad.left.down", version = 1)]
fn left_down() -> bool;
[IntrinsicCall(name = "input.pad.left.hold", version = 1)]
fn left_hold() -> long;
[IntrinsicCall(name = "input.pad.right.pressed", version = 1)]
fn right_pressed() -> bool;
[IntrinsicCall(name = "input.pad.right.released", version = 1)]
fn right_released() -> bool;
[IntrinsicCall(name = "input.pad.right.down", version = 1)]
fn right_down() -> bool;
[IntrinsicCall(name = "input.pad.right.hold", version = 1)]
fn right_hold() -> long;
[IntrinsicCall(name = "input.pad.a.pressed", version = 1)]
fn a_pressed() -> bool;
[IntrinsicCall(name = "input.pad.a.released", version = 1)]
fn a_released() -> bool;
[IntrinsicCall(name = "input.pad.a.down", version = 1)]
fn a_down() -> bool;
[IntrinsicCall(name = "input.pad.a.hold", version = 1)]
fn a_hold() -> long;
[IntrinsicCall(name = "input.pad.b.pressed", version = 1)]
fn b_pressed() -> bool;
[IntrinsicCall(name = "input.pad.b.released", version = 1)]
fn b_released() -> bool;
[IntrinsicCall(name = "input.pad.b.down", version = 1)]
fn b_down() -> bool;
[IntrinsicCall(name = "input.pad.b.hold", version = 1)]
fn b_hold() -> long;
[IntrinsicCall(name = "input.pad.x.pressed", version = 1)]
fn x_pressed() -> bool;
[IntrinsicCall(name = "input.pad.x.released", version = 1)]
fn x_released() -> bool;
[IntrinsicCall(name = "input.pad.x.down", version = 1)]
fn x_down() -> bool;
[IntrinsicCall(name = "input.pad.x.hold", version = 1)]
fn x_hold() -> long;
[IntrinsicCall(name = "input.pad.y.pressed", version = 1)]
fn y_pressed() -> bool;
[IntrinsicCall(name = "input.pad.y.released", version = 1)]
fn y_released() -> bool;
[IntrinsicCall(name = "input.pad.y.down", version = 1)]
fn y_down() -> bool;
[IntrinsicCall(name = "input.pad.y.hold", version = 1)]
fn y_hold() -> long;
[IntrinsicCall(name = "input.pad.l.pressed", version = 1)]
fn l_pressed() -> bool;
[IntrinsicCall(name = "input.pad.l.released", version = 1)]
fn l_released() -> bool;
[IntrinsicCall(name = "input.pad.l.down", version = 1)]
fn l_down() -> bool;
[IntrinsicCall(name = "input.pad.l.hold", version = 1)]
fn l_hold() -> long;
[IntrinsicCall(name = "input.pad.r.pressed", version = 1)]
fn r_pressed() -> bool;
[IntrinsicCall(name = "input.pad.r.released", version = 1)]
fn r_released() -> bool;
[IntrinsicCall(name = "input.pad.r.down", version = 1)]
fn r_down() -> bool;
[IntrinsicCall(name = "input.pad.r.hold", version = 1)]
fn r_hold() -> long;
[IntrinsicCall(name = "input.pad.start.pressed", version = 1)]
fn start_pressed() -> bool;
[IntrinsicCall(name = "input.pad.start.released", version = 1)]
fn start_released() -> bool;
[IntrinsicCall(name = "input.pad.start.down", version = 1)]
fn start_down() -> bool;
[IntrinsicCall(name = "input.pad.start.hold", version = 1)]
fn start_hold() -> long;
[IntrinsicCall(name = "input.pad.select.pressed", version = 1)]
fn select_pressed() -> bool;
[IntrinsicCall(name = "input.pad.select.released", version = 1)]
fn select_released() -> bool;
[IntrinsicCall(name = "input.pad.select.down", version = 1)]
fn select_down() -> bool;
[IntrinsicCall(name = "input.pad.select.hold", version = 1)]
fn select_hold() -> long;
}
[Doc(markdown = """
Represents the current touch input state.
Queries the selected gamepad button at the time of the call.
""")]
declare service Pad {
[Doc(markdown = """
Queries pressed for the selected button. Only that endpoint is executed.
""")]
fn pressed(button: Button) -> bool {
return switch button {
Button.Up: { LowPad.up_pressed() },
Button.Down: { LowPad.down_pressed() },
Button.Left: { LowPad.left_pressed() },
Button.Right: { LowPad.right_pressed() },
Button.A: { LowPad.a_pressed() },
Button.B: { LowPad.b_pressed() },
Button.X: { LowPad.x_pressed() },
Button.Y: { LowPad.y_pressed() },
Button.L: { LowPad.l_pressed() },
Button.R: { LowPad.r_pressed() },
Button.Start: { LowPad.start_pressed() },
Button.Select: { LowPad.select_pressed() },
};
}
[Doc(markdown = """
Queries released for the selected button. Only that endpoint is executed.
""")]
fn released(button: Button) -> bool {
return switch button {
Button.Up: { LowPad.up_released() },
Button.Down: { LowPad.down_released() },
Button.Left: { LowPad.left_released() },
Button.Right: { LowPad.right_released() },
Button.A: { LowPad.a_released() },
Button.B: { LowPad.b_released() },
Button.X: { LowPad.x_released() },
Button.Y: { LowPad.y_released() },
Button.L: { LowPad.l_released() },
Button.R: { LowPad.r_released() },
Button.Start: { LowPad.start_released() },
Button.Select: { LowPad.select_released() },
};
}
[Doc(markdown = """
Queries down for the selected button. Only that endpoint is executed.
""")]
fn down(button: Button) -> bool {
return switch button {
Button.Up: { LowPad.up_down() },
Button.Down: { LowPad.down_down() },
Button.Left: { LowPad.left_down() },
Button.Right: { LowPad.right_down() },
Button.A: { LowPad.a_down() },
Button.B: { LowPad.b_down() },
Button.X: { LowPad.x_down() },
Button.Y: { LowPad.y_down() },
Button.L: { LowPad.l_down() },
Button.R: { LowPad.r_down() },
Button.Start: { LowPad.start_down() },
Button.Select: { LowPad.select_down() },
};
}
[Doc(markdown = """
Queries hold for the selected button. Only that endpoint is executed.
""")]
fn hold(button: Button) -> long {
return switch button {
Button.Up: { LowPad.up_hold() },
Button.Down: { LowPad.down_hold() },
Button.Left: { LowPad.left_hold() },
Button.Right: { LowPad.right_hold() },
Button.A: { LowPad.a_hold() },
Button.B: { LowPad.b_hold() },
Button.X: { LowPad.x_hold() },
Button.Y: { LowPad.y_hold() },
Button.L: { LowPad.l_hold() },
Button.R: { LowPad.r_hold() },
Button.Start: { LowPad.start_hold() },
Button.Select: { LowPad.select_hold() },
};
}
}
[Doc(markdown = """
Queries the current touch contact and coordinates.
""")]
[BuiltinType(name = "input.touch", version = 1)]
declare builtin type InputTouch() {
[Doc(markdown = """
Returns the touch contact as a button state.
""")]
[IntrinsicCall(name = "button", version = 1)]
fn button() -> InputButton;
[Doc(markdown = """
Returns the current touch x coordinate.
""")]
[IntrinsicCall(name = "x", version = 1)]
fn x() -> int;
[Doc(markdown = """
Returns the current touch y coordinate.
""")]
[IntrinsicCall(name = "y", version = 1)]
fn y() -> int;
}
[Doc(markdown = """
Represents one digital input button.
""")]
[BuiltinType(name = "input.button", version = 1)]
declare builtin type InputButton() {
[Doc(markdown = """
Returns true on the frame where the button becomes down.
""")]
[IntrinsicCall(name = "pressed", version = 1)]
declare builtin type Touch() {
[IntrinsicCall(name = "input.touch.button.pressed", version = 1)]
fn pressed() -> bool;
[Doc(markdown = """
Returns true on the frame where the button becomes up.
""")]
[IntrinsicCall(name = "released", version = 1)]
[IntrinsicCall(name = "input.touch.button.released", version = 1)]
fn released() -> bool;
[Doc(markdown = """
Returns true while the button is currently down.
""")]
[IntrinsicCall(name = "down", version = 1)]
[IntrinsicCall(name = "input.touch.button.down", version = 1)]
fn down() -> bool;
[Doc(markdown = """
Returns how long the button has been held.
""")]
[IntrinsicCall(name = "hold", version = 1)]
fn hold() -> int;
}
[Doc(markdown = """
High-level access to player input devices.
""")]
declare service Input
{
[Doc(markdown = """
Returns the gamepad input state.
""")]
fn pad() -> InputPad
{
return InputType.pad();
}
[Doc(markdown = """
Returns the touch input state.
""")]
fn touch() -> InputTouch
{
return InputType.touch();
}
[IntrinsicCall(name = "input.touch.button.hold", version = 1)]
fn hold() -> long;
[IntrinsicCall(name = "x", version = 1)]
fn x() -> long;
[IntrinsicCall(name = "y", version = 1)]
fn y() -> long;
}
@@ -1,4 +1,4 @@
pub struct InputPad;
pub struct InputTouch;
pub struct InputButton;
pub service Input;
pub enum Button;
pub service Pad;
pub struct Touch;
pub struct LowPad;
@@ -41,6 +41,15 @@ class PbsGateUSdkInterfaceConformanceTest {
@TempDir
Path tempDir;
@Test
void removedInputObjectsExplainTheirMigration() throws IOException {
final var result = compileWorkspaceModule(tempDir.resolve("removed-input"), """
import { Input } from @sdk:input;
fn main() -> void { let button = Input.pad().a(); }
""", "pub fn main() -> void;", 1, null);
assertTrue(result.diagnostics().stream().anyMatch(d -> d.getMessage().contains("migrate to Pad.pressed")));
}
@Test
void gateU_shouldClassifySourceKindForInterfaceDeclarations() {
final var source = """
@@ -88,12 +97,12 @@ class PbsGateUSdkInterfaceConformanceTest {
import { Color } from @core:color;
import { Composer } from @sdk:composer;
import { Gfx } from @sdk:gfx;
import { Input, InputPad, InputButton } from @sdk:input;
import { Pad, Button, Touch } from @sdk:input;
import { Assets } from @sdk:asset;
import { Log } from @sdk:log;
declare contract Renderer {
fn render(composer: Composer, gfx: Gfx, color: Color, input: Input, pad: InputPad, button: InputButton, assets: Assets, log: Log) -> void;
fn render(composer: Composer, gfx: Gfx, color: Color, pad: Pad, button: Button, assets: Assets, log: Log) -> void;
}
""",
"pub contract Renderer;",
@@ -106,12 +115,12 @@ class PbsGateUSdkInterfaceConformanceTest {
assertTrue(positive.irBackend().getReservedMetadata().builtinTypeSurfaces().stream()
.anyMatch(t -> t.sourceTypeName().equals("Color")));
assertTrue(positive.irBackend().getReservedMetadata().builtinTypeSurfaces().stream()
.anyMatch(t -> t.sourceTypeName().equals("InputType") && t.canonicalTypeName().equals("input")));
.anyMatch(t -> t.sourceTypeName().equals("LowPad") && t.canonicalTypeName().equals("input.pad")));
assertFalse(positive.irBackend().getReservedMetadata().builtinConstSurfaces().stream()
.anyMatch(c -> c.canonicalTarget().equals("input")));
assertTrue(positive.irBackend().getReservedMetadata().builtinTypeSurfaces().stream()
.anyMatch(t -> t.sourceTypeName().equals("InputButton")
&& t.intrinsics().stream().anyMatch(i -> i.canonicalName().equals("input.button.hold"))));
.anyMatch(t -> t.sourceTypeName().equals("LowPad")
&& t.intrinsics().stream().anyMatch(i -> i.canonicalName().equals("input.pad.a.hold"))));
assertTrue(positive.irBackend().getReservedMetadata().hostMethodBindings().stream()
.anyMatch(h -> h.ownerName().equals("LowGfx")
&& h.abiModule().equals("gfx2d")
@@ -133,11 +133,11 @@ final class PbsSemanticPresentationContractTest {
@Test
void shouldClassifyBuiltinTypesExposedByImportedStdlibModules() {
final List<FrontendSemanticToken> semanticTokens = PBSDefinitions.PBS.getSemanticTokenProvider().tokenize("""
import { Input } from @sdk:input;
import { Pad, Button, Touch } from @sdk:input;
fn main() -> void {
let touch : InputTouch = Input.touch();
let pad : InputPad = Input.pad();
let touch : long = Touch.x();
let held : long = Pad.hold(Button.A);
}
""");
@@ -1152,8 +1152,8 @@ class PBSFrontendPhaseServiceTest {
.toList();
assertTrue(intrinsicNames.contains("color.rgba"), "intrinsics=" + intrinsicNames);
assertTrue(intrinsicNames.contains("color.html_rgba"), "intrinsics=" + intrinsicNames);
assertTrue(intrinsicNames.contains("color.red"), "intrinsics=" + intrinsicNames);
assertTrue(intrinsicNames.contains("color.transparent"), "intrinsics=" + intrinsicNames);
assertTrue(irBackend.getExecutableFunctions().stream().flatMap(f -> f.instructions().stream()).anyMatch(i -> i.kind() == IRBackendExecutableFunction.InstructionKind.BUILTIN_CONST && i.label().equals("color.red@1")));
assertTrue(irBackend.getExecutableFunctions().stream().flatMap(f -> f.instructions().stream()).anyMatch(i -> i.kind() == IRBackendExecutableFunction.InstructionKind.BUILTIN_CONST && i.label().equals("color.transparent@1")));
}
@Test
@@ -1647,7 +1647,7 @@ class PBSFrontendPhaseServiceTest {
}
@Test
void shouldResolveImportedBuiltinTypeChainsInExpressionFlowSemantics() throws IOException {
void shouldResolveImportedPadFacadeAndTypedButton() throws IOException {
final var projectRoot = tempDir.resolve("project-builtin-chain");
final var sourceRoot = projectRoot.resolve("src");
Files.createDirectories(sourceRoot);
@@ -1656,12 +1656,12 @@ class PBSFrontendPhaseServiceTest {
final var modBarrel = sourceRoot.resolve("mod.barrel");
Files.writeString(sourceFile, """
import { Log } from @sdk:log;
import { Input } from @sdk:input;
import { Pad, Button, Touch } from @sdk:input;
[Frame]
fn frame() -> void
{
if (Input.pad().a().pressed())
if (Pad.pressed(Button.A))
{
Log.debug("Hello World!");
}
@@ -1722,23 +1722,11 @@ class PBSFrontendPhaseServiceTest {
.filter(instruction ->
instruction.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC)
.toList();
final var inputPadExecutable = irBackend.getExecutableFunctions().stream()
.filter(function -> "Input.pad".equals(function.callableName()))
.findFirst()
.orElseThrow();
final var inputPadIntrinsics = inputPadExecutable.instructions().stream()
.filter(instruction ->
instruction.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC
&& instruction.intrinsicCall() != null)
.map(instruction -> instruction.intrinsicCall().canonicalName())
.toList();
assertTrue(callableCalls.contains("Input.pad"));
assertTrue(intrinsicCalls.stream().anyMatch(instruction -> Integer.valueOf(1).equals(instruction.expectedArgSlots())));
assertTrue(inputPadIntrinsics.contains("input.pad"));
assertTrue(intrinsicCalls.stream().anyMatch(instruction ->
instruction.intrinsicCall() != null && "input.pad.a".equals(instruction.intrinsicCall().canonicalName())));
assertTrue(intrinsicCalls.stream().anyMatch(instruction ->
instruction.intrinsicCall() != null && "input.button.pressed".equals(instruction.intrinsicCall().canonicalName())));
assertTrue(callableCalls.contains("Pad.pressed"));
assertTrue(intrinsicCalls.isEmpty());
final var pad = irBackend.getExecutableFunctions().stream().filter(f -> f.callableName().equals("Pad.pressed")).findFirst().orElseThrow();
assertEquals(12, pad.instructions().stream().filter(i -> i.intrinsicCall() != null).count());
assertTrue(pad.instructions().stream().filter(i -> i.intrinsicCall() != null).allMatch(i -> i.expectedArgSlots() == 0));
assertTrue(
irBackend.getExecutableFunctions().stream().anyMatch(function -> "frame".equals(function.callableName())),
"diagnostics="
@@ -1748,7 +1736,7 @@ class PBSFrontendPhaseServiceTest {
}
@Test
void shouldResolveIntrinsicChainsWithMethodNameCollisionsByOwnerIdentity() throws IOException {
void shouldResolvePadAndTouchQueriesByOwnerIdentity() throws IOException {
final var projectRoot = tempDir.resolve("project-intrinsic-owner-collision");
final var sourceRoot = projectRoot.resolve("src");
Files.createDirectories(sourceRoot);
@@ -1756,13 +1744,13 @@ class PBSFrontendPhaseServiceTest {
final var sourceFile = sourceRoot.resolve("main.pbs");
final var modBarrel = sourceRoot.resolve("mod.barrel");
Files.writeString(sourceFile, """
import { Input } from @sdk:input;
import { Pad, Button, Touch } from @sdk:input;
[Frame]
fn frame() -> void
{
Input.pad().x().pressed();
Input.touch().x();
Pad.pressed(Button.X);
Touch.x();
}
""");
Files.writeString(modBarrel, "pub fn frame() -> void;");
@@ -1811,37 +1799,16 @@ class PBSFrontendPhaseServiceTest {
&& instruction.intrinsicCall() != null)
.map(instruction -> instruction.intrinsicCall().canonicalName())
.toList();
final var inputPadExecutable = irBackend.getExecutableFunctions().stream()
.filter(function -> "Input.pad".equals(function.callableName()))
.findFirst()
.orElseThrow();
final var inputTouchExecutable = irBackend.getExecutableFunctions().stream()
.filter(function -> "Input.touch".equals(function.callableName()))
.findFirst()
.orElseThrow();
final var inputPadIntrinsics = inputPadExecutable.instructions().stream()
.filter(instruction ->
instruction.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC
&& instruction.intrinsicCall() != null)
.map(instruction -> instruction.intrinsicCall().canonicalName())
.toList();
final var inputTouchIntrinsics = inputTouchExecutable.instructions().stream()
.filter(instruction ->
instruction.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC
&& instruction.intrinsicCall() != null)
.map(instruction -> instruction.intrinsicCall().canonicalName())
.toList();
assertTrue(callableCalls.contains("Input.pad"));
assertTrue(callableCalls.contains("Input.touch"));
assertTrue(inputPadIntrinsics.contains("input.pad"));
assertTrue(intrinsicCalls.contains("input.pad.x"));
assertTrue(intrinsicCalls.contains("input.button.pressed"));
assertTrue(inputTouchIntrinsics.contains("input.touch"));
assertTrue(callableCalls.contains("Pad.pressed"));
assertTrue(intrinsicCalls.contains("input.touch.x"));
assertFalse(intrinsicCalls.contains("input.pad"));
final var pad = irBackend.getExecutableFunctions().stream().filter(f -> f.callableName().equals("Pad.pressed")).findFirst().orElseThrow();
assertTrue(pad.instructions().stream().anyMatch(i -> i.intrinsicCall() != null && i.intrinsicCall().canonicalName().equals("input.pad.x.pressed")));
}
@Test
void shouldResolveIntrinsicCallsOnBuiltinValuesStoredInLocals() throws IOException {
void shouldPassStoredButtonValuesToPadQueries() throws IOException {
final var projectRoot = tempDir.resolve("project-intrinsic-local-builtin-receiver");
final var sourceRoot = projectRoot.resolve("src");
Files.createDirectories(sourceRoot);
@@ -1849,14 +1816,14 @@ class PBSFrontendPhaseServiceTest {
final var sourceFile = sourceRoot.resolve("main.pbs");
final var modBarrel = sourceRoot.resolve("mod.barrel");
Files.writeString(sourceFile, """
import { Input } from @sdk:input;
import { Pad, Button, Touch } from @sdk:input;
[Frame]
fn frame() -> void
{
let touch = Input.touch();
touch.x();
touch.y();
let button = Button.A;
Pad.pressed(button);
Touch.y();
}
""");
Files.writeString(modBarrel, "pub fn frame() -> void;");
@@ -1905,20 +1872,10 @@ class PBSFrontendPhaseServiceTest {
&& instruction.intrinsicCall() != null)
.map(instruction -> instruction.intrinsicCall().canonicalName())
.toList();
final var inputTouchExecutable = irBackend.getExecutableFunctions().stream()
.filter(function -> "Input.touch".equals(function.callableName()))
.findFirst()
.orElseThrow();
final var inputTouchIntrinsics = inputTouchExecutable.instructions().stream()
.filter(instruction ->
instruction.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC
&& instruction.intrinsicCall() != null)
.map(instruction -> instruction.intrinsicCall().canonicalName())
.toList();
assertTrue(callableCalls.contains("Input.touch"));
assertTrue(inputTouchIntrinsics.contains("input.touch"));
assertTrue(intrinsicCalls.contains("input.touch.x"));
assertTrue(callableCalls.contains("Pad.pressed"));
assertTrue(intrinsicCalls.contains("input.touch.y"));
assertFalse(intrinsicCalls.contains("input.touch"));
}
@Test
@@ -3,9 +3,18 @@ plugins {
}
dependencies {
testImplementation(project(":prometeu-compiler:frontends:prometeu-frontend-pbs"))
api(project(":prometeu-infra"))
implementation(project(":prometeu-compiler:prometeu-deps"))
implementation(project(":prometeu-compiler:prometeu-compiler-core"))
implementation(project(":prometeu-compiler:prometeu-frontend-api"))
implementation(project(":prometeu-compiler:prometeu-frontend-registry"))
}
// Versioned provider mirrors are packaged locally; normal builds never read ../runtime.
tasks.named<ProcessResources>("processResources") {
from(rootProject.layout.projectDirectory.dir("docs/vm-arch")) {
include("INTRINSICS.csv", "BUILTIN_CONSTANTS.csv", "runtime-catalog.properties")
into("runtime-catalog/compiler")
}
}
@@ -6,6 +6,7 @@ import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Objects;
@@ -48,8 +49,11 @@ public class BytecodeEmitter {
final EmissionPlan plan) {
final var inputPlan = plan == null ? EmissionPlan.empty() : plan;
final var canonicalConstPool = dedupeConstPool(inputPlan.constPool());
final var constantIndices = new HashMap<BytecodeModule.ConstantPoolEntry, Integer>();
for (int i=0;i<canonicalConstPool.size();i++) constantIndices.put(canonicalConstPool.get(i),i);
final var canonicalExports = dedupeExports(inputPlan.exports());
final var functionFragments = new ArrayList<BytecodeFunctionLayoutBuilder.FunctionFragment>(inputPlan.functions().size());
final var intrinsicSignatures = new LinkedHashMap<Integer, BytecodeModule.IntrinsicSignature>();
final var orderedSyscalls = new LinkedHashMap<SyscallIdentity, BytecodeModule.SyscallDecl>();
final var syscallIndexByIdentity = new LinkedHashMap<SyscallIdentity, Integer>();
@@ -59,6 +63,12 @@ public class BytecodeEmitter {
for (final var op : function.operations()) {
final var pc = code.size();
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());
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
}
case HALT -> {
writeOpNoImm(code, OP_HALT);
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
@@ -72,7 +82,7 @@ public class BytecodeEmitter {
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
}
case PUSH_CONST -> {
writeOpU32(code, OP_PUSH_CONST, op.immediate());
writeOpU32(code, OP_PUSH_CONST, constantIndices.get(inputPlan.constPool().get(op.immediate())));
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
}
case POP -> {
@@ -176,6 +186,11 @@ public class BytecodeEmitter {
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
}
case INTRINSIC -> {
p.studio.compiler.backend.irvm.IRVMIntrinsicRegistry.requireById(op.immediate())
.validateSignature(op.expectedArgSlots(), op.expectedRetSlots());
var signature = new BytecodeModule.IntrinsicSignature(op.immediate(),op.expectedArgSlots(),op.expectedRetSlots());
var previous = intrinsicSignatures.putIfAbsent(op.immediate(),signature);
if(previous!=null&&!previous.equals(signature)) throw new IllegalArgumentException("conflicting intrinsic ABI");
writeOpU32(code, OP_INTRINSIC, op.immediate());
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
}
@@ -197,6 +212,7 @@ public class BytecodeEmitter {
syscallIndexByIdentity.put(identity, syscallIndexByIdentity.size());
orderedSyscalls.put(identity, decl);
}
if (!orderedSyscalls.get(identity).equals(decl)) throw new IllegalArgumentException("conflicting HOSTCALL ABI for " + identity);
final var index = syscallIndexByIdentity.get(identity);
writeOpU32(code, OP_HOSTCALL, index);
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
@@ -213,7 +229,7 @@ public class BytecodeEmitter {
function.localSlots(),
function.returnSlots(),
function.maxStackSlots(),
ReadOnlyList.wrap(spans)));
ReadOnlyList.wrap(spans), function.metadata()));
}
final var layout = BytecodeFunctionLayoutBuilder.build(ReadOnlyList.wrap(functionFragments));
@@ -224,7 +240,7 @@ public class BytecodeEmitter {
layout.code(),
layout.debugInfo(),
canonicalExports,
ReadOnlyList.wrap(orderedSyscalls.values()));
ReadOnlyList.wrap(orderedSyscalls.values()), inputPlan.registry(), ReadOnlyList.wrap(intrinsicSignatures.values()));
}
private static void writeOpNoImm(final ByteArrayOutputStream out, final int opcode) {
@@ -283,10 +299,19 @@ public class BytecodeEmitter {
}
public record EmissionPlan(
int version,
ReadOnlyList<BytecodeModule.ConstantPoolEntry> constPool,
ReadOnlyList<BytecodeModule.Export> exports,
ReadOnlyList<FunctionPlan> functions,
p.studio.compiler.models.registry.IRRegistry registry) {
public EmissionPlan(
int version,
ReadOnlyList<BytecodeModule.ConstantPoolEntry> constPool,
ReadOnlyList<BytecodeModule.Export> exports,
ReadOnlyList<FunctionPlan> functions) {
this(version, constPool, exports, functions, p.studio.compiler.models.registry.IRRegistry.empty());
}
public EmissionPlan {
constPool = constPool == null ? ReadOnlyList.empty() : constPool;
exports = exports == null ? ReadOnlyList.empty() : exports;
@@ -299,12 +324,23 @@ public class BytecodeEmitter {
}
public record FunctionPlan(
String name,
int paramSlots,
int localSlots,
int returnSlots,
int maxStackSlots,
ReadOnlyList<Operation> operations,
p.studio.compiler.models.registry.IRRegistry.FunctionMetadata metadata) {
public FunctionPlan(
String name,
int paramSlots,
int localSlots,
int returnSlots,
int maxStackSlots,
ReadOnlyList<Operation> operations) {
this(name, paramSlots, localSlots, returnSlots, maxStackSlots, operations, paramSlots == 0 && returnSlots == 0 ? p.studio.compiler.models.registry.IRRegistry.FunctionMetadata.empty() : null);
}
public FunctionPlan {
Objects.requireNonNull(name, "name");
operations = operations == null ? ReadOnlyList.empty() : operations;
@@ -312,6 +348,7 @@ public class BytecodeEmitter {
}
public enum OperationKind {
REGISTRY,
HALT,
RET,
PUSH_CONST,
@@ -347,12 +384,23 @@ public class BytecodeEmitter {
}
public record Operation(
OperationKind kind,
int immediate,
BytecodeModule.SyscallDecl syscallDecl,
Integer expectedArgSlots,
Integer expectedRetSlots,
BytecodeModule.SourceSpan span,
p.studio.compiler.backend.irvm.IRVMInstruction registryInstruction) {
public Operation(
OperationKind kind,
int immediate,
BytecodeModule.SyscallDecl syscallDecl,
Integer expectedArgSlots,
Integer expectedRetSlots,
BytecodeModule.SourceSpan span) {
this(kind, immediate, syscallDecl, expectedArgSlots, expectedRetSlots, span, null);
}
public Operation {
Objects.requireNonNull(kind, "kind");
}
@@ -25,7 +25,7 @@ public final class BytecodeFunctionLayoutBuilder {
fn.paramSlots(),
fn.localSlots(),
fn.returnSlots(),
fn.maxStackSlots()));
fn.maxStackSlots(), fn.metadata()));
names.add(new BytecodeModule.FunctionName(i, fn.name()));
for (final var span : fn.instructionSpans()) {
@@ -58,6 +58,15 @@ public final class BytecodeFunctionLayoutBuilder {
}
public record FunctionFragment(
String name,
byte[] code,
int paramSlots,
int localSlots,
int returnSlots,
int maxStackSlots,
ReadOnlyList<InstructionSpan> instructionSpans,
p.studio.compiler.models.registry.IRRegistry.FunctionMetadata metadata) {
public FunctionFragment(
String name,
byte[] code,
int paramSlots,
@@ -65,6 +74,9 @@ public final class BytecodeFunctionLayoutBuilder {
int returnSlots,
int maxStackSlots,
ReadOnlyList<InstructionSpan> instructionSpans) {
this(name, code, paramSlots, localSlots, returnSlots, maxStackSlots, instructionSpans, paramSlots == 0 && returnSlots == 0 ? p.studio.compiler.models.registry.IRRegistry.FunctionMetadata.empty() : null);
}
public FunctionFragment {
Objects.requireNonNull(name, "name");
code = code == null ? new byte[0] : code.clone();
@@ -46,16 +46,18 @@ public class BytecodeLinkPrecheckService {
}
private void validateHostcallIndices(final BytecodeModule module) {
var pc = 0;
while (pc < module.code().length) {
if (pc + 2 > module.code().length) {
for (var function : module.functions()) {
int end = function.codeOffset() + function.codeLen();
var pc = function.codeOffset();
while (pc < end) {
if (pc + 2 > end) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_TRUNCATED_INSTRUCTION,
"truncated opcode at pc=" + pc);
}
final var opcode = readU16(module.code(), pc);
final var size = sizeForOpcode(opcode, pc);
if (pc + size > module.code().length) {
if (pc + size > end) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_TRUNCATED_INSTRUCTION,
"truncated instruction at pc=" + pc);
@@ -70,6 +72,7 @@ public class BytecodeLinkPrecheckService {
}
pc += size;
}
}
}
private int sizeForOpcode(
@@ -1,6 +1,8 @@
package p.studio.compiler.backend.bytecode;
public enum BytecodeMarshalingErrorCode {
MARSHAL_FORMAT_REGISTRY_INVALID,
MARSHAL_VERIFY_REGISTRY_INVALID,
MARSHAL_FORMAT_STRING_TOO_LONG,
MARSHAL_FORMAT_SYSC_MODULE_TOO_LONG,
MARSHAL_FORMAT_SYSC_NAME_TOO_LONG,
@@ -2,11 +2,7 @@ package p.studio.compiler.backend.bytecode;
import p.studio.utilities.structures.ReadOnlyList;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Objects;
public record BytecodeModule(
@@ -16,16 +12,38 @@ public record BytecodeModule(
byte[] code,
DebugInfo debugInfo,
ReadOnlyList<Export> exports,
ReadOnlyList<SyscallDecl> syscalls) {
ReadOnlyList<SyscallDecl> syscalls,
p.studio.compiler.models.registry.IRRegistry registry,
ReadOnlyList<IntrinsicSignature> intrinsicSignatures) {
public BytecodeModule(
int version,
ReadOnlyList<ConstantPoolEntry> constPool,
ReadOnlyList<FunctionMeta> functions,
byte[] code,
DebugInfo debugInfo,
ReadOnlyList<Export> exports,
ReadOnlyList<SyscallDecl> syscalls,
p.studio.compiler.models.registry.IRRegistry registry) {
this(version, constPool, functions, code, debugInfo, exports, syscalls, registry, ReadOnlyList.empty());
}
private static final int SECTION_KIND_CONST_POOL = 0;
private static final int SECTION_KIND_FUNCTIONS = 1;
private static final int SECTION_KIND_CODE = 2;
private static final int SECTION_KIND_DEBUG = 3;
private static final int SECTION_KIND_EXPORTS = 4;
private static final int SECTION_KIND_SYSCALLS = 5;
public BytecodeModule(
int version,
ReadOnlyList<ConstantPoolEntry> constPool,
ReadOnlyList<FunctionMeta> functions,
byte[] code,
DebugInfo debugInfo,
ReadOnlyList<Export> exports,
ReadOnlyList<SyscallDecl> syscalls) {
this(version, constPool, functions, code, debugInfo, exports, syscalls, p.studio.compiler.models.registry.IRRegistry.empty());
}
public record IntrinsicSignature(int id, int argumentSlots, int resultSlots) {}
public BytecodeModule {
intrinsicSignatures = intrinsicSignatures == null ? ReadOnlyList.empty() : ReadOnlyList.wrap(intrinsicSignatures.asList());
registry = registry == null ? p.studio.compiler.models.registry.IRRegistry.empty() : registry;
constPool = constPool == null ? ReadOnlyList.empty() : constPool;
functions = functions == null ? ReadOnlyList.empty() : functions;
code = code == null ? new byte[0] : code.clone();
@@ -45,191 +63,7 @@ public record BytecodeModule(
}
public byte[] serialize() {
final var constPoolData = serializeConstPool();
final var functionData = serializeFunctions();
final var codeData = code();
final var debugData = serializeDebug();
final var exportData = serializeExports();
final var syscallData = serializeSyscalls();
final var sections = new ArrayList<SectionData>();
if (constPoolData.length > 0) {
sections.add(new SectionData(SECTION_KIND_CONST_POOL, constPoolData));
}
if (functionData.length > 0) {
sections.add(new SectionData(SECTION_KIND_FUNCTIONS, functionData));
}
if (codeData.length > 0) {
sections.add(new SectionData(SECTION_KIND_CODE, codeData));
}
if (debugData.length > 0) {
sections.add(new SectionData(SECTION_KIND_DEBUG, debugData));
}
if (exportData.length > 0) {
sections.add(new SectionData(SECTION_KIND_EXPORTS, exportData));
}
// SYSC section is mandatory, even when empty.
sections.add(new SectionData(SECTION_KIND_SYSCALLS, syscallData));
final var out = new ByteArrayOutputStream();
out.writeBytes(new byte[] { 'P', 'B', 'X', 0 });
writeU16(out, version & 0xFFFF);
out.write(0);
out.write(0);
writeU32(out, sections.size());
out.writeBytes(new byte[20]);
var currentOffset = 32 + (sections.size() * 12);
for (final var section : sections) {
writeU32(out, section.kind);
writeU32(out, currentOffset);
writeU32(out, section.data.length);
currentOffset += section.data.length;
}
for (final var section : sections) {
out.writeBytes(section.data);
}
return out.toByteArray();
}
private byte[] serializeConstPool() {
if (constPool.isEmpty()) {
return new byte[0];
}
final var out = new ByteArrayOutputStream();
writeU32(out, constPool.size());
for (final var entry : constPool) {
switch (entry) {
case NullConstant ignored -> out.write(0);
case Int64Constant value -> {
out.write(1);
writeI64(out, value.value());
}
case Float64Constant value -> {
out.write(2);
writeF64(out, value.value());
}
case BooleanConstant value -> {
out.write(3);
out.write(value.value() ? 1 : 0);
}
case StringConstant value -> {
out.write(4);
writeU32(out, value.utf8().length);
out.writeBytes(value.utf8());
}
case Int32Constant value -> {
out.write(5);
writeI32(out, value.value());
}
}
}
return out.toByteArray();
}
private byte[] serializeFunctions() {
if (functions.isEmpty()) {
return new byte[0];
}
final var out = new ByteArrayOutputStream();
writeU32(out, functions.size());
for (final var function : functions) {
writeU32(out, function.codeOffset());
writeU32(out, function.codeLen());
writeU16(out, function.paramSlots());
writeU16(out, function.localSlots());
writeU16(out, function.returnSlots());
writeU16(out, function.maxStackSlots());
}
return out.toByteArray();
}
private byte[] serializeDebug() {
if (debugInfo == null || (debugInfo.pcToSpan().isEmpty() && debugInfo.functionNames().isEmpty())) {
return new byte[0];
}
final var out = new ByteArrayOutputStream();
writeU32(out, debugInfo.pcToSpan().size());
for (final var entry : debugInfo.pcToSpan()) {
writeU32(out, entry.pc());
writeU32(out, entry.span().fileId());
writeU32(out, entry.span().start());
writeU32(out, entry.span().end());
}
writeU32(out, debugInfo.functionNames().size());
for (final var fn : debugInfo.functionNames()) {
writeU32(out, fn.funcIdx());
writeU32(out, fn.utf8Name().length);
out.writeBytes(fn.utf8Name());
}
return out.toByteArray();
}
private byte[] serializeExports() {
if (exports.isEmpty()) {
return new byte[0];
}
final var out = new ByteArrayOutputStream();
writeU32(out, exports.size());
for (final var export : exports) {
writeU32(out, export.funcIdx());
writeU32(out, export.utf8Symbol().length);
out.writeBytes(export.utf8Symbol());
}
return out.toByteArray();
}
private byte[] serializeSyscalls() {
final var out = new ByteArrayOutputStream();
writeU32(out, syscalls.size());
for (final var syscall : syscalls) {
if (syscall.utf8Module().length > 0xFFFF) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_FORMAT_SYSC_MODULE_TOO_LONG,
"syscall module name exceeds u16: " + syscall.module());
}
if (syscall.utf8Name().length > 0xFFFF) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_FORMAT_SYSC_NAME_TOO_LONG,
"syscall name exceeds u16: " + syscall.name());
}
writeU16(out, syscall.utf8Module().length);
out.writeBytes(syscall.utf8Module());
writeU16(out, syscall.utf8Name().length);
out.writeBytes(syscall.utf8Name());
writeU16(out, syscall.version());
writeU16(out, syscall.argSlots());
writeU16(out, syscall.retSlots());
}
return out.toByteArray();
}
private static void writeU16(final ByteArrayOutputStream out, final int value) {
out.writeBytes(ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort((short) (value & 0xFFFF)).array());
}
private static void writeU32(final ByteArrayOutputStream out, final int value) {
out.writeBytes(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(value).array());
}
private static void writeI32(final ByteArrayOutputStream out, final int value) {
out.writeBytes(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(value).array());
}
private static void writeI64(final ByteArrayOutputStream out, final long value) {
out.writeBytes(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(value).array());
}
private static void writeF64(final ByteArrayOutputStream out, final double value) {
out.writeBytes(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putDouble(value).array());
}
private record SectionData(
int kind,
byte[] data) {
return PbxRegistryWriter.write(this);
}
public sealed interface ConstantPoolEntry permits NullConstant, Int64Constant, Float64Constant, BooleanConstant, StringConstant, Int32Constant {
@@ -271,12 +105,23 @@ public record BytecodeModule(
}
public record FunctionMeta(
int codeOffset,
int codeLen,
int paramSlots,
int localSlots,
int returnSlots,
int maxStackSlots,
p.studio.compiler.models.registry.IRRegistry.FunctionMetadata metadata) {
public FunctionMeta(
int codeOffset,
int codeLen,
int paramSlots,
int localSlots,
int returnSlots,
int maxStackSlots) {
this(codeOffset, codeLen, paramSlots, localSlots, returnSlots, maxStackSlots, paramSlots == 0 && returnSlots == 0 ? p.studio.compiler.models.registry.IRRegistry.FunctionMetadata.empty() : null);
}
}
public record SourceSpan(
@@ -1,58 +1,17 @@
package p.studio.compiler.backend.bytecode;
import java.util.Map;
final class BytecodeOpcodeLayout {
private static final Map<Integer, Integer> SIZE_BY_OPCODE = Map.ofEntries(
Map.entry(0x01, 2),
Map.entry(0x02, 6),
Map.entry(0x03, 6),
Map.entry(0x04, 6),
Map.entry(0x10, 6),
Map.entry(0x11, 2),
Map.entry(0x14, 10),
Map.entry(0x15, 10),
Map.entry(0x16, 3),
Map.entry(0x17, 6),
Map.entry(0x18, 6),
Map.entry(0x20, 2),
Map.entry(0x21, 2),
Map.entry(0x22, 2),
Map.entry(0x23, 2),
Map.entry(0x24, 2),
Map.entry(0x30, 2),
Map.entry(0x31, 2),
Map.entry(0x32, 2),
Map.entry(0x33, 2),
Map.entry(0x34, 2),
Map.entry(0x35, 2),
Map.entry(0x36, 2),
Map.entry(0x3C, 2),
Map.entry(0x3D, 2),
Map.entry(0x3E, 2),
Map.entry(0x40, 6),
Map.entry(0x41, 6),
Map.entry(0x42, 6),
Map.entry(0x43, 6),
Map.entry(0x50, 6),
Map.entry(0x51, 2),
Map.entry(0x52, 10),
Map.entry(0x54, 10),
Map.entry(0x56, 6),
Map.entry(0x70, 6),
Map.entry(0x71, 6),
Map.entry(0x72, 6));
private BytecodeOpcodeLayout() {
}
static int sizeForOpcode(final int opcode) {
final var size = SIZE_BY_OPCODE.get(opcode);
if (size == null) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNKNOWN_OPCODE,
"unknown opcode 0x%04X".formatted(opcode));
}
return size;
private 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;
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;
case 0x14,0x15,0x52,0x58,0x93,0x94,0x95,0x96,0xa1,0xa2 -> 10;
default -> throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNKNOWN_OPCODE,
"unknown or retired opcode 0x%04X".formatted(opcode));
};
}
}
@@ -1,70 +1,262 @@
package p.studio.compiler.backend.bytecode;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Set;
import p.studio.compiler.backend.registry.LinkedRegistry;
import p.studio.compiler.backend.irvm.IRVMIntrinsicRegistry;
import p.studio.compiler.backend.irvm.IRVMIntrinsicDefinition.Carrier;
import p.studio.compiler.models.registry.IRRegistry.*;
import java.nio.*;
import java.util.*;
/** Producer verification. Native binding and final admission remain loader responsibilities. */
public class BytecodePreloadVerifierService {
private static final Set<Integer> TERMINATORS = Set.of(0x01, 0x02, 0x51);
public void verify(final BytecodeModule module) {
final var input = module == null ? BytecodeModule.empty() : module;
for (final var function : input.functions()) {
verifyFunction(input.code(), function);
public void verify(BytecodeModule module) {
try { verifyRegistry(module); }
catch (IllegalArgumentException | ArithmeticException error) {
throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_REGISTRY_INVALID, error.getMessage());
}
}
private void verifyFunction(final byte[] code, final BytecodeModule.FunctionMeta function) {
final var start = function.codeOffset();
final var end = start + function.codeLen();
if (end <= start) {
return;
private void verifyRegistry(BytecodeModule module) {
Objects.requireNonNull(module);
// Validate the complete declarative schema without accepting an old slot-only image.
module.serialize();
var registry=LinkedRegistry.link(module.registry());
var intrinsicIds = new HashSet<Integer>();
for (var signature : module.intrinsicSignatures()) {
check(intrinsicIds.add(signature.id()), "duplicate intrinsic signature");
IRVMIntrinsicRegistry.requireById(signature.id()).validateSignature(signature.argumentSlots(), signature.resultSlots());
}
var pc = start;
var lastOpcode = -1;
while (pc < end) {
if (pc + 2 > end) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_TRUNCATED_INSTRUCTION,
"truncated opcode in function at pc=" + pc);
var used=new boolean[module.syscalls().size()];
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 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);
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");
case 0x15 -> check(Double.isFinite(Double.longBitsToDouble(ByteBuffer.wrap(code,pc+2,8).order(ByteOrder.LITTLE_ENDIAN).getLong())),"nonfinite PushF64");
case 0x18,0x40,0x41 -> check(a>=0,"negative count/index");
case 0x42,0x43 -> check(a>=0&&(long)a<f.paramSlots()+f.localSlots(),"local index");
case 0x50 -> {check(a>=0&&a<m.functions().size(),"FunctionId");check(m.functions().get(a).metadata().captures().isEmpty(),"direct call into capturing function");}
case 0x71 -> {check(a>=0&&a<m.syscalls().size(),"HOSTCALL declaration index");used[a]=true;}
case 0x72 -> {
IRVMIntrinsicRegistry.requireById(a);
check(m.intrinsicSignatures().stream().anyMatch(n->n.id()==a),"intrinsic signature required");
}
case 0x52 -> {check(a>=0&&a<m.functions().size(),"closure FunctionId");var target=m.functions().get(a).metadata();var fn=fn(r,b);check(r.sameSignature(fn.signature(),target.signature()),"closure signature");}
case 0x53 -> fn(r,a);
case 0x57 -> check(a>=0&&a<f.metadata().captures().size(),"capture index");
case 0x58 -> method(r,a,b);
case 0x90,0x91,0x92 -> type(r,a);
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);
default -> { }
}
final var opcode = readU16(code, pc);
if (opcode == 0x70) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_LINKAGE_RAW_SYSCALL_IN_PRELOAD,
"raw syscall is forbidden in pre-load artifact");
pc+=size;
}
for(var instruction:instructions.values())if(instruction.opcode()>=2&&instruction.opcode()<=4) {
// Jump operands are absolute offsets within the current function.
long target=(long)start+instruction.first();
check(target>=start&&target<=end&&(target==end||instructions.containsKey((int)target)),"jump outside function or into immediate");
}
analyze(m,r,index,instructions,end);
}
private record Fact(String carrier,Integer type,int component,int width,Long origin,Long integer) {
Fact(String carrier,Integer type,int component,int width) {this(carrier,type,component,width,null,null);}
static final Fact UNKNOWN=new Fact("?",null,0,1),NULL=new Fact("null",null,0,1);
}
private static final class State {
Fact[] locals;ArrayList<Fact> stack;
State(Fact[] locals,ArrayList<Fact> stack){this.locals=locals;this.stack=stack;}
State copy(){return new State(locals.clone(),new ArrayList<>(stack));}
}
private List<Fact> facts(LinkedRegistry r,TypeRef ref) {
int id=r.id(ref).index(),width=r.width(ref);var body=r.type(ref).body();String carrier;
if(body instanceof Builtin b)carrier=switch(b.id()){case 1,3,0x11->"i64";case 2,0x12->"f64";case 0x10->"i32";case 0x13->"bool";case 0x14->"string";case 0x15->"?";default->"ref";};
else carrier=body instanceof Trait?"?":"ref";
var result=new ArrayList<Fact>();for(int i=0;i<width;i++) {
String componentCarrier=carrier;
if(body instanceof Builtin b&&(b.id()==1||b.id()==3&&i==2))componentCarrier="color";
if(body instanceof Trait)componentCarrier=i==0?"ref|null":"i64|null";
if(body instanceof Builtin b&&b.id()==0x15)componentCarrier=i==0?"i64|null":"?";
result.add(new Fact(componentCarrier,id,i,width));
}return result;
}
private List<Fact> parameters(LinkedRegistry r,Signature signature){var result=new ArrayList<Fact>();for(var p:signature.parameters())result.addAll(facts(r,p.type()));return result;}
private List<Fact> results(LinkedRegistry r,Signature signature){var result=new ArrayList<Fact>();for(var t:signature.results())result.addAll(facts(r,t));return result;}
private void analyze(BytecodeModule m,LinkedRegistry r,int index,TreeMap<Integer,Instruction> code,int end) {
var f=m.functions().get(index);var locals=new Fact[f.paramSlots()+f.localSlots()];Arrays.fill(locals,Fact.NULL);
var parameters=parameters(r,f.metadata().signature());for(int i=0;i<parameters.size();i++)locals[i]=stamp(parameters.get(i),-1L-(i-parameters.get(i).component()));
var states=new HashMap<Integer,State>();var queue=new ArrayDeque<Integer>();states.put(f.codeOffset(),new State(locals,new ArrayList<>()));queue.add(f.codeOffset());
long work=0;
while(!queue.isEmpty()) {
int pc=queue.remove();var state=states.get(pc).copy();var instruction=code.get(pc);
check((work+=1L+state.locals.length+state.stack.size())<=268_435_456L,"verification work limit");
transfer(m,r,index,instruction,state,false);
for(int successor:successors(instruction,f.codeOffset(),end)) {
if(successor==end) {check(state.stack.size()==f.returnSlots(),"function-end result height");continue;}
var old=states.get(successor);if(old==null){check((long)(states.size()+1)*(locals.length+f.maxStackSlots())*24<=128L*1024*1024,"verification scratch limit");states.put(successor,state.copy());queue.add(successor);}
else {check(old.stack.size()==state.stack.size(),"join height mismatch");boolean changed=false;
for(int i=0;i<old.locals.length;i++)if(!old.locals[i].equals(state.locals[i])&&!old.locals[i].equals(Fact.UNKNOWN)){old.locals[i]=Fact.UNKNOWN;changed=true;}
for(int i=0;i<old.stack.size();i++)if(!old.stack.get(i).equals(state.stack.get(i))&&!old.stack.get(i).equals(Fact.UNKNOWN)){old.stack.set(i,Fact.UNKNOWN);changed=true;}
if(changed)queue.add(successor);
}
}
final var size = sizeForOpcode(opcode, pc);
if (pc + size > end) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_TRUNCATED_INSTRUCTION,
"truncated instruction in function at pc=" + pc);
}
// Diagnose proven type errors after widening has converged, not in traversal order.
for(var entry:states.entrySet()) {
var state=entry.getValue().copy();var instruction=code.get(entry.getKey());transfer(m,r,index,instruction,state,true);
for(int successor:successors(instruction,f.codeOffset(),end))if(successor==end)compatible(state.stack,results(r,f.metadata().signature()),true);
}
}
private List<Integer> successors(Instruction i,int start,int end) {
if(i.opcode()==0x01||i.opcode()==0x05||i.opcode()==0x51)return List.of();
if(i.opcode()==0x02)return List.of(start+i.first());
if(i.opcode()==0x03||i.opcode()==0x04) {
if(i.next()==end)throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNTERMINATED_FUNCTION,"conditional fallthrough at function end");
return List.of(i.next(),start+i.first());
}
if(i.next()>=end)throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNTERMINATED_FUNCTION,"reachable fallthrough at function end");return List.of(i.next());
}
private void transfer(BytecodeModule m,LinkedRegistry r,int index,Instruction i,State s,boolean typed) {
int a=i.first(),b=i.second(),op=i.opcode();var f=m.functions().get(index);
switch(op) {
case 0x00,0x01,0x02,0x05,0x80 -> { }
case 0x03,0x04 -> consume(s,List.of(carrier("bool")),typed);
case 0x10 -> {var c=m.constPool().get(a);s.stack.add(switch(c){case BytecodeModule.NullConstant n->Fact.NULL;case BytecodeModule.Int32Constant n->carrier("i32");case BytecodeModule.Int64Constant n->integer(n.value());case BytecodeModule.Float64Constant n->carrier("f64");case BytecodeModule.BooleanConstant n->carrier("bool");case BytecodeModule.StringConstant n->carrier("string");});}
case 0x11 -> pop(s,1);
case 0x12 -> {var value=pop(s,1);s.stack.addAll(value);s.stack.addAll(value);}
case 0x13 -> {var values=pop(s,2);s.stack.add(values.get(1));s.stack.add(values.get(0));}
case 0x14 -> s.stack.add(integer(Integer.toUnsignedLong(a)|((long)b<<32)));case 0x15->s.stack.add(carrier("f64"));case 0x16->s.stack.add(carrier("bool"));case 0x17->s.stack.add(carrier("i32"));case 0x18->pop(s,a);
case 0x40 -> s.stack.add(Fact.UNKNOWN);case 0x41->pop(s,1);
case 0x42 -> s.stack.add(s.locals[a]);case 0x43 -> {var value=pop(s,1).getFirst();s.locals[a]=value;}
case 0x50 -> {var metadata=m.functions().get(a).metadata();consume(s,parameters(r,metadata.signature()),typed);produce(s,results(r,metadata.signature()),i.pc());}
case 0x51 -> {check(s.stack.size()==f.returnSlots(),"RET result height");compatible(s.stack,results(r,f.metadata().signature()),typed);}
case 0x52 -> {var target=m.functions().get(a).metadata();var expected=new ArrayList<Fact>();for(var t:target.captures())expected.addAll(facts(r,t));consume(s,expected,typed);produce(s,facts(r,type(r,b).identity()),i.pc());}
case 0x53 -> {consume(s,facts(r,type(r,a).identity()),typed);var signature=fn(r,a).signature();consume(s,parameters(r,signature),typed);produce(s,results(r,signature),i.pc());}
case 0x57 -> produce(s,facts(r,f.metadata().captures().get(a)),i.pc());
case 0x58 -> {consume(s,facts(r,type(r,a).identity()),typed);var signature=method(r,a,b).signature();consume(s,parameters(r,signature),typed);produce(s,results(r,signature),i.pc());}
case 0x71 -> {var decl=m.syscalls().get(a);pop(s,decl.argSlots());for(int n=0;n<decl.retSlots();n++)s.stack.add(Fact.UNKNOWN);}
case 0x72 -> {
var definition = IRVMIntrinsicRegistry.requireById(a);
consume(s, intrinsicFacts(definition.argumentCarriers()), typed);
produce(s, intrinsicFacts(definition.resultCarriers()), i.pc());
}
case 0x90 -> {
consume(s,facts(r,type(r,a).identity()),typed);
s.stack.add(new Fact("i64|null",-1,0,2,(long)i.pc(),null));
s.stack.add(new Fact("?",-1,1,2,(long)i.pc(),null));
}
case 0x91 -> {dynamic(pop(s,2),typed);produce(s,facts(r,type(r,a).identity()),i.pc());}
case 0x92 -> {dynamic(pop(s,2),typed);s.stack.add(carrier("bool"));}
case 0x93,0x94,0x95,0x96 -> {consume(s,facts(r,type(r,a).identity()),typed);if(op==0x93||op==0x94)s.stack.add(carrier("bool"));else produce(s,facts(r,type(r,b).identity()),i.pc());}
case 0xa0 -> produce(s,facts(r,type(r,a).identity()),i.pc());
case 0xa1 -> {consume(s,facts(r,type(r,a).identity()),typed);produce(s,facts(r,field(r,a,b).type()),i.pc());}
case 0xa2 -> {consume(s,facts(r,field(r,a,b).type()),typed);consume(s,facts(r,type(r,a).identity()),typed);}
case 0xa3 -> {consume(s,List.of(carrier("i64")),typed);produce(s,facts(r,type(r,a).identity()),i.pc());}
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 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 -> {
var value=pop(s,1).getFirst();
if(typed)check(value.carrier().equals("?")||numeric(value.carrier()),"numeric operand required");
s.stack.add(carrier(value.carrier()));
}
default -> {
var operands=pop(s,2);String left=operands.get(0).carrier(),right=operands.get(1).carrier();
boolean comparison=op>=0x30&&op<=0x33||op==0x3c||op==0x3d;
boolean unknown=left.equals("?")||right.equals("?")||left.contains("|")||right.contains("|");
boolean stringAdd=op==0x20&&(left.equals("string")||right.equals("string"));
if(typed&&!unknown&&op!=0x30&&op!=0x31) {
if(comparison)check(numeric(left)&&numeric(right)||left.equals(right)&&(left.equals("string")||left.equals("bool")),"comparison carrier mismatch");
else if(!stringAdd)check(numeric(left)&&numeric(right)&&(op<0x37||!left.equals("f64")&&!right.equals("f64")),"numeric operand mismatch");
}
String result="?";
if(stringAdd)result="string";
else if(numeric(left)&&numeric(right))result=left.equals("f64")||right.equals("f64")?"f64":left.equals("i64")||right.equals("i64")?"i64":"i32";
s.stack.add(carrier(comparison?"bool":result));
}
lastOpcode = opcode;
pc += size;
}
if (!TERMINATORS.contains(lastOpcode)) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNTERMINATED_FUNCTION,
"function ends without terminator opcode");
}
check(s.stack.size()<=f.maxStackSlots(),"maximum temporary slots exceeded (zero is literal)");
}
// Mirrors runtime registry_verifier/types.rs::abi_mask. Intrinsic String excludes a
// proven null at load time even though builtins.rs::value_matches_abi_type accepts it.
// Reference/session identity is still validated by the runtime, not proved by these carrier facts.
private static List<Fact> intrinsicFacts(List<Carrier> carriers) {
return carriers.stream().map(value -> carrier(switch (value) {
case INT32 -> "i32";
case INT64 -> "i64";
case FLOAT -> "f64";
case BOOL -> "bool";
case STRING -> "string!";
case COLOR -> "color";
case RANDOM -> "ref|null";
})).toList();
}
private int sizeForOpcode(
final int opcode,
final int pc) {
try {
return BytecodeOpcodeLayout.sizeForOpcode(opcode);
} catch (BytecodeMarshalingException ignored) {
throw new BytecodeMarshalingException(
BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNKNOWN_OPCODE,
"unknown opcode 0x%04X at pc=%d".formatted(opcode, pc));
private static boolean numeric(String carrier){return carrier.equals("i32")||carrier.equals("i64")||carrier.equals("f64")||carrier.equals("color");}
private static Fact carrier(String name){return new Fact(name,null,0,1);}
private static List<Fact> pop(State s,int n){check(n>=0&&n<=s.stack.size(),"temporary stack underflow");var result=new ArrayList<>(s.stack.subList(s.stack.size()-n,s.stack.size()));s.stack.subList(s.stack.size()-n,s.stack.size()).clear();return result;}
private static void consume(State s,List<Fact> expected,boolean typed){compatible(pop(s,expected.size()),expected,typed);}
private static Fact stamp(Fact fact,long origin) {
return new Fact(fact.carrier(),fact.type(),fact.component(),fact.width(),origin,fact.integer());
}
private static Fact integer(long value){return new Fact("i64",null,0,1,null,value);}
private static void produce(State state,List<Fact> values,int pc) {
for(int n=0;n<values.size();n++)state.stack.add(stamp(values.get(n),((long)pc<<32)|(n-values.get(n).component())));
}
private static int mask(String carrier) {
int result=0;
for(String part:carrier.split("\\|"))result|=switch(part) {
case "i32"->1;case "i64","color"->2;case "f64"->4;case "bool"->8;case "string!"->16;case "string"->16|64;case "ref"->32|64;case "null"->64;default->127;
};
return result;
}
private static void dynamic(List<Fact> values,boolean typed) {
if(!typed)return;
check((mask(values.get(0).carrier())&(2|64))!=0,"Dynamic tag carrier");
if(values.get(0).equals(Fact.NULL))check((mask(values.get(1).carrier())&64)!=0,"invalid null Dynamic pair");
}
private static void compatible(List<Fact> actual,List<Fact> expected,boolean typed) {
if(!typed)return;
check(actual.size()==expected.size(),"group height");
for(int i=0;i<actual.size();i++) {
var a=actual.get(i);var e=expected.get(i);
if(e.component()==0&&e.type()!=null&&a.type()!=null&&a.origin()!=null&&a.component()==0&&a.width()==e.width()) {
boolean complete=true;
for(int n=0;n<e.width();n++) {
var member=actual.get(i+n);
complete&=Objects.equals(member.type(),a.type())&&Objects.equals(member.origin(),a.origin())&&member.component()==n;
}
// A full proven semantic group cannot silently become another type.
if(complete) {
if(a.type()==-1)check(e.carrier().equals("i64|null")&&expected.get(i+1).carrier().equals("?"),"Dynamic semantic group mismatch");
else check(a.type().equals(e.type()),"proven semantic group mismatch");
}
}
check((mask(a.carrier())&mask(e.carrier()))!=0,"proven carrier mismatch");
if(e.carrier().equals("color")&&a.integer()!=null)check(a.integer()>=0&&a.integer()<=0xffffffffL,"color range");
if(e.component()==0&&e.width()==2&&e.carrier().contains("|" )&&a.equals(Fact.NULL))
check((mask(actual.get(i+1).carrier())&64)!=0,"invalid null group pair");
}
}
private int readU16(final byte[] code, final int offset) {
return ByteBuffer.wrap(code, offset, 2).order(ByteOrder.LITTLE_ENDIAN).getShort() & 0xFFFF;
}
private static TypeDeclaration type(LinkedRegistry r,int id){check(id>=0&&id<r.types().size(),"TypeId");return r.types().get(id);}
private static Fn fn(LinkedRegistry r,int id){check(type(r,id).body() instanceof Fn,"Fn category");return (Fn)type(r,id).body();}
private static Method method(LinkedRegistry r,int id,int member){check(type(r,id).body() instanceof Trait,"Trait category");var t=(Trait)type(r,id).body();check(member>=0&&member<t.methods().size(),"method index");return t.methods().get(member);}
private static NamedType field(LinkedRegistry r,int id,int member){check(type(r,id).body() instanceof Struct,"Struct category");var t=(Struct)type(r,id).body();check(member>=0&&member<t.fields().size(),"field index");return t.fields().get(member);}
private static Array array(LinkedRegistry r,int id){check(type(r,id).body() instanceof Array,"Array category");return (Array)type(r,id).body();}
private static int u16(byte[] code,int offset){return ByteBuffer.wrap(code,offset,2).order(ByteOrder.LITTLE_ENDIAN).getShort()&65535;}
private static int i32(byte[] code,int offset){return ByteBuffer.wrap(code,offset,4).order(ByteOrder.LITTLE_ENDIAN).getInt();}
private static void check(boolean ok,String message){if(!ok)throw new IllegalArgumentException(message);}
}
@@ -0,0 +1,154 @@
package p.studio.compiler.backend.bytecode;
import p.studio.compiler.backend.registry.LinkedRegistry;
import p.studio.compiler.models.registry.IRRegistry.*;
import p.studio.utilities.structures.ReadOnlyList;
import java.io.ByteArrayOutputStream;
import java.nio.*;
import java.nio.charset.*;
import java.util.*;
/** Current PBX v0 field encoding. Never serialize a native layout or infer a semantic type. */
final class PbxRegistryWriter {
private final BytecodeModule module;
private final LinkedRegistry registry;
private final Set<Role> roles = EnumSet.noneOf(Role.class);
private long nested, names;
private PbxRegistryWriter(BytecodeModule module) {
this.module=module;
this.registry=LinkedRegistry.link(module.registry());
}
static byte[] write(BytecodeModule module) {
try { return new PbxRegistryWriter(module).write(); }
catch (IllegalArgumentException | ArithmeticException error) {
throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_FORMAT_REGISTRY_INVALID, error.getMessage());
}
}
private byte[] write() {
require(module.version()==0,"PBX version must be zero");
require(module.code().length<=32*1024*1024,"Code limit");
require(module.functions().size()<=65_536,"function limit");
require(module.registry().implementations().size()<=262_144,"implementation limit");
var sections=new TreeMap<Integer,byte[]>();
sections.put(0,constants()); sections.put(1,functions()); sections.put(2,module.code());
if(module.debugInfo()!=null) sections.put(3,debug());
sections.put(4,exports()); sections.put(5,syscalls()); sections.put(6,types());
sections.put(7,implementations()); sections.put(8,captures());
require(nested<=1_048_576,"nested record limit"); require(names<=4*1024*1024,"Registry name limit");
long total=32+12L*sections.size(); for(var bytes:sections.values()) total+=bytes.length;
require(total<=64*1024*1024,"PBX file limit");
var out=new Bytes();out.raw(new byte[]{'P','B','X',0});out.u16(0);out.u8(0);out.u8(0);out.u32(sections.size());out.raw(new byte[20]);
int offset=32+12*sections.size();
for(var section:sections.entrySet()) {out.u32(section.getKey());out.u32(offset);out.u32(section.getValue().length);offset+=section.getValue().length;}
sections.values().forEach(out::raw);return out.bytes();
}
private void optionalName(Bytes out,String name) {
names+=LinkedRegistry.nameBytes(name);out.u8(name==null?0:1);if(name!=null)out.text32(name);
}
private void parameters(Bytes out,ReadOnlyList<NamedType> values) {
nested+=values.size();out.u32(values.size());for(var p:values) {out.u32(registry.id(p.type()).index());optionalName(out,p.name());}
}
private void results(Bytes out,ReadOnlyList<TypeRef> values) {
nested+=values.size();out.u32(values.size());for(var r:values)out.u32(registry.id(r).index());
}
private void signature(Bytes out,Signature signature) {
require(registry.parameterSlots(signature)<=16_384,"signature parameter limit");
require(registry.resultSlots(signature)<=1024,"signature result limit");
parameters(out,signature.parameters());results(out,signature.results());
}
private byte[] types() {
var out=new Bytes();out.u32(registry.types().size());
for(var t:registry.types()) {
int tag=switch(t.body()){case Builtin b->0;case Struct s->1;case Array a->2;case Trait t1->3;case Fn f->4;};
out.u8(tag);optionalName(out,t.name());
switch(t.body()) {
case Builtin b -> out.u32(b.id());
case Struct s -> parameters(out,s.fields());
case Array a -> out.u32(registry.id(a.element()).index());
case Fn f -> signature(out,f.signature());
case Trait t1 -> {nested+=t1.methods().size();out.u32(t1.methods().size());for(var m:t1.methods()){optionalName(out,m.name());signature(out,m.signature());}}
}
}
return out.bytes();
}
private byte[] functions() {
var out=new Bytes();out.u32(module.functions().size());long previousEnd=0;
for(int i=0;i<module.functions().size();i++) {
var f=module.functions().get(i);var m=f.metadata();require(m!=null,"typed function metadata required");
int p=registry.parameterSlots(m.signature()),r=registry.resultSlots(m.signature());
require(p==f.paramSlots()&&r==f.returnSlots(),"slot cache differs from typed signature");
require(f.localSlots()>=0&&f.maxStackSlots()>=0&&(long)p+f.localSlots()+f.maxStackSlots()<=16_384&&r<=1024,"frame/result limit");
long end=(long)f.codeOffset()+f.codeLen();
require(f.codeOffset()>=previousEnd&&f.codeLen()>0&&end<=module.code().length,"invalid function Code range");previousEnd=end;
if(i==0)require(m.captures().isEmpty(),"boot function must have no captures");
if(m.role()!=Role.NONE) {
require(roles.add(m.role()),"duplicate lifecycle role");require(i!=0&&m.captures().isEmpty(),"invalid lifecycle target");
if(m.role()==Role.MEMCARD_BOOT) require(m.signature().parameters().size()==1&&m.signature().results().size()==1
&&isBuiltin(m.signature().parameters().getFirst().type(),0x11)&&isBuiltin(m.signature().results().getFirst(),0x10),"MemcardBoot signature");
else require(p==0&&r==0,"pause/resume signature");
}
out.u8(m.role().wire());optionalName(out,m.name());signature(out,m.signature());
out.u32(f.codeOffset());out.u32(f.codeLen());out.u32(f.localSlots());out.u32(f.maxStackSlots());
}
return out.bytes();
}
private boolean isBuiltin(TypeRef type,int expected) {return registry.type(type).body() instanceof Builtin b&&b.id()==expected;}
private byte[] captures(){var out=new Bytes();out.u32(module.functions().size());for(var f:module.functions())results(out,f.metadata().captures());return out.bytes();}
private byte[] implementations() {
var out=new Bytes();var declarations=new TreeMap<String,Implementation>();
for(var impl:module.registry().implementations()) {
var old=declarations.putIfAbsent(impl.declarationKey(),impl);require(old==null||old.equals(impl),"conflicting implementation declaration");
}
out.u32(declarations.size());var pairs=new HashSet<List<Integer>>();
for(var impl:declarations.values()) {
require(registry.type(impl.owner()).body() instanceof Struct,"implementation owner must be Struct");
require(registry.type(impl.trait()).body() instanceof Trait,"implementation trait category");
var trait=(Trait)registry.type(impl.trait()).body();int owner=registry.id(impl.owner()).index(),traitId=registry.id(impl.trait()).index();
require(pairs.add(List.of(owner,traitId)),"duplicate implementation pair");
require(trait.methods().size()==impl.methods().size(),"implementation method count");
out.u32(owner);out.u32(traitId);out.u32(impl.methods().size());nested+=impl.methods().size();
for(int i=0;i<impl.methods().size();i++) {
int function=impl.methods().get(i).getIndex();require(function<module.functions().size(),"implementation FunctionId");
var metadata=module.functions().get(function).metadata();var actual=metadata.signature();var expected=trait.methods().get(i).signature();
require(metadata.captures().isEmpty()&&!actual.parameters().isEmpty()&&registry.id(actual.parameters().getFirst().type()).index()==owner,"implementation receiver/captures");
require(registry.sameSignature(new Signature(actual.parameters().immutableSubList(1,actual.parameters().size()),actual.results()),expected),"implementation signature mismatch");out.u32(function);
}
}
return out.bytes();
}
private byte[] constants(){
var out=new Bytes();require(module.constPool().size()<=1_048_576,"constant count");out.u32(module.constPool().size());long strings=0;
for(var c:module.constPool())switch(c){
case BytecodeModule.NullConstant n->out.u8(0);
case BytecodeModule.Int64Constant n->{out.u8(1);out.i64(n.value());}
case BytecodeModule.Float64Constant n->{require(Double.isFinite(n.value()),"nonfinite Float constant");out.u8(2);out.i64(Double.doubleToRawLongBits(n.value()));}
case BytecodeModule.BooleanConstant n->{out.u8(3);out.u8(n.value()?1:0);}
case BytecodeModule.StringConstant n->{var bytes=Bytes.utf8(n.value());require(bytes.length<=4*1024*1024,"individual string limit");strings+=bytes.length;require(strings<=64*1024*1024,"constant string limit");out.u8(4);out.u32(bytes.length);out.raw(bytes);}
case BytecodeModule.Int32Constant n->{out.u8(5);out.i32(n.value());}
}return out.bytes();
}
private byte[] exports(){var out=new Bytes();out.u32(module.exports().size());for(var e:module.exports()){require(e.funcIdx()>=0&&e.funcIdx()<module.functions().size(),"export FunctionId");out.u32(e.funcIdx());out.text32(e.symbol());}return out.bytes();}
private byte[] debug(){
var out=new Bytes();var d=module.debugInfo();out.u32(d.pcToSpan().size());
for(var p:d.pcToSpan()){require(p.pc()>=0&&p.pc()<module.code().length&&p.span().start()>=0&&p.span().end()>=p.span().start(),"debug span range");out.u32(p.pc());out.i32(p.span().fileId());out.u32(p.span().start());out.u32(p.span().end());}
out.u32(d.functionNames().size());for(var n:d.functionNames()){require(n.funcIdx()>=0&&n.funcIdx()<module.functions().size(),"debug FunctionId");out.u32(n.funcIdx());out.text32(n.name());}return out.bytes();
}
private byte[] syscalls(){var out=new Bytes();var identities=new HashSet<List<Object>>();out.u32(module.syscalls().size());
for(var s:module.syscalls()){require(identities.add(List.of(s.module(),s.name(),s.version())),"duplicate syscall declaration");if(Bytes.utf8(s.module()).length>65535)throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_FORMAT_SYSC_MODULE_TOO_LONG,"syscall module name exceeds u16");
if(Bytes.utf8(s.name()).length>65535)throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_FORMAT_SYSC_NAME_TOO_LONG,"syscall name exceeds u16");
out.text16(s.module());out.text16(s.name());out.u16(s.version());out.u16(s.argSlots());out.u16(s.retSlots());}return out.bytes();}
static void require(boolean condition,String message){if(!condition)throw new IllegalArgumentException(message);}
private static final class Bytes {
final ByteArrayOutputStream out=new ByteArrayOutputStream();
void u8(int v){require(v>=0&&v<=255,"u8 range");out.write(v);}
void u16(int v){require(v>=0&&v<=65535,"u16 range");out.write(v);out.write(v>>>8);}
void u32(int v){require(v>=0,"u32 range/product limit");i32(v);}
void i32(int v){for(int i=0;i<4;i++)out.write(v>>>(8*i));}
void i64(long v){for(int i=0;i<8;i++)out.write((int)(v>>>(8*i)));}
void raw(byte[] v){out.writeBytes(v);}
void text32(String v){var b=utf8(v);u32(b.length);raw(b);}
void text16(String v){var b=utf8(v);u16(b.length);raw(b);}
byte[] bytes(){return out.toByteArray();}
static byte[] utf8(String text){try{var b=StandardCharsets.UTF_8.newEncoder().encode(CharBuffer.wrap(text));var bytes=new byte[b.remaining()];b.get(bytes);return bytes;}catch(CharacterCodingException e){throw new IllegalArgumentException("invalid UTF-8 source",e);}}
}
}
@@ -0,0 +1,74 @@
package p.studio.compiler.backend.irvm;
import p.studio.compiler.backend.bytecode.BytecodeModule;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.List;
import java.util.Optional;
/** Runtime constants are ordered value groups, independent of intrinsic identities. */
public enum IRVMBuiltinConstantDefinition {
COLOR_BLACK("color", "black", 1, Slot.integer(255)),
COLOR_BLUE("color", "blue", 1, Slot.integer(65535)),
COLOR_CYAN("color", "cyan", 1, Slot.integer(16777215)),
COLOR_GRAY("color", "gray", 1, Slot.integer(2155905279L)),
COLOR_GREEN("color", "green", 1, Slot.integer(16711935)),
COLOR_INDIGO("color", "indigo", 1, Slot.integer(1258324735)),
COLOR_MAGENTA("color", "magenta", 1, Slot.integer(4278255615L)),
COLOR_ORANGE("color", "orange", 1, Slot.integer(4286578943L)),
COLOR_RED("color", "red", 1, Slot.integer(4278190335L)),
COLOR_TRANSPARENT("color", "transparent", 1, Slot.integer(0)),
COLOR_WHITE("color", "white", 1, Slot.integer(4294967295L)),
COLOR_YELLOW("color", "yellow", 1, Slot.integer(4294902015L)),
MATHF_PI("mathf", "pi", 1, Slot.floating(0x400921FB54442D18L)),
MATHF_TAU("mathf", "tau", 1, Slot.floating(0x401921FB54442D18L)),
VEC2_ZERO("vec2", "zero", 1, Slot.floating(0), Slot.floating(0));
private final String target;
private final String constantName;
private final long version;
private final List<Slot> slots;
IRVMBuiltinConstantDefinition(String target, String name, long version, Slot... slots) {
this.target = target;
this.constantName = name;
this.version = version;
this.slots = List.of(slots);
}
public String target() { return target; }
public String constantName() { return constantName; }
public long version() { return version; }
public List<Slot> slots() { return slots; }
public static Optional<IRVMBuiltinConstantDefinition> resolve(String target, String name, long version) {
for (var definition : values()) {
if (definition.target.equals(target) && definition.constantName.equals(name) && definition.version == version) {
return Optional.of(definition);
}
}
return Optional.empty();
}
public ReadOnlyList<BytecodeModule.ConstantPoolEntry> materialize() {
return ReadOnlyList.wrap(slots.stream().map(Slot::materialize).toList());
}
public record Slot(IRVMIntrinsicDefinition.Carrier carrier, long bitsOrValue) {
public Slot {
if (carrier != IRVMIntrinsicDefinition.Carrier.INT64 && carrier != IRVMIntrinsicDefinition.Carrier.FLOAT) {
throw new IllegalArgumentException("unsupported canonical constant carrier: " + carrier);
}
if (carrier == IRVMIntrinsicDefinition.Carrier.FLOAT && !Double.isFinite(Double.longBitsToDouble(bitsOrValue))) {
throw new IllegalArgumentException("nonfinite canonical constant");
}
}
static Slot integer(long value) { return new Slot(IRVMIntrinsicDefinition.Carrier.INT64, value); }
static Slot floating(long bits) { return new Slot(IRVMIntrinsicDefinition.Carrier.FLOAT, bits); }
public BytecodeModule.ConstantPoolEntry materialize() {
return carrier == IRVMIntrinsicDefinition.Carrier.FLOAT
? new BytecodeModule.Float64Constant(Double.longBitsToDouble(bitsOrValue))
: new BytecodeModule.Int64Constant(bitsOrValue);
}
}
}
@@ -5,12 +5,23 @@ import p.studio.utilities.structures.ReadOnlyList;
import java.util.Objects;
public record IRVMFunction(
String name,
int paramSlots,
int localSlots,
int returnSlots,
int maxStackSlots,
ReadOnlyList<IRVMInstruction> instructions,
p.studio.compiler.models.registry.IRRegistry.FunctionMetadata metadata) {
public IRVMFunction(
String name,
int paramSlots,
int localSlots,
int returnSlots,
int maxStackSlots,
ReadOnlyList<IRVMInstruction> instructions) {
this(name, paramSlots, localSlots, returnSlots, maxStackSlots, instructions, paramSlots == 0 && returnSlots == 0 ? p.studio.compiler.models.registry.IRRegistry.FunctionMetadata.empty() : null);
}
public IRVMFunction {
Objects.requireNonNull(name, "name");
instructions = instructions == null ? ReadOnlyList.empty() : instructions;
@@ -3,10 +3,23 @@ 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 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());
}
@@ -2,49 +2,113 @@ package p.studio.compiler.backend.irvm;
import java.util.Objects;
enum IRVMIntrinsicDefinition {
VEC2_DOT("vec2.dot", 1, 0x1000, "vec2", "dot", 4, 1, "float|float|float|float", "float", true, false),
VEC2_LENGTH("vec2.length", 1, 0x1001, "vec2", "length", 2, 1, "float|float", "float", true, false),
COLOR_FROM_RAW("color.from_raw", 1, 0x1100, "color", "from_raw", 1, 1, "int", "builtin:color", true, false),
COLOR_RGB("color.rgb", 1, 0x1101, "color", "rgb", 3, 1, "int|int|int", "builtin:color", true, false),
COLOR_RGBA("color.rgba", 1, 0x1102, "color", "rgba", 4, 1, "int|int|int|int", "builtin:color", true, false),
COLOR_HTML_RGBA("color.html_rgba", 1, 0x1103, "color", "html_rgba", 1, 1, "str", "builtin:color", true, false),
COLOR_GRAY_SCALE("color.gray_scale", 1, 0x1104, "color", "gray_scale", 1, 1, "int", "builtin:color", true, false),
COLOR_HEX("color.hex", 1, 0x1105, "color", "hex", 1, 1, "builtin:color", "int", true, false),
COLOR_ALPHA("color.alpha", 1, 0x1106, "color", "alpha", 1, 1, "builtin:color", "int", true, false),
COLOR_BLACK("color.black", 1, 0x1120, "color", "black", 0, 1, "", "builtin:color", true, false),
COLOR_WHITE("color.white", 1, 0x1121, "color", "white", 0, 1, "", "builtin:color", true, false),
COLOR_RED("color.red", 1, 0x1122, "color", "red", 0, 1, "", "builtin:color", true, false),
COLOR_GREEN("color.green", 1, 0x1123, "color", "green", 0, 1, "", "builtin:color", true, false),
COLOR_BLUE("color.blue", 1, 0x1124, "color", "blue", 0, 1, "", "builtin:color", true, false),
COLOR_YELLOW("color.yellow", 1, 0x1125, "color", "yellow", 0, 1, "", "builtin:color", true, false),
COLOR_ORANGE("color.orange", 1, 0x1126, "color", "orange", 0, 1, "", "builtin:color", true, false),
COLOR_INDIGO("color.indigo", 1, 0x1127, "color", "indigo", 0, 1, "", "builtin:color", true, false),
COLOR_GRAY("color.gray", 1, 0x1128, "color", "gray", 0, 1, "", "builtin:color", true, false),
COLOR_CYAN("color.cyan", 1, 0x1129, "color", "cyan", 0, 1, "", "builtin:color", true, false),
COLOR_MAGENTA("color.magenta", 1, 0x112A, "color", "magenta", 0, 1, "", "builtin:color", true, false),
COLOR_TRANSPARENT("color.transparent", 1, 0x112B, "color", "transparent", 0, 1, "", "builtin:color", true, false),
INPUT_PAD("input.pad", 1, 0x2000, "input", "pad", 0, 1, "", "builtin:input.pad", true, false),
INPUT_TOUCH("input.touch", 1, 0x2001, "input", "touch", 0, 1, "", "builtin:input.touch", true, false),
INPUT_PAD_UP("input.pad.up", 1, 0x2010, "input.pad", "up", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_DOWN("input.pad.down", 1, 0x2011, "input.pad", "down", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_LEFT("input.pad.left", 1, 0x2012, "input.pad", "left", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_RIGHT("input.pad.right", 1, 0x2013, "input.pad", "right", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_A("input.pad.a", 1, 0x2014, "input.pad", "a", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_B("input.pad.b", 1, 0x2015, "input.pad", "b", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_X("input.pad.x", 1, 0x2016, "input.pad", "x", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_Y("input.pad.y", 1, 0x2017, "input.pad", "y", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_L("input.pad.l", 1, 0x2018, "input.pad", "l", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_R("input.pad.r", 1, 0x2019, "input.pad", "r", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_START("input.pad.start", 1, 0x201A, "input.pad", "start", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_PAD_SELECT("input.pad.select", 1, 0x201B, "input.pad", "select", 1, 1, "builtin:input.pad", "builtin:input.button", true, false),
INPUT_TOUCH_BUTTON("input.touch.button", 1, 0x2020, "input.touch", "button", 1, 1, "builtin:input.touch", "builtin:input.button", true, false),
INPUT_TOUCH_X("input.touch.x", 1, 0x2021, "input.touch", "x", 1, 1, "builtin:input.touch", "int", true, false),
INPUT_TOUCH_Y("input.touch.y", 1, 0x2022, "input.touch", "y", 1, 1, "builtin:input.touch", "int", true, false),
INPUT_BUTTON_PRESSED("input.button.pressed", 1, 0x2030, "input.button", "pressed", 1, 1, "builtin:input.button", "bool", true, false),
INPUT_BUTTON_RELEASED("input.button.released", 1, 0x2031, "input.button", "released", 1, 1, "builtin:input.button", "bool", true, false),
INPUT_BUTTON_DOWN("input.button.down", 1, 0x2032, "input.button", "down", 1, 1, "builtin:input.button", "bool", true, false),
INPUT_BUTTON_HOLD("input.button.hold", 1, 0x2033, "input.button", "hold", 1, 1, "builtin:input.button", "int", true, false);
/** Manually maintained target contract. No runtime or CSV lookup occurs during compilation. */
public enum IRVMIntrinsicDefinition {
VEC2_DOT("vec2.dot", 1, 0x1000, "vec2", "dot", 4, 1, "Float|Float|Float|Float", "Float", true, false, "ax|ay|bx|by", 10, false, CostModel.FIXED, 0, 0),
VEC2_LENGTH("vec2.length", 1, 0x1001, "vec2", "length", 2, 1, "Float|Float", "Float", true, false, "x|y", 40, false, CostModel.FIXED, 0, 0),
COLOR_FROM_RAW("color.from_raw", 1, 0x1100, "color", "from_raw", 1, 1, "Int64", "builtin:color", true, false, "raw", 1, false, CostModel.FIXED, 0, 0),
COLOR_RGB("color.rgb", 1, 0x1101, "color", "rgb", 3, 1, "Int64|Int64|Int64", "builtin:color", true, false, "r|g|b", 1, false, CostModel.FIXED, 0, 0),
COLOR_RGBA("color.rgba", 1, 0x1102, "color", "rgba", 4, 1, "Int64|Int64|Int64|Int64", "builtin:color", true, false, "r|g|b|a", 1, false, CostModel.FIXED, 0, 0),
COLOR_HTML_RGBA("color.html_rgba", 1, 0x1103, "color", "html_rgba", 1, 1, "String", "builtin:color", true, false, "value", 1, false, CostModel.FIXED, 0, 0),
COLOR_GRAY_SCALE("color.gray_scale", 1, 0x1104, "color", "gray_scale", 1, 1, "Int64", "builtin:color", true, false, "value", 1, false, CostModel.FIXED, 0, 0),
COLOR_HEX("color.hex", 1, 0x1105, "color", "hex", 1, 1, "builtin:color", "Int64", true, false, "color", 1, false, CostModel.FIXED, 0, 0),
COLOR_ALPHA("color.alpha", 1, 0x1106, "color", "alpha", 1, 1, "builtin:color", "Int64", true, false, "color", 1, false, CostModel.FIXED, 0, 0),
COLOR_BLACK("color.black", 1, 0x1120, "color", "black", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_WHITE("color.white", 1, 0x1121, "color", "white", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_RED("color.red", 1, 0x1122, "color", "red", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_GREEN("color.green", 1, 0x1123, "color", "green", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_BLUE("color.blue", 1, 0x1124, "color", "blue", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_YELLOW("color.yellow", 1, 0x1125, "color", "yellow", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_ORANGE("color.orange", 1, 0x1126, "color", "orange", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_INDIGO("color.indigo", 1, 0x1127, "color", "indigo", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_GRAY("color.gray", 1, 0x1128, "color", "gray", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_CYAN("color.cyan", 1, 0x1129, "color", "cyan", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_MAGENTA("color.magenta", 1, 0x112A, "color", "magenta", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
COLOR_TRANSPARENT("color.transparent", 1, 0x112B, "color", "transparent", 0, 1, "", "builtin:color", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_UP_PRESSED("input.pad.up.pressed", 1, 0x2100, "input.pad.up", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_UP_RELEASED("input.pad.up.released", 1, 0x2101, "input.pad.up", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_UP_DOWN("input.pad.up.down", 1, 0x2102, "input.pad.up", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_UP_HOLD("input.pad.up.hold", 1, 0x2103, "input.pad.up", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_DOWN_PRESSED("input.pad.down.pressed", 1, 0x2104, "input.pad.down", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_DOWN_RELEASED("input.pad.down.released", 1, 0x2105, "input.pad.down", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_DOWN_DOWN("input.pad.down.down", 1, 0x2106, "input.pad.down", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_DOWN_HOLD("input.pad.down.hold", 1, 0x2107, "input.pad.down", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_LEFT_PRESSED("input.pad.left.pressed", 1, 0x2108, "input.pad.left", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_LEFT_RELEASED("input.pad.left.released", 1, 0x2109, "input.pad.left", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_LEFT_DOWN("input.pad.left.down", 1, 0x210A, "input.pad.left", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_LEFT_HOLD("input.pad.left.hold", 1, 0x210B, "input.pad.left", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_RIGHT_PRESSED("input.pad.right.pressed", 1, 0x210C, "input.pad.right", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_RIGHT_RELEASED("input.pad.right.released", 1, 0x210D, "input.pad.right", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_RIGHT_DOWN("input.pad.right.down", 1, 0x210E, "input.pad.right", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_RIGHT_HOLD("input.pad.right.hold", 1, 0x210F, "input.pad.right", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_A_PRESSED("input.pad.a.pressed", 1, 0x2110, "input.pad.a", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_A_RELEASED("input.pad.a.released", 1, 0x2111, "input.pad.a", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_A_DOWN("input.pad.a.down", 1, 0x2112, "input.pad.a", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_A_HOLD("input.pad.a.hold", 1, 0x2113, "input.pad.a", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_B_PRESSED("input.pad.b.pressed", 1, 0x2114, "input.pad.b", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_B_RELEASED("input.pad.b.released", 1, 0x2115, "input.pad.b", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_B_DOWN("input.pad.b.down", 1, 0x2116, "input.pad.b", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_B_HOLD("input.pad.b.hold", 1, 0x2117, "input.pad.b", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_X_PRESSED("input.pad.x.pressed", 1, 0x2118, "input.pad.x", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_X_RELEASED("input.pad.x.released", 1, 0x2119, "input.pad.x", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_X_DOWN("input.pad.x.down", 1, 0x211A, "input.pad.x", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_X_HOLD("input.pad.x.hold", 1, 0x211B, "input.pad.x", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_Y_PRESSED("input.pad.y.pressed", 1, 0x211C, "input.pad.y", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_Y_RELEASED("input.pad.y.released", 1, 0x211D, "input.pad.y", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_Y_DOWN("input.pad.y.down", 1, 0x211E, "input.pad.y", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_Y_HOLD("input.pad.y.hold", 1, 0x211F, "input.pad.y", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_L_PRESSED("input.pad.l.pressed", 1, 0x2120, "input.pad.l", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_L_RELEASED("input.pad.l.released", 1, 0x2121, "input.pad.l", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_L_DOWN("input.pad.l.down", 1, 0x2122, "input.pad.l", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_L_HOLD("input.pad.l.hold", 1, 0x2123, "input.pad.l", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_R_PRESSED("input.pad.r.pressed", 1, 0x2124, "input.pad.r", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_R_RELEASED("input.pad.r.released", 1, 0x2125, "input.pad.r", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_R_DOWN("input.pad.r.down", 1, 0x2126, "input.pad.r", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_R_HOLD("input.pad.r.hold", 1, 0x2127, "input.pad.r", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_START_PRESSED("input.pad.start.pressed", 1, 0x2128, "input.pad.start", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_START_RELEASED("input.pad.start.released", 1, 0x2129, "input.pad.start", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_START_DOWN("input.pad.start.down", 1, 0x212A, "input.pad.start", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_START_HOLD("input.pad.start.hold", 1, 0x212B, "input.pad.start", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_SELECT_PRESSED("input.pad.select.pressed", 1, 0x212C, "input.pad.select", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_SELECT_RELEASED("input.pad.select.released", 1, 0x212D, "input.pad.select", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_SELECT_DOWN("input.pad.select.down", 1, 0x212E, "input.pad.select", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_PAD_SELECT_HOLD("input.pad.select.hold", 1, 0x212F, "input.pad.select", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_TOUCH_BUTTON_PRESSED("input.touch.button.pressed", 1, 0x2130, "input.touch.button", "pressed", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_TOUCH_BUTTON_RELEASED("input.touch.button.released", 1, 0x2131, "input.touch.button", "released", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_TOUCH_BUTTON_DOWN("input.touch.button.down", 1, 0x2132, "input.touch.button", "down", 0, 1, "", "Bool", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_TOUCH_BUTTON_HOLD("input.touch.button.hold", 1, 0x2133, "input.touch.button", "hold", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_TOUCH_X("input.touch.x", 1, 0x2134, "input.touch", "x", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
INPUT_TOUCH_Y("input.touch.y", 1, 0x2135, "input.touch", "y", 0, 1, "", "Int64", true, false, "", 1, false, CostModel.FIXED, 0, 0),
MATH_I32_MIN("math.i32.min", 1, 0x3000, "math.i32", "min", 2, 1, "Int32|Int32", "Int32", true, false, "a|b", 2, false, CostModel.FIXED, 0, 0),
MATH_I32_MAX("math.i32.max", 1, 0x3001, "math.i32", "max", 2, 1, "Int32|Int32", "Int32", true, false, "a|b", 2, false, CostModel.FIXED, 0, 0),
MATH_I32_CLAMP("math.i32.clamp", 1, 0x3002, "math.i32", "clamp", 3, 1, "Int32|Int32|Int32", "Int32", true, false, "value|min|max", 4, false, CostModel.FIXED, 0, 0),
MATH_I32_ABS("math.i32.abs", 1, 0x3003, "math.i32", "abs", 1, 1, "Int32", "Int32", true, false, "value", 2, false, CostModel.FIXED, 0, 0),
MATH_I64_MIN("math.i64.min", 1, 0x3004, "math.i64", "min", 2, 1, "Int64|Int64", "Int64", true, false, "a|b", 2, false, CostModel.FIXED, 0, 0),
MATH_I64_MAX("math.i64.max", 1, 0x3005, "math.i64", "max", 2, 1, "Int64|Int64", "Int64", true, false, "a|b", 2, false, CostModel.FIXED, 0, 0),
MATH_I64_CLAMP("math.i64.clamp", 1, 0x3006, "math.i64", "clamp", 3, 1, "Int64|Int64|Int64", "Int64", true, false, "value|min|max", 4, false, CostModel.FIXED, 0, 0),
MATH_I64_ABS("math.i64.abs", 1, 0x3007, "math.i64", "abs", 1, 1, "Int64", "Int64", true, false, "value", 2, false, CostModel.FIXED, 0, 0),
MATHF_MIN("mathf.min", 1, 0x3008, "mathf", "min", 2, 1, "Float|Float", "Float", true, false, "a|b", 2, false, CostModel.FIXED, 0, 0),
MATHF_MAX("mathf.max", 1, 0x3009, "mathf", "max", 2, 1, "Float|Float", "Float", true, false, "a|b", 2, false, CostModel.FIXED, 0, 0),
MATHF_CLAMP("mathf.clamp", 1, 0x300A, "mathf", "clamp", 3, 1, "Float|Float|Float", "Float", true, false, "value|min|max", 4, false, CostModel.FIXED, 0, 0),
MATHF_ABS("mathf.abs", 1, 0x300B, "mathf", "abs", 1, 1, "Float", "Float", true, false, "value", 2, false, CostModel.FIXED, 0, 0),
MATHF_FLOOR("mathf.floor", 1, 0x300C, "mathf", "floor", 1, 1, "Float", "Float", true, false, "value", 4, false, CostModel.FIXED, 0, 0),
MATHF_CEIL("mathf.ceil", 1, 0x300D, "mathf", "ceil", 1, 1, "Float", "Float", true, false, "value", 4, false, CostModel.FIXED, 0, 0),
MATHF_TRUNC("mathf.trunc", 1, 0x300E, "mathf", "trunc", 1, 1, "Float", "Float", true, false, "value", 4, false, CostModel.FIXED, 0, 0),
MATHF_ROUND("mathf.round", 1, 0x300F, "mathf", "round", 1, 1, "Float", "Float", true, false, "value", 4, false, CostModel.FIXED, 0, 0),
MATHF_SQRT("mathf.sqrt", 1, 0x3010, "mathf", "sqrt", 1, 1, "Float", "Float", true, false, "value", 16, false, CostModel.FIXED, 0, 0),
MATHF_SIN("mathf.sin", 1, 0x3011, "mathf", "sin", 1, 1, "Float", "Float", true, false, "angle", 32, false, CostModel.FIXED, 0, 0),
MATHF_COS("mathf.cos", 1, 0x3012, "mathf", "cos", 1, 1, "Float", "Float", true, false, "angle", 32, false, CostModel.FIXED, 0, 0),
MATHF_ATAN2("mathf.atan2", 1, 0x3013, "mathf", "atan2", 2, 1, "Float|Float", "Float", true, false, "y|x", 48, false, CostModel.FIXED, 0, 0),
MATH_I32_TO_I64("math.i32.to_i64", 1, 0x3014, "math.i32", "to_i64", 1, 1, "Int32", "Int64", true, false, "value", 2, false, CostModel.FIXED, 0, 0),
MATH_I64_TO_I32("math.i64.to_i32", 1, 0x3015, "math.i64", "to_i32", 1, 1, "Int64", "Int32", true, false, "value", 2, false, CostModel.FIXED, 0, 0),
MATH_I32_TO_FLOAT("math.i32.to_float", 1, 0x3016, "math.i32", "to_float", 1, 1, "Int32", "Float", true, false, "value", 2, false, CostModel.FIXED, 0, 0),
MATH_I64_TO_FLOAT("math.i64.to_float", 1, 0x3017, "math.i64", "to_float", 1, 1, "Int64", "Float", true, false, "value", 2, false, CostModel.FIXED, 0, 0),
MATHF_TO_I32("mathf.to_i32", 1, 0x3018, "mathf", "to_i32", 1, 1, "Float", "Int32", true, false, "value", 2, false, CostModel.FIXED, 0, 0),
MATHF_TO_I64("mathf.to_i64", 1, 0x3019, "mathf", "to_i64", 1, 1, "Float", "Int64", true, false, "value", 2, false, CostModel.FIXED, 0, 0),
RANDOM_NEW("random.new", 1, 0x4000, "random", "new", 1, 1, "Int64", "builtin:Random", true, true, "seed", 32, true, CostModel.FIXED, 0, 0),
RANDOM_NEXT("random.next", 1, 0x4001, "random", "next", 1, 1, "builtin:Random", "Int64", true, false, "rng", 8, true, CostModel.FIXED, 0, 0),
RANDOM_NEXTF("random.nextf", 1, 0x4002, "random", "nextf", 1, 1, "builtin:Random", "Float", true, false, "rng", 12, true, CostModel.FIXED, 0, 0),
RANDOM_RANGE("random.range", 1, 0x4003, "random", "range", 3, 1, "builtin:Random|Int64|Int64", "Int64", true, false, "rng|min|max", 4, true, CostModel.RANDOM_REJECTION_V1, 8, 64);
private final String canonicalName;
private final long canonicalVersion;
@@ -57,6 +121,14 @@ enum IRVMIntrinsicDefinition {
private final String retLayout;
private final boolean deterministic;
private final boolean mayAllocate;
private final String parameterNames;
private final long costCycles;
private final boolean mutatesGuestState;
private final CostModel costModel;
private final long attemptCostCycles;
private final int maxAttempts;
private final java.util.List<Carrier> argumentCarriers;
private final java.util.List<Carrier> resultCarriers;
IRVMIntrinsicDefinition(
final String canonicalName,
@@ -69,7 +141,13 @@ enum IRVMIntrinsicDefinition {
final String argLayout,
final String retLayout,
final boolean deterministic,
final boolean mayAllocate) {
final boolean mayAllocate,
final String parameterNames,
final long costCycles,
final boolean mutatesGuestState,
final CostModel costModel,
final long attemptCostCycles,
final int maxAttempts) {
this.canonicalName = requireNonBlank(canonicalName, "canonicalName");
this.canonicalVersion = requireNonNegative(canonicalVersion, "canonicalVersion");
this.finalId = finalId;
@@ -81,56 +159,114 @@ enum IRVMIntrinsicDefinition {
this.retLayout = Objects.requireNonNull(retLayout, "retLayout");
this.deterministic = deterministic;
this.mayAllocate = mayAllocate;
this.parameterNames = Objects.requireNonNull(parameterNames, "parameterNames");
this.costCycles = requireNonNegative(costCycles, "costCycles");
this.mutatesGuestState = mutatesGuestState;
this.costModel = Objects.requireNonNull(costModel, "costModel");
this.attemptCostCycles = requireNonNegative(attemptCostCycles, "attemptCostCycles");
this.maxAttempts = requireNonNegative(maxAttempts, "maxAttempts");
this.argumentCarriers = parseLayout(argLayout);
this.resultCarriers = parseLayout(retLayout);
if (!canonicalName.equals(owner + "." + name)
|| argumentCarriers.size() != argSlots || resultCarriers.size() != retSlots
|| (parameterNames.isEmpty() ? 0 : parameterNames.split("\\|", -1).length) != argSlots) {
throw new IllegalArgumentException("incoherent intrinsic definition: " + canonicalName);
}
if (costModel == CostModel.FIXED && (attemptCostCycles != 0 || maxAttempts != 0)
|| costModel == CostModel.RANDOM_REJECTION_V1 && (attemptCostCycles == 0 || maxAttempts == 0)) {
throw new IllegalArgumentException("incoherent cost model: " + canonicalName);
}
}
String canonicalName() {
public String canonicalName() {
return canonicalName;
}
long canonicalVersion() {
public long canonicalVersion() {
return canonicalVersion;
}
int finalId() {
public int finalId() {
return finalId;
}
String owner() {
public String owner() {
return owner;
}
String intrinsicName() {
public String intrinsicName() {
return name;
}
int argSlots() {
public int argSlots() {
return argSlots;
}
int retSlots() {
public int retSlots() {
return retSlots;
}
String argLayout() {
public String argLayout() {
return argLayout;
}
String retLayout() {
public String retLayout() {
return retLayout;
}
boolean deterministic() {
public boolean deterministic() {
return deterministic;
}
boolean mayAllocate() {
public boolean mayAllocate() {
return mayAllocate;
}
String canonicalIdentity() {
public String canonicalIdentity() {
return canonicalName + "@" + canonicalVersion;
}
public String parameterNames() { return parameterNames; }
public long costCycles() { return costCycles; }
public boolean mutatesGuestState() { return mutatesGuestState; }
public CostModel costModel() { return costModel; }
public long attemptCostCycles() { return attemptCostCycles; }
public int maxAttempts() { return maxAttempts; }
public java.util.List<Carrier> argumentCarriers() { return argumentCarriers; }
public java.util.List<Carrier> resultCarriers() { return resultCarriers; }
public void validateSignature(Integer arguments, Integer results) {
if (arguments == null || results == null || arguments != argSlots || results != retSlots) {
throw new IllegalArgumentException("intrinsic ABI mismatch for " + canonicalIdentity()
+ ": provider=" + argSlots + "->" + retSlots + ", caller=" + arguments + "->" + results);
}
}
static java.util.List<Carrier> parseLayout(String layout) {
return layout.isEmpty() ? java.util.List.of()
: java.util.Arrays.stream(layout.split("\\|", -1)).map(Carrier::fromToken).toList();
}
public enum CostModel {
FIXED("fixed"), RANDOM_REJECTION_V1("random_rejection_v1");
private final String token;
CostModel(String token) { this.token = token; }
public String token() { return token; }
}
/** Explicit runtime ABI atoms, not source-language type aliases. */
public enum Carrier {
INT32("Int32"), INT64("Int64"), FLOAT("Float"), BOOL("Bool"), STRING("String"),
COLOR("builtin:color"), RANDOM("builtin:Random");
private final String token;
Carrier(String token) { this.token = token; }
public String token() { return token; }
public static Carrier fromToken(String token) {
for (var value : values()) if (value.token.equals(token)) return value;
throw new IllegalArgumentException("unsupported runtime ABI carrier: " + token);
}
}
private static String requireNonBlank(
final String value,
final String fieldName) {
@@ -3,13 +3,34 @@ package p.studio.compiler.backend.irvm;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalInt;
import java.util.Optional;
final class IRVMIntrinsicRegistry {
public final class IRVMIntrinsicRegistry {
private static final Map<IntrinsicKey, Integer> FINAL_ID_BY_INTRINSIC = loadRegistry();
private static final Map<Integer, IRVMIntrinsicDefinition> DEFINITION_BY_ID = definitionsById();
private IRVMIntrinsicRegistry() {
}
public static Optional<IRVMIntrinsicDefinition> resolve(String name, long version) {
var id = resolveFinalId(name, version);
return id.isPresent() ? Optional.of(requireById(id.getAsInt())) : Optional.empty();
}
public static IRVMIntrinsicDefinition requireById(int id) {
var definition = DEFINITION_BY_ID.get(id);
if (definition == null) {
throw new IllegalArgumentException("unknown runtime intrinsic ID: 0x%08X".formatted(id));
}
return definition;
}
private static Map<Integer, IRVMIntrinsicDefinition> definitionsById() {
var result = new HashMap<Integer, IRVMIntrinsicDefinition>();
for (var definition : IRVMIntrinsicDefinition.values()) result.put(definition.finalId(), definition);
return Map.copyOf(result);
}
static OptionalInt resolveFinalId(
final String canonicalName,
final long canonicalVersion) {
@@ -66,7 +87,7 @@ final class IRVMIntrinsicRegistry {
final long canonicalVersion,
final int finalId) {
final var previous = registry.put(new IntrinsicKey(canonicalName, canonicalVersion), finalId);
if (previous != null && previous != finalId) {
if (previous != null) {
throw new IllegalStateException("duplicate intrinsic registry identity: " + canonicalName + "@" + canonicalVersion);
}
}
@@ -3,8 +3,15 @@ package p.studio.compiler.backend.irvm;
import p.studio.utilities.structures.ReadOnlyList;
public record IRVMModule(
String vmProfile,
ReadOnlyList<IRVMFunction> functions,
p.studio.compiler.models.registry.IRRegistry registry) {
public IRVMModule(
String vmProfile,
ReadOnlyList<IRVMFunction> functions) {
this(vmProfile, functions, p.studio.compiler.models.registry.IRRegistry.empty());
}
public IRVMModule {
vmProfile = vmProfile == null || vmProfile.isBlank() ? "core-v1" : vmProfile;
functions = functions == null ? ReadOnlyList.empty() : functions;
@@ -91,6 +91,7 @@ public final class IRVMProfileFeatureGate {
final String profile,
final IRVMOp op) {
final var allowed = allowedByProfile.get(normalizeProfile(profile));
return allowed != null && allowed.contains(op);
return allowed != null && (allowed.contains(op) ||
op.name().startsWith("REGISTRY_") && java.util.Arrays.stream(p.studio.compiler.models.registry.IRRegistryInstruction.Kind.values()).anyMatch(k -> k.opcode() == op.opcode()));
}
}
@@ -54,13 +54,17 @@ public record IRVMProgram(
private void validateCoherence(
final IRVMModule module,
final BytecodeEmitter.EmissionPlan emissionPlan) {
if (!module.registry().equals(emissionPlan.registry())) {
throw new IllegalArgumentException("irvm/emission plan registry mismatch");
}
if (module.functions().size() != emissionPlan.functions().size()) {
throw new IllegalArgumentException("irvm module/function-plan size mismatch");
}
for (var fnIndex = 0; fnIndex < module.functions().size(); fnIndex++) {
final var irvmFunction = module.functions().get(fnIndex);
final var planFunction = emissionPlan.functions().get(fnIndex);
if (!irvmFunction.name().equals(planFunction.name())
if (!java.util.Objects.equals(irvmFunction.metadata(), planFunction.metadata())
|| !irvmFunction.name().equals(planFunction.name())
|| irvmFunction.paramSlots() != planFunction.paramSlots()
|| irvmFunction.localSlots() != planFunction.localSlots()
|| irvmFunction.returnSlots() != planFunction.returnSlots()
@@ -84,6 +88,7 @@ public record IRVMProgram(
private boolean coherentPair(
final IRVMInstruction instruction,
final BytecodeEmitter.Operation operation) {
if (operation.kind() == BytecodeEmitter.OperationKind.REGISTRY) return instruction.equals(operation.registryInstruction());
if (instruction.op().internal()) {
return true;
}
@@ -137,13 +142,13 @@ public record IRVMProgram(
function.localSlots(),
function.returnSlots(),
function.maxStackSlots(),
ReadOnlyList.wrap(operations)));
ReadOnlyList.wrap(operations), function.metadata()));
}
return new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.empty(),
ReadOnlyList.empty(),
ReadOnlyList.wrap(functions));
ReadOnlyList.wrap(functions), module.registry());
}
private boolean containsRawSyscall(final BytecodeEmitter.EmissionPlan plan) {
@@ -171,6 +176,7 @@ public record IRVMProgram(
}
private BytecodeEmitter.Operation deriveOperation(final IRVMInstruction instruction) {
if (instruction.op().name().startsWith("REGISTRY_")) return new BytecodeEmitter.Operation(BytecodeEmitter.OperationKind.REGISTRY, 0, null, null, null, null, instruction);
final var immediate = instruction.immediate() == null ? 0 : instruction.immediate();
return switch (instruction.op().opcode()) {
case 0x01 -> BytecodeEmitter.Operation.halt();
@@ -204,7 +210,10 @@ public record IRVMProgram(
case 0x03 -> BytecodeEmitter.Operation.jmpIfFalse(immediate, null);
case 0x71 -> throw new IllegalArgumentException(
"cannot derive HOSTCALL operation without canonical syscall metadata");
case 0x72 -> BytecodeEmitter.Operation.intrinsic(immediate);
case 0x72 -> {
var definition = IRVMIntrinsicRegistry.requireById(immediate);
yield BytecodeEmitter.Operation.intrinsic(immediate, definition.argSlots(), definition.retSlots(), null);
}
default -> throw new IllegalArgumentException("cannot derive emission op for irvm opcode: " + instruction.op().opcode());
};
}
@@ -240,13 +249,14 @@ public record IRVMProgram(
functionPlan.localSlots(),
functionPlan.returnSlots(),
functionPlan.maxStackSlots(),
ReadOnlyList.wrap(instructions)));
ReadOnlyList.wrap(instructions), functionPlan.metadata()));
}
return new IRVMModule("core-v1", ReadOnlyList.wrap(functions));
return new IRVMModule("core-v1", ReadOnlyList.wrap(functions), inputPlan.registry());
}
private static IRVMInstruction operationToInstruction(final BytecodeEmitter.Operation operation) {
return switch (operation.kind()) {
case REGISTRY -> operation.registryInstruction();
case HALT -> new IRVMInstruction(IRVMOp.HALT, null);
case RET -> new IRVMInstruction(IRVMOp.RET, null);
case PUSH_CONST -> new IRVMInstruction(IRVMOp.PUSH_CONST, operation.immediate());
@@ -9,6 +9,8 @@ public enum IRVMValidationErrorCode {
MARSHAL_VERIFY_PRECHECK_INVALID_FUNC_ID,
MARSHAL_VERIFY_PRECHECK_HOSTCALL_METADATA_MISSING,
MARSHAL_VERIFY_PRECHECK_INTRINSIC_SIGNATURE_MISSING,
MARSHAL_VERIFY_PRECHECK_INVALID_INTRINSIC_ID,
MARSHAL_VERIFY_PRECHECK_INTRINSIC_ABI_MISMATCH,
MARSHAL_VERIFY_PRECHECK_UNTERMINATED_PATH,
MARSHAL_VERIFY_PRECHECK_INTERNAL_OPCODE_RESIDUAL,
MARSHAL_VERIFY_PRECHECK_OPCODE_NOT_ALLOWED_FOR_PROFILE,
@@ -62,6 +62,29 @@ public class IRVMValidator {
final var instr = instructions.get(i);
pcByIndex[i] = pc;
indexByPc.put(pc, i);
if (instr.op() == IRVMOp.INTRINSIC) {
final IRVMIntrinsicDefinition definition;
try {
if (instr.immediate() == null) throw new IllegalArgumentException("missing intrinsic ID");
definition = IRVMIntrinsicRegistry.requireById(instr.immediate());
} catch (IllegalArgumentException error) {
throw new IRVMValidationException(IRVMValidationErrorCode.MARSHAL_VERIFY_PRECHECK_INVALID_INTRINSIC_ID,
error.getMessage(), functionIndex, pc);
}
if (functionPlan != null) {
var operation = functionPlan.operations().get(i);
if (operation.expectedArgSlots() == null || operation.expectedRetSlots() == null) {
throw new IRVMValidationException(IRVMValidationErrorCode.MARSHAL_VERIFY_PRECHECK_INTRINSIC_SIGNATURE_MISSING,
"intrinsic stack signature metadata is missing", functionIndex, pc);
}
try {
definition.validateSignature(operation.expectedArgSlots(), operation.expectedRetSlots());
} catch (IllegalArgumentException error) {
throw new IRVMValidationException(IRVMValidationErrorCode.MARSHAL_VERIFY_PRECHECK_INTRINSIC_ABI_MISMATCH,
error.getMessage(), functionIndex, pc);
}
}
}
pc += instr.encodedSize();
if (rejectInternalOpcodes && instr.op().internal()) {
throw new IRVMValidationException(
@@ -299,17 +322,8 @@ public class IRVMValidator {
}
if (instruction.op() == IRVMOp.INTRINSIC) {
if (operation == null || operation.kind() != BytecodeEmitter.OperationKind.INTRINSIC) {
return new StackEffect(instruction.op().pops(), instruction.op().pushes());
}
if (operation.expectedArgSlots() == null || operation.expectedRetSlots() == null) {
throw new IRVMValidationException(
IRVMValidationErrorCode.MARSHAL_VERIFY_PRECHECK_INTRINSIC_SIGNATURE_MISSING,
"intrinsic stack signature metadata is missing",
functionIndex,
pc);
}
return new StackEffect(operation.expectedArgSlots(), operation.expectedRetSlots());
var definition = IRVMIntrinsicRegistry.requireById(instruction.immediate());
return new StackEffect(definition.argSlots(), definition.retSlots());
}
return new StackEffect(instruction.op().pops(), instruction.op().pushes());
@@ -41,10 +41,12 @@ public class LowerToIRVMService {
}
validateCommonLifecycleStructure(backend);
final var registry = p.studio.compiler.backend.registry.LinkedRegistry.link(backend.getRegistry());
final var ordered = orderFunctions(backend);
final var funcIdByCallableId = new HashMap<CallableId, Integer>();
for (var i = 0; i < ordered.size(); i++) {
final var fn = ordered.get(i);
if (fn.metadata() == null) throw new IllegalArgumentException("typed function metadata required: " + fn.callableName());
if (funcIdByCallableId.putIfAbsent(fn.callableId(), i) != null) {
throw loweringError(
fn,
@@ -60,6 +62,9 @@ public class LowerToIRVMService {
for (var i = 0; i < ordered.size(); i++) {
final var fn = ordered.get(i);
if (fn.metadata() == null) throw new IllegalArgumentException("typed function metadata required: " + fn.callableName());
final var parameterSlots = registry.parameterSlots(fn.metadata().signature());
final var resultSlots = registry.resultSlots(fn.metadata().signature());
final var instructions = new ArrayList<IRVMInstruction>(fn.instructions().size());
final var operations = new ArrayList<BytecodeEmitter.Operation>(fn.instructions().size());
final var labelToPc = new HashMap<String, Integer>();
@@ -68,6 +73,12 @@ public class LowerToIRVMService {
for (final var instr : fn.instructions()) {
final var sourceSpan = toBytecodeSpan(fn.fileId().getId(), instr.span());
switch (instr.kind()) {
case REGISTRY -> {
var typed = p.studio.compiler.backend.registry.RegistryInstructionLowerer.lower(instr.registryInstruction(), registry, fn, ordered, funcIdByCallableId);
instructions.add(typed);
operations.add(new BytecodeEmitter.Operation(BytecodeEmitter.OperationKind.REGISTRY, 0, null, null, null, sourceSpan, typed));
functionPc += typed.encodedSize();
}
case PUSH_I32 -> {
final int value;
try {
@@ -83,6 +94,28 @@ public class LowerToIRVMService {
operations.add(BytecodeEmitter.Operation.pushI32(value, sourceSpan));
functionPc += IRVMOp.PUSH_I32.immediateSize() + 2;
}
case PUSH_I64, PUSH_F64, BUILTIN_CONST -> {
final java.util.List<BytecodeModule.ConstantPoolEntry> entries;
if (instr.kind() == IRBackendExecutableFunction.InstructionKind.BUILTIN_CONST) {
var key = instr.label().split("@", 2);
int dot = key[0].lastIndexOf('.');
entries = IRVMBuiltinConstantDefinition.resolve(key[0].substring(0, dot), key[0].substring(dot + 1), Long.parseLong(key[1]))
.orElseThrow(() -> new IllegalArgumentException("Unknown builtin constant: " + instr.label())).materialize().asList();
} else {
entries = java.util.List.of(instr.kind() == IRBackendExecutableFunction.InstructionKind.PUSH_I64
? new BytecodeModule.Int64Constant(Long.parseLong(instr.label()))
: new BytecodeModule.Float64Constant(Double.parseDouble(instr.label())));
}
if (instr.kind() == IRBackendExecutableFunction.InstructionKind.BUILTIN_CONST && entries.size() != instr.expectedRetSlots())
throw new IllegalArgumentException("Builtin constant group width differs from declared type: " + instr.label());
for (var value : entries) {
int index = constPool.indexOf(value);
if (index < 0) { index = constPool.size(); constPool.add(value); }
instructions.add(new IRVMInstruction(IRVMOp.PUSH_CONST, index));
operations.add(BytecodeEmitter.Operation.pushConst(index, sourceSpan));
functionPc += IRVMOp.PUSH_CONST.immediateSize() + 2;
}
}
case PUSH_CONST -> {
final var value = instr.label() == null ? "" : instr.label();
final var constPoolIndex = constPoolIndexByString.computeIfAbsent(value, ignored -> {
@@ -271,7 +304,7 @@ public class LowerToIRVMService {
}
final var calleeFunction = ordered.get(calleeId);
if (instr.expectedArgSlots() != null
&& instr.expectedArgSlots() != calleeFunction.paramSlots()) {
&& instr.expectedArgSlots() != registry.parameterSlots(calleeFunction.metadata().signature())) {
throw loweringError(
fn,
instr,
@@ -279,10 +312,10 @@ public class LowerToIRVMService {
"call arg_slots mismatch for %s. expected=%d actual=%d".formatted(
instr.calleeCallableName(),
instr.expectedArgSlots(),
calleeFunction.paramSlots()));
registry.parameterSlots(calleeFunction.metadata().signature())));
}
if (instr.expectedRetSlots() != null
&& instr.expectedRetSlots() != calleeFunction.returnSlots()) {
&& instr.expectedRetSlots() != registry.resultSlots(calleeFunction.metadata().signature())) {
throw loweringError(
fn,
instr,
@@ -290,7 +323,7 @@ public class LowerToIRVMService {
"call ret_slots mismatch for %s. expected=%d actual=%d".formatted(
instr.calleeCallableName(),
instr.expectedRetSlots(),
calleeFunction.returnSlots()));
registry.resultSlots(calleeFunction.metadata().signature())));
}
instructions.add(new IRVMInstruction(IRVMOp.CALL, calleeId));
operations.add(BytecodeEmitter.Operation.callFunc(calleeId, sourceSpan));
@@ -334,8 +367,8 @@ public class LowerToIRVMService {
canonicalIntrinsic.canonicalName(),
canonicalIntrinsic.canonicalVersion()));
}
final var finalIntrinsicId = IRVMIntrinsicRegistry
.resolveFinalId(canonicalIntrinsic.canonicalName(), canonicalIntrinsic.canonicalVersion())
final var definition = IRVMIntrinsicRegistry
.resolve(canonicalIntrinsic.canonicalName(), canonicalIntrinsic.canonicalVersion())
.orElseThrow(() -> loweringError(
fn,
instr,
@@ -343,11 +376,22 @@ public class LowerToIRVMService {
"unknown VM-owned intrinsic identity: %s@%d".formatted(
canonicalIntrinsic.canonicalName(),
canonicalIntrinsic.canonicalVersion())));
if (instr.expectedArgSlots() == null || instr.expectedArgSlots() != definition.argSlots()) {
throw loweringError(fn, instr, IRVMLoweringErrorCode.LOWER_IRVM_CALL_ARG_SLOTS_MISMATCH,
"intrinsic argument slots mismatch for " + definition.canonicalIdentity()
+ ": provider=" + definition.argSlots() + ", caller=" + instr.expectedArgSlots());
}
if (instr.expectedRetSlots() == null || instr.expectedRetSlots() != definition.retSlots()) {
throw loweringError(fn, instr, IRVMLoweringErrorCode.LOWER_IRVM_CALL_RET_SLOTS_MISMATCH,
"intrinsic result slots mismatch for " + definition.canonicalIdentity()
+ ": provider=" + definition.retSlots() + ", caller=" + instr.expectedRetSlots());
}
final var finalIntrinsicId = definition.finalId();
instructions.add(new IRVMInstruction(IRVMOp.INTRINSIC, finalIntrinsicId));
operations.add(BytecodeEmitter.Operation.intrinsic(
finalIntrinsicId,
instr.expectedArgSlots(),
instr.expectedRetSlots(),
definition.argSlots(),
definition.retSlots(),
sourceSpan));
functionPc += IRVMOp.INTRINSIC.immediateSize() + 2;
}
@@ -404,27 +448,38 @@ public class LowerToIRVMService {
}
loweredFunctions.add(new IRVMFunction(
fn.callableName(),
fn.paramSlots(),
parameterSlots,
fn.localSlots(),
fn.returnSlots(),
resultSlots,
fn.maxStackSlots(),
ReadOnlyList.wrap(instructions)));
ReadOnlyList.wrap(instructions), fn.metadata()));
emissionFunctions.add(new BytecodeEmitter.FunctionPlan(
fn.callableName(),
fn.paramSlots(),
parameterSlots,
fn.localSlots(),
fn.returnSlots(),
resultSlots,
fn.maxStackSlots(),
ReadOnlyList.wrap(operations)));
ReadOnlyList.wrap(operations), fn.metadata()));
}
final var imageImplementations = new ArrayList<p.studio.compiler.models.registry.IRRegistry.Implementation>();
for (var impl : backend.getRegistry().implementations()) {
var methods = new ArrayList<CallableId>();
for (var method : impl.methods()) {
var target = funcIdByCallableId.get(method);
if (target == null) throw new IllegalArgumentException("undefined implementation function: " + method);
methods.add(new CallableId(target));
}
imageImplementations.add(new p.studio.compiler.models.registry.IRRegistry.Implementation(impl.declarationKey(), impl.owner(), impl.trait(), ReadOnlyList.wrap(methods)));
}
var imageRegistry = new p.studio.compiler.models.registry.IRRegistry(backend.getRegistry().types(), ReadOnlyList.wrap(imageImplementations));
final var program = new IRVMProgram(
new IRVMModule(vmProfile, ReadOnlyList.wrap(loweredFunctions)),
new IRVMModule(vmProfile, ReadOnlyList.wrap(loweredFunctions), imageRegistry),
new BytecodeEmitter.EmissionPlan(
0,
ReadOnlyList.wrap(constPool),
ReadOnlyList.empty(),
ReadOnlyList.wrap(emissionFunctions)));
ReadOnlyList.wrap(emissionFunctions), imageRegistry));
validator.validate(program, false);
return program;
}
@@ -124,12 +124,12 @@ public class OptimizeIRVMService {
}
return new IRVMProgram(
new IRVMModule(input.module().vmProfile(), ReadOnlyList.wrap(rewrittenFunctions)),
new IRVMModule(input.module().vmProfile(), ReadOnlyList.wrap(rewrittenFunctions), input.module().registry()),
new BytecodeEmitter.EmissionPlan(
input.emissionPlan().version(),
input.emissionPlan().constPool(),
input.emissionPlan().exports(),
ReadOnlyList.wrap(rewrittenPlans)));
ReadOnlyList.wrap(rewrittenPlans), input.emissionPlan().registry()));
}
private static boolean isJump(final IRVMOp op) {
@@ -218,14 +218,14 @@ public class OptimizeIRVMService {
function.localSlots(),
function.returnSlots(),
function.maxStackSlots(),
ReadOnlyList.wrap(newInstructions));
ReadOnlyList.wrap(newInstructions), function.metadata());
final var rewrittenPlan = new BytecodeEmitter.FunctionPlan(
functionPlan.name(),
functionPlan.paramSlots(),
functionPlan.localSlots(),
functionPlan.returnSlots(),
functionPlan.maxStackSlots(),
ReadOnlyList.wrap(newOperations));
ReadOnlyList.wrap(newOperations), functionPlan.metadata());
return new FunctionRewrite(rewrittenFunction, rewrittenPlan, changed);
}
@@ -383,14 +383,14 @@ public class OptimizeIRVMService {
function.localSlots(),
function.returnSlots(),
function.maxStackSlots(),
ReadOnlyList.wrap(instructions)),
ReadOnlyList.wrap(instructions), function.metadata()),
new BytecodeEmitter.FunctionPlan(
functionPlan.name(),
functionPlan.paramSlots(),
functionPlan.localSlots(),
functionPlan.returnSlots(),
functionPlan.maxStackSlots(),
ReadOnlyList.wrap(operations)),
ReadOnlyList.wrap(operations), functionPlan.metadata()),
true);
}
@@ -0,0 +1,130 @@
package p.studio.compiler.backend.registry;
import p.studio.compiler.models.registry.IRRegistry;
import p.studio.compiler.models.registry.IRRegistry.*;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.*;
/** Immutable image type table and reference resolution. No language-specific type inference. */
public final class LinkedRegistry {
public record TypeId(int index) { public TypeId { if (index < 0) throw new IllegalArgumentException("negative TypeId"); } }
public record FunctionId(int index) { public FunctionId { if (index < 0) throw new IllegalArgumentException("negative FunctionId"); } }
public record ImplementationId(int index) { public ImplementationId { if (index < 0) throw new IllegalArgumentException("negative ImplementationId"); } }
private final ReadOnlyList<TypeDeclaration> types;
private final Map<TypeRef, TypeId> references;
private LinkedRegistry(List<TypeDeclaration> types, Map<TypeRef, TypeId> references) {
this.types = ReadOnlyList.wrap(types);
this.references = Map.copyOf(references);
}
public ReadOnlyList<TypeDeclaration> types() { return types; }
public TypeId id(TypeRef reference) {
final var id = references.get(reference);
if (id == null) throw new IllegalArgumentException("undefined type declaration: " + reference);
return id;
}
public TypeDeclaration type(TypeRef reference) { return types.get(id(reference).index()); }
public int width(TypeRef reference) {
return switch (type(reference).body()) {
case Builtin b -> switch (b.id()) { case 2, 0x15 -> 2; case 3 -> 3; default -> 1; };
case Trait ignored -> 2;
default -> 1;
};
}
public int parameterSlots(Signature signature) {
var slots = 0;
for (var p : signature.parameters()) slots = Math.addExact(slots, width(p.type()));
return slots;
}
public int resultSlots(Signature signature) {
var slots = 0;
for (var r : signature.results()) slots = Math.addExact(slots, width(r));
return slots;
}
public boolean sameSignature(Signature a, Signature b) {
if (a.parameters().size() != b.parameters().size() || a.results().size() != b.results().size()) return false;
for (int i=0; i<a.parameters().size(); i++) if (!id(a.parameters().get(i).type()).equals(id(b.parameters().get(i).type()))) return false;
for (int i=0; i<a.results().size(); i++) if (!id(a.results().get(i)).equals(id(b.results().get(i)))) return false;
return true;
}
public static LinkedRegistry link(IRRegistry source) {
final var declarations = new TreeMap<String, TypeDeclaration>();
for (var declaration : source.types()) {
final var old = declarations.putIfAbsent(declaration.identity().declarationKey(), declaration);
if (old != null && !old.equals(declaration)) throw new IllegalArgumentException("conflicting type declaration: " + declaration.identity());
}
if (declarations.size() > 65_536) throw new IllegalArgumentException("type record limit exceeded");
final var output = new ArrayList<TypeDeclaration>();
final var ids = new HashMap<TypeRef, TypeId>();
final var structuralKeys = new HashMap<Object, TypeId>();
final var remaining = new TreeMap<String, TypeDeclaration>();
long names = 0, nested = 0;
for (var d : declarations.values()) {
names += nameBytes(d.name());
for (var ref : references(d.body())) {
if (!declarations.containsKey(ref.declarationKey())) throw new IllegalArgumentException("undefined type: " + ref);
}
switch (d.body()) {
case Builtin b -> assign(d, List.of("builtin", b.id()), structuralKeys, output, ids);
case Struct s -> { assign(d, d.identity(), structuralKeys, output, ids); nested += s.fields().size(); for(var f:s.fields()) names += nameBytes(f.name()); }
case Trait t -> {
assign(d, d.identity(), structuralKeys, output, ids); nested += t.methods().size();
for(var m:t.methods()) { names += nameBytes(m.name()); nested += m.signature().parameters().size()+m.signature().results().size(); for(var p:m.signature().parameters()) names += nameBytes(p.name()); }
}
case Array ignored -> remaining.put(d.identity().declarationKey(), d);
case Fn f -> { remaining.put(d.identity().declarationKey(), d); nested += f.signature().parameters().size()+f.signature().results().size(); for(var p:f.signature().parameters()) names += nameBytes(p.name()); }
}
}
if (names > 4L*1024*1024 || nested > 1_048_576) throw new IllegalArgumentException("registry name/nested-record limit exceeded");
// Kahn traversal avoids recursive expansion and accepts nominal anchors.
final var pending = new HashMap<TypeRef, Integer>();
final var dependents = new HashMap<TypeRef, List<TypeRef>>();
final var ready = new PriorityQueue<TypeRef>(Comparator.comparing(TypeRef::declarationKey));
for(var d:remaining.values()) {
int count=0;
for(var r:references(d.body())) if(!ids.containsKey(r)) { count++; dependents.computeIfAbsent(r, ignored -> new ArrayList<>()).add(d.identity()); }
pending.put(d.identity(),count);
if(count==0) ready.add(d.identity());
}
while(!ready.isEmpty()) {
var reference=ready.remove(); var d=remaining.get(reference.declarationKey());
Object key;
if(d.body() instanceof Array a) key=List.of("array",ids.get(a.element()));
else {
var signature=((Fn)d.body()).signature();
key=List.of("fn",signature.parameters().stream().map(p->ids.get(p.type())).toList(),signature.results().stream().map(ids::get).toList());
}
assign(d,key,structuralKeys,output,ids);
for(var dependent:dependents.getOrDefault(reference,List.of())) if(pending.compute(dependent,(k,v)->v-1)==0) ready.add(dependent);
}
if(ids.size()!=declarations.size()) throw new IllegalArgumentException("structural Array/Fn cycle");
return new LinkedRegistry(output,ids);
}
private static void assign(TypeDeclaration d,Object key,Map<Object,TypeId> keys,List<TypeDeclaration> output,Map<TypeRef,TypeId> ids) {
var id=keys.get(key);
if(id==null) { id=new TypeId(output.size()); output.add(d); keys.put(key,id); }
ids.put(d.identity(),id);
}
private static List<TypeRef> references(TypeBody body) {
final var result=new ArrayList<TypeRef>();
switch(body) {
case Struct s -> s.fields().forEach(f->result.add(f.type()));
case Array a -> result.add(a.element());
case Fn f -> signatureReferences(f.signature(),result);
case Trait t -> t.methods().forEach(m->signatureReferences(m.signature(),result));
case Builtin ignored -> { }
}
return result;
}
private static void signatureReferences(Signature signature,List<TypeRef> result) {
signature.parameters().forEach(p->result.add(p.type())); result.addAll(signature.results().asList());
}
public static int nameBytes(String name) {
if(name==null) return 0;
try {
int size=java.nio.charset.StandardCharsets.UTF_8.newEncoder().encode(java.nio.CharBuffer.wrap(name)).remaining();
if(size>4096) throw new IllegalArgumentException("name exceeds 4096 UTF-8 bytes");
return size;
} catch(java.nio.charset.CharacterCodingException e) { throw new IllegalArgumentException("invalid UTF-8 source string",e); }
}
}
@@ -0,0 +1,62 @@
package p.studio.compiler.backend.registry;
import p.studio.compiler.models.IRBackendExecutableFunction;
import p.studio.compiler.models.registry.IRRegistry.*;
import p.studio.compiler.models.registry.IRRegistryInstruction;
import p.studio.compiler.backend.irvm.*;
import p.studio.compiler.source.identifiers.CallableId;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.Map;
public final class RegistryInstructionLowerer {
private 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;
var body=instruction.type()==null?null:registry.type(instruction.type()).body();
switch(instruction.kind()) {
case MAKE_CLOSURE -> {
if(!(body instanceof Fn type))throw new IllegalArgumentException("closure type must be Fn");
var target=functionIds.get(instruction.function());if(target==null)throw new IllegalArgumentException("undefined closure function");
var metadata=functions.get(target).metadata();
if(metadata==null||!registry.sameSignature(type.signature(),metadata.signature()))throw new IllegalArgumentException("closure signature mismatch");
for(var capture:metadata.captures())pops=Math.addExact(pops,registry.width(capture));
second=first;first=target;pushes=1;
}
case CALL_CLOSURE -> {
if(!(body instanceof Fn type))throw new IllegalArgumentException("call type must be Fn");
pops=registry.parameterSlots(type.signature())+1;pushes=registry.resultSlots(type.signature());
}
case GET_CAPTURE -> {if(instruction.member()>=current.metadata().captures().size())throw new IllegalArgumentException("capture index");pushes=registry.width(current.metadata().captures().get(instruction.member()));}
case CALL_TRAIT -> {
if(!(body instanceof Trait trait)||instruction.member()>=trait.methods().size())throw new IllegalArgumentException("trait/method operand");
var signature=trait.methods().get(instruction.member()).signature();second=instruction.member();pops=registry.parameterSlots(signature)+2;pushes=registry.resultSlots(signature);
}
case DYN_PACK -> {pops=registry.width(instruction.type());pushes=2;}
case DYN_UNPACK -> {pops=2;pushes=registry.width(instruction.type());}
case DYN_CAN_UNPACK -> {pops=2;pushes=1;}
case REF_IS_EXACT,REF_IMPLEMENTS,REF_TO_TRAIT,REF_CAST -> {
if(!(body instanceof Struct||body instanceof Array||body instanceof Trait))throw new IllegalArgumentException("reference source category");
var target=registry.type(instruction.targetType()).body();boolean trait=instruction.kind()==IRRegistryInstruction.Kind.REF_IMPLEMENTS||instruction.kind()==IRRegistryInstruction.Kind.REF_TO_TRAIT;
if(trait?!(target instanceof Trait):!(target instanceof Struct||target instanceof Array))throw new IllegalArgumentException("reference target category");
second=registry.id(instruction.targetType()).index();pops=registry.width(instruction.type());pushes=instruction.kind()==IRRegistryInstruction.Kind.REF_TO_TRAIT?2:1;
}
case STRUCT_NEW,STRUCT_GET,STRUCT_SET -> {
if(!(body instanceof Struct struct))throw new IllegalArgumentException("Struct category required");
if(instruction.kind()==IRRegistryInstruction.Kind.STRUCT_NEW)pushes=1;
else {
if(instruction.member()>=struct.fields().size())throw new IllegalArgumentException("field index");
second=instruction.member();int width=registry.width(struct.fields().get(instruction.member()).type());
pops=instruction.kind()==IRRegistryInstruction.Kind.STRUCT_SET?1+width:1;pushes=instruction.kind()==IRRegistryInstruction.Kind.STRUCT_GET?width:0;
}
}
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);
}
}
@@ -201,6 +201,7 @@ public final class LifecycleAssemblerService {
frameExecutable.span()));
return IRBackend.builder()
.registry(backend.getRegistry())
.entryPointCallableName(entryPointCallableName)
.entryPointModuleId(entryPointModuleId)
.functions(backend.getFunctions())
@@ -54,6 +54,10 @@ public class LowerToIRVMPipelineStage implements PipelineStage {
.pc(e.pc())
.message("[BUILD]: lower to irvm validation failed: " + e.getMessage())
.exception(e));
} catch (IllegalArgumentException e) {
return BuildingIssueSink.empty().report(builder -> builder.error(true)
.phase("BACKEND_LOWER_TO_IRVM").code("LOWER_IRVM_INVALID_TYPED_REGISTRY")
.message("[BUILD]: invalid common typed IR: " + e.getMessage()).exception(e));
}
return BuildingIssueSink.empty();
}
@@ -66,10 +66,11 @@ class GoldenArtifactsTest {
IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC,
"",
null,
new IRBackendExecutableFunction.IntrinsicCallMetadata("input.pad", 1, new IntrinsicId(0)),
0,
new IRBackendExecutableFunction.IntrinsicCallMetadata("input.pad.a.down", 1, new IntrinsicId(0)),
0,
1,
Span.none()),
new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.POP, "", null, null, Span.none()),
new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.RET,
"",
@@ -77,7 +78,7 @@ class GoldenArtifactsTest {
null,
Span.none())),
Span.none())))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad", 1)))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad.a.down", 1)))
.build();
}
@@ -106,12 +106,12 @@ class BytecodeEmitterTest {
0,
1,
ReadOnlyList.from(
BytecodeEmitter.Operation.intrinsic(0x2000),
BytecodeEmitter.Operation.intrinsic(0x2112, 0, 1, null),
BytecodeEmitter.Operation.halt())))));
assertTrue(module.syscalls().isEmpty());
assertEquals(0x72, readU16(module.code(), 0));
assertEquals(0x2000, readU32(module.code(), 2));
assertEquals(0x2112, readU32(module.code(), 2));
}
@Test
@@ -131,7 +131,7 @@ class BytecodeEmitterTest {
ReadOnlyList.from(
BytecodeEmitter.Operation.halt(new BytecodeModule.SourceSpan(1, 0, 1)),
BytecodeEmitter.Operation.callFunc(0, new BytecodeModule.SourceSpan(1, 2, 3)),
BytecodeEmitter.Operation.intrinsic(0x2000, new BytecodeModule.SourceSpan(1, 4, 5)),
BytecodeEmitter.Operation.intrinsic(0x2112, 0, 1, new BytecodeModule.SourceSpan(1, 4, 5)),
BytecodeEmitter.Operation.ret(new BytecodeModule.SourceSpan(1, 6, 7)))))));
assertEquals(4, module.debugInfo().pcToSpan().size());
@@ -22,13 +22,13 @@ class BytecodeModuleTest {
assertEquals(0, bytes[3]);
final var sectionCount = readU32(bytes, 8);
assertEquals(1, sectionCount);
assertEquals(8, sectionCount);
final var sectionKind = readU32(bytes, 32);
final var sectionOffset = readU32(bytes, 36);
final var sectionLength = readU32(bytes, 40);
final var sectionKind = readU32(bytes, 32 + 4 * 12);
final var sectionOffset = readU32(bytes, 36 + 4 * 12);
final var sectionLength = readU32(bytes, 40 + 4 * 12);
assertEquals(5, sectionKind);
assertEquals(44, sectionOffset);
assertEquals(140, sectionOffset);
assertEquals(4, sectionLength);
final var syscCount = readU32(bytes, sectionOffset);
@@ -1,330 +1,127 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import p.studio.compiler.backend.bytecode.BytecodeModule;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.*;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.*;
@Disabled("parity test for intrinsic registry, should be enabled when modifying intrinsics definitions or spec")
class IRVMIntrinsicRegistryParityTest {
private static final String VM_ARCH_INTRINSICS_PATH = "docs/vm-arch/INTRINSICS.csv";
private static final String RUNTIME_INTRINSICS_PATH = "crates/console/prometeu-vm/src/builtins.rs";
private static final Pattern OWNER_PATTERN = Pattern.compile("owner:\\s*\"([^\"]+)\",");
private static final Pattern NAME_PATTERN = Pattern.compile("name:\\s*\"([^\"]+)\",");
private static final Pattern VERSION_PATTERN = Pattern.compile("version:\\s*(\\d+),");
private static final Pattern ID_PATTERN = Pattern.compile("id:\\s*(0x[0-9A-Fa-f]+|\\d+),");
private static final String STRICT_PARITY_ENV = "PROMETEU_INTRINSIC_PARITY_STRICT";
private static final String RESOURCE = "/runtime-catalog/compiler/";
private static final String HEADER = "final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate,parameter_names,cost_cycles,mutates_guest_state,cost_model,attempt_cost_cycles,max_attempts";
@Test
void registryDefinitionsMustExposeExpectedCanonicalEntries() {
final var snapshot = IRVMIntrinsicRegistry.snapshotByCanonicalIdentity();
assertFalse(snapshot.isEmpty());
assertEquals(23, snapshot.size());
assertEquals(0x2000, snapshot.get("input.pad@1"));
assertEquals(0x2033, snapshot.get("input.button.hold@1"));
void completeManualDefinitionsMatchIndependentlyPublishedProviderRows() throws Exception {
var lines = text("INTRINSICS.csv").lines().toList();
assertEquals(HEADER, lines.getFirst());
assertEquals(106, lines.size());
var actual = new ArrayList<String>();
var ids = new HashSet<Integer>();
var identities = new HashSet<String>();
for (var d : IRVMIntrinsicDefinition.values()) {
assertTrue(ids.add(d.finalId()));
assertTrue(identities.add(d.canonicalIdentity()));
assertSame(d, IRVMIntrinsicRegistry.requireById(d.finalId()));
assertSame(d, IRVMIntrinsicRegistry.resolve(d.canonicalName(), d.canonicalVersion()).orElseThrow());
actual.add(String.join(",", "0x%04X".formatted(d.finalId()), Integer.toUnsignedString(d.finalId()),
d.canonicalName(), Long.toString(d.canonicalVersion()), d.owner(), d.intrinsicName(),
Integer.toString(d.argSlots()), Integer.toString(d.retSlots()), d.argLayout(), d.retLayout(),
Boolean.toString(d.deterministic()), Boolean.toString(d.mayAllocate()), d.parameterNames(),
Long.toString(d.costCycles()), Boolean.toString(d.mutatesGuestState()), d.costModel().token(),
Long.toString(d.attemptCostCycles()), Integer.toString(d.maxAttempts())));
}
assertEquals(lines.subList(1, lines.size()), actual);
}
@Test
void registryDefinitionsMustStayInSyncWithVmArchCsv() throws IOException {
final var specPath = locateRepoRoot().resolve(VM_ARCH_INTRINSICS_PATH);
assertTrue(Files.isRegularFile(specPath), "intrinsics spec is missing: " + specPath);
final var expected = parseVmArchIntrinsicRows(specPath);
final var actual = Arrays.stream(IRVMIntrinsicDefinition.values())
.map(IntrinsicCsvRow::fromDefinition)
.toList();
assertEquals(expected, actual);
void completeConstantGroupsMatchProviderCarriersAndExactBits() throws Exception {
var lines = text("BUILTIN_CONSTANTS.csv").lines().toList();
assertEquals("target,name,version,slot,carrier,bits_or_value", lines.getFirst());
var actual = new ArrayList<String>();
var identities = new HashSet<String>();
for (var d : IRVMBuiltinConstantDefinition.values()) {
assertTrue(identities.add(d.target() + "." + d.constantName() + "@" + d.version()));
assertSame(d, IRVMBuiltinConstantDefinition.resolve(d.target(), d.constantName(), d.version()).orElseThrow());
var values = d.materialize();
assertEquals(d.slots().size(), values.size());
for (int index = 0; index < d.slots().size(); index++) {
var slot = d.slots().get(index);
var value = values.get(index);
String bits;
if (value instanceof BytecodeModule.Float64Constant f) {
assertEquals(slot.bitsOrValue(), Double.doubleToRawLongBits(f.value()));
bits = "0x%016X".formatted(Double.doubleToRawLongBits(f.value()));
} else {
bits = Long.toString(((BytecodeModule.Int64Constant) value).value());
}
actual.add(String.join(",", d.target(), d.constantName(), Long.toString(d.version()),
Integer.toString(index), slot.carrier().token(), bits));
}
}
assertEquals(15, identities.size());
assertEquals(16, actual.size());
assertEquals(lines.subList(1, lines.size()), actual);
assertTrue(IRVMIntrinsicRegistry.resolve("vec2.zero", 1).isEmpty());
assertTrue(IRVMIntrinsicRegistry.resolve("mathf.pi", 1).isEmpty());
assertTrue(IRVMBuiltinConstantDefinition.resolve("mathf", "pi", 2).isEmpty());
}
@Test
void registryMustStayInSyncWithRuntimeBuiltinsTable() throws IOException {
final var compilerSnapshot = IRVMIntrinsicRegistry.snapshotByCanonicalIdentity();
final var runtimeIntrinsicsFile = locateRuntimeIntrinsicsFile();
if (!Files.exists(runtimeIntrinsicsFile)) {
if (strictParityEnabled()) {
fail("runtime intrinsic table not found: " + runtimeIntrinsicsFile.toAbsolutePath());
}
return;
void provenanceMatchesPackagedCatalogBytesWithoutRuntimeCheckout() throws Exception {
var properties = new Properties();
try (var input = getClass().getResourceAsStream(RESOURCE + "runtime-catalog.properties")) {
assertNotNull(input);
properties.load(input);
}
final var runtimeSnapshot = parseRuntimeIntrinsicSnapshot(runtimeIntrinsicsFile);
assertEquals(runtimeSnapshot, compilerSnapshot, parityDiff(runtimeSnapshot, compilerSnapshot));
}
private List<IntrinsicCsvRow> parseVmArchIntrinsicRows(final Path specPath) throws IOException {
final var lines = Files.readAllLines(specPath, StandardCharsets.UTF_8);
final var rows = new ArrayList<IntrinsicCsvRow>();
for (int index = 0; index < lines.size(); index++) {
final var rawLine = lines.get(index);
final var line = rawLine.trim();
if (line.isBlank()) {
continue;
}
if (index == 0) {
assertEquals(
"final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate",
line,
"unexpected intrinsic csv header");
continue;
}
final var columns = line.split(",", -1);
if (columns.length != 12) {
fail("invalid intrinsics csv row at line " + (index + 1) + ": " + rawLine);
}
rows.add(new IntrinsicCsvRow(
normalizeHexId(columns[0]),
parseUnsignedInt(columns[1], "final_id_dec", index + 1),
requireNonBlank(columns[2], "canonical_name", index + 1),
parseLong(columns[3], "canonical_version", index + 1),
requireNonBlank(columns[4], "owner", index + 1),
requireNonBlank(columns[5], "name", index + 1),
parseInt(columns[6], "arg_slots", index + 1),
parseInt(columns[7], "ret_slots", index + 1),
columns[8].trim(),
columns[9].trim(),
parseBoolean(columns[10], "deterministic", index + 1),
parseBoolean(columns[11], "may_allocate", index + 1)));
}
return List.copyOf(rows);
}
private Map<String, Integer> parseRuntimeIntrinsicSnapshot(final Path runtimeFile) throws IOException {
final var runtimeSource = Files.readAllLines(runtimeFile, StandardCharsets.UTF_8);
final var mapped = new LinkedHashMap<String, Integer>();
var insideIntrinsicsArray = false;
var insideEntry = false;
String owner = null;
String name = null;
Long version = null;
Integer id = null;
for (final var rawLine : runtimeSource) {
final var line = rawLine.trim();
if (!insideIntrinsicsArray) {
if (line.startsWith("const INTRINSICS:")) {
insideIntrinsicsArray = true;
}
continue;
}
if (line.equals("];")) {
break;
}
if (line.startsWith("IntrinsicMeta {")) {
insideEntry = true;
owner = null;
name = null;
version = null;
id = null;
continue;
}
if (!insideEntry) {
continue;
}
final var ownerMatcher = OWNER_PATTERN.matcher(line);
if (ownerMatcher.find()) {
owner = ownerMatcher.group(1);
}
final var nameMatcher = NAME_PATTERN.matcher(line);
if (nameMatcher.find()) {
name = nameMatcher.group(1);
}
final var versionMatcher = VERSION_PATTERN.matcher(line);
if (versionMatcher.find()) {
version = Long.parseLong(versionMatcher.group(1));
}
final var idMatcher = ID_PATTERN.matcher(line);
if (idMatcher.find()) {
id = parseRuntimeId(idMatcher.group(1));
}
if (line.equals("},")) {
if (owner == null || name == null || version == null || id == null) {
throw new IllegalStateException("invalid runtime intrinsic entry near line: " + rawLine);
}
final var identity = owner + "." + name + "@" + version;
final var previous = mapped.putIfAbsent(identity, id);
if (previous != null && previous != id) {
throw new IllegalStateException(
"runtime intrinsic identity collision for " + identity + ": " + previous + " vs " + id);
}
insideEntry = false;
}
}
return Map.copyOf(mapped);
}
private Path locateRepoRoot() {
var cursor = Path.of(System.getProperty("user.dir")).toAbsolutePath().normalize();
while (cursor != null) {
if (Files.isDirectory(cursor.resolve("docs"))
&& Files.isDirectory(cursor.resolve("prometeu-compiler"))
&& Files.exists(cursor.resolve("settings.gradle.kts"))) {
return cursor;
}
cursor = cursor.getParent();
}
fail("could not locate repository root from working directory");
throw new IllegalStateException("unreachable");
}
private Path locateRuntimeIntrinsicsFile() {
return locateRepoRoot()
.getParent()
.resolve("runtime")
.resolve(RUNTIME_INTRINSICS_PATH);
}
private int parseRuntimeId(final String rawId) {
if (rawId.startsWith("0x") || rawId.startsWith("0X")) {
return Integer.parseUnsignedInt(rawId.substring(2), 16);
}
return Integer.parseUnsignedInt(rawId, 10);
}
private String requireNonBlank(
final String value,
final String fieldName,
final int lineNumber) {
final var normalized = value.trim();
if (normalized.isBlank()) {
fail("blank " + fieldName + " at line " + lineNumber);
}
return normalized;
}
private int parseInt(
final String rawValue,
final String fieldName,
final int lineNumber) {
try {
return Integer.parseInt(rawValue.trim());
} catch (NumberFormatException ex) {
fail("invalid " + fieldName + " at line " + lineNumber + ": " + rawValue);
throw new IllegalStateException("unreachable", ex);
assertEquals("f7d7c167531bb662a1af9d73fc326bb476bf8aa7", properties.getProperty("runtime.revision"));
for (var name : List.of("INTRINSICS.csv", "BUILTIN_CONSTANTS.csv")) {
assertEquals(properties.getProperty(name + ".sha256"),
HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes(name))));
}
}
private long parseLong(
final String rawValue,
final String fieldName,
final int lineNumber) {
try {
return Long.parseLong(rawValue.trim());
} catch (NumberFormatException ex) {
fail("invalid " + fieldName + " at line " + lineNumber + ": " + rawValue);
throw new IllegalStateException("unreachable", ex);
@Test
void changedInputAbiAndRemovedEndpointIdsCannotResolve() {
var x = IRVMIntrinsicRegistry.resolve("input.touch.x", 1).orElseThrow();
assertEquals(0x2134, x.finalId());
assertDoesNotThrow(() -> x.validateSignature(0, 1));
assertThrows(IllegalArgumentException.class, () -> x.validateSignature(1, 1));
assertThrows(IllegalArgumentException.class, () -> x.validateSignature(0, 0));
assertThrows(IllegalArgumentException.class, () -> x.validateSignature(null, 1));
assertTrue(IRVMIntrinsicRegistry.resolve("input.pad", 1).isEmpty());
assertTrue(IRVMIntrinsicRegistry.resolve("input.touch.x", 2).isEmpty());
for (int id : new int[]{0x2000, 0x2001, 0x2010, 0x201b, 0x2020, 0x2021, 0x2022, 0x2030, 0x2033, 0xffff}) {
assertThrows(IllegalArgumentException.class, () -> IRVMIntrinsicRegistry.requireById(id));
}
assertThrows(IllegalArgumentException.class, () -> IRVMIntrinsicDefinition.Carrier.fromToken("int"));
assertThrows(IllegalArgumentException.class, () -> IRVMIntrinsicDefinition.Carrier.fromToken("builtin:input.pad"));
assertThrows(IllegalArgumentException.class, () -> IRVMIntrinsicDefinition.parseLayout("Float|"));
}
private int parseUnsignedInt(
final String rawValue,
final String fieldName,
final int lineNumber) {
try {
return Integer.parseUnsignedInt(rawValue.trim(), 10);
} catch (NumberFormatException ex) {
fail("invalid " + fieldName + " at line " + lineNumber + ": " + rawValue);
throw new IllegalStateException("unreachable", ex);
@Test
void deterministicRandomStillMutatesAndRangeHasAttemptCosts() {
for (int id = 0x4000; id <= 0x4003; id++) {
var d = IRVMIntrinsicRegistry.requireById(id);
assertTrue(d.deterministic());
assertTrue(d.mutatesGuestState());
assertEquals(id == 0x4000, d.mayAllocate());
}
var range = IRVMIntrinsicRegistry.requireById(0x4003);
assertEquals(IRVMIntrinsicDefinition.CostModel.RANDOM_REJECTION_V1, range.costModel());
assertEquals(4, range.costCycles());
assertEquals(8, range.attemptCostCycles());
assertEquals(64, range.maxAttempts());
}
private boolean parseBoolean(
final String rawValue,
final String fieldName,
final int lineNumber) {
return switch (rawValue.trim().toLowerCase(Locale.ROOT)) {
case "true" -> true;
case "false" -> false;
default -> {
fail("invalid " + fieldName + " at line " + lineNumber + ": " + rawValue);
yield false;
}
};
}
private String normalizeHexId(final String rawValue) {
final var trimmed = rawValue.trim();
final var parsed = parseRuntimeId(trimmed);
return "0x" + Integer.toUnsignedString(parsed, 16).toUpperCase(Locale.ROOT);
}
private boolean strictParityEnabled() {
final var strict = System.getenv(STRICT_PARITY_ENV);
if (isTruthy(strict)) {
return true;
}
return false;
}
private boolean isTruthy(final String value) {
if (value == null) {
return false;
}
return switch (value.trim().toLowerCase()) {
case "1", "true", "yes", "on" -> true;
default -> false;
};
}
private String parityDiff(
final Map<String, Integer> runtimeSnapshot,
final Map<String, Integer> compilerSnapshot) {
final var missingInCompiler = new ArrayList<String>();
final var missingInRuntime = new ArrayList<String>();
final var mismatchedIds = new ArrayList<String>();
for (final var runtimeEntry : runtimeSnapshot.entrySet()) {
final var compilerId = compilerSnapshot.get(runtimeEntry.getKey());
if (compilerId == null) {
missingInCompiler.add(runtimeEntry.getKey());
continue;
}
if (!runtimeEntry.getValue().equals(compilerId)) {
mismatchedIds.add(runtimeEntry.getKey() + " runtime=" + runtimeEntry.getValue() + " compiler=" + compilerId);
}
}
for (final var compilerEntry : compilerSnapshot.entrySet()) {
if (!runtimeSnapshot.containsKey(compilerEntry.getKey())) {
missingInRuntime.add(compilerEntry.getKey());
}
}
return "intrinsic registry parity mismatch"
+ "\nmissingInCompiler=" + List.copyOf(missingInCompiler)
+ "\nmissingInRuntime=" + List.copyOf(missingInRuntime)
+ "\nidMismatches=" + List.copyOf(mismatchedIds);
}
private record IntrinsicCsvRow(
String finalIdHex,
int finalIdDec,
String canonicalName,
long canonicalVersion,
String owner,
String name,
int argSlots,
int retSlots,
String argLayout,
String retLayout,
boolean deterministic,
boolean mayAllocate) {
private static IntrinsicCsvRow fromDefinition(final IRVMIntrinsicDefinition definition) {
return new IntrinsicCsvRow(
"0x" + Integer.toUnsignedString(definition.finalId(), 16).toUpperCase(Locale.ROOT),
definition.finalId(),
definition.canonicalName(),
definition.canonicalVersion(),
definition.owner(),
definition.intrinsicName(),
definition.argSlots(),
definition.retSlots(),
definition.argLayout(),
definition.retLayout(),
definition.deterministic(),
definition.mayAllocate());
private byte[] bytes(String name) throws IOException {
try (var stream = getClass().getResourceAsStream(RESOURCE + name)) {
assertNotNull(stream, "missing packaged provider artifact " + name);
return stream.readAllBytes();
}
}
private String text(String name) throws IOException { return new String(bytes(name), StandardCharsets.UTF_8); }
}
@@ -180,7 +180,7 @@ class IRVMValidatorTest {
0,
1,
ReadOnlyList.from(
new IRVMInstruction(IRVMOp.INTRINSIC, 0),
new IRVMInstruction(IRVMOp.INTRINSIC, 0x2112),
new IRVMInstruction(IRVMOp.RET, null)))));
final var plan = new BytecodeEmitter.EmissionPlan(
0,
@@ -193,7 +193,7 @@ class IRVMValidatorTest {
0,
1,
ReadOnlyList.from(
BytecodeEmitter.Operation.intrinsic(0, null),
BytecodeEmitter.Operation.intrinsic(0x2112, null),
BytecodeEmitter.Operation.ret()))));
final var program = new IRVMProgram(module, plan);
@@ -212,9 +212,10 @@ class LowerToIRVMServiceTest {
.executableFunctions(ReadOnlyList.from(
fn("main", "app", 10, ReadOnlyList.from(
callHost("gfx2d", "draw_pixel", 1, 0, 0),
callIntrinsic("input.pad", 1, 0),
callIntrinsic("input.pad.a.down", 1, 0),
pop(),
ret()))))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad", 1)))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad.a.down", 1)))
.build();
final var lowered = new LowerToIRVMService().lower(backend);
@@ -222,7 +223,7 @@ class LowerToIRVMServiceTest {
final var instructions = lowered.module().functions().getFirst().instructions();
assertEquals(IRVMOp.HOSTCALL, instructions.get(0).op());
assertEquals(IRVMOp.INTRINSIC, instructions.get(1).op());
assertEquals(0x2000, instructions.get(1).immediate());
assertEquals(0x2112, instructions.get(1).immediate());
final var emissionOps = lowered.emissionPlan().functions().getFirst().operations();
assertEquals(p.studio.compiler.backend.bytecode.BytecodeEmitter.OperationKind.HOSTCALL, emissionOps.get(0).kind());
assertEquals(p.studio.compiler.backend.bytecode.BytecodeEmitter.OperationKind.INTRINSIC, emissionOps.get(1).kind());
@@ -238,12 +239,13 @@ class LowerToIRVMServiceTest {
.executableFunctions(ReadOnlyList.from(
fnWithModule("tick", "game/runtime", 0, 10, ReadOnlyList.from(
callHost("timer", "poll", 1, 0, 0),
callIntrinsic("input.pad", 1, 0),
callIntrinsic("input.pad.a.down", 1, 0),
pop(),
callFuncWithExpected("game/runtime", "helper", 20, 0, 0),
ret())),
fnWithModule("helper", "game/runtime", 0, 20, ReadOnlyList.from(
ret()))))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad", 1)))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad.a.down", 1)))
.build();
final var lowered = new LowerToIRVMService().lower(backend);
@@ -253,9 +255,10 @@ class LowerToIRVMServiceTest {
final var instructions = lowered.module().functions().getFirst().instructions();
assertEquals(IRVMOp.HOSTCALL, instructions.get(0).op());
assertEquals(IRVMOp.INTRINSIC, instructions.get(1).op());
assertEquals(IRVMOp.CALL, instructions.get(2).op());
assertEquals(1, instructions.get(2).immediate());
assertEquals(IRVMOp.RET, instructions.get(3).op());
assertEquals(IRVMOp.POP, instructions.get(2).op());
assertEquals(IRVMOp.CALL, instructions.get(3).op());
assertEquals(1, instructions.get(3).immediate());
assertEquals(IRVMOp.RET, instructions.get(4).op());
}
@Test
@@ -527,6 +530,10 @@ class LowerToIRVMServiceTest {
Span.none());
}
private static IRBackendExecutableFunction.Instruction pop() {
return new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.POP, "", null, null, Span.none());
}
private static IRBackendExecutableFunction.Instruction ret() {
return new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.RET,
@@ -592,7 +599,7 @@ class LowerToIRVMServiceTest {
null,
new IRBackendExecutableFunction.IntrinsicCallMetadata(canonicalName, canonicalVersion, new IntrinsicId(intrinsicId)),
0,
0,
1,
Span.none());
}
@@ -30,9 +30,10 @@ class OptimizeIRVMEquivalenceHarnessTest {
.executableFunctions(ReadOnlyList.from(
fn("main", 1, ReadOnlyList.from(
callHost("gfx2d", "draw_pixel", 1, 0, 0),
callIntrinsic("input.pad", 1, 0, 0, 0),
callIntrinsic("input.pad.a.down", 1, 0, 0, 1),
new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.POP, "", null, null, Span.none()),
ret()))))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad", 1)))
.intrinsicPool(ReadOnlyList.from(new IntrinsicReference("input.pad.a.down", 1)))
.build();
final var lowered = new LowerToIRVMService().lower(backend);
@@ -45,14 +46,14 @@ class OptimizeIRVMEquivalenceHarnessTest {
"main",
1,
ReadOnlyList.from(
new IRVMInstruction(IRVMOp.INTRINSIC, 901),
new IRVMInstruction(IRVMOp.INTRINSIC, 0x2110),
new IRVMInstruction(IRVMOp.JMP_IF_FALSE, 24),
new IRVMInstruction(IRVMOp.HOSTCALL, 0),
new IRVMInstruction(IRVMOp.JMP, 30),
new IRVMInstruction(IRVMOp.HOSTCALL, 0),
new IRVMInstruction(IRVMOp.RET, null)),
ReadOnlyList.from(
BytecodeEmitter.Operation.intrinsic(901, 0, 1, null),
BytecodeEmitter.Operation.intrinsic(0x2110, 0, 1, null),
BytecodeEmitter.Operation.jmpIfFalse(24, null),
hostcallOp("gfx", "draw_true_branch", 1, 0, 0),
BytecodeEmitter.Operation.jmp(30, null),
@@ -67,13 +68,13 @@ class OptimizeIRVMEquivalenceHarnessTest {
"main",
1,
ReadOnlyList.from(
new IRVMInstruction(IRVMOp.INTRINSIC, 900),
new IRVMInstruction(IRVMOp.INTRINSIC, 0x2112),
new IRVMInstruction(IRVMOp.JMP_IF_FALSE, 24),
new IRVMInstruction(IRVMOp.HOSTCALL, 0),
new IRVMInstruction(IRVMOp.JMP, 0),
new IRVMInstruction(IRVMOp.RET, null)),
ReadOnlyList.from(
BytecodeEmitter.Operation.intrinsic(900, 0, 1, null),
BytecodeEmitter.Operation.intrinsic(0x2112, 0, 1, null),
BytecodeEmitter.Operation.jmpIfFalse(24, null),
hostcallOp("gfx", "loop_tick", 1, 0, 0),
BytecodeEmitter.Operation.jmp(0, null),
@@ -199,6 +200,9 @@ class OptimizeIRVMEquivalenceHarnessTest {
}
applyOperationStackEffect(stack, operation);
frame.advance();
} else if (op == IRVMOp.POP) {
stack.pop();
frame.advance();
} else if (op == IRVMOp.INTRINSIC) {
trace.add("INTRINSIC#" + instruction.immediate());
applyOperationStackEffect(stack, operation);
@@ -238,13 +242,14 @@ class OptimizeIRVMEquivalenceHarnessTest {
}
}
// Scripted input samples exercise control flow; this interpreter is not a runtime input oracle.
private int simulatedIntrinsicValue(
final int intrinsicId,
final int invocationIndex) {
if (intrinsicId == 900) {
if (intrinsicId == 0x2112) {
return invocationIndex == 0 ? 1 : 0;
}
if (intrinsicId == 901) {
if (intrinsicId == 0x2110) {
return 0;
}
return 1;
@@ -0,0 +1,124 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.models.IRBackend;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.models.SourceKind;
import java.nio.file.*;
import static org.junit.jupiter.api.Assertions.*;
class PbsRuntimeAlignmentTest {
private static String sdk(String path) throws Exception {
try(var stream = PbsFrontendCompiler.class.getResourceAsStream("/game/stdlib/1/"+path+"/main.pbs")) {
assertNotNull(stream); return new String(stream.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8);
}
}
private static BytecodeModule compile(String name,String source) throws Exception {
var diagnostics=DiagnosticSink.empty();
var fileId = new FileId(0);
var ast = p.studio.compiler.pbs.parser.PbsParser.parse(p.studio.compiler.pbs.lexer.PbsLexer.lex(source,fileId,diagnostics),fileId,diagnostics,p.studio.compiler.pbs.parser.PbsParser.ParseMode.INTERFACE_MODULE);
var file=new PbsFrontendCompiler().compileParsedFile(fileId,ast,diagnostics,SourceKind.SDK_INTERFACE,
new p.studio.compiler.source.identifiers.ModuleId(0),p.studio.utilities.structures.ReadOnlyList.empty(),
p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),p.studio.compiler.messages.FESurfaceContext.empty(),new p.studio.compiler.source.tables.NameTable());
assertFalse(diagnostics.hasErrors(), ()->diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()).toList().toString());
var backend=IRBackend.builder().entryPointCallableName("Probe.main").entryPointModuleId(new p.studio.compiler.source.identifiers.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());
new BytecodePreloadVerifierService().verify(module);
var directory=Path.of("build/registry-conformance");Files.createDirectories(directory);
Files.write(directory.resolve("pbs-"+name+".pbx"),module.serialize());
return module;
}
@Test void numericValuesAndExplicitConversionsReachProductionPbx() throws Exception {
compile("numeric", """
declare service Probe {
fn widen(value: int) -> long { return value as long; }
fn contextual() -> long { return 1; }
fn receive(value: long) -> long { return value; }
fn main() -> long {
let large: long = 2147483648;
let maximum: long = 9223372036854775807;
Probe.contextual();
Probe.receive(2147483648);
return large + Probe.widen(7);
}
}
""");
compile("narrow", "declare service Probe { fn main() -> int { let value: long = 2147483648; return value as int; } }");
compile("float", "declare service Probe { fn main() -> float { return 1.5 + 2.25; } }");
compile("overflow", "declare service Probe { fn main() -> int { return 2147483647 + 1; } }");
}
@Test void allInputQueriesCompileFromThePublishedFacade() throws Exception {
var input=sdk("sdk/input");
for (var button : new String[]{"Up","Down","Left","Right","A","B","X","Y","L","R","Start","Select"}) {
for(var query:new String[]{"pressed","released","down","hold"}) {
var type=query.equals("hold")?"long":"bool";
compile("pad-"+button.toLowerCase()+"-"+query,input+"\ndeclare service Probe { fn select(button: Button) -> Button { return button; } fn main() -> "+type+" { let button = Probe.select(Button."+button+"); return Pad."+query+"(button); } }");
compile("pad-"+button.toLowerCase()+"-"+query+"-constant", input+"\ndeclare service Probe { fn main() -> "+type+" { return Pad."+query+"(Button."+button+"); } }");
}
}
for(var query:new String[]{"pressed","released","down","hold","x","y"}) {
var type=java.util.Set.of("hold","x","y").contains(query)?"long":"bool";
compile("touch-"+query,input+"\ndeclare service Probe { fn main() -> "+type+" { return Touch."+query+"(); } }");
}
}
@Test void completeBuiltinAndTupleGroupsReachProductionPbx() throws Exception {
compile("vec2", """
[BuiltinType(name = "vec2", version = 1)]
declare builtin type Vector(pub x: float, pub y: float) {}
[BuiltinConst(target = "vec2", name = "zero", version = 1)]
declare const ZERO: Vector;
declare service Probe {
fn echo(value: Vector) -> Vector { return value; }
fn main() -> Vector { let vector = Probe.echo(ZERO); return vector; }
}
""");
compile("tuple", """
declare service Probe {
fn pair() -> (x: int, y: long) { return (x: 7, y: 8 as long); }
fn main() -> (x: int, y: long) { let pair = Probe.pair(); return pair; }
}
""");
compile("named", """
declare global trace: int = 0;
declare service Probe {
fn x() -> int { trace = trace * 10 + 1; return 10; }
fn y() -> int { trace = trace * 10 + 2; return 20; }
fn combine(x: int, y: int) -> int { return x * 100 + y; }
fn main() -> int {
trace = 0;
let value = Probe.combine apply (y: Probe.y(), x: Probe.x());
return trace * 10000 + value;
}
}
""");
compile("color", sdk("core/color") + "declare service Probe { fn echo(value: Color) -> Color { return value; } fn main() -> Color { return Probe.echo(RED); } }");
compile("pixel", sdk("core/color") + """
[BuiltinType(name = "pixel", version = 1)]
declare builtin type Pixel(pub x: long, pub y: long, pub color: Color) {}
declare service Probe {
fn echo(value: Pixel) -> Pixel { return value; }
fn main() -> Pixel { let value = new Pixel(12, -34, RED); return Probe.echo(value); }
}
""");
compile("string", "declare service Probe { fn echo(value: str) -> str { return value; } fn main() -> str { return Probe.echo(\"hello\"); } }");
compile("division", "declare service Probe { fn main() -> int { return 1 / 0; } }");
compile("minimum", "declare service Probe { fn main() -> long { return -9223372036854775808; } }");
}
@Test void wrongNumericAndButtonTypesAreRejected() throws Exception {
for(var expression:new String[]{"1","1 as long","true","Other.A","\"A\""}) {
var diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0),sdk("sdk/input")+"\ndeclare enum Other(A); declare service Probe { fn main() -> bool { return Pad.pressed("+expression+"); } }",diagnostics,SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.hasErrors());
}
for(var source:new String[]{"fn main() -> long { let x: long = 1; let y: int = 1; return x + y; }", "fn main() -> int { return 2147483648; }", "fn main() -> long { return 9223372036854775808; }"}) {
var diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0),source,diagnostics);
assertTrue(diagnostics.hasErrors());
}
}
}
@@ -0,0 +1,245 @@
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.models.registry.IRRegistry;
import p.studio.compiler.models.registry.IRRegistry.*;
import p.studio.compiler.source.Span;
import p.studio.compiler.source.identifiers.*;
import p.studio.compiler.source.tables.IntrinsicReference;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.file.*;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import static p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.*;
class RuntimeCatalogPipelineTest {
private static final BytecodePreloadVerifierService VERIFIER = new BytecodePreloadVerifierService();
private static final IRRegistry REGISTRY = new IRRegistry(ReadOnlyList.from(
new TypeDeclaration(TypeRef.builtin(0x10), null, new Builtin(0x10)),
new TypeDeclaration(TypeRef.builtin(0x11), null, new Builtin(0x11)),
new TypeDeclaration(TypeRef.builtin(0x12), null, new Builtin(0x12)),
new TypeDeclaration(TypeRef.builtin(0x16), null, new Builtin(0x16)),
new TypeDeclaration(TypeRef.builtin(2), null, new Builtin(2))), null);
@Test
void commonPipelinePreservesAliasedRandomMutationAndDiscardedResult() throws Exception {
var source = new CommonInput();
source.integer64(42);
source.call("random.new", 1, 1);
source.local(SET_LOCAL, 0);
source.local(GET_LOCAL, 0);
source.local(SET_LOCAL, 1); // Alias the handle; do not fork the generator.
source.local(GET_LOCAL, 0);
source.call("random.next", 1, 1);
source.plain(POP); // Even an unused result must advance the sequence.
source.local(GET_LOCAL, 1);
source.call("random.nextf", 1, 1);
source.local(GET_LOCAL, 0);
source.integer64(-7);
source.integer64(-6);
source.call("random.range", 3, 1);
source.local(GET_LOCAL, 1);
source.call("random.next", 1, 1);
source.plain(HALT);
var lowered = new LowerToIRVMService().lower(source.backend());
var optimized = new OptimizeIRVMService().optimize(lowered);
var trace = List.of(0x3014, 0x4000, 0x4001, 0x4002, 0x3014, 0x3014, 0x4003, 0x4001);
assertEquals(trace, intrinsicTrace(lowered));
assertEquals(trace, intrinsicTrace(optimized));
var emitter = new BytecodeEmitter();
var off = emitter.emit(lowered.coherentEmissionPlan());
var on = emitter.emit(optimized.coherentEmissionPlan());
VERIFIER.verify(off);
VERIFIER.verify(on);
fixture("catalog-random-off", off);
fixture("catalog-random-on", on);
}
@Test
void exactConstantBitsAndCompleteVec2GroupReachEmission() throws Exception {
var pool = new ArrayList<BytecodeModule.ConstantPoolEntry>();
for (var definition : IRVMBuiltinConstantDefinition.values()) pool.addAll(definition.materialize().asList());
var ops = new ArrayList<BytecodeEmitter.Operation>();
for (int index = 0; index < pool.size(); index++) ops.add(BytecodeEmitter.Operation.pushConst(index, null));
ops.add(BytecodeEmitter.Operation.halt());
var plan = new BytecodeEmitter.EmissionPlan(0, ReadOnlyList.wrap(pool), null,
ReadOnlyList.from(new BytecodeEmitter.FunctionPlan("main", 0, 0, 0, 16, ReadOnlyList.wrap(ops))), REGISTRY);
var program = new OptimizeIRVMService().optimize(new IRVMProgram(false, plan));
var module = new BytecodeEmitter().emit(program.coherentEmissionPlan());
VERIFIER.verify(module);
// Pool deduplication is valid; each of the 16 group components must still be pushed.
var code = java.nio.ByteBuffer.wrap(module.code()).order(java.nio.ByteOrder.LITTLE_ENDIAN);
for (var expected : pool) {
assertEquals(0x10, Short.toUnsignedInt(code.getShort()));
assertEquals(expected, module.constPool().get(code.getInt()));
}
assertEquals(0x01, Short.toUnsignedInt(code.getShort()));
assertFalse(code.hasRemaining());
assertEquals(0x400921FB54442D18L, Double.doubleToRawLongBits(((BytecodeModule.Float64Constant) module.constPool().get(12)).value()));
assertEquals(0x401921FB54442D18L, Double.doubleToRawLongBits(((BytecodeModule.Float64Constant) module.constPool().get(13)).value()));
fixture("catalog-constants", module);
var vector = IRVMBuiltinConstantDefinition.VEC2_ZERO.materialize();
var signature = FunctionMetadata.ordinary(new Signature(null, ReadOnlyList.from(TypeRef.builtin(2))));
var vectorPlan = new BytecodeEmitter.EmissionPlan(0, vector, null, ReadOnlyList.from(
new BytecodeEmitter.FunctionPlan("main", 0, 0, 2, 2, ReadOnlyList.from(
BytecodeEmitter.Operation.pushConst(0, null), BytecodeEmitter.Operation.pushConst(1, null),
BytecodeEmitter.Operation.ret()), signature)), REGISTRY);
var group = new BytecodeEmitter().emit(new OptimizeIRVMService().optimize(new IRVMProgram(false, vectorPlan)).coherentEmissionPlan());
VERIFIER.verify(group);
fixture("catalog-vec2-zero", group);
var truncated = new BytecodeEmitter.EmissionPlan(0, vector, null, ReadOnlyList.from(
new BytecodeEmitter.FunctionPlan("main", 0, 0, 2, 2, ReadOnlyList.from(
BytecodeEmitter.Operation.pushConst(0, null), BytecodeEmitter.Operation.ret()), signature)), REGISTRY);
assertThrows(IRVMValidationException.class, () -> new OptimizeIRVMService().optimize(new IRVMProgram(false, truncated)));
}
@Test
void sameWidthCarriersCannotSubstituteForEachOtherAndResultsAreTyped() {
var wrongArgument = new CommonInput();
wrongArgument.integer32(42);
wrongArgument.call("math.i64.abs", 1, 1);
wrongArgument.plain(HALT);
var error = assertThrows(BytecodeMarshalingException.class, () -> VERIFIER.verify(emit(wrongArgument)));
assertTrue(error.getMessage().contains("carrier mismatch"));
var wrongResult = new CommonInput();
wrongResult.integer32(42);
wrongResult.call("math.i32.to_i64", 1, 1);
wrongResult.call("math.i32.abs", 1, 1);
wrongResult.plain(HALT);
assertThrows(BytecodeMarshalingException.class, () -> VERIFIER.verify(emit(wrongResult)));
var valid = new CommonInput();
valid.integer64(-42);
valid.call("math.i64.abs", 1, 1);
valid.plain(HALT);
assertDoesNotThrow(() -> VERIFIER.verify(emit(valid)));
var badColor = new CommonInput();
badColor.integer32(255);
badColor.call("color.from_raw", 1, 1);
badColor.plain(HALT);
assertThrows(BytecodeMarshalingException.class, () -> VERIFIER.verify(emit(badColor)));
var badReference = new CommonInput();
badReference.integer64(42);
badReference.call("random.next", 1, 1);
badReference.plain(HALT);
assertThrows(BytecodeMarshalingException.class, () -> VERIFIER.verify(emit(badReference)));
}
@Test
void stringIntrinsicUsesTheProviderLoadTimeCarrierRule() throws Exception {
for (var constant : List.of(new BytecodeModule.StringConstant("#AABBCCDD"), new BytecodeModule.NullConstant())) {
var plan = new BytecodeEmitter.EmissionPlan(0, ReadOnlyList.from(constant), null,
ReadOnlyList.from(new BytecodeEmitter.FunctionPlan("main", 0, 0, 0, 1, ReadOnlyList.from(
BytecodeEmitter.Operation.pushConst(0, null),
BytecodeEmitter.Operation.intrinsic(0x1103, 1, 1, null),
BytecodeEmitter.Operation.halt()))), REGISTRY);
var module = new BytecodeEmitter().emit(plan);
if (constant instanceof BytecodeModule.NullConstant) {
assertThrows(BytecodeMarshalingException.class, () -> VERIFIER.verify(module));
fixture("catalog-null-string", module);
} else {
VERIFIER.verify(module);
fixture("catalog-string", module);
}
}
}
@Test
void moduleWithoutSeparateEmissionPlanDerivesIntrinsicAbiFromTarget() {
var input = new CommonInput();
input.call("input.touch.x", 0, 1);
input.plain(HALT);
var moduleOnly = new IRVMProgram(new LowerToIRVMService().lower(input.backend()).module());
var emitted = new BytecodeEmitter().emit(new OptimizeIRVMService().optimize(moduleOnly).coherentEmissionPlan());
assertEquals(new BytecodeModule.IntrinsicSignature(0x2134, 0, 1), emitted.intrinsicSignatures().getFirst());
VERIFIER.verify(emitted);
}
@Test
void loweringRejectsRetiredNamesAndChangedReceiverAbiWithStageContext() {
var retired = new CommonInput();
retired.call("input.pad", 0, 1);
retired.plain(HALT);
var unknown = assertThrows(IRVMLoweringException.class, () -> new LowerToIRVMService().lower(retired.backend()));
assertEquals(IRVMLoweringErrorCode.LOWER_IRVM_INVALID_INTRINSIC_ID, unknown.code());
var stale = new CommonInput();
stale.integer32(0);
stale.call("input.touch.x", 1, 1);
stale.plain(HALT);
var abi = assertThrows(IRVMLoweringException.class, () -> new LowerToIRVMService().lower(stale.backend()));
assertEquals(IRVMLoweringErrorCode.LOWER_IRVM_CALL_ARG_SLOTS_MISMATCH, abi.code());
}
@Test
void unknownUnreachableIdsAndForgedSignatureCachesAreRejected() throws Exception {
var input = new CommonInput();
input.call("input.touch.x", 0, 1);
input.plain(HALT);
var valid = emit(input);
VERIFIER.verify(valid);
fixture("catalog-input", valid); // Provider must trap UNAVAILABLE without hardware, not INVALID_INTRINSIC.
var forged = new BytecodeModule(0, valid.constPool(), valid.functions(), valid.code(), null, null, null,
valid.registry(), ReadOnlyList.from(new BytecodeModule.IntrinsicSignature(0x2134, 1, 1)));
assertThrows(BytecodeMarshalingException.class, () -> VERIFIER.verify(forged));
// HALT before the obsolete instruction: operand validation must still reject it.
var code = new byte[]{1, 0, 0x72, 0, 0, 0x20, 0, 0};
var removed = new BytecodeModule(0, null, ReadOnlyList.from(new BytecodeModule.FunctionMeta(
0, code.length, 0, 0, 0, 1, FunctionMetadata.empty())), code, null, null, null, REGISTRY);
assertThrows(BytecodeMarshalingException.class, () -> VERIFIER.verify(removed));
fixture("catalog-removed", removed);
var duplicate = new BytecodeModule(0, valid.constPool(), valid.functions(), valid.code(), null, null, null,
valid.registry(), ReadOnlyList.from(valid.intrinsicSignatures().getFirst(), valid.intrinsicSignatures().getFirst()));
assertThrows(BytecodeMarshalingException.class, () -> VERIFIER.verify(duplicate));
}
private static List<Integer> intrinsicTrace(IRVMProgram program) {
return program.module().functions().getFirst().instructions().stream()
.filter(i -> i.op() == IRVMOp.INTRINSIC).map(IRVMInstruction::immediate).toList();
}
private static BytecodeModule emit(CommonInput input) {
return new BytecodeEmitter().emit(new OptimizeIRVMService().optimize(
new LowerToIRVMService().lower(input.backend())).coherentEmissionPlan());
}
private static void fixture(String name, BytecodeModule module) throws Exception {
var directory = Path.of("build/registry-conformance");
Files.createDirectories(directory);
Files.write(directory.resolve(name + ".pbx"), module.serialize());
}
private static final class CommonInput {
final List<IRBackendExecutableFunction.Instruction> instructions = new ArrayList<>();
final List<IntrinsicReference> pool = new ArrayList<>();
void plain(IRBackendExecutableFunction.InstructionKind kind) {
instructions.add(new IRBackendExecutableFunction.Instruction(kind, "", null, null, Span.none()));
}
void integer32(int value) {
instructions.add(new IRBackendExecutableFunction.Instruction(PUSH_I32, null, null, null, null, null,
Integer.toString(value), null, null, null, Span.none()));
}
void integer64(int value) { integer32(value); call("math.i32.to_i64", 1, 1); }
void local(IRBackendExecutableFunction.InstructionKind kind, int slot) {
instructions.add(new IRBackendExecutableFunction.Instruction(kind, "", null, null, slot, null, Span.none()));
}
void call(String name, int arguments, int results) {
var id = new IntrinsicId(pool.size());
pool.add(new IntrinsicReference(name, 1));
instructions.add(new IRBackendExecutableFunction.Instruction(CALL_INTRINSIC, "", null,
new IRBackendExecutableFunction.IntrinsicCallMetadata(name, 1, id), arguments, results, Span.none()));
}
IRBackend backend() {
return IRBackend.builder().entryPointModuleId(new ModuleId(0)).registry(REGISTRY)
.intrinsicPool(ReadOnlyList.wrap(pool)).executableFunctions(ReadOnlyList.from(
new IRBackendExecutableFunction(new FileId(0), new ModuleId(0), "main", new CallableId(0),
0, 0, 0, 2, 0, 8, ReadOnlyList.wrap(instructions), Span.none(), FunctionMetadata.empty())))
.build();
}
}
}
@@ -0,0 +1,53 @@
package p.studio.compiler.backend.registry;
import org.junit.jupiter.api.Test;
import p.studio.compiler.models.registry.IRRegistry;
import p.studio.compiler.models.registry.IRRegistry.*;
import p.studio.utilities.structures.ReadOnlyList;
import static org.junit.jupiter.api.Assertions.*;
class LinkedRegistryTest {
private static TypeRef ref(String key) { return new TypeRef(key); }
private static TypeDeclaration decl(String key, TypeBody body) { return new TypeDeclaration(ref(key), "same display name", body); }
private static IRRegistry source(TypeDeclaration... types) { return new IRRegistry(ReadOnlyList.from(types), null); }
@Test void nominalIdentityIsNotNameOrShapeAndRepeatedDeclarationsConverge() {
var a=decl("module:a",new Struct(null)); var b=decl("module:b",new Struct(null));
var linked=LinkedRegistry.link(source(a,b,a));
assertEquals(2,linked.types().size());
assertNotEquals(linked.id(a.identity()),linked.id(b.identity()));
assertEquals(linked.types(),LinkedRegistry.link(source(b,a)).types());
}
@Test void importsArraysAndFnSignaturesCanonicalizeWithoutUsingParameterNames() {
var signature=new Signature(ReadOnlyList.from(new NamedType(ref("v1"),"x")),ReadOnlyList.from(ref("v2")));
var renamed=new Signature(ReadOnlyList.from(new NamedType(ref("v2"),"renamed")),ReadOnlyList.from(ref("v1")));
var linked=LinkedRegistry.link(source(decl("v1",new Builtin(2)),decl("v2",new Builtin(2)),
decl("a1",new Array(ref("v1"))),decl("a2",new Array(ref("v2"))),
decl("f1",new Fn(signature)),decl("f2",new Fn(renamed))));
assertEquals(3,linked.types().size());
assertEquals(linked.id(ref("v1")),linked.id(ref("v2")));
assertEquals(linked.id(ref("a1")),linked.id(ref("a2")));
assertEquals(linked.id(ref("f1")),linked.id(ref("f2")));
assertEquals(2,linked.parameterSlots(signature));
assertEquals(2,linked.resultSlots(signature));
}
@Test void nominalAnchorsAllowForwardReferencesButStructuralCyclesReject() {
var signature=new Signature(ReadOnlyList.from(new NamedType(ref("array"),null)),null);
assertThrows(IllegalArgumentException.class,()->LinkedRegistry.link(source(
decl("fn",new Fn(signature)),decl("array",new Array(ref("fn"))))));
assertDoesNotThrow(()->LinkedRegistry.link(source(
decl("fn",new Fn(signature)),decl("array",new Array(ref("node"))),
decl("node",new Struct(ReadOnlyList.from(new NamedType(ref("fn"),null)))))));
}
@Test void missingReferencesConflictingDeclarationsAndRemovedImportsReject() {
assertThrows(IllegalArgumentException.class,()->LinkedRegistry.link(source(decl("a",new Array(ref("missing"))))));
assertThrows(IllegalArgumentException.class,()->LinkedRegistry.link(source(decl("a",new Builtin(1)),decl("a",new Builtin(2)))));
assertThrows(IllegalArgumentException.class,()->new Builtin(0x100));
assertEquals(1,LinkedRegistry.link(source(decl("random",new Builtin(0x16)))).width(ref("random")));
}
@Test void namesAreStrictUtf8AndBounded() {
assertThrows(IllegalArgumentException.class,()->LinkedRegistry.nameBytes("\uD800"));
assertThrows(IllegalArgumentException.class,()->LinkedRegistry.nameBytes("é".repeat(2049)));
assertEquals(4096,LinkedRegistry.nameBytes("é".repeat(2048)));
}
}
@@ -0,0 +1,188 @@
package p.studio.compiler.backend.registry;
import org.junit.jupiter.api.Test;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.models.registry.IRRegistry;
import p.studio.compiler.models.registry.IRRegistry.*;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.*;
import java.nio.file.*;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
class PbxRegistryConformanceTest {
private static byte[] instructions(int[]... instructions) {
var out=new java.io.ByteArrayOutputStream();
for(var instruction:instructions) {
out.write(instruction[0]);out.write(instruction[0]>>>8);
for(int n=1;n<instruction.length;n++)for(int b=0;b<4;b++)out.write(instruction[n]>>>(8*b));
}
return out.toByteArray();
}
private static int[] op(int... values) { return values; }
private static final BytecodePreloadVerifierService VERIFIER = new BytecodePreloadVerifierService();
private static IRRegistry registry(int builtin) {
return new IRRegistry(ReadOnlyList.from(new TypeDeclaration(TypeRef.builtin(builtin), null, new Builtin(builtin))), null);
}
private static byte[] hex(String value) { return HexFormat.of().parseHex(value.replaceAll("\\s", "")); }
private static BytecodeModule module(IRRegistry registry, byte[] code, int parameters, int results, int max, FunctionMetadata metadata) {
return new BytecodeModule(0, null, ReadOnlyList.from(new BytecodeModule.FunctionMeta(0, code.length, parameters, 0, results, max, metadata)), code, null, null, null, registry);
}
private static BytecodeModule emptyFunction(byte[] code, int max) {
return module(registry(0x13), code, 0, 0, max, FunctionMetadata.empty());
}
private static void fixture(String name, BytecodeModule module) throws Exception {
Path directory=Path.of("build/registry-conformance"); Files.createDirectories(directory);
Files.write(directory.resolve(name+".pbx"), module.serialize());
}
private static byte[] section(byte[] bytes, int kind) {
var b=ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
for(int i=0;i<b.getInt(8);i++) {int at=32+12*i; if(b.getInt(at)==kind)return Arrays.copyOfRange(bytes,b.getInt(at+4),b.getInt(at+4)+b.getInt(at+8));}
throw new AssertionError("missing section "+kind);
}
@Test void matchesIndependent194ByteOracle() throws Exception {
var module=emptyFunction(hex("01 00"),0);
String source=Files.readString(Path.of("src/test/resources/registry/minimal-runtime-v0.hex"));
byte[] expected=hex(source.substring(source.indexOf('\n')+1));
assertEquals(194, expected.length);
assertArrayEquals(expected,module.serialize());
VERIFIER.verify(module);fixture("minimal",module);
}
@Test void vec2SignatureHasLogicalArityAndNoWireWidthCaches() throws Exception {
var type=TypeRef.builtin(2);
var metadata=FunctionMetadata.ordinary(new Signature(ReadOnlyList.from(new NamedType(type,null)),ReadOnlyList.from(type)));
var module=module(registry(2),hex("42 00 00 00 00 00 42 00 01 00 00 00 51 00"),2,2,2,metadata);
assertArrayEquals(hex("01 00 00 00 00 00 01 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 0e 00 00 00 00 00 00 00 02 00 00 00"),section(module.serialize(),1));
VERIFIER.verify(module);fixture("vec2-signature",module);
assertThrows(BytecodeMarshalingException.class,()->module(registry(2),module.code(),1,1,2,metadata).serialize());
}
@Test void absoluteJumpToFunctionEndAndUnreachableOperands() throws Exception {
var module=emptyFunction(hex("02 00 08 00 00 00 00 00"),0);
VERIFIER.verify(module);fixture("jump-end",module);
assertThrows(BytecodeMarshalingException.class,()->VERIFIER.verify(emptyFunction(hex("02 00 03 00 00 00 01 00"),0)));
assertThrows(BytecodeMarshalingException.class,()->VERIFIER.verify(emptyFunction(hex("01 00 50 00 ff ff ff ff"),0)));
for(int op:new int[]{0x54,0x55,0x56,0x70})assertThrows(RuntimeException.class,()->VERIFIER.verify(emptyFunction(new byte[]{1,0,(byte)op,0,0,0,0,0},0)));
}
@Test void literalZeroAndExactReturnHeight() {
assertThrows(BytecodeMarshalingException.class,()->VERIFIER.verify(emptyFunction(hex("17 00 00 00 00 00 11 00 51 00"),0)));
assertThrows(BytecodeMarshalingException.class,()->VERIFIER.verify(emptyFunction(hex("17 00 00 00 00 00 51 00"),1)));
assertDoesNotThrow(()->VERIFIER.verify(emptyFunction(hex("17 00 00 00 00 00 11 00 51 00"),1)));
}
@Test void nonfiniteUnreachableImmediateAndConstantReject() {
assertThrows(BytecodeMarshalingException.class,()->VERIFIER.verify(emptyFunction(hex("01 00 15 00 00 00 00 00 00 00 f0 7f"),0)));
var m=emptyFunction(hex("01 00"),0);
var bad=new BytecodeModule(0,ReadOnlyList.from(new BytecodeModule.Float64Constant(Double.NaN)),m.functions(),m.code(),null,null,null,m.registry());
assertThrows(BytecodeMarshalingException.class,bad::serialize);
}
@Test void signatureNamesAreIndependentFromDebugAndStrictUtf8() {
var metadata=new FunctionMetadata(Signature.empty(),null,Role.NONE,"");
var module=module(registry(0x13),hex("01 00"),0,0,0,metadata);
byte[] function=section(module.serialize(),1);
assertEquals(1,function[5]);assertEquals(34,function.length);
var bad=new FunctionMetadata(Signature.empty(),null,Role.NONE,"\uD800");
assertThrows(BytecodeMarshalingException.class,()->module(registry(0x13),hex("01 00"),0,0,0,bad).serialize());
}
@Test void unusedAndConflictingNativeDeclarationsRejectWithoutBindingFallback() {
var m=emptyFunction(hex("01 00"),0);
var declaration=new BytecodeModule.SyscallDecl("test","native",0,0,0);
var unused=new BytecodeModule(0,null,m.functions(),m.code(),null,null,ReadOnlyList.from(declaration),m.registry());
assertThrows(BytecodeMarshalingException.class,()->VERIFIER.verify(unused));
var duplicate=new BytecodeModule(0,null,m.functions(),m.code(),null,null,ReadOnlyList.from(declaration,declaration),m.registry());
assertThrows(BytecodeMarshalingException.class,duplicate::serialize);
}
@Test void closuresCarryExplicitEnvironmentAndExactFnIdentity() throws Exception {
var integer=TypeRef.builtin(0x10);var fn=new TypeRef("fixture:fn");
var signature=new Signature(null,ReadOnlyList.from(integer));
var registry=new IRRegistry(ReadOnlyList.from(new TypeDeclaration(integer,null,new Builtin(0x10)),new TypeDeclaration(fn,null,new Fn(signature))),null);
int fnId=LinkedRegistry.link(registry).id(fn).index();
byte[] boot=instructions(op(0x17,42),op(0x52,1,fnId),op(0x53,fnId),op(1));
byte[] body=instructions(op(0x57,0),op(0x51));
var bytes=new java.io.ByteArrayOutputStream();bytes.writeBytes(boot);bytes.writeBytes(body);
var functions=ReadOnlyList.from(new BytecodeModule.FunctionMeta(0,boot.length,0,0,0,1,FunctionMetadata.empty()),
new BytecodeModule.FunctionMeta(boot.length,body.length,0,0,1,1,new FunctionMetadata(signature,ReadOnlyList.from(integer),Role.NONE)));
var m=new BytecodeModule(0,null,functions,bytes.toByteArray(),null,null,null,registry);
VERIFIER.verify(m);fixture("closure-capture",m);
byte[] direct=m.code().clone();direct[6]=0x50;
var invalid=new BytecodeModule(0,null,functions,direct,null,null,null,registry);
assertThrows(BytecodeMarshalingException.class,()->VERIFIER.verify(invalid));
}
@Test void traitImplementationUsesConcreteReceiverAndOrderedMethods() throws Exception {
var integer=TypeRef.builtin(0x10);var owner=new TypeRef("fixture:owner");var trait=new TypeRef("fixture:trait");
var result=new Signature(null,ReadOnlyList.from(integer));
var registry=new IRRegistry(ReadOnlyList.from(new TypeDeclaration(integer,null,new Builtin(0x10)),
new TypeDeclaration(owner,null,new Struct(null)),new TypeDeclaration(trait,null,new Trait(ReadOnlyList.from(new Method("answer",result))))),
ReadOnlyList.from(new Implementation("fixture:impl",owner,trait,ReadOnlyList.from(new p.studio.compiler.source.identifiers.CallableId(1)))));
var linked=LinkedRegistry.link(registry);int ownerId=linked.id(owner).index(),traitId=linked.id(trait).index();
byte[] boot=instructions(op(0xa0,ownerId),op(0x95,ownerId,traitId),op(0x58,traitId,0),op(1));
byte[] body=instructions(op(0x17,42),op(0x51));
var bytes=new java.io.ByteArrayOutputStream();bytes.writeBytes(boot);bytes.writeBytes(body);
var implementation=FunctionMetadata.ordinary(new Signature(ReadOnlyList.from(new NamedType(owner,"self")),ReadOnlyList.from(integer)));
var m=new BytecodeModule(0,null,ReadOnlyList.from(new BytecodeModule.FunctionMeta(0,boot.length,0,0,0,2,FunctionMetadata.empty()),
new BytecodeModule.FunctionMeta(boot.length,body.length,1,0,1,1,implementation)),bytes.toByteArray(),null,null,null,registry);
VERIFIER.verify(m);fixture("trait-call",m);
}
@Test void arrayAndDynamicOperationsUseRegistryOperands() throws Exception {
var integer=TypeRef.builtin(0x10);var array=new TypeRef("fixture:array");
var registry=new IRRegistry(ReadOnlyList.from(new TypeDeclaration(integer,null,new Builtin(0x10)),new TypeDeclaration(array,null,new Array(integer))),null);
var linked=LinkedRegistry.link(registry);int arrayId=linked.id(array).index(),intId=linked.id(integer).index();
byte[] code=instructions(op(0x14,1,0),op(0xa3,arrayId),op(0x14,0,0),op(0xa5,arrayId),op(0x90,intId),op(0x91,intId),op(1));
var m=module(registry,code,0,0,2,FunctionMetadata.empty());
VERIFIER.verify(m);fixture("array-dynamic",m);
}
@Test void bindingFixturesKeepExactCanonicalIdentityAndAbi() throws Exception {
var m=emptyFunction(instructions(op(0x71,0),op(0x11),op(1)),1);
for(String name:List.of("native", "unknown-native", "native-abi", "unused-native")) {
var declaration=new BytecodeModule.SyscallDecl("system",name.equals("unknown-native")?"missing":"has_cart",1,name.equals("native-abi")?1:0,1);
byte[] code=name.equals("unused-native")?instructions(op(1)):m.code();
var functions=ReadOnlyList.from(new BytecodeModule.FunctionMeta(0,code.length,0,0,0,1,FunctionMetadata.empty()));
var module=new BytecodeModule(0,null,functions,code,null,null,ReadOnlyList.from(declaration),m.registry());
fixture(name,module);
if(name.equals("native"))VERIFIER.verify(module);
}
}
@Test void rolesStayMetadataAndValidateSignatureAndUniqueness() throws Exception {
var pause=new FunctionMetadata(Signature.empty(),null,Role.BEFORE_PAUSE);
var resume=new FunctionMetadata(Signature.empty(),null,Role.BEFORE_RESUME);
var functions=ReadOnlyList.from(new BytecodeModule.FunctionMeta(0,2,0,0,0,0,FunctionMetadata.empty()),
new BytecodeModule.FunctionMeta(2,2,0,0,0,0,pause),new BytecodeModule.FunctionMeta(4,2,0,0,0,0,resume));
var m=new BytecodeModule(0,null,functions,instructions(op(1),op(0x51),op(0x51)),null,null,null,IRRegistry.empty());
VERIFIER.verify(m);fixture("roles",m);
assertThrows(BytecodeMarshalingException.class,()->module(IRRegistry.empty(),instructions(op(1)),0,0,0,pause).serialize());
var duplicated=ReadOnlyList.from(functions.get(0),functions.get(1),new BytecodeModule.FunctionMeta(4,2,0,0,0,0,pause));
assertThrows(BytecodeMarshalingException.class,()->new BytecodeModule(0,null,duplicated,m.code(),null,null,null,m.registry()).serialize());
}
@Test void joinsWidenFactsAndLoopsReachFixedPoint() throws Exception {
// bool; branch to alternate scalar; equal-height join becomes Unknown, then POP.
byte[] code=instructions(op(0x10,0),op(0x03,24),op(0x17,1),op(0x02,30),op(0x10,1),op(0x11),op(1));
var base=emptyFunction(code,1);
var m=new BytecodeModule(0,ReadOnlyList.from(new BytecodeModule.BooleanConstant(false),new BytecodeModule.StringConstant("alternate")),base.functions(),code,null,null,null,base.registry());
VERIFIER.verify(m);fixture("join",m);
byte[] loop=instructions(op(0x40,0),op(0x03,18),op(0x02,0),op(1));
assertDoesNotThrow(()->VERIFIER.verify(emptyFunction(loop,1)));
assertThrows(BytecodeMarshalingException.class,()->VERIFIER.verify(emptyFunction(hex("16 00 01 16 00 00 20 00 01 00"),2)));
}
@Test void memcardRoleHasExactTypedSignatureWithoutLanguagePolicy() throws Exception {
var input=TypeRef.builtin(0x11);var output=TypeRef.builtin(0x10);
var registry=new IRRegistry(ReadOnlyList.from(new TypeDeclaration(input,null,new Builtin(0x11)),new TypeDeclaration(output,null,new Builtin(0x10))),null);
var metadata=new FunctionMetadata(new Signature(ReadOnlyList.from(new NamedType(input,"status")),ReadOnlyList.from(output)),null,Role.MEMCARD_BOOT);
var code=instructions(op(1),op(0x17,0),op(0x51));
var functions=ReadOnlyList.from(new BytecodeModule.FunctionMeta(0,2,0,0,0,0,FunctionMetadata.empty()),new BytecodeModule.FunctionMeta(2,8,1,0,1,1,metadata));
var module=new BytecodeModule(0,null,functions,code,null,null,null,registry);
VERIFIER.verify(module);fixture("memcard-role",module);
}
@Test void knownSemanticGroupsCannotSilentlyChangeType() {
var color=TypeRef.builtin(1);var integer=TypeRef.builtin(0x11);
var registry=new IRRegistry(ReadOnlyList.from(new TypeDeclaration(color,null,new Builtin(1)),new TypeDeclaration(integer,null,new Builtin(0x11))),null);
var metadata=FunctionMetadata.ordinary(new Signature(ReadOnlyList.from(new NamedType(color,null)),ReadOnlyList.from(integer)));
var module=module(registry,instructions(op(0x42,0),op(0x51)),1,1,1,metadata);
assertThrows(BytecodeMarshalingException.class,()->VERIFIER.verify(module));
}
@Test void intrinsicFixtureUsesExplicitAbiAndKeepsNativeBindingSeparate() throws Exception {
// Runtime vec2.dot@1 (0x1000): four Float carriers -> one Float.
byte[] code=instructions(op(0x15,0,0x3ff00000),op(0x15,0,0x40000000),
op(0x15,0,0x40080000),op(0x15,0,0x40100000),op(0x72,0x1000),op(1));
var base=emptyFunction(code,4);
var module=new BytecodeModule(0,null,base.functions(),code,null,null,null,base.registry(),ReadOnlyList.from(new BytecodeModule.IntrinsicSignature(0x1000,4,1)));
VERIFIER.verify(module);fixture("intrinsic-dot",module);
}
}
@@ -0,0 +1,46 @@
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 static org.junit.jupiter.api.Assertions.*;
class TypedCommonPipelineTest {
private static IRBackendExecutableFunction.Instruction plain(IRBackendExecutableFunction.InstructionKind kind, String literal) {
return new IRBackendExecutableFunction.Instruction(kind,null,null,null,null,null,literal,null,null,null,null);
}
private static IRBackendExecutableFunction.Instruction typed(IRRegistryInstruction operand) {
return new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.REGISTRY,null,null,null,null,null,null,null,null,null,null,operand);
}
@Test void syntheticFrontendClosureSurvivesLinkLowerOptimizeAndEmit() throws Exception {
var integer=TypeRef.builtin(0x10);var fn=new TypeRef("synthetic:fn");
var signature=new Signature(null,ReadOnlyList.from(integer));
var registry=new IRRegistry(ReadOnlyList.from(new TypeDeclaration(integer,null,new Builtin(0x10)),new TypeDeclaration(fn,null,new Fn(signature))),null);
var capture=new CallableId(71);
var main=new IRBackendExecutableFunction(new FileId(0),new ModuleId(0),"main",new CallableId(91),0,0,0,0,0,1,
ReadOnlyList.from(plain(IRBackendExecutableFunction.InstructionKind.PUSH_I32,"42"),
typed(new IRRegistryInstruction(IRRegistryInstruction.Kind.MAKE_CLOSURE,fn,null,capture,0)),
typed(new IRRegistryInstruction(IRRegistryInstruction.Kind.CALL_CLOSURE,fn,null,null,0)),
plain(IRBackendExecutableFunction.InstructionKind.POP,null),plain(IRBackendExecutableFunction.InstructionKind.RET,null)),null,FunctionMetadata.empty());
var body=new IRBackendExecutableFunction(new FileId(0),new ModuleId(0),"captured",capture,0,0,0,0,1,1,
ReadOnlyList.from(typed(new IRRegistryInstruction(IRRegistryInstruction.Kind.GET_CAPTURE,null,null,null,0)),
plain(IRBackendExecutableFunction.InstructionKind.RET,null)),null,new FunctionMetadata(signature,ReadOnlyList.from(integer),Role.NONE));
var backend=IRBackend.builder().entryPointModuleId(new ModuleId(0)).registry(registry).executableFunctions(ReadOnlyList.from(body,main)).build();
var lowered=new LowerToIRVMService().lower(backend);
var optimized=new OptimizeIRVMService().optimize(lowered);
assertEquals(registry,optimized.module().registry());
assertEquals(body.metadata(),optimized.module().functions().get(1).metadata());
var module=new BytecodeEmitter().emit(optimized.coherentEmissionPlan());
new BytecodePreloadVerifierService().verify(module);
var repeated=new BytecodeEmitter().emit(new OptimizeIRVMService().optimize(new LowerToIRVMService().lower(backend)).coherentEmissionPlan());
assertArrayEquals(module.serialize(),repeated.serialize());
Files.createDirectories(Path.of("build/registry-conformance"));
Files.write(Path.of("build/registry-conformance/common-pipeline.pbx"),module.serialize());
}
}
@@ -142,9 +142,10 @@ class BackendGateIIntegrationTest {
void gateI_validIntrinsicPath() {
final var module = emitFromBackend(backendWithSingleFunction(
fn("main", ReadOnlyList.from(
callIntrinsic("input.pad", 1, 0),
callIntrinsic("input.pad.a.down", 1, 0),
pop(),
ret())),
ReadOnlyList.from(new IntrinsicReference("input.pad", 1))));
ReadOnlyList.from(new IntrinsicReference("input.pad.a.down", 1))));
final var check = compatibilityAdapter.check(module);
assertEquals(CompatibilityError.NONE, check.error());
@@ -199,6 +200,10 @@ class BackendGateIIntegrationTest {
Span.none());
}
private static IRBackendExecutableFunction.Instruction pop() {
return new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.POP, "", null, null, Span.none());
}
private IRBackendExecutableFunction.Instruction ret() {
return new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.RET,
@@ -232,7 +237,7 @@ class BackendGateIIntegrationTest {
null,
new IRBackendExecutableFunction.IntrinsicCallMetadata(canonicalName, canonicalVersion, new IntrinsicId(intrinsicId)),
0,
0,
1,
Span.none());
}

Some files were not shown because too many files have changed in this diff Show More