blob: 1806926ef3f0a6a615fa37f7887f2c3fd2a83c07 [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
15#include "text_handler.h"
16
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
David Netofcc7d582015-10-27 15:31:10 -040023#include "assembly_grammar.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040024#include "binary.h"
25#include "ext_inst.h"
David Netob5dc8fc2015-10-06 16:22:00 -040026#include "instruction.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040027#include "opcode.h"
28#include "text.h"
Andrew Woloszynf731cbf2015-10-23 09:53:58 -040029#include "util/bitutils.h"
David Neto9e545d72015-11-06 18:08:49 -050030#include "util/hex_float.h"
qining1773b952016-09-01 14:27:04 -040031#include "util/parse_number.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040032
33namespace {
Lei Zhangf2cf7192016-04-21 15:50:23 -040034// Advances |text| to the start of the next line and writes the new position to
35// |position|.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040036spv_result_t advanceLine(spv_text text, spv_position position) {
37 while (true) {
Lei Zhangf2cf7192016-04-21 15:50:23 -040038 if (position->index >= text->length) return SPV_END_OF_STREAM;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040039 switch (text->str[position->index]) {
40 case '\0':
41 return SPV_END_OF_STREAM;
42 case '\n':
43 position->column = 0;
44 position->line++;
45 position->index++;
46 return SPV_SUCCESS;
47 default:
48 position->column++;
49 position->index++;
50 break;
51 }
52 }
53}
54
Lei Zhangf2cf7192016-04-21 15:50:23 -040055// Advances |text| to first non white space character and writes the new
56// position to |position|.
57// If a null terminator is found during the text advance, SPV_END_OF_STREAM is
58// returned, SPV_SUCCESS otherwise. No error checking is performed on the
59// parameters, its the users responsibility to ensure these are non null.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040060spv_result_t advance(spv_text text, spv_position position) {
61 // NOTE: Consume white space, otherwise don't advance.
Lei Zhang8f6ba142015-11-06 15:09:04 -050062 if (position->index >= text->length) return SPV_END_OF_STREAM;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040063 switch (text->str[position->index]) {
64 case '\0':
65 return SPV_END_OF_STREAM;
66 case ';':
67 if (spv_result_t error = advanceLine(text, position)) return error;
68 return advance(text, position);
69 case ' ':
70 case '\t':
Dejan Mircevskia1de2b32016-04-01 00:47:02 -040071 case '\r':
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040072 position->column++;
73 position->index++;
74 return advance(text, position);
75 case '\n':
76 position->column = 0;
77 position->line++;
78 position->index++;
79 return advance(text, position);
80 default:
81 break;
82 }
83 return SPV_SUCCESS;
84}
85
Lei Zhang6032b982016-03-15 16:52:40 -040086// Fetches the next word from the given text stream starting from the given
87// *position. On success, writes the decoded word into *word and updates
88// *position to the location past the returned word.
89//
90// A word ends at the next comment or whitespace. However, double-quoted
91// strings remain intact, and a backslash always escapes the next character.
92spv_result_t getWord(spv_text text, spv_position position, std::string* word) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040093 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
Lei Zhang6032b982016-03-15 16:52:40 -040094 if (!position) return SPV_ERROR_INVALID_POINTER;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040095
Lei Zhang6032b982016-03-15 16:52:40 -040096 const size_t start_index = position->index;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040097
98 bool quoting = false;
99 bool escaping = false;
100
101 // NOTE: Assumes first character is not white space!
102 while (true) {
Lei Zhang6032b982016-03-15 16:52:40 -0400103 if (position->index >= text->length) {
104 word->assign(text->str + start_index, text->str + position->index);
Lei Zhang9413fbb2016-02-22 17:17:25 -0500105 return SPV_SUCCESS;
106 }
Lei Zhang6032b982016-03-15 16:52:40 -0400107 const char ch = text->str[position->index];
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400108 if (ch == '\\')
109 escaping = !escaping;
110 else {
111 switch (ch) {
112 case '"':
113 if (!escaping) quoting = !quoting;
114 break;
115 case ' ':
116 case ';':
117 case '\t':
118 case '\n':
Dejan Mircevskia1de2b32016-04-01 00:47:02 -0400119 case '\r':
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400120 if (escaping || quoting) break;
121 // Fall through.
122 case '\0': { // NOTE: End of word found!
Lei Zhang6032b982016-03-15 16:52:40 -0400123 word->assign(text->str + start_index, text->str + position->index);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400124 return SPV_SUCCESS;
125 }
126 default:
127 break;
128 }
129 escaping = false;
130 }
131
Lei Zhang6032b982016-03-15 16:52:40 -0400132 position->column++;
133 position->index++;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400134 }
135}
136
137// Returns true if the characters in the text as position represent
138// the start of an Opcode.
139bool startsWithOp(spv_text text, spv_position position) {
140 if (text->length < position->index + 3) return false;
141 char ch0 = text->str[position->index];
142 char ch1 = text->str[position->index + 1];
143 char ch2 = text->str[position->index + 2];
144 return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
145}
146
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400147} // anonymous namespace
148
149namespace libspirv {
150
David Neto78e677b2015-10-05 13:28:46 -0400151const IdType kUnknownType = {0, false, IdTypeClass::kBottom};
152
David Neto78e677b2015-10-05 13:28:46 -0400153// TODO(dneto): Reorder AssemblyContext definitions to match declaration order.
154
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400155// This represents all of the data that is only valid for the duration of
156// a single compilation.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500157uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) {
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400158 if (!ids_to_preserve_.empty()) {
159 uint32_t id = 0;
160 if (spvutils::ParseNumber(textValue, &id)) {
161 if (ids_to_preserve_.find(id) != ids_to_preserve_.end()) {
162 bound_ = std::max(bound_, id + 1);
163 return id;
164 }
165 }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400166 }
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400167
168 const auto it = named_ids_.find(textValue);
169 if (it == named_ids_.end()) {
170 uint32_t id = next_id_++;
171 if (!ids_to_preserve_.empty()) {
172 while (ids_to_preserve_.find(id) != ids_to_preserve_.end()) {
173 id = next_id_++;
174 }
175 }
176
177 named_ids_.emplace(textValue, id);
178 bound_ = std::max(bound_, id + 1);
179 return id;
180 }
181
182 return it->second;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400183}
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400184
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400185uint32_t AssemblyContext::getBound() const { return bound_; }
186
187spv_result_t AssemblyContext::advance() {
188 return ::advance(text_, &current_position_);
189}
190
Lei Zhang6032b982016-03-15 16:52:40 -0400191spv_result_t AssemblyContext::getWord(std::string* word,
192 spv_position next_position) {
193 *next_position = current_position_;
194 return ::getWord(text_, next_position, word);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400195}
196
197bool AssemblyContext::startsWithOp() {
198 return ::startsWithOp(text_, &current_position_);
199}
200
201bool AssemblyContext::isStartOfNewInst() {
Lei Zhang6032b982016-03-15 16:52:40 -0400202 spv_position_t pos = current_position_;
203 if (::advance(text_, &pos)) return false;
204 if (::startsWithOp(text_, &pos)) return true;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400205
206 std::string word;
Lei Zhang6032b982016-03-15 16:52:40 -0400207 pos = current_position_;
208 if (::getWord(text_, &pos, &word)) return false;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400209 if ('%' != word.front()) return false;
210
Lei Zhang6032b982016-03-15 16:52:40 -0400211 if (::advance(text_, &pos)) return false;
212 if (::getWord(text_, &pos, &word)) return false;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400213 if ("=" != word) return false;
214
Lei Zhang6032b982016-03-15 16:52:40 -0400215 if (::advance(text_, &pos)) return false;
216 if (::startsWithOp(text_, &pos)) return true;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400217 return false;
218}
Andrew Woloszyn4274f932015-10-20 09:12:32 -0400219
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400220char AssemblyContext::peek() const {
221 return text_->str[current_position_.index];
222}
223
224bool AssemblyContext::hasText() const {
225 return text_->length > current_position_.index;
226}
Andrew Woloszyn4274f932015-10-20 09:12:32 -0400227
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400228void AssemblyContext::seekForward(uint32_t size) {
229 current_position_.index += size;
230 current_position_.column += size;
231}
232
233spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500234 spv_instruction_t* pInst) {
Andrew Woloszyn4ddb4312016-02-17 13:00:23 -0500235 pInst->words.insert(pInst->words.end(), value);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400236 return SPV_SUCCESS;
237}
238
David Neto78e677b2015-10-05 13:28:46 -0400239spv_result_t AssemblyContext::binaryEncodeNumericLiteral(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500240 const char* val, spv_result_t error_code, const IdType& type,
241 spv_instruction_t* pInst) {
qining1773b952016-09-01 14:27:04 -0400242 using spvutils::EncodeNumberStatus;
243 // Populate the NumberType from the IdType for parsing.
244 spvutils::NumberType number_type;
245 switch (type.type_class) {
246 case IdTypeClass::kOtherType:
247 return diagnostic(SPV_ERROR_INTERNAL)
248 << "Unexpected numeric literal type";
249 case IdTypeClass::kScalarIntegerType:
250 if (type.isSigned) {
251 number_type = {type.bitwidth, SPV_NUMBER_SIGNED_INT};
252 } else {
253 number_type = {type.bitwidth, SPV_NUMBER_UNSIGNED_INT};
254 }
255 break;
256 case IdTypeClass::kScalarFloatType:
257 number_type = {type.bitwidth, SPV_NUMBER_FLOATING};
258 break;
259 case IdTypeClass::kBottom:
260 // kBottom means the type is unknown and we need to infer the type before
261 // parsing the number. The rule is: If there is a decimal point, treat
262 // the value as a floating point value, otherwise a integer value, then
263 // if the first char of the integer text is '-', treat the integer as a
264 // signed integer, otherwise an unsigned integer.
265 uint32_t bitwidth = static_cast<uint32_t>(assumedBitWidth(type));
266 if (strchr(val, '.')) {
267 number_type = {bitwidth, SPV_NUMBER_FLOATING};
268 } else if (type.isSigned || val[0] == '-') {
269 number_type = {bitwidth, SPV_NUMBER_SIGNED_INT};
270 } else {
271 number_type = {bitwidth, SPV_NUMBER_UNSIGNED_INT};
272 }
273 break;
David Neto78e677b2015-10-05 13:28:46 -0400274 }
275
qining1773b952016-09-01 14:27:04 -0400276 std::string error_msg;
277 EncodeNumberStatus parse_status = ParseAndEncodeNumber(
278 val, number_type,
279 [this, pInst](uint32_t d) { this->binaryEncodeU32(d, pInst); },
280 &error_msg);
281 switch (parse_status) {
282 case EncodeNumberStatus::kSuccess:
283 return SPV_SUCCESS;
284 case EncodeNumberStatus::kInvalidText:
285 return diagnostic(error_code) << error_msg;
286 case EncodeNumberStatus::kUnsupported:
287 return diagnostic(SPV_ERROR_INTERNAL) << error_msg;
288 case EncodeNumberStatus::kInvalidUsage:
289 return diagnostic(SPV_ERROR_INVALID_TEXT) << error_msg;
290 }
291 // This line is not reachable, only added to satisfy the compiler.
292 return diagnostic(SPV_ERROR_INTERNAL)
293 << "Unexpected result code from ParseAndEncodeNumber()";
David Neto78e677b2015-10-05 13:28:46 -0400294}
295
Lei Zhang1a0334e2015-11-02 09:41:20 -0500296spv_result_t AssemblyContext::binaryEncodeString(const char* value,
297 spv_instruction_t* pInst) {
David Netob5dc8fc2015-10-06 16:22:00 -0400298 const size_t length = strlen(value);
299 const size_t wordCount = (length / 4) + 1;
300 const size_t oldWordCount = pInst->words.size();
301 const size_t newWordCount = oldWordCount + wordCount;
302
303 // TODO(dneto): We can just defer this check until later.
304 if (newWordCount > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) {
David Neto78e677b2015-10-05 13:28:46 -0400305 return diagnostic() << "Instruction too long: more than "
306 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400307 }
308
David Netob5dc8fc2015-10-06 16:22:00 -0400309 pInst->words.resize(newWordCount);
310
311 // Make sure all the bytes in the last word are 0, in case we only
312 // write a partial word at the end.
313 pInst->words.back() = 0;
314
Lei Zhang1a0334e2015-11-02 09:41:20 -0500315 char* dest = (char*)&pInst->words[oldWordCount];
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400316 strncpy(dest, value, length);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400317
318 return SPV_SUCCESS;
319}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400320
321spv_result_t AssemblyContext::recordTypeDefinition(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500322 const spv_instruction_t* pInst) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400323 uint32_t value = pInst->words[1];
324 if (types_.find(value) != types_.end()) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500325 return diagnostic() << "Value " << value
326 << " has already been used to generate a type";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400327 }
328
Lei Zhangb36e7042015-10-28 13:40:52 -0400329 if (pInst->opcode == SpvOpTypeInt) {
David Neto78e677b2015-10-05 13:28:46 -0400330 if (pInst->words.size() != 4)
331 return diagnostic() << "Invalid OpTypeInt instruction";
332 types_[value] = {pInst->words[2], pInst->words[3] != 0,
333 IdTypeClass::kScalarIntegerType};
Lei Zhangb36e7042015-10-28 13:40:52 -0400334 } else if (pInst->opcode == SpvOpTypeFloat) {
David Neto78e677b2015-10-05 13:28:46 -0400335 if (pInst->words.size() != 3)
336 return diagnostic() << "Invalid OpTypeFloat instruction";
337 types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400338 } else {
David Neto78e677b2015-10-05 13:28:46 -0400339 types_[value] = {0, false, IdTypeClass::kOtherType};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400340 }
341 return SPV_SUCCESS;
342}
343
344IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const {
345 auto type = types_.find(value);
346 if (type == types_.end()) {
David Neto78e677b2015-10-05 13:28:46 -0400347 return kUnknownType;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400348 }
349 return std::get<1>(*type);
350}
351
352IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const {
353 auto type_value = value_types_.find(value);
354 if (type_value == value_types_.end()) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500355 return {0, false, IdTypeClass::kBottom};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400356 }
357 return getTypeOfTypeGeneratingValue(std::get<1>(*type_value));
358}
359
360spv_result_t AssemblyContext::recordTypeIdForValue(uint32_t value,
361 uint32_t type) {
362 bool successfully_inserted = false;
363 std::tie(std::ignore, successfully_inserted) =
364 value_types_.insert(std::make_pair(value, type));
David Neto78e677b2015-10-05 13:28:46 -0400365 if (!successfully_inserted)
366 return diagnostic() << "Value is being defined a second time";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400367 return SPV_SUCCESS;
368}
369
David Neto2ae4a682015-11-09 18:55:42 -0500370spv_result_t AssemblyContext::recordIdAsExtInstImport(
371 uint32_t id, spv_ext_inst_type_t type) {
372 bool successfully_inserted = false;
373 std::tie(std::ignore, successfully_inserted) =
374 import_id_to_ext_inst_type_.insert(std::make_pair(id, type));
375 if (!successfully_inserted)
376 return diagnostic() << "Import Id is being defined a second time";
377 return SPV_SUCCESS;
378}
379
380spv_ext_inst_type_t AssemblyContext::getExtInstTypeForId(uint32_t id) const {
381 auto type = import_id_to_ext_inst_type_.find(id);
382 if (type == import_id_to_ext_inst_type_.end()) {
383 return SPV_EXT_INST_TYPE_NONE;
384 }
385 return std::get<1>(*type);
386}
387
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400388std::set<uint32_t> AssemblyContext::GetNumericIds() const {
389 std::set<uint32_t> ids;
390 for (const auto& kv : named_ids_) {
391 uint32_t id;
392 if (spvutils::ParseNumber(kv.first.c_str(), &id))
393 ids.insert(id);
394 }
395 return ids;
396}
397
Lei Zhang1a0334e2015-11-02 09:41:20 -0500398} // namespace libspirv