blob: 5e60654c84c68471114ad73db2df94db89b5019b [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
47#define RET_ENUM(type, Enum, sym) \
48 llvmAsmlval.type = Enum; \
Reid Spencer3ed469c2006-11-02 20:25:50 +000049 return sym
50
51// Construct a token value for an obsolete token
Reid Spencera132e042006-12-03 05:46:11 +000052#define RET_TY(CTYPE, SYM) \
53 llvmAsmlval.PrimType = CTYPE;\
Reid Spencer481169e2006-12-01 00:33:46 +000054 return SYM
Chris Lattner32eecb02006-02-14 05:14:46 +000055
56namespace llvm {
57
58// TODO: All of the static identifiers are figured out by the lexer,
59// these should be hashed to reduce the lexer size
60
61
62// atoull - Convert an ascii string of decimal digits into the unsigned long
63// long representation... this does not have to do input error checking,
64// because we know that the input will be matched by a suitable regex...
65//
66static uint64_t atoull(const char *Buffer) {
67 uint64_t Result = 0;
68 for (; *Buffer; Buffer++) {
69 uint64_t OldRes = Result;
70 Result *= 10;
71 Result += *Buffer-'0';
72 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000073 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000074 }
75 return Result;
76}
77
78static uint64_t HexIntToVal(const char *Buffer) {
79 uint64_t Result = 0;
80 for (; *Buffer; ++Buffer) {
81 uint64_t OldRes = Result;
82 Result *= 16;
83 char C = *Buffer;
84 if (C >= '0' && C <= '9')
85 Result += C-'0';
86 else if (C >= 'A' && C <= 'F')
87 Result += C-'A'+10;
88 else if (C >= 'a' && C <= 'f')
89 Result += C-'a'+10;
90
91 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000092 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000093 }
94 return Result;
95}
96
97
98// HexToFP - Convert the ascii string in hexidecimal format to the floating
99// point representation of it.
100//
101static double HexToFP(const char *Buffer) {
102 // Behave nicely in the face of C TBAA rules... see:
103 // http://www.nullstone.com/htmls/category/aliastyp.htm
104 union {
105 uint64_t UI;
106 double FP;
107 } UIntToFP;
108 UIntToFP.UI = HexIntToVal(Buffer);
109
110 assert(sizeof(double) == sizeof(uint64_t) &&
111 "Data sizes incompatible on this target!");
112 return UIntToFP.FP; // Cast Hex constant to double
113}
114
115
116// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
117// appropriate character. If AllowNull is set to false, a \00 value will cause
118// an exception to be thrown.
119//
120// If AllowNull is set to true, the return value of the function points to the
121// last character of the string in memory.
122//
123char *UnEscapeLexed(char *Buffer, bool AllowNull) {
124 char *BOut = Buffer;
125 for (char *BIn = Buffer; *BIn; ) {
126 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
127 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
128 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
129 if (!AllowNull && !*BOut)
Reid Spencer61c83e02006-08-18 08:43:06 +0000130 GenerateError("String literal cannot accept \\00 escape!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000131
132 BIn[3] = Tmp; // Restore character
133 BIn += 3; // Skip over handled chars
134 ++BOut;
135 } else {
136 *BOut++ = *BIn++;
137 }
138 }
139
140 return BOut;
141}
142
143} // End llvm namespace
144
145using namespace llvm;
146
147#define YY_NEVER_INTERACTIVE 1
148%}
149
150
151
152/* Comments start with a ; and go till end of line */
153Comment ;.*
154
155/* Variable(Value) identifiers start with a % sign */
156VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
157
158/* Label identifiers end with a colon */
159Label [-a-zA-Z$._0-9]+:
160QuoteLabel \"[^\"]+\":
161
162/* Quoted names can contain any character except " and \ */
163StringConstant \"[^\"]*\"
164
165
166/* [PN]Integer: match positive and negative literal integer values that
167 * are preceeded by a '%' character. These represent unnamed variable slots.
168 */
169EPInteger %[0-9]+
170ENInteger %-[0-9]+
171
172
173/* E[PN]Integer: match positive and negative literal integer values */
174PInteger [0-9]+
175NInteger -[0-9]+
176
177/* FPConstant - A Floating point constant.
178 */
179FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
180
181/* HexFPConstant - Floating point constant represented in IEEE format as a
182 * hexadecimal number for when exponential notation is not precise enough.
183 */
184HexFPConstant 0x[0-9A-Fa-f]+
185
186/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
187 * it to deal with 64 bit numbers.
188 */
189HexIntConstant [us]0x[0-9A-Fa-f]+
190%%
191
192{Comment} { /* Ignore comments for now */ }
193
194begin { return BEGINTOK; }
195end { return ENDTOK; }
196true { return TRUETOK; }
197false { return FALSETOK; }
198declare { return DECLARE; }
199global { return GLOBAL; }
200constant { return CONSTANT; }
201internal { return INTERNAL; }
202linkonce { return LINKONCE; }
203weak { return WEAK; }
204appending { return APPENDING; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000205dllimport { return DLLIMPORT; }
206dllexport { return DLLEXPORT; }
207extern_weak { return EXTERN_WEAK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000208external { return EXTERNAL; }
209implementation { return IMPLEMENTATION; }
210zeroinitializer { return ZEROINITIALIZER; }
211\.\.\. { return DOTDOTDOT; }
212undef { return UNDEF; }
213null { return NULL_TOK; }
214to { return TO; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000215tail { return TAIL; }
216target { return TARGET; }
217triple { return TRIPLE; }
218deplibs { return DEPLIBS; }
219endian { return ENDIAN; }
220pointersize { return POINTERSIZE; }
Chris Lattner1ae022f2006-10-22 06:08:13 +0000221datalayout { return DATALAYOUT; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000222little { return LITTLE; }
223big { return BIG; }
224volatile { return VOLATILE; }
225align { return ALIGN; }
226section { return SECTION; }
227module { return MODULE; }
228asm { return ASM_TOK; }
229sideeffect { return SIDEEFFECT; }
230
231cc { return CC_TOK; }
232ccc { return CCC_TOK; }
Chris Lattner75466192006-05-19 21:28:53 +0000233csretcc { return CSRETCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000234fastcc { return FASTCC_TOK; }
235coldcc { return COLDCC_TOK; }
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000236x86_stdcallcc { return X86_STDCALLCC_TOK; }
237x86_fastcallcc { return X86_FASTCALLCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000238
Reid Spencera132e042006-12-03 05:46:11 +0000239void { RET_TY(Type::VoidTy, VOID); }
240bool { RET_TY(Type::BoolTy, BOOL); }
241sbyte { RET_TY(Type::SByteTy, SBYTE); }
242ubyte { RET_TY(Type::UByteTy, UBYTE); }
243short { RET_TY(Type::ShortTy, SHORT); }
244ushort { RET_TY(Type::UShortTy,USHORT);}
245int { RET_TY(Type::IntTy, INT); }
246uint { RET_TY(Type::UIntTy, UINT); }
247long { RET_TY(Type::LongTy, LONG); }
248ulong { RET_TY(Type::ULongTy, ULONG); }
249float { RET_TY(Type::FloatTy, FLOAT); }
250double { RET_TY(Type::DoubleTy,DOUBLE);}
251label { RET_TY(Type::LabelTy, LABEL); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000252type { return TYPE; }
253opaque { return OPAQUE; }
254
255add { RET_TOK(BinaryOpVal, Add, ADD); }
256sub { RET_TOK(BinaryOpVal, Sub, SUB); }
257mul { RET_TOK(BinaryOpVal, Mul, MUL); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000258udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
259sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
260fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000261urem { RET_TOK(BinaryOpVal, URem, UREM); }
262srem { RET_TOK(BinaryOpVal, SRem, SREM); }
263frem { RET_TOK(BinaryOpVal, FRem, FREM); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000264and { RET_TOK(BinaryOpVal, And, AND); }
265or { RET_TOK(BinaryOpVal, Or , OR ); }
266xor { RET_TOK(BinaryOpVal, Xor, XOR); }
267setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
268seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
269setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
270setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
271setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
272setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
Reid Spencera132e042006-12-03 05:46:11 +0000273icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
274fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
275eq { RET_ENUM(IPredicate, ICmpInst::ICMP_EQ, EQ); }
276ne { RET_ENUM(IPredicate, ICmpInst::ICMP_NE, NE); }
277slt { RET_ENUM(IPredicate, ICmpInst::ICMP_SLT, SLT); }
278sgt { RET_ENUM(IPredicate, ICmpInst::ICMP_SGT, SGT); }
279sle { RET_ENUM(IPredicate, ICmpInst::ICMP_SLE, SLE); }
280sge { RET_ENUM(IPredicate, ICmpInst::ICMP_SGE, SGE); }
281ult { RET_ENUM(IPredicate, ICmpInst::ICMP_ULT, ULT); }
282ugt { RET_ENUM(IPredicate, ICmpInst::ICMP_UGT, UGT); }
283ule { RET_ENUM(IPredicate, ICmpInst::ICMP_ULE, ULE); }
284uge { RET_ENUM(IPredicate, ICmpInst::ICMP_UGE, UGE); }
285ordeq { RET_ENUM(FPredicate, FCmpInst::FCMP_OEQ, ORDEQ); }
286ordne { RET_ENUM(FPredicate, FCmpInst::FCMP_ONE, ORDNE); }
287ordlt { RET_ENUM(FPredicate, FCmpInst::FCMP_OLT, ORDLT); }
288ordgt { RET_ENUM(FPredicate, FCmpInst::FCMP_OGT, ORDGT); }
289ordle { RET_ENUM(FPredicate, FCmpInst::FCMP_OLE, ORDLE); }
290ordge { RET_ENUM(FPredicate, FCmpInst::FCMP_OGE, ORDGE); }
291ord { RET_ENUM(FPredicate, FCmpInst::FCMP_ORD, ORD); }
292uno { RET_ENUM(FPredicate, FCmpInst::FCMP_UNO, UNO); }
293unoeq { RET_ENUM(FPredicate, FCmpInst::FCMP_UEQ, UNOEQ); }
294unone { RET_ENUM(FPredicate, FCmpInst::FCMP_UNE, UNONE); }
295unolt { RET_ENUM(FPredicate, FCmpInst::FCMP_ULT, UNOLT); }
296unogt { RET_ENUM(FPredicate, FCmpInst::FCMP_UGT, UNOGT); }
297unole { RET_ENUM(FPredicate, FCmpInst::FCMP_ULE, UNOLE); }
298unoge { RET_ENUM(FPredicate, FCmpInst::FCMP_UGE, UNOGE); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000299
300phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
301call { RET_TOK(OtherOpVal, Call, CALL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000302trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
303zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
304sext { RET_TOK(CastOpVal, SExt, SEXT); }
305fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
306fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
307uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
308sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
309fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
310fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
311inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
312ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
313bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000314select { RET_TOK(OtherOpVal, Select, SELECT); }
315shl { RET_TOK(OtherOpVal, Shl, SHL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000316lshr { RET_TOK(OtherOpVal, LShr, LSHR); }
317ashr { RET_TOK(OtherOpVal, AShr, ASHR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000318va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
319ret { RET_TOK(TermOpVal, Ret, RET); }
320br { RET_TOK(TermOpVal, Br, BR); }
321switch { RET_TOK(TermOpVal, Switch, SWITCH); }
322invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
323unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
324unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
325
326malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
327alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
328free { RET_TOK(MemOpVal, Free, FREE); }
329load { RET_TOK(MemOpVal, Load, LOAD); }
330store { RET_TOK(MemOpVal, Store, STORE); }
331getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
332
333extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
334insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
Chris Lattnerd5efe842006-04-08 01:18:56 +0000335shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000336
337
338{VarID} {
339 UnEscapeLexed(yytext+1);
340 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
341 return VAR_ID;
342 }
343{Label} {
344 yytext[strlen(yytext)-1] = 0; // nuke colon
345 UnEscapeLexed(yytext);
346 llvmAsmlval.StrVal = strdup(yytext);
347 return LABELSTR;
348 }
349{QuoteLabel} {
350 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
351 UnEscapeLexed(yytext+1);
352 llvmAsmlval.StrVal = strdup(yytext+1);
353 return LABELSTR;
354 }
355
356{StringConstant} { // Note that we cannot unescape a string constant here! The
357 // string constant might contain a \00 which would not be
358 // understood by the string stuff. It is valid to make a
359 // [sbyte] c"Hello World\00" constant, for example.
360 //
361 yytext[strlen(yytext)-1] = 0; // nuke end quote
362 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
363 return STRINGCONSTANT;
364 }
365
366
367{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
368{NInteger} {
369 uint64_t Val = atoull(yytext+1);
370 // +1: we have bigger negative range
371 if (Val > (uint64_t)INT64_MAX+1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000372 GenerateError("Constant too large for signed 64 bits!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000373 llvmAsmlval.SInt64Val = -Val;
374 return ESINT64VAL;
375 }
376{HexIntConstant} {
377 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
378 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
379 }
380
381{EPInteger} {
382 uint64_t Val = atoull(yytext+1);
383 if ((unsigned)Val != Val)
Reid Spencer61c83e02006-08-18 08:43:06 +0000384 GenerateError("Invalid value number (too large)!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000385 llvmAsmlval.UIntVal = unsigned(Val);
386 return UINTVAL;
387 }
388{ENInteger} {
389 uint64_t Val = atoull(yytext+2);
390 // +1: we have bigger negative range
391 if (Val > (uint64_t)INT32_MAX+1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000392 GenerateError("Constant too large for signed 32 bits!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000393 llvmAsmlval.SIntVal = (int)-Val;
394 return SINTVAL;
395 }
396
397{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
398{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
399
400<<EOF>> {
401 /* Make sure to free the internal buffers for flex when we are
402 * done reading our input!
403 */
404 yy_delete_buffer(YY_CURRENT_BUFFER);
405 return EOF;
406 }
407
408[ \r\t\n] { /* Ignore whitespace */ }
409. { return yytext[0]; }
410
411%%