1 //===- MILexer.cpp - Machine instructions lexer implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the lexing of machine instructions.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/ADT/Twine.h"
24 /// This class provides a way to iterate and get characters from the source
31 Cursor(NoneType) : Ptr(nullptr), End(nullptr) {}
33 explicit Cursor(StringRef Str) {
35 End = Ptr + Str.size();
38 bool isEOF() const { return Ptr == End; }
40 char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
42 void advance(unsigned I = 1) { Ptr += I; }
44 StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
46 StringRef upto(Cursor C) const {
47 assert(C.Ptr >= Ptr && C.Ptr <= End);
48 return StringRef(Ptr, C.Ptr - Ptr);
51 StringRef::iterator location() const { return Ptr; }
53 operator bool() const { return Ptr != nullptr; }
56 } // end anonymous namespace
58 /// Skip the leading whitespace characters and return the updated cursor.
59 static Cursor skipWhitespace(Cursor C) {
60 while (isspace(C.peek()))
65 /// Return true if the given character satisfies the following regular
66 /// expression: [-a-zA-Z$._0-9]
67 static bool isIdentifierChar(char C) {
68 return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' ||
72 /// Unescapes the given string value.
74 /// Expects the string value to be quoted.
75 static std::string unescapeQuotedString(StringRef Value) {
76 assert(Value.front() == '"' && Value.back() == '"');
77 Cursor C = Cursor(Value.substr(1, Value.size() - 2));
80 Str.reserve(C.remaining().size());
84 if (C.peek(1) == '\\') {
90 if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
91 Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
102 /// Lex a string constant using the following regular expression: \"[^\"]*\"
103 static Cursor lexStringConstant(
105 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
106 assert(C.peek() == '"');
107 for (C.advance(); C.peek() != '"'; C.advance()) {
111 "end of machine instruction reached before the closing '\"'");
119 static Cursor lexName(
120 Cursor C, MIToken &Token, MIToken::TokenKind Type, unsigned PrefixLength,
121 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
123 C.advance(PrefixLength);
124 if (C.peek() == '"') {
125 if (Cursor R = lexStringConstant(C, ErrorCallback)) {
126 StringRef String = Range.upto(R);
127 Token = MIToken(Type, String,
128 unescapeQuotedString(String.drop_front(PrefixLength)),
132 Token = MIToken(MIToken::Error, Range.remaining());
135 while (isIdentifierChar(C.peek()))
137 Token = MIToken(Type, Range.upto(C), PrefixLength);
141 static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
142 return StringSwitch<MIToken::TokenKind>(Identifier)
143 .Case("_", MIToken::underscore)
144 .Case("implicit", MIToken::kw_implicit)
145 .Case("implicit-def", MIToken::kw_implicit_define)
146 .Case("dead", MIToken::kw_dead)
147 .Case("killed", MIToken::kw_killed)
148 .Case("undef", MIToken::kw_undef)
149 .Case("debug-use", MIToken::kw_debug_use)
150 .Case("frame-setup", MIToken::kw_frame_setup)
151 .Case("debug-location", MIToken::kw_debug_location)
152 .Case(".cfi_offset", MIToken::kw_cfi_offset)
153 .Case(".cfi_def_cfa_register", MIToken::kw_cfi_def_cfa_register)
154 .Case(".cfi_def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
155 .Case(".cfi_def_cfa", MIToken::kw_cfi_def_cfa)
156 .Case("blockaddress", MIToken::kw_blockaddress)
157 .Case("target-index", MIToken::kw_target_index)
158 .Case("half", MIToken::kw_half)
159 .Case("float", MIToken::kw_float)
160 .Case("double", MIToken::kw_double)
161 .Case("x86_fp80", MIToken::kw_x86_fp80)
162 .Case("fp128", MIToken::kw_fp128)
163 .Case("ppc_fp128", MIToken::kw_ppc_fp128)
164 .Case("volatile", MIToken::kw_volatile)
165 .Default(MIToken::Identifier);
168 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
169 if (!isalpha(C.peek()) && C.peek() != '_' && C.peek() != '.')
172 while (isIdentifierChar(C.peek()))
174 auto Identifier = Range.upto(C);
175 Token = MIToken(getIdentifierKind(Identifier), Identifier);
179 static Cursor maybeLexMachineBasicBlock(
180 Cursor C, MIToken &Token,
181 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
182 if (!C.remaining().startswith("%bb."))
185 C.advance(4); // Skip '%bb.'
186 if (!isdigit(C.peek())) {
187 Token = MIToken(MIToken::Error, C.remaining());
188 ErrorCallback(C.location(), "expected a number after '%bb.'");
191 auto NumberRange = C;
192 while (isdigit(C.peek()))
194 StringRef Number = NumberRange.upto(C);
195 unsigned StringOffset = 4 + Number.size(); // Drop '%bb.<id>'
196 if (C.peek() == '.') {
197 C.advance(); // Skip '.'
199 while (isIdentifierChar(C.peek()))
202 Token = MIToken(MIToken::MachineBasicBlock, Range.upto(C), APSInt(Number),
207 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
208 MIToken::TokenKind Kind) {
209 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
212 C.advance(Rule.size());
213 auto NumberRange = C;
214 while (isdigit(C.peek()))
216 Token = MIToken(Kind, Range.upto(C), APSInt(NumberRange.upto(C)));
220 static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
221 MIToken::TokenKind Kind) {
222 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
225 C.advance(Rule.size());
226 auto NumberRange = C;
227 while (isdigit(C.peek()))
229 StringRef Number = NumberRange.upto(C);
230 unsigned StringOffset = Rule.size() + Number.size();
231 if (C.peek() == '.') {
234 while (isIdentifierChar(C.peek()))
237 Token = MIToken(Kind, Range.upto(C), APSInt(Number), StringOffset);
241 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
242 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
245 static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
246 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
249 static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
250 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
253 static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
254 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
257 static Cursor maybeLexIRBlock(
258 Cursor C, MIToken &Token,
259 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
260 const StringRef Rule = "%ir-block.";
261 if (!C.remaining().startswith(Rule))
263 if (isdigit(C.peek(Rule.size())))
264 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
265 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
268 static Cursor maybeLexIRValue(
269 Cursor C, MIToken &Token,
270 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
271 const StringRef Rule = "%ir.";
272 if (!C.remaining().startswith(Rule))
274 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
277 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
279 C.advance(); // Skip '%'
280 auto NumberRange = C;
281 while (isdigit(C.peek()))
283 Token = MIToken(MIToken::VirtualRegister, Range.upto(C),
284 APSInt(NumberRange.upto(C)));
288 static Cursor maybeLexRegister(Cursor C, MIToken &Token) {
291 if (isdigit(C.peek(1)))
292 return lexVirtualRegister(C, Token);
294 C.advance(); // Skip '%'
295 while (isIdentifierChar(C.peek()))
297 Token = MIToken(MIToken::NamedRegister, Range.upto(C),
298 /*StringOffset=*/1); // Drop the '%'
302 static Cursor maybeLexGlobalValue(
303 Cursor C, MIToken &Token,
304 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
307 if (!isdigit(C.peek(1)))
308 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
311 C.advance(1); // Skip the '@'
312 auto NumberRange = C;
313 while (isdigit(C.peek()))
316 MIToken(MIToken::GlobalValue, Range.upto(C), APSInt(NumberRange.upto(C)));
320 static Cursor maybeLexExternalSymbol(
321 Cursor C, MIToken &Token,
322 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
325 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
329 static bool isValidHexFloatingPointPrefix(char C) {
330 return C == 'H' || C == 'K' || C == 'L' || C == 'M';
333 static Cursor maybeLexHexFloatingPointLiteral(Cursor C, MIToken &Token) {
334 if (C.peek() != '0' || C.peek(1) != 'x')
337 C.advance(2); // Skip '0x'
338 if (isValidHexFloatingPointPrefix(C.peek()))
340 while (isxdigit(C.peek()))
342 Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C));
346 static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
348 // Skip over [0-9]*([eE][-+]?[0-9]+)?
349 while (isdigit(C.peek()))
351 if ((C.peek() == 'e' || C.peek() == 'E') &&
352 (isdigit(C.peek(1)) ||
353 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
355 while (isdigit(C.peek()))
358 Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C));
362 static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
363 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
367 while (isdigit(C.peek()))
370 return lexFloatingPointLiteral(Range, C, Token);
371 StringRef StrVal = Range.upto(C);
372 Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal));
376 static MIToken::TokenKind symbolToken(char C) {
379 return MIToken::comma;
381 return MIToken::equal;
383 return MIToken::colon;
385 return MIToken::exclaim;
387 return MIToken::lparen;
389 return MIToken::rparen;
391 return MIToken::Error;
395 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
396 MIToken::TokenKind Kind;
398 if (C.peek() == ':' && C.peek(1) == ':') {
399 Kind = MIToken::coloncolon;
402 Kind = symbolToken(C.peek());
403 if (Kind == MIToken::Error)
407 Token = MIToken(Kind, Range.upto(C));
411 StringRef llvm::lexMIToken(
412 StringRef Source, MIToken &Token,
413 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
414 auto C = skipWhitespace(Cursor(Source));
416 Token = MIToken(MIToken::Eof, C.remaining());
417 return C.remaining();
420 if (Cursor R = maybeLexIdentifier(C, Token))
421 return R.remaining();
422 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
423 return R.remaining();
424 if (Cursor R = maybeLexJumpTableIndex(C, Token))
425 return R.remaining();
426 if (Cursor R = maybeLexStackObject(C, Token))
427 return R.remaining();
428 if (Cursor R = maybeLexFixedStackObject(C, Token))
429 return R.remaining();
430 if (Cursor R = maybeLexConstantPoolItem(C, Token))
431 return R.remaining();
432 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
433 return R.remaining();
434 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
435 return R.remaining();
436 if (Cursor R = maybeLexRegister(C, Token))
437 return R.remaining();
438 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
439 return R.remaining();
440 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
441 return R.remaining();
442 if (Cursor R = maybeLexHexFloatingPointLiteral(C, Token))
443 return R.remaining();
444 if (Cursor R = maybeLexNumericalLiteral(C, Token))
445 return R.remaining();
446 if (Cursor R = maybeLexSymbol(C, Token))
447 return R.remaining();
449 Token = MIToken(MIToken::Error, C.remaining());
450 ErrorCallback(C.location(),
451 Twine("unexpected character '") + Twine(C.peek()) + "'");
452 return C.remaining();