1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use core::mem::size_of;
pub type LocalIndex = u16;
pub type UpvalueIndex = u16;
const OP_NOP: u8 = 0x00;
const OP_RETURN: u8 = 0x01;
const OP_CALL: u8 = 0x02;
const OP_CALL_UNPACK: u8 = 0x03;
const OP_POP: u8 = 0x10;
const OP_DROP: u8 = 0x11;
const OP_CLONE: u8 = 0x12;
const OP_TUPLE: u8 = 0x13;
const OP_TUPLEN: u8 = 0x14;
const OP_ITER_INIT: u8 = 0x18;
const OP_ITER_NEXT: u8 = 0x19;
const OP_LD_FUN: u8 = 0x40;
const OP_LD_FUN_16: u8 = 0x41;
const OP_LD_CONST: u8 = 0x42;
const OP_LD_CONST_16: u8 = 0x43;
const OP_IN_GLOBAL_IM: u8 = 0x48;
const OP_IN_GLOBAL_MUT: u8 = 0x49;
const OP_ST_GLOBAL: u8 = 0x4A;
const OP_LD_GLOBAL: u8 = 0x4B;
const OP_DP_GLOBAL: u8 = 0x4C;
const OP_IN_LOCAL: u8 = 0x50;
const OP_ST_LOCAL: u8 = 0x51;
const OP_ST_LOCAL_16: u8 = 0x52;
const OP_LD_LOCAL: u8 = 0x53;
const OP_LD_LOCAL_16: u8 = 0x54;
const OP_DP_LOCALS: u8 = 0x55;
const OP_ST_UPVAL: u8 = 0x58;
const OP_ST_UPVAL_16: u8 = 0x59;
const OP_LD_UPVAL: u8 = 0x5A;
const OP_LD_UPVAL_16: u8 = 0x5B;
const OP_CLOSE_UPVAL: u8 = 0x5C;
const OP_CLOSE_UPVAL_16: u8 = 0x5D;
const OP_LD_NIL: u8 = 0x60;
const OP_LD_FALSE: u8 = 0x61;
const OP_LD_TRUE: u8 = 0x62;
const OP_LD_EMPTY: u8 = 0x63;
const OP_LD_U8: u8 = 0x64;
const OP_LD_I8: u8 = 0x65;
const OP_LD_I16: u8 = 0x66;
const OP_NEG: u8 = 0x70;
const OP_POS: u8 = 0x71;
const OP_INV: u8 = 0x72;
const OP_NOT: u8 = 0x73;
const OP_AND: u8 = 0x78;
const OP_XOR: u8 = 0x79;
const OP_OR: u8 = 0x7A;
const OP_SHL: u8 = 0x7B;
const OP_SHR: u8 = 0x7C;
const OP_ADD: u8 = 0x80;
const OP_SUB: u8 = 0x81;
const OP_MUL: u8 = 0x82;
const OP_DIV: u8 = 0x83;
const OP_MOD: u8 = 0x84;
const OP_EQ: u8 = 0x88;
const OP_NE: u8 = 0x89;
const OP_LT: u8 = 0x8A;
const OP_LE: u8 = 0x8B;
const OP_GE: u8 = 0x8C;
const OP_GT: u8 = 0x8D;
const OP_JUMP: u8 = 0x90;
const OP_JUMP_FALSE: u8 = 0x91;
const OP_JUMP_TRUE: u8 = 0x92;
const OP_PJMP_FALSE: u8 = 0x93;
const OP_PJMP_TRUE: u8 = 0x94;
const OP_LJUMP: u8 = 0x98;
const OP_LJUMP_FALSE: u8 = 0x99;
const OP_LJUMP_TRUE: u8 = 0x9A;
const OP_PLJMP_FALSE: u8 = 0x9B;
const OP_PLJMP_TRUE: u8 = 0x9C;
const DBG_INSPECT: u8 = 0xF0;
const DBG_ASSERT: u8 = 0xF1;
const DBG_DUMP_STACK: u8 = 0xF2;
const DBG_DUMP_GLOBALS: u8 = 0xF3;
const DBG_DUMP_STRINGS: u8 = 0xF4;
#[repr(u8)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum OpCode {
Nop = OP_NOP,
Return = OP_RETURN,
Call = OP_CALL,
CallUnpack = OP_CALL_UNPACK,
Pop = OP_POP,
Drop = OP_DROP,
Clone = OP_CLONE,
Tuple = OP_TUPLE,
TupleN = OP_TUPLEN,
LoadFunction = OP_LD_FUN,
LoadFunction16 = OP_LD_FUN_16,
LoadConst = OP_LD_CONST,
LoadConst16 = OP_LD_CONST_16,
InsertGlobal = OP_IN_GLOBAL_IM,
InsertGlobalMut = OP_IN_GLOBAL_MUT,
StoreGlobal = OP_ST_GLOBAL,
LoadGlobal = OP_LD_GLOBAL,
InsertLocal = OP_IN_LOCAL,
StoreLocal = OP_ST_LOCAL,
StoreLocal16 = OP_ST_LOCAL_16,
LoadLocal = OP_LD_LOCAL,
LoadLocal16 = OP_LD_LOCAL_16,
DropLocals = OP_DP_LOCALS,
StoreUpvalue = OP_ST_UPVAL,
StoreUpvalue16 = OP_ST_UPVAL_16,
LoadUpvalue = OP_LD_UPVAL,
LoadUpvalue16 = OP_LD_UPVAL_16,
CloseUpvalue = OP_CLOSE_UPVAL,
CloseUpvalue16 = OP_CLOSE_UPVAL_16,
Nil = OP_LD_NIL,
True = OP_LD_TRUE,
False = OP_LD_FALSE,
Empty = OP_LD_EMPTY,
UInt8 = OP_LD_U8,
Int8 = OP_LD_I8,
Int16 = OP_LD_I16,
Neg = OP_NEG,
Pos = OP_POS,
Inv = OP_INV,
Not = OP_NOT,
And = OP_AND,
Xor = OP_XOR,
Or = OP_OR,
Shl = OP_SHL,
Shr = OP_SHR,
Add = OP_ADD,
Sub = OP_SUB,
Mul = OP_MUL,
Div = OP_DIV,
Mod = OP_MOD,
EQ = OP_EQ,
NE = OP_NE,
LT = OP_LT,
LE = OP_LE,
GE = OP_GE,
GT = OP_GT,
Jump = OP_JUMP,
JumpIfFalse = OP_JUMP_FALSE,
JumpIfTrue = OP_JUMP_TRUE,
PopJumpIfFalse = OP_PJMP_FALSE,
PopJumpIfTrue = OP_PJMP_TRUE,
LongJump = OP_LJUMP,
LongJumpIfFalse = OP_LJUMP_FALSE,
LongJumpIfTrue = OP_LJUMP_TRUE,
PopLongJumpIfFalse = OP_PLJMP_FALSE,
PopLongJumpIfTrue = OP_PLJMP_TRUE,
Inspect = DBG_INSPECT,
Assert = DBG_ASSERT,
}
impl OpCode {
#[inline]
pub fn from_byte(byte: u8) -> Option<OpCode> {
let opcode = match byte {
OP_NOP => Self::Nop,
OP_RETURN => Self::Return,
OP_CALL => Self::Call,
OP_CALL_UNPACK => Self::CallUnpack,
OP_POP => Self::Pop,
OP_DROP => Self::Drop,
OP_CLONE => Self::Clone,
OP_TUPLE => Self::Tuple,
OP_TUPLEN => Self::TupleN,
OP_LD_FUN => Self::LoadFunction,
OP_LD_FUN_16 => Self::LoadFunction16,
OP_LD_CONST => Self::LoadConst,
OP_LD_CONST_16 => Self::LoadConst16,
OP_IN_GLOBAL_IM => Self::InsertGlobal,
OP_IN_GLOBAL_MUT => Self::InsertGlobalMut,
OP_ST_GLOBAL => Self::StoreGlobal,
OP_LD_GLOBAL => Self::LoadGlobal,
OP_IN_LOCAL => Self::InsertLocal,
OP_ST_LOCAL => Self::StoreLocal,
OP_ST_LOCAL_16 => Self::StoreLocal16,
OP_LD_LOCAL => Self::LoadLocal,
OP_LD_LOCAL_16 => Self::LoadLocal16,
OP_DP_LOCALS => Self::DropLocals,
OP_ST_UPVAL => Self::StoreUpvalue,
OP_ST_UPVAL_16 => Self::StoreUpvalue16,
OP_LD_UPVAL => Self::LoadUpvalue,
OP_LD_UPVAL_16 => Self::LoadUpvalue16,
OP_CLOSE_UPVAL => Self::CloseUpvalue,
OP_CLOSE_UPVAL_16 => Self::CloseUpvalue16,
OP_LD_NIL => Self::Nil,
OP_LD_TRUE => Self::True,
OP_LD_FALSE => Self::False,
OP_LD_EMPTY => Self::Empty,
OP_LD_U8 => Self::UInt8,
OP_LD_I8 => Self::Int8,
OP_LD_I16 => Self::Int16,
OP_NEG => Self::Neg,
OP_POS => Self::Pos,
OP_INV => Self::Inv,
OP_NOT => Self::Not,
OP_AND => Self::And,
OP_XOR => Self::Xor,
OP_OR => Self::Or,
OP_SHL => Self::Shl,
OP_SHR => Self::Shr,
OP_ADD => Self::Add,
OP_SUB => Self::Sub,
OP_MUL => Self::Mul,
OP_DIV => Self::Div,
OP_MOD => Self::Mod,
OP_EQ => Self::EQ,
OP_NE => Self::NE,
OP_LT => Self::LT,
OP_LE => Self::LE,
OP_GE => Self::GE,
OP_GT => Self::GT,
OP_JUMP => Self::Jump,
OP_JUMP_FALSE => Self::JumpIfFalse,
OP_JUMP_TRUE => Self::JumpIfTrue,
OP_PJMP_FALSE => Self::PopJumpIfFalse,
OP_PJMP_TRUE => Self::PopJumpIfTrue,
OP_LJUMP => Self::LongJump,
OP_LJUMP_FALSE => Self::LongJumpIfFalse,
OP_LJUMP_TRUE => Self::LongJumpIfTrue,
OP_PLJMP_FALSE => Self::PopLongJumpIfFalse,
OP_PLJMP_TRUE => Self::PopLongJumpIfTrue,
DBG_INSPECT => Self::Inspect,
DBG_ASSERT => Self::Assert,
_ => return None,
};
Some(opcode)
}
#[inline]
pub const fn instr_len(&self) -> usize {
match self {
Self::Drop => 1 + size_of::<u8>(),
Self::LoadFunction => 1 + size_of::<u8>(),
Self::LoadFunction16 => 1 + size_of::<u16>(),
Self::LoadConst => 1 + size_of::<u8>(),
Self::LoadConst16 => 1 + size_of::<u16>(),
Self::StoreLocal => 1 + size_of::<u8>(),
Self::StoreLocal16 => 1 + size_of::<u16>(),
Self::LoadLocal => 1 + size_of::<u8>(),
Self::LoadLocal16 => 1 + size_of::<u16>(),
Self::DropLocals => 1 + size_of::<u8>(),
Self::StoreUpvalue => 1 + size_of::<u8>(),
Self::StoreUpvalue16 => 1 + size_of::<u16>(),
Self::LoadUpvalue => 1 + size_of::<u8>(),
Self::LoadUpvalue16 => 1 + size_of::<u16>(),
Self::CloseUpvalue => 1 + size_of::<u8>(),
Self::CloseUpvalue16 => 1 + size_of::<u16>(),
Self::Tuple => 1 + size_of::<u8>(),
Self::UInt8 => 1 + size_of::<u8>(),
Self::Int8 => 1 + size_of::<i8>(),
Self::Int16 => 1 + size_of::<i16>(),
Self::Jump => 1 + size_of::<i16>(),
Self::JumpIfFalse => 1 + size_of::<i16>(),
Self::JumpIfTrue => 1 + size_of::<i16>(),
Self::PopJumpIfFalse => 1 + size_of::<i16>(),
Self::PopJumpIfTrue => 1 + size_of::<i16>(),
_ => 1,
}
}
}
impl From<OpCode> for u8 {
fn from(opcode: OpCode) -> Self { opcode as u8 }
}
impl TryFrom<u8> for OpCode {
type Error = u8;
fn try_from(byte: u8) -> Result<Self, Self::Error> {
if let Some(opcode) = OpCode::from_byte(byte) {
Ok(opcode)
} else {
Err(byte)
}
}
}
impl PartialEq<u8> for OpCode {
fn eq(&self, other: &u8) -> bool { *other == (*self).into() }
}
impl core::fmt::Display for OpCode {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mnemonic = match *self {
Self::Nop => "NOP",
Self::Return => "RETURN",
Self::Call => "CALL",
Self::CallUnpack => "CALL_UNPACK",
Self::Pop => "POP",
Self::Drop => "DROP",
Self::Clone => "CLONE",
Self::Tuple => "TUPLE",
Self::TupleN => "TUPLEN",
Self::LoadFunction => "OP_LD_FUN",
Self::LoadFunction16 => "OP_LD_FUN_16",
Self::LoadConst => "LD_CONST",
Self::LoadConst16 => "LD_CONST_16",
Self::InsertGlobal => "IN_GLOBAL_IM",
Self::InsertGlobalMut => "IN_GLOBAL_MUT",
Self::StoreGlobal => "ST_GLOBAL",
Self::LoadGlobal => "LD_GLOBAL",
Self::InsertLocal => "IN_LOCAL",
Self::StoreLocal => "ST_LOCAL",
Self::StoreLocal16 => "ST_LOCAL_16",
Self::LoadLocal => "LD_LOCAL",
Self::LoadLocal16 => "LD_LOCAL_16",
Self::DropLocals => "DP_LOCALS",
Self::StoreUpvalue => "ST_UPVAL",
Self::StoreUpvalue16 => "ST_UPVAL_16",
Self::LoadUpvalue => "LD_UPVAL",
Self::LoadUpvalue16 => "LD_UPVAL_16",
Self::CloseUpvalue => "CLOSE_UPVAL",
Self::CloseUpvalue16 => "CLOSE_UPVAL_16",
Self::Nil => "LD_NIL",
Self::True => "LD_TRUE",
Self::False => "LD_FALSE",
Self::Empty => "LD_EMPTY",
Self::UInt8 => "LD_U8",
Self::Int8 => "LD_I8",
Self::Int16 => "LD_I16",
Self::Neg => "NEG",
Self::Pos => "POS",
Self::Inv => "INV",
Self::Not => "NOT",
Self::And => "AND",
Self::Xor => "XOR",
Self::Or => "OR",
Self::Shl => "SHL",
Self::Shr => "SHR",
Self::Add => "ADD",
Self::Sub => "SUB",
Self::Mul => "MUL",
Self::Div => "DIV",
Self::Mod => "MOD",
Self::EQ => "CMP_EQ",
Self::NE => "CMP_NE",
Self::LT => "CMP_LT",
Self::LE => "CMP_LE",
Self::GE => "CMP_GE",
Self::GT => "CMP_GT",
Self::Jump => "JUMP",
Self::JumpIfFalse => "JUMP_FALSE",
Self::JumpIfTrue => "JUMP_TRUE",
Self::PopJumpIfFalse => "PJMP_FALSE",
Self::PopJumpIfTrue => "PJMP_TRUE",
Self::LongJump => "LJUMP",
Self::LongJumpIfFalse => "LJUMP_FALSE",
Self::LongJumpIfTrue => "LJUMP_TRUE",
Self::PopLongJumpIfFalse => "PLJMP_FALSE",
Self::PopLongJumpIfTrue => "PLJMP_TRUE",
Self::Inspect => "DBG_INSPECT",
Self::Assert => "DBG_ASSERT",
};
if let Some(width) = fmt.width() {
write!(fmt, "{:1$}", mnemonic, width)
} else {
fmt.write_str(mnemonic)
}
}
}