98a21cabaaa581d0f45390e674aab30decd872a7
[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 pure            { return PURE; }
279 const           { return CONST; }
280 sext{WSNL}      { // For auto-upgrade only, drop in LLVM 3.0 
281                   return SIGNEXT; } 
282 zext{WSNL}      { // For auto-upgrade only, drop in LLVM 3.0
283                   return ZEROEXT; } 
284
285 void            { RET_TY(Type::VoidTy,  VOID);  }
286 float           { RET_TY(Type::FloatTy, FLOAT); }
287 double          { RET_TY(Type::DoubleTy,DOUBLE);}
288 x86_fp80        { RET_TY(Type::X86_FP80Ty, X86_FP80);}
289 fp128           { RET_TY(Type::FP128Ty, FP128);}
290 ppc_fp128       { RET_TY(Type::PPC_FP128Ty, PPC_FP128);}
291 label           { RET_TY(Type::LabelTy, LABEL); }
292 type            { return TYPE;   }
293 opaque          { return OPAQUE; }
294 {IntegerType}   { uint64_t NumBits = atoull(yytext+1);
295                   if (NumBits < IntegerType::MIN_INT_BITS || 
296                       NumBits > IntegerType::MAX_INT_BITS)
297                     GenerateError("Bitwidth for integer type out of range!");
298                   const Type* Ty = IntegerType::get(NumBits);
299                   RET_TY(Ty, INTTYPE);
300                 }
301
302 add             { RET_TOK(BinaryOpVal, Add, ADD); }
303 sub             { RET_TOK(BinaryOpVal, Sub, SUB); }
304 mul             { RET_TOK(BinaryOpVal, Mul, MUL); }
305 udiv            { RET_TOK(BinaryOpVal, UDiv, UDIV); }
306 sdiv            { RET_TOK(BinaryOpVal, SDiv, SDIV); }
307 fdiv            { RET_TOK(BinaryOpVal, FDiv, FDIV); }
308 urem            { RET_TOK(BinaryOpVal, URem, UREM); }
309 srem            { RET_TOK(BinaryOpVal, SRem, SREM); }
310 frem            { RET_TOK(BinaryOpVal, FRem, FREM); }
311 shl             { RET_TOK(BinaryOpVal, Shl, SHL); }
312 lshr            { RET_TOK(BinaryOpVal, LShr, LSHR); }
313 ashr            { RET_TOK(BinaryOpVal, AShr, ASHR); }
314 and             { RET_TOK(BinaryOpVal, And, AND); }
315 or              { RET_TOK(BinaryOpVal, Or , OR ); }
316 xor             { RET_TOK(BinaryOpVal, Xor, XOR); }
317 icmp            { RET_TOK(OtherOpVal,  ICmp,  ICMP); }
318 fcmp            { RET_TOK(OtherOpVal,  FCmp,  FCMP); }
319
320 eq              { return EQ;  }
321 ne              { return NE;  }
322 slt             { return SLT; }
323 sgt             { return SGT; }
324 sle             { return SLE; }
325 sge             { return SGE; }
326 ult             { return ULT; }
327 ugt             { return UGT; }
328 ule             { return ULE; }
329 uge             { return UGE; }
330 oeq             { return OEQ; }
331 one             { return ONE; }
332 olt             { return OLT; }
333 ogt             { return OGT; }
334 ole             { return OLE; }
335 oge             { return OGE; }
336 ord             { return ORD; }
337 uno             { return UNO; }
338 ueq             { return UEQ; }
339 une             { return UNE; }
340
341 phi             { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
342 call            { RET_TOK(OtherOpVal, Call, CALL); }
343 trunc           { RET_TOK(CastOpVal, Trunc, TRUNC); }
344 zext            { RET_TOK(CastOpVal, ZExt, ZEXT); }
345 sext            { RET_TOK(CastOpVal, SExt, SEXT); }
346 fptrunc         { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
347 fpext           { RET_TOK(CastOpVal, FPExt, FPEXT); }
348 uitofp          { RET_TOK(CastOpVal, UIToFP, UITOFP); }
349 sitofp          { RET_TOK(CastOpVal, SIToFP, SITOFP); }
350 fptoui          { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
351 fptosi          { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
352 inttoptr        { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
353 ptrtoint        { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
354 bitcast         { RET_TOK(CastOpVal, BitCast, BITCAST); }
355 select          { RET_TOK(OtherOpVal, Select, SELECT); }
356 va_arg          { RET_TOK(OtherOpVal, VAArg , VAARG); }
357 ret             { RET_TOK(TermOpVal, Ret, RET); }
358 br              { RET_TOK(TermOpVal, Br, BR); }
359 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
360 invoke          { RET_TOK(TermOpVal, Invoke, INVOKE); }
361 unwind          { RET_TOK(TermOpVal, Unwind, UNWIND); }
362 unreachable     { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
363
364 malloc          { RET_TOK(MemOpVal, Malloc, MALLOC); }
365 alloca          { RET_TOK(MemOpVal, Alloca, ALLOCA); }
366 free            { RET_TOK(MemOpVal, Free, FREE); }
367 load            { RET_TOK(MemOpVal, Load, LOAD); }
368 store           { RET_TOK(MemOpVal, Store, STORE); }
369 getelementptr   { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
370
371 extractelement  { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
372 insertelement   { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
373 shufflevector   { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
374
375
376 {LocalVarName}  {
377                   llvmAsmlval.StrVal = new std::string(yytext+1);   // Skip %
378                   return LOCALVAR;
379                 }
380 {GlobalVarName} {
381                   llvmAsmlval.StrVal = new std::string(yytext+1);   // Skip @
382                   return GLOBALVAR;
383                 }
384 {Label}         {
385                   yytext[yyleng-1] = 0;            // nuke colon
386                   llvmAsmlval.StrVal = new std::string(yytext);
387                   return LABELSTR;
388                 }
389 {QuoteLabel}    {
390                   yytext[yyleng-2] = 0;  // nuke colon, end quote
391                   const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
392                   llvmAsmlval.StrVal = 
393                     new std::string(yytext+1, EndChar - yytext - 1);
394                   return LABELSTR;
395                 }
396
397 {StringConstant} { yytext[yyleng-1] = 0;           // nuke end quote
398                    const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
399                    llvmAsmlval.StrVal = 
400                      new std::string(yytext+1, EndChar - yytext - 1);
401                    return STRINGCONSTANT;
402                  }
403 {AtStringConstant} {
404                      yytext[yyleng-1] = 0;         // nuke end quote
405                      const char* EndChar = 
406                        UnEscapeLexed(yytext+2, yytext+yyleng);
407                      llvmAsmlval.StrVal = 
408                        new std::string(yytext+2, EndChar - yytext - 2);
409                      return ATSTRINGCONSTANT;
410                    }
411 {PctStringConstant} {
412                      yytext[yyleng-1] = 0;           // nuke end quote
413                      const char* EndChar = 
414                        UnEscapeLexed(yytext+2, yytext+yyleng);
415                      llvmAsmlval.StrVal = 
416                        new std::string(yytext+2, EndChar - yytext - 2);
417                      return PCTSTRINGCONSTANT;
418                    }
419 {PInteger}      { 
420                   uint32_t numBits = ((yyleng * 64) / 19) + 1;
421                   APInt Tmp(numBits, yytext, yyleng, 10);
422                   uint32_t activeBits = Tmp.getActiveBits();
423                   if (activeBits > 0 && activeBits < numBits)
424                     Tmp.trunc(activeBits);
425                   if (Tmp.getBitWidth() > 64) {
426                     llvmAsmlval.APIntVal = new APInt(Tmp);
427                     return EUAPINTVAL; 
428                   } else {
429                     llvmAsmlval.UInt64Val = Tmp.getZExtValue();
430                     return EUINT64VAL;
431                   }
432                 }
433 {NInteger}      {
434                   uint32_t numBits = (((yyleng-1) * 64) / 19) + 2;
435                   APInt Tmp(numBits, yytext, yyleng, 10);
436                   uint32_t minBits = Tmp.getMinSignedBits();
437                   if (minBits > 0 && minBits < numBits)
438                     Tmp.trunc(minBits);
439                   if (Tmp.getBitWidth() > 64) {
440                     llvmAsmlval.APIntVal = new APInt(Tmp);
441                     return ESAPINTVAL;
442                   } else {
443                     llvmAsmlval.SInt64Val = Tmp.getSExtValue();
444                     return ESINT64VAL;
445                   }
446                 }
447
448 {HexIntConstant} { int len = yyleng - 3;
449                    uint32_t bits = len * 4;
450                    APInt Tmp(bits, yytext+3, len, 16);
451                    uint32_t activeBits = Tmp.getActiveBits();
452                    if (activeBits > 0 && activeBits < bits)
453                      Tmp.trunc(activeBits);
454                    if (Tmp.getBitWidth() > 64) {
455                      llvmAsmlval.APIntVal = new APInt(Tmp);
456                      return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
457                    } else if (yytext[0] == 's') {
458                      llvmAsmlval.SInt64Val = Tmp.getSExtValue();
459                      return ESINT64VAL;
460                    } else {
461                      llvmAsmlval.UInt64Val = Tmp.getZExtValue();
462                      return EUINT64VAL;
463                    }
464                  }
465
466 {LocalVarID}     {
467                   uint64_t Val = atoull(yytext+1);
468                   if ((unsigned)Val != Val)
469                     GenerateError("Invalid value number (too large)!");
470                   llvmAsmlval.UIntVal = unsigned(Val);
471                   return LOCALVAL_ID;
472                 }
473 {GlobalVarID}   {
474                   uint64_t Val = atoull(yytext+1);
475                   if ((unsigned)Val != Val)
476                     GenerateError("Invalid value number (too large)!");
477                   llvmAsmlval.UIntVal = unsigned(Val);
478                   return GLOBALVAL_ID;
479                 }
480
481 {FPConstant}    { llvmAsmlval.FPVal = new APFloat(atof(yytext)); return FPVAL; }
482 {HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext+2)); 
483                   return FPVAL; 
484                 }
485 {HexFP80Constant} { uint64_t Pair[2];
486                     HexToIntPair(yytext+3, Pair);
487                     llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
488                     return FPVAL;
489                 }
490 {HexFP128Constant} { uint64_t Pair[2];
491                     HexToIntPair(yytext+3, Pair);
492                     llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair), true);
493                     return FPVAL;
494                 }
495 {HexPPC128Constant} { uint64_t Pair[2];
496                     HexToIntPair(yytext+3, Pair);
497                     llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
498                     return FPVAL;
499                 }
500
501 <<EOF>>         {
502                   /* Make sure to free the internal buffers for flex when we are
503                    * done reading our input!
504                    */
505                   yy_delete_buffer(YY_CURRENT_BUFFER);
506                   return EOF;
507                 }
508
509 [ \r\t\n]       { /* Ignore whitespace */ }
510 .               { return yytext[0]; }
511
512 %%