blob: f93060968479b4744efe5bb0712fd6e50382e3af [file] [log] [blame]
Chris Lattner699f1eb2002-08-14 17:12:33 +00001/*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// 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.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the flex scanner for LLVM assembly languages files.
11//
Chris Lattner699f1eb2002-08-14 17:12:33 +000012//===----------------------------------------------------------------------===*/
Chris Lattner00950542001-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 Lattnerb9bcbb52003-04-22 19:07:06 +000029#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000030#include <list>
31#include "llvmAsmParser.h"
Brian Gaeke778fab22003-10-10 19:12:08 +000032#include <cctype>
33#include <cstdlib>
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner6184feb2005-05-20 03:25:47 +000035void set_scan_file(FILE * F){
36 yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
37}
38void set_scan_string (const char * str) {
39 yy_scan_string (str);
40}
41
Chris Lattner00950542001-06-06 20:29:01 +000042#define RET_TOK(type, Enum, sym) \
43 llvmAsmlval.type = Instruction::Enum; return sym
44
Brian Gaeked0fde302003-11-11 22:41:34 +000045namespace llvm {
Chris Lattner00950542001-06-06 20:29:01 +000046
Misha Brukman5a2a3822005-05-10 22:02:28 +000047// TODO: All of the static identifiers are figured out by the lexer,
Chris Lattnere1fe8752001-09-07 16:32:43 +000048// these should be hashed to reduce the lexer size
Chris Lattner00950542001-06-06 20:29:01 +000049
50
51// atoull - Convert an ascii string of decimal digits into the unsigned long
Misha Brukman5a2a3822005-05-10 22:02:28 +000052// long representation... this does not have to do input error checking,
Chris Lattner00950542001-06-06 20:29:01 +000053// because we know that the input will be matched by a suitable regex...
54//
Chris Lattner275da862002-04-07 08:10:41 +000055static uint64_t atoull(const char *Buffer) {
Chris Lattner00950542001-06-06 20:29:01 +000056 uint64_t Result = 0;
57 for (; *Buffer; Buffer++) {
58 uint64_t OldRes = Result;
59 Result *= 10;
60 Result += *Buffer-'0';
Chris Lattner275da862002-04-07 08:10:41 +000061 if (Result < OldRes) // Uh, oh, overflow detected!!!
Chris Lattner00950542001-06-06 20:29:01 +000062 ThrowException("constant bigger than 64 bits detected!");
Chris Lattner00950542001-06-06 20:29:01 +000063 }
64 return Result;
65}
66
Chris Lattner3e8ba102003-04-17 22:17:32 +000067static uint64_t HexIntToVal(const char *Buffer) {
Chris Lattner275da862002-04-07 08:10:41 +000068 uint64_t Result = 0;
69 for (; *Buffer; ++Buffer) {
70 uint64_t OldRes = Result;
71 Result *= 16;
72 char C = *Buffer;
73 if (C >= '0' && C <= '9')
74 Result += C-'0';
75 else if (C >= 'A' && C <= 'F')
76 Result += C-'A'+10;
77 else if (C >= 'a' && C <= 'f')
78 Result += C-'a'+10;
79
80 if (Result < OldRes) // Uh, oh, overflow detected!!!
81 ThrowException("constant bigger than 64 bits detected!");
82 }
Chris Lattner3e8ba102003-04-17 22:17:32 +000083 return Result;
84}
85
86
87// HexToFP - Convert the ascii string in hexidecimal format to the floating
88// point representation of it.
89//
90static double HexToFP(const char *Buffer) {
Chris Lattnerb55679d2002-04-07 08:31:26 +000091 // Behave nicely in the face of C TBAA rules... see:
92 // http://www.nullstone.com/htmls/category/aliastyp.htm
Chris Lattner7a5a1f72003-04-22 20:20:28 +000093 union {
94 uint64_t UI;
95 double FP;
96 } UIntToFP;
97 UIntToFP.UI = HexIntToVal(Buffer);
98
99 assert(sizeof(double) == sizeof(uint64_t) &&
100 "Data sizes incompatible on this target!");
101 return UIntToFP.FP; // Cast Hex constant to double
Chris Lattner275da862002-04-07 08:10:41 +0000102}
103
Chris Lattner00950542001-06-06 20:29:01 +0000104
Chris Lattner93750fa2001-07-28 17:48:55 +0000105// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
106// appropriate character. If AllowNull is set to false, a \00 value will cause
107// an exception to be thrown.
108//
109// If AllowNull is set to true, the return value of the function points to the
110// last character of the string in memory.
111//
Chris Lattnerbcafcce2002-07-25 06:17:42 +0000112char *UnEscapeLexed(char *Buffer, bool AllowNull) {
Chris Lattner93750fa2001-07-28 17:48:55 +0000113 char *BOut = Buffer;
114 for (char *BIn = Buffer; *BIn; ) {
115 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
116 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
Chris Lattnera8101c12005-01-08 20:07:03 +0000117 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
Chris Lattner93750fa2001-07-28 17:48:55 +0000118 if (!AllowNull && !*BOut)
119 ThrowException("String literal cannot accept \\00 escape!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000120
Chris Lattner93750fa2001-07-28 17:48:55 +0000121 BIn[3] = Tmp; // Restore character
122 BIn += 3; // Skip over handled chars
123 ++BOut;
124 } else {
125 *BOut++ = *BIn++;
126 }
127 }
128
129 return BOut;
130}
131
Brian Gaeked0fde302003-11-11 22:41:34 +0000132} // End llvm namespace
133
134using namespace llvm;
135
Chris Lattner00950542001-06-06 20:29:01 +0000136#define YY_NEVER_INTERACTIVE 1
137%}
138
139
140
141/* Comments start with a ; and go till end of line */
142Comment ;.*
143
Chris Lattnere1815642001-07-15 06:35:53 +0000144/* Variable(Value) identifiers start with a % sign */
Chris Lattner9a88d272001-12-04 04:31:30 +0000145VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner00950542001-06-06 20:29:01 +0000146
147/* Label identifiers end with a colon */
Chris Lattner9a88d272001-12-04 04:31:30 +0000148Label [-a-zA-Z$._0-9]+:
Alkis Evlogimenosd82ef512004-12-10 05:40:19 +0000149QuoteLabel \"[^\"]+\":
Chris Lattner00950542001-06-06 20:29:01 +0000150
151/* Quoted names can contain any character except " and \ */
Chris Lattner99e7ab72003-10-18 05:53:13 +0000152StringConstant \"[^\"]*\"
Chris Lattner00950542001-06-06 20:29:01 +0000153
154
155/* [PN]Integer: match positive and negative literal integer values that
156 * are preceeded by a '%' character. These represent unnamed variable slots.
157 */
158EPInteger %[0-9]+
159ENInteger %-[0-9]+
160
161
162/* E[PN]Integer: match positive and negative literal integer values */
163PInteger [0-9]+
164NInteger -[0-9]+
165
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000166/* FPConstant - A Floating point constant.
Chris Lattner275da862002-04-07 08:10:41 +0000167 */
Chris Lattner1e2c6142001-11-01 22:06:08 +0000168FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000169
Chris Lattner275da862002-04-07 08:10:41 +0000170/* HexFPConstant - Floating point constant represented in IEEE format as a
171 * hexadecimal number for when exponential notation is not precise enough.
172 */
173HexFPConstant 0x[0-9A-Fa-f]+
Chris Lattner3e8ba102003-04-17 22:17:32 +0000174
175/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
176 * it to deal with 64 bit numbers.
177 */
178HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattner00950542001-06-06 20:29:01 +0000179%%
180
181{Comment} { /* Ignore comments for now */ }
182
183begin { return BEGINTOK; }
Chris Lattner9b02cc32002-05-03 18:23:48 +0000184end { return ENDTOK; }
Chris Lattner91ef4602004-03-31 03:49:47 +0000185true { return TRUETOK; }
186false { return FALSETOK; }
Chris Lattner00950542001-06-06 20:29:01 +0000187declare { return DECLARE; }
Chris Lattner70cc3392001-09-10 07:58:01 +0000188global { return GLOBAL; }
Chris Lattner1781aca2001-09-18 04:00:54 +0000189constant { return CONSTANT; }
Chris Lattnerdda71962001-11-26 18:54:16 +0000190internal { return INTERNAL; }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000191linkonce { return LINKONCE; }
Chris Lattnerf797cab2003-10-10 04:54:02 +0000192weak { return WEAK; }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000193appending { return APPENDING; }
Chris Lattner08c0e6a2002-10-06 22:45:09 +0000194uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
195external { return EXTERNAL; }
Chris Lattner00950542001-06-06 20:29:01 +0000196implementation { return IMPLEMENTATION; }
Chris Lattnerf4600482003-06-28 20:01:34 +0000197zeroinitializer { return ZEROINITIALIZER; }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000198\.\.\. { return DOTDOTDOT; }
Chris Lattner16710e92004-10-16 18:17:13 +0000199undef { return UNDEF; }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000200null { return NULL_TOK; }
Chris Lattner5efbbc22001-10-13 06:37:14 +0000201to { return TO; }
Chris Lattnera58e3a12004-02-08 21:48:25 +0000202except { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattner699f1eb2002-08-14 17:12:33 +0000203not { return NOT; } /* Deprecated, turned into XOR */
Chris Lattnerc28ade42005-05-06 06:20:33 +0000204tail { return TAIL; }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000205target { return TARGET; }
Reid Spencer3cd2fe32004-07-25 17:56:00 +0000206triple { return TRIPLE; }
207deplibs { return DEPLIBS; }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000208endian { return ENDIAN; }
209pointersize { return POINTERSIZE; }
210little { return LITTLE; }
211big { return BIG; }
Chris Lattner15c9c032003-09-08 18:20:29 +0000212volatile { return VOLATILE; }
Chris Lattnerb7d08a52005-11-12 00:11:30 +0000213align { return ALIGN; }
214section { return SECTION; }
Chris Lattner71cdba32006-01-24 00:40:17 +0000215module { return MODULE; }
Chris Lattneree454772006-01-23 23:05:15 +0000216asm { return ASM_TOK; }
Chris Lattneraa2c8532006-01-25 22:26:43 +0000217sideeffect { return SIDEEFFECT; }
Chris Lattner00950542001-06-06 20:29:01 +0000218
Chris Lattnera8e8f162005-05-06 20:27:19 +0000219cc { return CC_TOK; }
220ccc { return CCC_TOK; }
221fastcc { return FASTCC_TOK; }
222coldcc { return COLDCC_TOK; }
223
Chris Lattnere1fe8752001-09-07 16:32:43 +0000224void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
225bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
226sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
227ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
228short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
229ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
230int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
231uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
232long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
233ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
234float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
235double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000236label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Reid Spencer77f4d862004-07-04 12:17:44 +0000237type { return TYPE; }
Chris Lattner4e4cae82002-04-04 19:22:17 +0000238opaque { return OPAQUE; }
Chris Lattner00950542001-06-06 20:29:01 +0000239
Chris Lattner00950542001-06-06 20:29:01 +0000240add { RET_TOK(BinaryOpVal, Add, ADD); }
241sub { RET_TOK(BinaryOpVal, Sub, SUB); }
242mul { RET_TOK(BinaryOpVal, Mul, MUL); }
243div { RET_TOK(BinaryOpVal, Div, DIV); }
244rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattner42c9e772001-10-20 09:32:59 +0000245and { RET_TOK(BinaryOpVal, And, AND); }
246or { RET_TOK(BinaryOpVal, Or , OR ); }
247xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner00950542001-06-06 20:29:01 +0000248setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
249seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
250setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
251setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
252setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
253setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
254
Chris Lattner3b237fc2003-10-19 21:34:28 +0000255phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
Chris Lattner027dcc52001-07-08 21:10:27 +0000256call { RET_TOK(OtherOpVal, Call, CALL); }
257cast { RET_TOK(OtherOpVal, Cast, CAST); }
Chris Lattner76f0ff32004-03-12 05:51:36 +0000258select { RET_TOK(OtherOpVal, Select, SELECT); }
Chris Lattner027dcc52001-07-08 21:10:27 +0000259shl { RET_TOK(OtherOpVal, Shl, SHL); }
260shr { RET_TOK(OtherOpVal, Shr, SHR); }
Andrew Lenharth558bc882005-06-18 18:34:52 +0000261vanext { return VANEXT_old; }
262vaarg { return VAARG_old; }
263va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
Chris Lattner00950542001-06-06 20:29:01 +0000264ret { RET_TOK(TermOpVal, Ret, RET); }
265br { RET_TOK(TermOpVal, Br, BR); }
266switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner5efbbc22001-10-13 06:37:14 +0000267invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner36143fc2003-09-08 18:54:55 +0000268unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
Chris Lattner16710e92004-10-16 18:17:13 +0000269unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
Chris Lattner00950542001-06-06 20:29:01 +0000270
271malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
272alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
273free { RET_TOK(MemOpVal, Free, FREE); }
274load { RET_TOK(MemOpVal, Load, LOAD); }
275store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000276getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner00950542001-06-06 20:29:01 +0000277
Robert Bocchino9c62b562006-01-10 19:04:32 +0000278extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
Robert Bocchino2def1b32006-01-17 20:06:25 +0000279insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
Chris Lattner4c949082006-04-08 01:18:35 +0000280shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
Robert Bocchino9c62b562006-01-10 19:04:32 +0000281
Chris Lattner00950542001-06-06 20:29:01 +0000282
Chris Lattner93750fa2001-07-28 17:48:55 +0000283{VarID} {
284 UnEscapeLexed(yytext+1);
285 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
Misha Brukman5a2a3822005-05-10 22:02:28 +0000286 return VAR_ID;
Chris Lattner93750fa2001-07-28 17:48:55 +0000287 }
288{Label} {
Chris Lattner00950542001-06-06 20:29:01 +0000289 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner93750fa2001-07-28 17:48:55 +0000290 UnEscapeLexed(yytext);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000291 llvmAsmlval.StrVal = strdup(yytext);
292 return LABELSTR;
Chris Lattner00950542001-06-06 20:29:01 +0000293 }
Chris Lattnerc6c97722004-12-10 05:27:29 +0000294{QuoteLabel} {
295 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
296 UnEscapeLexed(yytext+1);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000297 llvmAsmlval.StrVal = strdup(yytext+1);
298 return LABELSTR;
Chris Lattnerc6c97722004-12-10 05:27:29 +0000299 }
Chris Lattner00950542001-06-06 20:29:01 +0000300
Chris Lattner93750fa2001-07-28 17:48:55 +0000301{StringConstant} { // Note that we cannot unescape a string constant here! The
Misha Brukman5a2a3822005-05-10 22:02:28 +0000302 // string constant might contain a \00 which would not be
Chris Lattner93750fa2001-07-28 17:48:55 +0000303 // understood by the string stuff. It is valid to make a
304 // [sbyte] c"Hello World\00" constant, for example.
305 //
Misha Brukman5a2a3822005-05-10 22:02:28 +0000306 yytext[strlen(yytext)-1] = 0; // nuke end quote
307 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
308 return STRINGCONSTANT;
Chris Lattner93750fa2001-07-28 17:48:55 +0000309 }
Chris Lattner00950542001-06-06 20:29:01 +0000310
311
312{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
Misha Brukman5a2a3822005-05-10 22:02:28 +0000313{NInteger} {
Chris Lattner00950542001-06-06 20:29:01 +0000314 uint64_t Val = atoull(yytext+1);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000315 // +1: we have bigger negative range
316 if (Val > (uint64_t)INT64_MAX+1)
317 ThrowException("Constant too large for signed 64 bits!");
318 llvmAsmlval.SInt64Val = -Val;
319 return ESINT64VAL;
Chris Lattner00950542001-06-06 20:29:01 +0000320 }
Chris Lattner3e8ba102003-04-17 22:17:32 +0000321{HexIntConstant} {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000322 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
Chris Lattner3e8ba102003-04-17 22:17:32 +0000323 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
324 }
Chris Lattner00950542001-06-06 20:29:01 +0000325
Chris Lattnera8101c12005-01-08 20:07:03 +0000326{EPInteger} {
327 uint64_t Val = atoull(yytext+1);
328 if ((unsigned)Val != Val)
329 ThrowException("Invalid value number (too large)!");
330 llvmAsmlval.UIntVal = unsigned(Val);
331 return UINTVAL;
332 }
Chris Lattner00950542001-06-06 20:29:01 +0000333{ENInteger} {
334 uint64_t Val = atoull(yytext+2);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000335 // +1: we have bigger negative range
336 if (Val > (uint64_t)INT32_MAX+1)
337 ThrowException("Constant too large for signed 32 bits!");
Chris Lattnera8101c12005-01-08 20:07:03 +0000338 llvmAsmlval.SIntVal = (int)-Val;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000339 return SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000340 }
341
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000342{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner275da862002-04-07 08:10:41 +0000343{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner00950542001-06-06 20:29:01 +0000344
Chris Lattnere1512592004-03-19 23:34:33 +0000345<<EOF>> {
346 /* Make sure to free the internal buffers for flex when we are
347 * done reading our input!
348 */
349 yy_delete_buffer(YY_CURRENT_BUFFER);
350 return EOF;
351 }
352
Chris Lattnera0846a32004-05-27 17:49:14 +0000353[ \r\t\n] { /* Ignore whitespace */ }
Chris Lattnere1fe8752001-09-07 16:32:43 +0000354. { return yytext[0]; }
Chris Lattner00950542001-06-06 20:29:01 +0000355
356%%
Chris Lattner2fecc0f2006-02-15 07:02:59 +0000357