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