wasm_interp: neater creation of Value from unsigned integers

This commit is contained in:
Brian Carroll 2022-11-27 20:53:12 +00:00
parent f7dcd8f421
commit 78fbc75249
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0
2 changed files with 21 additions and 18 deletions

View file

@ -776,8 +776,7 @@ impl<'a> ExecutionState<'a> {
let arg2 = self.value_stack.pop_u32();
let arg1 = self.value_stack.pop_u32();
let result: u32 = arg1 / arg2;
self.value_stack
.push(Value::I32(i32::from_ne_bytes(result.to_ne_bytes())));
self.value_stack.push(Value::from(result));
}
I32REMS => {
let arg2 = self.value_stack.pop_i32();
@ -788,37 +787,32 @@ impl<'a> ExecutionState<'a> {
let arg2 = self.value_stack.pop_u32();
let arg1 = self.value_stack.pop_u32();
let result: u32 = arg1 % arg2;
self.value_stack
.push(Value::I32(i32::from_ne_bytes(result.to_ne_bytes())));
self.value_stack.push(Value::from(result));
}
I32AND => {
let arg2 = self.value_stack.pop_u32();
let arg1 = self.value_stack.pop_u32();
let result: u32 = arg1 & arg2;
self.value_stack
.push(Value::I32(i32::from_ne_bytes(result.to_ne_bytes())));
self.value_stack.push(Value::from(result));
}
I32OR => {
let arg2 = self.value_stack.pop_u32();
let arg1 = self.value_stack.pop_u32();
let result: u32 = arg1 | arg2;
self.value_stack
.push(Value::I32(i32::from_ne_bytes(result.to_ne_bytes())));
self.value_stack.push(Value::from(result));
}
I32XOR => {
let arg2 = self.value_stack.pop_u32();
let arg1 = self.value_stack.pop_u32();
let result: u32 = arg1 ^ arg2;
self.value_stack
.push(Value::I32(i32::from_ne_bytes(result.to_ne_bytes())));
self.value_stack.push(Value::from(result));
}
I32SHL => {
let arg2 = self.value_stack.pop_u32();
let arg1 = self.value_stack.pop_u32();
let k = arg2 % 32;
let result: u32 = arg1 << k;
self.value_stack
.push(Value::I32(i32::from_ne_bytes(result.to_ne_bytes())));
self.value_stack.push(Value::from(result));
}
I32SHRS => {
let arg2 = self.value_stack.pop_i32();
@ -831,22 +825,19 @@ impl<'a> ExecutionState<'a> {
let arg1 = self.value_stack.pop_u32();
let k = arg2 % 32;
let result: u32 = arg1 >> k;
self.value_stack
.push(Value::I32(i32::from_ne_bytes(result.to_ne_bytes())));
self.value_stack.push(Value::from(result));
}
I32ROTL => {
let arg2 = self.value_stack.pop_u32();
let arg1 = self.value_stack.pop_u32();
let result: u32 = arg1.rotate_left(arg2);
self.value_stack
.push(Value::I32(i32::from_ne_bytes(result.to_ne_bytes())));
self.value_stack.push(Value::from(result));
}
I32ROTR => {
let arg2 = self.value_stack.pop_u32();
let arg1 = self.value_stack.pop_u32();
let result: u32 = arg1.rotate_right(arg2);
self.value_stack
.push(Value::I32(i32::from_ne_bytes(result.to_ne_bytes())));
self.value_stack.push(Value::from(result));
}
I64CLZ => todo!("{:?} @ {:#x}", op_code, file_offset),

View file

@ -721,6 +721,18 @@ impl Value {
}
}
impl From<u32> for Value {
fn from(x: u32) -> Self {
Value::I32(i32::from_ne_bytes(x.to_ne_bytes()))
}
}
impl From<u64> for Value {
fn from(x: u64) -> Self {
Value::I64(i64::from_ne_bytes(x.to_ne_bytes()))
}
}
/// Wasm memory alignment for load/store instructions.
/// Rust representation matches Wasm encoding.
/// It's an error to specify alignment higher than the "natural" alignment of the instruction