Generated files for previous patch.
[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 "llvm/Support/MathExtras.h"
31 #include <list>
32 #include "llvmAsmParser.h"
33 #include <cctype>
34 #include <cstdlib>
35
36 void set_scan_file(FILE * F){
37   yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
38 }
39 void set_scan_string (const char * str) {
40   yy_scan_string (str);
41 }
42
43 // Construct a token value for a non-obsolete token
44 #define RET_TOK(type, Enum, sym) \
45   llvmAsmlval.type = Instruction::Enum; \
46   return sym
47
48 // Construct a token value for an obsolete token
49 #define RET_TY(CTYPE, SYM) \
50   llvmAsmlval.PrimType = CTYPE;\
51   return SYM
52
53 namespace llvm {
54
55 // TODO: All of the static identifiers are figured out by the lexer,
56 // these should be hashed to reduce the lexer size
57
58
59 // atoull - Convert an ascii string of decimal digits into the unsigned long
60 // long representation... this does not have to do input error checking,
61 // because we know that the input will be matched by a suitable regex...
62 //
63 static uint64_t atoull(const char *Buffer) {
64   uint64_t Result = 0;
65   for (; *Buffer; Buffer++) {
66     uint64_t OldRes = Result;
67     Result *= 10;
68     Result += *Buffer-'0';
69     if (Result < OldRes)   // Uh, oh, overflow detected!!!
70       GenerateError("constant bigger than 64 bits detected!");
71   }
72   return Result;
73 }
74
75 static uint64_t HexIntToVal(const char *Buffer) {
76   uint64_t Result = 0;
77   for (; *Buffer; ++Buffer) {
78     uint64_t OldRes = Result;
79     Result *= 16;
80     char C = *Buffer;
81     if (C >= '0' && C <= '9')
82       Result += C-'0';
83     else if (C >= 'A' && C <= 'F')
84       Result += C-'A'+10;
85     else if (C >= 'a' && C <= 'f')
86       Result += C-'a'+10;
87
88     if (Result < OldRes)   // Uh, oh, overflow detected!!!
89       GenerateError("constant bigger than 64 bits detected!");
90   }
91   return Result;
92 }
93
94 // HexToFP - Convert the ascii string in hexadecimal format to the floating
95 // point representation of it.
96 //
97 static double HexToFP(const char *Buffer) {
98   return BitsToDouble(HexIntToVal(Buffer));   // Cast Hex constant to double
99 }
100
101 static void HexToIntPair(const char *Buffer, uint64_t Pair[2]) {
102   Pair[0] = 0;
103   for (int i=0; i<16; i++, Buffer++) {
104     assert(*Buffer);
105     Pair[0] *= 16;
106     char C = *Buffer;
107     if (C >= '0' && C <= '9')
108       Pair[0] += C-'0';
109     else if (C >= 'A' && C <= 'F')
110       Pair[0] += C-'A'+10;
111     else if (C >= 'a' && C <= 'f')
112       Pair[0] += C-'a'+10;
113   }
114   Pair[1] = 0;
115   for (int i=0; i<16 && *Buffer; i++, Buffer++) {
116     Pair[1] *= 16;
117     char C = *Buffer;
118     if (C >= '0' && C <= '9')
119       Pair[1] += C-'0';
120     else if (C >= 'A' && C <= 'F')
121       Pair[1] += C-'A'+10;
122     else if (C >= 'a' && C <= 'f')
123       Pair[1] += C-'a'+10;
124   }
125   if (*Buffer)
126     GenerateError("constant bigger than 128 bits detected!");
127 }
128
129 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
130 // appropriate character.
131 char *UnEscapeLexed(char *Buffer, char* EndBuffer) {
132   char *BOut = Buffer;
133   for (char *BIn = Buffer; *BIn; ) {
134     if (BIn[0] == '\\') {
135       if (BIn < EndBuffer-1 && BIn[1] == '\\') {
136         *BOut++ = '\\'; // Two \ becomes one
137         BIn += 2;
138       } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
139         char Tmp = BIn[3]; BIn[3] = 0;      // Terminate string
140         *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
141         BIn[3] = Tmp;                       // Restore character
142         BIn += 3;                           // Skip over handled chars
143         ++BOut;
144       } else {
145         *BOut++ = *BIn++;
146       }
147     } else {
148       *BOut++ = *BIn++;
149     }
150   }
151   return BOut;
152 }
153
154 } // End llvm namespace
155
156 using namespace llvm;
157
158 #define YY_NEVER_INTERACTIVE 1
159 %}
160
161
162
163 /* Comments start with a ; and go till end of line */
164 Comment    ;.*
165
166 /* Local Values and Type identifiers start with a % sign */
167 LocalVarName       %[-a-zA-Z$._][-a-zA-Z$._0-9]*
168
169 /* Global Value identifiers start with an @ sign */
170 GlobalVarName       @[-a-zA-Z$._][-a-zA-Z$._0-9]*
171
172 /* Label identifiers end with a colon */
173 Label       [-a-zA-Z$._0-9]+:
174 QuoteLabel \"[^\"]+\":
175
176 /* Quoted names can contain any character except " and \ */
177 StringConstant \"[^\"]*\"
178 AtStringConstant @\"[^\"]*\"
179 PctStringConstant %\"[^\"]*\"
180   
181 /* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
182 LocalVarID     %[0-9]+
183 GlobalVarID    @[0-9]+
184
185 /* Integer types are specified with i and a bitwidth */
186 IntegerType i[0-9]+
187
188 /* E[PN]Integer: match positive and negative literal integer values. */
189 PInteger   [0-9]+
190 NInteger  -[0-9]+
191
192 /* FPConstant - A Floating point constant.  Float and double only.
193  */
194 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
195
196 /* HexFPConstant - Floating point constant represented in IEEE format as a
197  *  hexadecimal number for when exponential notation is not precise enough.
198  *  Float and double only.
199  */
200 HexFPConstant 0x[0-9A-Fa-f]+
201
202 /* F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
203  */
204 HexFP80Constant 0xK[0-9A-Fa-f]+
205
206 /* F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
207  */
208 HexFP128Constant 0xL[0-9A-Fa-f]+
209
210 /* PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
211  */
212 HexPPC128Constant 0xM[0-9A-Fa-f]+
213
214 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
215  * it to deal with 64 bit numbers.
216  */
217 HexIntConstant [us]0x[0-9A-Fa-f]+
218
219 /* WSNL - shorthand for whitespace followed by newline */
220 WSNL [ \r\t]*$
221 %%
222
223 {Comment}       { /* Ignore comments for now */ }
224
225 begin           { return BEGINTOK; }
226 end             { return ENDTOK; }
227 true            { return TRUETOK;  }
228 false           { return FALSETOK; }
229 declare         { return DECLARE; }
230 define          { return DEFINE; }
231 global          { return GLOBAL; }
232 constant        { return CONSTANT; }
233 internal        { return INTERNAL; }
234 linkonce        { return LINKONCE; }
235 weak            { return WEAK; }
236 appending       { return APPENDING; }
237 dllimport       { return DLLIMPORT; }
238 dllexport       { return DLLEXPORT; }
239 hidden          { return HIDDEN; }
240 protected       { return PROTECTED; }
241 extern_weak     { return EXTERN_WEAK; }
242 external        { return EXTERNAL; }
243 thread_local    { return THREAD_LOCAL; }
244 zeroinitializer { return ZEROINITIALIZER; }
245 \.\.\.          { return DOTDOTDOT; }
246 undef           { return UNDEF; }
247 null            { return NULL_TOK; }
248 to              { return TO; }
249 tail            { return TAIL; }
250 target          { return TARGET; }
251 triple          { return TRIPLE; }
252 deplibs         { return DEPLIBS; }
253 datalayout      { return DATALAYOUT; }
254 volatile        { return VOLATILE; }
255 align           { return ALIGN;  }
256 section         { return SECTION; }
257 alias           { return ALIAS; }
258 module          { return MODULE; }
259 asm             { return ASM_TOK; }
260 sideeffect      { return SIDEEFFECT; }
261
262 cc              { return CC_TOK; }
263 ccc             { return CCC_TOK; }
264 fastcc          { return FASTCC_TOK; }
265 coldcc          { return COLDCC_TOK; }
266 x86_stdcallcc   { return X86_STDCALLCC_TOK; }
267 x86_fastcallcc  { return X86_FASTCALLCC_TOK; }
268
269 signext         { return SIGNEXT; }
270 zeroext         { return ZEROEXT; }
271 inreg           { return INREG; }
272 sret            { return SRET;  }
273 nounwind        { return NOUNWIND; }
274 noreturn        { return NORETURN; }
275 noalias         { return NOALIAS; }
276 byval           { return BYVAL; }
277 nest            { return NEST; }
278 sext{WSNL}      { // For auto-upgrade only, drop in LLVM 3.0 
279                   return SIGNEXT; } 
280 zext{WSNL}      { // For auto-upgrade only, drop in LLVM 3.0
281                   return ZEROEXT; } 
282
283 void            { RET_TY(Type::VoidTy,  VOID);  }
284 float           { RET_TY(Type::FloatTy, FLOAT); }
285 double          { RET_TY(Type::DoubleTy,DOUBLE);}
286 x86_fp80        { RET_TY(Type::X86_FP80Ty, X86_FP80);}
287 fp128           { RET_TY(Type::FP128Ty, FP128);}
288 ppc_fp128       { RET_TY(Type::PPC_FP128Ty, PPC_FP128);}
289 label           { RET_TY(Type::LabelTy, LABEL); }
290 type            { return TYPE;   }
291 opaque          { return OPAQUE; }
292 {IntegerType}   { uint64_t NumBits = atoull(yytext+1);
293                   if (NumBits < IntegerType::MIN_INT_BITS || 
294                       NumBits > IntegerType::MAX_INT_BITS)
295                     GenerateError("Bitwidth for integer type out of range!");
296                   const Type* Ty = IntegerType::get(NumBits);
297                   RET_TY(Ty, INTTYPE);
298                 }
299
300 add             { RET_TOK(BinaryOpVal, Add, ADD); }
301 sub             { RET_TOK(BinaryOpVal, Sub, SUB); }
302 mul             { RET_TOK(BinaryOpVal, Mul, MUL); }
303 udiv            { RET_TOK(BinaryOpVal, UDiv, UDIV); }
304 sdiv            { RET_TOK(BinaryOpVal, SDiv, SDIV); }
305 fdiv            { RET_TOK(BinaryOpVal, FDiv, FDIV); }
306 urem            { RET_TOK(BinaryOpVal, URem, UREM); }
307 srem            { RET_TOK(BinaryOpVal, SRem, SREM); }
308 frem            { RET_TOK(BinaryOpVal, FRem, FREM); }
309 shl             { RET_TOK(BinaryOpVal, Shl, SHL); }
310 lshr            { RET_TOK(BinaryOpVal, LShr, LSHR); }
311 ashr            { RET_TOK(BinaryOpVal, AShr, ASHR); }
312 and             { RET_TOK(BinaryOpVal, And, AND); }
313 or              { RET_TOK(BinaryOpVal, Or , OR ); }
314 xor             { RET_TOK(BinaryOpVal, Xor, XOR); }
315 icmp            { RET_TOK(OtherOpVal,  ICmp,  ICMP); }
316 fcmp            { RET_TOK(OtherOpVal,  FCmp,  FCMP); }
317
318 eq              { return EQ;  }
319 ne              { return NE;  }
320 slt             { return SLT; }
321 sgt             { return SGT; }
322 sle             { return SLE; }
323 sge             { return SGE; }
324 ult             { return ULT; }
325 ugt             { return UGT; }
326 ule             { return ULE; }
327 uge             { return UGE; }
328 oeq             { return OEQ; }
329 one             { return ONE; }
330 olt             { return OLT; }
331 ogt             { return OGT; }
332 ole             { return OLE; }
333 oge             { return OGE; }
334 ord             { return ORD; }
335 uno             { return UNO; }
336 ueq             { return UEQ; }
337 une             { return UNE; }
338
339 phi             { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
340 call            { RET_TOK(OtherOpVal, Call, CALL); }
341 trunc           { RET_TOK(CastOpVal, Trunc, TRUNC); }
342 zext            { RET_TOK(CastOpVal, ZExt, ZEXT); }
343 sext            { RET_TOK(CastOpVal, SExt, SEXT); }
344 fptrunc         { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
345 fpext           { RET_TOK(CastOpVal, FPExt, FPEXT); }
346 uitofp          { RET_TOK(CastOpVal, UIToFP, UITOFP); }
347 sitofp          { RET_TOK(CastOpVal, SIToFP, SITOFP); }
348 fptoui          { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
349 fptosi          { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
350 inttoptr        { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
351 ptrtoint        { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
352 bitcast         { RET_TOK(CastOpVal, BitCast, BITCAST); }
353 select          { RET_TOK(OtherOpVal, Select, SELECT); }
354 va_arg          { RET_TOK(OtherOpVal, VAArg , VAARG); }
355 ret             { RET_TOK(TermOpVal, Ret, RET); }
356 br              { RET_TOK(TermOpVal, Br, BR); }
357 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
358 invoke          { RET_TOK(TermOpVal, Invoke, INVOKE); }
359 unwind          { RET_TOK(TermOpVal, Unwind, UNWIND); }
360 unreachable     { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
361
362 malloc          { RET_TOK(MemOpVal, Malloc, MALLOC); }
363 alloca          { RET_TOK(MemOpVal, Alloca, ALLOCA); }
364 free            { RET_TOK(MemOpVal, Free, FREE); }
365 load            { RET_TOK(MemOpVal, Load, LOAD); }
366 store           { RET_TOK(MemOpVal, Store, STORE); }
367 getelementptr   { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
368
369 extractelement  { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
370 insertelement   { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
371 shufflevector   { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
372
373
374 {LocalVarName}  {
375                   llvmAsmlval.StrVal = new std::string(yytext+1);   // Skip %
376                   return LOCALVAR;
377                 }
378 {GlobalVarName} {
379                   llvmAsmlval.StrVal = new std::string(yytext+1);   // Skip @
380                   return GLOBALVAR;
381                 }
382 {Label}         {
383                   yytext[yyleng-1] = 0;            // nuke colon
384                   llvmAsmlval.StrVal = new std::string(yytext);
385                   return LABELSTR;
386                 }
387 {QuoteLabel}    {
388                   yytext[yyleng-2] = 0;  // nuke colon, end quote
389                   const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
390                   llvmAsmlval.StrVal = 
391                     new std::string(yytext+1, EndChar - yytext - 1);
392                   return LABELSTR;
393                 }
394
395 {StringConstant} { yytext[yyleng-1] = 0;           // nuke end quote
396                    const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
397                    llvmAsmlval.StrVal = 
398                      new std::string(yytext+1, EndChar - yytext - 1);
399                    return STRINGCONSTANT;
400                  }
401 {AtStringConstant} {
402                      yytext[yyleng-1] = 0;         // nuke end quote
403                      const char* EndChar = 
404                        UnEscapeLexed(yytext+2, yytext+yyleng);
405                      llvmAsmlval.StrVal = 
406                        new std::string(yytext+2, EndChar - yytext - 2);
407                      return ATSTRINGCONSTANT;
408                    }
409 {PctStringConstant} {
410                      yytext[yyleng-1] = 0;           // nuke end quote
411                      const char* EndChar = 
412                        UnEscapeLexed(yytext+2, yytext+yyleng);
413                      llvmAsmlval.StrVal = 
414                        new std::string(yytext+2, EndChar - yytext - 2);
415                      return PCTSTRINGCONSTANT;
416                    }
417 {PInteger}      { 
418                   uint32_t numBits = ((yyleng * 64) / 19) + 1;
419                   APInt Tmp(numBits, yytext, yyleng, 10);
420                   uint32_t activeBits = Tmp.getActiveBits();
421                   if (activeBits > 0 && activeBits < numBits)
422                     Tmp.trunc(activeBits);
423                   if (Tmp.getBitWidth() > 64) {
424                     llvmAsmlval.APIntVal = new APInt(Tmp);
425                     return EUAPINTVAL; 
426                   } else {
427                     llvmAsmlval.UInt64Val = Tmp.getZExtValue();
428                     return EUINT64VAL;
429                   }
430                 }
431 {NInteger}      {
432                   uint32_t numBits = (((yyleng-1) * 64) / 19) + 2;
433                   APInt Tmp(numBits, yytext, yyleng, 10);
434                   uint32_t minBits = Tmp.getMinSignedBits();
435                   if (minBits > 0 && minBits < numBits)
436                     Tmp.trunc(minBits);
437                   if (Tmp.getBitWidth() > 64) {
438                     llvmAsmlval.APIntVal = new APInt(Tmp);
439                     return ESAPINTVAL;
440                   } else {
441                     llvmAsmlval.SInt64Val = Tmp.getSExtValue();
442                     return ESINT64VAL;
443                   }
444                 }
445
446 {HexIntConstant} { int len = yyleng - 3;
447                    uint32_t bits = len * 4;
448                    APInt Tmp(bits, yytext+3, len, 16);
449                    uint32_t activeBits = Tmp.getActiveBits();
450                    if (activeBits > 0 && activeBits < bits)
451                      Tmp.trunc(activeBits);
452                    if (Tmp.getBitWidth() > 64) {
453                      llvmAsmlval.APIntVal = new APInt(Tmp);
454                      return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
455                    } else if (yytext[0] == 's') {
456                      llvmAsmlval.SInt64Val = Tmp.getSExtValue();
457                      return ESINT64VAL;
458                    } else {
459                      llvmAsmlval.UInt64Val = Tmp.getZExtValue();
460                      return EUINT64VAL;
461                    }
462                  }
463
464 {LocalVarID}     {
465                   uint64_t Val = atoull(yytext+1);
466                   if ((unsigned)Val != Val)
467                     GenerateError("Invalid value number (too large)!");
468                   llvmAsmlval.UIntVal = unsigned(Val);
469                   return LOCALVAL_ID;
470                 }
471 {GlobalVarID}   {
472                   uint64_t Val = atoull(yytext+1);
473                   if ((unsigned)Val != Val)
474                     GenerateError("Invalid value number (too large)!");
475                   llvmAsmlval.UIntVal = unsigned(Val);
476                   return GLOBALVAL_ID;
477                 }
478
479 {FPConstant}    { llvmAsmlval.FPVal = new APFloat(atof(yytext)); return FPVAL; }
480 {HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext+2)); 
481                   return FPVAL; 
482                 }
483 {HexFP80Constant} { uint64_t Pair[2];
484                     HexToIntPair(yytext+3, Pair);
485                     llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
486                     return FPVAL;
487                 }
488 {HexFP128Constant} { uint64_t Pair[2];
489                     HexToIntPair(yytext+3, Pair);
490                     llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
491                     return FPVAL;
492                 }
493 {HexPPC128Constant} { uint64_t Pair[2];
494                     HexToIntPair(yytext+3, Pair);
495                     llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
496                     return FPVAL;
497                 }
498
499 <<EOF>>         {
500                   /* Make sure to free the internal buffers for flex when we are
501                    * done reading our input!
502                    */
503                   yy_delete_buffer(YY_CURRENT_BUFFER);
504                   return EOF;
505                 }
506
507 [ \r\t\n]       { /* Ignore whitespace */ }
508 .               { return yytext[0]; }
509
510 %%