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 | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringSwitch.h" |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Twine.h" |
| 18 | #include <cctype> |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | /// This class provides a way to iterate and get characters from the source |
| 25 | /// string. |
| 26 | class Cursor { |
| 27 | const char *Ptr; |
| 28 | const char *End; |
| 29 | |
| 30 | public: |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 31 | Cursor(NoneType) : Ptr(nullptr), End(nullptr) {} |
| 32 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 33 | explicit Cursor(StringRef Str) { |
| 34 | Ptr = Str.data(); |
| 35 | End = Ptr + Str.size(); |
| 36 | } |
| 37 | |
| 38 | bool isEOF() const { return Ptr == End; } |
| 39 | |
NAKAMURA Takumi | c267b5f | 2015-06-24 06:40:09 +0000 | [diff] [blame] | 40 | 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] | 41 | |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 42 | void advance(unsigned I = 1) { Ptr += I; } |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 43 | |
| 44 | StringRef remaining() const { return StringRef(Ptr, End - Ptr); } |
| 45 | |
| 46 | StringRef upto(Cursor C) const { |
| 47 | assert(C.Ptr >= Ptr && C.Ptr <= End); |
| 48 | return StringRef(Ptr, C.Ptr - Ptr); |
| 49 | } |
| 50 | |
| 51 | StringRef::iterator location() const { return Ptr; } |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 52 | |
| 53 | operator bool() const { return Ptr != nullptr; } |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | } // end anonymous namespace |
| 57 | |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 58 | MIToken &MIToken::reset(TokenKind Kind, StringRef Range) { |
| 59 | this->Kind = Kind; |
| 60 | this->Range = Range; |
| 61 | return *this; |
| 62 | } |
| 63 | |
| 64 | MIToken &MIToken::setStringValue(StringRef StrVal) { |
| 65 | StringValue = StrVal; |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | MIToken &MIToken::setOwnedStringValue(std::string StrVal) { |
| 70 | StringValueStorage = std::move(StrVal); |
| 71 | StringValue = StringValueStorage; |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | MIToken &MIToken::setIntegerValue(APSInt IntVal) { |
| 76 | this->IntVal = std::move(IntVal); |
| 77 | return *this; |
| 78 | } |
| 79 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 80 | /// Skip the leading whitespace characters and return the updated cursor. |
| 81 | static Cursor skipWhitespace(Cursor C) { |
| 82 | while (isspace(C.peek())) |
| 83 | C.advance(); |
| 84 | return C; |
| 85 | } |
| 86 | |
Alex Lorenz | 484903e | 2015-07-17 22:48:04 +0000 | [diff] [blame] | 87 | /// Return true if the given character satisfies the following regular |
| 88 | /// expression: [-a-zA-Z$._0-9] |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 89 | static bool isIdentifierChar(char C) { |
Alex Lorenz | 484903e | 2015-07-17 22:48:04 +0000 | [diff] [blame] | 90 | return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' || |
| 91 | C == '$'; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 94 | /// Unescapes the given string value. |
| 95 | /// |
| 96 | /// Expects the string value to be quoted. |
| 97 | static std::string unescapeQuotedString(StringRef Value) { |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 98 | assert(Value.front() == '"' && Value.back() == '"'); |
| 99 | Cursor C = Cursor(Value.substr(1, Value.size() - 2)); |
| 100 | |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 101 | std::string Str; |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 102 | Str.reserve(C.remaining().size()); |
| 103 | while (!C.isEOF()) { |
| 104 | char Char = C.peek(); |
| 105 | if (Char == '\\') { |
| 106 | if (C.peek(1) == '\\') { |
| 107 | // Two '\' become one |
| 108 | Str += '\\'; |
| 109 | C.advance(2); |
| 110 | continue; |
| 111 | } |
| 112 | if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) { |
| 113 | Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2)); |
| 114 | C.advance(3); |
| 115 | continue; |
| 116 | } |
| 117 | } |
| 118 | Str += Char; |
| 119 | C.advance(); |
| 120 | } |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 121 | return Str; |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /// Lex a string constant using the following regular expression: \"[^\"]*\" |
| 125 | static Cursor lexStringConstant( |
| 126 | Cursor C, |
| 127 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 128 | assert(C.peek() == '"'); |
| 129 | for (C.advance(); C.peek() != '"'; C.advance()) { |
| 130 | if (C.isEOF()) { |
| 131 | ErrorCallback( |
| 132 | C.location(), |
| 133 | "end of machine instruction reached before the closing '\"'"); |
| 134 | return None; |
| 135 | } |
| 136 | } |
| 137 | C.advance(); |
| 138 | return C; |
| 139 | } |
| 140 | |
Alex Lorenz | 82a1cfd | 2015-07-28 17:03:40 +0000 | [diff] [blame] | 141 | static Cursor lexName( |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 142 | Cursor C, MIToken &Token, MIToken::TokenKind Type, unsigned PrefixLength, |
Alex Lorenz | 82a1cfd | 2015-07-28 17:03:40 +0000 | [diff] [blame] | 143 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 144 | auto Range = C; |
| 145 | C.advance(PrefixLength); |
| 146 | if (C.peek() == '"') { |
| 147 | if (Cursor R = lexStringConstant(C, ErrorCallback)) { |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 148 | StringRef String = Range.upto(R); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 149 | Token.reset(Type, String) |
| 150 | .setOwnedStringValue( |
| 151 | unescapeQuotedString(String.drop_front(PrefixLength))); |
Alex Lorenz | 82a1cfd | 2015-07-28 17:03:40 +0000 | [diff] [blame] | 152 | return R; |
| 153 | } |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 154 | Token.reset(MIToken::Error, Range.remaining()); |
Alex Lorenz | 82a1cfd | 2015-07-28 17:03:40 +0000 | [diff] [blame] | 155 | return Range; |
| 156 | } |
| 157 | while (isIdentifierChar(C.peek())) |
| 158 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 159 | Token.reset(Type, Range.upto(C)) |
| 160 | .setStringValue(Range.upto(C).drop_front(PrefixLength)); |
Alex Lorenz | 82a1cfd | 2015-07-28 17:03:40 +0000 | [diff] [blame] | 161 | return C; |
| 162 | } |
| 163 | |
Alex Lorenz | 05e3882 | 2015-08-05 18:52:21 +0000 | [diff] [blame] | 164 | static Cursor maybeLexIntegerType(Cursor C, MIToken &Token) { |
| 165 | if (C.peek() != 'i' || !isdigit(C.peek(1))) |
| 166 | return None; |
| 167 | auto Range = C; |
| 168 | C.advance(); // Skip 'i' |
| 169 | while (isdigit(C.peek())) |
| 170 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 171 | Token.reset(MIToken::IntegerType, Range.upto(C)); |
Alex Lorenz | 05e3882 | 2015-08-05 18:52:21 +0000 | [diff] [blame] | 172 | return C; |
| 173 | } |
| 174 | |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 175 | static MIToken::TokenKind getIdentifierKind(StringRef Identifier) { |
| 176 | return StringSwitch<MIToken::TokenKind>(Identifier) |
| 177 | .Case("_", MIToken::underscore) |
| 178 | .Case("implicit", MIToken::kw_implicit) |
| 179 | .Case("implicit-def", MIToken::kw_implicit_define) |
Alex Lorenz | cbbfd0b | 2015-07-07 20:34:53 +0000 | [diff] [blame] | 180 | .Case("dead", MIToken::kw_dead) |
Alex Lorenz | 495ad87 | 2015-07-08 21:23:34 +0000 | [diff] [blame] | 181 | .Case("killed", MIToken::kw_killed) |
Alex Lorenz | 4d026b89 | 2015-07-08 23:58:31 +0000 | [diff] [blame] | 182 | .Case("undef", MIToken::kw_undef) |
Alex Lorenz | 01c1a5e | 2015-08-05 17:49:03 +0000 | [diff] [blame] | 183 | .Case("early-clobber", MIToken::kw_early_clobber) |
Alex Lorenz | 9075258 | 2015-08-05 17:41:17 +0000 | [diff] [blame] | 184 | .Case("debug-use", MIToken::kw_debug_use) |
Alex Lorenz | e5a4466 | 2015-07-17 00:24:15 +0000 | [diff] [blame] | 185 | .Case("frame-setup", MIToken::kw_frame_setup) |
Alex Lorenz | 46d760d | 2015-07-22 21:15:11 +0000 | [diff] [blame] | 186 | .Case("debug-location", MIToken::kw_debug_location) |
Alex Lorenz | 8cfc686 | 2015-07-23 23:09:07 +0000 | [diff] [blame] | 187 | .Case(".cfi_offset", MIToken::kw_cfi_offset) |
Alex Lorenz | 5b0d5f6 | 2015-07-27 20:39:03 +0000 | [diff] [blame] | 188 | .Case(".cfi_def_cfa_register", MIToken::kw_cfi_def_cfa_register) |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 189 | .Case(".cfi_def_cfa_offset", MIToken::kw_cfi_def_cfa_offset) |
Alex Lorenz | b139323 | 2015-07-29 18:57:23 +0000 | [diff] [blame] | 190 | .Case(".cfi_def_cfa", MIToken::kw_cfi_def_cfa) |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 191 | .Case("blockaddress", MIToken::kw_blockaddress) |
Alex Lorenz | ef5c196 | 2015-07-28 23:02:45 +0000 | [diff] [blame] | 192 | .Case("target-index", MIToken::kw_target_index) |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 193 | .Case("half", MIToken::kw_half) |
| 194 | .Case("float", MIToken::kw_float) |
| 195 | .Case("double", MIToken::kw_double) |
| 196 | .Case("x86_fp80", MIToken::kw_x86_fp80) |
| 197 | .Case("fp128", MIToken::kw_fp128) |
| 198 | .Case("ppc_fp128", MIToken::kw_ppc_fp128) |
Alex Lorenz | 49873a8 | 2015-08-06 00:44:07 +0000 | [diff] [blame] | 199 | .Case("target-flags", MIToken::kw_target_flags) |
Alex Lorenz | a518b79 | 2015-08-04 00:24:45 +0000 | [diff] [blame] | 200 | .Case("volatile", MIToken::kw_volatile) |
Alex Lorenz | 10fd038 | 2015-08-06 16:49:30 +0000 | [diff] [blame] | 201 | .Case("non-temporal", MIToken::kw_non_temporal) |
Alex Lorenz | dc8de2a | 2015-08-06 16:55:53 +0000 | [diff] [blame] | 202 | .Case("invariant", MIToken::kw_invariant) |
Alex Lorenz | 61420f7 | 2015-08-07 20:48:30 +0000 | [diff] [blame] | 203 | .Case("align", MIToken::kw_align) |
Alex Lorenz | 91097a3 | 2015-08-12 20:33:26 +0000 | [diff] [blame^] | 204 | .Case("constant-pool", MIToken::kw_constant_pool) |
Alex Lorenz | b97c9ef | 2015-08-10 23:24:42 +0000 | [diff] [blame] | 205 | .Case("liveout", MIToken::kw_liveout) |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 206 | .Default(MIToken::Identifier); |
| 207 | } |
| 208 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 209 | static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) { |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 210 | if (!isalpha(C.peek()) && C.peek() != '_' && C.peek() != '.') |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 211 | return None; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 212 | auto Range = C; |
| 213 | while (isIdentifierChar(C.peek())) |
| 214 | C.advance(); |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 215 | auto Identifier = Range.upto(C); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 216 | Token.reset(getIdentifierKind(Identifier), Identifier) |
| 217 | .setStringValue(Identifier); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 218 | return C; |
| 219 | } |
| 220 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 221 | static Cursor maybeLexMachineBasicBlock( |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 222 | Cursor C, MIToken &Token, |
| 223 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 224 | if (!C.remaining().startswith("%bb.")) |
| 225 | return None; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 226 | auto Range = C; |
| 227 | C.advance(4); // Skip '%bb.' |
| 228 | if (!isdigit(C.peek())) { |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 229 | Token.reset(MIToken::Error, C.remaining()); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 230 | ErrorCallback(C.location(), "expected a number after '%bb.'"); |
| 231 | return C; |
| 232 | } |
| 233 | auto NumberRange = C; |
| 234 | while (isdigit(C.peek())) |
| 235 | C.advance(); |
| 236 | StringRef Number = NumberRange.upto(C); |
| 237 | unsigned StringOffset = 4 + Number.size(); // Drop '%bb.<id>' |
| 238 | if (C.peek() == '.') { |
| 239 | C.advance(); // Skip '.' |
| 240 | ++StringOffset; |
| 241 | while (isIdentifierChar(C.peek())) |
| 242 | C.advance(); |
| 243 | } |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 244 | Token.reset(MIToken::MachineBasicBlock, Range.upto(C)) |
| 245 | .setIntegerValue(APSInt(Number)) |
| 246 | .setStringValue(Range.upto(C).drop_front(StringOffset)); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 247 | return C; |
| 248 | } |
| 249 | |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 250 | static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, |
| 251 | MIToken::TokenKind Kind) { |
| 252 | if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size()))) |
| 253 | return None; |
| 254 | auto Range = C; |
| 255 | C.advance(Rule.size()); |
| 256 | auto NumberRange = C; |
| 257 | while (isdigit(C.peek())) |
| 258 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 259 | Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C))); |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 260 | return C; |
| 261 | } |
| 262 | |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 263 | static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule, |
| 264 | MIToken::TokenKind Kind) { |
| 265 | if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size()))) |
| 266 | return None; |
| 267 | auto Range = C; |
| 268 | C.advance(Rule.size()); |
| 269 | auto NumberRange = C; |
| 270 | while (isdigit(C.peek())) |
| 271 | C.advance(); |
| 272 | StringRef Number = NumberRange.upto(C); |
| 273 | unsigned StringOffset = Rule.size() + Number.size(); |
| 274 | if (C.peek() == '.') { |
| 275 | C.advance(); |
| 276 | ++StringOffset; |
| 277 | while (isIdentifierChar(C.peek())) |
| 278 | C.advance(); |
| 279 | } |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 280 | Token.reset(Kind, Range.upto(C)) |
| 281 | .setIntegerValue(APSInt(Number)) |
| 282 | .setStringValue(Range.upto(C).drop_front(StringOffset)); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 283 | return C; |
| 284 | } |
| 285 | |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 286 | static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) { |
| 287 | return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex); |
| 288 | } |
| 289 | |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 290 | static Cursor maybeLexStackObject(Cursor C, MIToken &Token) { |
| 291 | return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject); |
| 292 | } |
| 293 | |
| 294 | static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) { |
| 295 | return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject); |
| 296 | } |
| 297 | |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 298 | static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) { |
| 299 | return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem); |
| 300 | } |
| 301 | |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 302 | static Cursor maybeLexIRBlock( |
| 303 | Cursor C, MIToken &Token, |
| 304 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 305 | const StringRef Rule = "%ir-block."; |
| 306 | if (!C.remaining().startswith(Rule)) |
| 307 | return None; |
| 308 | if (isdigit(C.peek(Rule.size()))) |
| 309 | return maybeLexIndex(C, Token, Rule, MIToken::IRBlock); |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 310 | return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback); |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 313 | static Cursor maybeLexIRValue( |
| 314 | Cursor C, MIToken &Token, |
| 315 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 316 | const StringRef Rule = "%ir."; |
| 317 | if (!C.remaining().startswith(Rule)) |
| 318 | return None; |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 319 | return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback); |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 322 | static Cursor lexVirtualRegister(Cursor C, MIToken &Token) { |
| 323 | auto Range = C; |
| 324 | C.advance(); // Skip '%' |
| 325 | auto NumberRange = C; |
| 326 | while (isdigit(C.peek())) |
| 327 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 328 | Token.reset(MIToken::VirtualRegister, Range.upto(C)) |
| 329 | .setIntegerValue(APSInt(NumberRange.upto(C))); |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 330 | return C; |
| 331 | } |
| 332 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 333 | static Cursor maybeLexRegister(Cursor C, MIToken &Token) { |
| 334 | if (C.peek() != '%') |
| 335 | return None; |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 336 | if (isdigit(C.peek(1))) |
| 337 | return lexVirtualRegister(C, Token); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 338 | auto Range = C; |
| 339 | C.advance(); // Skip '%' |
| 340 | while (isIdentifierChar(C.peek())) |
| 341 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 342 | Token.reset(MIToken::NamedRegister, Range.upto(C)) |
| 343 | .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%' |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 344 | return C; |
| 345 | } |
| 346 | |
Alex Lorenz | c1fbb35 | 2015-07-21 21:23:08 +0000 | [diff] [blame] | 347 | static Cursor maybeLexGlobalValue( |
| 348 | Cursor C, MIToken &Token, |
| 349 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 350 | if (C.peek() != '@') |
| 351 | return None; |
| 352 | if (!isdigit(C.peek(1))) |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 353 | return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1, |
Alex Lorenz | c1fbb35 | 2015-07-21 21:23:08 +0000 | [diff] [blame] | 354 | ErrorCallback); |
| 355 | auto Range = C; |
| 356 | C.advance(1); // Skip the '@' |
| 357 | auto NumberRange = C; |
| 358 | while (isdigit(C.peek())) |
| 359 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 360 | Token.reset(MIToken::GlobalValue, Range.upto(C)) |
| 361 | .setIntegerValue(APSInt(NumberRange.upto(C))); |
Alex Lorenz | c1fbb35 | 2015-07-21 21:23:08 +0000 | [diff] [blame] | 362 | return C; |
| 363 | } |
| 364 | |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 365 | static Cursor maybeLexExternalSymbol( |
| 366 | Cursor C, MIToken &Token, |
| 367 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 368 | if (C.peek() != '$') |
| 369 | return None; |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 370 | return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1, |
| 371 | ErrorCallback); |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 374 | static bool isValidHexFloatingPointPrefix(char C) { |
| 375 | return C == 'H' || C == 'K' || C == 'L' || C == 'M'; |
| 376 | } |
| 377 | |
| 378 | static Cursor maybeLexHexFloatingPointLiteral(Cursor C, MIToken &Token) { |
| 379 | if (C.peek() != '0' || C.peek(1) != 'x') |
| 380 | return None; |
| 381 | Cursor Range = C; |
| 382 | C.advance(2); // Skip '0x' |
| 383 | if (isValidHexFloatingPointPrefix(C.peek())) |
| 384 | C.advance(); |
| 385 | while (isxdigit(C.peek())) |
| 386 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 387 | Token.reset(MIToken::FloatingPointLiteral, Range.upto(C)); |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 388 | return C; |
| 389 | } |
| 390 | |
| 391 | static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) { |
| 392 | C.advance(); |
| 393 | // Skip over [0-9]*([eE][-+]?[0-9]+)? |
| 394 | while (isdigit(C.peek())) |
| 395 | C.advance(); |
| 396 | if ((C.peek() == 'e' || C.peek() == 'E') && |
| 397 | (isdigit(C.peek(1)) || |
| 398 | ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) { |
| 399 | C.advance(2); |
| 400 | while (isdigit(C.peek())) |
| 401 | C.advance(); |
| 402 | } |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 403 | Token.reset(MIToken::FloatingPointLiteral, Range.upto(C)); |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 404 | return C; |
| 405 | } |
| 406 | |
| 407 | static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) { |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 408 | if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1)))) |
| 409 | return None; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 410 | auto Range = C; |
| 411 | C.advance(); |
| 412 | while (isdigit(C.peek())) |
| 413 | C.advance(); |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 414 | if (C.peek() == '.') |
| 415 | return lexFloatingPointLiteral(Range, C, Token); |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 416 | StringRef StrVal = Range.upto(C); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 417 | Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal)); |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 418 | return C; |
| 419 | } |
| 420 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 421 | static MIToken::TokenKind symbolToken(char C) { |
| 422 | switch (C) { |
| 423 | case ',': |
| 424 | return MIToken::comma; |
| 425 | case '=': |
| 426 | return MIToken::equal; |
Alex Lorenz | 2eacca8 | 2015-07-13 23:24:34 +0000 | [diff] [blame] | 427 | case ':': |
| 428 | return MIToken::colon; |
Alex Lorenz | 35e4446 | 2015-07-22 17:58:46 +0000 | [diff] [blame] | 429 | case '!': |
| 430 | return MIToken::exclaim; |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 431 | case '(': |
| 432 | return MIToken::lparen; |
| 433 | case ')': |
| 434 | return MIToken::rparen; |
Alex Lorenz | 5672a89 | 2015-08-05 22:26:15 +0000 | [diff] [blame] | 435 | case '+': |
| 436 | return MIToken::plus; |
| 437 | case '-': |
| 438 | return MIToken::minus; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 439 | default: |
| 440 | return MIToken::Error; |
| 441 | } |
| 442 | } |
| 443 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 444 | static Cursor maybeLexSymbol(Cursor C, MIToken &Token) { |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 445 | MIToken::TokenKind Kind; |
| 446 | unsigned Length = 1; |
| 447 | if (C.peek() == ':' && C.peek(1) == ':') { |
| 448 | Kind = MIToken::coloncolon; |
| 449 | Length = 2; |
| 450 | } else |
| 451 | Kind = symbolToken(C.peek()); |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 452 | if (Kind == MIToken::Error) |
| 453 | return None; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 454 | auto Range = C; |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 455 | C.advance(Length); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 456 | Token.reset(Kind, Range.upto(C)); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 457 | return C; |
| 458 | } |
| 459 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 460 | StringRef llvm::lexMIToken( |
| 461 | StringRef Source, MIToken &Token, |
| 462 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 463 | auto C = skipWhitespace(Cursor(Source)); |
| 464 | if (C.isEOF()) { |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 465 | Token.reset(MIToken::Eof, C.remaining()); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 466 | return C.remaining(); |
| 467 | } |
| 468 | |
Alex Lorenz | 05e3882 | 2015-08-05 18:52:21 +0000 | [diff] [blame] | 469 | if (Cursor R = maybeLexIntegerType(C, Token)) |
| 470 | return R.remaining(); |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 471 | if (Cursor R = maybeLexIdentifier(C, Token)) |
| 472 | return R.remaining(); |
| 473 | if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback)) |
| 474 | return R.remaining(); |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 475 | if (Cursor R = maybeLexJumpTableIndex(C, Token)) |
| 476 | return R.remaining(); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 477 | if (Cursor R = maybeLexStackObject(C, Token)) |
| 478 | return R.remaining(); |
| 479 | if (Cursor R = maybeLexFixedStackObject(C, Token)) |
| 480 | return R.remaining(); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 481 | if (Cursor R = maybeLexConstantPoolItem(C, Token)) |
| 482 | return R.remaining(); |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 483 | if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback)) |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 484 | return R.remaining(); |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 485 | if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback)) |
| 486 | return R.remaining(); |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 487 | if (Cursor R = maybeLexRegister(C, Token)) |
| 488 | return R.remaining(); |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 489 | if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback)) |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 490 | return R.remaining(); |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 491 | if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback)) |
| 492 | return R.remaining(); |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 493 | if (Cursor R = maybeLexHexFloatingPointLiteral(C, Token)) |
| 494 | return R.remaining(); |
| 495 | if (Cursor R = maybeLexNumericalLiteral(C, Token)) |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 496 | return R.remaining(); |
| 497 | if (Cursor R = maybeLexSymbol(C, Token)) |
| 498 | return R.remaining(); |
| 499 | |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 500 | Token.reset(MIToken::Error, C.remaining()); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 501 | ErrorCallback(C.location(), |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 502 | Twine("unexpected character '") + Twine(C.peek()) + "'"); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 503 | return C.remaining(); |
| 504 | } |