MC/AsmParser: Report .stabs directive as unsupported.
[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 }
27
28 AsmLexer::~AsmLexer() {
29 }
30
31 void AsmLexer::setBuffer(const MemoryBuffer *buf, const char *ptr) {
32   CurBuf = buf;
33   
34   if (ptr)
35     CurPtr = ptr;
36   else
37     CurPtr = CurBuf->getBufferStart();
38   
39   TokStart = 0;
40 }
41
42 /// ReturnError - Set the error to the specified string at the specified
43 /// location.  This is defined to always return AsmToken::Error.
44 AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
45   SetError(SMLoc::getFromPointer(Loc), Msg);
46   
47   return AsmToken(AsmToken::Error, StringRef(Loc, 0));
48 }
49
50 int AsmLexer::getNextChar() {
51   char CurChar = *CurPtr++;
52   switch (CurChar) {
53   default:
54     return (unsigned char)CurChar;
55   case 0:
56     // A nul character in the stream is either the end of the current buffer or
57     // a random nul in the file.  Disambiguate that here.
58     if (CurPtr-1 != CurBuf->getBufferEnd())
59       return 0;  // Just whitespace.
60     
61     // Otherwise, return end of file.
62     --CurPtr;  // Another call to lex will return EOF again.  
63     return EOF;
64   }
65 }
66
67 /// LexFloatLiteral: [0-9]*[.][0-9]*([eE][+-]?[0-9]*)?
68 ///
69 /// The leading integral digit sequence and dot should have already been
70 /// consumed, some or all of the fractional digit sequence *can* have been
71 /// consumed.
72 AsmToken AsmLexer::LexFloatLiteral() {
73   // Skip the fractional digit sequence.
74   while (isdigit(*CurPtr))
75     ++CurPtr;
76
77   // Check for exponent; we intentionally accept a slighlty wider set of
78   // literals here and rely on the upstream client to reject invalid ones (e.g.,
79   // "1e+").
80   if (*CurPtr == 'e' || *CurPtr == 'E') {
81     ++CurPtr;
82     if (*CurPtr == '-' || *CurPtr == '+')
83       ++CurPtr;
84     while (isdigit(*CurPtr))
85       ++CurPtr;
86   }
87
88   return AsmToken(AsmToken::Real,
89                   StringRef(TokStart, CurPtr - TokStart));
90 }
91
92 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
93 static bool IsIdentifierChar(char c) {
94   return isalnum(c) || c == '_' || c == '$' || c == '.' || c == '@';
95 }
96 AsmToken AsmLexer::LexIdentifier() {
97   // Check for floating point literals.
98   if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
99     // Disambiguate a .1243foo identifier from a floating literal.
100     while (isdigit(*CurPtr))
101       ++CurPtr;
102     if (*CurPtr == 'e' || *CurPtr == 'E' || !IsIdentifierChar(*CurPtr))
103       return LexFloatLiteral();
104   }
105
106   while (IsIdentifierChar(*CurPtr))
107     ++CurPtr;
108   
109   // Handle . as a special case.
110   if (CurPtr == TokStart+1 && TokStart[0] == '.')
111     return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
112   
113   return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
114 }
115
116 /// LexSlash: Slash: /
117 ///           C-Style Comment: /* ... */
118 AsmToken AsmLexer::LexSlash() {
119   switch (*CurPtr) {
120   case '*': break; // C style comment.
121   case '/': return ++CurPtr, LexLineComment();
122   default:  return AsmToken(AsmToken::Slash, StringRef(CurPtr, 1));
123   }
124
125   // C Style comment.
126   ++CurPtr;  // skip the star.
127   while (1) {
128     int CurChar = getNextChar();
129     switch (CurChar) {
130     case EOF:
131       return ReturnError(TokStart, "unterminated comment");
132     case '*':
133       // End of the comment?
134       if (CurPtr[0] != '/') break;
135       
136       ++CurPtr;   // End the */.
137       return LexToken();
138     }
139   }
140 }
141
142 /// LexLineComment: Comment: #[^\n]*
143 ///                        : //[^\n]*
144 AsmToken AsmLexer::LexLineComment() {
145   // FIXME: This is broken if we happen to a comment at the end of a file, which
146   // was .included, and which doesn't end with a newline.
147   int CurChar = getNextChar();
148   while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
149     CurChar = getNextChar();
150   
151   if (CurChar == EOF)
152     return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
153   return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
154 }
155
156 static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
157   if (CurPtr[0] == 'L' && CurPtr[1] == 'L')
158     CurPtr += 2;
159   if (CurPtr[0] == 'U' && CurPtr[1] == 'L' && CurPtr[2] == 'L')
160     CurPtr += 3;
161 }
162
163 /// LexDigit: First character is [0-9].
164 ///   Local Label: [0-9][:]
165 ///   Forward/Backward Label: [0-9][fb]
166 ///   Binary integer: 0b[01]+
167 ///   Octal integer: 0[0-7]+
168 ///   Hex integer: 0x[0-9a-fA-F]+
169 ///   Decimal integer: [1-9][0-9]*
170 AsmToken AsmLexer::LexDigit() {
171   // Decimal integer: [1-9][0-9]*
172   if (CurPtr[-1] != '0' || CurPtr[0] == '.') {
173     while (isdigit(*CurPtr))
174       ++CurPtr;
175
176     // Check for floating point literals.
177     if (*CurPtr == '.' || *CurPtr == 'e') {
178       ++CurPtr;
179       return LexFloatLiteral();
180     }
181
182     StringRef Result(TokStart, CurPtr - TokStart);
183
184     long long Value;
185     if (Result.getAsInteger(10, Value)) {
186       // We have to handle minint_as_a_positive_value specially, because
187       // - minint_as_a_positive_value = minint and it is valid.
188       if (Result == "9223372036854775808")
189         Value = -9223372036854775808ULL;
190       else
191         return ReturnError(TokStart, "Invalid decimal number");
192     }
193     
194     // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
195     // suffixes on integer literals.
196     SkipIgnoredIntegerSuffix(CurPtr);
197     
198     return AsmToken(AsmToken::Integer, Result, Value);
199   }
200   
201   if (*CurPtr == 'b') {
202     ++CurPtr;
203     // See if we actually have "0b" as part of something like "jmp 0b\n"
204     if (!isdigit(CurPtr[0])) {
205       --CurPtr;
206       StringRef Result(TokStart, CurPtr - TokStart);
207       return AsmToken(AsmToken::Integer, Result, 0);
208     }
209     const char *NumStart = CurPtr;
210     while (CurPtr[0] == '0' || CurPtr[0] == '1')
211       ++CurPtr;
212     
213     // Requires at least one binary digit.
214     if (CurPtr == NumStart)
215       return ReturnError(TokStart, "Invalid binary number");
216     
217     StringRef Result(TokStart, CurPtr - TokStart);
218     
219     long long Value;
220     if (Result.substr(2).getAsInteger(2, Value))
221       return ReturnError(TokStart, "Invalid binary number");
222     
223     // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
224     // suffixes on integer literals.
225     SkipIgnoredIntegerSuffix(CurPtr);
226     
227     return AsmToken(AsmToken::Integer, Result, Value);
228   }
229  
230   if (*CurPtr == 'x') {
231     ++CurPtr;
232     const char *NumStart = CurPtr;
233     while (isxdigit(CurPtr[0]))
234       ++CurPtr;
235     
236     // Requires at least one hex digit.
237     if (CurPtr == NumStart)
238       return ReturnError(CurPtr-2, "Invalid hexadecimal number");
239
240     unsigned long long Result;
241     if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
242       return ReturnError(TokStart, "Invalid hexadecimal number");
243       
244     // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
245     // suffixes on integer literals.
246     SkipIgnoredIntegerSuffix(CurPtr);
247     
248     return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
249                     (int64_t)Result);
250   }
251   
252   // Must be an octal number, it starts with 0.
253   while (*CurPtr >= '0' && *CurPtr <= '7')
254     ++CurPtr;
255   
256   StringRef Result(TokStart, CurPtr - TokStart);
257   long long Value;
258   if (Result.getAsInteger(8, Value))
259     return ReturnError(TokStart, "Invalid octal number");
260   
261   // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
262   // suffixes on integer literals.
263   SkipIgnoredIntegerSuffix(CurPtr);
264   
265   return AsmToken(AsmToken::Integer, Result, Value);
266 }
267
268 /// LexQuote: String: "..."
269 AsmToken AsmLexer::LexQuote() {
270   int CurChar = getNextChar();
271   // TODO: does gas allow multiline string constants?
272   while (CurChar != '"') {
273     if (CurChar == '\\') {
274       // Allow \", etc.
275       CurChar = getNextChar();
276     }
277     
278     if (CurChar == EOF)
279       return ReturnError(TokStart, "unterminated string constant");
280
281     CurChar = getNextChar();
282   }
283   
284   return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
285 }
286
287 StringRef AsmLexer::LexUntilEndOfStatement() {
288   TokStart = CurPtr;
289
290   while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
291           *CurPtr != ';' &&  // End of statement marker.
292          *CurPtr != '\n' &&
293          *CurPtr != '\r' &&
294          (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
295     ++CurPtr;
296   }
297   return StringRef(TokStart, CurPtr-TokStart);
298 }
299
300 bool AsmLexer::isAtStartOfComment(char Char) {
301   // FIXME: This won't work for multi-character comment indicators like "//".
302   return Char == *MAI.getCommentString();
303 }
304
305 AsmToken AsmLexer::LexToken() {
306   TokStart = CurPtr;
307   // This always consumes at least one character.
308   int CurChar = getNextChar();
309   
310   if (isAtStartOfComment(CurChar))
311     return LexLineComment();
312
313   switch (CurChar) {
314   default:
315     // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
316     if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
317       return LexIdentifier();
318     
319     // Unknown character, emit an error.
320     return ReturnError(TokStart, "invalid character in input");
321   case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
322   case 0:
323   case ' ':
324   case '\t':
325     // Ignore whitespace.
326     return LexToken();
327   case '\n': // FALL THROUGH.
328   case '\r': // FALL THROUGH.
329   case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
330   case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
331   case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
332   case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
333   case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
334   case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
335   case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
336   case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
337   case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
338   case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
339   case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
340   case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
341   case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
342   case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
343   case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
344   case '=': 
345     if (*CurPtr == '=')
346       return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
347     return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
348   case '|': 
349     if (*CurPtr == '|')
350       return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
351     return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
352   case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
353   case '&': 
354     if (*CurPtr == '&')
355       return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
356     return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
357   case '!': 
358     if (*CurPtr == '=')
359       return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
360     return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
361   case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
362   case '/': return LexSlash();
363   case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
364   case '"': return LexQuote();
365   case '0': case '1': case '2': case '3': case '4':
366   case '5': case '6': case '7': case '8': case '9':
367     return LexDigit();
368   case '<':
369     switch (*CurPtr) {
370     case '<': return ++CurPtr, AsmToken(AsmToken::LessLess, 
371                                         StringRef(TokStart, 2));
372     case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual, 
373                                         StringRef(TokStart, 2));
374     case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater, 
375                                         StringRef(TokStart, 2));
376     default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
377     }
378   case '>':
379     switch (*CurPtr) {
380     case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater, 
381                                         StringRef(TokStart, 2));
382     case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual, 
383                                         StringRef(TokStart, 2));
384     default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
385     }
386       
387   // TODO: Quoted identifiers (objc methods etc)
388   // local labels: [0-9][:]
389   // Forward/backward labels: [0-9][fb]
390   // Integers, fp constants, character constants.
391   }
392 }