blob: ed1f6bf4570595dd3500afb995a23b46c5e1035b [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"
Chris Lattner8e008322007-05-22 06:47:55 +000030#include "llvm/Support/MathExtras.h"
Chris Lattner32eecb02006-02-14 05:14:46 +000031#include <list>
32#include "llvmAsmParser.h"
33#include <cctype>
34#include <cstdlib>
35
36void set_scan_file(FILE * F){
37 yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
38}
39void set_scan_string (const char * str) {
40 yy_scan_string (str);
41}
42
Reid Spencer3ed469c2006-11-02 20:25:50 +000043// Construct a token value for a non-obsolete token
Chris Lattner32eecb02006-02-14 05:14:46 +000044#define RET_TOK(type, Enum, sym) \
Reid Spencera132e042006-12-03 05:46:11 +000045 llvmAsmlval.type = Instruction::Enum; \
46 return sym
47
Reid Spencer3ed469c2006-11-02 20:25:50 +000048// Construct a token value for an obsolete token
Reid Spencera132e042006-12-03 05:46:11 +000049#define RET_TY(CTYPE, SYM) \
50 llvmAsmlval.PrimType = CTYPE;\
Reid Spencer481169e2006-12-01 00:33:46 +000051 return SYM
Chris Lattner32eecb02006-02-14 05:14:46 +000052
53namespace llvm {
54
55// TODO: All of the static identifiers are figured out by the lexer,
56// these should be hashed to reduce the lexer size
57
58
59// atoull - Convert an ascii string of decimal digits into the unsigned long
60// long representation... this does not have to do input error checking,
61// because we know that the input will be matched by a suitable regex...
62//
63static uint64_t atoull(const char *Buffer) {
64 uint64_t Result = 0;
65 for (; *Buffer; Buffer++) {
66 uint64_t OldRes = Result;
67 Result *= 10;
68 Result += *Buffer-'0';
69 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000070 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000071 }
72 return Result;
73}
74
75static uint64_t HexIntToVal(const char *Buffer) {
76 uint64_t Result = 0;
77 for (; *Buffer; ++Buffer) {
78 uint64_t OldRes = Result;
79 Result *= 16;
80 char C = *Buffer;
81 if (C >= '0' && C <= '9')
82 Result += C-'0';
83 else if (C >= 'A' && C <= 'F')
84 Result += C-'A'+10;
85 else if (C >= 'a' && C <= 'f')
86 Result += C-'a'+10;
87
88 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000089 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000090 }
91 return Result;
92}
93
94
95// HexToFP - Convert the ascii string in hexidecimal format to the floating
96// point representation of it.
97//
98static double HexToFP(const char *Buffer) {
Chris Lattner8e008322007-05-22 06:47:55 +000099 return BitsToDouble(HexIntToVal(Buffer)); // Cast Hex constant to double
Chris Lattner32eecb02006-02-14 05:14:46 +0000100}
101
102
103// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
104// appropriate character. If AllowNull is set to false, a \00 value will cause
105// an exception to be thrown.
106//
107// If AllowNull is set to true, the return value of the function points to the
108// last character of the string in memory.
109//
110char *UnEscapeLexed(char *Buffer, bool AllowNull) {
111 char *BOut = Buffer;
112 for (char *BIn = Buffer; *BIn; ) {
113 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
114 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
115 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
116 if (!AllowNull && !*BOut)
Reid Spencer61c83e02006-08-18 08:43:06 +0000117 GenerateError("String literal cannot accept \\00 escape!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000118
119 BIn[3] = Tmp; // Restore character
120 BIn += 3; // Skip over handled chars
121 ++BOut;
122 } else {
123 *BOut++ = *BIn++;
124 }
125 }
126
127 return BOut;
128}
129
130} // End llvm namespace
131
132using namespace llvm;
133
134#define YY_NEVER_INTERACTIVE 1
135%}
136
137
138
139/* Comments start with a ; and go till end of line */
140Comment ;.*
141
Reid Spencer41dff5e2007-01-26 08:05:27 +0000142/* Local Values and Type identifiers start with a % sign */
143LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
144
145/* Global Value identifiers start with an @ sign */
146GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner32eecb02006-02-14 05:14:46 +0000147
148/* Label identifiers end with a colon */
149Label [-a-zA-Z$._0-9]+:
150QuoteLabel \"[^\"]+\":
151
152/* Quoted names can contain any character except " and \ */
153StringConstant \"[^\"]*\"
Reid Spencer41dff5e2007-01-26 08:05:27 +0000154AtStringConstant @\"[^\"]*\"
Reid Spencered951ea2007-05-19 07:22:10 +0000155PctStringConstant %\"[^\"]*\"
Reid Spencer41dff5e2007-01-26 08:05:27 +0000156
157/* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
158LocalVarID %[0-9]+
159GlobalVarID @[0-9]+
Chris Lattner32eecb02006-02-14 05:14:46 +0000160
Reid Spencer41dff5e2007-01-26 08:05:27 +0000161/* Integer types are specified with i and a bitwidth */
Reid Spencer4db20632007-01-12 07:28:27 +0000162IntegerType i[0-9]+
Reid Spencera54b7cb2007-01-12 07:05:14 +0000163
Reid Spencer41dff5e2007-01-26 08:05:27 +0000164/* E[PN]Integer: match positive and negative literal integer values. */
Chris Lattner32eecb02006-02-14 05:14:46 +0000165PInteger [0-9]+
166NInteger -[0-9]+
167
168/* FPConstant - A Floating point constant.
169 */
170FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
171
172/* HexFPConstant - Floating point constant represented in IEEE format as a
173 * hexadecimal number for when exponential notation is not precise enough.
174 */
175HexFPConstant 0x[0-9A-Fa-f]+
176
177/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
178 * it to deal with 64 bit numbers.
179 */
180HexIntConstant [us]0x[0-9A-Fa-f]+
Reid Spencer38c91a92007-02-28 02:24:54 +0000181
Chris Lattner32eecb02006-02-14 05:14:46 +0000182%%
183
184{Comment} { /* Ignore comments for now */ }
185
186begin { return BEGINTOK; }
187end { return ENDTOK; }
188true { return TRUETOK; }
189false { return FALSETOK; }
190declare { return DECLARE; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000191define { return DEFINE; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000192global { return GLOBAL; }
193constant { return CONSTANT; }
194internal { return INTERNAL; }
195linkonce { return LINKONCE; }
196weak { return WEAK; }
197appending { return APPENDING; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000198dllimport { return DLLIMPORT; }
199dllexport { return DLLEXPORT; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000200hidden { return HIDDEN; }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000201protected { return PROTECTED; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000202extern_weak { return EXTERN_WEAK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000203external { return EXTERNAL; }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000204thread_local { return THREAD_LOCAL; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000205zeroinitializer { return ZEROINITIALIZER; }
206\.\.\. { return DOTDOTDOT; }
207undef { return UNDEF; }
208null { return NULL_TOK; }
209to { return TO; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000210tail { return TAIL; }
211target { return TARGET; }
212triple { return TRIPLE; }
213deplibs { return DEPLIBS; }
Chris Lattner1ae022f2006-10-22 06:08:13 +0000214datalayout { return DATALAYOUT; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000215volatile { return VOLATILE; }
216align { return ALIGN; }
217section { return SECTION; }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +0000218alias { return ALIAS; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000219module { return MODULE; }
220asm { return ASM_TOK; }
221sideeffect { return SIDEEFFECT; }
222
223cc { return CC_TOK; }
224ccc { return CCC_TOK; }
225fastcc { return FASTCC_TOK; }
226coldcc { return COLDCC_TOK; }
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000227x86_stdcallcc { return X86_STDCALLCC_TOK; }
228x86_fastcallcc { return X86_FASTCALLCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000229
Reid Spencer832254e2007-02-02 02:16:23 +0000230inreg { return INREG; }
231sret { return SRET; }
Reid Spencer67d8ed92007-03-22 02:14:08 +0000232nounwind { return NOUNWIND; }
233noreturn { return NORETURN; }
Reid Spencer832254e2007-02-02 02:16:23 +0000234
Reid Spencera132e042006-12-03 05:46:11 +0000235void { RET_TY(Type::VoidTy, VOID); }
Reid Spencera132e042006-12-03 05:46:11 +0000236float { RET_TY(Type::FloatTy, FLOAT); }
237double { RET_TY(Type::DoubleTy,DOUBLE);}
238label { RET_TY(Type::LabelTy, LABEL); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000239type { return TYPE; }
240opaque { return OPAQUE; }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000241{IntegerType} { uint64_t NumBits = atoull(yytext+1);
242 if (NumBits < IntegerType::MIN_INT_BITS ||
243 NumBits > IntegerType::MAX_INT_BITS)
244 GenerateError("Bitwidth for integer type out of range!");
245 const Type* Ty = IntegerType::get(NumBits);
246 RET_TY(Ty, INTTYPE);
247 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000248
249add { RET_TOK(BinaryOpVal, Add, ADD); }
250sub { RET_TOK(BinaryOpVal, Sub, SUB); }
251mul { RET_TOK(BinaryOpVal, Mul, MUL); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000252udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
253sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
254fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000255urem { RET_TOK(BinaryOpVal, URem, UREM); }
256srem { RET_TOK(BinaryOpVal, SRem, SREM); }
257frem { RET_TOK(BinaryOpVal, FRem, FREM); }
Reid Spencer832254e2007-02-02 02:16:23 +0000258shl { RET_TOK(BinaryOpVal, Shl, SHL); }
259lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
260ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000261and { RET_TOK(BinaryOpVal, And, AND); }
262or { RET_TOK(BinaryOpVal, Or , OR ); }
263xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Reid Spencera132e042006-12-03 05:46:11 +0000264icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
265fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
Reid Spencer832254e2007-02-02 02:16:23 +0000266
Reid Spencer6e18b7d2006-12-03 06:59:29 +0000267eq { return EQ; }
268ne { return NE; }
269slt { return SLT; }
270sgt { return SGT; }
271sle { return SLE; }
272sge { return SGE; }
273ult { return ULT; }
274ugt { return UGT; }
275ule { return ULE; }
276uge { return UGE; }
277oeq { return OEQ; }
278one { return ONE; }
279olt { return OLT; }
280ogt { return OGT; }
281ole { return OLE; }
282oge { return OGE; }
283ord { return ORD; }
284uno { return UNO; }
285ueq { return UEQ; }
286une { return UNE; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000287
288phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
289call { RET_TOK(OtherOpVal, Call, CALL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000290trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
291zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
292sext { RET_TOK(CastOpVal, SExt, SEXT); }
293fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
294fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
295uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
296sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
297fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
298fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
299inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
300ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
301bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000302select { RET_TOK(OtherOpVal, Select, SELECT); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000303va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
304ret { RET_TOK(TermOpVal, Ret, RET); }
305br { RET_TOK(TermOpVal, Br, BR); }
306switch { RET_TOK(TermOpVal, Switch, SWITCH); }
307invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
308unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
309unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
310
311malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
312alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
313free { RET_TOK(MemOpVal, Free, FREE); }
314load { RET_TOK(MemOpVal, Load, LOAD); }
315store { RET_TOK(MemOpVal, Store, STORE); }
316getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
317
318extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
319insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
Chris Lattnerd5efe842006-04-08 01:18:56 +0000320shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000321
322
Reid Spencer41dff5e2007-01-26 08:05:27 +0000323{LocalVarName} {
Chris Lattner32eecb02006-02-14 05:14:46 +0000324 UnEscapeLexed(yytext+1);
325 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
Reid Spencer41dff5e2007-01-26 08:05:27 +0000326 return LOCALVAR;
327 }
328{GlobalVarName} {
329 UnEscapeLexed(yytext+1);
330 llvmAsmlval.StrVal = strdup(yytext+1); // Skip @
331 return GLOBALVAR;
Chris Lattner32eecb02006-02-14 05:14:46 +0000332 }
333{Label} {
334 yytext[strlen(yytext)-1] = 0; // nuke colon
335 UnEscapeLexed(yytext);
336 llvmAsmlval.StrVal = strdup(yytext);
337 return LABELSTR;
338 }
339{QuoteLabel} {
340 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
341 UnEscapeLexed(yytext+1);
342 llvmAsmlval.StrVal = strdup(yytext+1);
343 return LABELSTR;
344 }
345
346{StringConstant} { // Note that we cannot unescape a string constant here! The
347 // string constant might contain a \00 which would not be
348 // understood by the string stuff. It is valid to make a
349 // [sbyte] c"Hello World\00" constant, for example.
350 //
351 yytext[strlen(yytext)-1] = 0; // nuke end quote
352 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
353 return STRINGCONSTANT;
354 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000355{AtStringConstant} {
356 yytext[strlen(yytext)-1] = 0; // nuke end quote
357 llvmAsmlval.StrVal = strdup(yytext+2); // Nuke @, quote
358 return ATSTRINGCONSTANT;
359 }
360
Reid Spencered951ea2007-05-19 07:22:10 +0000361{PctStringConstant} {
362 yytext[strlen(yytext)-1] = 0; // nuke end quote
363 llvmAsmlval.StrVal = strdup(yytext+2); // Nuke @, quote
364 return PCTSTRINGCONSTANT;
365 }
Reid Spencer38c91a92007-02-28 02:24:54 +0000366{PInteger} { int len = strlen(yytext);
367 uint32_t numBits = ((len * 64) / 19) + 1;
368 APInt Tmp(numBits, yytext, len, 10);
369 uint32_t activeBits = Tmp.getActiveBits();
370 if (activeBits > 0 && activeBits < numBits)
371 Tmp.trunc(activeBits);
372 if (Tmp.getBitWidth() > 64) {
373 llvmAsmlval.APIntVal = new APInt(Tmp);
374 return EUAPINTVAL;
375 } else {
376 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
377 return EUINT64VAL;
378 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000379 }
Reid Spencer38c91a92007-02-28 02:24:54 +0000380{NInteger} { int len = strlen(yytext);
Reid Spencerafc37822007-03-09 21:19:09 +0000381 uint32_t numBits = (((len-1) * 64) / 19) + 2;
Reid Spencer38c91a92007-02-28 02:24:54 +0000382 APInt Tmp(numBits, yytext, len, 10);
383 uint32_t minBits = Tmp.getMinSignedBits();
384 if (minBits > 0 && minBits < numBits)
385 Tmp.trunc(minBits);
386 if (Tmp.getBitWidth() > 64) {
387 llvmAsmlval.APIntVal = new APInt(Tmp);
388 return ESAPINTVAL;
389 } else {
390 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
391 return ESINT64VAL;
392 }
393 }
394
395{HexIntConstant} { int len = strlen(yytext+3) - 3;
396 uint32_t bits = len * 4;
397 APInt Tmp(bits, yytext+3, len, 16);
398 uint32_t activeBits = Tmp.getActiveBits();
399 if (activeBits > 0 && activeBits < bits)
400 Tmp.trunc(activeBits);
401 if (Tmp.getBitWidth() > 64) {
402 llvmAsmlval.APIntVal = new APInt(Tmp);
403 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
404 } else if (yytext[0] == 's') {
405 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
406 return ESINT64VAL;
407 } else {
408 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
409 return EUINT64VAL;
410 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000411 }
412
Reid Spencer41dff5e2007-01-26 08:05:27 +0000413{LocalVarID} {
Chris Lattner32eecb02006-02-14 05:14:46 +0000414 uint64_t Val = atoull(yytext+1);
415 if ((unsigned)Val != Val)
Reid Spencer61c83e02006-08-18 08:43:06 +0000416 GenerateError("Invalid value number (too large)!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000417 llvmAsmlval.UIntVal = unsigned(Val);
Reid Spencer41dff5e2007-01-26 08:05:27 +0000418 return LOCALVAL_ID;
Chris Lattner32eecb02006-02-14 05:14:46 +0000419 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000420{GlobalVarID} {
421 uint64_t Val = atoull(yytext+1);
422 if ((unsigned)Val != Val)
423 GenerateError("Invalid value number (too large)!");
424 llvmAsmlval.UIntVal = unsigned(Val);
425 return GLOBALVAL_ID;
Chris Lattner32eecb02006-02-14 05:14:46 +0000426 }
427
428{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
429{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
430
431<<EOF>> {
432 /* Make sure to free the internal buffers for flex when we are
433 * done reading our input!
434 */
435 yy_delete_buffer(YY_CURRENT_BUFFER);
436 return EOF;
437 }
438
439[ \r\t\n] { /* Ignore whitespace */ }
440. { return yytext[0]; }
441
442%%