blob: 89d776bbb0cdbe4973c3bd1a26b70215d6cd02be [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
114neg { RET_TOK(UnaryOpVal, Neg, NEG); }
115not { RET_TOK(UnaryOpVal, Not, NOT); }
116
117phi { return PHI; }
118call { return CALL; }
119add { RET_TOK(BinaryOpVal, Add, ADD); }
120sub { RET_TOK(BinaryOpVal, Sub, SUB); }
121mul { RET_TOK(BinaryOpVal, Mul, MUL); }
122div { RET_TOK(BinaryOpVal, Div, DIV); }
123rem { RET_TOK(BinaryOpVal, Rem, REM); }
124setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
125seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
126setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
127setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
128setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
129setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
130
131ret { RET_TOK(TermOpVal, Ret, RET); }
132br { RET_TOK(TermOpVal, Br, BR); }
133switch { RET_TOK(TermOpVal, Switch, SWITCH); }
134
135
136malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
137alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
138free { RET_TOK(MemOpVal, Free, FREE); }
139load { RET_TOK(MemOpVal, Load, LOAD); }
140store { RET_TOK(MemOpVal, Store, STORE); }
141getfield { RET_TOK(MemOpVal, GetField, GETFIELD); }
142putfield { RET_TOK(MemOpVal, PutField, PUTFIELD); }
143
144
145{VarID} { llvmAsmlval.StrVal = strdup(yytext+1); return VAR_ID; }
146{Label} {
147 yytext[strlen(yytext)-1] = 0; // nuke colon
148 llvmAsmlval.StrVal = strdup(yytext);
149 return LABELSTR;
150 }
151
152{StringConstant} {
153 yytext[strlen(yytext)-1] = 0; // nuke end quote
154 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
155 return STRINGCONSTANT;
156 }
157
158
159{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
160{NInteger} {
161 uint64_t Val = atoull(yytext+1);
162 // +1: we have bigger negative range
163 if (Val > (uint64_t)INT64_MAX+1)
164 ThrowException("Constant too large for signed 64 bits!");
165 llvmAsmlval.SInt64Val = -Val;
166 return ESINT64VAL;
167 }
168
169
170{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
171{ENInteger} {
172 uint64_t Val = atoull(yytext+2);
173 // +1: we have bigger negative range
174 if (Val > (uint64_t)INT32_MAX+1)
175 ThrowException("Constant too large for signed 32 bits!");
176 llvmAsmlval.SIntVal = -Val;
177 return SINTVAL;
178 }
179
180
181[ \t\n] { /* Ignore whitespace */ }
182. { /*printf("'%s'", yytext);*/ return yytext[0]; }
183
184%%