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