llvm-mcmarkup.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //===-- llvm-mcmarkup.cpp - Parse the MC assembly markup tags -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Example simple parser implementation for the MC assembly markup language.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/CommandLine.h"
  14. #include "llvm/Support/ManagedStatic.h"
  15. #include "llvm/Support/MemoryBuffer.h"
  16. #include "llvm/Support/PrettyStackTrace.h"
  17. #include "llvm/Support/Signals.h"
  18. #include "llvm/Support/SourceMgr.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. static cl::list<std::string>
  22. InputFilenames(cl::Positional, cl::desc("<input files>"),
  23. cl::ZeroOrMore);
  24. static cl::opt<bool>
  25. DumpTags("dump-tags", cl::desc("List all tags encountered in input"));
  26. static StringRef ToolName;
  27. /// Trivial lexer for the markup parser. Input is always handled a character
  28. /// at a time. The lexer just encapsulates EOF and lookahead handling.
  29. class MarkupLexer {
  30. StringRef::const_iterator Start;
  31. StringRef::const_iterator CurPtr;
  32. StringRef::const_iterator End;
  33. public:
  34. MarkupLexer(StringRef Source)
  35. : Start(Source.begin()), CurPtr(Source.begin()), End(Source.end()) {}
  36. // When processing non-markup, input is consumed a character at a time.
  37. bool isEOF() { return CurPtr == End; }
  38. int getNextChar() {
  39. if (CurPtr == End) return EOF;
  40. return *CurPtr++;
  41. }
  42. int peekNextChar() {
  43. if (CurPtr == End) return EOF;
  44. return *CurPtr;
  45. }
  46. StringRef::const_iterator getPosition() const { return CurPtr; }
  47. };
  48. /// A markup tag is a name and a (usually empty) list of modifiers.
  49. class MarkupTag {
  50. StringRef Name;
  51. StringRef Modifiers;
  52. SMLoc StartLoc;
  53. public:
  54. MarkupTag(StringRef n, StringRef m, SMLoc Loc)
  55. : Name(n), Modifiers(m), StartLoc(Loc) {}
  56. StringRef getName() const { return Name; }
  57. StringRef getModifiers() const { return Modifiers; }
  58. SMLoc getLoc() const { return StartLoc; }
  59. };
  60. /// A simple parser implementation for creating MarkupTags from input text.
  61. class MarkupParser {
  62. MarkupLexer &Lex;
  63. SourceMgr &SM;
  64. public:
  65. MarkupParser(MarkupLexer &lex, SourceMgr &SrcMgr) : Lex(lex), SM(SrcMgr) {}
  66. /// Create a MarkupTag from the current position in the MarkupLexer.
  67. /// The parseTag() method should be called when the lexer has processed
  68. /// the opening '<' character. Input will be consumed up to and including
  69. /// the ':' which terminates the tag open.
  70. MarkupTag parseTag();
  71. /// Issue a diagnostic and terminate program execution.
  72. void FatalError(SMLoc Loc, StringRef Msg);
  73. };
  74. void MarkupParser::FatalError(SMLoc Loc, StringRef Msg) {
  75. SM.PrintMessage(Loc, SourceMgr::DK_Error, Msg);
  76. exit(1);
  77. }
  78. // Example handler for when a tag is recognized.
  79. static void processStartTag(MarkupTag &Tag) {
  80. // If we're just printing the tags, do that, otherwise do some simple
  81. // colorization.
  82. if (DumpTags) {
  83. outs() << Tag.getName();
  84. if (Tag.getModifiers().size())
  85. outs() << " " << Tag.getModifiers();
  86. outs() << "\n";
  87. return;
  88. }
  89. if (!outs().has_colors())
  90. return;
  91. // Color registers as red and immediates as cyan. Those don't have nested
  92. // tags, so don't bother keeping a stack of colors to reset to.
  93. if (Tag.getName() == "reg")
  94. outs().changeColor(raw_ostream::RED);
  95. else if (Tag.getName() == "imm")
  96. outs().changeColor(raw_ostream::CYAN);
  97. }
  98. // Example handler for when the end of a tag is recognized.
  99. static void processEndTag(MarkupTag &Tag) {
  100. // If we're printing the tags, there's nothing more to do here. Otherwise,
  101. // set the color back the normal.
  102. if (DumpTags)
  103. return;
  104. if (!outs().has_colors())
  105. return;
  106. // Just reset to basic white.
  107. outs().changeColor(raw_ostream::WHITE, false);
  108. }
  109. MarkupTag MarkupParser::parseTag() {
  110. // First off, extract the tag into it's own StringRef so we can look at it
  111. // outside of the context of consuming input.
  112. StringRef::const_iterator Start = Lex.getPosition();
  113. SMLoc Loc = SMLoc::getFromPointer(Start - 1);
  114. while(Lex.getNextChar() != ':') {
  115. // EOF is an error.
  116. if (Lex.isEOF())
  117. FatalError(SMLoc::getFromPointer(Start), "unterminated markup tag");
  118. }
  119. StringRef RawTag(Start, Lex.getPosition() - Start - 1);
  120. std::pair<StringRef, StringRef> SplitTag = RawTag.split(' ');
  121. return MarkupTag(SplitTag.first, SplitTag.second, Loc);
  122. }
  123. static void parseMCMarkup(StringRef Filename) {
  124. ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
  125. MemoryBuffer::getFileOrSTDIN(Filename);
  126. if (std::error_code EC = BufferPtr.getError()) {
  127. errs() << ToolName << ": " << EC.message() << '\n';
  128. return;
  129. }
  130. std::unique_ptr<MemoryBuffer> &Buffer = BufferPtr.get();
  131. SourceMgr SrcMgr;
  132. StringRef InputSource = Buffer->getBuffer();
  133. // Tell SrcMgr about this buffer, which is what the parser will pick up.
  134. SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
  135. MarkupLexer Lex(InputSource);
  136. MarkupParser Parser(Lex, SrcMgr);
  137. SmallVector<MarkupTag, 4> TagStack;
  138. for (int CurChar = Lex.getNextChar();
  139. CurChar != EOF;
  140. CurChar = Lex.getNextChar()) {
  141. switch (CurChar) {
  142. case '<': {
  143. // A "<<" is output as a literal '<' and does not start a markup tag.
  144. if (Lex.peekNextChar() == '<') {
  145. (void)Lex.getNextChar();
  146. break;
  147. }
  148. // Parse the markup entry.
  149. TagStack.push_back(Parser.parseTag());
  150. // Do any special handling for the start of a tag.
  151. processStartTag(TagStack.back());
  152. continue;
  153. }
  154. case '>': {
  155. SMLoc Loc = SMLoc::getFromPointer(Lex.getPosition() - 1);
  156. // A ">>" is output as a literal '>' and does not end a markup tag.
  157. if (Lex.peekNextChar() == '>') {
  158. (void)Lex.getNextChar();
  159. break;
  160. }
  161. // Close out the innermost tag.
  162. if (TagStack.empty())
  163. Parser.FatalError(Loc, "'>' without matching '<'");
  164. // Do any special handling for the end of a tag.
  165. processEndTag(TagStack.back());
  166. TagStack.pop_back();
  167. continue;
  168. }
  169. default:
  170. break;
  171. }
  172. // For anything else, just echo the character back out.
  173. if (!DumpTags && CurChar != EOF)
  174. outs() << (char)CurChar;
  175. }
  176. // If there are any unterminated markup tags, issue diagnostics for them.
  177. while (!TagStack.empty()) {
  178. MarkupTag &Tag = TagStack.back();
  179. SrcMgr.PrintMessage(Tag.getLoc(), SourceMgr::DK_Error,
  180. "unterminated markup tag");
  181. TagStack.pop_back();
  182. }
  183. }
  184. int main(int argc, char **argv) {
  185. // Print a stack trace if we signal out.
  186. sys::PrintStackTraceOnErrorSignal(argv[0]);
  187. PrettyStackTraceProgram X(argc, argv);
  188. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  189. cl::ParseCommandLineOptions(argc, argv, "llvm MC markup parser\n");
  190. ToolName = argv[0];
  191. // If no input files specified, read from stdin.
  192. if (InputFilenames.size() == 0)
  193. InputFilenames.push_back("-");
  194. llvm::for_each(InputFilenames, parseMCMarkup);
  195. return 0;
  196. }