MILexer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. //===- MILexer.cpp - Machine instructions lexer implementation ------------===//
  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 file implements the lexing of machine instructions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "MILexer.h"
  13. #include "llvm/ADT/APSInt.h"
  14. #include "llvm/ADT/None.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/StringSwitch.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/Twine.h"
  20. #include <algorithm>
  21. #include <cassert>
  22. #include <cctype>
  23. #include <string>
  24. using namespace llvm;
  25. namespace {
  26. using ErrorCallbackType =
  27. function_ref<void(StringRef::iterator Loc, const Twine &)>;
  28. /// This class provides a way to iterate and get characters from the source
  29. /// string.
  30. class Cursor {
  31. const char *Ptr = nullptr;
  32. const char *End = nullptr;
  33. public:
  34. Cursor(NoneType) {}
  35. explicit Cursor(StringRef Str) {
  36. Ptr = Str.data();
  37. End = Ptr + Str.size();
  38. }
  39. bool isEOF() const { return Ptr == End; }
  40. char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
  41. void advance(unsigned I = 1) { Ptr += I; }
  42. StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
  43. StringRef upto(Cursor C) const {
  44. assert(C.Ptr >= Ptr && C.Ptr <= End);
  45. return StringRef(Ptr, C.Ptr - Ptr);
  46. }
  47. StringRef::iterator location() const { return Ptr; }
  48. operator bool() const { return Ptr != nullptr; }
  49. };
  50. } // end anonymous namespace
  51. MIToken &MIToken::reset(TokenKind Kind, StringRef Range) {
  52. this->Kind = Kind;
  53. this->Range = Range;
  54. return *this;
  55. }
  56. MIToken &MIToken::setStringValue(StringRef StrVal) {
  57. StringValue = StrVal;
  58. return *this;
  59. }
  60. MIToken &MIToken::setOwnedStringValue(std::string StrVal) {
  61. StringValueStorage = std::move(StrVal);
  62. StringValue = StringValueStorage;
  63. return *this;
  64. }
  65. MIToken &MIToken::setIntegerValue(APSInt IntVal) {
  66. this->IntVal = std::move(IntVal);
  67. return *this;
  68. }
  69. /// Skip the leading whitespace characters and return the updated cursor.
  70. static Cursor skipWhitespace(Cursor C) {
  71. while (isblank(C.peek()))
  72. C.advance();
  73. return C;
  74. }
  75. static bool isNewlineChar(char C) { return C == '\n' || C == '\r'; }
  76. /// Skip a line comment and return the updated cursor.
  77. static Cursor skipComment(Cursor C) {
  78. if (C.peek() != ';')
  79. return C;
  80. while (!isNewlineChar(C.peek()) && !C.isEOF())
  81. C.advance();
  82. return C;
  83. }
  84. /// Return true if the given character satisfies the following regular
  85. /// expression: [-a-zA-Z$._0-9]
  86. static bool isIdentifierChar(char C) {
  87. return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' ||
  88. C == '$';
  89. }
  90. /// Unescapes the given string value.
  91. ///
  92. /// Expects the string value to be quoted.
  93. static std::string unescapeQuotedString(StringRef Value) {
  94. assert(Value.front() == '"' && Value.back() == '"');
  95. Cursor C = Cursor(Value.substr(1, Value.size() - 2));
  96. std::string Str;
  97. Str.reserve(C.remaining().size());
  98. while (!C.isEOF()) {
  99. char Char = C.peek();
  100. if (Char == '\\') {
  101. if (C.peek(1) == '\\') {
  102. // Two '\' become one
  103. Str += '\\';
  104. C.advance(2);
  105. continue;
  106. }
  107. if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
  108. Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
  109. C.advance(3);
  110. continue;
  111. }
  112. }
  113. Str += Char;
  114. C.advance();
  115. }
  116. return Str;
  117. }
  118. /// Lex a string constant using the following regular expression: \"[^\"]*\"
  119. static Cursor lexStringConstant(Cursor C, ErrorCallbackType ErrorCallback) {
  120. assert(C.peek() == '"');
  121. for (C.advance(); C.peek() != '"'; C.advance()) {
  122. if (C.isEOF() || isNewlineChar(C.peek())) {
  123. ErrorCallback(
  124. C.location(),
  125. "end of machine instruction reached before the closing '\"'");
  126. return None;
  127. }
  128. }
  129. C.advance();
  130. return C;
  131. }
  132. static Cursor lexName(Cursor C, MIToken &Token, MIToken::TokenKind Type,
  133. unsigned PrefixLength, ErrorCallbackType ErrorCallback) {
  134. auto Range = C;
  135. C.advance(PrefixLength);
  136. if (C.peek() == '"') {
  137. if (Cursor R = lexStringConstant(C, ErrorCallback)) {
  138. StringRef String = Range.upto(R);
  139. Token.reset(Type, String)
  140. .setOwnedStringValue(
  141. unescapeQuotedString(String.drop_front(PrefixLength)));
  142. return R;
  143. }
  144. Token.reset(MIToken::Error, Range.remaining());
  145. return Range;
  146. }
  147. while (isIdentifierChar(C.peek()))
  148. C.advance();
  149. Token.reset(Type, Range.upto(C))
  150. .setStringValue(Range.upto(C).drop_front(PrefixLength));
  151. return C;
  152. }
  153. static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
  154. return StringSwitch<MIToken::TokenKind>(Identifier)
  155. .Case("_", MIToken::underscore)
  156. .Case("implicit", MIToken::kw_implicit)
  157. .Case("implicit-def", MIToken::kw_implicit_define)
  158. .Case("def", MIToken::kw_def)
  159. .Case("dead", MIToken::kw_dead)
  160. .Case("killed", MIToken::kw_killed)
  161. .Case("undef", MIToken::kw_undef)
  162. .Case("internal", MIToken::kw_internal)
  163. .Case("early-clobber", MIToken::kw_early_clobber)
  164. .Case("debug-use", MIToken::kw_debug_use)
  165. .Case("renamable", MIToken::kw_renamable)
  166. .Case("tied-def", MIToken::kw_tied_def)
  167. .Case("frame-setup", MIToken::kw_frame_setup)
  168. .Case("frame-destroy", MIToken::kw_frame_destroy)
  169. .Case("nnan", MIToken::kw_nnan)
  170. .Case("ninf", MIToken::kw_ninf)
  171. .Case("nsz", MIToken::kw_nsz)
  172. .Case("arcp", MIToken::kw_arcp)
  173. .Case("contract", MIToken::kw_contract)
  174. .Case("afn", MIToken::kw_afn)
  175. .Case("reassoc", MIToken::kw_reassoc)
  176. .Case("nuw" , MIToken::kw_nuw)
  177. .Case("nsw" , MIToken::kw_nsw)
  178. .Case("exact" , MIToken::kw_exact)
  179. .Case("fpexcept", MIToken::kw_fpexcept)
  180. .Case("debug-location", MIToken::kw_debug_location)
  181. .Case("same_value", MIToken::kw_cfi_same_value)
  182. .Case("offset", MIToken::kw_cfi_offset)
  183. .Case("rel_offset", MIToken::kw_cfi_rel_offset)
  184. .Case("def_cfa_register", MIToken::kw_cfi_def_cfa_register)
  185. .Case("def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
  186. .Case("adjust_cfa_offset", MIToken::kw_cfi_adjust_cfa_offset)
  187. .Case("escape", MIToken::kw_cfi_escape)
  188. .Case("def_cfa", MIToken::kw_cfi_def_cfa)
  189. .Case("remember_state", MIToken::kw_cfi_remember_state)
  190. .Case("restore", MIToken::kw_cfi_restore)
  191. .Case("restore_state", MIToken::kw_cfi_restore_state)
  192. .Case("undefined", MIToken::kw_cfi_undefined)
  193. .Case("register", MIToken::kw_cfi_register)
  194. .Case("window_save", MIToken::kw_cfi_window_save)
  195. .Case("negate_ra_sign_state", MIToken::kw_cfi_aarch64_negate_ra_sign_state)
  196. .Case("blockaddress", MIToken::kw_blockaddress)
  197. .Case("intrinsic", MIToken::kw_intrinsic)
  198. .Case("target-index", MIToken::kw_target_index)
  199. .Case("half", MIToken::kw_half)
  200. .Case("float", MIToken::kw_float)
  201. .Case("double", MIToken::kw_double)
  202. .Case("x86_fp80", MIToken::kw_x86_fp80)
  203. .Case("fp128", MIToken::kw_fp128)
  204. .Case("ppc_fp128", MIToken::kw_ppc_fp128)
  205. .Case("target-flags", MIToken::kw_target_flags)
  206. .Case("volatile", MIToken::kw_volatile)
  207. .Case("non-temporal", MIToken::kw_non_temporal)
  208. .Case("dereferenceable", MIToken::kw_dereferenceable)
  209. .Case("invariant", MIToken::kw_invariant)
  210. .Case("align", MIToken::kw_align)
  211. .Case("addrspace", MIToken::kw_addrspace)
  212. .Case("stack", MIToken::kw_stack)
  213. .Case("got", MIToken::kw_got)
  214. .Case("jump-table", MIToken::kw_jump_table)
  215. .Case("constant-pool", MIToken::kw_constant_pool)
  216. .Case("call-entry", MIToken::kw_call_entry)
  217. .Case("liveout", MIToken::kw_liveout)
  218. .Case("address-taken", MIToken::kw_address_taken)
  219. .Case("landing-pad", MIToken::kw_landing_pad)
  220. .Case("liveins", MIToken::kw_liveins)
  221. .Case("successors", MIToken::kw_successors)
  222. .Case("floatpred", MIToken::kw_floatpred)
  223. .Case("intpred", MIToken::kw_intpred)
  224. .Case("shufflemask", MIToken::kw_shufflemask)
  225. .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol)
  226. .Case("post-instr-symbol", MIToken::kw_post_instr_symbol)
  227. .Case("unknown-size", MIToken::kw_unknown_size)
  228. .Default(MIToken::Identifier);
  229. }
  230. static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
  231. if (!isalpha(C.peek()) && C.peek() != '_')
  232. return None;
  233. auto Range = C;
  234. while (isIdentifierChar(C.peek()))
  235. C.advance();
  236. auto Identifier = Range.upto(C);
  237. Token.reset(getIdentifierKind(Identifier), Identifier)
  238. .setStringValue(Identifier);
  239. return C;
  240. }
  241. static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
  242. ErrorCallbackType ErrorCallback) {
  243. bool IsReference = C.remaining().startswith("%bb.");
  244. if (!IsReference && !C.remaining().startswith("bb."))
  245. return None;
  246. auto Range = C;
  247. unsigned PrefixLength = IsReference ? 4 : 3;
  248. C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
  249. if (!isdigit(C.peek())) {
  250. Token.reset(MIToken::Error, C.remaining());
  251. ErrorCallback(C.location(), "expected a number after '%bb.'");
  252. return C;
  253. }
  254. auto NumberRange = C;
  255. while (isdigit(C.peek()))
  256. C.advance();
  257. StringRef Number = NumberRange.upto(C);
  258. unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>'
  259. // TODO: The format bb.<id>.<irname> is supported only when it's not a
  260. // reference. Once we deprecate the format where the irname shows up, we
  261. // should only lex forward if it is a reference.
  262. if (C.peek() == '.') {
  263. C.advance(); // Skip '.'
  264. ++StringOffset;
  265. while (isIdentifierChar(C.peek()))
  266. C.advance();
  267. }
  268. Token.reset(IsReference ? MIToken::MachineBasicBlock
  269. : MIToken::MachineBasicBlockLabel,
  270. Range.upto(C))
  271. .setIntegerValue(APSInt(Number))
  272. .setStringValue(Range.upto(C).drop_front(StringOffset));
  273. return C;
  274. }
  275. static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
  276. MIToken::TokenKind Kind) {
  277. if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
  278. return None;
  279. auto Range = C;
  280. C.advance(Rule.size());
  281. auto NumberRange = C;
  282. while (isdigit(C.peek()))
  283. C.advance();
  284. Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
  285. return C;
  286. }
  287. static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
  288. MIToken::TokenKind Kind) {
  289. if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
  290. return None;
  291. auto Range = C;
  292. C.advance(Rule.size());
  293. auto NumberRange = C;
  294. while (isdigit(C.peek()))
  295. C.advance();
  296. StringRef Number = NumberRange.upto(C);
  297. unsigned StringOffset = Rule.size() + Number.size();
  298. if (C.peek() == '.') {
  299. C.advance();
  300. ++StringOffset;
  301. while (isIdentifierChar(C.peek()))
  302. C.advance();
  303. }
  304. Token.reset(Kind, Range.upto(C))
  305. .setIntegerValue(APSInt(Number))
  306. .setStringValue(Range.upto(C).drop_front(StringOffset));
  307. return C;
  308. }
  309. static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
  310. return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
  311. }
  312. static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
  313. return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
  314. }
  315. static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
  316. return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
  317. }
  318. static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
  319. return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
  320. }
  321. static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
  322. ErrorCallbackType ErrorCallback) {
  323. const StringRef Rule = "%subreg.";
  324. if (!C.remaining().startswith(Rule))
  325. return None;
  326. return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
  327. ErrorCallback);
  328. }
  329. static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
  330. ErrorCallbackType ErrorCallback) {
  331. const StringRef Rule = "%ir-block.";
  332. if (!C.remaining().startswith(Rule))
  333. return None;
  334. if (isdigit(C.peek(Rule.size())))
  335. return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
  336. return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
  337. }
  338. static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
  339. ErrorCallbackType ErrorCallback) {
  340. const StringRef Rule = "%ir.";
  341. if (!C.remaining().startswith(Rule))
  342. return None;
  343. if (isdigit(C.peek(Rule.size())))
  344. return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
  345. return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
  346. }
  347. static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
  348. ErrorCallbackType ErrorCallback) {
  349. if (C.peek() != '"')
  350. return None;
  351. return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
  352. ErrorCallback);
  353. }
  354. static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
  355. auto Range = C;
  356. C.advance(); // Skip '%'
  357. auto NumberRange = C;
  358. while (isdigit(C.peek()))
  359. C.advance();
  360. Token.reset(MIToken::VirtualRegister, Range.upto(C))
  361. .setIntegerValue(APSInt(NumberRange.upto(C)));
  362. return C;
  363. }
  364. /// Returns true for a character allowed in a register name.
  365. static bool isRegisterChar(char C) {
  366. return isIdentifierChar(C) && C != '.';
  367. }
  368. static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
  369. Cursor Range = C;
  370. C.advance(); // Skip '%'
  371. while (isRegisterChar(C.peek()))
  372. C.advance();
  373. Token.reset(MIToken::NamedVirtualRegister, Range.upto(C))
  374. .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
  375. return C;
  376. }
  377. static Cursor maybeLexRegister(Cursor C, MIToken &Token,
  378. ErrorCallbackType ErrorCallback) {
  379. if (C.peek() != '%' && C.peek() != '$')
  380. return None;
  381. if (C.peek() == '%') {
  382. if (isdigit(C.peek(1)))
  383. return lexVirtualRegister(C, Token);
  384. if (isRegisterChar(C.peek(1)))
  385. return lexNamedVirtualRegister(C, Token);
  386. return None;
  387. }
  388. assert(C.peek() == '$');
  389. auto Range = C;
  390. C.advance(); // Skip '$'
  391. while (isRegisterChar(C.peek()))
  392. C.advance();
  393. Token.reset(MIToken::NamedRegister, Range.upto(C))
  394. .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
  395. return C;
  396. }
  397. static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
  398. ErrorCallbackType ErrorCallback) {
  399. if (C.peek() != '@')
  400. return None;
  401. if (!isdigit(C.peek(1)))
  402. return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
  403. ErrorCallback);
  404. auto Range = C;
  405. C.advance(1); // Skip the '@'
  406. auto NumberRange = C;
  407. while (isdigit(C.peek()))
  408. C.advance();
  409. Token.reset(MIToken::GlobalValue, Range.upto(C))
  410. .setIntegerValue(APSInt(NumberRange.upto(C)));
  411. return C;
  412. }
  413. static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
  414. ErrorCallbackType ErrorCallback) {
  415. if (C.peek() != '&')
  416. return None;
  417. return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
  418. ErrorCallback);
  419. }
  420. static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
  421. ErrorCallbackType ErrorCallback) {
  422. const StringRef Rule = "<mcsymbol ";
  423. if (!C.remaining().startswith(Rule))
  424. return None;
  425. auto Start = C;
  426. C.advance(Rule.size());
  427. // Try a simple unquoted name.
  428. if (C.peek() != '"') {
  429. while (isIdentifierChar(C.peek()))
  430. C.advance();
  431. StringRef String = Start.upto(C).drop_front(Rule.size());
  432. if (C.peek() != '>') {
  433. ErrorCallback(C.location(),
  434. "expected the '<mcsymbol ...' to be closed by a '>'");
  435. Token.reset(MIToken::Error, Start.remaining());
  436. return Start;
  437. }
  438. C.advance();
  439. Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
  440. return C;
  441. }
  442. // Otherwise lex out a quoted name.
  443. Cursor R = lexStringConstant(C, ErrorCallback);
  444. if (!R) {
  445. ErrorCallback(C.location(),
  446. "unable to parse quoted string from opening quote");
  447. Token.reset(MIToken::Error, Start.remaining());
  448. return Start;
  449. }
  450. StringRef String = Start.upto(R).drop_front(Rule.size());
  451. if (R.peek() != '>') {
  452. ErrorCallback(R.location(),
  453. "expected the '<mcsymbol ...' to be closed by a '>'");
  454. Token.reset(MIToken::Error, Start.remaining());
  455. return Start;
  456. }
  457. R.advance();
  458. Token.reset(MIToken::MCSymbol, Start.upto(R))
  459. .setOwnedStringValue(unescapeQuotedString(String));
  460. return R;
  461. }
  462. static bool isValidHexFloatingPointPrefix(char C) {
  463. return C == 'H' || C == 'K' || C == 'L' || C == 'M';
  464. }
  465. static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
  466. C.advance();
  467. // Skip over [0-9]*([eE][-+]?[0-9]+)?
  468. while (isdigit(C.peek()))
  469. C.advance();
  470. if ((C.peek() == 'e' || C.peek() == 'E') &&
  471. (isdigit(C.peek(1)) ||
  472. ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
  473. C.advance(2);
  474. while (isdigit(C.peek()))
  475. C.advance();
  476. }
  477. Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
  478. return C;
  479. }
  480. static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
  481. if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
  482. return None;
  483. Cursor Range = C;
  484. C.advance(2);
  485. unsigned PrefLen = 2;
  486. if (isValidHexFloatingPointPrefix(C.peek())) {
  487. C.advance();
  488. PrefLen++;
  489. }
  490. while (isxdigit(C.peek()))
  491. C.advance();
  492. StringRef StrVal = Range.upto(C);
  493. if (StrVal.size() <= PrefLen)
  494. return None;
  495. if (PrefLen == 2)
  496. Token.reset(MIToken::HexLiteral, Range.upto(C));
  497. else // It must be 3, which means that there was a floating-point prefix.
  498. Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
  499. return C;
  500. }
  501. static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
  502. if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
  503. return None;
  504. auto Range = C;
  505. C.advance();
  506. while (isdigit(C.peek()))
  507. C.advance();
  508. if (C.peek() == '.')
  509. return lexFloatingPointLiteral(Range, C, Token);
  510. StringRef StrVal = Range.upto(C);
  511. Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
  512. return C;
  513. }
  514. static MIToken::TokenKind getMetadataKeywordKind(StringRef Identifier) {
  515. return StringSwitch<MIToken::TokenKind>(Identifier)
  516. .Case("!tbaa", MIToken::md_tbaa)
  517. .Case("!alias.scope", MIToken::md_alias_scope)
  518. .Case("!noalias", MIToken::md_noalias)
  519. .Case("!range", MIToken::md_range)
  520. .Case("!DIExpression", MIToken::md_diexpr)
  521. .Case("!DILocation", MIToken::md_dilocation)
  522. .Default(MIToken::Error);
  523. }
  524. static Cursor maybeLexExlaim(Cursor C, MIToken &Token,
  525. ErrorCallbackType ErrorCallback) {
  526. if (C.peek() != '!')
  527. return None;
  528. auto Range = C;
  529. C.advance(1);
  530. if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
  531. Token.reset(MIToken::exclaim, Range.upto(C));
  532. return C;
  533. }
  534. while (isIdentifierChar(C.peek()))
  535. C.advance();
  536. StringRef StrVal = Range.upto(C);
  537. Token.reset(getMetadataKeywordKind(StrVal), StrVal);
  538. if (Token.isError())
  539. ErrorCallback(Token.location(),
  540. "use of unknown metadata keyword '" + StrVal + "'");
  541. return C;
  542. }
  543. static MIToken::TokenKind symbolToken(char C) {
  544. switch (C) {
  545. case ',':
  546. return MIToken::comma;
  547. case '.':
  548. return MIToken::dot;
  549. case '=':
  550. return MIToken::equal;
  551. case ':':
  552. return MIToken::colon;
  553. case '(':
  554. return MIToken::lparen;
  555. case ')':
  556. return MIToken::rparen;
  557. case '{':
  558. return MIToken::lbrace;
  559. case '}':
  560. return MIToken::rbrace;
  561. case '+':
  562. return MIToken::plus;
  563. case '-':
  564. return MIToken::minus;
  565. case '<':
  566. return MIToken::less;
  567. case '>':
  568. return MIToken::greater;
  569. default:
  570. return MIToken::Error;
  571. }
  572. }
  573. static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
  574. MIToken::TokenKind Kind;
  575. unsigned Length = 1;
  576. if (C.peek() == ':' && C.peek(1) == ':') {
  577. Kind = MIToken::coloncolon;
  578. Length = 2;
  579. } else
  580. Kind = symbolToken(C.peek());
  581. if (Kind == MIToken::Error)
  582. return None;
  583. auto Range = C;
  584. C.advance(Length);
  585. Token.reset(Kind, Range.upto(C));
  586. return C;
  587. }
  588. static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
  589. if (!isNewlineChar(C.peek()))
  590. return None;
  591. auto Range = C;
  592. C.advance();
  593. Token.reset(MIToken::Newline, Range.upto(C));
  594. return C;
  595. }
  596. static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
  597. ErrorCallbackType ErrorCallback) {
  598. if (C.peek() != '`')
  599. return None;
  600. auto Range = C;
  601. C.advance();
  602. auto StrRange = C;
  603. while (C.peek() != '`') {
  604. if (C.isEOF() || isNewlineChar(C.peek())) {
  605. ErrorCallback(
  606. C.location(),
  607. "end of machine instruction reached before the closing '`'");
  608. Token.reset(MIToken::Error, Range.remaining());
  609. return C;
  610. }
  611. C.advance();
  612. }
  613. StringRef Value = StrRange.upto(C);
  614. C.advance();
  615. Token.reset(MIToken::QuotedIRValue, Range.upto(C)).setStringValue(Value);
  616. return C;
  617. }
  618. StringRef llvm::lexMIToken(StringRef Source, MIToken &Token,
  619. ErrorCallbackType ErrorCallback) {
  620. auto C = skipComment(skipWhitespace(Cursor(Source)));
  621. if (C.isEOF()) {
  622. Token.reset(MIToken::Eof, C.remaining());
  623. return C.remaining();
  624. }
  625. if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
  626. return R.remaining();
  627. if (Cursor R = maybeLexIdentifier(C, Token))
  628. return R.remaining();
  629. if (Cursor R = maybeLexJumpTableIndex(C, Token))
  630. return R.remaining();
  631. if (Cursor R = maybeLexStackObject(C, Token))
  632. return R.remaining();
  633. if (Cursor R = maybeLexFixedStackObject(C, Token))
  634. return R.remaining();
  635. if (Cursor R = maybeLexConstantPoolItem(C, Token))
  636. return R.remaining();
  637. if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
  638. return R.remaining();
  639. if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
  640. return R.remaining();
  641. if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
  642. return R.remaining();
  643. if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
  644. return R.remaining();
  645. if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
  646. return R.remaining();
  647. if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
  648. return R.remaining();
  649. if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
  650. return R.remaining();
  651. if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
  652. return R.remaining();
  653. if (Cursor R = maybeLexNumericalLiteral(C, Token))
  654. return R.remaining();
  655. if (Cursor R = maybeLexExlaim(C, Token, ErrorCallback))
  656. return R.remaining();
  657. if (Cursor R = maybeLexSymbol(C, Token))
  658. return R.remaining();
  659. if (Cursor R = maybeLexNewline(C, Token))
  660. return R.remaining();
  661. if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
  662. return R.remaining();
  663. if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
  664. return R.remaining();
  665. Token.reset(MIToken::Error, C.remaining());
  666. ErrorCallback(C.location(),
  667. Twine("unexpected character '") + Twine(C.peek()) + "'");
  668. return C.remaining();
  669. }