LLVM 19.0.0git
MILexer.cpp
Go to the documentation of this file.
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
13#include "MILexer.h"
16#include "llvm/ADT/Twine.h"
17#include <cassert>
18#include <cctype>
19#include <string>
20
21using namespace llvm;
22
23namespace {
24
26 function_ref<void(StringRef::iterator Loc, const Twine &)>;
27
28/// This class provides a way to iterate and get characters from the source
29/// string.
30class Cursor {
31 const char *Ptr = nullptr;
32 const char *End = nullptr;
33
34public:
35 Cursor(std::nullopt_t) {}
36
37 explicit Cursor(StringRef Str) {
38 Ptr = Str.data();
39 End = Ptr + Str.size();
40 }
41
42 bool isEOF() const { return Ptr == End; }
43
44 char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
45
46 void advance(unsigned I = 1) { Ptr += I; }
47
48 StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
49
50 StringRef upto(Cursor C) const {
51 assert(C.Ptr >= Ptr && C.Ptr <= End);
52 return StringRef(Ptr, C.Ptr - Ptr);
53 }
54
55 StringRef::iterator location() const { return Ptr; }
56
57 operator bool() const { return Ptr != nullptr; }
58};
59
60} // end anonymous namespace
61
63 this->Kind = Kind;
64 this->Range = Range;
65 return *this;
66}
67
69 StringValue = StrVal;
70 return *this;
71}
72
74 StringValueStorage = std::move(StrVal);
75 StringValue = StringValueStorage;
76 return *this;
77}
78
80 this->IntVal = std::move(IntVal);
81 return *this;
82}
83
84/// Skip the leading whitespace characters and return the updated cursor.
85static Cursor skipWhitespace(Cursor C) {
86 while (isblank(C.peek()))
87 C.advance();
88 return C;
89}
90
91static bool isNewlineChar(char C) { return C == '\n' || C == '\r'; }
92
93/// Skip a line comment and return the updated cursor.
94static Cursor skipComment(Cursor C) {
95 if (C.peek() != ';')
96 return C;
97 while (!isNewlineChar(C.peek()) && !C.isEOF())
98 C.advance();
99 return C;
100}
101
102/// Machine operands can have comments, enclosed between /* and */.
103/// This eats up all tokens, including /* and */.
104static Cursor skipMachineOperandComment(Cursor C) {
105 if (C.peek() != '/' || C.peek(1) != '*')
106 return C;
107
108 while (C.peek() != '*' || C.peek(1) != '/')
109 C.advance();
110
111 C.advance();
112 C.advance();
113 return C;
114}
115
116/// Return true if the given character satisfies the following regular
117/// expression: [-a-zA-Z$._0-9]
118static bool isIdentifierChar(char C) {
119 return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' ||
120 C == '$';
121}
122
123/// Unescapes the given string value.
124///
125/// Expects the string value to be quoted.
127 assert(Value.front() == '"' && Value.back() == '"');
128 Cursor C = Cursor(Value.substr(1, Value.size() - 2));
129
130 std::string Str;
131 Str.reserve(C.remaining().size());
132 while (!C.isEOF()) {
133 char Char = C.peek();
134 if (Char == '\\') {
135 if (C.peek(1) == '\\') {
136 // Two '\' become one
137 Str += '\\';
138 C.advance(2);
139 continue;
140 }
141 if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
142 Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
143 C.advance(3);
144 continue;
145 }
146 }
147 Str += Char;
148 C.advance();
149 }
150 return Str;
151}
152
153/// Lex a string constant using the following regular expression: \"[^\"]*\"
154static 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())) {
158 ErrorCallback(
159 C.location(),
160 "end of machine instruction reached before the closing '\"'");
161 return std::nullopt;
162 }
163 }
164 C.advance();
165 return C;
166}
167
168static Cursor lexName(Cursor C, MIToken &Token, MIToken::TokenKind Type,
169 unsigned PrefixLength, ErrorCallbackType ErrorCallback) {
170 auto Range = C;
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)
177 unescapeQuotedString(String.drop_front(PrefixLength)));
178 return R;
179 }
180 Token.reset(MIToken::Error, Range.remaining());
181 return Range;
182 }
183 while (isIdentifierChar(C.peek()))
184 C.advance();
185 Token.reset(Type, Range.upto(C))
186 .setStringValue(Range.upto(C).drop_front(PrefixLength));
187 return C;
188}
189
191 return StringSwitch<MIToken::TokenKind>(Identifier)
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("nusw", MIToken::kw_nusw)
216 .Case("exact", MIToken::kw_exact)
217 .Case("nneg", MIToken::kw_nneg)
218 .Case("disjoint", MIToken::kw_disjoint)
219 .Case("nofpexcept", MIToken::kw_nofpexcept)
220 .Case("unpredictable", MIToken::kw_unpredictable)
221 .Case("debug-location", MIToken::kw_debug_location)
222 .Case("debug-instr-number", MIToken::kw_debug_instr_number)
223 .Case("dbg-instr-ref", MIToken::kw_dbg_instr_ref)
224 .Case("same_value", MIToken::kw_cfi_same_value)
225 .Case("offset", MIToken::kw_cfi_offset)
226 .Case("rel_offset", MIToken::kw_cfi_rel_offset)
227 .Case("def_cfa_register", MIToken::kw_cfi_def_cfa_register)
228 .Case("def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
229 .Case("adjust_cfa_offset", MIToken::kw_cfi_adjust_cfa_offset)
230 .Case("escape", MIToken::kw_cfi_escape)
231 .Case("def_cfa", MIToken::kw_cfi_def_cfa)
232 .Case("llvm_def_aspace_cfa", MIToken::kw_cfi_llvm_def_aspace_cfa)
233 .Case("remember_state", MIToken::kw_cfi_remember_state)
234 .Case("restore", MIToken::kw_cfi_restore)
235 .Case("restore_state", MIToken::kw_cfi_restore_state)
236 .Case("undefined", MIToken::kw_cfi_undefined)
237 .Case("register", MIToken::kw_cfi_register)
238 .Case("window_save", MIToken::kw_cfi_window_save)
239 .Case("negate_ra_sign_state",
241 .Case("blockaddress", MIToken::kw_blockaddress)
242 .Case("intrinsic", MIToken::kw_intrinsic)
243 .Case("target-index", MIToken::kw_target_index)
244 .Case("half", MIToken::kw_half)
245 .Case("float", MIToken::kw_float)
246 .Case("double", MIToken::kw_double)
247 .Case("x86_fp80", MIToken::kw_x86_fp80)
248 .Case("fp128", MIToken::kw_fp128)
249 .Case("ppc_fp128", MIToken::kw_ppc_fp128)
250 .Case("target-flags", MIToken::kw_target_flags)
251 .Case("volatile", MIToken::kw_volatile)
252 .Case("non-temporal", MIToken::kw_non_temporal)
253 .Case("dereferenceable", MIToken::kw_dereferenceable)
254 .Case("invariant", MIToken::kw_invariant)
255 .Case("align", MIToken::kw_align)
256 .Case("basealign", MIToken::kw_basealign)
257 .Case("addrspace", MIToken::kw_addrspace)
258 .Case("stack", MIToken::kw_stack)
259 .Case("got", MIToken::kw_got)
260 .Case("jump-table", MIToken::kw_jump_table)
261 .Case("constant-pool", MIToken::kw_constant_pool)
262 .Case("call-entry", MIToken::kw_call_entry)
263 .Case("custom", MIToken::kw_custom)
264 .Case("liveout", MIToken::kw_liveout)
265 .Case("landing-pad", MIToken::kw_landing_pad)
266 .Case("inlineasm-br-indirect-target",
268 .Case("ehfunclet-entry", MIToken::kw_ehfunclet_entry)
269 .Case("liveins", MIToken::kw_liveins)
270 .Case("successors", MIToken::kw_successors)
271 .Case("floatpred", MIToken::kw_floatpred)
272 .Case("intpred", MIToken::kw_intpred)
273 .Case("shufflemask", MIToken::kw_shufflemask)
274 .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol)
275 .Case("post-instr-symbol", MIToken::kw_post_instr_symbol)
276 .Case("heap-alloc-marker", MIToken::kw_heap_alloc_marker)
277 .Case("pcsections", MIToken::kw_pcsections)
278 .Case("cfi-type", MIToken::kw_cfi_type)
279 .Case("bbsections", MIToken::kw_bbsections)
280 .Case("bb_id", MIToken::kw_bb_id)
281 .Case("unknown-size", MIToken::kw_unknown_size)
282 .Case("unknown-address", MIToken::kw_unknown_address)
283 .Case("distinct", MIToken::kw_distinct)
284 .Case("ir-block-address-taken", MIToken::kw_ir_block_address_taken)
285 .Case("machine-block-address-taken",
287 .Case("call-frame-size", MIToken::kw_call_frame_size)
288 .Case("noconvergent", MIToken::kw_noconvergent)
290}
291
292static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
293 if (!isalpha(C.peek()) && C.peek() != '_')
294 return std::nullopt;
295 auto Range = C;
296 while (isIdentifierChar(C.peek()))
297 C.advance();
298 auto Identifier = Range.upto(C);
299 Token.reset(getIdentifierKind(Identifier), Identifier)
300 .setStringValue(Identifier);
301 return C;
302}
303
304static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
305 ErrorCallbackType ErrorCallback) {
306 bool IsReference = C.remaining().starts_with("%bb.");
307 if (!IsReference && !C.remaining().starts_with("bb."))
308 return std::nullopt;
309 auto Range = C;
310 unsigned PrefixLength = IsReference ? 4 : 3;
311 C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
312 if (!isdigit(C.peek())) {
313 Token.reset(MIToken::Error, C.remaining());
314 ErrorCallback(C.location(), "expected a number after '%bb.'");
315 return C;
316 }
317 auto NumberRange = C;
318 while (isdigit(C.peek()))
319 C.advance();
320 StringRef Number = NumberRange.upto(C);
321 unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>'
322 // TODO: The format bb.<id>.<irname> is supported only when it's not a
323 // reference. Once we deprecate the format where the irname shows up, we
324 // should only lex forward if it is a reference.
325 if (C.peek() == '.') {
326 C.advance(); // Skip '.'
327 ++StringOffset;
328 while (isIdentifierChar(C.peek()))
329 C.advance();
330 }
331 Token.reset(IsReference ? MIToken::MachineBasicBlock
333 Range.upto(C))
335 .setStringValue(Range.upto(C).drop_front(StringOffset));
336 return C;
337}
338
339static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
340 MIToken::TokenKind Kind) {
341 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
342 return std::nullopt;
343 auto Range = C;
344 C.advance(Rule.size());
345 auto NumberRange = C;
346 while (isdigit(C.peek()))
347 C.advance();
348 Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
349 return C;
350}
351
352static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
353 MIToken::TokenKind Kind) {
354 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
355 return std::nullopt;
356 auto Range = C;
357 C.advance(Rule.size());
358 auto NumberRange = C;
359 while (isdigit(C.peek()))
360 C.advance();
361 StringRef Number = NumberRange.upto(C);
362 unsigned StringOffset = Rule.size() + Number.size();
363 if (C.peek() == '.') {
364 C.advance();
365 ++StringOffset;
366 while (isIdentifierChar(C.peek()))
367 C.advance();
368 }
369 Token.reset(Kind, Range.upto(C))
371 .setStringValue(Range.upto(C).drop_front(StringOffset));
372 return C;
373}
374
375static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
376 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
377}
378
379static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
380 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
381}
382
383static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
384 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
385}
386
387static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
388 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
389}
390
391static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
392 ErrorCallbackType ErrorCallback) {
393 const StringRef Rule = "%subreg.";
394 if (!C.remaining().starts_with(Rule))
395 return std::nullopt;
396 return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
397 ErrorCallback);
398}
399
400static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
401 ErrorCallbackType ErrorCallback) {
402 const StringRef Rule = "%ir-block.";
403 if (!C.remaining().starts_with(Rule))
404 return std::nullopt;
405 if (isdigit(C.peek(Rule.size())))
406 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
407 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
408}
409
410static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
411 ErrorCallbackType ErrorCallback) {
412 const StringRef Rule = "%ir.";
413 if (!C.remaining().starts_with(Rule))
414 return std::nullopt;
415 if (isdigit(C.peek(Rule.size())))
416 return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
417 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
418}
419
420static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
421 ErrorCallbackType ErrorCallback) {
422 if (C.peek() != '"')
423 return std::nullopt;
424 return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
425 ErrorCallback);
426}
427
428static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
429 auto Range = C;
430 C.advance(); // Skip '%'
431 auto NumberRange = C;
432 while (isdigit(C.peek()))
433 C.advance();
435 .setIntegerValue(APSInt(NumberRange.upto(C)));
436 return C;
437}
438
439/// Returns true for a character allowed in a register name.
440static bool isRegisterChar(char C) {
441 return isIdentifierChar(C) && C != '.';
442}
443
444static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
445 Cursor Range = C;
446 C.advance(); // Skip '%'
447 while (isRegisterChar(C.peek()))
448 C.advance();
450 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
451 return C;
452}
453
454static Cursor maybeLexRegister(Cursor C, MIToken &Token,
455 ErrorCallbackType ErrorCallback) {
456 if (C.peek() != '%' && C.peek() != '$')
457 return std::nullopt;
458
459 if (C.peek() == '%') {
460 if (isdigit(C.peek(1)))
461 return lexVirtualRegister(C, Token);
462
463 if (isRegisterChar(C.peek(1)))
464 return lexNamedVirtualRegister(C, Token);
465
466 return std::nullopt;
467 }
468
469 assert(C.peek() == '$');
470 auto Range = C;
471 C.advance(); // Skip '$'
472 while (isRegisterChar(C.peek()))
473 C.advance();
474 Token.reset(MIToken::NamedRegister, Range.upto(C))
475 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
476 return C;
477}
478
479static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
480 ErrorCallbackType ErrorCallback) {
481 if (C.peek() != '@')
482 return std::nullopt;
483 if (!isdigit(C.peek(1)))
484 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
485 ErrorCallback);
486 auto Range = C;
487 C.advance(1); // Skip the '@'
488 auto NumberRange = C;
489 while (isdigit(C.peek()))
490 C.advance();
491 Token.reset(MIToken::GlobalValue, Range.upto(C))
492 .setIntegerValue(APSInt(NumberRange.upto(C)));
493 return C;
494}
495
496static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
497 ErrorCallbackType ErrorCallback) {
498 if (C.peek() != '&')
499 return std::nullopt;
500 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
501 ErrorCallback);
502}
503
504static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
505 ErrorCallbackType ErrorCallback) {
506 const StringRef Rule = "<mcsymbol ";
507 if (!C.remaining().starts_with(Rule))
508 return std::nullopt;
509 auto Start = C;
510 C.advance(Rule.size());
511
512 // Try a simple unquoted name.
513 if (C.peek() != '"') {
514 while (isIdentifierChar(C.peek()))
515 C.advance();
516 StringRef String = Start.upto(C).drop_front(Rule.size());
517 if (C.peek() != '>') {
518 ErrorCallback(C.location(),
519 "expected the '<mcsymbol ...' to be closed by a '>'");
520 Token.reset(MIToken::Error, Start.remaining());
521 return Start;
522 }
523 C.advance();
524
525 Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
526 return C;
527 }
528
529 // Otherwise lex out a quoted name.
530 Cursor R = lexStringConstant(C, ErrorCallback);
531 if (!R) {
532 ErrorCallback(C.location(),
533 "unable to parse quoted string from opening quote");
534 Token.reset(MIToken::Error, Start.remaining());
535 return Start;
536 }
537 StringRef String = Start.upto(R).drop_front(Rule.size());
538 if (R.peek() != '>') {
539 ErrorCallback(R.location(),
540 "expected the '<mcsymbol ...' to be closed by a '>'");
541 Token.reset(MIToken::Error, Start.remaining());
542 return Start;
543 }
544 R.advance();
545
546 Token.reset(MIToken::MCSymbol, Start.upto(R))
548 return R;
549}
550
552 return C == 'H' || C == 'K' || C == 'L' || C == 'M' || C == 'R';
553}
554
555static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
556 C.advance();
557 // Skip over [0-9]*([eE][-+]?[0-9]+)?
558 while (isdigit(C.peek()))
559 C.advance();
560 if ((C.peek() == 'e' || C.peek() == 'E') &&
561 (isdigit(C.peek(1)) ||
562 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
563 C.advance(2);
564 while (isdigit(C.peek()))
565 C.advance();
566 }
568 return C;
569}
570
571static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
572 if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
573 return std::nullopt;
574 Cursor Range = C;
575 C.advance(2);
576 unsigned PrefLen = 2;
577 if (isValidHexFloatingPointPrefix(C.peek())) {
578 C.advance();
579 PrefLen++;
580 }
581 while (isxdigit(C.peek()))
582 C.advance();
583 StringRef StrVal = Range.upto(C);
584 if (StrVal.size() <= PrefLen)
585 return std::nullopt;
586 if (PrefLen == 2)
587 Token.reset(MIToken::HexLiteral, Range.upto(C));
588 else // It must be 3, which means that there was a floating-point prefix.
590 return C;
591}
592
593static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
594 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
595 return std::nullopt;
596 auto Range = C;
597 C.advance();
598 while (isdigit(C.peek()))
599 C.advance();
600 if (C.peek() == '.')
601 return lexFloatingPointLiteral(Range, C, Token);
602 StringRef StrVal = Range.upto(C);
603 Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
604 return C;
605}
606
608 return StringSwitch<MIToken::TokenKind>(Identifier)
609 .Case("!tbaa", MIToken::md_tbaa)
610 .Case("!alias.scope", MIToken::md_alias_scope)
611 .Case("!noalias", MIToken::md_noalias)
612 .Case("!range", MIToken::md_range)
613 .Case("!DIExpression", MIToken::md_diexpr)
614 .Case("!DILocation", MIToken::md_dilocation)
616}
617
618static Cursor maybeLexExclaim(Cursor C, MIToken &Token,
619 ErrorCallbackType ErrorCallback) {
620 if (C.peek() != '!')
621 return std::nullopt;
622 auto Range = C;
623 C.advance(1);
624 if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
625 Token.reset(MIToken::exclaim, Range.upto(C));
626 return C;
627 }
628 while (isIdentifierChar(C.peek()))
629 C.advance();
630 StringRef StrVal = Range.upto(C);
631 Token.reset(getMetadataKeywordKind(StrVal), StrVal);
632 if (Token.isError())
633 ErrorCallback(Token.location(),
634 "use of unknown metadata keyword '" + StrVal + "'");
635 return C;
636}
637
639 switch (C) {
640 case ',':
641 return MIToken::comma;
642 case '.':
643 return MIToken::dot;
644 case '=':
645 return MIToken::equal;
646 case ':':
647 return MIToken::colon;
648 case '(':
649 return MIToken::lparen;
650 case ')':
651 return MIToken::rparen;
652 case '{':
653 return MIToken::lbrace;
654 case '}':
655 return MIToken::rbrace;
656 case '+':
657 return MIToken::plus;
658 case '-':
659 return MIToken::minus;
660 case '<':
661 return MIToken::less;
662 case '>':
663 return MIToken::greater;
664 default:
665 return MIToken::Error;
666 }
667}
668
669static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
671 unsigned Length = 1;
672 if (C.peek() == ':' && C.peek(1) == ':') {
673 Kind = MIToken::coloncolon;
674 Length = 2;
675 } else
676 Kind = symbolToken(C.peek());
677 if (Kind == MIToken::Error)
678 return std::nullopt;
679 auto Range = C;
680 C.advance(Length);
681 Token.reset(Kind, Range.upto(C));
682 return C;
683}
684
685static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
686 if (!isNewlineChar(C.peek()))
687 return std::nullopt;
688 auto Range = C;
689 C.advance();
690 Token.reset(MIToken::Newline, Range.upto(C));
691 return C;
692}
693
694static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
695 ErrorCallbackType ErrorCallback) {
696 if (C.peek() != '`')
697 return std::nullopt;
698 auto Range = C;
699 C.advance();
700 auto StrRange = C;
701 while (C.peek() != '`') {
702 if (C.isEOF() || isNewlineChar(C.peek())) {
703 ErrorCallback(
704 C.location(),
705 "end of machine instruction reached before the closing '`'");
706 Token.reset(MIToken::Error, Range.remaining());
707 return C;
708 }
709 C.advance();
710 }
711 StringRef Value = StrRange.upto(C);
712 C.advance();
714 return C;
715}
716
718 ErrorCallbackType ErrorCallback) {
719 auto C = skipComment(skipWhitespace(Cursor(Source)));
720 if (C.isEOF()) {
721 Token.reset(MIToken::Eof, C.remaining());
722 return C.remaining();
723 }
724
726
727 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
728 return R.remaining();
729 if (Cursor R = maybeLexIdentifier(C, Token))
730 return R.remaining();
731 if (Cursor R = maybeLexJumpTableIndex(C, Token))
732 return R.remaining();
733 if (Cursor R = maybeLexStackObject(C, Token))
734 return R.remaining();
735 if (Cursor R = maybeLexFixedStackObject(C, Token))
736 return R.remaining();
737 if (Cursor R = maybeLexConstantPoolItem(C, Token))
738 return R.remaining();
739 if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
740 return R.remaining();
741 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
742 return R.remaining();
743 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
744 return R.remaining();
745 if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
746 return R.remaining();
747 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
748 return R.remaining();
749 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
750 return R.remaining();
751 if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
752 return R.remaining();
753 if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
754 return R.remaining();
755 if (Cursor R = maybeLexNumericalLiteral(C, Token))
756 return R.remaining();
757 if (Cursor R = maybeLexExclaim(C, Token, ErrorCallback))
758 return R.remaining();
759 if (Cursor R = maybeLexSymbol(C, Token))
760 return R.remaining();
761 if (Cursor R = maybeLexNewline(C, Token))
762 return R.remaining();
763 if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
764 return R.remaining();
765 if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
766 return R.remaining();
767
768 Token.reset(MIToken::Error, C.remaining());
769 ErrorCallback(C.location(),
770 Twine("unexpected character '") + Twine(C.peek()) + "'");
771 return C.remaining();
772}
bool End
Definition: ELF_riscv.cpp:480
#define I(x, y, z)
Definition: MD5.cpp:58
static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:694
static Cursor skipComment(Cursor C)
Skip a line comment and return the updated cursor.
Definition: MILexer.cpp:94
static bool isRegisterChar(char C)
Returns true for a character allowed in a register name.
Definition: MILexer.cpp:440
static Cursor lexStringConstant(Cursor C, ErrorCallbackType ErrorCallback)
Lex a string constant using the following regular expression: "[^"]*".
Definition: MILexer.cpp:154
static bool isNewlineChar(char C)
Definition: MILexer.cpp:91
static MIToken::TokenKind symbolToken(char C)
Definition: MILexer.cpp:638
static bool isValidHexFloatingPointPrefix(char C)
Definition: MILexer.cpp:551
static MIToken::TokenKind getIdentifierKind(StringRef Identifier)
Definition: MILexer.cpp:190
static Cursor maybeLexIRBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:400
static Cursor maybeLexSymbol(Cursor C, MIToken &Token)
Definition: MILexer.cpp:669
static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token)
Definition: MILexer.cpp:375
static Cursor maybeLexRegister(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:454
static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token)
Definition: MILexer.cpp:571
static Cursor lexVirtualRegister(Cursor C, MIToken &Token)
Definition: MILexer.cpp:428
static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token)
Definition: MILexer.cpp:593
static Cursor maybeLexExclaim(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:618
static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token)
Definition: MILexer.cpp:444
static std::string unescapeQuotedString(StringRef Value)
Unescapes the given string value.
Definition: MILexer.cpp:126
static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule, MIToken::TokenKind Kind)
Definition: MILexer.cpp:352
static Cursor maybeLexNewline(Cursor C, MIToken &Token)
Definition: MILexer.cpp:685
static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:504
static Cursor skipMachineOperandComment(Cursor C)
Machine operands can have comments, enclosed between /* and ‍/.
Definition: MILexer.cpp:104
static MIToken::TokenKind getMetadataKeywordKind(StringRef Identifier)
Definition: MILexer.cpp:607
static Cursor maybeLexIdentifier(Cursor C, MIToken &Token)
Definition: MILexer.cpp:292
static Cursor maybeLexStackObject(Cursor C, MIToken &Token)
Definition: MILexer.cpp:379
static Cursor skipWhitespace(Cursor C)
Skip the leading whitespace characters and return the updated cursor.
Definition: MILexer.cpp:85
static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:496
static bool isIdentifierChar(char C)
Return true if the given character satisfies the following regular expression: [-a-zA-Z$....
Definition: MILexer.cpp:118
static Cursor lexName(Cursor C, MIToken &Token, MIToken::TokenKind Type, unsigned PrefixLength, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:168
static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:479
static Cursor maybeLexIRValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:410
static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token)
Definition: MILexer.cpp:383
static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:304
static Cursor maybeLexStringConstant(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:420
static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:391
static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token)
Definition: MILexer.cpp:555
static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token)
Definition: MILexer.cpp:387
static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, MIToken::TokenKind Kind)
Definition: MILexer.cpp:339
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some functions that are useful when dealing with strings.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static bool peek(struct InternalInstruction *insn, uint8_t &byte)
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
An efficient, type-erasing, non-owning reference to a callable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:470
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:456
StringRef lexMIToken(StringRef Source, MIToken &Token, function_ref< void(StringRef::iterator, const Twine &)> ErrorCallback)
Consume a single machine instruction token in the given source and return the remaining source string...
A token produced by the machine instruction lexer.
Definition: MILexer.h:26
MIToken & setStringValue(StringRef StrVal)
Definition: MILexer.cpp:68
@ kw_target_flags
Definition: MILexer.h:108
@ kw_landing_pad
Definition: MILexer.h:122
@ kw_blockaddress
Definition: MILexer.h:99
@ kw_pre_instr_symbol
Definition: MILexer.h:130
@ md_alias_scope
Definition: MILexer.h:149
@ NamedGlobalValue
Definition: MILexer.h:163
@ kw_call_frame_size
Definition: MILexer.h:141
@ SubRegisterIndex
Definition: MILexer.h:181
@ kw_frame_setup
Definition: MILexer.h:63
@ kw_cfi_aarch64_negate_ra_sign_state
Definition: MILexer.h:98
@ ConstantPoolItem
Definition: MILexer.h:174
@ kw_cfi_llvm_def_aspace_cfa
Definition: MILexer.h:91
@ MachineBasicBlock
Definition: MILexer.h:160
@ kw_dbg_instr_ref
Definition: MILexer.h:82
@ NamedVirtualRegister
Definition: MILexer.h:158
@ kw_early_clobber
Definition: MILexer.h:59
@ kw_cfi_offset
Definition: MILexer.h:84
@ kw_unpredictable
Definition: MILexer.h:77
@ FloatingPointLiteral
Definition: MILexer.h:170
@ kw_debug_use
Definition: MILexer.h:60
@ kw_cfi_window_save
Definition: MILexer.h:97
@ kw_constant_pool
Definition: MILexer.h:118
@ kw_frame_destroy
Definition: MILexer.h:64
@ kw_cfi_undefined
Definition: MILexer.h:96
@ StringConstant
Definition: MILexer.h:182
@ MachineBasicBlockLabel
Definition: MILexer.h:159
@ kw_cfi_restore
Definition: MILexer.h:94
@ kw_non_temporal
Definition: MILexer.h:110
@ kw_cfi_register
Definition: MILexer.h:92
@ kw_inlineasm_br_indirect_target
Definition: MILexer.h:123
@ kw_cfi_rel_offset
Definition: MILexer.h:85
@ kw_ehfunclet_entry
Definition: MILexer.h:124
@ kw_cfi_def_cfa_register
Definition: MILexer.h:86
@ FixedStackObject
Definition: MILexer.h:162
@ kw_cfi_same_value
Definition: MILexer.h:83
@ kw_target_index
Definition: MILexer.h:101
@ kw_cfi_adjust_cfa_offset
Definition: MILexer.h:88
@ kw_dereferenceable
Definition: MILexer.h:55
@ kw_implicit_define
Definition: MILexer.h:52
@ kw_cfi_def_cfa
Definition: MILexer.h:90
@ kw_cfi_escape
Definition: MILexer.h:89
@ VirtualRegister
Definition: MILexer.h:173
@ kw_cfi_def_cfa_offset
Definition: MILexer.h:87
@ kw_machine_block_address_taken
Definition: MILexer.h:140
@ kw_renamable
Definition: MILexer.h:61
@ ExternalSymbol
Definition: MILexer.h:165
@ kw_unknown_size
Definition: MILexer.h:137
@ IntegerLiteral
Definition: MILexer.h:169
@ kw_cfi_remember_state
Definition: MILexer.h:93
@ kw_debug_instr_number
Definition: MILexer.h:81
@ kw_post_instr_symbol
Definition: MILexer.h:131
@ kw_cfi_restore_state
Definition: MILexer.h:95
@ kw_nofpexcept
Definition: MILexer.h:76
@ kw_ir_block_address_taken
Definition: MILexer.h:139
@ kw_unknown_address
Definition: MILexer.h:138
@ JumpTableIndex
Definition: MILexer.h:175
@ kw_shufflemask
Definition: MILexer.h:129
@ kw_debug_location
Definition: MILexer.h:80
@ kw_noconvergent
Definition: MILexer.h:142
@ kw_heap_alloc_marker
Definition: MILexer.h:132
MIToken & setIntegerValue(APSInt IntVal)
Definition: MILexer.cpp:79
MIToken & reset(TokenKind Kind, StringRef Range)
Definition: MILexer.cpp:62
bool isError() const
Definition: MILexer.h:203
MIToken & setOwnedStringValue(std::string StrVal)
Definition: MILexer.cpp:73
StringRef::iterator location() const
Definition: MILexer.h:232