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