Tests for different bitwidth integers

This commit is contained in:
Brian Carroll 2021-09-13 17:01:06 +02:00
parent 8407e8ad4c
commit 408c31ebcc
3 changed files with 210 additions and 110 deletions

View file

@ -103,7 +103,6 @@ impl WasmLayout {
// LocalOnly(F32, 2) => Ok(), // convert F16 to F32 (lowlevel function? Wasm-only?)
// StackMemory(size) => Ok(), // would this be some kind of memcpy in the IR?
HeapMemory => {
if PTR_TYPE == I64 {
Ok(I64Load(ALIGN_8, offset))
@ -134,7 +133,6 @@ impl WasmLayout {
// LocalOnly(F32, 2) => Ok(), // convert F32 to F16 (lowlevel function? Wasm-only?)
// StackMemory(size) => Ok(), // would this be some kind of memcpy in the IR?
HeapMemory => {
if PTR_TYPE == I64 {
Ok(I64Store(ALIGN_8, offset))
@ -498,20 +496,27 @@ impl<'a> WasmBackend<'a> {
Ok(())
}
Literal::Int(x) => {
match layout {
Layout::Builtin(Builtin::Int32) => {
self.instructions.push(I32Const(*x as i32));
}
Layout::Builtin(Builtin::Int64) => {
self.instructions.push(I64Const(*x as i64));
}
let instruction = match layout {
Layout::Builtin(Builtin::Int64) => I64Const(*x as i64),
Layout::Builtin(
Builtin::Int32
| Builtin::Int16
| Builtin::Int8
| Builtin::Int1
| Builtin::Usize,
) => I32Const(*x as i32),
x => panic!("loading literal, {:?}, is not yet implemented", x),
}
};
self.instructions.push(instruction);
Ok(())
}
Literal::Float(x) => {
let val: f64 = *x;
self.instructions.push(F64Const(val.to_bits()));
let instruction = match layout {
Layout::Builtin(Builtin::Float64) => F64Const((*x as f64).to_bits()),
Layout::Builtin(Builtin::Float32) => F32Const((*x as f32).to_bits()),
x => panic!("loading literal, {:?}, is not yet implemented", x),
};
self.instructions.push(instruction);
Ok(())
}
x => Err(format!("loading literal, {:?}, is not yet implemented", x)),

View file

@ -36,6 +36,101 @@ mod wasm_num {
assert_evals_to!(&format!("{:0.1}", f64::MAX), f64::MAX, f64);
}
#[test]
fn i8_add_wrap() {
assert_evals_to!(
indoc!(
r#"
x : I8
x = 0x7f + 0x7f
x
"#
),
-2,
i8
);
}
#[test]
fn i16_add_wrap() {
assert_evals_to!(
indoc!(
r#"
x : I16
x = 0x7fff + 0x7fff
x
"#
),
-2,
i16
);
}
#[test]
fn i32_add_wrap() {
assert_evals_to!(
indoc!(
r#"
x : I32
x = 0x7fffffff + 0x7fffffff
x
"#
),
-2,
i32
);
}
#[test]
fn u8_add_wrap() {
assert_evals_to!(
indoc!(
r#"
x : U8
x = 0xff + 0xff
x
"#
),
0xfe,
u8
);
}
#[test]
fn u16_add_wrap() {
assert_evals_to!(
indoc!(
r#"
x : U16
x = 0xffff + 0xffff
x
"#
),
0xfffe,
u16
);
}
#[test]
fn u32_add_wrap() {
assert_evals_to!(
indoc!(
r#"
x : U32
x = 0xffffffff + 0xffffffff
x
"#
),
0xfffffffe,
u32
);
}
#[test]
fn gen_add_i64() {
assert_evals_to!(
@ -154,44 +249,44 @@ mod wasm_num {
);
}
// #[test]
// fn gen_add_f64() {
// assert_evals_to!(
// indoc!(
// r#"
// 1.1 + 2.4 + 3
// "#
// ),
// 6.5,
// f64
// );
// }
#[test]
fn gen_add_f64() {
assert_evals_to!(
indoc!(
r#"
1.1 + 2.4 + 3
"#
),
6.5,
f64
);
}
// #[test]
// fn gen_sub_i64() {
// assert_evals_to!(
// indoc!(
// r#"
// 1 - 2 - 3
// "#
// ),
// -4,
// i64
// );
// }
#[test]
fn gen_sub_i64() {
assert_evals_to!(
indoc!(
r#"
1 - 2 - 3
"#
),
-4,
i64
);
}
// #[test]
// fn gen_mul_i64() {
// assert_evals_to!(
// indoc!(
// r#"
// 2 * 4 * 6
// "#
// ),
// 48,
// i64
// );
// }
#[test]
fn gen_mul_i64() {
assert_evals_to!(
indoc!(
r#"
2 * 4 * 6
"#
),
48,
i64
);
}
#[test]
fn i64_force_stack() {
@ -476,18 +571,18 @@ mod wasm_num {
// );
// }
// #[test]
// fn gen_sub_f64() {
// assert_evals_to!(
// indoc!(
// r#"
// 1.5 - 2.4 - 3
// "#
// ),
// -3.9,
// f64
// );
// }
#[test]
fn gen_sub_f64() {
assert_evals_to!(
indoc!(
r#"
1.5 - 2.4 - 3
"#
),
-3.9,
f64
);
}
// #[test]
// fn gen_div_i64() {
@ -685,31 +780,31 @@ mod wasm_num {
// assert_evals_to!("0.0 >= 0.0", true, bool);
// }
// #[test]
// fn gen_order_of_arithmetic_ops() {
// assert_evals_to!(
// indoc!(
// r#"
// 1 + 3 * 7 - 2
// "#
// ),
// 20,
// i64
// );
// }
#[test]
fn gen_order_of_arithmetic_ops() {
assert_evals_to!(
indoc!(
r#"
1 + 3 * 7 - 2
"#
),
20,
i64
);
}
// #[test]
// fn gen_order_of_arithmetic_ops_complex_float() {
// assert_evals_to!(
// indoc!(
// r#"
// 3 - 48 * 2.0
// "#
// ),
// -93.0,
// f64
// );
// }
#[test]
fn gen_order_of_arithmetic_ops_complex_float() {
assert_evals_to!(
indoc!(
r#"
3 - 48 * 2.0
"#
),
-93.0,
f64
);
}
// #[test]
// fn if_guard_bind_variable_false() {

View file

@ -389,18 +389,18 @@ mod wasm_records {
// // );
// // }
// #[test]
// fn i64_record1_literal() {
// assert_evals_to!(
// indoc!(
// r#"
// { a: 3 }
// "#
// ),
// 3,
// i64
// );
// }
#[test]
fn i64_record1_literal() {
assert_evals_to!(
indoc!(
r#"
{ a: 3 }
"#
),
3,
i64
);
}
// // #[test]
// // fn i64_record9_literal() {
@ -428,21 +428,21 @@ mod wasm_records {
// // );
// // }
// #[test]
// fn bool_literal() {
// assert_evals_to!(
// indoc!(
// r#"
// x : Bool
// x = True
#[test]
fn bool_literal() {
assert_evals_to!(
indoc!(
r#"
x : Bool
x = True
// x
// "#
// ),
// true,
// bool
// );
// }
x
"#
),
true,
bool
);
}
// #[test]
// fn optional_field_when_use_default() {