Skip to content

Commit 2fb5c4a

Browse files
chore: simplify operand structs
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent ff34ee1 commit 2fb5c4a

7 files changed

Lines changed: 179 additions & 227 deletions

File tree

‎Cargo.lock‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/parser/src/optimize/rewrite.rs‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use alloc::vec::Vec;
66
use core::ops::{Deref, DerefMut};
77
use tinywasm_types::{
88
BinOp, BinOp128, CmpOp, CompactMemoryArg, GlobalConst32, GlobalConst64, GlobalUpdate, GlobalV128, I32LocalArg,
9-
I64Local, Instruction, LocalConst64, LocalConstCmp, LocalConstSet32, LocalConstSet64, LocalConstSetV128,
9+
I64Operand, Instruction, LocalConst64, LocalConstCmp, LocalConstSet32, LocalConstSet64, LocalConstSetV128,
1010
LocalLocalCmp, LocalTripleArg, LocalU32, LocalUpdate, LocalUpdateCmp, LocalV128, MemoryArg, MemoryFillConstOp,
11-
MemoryLocalArg, OperandIdx, PackedOp, StackConst32, StackConst64, TargetLocal, TargetLocalArg, V128LocalArg,
12-
V128Operand, ValueCounts,
11+
MemoryLocalArg, OperandIdx, PackedOp, StackConst32, StackConst64, TargetLocal, TargetLocalArg, V128Operand,
12+
ValueCounts,
1313
};
1414

