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