Skip to content

Commit ae83e46

Browse files
committed
move-sui: add simulation for the cast_u8 instruction
1 parent 230d00e commit ae83e46

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

CoqOfRust/move_sui/simulations/move_vm_runtime/interpreter.v

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,16 @@ Definition execute_instruction (pc : Z)
10391039
.push(Value::u8(integer_value.cast_u8()?))?;
10401040
}
10411041
*)
1042-
| Bytecode.CastU8 => returnS! $ Result.Ok InstrRet.Ok
1042+
| Bytecode.CastU8 =>
1043+
letS!? integer_value := liftS! Interpreter.Lens.lens_state_self (
1044+
liftS! Interpreter.Lens.lens_self_stack $ Stack.Impl_Stack.pop_as IntegerValue.t
1045+
) in
1046+
letS!? integer_value := returnS! $ IntegerValue.cast_u8 integer_value in
1047+
doS!? liftS! Interpreter.Lens.lens_state_self (
1048+
liftS! Interpreter.Lens.lens_self_stack $ Stack.Impl_Stack.push $
1049+
ValueImpl.U8 integer_value
1050+
) in
1051+
returnS!? InstrRet.Ok
10431052

10441053
(*
10451054
Bytecode::CastU16 => {

CoqOfRust/move_sui/simulations/move_vm_types/values/values_impl.v

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,21 +1034,21 @@ Module IntegerValue.
10341034

10351035
Definition cast_u8 (self : IntegerValue.t) : PartialVMResult.t Z :=
10361036
match self with
1037-
| IntegerValue.U8 l => Result.Ok (l)
1037+
| IntegerValue.U8 l => Result.Ok l
10381038
| IntegerValue.U16 l => if l <=? 2^8 - 1
1039-
then Result.Ok (l)
1039+
then Result.Ok l
10401040
else Result.Err (PartialVMError.new StatusCode.ARITHMETIC_ERROR)
10411041
| IntegerValue.U32 l => if l <=? 2^8 - 1
1042-
then Result.Ok (l)
1042+
then Result.Ok l
10431043
else Result.Err (PartialVMError.new StatusCode.ARITHMETIC_ERROR)
10441044
| IntegerValue.U64 l => if l <=? 2^8 - 1
1045-
then Result.Ok (l)
1045+
then Result.Ok l
10461046
else Result.Err (PartialVMError.new StatusCode.ARITHMETIC_ERROR)
10471047
| IntegerValue.U128 l => if l <=? 2^8 - 1
1048-
then Result.Ok ( l)
1048+
then Result.Ok l
10491049
else Result.Err (PartialVMError.new StatusCode.ARITHMETIC_ERROR)
10501050
| IntegerValue.U256 l => if l <=? 2^8 - 1
1051-
then Result.Ok (l)
1051+
then Result.Ok l
10521052
else Result.Err (PartialVMError.new StatusCode.ARITHMETIC_ERROR)
10531053
end.
10541054

@@ -1614,3 +1614,32 @@ impl IntegerValue {
16141614
}
16151615
*)
16161616

1617+
(*
1618+
impl VMValueCast<IntegerValue> for Value {
1619+
fn cast(self) -> PartialVMResult<IntegerValue> {
1620+
match self.0 {
1621+
ValueImpl::U8(x) => Ok(IntegerValue::U8(x)),
1622+
ValueImpl::U16(x) => Ok(IntegerValue::U16(x)),
1623+
ValueImpl::U32(x) => Ok(IntegerValue::U32(x)),
1624+
ValueImpl::U64(x) => Ok(IntegerValue::U64(x)),
1625+
ValueImpl::U128(x) => Ok(IntegerValue::U128(x)),
1626+
ValueImpl::U256(x) => Ok(IntegerValue::U256(x)),
1627+
v => Err(PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR)
1628+
.with_message(format!("cannot cast {:?} to integer", v,))),
1629+
}
1630+
}
1631+
}
1632+
*)
1633+
Global Instance Impl_VMValueCast_IntegerValue_for_Value :
1634+
VMValueCast.Trait Value.t IntegerValue.t : Set := {
1635+
cast self :=
1636+
match self with
1637+
| ValueImpl.U8 x => return? $ IntegerValue.U8 x
1638+
| ValueImpl.U16 x => return? $ IntegerValue.U16 x
1639+
| ValueImpl.U32 x => return? $ IntegerValue.U32 x
1640+
| ValueImpl.U64 x => return? $ IntegerValue.U64 x
1641+
| ValueImpl.U128 x => return? $ IntegerValue.U128 x
1642+
| ValueImpl.U256 x => return? $ IntegerValue.U256 x
1643+
| _ => Result.Err $ PartialVMError.new StatusCode.INTERNAL_TYPE_ERROR
1644+
end;
1645+
}.

0 commit comments

Comments
 (0)