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 // Construct a token value for a non-obsolete token
43 #define RET_TOK(type, Enum, sym) \
44 llvmAsmlval.type = Instruction::Enum; \
47 // Construct a token value for an obsolete token
48 #define RET_TY(CTYPE, SYM) \
49 llvmAsmlval.PrimType = CTYPE;\
54 // TODO: All of the static identifiers are figured out by the lexer,
55 // these should be hashed to reduce the lexer size
58 // atoull - Convert an ascii string of decimal digits into the unsigned long
59 // long representation... this does not have to do input error checking,
60 // because we know that the input will be matched by a suitable regex...
62 static uint64_t atoull(const char *Buffer) {
64 for (; *Buffer; Buffer++) {
65 uint64_t OldRes = Result;
67 Result += *Buffer-'0';
68 if (Result < OldRes) // Uh, oh, overflow detected!!!
69 GenerateError("constant bigger than 64 bits detected!");
74 static uint64_t HexIntToVal(const char *Buffer) {
76 for (; *Buffer; ++Buffer) {
77 uint64_t OldRes = Result;
80 if (C >= '0' && C <= '9')
82 else if (C >= 'A' && C <= 'F')
84 else if (C >= 'a' && C <= 'f')
87 if (Result < OldRes) // Uh, oh, overflow detected!!!
88 GenerateError("constant bigger than 64 bits detected!");
94 // HexToFP - Convert the ascii string in hexidecimal format to the floating
95 // point representation of it.
97 static double HexToFP(const char *Buffer) {
98 // Behave nicely in the face of C TBAA rules... see:
99 // http://www.nullstone.com/htmls/category/aliastyp.htm
104 UIntToFP.UI = HexIntToVal(Buffer);
106 assert(sizeof(double) == sizeof(uint64_t) &&
107 "Data sizes incompatible on this target!");
108 return UIntToFP.FP; // Cast Hex constant to double
112 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
113 // appropriate character. If AllowNull is set to false, a \00 value will cause
114 // an exception to be thrown.
116 // If AllowNull is set to true, the return value of the function points to the
117 // last character of the string in memory.
119 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
121 for (char *BIn = Buffer; *BIn; ) {
122 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
123 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
124 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
125 if (!AllowNull && !*BOut)
126 GenerateError("String literal cannot accept \\00 escape!");
128 BIn[3] = Tmp; // Restore character
129 BIn += 3; // Skip over handled chars
139 } // End llvm namespace
141 using namespace llvm;
143 #define YY_NEVER_INTERACTIVE 1
148 /* Comments start with a ; and go till end of line */
151 /* Local Values and Type identifiers start with a % sign */
152 LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
154 /* Global Value identifiers start with an @ sign */
155 GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
157 /* Label identifiers end with a colon */
158 Label [-a-zA-Z$._0-9]+:
159 QuoteLabel \"[^\"]+\":
161 /* Quoted names can contain any character except " and \ */
162 StringConstant \"[^\"]*\"
163 AtStringConstant @\"[^\"]*\"
165 /* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
169 /* Integer types are specified with i and a bitwidth */
172 /* E[PN]Integer: match positive and negative literal integer values. */
176 /* FPConstant - A Floating point constant.
178 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
180 /* HexFPConstant - Floating point constant represented in IEEE format as a
181 * hexadecimal number for when exponential notation is not precise enough.
183 HexFPConstant 0x[0-9A-Fa-f]+
185 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
186 * it to deal with 64 bit numbers.
188 HexIntConstant [us]0x[0-9A-Fa-f]+
192 {Comment} { /* Ignore comments for now */ }
194 begin { return BEGINTOK; }
195 end { return ENDTOK; }
196 true { return TRUETOK; }
197 false { return FALSETOK; }
198 declare { return DECLARE; }
199 define { return DEFINE; }
200 global { return GLOBAL; }
201 constant { return CONSTANT; }
202 internal { return INTERNAL; }
203 linkonce { return LINKONCE; }
204 weak { return WEAK; }
205 appending { return APPENDING; }
206 dllimport { return DLLIMPORT; }
207 dllexport { return DLLEXPORT; }
208 hidden { return HIDDEN; }
209 extern_weak { return EXTERN_WEAK; }
210 external { return EXTERNAL; }
211 thread_local { return THREAD_LOCAL; }
212 zeroinitializer { return ZEROINITIALIZER; }
213 \.\.\. { return DOTDOTDOT; }
214 undef { return UNDEF; }
215 null { return NULL_TOK; }
217 tail { return TAIL; }
218 target { return TARGET; }
219 triple { return TRIPLE; }
220 deplibs { return DEPLIBS; }
221 datalayout { return DATALAYOUT; }
222 volatile { return VOLATILE; }
223 align { return ALIGN; }
224 section { return SECTION; }
225 alias { return ALIAS; }
226 module { return MODULE; }
227 asm { return ASM_TOK; }
228 sideeffect { return SIDEEFFECT; }
230 cc { return CC_TOK; }
231 ccc { return CCC_TOK; }
232 fastcc { return FASTCC_TOK; }
233 coldcc { return COLDCC_TOK; }
234 x86_stdcallcc { return X86_STDCALLCC_TOK; }
235 x86_fastcallcc { return X86_FASTCALLCC_TOK; }
237 inreg { return INREG; }
238 sret { return SRET; }
239 nounwind { return NOUNWIND; }
240 noreturn { return NORETURN; }
242 void { RET_TY(Type::VoidTy, VOID); }
243 float { RET_TY(Type::FloatTy, FLOAT); }
244 double { RET_TY(Type::DoubleTy,DOUBLE);}
245 label { RET_TY(Type::LabelTy, LABEL); }
246 type { return TYPE; }
247 opaque { return OPAQUE; }
248 {IntegerType} { uint64_t NumBits = atoull(yytext+1);
249 if (NumBits < IntegerType::MIN_INT_BITS ||
250 NumBits > IntegerType::MAX_INT_BITS)
251 GenerateError("Bitwidth for integer type out of range!");
252 const Type* Ty = IntegerType::get(NumBits);
256 add { RET_TOK(BinaryOpVal, Add, ADD); }
257 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
258 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
259 udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
260 sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
261 fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
262 urem { RET_TOK(BinaryOpVal, URem, UREM); }
263 srem { RET_TOK(BinaryOpVal, SRem, SREM); }
264 frem { RET_TOK(BinaryOpVal, FRem, FREM); }
265 shl { RET_TOK(BinaryOpVal, Shl, SHL); }
266 lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
267 ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
268 and { RET_TOK(BinaryOpVal, And, AND); }
269 or { RET_TOK(BinaryOpVal, Or , OR ); }
270 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
271 icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
272 fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
295 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
296 call { RET_TOK(OtherOpVal, Call, CALL); }
297 trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
298 zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
299 sext { RET_TOK(CastOpVal, SExt, SEXT); }
300 fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
301 fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
302 uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
303 sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
304 fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
305 fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
306 inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
307 ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
308 bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
309 select { RET_TOK(OtherOpVal, Select, SELECT); }
310 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
311 ret { RET_TOK(TermOpVal, Ret, RET); }
312 br { RET_TOK(TermOpVal, Br, BR); }
313 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
314 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
315 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
316 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
318 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
319 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
320 free { RET_TOK(MemOpVal, Free, FREE); }
321 load { RET_TOK(MemOpVal, Load, LOAD); }
322 store { RET_TOK(MemOpVal, Store, STORE); }
323 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
325 extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
326 insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
327 shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
331 UnEscapeLexed(yytext+1);
332 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
336 UnEscapeLexed(yytext+1);
337 llvmAsmlval.StrVal = strdup(yytext+1); // Skip @
341 yytext[strlen(yytext)-1] = 0; // nuke colon
342 UnEscapeLexed(yytext);
343 llvmAsmlval.StrVal = strdup(yytext);
347 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
348 UnEscapeLexed(yytext+1);
349 llvmAsmlval.StrVal = strdup(yytext+1);
353 {StringConstant} { // Note that we cannot unescape a string constant here! The
354 // string constant might contain a \00 which would not be
355 // understood by the string stuff. It is valid to make a
356 // [sbyte] c"Hello World\00" constant, for example.
358 yytext[strlen(yytext)-1] = 0; // nuke end quote
359 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
360 return STRINGCONSTANT;
363 yytext[strlen(yytext)-1] = 0; // nuke end quote
364 llvmAsmlval.StrVal = strdup(yytext+2); // Nuke @, quote
365 return ATSTRINGCONSTANT;
368 {PInteger} { int len = strlen(yytext);
369 uint32_t numBits = ((len * 64) / 19) + 1;
370 APInt Tmp(numBits, yytext, len, 10);
371 uint32_t activeBits = Tmp.getActiveBits();
372 if (activeBits > 0 && activeBits < numBits)
373 Tmp.trunc(activeBits);
374 if (Tmp.getBitWidth() > 64) {
375 llvmAsmlval.APIntVal = new APInt(Tmp);
378 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
382 {NInteger} { int len = strlen(yytext);
383 uint32_t numBits = (((len-1) * 64) / 19) + 2;
384 APInt Tmp(numBits, yytext, len, 10);
385 uint32_t minBits = Tmp.getMinSignedBits();
386 if (minBits > 0 && minBits < numBits)
388 if (Tmp.getBitWidth() > 64) {
389 llvmAsmlval.APIntVal = new APInt(Tmp);
392 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
397 {HexIntConstant} { int len = strlen(yytext+3) - 3;
398 uint32_t bits = len * 4;
399 APInt Tmp(bits, yytext+3, len, 16);
400 uint32_t activeBits = Tmp.getActiveBits();
401 if (activeBits > 0 && activeBits < bits)
402 Tmp.trunc(activeBits);
403 if (Tmp.getBitWidth() > 64) {
404 llvmAsmlval.APIntVal = new APInt(Tmp);
405 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
406 } else if (yytext[0] == 's') {
407 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
410 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
416 uint64_t Val = atoull(yytext+1);
417 if ((unsigned)Val != Val)
418 GenerateError("Invalid value number (too large)!");
419 llvmAsmlval.UIntVal = unsigned(Val);
423 uint64_t Val = atoull(yytext+1);
424 if ((unsigned)Val != Val)
425 GenerateError("Invalid value number (too large)!");
426 llvmAsmlval.UIntVal = unsigned(Val);
430 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
431 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
434 /* Make sure to free the internal buffers for flex when we are
435 * done reading our input!
437 yy_delete_buffer(YY_CURRENT_BUFFER);
441 [ \r\t\n] { /* Ignore whitespace */ }
442 . { return yytext[0]; }