Regenerate configure.
[oota-llvm.git] / tools / llvm-mc / AsmLexer.cpp
index f871d065c4366dc4f318100ace57f3274d07701c..27454ee922cca33940163d2552aa86493b470d20 100644 (file)
@@ -12,7 +12,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "AsmLexer.h"
-#include "llvm/ADT/StringSet.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Config/config.h"  // for strtoull.
 #include <cstdlib>
 using namespace llvm;
 
-static StringSet<> &getSS(void *TheSS) {
-  return *(StringSet<>*)TheSS;
-}
-
-AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
+AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM)  {
   CurBuffer = 0;
   CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
   CurPtr = CurBuf->getBufferStart();
   TokStart = 0;
-  
-  TheStringSet = new StringSet<>();
 }
 
 AsmLexer::~AsmLexer() {
-  delete &getSS(TheStringSet);
 }
 
 SMLoc AsmLexer::getLoc() const {
   return SMLoc::getFromPointer(TokStart);
 }
 
-void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg) const {
-  SrcMgr.PrintMessage(Loc, Msg);
+void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg, 
+                            const char *Type) const {
+  SrcMgr.PrintMessage(Loc, Msg, Type);
 }
 
 /// ReturnError - Set the error to the specified string at the specified
-/// location.  This is defined to always return asmtok::Error.
-asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
-  SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg);
-  return asmtok::Error;
+/// location.  This is defined to always return AsmToken::Error.
+AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
+  SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
+  return AsmToken(AsmToken::Error, StringRef(Loc, 0));
+}
+
+/// EnterIncludeFile - Enter the specified file.  This prints an error and
+/// returns true on failure.
+bool AsmLexer::EnterIncludeFile(const std::string &Filename) {
+  int NewBuf = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
+  if (NewBuf == -1)
+    return true;
+  
+  // Save the line number and lex buffer of the includer.
+  CurBuffer = NewBuf;
+  CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
+  CurPtr = CurBuf->getBufferStart();
+  return false;
 }
 
