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
This commit is contained in:
Kevin Enderby 2009-09-04 21:45:34 +00:00
parent 68f195cc50
commit 9823ca971d
5 changed files with 49 additions and 26 deletions

View File

@ -41,7 +41,7 @@ struct AsmToken {
Star, Comma, Dollar, Equal, EqualEqual, Star, Comma, Dollar, Equal, EqualEqual,
Pipe, PipePipe, Caret, Pipe, PipePipe, Caret,
Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
Less, LessEqual, LessLess, LessGreater, Less, LessEqual, LessLess, LessGreater,
Greater, GreaterEqual, GreaterGreater Greater, GreaterEqual, GreaterGreater
}; };

View File

@ -15,12 +15,14 @@
#include "llvm/Support/SourceMgr.h" #include "llvm/Support/SourceMgr.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Config/config.h" // for strtoull. #include "llvm/Config/config.h" // for strtoull.
#include "llvm/MC/MCAsmInfo.h"
#include <cerrno> #include <cerrno>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
using namespace llvm; using namespace llvm;
AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) { AsmLexer::AsmLexer(SourceMgr &SM, const MCAsmInfo &_MAI) : SrcMgr(SM),
MAI(_MAI) {
CurBuffer = 0; CurBuffer = 0;
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer); CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
CurPtr = CurBuf->getBufferStart(); CurPtr = CurBuf->getBufferStart();
@ -230,12 +232,16 @@ AsmToken AsmLexer::LexQuote() {
StringRef AsmLexer::LexUntilEndOfStatement() { StringRef AsmLexer::LexUntilEndOfStatement() {
TokStart = CurPtr; TokStart = CurPtr;
while (*CurPtr != '#' && // Start of line comment. while (*CurPtr != ';' && // End of statement marker.
*CurPtr != ';' && // End of statement marker.
*CurPtr != '\n' && *CurPtr != '\n' &&
*CurPtr != '\r' && *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; ++CurPtr;
}
return StringRef(TokStart, CurPtr-TokStart); return StringRef(TokStart, CurPtr-TokStart);
} }
@ -244,6 +250,10 @@ AsmToken AsmLexer::LexToken() {
// This always consumes at least one character. // This always consumes at least one character.
int CurChar = getNextChar(); int CurChar = getNextChar();
for (const char *p = MAI.getCommentString(); *p != 0; ++p)
if (CurChar == *p)
return LexLineComment();
switch (CurChar) { switch (CurChar) {
default: default:
// Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]* // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
@ -289,7 +299,7 @@ AsmToken AsmLexer::LexToken() {
return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1)); return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1)); case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
case '/': return LexSlash(); case '/': return LexSlash();
case '#': return LexLineComment(); case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
case '"': return LexQuote(); case '"': return LexQuote();
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': case '5': case '6': case '7': case '8': case '9':

View File

@ -16,6 +16,7 @@
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/MC/MCAsmLexer.h" #include "llvm/MC/MCAsmLexer.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include <string> #include <string>
#include <cassert> #include <cassert>
@ -24,10 +25,12 @@ namespace llvm {
class MemoryBuffer; class MemoryBuffer;
class SourceMgr; class SourceMgr;
class SMLoc; class SMLoc;
class MCAsmInfo;
/// AsmLexer - Lexer class for assembly files. /// AsmLexer - Lexer class for assembly files.
class AsmLexer : public MCAsmLexer { class AsmLexer : public MCAsmLexer {
SourceMgr &SrcMgr; SourceMgr &SrcMgr;
const MCAsmInfo MAI;
const char *CurPtr; const char *CurPtr;
const MemoryBuffer *CurBuf; const MemoryBuffer *CurBuf;
@ -46,7 +49,7 @@ protected:
virtual AsmToken LexToken(); virtual AsmToken LexToken();
public: public:
AsmLexer(SourceMgr &SrcMgr); AsmLexer(SourceMgr &SrcMgr, const MCAsmInfo &MAI);
~AsmLexer(); ~AsmLexer();
SMLoc getLoc() const; SMLoc getLoc() const;

View File

@ -20,6 +20,7 @@
#include "llvm/MC/MCAsmParser.h" #include "llvm/MC/MCAsmParser.h"
#include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCAsmInfo.h"
namespace llvm { namespace llvm {
class AsmCond; class AsmCond;
@ -27,6 +28,7 @@ class MCContext;
class MCExpr; class MCExpr;
class MCInst; class MCInst;
class MCStreamer; class MCStreamer;
class MCAsmInfo;
class MCValue; class MCValue;
class TargetAsmParser; class TargetAsmParser;
class Twine; class Twine;
@ -46,8 +48,9 @@ private:
mutable void *SectionUniquingMap; mutable void *SectionUniquingMap;
public: public:
AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out) AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
: Lexer(_SM), Ctx(_Ctx), Out(_Out), TargetParser(0), const MCAsmInfo &_MAI)
: Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
SectionUniquingMap(0) {} SectionUniquingMap(0) {}
~AsmParser(); ~AsmParser();

View File

@ -82,6 +82,18 @@ Action(cl::desc("Action to perform:"),
"Assemble a .s file (default)"), "Assemble a .s file (default)"),
clEnumValEnd)); 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) { static int AsLexInput(const char *ProgName) {
std::string ErrorMessage; std::string ErrorMessage;
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
@ -104,7 +116,14 @@ static int AsLexInput(const char *ProgName) {
// it later. // it later.
SrcMgr.setIncludeDirs(IncludeDirs); 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; bool Error = false;
@ -162,18 +181,6 @@ static int AsLexInput(const char *ProgName) {
return Error; 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() { static formatted_raw_ostream *GetOutputStream() {
if (OutputFilename == "") if (OutputFilename == "")
OutputFilename = "-"; OutputFilename = "-";
@ -239,10 +246,10 @@ static int AssembleInput(const char *ProgName) {
OwningPtr<MCCodeEmitter> CE; OwningPtr<MCCodeEmitter> CE;
OwningPtr<MCStreamer> Str; OwningPtr<MCStreamer> Str;
if (FileType == OFT_AssemblyFile) {
const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName); const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName);
assert(MAI && "Unable to create target asm info!"); assert(MAI && "Unable to create target asm info!");
if (FileType == OFT_AssemblyFile) {
AP.reset(TheTarget->createAsmPrinter(*Out, *TM, MAI, true)); AP.reset(TheTarget->createAsmPrinter(*Out, *TM, MAI, true));
if (ShowEncoding) if (ShowEncoding)
CE.reset(TheTarget->createCodeEmitter(*TM)); CE.reset(TheTarget->createCodeEmitter(*TM));
@ -253,7 +260,7 @@ static int AssembleInput(const char *ProgName) {
Str.reset(createMachOStreamer(Ctx, *Out, CE.get())); Str.reset(createMachOStreamer(Ctx, *Out, CE.get()));
} }
AsmParser Parser(SrcMgr, Ctx, *Str.get()); AsmParser Parser(SrcMgr, Ctx, *Str.get(), *MAI);
OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(Parser)); OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(Parser));
if (!TAP) { if (!TAP) {
errs() << ProgName errs() << ProgName