blob: 553ba8100e4ba2ebeded15e68f9022eeeadf3526 [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 Spencer3ed469c2006-11-02 20:25:50 +000044 llvmAsmlval.type.opcode = Instruction::Enum; \
45 llvmAsmlval.type.obsolete = false; \
46 return sym
47
48// Construct a token value for an obsolete token
49#define RET_TOK_OBSOLETE(type, Enum, sym) \
50 llvmAsmlval.type.opcode = Instruction::Enum; \
51 llvmAsmlval.type.obsolete = true; \
52 return sym
53
Reid Spencer3da59db2006-11-27 01:05:10 +000054// Construct a token value for an obsolete token
Reid Spencer481169e2006-12-01 00:33:46 +000055#define RET_TY(CTYPE, SIGN, SYM) \
56 llvmAsmlval.TypeVal.type = new PATypeHolder(CTYPE); \
57 llvmAsmlval.TypeVal.signedness = SIGN; \
58 return SYM
Chris Lattner32eecb02006-02-14 05:14:46 +000059
60namespace llvm {
61
62// TODO: All of the static identifiers are figured out by the lexer,
63// these should be hashed to reduce the lexer size
64
65
66// atoull - Convert an ascii string of decimal digits into the unsigned long
67// long representation... this does not have to do input error checking,
68// because we know that the input will be matched by a suitable regex...
69//
70static uint64_t atoull(const char *Buffer) {
71 uint64_t Result = 0;
72 for (; *Buffer; Buffer++) {
73 uint64_t OldRes = Result;
74 Result *= 10;
75 Result += *Buffer-'0';
76 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000077 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000078 }
79 return Result;
80}
81
82static uint64_t HexIntToVal(const char *Buffer) {
83 uint64_t Result = 0;
84 for (; *Buffer; ++Buffer) {
85 uint64_t OldRes = Result;
86 Result *= 16;
87 char C = *Buffer;
88 if (C >= '0' && C <= '9')
89 Result += C-'0';
90 else if (C >= 'A' && C <= 'F')
91 Result += C-'A'+10;
92 else if (C >= 'a' && C <= 'f')
93 Result += C-'a'+10;
94
95 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000096 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000097 }
98 return Result;
99}
100
101
102// HexToFP - Convert the ascii string in hexidecimal format to the floating
103// point representation of it.
104//
105static double HexToFP(const char *Buffer) {
106 // Behave nicely in the face of C TBAA rules... see:
107 // http://www.nullstone.com/htmls/category/aliastyp.htm
108 union {
109 uint64_t UI;
110 double FP;
111 } UIntToFP;
112 UIntToFP.UI = HexIntToVal(Buffer);
113
114 assert(sizeof(double) == sizeof(uint64_t) &&
115 "Data sizes incompatible on this target!");
116 return UIntToFP.FP; // Cast Hex constant to double
117}
118
119
120// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
121// appropriate character. If AllowNull is set to false, a \00 value will cause
122// an exception to be thrown.
123//
124// If AllowNull is set to true, the return value of the function points to the
125// last character of the string in memory.
126//
127char *UnEscapeLexed(char *Buffer, bool AllowNull) {
128 char *BOut = Buffer;
129 for (char *BIn = Buffer; *BIn; ) {
130 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
131 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
132 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
133 if (!AllowNull && !*BOut)
Reid Spencer61c83e02006-08-18 08:43:06 +0000134 GenerateError("String literal cannot accept \\00 escape!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000135
136 BIn[3] = Tmp; // Restore character
137 BIn += 3; // Skip over handled chars
138 ++BOut;
139 } else {
140 *BOut++ = *BIn++;
141 }
142 }
143
144 return BOut;
145}
146
147} // End llvm namespace
148
149using namespace llvm;
150
151#define YY_NEVER_INTERACTIVE 1
152%}
153
154
155
156/* Comments start with a ; and go till end of line */
157Comment ;.*
158
159/* Variable(Value) identifiers start with a % sign */
160VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
161
162/* Label identifiers end with a colon */
163Label [-a-zA-Z$._0-9]+:
164QuoteLabel \"[^\"]+\":
165
166/* Quoted names can contain any character except " and \ */
167StringConstant \"[^\"]*\"
168
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
198begin { 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; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000209dllimport { return DLLIMPORT; }
210dllexport { return DLLEXPORT; }
211extern_weak { return EXTERN_WEAK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000212uninitialized { 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 { RET_TOK(TermOpVal, Unwind, UNWIND); }
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; }
Chris Lattner1ae022f2006-10-22 06:08:13 +0000228datalayout { return DATALAYOUT; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000229little { return LITTLE; }
230big { return BIG; }
231volatile { return VOLATILE; }
232align { return ALIGN; }
233section { return SECTION; }
234module { return MODULE; }
235asm { return ASM_TOK; }
236sideeffect { return SIDEEFFECT; }
237
238cc { return CC_TOK; }
239ccc { return CCC_TOK; }
Chris Lattner75466192006-05-19 21:28:53 +0000240csretcc { return CSRETCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000241fastcc { return FASTCC_TOK; }
242coldcc { return COLDCC_TOK; }
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000243x86_stdcallcc { return X86_STDCALLCC_TOK; }
244x86_fastcallcc { return X86_FASTCALLCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000245
Reid Spencer481169e2006-12-01 00:33:46 +0000246void { RET_TY(Type::VoidTy, isSignless, VOID); }
247bool { RET_TY(Type::BoolTy, isSignless, BOOL); }
248sbyte { RET_TY(Type::SByteTy, isSigned, SBYTE); }
249ubyte { RET_TY(Type::UByteTy, isUnsigned, UBYTE); }
250short { RET_TY(Type::ShortTy, isSigned, SHORT); }
251ushort { RET_TY(Type::UShortTy,isUnsigned, USHORT);}
252int { RET_TY(Type::IntTy, isSigned, INT); }
253uint { RET_TY(Type::UIntTy, isUnsigned, UINT); }
254long { RET_TY(Type::LongTy, isSigned, LONG); }
255ulong { RET_TY(Type::ULongTy, isUnsigned, ULONG); }
256float { RET_TY(Type::FloatTy, isSignless, FLOAT); }
257double { RET_TY(Type::DoubleTy,isSignless, DOUBLE);}
258label { RET_TY(Type::LabelTy, isSignless, LABEL); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000259type { return TYPE; }
260opaque { return OPAQUE; }
261
262add { RET_TOK(BinaryOpVal, Add, ADD); }
263sub { RET_TOK(BinaryOpVal, Sub, SUB); }
264mul { RET_TOK(BinaryOpVal, Mul, MUL); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000265div { RET_TOK_OBSOLETE(BinaryOpVal, UDiv, UDIV); }
266udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
267sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
268fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
269rem { RET_TOK_OBSOLETE(BinaryOpVal, URem, UREM); }
270urem { RET_TOK(BinaryOpVal, URem, UREM); }
271srem { RET_TOK(BinaryOpVal, SRem, SREM); }
272frem { RET_TOK(BinaryOpVal, FRem, FREM); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000273and { RET_TOK(BinaryOpVal, And, AND); }
274or { RET_TOK(BinaryOpVal, Or , OR ); }
275xor { RET_TOK(BinaryOpVal, Xor, 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); }
282
283phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
284call { RET_TOK(OtherOpVal, Call, CALL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000285cast { RET_TOK_OBSOLETE(CastOpVal, Trunc, TRUNC); }
286trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
287zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
288sext { RET_TOK(CastOpVal, SExt, SEXT); }
289fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
290fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
291uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
292sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
293fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
294fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
295inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
296ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
297bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000298select { RET_TOK(OtherOpVal, Select, SELECT); }
299shl { RET_TOK(OtherOpVal, Shl, SHL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000300shr { RET_TOK_OBSOLETE(OtherOpVal, LShr, LSHR); }
301lshr { RET_TOK(OtherOpVal, LShr, LSHR); }
302ashr { RET_TOK(OtherOpVal, AShr, ASHR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000303vanext { return VANEXT_old; }
304vaarg { return VAARG_old; }
305va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
306ret { RET_TOK(TermOpVal, Ret, RET); }
307br { RET_TOK(TermOpVal, Br, BR); }
308switch { RET_TOK(TermOpVal, Switch, SWITCH); }
309invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
310unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
311unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
312
313malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
314alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
315free { RET_TOK(MemOpVal, Free, FREE); }
316load { RET_TOK(MemOpVal, Load, LOAD); }
317store { RET_TOK(MemOpVal, Store, STORE); }
318getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
319
320extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
321insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
Chris Lattnerd5efe842006-04-08 01:18:56 +0000322shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000323
324
325{VarID} {
326 UnEscapeLexed(yytext+1);
327 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
328 return VAR_ID;
329 }
330{Label} {
331 yytext[strlen(yytext)-1] = 0; // nuke colon
332 UnEscapeLexed(yytext);
333 llvmAsmlval.StrVal = strdup(yytext);
334 return LABELSTR;
335 }
336{QuoteLabel} {
337 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
338 UnEscapeLexed(yytext+1);
339 llvmAsmlval.StrVal = strdup(yytext+1);
340 return LABELSTR;
341 }
342
343{StringConstant} { // Note that we cannot unescape a string constant here! The
344 // string constant might contain a \00 which would not be
345 // understood by the string stuff. It is valid to make a
346 // [sbyte] c"Hello World\00" constant, for example.
347 //
348 yytext[strlen(yytext)-1] = 0; // nuke end quote
349 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
350 return STRINGCONSTANT;
351 }
352
353
354{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
355{NInteger} {
356 uint64_t Val = atoull(yytext+1);
357 // +1: we have bigger negative range
358 if (Val > (uint64_t)INT64_MAX+1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000359 GenerateError("Constant too large for signed 64 bits!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000360 llvmAsmlval.SInt64Val = -Val;
361 return ESINT64VAL;
362 }
363{HexIntConstant} {
364 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
365 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
366 }
367
368{EPInteger} {
369 uint64_t Val = atoull(yytext+1);
370 if ((unsigned)Val != Val)
Reid Spencer61c83e02006-08-18 08:43:06 +0000371 GenerateError("Invalid value number (too large)!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000372 llvmAsmlval.UIntVal = unsigned(Val);
373 return UINTVAL;
374 }
375{ENInteger} {
376 uint64_t Val = atoull(yytext+2);
377 // +1: we have bigger negative range
378 if (Val > (uint64_t)INT32_MAX+1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000379 GenerateError("Constant too large for signed 32 bits!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000380 llvmAsmlval.SIntVal = (int)-Val;
381 return SINTVAL;
382 }
383
384{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
385{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
386
387<<EOF>> {
388 /* Make sure to free the internal buffers for flex when we are
389 * done reading our input!
390 */
391 yy_delete_buffer(YY_CURRENT_BUFFER);
392 return EOF;
393 }
394
395[ \r\t\n] { /* Ignore whitespace */ }
396. { return yytext[0]; }
397
398%%