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