Add some more handlers for ELF section directives.
[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 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
68 AsmToken AsmLexer::LexIdentifier() {
69   while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
70          *CurPtr == '.' || *CurPtr == '@')
71     ++CurPtr;
72   
73   // Handle . as a special case.
74   if (CurPtr == TokStart+1 && TokStart[0] == '.')
75     return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
76   
77   return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
78 }
79
80 /// LexSlash: Slash: /
81 ///           C-Style Comment: /* ... */
82 AsmToken AsmLexer::LexSlash() {
83   switch (*CurPtr) {
84   case '*': break; // C style comment.
85   case '/': return ++CurPtr, LexLineComment();
86   default:  return AsmToken(AsmToken::Slash, StringRef(CurPtr, 1));
87   }
88
89   // C Style comment.
90   ++CurPtr;  // skip the star.
91   while (1) {
92     int CurChar = getNextChar();
93     switch (CurChar) {
94     case EOF:
95       return ReturnError(TokStart, "unterminated comment");
96     case '*':
97       // End of the comment?
98       if (CurPtr[0] != '/') break;
99       
100       ++CurPtr;   // End the */.
101       return LexToken();
102     }
103   }
104 }
105
106 /// LexLineComment: Comment: #[^\n]*
107 ///                        : //[^\n]*
108 AsmToken AsmLexer::LexLineComment() {
109   // FIXME: This is broken if we happen to a comment at the end of a file, which
110   // was .included, and which doesn't end with a newline.
111   int CurChar = getNextChar();
112   while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
113     CurChar = getNextChar();
114   
115   if (CurChar == EOF)
116     return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
117   return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
118 }
119
120
121 /// LexDigit: First character is [0-9].
122 ///   Local Label: [0-9][:]
123 ///   Forward/Backward Label: [0-9][fb]
124 ///   Binary integer: 0b[01]+
125 ///   Octal integer: 0[0-7]+
126 ///   Hex integer: 0x[0-9a-fA-F]+
127 ///   Decimal integer: [1-9][0-9]*
128 /// TODO: FP literal.
129 AsmToken AsmLexer::LexDigit() {
130   // Decimal integer: [1-9][0-9]*
131   if (CurPtr[-1] != '0') {
132     while (isdigit(*CurPtr))
133       ++CurPtr;
134     
135     StringRef Result(TokStart, CurPtr - TokStart);
136     
137     long long Value;
138     if (Result.getAsInteger(10, Value)) {
139       // We have to handle minint_as_a_positive_value specially, because
140       // - minint_as_a_positive_value = minint and it is valid.
141       if (Result == "9223372036854775808")
142         Value = -9223372036854775808ULL;
143       else
144         return ReturnError(TokStart, "Invalid decimal number");
145     }
146     return AsmToken(AsmToken::Integer, Result, Value);
147   }
148   
149   if (*CurPtr == 'b') {
150     ++CurPtr;
151     // See if we actually have "0b" as part of something like "jmp 0b\n"
152     if (!isdigit(CurPtr[0])) {
153       --CurPtr;
154       StringRef Result(TokStart, CurPtr - TokStart);
155       return AsmToken(AsmToken::Integer, Result, 0);
156     }
157     const char *NumStart = CurPtr;
158     while (CurPtr[0] == '0' || CurPtr[0] == '1')
159       ++CurPtr;
160     
161     // Requires at least one binary digit.
162     if (CurPtr == NumStart)
163       return ReturnError(TokStart, "Invalid binary number");
164     
165     StringRef Result(TokStart, CurPtr - TokStart);
166     
167     long long Value;
168     if (Result.getAsInteger(2, Value))
169       return ReturnError(TokStart, "Invalid binary number");
170     
171     return AsmToken(AsmToken::Integer, Result, Value);
172   }
173  
174   if (*CurPtr == 'x') {
175     ++CurPtr;
176     const char *NumStart = CurPtr;
177     while (isxdigit(CurPtr[0]))
178       ++CurPtr;
179     
180     // Requires at least one hex digit.
181     if (CurPtr == NumStart)
182       return ReturnError(CurPtr-2, "Invalid hexadecimal number");
183
184     unsigned long long Result;
185     if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
186       return ReturnError(TokStart, "Invalid hexadecimal number");
187       
188     return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
189                     (int64_t)Result);
190   }
191   
192   // Must be an octal number, it starts with 0.
193   while (*CurPtr >= '0' && *CurPtr <= '7')
194     ++CurPtr;
195   
196   StringRef Result(TokStart, CurPtr - TokStart);
197   long long Value;
198   if (Result.getAsInteger(8, Value))
199     return ReturnError(TokStart, "Invalid octal number");
200   
201   return AsmToken(AsmToken::Integer, Result, Value);
202 }
203
204 /// LexQuote: String: "..."
205 AsmToken AsmLexer::LexQuote() {
206   int CurChar = getNextChar();
207   // TODO: does gas allow multiline string constants?
208   while (CurChar != '"') {
209     if (CurChar == '\\') {
210       // Allow \", etc.
211       CurChar = getNextChar();
212     }
213     
214     if (CurChar == EOF)
215       return ReturnError(TokStart, "unterminated string constant");
216
217     CurChar = getNextChar();
218   }
219   
220   return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
221 }
222
223 StringRef AsmLexer::LexUntilEndOfStatement() {
224   TokStart = CurPtr;
225
226   while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
227           *CurPtr != ';' &&  // End of statement marker.
228          *CurPtr != '\n' &&
229          *CurPtr != '\r' &&
230          (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
231     ++CurPtr;
232   }
233   return StringRef(TokStart, CurPtr-TokStart);
234 }
235
236 bool AsmLexer::isAtStartOfComment(char Char) {
237   // FIXME: This won't work for multi-character comment indicators like "//".
238   return Char == *MAI.getCommentString();
239 }
240
241 AsmToken AsmLexer::LexToken() {
242   TokStart = CurPtr;
243   // This always consumes at least one character.
244   int CurChar = getNextChar();
245   
246   if (isAtStartOfComment(CurChar))
247     return LexLineComment();
248
249   switch (CurChar) {
250   default:
251     // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
252     if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
253       return LexIdentifier();
254     
255     // Unknown character, emit an error.
256     return ReturnError(TokStart, "invalid character in input");
257   case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
258   case 0:
259   case ' ':
260   case '\t':
261     // Ignore whitespace.
262     return LexToken();
263   case '\n': // FALL THROUGH.
264   case '\r': // FALL THROUGH.
265   case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
266   case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
267   case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
268   case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
269   case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
270   case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
271   case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
272   case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
273   case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
274   case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
275   case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
276   case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
277   case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
278   case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
279   case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
280   case '=': 
281     if (*CurPtr == '=')
282       return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
283     return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
284   case '|': 
285     if (*CurPtr == '|')
286       return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
287     return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
288   case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
289   case '&': 
290     if (*CurPtr == '&')
291       return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
292     return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
293   case '!': 
294     if (*CurPtr == '=')
295       return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
296     return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
297   case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
298   case '/': return LexSlash();
299   case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
300   case '"': return LexQuote();
301   case '0': case '1': case '2': case '3': case '4':
302   case '5': case '6': case '7': case '8': case '9':
303     return LexDigit();
304   case '<':
305     switch (*CurPtr) {
306     case '<': return ++CurPtr, AsmToken(AsmToken::LessLess, 
307                                         StringRef(TokStart, 2));
308     case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual, 
309                                         StringRef(TokStart, 2));
310     case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater, 
311                                         StringRef(TokStart, 2));
312     default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
313     }
314   case '>':
315     switch (*CurPtr) {
316     case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater, 
317                                         StringRef(TokStart, 2));
318     case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual, 
319                                         StringRef(TokStart, 2));
320     default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
321     }
322       
323   // TODO: Quoted identifiers (objc methods etc)
324   // local labels: [0-9][:]
325   // Forward/backward labels: [0-9][fb]
326   // Integers, fp constants, character constants.
327   }
328 }