Add support for hexadecimal FP constants!
[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   void *ProxyPointer = &Result;  // Break TBAA correctly
74   cerr << "VALUE: " << *(double*)ProxyPointer << "\n";
75   return *(double*)ProxyPointer;   // Cast Hex constant to double
76 }
77
78
79 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
80 // appropriate character.  If AllowNull is set to false, a \00 value will cause
81 // an exception to be thrown.
82 //
83 // If AllowNull is set to true, the return value of the function points to the
84 // last character of the string in memory.
85 //
86 char *UnEscapeLexed(char *Buffer, bool AllowNull = false) {
87   char *BOut = Buffer;
88   for (char *BIn = Buffer; *BIn; ) {
89     if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
90       char Tmp = BIn[3]; BIn[3] = 0;     // Terminate string
91       *BOut = strtol(BIn+1, 0, 16);  // Convert to number
92       if (!AllowNull && !*BOut)
93         ThrowException("String literal cannot accept \\00 escape!");
94       
95       BIn[3] = Tmp;                  // Restore character
96       BIn += 3;                      // Skip over handled chars
97       ++BOut;
98     } else {
99       *BOut++ = *BIn++;
100     }
101   }
102
103   return BOut;
104 }
105
106 #define YY_NEVER_INTERACTIVE 1
107 %}
108
109
110
111 /* Comments start with a ; and go till end of line */
112 Comment    ;.*
113
114 /* Variable(Value) identifiers start with a % sign */
115 VarID       %[-a-zA-Z$._][-a-zA-Z$._0-9]*
116
117 /* Label identifiers end with a colon */
118 Label       [-a-zA-Z$._0-9]+:
119
120 /* Quoted names can contain any character except " and \ */
121 StringConstant \"[^\"]+\"
122
123
124 /* [PN]Integer: match positive and negative literal integer values that
125  * are preceeded by a '%' character.  These represent unnamed variable slots.
126  */
127 EPInteger     %[0-9]+
128 ENInteger    %-[0-9]+
129
130
131 /* E[PN]Integer: match positive and negative literal integer values */
132 PInteger   [0-9]+
133 NInteger  -[0-9]+
134
135 /* FPConstant - A Floating point constant.
136  */
137 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
138
139 /* HexFPConstant - Floating point constant represented in IEEE format as a
140  *  hexadecimal number for when exponential notation is not precise enough.
141  */
142 HexFPConstant 0x[0-9A-Fa-f]+
143 %%
144
145 {Comment}       { /* Ignore comments for now */ }
146
147 begin           { return BEGINTOK; }
148 end             { return END; }
149 true            { return TRUE;  }
150 false           { return FALSE; }
151 declare         { return DECLARE; }
152 global          { return GLOBAL; }
153 constant        { return CONSTANT; }
154 const           { return CONST; }
155 internal        { return INTERNAL; }
156 uninitialized   { return UNINIT; }
157 implementation  { return IMPLEMENTATION; }
158 \.\.\.          { return DOTDOTDOT; }
159 string          { return STRING; }
160 null            { return NULL_TOK; }
161 to              { return TO; }
162 except          { return EXCEPT; }
163
164 void            { llvmAsmlval.PrimType = Type::VoidTy  ; return VOID;   }
165 bool            { llvmAsmlval.PrimType = Type::BoolTy  ; return BOOL;   }
166 sbyte           { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE;  }
167 ubyte           { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE;  }
168 short           { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT;  }
169 ushort          { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
170 int             { llvmAsmlval.PrimType = Type::IntTy   ; return INT;    }
171 uint            { llvmAsmlval.PrimType = Type::UIntTy  ; return UINT;   }
172 long            { llvmAsmlval.PrimType = Type::LongTy  ; return LONG;   }
173 ulong           { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG;  }
174 float           { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT;  }
175 double          { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
176 type            { llvmAsmlval.PrimType = Type::TypeTy  ; return TYPE;   }
177 label           { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL;  }
178 opaque          { return OPAQUE; }
179
180
181 not             { RET_TOK(UnaryOpVal, Not, NOT); }
182
183 add             { RET_TOK(BinaryOpVal, Add, ADD); }
184 sub             { RET_TOK(BinaryOpVal, Sub, SUB); }
185 mul             { RET_TOK(BinaryOpVal, Mul, MUL); }
186 div             { RET_TOK(BinaryOpVal, Div, DIV); }
187 rem             { RET_TOK(BinaryOpVal, Rem, REM); }
188 and             { RET_TOK(BinaryOpVal, And, AND); }
189 or              { RET_TOK(BinaryOpVal, Or , OR ); }
190 xor             { RET_TOK(BinaryOpVal, Xor, XOR); }
191 setne           { RET_TOK(BinaryOpVal, SetNE, SETNE); }
192 seteq           { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
193 setlt           { RET_TOK(BinaryOpVal, SetLT, SETLT); }
194 setgt           { RET_TOK(BinaryOpVal, SetGT, SETGT); }
195 setle           { RET_TOK(BinaryOpVal, SetLE, SETLE); }
196 setge           { RET_TOK(BinaryOpVal, SetGE, SETGE); }
197
198 phi             { RET_TOK(OtherOpVal, PHINode, PHI); }
199 call            { RET_TOK(OtherOpVal, Call, CALL); }
200 cast            { RET_TOK(OtherOpVal, Cast, CAST); }
201 shl             { RET_TOK(OtherOpVal, Shl, SHL); }
202 shr             { RET_TOK(OtherOpVal, Shr, SHR); }
203
204 ret             { RET_TOK(TermOpVal, Ret, RET); }
205 br              { RET_TOK(TermOpVal, Br, BR); }
206 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
207 invoke          { RET_TOK(TermOpVal, Invoke, INVOKE); }
208
209
210 malloc          { RET_TOK(MemOpVal, Malloc, MALLOC); }
211 alloca          { RET_TOK(MemOpVal, Alloca, ALLOCA); }
212 free            { RET_TOK(MemOpVal, Free, FREE); }
213 load            { RET_TOK(MemOpVal, Load, LOAD); }
214 store           { RET_TOK(MemOpVal, Store, STORE); }
215 getelementptr   { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
216
217
218 {VarID}         {
219                   UnEscapeLexed(yytext+1);
220                   llvmAsmlval.StrVal = strdup(yytext+1);             // Skip %
221                   return VAR_ID; 
222                 }
223 {Label}         {
224                   yytext[strlen(yytext)-1] = 0;  // nuke colon
225                   UnEscapeLexed(yytext);
226                   llvmAsmlval.StrVal = strdup(yytext);
227                   return LABELSTR; 
228                 }
229
230 {StringConstant} { // Note that we cannot unescape a string constant here!  The
231                    // string constant might contain a \00 which would not be 
232                    // understood by the string stuff.  It is valid to make a
233                    // [sbyte] c"Hello World\00" constant, for example.
234                    //
235                   yytext[strlen(yytext)-1] = 0;           // nuke end quote
236                   llvmAsmlval.StrVal = strdup(yytext+1);  // Nuke start quote
237                   return STRINGCONSTANT;
238                  }
239
240
241 {PInteger}      { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
242 {NInteger}      { 
243                   uint64_t Val = atoull(yytext+1);
244                   // +1:  we have bigger negative range
245                   if (Val > (uint64_t)INT64_MAX+1)
246                     ThrowException("Constant too large for signed 64 bits!");
247                   llvmAsmlval.SInt64Val = -Val; 
248                   return ESINT64VAL; 
249                 }
250
251
252 {EPInteger}     { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
253 {ENInteger}     {
254                   uint64_t Val = atoull(yytext+2);
255                   // +1:  we have bigger negative range
256                   if (Val > (uint64_t)INT32_MAX+1)
257                     ThrowException("Constant too large for signed 32 bits!");
258                   llvmAsmlval.SIntVal = -Val;
259                   return SINTVAL;
260                 }
261
262 {FPConstant}    { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
263 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
264
265 [ \t\n]         { /* Ignore whitespace */ }
266 .               { return yytext[0]; }
267
268 %%