blob: 15c1741f67b508618f3ba98931a0db8e3ee1c303 [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -04002//
David Neto9fc86582016-09-01 15:33:59 -04003// 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
Andrew Woloszyn71fc0552015-09-24 10:26:51 -04006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Andrew Woloszyn71fc0552015-09-24 10:26:51 -04008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040014
dan sinclaireda2cfb2018-08-03 15:06:09 -040015#include "source/text_handler.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040016
Andrey Tuganovb173d1c2017-04-11 19:46:15 -040017#include <algorithm>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040018#include <cassert>
David Neto78e677b2015-10-05 13:28:46 -040019#include <cstdlib>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040020#include <cstring>
Andrew Woloszyn537e7762015-09-29 11:28:34 -040021#include <tuple>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040022
dan sinclaireda2cfb2018-08-03 15:06:09 -040023#include "source/assembly_grammar.h"
24#include "source/binary.h"
25#include "source/ext_inst.h"
26#include "source/instruction.h"
27#include "source/opcode.h"
28#include "source/text.h"
29#include "source/util/bitutils.h"
30#include "source/util/hex_float.h"
31#include "source/util/parse_number.h"
Marius Hillenbrand1ed847f2021-12-08 18:01:26 +010032#include "source/util/string_utils.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040033
dan sinclair3dad1cd2018-07-07 09:38:00 -040034namespace spvtools {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040035namespace {
dan sinclair3dad1cd2018-07-07 09:38:00 -040036
Lei Zhangf2cf7192016-04-21 15:50:23 -040037// Advances |text| to the start of the next line and writes the new position to
38// |position|.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040039spv_result_t advanceLine(spv_text text, spv_position position) {
40 while (true) {
Lei Zhangf2cf7192016-04-21 15:50:23 -040041 if (position->index >= text->length) return SPV_END_OF_STREAM;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040042 switch (text->str[position->index]) {
43 case '\0':
44 return SPV_END_OF_STREAM;
45 case '\n':
46 position->column = 0;
47 position->line++;
48 position->index++;
49 return SPV_SUCCESS;
50 default:
51 position->column++;
52 position->index++;
53 break;
54 }
55 }
56}
57
Lei Zhangf2cf7192016-04-21 15:50:23 -040058// Advances |text| to first non white space character and writes the new
59// position to |position|.
60// If a null terminator is found during the text advance, SPV_END_OF_STREAM is
61// returned, SPV_SUCCESS otherwise. No error checking is performed on the
62// parameters, its the users responsibility to ensure these are non null.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040063spv_result_t advance(spv_text text, spv_position position) {
64 // NOTE: Consume white space, otherwise don't advance.
Alastair Donaldson388ce0e2022-07-26 15:56:04 +010065 while (true) {
66 if (position->index >= text->length) return SPV_END_OF_STREAM;
67 switch (text->str[position->index]) {
68 case '\0':
69 return SPV_END_OF_STREAM;
70 case ';':
71 if (spv_result_t error = advanceLine(text, position)) return error;
72 continue;
73 case ' ':
74 case '\t':
75 case '\r':
76 position->column++;
77 position->index++;
78 continue;
79 case '\n':
80 position->column = 0;
81 position->line++;
82 position->index++;
83 continue;
84 default:
85 return SPV_SUCCESS;
86 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040087 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040088}
89
Lei Zhang6032b982016-03-15 16:52:40 -040090// Fetches the next word from the given text stream starting from the given
91// *position. On success, writes the decoded word into *word and updates
92// *position to the location past the returned word.
93//
94// A word ends at the next comment or whitespace. However, double-quoted
95// strings remain intact, and a backslash always escapes the next character.
96spv_result_t getWord(spv_text text, spv_position position, std::string* word) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040097 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
Lei Zhang6032b982016-03-15 16:52:40 -040098 if (!position) return SPV_ERROR_INVALID_POINTER;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040099
Lei Zhang6032b982016-03-15 16:52:40 -0400100 const size_t start_index = position->index;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400101
102 bool quoting = false;
103 bool escaping = false;
104
105 // NOTE: Assumes first character is not white space!
106 while (true) {
Lei Zhang6032b982016-03-15 16:52:40 -0400107 if (position->index >= text->length) {
108 word->assign(text->str + start_index, text->str + position->index);
Lei Zhang9413fbb2016-02-22 17:17:25 -0500109 return SPV_SUCCESS;
110 }
Lei Zhang6032b982016-03-15 16:52:40 -0400111 const char ch = text->str[position->index];
dan sinclair9991d662018-08-07 09:09:47 -0400112 if (ch == '\\') {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400113 escaping = !escaping;
dan sinclair9991d662018-08-07 09:09:47 -0400114 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400115 switch (ch) {
116 case '"':
117 if (!escaping) quoting = !quoting;
118 break;
119 case ' ':
120 case ';':
121 case '\t':
122 case '\n':
Dejan Mircevskia1de2b32016-04-01 00:47:02 -0400123 case '\r':
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400124 if (escaping || quoting) break;
David Neto9529d3c2021-10-01 08:43:59 -0400125 word->assign(text->str + start_index, text->str + position->index);
126 return SPV_SUCCESS;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400127 case '\0': { // NOTE: End of word found!
Lei Zhang6032b982016-03-15 16:52:40 -0400128 word->assign(text->str + start_index, text->str + position->index);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400129 return SPV_SUCCESS;
130 }
131 default:
132 break;
133 }
134 escaping = false;
135 }
136
Lei Zhang6032b982016-03-15 16:52:40 -0400137 position->column++;
138 position->index++;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400139 }
140}
141
142// Returns true if the characters in the text as position represent
143// the start of an Opcode.
144bool startsWithOp(spv_text text, spv_position position) {
145 if (text->length < position->index + 3) return false;
146 char ch0 = text->str[position->index];
147 char ch1 = text->str[position->index + 1];
148 char ch2 = text->str[position->index + 2];
149 return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
150}
151
dan sinclair3dad1cd2018-07-07 09:38:00 -0400152} // namespace
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400153
David Neto78e677b2015-10-05 13:28:46 -0400154const IdType kUnknownType = {0, false, IdTypeClass::kBottom};
155
David Neto78e677b2015-10-05 13:28:46 -0400156// TODO(dneto): Reorder AssemblyContext definitions to match declaration order.
157
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400158// This represents all of the data that is only valid for the duration of
159// a single compilation.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500160uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) {
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400161 if (!ids_to_preserve_.empty()) {
162 uint32_t id = 0;
dan sinclair76e0bde2018-07-06 13:25:17 -0400163 if (spvtools::utils::ParseNumber(textValue, &id)) {
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400164 if (ids_to_preserve_.find(id) != ids_to_preserve_.end()) {
165 bound_ = std::max(bound_, id + 1);
166 return id;
167 }
168 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400169 }
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400170
171 const auto it = named_ids_.find(textValue);
172 if (it == named_ids_.end()) {
173 uint32_t id = next_id_++;
174 if (!ids_to_preserve_.empty()) {
175 while (ids_to_preserve_.find(id) != ids_to_preserve_.end()) {
176 id = next_id_++;
177 }
178 }
179
180 named_ids_.emplace(textValue, id);
181 bound_ = std::max(bound_, id + 1);
182 return id;
183 }
184
185 return it->second;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400186}
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400187
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400188uint32_t AssemblyContext::getBound() const { return bound_; }
189
190spv_result_t AssemblyContext::advance() {
dan sinclair3dad1cd2018-07-07 09:38:00 -0400191 return spvtools::advance(text_, &current_position_);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400192}
193
Lei Zhang6032b982016-03-15 16:52:40 -0400194spv_result_t AssemblyContext::getWord(std::string* word,
195 spv_position next_position) {
196 *next_position = current_position_;
dan sinclair3dad1cd2018-07-07 09:38:00 -0400197 return spvtools::getWord(text_, next_position, word);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400198}
199
200bool AssemblyContext::startsWithOp() {
dan sinclair3dad1cd2018-07-07 09:38:00 -0400201 return spvtools::startsWithOp(text_, &current_position_);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400202}
203
204bool AssemblyContext::isStartOfNewInst() {
Lei Zhang6032b982016-03-15 16:52:40 -0400205 spv_position_t pos = current_position_;
dan sinclair3dad1cd2018-07-07 09:38:00 -0400206 if (spvtools::advance(text_, &pos)) return false;
207 if (spvtools::startsWithOp(text_, &pos)) return true;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400208
209 std::string word;
Lei Zhang6032b982016-03-15 16:52:40 -0400210 pos = current_position_;
dan sinclair3dad1cd2018-07-07 09:38:00 -0400211 if (spvtools::getWord(text_, &pos, &word)) return false;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400212 if ('%' != word.front()) return false;
213
dan sinclair3dad1cd2018-07-07 09:38:00 -0400214 if (spvtools::advance(text_, &pos)) return false;
215 if (spvtools::getWord(text_, &pos, &word)) return false;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400216 if ("=" != word) return false;
217
dan sinclair3dad1cd2018-07-07 09:38:00 -0400218 if (spvtools::advance(text_, &pos)) return false;
219 if (spvtools::startsWithOp(text_, &pos)) return true;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400220 return false;
221}
Andrew Woloszyn4274f932015-10-20 09:12:32 -0400222
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400223char AssemblyContext::peek() const {
224 return text_->str[current_position_.index];
225}
226
227bool AssemblyContext::hasText() const {
228 return text_->length > current_position_.index;
229}
Andrew Woloszyn4274f932015-10-20 09:12:32 -0400230
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400231void AssemblyContext::seekForward(uint32_t size) {
232 current_position_.index += size;
233 current_position_.column += size;
234}
235
236spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500237 spv_instruction_t* pInst) {
Andrew Woloszyn4ddb4312016-02-17 13:00:23 -0500238 pInst->words.insert(pInst->words.end(), value);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400239 return SPV_SUCCESS;
240}
241
David Neto78e677b2015-10-05 13:28:46 -0400242spv_result_t AssemblyContext::binaryEncodeNumericLiteral(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500243 const char* val, spv_result_t error_code, const IdType& type,
244 spv_instruction_t* pInst) {
dan sinclair76e0bde2018-07-06 13:25:17 -0400245 using spvtools::utils::EncodeNumberStatus;
qining1773b952016-09-01 14:27:04 -0400246 // Populate the NumberType from the IdType for parsing.
dan sinclair76e0bde2018-07-06 13:25:17 -0400247 spvtools::utils::NumberType number_type;
qining1773b952016-09-01 14:27:04 -0400248 switch (type.type_class) {
249 case IdTypeClass::kOtherType:
250 return diagnostic(SPV_ERROR_INTERNAL)
251 << "Unexpected numeric literal type";
252 case IdTypeClass::kScalarIntegerType:
253 if (type.isSigned) {
254 number_type = {type.bitwidth, SPV_NUMBER_SIGNED_INT};
255 } else {
256 number_type = {type.bitwidth, SPV_NUMBER_UNSIGNED_INT};
257 }
258 break;
259 case IdTypeClass::kScalarFloatType:
260 number_type = {type.bitwidth, SPV_NUMBER_FLOATING};
261 break;
262 case IdTypeClass::kBottom:
263 // kBottom means the type is unknown and we need to infer the type before
264 // parsing the number. The rule is: If there is a decimal point, treat
265 // the value as a floating point value, otherwise a integer value, then
266 // if the first char of the integer text is '-', treat the integer as a
267 // signed integer, otherwise an unsigned integer.
268 uint32_t bitwidth = static_cast<uint32_t>(assumedBitWidth(type));
269 if (strchr(val, '.')) {
270 number_type = {bitwidth, SPV_NUMBER_FLOATING};
271 } else if (type.isSigned || val[0] == '-') {
272 number_type = {bitwidth, SPV_NUMBER_SIGNED_INT};
273 } else {
274 number_type = {bitwidth, SPV_NUMBER_UNSIGNED_INT};
275 }
276 break;
David Neto78e677b2015-10-05 13:28:46 -0400277 }
278
qining1773b952016-09-01 14:27:04 -0400279 std::string error_msg;
280 EncodeNumberStatus parse_status = ParseAndEncodeNumber(
281 val, number_type,
282 [this, pInst](uint32_t d) { this->binaryEncodeU32(d, pInst); },
283 &error_msg);
284 switch (parse_status) {
285 case EncodeNumberStatus::kSuccess:
286 return SPV_SUCCESS;
287 case EncodeNumberStatus::kInvalidText:
288 return diagnostic(error_code) << error_msg;
289 case EncodeNumberStatus::kUnsupported:
290 return diagnostic(SPV_ERROR_INTERNAL) << error_msg;
291 case EncodeNumberStatus::kInvalidUsage:
292 return diagnostic(SPV_ERROR_INVALID_TEXT) << error_msg;
293 }
294 // This line is not reachable, only added to satisfy the compiler.
295 return diagnostic(SPV_ERROR_INTERNAL)
296 << "Unexpected result code from ParseAndEncodeNumber()";
David Neto78e677b2015-10-05 13:28:46 -0400297}
298
Lei Zhang1a0334e2015-11-02 09:41:20 -0500299spv_result_t AssemblyContext::binaryEncodeString(const char* value,
300 spv_instruction_t* pInst) {
David Netob5dc8fc2015-10-06 16:22:00 -0400301 const size_t length = strlen(value);
302 const size_t wordCount = (length / 4) + 1;
303 const size_t oldWordCount = pInst->words.size();
304 const size_t newWordCount = oldWordCount + wordCount;
305
306 // TODO(dneto): We can just defer this check until later.
307 if (newWordCount > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Neto78e677b2015-10-05 13:28:46 -0400308 return diagnostic() << "Instruction too long: more than "
309 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400310 }
311
Marius Hillenbrand1ed847f2021-12-08 18:01:26 +0100312 pInst->words.reserve(newWordCount);
313 spvtools::utils::AppendToVector(value, &pInst->words);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400314
315 return SPV_SUCCESS;
316}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400317
318spv_result_t AssemblyContext::recordTypeDefinition(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500319 const spv_instruction_t* pInst) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400320 uint32_t value = pInst->words[1];
321 if (types_.find(value) != types_.end()) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500322 return diagnostic() << "Value " << value
323 << " has already been used to generate a type";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400324 }
325
Lei Zhangb36e7042015-10-28 13:40:52 -0400326 if (pInst->opcode == SpvOpTypeInt) {
David Neto78e677b2015-10-05 13:28:46 -0400327 if (pInst->words.size() != 4)
328 return diagnostic() << "Invalid OpTypeInt instruction";
329 types_[value] = {pInst->words[2], pInst->words[3] != 0,
330 IdTypeClass::kScalarIntegerType};
Lei Zhangb36e7042015-10-28 13:40:52 -0400331 } else if (pInst->opcode == SpvOpTypeFloat) {
David Neto78e677b2015-10-05 13:28:46 -0400332 if (pInst->words.size() != 3)
333 return diagnostic() << "Invalid OpTypeFloat instruction";
334 types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400335 } else {
David Neto78e677b2015-10-05 13:28:46 -0400336 types_[value] = {0, false, IdTypeClass::kOtherType};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400337 }
338 return SPV_SUCCESS;
339}
340
341IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const {
342 auto type = types_.find(value);
343 if (type == types_.end()) {
David Neto78e677b2015-10-05 13:28:46 -0400344 return kUnknownType;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400345 }
346 return std::get<1>(*type);
347}
348
349IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const {
350 auto type_value = value_types_.find(value);
351 if (type_value == value_types_.end()) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500352 return {0, false, IdTypeClass::kBottom};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400353 }
354 return getTypeOfTypeGeneratingValue(std::get<1>(*type_value));
355}
356
357spv_result_t AssemblyContext::recordTypeIdForValue(uint32_t value,
358 uint32_t type) {
359 bool successfully_inserted = false;
360 std::tie(std::ignore, successfully_inserted) =
361 value_types_.insert(std::make_pair(value, type));
David Neto78e677b2015-10-05 13:28:46 -0400362 if (!successfully_inserted)
363 return diagnostic() << "Value is being defined a second time";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400364 return SPV_SUCCESS;
365}
366
David Neto2ae4a682015-11-09 18:55:42 -0500367spv_result_t AssemblyContext::recordIdAsExtInstImport(
368 uint32_t id, spv_ext_inst_type_t type) {
369 bool successfully_inserted = false;
370 std::tie(std::ignore, successfully_inserted) =
371 import_id_to_ext_inst_type_.insert(std::make_pair(id, type));
372 if (!successfully_inserted)
373 return diagnostic() << "Import Id is being defined a second time";
374 return SPV_SUCCESS;
375}
376
377spv_ext_inst_type_t AssemblyContext::getExtInstTypeForId(uint32_t id) const {
378 auto type = import_id_to_ext_inst_type_.find(id);
379 if (type == import_id_to_ext_inst_type_.end()) {
380 return SPV_EXT_INST_TYPE_NONE;
381 }
382 return std::get<1>(*type);
383}
384
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400385std::set<uint32_t> AssemblyContext::GetNumericIds() const {
386 std::set<uint32_t> ids;
387 for (const auto& kv : named_ids_) {
388 uint32_t id;
dan sinclair76e0bde2018-07-06 13:25:17 -0400389 if (spvtools::utils::ParseNumber(kv.first.c_str(), &id)) ids.insert(id);
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400390 }
391 return ids;
392}
393
dan sinclair3dad1cd2018-07-07 09:38:00 -0400394} // namespace spvtools