blob: f0f007b4b3a1fb6ad59940d12dc50198853a7ea9 [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 Lattner20126132003-04-22 19:07:06 +000022#include "llvm/Module.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000023#include <list>
24#include "llvmAsmParser.h"
Brian Gaekebc127092003-10-10 19:12:08 +000025#include <cctype>
26#include <cstdlib>
Chris Lattner2f7c9632001-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 Lattner47af30c2001-09-07 16:32:43 +000033// these should be hashed to reduce the lexer size
Chris Lattner2f7c9632001-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 Lattner1b343742002-04-07 08:10:41 +000040static uint64_t atoull(const char *Buffer) {
Chris Lattner2f7c9632001-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 Lattner1b343742002-04-07 08:10:41 +000046 if (Result < OldRes) // Uh, oh, overflow detected!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +000047 ThrowException("constant bigger than 64 bits detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000048 }
49 return Result;
50}
51
Chris Lattnere509bd42003-04-17 22:17:32 +000052static uint64_t HexIntToVal(const char *Buffer) {
Chris Lattner1b343742002-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 Lattnere509bd42003-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 Lattnerb99f4792002-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 Lattner62fa7432003-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 Lattner1b343742002-04-07 08:10:41 +000087}
88
Chris Lattner2f7c9632001-06-06 20:29:01 +000089
Chris Lattner26e50dc2001-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 Lattnere6801392002-07-25 06:17:42 +000097char *UnEscapeLexed(char *Buffer, bool AllowNull) {
Chris Lattner26e50dc2001-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 Lattner2f7c9632001-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 Lattner17f729e2001-07-15 06:35:53 +0000125/* Variable(Value) identifiers start with a % sign */
Chris Lattner1366b302001-12-04 04:31:30 +0000126VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127
128/* Label identifiers end with a colon */
Chris Lattner1366b302001-12-04 04:31:30 +0000129Label [-a-zA-Z$._0-9]+:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130
131/* Quoted names can contain any character except " and \ */
Chris Lattner0079e7d2003-10-18 05:53:13 +0000132StringConstant \"[^\"]*\"
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133
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 Lattner212f70d2001-07-15 00:17:01 +0000146/* FPConstant - A Floating point constant.
Chris Lattner1b343742002-04-07 08:10:41 +0000147 */
Chris Lattner50f68ac2001-11-01 22:06:08 +0000148FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner212f70d2001-07-15 00:17:01 +0000149
Chris Lattner1b343742002-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 Lattnere509bd42003-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 Lattner2f7c9632001-06-06 20:29:01 +0000159%%
160
161{Comment} { /* Ignore comments for now */ }
162
163begin { return BEGINTOK; }
Chris Lattner1e1a9b42002-05-03 18:23:48 +0000164end { return ENDTOK; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000165true { return TRUE; }
166false { return FALSE; }
167declare { return DECLARE; }
Chris Lattnerda975502001-09-10 07:58:01 +0000168global { return GLOBAL; }
Chris Lattner7fb14f52001-09-18 04:00:54 +0000169constant { return CONSTANT; }
Chris Lattner571a6b02001-10-03 01:49:25 +0000170const { return CONST; }
Chris Lattner841d8b92001-11-26 18:54:16 +0000171internal { return INTERNAL; }
Chris Lattner379a8d22003-04-16 20:28:45 +0000172linkonce { return LINKONCE; }
Chris Lattnerf4120d92003-10-10 04:54:02 +0000173weak { return WEAK; }
Chris Lattner379a8d22003-04-16 20:28:45 +0000174appending { return APPENDING; }
Chris Lattnere2dd92b2002-10-06 22:45:09 +0000175uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
176external { return EXTERNAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000177implementation { return IMPLEMENTATION; }
Chris Lattner79694012003-06-28 20:01:34 +0000178zeroinitializer { return ZEROINITIALIZER; }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000179\.\.\. { return DOTDOTDOT; }
Chris Lattnerfbdec252001-09-30 22:46:54 +0000180null { return NULL_TOK; }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000181to { return TO; }
182except { return EXCEPT; }
Chris Lattnerdb3b2022002-08-14 17:12:33 +0000183not { return NOT; } /* Deprecated, turned into XOR */
Chris Lattner20126132003-04-22 19:07:06 +0000184target { return TARGET; }
185endian { return ENDIAN; }
186pointersize { return POINTERSIZE; }
187little { return LITTLE; }
188big { return BIG; }
Chris Lattner8b1680e2003-09-08 18:20:29 +0000189volatile { return VOLATILE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190
Chris Lattner47af30c2001-09-07 16:32:43 +0000191void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
192bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
193sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
194ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
195short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
196ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
197int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
198uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
199long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
200ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
201float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
202double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000203type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000204label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Chris Lattnerf269b9d2002-04-04 19:22:17 +0000205opaque { return OPAQUE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207add { RET_TOK(BinaryOpVal, Add, ADD); }
208sub { RET_TOK(BinaryOpVal, Sub, SUB); }
209mul { RET_TOK(BinaryOpVal, Mul, MUL); }
210div { RET_TOK(BinaryOpVal, Div, DIV); }
211rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000212and { RET_TOK(BinaryOpVal, And, AND); }
213or { RET_TOK(BinaryOpVal, Or , OR ); }
214xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
216seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
217setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
218setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
219setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
220setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
221
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000222phi { RET_TOK(OtherOpVal, PHINode, PHI); }
223call { RET_TOK(OtherOpVal, Call, CALL); }
224cast { RET_TOK(OtherOpVal, Cast, CAST); }
225shl { RET_TOK(OtherOpVal, Shl, SHL); }
226shr { RET_TOK(OtherOpVal, Shr, SHR); }
Chris Lattner0079e7d2003-10-18 05:53:13 +0000227va_arg { return VA_ARG; /* FIXME: OBSOLETE */}
228vanext { RET_TOK(OtherOpVal, VANext, VANEXT); }
229vaarg { RET_TOK(OtherOpVal, VAArg , VAARG); }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000230
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231ret { RET_TOK(TermOpVal, Ret, RET); }
232br { RET_TOK(TermOpVal, Br, BR); }
233switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000234invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner9c58cf62003-09-08 18:54:55 +0000235unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000236
237
238malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
239alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
240free { RET_TOK(MemOpVal, Free, FREE); }
241load { RET_TOK(MemOpVal, Load, LOAD); }
242store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000243getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244
245
Chris Lattner26e50dc2001-07-28 17:48:55 +0000246{VarID} {
247 UnEscapeLexed(yytext+1);
248 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
249 return VAR_ID;
250 }
251{Label} {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000252 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner26e50dc2001-07-28 17:48:55 +0000253 UnEscapeLexed(yytext);
254 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000255 return LABELSTR;
256 }
257
Chris Lattner26e50dc2001-07-28 17:48:55 +0000258{StringConstant} { // Note that we cannot unescape a string constant here! The
259 // string constant might contain a \00 which would not be
260 // understood by the string stuff. It is valid to make a
261 // [sbyte] c"Hello World\00" constant, for example.
262 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000263 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner26e50dc2001-07-28 17:48:55 +0000264 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265 return STRINGCONSTANT;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000266 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267
268
269{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
270{NInteger} {
271 uint64_t Val = atoull(yytext+1);
272 // +1: we have bigger negative range
273 if (Val > (uint64_t)INT64_MAX+1)
274 ThrowException("Constant too large for signed 64 bits!");
275 llvmAsmlval.SInt64Val = -Val;
276 return ESINT64VAL;
277 }
Chris Lattnere509bd42003-04-17 22:17:32 +0000278{HexIntConstant} {
279 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
280 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
281 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000282
283{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
284{ENInteger} {
285 uint64_t Val = atoull(yytext+2);
286 // +1: we have bigger negative range
287 if (Val > (uint64_t)INT32_MAX+1)
288 ThrowException("Constant too large for signed 32 bits!");
289 llvmAsmlval.SIntVal = -Val;
290 return SINTVAL;
291 }
292
Chris Lattner212f70d2001-07-15 00:17:01 +0000293{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner1b343742002-04-07 08:10:41 +0000294{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295
296[ \t\n] { /* Ignore whitespace */ }
Chris Lattner47af30c2001-09-07 16:32:43 +0000297. { return yytext[0]; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000298
299%%