blob: a5cb4eff2658dbab93caadd7fc94a36aeb2048f5 [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"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include <list>
23#include "llvmAsmParser.h"
Chris Lattner26e50dc2001-07-28 17:48:55 +000024#include <ctype.h>
25#include <stdlib.h>
Chris Lattner2f7c9632001-06-06 20:29:01 +000026
27#define RET_TOK(type, Enum, sym) \
28 llvmAsmlval.type = Instruction::Enum; return sym
29
30
31// TODO: All of the static identifiers are figured out by the lexer,
Chris Lattner47af30c2001-09-07 16:32:43 +000032// these should be hashed to reduce the lexer size
Chris Lattner2f7c9632001-06-06 20:29:01 +000033
34
35// atoull - Convert an ascii string of decimal digits into the unsigned long
36// long representation... this does not have to do input error checking,
37// because we know that the input will be matched by a suitable regex...
38//
Chris Lattner1b343742002-04-07 08:10:41 +000039static uint64_t atoull(const char *Buffer) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000040 uint64_t Result = 0;
41 for (; *Buffer; Buffer++) {
42 uint64_t OldRes = Result;
43 Result *= 10;
44 Result += *Buffer-'0';
Chris Lattner1b343742002-04-07 08:10:41 +000045 if (Result < OldRes) // Uh, oh, overflow detected!!!
Chris Lattner2f7c9632001-06-06 20:29:01 +000046 ThrowException("constant bigger than 64 bits detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000047 }
48 return Result;
49}
50
Chris Lattner1b343742002-04-07 08:10:41 +000051// HexToFP - Convert the ascii string in hexidecimal format to the floating
52// point representation of it.
53//
54static double HexToFP(const char *Buffer) {
55 uint64_t Result = 0;
56 for (; *Buffer; ++Buffer) {
57 uint64_t OldRes = Result;
58 Result *= 16;
59 char C = *Buffer;
60 if (C >= '0' && C <= '9')
61 Result += C-'0';
62 else if (C >= 'A' && C <= 'F')
63 Result += C-'A'+10;
64 else if (C >= 'a' && C <= 'f')
65 Result += C-'a'+10;
66
67 if (Result < OldRes) // Uh, oh, overflow detected!!!
68 ThrowException("constant bigger than 64 bits detected!");
69 }
70
71 assert(sizeof(double) == sizeof(Result) &&
72 "Data sizes incompatible on this target!");
73 void *ProxyPointer = &Result; // Break TBAA correctly
Chris Lattner1b343742002-04-07 08:10:41 +000074 return *(double*)ProxyPointer; // Cast Hex constant to double
75}
76
Chris Lattner2f7c9632001-06-06 20:29:01 +000077
Chris Lattner26e50dc2001-07-28 17:48:55 +000078// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
79// appropriate character. If AllowNull is set to false, a \00 value will cause
80// an exception to be thrown.
81//
82// If AllowNull is set to true, the return value of the function points to the
83// last character of the string in memory.
84//
85char *UnEscapeLexed(char *Buffer, bool AllowNull = false) {
86 char *BOut = Buffer;
87 for (char *BIn = Buffer; *BIn; ) {
88 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
89 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
90 *BOut = strtol(BIn+1, 0, 16); // Convert to number
91 if (!AllowNull && !*BOut)
92 ThrowException("String literal cannot accept \\00 escape!");
93
94 BIn[3] = Tmp; // Restore character
95 BIn += 3; // Skip over handled chars
96 ++BOut;
97 } else {
98 *BOut++ = *BIn++;
99 }
100 }
101
102 return BOut;
103}
104
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105#define YY_NEVER_INTERACTIVE 1
106%}
107
108
109
110/* Comments start with a ; and go till end of line */
111Comment ;.*
112
Chris Lattner17f729e2001-07-15 06:35:53 +0000113/* Variable(Value) identifiers start with a % sign */
Chris Lattner1366b302001-12-04 04:31:30 +0000114VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115
116/* Label identifiers end with a colon */
Chris Lattner1366b302001-12-04 04:31:30 +0000117Label [-a-zA-Z$._0-9]+:
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118
119/* Quoted names can contain any character except " and \ */
120StringConstant \"[^\"]+\"
121
122
123/* [PN]Integer: match positive and negative literal integer values that
124 * are preceeded by a '%' character. These represent unnamed variable slots.
125 */
126EPInteger %[0-9]+
127ENInteger %-[0-9]+
128
129
130/* E[PN]Integer: match positive and negative literal integer values */
131PInteger [0-9]+
132NInteger -[0-9]+
133
Chris Lattner212f70d2001-07-15 00:17:01 +0000134/* FPConstant - A Floating point constant.
Chris Lattner1b343742002-04-07 08:10:41 +0000135 */
Chris Lattner50f68ac2001-11-01 22:06:08 +0000136FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattner212f70d2001-07-15 00:17:01 +0000137
Chris Lattner1b343742002-04-07 08:10:41 +0000138/* HexFPConstant - Floating point constant represented in IEEE format as a
139 * hexadecimal number for when exponential notation is not precise enough.
140 */
141HexFPConstant 0x[0-9A-Fa-f]+
Chris Lattner2f7c9632001-06-06 20:29:01 +0000142%%
143
144{Comment} { /* Ignore comments for now */ }
145
146begin { return BEGINTOK; }
147end { return END; }
148true { return TRUE; }
149false { return FALSE; }
150declare { return DECLARE; }
Chris Lattnerda975502001-09-10 07:58:01 +0000151global { return GLOBAL; }
Chris Lattner7fb14f52001-09-18 04:00:54 +0000152constant { return CONSTANT; }
Chris Lattner571a6b02001-10-03 01:49:25 +0000153const { return CONST; }
Chris Lattner841d8b92001-11-26 18:54:16 +0000154internal { return INTERNAL; }
Chris Lattner7fb14f52001-09-18 04:00:54 +0000155uninitialized { return UNINIT; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156implementation { return IMPLEMENTATION; }
Chris Lattner42b5a8a2001-07-25 22:47:46 +0000157\.\.\. { return DOTDOTDOT; }
Chris Lattner26e50dc2001-07-28 17:48:55 +0000158string { return STRING; }
Chris Lattnerfbdec252001-09-30 22:46:54 +0000159null { return NULL_TOK; }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000160to { return TO; }
161except { return EXCEPT; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162
Chris Lattner47af30c2001-09-07 16:32:43 +0000163void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
164bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
165sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
166ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
167short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
168ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
169int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
170uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
171long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
172ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
173float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
174double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000175type { llvmAsmlval.PrimType = Type::TypeTy ; return TYPE; }
Chris Lattner47af30c2001-09-07 16:32:43 +0000176label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
Chris Lattnerf269b9d2002-04-04 19:22:17 +0000177opaque { return OPAQUE; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000178
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000179
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180not { RET_TOK(UnaryOpVal, Not, NOT); }
181
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182add { RET_TOK(BinaryOpVal, Add, ADD); }
183sub { RET_TOK(BinaryOpVal, Sub, SUB); }
184mul { RET_TOK(BinaryOpVal, Mul, MUL); }
185div { RET_TOK(BinaryOpVal, Div, DIV); }
186rem { RET_TOK(BinaryOpVal, Rem, REM); }
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000187and { RET_TOK(BinaryOpVal, And, AND); }
188or { RET_TOK(BinaryOpVal, Or , OR ); }
189xor { RET_TOK(BinaryOpVal, Xor, XOR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
191seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
192setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
193setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
194setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
195setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
196
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000197phi { RET_TOK(OtherOpVal, PHINode, PHI); }
198call { RET_TOK(OtherOpVal, Call, CALL); }
199cast { RET_TOK(OtherOpVal, Cast, CAST); }
200shl { RET_TOK(OtherOpVal, Shl, SHL); }
201shr { RET_TOK(OtherOpVal, Shr, SHR); }
202
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203ret { RET_TOK(TermOpVal, Ret, RET); }
204br { RET_TOK(TermOpVal, Br, BR); }
205switch { RET_TOK(TermOpVal, Switch, SWITCH); }
Chris Lattner3d4b2902001-10-13 06:37:14 +0000206invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207
208
209malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
210alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
211free { RET_TOK(MemOpVal, Free, FREE); }
212load { RET_TOK(MemOpVal, Load, LOAD); }
213store { RET_TOK(MemOpVal, Store, STORE); }
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000214getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215
216
Chris Lattner26e50dc2001-07-28 17:48:55 +0000217{VarID} {
218 UnEscapeLexed(yytext+1);
219 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
220 return VAR_ID;
221 }
222{Label} {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000223 yytext[strlen(yytext)-1] = 0; // nuke colon
Chris Lattner26e50dc2001-07-28 17:48:55 +0000224 UnEscapeLexed(yytext);
225 llvmAsmlval.StrVal = strdup(yytext);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226 return LABELSTR;
227 }
228
Chris Lattner26e50dc2001-07-28 17:48:55 +0000229{StringConstant} { // Note that we cannot unescape a string constant here! The
230 // string constant might contain a \00 which would not be
231 // understood by the string stuff. It is valid to make a
232 // [sbyte] c"Hello World\00" constant, for example.
233 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234 yytext[strlen(yytext)-1] = 0; // nuke end quote
Chris Lattner26e50dc2001-07-28 17:48:55 +0000235 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
Chris Lattner2f7c9632001-06-06 20:29:01 +0000236 return STRINGCONSTANT;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000237 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238
239
240{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
241{NInteger} {
242 uint64_t Val = atoull(yytext+1);
243 // +1: we have bigger negative range
244 if (Val > (uint64_t)INT64_MAX+1)
245 ThrowException("Constant too large for signed 64 bits!");
246 llvmAsmlval.SInt64Val = -Val;
247 return ESINT64VAL;
248 }
249
250
251{EPInteger} { llvmAsmlval.UIntVal = atoull(yytext+1); return UINTVAL; }
252{ENInteger} {
253 uint64_t Val = atoull(yytext+2);
254 // +1: we have bigger negative range
255 if (Val > (uint64_t)INT32_MAX+1)
256 ThrowException("Constant too large for signed 32 bits!");
257 llvmAsmlval.SIntVal = -Val;
258 return SINTVAL;
259 }
260
Chris Lattner212f70d2001-07-15 00:17:01 +0000261{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
Chris Lattner1b343742002-04-07 08:10:41 +0000262{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000263
264[ \t\n] { /* Ignore whitespace */ }
Chris Lattner47af30c2001-09-07 16:32:43 +0000265. { return yytext[0]; }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000266
267%%