blob: 1f4ea1a6de135f1f034c4cfff56e0672794023ef [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
Chris Lattner17f729e2001-07-15 06:35:53 +000062/* Variable(Value) identifiers start with a % sign */
Chris Lattner2f7c9632001-06-06 20:29:01 +000063VarID %[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; }
Chris Lattner42b5a8a2001-07-25 22:47:46 +000097\.\.\. { return DOTDOTDOT; }
Chris Lattner2f7c9632001-06-06 20:29:01 +000098
Chris Lattner2f7c9632001-06-06 20:29:01 +000099void { llvmAsmlval.TypeVal = Type::VoidTy ; return VOID; }
100bool { llvmAsmlval.TypeVal = Type::BoolTy ; return BOOL; }
101sbyte { llvmAsmlval.TypeVal = Type::SByteTy ; return SBYTE; }
102ubyte { llvmAsmlval.TypeVal = Type::UByteTy ; return UBYTE; }
103short { llvmAsmlval.TypeVal = Type::ShortTy ; return SHORT; }
104ushort { llvmAsmlval.TypeVal = Type::UShortTy; return USHORT; }
105int { llvmAsmlval.TypeVal = Type::IntTy ; return INT; }
106uint { llvmAsmlval.TypeVal = Type::UIntTy ; return UINT; }
107long { llvmAsmlval.TypeVal = Type::LongTy ; return LONG; }
108ulong { llvmAsmlval.TypeVal = Type::ULongTy ; return ULONG; }
109float { llvmAsmlval.TypeVal = Type::FloatTy ; return FLOAT; }
110double { llvmAsmlval.TypeVal = Type::DoubleTy; return DOUBLE; }
111
112type { llvmAsmlval.TypeVal = Type::TypeTy ; return TYPE; }
113
114label { llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL; }
115
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000116
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117not { RET_TOK(UnaryOpVal, Not, NOT); }
118
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119add { 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
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000131to { return TO; }
132phi { RET_TOK(OtherOpVal, PHINode, PHI); }
133call { RET_TOK(OtherOpVal, Call, CALL); }
134cast { RET_TOK(OtherOpVal, Cast, CAST); }
135shl { RET_TOK(OtherOpVal, Shl, SHL); }
136shr { RET_TOK(OtherOpVal, Shr, SHR); }
137
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138ret { RET_TOK(TermOpVal, Ret, RET); }
139br { RET_TOK(TermOpVal, Br, BR); }
140switch { RET_TOK(TermOpVal, Switch, SWITCH); }
141
142
143malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
144alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
145free { RET_TOK(MemOpVal, Free, FREE); }
146load { RET_TOK(MemOpVal, Load, LOAD); }
147store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000148getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149
150
151{VarID} { llvmAsmlval.StrVal = strdup(yytext+1); return VAR_ID; }
152{Label} {
153 yytext[strlen(yytext)-1] = 0; // nuke colon
154 llvmAsmlval.StrVal = strdup(yytext);
155 return LABELSTR;
156 }
157
158{StringConstant} {
159 yytext[strlen(yytext)-1] = 0; // nuke end quote
160 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
161 return STRINGCONSTANT;
162 }
163
164
165{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
166{NInteger} {
167 uint64_t Val = atoull(yytext+1);
168 // +1: we have bigger negative range
169 if (Val > (uint64_t)INT64_MAX+1)
170 ThrowException("Constant too large for signed 64 bits!");
171 llvmAsmlval.SInt64Val = -Val;
172 return ESINT64VAL;
173 }
174
175
176{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
177{ENInteger} {
178 uint64_t Val = atoull(yytext+2);
179 // +1: we have bigger negative range
180 if (Val > (uint64_t)INT32_MAX+1)
181 ThrowException("Constant too large for signed 32 bits!");
182 llvmAsmlval.SIntVal = -Val;
183 return SINTVAL;
184 }
185
Chris Lattner212f70d2001-07-15 00:17:01 +0000186{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187
188[ \t\n] { /* Ignore whitespace */ }
189. { /*printf("'%s'", yytext);*/ return yytext[0]; }
190
191%%