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