Moved Cast from being a Unary instruction to being an "Other" instruction
[oota-llvm.git] / lib / AsmParser / Lexer.l
1 /*===-- Lexer.l - Scanner for llvm assembly files ----------------*- C++ -*--=//
2 //
3 //  This file implements the flex scanner for LLVM assembly languages files.
4 //
5 //===------------------------------------------------------------------------=*/
6
7 %option prefix="llvmAsm"
8 %option yylineno
9 %option nostdinit
10 %option never-interactive
11 %option batch
12 %option noyywrap
13 %option nodefault
14 %option 8bit
15 %option outfile="Lexer.cpp"
16 %option ecs
17 %option noreject
18 %option noyymore
19
20 %{
21 #include "ParserInternals.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/Method.h"
24 #include "llvm/Module.h"
25 #include <list>
26 #include "llvmAsmParser.h"
27
28 #define RET_TOK(type, Enum, sym) \
29   llvmAsmlval.type = Instruction::Enum; return sym
30
31
32 // TODO: All of the static identifiers are figured out by the lexer, 
33 // these should be hashed.
34
35
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...
39 //
40 uint64_t atoull(const char *Buffer) {
41   uint64_t Result = 0;
42   for (; *Buffer; Buffer++) {
43     uint64_t OldRes = Result;
44     Result *= 10;
45     Result += *Buffer-'0';
46     if (Result < OldRes) {  // Uh, oh, overflow detected!!!
47       ThrowException("constant bigger than 64 bits detected!");
48     }
49   }
50   return Result;
51 }
52
53
54 #define YY_NEVER_INTERACTIVE 1
55 %}
56
57
58
59 /* Comments start with a ; and go till end of line */
60 Comment    ;.*
61
62 /* Variable(Def) identifiers start with a % sign */
63 VarID       %[a-zA-Z$._][a-zA-Z$._0-9]*
64
65 /* Label identifiers end with a colon */
66 Label       [a-zA-Z$._0-9]+:
67
68 /* Quoted names can contain any character except " and \ */
69 StringConstant \"[^\"]+\"
70
71
72 /* [PN]Integer: match positive and negative literal integer values that
73  * are preceeded by a '%' character.  These represent unnamed variable slots.
74  */
75 EPInteger     %[0-9]+
76 ENInteger    %-[0-9]+
77
78
79 /* E[PN]Integer: match positive and negative literal integer values */
80 PInteger   [0-9]+
81 NInteger  -[0-9]+
82
83 %%
84
85 {Comment}       { /* Ignore comments for now */ }
86
87 begin           { return BEGINTOK; }
88 end             { return END; }
89 true            { return TRUE;  }
90 false           { return FALSE; }
91 declare         { return DECLARE; }
92 implementation  { return IMPLEMENTATION; }
93
94 -               { cerr << "deprecated argument '-' used!\n"; return '-'; }
95 bb              { cerr << "deprecated type 'bb' used!\n"; llvmAsmlval.TypeVal = Type::LabelTy; return LABEL;}
96
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; }
109
110 type            { llvmAsmlval.TypeVal = Type::TypeTy  ; return TYPE;   }
111
112 label           { llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL;  }
113
114 not             { RET_TOK(UnaryOpVal, Not, NOT); }
115
116 phi             { return PHI; }
117 call            { return CALL; }
118 cast            { return CAST; }
119 to              { return TO; }
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); }
131
132 ret             { RET_TOK(TermOpVal, Ret, RET); }
133 br              { RET_TOK(TermOpVal, Br, BR); }
134 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
135
136
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); }
144
145
146 {VarID}         { llvmAsmlval.StrVal = strdup(yytext+1); return VAR_ID; }
147 {Label}         { 
148                   yytext[strlen(yytext)-1] = 0;  // nuke colon
149                   llvmAsmlval.StrVal = strdup(yytext); 
150                   return LABELSTR; 
151                 }
152
153 {StringConstant} { 
154                   yytext[strlen(yytext)-1] = 0;           // nuke end quote
155                   llvmAsmlval.StrVal = strdup(yytext+1);  // Nuke start quote 
156                   return STRINGCONSTANT;
157                 }
158
159
160 {PInteger}      { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
161 {NInteger}      { 
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; 
167                   return ESINT64VAL; 
168                 }
169
170
171 {EPInteger}     { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
172 {ENInteger}     {
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;
178                   return SINTVAL;
179                 }
180
181
182 [ \t\n]         { /* Ignore whitespace */ }
183 .               { /*printf("'%s'", yytext);*/ return yytext[0]; }
184
185 %%