Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 1 | // Copyright (c) 2015 The Khronos Group Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | // copy of this software and/or associated documentation files (the |
| 5 | // "Materials"), to deal in the Materials without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Materials, and to |
| 8 | // permit persons to whom the Materials are furnished to do so, subject to |
| 9 | // the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be included |
| 12 | // in all copies or substantial portions of the Materials. |
| 13 | // |
| 14 | // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS |
| 15 | // KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS |
| 16 | // SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT |
| 17 | // https://www.khronos.org/registry/ |
| 18 | // |
| 19 | // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 26 | |
| 27 | #include "text_handler.h" |
| 28 | |
| 29 | #include <algorithm> |
| 30 | #include <cassert> |
| 31 | #include <cstring> |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 32 | #include <tuple> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 33 | |
| 34 | #include "binary.h" |
| 35 | #include "ext_inst.h" |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 36 | #include "instruction.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 37 | #include "opcode.h" |
| 38 | #include "text.h" |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | /// @brief Advance text to the start of the next line |
| 43 | /// |
| 44 | /// @param[in] text to be parsed |
| 45 | /// @param[in,out] position position text has been advanced to |
| 46 | /// |
| 47 | /// @return result code |
| 48 | spv_result_t advanceLine(spv_text text, spv_position position) { |
| 49 | while (true) { |
| 50 | switch (text->str[position->index]) { |
| 51 | case '\0': |
| 52 | return SPV_END_OF_STREAM; |
| 53 | case '\n': |
| 54 | position->column = 0; |
| 55 | position->line++; |
| 56 | position->index++; |
| 57 | return SPV_SUCCESS; |
| 58 | default: |
| 59 | position->column++; |
| 60 | position->index++; |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// @brief Advance text to first non white space character |
| 67 | /// If a null terminator is found during the text advance SPV_END_OF_STREAM is |
| 68 | /// returned, SPV_SUCCESS otherwise. No error checking is performed on the |
| 69 | /// parameters, its the users responsibility to ensure these are non null. |
| 70 | /// |
| 71 | /// @param[in] text to be parsed |
| 72 | /// @param[in,out] position text has been advanced to |
| 73 | /// |
| 74 | /// @return result code |
| 75 | spv_result_t advance(spv_text text, spv_position position) { |
| 76 | // NOTE: Consume white space, otherwise don't advance. |
| 77 | switch (text->str[position->index]) { |
| 78 | case '\0': |
| 79 | return SPV_END_OF_STREAM; |
| 80 | case ';': |
| 81 | if (spv_result_t error = advanceLine(text, position)) return error; |
| 82 | return advance(text, position); |
| 83 | case ' ': |
| 84 | case '\t': |
| 85 | position->column++; |
| 86 | position->index++; |
| 87 | return advance(text, position); |
| 88 | case '\n': |
| 89 | position->column = 0; |
| 90 | position->line++; |
| 91 | position->index++; |
| 92 | return advance(text, position); |
| 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | return SPV_SUCCESS; |
| 97 | } |
| 98 | |
| 99 | /// @brief Fetch the next word from the text stream. |
| 100 | /// |
| 101 | /// A word ends at the next comment or whitespace. However, double-quoted |
| 102 | /// strings remain intact, and a backslash always escapes the next character. |
| 103 | /// |
| 104 | /// @param[in] text stream to read from |
| 105 | /// @param[in] position current position in text stream |
| 106 | /// @param[out] word returned word |
| 107 | /// @param[out] endPosition one past the end of the returned word |
| 108 | /// |
| 109 | /// @return result code |
| 110 | spv_result_t getWord(spv_text text, spv_position position, std::string &word, |
| 111 | spv_position endPosition) { |
| 112 | if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT; |
| 113 | if (!position || !endPosition) return SPV_ERROR_INVALID_POINTER; |
| 114 | |
| 115 | *endPosition = *position; |
| 116 | |
| 117 | bool quoting = false; |
| 118 | bool escaping = false; |
| 119 | |
| 120 | // NOTE: Assumes first character is not white space! |
| 121 | while (true) { |
| 122 | const char ch = text->str[endPosition->index]; |
| 123 | if (ch == '\\') |
| 124 | escaping = !escaping; |
| 125 | else { |
| 126 | switch (ch) { |
| 127 | case '"': |
| 128 | if (!escaping) quoting = !quoting; |
| 129 | break; |
| 130 | case ' ': |
| 131 | case ';': |
| 132 | case '\t': |
| 133 | case '\n': |
| 134 | if (escaping || quoting) break; |
| 135 | // Fall through. |
| 136 | case '\0': { // NOTE: End of word found! |
| 137 | word.assign(text->str + position->index, |
| 138 | (size_t)(endPosition->index - position->index)); |
| 139 | return SPV_SUCCESS; |
| 140 | } |
| 141 | default: |
| 142 | break; |
| 143 | } |
| 144 | escaping = false; |
| 145 | } |
| 146 | |
| 147 | endPosition->column++; |
| 148 | endPosition->index++; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Returns true if the characters in the text as position represent |
| 153 | // the start of an Opcode. |
| 154 | bool startsWithOp(spv_text text, spv_position position) { |
| 155 | if (text->length < position->index + 3) return false; |
| 156 | char ch0 = text->str[position->index]; |
| 157 | char ch1 = text->str[position->index + 1]; |
| 158 | char ch2 = text->str[position->index + 2]; |
| 159 | return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z')); |
| 160 | } |
| 161 | |
| 162 | /// @brief Parses a mask expression string for the given operand type. |
| 163 | /// |
| 164 | /// A mask expression is a sequence of one or more terms separated by '|', |
| 165 | /// where each term a named enum value for the given type. No whitespace |
| 166 | /// is permitted. |
| 167 | /// |
| 168 | /// On success, the value is written to pValue. |
| 169 | /// |
| 170 | /// @param[in] operandTable operand lookup table |
| 171 | /// @param[in] type of the operand |
| 172 | /// @param[in] textValue word of text to be parsed |
| 173 | /// @param[out] pValue where the resulting value is written |
| 174 | /// |
| 175 | /// @return result code |
| 176 | spv_result_t spvTextParseMaskOperand(const spv_operand_table operandTable, |
| 177 | const spv_operand_type_t type, |
| 178 | const char *textValue, uint32_t *pValue) { |
| 179 | if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT; |
| 180 | size_t text_length = strlen(textValue); |
| 181 | if (text_length == 0) return SPV_ERROR_INVALID_TEXT; |
| 182 | const char *text_end = textValue + text_length; |
| 183 | |
| 184 | // We only support mask expressions in ASCII, so the separator value is a |
| 185 | // char. |
| 186 | const char separator = '|'; |
| 187 | |
| 188 | // Accumulate the result by interpreting one word at a time, scanning |
| 189 | // from left to right. |
| 190 | uint32_t value = 0; |
| 191 | const char *begin = textValue; // The left end of the current word. |
| 192 | const char *end = nullptr; // One character past the end of the current word. |
| 193 | do { |
| 194 | end = std::find(begin, text_end, separator); |
| 195 | |
| 196 | spv_operand_desc entry = nullptr; |
| 197 | if (spvOperandTableNameLookup(operandTable, type, begin, end - begin, |
| 198 | &entry)) { |
| 199 | return SPV_ERROR_INVALID_TEXT; |
| 200 | } |
| 201 | value |= entry->value; |
| 202 | |
| 203 | // Advance to the next word by skipping over the separator. |
| 204 | begin = end + 1; |
| 205 | } while (end != text_end); |
| 206 | |
| 207 | *pValue = value; |
| 208 | return SPV_SUCCESS; |
| 209 | } |
| 210 | |
| 211 | } // anonymous namespace |
| 212 | |
| 213 | namespace libspirv { |
| 214 | |
| 215 | bool AssemblyGrammar::isValid() const { |
| 216 | return operandTable_ && opcodeTable_ && extInstTable_; |
| 217 | } |
| 218 | |
| 219 | spv_result_t AssemblyGrammar::lookupOpcode(const char *name, |
| 220 | spv_opcode_desc *desc) const { |
| 221 | return spvOpcodeTableNameLookup(opcodeTable_, name, desc); |
| 222 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 223 | |
| 224 | spv_result_t AssemblyGrammar::lookupOpcode(Op opcode, |
| 225 | spv_opcode_desc *desc) const { |
| 226 | return spvOpcodeTableValueLookup(opcodeTable_, opcode, desc); |
| 227 | } |
| 228 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 229 | spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type, |
| 230 | const char *name, size_t name_len, |
| 231 | spv_operand_desc *desc) const { |
| 232 | return spvOperandTableNameLookup(operandTable_, type, name, name_len, desc); |
| 233 | } |
| 234 | |
| 235 | spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type, |
| 236 | const char *textValue, |
| 237 | uint32_t *pValue) const { |
| 238 | return spvTextParseMaskOperand(operandTable_, type, textValue, pValue); |
| 239 | } |
| 240 | spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type, |
| 241 | const char *textValue, |
| 242 | spv_ext_inst_desc *extInst) const { |
| 243 | return spvExtInstTableNameLookup(extInstTable_, type, textValue, extInst); |
| 244 | } |
| 245 | |
| 246 | void AssemblyGrammar::prependOperandTypesForMask( |
| 247 | const spv_operand_type_t type, const uint32_t mask, |
| 248 | spv_operand_pattern_t *pattern) const { |
| 249 | spvPrependOperandTypesForMask(operandTable_, type, mask, pattern); |
| 250 | } |
| 251 | |
| 252 | // This represents all of the data that is only valid for the duration of |
| 253 | // a single compilation. |
| 254 | uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char *textValue) { |
| 255 | if (named_ids_.end() == named_ids_.find(textValue)) { |
| 256 | named_ids_[std::string(textValue)] = bound_++; |
| 257 | } |
| 258 | return named_ids_[textValue]; |
| 259 | } |
| 260 | uint32_t AssemblyContext::getBound() const { return bound_; } |
| 261 | |
| 262 | spv_result_t AssemblyContext::advance() { |
| 263 | return ::advance(text_, ¤t_position_); |
| 264 | } |
| 265 | |
| 266 | spv_result_t AssemblyContext::getWord(std::string &word, |
| 267 | spv_position endPosition) { |
| 268 | return ::getWord(text_, ¤t_position_, word, endPosition); |
| 269 | } |
| 270 | |
| 271 | bool AssemblyContext::startsWithOp() { |
| 272 | return ::startsWithOp(text_, ¤t_position_); |
| 273 | } |
| 274 | |
| 275 | bool AssemblyContext::isStartOfNewInst() { |
| 276 | spv_position_t nextPosition = current_position_; |
| 277 | if (::advance(text_, &nextPosition)) return false; |
| 278 | if (::startsWithOp(text_, &nextPosition)) return true; |
| 279 | |
| 280 | std::string word; |
| 281 | spv_position_t startPosition = current_position_; |
| 282 | if (::getWord(text_, &startPosition, word, &nextPosition)) return false; |
| 283 | if ('%' != word.front()) return false; |
| 284 | |
| 285 | if (::advance(text_, &nextPosition)) return false; |
| 286 | startPosition = nextPosition; |
| 287 | if (::getWord(text_, &startPosition, word, &nextPosition)) return false; |
| 288 | if ("=" != word) return false; |
| 289 | |
| 290 | if (::advance(text_, &nextPosition)) return false; |
| 291 | startPosition = nextPosition; |
| 292 | if (::startsWithOp(text_, &startPosition)) return true; |
| 293 | return false; |
| 294 | } |
| 295 | char AssemblyContext::peek() const { |
| 296 | return text_->str[current_position_.index]; |
| 297 | } |
| 298 | |
| 299 | bool AssemblyContext::hasText() const { |
| 300 | return text_->length > current_position_.index; |
| 301 | } |
| 302 | std::string AssemblyContext::getWord() const { |
| 303 | size_t index = current_position_.index; |
| 304 | while (true) { |
| 305 | switch (text_->str[index]) { |
| 306 | case '\0': |
| 307 | case '\t': |
| 308 | case '\v': |
| 309 | case '\r': |
| 310 | case '\n': |
| 311 | case ' ': |
| 312 | return std::string(text_->str, text_->str + index); |
| 313 | default: |
| 314 | index++; |
| 315 | } |
| 316 | } |
| 317 | assert(0 && "Unreachable"); |
| 318 | return ""; // Make certain compilers happy. |
| 319 | } |
| 320 | |
| 321 | void AssemblyContext::seekForward(uint32_t size) { |
| 322 | current_position_.index += size; |
| 323 | current_position_.column += size; |
| 324 | } |
| 325 | |
| 326 | spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value, |
| 327 | spv_instruction_t *pInst) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 328 | spvInstructionAddWord(pInst, value); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 329 | return SPV_SUCCESS; |
| 330 | } |
| 331 | |
| 332 | spv_result_t AssemblyContext::binaryEncodeU64(const uint64_t value, |
| 333 | spv_instruction_t *pInst) { |
| 334 | uint32_t low = (uint32_t)(0x00000000ffffffff & value); |
| 335 | uint32_t high = (uint32_t)((0xffffffff00000000 & value) >> 32); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame^] | 336 | binaryEncodeU32(low, pInst); |
| 337 | binaryEncodeU32(high, pInst); |
| 338 | return SPV_SUCCESS; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | spv_result_t AssemblyContext::binaryEncodeString( |
| 342 | const char *value, spv_instruction_t *pInst) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 343 | const size_t length = strlen(value); |
| 344 | const size_t wordCount = (length / 4) + 1; |
| 345 | const size_t oldWordCount = pInst->words.size(); |
| 346 | const size_t newWordCount = oldWordCount + wordCount; |
| 347 | |
| 348 | // TODO(dneto): We can just defer this check until later. |
| 349 | if (newWordCount > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
David Neto | f6b8651 | 2015-10-08 15:20:25 -0400 | [diff] [blame] | 350 | diagnostic() << "Instruction too long: more than " |
| 351 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words."; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 352 | return SPV_ERROR_INVALID_TEXT; |
| 353 | } |
| 354 | |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 355 | pInst->words.resize(newWordCount); |
| 356 | |
| 357 | // Make sure all the bytes in the last word are 0, in case we only |
| 358 | // write a partial word at the end. |
| 359 | pInst->words.back() = 0; |
| 360 | |
| 361 | char *dest = (char *)&pInst->words[oldWordCount]; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 362 | strncpy(dest, value, length); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 363 | |
| 364 | return SPV_SUCCESS; |
| 365 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 366 | |
| 367 | spv_result_t AssemblyContext::recordTypeDefinition( |
| 368 | const spv_instruction_t *pInst) { |
| 369 | uint32_t value = pInst->words[1]; |
| 370 | if (types_.find(value) != types_.end()) { |
| 371 | diagnostic() << "Value " << value |
| 372 | << " has already been used to generate a type"; |
| 373 | return SPV_ERROR_INVALID_VALUE; |
| 374 | } |
| 375 | |
| 376 | if (pInst->opcode == OpTypeInt) { |
| 377 | if (pInst->words.size() != 4) { |
| 378 | diagnostic() << "Invalid OpTypeInt instruction"; |
| 379 | return SPV_ERROR_INVALID_VALUE; |
| 380 | } |
| 381 | types_[value] = { pInst->words[2], IdTypeClass::kScalarIntegerType }; |
| 382 | } else if (pInst->opcode == OpTypeFloat) { |
| 383 | if (pInst->words.size() != 3) { |
| 384 | diagnostic() << "Invalid OpTypeFloat instruction"; |
| 385 | return SPV_ERROR_INVALID_VALUE; |
| 386 | } |
| 387 | types_[value] = { pInst->words[2], IdTypeClass::kScalarFloatType }; |
| 388 | } else { |
| 389 | types_[value] = { 0, IdTypeClass::kOtherType }; |
| 390 | } |
| 391 | return SPV_SUCCESS; |
| 392 | } |
| 393 | |
| 394 | IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const { |
| 395 | auto type = types_.find(value); |
| 396 | if (type == types_.end()) { |
| 397 | return {0, IdTypeClass::kBottom}; |
| 398 | } |
| 399 | return std::get<1>(*type); |
| 400 | } |
| 401 | |
| 402 | IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const { |
| 403 | auto type_value = value_types_.find(value); |
| 404 | if (type_value == value_types_.end()) { |
| 405 | return { 0, IdTypeClass::kBottom}; |
| 406 | } |
| 407 | return getTypeOfTypeGeneratingValue(std::get<1>(*type_value)); |
| 408 | } |
| 409 | |
| 410 | spv_result_t AssemblyContext::recordTypeIdForValue(uint32_t value, |
| 411 | uint32_t type) { |
| 412 | bool successfully_inserted = false; |
| 413 | std::tie(std::ignore, successfully_inserted) = |
| 414 | value_types_.insert(std::make_pair(value, type)); |
| 415 | if (!successfully_inserted) { |
| 416 | diagnostic() << "Value is being defined a second time"; |
| 417 | return SPV_ERROR_INVALID_VALUE; |
| 418 | } |
| 419 | return SPV_SUCCESS; |
| 420 | } |
| 421 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 422 | } |
| 423 | |