blob: 7183e0e3ce2fd56020c2806c9db92d87ad747724 [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
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000104// appropriate character.
105char *UnEscapeLexed(char *Buffer) {
Chris Lattner32eecb02006-02-14 05:14:46 +0000106 char *BOut = Buffer;
107 for (char *BIn = Buffer; *BIn; ) {
108 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000109 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
110 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
111 BIn[3] = Tmp; // Restore character
112 BIn += 3; // Skip over handled chars
Chris Lattner32eecb02006-02-14 05:14:46 +0000113 ++BOut;
114 } else {
115 *BOut++ = *BIn++;
116 }
117 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000118 return BOut;
119}
120
121} // End llvm namespace
122
123using namespace llvm;
124
125#define YY_NEVER_INTERACTIVE 1
126%}
127
128
129
130/* Comments start with a ; and go till end of line */
131Comment ;.*
132
Reid Spencer41dff5e2007-01-26 08:05:27 +0000133/* Local Values and Type identifiers start with a % sign */
134LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
135
136/* Global Value identifiers start with an @ sign */
137GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner32eecb02006-02-14 05:14:46 +0000138
139/* Label identifiers end with a colon */
140Label [-a-zA-Z$._0-9]+:
141QuoteLabel \"[^\"]+\":
142
143/* Quoted names can contain any character except " and \ */
144StringConstant \"[^\"]*\"
Reid Spencer41dff5e2007-01-26 08:05:27 +0000145AtStringConstant @\"[^\"]*\"
Reid Spencered951ea2007-05-19 07:22:10 +0000146PctStringConstant %\"[^\"]*\"
Reid Spencer41dff5e2007-01-26 08:05:27 +0000147
148/* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
149LocalVarID %[0-9]+
150GlobalVarID @[0-9]+
Chris Lattner32eecb02006-02-14 05:14:46 +0000151
Reid Spencer41dff5e2007-01-26 08:05:27 +0000152/* Integer types are specified with i and a bitwidth */
Reid Spencer4db20632007-01-12 07:28:27 +0000153IntegerType i[0-9]+
Reid Spencera54b7cb2007-01-12 07:05:14 +0000154
Reid Spencer41dff5e2007-01-26 08:05:27 +0000155/* E[PN]Integer: match positive and negative literal integer values. */
Chris Lattner32eecb02006-02-14 05:14:46 +0000156PInteger [0-9]+
157NInteger -[0-9]+
158
159/* FPConstant - A Floating point constant.
160 */
161FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
162
163/* HexFPConstant - Floating point constant represented in IEEE format as a
164 * hexadecimal number for when exponential notation is not precise enough.
165 */
166HexFPConstant 0x[0-9A-Fa-f]+
167
168/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
169 * it to deal with 64 bit numbers.
170 */
171HexIntConstant [us]0x[0-9A-Fa-f]+
Reid Spencer38c91a92007-02-28 02:24:54 +0000172
Chris Lattner32eecb02006-02-14 05:14:46 +0000173%%
174
175{Comment} { /* Ignore comments for now */ }
176
177begin { return BEGINTOK; }
178end { return ENDTOK; }
179true { return TRUETOK; }
180false { return FALSETOK; }
181declare { return DECLARE; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000182define { return DEFINE; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000183global { return GLOBAL; }
184constant { return CONSTANT; }
185internal { return INTERNAL; }
186linkonce { return LINKONCE; }
187weak { return WEAK; }
188appending { return APPENDING; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000189dllimport { return DLLIMPORT; }
190dllexport { return DLLEXPORT; }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000191hidden { return HIDDEN; }
Anton Korobeynikov6f9896f2007-04-29 18:35:00 +0000192protected { return PROTECTED; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000193extern_weak { return EXTERN_WEAK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000194external { return EXTERNAL; }
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +0000195thread_local { return THREAD_LOCAL; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000196zeroinitializer { return ZEROINITIALIZER; }
197\.\.\. { return DOTDOTDOT; }
198undef { return UNDEF; }
199null { return NULL_TOK; }
200to { return TO; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000201tail { return TAIL; }
202target { return TARGET; }
203triple { return TRIPLE; }
204deplibs { return DEPLIBS; }
Chris Lattner1ae022f2006-10-22 06:08:13 +0000205datalayout { return DATALAYOUT; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000206volatile { return VOLATILE; }
207align { return ALIGN; }
208section { return SECTION; }
Anton Korobeynikov77d0f972007-04-25 14:29:12 +0000209alias { return ALIAS; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000210module { return MODULE; }
211asm { return ASM_TOK; }
212sideeffect { return SIDEEFFECT; }
213
214cc { return CC_TOK; }
215ccc { return CCC_TOK; }
216fastcc { return FASTCC_TOK; }
217coldcc { return COLDCC_TOK; }
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000218x86_stdcallcc { return X86_STDCALLCC_TOK; }
219x86_fastcallcc { return X86_FASTCALLCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000220
Reid Spencer832254e2007-02-02 02:16:23 +0000221inreg { return INREG; }
222sret { return SRET; }
Reid Spencer67d8ed92007-03-22 02:14:08 +0000223nounwind { return NOUNWIND; }
224noreturn { return NORETURN; }
Reid Spencer832254e2007-02-02 02:16:23 +0000225
Reid Spencera132e042006-12-03 05:46:11 +0000226void { RET_TY(Type::VoidTy, VOID); }
Reid Spencera132e042006-12-03 05:46:11 +0000227float { RET_TY(Type::FloatTy, FLOAT); }
228double { RET_TY(Type::DoubleTy,DOUBLE);}
229label { RET_TY(Type::LabelTy, LABEL); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000230type { return TYPE; }
231opaque { return OPAQUE; }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000232{IntegerType} { uint64_t NumBits = atoull(yytext+1);
233 if (NumBits < IntegerType::MIN_INT_BITS ||
234 NumBits > IntegerType::MAX_INT_BITS)
235 GenerateError("Bitwidth for integer type out of range!");
236 const Type* Ty = IntegerType::get(NumBits);
237 RET_TY(Ty, INTTYPE);
238 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000239
240add { RET_TOK(BinaryOpVal, Add, ADD); }
241sub { RET_TOK(BinaryOpVal, Sub, SUB); }
242mul { RET_TOK(BinaryOpVal, Mul, MUL); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000243udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
244sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
245fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000246urem { RET_TOK(BinaryOpVal, URem, UREM); }
247srem { RET_TOK(BinaryOpVal, SRem, SREM); }
248frem { RET_TOK(BinaryOpVal, FRem, FREM); }
Reid Spencer832254e2007-02-02 02:16:23 +0000249shl { RET_TOK(BinaryOpVal, Shl, SHL); }
250lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
251ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000252and { RET_TOK(BinaryOpVal, And, AND); }
253or { RET_TOK(BinaryOpVal, Or , OR ); }
254xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Reid Spencera132e042006-12-03 05:46:11 +0000255icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
256fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
Reid Spencer832254e2007-02-02 02:16:23 +0000257
Reid Spencer6e18b7d2006-12-03 06:59:29 +0000258eq { return EQ; }
259ne { return NE; }
260slt { return SLT; }
261sgt { return SGT; }
262sle { return SLE; }
263sge { return SGE; }
264ult { return ULT; }
265ugt { return UGT; }
266ule { return ULE; }
267uge { return UGE; }
268oeq { return OEQ; }
269one { return ONE; }
270olt { return OLT; }
271ogt { return OGT; }
272ole { return OLE; }
273oge { return OGE; }
274ord { return ORD; }
275uno { return UNO; }
276ueq { return UEQ; }
277une { return UNE; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000278
279phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
280call { RET_TOK(OtherOpVal, Call, CALL); }
Reid Spencer3da59db2006-11-27 01:05:10 +0000281trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
282zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
283sext { RET_TOK(CastOpVal, SExt, SEXT); }
284fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
285fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
286uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
287sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
288fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
289fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
290inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
291ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
292bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000293select { RET_TOK(OtherOpVal, Select, SELECT); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000294va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
295ret { RET_TOK(TermOpVal, Ret, RET); }
296br { RET_TOK(TermOpVal, Br, BR); }
297switch { RET_TOK(TermOpVal, Switch, SWITCH); }
298invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
299unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
300unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
301
302malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
303alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
304free { RET_TOK(MemOpVal, Free, FREE); }
305load { RET_TOK(MemOpVal, Load, LOAD); }
306store { RET_TOK(MemOpVal, Store, STORE); }
307getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
308
309extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
310insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
Chris Lattnerd5efe842006-04-08 01:18:56 +0000311shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000312
313
Reid Spencer41dff5e2007-01-26 08:05:27 +0000314{LocalVarName} {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000315 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip %
Reid Spencer41dff5e2007-01-26 08:05:27 +0000316 return LOCALVAR;
317 }
318{GlobalVarName} {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000319 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip @
Reid Spencer41dff5e2007-01-26 08:05:27 +0000320 return GLOBALVAR;
Chris Lattner32eecb02006-02-14 05:14:46 +0000321 }
322{Label} {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000323 yytext[yyleng-1] = 0; // nuke colon
324 llvmAsmlval.StrVal = new std::string(yytext);
Chris Lattner32eecb02006-02-14 05:14:46 +0000325 return LABELSTR;
326 }
327{QuoteLabel} {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000328 yytext[yyleng-2] = 0; // nuke colon, end quote
329 const char* EndChar = UnEscapeLexed(yytext+1);
330 llvmAsmlval.StrVal =
331 new std::string(yytext+1, EndChar - yytext - 1);
Chris Lattner32eecb02006-02-14 05:14:46 +0000332 return LABELSTR;
333 }
334
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000335{StringConstant} { yytext[yyleng-1] = 0; // nuke end quote
336 const char* EndChar = UnEscapeLexed(yytext+1);
337 llvmAsmlval.StrVal =
338 new std::string(yytext+1, EndChar - yytext - 1);
Chris Lattner32eecb02006-02-14 05:14:46 +0000339 return STRINGCONSTANT;
340 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000341{AtStringConstant} {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000342 yytext[yyleng-1] = 0; // nuke end quote
343 const char* EndChar = UnEscapeLexed(yytext+2);
344 llvmAsmlval.StrVal =
345 new std::string(yytext+2, EndChar - yytext - 2);
Reid Spencer41dff5e2007-01-26 08:05:27 +0000346 return ATSTRINGCONSTANT;
347 }
Reid Spencered951ea2007-05-19 07:22:10 +0000348{PctStringConstant} {
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000349 yytext[yyleng-1] = 0; // nuke end quote
350 const char* EndChar = UnEscapeLexed(yytext+2);
351 llvmAsmlval.StrVal =
352 new std::string(yytext+2, EndChar - yytext - 2);
Reid Spencered951ea2007-05-19 07:22:10 +0000353 return PCTSTRINGCONSTANT;
354 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000355{PInteger} {
356 uint32_t numBits = ((yyleng * 64) / 19) + 1;
357 APInt Tmp(numBits, yytext, yyleng, 10);
Reid Spencer38c91a92007-02-28 02:24:54 +0000358 uint32_t activeBits = Tmp.getActiveBits();
359 if (activeBits > 0 && activeBits < numBits)
360 Tmp.trunc(activeBits);
361 if (Tmp.getBitWidth() > 64) {
362 llvmAsmlval.APIntVal = new APInt(Tmp);
363 return EUAPINTVAL;
364 } else {
365 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
366 return EUINT64VAL;
367 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000368 }
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000369{NInteger} {
370 uint32_t numBits = (((yyleng-1) * 64) / 19) + 2;
371 APInt Tmp(numBits, yytext, yyleng, 10);
Reid Spencer38c91a92007-02-28 02:24:54 +0000372 uint32_t minBits = Tmp.getMinSignedBits();
373 if (minBits > 0 && minBits < numBits)
374 Tmp.trunc(minBits);
375 if (Tmp.getBitWidth() > 64) {
376 llvmAsmlval.APIntVal = new APInt(Tmp);
377 return ESAPINTVAL;
378 } else {
379 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
380 return ESINT64VAL;
381 }
382 }
383
Reid Spencer0a8a16b2007-05-22 18:52:55 +0000384{HexIntConstant} { int len = yyleng - 3;
Reid Spencer38c91a92007-02-28 02:24:54 +0000385 uint32_t bits = len * 4;
386 APInt Tmp(bits, yytext+3, len, 16);
387 uint32_t activeBits = Tmp.getActiveBits();
388 if (activeBits > 0 && activeBits < bits)
389 Tmp.trunc(activeBits);
390 if (Tmp.getBitWidth() > 64) {
391 llvmAsmlval.APIntVal = new APInt(Tmp);
392 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
393 } else if (yytext[0] == 's') {
394 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
395 return ESINT64VAL;
396 } else {
397 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
398 return EUINT64VAL;
399 }
Chris Lattner32eecb02006-02-14 05:14:46 +0000400 }
401
Reid Spencer41dff5e2007-01-26 08:05:27 +0000402{LocalVarID} {
Chris Lattner32eecb02006-02-14 05:14:46 +0000403 uint64_t Val = atoull(yytext+1);
404 if ((unsigned)Val != Val)
Reid Spencer61c83e02006-08-18 08:43:06 +0000405 GenerateError("Invalid value number (too large)!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000406 llvmAsmlval.UIntVal = unsigned(Val);
Reid Spencer41dff5e2007-01-26 08:05:27 +0000407 return LOCALVAL_ID;
Chris Lattner32eecb02006-02-14 05:14:46 +0000408 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000409{GlobalVarID} {
410 uint64_t Val = atoull(yytext+1);
411 if ((unsigned)Val != Val)
412 GenerateError("Invalid value number (too large)!");
413 llvmAsmlval.UIntVal = unsigned(Val);
414 return GLOBALVAL_ID;
Chris Lattner32eecb02006-02-14 05:14:46 +0000415 }
416
417{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
418{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
419
420<<EOF>> {
421 /* Make sure to free the internal buffers for flex when we are
422 * done reading our input!
423 */
424 yy_delete_buffer(YY_CURRENT_BUFFER);
425 return EOF;
426 }
427
428[ \r\t\n] { /* Ignore whitespace */ }
429. { return yytext[0]; }
430
431%%