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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use grammar;
use spirv;
use spirv::Word;
use utils::version;
use std::{convert, fmt, iter};
#[derive(Debug, Default)]
pub struct Module {
pub header: Option<ModuleHeader>,
pub capabilities: Vec<Instruction>,
pub extensions: Vec<Instruction>,
pub ext_inst_imports: Vec<Instruction>,
pub memory_model: Option<Instruction>,
pub entry_points: Vec<Instruction>,
pub execution_modes: Vec<Instruction>,
pub debugs: Vec<Instruction>,
pub annotations: Vec<Instruction>,
pub types_global_values: Vec<Instruction>,
pub functions: Vec<Function>,
}
#[derive(Debug, PartialEq)]
pub struct ModuleHeader {
pub magic_number: Word,
pub version: Word,
pub generator: Word,
pub bound: Word,
pub reserved_word: Word,
}
#[derive(Debug, Default)]
pub struct Function {
pub def: Option<Instruction>,
pub end: Option<Instruction>,
pub parameters: Vec<Instruction>,
pub basic_blocks: Vec<BasicBlock>,
}
#[derive(Debug, Default)]
pub struct BasicBlock {
pub label: Option<Instruction>,
pub instructions: Vec<Instruction>,
}
#[derive(Debug)]
pub struct Instruction {
pub class: &'static grammar::Instruction<'static>,
pub result_type: Option<Word>,
pub result_id: Option<Word>,
pub operands: Vec<Operand>,
}
pub struct InstIter<'i> {
instructions: Vec<&'i Instruction>,
index: usize,
}
impl<'i> InstIter<'i> {
pub fn new(insts: Vec<&'i Instruction>) -> InstIter<'i> {
InstIter {
instructions: insts,
index: 0,
}
}
}
impl<'i> iter::Iterator for InstIter<'i> {
type Item = &'i Instruction;
fn next(&mut self) -> Option<&'i Instruction> {
if self.index < self.instructions.len() {
let inst = self.instructions[self.index];
self.index += 1;
Some(inst)
} else {
None
}
}
}
include!("operand.rs");
impl Module {
pub fn new() -> Module {
Module {
header: None,
capabilities: vec![],
extensions: vec![],
ext_inst_imports: vec![],
memory_model: None,
entry_points: vec![],
execution_modes: vec![],
debugs: vec![],
annotations: vec![],
types_global_values: vec![],
functions: vec![],
}
}
pub fn global_inst_iter(&self) -> InstIter {
let mut insts = vec![];
let mut i: Vec<&Instruction> = self.capabilities.iter().collect();
insts.append(&mut i);
let mut i: Vec<&Instruction> = self.extensions.iter().collect();
insts.append(&mut i);
let mut i: Vec<&Instruction> = self.ext_inst_imports.iter().collect();
insts.append(&mut i);
if let Some(ref i) = self.memory_model {
insts.push(i);
}
let mut i: Vec<&Instruction> = self.entry_points.iter().collect();
insts.append(&mut i);
let mut i: Vec<&Instruction> = self.execution_modes.iter().collect();
insts.append(&mut i);
let mut i: Vec<&Instruction> = self.debugs.iter().collect();
insts.append(&mut i);
let mut i: Vec<&Instruction> = self.annotations.iter().collect();
insts.append(&mut i);
let mut i: Vec<&Instruction> = self.types_global_values.iter().collect();
insts.append(&mut i);
InstIter::new(insts)
}
}
impl ModuleHeader {
pub fn new(bound: Word) -> ModuleHeader {
ModuleHeader {
magic_number: spirv::MAGIC_NUMBER,
version: (spirv::MAJOR_VERSION << 16) | (spirv::MINOR_VERSION << 8),
generator: 0x000f0000,
bound: bound,
reserved_word: 0,
}
}
pub fn set_version(&mut self, major: u8, minor: u8) {
self.version = ((major as u32) << 16) | ((minor as u32) << 8);
}
pub fn version(&self) -> (u8, u8) {
version::create_version_from_word(self.version)
}
pub fn generator(&self) -> (&str, u16) {
let tool = (self.generator & 0xffff0000) >> 16;
let version = (self.generator & 0xffff) as u16;
let tool: &str = match tool {
0 => "The Khronos Group",
1 => "LunarG",
2 => "Valve",
3 => "Codeplay",
4 => "NVIDIA",
5 => "ARM",
6 => "LLVM/SPIR-V Translator",
7 => "SPIR-V Tools Assembler",
8 => "Glslang",
9 => "Qualcomm",
10 => "AMD",
11 => "Intel",
12 => "Imagination",
13 => "Shaderc",
14 => "spiregg",
15 => "rspirv",
_ => "Unknown",
};
(tool, version)
}
}
impl Function {
pub fn new() -> Function {
Function {
def: None,
end: None,
parameters: vec![],
basic_blocks: vec![],
}
}
}
impl BasicBlock {
pub fn new() -> BasicBlock {
BasicBlock {
label: None,
instructions: vec![],
}
}
}
impl Instruction {
pub fn new(opcode: spirv::Op,
result_type: Option<Word>,
result_id: Option<Word>,
operands: Vec<Operand>)
-> Instruction {
Instruction {
class: grammar::CoreInstructionTable::get(opcode),
result_type: result_type,
result_id: result_id,
operands: operands,
}
}
}
impl<'a> convert::From<&'a str> for Operand {
fn from(val: &'a str) -> Self {
Operand::LiteralString(val.to_owned())
}
}
impl convert::From<u32> for Operand {
fn from(val: u32) -> Self {
Operand::LiteralInt32(val)
}
}
#[cfg(test)]
mod tests {
use mr;
use spirv;
#[test]
fn test_convert_from_string() {
assert_eq!(mr::Operand::LiteralString("wow".to_string()),
mr::Operand::from("wow"));
assert_eq!(mr::Operand::LiteralString("wow".to_string()),
mr::Operand::from("wow".to_string()));
}
#[test]
fn test_convert_from_numbers() {
assert_eq!(mr::Operand::LiteralInt32(16u32), mr::Operand::from(16u32));
assert_eq!(mr::Operand::LiteralInt64(128934u64),
mr::Operand::from(128934u64));
assert_eq!(mr::Operand::LiteralFloat32(3.14f32),
mr::Operand::from(3.14f32));
assert_eq!(mr::Operand::LiteralFloat64(10.4235f64),
mr::Operand::from(10.4235f64));
}
#[test]
fn test_convert_from_bit_enums() {
assert_eq!(mr::Operand::LoopControl(spirv::LoopControl::DONT_UNROLL |
spirv::LoopControl::UNROLL),
mr::Operand::from(spirv::LoopControl::DONT_UNROLL | spirv::LoopControl::UNROLL));
assert_eq!(mr::Operand::MemoryAccess(spirv::MemoryAccess::NONE),
mr::Operand::from(spirv::MemoryAccess::NONE));
}
#[test]
fn test_convert_from_value_enums() {
assert_eq!(mr::Operand::BuiltIn(spirv::BuiltIn::Position),
mr::Operand::from(spirv::BuiltIn::Position));
assert_eq!(mr::Operand::Capability(spirv::Capability::Pipes),
mr::Operand::from(spirv::Capability::Pipes));
}
#[test]
fn test_convert_from_op() {
assert_eq!(mr::Operand::LiteralSpecConstantOpInteger(spirv::Op::IAdd),
mr::Operand::from(spirv::Op::IAdd));
}
}