glyph_runtime/
instruction.rs1use glyph_types::Value;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub enum Instruction {
9 Push(Value),
11 Pop,
12 Dup,
13 Swap,
14 Rot3, Add,
18 Sub,
19 Mul,
20 Div,
21 Mod,
22 Neg,
23 Pow,
24
25 Eq,
27 Ne,
28 Lt,
29 Le,
30 Gt,
31 Ge,
32
33 And,
35 Or,
36 Not,
37
38 Jump(usize),
40 JumpIf(usize),
41 JumpIfNot(usize),
42 Call(usize), CallNative(String), Return,
45
46 BindLocal(String), LoadLocal(String), LoadGlobal(String), MakeList(usize), MakeDict(usize), MakeTuple(usize), GetIndex, GetAttr(String), Slice(Option<i64>, Option<i64>), ListAppend, ListConcat, DictInsert, DictMerge, MatchValue, MatchType(String), Destructure(usize), MakePromise,
72 AwaitPromise,
73 ResolvePromise,
74 RejectPromise,
75
76 MakeOk,
78 MakeErr,
79 UnwrapResult,
80 MapResult,
81
82 RequireCapability(String),
84 CheckCapability(String),
85
86 CallIntrinsic {
88 name: String,
89 capability: Option<String>,
90 },
91
92 TraceValue(String), RecordTelemetry(String), Nop,
98 Halt,
99}
100
101impl Instruction {
102 pub fn size(&self) -> usize {
104 use std::mem;
105 match self {
107 Instruction::Add
109 | Instruction::Sub
110 | Instruction::Mul
111 | Instruction::Div
112 | Instruction::Mod
113 | Instruction::Neg
114 | Instruction::Pow
115 | Instruction::Eq
116 | Instruction::Ne
117 | Instruction::Lt
118 | Instruction::Le
119 | Instruction::Gt
120 | Instruction::Ge
121 | Instruction::And
122 | Instruction::Or
123 | Instruction::Not
124 | Instruction::Pop
125 | Instruction::Dup
126 | Instruction::Swap
127 | Instruction::Rot3
128 | Instruction::GetIndex
129 | Instruction::ListAppend
130 | Instruction::ListConcat
131 | Instruction::DictInsert
132 | Instruction::DictMerge
133 | Instruction::MatchValue
134 | Instruction::MakePromise
135 | Instruction::AwaitPromise
136 | Instruction::ResolvePromise
137 | Instruction::RejectPromise
138 | Instruction::MakeOk
139 | Instruction::MakeErr
140 | Instruction::UnwrapResult
141 | Instruction::MapResult
142 | Instruction::Return
143 | Instruction::Nop
144 | Instruction::Halt => 1,
145
146 Instruction::Push(value) => 1 + mem::size_of_val(value),
148 Instruction::Jump(_)
149 | Instruction::JumpIf(_)
150 | Instruction::JumpIfNot(_)
151 | Instruction::Call(_) => 1 + mem::size_of::<usize>(),
152 Instruction::MakeList(n)
153 | Instruction::MakeDict(n)
154 | Instruction::MakeTuple(n)
155 | Instruction::Destructure(n) => 1 + mem::size_of_val(n),
156 Instruction::BindLocal(s)
157 | Instruction::LoadLocal(s)
158 | Instruction::LoadGlobal(s)
159 | Instruction::CallNative(s)
160 | Instruction::GetAttr(s)
161 | Instruction::MatchType(s)
162 | Instruction::RequireCapability(s)
163 | Instruction::CheckCapability(s)
164 | Instruction::TraceValue(s)
165 | Instruction::RecordTelemetry(s) => 1 + s.len(),
166 Instruction::CallIntrinsic { name, capability } => {
167 1 + name.len() + capability.as_ref().map_or(0, |c| c.len())
168 }
169 Instruction::Slice(start, end) => 1 + mem::size_of_val(start) + mem::size_of_val(end),
170 }
171 }
172
173 pub fn required_capability(&self) -> Option<&str> {
175 match self {
176 Instruction::RequireCapability(cap) => Some(cap),
177 Instruction::CallIntrinsic {
178 capability: Some(cap),
179 ..
180 } => Some(cap),
181 _ => None,
182 }
183 }
184
185 pub fn is_pure(&self) -> bool {
187 match self {
188 Instruction::CallIntrinsic { .. }
190 | Instruction::TraceValue(_)
191 | Instruction::RecordTelemetry(_) => false,
192 _ => true,
193 }
194 }
195}