llvm.org GIT mirror llvm / 9823ca9
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 Kevin Enderby 11 years ago
5 changed file(s) with 50 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
4040 Star, Comma, Dollar, Equal, EqualEqual,
4141
4242 Pipe, PipePipe, Caret,
43 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent,
43 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
4444 Less, LessEqual, LessLess, LessGreater,
4545 Greater, GreaterEqual, GreaterGreater
4646 };
1414 #include "llvm/Support/SourceMgr.h"
1515 #include "llvm/Support/MemoryBuffer.h"
1616 #include "llvm/Config/config.h" // for strtoull.
17 #include "llvm/MC/MCAsmInfo.h"
1718 #include
1819 #include
1920 #include
2021 using namespace llvm;
2122
22 AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
23 AsmLexer::AsmLexer(SourceMgr &SM, const MCAsmInfo &_MAI) : SrcMgr(SM),
24 MAI(_MAI) {
2325 CurBuffer = 0;
2426 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
2527 CurPtr = CurBuf->getBufferStart();
229231 StringRef AsmLexer::LexUntilEndOfStatement() {
230232 TokStart = CurPtr;
231233
232 while (*CurPtr != '#' && // Start of line comment.
233 *CurPtr != ';' && // End of statement marker.
234 while (*CurPtr != ';' && // End of statement marker.
234235 *CurPtr != '\n' &&
235236 *CurPtr != '\r' &&
236 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd()))
237 ++CurPtr;
237 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
238 // check for start of line comment.
239 for (const char *p = MAI.getCommentString(); *p != 0; ++p)
240 if (*CurPtr == *p)
241 break;
242 ++CurPtr;
243 }
238244 return StringRef(TokStart, CurPtr-TokStart);
239245 }
240246
243249 // This always consumes at least one character.
244250 int CurChar = getNextChar();
245251
252 for (const char *p = MAI.getCommentString(); *p != 0; ++p)
253 if (CurChar == *p)
254 return LexLineComment();
255
246256 switch (CurChar) {
247257 default:
248258 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
288298 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
289299 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
290300 case '/': return LexSlash();
291 case '#': return LexLineComment();
301 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
292302 case '"': return LexQuote();
293303 case '0': case '1': case '2': case '3': case '4':
294304 case '5': case '6': case '7': case '8': case '9':
1515
1616 #include "llvm/ADT/StringRef.h"
1717 #include "llvm/MC/MCAsmLexer.h"
18 #include "llvm/MC/MCAsmInfo.h"
1819 #include "llvm/Support/DataTypes.h"
1920 #include
2021 #include
2324 class MemoryBuffer;
2425 class SourceMgr;
2526 class SMLoc;
27 class MCAsmInfo;
2628
2729 /// AsmLexer - Lexer class for assembly files.
2830 class AsmLexer : public MCAsmLexer {
2931 SourceMgr &SrcMgr;
32 const MCAsmInfo MAI;
3033
3134 const char *CurPtr;
3235 const MemoryBuffer *CurBuf;
4548 virtual AsmToken LexToken();
4649
4750 public:
48 AsmLexer(SourceMgr &SrcMgr);
51 AsmLexer(SourceMgr &SrcMgr, const MCAsmInfo &MAI);
4952 ~AsmLexer();
5053
5154 SMLoc getLoc() const;
1919 #include "llvm/MC/MCAsmParser.h"
2020 #include "llvm/MC/MCSectionMachO.h"
2121 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCAsmInfo.h"
2223
2324 namespace llvm {
2425 class AsmCond;
2627 class MCExpr;
2728 class MCInst;
2829 class MCStreamer;
30 class MCAsmInfo;
2931 class MCValue;
3032 class TargetAsmParser;
3133 class Twine;
4547 mutable void *SectionUniquingMap;
4648
4749 public:
48 AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out)
49 : Lexer(_SM), Ctx(_Ctx), Out(_Out), TargetParser(0),
50 AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
51 const MCAsmInfo &_MAI)
52 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
5053 SectionUniquingMap(0) {}
5154 ~AsmParser();
5255
8181 "Assemble a .s file (default)"),
8282 clEnumValEnd));
8383
84 static const Target *GetTarget(const char *ProgName) {
85 // Get the target specific parser.
86 std::string Error;
87 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
88 if (TheTarget)
89 return TheTarget;
90
91 errs() << ProgName << ": error: unable to get target for '" << TripleName
92 << "', see --version and --triple.\n";
93 return 0;
94 }
95
8496 static int AsLexInput(const char *ProgName) {
8597 std::string ErrorMessage;
8698 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
103115 // it later.
104116 SrcMgr.setIncludeDirs(IncludeDirs);
105117
106 AsmLexer Lexer(SrcMgr);
118 const Target *TheTarget = GetTarget(ProgName);
119 if (!TheTarget)
120 return 1;
121
122 const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName);
123 assert(MAI && "Unable to create target asm info!");
124
125 AsmLexer Lexer(SrcMgr, *MAI);
107126
108127 bool Error = false;
109128
161180 return Error;
162181 }
163182
164 static const Target *GetTarget(const char *ProgName) {
165 // Get the target specific parser.
166 std::string Error;
167 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
168 if (TheTarget)
169 return TheTarget;
170
171 errs() << ProgName << ": error: unable to get target for '" << TripleName
172 << "', see --version and --triple.\n";
173 return 0;
174 }
175
176183 static formatted_raw_ostream *GetOutputStream() {
177184 if (OutputFilename == "")
178185 OutputFilename = "-";
238245 OwningPtr CE;
239246 OwningPtr Str;
240247
248 const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName);
249 assert(MAI && "Unable to create target asm info!");
250
241251 if (FileType == OFT_AssemblyFile) {
242 const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName);
243 assert(MAI && "Unable to create target asm info!");
244
245252 AP.reset(TheTarget->createAsmPrinter(*Out, *TM, MAI, true));
246253 if (ShowEncoding)
247254 CE.reset(TheTarget->createCodeEmitter(*TM));
252259 Str.reset(createMachOStreamer(Ctx, *Out, CE.get()));
253260 }
254261
255 AsmParser Parser(SrcMgr, Ctx, *Str.get());
262 AsmParser Parser(SrcMgr, Ctx, *Str.get(), *MAI);
256263 OwningPtr TAP(TheTarget->createAsmParser(Parser));
257264 if (!TAP) {
258265 errs() << ProgName