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(Def) 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 */
85 {Comment} { /* Ignore comments for now */ }
87 begin { return BEGINTOK; }
90 false { return FALSE; }
91 declare { return DECLARE; }
92 implementation { return IMPLEMENTATION; }
94 - { cerr << "deprecated argument '-' used!\n"; return '-'; }
95 bb { cerr << "deprecated type 'bb' used!\n"; llvmAsmlval.TypeVal = Type::LabelTy; return LABEL;}
97 void { llvmAsmlval.TypeVal = Type::VoidTy ; return VOID; }
98 bool { llvmAsmlval.TypeVal = Type::BoolTy ; return BOOL; }
99 sbyte { llvmAsmlval.TypeVal = Type::SByteTy ; return SBYTE; }
100 ubyte { llvmAsmlval.TypeVal = Type::UByteTy ; return UBYTE; }
101 short { llvmAsmlval.TypeVal = Type::ShortTy ; return SHORT; }
102 ushort { llvmAsmlval.TypeVal = Type::UShortTy; return USHORT; }
103 int { llvmAsmlval.TypeVal = Type::IntTy ; return INT; }
104 uint { llvmAsmlval.TypeVal = Type::UIntTy ; return UINT; }
105 long { llvmAsmlval.TypeVal = Type::LongTy ; return LONG; }
106 ulong { llvmAsmlval.TypeVal = Type::ULongTy ; return ULONG; }
107 float { llvmAsmlval.TypeVal = Type::FloatTy ; return FLOAT; }
108 double { llvmAsmlval.TypeVal = Type::DoubleTy; return DOUBLE; }
110 type { llvmAsmlval.TypeVal = Type::TypeTy ; return TYPE; }
112 label { llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL; }
114 not { RET_TOK(UnaryOpVal, Not, NOT); }
115 cast { RET_TOK(UnaryOpVal, Cast, CAST); }
119 call { return CALL; }
120 add { RET_TOK(BinaryOpVal, Add, ADD); }
121 sub { RET_TOK(BinaryOpVal, Sub, SUB); }
122 mul { RET_TOK(BinaryOpVal, Mul, MUL); }
123 div { RET_TOK(BinaryOpVal, Div, DIV); }
124 rem { RET_TOK(BinaryOpVal, Rem, REM); }
125 setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
126 seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
127 setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
128 setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
129 setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
130 setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
132 ret { RET_TOK(TermOpVal, Ret, RET); }
133 br { RET_TOK(TermOpVal, Br, BR); }
134 switch { RET_TOK(TermOpVal, Switch, SWITCH); }
137 malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
138 alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
139 free { RET_TOK(MemOpVal, Free, FREE); }
140 load { RET_TOK(MemOpVal, Load, LOAD); }
141 store { RET_TOK(MemOpVal, Store, STORE); }
142 getfield { RET_TOK(MemOpVal, GetField, GETFIELD); }
143 putfield { RET_TOK(MemOpVal, PutField, PUTFIELD); }
146 {VarID} { llvmAsmlval.StrVal = strdup(yytext+1); return VAR_ID; }
148 yytext[strlen(yytext)-1] = 0; // nuke colon
149 llvmAsmlval.StrVal = strdup(yytext);
154 yytext[strlen(yytext)-1] = 0; // nuke end quote
155 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
156 return STRINGCONSTANT;
160 {PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
162 uint64_t Val = atoull(yytext+1);
163 // +1: we have bigger negative range
164 if (Val > (uint64_t)INT64_MAX+1)
165 ThrowException("Constant too large for signed 64 bits!");
166 llvmAsmlval.SInt64Val = -Val;
171 {EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
173 uint64_t Val = atoull(yytext+2);
174 // +1: we have bigger negative range
175 if (Val > (uint64_t)INT32_MAX+1)
176 ThrowException("Constant too large for signed 32 bits!");
177 llvmAsmlval.SIntVal = -Val;
182 [ \t\n] { /* Ignore whitespace */ }
183 . { /*printf("'%s'", yytext);*/ return yytext[0]; }