diff options
author | rdivacky <rdivacky@FreeBSD.org> | 2009-10-14 17:57:32 +0000 |
---|---|---|
committer | rdivacky <rdivacky@FreeBSD.org> | 2009-10-14 17:57:32 +0000 |
commit | cd749a9c07f1de2fb8affde90537efa4bc3e7c54 (patch) | |
tree | b21f6de4e08b89bb7931806bab798fc2a5e3a686 /tools/llvm-mc/AsmLexer.cpp | |
parent | 72621d11de5b873f1695f391eb95f0b336c3d2d4 (diff) | |
download | FreeBSD-src-cd749a9c07f1de2fb8affde90537efa4bc3e7c54.zip FreeBSD-src-cd749a9c07f1de2fb8affde90537efa4bc3e7c54.tar.gz |
Update llvm to r84119.
Diffstat (limited to 'tools/llvm-mc/AsmLexer.cpp')
-rw-r--r-- | tools/llvm-mc/AsmLexer.cpp | 185 |
1 files changed, 104 insertions, 81 deletions
diff --git a/tools/llvm-mc/AsmLexer.cpp b/tools/llvm-mc/AsmLexer.cpp index 7b744fb..99055c6 100644 --- a/tools/llvm-mc/AsmLexer.cpp +++ b/tools/llvm-mc/AsmLexer.cpp @@ -12,30 +12,24 @@ //===----------------------------------------------------------------------===// #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 "llvm/MC/MCAsmInfo.h" #include <cerrno> #include <cstdio> #include <cstdlib> using namespace llvm; -static StringSet<> &getSS(void *TheSS) { - return *(StringSet<>*)TheSS; -} - -AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) { +AsmLexer::AsmLexer(SourceMgr &SM, const MCAsmInfo &_MAI) : SrcMgr(SM), + MAI(_MAI) { CurBuffer = 0; CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); CurPtr = CurBuf->getBufferStart(); TokStart = 0; - - TheStringSet = new StringSet<>(); } AsmLexer::~AsmLexer() { - delete &getSS(TheStringSet); } SMLoc AsmLexer::getLoc() const { @@ -48,12 +42,27 @@ void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg, } /// 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) { +/// 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 asmtok::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) { @@ -72,6 +81,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(); } @@ -83,37 +96,20 @@ 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; -} - -/// LexPercent: Register: %[a-zA-Z0-9]+ -asmtok::TokKind AsmLexer::LexPercent() { - if (!isalnum(*CurPtr)) - return asmtok::Percent; // Single %. - - while (isalnum(*CurPtr)) - ++CurPtr; - - // Unique string. - CurStrVal = - getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData(); - return asmtok::Register; + return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart)); } /// LexSlash: Slash: / /// C-Style Comment: /* ... */ -asmtok::TokKind AsmLexer::LexSlash() { +AsmToken AsmLexer::LexSlash() { switch (*CurPtr) { case '*': break; // C style comment. case '/': return ++CurPtr, LexLineComment(); - default: return asmtok::Slash; + default: return AsmToken(AsmToken::Slash, StringRef(CurPtr, 1)); } // C Style comment. @@ -135,14 +131,16 @@ asmtok::TokKind AsmLexer::LexSlash() { /// LexLineComment: Comment: #[^\n]* /// : //[^\n]* -asmtok::TokKind AsmLexer::LexLineComment() { +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)); } @@ -154,7 +152,7 @@ asmtok::TokKind AsmLexer::LexLineComment() { /// 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') @@ -164,8 +162,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') { @@ -177,8 +175,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') { @@ -192,29 +190,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 != '"') { @@ -229,18 +226,35 @@ 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 (!isAtStartOfComment(*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); } +bool AsmLexer::isAtStartOfComment(char Char) { + // FIXME: This won't work for multi-character comment indicators like "//". + return Char == *MAI.getCommentString(); +} -asmtok::TokKind AsmLexer::LexToken() { +AsmToken AsmLexer::LexToken() { TokStart = CurPtr; // This always consumes at least one character. int CurChar = getNextChar(); + if (isAtStartOfComment(CurChar)) + return LexLineComment(); + switch (CurChar) { default: // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]* @@ -249,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': @@ -257,52 +271,61 @@ 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::LBrac, StringRef(TokStart, 1)); + case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1)); + case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1)); + case '}': return AsmToken(AsmToken::RCurly, 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; - case '%': return LexPercent(); + return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2)); + return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1)); + case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1)); case '/': return LexSlash(); - case '#': return LexLineComment(); + case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1)); 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) |