Skip to main content

lua_code/
opcodes.rs

1//! Opcode definitions and instruction encoding/decoding for the Lua 5.4 VM.
2//!
3//! Ports `src/lopcodes.c` (the `luaP_opmodes` table) and `src/lopcodes.h`
4//! (the `OpCode`/`OpMode` enums, field-size constants, and instruction
5//! accessor macros). Per PORTING.md §1, headers merge into their consuming
6//! `.rs`.
7//!
8//! C source preserved inline as `
9
10// ─── Instruction format diagram ──────────────────────────────────────────────
11//
12
13// ─── OpMode ──────────────────────────────────────────────────────────────────
14
15/// Instruction addressing mode.
16///
17#[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
27// ─── Field size constants ─────────────────────────────────────────────────────
28//
29
30pub 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
38// ─── Field position constants ─────────────────────────────────────────────────
39//
40
41pub 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
50// ─── Argument limit constants ─────────────────────────────────────────────────
51//
52
53pub 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
63/// Sentinel "no register" value that fits in 8 bits.
64///
65pub const NO_REG: u32 = MAXARG_A;
66
67/// Maximum RK index (for debugging only).
68///
69pub const MAXINDEXRK: u32 = MAXARG_B;
70
71/// Number of list items to accumulate before a SETLIST instruction.
72///
73pub const LFIELDS_PER_FLUSH: u32 = 50;
74
75// ─── OpCode enum ─────────────────────────────────────────────────────────────
76//
77//
78// ORDER OP — variant discriminants must match `lopcodes.h` exactly.
79// The VM casts the raw opcode field directly to this enum.
80
81/// The opcode enum is owned by `lua-vm` (the VM hot path decodes against
82/// it directly); `lua-code` re-exports it so codegen and the VM share one
83/// definition. `OpCode::from_u32` is the canonical decoder.
84pub use lua_vm::vm::OpCode;
85
86/// Total number of opcodes.
87///
88pub const NUM_OPCODES: usize = OpCode::GetVArg as usize + 1;
89
90// ─── opmode_byte helper ───────────────────────────────────────────────────────
91//
92//        (((mm)<<7) | ((ot)<<6) | ((it)<<5) | ((t)<<4) | ((a)<<3) | (m))
93//
94// Bit layout for each entry in OP_MODES:
95//   bits 0-2: OpMode value (Abc=0 ABx=1 AsBx=2 Ax=3 SJ=4)
96//   bit 3:    instruction sets register A
97//   bit 4:    is a test (next instruction must be a jump)
98//   bit 5:    instruction uses L->top from previous (IT mode)
99//   bit 6:    instruction sets L->top for next (OT mode)
100//   bit 7:    is a metamethod instruction (MM)
101
102const 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
106// Shorthand mode constants for the OP_MODES table below.
107const 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
113// ─── OP_MODES table ───────────────────────────────────────────────────────────
114//
115//
116// Per macros.tsv: LUAI_DDEF → drop (definition site, no modifier needed in Rust).
117// Per macros.tsv: LUAI_DDEC → `pub(crate) static` at the declaration site.
118
119/// Opcode properties table, indexed by `OpCode as usize`.
120///
121///
122/// Use `get_op_mode`, `test_a_mode`, etc. to query individual properties
123/// rather than indexing this array directly.
124pub(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
213// ─── OP_MODES accessors ───────────────────────────────────────────────────────
214//
215
216/// Extract the `OpMode` for an opcode.
217///
218pub 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        // PERF(port): unreachable branch — values 5-7 are unused; profile in Phase B
226        _ => OpMode::Abc,
227    }
228}
229
230/// True if this opcode writes to register A.
231///
232#[inline]
233pub fn test_a_mode(op: OpCode) -> bool {
234    (OP_MODES[op as usize] & (1 << 3)) != 0
235}
236
237/// True if this opcode is a test (the next instruction must be a jump).
238///
239#[inline]
240pub fn test_t_mode(op: OpCode) -> bool {
241    (OP_MODES[op as usize] & (1 << 4)) != 0
242}
243
244/// True if this opcode uses `L->top` as set by the previous instruction (B == 0 case).
245///
246#[inline]
247pub fn test_it_mode(op: OpCode) -> bool {
248    (OP_MODES[op as usize] & (1 << 5)) != 0
249}
250
251/// True if this opcode sets `L->top` for the next instruction (C == 0 case).
252///
253#[inline]
254pub fn test_ot_mode(op: OpCode) -> bool {
255    (OP_MODES[op as usize] & (1 << 6)) != 0
256}
257
258/// True if this opcode is a metamethod call.
259///
260#[inline]
261pub fn test_mm_mode(op: OpCode) -> bool {
262    (OP_MODES[op as usize] & (1 << 7)) != 0
263}
264
265// ─── Instruction newtype ──────────────────────────────────────────────────────
266//
267// Per types.tsv: `Instruction` is a `u32` newtype; bytecode word.
268// All accessor/builder macros from lopcodes.h become methods here.
269
270/// A single Lua bytecode instruction (unsigned 32-bit word).
271///
272#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
273#[repr(transparent)]
274pub struct Instruction(pub u32);
275
276impl Instruction {
277    // ── Low-level field accessors ─────────────────────────────────────────
278
279    /// Extract a bit-field of `size` bits at position `pos`.
280    ///
281    #[inline]
282    pub const fn get_arg(self, pos: u32, size: u32) -> u32 {
283        (self.0 >> pos) & ((1u32 << size) - 1)
284    }
285
286    /// Set a bit-field of `size` bits at position `pos` to `v`.
287    ///
288    #[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    // ── Opcode field ──────────────────────────────────────────────────────
295
296    /// Extract the opcode.
297    ///
298    #[inline]
299    pub fn opcode(self) -> Option<OpCode> {
300        OpCode::from_u32(self.get_arg(POS_OP, SIZE_OP))
301    }
302
303    /// Replace the opcode field.
304    ///
305    #[inline]
306    pub fn set_opcode(&mut self, op: OpCode) {
307        self.set_arg(op as u32, POS_OP, SIZE_OP);
308    }
309
310    // ── A field ───────────────────────────────────────────────────────────
311
312    #[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    // ── k bit ─────────────────────────────────────────────────────────────
323
324    #[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    // ── B field (iABC only) ───────────────────────────────────────────────
340
341    #[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    // ── C field (iABC only) ───────────────────────────────────────────────
357
358    #[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    // ── Bx field (iABx / iAsBx) ──────────────────────────────────────────
374
375    #[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    // ── Ax field (iAx) ────────────────────────────────────────────────────
396
397    #[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    // ── sJ field (isJ) ────────────────────────────────────────────────────
408
409    #[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    // ── Instruction builders ──────────────────────────────────────────────
420
421    /// Build an `iABC` instruction.
422    ///
423    #[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    /// Build an `iABx` instruction.
429    ///
430    #[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    /// Build an `iAx` instruction.
436    ///
437    #[inline]
438    pub fn ax(op: OpCode, a: u32) -> Self {
439        Self(((op as u32) << POS_OP) | (a << POS_AX))
440    }
441
442    /// Build an `isJ` instruction.
443    ///
444    #[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    // ── Mode query helpers (isOT / isIT) ──────────────────────────────────
450
451    /// True if this instruction sets `L->top` for the next instruction.
452    ///
453    /// `(testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || GET_OPCODE(i) == OP_TAILCALL`
454    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    /// True if this instruction uses `L->top` from the previous instruction.
462    ///
463    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    /// Return the `OpMode` for this instruction.
471    ///
472    pub fn op_mode(self) -> Option<OpMode> {
473        self.opcode().map(get_op_mode)
474    }
475}
476
477// ─── Signed-argument encode/decode helpers ────────────────────────────────────
478//
479//
480// These are inline helpers used at call sites; the Instruction methods above
481// incorporate them, but standalone functions are provided for codegen use.
482
483/// Encode a signed integer into an unsigned C-field value.
484///
485#[inline]
486pub const fn int_to_s_c(i: i32) -> u32 {
487    (i + OFFSET_S_C) as u32
488}
489
490/// Decode a C-field unsigned value to a signed integer.
491///
492#[inline]
493pub const fn s_c_to_int(i: u32) -> i32 {
494    i as i32 - OFFSET_S_C
495}
496
497// ─── Tests ────────────────────────────────────────────────────────────────────
498
499#[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); // a=1, mode=iABC=0 → 8
520        assert_eq!(OP_MODES[OpCode::LoadI as usize], 0b00001010); // a=1, mode=iAsBx=2 → 10
521        assert_eq!(OP_MODES[OpCode::Jmp as usize], 0b00000100); // a=0, mode=isJ=4 → 4
522        assert_eq!(OP_MODES[OpCode::MmBin as usize], 0b10000000); // mm=1, a=0, mode=iABC=0 → 128
523        assert_eq!(OP_MODES[OpCode::Call as usize], 0b01101000); // ot=1,it=1,a=1,mode=0 → 104
524        assert_eq!(OP_MODES[OpCode::ExtraArg as usize], 0b00000011); // mode=iAx=3 → 3
525    }
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// ──────────────────────────────────────────────────────────────────────────
582// PORT STATUS
583//   source:        src/lopcodes.c  (104 lines, 0 functions — data only)
584//                  src/lopcodes.h  (406 lines, merged per PORTING.md §1)
585//   target_crate:  lua-code
586//   confidence:    high
587//   todos:         1
588//   port_notes:    1
589//   unsafe_blocks: 0
590//   notes:         Pure data/encoding translation; OpCode::from_u32 needs
591//                  Phase B review for transmute vs. num_enum. Self_ rename
592//                  is permanent (Rust keyword conflict).
593// ──────────────────────────────────────────────────────────────────────────