blob: 45361bd77b8e6ee5218208098f6b05615671251b [file] [log] [blame]
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001/*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
John Criswell29265fe2003-10-21 15:17:13 +00002//
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//===----------------------------------------------------------------------===//
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
35#define RET_TOK(type, Enum, sym) \
36 llvmAsmlval.type = Instruction::Enum; return sym
37
Brian Gaeke960707c2003-11-11 22:41:34 +000038namespace llvm {
Chris Lattner2f7c9632001-06-06 20:29:01 +000039
40// TODO: All of the static identifiers are figured out by the lexer,
Chris Lattner47af30c2001-09-07 16:32:43 +000041// these should be hashed to reduce the lexer size
Chris Lattner2f7c9632001-06-06 20:29:01 +000042
43
44// atoull - Convert an ascii string of decimal digits into the unsigned long
45// long representation... this does not have to do input error checking,
46// because we know that the input will be matched by a suitable regex...
47//
Chris Lattner1b343742002-04-07 08:10:41 +000048static uint64_t atoull(const char *Buffer) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000049 uint64_t Result = 0;
50 for (; *Buffer; Buffer++) {
51 uint64_t OldRes = Result;
52 Result *= 10;
53 Result += *Buffer-'0';
Chris Lattner1b343742002-04-07 08:10:41 +000054 if (Result < OldRes) // Uh, oh, overflow detected!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +000055 ThrowException("constant bigger than 64 bits detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000056 }
57 return Result;
58}
59
Chris Lattnere509bd42003-04-17 22:17:32 +000060static uint64_t HexIntToVal(const char *Buffer) {
Chris Lattner1b343742002-04-07 08:10:41 +000061 uint64_t Result = 0;
62 for (; *Buffer; ++Buffer) {
63 uint64_t OldRes = Result;
64 Result *= 16;
65 char C = *Buffer;
66 if (C >= '0' && C <= '9')
67 Result += C-'0';
68 else if (C >= 'A' && C <= 'F')
69 Result += C-'A'+10;
70 else if (C >= 'a' && C <= 'f')
71 Result += C-'a'+10;
72
73 if (Result < OldRes) // Uh, oh, overflow detected!!!
74 ThrowException("constant bigger than 64 bits detected!");
75 }
Chris Lattnere509bd42003-04-17 22:17:32 +000076 return Result;
77}
78
79
80// HexToFP - Convert the ascii string in hexidecimal format to the floating
81// point representation of it.
82//
83static double HexToFP(const char *Buffer) {
Chris Lattnerb99f4792002-04-07 08:31:26 +000084 // Behave nicely in the face of C TBAA rules... see:
85 // http://www.nullstone.com/htmls/category/aliastyp.htm
Chris Lattner62fa7432003-04-22 20:20:28 +000086 union {
87 uint64_t UI;
88 double FP;
89 } UIntToFP;
90 UIntToFP.UI = HexIntToVal(Buffer);
91
92 assert(sizeof(double) == sizeof(uint64_t) &&
93 "Data sizes incompatible on this target!");
94 return UIntToFP.FP; // Cast Hex constant to double
Chris Lattner1b343742002-04-07 08:10:41 +000095}
96
Chris Lattner2f7c9632001-06-06 20:29:01 +000097
Chris Lattner26e50dc2001-07-28 17:48:55 +000098// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
99// appropriate character. If AllowNull is set to false, a \00 value will cause
100// an exception to be thrown.
101//
102// If AllowNull is set to true, the return value of the function points to the
103// last character of the string in memory.
104//
Chris Lattnere6801392002-07-25 06:17:42 +0000105char *UnEscapeLexed(char *Buffer, bool AllowNull) {
Chris Lattner26e50dc2001-07-28 17:48:55 +0000106 char *BOut = Buffer;
107 for (char *BIn = Buffer; *BIn; ) {
108 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
109 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
110 *BOut = strtol(BIn+1, 0, 16); // Convert to number
111 if (!AllowNull && !*BOut)
112 ThrowException("String literal cannot accept \\00 escape!");
113
114 BIn[3] = Tmp; // Restore character
115 BIn += 3; // Skip over handled chars
116 ++BOut;
117 } else {
118 *BOut++ = *BIn++;
119 }
120 }
121
122 return BOut;
123}
124
Brian Gaeke960707c2003-11-11 22:41:34 +0000125} // End llvm namespace
126
127using namespace llvm;
128
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129#define YY_NEVER_INTERACTIVE 1
130%}
131
132
133
134/* Comments start with a ; and go till end of line */
135Comment ;.*
136
Chris Lattner17f729e2001-07-15 06:35:53 +0000137/* Variable(Value) identifiers start with a % sign */
Chris Lattner1366b302001-12-04 04:31:30 +0000138VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000139
140/* Label identifiers end with a colon */
Chris Lattner1366b302001-12-04 04:31:30 +0000141Label [-a-zA-Z$._0-9]+:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000142
143/* Quoted names can contain any character except " and \ */
Chris Lattner0079e7d2003-10-18 05:53:13 +0000144StringConstant \"[^\"]*\"
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145
146
147/* [PN]Integer: match positive and negative literal integer values that
148 * are preceeded by a '%' character. These represent unnamed variable slots.
149 */
150EPInteger %[0-9]+
151ENInteger %-[0-9]+
152
153
154/* E[PN]Integer: match positive and negative literal integer values */
155PInteger [0-9]+
156NInteger -[0-9]+
157
Chris Lattner212f70d2001-07-15 00:17:01 +0000158/* FPConstant - A Floating point constant.
Chris Lattner1b343742002-04-07 08:10:41 +0000159 */
Chris Lattner50f68ac2001-11-01 22:06:08 +0000160FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner212f70d2001-07-15 00:17:01 +0000161
Chris Lattner1b343742002-04-07 08:10:41 +0000162/* HexFPConstant - Floating point constant represented in IEEE format as a
163 * hexadecimal number for when exponential notation is not precise enough.
164 */
165HexFPConstant 0x[0-9A-Fa-f]+
Chris Lattnere509bd42003-04-17 22:17:32 +0000166
167/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
168 * it to deal with 64 bit numbers.
169 */
170HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171%%
172
173{Comment} { /* Ignore comments for now */ }
174
175begin { return BEGINTOK; }
Chris Lattner1e1a9b42002-05-03 18:23:48 +0000176end { return ENDTOK; }
Chris Lattnerc0ba90e2004-03-31 03:49:47 +0000177true { return TRUETOK; }
178false { return FALSETOK; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179declare { return DECLARE; }
Chris Lattnerda975502001-09-10 07:58:01 +0000180global { return GLOBAL; }
Chris Lattner7fb14f52001-09-18 04:00:54 +0000181constant { return CONSTANT; }
Chris Lattner841d8b92001-11-26 18:54:16 +0000182internal { return INTERNAL; }
Chris Lattner379a8d22003-04-16 20:28:45 +0000183linkonce { return LINKONCE; }
Chris Lattnerf4120d92003-10-10 04:54:02 +0000184weak { return WEAK; }
Chris Lattner379a8d22003-04-16 20:28:45 +0000185appending { return APPENDING; }
Chris Lattnere2dd92b2002-10-06 22:45:09 +0000186uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
187external { return EXTERNAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188implementation { return IMPLEMENTATION; }
Chris Lattner79694012003-06-28 20:01:34 +0000189zeroinitializer { return ZEROINITIALIZER; }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000190\.\.\. { return DOTDOTDOT; }
Chris Lattnerfbdec252001-09-30 22:46:54 +0000191null { return NULL_TOK; }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000192to { return TO; }
Chris Lattner17235712004-02-08 21:48:25 +0000193except { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattnerdb3b2022002-08-14 17:12:33 +0000194not { return NOT; } /* Deprecated, turned into XOR */
Chris Lattner20126132003-04-22 19:07:06 +0000195target { return TARGET; }
Reid Spencer4add9192004-07-25 17:56:00 +0000196triple { return TRIPLE; }
197deplibs { return DEPLIBS; }
Chris Lattner20126132003-04-22 19:07:06 +0000198endian { return ENDIAN; }
199pointersize { return POINTERSIZE; }
200little { return LITTLE; }
201big { return BIG; }
Chris Lattner8b1680e2003-09-08 18:20:29 +0000202volatile { return VOLATILE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203
Chris Lattner47af30c2001-09-07 16:32:43 +0000204void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
205bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
206sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
207ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
208short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
209ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
210int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
211uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
212long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
213ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
214float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
215double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000216label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Reid Spencere258e942004-07-04 12:17:44 +0000217type { return TYPE; }
Chris Lattnerf269b9d2002-04-04 19:22:17 +0000218opaque { return OPAQUE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220add { RET_TOK(BinaryOpVal, Add, ADD); }
221sub { RET_TOK(BinaryOpVal, Sub, SUB); }
222mul { RET_TOK(BinaryOpVal, Mul, MUL); }
223div { RET_TOK(BinaryOpVal, Div, DIV); }
224rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000225and { RET_TOK(BinaryOpVal, And, AND); }
226or { RET_TOK(BinaryOpVal, Or , OR ); }
227xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
229seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
230setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
231setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
232setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
233setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
234
Chris Lattnerb94550e2003-10-19 21:34:28 +0000235phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000236call { RET_TOK(OtherOpVal, Call, CALL); }
237cast { RET_TOK(OtherOpVal, Cast, CAST); }
Chris Lattner6536f0c2004-03-12 05:51:36 +0000238select { RET_TOK(OtherOpVal, Select, SELECT); }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000239shl { RET_TOK(OtherOpVal, Shl, SHL); }
240shr { RET_TOK(OtherOpVal, Shr, SHR); }
Chris Lattner0079e7d2003-10-18 05:53:13 +0000241va_arg { return VA_ARG; /* FIXME: OBSOLETE */}
242vanext { RET_TOK(OtherOpVal, VANext, VANEXT); }
243vaarg { RET_TOK(OtherOpVal, VAArg , VAARG); }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000244
Chris Lattner2f7c9632001-06-06 20:29:01 +0000245ret { RET_TOK(TermOpVal, Ret, RET); }
246br { RET_TOK(TermOpVal, Br, BR); }
247switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000248invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner9c58cf62003-09-08 18:54:55 +0000249unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000250
251
252malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
253alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
254free { RET_TOK(MemOpVal, Free, FREE); }
255load { RET_TOK(MemOpVal, Load, LOAD); }
256store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000257getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000258
259
Chris Lattner26e50dc2001-07-28 17:48:55 +0000260{VarID} {
261 UnEscapeLexed(yytext+1);
262 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
263 return VAR_ID;
264 }
265{Label} {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000266 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner26e50dc2001-07-28 17:48:55 +0000267 UnEscapeLexed(yytext);
268 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269 return LABELSTR;
270 }
271
Chris Lattner26e50dc2001-07-28 17:48:55 +0000272{StringConstant} { // Note that we cannot unescape a string constant here! The
273 // string constant might contain a \00 which would not be
274 // understood by the string stuff. It is valid to make a
275 // [sbyte] c"Hello World\00" constant, for example.
276 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner26e50dc2001-07-28 17:48:55 +0000278 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner2f7c9632001-06-06 20:29:01 +0000279 return STRINGCONSTANT;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000280 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000281
282
283{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
284{NInteger} {
285 uint64_t Val = atoull(yytext+1);
286 // +1: we have bigger negative range
287 if (Val > (uint64_t)INT64_MAX+1)
288 ThrowException("Constant too large for signed 64 bits!");
289 llvmAsmlval.SInt64Val = -Val;
290 return ESINT64VAL;
291 }
Chris Lattnere509bd42003-04-17 22:17:32 +0000292{HexIntConstant} {
293 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
294 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
295 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000296
297{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
298{ENInteger} {
299 uint64_t Val = atoull(yytext+2);
300 // +1: we have bigger negative range
301 if (Val > (uint64_t)INT32_MAX+1)
302 ThrowException("Constant too large for signed 32 bits!");
303 llvmAsmlval.SIntVal = -Val;
304 return SINTVAL;
305 }
306
Chris Lattner212f70d2001-07-15 00:17:01 +0000307{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner1b343742002-04-07 08:10:41 +0000308{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000309
Chris Lattner3922d602004-03-19 23:34:33 +0000310<<EOF>> {
311 /* Make sure to free the internal buffers for flex when we are
312 * done reading our input!
313 */
314 yy_delete_buffer(YY_CURRENT_BUFFER);
315 return EOF;
316 }
317
Chris Lattner604e19e2004-05-27 17:49:14 +0000318[ \r\t\n] { /* Ignore whitespace */ }
Chris Lattner47af30c2001-09-07 16:32:43 +0000319. { return yytext[0]; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000320
321%%