1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18#[repr(u8)]
19pub enum OpMode {
20 Abc = 0,
21 ABx = 1,
22 AsBx = 2,
23 Ax = 3,
24 SJ = 4,
25}
26
27pub const SIZE_C: u32 = 8;
31pub const SIZE_B: u32 = 8;
32pub const SIZE_BX: u32 = SIZE_C + SIZE_B + 1;
33pub const SIZE_A: u32 = 8;
34pub const SIZE_AX: u32 = SIZE_BX + SIZE_A;
35pub const SIZE_S_J: u32 = SIZE_BX + SIZE_A;
36pub const SIZE_OP: u32 = 7;
37
38pub const POS_OP: u32 = 0;
42pub const POS_A: u32 = POS_OP + SIZE_OP;
43pub const POS_K: u32 = POS_A + SIZE_A;
44pub const POS_B: u32 = POS_K + 1;
45pub const POS_C: u32 = POS_B + SIZE_B;
46pub const POS_BX: u32 = POS_K;
47pub const POS_AX: u32 = POS_A;
48pub const POS_S_J: u32 = POS_A;
49
50pub const MAXARG_BX: u32 = (1u32 << SIZE_BX) - 1;
54pub const OFFSET_S_BX: i32 = (MAXARG_BX >> 1) as i32;
55pub const MAXARG_AX: u32 = (1u32 << SIZE_AX) - 1;
56pub const MAXARG_S_J: u32 = (1u32 << SIZE_S_J) - 1;
57pub const OFFSET_S_J: i32 = (MAXARG_S_J >> 1) as i32;
58pub const MAXARG_A: u32 = (1u32 << SIZE_A) - 1;
59pub const MAXARG_B: u32 = (1u32 << SIZE_B) - 1;
60pub const MAXARG_C: u32 = (1u32 << SIZE_C) - 1;
61pub const OFFSET_S_C: i32 = (MAXARG_C >> 1) as i32;
62
63pub const NO_REG: u32 = MAXARG_A;
66
67pub const MAXINDEXRK: u32 = MAXARG_B;
70
71pub const LFIELDS_PER_FLUSH: u32 = 50;
74
75pub use lua_vm::vm::OpCode;
85
86pub const NUM_OPCODES: usize = OpCode::GetVArg as usize + 1;
89
90const fn opmode_byte(mm: u8, ot: u8, it: u8, t: u8, a: u8, m: u8) -> u8 {
103 (mm << 7) | (ot << 6) | (it << 5) | (t << 4) | (a << 3) | m
104}
105
106const M_ABC: u8 = OpMode::Abc as u8;
108const M_ABX: u8 = OpMode::ABx as u8;
109const M_ASBX: u8 = OpMode::AsBx as u8;
110const M_AX: u8 = OpMode::Ax as u8;
111const M_SJ: u8 = OpMode::SJ as u8;
112
113pub(crate) const OP_MODES: [u8; NUM_OPCODES] = [
125 opmode_byte(0, 0, 0, 0, 1, M_ABC),
126 opmode_byte(0, 0, 0, 0, 1, M_ASBX),
127 opmode_byte(0, 0, 0, 0, 1, M_ASBX),
128 opmode_byte(0, 0, 0, 0, 1, M_ABX),
129 opmode_byte(0, 0, 0, 0, 1, M_ABX),
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, 1, M_ABC),
135 opmode_byte(0, 0, 0, 0, 0, M_ABC),
136 opmode_byte(0, 0, 0, 0, 1, M_ABC),
137 opmode_byte(0, 0, 0, 0, 1, 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, 0, M_ABC),
141 opmode_byte(0, 0, 0, 0, 0, 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, 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(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(1, 0, 0, 0, 0, M_ABC),
172 opmode_byte(1, 0, 0, 0, 0, M_ABC),
173 opmode_byte(1, 0, 0, 0, 0, M_ABC),
174 opmode_byte(0, 0, 0, 0, 1, M_ABC),
175 opmode_byte(0, 0, 0, 0, 1, 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, 0, M_ABC),
180 opmode_byte(0, 0, 0, 0, 0, M_ABC),
181 opmode_byte(0, 0, 0, 0, 0, M_SJ),
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, 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, 1, M_ABC),
193 opmode_byte(0, 1, 1, 0, 1, M_ABC),
194 opmode_byte(0, 1, 1, 0, 1, M_ABC),
195 opmode_byte(0, 0, 1, 0, 0, M_ABC),
196 opmode_byte(0, 0, 0, 0, 0, M_ABC),
197 opmode_byte(0, 0, 0, 0, 0, M_ABC),
198 opmode_byte(0, 0, 0, 0, 1, M_ABX),
199 opmode_byte(0, 0, 0, 0, 1, M_ABX),
200 opmode_byte(0, 0, 0, 0, 0, M_ABX),
201 opmode_byte(0, 0, 0, 0, 0, M_ABC),
202 opmode_byte(0, 0, 0, 0, 1, M_ABX),
203 opmode_byte(0, 0, 1, 0, 0, M_ABC),
204 opmode_byte(0, 0, 0, 0, 1, M_ABX),
205 opmode_byte(0, 1, 0, 0, 1, M_ABC),
206 opmode_byte(0, 0, 1, 0, 1, M_ABC),
207 opmode_byte(0, 0, 0, 0, 0, M_AX),
208 opmode_byte(0, 0, 0, 0, 0, M_ABX),
209 opmode_byte(0, 0, 0, 0, 1, M_ABC),
210 opmode_byte(0, 0, 0, 0, 1, M_ABC),
211];
212
213pub fn get_op_mode(op: OpCode) -> OpMode {
219 match OP_MODES[op as usize] & 7 {
220 0 => OpMode::Abc,
221 1 => OpMode::ABx,
222 2 => OpMode::AsBx,
223 3 => OpMode::Ax,
224 4 => OpMode::SJ,
225 _ => OpMode::Abc,
227 }
228}
229
230#[inline]
233pub fn test_a_mode(op: OpCode) -> bool {
234 (OP_MODES[op as usize] & (1 << 3)) != 0
235}
236
237#[inline]
240pub fn test_t_mode(op: OpCode) -> bool {
241 (OP_MODES[op as usize] & (1 << 4)) != 0
242}
243
244#[inline]
247pub fn test_it_mode(op: OpCode) -> bool {
248 (OP_MODES[op as usize] & (1 << 5)) != 0
249}
250
251#[inline]
254pub fn test_ot_mode(op: OpCode) -> bool {
255 (OP_MODES[op as usize] & (1 << 6)) != 0
256}
257
258#[inline]
261pub fn test_mm_mode(op: OpCode) -> bool {
262 (OP_MODES[op as usize] & (1 << 7)) != 0
263}
264
265#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
273#[repr(transparent)]
274pub struct Instruction(pub u32);
275
276impl Instruction {
277 #[inline]
282 pub const fn get_arg(self, pos: u32, size: u32) -> u32 {
283 (self.0 >> pos) & ((1u32 << size) - 1)
284 }
285
286 #[inline]
289 pub fn set_arg(&mut self, v: u32, pos: u32, size: u32) {
290 let mask = ((1u32 << size) - 1) << pos;
291 self.0 = (self.0 & !mask) | ((v << pos) & mask);
292 }
293
294 #[inline]
299 pub fn opcode(self) -> Option<OpCode> {
300 OpCode::from_u32(self.get_arg(POS_OP, SIZE_OP))
301 }
302
303 #[inline]
306 pub fn set_opcode(&mut self, op: OpCode) {
307 self.set_arg(op as u32, POS_OP, SIZE_OP);
308 }
309
310 #[inline]
313 pub const fn arg_a(self) -> u32 {
314 self.get_arg(POS_A, SIZE_A)
315 }
316
317 #[inline]
318 pub fn set_arg_a(&mut self, v: u32) {
319 self.set_arg(v, POS_A, SIZE_A);
320 }
321
322 #[inline]
325 pub const fn arg_k(self) -> u32 {
326 self.get_arg(POS_K, 1)
327 }
328
329 #[inline]
330 pub const fn test_k(self) -> bool {
331 self.arg_k() != 0
332 }
333
334 #[inline]
335 pub fn set_arg_k(&mut self, v: u32) {
336 self.set_arg(v, POS_K, 1);
337 }
338
339 #[inline]
342 pub const fn arg_b(self) -> u32 {
343 self.get_arg(POS_B, SIZE_B)
344 }
345
346 #[inline]
347 pub const fn arg_s_b(self) -> i32 {
348 self.arg_b() as i32 - OFFSET_S_C
349 }
350
351 #[inline]
352 pub fn set_arg_b(&mut self, v: u32) {
353 self.set_arg(v, POS_B, SIZE_B);
354 }
355
356 #[inline]
359 pub const fn arg_c(self) -> u32 {
360 self.get_arg(POS_C, SIZE_C)
361 }
362
363 #[inline]
364 pub const fn arg_s_c(self) -> i32 {
365 self.arg_c() as i32 - OFFSET_S_C
366 }
367
368 #[inline]
369 pub fn set_arg_c(&mut self, v: u32) {
370 self.set_arg(v, POS_C, SIZE_C);
371 }
372
373 #[inline]
376 pub const fn arg_bx(self) -> u32 {
377 self.get_arg(POS_BX, SIZE_BX)
378 }
379
380 #[inline]
381 pub fn set_arg_bx(&mut self, v: u32) {
382 self.set_arg(v, POS_BX, SIZE_BX);
383 }
384
385 #[inline]
386 pub const fn arg_s_bx(self) -> i32 {
387 self.arg_bx() as i32 - OFFSET_S_BX
388 }
389
390 #[inline]
391 pub fn set_arg_s_bx(&mut self, b: i32) {
392 self.set_arg_bx((b + OFFSET_S_BX) as u32);
393 }
394
395 #[inline]
398 pub const fn arg_ax(self) -> u32 {
399 self.get_arg(POS_AX, SIZE_AX)
400 }
401
402 #[inline]
403 pub fn set_arg_ax(&mut self, v: u32) {
404 self.set_arg(v, POS_AX, SIZE_AX);
405 }
406
407 #[inline]
410 pub const fn arg_s_j(self) -> i32 {
411 self.get_arg(POS_S_J, SIZE_S_J) as i32 - OFFSET_S_J
412 }
413
414 #[inline]
415 pub fn set_arg_s_j(&mut self, j: i32) {
416 self.set_arg((j + OFFSET_S_J) as u32, POS_S_J, SIZE_S_J);
417 }
418
419 #[inline]
424 pub fn abck(op: OpCode, a: u32, b: u32, c: u32, k: u32) -> Self {
425 Self(((op as u32) << POS_OP) | (a << POS_A) | (b << POS_B) | (c << POS_C) | (k << POS_K))
426 }
427
428 #[inline]
431 pub fn abx(op: OpCode, a: u32, bc: u32) -> Self {
432 Self(((op as u32) << POS_OP) | (a << POS_A) | (bc << POS_BX))
433 }
434
435 #[inline]
438 pub fn ax(op: OpCode, a: u32) -> Self {
439 Self(((op as u32) << POS_OP) | (a << POS_AX))
440 }
441
442 #[inline]
445 pub fn sj(op: OpCode, j: u32, k: u32) -> Self {
446 Self(((op as u32) << POS_OP) | (j << POS_S_J) | (k << POS_K))
447 }
448
449 pub fn is_out_top(self) -> bool {
455 match self.opcode() {
456 Some(op) => (test_ot_mode(op) && self.arg_c() == 0) || op == OpCode::TailCall,
457 None => false,
458 }
459 }
460
461 pub fn is_in_top(self) -> bool {
464 match self.opcode() {
465 Some(op) => test_it_mode(op) && self.arg_b() == 0,
466 None => false,
467 }
468 }
469
470 pub fn op_mode(self) -> Option<OpMode> {
473 self.opcode().map(get_op_mode)
474 }
475}
476
477#[inline]
486pub const fn int_to_s_c(i: i32) -> u32 {
487 (i + OFFSET_S_C) as u32
488}
489
490#[inline]
493pub const fn s_c_to_int(i: u32) -> i32 {
494 i as i32 - OFFSET_S_C
495}
496
497#[cfg(test)]
500mod tests {
501 use super::*;
502
503 #[test]
504 fn num_opcodes_matches_enum() {
505 assert_eq!(NUM_OPCODES, 86);
506 assert_eq!(OpCode::ExtraArg as usize, 82);
507 assert_eq!(OpCode::ErrNNil as usize, 83);
508 assert_eq!(OpCode::VarArgPack as usize, 84);
509 assert_eq!(OpCode::GetVArg as usize, 85);
510 }
511
512 #[test]
513 fn op_modes_table_length() {
514 assert_eq!(OP_MODES.len(), NUM_OPCODES);
515 }
516
517 #[test]
518 fn opmode_byte_values() {
519 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); }
526
527 #[test]
528 fn from_u32_round_trip() {
529 for i in 0..NUM_OPCODES {
530 let op = OpCode::from_u32(i as u32).expect("valid opcode");
531 assert_eq!(op as usize, i);
532 }
533 assert!(OpCode::from_u32(86).is_none());
534 }
535
536 #[test]
537 fn instruction_arg_a() {
538 let i = Instruction::abck(OpCode::Move, 5, 3, 0, 0);
539 assert_eq!(i.arg_a(), 5);
540 assert_eq!(i.arg_b(), 3);
541 }
542
543 #[test]
544 fn instruction_s_bx_round_trip() {
545 let mut i = Instruction::abx(OpCode::LoadK, 0, 0);
546 i.set_arg_s_bx(-10);
547 assert_eq!(i.arg_s_bx(), -10);
548 i.set_arg_s_bx(0);
549 assert_eq!(i.arg_s_bx(), 0);
550 i.set_arg_s_bx(100);
551 assert_eq!(i.arg_s_bx(), 100);
552 }
553
554 #[test]
555 fn instruction_s_j_round_trip() {
556 let mut i = Instruction::sj(OpCode::Jmp, (OFFSET_S_J) as u32, 0);
557 assert_eq!(i.arg_s_j(), 0);
558 i.set_arg_s_j(42);
559 assert_eq!(i.arg_s_j(), 42);
560 i.set_arg_s_j(-1);
561 assert_eq!(i.arg_s_j(), -1);
562 }
563
564 #[test]
565 fn get_op_mode_smoke() {
566 assert_eq!(get_op_mode(OpCode::Move), OpMode::Abc);
567 assert_eq!(get_op_mode(OpCode::LoadI), OpMode::AsBx);
568 assert_eq!(get_op_mode(OpCode::LoadK), OpMode::ABx);
569 assert_eq!(get_op_mode(OpCode::Jmp), OpMode::SJ);
570 assert_eq!(get_op_mode(OpCode::ExtraArg), OpMode::Ax);
571 }
572
573 #[test]
574 fn int_to_s_c_round_trip() {
575 assert_eq!(s_c_to_int(int_to_s_c(0)), 0);
576 assert_eq!(s_c_to_int(int_to_s_c(100)), 100);
577 assert_eq!(s_c_to_int(int_to_s_c(-50)), -50);
578 }
579}
580
581