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