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