LLLexer.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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/StringExtras.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/AsmParser/Parser.h"
  17. #include "llvm/IR/DerivedTypes.h"
  18. #include "llvm/IR/Instruction.h"
  19. #include "llvm/IR/LLVMContext.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/MathExtras.h"
  22. #include "llvm/Support/MemoryBuffer.h"
  23. #include "llvm/Support/SourceMgr.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <cctype>
  26. #include <cstdio>
  27. #include <cstdlib>
  28. #include <cstring>
  29. using namespace llvm;
  30. bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
  31. ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
  32. return true;
  33. }
  34. //===----------------------------------------------------------------------===//
  35. // Helper functions.
  36. //===----------------------------------------------------------------------===//
  37. // atoull - Convert an ascii string of decimal digits into the unsigned long
  38. // long representation... this does not have to do input error checking,
  39. // because we know that the input will be matched by a suitable regex...
  40. //
  41. uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
  42. uint64_t Result = 0;
  43. for (; Buffer != End; Buffer++) {
  44. uint64_t OldRes = Result;
  45. Result *= 10;
  46. Result += *Buffer-'0';
  47. if (Result < OldRes) { // Uh, oh, overflow detected!!!
  48. Error("constant bigger than 64 bits detected!");
  49. return 0;
  50. }
  51. }
  52. return Result;
  53. }
  54. uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
  55. uint64_t Result = 0;
  56. for (; Buffer != End; ++Buffer) {
  57. uint64_t OldRes = Result;
  58. Result *= 16;
  59. Result += hexDigitValue(*Buffer);
  60. if (Result < OldRes) { // Uh, oh, overflow detected!!!
  61. Error("constant bigger than 64 bits detected!");
  62. return 0;
  63. }
  64. }
  65. return Result;
  66. }
  67. void LLLexer::HexToIntPair(const char *Buffer, const char *End,
  68. uint64_t Pair[2]) {
  69. Pair[0] = 0;
  70. for (int i=0; i<16; i++, Buffer++) {
  71. assert(Buffer != End);
  72. Pair[0] *= 16;
  73. Pair[0] += hexDigitValue(*Buffer);
  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; 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 (1) {
  135. if (CurPtr[0] == ':') return CurPtr+1;
  136. if (!isLabelChar(CurPtr[0])) return 0;
  137. ++CurPtr;
  138. }
  139. }
  140. //===----------------------------------------------------------------------===//
  141. // Lexer definition.
  142. //===----------------------------------------------------------------------===//
  143. LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
  144. LLVMContext &C)
  145. : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
  146. CurPtr = CurBuf->getBufferStart();
  147. }
  148. std::string LLLexer::getFilename() const {
  149. return CurBuf->getBufferIdentifier();
  150. }
  151. int LLLexer::getNextChar() {
  152. char CurChar = *CurPtr++;
  153. switch (CurChar) {
  154. default: return (unsigned char)CurChar;
  155. case 0:
  156. // A nul character in the stream is either the end of the current buffer or
  157. // a random nul in the file. Disambiguate that here.
  158. if (CurPtr-1 != CurBuf->getBufferEnd())
  159. return 0; // Just whitespace.
  160. // Otherwise, return end of file.
  161. --CurPtr; // Another call to lex will return EOF again.
  162. return EOF;
  163. }
  164. }
  165. lltok::Kind LLLexer::LexToken() {
  166. TokStart = CurPtr;
  167. int CurChar = getNextChar();
  168. switch (CurChar) {
  169. default:
  170. // Handle letters: [a-zA-Z_]
  171. if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
  172. return LexIdentifier();
  173. return lltok::Error;
  174. case EOF: return lltok::Eof;
  175. case 0:
  176. case ' ':
  177. case '\t':
  178. case '\n':
  179. case '\r':
  180. // Ignore whitespace.
  181. return LexToken();
  182. case '+': return LexPositive();
  183. case '@': return LexAt();
  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. if (const char *Ptr = isLabelTail(CurPtr)) {
  199. CurPtr = Ptr;
  200. StrVal.assign(TokStart, CurPtr-1);
  201. return lltok::LabelStr;
  202. }
  203. return lltok::Error;
  204. case ';':
  205. SkipLineComment();
  206. return LexToken();
  207. case '!': return LexExclaim();
  208. case '#': return LexHash();
  209. case '0': case '1': case '2': case '3': case '4':
  210. case '5': case '6': case '7': case '8': case '9':
  211. case '-':
  212. return LexDigitOrNegative();
  213. case '=': return lltok::equal;
  214. case '[': return lltok::lsquare;
  215. case ']': return lltok::rsquare;
  216. case '{': return lltok::lbrace;
  217. case '}': return lltok::rbrace;
  218. case '<': return lltok::less;
  219. case '>': return lltok::greater;
  220. case '(': return lltok::lparen;
  221. case ')': return lltok::rparen;
  222. case ',': return lltok::comma;
  223. case '*': return lltok::star;
  224. case '\\': return lltok::backslash;
  225. }
  226. }
  227. void LLLexer::SkipLineComment() {
  228. while (1) {
  229. if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
  230. return;
  231. }
  232. }
  233. /// LexAt - Lex all tokens that start with an @ character:
  234. /// GlobalVar @\"[^\"]*\"
  235. /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
  236. /// GlobalVarID @[0-9]+
  237. lltok::Kind LLLexer::LexAt() {
  238. // Handle AtStringConstant: @\"[^\"]*\"
  239. if (CurPtr[0] == '"') {
  240. ++CurPtr;
  241. while (1) {
  242. int CurChar = getNextChar();
  243. if (CurChar == EOF) {
  244. Error("end of file in global variable name");
  245. return lltok::Error;
  246. }
  247. if (CurChar == '"') {
  248. StrVal.assign(TokStart+2, CurPtr-1);
  249. UnEscapeLexed(StrVal);
  250. if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
  251. Error("Null bytes are not allowed in names");
  252. return lltok::Error;
  253. }
  254. return lltok::GlobalVar;
  255. }
  256. }
  257. }
  258. // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
  259. if (ReadVarName())
  260. return lltok::GlobalVar;
  261. // Handle GlobalVarID: @[0-9]+
  262. if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
  263. for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  264. /*empty*/;
  265. uint64_t Val = atoull(TokStart+1, CurPtr);
  266. if ((unsigned)Val != Val)
  267. Error("invalid value number (too large)!");
  268. UIntVal = unsigned(Val);
  269. return lltok::GlobalID;
  270. }
  271. return lltok::Error;
  272. }
  273. /// ReadString - Read a string until the closing quote.
  274. lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
  275. const char *Start = CurPtr;
  276. while (1) {
  277. int CurChar = getNextChar();
  278. if (CurChar == EOF) {
  279. Error("end of file in string constant");
  280. return lltok::Error;
  281. }
  282. if (CurChar == '"') {
  283. StrVal.assign(Start, CurPtr-1);
  284. UnEscapeLexed(StrVal);
  285. return kind;
  286. }
  287. }
  288. }
  289. /// ReadVarName - Read the rest of a token containing a variable name.
  290. bool LLLexer::ReadVarName() {
  291. const char *NameStart = CurPtr;
  292. if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
  293. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  294. CurPtr[0] == '.' || CurPtr[0] == '_') {
  295. ++CurPtr;
  296. while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
  297. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  298. CurPtr[0] == '.' || CurPtr[0] == '_')
  299. ++CurPtr;
  300. StrVal.assign(NameStart, CurPtr);
  301. return true;
  302. }
  303. return false;
  304. }
  305. /// LexPercent - Lex all tokens that start with a % character:
  306. /// LocalVar ::= %\"[^\"]*\"
  307. /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
  308. /// LocalVarID ::= %[0-9]+
  309. lltok::Kind LLLexer::LexPercent() {
  310. // Handle LocalVarName: %\"[^\"]*\"
  311. if (CurPtr[0] == '"') {
  312. ++CurPtr;
  313. return ReadString(lltok::LocalVar);
  314. }
  315. // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
  316. if (ReadVarName())
  317. return lltok::LocalVar;
  318. // Handle LocalVarID: %[0-9]+
  319. if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
  320. for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  321. /*empty*/;
  322. uint64_t Val = atoull(TokStart+1, CurPtr);
  323. if ((unsigned)Val != Val)
  324. Error("invalid value number (too large)!");
  325. UIntVal = unsigned(Val);
  326. return lltok::LocalVarID;
  327. }
  328. return lltok::Error;
  329. }
  330. /// LexQuote - Lex all tokens that start with a " character:
  331. /// QuoteLabel "[^"]+":
  332. /// StringConstant "[^"]*"
  333. lltok::Kind LLLexer::LexQuote() {
  334. lltok::Kind kind = ReadString(lltok::StringConstant);
  335. if (kind == lltok::Error || kind == lltok::Eof)
  336. return kind;
  337. if (CurPtr[0] == ':') {
  338. ++CurPtr;
  339. kind = lltok::LabelStr;
  340. }
  341. return kind;
  342. }
  343. /// LexExclaim:
  344. /// !foo
  345. /// !
  346. lltok::Kind LLLexer::LexExclaim() {
  347. // Lex a metadata name as a MetadataVar.
  348. if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
  349. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  350. CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
  351. ++CurPtr;
  352. while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
  353. CurPtr[0] == '-' || CurPtr[0] == '$' ||
  354. CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
  355. ++CurPtr;
  356. StrVal.assign(TokStart+1, CurPtr); // Skip !
  357. UnEscapeLexed(StrVal);
  358. return lltok::MetadataVar;
  359. }
  360. return lltok::exclaim;
  361. }
  362. /// LexHash - Lex all tokens that start with a # character:
  363. /// AttrGrpID ::= #[0-9]+
  364. lltok::Kind LLLexer::LexHash() {
  365. // Handle AttrGrpID: #[0-9]+
  366. if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
  367. for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  368. /*empty*/;
  369. uint64_t Val = atoull(TokStart+1, CurPtr);
  370. if ((unsigned)Val != Val)
  371. Error("invalid value number (too large)!");
  372. UIntVal = unsigned(Val);
  373. return lltok::AttrGrpID;
  374. }
  375. return lltok::Error;
  376. }
  377. /// LexIdentifier: Handle several related productions:
  378. /// Label [-a-zA-Z$._0-9]+:
  379. /// IntegerType i[0-9]+
  380. /// Keyword sdiv, float, ...
  381. /// HexIntConstant [us]0x[0-9A-Fa-f]+
  382. lltok::Kind LLLexer::LexIdentifier() {
  383. const char *StartChar = CurPtr;
  384. const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
  385. const char *KeywordEnd = 0;
  386. for (; isLabelChar(*CurPtr); ++CurPtr) {
  387. // If we decide this is an integer, remember the end of the sequence.
  388. if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
  389. IntEnd = CurPtr;
  390. if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
  391. *CurPtr != '_')
  392. KeywordEnd = CurPtr;
  393. }
  394. // If we stopped due to a colon, this really is a label.
  395. if (*CurPtr == ':') {
  396. StrVal.assign(StartChar-1, CurPtr++);
  397. return lltok::LabelStr;
  398. }
  399. // Otherwise, this wasn't a label. If this was valid as an integer type,
  400. // return it.
  401. if (IntEnd == 0) IntEnd = CurPtr;
  402. if (IntEnd != StartChar) {
  403. CurPtr = IntEnd;
  404. uint64_t NumBits = atoull(StartChar, CurPtr);
  405. if (NumBits < IntegerType::MIN_INT_BITS ||
  406. NumBits > IntegerType::MAX_INT_BITS) {
  407. Error("bitwidth for integer type out of range!");
  408. return lltok::Error;
  409. }
  410. TyVal = IntegerType::get(Context, NumBits);
  411. return lltok::Type;
  412. }
  413. // Otherwise, this was a letter sequence. See which keyword this is.
  414. if (KeywordEnd == 0) KeywordEnd = CurPtr;
  415. CurPtr = KeywordEnd;
  416. --StartChar;
  417. unsigned Len = CurPtr-StartChar;
  418. #define KEYWORD(STR) \
  419. do { \
  420. if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
  421. return lltok::kw_##STR; \
  422. } while (0)
  423. KEYWORD(true); KEYWORD(false);
  424. KEYWORD(declare); KEYWORD(define);
  425. KEYWORD(global); KEYWORD(constant);
  426. KEYWORD(private);
  427. KEYWORD(internal);
  428. KEYWORD(available_externally);
  429. KEYWORD(linkonce);
  430. KEYWORD(linkonce_odr);
  431. KEYWORD(weak);
  432. KEYWORD(weak_odr);
  433. KEYWORD(appending);
  434. KEYWORD(dllimport);
  435. KEYWORD(dllexport);
  436. KEYWORD(common);
  437. KEYWORD(default);
  438. KEYWORD(hidden);
  439. KEYWORD(protected);
  440. KEYWORD(unnamed_addr);
  441. KEYWORD(externally_initialized);
  442. KEYWORD(extern_weak);
  443. KEYWORD(external);
  444. KEYWORD(thread_local);
  445. KEYWORD(localdynamic);
  446. KEYWORD(initialexec);
  447. KEYWORD(localexec);
  448. KEYWORD(zeroinitializer);
  449. KEYWORD(undef);
  450. KEYWORD(null);
  451. KEYWORD(to);
  452. KEYWORD(tail);
  453. KEYWORD(target);
  454. KEYWORD(triple);
  455. KEYWORD(unwind);
  456. KEYWORD(deplibs); // FIXME: Remove in 4.0.
  457. KEYWORD(datalayout);
  458. KEYWORD(volatile);
  459. KEYWORD(atomic);
  460. KEYWORD(unordered);
  461. KEYWORD(monotonic);
  462. KEYWORD(acquire);
  463. KEYWORD(release);
  464. KEYWORD(acq_rel);
  465. KEYWORD(seq_cst);
  466. KEYWORD(singlethread);
  467. KEYWORD(nnan);
  468. KEYWORD(ninf);
  469. KEYWORD(nsz);
  470. KEYWORD(arcp);
  471. KEYWORD(fast);
  472. KEYWORD(nuw);
  473. KEYWORD(nsw);
  474. KEYWORD(exact);
  475. KEYWORD(inbounds);
  476. KEYWORD(align);
  477. KEYWORD(addrspace);
  478. KEYWORD(section);
  479. KEYWORD(alias);
  480. KEYWORD(module);
  481. KEYWORD(asm);
  482. KEYWORD(sideeffect);
  483. KEYWORD(alignstack);
  484. KEYWORD(inteldialect);
  485. KEYWORD(gc);
  486. KEYWORD(prefix);
  487. KEYWORD(ccc);
  488. KEYWORD(fastcc);
  489. KEYWORD(coldcc);
  490. KEYWORD(x86_stdcallcc);
  491. KEYWORD(x86_fastcallcc);
  492. KEYWORD(x86_thiscallcc);
  493. KEYWORD(x86_cdeclmethodcc);
  494. KEYWORD(arm_apcscc);
  495. KEYWORD(arm_aapcscc);
  496. KEYWORD(arm_aapcs_vfpcc);
  497. KEYWORD(msp430_intrcc);
  498. KEYWORD(ptx_kernel);
  499. KEYWORD(ptx_device);
  500. KEYWORD(spir_kernel);
  501. KEYWORD(spir_func);
  502. KEYWORD(intel_ocl_bicc);
  503. KEYWORD(x86_64_sysvcc);
  504. KEYWORD(x86_64_win64cc);
  505. KEYWORD(webkit_jscc);
  506. KEYWORD(anyregcc);
  507. KEYWORD(preserve_mostcc);
  508. KEYWORD(preserve_allcc);
  509. KEYWORD(cc);
  510. KEYWORD(c);
  511. KEYWORD(attributes);
  512. KEYWORD(alwaysinline);
  513. KEYWORD(builtin);
  514. KEYWORD(byval);
  515. KEYWORD(inalloca);
  516. KEYWORD(cold);
  517. KEYWORD(inlinehint);
  518. KEYWORD(inreg);
  519. KEYWORD(minsize);
  520. KEYWORD(naked);
  521. KEYWORD(nest);
  522. KEYWORD(noalias);
  523. KEYWORD(nobuiltin);
  524. KEYWORD(nocapture);
  525. KEYWORD(noduplicate);
  526. KEYWORD(noimplicitfloat);
  527. KEYWORD(noinline);
  528. KEYWORD(nonlazybind);
  529. KEYWORD(noredzone);
  530. KEYWORD(noreturn);
  531. KEYWORD(nounwind);
  532. KEYWORD(optnone);
  533. KEYWORD(optsize);
  534. KEYWORD(readnone);
  535. KEYWORD(readonly);
  536. KEYWORD(returned);
  537. KEYWORD(returns_twice);
  538. KEYWORD(signext);
  539. KEYWORD(sret);
  540. KEYWORD(ssp);
  541. KEYWORD(sspreq);
  542. KEYWORD(sspstrong);
  543. KEYWORD(sanitize_address);
  544. KEYWORD(sanitize_thread);
  545. KEYWORD(sanitize_memory);
  546. KEYWORD(uwtable);
  547. KEYWORD(zeroext);
  548. KEYWORD(type);
  549. KEYWORD(opaque);
  550. KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
  551. KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
  552. KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
  553. KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
  554. KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
  555. KEYWORD(umin);
  556. KEYWORD(x);
  557. KEYWORD(blockaddress);
  558. KEYWORD(personality);
  559. KEYWORD(cleanup);
  560. KEYWORD(catch);
  561. KEYWORD(filter);
  562. #undef KEYWORD
  563. // Keywords for types.
  564. #define TYPEKEYWORD(STR, LLVMTY) \
  565. if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
  566. TyVal = LLVMTY; return lltok::Type; }
  567. TYPEKEYWORD("void", Type::getVoidTy(Context));
  568. TYPEKEYWORD("half", Type::getHalfTy(Context));
  569. TYPEKEYWORD("float", Type::getFloatTy(Context));
  570. TYPEKEYWORD("double", Type::getDoubleTy(Context));
  571. TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
  572. TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
  573. TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
  574. TYPEKEYWORD("label", Type::getLabelTy(Context));
  575. TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
  576. TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
  577. #undef TYPEKEYWORD
  578. // Keywords for instructions.
  579. #define INSTKEYWORD(STR, Enum) \
  580. if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
  581. UIntVal = Instruction::Enum; return lltok::kw_##STR; }
  582. INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
  583. INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
  584. INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
  585. INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
  586. INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
  587. INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
  588. INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
  589. INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
  590. INSTKEYWORD(phi, PHI);
  591. INSTKEYWORD(call, Call);
  592. INSTKEYWORD(trunc, Trunc);
  593. INSTKEYWORD(zext, ZExt);
  594. INSTKEYWORD(sext, SExt);
  595. INSTKEYWORD(fptrunc, FPTrunc);
  596. INSTKEYWORD(fpext, FPExt);
  597. INSTKEYWORD(uitofp, UIToFP);
  598. INSTKEYWORD(sitofp, SIToFP);
  599. INSTKEYWORD(fptoui, FPToUI);
  600. INSTKEYWORD(fptosi, FPToSI);
  601. INSTKEYWORD(inttoptr, IntToPtr);
  602. INSTKEYWORD(ptrtoint, PtrToInt);
  603. INSTKEYWORD(bitcast, BitCast);
  604. INSTKEYWORD(addrspacecast, AddrSpaceCast);
  605. INSTKEYWORD(select, Select);
  606. INSTKEYWORD(va_arg, VAArg);
  607. INSTKEYWORD(ret, Ret);
  608. INSTKEYWORD(br, Br);
  609. INSTKEYWORD(switch, Switch);
  610. INSTKEYWORD(indirectbr, IndirectBr);
  611. INSTKEYWORD(invoke, Invoke);
  612. INSTKEYWORD(resume, Resume);
  613. INSTKEYWORD(unreachable, Unreachable);
  614. INSTKEYWORD(alloca, Alloca);
  615. INSTKEYWORD(load, Load);
  616. INSTKEYWORD(store, Store);
  617. INSTKEYWORD(cmpxchg, AtomicCmpXchg);
  618. INSTKEYWORD(atomicrmw, AtomicRMW);
  619. INSTKEYWORD(fence, Fence);
  620. INSTKEYWORD(getelementptr, GetElementPtr);
  621. INSTKEYWORD(extractelement, ExtractElement);
  622. INSTKEYWORD(insertelement, InsertElement);
  623. INSTKEYWORD(shufflevector, ShuffleVector);
  624. INSTKEYWORD(extractvalue, ExtractValue);
  625. INSTKEYWORD(insertvalue, InsertValue);
  626. INSTKEYWORD(landingpad, LandingPad);
  627. #undef INSTKEYWORD
  628. // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
  629. // the CFE to avoid forcing it to deal with 64-bit numbers.
  630. if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
  631. TokStart[1] == '0' && TokStart[2] == 'x' &&
  632. isxdigit(static_cast<unsigned char>(TokStart[3]))) {
  633. int len = CurPtr-TokStart-3;
  634. uint32_t bits = len * 4;
  635. APInt Tmp(bits, StringRef(TokStart+3, len), 16);
  636. uint32_t activeBits = Tmp.getActiveBits();
  637. if (activeBits > 0 && activeBits < bits)
  638. Tmp = Tmp.trunc(activeBits);
  639. APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
  640. return lltok::APSInt;
  641. }
  642. // If this is "cc1234", return this as just "cc".
  643. if (TokStart[0] == 'c' && TokStart[1] == 'c') {
  644. CurPtr = TokStart+2;
  645. return lltok::kw_cc;
  646. }
  647. // Finally, if this isn't known, return an error.
  648. CurPtr = TokStart+1;
  649. return lltok::Error;
  650. }
  651. /// Lex0x: Handle productions that start with 0x, knowing that it matches and
  652. /// that this is not a label:
  653. /// HexFPConstant 0x[0-9A-Fa-f]+
  654. /// HexFP80Constant 0xK[0-9A-Fa-f]+
  655. /// HexFP128Constant 0xL[0-9A-Fa-f]+
  656. /// HexPPC128Constant 0xM[0-9A-Fa-f]+
  657. /// HexHalfConstant 0xH[0-9A-Fa-f]+
  658. lltok::Kind LLLexer::Lex0x() {
  659. CurPtr = TokStart + 2;
  660. char Kind;
  661. if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
  662. Kind = *CurPtr++;
  663. } else {
  664. Kind = 'J';
  665. }
  666. if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
  667. // Bad token, return it as an error.
  668. CurPtr = TokStart+1;
  669. return lltok::Error;
  670. }
  671. while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
  672. ++CurPtr;
  673. if (Kind == 'J') {
  674. // HexFPConstant - Floating point constant represented in IEEE format as a
  675. // hexadecimal number for when exponential notation is not precise enough.
  676. // Half, Float, and double only.
  677. APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
  678. return lltok::APFloat;
  679. }
  680. uint64_t Pair[2];
  681. switch (Kind) {
  682. default: llvm_unreachable("Unknown kind!");
  683. case 'K':
  684. // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
  685. FP80HexToIntPair(TokStart+3, CurPtr, Pair);
  686. APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
  687. return lltok::APFloat;
  688. case 'L':
  689. // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
  690. HexToIntPair(TokStart+3, CurPtr, Pair);
  691. APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
  692. return lltok::APFloat;
  693. case 'M':
  694. // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
  695. HexToIntPair(TokStart+3, CurPtr, Pair);
  696. APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
  697. return lltok::APFloat;
  698. case 'H':
  699. APFloatVal = APFloat(APFloat::IEEEhalf,
  700. APInt(16,HexIntToVal(TokStart+3, CurPtr)));
  701. return lltok::APFloat;
  702. }
  703. }
  704. /// LexIdentifier: Handle several related productions:
  705. /// Label [-a-zA-Z$._0-9]+:
  706. /// NInteger -[0-9]+
  707. /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
  708. /// PInteger [0-9]+
  709. /// HexFPConstant 0x[0-9A-Fa-f]+
  710. /// HexFP80Constant 0xK[0-9A-Fa-f]+
  711. /// HexFP128Constant 0xL[0-9A-Fa-f]+
  712. /// HexPPC128Constant 0xM[0-9A-Fa-f]+
  713. lltok::Kind LLLexer::LexDigitOrNegative() {
  714. // If the letter after the negative is not a number, this is probably a label.
  715. if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
  716. !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
  717. // Okay, this is not a number after the -, it's probably a label.
  718. if (const char *End = isLabelTail(CurPtr)) {
  719. StrVal.assign(TokStart, End-1);
  720. CurPtr = End;
  721. return lltok::LabelStr;
  722. }
  723. return lltok::Error;
  724. }
  725. // At this point, it is either a label, int or fp constant.
  726. // Skip digits, we have at least one.
  727. for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  728. /*empty*/;
  729. // Check to see if this really is a label afterall, e.g. "-1:".
  730. if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
  731. if (const char *End = isLabelTail(CurPtr)) {
  732. StrVal.assign(TokStart, End-1);
  733. CurPtr = End;
  734. return lltok::LabelStr;
  735. }
  736. }
  737. // If the next character is a '.', then it is a fp value, otherwise its
  738. // integer.
  739. if (CurPtr[0] != '.') {
  740. if (TokStart[0] == '0' && TokStart[1] == 'x')
  741. return Lex0x();
  742. unsigned Len = CurPtr-TokStart;
  743. uint32_t numBits = ((Len * 64) / 19) + 2;
  744. APInt Tmp(numBits, StringRef(TokStart, Len), 10);
  745. if (TokStart[0] == '-') {
  746. uint32_t minBits = Tmp.getMinSignedBits();
  747. if (minBits > 0 && minBits < numBits)
  748. Tmp = Tmp.trunc(minBits);
  749. APSIntVal = APSInt(Tmp, false);
  750. } else {
  751. uint32_t activeBits = Tmp.getActiveBits();
  752. if (activeBits > 0 && activeBits < numBits)
  753. Tmp = Tmp.trunc(activeBits);
  754. APSIntVal = APSInt(Tmp, true);
  755. }
  756. return lltok::APSInt;
  757. }
  758. ++CurPtr;
  759. // Skip over [0-9]*([eE][-+]?[0-9]+)?
  760. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  761. if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
  762. if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
  763. ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
  764. isdigit(static_cast<unsigned char>(CurPtr[2])))) {
  765. CurPtr += 2;
  766. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  767. }
  768. }
  769. APFloatVal = APFloat(std::atof(TokStart));
  770. return lltok::APFloat;
  771. }
  772. /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
  773. lltok::Kind LLLexer::LexPositive() {
  774. // If the letter after the negative is a number, this is probably not a
  775. // label.
  776. if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
  777. return lltok::Error;
  778. // Skip digits.
  779. for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
  780. /*empty*/;
  781. // At this point, we need a '.'.
  782. if (CurPtr[0] != '.') {
  783. CurPtr = TokStart+1;
  784. return lltok::Error;
  785. }
  786. ++CurPtr;
  787. // Skip over [0-9]*([eE][-+]?[0-9]+)?
  788. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  789. if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
  790. if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
  791. ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
  792. isdigit(static_cast<unsigned char>(CurPtr[2])))) {
  793. CurPtr += 2;
  794. while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
  795. }
  796. }
  797. APFloatVal = APFloat(std::atof(TokStart));
  798. return lltok::APFloat;
  799. }