From 9823ca971d5cb475401e59fde244caf5087c74a1 Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Fri, 4 Sep 2009 21:45:34 +0000 Subject: [PATCH] Added the AsmToken::Hash enum constant to MCAsmLexer.h in preparation of supporting other targets. Changed the code to pass MCAsmInfo to the parser and the lexer. Then changed the lexer to use CommentString from MCAsmInfo instead of a literal '#' character. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81046 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCAsmLexer.h | 2 +- tools/llvm-mc/AsmLexer.cpp | 20 +++++++++++++----- tools/llvm-mc/AsmLexer.h | 5 ++++- tools/llvm-mc/AsmParser.h | 7 ++++-- tools/llvm-mc/llvm-mc.cpp | 41 +++++++++++++++++++++--------------- 5 files changed, 49 insertions(+), 26 deletions(-) diff --git a/include/llvm/MC/MCAsmLexer.h b/include/llvm/MC/MCAsmLexer.h index a1d67b1861a..2f7383dc2c3 100644 --- a/include/llvm/MC/MCAsmLexer.h +++ b/include/llvm/MC/MCAsmLexer.h @@ -41,7 +41,7 @@ struct AsmToken { Star, Comma, Dollar, Equal, EqualEqual, Pipe, PipePipe, Caret, - Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, + Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash, Less, LessEqual, LessLess, LessGreater, Greater, GreaterEqual, GreaterGreater }; diff --git a/tools/llvm-mc/AsmLexer.cpp b/tools/llvm-mc/AsmLexer.cpp index 4dafa0eae94..f6be8864aa1 100644 --- a/tools/llvm-mc/AsmLexer.cpp +++ b/tools/llvm-mc/AsmLexer.cpp @@ -15,12 +15,14 @@ #include "llvm/Support/SourceMgr.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Config/config.h" // for strtoull. +#include "llvm/MC/MCAsmInfo.h" #include #include #include using namespace llvm; -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(); @@ -230,12 +232,16 @@ AsmToken AsmLexer::LexQuote() { StringRef AsmLexer::LexUntilEndOfStatement() { TokStart = CurPtr; - while (*CurPtr != '#' && // Start of line comment. - *CurPtr != ';' && // End of statement marker. + while (*CurPtr != ';' && // End of statement marker. *CurPtr != '\n' && *CurPtr != '\r' && - (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) + (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) { + // check for start of line comment. + for (const char *p = MAI.getCommentString(); *p != 0; ++p) + if (*CurPtr == *p) + break; ++CurPtr; + } return StringRef(TokStart, CurPtr-TokStart); } @@ -244,6 +250,10 @@ AsmToken AsmLexer::LexToken() { // This always consumes at least one character. int CurChar = getNextChar(); + for (const char *p = MAI.getCommentString(); *p != 0; ++p) + if (CurChar == *p) + return LexLineComment(); + switch (CurChar) { default: // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]* @@ -289,7 +299,7 @@ AsmToken AsmLexer::LexToken() { 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': diff --git a/tools/llvm-mc/AsmLexer.h b/tools/llvm-mc/AsmLexer.h index 000df59c88d..32b0c5f5fb4 100644 --- a/tools/llvm-mc/AsmLexer.h +++ b/tools/llvm-mc/AsmLexer.h @@ -16,6 +16,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/MC/MCAsmLexer.h" +#include "llvm/MC/MCAsmInfo.h" #include "llvm/Support/DataTypes.h" #include #include @@ -24,10 +25,12 @@ namespace llvm { class MemoryBuffer; class SourceMgr; class SMLoc; +class MCAsmInfo; /// AsmLexer - Lexer class for assembly files. class AsmLexer : public MCAsmLexer { SourceMgr &SrcMgr; + const MCAsmInfo MAI; const char *CurPtr; const MemoryBuffer *CurBuf; @@ -46,7 +49,7 @@ protected: virtual AsmToken LexToken(); public: - AsmLexer(SourceMgr &SrcMgr); + AsmLexer(SourceMgr &SrcMgr, const MCAsmInfo &MAI); ~AsmLexer(); SMLoc getLoc() const; diff --git a/tools/llvm-mc/AsmParser.h b/tools/llvm-mc/AsmParser.h index 7471a901a9b..7f038c90ff0 100644 --- a/tools/llvm-mc/AsmParser.h +++ b/tools/llvm-mc/AsmParser.h @@ -20,6 +20,7 @@ #include "llvm/MC/MCAsmParser.h" #include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCStreamer.h" +#include "llvm/MC/MCAsmInfo.h" namespace llvm { class AsmCond; @@ -27,6 +28,7 @@ class MCContext; class MCExpr; class MCInst; class MCStreamer; +class MCAsmInfo; class MCValue; class TargetAsmParser; class Twine; @@ -46,8 +48,9 @@ private: mutable void *SectionUniquingMap; public: - AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out) - : Lexer(_SM), Ctx(_Ctx), Out(_Out), TargetParser(0), + AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out, + const MCAsmInfo &_MAI) + : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0), SectionUniquingMap(0) {} ~AsmParser(); diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp index 84131c7ca66..bf3c01794bc 100644 --- a/tools/llvm-mc/llvm-mc.cpp +++ b/tools/llvm-mc/llvm-mc.cpp @@ -82,6 +82,18 @@ Action(cl::desc("Action to perform:"), "Assemble a .s file (default)"), clEnumValEnd)); +static const Target *GetTarget(const char *ProgName) { + // Get the target specific parser. + std::string Error; + const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); + if (TheTarget) + return TheTarget; + + errs() << ProgName << ": error: unable to get target for '" << TripleName + << "', see --version and --triple.\n"; + return 0; +} + static int AsLexInput(const char *ProgName) { std::string ErrorMessage; MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, @@ -104,7 +116,14 @@ static int AsLexInput(const char *ProgName) { // it later. SrcMgr.setIncludeDirs(IncludeDirs); - AsmLexer Lexer(SrcMgr); + const Target *TheTarget = GetTarget(ProgName); + if (!TheTarget) + return 1; + + const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName); + assert(MAI && "Unable to create target asm info!"); + + AsmLexer Lexer(SrcMgr, *MAI); bool Error = false; @@ -162,18 +181,6 @@ static int AsLexInput(const char *ProgName) { return Error; } -static const Target *GetTarget(const char *ProgName) { - // Get the target specific parser. - std::string Error; - const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); - if (TheTarget) - return TheTarget; - - errs() << ProgName << ": error: unable to get target for '" << TripleName - << "', see --version and --triple.\n"; - return 0; -} - static formatted_raw_ostream *GetOutputStream() { if (OutputFilename == "") OutputFilename = "-"; @@ -239,10 +246,10 @@ static int AssembleInput(const char *ProgName) { OwningPtr CE; OwningPtr Str; - if (FileType == OFT_AssemblyFile) { - const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName); - assert(MAI && "Unable to create target asm info!"); + const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName); + assert(MAI && "Unable to create target asm info!"); + if (FileType == OFT_AssemblyFile) { AP.reset(TheTarget->createAsmPrinter(*Out, *TM, MAI, true)); if (ShowEncoding) CE.reset(TheTarget->createCodeEmitter(*TM)); @@ -253,7 +260,7 @@ static int AssembleInput(const char *ProgName) { Str.reset(createMachOStreamer(Ctx, *Out, CE.get())); } - AsmParser Parser(SrcMgr, Ctx, *Str.get()); + AsmParser Parser(SrcMgr, Ctx, *Str.get(), *MAI); OwningPtr TAP(TheTarget->createAsmParser(Parser)); if (!TAP) { errs() << ProgName