MILexer.cpp 24 KB

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