Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
neri committed Feb 6, 2024
1 parent bf3cdce commit bd95bf5
Show file tree
Hide file tree
Showing 8 changed files with 435 additions and 29 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ default = []
# float = []

[dependencies]
bitflags = { version = "2.4.2", default-features = false }
num-traits = { version = "0.2.17", default-features = false }
smallvec = { version = "1.10.0", default-features = false }
bitflags = {version = "2.4.2", default-features = false}
libm = {}
num-traits = {version = "0.2.17", default-features = false}
smallvec = {version = "1.10.0", default-features = false}

[workspace]
members = [
"cli",
"cli",
]
2 changes: 0 additions & 2 deletions src/cg/intcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ pub enum WasmImInstruction {
FusedI64SetConst(LocalVarIndex, i64),

FusedI32AddI(i32),
FusedI32SubI(i32),
FusedI32AndI(u32),
FusedI32OrI(u32),
FusedI32XorI(u32),
Expand All @@ -240,7 +239,6 @@ pub enum WasmImInstruction {
FusedI32ShrUI(u32),

FusedI64AddI(i64),
FusedI64SubI(i64),
FusedI64AndI(u64),
FusedI64OrI(u64),
FusedI64XorI(u64),
Expand Down
10 changes: 0 additions & 10 deletions src/cg/intr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,11 +1211,6 @@ impl WasmInterpreter<'_> {
lhs.map_i32(|lhs| lhs.wrapping_add(val));
});
}
WasmImInstruction::FusedI32SubI(val) => {
Self::unary_op(code, &mut value_stack, |lhs| unsafe {
lhs.map_i32(|lhs| lhs.wrapping_sub(val));
});
}
WasmImInstruction::FusedI32AndI(val) => {
Self::unary_op(code, &mut value_stack, |lhs| unsafe {
lhs.map_u32(|lhs| lhs & val);
Expand Down Expand Up @@ -1252,11 +1247,6 @@ impl WasmInterpreter<'_> {
lhs.map_i64(|lhs| lhs.wrapping_add(val));
});
}
WasmImInstruction::FusedI64SubI(val) => {
Self::unary_op(code, &mut value_stack, |lhs| unsafe {
lhs.map_i64(|lhs| lhs.wrapping_sub(val));
});
}
WasmImInstruction::FusedI64AndI(val) => {
Self::unary_op(code, &mut value_stack, |lhs| unsafe {
lhs.map_u64(|lhs| lhs & val);
Expand Down
4 changes: 2 additions & 2 deletions src/cg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ impl WasmCodeBlock {
fused2!(int_codes, i, FusedI32AddI(*val));
}
(I32Const(val), I32Sub) => {
fused2!(int_codes, i, FusedI32SubI(*val));
fused2!(int_codes, i, FusedI32AddI(0i32.wrapping_sub(*val)));
}
(I32Const(val), I32And) => {
fused2!(int_codes, i, FusedI32AndI(*val as u32));
Expand Down Expand Up @@ -1449,7 +1449,7 @@ impl WasmCodeBlock {
fused2!(int_codes, i, FusedI64AddI(*val));
}
(I64Const(val), I64Sub) => {
fused2!(int_codes, i, FusedI64SubI(*val));
fused2!(int_codes, i, FusedI64AddI(0i64.wrapping_sub(*val)));
}
(I64Const(val), I64And) => {
fused2!(int_codes, i, FusedI64AndI(*val as u64));
Expand Down
106 changes: 101 additions & 5 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,54 @@ fn opr_test_i32() {
assert_eq!(memory.read_u32(0x74), 0xCCCCCCCC);
assert_eq!(memory.read_u64(0x78), 0xCCCC_CCCC_CCCC_CCCC);
}

for val in [
0i32,
1,
-1,
0x1234_5678,
0x5555_5555,
0xAAAA_AAAAu32 as i32,
0x0000_FFFF,
0xFFFF_0000u32 as i32,
] {
memory.fill(0xCC);
let result = instance
.function("test_fused_i32")
.unwrap()
.invoke(&[val.into()])
.unwrap()
.unwrap()
.get_i32()
.unwrap();
assert_eq!(result, 0x4C);

assert_eq!(memory.read_u64(0), 0xCCCC_CCCC_CCCC_CCCC);
assert_eq!(memory.read_u64(8), 0xCCCC_CCCC_CCCC_CCCC);

let valu = val as u32;
assert_eq!(memory.read_i32(0x10), 0x12345678);
assert_eq!(memory.read_i32(0x14), val.wrapping_add(1234));
assert_eq!(memory.read_i32(0x18), val.wrapping_sub(1234));
assert_eq!(memory.read_i32(0x1C), val.wrapping_add(5678));

assert_eq!(memory.read_u32(0x20), valu & 0x5555_5555);
assert_eq!(memory.read_u32(0x24), valu & 0xaaaa_aaaa);
assert_eq!(memory.read_u32(0x28), valu | 0x5555_5555);
assert_eq!(memory.read_u32(0x2C), valu | 0xaaaa_aaaa);
assert_eq!(memory.read_u32(0x30), valu ^ 0x5555_5555);
assert_eq!(memory.read_u32(0x34), valu ^ 0xaaaa_aaaa);

assert_eq!(memory.read_u32(0x38), valu.wrapping_shl(7));
assert_eq!(memory.read_u32(0x3C), valu.wrapping_shl(19));
assert_eq!(memory.read_i32(0x40), val.wrapping_shr(5));
assert_eq!(memory.read_i32(0x44), val.wrapping_shr(17));
assert_eq!(memory.read_u32(0x48), valu.wrapping_shr(3));
assert_eq!(memory.read_u32(0x4C), valu.wrapping_shr(13));

assert_eq!(memory.read_u64(0x50), 0xCCCC_CCCC_CCCC_CCCC);
assert_eq!(memory.read_u64(0x58), 0xCCCC_CCCC_CCCC_CCCC);
}
}

#[test]
Expand Down Expand Up @@ -1072,6 +1120,54 @@ fn opr_test_i64() {

assert_eq!(memory.read_u64(0xB0), 0xCCCC_CCCC_CCCC_CCCC);
}

for val in [
0i64,
1,
-1,
0x1234_5678_ABCD_DEF0,
0x5555_5555_5555_5555,
0xAAAA_AAAA_AAAA_AAAAu64 as i64,
0x0000_0000_FFFF_FFFF,
0xFFFF_FFFF_0000_0000u64 as i64,
] {
memory.fill(0xCC);
let result = instance
.function("test_fused_i64")
.unwrap()
.invoke(&[val.into()])
.unwrap()
.unwrap()
.get_i32()
.unwrap();
assert_eq!(result, 0x88);

assert_eq!(memory.read_u64(0), 0xCCCC_CCCC_CCCC_CCCC);
assert_eq!(memory.read_u64(8), 0xCCCC_CCCC_CCCC_CCCC);

let valu = val as u64;
assert_eq!(memory.read_i64(0x10), 0x12345678);
assert_eq!(memory.read_i64(0x18), val.wrapping_add(12345678));
assert_eq!(memory.read_i64(0x20), val.wrapping_sub(12345678));
assert_eq!(memory.read_i64(0x28), val.wrapping_add(987654321));

assert_eq!(memory.read_u64(0x30), valu & 0x5555_5555_5555_5555);
assert_eq!(memory.read_u64(0x38), valu & 0xaaaa_aaaa_aaaa_aaaa);
assert_eq!(memory.read_u64(0x40), valu | 0x5555_5555_5555_5555);
assert_eq!(memory.read_u64(0x48), valu | 0xaaaa_aaaa_aaaa_aaaa);
assert_eq!(memory.read_u64(0x50), valu ^ 0x5555_5555_5555_5555);
assert_eq!(memory.read_u64(0x58), valu ^ 0xaaaa_aaaa_aaaa_aaaa);

assert_eq!(memory.read_u64(0x60), valu.wrapping_shl(7));
assert_eq!(memory.read_u64(0x68), valu.wrapping_shl(19));
assert_eq!(memory.read_i64(0x70), val.wrapping_shr(5));
assert_eq!(memory.read_i64(0x78), val.wrapping_shr(17));
assert_eq!(memory.read_u64(0x80), valu.wrapping_shr(3));
assert_eq!(memory.read_u64(0x88), valu.wrapping_shr(13));

assert_eq!(memory.read_u64(0x90), 0xCCCC_CCCC_CCCC_CCCC);
assert_eq!(memory.read_u64(0x98), 0xCCCC_CCCC_CCCC_CCCC);
}
}

#[test]
Expand Down Expand Up @@ -1183,24 +1279,24 @@ fn call_indirect_test() {

// let memory = instance.memory(0).unwrap().try_borrow().unwrap();

for a1 in [0u32, 0x12345678, 0x55555555, 0xAAAAAAAA] {
for a1 in [0i32, 0x12345678, 0x55555555, 0xAAAAAAAAu32 as i32] {
for i in 1..=3 {
let base = [0, 123, 456, 789][i as usize];
let base = [0i32, 123, -456, 789][i as usize];
let result = instance
.function("call_indirect_test")
.unwrap()
.invoke(&[i.into(), a1.into()])
.invoke(&[(i as i32).into(), a1.into()])
.unwrap()
.unwrap()
.get_u32()
.get_i32()
.unwrap();
assert_eq!(result, a1.wrapping_add(base));
}

let e = instance
.function("call_indirect_test")
.unwrap()
.invoke(&[0.into(), a1.into()])
.invoke(&[4.into(), a1.into()])
.unwrap_err();
assert_matches!(e.kind(), WasmRuntimeErrorKind::TypeMismatch);

Expand Down
5 changes: 2 additions & 3 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,6 @@ impl WasmInstance {
}

#[inline]
#[allow(dead_code)]
pub fn function(&self, name: &str) -> Result<WasmRunnable, WasmRuntimeErrorKind> {
for export in &self.module.exports {
if let WasmExportDesc::Function(index) = export.desc {
Expand All @@ -640,7 +639,7 @@ impl WasmInstance {
.module
.functions
.get(index)
.map(|v| WasmRunnable::from_function(v, self))
.map(|v| WasmRunnable::new(v, self))
.ok_or(WasmRuntimeErrorKind::NoMethod);
}
}
Expand Down Expand Up @@ -2360,7 +2359,7 @@ pub struct WasmRunnable<'a> {

impl<'a> WasmRunnable<'a> {
#[inline]
const fn from_function(function: &'a WasmFunction, instance: &'a WasmInstance) -> Self {
const fn new(function: &'a WasmFunction, instance: &'a WasmInstance) -> Self {
Self { function, instance }
}
}
Expand Down
Binary file modified test/tester.wasm
Binary file not shown.
Loading

0 comments on commit bd95bf5

Please sign in to comment.