LLLexer.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Implement the Lexer for .ll files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/AsmParser/LLLexer.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/StringExtras.h"
  16. #include "llvm/ADT/Twine.h"
  17. #include "llvm/IR/DerivedTypes.h"
  18. #include "llvm/IR/Instruction.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include "llvm/Support/SourceMgr.h"
  21. #include <cassert>
  22. #include <cctype>
  23. #include <cstdio>
  24. using namespace llvm;
  25. bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
  26. ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
  27. return true;
  28. }
  29. void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
  30. SM.PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
  31. }
  32. //===----------------------------------------------------------------------===//
  33. // Helper functions.
  34. //===----------------------------------------------------------------------===//
  35. // atoull - Convert an ascii string of decimal digits into the unsigned long
  36. // long representation... this does not have to do input error checking,
  37. // because we know that the input will be matched by a suitable regex...
  38. //
  39. uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
  40. uint64_t Result = 0;
  41. for (; Buffer != End; Buffer++) {
  42. uint64_t OldRes = Result;
  43. Result *= 10;
  44. Result += *Buffer-'0';
  45. if (Result < OldRes) { // Uh, oh, overflow detected!!!
  46. Error("constant bigger than 64 bits detected!");
  47. return 0;
  48. }
  49. }
  50. return Result;
  51. }
  52. uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
  53. uint64_t Result = 0;
  54. for (; Buffer != End; ++Buffer) {
  55. uint64_t OldRes = Result;
  56. Result *= 16;
  57. Result += hexDigitValue(*Buffer);
  58. if (Result < OldRes) { // Uh, oh, overflow detected!!!
  59. Error("constant bigger than 64 bits detected!");
  60. return 0;
  61. }
  62. }
  63. return Result;
  64. }
  65. void LLLexer::HexToIntPair(const char *Buffer, const char *End,
  66. uint64_t Pair[2]) {
  67. Pair[0] = 0;
  68. if (End - Buffer >= 16) {
  69. for (int i = 0; i < 16; i++, Buffer++) {
  70. assert(Buffer != End);
  71. Pair[0] *= 16;
  72. Pair[0] += hexDigitValue(*Buffer);
  73. }
  74. }
  75. Pair[1] = 0;
  76. for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
  77. Pair[1] *= 16;
  78. Pair[1] += hexDigitValue(*Buffer);
  79. }
  80. if (Buffer != End)
  81. Error("constant bigger than 128 bits detected!");
  82. }
  83. /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
  84. /// { low64, high16 } as usual for an APInt.
  85. void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
  86. uint64_t Pair[2]) {
  87. Pair[1] = 0;
  88. for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
  89. assert(Buffer != End);
  90. Pair[1] *= 16;
  91. Pair[1] += hexDigitValue(*Buffer);
  92. }
  93. Pair[0] = 0;
  94. for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
  95. Pair[0] *= 16;
  96. Pair[0] += hexDigitValue(*Buffer);
  97. }
  98. if (Buffer != End)
  99. Error("constant bigger than 128 bits detected!");
  100. }
  101. // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
  102. // appropriate character.
  103. static void UnEscapeLexed(std::string &Str) {
  104. if (Str.empty()) return;
  105. char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
  106. char *BOut = Buffer;
  107. for (char *BIn = Buffer; BIn != EndBuffer; ) {
  108. if (BIn[0] == '\\') {
  109. if (BIn < EndBuffer-1 && BIn[1] == '\\') {
  110. *BOut++ = '\\'; // Two \ becomes one
  111. BIn += 2;
  112. } else if (BIn < EndBuffer-2 &&
  113. isxdigit(static_cast<unsigned char>(BIn[1])) &&
  114. isxdigit(static_cast<unsigned char>(BIn[2]))) {
  115. *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
  116. BIn += 3; // Skip over handled chars
  117. ++BOut;
  118. } else {
  119. *BOut++ = *BIn++;
  120. }
  121. } else {
  122. *BOut++ = *BIn++;
  123. }
  124. }
  125. Str.resize(BOut-Buffer);
  126. }
  127. /// isLabelChar - Return true for [-a-zA-Z$._0-9].
  128. static bool isLabelChar(char C) {
  129. return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
  130. C == '.' || C == '_';
  131. }
  132. /// isLabelTail - Return true if this pointer points to a valid end of a label.
  133. static const char *isLabelTail(const char *CurPtr) {
  134. while (true) {
  135. if (CurPtr[0] == ':') return CurPtr+1;
  136. if (!isLabelChar(CurPtr[0])) return nullptr;
  137. ++CurPtr;
  138. }
  139. }
  140. //===----------------------------------------------------------------------===//
  141. // Lexer definition.
  142. //===----------------------------------------------------------------------===//
  143. LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
  144. LLVMContext &C)
  145. : CurBuf(StartBuf), ErrorInfo(Err), SM(SM), Context(C), APFloatVal(0.0),
  146. IgnoreColonInIdentifiers(false) {
  147. CurPtr = CurBuf.begin();
  148. }
  149. int LLLexer::getNextChar() {
  150. char CurChar = *CurPtr++;
  151. switch (CurChar) {
  152. default: return (unsigned char)CurChar;
  153. case 0:
  154. // A nul character in the stream is either the end of the current buffer or
  155. // a random nul in the file. Disambiguate that here.
  156. if (CurPtr-1 != CurBuf.end())
  157. return 0; // Just whitespace.
  158. // Otherwise, return end of file.
  159. --CurPtr; // Another call to lex will return EOF again.
  160. return EOF;
  161. }
  162. }
  163. lltok::Kind LLLexer::LexToken() {
  164. while (true) {
  165. TokStart = CurPtr;
  166. int CurChar = getNextChar();
  167. switch (CurChar) {
  168. default:
  169. // Handle letters: [a-zA-Z_]
  170. if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
  171. return LexIdentifier();
  172. return lltok::Error;
  173. case EOF: return lltok::Eof;
  174. case 0:
  175. case ' ':
  176. case '\t':
  177. case '\n':
  178. case '\r':
  179. // Ignore whitespace.
  180. continue;
  181. case '+': return LexPositive();
  182. case '@': return LexAt();
  183. case '$': return LexDollar();
  184. case '%': return LexPercent();
  185. case '"': return LexQuote();
  186. case '.':
  187. if (const char *Ptr = isLabelTail(CurPtr)) {
  188. CurPtr = Ptr;
  189. StrVal.assign(TokStart, CurPtr-1);
  190. return lltok::LabelStr;
  191. }
  192. if (CurPtr[0] == '.' && CurPtr[1] == '.') {
  193. CurPtr += 2;
  194. return lltok::dotdotdot;
  195. }
  196. return lltok::Error;
  197. case ';':
  198. SkipLineComment();
  199. continue;
  200. case '!': return LexExclaim();
  201. case '^':
  202. return LexCaret();
  203. case ':':
  204. return lltok::colon;
  205. case '#': return LexHash();
  206. case '0': case '1': case '2': case '3': case '4':
  207. case '5': case '6': case '7': case '8': case '9':
  208. case '-':
  209. return LexDigitOrNegative();
  210. case '=': return lltok::equal;
  211. case '[': return lltok::lsquare;
  212. case ']': return lltok::rsquare;
  213. case '{': return lltok::lbrace;
  214. case '}': return lltok::rbrace;
  215. case '<': return lltok::less;
  216. case '>': return lltok::greater;
  217. case '(': return lltok::lparen;
  218. case ')': return lltok::rparen;
  219. case ',': return lltok::comma;
  220. case '*': return lltok::star;
  221. case '|': return lltok::bar;
  222. }
  223. }
  224. }
  225. void LLLexer::SkipLineComment() {
  226. while (true) {
  227. if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
  228. return;
  229. }
  230. }
  231. /// Lex all tokens that start with an @ character.
  232. /// GlobalVar @\"[^\"]*\"
  233. /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
  234. /// GlobalVarID @[0-9]+
  235. lltok::Kind LLLexer::LexAt() {
  236. return LexVar(lltok::GlobalVar, lltok::GlobalID);
  237. }
  238. lltok::Kind LLLexer::LexDollar() {
  239. if (const char *Ptr = isLabelTail(TokStart)) {
  240. CurPtr = Ptr;
  241. StrVal.assign(TokStart, CurPtr - 1);
  242. return lltok::LabelStr;
  243. }
  244. // Handle DollarStringConstant: $\"[^\"]*\"
  245. if (CurPtr[0] == '"') {
  246. ++CurPtr;
  247. while (true) {
  248. int CurChar = getNextChar();
  249. if (CurChar == EOF) {
  250. Error("end of file in COMDAT variable name");
  251. return lltok::Error;
  252. }
  253. if (CurChar == '"') {
  254. StrVal.assign(TokStart + 2, CurPtr - 1);
  255. UnEscapeLexed(StrVal);
  256. if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
  257. Error("Null bytes are not allowed in names");
  258. return lltok::Error;
  259. }
  260. return lltok::ComdatVar;
  261. }
  262. }
  263. }
  264. // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
  265. if (ReadVarName())
  266. return lltok::ComdatVar;
  267. return lltok::Error;
  268. }
  269. /// ReadString - Read a string until the closing quote.
  270. lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
  271. const char *Start = CurPtr;
  272. while (true) {
  273. int CurChar = getNextChar();
  274. if (CurChar == EOF) {
  275. Error("end of file in string constant");
  276. return lltok::Error;
  277. }
  278. if (CurChar == '"') {
  279. StrVal.assign(Start, CurPtr-1);
  280. UnEscapeLexed(StrVal);
  281. return kind;
  282. }
  283. }
  284. }
  285. /// ReadVarName - Read the rest of a token containing a variable name.
  286. bool LLLexer::ReadVarName() {
  287. const char *NameStart = CurPtr;
  288. if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
  289. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  290. CurPtr[0] == '.' || CurPtr[0] == '_') {
  291. ++CurPtr;
  292. while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
  293. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  294. CurPtr[0] == '.' || CurPtr[0] == '_')
  295. ++CurPtr;
  296. StrVal.assign(NameStart, CurPtr);
  297. return true;
  298. }
  299. return false;
  300. }
  301. // Lex an ID: [0-9]+. On success, the ID is stored in UIntVal and Token is
  302. // returned, otherwise the Error token is returned.
  303. lltok::Kind LLLexer::LexUIntID(lltok::Kind Token) {
  304. if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
  305. return lltok::Error;
  306. for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  307. /*empty*/;
  308. uint64_t Val = atoull(TokStart + 1, CurPtr);
  309. if ((unsigned)Val != Val)
  310. Error("invalid value number (too large)!");
  311. UIntVal = unsigned(Val);
  312. return Token;
  313. }
  314. lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
  315. // Handle StringConstant: \"[^\"]*\"
  316. if (CurPtr[0] == '"') {
  317. ++CurPtr;
  318. while (true) {
  319. int CurChar = getNextChar();
  320. if (CurChar == EOF) {
  321. Error("end of file in global variable name");
  322. return lltok::Error;
  323. }
  324. if (CurChar == '"') {
  325. StrVal.assign(TokStart+2, CurPtr-1);
  326. UnEscapeLexed(StrVal);
  327. if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
  328. Error("Null bytes are not allowed in names");
  329. return lltok::Error;
  330. }
  331. return Var;
  332. }
  333. }
  334. }
  335. // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
  336. if (ReadVarName())
  337. return Var;
  338. // Handle VarID: [0-9]+
  339. return LexUIntID(VarID);
  340. }
  341. /// Lex all tokens that start with a % character.
  342. /// LocalVar ::= %\"[^\"]*\"
  343. /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
  344. /// LocalVarID ::= %[0-9]+
  345. lltok::Kind LLLexer::LexPercent() {
  346. return LexVar(lltok::LocalVar, lltok::LocalVarID);
  347. }
  348. /// Lex all tokens that start with a " character.
  349. /// QuoteLabel "[^"]+":
  350. /// StringConstant "[^"]*"
  351. lltok::Kind LLLexer::LexQuote() {
  352. lltok::Kind kind = ReadString(lltok::StringConstant);
  353. if (kind == lltok::Error || kind == lltok::Eof)
  354. return kind;
  355. if (CurPtr[0] == ':') {
  356. ++CurPtr;
  357. if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
  358. Error("Null bytes are not allowed in names");
  359. kind = lltok::Error;
  360. } else {
  361. kind = lltok::LabelStr;
  362. }
  363. }
  364. return kind;
  365. }
  366. /// Lex all tokens that start with a ! character.
  367. /// !foo
  368. /// !
  369. lltok::Kind LLLexer::LexExclaim() {
  370. // Lex a metadata name as a MetadataVar.
  371. if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
  372. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  373. CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
  374. ++CurPtr;
  375. while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
  376. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  377. CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
  378. ++CurPtr;
  379. StrVal.assign(TokStart+1, CurPtr); // Skip !
  380. UnEscapeLexed(StrVal);
  381. return lltok::MetadataVar;
  382. }
  383. return lltok::exclaim;
  384. }
  385. /// Lex all tokens that start with a ^ character.
  386. /// SummaryID ::= ^[0-9]+
  387. lltok::Kind LLLexer::LexCaret() {
  388. // Handle SummaryID: ^[0-9]+
  389. return LexUIntID(lltok::SummaryID);
  390. }
  391. /// Lex all tokens that start with a # character.
  392. /// AttrGrpID ::= #[0-9]+
  393. lltok::Kind LLLexer::LexHash() {
  394. // Handle AttrGrpID: #[0-9]+
  395. return LexUIntID(lltok::AttrGrpID);
  396. }
  397. /// Lex a label, integer type, keyword, or hexadecimal integer constant.
  398. /// Label [-a-zA-Z$._0-9]+:
  399. /// IntegerType i[0-9]+
  400. /// Keyword sdiv, float, ...
  401. /// HexIntConstant [us]0x[0-9A-Fa-f]+
  402. lltok::Kind LLLexer::LexIdentifier() {
  403. const char *StartChar = CurPtr;
  404. const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
  405. const char *KeywordEnd = nullptr;
  406. for (; isLabelChar(*CurPtr); ++CurPtr) {
  407. // If we decide this is an integer, remember the end of the sequence.
  408. if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
  409. IntEnd = CurPtr;
  410. if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
  411. *CurPtr != '_')
  412. KeywordEnd = CurPtr;
  413. }
  414. // If we stopped due to a colon, unless we were directed to ignore it,
  415. // this really is a label.
  416. if (!IgnoreColonInIdentifiers && *CurPtr == ':') {
  417. StrVal.assign(StartChar-1, CurPtr++);
  418. return lltok::LabelStr;
  419. }
  420. // Otherwise, this wasn't a label. If this was valid as an integer type,
  421. // return it.
  422. if (!IntEnd) IntEnd = CurPtr;
  423. if (IntEnd != StartChar) {
  424. CurPtr = IntEnd;
  425. uint64_t NumBits = atoull(StartChar, CurPtr);
  426. if (NumBits < IntegerType::MIN_INT_BITS ||
  427. NumBits > IntegerType::MAX_INT_BITS) {
  428. Error("bitwidth for integer type out of range!");
  429. return lltok::Error;
  430. }
  431. TyVal = IntegerType::get(Context, NumBits);
  432. return lltok::Type;
  433. }
  434. // Otherwise, this was a letter sequence. See which keyword this is.
  435. if (!KeywordEnd) KeywordEnd = CurPtr;
  436. CurPtr = KeywordEnd;
  437. --StartChar;
  438. StringRef Keyword(StartChar, CurPtr - StartChar);
  439. #define KEYWORD(STR) \
  440. do { \
  441. if (Keyword == #STR) \
  442. return lltok::kw_##STR; \
  443. } while (false)
  444. KEYWORD(true); KEYWORD(false);
  445. KEYWORD(declare); KEYWORD(define);
  446. KEYWORD(global); KEYWORD(constant);
  447. KEYWORD(dso_local);
  448. KEYWORD(dso_preemptable);
  449. KEYWORD(private);
  450. KEYWORD(internal);
  451. KEYWORD(available_externally);
  452. KEYWORD(linkonce);
  453. KEYWORD(linkonce_odr);
  454. KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
  455. KEYWORD(weak_odr);
  456. KEYWORD(appending);
  457. KEYWORD(dllimport);
  458. KEYWORD(dllexport);
  459. KEYWORD(common);
  460. KEYWORD(default);
  461. KEYWORD(hidden);
  462. KEYWORD(protected);
  463. KEYWORD(unnamed_addr);
  464. KEYWORD(local_unnamed_addr);
  465. KEYWORD(externally_initialized);
  466. KEYWORD(extern_weak);
  467. KEYWORD(external);
  468. KEYWORD(thread_local);
  469. KEYWORD(localdynamic);
  470. KEYWORD(initialexec);
  471. KEYWORD(localexec);
  472. KEYWORD(zeroinitializer);
  473. KEYWORD(undef);
  474. KEYWORD(null);
  475. KEYWORD(none);
  476. KEYWORD(poison);
  477. KEYWORD(to);
  478. KEYWORD(caller);
  479. KEYWORD(within);
  480. KEYWORD(from);
  481. KEYWORD(tail);
  482. KEYWORD(musttail);
  483. KEYWORD(notail);
  484. KEYWORD(target);
  485. KEYWORD(triple);
  486. KEYWORD(source_filename);
  487. KEYWORD(unwind);
  488. KEYWORD(datalayout);
  489. KEYWORD(volatile);
  490. KEYWORD(atomic);
  491. KEYWORD(unordered);
  492. KEYWORD(monotonic);
  493. KEYWORD(acquire);
  494. KEYWORD(release);
  495. KEYWORD(acq_rel);
  496. KEYWORD(seq_cst);
  497. KEYWORD(syncscope);
  498. KEYWORD(nnan);
  499. KEYWORD(ninf);
  500. KEYWORD(nsz);
  501. KEYWORD(arcp);
  502. KEYWORD(contract);
  503. KEYWORD(reassoc);
  504. KEYWORD(afn);
  505. KEYWORD(fast);
  506. KEYWORD(nuw);
  507. KEYWORD(nsw);
  508. KEYWORD(exact);
  509. KEYWORD(inbounds);
  510. KEYWORD(inrange);
  511. KEYWORD(align);
  512. KEYWORD(addrspace);
  513. KEYWORD(section);
  514. KEYWORD(partition);
  515. KEYWORD(alias);
  516. KEYWORD(ifunc);
  517. KEYWORD(module);
  518. KEYWORD(asm);
  519. KEYWORD(sideeffect);
  520. KEYWORD(alignstack);
  521. KEYWORD(inteldialect);
  522. KEYWORD(gc);
  523. KEYWORD(prefix);
  524. KEYWORD(prologue);
  525. KEYWORD(ccc);
  526. KEYWORD(fastcc);
  527. KEYWORD(coldcc);
  528. KEYWORD(cfguard_checkcc);
  529. KEYWORD(x86_stdcallcc);
  530. KEYWORD(x86_fastcallcc);
  531. KEYWORD(x86_thiscallcc);
  532. KEYWORD(x86_vectorcallcc);
  533. KEYWORD(arm_apcscc);
  534. KEYWORD(arm_aapcscc);
  535. KEYWORD(arm_aapcs_vfpcc);
  536. KEYWORD(aarch64_vector_pcs);
  537. KEYWORD(aarch64_sve_vector_pcs);
  538. KEYWORD(msp430_intrcc);
  539. KEYWORD(avr_intrcc);
  540. KEYWORD(avr_signalcc);
  541. KEYWORD(ptx_kernel);
  542. KEYWORD(ptx_device);
  543. KEYWORD(spir_kernel);
  544. KEYWORD(spir_func);
  545. KEYWORD(intel_ocl_bicc);
  546. KEYWORD(x86_64_sysvcc);
  547. KEYWORD(win64cc);
  548. KEYWORD(x86_regcallcc);
  549. KEYWORD(webkit_jscc);
  550. KEYWORD(swiftcc);
  551. KEYWORD(swifttailcc);
  552. KEYWORD(anyregcc);
  553. KEYWORD(preserve_mostcc);
  554. KEYWORD(preserve_allcc);
  555. KEYWORD(ghccc);
  556. KEYWORD(x86_intrcc);
  557. KEYWORD(hhvmcc);
  558. KEYWORD(hhvm_ccc);
  559. KEYWORD(cxx_fast_tlscc);
  560. KEYWORD(amdgpu_vs);
  561. KEYWORD(amdgpu_ls);
  562. KEYWORD(amdgpu_hs);
  563. KEYWORD(amdgpu_es);
  564. KEYWORD(amdgpu_gs);
  565. KEYWORD(amdgpu_ps);
  566. KEYWORD(amdgpu_cs);
  567. KEYWORD(amdgpu_kernel);
  568. KEYWORD(amdgpu_gfx);
  569. KEYWORD(tailcc);
  570. KEYWORD(cc);
  571. KEYWORD(c);
  572. KEYWORD(attributes);
  573. KEYWORD(alwaysinline);
  574. KEYWORD(allocsize);
  575. KEYWORD(argmemonly);
  576. KEYWORD(builtin);
  577. KEYWORD(byval);
  578. KEYWORD(inalloca);
  579. KEYWORD(cold);
  580. KEYWORD(convergent);
  581. KEYWORD(dereferenceable);
  582. KEYWORD(dereferenceable_or_null);
  583. KEYWORD(disable_sanitizer_instrumentation);
  584. KEYWORD(elementtype);
  585. KEYWORD(inaccessiblememonly);
  586. KEYWORD(inaccessiblemem_or_argmemonly);
  587. KEYWORD(inlinehint);
  588. KEYWORD(inreg);
  589. KEYWORD(jumptable);
  590. KEYWORD(minsize);
  591. KEYWORD(naked);
  592. KEYWORD(nest);
  593. KEYWORD(noalias);
  594. KEYWORD(nobuiltin);
  595. KEYWORD(nocallback);
  596. KEYWORD(nocapture);
  597. KEYWORD(noduplicate);
  598. KEYWORD(nofree);
  599. KEYWORD(noimplicitfloat);
  600. KEYWORD(noinline);
  601. KEYWORD(norecurse);
  602. KEYWORD(nonlazybind);
  603. KEYWORD(nomerge);
  604. KEYWORD(nonnull);
  605. KEYWORD(noprofile);
  606. KEYWORD(noredzone);
  607. KEYWORD(noreturn);
  608. KEYWORD(nosync);
  609. KEYWORD(nocf_check);
  610. KEYWORD(noundef);
  611. KEYWORD(nounwind);
  612. KEYWORD(nosanitize_coverage);
  613. KEYWORD(null_pointer_is_valid);
  614. KEYWORD(optforfuzzing);
  615. KEYWORD(optnone);
  616. KEYWORD(optsize);
  617. KEYWORD(preallocated);
  618. KEYWORD(readnone);
  619. KEYWORD(readonly);
  620. KEYWORD(returned);
  621. KEYWORD(returns_twice);
  622. KEYWORD(signext);
  623. KEYWORD(speculatable);
  624. KEYWORD(sret);
  625. KEYWORD(ssp);
  626. KEYWORD(sspreq);
  627. KEYWORD(sspstrong);
  628. KEYWORD(strictfp);
  629. KEYWORD(safestack);
  630. KEYWORD(shadowcallstack);
  631. KEYWORD(sanitize_address);
  632. KEYWORD(sanitize_hwaddress);
  633. KEYWORD(sanitize_memtag);
  634. KEYWORD(sanitize_thread);
  635. KEYWORD(sanitize_memory);
  636. KEYWORD(speculative_load_hardening);
  637. KEYWORD(swifterror);
  638. KEYWORD(swiftself);
  639. KEYWORD(swiftasync);
  640. KEYWORD(uwtable);
  641. KEYWORD(vscale_range);
  642. KEYWORD(willreturn);
  643. KEYWORD(writeonly);
  644. KEYWORD(zeroext);
  645. KEYWORD(immarg);
  646. KEYWORD(byref);
  647. KEYWORD(mustprogress);
  648. KEYWORD(type);
  649. KEYWORD(opaque);
  650. KEYWORD(comdat);
  651. // Comdat types
  652. KEYWORD(any);
  653. KEYWORD(exactmatch);
  654. KEYWORD(largest);
  655. KEYWORD(nodeduplicate);
  656. KEYWORD(samesize);
  657. KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
  658. KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
  659. KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
  660. KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
  661. KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
  662. KEYWORD(umin);
  663. KEYWORD(vscale);
  664. KEYWORD(x);
  665. KEYWORD(blockaddress);
  666. KEYWORD(dso_local_equivalent);
  667. KEYWORD(no_cfi);
  668. // Metadata types.
  669. KEYWORD(distinct);
  670. // Use-list order directives.
  671. KEYWORD(uselistorder);
  672. KEYWORD(uselistorder_bb);
  673. KEYWORD(personality);
  674. KEYWORD(cleanup);
  675. KEYWORD(catch);
  676. KEYWORD(filter);
  677. // Summary index keywords.
  678. KEYWORD(path);
  679. KEYWORD(hash);
  680. KEYWORD(gv);
  681. KEYWORD(guid);
  682. KEYWORD(name);
  683. KEYWORD(summaries);
  684. KEYWORD(flags);
  685. KEYWORD(blockcount);
  686. KEYWORD(linkage);
  687. KEYWORD(visibility);
  688. KEYWORD(notEligibleToImport);
  689. KEYWORD(live);
  690. KEYWORD(dsoLocal);
  691. KEYWORD(canAutoHide);
  692. KEYWORD(function);
  693. KEYWORD(insts);
  694. KEYWORD(funcFlags);
  695. KEYWORD(readNone);
  696. KEYWORD(readOnly);
  697. KEYWORD(noRecurse);
  698. KEYWORD(returnDoesNotAlias);
  699. KEYWORD(noInline);
  700. KEYWORD(alwaysInline);
  701. KEYWORD(noUnwind);
  702. KEYWORD(mayThrow);
  703. KEYWORD(hasUnknownCall);
  704. KEYWORD(mustBeUnreachable);
  705. KEYWORD(calls);
  706. KEYWORD(callee);
  707. KEYWORD(params);
  708. KEYWORD(param);
  709. KEYWORD(hotness);
  710. KEYWORD(unknown);
  711. KEYWORD(hot);
  712. KEYWORD(critical);
  713. KEYWORD(relbf);
  714. KEYWORD(variable);
  715. KEYWORD(vTableFuncs);
  716. KEYWORD(virtFunc);
  717. KEYWORD(aliasee);
  718. KEYWORD(refs);
  719. KEYWORD(typeIdInfo);
  720. KEYWORD(typeTests);
  721. KEYWORD(typeTestAssumeVCalls);
  722. KEYWORD(typeCheckedLoadVCalls);
  723. KEYWORD(typeTestAssumeConstVCalls);
  724. KEYWORD(typeCheckedLoadConstVCalls);
  725. KEYWORD(vFuncId);
  726. KEYWORD(offset);
  727. KEYWORD(args);
  728. KEYWORD(typeid);
  729. KEYWORD(typeidCompatibleVTable);
  730. KEYWORD(summary);
  731. KEYWORD(typeTestRes);
  732. KEYWORD(kind);
  733. KEYWORD(unsat);
  734. KEYWORD(byteArray);
  735. KEYWORD(inline);
  736. KEYWORD(single);
  737. KEYWORD(allOnes);
  738. KEYWORD(sizeM1BitWidth);
  739. KEYWORD(alignLog2);
  740. KEYWORD(sizeM1);
  741. KEYWORD(bitMask);
  742. KEYWORD(inlineBits);
  743. KEYWORD(vcall_visibility);
  744. KEYWORD(wpdResolutions);
  745. KEYWORD(wpdRes);
  746. KEYWORD(indir);
  747. KEYWORD(singleImpl);
  748. KEYWORD(branchFunnel);
  749. KEYWORD(singleImplName);
  750. KEYWORD(resByArg);
  751. KEYWORD(byArg);
  752. KEYWORD(uniformRetVal);
  753. KEYWORD(uniqueRetVal);
  754. KEYWORD(virtualConstProp);
  755. KEYWORD(info);
  756. KEYWORD(byte);
  757. KEYWORD(bit);
  758. KEYWORD(varFlags);
  759. #undef KEYWORD
  760. // Keywords for types.
  761. #define TYPEKEYWORD(STR, LLVMTY) \
  762. do { \
  763. if (Keyword == STR) { \
  764. TyVal = LLVMTY; \
  765. return lltok::Type; \
  766. } \
  767. } while (false)
  768. TYPEKEYWORD("void", Type::getVoidTy(Context));
  769. TYPEKEYWORD("half", Type::getHalfTy(Context));
  770. TYPEKEYWORD("bfloat", Type::getBFloatTy(Context));
  771. TYPEKEYWORD("float", Type::getFloatTy(Context));
  772. TYPEKEYWORD("double", Type::getDoubleTy(Context));
  773. TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
  774. TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
  775. TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
  776. TYPEKEYWORD("label", Type::getLabelTy(Context));
  777. TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
  778. TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
  779. TYPEKEYWORD("x86_amx", Type::getX86_AMXTy(Context));
  780. TYPEKEYWORD("token", Type::getTokenTy(Context));
  781. if (Keyword == "ptr") {
  782. if (Context.supportsTypedPointers()) {
  783. Warning("ptr type is only supported in -opaque-pointers mode");
  784. return lltok::Error;
  785. }
  786. TyVal = PointerType::getUnqual(Context);
  787. return lltok::Type;
  788. }
  789. #undef TYPEKEYWORD
  790. // Keywords for instructions.
  791. #define INSTKEYWORD(STR, Enum) \
  792. do { \
  793. if (Keyword == #STR) { \
  794. UIntVal = Instruction::Enum; \
  795. return lltok::kw_##STR; \
  796. } \
  797. } while (false)
  798. INSTKEYWORD(fneg, FNeg);
  799. INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
  800. INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
  801. INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
  802. INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
  803. INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
  804. INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
  805. INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
  806. INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
  807. INSTKEYWORD(phi, PHI);
  808. INSTKEYWORD(call, Call);
  809. INSTKEYWORD(trunc, Trunc);
  810. INSTKEYWORD(zext, ZExt);
  811. INSTKEYWORD(sext, SExt);
  812. INSTKEYWORD(fptrunc, FPTrunc);
  813. INSTKEYWORD(fpext, FPExt);
  814. INSTKEYWORD(uitofp, UIToFP);
  815. INSTKEYWORD(sitofp, SIToFP);
  816. INSTKEYWORD(fptoui, FPToUI);
  817. INSTKEYWORD(fptosi, FPToSI);
  818. INSTKEYWORD(inttoptr, IntToPtr);
  819. INSTKEYWORD(ptrtoint, PtrToInt);
  820. INSTKEYWORD(bitcast, BitCast);
  821. INSTKEYWORD(addrspacecast, AddrSpaceCast);
  822. INSTKEYWORD(select, Select);
  823. INSTKEYWORD(va_arg, VAArg);
  824. INSTKEYWORD(ret, Ret);
  825. INSTKEYWORD(br, Br);
  826. INSTKEYWORD(switch, Switch);
  827. INSTKEYWORD(indirectbr, IndirectBr);
  828. INSTKEYWORD(invoke, Invoke);
  829. INSTKEYWORD(resume, Resume);
  830. INSTKEYWORD(unreachable, Unreachable);
  831. INSTKEYWORD(callbr, CallBr);
  832. INSTKEYWORD(alloca, Alloca);
  833. INSTKEYWORD(load, Load);
  834. INSTKEYWORD(store, Store);
  835. INSTKEYWORD(cmpxchg, AtomicCmpXchg);
  836. INSTKEYWORD(atomicrmw, AtomicRMW);
  837. INSTKEYWORD(fence, Fence);
  838. INSTKEYWORD(getelementptr, GetElementPtr);
  839. INSTKEYWORD(extractelement, ExtractElement);
  840. INSTKEYWORD(insertelement, InsertElement);
  841. INSTKEYWORD(shufflevector, ShuffleVector);
  842. INSTKEYWORD(extractvalue, ExtractValue);
  843. INSTKEYWORD(insertvalue, InsertValue);
  844. INSTKEYWORD(landingpad, LandingPad);
  845. INSTKEYWORD(cleanupret, CleanupRet);
  846. INSTKEYWORD(catchret, CatchRet);
  847. INSTKEYWORD(catchswitch, CatchSwitch);
  848. INSTKEYWORD(catchpad, CatchPad);
  849. INSTKEYWORD(cleanuppad, CleanupPad);
  850. INSTKEYWORD(freeze, Freeze);
  851. #undef INSTKEYWORD
  852. #define DWKEYWORD(TYPE, TOKEN) \
  853. do { \
  854. if (Keyword.startswith("DW_" #TYPE "_")) { \
  855. StrVal.assign(Keyword.begin(), Keyword.end()); \
  856. return lltok::TOKEN; \
  857. } \
  858. } while (false)
  859. DWKEYWORD(TAG, DwarfTag);
  860. DWKEYWORD(ATE, DwarfAttEncoding);
  861. DWKEYWORD(VIRTUALITY, DwarfVirtuality);
  862. DWKEYWORD(LANG, DwarfLang);
  863. DWKEYWORD(CC, DwarfCC);
  864. DWKEYWORD(OP, DwarfOp);
  865. DWKEYWORD(MACINFO, DwarfMacinfo);
  866. #undef DWKEYWORD
  867. if (Keyword.startswith("DIFlag")) {
  868. StrVal.assign(Keyword.begin(), Keyword.end());
  869. return lltok::DIFlag;
  870. }
  871. if (Keyword.startswith("DISPFlag")) {
  872. StrVal.assign(Keyword.begin(), Keyword.end());
  873. return lltok::DISPFlag;
  874. }
  875. if (Keyword.startswith("CSK_")) {
  876. StrVal.assign(Keyword.begin(), Keyword.end());
  877. return lltok::ChecksumKind;
  878. }
  879. if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
  880. Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") {
  881. StrVal.assign(Keyword.begin(), Keyword.end());
  882. return lltok::EmissionKind;
  883. }
  884. if (Keyword == "GNU" || Keyword == "None" || Keyword == "Default") {
  885. StrVal.assign(Keyword.begin(), Keyword.end());
  886. return lltok::NameTableKind;
  887. }
  888. // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
  889. // the CFE to avoid forcing it to deal with 64-bit numbers.
  890. if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
  891. TokStart[1] == '0' && TokStart[2] == 'x' &&
  892. isxdigit(static_cast<unsigned char>(TokStart[3]))) {
  893. int len = CurPtr-TokStart-3;
  894. uint32_t bits = len * 4;
  895. StringRef HexStr(TokStart + 3, len);
  896. if (!all_of(HexStr, isxdigit)) {
  897. // Bad token, return it as an error.
  898. CurPtr = TokStart+3;
  899. return lltok::Error;
  900. }
  901. APInt Tmp(bits, HexStr, 16);
  902. uint32_t activeBits = Tmp.getActiveBits();
  903. if (activeBits > 0 && activeBits < bits)
  904. Tmp = Tmp.trunc(activeBits);
  905. APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
  906. return lltok::APSInt;
  907. }
  908. // If this is "cc1234", return this as just "cc".
  909. if (TokStart[0] == 'c' && TokStart[1] == 'c') {
  910. CurPtr = TokStart+2;
  911. return lltok::kw_cc;
  912. }
  913. // Finally, if this isn't known, return an error.
  914. CurPtr = TokStart+1;
  915. return lltok::Error;
  916. }
  917. /// Lex all tokens that start with a 0x prefix, knowing they match and are not
  918. /// labels.
  919. /// HexFPConstant 0x[0-9A-Fa-f]+
  920. /// HexFP80Constant 0xK[0-9A-Fa-f]+
  921. /// HexFP128Constant 0xL[0-9A-Fa-f]+
  922. /// HexPPC128Constant 0xM[0-9A-Fa-f]+
  923. /// HexHalfConstant 0xH[0-9A-Fa-f]+
  924. /// HexBFloatConstant 0xR[0-9A-Fa-f]+
  925. lltok::Kind LLLexer::Lex0x() {
  926. CurPtr = TokStart + 2;
  927. char Kind;
  928. if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H' ||
  929. CurPtr[0] == 'R') {
  930. Kind = *CurPtr++;
  931. } else {
  932. Kind = 'J';
  933. }
  934. if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
  935. // Bad token, return it as an error.
  936. CurPtr = TokStart+1;
  937. return lltok::Error;
  938. }
  939. while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
  940. ++CurPtr;
  941. if (Kind == 'J') {
  942. // HexFPConstant - Floating point constant represented in IEEE format as a
  943. // hexadecimal number for when exponential notation is not precise enough.
  944. // Half, BFloat, Float, and double only.
  945. APFloatVal = APFloat(APFloat::IEEEdouble(),
  946. APInt(64, HexIntToVal(TokStart + 2, CurPtr)));
  947. return lltok::APFloat;
  948. }
  949. uint64_t Pair[2];
  950. switch (Kind) {
  951. default: llvm_unreachable("Unknown kind!");
  952. case 'K':
  953. // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
  954. FP80HexToIntPair(TokStart+3, CurPtr, Pair);
  955. APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
  956. return lltok::APFloat;
  957. case 'L':
  958. // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
  959. HexToIntPair(TokStart+3, CurPtr, Pair);
  960. APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
  961. return lltok::APFloat;
  962. case 'M':
  963. // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
  964. HexToIntPair(TokStart+3, CurPtr, Pair);
  965. APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
  966. return lltok::APFloat;
  967. case 'H':
  968. APFloatVal = APFloat(APFloat::IEEEhalf(),
  969. APInt(16,HexIntToVal(TokStart+3, CurPtr)));
  970. return lltok::APFloat;
  971. case 'R':
  972. // Brain floating point
  973. APFloatVal = APFloat(APFloat::BFloat(),
  974. APInt(16, HexIntToVal(TokStart + 3, CurPtr)));
  975. return lltok::APFloat;
  976. }
  977. }
  978. /// Lex tokens for a label or a numeric constant, possibly starting with -.
  979. /// Label [-a-zA-Z$._0-9]+:
  980. /// NInteger -[0-9]+
  981. /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
  982. /// PInteger [0-9]+
  983. /// HexFPConstant 0x[0-9A-Fa-f]+
  984. /// HexFP80Constant 0xK[0-9A-Fa-f]+
  985. /// HexFP128Constant 0xL[0-9A-Fa-f]+
  986. /// HexPPC128Constant 0xM[0-9A-Fa-f]+
  987. lltok::Kind LLLexer::LexDigitOrNegative() {
  988. // If the letter after the negative is not a number, this is probably a label.
  989. if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
  990. !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
  991. // Okay, this is not a number after the -, it's probably a label.
  992. if (const char *End = isLabelTail(CurPtr)) {
  993. StrVal.assign(TokStart, End-1);
  994. CurPtr = End;
  995. return lltok::LabelStr;
  996. }
  997. return lltok::Error;
  998. }
  999. // At this point, it is either a label, int or fp constant.
  1000. // Skip digits, we have at least one.
  1001. for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  1002. /*empty*/;
  1003. // Check if this is a fully-numeric label:
  1004. if (isdigit(TokStart[0]) && CurPtr[0] == ':') {
  1005. uint64_t Val = atoull(TokStart, CurPtr);
  1006. ++CurPtr; // Skip the colon.
  1007. if ((unsigned)Val != Val)
  1008. Error("invalid value number (too large)!");
  1009. UIntVal = unsigned(Val);
  1010. return lltok::LabelID;
  1011. }
  1012. // Check to see if this really is a string label, e.g. "-1:".
  1013. if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
  1014. if (const char *End = isLabelTail(CurPtr)) {
  1015. StrVal.assign(TokStart, End-1);
  1016. CurPtr = End;
  1017. return lltok::LabelStr;
  1018. }
  1019. }
  1020. // If the next character is a '.', then it is a fp value, otherwise its
  1021. // integer.
  1022. if (CurPtr[0] != '.') {
  1023. if (TokStart[0] == '0' && TokStart[1] == 'x')
  1024. return Lex0x();
  1025. APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
  1026. return lltok::APSInt;
  1027. }
  1028. ++CurPtr;
  1029. // Skip over [0-9]*([eE][-+]?[0-9]+)?
  1030. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  1031. if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
  1032. if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
  1033. ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
  1034. isdigit(static_cast<unsigned char>(CurPtr[2])))) {
  1035. CurPtr += 2;
  1036. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  1037. }
  1038. }
  1039. APFloatVal = APFloat(APFloat::IEEEdouble(),
  1040. StringRef(TokStart, CurPtr - TokStart));
  1041. return lltok::APFloat;
  1042. }
  1043. /// Lex a floating point constant starting with +.
  1044. /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
  1045. lltok::Kind LLLexer::LexPositive() {
  1046. // If the letter after the negative is a number, this is probably not a
  1047. // label.
  1048. if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
  1049. return lltok::Error;
  1050. // Skip digits.
  1051. for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  1052. /*empty*/;
  1053. // At this point, we need a '.'.
  1054. if (CurPtr[0] != '.') {
  1055. CurPtr = TokStart+1;
  1056. return lltok::Error;
  1057. }
  1058. ++CurPtr;
  1059. // Skip over [0-9]*([eE][-+]?[0-9]+)?
  1060. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  1061. if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
  1062. if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
  1063. ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
  1064. isdigit(static_cast<unsigned char>(CurPtr[2])))) {
  1065. CurPtr += 2;
  1066. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  1067. }
  1068. }
  1069. APFloatVal = APFloat(APFloat::IEEEdouble(),
  1070. StringRef(TokStart, CurPtr - TokStart));
  1071. return lltok::APFloat;
  1072. }