blob: 19be3dc3b4961629a0986b95178ef21bb0596672 [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
Reid Spenceraa8ae282007-07-31 03:50:36 +0000180/* WSNL - shorthand for newline followed by whitespace */
181WSNL [ \r\t\n]*$
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182%%
183
184{Comment} { /* Ignore comments for now */ }
185
186begin { return BEGINTOK; }
187end { return ENDTOK; }
188true { return TRUETOK; }
189false { return FALSETOK; }
190declare { return DECLARE; }
191define { return DEFINE; }
192global { return GLOBAL; }
193constant { return CONSTANT; }
194internal { return INTERNAL; }
195linkonce { return LINKONCE; }
196weak { return WEAK; }
197appending { return APPENDING; }
198dllimport { return DLLIMPORT; }
199dllexport { return DLLEXPORT; }
200hidden { return HIDDEN; }
201protected { return PROTECTED; }
202extern_weak { return EXTERN_WEAK; }
203external { return EXTERNAL; }
204thread_local { return THREAD_LOCAL; }
205zeroinitializer { return ZEROINITIALIZER; }
206\.\.\. { return DOTDOTDOT; }
207undef { return UNDEF; }
208null { return NULL_TOK; }
209to { return TO; }
210tail { return TAIL; }
211target { return TARGET; }
212triple { return TRIPLE; }
213deplibs { return DEPLIBS; }
214datalayout { return DATALAYOUT; }
215volatile { return VOLATILE; }
216align { return ALIGN; }
217section { return SECTION; }
218alias { return ALIAS; }
219module { return MODULE; }
220asm { return ASM_TOK; }
221sideeffect { return SIDEEFFECT; }
222
223cc { return CC_TOK; }
224ccc { return CCC_TOK; }
225fastcc { return FASTCC_TOK; }
226coldcc { return COLDCC_TOK; }
227x86_stdcallcc { return X86_STDCALLCC_TOK; }
228x86_fastcallcc { return X86_FASTCALLCC_TOK; }
229
Reid Spenceraa8ae282007-07-31 03:50:36 +0000230signext { return SIGNEXT; }
231zeroext { return ZEROEXT; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232inreg { return INREG; }
233sret { return SRET; }
234nounwind { return NOUNWIND; }
235noreturn { return NORETURN; }
236noalias { return NOALIAS; }
Reid Spenceraa8ae282007-07-31 03:50:36 +0000237byval { return BYVAL; }
238nest { return NEST; }
239sext{WSNL} { // For auto-upgrade only, drop in LLVM 3.0
240 return SIGNEXT; }
241zext{WSNL} { // For auto-upgrade only, drop in LLVM 3.0
242 return ZEROEXT; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243
244void { RET_TY(Type::VoidTy, VOID); }
245float { RET_TY(Type::FloatTy, FLOAT); }
246double { RET_TY(Type::DoubleTy,DOUBLE);}
247label { RET_TY(Type::LabelTy, LABEL); }
248type { return TYPE; }
249opaque { return OPAQUE; }
250{IntegerType} { uint64_t NumBits = atoull(yytext+1);
251 if (NumBits < IntegerType::MIN_INT_BITS ||
252 NumBits > IntegerType::MAX_INT_BITS)
253 GenerateError("Bitwidth for integer type out of range!");
254 const Type* Ty = IntegerType::get(NumBits);
255 RET_TY(Ty, INTTYPE);
256 }
257
258add { RET_TOK(BinaryOpVal, Add, ADD); }
259sub { RET_TOK(BinaryOpVal, Sub, SUB); }
260mul { RET_TOK(BinaryOpVal, Mul, MUL); }
261udiv { RET_TOK(BinaryOpVal, UDiv, UDIV); }
262sdiv { RET_TOK(BinaryOpVal, SDiv, SDIV); }
263fdiv { RET_TOK(BinaryOpVal, FDiv, FDIV); }
264urem { RET_TOK(BinaryOpVal, URem, UREM); }
265srem { RET_TOK(BinaryOpVal, SRem, SREM); }
266frem { RET_TOK(BinaryOpVal, FRem, FREM); }
267shl { RET_TOK(BinaryOpVal, Shl, SHL); }
268lshr { RET_TOK(BinaryOpVal, LShr, LSHR); }
269ashr { RET_TOK(BinaryOpVal, AShr, ASHR); }
270and { RET_TOK(BinaryOpVal, And, AND); }
271or { RET_TOK(BinaryOpVal, Or , OR ); }
272xor { RET_TOK(BinaryOpVal, Xor, XOR); }
273icmp { RET_TOK(OtherOpVal, ICmp, ICMP); }
274fcmp { RET_TOK(OtherOpVal, FCmp, FCMP); }
275
276eq { return EQ; }
277ne { return NE; }
278slt { return SLT; }
279sgt { return SGT; }
280sle { return SLE; }
281sge { return SGE; }
282ult { return ULT; }
283ugt { return UGT; }
284ule { return ULE; }
285uge { return UGE; }
286oeq { return OEQ; }
287one { return ONE; }
288olt { return OLT; }
289ogt { return OGT; }
290ole { return OLE; }
291oge { return OGE; }
292ord { return ORD; }
293uno { return UNO; }
294ueq { return UEQ; }
295une { return UNE; }
296
297phi { RET_TOK(OtherOpVal, PHI, PHI_TOK); }
298call { RET_TOK(OtherOpVal, Call, CALL); }
299trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
300zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
301sext { RET_TOK(CastOpVal, SExt, SEXT); }
302fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
303fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
304uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }
305sitofp { RET_TOK(CastOpVal, SIToFP, SITOFP); }
306fptoui { RET_TOK(CastOpVal, FPToUI, FPTOUI); }
307fptosi { RET_TOK(CastOpVal, FPToSI, FPTOSI); }
308inttoptr { RET_TOK(CastOpVal, IntToPtr, INTTOPTR); }
309ptrtoint { RET_TOK(CastOpVal, PtrToInt, PTRTOINT); }
310bitcast { RET_TOK(CastOpVal, BitCast, BITCAST); }
311select { RET_TOK(OtherOpVal, Select, SELECT); }
312va_arg { RET_TOK(OtherOpVal, VAArg , VAARG); }
313ret { RET_TOK(TermOpVal, Ret, RET); }
314br { RET_TOK(TermOpVal, Br, BR); }
315switch { RET_TOK(TermOpVal, Switch, SWITCH); }
316invoke { RET_TOK(TermOpVal, Invoke, INVOKE); }
317unwind { RET_TOK(TermOpVal, Unwind, UNWIND); }
318unreachable { RET_TOK(TermOpVal, Unreachable, UNREACHABLE); }
319
320malloc { RET_TOK(MemOpVal, Malloc, MALLOC); }
321alloca { RET_TOK(MemOpVal, Alloca, ALLOCA); }
322free { RET_TOK(MemOpVal, Free, FREE); }
323load { RET_TOK(MemOpVal, Load, LOAD); }
324store { RET_TOK(MemOpVal, Store, STORE); }
325getelementptr { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
326
327extractelement { RET_TOK(OtherOpVal, ExtractElement, EXTRACTELEMENT); }
328insertelement { RET_TOK(OtherOpVal, InsertElement, INSERTELEMENT); }
329shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); }
330
331
332{LocalVarName} {
333 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip %
334 return LOCALVAR;
335 }
336{GlobalVarName} {
337 llvmAsmlval.StrVal = new std::string(yytext+1); // Skip @
338 return GLOBALVAR;
339 }
340{Label} {
341 yytext[yyleng-1] = 0; // nuke colon
342 llvmAsmlval.StrVal = new std::string(yytext);
343 return LABELSTR;
344 }
345{QuoteLabel} {
346 yytext[yyleng-2] = 0; // nuke colon, end quote
347 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
348 llvmAsmlval.StrVal =
349 new std::string(yytext+1, EndChar - yytext - 1);
350 return LABELSTR;
351 }
352
353{StringConstant} { yytext[yyleng-1] = 0; // nuke end quote
354 const char* EndChar = UnEscapeLexed(yytext+1, yytext+yyleng);
355 llvmAsmlval.StrVal =
356 new std::string(yytext+1, EndChar - yytext - 1);
357 return STRINGCONSTANT;
358 }
359{AtStringConstant} {
360 yytext[yyleng-1] = 0; // nuke end quote
361 const char* EndChar =
362 UnEscapeLexed(yytext+2, yytext+yyleng);
363 llvmAsmlval.StrVal =
364 new std::string(yytext+2, EndChar - yytext - 2);
365 return ATSTRINGCONSTANT;
366 }
367{PctStringConstant} {
368 yytext[yyleng-1] = 0; // nuke end quote
369 const char* EndChar =
370 UnEscapeLexed(yytext+2, yytext+yyleng);
371 llvmAsmlval.StrVal =
372 new std::string(yytext+2, EndChar - yytext - 2);
373 return PCTSTRINGCONSTANT;
374 }
375{PInteger} {
376 uint32_t numBits = ((yyleng * 64) / 19) + 1;
377 APInt Tmp(numBits, yytext, yyleng, 10);
378 uint32_t activeBits = Tmp.getActiveBits();
379 if (activeBits > 0 && activeBits < numBits)
380 Tmp.trunc(activeBits);
381 if (Tmp.getBitWidth() > 64) {
382 llvmAsmlval.APIntVal = new APInt(Tmp);
383 return EUAPINTVAL;
384 } else {
385 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
386 return EUINT64VAL;
387 }
388 }
389{NInteger} {
390 uint32_t numBits = (((yyleng-1) * 64) / 19) + 2;
391 APInt Tmp(numBits, yytext, yyleng, 10);
392 uint32_t minBits = Tmp.getMinSignedBits();
393 if (minBits > 0 && minBits < numBits)
394 Tmp.trunc(minBits);
395 if (Tmp.getBitWidth() > 64) {
396 llvmAsmlval.APIntVal = new APInt(Tmp);
397 return ESAPINTVAL;
398 } else {
399 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
400 return ESINT64VAL;
401 }
402 }
403
404{HexIntConstant} { int len = yyleng - 3;
405 uint32_t bits = len * 4;
406 APInt Tmp(bits, yytext+3, len, 16);
407 uint32_t activeBits = Tmp.getActiveBits();
408 if (activeBits > 0 && activeBits < bits)
409 Tmp.trunc(activeBits);
410 if (Tmp.getBitWidth() > 64) {
411 llvmAsmlval.APIntVal = new APInt(Tmp);
412 return yytext[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
413 } else if (yytext[0] == 's') {
414 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
415 return ESINT64VAL;
416 } else {
417 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
418 return EUINT64VAL;
419 }
420 }
421
422{LocalVarID} {
423 uint64_t Val = atoull(yytext+1);
424 if ((unsigned)Val != Val)
425 GenerateError("Invalid value number (too large)!");
426 llvmAsmlval.UIntVal = unsigned(Val);
427 return LOCALVAL_ID;
428 }
429{GlobalVarID} {
430 uint64_t Val = atoull(yytext+1);
431 if ((unsigned)Val != Val)
432 GenerateError("Invalid value number (too large)!");
433 llvmAsmlval.UIntVal = unsigned(Val);
434 return GLOBALVAL_ID;
435 }
436
437{FPConstant} { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
438{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
439
440<<EOF>> {
441 /* Make sure to free the internal buffers for flex when we are
442 * done reading our input!
443 */
444 yy_delete_buffer(YY_CURRENT_BUFFER);
445 return EOF;
446 }
447
448[ \r\t\n] { /* Ignore whitespace */ }
449. { return yytext[0]; }
450
451%%