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