Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 1 | //===- MILexer.cpp - Machine instructions lexer implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the lexing of machine instructions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "MILexer.h" |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame^] | 15 | #include "llvm/ADT/StringSwitch.h" |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
| 17 | #include <cctype> |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | /// This class provides a way to iterate and get characters from the source |
| 24 | /// string. |
| 25 | class Cursor { |
| 26 | const char *Ptr; |
| 27 | const char *End; |
| 28 | |
| 29 | public: |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 30 | Cursor(NoneType) : Ptr(nullptr), End(nullptr) {} |
| 31 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 32 | explicit Cursor(StringRef Str) { |
| 33 | Ptr = Str.data(); |
| 34 | End = Ptr + Str.size(); |
| 35 | } |
| 36 | |
| 37 | bool isEOF() const { return Ptr == End; } |
| 38 | |
NAKAMURA Takumi | c267b5f | 2015-06-24 06:40:09 +0000 | [diff] [blame] | 39 | char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; } |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 40 | |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 41 | void advance(unsigned I = 1) { Ptr += I; } |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 42 | |
| 43 | StringRef remaining() const { return StringRef(Ptr, End - Ptr); } |
| 44 | |
| 45 | StringRef upto(Cursor C) const { |
| 46 | assert(C.Ptr >= Ptr && C.Ptr <= End); |
| 47 | return StringRef(Ptr, C.Ptr - Ptr); |
| 48 | } |
| 49 | |
| 50 | StringRef::iterator location() const { return Ptr; } |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 51 | |
| 52 | operator bool() const { return Ptr != nullptr; } |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | } // end anonymous namespace |
| 56 | |
| 57 | /// Skip the leading whitespace characters and return the updated cursor. |
| 58 | static Cursor skipWhitespace(Cursor C) { |
| 59 | while (isspace(C.peek())) |
| 60 | C.advance(); |
| 61 | return C; |
| 62 | } |
| 63 | |
| 64 | static bool isIdentifierChar(char C) { |
| 65 | return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.'; |
| 66 | } |
| 67 | |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame^] | 68 | static MIToken::TokenKind getIdentifierKind(StringRef Identifier) { |
| 69 | return StringSwitch<MIToken::TokenKind>(Identifier) |
| 70 | .Case("_", MIToken::underscore) |
| 71 | .Case("implicit", MIToken::kw_implicit) |
| 72 | .Case("implicit-def", MIToken::kw_implicit_define) |
| 73 | .Default(MIToken::Identifier); |
| 74 | } |
| 75 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 76 | static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) { |
| 77 | if (!isalpha(C.peek()) && C.peek() != '_') |
| 78 | return None; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 79 | auto Range = C; |
| 80 | while (isIdentifierChar(C.peek())) |
| 81 | C.advance(); |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 82 | auto Identifier = Range.upto(C); |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame^] | 83 | Token = MIToken(getIdentifierKind(Identifier), Identifier); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 84 | return C; |
| 85 | } |
| 86 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 87 | static Cursor maybeLexMachineBasicBlock( |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 88 | Cursor C, MIToken &Token, |
| 89 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 90 | if (!C.remaining().startswith("%bb.")) |
| 91 | return None; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 92 | auto Range = C; |
| 93 | C.advance(4); // Skip '%bb.' |
| 94 | if (!isdigit(C.peek())) { |
| 95 | Token = MIToken(MIToken::Error, C.remaining()); |
| 96 | ErrorCallback(C.location(), "expected a number after '%bb.'"); |
| 97 | return C; |
| 98 | } |
| 99 | auto NumberRange = C; |
| 100 | while (isdigit(C.peek())) |
| 101 | C.advance(); |
| 102 | StringRef Number = NumberRange.upto(C); |
| 103 | unsigned StringOffset = 4 + Number.size(); // Drop '%bb.<id>' |
| 104 | if (C.peek() == '.') { |
| 105 | C.advance(); // Skip '.' |
| 106 | ++StringOffset; |
| 107 | while (isIdentifierChar(C.peek())) |
| 108 | C.advance(); |
| 109 | } |
| 110 | Token = MIToken(MIToken::MachineBasicBlock, Range.upto(C), APSInt(Number), |
| 111 | StringOffset); |
| 112 | return C; |
| 113 | } |
| 114 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 115 | static Cursor maybeLexRegister(Cursor C, MIToken &Token) { |
| 116 | if (C.peek() != '%') |
| 117 | return None; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 118 | auto Range = C; |
| 119 | C.advance(); // Skip '%' |
| 120 | while (isIdentifierChar(C.peek())) |
| 121 | C.advance(); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 122 | Token = MIToken(MIToken::NamedRegister, Range.upto(C), |
| 123 | /*StringOffset=*/1); // Drop the '%' |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 124 | return C; |
| 125 | } |
| 126 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 127 | static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token) { |
| 128 | if (C.peek() != '@') |
| 129 | return None; |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 130 | auto Range = C; |
| 131 | C.advance(); // Skip the '@' |
| 132 | // TODO: add support for quoted names. |
| 133 | if (!isdigit(C.peek())) { |
| 134 | while (isIdentifierChar(C.peek())) |
| 135 | C.advance(); |
| 136 | Token = MIToken(MIToken::NamedGlobalValue, Range.upto(C), |
| 137 | /*StringOffset=*/1); // Drop the '@' |
| 138 | return C; |
| 139 | } |
| 140 | auto NumberRange = C; |
| 141 | while (isdigit(C.peek())) |
| 142 | C.advance(); |
| 143 | Token = |
| 144 | MIToken(MIToken::GlobalValue, Range.upto(C), APSInt(NumberRange.upto(C))); |
| 145 | return C; |
| 146 | } |
| 147 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 148 | static Cursor maybeLexIntegerLiteral(Cursor C, MIToken &Token) { |
| 149 | if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1)))) |
| 150 | return None; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 151 | auto Range = C; |
| 152 | C.advance(); |
| 153 | while (isdigit(C.peek())) |
| 154 | C.advance(); |
| 155 | StringRef StrVal = Range.upto(C); |
| 156 | Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal)); |
| 157 | return C; |
| 158 | } |
| 159 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 160 | static MIToken::TokenKind symbolToken(char C) { |
| 161 | switch (C) { |
| 162 | case ',': |
| 163 | return MIToken::comma; |
| 164 | case '=': |
| 165 | return MIToken::equal; |
| 166 | default: |
| 167 | return MIToken::Error; |
| 168 | } |
| 169 | } |
| 170 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 171 | static Cursor maybeLexSymbol(Cursor C, MIToken &Token) { |
| 172 | auto Kind = symbolToken(C.peek()); |
| 173 | if (Kind == MIToken::Error) |
| 174 | return None; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 175 | auto Range = C; |
| 176 | C.advance(); |
| 177 | Token = MIToken(Kind, Range.upto(C)); |
| 178 | return C; |
| 179 | } |
| 180 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 181 | StringRef llvm::lexMIToken( |
| 182 | StringRef Source, MIToken &Token, |
| 183 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 184 | auto C = skipWhitespace(Cursor(Source)); |
| 185 | if (C.isEOF()) { |
| 186 | Token = MIToken(MIToken::Eof, C.remaining()); |
| 187 | return C.remaining(); |
| 188 | } |
| 189 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 190 | if (Cursor R = maybeLexIdentifier(C, Token)) |
| 191 | return R.remaining(); |
| 192 | if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback)) |
| 193 | return R.remaining(); |
| 194 | if (Cursor R = maybeLexRegister(C, Token)) |
| 195 | return R.remaining(); |
| 196 | if (Cursor R = maybeLexGlobalValue(C, Token)) |
| 197 | return R.remaining(); |
| 198 | if (Cursor R = maybeLexIntegerLiteral(C, Token)) |
| 199 | return R.remaining(); |
| 200 | if (Cursor R = maybeLexSymbol(C, Token)) |
| 201 | return R.remaining(); |
| 202 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 203 | Token = MIToken(MIToken::Error, C.remaining()); |
| 204 | ErrorCallback(C.location(), |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 205 | Twine("unexpected character '") + Twine(C.peek()) + "'"); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 206 | return C.remaining(); |
| 207 | } |