Skip to main content

move_neovm/bytecode/
types.rs

1// Copyright (c) 2025-2026 R3E Network
2// SPDX-License-Identifier: MIT
3
4//! Move bytecode type definitions.
5
6/// Move bytecode version
7#[derive(Debug, Clone, Copy)]
8pub struct BytecodeVersion(pub u32);
9
10/// A parsed Move module
11#[derive(Debug, Clone)]
12pub struct MoveModule {
13    /// Module version
14    pub version: BytecodeVersion,
15    /// Module name
16    pub name: String,
17    /// Identifier table offset
18    pub identifiers_offset: u32,
19    /// Identifier table count
20    pub identifiers_count: u32,
21    /// Struct table offset
22    pub struct_defs_offset: u32,
23    /// Struct table count
24    pub struct_defs_count: u32,
25    /// Function handle offset (unused placeholder)
26    pub _function_handles_offset: u32,
27    /// Function handle count (unused placeholder)
28    pub _function_handles_count: u32,
29    /// Function table offset
30    pub function_defs_offset: u32,
31    /// Function table count
32    pub function_defs_count: u32,
33    /// Struct definitions
34    pub structs: Vec<StructDef>,
35    /// Function definitions
36    pub functions: Vec<FunctionDef>,
37}
38
39/// Ability flags attached to a struct definition
40#[derive(Debug, Clone, Copy, Default)]
41pub struct AbilitySet {
42    pub copy: bool,
43    pub drop: bool,
44    pub store: bool,
45    pub key: bool,
46}
47
48impl AbilitySet {
49    pub fn is_resource(&self) -> bool {
50        self.key
51    }
52}
53
54/// A struct (or resource) definition
55#[derive(Debug, Clone)]
56pub struct StructDef {
57    /// Struct name (fully-qualified or simple)
58    pub name: String,
59    /// Ability set declared on the struct
60    pub abilities: AbilitySet,
61    /// Field definitions
62    pub fields: Vec<FieldDef>,
63}
64
65/// A field definition
66#[derive(Debug, Clone)]
67pub struct FieldDef {
68    /// Field name
69    pub name: String,
70    /// Field type
71    pub type_tag: TypeTag,
72}
73
74/// A function definition
75#[derive(Debug, Clone)]
76pub struct FunctionDef {
77    /// Function name
78    pub name: String,
79    /// Is this a public function?
80    pub is_public: bool,
81    /// Is this an entry function?
82    pub is_entry: bool,
83    /// Parameter types
84    pub parameters: Vec<TypeTag>,
85    /// Return types
86    pub returns: Vec<TypeTag>,
87    /// Local slot layout (parameters are always the first locals)
88    pub locals: Vec<TypeTag>,
89    /// Function body (opcodes)
90    pub code: Vec<MoveOpcode>,
91}
92
93/// Move type tags
94#[derive(Debug, Clone)]
95pub enum TypeTag {
96    Bool,
97    U8,
98    U64,
99    U128,
100    U256,
101    Address,
102    Signer,
103    Vector(Box<TypeTag>),
104    Struct(String),
105    Reference(Box<TypeTag>),
106    MutableReference(Box<TypeTag>),
107}
108
109impl TypeTag {
110    /// Convert to WASM-compatible representation
111    pub fn to_wasm_type(&self) -> &'static str {
112        match self {
113            TypeTag::Bool => "i32",
114            TypeTag::U8 => "i32",
115            TypeTag::U64 => "i64",
116            TypeTag::U128 => "i64",      // Requires multi-word handling
117            TypeTag::U256 => "i64",      // Requires multi-word handling
118            TypeTag::Address => "i32",   // Pointer to 32-byte array
119            TypeTag::Signer => "i32",    // Pointer to signer data
120            TypeTag::Vector(_) => "i32", // Pointer to vector
121            TypeTag::Struct(_) => "i32", // Pointer to struct
122            TypeTag::Reference(_) => "i32",
123            TypeTag::MutableReference(_) => "i32",
124        }
125    }
126}
127
128/// Move VM opcodes (simplified subset)
129#[derive(Debug, Clone)]
130pub enum MoveOpcode {
131    // Constants
132    LdU8(u8),
133    LdU64(u64),
134    LdU128(u128),
135    LdTrue,
136    LdFalse,
137    LdConst(u16), // Constant pool index
138
139    // Local operations
140    CopyLoc(u8),
141    MoveLoc(u8),
142    StLoc(u8),
143    MutBorrowLoc(u8),
144    ImmBorrowLoc(u8),
145
146    // Arithmetic
147    Add,
148    Sub,
149    Mul,
150    Div,
151    Mod,
152
153    // Comparison
154    Lt,
155    Gt,
156    Le,
157    Ge,
158    Eq,
159    Neq,
160
161    // Logical
162    And,
163    Or,
164    Not,
165
166    // Control flow
167    Branch(u16),
168    BrTrue(u16),
169    BrFalse(u16),
170    Call(u16), // Function index
171    Ret,
172    Abort,
173
174    // Resource operations
175    Pack(u16), // Struct index
176    Unpack(u16),
177    BorrowField(u16),
178    MutBorrowField(u16),
179    MoveFrom(u16), // Struct index
180    MoveTo(u16),
181    Exists(u16),
182    BorrowGlobal(u16),
183    MutBorrowGlobal(u16),
184
185    // Stack
186    Pop,
187
188    // Vector operations
189    VecPack(u16, u64),
190    VecLen(u16),
191    VecImmBorrow(u16),
192    VecMutBorrow(u16),
193    VecPushBack(u16),
194    VecPopBack(u16),
195
196    // Casting
197    CastU8,
198    CastU64,
199    CastU128,
200
201    // Nop (placeholder for unsupported)
202    Nop,
203}
204
205impl MoveOpcode {
206    /// Get opcode byte value
207    pub fn opcode_byte(&self) -> u8 {
208        match self {
209            MoveOpcode::Pop => 0x01,
210            MoveOpcode::Ret => 0x02,
211            MoveOpcode::BrTrue(_) => 0x03,
212            MoveOpcode::BrFalse(_) => 0x04,
213            MoveOpcode::Branch(_) => 0x05,
214            MoveOpcode::LdU8(_) => 0x06,
215            MoveOpcode::LdU64(_) => 0x07,
216            MoveOpcode::LdU128(_) => 0x08,
217            MoveOpcode::CastU8 => 0x09,
218            MoveOpcode::CastU64 => 0x0A,
219            MoveOpcode::CastU128 => 0x0B,
220            MoveOpcode::LdConst(_) => 0x0C,
221            MoveOpcode::LdTrue => 0x0D,
222            MoveOpcode::LdFalse => 0x0E,
223            MoveOpcode::CopyLoc(_) => 0x0F,
224            MoveOpcode::MoveLoc(_) => 0x10,
225            MoveOpcode::StLoc(_) => 0x11,
226            MoveOpcode::MutBorrowLoc(_) => 0x12,
227            MoveOpcode::ImmBorrowLoc(_) => 0x13,
228            MoveOpcode::MutBorrowField(_) => 0x14,
229            MoveOpcode::BorrowField(_) => 0x15,
230            MoveOpcode::Call(_) => 0x16,
231            MoveOpcode::Pack(_) => 0x17,
232            MoveOpcode::Unpack(_) => 0x18,
233            MoveOpcode::Add => 0x22,
234            MoveOpcode::Sub => 0x23,
235            MoveOpcode::Mul => 0x24,
236            MoveOpcode::Mod => 0x25,
237            MoveOpcode::Div => 0x26,
238            MoveOpcode::Lt => 0x32,
239            MoveOpcode::Gt => 0x33,
240            MoveOpcode::Le => 0x34,
241            MoveOpcode::Ge => 0x35,
242            MoveOpcode::And => 0x40,
243            MoveOpcode::Or => 0x41,
244            MoveOpcode::Not => 0x42,
245            MoveOpcode::Eq => 0x43,
246            MoveOpcode::Neq => 0x44,
247            MoveOpcode::Abort => 0x45,
248            MoveOpcode::Exists(_) => 0x50,
249            MoveOpcode::BorrowGlobal(_) => 0x51,
250            MoveOpcode::MutBorrowGlobal(_) => 0x52,
251            MoveOpcode::MoveFrom(_) => 0x53,
252            MoveOpcode::MoveTo(_) => 0x54,
253            MoveOpcode::VecPack(_, _) => 0x60,
254            MoveOpcode::VecLen(_) => 0x61,
255            MoveOpcode::VecImmBorrow(_) => 0x62,
256            MoveOpcode::VecMutBorrow(_) => 0x63,
257            MoveOpcode::VecPushBack(_) => 0x64,
258            MoveOpcode::VecPopBack(_) => 0x65,
259            MoveOpcode::Nop => 0x00,
260        }
261    }
262}