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"
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.
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 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!");
54 #define YY_NEVER_INTERACTIVE 1
59 /* Comments start with a ; and go till end of line */
62 /* Variable(Value) identifiers start with a % sign */
63 VarID %[a-zA-Z$._][a-zA-Z$._0-9]*
65 /* Label identifiers end with a colon */
66 Label [a-zA-Z$._0-9]+:
68 /* Quoted names can contain any character except " and \ */
69 StringConstant \"[^\"]+\"
72 /* [PN]Integer: match positive and negative literal integer values that
73 * are preceeded by a '%' character. These represent unnamed variable slots.
79 /* E[PN]Integer: match positive and negative literal integer values */
83 /* FPConstant - A Floating point constant.
84 TODO: Expand lexer to support 10e50 FP constant notation */
85 FPConstant [0-9]+[.][0-9]*
89 {Comment} { /* Ignore comments for now */ }
91 begin { return BEGINTOK; }
94 false { return FALSE; }
95 declare { return DECLARE; }
96 implementation { return IMPLEMENTATION; }
98 void { llvmAsmlval.TypeVal = Type::VoidTy ; return VOID; }
99 bool { llvmAsmlval.TypeVal = Type::BoolTy ; return BOOL; }
100 sbyte { llvmAsmlval.TypeVal = Type::SByteTy ; return SBYTE; }
101 ubyte { llvmAsmlval.TypeVal = Type::UByteTy ; return UBYTE; }
102 short { llvmAsmlval.TypeVal = Type::ShortTy ; return SHORT; }
103 ushort { llvmAsmlval.TypeVal = Type::UShortTy; return USHORT; }
104 int { llvmAsmlval.TypeVal = Type::IntTy ; return INT; }
105 uint { llvmAsmlval.TypeVal = Type::UIntTy ; return UINT; }
106 long { llvmAsmlval.TypeVal = Type::LongTy ; return LONG; }
107 ulong { llvmAsmlval.TypeVal = Type::ULongTy ; return ULONG; }
108 float { llvmAsmlval.TypeVal = Type::FloatTy ; return FLOAT; }
109 double { llvmAsmlval.TypeVal = Type::DoubleTy; return DOUBLE; }
111 type { llvmAsmlval.TypeVal = Type::TypeTy ; return TYPE; }
113 label { llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL; }
116 not { RET_TOK(UnaryOpVal, Not, NOT); }
118 add { RET_TOK(BinaryOpVal, Add, ADD); }
119 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
120 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
121 div { RET_TOK(BinaryOpVal, Div, DIV); }
122 rem { RET_TOK(BinaryOpVal, Rem, REM); }
123 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
124 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
125 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
126 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
127 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
128 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
131 phi { RET_TOK(OtherOpVal, PHINode, PHI); }
132 call { RET_TOK(OtherOpVal, Call, CALL); }
133 cast { RET_TOK(OtherOpVal, Cast, CAST); }
134 shl { RET_TOK(OtherOpVal, Shl, SHL); }
135 shr { RET_TOK(OtherOpVal, Shr, SHR); }
137 ret { RET_TOK(TermOpVal, Ret, RET); }
138 br { RET_TOK(TermOpVal, Br, BR); }
139 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
142 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
143 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
144 free { RET_TOK(MemOpVal, Free, FREE); }
145 load { RET_TOK(MemOpVal, Load, LOAD); }
146 store { RET_TOK(MemOpVal, Store, STORE); }
147 getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
150 {VarID} { llvmAsmlval.StrVal = strdup(yytext+1); return VAR_ID; }
152 yytext[strlen(yytext)-1] = 0; // nuke colon
153 llvmAsmlval.StrVal = strdup(yytext);
158 yytext[strlen(yytext)-1] = 0; // nuke end quote
159 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
160 return STRINGCONSTANT;
164 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
166 uint64_t Val = atoull(yytext+1);
167 // +1: we have bigger negative range
168 if (Val > (uint64_t)INT64_MAX+1)
169 ThrowException("Constant too large for signed 64 bits!");
170 llvmAsmlval.SInt64Val = -Val;
175 {EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
177 uint64_t Val = atoull(yytext+2);
178 // +1: we have bigger negative range
179 if (Val > (uint64_t)INT32_MAX+1)
180 ThrowException("Constant too large for signed 32 bits!");
181 llvmAsmlval.SIntVal = -Val;
185 {FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
187 [ \t\n] { /* Ignore whitespace */ }
188 . { /*printf("'%s'", yytext);*/ return yytext[0]; }