1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[repr(u8)]
17pub enum OpMode {
18 Abc = 0,
19 ABx = 1,
20 AsBx = 2,
21 Ax = 3,
22 SJ = 4,
23}
24
25pub const SIZE_C: u32 = 8;
29pub const SIZE_B: u32 = 8;
30pub const SIZE_BX: u32 = SIZE_C + SIZE_B + 1;
31pub const SIZE_A: u32 = 8;
32pub const SIZE_AX: u32 = SIZE_BX + SIZE_A;
33pub const SIZE_S_J: u32 = SIZE_BX + SIZE_A;
34pub const SIZE_OP: u32 = 7;
35
36pub const POS_OP: u32 = 0;
40pub const POS_A: u32 = POS_OP + SIZE_OP;
41pub const POS_K: u32 = POS_A + SIZE_A;
42pub const POS_B: u32 = POS_K + 1;
43pub const POS_C: u32 = POS_B + SIZE_B;
44pub const POS_BX: u32 = POS_K;
45pub const POS_AX: u32 = POS_A;
46pub const POS_S_J: u32 = POS_A;
47
48pub const MAXARG_BX: u32 = (1u32 << SIZE_BX) - 1;
52pub const OFFSET_S_BX: i32 = (MAXARG_BX >> 1) as i32;
53pub const MAXARG_AX: u32 = (1u32 << SIZE_AX) - 1;
54pub const MAXARG_S_J: u32 = (1u32 << SIZE_S_J) - 1;
55pub const OFFSET_S_J: i32 = (MAXARG_S_J >> 1) as i32;
56pub const MAXARG_A: u32 = (1u32 << SIZE_A) - 1;
57pub const MAXARG_B: u32 = (1u32 << SIZE_B) - 1;
58pub const MAXARG_C: u32 = (1u32 << SIZE_C) - 1;
59pub const OFFSET_S_C: i32 = (MAXARG_C >> 1) as i32;
60
61pub const NO_REG: u32 = MAXARG_A;
64
65pub const MAXINDEXRK: u32 = MAXARG_B;
68
69pub const LFIELDS_PER_FLUSH: u32 = 50;
72
73pub use lua_vm::vm::OpCode;
83
84pub const NUM_OPCODES: usize = OpCode::GetVArg as usize + 1;
87
88const fn opmode_byte(mm: u8, ot: u8, it: u8, t: u8, a: u8, m: u8) -> u8 {
101 (mm << 7) | (ot << 6) | (it << 5) | (t << 4) | (a << 3) | m
102}
103
104const M_ABC: u8 = OpMode::Abc as u8;
106const M_ABX: u8 = OpMode::ABx as u8;
107const M_ASBX: u8 = OpMode::AsBx as u8;
108const M_AX: u8 = OpMode::Ax as u8;
109const M_SJ: u8 = OpMode::SJ as u8;
110
111pub(crate) const OP_MODES: [u8; NUM_OPCODES] = [
119 opmode_byte(0, 0, 0, 0, 1, M_ABC),
120 opmode_byte(0, 0, 0, 0, 1, M_ASBX),
121 opmode_byte(0, 0, 0, 0, 1, M_ASBX),
122 opmode_byte(0, 0, 0, 0, 1, M_ABX),
123 opmode_byte(0, 0, 0, 0, 1, M_ABX),
124 opmode_byte(0, 0, 0, 0, 1, M_ABC),
125 opmode_byte(0, 0, 0, 0, 1, M_ABC),
126 opmode_byte(0, 0, 0, 0, 1, M_ABC),
127 opmode_byte(0, 0, 0, 0, 1, M_ABC),
128 opmode_byte(0, 0, 0, 0, 1, M_ABC),
129 opmode_byte(0, 0, 0, 0, 0, M_ABC),
130 opmode_byte(0, 0, 0, 0, 1, M_ABC),
131 opmode_byte(0, 0, 0, 0, 1, M_ABC),
132 opmode_byte(0, 0, 0, 0, 1, M_ABC),
133 opmode_byte(0, 0, 0, 0, 1, M_ABC),
134 opmode_byte(0, 0, 0, 0, 0, M_ABC),
135 opmode_byte(0, 0, 0, 0, 0, M_ABC),
136 opmode_byte(0, 0, 0, 0, 0, M_ABC),
137 opmode_byte(0, 0, 0, 0, 0, M_ABC),
138 opmode_byte(0, 0, 0, 0, 1, M_ABC),
139 opmode_byte(0, 0, 0, 0, 1, M_ABC),
140 opmode_byte(0, 0, 0, 0, 1, M_ABC),
141 opmode_byte(0, 0, 0, 0, 1, M_ABC),
142 opmode_byte(0, 0, 0, 0, 1, M_ABC),
143 opmode_byte(0, 0, 0, 0, 1, M_ABC),
144 opmode_byte(0, 0, 0, 0, 1, M_ABC),
145 opmode_byte(0, 0, 0, 0, 1, M_ABC),
146 opmode_byte(0, 0, 0, 0, 1, M_ABC),
147 opmode_byte(0, 0, 0, 0, 1, M_ABC),
148 opmode_byte(0, 0, 0, 0, 1, M_ABC),
149 opmode_byte(0, 0, 0, 0, 1, M_ABC),
150 opmode_byte(0, 0, 0, 0, 1, M_ABC),
151 opmode_byte(0, 0, 0, 0, 1, M_ABC),
152 opmode_byte(0, 0, 0, 0, 1, M_ABC),
153 opmode_byte(0, 0, 0, 0, 1, M_ABC),
154 opmode_byte(0, 0, 0, 0, 1, M_ABC),
155 opmode_byte(0, 0, 0, 0, 1, M_ABC),
156 opmode_byte(0, 0, 0, 0, 1, M_ABC),
157 opmode_byte(0, 0, 0, 0, 1, M_ABC),
158 opmode_byte(0, 0, 0, 0, 1, M_ABC),
159 opmode_byte(0, 0, 0, 0, 1, M_ABC),
160 opmode_byte(0, 0, 0, 0, 1, M_ABC),
161 opmode_byte(0, 0, 0, 0, 1, M_ABC),
162 opmode_byte(0, 0, 0, 0, 1, M_ABC),
163 opmode_byte(0, 0, 0, 0, 1, M_ABC),
164 opmode_byte(0, 0, 0, 0, 1, M_ABC),
165 opmode_byte(1, 0, 0, 0, 0, M_ABC),
166 opmode_byte(1, 0, 0, 0, 0, M_ABC),
167 opmode_byte(1, 0, 0, 0, 0, M_ABC),
168 opmode_byte(0, 0, 0, 0, 1, M_ABC),
169 opmode_byte(0, 0, 0, 0, 1, M_ABC),
170 opmode_byte(0, 0, 0, 0, 1, M_ABC),
171 opmode_byte(0, 0, 0, 0, 1, M_ABC),
172 opmode_byte(0, 0, 0, 0, 1, M_ABC),
173 opmode_byte(0, 0, 0, 0, 0, M_ABC),
174 opmode_byte(0, 0, 0, 0, 0, M_ABC),
175 opmode_byte(0, 0, 0, 0, 0, M_SJ),
176 opmode_byte(0, 0, 0, 1, 0, M_ABC),
177 opmode_byte(0, 0, 0, 1, 0, M_ABC),
178 opmode_byte(0, 0, 0, 1, 0, M_ABC),
179 opmode_byte(0, 0, 0, 1, 0, M_ABC),
180 opmode_byte(0, 0, 0, 1, 0, M_ABC),
181 opmode_byte(0, 0, 0, 1, 0, M_ABC),
182 opmode_byte(0, 0, 0, 1, 0, M_ABC),
183 opmode_byte(0, 0, 0, 1, 0, M_ABC),
184 opmode_byte(0, 0, 0, 1, 0, M_ABC),
185 opmode_byte(0, 0, 0, 1, 0, M_ABC),
186 opmode_byte(0, 0, 0, 1, 1, M_ABC),
187 opmode_byte(0, 1, 1, 0, 1, M_ABC),
188 opmode_byte(0, 1, 1, 0, 1, M_ABC),
189 opmode_byte(0, 0, 1, 0, 0, M_ABC),
190 opmode_byte(0, 0, 0, 0, 0, M_ABC),
191 opmode_byte(0, 0, 0, 0, 0, M_ABC),
192 opmode_byte(0, 0, 0, 0, 1, M_ABX),
193 opmode_byte(0, 0, 0, 0, 1, M_ABX),
194 opmode_byte(0, 0, 0, 0, 0, M_ABX),
195 opmode_byte(0, 0, 0, 0, 0, M_ABC),
196 opmode_byte(0, 0, 0, 0, 1, M_ABX),
197 opmode_byte(0, 0, 1, 0, 0, M_ABC),
198 opmode_byte(0, 0, 0, 0, 1, M_ABX),
199 opmode_byte(0, 1, 0, 0, 1, M_ABC),
200 opmode_byte(0, 0, 1, 0, 1, M_ABC),
201 opmode_byte(0, 0, 0, 0, 0, M_AX),
202 opmode_byte(0, 0, 0, 0, 0, M_ABX),
203 opmode_byte(0, 0, 0, 0, 1, M_ABC),
204 opmode_byte(0, 0, 0, 0, 1, M_ABC),
205];
206
207pub fn get_op_mode(op: OpCode) -> OpMode {
213 match OP_MODES[op as usize] & 7 {
214 0 => OpMode::Abc,
215 1 => OpMode::ABx,
216 2 => OpMode::AsBx,
217 3 => OpMode::Ax,
218 4 => OpMode::SJ,
219 _ => OpMode::Abc,
221 }
222}
223
224#[inline]
227pub fn test_a_mode(op: OpCode) -> bool {
228 (OP_MODES[op as usize] & (1 << 3)) != 0
229}
230
231#[inline]
234pub fn test_t_mode(op: OpCode) -> bool {
235 (OP_MODES[op as usize] & (1 << 4)) != 0
236}
237
238#[inline]
241pub fn test_it_mode(op: OpCode) -> bool {
242 (OP_MODES[op as usize] & (1 << 5)) != 0
243}
244
245#[inline]
248pub fn test_ot_mode(op: OpCode) -> bool {
249 (OP_MODES[op as usize] & (1 << 6)) != 0
250}
251
252#[inline]
255pub fn test_mm_mode(op: OpCode) -> bool {
256 (OP_MODES[op as usize] & (1 << 7)) != 0
257}
258
259#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
267#[repr(transparent)]
268pub struct Instruction(pub u32);
269
270impl Instruction {
271 #[inline]
276 pub const fn get_arg(self, pos: u32, size: u32) -> u32 {
277 (self.0 >> pos) & ((1u32 << size) - 1)
278 }
279
280 #[inline]
283 pub fn set_arg(&mut self, v: u32, pos: u32, size: u32) {
284 let mask = ((1u32 << size) - 1) << pos;
285 self.0 = (self.0 & !mask) | ((v << pos) & mask);
286 }
287
288 #[inline]
293 pub fn opcode(self) -> Option<OpCode> {
294 OpCode::from_u32(self.get_arg(POS_OP, SIZE_OP))
295 }
296
297 #[inline]
300 pub fn set_opcode(&mut self, op: OpCode) {
301 self.set_arg(op as u32, POS_OP, SIZE_OP);
302 }
303
304 #[inline]
307 pub const fn arg_a(self) -> u32 {
308 self.get_arg(POS_A, SIZE_A)
309 }
310
311 #[inline]
312 pub fn set_arg_a(&mut self, v: u32) {
313 self.set_arg(v, POS_A, SIZE_A);
314 }
315
316 #[inline]
319 pub const fn arg_k(self) -> u32 {
320 self.get_arg(POS_K, 1)
321 }
322
323 #[inline]
324 pub const fn test_k(self) -> bool {
325 self.arg_k() != 0
326 }
327
328 #[inline]
329 pub fn set_arg_k(&mut self, v: u32) {
330 self.set_arg(v, POS_K, 1);
331 }
332
333 #[inline]
336 pub const fn arg_b(self) -> u32 {
337 self.get_arg(POS_B, SIZE_B)
338 }
339
340 #[inline]
341 pub const fn arg_s_b(self) -> i32 {
342 self.arg_b() as i32 - OFFSET_S_C
343 }
344
345 #[inline]
346 pub fn set_arg_b(&mut self, v: u32) {
347 self.set_arg(v, POS_B, SIZE_B);
348 }
349
350 #[inline]
353 pub const fn arg_c(self) -> u32 {
354 self.get_arg(POS_C, SIZE_C)
355 }
356
357 #[inline]
358 pub const fn arg_s_c(self) -> i32 {
359 self.arg_c() as i32 - OFFSET_S_C
360 }
361
362 #[inline]
363 pub fn set_arg_c(&mut self, v: u32) {
364 self.set_arg(v, POS_C, SIZE_C);
365 }
366
367 #[inline]
370 pub const fn arg_bx(self) -> u32 {
371 self.get_arg(POS_BX, SIZE_BX)
372 }
373
374 #[inline]
375 pub fn set_arg_bx(&mut self, v: u32) {
376 self.set_arg(v, POS_BX, SIZE_BX);
377 }
378
379 #[inline]
380 pub const fn arg_s_bx(self) -> i32 {
381 self.arg_bx() as i32 - OFFSET_S_BX
382 }
383
384 #[inline]
385 pub fn set_arg_s_bx(&mut self, b: i32) {
386 self.set_arg_bx((b + OFFSET_S_BX) as u32);
387 }
388
389 #[inline]
392 pub const fn arg_ax(self) -> u32 {
393 self.get_arg(POS_AX, SIZE_AX)
394 }
395
396 #[inline]
397 pub fn set_arg_ax(&mut self, v: u32) {
398 self.set_arg(v, POS_AX, SIZE_AX);
399 }
400
401 #[inline]
404 pub const fn arg_s_j(self) -> i32 {
405 self.get_arg(POS_S_J, SIZE_S_J) as i32 - OFFSET_S_J
406 }
407
408 #[inline]
409 pub fn set_arg_s_j(&mut self, j: i32) {
410 self.set_arg((j + OFFSET_S_J) as u32, POS_S_J, SIZE_S_J);
411 }
412
413 #[inline]
418 pub fn abck(op: OpCode, a: u32, b: u32, c: u32, k: u32) -> Self {
419 Self(((op as u32) << POS_OP) | (a << POS_A) | (b << POS_B) | (c << POS_C) | (k << POS_K))
420 }
421
422 #[inline]
425 pub fn abx(op: OpCode, a: u32, bc: u32) -> Self {
426 Self(((op as u32) << POS_OP) | (a << POS_A) | (bc << POS_BX))
427 }
428
429 #[inline]
432 pub fn ax(op: OpCode, a: u32) -> Self {
433 Self(((op as u32) << POS_OP) | (a << POS_AX))
434 }
435
436 #[inline]
439 pub fn sj(op: OpCode, j: u32, k: u32) -> Self {
440 Self(((op as u32) << POS_OP) | (j << POS_S_J) | (k << POS_K))
441 }
442
443 pub fn is_out_top(self) -> bool {
449 match self.opcode() {
450 Some(op) => (test_ot_mode(op) && self.arg_c() == 0) || op == OpCode::TailCall,
451 None => false,
452 }
453 }
454
455 pub fn is_in_top(self) -> bool {
458 match self.opcode() {
459 Some(op) => test_it_mode(op) && self.arg_b() == 0,
460 None => false,
461 }
462 }
463
464 pub fn op_mode(self) -> Option<OpMode> {
467 self.opcode().map(get_op_mode)
468 }
469}
470
471#[inline]
480pub const fn int_to_s_c(i: i32) -> u32 {
481 (i + OFFSET_S_C) as u32
482}
483
484#[inline]
487pub const fn s_c_to_int(i: u32) -> i32 {
488 i as i32 - OFFSET_S_C
489}
490
491#[cfg(test)]
494mod tests {
495 use super::*;
496
497 #[test]
498 fn num_opcodes_matches_enum() {
499 assert_eq!(NUM_OPCODES, 86);
500 assert_eq!(OpCode::ExtraArg as usize, 82);
501 assert_eq!(OpCode::ErrNNil as usize, 83);
502 assert_eq!(OpCode::VarArgPack as usize, 84);
503 assert_eq!(OpCode::GetVArg as usize, 85);
504 }
505
506 #[test]
507 fn op_modes_table_length() {
508 assert_eq!(OP_MODES.len(), NUM_OPCODES);
509 }
510
511 #[test]
512 fn opmode_byte_values() {
513 assert_eq!(OP_MODES[OpCode::Move as usize], 0b00001000); assert_eq!(OP_MODES[OpCode::LoadI as usize], 0b00001010); assert_eq!(OP_MODES[OpCode::Jmp as usize], 0b00000100); assert_eq!(OP_MODES[OpCode::MmBin as usize], 0b10000000); assert_eq!(OP_MODES[OpCode::Call as usize], 0b01101000); assert_eq!(OP_MODES[OpCode::ExtraArg as usize], 0b00000011); }
520
521 #[test]
522 fn from_u32_round_trip() {
523 for i in 0..NUM_OPCODES {
524 let op = OpCode::from_u32(i as u32).expect("valid opcode");
525 assert_eq!(op as usize, i);
526 }
527 assert!(OpCode::from_u32(86).is_none());
528 }
529
530 #[test]
531 fn instruction_arg_a() {
532 let i = Instruction::abck(OpCode::Move, 5, 3, 0, 0);
533 assert_eq!(i.arg_a(), 5);
534 assert_eq!(i.arg_b(), 3);
535 }
536
537 #[test]
538 fn instruction_s_bx_round_trip() {
539 let mut i = Instruction::abx(OpCode::LoadK, 0, 0);
540 i.set_arg_s_bx(-10);
541 assert_eq!(i.arg_s_bx(), -10);
542 i.set_arg_s_bx(0);
543 assert_eq!(i.arg_s_bx(), 0);
544 i.set_arg_s_bx(100);
545 assert_eq!(i.arg_s_bx(), 100);
546 }
547
548 #[test]
549 fn instruction_s_j_round_trip() {
550 let mut i = Instruction::sj(OpCode::Jmp, (OFFSET_S_J) as u32, 0);
551 assert_eq!(i.arg_s_j(), 0);
552 i.set_arg_s_j(42);
553 assert_eq!(i.arg_s_j(), 42);
554 i.set_arg_s_j(-1);
555 assert_eq!(i.arg_s_j(), -1);
556 }
557
558 #[test]
559 fn get_op_mode_smoke() {
560 assert_eq!(get_op_mode(OpCode::Move), OpMode::Abc);
561 assert_eq!(get_op_mode(OpCode::LoadI), OpMode::AsBx);
562 assert_eq!(get_op_mode(OpCode::LoadK), OpMode::ABx);
563 assert_eq!(get_op_mode(OpCode::Jmp), OpMode::SJ);
564 assert_eq!(get_op_mode(OpCode::ExtraArg), OpMode::Ax);
565 }
566
567 #[test]
568 fn int_to_s_c_round_trip() {
569 assert_eq!(s_c_to_int(int_to_s_c(0)), 0);
570 assert_eq!(s_c_to_int(int_to_s_c(100)), 100);
571 assert_eq!(s_c_to_int(int_to_s_c(-50)), -50);
572 }
573}