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/StringExtras.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Twine.h"
25 using ErrorCallbackType
=
26 function_ref
<void(StringRef::iterator Loc
, const Twine
&)>;
28 /// This class provides a way to iterate and get characters from the source
31 const char *Ptr
= nullptr;
32 const char *End
= nullptr;
35 Cursor(std::nullopt_t
) {}
37 explicit Cursor(StringRef Str
) {
39 End
= Ptr
+ Str
.size();
42 bool isEOF() const { return Ptr
== End
; }
44 char peek(int I
= 0) const { return End
- Ptr
<= I
? 0 : Ptr
[I
]; }
46 void advance(unsigned I
= 1) { Ptr
+= I
; }
48 StringRef
remaining() const { return StringRef(Ptr
, End
- Ptr
); }
50 StringRef
upto(Cursor C
) const {
51 assert(C
.Ptr
>= Ptr
&& C
.Ptr
<= End
);
52 return StringRef(Ptr
, C
.Ptr
- Ptr
);
55 StringRef::iterator
location() const { return Ptr
; }
57 operator bool() const { return Ptr
!= nullptr; }
60 } // end anonymous namespace
62 MIToken
&MIToken::reset(TokenKind Kind
, StringRef Range
) {
68 MIToken
&MIToken::setStringValue(StringRef StrVal
) {
73 MIToken
&MIToken::setOwnedStringValue(std::string StrVal
) {
74 StringValueStorage
= std::move(StrVal
);
75 StringValue
= StringValueStorage
;
79 MIToken
&MIToken::setIntegerValue(APSInt IntVal
) {
80 this->IntVal
= std::move(IntVal
);
84 /// Skip the leading whitespace characters and return the updated cursor.
85 static Cursor
skipWhitespace(Cursor C
) {
86 while (isblank(C
.peek()))
91 static bool isNewlineChar(char C
) { return C
== '\n' || C
== '\r'; }
93 /// Skip a line comment and return the updated cursor.
94 static Cursor
skipComment(Cursor C
) {
97 while (!isNewlineChar(C
.peek()) && !C
.isEOF())
102 /// Machine operands can have comments, enclosed between /* and */.
103 /// This eats up all tokens, including /* and */.
104 static Cursor
skipMachineOperandComment(Cursor C
) {
105 if (C
.peek() != '/' || C
.peek(1) != '*')
108 while (C
.peek() != '*' || C
.peek(1) != '/')
116 /// Return true if the given character satisfies the following regular
117 /// expression: [-a-zA-Z$._0-9]
118 static bool isIdentifierChar(char C
) {
119 return isalpha(C
) || isdigit(C
) || C
== '_' || C
== '-' || C
== '.' ||
123 /// Unescapes the given string value.
125 /// Expects the string value to be quoted.
126 static std::string
unescapeQuotedString(StringRef Value
) {
127 assert(Value
.front() == '"' && Value
.back() == '"');
128 Cursor C
= Cursor(Value
.substr(1, Value
.size() - 2));
131 Str
.reserve(C
.remaining().size());
133 char Char
= C
.peek();
135 if (C
.peek(1) == '\\') {
136 // Two '\' become one
141 if (isxdigit(C
.peek(1)) && isxdigit(C
.peek(2))) {
142 Str
+= hexDigitValue(C
.peek(1)) * 16 + hexDigitValue(C
.peek(2));
153 /// Lex a string constant using the following regular expression: \"[^\"]*\"
154 static Cursor
lexStringConstant(Cursor C
, ErrorCallbackType ErrorCallback
) {
155 assert(C
.peek() == '"');
156 for (C
.advance(); C
.peek() != '"'; C
.advance()) {
157 if (C
.isEOF() || isNewlineChar(C
.peek())) {
160 "end of machine instruction reached before the closing '\"'");
168 static Cursor
lexName(Cursor C
, MIToken
&Token
, MIToken::TokenKind Type
,
169 unsigned PrefixLength
, ErrorCallbackType ErrorCallback
) {
171 C
.advance(PrefixLength
);
172 if (C
.peek() == '"') {
173 if (Cursor R
= lexStringConstant(C
, ErrorCallback
)) {
174 StringRef String
= Range
.upto(R
);
175 Token
.reset(Type
, String
)
176 .setOwnedStringValue(
177 unescapeQuotedString(String
.drop_front(PrefixLength
)));
180 Token
.reset(MIToken::Error
, Range
.remaining());
183 while (isIdentifierChar(C
.peek()))
185 Token
.reset(Type
, Range
.upto(C
))
186 .setStringValue(Range
.upto(C
).drop_front(PrefixLength
));
190 static MIToken::TokenKind
getIdentifierKind(StringRef Identifier
) {
191 return StringSwitch
<MIToken::TokenKind
>(Identifier
)
192 .Case("_", MIToken::underscore
)
193 .Case("implicit", MIToken::kw_implicit
)
194 .Case("implicit-def", MIToken::kw_implicit_define
)
195 .Case("def", MIToken::kw_def
)
196 .Case("dead", MIToken::kw_dead
)
197 .Case("killed", MIToken::kw_killed
)
198 .Case("undef", MIToken::kw_undef
)
199 .Case("internal", MIToken::kw_internal
)
200 .Case("early-clobber", MIToken::kw_early_clobber
)
201 .Case("debug-use", MIToken::kw_debug_use
)
202 .Case("renamable", MIToken::kw_renamable
)
203 .Case("tied-def", MIToken::kw_tied_def
)
204 .Case("frame-setup", MIToken::kw_frame_setup
)
205 .Case("frame-destroy", MIToken::kw_frame_destroy
)
206 .Case("nnan", MIToken::kw_nnan
)
207 .Case("ninf", MIToken::kw_ninf
)
208 .Case("nsz", MIToken::kw_nsz
)
209 .Case("arcp", MIToken::kw_arcp
)
210 .Case("contract", MIToken::kw_contract
)
211 .Case("afn", MIToken::kw_afn
)
212 .Case("reassoc", MIToken::kw_reassoc
)
213 .Case("nuw", MIToken::kw_nuw
)
214 .Case("nsw", MIToken::kw_nsw
)
215 .Case("exact", MIToken::kw_exact
)
216 .Case("nofpexcept", MIToken::kw_nofpexcept
)
217 .Case("unpredictable", MIToken::kw_unpredictable
)
218 .Case("debug-location", MIToken::kw_debug_location
)
219 .Case("debug-instr-number", MIToken::kw_debug_instr_number
)
220 .Case("dbg-instr-ref", MIToken::kw_dbg_instr_ref
)
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_basealign
)
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("landing-pad", MIToken::kw_landing_pad
)
263 .Case("inlineasm-br-indirect-target",
264 MIToken::kw_inlineasm_br_indirect_target
)
265 .Case("ehfunclet-entry", MIToken::kw_ehfunclet_entry
)
266 .Case("liveins", MIToken::kw_liveins
)
267 .Case("successors", MIToken::kw_successors
)
268 .Case("floatpred", MIToken::kw_floatpred
)
269 .Case("intpred", MIToken::kw_intpred
)
270 .Case("shufflemask", MIToken::kw_shufflemask
)
271 .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol
)
272 .Case("post-instr-symbol", MIToken::kw_post_instr_symbol
)
273 .Case("heap-alloc-marker", MIToken::kw_heap_alloc_marker
)
274 .Case("pcsections", MIToken::kw_pcsections
)
275 .Case("cfi-type", MIToken::kw_cfi_type
)
276 .Case("bbsections", MIToken::kw_bbsections
)
277 .Case("bb_id", MIToken::kw_bb_id
)
278 .Case("unknown-size", MIToken::kw_unknown_size
)
279 .Case("unknown-address", MIToken::kw_unknown_address
)
280 .Case("distinct", MIToken::kw_distinct
)
281 .Case("ir-block-address-taken", MIToken::kw_ir_block_address_taken
)
282 .Case("machine-block-address-taken",
283 MIToken::kw_machine_block_address_taken
)
284 .Case("call-frame-size", MIToken::kw_call_frame_size
)
285 .Case("noconvergent", MIToken::kw_noconvergent
)
286 .Default(MIToken::Identifier
);
289 static Cursor
maybeLexIdentifier(Cursor C
, MIToken
&Token
) {
290 if (!isalpha(C
.peek()) && C
.peek() != '_')
293 while (isIdentifierChar(C
.peek()))
295 auto Identifier
= Range
.upto(C
);
296 Token
.reset(getIdentifierKind(Identifier
), Identifier
)
297 .setStringValue(Identifier
);
301 static Cursor
maybeLexMachineBasicBlock(Cursor C
, MIToken
&Token
,
302 ErrorCallbackType ErrorCallback
) {
303 bool IsReference
= C
.remaining().startswith("%bb.");
304 if (!IsReference
&& !C
.remaining().startswith("bb."))
307 unsigned PrefixLength
= IsReference
? 4 : 3;
308 C
.advance(PrefixLength
); // Skip '%bb.' or 'bb.'
309 if (!isdigit(C
.peek())) {
310 Token
.reset(MIToken::Error
, C
.remaining());
311 ErrorCallback(C
.location(), "expected a number after '%bb.'");
314 auto NumberRange
= C
;
315 while (isdigit(C
.peek()))
317 StringRef Number
= NumberRange
.upto(C
);
318 unsigned StringOffset
= PrefixLength
+ Number
.size(); // Drop '%bb.<id>'
319 // TODO: The format bb.<id>.<irname> is supported only when it's not a
320 // reference. Once we deprecate the format where the irname shows up, we
321 // should only lex forward if it is a reference.
322 if (C
.peek() == '.') {
323 C
.advance(); // Skip '.'
325 while (isIdentifierChar(C
.peek()))
328 Token
.reset(IsReference
? MIToken::MachineBasicBlock
329 : MIToken::MachineBasicBlockLabel
,
331 .setIntegerValue(APSInt(Number
))
332 .setStringValue(Range
.upto(C
).drop_front(StringOffset
));
336 static Cursor
maybeLexIndex(Cursor C
, MIToken
&Token
, StringRef Rule
,
337 MIToken::TokenKind Kind
) {
338 if (!C
.remaining().startswith(Rule
) || !isdigit(C
.peek(Rule
.size())))
341 C
.advance(Rule
.size());
342 auto NumberRange
= C
;
343 while (isdigit(C
.peek()))
345 Token
.reset(Kind
, Range
.upto(C
)).setIntegerValue(APSInt(NumberRange
.upto(C
)));
349 static Cursor
maybeLexIndexAndName(Cursor C
, MIToken
&Token
, StringRef Rule
,
350 MIToken::TokenKind Kind
) {
351 if (!C
.remaining().startswith(Rule
) || !isdigit(C
.peek(Rule
.size())))
354 C
.advance(Rule
.size());
355 auto NumberRange
= C
;
356 while (isdigit(C
.peek()))
358 StringRef Number
= NumberRange
.upto(C
);
359 unsigned StringOffset
= Rule
.size() + Number
.size();
360 if (C
.peek() == '.') {
363 while (isIdentifierChar(C
.peek()))
366 Token
.reset(Kind
, Range
.upto(C
))
367 .setIntegerValue(APSInt(Number
))
368 .setStringValue(Range
.upto(C
).drop_front(StringOffset
));
372 static Cursor
maybeLexJumpTableIndex(Cursor C
, MIToken
&Token
) {
373 return maybeLexIndex(C
, Token
, "%jump-table.", MIToken::JumpTableIndex
);
376 static Cursor
maybeLexStackObject(Cursor C
, MIToken
&Token
) {
377 return maybeLexIndexAndName(C
, Token
, "%stack.", MIToken::StackObject
);
380 static Cursor
maybeLexFixedStackObject(Cursor C
, MIToken
&Token
) {
381 return maybeLexIndex(C
, Token
, "%fixed-stack.", MIToken::FixedStackObject
);
384 static Cursor
maybeLexConstantPoolItem(Cursor C
, MIToken
&Token
) {
385 return maybeLexIndex(C
, Token
, "%const.", MIToken::ConstantPoolItem
);
388 static Cursor
maybeLexSubRegisterIndex(Cursor C
, MIToken
&Token
,
389 ErrorCallbackType ErrorCallback
) {
390 const StringRef Rule
= "%subreg.";
391 if (!C
.remaining().startswith(Rule
))
393 return lexName(C
, Token
, MIToken::SubRegisterIndex
, Rule
.size(),
397 static Cursor
maybeLexIRBlock(Cursor C
, MIToken
&Token
,
398 ErrorCallbackType ErrorCallback
) {
399 const StringRef Rule
= "%ir-block.";
400 if (!C
.remaining().startswith(Rule
))
402 if (isdigit(C
.peek(Rule
.size())))
403 return maybeLexIndex(C
, Token
, Rule
, MIToken::IRBlock
);
404 return lexName(C
, Token
, MIToken::NamedIRBlock
, Rule
.size(), ErrorCallback
);
407 static Cursor
maybeLexIRValue(Cursor C
, MIToken
&Token
,
408 ErrorCallbackType ErrorCallback
) {
409 const StringRef Rule
= "%ir.";
410 if (!C
.remaining().startswith(Rule
))
412 if (isdigit(C
.peek(Rule
.size())))
413 return maybeLexIndex(C
, Token
, Rule
, MIToken::IRValue
);
414 return lexName(C
, Token
, MIToken::NamedIRValue
, Rule
.size(), ErrorCallback
);
417 static Cursor
maybeLexStringConstant(Cursor C
, MIToken
&Token
,
418 ErrorCallbackType ErrorCallback
) {
421 return lexName(C
, Token
, MIToken::StringConstant
, /*PrefixLength=*/0,
425 static Cursor
lexVirtualRegister(Cursor C
, MIToken
&Token
) {
427 C
.advance(); // Skip '%'
428 auto NumberRange
= C
;
429 while (isdigit(C
.peek()))
431 Token
.reset(MIToken::VirtualRegister
, Range
.upto(C
))
432 .setIntegerValue(APSInt(NumberRange
.upto(C
)));
436 /// Returns true for a character allowed in a register name.
437 static bool isRegisterChar(char C
) {
438 return isIdentifierChar(C
) && C
!= '.';
441 static Cursor
lexNamedVirtualRegister(Cursor C
, MIToken
&Token
) {
443 C
.advance(); // Skip '%'
444 while (isRegisterChar(C
.peek()))
446 Token
.reset(MIToken::NamedVirtualRegister
, Range
.upto(C
))
447 .setStringValue(Range
.upto(C
).drop_front(1)); // Drop the '%'
451 static Cursor
maybeLexRegister(Cursor C
, MIToken
&Token
,
452 ErrorCallbackType ErrorCallback
) {
453 if (C
.peek() != '%' && C
.peek() != '$')
456 if (C
.peek() == '%') {
457 if (isdigit(C
.peek(1)))
458 return lexVirtualRegister(C
, Token
);
460 if (isRegisterChar(C
.peek(1)))
461 return lexNamedVirtualRegister(C
, Token
);
466 assert(C
.peek() == '$');
468 C
.advance(); // Skip '$'
469 while (isRegisterChar(C
.peek()))
471 Token
.reset(MIToken::NamedRegister
, Range
.upto(C
))
472 .setStringValue(Range
.upto(C
).drop_front(1)); // Drop the '$'
476 static Cursor
maybeLexGlobalValue(Cursor C
, MIToken
&Token
,
477 ErrorCallbackType ErrorCallback
) {
480 if (!isdigit(C
.peek(1)))
481 return lexName(C
, Token
, MIToken::NamedGlobalValue
, /*PrefixLength=*/1,
484 C
.advance(1); // Skip the '@'
485 auto NumberRange
= C
;
486 while (isdigit(C
.peek()))
488 Token
.reset(MIToken::GlobalValue
, Range
.upto(C
))
489 .setIntegerValue(APSInt(NumberRange
.upto(C
)));
493 static Cursor
maybeLexExternalSymbol(Cursor C
, MIToken
&Token
,
494 ErrorCallbackType ErrorCallback
) {
497 return lexName(C
, Token
, MIToken::ExternalSymbol
, /*PrefixLength=*/1,
501 static Cursor
maybeLexMCSymbol(Cursor C
, MIToken
&Token
,
502 ErrorCallbackType ErrorCallback
) {
503 const StringRef Rule
= "<mcsymbol ";
504 if (!C
.remaining().startswith(Rule
))
507 C
.advance(Rule
.size());
509 // Try a simple unquoted name.
510 if (C
.peek() != '"') {
511 while (isIdentifierChar(C
.peek()))
513 StringRef String
= Start
.upto(C
).drop_front(Rule
.size());
514 if (C
.peek() != '>') {
515 ErrorCallback(C
.location(),
516 "expected the '<mcsymbol ...' to be closed by a '>'");
517 Token
.reset(MIToken::Error
, Start
.remaining());
522 Token
.reset(MIToken::MCSymbol
, Start
.upto(C
)).setStringValue(String
);
526 // Otherwise lex out a quoted name.
527 Cursor R
= lexStringConstant(C
, ErrorCallback
);
529 ErrorCallback(C
.location(),
530 "unable to parse quoted string from opening quote");
531 Token
.reset(MIToken::Error
, Start
.remaining());
534 StringRef String
= Start
.upto(R
).drop_front(Rule
.size());
535 if (R
.peek() != '>') {
536 ErrorCallback(R
.location(),
537 "expected the '<mcsymbol ...' to be closed by a '>'");
538 Token
.reset(MIToken::Error
, Start
.remaining());
543 Token
.reset(MIToken::MCSymbol
, Start
.upto(R
))
544 .setOwnedStringValue(unescapeQuotedString(String
));
548 static bool isValidHexFloatingPointPrefix(char C
) {
549 return C
== 'H' || C
== 'K' || C
== 'L' || C
== 'M' || C
== 'R';
552 static Cursor
lexFloatingPointLiteral(Cursor Range
, Cursor C
, MIToken
&Token
) {
554 // Skip over [0-9]*([eE][-+]?[0-9]+)?
555 while (isdigit(C
.peek()))
557 if ((C
.peek() == 'e' || C
.peek() == 'E') &&
558 (isdigit(C
.peek(1)) ||
559 ((C
.peek(1) == '-' || C
.peek(1) == '+') && isdigit(C
.peek(2))))) {
561 while (isdigit(C
.peek()))
564 Token
.reset(MIToken::FloatingPointLiteral
, Range
.upto(C
));
568 static Cursor
maybeLexHexadecimalLiteral(Cursor C
, MIToken
&Token
) {
569 if (C
.peek() != '0' || (C
.peek(1) != 'x' && C
.peek(1) != 'X'))
573 unsigned PrefLen
= 2;
574 if (isValidHexFloatingPointPrefix(C
.peek())) {
578 while (isxdigit(C
.peek()))
580 StringRef StrVal
= Range
.upto(C
);
581 if (StrVal
.size() <= PrefLen
)
584 Token
.reset(MIToken::HexLiteral
, Range
.upto(C
));
585 else // It must be 3, which means that there was a floating-point prefix.
586 Token
.reset(MIToken::FloatingPointLiteral
, Range
.upto(C
));
590 static Cursor
maybeLexNumericalLiteral(Cursor C
, MIToken
&Token
) {
591 if (!isdigit(C
.peek()) && (C
.peek() != '-' || !isdigit(C
.peek(1))))
595 while (isdigit(C
.peek()))
598 return lexFloatingPointLiteral(Range
, C
, Token
);
599 StringRef StrVal
= Range
.upto(C
);
600 Token
.reset(MIToken::IntegerLiteral
, StrVal
).setIntegerValue(APSInt(StrVal
));
604 static MIToken::TokenKind
getMetadataKeywordKind(StringRef Identifier
) {
605 return StringSwitch
<MIToken::TokenKind
>(Identifier
)
606 .Case("!tbaa", MIToken::md_tbaa
)
607 .Case("!alias.scope", MIToken::md_alias_scope
)
608 .Case("!noalias", MIToken::md_noalias
)
609 .Case("!range", MIToken::md_range
)
610 .Case("!DIExpression", MIToken::md_diexpr
)
611 .Case("!DILocation", MIToken::md_dilocation
)
612 .Default(MIToken::Error
);
615 static Cursor
maybeLexExclaim(Cursor C
, MIToken
&Token
,
616 ErrorCallbackType ErrorCallback
) {
621 if (isdigit(C
.peek()) || !isIdentifierChar(C
.peek())) {
622 Token
.reset(MIToken::exclaim
, Range
.upto(C
));
625 while (isIdentifierChar(C
.peek()))
627 StringRef StrVal
= Range
.upto(C
);
628 Token
.reset(getMetadataKeywordKind(StrVal
), StrVal
);
630 ErrorCallback(Token
.location(),
631 "use of unknown metadata keyword '" + StrVal
+ "'");
635 static MIToken::TokenKind
symbolToken(char C
) {
638 return MIToken::comma
;
642 return MIToken::equal
;
644 return MIToken::colon
;
646 return MIToken::lparen
;
648 return MIToken::rparen
;
650 return MIToken::lbrace
;
652 return MIToken::rbrace
;
654 return MIToken::plus
;
656 return MIToken::minus
;
658 return MIToken::less
;
660 return MIToken::greater
;
662 return MIToken::Error
;
666 static Cursor
maybeLexSymbol(Cursor C
, MIToken
&Token
) {
667 MIToken::TokenKind Kind
;
669 if (C
.peek() == ':' && C
.peek(1) == ':') {
670 Kind
= MIToken::coloncolon
;
673 Kind
= symbolToken(C
.peek());
674 if (Kind
== MIToken::Error
)
678 Token
.reset(Kind
, Range
.upto(C
));
682 static Cursor
maybeLexNewline(Cursor C
, MIToken
&Token
) {
683 if (!isNewlineChar(C
.peek()))
687 Token
.reset(MIToken::Newline
, Range
.upto(C
));
691 static Cursor
maybeLexEscapedIRValue(Cursor C
, MIToken
&Token
,
692 ErrorCallbackType ErrorCallback
) {
698 while (C
.peek() != '`') {
699 if (C
.isEOF() || isNewlineChar(C
.peek())) {
702 "end of machine instruction reached before the closing '`'");
703 Token
.reset(MIToken::Error
, Range
.remaining());
708 StringRef Value
= StrRange
.upto(C
);
710 Token
.reset(MIToken::QuotedIRValue
, Range
.upto(C
)).setStringValue(Value
);
714 StringRef
llvm::lexMIToken(StringRef Source
, MIToken
&Token
,
715 ErrorCallbackType ErrorCallback
) {
716 auto C
= skipComment(skipWhitespace(Cursor(Source
)));
718 Token
.reset(MIToken::Eof
, C
.remaining());
719 return C
.remaining();
722 C
= skipMachineOperandComment(C
);
724 if (Cursor R
= maybeLexMachineBasicBlock(C
, Token
, ErrorCallback
))
725 return R
.remaining();
726 if (Cursor R
= maybeLexIdentifier(C
, Token
))
727 return R
.remaining();
728 if (Cursor R
= maybeLexJumpTableIndex(C
, Token
))
729 return R
.remaining();
730 if (Cursor R
= maybeLexStackObject(C
, Token
))
731 return R
.remaining();
732 if (Cursor R
= maybeLexFixedStackObject(C
, Token
))
733 return R
.remaining();
734 if (Cursor R
= maybeLexConstantPoolItem(C
, Token
))
735 return R
.remaining();
736 if (Cursor R
= maybeLexSubRegisterIndex(C
, Token
, ErrorCallback
))
737 return R
.remaining();
738 if (Cursor R
= maybeLexIRBlock(C
, Token
, ErrorCallback
))
739 return R
.remaining();
740 if (Cursor R
= maybeLexIRValue(C
, Token
, ErrorCallback
))
741 return R
.remaining();
742 if (Cursor R
= maybeLexRegister(C
, Token
, ErrorCallback
))
743 return R
.remaining();
744 if (Cursor R
= maybeLexGlobalValue(C
, Token
, ErrorCallback
))
745 return R
.remaining();
746 if (Cursor R
= maybeLexExternalSymbol(C
, Token
, ErrorCallback
))
747 return R
.remaining();
748 if (Cursor R
= maybeLexMCSymbol(C
, Token
, ErrorCallback
))
749 return R
.remaining();
750 if (Cursor R
= maybeLexHexadecimalLiteral(C
, Token
))
751 return R
.remaining();
752 if (Cursor R
= maybeLexNumericalLiteral(C
, Token
))
753 return R
.remaining();
754 if (Cursor R
= maybeLexExclaim(C
, Token
, ErrorCallback
))
755 return R
.remaining();
756 if (Cursor R
= maybeLexSymbol(C
, Token
))
757 return R
.remaining();
758 if (Cursor R
= maybeLexNewline(C
, Token
))
759 return R
.remaining();
760 if (Cursor R
= maybeLexEscapedIRValue(C
, Token
, ErrorCallback
))
761 return R
.remaining();
762 if (Cursor R
= maybeLexStringConstant(C
, Token
, ErrorCallback
))
763 return R
.remaining();
765 Token
.reset(MIToken::Error
, C
.remaining());
766 ErrorCallback(C
.location(),
767 Twine("unexpected character '") + Twine(C
.peek()) + "'");
768 return C
.remaining();