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("noconvergent", MIToken::kw_noconvergent)
304 .Case("mmra", MIToken::kw_mmra)
305 .Case("lr-split", MIToken::kw_lr_split)
307}
308
309static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
310 if (!isalpha(C.peek()) && C.peek() != '_')
311 return std::nullopt;
312 auto Range = C;
313 while (isIdentifierChar(C.peek()))
314 C.advance();
315 auto Identifier = Range.upto(C);
316 Token.reset(getIdentifierKind(Identifier), Identifier)
317 .setStringValue(Identifier);
318 return C;
319}
320
321static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
322 ErrorCallbackType ErrorCallback) {
323 bool IsReference = C.remaining().starts_with("%bb.");
324 if (!IsReference && !C.remaining().starts_with("bb."))
325 return std::nullopt;
326 auto Range = C;
327 unsigned PrefixLength = IsReference ? 4 : 3;
328 C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
329 if (!isdigit(C.peek())) {
330 Token.reset(MIToken::Error, C.remaining());
331 ErrorCallback(C.location(), "expected a number after '%bb.'");
332 return C;
333 }
334 auto NumberRange = C;
335 while (isdigit(C.peek()))
336 C.advance();
337 StringRef Number = NumberRange.upto(C);
338 unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>'
339 // TODO: The format bb.<id>.<irname> is supported only when it's not a
340 // reference. Once we deprecate the format where the irname shows up, we
341 // should only lex forward if it is a reference.
342 if (C.peek() == '.') {
343 C.advance(); // Skip '.'
344 ++StringOffset;
345 // The name is quoted if it is not a plain identifier.
346 if (C.peek() == '"') {
347 Cursor R = lexStringConstant(C, ErrorCallback);
348 if (!R) {
349 ErrorCallback(C.location(),
350 "unable to parse quoted string from opening quote");
351 Token.reset(MIToken::Error, Range.remaining());
352 return Range;
353 }
356 Token.reset(Kind, Range.upto(R))
359 unescapeQuotedString(Range.upto(R).drop_front(StringOffset)));
360 return R;
361 }
362 while (isIdentifierChar(C.peek()))
363 C.advance();
364 }
365 Token.reset(IsReference ? MIToken::MachineBasicBlock
367 Range.upto(C))
369 .setStringValue(Range.upto(C).drop_front(StringOffset));
370 return C;
371}
372
373static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
374 MIToken::TokenKind Kind) {
375 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
376 return std::nullopt;
377 auto Range = C;
378 C.advance(Rule.size());
379 auto NumberRange = C;
380 while (isdigit(C.peek()))
381 C.advance();
382 Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
383 return C;
384}
385
386static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
387 MIToken::TokenKind Kind) {
388 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
389 return std::nullopt;
390 auto Range = C;
391 C.advance(Rule.size());
392 auto NumberRange = C;
393 while (isdigit(C.peek()))
394 C.advance();
395 StringRef Number = NumberRange.upto(C);
396 unsigned StringOffset = Rule.size() + Number.size();
397 if (C.peek() == '.') {
398 C.advance();
399 ++StringOffset;
400 while (isIdentifierChar(C.peek()))
401 C.advance();
402 }
403 Token.reset(Kind, Range.upto(C))
405 .setStringValue(Range.upto(C).drop_front(StringOffset));
406 return C;
407}
408
409static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
410 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
411}
412
413static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
414 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
415}
416
417static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
418 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
419}
420
421static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
422 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
423}
424
425static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
426 ErrorCallbackType ErrorCallback) {
427 const StringRef Rule = "%subreg.";
428 if (!C.remaining().starts_with(Rule))
429 return std::nullopt;
430 return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
431 ErrorCallback);
432}
433
434static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
435 ErrorCallbackType ErrorCallback) {
436 const StringRef Rule = "%ir-block.";
437 if (!C.remaining().starts_with(Rule))
438 return std::nullopt;
439 if (isdigit(C.peek(Rule.size())))
440 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
441 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
442}
443
444static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
445 ErrorCallbackType ErrorCallback) {
446 const StringRef Rule = "%ir.";
447 if (!C.remaining().starts_with(Rule))
448 return std::nullopt;
449 if (isdigit(C.peek(Rule.size())))
450 return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
451 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
452}
453
454static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
455 ErrorCallbackType ErrorCallback) {
456 if (C.peek() != '"')
457 return std::nullopt;
458 return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
459 ErrorCallback);
460}
461
462static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
463 auto Range = C;
464 C.advance(); // Skip '%'
465 auto NumberRange = C;
466 while (isdigit(C.peek()))
467 C.advance();
469 .setIntegerValue(APSInt(NumberRange.upto(C)));
470 return C;
471}
472
473/// Returns true for a character allowed in a register name.
474static bool isRegisterChar(char C) {
475 return isIdentifierChar(C) && C != '.';
476}
477
478static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
479 Cursor Range = C;
480 C.advance(); // Skip '%'
481 while (isRegisterChar(C.peek()))
482 C.advance();
484 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
485 return C;
486}
487
488static Cursor maybeLexRegister(Cursor C, MIToken &Token,
489 ErrorCallbackType ErrorCallback) {
490 if (C.peek() != '%' && C.peek() != '$')
491 return std::nullopt;
492
493 if (C.peek() == '%') {
494 if (isdigit(C.peek(1)))
495 return lexVirtualRegister(C, Token);
496
497 if (isRegisterChar(C.peek(1)))
498 return lexNamedVirtualRegister(C, Token);
499
500 return std::nullopt;
501 }
502
503 assert(C.peek() == '$');
504 auto Range = C;
505 C.advance(); // Skip '$'
506 while (isRegisterChar(C.peek()))
507 C.advance();
508 Token.reset(MIToken::NamedRegister, Range.upto(C))
509 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
510 return C;
511}
512
513static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
514 ErrorCallbackType ErrorCallback) {
515 if (C.peek() != '@')
516 return std::nullopt;
517 if (!isdigit(C.peek(1)))
518 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
519 ErrorCallback);
520 auto Range = C;
521 C.advance(1); // Skip the '@'
522 auto NumberRange = C;
523 while (isdigit(C.peek()))
524 C.advance();
525 Token.reset(MIToken::GlobalValue, Range.upto(C))
526 .setIntegerValue(APSInt(NumberRange.upto(C)));
527 return C;
528}
529
530static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
531 ErrorCallbackType ErrorCallback) {
532 if (C.peek() != '&')
533 return std::nullopt;
534 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
535 ErrorCallback);
536}
537
538static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
539 ErrorCallbackType ErrorCallback) {
540 const StringRef Rule = "<mcsymbol ";
541 if (!C.remaining().starts_with(Rule))
542 return std::nullopt;
543 auto Start = C;
544 C.advance(Rule.size());
545
546 // Try a simple unquoted name.
547 if (C.peek() != '"') {
548 while (isIdentifierChar(C.peek()))
549 C.advance();
550 StringRef String = Start.upto(C).drop_front(Rule.size());
551 if (C.peek() != '>') {
552 ErrorCallback(C.location(),
553 "expected the '<mcsymbol ...' to be closed by a '>'");
554 Token.reset(MIToken::Error, Start.remaining());
555 return Start;
556 }
557 C.advance();
558
559 Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
560 return C;
561 }
562
563 // Otherwise lex out a quoted name.
564 Cursor R = lexStringConstant(C, ErrorCallback);
565 if (!R) {
566 ErrorCallback(C.location(),
567 "unable to parse quoted string from opening quote");
568 Token.reset(MIToken::Error, Start.remaining());
569 return Start;
570 }
571 StringRef String = Start.upto(R).drop_front(Rule.size());
572 if (R.peek() != '>') {
573 ErrorCallback(R.location(),
574 "expected the '<mcsymbol ...' to be closed by a '>'");
575 Token.reset(MIToken::Error, Start.remaining());
576 return Start;
577 }
578 R.advance();
579
580 Token.reset(MIToken::MCSymbol, Start.upto(R))
582 return R;
583}
584
586 return C == 'H' || C == 'K' || C == 'L' || C == 'M' || C == 'R';
587}
588
589static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
590 C.advance();
591 // Skip over [0-9]*([eE][-+]?[0-9]+)?
592 while (isdigit(C.peek()))
593 C.advance();
594 if ((C.peek() == 'e' || C.peek() == 'E') &&
595 (isdigit(C.peek(1)) ||
596 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
597 C.advance(2);
598 while (isdigit(C.peek()))
599 C.advance();
600 }
602 return C;
603}
604
605static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
606 if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
607 return std::nullopt;
608 Cursor Range = C;
609 C.advance(2);
610 unsigned PrefLen = 2;
611 if (isValidHexFloatingPointPrefix(C.peek())) {
612 C.advance();
613 PrefLen++;
614 }
615 while (isxdigit(C.peek()))
616 C.advance();
617 StringRef StrVal = Range.upto(C);
618 if (StrVal.size() <= PrefLen)
619 return std::nullopt;
620 if (PrefLen == 2)
621 Token.reset(MIToken::HexLiteral, Range.upto(C));
622 else // It must be 3, which means that there was a floating-point prefix.
624 return C;
625}
626
627static Cursor maybeLexFloatHexBits(Cursor C, MIToken &Token) {
628 if (C.peek() != 'f')
629 return std::nullopt;
630 if (C.peek(1) != '0' || (C.peek(2) != 'x' && C.peek(2) != 'X'))
631 return std::nullopt;
632 Cursor Range = C;
633 C.advance(3);
634 while (isxdigit(C.peek()))
635 C.advance();
636 StringRef StrVal = Range.upto(C);
637 if (StrVal.size() <= 3)
638 return std::nullopt;
640 return C;
641}
642
643static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
644 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
645 return std::nullopt;
646 auto Range = C;
647 C.advance();
648 while (isdigit(C.peek()))
649 C.advance();
650 if (C.peek() == '.')
651 return lexFloatingPointLiteral(Range, C, Token);
652 StringRef StrVal = Range.upto(C);
653 Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
654 return C;
655}
656
658 return StringSwitch<MIToken::TokenKind>(Identifier)
659 .Case("!tbaa", MIToken::md_tbaa)
660 .Case("!alias.scope", MIToken::md_alias_scope)
661 .Case("!noalias", MIToken::md_noalias)
662 .Case("!range", MIToken::md_range)
663 .Case("!mem.cache_hint", MIToken::md_mem_cache_hint)
664 .Case("!DIExpression", MIToken::md_diexpr)
665 .Case("!DILocation", MIToken::md_dilocation)
666 .Case("!noalias.addrspace", MIToken::md_noalias_addrspace)
668}
669
670static Cursor maybeLexExclaim(Cursor C, MIToken &Token,
671 ErrorCallbackType ErrorCallback) {
672 if (C.peek() != '!')
673 return std::nullopt;
674 auto Range = C;
675 C.advance(1);
676 if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
677 Token.reset(MIToken::exclaim, Range.upto(C));
678 return C;
679 }
680 while (isIdentifierChar(C.peek()))
681 C.advance();
682 StringRef StrVal = Range.upto(C);
683 Token.reset(getMetadataKeywordKind(StrVal), StrVal);
684 if (Token.isError())
685 ErrorCallback(Token.location(),
686 "use of unknown metadata keyword '" + StrVal + "'");
687 return C;
688}
689
691 switch (C) {
692 case ',':
693 return MIToken::comma;
694 case '.':
695 return MIToken::dot;
696 case '=':
697 return MIToken::equal;
698 case ':':
699 return MIToken::colon;
700 case '(':
701 return MIToken::lparen;
702 case ')':
703 return MIToken::rparen;
704 case '{':
705 return MIToken::lbrace;
706 case '}':
707 return MIToken::rbrace;
708 case '+':
709 return MIToken::plus;
710 case '-':
711 return MIToken::minus;
712 case '<':
713 return MIToken::less;
714 case '>':
715 return MIToken::greater;
716 default:
717 return MIToken::Error;
718 }
719}
720
721static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
723 unsigned Length = 1;
724 if (C.peek() == ':' && C.peek(1) == ':') {
725 Kind = MIToken::coloncolon;
726 Length = 2;
727 } else
728 Kind = symbolToken(C.peek());
729 if (Kind == MIToken::Error)
730 return std::nullopt;
731 auto Range = C;
732 C.advance(Length);
733 Token.reset(Kind, Range.upto(C));
734 return C;
735}
736
737static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
738 if (!isNewlineChar(C.peek()))
739 return std::nullopt;
740 auto Range = C;
741 C.advance();
742 Token.reset(MIToken::Newline, Range.upto(C));
743 return C;
744}
745
746static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
747 ErrorCallbackType ErrorCallback) {
748 if (C.peek() != '`')
749 return std::nullopt;
750 auto Range = C;
751 C.advance();
752 auto StrRange = C;
753 while (C.peek() != '`') {
754 if (C.isEOF() || isNewlineChar(C.peek())) {
755 ErrorCallback(
756 C.location(),
757 "end of machine instruction reached before the closing '`'");
758 Token.reset(MIToken::Error, Range.remaining());
759 return C;
760 }
761 C.advance();
762 }
763 StringRef Value = StrRange.upto(C);
764 C.advance();
766 return C;
767}
768
770 ErrorCallbackType ErrorCallback) {
771 auto C = skipComment(skipWhitespace(Cursor(Source)));
772 if (C.isEOF()) {
773 Token.reset(MIToken::Eof, C.remaining());
774 return C.remaining();
775 }
776
778
779 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
780 return R.remaining();
781 if (Cursor R = maybeLexFloatHexBits(C, Token))
782 return R.remaining();
783 if (Cursor R = maybeLexIdentifier(C, Token))
784 return R.remaining();
785 if (Cursor R = maybeLexJumpTableIndex(C, Token))
786 return R.remaining();
787 if (Cursor R = maybeLexStackObject(C, Token))
788 return R.remaining();
789 if (Cursor R = maybeLexFixedStackObject(C, Token))
790 return R.remaining();
791 if (Cursor R = maybeLexConstantPoolItem(C, Token))
792 return R.remaining();
793 if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
794 return R.remaining();
795 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
796 return R.remaining();
797 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
798 return R.remaining();
799 if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
800 return R.remaining();
801 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
802 return R.remaining();
803 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
804 return R.remaining();
805 if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
806 return R.remaining();
807 if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
808 return R.remaining();
809 if (Cursor R = maybeLexNumericalLiteral(C, Token))
810 return R.remaining();
811 if (Cursor R = maybeLexExclaim(C, Token, ErrorCallback))
812 return R.remaining();
813 if (Cursor R = maybeLexSymbol(C, Token))
814 return R.remaining();
815 if (Cursor R = maybeLexNewline(C, Token))
816 return R.remaining();
817 if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
818 return R.remaining();
819 if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
820 return R.remaining();
821
822 Token.reset(MIToken::Error, C.remaining());
823 ErrorCallback(C.location(),
824 Twine("unexpected character '") + Twine(C.peek()) + "'");
825 return C.remaining();
826}
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:746
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:474
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:690
static bool isValidHexFloatingPointPrefix(char C)
Definition MILexer.cpp:585
static MIToken::TokenKind getIdentifierKind(StringRef Identifier)
Definition MILexer.cpp:190
static Cursor maybeLexIRBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:434
static Cursor maybeLexSymbol(Cursor C, MIToken &Token)
Definition MILexer.cpp:721
static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token)
Definition MILexer.cpp:409
static Cursor maybeLexRegister(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:488
static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token)
Definition MILexer.cpp:605
static Cursor lexVirtualRegister(Cursor C, MIToken &Token)
Definition MILexer.cpp:462
static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token)
Definition MILexer.cpp:643
static Cursor maybeLexExclaim(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:670
static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token)
Definition MILexer.cpp:478
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:386
static Cursor maybeLexNewline(Cursor C, MIToken &Token)
Definition MILexer.cpp:737
static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:538
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:657
static Cursor maybeLexIdentifier(Cursor C, MIToken &Token)
Definition MILexer.cpp:309
static Cursor maybeLexStackObject(Cursor C, MIToken &Token)
Definition MILexer.cpp:413
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:530
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:513
static Cursor maybeLexIRValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:444
static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token)
Definition MILexer.cpp:417
static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:321
static Cursor maybeLexStringConstant(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:454
static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:425
static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token)
Definition MILexer.cpp:589
static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token)
Definition MILexer.cpp:421
static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, MIToken::TokenKind Kind)
Definition MILexer.cpp:373
static Cursor maybeLexFloatHexBits(Cursor C, MIToken &Token)
Definition MILexer.cpp:627
function_ref< bool(StringRef::iterator Loc, const Twine &)> ErrorCallbackType
Definition MIParser.cpp:605
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:177
@ kw_dbg_instr_ref
Definition MILexer.h:85
@ NamedVirtualRegister
Definition MILexer.h:175
@ kw_early_clobber
Definition MILexer.h:59
@ kw_unpredictable
Definition MILexer.h:77
@ FloatingPointLiteral
Definition MILexer.h:187
@ 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:176
@ 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_cfi_def_cfa_offset
Definition MILexer.h:90
@ md_mem_cache_hint
Definition MILexer.h:168
@ 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:166
@ 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:220
MIToken & setOwnedStringValue(std::string StrVal)
Definition MILexer.cpp:73
StringRef::iterator location() const
Definition MILexer.h:249