LLVM 20.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("samesign", MIToken::kw_samesign)
220 .Case("nofpexcept", MIToken::kw_nofpexcept)
221 .Case("unpredictable", MIToken::kw_unpredictable)
222 .Case("debug-location", MIToken::kw_debug_location)
223 .Case("debug-instr-number", MIToken::kw_debug_instr_number)
224 .Case("dbg-instr-ref", MIToken::kw_dbg_instr_ref)
225 .Case("same_value", MIToken::kw_cfi_same_value)
226 .Case("offset", MIToken::kw_cfi_offset)
227 .Case("rel_offset", MIToken::kw_cfi_rel_offset)
228 .Case("def_cfa_register", MIToken::kw_cfi_def_cfa_register)
229 .Case("def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
230 .Case("adjust_cfa_offset", MIToken::kw_cfi_adjust_cfa_offset)
231 .Case("escape", MIToken::kw_cfi_escape)
232 .Case("def_cfa", MIToken::kw_cfi_def_cfa)
233 .Case("llvm_def_aspace_cfa", MIToken::kw_cfi_llvm_def_aspace_cfa)
234 .Case("remember_state", MIToken::kw_cfi_remember_state)
235 .Case("restore", MIToken::kw_cfi_restore)
236 .Case("restore_state", MIToken::kw_cfi_restore_state)
237 .Case("undefined", MIToken::kw_cfi_undefined)
238 .Case("register", MIToken::kw_cfi_register)
239 .Case("window_save", MIToken::kw_cfi_window_save)
240 .Case("negate_ra_sign_state",
242 .Case("negate_ra_sign_state_with_pc",
244 .Case("blockaddress", MIToken::kw_blockaddress)
245 .Case("intrinsic", MIToken::kw_intrinsic)
246 .Case("target-index", MIToken::kw_target_index)
247 .Case("half", MIToken::kw_half)
248 .Case("bfloat", MIToken::kw_bfloat)
249 .Case("float", MIToken::kw_float)
250 .Case("double", MIToken::kw_double)
251 .Case("x86_fp80", MIToken::kw_x86_fp80)
252 .Case("fp128", MIToken::kw_fp128)
253 .Case("ppc_fp128", MIToken::kw_ppc_fp128)
254 .Case("target-flags", MIToken::kw_target_flags)
255 .Case("volatile", MIToken::kw_volatile)
256 .Case("non-temporal", MIToken::kw_non_temporal)
257 .Case("dereferenceable", MIToken::kw_dereferenceable)
258 .Case("invariant", MIToken::kw_invariant)
259 .Case("align", MIToken::kw_align)
260 .Case("basealign", MIToken::kw_basealign)
261 .Case("addrspace", MIToken::kw_addrspace)
262 .Case("stack", MIToken::kw_stack)
263 .Case("got", MIToken::kw_got)
264 .Case("jump-table", MIToken::kw_jump_table)
265 .Case("constant-pool", MIToken::kw_constant_pool)
266 .Case("call-entry", MIToken::kw_call_entry)
267 .Case("custom", MIToken::kw_custom)
268 .Case("liveout", MIToken::kw_liveout)
269 .Case("landing-pad", MIToken::kw_landing_pad)
270 .Case("inlineasm-br-indirect-target",
272 .Case("ehfunclet-entry", MIToken::kw_ehfunclet_entry)
273 .Case("liveins", MIToken::kw_liveins)
274 .Case("successors", MIToken::kw_successors)
275 .Case("floatpred", MIToken::kw_floatpred)
276 .Case("intpred", MIToken::kw_intpred)
277 .Case("shufflemask", MIToken::kw_shufflemask)
278 .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol)
279 .Case("post-instr-symbol", MIToken::kw_post_instr_symbol)
280 .Case("heap-alloc-marker", MIToken::kw_heap_alloc_marker)
281 .Case("pcsections", MIToken::kw_pcsections)
282 .Case("cfi-type", MIToken::kw_cfi_type)
283 .Case("bbsections", MIToken::kw_bbsections)
284 .Case("bb_id", MIToken::kw_bb_id)
285 .Case("unknown-size", MIToken::kw_unknown_size)
286 .Case("unknown-address", MIToken::kw_unknown_address)
287 .Case("distinct", MIToken::kw_distinct)
288 .Case("ir-block-address-taken", MIToken::kw_ir_block_address_taken)
289 .Case("machine-block-address-taken",
291 .Case("call-frame-size", MIToken::kw_call_frame_size)
292 .Case("noconvergent", MIToken::kw_noconvergent)
294}
295
296static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
297 if (!isalpha(C.peek()) && C.peek() != '_')
298 return std::nullopt;
299 auto Range = C;
300 while (isIdentifierChar(C.peek()))
301 C.advance();
302 auto Identifier = Range.upto(C);
303 Token.reset(getIdentifierKind(Identifier), Identifier)
304 .setStringValue(Identifier);
305 return C;
306}
307
308static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
309 ErrorCallbackType ErrorCallback) {
310 bool IsReference = C.remaining().starts_with("%bb.");
311 if (!IsReference && !C.remaining().starts_with("bb."))
312 return std::nullopt;
313 auto Range = C;
314 unsigned PrefixLength = IsReference ? 4 : 3;
315 C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
316 if (!isdigit(C.peek())) {
317 Token.reset(MIToken::Error, C.remaining());
318 ErrorCallback(C.location(), "expected a number after '%bb.'");
319 return C;
320 }
321 auto NumberRange = C;
322 while (isdigit(C.peek()))
323 C.advance();
324 StringRef Number = NumberRange.upto(C);
325 unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>'
326 // TODO: The format bb.<id>.<irname> is supported only when it's not a
327 // reference. Once we deprecate the format where the irname shows up, we
328 // should only lex forward if it is a reference.
329 if (C.peek() == '.') {
330 C.advance(); // Skip '.'
331 ++StringOffset;
332 while (isIdentifierChar(C.peek()))
333 C.advance();
334 }
335 Token.reset(IsReference ? MIToken::MachineBasicBlock
337 Range.upto(C))
339 .setStringValue(Range.upto(C).drop_front(StringOffset));
340 return C;
341}
342
343static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
344 MIToken::TokenKind Kind) {
345 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
346 return std::nullopt;
347 auto Range = C;
348 C.advance(Rule.size());
349 auto NumberRange = C;
350 while (isdigit(C.peek()))
351 C.advance();
352 Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
353 return C;
354}
355
356static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
357 MIToken::TokenKind Kind) {
358 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
359 return std::nullopt;
360 auto Range = C;
361 C.advance(Rule.size());
362 auto NumberRange = C;
363 while (isdigit(C.peek()))
364 C.advance();
365 StringRef Number = NumberRange.upto(C);
366 unsigned StringOffset = Rule.size() + Number.size();
367 if (C.peek() == '.') {
368 C.advance();
369 ++StringOffset;
370 while (isIdentifierChar(C.peek()))
371 C.advance();
372 }
373 Token.reset(Kind, Range.upto(C))
375 .setStringValue(Range.upto(C).drop_front(StringOffset));
376 return C;
377}
378
379static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
380 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
381}
382
383static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
384 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
385}
386
387static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
388 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
389}
390
391static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
392 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
393}
394
395static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
396 ErrorCallbackType ErrorCallback) {
397 const StringRef Rule = "%subreg.";
398 if (!C.remaining().starts_with(Rule))
399 return std::nullopt;
400 return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
401 ErrorCallback);
402}
403
404static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
405 ErrorCallbackType ErrorCallback) {
406 const StringRef Rule = "%ir-block.";
407 if (!C.remaining().starts_with(Rule))
408 return std::nullopt;
409 if (isdigit(C.peek(Rule.size())))
410 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
411 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
412}
413
414static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
415 ErrorCallbackType ErrorCallback) {
416 const StringRef Rule = "%ir.";
417 if (!C.remaining().starts_with(Rule))
418 return std::nullopt;
419 if (isdigit(C.peek(Rule.size())))
420 return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
421 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
422}
423
424static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
425 ErrorCallbackType ErrorCallback) {
426 if (C.peek() != '"')
427 return std::nullopt;
428 return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
429 ErrorCallback);
430}
431
432static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
433 auto Range = C;
434 C.advance(); // Skip '%'
435 auto NumberRange = C;
436 while (isdigit(C.peek()))
437 C.advance();
439 .setIntegerValue(APSInt(NumberRange.upto(C)));
440 return C;
441}
442
443/// Returns true for a character allowed in a register name.
444static bool isRegisterChar(char C) {
445 return isIdentifierChar(C) && C != '.';
446}
447
448static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
449 Cursor Range = C;
450 C.advance(); // Skip '%'
451 while (isRegisterChar(C.peek()))
452 C.advance();
454 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
455 return C;
456}
457
458static Cursor maybeLexRegister(Cursor C, MIToken &Token,
459 ErrorCallbackType ErrorCallback) {
460 if (C.peek() != '%' && C.peek() != '$')
461 return std::nullopt;
462
463 if (C.peek() == '%') {
464 if (isdigit(C.peek(1)))
465 return lexVirtualRegister(C, Token);
466
467 if (isRegisterChar(C.peek(1)))
468 return lexNamedVirtualRegister(C, Token);
469
470 return std::nullopt;
471 }
472
473 assert(C.peek() == '$');
474 auto Range = C;
475 C.advance(); // Skip '$'
476 while (isRegisterChar(C.peek()))
477 C.advance();
478 Token.reset(MIToken::NamedRegister, Range.upto(C))
479 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
480 return C;
481}
482
483static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
484 ErrorCallbackType ErrorCallback) {
485 if (C.peek() != '@')
486 return std::nullopt;
487 if (!isdigit(C.peek(1)))
488 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
489 ErrorCallback);
490 auto Range = C;
491 C.advance(1); // Skip the '@'
492 auto NumberRange = C;
493 while (isdigit(C.peek()))
494 C.advance();
495 Token.reset(MIToken::GlobalValue, Range.upto(C))
496 .setIntegerValue(APSInt(NumberRange.upto(C)));
497 return C;
498}
499
500static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
501 ErrorCallbackType ErrorCallback) {
502 if (C.peek() != '&')
503 return std::nullopt;
504 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
505 ErrorCallback);
506}
507
508static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
509 ErrorCallbackType ErrorCallback) {
510 const StringRef Rule = "<mcsymbol ";
511 if (!C.remaining().starts_with(Rule))
512 return std::nullopt;
513 auto Start = C;
514 C.advance(Rule.size());
515
516 // Try a simple unquoted name.
517 if (C.peek() != '"') {
518 while (isIdentifierChar(C.peek()))
519 C.advance();
520 StringRef String = Start.upto(C).drop_front(Rule.size());
521 if (C.peek() != '>') {
522 ErrorCallback(C.location(),
523 "expected the '<mcsymbol ...' to be closed by a '>'");
524 Token.reset(MIToken::Error, Start.remaining());
525 return Start;
526 }
527 C.advance();
528
529 Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
530 return C;
531 }
532
533 // Otherwise lex out a quoted name.
534 Cursor R = lexStringConstant(C, ErrorCallback);
535 if (!R) {
536 ErrorCallback(C.location(),
537 "unable to parse quoted string from opening quote");
538 Token.reset(MIToken::Error, Start.remaining());
539 return Start;
540 }
541 StringRef String = Start.upto(R).drop_front(Rule.size());
542 if (R.peek() != '>') {
543 ErrorCallback(R.location(),
544 "expected the '<mcsymbol ...' to be closed by a '>'");
545 Token.reset(MIToken::Error, Start.remaining());
546 return Start;
547 }
548 R.advance();
549
550 Token.reset(MIToken::MCSymbol, Start.upto(R))
552 return R;
553}
554
556 return C == 'H' || C == 'K' || C == 'L' || C == 'M' || C == 'R';
557}
558
559static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
560 C.advance();
561 // Skip over [0-9]*([eE][-+]?[0-9]+)?
562 while (isdigit(C.peek()))
563 C.advance();
564 if ((C.peek() == 'e' || C.peek() == 'E') &&
565 (isdigit(C.peek(1)) ||
566 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
567 C.advance(2);
568 while (isdigit(C.peek()))
569 C.advance();
570 }
572 return C;
573}
574
575static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
576 if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
577 return std::nullopt;
578 Cursor Range = C;
579 C.advance(2);
580 unsigned PrefLen = 2;
581 if (isValidHexFloatingPointPrefix(C.peek())) {
582 C.advance();
583 PrefLen++;
584 }
585 while (isxdigit(C.peek()))
586 C.advance();
587 StringRef StrVal = Range.upto(C);
588 if (StrVal.size() <= PrefLen)
589 return std::nullopt;
590 if (PrefLen == 2)
591 Token.reset(MIToken::HexLiteral, Range.upto(C));
592 else // It must be 3, which means that there was a floating-point prefix.
594 return C;
595}
596
597static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
598 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
599 return std::nullopt;
600 auto Range = C;
601 C.advance();
602 while (isdigit(C.peek()))
603 C.advance();
604 if (C.peek() == '.')
605 return lexFloatingPointLiteral(Range, C, Token);
606 StringRef StrVal = Range.upto(C);
607 Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
608 return C;
609}
610
612 return StringSwitch<MIToken::TokenKind>(Identifier)
613 .Case("!tbaa", MIToken::md_tbaa)
614 .Case("!alias.scope", MIToken::md_alias_scope)
615 .Case("!noalias", MIToken::md_noalias)
616 .Case("!range", MIToken::md_range)
617 .Case("!DIExpression", MIToken::md_diexpr)
618 .Case("!DILocation", MIToken::md_dilocation)
620}
621
622static Cursor maybeLexExclaim(Cursor C, MIToken &Token,
623 ErrorCallbackType ErrorCallback) {
624 if (C.peek() != '!')
625 return std::nullopt;
626 auto Range = C;
627 C.advance(1);
628 if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
629 Token.reset(MIToken::exclaim, Range.upto(C));
630 return C;
631 }
632 while (isIdentifierChar(C.peek()))
633 C.advance();
634 StringRef StrVal = Range.upto(C);
635 Token.reset(getMetadataKeywordKind(StrVal), StrVal);
636 if (Token.isError())
637 ErrorCallback(Token.location(),
638 "use of unknown metadata keyword '" + StrVal + "'");
639 return C;
640}
641
643 switch (C) {
644 case ',':
645 return MIToken::comma;
646 case '.':
647 return MIToken::dot;
648 case '=':
649 return MIToken::equal;
650 case ':':
651 return MIToken::colon;
652 case '(':
653 return MIToken::lparen;
654 case ')':
655 return MIToken::rparen;
656 case '{':
657 return MIToken::lbrace;
658 case '}':
659 return MIToken::rbrace;
660 case '+':
661 return MIToken::plus;
662 case '-':
663 return MIToken::minus;
664 case '<':
665 return MIToken::less;
666 case '>':
667 return MIToken::greater;
668 default:
669 return MIToken::Error;
670 }
671}
672
673static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
675 unsigned Length = 1;
676 if (C.peek() == ':' && C.peek(1) == ':') {
677 Kind = MIToken::coloncolon;
678 Length = 2;
679 } else
680 Kind = symbolToken(C.peek());
681 if (Kind == MIToken::Error)
682 return std::nullopt;
683 auto Range = C;
684 C.advance(Length);
685 Token.reset(Kind, Range.upto(C));
686 return C;
687}
688
689static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
690 if (!isNewlineChar(C.peek()))
691 return std::nullopt;
692 auto Range = C;
693 C.advance();
694 Token.reset(MIToken::Newline, Range.upto(C));
695 return C;
696}
697
698static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
699 ErrorCallbackType ErrorCallback) {
700 if (C.peek() != '`')
701 return std::nullopt;
702 auto Range = C;
703 C.advance();
704 auto StrRange = C;
705 while (C.peek() != '`') {
706 if (C.isEOF() || isNewlineChar(C.peek())) {
707 ErrorCallback(
708 C.location(),
709 "end of machine instruction reached before the closing '`'");
710 Token.reset(MIToken::Error, Range.remaining());
711 return C;
712 }
713 C.advance();
714 }
715 StringRef Value = StrRange.upto(C);
716 C.advance();
718 return C;
719}
720
722 ErrorCallbackType ErrorCallback) {
723 auto C = skipComment(skipWhitespace(Cursor(Source)));
724 if (C.isEOF()) {
725 Token.reset(MIToken::Eof, C.remaining());
726 return C.remaining();
727 }
728
730
731 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
732 return R.remaining();
733 if (Cursor R = maybeLexIdentifier(C, Token))
734 return R.remaining();
735 if (Cursor R = maybeLexJumpTableIndex(C, Token))
736 return R.remaining();
737 if (Cursor R = maybeLexStackObject(C, Token))
738 return R.remaining();
739 if (Cursor R = maybeLexFixedStackObject(C, Token))
740 return R.remaining();
741 if (Cursor R = maybeLexConstantPoolItem(C, Token))
742 return R.remaining();
743 if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
744 return R.remaining();
745 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
746 return R.remaining();
747 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
748 return R.remaining();
749 if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
750 return R.remaining();
751 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
752 return R.remaining();
753 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
754 return R.remaining();
755 if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
756 return R.remaining();
757 if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
758 return R.remaining();
759 if (Cursor R = maybeLexNumericalLiteral(C, Token))
760 return R.remaining();
761 if (Cursor R = maybeLexExclaim(C, Token, ErrorCallback))
762 return R.remaining();
763 if (Cursor R = maybeLexSymbol(C, Token))
764 return R.remaining();
765 if (Cursor R = maybeLexNewline(C, Token))
766 return R.remaining();
767 if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
768 return R.remaining();
769 if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
770 return R.remaining();
771
772 Token.reset(MIToken::Error, C.remaining());
773 ErrorCallback(C.location(),
774 Twine("unexpected character '") + Twine(C.peek()) + "'");
775 return C.remaining();
776}
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:698
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:444
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:642
static bool isValidHexFloatingPointPrefix(char C)
Definition: MILexer.cpp:555
static MIToken::TokenKind getIdentifierKind(StringRef Identifier)
Definition: MILexer.cpp:190
static Cursor maybeLexIRBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:404
static Cursor maybeLexSymbol(Cursor C, MIToken &Token)
Definition: MILexer.cpp:673
static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token)
Definition: MILexer.cpp:379
static Cursor maybeLexRegister(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:458
static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token)
Definition: MILexer.cpp:575
static Cursor lexVirtualRegister(Cursor C, MIToken &Token)
Definition: MILexer.cpp:432
static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token)
Definition: MILexer.cpp:597
static Cursor maybeLexExclaim(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:622
static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token)
Definition: MILexer.cpp:448
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:356
static Cursor maybeLexNewline(Cursor C, MIToken &Token)
Definition: MILexer.cpp:689
static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:508
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:611
static Cursor maybeLexIdentifier(Cursor C, MIToken &Token)
Definition: MILexer.cpp:296
static Cursor maybeLexStackObject(Cursor C, MIToken &Token)
Definition: MILexer.cpp:383
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:500
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:483
static Cursor maybeLexIRValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:414
static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token)
Definition: MILexer.cpp:387
static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:308
static Cursor maybeLexStringConstant(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:424
static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:395
static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token)
Definition: MILexer.cpp:559
static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token)
Definition: MILexer.cpp:391
static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, MIToken::TokenKind Kind)
Definition: MILexer.cpp:343
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:51
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
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:463
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:480
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:111
@ kw_landing_pad
Definition: MILexer.h:125
@ kw_blockaddress
Definition: MILexer.h:101
@ kw_pre_instr_symbol
Definition: MILexer.h:133
@ md_alias_scope
Definition: MILexer.h:152
@ NamedGlobalValue
Definition: MILexer.h:166
@ kw_call_frame_size
Definition: MILexer.h:144
@ SubRegisterIndex
Definition: MILexer.h:184
@ kw_frame_setup
Definition: MILexer.h:63
@ kw_cfi_aarch64_negate_ra_sign_state
Definition: MILexer.h:99
@ ConstantPoolItem
Definition: MILexer.h:177
@ kw_cfi_llvm_def_aspace_cfa
Definition: MILexer.h:92
@ MachineBasicBlock
Definition: MILexer.h:163
@ kw_dbg_instr_ref
Definition: MILexer.h:83
@ NamedVirtualRegister
Definition: MILexer.h:161
@ kw_early_clobber
Definition: MILexer.h:59
@ kw_cfi_offset
Definition: MILexer.h:85
@ kw_unpredictable
Definition: MILexer.h:77
@ FloatingPointLiteral
Definition: MILexer.h:173
@ kw_debug_use
Definition: MILexer.h:60
@ kw_cfi_window_save
Definition: MILexer.h:98
@ kw_constant_pool
Definition: MILexer.h:121
@ kw_frame_destroy
Definition: MILexer.h:64
@ kw_cfi_undefined
Definition: MILexer.h:97
@ StringConstant
Definition: MILexer.h:185
@ MachineBasicBlockLabel
Definition: MILexer.h:162
@ kw_cfi_restore
Definition: MILexer.h:95
@ kw_non_temporal
Definition: MILexer.h:113
@ kw_cfi_register
Definition: MILexer.h:93
@ kw_inlineasm_br_indirect_target
Definition: MILexer.h:126
@ kw_cfi_rel_offset
Definition: MILexer.h:86
@ kw_ehfunclet_entry
Definition: MILexer.h:127
@ kw_cfi_aarch64_negate_ra_sign_state_with_pc
Definition: MILexer.h:100
@ kw_cfi_def_cfa_register
Definition: MILexer.h:87
@ FixedStackObject
Definition: MILexer.h:165
@ kw_cfi_same_value
Definition: MILexer.h:84
@ kw_target_index
Definition: MILexer.h:103
@ kw_cfi_adjust_cfa_offset
Definition: MILexer.h:89
@ kw_dereferenceable
Definition: MILexer.h:55
@ kw_implicit_define
Definition: MILexer.h:52
@ kw_cfi_def_cfa
Definition: MILexer.h:91
@ kw_cfi_escape
Definition: MILexer.h:90
@ VirtualRegister
Definition: MILexer.h:176
@ kw_cfi_def_cfa_offset
Definition: MILexer.h:88
@ kw_machine_block_address_taken
Definition: MILexer.h:143
@ kw_renamable
Definition: MILexer.h:61
@ ExternalSymbol
Definition: MILexer.h:168
@ kw_unknown_size
Definition: MILexer.h:140
@ IntegerLiteral
Definition: MILexer.h:172
@ kw_cfi_remember_state
Definition: MILexer.h:94
@ kw_debug_instr_number
Definition: MILexer.h:82
@ kw_post_instr_symbol
Definition: MILexer.h:134
@ kw_cfi_restore_state
Definition: MILexer.h:96
@ kw_nofpexcept
Definition: MILexer.h:76
@ kw_ir_block_address_taken
Definition: MILexer.h:142
@ kw_unknown_address
Definition: MILexer.h:141
@ JumpTableIndex
Definition: MILexer.h:178
@ kw_shufflemask
Definition: MILexer.h:132
@ kw_debug_location
Definition: MILexer.h:81
@ kw_noconvergent
Definition: MILexer.h:145
@ kw_heap_alloc_marker
Definition: MILexer.h:135
MIToken & setIntegerValue(APSInt IntVal)
Definition: MILexer.cpp:79
MIToken & reset(TokenKind Kind, StringRef Range)
Definition: MILexer.cpp:62
bool isError() const
Definition: MILexer.h:206
MIToken & setOwnedStringValue(std::string StrVal)
Definition: MILexer.cpp:73
StringRef::iterator location() const
Definition: MILexer.h:235