1 /*===-- UpgradeLexer.l - Scanner for 1.9 assembly files --------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the flex scanner for LLVM 1.9 assembly languages files.
12 //===----------------------------------------------------------------------===*/
14 %option prefix="Upgrade"
17 %option never-interactive
22 %option outfile="UpgradeLexer.cpp"
28 #include "UpgradeInternals.h"
29 #include "llvm/Module.h"
31 #include "UpgradeParser.h"
35 #define YY_INPUT(buf,result,max_size) \
37 if (LexInput->good() && !LexInput->eof()) { \
38 LexInput->read(buf,max_size); \
39 result = LexInput->gcount(); \
45 #define YY_NEVER_INTERACTIVE 1
47 // Construct a token value for a non-obsolete token
48 #define RET_TOK(type, Enum, sym) \
49 Upgradelval.type = Enum; \
52 #define RET_TY(sym,NewTY,sign) \
53 Upgradelval.PrimType.T = NewTY; \
55 case 0: Upgradelval.PrimType.S.makeSignless(); break; \
56 case 1: Upgradelval.PrimType.S.makeUnsigned(); break; \
57 case 2: Upgradelval.PrimType.S.makeSigned(); break; \
58 default: assert(0 && "Invalid sign kind"); break; \
64 // TODO: All of the static identifiers are figured out by the lexer,
65 // these should be hashed to reduce the lexer size
67 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
68 // appropriate character. If AllowNull is set to false, a \00 value will cause
69 // an exception to be thrown.
71 // If AllowNull is set to true, the return value of the function points to the
72 // last character of the string in memory.
74 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
76 for (char *BIn = Buffer; *BIn; ) {
77 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
78 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
79 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
80 if (!AllowNull && !*BOut)
81 error("String literal cannot accept \\00 escape!");
83 BIn[3] = Tmp; // Restore character
84 BIn += 3; // Skip over handled chars
94 // atoull - Convert an ascii string of decimal digits into the unsigned long
95 // long representation... this does not have to do input error checking,
96 // because we know that the input will be matched by a suitable regex...
98 static uint64_t atoull(const char *Buffer) {
100 for (; *Buffer; Buffer++) {
101 uint64_t OldRes = Result;
103 Result += *Buffer-'0';
104 if (Result < OldRes) // Uh, oh, overflow detected!!!
105 error("constant bigger than 64 bits detected!");
110 static uint64_t HexIntToVal(const char *Buffer) {
112 for (; *Buffer; ++Buffer) {
113 uint64_t OldRes = Result;
116 if (C >= '0' && C <= '9')
118 else if (C >= 'A' && C <= 'F')
120 else if (C >= 'a' && C <= 'f')
123 if (Result < OldRes) // Uh, oh, overflow detected!!!
124 error("constant bigger than 64 bits detected!");
130 // HexToFP - Convert the ascii string in hexidecimal format to the floating
131 // point representation of it.
133 static double HexToFP(const char *Buffer) {
134 // Behave nicely in the face of C TBAA rules... see:
135 // http://www.nullstone.com/htmls/category/aliastyp.htm
140 UIntToFP.UI = HexIntToVal(Buffer);
142 assert(sizeof(double) == sizeof(uint64_t) &&
143 "Data sizes incompatible on this target!");
144 return UIntToFP.FP; // Cast Hex constant to double
148 } // End llvm namespace
150 using namespace llvm;
156 /* Comments start with a ; and go till end of line */
159 /* Variable(Value) identifiers start with a % sign */
160 VarID [%@][-a-zA-Z$._][-a-zA-Z$._0-9]*
162 /* Label identifiers end with a colon */
163 Label [-a-zA-Z$._0-9]+:
164 QuoteLabel \"[^\"]+\":
166 /* Quoted names can contain any character except " and \ */
167 StringConstant @?\"[^\"]*\"
170 /* [PN]Integer: match positive and negative literal integer values that
171 * are preceeded by a '%' character. These represent unnamed variable slots.
177 /* E[PN]Integer: match positive and negative literal integer values */
181 /* FPConstant - A Floating point constant.
183 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
185 /* HexFPConstant - Floating point constant represented in IEEE format as a
186 * hexadecimal number for when exponential notation is not precise enough.
188 HexFPConstant 0x[0-9A-Fa-f]+
190 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
191 * it to deal with 64 bit numbers.
193 HexIntConstant [us]0x[0-9A-Fa-f]+
196 {Comment} { /* Ignore comments for now */ }
198 begin { return BEGINTOK; }
199 end { return ENDTOK; }
200 true { return TRUETOK; }
201 false { return FALSETOK; }
202 declare { return DECLARE; }
203 global { return GLOBAL; }
204 constant { return CONSTANT; }
205 internal { return INTERNAL; }
206 linkonce { return LINKONCE; }
207 weak { return WEAK; }
208 appending { return APPENDING; }
209 dllimport { return DLLIMPORT; }
210 dllexport { return DLLEXPORT; }
211 extern_weak { return EXTERN_WEAK; }
212 uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
213 external { return EXTERNAL; }
214 implementation { return IMPLEMENTATION; }
215 zeroinitializer { return ZEROINITIALIZER; }
216 \.\.\. { return DOTDOTDOT; }
217 undef { return UNDEF; }
218 null { return NULL_TOK; }
220 except { return EXCEPT; }
221 not { return NOT; } /* Deprecated, turned into XOR */
222 tail { return TAIL; }
223 target { return TARGET; }
224 triple { return TRIPLE; }
225 deplibs { return DEPLIBS; }
226 endian { return ENDIAN; }
227 pointersize { return POINTERSIZE; }
228 datalayout { return DATALAYOUT; }
229 little { return LITTLE; }
231 volatile { return VOLATILE; }
232 align { return ALIGN; }
233 section { return SECTION; }
234 module { return MODULE; }
235 asm { return ASM_TOK; }
236 sideeffect { return SIDEEFFECT; }
238 cc { return CC_TOK; }
239 ccc { return CCC_TOK; }
240 csretcc { return CSRETCC_TOK; }
241 fastcc { return FASTCC_TOK; }
242 coldcc { return COLDCC_TOK; }
243 x86_stdcallcc { return X86_STDCALLCC_TOK; }
244 x86_fastcallcc { return X86_FASTCALLCC_TOK; }
246 sbyte { RET_TY(SBYTE, Type::Int8Ty, 2); }
247 ubyte { RET_TY(UBYTE, Type::Int8Ty, 1); }
248 i8 { RET_TY(UBYTE, Type::Int8Ty, 1); }
249 short { RET_TY(SHORT, Type::Int16Ty, 2); }
250 ushort { RET_TY(USHORT, Type::Int16Ty, 1); }
251 i16 { RET_TY(USHORT, Type::Int16Ty, 1); }
252 int { RET_TY(INT, Type::Int32Ty, 2); }
253 uint { RET_TY(UINT, Type::Int32Ty, 1); }
254 i32 { RET_TY(UINT, Type::Int32Ty, 1); }
255 long { RET_TY(LONG, Type::Int64Ty, 2); }
256 ulong { RET_TY(ULONG, Type::Int64Ty, 1); }
257 i64 { RET_TY(ULONG, Type::Int64Ty, 1); }
258 void { RET_TY(VOID, Type::VoidTy, 0); }
259 bool { RET_TY(BOOL, Type::Int1Ty, 1); }
260 i1 { RET_TY(BOOL, Type::Int1Ty, 1); }
261 float { RET_TY(FLOAT, Type::FloatTy, 0); }
262 double { RET_TY(DOUBLE, Type::DoubleTy,0); }
263 label { RET_TY(LABEL, Type::LabelTy, 0); }
264 type { return TYPE; }
265 opaque { return OPAQUE; }
267 add { RET_TOK(BinaryOpVal, AddOp, ADD); }
268 sub { RET_TOK(BinaryOpVal, SubOp, SUB); }
269 mul { RET_TOK(BinaryOpVal, MulOp, MUL); }
270 div { RET_TOK(BinaryOpVal, DivOp, DIV); }
271 udiv { RET_TOK(BinaryOpVal, UDivOp, UDIV); }
272 sdiv { RET_TOK(BinaryOpVal, SDivOp, SDIV); }
273 fdiv { RET_TOK(BinaryOpVal, FDivOp, FDIV); }
274 rem { RET_TOK(BinaryOpVal, RemOp, REM); }
275 urem { RET_TOK(BinaryOpVal, URemOp, UREM); }
276 srem { RET_TOK(BinaryOpVal, SRemOp, SREM); }
277 frem { RET_TOK(BinaryOpVal, FRemOp, FREM); }
278 and { RET_TOK(BinaryOpVal, AndOp, AND); }
279 or { RET_TOK(BinaryOpVal, OrOp , OR ); }
280 xor { RET_TOK(BinaryOpVal, XorOp, XOR); }
281 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
282 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
283 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
284 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
285 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
286 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
287 shl { RET_TOK(BinaryOpVal, ShlOp, SHL); }
288 shr { RET_TOK(BinaryOpVal, ShrOp, SHR); }
289 lshr { RET_TOK(BinaryOpVal, LShrOp, LSHR); }
290 ashr { RET_TOK(BinaryOpVal, AShrOp, ASHR); }
292 icmp { RET_TOK(OtherOpVal, ICmpOp, ICMP); }
293 fcmp { RET_TOK(OtherOpVal, FCmpOp, FCMP); }
316 phi { RET_TOK(OtherOpVal, PHIOp, PHI_TOK); }
317 call { RET_TOK(OtherOpVal, CallOp, CALL); }
318 cast { RET_TOK(CastOpVal, CastOp, CAST); }
319 trunc { RET_TOK(CastOpVal, TruncOp, TRUNC); }
320 zext { RET_TOK(CastOpVal, ZExtOp , ZEXT); }
321 sext { RET_TOK(CastOpVal, SExtOp, SEXT); }
322 fptrunc { RET_TOK(CastOpVal, FPTruncOp, FPTRUNC); }
323 fpext { RET_TOK(CastOpVal, FPExtOp, FPEXT); }
324 fptoui { RET_TOK(CastOpVal, FPToUIOp, FPTOUI); }
325 fptosi { RET_TOK(CastOpVal, FPToSIOp, FPTOSI); }
326 uitofp { RET_TOK(CastOpVal, UIToFPOp, UITOFP); }
327 sitofp { RET_TOK(CastOpVal, SIToFPOp, SITOFP); }
328 ptrtoint { RET_TOK(CastOpVal, PtrToIntOp, PTRTOINT); }
329 inttoptr { RET_TOK(CastOpVal, IntToPtrOp, INTTOPTR); }
330 bitcast { RET_TOK(CastOpVal, BitCastOp, BITCAST); }
331 select { RET_TOK(OtherOpVal, SelectOp, SELECT); }
332 vanext { return VANEXT_old; }
333 vaarg { return VAARG_old; }
334 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
335 ret { RET_TOK(TermOpVal, RetOp, RET); }
336 br { RET_TOK(TermOpVal, BrOp, BR); }
337 switch { RET_TOK(TermOpVal, SwitchOp, SWITCH); }
338 invoke { RET_TOK(TermOpVal, InvokeOp, INVOKE); }
339 unwind { return UNWIND; }
340 unreachable { RET_TOK(TermOpVal, UnreachableOp, UNREACHABLE); }
342 malloc { RET_TOK(MemOpVal, MallocOp, MALLOC); }
343 alloca { RET_TOK(MemOpVal, AllocaOp, ALLOCA); }
344 free { RET_TOK(MemOpVal, FreeOp, FREE); }
345 load { RET_TOK(MemOpVal, LoadOp, LOAD); }
346 store { RET_TOK(MemOpVal, StoreOp, STORE); }
347 getelementptr { RET_TOK(MemOpVal, GetElementPtrOp, GETELEMENTPTR); }
349 extractelement { RET_TOK(OtherOpVal, ExtractElementOp, EXTRACTELEMENT); }
350 insertelement { RET_TOK(OtherOpVal, InsertElementOp, INSERTELEMENT); }
351 shufflevector { RET_TOK(OtherOpVal, ShuffleVectorOp, SHUFFLEVECTOR); }
355 UnEscapeLexed(yytext+1);
356 Upgradelval.StrVal = strdup(yytext+1); // Skip %
360 yytext[strlen(yytext)-1] = 0; // nuke colon
361 UnEscapeLexed(yytext);
362 Upgradelval.StrVal = strdup(yytext);
366 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
367 UnEscapeLexed(yytext+1);
368 Upgradelval.StrVal = strdup(yytext+1);
372 {StringConstant} { // Note that we cannot unescape a string constant here! The
373 // string constant might contain a \00 which would not be
374 // understood by the string stuff. It is valid to make a
375 // [sbyte] c"Hello World\00" constant, for example.
377 yytext[strlen(yytext)-1] = 0; // nuke end quote
378 Upgradelval.StrVal = strdup(yytext+1); // Nuke start quote
379 return STRINGCONSTANT;
383 {PInteger} { Upgradelval.UInt64Val = atoull(yytext); return EUINT64VAL; }
385 uint64_t Val = atoull(yytext+1);
386 // +1: we have bigger negative range
387 if (Val > (uint64_t)INT64_MAX+1)
388 error("Constant too large for signed 64 bits!");
389 Upgradelval.SInt64Val = -Val;
393 Upgradelval.UInt64Val = HexIntToVal(yytext+3);
394 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
398 uint64_t Val = atoull(yytext+1);
399 if ((unsigned)Val != Val)
400 error("Invalid value number (too large)!");
401 Upgradelval.UIntVal = unsigned(Val);
405 uint64_t Val = atoull(yytext+2);
406 // +1: we have bigger negative range
407 if (Val > (uint64_t)INT32_MAX+1)
408 error("Constant too large for signed 32 bits!");
409 Upgradelval.SIntVal = (int)-Val;
413 {FPConstant} { Upgradelval.FPVal = new APFloat(atof(yytext)); return FPVAL; }
414 {HexFPConstant} { Upgradelval.FPVal = new APFloat(HexToFP(yytext));
419 /* Make sure to free the internal buffers for flex when we are
420 * done reading our input!
422 yy_delete_buffer(YY_CURRENT_BUFFER);
426 [ \r\t\n] { /* Ignore whitespace */ }
427 . { return yytext[0]; }