Skip to main content

lua_code/
opcodes.rs

1//! Opcode definitions and instruction encoding/decoding for the Lua 5.4 VM.
2//!
3//! C source: `reference/lua-5.4.7/src/lopcodes.c` (the `luaP_opmodes` table)
4//! and `reference/lua-5.4.7/src/lopcodes.h` (the `OpCode`/`OpMode` enums,
5//! field-size constants, and instruction accessor macros — the header
6//! merges into this file, per PORTING.md §1).
7
8// ─── Instruction format diagram ──────────────────────────────────────────────
9//
10
11// ─── OpMode ──────────────────────────────────────────────────────────────────
12
13/// Instruction addressing mode.
14///
15#[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
25// ─── Field size constants ─────────────────────────────────────────────────────
26//
27
28pub 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
36// ─── Field position constants ─────────────────────────────────────────────────
37//
38
39pub 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
48// ─── Argument limit constants ─────────────────────────────────────────────────
49//
50
51pub 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
61/// Sentinel "no register" value that fits in 8 bits.
62///
63pub const NO_REG: u32 = MAXARG_A;
64
65/// Maximum RK index (for debugging only).
66///
67pub const MAXINDEXRK: u32 = MAXARG_B;
68
69/// Number of list items to accumulate before a SETLIST instruction.
70///
71pub const LFIELDS_PER_FLUSH: u32 = 50;
72
73// ─── OpCode enum ─────────────────────────────────────────────────────────────
74//
75//
76// ORDER OP — variant discriminants must match `lopcodes.h` exactly.
77// The VM casts the raw opcode field directly to this enum.
78
79/// The opcode enum is owned by `lua-vm` (the VM hot path decodes against
80/// it directly); `lua-code` re-exports it so codegen and the VM share one
81/// definition. `OpCode::from_u32` is the canonical decoder.
82pub use lua_vm::vm::OpCode;
83
84/// Total number of opcodes.
85///
86pub const NUM_OPCODES: usize = OpCode::GetVArg as usize + 1;
87
88// ─── opmode_byte helper ───────────────────────────────────────────────────────
89//
90//        (((mm)<<7) | ((ot)<<6) | ((it)<<5) | ((t)<<4) | ((a)<<3) | (m))
91//
92// Bit layout for each entry in OP_MODES:
93//   bits 0-2: OpMode value (Abc=0 ABx=1 AsBx=2 Ax=3 SJ=4)
94//   bit 3:    instruction sets register A
95//   bit 4:    is a test (next instruction must be a jump)
96//   bit 5:    instruction uses L->top from previous (IT mode)
97//   bit 6:    instruction sets L->top for next (OT mode)
98//   bit 7:    is a metamethod instruction (MM)
99
100const 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
104// Shorthand mode constants for the OP_MODES table below.
105const 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
111// ─── OP_MODES table ───────────────────────────────────────────────────────────
112
113/// Opcode properties table, indexed by `OpCode as usize`.
114///
115///
116/// Use `get_op_mode`, `test_a_mode`, etc. to query individual properties
117/// rather than indexing this array directly.
118pub(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
207// ─── OP_MODES accessors ───────────────────────────────────────────────────────
208//
209
210/// Extract the `OpMode` for an opcode.
211///
212pub 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        // Values 5-7 are unused by any opcode; this arm is unreachable in practice.
220        _ => OpMode::Abc,
221    }
222}
223
224/// True if this opcode writes to register A.
225///
226#[inline]
227pub fn test_a_mode(op: OpCode) -> bool {
228    (OP_MODES[op as usize] & (1 << 3)) != 0
229}
230
231/// True if this opcode is a test (the next instruction must be a jump).
232///
233#[inline]
234pub fn test_t_mode(op: OpCode) -> bool {
235    (OP_MODES[op as usize] & (1 << 4)) != 0
236}
237
238/// True if this opcode uses `L->top` as set by the previous instruction (B == 0 case).
239///
240#[inline]
241pub fn test_it_mode(op: OpCode) -> bool {
242    (OP_MODES[op as usize] & (1 << 5)) != 0
243}
244
245/// True if this opcode sets `L->top` for the next instruction (C == 0 case).
246///
247#[inline]
248pub fn test_ot_mode(op: OpCode) -> bool {
249    (OP_MODES[op as usize] & (1 << 6)) != 0
250}
251
252/// True if this opcode is a metamethod call.
253///
254#[inline]
255pub fn test_mm_mode(op: OpCode) -> bool {
256    (OP_MODES[op as usize] & (1 << 7)) != 0
257}
258
259// ─── Instruction newtype ──────────────────────────────────────────────────────
260//
261// A single bytecode word; accessor/builder logic corresponding to
262// `lopcodes.h`'s macros lives as methods below.
263
264/// A single Lua bytecode instruction (unsigned 32-bit word).
265///
266#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
267#[repr(transparent)]
268pub struct Instruction(pub u32);
269
270impl Instruction {
271    // ── Low-level field accessors ─────────────────────────────────────────
272
273    /// Extract a bit-field of `size` bits at position `pos`.
274    ///
275    #[inline]
276    pub const fn get_arg(self, pos: u32, size: u32) -> u32 {
277        (self.0 >> pos) & ((1u32 << size) - 1)
278    }
279
280    /// Set a bit-field of `size` bits at position `pos` to `v`.
281    ///
282    #[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    // ── Opcode field ──────────────────────────────────────────────────────
289
290    /// Extract the opcode.
291    ///
292    #[inline]
293    pub fn opcode(self) -> Option<OpCode> {
294        OpCode::from_u32(self.get_arg(POS_OP, SIZE_OP))
295    }
296
297    /// Replace the opcode field.
298    ///
299    #[inline]
300    pub fn set_opcode(&mut self, op: OpCode) {
301        self.set_arg(op as u32, POS_OP, SIZE_OP);
302    }
303
304    // ── A field ───────────────────────────────────────────────────────────
305
306    #[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    // ── k bit ─────────────────────────────────────────────────────────────
317
318    #[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    // ── B field (iABC only) ───────────────────────────────────────────────
334
335    #[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    // ── C field (iABC only) ───────────────────────────────────────────────
351
352    #[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    // ── Bx field (iABx / iAsBx) ──────────────────────────────────────────
368
369    #[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    // ── Ax field (iAx) ────────────────────────────────────────────────────
390
391    #[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    // ── sJ field (isJ) ────────────────────────────────────────────────────
402
403    #[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    // ── Instruction builders ──────────────────────────────────────────────
414
415    /// Build an `iABC` instruction.
416    ///
417    #[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    /// Build an `iABx` instruction.
423    ///
424    #[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    /// Build an `iAx` instruction.
430    ///
431    #[inline]
432    pub fn ax(op: OpCode, a: u32) -> Self {
433        Self(((op as u32) << POS_OP) | (a << POS_AX))
434    }
435
436    /// Build an `isJ` instruction.
437    ///
438    #[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    // ── Mode query helpers (isOT / isIT) ──────────────────────────────────
444
445    /// True if this instruction sets `L->top` for the next instruction.
446    ///
447    /// `(testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || GET_OPCODE(i) == OP_TAILCALL`
448    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    /// True if this instruction uses `L->top` from the previous instruction.
456    ///
457    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    /// Return the `OpMode` for this instruction.
465    ///
466    pub fn op_mode(self) -> Option<OpMode> {
467        self.opcode().map(get_op_mode)
468    }
469}
470
471// ─── Signed-argument encode/decode helpers ────────────────────────────────────
472//
473//
474// These are inline helpers used at call sites; the Instruction methods above
475// incorporate them, but standalone functions are provided for codegen use.
476
477/// Encode a signed integer into an unsigned C-field value.
478///
479#[inline]
480pub const fn int_to_s_c(i: i32) -> u32 {
481    (i + OFFSET_S_C) as u32
482}
483
484/// Decode a C-field unsigned value to a signed integer.
485///
486#[inline]
487pub const fn s_c_to_int(i: u32) -> i32 {
488    i as i32 - OFFSET_S_C
489}
490
491// ─── Tests ────────────────────────────────────────────────────────────────────
492
493#[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); // a=1, mode=iABC=0 → 8
514        assert_eq!(OP_MODES[OpCode::LoadI as usize], 0b00001010); // a=1, mode=iAsBx=2 → 10
515        assert_eq!(OP_MODES[OpCode::Jmp as usize], 0b00000100); // a=0, mode=isJ=4 → 4
516        assert_eq!(OP_MODES[OpCode::MmBin as usize], 0b10000000); // mm=1, a=0, mode=iABC=0 → 128
517        assert_eq!(OP_MODES[OpCode::Call as usize], 0b01101000); // ot=1,it=1,a=1,mode=0 → 104
518        assert_eq!(OP_MODES[OpCode::ExtraArg as usize], 0b00000011); // mode=iAx=3 → 3
519    }
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}