Add bytecode function creation with defaults and shared closures
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 6m28s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 6m28s
This commit is contained in:
@@ -11,10 +11,10 @@
|
||||
| [builtins.rs](builtins.rs) | String、Dict、Cell、Function、BaseException 载荷与 trace 边 |
|
||||
| [number.rs](number.rs) | EndorNumber、Integer/Decimal/Rational/BinaryFloat/Complex 表示及装箱载荷 |
|
||||
| [vm_builtins.rs](vm_builtins.rs) | 内置载荷校验、代码/函数工厂、Dict/Cell 操作、键语义和可变引用更新 |
|
||||
| [instruction.rs](instruction.rs) | Opcode、逻辑指令、EXTENDED_ARG 编解码、指令位置与比较参数 |
|
||||
| [instruction.rs](instruction.rs) | Opcode、逻辑指令、EXTENDED_ARG 编解码、MakeFunction 计数构造、指令位置与比较参数 |
|
||||
| [code.rs](code.rs) | CodeLayout、EndorCode、共享 EndorCodeObject、操作数/控制流/栈校验 |
|
||||
| [frame.rs](frame.rs) | EndorFrame、栈条目、执行位置元数据、ExecutionLimits |
|
||||
| [eval.rs](eval.rs) | execute/call、参数绑定、帧切换、取指分派、结果根转移与失败清理 |
|
||||
| [eval.rs](eval.rs) | execute/call、参数绑定、帧切换、取指分派、字节码创建函数、结果根转移与失败清理 |
|
||||
|
||||
## 常见修改入口
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::collections::{BTreeSet, VecDeque};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::builtins::trace_value;
|
||||
use crate::instruction::{compare_operation, decode, invalid};
|
||||
use crate::instruction::{compare_operation, decode, function_counts, invalid};
|
||||
use crate::{DecodedInstruction, Instruction, ObjectPayload, ObjectRef, Opcode, Value, VmError};
|
||||
|
||||
/// Local slots start with positional parameters. Captured locals occupy their
|
||||
@@ -330,6 +330,11 @@ fn verify(
|
||||
.ok_or_else(|| invalid(instruction.offset, "map operand overflow"))?;
|
||||
(consumed, consumed, 1)
|
||||
}
|
||||
Opcode::MakeFunction => {
|
||||
let (defaults, closure) = function_counts(instruction.instruction.argument);
|
||||
let consumed = defaults + closure + 1;
|
||||
(consumed, consumed, 1)
|
||||
}
|
||||
Opcode::ReturnValue => (1, 1, 0),
|
||||
_ => (0, 0, 0),
|
||||
};
|
||||
|
||||
+32
-1
@@ -1,6 +1,6 @@
|
||||
use crate::code::jump_target;
|
||||
use crate::frame::StackEntry;
|
||||
use crate::instruction::{compare_operation, invalid};
|
||||
use crate::instruction::{compare_operation, function_counts, invalid};
|
||||
use crate::{
|
||||
CompareOp, EndorCellObject, EndorCodeObject, EndorFrame, EndorFunctionObject,
|
||||
EndorStringObject, ExecutionLimits, ExecutionLocation, ObjectRef, Opcode, Value, Vm, VmError,
|
||||
@@ -314,6 +314,31 @@ impl Vm {
|
||||
Ok(dictionary)
|
||||
}
|
||||
|
||||
fn make_function(&mut self, frame: &EndorFrame, argument: u32) -> Result<ObjectRef, VmError> {
|
||||
let (default_count, closure_count) = function_counts(argument);
|
||||
let Value::Object(code) = Self::top_value(frame)? else {
|
||||
return Err(VmError::ObjectKindMismatch("code"));
|
||||
};
|
||||
let name = self
|
||||
.payload::<EndorCodeObject>(code, "code")?
|
||||
.code()
|
||||
.name()
|
||||
.to_owned();
|
||||
let start = frame.stack.len() - default_count - closure_count - 1;
|
||||
let defaults = (start..start + default_count)
|
||||
.map(|index| Self::stack_value(frame, index))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let closure = (start + default_count..frame.stack.len() - 1)
|
||||
.map(|index| match Self::stack_value(frame, index)? {
|
||||
Value::Object(cell) => Ok(cell),
|
||||
_ => Err(VmError::ObjectKindMismatch("cell")),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
// Keep every input root on the stack until allocation has validated
|
||||
// and retained all function edges. Failure leaves cleanup to the frame.
|
||||
self.new_function(name, code, frame.globals, defaults, closure)
|
||||
}
|
||||
|
||||
fn eval_frames(
|
||||
&mut self,
|
||||
frames: &mut Vec<EndorFrame>,
|
||||
@@ -528,6 +553,12 @@ impl Vm {
|
||||
.stack
|
||||
.push(StackEntry::Value(Value::Object(dictionary)));
|
||||
}
|
||||
Opcode::MakeFunction => {
|
||||
let function = self.make_function(frame, decoded.instruction.argument)?;
|
||||
let (defaults, closure) = function_counts(decoded.instruction.argument);
|
||||
self.discard(frame, defaults + closure + 1);
|
||||
frame.stack.push(StackEntry::Value(Value::Object(function)));
|
||||
}
|
||||
Opcode::ExtendedArg => unreachable!("EXTENDED_ARG was decoded before evaluation"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ pub enum Opcode {
|
||||
LoadAttr = 25,
|
||||
StoreAttr = 26,
|
||||
BuildMap = 27,
|
||||
MakeFunction = 28,
|
||||
ExtendedArg = 255,
|
||||
}
|
||||
|
||||
@@ -67,6 +68,7 @@ impl Opcode {
|
||||
25 => Self::LoadAttr,
|
||||
26 => Self::StoreAttr,
|
||||
27 => Self::BuildMap,
|
||||
28 => Self::MakeFunction,
|
||||
255 => Self::ExtendedArg,
|
||||
_ => return None,
|
||||
})
|
||||
@@ -100,6 +102,17 @@ impl Instruction {
|
||||
Self::new(opcode, 0)
|
||||
}
|
||||
|
||||
/// Build a function from [defaults..., closure cells..., code] on the stack.
|
||||
/// The low 16 operand bits count trailing positional defaults; the high
|
||||
/// 16 bits count cells in the code's free-variable order. No tuples or
|
||||
/// subsequent mutation of the function object are required.
|
||||
pub const fn make_function(defaults: u16, closure: u16) -> Self {
|
||||
Self::new(
|
||||
Opcode::MakeFunction,
|
||||
(defaults as u32) | ((closure as u32) << 16),
|
||||
)
|
||||
}
|
||||
|
||||
pub const fn compare(operation: CompareOp) -> Self {
|
||||
Self::new(
|
||||
Opcode::CompareOp,
|
||||
@@ -198,6 +211,10 @@ pub(crate) fn compare_operation(argument: u32) -> Option<CompareOp> {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn function_counts(argument: u32) -> (usize, usize) {
|
||||
((argument & 0xffff) as usize, (argument >> 16) as usize)
|
||||
}
|
||||
|
||||
pub(crate) fn invalid(offset: usize, reason: &'static str) -> VmError {
|
||||
VmError::InvalidBytecode { offset, reason }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user