blob: 93b84be44131885f1ba0fc7bc5d9e464eb3fa383 [file] [log] [blame]
Chris Lattner32eecb02006-02-14 05:14:46 +00001/*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the flex scanner for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===*/
13
14%option prefix="llvmAsm"
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="Lexer.cpp"
23%option ecs
24%option noreject
25%option noyymore
26
27%{
28#include "ParserInternals.h"
29#include "llvm/Module.h"
30#include <list>
31#include "llvmAsmParser.h"
32#include <cctype>
33#include <cstdlib>
34
35void set_scan_file(FILE * F){
36 yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
37}
38void set_scan_string (const char * str) {
39 yy_scan_string (str);
40}
41
Reid Spencer3ed469c2006-11-02 20:25:50 +000042// Construct a token value for a non-obsolete token
Chris Lattner32eecb02006-02-14 05:14:46 +000043#define RET_TOK(type, Enum, sym) \
Reid Spencera132e042006-12-03 05:46:11 +000044 llvmAsmlval.type = Instruction::Enum; \
45 return sym
46
Reid Spencer3ed469c2006-11-02 20:25:50 +000047// Construct a token value for an obsolete token
Reid Spencera132e042006-12-03 05:46:11 +000048#define RET_TY(CTYPE, SYM) \
49 llvmAsmlval.PrimType = CTYPE;\
Reid Spencer481169e2006-12-01 00:33:46 +000050 return SYM
Chris Lattner32eecb02006-02-14 05:14:46 +000051
52namespace llvm {
53
54// TODO: All of the static identifiers are figured out by the lexer,
55// these should be hashed to reduce the lexer size
56
57
58// atoull - Convert an ascii string of decimal digits into the unsigned long
59// long representation... this does not have to do input error checking,
60// because we know that the input will be matched by a suitable regex...
61//
62static uint64_t atoull(const char *Buffer) {
63 uint64_t Result = 0;
64 for (; *Buffer; Buffer++) {
65 uint64_t OldRes = Result;
66 Result *= 10;
67 Result += *Buffer-'0';
68 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000069 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000070 }
71 return Result;
72}
73
74static uint64_t HexIntToVal(const char *Buffer) {
75 uint64_t Result = 0;
76 for (; *Buffer; ++Buffer) {
77 uint64_t OldRes = Result;
78 Result *= 16;
79 char C = *Buffer;
80 if (C >= '0' && C <= '9')
81 Result += C-'0';
82 else if (C >= 'A' && C <= 'F')
83 Result += C-'A'+10;
84 else if (C >= 'a' && C <= 'f')
85 Result += C-'a'+10;
86
87 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000088 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000089 }
90 return Result;
91}
92
93
94// HexToFP - Convert the ascii string in hexidecimal format to the floating
95// point representation of it.
96//
97static double HexToFP(const char *Buffer) {
98 // Behave nicely in the face of C TBAA rules... see:
99 // http://www.nullstone.com/htmls/category/aliastyp.htm
100 union {
101 uint64_t UI;
102 double FP;
103 } UIntToFP;
104 UIntToFP.UI = HexIntToVal(Buffer);
105
106 assert(sizeof(double) == sizeof(uint64_t) &&
107 "Data sizes incompatible on this target!");
108 return UIntToFP.FP; // Cast Hex constant to double
109}
110
111
112// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
113// appropriate character. If AllowNull is set to false, a \00 value will cause
114// an exception to be thrown.
115//
116// If AllowNull is set to true, the return value of the function points to the
117// last character of the string in memory.
118//
119char *UnEscapeLexed(char *Buffer, bool AllowNull) {
120 char *BOut = Buffer;
121 for (char *BIn = Buffer; *BIn; ) {
122 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
123 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
124 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
125 if (!AllowNull && !*BOut)
Reid Spencer61c83e02006-08-18 08:43:06 +0000126 GenerateError("String literal cannot accept \\00 escape!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000127
128 BIn[3] = Tmp; // Restore character
129 BIn += 3; // Skip over handled chars
130 ++BOut;
131 } else {
132 *BOut++ = *BIn++;
133 }
134 }
135
136 return BOut;
137}
138
139} // End llvm namespace
140
141using namespace llvm;
142
143#define YY_NEVER_INTERACTIVE 1
144%}
145
146
147
148/* Comments start with a ; and go till end of line */
149Comment ;.*
150
151/* Variable(Value) identifiers start with a % sign */
152VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
153
154/* Label identifiers end with a colon */
155Label [-a-zA-Z$._0-9]+:
156QuoteLabel \"[^\"]+\":
157
158/* Quoted names can contain any character except " and \ */
159StringConstant \"[^\"]*\"
160
161
162/* [PN]Integer: match positive and negative literal integer values that
163 * are preceeded by a '%' character. These represent unnamed variable slots.
164 */
165EPInteger %[0-9]+
166ENInteger %-[0-9]+
167
168
169/* E[PN]Integer: match positive and negative literal integer values */
170PInteger [0-9]+
171NInteger -[0-9]+
172
173/* FPConstant - A Floating point constant.
174 */
175FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
176
177/* HexFPConstant - Floating point constant represented in IEEE format as a
178 * hexadecimal number for when exponential notation is not precise enough.
179 */
180HexFPConstant 0x[0-9A-Fa-f]+
181
182/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
183 * it to deal with 64 bit numbers.
184 */
185HexIntConstant [us]0x[0-9A-Fa-f]+
186%%
187
188{Comment} { /* Ignore comments for now */ }
189
190begin { return BEGINTOK; }
191end { return ENDTOK; }
192true { return TRUETOK; }
193false { return FALSETOK; }
194declare { return DECLARE; }
195global { return GLOBAL; }
196constant { return CONSTANT; }
197internal { return INTERNAL; }
198linkonce { return LINKONCE; }
199weak { return WEAK; }
200appending { return APPENDING; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000201dllimport { return DLLIMPORT; }
202dllexport { return DLLEXPORT; }
203extern_weak { return EXTERN_WEAK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000204external { return EXTERNAL; }
205implementation { return IMPLEMENTATION; }
206zeroinitializer { return ZEROINITIALIZER; }
207\.\.\. { return DOTDOTDOT; }
208undef { return UNDEF; }
209null { return NULL_TOK; }
210to { return TO; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000211tail { return TAIL; }
212target { return TARGET; }
213triple { return TRIPLE; }
214deplibs { return DEPLIBS; }
215endian { return ENDIAN; }
216pointersize { return POINTERSIZE; }
Chris Lattner1ae022f2006-10-22 06:08:13 +0000217datalayout { return DATALAYOUT; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000218little { return LITTLE; }
219big { return BIG; }
220volatile { return VOLATILE; }
221align { return ALIGN; }
222section { return SECTION; }
223module { return MODULE; }
224asm { return ASM_TOK; }
225sideeffect { return SIDEEFFECT; }
226
227cc { return CC_TOK; }
228ccc { return CCC_TOK; }
Chris Lattner75466192006-05-19 21:28:53 +0000229csretcc { return CSRETCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000230fastcc { return FASTCC_TOK; }
231coldcc { return COLDCC_TOK; }
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000232x86_stdcallcc { return X86_STDCALLCC_TOK; }
233x86_fastcallcc { return X86_FASTCALLCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000234
Reid Spencera132e042006-12-03 05:46:11 +0000235void { RET_TY(Type::VoidTy, VOID); }
236bool { RET_TY(Type::BoolTy, BOOL); }
237sbyte { RET_TY(Type::SByteTy, SBYTE); }
238ubyte { RET_TY(Type::UByteTy, UBYTE); }
239short { RET_TY(Type::ShortTy, SHORT); }
240ushort { RET_TY(Type::UShortTy,USHORT);}
241int { RET_TY(Type::IntTy, INT); }
242uint { RET_TY(Type::UIntTy, UINT); }
243long { RET_TY(Type::LongTy, LONG); }
244ulong { RET_TY(Type::ULongTy, ULONG); }
245float { RET_TY(Type::FloatTy, FLOAT); }
246double { RET_TY(Type::DoubleTy,DOUBLE);}
247label { RET_TY(Type::LabelTy, LABEL); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000248type { return TYPE; }
249opaque { return OPAQUE; }
250
251add { RET_TOK(BinaryOpVal, Add, ADD); }
252sub { RET_TOK(BinaryOpVal, Sub, SUB); }
253mul { RET_TOK(BinaryOpVal, Mul, MUL); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000254udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
255sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
256fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000257urem { RET_TOK(BinaryOpVal, URem, UREM); }
258srem { RET_TOK(BinaryOpVal, SRem, SREM); }
259frem { RET_TOK(BinaryOpVal, FRem, FREM); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000260and { RET_TOK(BinaryOpVal, And, AND); }
261or { RET_TOK(BinaryOpVal, Or , OR ); }
262xor { RET_TOK(BinaryOpVal, Xor, XOR); }
263setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
264seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
265setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
266setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
267setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
268setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
Reid Spencera132e042006-12-03 05:46:11 +0000269icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
270fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
Reid Spencer6e18b7d2006-12-03 06:59:29 +0000271eq { return EQ; }
272ne { return NE; }
273slt { return SLT; }
274sgt { return SGT; }
275sle { return SLE; }
276sge { return SGE; }
277ult { return ULT; }
278ugt { return UGT; }
279ule { return ULE; }
280uge { return UGE; }
281oeq { return OEQ; }
282one { return ONE; }
283olt { return OLT; }
284ogt { return OGT; }
285ole { return OLE; }
286oge { return OGE; }
287ord { return ORD; }
288uno { return UNO; }
289ueq { return UEQ; }
290une { return UNE; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000291
292phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
293call { RET_TOK(OtherOpVal, Call, CALL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000294trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
295zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
296sext { RET_TOK(CastOpVal, SExt, SEXT); }
297fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
298fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
299uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
300sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
301fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
302fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
303inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
304ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
305bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000306select { RET_TOK(OtherOpVal, Select, SELECT); }
307shl { RET_TOK(OtherOpVal, Shl, SHL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000308lshr { RET_TOK(OtherOpVal, LShr, LSHR); }
309ashr { RET_TOK(OtherOpVal, AShr, ASHR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000310va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
311ret { RET_TOK(TermOpVal, Ret, RET); }
312br { RET_TOK(TermOpVal, Br, BR); }
313switch { RET_TOK(TermOpVal, Switch, SWITCH); }
314invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
315unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
316unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
317
318malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
319alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
320free { RET_TOK(MemOpVal, Free, FREE); }
321load { RET_TOK(MemOpVal, Load, LOAD); }
322store { RET_TOK(MemOpVal, Store, STORE); }
323getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
324
325extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
326insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
Chris Lattnerd5efe842006-04-08 01:18:56 +0000327shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000328
329
330{VarID} {
331 UnEscapeLexed(yytext+1);
332 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
333 return VAR_ID;
334 }
335{Label} {
336 yytext[strlen(yytext)-1] = 0; // nuke colon
337 UnEscapeLexed(yytext);
338 llvmAsmlval.StrVal = strdup(yytext);
339 return LABELSTR;
340 }
341{QuoteLabel} {
342 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
343 UnEscapeLexed(yytext+1);
344 llvmAsmlval.StrVal = strdup(yytext+1);
345 return LABELSTR;
346 }
347
348{StringConstant} { // Note that we cannot unescape a string constant here! The
349 // string constant might contain a \00 which would not be
350 // understood by the string stuff. It is valid to make a
351 // [sbyte] c"Hello World\00" constant, for example.
352 //
353 yytext[strlen(yytext)-1] = 0; // nuke end quote
354 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
355 return STRINGCONSTANT;
356 }
357
358
359{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
360{NInteger} {
361 uint64_t Val = atoull(yytext+1);
362 // +1: we have bigger negative range
363 if (Val > (uint64_t)INT64_MAX+1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000364 GenerateError("Constant too large for signed 64 bits!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000365 llvmAsmlval.SInt64Val = -Val;
366 return ESINT64VAL;
367 }
368{HexIntConstant} {
369 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
370 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
371 }
372
373{EPInteger} {
374 uint64_t Val = atoull(yytext+1);
375 if ((unsigned)Val != Val)
Reid Spencer61c83e02006-08-18 08:43:06 +0000376 GenerateError("Invalid value number (too large)!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000377 llvmAsmlval.UIntVal = unsigned(Val);
378 return UINTVAL;
379 }
380{ENInteger} {
381 uint64_t Val = atoull(yytext+2);
382 // +1: we have bigger negative range
383 if (Val > (uint64_t)INT32_MAX+1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000384 GenerateError("Constant too large for signed 32 bits!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000385 llvmAsmlval.SIntVal = (int)-Val;
386 return SINTVAL;
387 }
388
389{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
390{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
391
392<<EOF>> {
393 /* Make sure to free the internal buffers for flex when we are
394 * done reading our input!
395 */
396 yy_delete_buffer(YY_CURRENT_BUFFER);
397 return EOF;
398 }
399
400[ \r\t\n] { /* Ignore whitespace */ }
401. { return yytext[0]; }
402
403%%