2
0

llvm-mcmarkup.cpp 7.2 KB

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