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.opcode = Instruction::Enum; \
45 llvmAsmlval.type.obsolete = false; \
48 // Construct a token value for an obsolete token
49 #define RET_TOK_OBSOLETE(type, Enum, sym) \
50 llvmAsmlval.type.opcode = Instruction::Enum; \
51 llvmAsmlval.type.obsolete = true; \
54 // Construct a token value for an obsolete token
55 #define RET_TY(CTYPE, SIGN, SYM) \
56 llvmAsmlval.TypeVal.type = new PATypeHolder(CTYPE); \
57 llvmAsmlval.TypeVal.signedness = SIGN; \
62 // TODO: All of the static identifiers are figured out by the lexer,
63 // these should be hashed to reduce the lexer size
66 // atoull - Convert an ascii string of decimal digits into the unsigned long
67 // long representation... this does not have to do input error checking,
68 // because we know that the input will be matched by a suitable regex...
70 static uint64_t atoull(const char *Buffer) {
72 for (; *Buffer; Buffer++) {
73 uint64_t OldRes = Result;
75 Result += *Buffer-'0';
76 if (Result < OldRes) // Uh, oh, overflow detected!!!
77 GenerateError("constant bigger than 64 bits detected!");
82 static uint64_t HexIntToVal(const char *Buffer) {
84 for (; *Buffer; ++Buffer) {
85 uint64_t OldRes = Result;
88 if (C >= '0' && C <= '9')
90 else if (C >= 'A' && C <= 'F')
92 else if (C >= 'a' && C <= 'f')
95 if (Result < OldRes) // Uh, oh, overflow detected!!!
96 GenerateError("constant bigger than 64 bits detected!");
102 // HexToFP - Convert the ascii string in hexidecimal format to the floating
103 // point representation of it.
105 static double HexToFP(const char *Buffer) {
106 // Behave nicely in the face of C TBAA rules... see:
107 // http://www.nullstone.com/htmls/category/aliastyp.htm
112 UIntToFP.UI = HexIntToVal(Buffer);
114 assert(sizeof(double) == sizeof(uint64_t) &&
115 "Data sizes incompatible on this target!");
116 return UIntToFP.FP; // Cast Hex constant to double
120 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
121 // appropriate character. If AllowNull is set to false, a \00 value will cause
122 // an exception to be thrown.
124 // If AllowNull is set to true, the return value of the function points to the
125 // last character of the string in memory.
127 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
129 for (char *BIn = Buffer; *BIn; ) {
130 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
131 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
132 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
133 if (!AllowNull && !*BOut)
134 GenerateError("String literal cannot accept \\00 escape!");
136 BIn[3] = Tmp; // Restore character
137 BIn += 3; // Skip over handled chars
147 } // End llvm namespace
149 using namespace llvm;
151 #define YY_NEVER_INTERACTIVE 1
156 /* Comments start with a ; and go till end of line */
159 /* Variable(Value) identifiers start with a % sign */
160 VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
162 /* Label identifiers end with a colon */
163 Label [-a-zA-Z$._0-9]+:
164 QuoteLabel \"[^\"]+\":
166 /* Quoted names can contain any character except " and \ */
167 StringConstant \"[^\"]*\"
170 /* [PN]Integer: match positive and negative literal integer values that
171 * are preceeded by a '%' character. These represent unnamed variable slots.
177 /* E[PN]Integer: match positive and negative literal integer values */
181 /* FPConstant - A Floating point constant.
183 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
185 /* HexFPConstant - Floating point constant represented in IEEE format as a
186 * hexadecimal number for when exponential notation is not precise enough.
188 HexFPConstant 0x[0-9A-Fa-f]+
190 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
191 * it to deal with 64 bit numbers.
193 HexIntConstant [us]0x[0-9A-Fa-f]+
196 {Comment} { /* Ignore comments for now */ }
198 begin { return BEGINTOK; }
199 end { return ENDTOK; }
200 true { return TRUETOK; }
201 false { return FALSETOK; }
202 declare { return DECLARE; }
203 global { return GLOBAL; }
204 constant { return CONSTANT; }
205 internal { return INTERNAL; }
206 linkonce { return LINKONCE; }
207 weak { return WEAK; }
208 appending { return APPENDING; }
209 dllimport { return DLLIMPORT; }
210 dllexport { return DLLEXPORT; }
211 extern_weak { return EXTERN_WEAK; }
212 uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
213 external { return EXTERNAL; }
214 implementation { return IMPLEMENTATION; }
215 zeroinitializer { return ZEROINITIALIZER; }
216 \.\.\. { return DOTDOTDOT; }
217 undef { return UNDEF; }
218 null { return NULL_TOK; }
220 except { RET_TOK(TermOpVal, Unwind, UNWIND); }
221 not { return NOT; } /* Deprecated, turned into XOR */
222 tail { return TAIL; }
223 target { return TARGET; }
224 triple { return TRIPLE; }
225 deplibs { return DEPLIBS; }
226 endian { return ENDIAN; }
227 pointersize { return POINTERSIZE; }
228 datalayout { return DATALAYOUT; }
229 little { return LITTLE; }
231 volatile { return VOLATILE; }
232 align { return ALIGN; }
233 section { return SECTION; }
234 module { return MODULE; }
235 asm { return ASM_TOK; }
236 sideeffect { return SIDEEFFECT; }
238 cc { return CC_TOK; }
239 ccc { return CCC_TOK; }
240 csretcc { return CSRETCC_TOK; }
241 fastcc { return FASTCC_TOK; }
242 coldcc { return COLDCC_TOK; }
243 x86_stdcallcc { return X86_STDCALLCC_TOK; }
244 x86_fastcallcc { return X86_FASTCALLCC_TOK; }
246 void { RET_TY(Type::VoidTy, isSignless, VOID); }
247 bool { RET_TY(Type::BoolTy, isSignless, BOOL); }
248 sbyte { RET_TY(Type::SByteTy, isSigned, SBYTE); }
249 ubyte { RET_TY(Type::UByteTy, isUnsigned, UBYTE); }
250 short { RET_TY(Type::ShortTy, isSigned, SHORT); }
251 ushort { RET_TY(Type::UShortTy,isUnsigned, USHORT);}
252 int { RET_TY(Type::IntTy, isSigned, INT); }
253 uint { RET_TY(Type::UIntTy, isUnsigned, UINT); }
254 long { RET_TY(Type::LongTy, isSigned, LONG); }
255 ulong { RET_TY(Type::ULongTy, isUnsigned, ULONG); }
256 float { RET_TY(Type::FloatTy, isSignless, FLOAT); }
257 double { RET_TY(Type::DoubleTy,isSignless, DOUBLE);}
258 label { RET_TY(Type::LabelTy, isSignless, LABEL); }
259 type { return TYPE; }
260 opaque { return OPAQUE; }
262 add { RET_TOK(BinaryOpVal, Add, ADD); }
263 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
264 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
265 div { RET_TOK_OBSOLETE(BinaryOpVal, UDiv, UDIV); }
266 udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
267 sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
268 fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
269 rem { RET_TOK_OBSOLETE(BinaryOpVal, URem, UREM); }
270 urem { RET_TOK(BinaryOpVal, URem, UREM); }
271 srem { RET_TOK(BinaryOpVal, SRem, SREM); }
272 frem { RET_TOK(BinaryOpVal, FRem, FREM); }
273 and { RET_TOK(BinaryOpVal, And, AND); }
274 or { RET_TOK(BinaryOpVal, Or , OR ); }
275 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
276 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
277 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
278 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
279 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
280 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
281 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
283 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
284 call { RET_TOK(OtherOpVal, Call, CALL); }
285 cast { RET_TOK_OBSOLETE(CastOpVal, Trunc, TRUNC); }
286 trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
287 zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
288 sext { RET_TOK(CastOpVal, SExt, SEXT); }
289 fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
290 fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
291 uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
292 sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
293 fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
294 fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
295 inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
296 ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
297 bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
298 select { RET_TOK(OtherOpVal, Select, SELECT); }
299 shl { RET_TOK(OtherOpVal, Shl, SHL); }
300 shr { RET_TOK_OBSOLETE(OtherOpVal, LShr, LSHR); }
301 lshr { RET_TOK(OtherOpVal, LShr, LSHR); }
302 ashr { RET_TOK(OtherOpVal, AShr, ASHR); }
303 vanext { return VANEXT_old; }
304 vaarg { return VAARG_old; }
305 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
306 ret { RET_TOK(TermOpVal, Ret, RET); }
307 br { RET_TOK(TermOpVal, Br, BR); }
308 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
309 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
310 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
311 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
313 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
314 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
315 free { RET_TOK(MemOpVal, Free, FREE); }
316 load { RET_TOK(MemOpVal, Load, LOAD); }
317 store { RET_TOK(MemOpVal, Store, STORE); }
318 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
320 extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
321 insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
322 shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
326 UnEscapeLexed(yytext+1);
327 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
331 yytext[strlen(yytext)-1] = 0; // nuke colon
332 UnEscapeLexed(yytext);
333 llvmAsmlval.StrVal = strdup(yytext);
337 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
338 UnEscapeLexed(yytext+1);
339 llvmAsmlval.StrVal = strdup(yytext+1);
343 {StringConstant} { // Note that we cannot unescape a string constant here! The
344 // string constant might contain a \00 which would not be
345 // understood by the string stuff. It is valid to make a
346 // [sbyte] c"Hello World\00" constant, for example.
348 yytext[strlen(yytext)-1] = 0; // nuke end quote
349 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
350 return STRINGCONSTANT;
354 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
356 uint64_t Val = atoull(yytext+1);
357 // +1: we have bigger negative range
358 if (Val > (uint64_t)INT64_MAX+1)
359 GenerateError("Constant too large for signed 64 bits!");
360 llvmAsmlval.SInt64Val = -Val;
364 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
365 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
369 uint64_t Val = atoull(yytext+1);
370 if ((unsigned)Val != Val)
371 GenerateError("Invalid value number (too large)!");
372 llvmAsmlval.UIntVal = unsigned(Val);
376 uint64_t Val = atoull(yytext+2);
377 // +1: we have bigger negative range
378 if (Val > (uint64_t)INT32_MAX+1)
379 GenerateError("Constant too large for signed 32 bits!");
380 llvmAsmlval.SIntVal = (int)-Val;
384 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
385 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
388 /* Make sure to free the internal buffers for flex when we are
389 * done reading our input!
391 yy_delete_buffer(YY_CURRENT_BUFFER);
395 [ \r\t\n] { /* Ignore whitespace */ }
396 . { return yytext[0]; }