Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 2 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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 |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 6 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 8 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 14 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 15 | #include "source/text_handler.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 16 | |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 17 | #include <algorithm> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 18 | #include <cassert> |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 19 | #include <cstdlib> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 20 | #include <cstring> |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 21 | #include <tuple> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 22 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 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" |
Marius Hillenbrand | 1ed847f | 2021-12-08 18:01:26 +0100 | [diff] [blame] | 32 | #include "source/util/string_utils.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 33 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 34 | namespace spvtools { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 35 | namespace { |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 36 | |
Lei Zhang | f2cf719 | 2016-04-21 15:50:23 -0400 | [diff] [blame] | 37 | // Advances |text| to the start of the next line and writes the new position to |
| 38 | // |position|. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 39 | spv_result_t advanceLine(spv_text text, spv_position position) { |
| 40 | while (true) { |
Lei Zhang | f2cf719 | 2016-04-21 15:50:23 -0400 | [diff] [blame] | 41 | if (position->index >= text->length) return SPV_END_OF_STREAM; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 42 | 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 Zhang | f2cf719 | 2016-04-21 15:50:23 -0400 | [diff] [blame] | 58 | // 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 Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 63 | spv_result_t advance(spv_text text, spv_position position) { |
| 64 | // NOTE: Consume white space, otherwise don't advance. |
Lei Zhang | 8f6ba14 | 2015-11-06 15:09:04 -0500 | [diff] [blame] | 65 | if (position->index >= text->length) return SPV_END_OF_STREAM; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 66 | 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 Mircevski | a1de2b3 | 2016-04-01 00:47:02 -0400 | [diff] [blame] | 74 | case '\r': |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 75 | 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 Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 89 | // 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. |
| 95 | 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] | 96 | if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 97 | if (!position) return SPV_ERROR_INVALID_POINTER; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 98 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 99 | const size_t start_index = position->index; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 100 | |
| 101 | bool quoting = false; |
| 102 | bool escaping = false; |
| 103 | |
| 104 | // NOTE: Assumes first character is not white space! |
| 105 | while (true) { |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 106 | if (position->index >= text->length) { |
| 107 | word->assign(text->str + start_index, text->str + position->index); |
Lei Zhang | 9413fbb | 2016-02-22 17:17:25 -0500 | [diff] [blame] | 108 | return SPV_SUCCESS; |
| 109 | } |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 110 | const char ch = text->str[position->index]; |
dan sinclair | 9991d66 | 2018-08-07 09:09:47 -0400 | [diff] [blame] | 111 | if (ch == '\\') { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 112 | escaping = !escaping; |
dan sinclair | 9991d66 | 2018-08-07 09:09:47 -0400 | [diff] [blame] | 113 | } else { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 114 | switch (ch) { |
| 115 | case '"': |
| 116 | if (!escaping) quoting = !quoting; |
| 117 | break; |
| 118 | case ' ': |
| 119 | case ';': |
| 120 | case '\t': |
| 121 | case '\n': |
Dejan Mircevski | a1de2b3 | 2016-04-01 00:47:02 -0400 | [diff] [blame] | 122 | case '\r': |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 123 | if (escaping || quoting) break; |
David Neto | 9529d3c | 2021-10-01 08:43:59 -0400 | [diff] [blame] | 124 | word->assign(text->str + start_index, text->str + position->index); |
| 125 | return SPV_SUCCESS; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 126 | case '\0': { // NOTE: End of word found! |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 127 | word->assign(text->str + start_index, text->str + position->index); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 128 | return SPV_SUCCESS; |
| 129 | } |
| 130 | default: |
| 131 | break; |
| 132 | } |
| 133 | escaping = false; |
| 134 | } |
| 135 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 136 | position->column++; |
| 137 | position->index++; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | // Returns true if the characters in the text as position represent |
| 142 | // the start of an Opcode. |
| 143 | bool 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 sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 151 | } // namespace |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 152 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 153 | const IdType kUnknownType = {0, false, IdTypeClass::kBottom}; |
| 154 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 155 | // TODO(dneto): Reorder AssemblyContext definitions to match declaration order. |
| 156 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 157 | // This represents all of the data that is only valid for the duration of |
| 158 | // a single compilation. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 159 | uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) { |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 160 | if (!ids_to_preserve_.empty()) { |
| 161 | uint32_t id = 0; |
dan sinclair | 76e0bde | 2018-07-06 13:25:17 -0400 | [diff] [blame] | 162 | if (spvtools::utils::ParseNumber(textValue, &id)) { |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 163 | if (ids_to_preserve_.find(id) != ids_to_preserve_.end()) { |
| 164 | bound_ = std::max(bound_, id + 1); |
| 165 | return id; |
| 166 | } |
| 167 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 168 | } |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 169 | |
| 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 Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 185 | } |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 186 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 187 | uint32_t AssemblyContext::getBound() const { return bound_; } |
| 188 | |
| 189 | spv_result_t AssemblyContext::advance() { |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 190 | return spvtools::advance(text_, ¤t_position_); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 193 | spv_result_t AssemblyContext::getWord(std::string* word, |
| 194 | spv_position next_position) { |
| 195 | *next_position = current_position_; |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 196 | return spvtools::getWord(text_, next_position, word); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool AssemblyContext::startsWithOp() { |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 200 | return spvtools::startsWithOp(text_, ¤t_position_); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | bool AssemblyContext::isStartOfNewInst() { |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 204 | spv_position_t pos = current_position_; |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 205 | if (spvtools::advance(text_, &pos)) return false; |
| 206 | if (spvtools::startsWithOp(text_, &pos)) return true; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 207 | |
| 208 | std::string word; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 209 | pos = current_position_; |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 210 | if (spvtools::getWord(text_, &pos, &word)) return false; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 211 | if ('%' != word.front()) return false; |
| 212 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 213 | if (spvtools::advance(text_, &pos)) return false; |
| 214 | if (spvtools::getWord(text_, &pos, &word)) return false; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 215 | if ("=" != word) return false; |
| 216 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 217 | if (spvtools::advance(text_, &pos)) return false; |
| 218 | if (spvtools::startsWithOp(text_, &pos)) return true; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 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 | void AssemblyContext::seekForward(uint32_t size) { |
| 231 | current_position_.index += size; |
| 232 | current_position_.column += size; |
| 233 | } |
| 234 | |
| 235 | spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 236 | spv_instruction_t* pInst) { |
Andrew Woloszyn | 4ddb431 | 2016-02-17 13:00:23 -0500 | [diff] [blame] | 237 | pInst->words.insert(pInst->words.end(), value); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 238 | return SPV_SUCCESS; |
| 239 | } |
| 240 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 241 | spv_result_t AssemblyContext::binaryEncodeNumericLiteral( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 242 | const char* val, spv_result_t error_code, const IdType& type, |
| 243 | spv_instruction_t* pInst) { |
dan sinclair | 76e0bde | 2018-07-06 13:25:17 -0400 | [diff] [blame] | 244 | using spvtools::utils::EncodeNumberStatus; |
qining | 1773b95 | 2016-09-01 14:27:04 -0400 | [diff] [blame] | 245 | // Populate the NumberType from the IdType for parsing. |
dan sinclair | 76e0bde | 2018-07-06 13:25:17 -0400 | [diff] [blame] | 246 | spvtools::utils::NumberType number_type; |
qining | 1773b95 | 2016-09-01 14:27:04 -0400 | [diff] [blame] | 247 | 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 Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 276 | } |
| 277 | |
qining | 1773b95 | 2016-09-01 14:27:04 -0400 | [diff] [blame] | 278 | 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 Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 298 | spv_result_t AssemblyContext::binaryEncodeString(const char* value, |
| 299 | spv_instruction_t* pInst) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 300 | 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 Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 307 | return diagnostic() << "Instruction too long: more than " |
| 308 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words."; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Marius Hillenbrand | 1ed847f | 2021-12-08 18:01:26 +0100 | [diff] [blame] | 311 | pInst->words.reserve(newWordCount); |
| 312 | spvtools::utils::AppendToVector(value, &pInst->words); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 313 | |
| 314 | return SPV_SUCCESS; |
| 315 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 316 | |
| 317 | spv_result_t AssemblyContext::recordTypeDefinition( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 318 | const spv_instruction_t* pInst) { |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 319 | uint32_t value = pInst->words[1]; |
| 320 | if (types_.find(value) != types_.end()) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 321 | return diagnostic() << "Value " << value |
| 322 | << " has already been used to generate a type"; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 323 | } |
| 324 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 325 | if (pInst->opcode == SpvOpTypeInt) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 326 | 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 Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 330 | } else if (pInst->opcode == SpvOpTypeFloat) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 331 | if (pInst->words.size() != 3) |
| 332 | return diagnostic() << "Invalid OpTypeFloat instruction"; |
| 333 | types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 334 | } else { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 335 | types_[value] = {0, false, IdTypeClass::kOtherType}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 336 | } |
| 337 | return SPV_SUCCESS; |
| 338 | } |
| 339 | |
| 340 | IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const { |
| 341 | auto type = types_.find(value); |
| 342 | if (type == types_.end()) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 343 | return kUnknownType; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 344 | } |
| 345 | return std::get<1>(*type); |
| 346 | } |
| 347 | |
| 348 | IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const { |
| 349 | auto type_value = value_types_.find(value); |
| 350 | if (type_value == value_types_.end()) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 351 | return {0, false, IdTypeClass::kBottom}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 352 | } |
| 353 | return getTypeOfTypeGeneratingValue(std::get<1>(*type_value)); |
| 354 | } |
| 355 | |
| 356 | spv_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 Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 361 | if (!successfully_inserted) |
| 362 | return diagnostic() << "Value is being defined a second time"; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 363 | return SPV_SUCCESS; |
| 364 | } |
| 365 | |
David Neto | 2ae4a68 | 2015-11-09 18:55:42 -0500 | [diff] [blame] | 366 | spv_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 | |
| 376 | spv_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 Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 384 | std::set<uint32_t> AssemblyContext::GetNumericIds() const { |
| 385 | std::set<uint32_t> ids; |
| 386 | for (const auto& kv : named_ids_) { |
| 387 | uint32_t id; |
dan sinclair | 76e0bde | 2018-07-06 13:25:17 -0400 | [diff] [blame] | 388 | if (spvtools::utils::ParseNumber(kv.first.c_str(), &id)) ids.insert(id); |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 389 | } |
| 390 | return ids; |
| 391 | } |
| 392 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 393 | } // namespace spvtools |