1use std::fmt::{Display, Formatter};
6
7#[repr(u8)]
8#[derive(Debug)]
9pub enum OpCode {
10 Nop,
11 Hlt, UserHalt,
13 Step, Panic, Trap, Brk, AddU32,
21 AddU32Imm,
22 MulU32,
23 SubU32,
24
25 NegI32,
27 ModI32,
28 DivI32,
29
30 MulF32,
32 DivF32,
33
34 LtI32,
38 LeI32,
39 GtI32,
40 GeI32,
41
42 GeU32,
44 LtU32,
45
46 Eq8Imm,
48 CmpReg,
49 CmpBlock,
50
51 TrapOnLessThan,
53
54 BFalse,
56 BTrue,
57
58 B,
60
61 Call,
63 Enter,
64 Ret,
65
66 BlockCopy, FrameMemClr,
68
69 LdPtrFromEffectiveFrameAddress, Ld8FromPointerWithOffset,
74 Ld16FromPointerWithOffset,
75 Ld32FromPointerWithOffset,
76
77 LdRegFromFrameRange,
78 LdRegFromFrameUsingMask,
79
80 Ld8FromAbsoluteAddress,
83 Ld32FromAbsoluteAddress,
84
85 St8UsingPtrWithOffset,
87 St16UsingPtrWithOffset,
88 St32UsingPtrWithOffset,
89 StRegToFrame,
90 StRegToFrameUsingMask,
91
92 MovReg,
94 Mov8FromImmediateValue,
96 Mov16FromImmediateValue,
97 Mov32FromImmediateValue,
98
99 MovEqualToZero,
100 FloatRound,
106 FloatFloor,
107 FloatSqrt,
108 FloatSign,
109 FloatAbs,
110 FloatPseudoRandom,
111 FloatSin,
112 FloatCos,
113 FloatAcos,
114 FloatAsin,
115 FloatAtan2,
116 FloatMin,
117 FloatMax,
118 FloatClamp,
119 FloatToString,
120
121 IntToRnd,
123 IntToFloat,
124 IntAbs,
125 IntMin,
126 IntMax,
127 IntClamp,
128 IntToString,
129
130 CodepointToString,
132
133 BoolToString,
135
136 ByteToString,
137
138 RangeInit,
142 RangeIterInit,
143 RangeIterNext,
144
145 ArrayInitWithLenAndCapacity,
147
148 VecInit, VecCmp,
151 VecCopy,
152 VecPushAddr,
153 VecRemoveIndex,
154 VecPop,
155 VecRemoveIndexGetValue,
156 VecGet,
157 VecCopyRange,
158 VecSwap,
159 VecIterInit,
160 VecIterNext,
161 VecIterNextPair,
162
163 MapInitWithCapacityAndKeyAndTupleSizeAddr, MapGetEntryLocation, MapGetOrReserveEntryLocation, MapRemove,
168 MapHas,
169 MapOverwrite,
170
171 MapIterInit,
172 MapIterNext,
173 MapIterNextPair,
174
175 StringAppend,
177 StringRepeat,
178 StringCmp,
179 StringToString,
180 StringIterInit,
181 StringIterNext,
182 StringIterNextPair,
183
184 HostCall, SparseInit,
189 SparseAddGiveEntryAddress,
190 SparseRemove,
191 SparseGetEntryAddr,
192 SparseIterNext,
193 SparseIterNextPair,
194 SparseIterInit,
195 SparseIsAlive,
196
197 GridInit,
199 GridGetEntryAddr,
200}
201
202impl OpCode {
203 #[allow(clippy::too_many_lines)]
204 #[must_use]
205 pub const fn as_string(&self) -> &str {
206 match self {
207 Self::Nop => "nop",
208 Self::Hlt => "hlt",
209 Self::UserHalt => "user_halt",
210 Self::Step => "step",
211 Self::Panic => "panic",
212 Self::Trap => "trap",
213 Self::Brk => "brk",
214
215 Self::AddU32 | Self::AddU32Imm => "add",
217 Self::MulU32 => "mul",
218 Self::SubU32 => "sub",
219
220 Self::NegI32 => "s.neg",
221 Self::ModI32 => "s.mod",
222 Self::DivI32 => "s.div",
223
224 Self::MulF32 => "f.mul",
226 Self::DivF32 => "f.div",
227
228 Self::LtI32 => "lt",
230 Self::LeI32 => "le",
231 Self::GtI32 => "gt",
232 Self::GeI32 => "ge",
233
234 Self::GeU32 => "uge",
235 Self::LtU32 => "ult",
236
237 Self::Eq8Imm | Self::CmpReg => "cmp",
239 Self::CmpBlock => "cmp.blk",
240 Self::FrameMemClr => "clr.blk.f",
241
242 Self::TrapOnLessThan => "trap.lt",
244
245 Self::BFalse => "b.false",
247 Self::BTrue => "b.true",
248 Self::B => "b",
249
250 Self::Call => "call",
252 Self::Enter => "enter",
253 Self::Ret => "ret",
254
255 Self::BlockCopy => "blk.cpy",
257 Self::LdPtrFromEffectiveFrameAddress => "lea",
258
259 Self::Mov8FromImmediateValue => "mov.b",
261 Self::Mov16FromImmediateValue => "mov.h",
262 Self::MovReg | Self::Mov32FromImmediateValue => "mov", Self::MovEqualToZero => "meqz",
264
265 Self::Ld8FromPointerWithOffset | Self::Ld8FromAbsoluteAddress => "ld.b",
267 Self::Ld16FromPointerWithOffset => "ld.h",
268 Self::Ld32FromPointerWithOffset | Self::Ld32FromAbsoluteAddress => "ld", Self::LdRegFromFrameUsingMask | Self::LdRegFromFrameRange => "ldmf",
270
271 Self::St32UsingPtrWithOffset => "st", Self::St16UsingPtrWithOffset => "st.h",
274 Self::St8UsingPtrWithOffset => "st.b",
275 Self::StRegToFrameUsingMask | Self::StRegToFrame => "stmf",
276
277 Self::FloatRound => "f.round",
279 Self::FloatFloor => "f.floor",
280 Self::FloatSqrt => "f.sqrt",
281 Self::FloatSign => "f.sign",
282 Self::FloatAbs => "f.abs",
283 Self::FloatPseudoRandom => "f.prnd",
284 Self::FloatSin => "f.sin",
285 Self::FloatCos => "f.cos",
286 Self::FloatAcos => "f.acos",
287 Self::FloatAsin => "f.asin",
288 Self::FloatAtan2 => "f.atan2",
289 Self::FloatMin => "f.min",
290 Self::FloatMax => "f.max",
291 Self::FloatClamp => "f.clamp",
292 Self::FloatToString => "f.to.str",
293
294 Self::IntToRnd => "i.rnd",
296 Self::IntToFloat => "i.tof",
297 Self::IntAbs => "i.abs",
298 Self::IntMin => "i.min",
299 Self::IntMax => "i.max",
300 Self::IntClamp => "i.clamp",
301 Self::IntToString => "i.tos",
302
303 Self::HostCall => "host",
305
306 Self::BoolToString => "bool.to.str",
308
309 Self::CodepointToString => "codepoint.to.str",
311
312 Self::ByteToString => "byte.to.str",
314
315 Self::RangeInit => "range.init",
317 Self::RangeIterInit => "range.iter",
318 Self::RangeIterNext => "range.iter.next",
319
320 Self::ArrayInitWithLenAndCapacity => "array.init",
322
323 Self::VecPushAddr => "vec.push",
325 Self::VecRemoveIndex => "vec.rem",
326 Self::VecPop => "vec.pop",
327 Self::VecRemoveIndexGetValue => "vec.rem.v",
328 Self::VecGet => "vec.get",
329 Self::VecCopyRange => "vec.copy.range",
330 Self::VecSwap => "vec.swap",
331 Self::VecInit => "vec.init",
332 Self::VecCopy => "vec.copy",
333 Self::VecCmp => "vec.cmp",
334 Self::VecIterInit => "vec.iter",
335 Self::VecIterNext => "vec.iter.next",
336 Self::VecIterNextPair => "vec.iter.next.pair",
337
338 Self::MapInitWithCapacityAndKeyAndTupleSizeAddr => "map.init",
340 Self::MapGetEntryLocation => "map.entry",
341 Self::MapGetOrReserveEntryLocation => "map.entry.or_create",
342 Self::MapHas => "map.has",
343 Self::MapOverwrite => "map.overwrite",
344 Self::MapRemove => "map.rem",
345 Self::MapIterInit => "map.iter.init",
346 Self::MapIterNext => "map.iter.next",
347 Self::MapIterNextPair => "map.iter.next.pair",
348
349 Self::SparseInit => "sparse.init",
351 Self::SparseAddGiveEntryAddress => "sparse.add_entry_addr",
352 Self::SparseRemove => "sparse.remove",
353 Self::SparseGetEntryAddr => "sparse.entry_addr",
354 Self::SparseIsAlive => "sparse.is_alive",
355 Self::SparseIterInit => "sparse.iter.init",
357 Self::SparseIterNext => "sparse.iter.next",
358 Self::SparseIterNextPair => "sparse.iter.next.pair",
359
360 Self::GridInit => "grid.init",
362 Self::GridGetEntryAddr => "grid.entry_addr",
363
364 Self::StringAppend => "str.app",
366 Self::StringRepeat => "str.repeat",
367 Self::StringCmp => "str.cmp",
368 Self::StringToString => "str.tos",
369 Self::StringIterInit => "str.iter",
370 Self::StringIterNext => "str.iter.next",
371 Self::StringIterNextPair => "str.iter.next.pair",
372 }
373 }
374}
375impl Display for OpCode {
376 #[allow(clippy::too_many_lines)]
377 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
378 write!(f, "{}", self.as_string())
379 }
380}
381
382impl From<u8> for OpCode {
384 fn from(value: u8) -> Self {
385 unsafe { std::mem::transmute(value) }
388 }
389}