1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19#[repr(u8)]
20pub enum OpMode {
21 Abc = 0,
22 ABx = 1,
23 AsBx = 2,
24 Ax = 3,
25 SJ = 4,
26}
27
28pub const SIZE_C: u32 = 8;
32pub const SIZE_B: u32 = 8;
33pub const SIZE_BX: u32 = SIZE_C + SIZE_B + 1;
34pub const SIZE_A: u32 = 8;
35pub const SIZE_AX: u32 = SIZE_BX + SIZE_A;
36pub const SIZE_S_J: u32 = SIZE_BX + SIZE_A;
37pub const SIZE_OP: u32 = 7;
38
39pub const POS_OP: u32 = 0;
43pub const POS_A: u32 = POS_OP + SIZE_OP;
44pub const POS_K: u32 = POS_A + SIZE_A;
45pub const POS_B: u32 = POS_K + 1;
46pub const POS_C: u32 = POS_B + SIZE_B;
47pub const POS_BX: u32 = POS_K;
48pub const POS_AX: u32 = POS_A;
49pub const POS_S_J: u32 = POS_A;
50
51pub const MAXARG_BX: u32 = (1u32 << SIZE_BX) - 1;
55pub const OFFSET_S_BX: i32 = (MAXARG_BX >> 1) as i32;
56pub const MAXARG_AX: u32 = (1u32 << SIZE_AX) - 1;
57pub const MAXARG_S_J: u32 = (1u32 << SIZE_S_J) - 1;
58pub const OFFSET_S_J: i32 = (MAXARG_S_J >> 1) as i32;
59pub const MAXARG_A: u32 = (1u32 << SIZE_A) - 1;
60pub const MAXARG_B: u32 = (1u32 << SIZE_B) - 1;
61pub const MAXARG_C: u32 = (1u32 << SIZE_C) - 1;
62pub const OFFSET_S_C: i32 = (MAXARG_C >> 1) as i32;
63
64pub const NO_REG: u32 = MAXARG_A;
67
68pub const MAXINDEXRK: u32 = MAXARG_B;
71
72pub const LFIELDS_PER_FLUSH: u32 = 50;
75
76pub use lua_vm::vm::OpCode;
86
87pub const NUM_OPCODES: usize = OpCode::VarArgPack as usize + 1;
90
91
92const fn opmode_byte(mm: u8, ot: u8, it: u8, t: u8, a: u8, m: u8) -> u8 {
105 (mm << 7) | (ot << 6) | (it << 5) | (t << 4) | (a << 3) | m
106}
107
108const M_ABC: u8 = OpMode::Abc as u8;
110const M_ABX: u8 = OpMode::ABx as u8;
111const M_ASBX: u8 = OpMode::AsBx as u8;
112const M_AX: u8 = OpMode::Ax as u8;
113const M_SJ: u8 = OpMode::SJ as u8;
114
115pub(crate) const OP_MODES: [u8; NUM_OPCODES] = [
127 opmode_byte(0, 0, 0, 0, 1, M_ABC),
128 opmode_byte(0, 0, 0, 0, 1, M_ASBX),
129 opmode_byte(0, 0, 0, 0, 1, M_ASBX),
130 opmode_byte(0, 0, 0, 0, 1, M_ABX),
131 opmode_byte(0, 0, 0, 0, 1, M_ABX),
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, 1, M_ABC),
135 opmode_byte(0, 0, 0, 0, 1, M_ABC),
136 opmode_byte(0, 0, 0, 0, 1, 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, 0, M_ABC),
143 opmode_byte(0, 0, 0, 0, 0, M_ABC),
144 opmode_byte(0, 0, 0, 0, 0, M_ABC),
145 opmode_byte(0, 0, 0, 0, 0, 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(0, 0, 0, 0, 1, M_ABC),
166 opmode_byte(0, 0, 0, 0, 1, M_ABC),
167 opmode_byte(0, 0, 0, 0, 1, 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(1, 0, 0, 0, 0, M_ABC),
174 opmode_byte(1, 0, 0, 0, 0, M_ABC),
175 opmode_byte(1, 0, 0, 0, 0, M_ABC),
176 opmode_byte(0, 0, 0, 0, 1, M_ABC),
177 opmode_byte(0, 0, 0, 0, 1, M_ABC),
178 opmode_byte(0, 0, 0, 0, 1, M_ABC),
179 opmode_byte(0, 0, 0, 0, 1, M_ABC),
180 opmode_byte(0, 0, 0, 0, 1, M_ABC),
181 opmode_byte(0, 0, 0, 0, 0, M_ABC),
182 opmode_byte(0, 0, 0, 0, 0, M_ABC),
183 opmode_byte(0, 0, 0, 0, 0, M_SJ),
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, 0, M_ABC),
187 opmode_byte(0, 0, 0, 1, 0, M_ABC),
188 opmode_byte(0, 0, 0, 1, 0, M_ABC),
189 opmode_byte(0, 0, 0, 1, 0, M_ABC),
190 opmode_byte(0, 0, 0, 1, 0, M_ABC),
191 opmode_byte(0, 0, 0, 1, 0, M_ABC),
192 opmode_byte(0, 0, 0, 1, 0, M_ABC),
193 opmode_byte(0, 0, 0, 1, 0, M_ABC),
194 opmode_byte(0, 0, 0, 1, 1, M_ABC),
195 opmode_byte(0, 1, 1, 0, 1, M_ABC),
196 opmode_byte(0, 1, 1, 0, 1, M_ABC),
197 opmode_byte(0, 0, 1, 0, 0, M_ABC),
198 opmode_byte(0, 0, 0, 0, 0, M_ABC),
199 opmode_byte(0, 0, 0, 0, 0, M_ABC),
200 opmode_byte(0, 0, 0, 0, 1, M_ABX),
201 opmode_byte(0, 0, 0, 0, 1, M_ABX),
202 opmode_byte(0, 0, 0, 0, 0, M_ABX),
203 opmode_byte(0, 0, 0, 0, 0, M_ABC),
204 opmode_byte(0, 0, 0, 0, 1, M_ABX),
205 opmode_byte(0, 0, 1, 0, 0, M_ABC),
206 opmode_byte(0, 0, 0, 0, 1, M_ABX),
207 opmode_byte(0, 1, 0, 0, 1, M_ABC),
208 opmode_byte(0, 0, 1, 0, 1, M_ABC),
209 opmode_byte(0, 0, 0, 0, 0, M_AX),
210 opmode_byte(0, 0, 0, 0, 0, M_ABX),
211 opmode_byte(0, 0, 0, 0, 1, M_ABC),
212];
213
214pub fn get_op_mode(op: OpCode) -> OpMode {
220 match OP_MODES[op as usize] & 7 {
221 0 => OpMode::Abc,
222 1 => OpMode::ABx,
223 2 => OpMode::AsBx,
224 3 => OpMode::Ax,
225 4 => OpMode::SJ,
226 _ => OpMode::Abc,
228 }
229}
230
231#[inline]
234pub fn test_a_mode(op: OpCode) -> bool {
235 (OP_MODES[op as usize] & (1 << 3)) != 0
236}
237
238#[inline]
241pub fn test_t_mode(op: OpCode) -> bool {
242 (OP_MODES[op as usize] & (1 << 4)) != 0
243}
244
245#[inline]
248pub fn test_it_mode(op: OpCode) -> bool {
249 (OP_MODES[op as usize] & (1 << 5)) != 0
250}
251
252#[inline]
255pub fn test_ot_mode(op: OpCode) -> bool {
256 (OP_MODES[op as usize] & (1 << 6)) != 0
257}
258
259#[inline]
262pub fn test_mm_mode(op: OpCode) -> bool {
263 (OP_MODES[op as usize] & (1 << 7)) != 0
264}
265
266#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
274#[repr(transparent)]
275pub struct Instruction(pub u32);
276
277impl Instruction {
278 #[inline]
283 pub const fn get_arg(self, pos: u32, size: u32) -> u32 {
284 (self.0 >> pos) & ((1u32 << size) - 1)
285 }
286
287 #[inline]
290 pub fn set_arg(&mut self, v: u32, pos: u32, size: u32) {
291 let mask = ((1u32 << size) - 1) << pos;
292 self.0 = (self.0 & !mask) | ((v << pos) & mask);
293 }
294
295 #[inline]
300 pub fn opcode(self) -> Option<OpCode> {
301 OpCode::from_u32(self.get_arg(POS_OP, SIZE_OP))
302 }
303
304 #[inline]
307 pub fn set_opcode(&mut self, op: OpCode) {
308 self.set_arg(op as u32, POS_OP, SIZE_OP);
309 }
310
311 #[inline]
314 pub const fn arg_a(self) -> u32 {
315 self.get_arg(POS_A, SIZE_A)
316 }
317
318 #[inline]
319 pub fn set_arg_a(&mut self, v: u32) {
320 self.set_arg(v, POS_A, SIZE_A);
321 }
322
323 #[inline]
326 pub const fn arg_k(self) -> u32 {
327 self.get_arg(POS_K, 1)
328 }
329
330 #[inline]
331 pub const fn test_k(self) -> bool {
332 self.arg_k() != 0
333 }
334
335 #[inline]
336 pub fn set_arg_k(&mut self, v: u32) {
337 self.set_arg(v, POS_K, 1);
338 }
339
340 #[inline]
343 pub const fn arg_b(self) -> u32 {
344 self.get_arg(POS_B, SIZE_B)
345 }
346
347 #[inline]
348 pub const fn arg_s_b(self) -> i32 {
349 self.arg_b() as i32 - OFFSET_S_C
350 }
351
352 #[inline]
353 pub fn set_arg_b(&mut self, v: u32) {
354 self.set_arg(v, POS_B, SIZE_B);
355 }
356
357 #[inline]
360 pub const fn arg_c(self) -> u32 {
361 self.get_arg(POS_C, SIZE_C)
362 }
363
364 #[inline]
365 pub const fn arg_s_c(self) -> i32 {
366 self.arg_c() as i32 - OFFSET_S_C
367 }
368
369 #[inline]
370 pub fn set_arg_c(&mut self, v: u32) {
371 self.set_arg(v, POS_C, SIZE_C);
372 }
373
374 #[inline]
377 pub const fn arg_bx(self) -> u32 {
378 self.get_arg(POS_BX, SIZE_BX)
379 }
380
381 #[inline]
382 pub fn set_arg_bx(&mut self, v: u32) {
383 self.set_arg(v, POS_BX, SIZE_BX);
384 }
385
386 #[inline]
387 pub const fn arg_s_bx(self) -> i32 {
388 self.arg_bx() as i32 - OFFSET_S_BX
389 }
390
391 #[inline]
392 pub fn set_arg_s_bx(&mut self, b: i32) {
393 self.set_arg_bx((b + OFFSET_S_BX) as u32);
394 }
395
396 #[inline]
399 pub const fn arg_ax(self) -> u32 {
400 self.get_arg(POS_AX, SIZE_AX)
401 }
402
403 #[inline]
404 pub fn set_arg_ax(&mut self, v: u32) {
405 self.set_arg(v, POS_AX, SIZE_AX);
406 }
407
408 #[inline]
411 pub const fn arg_s_j(self) -> i32 {
412 self.get_arg(POS_S_J, SIZE_S_J) as i32 - OFFSET_S_J
413 }
414
415 #[inline]
416 pub fn set_arg_s_j(&mut self, j: i32) {
417 self.set_arg((j + OFFSET_S_J) as u32, POS_S_J, SIZE_S_J);
418 }
419
420 #[inline]
425 pub fn abck(op: OpCode, a: u32, b: u32, c: u32, k: u32) -> Self {
426 Self(
427 ((op as u32) << POS_OP)
428 | (a << POS_A)
429 | (b << POS_B)
430 | (c << POS_C)
431 | (k << POS_K),
432 )
433 }
434
435 #[inline]
438 pub fn abx(op: OpCode, a: u32, bc: u32) -> Self {
439 Self(((op as u32) << POS_OP) | (a << POS_A) | (bc << POS_BX))
440 }
441
442 #[inline]
445 pub fn ax(op: OpCode, a: u32) -> Self {
446 Self(((op as u32) << POS_OP) | (a << POS_AX))
447 }
448
449 #[inline]
452 pub fn sj(op: OpCode, j: u32, k: u32) -> Self {
453 Self(((op as u32) << POS_OP) | (j << POS_S_J) | (k << POS_K))
454 }
455
456 pub fn is_out_top(self) -> bool {
462 match self.opcode() {
463 Some(op) => (test_ot_mode(op) && self.arg_c() == 0) || op == OpCode::TailCall,
464 None => false,
465 }
466 }
467
468 pub fn is_in_top(self) -> bool {
471 match self.opcode() {
472 Some(op) => test_it_mode(op) && self.arg_b() == 0,
473 None => false,
474 }
475 }
476
477 pub fn op_mode(self) -> Option<OpMode> {
480 self.opcode().map(get_op_mode)
481 }
482}
483
484#[inline]
493pub const fn int_to_s_c(i: i32) -> u32 {
494 (i + OFFSET_S_C) as u32
495}
496
497#[inline]
500pub const fn s_c_to_int(i: u32) -> i32 {
501 i as i32 - OFFSET_S_C
502}
503
504#[cfg(test)]
507mod tests {
508 use super::*;
509
510 #[test]
511 fn num_opcodes_matches_enum() {
512 assert_eq!(NUM_OPCODES, 85);
513 assert_eq!(OpCode::ExtraArg as usize, 82);
514 assert_eq!(OpCode::ErrNNil as usize, 83);
515 assert_eq!(OpCode::VarArgPack as usize, 84);
516 }
517
518 #[test]
519 fn op_modes_table_length() {
520 assert_eq!(OP_MODES.len(), NUM_OPCODES);
521 }
522
523 #[test]
524 fn opmode_byte_values() {
525 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); }
532
533 #[test]
534 fn from_u32_round_trip() {
535 for i in 0..NUM_OPCODES {
536 let op = OpCode::from_u32(i as u32).expect("valid opcode");
537 assert_eq!(op as usize, i);
538 }
539 assert!(OpCode::from_u32(85).is_none());
540 }
541
542 #[test]
543 fn instruction_arg_a() {
544 let i = Instruction::abck(OpCode::Move, 5, 3, 0, 0);
545 assert_eq!(i.arg_a(), 5);
546 assert_eq!(i.arg_b(), 3);
547 }
548
549 #[test]
550 fn instruction_s_bx_round_trip() {
551 let mut i = Instruction::abx(OpCode::LoadK, 0, 0);
552 i.set_arg_s_bx(-10);
553 assert_eq!(i.arg_s_bx(), -10);
554 i.set_arg_s_bx(0);
555 assert_eq!(i.arg_s_bx(), 0);
556 i.set_arg_s_bx(100);
557 assert_eq!(i.arg_s_bx(), 100);
558 }
559
560 #[test]
561 fn instruction_s_j_round_trip() {
562 let mut i = Instruction::sj(OpCode::Jmp, (OFFSET_S_J) as u32, 0);
563 assert_eq!(i.arg_s_j(), 0);
564 i.set_arg_s_j(42);
565 assert_eq!(i.arg_s_j(), 42);
566 i.set_arg_s_j(-1);
567 assert_eq!(i.arg_s_j(), -1);
568 }
569
570 #[test]
571 fn get_op_mode_smoke() {
572 assert_eq!(get_op_mode(OpCode::Move), OpMode::Abc);
573 assert_eq!(get_op_mode(OpCode::LoadI), OpMode::AsBx);
574 assert_eq!(get_op_mode(OpCode::LoadK), OpMode::ABx);
575 assert_eq!(get_op_mode(OpCode::Jmp), OpMode::SJ);
576 assert_eq!(get_op_mode(OpCode::ExtraArg), OpMode::Ax);
577 }
578
579 #[test]
580 fn int_to_s_c_round_trip() {
581 assert_eq!(s_c_to_int(int_to_s_c(0)), 0);
582 assert_eq!(s_c_to_int(int_to_s_c(100)), 100);
583 assert_eq!(s_c_to_int(int_to_s_c(-50)), -50);
584 }
585}
586
587