Skip to content

Commit 9db3f79

Browse files
committed
feat(compiler): allocate locals per script frame
1 parent 902efc3 commit 9db3f79

52 files changed

Lines changed: 9791 additions & 622 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/callable-runtime.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
# Script call frames and callable values
22

3-
RustScript bytecode format version 11 (VMBC v11) introduces runtime script call frames, first-class callable values, and the static builtin ID catalog.
3+
RustScript bytecode format version 12 (VMBC v12) carries runtime script call frames, first-class callable values, the static builtin ID catalog, and the direct script-call opcode. Version 11 introduced frames, callable values, and the static catalog; version 12 adds `callscript` for statically resolved named calls.
44

55
## Bytecode contract
66

77
- `call <import:u16> <argc:u8>` remains the direct host/builtin operation; the `u16` operand is an explicit static builtin call index from the catalog (or a host-import slot) — never a count-derived offset.
88
- `callvalue <argc:u8>` consumes a stack segment in `callee, arg0, ..., argN` order.
9+
- `callscript <prototype_id:u32> <argc:u8>` calls a statically resolved named script function by prototype ID. It consumes only `argc` arguments; no callable value is taken from the stack, so environment-free named functions can be called without a hidden callable local.
910
- callable environments are bound through the internal builtin call path; callable creation adds no bytecode opcode.
1011
- `ret` completes the active script frame. A nested frame leaves exactly one result at the caller segment base, using `null` when the body produced no value. Root `ret` keeps the historical program-result stack behavior.
1112

12-
VMBC v11 is a hard format boundary. Decoders reject all earlier versions (v10 and below) with a deterministic unsupported-version error; there is no compatibility decoder and no old-ID alias. The stream includes script-function entry ranges, callable prototypes, function regions, root callable bindings, and call indices drawn from the static builtin catalog. PDRC v6 recordings and AOT artifacts (format 7, ABI 7) use their corresponding bumped versions and include callable metadata in cache identity.
13+
### Call ownership
14+
15+
The three call opcodes differ in who owns the callee and what the frame must provide:
16+
17+
- `call` — the callee is owned by the static builtin catalog (or the host-import slot). The frame contributes only `argc` arguments; there is no callable value anywhere in the program.
18+
- `callvalue` — the callee is a `Value::Callable` owned by the caller operand stack at the call site, and remains the caller's responsibility after the call. This path carries environments, closures, and any callable whose identity or capture state is runtime-valued.
19+
- `callscript` — the callee is owned by program callable metadata (the prototype table). The frame contributes only `argc` arguments and no callable value, but unlike `call` the callee is a script function rather than a builtin, so the call enters a new script frame with its own local base.
20+
21+
VMBC v12 is a hard format boundary. Decoders reject all earlier versions (v11 and below) with a deterministic unsupported-version error; there is no compatibility decoder and no old-ID alias. The stream includes script-function entry ranges, callable prototypes, function regions, root callable bindings, and call indices drawn from the static builtin catalog. PDRC v6 recordings and AOT artifacts (format 8, ABI 8) use their corresponding bumped versions and include callable metadata in cache identity.
1322

1423
## Static builtin IDs
1524

@@ -18,7 +27,7 @@ Every VM-visible builtin (ordinary, internal, and special-call) has one explicit
1827
- **Immutable explicit IDs.** IDs never change once assigned. Adding or reordering catalog entries never renumbers existing entries; new builtins take the next free ID in their documented block (extension `0x0000..=0xFF8F` for future builtins and host imports, special-call `0xFF90..=0xFFA1`, ordinary `0xFFA2..=0xFFFF`). The reserved sentinel gap `0xFF90..=0xFF92` stays unassigned.
1928
- **Build-time validation.** The build fails on duplicate IDs, duplicate source names, duplicate Rust variants, out-of-block IDs, class/gate inconsistencies, a discovered runtime callable without an explicit ID, or a catalog entry without a runtime callable.
2029
- **Shared std/no-std IDs.** `pd-vm-nostd` dispatches on the same static indices through the checked-in generated mirror `pd-vm-nostd/src/generated_builtin_ids.rs`; the workspace test `static_builtin_ids_are_frozen` fails when the mirror drifts from the catalog.
21-
- **One-time format break.** The static ID migration bumped VMBC to v11 (and the internal bytecode ABI to 11). Older VMBC versions are rejected, never decoded.
30+
- **Format breaks are permanent.** The static ID migration bumped VMBC to v11 (and the internal bytecode ABI to 11); the `callscript` opcode break bumped both to v12. Versions below the current format are rejected, never decoded.
2231

