72e19f1d4d3178d775b2df7625a3a16bd191986f
[oota-llvm.git] / lib / AsmParser / LLLexer.cpp
1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implement the Lexer for .ll files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "LLLexer.h"
15 #include "ParserInternals.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/MathExtras.h"
18
19 #include <list>
20 #include "llvmAsmParser.h"
21 using namespace llvm;
22
23 //===----------------------------------------------------------------------===//
24 // Helper functions.
25 //===----------------------------------------------------------------------===//
26
27 // atoull - Convert an ascii string of decimal digits into the unsigned long
28 // long representation... this does not have to do input error checking,
29 // because we know that the input will be matched by a suitable regex...
30 //
31 static uint64_t atoull(const char *Buffer, const char *End) {
32   uint64_t Result = 0;
33   for (; Buffer != End; Buffer++) {
34     uint64_t OldRes = Result;
35     Result *= 10;
36     Result += *Buffer-'0';
37     if (Result < OldRes) {  // Uh, oh, overflow detected!!!
38       GenerateError("constant bigger than 64 bits detected!");
39       return 0;
40     }
41   }
42   return Result;
43 }
44
45 static uint64_t HexIntToVal(const char *Buffer, const char *End) {
46   uint64_t Result = 0;
47   for (; Buffer != End; ++Buffer) {
48     uint64_t OldRes = Result;
49     Result *= 16;
50     char C = *Buffer;
51     if (C >= '0' && C <= '9')
52       Result += C-'0';
53     else if (C >= 'A' && C <= 'F')
54       Result += C-'A'+10;
55     else if (C >= 'a' && C <= 'f')
56       Result += C-'a'+10;
57     
58     if (Result < OldRes) {   // Uh, oh, overflow detected!!!
59       GenerateError("constant bigger than 64 bits detected!");
60       return 0;
61     }
62   }
63   return Result;
64 }
65
66 // HexToFP - Convert the ascii string in hexadecimal format to the floating
67 // point representation of it.
68 //
69 static double HexToFP(const char *Buffer, const char *End) {
70   return BitsToDouble(HexIntToVal(Buffer, End)); // Cast Hex constant to double
71 }
72
73 static void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]){
74   Pair[0] = 0;
75   for (int i=0; i<16; i++, Buffer++) {
76     assert(Buffer != End);
77     Pair[0] *= 16;
78     char C = *Buffer;
79     if (C >= '0' && C <= '9')
80       Pair[0] += C-'0';
81     else if (C >= 'A' && C <= 'F')
82       Pair[0] += C-'A'+10;
83     else if (C >= 'a' && C <= 'f')
84       Pair[0] += C-'a'+10;
85   }
86   Pair[1] = 0;
87   for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
88     Pair[1] *= 16;
89     char C = *Buffer;
90     if (C >= '0' && C <= '9')
91       Pair[1] += C-'0';
92     else if (C >= 'A' && C <= 'F')
93       Pair[1] += C-'A'+10;
94     else if (C >= 'a' && C <= 'f')
95       Pair[1] += C-'a'+10;
96   }
97   if (Buffer != End)
98     GenerateError("constant bigger than 128 bits detected!");
99 }
100
101 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
102 // appropriate character.
103 static void UnEscapeLexed(std::string &Str) {
104   if (Str.empty()) return;
105   
106   char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
107   char *BOut = Buffer;
108   for (char *BIn = Buffer; BIn != EndBuffer; ) {
109     if (BIn[0] == '\\') {
110       if (BIn < EndBuffer-1 && BIn[1] == '\\') {
111         *BOut++ = '\\'; // Two \ becomes one
112         BIn += 2;
113       } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
114         char Tmp = BIn[3]; BIn[3] = 0;      // Terminate string
115         *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
116         BIn[3] = Tmp;                       // Restore character
117         BIn += 3;                           // Skip over handled chars
118         ++BOut;
119       } else {
120         *BOut++ = *BIn++;
121       }
122     } else {
123       *BOut++ = *BIn++;
124     }
125   }
126   Str.resize(BOut-Buffer);
127 }
128
129 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
130 static bool isLabelChar(char C) {
131   return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
132 }
133
134
135 /// isLabelTail - Return true if this pointer points to a valid end of a label.
136 static const char *isLabelTail(const char *CurPtr) {
137   while (1) {
138     if (CurPtr[0] == ':') return CurPtr+1;
139     if (!isLabelChar(CurPtr[0])) return 0;
140     ++CurPtr;
141   }
142 }
143
144
145
146 //===----------------------------------------------------------------------===//
147 // Lexer definition.
148 //===----------------------------------------------------------------------===//
149
150 // FIXME: REMOVE THIS.
151 #define YYEOF 0
152 #define YYERROR -2
153
154 LLLexer::LLLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
155   CurPtr = CurBuf->getBufferStart();
156 }
157
158 std::string LLLexer::getFilename() const {
159   return CurBuf->getBufferIdentifier();
160 }
161
162 int LLLexer::getNextChar() {
163   char CurChar = *CurPtr++;
164   switch (CurChar) {
165   default: return (unsigned char)CurChar;
166   case 0:
167     // A nul character in the stream is either the end of the current buffer or
168     // a random nul in the file.  Disambiguate that here.
169     if (CurPtr-1 != CurBuf->getBufferEnd())
170       return 0;  // Just whitespace.
171         
172     // Otherwise, return end of file.
173     --CurPtr;  // Another call to lex will return EOF again.  
174     return EOF;
175   case '\n':
176   case '\r':
177     // Handle the newline character by ignoring it and incrementing the line
178     // count.  However, be careful about 'dos style' files with \n\r in them.
179     // Only treat a \n\r or \r\n as a single line.
180     if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
181         *CurPtr != CurChar)
182       ++CurPtr;  // Eat the two char newline sequence.
183       
184     ++CurLineNo;
185     return '\n';
186   }  
187 }
188
189
190 int LLLexer::LexToken() {
191   TokStart = CurPtr;
192   
193   int CurChar = getNextChar();
194   
195   switch (CurChar) {
196   default:
197     // Handle letters: [a-zA-Z_]
198     if (isalpha(CurChar) || CurChar == '_')
199       return LexIdentifier();
200     
201     return CurChar;
202   case EOF: return YYEOF;
203   case 0:
204   case ' ':
205   case '\t':
206   case '\n':
207   case '\r':
208     // Ignore whitespace.
209     return LexToken();
210   case '+': return LexPositive();
211   case '@': return LexAt();
212   case '%': return LexPercent();
213   case '"': return LexQuote();
214   case '.':
215     if (const char *Ptr = isLabelTail(CurPtr)) {
216       CurPtr = Ptr;
217       llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
218       return LABELSTR;
219     }
220     if (CurPtr[0] == '.' && CurPtr[1] == '.') {
221       CurPtr += 2;
222       return DOTDOTDOT;
223     }
224     return '.';
225   case '$':
226     if (const char *Ptr = isLabelTail(CurPtr)) {
227       CurPtr = Ptr;
228       llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
229       return LABELSTR;
230     }
231     return '$';
232   case ';':
233     SkipLineComment();
234     return LexToken();
235   case '0': case '1': case '2': case '3': case '4':
236   case '5': case '6': case '7': case '8': case '9':
237   case '-': 
238     return LexDigitOrNegative();
239   }
240 }
241
242 void LLLexer::SkipLineComment() {
243   while (1) {
244     if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
245       return;
246   }
247 }
248
249 /// LexAt - Lex all tokens that start with an @ character:
250 ///   AtStringConstant @\"[^\"]*\"
251 ///   GlobalVarName    @[-a-zA-Z$._][-a-zA-Z$._0-9]*
252 ///   GlobalVarID      @[0-9]+
253 int LLLexer::LexAt() {
254   // Handle AtStringConstant: @\"[^\"]*\"
255   if (CurPtr[0] == '"') {
256     ++CurPtr;
257     
258     while (1) {
259       int CurChar = getNextChar();
260     
261       if (CurChar == EOF) { 
262         GenerateError("End of file in global variable name");
263         return YYERROR;
264       }
265       if (CurChar == '"') {
266         llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
267         UnEscapeLexed(*llvmAsmlval.StrVal);
268         return ATSTRINGCONSTANT;
269       }
270     }
271   }
272   
273   // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
274   if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || 
275       CurPtr[0] == '.' || CurPtr[0] == '_') {
276     ++CurPtr;
277     while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || 
278            CurPtr[0] == '.' || CurPtr[0] == '_')
279       ++CurPtr;
280
281     llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr);   // Skip @
282     return GLOBALVAR;
283   }
284   
285   // Handle GlobalVarID: @[0-9]+
286   if (isdigit(CurPtr[0])) {
287     for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr);
288     
289     uint64_t Val = atoull(TokStart+1, CurPtr);
290     if ((unsigned)Val != Val)
291       GenerateError("Invalid value number (too large)!");
292     llvmAsmlval.UIntVal = unsigned(Val);
293     return GLOBALVAL_ID;
294   }
295   
296   return '@';
297 }
298
299
300 /// LexPercent - Lex all tokens that start with a % character:
301 ///   PctStringConstant  %\"[^\"]*\"
302 ///   LocalVarName       %[-a-zA-Z$._][-a-zA-Z$._0-9]*
303 ///   LocalVarID         %[0-9]+
304 int LLLexer::LexPercent() {
305   // Handle PctStringConstant: %\"[^\"]*\"
306   if (CurPtr[0] == '"') {
307     ++CurPtr;
308     
309     while (1) {
310       int CurChar = getNextChar();
311       
312       if (CurChar == EOF) { 
313         GenerateError("End of file in local variable name");
314         return YYERROR;
315       }
316       if (CurChar == '"') {
317         llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
318         UnEscapeLexed(*llvmAsmlval.StrVal);
319         return PCTSTRINGCONSTANT;
320       }
321     }
322   }
323   
324   // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
325   if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || 
326       CurPtr[0] == '.' || CurPtr[0] == '_') {
327     ++CurPtr;
328     while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || 
329            CurPtr[0] == '.' || CurPtr[0] == '_')
330       ++CurPtr;
331     
332     llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr);   // Skip %
333     return LOCALVAR;
334   }
335   
336   // Handle LocalVarID: %[0-9]+
337   if (isdigit(CurPtr[0])) {
338     for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr);
339     
340     uint64_t Val = atoull(TokStart+1, CurPtr);
341     if ((unsigned)Val != Val)
342       GenerateError("Invalid value number (too large)!");
343     llvmAsmlval.UIntVal = unsigned(Val);
344     return LOCALVAL_ID;
345   }
346   
347   return '%';
348 }
349
350 /// LexQuote - Lex all tokens that start with a " character:
351 ///   QuoteLabel        "[^"]+":
352 ///   StringConstant    "[^"]*"
353 int LLLexer::LexQuote() {
354   while (1) {
355     int CurChar = getNextChar();
356     
357     if (CurChar == EOF) { 
358       GenerateError("End of file in quoted string");
359       return YYERROR;
360     }
361     
362     if (CurChar != '"') continue;
363
364     if (CurPtr[0] != ':') {
365       llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-1);
366       UnEscapeLexed(*llvmAsmlval.StrVal);
367       return STRINGCONSTANT;
368     }
369     
370     ++CurPtr;
371     llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-2);
372     UnEscapeLexed(*llvmAsmlval.StrVal);
373     return LABELSTR;
374   }
375 }
376
377 static bool JustWhitespaceNewLine(const char *&Ptr) {
378   const char *ThisPtr = Ptr;
379   while (*ThisPtr == ' ' || *ThisPtr == '\t')
380     ++ThisPtr;
381   if (*ThisPtr == '\n' || *ThisPtr == '\r') {
382     Ptr = ThisPtr;
383     return true;
384   }
385   return false;
386 }
387
388
389 /// LexIdentifier: Handle several related productions:
390 ///    Label           [-a-zA-Z$._0-9]+:
391 ///    IntegerType     i[0-9]+
392 ///    Keyword         sdiv, float, ...
393 ///    HexIntConstant  [us]0x[0-9A-Fa-f]+
394 int LLLexer::LexIdentifier() {
395   const char *StartChar = CurPtr;
396   const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
397   const char *KeywordEnd = 0;
398   
399   for (; isLabelChar(*CurPtr); ++CurPtr) {
400     // If we decide this is an integer, remember the end of the sequence.
401     if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
402     if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
403   }
404   
405   // If we stopped due to a colon, this really is a label.
406   if (*CurPtr == ':') {
407     llvmAsmlval.StrVal = new std::string(StartChar-1, CurPtr++);
408     return LABELSTR;
409   }
410   
411   // Otherwise, this wasn't a label.  If this was valid as an integer type,
412   // return it.
413   if (IntEnd == 0) IntEnd = CurPtr;
414   if (IntEnd != StartChar) {
415     CurPtr = IntEnd;
416     uint64_t NumBits = atoull(StartChar, CurPtr);
417     if (NumBits < IntegerType::MIN_INT_BITS || 
418         NumBits > IntegerType::MAX_INT_BITS) {
419       GenerateError("Bitwidth for integer type out of range!");
420       return YYERROR;
421     }
422     const Type* Ty = IntegerType::get(NumBits);
423     llvmAsmlval.PrimType = Ty;
424     return INTTYPE;
425   }
426   
427   // Otherwise, this was a letter sequence.  See which keyword this is.
428   if (KeywordEnd == 0) KeywordEnd = CurPtr;
429   CurPtr = KeywordEnd;
430   --StartChar;
431   unsigned Len = CurPtr-StartChar;
432 #define KEYWORD(STR, TOK) \
433   if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) return TOK;
434
435   KEYWORD("begin",     BEGINTOK);
436   KEYWORD("end",       ENDTOK);
437   KEYWORD("true",      TRUETOK);
438   KEYWORD("false",     FALSETOK);
439   KEYWORD("declare",   DECLARE);
440   KEYWORD("define",    DEFINE);
441   KEYWORD("global",    GLOBAL);
442   KEYWORD("constant",  CONSTANT);
443   
444   KEYWORD("internal",  INTERNAL);
445   KEYWORD("linkonce",  LINKONCE);
446   KEYWORD("weak",      WEAK);
447   KEYWORD("appending", APPENDING);
448   KEYWORD("dllimport", DLLIMPORT);
449   KEYWORD("dllexport", DLLEXPORT);
450   KEYWORD("hidden", HIDDEN);
451   KEYWORD("protected", PROTECTED);
452   KEYWORD("extern_weak", EXTERN_WEAK);
453   KEYWORD("external", EXTERNAL);
454   KEYWORD("thread_local", THREAD_LOCAL);
455   KEYWORD("zeroinitializer", ZEROINITIALIZER);
456   KEYWORD("undef", UNDEF);
457   KEYWORD("null", NULL_TOK);
458   KEYWORD("to", TO);
459   KEYWORD("tail", TAIL);
460   KEYWORD("target", TARGET);
461   KEYWORD("triple", TRIPLE);
462   KEYWORD("deplibs", DEPLIBS);
463   KEYWORD("datalayout", DATALAYOUT);
464   KEYWORD("volatile", VOLATILE);
465   KEYWORD("align", ALIGN);
466   KEYWORD("section", SECTION);
467   KEYWORD("alias", ALIAS);
468   KEYWORD("module", MODULE);
469   KEYWORD("asm", ASM_TOK);
470   KEYWORD("sideeffect", SIDEEFFECT);
471   
472   KEYWORD("cc", CC_TOK);
473   KEYWORD("ccc", CCC_TOK);
474   KEYWORD("fastcc", FASTCC_TOK);
475   KEYWORD("coldcc", COLDCC_TOK);
476   KEYWORD("x86_stdcallcc", X86_STDCALLCC_TOK);
477   KEYWORD("x86_fastcallcc", X86_FASTCALLCC_TOK);
478   
479   KEYWORD("signext", SIGNEXT);
480   KEYWORD("zeroext", ZEROEXT);
481   KEYWORD("inreg", INREG);
482   KEYWORD("sret", SRET);
483   KEYWORD("nounwind", NOUNWIND);
484   KEYWORD("noreturn", NORETURN);
485   KEYWORD("noalias", NOALIAS);
486   KEYWORD("byval", BYVAL);
487   KEYWORD("nest", NEST);
488   KEYWORD("readnone", READNONE);
489   KEYWORD("readonly", READONLY);
490   
491   KEYWORD("type", TYPE);
492   KEYWORD("opaque", OPAQUE);
493
494   KEYWORD("eq" , EQ);
495   KEYWORD("ne" , NE);
496   KEYWORD("slt", SLT);
497   KEYWORD("sgt", SGT);
498   KEYWORD("sle", SLE);
499   KEYWORD("sge", SGE);
500   KEYWORD("ult", ULT);
501   KEYWORD("ugt", UGT);
502   KEYWORD("ule", ULE);
503   KEYWORD("uge", UGE);
504   KEYWORD("oeq", OEQ);
505   KEYWORD("one", ONE);
506   KEYWORD("olt", OLT);
507   KEYWORD("ogt", OGT);
508   KEYWORD("ole", OLE);
509   KEYWORD("oge", OGE);
510   KEYWORD("ord", ORD);
511   KEYWORD("uno", UNO);
512   KEYWORD("ueq", UEQ);
513   KEYWORD("une", UNE);
514 #undef KEYWORD
515
516   // Keywords for types.
517 #define TYPEKEYWORD(STR, LLVMTY, TOK) \
518   if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
519     llvmAsmlval.PrimType = LLVMTY; return TOK; }
520   TYPEKEYWORD("void",      Type::VoidTy,  VOID);
521   TYPEKEYWORD("float",     Type::FloatTy, FLOAT);
522   TYPEKEYWORD("double",    Type::DoubleTy, DOUBLE);
523   TYPEKEYWORD("x86_fp80",  Type::X86_FP80Ty, X86_FP80);
524   TYPEKEYWORD("fp128",     Type::FP128Ty, FP128);
525   TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty, PPC_FP128);
526   TYPEKEYWORD("label",     Type::LabelTy, LABEL);
527 #undef TYPEKEYWORD
528
529   // Handle special forms for autoupgrading.  Drop these in LLVM 3.0.  This is
530   // to avoid conflicting with the sext/zext instructions, below.
531   if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
532     // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
533     if (JustWhitespaceNewLine(CurPtr))
534       return SIGNEXT;
535   } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
536     // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
537     if (JustWhitespaceNewLine(CurPtr))
538       return ZEROEXT;
539   }
540   
541   // Keywords for instructions.
542 #define INSTKEYWORD(STR, type, Enum, TOK) \
543   if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
544     llvmAsmlval.type = Instruction::Enum; return TOK; }
545
546   INSTKEYWORD("add",     BinaryOpVal, Add, ADD);
547   INSTKEYWORD("sub",     BinaryOpVal, Sub, SUB);
548   INSTKEYWORD("mul",     BinaryOpVal, Mul, MUL);
549   INSTKEYWORD("udiv",    BinaryOpVal, UDiv, UDIV);
550   INSTKEYWORD("sdiv",    BinaryOpVal, SDiv, SDIV);
551   INSTKEYWORD("fdiv",    BinaryOpVal, FDiv, FDIV);
552   INSTKEYWORD("urem",    BinaryOpVal, URem, UREM);
553   INSTKEYWORD("srem",    BinaryOpVal, SRem, SREM);
554   INSTKEYWORD("frem",    BinaryOpVal, FRem, FREM);
555   INSTKEYWORD("shl",     BinaryOpVal, Shl, SHL);
556   INSTKEYWORD("lshr",    BinaryOpVal, LShr, LSHR);
557   INSTKEYWORD("ashr",    BinaryOpVal, AShr, ASHR);
558   INSTKEYWORD("and",     BinaryOpVal, And, AND);
559   INSTKEYWORD("or",      BinaryOpVal, Or , OR );
560   INSTKEYWORD("xor",     BinaryOpVal, Xor, XOR);
561   INSTKEYWORD("icmp",    OtherOpVal,  ICmp,  ICMP);
562   INSTKEYWORD("fcmp",    OtherOpVal,  FCmp,  FCMP);
563
564   INSTKEYWORD("phi",         OtherOpVal, PHI, PHI_TOK);
565   INSTKEYWORD("call",        OtherOpVal, Call, CALL);
566   INSTKEYWORD("trunc",       CastOpVal, Trunc, TRUNC);
567   INSTKEYWORD("zext",        CastOpVal, ZExt, ZEXT);
568   INSTKEYWORD("sext",        CastOpVal, SExt, SEXT);
569   INSTKEYWORD("fptrunc",     CastOpVal, FPTrunc, FPTRUNC);
570   INSTKEYWORD("fpext",       CastOpVal, FPExt, FPEXT);
571   INSTKEYWORD("uitofp",      CastOpVal, UIToFP, UITOFP);
572   INSTKEYWORD("sitofp",      CastOpVal, SIToFP, SITOFP);
573   INSTKEYWORD("fptoui",      CastOpVal, FPToUI, FPTOUI);
574   INSTKEYWORD("fptosi",      CastOpVal, FPToSI, FPTOSI);
575   INSTKEYWORD("inttoptr",    CastOpVal, IntToPtr, INTTOPTR);
576   INSTKEYWORD("ptrtoint",    CastOpVal, PtrToInt, PTRTOINT);
577   INSTKEYWORD("bitcast",     CastOpVal, BitCast, BITCAST);
578   INSTKEYWORD("select",      OtherOpVal, Select, SELECT);
579   INSTKEYWORD("va_arg",      OtherOpVal, VAArg , VAARG);
580   INSTKEYWORD("ret",         TermOpVal, Ret, RET);
581   INSTKEYWORD("br",          TermOpVal, Br, BR);
582   INSTKEYWORD("switch",      TermOpVal, Switch, SWITCH);
583   INSTKEYWORD("invoke",      TermOpVal, Invoke, INVOKE);
584   INSTKEYWORD("unwind",      TermOpVal, Unwind, UNWIND);
585   INSTKEYWORD("unreachable", TermOpVal, Unreachable, UNREACHABLE);
586
587   INSTKEYWORD("malloc",      MemOpVal, Malloc, MALLOC);
588   INSTKEYWORD("alloca",      MemOpVal, Alloca, ALLOCA);
589   INSTKEYWORD("free",        MemOpVal, Free, FREE);
590   INSTKEYWORD("load",        MemOpVal, Load, LOAD);
591   INSTKEYWORD("store",       MemOpVal, Store, STORE);
592   INSTKEYWORD("getelementptr", MemOpVal, GetElementPtr, GETELEMENTPTR);
593
594   INSTKEYWORD("extractelement", OtherOpVal, ExtractElement, EXTRACTELEMENT);
595   INSTKEYWORD("insertelement", OtherOpVal, InsertElement, INSERTELEMENT);
596   INSTKEYWORD("shufflevector", OtherOpVal, ShuffleVector, SHUFFLEVECTOR);
597 #undef INSTKEYWORD 
598   
599   // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
600   // the CFE to avoid forcing it to deal with 64-bit numbers.
601   if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
602       TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
603     int len = CurPtr-TokStart-3;
604     uint32_t bits = len * 4;
605     APInt Tmp(bits, TokStart+3, len, 16);
606     uint32_t activeBits = Tmp.getActiveBits();
607     if (activeBits > 0 && activeBits < bits)
608       Tmp.trunc(activeBits);
609     if (Tmp.getBitWidth() > 64) {
610       llvmAsmlval.APIntVal = new APInt(Tmp);
611       return TokStart[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
612     } else if (TokStart[0] == 's') {
613       llvmAsmlval.SInt64Val = Tmp.getSExtValue();
614       return ESINT64VAL;
615     } else {
616       llvmAsmlval.UInt64Val = Tmp.getZExtValue();
617       return EUINT64VAL;
618     }
619   }
620   
621   // If this is "cc1234", return this as just "cc".
622   if (TokStart[0] == 'c' && TokStart[1] == 'c') {
623     CurPtr = TokStart+2;
624     return CC_TOK;
625   }
626   
627   // If this starts with "call", return it as CALL.  This is to support old
628   // broken .ll files.  FIXME: remove this with LLVM 3.0.
629   if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
630     CurPtr = TokStart+4;
631     llvmAsmlval.OtherOpVal = Instruction::Call;
632     return CALL;
633   }
634   
635   // Finally, if this isn't known, return just a single character.
636   CurPtr = TokStart+1;
637   return TokStart[0];
638 }
639
640
641 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
642 /// that this is not a label:
643 ///    HexFPConstant     0x[0-9A-Fa-f]+
644 ///    HexFP80Constant   0xK[0-9A-Fa-f]+
645 ///    HexFP128Constant  0xL[0-9A-Fa-f]+
646 ///    HexPPC128Constant 0xM[0-9A-Fa-f]+
647 int LLLexer::Lex0x() {
648   CurPtr = TokStart + 2;
649   
650   char Kind;
651   if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
652     Kind = *CurPtr++;
653   } else {
654     Kind = 'J';
655   }
656
657   if (!isxdigit(CurPtr[0])) {
658     // Bad token, return it as just zero.
659     CurPtr = TokStart+1;
660     return '0';
661   }
662   
663   while (isxdigit(CurPtr[0]))
664     ++CurPtr;
665   
666   if (Kind == 'J') {
667     // HexFPConstant - Floating point constant represented in IEEE format as a
668     // hexadecimal number for when exponential notation is not precise enough.
669     // Float and double only.
670     llvmAsmlval.FPVal = new APFloat(HexToFP(TokStart+2, CurPtr)); 
671     return FPVAL;
672   }
673   
674   uint64_t Pair[2];
675   HexToIntPair(TokStart+3, CurPtr, Pair);
676   switch (Kind) {
677   default: assert(0 && "Unknown kind!");
678   case 'K':
679     // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
680     llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
681     return FPVAL;
682   case 'L':
683     // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
684     llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair), true);
685     return FPVAL;
686   case 'M':
687     // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
688     llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
689     return FPVAL;
690   }
691 }
692
693 /// LexIdentifier: Handle several related productions:
694 ///    Label             [-a-zA-Z$._0-9]+:
695 ///    NInteger          -[0-9]+
696 ///    FPConstant        [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
697 ///    PInteger          [0-9]+
698 ///    HexFPConstant     0x[0-9A-Fa-f]+
699 ///    HexFP80Constant   0xK[0-9A-Fa-f]+
700 ///    HexFP128Constant  0xL[0-9A-Fa-f]+
701 ///    HexPPC128Constant 0xM[0-9A-Fa-f]+
702 int LLLexer::LexDigitOrNegative() {
703   // If the letter after the negative is a number, this is probably a label.
704   if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
705     // Okay, this is not a number after the -, it's probably a label.
706     if (const char *End = isLabelTail(CurPtr)) {
707       llvmAsmlval.StrVal = new std::string(TokStart, End-1);
708       CurPtr = End;
709       return LABELSTR;
710     }
711     
712     return CurPtr[-1];
713   }
714   
715   // At this point, it is either a label, int or fp constant.
716   
717   // Skip digits, we have at least one.
718   for (; isdigit(CurPtr[0]); ++CurPtr);
719     
720   // Check to see if this really is a label afterall, e.g. "-1:".
721   if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
722     if (const char *End = isLabelTail(CurPtr)) {
723       llvmAsmlval.StrVal = new std::string(TokStart, End-1);
724       CurPtr = End;
725       return LABELSTR;
726     }
727   }
728     
729   // If the next character is a '.', then it is a fp value, otherwise its
730   // integer.
731   if (CurPtr[0] != '.') {
732     if (TokStart[0] == '0' && TokStart[1] == 'x')
733       return Lex0x();
734     unsigned Len = CurPtr-TokStart;
735     uint32_t numBits = ((Len * 64) / 19) + 2;
736     APInt Tmp(numBits, TokStart, Len, 10);
737     if (TokStart[0] == '-') {
738       uint32_t minBits = Tmp.getMinSignedBits();
739       if (minBits > 0 && minBits < numBits)
740         Tmp.trunc(minBits);
741       if (Tmp.getBitWidth() > 64) {
742         llvmAsmlval.APIntVal = new APInt(Tmp);
743         return ESAPINTVAL;
744       } else {
745         llvmAsmlval.SInt64Val = Tmp.getSExtValue();
746         return ESINT64VAL;
747       }
748     } else {
749       uint32_t activeBits = Tmp.getActiveBits();
750       if (activeBits > 0 && activeBits < numBits)
751         Tmp.trunc(activeBits);
752       if (Tmp.getBitWidth() > 64) {
753         llvmAsmlval.APIntVal = new APInt(Tmp);
754         return EUAPINTVAL; 
755       } else {
756         llvmAsmlval.UInt64Val = Tmp.getZExtValue();
757         return EUINT64VAL;
758       }
759     }
760   }
761
762   ++CurPtr;
763   
764   // Skip over [0-9]*([eE][-+]?[0-9]+)?
765   while (isdigit(CurPtr[0])) ++CurPtr;
766   
767   if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
768     if (isdigit(CurPtr[1]) || 
769         ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
770       CurPtr += 2;
771       while (isdigit(CurPtr[0])) ++CurPtr;
772     }
773   }
774   
775   llvmAsmlval.FPVal = new APFloat(atof(TokStart));
776   return FPVAL; 
777 }
778
779 ///    FPConstant  [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
780 int LLLexer::LexPositive() {
781   // If the letter after the negative is a number, this is probably not a
782   // label.
783   if (!isdigit(CurPtr[0]))
784     return CurPtr[-1];
785   
786   // Skip digits.
787   for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr);
788
789   // At this point, we need a '.'.
790   if (CurPtr[0] != '.') {
791     CurPtr = TokStart+1;
792     return TokStart[0];
793   }
794   
795   ++CurPtr;
796   
797   // Skip over [0-9]*([eE][-+]?[0-9]+)?
798   while (isdigit(CurPtr[0])) ++CurPtr;
799   
800   if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
801     if (isdigit(CurPtr[1]) || 
802         ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
803       CurPtr += 2;
804       while (isdigit(CurPtr[0])) ++CurPtr;
805     }
806   }
807   
808   llvmAsmlval.FPVal = new APFloat(atof(TokStart));
809   return FPVAL; 
810 }
811
812
813 //===----------------------------------------------------------------------===//
814 // Define the interface to this file.
815 //===----------------------------------------------------------------------===//
816
817 static LLLexer *TheLexer;
818
819 void InitLLLexer(llvm::MemoryBuffer *MB) {
820   assert(TheLexer == 0 && "LL Lexer isn't reentrant yet");
821   TheLexer = new LLLexer(MB);
822 }
823
824 int llvmAsmlex() {
825   return TheLexer->LexToken();
826 }
827 const char *LLLgetTokenStart() { return TheLexer->getTokStart(); }
828 unsigned LLLgetTokenLength() { return TheLexer->getTokLength(); }
829 std::string LLLgetFilename() { return TheLexer->getFilename(); }
830 unsigned LLLgetLineNo() { return TheLexer->getLineNo(); }
831
832 void FreeLexer() {
833   delete TheLexer;
834   TheLexer = 0;
835 }