blob: ebab6db9c0c2b101a8f392e4f30a99af436c1e84 [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; \
54 Upgradelval.PrimType.S = sign; \
Reid Spencere7c3c602006-11-30 06:36:44 +000055 return sym
56
Reid Spencer950bf602007-01-26 08:19:09 +000057namespace llvm {
58
59// TODO: All of the static identifiers are figured out by the lexer,
60// these should be hashed to reduce the lexer size
61
62// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
63// appropriate character. If AllowNull is set to false, a \00 value will cause
64// an exception to be thrown.
65//
66// If AllowNull is set to true, the return value of the function points to the
67// last character of the string in memory.
68//
69char *UnEscapeLexed(char *Buffer, bool AllowNull) {
70 char *BOut = Buffer;
71 for (char *BIn = Buffer; *BIn; ) {
72 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
73 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
74 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
75 if (!AllowNull && !*BOut)
76 error("String literal cannot accept \\00 escape!");
77
78 BIn[3] = Tmp; // Restore character
79 BIn += 3; // Skip over handled chars
80 ++BOut;
81 } else {
82 *BOut++ = *BIn++;
83 }
84 }
85
86 return BOut;
87}
88
89// atoull - Convert an ascii string of decimal digits into the unsigned long
90// long representation... this does not have to do input error checking,
91// because we know that the input will be matched by a suitable regex...
92//
93static uint64_t atoull(const char *Buffer) {
94 uint64_t Result = 0;
95 for (; *Buffer; Buffer++) {
96 uint64_t OldRes = Result;
97 Result *= 10;
98 Result += *Buffer-'0';
99 if (Result < OldRes) // Uh, oh, overflow detected!!!
100 error("constant bigger than 64 bits detected!");
101 }
102 return Result;
103}
104
105static uint64_t HexIntToVal(const char *Buffer) {
106 uint64_t Result = 0;
107 for (; *Buffer; ++Buffer) {
108 uint64_t OldRes = Result;
109 Result *= 16;
110 char C = *Buffer;
111 if (C >= '0' && C <= '9')
112 Result += C-'0';
113 else if (C >= 'A' && C <= 'F')
114 Result += C-'A'+10;
115 else if (C >= 'a' && C <= 'f')
116 Result += C-'a'+10;
117
118 if (Result < OldRes) // Uh, oh, overflow detected!!!
119 error("constant bigger than 64 bits detected!");
120 }
121 return Result;
122}
123
124
125// HexToFP - Convert the ascii string in hexidecimal format to the floating
126// point representation of it.
127//
128static double HexToFP(const char *Buffer) {
129 // Behave nicely in the face of C TBAA rules... see:
130 // http://www.nullstone.com/htmls/category/aliastyp.htm
131 union {
132 uint64_t UI;
133 double FP;
134 } UIntToFP;
135 UIntToFP.UI = HexIntToVal(Buffer);
136
137 assert(sizeof(double) == sizeof(uint64_t) &&
138 "Data sizes incompatible on this target!");
139 return UIntToFP.FP; // Cast Hex constant to double
140}
141
142
143} // End llvm namespace
144
145using namespace llvm;
146
Reid Spencere7c3c602006-11-30 06:36:44 +0000147%}
148
149
150
151/* Comments start with a ; and go till end of line */
152Comment ;.*
153
154/* Variable(Value) identifiers start with a % sign */
Reid Spencer785a5ae2007-02-08 00:21:40 +0000155VarID [%@][-a-zA-Z$._][-a-zA-Z$._0-9]*
Reid Spencere7c3c602006-11-30 06:36:44 +0000156
157/* Label identifiers end with a colon */
158Label [-a-zA-Z$._0-9]+:
159QuoteLabel \"[^\"]+\":
160
161/* Quoted names can contain any character except " and \ */
Reid Spencer785a5ae2007-02-08 00:21:40 +0000162StringConstant @?\"[^\"]*\"
Reid Spencere7c3c602006-11-30 06:36:44 +0000163
164
165/* [PN]Integer: match positive and negative literal integer values that
166 * are preceeded by a '%' character. These represent unnamed variable slots.
167 */
168EPInteger %[0-9]+
169ENInteger %-[0-9]+
170
171
172/* E[PN]Integer: match positive and negative literal integer values */
173PInteger [0-9]+
174NInteger -[0-9]+
175
176/* FPConstant - A Floating point constant.
177 */
178FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
179
180/* HexFPConstant - Floating point constant represented in IEEE format as a
181 * hexadecimal number for when exponential notation is not precise enough.
182 */
183HexFPConstant 0x[0-9A-Fa-f]+
184
185/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
186 * it to deal with 64 bit numbers.
187 */
188HexIntConstant [us]0x[0-9A-Fa-f]+
189%%
190
191{Comment} { /* Ignore comments for now */ }
192
Reid Spencer950bf602007-01-26 08:19:09 +0000193begin { return BEGINTOK; }
194end { return ENDTOK; }
195true { return TRUETOK; }
196false { return FALSETOK; }
197declare { return DECLARE; }
198global { return GLOBAL; }
199constant { return CONSTANT; }
200internal { return INTERNAL; }
201linkonce { return LINKONCE; }
202weak { return WEAK; }
203appending { return APPENDING; }
204dllimport { return DLLIMPORT; }
205dllexport { return DLLEXPORT; }
206extern_weak { return EXTERN_WEAK; }
207uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
208external { return EXTERNAL; }
209implementation { return IMPLEMENTATION; }
210zeroinitializer { return ZEROINITIALIZER; }
211\.\.\. { return DOTDOTDOT; }
212undef { return UNDEF; }
213null { return NULL_TOK; }
214to { return TO; }
215except { return EXCEPT; }
216not { return NOT; } /* Deprecated, turned into XOR */
217tail { return TAIL; }
218target { return TARGET; }
219triple { return TRIPLE; }
220deplibs { return DEPLIBS; }
221endian { return ENDIAN; }
222pointersize { return POINTERSIZE; }
223datalayout { return DATALAYOUT; }
224little { return LITTLE; }
225big { return BIG; }
226volatile { return VOLATILE; }
227align { return ALIGN; }
228section { return SECTION; }
229module { return MODULE; }
230asm { return ASM_TOK; }
231sideeffect { return SIDEEFFECT; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000232
Reid Spencer950bf602007-01-26 08:19:09 +0000233cc { return CC_TOK; }
234ccc { return CCC_TOK; }
235csretcc { return CSRETCC_TOK; }
236fastcc { return FASTCC_TOK; }
237coldcc { return COLDCC_TOK; }
238x86_stdcallcc { return X86_STDCALLCC_TOK; }
239x86_fastcallcc { return X86_FASTCALLCC_TOK; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000240
Reid Spencer950bf602007-01-26 08:19:09 +0000241sbyte { RET_TY(SBYTE, Type::Int8Ty, Signed); }
242ubyte { RET_TY(UBYTE, Type::Int8Ty, Unsigned); }
Reid Spencer785a5ae2007-02-08 00:21:40 +0000243i8 { RET_TY(UBYTE, Type::Int8Ty, Unsigned); }
Reid Spencer950bf602007-01-26 08:19:09 +0000244short { RET_TY(SHORT, Type::Int16Ty, Signed); }
245ushort { RET_TY(USHORT, Type::Int16Ty, Unsigned); }
Reid Spencer785a5ae2007-02-08 00:21:40 +0000246i16 { RET_TY(USHORT, Type::Int16Ty, Unsigned); }
Reid Spencer950bf602007-01-26 08:19:09 +0000247int { RET_TY(INT, Type::Int32Ty, Signed); }
248uint { RET_TY(UINT, Type::Int32Ty, Unsigned); }
Reid Spencer785a5ae2007-02-08 00:21:40 +0000249i32 { RET_TY(UINT, Type::Int32Ty, Unsigned); }
Reid Spencer950bf602007-01-26 08:19:09 +0000250long { RET_TY(LONG, Type::Int64Ty, Signed); }
251ulong { RET_TY(ULONG, Type::Int64Ty, Unsigned); }
Reid Spencer785a5ae2007-02-08 00:21:40 +0000252i64 { RET_TY(ULONG, Type::Int64Ty, Unsigned); }
Reid Spencer950bf602007-01-26 08:19:09 +0000253void { RET_TY(VOID, Type::VoidTy, Signless ); }
254bool { RET_TY(BOOL, Type::Int1Ty, Unsigned ); }
Reid Spencer785a5ae2007-02-08 00:21:40 +0000255i1 { RET_TY(BOOL, Type::Int1Ty, Unsigned ); }
Reid Spencer950bf602007-01-26 08:19:09 +0000256float { RET_TY(FLOAT, Type::FloatTy, Signless ); }
257double { RET_TY(DOUBLE, Type::DoubleTy,Signless); }
258label { RET_TY(LABEL, Type::LabelTy, Signless ); }
259type { return TYPE; }
260opaque { return OPAQUE; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000261
Reid Spencer950bf602007-01-26 08:19:09 +0000262add { RET_TOK(BinaryOpVal, AddOp, ADD); }
263sub { RET_TOK(BinaryOpVal, SubOp, SUB); }
264mul { RET_TOK(BinaryOpVal, MulOp, MUL); }
265div { RET_TOK(BinaryOpVal, DivOp, DIV); }
266udiv { RET_TOK(BinaryOpVal, UDivOp, UDIV); }
267sdiv { RET_TOK(BinaryOpVal, SDivOp, SDIV); }
268fdiv { RET_TOK(BinaryOpVal, FDivOp, FDIV); }
269rem { RET_TOK(BinaryOpVal, RemOp, REM); }
270urem { RET_TOK(BinaryOpVal, URemOp, UREM); }
271srem { RET_TOK(BinaryOpVal, SRemOp, SREM); }
272frem { RET_TOK(BinaryOpVal, FRemOp, FREM); }
273and { RET_TOK(BinaryOpVal, AndOp, AND); }
274or { RET_TOK(BinaryOpVal, OrOp , OR ); }
275xor { RET_TOK(BinaryOpVal, XorOp, XOR); }
276setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
277seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
278setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
279setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
280setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
281setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
Reid Spencer832254e2007-02-02 02:16:23 +0000282shl { RET_TOK(BinaryOpVal, ShlOp, SHL); }
283shr { RET_TOK(BinaryOpVal, ShrOp, SHR); }
284lshr { RET_TOK(BinaryOpVal, LShrOp, LSHR); }
285ashr { RET_TOK(BinaryOpVal, AShrOp, ASHR); }
286
Reid Spencer950bf602007-01-26 08:19:09 +0000287icmp { RET_TOK(OtherOpVal, ICmpOp, ICMP); }
288fcmp { RET_TOK(OtherOpVal, FCmpOp, FCMP); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000289
Reid Spencer950bf602007-01-26 08:19:09 +0000290eq { return EQ; }
291ne { return NE; }
292slt { return SLT; }
293sgt { return SGT; }
294sle { return SLE; }
295sge { return SGE; }
296ult { return ULT; }
297ugt { return UGT; }
298ule { return ULE; }
299uge { return UGE; }
300oeq { return OEQ; }
301one { return ONE; }
302olt { return OLT; }
303ogt { return OGT; }
304ole { return OLE; }
305oge { return OGE; }
306ord { return ORD; }
307uno { return UNO; }
308ueq { return UEQ; }
309une { return UNE; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000310
Reid Spencer950bf602007-01-26 08:19:09 +0000311phi { RET_TOK(OtherOpVal, PHIOp, PHI_TOK); }
312call { RET_TOK(OtherOpVal, CallOp, CALL); }
313cast { RET_TOK(CastOpVal, CastOp, CAST); }
314trunc { RET_TOK(CastOpVal, TruncOp, TRUNC); }
315zext { RET_TOK(CastOpVal, ZExtOp , ZEXT); }
316sext { RET_TOK(CastOpVal, SExtOp, SEXT); }
317fptrunc { RET_TOK(CastOpVal, FPTruncOp, FPTRUNC); }
318fpext { RET_TOK(CastOpVal, FPExtOp, FPEXT); }
319fptoui { RET_TOK(CastOpVal, FPToUIOp, FPTOUI); }
320fptosi { RET_TOK(CastOpVal, FPToSIOp, FPTOSI); }
321uitofp { RET_TOK(CastOpVal, UIToFPOp, UITOFP); }
322sitofp { RET_TOK(CastOpVal, SIToFPOp, SITOFP); }
323ptrtoint { RET_TOK(CastOpVal, PtrToIntOp, PTRTOINT); }
324inttoptr { RET_TOK(CastOpVal, IntToPtrOp, INTTOPTR); }
325bitcast { RET_TOK(CastOpVal, BitCastOp, BITCAST); }
326select { RET_TOK(OtherOpVal, SelectOp, SELECT); }
Reid Spencer950bf602007-01-26 08:19:09 +0000327vanext { return VANEXT_old; }
328vaarg { return VAARG_old; }
329va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
330ret { RET_TOK(TermOpVal, RetOp, RET); }
331br { RET_TOK(TermOpVal, BrOp, BR); }
332switch { RET_TOK(TermOpVal, SwitchOp, SWITCH); }
333invoke { RET_TOK(TermOpVal, InvokeOp, INVOKE); }
334unwind { return UNWIND; }
335unreachable { RET_TOK(TermOpVal, UnreachableOp, UNREACHABLE); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000336
Reid Spencer950bf602007-01-26 08:19:09 +0000337malloc { RET_TOK(MemOpVal, MallocOp, MALLOC); }
338alloca { RET_TOK(MemOpVal, AllocaOp, ALLOCA); }
339free { RET_TOK(MemOpVal, FreeOp, FREE); }
340load { RET_TOK(MemOpVal, LoadOp, LOAD); }
341store { RET_TOK(MemOpVal, StoreOp, STORE); }
342getelementptr { RET_TOK(MemOpVal, GetElementPtrOp, GETELEMENTPTR); }
343
344extractelement { RET_TOK(OtherOpVal, ExtractElementOp, EXTRACTELEMENT); }
345insertelement { RET_TOK(OtherOpVal, InsertElementOp, INSERTELEMENT); }
346shufflevector { RET_TOK(OtherOpVal, ShuffleVectorOp, SHUFFLEVECTOR); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000347
348
Reid Spencer950bf602007-01-26 08:19:09 +0000349{VarID} {
350 UnEscapeLexed(yytext+1);
351 Upgradelval.StrVal = strdup(yytext+1); // Skip %
352 return VAR_ID;
353 }
354{Label} {
355 yytext[strlen(yytext)-1] = 0; // nuke colon
356 UnEscapeLexed(yytext);
357 Upgradelval.StrVal = strdup(yytext);
358 return LABELSTR;
359 }
360{QuoteLabel} {
361 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
362 UnEscapeLexed(yytext+1);
363 Upgradelval.StrVal = strdup(yytext+1);
364 return LABELSTR;
365 }
366
367{StringConstant} { // Note that we cannot unescape a string constant here! The
368 // string constant might contain a \00 which would not be
369 // understood by the string stuff. It is valid to make a
370 // [sbyte] c"Hello World\00" constant, for example.
371 //
372 yytext[strlen(yytext)-1] = 0; // nuke end quote
373 Upgradelval.StrVal = strdup(yytext+1); // Nuke start quote
374 return STRINGCONSTANT;
375 }
376
377
378{PInteger} { Upgradelval.UInt64Val = atoull(yytext); return EUINT64VAL; }
379{NInteger} {
380 uint64_t Val = atoull(yytext+1);
381 // +1: we have bigger negative range
382 if (Val > (uint64_t)INT64_MAX+1)
383 error("Constant too large for signed 64 bits!");
384 Upgradelval.SInt64Val = -Val;
385 return ESINT64VAL;
386 }
387{HexIntConstant} {
388 Upgradelval.UInt64Val = HexIntToVal(yytext+3);
389 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
390 }
391
392{EPInteger} {
393 uint64_t Val = atoull(yytext+1);
394 if ((unsigned)Val != Val)
395 error("Invalid value number (too large)!");
396 Upgradelval.UIntVal = unsigned(Val);
397 return UINTVAL;
398 }
399{ENInteger} {
400 uint64_t Val = atoull(yytext+2);
401 // +1: we have bigger negative range
402 if (Val > (uint64_t)INT32_MAX+1)
403 error("Constant too large for signed 32 bits!");
404 Upgradelval.SIntVal = (int)-Val;
405 return SINTVAL;
406 }
407
408{FPConstant} { Upgradelval.FPVal = atof(yytext); return FPVAL; }
409{HexFPConstant} { Upgradelval.FPVal = HexToFP(yytext); return FPVAL; }
410
411<<EOF>> {
Reid Spencere7c3c602006-11-30 06:36:44 +0000412 /* Make sure to free the internal buffers for flex when we are
413 * done reading our input!
414 */
415 yy_delete_buffer(YY_CURRENT_BUFFER);
416 return EOF;
417 }
418
419[ \r\t\n] { /* Ignore whitespace */ }
420. { return yytext[0]; }
421
422%%