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/BasicBlock.h"
23 #include "llvm/Method.h"
24 #include "llvm/Module.h"
26 #include "llvmAsmParser.h"
30 #define RET_TOK(type, Enum, sym) \
31 llvmAsmlval.type = Instruction::Enum; return sym
34 // TODO: All of the static identifiers are figured out by the lexer,
35 // these should be hashed to reduce the lexer size
38 // atoull - Convert an ascii string of decimal digits into the unsigned long
39 // long representation... this does not have to do input error checking,
40 // because we know that the input will be matched by a suitable regex...
42 uint64_t atoull(const char *Buffer) {
44 for (; *Buffer; Buffer++) {
45 uint64_t OldRes = Result;
47 Result += *Buffer-'0';
48 if (Result < OldRes) { // Uh, oh, overflow detected!!!
49 ThrowException("constant bigger than 64 bits detected!");
56 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
57 // appropriate character. If AllowNull is set to false, a \00 value will cause
58 // an exception to be thrown.
60 // If AllowNull is set to true, the return value of the function points to the
61 // last character of the string in memory.
63 char *UnEscapeLexed(char *Buffer, bool AllowNull = false) {
65 for (char *BIn = Buffer; *BIn; ) {
66 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
67 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
68 *BOut = strtol(BIn+1, 0, 16); // Convert to number
69 if (!AllowNull && !*BOut)
70 ThrowException("String literal cannot accept \\00 escape!");
72 BIn[3] = Tmp; // Restore character
73 BIn += 3; // Skip over handled chars
83 #define YY_NEVER_INTERACTIVE 1
88 /* Comments start with a ; and go till end of line */
91 /* Variable(Value) identifiers start with a % sign */
92 VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
94 /* Label identifiers end with a colon */
95 Label [-a-zA-Z$._0-9]+:
97 /* Quoted names can contain any character except " and \ */
98 StringConstant \"[^\"]+\"
101 /* [PN]Integer: match positive and negative literal integer values that
102 * are preceeded by a '%' character. These represent unnamed variable slots.
108 /* E[PN]Integer: match positive and negative literal integer values */
112 /* FPConstant - A Floating point constant.
113 TODO: Expand lexer to support 10e50 FP constant notation */
114 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
118 {Comment} { /* Ignore comments for now */ }
120 begin { return BEGINTOK; }
122 true { return TRUE; }
123 false { return FALSE; }
124 declare { return DECLARE; }
125 global { return GLOBAL; }
126 constant { return CONSTANT; }
127 const { return CONST; }
128 internal { return INTERNAL; }
129 uninitialized { return UNINIT; }
130 implementation { return IMPLEMENTATION; }
131 \.\.\. { return DOTDOTDOT; }
132 string { return STRING; }
133 null { return NULL_TOK; }
135 except { return EXCEPT; }
137 void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
138 bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
139 sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
140 ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
141 short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
142 ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
143 int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
144 uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
145 long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
146 ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
147 float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
148 double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
150 type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
152 label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
153 opaque { llvmAsmlval.TypeVal =
154 new PATypeHolder<Type>(OpaqueType::get());
159 not { RET_TOK(UnaryOpVal, Not, NOT); }
161 add { RET_TOK(BinaryOpVal, Add, ADD); }
162 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
163 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
164 div { RET_TOK(BinaryOpVal, Div, DIV); }
165 rem { RET_TOK(BinaryOpVal, Rem, REM); }
166 and { RET_TOK(BinaryOpVal, And, AND); }
167 or { RET_TOK(BinaryOpVal, Or , OR ); }
168 xor { RET_TOK(BinaryOpVal, Xor, XOR); }
169 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
170 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
171 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
172 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
173 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
174 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
176 phi { RET_TOK(OtherOpVal, PHINode, PHI); }
177 call { RET_TOK(OtherOpVal, Call, CALL); }
178 cast { RET_TOK(OtherOpVal, Cast, CAST); }
179 shl { RET_TOK(OtherOpVal, Shl, SHL); }
180 shr { RET_TOK(OtherOpVal, Shr, SHR); }
182 ret { RET_TOK(TermOpVal, Ret, RET); }
183 br { RET_TOK(TermOpVal, Br, BR); }
184 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
185 invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
188 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
189 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
190 free { RET_TOK(MemOpVal, Free, FREE); }
191 load { RET_TOK(MemOpVal, Load, LOAD); }
192 store { RET_TOK(MemOpVal, Store, STORE); }
193 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
197 UnEscapeLexed(yytext+1);
198 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
202 yytext[strlen(yytext)-1] = 0; // nuke colon
203 UnEscapeLexed(yytext);
204 llvmAsmlval.StrVal = strdup(yytext);
208 {StringConstant} { // Note that we cannot unescape a string constant here! The
209 // string constant might contain a \00 which would not be
210 // understood by the string stuff. It is valid to make a
211 // [sbyte] c"Hello World\00" constant, for example.
213 yytext[strlen(yytext)-1] = 0; // nuke end quote
214 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
215 return STRINGCONSTANT;
219 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
221 uint64_t Val = atoull(yytext+1);
222 // +1: we have bigger negative range
223 if (Val > (uint64_t)INT64_MAX+1)
224 ThrowException("Constant too large for signed 64 bits!");
225 llvmAsmlval.SInt64Val = -Val;
230 {EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
232 uint64_t Val = atoull(yytext+2);
233 // +1: we have bigger negative range
234 if (Val > (uint64_t)INT32_MAX+1)
235 ThrowException("Constant too large for signed 32 bits!");
236 llvmAsmlval.SIntVal = -Val;
240 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
242 [ \t\n] { /* Ignore whitespace */ }
243 . { return yytext[0]; }