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 | |
| 15 | #include "text_handler.h" |
| 16 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 17 | #include <cassert> |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 18 | #include <cstdlib> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 19 | #include <cstring> |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 20 | #include <tuple> |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 21 | |
David Neto | fcc7d58 | 2015-10-27 15:31:10 -0400 | [diff] [blame] | 22 | #include "assembly_grammar.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 23 | #include "binary.h" |
| 24 | #include "ext_inst.h" |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 25 | #include "instruction.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 26 | #include "opcode.h" |
| 27 | #include "text.h" |
Andrew Woloszyn | f731cbf | 2015-10-23 09:53:58 -0400 | [diff] [blame] | 28 | #include "util/bitutils.h" |
David Neto | 9e545d7 | 2015-11-06 18:08:49 -0500 | [diff] [blame] | 29 | #include "util/hex_float.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 33 | using spvutils::BitwiseCast; |
David Neto | 9e545d7 | 2015-11-06 18:08:49 -0500 | [diff] [blame] | 34 | using spvutils::FloatProxy; |
| 35 | using spvutils::HexFloat; |
David Neto | 78e677b | 2015-10-05 13:28:46 -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]; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 111 | if (ch == '\\') |
| 112 | escaping = !escaping; |
| 113 | else { |
| 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; |
| 124 | // Fall through. |
| 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 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 150 | } // anonymous namespace |
| 151 | |
| 152 | namespace libspirv { |
| 153 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 154 | const IdType kUnknownType = {0, false, IdTypeClass::kBottom}; |
| 155 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 156 | // TODO(dneto): Reorder AssemblyContext definitions to match declaration order. |
| 157 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 158 | // This represents all of the data that is only valid for the duration of |
| 159 | // a single compilation. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 160 | uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) { |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 161 | if (named_ids_.end() == named_ids_.find(textValue)) { |
| 162 | named_ids_[std::string(textValue)] = bound_++; |
| 163 | } |
| 164 | return named_ids_[textValue]; |
| 165 | } |
| 166 | uint32_t AssemblyContext::getBound() const { return bound_; } |
| 167 | |
| 168 | spv_result_t AssemblyContext::advance() { |
| 169 | return ::advance(text_, ¤t_position_); |
| 170 | } |
| 171 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 172 | spv_result_t AssemblyContext::getWord(std::string* word, |
| 173 | spv_position next_position) { |
| 174 | *next_position = current_position_; |
| 175 | return ::getWord(text_, next_position, word); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | bool AssemblyContext::startsWithOp() { |
| 179 | return ::startsWithOp(text_, ¤t_position_); |
| 180 | } |
| 181 | |
| 182 | bool AssemblyContext::isStartOfNewInst() { |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 183 | spv_position_t pos = current_position_; |
| 184 | if (::advance(text_, &pos)) return false; |
| 185 | if (::startsWithOp(text_, &pos)) return true; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 186 | |
| 187 | std::string word; |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 188 | pos = current_position_; |
| 189 | if (::getWord(text_, &pos, &word)) return false; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 190 | if ('%' != word.front()) return false; |
| 191 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 192 | if (::advance(text_, &pos)) return false; |
| 193 | if (::getWord(text_, &pos, &word)) return false; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 194 | if ("=" != word) return false; |
| 195 | |
Lei Zhang | 6032b98 | 2016-03-15 16:52:40 -0400 | [diff] [blame] | 196 | if (::advance(text_, &pos)) return false; |
| 197 | if (::startsWithOp(text_, &pos)) return true; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 198 | return false; |
| 199 | } |
Andrew Woloszyn | 4274f93 | 2015-10-20 09:12:32 -0400 | [diff] [blame] | 200 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 201 | char AssemblyContext::peek() const { |
| 202 | return text_->str[current_position_.index]; |
| 203 | } |
| 204 | |
| 205 | bool AssemblyContext::hasText() const { |
| 206 | return text_->length > current_position_.index; |
| 207 | } |
Andrew Woloszyn | 4274f93 | 2015-10-20 09:12:32 -0400 | [diff] [blame] | 208 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 209 | void AssemblyContext::seekForward(uint32_t size) { |
| 210 | current_position_.index += size; |
| 211 | current_position_.column += size; |
| 212 | } |
| 213 | |
| 214 | spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 215 | spv_instruction_t* pInst) { |
Andrew Woloszyn | 4ddb431 | 2016-02-17 13:00:23 -0500 | [diff] [blame] | 216 | pInst->words.insert(pInst->words.end(), value); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 217 | return SPV_SUCCESS; |
| 218 | } |
| 219 | |
| 220 | spv_result_t AssemblyContext::binaryEncodeU64(const uint64_t value, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 221 | spv_instruction_t* pInst) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 222 | uint32_t low = uint32_t(0x00000000ffffffff & value); |
| 223 | uint32_t high = uint32_t((0xffffffff00000000 & value) >> 32); |
David Neto | ac508b0 | 2015-10-09 15:48:09 -0400 | [diff] [blame] | 224 | binaryEncodeU32(low, pInst); |
| 225 | binaryEncodeU32(high, pInst); |
| 226 | return SPV_SUCCESS; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 227 | } |
| 228 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 229 | spv_result_t AssemblyContext::binaryEncodeNumericLiteral( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 230 | const char* val, spv_result_t error_code, const IdType& type, |
| 231 | spv_instruction_t* pInst) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 232 | const bool is_bottom = type.type_class == libspirv::IdTypeClass::kBottom; |
| 233 | const bool is_floating = libspirv::isScalarFloating(type); |
| 234 | const bool is_integer = libspirv::isScalarIntegral(type); |
| 235 | |
| 236 | if (!is_bottom && !is_floating && !is_integer) { |
| 237 | return diagnostic(SPV_ERROR_INTERNAL) |
| 238 | << "The expected type is not a scalar integer or float type"; |
| 239 | } |
| 240 | |
| 241 | // If this is bottom, but looks like a float, we should treat it like a |
| 242 | // float. |
| 243 | const bool looks_like_float = is_bottom && strchr(val, '.'); |
| 244 | |
| 245 | // If we explicitly expect a floating-point number, we should handle that |
| 246 | // first. |
| 247 | if (is_floating || looks_like_float) |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 248 | return binaryEncodeFloatingPointLiteral(val, error_code, type, pInst); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 249 | |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 250 | return binaryEncodeIntegerLiteral(val, error_code, type, pInst); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 253 | spv_result_t AssemblyContext::binaryEncodeString(const char* value, |
| 254 | spv_instruction_t* pInst) { |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 255 | const size_t length = strlen(value); |
| 256 | const size_t wordCount = (length / 4) + 1; |
| 257 | const size_t oldWordCount = pInst->words.size(); |
| 258 | const size_t newWordCount = oldWordCount + wordCount; |
| 259 | |
| 260 | // TODO(dneto): We can just defer this check until later. |
| 261 | if (newWordCount > SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 262 | return diagnostic() << "Instruction too long: more than " |
| 263 | << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words."; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 264 | } |
| 265 | |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 266 | pInst->words.resize(newWordCount); |
| 267 | |
| 268 | // Make sure all the bytes in the last word are 0, in case we only |
| 269 | // write a partial word at the end. |
| 270 | pInst->words.back() = 0; |
| 271 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 272 | char* dest = (char*)&pInst->words[oldWordCount]; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 273 | strncpy(dest, value, length); |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 274 | |
| 275 | return SPV_SUCCESS; |
| 276 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 277 | |
| 278 | spv_result_t AssemblyContext::recordTypeDefinition( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 279 | const spv_instruction_t* pInst) { |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 280 | uint32_t value = pInst->words[1]; |
| 281 | if (types_.find(value) != types_.end()) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 282 | return diagnostic() << "Value " << value |
| 283 | << " has already been used to generate a type"; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 286 | if (pInst->opcode == SpvOpTypeInt) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 287 | if (pInst->words.size() != 4) |
| 288 | return diagnostic() << "Invalid OpTypeInt instruction"; |
| 289 | types_[value] = {pInst->words[2], pInst->words[3] != 0, |
| 290 | IdTypeClass::kScalarIntegerType}; |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 291 | } else if (pInst->opcode == SpvOpTypeFloat) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 292 | if (pInst->words.size() != 3) |
| 293 | return diagnostic() << "Invalid OpTypeFloat instruction"; |
| 294 | types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 295 | } else { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 296 | types_[value] = {0, false, IdTypeClass::kOtherType}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 297 | } |
| 298 | return SPV_SUCCESS; |
| 299 | } |
| 300 | |
| 301 | IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const { |
| 302 | auto type = types_.find(value); |
| 303 | if (type == types_.end()) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 304 | return kUnknownType; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 305 | } |
| 306 | return std::get<1>(*type); |
| 307 | } |
| 308 | |
| 309 | IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const { |
| 310 | auto type_value = value_types_.find(value); |
| 311 | if (type_value == value_types_.end()) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 312 | return {0, false, IdTypeClass::kBottom}; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 313 | } |
| 314 | return getTypeOfTypeGeneratingValue(std::get<1>(*type_value)); |
| 315 | } |
| 316 | |
| 317 | spv_result_t AssemblyContext::recordTypeIdForValue(uint32_t value, |
| 318 | uint32_t type) { |
| 319 | bool successfully_inserted = false; |
| 320 | std::tie(std::ignore, successfully_inserted) = |
| 321 | value_types_.insert(std::make_pair(value, type)); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 322 | if (!successfully_inserted) |
| 323 | return diagnostic() << "Value is being defined a second time"; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 324 | return SPV_SUCCESS; |
| 325 | } |
| 326 | |
David Neto | 2ae4a68 | 2015-11-09 18:55:42 -0500 | [diff] [blame] | 327 | spv_result_t AssemblyContext::recordIdAsExtInstImport( |
| 328 | uint32_t id, spv_ext_inst_type_t type) { |
| 329 | bool successfully_inserted = false; |
| 330 | std::tie(std::ignore, successfully_inserted) = |
| 331 | import_id_to_ext_inst_type_.insert(std::make_pair(id, type)); |
| 332 | if (!successfully_inserted) |
| 333 | return diagnostic() << "Import Id is being defined a second time"; |
| 334 | return SPV_SUCCESS; |
| 335 | } |
| 336 | |
| 337 | spv_ext_inst_type_t AssemblyContext::getExtInstTypeForId(uint32_t id) const { |
| 338 | auto type = import_id_to_ext_inst_type_.find(id); |
| 339 | if (type == import_id_to_ext_inst_type_.end()) { |
| 340 | return SPV_EXT_INST_TYPE_NONE; |
| 341 | } |
| 342 | return std::get<1>(*type); |
| 343 | } |
| 344 | |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 345 | spv_result_t AssemblyContext::binaryEncodeFloatingPointLiteral( |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 346 | const char* val, spv_result_t error_code, const IdType& type, |
| 347 | spv_instruction_t* pInst) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 348 | const auto bit_width = assumedBitWidth(type); |
| 349 | switch (bit_width) { |
Andrew Woloszyn | 43401d2 | 2016-01-08 09:54:42 -0500 | [diff] [blame] | 350 | case 16: { |
| 351 | spvutils::HexFloat<FloatProxy<spvutils::Float16>> hVal(0); |
| 352 | if (auto error = parseNumber(val, error_code, &hVal, |
| 353 | "Invalid 16-bit float literal: ")) |
| 354 | return error; |
| 355 | // getAsFloat will return the spvutils::Float16 value, and get_value |
| 356 | // will return a uint16_t representing the bits of the float. |
| 357 | // The encoding is therefore correct from the perspective of the SPIR-V |
| 358 | // spec since the top 16 bits will be 0. |
| 359 | return binaryEncodeU32( |
| 360 | static_cast<uint32_t>(hVal.value().getAsFloat().get_value()), pInst); |
| 361 | } break; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 362 | case 32: { |
David Neto | 9e545d7 | 2015-11-06 18:08:49 -0500 | [diff] [blame] | 363 | spvutils::HexFloat<FloatProxy<float>> fVal(0.0f); |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 364 | if (auto error = parseNumber(val, error_code, &fVal, |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 365 | "Invalid 32-bit float literal: ")) |
| 366 | return error; |
| 367 | return binaryEncodeU32(BitwiseCast<uint32_t>(fVal), pInst); |
| 368 | } break; |
| 369 | case 64: { |
David Neto | 9e545d7 | 2015-11-06 18:08:49 -0500 | [diff] [blame] | 370 | spvutils::HexFloat<FloatProxy<double>> dVal(0.0); |
David Neto | 51013d1 | 2015-10-14 11:31:51 -0400 | [diff] [blame] | 371 | if (auto error = parseNumber(val, error_code, &dVal, |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 372 | "Invalid 64-bit float literal: ")) |
| 373 | return error; |
| 374 | return binaryEncodeU64(BitwiseCast<uint64_t>(dVal), pInst); |
| 375 | } break; |
| 376 | default: |
| 377 | break; |
| 378 | } |
| 379 | return diagnostic() << "Unsupported " << bit_width << "-bit float literals"; |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 380 | } |
| 381 | |
Andrew Woloszyn | 4ddb431 | 2016-02-17 13:00:23 -0500 | [diff] [blame] | 382 | // Returns SPV_SUCCESS if the given value fits within the target scalar |
| 383 | // integral type. The target type may have an unusual bit width. |
| 384 | // If the value was originally specified as a hexadecimal number, then |
| 385 | // the overflow bits should be zero. If it was hex and the target type is |
| 386 | // signed, then return the sign-extended value through the |
| 387 | // updated_value_for_hex pointer argument. |
| 388 | // On failure, return the given error code and emit a diagnostic if that error |
| 389 | // code is not SPV_FAILED_MATCH. |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 390 | template <typename T> |
Andrew Woloszyn | 4ddb431 | 2016-02-17 13:00:23 -0500 | [diff] [blame] | 391 | spv_result_t checkRangeAndIfHexThenSignExtend(T value, spv_result_t error_code, |
| 392 | const IdType& type, bool is_hex, |
| 393 | T* updated_value_for_hex) { |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 394 | // The encoded result has three regions of bits that are of interest, from |
| 395 | // least to most significant: |
| 396 | // - magnitude bits, where the magnitude of the number would be stored if |
| 397 | // we were using a signed-magnitude representation. |
| 398 | // - an optional sign bit |
| 399 | // - overflow bits, up to bit 63 of a 64-bit number |
| 400 | // For example: |
| 401 | // Type Overflow Sign Magnitude |
| 402 | // --------------- -------- ---- --------- |
| 403 | // unsigned 8 bit 8-63 n/a 0-7 |
| 404 | // signed 8 bit 8-63 7 0-6 |
| 405 | // unsigned 16 bit 16-63 n/a 0-15 |
| 406 | // signed 16 bit 16-63 15 0-14 |
| 407 | |
| 408 | // We'll use masks to define the three regions. |
| 409 | // At first we'll assume the number is unsigned. |
| 410 | const uint32_t bit_width = assumedBitWidth(type); |
| 411 | uint64_t magnitude_mask = |
| 412 | (bit_width == 64) ? -1 : ((uint64_t(1) << bit_width) - 1); |
| 413 | uint64_t sign_mask = 0; |
| 414 | uint64_t overflow_mask = ~magnitude_mask; |
| 415 | |
| 416 | if (value < 0 || type.isSigned) { |
| 417 | // Accommodate the sign bit. |
| 418 | magnitude_mask >>= 1; |
| 419 | sign_mask = magnitude_mask + 1; |
| 420 | } |
| 421 | |
| 422 | bool failed = false; |
| 423 | if (value < 0) { |
| 424 | // The top bits must all be 1 for a negative signed value. |
| 425 | failed = ((value & overflow_mask) != overflow_mask) || |
| 426 | ((value & sign_mask) != sign_mask); |
| 427 | } else { |
| 428 | if (is_hex) { |
| 429 | // Hex values are a bit special. They decode as unsigned values, but |
| 430 | // may represent a negative number. In this case, the overflow bits |
| 431 | // should be zero. |
Ben Vanik | 01c8d7a | 2015-11-22 08:32:53 -0800 | [diff] [blame] | 432 | failed = (value & overflow_mask) != 0; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 433 | } else { |
Lei Zhang | 8bd75d6 | 2015-11-18 09:22:10 -0500 | [diff] [blame] | 434 | const uint64_t value_as_u64 = static_cast<uint64_t>(value); |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 435 | // Check overflow in the ordinary case. |
Lei Zhang | 8bd75d6 | 2015-11-18 09:22:10 -0500 | [diff] [blame] | 436 | failed = (value_as_u64 & magnitude_mask) != value_as_u64; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | if (failed) { |
Andrew Woloszyn | 4ddb431 | 2016-02-17 13:00:23 -0500 | [diff] [blame] | 441 | return error_code; |
David Neto | 78e677b | 2015-10-05 13:28:46 -0400 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Sign extend hex the number. |
| 445 | if (is_hex && (value & sign_mask)) |
| 446 | *updated_value_for_hex = (value | overflow_mask); |
| 447 | |
| 448 | return SPV_SUCCESS; |
| 449 | } |
Andrew Woloszyn | 4ddb431 | 2016-02-17 13:00:23 -0500 | [diff] [blame] | 450 | |
| 451 | spv_result_t AssemblyContext::binaryEncodeIntegerLiteral( |
| 452 | const char* val, spv_result_t error_code, const IdType& type, |
| 453 | spv_instruction_t* pInst) { |
| 454 | const bool is_bottom = type.type_class == libspirv::IdTypeClass::kBottom; |
| 455 | const uint32_t bit_width = assumedBitWidth(type); |
| 456 | |
| 457 | if (bit_width > 64) |
| 458 | return diagnostic(SPV_ERROR_INTERNAL) << "Unsupported " << bit_width |
| 459 | << "-bit integer literals"; |
| 460 | |
| 461 | // Either we are expecting anything or integer. |
| 462 | bool is_negative = val[0] == '-'; |
| 463 | bool can_be_signed = is_bottom || type.isSigned; |
| 464 | |
| 465 | if (is_negative && !can_be_signed) { |
| 466 | return diagnostic() |
| 467 | << "Cannot put a negative number in an unsigned literal"; |
| 468 | } |
| 469 | |
| 470 | const bool is_hex = val[0] == '0' && (val[1] == 'x' || val[1] == 'X'); |
| 471 | |
| 472 | uint64_t decoded_bits; |
| 473 | if (is_negative) { |
| 474 | int64_t decoded_signed = 0; |
| 475 | |
| 476 | if (auto error = parseNumber(val, error_code, &decoded_signed, |
| 477 | "Invalid signed integer literal: ")) |
| 478 | return error; |
| 479 | if (auto error = checkRangeAndIfHexThenSignExtend( |
| 480 | decoded_signed, error_code, type, is_hex, &decoded_signed)) { |
| 481 | diagnostic(error_code) |
| 482 | << "Integer " << (is_hex ? std::hex : std::dec) << std::showbase |
| 483 | << decoded_signed << " does not fit in a " << std::dec << bit_width |
| 484 | << "-bit " << (type.isSigned ? "signed" : "unsigned") << " integer"; |
| 485 | return error; |
| 486 | } |
| 487 | decoded_bits = decoded_signed; |
| 488 | } else { |
| 489 | // There's no leading minus sign, so parse it as an unsigned integer. |
| 490 | if (auto error = parseNumber(val, error_code, &decoded_bits, |
| 491 | "Invalid unsigned integer literal: ")) |
| 492 | return error; |
| 493 | if (auto error = checkRangeAndIfHexThenSignExtend( |
| 494 | decoded_bits, error_code, type, is_hex, &decoded_bits)) { |
| 495 | diagnostic(error_code) |
| 496 | << "Integer " << (is_hex ? std::hex : std::dec) << std::showbase |
| 497 | << decoded_bits << " does not fit in a " << std::dec << bit_width |
| 498 | << "-bit " << (type.isSigned ? "signed" : "unsigned") << " integer"; |
| 499 | return error; |
| 500 | } |
| 501 | } |
| 502 | if (bit_width > 32) { |
| 503 | return binaryEncodeU64(decoded_bits, pInst); |
| 504 | } else { |
| 505 | return binaryEncodeU32(uint32_t(decoded_bits), pInst); |
| 506 | } |
| 507 | } |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 508 | } // namespace libspirv |