runmat_vm/interpreter/
state.rs1use crate::bytecode::{Bytecode, ExecutionContext};
2#[cfg(feature = "native-accel")]
3use runmat_accelerate::{prepare_fusion_plan, FusionPlan};
4use runmat_builtins::Value;
5use std::collections::HashMap;
6#[cfg(feature = "native-accel")]
7use std::sync::Arc;
8
9#[derive(Debug)]
10pub enum InterpreterOutcome {
11 Completed(Vec<Value>),
12}
13
14#[derive(Debug)]
15pub struct InterpreterState {
16 pub bytecode: Bytecode,
17 pub stack: Vec<Value>,
18 pub vars: Vec<Value>,
19 pub pc: usize,
20 pub context: ExecutionContext,
21 pub try_stack: Vec<(usize, Option<usize>)>,
22 pub last_exception: Option<runmat_builtins::MException>,
23 pub imports: Vec<(Vec<String>, bool)>,
24 pub global_aliases: HashMap<usize, String>,
25 pub persistent_aliases: HashMap<usize, String>,
26 pub current_function_name: String,
27 pub call_counts: Vec<(usize, usize)>,
28 #[cfg(feature = "native-accel")]
29 pub fusion_plan: Option<Arc<FusionPlan>>,
30}
31
32impl InterpreterState {
33 pub fn new(
34 bytecode: Bytecode,
35 initial_vars: &mut [Value],
36 current_function_name: Option<&str>,
37 call_counts: Vec<(usize, usize)>,
38 ) -> Self {
39 let mut vars = initial_vars.to_vec();
40 if vars.len() < bytecode.var_count {
41 vars.resize(bytecode.var_count, Value::Num(0.0));
42 }
43 Self {
44 stack: Vec::new(),
45 context: ExecutionContext {
46 call_stack: Vec::new(),
47 locals: Vec::new(),
48 instruction_pointer: 0,
49 functions: bytecode.functions.clone(),
50 },
51 try_stack: Vec::new(),
52 last_exception: None,
53 imports: Vec::new(),
54 global_aliases: HashMap::new(),
55 persistent_aliases: HashMap::new(),
56 vars,
57 pc: 0,
58 call_counts,
59 current_function_name: current_function_name
60 .map(|s| s.to_string())
61 .unwrap_or_else(|| "<main>".to_string()),
62 #[cfg(feature = "native-accel")]
63 fusion_plan: prepare_fusion_plan(
64 bytecode.accel_graph.as_ref(),
65 &bytecode.fusion_groups,
66 ),
67 bytecode,
68 }
69 }
70}