+
 int AsmLexer::getNextChar() {
   char CurChar = *CurPtr++;
   switch (CurChar) {
@@ -71,6 +79,10 @@ int AsmLexer::getNextChar() {
       CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
       CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
       CurPtr = ParentIncludeLoc.getPointer();
+      
+      // Reset the token start pointer to the start of the new file.
+      TokStart = CurPtr;
+      
       return getNextChar();
     }
     
@@ -82,35 +94,32 @@ int AsmLexer::getNextChar() {
 }
 
 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
-asmtok::TokKind AsmLexer::LexIdentifier() {
+AsmToken AsmLexer::LexIdentifier() {
   while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
          *CurPtr == '.' || *CurPtr == '@')
     ++CurPtr;
-  // Unique string.
-  CurStrVal =
-    getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData();
-  return asmtok::Identifier;
+  return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
 }
 
 /// LexPercent: Register: %[a-zA-Z0-9]+
-asmtok::TokKind AsmLexer::LexPercent() {
+AsmToken AsmLexer::LexPercent() {
   if (!isalnum(*CurPtr))
-    return asmtok::Percent;  // Single %.
+    return AsmToken(AsmToken::Percent, StringRef(CurPtr, 1));  // Single %.
   
   while (isalnum(*CurPtr))
     ++CurPtr;
   
-  // Unique string.
-  CurStrVal =
-    getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData();
-  return asmtok::Register;
+  return AsmToken(AsmToken::Register, StringRef(TokStart, CurPtr - TokStart));
 }
 
 /// LexSlash: Slash: /
 ///           C-Style Comment: /* ... */
-asmtok::TokKind AsmLexer::LexSlash() {
-  if (*CurPtr != '*')
-    return asmtok::Slash;
+AsmToken AsmLexer::LexSlash() {
+  switch (*CurPtr) {
+  case '*': break; // C style comment.
+  case '/': return ++CurPtr, LexLineComment();
+  default:  return AsmToken(AsmToken::Slash, StringRef(CurPtr, 1));
+  }
 
   // C Style comment.
   ++CurPtr;  // skip the star.
@@ -129,15 +138,18 @@ asmtok::TokKind AsmLexer::LexSlash() {
   }
 }
 
-/// LexHash: Comment: #[^\n]*
-asmtok::TokKind AsmLexer::LexHash() {
+/// LexLineComment: Comment: #[^\n]*
+///                        : //[^\n]*
+AsmToken AsmLexer::LexLineComment() {
+  // FIXME: This is broken if we happen to a comment at the end of a file, which
+  // was .included, and which doesn't end with a newline.
   int CurChar = getNextChar();
   while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
     CurChar = getNextChar();
   
   if (CurChar == EOF)
-    return asmtok::Eof;
-  return asmtok::EndOfStatement;
+    return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
+  return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
 }
 
 
@@ -149,7 +161,7 @@ asmtok::TokKind AsmLexer::LexHash() {
 ///   Hex integer: 0x[0-9a-fA-F]+
 ///   Decimal integer: [1-9][0-9]*
 /// TODO: FP literal.
-asmtok::TokKind AsmLexer::LexDigit() {
+AsmToken AsmLexer::LexDigit() {
   if (*CurPtr == ':')
     return ReturnError(TokStart, "FIXME: local label not implemented");
   if (*CurPtr == 'f' || *CurPtr == 'b')
@@ -159,8 +171,8 @@ asmtok::TokKind AsmLexer::LexDigit() {
   if (CurPtr[-1] != '0') {
     while (isdigit(*CurPtr))
       ++CurPtr;
-    CurIntVal = strtoll(TokStart, 0, 10);
-    return asmtok::IntVal;
+    return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart), 
+                    strtoll(TokStart, 0, 10));
   }
   
   if (*CurPtr == 'b') {
@@ -172,8 +184,8 @@ asmtok::TokKind AsmLexer::LexDigit() {
     // Requires at least one binary digit.
     if (CurPtr == NumStart)
       return ReturnError(CurPtr-2, "Invalid binary number");
-    CurIntVal = strtoll(NumStart, 0, 2);
-    return asmtok::IntVal;
+    return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
+                    strtoll(NumStart, 0, 2));
   }
  
   if (*CurPtr == 'x') {
@@ -187,29 +199,28 @@ asmtok::TokKind AsmLexer::LexDigit() {
       return ReturnError(CurPtr-2, "Invalid hexadecimal number");
     
     errno = 0;
-    CurIntVal = strtoll(NumStart, 0, 16);
     if (errno == EINVAL)
       return ReturnError(CurPtr-2, "Invalid hexadecimal number");
     if (errno == ERANGE) {
       errno = 0;
-      CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
       if (errno == EINVAL)
         return ReturnError(CurPtr-2, "Invalid hexadecimal number");
       if (errno == ERANGE)
         return ReturnError(CurPtr-2, "Hexadecimal number out of range");
     }
-    return asmtok::IntVal;
+    return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
+                    (int64_t) strtoull(NumStart, 0, 16));
   }
   
   // Must be an octal number, it starts with 0.
   while (*CurPtr >= '0' && *CurPtr <= '7')
     ++CurPtr;
-  CurIntVal = strtoll(TokStart, 0, 8);
-  return asmtok::IntVal;
+  return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
+                  strtoll(TokStart, 0, 8));
 }
 
 /// LexQuote: String: "..."
-asmtok::TokKind AsmLexer::LexQuote() {
+AsmToken AsmLexer::LexQuote() {
   int CurChar = getNextChar();
   // TODO: does gas allow multiline string constants?
   while (CurChar != '"') {
@@ -224,14 +235,22 @@ asmtok::TokKind AsmLexer::LexQuote() {
     CurChar = getNextChar();
   }
   
-  // Unique string, include quotes for now.
-  CurStrVal =
-    getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData();
-  return asmtok::String;
+  return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
 }
 
+StringRef AsmLexer::LexUntilEndOfStatement() {
+  TokStart = CurPtr;
+
+  while (*CurPtr != '#' &&  // Start of line comment.
+         *CurPtr != ';' &&  // End of statement marker.
+         *CurPtr != '\n' &&
+         *CurPtr != '\r' &&
+         (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd()))
+    ++CurPtr;
+  return StringRef(TokStart, CurPtr-TokStart);
+}
 
-asmtok::TokKind AsmLexer::LexToken() {
+AsmToken AsmLexer::LexToken() {
   TokStart = CurPtr;
   // This always consumes at least one character.
   int CurChar = getNextChar();
@@ -244,7 +263,7 @@ asmtok::TokKind AsmLexer::LexToken() {
     
     // Unknown character, emit an error.
     return ReturnError(TokStart, "invalid character in input");
-  case EOF: return asmtok::Eof;
+  case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
   case 0:
   case ' ':
   case '\t':
@@ -252,52 +271,57 @@ asmtok::TokKind AsmLexer::LexToken() {
     return LexToken();
   case '\n': // FALL THROUGH.
   case '\r': // FALL THROUGH.
-  case ';': return asmtok::EndOfStatement;
-  case ':': return asmtok::Colon;
-  case '+': return asmtok::Plus;
-  case '-': return asmtok::Minus;
-  case '~': return asmtok::Tilde;
-  case '(': return asmtok::LParen;
-  case ')': return asmtok::RParen;
-  case '*': return asmtok::Star;
-  case ',': return asmtok::Comma;
-  case '$': return asmtok::Dollar;
+  case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
+  case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
+  case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
+  case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
+  case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
+  case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
+  case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
+  case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
+  case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
+  case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
   case '=': 
     if (*CurPtr == '=')
-      return ++CurPtr, asmtok::EqualEqual;
-    return asmtok::Equal;
+      return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
+    return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
   case '|': 
     if (*CurPtr == '|')
-      return ++CurPtr, asmtok::PipePipe;
-    return asmtok::Pipe;
-  case '^': return asmtok::Caret;
+      return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
+    return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
+  case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
   case '&': 
     if (*CurPtr == '&')
-      return ++CurPtr, asmtok::AmpAmp;
-    return asmtok::Amp;
+      return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
+    return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
   case '!': 
     if (*CurPtr == '=')
-      return ++CurPtr, asmtok::ExclaimEqual;
-    return asmtok::Exclaim;
+      return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
+    return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
   case '%': return LexPercent();
   case '/': return LexSlash();
-  case '#': return LexHash();
+  case '#': return LexLineComment();
   case '"': return LexQuote();
   case '0': case '1': case '2': case '3': case '4':
   case '5': case '6': case '7': case '8': case '9':
     return LexDigit();
   case '<':
     switch (*CurPtr) {
-    case '<': return ++CurPtr, asmtok::LessLess;
-    case '=': return ++CurPtr, asmtok::LessEqual;
-    case '>': return ++CurPtr, asmtok::LessGreater;
-    default: return asmtok::Less;
+    case '<': return ++CurPtr, AsmToken(AsmToken::LessLess, 
+                                        StringRef(TokStart, 2));
+    case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual, 
+                                        StringRef(TokStart, 2));
+    case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater, 
+                                        StringRef(TokStart, 2));
+    default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
     }
   case '>':
     switch (*CurPtr) {
-    case '>': return ++CurPtr, asmtok::GreaterGreater;      
-    case '=': return ++CurPtr, asmtok::GreaterEqual;      
-    default: return asmtok::Greater;
+    case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater, 
+                                        StringRef(TokStart, 2));
+    case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual, 
+                                        StringRef(TokStart, 2));
+    default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
     }
       
   // TODO: Quoted identifiers (objc methods etc)