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 tail { return TAIL; }
198 target { return TARGET; }
199 triple { return TRIPLE; }
200 deplibs { return DEPLIBS; }
201 endian { return ENDIAN; }
202 pointersize { return POINTERSIZE; }
203 little { return LITTLE; }
205 volatile { return VOLATILE; }
207 cc { return CC_TOK; }
208 ccc { return CCC_TOK; }
209 fastcc { return FASTCC_TOK; }
210 coldcc { return COLDCC_TOK; }
212 void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
213 bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
214 sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
215 ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
216 short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
217 ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
218 int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
219 uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
220 long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
221 ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
222 float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
223 double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
224 label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
225 type { return TYPE; }
226 opaque { return OPAQUE; }
228 add { RET_TOK(BinaryOpVal, Add, ADD); }
229 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
230 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
231 div { RET_TOK(BinaryOpVal, Div, DIV); }
232 rem { RET_TOK(BinaryOpVal, Rem, REM); }
233 and { RET_TOK(BinaryOpVal, And, AND); }
234 or { RET_TOK(BinaryOpVal, Or , OR ); }
235 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
236 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
237 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
238 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
239 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
240 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
241 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
243 phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
244 call { RET_TOK(OtherOpVal, Call, CALL); }
245 cast { RET_TOK(OtherOpVal, Cast, CAST); }
246 select { RET_TOK(OtherOpVal, Select, SELECT); }
247 shl { RET_TOK(OtherOpVal, Shl, SHL); }
248 shr { RET_TOK(OtherOpVal, Shr, SHR); }
249 vanext { RET_TOK(OtherOpVal, VANext, VANEXT); }
250 vaarg { RET_TOK(OtherOpVal, VAArg , VAARG); }
252 ret { RET_TOK(TermOpVal, Ret, RET); }
253 br { RET_TOK(TermOpVal, Br, BR); }
254 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
255 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
256 unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
257 unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
259 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
260 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
261 free { RET_TOK(MemOpVal, Free, FREE); }
262 load { RET_TOK(MemOpVal, Load, LOAD); }
263 store { RET_TOK(MemOpVal, Store, STORE); }
264 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
268 UnEscapeLexed(yytext+1);
269 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
273 yytext[strlen(yytext)-1] = 0; // nuke colon
274 UnEscapeLexed(yytext);
275 llvmAsmlval.StrVal = strdup(yytext);
279 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
280 UnEscapeLexed(yytext+1);
281 llvmAsmlval.StrVal = strdup(yytext+1);
285 {StringConstant} { // Note that we cannot unescape a string constant here! The
286 // string constant might contain a \00 which would not be
287 // understood by the string stuff. It is valid to make a
288 // [sbyte] c"Hello World\00" constant, for example.
290 yytext[strlen(yytext)-1] = 0; // nuke end quote
291 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
292 return STRINGCONSTANT;
296 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
298 uint64_t Val = atoull(yytext+1);
299 // +1: we have bigger negative range
300 if (Val > (uint64_t)INT64_MAX+1)
301 ThrowException("Constant too large for signed 64 bits!");
302 llvmAsmlval.SInt64Val = -Val;
306 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
307 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
311 uint64_t Val = atoull(yytext+1);
312 if ((unsigned)Val != Val)
313 ThrowException("Invalid value number (too large)!");
314 llvmAsmlval.UIntVal = unsigned(Val);
318 uint64_t Val = atoull(yytext+2);
319 // +1: we have bigger negative range
320 if (Val > (uint64_t)INT32_MAX+1)
321 ThrowException("Constant too large for signed 32 bits!");
322 llvmAsmlval.SIntVal = (int)-Val;
326 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
327 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
330 /* Make sure to free the internal buffers for flex when we are
331 * done reading our input!
333 yy_delete_buffer(YY_CURRENT_BUFFER);
337 [ \r\t\n] { /* Ignore whitespace */ }
338 . { return yytext[0]; }