Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit e883003

Browse files
some new instructuons
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 5e573f0 commit e883003

File tree

4 files changed

+45
-38
lines changed

4 files changed

+45
-38
lines changed

crates/benchmarks/benches/selfhosted.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ fn criterion_benchmark(c: &mut Criterion) {
6161
{
6262
let twasm = util::wasm_to_twasm(TINYWASM);
6363
let mut group = c.benchmark_group("selfhosted");
64-
group.bench_function("native", |b| b.iter(run_native));
64+
// group.bench_function("native", |b| b.iter(run_native));
6565
group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm)));
66-
group.bench_function("wasmi", |b| b.iter(|| run_wasmi(TINYWASM)));
67-
group.bench_function("wasmer", |b| b.iter(|| run_wasmer(TINYWASM)));
66+
// group.bench_function("wasmi", |b| b.iter(|| run_wasmi(TINYWASM)));
67+
// group.bench_function("wasmer", |b| b.iter(|| run_wasmer(TINYWASM)));
6868
}
6969
}
7070

crates/parser/src/visit.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
325325
fn visit_local_get(&mut self, idx: u32) -> Self::Output {
326326
if let Some(instruction) = self.instructions.last_mut() {
327327
match instruction {
328-
// Instruction::LocalGet(a) => *instruction = Instruction::LocalGet2(*a, idx),
329-
// Instruction::LocalGet2(a, b) => *instruction = Instruction::LocalGet3(*a, *b, idx),
330-
// Instruction::LocalGet3(a, b, c) => *instruction = Instruction::LocalGet4(*a, *b, *c, idx),
331-
// Instruction::LocalTee(a) => *instruction = Instruction::LocalTeeGet(*a, idx),
328+
Instruction::LocalGet(a) => *instruction = Instruction::LocalGet2(*a, idx),
329+
Instruction::LocalGet2(a, b) => *instruction = Instruction::LocalGet3(*a, *b, idx),
330+
Instruction::LocalGet3(a, b, c) => *instruction = Instruction::LocalGet4(*a, *b, *c, idx),
331+
Instruction::LocalTee(a) => *instruction = Instruction::LocalTeeGet(*a, idx),
332332
_ => return self.visit(Instruction::LocalGet(idx)),
333333
};
334334
Ok(())
@@ -338,16 +338,15 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
338338
}
339339

340340
fn visit_local_set(&mut self, idx: u32) -> Self::Output {
341-
// LocalGetSet
342-
if let Some(instruction) = self.instructions.last_mut() {
343-
match instruction {
344-
// Instruction::LocalGet(a) => *instruction = Instruction::LocalGetSet(*a, idx),
345-
_ => return self.visit(Instruction::LocalSet(idx)),
346-
};
347-
Ok(())
348-
} else {
349-
self.visit(Instruction::LocalSet(idx))
341+
if self.instructions.len() < 1 {
342+
return self.visit(Instruction::I64Rotl);
350343
}
344+
345+
// LocalGetSet
346+
match self.instructions[self.instructions.len() - 1..] {
347+
// Instruction::LocalGet(a) => *instruction = Instruction::LocalGetSet(*a, idx),
348+
_ => return self.visit(Instruction::LocalSet(idx)),
349+
};
351350
}
352351

353352
fn visit_local_tee(&mut self, idx: u32) -> Self::Output {
@@ -360,11 +359,11 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
360359
}
361360

362361
match self.instructions[self.instructions.len() - 2..] {
363-
// [Instruction::I64Xor, Instruction::I64Const(a)] => {
364-
// self.instructions.pop();
365-
// self.instructions.pop();
366-
// self.visit(Instruction::I64XorConstRotl(a))
367-
// }
362+
[Instruction::I64Xor, Instruction::I64Const(a)] => {
363+
self.instructions.pop();
364+
self.instructions.pop();
365+
self.visit(Instruction::I64XorConstRotl(a))
366+
}
368367
_ => self.visit(Instruction::I64Rotl),
369368
}
370369
}

crates/tinywasm/src/runtime/interpreter/mod.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -680,19 +680,22 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
680680

681681
// custom instructions
682682
LocalGet2(a, b) => {
683-
stack.values.push(cf.get_local(a as usize));
684-
stack.values.push(cf.get_local(b as usize));
683+
stack.values.extend_from_slice(&[cf.get_local(a as usize), cf.get_local(b as usize)]);
685684
}
686685
LocalGet3(a, b, c) => {
687-
stack.values.push(cf.get_local(a as usize));
688-
stack.values.push(cf.get_local(b as usize));
689-
stack.values.push(cf.get_local(c as usize));
686+
stack.values.extend_from_slice(&[
687+
cf.get_local(a as usize),
688+
cf.get_local(b as usize),
689+
cf.get_local(c as usize),
690+
]);
690691
}
691692
LocalGet4(a, b, c, d) => {
692-
stack.values.push(cf.get_local(a as usize));
693-
stack.values.push(cf.get_local(b as usize));
694-
stack.values.push(cf.get_local(c as usize));
695-
stack.values.push(cf.get_local(d as usize));
693+
stack.values.extend_from_slice(&[
694+
cf.get_local(a as usize),
695+
cf.get_local(b as usize),
696+
cf.get_local(c as usize),
697+
cf.get_local(d as usize),
698+
]);
696699
}
697700
LocalTeeGet(a, b) => {
698701
let last =
@@ -701,10 +704,14 @@ fn exec_one(cf: &mut CallFrame, stack: &mut Stack, store: &mut Store, module: &M
701704
stack.values.push(cf.get_local(b as usize));
702705
}
703706

704-
// LocalTeeGet
705-
// LocalGetSet
706-
// I64XorConstRotl
707-
// I32LocalGetConstAdd
707+
// I64Xor + I64Const + I64RotL
708+
I64XorConstRotl(rotate_by) => {
709+
let val = stack.values.pop_t::<i64>()?;
710+
let mask = stack.values.pop_t::<i64>()?;
711+
let res = val ^ mask;
712+
stack.values.push(res.rotate_left(rotate_by as u32).into());
713+
}
714+
708715
i => {
709716
cold();
710717
log::error!("unimplemented instruction: {:?}", i);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ impl ValueStack {
2525

2626
#[inline]
2727
pub(crate) fn extend_from_typed(&mut self, values: &[WasmValue]) {
28-
if values.is_empty() {
29-
return;
30-
}
31-
3228
self.stack.extend(values.iter().map(|v| RawWasmValue::from(*v)));
3329
}
3430

31+
#[inline]
32+
pub(crate) fn extend_from_slice(&mut self, values: &[RawWasmValue]) {
33+
self.stack.extend_from_slice(values);
34+
}
35+
3536
#[inline]
3637
pub(crate) fn len(&self) -> usize {
3738
self.stack.len()

0 commit comments

Comments
 (0)