1 /*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
3 // This file implements the flex scanner for LLVM assembly languages files.
5 //===----------------------------------------------------------------------===*/
7 %option prefix="llvmAsm"
10 %option never-interactive
15 %option outfile="Lexer.cpp"
21 #include "ParserInternals.h"
22 #include "llvm/Module.h"
24 #include "llvmAsmParser.h"
26 #include "Config/stdlib.h"
28 #define RET_TOK(type, Enum, sym) \
29 llvmAsmlval.type = Instruction::Enum; return sym
32 // TODO: All of the static identifiers are figured out by the lexer,
33 // these should be hashed to reduce the lexer size
36 // atoull - Convert an ascii string of decimal digits into the unsigned long
37 // long representation... this does not have to do input error checking,
38 // because we know that the input will be matched by a suitable regex...
40 static uint64_t atoull(const char *Buffer) {
42 for (; *Buffer; Buffer++) {
43 uint64_t OldRes = Result;
45 Result += *Buffer-'0';
46 if (Result < OldRes) // Uh, oh, overflow detected!!!
47 ThrowException("constant bigger than 64 bits detected!");
52 static uint64_t HexIntToVal(const char *Buffer) {
54 for (; *Buffer; ++Buffer) {
55 uint64_t OldRes = Result;
58 if (C >= '0' && C <= '9')
60 else if (C >= 'A' && C <= 'F')
62 else if (C >= 'a' && C <= 'f')
65 if (Result < OldRes) // Uh, oh, overflow detected!!!
66 ThrowException("constant bigger than 64 bits detected!");
72 // HexToFP - Convert the ascii string in hexidecimal format to the floating
73 // point representation of it.
75 static double HexToFP(const char *Buffer) {
76 // Behave nicely in the face of C TBAA rules... see:
77 // http://www.nullstone.com/htmls/category/aliastyp.htm
82 UIntToFP.UI = HexIntToVal(Buffer);
84 assert(sizeof(double) == sizeof(uint64_t) &&
85 "Data sizes incompatible on this target!");
86 return UIntToFP.FP; // Cast Hex constant to double
90 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
91 // appropriate character. If AllowNull is set to false, a \00 value will cause
92 // an exception to be thrown.
94 // If AllowNull is set to true, the return value of the function points to the
95 // last character of the string in memory.
97 char *UnEscapeLexed(char *Buffer, bool AllowNull) {
99 for (char *BIn = Buffer; *BIn; ) {
100 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
101 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
102 *BOut = strtol(BIn+1, 0, 16); // Convert to number
103 if (!AllowNull && !*BOut)
104 ThrowException("String literal cannot accept \\00 escape!");
106 BIn[3] = Tmp; // Restore character
107 BIn += 3; // Skip over handled chars
117 #define YY_NEVER_INTERACTIVE 1
122 /* Comments start with a ; and go till end of line */
125 /* Variable(Value) identifiers start with a % sign */
126 VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
128 /* Label identifiers end with a colon */
129 Label [-a-zA-Z$._0-9]+:
131 /* Quoted names can contain any character except " and \ */
132 StringConstant \"[^\"]+\"
135 /* [PN]Integer: match positive and negative literal integer values that
136 * are preceeded by a '%' character. These represent unnamed variable slots.
142 /* E[PN]Integer: match positive and negative literal integer values */
146 /* FPConstant - A Floating point constant.
148 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
150 /* HexFPConstant - Floating point constant represented in IEEE format as a
151 * hexadecimal number for when exponential notation is not precise enough.
153 HexFPConstant 0x[0-9A-Fa-f]+
155 /* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
156 * it to deal with 64 bit numbers.
158 HexIntConstant [us]0x[0-9A-Fa-f]+
161 {Comment} { /* Ignore comments for now */ }
163 begin { return BEGINTOK; }
164 end { return ENDTOK; }
165 true { return TRUE; }
166 false { return FALSE; }
167 declare { return DECLARE; }
168 global { return GLOBAL; }
169 constant { return CONSTANT; }
170 const { return CONST; }
171 internal { return INTERNAL; }
172 linkonce { return LINKONCE; }
173 appending { return APPENDING; }
174 uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
175 external { return EXTERNAL; }
176 implementation { return IMPLEMENTATION; }
177 zeroinitializer { return ZEROINITIALIZER; }
178 \.\.\. { return DOTDOTDOT; }
179 null { return NULL_TOK; }
181 except { return EXCEPT; }
182 not { return NOT; } /* Deprecated, turned into XOR */
183 target { return TARGET; }
184 endian { return ENDIAN; }
185 pointersize { return POINTERSIZE; }
186 little { return LITTLE; }
189 void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
190 bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
191 sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
192 ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
193 short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
194 ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
195 int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
196 uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
197 long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
198 ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
199 float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
200 double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
201 type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
202 label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
203 opaque { return OPAQUE; }
205 add { RET_TOK(BinaryOpVal, Add, ADD); }
206 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
207 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
208 div { RET_TOK(BinaryOpVal, Div, DIV); }
209 rem { RET_TOK(BinaryOpVal, Rem, REM); }
210 and { RET_TOK(BinaryOpVal, And, AND); }
211 or { RET_TOK(BinaryOpVal, Or , OR ); }
212 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
213 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
214 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
215 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
216 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
217 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
218 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
220 phi { RET_TOK(OtherOpVal, PHINode, PHI); }
221 call { RET_TOK(OtherOpVal, Call, CALL); }
222 cast { RET_TOK(OtherOpVal, Cast, CAST); }
223 shl { RET_TOK(OtherOpVal, Shl, SHL); }
224 shr { RET_TOK(OtherOpVal, Shr, SHR); }
225 va_arg { RET_TOK(OtherOpVal, VarArg, VA_ARG); }
227 ret { RET_TOK(TermOpVal, Ret, RET); }
228 br { RET_TOK(TermOpVal, Br, BR); }
229 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
230 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
233 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
234 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
235 free { RET_TOK(MemOpVal, Free, FREE); }
236 load { RET_TOK(MemOpVal, Load, LOAD); }
237 store { RET_TOK(MemOpVal, Store, STORE); }
238 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
242 UnEscapeLexed(yytext+1);
243 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
247 yytext[strlen(yytext)-1] = 0; // nuke colon
248 UnEscapeLexed(yytext);
249 llvmAsmlval.StrVal = strdup(yytext);
253 {StringConstant} { // Note that we cannot unescape a string constant here! The
254 // string constant might contain a \00 which would not be
255 // understood by the string stuff. It is valid to make a
256 // [sbyte] c"Hello World\00" constant, for example.
258 yytext[strlen(yytext)-1] = 0; // nuke end quote
259 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
260 return STRINGCONSTANT;
264 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
266 uint64_t Val = atoull(yytext+1);
267 // +1: we have bigger negative range
268 if (Val > (uint64_t)INT64_MAX+1)
269 ThrowException("Constant too large for signed 64 bits!");
270 llvmAsmlval.SInt64Val = -Val;
274 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
275 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
278 {EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
280 uint64_t Val = atoull(yytext+2);
281 // +1: we have bigger negative range
282 if (Val > (uint64_t)INT32_MAX+1)
283 ThrowException("Constant too large for signed 32 bits!");
284 llvmAsmlval.SIntVal = -Val;
288 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
289 {HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
291 [ \t\n] { /* Ignore whitespace */ }
292 . { return yytext[0]; }