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