4a68d0d354c8571c49b12b477eb9c3d6f9e59e61
[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);   // Skip %
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
198 asmtok::TokKind AsmLexer::LexToken() {
199   TokStart = CurPtr;
200   // This always consumes at least one character.
201   int CurChar = getNextChar();
202   
203   switch (CurChar) {
204   default:
205     // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
206     if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
207       return LexIdentifier();
208     
209     // Unknown character, emit an error.
210     return asmtok::Error;
211   case EOF: return asmtok::Eof;
212   case 0:
213   case ' ':
214   case '\t':
215     // Ignore whitespace.
216     return LexToken();
217   case '\n': // FALL THROUGH.
218   case '\r': // FALL THROUGH.
219   case ';': return asmtok::EndOfStatement;
220   case ':': return asmtok::Colon;
221   case '+': return asmtok::Plus;
222   case '-': return asmtok::Minus;
223   case '(': return asmtok::LParen;
224   case ')': return asmtok::RParen;
225   case '*': return asmtok::Star;
226   case ',': return asmtok::Comma;
227   case '$': return asmtok::Dollar;
228   case '%': return LexPercent();
229   case '/': return LexSlash();
230   case '#': return LexHash();
231   case '0': case '1': case '2': case '3': case '4':
232   case '5': case '6': case '7': case '8': case '9':
233     return LexDigit();
234       
235   // TODO: Quoted identifiers (objc methods etc)
236   // local labels: [0-9][:]
237   // Forward/backward labels: [0-9][fb]
238   // Integers, fp constants, character constants.
239   }
240 }