Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 1 | //===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===// |
| 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 class implements the lexer for assembly files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "AsmLexer.h" |
| 15 | #include "llvm/Support/SourceMgr.h" |
| 16 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | bcd0b8d | 2009-06-23 00:24:36 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" // for strtoull. |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 18 | #include <cerrno> |
Duncan Sands | 66b9f29 | 2009-06-22 06:59:32 +0000 | [diff] [blame] | 19 | #include <cstdio> |
Chris Lattner | 4506bd2 | 2009-06-21 19:43:50 +0000 | [diff] [blame] | 20 | #include <cstdlib> |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) { |
| 24 | CurBuffer = 0; |
| 25 | CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); |
| 26 | CurPtr = CurBuf->getBufferStart(); |
| 27 | TokStart = 0; |
| 28 | } |
| 29 | |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 30 | SMLoc AsmLexer::getLoc() const { |
| 31 | return SMLoc::getFromPointer(TokStart); |
| 32 | } |
| 33 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 34 | void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg) const { |
| 35 | SrcMgr.PrintMessage(Loc, Msg); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 38 | /// ReturnError - Set the error to the specified string at the specified |
| 39 | /// location. This is defined to always return asmtok::Error. |
| 40 | asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) { |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 41 | SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 42 | return asmtok::Error; |
| 43 | } |
| 44 | |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 45 | int AsmLexer::getNextChar() { |
| 46 | char CurChar = *CurPtr++; |
| 47 | switch (CurChar) { |
| 48 | default: |
| 49 | return (unsigned char)CurChar; |
| 50 | case 0: { |
| 51 | // A nul character in the stream is either the end of the current buffer or |
| 52 | // a random nul in the file. Disambiguate that here. |
| 53 | if (CurPtr-1 != CurBuf->getBufferEnd()) |
| 54 | return 0; // Just whitespace. |
| 55 | |
| 56 | // If this is the end of an included file, pop the parent file off the |
| 57 | // include stack. |
| 58 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 59 | if (ParentIncludeLoc != SMLoc()) { |
| 60 | CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc); |
| 61 | CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); |
| 62 | CurPtr = ParentIncludeLoc.getPointer(); |
| 63 | return getNextChar(); |
| 64 | } |
| 65 | |
| 66 | // Otherwise, return end of file. |
| 67 | --CurPtr; // Another call to lex will return EOF again. |
| 68 | return EOF; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 73 | /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]* |
| 74 | asmtok::TokKind AsmLexer::LexIdentifier() { |
| 75 | while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' || |
| 76 | *CurPtr == '.' || *CurPtr == '@') |
| 77 | ++CurPtr; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame^] | 78 | CurStrVal.assign(TokStart, CurPtr); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 79 | return asmtok::Identifier; |
| 80 | } |
| 81 | |
| 82 | /// LexPercent: Register: %[a-zA-Z0-9]+ |
| 83 | asmtok::TokKind AsmLexer::LexPercent() { |
| 84 | if (!isalnum(*CurPtr)) |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame^] | 85 | return asmtok::Percent; // Single %. |
| 86 | |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 87 | while (isalnum(*CurPtr)) |
| 88 | ++CurPtr; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame^] | 89 | CurStrVal.assign(TokStart, CurPtr); // Include % |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 90 | return asmtok::Register; |
| 91 | } |
| 92 | |
| 93 | /// LexSlash: Slash: / |
| 94 | /// C-Style Comment: /* ... */ |
| 95 | asmtok::TokKind AsmLexer::LexSlash() { |
| 96 | if (*CurPtr != '*') |
| 97 | return asmtok::Slash; |
| 98 | |
| 99 | // C Style comment. |
| 100 | ++CurPtr; // skip the star. |
| 101 | while (1) { |
| 102 | int CurChar = getNextChar(); |
| 103 | switch (CurChar) { |
| 104 | case EOF: |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 105 | return ReturnError(TokStart, "unterminated comment"); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 106 | case '*': |
| 107 | // End of the comment? |
| 108 | if (CurPtr[0] != '/') break; |
| 109 | |
| 110 | ++CurPtr; // End the */. |
| 111 | return LexToken(); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// LexHash: Comment: #[^\n]* |
| 117 | asmtok::TokKind AsmLexer::LexHash() { |
| 118 | int CurChar = getNextChar(); |
| 119 | while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF) |
| 120 | CurChar = getNextChar(); |
| 121 | |
| 122 | if (CurChar == EOF) |
| 123 | return asmtok::Eof; |
| 124 | return asmtok::EndOfStatement; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /// LexDigit: First character is [0-9]. |
| 129 | /// Local Label: [0-9][:] |
| 130 | /// Forward/Backward Label: [0-9][fb] |
| 131 | /// Binary integer: 0b[01]+ |
| 132 | /// Octal integer: 0[0-7]+ |
| 133 | /// Hex integer: 0x[0-9a-fA-F]+ |
| 134 | /// Decimal integer: [1-9][0-9]* |
| 135 | /// TODO: FP literal. |
| 136 | asmtok::TokKind AsmLexer::LexDigit() { |
| 137 | if (*CurPtr == ':') |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 138 | return ReturnError(TokStart, "FIXME: local label not implemented"); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 139 | if (*CurPtr == 'f' || *CurPtr == 'b') |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 140 | return ReturnError(TokStart, "FIXME: directional label not implemented"); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 141 | |
| 142 | // Decimal integer: [1-9][0-9]* |
| 143 | if (CurPtr[-1] != '0') { |
| 144 | while (isdigit(*CurPtr)) |
| 145 | ++CurPtr; |
| 146 | CurIntVal = strtoll(TokStart, 0, 10); |
| 147 | return asmtok::IntVal; |
| 148 | } |
| 149 | |
| 150 | if (*CurPtr == 'b') { |
| 151 | ++CurPtr; |
| 152 | const char *NumStart = CurPtr; |
| 153 | while (CurPtr[0] == '0' || CurPtr[0] == '1') |
| 154 | ++CurPtr; |
| 155 | |
| 156 | // Requires at least one binary digit. |
| 157 | if (CurPtr == NumStart) |
| 158 | return ReturnError(CurPtr-2, "Invalid binary number"); |
| 159 | CurIntVal = strtoll(NumStart, 0, 2); |
| 160 | return asmtok::IntVal; |
| 161 | } |
| 162 | |
| 163 | if (*CurPtr == 'x') { |
| 164 | ++CurPtr; |
| 165 | const char *NumStart = CurPtr; |
| 166 | while (isxdigit(CurPtr[0])) |
| 167 | ++CurPtr; |
| 168 | |
| 169 | // Requires at least one hex digit. |
| 170 | if (CurPtr == NumStart) |
| 171 | return ReturnError(CurPtr-2, "Invalid hexadecimal number"); |
| 172 | |
| 173 | errno = 0; |
| 174 | CurIntVal = strtoll(NumStart, 0, 16); |
| 175 | if (errno == EINVAL) |
| 176 | return ReturnError(CurPtr-2, "Invalid hexadecimal number"); |
| 177 | if (errno == ERANGE) { |
| 178 | errno = 0; |
| 179 | CurIntVal = (int64_t)strtoull(NumStart, 0, 16); |
| 180 | if (errno == EINVAL) |
| 181 | return ReturnError(CurPtr-2, "Invalid hexadecimal number"); |
| 182 | if (errno == ERANGE) |
| 183 | return ReturnError(CurPtr-2, "Hexadecimal number out of range"); |
| 184 | } |
| 185 | return asmtok::IntVal; |
| 186 | } |
| 187 | |
| 188 | // Must be an octal number, it starts with 0. |
| 189 | while (*CurPtr >= '0' && *CurPtr <= '7') |
| 190 | ++CurPtr; |
| 191 | CurIntVal = strtoll(TokStart, 0, 8); |
| 192 | return asmtok::IntVal; |
| 193 | } |
| 194 | |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 195 | /// LexQuote: String: "..." |
| 196 | asmtok::TokKind AsmLexer::LexQuote() { |
| 197 | int CurChar = getNextChar(); |
| 198 | // TODO: does gas allow multiline string constants? |
| 199 | while (CurChar != '"') { |
| 200 | if (CurChar == '\\') { |
| 201 | // Allow \", etc. |
| 202 | CurChar = getNextChar(); |
| 203 | } |
| 204 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 205 | if (CurChar == EOF) |
| 206 | return ReturnError(TokStart, "unterminated string constant"); |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 207 | |
| 208 | CurChar = getNextChar(); |
| 209 | } |
| 210 | |
| 211 | CurStrVal.assign(TokStart, CurPtr); // include quotes. |
| 212 | return asmtok::String; |
| 213 | } |
| 214 | |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 215 | |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 216 | asmtok::TokKind AsmLexer::LexToken() { |
| 217 | TokStart = CurPtr; |
| 218 | // This always consumes at least one character. |
| 219 | int CurChar = getNextChar(); |
| 220 | |
| 221 | switch (CurChar) { |
| 222 | default: |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 223 | // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]* |
| 224 | if (isalpha(CurChar) || CurChar == '_' || CurChar == '.') |
| 225 | return LexIdentifier(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 226 | |
| 227 | // Unknown character, emit an error. |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 228 | return ReturnError(TokStart, "invalid character in input"); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 229 | case EOF: return asmtok::Eof; |
| 230 | case 0: |
| 231 | case ' ': |
| 232 | case '\t': |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 233 | // Ignore whitespace. |
| 234 | return LexToken(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 235 | case '\n': // FALL THROUGH. |
| 236 | case '\r': // FALL THROUGH. |
| 237 | case ';': return asmtok::EndOfStatement; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 238 | case ':': return asmtok::Colon; |
| 239 | case '+': return asmtok::Plus; |
| 240 | case '-': return asmtok::Minus; |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 241 | case '~': return asmtok::Tilde; |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 242 | case '(': return asmtok::LParen; |
| 243 | case ')': return asmtok::RParen; |
| 244 | case '*': return asmtok::Star; |
| 245 | case ',': return asmtok::Comma; |
| 246 | case '$': return asmtok::Dollar; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame^] | 247 | case '|': return asmtok::Pipe; |
| 248 | case '^': return asmtok::Caret; |
| 249 | case '&': return asmtok::Amp; |
| 250 | case '!': return asmtok::Exclaim; |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 251 | case '%': return LexPercent(); |
| 252 | case '/': return LexSlash(); |
| 253 | case '#': return LexHash(); |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 254 | case '"': return LexQuote(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 255 | case '0': case '1': case '2': case '3': case '4': |
| 256 | case '5': case '6': case '7': case '8': case '9': |
| 257 | return LexDigit(); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame^] | 258 | case '<': |
| 259 | if (*CurPtr == '<') { |
| 260 | ++CurPtr; |
| 261 | return asmtok::LessLess; |
| 262 | } |
| 263 | // Don't have any use for bare '<' yet. |
| 264 | return ReturnError(TokStart, "invalid character in input"); |
| 265 | case '>': |
| 266 | if (*CurPtr == '>') { |
| 267 | ++CurPtr; |
| 268 | return asmtok::GreaterGreater; |
| 269 | } |
| 270 | // Don't have any use for bare '>' yet. |
| 271 | return ReturnError(TokStart, "invalid character in input"); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 272 | |
| 273 | // TODO: Quoted identifiers (objc methods etc) |
| 274 | // local labels: [0-9][:] |
| 275 | // Forward/backward labels: [0-9][fb] |
| 276 | // Integers, fp constants, character constants. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 277 | } |
Duncan Sands | 66b9f29 | 2009-06-22 06:59:32 +0000 | [diff] [blame] | 278 | } |