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 | 46e9558 | 2015-08-12 20:44:16 +0000 | [diff] [blame^] | 204 | .Case("stack", MIToken::kw_stack) |
Alex Lorenz | 91097a3 | 2015-08-12 20:33:26 +0000 | [diff] [blame] | 205 | .Case("constant-pool", MIToken::kw_constant_pool) |
Alex Lorenz | b97c9ef | 2015-08-10 23:24:42 +0000 | [diff] [blame] | 206 | .Case("liveout", MIToken::kw_liveout) |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 207 | .Default(MIToken::Identifier); |
| 208 | } |
| 209 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 210 | static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) { |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 211 | if (!isalpha(C.peek()) && C.peek() != '_' && C.peek() != '.') |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 212 | return None; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 213 | auto Range = C; |
| 214 | while (isIdentifierChar(C.peek())) |
| 215 | C.advance(); |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 216 | auto Identifier = Range.upto(C); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 217 | Token.reset(getIdentifierKind(Identifier), Identifier) |
| 218 | .setStringValue(Identifier); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 219 | return C; |
| 220 | } |
| 221 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 222 | static Cursor maybeLexMachineBasicBlock( |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 223 | Cursor C, MIToken &Token, |
| 224 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 225 | if (!C.remaining().startswith("%bb.")) |
| 226 | return None; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 227 | auto Range = C; |
| 228 | C.advance(4); // Skip '%bb.' |
| 229 | if (!isdigit(C.peek())) { |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 230 | Token.reset(MIToken::Error, C.remaining()); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 231 | ErrorCallback(C.location(), "expected a number after '%bb.'"); |
| 232 | return C; |
| 233 | } |
| 234 | auto NumberRange = C; |
| 235 | while (isdigit(C.peek())) |
| 236 | C.advance(); |
| 237 | StringRef Number = NumberRange.upto(C); |
| 238 | unsigned StringOffset = 4 + Number.size(); // Drop '%bb.<id>' |
| 239 | if (C.peek() == '.') { |
| 240 | C.advance(); // Skip '.' |
| 241 | ++StringOffset; |
| 242 | while (isIdentifierChar(C.peek())) |
| 243 | C.advance(); |
| 244 | } |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 245 | Token.reset(MIToken::MachineBasicBlock, Range.upto(C)) |
| 246 | .setIntegerValue(APSInt(Number)) |
| 247 | .setStringValue(Range.upto(C).drop_front(StringOffset)); |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 248 | return C; |
| 249 | } |
| 250 | |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 251 | static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, |
| 252 | MIToken::TokenKind Kind) { |
| 253 | if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size()))) |
| 254 | return None; |
| 255 | auto Range = C; |
| 256 | C.advance(Rule.size()); |
| 257 | auto NumberRange = C; |
| 258 | while (isdigit(C.peek())) |
| 259 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 260 | Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C))); |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 261 | return C; |
| 262 | } |
| 263 | |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 264 | static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule, |
| 265 | MIToken::TokenKind Kind) { |
| 266 | if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size()))) |
| 267 | return None; |
| 268 | auto Range = C; |
| 269 | C.advance(Rule.size()); |
| 270 | auto NumberRange = C; |
| 271 | while (isdigit(C.peek())) |
| 272 | C.advance(); |
| 273 | StringRef Number = NumberRange.upto(C); |
| 274 | unsigned StringOffset = Rule.size() + Number.size(); |
| 275 | if (C.peek() == '.') { |
| 276 | C.advance(); |
| 277 | ++StringOffset; |
| 278 | while (isIdentifierChar(C.peek())) |
| 279 | C.advance(); |
| 280 | } |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 281 | Token.reset(Kind, Range.upto(C)) |
| 282 | .setIntegerValue(APSInt(Number)) |
| 283 | .setStringValue(Range.upto(C).drop_front(StringOffset)); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 284 | return C; |
| 285 | } |
| 286 | |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 287 | static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) { |
| 288 | return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex); |
| 289 | } |
| 290 | |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 291 | static Cursor maybeLexStackObject(Cursor C, MIToken &Token) { |
| 292 | return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject); |
| 293 | } |
| 294 | |
| 295 | static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) { |
| 296 | return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject); |
| 297 | } |
| 298 | |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 299 | static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) { |
| 300 | return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem); |
| 301 | } |
| 302 | |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 303 | static Cursor maybeLexIRBlock( |
| 304 | Cursor C, MIToken &Token, |
| 305 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 306 | const StringRef Rule = "%ir-block."; |
| 307 | if (!C.remaining().startswith(Rule)) |
| 308 | return None; |
| 309 | if (isdigit(C.peek(Rule.size()))) |
| 310 | return maybeLexIndex(C, Token, Rule, MIToken::IRBlock); |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 311 | return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback); |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 314 | static Cursor maybeLexIRValue( |
| 315 | Cursor C, MIToken &Token, |
| 316 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 317 | const StringRef Rule = "%ir."; |
| 318 | if (!C.remaining().startswith(Rule)) |
| 319 | return None; |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 320 | return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback); |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 323 | static Cursor lexVirtualRegister(Cursor C, MIToken &Token) { |
| 324 | auto Range = C; |
| 325 | C.advance(); // Skip '%' |
| 326 | auto NumberRange = C; |
| 327 | while (isdigit(C.peek())) |
| 328 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 329 | Token.reset(MIToken::VirtualRegister, Range.upto(C)) |
| 330 | .setIntegerValue(APSInt(NumberRange.upto(C))); |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 331 | return C; |
| 332 | } |
| 333 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 334 | static Cursor maybeLexRegister(Cursor C, MIToken &Token) { |
| 335 | if (C.peek() != '%') |
| 336 | return None; |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 337 | if (isdigit(C.peek(1))) |
| 338 | return lexVirtualRegister(C, Token); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 339 | auto Range = C; |
| 340 | C.advance(); // Skip '%' |
| 341 | while (isIdentifierChar(C.peek())) |
| 342 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 343 | Token.reset(MIToken::NamedRegister, Range.upto(C)) |
| 344 | .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%' |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 345 | return C; |
| 346 | } |
| 347 | |
Alex Lorenz | c1fbb35 | 2015-07-21 21:23:08 +0000 | [diff] [blame] | 348 | static Cursor maybeLexGlobalValue( |
| 349 | Cursor C, MIToken &Token, |
| 350 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 351 | if (C.peek() != '@') |
| 352 | return None; |
| 353 | if (!isdigit(C.peek(1))) |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 354 | return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1, |
Alex Lorenz | c1fbb35 | 2015-07-21 21:23:08 +0000 | [diff] [blame] | 355 | ErrorCallback); |
| 356 | auto Range = C; |
| 357 | C.advance(1); // Skip the '@' |
| 358 | auto NumberRange = C; |
| 359 | while (isdigit(C.peek())) |
| 360 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 361 | Token.reset(MIToken::GlobalValue, Range.upto(C)) |
| 362 | .setIntegerValue(APSInt(NumberRange.upto(C))); |
Alex Lorenz | c1fbb35 | 2015-07-21 21:23:08 +0000 | [diff] [blame] | 363 | return C; |
| 364 | } |
| 365 | |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 366 | static Cursor maybeLexExternalSymbol( |
| 367 | Cursor C, MIToken &Token, |
| 368 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 369 | if (C.peek() != '$') |
| 370 | return None; |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 371 | return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1, |
| 372 | ErrorCallback); |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 375 | static bool isValidHexFloatingPointPrefix(char C) { |
| 376 | return C == 'H' || C == 'K' || C == 'L' || C == 'M'; |
| 377 | } |
| 378 | |
| 379 | static Cursor maybeLexHexFloatingPointLiteral(Cursor C, MIToken &Token) { |
| 380 | if (C.peek() != '0' || C.peek(1) != 'x') |
| 381 | return None; |
| 382 | Cursor Range = C; |
| 383 | C.advance(2); // Skip '0x' |
| 384 | if (isValidHexFloatingPointPrefix(C.peek())) |
| 385 | C.advance(); |
| 386 | while (isxdigit(C.peek())) |
| 387 | C.advance(); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 388 | Token.reset(MIToken::FloatingPointLiteral, Range.upto(C)); |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 389 | return C; |
| 390 | } |
| 391 | |
| 392 | static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) { |
| 393 | C.advance(); |
| 394 | // Skip over [0-9]*([eE][-+]?[0-9]+)? |
| 395 | while (isdigit(C.peek())) |
| 396 | C.advance(); |
| 397 | if ((C.peek() == 'e' || C.peek() == 'E') && |
| 398 | (isdigit(C.peek(1)) || |
| 399 | ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) { |
| 400 | C.advance(2); |
| 401 | while (isdigit(C.peek())) |
| 402 | C.advance(); |
| 403 | } |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 404 | Token.reset(MIToken::FloatingPointLiteral, Range.upto(C)); |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 405 | return C; |
| 406 | } |
| 407 | |
| 408 | static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) { |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 409 | if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1)))) |
| 410 | return None; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 411 | auto Range = C; |
| 412 | C.advance(); |
| 413 | while (isdigit(C.peek())) |
| 414 | C.advance(); |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 415 | if (C.peek() == '.') |
| 416 | return lexFloatingPointLiteral(Range, C, Token); |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 417 | StringRef StrVal = Range.upto(C); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 418 | Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal)); |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 419 | return C; |
| 420 | } |
| 421 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 422 | static MIToken::TokenKind symbolToken(char C) { |
| 423 | switch (C) { |
| 424 | case ',': |
| 425 | return MIToken::comma; |
| 426 | case '=': |
| 427 | return MIToken::equal; |
Alex Lorenz | 2eacca8 | 2015-07-13 23:24:34 +0000 | [diff] [blame] | 428 | case ':': |
| 429 | return MIToken::colon; |
Alex Lorenz | 35e4446 | 2015-07-22 17:58:46 +0000 | [diff] [blame] | 430 | case '!': |
| 431 | return MIToken::exclaim; |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 432 | case '(': |
| 433 | return MIToken::lparen; |
| 434 | case ')': |
| 435 | return MIToken::rparen; |
Alex Lorenz | 5672a89 | 2015-08-05 22:26:15 +0000 | [diff] [blame] | 436 | case '+': |
| 437 | return MIToken::plus; |
| 438 | case '-': |
| 439 | return MIToken::minus; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 440 | default: |
| 441 | return MIToken::Error; |
| 442 | } |
| 443 | } |
| 444 | |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 445 | static Cursor maybeLexSymbol(Cursor C, MIToken &Token) { |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 446 | MIToken::TokenKind Kind; |
| 447 | unsigned Length = 1; |
| 448 | if (C.peek() == ':' && C.peek(1) == ':') { |
| 449 | Kind = MIToken::coloncolon; |
| 450 | Length = 2; |
| 451 | } else |
| 452 | Kind = symbolToken(C.peek()); |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 453 | if (Kind == MIToken::Error) |
| 454 | return None; |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 455 | auto Range = C; |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 456 | C.advance(Length); |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 457 | Token.reset(Kind, Range.upto(C)); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 458 | return C; |
| 459 | } |
| 460 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 461 | StringRef llvm::lexMIToken( |
| 462 | StringRef Source, MIToken &Token, |
| 463 | function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) { |
| 464 | auto C = skipWhitespace(Cursor(Source)); |
| 465 | if (C.isEOF()) { |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 466 | Token.reset(MIToken::Eof, C.remaining()); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 467 | return C.remaining(); |
| 468 | } |
| 469 | |
Alex Lorenz | 05e3882 | 2015-08-05 18:52:21 +0000 | [diff] [blame] | 470 | if (Cursor R = maybeLexIntegerType(C, Token)) |
| 471 | return R.remaining(); |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 472 | if (Cursor R = maybeLexIdentifier(C, Token)) |
| 473 | return R.remaining(); |
| 474 | if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback)) |
| 475 | return R.remaining(); |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 476 | if (Cursor R = maybeLexJumpTableIndex(C, Token)) |
| 477 | return R.remaining(); |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 478 | if (Cursor R = maybeLexStackObject(C, Token)) |
| 479 | return R.remaining(); |
| 480 | if (Cursor R = maybeLexFixedStackObject(C, Token)) |
| 481 | return R.remaining(); |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 482 | if (Cursor R = maybeLexConstantPoolItem(C, Token)) |
| 483 | return R.remaining(); |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 484 | if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback)) |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 485 | return R.remaining(); |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 486 | if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback)) |
| 487 | return R.remaining(); |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 488 | if (Cursor R = maybeLexRegister(C, Token)) |
| 489 | return R.remaining(); |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 490 | if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback)) |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 491 | return R.remaining(); |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 492 | if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback)) |
| 493 | return R.remaining(); |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 494 | if (Cursor R = maybeLexHexFloatingPointLiteral(C, Token)) |
| 495 | return R.remaining(); |
| 496 | if (Cursor R = maybeLexNumericalLiteral(C, Token)) |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 497 | return R.remaining(); |
| 498 | if (Cursor R = maybeLexSymbol(C, Token)) |
| 499 | return R.remaining(); |
| 500 | |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 501 | Token.reset(MIToken::Error, C.remaining()); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 502 | ErrorCallback(C.location(), |
Alex Lorenz | 6c6c46e | 2015-06-30 16:51:29 +0000 | [diff] [blame] | 503 | Twine("unexpected character '") + Twine(C.peek()) + "'"); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 504 | return C.remaining(); |
| 505 | } |