blob: 6391d17a52eb536c49cdf6173d69b139564f5db1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29: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 "llvm/Support/MathExtras.h"
31#include <list>
32#include "llvmAsmParser.h"
33#include <cctype>
34#include <cstdlib>
35
36void set_scan_file(FILE * F){
37 yy_switch_to_buffer(yy_create_buffer( F, YY_BUF_SIZE ) );
38}
39void set_scan_string (const char * str) {
40 yy_scan_string (str);
41}
42
43// Construct a token value for a non-obsolete token
44#define RET_TOK(type, Enum, sym) \
45 llvmAsmlval.type = Instruction::Enum; \
46 return sym
47
48// Construct a token value for an obsolete token
49#define RET_TY(CTYPE, SYM) \
50 llvmAsmlval.PrimType = CTYPE;\
51 return SYM
52
53namespace llvm {
54
55// TODO: All of the static identifiers are figured out by the lexer,
56// these should be hashed to reduce the lexer size
57
58
59// atoull - Convert an ascii string of decimal digits into the unsigned long
60// long representation... this does not have to do input error checking,
61// because we know that the input will be matched by a suitable regex...
62//
63static uint64_t atoull(const char *Buffer) {
64 uint64_t Result = 0;
65 for (; *Buffer; Buffer++) {
66 uint64_t OldRes = Result;
67 Result *= 10;
68 Result += *Buffer-'0';
69 if (Result < OldRes) // Uh, oh, overflow detected!!!
70 GenerateError("constant bigger than 64 bits detected!");
71 }
72 return Result;
73}
74
75static uint64_t HexIntToVal(const char *Buffer) {
76 uint64_t Result = 0;
77 for (; *Buffer; ++Buffer) {
78 uint64_t OldRes = Result;
79 Result *= 16;
80 char C = *Buffer;
81 if (C >= '0' && C <= '9')
82 Result += C-'0';
83 else if (C >= 'A' && C <= 'F')
84 Result += C-'A'+10;
85 else if (C >= 'a' && C <= 'f')
86 Result += C-'a'+10;
87
88 if (Result < OldRes) // Uh, oh, overflow detected!!!
89 GenerateError("constant bigger than 64 bits detected!");
90 }
91 return Result;
92}
93
94
95// HexToFP - Convert the ascii string in hexidecimal format to the floating
96// point representation of it.
97//
98static double HexToFP(const char *Buffer) {
99 return BitsToDouble(HexIntToVal(Buffer)); // Cast Hex constant to double
100}
101
102
103// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
104// appropriate character.
105char *UnEscapeLexed(char *Buffer, char* EndBuffer) {
106 char *BOut = Buffer;
107 for (char *BIn = Buffer; *BIn; ) {
108 if (BIn[0] == '\\') {
109 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
110 *BOut++ = '\\'; // Two \ becomes one
111 BIn += 2;
112 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
113 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
114 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
115 BIn[3] = Tmp; // Restore character
116 BIn += 3; // Skip over handled chars
117 ++BOut;
118 } else {
119 *BOut++ = *BIn++;
120 }
121 } else {
122 *BOut++ = *BIn++;
123 }
124 }
125 return BOut;
126}
127
128} // End llvm namespace
129
130using namespace llvm;
131
132#define YY_NEVER_INTERACTIVE 1
133%}
134
135
136
137/* Comments start with a ; and go till end of line */
138Comment ;.*
139
140/* Local Values and Type identifiers start with a % sign */
141LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
142
143/* Global Value identifiers start with an @ sign */
144GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
145
146/* Label identifiers end with a colon */
147Label [-a-zA-Z$._0-9]+:
148QuoteLabel \"[^\"]+\":
149
150/* Quoted names can contain any character except " and \ */
151StringConstant \"[^\"]*\"
152AtStringConstant @\"[^\"]*\"
153PctStringConstant %\"[^\"]*\"
154
155/* LocalVarID/GlobalVarID: match an unnamed local variable slot ID. */
156LocalVarID %[0-9]+
157GlobalVarID @[0-9]+
158
159/* Integer types are specified with i and a bitwidth */
160IntegerType i[0-9]+
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
182{Comment} { /* Ignore comments for now */ }
183
184begin { return BEGINTOK; }
185end { return ENDTOK; }
186true { return TRUETOK; }
187false { return FALSETOK; }
188declare { return DECLARE; }
189define { return DEFINE; }
190global { return GLOBAL; }
191constant { return CONSTANT; }
192internal { return INTERNAL; }
193linkonce { return LINKONCE; }
194weak { return WEAK; }
195appending { return APPENDING; }
196dllimport { return DLLIMPORT; }
197dllexport { return DLLEXPORT; }
198hidden { return HIDDEN; }
199protected { return PROTECTED; }
200extern_weak { return EXTERN_WEAK; }
201external { return EXTERNAL; }
202thread_local { return THREAD_LOCAL; }
203zeroinitializer { return ZEROINITIALIZER; }
204\.\.\. { return DOTDOTDOT; }
205undef { return UNDEF; }
206null { return NULL_TOK; }
207to { return TO; }
208tail { return TAIL; }
209target { return TARGET; }
210triple { return TRIPLE; }
211deplibs { return DEPLIBS; }
212datalayout { return DATALAYOUT; }
213volatile { return VOLATILE; }
214align { return ALIGN; }
215section { return SECTION; }
216alias { return ALIAS; }
217module { return MODULE; }
218asm { return ASM_TOK; }
219sideeffect { return SIDEEFFECT; }
220
221cc { return CC_TOK; }
222ccc { return CCC_TOK; }
223fastcc { return FASTCC_TOK; }
224coldcc { return COLDCC_TOK; }
225x86_stdcallcc { return X86_STDCALLCC_TOK; }
226x86_fastcallcc { return X86_FASTCALLCC_TOK; }
227
228inreg { return INREG; }
229sret { return SRET; }
230nounwind { return NOUNWIND; }
231noreturn { return NORETURN; }
232noalias { return NOALIAS; }
233
234void { RET_TY(Type::VoidTy, VOID); }
235float { RET_TY(Type::FloatTy, FLOAT); }
236double { RET_TY(Type::DoubleTy,DOUBLE);}
237label { RET_TY(Type::LabelTy, LABEL); }
238type { return TYPE; }
239opaque { return OPAQUE; }
240{IntegerType} { uint64_t NumBits = atoull(yytext+1);
241 if (NumBits < IntegerType::MIN_INT_BITS ||
242 NumBits > IntegerType::MAX_INT_BITS)
243 GenerateError("Bitwidth for integer type out of range!");
244 const Type* Ty = IntegerType::get(NumBits);
245 RET_TY(Ty, INTTYPE);
246 }
247
248add { RET_TOK(BinaryOpVal, Add, ADD); }
249sub { RET_TOK(BinaryOpVal, Sub, SUB); }
250mul { RET_TOK(BinaryOpVal, Mul, MUL); }
251udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
252sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
253fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
254urem { RET_TOK(BinaryOpVal, URem, UREM); }
255srem { RET_TOK(BinaryOpVal, SRem, SREM); }
256frem { RET_TOK(BinaryOpVal, FRem, FREM); }
257shl { RET_TOK(BinaryOpVal, Shl, SHL); }
258lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
259ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
260and { RET_TOK(BinaryOpVal, And, AND); }
261or { RET_TOK(BinaryOpVal, Or , OR ); }
262xor { RET_TOK(BinaryOpVal, Xor, XOR); }
263icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
264fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
265
266eq { return EQ; }
267ne { return NE; }
268slt { return SLT; }
269sgt { return SGT; }
270sle { return SLE; }
271sge { return SGE; }
272ult { return ULT; }
273ugt { return UGT; }
274ule { return ULE; }
275uge { return UGE; }
276oeq { return OEQ; }
277one { return ONE; }
278olt { return OLT; }
279ogt { return OGT; }
280ole { return OLE; }
281oge { return OGE; }
282ord { return ORD; }
283uno { return UNO; }
284ueq { return UEQ; }
285une { return UNE; }
286
287phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
288call { RET_TOK(OtherOpVal, Call, CALL); }
289trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
290zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
291sext { RET_TOK(CastOpVal, SExt, SEXT); }
292fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
293fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
294uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
295sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
296fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
297fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
298inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
299ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
300bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
301select { RET_TOK(OtherOpVal, Select, SELECT); }
302va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
303ret { RET_TOK(TermOpVal, Ret, RET); }
304br { RET_TOK(TermOpVal, Br, BR); }
305switch { RET_TOK(TermOpVal, Switch, SWITCH); }
306invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
307unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
308unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
309
310malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
311alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
312free { RET_TOK(MemOpVal, Free, FREE); }
313load { RET_TOK(MemOpVal, Load, LOAD); }
314store { RET_TOK(MemOpVal, Store, STORE); }
315getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
316
317extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
318insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
319shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
320
321
322{LocalVarName} {
323 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip %
324 return LOCALVAR;
325 }
326{GlobalVarName} {
327 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip @
328 return GLOBALVAR;
329 }
330{Label} {
331 yytext[yyleng-1] = 0; // nuke colon
332 llvmAsmlval.StrVal = new std::string(yytext);
333 return LABELSTR;
334 }
335{QuoteLabel} {
336 yytext[yyleng-2] = 0; // nuke colon, end quote
337 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
338 llvmAsmlval.StrVal =
339 new std::string(yytext+1, EndChar - yytext - 1);
340 return LABELSTR;
341 }
342
343{StringConstant} { yytext[yyleng-1] = 0; // nuke end quote
344 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
345 llvmAsmlval.StrVal =
346 new std::string(yytext+1, EndChar - yytext - 1);
347 return STRINGCONSTANT;
348 }
349{AtStringConstant} {
350 yytext[yyleng-1] = 0; // nuke end quote
351 const char* EndChar =
352 UnEscapeLexed(yytext+2, yytext+yyleng);
353 llvmAsmlval.StrVal =
354 new std::string(yytext+2, EndChar - yytext - 2);
355 return ATSTRINGCONSTANT;
356 }
357{PctStringConstant} {
358 yytext[yyleng-1] = 0; // nuke end quote
359 const char* EndChar =
360 UnEscapeLexed(yytext+2, yytext+yyleng);
361 llvmAsmlval.StrVal =
362 new std::string(yytext+2, EndChar - yytext - 2);
363 return PCTSTRINGCONSTANT;
364 }
365{PInteger} {
366 uint32_t numBits = ((yyleng * 64) / 19) + 1;
367 APInt Tmp(numBits, yytext, yyleng, 10);
368 uint32_t activeBits = Tmp.getActiveBits();
369 if (activeBits > 0 && activeBits < numBits)
370 Tmp.trunc(activeBits);
371 if (Tmp.getBitWidth() > 64) {
372 llvmAsmlval.APIntVal = new APInt(Tmp);
373 return EUAPINTVAL;
374 } else {
375 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
376 return EUINT64VAL;
377 }
378 }
379{NInteger} {
380 uint32_t numBits = (((yyleng-1) * 64) / 19) + 2;
381 APInt Tmp(numBits, yytext, yyleng, 10);
382 uint32_t minBits = Tmp.getMinSignedBits();
383 if (minBits > 0 && minBits < numBits)
384 Tmp.trunc(minBits);
385 if (Tmp.getBitWidth() > 64) {
386 llvmAsmlval.APIntVal = new APInt(Tmp);
387 return ESAPINTVAL;
388 } else {
389 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
390 return ESINT64VAL;
391 }
392 }
393
394{HexIntConstant} { int len = yyleng - 3;
395 uint32_t bits = len * 4;
396 APInt Tmp(bits, yytext+3, len, 16);
397 uint32_t activeBits = Tmp.getActiveBits();
398 if (activeBits > 0 && activeBits < bits)
399 Tmp.trunc(activeBits);
400 if (Tmp.getBitWidth() > 64) {
401 llvmAsmlval.APIntVal = new APInt(Tmp);
402 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
403 } else if (yytext[0] == 's') {
404 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
405 return ESINT64VAL;
406 } else {
407 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
408 return EUINT64VAL;
409 }
410 }
411
412{LocalVarID} {
413 uint64_t Val = atoull(yytext+1);
414 if ((unsigned)Val != Val)
415 GenerateError("Invalid value number (too large)!");
416 llvmAsmlval.UIntVal = unsigned(Val);
417 return LOCALVAL_ID;
418 }
419{GlobalVarID} {
420 uint64_t Val = atoull(yytext+1);
421 if ((unsigned)Val != Val)
422 GenerateError("Invalid value number (too large)!");
423 llvmAsmlval.UIntVal = unsigned(Val);
424 return GLOBALVAL_ID;
425 }
426
427{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
428{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
429
430<<EOF>> {
431 /* Make sure to free the internal buffers for flex when we are
432 * done reading our input!
433 */
434 yy_delete_buffer(YY_CURRENT_BUFFER);
435 return EOF;
436 }
437
438[ \r\t\n] { /* Ignore whitespace */ }
439. { return yytext[0]; }
440
441%%