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