* Convert tabs to spaces, fix code alignment
[oota-llvm.git] / lib / AsmParser / Lexer.l
1 /*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the flex scanner for LLVM assembly languages files.
11 //
12 //===----------------------------------------------------------------------===*/
13
14 %option prefix="llvmAsm"
15 %option yylineno
16 %option nostdinit
17 %option never-interactive
18 %option batch
19 %option noyywrap
20 %option nodefault
21 %option 8bit
22 %option outfile="Lexer.cpp"
23 %option ecs
24 %option noreject
25 %option noyymore
26
27 %{
28 #include "ParserInternals.h"
29 #include "llvm/Module.h"
30 #include <list>
31 #include "llvmAsmParser.h"
32 #include <cctype>
33 #include <cstdlib>
34
35 #define RET_TOK(type, Enum, sym) \
36   llvmAsmlval.type = Instruction::Enum; return sym
37
38 namespace llvm {
39
40 // TODO: All of the static identifiers are figured out by the lexer,
41 // these should be hashed to reduce the lexer size
42
43
44 // atoull - Convert an ascii string of decimal digits into the unsigned long
45 // long representation... this does not have to do input error checking,
46 // because we know that the input will be matched by a suitable regex...
47 //
48 static uint64_t atoull(const char *Buffer) {
49   uint64_t Result = 0;
50   for (; *Buffer; Buffer++) {
51     uint64_t OldRes = Result;
52     Result *= 10;
53     Result += *Buffer-'0';
54     if (Result < OldRes)   // Uh, oh, overflow detected!!!
55       ThrowException("constant bigger than 64 bits detected!");
56   }
57   return Result;
58 }
59
60 static uint64_t HexIntToVal(const char *Buffer) {
61   uint64_t Result = 0;
62   for (; *Buffer; ++Buffer) {
63     uint64_t OldRes = Result;
64     Result *= 16;
65     char C = *Buffer;
66     if (C >= '0' && C <= '9')
67       Result += C-'0';
68     else if (C >= 'A' && C <= 'F')
69       Result += C-'A'+10;
70     else if (C >= 'a' && C <= 'f')
71       Result += C-'a'+10;
72
73     if (Result < OldRes)   // Uh, oh, overflow detected!!!
74       ThrowException("constant bigger than 64 bits detected!");
75   }
76   return Result;
77 }
78
79
80 // HexToFP - Convert the ascii string in hexidecimal format to the floating
81 // point representation of it.
82 //
83 static double HexToFP(const char *Buffer) {
84   // Behave nicely in the face of C TBAA rules... see:
85   // http://www.nullstone.com/htmls/category/aliastyp.htm
86   union {
87     uint64_t UI;
88     double FP;
89   } UIntToFP;
90   UIntToFP.UI = HexIntToVal(Buffer);
91
92   assert(sizeof(double) == sizeof(uint64_t) &&
93          "Data sizes incompatible on this target!");
94   return UIntToFP.FP;   // Cast Hex constant to double
95 }
96
97
98 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
99 // appropriate character.  If AllowNull is set to false, a \00 value will cause
100 // an exception to be thrown.
101 //
102 // If AllowNull is set to true, the return value of the function points to the
103 // last character of the string in memory.
104 //
105 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
106   char *BOut = Buffer;
107   for (char *BIn = Buffer; *BIn; ) {
108     if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
109       char Tmp = BIn[3]; BIn[3] = 0;     // Terminate string
110       *BOut = (char)strtol(BIn+1, 0, 16);  // Convert to number
111       if (!AllowNull && !*BOut)
112         ThrowException("String literal cannot accept \\00 escape!");
113
114       BIn[3] = Tmp;                  // Restore character
115       BIn += 3;                      // Skip over handled chars
116       ++BOut;
117     } else {
118       *BOut++ = *BIn++;
119     }
120   }
121
122   return BOut;
123 }
124
125 } // End llvm namespace
126
127 using namespace llvm;
128
129 #define YY_NEVER_INTERACTIVE 1
130 %}
131
132
133
134 /* Comments start with a ; and go till end of line */
135 Comment    ;.*
136
137 /* Variable(Value) identifiers start with a % sign */
138 VarID       %[-a-zA-Z$._][-a-zA-Z$._0-9]*
139
140 /* Label identifiers end with a colon */
141 Label       [-a-zA-Z$._0-9]+:
142 QuoteLabel \"[^\"]+\":
143
144 /* Quoted names can contain any character except " and \ */
145 StringConstant \"[^\"]*\"
146
147
148 /* [PN]Integer: match positive and negative literal integer values that
149  * are preceeded by a '%' character.  These represent unnamed variable slots.
150  */
151 EPInteger     %[0-9]+
152 ENInteger    %-[0-9]+
153
154
155 /* E[PN]Integer: match positive and negative literal integer values */
156 PInteger   [0-9]+
157 NInteger  -[0-9]+
158
159 /* FPConstant - A Floating point constant.
160  */
161 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
162
163 /* HexFPConstant - Floating point constant represented in IEEE format as a
164  *  hexadecimal number for when exponential notation is not precise enough.
165  */
166 HexFPConstant 0x[0-9A-Fa-f]+
167
168 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
169  * it to deal with 64 bit numbers.
170  */
171 HexIntConstant [us]0x[0-9A-Fa-f]+
172 %%
173
174 {Comment}       { /* Ignore comments for now */ }
175
176 begin           { return BEGINTOK; }
177 end             { return ENDTOK; }
178 true            { return TRUETOK;  }
179 false           { return FALSETOK; }
180 declare         { return DECLARE; }
181 global          { return GLOBAL; }
182 constant        { return CONSTANT; }
183 internal        { return INTERNAL; }
184 linkonce        { return LINKONCE; }
185 weak            { return WEAK; }
186 appending       { return APPENDING; }
187 uninitialized   { return EXTERNAL; }    /* Deprecated, turn into external */
188 external        { return EXTERNAL; }
189 implementation  { return IMPLEMENTATION; }
190 zeroinitializer { return ZEROINITIALIZER; }
191 \.\.\.          { return DOTDOTDOT; }
192 undef           { return UNDEF; }
193 null            { return NULL_TOK; }
194 to              { return TO; }
195 except          { RET_TOK(TermOpVal, Unwind, UNWIND); }
196 not             { return NOT; }  /* Deprecated, turned into XOR */
197 tail            { return TAIL; }
198 target          { return TARGET; }
199 triple          { return TRIPLE; }
200 deplibs         { return DEPLIBS; }
201 endian          { return ENDIAN; }
202 pointersize     { return POINTERSIZE; }
203 little          { return LITTLE; }
204 big             { return BIG; }
205 volatile        { return VOLATILE; }
206
207 cc              { return CC_TOK; }
208 ccc             { return CCC_TOK; }
209 fastcc          { return FASTCC_TOK; }
210 coldcc          { return COLDCC_TOK; }
211
212 void            { llvmAsmlval.PrimType = Type::VoidTy  ; return VOID;   }
213 bool            { llvmAsmlval.PrimType = Type::BoolTy  ; return BOOL;   }
214 sbyte           { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE;  }
215 ubyte           { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE;  }
216 short           { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT;  }
217 ushort          { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
218 int             { llvmAsmlval.PrimType = Type::IntTy   ; return INT;    }
219 uint            { llvmAsmlval.PrimType = Type::UIntTy  ; return UINT;   }
220 long            { llvmAsmlval.PrimType = Type::LongTy  ; return LONG;   }
221 ulong           { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG;  }
222 float           { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT;  }
223 double          { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
224 label           { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL;  }
225 type            { return TYPE;   }
226 opaque          { return OPAQUE; }
227
228 add             { RET_TOK(BinaryOpVal, Add, ADD); }
229 sub             { RET_TOK(BinaryOpVal, Sub, SUB); }
230 mul             { RET_TOK(BinaryOpVal, Mul, MUL); }
231 div             { RET_TOK(BinaryOpVal, Div, DIV); }
232 rem             { RET_TOK(BinaryOpVal, Rem, REM); }
233 and             { RET_TOK(BinaryOpVal, And, AND); }
234 or              { RET_TOK(BinaryOpVal, Or , OR ); }
235 xor             { RET_TOK(BinaryOpVal, Xor, XOR); }
236 setne           { RET_TOK(BinaryOpVal, SetNE, SETNE); }
237 seteq           { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
238 setlt           { RET_TOK(BinaryOpVal, SetLT, SETLT); }
239 setgt           { RET_TOK(BinaryOpVal, SetGT, SETGT); }
240 setle           { RET_TOK(BinaryOpVal, SetLE, SETLE); }
241 setge           { RET_TOK(BinaryOpVal, SetGE, SETGE); }
242
243 phi             { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
244 call            { RET_TOK(OtherOpVal, Call, CALL); }
245 cast            { RET_TOK(OtherOpVal, Cast, CAST); }
246 select          { RET_TOK(OtherOpVal, Select, SELECT); }
247 shl             { RET_TOK(OtherOpVal, Shl, SHL); }
248 shr             { RET_TOK(OtherOpVal, Shr, SHR); }
249 vanext          { RET_TOK(OtherOpVal, VANext, VANEXT); }
250 vaarg           { RET_TOK(OtherOpVal, VAArg , VAARG); }
251
252 ret             { RET_TOK(TermOpVal, Ret, RET); }
253 br              { RET_TOK(TermOpVal, Br, BR); }
254 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
255 invoke          { RET_TOK(TermOpVal, Invoke, INVOKE); }
256 unwind          { RET_TOK(TermOpVal, Unwind, UNWIND); }
257 unreachable     { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
258
259 malloc          { RET_TOK(MemOpVal, Malloc, MALLOC); }
260 alloca          { RET_TOK(MemOpVal, Alloca, ALLOCA); }
261 free            { RET_TOK(MemOpVal, Free, FREE); }
262 load            { RET_TOK(MemOpVal, Load, LOAD); }
263 store           { RET_TOK(MemOpVal, Store, STORE); }
264 getelementptr   { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
265
266
267 {VarID}         {
268                   UnEscapeLexed(yytext+1);
269                   llvmAsmlval.StrVal = strdup(yytext+1);             // Skip %
270                   return VAR_ID;
271                 }
272 {Label}         {
273                   yytext[strlen(yytext)-1] = 0;  // nuke colon
274                   UnEscapeLexed(yytext);
275                   llvmAsmlval.StrVal = strdup(yytext);
276                   return LABELSTR;
277                 }
278 {QuoteLabel}    {
279                   yytext[strlen(yytext)-2] = 0;  // nuke colon, end quote
280                   UnEscapeLexed(yytext+1);
281                   llvmAsmlval.StrVal = strdup(yytext+1);
282                   return LABELSTR;
283                 }
284
285 {StringConstant} { // Note that we cannot unescape a string constant here!  The
286                    // string constant might contain a \00 which would not be
287                    // understood by the string stuff.  It is valid to make a
288                    // [sbyte] c"Hello World\00" constant, for example.
289                    //
290                    yytext[strlen(yytext)-1] = 0;           // nuke end quote
291                    llvmAsmlval.StrVal = strdup(yytext+1);  // Nuke start quote
292                    return STRINGCONSTANT;
293                  }
294
295
296 {PInteger}      { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
297 {NInteger}      {
298                   uint64_t Val = atoull(yytext+1);
299                   // +1:  we have bigger negative range
300                   if (Val > (uint64_t)INT64_MAX+1)
301                     ThrowException("Constant too large for signed 64 bits!");
302                   llvmAsmlval.SInt64Val = -Val;
303                   return ESINT64VAL;
304                 }
305 {HexIntConstant} {
306                    llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
307                    return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
308                  }
309
310 {EPInteger}     {
311                   uint64_t Val = atoull(yytext+1);
312                   if ((unsigned)Val != Val)
313                     ThrowException("Invalid value number (too large)!");
314                   llvmAsmlval.UIntVal = unsigned(Val);
315                   return UINTVAL;
316                 }
317 {ENInteger}     {
318                   uint64_t Val = atoull(yytext+2);
319                   // +1:  we have bigger negative range
320                   if (Val > (uint64_t)INT32_MAX+1)
321                     ThrowException("Constant too large for signed 32 bits!");
322                   llvmAsmlval.SIntVal = (int)-Val;
323                   return SINTVAL;
324                 }
325
326 {FPConstant}    { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
327 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
328
329 <<EOF>>         {
330                   /* Make sure to free the internal buffers for flex when we are
331                    * done reading our input!
332                    */
333                   yy_delete_buffer(YY_CURRENT_BUFFER);
334                   return EOF;
335                 }
336
337 [ \r\t\n]       { /* Ignore whitespace */ }
338 .               { return yytext[0]; }
339
340 %%