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