blob: 50da49adcc6279b1dc9cb83258e5ec25436ba382 [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,
Chris Lattner47af30c2001-09-07 16:32:43 +000035// these should be hashed to reduce the lexer size
Chris Lattner2f7c9632001-06-06 20:29:01 +000036
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
Chris Lattner2f7c9632001-06-06 20:29:01 +000083#define YY_NEVER_INTERACTIVE 1
84%}
85
86
87
88/* Comments start with a ; and go till end of line */
89Comment ;.*
90
Chris Lattner17f729e2001-07-15 06:35:53 +000091/* Variable(Value) identifiers start with a % sign */
Chris Lattner1366b302001-12-04 04:31:30 +000092VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner2f7c9632001-06-06 20:29:01 +000093
94/* Label identifiers end with a colon */
Chris Lattner1366b302001-12-04 04:31:30 +000095Label [-a-zA-Z$._0-9]+:
Chris Lattner2f7c9632001-06-06 20:29:01 +000096
97/* Quoted names can contain any character except " and \ */
98StringConstant \"[^\"]+\"
99
100
101/* [PN]Integer: match positive and negative literal integer values that
102 * are preceeded by a '%' character. These represent unnamed variable slots.
103 */
104EPInteger %[0-9]+
105ENInteger %-[0-9]+
106
107
108/* E[PN]Integer: match positive and negative literal integer values */
109PInteger [0-9]+
110NInteger -[0-9]+
111
Chris Lattner212f70d2001-07-15 00:17:01 +0000112/* FPConstant - A Floating point constant.
113 TODO: Expand lexer to support 10e50 FP constant notation */
Chris Lattner50f68ac2001-11-01 22:06:08 +0000114FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner212f70d2001-07-15 00:17:01 +0000115
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116%%
117
118{Comment} { /* Ignore comments for now */ }
119
120begin { return BEGINTOK; }
121end { return END; }
122true { return TRUE; }
123false { return FALSE; }
124declare { return DECLARE; }
Chris Lattnerda975502001-09-10 07:58:01 +0000125global { return GLOBAL; }
Chris Lattner7fb14f52001-09-18 04:00:54 +0000126constant { return CONSTANT; }
Chris Lattner571a6b02001-10-03 01:49:25 +0000127const { return CONST; }
Chris Lattner841d8b92001-11-26 18:54:16 +0000128internal { return INTERNAL; }
Chris Lattner7fb14f52001-09-18 04:00:54 +0000129uninitialized { return UNINIT; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130implementation { return IMPLEMENTATION; }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000131\.\.\. { return DOTDOTDOT; }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000132string { return STRING; }
Chris Lattnerfbdec252001-09-30 22:46:54 +0000133null { return NULL_TOK; }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000134to { return TO; }
135except { return EXCEPT; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136
Chris Lattner47af30c2001-09-07 16:32:43 +0000137void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
138bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
139sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
140ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
141short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
142ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
143int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
144uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
145long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
146ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
147float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
148double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149
Chris Lattner47af30c2001-09-07 16:32:43 +0000150type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151
Chris Lattner47af30c2001-09-07 16:32:43 +0000152label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
153opaque { llvmAsmlval.TypeVal =
154 new PATypeHolder<Type>(OpaqueType::get());
155 return OPAQUE;
156 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000157
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000158
Chris Lattner2f7c9632001-06-06 20:29:01 +0000159not { RET_TOK(UnaryOpVal, Not, NOT); }
160
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161add { RET_TOK(BinaryOpVal, Add, ADD); }
162sub { RET_TOK(BinaryOpVal, Sub, SUB); }
163mul { RET_TOK(BinaryOpVal, Mul, MUL); }
164div { RET_TOK(BinaryOpVal, Div, DIV); }
165rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000166and { RET_TOK(BinaryOpVal, And, AND); }
167or { RET_TOK(BinaryOpVal, Or , OR ); }
168xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
170seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
171setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
172setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
173setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
174setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
175
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000176phi { RET_TOK(OtherOpVal, PHINode, PHI); }
177call { RET_TOK(OtherOpVal, Call, CALL); }
178cast { RET_TOK(OtherOpVal, Cast, CAST); }
179shl { RET_TOK(OtherOpVal, Shl, SHL); }
180shr { RET_TOK(OtherOpVal, Shr, SHR); }
181
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182ret { RET_TOK(TermOpVal, Ret, RET); }
183br { RET_TOK(TermOpVal, Br, BR); }
184switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000185invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000186
187
188malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
189alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
190free { RET_TOK(MemOpVal, Free, FREE); }
191load { RET_TOK(MemOpVal, Load, LOAD); }
192store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000193getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194
195
Chris Lattner26e50dc2001-07-28 17:48:55 +0000196{VarID} {
197 UnEscapeLexed(yytext+1);
198 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
199 return VAR_ID;
200 }
201{Label} {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner26e50dc2001-07-28 17:48:55 +0000203 UnEscapeLexed(yytext);
204 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205 return LABELSTR;
206 }
207
Chris Lattner26e50dc2001-07-28 17:48:55 +0000208{StringConstant} { // Note that we cannot unescape a string constant here! The
209 // string constant might contain a \00 which would not be
210 // understood by the string stuff. It is valid to make a
211 // [sbyte] c"Hello World\00" constant, for example.
212 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner26e50dc2001-07-28 17:48:55 +0000214 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215 return STRINGCONSTANT;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000216 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000217
218
219{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
220{NInteger} {
221 uint64_t Val = atoull(yytext+1);
222 // +1: we have bigger negative range
223 if (Val > (uint64_t)INT64_MAX+1)
224 ThrowException("Constant too large for signed 64 bits!");
225 llvmAsmlval.SInt64Val = -Val;
226 return ESINT64VAL;
227 }
228
229
230{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
231{ENInteger} {
232 uint64_t Val = atoull(yytext+2);
233 // +1: we have bigger negative range
234 if (Val > (uint64_t)INT32_MAX+1)
235 ThrowException("Constant too large for signed 32 bits!");
236 llvmAsmlval.SIntVal = -Val;
237 return SINTVAL;
238 }
239
Chris Lattner212f70d2001-07-15 00:17:01 +0000240{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000241
242[ \t\n] { /* Ignore whitespace */ }
Chris Lattner47af30c2001-09-07 16:32:43 +0000243. { return yytext[0]; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244
245%%