Skip to content

Commit

Permalink
fixup! fixup! fixup! [move-vm] Removed most RCs from runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tnowacki committed Apr 6, 2022
1 parent a055230 commit 9e386c4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
28 changes: 18 additions & 10 deletions language/move-vm/runtime/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,53 +1168,60 @@ impl Frame {
let elements = interpreter.operand_stack.popn(*num as u16)?;
let size = AbstractMemorySize::new(*num);
gas_status.charge_instr_with_size(Opcodes::VEC_PACK, size)?;
let value = Vector::pack(resolver.single_type_at(*si), elements)?;
let value = Vector::pack(
&resolver.instantiate_single_type(*si, self.ty_args())?,
elements,
)?;
interpreter.operand_stack.push(value)?;
}
Bytecode::VecLen(si) => {
let vec_ref = interpreter.operand_stack.pop_as::<VectorRef>()?;
gas_status.charge_instr(Opcodes::VEC_LEN)?;
// see REFERENCE SAFETY EXPLANATION in values
let value = unsafe { vec_ref.len(resolver.single_type_at(*si))? };
let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?;
let value = unsafe { vec_ref.len(vec_ty_arg)? };
interpreter.operand_stack.push(value)?;
}
Bytecode::VecImmBorrow(si) => {
let idx = interpreter.operand_stack.pop_as::<u64>()? as usize;
let vec_ref = interpreter.operand_stack.pop_as::<VectorRef>()?;
gas_status.charge_instr(Opcodes::VEC_IMM_BORROW)?;
// see REFERENCE SAFETY EXPLANATION in values
let value =
unsafe { vec_ref.borrow_elem(idx, resolver.single_type_at(*si))? };
let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?;
let value = unsafe { vec_ref.borrow_elem(idx, vec_ty_arg)? };
interpreter.operand_stack.push(value)?;
}
Bytecode::VecMutBorrow(si) => {
let idx = interpreter.operand_stack.pop_as::<u64>()? as usize;
let vec_ref = interpreter.operand_stack.pop_as::<VectorRef>()?;
gas_status.charge_instr(Opcodes::VEC_MUT_BORROW)?;
// see REFERENCE SAFETY EXPLANATION in values
let value =
unsafe { vec_ref.borrow_elem(idx, resolver.single_type_at(*si))? };
let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?;
let value = unsafe { vec_ref.borrow_elem(idx, vec_ty_arg)? };
interpreter.operand_stack.push(value)?;
}
Bytecode::VecPushBack(si) => {
let elem = interpreter.operand_stack.pop()?;
let vec_ref = interpreter.operand_stack.pop_as::<VectorRef>()?;
gas_status.charge_instr_with_size(Opcodes::VEC_PUSH_BACK, elem.size())?;
// see REFERENCE SAFETY EXPLANATION in values
unsafe { vec_ref.push_back(elem, resolver.single_type_at(*si))? };
let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?;
unsafe { vec_ref.push_back(elem, vec_ty_arg)? };
}
Bytecode::VecPopBack(si) => {
let vec_ref = interpreter.operand_stack.pop_as::<VectorRef>()?;
gas_status.charge_instr(Opcodes::VEC_POP_BACK)?;
// see REFERENCE SAFETY EXPLANATION in values
let value = unsafe { vec_ref.pop(resolver.single_type_at(*si))? };
let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?;
let value = unsafe { vec_ref.pop(vec_ty_arg)? };
interpreter.operand_stack.push(value)?;
}
Bytecode::VecUnpack(si, num) => {
let vec_val = interpreter.operand_stack.pop_as::<Vector>()?;
let size = AbstractMemorySize::new(*num);
gas_status.charge_instr_with_size(Opcodes::VEC_UNPACK, size)?;
let elements = vec_val.unpack(resolver.single_type_at(*si), *num)?;
let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?;
let elements = vec_val.unpack(vec_ty_arg, *num)?;
for value in elements {
interpreter.operand_stack.push(value)?;
}
Expand All @@ -1225,7 +1232,8 @@ impl Frame {
let vec_ref = interpreter.operand_stack.pop_as::<VectorRef>()?;
gas_status.charge_instr(Opcodes::VEC_SWAP)?;
// see REFERENCE SAFETY EXPLANATION in values
unsafe { vec_ref.swap(idx1, idx2, resolver.single_type_at(*si))? };
let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?;
unsafe { vec_ref.swap(idx1, idx2, vec_ty_arg)? };
}
}
// invariant: advance to pc +1 is iff instruction at pc executed without aborting
Expand Down
27 changes: 16 additions & 11 deletions language/move-vm/runtime/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,22 @@ impl<'a> Resolver<'a> {
))
}

fn single_type_at(&self, idx: SignatureIndex) -> &Type {
match &self.binary {
BinaryType::Module(module) => module.single_type_at(idx),
BinaryType::Script(script) => script.single_type_at(idx),
}
}

pub(crate) fn instantiate_single_type(
&self,
idx: SignatureIndex,
ty_args: &[Type],
) -> PartialVMResult<Type> {
let ty = self.single_type_at(idx);
ty.subst(ty_args)
}

//
// Fields resolution
//
Expand Down Expand Up @@ -1319,17 +1335,6 @@ impl<'a> Resolver<'a> {
self.loader.type_to_type_layout(ty)
}

//
// Type resolution
//

pub(crate) fn single_type_at(&self, idx: SignatureIndex) -> &Type {
match &self.binary {
BinaryType::Module(module) => module.single_type_at(idx),
BinaryType::Script(script) => script.single_type_at(idx),
}
}

// get the loader
pub(crate) fn loader(&self) -> &Loader {
self.loader
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 3 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//# publish

module 0x42.Tests {

public test<T: copy + drop>(x: T) {
let v: vector<T>;
label l0:
v = vec_pack_0<T>();
assert(vec_len<T>(&v) == 0, 42);
vec_push_back<T>(&mut v, copy(x));
vec_push_back<T>(&mut v, copy(x));
vec_swap<T>(&mut v, 0, 1);
_ = vec_imm_borrow<T>(&v, 0);
_ = vec_mut_borrow<T>(&mut v, 0);
_ = vec_pop_back<T>(&mut v);
_ = vec_pop_back<T>(&mut v);
vec_unpack_0<T>(move(v));
return;
}
}

//# run 0x42::Tests::test --type-args u64 --args 0

//# run 0x42::Tests::test --type-args vector<u8> --args b"hello"

0 comments on commit 9e386c4

Please sign in to comment.