b8a795567d2786ce6d94e7992cc226fbdce888a6
[oota-llvm.git] / lib / AsmParser / Lexer.l
1 /*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
2 //
3 //  This file implements the flex scanner for LLVM assembly languages files.
4 //
5 //===----------------------------------------------------------------------===*/
6
7 %option prefix="llvmAsm"
8 %option yylineno
9 %option nostdinit
10 %option never-interactive
11 %option batch
12 %option noyywrap
13 %option nodefault
14 %option 8bit
15 %option outfile="Lexer.cpp"
16 %option ecs
17 %option noreject
18 %option noyymore
19
20 %{
21 #include "ParserInternals.h"
22 #include <list>
23 #include "llvmAsmParser.h"
24 #include <ctype.h>
25 #include <stdlib.h>
26
27 #define RET_TOK(type, Enum, sym) \
28   llvmAsmlval.type = Instruction::Enum; return sym
29
30
31 // TODO: All of the static identifiers are figured out by the lexer, 
32 // these should be hashed to reduce the lexer size
33
34
35 // atoull - Convert an ascii string of decimal digits into the unsigned long
36 // long representation... this does not have to do input error checking, 
37 // because we know that the input will be matched by a suitable regex...
38 //
39 static uint64_t atoull(const char *Buffer) {
40   uint64_t Result = 0;
41   for (; *Buffer; Buffer++) {
42     uint64_t OldRes = Result;
43     Result *= 10;
44     Result += *Buffer-'0';
45     if (Result < OldRes)   // Uh, oh, overflow detected!!!
46       ThrowException("constant bigger than 64 bits detected!");
47   }
48   return Result;
49 }
50
51 // HexToFP - Convert the ascii string in hexidecimal format to the floating
52 // point representation of it.
53 //
54 static double HexToFP(const char *Buffer) {
55   uint64_t Result = 0;
56   for (; *Buffer; ++Buffer) {
57     uint64_t OldRes = Result;
58     Result *= 16;
59     char C = *Buffer;
60     if (C >= '0' && C <= '9')
61       Result += C-'0';
62     else if (C >= 'A' && C <= 'F')
63       Result += C-'A'+10;
64     else if (C >= 'a' && C <= 'f')
65       Result += C-'a'+10;
66
67     if (Result < OldRes)   // Uh, oh, overflow detected!!!
68       ThrowException("constant bigger than 64 bits detected!");
69   }
70
71   assert(sizeof(double) == sizeof(Result) &&
72          "Data sizes incompatible on this target!");
73   // Behave nicely in the face of C TBAA rules... see:
74   // http://www.nullstone.com/htmls/category/aliastyp.htm
75   //
76   char *ProxyPointer = (char*)&Result;
77   return *(double*)ProxyPointer;   // Cast Hex constant to double
78 }
79
80
81 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
82 // appropriate character.  If AllowNull is set to false, a \00 value will cause
83 // an exception to be thrown.
84 //
85 // If AllowNull is set to true, the return value of the function points to the
86 // last character of the string in memory.
87 //
88 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
89   char *BOut = Buffer;
90   for (char *BIn = Buffer; *BIn; ) {
91     if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
92       char Tmp = BIn[3]; BIn[3] = 0;     // Terminate string
93       *BOut = strtol(BIn+1, 0, 16);  // Convert to number
94       if (!AllowNull && !*BOut)
95         ThrowException("String literal cannot accept \\00 escape!");
96       
97       BIn[3] = Tmp;                  // Restore character
98       BIn += 3;                      // Skip over handled chars
99       ++BOut;
100     } else {
101       *BOut++ = *BIn++;
102     }
103   }
104
105   return BOut;
106 }
107
108 #define YY_NEVER_INTERACTIVE 1
109 %}
110
111
112
113 /* Comments start with a ; and go till end of line */
114 Comment    ;.*
115
116 /* Variable(Value) identifiers start with a % sign */
117 VarID       %[-a-zA-Z$._][-a-zA-Z$._0-9]*
118
119 /* Label identifiers end with a colon */
120 Label       [-a-zA-Z$._0-9]+:
121
122 /* Quoted names can contain any character except " and \ */
123 StringConstant \"[^\"]+\"
124
125
126 /* [PN]Integer: match positive and negative literal integer values that
127  * are preceeded by a '%' character.  These represent unnamed variable slots.
128  */
129 EPInteger     %[0-9]+
130 ENInteger    %-[0-9]+
131
132
133 /* E[PN]Integer: match positive and negative literal integer values */
134 PInteger   [0-9]+
135 NInteger  -[0-9]+
136
137 /* FPConstant - A Floating point constant.
138  */
139 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
140
141 /* HexFPConstant - Floating point constant represented in IEEE format as a
142  *  hexadecimal number for when exponential notation is not precise enough.
143  */
144 HexFPConstant 0x[0-9A-Fa-f]+
145 %%
146
147 {Comment}       { /* Ignore comments for now */ }
148
149 begin           { return BEGINTOK; }
150 end             { return ENDTOK; }
151 true            { return TRUE;  }
152 false           { return FALSE; }
153 declare         { return DECLARE; }
154 global          { return GLOBAL; }
155 constant        { return CONSTANT; }
156 const           { return CONST; }
157 internal        { return INTERNAL; }
158 uninitialized   { return UNINIT; }
159 implementation  { return IMPLEMENTATION; }
160 \.\.\.          { return DOTDOTDOT; }
161 null            { return NULL_TOK; }
162 to              { return TO; }
163 except          { return EXCEPT; }
164 not             { return NOT; }  /* Deprecated, turned into XOR */
165
166 void            { llvmAsmlval.PrimType = Type::VoidTy  ; return VOID;   }
167 bool            { llvmAsmlval.PrimType = Type::BoolTy  ; return BOOL;   }
168 sbyte           { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE;  }
169 ubyte           { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE;  }
170 short           { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT;  }
171 ushort          { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
172 int             { llvmAsmlval.PrimType = Type::IntTy   ; return INT;    }
173 uint            { llvmAsmlval.PrimType = Type::UIntTy  ; return UINT;   }
174 long            { llvmAsmlval.PrimType = Type::LongTy  ; return LONG;   }
175 ulong           { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG;  }
176 float           { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT;  }
177 double          { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
178 type            { llvmAsmlval.PrimType = Type::TypeTy  ; return TYPE;   }
179 label           { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL;  }
180 opaque          { return OPAQUE; }
181
182 add             { RET_TOK(BinaryOpVal, Add, ADD); }
183 sub             { RET_TOK(BinaryOpVal, Sub, SUB); }
184 mul             { RET_TOK(BinaryOpVal, Mul, MUL); }
185 div             { RET_TOK(BinaryOpVal, Div, DIV); }
186 rem             { RET_TOK(BinaryOpVal, Rem, REM); }
187 and             { RET_TOK(BinaryOpVal, And, AND); }
188 or              { RET_TOK(BinaryOpVal, Or , OR ); }
189 xor             { RET_TOK(BinaryOpVal, Xor, XOR); }
190 setne           { RET_TOK(BinaryOpVal, SetNE, SETNE); }
191 seteq           { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
192 setlt           { RET_TOK(BinaryOpVal, SetLT, SETLT); }
193 setgt           { RET_TOK(BinaryOpVal, SetGT, SETGT); }
194 setle           { RET_TOK(BinaryOpVal, SetLE, SETLE); }
195 setge           { RET_TOK(BinaryOpVal, SetGE, SETGE); }
196
197 phi             { RET_TOK(OtherOpVal, PHINode, PHI); }
198 call            { RET_TOK(OtherOpVal, Call, CALL); }
199 cast            { RET_TOK(OtherOpVal, Cast, CAST); }
200 shl             { RET_TOK(OtherOpVal, Shl, SHL); }
201 shr             { RET_TOK(OtherOpVal, Shr, SHR); }
202
203 ret             { RET_TOK(TermOpVal, Ret, RET); }
204 br              { RET_TOK(TermOpVal, Br, BR); }
205 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
206 invoke          { RET_TOK(TermOpVal, Invoke, INVOKE); }
207
208
209 malloc          { RET_TOK(MemOpVal, Malloc, MALLOC); }
210 alloca          { RET_TOK(MemOpVal, Alloca, ALLOCA); }
211 free            { RET_TOK(MemOpVal, Free, FREE); }
212 load            { RET_TOK(MemOpVal, Load, LOAD); }
213 store           { RET_TOK(MemOpVal, Store, STORE); }
214 getelementptr   { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
215
216
217 {VarID}         {
218                   UnEscapeLexed(yytext+1);
219                   llvmAsmlval.StrVal = strdup(yytext+1);             // Skip %
220                   return VAR_ID; 
221                 }
222 {Label}         {
223                   yytext[strlen(yytext)-1] = 0;  // nuke colon
224                   UnEscapeLexed(yytext);
225                   llvmAsmlval.StrVal = strdup(yytext);
226                   return LABELSTR; 
227                 }
228
229 {StringConstant} { // Note that we cannot unescape a string constant here!  The
230                    // string constant might contain a \00 which would not be 
231                    // understood by the string stuff.  It is valid to make a
232                    // [sbyte] c"Hello World\00" constant, for example.
233                    //
234                   yytext[strlen(yytext)-1] = 0;           // nuke end quote
235                   llvmAsmlval.StrVal = strdup(yytext+1);  // Nuke start quote
236                   return STRINGCONSTANT;
237                  }
238
239
240 {PInteger}      { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
241 {NInteger}      { 
242                   uint64_t Val = atoull(yytext+1);
243                   // +1:  we have bigger negative range
244                   if (Val > (uint64_t)INT64_MAX+1)
245                     ThrowException("Constant too large for signed 64 bits!");
246                   llvmAsmlval.SInt64Val = -Val; 
247                   return ESINT64VAL; 
248                 }
249
250
251 {EPInteger}     { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
252 {ENInteger}     {
253                   uint64_t Val = atoull(yytext+2);
254                   // +1:  we have bigger negative range
255                   if (Val > (uint64_t)INT32_MAX+1)
256                     ThrowException("Constant too large for signed 32 bits!");
257                   llvmAsmlval.SIntVal = -Val;
258                   return SINTVAL;
259                 }
260
261 {FPConstant}    { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
262 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
263
264 [ \t\n]         { /* Ignore whitespace */ }
265 .               { return yytext[0]; }
266
267 %%