blob: 6e69262c5f27310ca60bf62acbda207511ad0ab0 [file] [log] [blame]
Chris Lattner699f1eb2002-08-14 17:12:33 +00001/*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file implements the flex scanner for LLVM assembly languages files.
4//
Chris Lattner699f1eb2002-08-14 17:12:33 +00005//===----------------------------------------------------------------------===*/
Chris Lattner00950542001-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 Lattnerb9bcbb52003-04-22 19:07:06 +000022#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000023#include <list>
24#include "llvmAsmParser.h"
Chris Lattner93750fa2001-07-28 17:48:55 +000025#include <ctype.h>
John Criswell7a73b802003-06-30 21:59:07 +000026#include "Config/stdlib.h"
Chris Lattner00950542001-06-06 20:29:01 +000027
28#define RET_TOK(type, Enum, sym) \
29 llvmAsmlval.type = Instruction::Enum; return sym
30
31
32// TODO: All of the static identifiers are figured out by the lexer,
Chris Lattnere1fe8752001-09-07 16:32:43 +000033// these should be hashed to reduce the lexer size
Chris Lattner00950542001-06-06 20:29:01 +000034
35
36// atoull - Convert an ascii string of decimal digits into the unsigned long
37// long representation... this does not have to do input error checking,
38// because we know that the input will be matched by a suitable regex...
39//
Chris Lattner275da862002-04-07 08:10:41 +000040static uint64_t atoull(const char *Buffer) {
Chris Lattner00950542001-06-06 20:29:01 +000041 uint64_t Result = 0;
42 for (; *Buffer; Buffer++) {
43 uint64_t OldRes = Result;
44 Result *= 10;
45 Result += *Buffer-'0';
Chris Lattner275da862002-04-07 08:10:41 +000046 if (Result < OldRes) // Uh, oh, overflow detected!!!
Chris Lattner00950542001-06-06 20:29:01 +000047 ThrowException("constant bigger than 64 bits detected!");
Chris Lattner00950542001-06-06 20:29:01 +000048 }
49 return Result;
50}
51
Chris Lattner3e8ba102003-04-17 22:17:32 +000052static uint64_t HexIntToVal(const char *Buffer) {
Chris Lattner275da862002-04-07 08:10:41 +000053 uint64_t Result = 0;
54 for (; *Buffer; ++Buffer) {
55 uint64_t OldRes = Result;
56 Result *= 16;
57 char C = *Buffer;
58 if (C >= '0' && C <= '9')
59 Result += C-'0';
60 else if (C >= 'A' && C <= 'F')
61 Result += C-'A'+10;
62 else if (C >= 'a' && C <= 'f')
63 Result += C-'a'+10;
64
65 if (Result < OldRes) // Uh, oh, overflow detected!!!
66 ThrowException("constant bigger than 64 bits detected!");
67 }
Chris Lattner3e8ba102003-04-17 22:17:32 +000068 return Result;
69}
70
71
72// HexToFP - Convert the ascii string in hexidecimal format to the floating
73// point representation of it.
74//
75static double HexToFP(const char *Buffer) {
Chris Lattnerb55679d2002-04-07 08:31:26 +000076 // Behave nicely in the face of C TBAA rules... see:
77 // http://www.nullstone.com/htmls/category/aliastyp.htm
Chris Lattner7a5a1f72003-04-22 20:20:28 +000078 union {
79 uint64_t UI;
80 double FP;
81 } UIntToFP;
82 UIntToFP.UI = HexIntToVal(Buffer);
83
84 assert(sizeof(double) == sizeof(uint64_t) &&
85 "Data sizes incompatible on this target!");
86 return UIntToFP.FP; // Cast Hex constant to double
Chris Lattner275da862002-04-07 08:10:41 +000087}
88
Chris Lattner00950542001-06-06 20:29:01 +000089
Chris Lattner93750fa2001-07-28 17:48:55 +000090// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
91// appropriate character. If AllowNull is set to false, a \00 value will cause
92// an exception to be thrown.
93//
94// If AllowNull is set to true, the return value of the function points to the
95// last character of the string in memory.
96//
Chris Lattnerbcafcce2002-07-25 06:17:42 +000097char *UnEscapeLexed(char *Buffer, bool AllowNull) {
Chris Lattner93750fa2001-07-28 17:48:55 +000098 char *BOut = Buffer;
99 for (char *BIn = Buffer; *BIn; ) {
100 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
101 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
102 *BOut = strtol(BIn+1, 0, 16); // Convert to number
103 if (!AllowNull && !*BOut)
104 ThrowException("String literal cannot accept \\00 escape!");
105
106 BIn[3] = Tmp; // Restore character
107 BIn += 3; // Skip over handled chars
108 ++BOut;
109 } else {
110 *BOut++ = *BIn++;
111 }
112 }
113
114 return BOut;
115}
116
Chris Lattner00950542001-06-06 20:29:01 +0000117#define YY_NEVER_INTERACTIVE 1
118%}
119
120
121
122/* Comments start with a ; and go till end of line */
123Comment ;.*
124
Chris Lattnere1815642001-07-15 06:35:53 +0000125/* Variable(Value) identifiers start with a % sign */
Chris Lattner9a88d272001-12-04 04:31:30 +0000126VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner00950542001-06-06 20:29:01 +0000127
128/* Label identifiers end with a colon */
Chris Lattner9a88d272001-12-04 04:31:30 +0000129Label [-a-zA-Z$._0-9]+:
Chris Lattner00950542001-06-06 20:29:01 +0000130
131/* Quoted names can contain any character except " and \ */
132StringConstant \"[^\"]+\"
133
134
135/* [PN]Integer: match positive and negative literal integer values that
136 * are preceeded by a '%' character. These represent unnamed variable slots.
137 */
138EPInteger %[0-9]+
139ENInteger %-[0-9]+
140
141
142/* E[PN]Integer: match positive and negative literal integer values */
143PInteger [0-9]+
144NInteger -[0-9]+
145
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000146/* FPConstant - A Floating point constant.
Chris Lattner275da862002-04-07 08:10:41 +0000147 */
Chris Lattner1e2c6142001-11-01 22:06:08 +0000148FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000149
Chris Lattner275da862002-04-07 08:10:41 +0000150/* HexFPConstant - Floating point constant represented in IEEE format as a
151 * hexadecimal number for when exponential notation is not precise enough.
152 */
153HexFPConstant 0x[0-9A-Fa-f]+
Chris Lattner3e8ba102003-04-17 22:17:32 +0000154
155/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
156 * it to deal with 64 bit numbers.
157 */
158HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattner00950542001-06-06 20:29:01 +0000159%%
160
161{Comment} { /* Ignore comments for now */ }
162
163begin { return BEGINTOK; }
Chris Lattner9b02cc32002-05-03 18:23:48 +0000164end { return ENDTOK; }
Chris Lattner00950542001-06-06 20:29:01 +0000165true { return TRUE; }
166false { return FALSE; }
167declare { return DECLARE; }
Chris Lattner70cc3392001-09-10 07:58:01 +0000168global { return GLOBAL; }
Chris Lattner1781aca2001-09-18 04:00:54 +0000169constant { return CONSTANT; }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000170const { return CONST; }
Chris Lattnerdda71962001-11-26 18:54:16 +0000171internal { return INTERNAL; }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000172linkonce { return LINKONCE; }
173appending { return APPENDING; }
Chris Lattner08c0e6a2002-10-06 22:45:09 +0000174uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
175external { return EXTERNAL; }
Chris Lattner00950542001-06-06 20:29:01 +0000176implementation { return IMPLEMENTATION; }
Chris Lattnerf4600482003-06-28 20:01:34 +0000177zeroinitializer { return ZEROINITIALIZER; }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000178\.\.\. { return DOTDOTDOT; }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000179null { return NULL_TOK; }
Chris Lattner5efbbc22001-10-13 06:37:14 +0000180to { return TO; }
181except { return EXCEPT; }
Chris Lattner699f1eb2002-08-14 17:12:33 +0000182not { return NOT; } /* Deprecated, turned into XOR */
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000183target { return TARGET; }
184endian { return ENDIAN; }
185pointersize { return POINTERSIZE; }
186little { return LITTLE; }
187big { return BIG; }
Chris Lattner15c9c032003-09-08 18:20:29 +0000188volatile { return VOLATILE; }
Chris Lattner00950542001-06-06 20:29:01 +0000189
Chris Lattnere1fe8752001-09-07 16:32:43 +0000190void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
191bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
192sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
193ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
194short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
195ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
196int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
197uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
198long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
199ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
200float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
201double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000202type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000203label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Chris Lattner4e4cae82002-04-04 19:22:17 +0000204opaque { return OPAQUE; }
Chris Lattner00950542001-06-06 20:29:01 +0000205
Chris Lattner00950542001-06-06 20:29:01 +0000206add { RET_TOK(BinaryOpVal, Add, ADD); }
207sub { RET_TOK(BinaryOpVal, Sub, SUB); }
208mul { RET_TOK(BinaryOpVal, Mul, MUL); }
209div { RET_TOK(BinaryOpVal, Div, DIV); }
210rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattner42c9e772001-10-20 09:32:59 +0000211and { RET_TOK(BinaryOpVal, And, AND); }
212or { RET_TOK(BinaryOpVal, Or , OR ); }
213xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner00950542001-06-06 20:29:01 +0000214setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
215seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
216setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
217setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
218setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
219setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
220
Chris Lattner027dcc52001-07-08 21:10:27 +0000221phi { RET_TOK(OtherOpVal, PHINode, PHI); }
222call { RET_TOK(OtherOpVal, Call, CALL); }
223cast { RET_TOK(OtherOpVal, Cast, CAST); }
224shl { RET_TOK(OtherOpVal, Shl, SHL); }
225shr { RET_TOK(OtherOpVal, Shr, SHR); }
Chris Lattner8f77dae2003-05-08 02:44:12 +0000226va_arg { RET_TOK(OtherOpVal, VarArg, VA_ARG); }
Chris Lattner027dcc52001-07-08 21:10:27 +0000227
Chris Lattner00950542001-06-06 20:29:01 +0000228ret { RET_TOK(TermOpVal, Ret, RET); }
229br { RET_TOK(TermOpVal, Br, BR); }
230switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner5efbbc22001-10-13 06:37:14 +0000231invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner36143fc2003-09-08 18:54:55 +0000232unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattner00950542001-06-06 20:29:01 +0000233
234
235malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
236alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
237free { RET_TOK(MemOpVal, Free, FREE); }
238load { RET_TOK(MemOpVal, Load, LOAD); }
239store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000240getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner00950542001-06-06 20:29:01 +0000241
242
Chris Lattner93750fa2001-07-28 17:48:55 +0000243{VarID} {
244 UnEscapeLexed(yytext+1);
245 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
246 return VAR_ID;
247 }
248{Label} {
Chris Lattner00950542001-06-06 20:29:01 +0000249 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner93750fa2001-07-28 17:48:55 +0000250 UnEscapeLexed(yytext);
251 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner00950542001-06-06 20:29:01 +0000252 return LABELSTR;
253 }
254
Chris Lattner93750fa2001-07-28 17:48:55 +0000255{StringConstant} { // Note that we cannot unescape a string constant here! The
256 // string constant might contain a \00 which would not be
257 // understood by the string stuff. It is valid to make a
258 // [sbyte] c"Hello World\00" constant, for example.
259 //
Chris Lattner00950542001-06-06 20:29:01 +0000260 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner93750fa2001-07-28 17:48:55 +0000261 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner00950542001-06-06 20:29:01 +0000262 return STRINGCONSTANT;
Chris Lattner93750fa2001-07-28 17:48:55 +0000263 }
Chris Lattner00950542001-06-06 20:29:01 +0000264
265
266{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
267{NInteger} {
268 uint64_t Val = atoull(yytext+1);
269 // +1: we have bigger negative range
270 if (Val > (uint64_t)INT64_MAX+1)
271 ThrowException("Constant too large for signed 64 bits!");
272 llvmAsmlval.SInt64Val = -Val;
273 return ESINT64VAL;
274 }
Chris Lattner3e8ba102003-04-17 22:17:32 +0000275{HexIntConstant} {
276 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
277 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
278 }
Chris Lattner00950542001-06-06 20:29:01 +0000279
280{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
281{ENInteger} {
282 uint64_t Val = atoull(yytext+2);
283 // +1: we have bigger negative range
284 if (Val > (uint64_t)INT32_MAX+1)
285 ThrowException("Constant too large for signed 32 bits!");
286 llvmAsmlval.SIntVal = -Val;
287 return SINTVAL;
288 }
289
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000290{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner275da862002-04-07 08:10:41 +0000291{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner00950542001-06-06 20:29:01 +0000292
293[ \t\n] { /* Ignore whitespace */ }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000294. { return yytext[0]; }
Chris Lattner00950542001-06-06 20:29:01 +0000295
296%%