runmat_ignition/
functions.rs1use crate::instr::Instr;
2#[cfg(feature = "native-accel")]
3use runmat_accelerate::graph::AccelGraph;
4#[cfg(feature = "native-accel")]
5use runmat_accelerate::FusionGroup;
6use runmat_builtins::{Type, Value};
7use runmat_hir::{HirStmt, VarId};
8use serde::{Deserialize, Serialize};
9use std::collections::HashMap;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct UserFunction {
13 pub name: String,
14 pub params: Vec<VarId>,
15 pub outputs: Vec<VarId>,
16 pub body: Vec<HirStmt>,
17 pub local_var_count: usize,
18 pub has_varargin: bool,
19 pub has_varargout: bool,
20 #[serde(default)]
21 pub var_types: Vec<Type>,
22}
23
24#[derive(Debug, Clone)]
26pub struct CallFrame {
27 pub function_name: String,
28 pub return_address: usize,
29 pub locals_start: usize,
30 pub locals_count: usize,
31 pub expected_outputs: usize,
32}
33
34#[derive(Debug)]
36pub struct ExecutionContext {
37 pub call_stack: Vec<CallFrame>,
38 pub locals: Vec<Value>,
39 pub instruction_pointer: usize,
40 pub functions: std::collections::HashMap<String, UserFunction>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct Bytecode {
45 pub instructions: Vec<Instr>,
46 pub var_count: usize,
47 pub functions: HashMap<String, UserFunction>,
48 #[serde(default)]
49 pub var_types: Vec<Type>,
50 #[cfg(feature = "native-accel")]
51 #[serde(default)]
52 pub accel_graph: Option<AccelGraph>,
53 #[cfg(feature = "native-accel")]
54 #[serde(default)]
55 pub fusion_groups: Vec<FusionGroup>,
56}
57
58impl Bytecode {
59 pub fn empty() -> Self {
60 Self {
61 instructions: Vec::new(),
62 var_count: 0,
63 functions: HashMap::new(),
64 var_types: Vec::new(),
65 #[cfg(feature = "native-accel")]
66 accel_graph: None,
67 #[cfg(feature = "native-accel")]
68 fusion_groups: Vec::new(),
69 }
70 }
71
72 pub fn with_instructions(instructions: Vec<Instr>, var_count: usize) -> Self {
73 Self {
74 instructions,
75 var_count,
76 ..Self::empty()
77 }
78 }
79}