1 /*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the flex scanner for LLVM assembly languages files.
12 //===----------------------------------------------------------------------===*/
14 %option prefix="llvmAsm"
17 %option never-interactive
22 %option outfile="Lexer.cpp"
28 #include "ParserInternals.h"
29 #include "llvm/Module.h"
31 #include "llvmAsmParser.h"
35 void set_scan_file(FILE * F){
36 yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
38 void set_scan_string (const char * str) {
42 #define RET_TOK(type, Enum, sym) \
43 llvmAsmlval.type = Instruction::Enum; return sym
47 // TODO: All of the static identifiers are figured out by the lexer,
48 // these should be hashed to reduce the lexer size
51 // atoull - Convert an ascii string of decimal digits into the unsigned long
52 // long representation... this does not have to do input error checking,
53 // because we know that the input will be matched by a suitable regex...
55 static uint64_t atoull(const char *Buffer) {
57 for (; *Buffer; Buffer++) {
58 uint64_t OldRes = Result;
60 Result += *Buffer-'0';
61 if (Result < OldRes) // Uh, oh, overflow detected!!!
62 GenerateError("constant bigger than 64 bits detected!");
67 static uint64_t HexIntToVal(const char *Buffer) {
69 for (; *Buffer; ++Buffer) {
70 uint64_t OldRes = Result;
73 if (C >= '0' && C <= '9')
75 else if (C >= 'A' && C <= 'F')
77 else if (C >= 'a' && C <= 'f')
80 if (Result < OldRes) // Uh, oh, overflow detected!!!
81 GenerateError("constant bigger than 64 bits detected!");
87 // HexToFP - Convert the ascii string in hexidecimal format to the floating
88 // point representation of it.
90 static double HexToFP(const char *Buffer) {
91 // Behave nicely in the face of C TBAA rules... see:
92 // http://www.nullstone.com/htmls/category/aliastyp.htm
97 UIntToFP.UI = HexIntToVal(Buffer);
99 assert(sizeof(double) == sizeof(uint64_t) &&
100 "Data sizes incompatible on this target!");
101 return UIntToFP.FP; // Cast Hex constant to double
105 // 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.
109 // If AllowNull is set to true, the return value of the function points to the
110 // last character of the string in memory.
112 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
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
117 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
118 if (!AllowNull && !*BOut)
119 GenerateError("String literal cannot accept \\00 escape!");
121 BIn[3] = Tmp; // Restore character
122 BIn += 3; // Skip over handled chars
132 } // End llvm namespace
134 using namespace llvm;
136 #define YY_NEVER_INTERACTIVE 1
141 /* Comments start with a ; and go till end of line */
144 /* Variable(Value) identifiers start with a % sign */
145 VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
147 /* Label identifiers end with a colon */
148 Label [-a-zA-Z$._0-9]+:
149 QuoteLabel \"[^\"]+\":
151 /* Quoted names can contain any character except " and \ */
152 StringConstant \"[^\"]*\"
155 /* [PN]Integer: match positive and negative literal integer values that
156 * are preceeded by a '%' character. These represent unnamed variable slots.
162 /* E[PN]Integer: match positive and negative literal integer values */
166 /* FPConstant - A Floating point constant.
168 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
170 /* HexFPConstant - Floating point constant represented in IEEE format as a
171 * hexadecimal number for when exponential notation is not precise enough.
173 HexFPConstant 0x[0-9A-Fa-f]+
175 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
176 * it to deal with 64 bit numbers.
178 HexIntConstant [us]0x[0-9A-Fa-f]+
181 {Comment} { /* Ignore comments for now */ }
183 begin { return BEGINTOK; }
184 end { return ENDTOK; }
185 true { return TRUETOK; }
186 false { return FALSETOK; }
187 declare { return DECLARE; }
188 global { return GLOBAL; }
189 constant { return CONSTANT; }
190 internal { return INTERNAL; }
191 linkonce { return LINKONCE; }
192 weak { return WEAK; }
193 appending { return APPENDING; }
194 dllimport { return DLLIMPORT; }
195 dllexport { return DLLEXPORT; }
196 extern_weak { return EXTERN_WEAK; }
197 uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
198 external { return EXTERNAL; }
199 implementation { return IMPLEMENTATION; }
200 zeroinitializer { return ZEROINITIALIZER; }
201 \.\.\. { return DOTDOTDOT; }
202 undef { return UNDEF; }
203 null { return NULL_TOK; }
205 except { RET_TOK(TermOpVal, Unwind, UNWIND); }
206 not { return NOT; } /* Deprecated, turned into XOR */
207 tail { return TAIL; }
208 target { return TARGET; }
209 triple { return TRIPLE; }
210 deplibs { return DEPLIBS; }
211 endian { return ENDIAN; }
212 pointersize { return POINTERSIZE; }
213 datalayout { return DATALAYOUT; }
214 little { return LITTLE; }
216 volatile { return VOLATILE; }
217 align { return ALIGN; }
218 section { return SECTION; }
219 module { return MODULE; }
220 asm { return ASM_TOK; }
221 sideeffect { return SIDEEFFECT; }
223 cc { return CC_TOK; }
224 ccc { return CCC_TOK; }
225 csretcc { return CSRETCC_TOK; }
226 fastcc { return FASTCC_TOK; }
227 coldcc { return COLDCC_TOK; }
228 x86_stdcallcc { return X86_STDCALLCC_TOK; }
229 x86_fastcallcc { return X86_FASTCALLCC_TOK; }
231 void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
232 bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
233 sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
234 ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
235 short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
236 ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
237 int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
238 uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
239 long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
240 ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
241 float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
242 double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
243 label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
244 type { return TYPE; }
245 opaque { return OPAQUE; }
247 add { RET_TOK(BinaryOpVal, Add, ADD); }
248 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
249 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
250 div { RET_TOK(BinaryOpVal, Div, DIV); }
251 rem { RET_TOK(BinaryOpVal, Rem, REM); }
252 and { RET_TOK(BinaryOpVal, And, AND); }
253 or { RET_TOK(BinaryOpVal, Or , OR ); }
254 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
255 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
256 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
257 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
258 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
259 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
260 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
262 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
263 call { RET_TOK(OtherOpVal, Call, CALL); }
264 cast { RET_TOK(OtherOpVal, Cast, CAST); }
265 select { RET_TOK(OtherOpVal, Select, SELECT); }
266 shl { RET_TOK(OtherOpVal, Shl, SHL); }
267 shr { RET_TOK(OtherOpVal, Shr, SHR); }
268 vanext { return VANEXT_old; }
269 vaarg { return VAARG_old; }
270 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
271 ret { RET_TOK(TermOpVal, Ret, RET); }
272 br { RET_TOK(TermOpVal, Br, BR); }
273 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
274 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
275 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
276 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
278 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
279 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
280 free { RET_TOK(MemOpVal, Free, FREE); }
281 load { RET_TOK(MemOpVal, Load, LOAD); }
282 store { RET_TOK(MemOpVal, Store, STORE); }
283 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
285 extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
286 insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
287 shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
291 UnEscapeLexed(yytext+1);
292 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
296 yytext[strlen(yytext)-1] = 0; // nuke colon
297 UnEscapeLexed(yytext);
298 llvmAsmlval.StrVal = strdup(yytext);
302 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
303 UnEscapeLexed(yytext+1);
304 llvmAsmlval.StrVal = strdup(yytext+1);
308 {StringConstant} { // Note that we cannot unescape a string constant here! The
309 // string constant might contain a \00 which would not be
310 // understood by the string stuff. It is valid to make a
311 // [sbyte] c"Hello World\00" constant, for example.
313 yytext[strlen(yytext)-1] = 0; // nuke end quote
314 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
315 return STRINGCONSTANT;
319 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
321 uint64_t Val = atoull(yytext+1);
322 // +1: we have bigger negative range
323 if (Val > (uint64_t)INT64_MAX+1)
324 GenerateError("Constant too large for signed 64 bits!");
325 llvmAsmlval.SInt64Val = -Val;
329 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
330 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
334 uint64_t Val = atoull(yytext+1);
335 if ((unsigned)Val != Val)
336 GenerateError("Invalid value number (too large)!");
337 llvmAsmlval.UIntVal = unsigned(Val);
341 uint64_t Val = atoull(yytext+2);
342 // +1: we have bigger negative range
343 if (Val > (uint64_t)INT32_MAX+1)
344 GenerateError("Constant too large for signed 32 bits!");
345 llvmAsmlval.SIntVal = (int)-Val;
349 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
350 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
353 /* Make sure to free the internal buffers for flex when we are
354 * done reading our input!
356 yy_delete_buffer(YY_CURRENT_BUFFER);
360 [ \r\t\n] { /* Ignore whitespace */ }
361 . { return yytext[0]; }