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 #define RET_ENUM(type, Enum, sym) \
48 llvmAsmlval.type = Enum; \
51 // Construct a token value for an obsolete token
52 #define RET_TY(CTYPE, SYM) \
53 llvmAsmlval.PrimType = CTYPE;\
58 // TODO: All of the static identifiers are figured out by the lexer,
59 // these should be hashed to reduce the lexer size
62 // atoull - Convert an ascii string of decimal digits into the unsigned long
63 // long representation... this does not have to do input error checking,
64 // because we know that the input will be matched by a suitable regex...
66 static uint64_t atoull(const char *Buffer) {
68 for (; *Buffer; Buffer++) {
69 uint64_t OldRes = Result;
71 Result += *Buffer-'0';
72 if (Result < OldRes) // Uh, oh, overflow detected!!!
73 GenerateError("constant bigger than 64 bits detected!");
78 static uint64_t HexIntToVal(const char *Buffer) {
80 for (; *Buffer; ++Buffer) {
81 uint64_t OldRes = Result;
84 if (C >= '0' && C <= '9')
86 else if (C >= 'A' && C <= 'F')
88 else if (C >= 'a' && C <= 'f')
91 if (Result < OldRes) // Uh, oh, overflow detected!!!
92 GenerateError("constant bigger than 64 bits detected!");
98 // HexToFP - Convert the ascii string in hexidecimal format to the floating
99 // point representation of it.
101 static double HexToFP(const char *Buffer) {
102 // Behave nicely in the face of C TBAA rules... see:
103 // http://www.nullstone.com/htmls/category/aliastyp.htm
108 UIntToFP.UI = HexIntToVal(Buffer);
110 assert(sizeof(double) == sizeof(uint64_t) &&
111 "Data sizes incompatible on this target!");
112 return UIntToFP.FP; // Cast Hex constant to double
116 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
117 // appropriate character. If AllowNull is set to false, a \00 value will cause
118 // an exception to be thrown.
120 // If AllowNull is set to true, the return value of the function points to the
121 // last character of the string in memory.
123 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
125 for (char *BIn = Buffer; *BIn; ) {
126 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
127 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
128 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
129 if (!AllowNull && !*BOut)
130 GenerateError("String literal cannot accept \\00 escape!");
132 BIn[3] = Tmp; // Restore character
133 BIn += 3; // Skip over handled chars
143 } // End llvm namespace
145 using namespace llvm;
147 #define YY_NEVER_INTERACTIVE 1
152 /* Comments start with a ; and go till end of line */
155 /* Variable(Value) identifiers start with a % sign */
156 VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
158 /* Label identifiers end with a colon */
159 Label [-a-zA-Z$._0-9]+:
160 QuoteLabel \"[^\"]+\":
162 /* Quoted names can contain any character except " and \ */
163 StringConstant \"[^\"]*\"
166 /* [PN]Integer: match positive and negative literal integer values that
167 * are preceeded by a '%' character. These represent unnamed variable slots.
173 /* E[PN]Integer: match positive and negative literal integer values */
177 /* FPConstant - A Floating point constant.
179 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
181 /* HexFPConstant - Floating point constant represented in IEEE format as a
182 * hexadecimal number for when exponential notation is not precise enough.
184 HexFPConstant 0x[0-9A-Fa-f]+
186 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
187 * it to deal with 64 bit numbers.
189 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 global { return GLOBAL; }
200 constant { return CONSTANT; }
201 internal { return INTERNAL; }
202 linkonce { return LINKONCE; }
203 weak { return WEAK; }
204 appending { return APPENDING; }
205 dllimport { return DLLIMPORT; }
206 dllexport { return DLLEXPORT; }
207 extern_weak { return EXTERN_WEAK; }
208 external { return EXTERNAL; }
209 implementation { return IMPLEMENTATION; }
210 zeroinitializer { return ZEROINITIALIZER; }
211 \.\.\. { return DOTDOTDOT; }
212 undef { return UNDEF; }
213 null { return NULL_TOK; }
215 tail { return TAIL; }
216 target { return TARGET; }
217 triple { return TRIPLE; }
218 deplibs { return DEPLIBS; }
219 endian { return ENDIAN; }
220 pointersize { return POINTERSIZE; }
221 datalayout { return DATALAYOUT; }
222 little { return LITTLE; }
224 volatile { return VOLATILE; }
225 align { return ALIGN; }
226 section { return SECTION; }
227 module { return MODULE; }
228 asm { return ASM_TOK; }
229 sideeffect { return SIDEEFFECT; }
231 cc { return CC_TOK; }
232 ccc { return CCC_TOK; }
233 csretcc { return CSRETCC_TOK; }
234 fastcc { return FASTCC_TOK; }
235 coldcc { return COLDCC_TOK; }
236 x86_stdcallcc { return X86_STDCALLCC_TOK; }
237 x86_fastcallcc { return X86_FASTCALLCC_TOK; }
239 void { RET_TY(Type::VoidTy, VOID); }
240 bool { RET_TY(Type::BoolTy, BOOL); }
241 sbyte { RET_TY(Type::SByteTy, SBYTE); }
242 ubyte { RET_TY(Type::UByteTy, UBYTE); }
243 short { RET_TY(Type::ShortTy, SHORT); }
244 ushort { RET_TY(Type::UShortTy,USHORT);}
245 int { RET_TY(Type::IntTy, INT); }
246 uint { RET_TY(Type::UIntTy, UINT); }
247 long { RET_TY(Type::LongTy, LONG); }
248 ulong { RET_TY(Type::ULongTy, ULONG); }
249 float { RET_TY(Type::FloatTy, FLOAT); }
250 double { RET_TY(Type::DoubleTy,DOUBLE);}
251 label { RET_TY(Type::LabelTy, LABEL); }
252 type { return TYPE; }
253 opaque { return OPAQUE; }
255 add { RET_TOK(BinaryOpVal, Add, ADD); }
256 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
257 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
258 udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
259 sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
260 fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
261 urem { RET_TOK(BinaryOpVal, URem, UREM); }
262 srem { RET_TOK(BinaryOpVal, SRem, SREM); }
263 frem { RET_TOK(BinaryOpVal, FRem, FREM); }
264 and { RET_TOK(BinaryOpVal, And, AND); }
265 or { RET_TOK(BinaryOpVal, Or , OR ); }
266 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
267 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
268 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
269 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
270 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
271 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
272 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
273 icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
274 fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
275 eq { RET_ENUM(IPredicate, ICmpInst::ICMP_EQ, EQ); }
276 ne { RET_ENUM(IPredicate, ICmpInst::ICMP_NE, NE); }
277 slt { RET_ENUM(IPredicate, ICmpInst::ICMP_SLT, SLT); }
278 sgt { RET_ENUM(IPredicate, ICmpInst::ICMP_SGT, SGT); }
279 sle { RET_ENUM(IPredicate, ICmpInst::ICMP_SLE, SLE); }
280 sge { RET_ENUM(IPredicate, ICmpInst::ICMP_SGE, SGE); }
281 ult { RET_ENUM(IPredicate, ICmpInst::ICMP_ULT, ULT); }
282 ugt { RET_ENUM(IPredicate, ICmpInst::ICMP_UGT, UGT); }
283 ule { RET_ENUM(IPredicate, ICmpInst::ICMP_ULE, ULE); }
284 uge { RET_ENUM(IPredicate, ICmpInst::ICMP_UGE, UGE); }
285 ordeq { RET_ENUM(FPredicate, FCmpInst::FCMP_OEQ, ORDEQ); }
286 ordne { RET_ENUM(FPredicate, FCmpInst::FCMP_ONE, ORDNE); }
287 ordlt { RET_ENUM(FPredicate, FCmpInst::FCMP_OLT, ORDLT); }
288 ordgt { RET_ENUM(FPredicate, FCmpInst::FCMP_OGT, ORDGT); }
289 ordle { RET_ENUM(FPredicate, FCmpInst::FCMP_OLE, ORDLE); }
290 ordge { RET_ENUM(FPredicate, FCmpInst::FCMP_OGE, ORDGE); }
291 ord { RET_ENUM(FPredicate, FCmpInst::FCMP_ORD, ORD); }
292 uno { RET_ENUM(FPredicate, FCmpInst::FCMP_UNO, UNO); }
293 unoeq { RET_ENUM(FPredicate, FCmpInst::FCMP_UEQ, UNOEQ); }
294 unone { RET_ENUM(FPredicate, FCmpInst::FCMP_UNE, UNONE); }
295 unolt { RET_ENUM(FPredicate, FCmpInst::FCMP_ULT, UNOLT); }
296 unogt { RET_ENUM(FPredicate, FCmpInst::FCMP_UGT, UNOGT); }
297 unole { RET_ENUM(FPredicate, FCmpInst::FCMP_ULE, UNOLE); }
298 unoge { RET_ENUM(FPredicate, FCmpInst::FCMP_UGE, UNOGE); }
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 shl { RET_TOK(OtherOpVal, Shl, SHL); }
316 lshr { RET_TOK(OtherOpVal, LShr, LSHR); }
317 ashr { RET_TOK(OtherOpVal, AShr, ASHR); }
318 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
319 ret { RET_TOK(TermOpVal, Ret, RET); }
320 br { RET_TOK(TermOpVal, Br, BR); }
321 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
322 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
323 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
324 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
326 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
327 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
328 free { RET_TOK(MemOpVal, Free, FREE); }
329 load { RET_TOK(MemOpVal, Load, LOAD); }
330 store { RET_TOK(MemOpVal, Store, STORE); }
331 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
333 extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
334 insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
335 shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
339 UnEscapeLexed(yytext+1);
340 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
344 yytext[strlen(yytext)-1] = 0; // nuke colon
345 UnEscapeLexed(yytext);
346 llvmAsmlval.StrVal = strdup(yytext);
350 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
351 UnEscapeLexed(yytext+1);
352 llvmAsmlval.StrVal = strdup(yytext+1);
356 {StringConstant} { // Note that we cannot unescape a string constant here! The
357 // string constant might contain a \00 which would not be
358 // understood by the string stuff. It is valid to make a
359 // [sbyte] c"Hello World\00" constant, for example.
361 yytext[strlen(yytext)-1] = 0; // nuke end quote
362 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
363 return STRINGCONSTANT;
367 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
369 uint64_t Val = atoull(yytext+1);
370 // +1: we have bigger negative range
371 if (Val > (uint64_t)INT64_MAX+1)
372 GenerateError("Constant too large for signed 64 bits!");
373 llvmAsmlval.SInt64Val = -Val;
377 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
378 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
382 uint64_t Val = atoull(yytext+1);
383 if ((unsigned)Val != Val)
384 GenerateError("Invalid value number (too large)!");
385 llvmAsmlval.UIntVal = unsigned(Val);
389 uint64_t Val = atoull(yytext+2);
390 // +1: we have bigger negative range
391 if (Val > (uint64_t)INT32_MAX+1)
392 GenerateError("Constant too large for signed 32 bits!");
393 llvmAsmlval.SIntVal = (int)-Val;
397 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
398 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
401 /* Make sure to free the internal buffers for flex when we are
402 * done reading our input!
404 yy_delete_buffer(YY_CURRENT_BUFFER);
408 [ \r\t\n] { /* Ignore whitespace */ }
409 . { return yytext[0]; }