implement mc asmparser support for '.', which gets the
[oota-llvm.git] / lib / MC / MCParser / AsmLexer.cpp
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 "llvm/MC/MCParser/AsmLexer.h"
15 #include "llvm/Support/SMLoc.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include <cerrno>
19 #include <cstdio>
20 #include <cstdlib>
21 using namespace llvm;
22
23 AsmLexer::AsmLexer(const MCAsmInfo &_MAI) : MAI(_MAI)  {
24   CurBuf = NULL;
25   CurPtr = NULL;
26   TokStart = 0;
27 }
28
29 AsmLexer::~AsmLexer() {
30 }
31
32 void AsmLexer::setBuffer(const MemoryBuffer *buf, const char *ptr) {
33   CurBuf = buf;
34   
35   if (ptr)
36     CurPtr = ptr;
37   else
38     CurPtr = CurBuf->getBufferStart();
39   
40   TokStart = 0;
41 }
42
43 SMLoc AsmLexer::getLoc() const {
44   return SMLoc::getFromPointer(TokStart);
45 }
46
47 /// ReturnError - Set the error to the specified string at the specified
48 /// location.  This is defined to always return AsmToken::Error.
49 AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
50   SetError(SMLoc::getFromPointer(Loc), Msg);
51   
52   return AsmToken(AsmToken::Error, StringRef(Loc, 0));
53 }
54
55 int AsmLexer::getNextChar() {
56   char CurChar = *CurPtr++;
57   switch (CurChar) {
58   default:
59     return (unsigned char)CurChar;
60   case 0:
61     // A nul character in the stream is either the end of the current buffer or
62     // a random nul in the file.  Disambiguate that here.
63     if (CurPtr-1 != CurBuf->getBufferEnd())
64       return 0;  // Just whitespace.
65     
66     // Otherwise, return end of file.
67     --CurPtr;  // Another call to lex will return EOF again.  
68     return EOF;
69   }
70 }
71
72 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
73 AsmToken AsmLexer::LexIdentifier() {
74   while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
75          *CurPtr == '.' || *CurPtr == '@')
76     ++CurPtr;
77   
78   // Handle . as a special case.
79   if (CurPtr == TokStart+1 && TokStart[0] == '.')
80     return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
81   
82   return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
83 }
84
85 /// LexSlash: Slash: /
86 ///           C-Style Comment: /* ... */
87 AsmToken AsmLexer::LexSlash() {
88   switch (*CurPtr) {
89   case '*': break; // C style comment.
90   case '/': return ++CurPtr, LexLineComment();
91   default:  return AsmToken(AsmToken::Slash, StringRef(CurPtr, 1));
92   }
93
94   // C Style comment.
95   ++CurPtr;  // skip the star.
96   while (1) {
97     int CurChar = getNextChar();
98     switch (CurChar) {
99     case EOF:
100       return ReturnError(TokStart, "unterminated comment");
101     case '*':
102       // End of the comment?
103       if (CurPtr[0] != '/') break;
104       
105       ++CurPtr;   // End the */.
106       return LexToken();
107     }
108   }
109 }
110
111 /// LexLineComment: Comment: #[^\n]*
112 ///                        : //[^\n]*
113 AsmToken AsmLexer::LexLineComment() {
114   // FIXME: This is broken if we happen to a comment at the end of a file, which
115   // was .included, and which doesn't end with a newline.
116   int CurChar = getNextChar();
117   while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
118     CurChar = getNextChar();
119   
120   if (CurChar == EOF)
121     return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
122   return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
123 }
124
125
126 /// LexDigit: First character is [0-9].
127 ///   Local Label: [0-9][:]
128 ///   Forward/Backward Label: [0-9][fb]
129 ///   Binary integer: 0b[01]+
130 ///   Octal integer: 0[0-7]+
131 ///   Hex integer: 0x[0-9a-fA-F]+
132 ///   Decimal integer: [1-9][0-9]*
133 /// TODO: FP literal.
134 AsmToken AsmLexer::LexDigit() {
135   if (*CurPtr == ':')
136     return ReturnError(TokStart, "FIXME: local label not implemented");
137   if (*CurPtr == 'f' || *CurPtr == 'b')
138     return ReturnError(TokStart, "FIXME: directional label not implemented");
139   
140   // Decimal integer: [1-9][0-9]*
141   if (CurPtr[-1] != '0') {
142     while (isdigit(*CurPtr))
143       ++CurPtr;
144     
145     StringRef Result(TokStart, CurPtr - TokStart);
146     
147     long long Value;
148     if (Result.getAsInteger(10, Value)) {
149       // We have to handle minint_as_a_positive_value specially, because
150       // - minint_as_a_positive_value = minint and it is valid.
151       if (Result == "9223372036854775808")
152         Value = -9223372036854775808ULL;
153       else
154         return ReturnError(TokStart, "Invalid decimal number");
155     }
156     return AsmToken(AsmToken::Integer, Result, Value);
157   }
158   
159   if (*CurPtr == 'b') {
160     ++CurPtr;
161     const char *NumStart = CurPtr;
162     while (CurPtr[0] == '0' || CurPtr[0] == '1')
163       ++CurPtr;
164     
165     // Requires at least one binary digit.
166     if (CurPtr == NumStart)
167       return ReturnError(TokStart, "Invalid binary number");
168     
169     StringRef Result(TokStart, CurPtr - TokStart);
170     
171     long long Value;
172     if (Result.getAsInteger(2, Value))
173       return ReturnError(TokStart, "Invalid binary number");
174     
175     return AsmToken(AsmToken::Integer, Result, Value);
176   }
177  
178   if (*CurPtr == 'x') {
179     ++CurPtr;
180     const char *NumStart = CurPtr;
181     while (isxdigit(CurPtr[0]))
182       ++CurPtr;
183     
184     // Requires at least one hex digit.
185     if (CurPtr == NumStart)
186       return ReturnError(CurPtr-2, "Invalid hexadecimal number");
187
188     unsigned long long Result;
189     if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
190       return ReturnError(TokStart, "Invalid hexadecimal number");
191       
192     return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
193                     (int64_t)Result);
194   }
195   
196   // Must be an octal number, it starts with 0.
197   while (*CurPtr >= '0' && *CurPtr <= '7')
198     ++CurPtr;
199   
200   StringRef Result(TokStart, CurPtr - TokStart);
201   long long Value;
202   if (Result.getAsInteger(8, Value))
203     return ReturnError(TokStart, "Invalid octal number");
204   
205   return AsmToken(AsmToken::Integer, Result, Value);
206 }
207
208 /// LexQuote: String: "..."
209 AsmToken AsmLexer::LexQuote() {
210   int CurChar = getNextChar();
211   // TODO: does gas allow multiline string constants?
212   while (CurChar != '"') {
213     if (CurChar == '\\') {
214       // Allow \", etc.
215       CurChar = getNextChar();
216     }
217     
218     if (CurChar == EOF)
219       return ReturnError(TokStart, "unterminated string constant");
220
221     CurChar = getNextChar();
222   }
223   
224   return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
225 }
226
227 StringRef AsmLexer::LexUntilEndOfStatement() {
228   TokStart = CurPtr;
229
230   while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
231           *CurPtr != ';' &&  // End of statement marker.
232          *CurPtr != '\n' &&
233          *CurPtr != '\r' &&
234          (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
235     ++CurPtr;
236   }
237   return StringRef(TokStart, CurPtr-TokStart);
238 }
239
240 bool AsmLexer::isAtStartOfComment(char Char) {
241   // FIXME: This won't work for multi-character comment indicators like "//".
242   return Char == *MAI.getCommentString();
243 }
244
245 AsmToken AsmLexer::LexToken() {
246   TokStart = CurPtr;
247   // This always consumes at least one character.
248   int CurChar = getNextChar();
249   
250   if (isAtStartOfComment(CurChar))
251     return LexLineComment();
252
253   switch (CurChar) {
254   default:
255     // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
256     if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
257       return LexIdentifier();
258     
259     // Unknown character, emit an error.
260     return ReturnError(TokStart, "invalid character in input");
261   case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
262   case 0:
263   case ' ':
264   case '\t':
265     // Ignore whitespace.
266     return LexToken();
267   case '\n': // FALL THROUGH.
268   case '\r': // FALL THROUGH.
269   case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
270   case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
271   case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
272   case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
273   case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
274   case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
275   case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
276   case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
277   case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
278   case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
279   case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
280   case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
281   case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
282   case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
283   case '=': 
284     if (*CurPtr == '=')
285       return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
286     return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
287   case '|': 
288     if (*CurPtr == '|')
289       return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
290     return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
291   case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
292   case '&': 
293     if (*CurPtr == '&')
294       return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
295     return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
296   case '!': 
297     if (*CurPtr == '=')
298       return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
299     return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
300   case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
301   case '/': return LexSlash();
302   case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
303   case '"': return LexQuote();
304   case '0': case '1': case '2': case '3': case '4':
305   case '5': case '6': case '7': case '8': case '9':
306     return LexDigit();
307   case '<':
308     switch (*CurPtr) {
309     case '<': return ++CurPtr, AsmToken(AsmToken::LessLess, 
310                                         StringRef(TokStart, 2));
311     case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual, 
312                                         StringRef(TokStart, 2));
313     case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater, 
314                                         StringRef(TokStart, 2));
315     default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
316     }
317   case '>':
318     switch (*CurPtr) {
319     case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater, 
320                                         StringRef(TokStart, 2));
321     case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual, 
322                                         StringRef(TokStart, 2));
323     default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
324     }
325       
326   // TODO: Quoted identifiers (objc methods etc)
327   // local labels: [0-9][:]
328   // Forward/backward labels: [0-9][fb]
329   // Integers, fp constants, character constants.
330   }
331 }