Add ability to call CLIF functions with arbitrary arguments in filetests

This resolves the work started in https://github.com/bytecodealliance/cranelift/pull/1231 and https://github.com/bytecodealliance/wasmtime/pull/1436. Cranelift filetests currently have the ability to run CLIF functions with a signature like `() -> b*` and check that the result is true under the `test run` directive. This PR adds the ability to call functions with arbitrary arguments and non-boolean returns and either print the result or check against a list of expected results:
 - `run` commands look like `; run: %add(2, 2) == 4` or `; run: %add(2, 2) != 5` and verify that the executed CLIF function returns the expected value
 - `print` commands look like `; print: %add(2, 2)` and print the result of the function to stdout

To make this work, this PR compiles a single Cranelift `Function` into a `CompiledFunction` using a `SingleFunctionCompiler`. Because we will not know the signature of the function until runtime, we use a `Trampoline` to place the values in the appropriate location for the calling convention; this should look a lot like what @alexcrichton is doing with `VMTrampoline` in wasmtime (see 3b7cb6ee64/crates/api/src/func.rs (L510-L526), 3b7cb6ee64/crates/jit/src/compiler.rs (L260)). To avoid re-compiling `Trampoline`s for the same function signatures, `Trampoline`s are cached in the `SingleFunctionCompiler`.
This commit is contained in:
Andrew Brown
2020-04-15 13:50:51 -07:00
parent 2048d3d30c
commit 38dff29179
8 changed files with 510 additions and 93 deletions

View File

@@ -114,11 +114,25 @@ pub fn parse_test<'a>(text: &'a str, options: ParseOptions<'a>) -> ParseResult<T
})
}
/// Parse the entire `text` as a run command.
pub fn parse_run_command<'a>(text: &str, signature: &Signature) -> ParseResult<RunCommand> {
/// Parse a CLIF comment `text` as a run command.
///
/// Return:
/// - `Ok(None)` if the comment is not intended to be a `RunCommand` (i.e. does not start with `run`
/// or `print`
/// - `Ok(Some(command))` if the comment is intended as a `RunCommand` and can be parsed to one
/// - `Err` otherwise.
pub fn parse_run_command<'a>(text: &str, signature: &Signature) -> ParseResult<Option<RunCommand>> {
let _tt = timing::parse_text();
let mut parser = Parser::new(text);
parser.parse_run_command(signature)
// We remove leading spaces and semi-colons for convenience here instead of at the call sites
// since this function will be attempting to parse a RunCommand from a CLIF comment.
let trimmed_text = text.trim_start_matches(|c| c == ' ' || c == ';');
let mut parser = Parser::new(trimmed_text);
match parser.token() {
Some(Token::Identifier("run")) | Some(Token::Identifier("print")) => {
parser.parse_run_command(signature).map(|c| Some(c))
}
Some(_) | None => Ok(None),
}
}
pub struct Parser<'a> {