blob: e966de155fbfc17c541f4065ab141681fed50241 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001/*===-- 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//
40uint64_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 */
60Comment ;.*
61
62/* Variable(Def) identifiers start with a % sign */
63VarID %[a-zA-Z$._][a-zA-Z$._0-9]*
64
65/* Label identifiers end with a colon */
66Label [a-zA-Z$._0-9]+:
67
68/* Quoted names can contain any character except " and \ */
69StringConstant \"[^\"]+\"
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 */
75EPInteger %[0-9]+
76ENInteger %-[0-9]+
77
78
79/* E[PN]Integer: match positive and negative literal integer values */
80PInteger [0-9]+
81NInteger -[0-9]+
82
Chris Lattner212f70d2001-07-15 00:17:01 +000083/* FPConstant - A Floating point constant.
84 TODO: Expand lexer to support 10e50 FP constant notation */
85FPConstant [0-9]+[.][0-9]*
86
Chris Lattner2f7c9632001-06-06 20:29:01 +000087%%
88
89{Comment} { /* Ignore comments for now */ }
90
91begin { return BEGINTOK; }
92end { return END; }
93true { return TRUE; }
94false { return FALSE; }
95declare { return DECLARE; }
96implementation { return IMPLEMENTATION; }
97
98- { cerr << "deprecated argument '-' used!\n"; return '-'; }
99bb { cerr << "deprecated type 'bb' used!\n"; llvmAsmlval.TypeVal = Type::LabelTy; return LABEL;}
100
101void { llvmAsmlval.TypeVal = Type::VoidTy ; return VOID; }
102bool { llvmAsmlval.TypeVal = Type::BoolTy ; return BOOL; }
103sbyte { llvmAsmlval.TypeVal = Type::SByteTy ; return SBYTE; }
104ubyte { llvmAsmlval.TypeVal = Type::UByteTy ; return UBYTE; }
105short { llvmAsmlval.TypeVal = Type::ShortTy ; return SHORT; }
106ushort { llvmAsmlval.TypeVal = Type::UShortTy; return USHORT; }
107int { llvmAsmlval.TypeVal = Type::IntTy ; return INT; }
108uint { llvmAsmlval.TypeVal = Type::UIntTy ; return UINT; }
109long { llvmAsmlval.TypeVal = Type::LongTy ; return LONG; }
110ulong { llvmAsmlval.TypeVal = Type::ULongTy ; return ULONG; }
111float { llvmAsmlval.TypeVal = Type::FloatTy ; return FLOAT; }
112double { llvmAsmlval.TypeVal = Type::DoubleTy; return DOUBLE; }
113
114type { llvmAsmlval.TypeVal = Type::TypeTy ; return TYPE; }
115
116label { llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL; }
117
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000118
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119not { RET_TOK(UnaryOpVal, Not, NOT); }
120
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121add { RET_TOK(BinaryOpVal, Add, ADD); }
122sub { RET_TOK(BinaryOpVal, Sub, SUB); }
123mul { RET_TOK(BinaryOpVal, Mul, MUL); }
124div { RET_TOK(BinaryOpVal, Div, DIV); }
125rem { RET_TOK(BinaryOpVal, Rem, REM); }
126setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
127seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
128setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
129setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
130setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
131setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
132
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000133to { return TO; }
134phi { RET_TOK(OtherOpVal, PHINode, PHI); }
135call { RET_TOK(OtherOpVal, Call, CALL); }
136cast { RET_TOK(OtherOpVal, Cast, CAST); }
137shl { RET_TOK(OtherOpVal, Shl, SHL); }
138shr { RET_TOK(OtherOpVal, Shr, SHR); }
139
Chris Lattner2f7c9632001-06-06 20:29:01 +0000140ret { RET_TOK(TermOpVal, Ret, RET); }
141br { RET_TOK(TermOpVal, Br, BR); }
142switch { RET_TOK(TermOpVal, Switch, SWITCH); }
143
144
145malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
146alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
147free { RET_TOK(MemOpVal, Free, FREE); }
148load { RET_TOK(MemOpVal, Load, LOAD); }
149store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000150getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151
152
153{VarID} { llvmAsmlval.StrVal = strdup(yytext+1); return VAR_ID; }
154{Label} {
155 yytext[strlen(yytext)-1] = 0; // nuke colon
156 llvmAsmlval.StrVal = strdup(yytext);
157 return LABELSTR;
158 }
159
160{StringConstant} {
161 yytext[strlen(yytext)-1] = 0; // nuke end quote
162 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
163 return STRINGCONSTANT;
164 }
165
166
167{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
168{NInteger} {
169 uint64_t Val = atoull(yytext+1);
170 // +1: we have bigger negative range
171 if (Val > (uint64_t)INT64_MAX+1)
172 ThrowException("Constant too large for signed 64 bits!");
173 llvmAsmlval.SInt64Val = -Val;
174 return ESINT64VAL;
175 }
176
177
178{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
179{ENInteger} {
180 uint64_t Val = atoull(yytext+2);
181 // +1: we have bigger negative range
182 if (Val > (uint64_t)INT32_MAX+1)
183 ThrowException("Constant too large for signed 32 bits!");
184 llvmAsmlval.SIntVal = -Val;
185 return SINTVAL;
186 }
187
Chris Lattner212f70d2001-07-15 00:17:01 +0000188{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000189
190[ \t\n] { /* Ignore whitespace */ }
191. { /*printf("'%s'", yytext);*/ return yytext[0]; }
192
193%%