blob: 47c067ba8ce7e2736c03ac3b47464226362e1b30 [file] [log] [blame]
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001/*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
3// This file implements the flex scanner for LLVM assembly languages files.
4//
Chris Lattnerdb3b2022002-08-14 17:12:33 +00005//===----------------------------------------------------------------------===*/
Chris Lattner2f7c9632001-06-06 20:29:01 +00006
7%option prefix="llvmAsm"
8%option yylineno
9%option nostdinit
10%option never-interactive
11%option batch
12%option noyywrap
13%option nodefault
14%option 8bit
15%option outfile="Lexer.cpp"
16%option ecs
17%option noreject
18%option noyymore
19
20%{
21#include "ParserInternals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include <list>
23#include "llvmAsmParser.h"
Chris Lattner26e50dc2001-07-28 17:48:55 +000024#include <ctype.h>
25#include <stdlib.h>
Chris Lattner2f7c9632001-06-06 20:29:01 +000026
27#define RET_TOK(type, Enum, sym) \
28 llvmAsmlval.type = Instruction::Enum; return sym
29
30
31// TODO: All of the static identifiers are figured out by the lexer,
Chris Lattner47af30c2001-09-07 16:32:43 +000032// these should be hashed to reduce the lexer size
Chris Lattner2f7c9632001-06-06 20:29:01 +000033
34
35// atoull - Convert an ascii string of decimal digits into the unsigned long
36// long representation... this does not have to do input error checking,
37// because we know that the input will be matched by a suitable regex...
38//
Chris Lattner1b343742002-04-07 08:10:41 +000039static uint64_t atoull(const char *Buffer) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000040 uint64_t Result = 0;
41 for (; *Buffer; Buffer++) {
42 uint64_t OldRes = Result;
43 Result *= 10;
44 Result += *Buffer-'0';
Chris Lattner1b343742002-04-07 08:10:41 +000045 if (Result < OldRes) // Uh, oh, overflow detected!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +000046 ThrowException("constant bigger than 64 bits detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000047 }
48 return Result;
49}
50
Chris Lattnere509bd42003-04-17 22:17:32 +000051static uint64_t HexIntToVal(const char *Buffer) {
Chris Lattner1b343742002-04-07 08:10:41 +000052 uint64_t Result = 0;
53 for (; *Buffer; ++Buffer) {
54 uint64_t OldRes = Result;
55 Result *= 16;
56 char C = *Buffer;
57 if (C >= '0' && C <= '9')
58 Result += C-'0';
59 else if (C >= 'A' && C <= 'F')
60 Result += C-'A'+10;
61 else if (C >= 'a' && C <= 'f')
62 Result += C-'a'+10;
63
64 if (Result < OldRes) // Uh, oh, overflow detected!!!
65 ThrowException("constant bigger than 64 bits detected!");
66 }
Chris Lattnere509bd42003-04-17 22:17:32 +000067 return Result;
68}
69
70
71// HexToFP - Convert the ascii string in hexidecimal format to the floating
72// point representation of it.
73//
74static double HexToFP(const char *Buffer) {
75 uint64_t Result = HexIntToVal(Buffer);
Chris Lattner1b343742002-04-07 08:10:41 +000076
77 assert(sizeof(double) == sizeof(Result) &&
78 "Data sizes incompatible on this target!");
Chris Lattnerb99f4792002-04-07 08:31:26 +000079 // Behave nicely in the face of C TBAA rules... see:
80 // http://www.nullstone.com/htmls/category/aliastyp.htm
81 //
82 char *ProxyPointer = (char*)&Result;
Chris Lattner1b343742002-04-07 08:10:41 +000083 return *(double*)ProxyPointer; // Cast Hex constant to double
84}
85
Chris Lattner2f7c9632001-06-06 20:29:01 +000086
Chris Lattner26e50dc2001-07-28 17:48:55 +000087// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
88// appropriate character. If AllowNull is set to false, a \00 value will cause
89// an exception to be thrown.
90//
91// If AllowNull is set to true, the return value of the function points to the
92// last character of the string in memory.
93//
Chris Lattnere6801392002-07-25 06:17:42 +000094char *UnEscapeLexed(char *Buffer, bool AllowNull) {
Chris Lattner26e50dc2001-07-28 17:48:55 +000095 char *BOut = Buffer;
96 for (char *BIn = Buffer; *BIn; ) {
97 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
98 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
99 *BOut = strtol(BIn+1, 0, 16); // Convert to number
100 if (!AllowNull && !*BOut)
101 ThrowException("String literal cannot accept \\00 escape!");
102
103 BIn[3] = Tmp; // Restore character
104 BIn += 3; // Skip over handled chars
105 ++BOut;
106 } else {
107 *BOut++ = *BIn++;
108 }
109 }
110
111 return BOut;
112}
113
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114#define YY_NEVER_INTERACTIVE 1
115%}
116
117
118
119/* Comments start with a ; and go till end of line */
120Comment ;.*
121
Chris Lattner17f729e2001-07-15 06:35:53 +0000122/* Variable(Value) identifiers start with a % sign */
Chris Lattner1366b302001-12-04 04:31:30 +0000123VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124
125/* Label identifiers end with a colon */
Chris Lattner1366b302001-12-04 04:31:30 +0000126Label [-a-zA-Z$._0-9]+:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127
128/* Quoted names can contain any character except " and \ */
129StringConstant \"[^\"]+\"
130
131
132/* [PN]Integer: match positive and negative literal integer values that
133 * are preceeded by a '%' character. These represent unnamed variable slots.
134 */
135EPInteger %[0-9]+
136ENInteger %-[0-9]+
137
138
139/* E[PN]Integer: match positive and negative literal integer values */
140PInteger [0-9]+
141NInteger -[0-9]+
142
Chris Lattner212f70d2001-07-15 00:17:01 +0000143/* FPConstant - A Floating point constant.
Chris Lattner1b343742002-04-07 08:10:41 +0000144 */
Chris Lattner50f68ac2001-11-01 22:06:08 +0000145FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner212f70d2001-07-15 00:17:01 +0000146
Chris Lattner1b343742002-04-07 08:10:41 +0000147/* HexFPConstant - Floating point constant represented in IEEE format as a
148 * hexadecimal number for when exponential notation is not precise enough.
149 */
150HexFPConstant 0x[0-9A-Fa-f]+
Chris Lattnere509bd42003-04-17 22:17:32 +0000151
152/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
153 * it to deal with 64 bit numbers.
154 */
155HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156%%
157
158{Comment} { /* Ignore comments for now */ }
159
160begin { return BEGINTOK; }
Chris Lattner1e1a9b42002-05-03 18:23:48 +0000161end { return ENDTOK; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162true { return TRUE; }
163false { return FALSE; }
164declare { return DECLARE; }
Chris Lattnerda975502001-09-10 07:58:01 +0000165global { return GLOBAL; }
Chris Lattner7fb14f52001-09-18 04:00:54 +0000166constant { return CONSTANT; }
Chris Lattner571a6b02001-10-03 01:49:25 +0000167const { return CONST; }
Chris Lattner841d8b92001-11-26 18:54:16 +0000168internal { return INTERNAL; }
Chris Lattner379a8d22003-04-16 20:28:45 +0000169linkonce { return LINKONCE; }
170appending { return APPENDING; }
Chris Lattnere2dd92b2002-10-06 22:45:09 +0000171uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
172external { return EXTERNAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173implementation { return IMPLEMENTATION; }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000174\.\.\. { return DOTDOTDOT; }
Chris Lattnerfbdec252001-09-30 22:46:54 +0000175null { return NULL_TOK; }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000176to { return TO; }
177except { return EXCEPT; }
Chris Lattnerdb3b2022002-08-14 17:12:33 +0000178not { return NOT; } /* Deprecated, turned into XOR */
Chris Lattner2f7c9632001-06-06 20:29:01 +0000179
Chris Lattner47af30c2001-09-07 16:32:43 +0000180void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
181bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
182sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
183ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
184short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
185ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
186int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
187uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
188long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
189ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
190float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
191double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000192type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000193label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Chris Lattnerf269b9d2002-04-04 19:22:17 +0000194opaque { return OPAQUE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000195
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196add { RET_TOK(BinaryOpVal, Add, ADD); }
197sub { RET_TOK(BinaryOpVal, Sub, SUB); }
198mul { RET_TOK(BinaryOpVal, Mul, MUL); }
199div { RET_TOK(BinaryOpVal, Div, DIV); }
200rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000201and { RET_TOK(BinaryOpVal, And, AND); }
202or { RET_TOK(BinaryOpVal, Or , OR ); }
203xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000204setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
205seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
206setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
207setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
208setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
209setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
210
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000211phi { RET_TOK(OtherOpVal, PHINode, PHI); }
212call { RET_TOK(OtherOpVal, Call, CALL); }
213cast { RET_TOK(OtherOpVal, Cast, CAST); }
214shl { RET_TOK(OtherOpVal, Shl, SHL); }
215shr { RET_TOK(OtherOpVal, Shr, SHR); }
216
Chris Lattner2f7c9632001-06-06 20:29:01 +0000217ret { RET_TOK(TermOpVal, Ret, RET); }
218br { RET_TOK(TermOpVal, Br, BR); }
219switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000220invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221
222
223malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
224alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
225free { RET_TOK(MemOpVal, Free, FREE); }
226load { RET_TOK(MemOpVal, Load, LOAD); }
227store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000228getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000229
230
Chris Lattner26e50dc2001-07-28 17:48:55 +0000231{VarID} {
232 UnEscapeLexed(yytext+1);
233 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
234 return VAR_ID;
235 }
236{Label} {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner26e50dc2001-07-28 17:48:55 +0000238 UnEscapeLexed(yytext);
239 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000240 return LABELSTR;
241 }
242
Chris Lattner26e50dc2001-07-28 17:48:55 +0000243{StringConstant} { // Note that we cannot unescape a string constant here! The
244 // string constant might contain a \00 which would not be
245 // understood by the string stuff. It is valid to make a
246 // [sbyte] c"Hello World\00" constant, for example.
247 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000248 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner26e50dc2001-07-28 17:48:55 +0000249 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner2f7c9632001-06-06 20:29:01 +0000250 return STRINGCONSTANT;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000251 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000252
253
254{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
255{NInteger} {
256 uint64_t Val = atoull(yytext+1);
257 // +1: we have bigger negative range
258 if (Val > (uint64_t)INT64_MAX+1)
259 ThrowException("Constant too large for signed 64 bits!");
260 llvmAsmlval.SInt64Val = -Val;
261 return ESINT64VAL;
262 }
Chris Lattnere509bd42003-04-17 22:17:32 +0000263{HexIntConstant} {
264 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
265 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
266 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267
268{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
269{ENInteger} {
270 uint64_t Val = atoull(yytext+2);
271 // +1: we have bigger negative range
272 if (Val > (uint64_t)INT32_MAX+1)
273 ThrowException("Constant too large for signed 32 bits!");
274 llvmAsmlval.SIntVal = -Val;
275 return SINTVAL;
276 }
277
Chris Lattner212f70d2001-07-15 00:17:01 +0000278{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner1b343742002-04-07 08:10:41 +0000279{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000280
281[ \t\n] { /* Ignore whitespace */ }
Chris Lattner47af30c2001-09-07 16:32:43 +0000282. { return yytext[0]; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000283
284%%