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 = (char)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]+:
142 QuoteLabel \"[^\"]+\":
144 /* Quoted names can contain any character except " and \ */
145 StringConstant \"[^\"]*\"
148 /* [PN]Integer: match positive and negative literal integer values that
149 * are preceeded by a '%' character. These represent unnamed variable slots.
155 /* E[PN]Integer: match positive and negative literal integer values */
159 /* FPConstant - A Floating point constant.
161 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
163 /* HexFPConstant - Floating point constant represented in IEEE format as a
164 * hexadecimal number for when exponential notation is not precise enough.
166 HexFPConstant 0x[0-9A-Fa-f]+
168 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
169 * it to deal with 64 bit numbers.
171 HexIntConstant [us]0x[0-9A-Fa-f]+
174 {Comment} { /* Ignore comments for now */ }
176 begin { return BEGINTOK; }
177 end { return ENDTOK; }
178 true { return TRUETOK; }
179 false { return FALSETOK; }
180 declare { return DECLARE; }
181 global { return GLOBAL; }
182 constant { return CONSTANT; }
183 internal { return INTERNAL; }
184 linkonce { return LINKONCE; }
185 weak { return WEAK; }
186 appending { return APPENDING; }
187 uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
188 external { return EXTERNAL; }
189 implementation { return IMPLEMENTATION; }
190 zeroinitializer { return ZEROINITIALIZER; }
191 \.\.\. { return DOTDOTDOT; }
192 undef { return UNDEF; }
193 null { return NULL_TOK; }
195 except { RET_TOK(TermOpVal, Unwind, UNWIND); }
196 not { return NOT; } /* Deprecated, turned into XOR */
197 target { return TARGET; }
198 triple { return TRIPLE; }
199 deplibs { return DEPLIBS; }
200 endian { return ENDIAN; }
201 pointersize { return POINTERSIZE; }
202 little { return LITTLE; }
204 volatile { return VOLATILE; }
206 void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
207 bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
208 sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
209 ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
210 short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
211 ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
212 int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
213 uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
214 long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
215 ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
216 float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
217 double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
218 label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
219 type { return TYPE; }
220 opaque { return OPAQUE; }
222 add { RET_TOK(BinaryOpVal, Add, ADD); }
223 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
224 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
225 div { RET_TOK(BinaryOpVal, Div, DIV); }
226 rem { RET_TOK(BinaryOpVal, Rem, REM); }
227 and { RET_TOK(BinaryOpVal, And, AND); }
228 or { RET_TOK(BinaryOpVal, Or , OR ); }
229 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
230 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
231 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
232 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
233 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
234 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
235 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
237 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
238 call { RET_TOK(OtherOpVal, Call, CALL); }
239 cast { RET_TOK(OtherOpVal, Cast, CAST); }
240 select { RET_TOK(OtherOpVal, Select, SELECT); }
241 shl { RET_TOK(OtherOpVal, Shl, SHL); }
242 shr { RET_TOK(OtherOpVal, Shr, SHR); }
243 va_arg { return VA_ARG; /* FIXME: OBSOLETE */}
244 vanext { RET_TOK(OtherOpVal, VANext, VANEXT); }
245 vaarg { RET_TOK(OtherOpVal, VAArg , VAARG); }
247 ret { RET_TOK(TermOpVal, Ret, RET); }
248 br { RET_TOK(TermOpVal, Br, BR); }
249 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
250 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
251 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
252 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
254 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
255 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
256 free { RET_TOK(MemOpVal, Free, FREE); }
257 load { RET_TOK(MemOpVal, Load, LOAD); }
258 store { RET_TOK(MemOpVal, Store, STORE); }
259 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
263 UnEscapeLexed(yytext+1);
264 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
268 yytext[strlen(yytext)-1] = 0; // nuke colon
269 UnEscapeLexed(yytext);
270 llvmAsmlval.StrVal = strdup(yytext);
274 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
275 UnEscapeLexed(yytext+1);
276 llvmAsmlval.StrVal = strdup(yytext+1);
280 {StringConstant} { // Note that we cannot unescape a string constant here! The
281 // string constant might contain a \00 which would not be
282 // understood by the string stuff. It is valid to make a
283 // [sbyte] c"Hello World\00" constant, for example.
285 yytext[strlen(yytext)-1] = 0; // nuke end quote
286 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
287 return STRINGCONSTANT;
291 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
293 uint64_t Val = atoull(yytext+1);
294 // +1: we have bigger negative range
295 if (Val > (uint64_t)INT64_MAX+1)
296 ThrowException("Constant too large for signed 64 bits!");
297 llvmAsmlval.SInt64Val = -Val;
301 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
302 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
306 uint64_t Val = atoull(yytext+1);
307 if ((unsigned)Val != Val)
308 ThrowException("Invalid value number (too large)!");
309 llvmAsmlval.UIntVal = unsigned(Val);
313 uint64_t Val = atoull(yytext+2);
314 // +1: we have bigger negative range
315 if (Val > (uint64_t)INT32_MAX+1)
316 ThrowException("Constant too large for signed 32 bits!");
317 llvmAsmlval.SIntVal = (int)-Val;
321 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
322 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
325 /* Make sure to free the internal buffers for flex when we are
326 * done reading our input!
328 yy_delete_buffer(YY_CURRENT_BUFFER);
332 [ \r\t\n] { /* Ignore whitespace */ }
333 . { return yytext[0]; }