LLLexer.cpp 25 KB

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