blob: 6466cb721b760748162442ffa8972d70c281461f [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
38
39// TODO: All of the static identifiers are figured out by the lexer,
Chris Lattner47af30c2001-09-07 16:32:43 +000040// these should be hashed to reduce the lexer size
Chris Lattner2f7c9632001-06-06 20:29:01 +000041
42
43// atoull - Convert an ascii string of decimal digits into the unsigned long
44// long representation... this does not have to do input error checking,
45// because we know that the input will be matched by a suitable regex...
46//
Chris Lattner1b343742002-04-07 08:10:41 +000047static uint64_t atoull(const char *Buffer) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000048 uint64_t Result = 0;
49 for (; *Buffer; Buffer++) {
50 uint64_t OldRes = Result;
51 Result *= 10;
52 Result += *Buffer-'0';
Chris Lattner1b343742002-04-07 08:10:41 +000053 if (Result < OldRes) // Uh, oh, overflow detected!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +000054 ThrowException("constant bigger than 64 bits detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000055 }
56 return Result;
57}
58
Chris Lattnere509bd42003-04-17 22:17:32 +000059static uint64_t HexIntToVal(const char *Buffer) {
Chris Lattner1b343742002-04-07 08:10:41 +000060 uint64_t Result = 0;
61 for (; *Buffer; ++Buffer) {
62 uint64_t OldRes = Result;
63 Result *= 16;
64 char C = *Buffer;
65 if (C >= '0' && C <= '9')
66 Result += C-'0';
67 else if (C >= 'A' && C <= 'F')
68 Result += C-'A'+10;
69 else if (C >= 'a' && C <= 'f')
70 Result += C-'a'+10;
71
72 if (Result < OldRes) // Uh, oh, overflow detected!!!
73 ThrowException("constant bigger than 64 bits detected!");
74 }
Chris Lattnere509bd42003-04-17 22:17:32 +000075 return Result;
76}
77
78
79// HexToFP - Convert the ascii string in hexidecimal format to the floating
80// point representation of it.
81//
82static double HexToFP(const char *Buffer) {
Chris Lattnerb99f4792002-04-07 08:31:26 +000083 // Behave nicely in the face of C TBAA rules... see:
84 // http://www.nullstone.com/htmls/category/aliastyp.htm
Chris Lattner62fa7432003-04-22 20:20:28 +000085 union {
86 uint64_t UI;
87 double FP;
88 } UIntToFP;
89 UIntToFP.UI = HexIntToVal(Buffer);
90
91 assert(sizeof(double) == sizeof(uint64_t) &&
92 "Data sizes incompatible on this target!");
93 return UIntToFP.FP; // Cast Hex constant to double
Chris Lattner1b343742002-04-07 08:10:41 +000094}
95
Chris Lattner2f7c9632001-06-06 20:29:01 +000096
Chris Lattner26e50dc2001-07-28 17:48:55 +000097// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
98// appropriate character. If AllowNull is set to false, a \00 value will cause
99// an exception to be thrown.
100//
101// If AllowNull is set to true, the return value of the function points to the
102// last character of the string in memory.
103//
Chris Lattnere6801392002-07-25 06:17:42 +0000104char *UnEscapeLexed(char *Buffer, bool AllowNull) {
Chris Lattner26e50dc2001-07-28 17:48:55 +0000105 char *BOut = Buffer;
106 for (char *BIn = Buffer; *BIn; ) {
107 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
108 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
109 *BOut = strtol(BIn+1, 0, 16); // Convert to number
110 if (!AllowNull && !*BOut)
111 ThrowException("String literal cannot accept \\00 escape!");
112
113 BIn[3] = Tmp; // Restore character
114 BIn += 3; // Skip over handled chars
115 ++BOut;
116 } else {
117 *BOut++ = *BIn++;
118 }
119 }
120
121 return BOut;
122}
123
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124#define YY_NEVER_INTERACTIVE 1
125%}
126
127
128
129/* Comments start with a ; and go till end of line */
130Comment ;.*
131
Chris Lattner17f729e2001-07-15 06:35:53 +0000132/* Variable(Value) identifiers start with a % sign */
Chris Lattner1366b302001-12-04 04:31:30 +0000133VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000134
135/* Label identifiers end with a colon */
Chris Lattner1366b302001-12-04 04:31:30 +0000136Label [-a-zA-Z$._0-9]+:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000137
138/* Quoted names can contain any character except " and \ */
Chris Lattner0079e7d2003-10-18 05:53:13 +0000139StringConstant \"[^\"]*\"
Chris Lattner2f7c9632001-06-06 20:29:01 +0000140
141
142/* [PN]Integer: match positive and negative literal integer values that
143 * are preceeded by a '%' character. These represent unnamed variable slots.
144 */
145EPInteger %[0-9]+
146ENInteger %-[0-9]+
147
148
149/* E[PN]Integer: match positive and negative literal integer values */
150PInteger [0-9]+
151NInteger -[0-9]+
152
Chris Lattner212f70d2001-07-15 00:17:01 +0000153/* FPConstant - A Floating point constant.
Chris Lattner1b343742002-04-07 08:10:41 +0000154 */
Chris Lattner50f68ac2001-11-01 22:06:08 +0000155FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner212f70d2001-07-15 00:17:01 +0000156
Chris Lattner1b343742002-04-07 08:10:41 +0000157/* HexFPConstant - Floating point constant represented in IEEE format as a
158 * hexadecimal number for when exponential notation is not precise enough.
159 */
160HexFPConstant 0x[0-9A-Fa-f]+
Chris Lattnere509bd42003-04-17 22:17:32 +0000161
162/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
163 * it to deal with 64 bit numbers.
164 */
165HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166%%
167
168{Comment} { /* Ignore comments for now */ }
169
170begin { return BEGINTOK; }
Chris Lattner1e1a9b42002-05-03 18:23:48 +0000171end { return ENDTOK; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000172true { return TRUE; }
173false { return FALSE; }
174declare { return DECLARE; }
Chris Lattnerda975502001-09-10 07:58:01 +0000175global { return GLOBAL; }
Chris Lattner7fb14f52001-09-18 04:00:54 +0000176constant { return CONSTANT; }
Chris Lattner571a6b02001-10-03 01:49:25 +0000177const { return CONST; }
Chris Lattner841d8b92001-11-26 18:54:16 +0000178internal { return INTERNAL; }
Chris Lattner379a8d22003-04-16 20:28:45 +0000179linkonce { return LINKONCE; }
Chris Lattnerf4120d92003-10-10 04:54:02 +0000180weak { return WEAK; }
Chris Lattner379a8d22003-04-16 20:28:45 +0000181appending { return APPENDING; }
Chris Lattnere2dd92b2002-10-06 22:45:09 +0000182uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
183external { return EXTERNAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184implementation { return IMPLEMENTATION; }
Chris Lattner79694012003-06-28 20:01:34 +0000185zeroinitializer { return ZEROINITIALIZER; }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000186\.\.\. { return DOTDOTDOT; }
Chris Lattnerfbdec252001-09-30 22:46:54 +0000187null { return NULL_TOK; }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000188to { return TO; }
189except { return EXCEPT; }
Chris Lattnerdb3b2022002-08-14 17:12:33 +0000190not { return NOT; } /* Deprecated, turned into XOR */
Chris Lattner20126132003-04-22 19:07:06 +0000191target { return TARGET; }
192endian { return ENDIAN; }
193pointersize { return POINTERSIZE; }
194little { return LITTLE; }
195big { return BIG; }
Chris Lattner8b1680e2003-09-08 18:20:29 +0000196volatile { return VOLATILE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000197
Chris Lattner47af30c2001-09-07 16:32:43 +0000198void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
199bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
200sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
201ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
202short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
203ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
204int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
205uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
206long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
207ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
208float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
209double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000210type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000211label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Chris Lattnerf269b9d2002-04-04 19:22:17 +0000212opaque { return OPAQUE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214add { RET_TOK(BinaryOpVal, Add, ADD); }
215sub { RET_TOK(BinaryOpVal, Sub, SUB); }
216mul { RET_TOK(BinaryOpVal, Mul, MUL); }
217div { RET_TOK(BinaryOpVal, Div, DIV); }
218rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000219and { RET_TOK(BinaryOpVal, And, AND); }
220or { RET_TOK(BinaryOpVal, Or , OR ); }
221xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
223seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
224setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
225setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
226setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
227setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
228
Chris Lattnerb94550e2003-10-19 21:34:28 +0000229phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000230call { RET_TOK(OtherOpVal, Call, CALL); }
231cast { RET_TOK(OtherOpVal, Cast, CAST); }
232shl { RET_TOK(OtherOpVal, Shl, SHL); }
233shr { RET_TOK(OtherOpVal, Shr, SHR); }
Chris Lattner0079e7d2003-10-18 05:53:13 +0000234va_arg { return VA_ARG; /* FIXME: OBSOLETE */}
235vanext { RET_TOK(OtherOpVal, VANext, VANEXT); }
236vaarg { RET_TOK(OtherOpVal, VAArg , VAARG); }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000237
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238ret { RET_TOK(TermOpVal, Ret, RET); }
239br { RET_TOK(TermOpVal, Br, BR); }
240switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000241invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner9c58cf62003-09-08 18:54:55 +0000242unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243
244
245malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
246alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
247free { RET_TOK(MemOpVal, Free, FREE); }
248load { RET_TOK(MemOpVal, Load, LOAD); }
249store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000250getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000251
252
Chris Lattner26e50dc2001-07-28 17:48:55 +0000253{VarID} {
254 UnEscapeLexed(yytext+1);
255 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
256 return VAR_ID;
257 }
258{Label} {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000259 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner26e50dc2001-07-28 17:48:55 +0000260 UnEscapeLexed(yytext);
261 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000262 return LABELSTR;
263 }
264
Chris Lattner26e50dc2001-07-28 17:48:55 +0000265{StringConstant} { // Note that we cannot unescape a string constant here! The
266 // string constant might contain a \00 which would not be
267 // understood by the string stuff. It is valid to make a
268 // [sbyte] c"Hello World\00" constant, for example.
269 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000270 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner26e50dc2001-07-28 17:48:55 +0000271 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner2f7c9632001-06-06 20:29:01 +0000272 return STRINGCONSTANT;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000273 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000274
275
276{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
277{NInteger} {
278 uint64_t Val = atoull(yytext+1);
279 // +1: we have bigger negative range
280 if (Val > (uint64_t)INT64_MAX+1)
281 ThrowException("Constant too large for signed 64 bits!");
282 llvmAsmlval.SInt64Val = -Val;
283 return ESINT64VAL;
284 }
Chris Lattnere509bd42003-04-17 22:17:32 +0000285{HexIntConstant} {
286 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
287 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
288 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000289
290{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
291{ENInteger} {
292 uint64_t Val = atoull(yytext+2);
293 // +1: we have bigger negative range
294 if (Val > (uint64_t)INT32_MAX+1)
295 ThrowException("Constant too large for signed 32 bits!");
296 llvmAsmlval.SIntVal = -Val;
297 return SINTVAL;
298 }
299
Chris Lattner212f70d2001-07-15 00:17:01 +0000300{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner1b343742002-04-07 08:10:41 +0000301{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000302
303[ \t\n] { /* Ignore whitespace */ }
Chris Lattner47af30c2001-09-07 16:32:43 +0000304. { return yytext[0]; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000305
306%%