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"
30 #include "llvm/Support/MathExtras.h"
32 #include "llvmAsmParser.h"
36 void set_scan_file(FILE * F){
37 yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
39 void set_scan_string (const char * str) {
43 // Construct a token value for a non-obsolete token
44 #define RET_TOK(type, Enum, sym) \
45 llvmAsmlval.type = Instruction::Enum; \
48 // Construct a token value for an obsolete token
49 #define RET_TY(CTYPE, SYM) \
50 llvmAsmlval.PrimType = CTYPE;\
55 // TODO: All of the static identifiers are figured out by the lexer,
56 // these should be hashed to reduce the lexer size
59 // atoull - Convert an ascii string of decimal digits into the unsigned long
60 // long representation... this does not have to do input error checking,
61 // because we know that the input will be matched by a suitable regex...
63 static uint64_t atoull(const char *Buffer) {
65 for (; *Buffer; Buffer++) {
66 uint64_t OldRes = Result;
68 Result += *Buffer-'0';
69 if (Result < OldRes) // Uh, oh, overflow detected!!!
70 GenerateError("constant bigger than 64 bits detected!");
75 static uint64_t HexIntToVal(const char *Buffer) {
77 for (; *Buffer; ++Buffer) {
78 uint64_t OldRes = Result;
81 if (C >= '0' && C <= '9')
83 else if (C >= 'A' && C <= 'F')
85 else if (C >= 'a' && C <= 'f')
88 if (Result < OldRes) // Uh, oh, overflow detected!!!
89 GenerateError("constant bigger than 64 bits detected!");
94 // HexToFP - Convert the ascii string in hexadecimal format to the floating
95 // point representation of it.
97 static double HexToFP(const char *Buffer) {
98 return BitsToDouble(HexIntToVal(Buffer)); // Cast Hex constant to double
101 static void HexToIntPair(const char *Buffer, uint64_t Pair[2]) {
103 for (int i=0; i<16; i++, Buffer++) {
107 if (C >= '0' && C <= '9')
109 else if (C >= 'A' && C <= 'F')
111 else if (C >= 'a' && C <= 'f')
115 for (int i=0; i<16 && *Buffer; i++, Buffer++) {
118 if (C >= '0' && C <= '9')
120 else if (C >= 'A' && C <= 'F')
122 else if (C >= 'a' && C <= 'f')
126 GenerateError("constant bigger than 128 bits detected!");
129 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
130 // appropriate character.
131 char *UnEscapeLexed(char *Buffer, char* EndBuffer) {
133 for (char *BIn = Buffer; *BIn; ) {
134 if (BIn[0] == '\\') {
135 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
136 *BOut++ = '\\'; // Two \ becomes one
138 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
139 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
140 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
141 BIn[3] = Tmp; // Restore character
142 BIn += 3; // Skip over handled chars
154 } // End llvm namespace
156 using namespace llvm;
158 #define YY_NEVER_INTERACTIVE 1
163 /* Comments start with a ; and go till end of line */
166 /* Local Values and Type identifiers start with a % sign */
167 LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
169 /* Global Value identifiers start with an @ sign */
170 GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
172 /* Label identifiers end with a colon */
173 Label [-a-zA-Z$._0-9]+:
174 QuoteLabel \"[^\"]+\":
176 /* Quoted names can contain any character except " and \ */
177 StringConstant \"[^\"]*\"
178 AtStringConstant @\"[^\"]*\"
179 PctStringConstant %\"[^\"]*\"
181 /* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
185 /* Integer types are specified with i and a bitwidth */
188 /* E[PN]Integer: match positive and negative literal integer values. */
192 /* FPConstant - A Floating point constant. Float and double only.
194 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
196 /* HexFPConstant - Floating point constant represented in IEEE format as a
197 * hexadecimal number for when exponential notation is not precise enough.
198 * Float and double only.
200 HexFPConstant 0x[0-9A-Fa-f]+
202 /* F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
204 HexFP80Constant 0xK[0-9A-Fa-f]+
206 /* F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
208 HexFP128Constant 0xL[0-9A-Fa-f]+
210 /* PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
212 HexPPC128Constant 0xM[0-9A-Fa-f]+
214 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
215 * it to deal with 64 bit numbers.
217 HexIntConstant [us]0x[0-9A-Fa-f]+
219 /* WSNL - shorthand for whitespace followed by newline */
223 {Comment} { /* Ignore comments for now */ }
225 begin { return BEGINTOK; }
226 end { return ENDTOK; }
227 true { return TRUETOK; }
228 false { return FALSETOK; }
229 declare { return DECLARE; }
230 define { return DEFINE; }
231 global { return GLOBAL; }
232 constant { return CONSTANT; }
233 internal { return INTERNAL; }
234 linkonce { return LINKONCE; }
235 weak { return WEAK; }
236 appending { return APPENDING; }
237 dllimport { return DLLIMPORT; }
238 dllexport { return DLLEXPORT; }
239 hidden { return HIDDEN; }
240 protected { return PROTECTED; }
241 extern_weak { return EXTERN_WEAK; }
242 external { return EXTERNAL; }
243 thread_local { return THREAD_LOCAL; }
244 zeroinitializer { return ZEROINITIALIZER; }
245 \.\.\. { return DOTDOTDOT; }
246 undef { return UNDEF; }
247 null { return NULL_TOK; }
249 tail { return TAIL; }
250 target { return TARGET; }
251 triple { return TRIPLE; }
252 deplibs { return DEPLIBS; }
253 datalayout { return DATALAYOUT; }
254 volatile { return VOLATILE; }
255 align { return ALIGN; }
256 section { return SECTION; }
257 alias { return ALIAS; }
258 module { return MODULE; }
259 asm { return ASM_TOK; }
260 sideeffect { return SIDEEFFECT; }
262 cc { return CC_TOK; }
263 ccc { return CCC_TOK; }
264 fastcc { return FASTCC_TOK; }
265 coldcc { return COLDCC_TOK; }
266 x86_stdcallcc { return X86_STDCALLCC_TOK; }
267 x86_fastcallcc { return X86_FASTCALLCC_TOK; }
269 signext { return SIGNEXT; }
270 zeroext { return ZEROEXT; }
271 inreg { return INREG; }
272 sret { return SRET; }
273 nounwind { return NOUNWIND; }
274 noreturn { return NORETURN; }
275 noalias { return NOALIAS; }
276 byval { return BYVAL; }
277 nest { return NEST; }
278 pure { return PURE; }
279 const { return CONST; }
280 sext{WSNL} { // For auto-upgrade only, drop in LLVM 3.0
282 zext{WSNL} { // For auto-upgrade only, drop in LLVM 3.0
285 void { RET_TY(Type::VoidTy, VOID); }
286 float { RET_TY(Type::FloatTy, FLOAT); }
287 double { RET_TY(Type::DoubleTy,DOUBLE);}
288 x86_fp80 { RET_TY(Type::X86_FP80Ty, X86_FP80);}
289 fp128 { RET_TY(Type::FP128Ty, FP128);}
290 ppc_fp128 { RET_TY(Type::PPC_FP128Ty, PPC_FP128);}
291 label { RET_TY(Type::LabelTy, LABEL); }
292 type { return TYPE; }
293 opaque { return OPAQUE; }
294 {IntegerType} { uint64_t NumBits = atoull(yytext+1);
295 if (NumBits < IntegerType::MIN_INT_BITS ||
296 NumBits > IntegerType::MAX_INT_BITS)
297 GenerateError("Bitwidth for integer type out of range!");
298 const Type* Ty = IntegerType::get(NumBits);
302 add { RET_TOK(BinaryOpVal, Add, ADD); }
303 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
304 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
305 udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
306 sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
307 fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
308 urem { RET_TOK(BinaryOpVal, URem, UREM); }
309 srem { RET_TOK(BinaryOpVal, SRem, SREM); }
310 frem { RET_TOK(BinaryOpVal, FRem, FREM); }
311 shl { RET_TOK(BinaryOpVal, Shl, SHL); }
312 lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
313 ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
314 and { RET_TOK(BinaryOpVal, And, AND); }
315 or { RET_TOK(BinaryOpVal, Or , OR ); }
316 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
317 icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
318 fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
341 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
342 call { RET_TOK(OtherOpVal, Call, CALL); }
343 trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
344 zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
345 sext { RET_TOK(CastOpVal, SExt, SEXT); }
346 fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
347 fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
348 uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
349 sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
350 fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
351 fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
352 inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
353 ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
354 bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
355 select { RET_TOK(OtherOpVal, Select, SELECT); }
356 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
357 ret { RET_TOK(TermOpVal, Ret, RET); }
358 br { RET_TOK(TermOpVal, Br, BR); }
359 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
360 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
361 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
362 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
364 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
365 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
366 free { RET_TOK(MemOpVal, Free, FREE); }
367 load { RET_TOK(MemOpVal, Load, LOAD); }
368 store { RET_TOK(MemOpVal, Store, STORE); }
369 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
371 extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
372 insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
373 shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
377 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip %
381 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip @
385 yytext[yyleng-1] = 0; // nuke colon
386 llvmAsmlval.StrVal = new std::string(yytext);
390 yytext[yyleng-2] = 0; // nuke colon, end quote
391 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
393 new std::string(yytext+1, EndChar - yytext - 1);
397 {StringConstant} { yytext[yyleng-1] = 0; // nuke end quote
398 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
400 new std::string(yytext+1, EndChar - yytext - 1);
401 return STRINGCONSTANT;
404 yytext[yyleng-1] = 0; // nuke end quote
405 const char* EndChar =
406 UnEscapeLexed(yytext+2, yytext+yyleng);
408 new std::string(yytext+2, EndChar - yytext - 2);
409 return ATSTRINGCONSTANT;
411 {PctStringConstant} {
412 yytext[yyleng-1] = 0; // nuke end quote
413 const char* EndChar =
414 UnEscapeLexed(yytext+2, yytext+yyleng);
416 new std::string(yytext+2, EndChar - yytext - 2);
417 return PCTSTRINGCONSTANT;
420 uint32_t numBits = ((yyleng * 64) / 19) + 1;
421 APInt Tmp(numBits, yytext, yyleng, 10);
422 uint32_t activeBits = Tmp.getActiveBits();
423 if (activeBits > 0 && activeBits < numBits)
424 Tmp.trunc(activeBits);
425 if (Tmp.getBitWidth() > 64) {
426 llvmAsmlval.APIntVal = new APInt(Tmp);
429 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
434 uint32_t numBits = (((yyleng-1) * 64) / 19) + 2;
435 APInt Tmp(numBits, yytext, yyleng, 10);
436 uint32_t minBits = Tmp.getMinSignedBits();
437 if (minBits > 0 && minBits < numBits)
439 if (Tmp.getBitWidth() > 64) {
440 llvmAsmlval.APIntVal = new APInt(Tmp);
443 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
448 {HexIntConstant} { int len = yyleng - 3;
449 uint32_t bits = len * 4;
450 APInt Tmp(bits, yytext+3, len, 16);
451 uint32_t activeBits = Tmp.getActiveBits();
452 if (activeBits > 0 && activeBits < bits)
453 Tmp.trunc(activeBits);
454 if (Tmp.getBitWidth() > 64) {
455 llvmAsmlval.APIntVal = new APInt(Tmp);
456 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
457 } else if (yytext[0] == 's') {
458 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
461 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
467 uint64_t Val = atoull(yytext+1);
468 if ((unsigned)Val != Val)
469 GenerateError("Invalid value number (too large)!");
470 llvmAsmlval.UIntVal = unsigned(Val);
474 uint64_t Val = atoull(yytext+1);
475 if ((unsigned)Val != Val)
476 GenerateError("Invalid value number (too large)!");
477 llvmAsmlval.UIntVal = unsigned(Val);
481 {FPConstant} { llvmAsmlval.FPVal = new APFloat(atof(yytext)); return FPVAL; }
482 {HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext+2));
485 {HexFP80Constant} { uint64_t Pair[2];
486 HexToIntPair(yytext+3, Pair);
487 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
490 {HexFP128Constant} { uint64_t Pair[2];
491 HexToIntPair(yytext+3, Pair);
492 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair), true);
495 {HexPPC128Constant} { uint64_t Pair[2];
496 HexToIntPair(yytext+3, Pair);
497 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
502 /* Make sure to free the internal buffers for flex when we are
503 * done reading our input!
505 yy_delete_buffer(YY_CURRENT_BUFFER);
509 [ \r\t\n] { /* Ignore whitespace */ }
510 . { return yytext[0]; }