blob: b7ee5876b5a20b0bc96148c729fa16c10463b914 [file] [log] [blame]
Andrew Woloszyn71fc0552015-09-24 10:26:51 -04001// 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 Woloszyn71fc0552015-09-24 10:26:51 -040029#include <cassert>
David Neto78e677b2015-10-05 13:28:46 -040030#include <cstdlib>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040031#include <cstring>
Andrew Woloszyn537e7762015-09-29 11:28:34 -040032#include <tuple>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040033
David Netofcc7d582015-10-27 15:31:10 -040034#include "assembly_grammar.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040035#include "binary.h"
36#include "ext_inst.h"
David Netob5dc8fc2015-10-06 16:22:00 -040037#include "instruction.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040038#include "opcode.h"
39#include "text.h"
Andrew Woloszynf731cbf2015-10-23 09:53:58 -040040#include "util/bitutils.h"
David Neto9e545d72015-11-06 18:08:49 -050041#include "util/hex_float.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040042
43namespace {
44
David Neto78e677b2015-10-05 13:28:46 -040045using spvutils::BitwiseCast;
David Neto9e545d72015-11-06 18:08:49 -050046using spvutils::FloatProxy;
47using spvutils::HexFloat;
David Neto78e677b2015-10-05 13:28:46 -040048
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040049/// @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
55spv_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
82spv_result_t advance(spv_text text, spv_position position) {
83 // NOTE: Consume white space, otherwise don't advance.
Lei Zhang8f6ba142015-11-06 15:09:04 -050084 if (position->index >= text->length) return SPV_END_OF_STREAM;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040085 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 Zhang1a0334e2015-11-02 09:41:20 -0500118spv_result_t getWord(spv_text text, spv_position position, std::string& word,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400119 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.
162bool 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 Woloszyn71fc0552015-09-24 10:26:51 -0400170} // anonymous namespace
171
172namespace libspirv {
173
David Neto78e677b2015-10-05 13:28:46 -0400174const IdType kUnknownType = {0, false, IdTypeClass::kBottom};
175
David Neto78e677b2015-10-05 13:28:46 -0400176// TODO(dneto): Reorder AssemblyContext definitions to match declaration order.
177
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400178// This represents all of the data that is only valid for the duration of
179// a single compilation.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500180uint32_t AssemblyContext::spvNamedIdAssignOrGet(const char* textValue) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400181 if (named_ids_.end() == named_ids_.find(textValue)) {
182 named_ids_[std::string(textValue)] = bound_++;
183 }
184 return named_ids_[textValue];
185}
186uint32_t AssemblyContext::getBound() const { return bound_; }
187
188spv_result_t AssemblyContext::advance() {
189 return ::advance(text_, &current_position_);
190}
191
Lei Zhang1a0334e2015-11-02 09:41:20 -0500192spv_result_t AssemblyContext::getWord(std::string& word,
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400193 spv_position endPosition) {
194 return ::getWord(text_, &current_position_, word, endPosition);
195}
196
197bool AssemblyContext::startsWithOp() {
198 return ::startsWithOp(text_, &current_position_);
199}
200
201bool 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 Woloszyn4274f932015-10-20 09:12:32 -0400221
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400222char AssemblyContext::peek() const {
223 return text_->str[current_position_.index];
224}
225
226bool AssemblyContext::hasText() const {
227 return text_->length > current_position_.index;
228}
Andrew Woloszyn4274f932015-10-20 09:12:32 -0400229
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400230std::string AssemblyContext::getWord() const {
Andrew Woloszyn4274f932015-10-20 09:12:32 -0400231 uint64_t index = current_position_.index;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400232 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
249void AssemblyContext::seekForward(uint32_t size) {
250 current_position_.index += size;
251 current_position_.column += size;
252}
253
254spv_result_t AssemblyContext::binaryEncodeU32(const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500255 spv_instruction_t* pInst) {
David Netob5dc8fc2015-10-06 16:22:00 -0400256 spvInstructionAddWord(pInst, value);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400257 return SPV_SUCCESS;
258}
259
260spv_result_t AssemblyContext::binaryEncodeU64(const uint64_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500261 spv_instruction_t* pInst) {
David Neto78e677b2015-10-05 13:28:46 -0400262 uint32_t low = uint32_t(0x00000000ffffffff & value);
263 uint32_t high = uint32_t((0xffffffff00000000 & value) >> 32);
David Netoac508b02015-10-09 15:48:09 -0400264 binaryEncodeU32(low, pInst);
265 binaryEncodeU32(high, pInst);
266 return SPV_SUCCESS;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400267}
268
David Neto78e677b2015-10-05 13:28:46 -0400269spv_result_t AssemblyContext::binaryEncodeNumericLiteral(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500270 const char* val, spv_result_t error_code, const IdType& type,
271 spv_instruction_t* pInst) {
David Neto78e677b2015-10-05 13:28:46 -0400272 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 Neto51013d12015-10-14 11:31:51 -0400288 return binaryEncodeFloatingPointLiteral(val, error_code, type, pInst);
David Neto78e677b2015-10-05 13:28:46 -0400289
David Neto51013d12015-10-14 11:31:51 -0400290 return binaryEncodeIntegerLiteral(val, error_code, type, pInst);
David Neto78e677b2015-10-05 13:28:46 -0400291}
292
Lei Zhang1a0334e2015-11-02 09:41:20 -0500293spv_result_t AssemblyContext::binaryEncodeString(const char* value,
294 spv_instruction_t* pInst) {
David Netob5dc8fc2015-10-06 16:22:00 -0400295 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 Neto78e677b2015-10-05 13:28:46 -0400302 return diagnostic() << "Instruction too long: more than "
303 << SPV_LIMIT_INSTRUCTION_WORD_COUNT_MAX << " words.";
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400304 }
305
David Netob5dc8fc2015-10-06 16:22:00 -0400306 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 Zhang1a0334e2015-11-02 09:41:20 -0500312 char* dest = (char*)&pInst->words[oldWordCount];
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400313 strncpy(dest, value, length);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400314
315 return SPV_SUCCESS;
316}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400317
318spv_result_t AssemblyContext::recordTypeDefinition(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500319 const spv_instruction_t* pInst) {
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400320 uint32_t value = pInst->words[1];
321 if (types_.find(value) != types_.end()) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500322 return diagnostic() << "Value " << value
323 << " has already been used to generate a type";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400324 }
325
Lei Zhangb36e7042015-10-28 13:40:52 -0400326 if (pInst->opcode == SpvOpTypeInt) {
David Neto78e677b2015-10-05 13:28:46 -0400327 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 Zhangb36e7042015-10-28 13:40:52 -0400331 } else if (pInst->opcode == SpvOpTypeFloat) {
David Neto78e677b2015-10-05 13:28:46 -0400332 if (pInst->words.size() != 3)
333 return diagnostic() << "Invalid OpTypeFloat instruction";
334 types_[value] = {pInst->words[2], false, IdTypeClass::kScalarFloatType};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400335 } else {
David Neto78e677b2015-10-05 13:28:46 -0400336 types_[value] = {0, false, IdTypeClass::kOtherType};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400337 }
338 return SPV_SUCCESS;
339}
340
341IdType AssemblyContext::getTypeOfTypeGeneratingValue(uint32_t value) const {
342 auto type = types_.find(value);
343 if (type == types_.end()) {
David Neto78e677b2015-10-05 13:28:46 -0400344 return kUnknownType;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400345 }
346 return std::get<1>(*type);
347}
348
349IdType AssemblyContext::getTypeOfValueInstruction(uint32_t value) const {
350 auto type_value = value_types_.find(value);
351 if (type_value == value_types_.end()) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500352 return {0, false, IdTypeClass::kBottom};
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400353 }
354 return getTypeOfTypeGeneratingValue(std::get<1>(*type_value));
355}
356
357spv_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 Neto78e677b2015-10-05 13:28:46 -0400362 if (!successfully_inserted)
363 return diagnostic() << "Value is being defined a second time";
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400364 return SPV_SUCCESS;
365}
366
David Neto2ae4a682015-11-09 18:55:42 -0500367spv_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
377spv_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 Neto78e677b2015-10-05 13:28:46 -0400385spv_result_t AssemblyContext::binaryEncodeFloatingPointLiteral(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500386 const char* val, spv_result_t error_code, const IdType& type,
387 spv_instruction_t* pInst) {
David Neto78e677b2015-10-05 13:28:46 -0400388 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 Neto9e545d72015-11-06 18:08:49 -0500394 spvutils::HexFloat<FloatProxy<float>> fVal(0.0f);
David Neto51013d12015-10-14 11:31:51 -0400395 if (auto error = parseNumber(val, error_code, &fVal,
David Neto78e677b2015-10-05 13:28:46 -0400396 "Invalid 32-bit float literal: "))
397 return error;
398 return binaryEncodeU32(BitwiseCast<uint32_t>(fVal), pInst);
399 } break;
400 case 64: {
David Neto9e545d72015-11-06 18:08:49 -0500401 spvutils::HexFloat<FloatProxy<double>> dVal(0.0);
David Neto51013d12015-10-14 11:31:51 -0400402 if (auto error = parseNumber(val, error_code, &dVal,
David Neto78e677b2015-10-05 13:28:46 -0400403 "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 Woloszyn71fc0552015-09-24 10:26:51 -0400411}
412
David Neto78e677b2015-10-05 13:28:46 -0400413spv_result_t AssemblyContext::binaryEncodeIntegerLiteral(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500414 const char* val, spv_result_t error_code, const IdType& type,
415 spv_instruction_t* pInst) {
David Neto78e677b2015-10-05 13:28:46 -0400416 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 Neto51013d12015-10-14 11:31:51 -0400437 if (auto error = parseNumber(val, error_code, &decoded_signed,
David Neto78e677b2015-10-05 13:28:46 -0400438 "Invalid signed integer literal: "))
439 return error;
440 if (auto error = checkRangeAndIfHexThenSignExtend(
David Neto51013d12015-10-14 11:31:51 -0400441 decoded_signed, error_code, type, is_hex, &decoded_signed))
David Neto78e677b2015-10-05 13:28:46 -0400442 return error;
443 decoded_bits = decoded_signed;
444 } else {
445 // There's no leading minus sign, so parse it as an unsigned integer.
David Neto51013d12015-10-14 11:31:51 -0400446 if (auto error = parseNumber(val, error_code, &decoded_bits,
David Neto78e677b2015-10-05 13:28:46 -0400447 "Invalid unsigned integer literal: "))
448 return error;
449 if (auto error = checkRangeAndIfHexThenSignExtend(
David Neto51013d12015-10-14 11:31:51 -0400450 decoded_bits, error_code, type, is_hex, &decoded_bits))
David Neto78e677b2015-10-05 13:28:46 -0400451 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
460template <typename T>
461spv_result_t AssemblyContext::checkRangeAndIfHexThenSignExtend(
Lei Zhang1a0334e2015-11-02 09:41:20 -0500462 T value, spv_result_t error_code, const IdType& type, bool is_hex,
463 T* updated_value_for_hex) {
David Neto78e677b2015-10-05 13:28:46 -0400464 // 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.
502 failed = (value & overflow_mask);
503 } else {
Lei Zhang8bd75d62015-11-18 09:22:10 -0500504 const uint64_t value_as_u64 = static_cast<uint64_t>(value);
David Neto78e677b2015-10-05 13:28:46 -0400505 // Check overflow in the ordinary case.
Lei Zhang8bd75d62015-11-18 09:22:10 -0500506 failed = (value_as_u64 & magnitude_mask) != value_as_u64;
David Neto78e677b2015-10-05 13:28:46 -0400507 }
508 }
509
510 if (failed) {
David Neto51013d12015-10-14 11:31:51 -0400511 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 Neto78e677b2015-10-05 13:28:46 -0400515 }
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 Zhang1a0334e2015-11-02 09:41:20 -0500523} // namespace libspirv