blob: 92d4f3424f87b938b0912f2b12e7d2463b66364e [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>
26#include <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) {
76 uint64_t Result = HexIntToVal(Buffer);
Chris Lattner275da862002-04-07 08:10:41 +000077
78 assert(sizeof(double) == sizeof(Result) &&
79 "Data sizes incompatible on this target!");
Chris Lattnerb55679d2002-04-07 08:31:26 +000080 // Behave nicely in the face of C TBAA rules... see:
81 // http://www.nullstone.com/htmls/category/aliastyp.htm
82 //
83 char *ProxyPointer = (char*)&Result;
Chris Lattner275da862002-04-07 08:10:41 +000084 return *(double*)ProxyPointer; // Cast Hex constant to double
85}
86
Chris Lattner00950542001-06-06 20:29:01 +000087
Chris Lattner93750fa2001-07-28 17:48:55 +000088// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
89// appropriate character. If AllowNull is set to false, a \00 value will cause
90// an exception to be thrown.
91//
92// If AllowNull is set to true, the return value of the function points to the
93// last character of the string in memory.
94//
Chris Lattnerbcafcce2002-07-25 06:17:42 +000095char *UnEscapeLexed(char *Buffer, bool AllowNull) {
Chris Lattner93750fa2001-07-28 17:48:55 +000096 char *BOut = Buffer;
97 for (char *BIn = Buffer; *BIn; ) {
98 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
99 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
100 *BOut = strtol(BIn+1, 0, 16); // Convert to number
101 if (!AllowNull && !*BOut)
102 ThrowException("String literal cannot accept \\00 escape!");
103
104 BIn[3] = Tmp; // Restore character
105 BIn += 3; // Skip over handled chars
106 ++BOut;
107 } else {
108 *BOut++ = *BIn++;
109 }
110 }
111
112 return BOut;
113}
114
Chris Lattner00950542001-06-06 20:29:01 +0000115#define YY_NEVER_INTERACTIVE 1
116%}
117
118
119
120/* Comments start with a ; and go till end of line */
121Comment ;.*
122
Chris Lattnere1815642001-07-15 06:35:53 +0000123/* Variable(Value) identifiers start with a % sign */
Chris Lattner9a88d272001-12-04 04:31:30 +0000124VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner00950542001-06-06 20:29:01 +0000125
126/* Label identifiers end with a colon */
Chris Lattner9a88d272001-12-04 04:31:30 +0000127Label [-a-zA-Z$._0-9]+:
Chris Lattner00950542001-06-06 20:29:01 +0000128
129/* Quoted names can contain any character except " and \ */
130StringConstant \"[^\"]+\"
131
132
133/* [PN]Integer: match positive and negative literal integer values that
134 * are preceeded by a '%' character. These represent unnamed variable slots.
135 */
136EPInteger %[0-9]+
137ENInteger %-[0-9]+
138
139
140/* E[PN]Integer: match positive and negative literal integer values */
141PInteger [0-9]+
142NInteger -[0-9]+
143
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000144/* FPConstant - A Floating point constant.
Chris Lattner275da862002-04-07 08:10:41 +0000145 */
Chris Lattner1e2c6142001-11-01 22:06:08 +0000146FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000147
Chris Lattner275da862002-04-07 08:10:41 +0000148/* HexFPConstant - Floating point constant represented in IEEE format as a
149 * hexadecimal number for when exponential notation is not precise enough.
150 */
151HexFPConstant 0x[0-9A-Fa-f]+
Chris Lattner3e8ba102003-04-17 22:17:32 +0000152
153/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
154 * it to deal with 64 bit numbers.
155 */
156HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattner00950542001-06-06 20:29:01 +0000157%%
158
159{Comment} { /* Ignore comments for now */ }
160
161begin { return BEGINTOK; }
Chris Lattner9b02cc32002-05-03 18:23:48 +0000162end { return ENDTOK; }
Chris Lattner00950542001-06-06 20:29:01 +0000163true { return TRUE; }
164false { return FALSE; }
165declare { return DECLARE; }
Chris Lattner70cc3392001-09-10 07:58:01 +0000166global { return GLOBAL; }
Chris Lattner1781aca2001-09-18 04:00:54 +0000167constant { return CONSTANT; }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000168const { return CONST; }
Chris Lattnerdda71962001-11-26 18:54:16 +0000169internal { return INTERNAL; }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000170linkonce { return LINKONCE; }
171appending { return APPENDING; }
Chris Lattner08c0e6a2002-10-06 22:45:09 +0000172uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
173external { return EXTERNAL; }
Chris Lattner00950542001-06-06 20:29:01 +0000174implementation { return IMPLEMENTATION; }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000175\.\.\. { return DOTDOTDOT; }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000176null { return NULL_TOK; }
Chris Lattner5efbbc22001-10-13 06:37:14 +0000177to { return TO; }
178except { return EXCEPT; }
Chris Lattner699f1eb2002-08-14 17:12:33 +0000179not { return NOT; } /* Deprecated, turned into XOR */
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000180target { return TARGET; }
181endian { return ENDIAN; }
182pointersize { return POINTERSIZE; }
183little { return LITTLE; }
184big { return BIG; }
Chris Lattner00950542001-06-06 20:29:01 +0000185
Chris Lattnere1fe8752001-09-07 16:32:43 +0000186void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
187bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
188sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
189ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
190short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
191ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
192int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
193uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
194long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
195ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
196float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
197double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000198type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000199label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Chris Lattner4e4cae82002-04-04 19:22:17 +0000200opaque { return OPAQUE; }
Chris Lattner00950542001-06-06 20:29:01 +0000201
Chris Lattner00950542001-06-06 20:29:01 +0000202add { RET_TOK(BinaryOpVal, Add, ADD); }
203sub { RET_TOK(BinaryOpVal, Sub, SUB); }
204mul { RET_TOK(BinaryOpVal, Mul, MUL); }
205div { RET_TOK(BinaryOpVal, Div, DIV); }
206rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattner42c9e772001-10-20 09:32:59 +0000207and { RET_TOK(BinaryOpVal, And, AND); }
208or { RET_TOK(BinaryOpVal, Or , OR ); }
209xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner00950542001-06-06 20:29:01 +0000210setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
211seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
212setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
213setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
214setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
215setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
216
Chris Lattner027dcc52001-07-08 21:10:27 +0000217phi { RET_TOK(OtherOpVal, PHINode, PHI); }
218call { RET_TOK(OtherOpVal, Call, CALL); }
219cast { RET_TOK(OtherOpVal, Cast, CAST); }
220shl { RET_TOK(OtherOpVal, Shl, SHL); }
221shr { RET_TOK(OtherOpVal, Shr, SHR); }
222
Chris Lattner00950542001-06-06 20:29:01 +0000223ret { RET_TOK(TermOpVal, Ret, RET); }
224br { RET_TOK(TermOpVal, Br, BR); }
225switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner5efbbc22001-10-13 06:37:14 +0000226invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner00950542001-06-06 20:29:01 +0000227
228
229malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
230alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
231free { RET_TOK(MemOpVal, Free, FREE); }
232load { RET_TOK(MemOpVal, Load, LOAD); }
233store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000234getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner00950542001-06-06 20:29:01 +0000235
236
Chris Lattner93750fa2001-07-28 17:48:55 +0000237{VarID} {
238 UnEscapeLexed(yytext+1);
239 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
240 return VAR_ID;
241 }
242{Label} {
Chris Lattner00950542001-06-06 20:29:01 +0000243 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner93750fa2001-07-28 17:48:55 +0000244 UnEscapeLexed(yytext);
245 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner00950542001-06-06 20:29:01 +0000246 return LABELSTR;
247 }
248
Chris Lattner93750fa2001-07-28 17:48:55 +0000249{StringConstant} { // Note that we cannot unescape a string constant here! The
250 // string constant might contain a \00 which would not be
251 // understood by the string stuff. It is valid to make a
252 // [sbyte] c"Hello World\00" constant, for example.
253 //
Chris Lattner00950542001-06-06 20:29:01 +0000254 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner93750fa2001-07-28 17:48:55 +0000255 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner00950542001-06-06 20:29:01 +0000256 return STRINGCONSTANT;
Chris Lattner93750fa2001-07-28 17:48:55 +0000257 }
Chris Lattner00950542001-06-06 20:29:01 +0000258
259
260{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
261{NInteger} {
262 uint64_t Val = atoull(yytext+1);
263 // +1: we have bigger negative range
264 if (Val > (uint64_t)INT64_MAX+1)
265 ThrowException("Constant too large for signed 64 bits!");
266 llvmAsmlval.SInt64Val = -Val;
267 return ESINT64VAL;
268 }
Chris Lattner3e8ba102003-04-17 22:17:32 +0000269{HexIntConstant} {
270 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
271 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
272 }
Chris Lattner00950542001-06-06 20:29:01 +0000273
274{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
275{ENInteger} {
276 uint64_t Val = atoull(yytext+2);
277 // +1: we have bigger negative range
278 if (Val > (uint64_t)INT32_MAX+1)
279 ThrowException("Constant too large for signed 32 bits!");
280 llvmAsmlval.SIntVal = -Val;
281 return SINTVAL;
282 }
283
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000284{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner275da862002-04-07 08:10:41 +0000285{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner00950542001-06-06 20:29:01 +0000286
287[ \t\n] { /* Ignore whitespace */ }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000288. { return yytext[0]; }
Chris Lattner00950542001-06-06 20:29:01 +0000289
290%%