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