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 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 29 | #include <cassert> |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 30 | #include <cstdlib> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 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 | |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 34 | #include "assembly_grammar.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 35 | #include "binary.h" |
| 36 | #include "ext_inst.h" |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 37 | #include "instruction.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 38 | #include "opcode.h" |
| 39 | #include "text.h" |
Andrew Woloszyn | f731cbf | 2015-10-23 09:53:58 -0400 | [diff] [blame] | 40 | #include "util/bitutils.h" |
David Neto | 9e545d7 | 2015-11-06 18:08:49 -0500 | [diff] [blame] | 41 | #include "util/hex_float.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 42 | |
| 43 | namespace { |
| 44 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 45 | using spvutils::BitwiseCast; |
David Neto | 9e545d7 | 2015-11-06 18:08:49 -0500 | [diff] [blame] | 46 | using spvutils::FloatProxy; |
| 47 | using spvutils::HexFloat; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 48 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 49 | /// @brief Advance text to the start of the next line |
| 50 | /// |
| 51 | /// @param[in] text to be parsed |
| 52 | /// @param[in,out] position position text has been advanced to |
| 53 | /// |
| 54 | /// @return result code |
| 55 | spv_result_t advanceLine(spv_text text, spv_position position) { |
| 56 | while (true) { |
| 57 | switch (text->str[position->index]) { |
| 58 | case '\0': |
| 59 | return SPV_END_OF_STREAM; |
| 60 | case '\n': |
| 61 | position->column = 0; |
| 62 | position->line++; |
| 63 | position->index++; |
| 64 | return SPV_SUCCESS; |
| 65 | default: |
| 66 | position->column++; |
| 67 | position->index++; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// @brief Advance text to first non white space character |
| 74 | /// If a null terminator is found during the text advance SPV_END_OF_STREAM is |
| 75 | /// returned, SPV_SUCCESS otherwise. No error checking is performed on the |
| 76 | /// parameters, its the users responsibility to ensure these are non null. |
| 77 | /// |
| 78 | /// @param[in] text to be parsed |
| 79 | /// @param[in,out] position text has been advanced to |
| 80 | /// |
| 81 | /// @return result code |
| 82 | spv_result_t advance(spv_text text, spv_position position) { |
| 83 | // NOTE: Consume white space, otherwise don't advance. |
Lei Zhang | 8f6ba14 | 2015-11-06 15:09:04 -0500 | [diff] [blame] | 84 | if (position->index >= text->length) return SPV_END_OF_STREAM; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 85 | switch (text->str[position->index]) { |
| 86 | case '\0': |
| 87 | return SPV_END_OF_STREAM; |
| 88 | case ';': |
| 89 | if (spv_result_t error = advanceLine(text, position)) return error; |
| 90 | return advance(text, position); |
| 91 | case ' ': |
| 92 | case '\t': |
| 93 | position->column++; |
| 94 | position->index++; |
| 95 | return advance(text, position); |
| 96 | case '\n': |
| 97 | position->column = 0; |
| 98 | position->line++; |
| 99 | position->index++; |
| 100 | return advance(text, position); |
| 101 | default: |
| 102 | break; |
| 103 | } |
| 104 | return SPV_SUCCESS; |
| 105 | } |
| 106 | |
| 107 | /// @brief Fetch the next word from the text stream. |
| 108 | /// |
| 109 | /// A word ends at the next comment or whitespace. However, double-quoted |
| 110 | /// strings remain intact, and a backslash always escapes the next character. |
| 111 | /// |
| 112 | /// @param[in] text stream to read from |
| 113 | /// @param[in] position current position in text stream |
| 114 | /// @param[out] word returned word |
| 115 | /// @param[out] endPosition one past the end of the returned word |
| 116 | /// |
| 117 | /// @return result code |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 118 | spv_result_t getWord(spv_text text, spv_position position, std::string& word, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 119 | spv_position endPosition) { |
| 120 | if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT; |
| 121 | if (!position || !endPosition) return SPV_ERROR_INVALID_POINTER; |
| 122 | |
| 123 | *endPosition = *position; |
| 124 | |
| 125 | bool quoting = false; |
| 126 | bool escaping = false; |
| 127 | |
| 128 | // NOTE: Assumes first character is not white space! |
| 129 | while (true) { |
| 130 | const char ch = text->str[endPosition->index]; |
| 131 | if (ch == '\\') |
| 132 | escaping = !escaping; |
| 133 | else { |
| 134 | switch (ch) { |
| 135 | case '"': |
| 136 | if (!escaping) quoting = !quoting; |
| 137 | break; |
| 138 | case ' ': |
| 139 | case ';': |
| 140 | case '\t': |
| 141 | case '\n': |
| 142 | if (escaping || quoting) break; |
| 143 | // Fall through. |
| 144 | case '\0': { // NOTE: End of word found! |
| 145 | word.assign(text->str + position->index, |
| 146 | (size_t)(endPosition->index - position->index)); |
| 147 | return SPV_SUCCESS; |
| 148 | } |
| 149 | default: |
| 150 | break; |
| 151 | } |
| 152 | escaping = false; |
| 153 | } |
| 154 | |
| 155 | endPosition->column++; |
| 156 | endPosition->index++; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Returns true if the characters in the text as position represent |
| 161 | // the start of an Opcode. |
| 162 | bool startsWithOp(spv_text text, spv_position position) { |
| 163 | if (text->length < position->index + 3) return false; |
| 164 | char ch0 = text->str[position->index]; |
| 165 | char ch1 = text->str[position->index + 1]; |
| 166 | char ch2 = text->str[position->index + 2]; |
| 167 | return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z')); |
| 168 | } |
| 169 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 170 | } // anonymous namespace |
| 171 | |
| 172 | namespace libspirv { |
| 173 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 174 | const IdType kUnknownType = {0, false, IdTypeClass::kBottom}; |
| 175 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 176 | // TODO(dneto): Reorder AssemblyContext definitions to match declaration order. |
| 177 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 178 | // This represents all of the data that is only valid for the duration of |
| 179 | // a single compilation. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 180 | uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 181 | if (named_ids_.end() == named_ids_.find(textValue)) { |
| 182 | named_ids_[std::string(textValue)] = bound_++; |
| 183 | } |
| 184 | return named_ids_[textValue]; |
| 185 | } |
| 186 | uint32_t AssemblyContext::getBound() const { return bound_; } |
| 187 | |
| 188 | spv_result_t AssemblyContext::advance() { |
| 189 | return ::advance(text_, ¤t_position_); |
| 190 | } |
| 191 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 192 | spv_result_t AssemblyContext::getWord(std::string& word, |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 193 | spv_position endPosition) { |
| 194 | return ::getWord(text_, ¤t_position_, word, endPosition); |
| 195 | } |
| 196 | |
| 197 | bool AssemblyContext::startsWithOp() { |
| 198 | return ::startsWithOp(text_, ¤t_position_); |
| 199 | } |
| 200 | |
| 201 | bool AssemblyContext::isStartOfNewInst() { |
| 202 | spv_position_t nextPosition = current_position_; |
| 203 | if (::advance(text_, &nextPosition)) return false; |
| 204 | if (::startsWithOp(text_, &nextPosition)) return true; |
| 205 | |
| 206 | std::string word; |
| 207 | spv_position_t startPosition = current_position_; |
| 208 | if (::getWord(text_, &startPosition, word, &nextPosition)) return false; |
| 209 | if ('%' != word.front()) return false; |
| 210 | |
| 211 | if (::advance(text_, &nextPosition)) return false; |
| 212 | startPosition = nextPosition; |
| 213 | if (::getWord(text_, &startPosition, word, &nextPosition)) return false; |
| 214 | if ("=" != word) return false; |
| 215 | |
| 216 | if (::advance(text_, &nextPosition)) return false; |
| 217 | startPosition = nextPosition; |
| 218 | if (::startsWithOp(text_, &startPosition)) return true; |
| 219 | return false; |
| 220 | } |
Andrew Woloszyn | 4274f93 | 2015-10-20 09:12:32 -0400 | [diff] [blame] | 221 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 222 | char AssemblyContext::peek() const { |
| 223 | return text_->str[current_position_.index]; |
| 224 | } |
| 225 | |
| 226 | bool AssemblyContext::hasText() const { |
| 227 | return text_->length > current_position_.index; |
| 228 | } |
Andrew Woloszyn | 4274f93 | 2015-10-20 09:12:32 -0400 | [diff] [blame] | 229 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 230 | std::string AssemblyContext::getWord() const { |
Andrew Woloszyn | 4274f93 | 2015-10-20 09:12:32 -0400 | [diff] [blame] | 231 | uint64_t index = current_position_.index; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 232 | while (true) { |
| 233 | switch (text_->str[index]) { |
| 234 | case '\0': |
| 235 | case '\t': |
| 236 | case '\v': |
| 237 | case '\r': |
| 238 | case '\n': |
| 239 | case ' ': |
| 240 | return std::string(text_->str, text_->str + index); |
| 241 | default: |
| 242 | index++; |
| 243 | } |
| 244 | } |
| 245 | assert(0 && "Unreachable"); |
| 246 | return ""; // Make certain compilers happy. |
| 247 | } |
| 248 | |
| 249 | void AssemblyContext::seekForward(uint32_t size) { |
| 250 | current_position_.index += size; |
| 251 | current_position_.column += size; |
| 252 | } |
| 253 | |
| 254 | spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 255 | spv_instruction_t* pInst) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 256 | spvInstructionAddWord(pInst, value); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 257 | return SPV_SUCCESS; |
| 258 | } |
| 259 | |
| 260 | spv_result_t AssemblyContext::binaryEncodeU64(const uint64_t value, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 261 | spv_instruction_t* pInst) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 262 | uint32_t low = uint32_t(0x00000000ffffffff & value); |
| 263 | uint32_t high = uint32_t((0xffffffff00000000 & value) >> 32); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 264 | binaryEncodeU32(low, pInst); |
| 265 | binaryEncodeU32(high, pInst); |
| 266 | return SPV_SUCCESS; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 267 | } |
| 268 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 269 | spv_result_t AssemblyContext::binaryEncodeNumericLiteral( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 270 | const char* val, spv_result_t error_code, const IdType& type, |
| 271 | spv_instruction_t* pInst) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 272 | const bool is_bottom = type.type_class == libspirv::IdTypeClass::kBottom; |
| 273 | const bool is_floating = libspirv::isScalarFloating(type); |
| 274 | const bool is_integer = libspirv::isScalarIntegral(type); |
| 275 | |
| 276 | if (!is_bottom && !is_floating && !is_integer) { |
| 277 | return diagnostic(SPV_ERROR_INTERNAL) |
| 278 | << "The expected type is not a scalar integer or float type"; |
| 279 | } |
| 280 | |
| 281 | // If this is bottom, but looks like a float, we should treat it like a |
| 282 | // float. |
| 283 | const bool looks_like_float = is_bottom && strchr(val, '.'); |
| 284 | |
| 285 | // If we explicitly expect a floating-point number, we should handle that |
| 286 | // first. |
| 287 | if (is_floating || looks_like_float) |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 288 | return binaryEncodeFloatingPointLiteral(val, error_code, type, pInst); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 289 | |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 290 | return binaryEncodeIntegerLiteral(val, error_code, type, pInst); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 293 | spv_result_t AssemblyContext::binaryEncodeString(const char* value, |
| 294 | spv_instruction_t* pInst) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 295 | const size_t length = strlen(value); |
| 296 | const size_t wordCount = (length / 4) + 1; |
| 297 | const size_t oldWordCount = pInst->words.size(); |
| 298 | const size_t newWordCount = oldWordCount + wordCount; |
| 299 | |
| 300 | // TODO(dneto): We can just defer this check until later. |
| 301 | if (newWordCount > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 302 | return diagnostic() << "Instruction too long: more than " |
| 303 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words."; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 304 | } |
| 305 | |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 306 | pInst->words.resize(newWordCount); |
| 307 | |
| 308 | // Make sure all the bytes in the last word are 0, in case we only |
| 309 | // write a partial word at the end. |
| 310 | pInst->words.back() = 0; |
| 311 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 312 | char* dest = (char*)&pInst->words[oldWordCount]; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 313 | strncpy(dest, value, length); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 314 | |
| 315 | return SPV_SUCCESS; |
| 316 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 317 | |
| 318 | spv_result_t AssemblyContext::recordTypeDefinition( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 319 | const spv_instruction_t* pInst) { |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 320 | uint32_t value = pInst->words[1]; |
| 321 | if (types_.find(value) != types_.end()) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 322 | return diagnostic() << "Value " << value |
| 323 | << " has already been used to generate a type"; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 326 | if (pInst->opcode == SpvOpTypeInt) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 327 | 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 Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 331 | } else if (pInst->opcode == SpvOpTypeFloat) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 332 | if (pInst->words.size() != 3) |
| 333 | return diagnostic() << "Invalid OpTypeFloat instruction"; |
| 334 | types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 335 | } else { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 336 | types_[value] = {0, false, IdTypeClass::kOtherType}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 337 | } |
| 338 | return SPV_SUCCESS; |
| 339 | } |
| 340 | |
| 341 | IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const { |
| 342 | auto type = types_.find(value); |
| 343 | if (type == types_.end()) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 344 | return kUnknownType; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 345 | } |
| 346 | return std::get<1>(*type); |
| 347 | } |
| 348 | |
| 349 | IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const { |
| 350 | auto type_value = value_types_.find(value); |
| 351 | if (type_value == value_types_.end()) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 352 | return {0, false, IdTypeClass::kBottom}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 353 | } |
| 354 | return getTypeOfTypeGeneratingValue(std::get<1>(*type_value)); |
| 355 | } |
| 356 | |
| 357 | spv_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 Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 362 | if (!successfully_inserted) |
| 363 | return diagnostic() << "Value is being defined a second time"; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 364 | return SPV_SUCCESS; |
| 365 | } |
| 366 | |
David Neto | 2ae4a68 | 2015-11-09 18:55:42 -0500 | [diff] [blame] | 367 | spv_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 | |
| 377 | spv_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 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 385 | spv_result_t AssemblyContext::binaryEncodeFloatingPointLiteral( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 386 | const char* val, spv_result_t error_code, const IdType& type, |
| 387 | spv_instruction_t* pInst) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 388 | const auto bit_width = assumedBitWidth(type); |
| 389 | switch (bit_width) { |
| 390 | case 16: |
| 391 | return diagnostic(SPV_ERROR_INTERNAL) |
| 392 | << "Unsupported yet: 16-bit float constants."; |
| 393 | case 32: { |
David Neto | 9e545d7 | 2015-11-06 18:08:49 -0500 | [diff] [blame] | 394 | spvutils::HexFloat<FloatProxy<float>> fVal(0.0f); |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 395 | if (auto error = parseNumber(val, error_code, &fVal, |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 396 | "Invalid 32-bit float literal: ")) |
| 397 | return error; |
| 398 | return binaryEncodeU32(BitwiseCast<uint32_t>(fVal), pInst); |
| 399 | } break; |
| 400 | case 64: { |
David Neto | 9e545d7 | 2015-11-06 18:08:49 -0500 | [diff] [blame] | 401 | spvutils::HexFloat<FloatProxy<double>> dVal(0.0); |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 402 | if (auto error = parseNumber(val, error_code, &dVal, |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 403 | "Invalid 64-bit float literal: ")) |
| 404 | return error; |
| 405 | return binaryEncodeU64(BitwiseCast<uint64_t>(dVal), pInst); |
| 406 | } break; |
| 407 | default: |
| 408 | break; |
| 409 | } |
| 410 | return diagnostic() << "Unsupported " << bit_width << "-bit float literals"; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 411 | } |
| 412 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 413 | spv_result_t AssemblyContext::binaryEncodeIntegerLiteral( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 414 | const char* val, spv_result_t error_code, const IdType& type, |
| 415 | spv_instruction_t* pInst) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 416 | const bool is_bottom = type.type_class == libspirv::IdTypeClass::kBottom; |
| 417 | const auto bit_width = assumedBitWidth(type); |
| 418 | |
| 419 | if (bit_width > 64) |
| 420 | return diagnostic(SPV_ERROR_INTERNAL) << "Unsupported " << bit_width |
| 421 | << "-bit integer literals"; |
| 422 | |
| 423 | // Either we are expecting anything or integer. |
| 424 | bool is_negative = val[0] == '-'; |
| 425 | bool can_be_signed = is_bottom || type.isSigned; |
| 426 | |
| 427 | if (is_negative && !can_be_signed) { |
| 428 | return diagnostic() |
| 429 | << "Cannot put a negative number in an unsigned literal"; |
| 430 | } |
| 431 | |
| 432 | const bool is_hex = val[0] == '0' && (val[1] == 'x' || val[1] == 'X'); |
| 433 | |
| 434 | uint64_t decoded_bits; |
| 435 | if (is_negative) { |
| 436 | int64_t decoded_signed = 0; |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 437 | if (auto error = parseNumber(val, error_code, &decoded_signed, |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 438 | "Invalid signed integer literal: ")) |
| 439 | return error; |
| 440 | if (auto error = checkRangeAndIfHexThenSignExtend( |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 441 | decoded_signed, error_code, type, is_hex, &decoded_signed)) |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 442 | return error; |
| 443 | decoded_bits = decoded_signed; |
| 444 | } else { |
| 445 | // There's no leading minus sign, so parse it as an unsigned integer. |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 446 | if (auto error = parseNumber(val, error_code, &decoded_bits, |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 447 | "Invalid unsigned integer literal: ")) |
| 448 | return error; |
| 449 | if (auto error = checkRangeAndIfHexThenSignExtend( |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 450 | decoded_bits, error_code, type, is_hex, &decoded_bits)) |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 451 | return error; |
| 452 | } |
| 453 | if (bit_width > 32) { |
| 454 | return binaryEncodeU64(decoded_bits, pInst); |
| 455 | } else { |
| 456 | return binaryEncodeU32(uint32_t(decoded_bits), pInst); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | template <typename T> |
| 461 | spv_result_t AssemblyContext::checkRangeAndIfHexThenSignExtend( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 462 | T value, spv_result_t error_code, const IdType& type, bool is_hex, |
| 463 | T* updated_value_for_hex) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 464 | // The encoded result has three regions of bits that are of interest, from |
| 465 | // least to most significant: |
| 466 | // - magnitude bits, where the magnitude of the number would be stored if |
| 467 | // we were using a signed-magnitude representation. |
| 468 | // - an optional sign bit |
| 469 | // - overflow bits, up to bit 63 of a 64-bit number |
| 470 | // For example: |
| 471 | // Type Overflow Sign Magnitude |
| 472 | // --------------- -------- ---- --------- |
| 473 | // unsigned 8 bit 8-63 n/a 0-7 |
| 474 | // signed 8 bit 8-63 7 0-6 |
| 475 | // unsigned 16 bit 16-63 n/a 0-15 |
| 476 | // signed 16 bit 16-63 15 0-14 |
| 477 | |
| 478 | // We'll use masks to define the three regions. |
| 479 | // At first we'll assume the number is unsigned. |
| 480 | const uint32_t bit_width = assumedBitWidth(type); |
| 481 | uint64_t magnitude_mask = |
| 482 | (bit_width == 64) ? -1 : ((uint64_t(1) << bit_width) - 1); |
| 483 | uint64_t sign_mask = 0; |
| 484 | uint64_t overflow_mask = ~magnitude_mask; |
| 485 | |
| 486 | if (value < 0 || type.isSigned) { |
| 487 | // Accommodate the sign bit. |
| 488 | magnitude_mask >>= 1; |
| 489 | sign_mask = magnitude_mask + 1; |
| 490 | } |
| 491 | |
| 492 | bool failed = false; |
| 493 | if (value < 0) { |
| 494 | // The top bits must all be 1 for a negative signed value. |
| 495 | failed = ((value & overflow_mask) != overflow_mask) || |
| 496 | ((value & sign_mask) != sign_mask); |
| 497 | } else { |
| 498 | if (is_hex) { |
| 499 | // Hex values are a bit special. They decode as unsigned values, but |
| 500 | // may represent a negative number. In this case, the overflow bits |
| 501 | // should be zero. |
Ben Vanik | 01c8d7a | 2015-11-22 08:32:53 -0800 | [diff] [blame] | 502 | failed = (value & overflow_mask) != 0; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 503 | } else { |
Lei Zhang | 8bd75d6 | 2015-11-18 09:22:10 -0500 | [diff] [blame] | 504 | const uint64_t value_as_u64 = static_cast<uint64_t>(value); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 505 | // Check overflow in the ordinary case. |
Lei Zhang | 8bd75d6 | 2015-11-18 09:22:10 -0500 | [diff] [blame] | 506 | failed = (value_as_u64 & magnitude_mask) != value_as_u64; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | if (failed) { |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 511 | return diagnostic(error_code) |
| 512 | << "Integer " << (is_hex ? std::hex : std::dec) << std::showbase |
| 513 | << value << " does not fit in a " << std::dec << bit_width << "-bit " |
| 514 | << (type.isSigned ? "signed" : "unsigned") << " integer"; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // Sign extend hex the number. |
| 518 | if (is_hex && (value & sign_mask)) |
| 519 | *updated_value_for_hex = (value | overflow_mask); |
| 520 | |
| 521 | return SPV_SUCCESS; |
| 522 | } |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 523 | } // namespace libspirv |