LLVM 23.0.0git
MILexer.cpp
Go to the documentation of this file.
1//===- MILexer.cpp - Machine instructions lexer implementation ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the lexing of machine instructions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MILexer.h"
16#include "llvm/ADT/Twine.h"
17#include <cassert>
18#include <cctype>
19#include <string>
20
21using namespace llvm;
22
23namespace {
24
27
28/// This class provides a way to iterate and get characters from the source
29/// string.
30class Cursor {
31 const char *Ptr = nullptr;
32 const char *End = nullptr;
33
34public:
35 Cursor(std::nullopt_t) {}
36
37 explicit Cursor(StringRef Str) {
38 Ptr = Str.data();
39 End = Ptr + Str.size();
40 }
41
42 bool isEOF() const { return Ptr == End; }
43
44 char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
45
46 void advance(unsigned I = 1) { Ptr += I; }
47
48 StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
49
50 StringRef upto(Cursor C) const {
51 assert(C.Ptr >= Ptr && C.Ptr <= End);
52 return StringRef(Ptr, C.Ptr - Ptr);
53 }
54
55 StringRef::iterator location() const { return Ptr; }
56
57 operator bool() const { return Ptr != nullptr; }
58};
59
60} // end anonymous namespace
61
63 this->Kind = Kind;
64 this->Range = Range;
65 return *this;
66}
67
69 StringValue = StrVal;
70 return *this;
71}
72
74 StringValueStorage = std::move(StrVal);
75 StringValue = StringValueStorage;
76 return *this;
77}
78
80 this->IntVal = std::move(IntVal);
81 return *this;
82}
83
84/// Skip the leading whitespace characters and return the updated cursor.
85static Cursor skipWhitespace(Cursor C) {
86 while (isblank(C.peek()))
87 C.advance();
88 return C;
89}
90
91static bool isNewlineChar(char C) { return C == '\n' || C == '\r'; }
92
93/// Skip a line comment and return the updated cursor.
94static Cursor skipComment(Cursor C) {
95 if (C.peek() != ';')
96 return C;
97 while (!isNewlineChar(C.peek()) && !C.isEOF())
98 C.advance();
99 return C;
100}
101
102/// Machine operands can have comments, enclosed between /* and */.
103/// This eats up all tokens, including /* and */.
104static Cursor skipMachineOperandComment(Cursor C) {
105 if (C.peek() != '/' || C.peek(1) != '*')
106 return C;
107
108 while (C.peek() != '*' || C.peek(1) != '/')
109 C.advance();
110
111 C.advance();
112 C.advance();
113 return C;
114}
115
116/// Return true if the given character satisfies the following regular
117/// expression: [-a-zA-Z$._0-9]
118static bool isIdentifierChar(char C) {
119 return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' ||
120 C == '$';
121}
122
123/// Unescapes the given string value.
124///
125/// Expects the string value to be quoted.
127 assert(Value.front() == '"' && Value.back() == '"');
128 Cursor C = Cursor(Value.substr(1, Value.size() - 2));
129
130 std::string Str;
131 Str.reserve(C.remaining().size());
132 while (!C.isEOF()) {
133 char Char = C.peek();
134 if (Char == '\\') {
135 if (C.peek(1) == '\\') {
136 // Two '\' become one
137 Str += '\\';
138 C.advance(2);
139 continue;
140 }
141 if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
142 Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
143 C.advance(3);
144 continue;
145 }
146 }
147 Str += Char;
148 C.advance();
149 }
150 return Str;
151}
152
153/// Lex a string constant using the following regular expression: \"[^\"]*\"
154static Cursor lexStringConstant(Cursor C, ErrorCallbackType ErrorCallback) {
155 assert(C.peek() == '"');
156 for (C.advance(); C.peek() != '"'; C.advance()) {
157 if (C.isEOF() || isNewlineChar(C.peek())) {
158 ErrorCallback(
159 C.location(),
160 "end of machine instruction reached before the closing '\"'");
161 return std::nullopt;
162 }
163 }
164 C.advance();
165 return C;
166}
167
168static Cursor lexName(Cursor C, MIToken &Token, MIToken::TokenKind Type,
169 unsigned PrefixLength, ErrorCallbackType ErrorCallback) {
170 auto Range = C;
171 C.advance(PrefixLength);
172 if (C.peek() == '"') {
173 if (Cursor R = lexStringConstant(C, ErrorCallback)) {
174 StringRef String = Range.upto(R);
175 Token.reset(Type, String)
177 unescapeQuotedString(String.drop_front(PrefixLength)));
178 return R;
179 }
180 Token.reset(MIToken::Error, Range.remaining());
181 return Range;
182 }
183 while (isIdentifierChar(C.peek()))
184 C.advance();
185 Token.reset(Type, Range.upto(C))
186 .setStringValue(Range.upto(C).drop_front(PrefixLength));
187 return C;
188}
189
191 return StringSwitch<MIToken::TokenKind>(Identifier)
193 .Case("implicit", MIToken::kw_implicit)
194 .Case("implicit-def", MIToken::kw_implicit_define)
195 .Case("def", MIToken::kw_def)
196 .Case("dead", MIToken::kw_dead)
197 .Case("killed", MIToken::kw_killed)
198 .Case("undef", MIToken::kw_undef)
199 .Case("internal", MIToken::kw_internal)
200 .Case("early-clobber", MIToken::kw_early_clobber)
201 .Case("debug-use", MIToken::kw_debug_use)
202 .Case("renamable", MIToken::kw_renamable)
203 .Case("tied-def", MIToken::kw_tied_def)
204 .Case("frame-setup", MIToken::kw_frame_setup)
205 .Case("frame-destroy", MIToken::kw_frame_destroy)
206 .Case("nnan", MIToken::kw_nnan)
207 .Case("ninf", MIToken::kw_ninf)
208 .Case("nsz", MIToken::kw_nsz)
209 .Case("arcp", MIToken::kw_arcp)
210 .Case("contract", MIToken::kw_contract)
211 .Case("afn", MIToken::kw_afn)
212 .Case("reassoc", MIToken::kw_reassoc)
213 .Case("nuw", MIToken::kw_nuw)
214 .Case("nsw", MIToken::kw_nsw)
215 .Case("nusw", MIToken::kw_nusw)
216 .Case("exact", MIToken::kw_exact)
217 .Case("nneg", MIToken::kw_nneg)
218 .Case("disjoint", MIToken::kw_disjoint)
219 .Case("samesign", MIToken::kw_samesign)
220 .Case("inbounds", MIToken::kw_inbounds)
221 .Case("nofpexcept", MIToken::kw_nofpexcept)
222 .Case("unpredictable", MIToken::kw_unpredictable)
223 .Case("debug-location", MIToken::kw_debug_location)
224 .Case("debug-instr-number", MIToken::kw_debug_instr_number)
225 .Case("dbg-instr-ref", MIToken::kw_dbg_instr_ref)
226 .Case("same_value", MIToken::kw_cfi_same_value)
227 .Case("offset", MIToken::kw_cfi_offset)
228 .Case("rel_offset", MIToken::kw_cfi_rel_offset)
229 .Case("def_cfa_register", MIToken::kw_cfi_def_cfa_register)
230 .Case("def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
231 .Case("adjust_cfa_offset", MIToken::kw_cfi_adjust_cfa_offset)
232 .Case("escape", MIToken::kw_cfi_escape)
233 .Case("def_cfa", MIToken::kw_cfi_def_cfa)
234 .Case("llvm_def_aspace_cfa", MIToken::kw_cfi_llvm_def_aspace_cfa)
235 .Case("remember_state", MIToken::kw_cfi_remember_state)
236 .Case("restore", MIToken::kw_cfi_restore)
237 .Case("restore_state", MIToken::kw_cfi_restore_state)
238 .Case("undefined", MIToken::kw_cfi_undefined)
239 .Case("register", MIToken::kw_cfi_register)
240 .Case("window_save", MIToken::kw_cfi_window_save)
241 .Case("negate_ra_sign_state",
243 .Case("negate_ra_sign_state_with_pc",
245 .Case("llvm_register_pair", MIToken::kw_cfi_llvm_register_pair)
246 .Case("llvm_vector_registers", MIToken::kw_cfi_llvm_vector_registers)
247 .Case("llvm_vector_offset", MIToken::kw_cfi_llvm_vector_offset)
248 .Case("llvm_vector_register_mask",
250 .Case("blockaddress", MIToken::kw_blockaddress)
251 .Case("intrinsic", MIToken::kw_intrinsic)
252 .Case("target-index", MIToken::kw_target_index)
253 .Case("half", MIToken::kw_half)
254 .Case("bfloat", MIToken::kw_bfloat)
255 .Case("float", MIToken::kw_float)
256 .Case("double", MIToken::kw_double)
257 .Case("x86_fp80", MIToken::kw_x86_fp80)
258 .Case("fp128", MIToken::kw_fp128)
259 .Case("ppc_fp128", MIToken::kw_ppc_fp128)
260 .Case("target-flags", MIToken::kw_target_flags)
261 .Case("volatile", MIToken::kw_volatile)
262 .Case("non-temporal", MIToken::kw_non_temporal)
263 .Case("dereferenceable", MIToken::kw_dereferenceable)
264 .Case("invariant", MIToken::kw_invariant)
265 .Case("align", MIToken::kw_align)
266 .Case("basealign", MIToken::kw_basealign)
267 .Case("addrspace", MIToken::kw_addrspace)
268 .Case("stack", MIToken::kw_stack)
269 .Case("got", MIToken::kw_got)
270 .Case("jump-table", MIToken::kw_jump_table)
271 .Case("constant-pool", MIToken::kw_constant_pool)
272 .Case("call-entry", MIToken::kw_call_entry)
273 .Case("custom", MIToken::kw_custom)
274 .Case("lanemask", MIToken::kw_lanemask)
275 .Case("liveout", MIToken::kw_liveout)
276 .Case("landing-pad", MIToken::kw_landing_pad)
277 .Case("inlineasm-br-indirect-target",
279 .Case("ehscope-entry", MIToken::kw_ehscope_entry)
280 .Case("ehfunclet-entry", MIToken::kw_ehfunclet_entry)
281 .Case("liveins", MIToken::kw_liveins)
282 .Case("successors", MIToken::kw_successors)
283 .Case("floatpred", MIToken::kw_floatpred)
284 .Case("intpred", MIToken::kw_intpred)
285 .Case("shufflemask", MIToken::kw_shufflemask)
286 .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol)
287 .Case("post-instr-symbol", MIToken::kw_post_instr_symbol)
288 .Case("heap-alloc-marker", MIToken::kw_heap_alloc_marker)
289 .Case("pcsections", MIToken::kw_pcsections)
290 .Case("cfi-type", MIToken::kw_cfi_type)
291 .Case("deactivation-symbol", MIToken::kw_deactivation_symbol)
292 .Case("bbsections", MIToken::kw_bbsections)
293 .Case("bb_id", MIToken::kw_bb_id)
294 .Case("unknown-size", MIToken::kw_unknown_size)
295 .Case("unknown-address", MIToken::kw_unknown_address)
296 .Case("distinct", MIToken::kw_distinct)
297 .Case("ir-block-address-taken", MIToken::kw_ir_block_address_taken)
298 .Case("machine-block-address-taken",
300 .Case("call-frame-size", MIToken::kw_call_frame_size)
301 .Case("noconvergent", MIToken::kw_noconvergent)
302 .Case("mmra", MIToken::kw_mmra)
304}
305
306static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
307 if (!isalpha(C.peek()) && C.peek() != '_')
308 return std::nullopt;
309 auto Range = C;
310 while (isIdentifierChar(C.peek()))
311 C.advance();
312 auto Identifier = Range.upto(C);
313 Token.reset(getIdentifierKind(Identifier), Identifier)
314 .setStringValue(Identifier);
315 return C;
316}
317
318static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
319 ErrorCallbackType ErrorCallback) {
320 bool IsReference = C.remaining().starts_with("%bb.");
321 if (!IsReference && !C.remaining().starts_with("bb."))
322 return std::nullopt;
323 auto Range = C;
324 unsigned PrefixLength = IsReference ? 4 : 3;
325 C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
326 if (!isdigit(C.peek())) {
327 Token.reset(MIToken::Error, C.remaining());
328 ErrorCallback(C.location(), "expected a number after '%bb.'");
329 return C;
330 }
331 auto NumberRange = C;
332 while (isdigit(C.peek()))
333 C.advance();
334 StringRef Number = NumberRange.upto(C);
335 unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>'
336 // TODO: The format bb.<id>.<irname> is supported only when it's not a
337 // reference. Once we deprecate the format where the irname shows up, we
338 // should only lex forward if it is a reference.
339 if (C.peek() == '.') {
340 C.advance(); // Skip '.'
341 ++StringOffset;
342 while (isIdentifierChar(C.peek()))
343 C.advance();
344 }
345 Token.reset(IsReference ? MIToken::MachineBasicBlock
347 Range.upto(C))
349 .setStringValue(Range.upto(C).drop_front(StringOffset));
350 return C;
351}
352
353static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
354 MIToken::TokenKind Kind) {
355 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
356 return std::nullopt;
357 auto Range = C;
358 C.advance(Rule.size());
359 auto NumberRange = C;
360 while (isdigit(C.peek()))
361 C.advance();
362 Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
363 return C;
364}
365
366static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
367 MIToken::TokenKind Kind) {
368 if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
369 return std::nullopt;
370 auto Range = C;
371 C.advance(Rule.size());
372 auto NumberRange = C;
373 while (isdigit(C.peek()))
374 C.advance();
375 StringRef Number = NumberRange.upto(C);
376 unsigned StringOffset = Rule.size() + Number.size();
377 if (C.peek() == '.') {
378 C.advance();
379 ++StringOffset;
380 while (isIdentifierChar(C.peek()))
381 C.advance();
382 }
383 Token.reset(Kind, Range.upto(C))
385 .setStringValue(Range.upto(C).drop_front(StringOffset));
386 return C;
387}
388
389static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
390 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
391}
392
393static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
394 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
395}
396
397static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
398 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
399}
400
401static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
402 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
403}
404
405static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
406 ErrorCallbackType ErrorCallback) {
407 const StringRef Rule = "%subreg.";
408 if (!C.remaining().starts_with(Rule))
409 return std::nullopt;
410 return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
411 ErrorCallback);
412}
413
414static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
415 ErrorCallbackType ErrorCallback) {
416 const StringRef Rule = "%ir-block.";
417 if (!C.remaining().starts_with(Rule))
418 return std::nullopt;
419 if (isdigit(C.peek(Rule.size())))
420 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
421 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
422}
423
424static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
425 ErrorCallbackType ErrorCallback) {
426 const StringRef Rule = "%ir.";
427 if (!C.remaining().starts_with(Rule))
428 return std::nullopt;
429 if (isdigit(C.peek(Rule.size())))
430 return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
431 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
432}
433
434static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
435 ErrorCallbackType ErrorCallback) {
436 if (C.peek() != '"')
437 return std::nullopt;
438 return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
439 ErrorCallback);
440}
441
442static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
443 auto Range = C;
444 C.advance(); // Skip '%'
445 auto NumberRange = C;
446 while (isdigit(C.peek()))
447 C.advance();
449 .setIntegerValue(APSInt(NumberRange.upto(C)));
450 return C;
451}
452
453/// Returns true for a character allowed in a register name.
454static bool isRegisterChar(char C) {
455 return isIdentifierChar(C) && C != '.';
456}
457
458static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
459 Cursor Range = C;
460 C.advance(); // Skip '%'
461 while (isRegisterChar(C.peek()))
462 C.advance();
464 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
465 return C;
466}
467
468static Cursor maybeLexRegister(Cursor C, MIToken &Token,
469 ErrorCallbackType ErrorCallback) {
470 if (C.peek() != '%' && C.peek() != '$')
471 return std::nullopt;
472
473 if (C.peek() == '%') {
474 if (isdigit(C.peek(1)))
475 return lexVirtualRegister(C, Token);
476
477 if (isRegisterChar(C.peek(1)))
478 return lexNamedVirtualRegister(C, Token);
479
480 return std::nullopt;
481 }
482
483 assert(C.peek() == '$');
484 auto Range = C;
485 C.advance(); // Skip '$'
486 while (isRegisterChar(C.peek()))
487 C.advance();
488 Token.reset(MIToken::NamedRegister, Range.upto(C))
489 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
490 return C;
491}
492
493static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
494 ErrorCallbackType ErrorCallback) {
495 if (C.peek() != '@')
496 return std::nullopt;
497 if (!isdigit(C.peek(1)))
498 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
499 ErrorCallback);
500 auto Range = C;
501 C.advance(1); // Skip the '@'
502 auto NumberRange = C;
503 while (isdigit(C.peek()))
504 C.advance();
505 Token.reset(MIToken::GlobalValue, Range.upto(C))
506 .setIntegerValue(APSInt(NumberRange.upto(C)));
507 return C;
508}
509
510static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
511 ErrorCallbackType ErrorCallback) {
512 if (C.peek() != '&')
513 return std::nullopt;
514 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
515 ErrorCallback);
516}
517
518static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
519 ErrorCallbackType ErrorCallback) {
520 const StringRef Rule = "<mcsymbol ";
521 if (!C.remaining().starts_with(Rule))
522 return std::nullopt;
523 auto Start = C;
524 C.advance(Rule.size());
525
526 // Try a simple unquoted name.
527 if (C.peek() != '"') {
528 while (isIdentifierChar(C.peek()))
529 C.advance();
530 StringRef String = Start.upto(C).drop_front(Rule.size());
531 if (C.peek() != '>') {
532 ErrorCallback(C.location(),
533 "expected the '<mcsymbol ...' to be closed by a '>'");
534 Token.reset(MIToken::Error, Start.remaining());
535 return Start;
536 }
537 C.advance();
538
539 Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
540 return C;
541 }
542
543 // Otherwise lex out a quoted name.
544 Cursor R = lexStringConstant(C, ErrorCallback);
545 if (!R) {
546 ErrorCallback(C.location(),
547 "unable to parse quoted string from opening quote");
548 Token.reset(MIToken::Error, Start.remaining());
549 return Start;
550 }
551 StringRef String = Start.upto(R).drop_front(Rule.size());
552 if (R.peek() != '>') {
553 ErrorCallback(R.location(),
554 "expected the '<mcsymbol ...' to be closed by a '>'");
555 Token.reset(MIToken::Error, Start.remaining());
556 return Start;
557 }
558 R.advance();
559
560 Token.reset(MIToken::MCSymbol, Start.upto(R))
562 return R;
563}
564
566 return C == 'H' || C == 'K' || C == 'L' || C == 'M' || C == 'R';
567}
568
569static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
570 C.advance();
571 // Skip over [0-9]*([eE][-+]?[0-9]+)?
572 while (isdigit(C.peek()))
573 C.advance();
574 if ((C.peek() == 'e' || C.peek() == 'E') &&
575 (isdigit(C.peek(1)) ||
576 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
577 C.advance(2);
578 while (isdigit(C.peek()))
579 C.advance();
580 }
582 return C;
583}
584
585static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
586 if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
587 return std::nullopt;
588 Cursor Range = C;
589 C.advance(2);
590 unsigned PrefLen = 2;
591 if (isValidHexFloatingPointPrefix(C.peek())) {
592 C.advance();
593 PrefLen++;
594 }
595 while (isxdigit(C.peek()))
596 C.advance();
597 StringRef StrVal = Range.upto(C);
598 if (StrVal.size() <= PrefLen)
599 return std::nullopt;
600 if (PrefLen == 2)
601 Token.reset(MIToken::HexLiteral, Range.upto(C));
602 else // It must be 3, which means that there was a floating-point prefix.
604 return C;
605}
606
607static Cursor maybeLexFloatHexBits(Cursor C, MIToken &Token) {
608 if (C.peek() != 'f')
609 return std::nullopt;
610 if (C.peek(1) != '0' || (C.peek(2) != 'x' && C.peek(2) != 'X'))
611 return std::nullopt;
612 Cursor Range = C;
613 C.advance(3);
614 while (isxdigit(C.peek()))
615 C.advance();
616 StringRef StrVal = Range.upto(C);
617 if (StrVal.size() <= 3)
618 return std::nullopt;
620 return C;
621}
622
623static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
624 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
625 return std::nullopt;
626 auto Range = C;
627 C.advance();
628 while (isdigit(C.peek()))
629 C.advance();
630 if (C.peek() == '.')
631 return lexFloatingPointLiteral(Range, C, Token);
632 StringRef StrVal = Range.upto(C);
633 Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
634 return C;
635}
636
638 return StringSwitch<MIToken::TokenKind>(Identifier)
639 .Case("!tbaa", MIToken::md_tbaa)
640 .Case("!alias.scope", MIToken::md_alias_scope)
641 .Case("!noalias", MIToken::md_noalias)
642 .Case("!range", MIToken::md_range)
643 .Case("!DIExpression", MIToken::md_diexpr)
644 .Case("!DILocation", MIToken::md_dilocation)
645 .Case("!noalias.addrspace", MIToken::md_noalias_addrspace)
647}
648
649static Cursor maybeLexExclaim(Cursor C, MIToken &Token,
650 ErrorCallbackType ErrorCallback) {
651 if (C.peek() != '!')
652 return std::nullopt;
653 auto Range = C;
654 C.advance(1);
655 if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
656 Token.reset(MIToken::exclaim, Range.upto(C));
657 return C;
658 }
659 while (isIdentifierChar(C.peek()))
660 C.advance();
661 StringRef StrVal = Range.upto(C);
662 Token.reset(getMetadataKeywordKind(StrVal), StrVal);
663 if (Token.isError())
664 ErrorCallback(Token.location(),
665 "use of unknown metadata keyword '" + StrVal + "'");
666 return C;
667}
668
670 switch (C) {
671 case ',':
672 return MIToken::comma;
673 case '.':
674 return MIToken::dot;
675 case '=':
676 return MIToken::equal;
677 case ':':
678 return MIToken::colon;
679 case '(':
680 return MIToken::lparen;
681 case ')':
682 return MIToken::rparen;
683 case '{':
684 return MIToken::lbrace;
685 case '}':
686 return MIToken::rbrace;
687 case '+':
688 return MIToken::plus;
689 case '-':
690 return MIToken::minus;
691 case '<':
692 return MIToken::less;
693 case '>':
694 return MIToken::greater;
695 default:
696 return MIToken::Error;
697 }
698}
699
700static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
702 unsigned Length = 1;
703 if (C.peek() == ':' && C.peek(1) == ':') {
704 Kind = MIToken::coloncolon;
705 Length = 2;
706 } else
707 Kind = symbolToken(C.peek());
708 if (Kind == MIToken::Error)
709 return std::nullopt;
710 auto Range = C;
711 C.advance(Length);
712 Token.reset(Kind, Range.upto(C));
713 return C;
714}
715
716static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
717 if (!isNewlineChar(C.peek()))
718 return std::nullopt;
719 auto Range = C;
720 C.advance();
721 Token.reset(MIToken::Newline, Range.upto(C));
722 return C;
723}
724
725static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
726 ErrorCallbackType ErrorCallback) {
727 if (C.peek() != '`')
728 return std::nullopt;
729 auto Range = C;
730 C.advance();
731 auto StrRange = C;
732 while (C.peek() != '`') {
733 if (C.isEOF() || isNewlineChar(C.peek())) {
734 ErrorCallback(
735 C.location(),
736 "end of machine instruction reached before the closing '`'");
737 Token.reset(MIToken::Error, Range.remaining());
738 return C;
739 }
740 C.advance();
741 }
742 StringRef Value = StrRange.upto(C);
743 C.advance();
745 return C;
746}
747
749 ErrorCallbackType ErrorCallback) {
750 auto C = skipComment(skipWhitespace(Cursor(Source)));
751 if (C.isEOF()) {
752 Token.reset(MIToken::Eof, C.remaining());
753 return C.remaining();
754 }
755
757
758 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
759 return R.remaining();
760 if (Cursor R = maybeLexFloatHexBits(C, Token))
761 return R.remaining();
762 if (Cursor R = maybeLexIdentifier(C, Token))
763 return R.remaining();
764 if (Cursor R = maybeLexJumpTableIndex(C, Token))
765 return R.remaining();
766 if (Cursor R = maybeLexStackObject(C, Token))
767 return R.remaining();
768 if (Cursor R = maybeLexFixedStackObject(C, Token))
769 return R.remaining();
770 if (Cursor R = maybeLexConstantPoolItem(C, Token))
771 return R.remaining();
772 if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
773 return R.remaining();
774 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
775 return R.remaining();
776 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
777 return R.remaining();
778 if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
779 return R.remaining();
780 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
781 return R.remaining();
782 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
783 return R.remaining();
784 if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
785 return R.remaining();
786 if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
787 return R.remaining();
788 if (Cursor R = maybeLexNumericalLiteral(C, Token))
789 return R.remaining();
790 if (Cursor R = maybeLexExclaim(C, Token, ErrorCallback))
791 return R.remaining();
792 if (Cursor R = maybeLexSymbol(C, Token))
793 return R.remaining();
794 if (Cursor R = maybeLexNewline(C, Token))
795 return R.remaining();
796 if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
797 return R.remaining();
798 if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
799 return R.remaining();
800
801 Token.reset(MIToken::Error, C.remaining());
802 ErrorCallback(C.location(),
803 Twine("unexpected character '") + Twine(C.peek()) + "'");
804 return C.remaining();
805}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define I(x, y, z)
Definition MD5.cpp:57
static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:725
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:454
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:669
static bool isValidHexFloatingPointPrefix(char C)
Definition MILexer.cpp:565
static MIToken::TokenKind getIdentifierKind(StringRef Identifier)
Definition MILexer.cpp:190
static Cursor maybeLexIRBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:414
static Cursor maybeLexSymbol(Cursor C, MIToken &Token)
Definition MILexer.cpp:700
static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token)
Definition MILexer.cpp:389
static Cursor maybeLexRegister(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:468
static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token)
Definition MILexer.cpp:585
static Cursor lexVirtualRegister(Cursor C, MIToken &Token)
Definition MILexer.cpp:442
static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token)
Definition MILexer.cpp:623
static Cursor maybeLexExclaim(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:649
static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token)
Definition MILexer.cpp:458
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:366
static Cursor maybeLexNewline(Cursor C, MIToken &Token)
Definition MILexer.cpp:716
static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:518
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:637
static Cursor maybeLexIdentifier(Cursor C, MIToken &Token)
Definition MILexer.cpp:306
static Cursor maybeLexStackObject(Cursor C, MIToken &Token)
Definition MILexer.cpp:393
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:510
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:493
static Cursor maybeLexIRValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:424
static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token)
Definition MILexer.cpp:397
static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:318
static Cursor maybeLexStringConstant(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:434
static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition MILexer.cpp:405
static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token)
Definition MILexer.cpp:569
static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token)
Definition MILexer.cpp:401
static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, MIToken::TokenKind Kind)
Definition MILexer.cpp:353
static Cursor maybeLexFloatHexBits(Cursor C, MIToken &Token)
Definition MILexer.cpp:607
function_ref< bool(StringRef::iterator Loc, const Twine &)> ErrorCallbackType
Definition MIParser.cpp:628
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file contains some functions that are useful when dealing with strings.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static bool peek(struct InternalInstruction *insn, uint8_t &byte)
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
const char * iterator
Definition StringRef.h:60
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LocationClass< Ty > location(Ty &L)
This is an optimization pass for GlobalISel generic memory operations.
@ Length
Definition DWP.cpp:557
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
StringRef lexMIToken(StringRef Source, MIToken &Token, function_ref< void(StringRef::iterator, const Twine &)> ErrorCallback)
Consume a single machine instruction token in the given source and return the remaining source string...
A token produced by the machine instruction lexer.
Definition MILexer.h:26
MIToken & setStringValue(StringRef StrVal)
Definition MILexer.cpp:68
MIToken()=default
@ kw_pre_instr_symbol
Definition MILexer.h:140
@ kw_deactivation_symbol
Definition MILexer.h:145
@ kw_call_frame_size
Definition MILexer.h:152
@ kw_cfi_aarch64_negate_ra_sign_state
Definition MILexer.h:100
@ kw_cfi_llvm_def_aspace_cfa
Definition MILexer.h:93
@ MachineBasicBlock
Definition MILexer.h:173
@ kw_dbg_instr_ref
Definition MILexer.h:84
@ NamedVirtualRegister
Definition MILexer.h:171
@ kw_early_clobber
Definition MILexer.h:59
@ kw_unpredictable
Definition MILexer.h:77
@ FloatingPointLiteral
Definition MILexer.h:183
@ kw_cfi_window_save
Definition MILexer.h:99
@ kw_cfi_llvm_register_pair
Definition MILexer.h:102
@ kw_frame_destroy
Definition MILexer.h:64
@ kw_cfi_undefined
Definition MILexer.h:98
@ MachineBasicBlockLabel
Definition MILexer.h:172
@ kw_cfi_llvm_vector_offset
Definition MILexer.h:104
@ kw_cfi_register
Definition MILexer.h:94
@ kw_inlineasm_br_indirect_target
Definition MILexer.h:132
@ kw_cfi_rel_offset
Definition MILexer.h:87
@ kw_cfi_llvm_vector_registers
Definition MILexer.h:103
@ kw_ehfunclet_entry
Definition MILexer.h:134
@ kw_cfi_llvm_vector_register_mask
Definition MILexer.h:105
@ kw_cfi_aarch64_negate_ra_sign_state_with_pc
Definition MILexer.h:101
@ kw_cfi_def_cfa_register
Definition MILexer.h:88
@ kw_cfi_same_value
Definition MILexer.h:85
@ kw_cfi_adjust_cfa_offset
Definition MILexer.h:90
@ kw_dereferenceable
Definition MILexer.h:55
@ kw_implicit_define
Definition MILexer.h:52
@ kw_cfi_def_cfa_offset
Definition MILexer.h:89
@ kw_machine_block_address_taken
Definition MILexer.h:151
@ kw_cfi_remember_state
Definition MILexer.h:95
@ kw_debug_instr_number
Definition MILexer.h:83
@ kw_post_instr_symbol
Definition MILexer.h:141
@ kw_cfi_restore_state
Definition MILexer.h:97
@ kw_ir_block_address_taken
Definition MILexer.h:150
@ kw_unknown_address
Definition MILexer.h:149
@ md_noalias_addrspace
Definition MILexer.h:163
@ kw_debug_location
Definition MILexer.h:82
@ kw_heap_alloc_marker
Definition MILexer.h:142
MIToken & setIntegerValue(APSInt IntVal)
Definition MILexer.cpp:79
MIToken & reset(TokenKind Kind, StringRef Range)
Definition MILexer.cpp:62
bool isError() const
Definition MILexer.h:216
MIToken & setOwnedStringValue(std::string StrVal)
Definition MILexer.cpp:73
StringRef::iterator location() const
Definition MILexer.h:245