1use alloc::{vec, vec::Vec};
2use core::iter;
3
4use spirv::{Op, Word, MAGIC_NUMBER};
5
6use super::{Instruction, LogicalLayout, PhysicalLayout};
7
8#[cfg(test)]
9use alloc::format;
10
11const GENERATOR: Word = 28;
13
14impl PhysicalLayout {
15 pub(super) const fn new(major_version: u8, minor_version: u8) -> Self {
16 let version = ((major_version as u32) << 16) | ((minor_version as u32) << 8);
17 PhysicalLayout {
18 magic_number: MAGIC_NUMBER,
19 version,
20 generator: GENERATOR,
21 bound: 0,
22 instruction_schema: 0x0u32,
23 }
24 }
25
26 pub(super) fn in_words(&self, sink: &mut impl Extend<Word>) {
27 sink.extend(iter::once(self.magic_number));
28 sink.extend(iter::once(self.version));
29 sink.extend(iter::once(self.generator));
30 sink.extend(iter::once(self.bound));
31 sink.extend(iter::once(self.instruction_schema));
32 }
33
34 pub(super) const fn lang_version(&self) -> (u8, u8) {
36 let major = (self.version >> 16) as u8;
37 let minor = (self.version >> 8) as u8;
38 (major, minor)
39 }
40}
41
42impl super::reclaimable::Reclaimable for PhysicalLayout {
43 fn reclaim(self) -> Self {
44 PhysicalLayout {
45 magic_number: self.magic_number,
46 version: self.version,
47 generator: self.generator,
48 instruction_schema: self.instruction_schema,
49 bound: 0,
50 }
51 }
52}
53
54impl LogicalLayout {
55 pub(super) fn in_words(&self, sink: &mut impl Extend<Word>) {
56 sink.extend(self.capabilities.iter().cloned());
57 sink.extend(self.extensions.iter().cloned());
58 sink.extend(self.ext_inst_imports.iter().cloned());
59 sink.extend(self.memory_model.iter().cloned());
60 sink.extend(self.entry_points.iter().cloned());
61 sink.extend(self.execution_modes.iter().cloned());
62 sink.extend(self.debugs.iter().cloned());
63 sink.extend(self.annotations.iter().cloned());
64 sink.extend(self.declarations.iter().cloned());
65 sink.extend(self.function_declarations.iter().cloned());
66 sink.extend(self.function_definitions.iter().cloned());
67 }
68}
69
70impl super::reclaimable::Reclaimable for LogicalLayout {
71 fn reclaim(self) -> Self {
72 Self {
73 capabilities: self.capabilities.reclaim(),
74 extensions: self.extensions.reclaim(),
75 ext_inst_imports: self.ext_inst_imports.reclaim(),
76 memory_model: self.memory_model.reclaim(),
77 entry_points: self.entry_points.reclaim(),
78 execution_modes: self.execution_modes.reclaim(),
79 debugs: self.debugs.reclaim(),
80 annotations: self.annotations.reclaim(),
81 declarations: self.declarations.reclaim(),
82 function_declarations: self.function_declarations.reclaim(),
83 function_definitions: self.function_definitions.reclaim(),
84 }
85 }
86}
87
88impl Instruction {
89 pub(super) const fn new(op: Op) -> Self {
90 Instruction {
91 op,
92 wc: 1, type_id: None,
94 result_id: None,
95 operands: vec![],
96 }
97 }
98
99 pub(super) fn set_type(&mut self, id: Word) {
100 assert!(self.type_id.is_none(), "Type can only be set once");
101 self.type_id = Some(id);
102 self.wc += 1;
103 }
104
105 pub(super) fn set_result(&mut self, id: Word) {
106 assert!(self.result_id.is_none(), "Result can only be set once");
107 self.result_id = Some(id);
108 self.wc += 1;
109 }
110
111 pub(super) fn add_operand(&mut self, operand: Word) {
112 self.operands.push(operand);
113 self.wc += 1;
114 }
115
116 pub(super) fn add_operands(&mut self, operands: Vec<Word>) {
117 for operand in operands.into_iter() {
118 self.add_operand(operand)
119 }
120 }
121
122 pub(super) fn to_words(&self, sink: &mut impl Extend<Word>) {
123 sink.extend(Some((self.wc << 16) | self.op as u32));
124 sink.extend(self.type_id);
125 sink.extend(self.result_id);
126 sink.extend(self.operands.iter().cloned());
127 }
128}
129
130impl Instruction {
131 #[cfg(test)]
132 fn validate(&self, words: &[Word]) {
133 let mut inst_index = 0;
134 let (wc, op) = ((words[inst_index] >> 16) as u16, words[inst_index] as u16);
135 inst_index += 1;
136
137 assert_eq!(wc, words.len() as u16);
138 assert_eq!(op, self.op as u16);
139
140 if let Some(type_id) = self.type_id {
141 assert_eq!(words[inst_index], type_id);
142 inst_index += 1;
143 }
144
145 if let Some(result_id) = self.result_id {
146 assert_eq!(words[inst_index], result_id);
147 inst_index += 1;
148 }
149
150 for (op_index, i) in (inst_index..wc as usize).enumerate() {
151 assert_eq!(words[i], self.operands[op_index]);
152 }
153 }
154}
155
156#[test]
157fn test_physical_layout_in_words() {
158 let bound = 5;
159
160 let version = 0x0001_0200;
163
164 let mut output = vec![];
165 let mut layout = PhysicalLayout::new(1, 2);
166 layout.bound = bound;
167
168 layout.in_words(&mut output);
169
170 assert_eq!(&output, &[MAGIC_NUMBER, version, GENERATOR, bound, 0,]);
171}
172
173#[test]
174fn test_logical_layout_in_words() {
175 let mut output = vec![];
176 let mut layout = LogicalLayout::default();
177 let layout_vectors = 11;
178 let mut instructions = Vec::with_capacity(layout_vectors);
179
180 let vector_names = &[
181 "Capabilities",
182 "Extensions",
183 "External Instruction Imports",
184 "Memory Model",
185 "Entry Points",
186 "Execution Modes",
187 "Debugs",
188 "Annotations",
189 "Declarations",
190 "Function Declarations",
191 "Function Definitions",
192 ];
193
194 for (i, _) in vector_names.iter().enumerate().take(layout_vectors) {
195 let mut dummy_instruction = Instruction::new(Op::Constant);
196 dummy_instruction.set_type((i + 1) as u32);
197 dummy_instruction.set_result((i + 2) as u32);
198 dummy_instruction.add_operand((i + 3) as u32);
199 dummy_instruction.add_operands(super::helpers::string_to_words(
200 format!("This is the vector: {}", vector_names[i]).as_str(),
201 ));
202 instructions.push(dummy_instruction);
203 }
204
205 instructions[0].to_words(&mut layout.capabilities);
206 instructions[1].to_words(&mut layout.extensions);
207 instructions[2].to_words(&mut layout.ext_inst_imports);
208 instructions[3].to_words(&mut layout.memory_model);
209 instructions[4].to_words(&mut layout.entry_points);
210 instructions[5].to_words(&mut layout.execution_modes);
211 instructions[6].to_words(&mut layout.debugs);
212 instructions[7].to_words(&mut layout.annotations);
213 instructions[8].to_words(&mut layout.declarations);
214 instructions[9].to_words(&mut layout.function_declarations);
215 instructions[10].to_words(&mut layout.function_definitions);
216
217 layout.in_words(&mut output);
218
219 let mut index: usize = 0;
220 for instruction in instructions {
221 let wc = instruction.wc as usize;
222 instruction.validate(&output[index..index + wc]);
223 index += wc;
224 }
225}