|
| 1 | +# Backend Semantic Convergence Plan |
| 2 | + |
| 3 | +**Goal:** Reduce semantic duplication across interpreter, Trace JIT, AOT, native bridge, and no-std execution by introducing one canonical instruction/operation contract and differential verification. |
| 4 | + |
| 5 | +**Architecture:** Bytecode semantics, builtin signatures, ownership rules, traps, and deoptimization outcomes are defined once. Each backend lowers or interprets the same contract. Generated coverage tables and differential fixtures detect missing or divergent implementations. |
| 6 | + |
| 7 | +**Tech Stack:** Rust 2024, interpreter, Trace JIT, Cranelift AOT, native bridge, `pd-vm-nostd`, property/differential tests. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Independence and dependency |
| 12 | + |
| 13 | +- Independent of agent framework and module loading. |
| 14 | +- Static builtin IDs should land first. |
| 15 | +- VM decomposition should define Engine/Program ownership before large backend file moves. |
| 16 | +- This plan does not block immediate correctness plans. |
| 17 | + |
| 18 | +## Scope boundary |
| 19 | + |
| 20 | +### In scope |
| 21 | + |
| 22 | +- One canonical semantic description for opcodes and builtins. |
| 23 | +- Generated backend coverage checks. |
| 24 | +- Shared ownership/trap/helper contracts. |
| 25 | +- Differential interpreter/JIT/AOT/no-std tests. |
| 26 | +- Incremental removal of duplicated lowering logic. |
| 27 | + |
| 28 | +### Out of scope |
| 29 | + |
| 30 | +- New optimization targets or benchmark promises. |
| 31 | +- New bytecode opcodes solely to simplify one backend. |
| 32 | +- A complete JIT rewrite in one milestone. |
| 33 | +- Agent, HTTP, SQLite, or gateway behavior. |
| 34 | + |
| 35 | +## Implementation route |
| 36 | + |
| 37 | +### Milestone 1: Build a backend coverage inventory |
| 38 | + |
| 39 | +**Files:** |
| 40 | +- Create backend coverage tests/tools under `tests/` or `src/backend/` |
| 41 | +- Read interpreter, JIT recorder/lowerer, AOT IR/lowerer, no-std dispatch |
| 42 | + |
| 43 | +Generate a matrix for every opcode/builtin: |
| 44 | + |
| 45 | +```text |
| 46 | +semantic definition |
| 47 | +interpreter |
| 48 | +trace recorder |
| 49 | +JIT lowering |
| 50 | +AOT lowering |
| 51 | +no-std |
| 52 | +fallback/deopt rule |
| 53 | +``` |
| 54 | + |
| 55 | +Fail CI when a newly added operation lacks an explicit backend disposition. |
| 56 | + |
| 57 | +### Milestone 2: Define canonical operation semantics |
| 58 | + |
| 59 | +**Files:** |
| 60 | +- Create: `src/semantics/` or equivalent |
| 61 | +- Modify opcode/builtin metadata generation |
| 62 | + |
| 63 | +Represent: |
| 64 | + |
| 65 | +- operand/result types and stack effect; |
| 66 | +- ownership/borrow/clone/drop behavior; |
| 67 | +- trap/error conditions; |
| 68 | +- side-effect and suspension classification; |
| 69 | +- interpreter helper and native helper ABI; |
| 70 | +- deopt/fallback permission. |
| 71 | + |
| 72 | +Keep explicit Rust implementation hooks where declarative metadata is insufficient. |
| 73 | + |
| 74 | +### Milestone 3: Generate shared dispatch metadata |
| 75 | + |
| 76 | +1. Generate interpreter validation/stack-effect tables. |
| 77 | +2. Generate JIT/AOT eligibility and helper IDs. |
| 78 | +3. Generate no-std support/fallback declarations. |
| 79 | +4. Key builtins by static ID. |
| 80 | +5. Reject mismatched arity/type/ownership metadata at build time. |
| 81 | + |
| 82 | +### Milestone 4: Consolidate native helper contracts |
| 83 | + |
| 84 | +**Files:** |
| 85 | +- Modify native bridge/helper modules |
| 86 | +- Modify JIT/AOT lowerers |
| 87 | + |
| 88 | +1. Define one helper ABI for tagged/scalar/heap operands. |
| 89 | +2. Centralize owned temporary and Arc/raw-pointer rules. |
| 90 | +3. Centralize trap/status routing. |
| 91 | +4. Remove backend-specific reinterpretation of the same helper payload. |
| 92 | + |
| 93 | +### Milestone 5: Add differential execution harness |
| 94 | + |
| 95 | +For generated and curated programs, compare: |
| 96 | + |
| 97 | +- return value and structured error; |
| 98 | +- side-effect/event sequence; |
| 99 | +- ownership/drop counters where observable; |
| 100 | +- fuel/deadline behavior; |
| 101 | +- interpreter, JIT, AOT, and no-std supported subsets. |
| 102 | + |
| 103 | +Include arrays/maps/bytes, calls/closures, branches/loops, host-call boundaries, traps, and deopt cases. |
| 104 | + |
| 105 | +### Milestone 6: Migrate one semantic family at a time |
| 106 | + |
| 107 | +Recommended order: |
| 108 | + |
| 109 | +1. scalar arithmetic/comparison; |
| 110 | +2. stack/local/frame operations; |
| 111 | +3. collection access/mutation; |
| 112 | +4. calls/closures; |
| 113 | +5. builtin/native helper calls; |
| 114 | +6. suspension/deopt/terminal outcomes. |
| 115 | + |
| 116 | +Each family removes superseded duplicate tables after differential parity passes. |
| 117 | + |
| 118 | +### Milestone 7: Verification |
| 119 | + |
| 120 | +```bash |
| 121 | +cargo fmt --all -- --check |
| 122 | +cargo test --locked --workspace --all-features |
| 123 | +cargo test --locked -p pd-vm-nostd |
| 124 | +cargo clippy --locked --workspace --all-targets --all-features -- -D warnings |
| 125 | +git diff --check |
| 126 | +``` |
| 127 | + |
| 128 | +## Target criteria |
| 129 | + |
| 130 | +- Every opcode and builtin has one canonical semantic entry. |
| 131 | +- Every backend declares implement/fallback/unsupported explicitly. |
| 132 | +- New operations cannot compile without a complete backend disposition. |
| 133 | +- Interpreter/JIT/AOT/no-std differential fixtures agree on the supported subset. |
| 134 | +- Native ownership and trap ABI is shared by JIT and AOT. |
| 135 | +- Backend-specific large files lose duplicated semantic policy over incremental milestones. |
| 136 | +- Performance changes are measured separately from semantic convergence. |
0 commit comments