Avoid TRUE and FALSE which apparently conflict with some macros on OSX
[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 = 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
143 /* Quoted names can contain any character except " and \ */
144 StringConstant \"[^\"]*\"
145
146
147 /* [PN]Integer: match positive and negative literal integer values that
148  * are preceeded by a '%' character.  These represent unnamed variable slots.
149  */
150 EPInteger     %[0-9]+
151 ENInteger    %-[0-9]+
152
153
154 /* E[PN]Integer: match positive and negative literal integer values */
155 PInteger   [0-9]+
156 NInteger  -[0-9]+
157
158 /* FPConstant - A Floating point constant.
159  */
160 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
161
162 /* HexFPConstant - Floating point constant represented in IEEE format as a
163  *  hexadecimal number for when exponential notation is not precise enough.
164  */
165 HexFPConstant 0x[0-9A-Fa-f]+
166
167 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
168  * it to deal with 64 bit numbers.
169  */
170 HexIntConstant [us]0x[0-9A-Fa-f]+
171 %%
172
173 {Comment}       { /* Ignore comments for now */ }
174
175 begin           { return BEGINTOK; }
176 end             { return ENDTOK; }
177 true            { return TRUETOK;  }
178 false           { return FALSETOK; }
179 declare         { return DECLARE; }
180 global          { return GLOBAL; }
181 constant        { return CONSTANT; }
182 const           { return CONST; }
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 null            { return NULL_TOK; }
193 to              { return TO; }
194 except          { RET_TOK(TermOpVal, Unwind, UNWIND); }
195 not             { return NOT; }  /* Deprecated, turned into XOR */
196 target          { return TARGET; }
197 endian          { return ENDIAN; }
198 pointersize     { return POINTERSIZE; }
199 little          { return LITTLE; }
200 big             { return BIG; }
201 volatile        { return VOLATILE; }
202
203 void            { llvmAsmlval.PrimType = Type::VoidTy  ; return VOID;   }
204 bool            { llvmAsmlval.PrimType = Type::BoolTy  ; return BOOL;   }
205 sbyte           { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE;  }
206 ubyte           { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE;  }
207 short           { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT;  }
208 ushort          { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
209 int             { llvmAsmlval.PrimType = Type::IntTy   ; return INT;    }
210 uint            { llvmAsmlval.PrimType = Type::UIntTy  ; return UINT;   }
211 long            { llvmAsmlval.PrimType = Type::LongTy  ; return LONG;   }
212 ulong           { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG;  }
213 float           { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT;  }
214 double          { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
215 type            { llvmAsmlval.PrimType = Type::TypeTy  ; return TYPE;   }
216 label           { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL;  }
217 opaque          { return OPAQUE; }
218
219 add             { RET_TOK(BinaryOpVal, Add, ADD); }
220 sub             { RET_TOK(BinaryOpVal, Sub, SUB); }
221 mul             { RET_TOK(BinaryOpVal, Mul, MUL); }
222 div             { RET_TOK(BinaryOpVal, Div, DIV); }
223 rem             { RET_TOK(BinaryOpVal, Rem, REM); }
224 and             { RET_TOK(BinaryOpVal, And, AND); }
225 or              { RET_TOK(BinaryOpVal, Or , OR ); }
226 xor             { RET_TOK(BinaryOpVal, Xor, XOR); }
227 setne           { RET_TOK(BinaryOpVal, SetNE, SETNE); }
228 seteq           { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
229 setlt           { RET_TOK(BinaryOpVal, SetLT, SETLT); }
230 setgt           { RET_TOK(BinaryOpVal, SetGT, SETGT); }
231 setle           { RET_TOK(BinaryOpVal, SetLE, SETLE); }
232 setge           { RET_TOK(BinaryOpVal, SetGE, SETGE); }
233
234 phi             { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
235 call            { RET_TOK(OtherOpVal, Call, CALL); }
236 cast            { RET_TOK(OtherOpVal, Cast, CAST); }
237 select          { RET_TOK(OtherOpVal, Select, SELECT); }
238 shl             { RET_TOK(OtherOpVal, Shl, SHL); }
239 shr             { RET_TOK(OtherOpVal, Shr, SHR); }
240 va_arg          { return VA_ARG; /* FIXME: OBSOLETE */}
241 vanext          { RET_TOK(OtherOpVal, VANext, VANEXT); }
242 vaarg           { RET_TOK(OtherOpVal, VAArg , VAARG); }
243
244 ret             { RET_TOK(TermOpVal, Ret, RET); }
245 br              { RET_TOK(TermOpVal, Br, BR); }
246 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
247 invoke          { RET_TOK(TermOpVal, Invoke, INVOKE); }
248 unwind          { RET_TOK(TermOpVal, Unwind, UNWIND); }
249
250
251 malloc          { RET_TOK(MemOpVal, Malloc, MALLOC); }
252 alloca          { RET_TOK(MemOpVal, Alloca, ALLOCA); }
253 free            { RET_TOK(MemOpVal, Free, FREE); }
254 load            { RET_TOK(MemOpVal, Load, LOAD); }
255 store           { RET_TOK(MemOpVal, Store, STORE); }
256 getelementptr   { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
257
258
259 {VarID}         {
260                   UnEscapeLexed(yytext+1);
261                   llvmAsmlval.StrVal = strdup(yytext+1);             // Skip %
262                   return VAR_ID; 
263                 }
264 {Label}         {
265                   yytext[strlen(yytext)-1] = 0;  // nuke colon
266                   UnEscapeLexed(yytext);
267                   llvmAsmlval.StrVal = strdup(yytext);
268                   return LABELSTR; 
269                 }
270
271 {StringConstant} { // Note that we cannot unescape a string constant here!  The
272                    // string constant might contain a \00 which would not be 
273                    // understood by the string stuff.  It is valid to make a
274                    // [sbyte] c"Hello World\00" constant, for example.
275                    //
276                   yytext[strlen(yytext)-1] = 0;           // nuke end quote
277                   llvmAsmlval.StrVal = strdup(yytext+1);  // Nuke start quote
278                   return STRINGCONSTANT;
279                  }
280
281
282 {PInteger}      { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
283 {NInteger}      { 
284                   uint64_t Val = atoull(yytext+1);
285                   // +1:  we have bigger negative range
286                   if (Val > (uint64_t)INT64_MAX+1)
287                     ThrowException("Constant too large for signed 64 bits!");
288                   llvmAsmlval.SInt64Val = -Val; 
289                   return ESINT64VAL; 
290                 }
291 {HexIntConstant} {
292                    llvmAsmlval.UInt64Val = HexIntToVal(yytext+3); 
293                    return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
294                  }
295
296 {EPInteger}     { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
297 {ENInteger}     {
298                   uint64_t Val = atoull(yytext+2);
299                   // +1:  we have bigger negative range
300                   if (Val > (uint64_t)INT32_MAX+1)
301                     ThrowException("Constant too large for signed 32 bits!");
302                   llvmAsmlval.SIntVal = -Val;
303                   return SINTVAL;
304                 }
305
306 {FPConstant}    { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
307 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
308
309 <<EOF>>         {
310                   /* Make sure to free the internal buffers for flex when we are
311                    * done reading our input!
312                    */
313                   yy_delete_buffer(YY_CURRENT_BUFFER);
314                   return EOF;
315                 }
316
317 [ \t\n]         { /* Ignore whitespace */ }
318 .               { return yytext[0]; }
319
320 %%