2332
## Runtime model
2433

@@ -29,10 +38,24 @@ Each script invocation owns:
2938
- frame-local count;
3039
- active prototype and callable identity.
3140

32-
Arguments, captures, named callable bindings, and the self binding are installed before control moves to the function entry. Recursive calls therefore allocate independent local storage and are limited to 1,024 script frames.
41+
Arguments, captures, hidden callable bindings for materialized named functions, and the self binding are installed before control moves to the function entry. Recursive calls therefore allocate independent local storage and are limited to 1,024 script frames.
3342

3443
Branches are restricted to the active function region. Validation rejects cross-region targets before execution, and the interpreter repeats the check at runtime.
3544

45+
## Frame-local allocation and callable materialization
46+
47+
Each script invocation frame is an independent local-address space with its own `local_base`. Locals that are live at the same time inside one frame interfere and receive distinct relative slot numbers; locals that belong to different frames never interfere and may reuse the same relative slot number, because the runtime frame bases already separate them. A statically resolved named call keeps the caller's argument slots and post-call values live in the caller frame, while the callee body's locals are analyzed inside the callee frame.
48+
49+
Named functions receive a hidden callable slot only when runtime `Value::Callable` identity is actually required:
50+
51+
- the function is exported under the `ExportedCallable { local_slot }` contract;
52+
- the function is referenced as a value (stored, passed, or returned);
53+
- the function captures an environment;
54+
- a dynamic call site can target the function (invoked slot or argument flow into an invoked parameter);
55+
- the function's runtime self identity is required by a capturing or dynamic recursion path.
56+
57+
Functions that only receive plain direct calls — including non-capturing direct recursion — are lowered through `callscript` by prototype ID and consume no hidden callable local. The compiler reports the aggregate frame-local count (data slots plus materialized callable slots) in `FrameLocalLimitExceeded` diagnostics, so overflow reports real counts instead of a sentinel. Genuine same-frame pressure beyond 256 simultaneous locals keeps failing until wide local bytecode lands.
58+
3659
## Callable identity and lifetime
3760

3861
A callable contains its prototype ID, kind, and optional environment. The Program/Store owns the callable lifetime. Capture-free function items compare by prototype identity inside that Program; closures compare by runtime environment identity. Callable constants are forbidden; functions are initialized from Program metadata and closures are materialized at their declaration site.
@@ -57,8 +80,8 @@ Polling drives execution and provides backpressure: at most one event item is bu
5780

5881
## Optimized backends
5982

60-
Whole-program AOT and Trace JIT use the same builtin call path (static catalog IDs) for environment binding and native frame dispatch for `callvalue`. Script-frame entry and return preserve frame-relative locals and typed continuations.
83+
Whole-program AOT and Trace JIT use the same builtin call path (static catalog IDs) for environment binding, native frame dispatch for `callvalue`, and prototype-direct native dispatch for `callscript`. Script-frame entry and return preserve frame-relative locals and typed continuations.
6184

6285
## Embedded runtime
6386

64-
`pd-vm-nostd` decodes the same VMBC v11 callable metadata and executes callable binding, `callvalue`, recursive frames, captures, and direct host targets using `core` plus `alloc`, dispatching on the identical static builtin IDs via its checked-in generated mirror.
87+
`pd-vm-nostd` decodes the same VMBC v12 callable metadata and executes callable binding, `callvalue`, `callscript`, recursive frames, captures, and direct host targets using `core` plus `alloc`, dispatching on the identical static builtin IDs via its checked-in generated mirror.

pd-vm-nostd/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ compiler, parser, CLI, debugger, JIT/AOT backends, filesystem support, and opera
66

77
## Runtime surface
88

9-
- VMBC v11 decoding with script-call and callable metadata
9+
- VMBC v12 decoding with environment-free `CallScript` direct script calls alongside dynamic callable calls
1010
- stack, local, and recursive script-frame execution for direct bytecode opcodes
1111
- instruction fuel with pause/resume support
1212
- synchronous named host bindings and dynamic host dispatch

