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