AsmLexer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. //===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This class implements the lexer for assembly files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/MC/MCParser/AsmLexer.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/StringExtras.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/ADT/StringSwitch.h"
  18. #include "llvm/MC/MCAsmInfo.h"
  19. #include "llvm/MC/MCParser/MCAsmLexer.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include "llvm/Support/SMLoc.h"
  22. #include "llvm/Support/SaveAndRestore.h"
  23. #include <cassert>
  24. #include <cctype>
  25. #include <cstdio>
  26. #include <cstring>
  27. #include <string>
  28. #include <tuple>
  29. #include <utility>
  30. using namespace llvm;
  31. AsmLexer::AsmLexer(const MCAsmInfo &MAI) : MAI(MAI) {
  32. AllowAtInIdentifier = !StringRef(MAI.getCommentString()).startswith("@");
  33. }
  34. AsmLexer::~AsmLexer() = default;
  35. void AsmLexer::setBuffer(StringRef Buf, const char *ptr,
  36. bool EndStatementAtEOF) {
  37. CurBuf = Buf;
  38. if (ptr)
  39. CurPtr = ptr;
  40. else
  41. CurPtr = CurBuf.begin();
  42. TokStart = nullptr;
  43. this->EndStatementAtEOF = EndStatementAtEOF;
  44. }
  45. /// ReturnError - Set the error to the specified string at the specified
  46. /// location. This is defined to always return AsmToken::Error.
  47. AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
  48. SetError(SMLoc::getFromPointer(Loc), Msg);
  49. return AsmToken(AsmToken::Error, StringRef(Loc, CurPtr - Loc));
  50. }
  51. int AsmLexer::getNextChar() {
  52. if (CurPtr == CurBuf.end())
  53. return EOF;
  54. return (unsigned char)*CurPtr++;
  55. }
  56. int AsmLexer::peekNextChar() {
  57. if (CurPtr == CurBuf.end())
  58. return EOF;
  59. return (unsigned char)*CurPtr;
  60. }
  61. /// The leading integral digit sequence and dot should have already been
  62. /// consumed, some or all of the fractional digit sequence *can* have been
  63. /// consumed.
  64. AsmToken AsmLexer::LexFloatLiteral() {
  65. // Skip the fractional digit sequence.
  66. while (isDigit(*CurPtr))
  67. ++CurPtr;
  68. if (*CurPtr == '-' || *CurPtr == '+')
  69. return ReturnError(CurPtr, "Invalid sign in float literal");
  70. // Check for exponent
  71. if ((*CurPtr == 'e' || *CurPtr == 'E')) {
  72. ++CurPtr;
  73. if (*CurPtr == '-' || *CurPtr == '+')
  74. ++CurPtr;
  75. while (isDigit(*CurPtr))
  76. ++CurPtr;
  77. }
  78. return AsmToken(AsmToken::Real,
  79. StringRef(TokStart, CurPtr - TokStart));
  80. }
  81. /// LexHexFloatLiteral matches essentially (.[0-9a-fA-F]*)?[pP][+-]?[0-9a-fA-F]+
  82. /// while making sure there are enough actual digits around for the constant to
  83. /// be valid.
  84. ///
  85. /// The leading "0x[0-9a-fA-F]*" (i.e. integer part) has already been consumed
  86. /// before we get here.
  87. AsmToken AsmLexer::LexHexFloatLiteral(bool NoIntDigits) {
  88. assert((*CurPtr == 'p' || *CurPtr == 'P' || *CurPtr == '.') &&
  89. "unexpected parse state in floating hex");
  90. bool NoFracDigits = true;
  91. // Skip the fractional part if there is one
  92. if (*CurPtr == '.') {
  93. ++CurPtr;
  94. const char *FracStart = CurPtr;
  95. while (isHexDigit(*CurPtr))
  96. ++CurPtr;
  97. NoFracDigits = CurPtr == FracStart;
  98. }
  99. if (NoIntDigits && NoFracDigits)
  100. return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
  101. "expected at least one significand digit");
  102. // Make sure we do have some kind of proper exponent part
  103. if (*CurPtr != 'p' && *CurPtr != 'P')
  104. return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
  105. "expected exponent part 'p'");
  106. ++CurPtr;
  107. if (*CurPtr == '+' || *CurPtr == '-')
  108. ++CurPtr;
  109. // N.b. exponent digits are *not* hex
  110. const char *ExpStart = CurPtr;
  111. while (isDigit(*CurPtr))
  112. ++CurPtr;
  113. if (CurPtr == ExpStart)
  114. return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
  115. "expected at least one exponent digit");
  116. return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
  117. }
  118. /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@?]*
  119. static bool IsIdentifierChar(char c, bool AllowAt) {
  120. return isAlnum(c) || c == '_' || c == '$' || c == '.' ||
  121. (c == '@' && AllowAt) || c == '?';
  122. }
  123. AsmToken AsmLexer::LexIdentifier() {
  124. // Check for floating point literals.
  125. if (CurPtr[-1] == '.' && isDigit(*CurPtr)) {
  126. // Disambiguate a .1243foo identifier from a floating literal.
  127. while (isDigit(*CurPtr))
  128. ++CurPtr;
  129. if (!IsIdentifierChar(*CurPtr, AllowAtInIdentifier) ||
  130. *CurPtr == 'e' || *CurPtr == 'E')
  131. return LexFloatLiteral();
  132. }
  133. while (IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
  134. ++CurPtr;
  135. // Handle . as a special case.
  136. if (CurPtr == TokStart+1 && TokStart[0] == '.')
  137. return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
  138. return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
  139. }
  140. /// LexSlash: Slash: /
  141. /// C-Style Comment: /* ... */
  142. AsmToken AsmLexer::LexSlash() {
  143. switch (*CurPtr) {
  144. case '*':
  145. IsAtStartOfStatement = false;
  146. break; // C style comment.
  147. case '/':
  148. ++CurPtr;
  149. return LexLineComment();
  150. default:
  151. IsAtStartOfStatement = false;
  152. return AsmToken(AsmToken::Slash, StringRef(TokStart, 1));
  153. }
  154. // C Style comment.
  155. ++CurPtr; // skip the star.
  156. const char *CommentTextStart = CurPtr;
  157. while (CurPtr != CurBuf.end()) {
  158. switch (*CurPtr++) {
  159. case '*':
  160. // End of the comment?
  161. if (*CurPtr != '/')
  162. break;
  163. // If we have a CommentConsumer, notify it about the comment.
  164. if (CommentConsumer) {
  165. CommentConsumer->HandleComment(
  166. SMLoc::getFromPointer(CommentTextStart),
  167. StringRef(CommentTextStart, CurPtr - 1 - CommentTextStart));
  168. }
  169. ++CurPtr; // End the */.
  170. return AsmToken(AsmToken::Comment,
  171. StringRef(TokStart, CurPtr - TokStart));
  172. }
  173. }
  174. return ReturnError(TokStart, "unterminated comment");
  175. }
  176. /// LexLineComment: Comment: #[^\n]*
  177. /// : //[^\n]*
  178. AsmToken AsmLexer::LexLineComment() {
  179. // Mark This as an end of statement with a body of the
  180. // comment. While it would be nicer to leave this two tokens,
  181. // backwards compatability with TargetParsers makes keeping this in this form
  182. // better.
  183. const char *CommentTextStart = CurPtr;
  184. int CurChar = getNextChar();
  185. while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
  186. CurChar = getNextChar();
  187. if (CurChar == '\r' && CurPtr != CurBuf.end() && *CurPtr == '\n')
  188. ++CurPtr;
  189. // If we have a CommentConsumer, notify it about the comment.
  190. if (CommentConsumer) {
  191. CommentConsumer->HandleComment(
  192. SMLoc::getFromPointer(CommentTextStart),
  193. StringRef(CommentTextStart, CurPtr - 1 - CommentTextStart));
  194. }
  195. IsAtStartOfLine = true;
  196. // This is a whole line comment. leave newline
  197. if (IsAtStartOfStatement)
  198. return AsmToken(AsmToken::EndOfStatement,
  199. StringRef(TokStart, CurPtr - TokStart));
  200. IsAtStartOfStatement = true;
  201. return AsmToken(AsmToken::EndOfStatement,
  202. StringRef(TokStart, CurPtr - 1 - TokStart));
  203. }
  204. static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
  205. // Skip ULL, UL, U, L and LL suffices.
  206. if (CurPtr[0] == 'U')
  207. ++CurPtr;
  208. if (CurPtr[0] == 'L')
  209. ++CurPtr;
  210. if (CurPtr[0] == 'L')
  211. ++CurPtr;
  212. }
  213. // Look ahead to search for first non-hex digit, if it's [hH], then we treat the
  214. // integer as a hexadecimal, possibly with leading zeroes.
  215. static unsigned doHexLookAhead(const char *&CurPtr, unsigned DefaultRadix,
  216. bool LexHex) {
  217. const char *FirstNonDec = nullptr;
  218. const char *LookAhead = CurPtr;
  219. while (true) {
  220. if (isDigit(*LookAhead)) {
  221. ++LookAhead;
  222. } else {
  223. if (!FirstNonDec)
  224. FirstNonDec = LookAhead;
  225. // Keep going if we are looking for a 'h' suffix.
  226. if (LexHex && isHexDigit(*LookAhead))
  227. ++LookAhead;
  228. else
  229. break;
  230. }
  231. }
  232. bool isHex = LexHex && (*LookAhead == 'h' || *LookAhead == 'H');
  233. CurPtr = isHex || !FirstNonDec ? LookAhead : FirstNonDec;
  234. if (isHex)
  235. return 16;
  236. return DefaultRadix;
  237. }
  238. static const char *findLastDigit(const char *CurPtr, unsigned DefaultRadix) {
  239. while (hexDigitValue(*CurPtr) < DefaultRadix) {
  240. ++CurPtr;
  241. }
  242. return CurPtr;
  243. }
  244. static AsmToken intToken(StringRef Ref, APInt &Value) {
  245. if (Value.isIntN(64))
  246. return AsmToken(AsmToken::Integer, Ref, Value);
  247. return AsmToken(AsmToken::BigNum, Ref, Value);
  248. }
  249. static std::string radixName(unsigned Radix) {
  250. switch (Radix) {
  251. case 2:
  252. return "binary";
  253. case 8:
  254. return "octal";
  255. case 10:
  256. return "decimal";
  257. case 16:
  258. return "hexadecimal";
  259. default:
  260. return "base-" + std::to_string(Radix);
  261. }
  262. }
  263. /// LexDigit: First character is [0-9].
  264. /// Local Label: [0-9][:]
  265. /// Forward/Backward Label: [0-9][fb]
  266. /// Binary integer: 0b[01]+
  267. /// Octal integer: 0[0-7]+
  268. /// Hex integer: 0x[0-9a-fA-F]+ or [0x]?[0-9][0-9a-fA-F]*[hH]
  269. /// Decimal integer: [1-9][0-9]*
  270. AsmToken AsmLexer::LexDigit() {
  271. // MASM-flavor binary integer: [01]+[yY] (if DefaultRadix < 16, [bByY])
  272. // MASM-flavor octal integer: [0-7]+[oOqQ]
  273. // MASM-flavor decimal integer: [0-9]+[tT] (if DefaultRadix < 16, [dDtT])
  274. // MASM-flavor hexadecimal integer: [0-9][0-9a-fA-F]*[hH]
  275. if (LexMasmIntegers && isdigit(CurPtr[-1])) {
  276. const char *FirstNonBinary =
  277. (CurPtr[-1] != '0' && CurPtr[-1] != '1') ? CurPtr - 1 : nullptr;
  278. const char *FirstNonDecimal =
  279. (CurPtr[-1] < '0' || CurPtr[-1] > '9') ? CurPtr - 1 : nullptr;
  280. const char *OldCurPtr = CurPtr;
  281. while (isHexDigit(*CurPtr)) {
  282. switch (*CurPtr) {
  283. default:
  284. if (!FirstNonDecimal) {
  285. FirstNonDecimal = CurPtr;
  286. }
  287. LLVM_FALLTHROUGH;
  288. case '9':
  289. case '8':
  290. case '7':
  291. case '6':
  292. case '5':
  293. case '4':
  294. case '3':
  295. case '2':
  296. if (!FirstNonBinary) {
  297. FirstNonBinary = CurPtr;
  298. }
  299. break;
  300. case '1':
  301. case '0':
  302. break;
  303. }
  304. ++CurPtr;
  305. }
  306. if (*CurPtr == '.') {
  307. // MASM float literals (other than hex floats) always contain a ".", and
  308. // are always written in decimal.
  309. ++CurPtr;
  310. return LexFloatLiteral();
  311. }
  312. if (LexMasmHexFloats && (*CurPtr == 'r' || *CurPtr == 'R')) {
  313. ++CurPtr;
  314. return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
  315. }
  316. unsigned Radix = 0;
  317. if (*CurPtr == 'h' || *CurPtr == 'H') {
  318. // hexadecimal number
  319. ++CurPtr;
  320. Radix = 16;
  321. } else if (*CurPtr == 't' || *CurPtr == 'T') {
  322. // decimal number
  323. ++CurPtr;
  324. Radix = 10;
  325. } else if (*CurPtr == 'o' || *CurPtr == 'O' || *CurPtr == 'q' ||
  326. *CurPtr == 'Q') {
  327. // octal number
  328. ++CurPtr;
  329. Radix = 8;
  330. } else if (*CurPtr == 'y' || *CurPtr == 'Y') {
  331. // binary number
  332. ++CurPtr;
  333. Radix = 2;
  334. } else if (FirstNonDecimal && FirstNonDecimal + 1 == CurPtr &&
  335. DefaultRadix < 14 &&
  336. (*FirstNonDecimal == 'd' || *FirstNonDecimal == 'D')) {
  337. Radix = 10;
  338. } else if (FirstNonBinary && FirstNonBinary + 1 == CurPtr &&
  339. DefaultRadix < 12 &&
  340. (*FirstNonBinary == 'b' || *FirstNonBinary == 'B')) {
  341. Radix = 2;
  342. }
  343. if (Radix) {
  344. StringRef Result(TokStart, CurPtr - TokStart);
  345. APInt Value(128, 0, true);
  346. if (Result.drop_back().getAsInteger(Radix, Value))
  347. return ReturnError(TokStart, "invalid " + radixName(Radix) + " number");
  348. // MSVC accepts and ignores type suffices on integer literals.
  349. SkipIgnoredIntegerSuffix(CurPtr);
  350. return intToken(Result, Value);
  351. }
  352. // default-radix integers, or floating point numbers, fall through
  353. CurPtr = OldCurPtr;
  354. }
  355. // MASM default-radix integers: [0-9a-fA-F]+
  356. // (All other integer literals have a radix specifier.)
  357. if (LexMasmIntegers && UseMasmDefaultRadix) {
  358. CurPtr = findLastDigit(CurPtr, 16);
  359. StringRef Result(TokStart, CurPtr - TokStart);
  360. APInt Value(128, 0, true);
  361. if (Result.getAsInteger(DefaultRadix, Value)) {
  362. return ReturnError(TokStart,
  363. "invalid " + radixName(DefaultRadix) + " number");
  364. }
  365. return intToken(Result, Value);
  366. }
  367. // Decimal integer: [1-9][0-9]*
  368. if (CurPtr[-1] != '0' || CurPtr[0] == '.') {
  369. unsigned Radix = doHexLookAhead(CurPtr, 10, LexMasmIntegers);
  370. bool isHex = Radix == 16;
  371. // Check for floating point literals.
  372. if (!isHex && (*CurPtr == '.' || *CurPtr == 'e' || *CurPtr == 'E')) {
  373. if (*CurPtr == '.')
  374. ++CurPtr;
  375. return LexFloatLiteral();
  376. }
  377. StringRef Result(TokStart, CurPtr - TokStart);
  378. APInt Value(128, 0, true);
  379. if (Result.getAsInteger(Radix, Value)) {
  380. return ReturnError(TokStart, "invalid " + radixName(Radix) + " number");
  381. }
  382. // The darwin/x86 (and x86-64) assembler accepts and ignores type
  383. // suffices on integer literals.
  384. SkipIgnoredIntegerSuffix(CurPtr);
  385. return intToken(Result, Value);
  386. }
  387. if (!LexMasmIntegers && ((*CurPtr == 'b') || (*CurPtr == 'B'))) {
  388. ++CurPtr;
  389. // See if we actually have "0b" as part of something like "jmp 0b\n"
  390. if (!isDigit(CurPtr[0])) {
  391. --CurPtr;
  392. StringRef Result(TokStart, CurPtr - TokStart);
  393. return AsmToken(AsmToken::Integer, Result, 0);
  394. }
  395. const char *NumStart = CurPtr;
  396. while (CurPtr[0] == '0' || CurPtr[0] == '1')
  397. ++CurPtr;
  398. // Requires at least one binary digit.
  399. if (CurPtr == NumStart)
  400. return ReturnError(TokStart, "invalid binary number");
  401. StringRef Result(TokStart, CurPtr - TokStart);
  402. APInt Value(128, 0, true);
  403. if (Result.substr(2).getAsInteger(2, Value))
  404. return ReturnError(TokStart, "invalid binary number");
  405. // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
  406. // suffixes on integer literals.
  407. SkipIgnoredIntegerSuffix(CurPtr);
  408. return intToken(Result, Value);
  409. }
  410. if ((*CurPtr == 'x') || (*CurPtr == 'X')) {
  411. ++CurPtr;
  412. const char *NumStart = CurPtr;
  413. while (isHexDigit(CurPtr[0]))
  414. ++CurPtr;
  415. // "0x.0p0" is valid, and "0x0p0" (but not "0xp0" for example, which will be
  416. // diagnosed by LexHexFloatLiteral).
  417. if (CurPtr[0] == '.' || CurPtr[0] == 'p' || CurPtr[0] == 'P')
  418. return LexHexFloatLiteral(NumStart == CurPtr);
  419. // Otherwise requires at least one hex digit.
  420. if (CurPtr == NumStart)
  421. return ReturnError(CurPtr-2, "invalid hexadecimal number");
  422. APInt Result(128, 0);
  423. if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
  424. return ReturnError(TokStart, "invalid hexadecimal number");
  425. // Consume the optional [hH].
  426. if (LexMasmIntegers && (*CurPtr == 'h' || *CurPtr == 'H'))
  427. ++CurPtr;
  428. // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
  429. // suffixes on integer literals.
  430. SkipIgnoredIntegerSuffix(CurPtr);
  431. return intToken(StringRef(TokStart, CurPtr - TokStart), Result);
  432. }
  433. // Either octal or hexadecimal.
  434. APInt Value(128, 0, true);
  435. unsigned Radix = doHexLookAhead(CurPtr, 8, LexMasmIntegers);
  436. StringRef Result(TokStart, CurPtr - TokStart);
  437. if (Result.getAsInteger(Radix, Value))
  438. return ReturnError(TokStart, "invalid " + radixName(Radix) + " number");
  439. // Consume the [hH].
  440. if (Radix == 16)
  441. ++CurPtr;
  442. // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
  443. // suffixes on integer literals.
  444. SkipIgnoredIntegerSuffix(CurPtr);
  445. return intToken(Result, Value);
  446. }
  447. /// LexSingleQuote: Integer: 'b'
  448. AsmToken AsmLexer::LexSingleQuote() {
  449. int CurChar = getNextChar();
  450. if (LexMasmStrings) {
  451. while (CurChar != EOF) {
  452. if (CurChar != '\'') {
  453. CurChar = getNextChar();
  454. } else if (peekNextChar() == '\'') {
  455. // In MASM single-quote strings, doubled single-quotes mean an escaped
  456. // single quote, so should be lexed in.
  457. getNextChar();
  458. CurChar = getNextChar();
  459. } else {
  460. break;
  461. }
  462. }
  463. if (CurChar == EOF)
  464. return ReturnError(TokStart, "unterminated string constant");
  465. return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
  466. }
  467. if (CurChar == '\\')
  468. CurChar = getNextChar();
  469. if (CurChar == EOF)
  470. return ReturnError(TokStart, "unterminated single quote");
  471. CurChar = getNextChar();
  472. if (CurChar != '\'')
  473. return ReturnError(TokStart, "single quote way too long");
  474. // The idea here being that 'c' is basically just an integral
  475. // constant.
  476. StringRef Res = StringRef(TokStart,CurPtr - TokStart);
  477. long long Value;
  478. if (Res.startswith("\'\\")) {
  479. char theChar = Res[2];
  480. switch (theChar) {
  481. default: Value = theChar; break;
  482. case '\'': Value = '\''; break;
  483. case 't': Value = '\t'; break;
  484. case 'n': Value = '\n'; break;
  485. case 'b': Value = '\b'; break;
  486. }
  487. } else
  488. Value = TokStart[1];
  489. return AsmToken(AsmToken::Integer, Res, Value);
  490. }
  491. /// LexQuote: String: "..."
  492. AsmToken AsmLexer::LexQuote() {
  493. int CurChar = getNextChar();
  494. if (LexMasmStrings) {
  495. while (CurChar != EOF) {
  496. if (CurChar != '"') {
  497. CurChar = getNextChar();
  498. } else if (peekNextChar() == '"') {
  499. // In MASM double-quoted strings, doubled double-quotes mean an escaped
  500. // double quote, so should be lexed in.
  501. getNextChar();
  502. CurChar = getNextChar();
  503. } else {
  504. break;
  505. }
  506. }
  507. if (CurChar == EOF)
  508. return ReturnError(TokStart, "unterminated string constant");
  509. return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
  510. }
  511. // TODO: does gas allow multiline string constants?
  512. while (CurChar != '"') {
  513. if (CurChar == '\\') {
  514. // Allow \", etc.
  515. CurChar = getNextChar();
  516. }
  517. if (CurChar == EOF)
  518. return ReturnError(TokStart, "unterminated string constant");
  519. CurChar = getNextChar();
  520. }
  521. return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
  522. }
  523. StringRef AsmLexer::LexUntilEndOfStatement() {
  524. TokStart = CurPtr;
  525. while (!isAtStartOfComment(CurPtr) && // Start of line comment.
  526. !isAtStatementSeparator(CurPtr) && // End of statement marker.
  527. *CurPtr != '\n' && *CurPtr != '\r' && CurPtr != CurBuf.end()) {
  528. ++CurPtr;
  529. }
  530. return StringRef(TokStart, CurPtr-TokStart);
  531. }
  532. StringRef AsmLexer::LexUntilEndOfLine() {
  533. TokStart = CurPtr;
  534. while (*CurPtr != '\n' && *CurPtr != '\r' && CurPtr != CurBuf.end()) {
  535. ++CurPtr;
  536. }
  537. return StringRef(TokStart, CurPtr-TokStart);
  538. }
  539. size_t AsmLexer::peekTokens(MutableArrayRef<AsmToken> Buf,
  540. bool ShouldSkipSpace) {
  541. SaveAndRestore<const char *> SavedTokenStart(TokStart);
  542. SaveAndRestore<const char *> SavedCurPtr(CurPtr);
  543. SaveAndRestore<bool> SavedAtStartOfLine(IsAtStartOfLine);
  544. SaveAndRestore<bool> SavedAtStartOfStatement(IsAtStartOfStatement);
  545. SaveAndRestore<bool> SavedSkipSpace(SkipSpace, ShouldSkipSpace);
  546. SaveAndRestore<bool> SavedIsPeeking(IsPeeking, true);
  547. std::string SavedErr = getErr();
  548. SMLoc SavedErrLoc = getErrLoc();
  549. size_t ReadCount;
  550. for (ReadCount = 0; ReadCount < Buf.size(); ++ReadCount) {
  551. AsmToken Token = LexToken();
  552. Buf[ReadCount] = Token;
  553. if (Token.is(AsmToken::Eof))
  554. break;
  555. }
  556. SetError(SavedErrLoc, SavedErr);
  557. return ReadCount;
  558. }
  559. bool AsmLexer::isAtStartOfComment(const char *Ptr) {
  560. StringRef CommentString = MAI.getCommentString();
  561. if (CommentString.size() == 1)
  562. return CommentString[0] == Ptr[0];
  563. // Allow # preprocessor commments also be counted as comments for "##" cases
  564. if (CommentString[1] == '#')
  565. return CommentString[0] == Ptr[0];
  566. return strncmp(Ptr, CommentString.data(), CommentString.size()) == 0;
  567. }
  568. bool AsmLexer::isAtStatementSeparator(const char *Ptr) {
  569. return strncmp(Ptr, MAI.getSeparatorString(),
  570. strlen(MAI.getSeparatorString())) == 0;
  571. }
  572. AsmToken AsmLexer::LexToken() {
  573. TokStart = CurPtr;
  574. // This always consumes at least one character.
  575. int CurChar = getNextChar();
  576. if (!IsPeeking && CurChar == '#' && IsAtStartOfStatement) {
  577. // If this starts with a '#', this may be a cpp
  578. // hash directive and otherwise a line comment.
  579. AsmToken TokenBuf[2];
  580. MutableArrayRef<AsmToken> Buf(TokenBuf, 2);
  581. size_t num = peekTokens(Buf, true);
  582. // There cannot be a space preceding this
  583. if (IsAtStartOfLine && num == 2 && TokenBuf[0].is(AsmToken::Integer) &&
  584. TokenBuf[1].is(AsmToken::String)) {
  585. CurPtr = TokStart; // reset curPtr;
  586. StringRef s = LexUntilEndOfLine();
  587. UnLex(TokenBuf[1]);
  588. UnLex(TokenBuf[0]);
  589. return AsmToken(AsmToken::HashDirective, s);
  590. }
  591. return LexLineComment();
  592. }
  593. if (isAtStartOfComment(TokStart))
  594. return LexLineComment();
  595. if (isAtStatementSeparator(TokStart)) {
  596. CurPtr += strlen(MAI.getSeparatorString()) - 1;
  597. IsAtStartOfLine = true;
  598. IsAtStartOfStatement = true;
  599. return AsmToken(AsmToken::EndOfStatement,
  600. StringRef(TokStart, strlen(MAI.getSeparatorString())));
  601. }
  602. // If we're missing a newline at EOF, make sure we still get an
  603. // EndOfStatement token before the Eof token.
  604. if (CurChar == EOF && !IsAtStartOfStatement && EndStatementAtEOF) {
  605. IsAtStartOfLine = true;
  606. IsAtStartOfStatement = true;
  607. return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 0));
  608. }
  609. IsAtStartOfLine = false;
  610. bool OldIsAtStartOfStatement = IsAtStartOfStatement;
  611. IsAtStartOfStatement = false;
  612. switch (CurChar) {
  613. default:
  614. if (MAI.doesAllowSymbolAtNameStart()) {
  615. // Handle Microsoft-style identifier: [a-zA-Z_$.@?][a-zA-Z0-9_$.@?]*
  616. if (!isDigit(CurChar) &&
  617. IsIdentifierChar(CurChar, MAI.doesAllowAtInName()))
  618. return LexIdentifier();
  619. } else {
  620. // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
  621. if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
  622. return LexIdentifier();
  623. }
  624. // Unknown character, emit an error.
  625. return ReturnError(TokStart, "invalid character in input");
  626. case EOF:
  627. if (EndStatementAtEOF) {
  628. IsAtStartOfLine = true;
  629. IsAtStartOfStatement = true;
  630. }
  631. return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
  632. case 0:
  633. case ' ':
  634. case '\t':
  635. IsAtStartOfStatement = OldIsAtStartOfStatement;
  636. while (*CurPtr == ' ' || *CurPtr == '\t')
  637. CurPtr++;
  638. if (SkipSpace)
  639. return LexToken(); // Ignore whitespace.
  640. else
  641. return AsmToken(AsmToken::Space, StringRef(TokStart, CurPtr - TokStart));
  642. case '\r': {
  643. IsAtStartOfLine = true;
  644. IsAtStartOfStatement = true;
  645. // If this is a CR followed by LF, treat that as one token.
  646. if (CurPtr != CurBuf.end() && *CurPtr == '\n')
  647. ++CurPtr;
  648. return AsmToken(AsmToken::EndOfStatement,
  649. StringRef(TokStart, CurPtr - TokStart));
  650. }
  651. case '\n':
  652. IsAtStartOfLine = true;
  653. IsAtStartOfStatement = true;
  654. return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
  655. case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
  656. case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
  657. case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
  658. case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
  659. case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
  660. case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
  661. case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
  662. case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
  663. case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
  664. case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
  665. case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
  666. case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
  667. case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
  668. case '\\': return AsmToken(AsmToken::BackSlash, StringRef(TokStart, 1));
  669. case '=':
  670. if (*CurPtr == '=') {
  671. ++CurPtr;
  672. return AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
  673. }
  674. return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
  675. case '-':
  676. if (*CurPtr == '>') {
  677. ++CurPtr;
  678. return AsmToken(AsmToken::MinusGreater, StringRef(TokStart, 2));
  679. }
  680. return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
  681. case '|':
  682. if (*CurPtr == '|') {
  683. ++CurPtr;
  684. return AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
  685. }
  686. return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
  687. case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
  688. case '&':
  689. if (*CurPtr == '&') {
  690. ++CurPtr;
  691. return AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
  692. }
  693. return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
  694. case '!':
  695. if (*CurPtr == '=') {
  696. ++CurPtr;
  697. return AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
  698. }
  699. return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
  700. case '%':
  701. if (MAI.hasMipsExpressions()) {
  702. AsmToken::TokenKind Operator;
  703. unsigned OperatorLength;
  704. std::tie(Operator, OperatorLength) =
  705. StringSwitch<std::pair<AsmToken::TokenKind, unsigned>>(
  706. StringRef(CurPtr))
  707. .StartsWith("call16", {AsmToken::PercentCall16, 7})
  708. .StartsWith("call_hi", {AsmToken::PercentCall_Hi, 8})
  709. .StartsWith("call_lo", {AsmToken::PercentCall_Lo, 8})
  710. .StartsWith("dtprel_hi", {AsmToken::PercentDtprel_Hi, 10})
  711. .StartsWith("dtprel_lo", {AsmToken::PercentDtprel_Lo, 10})
  712. .StartsWith("got_disp", {AsmToken::PercentGot_Disp, 9})
  713. .StartsWith("got_hi", {AsmToken::PercentGot_Hi, 7})
  714. .StartsWith("got_lo", {AsmToken::PercentGot_Lo, 7})
  715. .StartsWith("got_ofst", {AsmToken::PercentGot_Ofst, 9})
  716. .StartsWith("got_page", {AsmToken::PercentGot_Page, 9})
  717. .StartsWith("gottprel", {AsmToken::PercentGottprel, 9})
  718. .StartsWith("got", {AsmToken::PercentGot, 4})
  719. .StartsWith("gp_rel", {AsmToken::PercentGp_Rel, 7})
  720. .StartsWith("higher", {AsmToken::PercentHigher, 7})
  721. .StartsWith("highest", {AsmToken::PercentHighest, 8})
  722. .StartsWith("hi", {AsmToken::PercentHi, 3})
  723. .StartsWith("lo", {AsmToken::PercentLo, 3})
  724. .StartsWith("neg", {AsmToken::PercentNeg, 4})
  725. .StartsWith("pcrel_hi", {AsmToken::PercentPcrel_Hi, 9})
  726. .StartsWith("pcrel_lo", {AsmToken::PercentPcrel_Lo, 9})
  727. .StartsWith("tlsgd", {AsmToken::PercentTlsgd, 6})
  728. .StartsWith("tlsldm", {AsmToken::PercentTlsldm, 7})
  729. .StartsWith("tprel_hi", {AsmToken::PercentTprel_Hi, 9})
  730. .StartsWith("tprel_lo", {AsmToken::PercentTprel_Lo, 9})
  731. .Default({AsmToken::Percent, 1});
  732. if (Operator != AsmToken::Percent) {
  733. CurPtr += OperatorLength - 1;
  734. return AsmToken(Operator, StringRef(TokStart, OperatorLength));
  735. }
  736. }
  737. return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
  738. case '/':
  739. IsAtStartOfStatement = OldIsAtStartOfStatement;
  740. return LexSlash();
  741. case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
  742. case '\'': return LexSingleQuote();
  743. case '"': return LexQuote();
  744. case '0': case '1': case '2': case '3': case '4':
  745. case '5': case '6': case '7': case '8': case '9':
  746. return LexDigit();
  747. case '<':
  748. switch (*CurPtr) {
  749. case '<':
  750. ++CurPtr;
  751. return AsmToken(AsmToken::LessLess, StringRef(TokStart, 2));
  752. case '=':
  753. ++CurPtr;
  754. return AsmToken(AsmToken::LessEqual, StringRef(TokStart, 2));
  755. case '>':
  756. ++CurPtr;
  757. return AsmToken(AsmToken::LessGreater, StringRef(TokStart, 2));
  758. default:
  759. return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
  760. }
  761. case '>':
  762. switch (*CurPtr) {
  763. case '>':
  764. ++CurPtr;
  765. return AsmToken(AsmToken::GreaterGreater, StringRef(TokStart, 2));
  766. case '=':
  767. ++CurPtr;
  768. return AsmToken(AsmToken::GreaterEqual, StringRef(TokStart, 2));
  769. default:
  770. return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
  771. }
  772. // TODO: Quoted identifiers (objc methods etc)
  773. // local labels: [0-9][:]
  774. // Forward/backward labels: [0-9][fb]
  775. // Integers, fp constants, character constants.
  776. }
  777. }