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 /* Variable(Value) identifiers start with a % sign */
152 VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
154 /* Label identifiers end with a colon */
155 Label [-a-zA-Z$._0-9]+:
156 QuoteLabel \"[^\"]+\":
158 /* Quoted names can contain any character except " and \ */
159 StringConstant \"[^\"]*\"
162 /* [PN]Integer: match positive and negative literal integer values that
163 * are preceeded by a '%' character. These represent unnamed variable slots.
169 /* E[PN]Integer: match positive and negative literal integer values */
173 /* FPConstant - A Floating point constant.
175 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
177 /* HexFPConstant - Floating point constant represented in IEEE format as a
178 * hexadecimal number for when exponential notation is not precise enough.
180 HexFPConstant 0x[0-9A-Fa-f]+
182 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
183 * it to deal with 64 bit numbers.
185 HexIntConstant [us]0x[0-9A-Fa-f]+
188 {Comment} { /* Ignore comments for now */ }
190 begin { return BEGINTOK; }
191 end { return ENDTOK; }
192 true { return TRUETOK; }
193 false { return FALSETOK; }
194 declare { return DECLARE; }
195 define { return DEFINE; }
196 global { return GLOBAL; }
197 constant { return CONSTANT; }
198 internal { return INTERNAL; }
199 linkonce { return LINKONCE; }
200 weak { return WEAK; }
201 appending { return APPENDING; }
202 dllimport { return DLLIMPORT; }
203 dllexport { return DLLEXPORT; }
204 extern_weak { return EXTERN_WEAK; }
205 external { return EXTERNAL; }
206 implementation { return IMPLEMENTATION; }
207 zeroinitializer { return ZEROINITIALIZER; }
208 \.\.\. { return DOTDOTDOT; }
209 undef { return UNDEF; }
210 null { return NULL_TOK; }
212 tail { return TAIL; }
213 target { return TARGET; }
214 triple { return TRIPLE; }
215 deplibs { return DEPLIBS; }
216 endian { return ENDIAN; }
217 pointersize { return POINTERSIZE; }
218 datalayout { return DATALAYOUT; }
219 little { return LITTLE; }
221 volatile { return VOLATILE; }
222 align { return ALIGN; }
223 section { return SECTION; }
224 module { return MODULE; }
225 asm { return ASM_TOK; }
226 sideeffect { return SIDEEFFECT; }
228 cc { return CC_TOK; }
229 ccc { return CCC_TOK; }
230 csretcc { return CSRETCC_TOK; }
231 fastcc { return FASTCC_TOK; }
232 coldcc { return COLDCC_TOK; }
233 x86_stdcallcc { return X86_STDCALLCC_TOK; }
234 x86_fastcallcc { return X86_FASTCALLCC_TOK; }
236 void { RET_TY(Type::VoidTy, VOID); }
237 bool { RET_TY(Type::BoolTy, BOOL); }
238 i8 { RET_TY(Type::Int8Ty, INT8); }
239 i16 { RET_TY(Type::Int16Ty, INT16); }
240 i32 { RET_TY(Type::Int32Ty, INT32); }
241 i64 { RET_TY(Type::Int64Ty, INT64); }
242 float { RET_TY(Type::FloatTy, FLOAT); }
243 double { RET_TY(Type::DoubleTy,DOUBLE);}
244 label { RET_TY(Type::LabelTy, LABEL); }
245 type { return TYPE; }
246 opaque { return OPAQUE; }
248 add { RET_TOK(BinaryOpVal, Add, ADD); }
249 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
250 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
251 udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
252 sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
253 fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
254 urem { RET_TOK(BinaryOpVal, URem, UREM); }
255 srem { RET_TOK(BinaryOpVal, SRem, SREM); }
256 frem { RET_TOK(BinaryOpVal, FRem, FREM); }
257 and { RET_TOK(BinaryOpVal, And, AND); }
258 or { RET_TOK(BinaryOpVal, Or , OR ); }
259 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
260 icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
261 fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
283 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
284 call { RET_TOK(OtherOpVal, Call, CALL); }
285 trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
286 zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
287 sext { RET_TOK(CastOpVal, SExt, SEXT); }
288 fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
289 fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
290 uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
291 sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
292 fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
293 fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
294 inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
295 ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
296 bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
297 select { RET_TOK(OtherOpVal, Select, SELECT); }
298 shl { RET_TOK(OtherOpVal, Shl, SHL); }
299 lshr { RET_TOK(OtherOpVal, LShr, LSHR); }
300 ashr { RET_TOK(OtherOpVal, AShr, ASHR); }
301 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
302 ret { RET_TOK(TermOpVal, Ret, RET); }
303 br { RET_TOK(TermOpVal, Br, BR); }
304 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
305 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
306 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
307 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
309 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
310 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
311 free { RET_TOK(MemOpVal, Free, FREE); }
312 load { RET_TOK(MemOpVal, Load, LOAD); }
313 store { RET_TOK(MemOpVal, Store, STORE); }
314 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
316 extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
317 insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
318 shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
322 UnEscapeLexed(yytext+1);
323 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
327 yytext[strlen(yytext)-1] = 0; // nuke colon
328 UnEscapeLexed(yytext);
329 llvmAsmlval.StrVal = strdup(yytext);
333 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
334 UnEscapeLexed(yytext+1);
335 llvmAsmlval.StrVal = strdup(yytext+1);
339 {StringConstant} { // Note that we cannot unescape a string constant here! The
340 // string constant might contain a \00 which would not be
341 // understood by the string stuff. It is valid to make a
342 // [sbyte] c"Hello World\00" constant, for example.
344 yytext[strlen(yytext)-1] = 0; // nuke end quote
345 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
346 return STRINGCONSTANT;
350 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
352 uint64_t Val = atoull(yytext+1);
353 // +1: we have bigger negative range
354 if (Val > (uint64_t)INT64_MAX+1)
355 GenerateError("Constant too large for signed 64 bits!");
356 llvmAsmlval.SInt64Val = -Val;
360 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
361 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
365 uint64_t Val = atoull(yytext+1);
366 if ((unsigned)Val != Val)
367 GenerateError("Invalid value number (too large)!");
368 llvmAsmlval.UIntVal = unsigned(Val);
372 uint64_t Val = atoull(yytext+2);
373 // +1: we have bigger negative range
374 if (Val > (uint64_t)INT32_MAX+1)
375 GenerateError("Constant too large for signed 32 bits!");
376 llvmAsmlval.SIntVal = (int)-Val;
380 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
381 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
384 /* Make sure to free the internal buffers for flex when we are
385 * done reading our input!
387 yy_delete_buffer(YY_CURRENT_BUFFER);
391 [ \r\t\n] { /* Ignore whitespace */ }
392 . { return yytext[0]; }