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