JSONBackend.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //===- JSONBackend.cpp - Generate a JSON dump of all records. -*- C++ -*-=====//
  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. // This TableGen back end generates a machine-readable representation
  11. // of all the classes and records defined by the input, in JSON format.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/BitVector.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/TableGen/Error.h"
  17. #include "llvm/TableGen/Record.h"
  18. #include "llvm/TableGen/TableGenBackend.h"
  19. #include "llvm/Support/JSON.h"
  20. #define DEBUG_TYPE "json-emitter"
  21. using namespace llvm;
  22. namespace {
  23. class JSONEmitter {
  24. private:
  25. RecordKeeper &Records;
  26. json::Value translateInit(const Init &I);
  27. json::Array listSuperclasses(const Record &R);
  28. public:
  29. JSONEmitter(RecordKeeper &R);
  30. void run(raw_ostream &OS);
  31. };
  32. } // end anonymous namespace
  33. JSONEmitter::JSONEmitter(RecordKeeper &R) : Records(R) {}
  34. json::Value JSONEmitter::translateInit(const Init &I) {
  35. // Init subclasses that we return as JSON primitive values of one
  36. // kind or another.
  37. if (isa<UnsetInit>(&I)) {
  38. return nullptr;
  39. } else if (auto *Bit = dyn_cast<BitInit>(&I)) {
  40. return Bit->getValue() ? 1 : 0;
  41. } else if (auto *Bits = dyn_cast<BitsInit>(&I)) {
  42. json::Array array;
  43. for (unsigned i = 0, limit = Bits->getNumBits(); i < limit; i++)
  44. array.push_back(translateInit(*Bits->getBit(i)));
  45. return array;
  46. } else if (auto *Int = dyn_cast<IntInit>(&I)) {
  47. return Int->getValue();
  48. } else if (auto *Str = dyn_cast<StringInit>(&I)) {
  49. return Str->getValue();
  50. } else if (auto *Code = dyn_cast<CodeInit>(&I)) {
  51. return Code->getValue();
  52. } else if (auto *List = dyn_cast<ListInit>(&I)) {
  53. json::Array array;
  54. for (auto val : *List)
  55. array.push_back(translateInit(*val));
  56. return array;
  57. }
  58. // Init subclasses that we return as JSON objects containing a
  59. // 'kind' discriminator. For these, we also provide the same
  60. // translation back into TableGen input syntax that -print-records
  61. // would give.
  62. json::Object obj;
  63. obj["printable"] = I.getAsString();
  64. if (auto *Def = dyn_cast<DefInit>(&I)) {
  65. obj["kind"] = "def";
  66. obj["def"] = Def->getDef()->getName();
  67. return obj;
  68. } else if (auto *Var = dyn_cast<VarInit>(&I)) {
  69. obj["kind"] = "var";
  70. obj["var"] = Var->getName();
  71. return obj;
  72. } else if (auto *VarBit = dyn_cast<VarBitInit>(&I)) {
  73. if (auto *Var = dyn_cast<VarInit>(VarBit->getBitVar())) {
  74. obj["kind"] = "varbit";
  75. obj["var"] = Var->getName();
  76. obj["index"] = VarBit->getBitNum();
  77. return obj;
  78. }
  79. } else if (auto *Dag = dyn_cast<DagInit>(&I)) {
  80. obj["kind"] = "dag";
  81. obj["operator"] = translateInit(*Dag->getOperator());
  82. if (auto name = Dag->getName())
  83. obj["name"] = name->getAsUnquotedString();
  84. json::Array args;
  85. for (unsigned i = 0, limit = Dag->getNumArgs(); i < limit; ++i) {
  86. json::Array arg;
  87. arg.push_back(translateInit(*Dag->getArg(i)));
  88. if (auto argname = Dag->getArgName(i))
  89. arg.push_back(argname->getAsUnquotedString());
  90. else
  91. arg.push_back(nullptr);
  92. args.push_back(std::move(arg));
  93. }
  94. obj["args"] = std::move(args);
  95. return obj;
  96. }
  97. // Final fallback: anything that gets past here is simply given a
  98. // kind field of 'complex', and the only other field is the standard
  99. // 'printable' representation.
  100. assert(!I.isConcrete());
  101. obj["kind"] = "complex";
  102. return obj;
  103. }
  104. void JSONEmitter::run(raw_ostream &OS) {
  105. json::Object root;
  106. root["!tablegen_json_version"] = 1;
  107. // Prepare the arrays that will list the instances of every class.
  108. // We mostly fill those in by iterating over the superclasses of
  109. // each def, but we also want to ensure we store an empty list for a
  110. // class with no instances at all, so we do a preliminary iteration
  111. // over the classes, invoking std::map::operator[] to default-
  112. // construct the array for each one.
  113. std::map<std::string, json::Array> instance_lists;
  114. for (const auto &C : Records.getClasses()) {
  115. auto &Name = C.second->getNameInitAsString();
  116. (void)instance_lists[Name];
  117. }
  118. // Main iteration over the defs.
  119. for (const auto &D : Records.getDefs()) {
  120. auto &Name = D.second->getNameInitAsString();
  121. auto &Def = *D.second;
  122. json::Object obj;
  123. json::Array fields;
  124. for (const RecordVal &RV : Def.getValues()) {
  125. if (!Def.isTemplateArg(RV.getNameInit())) {
  126. auto Name = RV.getNameInitAsString();
  127. if (RV.getPrefix())
  128. fields.push_back(Name);
  129. obj[Name] = translateInit(*RV.getValue());
  130. }
  131. }
  132. obj["!fields"] = std::move(fields);
  133. json::Array superclasses;
  134. for (const auto &SuperPair : Def.getSuperClasses())
  135. superclasses.push_back(SuperPair.first->getNameInitAsString());
  136. obj["!superclasses"] = std::move(superclasses);
  137. obj["!name"] = Name;
  138. obj["!anonymous"] = Def.isAnonymous();
  139. root[Name] = std::move(obj);
  140. // Add this def to the instance list for each of its superclasses.
  141. for (const auto &SuperPair : Def.getSuperClasses()) {
  142. auto SuperName = SuperPair.first->getNameInitAsString();
  143. instance_lists[SuperName].push_back(Name);
  144. }
  145. }
  146. // Make a JSON object from the std::map of instance lists.
  147. json::Object instanceof;
  148. for (auto kv: instance_lists)
  149. instanceof[kv.first] = std::move(kv.second);
  150. root["!instanceof"] = std::move(instanceof);
  151. // Done. Write the output.
  152. OS << json::Value(std::move(root)) << "\n";
  153. }
  154. namespace llvm {
  155. void EmitJSON(RecordKeeper &RK, raw_ostream &OS) { JSONEmitter(RK).run(OS); }
  156. } // end namespace llvm