LLLexer.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Implement the Lexer for .ll files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "LLLexer.h"
  14. #include "llvm/ADT/APInt.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/IR/DerivedTypes.h"
  19. #include "llvm/IR/Instruction.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/SourceMgr.h"
  22. #include <cassert>
  23. #include <cctype>
  24. #include <cstdio>
  25. using namespace llvm;
  26. bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
  27. ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
  28. return true;
  29. }
  30. void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
  31. SM.PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
  32. }
  33. //===----------------------------------------------------------------------===//
  34. // Helper functions.
  35. //===----------------------------------------------------------------------===//
  36. // atoull - Convert an ascii string of decimal digits into the unsigned long
  37. // long representation... this does not have to do input error checking,
  38. // because we know that the input will be matched by a suitable regex...
  39. //
  40. uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
  41. uint64_t Result = 0;
  42. for (; Buffer != End; Buffer++) {
  43. uint64_t OldRes = Result;
  44. Result *= 10;
  45. Result += *Buffer-'0';
  46. if (Result < OldRes) { // Uh, oh, overflow detected!!!
  47. Error("constant bigger than 64 bits detected!");
  48. return 0;
  49. }
  50. }
  51. return Result;
  52. }
  53. uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
  54. uint64_t Result = 0;
  55. for (; Buffer != End; ++Buffer) {
  56. uint64_t OldRes = Result;
  57. Result *= 16;
  58. Result += hexDigitValue(*Buffer);
  59. if (Result < OldRes) { // Uh, oh, overflow detected!!!
  60. Error("constant bigger than 64 bits detected!");
  61. return 0;
  62. }
  63. }
  64. return Result;
  65. }
  66. void LLLexer::HexToIntPair(const char *Buffer, const char *End,
  67. uint64_t Pair[2]) {
  68. Pair[0] = 0;
  69. if (End - Buffer >= 16) {
  70. for (int i = 0; i < 16; i++, Buffer++) {
  71. assert(Buffer != End);
  72. Pair[0] *= 16;
  73. Pair[0] += hexDigitValue(*Buffer);
  74. }
  75. }
  76. Pair[1] = 0;
  77. for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
  78. Pair[1] *= 16;
  79. Pair[1] += hexDigitValue(*Buffer);
  80. }
  81. if (Buffer != End)
  82. Error("constant bigger than 128 bits detected!");
  83. }
  84. /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
  85. /// { low64, high16 } as usual for an APInt.
  86. void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
  87. uint64_t Pair[2]) {
  88. Pair[1] = 0;
  89. for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
  90. assert(Buffer != End);
  91. Pair[1] *= 16;
  92. Pair[1] += hexDigitValue(*Buffer);
  93. }
  94. Pair[0] = 0;
  95. for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
  96. Pair[0] *= 16;
  97. Pair[0] += hexDigitValue(*Buffer);
  98. }
  99. if (Buffer != End)
  100. Error("constant bigger than 128 bits detected!");
  101. }
  102. // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
  103. // appropriate character.
  104. static void UnEscapeLexed(std::string &Str) {
  105. if (Str.empty()) return;
  106. char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
  107. char *BOut = Buffer;
  108. for (char *BIn = Buffer; BIn != EndBuffer; ) {
  109. if (BIn[0] == '\\') {
  110. if (BIn < EndBuffer-1 && BIn[1] == '\\') {
  111. *BOut++ = '\\'; // Two \ becomes one
  112. BIn += 2;
  113. } else if (BIn < EndBuffer-2 &&
  114. isxdigit(static_cast<unsigned char>(BIn[1])) &&
  115. isxdigit(static_cast<unsigned char>(BIn[2]))) {
  116. *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
  117. BIn += 3; // Skip over handled chars
  118. ++BOut;
  119. } else {
  120. *BOut++ = *BIn++;
  121. }
  122. } else {
  123. *BOut++ = *BIn++;
  124. }
  125. }
  126. Str.resize(BOut-Buffer);
  127. }
  128. /// isLabelChar - Return true for [-a-zA-Z$._0-9].
  129. static bool isLabelChar(char C) {
  130. return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
  131. C == '.' || C == '_';
  132. }
  133. /// isLabelTail - Return true if this pointer points to a valid end of a label.
  134. static const char *isLabelTail(const char *CurPtr) {
  135. while (true) {
  136. if (CurPtr[0] == ':') return CurPtr+1;
  137. if (!isLabelChar(CurPtr[0])) return nullptr;
  138. ++CurPtr;
  139. }
  140. }
  141. //===----------------------------------------------------------------------===//
  142. // Lexer definition.
  143. //===----------------------------------------------------------------------===//
  144. LLLexer::LLLexer(StringRef StartBuf, SourceMgr &sm, SMDiagnostic &Err,
  145. LLVMContext &C)
  146. : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
  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 '#': return LexHash();
  202. case '0': case '1': case '2': case '3': case '4':
  203. case '5': case '6': case '7': case '8': case '9':
  204. case '-':
  205. return LexDigitOrNegative();
  206. case '=': return lltok::equal;
  207. case '[': return lltok::lsquare;
  208. case ']': return lltok::rsquare;
  209. case '{': return lltok::lbrace;
  210. case '}': return lltok::rbrace;
  211. case '<': return lltok::less;
  212. case '>': return lltok::greater;
  213. case '(': return lltok::lparen;
  214. case ')': return lltok::rparen;
  215. case ',': return lltok::comma;
  216. case '*': return lltok::star;
  217. case '|': return lltok::bar;
  218. }
  219. }
  220. }
  221. void LLLexer::SkipLineComment() {
  222. while (true) {
  223. if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
  224. return;
  225. }
  226. }
  227. /// Lex all tokens that start with an @ character.
  228. /// GlobalVar @\"[^\"]*\"
  229. /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
  230. /// GlobalVarID @[0-9]+
  231. lltok::Kind LLLexer::LexAt() {
  232. return LexVar(lltok::GlobalVar, lltok::GlobalID);
  233. }
  234. lltok::Kind LLLexer::LexDollar() {
  235. if (const char *Ptr = isLabelTail(TokStart)) {
  236. CurPtr = Ptr;
  237. StrVal.assign(TokStart, CurPtr - 1);
  238. return lltok::LabelStr;
  239. }
  240. // Handle DollarStringConstant: $\"[^\"]*\"
  241. if (CurPtr[0] == '"') {
  242. ++CurPtr;
  243. while (true) {
  244. int CurChar = getNextChar();
  245. if (CurChar == EOF) {
  246. Error("end of file in COMDAT variable name");
  247. return lltok::Error;
  248. }
  249. if (CurChar == '"') {
  250. StrVal.assign(TokStart + 2, CurPtr - 1);
  251. UnEscapeLexed(StrVal);
  252. if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
  253. Error("Null bytes are not allowed in names");
  254. return lltok::Error;
  255. }
  256. return lltok::ComdatVar;
  257. }
  258. }
  259. }
  260. // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
  261. if (ReadVarName())
  262. return lltok::ComdatVar;
  263. return lltok::Error;
  264. }
  265. /// ReadString - Read a string until the closing quote.
  266. lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
  267. const char *Start = CurPtr;
  268. while (true) {
  269. int CurChar = getNextChar();
  270. if (CurChar == EOF) {
  271. Error("end of file in string constant");
  272. return lltok::Error;
  273. }
  274. if (CurChar == '"') {
  275. StrVal.assign(Start, CurPtr-1);
  276. UnEscapeLexed(StrVal);
  277. return kind;
  278. }
  279. }
  280. }
  281. /// ReadVarName - Read the rest of a token containing a variable name.
  282. bool LLLexer::ReadVarName() {
  283. const char *NameStart = CurPtr;
  284. if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
  285. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  286. CurPtr[0] == '.' || CurPtr[0] == '_') {
  287. ++CurPtr;
  288. while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
  289. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  290. CurPtr[0] == '.' || CurPtr[0] == '_')
  291. ++CurPtr;
  292. StrVal.assign(NameStart, CurPtr);
  293. return true;
  294. }
  295. return false;
  296. }
  297. lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
  298. // Handle StringConstant: \"[^\"]*\"
  299. if (CurPtr[0] == '"') {
  300. ++CurPtr;
  301. while (true) {
  302. int CurChar = getNextChar();
  303. if (CurChar == EOF) {
  304. Error("end of file in global variable name");
  305. return lltok::Error;
  306. }
  307. if (CurChar == '"') {
  308. StrVal.assign(TokStart+2, CurPtr-1);
  309. UnEscapeLexed(StrVal);
  310. if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
  311. Error("Null bytes are not allowed in names");
  312. return lltok::Error;
  313. }
  314. return Var;
  315. }
  316. }
  317. }
  318. // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
  319. if (ReadVarName())
  320. return Var;
  321. // Handle VarID: [0-9]+
  322. if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
  323. for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  324. /*empty*/;
  325. uint64_t Val = atoull(TokStart+1, CurPtr);
  326. if ((unsigned)Val != Val)
  327. Error("invalid value number (too large)!");
  328. UIntVal = unsigned(Val);
  329. return VarID;
  330. }
  331. return lltok::Error;
  332. }
  333. /// Lex all tokens that start with a % character.
  334. /// LocalVar ::= %\"[^\"]*\"
  335. /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
  336. /// LocalVarID ::= %[0-9]+
  337. lltok::Kind LLLexer::LexPercent() {
  338. return LexVar(lltok::LocalVar, lltok::LocalVarID);
  339. }
  340. /// Lex all tokens that start with a " character.
  341. /// QuoteLabel "[^"]+":
  342. /// StringConstant "[^"]*"
  343. lltok::Kind LLLexer::LexQuote() {
  344. lltok::Kind kind = ReadString(lltok::StringConstant);
  345. if (kind == lltok::Error || kind == lltok::Eof)
  346. return kind;
  347. if (CurPtr[0] == ':') {
  348. ++CurPtr;
  349. if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
  350. Error("Null bytes are not allowed in names");
  351. kind = lltok::Error;
  352. } else {
  353. kind = lltok::LabelStr;
  354. }
  355. }
  356. return kind;
  357. }
  358. /// Lex all tokens that start with a ! character.
  359. /// !foo
  360. /// !
  361. lltok::Kind LLLexer::LexExclaim() {
  362. // Lex a metadata name as a MetadataVar.
  363. if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
  364. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  365. CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
  366. ++CurPtr;
  367. while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
  368. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  369. CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
  370. ++CurPtr;
  371. StrVal.assign(TokStart+1, CurPtr); // Skip !
  372. UnEscapeLexed(StrVal);
  373. return lltok::MetadataVar;
  374. }
  375. return lltok::exclaim;
  376. }
  377. /// Lex all tokens that start with a # character.
  378. /// AttrGrpID ::= #[0-9]+
  379. lltok::Kind LLLexer::LexHash() {
  380. // Handle AttrGrpID: #[0-9]+
  381. if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
  382. for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  383. /*empty*/;
  384. uint64_t Val = atoull(TokStart+1, CurPtr);
  385. if ((unsigned)Val != Val)
  386. Error("invalid value number (too large)!");
  387. UIntVal = unsigned(Val);
  388. return lltok::AttrGrpID;
  389. }
  390. return lltok::Error;
  391. }
  392. /// Lex a label, integer type, keyword, or hexadecimal integer constant.
  393. /// Label [-a-zA-Z$._0-9]+:
  394. /// IntegerType i[0-9]+
  395. /// Keyword sdiv, float, ...
  396. /// HexIntConstant [us]0x[0-9A-Fa-f]+
  397. lltok::Kind LLLexer::LexIdentifier() {
  398. const char *StartChar = CurPtr;
  399. const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
  400. const char *KeywordEnd = nullptr;
  401. for (; isLabelChar(*CurPtr); ++CurPtr) {
  402. // If we decide this is an integer, remember the end of the sequence.
  403. if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
  404. IntEnd = CurPtr;
  405. if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
  406. *CurPtr != '_')
  407. KeywordEnd = CurPtr;
  408. }
  409. // If we stopped due to a colon, this really is a label.
  410. if (*CurPtr == ':') {
  411. StrVal.assign(StartChar-1, CurPtr++);
  412. return lltok::LabelStr;
  413. }
  414. // Otherwise, this wasn't a label. If this was valid as an integer type,
  415. // return it.
  416. if (!IntEnd) IntEnd = CurPtr;
  417. if (IntEnd != StartChar) {
  418. CurPtr = IntEnd;
  419. uint64_t NumBits = atoull(StartChar, CurPtr);
  420. if (NumBits < IntegerType::MIN_INT_BITS ||
  421. NumBits > IntegerType::MAX_INT_BITS) {
  422. Error("bitwidth for integer type out of range!");
  423. return lltok::Error;
  424. }
  425. TyVal = IntegerType::get(Context, NumBits);
  426. return lltok::Type;
  427. }
  428. // Otherwise, this was a letter sequence. See which keyword this is.
  429. if (!KeywordEnd) KeywordEnd = CurPtr;
  430. CurPtr = KeywordEnd;
  431. --StartChar;
  432. StringRef Keyword(StartChar, CurPtr - StartChar);
  433. #define KEYWORD(STR) \
  434. do { \
  435. if (Keyword == #STR) \
  436. return lltok::kw_##STR; \
  437. } while (false)
  438. KEYWORD(true); KEYWORD(false);
  439. KEYWORD(declare); KEYWORD(define);
  440. KEYWORD(global); KEYWORD(constant);
  441. KEYWORD(private);
  442. KEYWORD(internal);
  443. KEYWORD(available_externally);
  444. KEYWORD(linkonce);
  445. KEYWORD(linkonce_odr);
  446. KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
  447. KEYWORD(weak_odr);
  448. KEYWORD(appending);
  449. KEYWORD(dllimport);
  450. KEYWORD(dllexport);
  451. KEYWORD(common);
  452. KEYWORD(default);
  453. KEYWORD(hidden);
  454. KEYWORD(protected);
  455. KEYWORD(unnamed_addr);
  456. KEYWORD(local_unnamed_addr);
  457. KEYWORD(externally_initialized);
  458. KEYWORD(extern_weak);
  459. KEYWORD(external);
  460. KEYWORD(thread_local);
  461. KEYWORD(localdynamic);
  462. KEYWORD(initialexec);
  463. KEYWORD(localexec);
  464. KEYWORD(zeroinitializer);
  465. KEYWORD(undef);
  466. KEYWORD(null);
  467. KEYWORD(none);
  468. KEYWORD(to);
  469. KEYWORD(caller);
  470. KEYWORD(within);
  471. KEYWORD(from);
  472. KEYWORD(tail);
  473. KEYWORD(musttail);
  474. KEYWORD(notail);
  475. KEYWORD(target);
  476. KEYWORD(triple);
  477. KEYWORD(source_filename);
  478. KEYWORD(unwind);
  479. KEYWORD(deplibs); // FIXME: Remove in 4.0.
  480. KEYWORD(datalayout);
  481. KEYWORD(volatile);
  482. KEYWORD(atomic);
  483. KEYWORD(unordered);
  484. KEYWORD(monotonic);
  485. KEYWORD(acquire);
  486. KEYWORD(release);
  487. KEYWORD(acq_rel);
  488. KEYWORD(seq_cst);
  489. KEYWORD(singlethread);
  490. KEYWORD(nnan);
  491. KEYWORD(ninf);
  492. KEYWORD(nsz);
  493. KEYWORD(arcp);
  494. KEYWORD(contract);
  495. KEYWORD(fast);
  496. KEYWORD(nuw);
  497. KEYWORD(nsw);
  498. KEYWORD(exact);
  499. KEYWORD(inbounds);
  500. KEYWORD(inrange);
  501. KEYWORD(align);
  502. KEYWORD(addrspace);
  503. KEYWORD(section);
  504. KEYWORD(alias);
  505. KEYWORD(ifunc);
  506. KEYWORD(module);
  507. KEYWORD(asm);
  508. KEYWORD(sideeffect);
  509. KEYWORD(alignstack);
  510. KEYWORD(inteldialect);
  511. KEYWORD(gc);
  512. KEYWORD(prefix);
  513. KEYWORD(prologue);
  514. KEYWORD(ccc);
  515. KEYWORD(fastcc);
  516. KEYWORD(coldcc);
  517. KEYWORD(x86_stdcallcc);
  518. KEYWORD(x86_fastcallcc);
  519. KEYWORD(x86_thiscallcc);
  520. KEYWORD(x86_vectorcallcc);
  521. KEYWORD(arm_apcscc);
  522. KEYWORD(arm_aapcscc);
  523. KEYWORD(arm_aapcs_vfpcc);
  524. KEYWORD(msp430_intrcc);
  525. KEYWORD(avr_intrcc);
  526. KEYWORD(avr_signalcc);
  527. KEYWORD(ptx_kernel);
  528. KEYWORD(ptx_device);
  529. KEYWORD(spir_kernel);
  530. KEYWORD(spir_func);
  531. KEYWORD(intel_ocl_bicc);
  532. KEYWORD(x86_64_sysvcc);
  533. KEYWORD(x86_64_win64cc);
  534. KEYWORD(x86_regcallcc);
  535. KEYWORD(webkit_jscc);
  536. KEYWORD(swiftcc);
  537. KEYWORD(anyregcc);
  538. KEYWORD(preserve_mostcc);
  539. KEYWORD(preserve_allcc);
  540. KEYWORD(ghccc);
  541. KEYWORD(x86_intrcc);
  542. KEYWORD(hhvmcc);
  543. KEYWORD(hhvm_ccc);
  544. KEYWORD(cxx_fast_tlscc);
  545. KEYWORD(amdgpu_vs);
  546. KEYWORD(amdgpu_gs);
  547. KEYWORD(amdgpu_ps);
  548. KEYWORD(amdgpu_cs);
  549. KEYWORD(amdgpu_kernel);
  550. KEYWORD(cc);
  551. KEYWORD(c);
  552. KEYWORD(attributes);
  553. KEYWORD(alwaysinline);
  554. KEYWORD(allocsize);
  555. KEYWORD(argmemonly);
  556. KEYWORD(builtin);
  557. KEYWORD(byval);
  558. KEYWORD(inalloca);
  559. KEYWORD(cold);
  560. KEYWORD(convergent);
  561. KEYWORD(dereferenceable);
  562. KEYWORD(dereferenceable_or_null);
  563. KEYWORD(inaccessiblememonly);
  564. KEYWORD(inaccessiblemem_or_argmemonly);
  565. KEYWORD(inlinehint);
  566. KEYWORD(inreg);
  567. KEYWORD(jumptable);
  568. KEYWORD(minsize);
  569. KEYWORD(naked);
  570. KEYWORD(nest);
  571. KEYWORD(noalias);
  572. KEYWORD(nobuiltin);
  573. KEYWORD(nocapture);
  574. KEYWORD(noduplicate);
  575. KEYWORD(noimplicitfloat);
  576. KEYWORD(noinline);
  577. KEYWORD(norecurse);
  578. KEYWORD(nonlazybind);
  579. KEYWORD(nonnull);
  580. KEYWORD(noredzone);
  581. KEYWORD(noreturn);
  582. KEYWORD(nounwind);
  583. KEYWORD(optnone);
  584. KEYWORD(optsize);
  585. KEYWORD(readnone);
  586. KEYWORD(readonly);
  587. KEYWORD(returned);
  588. KEYWORD(returns_twice);
  589. KEYWORD(signext);
  590. KEYWORD(sret);
  591. KEYWORD(ssp);
  592. KEYWORD(sspreq);
  593. KEYWORD(sspstrong);
  594. KEYWORD(safestack);
  595. KEYWORD(sanitize_address);
  596. KEYWORD(sanitize_thread);
  597. KEYWORD(sanitize_memory);
  598. KEYWORD(swifterror);
  599. KEYWORD(swiftself);
  600. KEYWORD(uwtable);
  601. KEYWORD(writeonly);
  602. KEYWORD(zeroext);
  603. KEYWORD(type);
  604. KEYWORD(opaque);
  605. KEYWORD(comdat);
  606. // Comdat types
  607. KEYWORD(any);
  608. KEYWORD(exactmatch);
  609. KEYWORD(largest);
  610. KEYWORD(noduplicates);
  611. KEYWORD(samesize);
  612. KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
  613. KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
  614. KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
  615. KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
  616. KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
  617. KEYWORD(umin);
  618. KEYWORD(x);
  619. KEYWORD(blockaddress);
  620. // Metadata types.
  621. KEYWORD(distinct);
  622. // Use-list order directives.
  623. KEYWORD(uselistorder);
  624. KEYWORD(uselistorder_bb);
  625. KEYWORD(personality);
  626. KEYWORD(cleanup);
  627. KEYWORD(catch);
  628. KEYWORD(filter);
  629. #undef KEYWORD
  630. // Keywords for types.
  631. #define TYPEKEYWORD(STR, LLVMTY) \
  632. do { \
  633. if (Keyword == STR) { \
  634. TyVal = LLVMTY; \
  635. return lltok::Type; \
  636. } \
  637. } while (false)
  638. TYPEKEYWORD("void", Type::getVoidTy(Context));
  639. TYPEKEYWORD("half", Type::getHalfTy(Context));
  640. TYPEKEYWORD("float", Type::getFloatTy(Context));
  641. TYPEKEYWORD("double", Type::getDoubleTy(Context));
  642. TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
  643. TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
  644. TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
  645. TYPEKEYWORD("label", Type::getLabelTy(Context));
  646. TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
  647. TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
  648. TYPEKEYWORD("token", Type::getTokenTy(Context));
  649. #undef TYPEKEYWORD
  650. // Keywords for instructions.
  651. #define INSTKEYWORD(STR, Enum) \
  652. do { \
  653. if (Keyword == #STR) { \
  654. UIntVal = Instruction::Enum; \
  655. return lltok::kw_##STR; \
  656. } \
  657. } while (false)
  658. INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
  659. INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
  660. INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
  661. INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
  662. INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
  663. INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
  664. INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
  665. INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
  666. INSTKEYWORD(phi, PHI);
  667. INSTKEYWORD(call, Call);
  668. INSTKEYWORD(trunc, Trunc);
  669. INSTKEYWORD(zext, ZExt);
  670. INSTKEYWORD(sext, SExt);
  671. INSTKEYWORD(fptrunc, FPTrunc);
  672. INSTKEYWORD(fpext, FPExt);
  673. INSTKEYWORD(uitofp, UIToFP);
  674. INSTKEYWORD(sitofp, SIToFP);
  675. INSTKEYWORD(fptoui, FPToUI);
  676. INSTKEYWORD(fptosi, FPToSI);
  677. INSTKEYWORD(inttoptr, IntToPtr);
  678. INSTKEYWORD(ptrtoint, PtrToInt);
  679. INSTKEYWORD(bitcast, BitCast);
  680. INSTKEYWORD(addrspacecast, AddrSpaceCast);
  681. INSTKEYWORD(select, Select);
  682. INSTKEYWORD(va_arg, VAArg);
  683. INSTKEYWORD(ret, Ret);
  684. INSTKEYWORD(br, Br);
  685. INSTKEYWORD(switch, Switch);
  686. INSTKEYWORD(indirectbr, IndirectBr);
  687. INSTKEYWORD(invoke, Invoke);
  688. INSTKEYWORD(resume, Resume);
  689. INSTKEYWORD(unreachable, Unreachable);
  690. INSTKEYWORD(alloca, Alloca);
  691. INSTKEYWORD(load, Load);
  692. INSTKEYWORD(store, Store);
  693. INSTKEYWORD(cmpxchg, AtomicCmpXchg);
  694. INSTKEYWORD(atomicrmw, AtomicRMW);
  695. INSTKEYWORD(fence, Fence);
  696. INSTKEYWORD(getelementptr, GetElementPtr);
  697. INSTKEYWORD(extractelement, ExtractElement);
  698. INSTKEYWORD(insertelement, InsertElement);
  699. INSTKEYWORD(shufflevector, ShuffleVector);
  700. INSTKEYWORD(extractvalue, ExtractValue);
  701. INSTKEYWORD(insertvalue, InsertValue);
  702. INSTKEYWORD(landingpad, LandingPad);
  703. INSTKEYWORD(cleanupret, CleanupRet);
  704. INSTKEYWORD(catchret, CatchRet);
  705. INSTKEYWORD(catchswitch, CatchSwitch);
  706. INSTKEYWORD(catchpad, CatchPad);
  707. INSTKEYWORD(cleanuppad, CleanupPad);
  708. #undef INSTKEYWORD
  709. #define DWKEYWORD(TYPE, TOKEN) \
  710. do { \
  711. if (Keyword.startswith("DW_" #TYPE "_")) { \
  712. StrVal.assign(Keyword.begin(), Keyword.end()); \
  713. return lltok::TOKEN; \
  714. } \
  715. } while (false)
  716. DWKEYWORD(TAG, DwarfTag);
  717. DWKEYWORD(ATE, DwarfAttEncoding);
  718. DWKEYWORD(VIRTUALITY, DwarfVirtuality);
  719. DWKEYWORD(LANG, DwarfLang);
  720. DWKEYWORD(CC, DwarfCC);
  721. DWKEYWORD(OP, DwarfOp);
  722. DWKEYWORD(MACINFO, DwarfMacinfo);
  723. #undef DWKEYWORD
  724. if (Keyword.startswith("DIFlag")) {
  725. StrVal.assign(Keyword.begin(), Keyword.end());
  726. return lltok::DIFlag;
  727. }
  728. if (Keyword.startswith("CSK_")) {
  729. StrVal.assign(Keyword.begin(), Keyword.end());
  730. return lltok::ChecksumKind;
  731. }
  732. if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
  733. Keyword == "LineTablesOnly") {
  734. StrVal.assign(Keyword.begin(), Keyword.end());
  735. return lltok::EmissionKind;
  736. }
  737. // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
  738. // the CFE to avoid forcing it to deal with 64-bit numbers.
  739. if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
  740. TokStart[1] == '0' && TokStart[2] == 'x' &&
  741. isxdigit(static_cast<unsigned char>(TokStart[3]))) {
  742. int len = CurPtr-TokStart-3;
  743. uint32_t bits = len * 4;
  744. StringRef HexStr(TokStart + 3, len);
  745. if (!all_of(HexStr, isxdigit)) {
  746. // Bad token, return it as an error.
  747. CurPtr = TokStart+3;
  748. return lltok::Error;
  749. }
  750. APInt Tmp(bits, HexStr, 16);
  751. uint32_t activeBits = Tmp.getActiveBits();
  752. if (activeBits > 0 && activeBits < bits)
  753. Tmp = Tmp.trunc(activeBits);
  754. APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
  755. return lltok::APSInt;
  756. }
  757. // If this is "cc1234", return this as just "cc".
  758. if (TokStart[0] == 'c' && TokStart[1] == 'c') {
  759. CurPtr = TokStart+2;
  760. return lltok::kw_cc;
  761. }
  762. // Finally, if this isn't known, return an error.
  763. CurPtr = TokStart+1;
  764. return lltok::Error;
  765. }
  766. /// Lex all tokens that start with a 0x prefix, knowing they match and are not
  767. /// labels.
  768. /// HexFPConstant 0x[0-9A-Fa-f]+
  769. /// HexFP80Constant 0xK[0-9A-Fa-f]+
  770. /// HexFP128Constant 0xL[0-9A-Fa-f]+
  771. /// HexPPC128Constant 0xM[0-9A-Fa-f]+
  772. /// HexHalfConstant 0xH[0-9A-Fa-f]+
  773. lltok::Kind LLLexer::Lex0x() {
  774. CurPtr = TokStart + 2;
  775. char Kind;
  776. if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
  777. Kind = *CurPtr++;
  778. } else {
  779. Kind = 'J';
  780. }
  781. if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
  782. // Bad token, return it as an error.
  783. CurPtr = TokStart+1;
  784. return lltok::Error;
  785. }
  786. while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
  787. ++CurPtr;
  788. if (Kind == 'J') {
  789. // HexFPConstant - Floating point constant represented in IEEE format as a
  790. // hexadecimal number for when exponential notation is not precise enough.
  791. // Half, Float, and double only.
  792. APFloatVal = APFloat(APFloat::IEEEdouble(),
  793. APInt(64, HexIntToVal(TokStart + 2, CurPtr)));
  794. return lltok::APFloat;
  795. }
  796. uint64_t Pair[2];
  797. switch (Kind) {
  798. default: llvm_unreachable("Unknown kind!");
  799. case 'K':
  800. // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
  801. FP80HexToIntPair(TokStart+3, CurPtr, Pair);
  802. APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
  803. return lltok::APFloat;
  804. case 'L':
  805. // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
  806. HexToIntPair(TokStart+3, CurPtr, Pair);
  807. APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
  808. return lltok::APFloat;
  809. case 'M':
  810. // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
  811. HexToIntPair(TokStart+3, CurPtr, Pair);
  812. APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
  813. return lltok::APFloat;
  814. case 'H':
  815. APFloatVal = APFloat(APFloat::IEEEhalf(),
  816. APInt(16,HexIntToVal(TokStart+3, CurPtr)));
  817. return lltok::APFloat;
  818. }
  819. }
  820. /// Lex tokens for a label or a numeric constant, possibly starting with -.
  821. /// Label [-a-zA-Z$._0-9]+:
  822. /// NInteger -[0-9]+
  823. /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
  824. /// PInteger [0-9]+
  825. /// HexFPConstant 0x[0-9A-Fa-f]+
  826. /// HexFP80Constant 0xK[0-9A-Fa-f]+
  827. /// HexFP128Constant 0xL[0-9A-Fa-f]+
  828. /// HexPPC128Constant 0xM[0-9A-Fa-f]+
  829. lltok::Kind LLLexer::LexDigitOrNegative() {
  830. // If the letter after the negative is not a number, this is probably a label.
  831. if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
  832. !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
  833. // Okay, this is not a number after the -, it's probably a label.
  834. if (const char *End = isLabelTail(CurPtr)) {
  835. StrVal.assign(TokStart, End-1);
  836. CurPtr = End;
  837. return lltok::LabelStr;
  838. }
  839. return lltok::Error;
  840. }
  841. // At this point, it is either a label, int or fp constant.
  842. // Skip digits, we have at least one.
  843. for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  844. /*empty*/;
  845. // Check to see if this really is a label afterall, e.g. "-1:".
  846. if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
  847. if (const char *End = isLabelTail(CurPtr)) {
  848. StrVal.assign(TokStart, End-1);
  849. CurPtr = End;
  850. return lltok::LabelStr;
  851. }
  852. }
  853. // If the next character is a '.', then it is a fp value, otherwise its
  854. // integer.
  855. if (CurPtr[0] != '.') {
  856. if (TokStart[0] == '0' && TokStart[1] == 'x')
  857. return Lex0x();
  858. APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
  859. return lltok::APSInt;
  860. }
  861. ++CurPtr;
  862. // Skip over [0-9]*([eE][-+]?[0-9]+)?
  863. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  864. if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
  865. if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
  866. ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
  867. isdigit(static_cast<unsigned char>(CurPtr[2])))) {
  868. CurPtr += 2;
  869. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  870. }
  871. }
  872. APFloatVal = APFloat(APFloat::IEEEdouble(),
  873. StringRef(TokStart, CurPtr - TokStart));
  874. return lltok::APFloat;
  875. }
  876. /// Lex a floating point constant starting with +.
  877. /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
  878. lltok::Kind LLLexer::LexPositive() {
  879. // If the letter after the negative is a number, this is probably not a
  880. // label.
  881. if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
  882. return lltok::Error;
  883. // Skip digits.
  884. for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  885. /*empty*/;
  886. // At this point, we need a '.'.
  887. if (CurPtr[0] != '.') {
  888. CurPtr = TokStart+1;
  889. return lltok::Error;
  890. }
  891. ++CurPtr;
  892. // Skip over [0-9]*([eE][-+]?[0-9]+)?
  893. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  894. if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
  895. if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
  896. ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
  897. isdigit(static_cast<unsigned char>(CurPtr[2])))) {
  898. CurPtr += 2;
  899. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  900. }
  901. }
  902. APFloatVal = APFloat(APFloat::IEEEdouble(),
  903. StringRef(TokStart, CurPtr - TokStart));
  904. return lltok::APFloat;
  905. }