1515
pub(crate) struct OptimizeResult {
@@ -725,8 +725,7 @@ fn rewrite_local_set64(
725725
}
726726
Instruction::LocalGet64(src) => replace!(output, *read, 1 => Instruction::LocalCopy64(src, dst)),
727727
Instruction::Const64(index) => {
728-
let index = operand!(data, I64Local { value: data.operand(index).value, local: dst })?;
729-
replace!(output, *read, 1 => Instruction::SetLocalConst64(index));
728+
replace!(output, *read, 1 => Instruction::SetLocalConst64(PackedOp::new(dst, index)));
730729
}
731730
Instruction::BinOpLocalLocal64(op, left, right) => {
732731
let index = operand!(data, LocalTripleArg { left, right, dst })?;
@@ -735,8 +734,8 @@ fn rewrite_local_set64(
735734
instruction if let Some((op, src, value)) = local_const64(data, instruction) => {
736735
if src == dst && matches!(op, BinOp::IAdd | BinOp::ISub) {
737736
let delta = if op == BinOp::IAdd { value } else { value.wrapping_neg() };
738-
let index = operand!(data, I64Local { value: delta, local: dst })?;
739-
replace!(output, *read, 1 => Instruction::IncLocal64(index));
737+
let index = operand!(data, I64Operand { value: delta })?;
738+
replace!(output, *read, 1 => Instruction::IncLocal64(PackedOp::new(dst, index)));
740739
} else {
741740
let replacement = local_const_set!(data, 64, op, src, dst, value, false);
742741
replace!(output, *read, 1 => replacement);
@@ -776,7 +775,7 @@ fn rewrite_local_set128(
776775
}
777776
Instruction::LocalGet128(src) => replace!(output, *read, 1 => Instruction::LocalCopy128(src, dst)),
778777
Instruction::Const128(value) => {
779-
replace!(output, *read, 1 => Instruction::SetLocalConst128(V128LocalArg { value, local: dst }))
778+
replace!(output, *read, 1 => Instruction::SetLocalConst128(PackedOp::new(dst, value)))
780779
}
781780
Instruction::BinOpLocalLocal128(op, left, right) => {
782781
let index = operand!(data, LocalTripleArg { left, right, dst })?;
@@ -867,13 +866,11 @@ fn rewrite_local_tee64(
867866
if *read >= output.block_start + 2 {
868867
match [output[*read - 2], output[*read - 1]] {
869868
[Instruction::Const64(value), Instruction::I64And] => {
870-
let index = operand!(data, I64Local { value: data.operand(value).value, local: dst })?;
871-
replace!(output, *read, 2 => Instruction::AndConstTee64(index));
869+
replace!(output, *read, 2 => Instruction::AndConstTee64(PackedOp::new(dst, value)));
872870
return Ok(());
873871
}
874872
[Instruction::Const64(value), Instruction::I64Sub] => {
875-
let index = operand!(data, I64Local { value: data.operand(value).value, local: dst })?;
876-
replace!(output, *read, 2 => Instruction::SubConstTee64(index));
873+
replace!(output, *read, 2 => Instruction::SubConstTee64(PackedOp::new(dst, value)));
877874
return Ok(());
878875
}
879876
_ => {}

‎crates/parser/src/optimize/targets.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(super) fn set_rewrite_target(
5555
JumpCmpStackLocal32(packed) | JumpCmpStackLocal64(packed) => {
5656
*packed = push_packed_target_copy(data, *packed, target)?
5757
}
58-
BrOnCast(index) => *index = push_target_copy(data, *index, target)?,
58+
BrOnCast(index) | BrOnCastFail(index) => *index = push_target_copy(data, *index, target)?,
5959
JumpCmpStackConst32(packed) => *packed = push_packed_target_copy(data, *packed, target)?,
6060
JumpCmpStackConst64(packed) => *packed = push_packed_target_copy(data, *packed, target)?,
6161
BinOpLocalConstJump32(packed) | BinOpStackConstTeeLocalJump32(packed) => {
@@ -118,7 +118,7 @@ fn instruction_target(data: &FunctionDataBuilder, instruction: Instruction) -> O
118118
arg.target_ip
119119
}
120120
JumpCmpStackLocal32(packed) | JumpCmpStackLocal64(packed) => data.operand(packed.index).target,
121-
BrOnCast(index) => data.operand(index).target,
121+
BrOnCast(index) | BrOnCastFail(index) => data.operand(index).target,
122122
JumpCmpStackConst32(packed) => data.operand(packed.index).target,
123123
JumpCmpStackConst64(packed) => data.operand(packed.index).target,
124124
BinOpLocalConstJump32(packed) | BinOpStackConstTeeLocalJump32(packed) => data.operand(packed.index).target,
@@ -148,7 +148,7 @@ fn set_target(instruction: &mut Instruction, data: &mut FunctionDataBuilder, tar
148148
arg.target_ip = target
149149
}
150150
JumpCmpStackLocal32(packed) | JumpCmpStackLocal64(packed) => set_packed_operand_target(data, *packed, target),
151-
BrOnCast(index) => set_operand_target(data, *index, target),
151+
BrOnCast(index) | BrOnCastFail(index) => set_operand_target(data, *index, target),
152152
JumpCmpStackConst32(packed) => set_packed_operand_target(data, *packed, target),
153153
JumpCmpStackConst64(packed) => set_packed_operand_target(data, *packed, target),
154154
BinOpLocalConstJump32(packed) | BinOpStackConstTeeLocalJump32(packed) => {

‎crates/parser/src/visit.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,12 +1334,12 @@ impl FunctionBuilder<'_> {
13341334
self.pop_expect(ValueLane::S32)?;
13351335
let target = convert_heap_type(target.heap_type(), target.is_nullable())?;
13361336
let conditional_ip = self.instructions.len();
1337-
let operand = self.data.push_target_operand(CastBranch {
1338-
target: 0,
1339-
ref_type_bits: target.to_bits(),
1340-
branch_on_fail: u8::from(branch_on_fail),
1341-
})?;
1342-
self.instructions.push(Instruction::BrOnCast(operand));
1337+
let operand = self.data.push_target_operand(CastBranch { target: 0, ref_type_bits: target.to_bits() })?;
1338+
self.instructions.push(if branch_on_fail {
1339+
Instruction::BrOnCastFail(operand)
1340+
} else {
1341+
Instruction::BrOnCast(operand)
1342+
});
13431343
self.push_sizes(&[ValueLane::S32])?;
13441344
self.emit_dropkeep_to_label(relative_depth)?;
13451345
self.emit_branch_jump_or_return(relative_depth)?;
@@ -1503,7 +1503,7 @@ impl FunctionBuilder<'_> {
15031503
| Instruction::JumpIfRefNonNull(ip) => {
15041504
*ip = target as u32;
15051505
}
1506-
Instruction::BrOnCast(index) => {
1506+
Instruction::BrOnCast(index) | Instruction::BrOnCastFail(index) => {
15071507
let mut operand = self.data.operand(*index);
15081508
operand.target = target as u32;
15091509
self.data.set_operand(*index, operand);

‎crates/tinywasm/src/interpreter/executor.rs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
228228
if !is_non_null { ValueRef::stack_pop(&mut self.store.value_stack); }
229229
is_non_null
230230
}) { return Ok(None) },
231-
BrOnCast(idx) => { let v = idx.get(&self.func.data); if self.exec_ref_matches(v.ref_type()) == (v.branch_on_fail != 0) { self.cf.instr_ptr = v.target as usize; return Ok(None); } },
231+
BrOnCast(idx) => { let v = idx.get(&self.func.data); if !self.exec_ref_matches(v.ref_type()) { self.cf.instr_ptr = v.target as usize; return Ok(None); } },
232+
BrOnCastFail(idx) => { let v = idx.get(&self.func.data); if self.exec_ref_matches(v.ref_type()) { self.cf.instr_ptr = v.target as usize; return Ok(None); } },
232233
JumpIfLocalZero32(arg) => if Self::exec_jump_if(&mut self.cf, arg.target_ip, |cf| Value32::local_get(&self.store.value_stack, cf, arg.local) == 0) { return Ok(None) },
233234
JumpIfLocalNonZero32(arg) => if Self::exec_jump_if(&mut self.cf, arg.target_ip, |cf| Value32::local_get(&self.store.value_stack, cf, arg.local) != 0) { return Ok(None) },
234235
JumpIfLocalZero64(arg) => if Self::exec_jump_if(&mut self.cf, arg.target_ip, |cf| Value64::local_get(&self.store.value_stack, cf, arg.local) == 0) { return Ok(None) },
@@ -286,7 +287,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
286287
AddConst32(c) => exec_op!(unary i32 => i32, |v| v.wrapping_add(*c)),
287288
AddConst64(idx) => { let c = idx.get(&self.func.data).value; exec_op!(unary i64 => i64, |v| v.wrapping_add(c)); },
288289
IncLocal32(arg) => i32::local_update(&mut self.store.value_stack, &self.cf, arg.local, |v| v.wrapping_add(arg.value)),
289-
IncLocal64(idx) => { let v = idx.get(&self.func.data); i64::local_update(&mut self.store.value_stack, &self.cf, v.local, |n| n.wrapping_add(v.value)); },
290+
IncLocal64(packed) => { let v = packed.index.get(&self.func.data).value; i64::local_update(&mut self.store.value_stack, &self.cf, packed.op, |n| n.wrapping_add(v)); },
290291
I32Add3 => exec_op!(ternary i32 => i32, |a, b, c| a.wrapping_add(b).wrapping_add(c)),
291292
I64Add3 => exec_op!(ternary i64 => i64, |a, b, c| a.wrapping_add(b).wrapping_add(c)),
292293
MulAccLocal32(acc) => exec_op!(binop_acc_local i32, acc, |a: i32, b| a.wrapping_mul(b), |a: i32, b| a.wrapping_add(b)),
@@ -328,8 +329,8 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
328329
BinOpStackLocalTee32(op, local, dst) => exec_op!(binop_stack_local_tee Value32, exec_binop_32, op, local, dst),
329330
BinOpStackLocal128(op, local) => exec_op!(binop_stack_local Value128, exec_binop_128, op, local),
330331
SetLocalConst32(arg) => i32::local_set(&mut self.store.value_stack, &self.cf, arg.local, arg.value),
331-
SetLocalConst64(idx) => { let v = idx.get(&self.func.data); i64::local_set(&mut self.store.value_stack, &self.cf, v.local, v.value); },
332-
SetLocalConst128(arg) => Value128::local_set(&mut self.store.value_stack, &self.cf, arg.local, Value128(arg.value.get(&self.func.data).value)),
332+
SetLocalConst64(packed) => { let v = packed.index.get(&self.func.data).value; i64::local_set(&mut self.store.value_stack, &self.cf, packed.op, v); },
333+
SetLocalConst128(packed) => Value128::local_set(&mut self.store.value_stack, &self.cf, packed.op, Value128(packed.index.get(&self.func.data).value)),
333334
IncMemoryLocal32(arg) => self.exec_inc_memory_local::<i32, 4>(arg.memory_arg_idx.get(&self.func.data), arg.local1, |v| v.wrapping_add(1))?,
334335
IncMemoryLocal64(arg) => self.exec_inc_memory_local::<i64, 8>(arg.memory_arg_idx.get(&self.func.data), arg.local1, |v| v.wrapping_add(1))?,
335336
StoreLocalLocal32(arg) => self.exec_store_local_local::<u32, 4>(arg.memory_arg_idx.get(&self.func.data), arg.local1, arg.local2)?,
@@ -355,8 +356,8 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> {
355356
LoadLocalSet128(arg) => self.exec_load_local::<Value128, 16, _, true, false>(arg.memory_arg_idx.get(&self.func.data), arg.local1, arg.local2, |v| v)?,
356357
AndConstTee32(arg) => { exec_op!(unary i32 => i32, |v| v & arg.value); let value = i32::stack_peek(&self.store.value_stack); i32::local_set(&mut self.store.value_stack, &self.cf, arg.local, value); },
357358
SubConstTee32(arg) => { exec_op!(unary i32 => i32, |v| v.wrapping_sub(arg.value)); let value = i32::stack_peek(&self.store.value_stack); i32::local_set(&mut self.store.value_stack, &self.cf, arg.local, value); },
358-
AndConstTee64(idx) => { let v = idx.get(&self.func.data); exec_op!(unary i64 => i64, |n| n & v.value); let value = i64::stack_peek(&self.store.value_stack); i64::local_set(&mut self.store.value_stack, &self.cf, v.local, value); },
359-
SubConstTee64(idx) => { let v = idx.get(&self.func.data); exec_op!(unary i64 => i64, |n| n.wrapping_sub(v.value)); let value = i64::stack_peek(&self.store.value_stack); i64::local_set(&mut self.store.value_stack, &self.cf, v.local, value); },
359+
AndConstTee64(packed) => { let v = packed.index.get(&self.func.data).value; exec_op!(unary i64 => i64, |n| n & v); let value = i64::stack_peek(&self.store.value_stack); i64::local_set(&mut self.store.value_stack, &self.cf, packed.op, value); },
360+
SubConstTee64(packed) => { let v = packed.index.get(&self.func.data).value; exec_op!(unary i64 => i64, |n| n.wrapping_sub(v)); let value = i64::stack_peek(&self.store.value_stack); i64::local_set(&mut self.store.value_stack, &self.cf, packed.op, value); },
360361
LocalTee32(local_index) => exec_op!(local_tee Value32, local_index),
361362
LocalTee64(local_index) => exec_op!(local_tee Value64, local_index),
362363
LocalTee128(local_index) => exec_op!(local_tee Value128, local_index),

0 commit comments

Comments
 (0)