neo-devpack-solidity 0.22.0

Production-focused Solidity-to-NeoVM compilation system
Documentation
//! Code Generation Helpers
//!
//! Utility functions for bytecode generation.

/// Encode a small integer efficiently
pub fn encode_small_int(n: i64) -> Vec<u8> {
    match n {
        -1 => vec![0x0F],               // PUSHM1
        0..=16 => vec![0x10 + n as u8], // PUSH0-PUSH16
        _ => encode_varint(n),
    }
}

fn encode_varint(n: i64) -> Vec<u8> {
    let bytes = n.to_le_bytes();
    let len = minimal_bytes(n);
    let opcode = match len {
        1 => 0x00, // PUSHINT8
        2 => 0x01, // PUSHINT16
        4 => 0x02, // PUSHINT32
        _ => 0x03, // PUSHINT64
    };
    let mut result = vec![opcode];
    result.extend_from_slice(&bytes[..len]);
    result
}

fn minimal_bytes(n: i64) -> usize {
    if (-128..=127).contains(&n) {
        1
    } else if (-32768..=32767).contains(&n) {
        2
    } else if (-2147483648..=2147483647).contains(&n) {
        4
    } else {
        8
    }
}