Skip to content

Commit b65bd1c

Browse files
committed
Add an interpret command to clif-util
1 parent 9cf90b8 commit b65bd1c

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cranelift/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ path = "src/clif-util.rs"
1717
cfg-if = "0.1"
1818
cranelift-codegen = { path = "codegen", version = "0.63.0" }
1919
cranelift-entity = { path = "entity", version = "0.63.0" }
20+
cranelift-interpreter = { path = "interpreter", version = "*" }
2021
cranelift-reader = { path = "reader", version = "0.63.0" }
2122
cranelift-frontend = { path = "frontend", version = "0.63.0" }
2223
cranelift-serde = { path = "serde", version = "0.63.0", optional = true }
@@ -31,6 +32,7 @@ cranelift-preopt = { path = "preopt", version = "0.63.0" }
3132
cranelift = { path = "umbrella", version = "0.63.0" }
3233
filecheck = "0.5.0"
3334
clap = "2.32.0"
35+
log = "0.4.8"
3436
serde = "1.0.8"
3537
term = "0.6.1"
3638
capstone = { version = "0.6.0", optional = true }
@@ -39,6 +41,7 @@ target-lexicon = "0.10"
3941
pretty_env_logger = "0.4.0"
4042
file-per-thread-logger = "0.1.2"
4143
indicatif = "0.13.0"
44+
thiserror = "1.0.15"
4245
walkdir = "2.2"
4346

4447
[features]

cranelift/src/clif-util.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod bugpoint;
2424
mod cat;
2525
mod compile;
2626
mod disasm;
27+
mod interpret;
2728
mod print_cfg;
2829
mod run;
2930
mod utils;
@@ -179,6 +180,13 @@ fn main() {
179180
.arg(add_input_file_arg())
180181
.arg(add_debug_flag()),
181182
)
183+
.subcommand(
184+
SubCommand::with_name("interpret")
185+
.about("Interpret CLIF code")
186+
.arg(add_verbose_flag())
187+
.arg(add_input_file_arg())
188+
.arg(add_debug_flag()),
189+
)
182190
.subcommand(
183191
SubCommand::with_name("cat")
184192
.about("Outputs .clif file")
@@ -239,6 +247,14 @@ fn main() {
239247
)
240248
.map(|_time| ())
241249
}
250+
("interpret", Some(rest_cmd)) => {
251+
handle_debug_flag(rest_cmd.is_present("debug"));
252+
interpret::run(
253+
get_vec(rest_cmd.values_of("file")),
254+
rest_cmd.is_present("verbose"),
255+
)
256+
.map(|_time| ())
257+
}
242258
("pass", Some(rest_cmd)) => {
243259
handle_debug_flag(rest_cmd.is_present("debug"));
244260

cranelift/src/interpret.rs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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

Comments
 (0)