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