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