blob: c0bd75cf797174012dedfa0395d4755609883431 [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
83%%
84
85{Comment} { /* Ignore comments for now */ }
86
87begin { return BEGINTOK; }
88end { return END; }
89true { return TRUE; }
90false { return FALSE; }
91declare { return DECLARE; }
92implementation { return IMPLEMENTATION; }
93
94- { cerr << "deprecated argument '-' used!\n"; return '-'; }
95bb { cerr << "deprecated type 'bb' used!\n"; llvmAsmlval.TypeVal = Type::LabelTy; return LABEL;}
96
97void { llvmAsmlval.TypeVal = Type::VoidTy ; return VOID; }
98bool { llvmAsmlval.TypeVal = Type::BoolTy ; return BOOL; }
99sbyte { llvmAsmlval.TypeVal = Type::SByteTy ; return SBYTE; }
100ubyte { llvmAsmlval.TypeVal = Type::UByteTy ; return UBYTE; }
101short { llvmAsmlval.TypeVal = Type::ShortTy ; return SHORT; }
102ushort { llvmAsmlval.TypeVal = Type::UShortTy; return USHORT; }
103int { llvmAsmlval.TypeVal = Type::IntTy ; return INT; }
104uint { llvmAsmlval.TypeVal = Type::UIntTy ; return UINT; }
105long { llvmAsmlval.TypeVal = Type::LongTy ; return LONG; }
106ulong { llvmAsmlval.TypeVal = Type::ULongTy ; return ULONG; }
107float { llvmAsmlval.TypeVal = Type::FloatTy ; return FLOAT; }
108double { llvmAsmlval.TypeVal = Type::DoubleTy; return DOUBLE; }
109
110type { llvmAsmlval.TypeVal = Type::TypeTy ; return TYPE; }
111
112label { llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL; }
113
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000114
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115not { RET_TOK(UnaryOpVal, Not, NOT); }
116
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117add { RET_TOK(BinaryOpVal, Add, ADD); }
118sub { RET_TOK(BinaryOpVal, Sub, SUB); }
119mul { RET_TOK(BinaryOpVal, Mul, MUL); }
120div { RET_TOK(BinaryOpVal, Div, DIV); }
121rem { RET_TOK(BinaryOpVal, Rem, REM); }
122setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
123seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
124setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
125setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
126setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
127setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
128
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000129to { return TO; }
130phi { RET_TOK(OtherOpVal, PHINode, PHI); }
131call { RET_TOK(OtherOpVal, Call, CALL); }
132cast { RET_TOK(OtherOpVal, Cast, CAST); }
133shl { RET_TOK(OtherOpVal, Shl, SHL); }
134shr { RET_TOK(OtherOpVal, Shr, SHR); }
135
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136ret { RET_TOK(TermOpVal, Ret, RET); }
137br { RET_TOK(TermOpVal, Br, BR); }
138switch { RET_TOK(TermOpVal, Switch, SWITCH); }
139
140
141malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
142alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
143free { RET_TOK(MemOpVal, Free, FREE); }
144load { RET_TOK(MemOpVal, Load, LOAD); }
145store { RET_TOK(MemOpVal, Store, STORE); }
146getfield { RET_TOK(MemOpVal, GetField, GETFIELD); }
147putfield { RET_TOK(MemOpVal, PutField, PUTFIELD); }
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
186[ \t\n] { /* Ignore whitespace */ }
187. { /*printf("'%s'", yytext);*/ return yytext[0]; }
188
189%%