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 <tuple>
22
23 #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"
32
33 namespace spvtools {
34 namespace {
35
36 // Advances |text| to the start of the next line and writes the new position to
37 // |position|.
advanceLine(spv_text text,spv_position position)38 spv_result_t advanceLine(spv_text text, spv_position position) {
39 while (true) {
40 if (position->index >= text->length) return SPV_END_OF_STREAM;
41 switch (text->str[position->index]) {
42 case '\0':
43 return SPV_END_OF_STREAM;
44 case '\n':
45 position->column = 0;
46 position->line++;
47 position->index++;
48 return SPV_SUCCESS;
49 default:
50 position->column++;
51 position->index++;
52 break;
53 }
54 }
55 }
56
57 // Advances |text| to first non white space character and writes the new
58 // position to |position|.
59 // If a null terminator is found during the text advance, SPV_END_OF_STREAM is
60 // returned, SPV_SUCCESS otherwise. No error checking is performed on the
61 // parameters, its the users responsibility to ensure these are non null.
advance(spv_text text,spv_position position)62 spv_result_t advance(spv_text text, spv_position position) {
63 // NOTE: Consume white space, otherwise don't advance.
64 if (position->index >= text->length) return SPV_END_OF_STREAM;
65 switch (text->str[position->index]) {
66 case '\0':
67 return SPV_END_OF_STREAM;
68 case ';':
69 if (spv_result_t error = advanceLine(text, position)) return error;
70 return advance(text, position);
71 case ' ':
72 case '\t':
73 case '\r':
74 position->column++;
75 position->index++;
76 return advance(text, position);
77 case '\n':
78 position->column = 0;
79 position->line++;
80 position->index++;
81 return advance(text, position);
82 default:
83 break;
84 }
85 return SPV_SUCCESS;
86 }
87
88 // Fetches the next word from the given text stream starting from the given
89 // *position. On success, writes the decoded word into *word and updates
90 // *position to the location past the returned word.
91 //
92 // A word ends at the next comment or whitespace. However, double-quoted
93 // strings remain intact, and a backslash always escapes the next character.
getWord(spv_text text,spv_position position,std::string * word)94 spv_result_t getWord(spv_text text, spv_position position, std::string* word) {
95 if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT;
96 if (!position) return SPV_ERROR_INVALID_POINTER;
97
98 const size_t start_index = position->index;
99
100 bool quoting = false;
101 bool escaping = false;
102
103 // NOTE: Assumes first character is not white space!
104 while (true) {
105 if (position->index >= text->length) {
106 word->assign(text->str + start_index, text->str + position->index);
107 return SPV_SUCCESS;
108 }
109 const char ch = text->str[position->index];
110 if (ch == '\\') {
111 escaping = !escaping;
112 } else {
113 switch (ch) {
114 case '"':
115 if (!escaping) quoting = !quoting;
116 break;
117 case ' ':
118 case ';':
119 case '\t':
120 case '\n':
121 case '\r':
122 if (escaping || quoting) break;
123 // Fall through.
124 case '\0': { // NOTE: End of word found!
125 word->assign(text->str + start_index, text->str + position->index);
126 return SPV_SUCCESS;
127 }
128 default:
129 break;
130 }
131 escaping = false;
132 }
133
134 position->column++;
135 position->index++;
136 }
137 }
138
139 // Returns true if the characters in the text as position represent
140 // the start of an Opcode.
startsWithOp(spv_text text,spv_position position)141 bool startsWithOp(spv_text text, spv_position position) {
142 if (text->length < position->index + 3) return false;
143 char ch0 = text->str[position->index];
144 char ch1 = text->str[position->index + 1];
145 char ch2 = text->str[position->index + 2];
146 return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z'));
147 }
148
149 } // namespace
150
151 const IdType kUnknownType = {0, false, IdTypeClass::kBottom};
152
153 // TODO(dneto): Reorder AssemblyContext definitions to match declaration order.
154
155 // This represents all of the data that is only valid for the duration of
156 // a single compilation.
spvNamedIdAssignOrGet(const char * textValue)157 uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) {
158 if (!ids_to_preserve_.empty()) {
159 uint32_t id = 0;
160 if (spvtools::utils::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 }
166 }
167
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;
183 }
184
getBound() const185 uint32_t AssemblyContext::getBound() const { return bound_; }
186
advance()187 spv_result_t AssemblyContext::advance() {
188 return spvtools::advance(text_, ¤t_position_);
189 }
190
getWord(std::string * word,spv_position next_position)191 spv_result_t AssemblyContext::getWord(std::string* word,
192 spv_position next_position) {
193 *next_position = current_position_;
194 return spvtools::getWord(text_, next_position, word);
195 }
196
startsWithOp()197 bool AssemblyContext::startsWithOp() {
198 return spvtools::startsWithOp(text_, ¤t_position_);
199 }
200
isStartOfNewInst()201 bool AssemblyContext::isStartOfNewInst() {
202 spv_position_t pos = current_position_;
203 if (spvtools::advance(text_, &pos)) return false;
204 if (spvtools::startsWithOp(text_, &pos)) return true;
205
206 std::string word;
207 pos = current_position_;
208 if (spvtools::getWord(text_, &pos, &word)) return false;
209 if ('%' != word.front()) return false;
210
211 if (spvtools::advance(text_, &pos)) return false;
212 if (spvtools::getWord(text_, &pos, &word)) return false;
213 if ("=" != word) return false;
214
215 if (spvtools::advance(text_, &pos)) return false;
216 if (spvtools::startsWithOp(text_, &pos)) return true;
217 return false;
218 }
219
peek() const220 char AssemblyContext::peek() const {
221 return text_->str[current_position_.index];
222 }
223
hasText() const224 bool AssemblyContext::hasText() const {
225 return text_->length > current_position_.index;
226 }
227
seekForward(uint32_t size)228 void AssemblyContext::seekForward(uint32_t size) {
229 current_position_.index += size;
230 current_position_.column += size;
231 }
232
binaryEncodeU32(const uint32_t value,spv_instruction_t * pInst)233 spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value,
234 spv_instruction_t* pInst) {
235 pInst->words.insert(pInst->words.end(), value);
236 return SPV_SUCCESS;
237 }
238
binaryEncodeNumericLiteral(const char * val,spv_result_t error_code,const IdType & type,spv_instruction_t * pInst)239 spv_result_t AssemblyContext::binaryEncodeNumericLiteral(
240 const char* val, spv_result_t error_code, const IdType& type,
241 spv_instruction_t* pInst) {
242 using spvtools::utils::EncodeNumberStatus;
243 // Populate the NumberType from the IdType for parsing.
244 spvtools::utils::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;
274 }
275
276 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()";
294 }
295
binaryEncodeString(const char * value,spv_instruction_t * pInst)296 spv_result_t AssemblyContext::binaryEncodeString(const char* value,
297 spv_instruction_t* pInst) {
298 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) {
305 return diagnostic() << "Instruction too long: more than "
306 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words.";
307 }
308
309 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
315 char* dest = (char*)&pInst->words[oldWordCount];
316 strncpy(dest, value, length + 1);
317
318 return SPV_SUCCESS;
319 }
320
recordTypeDefinition(const spv_instruction_t * pInst)321 spv_result_t AssemblyContext::recordTypeDefinition(
322 const spv_instruction_t* pInst) {
323 uint32_t value = pInst->words[1];
324 if (types_.find(value) != types_.end()) {
325 return diagnostic() << "Value " << value
326 << " has already been used to generate a type";
327 }
328
329 if (pInst->opcode == SpvOpTypeInt) {
330 if (pInst->words.size() != 4)
331 return diagnostic() << "Invalid OpTypeInt instruction";
332 types_[value] = {pInst->words[2], pInst->words[3] != 0,
333 IdTypeClass::kScalarIntegerType};
334 } else if (pInst->opcode == SpvOpTypeFloat) {
335 if (pInst->words.size() != 3)
336 return diagnostic() << "Invalid OpTypeFloat instruction";
337 types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType};
338 } else {
339 types_[value] = {0, false, IdTypeClass::kOtherType};
340 }
341 return SPV_SUCCESS;
342 }
343
getTypeOfTypeGeneratingValue(uint32_t value) const344 IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const {
345 auto type = types_.find(value);
346 if (type == types_.end()) {
347 return kUnknownType;
348 }
349 return std::get<1>(*type);
350 }
351
getTypeOfValueInstruction(uint32_t value) const352 IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const {
353 auto type_value = value_types_.find(value);
354 if (type_value == value_types_.end()) {
355 return {0, false, IdTypeClass::kBottom};
356 }
357 return getTypeOfTypeGeneratingValue(std::get<1>(*type_value));
358 }
359
recordTypeIdForValue(uint32_t value,uint32_t type)360 spv_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));
365 if (!successfully_inserted)
366 return diagnostic() << "Value is being defined a second time";
367 return SPV_SUCCESS;
368 }
369
recordIdAsExtInstImport(uint32_t id,spv_ext_inst_type_t type)370 spv_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
getExtInstTypeForId(uint32_t id) const380 spv_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
GetNumericIds() const388 std::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 (spvtools::utils::ParseNumber(kv.first.c_str(), &id)) ids.insert(id);
393 }
394 return ids;
395 }
396
397 } // namespace spvtools
398