24467cf606d96bde9dddc4da811bbf37d68ae3a6
[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 "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
95 // HexToFP - Convert the ascii string in hexidecimal format to the floating
96 // point representation of it.
97 //
98 static double HexToFP(const char *Buffer) {
99   return BitsToDouble(HexIntToVal(Buffer));   // Cast Hex constant to double
100 }
101
102
103 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
104 // appropriate character.
105 char *UnEscapeLexed(char *Buffer, char* EndBuffer) {
106   char *BOut = Buffer;
107   for (char *BIn = Buffer; *BIn; ) {
108     if (BIn[0] == '\\') {
109       if (BIn < EndBuffer-1 && BIn[1] == '\\') {
110         *BOut++ = '\\'; // Two \ becomes one
111         BIn += 2;
112       } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
113         char Tmp = BIn[3]; BIn[3] = 0;      // Terminate string
114         *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
115         BIn[3] = Tmp;                       // Restore character
116         BIn += 3;                           // Skip over handled chars
117         ++BOut;
118       } else {
119         *BOut++ = *BIn++;
120       }
121     } else {
122       *BOut++ = *BIn++;
123     }
124   }
125   return BOut;
126 }
127
128 } // End llvm namespace
129
130 using namespace llvm;
131
132 #define YY_NEVER_INTERACTIVE 1
133 %}
134
135
136
137 /* Comments start with a ; and go till end of line */
138 Comment    ;.*
139
140 /* Local Values and Type identifiers start with a % sign */
141 LocalVarName       %[-a-zA-Z$._][-a-zA-Z$._0-9]*
142
143 /* Global Value identifiers start with an @ sign */
144 GlobalVarName       @[-a-zA-Z$._][-a-zA-Z$._0-9]*
145
146 /* Label identifiers end with a colon */
147 Label       [-a-zA-Z$._0-9]+:
148 QuoteLabel \"[^\"]+\":
149
150 /* Quoted names can contain any character except " and \ */
151 StringConstant \"[^\"]*\"
152 AtStringConstant @\"[^\"]*\"
153 PctStringConstant %\"[^\"]*\"
154   
155 /* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
156 LocalVarID     %[0-9]+
157 GlobalVarID    @[0-9]+
158
159 /* Integer types are specified with i and a bitwidth */
160 IntegerType i[0-9]+
161
162 /* E[PN]Integer: match positive and negative literal integer values. */
163 PInteger   [0-9]+
164 NInteger  -[0-9]+
165
166 /* FPConstant - A Floating point constant.
167  */
168 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
169
170 /* HexFPConstant - Floating point constant represented in IEEE format as a
171  *  hexadecimal number for when exponential notation is not precise enough.
172  */
173 HexFPConstant 0x[0-9A-Fa-f]+
174
175 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
176  * it to deal with 64 bit numbers.
177  */
178 HexIntConstant [us]0x[0-9A-Fa-f]+
179
180 %%
181
182 {Comment}       { /* Ignore comments for now */ }
183
184 begin           { return BEGINTOK; }
185 end             { return ENDTOK; }
186 true            { return TRUETOK;  }
187 false           { return FALSETOK; }
188 declare         { return DECLARE; }
189 define          { return DEFINE; }
190 global          { return GLOBAL; }
191 constant        { return CONSTANT; }
192 internal        { return INTERNAL; }
193 linkonce        { return LINKONCE; }
194 weak            { return WEAK; }
195 appending       { return APPENDING; }
196 dllimport       { return DLLIMPORT; }
197 dllexport       { return DLLEXPORT; }
198 hidden          { return HIDDEN; }
199 protected       { return PROTECTED; }
200 extern_weak     { return EXTERN_WEAK; }
201 external        { return EXTERNAL; }
202 thread_local    { return THREAD_LOCAL; }
203 zeroinitializer { return ZEROINITIALIZER; }
204 \.\.\.          { return DOTDOTDOT; }
205 undef           { return UNDEF; }
206 null            { return NULL_TOK; }
207 to              { return TO; }
208 tail            { return TAIL; }
209 target          { return TARGET; }
210 triple          { return TRIPLE; }
211 deplibs         { return DEPLIBS; }
212 datalayout      { return DATALAYOUT; }
213 volatile        { return VOLATILE; }
214 align           { return ALIGN;  }
215 section         { return SECTION; }
216 alias           { return ALIAS; }
217 module          { return MODULE; }
218 asm             { return ASM_TOK; }
219 sideeffect      { return SIDEEFFECT; }
220
221 cc              { return CC_TOK; }
222 ccc             { return CCC_TOK; }
223 fastcc          { return FASTCC_TOK; }
224 coldcc          { return COLDCC_TOK; }
225 x86_stdcallcc   { return X86_STDCALLCC_TOK; }
226 x86_fastcallcc  { return X86_FASTCALLCC_TOK; }
227
228 signext         { return SIGNEXT; }
229 zeroext         { return ZEROEXT; }
230 inreg           { return INREG; }
231 sret            { return SRET;  }
232 nounwind        { return NOUNWIND; }
233 noreturn        { return NORETURN; }
234 noalias         { return NOALIAS; }
235 byval           { return BYVAL; }
236
237 void            { RET_TY(Type::VoidTy,  VOID);  }
238 float           { RET_TY(Type::FloatTy, FLOAT); }
239 double          { RET_TY(Type::DoubleTy,DOUBLE);}
240 label           { RET_TY(Type::LabelTy, LABEL); }
241 type            { return TYPE;   }
242 opaque          { return OPAQUE; }
243 {IntegerType}   { uint64_t NumBits = atoull(yytext+1);
244                   if (NumBits < IntegerType::MIN_INT_BITS || 
245                       NumBits > IntegerType::MAX_INT_BITS)
246                     GenerateError("Bitwidth for integer type out of range!");
247                   const Type* Ty = IntegerType::get(NumBits);
248                   RET_TY(Ty, INTTYPE);
249                 }
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 shl             { RET_TOK(BinaryOpVal, Shl, SHL); }
261 lshr            { RET_TOK(BinaryOpVal, LShr, LSHR); }
262 ashr            { RET_TOK(BinaryOpVal, AShr, ASHR); }
263 and             { RET_TOK(BinaryOpVal, And, AND); }
264 or              { RET_TOK(BinaryOpVal, Or , OR ); }
265 xor             { RET_TOK(BinaryOpVal, Xor, XOR); }
266 icmp            { RET_TOK(OtherOpVal,  ICmp,  ICMP); }
267 fcmp            { RET_TOK(OtherOpVal,  FCmp,  FCMP); }
268
269 eq              { return EQ;  }
270 ne              { return NE;  }
271 slt             { return SLT; }
272 sgt             { return SGT; }
273 sle             { return SLE; }
274 sge             { return SGE; }
275 ult             { return ULT; }
276 ugt             { return UGT; }
277 ule             { return ULE; }
278 uge             { return UGE; }
279 oeq             { return OEQ; }
280 one             { return ONE; }
281 olt             { return OLT; }
282 ogt             { return OGT; }
283 ole             { return OLE; }
284 oge             { return OGE; }
285 ord             { return ORD; }
286 uno             { return UNO; }
287 ueq             { return UEQ; }
288 une             { return UNE; }
289
290 phi             { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
291 call            { RET_TOK(OtherOpVal, Call, CALL); }
292 trunc           { RET_TOK(CastOpVal, Trunc, TRUNC); }
293 zext            { RET_TOK(CastOpVal, ZExt, ZEXT); }
294 sext            { RET_TOK(CastOpVal, SExt, SEXT); }
295 fptrunc         { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
296 fpext           { RET_TOK(CastOpVal, FPExt, FPEXT); }
297 uitofp          { RET_TOK(CastOpVal, UIToFP, UITOFP); }
298 sitofp          { RET_TOK(CastOpVal, SIToFP, SITOFP); }
299 fptoui          { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
300 fptosi          { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
301 inttoptr        { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
302 ptrtoint        { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
303 bitcast         { RET_TOK(CastOpVal, BitCast, BITCAST); }
304 select          { RET_TOK(OtherOpVal, Select, SELECT); }
305 va_arg          { RET_TOK(OtherOpVal, VAArg , VAARG); }
306 ret             { RET_TOK(TermOpVal, Ret, RET); }
307 br              { RET_TOK(TermOpVal, Br, BR); }
308 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
309 invoke          { RET_TOK(TermOpVal, Invoke, INVOKE); }
310 unwind          { RET_TOK(TermOpVal, Unwind, UNWIND); }
311 unreachable     { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
312
313 malloc          { RET_TOK(MemOpVal, Malloc, MALLOC); }
314 alloca          { RET_TOK(MemOpVal, Alloca, ALLOCA); }
315 free            { RET_TOK(MemOpVal, Free, FREE); }
316 load            { RET_TOK(MemOpVal, Load, LOAD); }
317 store           { RET_TOK(MemOpVal, Store, STORE); }
318 getelementptr   { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
319
320 extractelement  { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
321 insertelement   { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
322 shufflevector   { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
323
324
325 {LocalVarName}  {
326                   llvmAsmlval.StrVal = new std::string(yytext+1);   // Skip %
327                   return LOCALVAR;
328                 }
329 {GlobalVarName} {
330                   llvmAsmlval.StrVal = new std::string(yytext+1);   // Skip @
331                   return GLOBALVAR;
332                 }
333 {Label}         {
334                   yytext[yyleng-1] = 0;            // nuke colon
335                   llvmAsmlval.StrVal = new std::string(yytext);
336                   return LABELSTR;
337                 }
338 {QuoteLabel}    {
339                   yytext[yyleng-2] = 0;  // nuke colon, end quote
340                   const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
341                   llvmAsmlval.StrVal = 
342                     new std::string(yytext+1, EndChar - yytext - 1);
343                   return LABELSTR;
344                 }
345
346 {StringConstant} { yytext[yyleng-1] = 0;           // nuke end quote
347                    const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
348                    llvmAsmlval.StrVal = 
349                      new std::string(yytext+1, EndChar - yytext - 1);
350                    return STRINGCONSTANT;
351                  }
352 {AtStringConstant} {
353                      yytext[yyleng-1] = 0;         // nuke end quote
354                      const char* EndChar = 
355                        UnEscapeLexed(yytext+2, yytext+yyleng);
356                      llvmAsmlval.StrVal = 
357                        new std::string(yytext+2, EndChar - yytext - 2);
358                      return ATSTRINGCONSTANT;
359                    }
360 {PctStringConstant} {
361                      yytext[yyleng-1] = 0;           // nuke end quote
362                      const char* EndChar = 
363                        UnEscapeLexed(yytext+2, yytext+yyleng);
364                      llvmAsmlval.StrVal = 
365                        new std::string(yytext+2, EndChar - yytext - 2);
366                      return PCTSTRINGCONSTANT;
367                    }
368 {PInteger}      { 
369                   uint32_t numBits = ((yyleng * 64) / 19) + 1;
370                   APInt Tmp(numBits, yytext, yyleng, 10);
371                   uint32_t activeBits = Tmp.getActiveBits();
372                   if (activeBits > 0 && activeBits < numBits)
373                     Tmp.trunc(activeBits);
374                   if (Tmp.getBitWidth() > 64) {
375                     llvmAsmlval.APIntVal = new APInt(Tmp);
376                     return EUAPINTVAL; 
377                   } else {
378                     llvmAsmlval.UInt64Val = Tmp.getZExtValue();
379                     return EUINT64VAL;
380                   }
381                 }
382 {NInteger}      {
383                   uint32_t numBits = (((yyleng-1) * 64) / 19) + 2;
384                   APInt Tmp(numBits, yytext, yyleng, 10);
385                   uint32_t minBits = Tmp.getMinSignedBits();
386                   if (minBits > 0 && minBits < numBits)
387                     Tmp.trunc(minBits);
388                   if (Tmp.getBitWidth() > 64) {
389                     llvmAsmlval.APIntVal = new APInt(Tmp);
390                     return ESAPINTVAL;
391                   } else {
392                     llvmAsmlval.SInt64Val = Tmp.getSExtValue();
393                     return ESINT64VAL;
394                   }
395                 }
396
397 {HexIntConstant} { int len = yyleng - 3;
398                    uint32_t bits = len * 4;
399                    APInt Tmp(bits, yytext+3, len, 16);
400                    uint32_t activeBits = Tmp.getActiveBits();
401                    if (activeBits > 0 && activeBits < bits)
402                      Tmp.trunc(activeBits);
403                    if (Tmp.getBitWidth() > 64) {
404                      llvmAsmlval.APIntVal = new APInt(Tmp);
405                      return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
406                    } else if (yytext[0] == 's') {
407                      llvmAsmlval.SInt64Val = Tmp.getSExtValue();
408                      return ESINT64VAL;
409                    } else {
410                      llvmAsmlval.UInt64Val = Tmp.getZExtValue();
411                      return EUINT64VAL;
412                    }
413                  }
414
415 {LocalVarID}     {
416                   uint64_t Val = atoull(yytext+1);
417                   if ((unsigned)Val != Val)
418                     GenerateError("Invalid value number (too large)!");
419                   llvmAsmlval.UIntVal = unsigned(Val);
420                   return LOCALVAL_ID;
421                 }
422 {GlobalVarID}   {
423                   uint64_t Val = atoull(yytext+1);
424                   if ((unsigned)Val != Val)
425                     GenerateError("Invalid value number (too large)!");
426                   llvmAsmlval.UIntVal = unsigned(Val);
427                   return GLOBALVAL_ID;
428                 }
429
430 {FPConstant}    { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
431 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
432
433 <<EOF>>         {
434                   /* Make sure to free the internal buffers for flex when we are
435                    * done reading our input!
436                    */
437                   yy_delete_buffer(YY_CURRENT_BUFFER);
438                   return EOF;
439                 }
440
441 [ \r\t\n]       { /* Ignore whitespace */ }
442 .               { return yytext[0]; }
443
444 %%