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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Comments from lopcodes.h
// We assume that instructions are unsigned numbers.
// All instructions have an opcode in the first 6 bits.
// Instructions can have the following fields:
// 	'A' : 8 bits
// 	'B' : 9 bits
// 	'C' : 9 bits
// 	'Ax' : 26 bits ('A', 'B', and 'C' together)
// 	'Bx' : 18 bits ('B' and 'C' together)
//  'sBx' : signed Bx

// A signed argument is represented in excess K; that is, the number
// value is the unsigned value minus K. K is exactly the maximum value
// for that argument (so that -max is represented by 0, and +max is
// represented by 2*max), which is half the maximum for the corresponding
// unsigned argument.

#[derive(Debug, Copy, Clone)]
pub enum OpMode {
    IA,
    IAB,
    IABC,
    IABx,
    IAsBx,
    IAx,
    IAC,
}

pub const SIZE_OP: u32 = 6;
pub const SIZE_A: u32 = 8;
pub const SIZE_B: u32 = 9;
pub const SIZE_C: u32 = 9;
pub const SIZE_AX: u32 = SIZE_C + SIZE_B + SIZE_A;
pub const SIZE_BX: u32 = SIZE_C + SIZE_B;

pub const POS_OP: u32 = 0;
pub const POS_A: u32 = POS_OP + SIZE_OP;
pub const POS_C: u32 = POS_A + SIZE_A;
pub const POS_B: u32 = POS_C + SIZE_C;
pub const POS_BX: u32 = POS_C;
pub const POS_AX: u32 = POS_A;

pub const MAXARG_A: u32 = (1 << SIZE_A) - 1;
pub const MAXARG_B: u32 = (1 << SIZE_B) - 1;
pub const MAXARG_C: u32 = (1 << SIZE_C) - 1;
pub const MAXARG_AX: u32 = (1 << SIZE_AX) - 1;
pub const MAXARG_BX: u32 = (1 << SIZE_BX) - 1;
pub const MAXARG_SBX: i32 = (MAXARG_BX as i32) >> 1;

pub const MASK_K: u32 = 1 << (SIZE_B - 1);

pub const NO_JUMP: i32 = -1;
pub const NO_REG: u32 = MAXARG_A;

pub fn is_const(index: u32) -> bool {
    index & MASK_K != 0
}

