For PR950:
[oota-llvm.git] / lib / AsmParser / Lexer.l.cvs
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 void set_scan_file(FILE * F){
36   yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
37 }
38 void set_scan_string (const char * str) {
39   yy_scan_string (str);
40 }
41
42 // Construct a token value for a non-obsolete token
43 #define RET_TOK(type, Enum, sym) \
44   llvmAsmlval.type = Instruction::Enum; \
45   return sym
46
47 // Construct a token value for an obsolete token
48 #define RET_TY(CTYPE, SYM) \
49   llvmAsmlval.PrimType = CTYPE;\
50   return SYM
51
52 namespace llvm {
53
54 // TODO: All of the static identifiers are figured out by the lexer,
55 // these should be hashed to reduce the lexer size
56
57
58 // atoull - Convert an ascii string of decimal digits into the unsigned long
59 // long representation... this does not have to do input error checking,
60 // because we know that the input will be matched by a suitable regex...
61 //
62 static uint64_t atoull(const char *Buffer) {
63   uint64_t Result = 0;
64   for (; *Buffer; Buffer++) {
65     uint64_t OldRes = Result;
66     Result *= 10;
67     Result += *Buffer-'0';
68     if (Result < OldRes)   // Uh, oh, overflow detected!!!
69       GenerateError("constant bigger than 64 bits detected!");
70   }
71   return Result;
72 }
73
74 static uint64_t HexIntToVal(const char *Buffer) {
75   uint64_t Result = 0;
76   for (; *Buffer; ++Buffer) {
77     uint64_t OldRes = Result;
78     Result *= 16;
79     char C = *Buffer;
80     if (C >= '0' && C <= '9')
81       Result += C-'0';
82     else if (C >= 'A' && C <= 'F')
83       Result += C-'A'+10;
84     else if (C >= 'a' && C <= 'f')
85       Result += C-'a'+10;
86
87     if (Result < OldRes)   // Uh, oh, overflow detected!!!
88       GenerateError("constant bigger than 64 bits detected!");
89   }
90   return Result;
91 }
92
93
94 // HexToFP - Convert the ascii string in hexidecimal format to the floating
95 // point representation of it.
96 //
97 static double HexToFP(const char *Buffer) {
98   // Behave nicely in the face of C TBAA rules... see:
99   // http://www.nullstone.com/htmls/category/aliastyp.htm
100   union {
101     uint64_t UI;
102     double FP;
103   } UIntToFP;
104   UIntToFP.UI = HexIntToVal(Buffer);
105
106   assert(sizeof(double) == sizeof(uint64_t) &&
107          "Data sizes incompatible on this target!");
108   return UIntToFP.FP;   // Cast Hex constant to double
109 }
110
111
112 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
113 // appropriate character.  If AllowNull is set to false, a \00 value will cause
114 // an exception to be thrown.
115 //
116 // If AllowNull is set to true, the return value of the function points to the
117 // last character of the string in memory.
118 //
119 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
120   char *BOut = Buffer;
121   for (char *BIn = Buffer; *BIn; ) {
122     if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
123       char Tmp = BIn[3]; BIn[3] = 0;     // Terminate string
124       *BOut = (char)strtol(BIn+1, 0, 16);  // Convert to number
125       if (!AllowNull && !*BOut)
126         GenerateError("String literal cannot accept \\00 escape!");
127
128       BIn[3] = Tmp;                  // Restore character
129       BIn += 3;                      // Skip over handled chars
130       ++BOut;
131     } else {
132       *BOut++ = *BIn++;
133     }
134   }
135
136   return BOut;
137 }
138
139 } // End llvm namespace
140
141 using namespace llvm;
142
143 #define YY_NEVER_INTERACTIVE 1
144 %}
145
146
147
148 /* Comments start with a ; and go till end of line */
149 Comment    ;.*
150
151 /* Variable(Value) identifiers start with a % sign */
152 VarID       %[-a-zA-Z$._][-a-zA-Z$._0-9]*
153
154 /* Label identifiers end with a colon */
155 Label       [-a-zA-Z$._0-9]+:
156 QuoteLabel \"[^\"]+\":
157
158 /* Quoted names can contain any character except " and \ */
159 StringConstant \"[^\"]*\"
160
161
162 /* [PN]Integer: match positive and negative literal integer values that
163  * are preceeded by a '%' character.  These represent unnamed variable slots.
164  */
165 EPInteger     %[0-9]+
166 ENInteger    %-[0-9]+
167
168
169 /* E[PN]Integer: match positive and negative literal integer values */
170 PInteger   [0-9]+
171 NInteger  -[0-9]+
172
173 /* FPConstant - A Floating point constant.
174  */
175 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
176
177 /* HexFPConstant - Floating point constant represented in IEEE format as a
178  *  hexadecimal number for when exponential notation is not precise enough.
179  */
180 HexFPConstant 0x[0-9A-Fa-f]+
181
182 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
183  * it to deal with 64 bit numbers.
184  */
185 HexIntConstant [us]0x[0-9A-Fa-f]+
186 %%
187
188 {Comment}       { /* Ignore comments for now */ }
189
190 begin           { return BEGINTOK; }
191 end             { return ENDTOK; }
192 true            { return TRUETOK;  }
193 false           { return FALSETOK; }
194 declare         { return DECLARE; }
195 global          { return GLOBAL; }
196 constant        { return CONSTANT; }
197 internal        { return INTERNAL; }
198 linkonce        { return LINKONCE; }
199 weak            { return WEAK; }
200 appending       { return APPENDING; }
201 dllimport       { return DLLIMPORT; }
202 dllexport       { return DLLEXPORT; }
203 extern_weak     { return EXTERN_WEAK; }
204 external        { return EXTERNAL; }
205 implementation  { return IMPLEMENTATION; }
206 zeroinitializer { return ZEROINITIALIZER; }
207 \.\.\.          { return DOTDOTDOT; }
208 undef           { return UNDEF; }
209 null            { return NULL_TOK; }
210 to              { return TO; }
211 tail            { return TAIL; }
212 target          { return TARGET; }
213 triple          { return TRIPLE; }
214 deplibs         { return DEPLIBS; }
215 endian          { return ENDIAN; }
216 pointersize     { return POINTERSIZE; }
217 datalayout      { return DATALAYOUT; }
218 little          { return LITTLE; }
219 big             { return BIG; }
220 volatile        { return VOLATILE; }
221 align           { return ALIGN;  }
222 section         { return SECTION; }
223 module          { return MODULE; }
224 asm             { return ASM_TOK; }
225 sideeffect      { return SIDEEFFECT; }
226
227 cc              { return CC_TOK; }
228 ccc             { return CCC_TOK; }
229 csretcc         { return CSRETCC_TOK; }
230 fastcc          { return FASTCC_TOK; }
231 coldcc          { return COLDCC_TOK; }
232 x86_stdcallcc   { return X86_STDCALLCC_TOK; }
233 x86_fastcallcc  { return X86_FASTCALLCC_TOK; }
234
235 void            { RET_TY(Type::VoidTy,  VOID);  }
236 bool            { RET_TY(Type::BoolTy,  BOOL);  }
237 sbyte           { RET_TY(Type::SByteTy, SBYTE); }
238 ubyte           { RET_TY(Type::UByteTy, UBYTE); }
239 short           { RET_TY(Type::ShortTy, SHORT); }
240 ushort          { RET_TY(Type::UShortTy,USHORT);}
241 int             { RET_TY(Type::IntTy,   INT);   }
242 uint            { RET_TY(Type::UIntTy,  UINT);  }
243 long            { RET_TY(Type::LongTy,  LONG);  }
244 ulong           { RET_TY(Type::ULongTy, ULONG); }
245 float           { RET_TY(Type::FloatTy, FLOAT); }
246 double          { RET_TY(Type::DoubleTy,DOUBLE);}
247 label           { RET_TY(Type::LabelTy, LABEL); }
248 type            { return TYPE;   }
249 opaque          { return OPAQUE; }
250
251 add             { RET_TOK(BinaryOpVal, Add, ADD); }
252 sub             { RET_TOK(BinaryOpVal, Sub, SUB); }
253 mul             { RET_TOK(BinaryOpVal, Mul, MUL); }
254 udiv            { RET_TOK(BinaryOpVal, UDiv, UDIV); }
255 sdiv            { RET_TOK(BinaryOpVal, SDiv, SDIV); }
256 fdiv            { RET_TOK(BinaryOpVal, FDiv, FDIV); }
257 urem            { RET_TOK(BinaryOpVal, URem, UREM); }
258 srem            { RET_TOK(BinaryOpVal, SRem, SREM); }
259 frem            { RET_TOK(BinaryOpVal, FRem, FREM); }
260 and             { RET_TOK(BinaryOpVal, And, AND); }
261 or              { RET_TOK(BinaryOpVal, Or , OR ); }
262 xor             { RET_TOK(BinaryOpVal, Xor, XOR); }
263 icmp            { RET_TOK(OtherOpVal,  ICmp,  ICMP); }
264 fcmp            { RET_TOK(OtherOpVal,  FCmp,  FCMP); }
265 eq              { return EQ;  }
266 ne              { return NE;  }
267 slt             { return SLT; }
268 sgt             { return SGT; }
269 sle             { return SLE; }
270 sge             { return SGE; }
271 ult             { return ULT; }
272 ugt             { return UGT; }
273 ule             { return ULE; }
274 uge             { return UGE; }
275 oeq             { return OEQ; }
276 one             { return ONE; }
277 olt             { return OLT; }
278 ogt             { return OGT; }
279 ole             { return OLE; }
280 oge             { return OGE; }
281 ord             { return ORD; }
282 uno             { return UNO; }
283 ueq             { return UEQ; }
284 une             { return UNE; }
285
286 phi             { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
287 call            { RET_TOK(OtherOpVal, Call, CALL); }
288 trunc           { RET_TOK(CastOpVal, Trunc, TRUNC); }
289 zext            { RET_TOK(CastOpVal, ZExt, ZEXT); }
290 sext            { RET_TOK(CastOpVal, SExt, SEXT); }
291 fptrunc         { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
292 fpext           { RET_TOK(CastOpVal, FPExt, FPEXT); }
293 uitofp          { RET_TOK(CastOpVal, UIToFP, UITOFP); }
294 sitofp          { RET_TOK(CastOpVal, SIToFP, SITOFP); }
295 fptoui          { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
296 fptosi          { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
297 inttoptr        { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
298 ptrtoint        { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
299 bitcast         { RET_TOK(CastOpVal, BitCast, BITCAST); }
300 select          { RET_TOK(OtherOpVal, Select, SELECT); }
301 shl             { RET_TOK(OtherOpVal, Shl, SHL); }
302 lshr            { RET_TOK(OtherOpVal, LShr, LSHR); }
303 ashr            { RET_TOK(OtherOpVal, AShr, ASHR); }
304 va_arg          { RET_TOK(OtherOpVal, VAArg , VAARG); }
305 ret             { RET_TOK(TermOpVal, Ret, RET); }
306 br              { RET_TOK(TermOpVal, Br, BR); }
307 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
308 invoke          { RET_TOK(TermOpVal, Invoke, INVOKE); }
309 unwind          { RET_TOK(TermOpVal, Unwind, UNWIND); }
310 unreachable     { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
311
312 malloc          { RET_TOK(MemOpVal, Malloc, MALLOC); }
313 alloca          { RET_TOK(MemOpVal, Alloca, ALLOCA); }
314 free            { RET_TOK(MemOpVal, Free, FREE); }
315 load            { RET_TOK(MemOpVal, Load, LOAD); }
316 store           { RET_TOK(MemOpVal, Store, STORE); }
317 getelementptr   { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
318
319 extractelement  { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
320 insertelement   { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
321 shufflevector   { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
322
323
324 {VarID}         {
325                   UnEscapeLexed(yytext+1);
326                   llvmAsmlval.StrVal = strdup(yytext+1);             // Skip %
327                   return VAR_ID;
328                 }
329 {Label}         {
330                   yytext[strlen(yytext)-1] = 0;  // nuke colon
331                   UnEscapeLexed(yytext);
332                   llvmAsmlval.StrVal = strdup(yytext);
333                   return LABELSTR;
334                 }
335 {QuoteLabel}    {
336                   yytext[strlen(yytext)-2] = 0;  // nuke colon, end quote
337                   UnEscapeLexed(yytext+1);
338                   llvmAsmlval.StrVal = strdup(yytext+1);
339                   return LABELSTR;
340                 }
341
342 {StringConstant} { // Note that we cannot unescape a string constant here!  The
343                    // string constant might contain a \00 which would not be
344                    // understood by the string stuff.  It is valid to make a
345                    // [sbyte] c"Hello World\00" constant, for example.
346                    //
347                    yytext[strlen(yytext)-1] = 0;           // nuke end quote
348                    llvmAsmlval.StrVal = strdup(yytext+1);  // Nuke start quote
349                    return STRINGCONSTANT;
350                  }
351
352
353 {PInteger}      { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
354 {NInteger}      {
355                   uint64_t Val = atoull(yytext+1);
356                   // +1:  we have bigger negative range
357                   if (Val > (uint64_t)INT64_MAX+1)
358                     GenerateError("Constant too large for signed 64 bits!");
359                   llvmAsmlval.SInt64Val = -Val;
360                   return ESINT64VAL;
361                 }
362 {HexIntConstant} {
363                    llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
364                    return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
365                  }
366
367 {EPInteger}     {
368                   uint64_t Val = atoull(yytext+1);
369                   if ((unsigned)Val != Val)
370                     GenerateError("Invalid value number (too large)!");
371                   llvmAsmlval.UIntVal = unsigned(Val);
372                   return UINTVAL;
373                 }
374 {ENInteger}     {
375                   uint64_t Val = atoull(yytext+2);
376                   // +1:  we have bigger negative range
377                   if (Val > (uint64_t)INT32_MAX+1)
378                     GenerateError("Constant too large for signed 32 bits!");
379                   llvmAsmlval.SIntVal = (int)-Val;
380                   return SINTVAL;
381                 }
382
383 {FPConstant}    { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
384 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
385
386 <<EOF>>         {
387                   /* Make sure to free the internal buffers for flex when we are
388                    * done reading our input!
389                    */
390                   yy_delete_buffer(YY_CURRENT_BUFFER);
391                   return EOF;
392                 }
393
394 [ \r\t\n]       { /* Ignore whitespace */ }
395 .               { return yytext[0]; }
396
397 %%