blob: 2218aa5164b8886c3d81cd2159c82d765fadb730 [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 @\"[^\"]*\"
Reid Spencered951ea2007-05-19 07:22:10 +0000164PctStringConstant %\"[^\"]*\"
Reid Spencer41dff5e2007-01-26 08:05:27 +0000165
166/* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
167LocalVarID %[0-9]+
168GlobalVarID @[0-9]+
Chris Lattner32eecb02006-02-14 05:14:46 +0000169
Reid Spencer41dff5e2007-01-26 08:05:27 +0000170/* Integer types are specified with i and a bitwidth */
Reid Spencer4db20632007-01-12 07:28:27 +0000171IntegerType i[0-9]+
Reid Spencera54b7cb2007-01-12 07:05:14 +0000172
Reid Spencer41dff5e2007-01-26 08:05:27 +0000173/* E[PN]Integer: match positive and negative literal integer values. */
Chris Lattner32eecb02006-02-14 05:14:46 +0000174PInteger [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]+
Reid Spencer38c91a92007-02-28 02:24:54 +0000190
Chris Lattner32eecb02006-02-14 05:14:46 +0000191%%
192
193{Comment} { /* Ignore comments for now */ }
194
195begin { return BEGINTOK; }
196end { return ENDTOK; }
197true { return TRUETOK; }
198false { return FALSETOK; }
199declare { return DECLARE; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000200define { return DEFINE; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000201global { return GLOBAL; }
202constant { return CONSTANT; }
203internal { return INTERNAL; }
204linkonce { return LINKONCE; }
205weak { return WEAK; }
206appending { return APPENDING; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000207dllimport { return DLLIMPORT; }
208dllexport { return DLLEXPORT; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000209hidden { return HIDDEN; }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000210protected { return PROTECTED; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000211extern_weak { return EXTERN_WEAK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000212external { return EXTERNAL; }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000213thread_local { return THREAD_LOCAL; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000214zeroinitializer { return ZEROINITIALIZER; }
215\.\.\. { return DOTDOTDOT; }
216undef { return UNDEF; }
217null { return NULL_TOK; }
218to { return TO; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000219tail { return TAIL; }
220target { return TARGET; }
221triple { return TRIPLE; }
222deplibs { return DEPLIBS; }
Chris Lattner1ae022f2006-10-22 06:08:13 +0000223datalayout { return DATALAYOUT; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000224volatile { return VOLATILE; }
225align { return ALIGN; }
226section { return SECTION; }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +0000227alias { return ALIAS; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000228module { return MODULE; }
229asm { return ASM_TOK; }
230sideeffect { return SIDEEFFECT; }
231
232cc { return CC_TOK; }
233ccc { return CCC_TOK; }
234fastcc { 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 Spencer832254e2007-02-02 02:16:23 +0000239inreg { return INREG; }
240sret { return SRET; }
Reid Spencer67d8ed92007-03-22 02:14:08 +0000241nounwind { return NOUNWIND; }
242noreturn { return NORETURN; }
Reid Spencer832254e2007-02-02 02:16:23 +0000243
Reid Spencera132e042006-12-03 05:46:11 +0000244void { RET_TY(Type::VoidTy, VOID); }
Reid Spencera132e042006-12-03 05:46:11 +0000245float { 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; }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000250{IntegerType} { uint64_t NumBits = atoull(yytext+1);
251 if (NumBits < IntegerType::MIN_INT_BITS ||
252 NumBits > IntegerType::MAX_INT_BITS)
253 GenerateError("Bitwidth for integer type out of range!");
254 const Type* Ty = IntegerType::get(NumBits);
255 RET_TY(Ty, INTTYPE);
256 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000257
258add { RET_TOK(BinaryOpVal, Add, ADD); }
259sub { RET_TOK(BinaryOpVal, Sub, SUB); }
260mul { RET_TOK(BinaryOpVal, Mul, MUL); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000261udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
262sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
263fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000264urem { RET_TOK(BinaryOpVal, URem, UREM); }
265srem { RET_TOK(BinaryOpVal, SRem, SREM); }
266frem { RET_TOK(BinaryOpVal, FRem, FREM); }
Reid Spencer832254e2007-02-02 02:16:23 +0000267shl { RET_TOK(BinaryOpVal, Shl, SHL); }
268lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
269ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000270and { RET_TOK(BinaryOpVal, And, AND); }
271or { RET_TOK(BinaryOpVal, Or , OR ); }
272xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Reid Spencera132e042006-12-03 05:46:11 +0000273icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
274fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
Reid Spencer832254e2007-02-02 02:16:23 +0000275
Reid Spencer6e18b7d2006-12-03 06:59:29 +0000276eq { return EQ; }
277ne { return NE; }
278slt { return SLT; }
279sgt { return SGT; }
280sle { return SLE; }
281sge { return SGE; }
282ult { return ULT; }
283ugt { return UGT; }
284ule { return ULE; }
285uge { return UGE; }
286oeq { return OEQ; }
287one { return ONE; }
288olt { return OLT; }
289ogt { return OGT; }
290ole { return OLE; }
291oge { return OGE; }
292ord { return ORD; }
293uno { return UNO; }
294ueq { return UEQ; }
295une { return UNE; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000296
297phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
298call { RET_TOK(OtherOpVal, Call, CALL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000299trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
300zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
301sext { RET_TOK(CastOpVal, SExt, SEXT); }
302fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
303fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
304uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
305sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
306fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
307fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
308inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
309ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
310bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000311select { RET_TOK(OtherOpVal, Select, SELECT); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000312va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
313ret { RET_TOK(TermOpVal, Ret, RET); }
314br { RET_TOK(TermOpVal, Br, BR); }
315switch { RET_TOK(TermOpVal, Switch, SWITCH); }
316invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
317unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
318unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
319
320malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
321alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
322free { RET_TOK(MemOpVal, Free, FREE); }
323load { RET_TOK(MemOpVal, Load, LOAD); }
324store { RET_TOK(MemOpVal, Store, STORE); }
325getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
326
327extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
328insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
Chris Lattnerd5efe842006-04-08 01:18:56 +0000329shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000330
331
Reid Spencer41dff5e2007-01-26 08:05:27 +0000332{LocalVarName} {
Chris Lattner32eecb02006-02-14 05:14:46 +0000333 UnEscapeLexed(yytext+1);
334 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
Reid Spencer41dff5e2007-01-26 08:05:27 +0000335 return LOCALVAR;
336 }
337{GlobalVarName} {
338 UnEscapeLexed(yytext+1);
339 llvmAsmlval.StrVal = strdup(yytext+1); // Skip @
340 return GLOBALVAR;
Chris Lattner32eecb02006-02-14 05:14:46 +0000341 }
342{Label} {
343 yytext[strlen(yytext)-1] = 0; // nuke colon
344 UnEscapeLexed(yytext);
345 llvmAsmlval.StrVal = strdup(yytext);
346 return LABELSTR;
347 }
348{QuoteLabel} {
349 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
350 UnEscapeLexed(yytext+1);
351 llvmAsmlval.StrVal = strdup(yytext+1);
352 return LABELSTR;
353 }
354
355{StringConstant} { // Note that we cannot unescape a string constant here! The
356 // string constant might contain a \00 which would not be
357 // understood by the string stuff. It is valid to make a
358 // [sbyte] c"Hello World\00" constant, for example.
359 //
360 yytext[strlen(yytext)-1] = 0; // nuke end quote
361 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
362 return STRINGCONSTANT;
363 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000364{AtStringConstant} {
365 yytext[strlen(yytext)-1] = 0; // nuke end quote
366 llvmAsmlval.StrVal = strdup(yytext+2); // Nuke @, quote
367 return ATSTRINGCONSTANT;
368 }
369
Reid Spencered951ea2007-05-19 07:22:10 +0000370{PctStringConstant} {
371 yytext[strlen(yytext)-1] = 0; // nuke end quote
372 llvmAsmlval.StrVal = strdup(yytext+2); // Nuke @, quote
373 return PCTSTRINGCONSTANT;
374 }
Reid Spencer38c91a92007-02-28 02:24:54 +0000375{PInteger} { int len = strlen(yytext);
376 uint32_t numBits = ((len * 64) / 19) + 1;
377 APInt Tmp(numBits, yytext, len, 10);
378 uint32_t activeBits = Tmp.getActiveBits();
379 if (activeBits > 0 && activeBits < numBits)
380 Tmp.trunc(activeBits);
381 if (Tmp.getBitWidth() > 64) {
382 llvmAsmlval.APIntVal = new APInt(Tmp);
383 return EUAPINTVAL;
384 } else {
385 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
386 return EUINT64VAL;
387 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000388 }
Reid Spencer38c91a92007-02-28 02:24:54 +0000389{NInteger} { int len = strlen(yytext);
Reid Spencerafc37822007-03-09 21:19:09 +0000390 uint32_t numBits = (((len-1) * 64) / 19) + 2;
Reid Spencer38c91a92007-02-28 02:24:54 +0000391 APInt Tmp(numBits, yytext, len, 10);
392 uint32_t minBits = Tmp.getMinSignedBits();
393 if (minBits > 0 && minBits < numBits)
394 Tmp.trunc(minBits);
395 if (Tmp.getBitWidth() > 64) {
396 llvmAsmlval.APIntVal = new APInt(Tmp);
397 return ESAPINTVAL;
398 } else {
399 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
400 return ESINT64VAL;
401 }
402 }
403
404{HexIntConstant} { int len = strlen(yytext+3) - 3;
405 uint32_t bits = len * 4;
406 APInt Tmp(bits, yytext+3, len, 16);
407 uint32_t activeBits = Tmp.getActiveBits();
408 if (activeBits > 0 && activeBits < bits)
409 Tmp.trunc(activeBits);
410 if (Tmp.getBitWidth() > 64) {
411 llvmAsmlval.APIntVal = new APInt(Tmp);
412 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
413 } else if (yytext[0] == 's') {
414 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
415 return ESINT64VAL;
416 } else {
417 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
418 return EUINT64VAL;
419 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000420 }
421
Reid Spencer41dff5e2007-01-26 08:05:27 +0000422{LocalVarID} {
Chris Lattner32eecb02006-02-14 05:14:46 +0000423 uint64_t Val = atoull(yytext+1);
424 if ((unsigned)Val != Val)
Reid Spencer61c83e02006-08-18 08:43:06 +0000425 GenerateError("Invalid value number (too large)!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000426 llvmAsmlval.UIntVal = unsigned(Val);
Reid Spencer41dff5e2007-01-26 08:05:27 +0000427 return LOCALVAL_ID;
Chris Lattner32eecb02006-02-14 05:14:46 +0000428 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000429{GlobalVarID} {
430 uint64_t Val = atoull(yytext+1);
431 if ((unsigned)Val != Val)
432 GenerateError("Invalid value number (too large)!");
433 llvmAsmlval.UIntVal = unsigned(Val);
434 return GLOBALVAL_ID;
Chris Lattner32eecb02006-02-14 05:14:46 +0000435 }
436
437{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
438{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
439
440<<EOF>> {
441 /* Make sure to free the internal buffers for flex when we are
442 * done reading our input!
443 */
444 yy_delete_buffer(YY_CURRENT_BUFFER);
445 return EOF;
446 }
447
448[ \r\t\n] { /* Ignore whitespace */ }
449. { return yytext[0]; }
450
451%%