pub fn is_var(index: u32) -> bool {
    !is_const(index)
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OpCode {
    // A B
    // R(A) := R(B)
    Move = 0,
    // A Bx
    // R(A) := Kst(Bx)
    LoadK,
    // A
    // R(A) := Kst(extra arg)
    LoadKx,
    // A B C
    // R(A) := (bool)B;
    // if (C) pc++
    LoadBool,
    // A B
    // R(A), R(A + 1), ..., R(A + B) := nil
    LoadNil,
    // A B
    // R(A) := UpValue[B]
    GetUpVal,

    // A B C
    // R(A) := UpValue[B][RK(C)]
    GetTabUp,
    // A B C
    // R(A) := R(B)[RK(C)]
    GetTable,

    // A B C
    // UpValue[A][RK(B)] := RK(C)
    SetTabUp,
    // A B
    // UpValue[B] := R(A)
    SetUpVal,
    // A B C
    // R(A)[RK(B)] := RK(C)
    SetTable,

    // A B C
    // R(A) := {} (size = B, C)
    NewTable,

    // A B C
    // R(A + 1) := R(B);
    // R(A) := R(B)[RK(C)]
    Self_,

    // A B C
    // R(A) := RK(B) + RK(C)
    Add,
    // A B C
    // R(A) := RK(B) - RK(C)
    Sub,
    // A B C
    // R(A) := RK(B) * RK(C)
    Mul,
    // A B C
    // R(A) := RK(B) % RK(C)
    Mod,
    // A B C
    // R(A) := RK(B) ^ RK(C)
    Pow,
    // A B C
    // R(A) := RK(B) / RK(C)
    Div,
    // A B C
    // R(A) := RK(B) // RK(C)
    IDiv,
    // A B C
    // R(A) := RK(B) & RK(C)
    BAdd,
    // A B C
    // R(A) := RK(B) | RK(C)
    BOr,
    // A B C
    // R(A) := RK(B) ~ RK(C)
    BXor,
    // A B C
    // R(A) := RK(B) << RK(C)
    Shl,
    // A B C
    // R(A) := RK(B) >> RK(C)
    Shr,

    // A B
    // R(A) := -R(B)
    Unm,
    // A B
    // R(A) := ~R(B)
    BNot,
    // A B
    // R(A) := not R(B)
    Not,
    // A B
    // R(A) := # R(B)
    Len,

    // A B C
    // R(A) := R(B).. ... ..R(C)
    Concat,

    // A sBx
    // pc += sBx;
    // if (A) close all upvalues >= R(A - 1)
    Jmp,

    // A B C
    // if ((RK(B) == RK(C)) ~= A) then pc++
    Eq,
    // A B C
    // if ((RK(B) < RK(C)) ~= A) then pc++
    Lt,
    // A B C
    // if ((RK(B) <= RK(C)) ~= A) then pc++
    Le,

    // A C
    // if not (R(A) <=> C) then pc++
    Test,
    // A B C
    // if (R(B) <=> C) then R(A) := R(B) else pc++
    TestSet,

    // A B C
    // R(A), ... , R(A + C - 2) := R(A)(R(A + 1), ... , R(A + B - 1))
    Call,

    // A B C
    // return R(A)(R(A + 1), ..., R(A + B - 1))
    TailCall,

    // A B
    // return R(A), ... , R(A + B - 2)
    Return,

    // A sBx
    // R(A) += R(A+2);
    // if R(A) <= R(A + 1) then { pc += sBx; R(A+3) = R(A) }
    ForLoop,
    // A sBx
    // R(A) -= R(A + 2);
    // pc += sBx
    ForPrep,

    // A C
    // R(A + 3), ... R(A + 2 + C) := R(A)(R(A + 1), R(A + 2))
    TForCall,
    // A sBx
    // if R(A + 1) ~= nil then { R(A) = R(A + 1); pc += sBx }
    TForLoop,

    // A B C
    // R(A)[(C-1)*FPF + i] := R(A + i), i <= i <= B
    SetList,

    // A Bx
    // R(A) := closure(KPROTO[Bx])
    Closure,

    // A B
    // R(A), R(A + 1), ..., R(A + B - 2) = vararg
    Vararg,

    // Ax
    // extra (larger) argument for previous opcode
    ExtraArg,
}

impl OpCode {
    pub fn from_u32(u: u32) -> OpCode {
        match u {
            _ if OpCode::Move as u32 == u => OpCode::Move,
            _ if OpCode::LoadK as u32 == u => OpCode::LoadK,
            _ if OpCode::LoadKx as u32 == u => OpCode::LoadKx,
            _ if OpCode::LoadBool as u32 == u => OpCode::LoadBool,
            _ if OpCode::LoadNil as u32 == u => OpCode::LoadNil,
            _ if OpCode::GetUpVal as u32 == u => OpCode::GetUpVal,
            _ if OpCode::GetTabUp as u32 == u => OpCode::GetTabUp,
            _ if OpCode::GetTable as u32 == u => OpCode::GetTable,
            _ if OpCode::SetTabUp as u32 == u => OpCode::SetTabUp,
            _ if OpCode::SetUpVal as u32 == u => OpCode::SetUpVal,
            _ if OpCode::SetTable as u32 == u => OpCode::SetTable,
            _ if OpCode::NewTable as u32 == u => OpCode::NewTable,
            _ if OpCode::Self_ as u32 == u => OpCode::Self_,
            _ if OpCode::Add as u32 == u => OpCode::Add,
            _ if OpCode::Sub as u32 == u => OpCode::Sub,
            _ if OpCode::Mul as u32 == u => OpCode::Mul,
            _ if OpCode::Mod as u32 == u => OpCode::Mod,
            _ if OpCode::Pow as u32 == u => OpCode::Pow,
            _ if OpCode::Div as u32 == u => OpCode::Div,
            _ if OpCode::IDiv as u32 == u => OpCode::IDiv,
            _ if OpCode::BAdd as u32 == u => OpCode::BAdd,
            _ if OpCode::BOr as u32 == u => OpCode::BOr,
            _ if OpCode::BXor as u32 == u => OpCode::BXor,
            _ if OpCode::Shl as u32 == u => OpCode::Shl,
            _ if OpCode::Shr as u32 == u => OpCode::Shr,
            _ if OpCode::Unm as u32 == u => OpCode::Unm,
            _ if OpCode::BNot as u32 == u => OpCode::BNot,
            _ if OpCode::Not as u32 == u => OpCode::Not,
            _ if OpCode::Len as u32 == u => OpCode::Len,
            _ if OpCode::Concat as u32 == u => OpCode::Concat,
            _ if OpCode::Jmp as u32 == u => OpCode::Jmp,
            _ if OpCode::Eq as u32 == u => OpCode::Eq,
            _ if OpCode::Lt as u32 == u => OpCode::Lt,
            _ if OpCode::Le as u32 == u => OpCode::Le,
            _ if OpCode::Test as u32 == u => OpCode::Test,
            _ if OpCode::TestSet as u32 == u => OpCode::TestSet,
            _ if OpCode::Call as u32 == u => OpCode::Call,
            _ if OpCode::TailCall as u32 == u => OpCode::TailCall,
            _ if OpCode::Return as u32 == u => OpCode::Return,
            _ if OpCode::ForLoop as u32 == u => OpCode::ForLoop,
            _ if OpCode::ForPrep as u32 == u => OpCode::ForPrep,
            _ if OpCode::TForCall as u32 == u => OpCode::TForCall,
            _ if OpCode::TForLoop as u32 == u => OpCode::TForLoop,
            _ if OpCode::SetList as u32 == u => OpCode::SetList,
            _ if OpCode::Closure as u32 == u => OpCode::Closure,
            _ if OpCode::Vararg as u32 == u => OpCode::Vararg,
            _ if OpCode::ExtraArg as u32 == u => OpCode::ExtraArg,
            _ => unreachable!("unknown op code : {}!", u),
        }
    }
}

pub struct Instruction(u32);

#[allow(dead_code)]
#[allow(non_snake_case)]
impl Instruction {
    pub fn new() -> Self {
        Instruction(0)
    }

    pub fn get_op(&self) -> OpCode {
        OpCode::from_u32(((self.0) >> POS_OP) & Instruction::mask1(SIZE_OP, 0))
    }

    pub fn set_op(&mut self, op: OpCode) {
        self.set_arg(op as u32, POS_OP, SIZE_OP)
    }

    pub fn get_arg_A(&self) -> u32 {
        self.get_arg(POS_A, SIZE_A)
    }

    pub fn set_arg_A(&mut self, value: u32) {
        self.set_arg(value, POS_A, SIZE_A);
    }

    pub fn get_arg_B(&self) -> u32 {
        self.get_arg(POS_B, SIZE_B)
    }

    pub fn set_arg_B(&mut self, value: u32) {
        self.set_arg(value, POS_B, SIZE_B);
    }

    pub fn get_arg_C(&self) -> u32 {
        self.get_arg(POS_C, SIZE_C)
    }

    pub fn set_arg_C(&mut self, value: u32) {
        self.set_arg(value, POS_C, SIZE_C);
    }

    pub fn get_arg_Ax(&self) -> u32 {
        self.get_arg(POS_AX, SIZE_AX)
    }

    pub fn set_arg_Ax(&mut self, value: u32) {
        self.set_arg(value, POS_AX, SIZE_AX);
    }

    pub fn get_arg_Bx(&self) -> u32 {
        self.get_arg(POS_BX, SIZE_BX)
    }

    pub fn set_arg_Bx(&mut self, value: u32) {
        self.set_arg(value, POS_BX, SIZE_BX);
    }

    pub fn get_arg_sBx(&self) -> i32 {
        (self.get_arg(POS_BX, SIZE_BX) as i32) - MAXARG_SBX
    }

    pub fn set_arg_sBx(&mut self, value: i32) {
        self.set_arg((value + MAXARG_SBX) as u32, POS_BX, SIZE_BX);
    }

    pub fn create_ABC(op: OpCode, a: u32, b: u32, c: u32) -> Self {
        Instruction(((op as u32) << POS_OP) | (a << POS_A) | (b << POS_B) | (c << POS_C))
    }

    pub fn create_ABx(op: OpCode, a: u32, bx: u32) -> Self {
        Instruction(((op as u32) << POS_OP) | (a << POS_A) | (bx << POS_BX))
    }

    pub fn create_AsBx(op: OpCode, a: u32, sBx: i32) -> Self {
        Instruction(
            ((op as u32) << POS_OP) | (a << POS_A) | (((sBx + MAXARG_SBX) as u32) << POS_BX),
        )
    }

    pub fn create_Ax(op: OpCode, a: u32) -> Self {
        Instruction(((op as u32) << POS_OP) | (a << POS_AX))
    }

    pub fn save(&mut self, a: u32) {
        let mask = !(((1 << SIZE_A) - 1) << POS_A);
        self.0 = (self.0 & mask) | (a << POS_A);
    }

    fn get_arg(&self, pos: u32, size: u32) -> u32 {
        (self.0 >> pos) & Instruction::mask1(size, 0)
    }

    fn set_arg(&mut self, value: u32, pos: u32, size: u32) {
        self.0 = (Instruction::mask1(size, pos) & (value << pos))
            | (self.0 & Instruction::mask0(size, pos))
    }

    fn mask1(n: u32, p: u32) -> u32 {
        (!((!0u32) << n)) << p
    }

    fn mask0(n: u32, p: u32) -> u32 {
        !Instruction::mask1(n, p)
    }

    pub fn mode(&self) -> OpMode {
        match self.get_op() {
            OpCode::Move => OpMode::IAB,
            OpCode::LoadK => OpMode::IABx,
            OpCode::LoadKx => OpMode::IA,
            OpCode::LoadBool => OpMode::IABC,
            OpCode::LoadNil => OpMode::IAB,
            OpCode::GetUpVal => OpMode::IAB,
            OpCode::GetTabUp => OpMode::IABC,
            OpCode::GetTable => OpMode::IABC,
            OpCode::SetTabUp => OpMode::IABC,
            OpCode::SetUpVal => OpMode::IAB,
            OpCode::SetTable => OpMode::IABC,
            OpCode::NewTable => OpMode::IABC,
            OpCode::Self_ => OpMode::IABC,
            OpCode::Add => OpMode::IABC,
            OpCode::Sub => OpMode::IABC,
            OpCode::Mul => OpMode::IABC,
            OpCode::Mod => OpMode::IABC,
            OpCode::Pow => OpMode::IABC,
            OpCode::Div => OpMode::IABC,
            OpCode::IDiv => OpMode::IABC,
            OpCode::BAdd => OpMode::IABC,
            OpCode::BOr => OpMode::IABC,
            OpCode::BXor => OpMode::IABC,
            OpCode::Shl => OpMode::IABC,
            OpCode::Shr => OpMode::IABC,
            OpCode::Unm => OpMode::IAB,
            OpCode::BNot => OpMode::IAB,
            OpCode::Not => OpMode::IAB,
            OpCode::Len => OpMode::IAB,
            OpCode::Concat => OpMode::IABC,
            OpCode::Jmp => OpMode::IAsBx,
            OpCode::Eq => OpMode::IABC,
            OpCode::Lt => OpMode::IABC,
            OpCode::Le => OpMode::IABC,
            OpCode::Test => OpMode::IAC,
            OpCode::TestSet => OpMode::IABC,
            OpCode::Call => OpMode::IABC,
            OpCode::TailCall => OpMode::IABC,
            OpCode::Return => OpMode::IAB,
            OpCode::ForLoop => OpMode::IAsBx,
            OpCode::ForPrep => OpMode::IAsBx,
            OpCode::TForCall => OpMode::IAC,
            OpCode::TForLoop => OpMode::IAsBx,
            OpCode::SetList => OpMode::IAsBx,
            OpCode::Closure => OpMode::IABx,
            OpCode::Vararg => OpMode::IAB,
            OpCode::ExtraArg => OpMode::IAx,
        }
    }
}

use std::fmt;
impl fmt::Debug for Instruction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.mode() {
            OpMode::IA => write!(
                f,
                "| {:<10} | {:<5} | {:<5} | {:<5} |",
                format!("{:?}", self.get_op()),
                self.get_arg_A(),
                " ",
                " "
            ),
            OpMode::IAB => write!(
                f,
                "| {:<10} | {:<5} | {:<5} | {:<5} |",
                format!("{:?}", self.get_op()),
                self.get_arg_A(),
                self.get_arg_B(),
                " "
            ),
            OpMode::IABC => write!(
                f,
                "| {:<10} | {:<5} | {:<5} | {:<5} |",
                format!("{:?}", self.get_op()),
                self.get_arg_A(),
                self.get_arg_B(),
                self.get_arg_C()
            ),
            OpMode::IAC => write!(
                f,
                "| {:<10} | {:<5} | {:<5} | {:<5} |",
                format!("{:?}", self.get_op()),
                self.get_arg_A(),
                " ",
                self.get_arg_C()
            ),
            OpMode::IABx => write!(
                f,
                "| {:<10} | {:<5} | {:<5} | {:<5} |",
                format!("{:?}", self.get_op()),
                self.get_arg_A(),
                self.get_arg_Bx(),
                " "
            ),
            OpMode::IAsBx => write!(
                f,
                "| {:<10} | {:<5} | {:<5} | {:<5} |",
                format!("{:?}", self.get_op()),
                self.get_arg_A(),
                self.get_arg_sBx(),
                " "
            ),
            OpMode::IAx => write!(
                f,
                "| {:<10} | {:<5} | {:<5} | {:<5} |",
                format!("{:?}", self.get_op()),
                self.get_arg_Ax(),
                " ",
                " "
            ),
        }
    }
}