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