Exterminate nasty Cisms
[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(Value) 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 /* FPConstant - A Floating point constant.
84    TODO: Expand lexer to support 10e50 FP constant notation */
85 FPConstant [0-9]+[.][0-9]*
86
87 %%
88
89 {Comment}       { /* Ignore comments for now */ }
90
91 begin           { return BEGINTOK; }
92 end             { return END; }
93 true            { return TRUE;  }
94 false           { return FALSE; }
95 declare         { return DECLARE; }
96 implementation  { return IMPLEMENTATION; }
97
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; }
110
111 type            { llvmAsmlval.TypeVal = Type::TypeTy  ; return TYPE;   }
112
113 label           { llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL;  }
114
115
116 not             { RET_TOK(UnaryOpVal, Not, NOT); }
117
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); }
129
130 to              { return TO; }
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); }
136
137 ret             { RET_TOK(TermOpVal, Ret, RET); }
138 br              { RET_TOK(TermOpVal, Br, BR); }
139 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
140
141
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); }
148
149
150 {VarID}         { llvmAsmlval.StrVal = strdup(yytext+1); return VAR_ID; }
151 {Label}         { 
152                   yytext[strlen(yytext)-1] = 0;  // nuke colon
153                   llvmAsmlval.StrVal = strdup(yytext); 
154                   return LABELSTR; 
155                 }
156
157 {StringConstant} { 
158                   yytext[strlen(yytext)-1] = 0;           // nuke end quote
159                   llvmAsmlval.StrVal = strdup(yytext+1);  // Nuke start quote 
160                   return STRINGCONSTANT;
161                 }
162
163
164 {PInteger}      { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
165 {NInteger}      { 
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; 
171                   return ESINT64VAL; 
172                 }
173
174
175 {EPInteger}     { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
176 {ENInteger}     {
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;
182                   return SINTVAL;
183                 }
184
185 {FPConstant}    { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
186
187 [ \t\n]         { /* Ignore whitespace */ }
188 .               { /*printf("'%s'", yytext);*/ return yytext[0]; }
189
190 %%