llvm.org GIT mirror llvm / c27f000
[MIRParser] Add parsing hex literals of arbitrary size as unsigned integers The current code does not parse hex literals larger than 32-bit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@289943 91177308-0d34-0410-b5e6-96231b3b80d8 Krzysztof Parzyszek 3 years ago
1 changed file(s) with 39 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
196196 /// Return true if an error occurred.
197197 bool getUint64(uint64_t &Result);
198198
199 /// Convert the hexadecimal literal in the current token into an unsigned
200 /// APInt with a minimum bitwidth required to represent the value.
201 ///
202 /// Return true if the literal does not represent an integer value.
203 bool getHexUint(APInt &Result);
204
199205 /// If the current token is of the given kind, consume it and return false.
200206 /// Otherwise report an error and return true.
201207 bool expectAndConsume(MIToken::TokenKind TokenKind);
11591165 return false;
11601166 }
11611167 if (Token.is(MIToken::HexLiteral)) {
1162 StringRef S = Token.range();
1163 assert(S[0] == '0' && tolower(S[1]) == 'x');
1164 // This could be a floating point literal with a special prefix.
1165 if (!isxdigit(S[2]))
1166 return true;
1167 StringRef V = S.substr(2);
1168 unsigned BW = std::min(V.size()*4, 32);
1169 APInt A(BW, V, 16);
1170 APInt Limit = APInt(BW, std::numeric_limits::max());
1171 if (A.ugt(Limit))
1168 APInt A;
1169 if (getHexUint(A))
1170 return true;
1171 if (A.getBitWidth() > 32)
11721172 return error("expected 32-bit integer (too large)");
11731173 Result = A.getZExtValue();
11741174 return false;
18221822 }
18231823
18241824 bool MIParser::getUint64(uint64_t &Result) {
1825 assert(Token.hasIntegerValue());
1826 if (Token.integerValue().getActiveBits() > 64)
1827 return error("expected 64-bit integer (too large)");
1828 Result = Token.integerValue().getZExtValue();
1825 if (Token.hasIntegerValue()) {
1826 if (Token.integerValue().getActiveBits() > 64)
1827 return error("expected 64-bit integer (too large)");
1828 Result = Token.integerValue().getZExtValue();
1829 return false;
1830 }
1831 if (Token.is(MIToken::HexLiteral)) {
1832 APInt A;
1833 if (getHexUint(A))
1834 return true;
1835 if (A.getBitWidth() > 64)
1836 return error("expected 64-bit integer (too large)");
1837 Result = A.getZExtValue();
1838 return false;
1839 }
1840 return true;
1841 }
1842
1843 bool MIParser::getHexUint(APInt &Result) {
1844 assert(Token.is(MIToken::HexLiteral));
1845 StringRef S = Token.range();
1846 assert(S[0] == '0' && tolower(S[1]) == 'x');
1847 // This could be a floating point literal with a special prefix.
1848 if (!isxdigit(S[2]))
1849 return true;
1850 StringRef V = S.substr(2);
1851 APInt A(V.size()*4, V, 16);
1852 Result = APInt(A.getActiveBits(),
1853 ArrayRef(A.getRawData(), A.getNumWords()));
18291854 return false;
18301855 }
18311856