Compile a simple function (#2)

* Implement basics.

* Execute code

* Add wasm2wat test cases.

* abi_loc_for_arg for stack.

* Assert that sp_depth is 0 at the epilogue

* Do 32bit add.

* Assert that RAX can be used as a scratch register

* Reuse assembler.

* Align stack slots.
This commit is contained in:
Sergey Pepyakin
2018-11-08 22:56:27 +01:00
committed by Dan Gohman
parent d3bc26bc93
commit 08240761d5
9 changed files with 338 additions and 72 deletions

34
src/tests.rs Normal file
View File

@@ -0,0 +1,34 @@
use super::{translate, TranslatedModule};
use wabt;
fn translate_wat(wat: &str) -> TranslatedModule {
let wasm = wabt::wat2wasm(wat).unwrap();
let compiled = translate(&wasm).unwrap();
compiled
}
/// Execute the first function in the module.
fn execute_wat(wat: &str, a: usize, b: usize) -> usize {
let translated = translate_wat(wat);
translated.execute_func(0, a, b)
}
#[test]
fn adds() {
const CASES: &[(usize, usize, usize)] = &[
(5, 3, 8),
(0, 228, 228),
(usize::max_value(), 1, 0),
];
let code = r#"
(module
(func (param i32) (param i32) (result i32) (i32.add (get_local 0) (get_local 1)))
)
"#;
for (a, b, expected) in CASES {
assert_eq!(execute_wat(code, *a, *b), *expected);
}
}
// TODO: Add a test that checks argument passing via the stack.