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 sext{WSNL} { // For auto-upgrade only, drop in LLVM 3.0
280 zext{WSNL} { // For auto-upgrade only, drop in LLVM 3.0
283 void { RET_TY(Type::VoidTy, VOID); }
284 float { RET_TY(Type::FloatTy, FLOAT); }
285 double { RET_TY(Type::DoubleTy,DOUBLE);}
286 x86_fp80 { RET_TY(Type::X86_FP80Ty, X86_FP80);}
287 fp128 { RET_TY(Type::FP128Ty, FP128);}
288 ppc_fp128 { RET_TY(Type::PPC_FP128Ty, PPC_FP128);}
289 label { RET_TY(Type::LabelTy, LABEL); }
290 type { return TYPE; }
291 opaque { return OPAQUE; }
292 {IntegerType} { uint64_t NumBits = atoull(yytext+1);
293 if (NumBits < IntegerType::MIN_INT_BITS ||
294 NumBits > IntegerType::MAX_INT_BITS)
295 GenerateError("Bitwidth for integer type out of range!");
296 const Type* Ty = IntegerType::get(NumBits);
300 add { RET_TOK(BinaryOpVal, Add, ADD); }
301 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
302 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
303 udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
304 sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
305 fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
306 urem { RET_TOK(BinaryOpVal, URem, UREM); }
307 srem { RET_TOK(BinaryOpVal, SRem, SREM); }
308 frem { RET_TOK(BinaryOpVal, FRem, FREM); }
309 shl { RET_TOK(BinaryOpVal, Shl, SHL); }
310 lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
311 ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
312 and { RET_TOK(BinaryOpVal, And, AND); }
313 or { RET_TOK(BinaryOpVal, Or , OR ); }
314 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
315 icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
316 fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
339 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
340 call { RET_TOK(OtherOpVal, Call, CALL); }
341 trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
342 zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
343 sext { RET_TOK(CastOpVal, SExt, SEXT); }
344 fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
345 fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
346 uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
347 sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
348 fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
349 fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
350 inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
351 ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
352 bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
353 select { RET_TOK(OtherOpVal, Select, SELECT); }
354 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
355 ret { RET_TOK(TermOpVal, Ret, RET); }
356 br { RET_TOK(TermOpVal, Br, BR); }
357 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
358 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
359 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
360 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
362 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
363 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
364 free { RET_TOK(MemOpVal, Free, FREE); }
365 load { RET_TOK(MemOpVal, Load, LOAD); }
366 store { RET_TOK(MemOpVal, Store, STORE); }
367 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
369 extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
370 insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
371 shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
375 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip %
379 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip @
383 yytext[yyleng-1] = 0; // nuke colon
384 llvmAsmlval.StrVal = new std::string(yytext);
388 yytext[yyleng-2] = 0; // nuke colon, end quote
389 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
391 new std::string(yytext+1, EndChar - yytext - 1);
395 {StringConstant} { yytext[yyleng-1] = 0; // nuke end quote
396 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
398 new std::string(yytext+1, EndChar - yytext - 1);
399 return STRINGCONSTANT;
402 yytext[yyleng-1] = 0; // nuke end quote
403 const char* EndChar =
404 UnEscapeLexed(yytext+2, yytext+yyleng);
406 new std::string(yytext+2, EndChar - yytext - 2);
407 return ATSTRINGCONSTANT;
409 {PctStringConstant} {
410 yytext[yyleng-1] = 0; // nuke end quote
411 const char* EndChar =
412 UnEscapeLexed(yytext+2, yytext+yyleng);
414 new std::string(yytext+2, EndChar - yytext - 2);
415 return PCTSTRINGCONSTANT;
418 uint32_t numBits = ((yyleng * 64) / 19) + 1;
419 APInt Tmp(numBits, yytext, yyleng, 10);
420 uint32_t activeBits = Tmp.getActiveBits();
421 if (activeBits > 0 && activeBits < numBits)
422 Tmp.trunc(activeBits);
423 if (Tmp.getBitWidth() > 64) {
424 llvmAsmlval.APIntVal = new APInt(Tmp);
427 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
432 uint32_t numBits = (((yyleng-1) * 64) / 19) + 2;
433 APInt Tmp(numBits, yytext, yyleng, 10);
434 uint32_t minBits = Tmp.getMinSignedBits();
435 if (minBits > 0 && minBits < numBits)
437 if (Tmp.getBitWidth() > 64) {
438 llvmAsmlval.APIntVal = new APInt(Tmp);
441 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
446 {HexIntConstant} { int len = yyleng - 3;
447 uint32_t bits = len * 4;
448 APInt Tmp(bits, yytext+3, len, 16);
449 uint32_t activeBits = Tmp.getActiveBits();
450 if (activeBits > 0 && activeBits < bits)
451 Tmp.trunc(activeBits);
452 if (Tmp.getBitWidth() > 64) {
453 llvmAsmlval.APIntVal = new APInt(Tmp);
454 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
455 } else if (yytext[0] == 's') {
456 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
459 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
465 uint64_t Val = atoull(yytext+1);
466 if ((unsigned)Val != Val)
467 GenerateError("Invalid value number (too large)!");
468 llvmAsmlval.UIntVal = unsigned(Val);
472 uint64_t Val = atoull(yytext+1);
473 if ((unsigned)Val != Val)
474 GenerateError("Invalid value number (too large)!");
475 llvmAsmlval.UIntVal = unsigned(Val);
479 {FPConstant} { llvmAsmlval.FPVal = new APFloat(atof(yytext)); return FPVAL; }
480 {HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext+2));
483 {HexFP80Constant} { uint64_t Pair[2];
484 HexToIntPair(yytext+3, Pair);
485 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
488 {HexFP128Constant} { uint64_t Pair[2];
489 HexToIntPair(yytext+3, Pair);
490 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
493 {HexPPC128Constant} { uint64_t Pair[2];
494 HexToIntPair(yytext+3, Pair);
495 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
500 /* Make sure to free the internal buffers for flex when we are
501 * done reading our input!
503 yy_delete_buffer(YY_CURRENT_BUFFER);
507 [ \r\t\n] { /* Ignore whitespace */ }
508 . { return yytext[0]; }