1 //===- MILexer.cpp - Machine instructions lexer implementation ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the lexing of machine instructions.
11 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/None.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/ADT/Twine.h"
27 using ErrorCallbackType
=
28 function_ref
<void(StringRef::iterator Loc
, const Twine
&)>;
30 /// This class provides a way to iterate and get characters from the source
33 const char *Ptr
= nullptr;
34 const char *End
= nullptr;
39 explicit Cursor(StringRef Str
) {
41 End
= Ptr
+ Str
.size();
44 bool isEOF() const { return Ptr
== End
; }
46 char peek(int I
= 0) const { return End
- Ptr
<= I
? 0 : Ptr
[I
]; }
48 void advance(unsigned I
= 1) { Ptr
+= I
; }
50 StringRef
remaining() const { return StringRef(Ptr
, End
- Ptr
); }
52 StringRef
upto(Cursor C
) const {
53 assert(C
.Ptr
>= Ptr
&& C
.Ptr
<= End
);
54 return StringRef(Ptr
, C
.Ptr
- Ptr
);
57 StringRef::iterator
location() const { return Ptr
; }
59 operator bool() const { return Ptr
!= nullptr; }
62 } // end anonymous namespace
64 MIToken
&MIToken::reset(TokenKind Kind
, StringRef Range
) {
70 MIToken
&MIToken::setStringValue(StringRef StrVal
) {
75 MIToken
&MIToken::setOwnedStringValue(std::string StrVal
) {
76 StringValueStorage
= std::move(StrVal
);
77 StringValue
= StringValueStorage
;
81 MIToken
&MIToken::setIntegerValue(APSInt IntVal
) {
82 this->IntVal
= std::move(IntVal
);
86 /// Skip the leading whitespace characters and return the updated cursor.
87 static Cursor
skipWhitespace(Cursor C
) {
88 while (isblank(C
.peek()))
93 static bool isNewlineChar(char C
) { return C
== '\n' || C
== '\r'; }
95 /// Skip a line comment and return the updated cursor.
96 static Cursor
skipComment(Cursor C
) {
99 while (!isNewlineChar(C
.peek()) && !C
.isEOF())
104 /// Machine operands can have comments, enclosed between /* and */.
105 /// This eats up all tokens, including /* and */.
106 static Cursor
skipMachineOperandComment(Cursor C
) {
107 if (C
.peek() != '/' || C
.peek(1) != '*')
110 while (C
.peek() != '*' || C
.peek(1) != '/')
118 /// Return true if the given character satisfies the following regular
119 /// expression: [-a-zA-Z$._0-9]
120 static bool isIdentifierChar(char C
) {
121 return isalpha(C
) || isdigit(C
) || C
== '_' || C
== '-' || C
== '.' ||
125 /// Unescapes the given string value.
127 /// Expects the string value to be quoted.
128 static std::string
unescapeQuotedString(StringRef Value
) {
129 assert(Value
.front() == '"' && Value
.back() == '"');
130 Cursor C
= Cursor(Value
.substr(1, Value
.size() - 2));
133 Str
.reserve(C
.remaining().size());
135 char Char
= C
.peek();
137 if (C
.peek(1) == '\\') {
138 // Two '\' become one
143 if (isxdigit(C
.peek(1)) && isxdigit(C
.peek(2))) {
144 Str
+= hexDigitValue(C
.peek(1)) * 16 + hexDigitValue(C
.peek(2));
155 /// Lex a string constant using the following regular expression: \"[^\"]*\"
156 static Cursor
lexStringConstant(Cursor C
, ErrorCallbackType ErrorCallback
) {
157 assert(C
.peek() == '"');
158 for (C
.advance(); C
.peek() != '"'; C
.advance()) {
159 if (C
.isEOF() || isNewlineChar(C
.peek())) {
162 "end of machine instruction reached before the closing '\"'");
170 static Cursor
lexName(Cursor C
, MIToken
&Token
, MIToken::TokenKind Type
,
171 unsigned PrefixLength
, ErrorCallbackType ErrorCallback
) {
173 C
.advance(PrefixLength
);
174 if (C
.peek() == '"') {
175 if (Cursor R
= lexStringConstant(C
, ErrorCallback
)) {
176 StringRef String
= Range
.upto(R
);
177 Token
.reset(Type
, String
)
178 .setOwnedStringValue(
179 unescapeQuotedString(String
.drop_front(PrefixLength
)));
182 Token
.reset(MIToken::Error
, Range
.remaining());
185 while (isIdentifierChar(C
.peek()))
187 Token
.reset(Type
, Range
.upto(C
))
188 .setStringValue(Range
.upto(C
).drop_front(PrefixLength
));
192 static MIToken::TokenKind
getIdentifierKind(StringRef Identifier
) {
193 return StringSwitch
<MIToken::TokenKind
>(Identifier
)
194 .Case("_", MIToken::underscore
)
195 .Case("implicit", MIToken::kw_implicit
)
196 .Case("implicit-def", MIToken::kw_implicit_define
)
197 .Case("def", MIToken::kw_def
)
198 .Case("dead", MIToken::kw_dead
)
199 .Case("killed", MIToken::kw_killed
)
200 .Case("undef", MIToken::kw_undef
)
201 .Case("internal", MIToken::kw_internal
)
202 .Case("early-clobber", MIToken::kw_early_clobber
)
203 .Case("debug-use", MIToken::kw_debug_use
)
204 .Case("renamable", MIToken::kw_renamable
)
205 .Case("tied-def", MIToken::kw_tied_def
)
206 .Case("frame-setup", MIToken::kw_frame_setup
)
207 .Case("frame-destroy", MIToken::kw_frame_destroy
)
208 .Case("nnan", MIToken::kw_nnan
)
209 .Case("ninf", MIToken::kw_ninf
)
210 .Case("nsz", MIToken::kw_nsz
)
211 .Case("arcp", MIToken::kw_arcp
)
212 .Case("contract", MIToken::kw_contract
)
213 .Case("afn", MIToken::kw_afn
)
214 .Case("reassoc", MIToken::kw_reassoc
)
215 .Case("nuw", MIToken::kw_nuw
)
216 .Case("nsw", MIToken::kw_nsw
)
217 .Case("exact", MIToken::kw_exact
)
218 .Case("nofpexcept", MIToken::kw_nofpexcept
)
219 .Case("debug-location", MIToken::kw_debug_location
)
220 .Case("debug-instr-number", MIToken::kw_debug_instr_number
)
221 .Case("same_value", MIToken::kw_cfi_same_value
)
222 .Case("offset", MIToken::kw_cfi_offset
)
223 .Case("rel_offset", MIToken::kw_cfi_rel_offset
)
224 .Case("def_cfa_register", MIToken::kw_cfi_def_cfa_register
)
225 .Case("def_cfa_offset", MIToken::kw_cfi_def_cfa_offset
)
226 .Case("adjust_cfa_offset", MIToken::kw_cfi_adjust_cfa_offset
)
227 .Case("escape", MIToken::kw_cfi_escape
)
228 .Case("def_cfa", MIToken::kw_cfi_def_cfa
)
229 .Case("llvm_def_aspace_cfa", MIToken::kw_cfi_llvm_def_aspace_cfa
)
230 .Case("remember_state", MIToken::kw_cfi_remember_state
)
231 .Case("restore", MIToken::kw_cfi_restore
)
232 .Case("restore_state", MIToken::kw_cfi_restore_state
)
233 .Case("undefined", MIToken::kw_cfi_undefined
)
234 .Case("register", MIToken::kw_cfi_register
)
235 .Case("window_save", MIToken::kw_cfi_window_save
)
236 .Case("negate_ra_sign_state",
237 MIToken::kw_cfi_aarch64_negate_ra_sign_state
)
238 .Case("blockaddress", MIToken::kw_blockaddress
)
239 .Case("intrinsic", MIToken::kw_intrinsic
)
240 .Case("target-index", MIToken::kw_target_index
)
241 .Case("half", MIToken::kw_half
)
242 .Case("float", MIToken::kw_float
)
243 .Case("double", MIToken::kw_double
)
244 .Case("x86_fp80", MIToken::kw_x86_fp80
)
245 .Case("fp128", MIToken::kw_fp128
)
246 .Case("ppc_fp128", MIToken::kw_ppc_fp128
)
247 .Case("target-flags", MIToken::kw_target_flags
)
248 .Case("volatile", MIToken::kw_volatile
)
249 .Case("non-temporal", MIToken::kw_non_temporal
)
250 .Case("dereferenceable", MIToken::kw_dereferenceable
)
251 .Case("invariant", MIToken::kw_invariant
)
252 .Case("align", MIToken::kw_align
)
253 .Case("basealign", MIToken::kw_align
)
254 .Case("addrspace", MIToken::kw_addrspace
)
255 .Case("stack", MIToken::kw_stack
)
256 .Case("got", MIToken::kw_got
)
257 .Case("jump-table", MIToken::kw_jump_table
)
258 .Case("constant-pool", MIToken::kw_constant_pool
)
259 .Case("call-entry", MIToken::kw_call_entry
)
260 .Case("custom", MIToken::kw_custom
)
261 .Case("liveout", MIToken::kw_liveout
)
262 .Case("address-taken", MIToken::kw_address_taken
)
263 .Case("landing-pad", MIToken::kw_landing_pad
)
264 .Case("ehfunclet-entry", MIToken::kw_ehfunclet_entry
)
265 .Case("liveins", MIToken::kw_liveins
)
266 .Case("successors", MIToken::kw_successors
)
267 .Case("floatpred", MIToken::kw_floatpred
)
268 .Case("intpred", MIToken::kw_intpred
)
269 .Case("shufflemask", MIToken::kw_shufflemask
)
270 .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol
)
271 .Case("post-instr-symbol", MIToken::kw_post_instr_symbol
)
272 .Case("heap-alloc-marker", MIToken::kw_heap_alloc_marker
)
273 .Case("bbsections", MIToken::kw_bbsections
)
274 .Case("unknown-size", MIToken::kw_unknown_size
)
275 .Case("unknown-address", MIToken::kw_unknown_address
)
276 .Case("distinct", MIToken::kw_distinct
)
277 .Default(MIToken::Identifier
);
280 static Cursor
maybeLexIdentifier(Cursor C
, MIToken
&Token
) {
281 if (!isalpha(C
.peek()) && C
.peek() != '_')
284 while (isIdentifierChar(C
.peek()))
286 auto Identifier
= Range
.upto(C
);
287 Token
.reset(getIdentifierKind(Identifier
), Identifier
)
288 .setStringValue(Identifier
);
292 static Cursor
maybeLexMachineBasicBlock(Cursor C
, MIToken
&Token
,
293 ErrorCallbackType ErrorCallback
) {
294 bool IsReference
= C
.remaining().startswith("%bb.");
295 if (!IsReference
&& !C
.remaining().startswith("bb."))
298 unsigned PrefixLength
= IsReference
? 4 : 3;
299 C
.advance(PrefixLength
); // Skip '%bb.' or 'bb.'
300 if (!isdigit(C
.peek())) {
301 Token
.reset(MIToken::Error
, C
.remaining());
302 ErrorCallback(C
.location(), "expected a number after '%bb.'");
305 auto NumberRange
= C
;
306 while (isdigit(C
.peek()))
308 StringRef Number
= NumberRange
.upto(C
);
309 unsigned StringOffset
= PrefixLength
+ Number
.size(); // Drop '%bb.<id>'
310 // TODO: The format bb.<id>.<irname> is supported only when it's not a
311 // reference. Once we deprecate the format where the irname shows up, we
312 // should only lex forward if it is a reference.
313 if (C
.peek() == '.') {
314 C
.advance(); // Skip '.'
316 while (isIdentifierChar(C
.peek()))
319 Token
.reset(IsReference
? MIToken::MachineBasicBlock
320 : MIToken::MachineBasicBlockLabel
,
322 .setIntegerValue(APSInt(Number
))
323 .setStringValue(Range
.upto(C
).drop_front(StringOffset
));
327 static Cursor
maybeLexIndex(Cursor C
, MIToken
&Token
, StringRef Rule
,
328 MIToken::TokenKind Kind
) {
329 if (!C
.remaining().startswith(Rule
) || !isdigit(C
.peek(Rule
.size())))
332 C
.advance(Rule
.size());
333 auto NumberRange
= C
;
334 while (isdigit(C
.peek()))
336 Token
.reset(Kind
, Range
.upto(C
)).setIntegerValue(APSInt(NumberRange
.upto(C
)));
340 static Cursor
maybeLexIndexAndName(Cursor C
, MIToken
&Token
, StringRef Rule
,
341 MIToken::TokenKind Kind
) {
342 if (!C
.remaining().startswith(Rule
) || !isdigit(C
.peek(Rule
.size())))
345 C
.advance(Rule
.size());
346 auto NumberRange
= C
;
347 while (isdigit(C
.peek()))
349 StringRef Number
= NumberRange
.upto(C
);
350 unsigned StringOffset
= Rule
.size() + Number
.size();
351 if (C
.peek() == '.') {
354 while (isIdentifierChar(C
.peek()))
357 Token
.reset(Kind
, Range
.upto(C
))
358 .setIntegerValue(APSInt(Number
))
359 .setStringValue(Range
.upto(C
).drop_front(StringOffset
));
363 static Cursor
maybeLexJumpTableIndex(Cursor C
, MIToken
&Token
) {
364 return maybeLexIndex(C
, Token
, "%jump-table.", MIToken::JumpTableIndex
);
367 static Cursor
maybeLexStackObject(Cursor C
, MIToken
&Token
) {
368 return maybeLexIndexAndName(C
, Token
, "%stack.", MIToken::StackObject
);
371 static Cursor
maybeLexFixedStackObject(Cursor C
, MIToken
&Token
) {
372 return maybeLexIndex(C
, Token
, "%fixed-stack.", MIToken::FixedStackObject
);
375 static Cursor
maybeLexConstantPoolItem(Cursor C
, MIToken
&Token
) {
376 return maybeLexIndex(C
, Token
, "%const.", MIToken::ConstantPoolItem
);
379 static Cursor
maybeLexSubRegisterIndex(Cursor C
, MIToken
&Token
,
380 ErrorCallbackType ErrorCallback
) {
381 const StringRef Rule
= "%subreg.";
382 if (!C
.remaining().startswith(Rule
))
384 return lexName(C
, Token
, MIToken::SubRegisterIndex
, Rule
.size(),
388 static Cursor
maybeLexIRBlock(Cursor C
, MIToken
&Token
,
389 ErrorCallbackType ErrorCallback
) {
390 const StringRef Rule
= "%ir-block.";
391 if (!C
.remaining().startswith(Rule
))
393 if (isdigit(C
.peek(Rule
.size())))
394 return maybeLexIndex(C
, Token
, Rule
, MIToken::IRBlock
);
395 return lexName(C
, Token
, MIToken::NamedIRBlock
, Rule
.size(), ErrorCallback
);
398 static Cursor
maybeLexIRValue(Cursor C
, MIToken
&Token
,
399 ErrorCallbackType ErrorCallback
) {
400 const StringRef Rule
= "%ir.";
401 if (!C
.remaining().startswith(Rule
))
403 if (isdigit(C
.peek(Rule
.size())))
404 return maybeLexIndex(C
, Token
, Rule
, MIToken::IRValue
);
405 return lexName(C
, Token
, MIToken::NamedIRValue
, Rule
.size(), ErrorCallback
);
408 static Cursor
maybeLexStringConstant(Cursor C
, MIToken
&Token
,
409 ErrorCallbackType ErrorCallback
) {
412 return lexName(C
, Token
, MIToken::StringConstant
, /*PrefixLength=*/0,
416 static Cursor
lexVirtualRegister(Cursor C
, MIToken
&Token
) {
418 C
.advance(); // Skip '%'
419 auto NumberRange
= C
;
420 while (isdigit(C
.peek()))
422 Token
.reset(MIToken::VirtualRegister
, Range
.upto(C
))
423 .setIntegerValue(APSInt(NumberRange
.upto(C
)));
427 /// Returns true for a character allowed in a register name.
428 static bool isRegisterChar(char C
) {
429 return isIdentifierChar(C
) && C
!= '.';
432 static Cursor
lexNamedVirtualRegister(Cursor C
, MIToken
&Token
) {
434 C
.advance(); // Skip '%'
435 while (isRegisterChar(C
.peek()))
437 Token
.reset(MIToken::NamedVirtualRegister
, Range
.upto(C
))
438 .setStringValue(Range
.upto(C
).drop_front(1)); // Drop the '%'
442 static Cursor
maybeLexRegister(Cursor C
, MIToken
&Token
,
443 ErrorCallbackType ErrorCallback
) {
444 if (C
.peek() != '%' && C
.peek() != '$')
447 if (C
.peek() == '%') {
448 if (isdigit(C
.peek(1)))
449 return lexVirtualRegister(C
, Token
);
451 if (isRegisterChar(C
.peek(1)))
452 return lexNamedVirtualRegister(C
, Token
);
457 assert(C
.peek() == '$');
459 C
.advance(); // Skip '$'
460 while (isRegisterChar(C
.peek()))
462 Token
.reset(MIToken::NamedRegister
, Range
.upto(C
))
463 .setStringValue(Range
.upto(C
).drop_front(1)); // Drop the '$'
467 static Cursor
maybeLexGlobalValue(Cursor C
, MIToken
&Token
,
468 ErrorCallbackType ErrorCallback
) {
471 if (!isdigit(C
.peek(1)))
472 return lexName(C
, Token
, MIToken::NamedGlobalValue
, /*PrefixLength=*/1,
475 C
.advance(1); // Skip the '@'
476 auto NumberRange
= C
;
477 while (isdigit(C
.peek()))
479 Token
.reset(MIToken::GlobalValue
, Range
.upto(C
))
480 .setIntegerValue(APSInt(NumberRange
.upto(C
)));
484 static Cursor
maybeLexExternalSymbol(Cursor C
, MIToken
&Token
,
485 ErrorCallbackType ErrorCallback
) {
488 return lexName(C
, Token
, MIToken::ExternalSymbol
, /*PrefixLength=*/1,
492 static Cursor
maybeLexMCSymbol(Cursor C
, MIToken
&Token
,
493 ErrorCallbackType ErrorCallback
) {
494 const StringRef Rule
= "<mcsymbol ";
495 if (!C
.remaining().startswith(Rule
))
498 C
.advance(Rule
.size());
500 // Try a simple unquoted name.
501 if (C
.peek() != '"') {
502 while (isIdentifierChar(C
.peek()))
504 StringRef String
= Start
.upto(C
).drop_front(Rule
.size());
505 if (C
.peek() != '>') {
506 ErrorCallback(C
.location(),
507 "expected the '<mcsymbol ...' to be closed by a '>'");
508 Token
.reset(MIToken::Error
, Start
.remaining());
513 Token
.reset(MIToken::MCSymbol
, Start
.upto(C
)).setStringValue(String
);
517 // Otherwise lex out a quoted name.
518 Cursor R
= lexStringConstant(C
, ErrorCallback
);
520 ErrorCallback(C
.location(),
521 "unable to parse quoted string from opening quote");
522 Token
.reset(MIToken::Error
, Start
.remaining());
525 StringRef String
= Start
.upto(R
).drop_front(Rule
.size());
526 if (R
.peek() != '>') {
527 ErrorCallback(R
.location(),
528 "expected the '<mcsymbol ...' to be closed by a '>'");
529 Token
.reset(MIToken::Error
, Start
.remaining());
534 Token
.reset(MIToken::MCSymbol
, Start
.upto(R
))
535 .setOwnedStringValue(unescapeQuotedString(String
));
539 static bool isValidHexFloatingPointPrefix(char C
) {
540 return C
== 'H' || C
== 'K' || C
== 'L' || C
== 'M' || C
== 'R';
543 static Cursor
lexFloatingPointLiteral(Cursor Range
, Cursor C
, MIToken
&Token
) {
545 // Skip over [0-9]*([eE][-+]?[0-9]+)?
546 while (isdigit(C
.peek()))
548 if ((C
.peek() == 'e' || C
.peek() == 'E') &&
549 (isdigit(C
.peek(1)) ||
550 ((C
.peek(1) == '-' || C
.peek(1) == '+') && isdigit(C
.peek(2))))) {
552 while (isdigit(C
.peek()))
555 Token
.reset(MIToken::FloatingPointLiteral
, Range
.upto(C
));
559 static Cursor
maybeLexHexadecimalLiteral(Cursor C
, MIToken
&Token
) {
560 if (C
.peek() != '0' || (C
.peek(1) != 'x' && C
.peek(1) != 'X'))
564 unsigned PrefLen
= 2;
565 if (isValidHexFloatingPointPrefix(C
.peek())) {
569 while (isxdigit(C
.peek()))
571 StringRef StrVal
= Range
.upto(C
);
572 if (StrVal
.size() <= PrefLen
)
575 Token
.reset(MIToken::HexLiteral
, Range
.upto(C
));
576 else // It must be 3, which means that there was a floating-point prefix.
577 Token
.reset(MIToken::FloatingPointLiteral
, Range
.upto(C
));
581 static Cursor
maybeLexNumericalLiteral(Cursor C
, MIToken
&Token
) {
582 if (!isdigit(C
.peek()) && (C
.peek() != '-' || !isdigit(C
.peek(1))))
586 while (isdigit(C
.peek()))
589 return lexFloatingPointLiteral(Range
, C
, Token
);
590 StringRef StrVal
= Range
.upto(C
);
591 Token
.reset(MIToken::IntegerLiteral
, StrVal
).setIntegerValue(APSInt(StrVal
));
595 static MIToken::TokenKind
getMetadataKeywordKind(StringRef Identifier
) {
596 return StringSwitch
<MIToken::TokenKind
>(Identifier
)
597 .Case("!tbaa", MIToken::md_tbaa
)
598 .Case("!alias.scope", MIToken::md_alias_scope
)
599 .Case("!noalias", MIToken::md_noalias
)
600 .Case("!range", MIToken::md_range
)
601 .Case("!DIExpression", MIToken::md_diexpr
)
602 .Case("!DILocation", MIToken::md_dilocation
)
603 .Default(MIToken::Error
);
606 static Cursor
maybeLexExclaim(Cursor C
, MIToken
&Token
,
607 ErrorCallbackType ErrorCallback
) {
612 if (isdigit(C
.peek()) || !isIdentifierChar(C
.peek())) {
613 Token
.reset(MIToken::exclaim
, Range
.upto(C
));
616 while (isIdentifierChar(C
.peek()))
618 StringRef StrVal
= Range
.upto(C
);
619 Token
.reset(getMetadataKeywordKind(StrVal
), StrVal
);
621 ErrorCallback(Token
.location(),
622 "use of unknown metadata keyword '" + StrVal
+ "'");
626 static MIToken::TokenKind
symbolToken(char C
) {
629 return MIToken::comma
;
633 return MIToken::equal
;
635 return MIToken::colon
;
637 return MIToken::lparen
;
639 return MIToken::rparen
;
641 return MIToken::lbrace
;
643 return MIToken::rbrace
;
645 return MIToken::plus
;
647 return MIToken::minus
;
649 return MIToken::less
;
651 return MIToken::greater
;
653 return MIToken::Error
;
657 static Cursor
maybeLexSymbol(Cursor C
, MIToken
&Token
) {
658 MIToken::TokenKind Kind
;
660 if (C
.peek() == ':' && C
.peek(1) == ':') {
661 Kind
= MIToken::coloncolon
;
664 Kind
= symbolToken(C
.peek());
665 if (Kind
== MIToken::Error
)
669 Token
.reset(Kind
, Range
.upto(C
));
673 static Cursor
maybeLexNewline(Cursor C
, MIToken
&Token
) {
674 if (!isNewlineChar(C
.peek()))
678 Token
.reset(MIToken::Newline
, Range
.upto(C
));
682 static Cursor
maybeLexEscapedIRValue(Cursor C
, MIToken
&Token
,
683 ErrorCallbackType ErrorCallback
) {
689 while (C
.peek() != '`') {
690 if (C
.isEOF() || isNewlineChar(C
.peek())) {
693 "end of machine instruction reached before the closing '`'");
694 Token
.reset(MIToken::Error
, Range
.remaining());
699 StringRef Value
= StrRange
.upto(C
);
701 Token
.reset(MIToken::QuotedIRValue
, Range
.upto(C
)).setStringValue(Value
);
705 StringRef
llvm::lexMIToken(StringRef Source
, MIToken
&Token
,
706 ErrorCallbackType ErrorCallback
) {
707 auto C
= skipComment(skipWhitespace(Cursor(Source
)));
709 Token
.reset(MIToken::Eof
, C
.remaining());
710 return C
.remaining();
713 C
= skipMachineOperandComment(C
);
715 if (Cursor R
= maybeLexMachineBasicBlock(C
, Token
, ErrorCallback
))
716 return R
.remaining();
717 if (Cursor R
= maybeLexIdentifier(C
, Token
))
718 return R
.remaining();
719 if (Cursor R
= maybeLexJumpTableIndex(C
, Token
))
720 return R
.remaining();
721 if (Cursor R
= maybeLexStackObject(C
, Token
))
722 return R
.remaining();
723 if (Cursor R
= maybeLexFixedStackObject(C
, Token
))
724 return R
.remaining();
725 if (Cursor R
= maybeLexConstantPoolItem(C
, Token
))
726 return R
.remaining();
727 if (Cursor R
= maybeLexSubRegisterIndex(C
, Token
, ErrorCallback
))
728 return R
.remaining();
729 if (Cursor R
= maybeLexIRBlock(C
, Token
, ErrorCallback
))
730 return R
.remaining();
731 if (Cursor R
= maybeLexIRValue(C
, Token
, ErrorCallback
))
732 return R
.remaining();
733 if (Cursor R
= maybeLexRegister(C
, Token
, ErrorCallback
))
734 return R
.remaining();
735 if (Cursor R
= maybeLexGlobalValue(C
, Token
, ErrorCallback
))
736 return R
.remaining();
737 if (Cursor R
= maybeLexExternalSymbol(C
, Token
, ErrorCallback
))
738 return R
.remaining();
739 if (Cursor R
= maybeLexMCSymbol(C
, Token
, ErrorCallback
))
740 return R
.remaining();
741 if (Cursor R
= maybeLexHexadecimalLiteral(C
, Token
))
742 return R
.remaining();
743 if (Cursor R
= maybeLexNumericalLiteral(C
, Token
))
744 return R
.remaining();
745 if (Cursor R
= maybeLexExclaim(C
, Token
, ErrorCallback
))
746 return R
.remaining();
747 if (Cursor R
= maybeLexSymbol(C
, Token
))
748 return R
.remaining();
749 if (Cursor R
= maybeLexNewline(C
, Token
))
750 return R
.remaining();
751 if (Cursor R
= maybeLexEscapedIRValue(C
, Token
, ErrorCallback
))
752 return R
.remaining();
753 if (Cursor R
= maybeLexStringConstant(C
, Token
, ErrorCallback
))
754 return R
.remaining();
756 Token
.reset(MIToken::Error
, C
.remaining());
757 ErrorCallback(C
.location(),
758 Twine("unexpected character '") + Twine(C
.peek()) + "'");
759 return C
.remaining();