blob: fe12a26e38355bdf0a10e3d98888fde36695fac6 [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.
Lei Zhang8f6ba142015-11-06 15:09:04 -050065 if (position->index >= text->length) return SPV_END_OF_STREAM;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040066 switch (text->str[position->index]) {
67 case '\0':
68 return SPV_END_OF_STREAM;
69 case ';':
70 if (spv_result_t error = advanceLine(text, position)) return error;
71 return advance(text, position);
72 case ' ':
73 case '\t':
Dejan Mircevskia1de2b32016-04-01 00:47:02 -040074 case '\r':
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040075 position->column++;
76 position->index++;
77 return advance(text, position);
78 case '\n':
79 position->column = 0;
80 position->line++;
81 position->index++;
82 return advance(text, position);
83 default:
84 break;
85 }
86 return SPV_SUCCESS;
87}
88
Lei Zhang6032b982016-03-15 16:52:40 -040089// Fetches the next word from the given text stream starting from the given
90// *position. On success, writes the decoded word into *word and updates
91// *position to the location past the returned word.
92//
93// A word ends at the next comment or whitespace. However, double-quoted
94// strings remain intact, and a backslash always escapes the next character.
95spv_result_t getWord(spv_text text, spv_position position, std::string* word) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040096 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
Lei Zhang6032b982016-03-15 16:52:40 -040097 if (!position) return SPV_ERROR_INVALID_POINTER;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040098
Lei Zhang6032b982016-03-15 16:52:40 -040099 const size_t start_index = position->index;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400100
101 bool quoting = false;
102 bool escaping = false;
103
104 // NOTE: Assumes first character is not white space!
105 while (true) {
Lei Zhang6032b982016-03-15 16:52:40 -0400106 if (position->index >= text->length) {
107 word->assign(text->str + start_index, text->str + position->index);
Lei Zhang9413fbb2016-02-22 17:17:25 -0500108 return SPV_SUCCESS;
109 }
Lei Zhang6032b982016-03-15 16:52:40 -0400110 const char ch = text->str[position->index];
dan sinclair9991d662018-08-07 09:09:47 -0400111 if (ch == '\\') {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400112 escaping = !escaping;
dan sinclair9991d662018-08-07 09:09:47 -0400113 } else {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400114 switch (ch) {
115 case '"':
116 if (!escaping) quoting = !quoting;
117 break;
118 case ' ':
119 case ';':
120 case '\t':
121 case '\n':
Dejan Mircevskia1de2b32016-04-01 00:47:02 -0400122 case '\r':
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400123 if (escaping || quoting) break;
David Neto9529d3c2021-10-01 08:43:59 -0400124 word->assign(text->str + start_index, text->str + position->index);
125 return SPV_SUCCESS;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400126 case '\0': { // NOTE: End of word found!
Lei Zhang6032b982016-03-15 16:52:40 -0400127 word->assign(text->str + start_index, text->str + position->index);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400128 return SPV_SUCCESS;
129 }
130 default:
131 break;
132 }
133 escaping = false;
134 }
135
Lei Zhang6032b982016-03-15 16:52:40 -0400136 position->column++;
137 position->index++;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400138 }
139}
140
141// Returns true if the characters in the text as position represent
142// the start of an Opcode.
143bool startsWithOp(spv_text text, spv_position position) {
144 if (text->length < position->index + 3) return false;
145 char ch0 = text->str[position->index];
146 char ch1 = text->str[position->index + 1];
147 char ch2 = text->str[position->index + 2];
148 return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
149}
150
dan sinclair3dad1cd2018-07-07 09:38:00 -0400151} // namespace
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400152
David Neto78e677b2015-10-05 13:28:46 -0400153const IdType kUnknownType = {0, false, IdTypeClass::kBottom};
154
David Neto78e677b2015-10-05 13:28:46 -0400155// TODO(dneto): Reorder AssemblyContext definitions to match declaration order.
156
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400157// This represents all of the data that is only valid for the duration of
158// a single compilation.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500159uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) {
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400160 if (!ids_to_preserve_.empty()) {
161 uint32_t id = 0;
dan sinclair76e0bde2018-07-06 13:25:17 -0400162 if (spvtools::utils::ParseNumber(textValue, &id)) {
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400163 if (ids_to_preserve_.find(id) != ids_to_preserve_.end()) {
164 bound_ = std::max(bound_, id + 1);
165 return id;
166 }
167 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400168 }
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400169
170 const auto it = named_ids_.find(textValue);
171 if (it == named_ids_.end()) {
172 uint32_t id = next_id_++;
173 if (!ids_to_preserve_.empty()) {
174 while (ids_to_preserve_.find(id) != ids_to_preserve_.end()) {
175 id = next_id_++;
176 }
177 }
178
179 named_ids_.emplace(textValue, id);
180 bound_ = std::max(bound_, id + 1);
181 return id;
182 }
183
184 return it->second;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400185}
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400186
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400187uint32_t AssemblyContext::getBound() const { return bound_; }
188
189spv_result_t AssemblyContext::advance() {
dan sinclair3dad1cd2018-07-07 09:38:00 -0400190 return spvtools::advance(text_, &current_position_);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400191}
192
Lei Zhang6032b982016-03-15 16:52:40 -0400193spv_result_t AssemblyContext::getWord(std::string* word,
194 spv_position next_position) {
195 *next_position = current_position_;
dan sinclair3dad1cd2018-07-07 09:38:00 -0400196 return spvtools::getWord(text_, next_position, word);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400197}
198
199bool AssemblyContext::startsWithOp() {
dan sinclair3dad1cd2018-07-07 09:38:00 -0400200 return spvtools::startsWithOp(text_, &current_position_);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400201}
202
203bool AssemblyContext::isStartOfNewInst() {
Lei Zhang6032b982016-03-15 16:52:40 -0400204 spv_position_t pos = current_position_;
dan sinclair3dad1cd2018-07-07 09:38:00 -0400205 if (spvtools::advance(text_, &pos)) return false;
206 if (spvtools::startsWithOp(text_, &pos)) return true;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400207
208 std::string word;
Lei Zhang6032b982016-03-15 16:52:40 -0400209 pos = current_position_;
dan sinclair3dad1cd2018-07-07 09:38:00 -0400210 if (spvtools::getWord(text_, &pos, &word)) return false;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400211 if ('%' != word.front()) return false;
212
dan sinclair3dad1cd2018-07-07 09:38:00 -0400213 if (spvtools::advance(text_, &pos)) return false;
214 if (spvtools::getWord(text_, &pos, &word)) return false;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400215 if ("=" != word) return false;
216
dan sinclair3dad1cd2018-07-07 09:38:00 -0400217 if (spvtools::advance(text_, &pos)) return false;
218 if (spvtools::startsWithOp(text_, &pos)) return true;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400219 return false;
220}
Andrew Woloszyn4274f932015-10-20 09:12:32 -0400221
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400222char AssemblyContext::peek() const {
223 return text_->str[current_position_.index];
224}
225
226bool AssemblyContext::hasText() const {
227 return text_->length > current_position_.index;
228}
Andrew Woloszyn4274f932015-10-20 09:12:32 -0400229
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400230void AssemblyContext::seekForward(uint32_t size) {
231 current_position_.index += size;
232 current_position_.column += size;
233}
234
235spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500236 spv_instruction_t* pInst) {
Andrew Woloszyn4ddb4312016-02-17 13:00:23 -0500237 pInst->words.insert(pInst->words.end(), value);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400238 return SPV_SUCCESS;
239}
240
David Neto78e677b2015-10-05 13:28:46 -0400241spv_result_t AssemblyContext::binaryEncodeNumericLiteral(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500242 const char* val, spv_result_t error_code, const IdType& type,
243 spv_instruction_t* pInst) {
dan sinclair76e0bde2018-07-06 13:25:17 -0400244 using spvtools::utils::EncodeNumberStatus;
qining1773b952016-09-01 14:27:04 -0400245 // Populate the NumberType from the IdType for parsing.
dan sinclair76e0bde2018-07-06 13:25:17 -0400246 spvtools::utils::NumberType number_type;
qining1773b952016-09-01 14:27:04 -0400247 switch (type.type_class) {
248 case IdTypeClass::kOtherType:
249 return diagnostic(SPV_ERROR_INTERNAL)
250 << "Unexpected numeric literal type";
251 case IdTypeClass::kScalarIntegerType:
252 if (type.isSigned) {
253 number_type = {type.bitwidth, SPV_NUMBER_SIGNED_INT};
254 } else {
255 number_type = {type.bitwidth, SPV_NUMBER_UNSIGNED_INT};
256 }
257 break;
258 case IdTypeClass::kScalarFloatType:
259 number_type = {type.bitwidth, SPV_NUMBER_FLOATING};
260 break;
261 case IdTypeClass::kBottom:
262 // kBottom means the type is unknown and we need to infer the type before
263 // parsing the number. The rule is: If there is a decimal point, treat
264 // the value as a floating point value, otherwise a integer value, then
265 // if the first char of the integer text is '-', treat the integer as a
266 // signed integer, otherwise an unsigned integer.
267 uint32_t bitwidth = static_cast<uint32_t>(assumedBitWidth(type));
268 if (strchr(val, '.')) {
269 number_type = {bitwidth, SPV_NUMBER_FLOATING};
270 } else if (type.isSigned || val[0] == '-') {
271 number_type = {bitwidth, SPV_NUMBER_SIGNED_INT};
272 } else {
273 number_type = {bitwidth, SPV_NUMBER_UNSIGNED_INT};
274 }
275 break;
David Neto78e677b2015-10-05 13:28:46 -0400276 }
277
qining1773b952016-09-01 14:27:04 -0400278 std::string error_msg;
279 EncodeNumberStatus parse_status = ParseAndEncodeNumber(
280 val, number_type,
281 [this, pInst](uint32_t d) { this->binaryEncodeU32(d, pInst); },
282 &error_msg);
283 switch (parse_status) {
284 case EncodeNumberStatus::kSuccess:
285 return SPV_SUCCESS;
286 case EncodeNumberStatus::kInvalidText:
287 return diagnostic(error_code) << error_msg;
288 case EncodeNumberStatus::kUnsupported:
289 return diagnostic(SPV_ERROR_INTERNAL) << error_msg;
290 case EncodeNumberStatus::kInvalidUsage:
291 return diagnostic(SPV_ERROR_INVALID_TEXT) << error_msg;
292 }
293 // This line is not reachable, only added to satisfy the compiler.
294 return diagnostic(SPV_ERROR_INTERNAL)
295 << "Unexpected result code from ParseAndEncodeNumber()";
David Neto78e677b2015-10-05 13:28:46 -0400296}
297
Lei Zhang1a0334e2015-11-02 09:41:20 -0500298spv_result_t AssemblyContext::binaryEncodeString(const char* value,
299 spv_instruction_t* pInst) {
David Netob5dc8fc2015-10-06 16:22:00 -0400300 const size_t length = strlen(value);
301 const size_t wordCount = (length / 4) + 1;
302 const size_t oldWordCount = pInst->words.size();
303 const size_t newWordCount = oldWordCount + wordCount;
304
305 // TODO(dneto): We can just defer this check until later.
306 if (newWordCount > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Neto78e677b2015-10-05 13:28:46 -0400307 return diagnostic() << "Instruction too long: more than "
308 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400309 }
310
Marius Hillenbrand1ed847f2021-12-08 18:01:26 +0100311 pInst->words.reserve(newWordCount);
312 spvtools::utils::AppendToVector(value, &pInst->words);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400313
314 return SPV_SUCCESS;
315}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400316
317spv_result_t AssemblyContext::recordTypeDefinition(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500318 const spv_instruction_t* pInst) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400319 uint32_t value = pInst->words[1];
320 if (types_.find(value) != types_.end()) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500321 return diagnostic() << "Value " << value
322 << " has already been used to generate a type";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400323 }
324
Lei Zhangb36e7042015-10-28 13:40:52 -0400325 if (pInst->opcode == SpvOpTypeInt) {
David Neto78e677b2015-10-05 13:28:46 -0400326 if (pInst->words.size() != 4)
327 return diagnostic() << "Invalid OpTypeInt instruction";
328 types_[value] = {pInst->words[2], pInst->words[3] != 0,
329 IdTypeClass::kScalarIntegerType};
Lei Zhangb36e7042015-10-28 13:40:52 -0400330 } else if (pInst->opcode == SpvOpTypeFloat) {
David Neto78e677b2015-10-05 13:28:46 -0400331 if (pInst->words.size() != 3)
332 return diagnostic() << "Invalid OpTypeFloat instruction";
333 types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400334 } else {
David Neto78e677b2015-10-05 13:28:46 -0400335 types_[value] = {0, false, IdTypeClass::kOtherType};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400336 }
337 return SPV_SUCCESS;
338}
339
340IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const {
341 auto type = types_.find(value);
342 if (type == types_.end()) {
David Neto78e677b2015-10-05 13:28:46 -0400343 return kUnknownType;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400344 }
345 return std::get<1>(*type);
346}
347
348IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const {
349 auto type_value = value_types_.find(value);
350 if (type_value == value_types_.end()) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500351 return {0, false, IdTypeClass::kBottom};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400352 }
353 return getTypeOfTypeGeneratingValue(std::get<1>(*type_value));
354}
355
356spv_result_t AssemblyContext::recordTypeIdForValue(uint32_t value,
357 uint32_t type) {
358 bool successfully_inserted = false;
359 std::tie(std::ignore, successfully_inserted) =
360 value_types_.insert(std::make_pair(value, type));
David Neto78e677b2015-10-05 13:28:46 -0400361 if (!successfully_inserted)
362 return diagnostic() << "Value is being defined a second time";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400363 return SPV_SUCCESS;
364}
365
David Neto2ae4a682015-11-09 18:55:42 -0500366spv_result_t AssemblyContext::recordIdAsExtInstImport(
367 uint32_t id, spv_ext_inst_type_t type) {
368 bool successfully_inserted = false;
369 std::tie(std::ignore, successfully_inserted) =
370 import_id_to_ext_inst_type_.insert(std::make_pair(id, type));
371 if (!successfully_inserted)
372 return diagnostic() << "Import Id is being defined a second time";
373 return SPV_SUCCESS;
374}
375
376spv_ext_inst_type_t AssemblyContext::getExtInstTypeForId(uint32_t id) const {
377 auto type = import_id_to_ext_inst_type_.find(id);
378 if (type == import_id_to_ext_inst_type_.end()) {
379 return SPV_EXT_INST_TYPE_NONE;
380 }
381 return std::get<1>(*type);
382}
383
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400384std::set<uint32_t> AssemblyContext::GetNumericIds() const {
385 std::set<uint32_t> ids;
386 for (const auto& kv : named_ids_) {
387 uint32_t id;
dan sinclair76e0bde2018-07-06 13:25:17 -0400388 if (spvtools::utils::ParseNumber(kv.first.c_str(), &id)) ids.insert(id);
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400389 }
390 return ids;
391}
392
dan sinclair3dad1cd2018-07-07 09:38:00 -0400393} // namespace spvtools