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" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 32 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 33 | namespace spvtools { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 34 | namespace { |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 35 | |
Lei Zhang | f2cf719 | 2016-04-21 15:50:23 -0400 | [diff] [blame] | 36 | // Advances |text| to the start of the next line and writes the new position to |
| 37 | // |position|. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 38 | spv_result_t advanceLine(spv_text text, spv_position position) { |
| 39 | while (true) { |
Lei Zhang | f2cf719 | 2016-04-21 15:50:23 -0400 | [diff] [blame] | 40 | if (position->index >= text->length) return SPV_END_OF_STREAM; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 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 | |
Lei Zhang | f2cf719 | 2016-04-21 15:50:23 -0400 | [diff] [blame] | 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. |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 62 | spv_result_t advance(spv_text text, spv_position position) { |
| 63 | // NOTE: Consume white space, otherwise don't advance. |
Lei Zhang | 8f6ba14 | 2015-11-06 15:09:04 -0500 | [diff] [blame] | 64 | if (position->index >= text->length) return SPV_END_OF_STREAM; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 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': |
Dejan Mircevski | a1de2b3 | 2016-04-01 00:47:02 -0400 | [diff] [blame] | 73 | case '\r': |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 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 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 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. |
| 94 | 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] | 95 | if (!text->str || !text->length) return SPV_ERROR_INVALID_TEXT; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 96 | if (!position) return SPV_ERROR_INVALID_POINTER; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 97 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 98 | const size_t start_index = position->index; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 99 | |
| 100 | bool quoting = false; |
| 101 | bool escaping = false; |
| 102 | |
| 103 | // NOTE: Assumes first character is not white space! |
| 104 | while (true) { |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 105 | if (position->index >= text->length) { |
| 106 | word->assign(text->str + start_index, text->str + position->index); |
Lei Zhang | 9413fbb | 2016-02-22 17:17:25 -0500 | [diff] [blame] | 107 | return SPV_SUCCESS; |
| 108 | } |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 109 | const char ch = text->str[position->index]; |
dan sinclair | 9991d66 | 2018-08-07 09:09:47 -0400 | [diff] [blame] | 110 | if (ch == '\\') { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 111 | escaping = !escaping; |
dan sinclair | 9991d66 | 2018-08-07 09:09:47 -0400 | [diff] [blame] | 112 | } else { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 113 | switch (ch) { |
| 114 | case '"': |
| 115 | if (!escaping) quoting = !quoting; |
| 116 | break; |
| 117 | case ' ': |
| 118 | case ';': |
| 119 | case '\t': |
| 120 | case '\n': |
Dejan Mircevski | a1de2b3 | 2016-04-01 00:47:02 -0400 | [diff] [blame] | 121 | case '\r': |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 122 | if (escaping || quoting) break; |
David Neto | 9529d3c | 2021-10-01 08:43:59 -0400 | [diff] [blame] | 123 | word->assign(text->str + start_index, text->str + position->index); |
| 124 | return SPV_SUCCESS; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 125 | case '\0': { // NOTE: End of word found! |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 126 | word->assign(text->str + start_index, text->str + position->index); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 127 | return SPV_SUCCESS; |
| 128 | } |
| 129 | default: |
| 130 | break; |
| 131 | } |
| 132 | escaping = false; |
| 133 | } |
| 134 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 135 | position->column++; |
| 136 | position->index++; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | // Returns true if the characters in the text as position represent |
| 141 | // the start of an Opcode. |
| 142 | bool startsWithOp(spv_text text, spv_position position) { |
| 143 | if (text->length < position->index + 3) return false; |
| 144 | char ch0 = text->str[position->index]; |
| 145 | char ch1 = text->str[position->index + 1]; |
| 146 | char ch2 = text->str[position->index + 2]; |
| 147 | return ('O' == ch0 && 'p' == ch1 && ('A' <= ch2 && ch2 <= 'Z')); |
| 148 | } |
| 149 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 150 | } // namespace |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 151 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 152 | const IdType kUnknownType = {0, false, IdTypeClass::kBottom}; |
| 153 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 154 | // TODO(dneto): Reorder AssemblyContext definitions to match declaration order. |
| 155 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 156 | // This represents all of the data that is only valid for the duration of |
| 157 | // a single compilation. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 158 | uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) { |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 159 | if (!ids_to_preserve_.empty()) { |
| 160 | uint32_t id = 0; |
dan sinclair | 76e0bde | 2018-07-06 13:25:17 -0400 | [diff] [blame] | 161 | if (spvtools::utils::ParseNumber(textValue, &id)) { |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 162 | if (ids_to_preserve_.find(id) != ids_to_preserve_.end()) { |
| 163 | bound_ = std::max(bound_, id + 1); |
| 164 | return id; |
| 165 | } |
| 166 | } |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 167 | } |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 168 | |
| 169 | const auto it = named_ids_.find(textValue); |
| 170 | if (it == named_ids_.end()) { |
| 171 | uint32_t id = next_id_++; |
| 172 | if (!ids_to_preserve_.empty()) { |
| 173 | while (ids_to_preserve_.find(id) != ids_to_preserve_.end()) { |
| 174 | id = next_id_++; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | named_ids_.emplace(textValue, id); |
| 179 | bound_ = std::max(bound_, id + 1); |
| 180 | return id; |
| 181 | } |
| 182 | |
| 183 | return it->second; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 184 | } |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 185 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 186 | uint32_t AssemblyContext::getBound() const { return bound_; } |
| 187 | |
| 188 | spv_result_t AssemblyContext::advance() { |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 189 | return spvtools::advance(text_, ¤t_position_); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 192 | spv_result_t AssemblyContext::getWord(std::string* word, |
| 193 | spv_position next_position) { |
| 194 | *next_position = current_position_; |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 195 | return spvtools::getWord(text_, next_position, word); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | bool AssemblyContext::startsWithOp() { |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 199 | return spvtools::startsWithOp(text_, ¤t_position_); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | bool AssemblyContext::isStartOfNewInst() { |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 203 | spv_position_t pos = current_position_; |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 204 | if (spvtools::advance(text_, &pos)) return false; |
| 205 | if (spvtools::startsWithOp(text_, &pos)) return true; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 206 | |
| 207 | std::string word; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 208 | pos = current_position_; |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 209 | if (spvtools::getWord(text_, &pos, &word)) return false; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 210 | if ('%' != word.front()) return false; |
| 211 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 212 | if (spvtools::advance(text_, &pos)) return false; |
| 213 | if (spvtools::getWord(text_, &pos, &word)) return false; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 214 | if ("=" != word) return false; |
| 215 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 216 | if (spvtools::advance(text_, &pos)) return false; |
| 217 | if (spvtools::startsWithOp(text_, &pos)) return true; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 218 | return false; |
| 219 | } |
Andrew Woloszyn | 4274f93 | 2015-10-20 09:12:32 -0400 | [diff] [blame] | 220 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 221 | char AssemblyContext::peek() const { |
| 222 | return text_->str[current_position_.index]; |
| 223 | } |
| 224 | |
| 225 | bool AssemblyContext::hasText() const { |
| 226 | return text_->length > current_position_.index; |
| 227 | } |
Andrew Woloszyn | 4274f93 | 2015-10-20 09:12:32 -0400 | [diff] [blame] | 228 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 229 | void AssemblyContext::seekForward(uint32_t size) { |
| 230 | current_position_.index += size; |
| 231 | current_position_.column += size; |
| 232 | } |
| 233 | |
| 234 | spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 235 | spv_instruction_t* pInst) { |
Andrew Woloszyn | 4ddb431 | 2016-02-17 13:00:23 -0500 | [diff] [blame] | 236 | pInst->words.insert(pInst->words.end(), value); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 237 | return SPV_SUCCESS; |
| 238 | } |
| 239 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 240 | spv_result_t AssemblyContext::binaryEncodeNumericLiteral( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 241 | const char* val, spv_result_t error_code, const IdType& type, |
| 242 | spv_instruction_t* pInst) { |
dan sinclair | 76e0bde | 2018-07-06 13:25:17 -0400 | [diff] [blame] | 243 | using spvtools::utils::EncodeNumberStatus; |
qining | 1773b95 | 2016-09-01 14:27:04 -0400 | [diff] [blame] | 244 | // Populate the NumberType from the IdType for parsing. |
dan sinclair | 76e0bde | 2018-07-06 13:25:17 -0400 | [diff] [blame] | 245 | spvtools::utils::NumberType number_type; |
qining | 1773b95 | 2016-09-01 14:27:04 -0400 | [diff] [blame] | 246 | switch (type.type_class) { |
| 247 | case IdTypeClass::kOtherType: |
| 248 | return diagnostic(SPV_ERROR_INTERNAL) |
| 249 | << "Unexpected numeric literal type"; |
| 250 | case IdTypeClass::kScalarIntegerType: |
| 251 | if (type.isSigned) { |
| 252 | number_type = {type.bitwidth, SPV_NUMBER_SIGNED_INT}; |
| 253 | } else { |
| 254 | number_type = {type.bitwidth, SPV_NUMBER_UNSIGNED_INT}; |
| 255 | } |
| 256 | break; |
| 257 | case IdTypeClass::kScalarFloatType: |
| 258 | number_type = {type.bitwidth, SPV_NUMBER_FLOATING}; |
| 259 | break; |
| 260 | case IdTypeClass::kBottom: |
| 261 | // kBottom means the type is unknown and we need to infer the type before |
| 262 | // parsing the number. The rule is: If there is a decimal point, treat |
| 263 | // the value as a floating point value, otherwise a integer value, then |
| 264 | // if the first char of the integer text is '-', treat the integer as a |
| 265 | // signed integer, otherwise an unsigned integer. |
| 266 | uint32_t bitwidth = static_cast<uint32_t>(assumedBitWidth(type)); |
| 267 | if (strchr(val, '.')) { |
| 268 | number_type = {bitwidth, SPV_NUMBER_FLOATING}; |
| 269 | } else if (type.isSigned || val[0] == '-') { |
| 270 | number_type = {bitwidth, SPV_NUMBER_SIGNED_INT}; |
| 271 | } else { |
| 272 | number_type = {bitwidth, SPV_NUMBER_UNSIGNED_INT}; |
| 273 | } |
| 274 | break; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 275 | } |
| 276 | |
qining | 1773b95 | 2016-09-01 14:27:04 -0400 | [diff] [blame] | 277 | std::string error_msg; |
| 278 | EncodeNumberStatus parse_status = ParseAndEncodeNumber( |
| 279 | val, number_type, |
| 280 | [this, pInst](uint32_t d) { this->binaryEncodeU32(d, pInst); }, |
| 281 | &error_msg); |
| 282 | switch (parse_status) { |
| 283 | case EncodeNumberStatus::kSuccess: |
| 284 | return SPV_SUCCESS; |
| 285 | case EncodeNumberStatus::kInvalidText: |
| 286 | return diagnostic(error_code) << error_msg; |
| 287 | case EncodeNumberStatus::kUnsupported: |
| 288 | return diagnostic(SPV_ERROR_INTERNAL) << error_msg; |
| 289 | case EncodeNumberStatus::kInvalidUsage: |
| 290 | return diagnostic(SPV_ERROR_INVALID_TEXT) << error_msg; |
| 291 | } |
| 292 | // This line is not reachable, only added to satisfy the compiler. |
| 293 | return diagnostic(SPV_ERROR_INTERNAL) |
| 294 | << "Unexpected result code from ParseAndEncodeNumber()"; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 295 | } |
| 296 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 297 | spv_result_t AssemblyContext::binaryEncodeString(const char* value, |
| 298 | spv_instruction_t* pInst) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 299 | const size_t length = strlen(value); |
| 300 | const size_t wordCount = (length / 4) + 1; |
| 301 | const size_t oldWordCount = pInst->words.size(); |
| 302 | const size_t newWordCount = oldWordCount + wordCount; |
| 303 | |
| 304 | // TODO(dneto): We can just defer this check until later. |
| 305 | if (newWordCount > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 306 | return diagnostic() << "Instruction too long: more than " |
| 307 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words."; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 308 | } |
| 309 | |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 310 | pInst->words.resize(newWordCount); |
| 311 | |
| 312 | // Make sure all the bytes in the last word are 0, in case we only |
| 313 | // write a partial word at the end. |
| 314 | pInst->words.back() = 0; |
| 315 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 316 | char* dest = (char*)&pInst->words[oldWordCount]; |
Steven Perron | c512c68 | 2018-12-13 15:03:28 -0500 | [diff] [blame] | 317 | strncpy(dest, value, length + 1); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 318 | |
| 319 | return SPV_SUCCESS; |
| 320 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 321 | |
| 322 | spv_result_t AssemblyContext::recordTypeDefinition( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 323 | const spv_instruction_t* pInst) { |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 324 | uint32_t value = pInst->words[1]; |
| 325 | if (types_.find(value) != types_.end()) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 326 | return diagnostic() << "Value " << value |
| 327 | << " has already been used to generate a type"; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 330 | if (pInst->opcode == SpvOpTypeInt) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 331 | if (pInst->words.size() != 4) |
| 332 | return diagnostic() << "Invalid OpTypeInt instruction"; |
| 333 | types_[value] = {pInst->words[2], pInst->words[3] != 0, |
| 334 | IdTypeClass::kScalarIntegerType}; |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 335 | } else if (pInst->opcode == SpvOpTypeFloat) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 336 | if (pInst->words.size() != 3) |
| 337 | return diagnostic() << "Invalid OpTypeFloat instruction"; |
| 338 | types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 339 | } else { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 340 | types_[value] = {0, false, IdTypeClass::kOtherType}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 341 | } |
| 342 | return SPV_SUCCESS; |
| 343 | } |
| 344 | |
| 345 | IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const { |
| 346 | auto type = types_.find(value); |
| 347 | if (type == types_.end()) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 348 | return kUnknownType; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 349 | } |
| 350 | return std::get<1>(*type); |
| 351 | } |
| 352 | |
| 353 | IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const { |
| 354 | auto type_value = value_types_.find(value); |
| 355 | if (type_value == value_types_.end()) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 356 | return {0, false, IdTypeClass::kBottom}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 357 | } |
| 358 | return getTypeOfTypeGeneratingValue(std::get<1>(*type_value)); |
| 359 | } |
| 360 | |
| 361 | spv_result_t AssemblyContext::recordTypeIdForValue(uint32_t value, |
| 362 | uint32_t type) { |
| 363 | bool successfully_inserted = false; |
| 364 | std::tie(std::ignore, successfully_inserted) = |
| 365 | value_types_.insert(std::make_pair(value, type)); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 366 | if (!successfully_inserted) |
| 367 | return diagnostic() << "Value is being defined a second time"; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 368 | return SPV_SUCCESS; |
| 369 | } |
| 370 | |
David Neto | 2ae4a68 | 2015-11-09 18:55:42 -0500 | [diff] [blame] | 371 | spv_result_t AssemblyContext::recordIdAsExtInstImport( |
| 372 | uint32_t id, spv_ext_inst_type_t type) { |
| 373 | bool successfully_inserted = false; |
| 374 | std::tie(std::ignore, successfully_inserted) = |
| 375 | import_id_to_ext_inst_type_.insert(std::make_pair(id, type)); |
| 376 | if (!successfully_inserted) |
| 377 | return diagnostic() << "Import Id is being defined a second time"; |
| 378 | return SPV_SUCCESS; |
| 379 | } |
| 380 | |
| 381 | spv_ext_inst_type_t AssemblyContext::getExtInstTypeForId(uint32_t id) const { |
| 382 | auto type = import_id_to_ext_inst_type_.find(id); |
| 383 | if (type == import_id_to_ext_inst_type_.end()) { |
| 384 | return SPV_EXT_INST_TYPE_NONE; |
| 385 | } |
| 386 | return std::get<1>(*type); |
| 387 | } |
| 388 | |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 389 | std::set<uint32_t> AssemblyContext::GetNumericIds() const { |
| 390 | std::set<uint32_t> ids; |
| 391 | for (const auto& kv : named_ids_) { |
| 392 | uint32_t id; |
dan sinclair | 76e0bde | 2018-07-06 13:25:17 -0400 | [diff] [blame] | 393 | if (spvtools::utils::ParseNumber(kv.first.c_str(), &id)) ids.insert(id); |
Andrey Tuganov | b173d1c | 2017-04-11 19:46:15 -0400 | [diff] [blame] | 394 | } |
| 395 | return ids; |
| 396 | } |
| 397 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 398 | } // namespace spvtools |