diff --git a/discussion/lessons/DSC-0075-compiler-runtime-catalog-parity/LSN-0080-manual-runtime-catalog-port-and-conformance.md b/discussion/lessons/DSC-0075-compiler-runtime-catalog-parity/LSN-0080-manual-runtime-catalog-port-and-conformance.md new file mode 100644 index 00000000..999e101e --- /dev/null +++ b/discussion/lessons/DSC-0075-compiler-runtime-catalog-parity/LSN-0080-manual-runtime-catalog-port-and-conformance.md @@ -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. diff --git a/docs/compiler/general/specs/runtime-catalog-support.md b/docs/compiler/general/specs/runtime-catalog-support.md new file mode 100644 index 00000000..707ab614 --- /dev/null +++ b/docs/compiler/general/specs/runtime-catalog-support.md @@ -0,0 +1,95 @@ +# 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. diff --git a/docs/vm-arch/BUILTIN_CONSTANTS.csv b/docs/vm-arch/BUILTIN_CONSTANTS.csv new file mode 100644 index 00000000..49267915 --- /dev/null +++ b/docs/vm-arch/BUILTIN_CONSTANTS.csv @@ -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 diff --git a/docs/vm-arch/INTRINSICS.csv b/docs/vm-arch/INTRINSICS.csv index 33779d1e..1c93cd0d 100644 --- a/docs/vm-arch/INTRINSICS.csv +++ b/docs/vm-arch/INTRINSICS.csv @@ -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 diff --git a/docs/vm-arch/README.md b/docs/vm-arch/README.md index 84656e40..0ac3a7f6 100644 --- a/docs/vm-arch/README.md +++ b/docs/vm-arch/README.md @@ -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). diff --git a/docs/vm-arch/runtime-catalog.properties b/docs/vm-arch/runtime-catalog.properties new file mode 100644 index 00000000..b192a0b6 --- /dev/null +++ b/docs/vm-arch/runtime-catalog.properties @@ -0,0 +1,3 @@ +runtime.revision=f7d7c167531bb662a1af9d73fc326bb476bf8aa7 +INTRINSICS.csv.sha256=0c1bb78a9b81d1e3480ef5713f3d70f0b06772a7dfab2fd00c1f0a6364f98daa +BUILTIN_CONSTANTS.csv.sha256=e407e4718127aa7ef7b1097b5b4b67d86dd979235e73a551a7c36b628cddc039 diff --git a/prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeProbe.java b/prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeProbe.java index ccb0b21b..043a38a7 100644 --- a/prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeProbe.java +++ b/prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeProbe.java @@ -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(); + 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"); diff --git a/prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeTest.java b/prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeTest.java index 2842652f..df63350b 100644 --- a/prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeTest.java +++ b/prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeTest.java @@ -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(); } } } diff --git a/prometeu-compiler/frontends/prometeu-frontend-pbs/build.gradle.kts b/prometeu-compiler/frontends/prometeu-frontend-pbs/build.gradle.kts index a7adba65..98203d7d 100644 --- a/prometeu-compiler/frontends/prometeu-frontend-pbs/build.gradle.kts +++ b/prometeu-compiler/frontends/prometeu-frontend-pbs/build.gradle.kts @@ -54,3 +54,10 @@ val generatePbsKnowledge by tasks.registering(Sync::class) { } } sourceSets.main { resources.srcDir(generatePbsKnowledge) } + +tasks.named("processResources") { + from(rootProject.layout.projectDirectory.dir("docs/vm-arch")) { + include("INTRINSICS.csv", "BUILTIN_CONSTANTS.csv", "runtime-catalog.properties") + into("runtime-catalog/pbs-sdk") + } +} diff --git a/prometeu-compiler/prometeu-build-pipeline/build.gradle.kts b/prometeu-compiler/prometeu-build-pipeline/build.gradle.kts index 4ad19ef0..313875b3 100644 --- a/prometeu-compiler/prometeu-build-pipeline/build.gradle.kts +++ b/prometeu-compiler/prometeu-build-pipeline/build.gradle.kts @@ -9,3 +9,11 @@ dependencies { 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") { + from(rootProject.layout.projectDirectory.dir("docs/vm-arch")) { + include("INTRINSICS.csv", "BUILTIN_CONSTANTS.csv", "runtime-catalog.properties") + into("runtime-catalog/compiler") + } +} diff --git a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodeEmitter.java b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodeEmitter.java index 5b7e61e0..46164500 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodeEmitter.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodeEmitter.java @@ -186,7 +186,8 @@ public class BytecodeEmitter { spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span()))); } case INTRINSIC -> { - if(op.expectedArgSlots()==null||op.expectedRetSlots()==null) throw new IllegalArgumentException("intrinsic signature is required"); + 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"); diff --git a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodePreloadVerifierService.java b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodePreloadVerifierService.java index 56e26a5e..9ec8246f 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodePreloadVerifierService.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodePreloadVerifierService.java @@ -1,6 +1,8 @@ package p.studio.compiler.backend.bytecode; 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.*; @@ -18,6 +20,11 @@ public class BytecodePreloadVerifierService { // Validate the complete declarative schema without accepting an old slot-only image. module.serialize(); var registry=LinkedRegistry.link(module.registry()); + var intrinsicIds = new HashSet(); + for (var signature : module.intrinsicSignatures()) { + check(intrinsicIds.add(signature.id()), "duplicate intrinsic signature"); + IRVMIntrinsicRegistry.requireById(signature.id()).validateSignature(signature.argumentSlots(), signature.resultSlots()); + } var used=new boolean[module.syscalls().size()]; for(int i=0;i check(a>=0&&(long)a {check(a>=0&&a {check(a>=0&&a check(m.intrinsicSignatures().stream().anyMatch(n->n.id()==a&&n.argumentSlots()>=0&&n.resultSlots()>=0),"intrinsic signature required"); + case 0x72 -> { + IRVMIntrinsicRegistry.requireById(a); + check(m.intrinsicSignatures().stream().anyMatch(n->n.id()==a),"intrinsic signature required"); + } case 0x52 -> {check(a>=0&&a fn(r,a); case 0x57 -> check(a>=0&&a {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 { - var signature=m.intrinsicSignatures().stream().filter(n->n.id()==a).findFirst().orElseThrow(()->new IllegalArgumentException("intrinsic signature required")); - pop(s,signature.argumentSlots());for(int n=0;n { consume(s,facts(r,type(r,a).identity()),typed); @@ -179,6 +190,21 @@ public class BytecodePreloadVerifierService { } 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 intrinsicFacts(List 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 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 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;} @@ -193,7 +219,7 @@ public class BytecodePreloadVerifierService { 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|64;case "ref"->32|64;case "null"->64;default->127; + 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; } diff --git a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMBuiltinConstantDefinition.java b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMBuiltinConstantDefinition.java new file mode 100644 index 00000000..28a6e050 --- /dev/null +++ b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMBuiltinConstantDefinition.java @@ -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 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 slots() { return slots; } + + public static Optional 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 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); + } + } +} diff --git a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMIntrinsicDefinition.java b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMIntrinsicDefinition.java index 36ad35a5..577fd449 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMIntrinsicDefinition.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMIntrinsicDefinition.java @@ -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 argumentCarriers; + private final java.util.List 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 argumentCarriers() { return argumentCarriers; } + public java.util.List 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 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) { diff --git a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMIntrinsicRegistry.java b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMIntrinsicRegistry.java index 151b70cf..12d9afd7 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMIntrinsicRegistry.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMIntrinsicRegistry.java @@ -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 FINAL_ID_BY_INTRINSIC = loadRegistry(); + private static final Map DEFINITION_BY_ID = definitionsById(); private IRVMIntrinsicRegistry() { } + public static Optional 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 definitionsById() { + var result = new HashMap(); + 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); } } diff --git a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMProgram.java b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMProgram.java index df68a9fe..be9c50b9 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMProgram.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMProgram.java @@ -210,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()); }; } diff --git a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMValidationErrorCode.java b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMValidationErrorCode.java index 1dcdb7f8..c8af6cd3 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMValidationErrorCode.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMValidationErrorCode.java @@ -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, diff --git a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMValidator.java b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMValidator.java index 75ff85aa..25142306 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMValidator.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/IRVMValidator.java @@ -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()); diff --git a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/LowerToIRVMService.java b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/LowerToIRVMService.java index 67d65003..31df2bfc 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/LowerToIRVMService.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/irvm/LowerToIRVMService.java @@ -345,8 +345,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, @@ -354,11 +354,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; } diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/GoldenArtifactsTest.java b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/GoldenArtifactsTest.java index 6d5a926c..222355dd 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/GoldenArtifactsTest.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/GoldenArtifactsTest.java @@ -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(); } diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/bytecode/BytecodeEmitterTest.java b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/bytecode/BytecodeEmitterTest.java index 87c6ae71..a9d10f06 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/bytecode/BytecodeEmitterTest.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/bytecode/BytecodeEmitterTest.java @@ -106,12 +106,12 @@ class BytecodeEmitterTest { 0, 1, ReadOnlyList.from( - BytecodeEmitter.Operation.intrinsic(0x2000, 0, 0, null), + 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, 0, 0, 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()); diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/IRVMIntrinsicRegistryParityTest.java b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/IRVMIntrinsicRegistryParityTest.java index e73252cf..efeddc3c 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/IRVMIntrinsicRegistryParityTest.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/IRVMIntrinsicRegistryParityTest.java @@ -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(); + var ids = new HashSet(); + var identities = new HashSet(); + 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(); + var identities = new HashSet(); + 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 parseVmArchIntrinsicRows(final Path specPath) throws IOException { - final var lines = Files.readAllLines(specPath, StandardCharsets.UTF_8); - final var rows = new ArrayList(); - 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 parseRuntimeIntrinsicSnapshot(final Path runtimeFile) throws IOException { - final var runtimeSource = Files.readAllLines(runtimeFile, StandardCharsets.UTF_8); - final var mapped = new LinkedHashMap(); - 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 runtimeSnapshot, - final Map compilerSnapshot) { - final var missingInCompiler = new ArrayList(); - final var missingInRuntime = new ArrayList(); - final var mismatchedIds = new ArrayList(); - 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); } } diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/IRVMValidatorTest.java b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/IRVMValidatorTest.java index 0fc0a017..16a84073 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/IRVMValidatorTest.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/IRVMValidatorTest.java @@ -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); diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/LowerToIRVMServiceTest.java b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/LowerToIRVMServiceTest.java index 4fed8007..02a36dd4 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/LowerToIRVMServiceTest.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/LowerToIRVMServiceTest.java @@ -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()); } diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/OptimizeIRVMEquivalenceHarnessTest.java b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/OptimizeIRVMEquivalenceHarnessTest.java index 0cdf3b88..6b0aa63a 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/OptimizeIRVMEquivalenceHarnessTest.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/OptimizeIRVMEquivalenceHarnessTest.java @@ -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; diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/RuntimeCatalogPipelineTest.java b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/RuntimeCatalogPipelineTest.java new file mode 100644 index 00000000..0aef9cd5 --- /dev/null +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/irvm/RuntimeCatalogPipelineTest.java @@ -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(); + for (var definition : IRVMBuiltinConstantDefinition.values()) pool.addAll(definition.materialize().asList()); + var ops = new ArrayList(); + 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 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 instructions = new ArrayList<>(); + final List 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(); + } + } +} diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/integration/BackendGateIIntegrationTest.java b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/integration/BackendGateIIntegrationTest.java index 7fb814b0..4aed69e3 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/integration/BackendGateIIntegrationTest.java +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/integration/BackendGateIIntegrationTest.java @@ -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()); } diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/resources/golden/bytecode-module.hex b/prometeu-compiler/prometeu-build-pipeline/src/test/resources/golden/bytecode-module.hex index 5c8c6246..15fbb875 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/test/resources/golden/bytecode-module.hex +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/resources/golden/bytecode-module.hex @@ -1 +1 @@ -5042580000000000090000000000000000000000000000000000000000000000000000008c0000000400000001000000900000001e00000002000000ae0000000e00000003000000bc0000004400000004000000000100000400000005000000040100001d000000060000002101000004000000070000002501000004000000080000002901000008000000000000000100000000000000000000000000000000000e000000000000000200000071000000000072000020000051000300000000000000010000000000000000000000060000000100000000000000000000000c0000000100000000000000000000000100000000000000040000006d61696e0000000001000000050067667832640a00647261775f706978656c01000000000000000000000000000100000000000000 +5042580000000000090000000000000000000000000000000000000000000000000000008c0000000400000001000000900000001e00000002000000ae0000001000000003000000be0000005400000004000000120100000400000005000000160100001d000000060000003301000004000000070000003701000004000000080000003b0100000800000000000000010000000000000000000000000000000000100000000000000002000000710000000000720012210000110051000400000000000000010000000000000000000000060000000100000000000000000000000c0000000100000000000000000000000e0000000100000000000000000000000100000000000000040000006d61696e0000000001000000050067667832640a00647261775f706978656c01000000000000000000000000000100000000000000 diff --git a/prometeu-compiler/prometeu-build-pipeline/src/test/resources/golden/irvm-program.txt b/prometeu-compiler/prometeu-build-pipeline/src/test/resources/golden/irvm-program.txt index 22087570..863485df 100644 --- a/prometeu-compiler/prometeu-build-pipeline/src/test/resources/golden/irvm-program.txt +++ b/prometeu-compiler/prometeu-build-pipeline/src/test/resources/golden/irvm-program.txt @@ -1,5 +1,6 @@ profile=core-v1 fn#0:main param=0 local=0 ret=0 max=2 HOSTCALL imm=0 - INTRINSIC imm=8192 + INTRINSIC imm=8466 + POP imm=- RET imm=- diff --git a/tools/pbx-conformance/README.md b/tools/pbx-conformance/README.md index 29e17715..2503eae2 100644 --- a/tools/pbx-conformance/README.md +++ b/tools/pbx-conformance/README.md @@ -6,7 +6,7 @@ it does not implement a Java approximation of runtime admission. From the Studio root: ```sh -./gradlew :prometeu-compiler:prometeu-build-pipeline:test --tests '*LinkedRegistryTest' --tests '*PbxRegistryConformanceTest' --tests '*TypedCommonPipelineTest' +./gradlew :prometeu-compiler:prometeu-build-pipeline:test --tests '*LinkedRegistryTest' --tests '*PbxRegistryConformanceTest' --tests '*TypedCommonPipelineTest' --tests '*RuntimeCatalogPipelineTest' python3 tools/pbx-conformance/run.py --runtime ../runtime ``` @@ -45,3 +45,20 @@ remove the exception once the schema is corrected. This corpus qualifies common backend production, not PBS lowering, lifecycle selection, memcard persistence or installed-runtime deployment. + +## Runtime catalog consumer fixtures + +The catalog corpus adds common-IR Random calls with an aliased handle, a discarded +first result, mixed next/nextf/range calls and a subsequent next. The VM checks the +independent seed-42 handoff vector and identical charged work before/after +optimization. Constants are compared slot-by-slot against runtime materialization, +including exact Float bits and both slots of `vec2.zero`. Pool deduplication may +share a value but cannot omit a group component. + +A known zero-argument touch query loads and traps `TRAP_UNAVAILABLE` without a +hardware context. A removed endpoint ID is rejected even after an unreachable +HALT. `color.html_rgba` executes with a String value and rejects a proven null +at load time, matching the runtime verifier's stricter rule despite the +dispatcher's nullable String carrier. These are distinct outcomes. Run the separate provider-export check with +`python3 tools/runtime-catalog/verify.py --runtime ../runtime` to compare the +selected source revision, imported CSV bytes and exporter output as well. diff --git a/tools/pbx-conformance/main.rs b/tools/pbx-conformance/main.rs index 8fb3b9bc..b088cae3 100644 --- a/tools/pbx-conformance/main.rs +++ b/tools/pbx-conformance/main.rs @@ -36,6 +36,7 @@ impl NativeInterface for NoNative { fn main() { let root = std::path::PathBuf::from(std::env::args().nth(1).expect("fixture directory")); + verify_catalog_fixtures(&root); for name in [ "minimal", "jump-end", @@ -257,6 +258,58 @@ fn main() { ); } +fn verify_catalog_fixtures(root: &std::path::Path) { + use prometeu_vm::LogicalFrameEndingReason as End; + let mut random_cycles = None; + for name in ["catalog-random-off", "catalog-random-on", "catalog-constants", "catalog-vec2-zero", "catalog-input", "catalog-string"] { + let bytes = std::fs::read(root.join(format!("{name}.pbx"))).unwrap(); + let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE) + .unwrap_or_else(|error| panic!("{name}: {error:?}")); + println!("LOAD PASS {name}"); + let mut vm = VirtualMachine::from_image(image, Default::default()); + vm.prepare_boot_call().unwrap(); + let report = vm.run_budget(10000, &mut NoNative, &mut HostContext::new(None)).unwrap(); + if name == "catalog-input" { + assert!(matches!(report.reason, End::Trap(ref trap) if trap.code == prometeu_bytecode::TRAP_UNAVAILABLE)); + println!("EXEC PASS {name}: TRAP_UNAVAILABLE (known query, no hardware)"); + continue; + } + assert!(matches!(report.reason, End::Halted | End::EndOfRom), "{name}: {report:?}"); + if name.starts_with("catalog-random-") { + // Independent seed-42 vector and unit-range consumption from the provider handoff/tests. + assert_eq!(vm.operand_stack_top(3), vec![ + Value::Int64(0xF9090E529A7DAE00u64 as i64), Value::Int64(-7), + Value::Float(f64::from_bits(0x3FB304AA46C98538)), + ], "{name}: aliasing, discarded first next, mixed operations and exact order"); + if let Some(cycles) = random_cycles { assert_eq!(report.cycles_used, cycles, "optimization changed charged work"); } + random_cycles = Some(report.cycles_used); + } else if name == "catalog-vec2-zero" { + assert_eq!(vm.operand_stack_top(2), vec![Value::Float(0.0), Value::Float(0.0)]); + } else if name == "catalog-string" { + assert_eq!(vm.operand_stack_top(1), vec![Value::Int64(0xAABBCCDD)]); + } else { + let mut constants: Vec<_> = prometeu_vm::builtin_constant_catalog().collect(); + constants.sort_by_key(|constant| constant.key.key()); + let mut expected = Vec::new(); + for constant in constants { expected.extend(constant.materialize().unwrap()); } + expected.reverse(); + let actual = vm.operand_stack_top(expected.len()); + assert_eq!(actual.len(), 16); + for (actual, expected) in actual.iter().zip(expected.iter()) { + match (actual, expected) { + (Value::Float(a), Value::Float(b)) => assert_eq!(a.to_bits(), b.to_bits()), + _ => assert_eq!(actual, expected), + } + } + } + println!("EXEC PASS {name}: {} cycles, {} steps", report.cycles_used, report.steps_executed); + } + let removed = std::fs::read(root.join("catalog-removed.pbx")).unwrap(); + reject(root, "catalog-removed", &removed, LoadLimits::default()); + let null_string = std::fs::read(root.join("catalog-null-string.pbx")).unwrap(); + reject(root, "catalog-null-string", &null_string, LoadLimits::default()); +} + fn reject(root: &std::path::Path, name: &str, bytes: &[u8], limits: LoadLimits) { let error = ValidatedProgramImage::load(bytes, &mut LoadBudget::new(limits), caps::NONE).unwrap_err(); diff --git a/tools/runtime-catalog/verify.py b/tools/runtime-catalog/verify.py new file mode 100644 index 00000000..cb394e21 --- /dev/null +++ b/tools/runtime-catalog/verify.py @@ -0,0 +1,44 @@ +"""Explicit provider check; ordinary Studio compilation does not invoke this tool.""" +import argparse +import hashlib +import pathlib +import platform +import subprocess +import tempfile + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--runtime", type=pathlib.Path, required=True) + args = parser.parse_args() + studio = pathlib.Path(__file__).resolve().parents[2] + mirror = studio / "docs/vm-arch" + properties = dict(line.split("=", 1) for line in + (mirror / "runtime-catalog.properties").read_text().splitlines() if line) + runtime = args.runtime.resolve(strict=True) + revision = subprocess.check_output(["git", "-C", str(runtime), "rev-parse", "HEAD"], text=True).strip() + if revision != properties["runtime.revision"]: + raise SystemExit(f"Expected selected provider {properties['runtime.revision']}, found {revision}; " + "this is an evidence-target mismatch, not a runtime compatibility verdict") + # Evidence must correspond to committed source, not just two matching local copies. + dirty = subprocess.check_output(["git", "-C", str(runtime), "status", "--porcelain"], text=True) + if dirty: + raise SystemExit("Provider checkout is dirty; verify the selected revision in a clean checkout") + for name in ("INTRINSICS.csv", "BUILTIN_CONSTANTS.csv"): + committed = subprocess.check_output(["git", "-C", str(runtime), "show", f"{revision}:docs/vm-arch/{name}"]) + if committed != (runtime / "docs/vm-arch" / name).read_bytes() or committed != (mirror / name).read_bytes(): + raise SystemExit(f"Provider/mirror bytes differ: {name}") + digest = hashlib.sha256(committed).hexdigest() + if digest != properties[name + ".sha256"]: + raise SystemExit(f"Provenance hash mismatch: {name}") + print(f"MATCH {name} sha256={digest}", flush=True) + print(f"Provider {revision}; {platform.platform()}; Cargo test profile, default features", flush=True) + with tempfile.TemporaryDirectory(prefix="studio-catalog-provider-") as target: + subprocess.run(["cargo", "test", "--locked", "--offline", "--target-dir", target, + "-p", "prometeu-vm", "--test", "numeric_contract", + "exported_artifacts_match_the_single_runtime_catalog", "--", "--exact"], + cwd=runtime, check=True) + + +if __name__ == "__main__": + main()