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 #define RET_TOK(type, Enum, sym) \
36 llvmAsmlval.type = Instruction::Enum; return sym
40 // TODO: All of the static identifiers are figured out by the lexer,
41 // these should be hashed to reduce the lexer size
44 // atoull - Convert an ascii string of decimal digits into the unsigned long
45 // long representation... this does not have to do input error checking,
46 // because we know that the input will be matched by a suitable regex...
48 static uint64_t atoull(const char *Buffer) {
50 for (; *Buffer; Buffer++) {
51 uint64_t OldRes = Result;
53 Result += *Buffer-'0';
54 if (Result < OldRes) // Uh, oh, overflow detected!!!
55 ThrowException("constant bigger than 64 bits detected!");
60 static uint64_t HexIntToVal(const char *Buffer) {
62 for (; *Buffer; ++Buffer) {
63 uint64_t OldRes = Result;
66 if (C >= '0' && C <= '9')
68 else if (C >= 'A' && C <= 'F')
70 else if (C >= 'a' && C <= 'f')
73 if (Result < OldRes) // Uh, oh, overflow detected!!!
74 ThrowException("constant bigger than 64 bits detected!");
80 // HexToFP - Convert the ascii string in hexidecimal format to the floating
81 // point representation of it.
83 static double HexToFP(const char *Buffer) {
84 // Behave nicely in the face of C TBAA rules... see:
85 // http://www.nullstone.com/htmls/category/aliastyp.htm
90 UIntToFP.UI = HexIntToVal(Buffer);
92 assert(sizeof(double) == sizeof(uint64_t) &&
93 "Data sizes incompatible on this target!");
94 return UIntToFP.FP; // Cast Hex constant to double
98 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
99 // appropriate character. If AllowNull is set to false, a \00 value will cause
100 // an exception to be thrown.
102 // If AllowNull is set to true, the return value of the function points to the
103 // last character of the string in memory.
105 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
107 for (char *BIn = Buffer; *BIn; ) {
108 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
109 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
110 *BOut = strtol(BIn+1, 0, 16); // Convert to number
111 if (!AllowNull && !*BOut)
112 ThrowException("String literal cannot accept \\00 escape!");
114 BIn[3] = Tmp; // Restore character
115 BIn += 3; // Skip over handled chars
125 } // End llvm namespace
127 using namespace llvm;
129 #define YY_NEVER_INTERACTIVE 1
134 /* Comments start with a ; and go till end of line */
137 /* Variable(Value) identifiers start with a % sign */
138 VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
140 /* Label identifiers end with a colon */
141 Label [-a-zA-Z$._0-9]+:
143 /* Quoted names can contain any character except " and \ */
144 StringConstant \"[^\"]*\"
147 /* [PN]Integer: match positive and negative literal integer values that
148 * are preceeded by a '%' character. These represent unnamed variable slots.
154 /* E[PN]Integer: match positive and negative literal integer values */
158 /* FPConstant - A Floating point constant.
160 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
162 /* HexFPConstant - Floating point constant represented in IEEE format as a
163 * hexadecimal number for when exponential notation is not precise enough.
165 HexFPConstant 0x[0-9A-Fa-f]+
167 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
168 * it to deal with 64 bit numbers.
170 HexIntConstant [us]0x[0-9A-Fa-f]+
173 {Comment} { /* Ignore comments for now */ }
175 begin { return BEGINTOK; }
176 end { return ENDTOK; }
177 true { return TRUETOK; }
178 false { return FALSETOK; }
179 declare { return DECLARE; }
180 global { return GLOBAL; }
181 constant { return CONSTANT; }
182 internal { return INTERNAL; }
183 linkonce { return LINKONCE; }
184 weak { return WEAK; }
185 appending { return APPENDING; }
186 uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
187 external { return EXTERNAL; }
188 implementation { return IMPLEMENTATION; }
189 zeroinitializer { return ZEROINITIALIZER; }
190 \.\.\. { return DOTDOTDOT; }
191 undef { return UNDEF; }
192 null { return NULL_TOK; }
194 except { RET_TOK(TermOpVal, Unwind, UNWIND); }
195 not { return NOT; } /* Deprecated, turned into XOR */
196 target { return TARGET; }
197 triple { return TRIPLE; }
198 deplibs { return DEPLIBS; }
199 endian { return ENDIAN; }
200 pointersize { return POINTERSIZE; }
201 little { return LITTLE; }
203 volatile { return VOLATILE; }
205 void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
206 bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
207 sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
208 ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
209 short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
210 ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
211 int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
212 uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
213 long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
214 ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
215 float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
216 double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
217 label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
218 type { return TYPE; }
219 opaque { return OPAQUE; }
221 add { RET_TOK(BinaryOpVal, Add, ADD); }
222 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
223 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
224 div { RET_TOK(BinaryOpVal, Div, DIV); }
225 rem { RET_TOK(BinaryOpVal, Rem, REM); }
226 and { RET_TOK(BinaryOpVal, And, AND); }
227 or { RET_TOK(BinaryOpVal, Or , OR ); }
228 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
229 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
230 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
231 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
232 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
233 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
234 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
236 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
237 call { RET_TOK(OtherOpVal, Call, CALL); }
238 cast { RET_TOK(OtherOpVal, Cast, CAST); }
239 select { RET_TOK(OtherOpVal, Select, SELECT); }
240 shl { RET_TOK(OtherOpVal, Shl, SHL); }
241 shr { RET_TOK(OtherOpVal, Shr, SHR); }
242 va_arg { return VA_ARG; /* FIXME: OBSOLETE */}
243 vanext { RET_TOK(OtherOpVal, VANext, VANEXT); }
244 vaarg { RET_TOK(OtherOpVal, VAArg , VAARG); }
246 ret { RET_TOK(TermOpVal, Ret, RET); }
247 br { RET_TOK(TermOpVal, Br, BR); }
248 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
249 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
250 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
251 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
253 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
254 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
255 free { RET_TOK(MemOpVal, Free, FREE); }
256 load { RET_TOK(MemOpVal, Load, LOAD); }
257 store { RET_TOK(MemOpVal, Store, STORE); }
258 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
262 UnEscapeLexed(yytext+1);
263 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
267 yytext[strlen(yytext)-1] = 0; // nuke colon
268 UnEscapeLexed(yytext);
269 llvmAsmlval.StrVal = strdup(yytext);
273 {StringConstant} { // Note that we cannot unescape a string constant here! The
274 // string constant might contain a \00 which would not be
275 // understood by the string stuff. It is valid to make a
276 // [sbyte] c"Hello World\00" constant, for example.
278 yytext[strlen(yytext)-1] = 0; // nuke end quote
279 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
280 return STRINGCONSTANT;
284 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
286 uint64_t Val = atoull(yytext+1);
287 // +1: we have bigger negative range
288 if (Val > (uint64_t)INT64_MAX+1)
289 ThrowException("Constant too large for signed 64 bits!");
290 llvmAsmlval.SInt64Val = -Val;
294 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
295 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
298 {EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
300 uint64_t Val = atoull(yytext+2);
301 // +1: we have bigger negative range
302 if (Val > (uint64_t)INT32_MAX+1)
303 ThrowException("Constant too large for signed 32 bits!");
304 llvmAsmlval.SIntVal = -Val;
308 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
309 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
312 /* Make sure to free the internal buffers for flex when we are
313 * done reading our input!
315 yy_delete_buffer(YY_CURRENT_BUFFER);
319 [ \r\t\n] { /* Ignore whitespace */ }
320 . { return yytext[0]; }