Skip to main content

ruso_runtime/runtime/
bytecode.rs

1//! Executable bytecode produced from the script AST.
2
3use crate::contract::{EvidenceKind, ExtractSource, QualifiedMatch};
4use crate::runtime::spec::ProgramSpec;
5
6/// Compiled script ready for the executor (probes, metadata, and instruction stream).
7#[derive(Debug, Clone)]
8pub struct BytecodeProgram {
9    pub spec: ProgramSpec,
10    pub code: Vec<Instr>,
11    pub strings: Vec<String>,
12    /// Binary payloads referenced by `Send.payload` (override bytes).
13    pub payloads: Vec<Vec<u8>>,
14    pub matchers: Vec<QualifiedMatch>,
15    pub extracts: Vec<ExtractSource>,
16    pub evidence: Vec<EvidenceKind>,
17}
18
19/// Instruction pointer index into `BytecodeProgram::code`.
20pub type Pc = u32;
21
22#[derive(Debug, Clone, PartialEq)]
23pub enum Instr {
24    Set {
25        name: u32,
26        value: u32,
27    },
28    SetList {
29        name: u32,
30        start: u32,
31        len: u16,
32    },
33    Send {
34        probe: u32,
35        payload: Option<u32>,
36    },
37    Match(u32),
38    MatchAll {
39        start: u32,
40        len: u16,
41    },
42    MatchAny {
43        start: u32,
44        len: u16,
45    },
46    Assert(u32),
47    Extract {
48        name: u32,
49        source: u32,
50    },
51    IfMatch {
52        matcher: u32,
53        else_pc: Pc,
54    },
55    ForList {
56        item: u32,
57        start: u32,
58        len: u16,
59        end_pc: Pc,
60    },
61    ForVar {
62        item: u32,
63        list: u32,
64        end_pc: Pc,
65    },
66    LoopBack,
67    Break,
68    Save {
69        from: u32,
70        to: u32,
71    },
72    Evidence(u32),
73    Retry {
74        probe: u32,
75        count: u32,
76    },
77    RetryDelay(u32),
78    Sleep(u32),
79    Stop,
80    Fail,
81    Continue,
82    Exit,
83}
84
85impl BytecodeProgram {
86    pub fn instr_count(&self) -> usize {
87        self.code.len()
88    }
89}