LLVM 23.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("ehscope-entry", MIToken::kw_ehscope_entry)
275 .Case("ehfunclet-entry", MIToken::kw_ehfunclet_entry)
276 .Case("liveins", MIToken::kw_liveins)
277 .Case("successors", MIToken::kw_successors)
278 .Case("floatpred", MIToken::kw_floatpred)
279 .Case("intpred", MIToken::kw_intpred)
280 .Case("shufflemask", MIToken::kw_shufflemask)
281 .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol)
282 .Case("post-instr-symbol", MIToken::kw_post_instr_symbol)
283 .Case("heap-alloc-marker", MIToken::kw_heap_alloc_marker)
284 .Case("pcsections", MIToken::kw_pcsections)
285 .Case("cfi-type", MIToken::kw_cfi_type)
286 .Case("deactivation-symbol", MIToken::kw_deactivation_symbol)
287 .Case("bbsections", MIToken::kw_bbsections)
288 .Case("bb_id", MIToken::kw_bb_id)
289 .Case("unknown-size", MIToken::kw_unknown_size)
290 .Case("unknown-address", MIToken::kw_unknown_address)
291 .Case("distinct", MIToken::kw_distinct)
292 .Case("ir-block-address-taken", MIToken::kw_ir_block_address_taken)
293 .Case("machine-block-address-taken",
295 .Case("call-frame-size", MIToken::kw_call_frame_size)
296 .Case("noconvergent", MIToken::kw_noconvergent)
298}
299
300static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
301 if (!isalpha(C.peek()) && C.peek() != '_')
302 return std::nullopt;
303 auto Range = C;
304 while (isIdentifierChar(C.peek()))
305 C.advance();
306 auto Identifier = Range.upto(C);
307 Token.reset(getIdentifierKind(Identifier), Identifier)
308 .setStringValue(Identifier);
309 return C;
310}
311
312static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
313 ErrorCallbackType ErrorCallback) {
314 bool IsReference = C.remaining().starts_with("%bb.");
315 if (!IsReference && !C.remaining().starts_with("bb."))
316 return std::nullopt;
317 auto Range = C;
318 unsigned PrefixLength = IsReference ? 4 : 3;
319 C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
320 if (!isdigit(C.peek())) {
321 Token.reset(MIToken::Error, C.remaining());
322 ErrorCallback(C.location(), "expected a number after '%bb.'");
323 return C;
324 }
325 auto NumberRange = C;
326 while (isdigit(C.peek()))
327 C.advance();
328 StringRef Number = NumberRange.upto(C);
329 unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>'
330 // TODO: The format bb.<id>.<irname> is supported only when it's not a
331 // reference. Once we deprecate the format where the irname shows up, we
332 // should only lex forward if it is a reference.
333 if (C.peek() == '.') {
334 C.advance(); // Skip '.'
335 ++StringOffset;
336 while (isIdentifierChar(C.peek()))
337 C.advance();
338 }
339 Token.reset(IsReference ? MIToken::MachineBasicBlock
341 Range.upto(C))
343 .setStringValue(Range.upto(C).drop_front(StringOffset));
344 return C;
345}
346
347static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
348 MIToken::TokenKind Kind) {
349 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
350 return std::nullopt;
351 auto Range = C;
352 C.advance(Rule.size());
353 auto NumberRange = C;
354 while (isdigit(C.peek()))
355 C.advance();
356 Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
357 return C;
358}
359
360static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
361 MIToken::TokenKind Kind) {
362 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
363 return std::nullopt;
364 auto Range = C;
365 C.advance(Rule.size());
366 auto NumberRange = C;
367 while (isdigit(C.peek()))
368 C.advance();
369 StringRef Number = NumberRange.upto(C);
370 unsigned StringOffset = Rule.size() + Number.size();
371 if (C.peek() == '.') {
372 C.advance();
373 ++StringOffset;
374 while (isIdentifierChar(C.peek()))
375 C.advance();
376 }
377 Token.reset(Kind, Range.upto(C))
379 .setStringValue(Range.upto(C).drop_front(StringOffset));
380 return C;
381}
382
383static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
384 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
385}
386
387static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
388 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
389}
390
391static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
392 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
393}
394
395static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
396 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
397}
398
399static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
400 ErrorCallbackType ErrorCallback) {
401 const StringRef Rule = "%subreg.";
402 if (!C.remaining().starts_with(Rule))
403 return std::nullopt;
404 return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
405 ErrorCallback);
406}
407
408static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
409 ErrorCallbackType ErrorCallback) {
410 const StringRef Rule = "%ir-block.";
411 if (!C.remaining().starts_with(Rule))
412 return std::nullopt;
413 if (isdigit(C.peek(Rule.size())))
414 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
415 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
416}
417
418static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
419 ErrorCallbackType ErrorCallback) {
420 const StringRef Rule = "%ir.";
421 if (!C.remaining().starts_with(Rule))
422 return std::nullopt;
423 if (isdigit(C.peek(Rule.size())))
424 return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
425 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
426}
427
428static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
429 ErrorCallbackType ErrorCallback) {
430 if (C.peek() != '"')
431 return std::nullopt;
432 return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
433 ErrorCallback);
434}
435
436static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
437 auto Range = C;
438 C.advance(); // Skip '%'
439 auto NumberRange = C;
440 while (isdigit(C.peek()))
441 C.advance();
443 .setIntegerValue(APSInt(NumberRange.upto(C)));
444 return C;
445}
446
447/// Returns true for a character allowed in a register name.
448static bool isRegisterChar(char C) {
449 return isIdentifierChar(C) && C != '.';
450}
451
452static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
453 Cursor Range = C;
454 C.advance(); // Skip '%'
455 while (isRegisterChar(C.peek()))
456 C.advance();
458 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
459 return C;
460}
461
462static Cursor maybeLexRegister(Cursor C, MIToken &Token,
463 ErrorCallbackType ErrorCallback) {
464 if (C.peek() != '%' && C.peek() != '$')
465 return std::nullopt;
466
467 if (C.peek() == '%') {
468 if (isdigit(C.peek(1)))
469 return lexVirtualRegister(C, Token);
470
471 if (isRegisterChar(C.peek(1)))
472 return lexNamedVirtualRegister(C, Token);
473
474 return std::nullopt;
475 }
476
477 assert(C.peek() == '$');
478 auto Range = C;
479 C.advance(); // Skip '$'
480 while (isRegisterChar(C.peek()))
481 C.advance();
482 Token.reset(MIToken::NamedRegister, Range.upto(C))
483 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
484 return C;
485}
486
487static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
488 ErrorCallbackType ErrorCallback) {
489 if (C.peek() != '@')
490 return std::nullopt;
491 if (!isdigit(C.peek(1)))
492 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
493 ErrorCallback);
494 auto Range = C;
495 C.advance(1); // Skip the '@'
496 auto NumberRange = C;
497 while (isdigit(C.peek()))
498 C.advance();
499 Token.reset(MIToken::GlobalValue, Range.upto(C))
500 .setIntegerValue(APSInt(NumberRange.upto(C)));
501 return C;
502}
503
504static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
505 ErrorCallbackType ErrorCallback) {
506 if (C.peek() != '&')
507 return std::nullopt;
508 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
509 ErrorCallback);
510}
511
512static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
513 ErrorCallbackType ErrorCallback) {
514 const StringRef Rule = "<mcsymbol ";
515 if (!C.remaining().starts_with(Rule))
516 return std::nullopt;
517 auto Start = C;
518 C.advance(Rule.size());
519
520 // Try a simple unquoted name.
521 if (C.peek() != '"') {
522 while (isIdentifierChar(C.peek()))
523 C.advance();
524 StringRef String = Start.upto(C).drop_front(Rule.size());
525 if (C.peek() != '>') {
526 ErrorCallback(C.location(),
527 "expected the '<mcsymbol ...' to be closed by a '>'");
528 Token.reset(MIToken::Error, Start.remaining());
529 return Start;
530 }
531 C.advance();
532
533 Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
534 return C;
535 }
536
537 // Otherwise lex out a quoted name.
538 Cursor R = lexStringConstant(C, ErrorCallback);
539 if (!R) {
540 ErrorCallback(C.location(),
541 "unable to parse quoted string from opening quote");
542 Token.reset(MIToken::Error, Start.remaining());
543 return Start;
544 }
545 StringRef String = Start.upto(R).drop_front(Rule.size());
546 if (R.peek() != '>') {
547 ErrorCallback(R.location(),
548 "expected the '<mcsymbol ...' to be closed by a '>'");
549 Token.reset(MIToken::Error, Start.remaining());
550 return Start;
551 }
552 R.advance();
553
554 Token.reset(MIToken::MCSymbol, Start.upto(R))
556 return R;
557}
558
560 return C == 'H' || C == 'K' || C == 'L' || C == 'M' || C == 'R';
561}
562
563static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
564 C.advance();
565 // Skip over [0-9]*([eE][-+]?[0-9]+)?
566 while (isdigit(C.peek()))
567 C.advance();
568 if ((C.peek() == 'e' || C.peek() == 'E') &&
569 (isdigit(C.peek(1)) ||
570 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
571 C.advance(2);
572 while (isdigit(C.peek()))
573 C.advance();
574 }
576 return C;
577}
578
579static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
580 if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
581 return std::nullopt;
582 Cursor Range = C;
583 C.advance(2);
584 unsigned PrefLen = 2;
585 if (isValidHexFloatingPointPrefix(C.peek())) {
586 C.advance();
587 PrefLen++;
588 }
589 while (isxdigit(C.peek()))
590 C.advance();
591 StringRef StrVal = Range.upto(C);
592 if (StrVal.size() <= PrefLen)
593 return std::nullopt;
594 if (PrefLen == 2)
595 Token.reset(MIToken::HexLiteral, Range.upto(C));
596 else // It must be 3, which means that there was a floating-point prefix.
598 return C;
599}
600
601static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
602 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
603 return std::nullopt;
604 auto Range = C;
605 C.advance();
606 while (isdigit(C.peek()))
607 C.advance();
608 if (C.peek() == '.')
609 return lexFloatingPointLiteral(Range, C, Token);
610 StringRef StrVal = Range.upto(C);
611 Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
612 return C;
613}
614
616 return StringSwitch<MIToken::TokenKind>(Identifier)
617 .Case("!tbaa", MIToken::md_tbaa)
618 .Case("!alias.scope", MIToken::md_alias_scope)
619 .Case("!noalias", MIToken::md_noalias)
620 .Case("!range", MIToken::md_range)
621 .Case("!DIExpression", MIToken::md_diexpr)
622 .Case("!DILocation", MIToken::md_dilocation)
623 .Case("!noalias.addrspace", MIToken::md_noalias_addrspace)
625}
626
627static Cursor maybeLexExclaim(Cursor C, MIToken &Token,
628 ErrorCallbackType ErrorCallback) {
629 if (C.peek() != '!')
630 return std::nullopt;
631 auto Range = C;
632 C.advance(1);
633 if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
634 Token.reset(MIToken::exclaim, Range.upto(C));
635 return C;
636 }
637 while (isIdentifierChar(C.peek()))
638 C.advance();
639 StringRef StrVal = Range.upto(C);
640 Token.reset(getMetadataKeywordKind(StrVal), StrVal);
641 if (Token.isError())
642 ErrorCallback(Token.location(),
643 "use of unknown metadata keyword '" + StrVal + "'");
644 return C;
645}
646
648 switch (C) {
649 case ',':
650 return MIToken::comma;
651 case '.':
652 return MIToken::dot;
653 case '=':
654 return MIToken::equal;
655 case ':':
656 return MIToken::colon;
657 case '(':
658 return MIToken::lparen;
659 case ')':
660 return MIToken::rparen;
661 case '{':
662 return MIToken::lbrace;
663 case '}':
664 return MIToken::rbrace;
665 case '+':
666 return MIToken::plus;
667 case '-':
668 return MIToken::minus;
669 case '<':
670 return MIToken::less;
671 case '>':
672 return MIToken::greater;
673 default:
674 return MIToken::Error;
675 }
676}
677
678static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
680 unsigned Length = 1;
681 if (C.peek() == ':' && C.peek(1) == ':') {
682 Kind = MIToken::coloncolon;
683 Length = 2;
684 } else
685 Kind = symbolToken(C.peek());
686 if (Kind == MIToken::Error)
687 return std::nullopt;
688 auto Range = C;
689 C.advance(Length);
690 Token.reset(Kind, Range.upto(C));
691 return C;
692}
693
694static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
695 if (!isNewlineChar(C.peek()))
696 return std::nullopt;
697 auto Range = C;
698 C.advance();
699 Token.reset(MIToken::Newline, Range.upto(C));
700 return C;
701}
702
703static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
704 ErrorCallbackType ErrorCallback) {
705 if (C.peek() != '`')
706 return std::nullopt;
707 auto Range = C;
708 C.advance();
709 auto StrRange = C;
710 while (C.peek() != '`') {
711 if (C.isEOF() || isNewlineChar(C.peek())) {
712 ErrorCallback(
713 C.location(),
714 "end of machine instruction reached before the closing '`'");
715 Token.reset(MIToken::Error, Range.remaining());
716 return C;
717 }
718 C.advance();
719 }
720 StringRef Value = StrRange.upto(C);
721 C.advance();
723 return C;
724}
725
727 ErrorCallbackType ErrorCallback) {
728 auto C = skipComment(skipWhitespace(Cursor(Source)));
729 if (C.isEOF()) {
730 Token.reset(MIToken::Eof, C.remaining());
731 return C.remaining();
732 }
733
735
736 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
737 return R.remaining();
738 if (Cursor R = maybeLexIdentifier(C, Token))
739 return R.remaining();
740 if (Cursor R = maybeLexJumpTableIndex(C, Token))
741 return R.remaining();
742 if (Cursor R = maybeLexStackObject(C, Token))
743 return R.remaining();
744 if (Cursor R = maybeLexFixedStackObject(C, Token))
745 return R.remaining();
746 if (Cursor R = maybeLexConstantPoolItem(C, Token))
747 return R.remaining();
748 if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
749 return R.remaining();
750 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
751 return R.remaining();
752 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
753 return R.remaining();
754 if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
755 return R.remaining();
756 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
757 return R.remaining();
758 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
759 return R.remaining();
760 if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
761 return R.remaining();
762 if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
763 return R.remaining();
764 if (Cursor R = maybeLexNumericalLiteral(C, Token))
765 return R.remaining();
766 if (Cursor R = maybeLexExclaim(C, Token, ErrorCallback))
767 return R.remaining();
768 if (Cursor R = maybeLexSymbol(C, Token))
769 return R.remaining();
770 if (Cursor R = maybeLexNewline(C, Token))
771 return R.remaining();
772 if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
773 return R.remaining();
774 if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
775 return R.remaining();
776
777 Token.reset(MIToken::Error, C.remaining());
778 ErrorCallback(C.location(),
779 Twine("unexpected character '") + Twine(C.peek()) + "'");
780 return C.remaining();
781}
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:703
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:448
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:647
static bool isValidHexFloatingPointPrefix(char C)
Definition MILexer.cpp:559
static MIToken::TokenKind getIdentifierKind(StringRef Identifier)
Definition MILexer.cpp:190
static Cursor maybeLexIRBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:408
static Cursor maybeLexSymbol(Cursor C, MIToken &Token)
Definition MILexer.cpp:678
static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token)
Definition MILexer.cpp:383
static Cursor maybeLexRegister(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:462
static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token)
Definition MILexer.cpp:579
static Cursor lexVirtualRegister(Cursor C, MIToken &Token)
Definition MILexer.cpp:436
static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token)
Definition MILexer.cpp:601
static Cursor maybeLexExclaim(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:627
static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token)
Definition MILexer.cpp:452
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:360
static Cursor maybeLexNewline(Cursor C, MIToken &Token)
Definition MILexer.cpp:694
static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:512
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:615
static Cursor maybeLexIdentifier(Cursor C, MIToken &Token)
Definition MILexer.cpp:300
static Cursor maybeLexStackObject(Cursor C, MIToken &Token)
Definition MILexer.cpp:387
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:504
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:487
static Cursor maybeLexIRValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:418
static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token)
Definition MILexer.cpp:391
static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:312
static Cursor maybeLexStringConstant(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:428
static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:399
static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token)
Definition MILexer.cpp:563
static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token)
Definition MILexer.cpp:395
static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, MIToken::TokenKind Kind)
Definition MILexer.cpp:347
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.
Definition Types.h:26
@ 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:136
@ kw_deactivation_symbol
Definition MILexer.h:141
@ kw_call_frame_size
Definition MILexer.h:148
@ 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:168
@ kw_dbg_instr_ref
Definition MILexer.h:84
@ NamedVirtualRegister
Definition MILexer.h:166
@ kw_early_clobber
Definition MILexer.h:59
@ kw_unpredictable
Definition MILexer.h:77
@ FloatingPointLiteral
Definition MILexer.h:178
@ 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:167
@ 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:130
@ 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:147
@ kw_cfi_remember_state
Definition MILexer.h:95
@ kw_debug_instr_number
Definition MILexer.h:83
@ kw_post_instr_symbol
Definition MILexer.h:137
@ kw_cfi_restore_state
Definition MILexer.h:97
@ kw_ir_block_address_taken
Definition MILexer.h:146
@ kw_unknown_address
Definition MILexer.h:145
@ md_noalias_addrspace
Definition MILexer.h:158
@ kw_debug_location
Definition MILexer.h:82
@ kw_heap_alloc_marker
Definition MILexer.h:138
MIToken & setIntegerValue(APSInt IntVal)
Definition MILexer.cpp:79
MIToken & reset(TokenKind Kind, StringRef Range)
Definition MILexer.cpp:62
bool isError() const
Definition MILexer.h:211
MIToken & setOwnedStringValue(std::string StrVal)
Definition MILexer.cpp:73
StringRef::iterator location() const
Definition MILexer.h:240