blob: 4df84f685e64e34bbc2b025a239266d40c6edaa6 [file] [log] [blame]
Chris Lattner32eecb02006-02-14 05:14:46 +00001/*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the flex scanner for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===*/
13
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"
29#include "llvm/Module.h"
30#include <list>
31#include "llvmAsmParser.h"
32#include <cctype>
33#include <cstdlib>
34
35void 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
Reid Spencer3ed469c2006-11-02 20:25:50 +000042// Construct a token value for a non-obsolete token
Chris Lattner32eecb02006-02-14 05:14:46 +000043#define RET_TOK(type, Enum, sym) \
Reid Spencer3ed469c2006-11-02 20:25:50 +000044 llvmAsmlval.type.opcode = Instruction::Enum; \
45 llvmAsmlval.type.obsolete = false; \
46 return sym
47
48// Construct a token value for an obsolete token
49#define RET_TOK_OBSOLETE(type, Enum, sym) \
50 llvmAsmlval.type.opcode = Instruction::Enum; \
51 llvmAsmlval.type.obsolete = true; \
52 return sym
53
Chris Lattner32eecb02006-02-14 05:14:46 +000054
55namespace llvm {
56
57// TODO: All of the static identifiers are figured out by the lexer,
58// these should be hashed to reduce the lexer size
59
60
61// atoull - Convert an ascii string of decimal digits into the unsigned long
62// long representation... this does not have to do input error checking,
63// because we know that the input will be matched by a suitable regex...
64//
65static uint64_t atoull(const char *Buffer) {
66 uint64_t Result = 0;
67 for (; *Buffer; Buffer++) {
68 uint64_t OldRes = Result;
69 Result *= 10;
70 Result += *Buffer-'0';
71 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000072 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000073 }
74 return Result;
75}
76
77static uint64_t HexIntToVal(const char *Buffer) {
78 uint64_t Result = 0;
79 for (; *Buffer; ++Buffer) {
80 uint64_t OldRes = Result;
81 Result *= 16;
82 char C = *Buffer;
83 if (C >= '0' && C <= '9')
84 Result += C-'0';
85 else if (C >= 'A' && C <= 'F')
86 Result += C-'A'+10;
87 else if (C >= 'a' && C <= 'f')
88 Result += C-'a'+10;
89
90 if (Result < OldRes) // Uh, oh, overflow detected!!!
Reid Spencer61c83e02006-08-18 08:43:06 +000091 GenerateError("constant bigger than 64 bits detected!");
Chris Lattner32eecb02006-02-14 05:14:46 +000092 }
93 return Result;
94}
95
96
97// HexToFP - Convert the ascii string in hexidecimal format to the floating
98// point representation of it.
99//
100static double HexToFP(const char *Buffer) {
101 // Behave nicely in the face of C TBAA rules... see:
102 // http://www.nullstone.com/htmls/category/aliastyp.htm
103 union {
104 uint64_t UI;
105 double FP;
106 } UIntToFP;
107 UIntToFP.UI = HexIntToVal(Buffer);
108
109 assert(sizeof(double) == sizeof(uint64_t) &&
110 "Data sizes incompatible on this target!");
111 return UIntToFP.FP; // Cast Hex constant to double
112}
113
114
115// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
116// appropriate character. If AllowNull is set to false, a \00 value will cause
117// an exception to be thrown.
118//
119// If AllowNull is set to true, the return value of the function points to the
120// last character of the string in memory.
121//
122char *UnEscapeLexed(char *Buffer, bool AllowNull) {
123 char *BOut = Buffer;
124 for (char *BIn = Buffer; *BIn; ) {
125 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
126 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
127 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
128 if (!AllowNull && !*BOut)
Reid Spencer61c83e02006-08-18 08:43:06 +0000129 GenerateError("String literal cannot accept \\00 escape!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000130
131 BIn[3] = Tmp; // Restore character
132 BIn += 3; // Skip over handled chars
133 ++BOut;
134 } else {
135 *BOut++ = *BIn++;
136 }
137 }
138
139 return BOut;
140}
141
142} // End llvm namespace
143
144using namespace llvm;
145
146#define YY_NEVER_INTERACTIVE 1
147%}
148
149
150
151/* Comments start with a ; and go till end of line */
152Comment ;.*
153
154/* Variable(Value) identifiers start with a % sign */
155VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
156
157/* Label identifiers end with a colon */
158Label [-a-zA-Z$._0-9]+:
159QuoteLabel \"[^\"]+\":
160
161/* Quoted names can contain any character except " and \ */
162StringConstant \"[^\"]*\"
163
164
165/* [PN]Integer: match positive and negative literal integer values that
166 * are preceeded by a '%' character. These represent unnamed variable slots.
167 */
168EPInteger %[0-9]+
169ENInteger %-[0-9]+
170
171
172/* E[PN]Integer: match positive and negative literal integer values */
173PInteger [0-9]+
174NInteger -[0-9]+
175
176/* FPConstant - A Floating point constant.
177 */
178FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
179
180/* HexFPConstant - Floating point constant represented in IEEE format as a
181 * hexadecimal number for when exponential notation is not precise enough.
182 */
183HexFPConstant 0x[0-9A-Fa-f]+
184
185/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
186 * it to deal with 64 bit numbers.
187 */
188HexIntConstant [us]0x[0-9A-Fa-f]+
189%%
190
191{Comment} { /* Ignore comments for now */ }
192
193begin { return BEGINTOK; }
194end { return ENDTOK; }
195true { return TRUETOK; }
196false { return FALSETOK; }
197declare { return DECLARE; }
198global { return GLOBAL; }
199constant { return CONSTANT; }
200internal { return INTERNAL; }
201linkonce { return LINKONCE; }
202weak { return WEAK; }
203appending { return APPENDING; }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000204dllimport { return DLLIMPORT; }
205dllexport { return DLLEXPORT; }
206extern_weak { return EXTERN_WEAK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000207uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
208external { return EXTERNAL; }
209implementation { return IMPLEMENTATION; }
210zeroinitializer { return ZEROINITIALIZER; }
211\.\.\. { return DOTDOTDOT; }
212undef { return UNDEF; }
213null { return NULL_TOK; }
214to { return TO; }
215except { RET_TOK(TermOpVal, Unwind, UNWIND); }
216not { return NOT; } /* Deprecated, turned into XOR */
217tail { return TAIL; }
218target { return TARGET; }
219triple { return TRIPLE; }
220deplibs { return DEPLIBS; }
221endian { return ENDIAN; }
222pointersize { return POINTERSIZE; }
Chris Lattner1ae022f2006-10-22 06:08:13 +0000223datalayout { return DATALAYOUT; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000224little { return LITTLE; }
225big { return BIG; }
226volatile { return VOLATILE; }
227align { return ALIGN; }
228section { return SECTION; }
229module { return MODULE; }
230asm { return ASM_TOK; }
231sideeffect { return SIDEEFFECT; }
232
233cc { return CC_TOK; }
234ccc { return CCC_TOK; }
Chris Lattner75466192006-05-19 21:28:53 +0000235csretcc { return CSRETCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000236fastcc { return FASTCC_TOK; }
237coldcc { return COLDCC_TOK; }
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000238x86_stdcallcc { return X86_STDCALLCC_TOK; }
239x86_fastcallcc { return X86_FASTCALLCC_TOK; }
Chris Lattner32eecb02006-02-14 05:14:46 +0000240
241void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
242bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
243sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
244ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
245short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
246ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
247int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
248uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
249long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
250ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
251float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
252double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
253label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
254type { return TYPE; }
255opaque { return OPAQUE; }
256
257add { RET_TOK(BinaryOpVal, Add, ADD); }
258sub { RET_TOK(BinaryOpVal, Sub, SUB); }
259mul { RET_TOK(BinaryOpVal, Mul, MUL); }
Reid Spencer3ed469c2006-11-02 20:25:50 +0000260div { RET_TOK_OBSOLETE(BinaryOpVal, UDiv, UDIV); }
261udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
262sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
263fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
264rem { RET_TOK_OBSOLETE(BinaryOpVal, URem, UREM); }
265urem { RET_TOK(BinaryOpVal, URem, UREM); }
266srem { RET_TOK(BinaryOpVal, SRem, SREM); }
267frem { RET_TOK(BinaryOpVal, FRem, FREM); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000268and { RET_TOK(BinaryOpVal, And, AND); }
269or { RET_TOK(BinaryOpVal, Or , OR ); }
270xor { RET_TOK(BinaryOpVal, Xor, XOR); }
271setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
272seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
273setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
274setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
275setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
276setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
277
278phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
279call { RET_TOK(OtherOpVal, Call, CALL); }
280cast { RET_TOK(OtherOpVal, Cast, CAST); }
281select { RET_TOK(OtherOpVal, Select, SELECT); }
282shl { RET_TOK(OtherOpVal, Shl, SHL); }
283shr { RET_TOK(OtherOpVal, Shr, SHR); }
284vanext { return VANEXT_old; }
285vaarg { return VAARG_old; }
286va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
287ret { RET_TOK(TermOpVal, Ret, RET); }
288br { RET_TOK(TermOpVal, Br, BR); }
289switch { RET_TOK(TermOpVal, Switch, SWITCH); }
290invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
291unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
292unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
293
294malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
295alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
296free { RET_TOK(MemOpVal, Free, FREE); }
297load { RET_TOK(MemOpVal, Load, LOAD); }
298store { RET_TOK(MemOpVal, Store, STORE); }
299getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
300
301extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
302insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
Chris Lattnerd5efe842006-04-08 01:18:56 +0000303shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000304
305
306{VarID} {
307 UnEscapeLexed(yytext+1);
308 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
309 return VAR_ID;
310 }
311{Label} {
312 yytext[strlen(yytext)-1] = 0; // nuke colon
313 UnEscapeLexed(yytext);
314 llvmAsmlval.StrVal = strdup(yytext);
315 return LABELSTR;
316 }
317{QuoteLabel} {
318 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
319 UnEscapeLexed(yytext+1);
320 llvmAsmlval.StrVal = strdup(yytext+1);
321 return LABELSTR;
322 }
323
324{StringConstant} { // Note that we cannot unescape a string constant here! The
325 // string constant might contain a \00 which would not be
326 // understood by the string stuff. It is valid to make a
327 // [sbyte] c"Hello World\00" constant, for example.
328 //
329 yytext[strlen(yytext)-1] = 0; // nuke end quote
330 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
331 return STRINGCONSTANT;
332 }
333
334
335{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
336{NInteger} {
337 uint64_t Val = atoull(yytext+1);
338 // +1: we have bigger negative range
339 if (Val > (uint64_t)INT64_MAX+1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000340 GenerateError("Constant too large for signed 64 bits!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000341 llvmAsmlval.SInt64Val = -Val;
342 return ESINT64VAL;
343 }
344{HexIntConstant} {
345 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
346 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
347 }
348
349{EPInteger} {
350 uint64_t Val = atoull(yytext+1);
351 if ((unsigned)Val != Val)
Reid Spencer61c83e02006-08-18 08:43:06 +0000352 GenerateError("Invalid value number (too large)!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000353 llvmAsmlval.UIntVal = unsigned(Val);
354 return UINTVAL;
355 }
356{ENInteger} {
357 uint64_t Val = atoull(yytext+2);
358 // +1: we have bigger negative range
359 if (Val > (uint64_t)INT32_MAX+1)
Reid Spencer61c83e02006-08-18 08:43:06 +0000360 GenerateError("Constant too large for signed 32 bits!");
Chris Lattner32eecb02006-02-14 05:14:46 +0000361 llvmAsmlval.SIntVal = (int)-Val;
362 return SINTVAL;
363 }
364
365{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
366{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
367
368<<EOF>> {
369 /* Make sure to free the internal buffers for flex when we are
370 * done reading our input!
371 */
372 yy_delete_buffer(YY_CURRENT_BUFFER);
373 return EOF;
374 }
375
376[ \r\t\n] { /* Ignore whitespace */ }
377. { return yytext[0]; }
378
379%%