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