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
use crate::cdsl::operands::OperandKind;
use std::fmt;
use std::rc::Rc;
#[derive(Debug)]
pub(crate) struct FormatField {
pub kind: OperandKind,
pub member: &'static str,
}
#[derive(Debug)]
pub(crate) struct InstructionFormat {
pub name: &'static str,
pub num_value_operands: usize,
pub has_value_list: bool,
pub imm_fields: Vec<FormatField>,
pub num_block_operands: usize,
pub typevar_operand: Option<usize>,
}
#[derive(Hash, PartialEq, Eq)]
pub(crate) struct FormatStructure {
pub num_value_operands: usize,
pub has_value_list: bool,
pub num_block_operands: usize,
pub imm_field_names: Vec<(&'static str, &'static str)>,
}
impl fmt::Display for InstructionFormat {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let imm_args = self
.imm_fields
.iter()
.map(|field| format!("{}: {}", field.member, field.kind.rust_type))
.collect::<Vec<_>>()
.join(", ");
fmt.write_fmt(format_args!(
"{}(imms=({}), vals={}, blocks={})",
self.name, imm_args, self.num_value_operands, self.num_block_operands,
))?;
Ok(())
}
}
impl InstructionFormat {
pub fn structure(&self) -> FormatStructure {
FormatStructure {
num_value_operands: self.num_value_operands,
has_value_list: self.has_value_list,
num_block_operands: self.num_block_operands,
imm_field_names: self
.imm_fields
.iter()
.map(|field| (field.kind.rust_field_name, field.kind.rust_type))
.collect::<Vec<_>>(),
}
}
}
pub(crate) struct InstructionFormatBuilder(InstructionFormat);
impl InstructionFormatBuilder {
pub fn new(name: &'static str) -> Self {
Self(InstructionFormat {
name,
num_value_operands: 0,
has_value_list: false,
num_block_operands: 0,
imm_fields: Vec::new(),
typevar_operand: None,
})
}
pub fn value(mut self) -> Self {
self.0.num_value_operands += 1;
self
}
pub fn varargs(mut self) -> Self {
self.0.has_value_list = true;
self
}
pub fn block(mut self) -> Self {
self.0.num_block_operands += 1;
self
}
pub fn imm(mut self, operand_kind: &OperandKind) -> Self {
let field = FormatField {
kind: operand_kind.clone(),
member: operand_kind.rust_field_name,
};
self.0.imm_fields.push(field);
self
}
pub fn typevar_operand(mut self, operand_index: usize) -> Self {
assert!(self.0.typevar_operand.is_none());
assert!(operand_index < self.0.num_value_operands);
self.0.typevar_operand = Some(operand_index);
self
}
pub fn build(mut self) -> Rc<InstructionFormat> {
if self.0.typevar_operand.is_none() && self.0.num_value_operands > 0 {
self.0.typevar_operand = Some(0);
};
Rc::new(self.0)
}
}