blob: 510fedb8698826a39f533a04699f1956bfa60227 [file] [log] [blame]
Chris Lattner699f1eb2002-08-14 17:12:33 +00001/*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
John Criswell856ba762003-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 Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the flex scanner for LLVM assembly languages files.
11//
Chris Lattner699f1eb2002-08-14 17:12:33 +000012//===----------------------------------------------------------------------===*/
Chris Lattner00950542001-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 Lattnerb9bcbb52003-04-22 19:07:06 +000029#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000030#include <list>
31#include "llvmAsmParser.h"
Brian Gaeke778fab22003-10-10 19:12:08 +000032#include <cctype>
33#include <cstdlib>
Chris Lattner00950542001-06-06 20:29:01 +000034
35#define RET_TOK(type, Enum, sym) \
36 llvmAsmlval.type = Instruction::Enum; return sym
37
Brian Gaeked0fde302003-11-11 22:41:34 +000038namespace llvm {
Chris Lattner00950542001-06-06 20:29:01 +000039
40// TODO: All of the static identifiers are figured out by the lexer,
Chris Lattnere1fe8752001-09-07 16:32:43 +000041// these should be hashed to reduce the lexer size
Chris Lattner00950542001-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 Lattner275da862002-04-07 08:10:41 +000048static uint64_t atoull(const char *Buffer) {
Chris Lattner00950542001-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 Lattner275da862002-04-07 08:10:41 +000054 if (Result < OldRes) // Uh, oh, overflow detected!!!
Chris Lattner00950542001-06-06 20:29:01 +000055 ThrowException("constant bigger than 64 bits detected!");
Chris Lattner00950542001-06-06 20:29:01 +000056 }
57 return Result;
58}
59
Chris Lattner3e8ba102003-04-17 22:17:32 +000060static uint64_t HexIntToVal(const char *Buffer) {
Chris Lattner275da862002-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 Lattner3e8ba102003-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 Lattnerb55679d2002-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 Lattner7a5a1f72003-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 Lattner275da862002-04-07 08:10:41 +000095}
96
Chris Lattner00950542001-06-06 20:29:01 +000097
Chris Lattner93750fa2001-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 Lattnerbcafcce2002-07-25 06:17:42 +0000105char *UnEscapeLexed(char *Buffer, bool AllowNull) {
Chris Lattner93750fa2001-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 Gaeked0fde302003-11-11 22:41:34 +0000125} // End llvm namespace
126
127using namespace llvm;
128
Chris Lattner00950542001-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 Lattnere1815642001-07-15 06:35:53 +0000137/* Variable(Value) identifiers start with a % sign */
Chris Lattner9a88d272001-12-04 04:31:30 +0000138VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner00950542001-06-06 20:29:01 +0000139
140/* Label identifiers end with a colon */
Chris Lattner9a88d272001-12-04 04:31:30 +0000141Label [-a-zA-Z$._0-9]+:
Chris Lattner00950542001-06-06 20:29:01 +0000142
143/* Quoted names can contain any character except " and \ */
Chris Lattner99e7ab72003-10-18 05:53:13 +0000144StringConstant \"[^\"]*\"
Chris Lattner00950542001-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 Lattner3d52b2f2001-07-15 00:17:01 +0000158/* FPConstant - A Floating point constant.
Chris Lattner275da862002-04-07 08:10:41 +0000159 */
Chris Lattner1e2c6142001-11-01 22:06:08 +0000160FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000161
Chris Lattner275da862002-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 Lattner3e8ba102003-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 Lattner00950542001-06-06 20:29:01 +0000171%%
172
173{Comment} { /* Ignore comments for now */ }
174
175begin { return BEGINTOK; }
Chris Lattner9b02cc32002-05-03 18:23:48 +0000176end { return ENDTOK; }
Chris Lattner91ef4602004-03-31 03:49:47 +0000177true { return TRUETOK; }
178false { return FALSETOK; }
Chris Lattner00950542001-06-06 20:29:01 +0000179declare { return DECLARE; }
Chris Lattner70cc3392001-09-10 07:58:01 +0000180global { return GLOBAL; }
Chris Lattner1781aca2001-09-18 04:00:54 +0000181constant { return CONSTANT; }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000182const { return CONST; }
Chris Lattnerdda71962001-11-26 18:54:16 +0000183internal { return INTERNAL; }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000184linkonce { return LINKONCE; }
Chris Lattnerf797cab2003-10-10 04:54:02 +0000185weak { return WEAK; }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000186appending { return APPENDING; }
Chris Lattner08c0e6a2002-10-06 22:45:09 +0000187uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
188external { return EXTERNAL; }
Chris Lattner00950542001-06-06 20:29:01 +0000189implementation { return IMPLEMENTATION; }
Chris Lattnerf4600482003-06-28 20:01:34 +0000190zeroinitializer { return ZEROINITIALIZER; }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000191\.\.\. { return DOTDOTDOT; }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000192null { return NULL_TOK; }
Chris Lattner5efbbc22001-10-13 06:37:14 +0000193to { return TO; }
Chris Lattnera58e3a12004-02-08 21:48:25 +0000194except { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattner699f1eb2002-08-14 17:12:33 +0000195not { return NOT; } /* Deprecated, turned into XOR */
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000196target { return TARGET; }
197endian { return ENDIAN; }
198pointersize { return POINTERSIZE; }
199little { return LITTLE; }
200big { return BIG; }
Chris Lattner15c9c032003-09-08 18:20:29 +0000201volatile { return VOLATILE; }
Chris Lattner00950542001-06-06 20:29:01 +0000202
Chris Lattnere1fe8752001-09-07 16:32:43 +0000203void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
204bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
205sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
206ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
207short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
208ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
209int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
210uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
211long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
212ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
213float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
214double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000215type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000216label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Chris Lattner4e4cae82002-04-04 19:22:17 +0000217opaque { return OPAQUE; }
Chris Lattner00950542001-06-06 20:29:01 +0000218
Chris Lattner00950542001-06-06 20:29:01 +0000219add { RET_TOK(BinaryOpVal, Add, ADD); }
220sub { RET_TOK(BinaryOpVal, Sub, SUB); }
221mul { RET_TOK(BinaryOpVal, Mul, MUL); }
222div { RET_TOK(BinaryOpVal, Div, DIV); }
223rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattner42c9e772001-10-20 09:32:59 +0000224and { RET_TOK(BinaryOpVal, And, AND); }
225or { RET_TOK(BinaryOpVal, Or , OR ); }
226xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner00950542001-06-06 20:29:01 +0000227setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
228seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
229setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
230setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
231setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
232setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
233
Chris Lattner3b237fc2003-10-19 21:34:28 +0000234phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
Chris Lattner027dcc52001-07-08 21:10:27 +0000235call { RET_TOK(OtherOpVal, Call, CALL); }
236cast { RET_TOK(OtherOpVal, Cast, CAST); }
Chris Lattner76f0ff32004-03-12 05:51:36 +0000237select { RET_TOK(OtherOpVal, Select, SELECT); }
Chris Lattner027dcc52001-07-08 21:10:27 +0000238shl { RET_TOK(OtherOpVal, Shl, SHL); }
239shr { RET_TOK(OtherOpVal, Shr, SHR); }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000240va_arg { return VA_ARG; /* FIXME: OBSOLETE */}
241vanext { RET_TOK(OtherOpVal, VANext, VANEXT); }
242vaarg { RET_TOK(OtherOpVal, VAArg , VAARG); }
Chris Lattner027dcc52001-07-08 21:10:27 +0000243
Chris Lattner00950542001-06-06 20:29:01 +0000244ret { RET_TOK(TermOpVal, Ret, RET); }
245br { RET_TOK(TermOpVal, Br, BR); }
246switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner5efbbc22001-10-13 06:37:14 +0000247invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner36143fc2003-09-08 18:54:55 +0000248unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattner00950542001-06-06 20:29:01 +0000249
250
251malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
252alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
253free { RET_TOK(MemOpVal, Free, FREE); }
254load { RET_TOK(MemOpVal, Load, LOAD); }
255store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000256getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner00950542001-06-06 20:29:01 +0000257
258
Chris Lattner93750fa2001-07-28 17:48:55 +0000259{VarID} {
260 UnEscapeLexed(yytext+1);
261 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
262 return VAR_ID;
263 }
264{Label} {
Chris Lattner00950542001-06-06 20:29:01 +0000265 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner93750fa2001-07-28 17:48:55 +0000266 UnEscapeLexed(yytext);
267 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner00950542001-06-06 20:29:01 +0000268 return LABELSTR;
269 }
270
Chris Lattner93750fa2001-07-28 17:48:55 +0000271{StringConstant} { // Note that we cannot unescape a string constant here! The
272 // string constant might contain a \00 which would not be
273 // understood by the string stuff. It is valid to make a
274 // [sbyte] c"Hello World\00" constant, for example.
275 //
Chris Lattner00950542001-06-06 20:29:01 +0000276 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner93750fa2001-07-28 17:48:55 +0000277 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner00950542001-06-06 20:29:01 +0000278 return STRINGCONSTANT;
Chris Lattner93750fa2001-07-28 17:48:55 +0000279 }
Chris Lattner00950542001-06-06 20:29:01 +0000280
281
282{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
283{NInteger} {
284 uint64_t Val = atoull(yytext+1);
285 // +1: we have bigger negative range
286 if (Val > (uint64_t)INT64_MAX+1)
287 ThrowException("Constant too large for signed 64 bits!");
288 llvmAsmlval.SInt64Val = -Val;
289 return ESINT64VAL;
290 }
Chris Lattner3e8ba102003-04-17 22:17:32 +0000291{HexIntConstant} {
292 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
293 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
294 }
Chris Lattner00950542001-06-06 20:29:01 +0000295
296{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
297{ENInteger} {
298 uint64_t Val = atoull(yytext+2);
299 // +1: we have bigger negative range
300 if (Val > (uint64_t)INT32_MAX+1)
301 ThrowException("Constant too large for signed 32 bits!");
302 llvmAsmlval.SIntVal = -Val;
303 return SINTVAL;
304 }
305
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000306{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner275da862002-04-07 08:10:41 +0000307{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner00950542001-06-06 20:29:01 +0000308
Chris Lattnere1512592004-03-19 23:34:33 +0000309<<EOF>> {
310 /* Make sure to free the internal buffers for flex when we are
311 * done reading our input!
312 */
313 yy_delete_buffer(YY_CURRENT_BUFFER);
314 return EOF;
315 }
316
Chris Lattner00950542001-06-06 20:29:01 +0000317[ \t\n] { /* Ignore whitespace */ }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000318. { return yytext[0]; }
Chris Lattner00950542001-06-06 20:29:01 +0000319
320%%