* llvm.spec.in: update blurb
[oota-llvm.git] / tools / llvm-upgrade / UpgradeLexer.l
1 /*===-- UpgradeLexer.l - Scanner for 1.9 assembly files --------*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the flex scanner for LLVM 1.9 assembly languages files.
11 //
12 //===----------------------------------------------------------------------===*/
13
14 %option prefix="Upgrade"
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="UpgradeLexer.cpp"
23 %option ecs
24 %option noreject
25 %option noyymore
26
27 %{
28 #include "UpgradeInternals.h"
29 #include "llvm/Module.h"
30 #include <list>
31 #include "UpgradeParser.h"
32 #include <cctype>
33 #include <cstdlib>
34
35 #define YY_INPUT(buf,result,max_size) \
36 { \
37   if (LexInput->good() && !LexInput->eof()) { \
38     LexInput->read(buf,max_size); \
39     result = LexInput->gcount(); \
40   } else {\
41     result = YY_NULL; \
42   } \
43 }
44
45 #define YY_NEVER_INTERACTIVE 1
46
47 // Construct a token value for a non-obsolete token
48 #define RET_TOK(type, Enum, sym) \
49   Upgradelval.type = Enum; \
50   return sym
51
52 #define RET_TY(sym,NewTY,sign) \
53   Upgradelval.PrimType.T = NewTY; \
54   switch (sign) { \
55     case 0: Upgradelval.PrimType.S.makeSignless(); break; \
56     case 1: Upgradelval.PrimType.S.makeUnsigned(); break; \
57     case 2: Upgradelval.PrimType.S.makeSigned(); break; \
58     default: assert(0 && "Invalid sign kind"); break; \
59   }\
60   return sym
61
62 namespace llvm {
63
64 // TODO: All of the static identifiers are figured out by the lexer,
65 // these should be hashed to reduce the lexer size
66
67 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
68 // appropriate character.  If AllowNull is set to false, a \00 value will cause
69 // an exception to be thrown.
70 //
71 // If AllowNull is set to true, the return value of the function points to the
72 // last character of the string in memory.
73 //
74 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
75   char *BOut = Buffer;
76   for (char *BIn = Buffer; *BIn; ) {
77     if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
78       char Tmp = BIn[3]; BIn[3] = 0;     // Terminate string
79       *BOut = (char)strtol(BIn+1, 0, 16);  // Convert to number
80       if (!AllowNull && !*BOut)
81         error("String literal cannot accept \\00 escape!");
82
83       BIn[3] = Tmp;                  // Restore character
84       BIn += 3;                      // Skip over handled chars
85       ++BOut;
86     } else {
87       *BOut++ = *BIn++;
88     }
89   }
90
91   return BOut;
92 }
93
94 // atoull - Convert an ascii string of decimal digits into the unsigned long
95 // long representation... this does not have to do input error checking,
96 // because we know that the input will be matched by a suitable regex...
97 //
98 static uint64_t atoull(const char *Buffer) {
99   uint64_t Result = 0;
100   for (; *Buffer; Buffer++) {
101     uint64_t OldRes = Result;
102     Result *= 10;
103     Result += *Buffer-'0';
104     if (Result < OldRes)   // Uh, oh, overflow detected!!!
105       error("constant bigger than 64 bits detected!");
106   }
107   return Result;
108 }
109
110 static uint64_t HexIntToVal(const char *Buffer) {
111   uint64_t Result = 0;
112   for (; *Buffer; ++Buffer) {
113     uint64_t OldRes = Result;
114     Result *= 16;
115     char C = *Buffer;
116     if (C >= '0' && C <= '9')
117       Result += C-'0';
118     else if (C >= 'A' && C <= 'F')
119       Result += C-'A'+10;
120     else if (C >= 'a' && C <= 'f')
121       Result += C-'a'+10;
122
123     if (Result < OldRes)   // Uh, oh, overflow detected!!!
124       error("constant bigger than 64 bits detected!");
125   }
126   return Result;
127 }
128
129
130 // HexToFP - Convert the ascii string in hexidecimal format to the floating
131 // point representation of it.
132 //
133 static double HexToFP(const char *Buffer) {
134   // Behave nicely in the face of C TBAA rules... see:
135   // http://www.nullstone.com/htmls/category/aliastyp.htm
136   union {
137     uint64_t UI;
138     double FP;
139   } UIntToFP;
140   UIntToFP.UI = HexIntToVal(Buffer);
141
142   assert(sizeof(double) == sizeof(uint64_t) &&
143          "Data sizes incompatible on this target!");
144   return UIntToFP.FP;   // Cast Hex constant to double
145 }
146
147
148 } // End llvm namespace
149
150 using namespace llvm;
151
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          { return EXCEPT; }
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 sbyte           { RET_TY(SBYTE,  Type::Int8Ty,  2); }
247 ubyte           { RET_TY(UBYTE,  Type::Int8Ty,  1); }
248 i8              { RET_TY(UBYTE,  Type::Int8Ty,  1); }
249 short           { RET_TY(SHORT,  Type::Int16Ty, 2); }
250 ushort          { RET_TY(USHORT, Type::Int16Ty, 1); }
251 i16             { RET_TY(USHORT, Type::Int16Ty, 1); }
252 int             { RET_TY(INT,    Type::Int32Ty, 2); }
253 uint            { RET_TY(UINT,   Type::Int32Ty, 1); }
254 i32             { RET_TY(UINT,   Type::Int32Ty, 1); }
255 long            { RET_TY(LONG,   Type::Int64Ty, 2); }
256 ulong           { RET_TY(ULONG,  Type::Int64Ty, 1); }
257 i64             { RET_TY(ULONG,  Type::Int64Ty, 1); }
258 void            { RET_TY(VOID,   Type::VoidTy,  0); }
259 bool            { RET_TY(BOOL,   Type::Int1Ty,  1); }
260 i1              { RET_TY(BOOL,   Type::Int1Ty,  1); }
261 float           { RET_TY(FLOAT,  Type::FloatTy, 0); }
262 double          { RET_TY(DOUBLE, Type::DoubleTy,0); }
263 label           { RET_TY(LABEL,  Type::LabelTy, 0); }
264 type            { return TYPE;   }
265 opaque          { return OPAQUE; }
266
267 add             { RET_TOK(BinaryOpVal, AddOp, ADD); }
268 sub             { RET_TOK(BinaryOpVal, SubOp, SUB); }
269 mul             { RET_TOK(BinaryOpVal, MulOp, MUL); }
270 div             { RET_TOK(BinaryOpVal, DivOp,  DIV); }
271 udiv            { RET_TOK(BinaryOpVal, UDivOp, UDIV); }
272 sdiv            { RET_TOK(BinaryOpVal, SDivOp, SDIV); }
273 fdiv            { RET_TOK(BinaryOpVal, FDivOp, FDIV); }
274 rem             { RET_TOK(BinaryOpVal, RemOp,  REM); }
275 urem            { RET_TOK(BinaryOpVal, URemOp, UREM); }
276 srem            { RET_TOK(BinaryOpVal, SRemOp, SREM); }
277 frem            { RET_TOK(BinaryOpVal, FRemOp, FREM); }
278 and             { RET_TOK(BinaryOpVal, AndOp, AND); }
279 or              { RET_TOK(BinaryOpVal, OrOp , OR ); }
280 xor             { RET_TOK(BinaryOpVal, XorOp, XOR); }
281 setne           { RET_TOK(BinaryOpVal, SetNE, SETNE); }
282 seteq           { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
283 setlt           { RET_TOK(BinaryOpVal, SetLT, SETLT); }
284 setgt           { RET_TOK(BinaryOpVal, SetGT, SETGT); }
285 setle           { RET_TOK(BinaryOpVal, SetLE, SETLE); }
286 setge           { RET_TOK(BinaryOpVal, SetGE, SETGE); }
287 shl             { RET_TOK(BinaryOpVal, ShlOp, SHL); }
288 shr             { RET_TOK(BinaryOpVal, ShrOp, SHR); }
289 lshr            { RET_TOK(BinaryOpVal, LShrOp, LSHR); }
290 ashr            { RET_TOK(BinaryOpVal, AShrOp, ASHR); }
291
292 icmp            { RET_TOK(OtherOpVal, ICmpOp, ICMP); }
293 fcmp            { RET_TOK(OtherOpVal, FCmpOp, FCMP); }
294
295 eq              { return EQ; }
296 ne              { return NE; }
297 slt             { return SLT; }
298 sgt             { return SGT; }
299 sle             { return SLE; }
300 sge             { return SGE; }
301 ult             { return ULT; }
302 ugt             { return UGT; }
303 ule             { return ULE; }
304 uge             { return UGE; }
305 oeq             { return OEQ; }
306 one             { return ONE; }
307 olt             { return OLT; }
308 ogt             { return OGT; }
309 ole             { return OLE; }
310 oge             { return OGE; }
311 ord             { return ORD; }
312 uno             { return UNO; }
313 ueq             { return UEQ; }
314 une             { return UNE; }
315
316 phi             { RET_TOK(OtherOpVal, PHIOp, PHI_TOK); }
317 call            { RET_TOK(OtherOpVal, CallOp, CALL); }
318 cast            { RET_TOK(CastOpVal, CastOp, CAST);  }
319 trunc           { RET_TOK(CastOpVal, TruncOp, TRUNC); }
320 zext            { RET_TOK(CastOpVal, ZExtOp , ZEXT); }
321 sext            { RET_TOK(CastOpVal, SExtOp, SEXT); }
322 fptrunc         { RET_TOK(CastOpVal, FPTruncOp, FPTRUNC); }
323 fpext           { RET_TOK(CastOpVal, FPExtOp, FPEXT); }
324 fptoui          { RET_TOK(CastOpVal, FPToUIOp, FPTOUI); }
325 fptosi          { RET_TOK(CastOpVal, FPToSIOp, FPTOSI); }
326 uitofp          { RET_TOK(CastOpVal, UIToFPOp, UITOFP); }
327 sitofp          { RET_TOK(CastOpVal, SIToFPOp, SITOFP); }
328 ptrtoint        { RET_TOK(CastOpVal, PtrToIntOp, PTRTOINT); }
329 inttoptr        { RET_TOK(CastOpVal, IntToPtrOp, INTTOPTR); }
330 bitcast         { RET_TOK(CastOpVal, BitCastOp, BITCAST); }
331 select          { RET_TOK(OtherOpVal, SelectOp, SELECT); }
332 vanext          { return VANEXT_old; }
333 vaarg           { return VAARG_old; }
334 va_arg          { RET_TOK(OtherOpVal, VAArg , VAARG); }
335 ret             { RET_TOK(TermOpVal, RetOp, RET); }
336 br              { RET_TOK(TermOpVal, BrOp, BR); }
337 switch          { RET_TOK(TermOpVal, SwitchOp, SWITCH); }
338 invoke          { RET_TOK(TermOpVal, InvokeOp, INVOKE); }
339 unwind          { return UNWIND; }
340 unreachable     { RET_TOK(TermOpVal, UnreachableOp, UNREACHABLE); }
341
342 malloc          { RET_TOK(MemOpVal, MallocOp, MALLOC); }
343 alloca          { RET_TOK(MemOpVal, AllocaOp, ALLOCA); }
344 free            { RET_TOK(MemOpVal, FreeOp, FREE); }
345 load            { RET_TOK(MemOpVal, LoadOp, LOAD); }
346 store           { RET_TOK(MemOpVal, StoreOp, STORE); }
347 getelementptr   { RET_TOK(MemOpVal, GetElementPtrOp, GETELEMENTPTR); }
348
349 extractelement  { RET_TOK(OtherOpVal, ExtractElementOp, EXTRACTELEMENT); }
350 insertelement   { RET_TOK(OtherOpVal, InsertElementOp, INSERTELEMENT); }
351 shufflevector   { RET_TOK(OtherOpVal, ShuffleVectorOp, SHUFFLEVECTOR); }
352
353
354 {VarID}         {
355                   UnEscapeLexed(yytext+1);
356                   Upgradelval.StrVal = strdup(yytext+1);             // Skip %
357                   return VAR_ID;
358                 }
359 {Label}         {
360                   yytext[strlen(yytext)-1] = 0;  // nuke colon
361                   UnEscapeLexed(yytext);
362                   Upgradelval.StrVal = strdup(yytext);
363                   return LABELSTR;
364                 }
365 {QuoteLabel}    {
366                   yytext[strlen(yytext)-2] = 0;  // nuke colon, end quote
367                   UnEscapeLexed(yytext+1);
368                   Upgradelval.StrVal = strdup(yytext+1);
369                   return LABELSTR;
370                 }
371
372 {StringConstant} { // Note that we cannot unescape a string constant here!  The
373                    // string constant might contain a \00 which would not be
374                    // understood by the string stuff.  It is valid to make a
375                    // [sbyte] c"Hello World\00" constant, for example.
376                    //
377                    yytext[strlen(yytext)-1] = 0;           // nuke end quote
378                    Upgradelval.StrVal = strdup(yytext+1);  // Nuke start quote
379                    return STRINGCONSTANT;
380                  }
381
382
383 {PInteger}      { Upgradelval.UInt64Val = atoull(yytext); return EUINT64VAL; }
384 {NInteger}      {
385                   uint64_t Val = atoull(yytext+1);
386                   // +1:  we have bigger negative range
387                   if (Val > (uint64_t)INT64_MAX+1)
388                     error("Constant too large for signed 64 bits!");
389                   Upgradelval.SInt64Val = -Val;
390                   return ESINT64VAL;
391                 }
392 {HexIntConstant} {
393                    Upgradelval.UInt64Val = HexIntToVal(yytext+3);
394                    return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
395                  }
396
397 {EPInteger}     {
398                   uint64_t Val = atoull(yytext+1);
399                   if ((unsigned)Val != Val)
400                     error("Invalid value number (too large)!");
401                   Upgradelval.UIntVal = unsigned(Val);
402                   return UINTVAL;
403                 }
404 {ENInteger}     {
405                   uint64_t Val = atoull(yytext+2);
406                   // +1:  we have bigger negative range
407                   if (Val > (uint64_t)INT32_MAX+1)
408                     error("Constant too large for signed 32 bits!");
409                   Upgradelval.SIntVal = (int)-Val;
410                   return SINTVAL;
411                 }
412
413 {FPConstant}    { Upgradelval.FPVal = atof(yytext); return FPVAL; }
414 {HexFPConstant} { Upgradelval.FPVal = HexToFP(yytext); return FPVAL; }
415
416 <<EOF>>         {
417                   /* Make sure to free the internal buffers for flex when we are
418                    * done reading our input!
419                    */
420                   yy_delete_buffer(YY_CURRENT_BUFFER);
421                   return EOF;
422                 }
423
424 [ \r\t\n]       { /* Ignore whitespace */ }
425 .               { return yytext[0]; }
426
427 %%