|
| 1 | +//! CLI tool to interpret Cranelift IR files. |
| 2 | +
|
| 3 | +use crate::utils::iterate_files; |
| 4 | +use cranelift_interpreter::environment::Environment; |
| 5 | +use cranelift_interpreter::interpreter::{ControlFlow, Interpreter}; |
| 6 | +use cranelift_reader::{parse_run_command, parse_test, ParseError, ParseOptions}; |
| 7 | +use log::debug; |
| 8 | +use std::path::PathBuf; |
| 9 | +use std::{fs, io}; |
| 10 | +use thiserror::Error; |
| 11 | + |
| 12 | +/// Run files through the Cranelift interpreter, interpreting any functions with annotations. |
| 13 | +pub fn run(files: Vec<String>, flag_print: bool) -> Result<(), String> { |
| 14 | + let mut total = 0; |
| 15 | + let mut errors = 0; |
| 16 | + for file in iterate_files(files) { |
| 17 | + total += 1; |
| 18 | + let runner = FileInterpreter::from_path(file).map_err(|e| e.to_string())?; |
| 19 | + match runner.run() { |
| 20 | + Ok(_) => { |
| 21 | + if flag_print { |
| 22 | + println!("{}", runner.path()); |
| 23 | + } |
| 24 | + } |
| 25 | + Err(e) => { |
| 26 | + if flag_print { |
| 27 | + println!("{}: {}", runner.path(), e.to_string()); |
| 28 | + } |
| 29 | + errors += 1; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + if flag_print { |
| 35 | + match total { |
| 36 | + 0 => println!("0 files"), |
| 37 | + 1 => println!("1 file"), |
| 38 | + n => println!("{} files", n), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + match errors { |
| 43 | + 0 => Ok(()), |
| 44 | + 1 => Err(String::from("1 failure")), |
| 45 | + n => Err(format!("{} failures", n)), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// Contains CLIF code that can be executed with [FileInterpreter::run]. |
| 50 | +pub struct FileInterpreter { |
| 51 | + path: Option<PathBuf>, |
| 52 | + contents: String, |
| 53 | +} |
| 54 | + |
| 55 | +impl FileInterpreter { |
| 56 | + /// Construct a file runner from a CLIF file path. |
| 57 | + pub fn from_path(path: impl Into<PathBuf>) -> Result<Self, io::Error> { |
| 58 | + let path = path.into(); |
| 59 | + debug!("New file runner from path: {}:", path.to_string_lossy()); |
| 60 | + let contents = fs::read_to_string(&path)?; |
| 61 | + Ok(Self { |
| 62 | + path: Some(path), |
| 63 | + contents, |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + /// Construct a file runner from a CLIF code string. Currently only used for testing. |
| 68 | + #[cfg(test)] |
| 69 | + pub fn from_inline_code(contents: String) -> Self { |
| 70 | + debug!("New file runner from inline code: {}:", &contents[..20]); |
| 71 | + Self { |
| 72 | + path: None, |
| 73 | + contents, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Return the path of the file runner or `[inline code]`. |
| 78 | + pub fn path(&self) -> String { |
| 79 | + match self.path { |
| 80 | + None => "[inline code]".to_string(), |
| 81 | + Some(ref p) => p.to_string_lossy().to_string(), |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// Run the file; this searches for annotations like `; run: %fn0(42)` or |
| 86 | + /// `; test: %fn0(42) == 2` and executes them, performing any test comparisons if necessary. |
| 87 | + pub fn run(&self) -> Result<(), FileInterpreterFailure> { |
| 88 | + // parse file |
| 89 | + let test = parse_test(&self.contents, ParseOptions::default()) |
| 90 | + .map_err(|e| FileInterpreterFailure::ParsingClif(self.path(), e))?; |
| 91 | + |
| 92 | + // collect functions |
| 93 | + let mut env = Environment::default(); |
| 94 | + let mut commands = vec![]; |
| 95 | + for (func, details) in test.functions.into_iter() { |
| 96 | + for comment in details.comments { |
| 97 | + if let Some(command) = parse_run_command(comment.text, &func.signature) |
| 98 | + .map_err(|e| FileInterpreterFailure::ParsingClif(self.path(), e))? |
| 99 | + { |
| 100 | + commands.push(command); |
| 101 | + } |
| 102 | + } |
| 103 | + // Note: func.name may truncate the function name |
| 104 | + env.add(func.name.to_string(), func); |
| 105 | + } |
| 106 | + |
| 107 | + // Run assertion commands |
| 108 | + let interpreter = Interpreter::new(env); |
| 109 | + for command in commands { |
| 110 | + command |
| 111 | + .run(|func_name, args| { |
| 112 | + // Because we have stored function names with a leading %, we need to re-add it. |
| 113 | + let func_name = &format!("%{}", func_name); |
| 114 | + match interpreter.call_by_name(func_name, args) { |
| 115 | + Ok(ControlFlow::Return(results)) => Ok(results), |
| 116 | + Ok(_) => panic!("Unexpected returned control flow--this is likely a bug."), |
| 117 | + Err(t) => Err(t.to_string()), |
| 118 | + } |
| 119 | + }) |
| 120 | + .map_err(|s| FileInterpreterFailure::FailedExecution(s))?; |
| 121 | + } |
| 122 | + |
| 123 | + Ok(()) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/// Possible sources of failure in this file. |
| 128 | +#[derive(Error, Debug)] |
| 129 | +pub enum FileInterpreterFailure { |
| 130 | + #[error("failure reading file")] |
| 131 | + Io(#[from] io::Error), |
| 132 | + #[error("failure parsing file {0}: {1}")] |
| 133 | + ParsingClif(String, ParseError), |
| 134 | + #[error("failed to run function: {0}")] |
| 135 | + FailedExecution(String), |
| 136 | +} |
| 137 | + |
| 138 | +#[cfg(test)] |
| 139 | +mod test { |
| 140 | + use super::*; |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn nop() { |
| 144 | + let code = String::from( |
| 145 | + " |
| 146 | + function %test() -> b8 { |
| 147 | + block0: |
| 148 | + nop |
| 149 | + v1 = bconst.b8 true |
| 150 | + v2 = iconst.i8 42 |
| 151 | + return v1 |
| 152 | + } |
| 153 | + ; run: %test() == true |
| 154 | + ", |
| 155 | + ); |
| 156 | + FileInterpreter::from_inline_code(code).run().unwrap() |
| 157 | + } |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn filetests() { |
| 161 | + run(vec!["../filetests/filetests/interpreter".to_string()], true).unwrap() |
| 162 | + } |
| 163 | +} |
0 commit comments