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 Cursor maybeLexIntegerType(Cursor C, MIToken &Token) {
142 if (C.peek() != 'i' || !isdigit(C.peek(1)))
145 C.advance(); // Skip 'i'
146 while (isdigit(C.peek()))
148 Token = MIToken(MIToken::IntegerType, Range.upto(C));
152 static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
153 return StringSwitch<MIToken::TokenKind>(Identifier)
154 .Case("_", MIToken::underscore)
155 .Case("implicit", MIToken::kw_implicit)
156 .Case("implicit-def", MIToken::kw_implicit_define)
157 .Case("dead", MIToken::kw_dead)
158 .Case("killed", MIToken::kw_killed)
159 .Case("undef", MIToken::kw_undef)
160 .Case("early-clobber", MIToken::kw_early_clobber)
161 .Case("debug-use", MIToken::kw_debug_use)
162 .Case("frame-setup", MIToken::kw_frame_setup)
163 .Case("debug-location", MIToken::kw_debug_location)
164 .Case(".cfi_offset", MIToken::kw_cfi_offset)
165 .Case(".cfi_def_cfa_register", MIToken::kw_cfi_def_cfa_register)
166 .Case(".cfi_def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
167 .Case(".cfi_def_cfa", MIToken::kw_cfi_def_cfa)
168 .Case("blockaddress", MIToken::kw_blockaddress)
169 .Case("target-index", MIToken::kw_target_index)
170 .Case("half", MIToken::kw_half)
171 .Case("float", MIToken::kw_float)
172 .Case("double", MIToken::kw_double)
173 .Case("x86_fp80", MIToken::kw_x86_fp80)
174 .Case("fp128", MIToken::kw_fp128)
175 .Case("ppc_fp128", MIToken::kw_ppc_fp128)
176 .Case("volatile", MIToken::kw_volatile)
177 .Default(MIToken::Identifier);
180 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
181 if (!isalpha(C.peek()) && C.peek() != '_' && C.peek() != '.')
184 while (isIdentifierChar(C.peek()))
186 auto Identifier = Range.upto(C);
187 Token = MIToken(getIdentifierKind(Identifier), Identifier);
191 static Cursor maybeLexMachineBasicBlock(
192 Cursor C, MIToken &Token,
193 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
194 if (!C.remaining().startswith("%bb."))
197 C.advance(4); // Skip '%bb.'
198 if (!isdigit(C.peek())) {
199 Token = MIToken(MIToken::Error, C.remaining());
200 ErrorCallback(C.location(), "expected a number after '%bb.'");
203 auto NumberRange = C;
204 while (isdigit(C.peek()))
206 StringRef Number = NumberRange.upto(C);
207 unsigned StringOffset = 4 + Number.size(); // Drop '%bb.<id>'
208 if (C.peek() == '.') {
209 C.advance(); // Skip '.'
211 while (isIdentifierChar(C.peek()))
214 Token = MIToken(MIToken::MachineBasicBlock, Range.upto(C), APSInt(Number),
219 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
220 MIToken::TokenKind Kind) {
221 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
224 C.advance(Rule.size());
225 auto NumberRange = C;
226 while (isdigit(C.peek()))
228 Token = MIToken(Kind, Range.upto(C), APSInt(NumberRange.upto(C)));
232 static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
233 MIToken::TokenKind Kind) {
234 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
237 C.advance(Rule.size());
238 auto NumberRange = C;
239 while (isdigit(C.peek()))
241 StringRef Number = NumberRange.upto(C);
242 unsigned StringOffset = Rule.size() + Number.size();
243 if (C.peek() == '.') {
246 while (isIdentifierChar(C.peek()))
249 Token = MIToken(Kind, Range.upto(C), APSInt(Number), StringOffset);
253 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
254 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
257 static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
258 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
261 static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
262 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
265 static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
266 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
269 static Cursor maybeLexIRBlock(
270 Cursor C, MIToken &Token,
271 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
272 const StringRef Rule = "%ir-block.";
273 if (!C.remaining().startswith(Rule))
275 if (isdigit(C.peek(Rule.size())))
276 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
277 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
280 static Cursor maybeLexIRValue(
281 Cursor C, MIToken &Token,
282 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
283 const StringRef Rule = "%ir.";
284 if (!C.remaining().startswith(Rule))
286 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
289 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
291 C.advance(); // Skip '%'
292 auto NumberRange = C;
293 while (isdigit(C.peek()))
295 Token = MIToken(MIToken::VirtualRegister, Range.upto(C),
296 APSInt(NumberRange.upto(C)));
300 static Cursor maybeLexRegister(Cursor C, MIToken &Token) {
303 if (isdigit(C.peek(1)))
304 return lexVirtualRegister(C, Token);
306 C.advance(); // Skip '%'
307 while (isIdentifierChar(C.peek()))
309 Token = MIToken(MIToken::NamedRegister, Range.upto(C),
310 /*StringOffset=*/1); // Drop the '%'
314 static Cursor maybeLexGlobalValue(
315 Cursor C, MIToken &Token,
316 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
319 if (!isdigit(C.peek(1)))
320 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
323 C.advance(1); // Skip the '@'
324 auto NumberRange = C;
325 while (isdigit(C.peek()))
328 MIToken(MIToken::GlobalValue, Range.upto(C), APSInt(NumberRange.upto(C)));
332 static Cursor maybeLexExternalSymbol(
333 Cursor C, MIToken &Token,
334 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
337 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
341 static bool isValidHexFloatingPointPrefix(char C) {
342 return C == 'H' || C == 'K' || C == 'L' || C == 'M';
345 static Cursor maybeLexHexFloatingPointLiteral(Cursor C, MIToken &Token) {
346 if (C.peek() != '0' || C.peek(1) != 'x')
349 C.advance(2); // Skip '0x'
350 if (isValidHexFloatingPointPrefix(C.peek()))
352 while (isxdigit(C.peek()))
354 Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C));
358 static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
360 // Skip over [0-9]*([eE][-+]?[0-9]+)?
361 while (isdigit(C.peek()))
363 if ((C.peek() == 'e' || C.peek() == 'E') &&
364 (isdigit(C.peek(1)) ||
365 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
367 while (isdigit(C.peek()))
370 Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C));
374 static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
375 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
379 while (isdigit(C.peek()))
382 return lexFloatingPointLiteral(Range, C, Token);
383 StringRef StrVal = Range.upto(C);
384 Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal));
388 static MIToken::TokenKind symbolToken(char C) {
391 return MIToken::comma;
393 return MIToken::equal;
395 return MIToken::colon;
397 return MIToken::exclaim;
399 return MIToken::lparen;
401 return MIToken::rparen;
403 return MIToken::Error;
407 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
408 MIToken::TokenKind Kind;
410 if (C.peek() == ':' && C.peek(1) == ':') {
411 Kind = MIToken::coloncolon;
414 Kind = symbolToken(C.peek());
415 if (Kind == MIToken::Error)
419 Token = MIToken(Kind, Range.upto(C));
423 StringRef llvm::lexMIToken(
424 StringRef Source, MIToken &Token,
425 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
426 auto C = skipWhitespace(Cursor(Source));
428 Token = MIToken(MIToken::Eof, C.remaining());
429 return C.remaining();
432 if (Cursor R = maybeLexIntegerType(C, Token))
433 return R.remaining();
434 if (Cursor R = maybeLexIdentifier(C, Token))
435 return R.remaining();
436 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
437 return R.remaining();
438 if (Cursor R = maybeLexJumpTableIndex(C, Token))
439 return R.remaining();
440 if (Cursor R = maybeLexStackObject(C, Token))
441 return R.remaining();
442 if (Cursor R = maybeLexFixedStackObject(C, Token))
443 return R.remaining();
444 if (Cursor R = maybeLexConstantPoolItem(C, Token))
445 return R.remaining();
446 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
447 return R.remaining();
448 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
449 return R.remaining();
450 if (Cursor R = maybeLexRegister(C, Token))
451 return R.remaining();
452 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
453 return R.remaining();
454 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
455 return R.remaining();
456 if (Cursor R = maybeLexHexFloatingPointLiteral(C, Token))
457 return R.remaining();
458 if (Cursor R = maybeLexNumericalLiteral(C, Token))
459 return R.remaining();
460 if (Cursor R = maybeLexSymbol(C, Token))
461 return R.remaining();
463 Token = MIToken(MIToken::Error, C.remaining());
464 ErrorCallback(C.location(),
465 Twine("unexpected character '") + Twine(C.peek()) + "'");
466 return C.remaining();