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!");
95 // HexToFP - Convert the ascii string in hexidecimal format to the floating
96 // point representation of it.
98 static double HexToFP(const char *Buffer) {
99 return BitsToDouble(HexIntToVal(Buffer)); // Cast Hex constant to double
103 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
104 // appropriate character.
105 char *UnEscapeLexed(char *Buffer, char* EndBuffer) {
107 for (char *BIn = Buffer; *BIn; ) {
108 if (BIn[0] == '\\') {
109 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
110 *BOut++ = '\\'; // Two \ becomes one
112 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
113 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
114 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
115 BIn[3] = Tmp; // Restore character
116 BIn += 3; // Skip over handled chars
128 } // End llvm namespace
130 using namespace llvm;
132 #define YY_NEVER_INTERACTIVE 1
137 /* Comments start with a ; and go till end of line */
140 /* Local Values and Type identifiers start with a % sign */
141 LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
143 /* Global Value identifiers start with an @ sign */
144 GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
146 /* Label identifiers end with a colon */
147 Label [-a-zA-Z$._0-9]+:
148 QuoteLabel \"[^\"]+\":
150 /* Quoted names can contain any character except " and \ */
151 StringConstant \"[^\"]*\"
152 AtStringConstant @\"[^\"]*\"
153 PctStringConstant %\"[^\"]*\"
155 /* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
159 /* Integer types are specified with i and a bitwidth */
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]+
180 /* WSNL - shorthand for whitespace followed by newline */
184 {Comment} { /* Ignore comments for now */ }
186 begin { return BEGINTOK; }
187 end { return ENDTOK; }
188 true { return TRUETOK; }
189 false { return FALSETOK; }
190 declare { return DECLARE; }
191 define { return DEFINE; }
192 global { return GLOBAL; }
193 constant { return CONSTANT; }
194 internal { return INTERNAL; }
195 linkonce { return LINKONCE; }
196 weak { return WEAK; }
197 appending { return APPENDING; }
198 dllimport { return DLLIMPORT; }
199 dllexport { return DLLEXPORT; }
200 hidden { return HIDDEN; }
201 protected { return PROTECTED; }
202 extern_weak { return EXTERN_WEAK; }
203 external { return EXTERNAL; }
204 thread_local { return THREAD_LOCAL; }
205 zeroinitializer { return ZEROINITIALIZER; }
206 \.\.\. { return DOTDOTDOT; }
207 undef { return UNDEF; }
208 null { return NULL_TOK; }
210 tail { return TAIL; }
211 target { return TARGET; }
212 triple { return TRIPLE; }
213 deplibs { return DEPLIBS; }
214 datalayout { return DATALAYOUT; }
215 volatile { return VOLATILE; }
216 align { return ALIGN; }
217 section { return SECTION; }
218 alias { return ALIAS; }
219 module { return MODULE; }
220 asm { return ASM_TOK; }
221 sideeffect { return SIDEEFFECT; }
223 cc { return CC_TOK; }
224 ccc { return CCC_TOK; }
225 fastcc { return FASTCC_TOK; }
226 coldcc { return COLDCC_TOK; }
227 x86_stdcallcc { return X86_STDCALLCC_TOK; }
228 x86_fastcallcc { return X86_FASTCALLCC_TOK; }
230 signext { return SIGNEXT; }
231 zeroext { return ZEROEXT; }
232 inreg { return INREG; }
233 sret { return SRET; }
234 nounwind { return NOUNWIND; }
235 noreturn { return NORETURN; }
236 noalias { return NOALIAS; }
237 byval { return BYVAL; }
238 nest { return NEST; }
239 sext{WSNL} { // For auto-upgrade only, drop in LLVM 3.0
241 zext{WSNL} { // For auto-upgrade only, drop in LLVM 3.0
244 void { RET_TY(Type::VoidTy, VOID); }
245 float { RET_TY(Type::FloatTy, FLOAT); }
246 double { RET_TY(Type::DoubleTy,DOUBLE);}
247 x86_fp80 { RET_TY(Type::X86_FP80Ty, X86_FP80);}
248 fp128 { RET_TY(Type::FP128Ty, FP128);}
249 ppc_fp128 { RET_TY(Type::PPC_FP128Ty, PPC_FP128);}
250 label { RET_TY(Type::LabelTy, LABEL); }
251 type { return TYPE; }
252 opaque { return OPAQUE; }
253 {IntegerType} { uint64_t NumBits = atoull(yytext+1);
254 if (NumBits < IntegerType::MIN_INT_BITS ||
255 NumBits > IntegerType::MAX_INT_BITS)
256 GenerateError("Bitwidth for integer type out of range!");
257 const Type* Ty = IntegerType::get(NumBits);
261 add { RET_TOK(BinaryOpVal, Add, ADD); }
262 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
263 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
264 udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
265 sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
266 fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
267 urem { RET_TOK(BinaryOpVal, URem, UREM); }
268 srem { RET_TOK(BinaryOpVal, SRem, SREM); }
269 frem { RET_TOK(BinaryOpVal, FRem, FREM); }
270 shl { RET_TOK(BinaryOpVal, Shl, SHL); }
271 lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
272 ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
273 and { RET_TOK(BinaryOpVal, And, AND); }
274 or { RET_TOK(BinaryOpVal, Or , OR ); }
275 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
276 icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
277 fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
300 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
301 call { RET_TOK(OtherOpVal, Call, CALL); }
302 trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
303 zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
304 sext { RET_TOK(CastOpVal, SExt, SEXT); }
305 fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
306 fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
307 uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
308 sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
309 fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
310 fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
311 inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
312 ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
313 bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
314 select { RET_TOK(OtherOpVal, Select, SELECT); }
315 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
316 ret { RET_TOK(TermOpVal, Ret, RET); }
317 br { RET_TOK(TermOpVal, Br, BR); }
318 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
319 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
320 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
321 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
323 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
324 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
325 free { RET_TOK(MemOpVal, Free, FREE); }
326 load { RET_TOK(MemOpVal, Load, LOAD); }
327 store { RET_TOK(MemOpVal, Store, STORE); }
328 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
330 extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
331 insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
332 shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
336 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip %
340 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip @
344 yytext[yyleng-1] = 0; // nuke colon
345 llvmAsmlval.StrVal = new std::string(yytext);
349 yytext[yyleng-2] = 0; // nuke colon, end quote
350 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
352 new std::string(yytext+1, EndChar - yytext - 1);
356 {StringConstant} { yytext[yyleng-1] = 0; // nuke end quote
357 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
359 new std::string(yytext+1, EndChar - yytext - 1);
360 return STRINGCONSTANT;
363 yytext[yyleng-1] = 0; // nuke end quote
364 const char* EndChar =
365 UnEscapeLexed(yytext+2, yytext+yyleng);
367 new std::string(yytext+2, EndChar - yytext - 2);
368 return ATSTRINGCONSTANT;
370 {PctStringConstant} {
371 yytext[yyleng-1] = 0; // nuke end quote
372 const char* EndChar =
373 UnEscapeLexed(yytext+2, yytext+yyleng);
375 new std::string(yytext+2, EndChar - yytext - 2);
376 return PCTSTRINGCONSTANT;
379 uint32_t numBits = ((yyleng * 64) / 19) + 1;
380 APInt Tmp(numBits, yytext, yyleng, 10);
381 uint32_t activeBits = Tmp.getActiveBits();
382 if (activeBits > 0 && activeBits < numBits)
383 Tmp.trunc(activeBits);
384 if (Tmp.getBitWidth() > 64) {
385 llvmAsmlval.APIntVal = new APInt(Tmp);
388 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
393 uint32_t numBits = (((yyleng-1) * 64) / 19) + 2;
394 APInt Tmp(numBits, yytext, yyleng, 10);
395 uint32_t minBits = Tmp.getMinSignedBits();
396 if (minBits > 0 && minBits < numBits)
398 if (Tmp.getBitWidth() > 64) {
399 llvmAsmlval.APIntVal = new APInt(Tmp);
402 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
407 {HexIntConstant} { int len = yyleng - 3;
408 uint32_t bits = len * 4;
409 APInt Tmp(bits, yytext+3, len, 16);
410 uint32_t activeBits = Tmp.getActiveBits();
411 if (activeBits > 0 && activeBits < bits)
412 Tmp.trunc(activeBits);
413 if (Tmp.getBitWidth() > 64) {
414 llvmAsmlval.APIntVal = new APInt(Tmp);
415 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
416 } else if (yytext[0] == 's') {
417 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
420 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
426 uint64_t Val = atoull(yytext+1);
427 if ((unsigned)Val != Val)
428 GenerateError("Invalid value number (too large)!");
429 llvmAsmlval.UIntVal = unsigned(Val);
433 uint64_t Val = atoull(yytext+1);
434 if ((unsigned)Val != Val)
435 GenerateError("Invalid value number (too large)!");
436 llvmAsmlval.UIntVal = unsigned(Val);
440 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
441 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
444 /* Make sure to free the internal buffers for flex when we are
445 * done reading our input!
447 yy_delete_buffer(YY_CURRENT_BUFFER);
451 [ \r\t\n] { /* Ignore whitespace */ }
452 . { return yytext[0]; }