blob: f93060968479b4744efe5bb0712fd6e50382e3af [file] [log] [blame]
Chris Lattner32eecb02006-02-14 05:14:46 +00001/*===-- Lexer.l - Scanner for llvm assembly files --------------*- C++ -*--===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the flex scanner for LLVM assembly languages files.
11//
12//===----------------------------------------------------------------------===*/
13
14%option prefix="llvmAsm"
15%option yylineno
16%option nostdinit
17%option never-interactive
18%option batch
19%option noyywrap
20%option nodefault
21%option 8bit
22%option outfile="Lexer.cpp"
23%option ecs
24%option noreject
25%option noyymore
26
27%{
28#include "ParserInternals.h"
29#include "llvm/Module.h"
30#include <list>
31#include "llvmAsmParser.h"
32#include <cctype>
33#include <cstdlib>
34
35void set_scan_file(FILE * F){
36 yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
37}
38void set_scan_string (const char * str) {
39 yy_scan_string (str);
40}
41
42#define RET_TOK(type, Enum, sym) \
43 llvmAsmlval.type = Instruction::Enum; return sym
44
45namespace llvm {
46
47// TODO: All of the static identifiers are figured out by the lexer,
48// these should be hashed to reduce the lexer size
49
50
51// atoull - Convert an ascii string of decimal digits into the unsigned long
52// long representation... this does not have to do input error checking,
53// because we know that the input will be matched by a suitable regex...
54//
55static uint64_t atoull(const char *Buffer) {
56 uint64_t Result = 0;
57 for (; *Buffer; Buffer++) {
58 uint64_t OldRes = Result;
59 Result *= 10;
60 Result += *Buffer-'0';
61 if (Result < OldRes) // Uh, oh, overflow detected!!!
62 ThrowException("constant bigger than 64 bits detected!");
63 }
64 return Result;
65}
66
67static uint64_t HexIntToVal(const char *Buffer) {
68 uint64_t Result = 0;
69 for (; *Buffer; ++Buffer) {
70 uint64_t OldRes = Result;
71 Result *= 16;
72 char C = *Buffer;
73 if (C >= '0' && C <= '9')
74 Result += C-'0';
75 else if (C >= 'A' && C <= 'F')
76 Result += C-'A'+10;
77 else if (C >= 'a' && C <= 'f')
78 Result += C-'a'+10;
79
80 if (Result < OldRes) // Uh, oh, overflow detected!!!
81 ThrowException("constant bigger than 64 bits detected!");
82 }
83 return Result;
84}
85
86
87// HexToFP - Convert the ascii string in hexidecimal format to the floating
88// point representation of it.
89//
90static double HexToFP(const char *Buffer) {
91 // Behave nicely in the face of C TBAA rules... see:
92 // http://www.nullstone.com/htmls/category/aliastyp.htm
93 union {
94 uint64_t UI;
95 double FP;
96 } UIntToFP;
97 UIntToFP.UI = HexIntToVal(Buffer);
98
99 assert(sizeof(double) == sizeof(uint64_t) &&
100 "Data sizes incompatible on this target!");
101 return UIntToFP.FP; // Cast Hex constant to double
102}
103
104
105// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
106// appropriate character. If AllowNull is set to false, a \00 value will cause
107// an exception to be thrown.
108//
109// If AllowNull is set to true, the return value of the function points to the
110// last character of the string in memory.
111//
112char *UnEscapeLexed(char *Buffer, bool AllowNull) {
113 char *BOut = Buffer;
114 for (char *BIn = Buffer; *BIn; ) {
115 if (BIn[0] == '\\' && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
116 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
117 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
118 if (!AllowNull && !*BOut)
119 ThrowException("String literal cannot accept \\00 escape!");
120
121 BIn[3] = Tmp; // Restore character
122 BIn += 3; // Skip over handled chars
123 ++BOut;
124 } else {
125 *BOut++ = *BIn++;
126 }
127 }
128
129 return BOut;
130}
131
132} // End llvm namespace
133
134using namespace llvm;
135
136#define YY_NEVER_INTERACTIVE 1
137%}
138
139
140
141/* Comments start with a ; and go till end of line */
142Comment ;.*
143
144/* Variable(Value) identifiers start with a % sign */
145VarID %[-a-zA-Z$._][-a-zA-Z$._0-9]*
146
147/* Label identifiers end with a colon */
148Label [-a-zA-Z$._0-9]+:
149QuoteLabel \"[^\"]+\":
150
151/* Quoted names can contain any character except " and \ */
152StringConstant \"[^\"]*\"
153
154
155/* [PN]Integer: match positive and negative literal integer values that
156 * are preceeded by a '%' character. These represent unnamed variable slots.
157 */
158EPInteger %[0-9]+
159ENInteger %-[0-9]+
160
161
162/* E[PN]Integer: match positive and negative literal integer values */
163PInteger [0-9]+
164NInteger -[0-9]+
165
166/* FPConstant - A Floating point constant.
167 */
168FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
169
170/* HexFPConstant - Floating point constant represented in IEEE format as a
171 * hexadecimal number for when exponential notation is not precise enough.
172 */
173HexFPConstant 0x[0-9A-Fa-f]+
174
175/* HexIntConstant - Hexadecimal constant generated by the CFE to avoid forcing
176 * it to deal with 64 bit numbers.
177 */
178HexIntConstant [us]0x[0-9A-Fa-f]+
179%%
180
181{Comment} { /* Ignore comments for now */ }
182
183begin { return BEGINTOK; }
184end { return ENDTOK; }
185true { return TRUETOK; }
186false { return FALSETOK; }
187declare { return DECLARE; }
188global { return GLOBAL; }
189constant { return CONSTANT; }
190internal { return INTERNAL; }
191linkonce { return LINKONCE; }
192weak { return WEAK; }
193appending { return APPENDING; }
194uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
195external { return EXTERNAL; }
196implementation { return IMPLEMENTATION; }
197zeroinitializer { return ZEROINITIALIZER; }
198\.\.\. { return DOTDOTDOT; }
199undef { return UNDEF; }
200null { return NULL_TOK; }
201to { return TO; }
202except { RET_TOK(TermOpVal, Unwind, UNWIND); }
203not { return NOT; } /* Deprecated, turned into XOR */
204tail { return TAIL; }
205target { return TARGET; }
206triple { return TRIPLE; }
207deplibs { return DEPLIBS; }
208endian { return ENDIAN; }
209pointersize { return POINTERSIZE; }
210little { return LITTLE; }
211big { return BIG; }
212volatile { return VOLATILE; }
213align { return ALIGN; }
214section { return SECTION; }
215module { return MODULE; }
216asm { return ASM_TOK; }
217sideeffect { return SIDEEFFECT; }
218
219cc { return CC_TOK; }
220ccc { return CCC_TOK; }
221fastcc { return FASTCC_TOK; }
222coldcc { return COLDCC_TOK; }
223
224void { llvmAsmlval.PrimType = Type::VoidTy ; return VOID; }
225bool { llvmAsmlval.PrimType = Type::BoolTy ; return BOOL; }
226sbyte { llvmAsmlval.PrimType = Type::SByteTy ; return SBYTE; }
227ubyte { llvmAsmlval.PrimType = Type::UByteTy ; return UBYTE; }
228short { llvmAsmlval.PrimType = Type::ShortTy ; return SHORT; }
229ushort { llvmAsmlval.PrimType = Type::UShortTy; return USHORT; }
230int { llvmAsmlval.PrimType = Type::IntTy ; return INT; }
231uint { llvmAsmlval.PrimType = Type::UIntTy ; return UINT; }
232long { llvmAsmlval.PrimType = Type::LongTy ; return LONG; }
233ulong { llvmAsmlval.PrimType = Type::ULongTy ; return ULONG; }
234float { llvmAsmlval.PrimType = Type::FloatTy ; return FLOAT; }
235double { llvmAsmlval.PrimType = Type::DoubleTy; return DOUBLE; }
236label { llvmAsmlval.PrimType = Type::LabelTy ; return LABEL; }
237type { return TYPE; }
238opaque { return OPAQUE; }
239
240add { RET_TOK(BinaryOpVal, Add, ADD); }
241sub { RET_TOK(BinaryOpVal, Sub, SUB); }
242mul { RET_TOK(BinaryOpVal, Mul, MUL); }
243div { RET_TOK(BinaryOpVal, Div, DIV); }
244rem { RET_TOK(BinaryOpVal, Rem, REM); }
245and { RET_TOK(BinaryOpVal, And, AND); }
246or { RET_TOK(BinaryOpVal, Or , OR ); }
247xor { RET_TOK(BinaryOpVal, Xor, XOR); }
248setne { RET_TOK(BinaryOpVal, SetNE, SETNE); }
249seteq { RET_TOK(BinaryOpVal, SetEQ, SETEQ); }
250setlt { RET_TOK(BinaryOpVal, SetLT, SETLT); }
251setgt { RET_TOK(BinaryOpVal, SetGT, SETGT); }
252setle { RET_TOK(BinaryOpVal, SetLE, SETLE); }
253setge { RET_TOK(BinaryOpVal, SetGE, SETGE); }
254
255phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
256call { RET_TOK(OtherOpVal, Call, CALL); }
257cast { RET_TOK(OtherOpVal, Cast, CAST); }
258select { RET_TOK(OtherOpVal, Select, SELECT); }
259shl { RET_TOK(OtherOpVal, Shl, SHL); }
260shr { RET_TOK(OtherOpVal, Shr, SHR); }
261vanext { return VANEXT_old; }
262vaarg { return VAARG_old; }
263va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
264ret { RET_TOK(TermOpVal, Ret, RET); }
265br { RET_TOK(TermOpVal, Br, BR); }
266switch { RET_TOK(TermOpVal, Switch, SWITCH); }
267invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
268unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
269unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
270
271malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
272alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
273free { RET_TOK(MemOpVal, Free, FREE); }
274load { RET_TOK(MemOpVal, Load, LOAD); }
275store { RET_TOK(MemOpVal, Store, STORE); }
276getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
277
278extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
279insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
Chris Lattnerd5efe842006-04-08 01:18:56 +0000280shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
Chris Lattner32eecb02006-02-14 05:14:46 +0000281
282
283{VarID} {
284 UnEscapeLexed(yytext+1);
285 llvmAsmlval.StrVal = strdup(yytext+1); // Skip %
286 return VAR_ID;
287 }
288{Label} {
289 yytext[strlen(yytext)-1] = 0; // nuke colon
290 UnEscapeLexed(yytext);
291 llvmAsmlval.StrVal = strdup(yytext);
292 return LABELSTR;
293 }
294{QuoteLabel} {
295 yytext[strlen(yytext)-2] = 0; // nuke colon, end quote
296 UnEscapeLexed(yytext+1);
297 llvmAsmlval.StrVal = strdup(yytext+1);
298 return LABELSTR;
299 }
300
301{StringConstant} { // Note that we cannot unescape a string constant here! The
302 // string constant might contain a \00 which would not be
303 // understood by the string stuff. It is valid to make a
304 // [sbyte] c"Hello World\00" constant, for example.
305 //
306 yytext[strlen(yytext)-1] = 0; // nuke end quote
307 llvmAsmlval.StrVal = strdup(yytext+1); // Nuke start quote
308 return STRINGCONSTANT;
309 }
310
311
312{PInteger} { llvmAsmlval.UInt64Val = atoull(yytext); return EUINT64VAL; }
313{NInteger} {
314 uint64_t Val = atoull(yytext+1);
315 // +1: we have bigger negative range
316 if (Val > (uint64_t)INT64_MAX+1)
317 ThrowException("Constant too large for signed 64 bits!");
318 llvmAsmlval.SInt64Val = -Val;
319 return ESINT64VAL;
320 }
321{HexIntConstant} {
322 llvmAsmlval.UInt64Val = HexIntToVal(yytext+3);
323 return yytext[0] == 's' ? ESINT64VAL : EUINT64VAL;
324 }
325
326{EPInteger} {
327 uint64_t Val = atoull(yytext+1);
328 if ((unsigned)Val != Val)
329 ThrowException("Invalid value number (too large)!");
330 llvmAsmlval.UIntVal = unsigned(Val);
331 return UINTVAL;
332 }
333{ENInteger} {
334 uint64_t Val = atoull(yytext+2);
335 // +1: we have bigger negative range
336 if (Val > (uint64_t)INT32_MAX+1)
337 ThrowException("Constant too large for signed 32 bits!");
338 llvmAsmlval.SIntVal = (int)-Val;
339 return SINTVAL;
340 }
341
342{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
343{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
344
345<<EOF>> {
346 /* Make sure to free the internal buffers for flex when we are
347 * done reading our input!
348 */
349 yy_delete_buffer(YY_CURRENT_BUFFER);
350 return EOF;
351 }
352
353[ \r\t\n] { /* Ignore whitespace */ }
354. { return yytext[0]; }
355
356%%
Chris Lattner2fecc0f2006-02-15 07:02:59 +0000357