Implement protected visibility. This partly implements PR1363. Linker
[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 = Instruction::Enum; \
45   return sym
46
47 // Construct a token value for an obsolete token
48 #define RET_TY(CTYPE, SYM) \
49   llvmAsmlval.PrimType = CTYPE;\
50   return SYM
51
52 namespace llvm {
53
54 // TODO: All of the static identifiers are figured out by the lexer,
55 // these should be hashed to reduce the lexer size
56
57
58 // atoull - Convert an ascii string of decimal digits into the unsigned long
59 // long representation... this does not have to do input error checking,
60 // because we know that the input will be matched by a suitable regex...
61 //
62 static uint64_t atoull(const char *Buffer) {
63   uint64_t Result = 0;
64   for (; *Buffer; Buffer++) {
65     uint64_t OldRes = Result;
66     Result *= 10;
67     Result += *Buffer-'0';
68     if (Result < OldRes)   // Uh, oh, overflow detected!!!
69       GenerateError("constant bigger than 64 bits detected!");
70   }
71   return Result;
72 }
73
74 static uint64_t HexIntToVal(const char *Buffer) {
75   uint64_t Result = 0;
76   for (; *Buffer; ++Buffer) {
77     uint64_t OldRes = Result;
78     Result *= 16;
79     char C = *Buffer;
80     if (C >= '0' && C <= '9')
81       Result += C-'0';
82     else if (C >= 'A' && C <= 'F')
83       Result += C-'A'+10;
84     else if (C >= 'a' && C <= 'f')
85       Result += C-'a'+10;
86
87     if (Result < OldRes)   // Uh, oh, overflow detected!!!
88       GenerateError("constant bigger than 64 bits detected!");
89   }
90   return Result;
91 }
92
93
94 // HexToFP - Convert the ascii string in hexidecimal format to the floating
95 // point representation of it.
96 //
97 static double HexToFP(const char *Buffer) {
98   // Behave nicely in the face of C TBAA rules... see:
99   // http://www.nullstone.com/htmls/category/aliastyp.htm
100   union {
101     uint64_t UI;
102     double FP;
103   } UIntToFP;
104   UIntToFP.UI = HexIntToVal(Buffer);
105
106   assert(sizeof(double) == sizeof(uint64_t) &&
107          "Data sizes incompatible on this target!");
108   return UIntToFP.FP;   // Cast Hex constant to double
109 }
110
111
112 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
113 // appropriate character.  If AllowNull is set to false, a \00 value will cause
114 // an exception to be thrown.
115 //
116 // If AllowNull is set to true, the return value of the function points to the
117 // last character of the string in memory.
118 //
119 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
120   char *BOut = Buffer;
121   for (char *BIn = Buffer; *BIn; ) {
122     if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
123       char Tmp = BIn[3]; BIn[3] = 0;     // Terminate string
124       *BOut = (char)strtol(BIn+1, 0, 16);  // Convert to number
125       if (!AllowNull && !*BOut)
126         GenerateError("String literal cannot accept \\00 escape!");
127
128       BIn[3] = Tmp;                  // Restore character
129       BIn += 3;                      // Skip over handled chars
130       ++BOut;
131     } else {
132       *BOut++ = *BIn++;
133     }
134   }
135
136   return BOut;
137 }
138
139 } // End llvm namespace
140
141 using namespace llvm;
142
143 #define YY_NEVER_INTERACTIVE 1
144 %}
145
146
147
148 /* Comments start with a ; and go till end of line */
149 Comment    ;.*
150
151 /* Local Values and Type identifiers start with a % sign */
152 LocalVarName       %[-a-zA-Z$._][-a-zA-Z$._0-9]*
153
154 /* Global Value identifiers start with an @ sign */
155 GlobalVarName       @[-a-zA-Z$._][-a-zA-Z$._0-9]*
156
157 /* Label identifiers end with a colon */
158 Label       [-a-zA-Z$._0-9]+:
159 QuoteLabel \"[^\"]+\":
160
161 /* Quoted names can contain any character except " and \ */
162 StringConstant \"[^\"]*\"
163 AtStringConstant @\"[^\"]*\"
164   
165 /* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
166 LocalVarID     %[0-9]+
167 GlobalVarID    @[0-9]+
168
169 /* Integer types are specified with i and a bitwidth */
170 IntegerType i[0-9]+
171
172 /* E[PN]Integer: match positive and negative literal integer values. */
173 PInteger   [0-9]+
174 NInteger  -[0-9]+
175
176 /* FPConstant - A Floating point constant.
177  */
178 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
179
180 /* HexFPConstant - Floating point constant represented in IEEE format as a
181  *  hexadecimal number for when exponential notation is not precise enough.
182  */
183 HexFPConstant 0x[0-9A-Fa-f]+
184
185 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
186  * it to deal with 64 bit numbers.
187  */
188 HexIntConstant [us]0x[0-9A-Fa-f]+
189
190 %%
191
192 {Comment}       { /* Ignore comments for now */ }
193
194 begin           { return BEGINTOK; }
195 end             { return ENDTOK; }
196 true            { return TRUETOK;  }
197 false           { return FALSETOK; }
198 declare         { return DECLARE; }
199 define          { return DEFINE; }
200 global          { return GLOBAL; }
201 constant        { return CONSTANT; }
202 internal        { return INTERNAL; }
203 linkonce        { return LINKONCE; }
204 weak            { return WEAK; }
205 appending       { return APPENDING; }
206 dllimport       { return DLLIMPORT; }
207 dllexport       { return DLLEXPORT; }
208 hidden          { return HIDDEN; }
209 protected       { return PROTECTED; }
210 extern_weak     { return EXTERN_WEAK; }
211 external        { return EXTERNAL; }
212 thread_local    { return THREAD_LOCAL; }
213 zeroinitializer { return ZEROINITIALIZER; }
214 \.\.\.          { return DOTDOTDOT; }
215 undef           { return UNDEF; }
216 null            { return NULL_TOK; }
217 to              { return TO; }
218 tail            { return TAIL; }
219 target          { return TARGET; }
220 triple          { return TRIPLE; }
221 deplibs         { return DEPLIBS; }
222 datalayout      { return DATALAYOUT; }
223 volatile        { return VOLATILE; }
224 align           { return ALIGN;  }
225 section         { return SECTION; }
226 alias           { return ALIAS; }
227 module          { return MODULE; }
228 asm             { return ASM_TOK; }
229 sideeffect      { return SIDEEFFECT; }
230
231 cc              { return CC_TOK; }
232 ccc             { return CCC_TOK; }
233 fastcc          { return FASTCC_TOK; }
234 coldcc          { return COLDCC_TOK; }
235 x86_stdcallcc   { return X86_STDCALLCC_TOK; }
236 x86_fastcallcc  { return X86_FASTCALLCC_TOK; }
237
238 inreg           { return INREG; }
239 sret            { return SRET;  }
240 nounwind        { return NOUNWIND; }
241 noreturn        { return NORETURN; }
242
243 void            { RET_TY(Type::VoidTy,  VOID);  }
244 float           { RET_TY(Type::FloatTy, FLOAT); }
245 double          { RET_TY(Type::DoubleTy,DOUBLE);}
246 label           { RET_TY(Type::LabelTy, LABEL); }
247 type            { return TYPE;   }
248 opaque          { return OPAQUE; }
249 {IntegerType}   { uint64_t NumBits = atoull(yytext+1);
250                   if (NumBits < IntegerType::MIN_INT_BITS || 
251                       NumBits > IntegerType::MAX_INT_BITS)
252                     GenerateError("Bitwidth for integer type out of range!");
253                   const Type* Ty = IntegerType::get(NumBits);
254                   RET_TY(Ty, INTTYPE);
255                 }
256
257 add             { RET_TOK(BinaryOpVal, Add, ADD); }
258 sub             { RET_TOK(BinaryOpVal, Sub, SUB); }
259 mul             { RET_TOK(BinaryOpVal, Mul, MUL); }
260 udiv            { RET_TOK(BinaryOpVal, UDiv, UDIV); }
261 sdiv            { RET_TOK(BinaryOpVal, SDiv, SDIV); }
262 fdiv            { RET_TOK(BinaryOpVal, FDiv, FDIV); }
263 urem            { RET_TOK(BinaryOpVal, URem, UREM); }
264 srem            { RET_TOK(BinaryOpVal, SRem, SREM); }
265 frem            { RET_TOK(BinaryOpVal, FRem, FREM); }
266 shl             { RET_TOK(BinaryOpVal, Shl, SHL); }
267 lshr            { RET_TOK(BinaryOpVal, LShr, LSHR); }
268 ashr            { RET_TOK(BinaryOpVal, AShr, ASHR); }
269 and             { RET_TOK(BinaryOpVal, And, AND); }
270 or              { RET_TOK(BinaryOpVal, Or , OR ); }
271 xor             { RET_TOK(BinaryOpVal, Xor, XOR); }
272 icmp            { RET_TOK(OtherOpVal,  ICmp,  ICMP); }
273 fcmp            { RET_TOK(OtherOpVal,  FCmp,  FCMP); }
274
275 eq              { return EQ;  }
276 ne              { return NE;  }
277 slt             { return SLT; }
278 sgt             { return SGT; }
279 sle             { return SLE; }
280 sge             { return SGE; }
281 ult             { return ULT; }
282 ugt             { return UGT; }
283 ule             { return ULE; }
284 uge             { return UGE; }
285 oeq             { return OEQ; }
286 one             { return ONE; }
287 olt             { return OLT; }
288 ogt             { return OGT; }
289 ole             { return OLE; }
290 oge             { return OGE; }
291 ord             { return ORD; }
292 uno             { return UNO; }
293 ueq             { return UEQ; }
294 une             { return UNE; }
295
296 phi             { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
297 call            { RET_TOK(OtherOpVal, Call, CALL); }
298 trunc           { RET_TOK(CastOpVal, Trunc, TRUNC); }
299 zext            { RET_TOK(CastOpVal, ZExt, ZEXT); }
300 sext            { RET_TOK(CastOpVal, SExt, SEXT); }
301 fptrunc         { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
302 fpext           { RET_TOK(CastOpVal, FPExt, FPEXT); }
303 uitofp          { RET_TOK(CastOpVal, UIToFP, UITOFP); }
304 sitofp          { RET_TOK(CastOpVal, SIToFP, SITOFP); }
305 fptoui          { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
306 fptosi          { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
307 inttoptr        { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
308 ptrtoint        { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
309 bitcast         { RET_TOK(CastOpVal, BitCast, BITCAST); }
310 select          { RET_TOK(OtherOpVal, Select, SELECT); }
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 {LocalVarName}  {
332                   UnEscapeLexed(yytext+1);
333                   llvmAsmlval.StrVal = strdup(yytext+1);             // Skip %
334                   return LOCALVAR;
335                 }
336 {GlobalVarName} {
337                   UnEscapeLexed(yytext+1);
338                   llvmAsmlval.StrVal = strdup(yytext+1);             // Skip @
339                   return GLOBALVAR;
340                 }
341 {Label}         {
342                   yytext[strlen(yytext)-1] = 0;  // nuke colon
343                   UnEscapeLexed(yytext);
344                   llvmAsmlval.StrVal = strdup(yytext);
345                   return LABELSTR;
346                 }
347 {QuoteLabel}    {
348                   yytext[strlen(yytext)-2] = 0;  // nuke colon, end quote
349                   UnEscapeLexed(yytext+1);
350                   llvmAsmlval.StrVal = strdup(yytext+1);
351                   return LABELSTR;
352                 }
353
354 {StringConstant} { // Note that we cannot unescape a string constant here!  The
355                    // string constant might contain a \00 which would not be
356                    // understood by the string stuff.  It is valid to make a
357                    // [sbyte] c"Hello World\00" constant, for example.
358                    //
359                    yytext[strlen(yytext)-1] = 0;           // nuke end quote
360                    llvmAsmlval.StrVal = strdup(yytext+1);  // Nuke start quote
361                    return STRINGCONSTANT;
362                  }
363 {AtStringConstant} {
364                      yytext[strlen(yytext)-1] = 0;           // nuke end quote
365                      llvmAsmlval.StrVal = strdup(yytext+2);  // Nuke @, quote
366                      return ATSTRINGCONSTANT;
367                    }
368
369 {PInteger}      { int len = strlen(yytext); 
370                   uint32_t numBits = ((len * 64) / 19) + 1;
371                   APInt Tmp(numBits, yytext, len, 10);
372                   uint32_t activeBits = Tmp.getActiveBits();
373                   if (activeBits > 0 && activeBits < numBits)
374                     Tmp.trunc(activeBits);
375                   if (Tmp.getBitWidth() > 64) {
376                     llvmAsmlval.APIntVal = new APInt(Tmp);
377                     return EUAPINTVAL; 
378                   } else {
379                     llvmAsmlval.UInt64Val = Tmp.getZExtValue();
380                     return EUINT64VAL;
381                   }
382                 }
383 {NInteger}      { int len = strlen(yytext); 
384                   uint32_t numBits = (((len-1) * 64) / 19) + 2;
385                   APInt Tmp(numBits, yytext, len, 10);
386                   uint32_t minBits = Tmp.getMinSignedBits();
387                   if (minBits > 0 && minBits < numBits)
388                     Tmp.trunc(minBits);
389                   if (Tmp.getBitWidth() > 64) {
390                     llvmAsmlval.APIntVal = new APInt(Tmp);
391                     return ESAPINTVAL;
392                   } else {
393                     llvmAsmlval.SInt64Val = Tmp.getSExtValue();
394                     return ESINT64VAL;
395                   }
396                 }
397
398 {HexIntConstant} { int len = strlen(yytext+3) - 3;
399                    uint32_t bits = len * 4;
400                    APInt Tmp(bits, yytext+3, len, 16);
401                    uint32_t activeBits = Tmp.getActiveBits();
402                    if (activeBits > 0 && activeBits < bits)
403                      Tmp.trunc(activeBits);
404                    if (Tmp.getBitWidth() > 64) {
405                      llvmAsmlval.APIntVal = new APInt(Tmp);
406                      return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
407                    } else if (yytext[0] == 's') {
408                      llvmAsmlval.SInt64Val = Tmp.getSExtValue();
409                      return ESINT64VAL;
410                    } else {
411                      llvmAsmlval.UInt64Val = Tmp.getZExtValue();
412                      return EUINT64VAL;
413                    }
414                  }
415
416 {LocalVarID}     {
417                   uint64_t Val = atoull(yytext+1);
418                   if ((unsigned)Val != Val)
419                     GenerateError("Invalid value number (too large)!");
420                   llvmAsmlval.UIntVal = unsigned(Val);
421                   return LOCALVAL_ID;
422                 }
423 {GlobalVarID}   {
424                   uint64_t Val = atoull(yytext+1);
425                   if ((unsigned)Val != Val)
426                     GenerateError("Invalid value number (too large)!");
427                   llvmAsmlval.UIntVal = unsigned(Val);
428                   return GLOBALVAL_ID;
429                 }
430
431 {FPConstant}    { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
432 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
433
434 <<EOF>>         {
435                   /* Make sure to free the internal buffers for flex when we are
436                    * done reading our input!
437                    */
438                   yy_delete_buffer(YY_CURRENT_BUFFER);
439                   return EOF;
440                 }
441
442 [ \r\t\n]       { /* Ignore whitespace */ }
443 .               { return yytext[0]; }
444
445 %%