1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use cranelift_codegen::ir::immediates::{Ieee32, Ieee64};
use cranelift_codegen::ir::ConstantData;
use std::fmt::{Display, Formatter, Result};
#[derive(PartialEq, Debug)]
pub enum RunCommand {
Print(Invocation),
Run(Invocation, Comparison, Vec<DataValue>),
}
impl Display for RunCommand {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
RunCommand::Print(invocation) => write!(f, "print: {}", invocation),
RunCommand::Run(invocation, comparison, expected) => {
write!(f, "run: {} {} ", invocation, comparison)?;
if expected.len() == 1 {
write!(f, "{}", expected[0])
} else {
write!(f, "[")?;
write_data_value_list(f, expected)?;
write!(f, "]")
}
}
}
}
}
#[derive(Debug, PartialEq)]
pub struct Invocation {
pub func: String,
pub args: Vec<DataValue>,
}
impl Invocation {
pub(crate) fn new(func: &str, args: Vec<DataValue>) -> Self {
let func = func.to_string();
Self { func, args }
}
}
impl Display for Invocation {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "%{}(", self.func)?;
write_data_value_list(f, &self.args)?;
write!(f, ")")
}
}
#[allow(missing_docs)]
#[derive(Clone, Debug, PartialEq)]
pub enum DataValue {
B(bool),
I8(i8),
I16(i16),
I32(i32),
I64(i64),
F32(f32),
F64(f64),
V128([u8; 16]),
}
macro_rules! from_data {
( $ty:ty, $variant:ident ) => {
impl From<$ty> for DataValue {
fn from(data: $ty) -> Self {
DataValue::$variant(data)
}
}
};
}
from_data!(bool, B);
from_data!(i8, I8);
from_data!(i16, I16);
from_data!(i32, I32);
from_data!(i64, I64);
from_data!(f32, F32);
from_data!(f64, F64);
from_data!([u8; 16], V128);
impl Display for DataValue {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
DataValue::B(dv) => write!(f, "{}", dv),
DataValue::I8(dv) => write!(f, "{}", dv),
DataValue::I16(dv) => write!(f, "{}", dv),
DataValue::I32(dv) => write!(f, "{}", dv),
DataValue::I64(dv) => write!(f, "{}", dv),
DataValue::F32(dv) => write!(f, "{}", Ieee32::from(*dv)),
DataValue::F64(dv) => write!(f, "{}", Ieee64::from(*dv)),
DataValue::V128(dv) => write!(f, "{}", ConstantData::from(&dv[..])),
}
}
}
fn write_data_value_list(f: &mut Formatter<'_>, list: &[DataValue]) -> Result {
match list.len() {
0 => Ok(()),
1 => write!(f, "{}", list[0]),
_ => {
write!(f, "{}", list[0])?;
for dv in list.iter().skip(1) {
write!(f, ", {}", dv)?;
}
Ok(())
}
}
}
#[allow(missing_docs)]
#[derive(Debug, PartialEq)]
pub enum Comparison {
Equals,
NotEquals,
}
impl Display for Comparison {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Comparison::Equals => write!(f, "=="),
Comparison::NotEquals => write!(f, "!="),
}
}
}