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 void MIToken::unescapeQuotedStringValue(std::string &Str) const {
73 assert(isStringValueQuoted() && "String value isn't quoted");
74 StringRef Value = Range.drop_front(StringOffset);
75 assert(Value.front() == '"' && Value.back() == '"');
76 Cursor C = Cursor(Value.substr(1, Value.size() - 2));
79 Str.reserve(C.remaining().size());
83 if (C.peek(1) == '\\') {
89 if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
90 Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
100 /// Lex a string constant using the following regular expression: \"[^\"]*\"
101 static Cursor lexStringConstant(
103 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
104 assert(C.peek() == '"');
105 for (C.advance(); C.peek() != '"'; C.advance()) {
109 "end of machine instruction reached before the closing '\"'");
117 static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
118 return StringSwitch<MIToken::TokenKind>(Identifier)
119 .Case("_", MIToken::underscore)
120 .Case("implicit", MIToken::kw_implicit)
121 .Case("implicit-def", MIToken::kw_implicit_define)
122 .Case("dead", MIToken::kw_dead)
123 .Case("killed", MIToken::kw_killed)
124 .Case("undef", MIToken::kw_undef)
125 .Case("frame-setup", MIToken::kw_frame_setup)
126 .Case("debug-location", MIToken::kw_debug_location)
127 .Case(".cfi_offset", MIToken::kw_cfi_offset)
128 .Case(".cfi_def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
129 .Default(MIToken::Identifier);
132 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
133 if (!isalpha(C.peek()) && C.peek() != '_' && C.peek() != '.')
136 while (isIdentifierChar(C.peek()))
138 auto Identifier = Range.upto(C);
139 Token = MIToken(getIdentifierKind(Identifier), Identifier);
143 static Cursor maybeLexMachineBasicBlock(
144 Cursor C, MIToken &Token,
145 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
146 if (!C.remaining().startswith("%bb."))
149 C.advance(4); // Skip '%bb.'
150 if (!isdigit(C.peek())) {
151 Token = MIToken(MIToken::Error, C.remaining());
152 ErrorCallback(C.location(), "expected a number after '%bb.'");
155 auto NumberRange = C;
156 while (isdigit(C.peek()))
158 StringRef Number = NumberRange.upto(C);
159 unsigned StringOffset = 4 + Number.size(); // Drop '%bb.<id>'
160 if (C.peek() == '.') {
161 C.advance(); // Skip '.'
163 while (isIdentifierChar(C.peek()))
166 Token = MIToken(MIToken::MachineBasicBlock, Range.upto(C), APSInt(Number),
171 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
172 MIToken::TokenKind Kind) {
173 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
176 C.advance(Rule.size());
177 auto NumberRange = C;
178 while (isdigit(C.peek()))
180 Token = MIToken(Kind, Range.upto(C), APSInt(NumberRange.upto(C)));
184 static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
185 MIToken::TokenKind Kind) {
186 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
189 C.advance(Rule.size());
190 auto NumberRange = C;
191 while (isdigit(C.peek()))
193 StringRef Number = NumberRange.upto(C);
194 unsigned StringOffset = Rule.size() + Number.size();
195 if (C.peek() == '.') {
198 while (isIdentifierChar(C.peek()))
201 Token = MIToken(Kind, Range.upto(C), APSInt(Number), StringOffset);
205 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
206 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
209 static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
210 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
213 static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
214 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
217 static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
218 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
221 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
223 C.advance(); // Skip '%'
224 auto NumberRange = C;
225 while (isdigit(C.peek()))
227 Token = MIToken(MIToken::VirtualRegister, Range.upto(C),
228 APSInt(NumberRange.upto(C)));
232 static Cursor maybeLexRegister(Cursor C, MIToken &Token) {
235 if (isdigit(C.peek(1)))
236 return lexVirtualRegister(C, Token);
238 C.advance(); // Skip '%'
239 while (isIdentifierChar(C.peek()))
241 Token = MIToken(MIToken::NamedRegister, Range.upto(C),
242 /*StringOffset=*/1); // Drop the '%'
246 static Cursor lexName(
247 Cursor C, MIToken &Token, MIToken::TokenKind Type,
248 MIToken::TokenKind QuotedType, unsigned PrefixLength,
249 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
251 C.advance(PrefixLength);
252 if (C.peek() == '"') {
253 if (Cursor R = lexStringConstant(C, ErrorCallback)) {
254 Token = MIToken(QuotedType, Range.upto(R), PrefixLength);
257 Token = MIToken(MIToken::Error, Range.remaining());
260 while (isIdentifierChar(C.peek()))
262 Token = MIToken(Type, Range.upto(C), PrefixLength);
266 static Cursor maybeLexGlobalValue(
267 Cursor C, MIToken &Token,
268 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
271 if (!isdigit(C.peek(1)))
272 return lexName(C, Token, MIToken::NamedGlobalValue,
273 MIToken::QuotedNamedGlobalValue, /*PrefixLength=*/1,
276 C.advance(1); // Skip the '@'
277 auto NumberRange = C;
278 while (isdigit(C.peek()))
281 MIToken(MIToken::GlobalValue, Range.upto(C), APSInt(NumberRange.upto(C)));
285 static Cursor maybeLexExternalSymbol(
286 Cursor C, MIToken &Token,
287 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
290 return lexName(C, Token, MIToken::ExternalSymbol,
291 MIToken::QuotedExternalSymbol,
292 /*PrefixLength=*/1, ErrorCallback);
295 static Cursor maybeLexIntegerLiteral(Cursor C, MIToken &Token) {
296 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
300 while (isdigit(C.peek()))
302 StringRef StrVal = Range.upto(C);
303 Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal));
307 static MIToken::TokenKind symbolToken(char C) {
310 return MIToken::comma;
312 return MIToken::equal;
314 return MIToken::colon;
316 return MIToken::exclaim;
318 return MIToken::Error;
322 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
323 auto Kind = symbolToken(C.peek());
324 if (Kind == MIToken::Error)
328 Token = MIToken(Kind, Range.upto(C));
332 StringRef llvm::lexMIToken(
333 StringRef Source, MIToken &Token,
334 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
335 auto C = skipWhitespace(Cursor(Source));
337 Token = MIToken(MIToken::Eof, C.remaining());
338 return C.remaining();
341 if (Cursor R = maybeLexIdentifier(C, Token))
342 return R.remaining();
343 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
344 return R.remaining();
345 if (Cursor R = maybeLexJumpTableIndex(C, Token))
346 return R.remaining();
347 if (Cursor R = maybeLexStackObject(C, Token))
348 return R.remaining();
349 if (Cursor R = maybeLexFixedStackObject(C, Token))
350 return R.remaining();
351 if (Cursor R = maybeLexConstantPoolItem(C, Token))
352 return R.remaining();
353 if (Cursor R = maybeLexRegister(C, Token))
354 return R.remaining();
355 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
356 return R.remaining();
357 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
358 return R.remaining();
359 if (Cursor R = maybeLexIntegerLiteral(C, Token))
360 return R.remaining();
361 if (Cursor R = maybeLexSymbol(C, Token))
362 return R.remaining();
364 Token = MIToken(MIToken::Error, C.remaining());
365 ErrorCallback(C.location(),
366 Twine("unexpected character '") + Twine(C.peek()) + "'");
367 return C.remaining();