Skip to content

Commit 539167f

Browse files
chore: unify v128 representation
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 1a58b1c commit 539167f

8 files changed

Lines changed: 22 additions & 23 deletions

File tree

crates/cli/src/value_parse.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ fn parse_arg(index: usize, ty: WasmType, value: &str) -> Result<WasmValue> {
1717
WasmType::I64 => value.parse::<i64>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?,
1818
WasmType::F32 => value.parse::<f32>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?,
1919
WasmType::F64 => value.parse::<f64>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?,
20-
WasmType::V128 => value.parse::<i128>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?,
20+
WasmType::V128 => value
21+
.parse::<i128>()
22+
.map(|v| WasmValue::V128(v.to_le_bytes()))
23+
.map_err(|e| format_error(index, ty, value, e))?,
2124
WasmType::RefFunc | WasmType::RefExtern => {
2225
bail!(
2326
"unsupported CLI argument type at position {}: {}; use the embedding API for reference values",

crates/cli/src/wast_runner.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result<WasmValue> {
745745
F64(f) => WasmValue::F64(f64::from_bits(f.bits)),
746746
I32(i) => WasmValue::I32(i),
747747
I64(i) => WasmValue::I64(i),
748-
V128(i) => WasmValue::V128(i128::from_le_bytes(i.to_le_bytes())),
748+
V128(i) => WasmValue::V128(i.to_le_bytes()),
749749
RefExtern(v) => WasmValue::RefExtern(ExternRef::new(Some(v))),
750750
RefNull(t) => match t {
751751
wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Func } => {
@@ -760,7 +760,7 @@ fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result<WasmValue> {
760760
})
761761
}
762762

763-
fn wast_i128_to_i128(i: wast::core::V128Pattern) -> i128 {
763+
fn wast_v128_to_bytes(i: wast::core::V128Pattern) -> [u8; 16] {
764764
let res: Vec<u8> = match i {
765765
wast::core::V128Pattern::F32x4(f) => {
766766
f.iter().flat_map(|v| nanpattern2tinywasmvalue(*v).unwrap().as_f32().unwrap().to_le_bytes()).collect()
@@ -773,7 +773,7 @@ fn wast_i128_to_i128(i: wast::core::V128Pattern) -> i128 {
773773
wast::core::V128Pattern::I64x2(f) => f.iter().flat_map(|v| v.to_le_bytes()).collect(),
774774
wast::core::V128Pattern::I8x16(f) => f.iter().flat_map(|v| v.to_le_bytes()).collect(),
775775
};
776-
i128::from_le_bytes(res.try_into().unwrap())
776+
res.try_into().unwrap()
777777
}
778778

779779
fn wastret2tinywasmvalues(ret: wast::WastRet) -> Result<Vec<WasmValue>> {
@@ -793,7 +793,7 @@ fn wastretcore2tinywasmvalue(ret: wast::core::WastRetCore) -> Result<WasmValue>
793793
F64(f) => nanpattern2tinywasmvalue(f)?,
794794
I32(i) => WasmValue::I32(i),
795795
I64(i) => WasmValue::I64(i),
796-
V128(i) => WasmValue::V128(wast_i128_to_i128(i)),
796+
V128(i) => WasmValue::V128(wast_v128_to_bytes(i)),
797797
RefNull(t) => match t {
798798
Some(wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Func }) => {
799799
WasmValue::RefFunc(FuncRef::null())

crates/parser/src/conversion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[C
271271
wasmparser::Operator::I64Const { value } => ConstInstruction::I64Const(*value),
272272
wasmparser::Operator::F32Const { value } => ConstInstruction::F32Const(f32::from_bits(value.bits())),
273273
wasmparser::Operator::F64Const { value } => ConstInstruction::F64Const(f64::from_bits(value.bits())),
274-
wasmparser::Operator::V128Const { value } => ConstInstruction::V128Const(value.i128()),
274+
wasmparser::Operator::V128Const { value } => ConstInstruction::V128Const(*value.bytes()),
275275
wasmparser::Operator::GlobalGet { global_index } => ConstInstruction::GlobalGet(*global_index),
276276
wasmparser::Operator::I32Add => ConstInstruction::I32Add,
277277
wasmparser::Operator::I32Sub => ConstInstruction::I32Sub,

crates/parser/src/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,11 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild
400400
}
401401

402402
fn visit_f32_const(&mut self, val: wasmparser::Ieee32) -> Self::Output {
403-
self.instructions.push(Instruction::Const32(i32::from_ne_bytes(val.bits().to_ne_bytes())));
403+
self.instructions.push(Instruction::Const32(val.bits() as i32));
404404
}
405405

406406
fn visit_f64_const(&mut self, val: wasmparser::Ieee64) -> Self::Output {
407-
self.instructions.push(Instruction::Const64(i64::from_ne_bytes(val.bits().to_ne_bytes())));
407+
self.instructions.push(Instruction::Const64(val.bits() as i64));
408408
}
409409

410410
fn visit_table_copy(&mut self, dst_table: u32, src_table: u32) -> Self::Output {

crates/tinywasm/src/interpreter/stack/value_stack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl ValueStack {
261261
WasmType::F64 => WasmValue::F64(f64::stack_pop(self)),
262262
WasmType::RefExtern => WasmValue::RefExtern(ExternRef::from_raw(ValueRef::stack_pop(self).raw())),
263263
WasmType::RefFunc => WasmValue::RefFunc(FuncRef::from_raw(ValueRef::stack_pop(self).raw())),
264-
WasmType::V128 => WasmValue::V128(Value128::stack_pop(self).into()),
264+
WasmType::V128 => WasmValue::V128(Value128::stack_pop(self).0),
265265
}
266266
}
267267

crates/tinywasm/src/interpreter/values.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl TinyWasmValue {
105105
(Self::Value64(v), WasmType::F64) => Some(WasmValue::F64(f64::from_bits(v))),
106106
(Self::ValueRef(v), WasmType::RefExtern) => Some(WasmValue::RefExtern(ExternRef::from_raw(v.raw()))),
107107
(Self::ValueRef(v), WasmType::RefFunc) => Some(WasmValue::RefFunc(FuncRef::from_raw(v.raw()))),
108-
(Self::Value128(v), WasmType::V128) => Some(WasmValue::V128((v).into())),
108+
(Self::Value128(v), WasmType::V128) => Some(WasmValue::V128(v.0)),
109109
(_, WasmType::I32 | WasmType::F32) => None,
110110
(_, WasmType::I64 | WasmType::F64) => None,
111111
(_, WasmType::RefExtern | WasmType::RefFunc) => None,
@@ -134,8 +134,8 @@ impl From<WasmValue> for TinyWasmValue {
134134
}
135135
}
136136

137-
impl From<i128> for TinyWasmValue {
138-
fn from(value: i128) -> Self {
137+
impl From<[u8; 16]> for TinyWasmValue {
138+
fn from(value: [u8; 16]) -> Self {
139139
Self::Value128(Value128::from(value))
140140
}
141141
}

crates/types/src/instructions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub enum ConstInstruction {
3636
I64Const(i64),
3737
F32Const(f32),
3838
F64Const(f64),
39-
V128Const(i128),
39+
V128Const([u8; 16]),
4040
GlobalGet(GlobalAddr),
4141
RefFunc(Option<FuncAddr>),
4242
RefExtern(Option<ExternAddr>),

crates/types/src/value.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum WasmValue {
1717
/// A 64-bit float.
1818
F64(f64),
1919
// /// A 128-bit vector
20-
V128(i128),
20+
V128([u8; 16]),
2121
RefExtern(ExternRef),
2222
RefFunc(FuncRef),
2323
}
@@ -176,7 +176,7 @@ impl WasmValue {
176176
WasmType::I64 => Self::I64(0),
177177
WasmType::F32 => Self::F32(0.0),
178178
WasmType::F64 => Self::F64(0.0),
179-
WasmType::V128 => Self::V128(0),
179+
WasmType::V128 => Self::V128([0; 16]),
180180
WasmType::RefFunc => Self::RefFunc(FuncRef::null()),
181181
WasmType::RefExtern => Self::RefExtern(ExternRef::null()),
182182
}
@@ -188,11 +188,7 @@ impl WasmValue {
188188
match (self, other) {
189189
(Self::I32(a), Self::I32(b)) => a == b,
190190
(Self::I64(a), Self::I64(b)) => a == b,
191-
(Self::V128(a), Self::V128(b)) => {
192-
let a_bytes = a.to_le_bytes();
193-
let b_bytes = b.to_le_bytes();
194-
a_bytes == b_bytes || Self::v128_nan_eq(a_bytes, b_bytes)
195-
}
191+
(Self::V128(a), Self::V128(b)) => a == b || Self::v128_nan_eq(*a, *b),
196192
(Self::RefExtern(addr), Self::RefExtern(addr2)) => addr == addr2,
197193
(Self::RefFunc(addr), Self::RefFunc(addr2)) => addr == addr2,
198194
(Self::F32(a), Self::F32(b)) => {
@@ -293,8 +289,8 @@ impl WasmValue {
293289
}
294290
}
295291

296-
/// Return the `i128` from a `WasmValue`, if it is a `V128`.
297-
pub const fn as_v128(&self) -> Option<i128> {
292+
/// Return the raw little-endian bytes from a `WasmValue`, if it is a `V128`.
293+
pub const fn as_v128(&self) -> Option<[u8; 16]> {
298294
match self {
299295
Self::V128(i) => Some(*i),
300296
_ => None,
@@ -394,4 +390,4 @@ macro_rules! impl_conversion_for_wasmvalue {
394390
}
395391
}
396392

397-
impl_conversion_for_wasmvalue! { i32 => I32, i64 => I64, f32 => F32, f64 => F64, i128 => V128, ExternRef => RefExtern, FuncRef => RefFunc }
393+
impl_conversion_for_wasmvalue! { i32 => I32, i64 => I64, f32 => F32, f64 => F64, [u8; 16] => V128, ExternRef => RefExtern, FuncRef => RefFunc }

0 commit comments

Comments
 (0)