blob: 702c95350dcb1078ef17903e72e870bbff7db98f [file] [log] [blame]
Reid Spencer96839be2006-11-30 16:50:26 +00001/*===-- UpgradeLexer.l - Scanner for 1.9 assembly files --------*- C++ -*--===//
Reid Spencere7c3c602006-11-30 06:36:44 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnercf786592007-12-29 20:47:37 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencere7c3c602006-11-30 06:36:44 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer96839be2006-11-30 16:50:26 +000010// This file implements the flex scanner for LLVM 1.9 assembly languages files.
Dale Johannesen4292d1c2007-09-28 18:06:58 +000011// This doesn't handle long double constants, since LLVM 1.9 did not have them.
Reid Spencere7c3c602006-11-30 06:36:44 +000012//
13//===----------------------------------------------------------------------===*/
14
15%option prefix="Upgrade"
16%option yylineno
17%option nostdinit
18%option never-interactive
19%option batch
20%option noyywrap
21%option nodefault
22%option 8bit
23%option outfile="UpgradeLexer.cpp"
24%option ecs
25%option noreject
26%option noyymore
27
28%{
Reid Spencer319a7302007-01-05 17:20:02 +000029#include "UpgradeInternals.h"
Reid Spencer950bf602007-01-26 08:19:09 +000030#include "llvm/Module.h"
31#include <list>
Reid Spencere7c3c602006-11-30 06:36:44 +000032#include "UpgradeParser.h"
33#include <cctype>
34#include <cstdlib>
35
Reid Spencer96839be2006-11-30 16:50:26 +000036#define YY_INPUT(buf,result,max_size) \
37{ \
38 if (LexInput->good() && !LexInput->eof()) { \
39 LexInput->read(buf,max_size); \
40 result = LexInput->gcount(); \
41 } else {\
42 result = YY_NULL; \
43 } \
44}
45
Reid Spencer950bf602007-01-26 08:19:09 +000046#define YY_NEVER_INTERACTIVE 1
Reid Spencer96839be2006-11-30 16:50:26 +000047
Reid Spencere7c3c602006-11-30 06:36:44 +000048// Construct a token value for a non-obsolete token
Reid Spencer950bf602007-01-26 08:19:09 +000049#define RET_TOK(type, Enum, sym) \
50 Upgradelval.type = Enum; \
Reid Spencere77e35e2006-12-01 20:26:20 +000051 return sym
52
Reid Spencer950bf602007-01-26 08:19:09 +000053#define RET_TY(sym,NewTY,sign) \
54 Upgradelval.PrimType.T = NewTY; \
Reid Spencerbb1fd572007-03-21 17:15:50 +000055 switch (sign) { \
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; \
60 }\
Reid Spencere7c3c602006-11-30 06:36:44 +000061 return sym
62
Reid Spencer950bf602007-01-26 08:19:09 +000063namespace llvm {
64
65// TODO: All of the static identifiers are figured out by the lexer,
66// these should be hashed to reduce the lexer size
67
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.
71//
72// If AllowNull is set to true, the return value of the function points to the
73// last character of the string in memory.
74//
75char *UnEscapeLexed(char *Buffer, bool AllowNull) {
76 char *BOut = Buffer;
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!");
83
84 BIn[3] = Tmp; // Restore character
85 BIn += 3; // Skip over handled chars
86 ++BOut;
87 } else {
88 *BOut++ = *BIn++;
89 }
90 }
91
92 return BOut;
93}
94
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...
98//
99static uint64_t atoull(const char *Buffer) {
100 uint64_t Result = 0;
101 for (; *Buffer; Buffer++) {
102 uint64_t OldRes = Result;
103 Result *= 10;
104 Result += *Buffer-'0';
105 if (Result < OldRes) // Uh, oh, overflow detected!!!
106 error("constant bigger than 64 bits detected!");
107 }
108 return Result;
109}
110
111static uint64_t HexIntToVal(const char *Buffer) {
112 uint64_t Result = 0;
113 for (; *Buffer; ++Buffer) {
114 uint64_t OldRes = Result;
115 Result *= 16;
116 char C = *Buffer;
117 if (C >= '0' && C <= '9')
118 Result += C-'0';
119 else if (C >= 'A' && C <= 'F')
120 Result += C-'A'+10;
121 else if (C >= 'a' && C <= 'f')
122 Result += C-'a'+10;
123
124 if (Result < OldRes) // Uh, oh, overflow detected!!!
125 error("constant bigger than 64 bits detected!");
126 }
127 return Result;
128}
129
130
131// HexToFP - Convert the ascii string in hexidecimal format to the floating
132// point representation of it.
133//
134static 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
137 union {
138 uint64_t UI;
139 double FP;
140 } UIntToFP;
141 UIntToFP.UI = HexIntToVal(Buffer);
142
143 assert(sizeof(double) == sizeof(uint64_t) &&
144 "Data sizes incompatible on this target!");
145 return UIntToFP.FP; // Cast Hex constant to double
146}
147
148
149} // End llvm namespace
150
151using namespace llvm;
152
Reid Spencere7c3c602006-11-30 06:36:44 +0000153%}
154
155
156
157/* Comments start with a ; and go till end of line */
158Comment ;.*
159
160/* Variable(Value) identifiers start with a % sign */
Reid Spencer785a5ae2007-02-08 00:21:40 +0000161VarID [%@][-a-zA-Z$._][-a-zA-Z$._0-9]*
Reid Spencere7c3c602006-11-30 06:36:44 +0000162
163/* Label identifiers end with a colon */
164Label [-a-zA-Z$._0-9]+:
165QuoteLabel \"[^\"]+\":
166
167/* Quoted names can contain any character except " and \ */
Reid Spencer785a5ae2007-02-08 00:21:40 +0000168StringConstant @?\"[^\"]*\"
Reid Spencere7c3c602006-11-30 06:36:44 +0000169
170
171/* [PN]Integer: match positive and negative literal integer values that
172 * are preceeded by a '%' character. These represent unnamed variable slots.
173 */
174EPInteger %[0-9]+
175ENInteger %-[0-9]+
176
177
178/* E[PN]Integer: match positive and negative literal integer values */
179PInteger [0-9]+
180NInteger -[0-9]+
181
182/* FPConstant - A Floating point constant.
183 */
184FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
185
186/* HexFPConstant - Floating point constant represented in IEEE format as a
187 * hexadecimal number for when exponential notation is not precise enough.
188 */
189HexFPConstant 0x[0-9A-Fa-f]+
190
191/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
192 * it to deal with 64 bit numbers.
193 */
194HexIntConstant [us]0x[0-9A-Fa-f]+
195%%
196
197{Comment} { /* Ignore comments for now */ }
198
Reid Spencer950bf602007-01-26 08:19:09 +0000199begin { return BEGINTOK; }
200end { return ENDTOK; }
201true { return TRUETOK; }
202false { return FALSETOK; }
203declare { return DECLARE; }
204global { return GLOBAL; }
205constant { return CONSTANT; }
206internal { return INTERNAL; }
207linkonce { return LINKONCE; }
208weak { return WEAK; }
209appending { return APPENDING; }
210dllimport { return DLLIMPORT; }
211dllexport { return DLLEXPORT; }
212extern_weak { return EXTERN_WEAK; }
213uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
214external { return EXTERNAL; }
215implementation { return IMPLEMENTATION; }
216zeroinitializer { return ZEROINITIALIZER; }
217\.\.\. { return DOTDOTDOT; }
218undef { return UNDEF; }
219null { return NULL_TOK; }
220to { return TO; }
221except { return EXCEPT; }
222not { return NOT; } /* Deprecated, turned into XOR */
223tail { return TAIL; }
224target { return TARGET; }
225triple { return TRIPLE; }
226deplibs { return DEPLIBS; }
227endian { return ENDIAN; }
228pointersize { return POINTERSIZE; }
229datalayout { return DATALAYOUT; }
230little { return LITTLE; }
231big { return BIG; }
232volatile { return VOLATILE; }
233align { return ALIGN; }
234section { return SECTION; }
235module { return MODULE; }
236asm { return ASM_TOK; }
237sideeffect { return SIDEEFFECT; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000238
Reid Spencer950bf602007-01-26 08:19:09 +0000239cc { return CC_TOK; }
240ccc { return CCC_TOK; }
241csretcc { return CSRETCC_TOK; }
242fastcc { return FASTCC_TOK; }
243coldcc { return COLDCC_TOK; }
244x86_stdcallcc { return X86_STDCALLCC_TOK; }
245x86_fastcallcc { return X86_FASTCALLCC_TOK; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000246
Reid Spencerbb1fd572007-03-21 17:15:50 +0000247sbyte { RET_TY(SBYTE, Type::Int8Ty, 2); }
248ubyte { RET_TY(UBYTE, Type::Int8Ty, 1); }
249i8 { RET_TY(UBYTE, Type::Int8Ty, 1); }
250short { RET_TY(SHORT, Type::Int16Ty, 2); }
251ushort { RET_TY(USHORT, Type::Int16Ty, 1); }
252i16 { RET_TY(USHORT, Type::Int16Ty, 1); }
253int { RET_TY(INT, Type::Int32Ty, 2); }
254uint { RET_TY(UINT, Type::Int32Ty, 1); }
255i32 { RET_TY(UINT, Type::Int32Ty, 1); }
256long { RET_TY(LONG, Type::Int64Ty, 2); }
257ulong { RET_TY(ULONG, Type::Int64Ty, 1); }
258i64 { RET_TY(ULONG, Type::Int64Ty, 1); }
259void { RET_TY(VOID, Type::VoidTy, 0); }
260bool { RET_TY(BOOL, Type::Int1Ty, 1); }
261i1 { RET_TY(BOOL, Type::Int1Ty, 1); }
262float { RET_TY(FLOAT, Type::FloatTy, 0); }
263double { RET_TY(DOUBLE, Type::DoubleTy,0); }
264label { RET_TY(LABEL, Type::LabelTy, 0); }
Reid Spencer950bf602007-01-26 08:19:09 +0000265type { return TYPE; }
266opaque { return OPAQUE; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000267
Reid Spencer950bf602007-01-26 08:19:09 +0000268add { RET_TOK(BinaryOpVal, AddOp, ADD); }
269sub { RET_TOK(BinaryOpVal, SubOp, SUB); }
270mul { RET_TOK(BinaryOpVal, MulOp, MUL); }
271div { RET_TOK(BinaryOpVal, DivOp, DIV); }
272udiv { RET_TOK(BinaryOpVal, UDivOp, UDIV); }
273sdiv { RET_TOK(BinaryOpVal, SDivOp, SDIV); }
274fdiv { RET_TOK(BinaryOpVal, FDivOp, FDIV); }
275rem { RET_TOK(BinaryOpVal, RemOp, REM); }
276urem { RET_TOK(BinaryOpVal, URemOp, UREM); }
277srem { RET_TOK(BinaryOpVal, SRemOp, SREM); }
278frem { RET_TOK(BinaryOpVal, FRemOp, FREM); }
279and { RET_TOK(BinaryOpVal, AndOp, AND); }
280or { RET_TOK(BinaryOpVal, OrOp , OR ); }
281xor { RET_TOK(BinaryOpVal, XorOp, XOR); }
282setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
283seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
284setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
285setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
286setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
287setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
Reid Spencer832254e2007-02-02 02:16:23 +0000288shl { RET_TOK(BinaryOpVal, ShlOp, SHL); }
289shr { RET_TOK(BinaryOpVal, ShrOp, SHR); }
290lshr { RET_TOK(BinaryOpVal, LShrOp, LSHR); }
291ashr { RET_TOK(BinaryOpVal, AShrOp, ASHR); }
292
Reid Spencer950bf602007-01-26 08:19:09 +0000293icmp { RET_TOK(OtherOpVal, ICmpOp, ICMP); }
294fcmp { RET_TOK(OtherOpVal, FCmpOp, FCMP); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000295
Reid Spencer950bf602007-01-26 08:19:09 +0000296eq { return EQ; }
297ne { return NE; }
298slt { return SLT; }
299sgt { return SGT; }
300sle { return SLE; }
301sge { return SGE; }
302ult { return ULT; }
303ugt { return UGT; }
304ule { return ULE; }
305uge { return UGE; }
306oeq { return OEQ; }
307one { return ONE; }
308olt { return OLT; }
309ogt { return OGT; }
310ole { return OLE; }
311oge { return OGE; }
312ord { return ORD; }
313uno { return UNO; }
314ueq { return UEQ; }
315une { return UNE; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000316
Reid Spencer950bf602007-01-26 08:19:09 +0000317phi { RET_TOK(OtherOpVal, PHIOp, PHI_TOK); }
318call { RET_TOK(OtherOpVal, CallOp, CALL); }
319cast { RET_TOK(CastOpVal, CastOp, CAST); }
320trunc { RET_TOK(CastOpVal, TruncOp, TRUNC); }
321zext { RET_TOK(CastOpVal, ZExtOp , ZEXT); }
322sext { RET_TOK(CastOpVal, SExtOp, SEXT); }
323fptrunc { RET_TOK(CastOpVal, FPTruncOp, FPTRUNC); }
324fpext { RET_TOK(CastOpVal, FPExtOp, FPEXT); }
325fptoui { RET_TOK(CastOpVal, FPToUIOp, FPTOUI); }
326fptosi { RET_TOK(CastOpVal, FPToSIOp, FPTOSI); }
327uitofp { RET_TOK(CastOpVal, UIToFPOp, UITOFP); }
328sitofp { RET_TOK(CastOpVal, SIToFPOp, SITOFP); }
329ptrtoint { RET_TOK(CastOpVal, PtrToIntOp, PTRTOINT); }
330inttoptr { RET_TOK(CastOpVal, IntToPtrOp, INTTOPTR); }
331bitcast { RET_TOK(CastOpVal, BitCastOp, BITCAST); }
332select { RET_TOK(OtherOpVal, SelectOp, SELECT); }
Reid Spencer950bf602007-01-26 08:19:09 +0000333vanext { return VANEXT_old; }
334vaarg { return VAARG_old; }
335va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
336ret { RET_TOK(TermOpVal, RetOp, RET); }
337br { RET_TOK(TermOpVal, BrOp, BR); }
338switch { RET_TOK(TermOpVal, SwitchOp, SWITCH); }
339invoke { RET_TOK(TermOpVal, InvokeOp, INVOKE); }
340unwind { return UNWIND; }
341unreachable { RET_TOK(TermOpVal, UnreachableOp, UNREACHABLE); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000342
Reid Spencer950bf602007-01-26 08:19:09 +0000343malloc { RET_TOK(MemOpVal, MallocOp, MALLOC); }
344alloca { RET_TOK(MemOpVal, AllocaOp, ALLOCA); }
345free { RET_TOK(MemOpVal, FreeOp, FREE); }
346load { RET_TOK(MemOpVal, LoadOp, LOAD); }
347store { RET_TOK(MemOpVal, StoreOp, STORE); }
348getelementptr { RET_TOK(MemOpVal, GetElementPtrOp, GETELEMENTPTR); }
349
350extractelement { RET_TOK(OtherOpVal, ExtractElementOp, EXTRACTELEMENT); }
351insertelement { RET_TOK(OtherOpVal, InsertElementOp, INSERTELEMENT); }
352shufflevector { RET_TOK(OtherOpVal, ShuffleVectorOp, SHUFFLEVECTOR); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000353
354
Reid Spencer950bf602007-01-26 08:19:09 +0000355{VarID} {
356 UnEscapeLexed(yytext+1);
357 Upgradelval.StrVal = strdup(yytext+1); // Skip %
358 return VAR_ID;
359 }
360{Label} {
361 yytext[strlen(yytext)-1] = 0; // nuke colon
362 UnEscapeLexed(yytext);
363 Upgradelval.StrVal = strdup(yytext);
364 return LABELSTR;
365 }
366{QuoteLabel} {
367 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
368 UnEscapeLexed(yytext+1);
369 Upgradelval.StrVal = strdup(yytext+1);
370 return LABELSTR;
371 }
372
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.
377 //
378 yytext[strlen(yytext)-1] = 0; // nuke end quote
379 Upgradelval.StrVal = strdup(yytext+1); // Nuke start quote
380 return STRINGCONSTANT;
381 }
382
383
384{PInteger} { Upgradelval.UInt64Val = atoull(yytext); return EUINT64VAL; }
385{NInteger} {
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;
391 return ESINT64VAL;
392 }
393{HexIntConstant} {
394 Upgradelval.UInt64Val = HexIntToVal(yytext+3);
395 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
396 }
397
398{EPInteger} {
399 uint64_t Val = atoull(yytext+1);
400 if ((unsigned)Val != Val)
401 error("Invalid value number (too large)!");
402 Upgradelval.UIntVal = unsigned(Val);
403 return UINTVAL;
404 }
405{ENInteger} {
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;
411 return SINTVAL;
412 }
413
Dale Johannesen43421b32007-09-06 18:13:44 +0000414{FPConstant} { Upgradelval.FPVal = new APFloat(atof(yytext)); return FPVAL; }
415{HexFPConstant} { Upgradelval.FPVal = new APFloat(HexToFP(yytext));
416 return FPVAL;
417 }
Reid Spencer950bf602007-01-26 08:19:09 +0000418
419<<EOF>> {
Reid Spencere7c3c602006-11-30 06:36:44 +0000420 /* Make sure to free the internal buffers for flex when we are
421 * done reading our input!
422 */
423 yy_delete_buffer(YY_CURRENT_BUFFER);
424 return EOF;
425 }
426
427[ \r\t\n] { /* Ignore whitespace */ }
428. { return yytext[0]; }
429
430%%