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