Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit 0c06bd9

Browse files
committed
[gas] add SimpleInstruction::to_opcode
1 parent ee1efcb commit 0c06bd9

File tree

2 files changed

+63
-59
lines changed

2 files changed

+63
-59
lines changed

language/move-vm/test-utils/src/gas_schedule.rs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -200,71 +200,14 @@ impl<'a> GasStatus<'a> {
200200
}
201201
}
202202

203-
fn get_simple_instruction_opcode(instr: SimpleInstruction) -> Opcodes {
204-
use Opcodes::*;
205-
use SimpleInstruction::*;
206-
207-
match instr {
208-
Nop => NOP,
209-
Ret => RET,
210-
211-
LdU8 => LD_U8,
212-
LdU64 => LD_U64,
213-
LdU128 => LD_U128,
214-
LdTrue => LD_TRUE,
215-
LdFalse => LD_FALSE,
216-
217-
FreezeRef => FREEZE_REF,
218-
MutBorrowLoc => MUT_BORROW_LOC,
219-
ImmBorrowLoc => IMM_BORROW_LOC,
220-
ImmBorrowField => IMM_BORROW_FIELD,
221-
MutBorrowField => MUT_BORROW_FIELD,
222-
ImmBorrowFieldGeneric => IMM_BORROW_FIELD_GENERIC,
223-
MutBorrowFieldGeneric => MUT_BORROW_FIELD_GENERIC,
224-
225-
CastU8 => CAST_U8,
226-
CastU64 => CAST_U64,
227-
CastU128 => CAST_U128,
228-
229-
Add => ADD,
230-
Sub => SUB,
231-
Mul => MUL,
232-
Mod => MOD,
233-
Div => DIV,
234-
235-
BitOr => BIT_OR,
236-
BitAnd => BIT_AND,
237-
Xor => XOR,
238-
Shl => SHL,
239-
Shr => SHR,
240-
241-
Or => OR,
242-
And => AND,
243-
Not => NOT,
244-
245-
Lt => LT,
246-
Gt => GT,
247-
Le => LE,
248-
Ge => GE,
249-
250-
Abort => ABORT,
251-
LdU16 => LD_U16,
252-
LdU32 => LD_U32,
253-
LdU256 => LD_U256,
254-
CastU16 => CAST_U16,
255-
CastU32 => CAST_U32,
256-
CastU256 => CAST_U256,
257-
}
258-
}
259-
260203
impl<'b> GasMeter for GasStatus<'b> {
261204
fn balance_internal(&self) -> InternalGas {
262205
self.gas_left
263206
}
264207

265208
/// Charge an instruction and fail if not enough gas units are left.
266209
fn charge_simple_instr(&mut self, instr: SimpleInstruction) -> PartialVMResult<()> {
267-
self.charge_instr(get_simple_instruction_opcode(instr))
210+
self.charge_instr(instr.to_opcode())
268211
}
269212

270213
fn charge_br_false(&mut self, _target_offset: Option<CodeOffset>) -> PartialVMResult<()> {

language/move-vm/types/src/gas.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::views::{TypeView, ValueView};
5-
use move_binary_format::{errors::PartialVMResult, file_format::CodeOffset};
5+
use move_binary_format::{
6+
errors::PartialVMResult, file_format::CodeOffset, file_format_common::Opcodes,
7+
};
68
use move_core_types::{
79
account_address::AccountAddress,
810
gas_algebra::{InternalGas, NumArgs, NumBytes},
@@ -64,6 +66,65 @@ pub enum SimpleInstruction {
6466
CastU256,
6567
}
6668

69+
impl SimpleInstruction {
70+
pub fn to_opcode(&self) -> Opcodes {
71+
use Opcodes::*;
72+
use SimpleInstruction::*;
73+
74+
match self {
75+
Nop => NOP,
76+
Ret => RET,
77+
78+
LdU8 => LD_U8,
79+
LdU64 => LD_U64,
80+
LdU128 => LD_U128,
81+
LdTrue => LD_TRUE,
82+
LdFalse => LD_FALSE,
83+
84+
FreezeRef => FREEZE_REF,
85+
MutBorrowLoc => MUT_BORROW_LOC,
86+
ImmBorrowLoc => IMM_BORROW_LOC,
87+
ImmBorrowField => IMM_BORROW_FIELD,
88+
MutBorrowField => MUT_BORROW_FIELD,
89+
ImmBorrowFieldGeneric => IMM_BORROW_FIELD_GENERIC,
90+
MutBorrowFieldGeneric => MUT_BORROW_FIELD_GENERIC,
91+
92+
CastU8 => CAST_U8,
93+
CastU64 => CAST_U64,
94+
CastU128 => CAST_U128,
95+
96+
Add => ADD,
97+
Sub => SUB,
98+
Mul => MUL,
99+
Mod => MOD,
100+
Div => DIV,
101+
102+
BitOr => BIT_OR,
103+
BitAnd => BIT_AND,
104+
Xor => XOR,
105+
Shl => SHL,
106+
Shr => SHR,
107+
108+
Or => OR,
109+
And => AND,
110+
Not => NOT,
111+
112+
Lt => LT,
113+
Gt => GT,
114+
Le => LE,
115+
Ge => GE,
116+
117+
Abort => ABORT,
118+
LdU16 => LD_U16,
119+
LdU32 => LD_U32,
120+
LdU256 => LD_U256,
121+
CastU16 => CAST_U16,
122+
CastU32 => CAST_U32,
123+
CastU256 => CAST_U256,
124+
}
125+
}
126+
}
127+
67128
/// Trait that defines a generic gas meter interface, allowing clients of the Move VM to implement
68129
/// their own metering scheme.
69130
pub trait GasMeter {

0 commit comments

Comments
 (0)