Skip to main content

hexicon_bytecode/
opcode.rs

1//! The T3 VM instruction set.
2//!
3//! The opcode numbers and operand layouts follow the T3 VM "Byte-Code
4//! Instruction Set" document and the reference interpreter. A few
5//! instructions are in the interpreter but not in the
6//! document (`iternext`, `getsetlcl1`, `getsetlcl1r0`, `dupr0`, and
7//! `getlcln0` through `getlcln5`); their layouts are taken from the
8//! interpreter.
9
10/// The kind and size of one fixed operand.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum OperandKind {
13    /// Unsigned 8-bit number.
14    U8,
15    /// Signed 8-bit number.
16    S8,
17    /// Unsigned 16-bit number.
18    U16,
19    /// Signed 32-bit number.
20    S32,
21    /// Argument count, 8 bits.
22    Argc8,
23    /// Argument count, 16 bits.
24    Argc16,
25    /// Local variable number, 8 bits.
26    Local8,
27    /// Local variable number, 16 bits.
28    Local16,
29    /// Parameter number, 8 bits.
30    Arg8,
31    /// Parameter number, 16 bits.
32    Arg16,
33    /// Constant pool offset of a string, 32 bits.
34    String,
35    /// Constant pool offset of a list, 32 bits.
36    List,
37    /// Object ID, 32 bits.
38    Object,
39    /// Property ID, 16 bits.
40    Property,
41    /// Code pool offset of a function, 32 bits.
42    Function,
43    /// Enumerator ID, 32 bits.
44    Enum,
45    /// Signed 16-bit branch offset, relative to the operand's own address.
46    Branch,
47    /// Unsigned 16-bit offset to a named argument table, relative to the
48    /// operand's own address.
49    TableOffset,
50    /// Index into the image's metaclass dependency list, 8 bits.
51    Metaclass8,
52    /// Index into the image's metaclass dependency list, 16 bits.
53    Metaclass16,
54    /// Index into the image's function set dependency list, 8 bits.
55    FunctionSet8,
56    /// Index into the image's function set dependency list, 16 bits.
57    FunctionSet16,
58    /// Index of a function within its function set, 8 bits.
59    FunctionIndex8,
60    /// Index of a function within its function set, 16 bits.
61    FunctionIndex16,
62}
63
64impl OperandKind {
65    /// Encoded size in bytes.
66    pub const fn size(self) -> usize {
67        match self {
68            Self::U8
69            | Self::S8
70            | Self::Argc8
71            | Self::Local8
72            | Self::Arg8
73            | Self::Metaclass8
74            | Self::FunctionSet8
75            | Self::FunctionIndex8 => 1,
76            Self::U16
77            | Self::Argc16
78            | Self::Local16
79            | Self::Arg16
80            | Self::Property
81            | Self::Branch
82            | Self::TableOffset
83            | Self::Metaclass16
84            | Self::FunctionSet16
85            | Self::FunctionIndex16 => 2,
86            Self::S32 | Self::String | Self::List | Self::Object | Self::Function | Self::Enum => 4,
87        }
88    }
89}
90
91/// How an instruction's operands are laid out.
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93pub enum Layout {
94    /// A fixed sequence of operands.
95    Fixed(&'static [OperandKind]),
96    /// `pushstri`: a 16-bit length followed by that many bytes of text.
97    InlineString,
98    /// `switch`: a 16-bit case count, that many cases of a five-byte value
99    /// and a branch offset, then a default branch offset.
100    Switch,
101    /// `namedargtab`: a 16-bit byte count followed by a table of argument names.
102    NamedArgTable,
103}
104
105macro_rules! opcodes {
106    ($( $code:literal $variant:ident $name:literal $layout:tt ; )*) => {
107        /// A T3 VM instruction.
108        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
109        #[repr(u8)]
110        #[allow(missing_docs)]
111        pub enum Opcode {
112            $( $variant = $code, )*
113        }
114
115        impl Opcode {
116            /// Every defined opcode, in numeric order.
117            pub const ALL: &'static [Opcode] = &[ $( Opcode::$variant, )* ];
118
119            /// The opcode for a byte, if the byte is a defined instruction.
120            pub const fn from_byte(byte: u8) -> Option<Self> {
121                match byte {
122                    $( $code => Some(Self::$variant), )*
123                    _ => None,
124                }
125            }
126
127            /// The instruction's mnemonic.
128            pub const fn name(self) -> &'static str {
129                match self {
130                    $( Self::$variant => $name, )*
131                }
132            }
133
134            /// How the instruction's operands are laid out.
135            pub const fn layout(self) -> Layout {
136                match self {
137                    $( Self::$variant => opcodes!(@layout $layout), )*
138                }
139            }
140        }
141    };
142    (@layout [ $( $kind:ident ),* ]) => { Layout::Fixed(&[ $( OperandKind::$kind ),* ]) };
143    (@layout InlineString) => { Layout::InlineString };
144    (@layout Switch) => { Layout::Switch };
145    (@layout NamedArgTable) => { Layout::NamedArgTable };
146}
147
148opcodes! {
149    0x01 Push0 "push_0" [];
150    0x02 Push1 "push_1" [];
151    0x03 PushInt8 "pushint8" [S8];
152    0x04 PushInt "pushint" [S32];
153    0x05 PushStr "pushstr" [String];
154    0x06 PushLst "pushlst" [List];
155    0x07 PushObj "pushobj" [Object];
156    0x08 PushNil "pushnil" [];
157    0x09 PushTrue "pushtrue" [];
158    0x0A PushPropId "pushpropid" [Property];
159    0x0B PushFnPtr "pushfnptr" [Function];
160    0x0C PushStrI "pushstri" InlineString;
161    0x0D PushParLst "pushparlst" [U8];
162    0x0E MakeLstPar "makelstpar" [];
163    0x0F PushEnum "pushenum" [Enum];
164    0x10 PushBifPtr "pushbifptr" [FunctionIndex16, FunctionSet16];
165
166    0x20 Neg "neg" [];
167    0x21 BNot "bnot" [];
168    0x22 Add "add" [];
169    0x23 Sub "sub" [];
170    0x24 Mul "mul" [];
171    0x25 BAnd "band" [];
172    0x26 BOr "bor" [];
173    0x27 Shl "shl" [];
174    0x28 AShr "ashr" [];
175    0x29 Xor "xor" [];
176    0x2A Div "div" [];
177    0x2B Mod "mod" [];
178    0x2C Not "not" [];
179    0x2D Boolize "boolize" [];
180    0x2E Inc "inc" [];
181    0x2F Dec "dec" [];
182    0x30 LShr "lshr" [];
183
184    0x40 Eq "eq" [];
185    0x41 Ne "ne" [];
186    0x42 Lt "lt" [];
187    0x43 Le "le" [];
188    0x44 Gt "gt" [];
189    0x45 Ge "ge" [];
190
191    0x50 RetVal "retval" [];
192    0x51 RetNil "retnil" [];
193    0x52 RetTrue "rettrue" [];
194    0x54 Ret "ret" [];
195    0x56 NamedArgPtr "namedargptr" [Argc8, TableOffset];
196    0x57 NamedArgTab "namedargtab" NamedArgTable;
197    0x58 Call "call" [Argc8, Function];
198    0x59 PtrCall "ptrcall" [Argc8];
199
200    0x60 GetProp "getprop" [Property];
201    0x61 CallProp "callprop" [Argc8, Property];
202    0x62 PtrCallProp "ptrcallprop" [Argc8];
203    0x63 GetPropSelf "getpropself" [Property];
204    0x64 CallPropSelf "callpropself" [Argc8, Property];
205    0x65 PtrCallPropSelf "ptrcallpropself" [Argc8];
206    0x66 ObjGetProp "objgetprop" [Object, Property];
207    0x67 ObjCallProp "objcallprop" [Argc8, Object, Property];
208    0x68 GetPropData "getpropdata" [Property];
209    0x69 PtrGetPropData "ptrgetpropdata" [];
210    0x6A GetPropLcl1 "getproplcl1" [Local8, Property];
211    0x6B CallPropLcl1 "callproplcl1" [Argc8, Local8, Property];
212    0x6C GetPropR0 "getpropr0" [Property];
213    0x6D CallPropR0 "callpropr0" [Argc8, Property];
214
215    0x72 Inherit "inherit" [Argc8, Property];
216    0x73 PtrInherit "ptrinherit" [Argc8];
217    0x74 ExpInherit "expinherit" [Argc8, Property, Object];
218    0x75 PtrExpInherit "ptrexpinherit" [Argc8, Object];
219    0x76 VarArgc "varargc" [];
220    0x77 Delegate "delegate" [Argc8, Property];
221    0x78 PtrDelegate "ptrdelegate" [Argc8];
222
223    0x7A Swap2 "swap2" [];
224    0x7B SwapN "swapn" [U8, U8];
225    0x7C GetArgN0 "getargn0" [];
226    0x7D GetArgN1 "getargn1" [];
227    0x7E GetArgN2 "getargn2" [];
228    0x7F GetArgN3 "getargn3" [];
229
230    0x80 GetLcl1 "getlcl1" [Local8];
231    0x81 GetLcl2 "getlcl2" [Local16];
232    0x82 GetArg1 "getarg1" [Arg8];
233    0x83 GetArg2 "getarg2" [Arg16];
234    0x84 PushSelf "pushself" [];
235    0x85 GetDbLcl "getdblcl" [Local16, U16];
236    0x86 GetDbArg "getdbarg" [Arg16, U16];
237    0x87 GetArgc "getargc" [];
238    0x88 Dup "dup" [];
239    0x89 Disc "disc" [];
240    0x8A Disc1 "disc1" [U8];
241    0x8B GetR0 "getr0" [];
242    0x8C GetDbArgc "getdbargc" [U16];
243    0x8D Swap "swap" [];
244    0x8E PushCtxEle "pushctxele" [U8];
245    0x8F Dup2 "dup2" [];
246
247    0x90 Switch "switch" Switch;
248    0x91 Jmp "jmp" [Branch];
249    0x92 Jt "jt" [Branch];
250    0x93 Jf "jf" [Branch];
251    0x94 Je "je" [Branch];
252    0x95 Jne "jne" [Branch];
253    0x96 Jgt "jgt" [Branch];
254    0x97 Jge "jge" [Branch];
255    0x98 Jlt "jlt" [Branch];
256    0x99 Jle "jle" [Branch];
257    0x9A Jst "jst" [Branch];
258    0x9B Jsf "jsf" [Branch];
259    0x9C LJsr "ljsr" [Branch];
260    0x9D LRet "lret" [Local16];
261    0x9E JNil "jnil" [Branch];
262    0x9F JNotNil "jnotnil" [Branch];
263    0xA0 JR0T "jr0t" [Branch];
264    0xA1 JR0F "jr0f" [Branch];
265    0xA2 IterNext "iternext" [Local16, Branch];
266    0xA3 GetSetLcl1R0 "getsetlcl1r0" [Local8];
267    0xA4 GetSetLcl1 "getsetlcl1" [Local8];
268    0xA5 DupR0 "dupr0" [];
269    0xA6 GetSpN "getspn" [U8];
270
271    0xAA GetLclN0 "getlcln0" [];
272    0xAB GetLclN1 "getlcln1" [];
273    0xAC GetLclN2 "getlcln2" [];
274    0xAD GetLclN3 "getlcln3" [];
275    0xAE GetLclN4 "getlcln4" [];
276    0xAF GetLclN5 "getlcln5" [];
277
278    0xB0 Say "say" [String];
279    0xB1 BuiltinA "builtin_a" [Argc8, FunctionIndex8];
280    0xB2 BuiltinB "builtin_b" [Argc8, FunctionIndex8];
281    0xB3 BuiltinC "builtin_c" [Argc8, FunctionIndex8];
282    0xB4 BuiltinD "builtin_d" [Argc8, FunctionIndex8];
283    0xB5 Builtin1 "builtin1" [Argc8, FunctionIndex8, FunctionSet8];
284    0xB6 Builtin2 "builtin2" [Argc8, FunctionIndex16, FunctionSet8];
285    0xB7 CallExt "callext" [];
286    0xB8 Throw "throw" [];
287    0xB9 SayVal "sayval" [];
288    0xBA Index "index" [];
289    0xBB IdxLcl1Int8 "idxlcl1int8" [Local8, U8];
290    0xBC IdxInt8 "idxint8" [U8];
291
292    0xC0 New1 "new1" [Argc8, Metaclass8];
293    0xC1 New2 "new2" [Argc16, Metaclass16];
294    0xC2 TrNew1 "trnew1" [Argc8, Metaclass8];
295    0xC3 TrNew2 "trnew2" [Argc16, Metaclass16];
296
297    0xD0 IncLcl "inclcl" [Local16];
298    0xD1 DecLcl "declcl" [Local16];
299    0xD2 AddILcl1 "addilcl1" [Local8, S8];
300    0xD3 AddILcl4 "addilcl4" [Local16, S32];
301    0xD4 AddToLcl "addtolcl" [Local16];
302    0xD5 SubFromLcl "subfromlcl" [Local16];
303    0xD6 ZeroLcl1 "zerolcl1" [Local8];
304    0xD7 ZeroLcl2 "zerolcl2" [Local16];
305    0xD8 NilLcl1 "nillcl1" [Local8];
306    0xD9 NilLcl2 "nillcl2" [Local16];
307    0xDA OneLcl1 "onelcl1" [Local8];
308    0xDB OneLcl2 "onelcl2" [Local16];
309
310    0xE0 SetLcl1 "setlcl1" [Local8];
311    0xE1 SetLcl2 "setlcl2" [Local16];
312    0xE2 SetArg1 "setarg1" [Arg8];
313    0xE3 SetArg2 "setarg2" [Arg16];
314    0xE4 SetInd "setind" [];
315    0xE5 SetProp "setprop" [Property];
316    0xE6 PtrSetProp "ptrsetprop" [];
317    0xE7 SetPropSelf "setpropself" [Property];
318    0xE8 ObjSetProp "objsetprop" [Object, Property];
319    0xE9 SetDbLcl "setdblcl" [Local16, U16];
320    0xEA SetDbArg "setdbarg" [Arg16, U16];
321    0xEB SetSelf "setself" [];
322    0xEC LoadCtx "loadctx" [];
323    0xED StoreCtx "storectx" [];
324    0xEE SetLcl1R0 "setlcl1r0" [Local8];
325    0xEF SetIndLcl1I8 "setindlcl1i8" [Local8, U8];
326
327    0xF1 Bp "bp" [];
328    0xF2 Nop "nop" [];
329}