blob: f9866f2bbcd3f82edfff728c2c9d9c74360ce89b [file] [log] [blame]
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001/*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
Misha Brukman8b477072005-05-10 22:02:28 +00002//
John Criswell29265fe2003-10-21 15:17:13 +00003// 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.
Misha Brukman8b477072005-05-10 22:02:28 +00007//
John Criswell29265fe2003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the flex scanner for LLVM assembly languages files.
11//
Chris Lattnerdb3b2022002-08-14 17:12:33 +000012//===----------------------------------------------------------------------===*/
Chris Lattner2f7c9632001-06-06 20:29:01 +000013
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"
Chris Lattner20126132003-04-22 19:07:06 +000029#include "llvm/Module.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000030#include <list>
31#include "llvmAsmParser.h"
Brian Gaekebc127092003-10-10 19:12:08 +000032#include <cctype>
33#include <cstdlib>
Chris Lattner2f7c9632001-06-06 20:29:01 +000034
Chris Lattner416a0d42005-05-20 03:25:47 +000035void 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
Chris Lattner2f7c9632001-06-06 20:29:01 +000042#define RET_TOK(type, Enum, sym) \
43 llvmAsmlval.type = Instruction::Enum; return sym
44
Brian Gaeke960707c2003-11-11 22:41:34 +000045namespace llvm {
Chris Lattner2f7c9632001-06-06 20:29:01 +000046
Misha Brukman8b477072005-05-10 22:02:28 +000047// TODO: All of the static identifiers are figured out by the lexer,
Chris Lattner47af30c2001-09-07 16:32:43 +000048// these should be hashed to reduce the lexer size
Chris Lattner2f7c9632001-06-06 20:29:01 +000049
50
51// atoull - Convert an ascii string of decimal digits into the unsigned long
Misha Brukman8b477072005-05-10 22:02:28 +000052// long representation... this does not have to do input error checking,
Chris Lattner2f7c9632001-06-06 20:29:01 +000053// because we know that the input will be matched by a suitable regex...
54//
Chris Lattner1b343742002-04-07 08:10:41 +000055static uint64_t atoull(const char *Buffer) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000056 uint64_t Result = 0;
57 for (; *Buffer; Buffer++) {
58 uint64_t OldRes = Result;
59 Result *= 10;
60 Result += *Buffer-'0';
Chris Lattner1b343742002-04-07 08:10:41 +000061 if (Result < OldRes) // Uh, oh, overflow detected!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +000062 ThrowException("constant bigger than 64 bits detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000063 }
64 return Result;
65}
66
Chris Lattnere509bd42003-04-17 22:17:32 +000067static uint64_t HexIntToVal(const char *Buffer) {
Chris Lattner1b343742002-04-07 08:10:41 +000068 uint64_t Result = 0;
69 for (; *Buffer; ++Buffer) {
70 uint64_t OldRes = Result;
71 Result *= 16;
72 char C = *Buffer;
73 if (C >= '0' && C <= '9')
74 Result += C-'0';
75 else if (C >= 'A' && C <= 'F')
76 Result += C-'A'+10;
77 else if (C >= 'a' && C <= 'f')
78 Result += C-'a'+10;
79
80 if (Result < OldRes) // Uh, oh, overflow detected!!!
81 ThrowException("constant bigger than 64 bits detected!");
82 }
Chris Lattnere509bd42003-04-17 22:17:32 +000083 return Result;
84}
85
86
87// HexToFP - Convert the ascii string in hexidecimal format to the floating
88// point representation of it.
89//
90static double HexToFP(const char *Buffer) {
Chris Lattnerb99f4792002-04-07 08:31:26 +000091 // Behave nicely in the face of C TBAA rules... see:
92 // http://www.nullstone.com/htmls/category/aliastyp.htm
Chris Lattner62fa7432003-04-22 20:20:28 +000093 union {
94 uint64_t UI;
95 double FP;
96 } UIntToFP;
97 UIntToFP.UI = HexIntToVal(Buffer);
98
99 assert(sizeof(double) == sizeof(uint64_t) &&
100 "Data sizes incompatible on this target!");
101 return UIntToFP.FP; // Cast Hex constant to double
Chris Lattner1b343742002-04-07 08:10:41 +0000102}
103
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104
Chris Lattner26e50dc2001-07-28 17:48:55 +0000105// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
106// appropriate character. If AllowNull is set to false, a \00 value will cause
107// an exception to be thrown.
108//
109// If AllowNull is set to true, the return value of the function points to the
110// last character of the string in memory.
111//
Chris Lattnere6801392002-07-25 06:17:42 +0000112char *UnEscapeLexed(char *Buffer, bool AllowNull) {
Chris Lattner26e50dc2001-07-28 17:48:55 +0000113 char *BOut = Buffer;
114 for (char *BIn = Buffer; *BIn; ) {
115 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
116 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
Chris Lattnerfa66dc72005-01-08 20:07:03 +0000117 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
Chris Lattner26e50dc2001-07-28 17:48:55 +0000118 if (!AllowNull && !*BOut)
119 ThrowException("String literal cannot accept \\00 escape!");
Misha Brukman8b477072005-05-10 22:02:28 +0000120
Chris Lattner26e50dc2001-07-28 17:48:55 +0000121 BIn[3] = Tmp; // Restore character
122 BIn += 3; // Skip over handled chars
123 ++BOut;
124 } else {
125 *BOut++ = *BIn++;
126 }
127 }
128
129 return BOut;
130}
131
Brian Gaeke960707c2003-11-11 22:41:34 +0000132} // End llvm namespace
133
134using namespace llvm;
135
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136#define YY_NEVER_INTERACTIVE 1
137%}
138
139
140
141/* Comments start with a ; and go till end of line */
142Comment ;.*
143
Chris Lattner17f729e2001-07-15 06:35:53 +0000144/* Variable(Value) identifiers start with a % sign */
Chris Lattner1366b302001-12-04 04:31:30 +0000145VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000146
147/* Label identifiers end with a colon */
Chris Lattner1366b302001-12-04 04:31:30 +0000148Label [-a-zA-Z$._0-9]+:
Alkis Evlogimenosa93b4522004-12-10 05:40:19 +0000149QuoteLabel \"[^\"]+\":
Chris Lattner2f7c9632001-06-06 20:29:01 +0000150
151/* Quoted names can contain any character except " and \ */
Chris Lattner0079e7d2003-10-18 05:53:13 +0000152StringConstant \"[^\"]*\"
Chris Lattner2f7c9632001-06-06 20:29:01 +0000153
154
155/* [PN]Integer: match positive and negative literal integer values that
156 * are preceeded by a '%' character. These represent unnamed variable slots.
157 */
158EPInteger %[0-9]+
159ENInteger %-[0-9]+
160
161
162/* E[PN]Integer: match positive and negative literal integer values */
163PInteger [0-9]+
164NInteger -[0-9]+
165
Chris Lattner212f70d2001-07-15 00:17:01 +0000166/* FPConstant - A Floating point constant.
Chris Lattner1b343742002-04-07 08:10:41 +0000167 */
Chris Lattner50f68ac2001-11-01 22:06:08 +0000168FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner212f70d2001-07-15 00:17:01 +0000169
Chris Lattner1b343742002-04-07 08:10:41 +0000170/* HexFPConstant - Floating point constant represented in IEEE format as a
171 * hexadecimal number for when exponential notation is not precise enough.
172 */
173HexFPConstant 0x[0-9A-Fa-f]+
Chris Lattnere509bd42003-04-17 22:17:32 +0000174
175/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
176 * it to deal with 64 bit numbers.
177 */
178HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179%%
180
181{Comment} { /* Ignore comments for now */ }
182
183begin { return BEGINTOK; }
Chris Lattner1e1a9b42002-05-03 18:23:48 +0000184end { return ENDTOK; }
Chris Lattnerc0ba90e2004-03-31 03:49:47 +0000185true { return TRUETOK; }
186false { return FALSETOK; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187declare { return DECLARE; }
Chris Lattnerda975502001-09-10 07:58:01 +0000188global { return GLOBAL; }
Chris Lattner7fb14f52001-09-18 04:00:54 +0000189constant { return CONSTANT; }
Chris Lattner841d8b92001-11-26 18:54:16 +0000190internal { return INTERNAL; }
Chris Lattner379a8d22003-04-16 20:28:45 +0000191linkonce { return LINKONCE; }
Chris Lattnerf4120d92003-10-10 04:54:02 +0000192weak { return WEAK; }
Chris Lattner379a8d22003-04-16 20:28:45 +0000193appending { return APPENDING; }
Chris Lattnere2dd92b2002-10-06 22:45:09 +0000194uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
195external { return EXTERNAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196implementation { return IMPLEMENTATION; }
Chris Lattner79694012003-06-28 20:01:34 +0000197zeroinitializer { return ZEROINITIALIZER; }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000198\.\.\. { return DOTDOTDOT; }
Chris Lattner4ff31492004-10-16 18:17:13 +0000199undef { return UNDEF; }
Chris Lattnerfbdec252001-09-30 22:46:54 +0000200null { return NULL_TOK; }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000201to { return TO; }
Chris Lattner17235712004-02-08 21:48:25 +0000202except { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattnerdb3b2022002-08-14 17:12:33 +0000203not { return NOT; } /* Deprecated, turned into XOR */
Chris Lattnerca4d4bd2005-05-06 06:20:33 +0000204tail { return TAIL; }
Chris Lattner20126132003-04-22 19:07:06 +0000205target { return TARGET; }
Reid Spencer4add9192004-07-25 17:56:00 +0000206triple { return TRIPLE; }
207deplibs { return DEPLIBS; }
Chris Lattner20126132003-04-22 19:07:06 +0000208endian { return ENDIAN; }
209pointersize { return POINTERSIZE; }
210little { return LITTLE; }
211big { return BIG; }
Chris Lattner8b1680e2003-09-08 18:20:29 +0000212volatile { return VOLATILE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213
Chris Lattner53bdd312005-05-06 20:27:19 +0000214cc { return CC_TOK; }
215ccc { return CCC_TOK; }
216fastcc { return FASTCC_TOK; }
217coldcc { return COLDCC_TOK; }
218
Chris Lattner47af30c2001-09-07 16:32:43 +0000219void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
220bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
221sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
222ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
223short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
224ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
225int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
226uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
227long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
228ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
229float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
230double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000231label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Reid Spencere258e942004-07-04 12:17:44 +0000232type { return TYPE; }
Chris Lattnerf269b9d2002-04-04 19:22:17 +0000233opaque { return OPAQUE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235add { RET_TOK(BinaryOpVal, Add, ADD); }
236sub { RET_TOK(BinaryOpVal, Sub, SUB); }
237mul { RET_TOK(BinaryOpVal, Mul, MUL); }
238div { RET_TOK(BinaryOpVal, Div, DIV); }
239rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000240and { RET_TOK(BinaryOpVal, And, AND); }
241or { RET_TOK(BinaryOpVal, Or , OR ); }
242xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
244seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
245setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
246setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
247setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
248setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
249
Chris Lattnerb94550e2003-10-19 21:34:28 +0000250phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000251call { RET_TOK(OtherOpVal, Call, CALL); }
252cast { RET_TOK(OtherOpVal, Cast, CAST); }
Chris Lattner6536f0c2004-03-12 05:51:36 +0000253select { RET_TOK(OtherOpVal, Select, SELECT); }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000254shl { RET_TOK(OtherOpVal, Shl, SHL); }
255shr { RET_TOK(OtherOpVal, Shr, SHR); }
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000256vanext { return VANEXT_old; }
257vaarg { return VAARG_old; }
258va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000259ret { RET_TOK(TermOpVal, Ret, RET); }
260br { RET_TOK(TermOpVal, Br, BR); }
261switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000262invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner9c58cf62003-09-08 18:54:55 +0000263unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattner4ff31492004-10-16 18:17:13 +0000264unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265
266malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
267alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
268free { RET_TOK(MemOpVal, Free, FREE); }
269load { RET_TOK(MemOpVal, Load, LOAD); }
270store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000271getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000272
273
Chris Lattner26e50dc2001-07-28 17:48:55 +0000274{VarID} {
275 UnEscapeLexed(yytext+1);
276 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
Misha Brukman8b477072005-05-10 22:02:28 +0000277 return VAR_ID;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000278 }
279{Label} {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000280 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner26e50dc2001-07-28 17:48:55 +0000281 UnEscapeLexed(yytext);
Misha Brukman8b477072005-05-10 22:02:28 +0000282 llvmAsmlval.StrVal = strdup(yytext);
283 return LABELSTR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000284 }
Chris Lattnerfcec9d62004-12-10 05:27:29 +0000285{QuoteLabel} {
286 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
287 UnEscapeLexed(yytext+1);
Misha Brukman8b477072005-05-10 22:02:28 +0000288 llvmAsmlval.StrVal = strdup(yytext+1);
289 return LABELSTR;
Chris Lattnerfcec9d62004-12-10 05:27:29 +0000290 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291
Chris Lattner26e50dc2001-07-28 17:48:55 +0000292{StringConstant} { // Note that we cannot unescape a string constant here! The
Misha Brukman8b477072005-05-10 22:02:28 +0000293 // string constant might contain a \00 which would not be
Chris Lattner26e50dc2001-07-28 17:48:55 +0000294 // understood by the string stuff. It is valid to make a
295 // [sbyte] c"Hello World\00" constant, for example.
296 //
Misha Brukman8b477072005-05-10 22:02:28 +0000297 yytext[strlen(yytext)-1] = 0; // nuke end quote
298 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
299 return STRINGCONSTANT;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000300 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000301
302
303{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
Misha Brukman8b477072005-05-10 22:02:28 +0000304{NInteger} {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000305 uint64_t Val = atoull(yytext+1);
Misha Brukman8b477072005-05-10 22:02:28 +0000306 // +1: we have bigger negative range
307 if (Val > (uint64_t)INT64_MAX+1)
308 ThrowException("Constant too large for signed 64 bits!");
309 llvmAsmlval.SInt64Val = -Val;
310 return ESINT64VAL;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311 }
Chris Lattnere509bd42003-04-17 22:17:32 +0000312{HexIntConstant} {
Misha Brukman8b477072005-05-10 22:02:28 +0000313 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
Chris Lattnere509bd42003-04-17 22:17:32 +0000314 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
315 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000316
Chris Lattnerfa66dc72005-01-08 20:07:03 +0000317{EPInteger} {
318 uint64_t Val = atoull(yytext+1);
319 if ((unsigned)Val != Val)
320 ThrowException("Invalid value number (too large)!");
321 llvmAsmlval.UIntVal = unsigned(Val);
322 return UINTVAL;
323 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000324{ENInteger} {
325 uint64_t Val = atoull(yytext+2);
Misha Brukman8b477072005-05-10 22:02:28 +0000326 // +1: we have bigger negative range
327 if (Val > (uint64_t)INT32_MAX+1)
328 ThrowException("Constant too large for signed 32 bits!");
Chris Lattnerfa66dc72005-01-08 20:07:03 +0000329 llvmAsmlval.SIntVal = (int)-Val;
Misha Brukman8b477072005-05-10 22:02:28 +0000330 return SINTVAL;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000331 }
332
Chris Lattner212f70d2001-07-15 00:17:01 +0000333{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner1b343742002-04-07 08:10:41 +0000334{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000335
Chris Lattner3922d602004-03-19 23:34:33 +0000336<<EOF>> {
337 /* Make sure to free the internal buffers for flex when we are
338 * done reading our input!
339 */
340 yy_delete_buffer(YY_CURRENT_BUFFER);
341 return EOF;
342 }
343
Chris Lattner604e19e2004-05-27 17:49:14 +0000344[ \r\t\n] { /* Ignore whitespace */ }
Chris Lattner47af30c2001-09-07 16:32:43 +0000345. { return yytext[0]; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000346
347%%