pd-vm-nostd/src/error.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ pub enum VmError {
1414
InvalidCall(u16),
1515
InvalidCallable,
1616
InvalidCallablePrototype(u32),
17+
/// Frame metadata (root binding slots, parameter or capture slots)
18+
/// does not match the script frame layout.
19+
InvalidFrameState(&'static str),
20+
/// `CallScript` targeted a prototype whose capture layout requires an
21+
/// environment; a static script call can never supply one.
22+
CallScriptRequiresEnvironment(u32),
1723
CallStackOverflow,
1824
InvalidCallStackLimit(usize),
1925
InvalidCallArity {
@@ -52,6 +58,11 @@ impl fmt::Display for VmError {
5258
Self::InvalidCallablePrototype(index) => {
5359
write!(f, "invalid callable prototype: {index}")
5460
}
61+
Self::InvalidFrameState(detail) => write!(f, "invalid frame state: {detail}"),
62+
Self::CallScriptRequiresEnvironment(prototype_id) => write!(
63+
f,
64+
"callscript prototype {prototype_id} requires a callable environment"
65+
),
5566
Self::CallStackOverflow => f.write_str("script call stack overflow"),
5667
Self::InvalidCallStackLimit(limit) => {
5768
write!(
@@ -96,6 +107,22 @@ pub enum WireError {
96107
InvalidDebugFlag(u8),
97108
InvalidValueType(u8),
98109
InvalidCaptureBindingMode(u8),
110+
/// `CallScript` referenced a prototype id that is out of range or does
111+
/// not target a script function.
112+
InvalidCallScriptTarget {
113+
prototype_id: u32,
114+
},
115+
/// `CallScript` declared an argc that disagrees with the prototype arity.
116+
InvalidCallScriptArity {
117+
prototype_id: u32,
118+
expected: u8,
119+
got: u8,
120+
},
121+
/// An instruction operand is truncated by the end of the code blob.
122+
TruncatedOperand {
123+
opcode: u8,
124+
expected_bytes: usize,
125+
},
99126
InvalidUtf8,
100127
LengthTooLarge(&'static str, usize),
101128
SchemaTooDeep,
@@ -119,6 +146,25 @@ impl fmt::Display for WireError {
119146
Self::InvalidCaptureBindingMode(value) => {
120147
write!(f, "invalid capture binding mode: {value}")
121148
}
149+
Self::InvalidCallScriptTarget { prototype_id } => write!(
150+
f,
151+
"callscript prototype {prototype_id} does not target a script function"
152+
),
153+
Self::InvalidCallScriptArity {
154+
prototype_id,
155+
expected,
156+
got,
157+
} => write!(
158+
f,
159+
"callscript prototype {prototype_id} arity mismatch: expected {expected}, got {got}"
160+
),
161+
Self::TruncatedOperand {
162+
opcode,
163+
expected_bytes,
164+
} => write!(
165+
f,
166+
"truncated operand for opcode {opcode:#04x}: expected {expected_bytes} bytes"
167+
),
122168
Self::InvalidUtf8 => f.write_str("invalid UTF-8 in VMBC string"),
123169
Self::LengthTooLarge(field, length) => {
124170
write!(f, "{field} length is too large: {length}")

pd-vm-nostd/src/program.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ pub enum OpCode {
230230
Not = 0x17,
231231
Lshr = 0x18,
232232
CallValue = 0x19,
233+
/// Static direct script-function call: `prototype_id:u32 LE, argc:u8`.
234+
///
235+
/// Mirrors the std ISA contract (opcode 0x1A, five operand bytes); the
236+
/// decoder validates the target prototype and arity against the callable
237+
/// metadata so an environment-free script call is a supported operation.
238+
CallScript = 0x1A,
233239
}
234240

235241
impl OpCode {
@@ -238,6 +244,7 @@ impl OpCode {
238244
Self::Ldc | Self::Br | Self::Brfalse => 4,
239245
Self::Ldloc | Self::Stloc | Self::CallValue => 1,
240246
Self::Call => 3,
247+
Self::CallScript => 5,
241248
_ => 0,
242249
}
243250
}
@@ -274,6 +281,7 @@ impl TryFrom<u8> for OpCode {
274281
0x17 => Ok(Self::Not),
275282
0x18 => Ok(Self::Lshr),
276283
0x19 => Ok(Self::CallValue),
284+
0x1a => Ok(Self::CallScript),
277285
_ => Err(()),
278286
}
279287
}

0 commit comments

Comments
 (0)