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; \
57 // TODO: All of the static identifiers are figured out by the lexer,
58 // these should be hashed to reduce the lexer size
61 // atoull - Convert an ascii string of decimal digits into the unsigned long
62 // long representation... this does not have to do input error checking,
63 // because we know that the input will be matched by a suitable regex...
65 static uint64_t atoull(const char *Buffer) {
67 for (; *Buffer; Buffer++) {
68 uint64_t OldRes = Result;
70 Result += *Buffer-'0';
71 if (Result < OldRes) // Uh, oh, overflow detected!!!
72 GenerateError("constant bigger than 64 bits detected!");
77 static uint64_t HexIntToVal(const char *Buffer) {
79 for (; *Buffer; ++Buffer) {
80 uint64_t OldRes = Result;
83 if (C >= '0' && C <= '9')
85 else if (C >= 'A' && C <= 'F')
87 else if (C >= 'a' && C <= 'f')
90 if (Result < OldRes) // Uh, oh, overflow detected!!!
91 GenerateError("constant bigger than 64 bits detected!");
97 // HexToFP - Convert the ascii string in hexidecimal format to the floating
98 // point representation of it.
100 static double HexToFP(const char *Buffer) {
101 // Behave nicely in the face of C TBAA rules... see:
102 // http://www.nullstone.com/htmls/category/aliastyp.htm
107 UIntToFP.UI = HexIntToVal(Buffer);
109 assert(sizeof(double) == sizeof(uint64_t) &&
110 "Data sizes incompatible on this target!");
111 return UIntToFP.FP; // Cast Hex constant to double
115 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
116 // appropriate character. If AllowNull is set to false, a \00 value will cause
117 // an exception to be thrown.
119 // If AllowNull is set to true, the return value of the function points to the
120 // last character of the string in memory.
122 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
124 for (char *BIn = Buffer; *BIn; ) {
125 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
126 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
127 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
128 if (!AllowNull && !*BOut)
129 GenerateError("String literal cannot accept \\00 escape!");
131 BIn[3] = Tmp; // Restore character
132 BIn += 3; // Skip over handled chars
142 } // End llvm namespace
144 using namespace llvm;
146 #define YY_NEVER_INTERACTIVE 1
151 /* Comments start with a ; and go till end of line */
154 /* Variable(Value) identifiers start with a % sign */
155 VarID %[-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 \"[^\"]*\"
165 /* [PN]Integer: match positive and negative literal integer values that
166 * are preceeded by a '%' character. These represent unnamed variable slots.
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]+
191 {Comment} { /* Ignore comments for now */ }
193 begin { return BEGINTOK; }
194 end { return ENDTOK; }
195 true { return TRUETOK; }
196 false { return FALSETOK; }
197 declare { return DECLARE; }
198 global { return GLOBAL; }
199 constant { return CONSTANT; }
200 internal { return INTERNAL; }
201 linkonce { return LINKONCE; }
202 weak { return WEAK; }
203 appending { return APPENDING; }
204 dllimport { return DLLIMPORT; }
205 dllexport { return DLLEXPORT; }
206 extern_weak { return EXTERN_WEAK; }
207 uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
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 except { RET_TOK(TermOpVal, Unwind, UNWIND); }
216 not { return NOT; } /* Deprecated, turned into XOR */
217 tail { return TAIL; }
218 target { return TARGET; }
219 triple { return TRIPLE; }
220 deplibs { return DEPLIBS; }
221 endian { return ENDIAN; }
222 pointersize { return POINTERSIZE; }
223 datalayout { return DATALAYOUT; }
224 little { return LITTLE; }
226 volatile { return VOLATILE; }
227 align { return ALIGN; }
228 section { return SECTION; }
229 module { return MODULE; }
230 asm { return ASM_TOK; }
231 sideeffect { return SIDEEFFECT; }
233 cc { return CC_TOK; }
234 ccc { return CCC_TOK; }
235 csretcc { return CSRETCC_TOK; }
236 fastcc { return FASTCC_TOK; }
237 coldcc { return COLDCC_TOK; }
238 x86_stdcallcc { return X86_STDCALLCC_TOK; }
239 x86_fastcallcc { return X86_FASTCALLCC_TOK; }
241 void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
242 bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
243 sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
244 ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
245 short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
246 ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
247 int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
248 uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
249 long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
250 ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
251 float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
252 double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
253 label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
254 type { return TYPE; }
255 opaque { return OPAQUE; }
257 add { RET_TOK(BinaryOpVal, Add, ADD); }
258 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
259 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
260 div { RET_TOK_OBSOLETE(BinaryOpVal, UDiv, UDIV); }
261 udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
262 sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
263 fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
264 rem { RET_TOK(BinaryOpVal, Rem, REM); }
265 and { RET_TOK(BinaryOpVal, And, AND); }
266 or { RET_TOK(BinaryOpVal, Or , OR ); }
267 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
268 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
269 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
270 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
271 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
272 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
273 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
275 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
276 call { RET_TOK(OtherOpVal, Call, CALL); }
277 cast { RET_TOK(OtherOpVal, Cast, CAST); }
278 select { RET_TOK(OtherOpVal, Select, SELECT); }
279 shl { RET_TOK(OtherOpVal, Shl, SHL); }
280 shr { RET_TOK(OtherOpVal, Shr, SHR); }
281 vanext { return VANEXT_old; }
282 vaarg { return VAARG_old; }
283 va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
284 ret { RET_TOK(TermOpVal, Ret, RET); }
285 br { RET_TOK(TermOpVal, Br, BR); }
286 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
287 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
288 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
289 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
291 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
292 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
293 free { RET_TOK(MemOpVal, Free, FREE); }
294 load { RET_TOK(MemOpVal, Load, LOAD); }
295 store { RET_TOK(MemOpVal, Store, STORE); }
296 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
298 extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
299 insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
300 shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
304 UnEscapeLexed(yytext+1);
305 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
309 yytext[strlen(yytext)-1] = 0; // nuke colon
310 UnEscapeLexed(yytext);
311 llvmAsmlval.StrVal = strdup(yytext);
315 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
316 UnEscapeLexed(yytext+1);
317 llvmAsmlval.StrVal = strdup(yytext+1);
321 {StringConstant} { // Note that we cannot unescape a string constant here! The
322 // string constant might contain a \00 which would not be
323 // understood by the string stuff. It is valid to make a
324 // [sbyte] c"Hello World\00" constant, for example.
326 yytext[strlen(yytext)-1] = 0; // nuke end quote
327 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
328 return STRINGCONSTANT;
332 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
334 uint64_t Val = atoull(yytext+1);
335 // +1: we have bigger negative range
336 if (Val > (uint64_t)INT64_MAX+1)
337 GenerateError("Constant too large for signed 64 bits!");
338 llvmAsmlval.SInt64Val = -Val;
342 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
343 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
347 uint64_t Val = atoull(yytext+1);
348 if ((unsigned)Val != Val)
349 GenerateError("Invalid value number (too large)!");
350 llvmAsmlval.UIntVal = unsigned(Val);
354 uint64_t Val = atoull(yytext+2);
355 // +1: we have bigger negative range
356 if (Val > (uint64_t)INT32_MAX+1)
357 GenerateError("Constant too large for signed 32 bits!");
358 llvmAsmlval.SIntVal = (int)-Val;
362 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
363 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
366 /* Make sure to free the internal buffers for flex when we are
367 * done reading our input!
369 yy_delete_buffer(YY_CURRENT_BUFFER);
373 [ \r\t\n] { /* Ignore whitespace */ }
374 . { return yytext[0]; }