blob: 382ce24462a732dc6a9964faca8eda6882c875dc [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
Reid Spencer41dff5e2007-01-26 08:05:27 +0000151/* Local Values and Type identifiers start with a % sign */
152LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
153
154/* Global Value identifiers start with an @ sign */
155GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner32eecb02006-02-14 05:14:46 +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 \ */
162StringConstant \"[^\"]*\"
Reid Spencer41dff5e2007-01-26 08:05:27 +0000163AtStringConstant @\"[^\"]*\"
164
165/* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
166LocalVarID %[0-9]+
167GlobalVarID @[0-9]+
Chris Lattner32eecb02006-02-14 05:14:46 +0000168
Reid Spencer41dff5e2007-01-26 08:05:27 +0000169/* Integer types are specified with i and a bitwidth */
Reid Spencer4db20632007-01-12 07:28:27 +0000170IntegerType i[0-9]+
Reid Spencera54b7cb2007-01-12 07:05:14 +0000171
Reid Spencer41dff5e2007-01-26 08:05:27 +0000172/* E[PN]Integer: match positive and negative literal integer values. */
Chris Lattner32eecb02006-02-14 05:14:46 +0000173PInteger [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]+
Reid Spencer38c91a92007-02-28 02:24:54 +0000189
Chris Lattner32eecb02006-02-14 05:14:46 +0000190%%
191
192{Comment} { /* Ignore comments for now */ }
193
194begin { return BEGINTOK; }
195end { return ENDTOK; }
196true { return TRUETOK; }
197false { return FALSETOK; }
198declare { return DECLARE; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000199define { return DEFINE; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000200global { return GLOBAL; }
201constant { return CONSTANT; }
202internal { return INTERNAL; }
203linkonce { return LINKONCE; }
204weak { return WEAK; }
205appending { return APPENDING; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000206dllimport { return DLLIMPORT; }
207dllexport { return DLLEXPORT; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000208hidden { return HIDDEN; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000209extern_weak { return EXTERN_WEAK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000210external { return EXTERNAL; }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000211thread_local { return THREAD_LOCAL; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000212zeroinitializer { return ZEROINITIALIZER; }
213\.\.\. { return DOTDOTDOT; }
214undef { return UNDEF; }
215null { return NULL_TOK; }
216to { return TO; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000217tail { return TAIL; }
218target { return TARGET; }
219triple { return TRIPLE; }
220deplibs { return DEPLIBS; }
Chris Lattner1ae022f2006-10-22 06:08:13 +0000221datalayout { return DATALAYOUT; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000222volatile { return VOLATILE; }
223align { return ALIGN; }
224section { return SECTION; }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +0000225alias { return ALIAS; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000226module { return MODULE; }
227asm { return ASM_TOK; }
228sideeffect { return SIDEEFFECT; }
229
230cc { return CC_TOK; }
231ccc { return CCC_TOK; }
232fastcc { return FASTCC_TOK; }
233coldcc { return COLDCC_TOK; }
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000234x86_stdcallcc { return X86_STDCALLCC_TOK; }
235x86_fastcallcc { return X86_FASTCALLCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000236
Reid Spencer832254e2007-02-02 02:16:23 +0000237inreg { return INREG; }
238sret { return SRET; }
Reid Spencer67d8ed92007-03-22 02:14:08 +0000239nounwind { return NOUNWIND; }
240noreturn { return NORETURN; }
Reid Spencer832254e2007-02-02 02:16:23 +0000241
Reid Spencera132e042006-12-03 05:46:11 +0000242void { RET_TY(Type::VoidTy, VOID); }
Reid Spencera132e042006-12-03 05:46:11 +0000243float { RET_TY(Type::FloatTy, FLOAT); }
244double { RET_TY(Type::DoubleTy,DOUBLE);}
245label { RET_TY(Type::LabelTy, LABEL); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000246type { return TYPE; }
247opaque { return OPAQUE; }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000248{IntegerType} { uint64_t NumBits = atoull(yytext+1);
249 if (NumBits < IntegerType::MIN_INT_BITS ||
250 NumBits > IntegerType::MAX_INT_BITS)
251 GenerateError("Bitwidth for integer type out of range!");
252 const Type* Ty = IntegerType::get(NumBits);
253 RET_TY(Ty, INTTYPE);
254 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000255
256add { RET_TOK(BinaryOpVal, Add, ADD); }
257sub { RET_TOK(BinaryOpVal, Sub, SUB); }
258mul { RET_TOK(BinaryOpVal, Mul, MUL); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000259udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
260sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
261fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000262urem { RET_TOK(BinaryOpVal, URem, UREM); }
263srem { RET_TOK(BinaryOpVal, SRem, SREM); }
264frem { RET_TOK(BinaryOpVal, FRem, FREM); }
Reid Spencer832254e2007-02-02 02:16:23 +0000265shl { RET_TOK(BinaryOpVal, Shl, SHL); }
266lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
267ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000268and { RET_TOK(BinaryOpVal, And, AND); }
269or { RET_TOK(BinaryOpVal, Or , OR ); }
270xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Reid Spencera132e042006-12-03 05:46:11 +0000271icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
272fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
Reid Spencer832254e2007-02-02 02:16:23 +0000273
Reid Spencer6e18b7d2006-12-03 06:59:29 +0000274eq { return EQ; }
275ne { return NE; }
276slt { return SLT; }
277sgt { return SGT; }
278sle { return SLE; }
279sge { return SGE; }
280ult { return ULT; }
281ugt { return UGT; }
282ule { return ULE; }
283uge { return UGE; }
284oeq { return OEQ; }
285one { return ONE; }
286olt { return OLT; }
287ogt { return OGT; }
288ole { return OLE; }
289oge { return OGE; }
290ord { return ORD; }
291uno { return UNO; }
292ueq { return UEQ; }
293une { return UNE; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000294
295phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
296call { RET_TOK(OtherOpVal, Call, CALL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000297trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
298zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
299sext { RET_TOK(CastOpVal, SExt, SEXT); }
300fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
301fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
302uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
303sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
304fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
305fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
306inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
307ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
308bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000309select { RET_TOK(OtherOpVal, Select, SELECT); }
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
Reid Spencer41dff5e2007-01-26 08:05:27 +0000330{LocalVarName} {
Chris Lattner32eecb02006-02-14 05:14:46 +0000331 UnEscapeLexed(yytext+1);
332 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
Reid Spencer41dff5e2007-01-26 08:05:27 +0000333 return LOCALVAR;
334 }
335{GlobalVarName} {
336 UnEscapeLexed(yytext+1);
337 llvmAsmlval.StrVal = strdup(yytext+1); // Skip @
338 return GLOBALVAR;
Chris Lattner32eecb02006-02-14 05:14:46 +0000339 }
340{Label} {
341 yytext[strlen(yytext)-1] = 0; // nuke colon
342 UnEscapeLexed(yytext);
343 llvmAsmlval.StrVal = strdup(yytext);
344 return LABELSTR;
345 }
346{QuoteLabel} {
347 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
348 UnEscapeLexed(yytext+1);
349 llvmAsmlval.StrVal = strdup(yytext+1);
350 return LABELSTR;
351 }
352
353{StringConstant} { // Note that we cannot unescape a string constant here! The
354 // string constant might contain a \00 which would not be
355 // understood by the string stuff. It is valid to make a
356 // [sbyte] c"Hello World\00" constant, for example.
357 //
358 yytext[strlen(yytext)-1] = 0; // nuke end quote
359 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
360 return STRINGCONSTANT;
361 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000362{AtStringConstant} {
363 yytext[strlen(yytext)-1] = 0; // nuke end quote
364 llvmAsmlval.StrVal = strdup(yytext+2); // Nuke @, quote
365 return ATSTRINGCONSTANT;
366 }
367
Reid Spencer38c91a92007-02-28 02:24:54 +0000368{PInteger} { int len = strlen(yytext);
369 uint32_t numBits = ((len * 64) / 19) + 1;
370 APInt Tmp(numBits, yytext, len, 10);
371 uint32_t activeBits = Tmp.getActiveBits();
372 if (activeBits > 0 && activeBits < numBits)
373 Tmp.trunc(activeBits);
374 if (Tmp.getBitWidth() > 64) {
375 llvmAsmlval.APIntVal = new APInt(Tmp);
376 return EUAPINTVAL;
377 } else {
378 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
379 return EUINT64VAL;
380 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000381 }
Reid Spencer38c91a92007-02-28 02:24:54 +0000382{NInteger} { int len = strlen(yytext);
Reid Spencerafc37822007-03-09 21:19:09 +0000383 uint32_t numBits = (((len-1) * 64) / 19) + 2;
Reid Spencer38c91a92007-02-28 02:24:54 +0000384 APInt Tmp(numBits, yytext, len, 10);
385 uint32_t minBits = Tmp.getMinSignedBits();
386 if (minBits > 0 && minBits < numBits)
387 Tmp.trunc(minBits);
388 if (Tmp.getBitWidth() > 64) {
389 llvmAsmlval.APIntVal = new APInt(Tmp);
390 return ESAPINTVAL;
391 } else {
392 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
393 return ESINT64VAL;
394 }
395 }
396
397{HexIntConstant} { int len = strlen(yytext+3) - 3;
398 uint32_t bits = len * 4;
399 APInt Tmp(bits, yytext+3, len, 16);
400 uint32_t activeBits = Tmp.getActiveBits();
401 if (activeBits > 0 && activeBits < bits)
402 Tmp.trunc(activeBits);
403 if (Tmp.getBitWidth() > 64) {
404 llvmAsmlval.APIntVal = new APInt(Tmp);
405 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
406 } else if (yytext[0] == 's') {
407 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
408 return ESINT64VAL;
409 } else {
410 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
411 return EUINT64VAL;
412 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000413 }
414
Reid Spencer41dff5e2007-01-26 08:05:27 +0000415{LocalVarID} {
Chris Lattner32eecb02006-02-14 05:14:46 +0000416 uint64_t Val = atoull(yytext+1);
417 if ((unsigned)Val != Val)
Reid Spencer61c83e02006-08-18 08:43:06 +0000418 GenerateError("Invalid value number (too large)!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000419 llvmAsmlval.UIntVal = unsigned(Val);
Reid Spencer41dff5e2007-01-26 08:05:27 +0000420 return LOCALVAL_ID;
Chris Lattner32eecb02006-02-14 05:14:46 +0000421 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000422{GlobalVarID} {
423 uint64_t Val = atoull(yytext+1);
424 if ((unsigned)Val != Val)
425 GenerateError("Invalid value number (too large)!");
426 llvmAsmlval.UIntVal = unsigned(Val);
427 return GLOBALVAL_ID;
Chris Lattner32eecb02006-02-14 05:14:46 +0000428 }
429
430{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
431{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
432
433<<EOF>> {
434 /* Make sure to free the internal buffers for flex when we are
435 * done reading our input!
436 */
437 yy_delete_buffer(YY_CURRENT_BUFFER);
438 return EOF;
439 }
440
441[ \r\t\n] { /* Ignore whitespace */ }
442. { return yytext[0]; }
443
444%%