NAC_Blockchain/nac-nvm/src/bytecode.rs

174 lines
4.0 KiB
Rust

//! 字节码定义和解析
use serde::{Deserialize, Serialize};
/// 操作码枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum Opcode {
// 栈操作
Push = 0x01,
Pop = 0x02,
Dup = 0x03,
Swap = 0x04,
// 算术运算
Add = 0x10,
Sub = 0x11,
Mul = 0x12,
Div = 0x13,
Mod = 0x14,
// 比较运算
Eq = 0x20,
Ne = 0x21,
Lt = 0x22,
Le = 0x23,
Gt = 0x24,
Ge = 0x25,
// 逻辑运算
And = 0x30,
Or = 0x31,
Not = 0x32,
// 内存操作
Load = 0x40,
Store = 0x41,
// 控制流
Jump = 0x50,
JumpIf = 0x51,
Call = 0x52,
Return = 0x53,
Halt = 0x54,
// 区块链操作
GetBalance = 0x60,
Transfer = 0x61,
GetCaller = 0x62,
GetTimestamp = 0x63,
GetBlockNumber = 0x64,
// 加密操作
Sha3 = 0x70,
VerifySignature = 0x71,
}
impl Opcode {
pub fn from_u8(byte: u8) -> Option<Self> {
match byte {
0x01 => Some(Opcode::Push),
0x02 => Some(Opcode::Pop),
0x03 => Some(Opcode::Dup),
0x04 => Some(Opcode::Swap),
0x10 => Some(Opcode::Add),
0x11 => Some(Opcode::Sub),
0x12 => Some(Opcode::Mul),
0x13 => Some(Opcode::Div),
0x14 => Some(Opcode::Mod),
0x20 => Some(Opcode::Eq),
0x21 => Some(Opcode::Ne),
0x22 => Some(Opcode::Lt),
0x23 => Some(Opcode::Le),
0x24 => Some(Opcode::Gt),
0x25 => Some(Opcode::Ge),
0x30 => Some(Opcode::And),
0x31 => Some(Opcode::Or),
0x32 => Some(Opcode::Not),
0x40 => Some(Opcode::Load),
0x41 => Some(Opcode::Store),
0x50 => Some(Opcode::Jump),
0x51 => Some(Opcode::JumpIf),
0x52 => Some(Opcode::Call),
0x53 => Some(Opcode::Return),
0x54 => Some(Opcode::Halt),
0x60 => Some(Opcode::GetBalance),
0x61 => Some(Opcode::Transfer),
0x62 => Some(Opcode::GetCaller),
0x63 => Some(Opcode::GetTimestamp),
0x64 => Some(Opcode::GetBlockNumber),
0x70 => Some(Opcode::Sha3),
0x71 => Some(Opcode::VerifySignature),
_ => None,
}
}
}
/// 指令结构
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Instruction {
pub opcode: Opcode,
pub operands: Vec<u64>,
}
impl Instruction {
pub fn new(opcode: Opcode) -> Self {
Instruction {
opcode,
operands: Vec::new(),
}
}
pub fn with_operand(opcode: Opcode, operand: u64) -> Self {
Instruction {
opcode,
operands: vec![operand],
}
}
}
/// 字节码容器
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bytecode {
pub instructions: Vec<Instruction>,
}
impl Bytecode {
pub fn new() -> Self {
Bytecode {
instructions: Vec::new(),
}
}
pub fn add_instruction(&mut self, instruction: Instruction) {
self.instructions.push(instruction);
}
pub fn len(&self) -> usize {
self.instructions.len()
}
pub fn is_empty(&self) -> bool {
self.instructions.is_empty()
}
}
impl Default for Bytecode {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_opcode_conversion() {
assert_eq!(Opcode::from_u8(0x01), Some(Opcode::Push));
assert_eq!(Opcode::from_u8(0x10), Some(Opcode::Add));
assert_eq!(Opcode::from_u8(0xFF), None);
}
#[test]
fn test_bytecode_creation() {
let mut bytecode = Bytecode::new();
bytecode.add_instruction(Instruction::with_operand(Opcode::Push, 42));
bytecode.add_instruction(Instruction::new(Opcode::Pop));
assert_eq!(bytecode.len(), 2);
assert!(!bytecode.is_empty());
}
}