1#[derive(Debug, Clone, Copy)]
8pub struct BytecodeVersion(pub u32);
9
10#[derive(Debug, Clone)]
12pub struct MoveModule {
13 pub version: BytecodeVersion,
15 pub name: String,
17 pub identifiers_offset: u32,
19 pub identifiers_count: u32,
21 pub struct_defs_offset: u32,
23 pub struct_defs_count: u32,
25 pub _function_handles_offset: u32,
27 pub _function_handles_count: u32,
29 pub function_defs_offset: u32,
31 pub function_defs_count: u32,
33 pub structs: Vec<StructDef>,
35 pub functions: Vec<FunctionDef>,
37}
38
39#[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#[derive(Debug, Clone)]
56pub struct StructDef {
57 pub name: String,
59 pub abilities: AbilitySet,
61 pub fields: Vec<FieldDef>,
63}
64
65#[derive(Debug, Clone)]
67pub struct FieldDef {
68 pub name: String,
70 pub type_tag: TypeTag,
72}
73
74#[derive(Debug, Clone)]
76pub struct FunctionDef {
77 pub name: String,
79 pub is_public: bool,
81 pub is_entry: bool,
83 pub parameters: Vec<TypeTag>,
85 pub returns: Vec<TypeTag>,
87 pub locals: Vec<TypeTag>,
89 pub code: Vec<MoveOpcode>,
91}
92
93#[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 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", TypeTag::U256 => "i64", TypeTag::Address => "i32", TypeTag::Signer => "i32", TypeTag::Vector(_) => "i32", TypeTag::Struct(_) => "i32", TypeTag::Reference(_) => "i32",
123 TypeTag::MutableReference(_) => "i32",
124 }
125 }
126}
127
128#[derive(Debug, Clone)]
130pub enum MoveOpcode {
131 LdU8(u8),
133 LdU64(u64),
134 LdU128(u128),
135 LdTrue,
136 LdFalse,
137 LdConst(u16), CopyLoc(u8),
141 MoveLoc(u8),
142 StLoc(u8),
143 MutBorrowLoc(u8),
144 ImmBorrowLoc(u8),
145
146 Add,
148 Sub,
149 Mul,
150 Div,
151 Mod,
152
153 Lt,
155 Gt,
156 Le,
157 Ge,
158 Eq,
159 Neq,
160
161 And,
163 Or,
164 Not,
165
166 Branch(u16),
168 BrTrue(u16),
169 BrFalse(u16),
170 Call(u16), Ret,
172 Abort,
173
174 Pack(u16), Unpack(u16),
177 BorrowField(u16),
178 MutBorrowField(u16),
179 MoveFrom(u16), MoveTo(u16),
181 Exists(u16),
182 BorrowGlobal(u16),
183 MutBorrowGlobal(u16),
184
185 Pop,
187
188 VecPack(u16, u64),
190 VecLen(u16),
191 VecImmBorrow(u16),
192 VecMutBorrow(u16),
193 VecPushBack(u16),
194 VecPopBack(u16),
195
196 CastU8,
198 CastU64,
199 CastU128,
200
201 Nop,
203}
204
205impl MoveOpcode {
206 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}