/src/shaderc/third_party/spirv-tools/source/text_handler.cpp
Line | Count | Source |
1 | | // Copyright (c) 2015-2016 The Khronos Group Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "source/text_handler.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <cassert> |
19 | | #include <cstdlib> |
20 | | #include <cstring> |
21 | | #include <string_view> |
22 | | #include <tuple> |
23 | | |
24 | | #include "source/assembly_grammar.h" |
25 | | #include "source/binary.h" |
26 | | #include "source/ext_inst.h" |
27 | | #include "source/instruction.h" |
28 | | #include "source/opcode.h" |
29 | | #include "source/text.h" |
30 | | #include "source/util/bitutils.h" |
31 | | #include "source/util/hex_float.h" |
32 | | #include "source/util/parse_number.h" |
33 | | #include "source/util/string_utils.h" |
34 | | |
35 | | namespace spvtools { |
36 | | namespace { |
37 | | |
38 | | // Advances |text| to the start of the next line and writes the new position to |
39 | | // |position|. |
40 | 0 | spv_result_t advanceLine(spv_text text, spv_position position) { |
41 | 0 | while (true) { |
42 | 0 | if (position->index >= text->length) return SPV_END_OF_STREAM; |
43 | 0 | switch (text->str[position->index]) { |
44 | 0 | case '\0': |
45 | 0 | return SPV_END_OF_STREAM; |
46 | 0 | case '\n': |
47 | 0 | position->column = 0; |
48 | 0 | position->line++; |
49 | 0 | position->index++; |
50 | 0 | return SPV_SUCCESS; |
51 | 0 | default: |
52 | 0 | position->column++; |
53 | 0 | position->index++; |
54 | 0 | break; |
55 | 0 | } |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | // Advances |text| to first non white space character and writes the new |
60 | | // position to |position|. |
61 | | // If a null terminator is found during the text advance, SPV_END_OF_STREAM is |
62 | | // returned, SPV_SUCCESS otherwise. No error checking is performed on the |
63 | | // parameters, its the users responsibility to ensure these are non null. |
64 | 0 | spv_result_t advance(spv_text text, spv_position position) { |
65 | | // NOTE: Consume white space, otherwise don't advance. |
66 | 0 | while (true) { |
67 | 0 | if (position->index >= text->length) return SPV_END_OF_STREAM; |
68 | 0 | switch (text->str[position->index]) { |
69 | 0 | case '\0': |
70 | 0 | return SPV_END_OF_STREAM; |
71 | 0 | case ';': |
72 | 0 | if (spv_result_t error = advanceLine(text, position)) return error; |
73 | 0 | continue; |
74 | 0 | case ' ': |
75 | 0 | case '\t': |
76 | 0 | case '\r': |
77 | 0 | position->column++; |
78 | 0 | position->index++; |
79 | 0 | continue; |
80 | 0 | case '\n': |
81 | 0 | position->column = 0; |
82 | 0 | position->line++; |
83 | 0 | position->index++; |
84 | 0 | continue; |
85 | 0 | default: |
86 | 0 | return SPV_SUCCESS; |
87 | 0 | } |
88 | 0 | } |
89 | 0 | } |
90 | | |
91 | | // Fetches the next word from the given text stream starting from the given |
92 | | // *position. On success, writes the decoded word into *word and updates |
93 | | // *position to the location past the returned word. |
94 | | // |
95 | | // A word ends at the next comment or whitespace. However, double-quoted |
96 | | // strings remain intact, and a backslash always escapes the next character. |
97 | 0 | spv_result_t getWord(spv_text text, spv_position position, std::string* word) { |
98 | 0 | if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT; |
99 | 0 | if (!position) return SPV_ERROR_INVALID_POINTER; |
100 | | |
101 | 0 | const size_t start_index = position->index; |
102 | |
|
103 | 0 | bool quoting = false; |
104 | 0 | bool escaping = false; |
105 | | |
106 | | // NOTE: Assumes first character is not white space! |
107 | 0 | while (true) { |
108 | 0 | if (position->index >= text->length) { |
109 | 0 | word->assign(text->str + start_index, text->str + position->index); |
110 | 0 | return SPV_SUCCESS; |
111 | 0 | } |
112 | 0 | const char ch = text->str[position->index]; |
113 | 0 | if (ch == '\\') { |
114 | 0 | escaping = !escaping; |
115 | 0 | } else { |
116 | 0 | switch (ch) { |
117 | 0 | case '"': |
118 | 0 | if (!escaping) quoting = !quoting; |
119 | 0 | break; |
120 | 0 | case ' ': |
121 | 0 | case ';': |
122 | 0 | case ',': |
123 | 0 | case '(': |
124 | 0 | case ')': |
125 | 0 | case '\t': |
126 | 0 | case '\n': |
127 | 0 | case '\r': |
128 | 0 | if (escaping || quoting) break; |
129 | 0 | word->assign(text->str + start_index, text->str + position->index); |
130 | 0 | return SPV_SUCCESS; |
131 | 0 | case '\0': { // NOTE: End of word found! |
132 | 0 | word->assign(text->str + start_index, text->str + position->index); |
133 | 0 | return SPV_SUCCESS; |
134 | 0 | } |
135 | 0 | default: |
136 | 0 | break; |
137 | 0 | } |
138 | 0 | escaping = false; |
139 | 0 | } |
140 | | |
141 | 0 | position->column++; |
142 | 0 | position->index++; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | // Returns true if the characters in the text as position represent |
147 | | // the start of an Opcode. |
148 | 0 | bool startsWithOp(spv_text text, spv_position position) { |
149 | 0 | if (text->length < position->index + 3) return false; |
150 | 0 | char ch0 = text->str[position->index]; |
151 | 0 | char ch1 = text->str[position->index + 1]; |
152 | 0 | char ch2 = text->str[position->index + 2]; |
153 | 0 | return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z')); |
154 | 0 | } |
155 | | |
156 | | // Returns false if the the floating point encoding requires a bit width |
157 | | // different from the given width. Write the expected bit width via *expected. |
158 | | bool validBitWidthForFPEncoding(spv_fp_encoding_t enc, uint32_t width, |
159 | 0 | uint32_t* expected) { |
160 | 0 | switch (enc) { |
161 | 0 | case SPV_FP_ENCODING_IEEE754_BINARY16: |
162 | 0 | case SPV_FP_ENCODING_BFLOAT16: |
163 | 0 | *expected = 16; |
164 | 0 | break; |
165 | 0 | case SPV_FP_ENCODING_IEEE754_BINARY32: |
166 | 0 | *expected = 32; |
167 | 0 | break; |
168 | 0 | case SPV_FP_ENCODING_IEEE754_BINARY64: |
169 | 0 | *expected = 64; |
170 | 0 | break; |
171 | 0 | case SPV_FP_ENCODING_FLOAT4_E2M1: |
172 | 0 | *expected = 4; |
173 | 0 | break; |
174 | 0 | case SPV_FP_ENCODING_FLOAT6_E2M3: |
175 | 0 | case SPV_FP_ENCODING_FLOAT6_E3M2: |
176 | 0 | *expected = 6; |
177 | 0 | break; |
178 | 0 | case SPV_FP_ENCODING_FLOAT8_E5M2: |
179 | 0 | case SPV_FP_ENCODING_FLOAT8_E4M3: |
180 | 0 | case SPV_FP_ENCODING_FLOAT8_UNSIGNED_E8M0: |
181 | 0 | case SPV_FP_ENCODING_MXINT8: |
182 | 0 | *expected = 8; |
183 | 0 | break; |
184 | 0 | default: |
185 | 0 | return true; |
186 | 0 | } |
187 | 0 | return width == *expected; |
188 | 0 | } |
189 | | |
190 | | } // namespace |
191 | | |
192 | | const IdType kUnknownType = {0, false, IdTypeClass::kBottom}; |
193 | | |
194 | | // TODO(dneto): Reorder AssemblyContext definitions to match declaration order. |
195 | | |
196 | | // This represents all of the data that is only valid for the duration of |
197 | | // a single compilation. |
198 | 0 | uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) { |
199 | 0 | if (!ids_to_preserve_.empty()) { |
200 | 0 | uint32_t id = 0; |
201 | 0 | if (spvtools::utils::ParseNumber(textValue, &id)) { |
202 | 0 | if (ids_to_preserve_.find(id) != ids_to_preserve_.end()) { |
203 | 0 | bound_ = std::max(bound_, id + 1); |
204 | 0 | return id; |
205 | 0 | } |
206 | 0 | } |
207 | 0 | } |
208 | | |
209 | 0 | const auto it = named_ids_.find(textValue); |
210 | 0 | if (it == named_ids_.end()) { |
211 | 0 | uint32_t id = next_id_++; |
212 | 0 | if (!ids_to_preserve_.empty()) { |
213 | 0 | while (ids_to_preserve_.find(id) != ids_to_preserve_.end()) { |
214 | 0 | id = next_id_++; |
215 | 0 | } |
216 | 0 | } |
217 | |
|
218 | 0 | named_ids_.emplace(textValue, id); |
219 | 0 | bound_ = std::max(bound_, id + 1); |
220 | 0 | return id; |
221 | 0 | } |
222 | | |
223 | 0 | return it->second; |
224 | 0 | } |
225 | | |
226 | 0 | uint32_t AssemblyContext::getBound() const { return bound_; } |
227 | | |
228 | 0 | spv_result_t AssemblyContext::advance() { |
229 | 0 | return spvtools::advance(text_, ¤t_position_); |
230 | 0 | } |
231 | | |
232 | | spv_result_t AssemblyContext::getWord(std::string* word, |
233 | 0 | spv_position next_position) { |
234 | 0 | *next_position = current_position_; |
235 | 0 | return spvtools::getWord(text_, next_position, word); |
236 | 0 | } |
237 | | |
238 | 0 | bool AssemblyContext::startsWithOp() { |
239 | 0 | return spvtools::startsWithOp(text_, ¤t_position_); |
240 | 0 | } |
241 | | |
242 | 0 | bool AssemblyContext::isStartOfNewInst() { |
243 | 0 | spv_position_t pos = current_position_; |
244 | 0 | if (spvtools::advance(text_, &pos)) return false; |
245 | 0 | if (spvtools::startsWithOp(text_, &pos)) return true; |
246 | | |
247 | 0 | std::string word; |
248 | 0 | pos = current_position_; |
249 | 0 | if (spvtools::getWord(text_, &pos, &word)) return false; |
250 | 0 | if ('%' != word.front()) return false; |
251 | | |
252 | 0 | if (spvtools::advance(text_, &pos)) return false; |
253 | 0 | if (spvtools::getWord(text_, &pos, &word)) return false; |
254 | 0 | if ("=" != word) return false; |
255 | | |
256 | 0 | if (spvtools::advance(text_, &pos)) return false; |
257 | 0 | if (spvtools::startsWithOp(text_, &pos)) return true; |
258 | 0 | return false; |
259 | 0 | } |
260 | | |
261 | 0 | char AssemblyContext::peek() const { |
262 | 0 | return text_->str[current_position_.index]; |
263 | 0 | } |
264 | | |
265 | 0 | bool AssemblyContext::hasText() const { |
266 | 0 | return text_->length > current_position_.index; |
267 | 0 | } |
268 | | |
269 | 0 | void AssemblyContext::seekForward(uint32_t size) { |
270 | 0 | current_position_.index += size; |
271 | 0 | current_position_.column += size; |
272 | 0 | } |
273 | | |
274 | | spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value, |
275 | 0 | spv_instruction_t* pInst) { |
276 | 0 | pInst->words.insert(pInst->words.end(), value); |
277 | 0 | return SPV_SUCCESS; |
278 | 0 | } |
279 | | |
280 | | spv_result_t AssemblyContext::binaryEncodeNumericLiteral( |
281 | | const char* val, spv_result_t error_code, const IdType& type, |
282 | 0 | spv_instruction_t* pInst) { |
283 | 0 | using spvtools::utils::EncodeNumberStatus; |
284 | | // Populate the NumberType from the IdType for parsing. |
285 | 0 | spvtools::utils::NumberType number_type; |
286 | 0 | switch (type.type_class) { |
287 | 0 | case IdTypeClass::kOtherType: |
288 | 0 | return diagnostic(SPV_ERROR_INTERNAL) |
289 | 0 | << "Unexpected numeric literal type"; |
290 | 0 | case IdTypeClass::kScalarIntegerType: |
291 | 0 | if (type.isSigned) { |
292 | 0 | number_type = {type.bitwidth, SPV_NUMBER_SIGNED_INT, type.encoding}; |
293 | 0 | } else { |
294 | 0 | number_type = {type.bitwidth, SPV_NUMBER_UNSIGNED_INT, type.encoding}; |
295 | 0 | } |
296 | 0 | break; |
297 | 0 | case IdTypeClass::kScalarFloatType: |
298 | 0 | number_type = {type.bitwidth, SPV_NUMBER_FLOATING, type.encoding}; |
299 | 0 | break; |
300 | 0 | case IdTypeClass::kBottom: |
301 | | // kBottom means the type is unknown and we need to infer the type before |
302 | | // parsing the number. The rule is: If there is a decimal point, treat |
303 | | // the value as a floating point value, otherwise a integer value, then |
304 | | // if the first char of the integer text is '-', treat the integer as a |
305 | | // signed integer, otherwise an unsigned integer. |
306 | 0 | uint32_t bitwidth = static_cast<uint32_t>(assumedBitWidth(type)); |
307 | 0 | if (strchr(val, '.')) { |
308 | 0 | number_type = {bitwidth, SPV_NUMBER_FLOATING, type.encoding}; |
309 | 0 | } else if (type.isSigned || val[0] == '-') { |
310 | 0 | number_type = {bitwidth, SPV_NUMBER_SIGNED_INT, type.encoding}; |
311 | 0 | } else { |
312 | 0 | number_type = {bitwidth, SPV_NUMBER_UNSIGNED_INT, type.encoding}; |
313 | 0 | } |
314 | 0 | break; |
315 | 0 | } |
316 | | |
317 | 0 | std::string error_msg; |
318 | 0 | EncodeNumberStatus parse_status = ParseAndEncodeNumber( |
319 | 0 | val, number_type, |
320 | 0 | [this, pInst](uint32_t d) { this->binaryEncodeU32(d, pInst); }, |
321 | 0 | &error_msg); |
322 | 0 | switch (parse_status) { |
323 | 0 | case EncodeNumberStatus::kSuccess: |
324 | 0 | return SPV_SUCCESS; |
325 | 0 | case EncodeNumberStatus::kInvalidText: |
326 | 0 | return diagnostic(error_code) << error_msg; |
327 | 0 | case EncodeNumberStatus::kUnsupported: |
328 | 0 | return diagnostic(SPV_ERROR_INTERNAL) << error_msg; |
329 | 0 | case EncodeNumberStatus::kInvalidUsage: |
330 | 0 | return diagnostic(SPV_ERROR_INVALID_TEXT) << error_msg; |
331 | 0 | } |
332 | | // This line is not reachable, only added to satisfy the compiler. |
333 | 0 | return diagnostic(SPV_ERROR_INTERNAL) |
334 | 0 | << "Unexpected result code from ParseAndEncodeNumber()"; |
335 | 0 | } |
336 | | |
337 | | spv_result_t AssemblyContext::binaryEncodeString(const char* value, |
338 | 0 | spv_instruction_t* pInst) { |
339 | 0 | const size_t length = strlen(value); |
340 | 0 | const size_t wordCount = (length / 4) + 1; |
341 | 0 | const size_t oldWordCount = pInst->words.size(); |
342 | 0 | const size_t newWordCount = oldWordCount + wordCount; |
343 | | |
344 | | // TODO(dneto): We can just defer this check until later. |
345 | 0 | if (newWordCount > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
346 | 0 | return diagnostic() << "Instruction too long: more than " |
347 | 0 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words."; |
348 | 0 | } |
349 | | |
350 | 0 | pInst->words.reserve(newWordCount); |
351 | 0 | spvtools::utils::AppendToVector(value, &pInst->words); |
352 | |
|
353 | 0 | return SPV_SUCCESS; |
354 | 0 | } |
355 | | |
356 | | spv_result_t AssemblyContext::recordTypeDefinition( |
357 | 0 | const spv_instruction_t* pInst) { |
358 | 0 | uint32_t value = pInst->words[1]; |
359 | 0 | if (types_.find(value) != types_.end()) { |
360 | 0 | return diagnostic() << "Value " << value |
361 | 0 | << " has already been used to generate a type"; |
362 | 0 | } |
363 | | |
364 | 0 | if (pInst->opcode == spv::Op::OpTypeInt) { |
365 | 0 | if (pInst->words.size() != 4) |
366 | 0 | return diagnostic() << "Invalid OpTypeInt instruction"; |
367 | 0 | types_[value] = {pInst->words[2], pInst->words[3] != 0, |
368 | 0 | IdTypeClass::kScalarIntegerType, SPV_FP_ENCODING_UNKNOWN}; |
369 | 0 | } else if (pInst->opcode == spv::Op::OpTypeFloat) { |
370 | 0 | if ((pInst->words.size() != 3) && (pInst->words.size() != 4)) |
371 | 0 | return diagnostic() << "Invalid OpTypeFloat instruction"; |
372 | 0 | spv_fp_encoding_t enc = SPV_FP_ENCODING_UNKNOWN; |
373 | 0 | if (pInst->words.size() >= 4) { |
374 | 0 | const spvtools::OperandDesc* desc = nullptr; |
375 | 0 | spv_result_t status = spvtools::LookupOperand(SPV_OPERAND_TYPE_FPENCODING, |
376 | 0 | pInst->words[3], &desc); |
377 | 0 | if (status == SPV_SUCCESS) { |
378 | 0 | enc = spvFPEncodingFromOperandFPEncoding( |
379 | 0 | static_cast<spv::FPEncoding>(desc->value)); |
380 | 0 | uint32_t expected_width; |
381 | 0 | if (!validBitWidthForFPEncoding(enc, pInst->words[2], |
382 | 0 | &expected_width)) { |
383 | 0 | const auto& name_span = desc->name(); |
384 | 0 | const std::string_view name(name_span.data(), name_span.size() - 1); |
385 | 0 | return diagnostic() << "Invalid bit width " << pInst->words[2] |
386 | 0 | << " for floating point encoding " << name |
387 | 0 | << "; expected " << expected_width; |
388 | 0 | } |
389 | 0 | } else { |
390 | 0 | return diagnostic() << "Invalid OpTypeFloat encoding"; |
391 | 0 | } |
392 | 0 | } |
393 | 0 | types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType, |
394 | 0 | enc}; |
395 | 0 | } else { |
396 | 0 | types_[value] = {0, false, IdTypeClass::kOtherType, |
397 | 0 | SPV_FP_ENCODING_UNKNOWN}; |
398 | 0 | } |
399 | 0 | return SPV_SUCCESS; |
400 | 0 | } |
401 | | |
402 | 0 | IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const { |
403 | 0 | auto type = types_.find(value); |
404 | 0 | if (type == types_.end()) { |
405 | 0 | return kUnknownType; |
406 | 0 | } |
407 | 0 | return std::get<1>(*type); |
408 | 0 | } |
409 | | |
410 | 0 | IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const { |
411 | 0 | auto type_value = value_types_.find(value); |
412 | 0 | if (type_value == value_types_.end()) { |
413 | 0 | return {0, false, IdTypeClass::kBottom}; |
414 | 0 | } |
415 | 0 | return getTypeOfTypeGeneratingValue(std::get<1>(*type_value)); |
416 | 0 | } |
417 | | |
418 | | spv_result_t AssemblyContext::recordTypeIdForValue(uint32_t value, |
419 | 0 | uint32_t type) { |
420 | 0 | bool successfully_inserted = false; |
421 | 0 | std::tie(std::ignore, successfully_inserted) = |
422 | 0 | value_types_.insert(std::make_pair(value, type)); |
423 | 0 | if (!successfully_inserted) |
424 | 0 | return diagnostic() << "Value is being defined a second time"; |
425 | 0 | return SPV_SUCCESS; |
426 | 0 | } |
427 | | |
428 | | spv_result_t AssemblyContext::recordIdAsExtInstImport( |
429 | 0 | uint32_t id, spv_ext_inst_type_t type) { |
430 | 0 | bool successfully_inserted = false; |
431 | 0 | std::tie(std::ignore, successfully_inserted) = |
432 | 0 | import_id_to_ext_inst_type_.insert(std::make_pair(id, type)); |
433 | 0 | if (!successfully_inserted) |
434 | 0 | return diagnostic() << "Import Id is being defined a second time"; |
435 | 0 | return SPV_SUCCESS; |
436 | 0 | } |
437 | | |
438 | 0 | spv_ext_inst_type_t AssemblyContext::getExtInstTypeForId(uint32_t id) const { |
439 | 0 | auto type = import_id_to_ext_inst_type_.find(id); |
440 | 0 | if (type == import_id_to_ext_inst_type_.end()) { |
441 | 0 | return SPV_EXT_INST_TYPE_NONE; |
442 | 0 | } |
443 | 0 | return std::get<1>(*type); |
444 | 0 | } |
445 | | |
446 | 0 | std::set<uint32_t> AssemblyContext::GetNumericIds() const { |
447 | 0 | std::set<uint32_t> ids; |
448 | 0 | for (const auto& kv : named_ids_) { |
449 | 0 | uint32_t id; |
450 | 0 | if (spvtools::utils::ParseNumber(kv.first.c_str(), &id)) ids.insert(id); |
451 | 0 | } |
452 | 0 | return ids; |
453 | 0 | } |
454 | | |
455 | | } // namespace spvtools |