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