1 //! Run commands.
2 //!
3 //! Functions in a `.clif` file can have *run commands* appended that control how a function is
4 //! invoked and tested within the `test run` context. The general syntax is:
5 //!
6 //! - `; run`: this assumes the function has a signature like `() -> b*`.
7 //! - `; run: %fn(42, 4.2) == false`: this syntax specifies the parameters and return values.
8 
9 use cranelift_codegen::ir::immediates::{Ieee32, Ieee64, Imm64};
10 use cranelift_codegen::ir::{self, types, ConstantData, Type};
11 use std::convert::TryInto;
12 use std::fmt::{self, Display, Formatter};
13 use thiserror::Error;
14 
15 /// A run command appearing in a test file.
16 ///
17 /// For parsing, see
18 /// [Parser::parse_run_command](crate::parser::Parser::parse_run_command).
19 #[derive(PartialEq, Debug)]
20 pub enum RunCommand {
21     /// Invoke a function and print its result.
22     Print(Invocation),
23     /// Invoke a function and compare its result to a value sequence.
24     Run(Invocation, Comparison, Vec<DataValue>),
25 }
26 
27 impl RunCommand {
28     /// Run the [RunCommand]:
29     ///  - for [RunCommand::Print], print the returned values from invoking the function.
30     ///  - for [RunCommand::Run], compare the returned values from the invoked function and
31     ///    return an `Err` with a descriptive string if the comparison fails.
32     ///
33     /// Accepts a function used for invoking the actual execution of the command. This function,
34     /// `invoked_fn`, is passed the _function name_ and _function arguments_ of the [Invocation].
35     pub fn run<F>(&self, invoke_fn: F) -> Result<(), String>
36     where
37         F: FnOnce(&str, &[DataValue]) -> Result<Vec<DataValue>, String>,
38     {
39         match self {
40             RunCommand::Print(invoke) => {
41                 let actual = invoke_fn(&invoke.func, &invoke.args)?;
42                 println!("{} -> {}", invoke, DisplayDataValues(&actual))
43             }
44             RunCommand::Run(invoke, compare, expected) => {
45                 let actual = invoke_fn(&invoke.func, &invoke.args)?;
46                 let matched = match compare {
47                     Comparison::Equals => *expected == actual,
48                     Comparison::NotEquals => *expected != actual,
49                 };
50                 if !matched {
51                     let actual = DisplayDataValues(&actual);
52                     return Err(format!("Failed test: {}, actual: {}", self, actual));
53                 }
54             }
55         }
56         Ok(())
57     }
58 }
59 
60 impl Display for RunCommand {
61     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
62         match self {
63             RunCommand::Print(invocation) => write!(f, "print: {}", invocation),
64             RunCommand::Run(invocation, comparison, expected) => {
65                 let expected = DisplayDataValues(expected);
66                 write!(f, "run: {} {} {}", invocation, comparison, expected)
67             }
68         }
69     }
70 }
71 
72 /// Represent a function call; [RunCommand]s invoke a CLIF function using an [Invocation].
73 #[derive(Debug, PartialEq)]
74 pub struct Invocation {
75     /// The name of the function to call. Note: this field is for mostly included for informational
76     /// purposes and may not always be necessary for identifying which function to call.
77     pub func: String,
78     /// The arguments to be passed to the function when invoked.
79     pub args: Vec<DataValue>,
80 }
81 
82 impl Invocation {
83     pub(crate) fn new(func: &str, args: Vec<DataValue>) -> Self {
84         let func = func.to_string();
85         Self { func, args }
86     }
87 }
88 
89 impl Display for Invocation {
90     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
91         write!(f, "%{}(", self.func)?;
92         write_data_value_list(f, &self.args)?;
93         write!(f, ")")
94     }
95 }
96 
97 /// Represent a data value. Where [Value] is an SSA reference, [DataValue] is the type + value
98 /// that would be referred to by a [Value].
99 ///
100 /// [Value]: cranelift_codegen::ir::Value
101 #[allow(missing_docs)]
102 #[derive(Clone, Debug, PartialEq)]
103 pub enum DataValue {
104     B(bool),
105     I8(i8),
106     I16(i16),
107     I32(i32),
108     I64(i64),
109     F32(f32),
110     F64(f64),
111     V128([u8; 16]),
112 }
113 
114 impl DataValue {
115     /// Try to cast an immediate integer ([Imm64]) to the given Cranelift [Type].
116     pub fn from_integer(imm: Imm64, ty: Type) -> Result<DataValue, DataValueCastFailure> {
117         match ty {
118             types::I8 => Ok(DataValue::I8(imm.bits() as i8)),
119             types::I16 => Ok(DataValue::I16(imm.bits() as i16)),
120             types::I32 => Ok(DataValue::I32(imm.bits() as i32)),
121             types::I64 => Ok(DataValue::I64(imm.bits())),
122             _ => Err(DataValueCastFailure::FromImm64(imm, ty)),
123         }
124     }
125 
126     /// Return the Cranelift IR [Type] for this [DataValue].
127     pub fn ty(&self) -> Type {
128         match self {
129             DataValue::B(_) => ir::types::B8, // A default type.
130             DataValue::I8(_) => ir::types::I8,
131             DataValue::I16(_) => ir::types::I16,
132             DataValue::I32(_) => ir::types::I32,
133             DataValue::I64(_) => ir::types::I64,
134             DataValue::F32(_) => ir::types::F32,
135             DataValue::F64(_) => ir::types::F64,
136             DataValue::V128(_) => ir::types::I8X16, // A default type.
137         }
138     }
139 
140     /// Return true if the value is a vector (i.e. `DataValue::V128`).
141     pub fn is_vector(&self) -> bool {
142         match self {
143             DataValue::V128(_) => true,
144             _ => false,
145         }
146     }
147 }
148 
149 /// Record failures to cast [DataValue].
150 #[derive(Error, Debug, PartialEq)]
151 #[allow(missing_docs)]
152 pub enum DataValueCastFailure {
153     #[error("unable to cast data value of type {0} to type {1}")]
154     TryInto(Type, Type),
155     #[error("unable to cast Imm64({0}) to a data value of type {1}")]
156     FromImm64(Imm64, Type),
157 }
158 
159 /// Helper for creating conversion implementations for [DataValue].
160 macro_rules! build_conversion_impl {
161     ( $rust_ty:ty, $data_value_ty:ident, $cranelift_ty:ident ) => {
162         impl From<$rust_ty> for DataValue {
163             fn from(data: $rust_ty) -> Self {
164                 DataValue::$data_value_ty(data)
165             }
166         }
167 
168         impl TryInto<$rust_ty> for DataValue {
169             type Error = DataValueCastFailure;
170             fn try_into(self) -> Result<$rust_ty, Self::Error> {
171                 if let DataValue::$data_value_ty(v) = self {
172                     Ok(v)
173                 } else {
174                     Err(DataValueCastFailure::TryInto(
175                         self.ty(),
176                         types::$cranelift_ty,
177                     ))
178                 }
179             }
180         }
181     };
182 }
183 build_conversion_impl!(bool, B, B8);
184 build_conversion_impl!(i8, I8, I8);
185 build_conversion_impl!(i16, I16, I16);
186 build_conversion_impl!(i32, I32, I32);
187 build_conversion_impl!(i64, I64, I64);
188 build_conversion_impl!(f32, F32, F32);
189 build_conversion_impl!(f64, F64, F64);
190 build_conversion_impl!([u8; 16], V128, I8X16);
191 
192 impl Display for DataValue {
193     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
194         match self {
195             DataValue::B(dv) => write!(f, "{}", dv),
196             DataValue::I8(dv) => write!(f, "{}", dv),
197             DataValue::I16(dv) => write!(f, "{}", dv),
198             DataValue::I32(dv) => write!(f, "{}", dv),
199             DataValue::I64(dv) => write!(f, "{}", dv),
200             // Use the Ieee* wrappers here to maintain a consistent syntax.
201             DataValue::F32(dv) => write!(f, "{}", Ieee32::from(*dv)),
202             DataValue::F64(dv) => write!(f, "{}", Ieee64::from(*dv)),
203             // Again, for syntax consistency, use ConstantData, which in this case displays as hex.
204             DataValue::V128(dv) => write!(f, "{}", ConstantData::from(&dv[..])),
205         }
206     }
207 }
208 
209 /// Helper structure for printing bracket-enclosed vectors of [DataValue]s.
210 /// - for empty vectors, display `[]`
211 /// - for single item vectors, display `42`, e.g.
212 /// - for multiple item vectors, display `[42, 43, 44]`, e.g.
213 struct DisplayDataValues<'a>(&'a [DataValue]);
214 
215 impl<'a> Display for DisplayDataValues<'a> {
216     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
217         if self.0.len() == 1 {
218             write!(f, "{}", self.0[0])
219         } else {
220             write!(f, "[")?;
221             write_data_value_list(f, &self.0)?;
222             write!(f, "]")
223         }
224     }
225 }
226 
227 /// Helper function for displaying `Vec<DataValue>`.
228 fn write_data_value_list(f: &mut Formatter<'_>, list: &[DataValue]) -> fmt::Result {
229     match list.len() {
230         0 => Ok(()),
231         1 => write!(f, "{}", list[0]),
232         _ => {
233             write!(f, "{}", list[0])?;
234             for dv in list.iter().skip(1) {
235                 write!(f, ", {}", dv)?;
236             }
237             Ok(())
238         }
239     }
240 }
241 
242 /// A CLIF comparison operation; e.g. `==`.
243 #[allow(missing_docs)]
244 #[derive(Debug, PartialEq)]
245 pub enum Comparison {
246     Equals,
247     NotEquals,
248 }
249 
250 impl Display for Comparison {
251     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
252         match self {
253             Comparison::Equals => write!(f, "=="),
254             Comparison::NotEquals => write!(f, "!="),
255         }
256     }
257 }
258 
259 #[cfg(test)]
260 mod test {
261     use super::*;
262     use crate::parse_run_command;
263     use cranelift_codegen::ir::{types, AbiParam, Signature};
264     use cranelift_codegen::isa::CallConv;
265 
266     #[test]
267     fn run_a_command() {
268         let mut signature = Signature::new(CallConv::Fast);
269         signature.returns.push(AbiParam::new(types::I32));
270         let command = parse_run_command(";; run: %return42() == 42 ", &signature)
271             .unwrap()
272             .unwrap();
273 
274         assert!(command.run(|_, _| Ok(vec![DataValue::I32(42)])).is_ok());
275         assert!(command.run(|_, _| Ok(vec![DataValue::I32(43)])).is_err());
276     }
277 
278     #[test]
279     fn type_conversions() {
280         assert_eq!(DataValue::B(true).ty(), types::B8);
281         assert_eq!(
282             TryInto::<bool>::try_into(DataValue::B(false)).unwrap(),
283             false
284         );
285         assert_eq!(
286             TryInto::<i32>::try_into(DataValue::B(false)).unwrap_err(),
287             DataValueCastFailure::TryInto(types::B8, types::I32)
288         );
289 
290         assert_eq!(DataValue::V128([0; 16]).ty(), types::I8X16);
291         assert_eq!(
292             TryInto::<[u8; 16]>::try_into(DataValue::V128([0; 16])).unwrap(),
293             [0; 16]
294         );
295         assert_eq!(
296             TryInto::<i32>::try_into(DataValue::V128([0; 16])).unwrap_err(),
297             DataValueCastFailure::TryInto(types::I8X16, types::I32)
298         );
299     }
300 }
301