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 | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 78 | CurStrVal.assign(TokStart, CurPtr); // Include % |
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 | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 85 | return ReturnError(TokStart, "invalid register name"); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 86 | while (isalnum(*CurPtr)) |
| 87 | ++CurPtr; |
| 88 | CurStrVal.assign(TokStart, CurPtr); // Skip % |
| 89 | return asmtok::Register; |
| 90 | } |
| 91 | |
| 92 | /// LexSlash: Slash: / |
| 93 | /// C-Style Comment: /* ... */ |
| 94 | asmtok::TokKind AsmLexer::LexSlash() { |
| 95 | if (*CurPtr != '*') |
| 96 | return asmtok::Slash; |
| 97 | |
| 98 | // C Style comment. |
| 99 | ++CurPtr; // skip the star. |
| 100 | while (1) { |
| 101 | int CurChar = getNextChar(); |
| 102 | switch (CurChar) { |
| 103 | case EOF: |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 104 | return ReturnError(TokStart, "unterminated comment"); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 105 | case '*': |
| 106 | // End of the comment? |
| 107 | if (CurPtr[0] != '/') break; |
| 108 | |
| 109 | ++CurPtr; // End the */. |
| 110 | return LexToken(); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// LexHash: Comment: #[^\n]* |
| 116 | asmtok::TokKind AsmLexer::LexHash() { |
| 117 | int CurChar = getNextChar(); |
| 118 | while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF) |
| 119 | CurChar = getNextChar(); |
| 120 | |
| 121 | if (CurChar == EOF) |
| 122 | return asmtok::Eof; |
| 123 | return asmtok::EndOfStatement; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /// LexDigit: First character is [0-9]. |
| 128 | /// Local Label: [0-9][:] |
| 129 | /// Forward/Backward Label: [0-9][fb] |
| 130 | /// Binary integer: 0b[01]+ |
| 131 | /// Octal integer: 0[0-7]+ |
| 132 | /// Hex integer: 0x[0-9a-fA-F]+ |
| 133 | /// Decimal integer: [1-9][0-9]* |
| 134 | /// TODO: FP literal. |
| 135 | asmtok::TokKind AsmLexer::LexDigit() { |
| 136 | if (*CurPtr == ':') |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 137 | return ReturnError(TokStart, "FIXME: local label not implemented"); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 138 | if (*CurPtr == 'f' || *CurPtr == 'b') |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 139 | return ReturnError(TokStart, "FIXME: directional label not implemented"); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 140 | |
| 141 | // Decimal integer: [1-9][0-9]* |
| 142 | if (CurPtr[-1] != '0') { |
| 143 | while (isdigit(*CurPtr)) |
| 144 | ++CurPtr; |
| 145 | CurIntVal = strtoll(TokStart, 0, 10); |
| 146 | return asmtok::IntVal; |
| 147 | } |
| 148 | |
| 149 | if (*CurPtr == 'b') { |
| 150 | ++CurPtr; |
| 151 | const char *NumStart = CurPtr; |
| 152 | while (CurPtr[0] == '0' || CurPtr[0] == '1') |
| 153 | ++CurPtr; |
| 154 | |
| 155 | // Requires at least one binary digit. |
| 156 | if (CurPtr == NumStart) |
| 157 | return ReturnError(CurPtr-2, "Invalid binary number"); |
| 158 | CurIntVal = strtoll(NumStart, 0, 2); |
| 159 | return asmtok::IntVal; |
| 160 | } |
| 161 | |
| 162 | if (*CurPtr == 'x') { |
| 163 | ++CurPtr; |
| 164 | const char *NumStart = CurPtr; |
| 165 | while (isxdigit(CurPtr[0])) |
| 166 | ++CurPtr; |
| 167 | |
| 168 | // Requires at least one hex digit. |
| 169 | if (CurPtr == NumStart) |
| 170 | return ReturnError(CurPtr-2, "Invalid hexadecimal number"); |
| 171 | |
| 172 | errno = 0; |
| 173 | CurIntVal = strtoll(NumStart, 0, 16); |
| 174 | if (errno == EINVAL) |
| 175 | return ReturnError(CurPtr-2, "Invalid hexadecimal number"); |
| 176 | if (errno == ERANGE) { |
| 177 | errno = 0; |
| 178 | CurIntVal = (int64_t)strtoull(NumStart, 0, 16); |
| 179 | if (errno == EINVAL) |
| 180 | return ReturnError(CurPtr-2, "Invalid hexadecimal number"); |
| 181 | if (errno == ERANGE) |
| 182 | return ReturnError(CurPtr-2, "Hexadecimal number out of range"); |
| 183 | } |
| 184 | return asmtok::IntVal; |
| 185 | } |
| 186 | |
| 187 | // Must be an octal number, it starts with 0. |
| 188 | while (*CurPtr >= '0' && *CurPtr <= '7') |
| 189 | ++CurPtr; |
| 190 | CurIntVal = strtoll(TokStart, 0, 8); |
| 191 | return asmtok::IntVal; |
| 192 | } |
| 193 | |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 194 | /// LexQuote: String: "..." |
| 195 | asmtok::TokKind AsmLexer::LexQuote() { |
| 196 | int CurChar = getNextChar(); |
| 197 | // TODO: does gas allow multiline string constants? |
| 198 | while (CurChar != '"') { |
| 199 | if (CurChar == '\\') { |
| 200 | // Allow \", etc. |
| 201 | CurChar = getNextChar(); |
| 202 | } |
| 203 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 204 | if (CurChar == EOF) |
| 205 | return ReturnError(TokStart, "unterminated string constant"); |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 206 | |
| 207 | CurChar = getNextChar(); |
| 208 | } |
| 209 | |
| 210 | CurStrVal.assign(TokStart, CurPtr); // include quotes. |
| 211 | return asmtok::String; |
| 212 | } |
| 213 | |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 214 | |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 215 | asmtok::TokKind AsmLexer::LexToken() { |
| 216 | TokStart = CurPtr; |
| 217 | // This always consumes at least one character. |
| 218 | int CurChar = getNextChar(); |
| 219 | |
| 220 | switch (CurChar) { |
| 221 | default: |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 222 | // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]* |
| 223 | if (isalpha(CurChar) || CurChar == '_' || CurChar == '.') |
| 224 | return LexIdentifier(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 225 | |
| 226 | // Unknown character, emit an error. |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 227 | return ReturnError(TokStart, "invalid character in input"); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 228 | case EOF: return asmtok::Eof; |
| 229 | case 0: |
| 230 | case ' ': |
| 231 | case '\t': |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 232 | // Ignore whitespace. |
| 233 | return LexToken(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 234 | case '\n': // FALL THROUGH. |
| 235 | case '\r': // FALL THROUGH. |
| 236 | case ';': return asmtok::EndOfStatement; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 237 | case ':': return asmtok::Colon; |
| 238 | case '+': return asmtok::Plus; |
| 239 | case '-': return asmtok::Minus; |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 240 | case '~': return asmtok::Tilde; |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 241 | case '(': return asmtok::LParen; |
| 242 | case ')': return asmtok::RParen; |
| 243 | case '*': return asmtok::Star; |
| 244 | case ',': return asmtok::Comma; |
| 245 | case '$': return asmtok::Dollar; |
| 246 | case '%': return LexPercent(); |
| 247 | case '/': return LexSlash(); |
| 248 | case '#': return LexHash(); |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 249 | case '"': return LexQuote(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 250 | case '0': case '1': case '2': case '3': case '4': |
| 251 | case '5': case '6': case '7': case '8': case '9': |
| 252 | return LexDigit(); |
| 253 | |
| 254 | // TODO: Quoted identifiers (objc methods etc) |
| 255 | // local labels: [0-9][:] |
| 256 | // Forward/backward labels: [0-9][fb] |
| 257 | // Integers, fp constants, character constants. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 258 | } |
Duncan Sands | 66b9f29 | 2009-06-22 06:59:32 +0000 | [diff] [blame] | 259 | } |