blob: 761c6b5e62378b8e14bc1e046feb8fcdcc9c39d5 [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"
Chris Lattner26e50dc2001-07-28 17:48:55 +000027#include <ctype.h>
28#include <stdlib.h>
Chris Lattner2f7c9632001-06-06 20:29:01 +000029
30#define RET_TOK(type, Enum, sym) \
31 llvmAsmlval.type = Instruction::Enum; return sym
32
33
34// TODO: All of the static identifiers are figured out by the lexer,
35// these should be hashed.
36
37
38// atoull - Convert an ascii string of decimal digits into the unsigned long
39// long representation... this does not have to do input error checking,
40// because we know that the input will be matched by a suitable regex...
41//
42uint64_t atoull(const char *Buffer) {
43 uint64_t Result = 0;
44 for (; *Buffer; Buffer++) {
45 uint64_t OldRes = Result;
46 Result *= 10;
47 Result += *Buffer-'0';
48 if (Result < OldRes) { // Uh, oh, overflow detected!!!
49 ThrowException("constant bigger than 64 bits detected!");
50 }
51 }
52 return Result;
53}
54
55
Chris Lattner26e50dc2001-07-28 17:48:55 +000056// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
57// appropriate character. If AllowNull is set to false, a \00 value will cause
58// an exception to be thrown.
59//
60// If AllowNull is set to true, the return value of the function points to the
61// last character of the string in memory.
62//
63char *UnEscapeLexed(char *Buffer, bool AllowNull = false) {
64 char *BOut = Buffer;
65 for (char *BIn = Buffer; *BIn; ) {
66 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
67 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
68 *BOut = strtol(BIn+1, 0, 16); // Convert to number
69 if (!AllowNull && !*BOut)
70 ThrowException("String literal cannot accept \\00 escape!");
71
72 BIn[3] = Tmp; // Restore character
73 BIn += 3; // Skip over handled chars
74 ++BOut;
75 } else {
76 *BOut++ = *BIn++;
77 }
78 }
79
80 return BOut;
81}
82
83
Chris Lattner2f7c9632001-06-06 20:29:01 +000084#define YY_NEVER_INTERACTIVE 1
85%}
86
87
88
89/* Comments start with a ; and go till end of line */
90Comment ;.*
91
Chris Lattner17f729e2001-07-15 06:35:53 +000092/* Variable(Value) identifiers start with a % sign */
Chris Lattner2f7c9632001-06-06 20:29:01 +000093VarID %[a-zA-Z$._][a-zA-Z$._0-9]*
94
95/* Label identifiers end with a colon */
96Label [a-zA-Z$._0-9]+:
97
98/* Quoted names can contain any character except " and \ */
99StringConstant \"[^\"]+\"
100
101
102/* [PN]Integer: match positive and negative literal integer values that
103 * are preceeded by a '%' character. These represent unnamed variable slots.
104 */
105EPInteger %[0-9]+
106ENInteger %-[0-9]+
107
108
109/* E[PN]Integer: match positive and negative literal integer values */
110PInteger [0-9]+
111NInteger -[0-9]+
112
Chris Lattner212f70d2001-07-15 00:17:01 +0000113/* FPConstant - A Floating point constant.
114 TODO: Expand lexer to support 10e50 FP constant notation */
115FPConstant [0-9]+[.][0-9]*
116
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117%%
118
119{Comment} { /* Ignore comments for now */ }
120
121begin { return BEGINTOK; }
122end { return END; }
123true { return TRUE; }
124false { return FALSE; }
125declare { return DECLARE; }
126implementation { return IMPLEMENTATION; }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000127\.\.\. { return DOTDOTDOT; }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000128string { return STRING; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130void { llvmAsmlval.TypeVal = Type::VoidTy ; return VOID; }
131bool { llvmAsmlval.TypeVal = Type::BoolTy ; return BOOL; }
132sbyte { llvmAsmlval.TypeVal = Type::SByteTy ; return SBYTE; }
133ubyte { llvmAsmlval.TypeVal = Type::UByteTy ; return UBYTE; }
134short { llvmAsmlval.TypeVal = Type::ShortTy ; return SHORT; }
135ushort { llvmAsmlval.TypeVal = Type::UShortTy; return USHORT; }
136int { llvmAsmlval.TypeVal = Type::IntTy ; return INT; }
137uint { llvmAsmlval.TypeVal = Type::UIntTy ; return UINT; }
138long { llvmAsmlval.TypeVal = Type::LongTy ; return LONG; }
139ulong { llvmAsmlval.TypeVal = Type::ULongTy ; return ULONG; }
140float { llvmAsmlval.TypeVal = Type::FloatTy ; return FLOAT; }
141double { llvmAsmlval.TypeVal = Type::DoubleTy; return DOUBLE; }
142
143type { llvmAsmlval.TypeVal = Type::TypeTy ; return TYPE; }
144
145label { llvmAsmlval.TypeVal = Type::LabelTy ; return LABEL; }
146
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000147
Chris Lattner2f7c9632001-06-06 20:29:01 +0000148not { RET_TOK(UnaryOpVal, Not, NOT); }
149
Chris Lattner2f7c9632001-06-06 20:29:01 +0000150add { RET_TOK(BinaryOpVal, Add, ADD); }
151sub { RET_TOK(BinaryOpVal, Sub, SUB); }
152mul { RET_TOK(BinaryOpVal, Mul, MUL); }
153div { RET_TOK(BinaryOpVal, Div, DIV); }
154rem { RET_TOK(BinaryOpVal, Rem, REM); }
155setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
156seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
157setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
158setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
159setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
160setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
161
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000162to { return TO; }
163phi { RET_TOK(OtherOpVal, PHINode, PHI); }
164call { RET_TOK(OtherOpVal, Call, CALL); }
165cast { RET_TOK(OtherOpVal, Cast, CAST); }
166shl { RET_TOK(OtherOpVal, Shl, SHL); }
167shr { RET_TOK(OtherOpVal, Shr, SHR); }
168
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169ret { RET_TOK(TermOpVal, Ret, RET); }
170br { RET_TOK(TermOpVal, Br, BR); }
171switch { RET_TOK(TermOpVal, Switch, SWITCH); }
172
173
174malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
175alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
176free { RET_TOK(MemOpVal, Free, FREE); }
177load { RET_TOK(MemOpVal, Load, LOAD); }
178store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000179getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180
181
Chris Lattner26e50dc2001-07-28 17:48:55 +0000182{VarID} {
183 UnEscapeLexed(yytext+1);
184 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
185 return VAR_ID;
186 }
187{Label} {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner26e50dc2001-07-28 17:48:55 +0000189 UnEscapeLexed(yytext);
190 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191 return LABELSTR;
192 }
193
Chris Lattner26e50dc2001-07-28 17:48:55 +0000194{StringConstant} { // Note that we cannot unescape a string constant here! The
195 // string constant might contain a \00 which would not be
196 // understood by the string stuff. It is valid to make a
197 // [sbyte] c"Hello World\00" constant, for example.
198 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000199 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner26e50dc2001-07-28 17:48:55 +0000200 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner2f7c9632001-06-06 20:29:01 +0000201 return STRINGCONSTANT;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000202 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203
204
205{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
206{NInteger} {
207 uint64_t Val = atoull(yytext+1);
208 // +1: we have bigger negative range
209 if (Val > (uint64_t)INT64_MAX+1)
210 ThrowException("Constant too large for signed 64 bits!");
211 llvmAsmlval.SInt64Val = -Val;
212 return ESINT64VAL;
213 }
214
215
216{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
217{ENInteger} {
218 uint64_t Val = atoull(yytext+2);
219 // +1: we have bigger negative range
220 if (Val > (uint64_t)INT32_MAX+1)
221 ThrowException("Constant too large for signed 32 bits!");
222 llvmAsmlval.SIntVal = -Val;
223 return SINTVAL;
224 }
225
Chris Lattner212f70d2001-07-15 00:17:01 +0000226{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227
228[ \t\n] { /* Ignore whitespace */ }
229. { /*printf("'%s'", yytext);*/ return yytext[0]; }
230
231%%