Record.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. //===- Record.cpp - Record implementation ---------------------------------===//
  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. // Implement the tablegen record classes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/TableGen/Record.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/FoldingSet.h"
  16. #include "llvm/ADT/Hashing.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringExtras.h"
  20. #include "llvm/ADT/StringMap.h"
  21. #include "llvm/Support/DataTypes.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include "llvm/Support/Format.h"
  24. #include "llvm/TableGen/Error.h"
  25. using namespace llvm;
  26. //===----------------------------------------------------------------------===//
  27. // std::string wrapper for DenseMap purposes
  28. //===----------------------------------------------------------------------===//
  29. namespace llvm {
  30. /// TableGenStringKey - This is a wrapper for std::string suitable for
  31. /// using as a key to a DenseMap. Because there isn't a particularly
  32. /// good way to indicate tombstone or empty keys for strings, we want
  33. /// to wrap std::string to indicate that this is a "special" string
  34. /// not expected to take on certain values (those of the tombstone and
  35. /// empty keys). This makes things a little safer as it clarifies
  36. /// that DenseMap is really not appropriate for general strings.
  37. class TableGenStringKey {
  38. public:
  39. TableGenStringKey(const std::string &str) : data(str) {}
  40. TableGenStringKey(const char *str) : data(str) {}
  41. const std::string &str() const { return data; }
  42. friend hash_code hash_value(const TableGenStringKey &Value) {
  43. using llvm::hash_value;
  44. return hash_value(Value.str());
  45. }
  46. private:
  47. std::string data;
  48. };
  49. /// Specialize DenseMapInfo for TableGenStringKey.
  50. template<> struct DenseMapInfo<TableGenStringKey> {
  51. static inline TableGenStringKey getEmptyKey() {
  52. TableGenStringKey Empty("<<<EMPTY KEY>>>");
  53. return Empty;
  54. }
  55. static inline TableGenStringKey getTombstoneKey() {
  56. TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>");
  57. return Tombstone;
  58. }
  59. static unsigned getHashValue(const TableGenStringKey& Val) {
  60. using llvm::hash_value;
  61. return hash_value(Val);
  62. }
  63. static bool isEqual(const TableGenStringKey& LHS,
  64. const TableGenStringKey& RHS) {
  65. return LHS.str() == RHS.str();
  66. }
  67. };
  68. } // namespace llvm
  69. //===----------------------------------------------------------------------===//
  70. // Type implementations
  71. //===----------------------------------------------------------------------===//
  72. BitRecTy BitRecTy::Shared;
  73. IntRecTy IntRecTy::Shared;
  74. StringRecTy StringRecTy::Shared;
  75. DagRecTy DagRecTy::Shared;
  76. void RecTy::dump() const { print(errs()); }
  77. ListRecTy *RecTy::getListTy() {
  78. if (!ListTy)
  79. ListTy.reset(new ListRecTy(this));
  80. return ListTy.get();
  81. }
  82. bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  83. assert(RHS && "NULL pointer");
  84. return Kind == RHS->getRecTyKind();
  85. }
  86. bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
  87. if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
  88. return true;
  89. if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
  90. return BitsTy->getNumBits() == 1;
  91. return false;
  92. }
  93. BitsRecTy *BitsRecTy::get(unsigned Sz) {
  94. static std::vector<std::unique_ptr<BitsRecTy>> Shared;
  95. if (Sz >= Shared.size())
  96. Shared.resize(Sz + 1);
  97. std::unique_ptr<BitsRecTy> &Ty = Shared[Sz];
  98. if (!Ty)
  99. Ty.reset(new BitsRecTy(Sz));
  100. return Ty.get();
  101. }
  102. std::string BitsRecTy::getAsString() const {
  103. return "bits<" + utostr(Size) + ">";
  104. }
  105. bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  106. if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
  107. return cast<BitsRecTy>(RHS)->Size == Size;
  108. RecTyKind kind = RHS->getRecTyKind();
  109. return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
  110. }
  111. bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  112. RecTyKind kind = RHS->getRecTyKind();
  113. return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
  114. }
  115. std::string StringRecTy::getAsString() const {
  116. return "string";
  117. }
  118. std::string ListRecTy::getAsString() const {
  119. return "list<" + Ty->getAsString() + ">";
  120. }
  121. bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  122. if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
  123. return Ty->typeIsConvertibleTo(ListTy->getElementType());
  124. return false;
  125. }
  126. std::string DagRecTy::getAsString() const {
  127. return "dag";
  128. }
  129. RecordRecTy *RecordRecTy::get(Record *R) {
  130. return dyn_cast<RecordRecTy>(R->getDefInit()->getType());
  131. }
  132. std::string RecordRecTy::getAsString() const {
  133. return Rec->getName();
  134. }
  135. bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  136. const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
  137. if (!RTy)
  138. return false;
  139. if (RTy->getRecord() == Rec || Rec->isSubClassOf(RTy->getRecord()))
  140. return true;
  141. for (Record *SC : RTy->getRecord()->getSuperClasses())
  142. if (Rec->isSubClassOf(SC))
  143. return true;
  144. return false;
  145. }
  146. /// resolveTypes - Find a common type that T1 and T2 convert to.
  147. /// Return null if no such type exists.
  148. ///
  149. RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
  150. if (T1->typeIsConvertibleTo(T2))
  151. return T2;
  152. if (T2->typeIsConvertibleTo(T1))
  153. return T1;
  154. // If one is a Record type, check superclasses
  155. if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
  156. // See if T2 inherits from a type T1 also inherits from
  157. for (Record *SuperRec1 : RecTy1->getRecord()->getSuperClasses()) {
  158. RecordRecTy *SuperRecTy1 = RecordRecTy::get(SuperRec1);
  159. RecTy *NewType1 = resolveTypes(SuperRecTy1, T2);
  160. if (NewType1)
  161. return NewType1;
  162. }
  163. }
  164. if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2)) {
  165. // See if T1 inherits from a type T2 also inherits from
  166. for (Record *SuperRec2 : RecTy2->getRecord()->getSuperClasses()) {
  167. RecordRecTy *SuperRecTy2 = RecordRecTy::get(SuperRec2);
  168. RecTy *NewType2 = resolveTypes(T1, SuperRecTy2);
  169. if (NewType2)
  170. return NewType2;
  171. }
  172. }
  173. return nullptr;
  174. }
  175. //===----------------------------------------------------------------------===//
  176. // Initializer implementations
  177. //===----------------------------------------------------------------------===//
  178. void Init::anchor() { }
  179. void Init::dump() const { return print(errs()); }
  180. UnsetInit *UnsetInit::get() {
  181. static UnsetInit TheInit;
  182. return &TheInit;
  183. }
  184. Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
  185. if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
  186. SmallVector<Init *, 16> NewBits(BRT->getNumBits());
  187. for (unsigned i = 0; i != BRT->getNumBits(); ++i)
  188. NewBits[i] = UnsetInit::get();
  189. return BitsInit::get(NewBits);
  190. }
  191. // All other types can just be returned.
  192. return const_cast<UnsetInit *>(this);
  193. }
  194. BitInit *BitInit::get(bool V) {
  195. static BitInit True(true);
  196. static BitInit False(false);
  197. return V ? &True : &False;
  198. }
  199. Init *BitInit::convertInitializerTo(RecTy *Ty) const {
  200. if (isa<BitRecTy>(Ty))
  201. return const_cast<BitInit *>(this);
  202. if (isa<IntRecTy>(Ty))
  203. return IntInit::get(getValue());
  204. if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
  205. // Can only convert single bit.
  206. if (BRT->getNumBits() == 1)
  207. return BitsInit::get(const_cast<BitInit *>(this));
  208. }
  209. return nullptr;
  210. }
  211. static void
  212. ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
  213. ID.AddInteger(Range.size());
  214. for (Init *I : Range)
  215. ID.AddPointer(I);
  216. }
  217. BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
  218. static FoldingSet<BitsInit> ThePool;
  219. static std::vector<std::unique_ptr<BitsInit>> TheActualPool;
  220. FoldingSetNodeID ID;
  221. ProfileBitsInit(ID, Range);
  222. void *IP = nullptr;
  223. if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
  224. return I;
  225. BitsInit *I = new BitsInit(Range);
  226. ThePool.InsertNode(I, IP);
  227. TheActualPool.push_back(std::unique_ptr<BitsInit>(I));
  228. return I;
  229. }
  230. void BitsInit::Profile(FoldingSetNodeID &ID) const {
  231. ProfileBitsInit(ID, Bits);
  232. }
  233. Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
  234. if (isa<BitRecTy>(Ty)) {
  235. if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
  236. return getBit(0);
  237. }
  238. if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
  239. // If the number of bits is right, return it. Otherwise we need to expand
  240. // or truncate.
  241. if (getNumBits() != BRT->getNumBits()) return nullptr;
  242. return const_cast<BitsInit *>(this);
  243. }
  244. if (isa<IntRecTy>(Ty)) {
  245. int64_t Result = 0;
  246. for (unsigned i = 0, e = getNumBits(); i != e; ++i)
  247. if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
  248. Result |= static_cast<int64_t>(Bit->getValue()) << i;
  249. else
  250. return nullptr;
  251. return IntInit::get(Result);
  252. }
  253. return nullptr;
  254. }
  255. Init *
  256. BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
  257. SmallVector<Init *, 16> NewBits(Bits.size());
  258. for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
  259. if (Bits[i] >= getNumBits())
  260. return nullptr;
  261. NewBits[i] = getBit(Bits[i]);
  262. }
  263. return BitsInit::get(NewBits);
  264. }
  265. std::string BitsInit::getAsString() const {
  266. std::string Result = "{ ";
  267. for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
  268. if (i) Result += ", ";
  269. if (Init *Bit = getBit(e-i-1))
  270. Result += Bit->getAsString();
  271. else
  272. Result += "*";
  273. }
  274. return Result + " }";
  275. }
  276. // Fix bit initializer to preserve the behavior that bit reference from a unset
  277. // bits initializer will resolve into VarBitInit to keep the field name and bit
  278. // number used in targets with fixed insn length.
  279. static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) {
  280. if (RV || !isa<UnsetInit>(After))
  281. return After;
  282. return Before;
  283. }
  284. // resolveReferences - If there are any field references that refer to fields
  285. // that have been filled in, we can propagate the values now.
  286. //
  287. Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const {
  288. bool Changed = false;
  289. SmallVector<Init *, 16> NewBits(getNumBits());
  290. Init *CachedInit = nullptr;
  291. Init *CachedBitVar = nullptr;
  292. bool CachedBitVarChanged = false;
  293. for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
  294. Init *CurBit = Bits[i];
  295. Init *CurBitVar = CurBit->getBitVar();
  296. NewBits[i] = CurBit;
  297. if (CurBitVar == CachedBitVar) {
  298. if (CachedBitVarChanged) {
  299. Init *Bit = CachedInit->getBit(CurBit->getBitNum());
  300. NewBits[i] = fixBitInit(RV, CurBit, Bit);
  301. }
  302. continue;
  303. }
  304. CachedBitVar = CurBitVar;
  305. CachedBitVarChanged = false;
  306. Init *B;
  307. do {
  308. B = CurBitVar;
  309. CurBitVar = CurBitVar->resolveReferences(R, RV);
  310. CachedBitVarChanged |= B != CurBitVar;
  311. Changed |= B != CurBitVar;
  312. } while (B != CurBitVar);
  313. CachedInit = CurBitVar;
  314. if (CachedBitVarChanged) {
  315. Init *Bit = CurBitVar->getBit(CurBit->getBitNum());
  316. NewBits[i] = fixBitInit(RV, CurBit, Bit);
  317. }
  318. }
  319. if (Changed)
  320. return BitsInit::get(NewBits);
  321. return const_cast<BitsInit *>(this);
  322. }
  323. IntInit *IntInit::get(int64_t V) {
  324. static DenseMap<int64_t, std::unique_ptr<IntInit>> ThePool;
  325. std::unique_ptr<IntInit> &I = ThePool[V];
  326. if (!I) I.reset(new IntInit(V));
  327. return I.get();
  328. }
  329. std::string IntInit::getAsString() const {
  330. return itostr(Value);
  331. }
  332. /// canFitInBitfield - Return true if the number of bits is large enough to hold
  333. /// the integer value.
  334. static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
  335. // For example, with NumBits == 4, we permit Values from [-7 .. 15].
  336. return (NumBits >= sizeof(Value) * 8) ||
  337. (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
  338. }
  339. Init *IntInit::convertInitializerTo(RecTy *Ty) const {
  340. if (isa<IntRecTy>(Ty))
  341. return const_cast<IntInit *>(this);
  342. if (isa<BitRecTy>(Ty)) {
  343. int64_t Val = getValue();
  344. if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
  345. return BitInit::get(Val != 0);
  346. }
  347. if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
  348. int64_t Value = getValue();
  349. // Make sure this bitfield is large enough to hold the integer value.
  350. if (!canFitInBitfield(Value, BRT->getNumBits()))
  351. return nullptr;
  352. SmallVector<Init *, 16> NewBits(BRT->getNumBits());
  353. for (unsigned i = 0; i != BRT->getNumBits(); ++i)
  354. NewBits[i] = BitInit::get(Value & (1LL << i));
  355. return BitsInit::get(NewBits);
  356. }
  357. return nullptr;
  358. }
  359. Init *
  360. IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
  361. SmallVector<Init *, 16> NewBits(Bits.size());
  362. for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
  363. if (Bits[i] >= 64)
  364. return nullptr;
  365. NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
  366. }
  367. return BitsInit::get(NewBits);
  368. }
  369. StringInit *StringInit::get(StringRef V) {
  370. static StringMap<std::unique_ptr<StringInit>> ThePool;
  371. std::unique_ptr<StringInit> &I = ThePool[V];
  372. if (!I) I.reset(new StringInit(V));
  373. return I.get();
  374. }
  375. Init *StringInit::convertInitializerTo(RecTy *Ty) const {
  376. if (isa<StringRecTy>(Ty))
  377. return const_cast<StringInit *>(this);
  378. return nullptr;
  379. }
  380. static void ProfileListInit(FoldingSetNodeID &ID,
  381. ArrayRef<Init *> Range,
  382. RecTy *EltTy) {
  383. ID.AddInteger(Range.size());
  384. ID.AddPointer(EltTy);
  385. for (Init *I : Range)
  386. ID.AddPointer(I);
  387. }
  388. ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
  389. static FoldingSet<ListInit> ThePool;
  390. static std::vector<std::unique_ptr<ListInit>> TheActualPool;
  391. FoldingSetNodeID ID;
  392. ProfileListInit(ID, Range, EltTy);
  393. void *IP = nullptr;
  394. if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
  395. return I;
  396. ListInit *I = new ListInit(Range, EltTy);
  397. ThePool.InsertNode(I, IP);
  398. TheActualPool.push_back(std::unique_ptr<ListInit>(I));
  399. return I;
  400. }
  401. void ListInit::Profile(FoldingSetNodeID &ID) const {
  402. RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
  403. ProfileListInit(ID, Values, EltTy);
  404. }
  405. Init *ListInit::convertInitializerTo(RecTy *Ty) const {
  406. if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
  407. std::vector<Init*> Elements;
  408. // Verify that all of the elements of the list are subclasses of the
  409. // appropriate class!
  410. for (Init *I : getValues())
  411. if (Init *CI = I->convertInitializerTo(LRT->getElementType()))
  412. Elements.push_back(CI);
  413. else
  414. return nullptr;
  415. if (isa<ListRecTy>(getType()))
  416. return ListInit::get(Elements, Ty);
  417. }
  418. return nullptr;
  419. }
  420. Init *
  421. ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) const {
  422. std::vector<Init*> Vals;
  423. for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
  424. if (Elements[i] >= size())
  425. return nullptr;
  426. Vals.push_back(getElement(Elements[i]));
  427. }
  428. return ListInit::get(Vals, getType());
  429. }
  430. Record *ListInit::getElementAsRecord(unsigned i) const {
  431. assert(i < Values.size() && "List element index out of range!");
  432. DefInit *DI = dyn_cast<DefInit>(Values[i]);
  433. if (!DI)
  434. PrintFatalError("Expected record in list!");
  435. return DI->getDef();
  436. }
  437. Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const {
  438. std::vector<Init*> Resolved;
  439. Resolved.reserve(size());
  440. bool Changed = false;
  441. for (Init *CurElt : getValues()) {
  442. Init *E;
  443. do {
  444. E = CurElt;
  445. CurElt = CurElt->resolveReferences(R, RV);
  446. Changed |= E != CurElt;
  447. } while (E != CurElt);
  448. Resolved.push_back(E);
  449. }
  450. if (Changed)
  451. return ListInit::get(Resolved, getType());
  452. return const_cast<ListInit *>(this);
  453. }
  454. Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV,
  455. unsigned Elt) const {
  456. if (Elt >= size())
  457. return nullptr; // Out of range reference.
  458. Init *E = getElement(Elt);
  459. // If the element is set to some value, or if we are resolving a reference
  460. // to a specific variable and that variable is explicitly unset, then
  461. // replace the VarListElementInit with it.
  462. if (IRV || !isa<UnsetInit>(E))
  463. return E;
  464. return nullptr;
  465. }
  466. std::string ListInit::getAsString() const {
  467. std::string Result = "[";
  468. for (unsigned i = 0, e = Values.size(); i != e; ++i) {
  469. if (i) Result += ", ";
  470. Result += Values[i]->getAsString();
  471. }
  472. return Result + "]";
  473. }
  474. Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV,
  475. unsigned Elt) const {
  476. Init *Resolved = resolveReferences(R, IRV);
  477. OpInit *OResolved = dyn_cast<OpInit>(Resolved);
  478. if (OResolved) {
  479. Resolved = OResolved->Fold(&R, nullptr);
  480. }
  481. if (Resolved != this) {
  482. TypedInit *Typed = cast<TypedInit>(Resolved);
  483. if (Init *New = Typed->resolveListElementReference(R, IRV, Elt))
  484. return New;
  485. return VarListElementInit::get(Typed, Elt);
  486. }
  487. return nullptr;
  488. }
  489. Init *OpInit::getBit(unsigned Bit) const {
  490. if (getType() == BitRecTy::get())
  491. return const_cast<OpInit*>(this);
  492. return VarBitInit::get(const_cast<OpInit*>(this), Bit);
  493. }
  494. UnOpInit *UnOpInit::get(UnaryOp opc, Init *lhs, RecTy *Type) {
  495. typedef std::pair<std::pair<unsigned, Init *>, RecTy *> Key;
  496. static DenseMap<Key, std::unique_ptr<UnOpInit>> ThePool;
  497. Key TheKey(std::make_pair(std::make_pair(opc, lhs), Type));
  498. std::unique_ptr<UnOpInit> &I = ThePool[TheKey];
  499. if (!I) I.reset(new UnOpInit(opc, lhs, Type));
  500. return I.get();
  501. }
  502. Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
  503. switch (getOpcode()) {
  504. case CAST: {
  505. if (isa<StringRecTy>(getType())) {
  506. if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
  507. return LHSs;
  508. if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
  509. return StringInit::get(LHSd->getAsString());
  510. if (IntInit *LHSi = dyn_cast<IntInit>(LHS))
  511. return StringInit::get(LHSi->getAsString());
  512. } else {
  513. if (StringInit *LHSs = dyn_cast<StringInit>(LHS)) {
  514. std::string Name = LHSs->getValue();
  515. // From TGParser::ParseIDValue
  516. if (CurRec) {
  517. if (const RecordVal *RV = CurRec->getValue(Name)) {
  518. if (RV->getType() != getType())
  519. PrintFatalError("type mismatch in cast");
  520. return VarInit::get(Name, RV->getType());
  521. }
  522. Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name,
  523. ":");
  524. if (CurRec->isTemplateArg(TemplateArgName)) {
  525. const RecordVal *RV = CurRec->getValue(TemplateArgName);
  526. assert(RV && "Template arg doesn't exist??");
  527. if (RV->getType() != getType())
  528. PrintFatalError("type mismatch in cast");
  529. return VarInit::get(TemplateArgName, RV->getType());
  530. }
  531. }
  532. if (CurMultiClass) {
  533. Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
  534. "::");
  535. if (CurMultiClass->Rec.isTemplateArg(MCName)) {
  536. const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
  537. assert(RV && "Template arg doesn't exist??");
  538. if (RV->getType() != getType())
  539. PrintFatalError("type mismatch in cast");
  540. return VarInit::get(MCName, RV->getType());
  541. }
  542. }
  543. assert(CurRec && "NULL pointer");
  544. if (Record *D = (CurRec->getRecords()).getDef(Name))
  545. return DefInit::get(D);
  546. PrintFatalError(CurRec->getLoc(),
  547. "Undefined reference:'" + Name + "'\n");
  548. }
  549. }
  550. break;
  551. }
  552. case HEAD: {
  553. if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
  554. assert(!LHSl->empty() && "Empty list in head");
  555. return LHSl->getElement(0);
  556. }
  557. break;
  558. }
  559. case TAIL: {
  560. if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
  561. assert(!LHSl->empty() && "Empty list in tail");
  562. // Note the +1. We can't just pass the result of getValues()
  563. // directly.
  564. return ListInit::get(LHSl->getValues().slice(1), LHSl->getType());
  565. }
  566. break;
  567. }
  568. case EMPTY: {
  569. if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
  570. return IntInit::get(LHSl->empty());
  571. if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
  572. return IntInit::get(LHSs->getValue().empty());
  573. break;
  574. }
  575. }
  576. return const_cast<UnOpInit *>(this);
  577. }
  578. Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
  579. Init *lhs = LHS->resolveReferences(R, RV);
  580. if (LHS != lhs)
  581. return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, nullptr);
  582. return Fold(&R, nullptr);
  583. }
  584. std::string UnOpInit::getAsString() const {
  585. std::string Result;
  586. switch (Opc) {
  587. case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
  588. case HEAD: Result = "!head"; break;
  589. case TAIL: Result = "!tail"; break;
  590. case EMPTY: Result = "!empty"; break;
  591. }
  592. return Result + "(" + LHS->getAsString() + ")";
  593. }
  594. BinOpInit *BinOpInit::get(BinaryOp opc, Init *lhs,
  595. Init *rhs, RecTy *Type) {
  596. typedef std::pair<
  597. std::pair<std::pair<unsigned, Init *>, Init *>,
  598. RecTy *
  599. > Key;
  600. static DenseMap<Key, std::unique_ptr<BinOpInit>> ThePool;
  601. Key TheKey(std::make_pair(std::make_pair(std::make_pair(opc, lhs), rhs),
  602. Type));
  603. std::unique_ptr<BinOpInit> &I = ThePool[TheKey];
  604. if (!I) I.reset(new BinOpInit(opc, lhs, rhs, Type));
  605. return I.get();
  606. }
  607. Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
  608. switch (getOpcode()) {
  609. case CONCAT: {
  610. DagInit *LHSs = dyn_cast<DagInit>(LHS);
  611. DagInit *RHSs = dyn_cast<DagInit>(RHS);
  612. if (LHSs && RHSs) {
  613. DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
  614. DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
  615. if (!LOp || !ROp || LOp->getDef() != ROp->getDef())
  616. PrintFatalError("Concated Dag operators do not match!");
  617. std::vector<Init*> Args;
  618. std::vector<std::string> ArgNames;
  619. for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
  620. Args.push_back(LHSs->getArg(i));
  621. ArgNames.push_back(LHSs->getArgName(i));
  622. }
  623. for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
  624. Args.push_back(RHSs->getArg(i));
  625. ArgNames.push_back(RHSs->getArgName(i));
  626. }
  627. return DagInit::get(LHSs->getOperator(), "", Args, ArgNames);
  628. }
  629. break;
  630. }
  631. case LISTCONCAT: {
  632. ListInit *LHSs = dyn_cast<ListInit>(LHS);
  633. ListInit *RHSs = dyn_cast<ListInit>(RHS);
  634. if (LHSs && RHSs) {
  635. std::vector<Init *> Args;
  636. Args.insert(Args.end(), LHSs->begin(), LHSs->end());
  637. Args.insert(Args.end(), RHSs->begin(), RHSs->end());
  638. return ListInit::get(
  639. Args, cast<ListRecTy>(LHSs->getType())->getElementType());
  640. }
  641. break;
  642. }
  643. case STRCONCAT: {
  644. StringInit *LHSs = dyn_cast<StringInit>(LHS);
  645. StringInit *RHSs = dyn_cast<StringInit>(RHS);
  646. if (LHSs && RHSs)
  647. return StringInit::get(LHSs->getValue() + RHSs->getValue());
  648. break;
  649. }
  650. case EQ: {
  651. // try to fold eq comparison for 'bit' and 'int', otherwise fallback
  652. // to string objects.
  653. IntInit *L =
  654. dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
  655. IntInit *R =
  656. dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
  657. if (L && R)
  658. return IntInit::get(L->getValue() == R->getValue());
  659. StringInit *LHSs = dyn_cast<StringInit>(LHS);
  660. StringInit *RHSs = dyn_cast<StringInit>(RHS);
  661. // Make sure we've resolved
  662. if (LHSs && RHSs)
  663. return IntInit::get(LHSs->getValue() == RHSs->getValue());
  664. break;
  665. }
  666. case ADD:
  667. case AND:
  668. case SHL:
  669. case SRA:
  670. case SRL: {
  671. IntInit *LHSi =
  672. dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
  673. IntInit *RHSi =
  674. dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
  675. if (LHSi && RHSi) {
  676. int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
  677. int64_t Result;
  678. switch (getOpcode()) {
  679. default: llvm_unreachable("Bad opcode!");
  680. case ADD: Result = LHSv + RHSv; break;
  681. case AND: Result = LHSv & RHSv; break;
  682. case SHL: Result = LHSv << RHSv; break;
  683. case SRA: Result = LHSv >> RHSv; break;
  684. case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
  685. }
  686. return IntInit::get(Result);
  687. }
  688. break;
  689. }
  690. }
  691. return const_cast<BinOpInit *>(this);
  692. }
  693. Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const {
  694. Init *lhs = LHS->resolveReferences(R, RV);
  695. Init *rhs = RHS->resolveReferences(R, RV);
  696. if (LHS != lhs || RHS != rhs)
  697. return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R,nullptr);
  698. return Fold(&R, nullptr);
  699. }
  700. std::string BinOpInit::getAsString() const {
  701. std::string Result;
  702. switch (Opc) {
  703. case CONCAT: Result = "!con"; break;
  704. case ADD: Result = "!add"; break;
  705. case AND: Result = "!and"; break;
  706. case SHL: Result = "!shl"; break;
  707. case SRA: Result = "!sra"; break;
  708. case SRL: Result = "!srl"; break;
  709. case EQ: Result = "!eq"; break;
  710. case LISTCONCAT: Result = "!listconcat"; break;
  711. case STRCONCAT: Result = "!strconcat"; break;
  712. }
  713. return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
  714. }
  715. TernOpInit *TernOpInit::get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
  716. RecTy *Type) {
  717. typedef std::pair<
  718. std::pair<
  719. std::pair<std::pair<unsigned, RecTy *>, Init *>,
  720. Init *
  721. >,
  722. Init *
  723. > Key;
  724. static DenseMap<Key, std::unique_ptr<TernOpInit>> ThePool;
  725. Key TheKey(std::make_pair(std::make_pair(std::make_pair(std::make_pair(opc,
  726. Type),
  727. lhs),
  728. mhs),
  729. rhs));
  730. std::unique_ptr<TernOpInit> &I = ThePool[TheKey];
  731. if (!I) I.reset(new TernOpInit(opc, lhs, mhs, rhs, Type));
  732. return I.get();
  733. }
  734. static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
  735. Record *CurRec, MultiClass *CurMultiClass);
  736. static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg,
  737. RecTy *Type, Record *CurRec,
  738. MultiClass *CurMultiClass) {
  739. // If this is a dag, recurse
  740. if (auto *TArg = dyn_cast<TypedInit>(Arg))
  741. if (isa<DagRecTy>(TArg->getType()))
  742. return ForeachHelper(LHS, Arg, RHSo, Type, CurRec, CurMultiClass);
  743. std::vector<Init *> NewOperands;
  744. for (unsigned i = 0; i < RHSo->getNumOperands(); ++i) {
  745. if (auto *RHSoo = dyn_cast<OpInit>(RHSo->getOperand(i))) {
  746. if (Init *Result = EvaluateOperation(RHSoo, LHS, Arg,
  747. Type, CurRec, CurMultiClass))
  748. NewOperands.push_back(Result);
  749. else
  750. NewOperands.push_back(Arg);
  751. } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) {
  752. NewOperands.push_back(Arg);
  753. } else {
  754. NewOperands.push_back(RHSo->getOperand(i));
  755. }
  756. }
  757. // Now run the operator and use its result as the new leaf
  758. const OpInit *NewOp = RHSo->clone(NewOperands);
  759. Init *NewVal = NewOp->Fold(CurRec, CurMultiClass);
  760. return (NewVal != NewOp) ? NewVal : nullptr;
  761. }
  762. static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
  763. Record *CurRec, MultiClass *CurMultiClass) {
  764. OpInit *RHSo = dyn_cast<OpInit>(RHS);
  765. if (!RHSo)
  766. PrintFatalError(CurRec->getLoc(), "!foreach requires an operator\n");
  767. TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
  768. if (!LHSt)
  769. PrintFatalError(CurRec->getLoc(), "!foreach requires typed variable\n");
  770. DagInit *MHSd = dyn_cast<DagInit>(MHS);
  771. if (MHSd && isa<DagRecTy>(Type)) {
  772. Init *Val = MHSd->getOperator();
  773. if (Init *Result = EvaluateOperation(RHSo, LHS, Val,
  774. Type, CurRec, CurMultiClass))
  775. Val = Result;
  776. std::vector<std::pair<Init *, std::string> > args;
  777. for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
  778. Init *Arg = MHSd->getArg(i);
  779. std::string ArgName = MHSd->getArgName(i);
  780. // Process args
  781. if (Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type,
  782. CurRec, CurMultiClass))
  783. Arg = Result;
  784. // TODO: Process arg names
  785. args.push_back(std::make_pair(Arg, ArgName));
  786. }
  787. return DagInit::get(Val, "", args);
  788. }
  789. ListInit *MHSl = dyn_cast<ListInit>(MHS);
  790. if (MHSl && isa<ListRecTy>(Type)) {
  791. std::vector<Init *> NewOperands;
  792. std::vector<Init *> NewList(MHSl->begin(), MHSl->end());
  793. for (Init *&Item : NewList) {
  794. NewOperands.clear();
  795. for(unsigned i = 0; i < RHSo->getNumOperands(); ++i) {
  796. // First, replace the foreach variable with the list item
  797. if (LHS->getAsString() == RHSo->getOperand(i)->getAsString())
  798. NewOperands.push_back(Item);
  799. else
  800. NewOperands.push_back(RHSo->getOperand(i));
  801. }
  802. // Now run the operator and use its result as the new list item
  803. const OpInit *NewOp = RHSo->clone(NewOperands);
  804. Init *NewItem = NewOp->Fold(CurRec, CurMultiClass);
  805. if (NewItem != NewOp)
  806. Item = NewItem;
  807. }
  808. return ListInit::get(NewList, MHSl->getType());
  809. }
  810. return nullptr;
  811. }
  812. Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const {
  813. switch (getOpcode()) {
  814. case SUBST: {
  815. DefInit *LHSd = dyn_cast<DefInit>(LHS);
  816. VarInit *LHSv = dyn_cast<VarInit>(LHS);
  817. StringInit *LHSs = dyn_cast<StringInit>(LHS);
  818. DefInit *MHSd = dyn_cast<DefInit>(MHS);
  819. VarInit *MHSv = dyn_cast<VarInit>(MHS);
  820. StringInit *MHSs = dyn_cast<StringInit>(MHS);
  821. DefInit *RHSd = dyn_cast<DefInit>(RHS);
  822. VarInit *RHSv = dyn_cast<VarInit>(RHS);
  823. StringInit *RHSs = dyn_cast<StringInit>(RHS);
  824. if (LHSd && MHSd && RHSd) {
  825. Record *Val = RHSd->getDef();
  826. if (LHSd->getAsString() == RHSd->getAsString())
  827. Val = MHSd->getDef();
  828. return DefInit::get(Val);
  829. }
  830. if (LHSv && MHSv && RHSv) {
  831. std::string Val = RHSv->getName();
  832. if (LHSv->getAsString() == RHSv->getAsString())
  833. Val = MHSv->getName();
  834. return VarInit::get(Val, getType());
  835. }
  836. if (LHSs && MHSs && RHSs) {
  837. std::string Val = RHSs->getValue();
  838. std::string::size_type found;
  839. std::string::size_type idx = 0;
  840. while (true) {
  841. found = Val.find(LHSs->getValue(), idx);
  842. if (found == std::string::npos)
  843. break;
  844. Val.replace(found, LHSs->getValue().size(), MHSs->getValue());
  845. idx = found + MHSs->getValue().size();
  846. }
  847. return StringInit::get(Val);
  848. }
  849. break;
  850. }
  851. case FOREACH: {
  852. if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(),
  853. CurRec, CurMultiClass))
  854. return Result;
  855. break;
  856. }
  857. case IF: {
  858. IntInit *LHSi = dyn_cast<IntInit>(LHS);
  859. if (Init *I = LHS->convertInitializerTo(IntRecTy::get()))
  860. LHSi = dyn_cast<IntInit>(I);
  861. if (LHSi) {
  862. if (LHSi->getValue())
  863. return MHS;
  864. return RHS;
  865. }
  866. break;
  867. }
  868. }
  869. return const_cast<TernOpInit *>(this);
  870. }
  871. Init *TernOpInit::resolveReferences(Record &R,
  872. const RecordVal *RV) const {
  873. Init *lhs = LHS->resolveReferences(R, RV);
  874. if (Opc == IF && lhs != LHS) {
  875. IntInit *Value = dyn_cast<IntInit>(lhs);
  876. if (Init *I = lhs->convertInitializerTo(IntRecTy::get()))
  877. Value = dyn_cast<IntInit>(I);
  878. if (Value) {
  879. // Short-circuit
  880. if (Value->getValue()) {
  881. Init *mhs = MHS->resolveReferences(R, RV);
  882. return (TernOpInit::get(getOpcode(), lhs, mhs,
  883. RHS, getType()))->Fold(&R, nullptr);
  884. }
  885. Init *rhs = RHS->resolveReferences(R, RV);
  886. return (TernOpInit::get(getOpcode(), lhs, MHS,
  887. rhs, getType()))->Fold(&R, nullptr);
  888. }
  889. }
  890. Init *mhs = MHS->resolveReferences(R, RV);
  891. Init *rhs = RHS->resolveReferences(R, RV);
  892. if (LHS != lhs || MHS != mhs || RHS != rhs)
  893. return (TernOpInit::get(getOpcode(), lhs, mhs, rhs,
  894. getType()))->Fold(&R, nullptr);
  895. return Fold(&R, nullptr);
  896. }
  897. std::string TernOpInit::getAsString() const {
  898. std::string Result;
  899. switch (Opc) {
  900. case SUBST: Result = "!subst"; break;
  901. case FOREACH: Result = "!foreach"; break;
  902. case IF: Result = "!if"; break;
  903. }
  904. return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " +
  905. RHS->getAsString() + ")";
  906. }
  907. RecTy *TypedInit::getFieldType(const std::string &FieldName) const {
  908. if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType()))
  909. if (RecordVal *Field = RecordType->getRecord()->getValue(FieldName))
  910. return Field->getType();
  911. return nullptr;
  912. }
  913. Init *
  914. TypedInit::convertInitializerTo(RecTy *Ty) const {
  915. if (isa<IntRecTy>(Ty)) {
  916. if (getType()->typeIsConvertibleTo(Ty))
  917. return const_cast<TypedInit *>(this);
  918. return nullptr;
  919. }
  920. if (isa<StringRecTy>(Ty)) {
  921. if (isa<StringRecTy>(getType()))
  922. return const_cast<TypedInit *>(this);
  923. return nullptr;
  924. }
  925. if (isa<BitRecTy>(Ty)) {
  926. // Accept variable if it is already of bit type!
  927. if (isa<BitRecTy>(getType()))
  928. return const_cast<TypedInit *>(this);
  929. if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) {
  930. // Accept only bits<1> expression.
  931. if (BitsTy->getNumBits() == 1)
  932. return const_cast<TypedInit *>(this);
  933. return nullptr;
  934. }
  935. // Ternary !if can be converted to bit, but only if both sides are
  936. // convertible to a bit.
  937. if (const auto *TOI = dyn_cast<TernOpInit>(this)) {
  938. if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
  939. TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
  940. TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
  941. return const_cast<TypedInit *>(this);
  942. return nullptr;
  943. }
  944. return nullptr;
  945. }
  946. if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
  947. if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType()))
  948. return BitsInit::get(const_cast<TypedInit *>(this));
  949. if (getType()->typeIsConvertibleTo(BRT)) {
  950. SmallVector<Init *, 16> NewBits(BRT->getNumBits());
  951. for (unsigned i = 0; i != BRT->getNumBits(); ++i)
  952. NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i);
  953. return BitsInit::get(NewBits);
  954. }
  955. return nullptr;
  956. }
  957. if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) {
  958. if (auto *SLRT = dyn_cast<ListRecTy>(getType()))
  959. if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
  960. return const_cast<TypedInit *>(this);
  961. return nullptr;
  962. }
  963. if (auto *DRT = dyn_cast<DagRecTy>(Ty)) {
  964. if (getType()->typeIsConvertibleTo(DRT))
  965. return const_cast<TypedInit *>(this);
  966. return nullptr;
  967. }
  968. if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) {
  969. // Ensure that this is compatible with Rec.
  970. if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType()))
  971. if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
  972. DRRT->getRecord() == SRRT->getRecord())
  973. return const_cast<TypedInit *>(this);
  974. return nullptr;
  975. }
  976. return nullptr;
  977. }
  978. Init *
  979. TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
  980. BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
  981. if (!T) return nullptr; // Cannot subscript a non-bits variable.
  982. unsigned NumBits = T->getNumBits();
  983. SmallVector<Init *, 16> NewBits(Bits.size());
  984. for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
  985. if (Bits[i] >= NumBits)
  986. return nullptr;
  987. NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), Bits[i]);
  988. }
  989. return BitsInit::get(NewBits);
  990. }
  991. Init *
  992. TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) const {
  993. ListRecTy *T = dyn_cast<ListRecTy>(getType());
  994. if (!T) return nullptr; // Cannot subscript a non-list variable.
  995. if (Elements.size() == 1)
  996. return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
  997. std::vector<Init*> ListInits;
  998. ListInits.reserve(Elements.size());
  999. for (unsigned i = 0, e = Elements.size(); i != e; ++i)
  1000. ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
  1001. Elements[i]));
  1002. return ListInit::get(ListInits, T);
  1003. }
  1004. VarInit *VarInit::get(const std::string &VN, RecTy *T) {
  1005. Init *Value = StringInit::get(VN);
  1006. return VarInit::get(Value, T);
  1007. }
  1008. VarInit *VarInit::get(Init *VN, RecTy *T) {
  1009. typedef std::pair<RecTy *, Init *> Key;
  1010. static DenseMap<Key, std::unique_ptr<VarInit>> ThePool;
  1011. Key TheKey(std::make_pair(T, VN));
  1012. std::unique_ptr<VarInit> &I = ThePool[TheKey];
  1013. if (!I) I.reset(new VarInit(VN, T));
  1014. return I.get();
  1015. }
  1016. const std::string &VarInit::getName() const {
  1017. StringInit *NameString = cast<StringInit>(getNameInit());
  1018. return NameString->getValue();
  1019. }
  1020. Init *VarInit::getBit(unsigned Bit) const {
  1021. if (getType() == BitRecTy::get())
  1022. return const_cast<VarInit*>(this);
  1023. return VarBitInit::get(const_cast<VarInit*>(this), Bit);
  1024. }
  1025. Init *VarInit::resolveListElementReference(Record &R,
  1026. const RecordVal *IRV,
  1027. unsigned Elt) const {
  1028. if (R.isTemplateArg(getNameInit())) return nullptr;
  1029. if (IRV && IRV->getNameInit() != getNameInit()) return nullptr;
  1030. RecordVal *RV = R.getValue(getNameInit());
  1031. assert(RV && "Reference to a non-existent variable?");
  1032. ListInit *LI = dyn_cast<ListInit>(RV->getValue());
  1033. if (!LI)
  1034. return VarListElementInit::get(cast<TypedInit>(RV->getValue()), Elt);
  1035. if (Elt >= LI->size())
  1036. return nullptr; // Out of range reference.
  1037. Init *E = LI->getElement(Elt);
  1038. // If the element is set to some value, or if we are resolving a reference
  1039. // to a specific variable and that variable is explicitly unset, then
  1040. // replace the VarListElementInit with it.
  1041. if (IRV || !isa<UnsetInit>(E))
  1042. return E;
  1043. return nullptr;
  1044. }
  1045. RecTy *VarInit::getFieldType(const std::string &FieldName) const {
  1046. if (RecordRecTy *RTy = dyn_cast<RecordRecTy>(getType()))
  1047. if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
  1048. return RV->getType();
  1049. return nullptr;
  1050. }
  1051. Init *VarInit::getFieldInit(Record &R, const RecordVal *RV,
  1052. const std::string &FieldName) const {
  1053. if (isa<RecordRecTy>(getType()))
  1054. if (const RecordVal *Val = R.getValue(VarName)) {
  1055. if (RV != Val && (RV || isa<UnsetInit>(Val->getValue())))
  1056. return nullptr;
  1057. Init *TheInit = Val->getValue();
  1058. assert(TheInit != this && "Infinite loop detected!");
  1059. if (Init *I = TheInit->getFieldInit(R, RV, FieldName))
  1060. return I;
  1061. return nullptr;
  1062. }
  1063. return nullptr;
  1064. }
  1065. /// resolveReferences - This method is used by classes that refer to other
  1066. /// variables which may not be defined at the time the expression is formed.
  1067. /// If a value is set for the variable later, this method will be called on
  1068. /// users of the value to allow the value to propagate out.
  1069. ///
  1070. Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const {
  1071. if (RecordVal *Val = R.getValue(VarName))
  1072. if (RV == Val || (!RV && !isa<UnsetInit>(Val->getValue())))
  1073. return Val->getValue();
  1074. return const_cast<VarInit *>(this);
  1075. }
  1076. VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
  1077. typedef std::pair<TypedInit *, unsigned> Key;
  1078. static DenseMap<Key, std::unique_ptr<VarBitInit>> ThePool;
  1079. Key TheKey(std::make_pair(T, B));
  1080. std::unique_ptr<VarBitInit> &I = ThePool[TheKey];
  1081. if (!I) I.reset(new VarBitInit(T, B));
  1082. return I.get();
  1083. }
  1084. Init *VarBitInit::convertInitializerTo(RecTy *Ty) const {
  1085. if (isa<BitRecTy>(Ty))
  1086. return const_cast<VarBitInit *>(this);
  1087. return nullptr;
  1088. }
  1089. std::string VarBitInit::getAsString() const {
  1090. return TI->getAsString() + "{" + utostr(Bit) + "}";
  1091. }
  1092. Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const {
  1093. Init *I = TI->resolveReferences(R, RV);
  1094. if (TI != I)
  1095. return I->getBit(getBitNum());
  1096. return const_cast<VarBitInit*>(this);
  1097. }
  1098. VarListElementInit *VarListElementInit::get(TypedInit *T,
  1099. unsigned E) {
  1100. typedef std::pair<TypedInit *, unsigned> Key;
  1101. static DenseMap<Key, std::unique_ptr<VarListElementInit>> ThePool;
  1102. Key TheKey(std::make_pair(T, E));
  1103. std::unique_ptr<VarListElementInit> &I = ThePool[TheKey];
  1104. if (!I) I.reset(new VarListElementInit(T, E));
  1105. return I.get();
  1106. }
  1107. std::string VarListElementInit::getAsString() const {
  1108. return TI->getAsString() + "[" + utostr(Element) + "]";
  1109. }
  1110. Init *
  1111. VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const {
  1112. if (Init *I = getVariable()->resolveListElementReference(R, RV,
  1113. getElementNum()))
  1114. return I;
  1115. return const_cast<VarListElementInit *>(this);
  1116. }
  1117. Init *VarListElementInit::getBit(unsigned Bit) const {
  1118. if (getType() == BitRecTy::get())
  1119. return const_cast<VarListElementInit*>(this);
  1120. return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
  1121. }
  1122. Init *VarListElementInit:: resolveListElementReference(Record &R,
  1123. const RecordVal *RV,
  1124. unsigned Elt) const {
  1125. if (Init *Result = TI->resolveListElementReference(R, RV, Element)) {
  1126. if (TypedInit *TInit = dyn_cast<TypedInit>(Result)) {
  1127. if (Init *Result2 = TInit->resolveListElementReference(R, RV, Elt))
  1128. return Result2;
  1129. return VarListElementInit::get(TInit, Elt);
  1130. }
  1131. return Result;
  1132. }
  1133. return nullptr;
  1134. }
  1135. DefInit *DefInit::get(Record *R) {
  1136. return R->getDefInit();
  1137. }
  1138. Init *DefInit::convertInitializerTo(RecTy *Ty) const {
  1139. if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
  1140. if (getDef()->isSubClassOf(RRT->getRecord()))
  1141. return const_cast<DefInit *>(this);
  1142. return nullptr;
  1143. }
  1144. RecTy *DefInit::getFieldType(const std::string &FieldName) const {
  1145. if (const RecordVal *RV = Def->getValue(FieldName))
  1146. return RV->getType();
  1147. return nullptr;
  1148. }
  1149. Init *DefInit::getFieldInit(Record &R, const RecordVal *RV,
  1150. const std::string &FieldName) const {
  1151. return Def->getValue(FieldName)->getValue();
  1152. }
  1153. std::string DefInit::getAsString() const {
  1154. return Def->getName();
  1155. }
  1156. FieldInit *FieldInit::get(Init *R, const std::string &FN) {
  1157. typedef std::pair<Init *, TableGenStringKey> Key;
  1158. static DenseMap<Key, std::unique_ptr<FieldInit>> ThePool;
  1159. Key TheKey(std::make_pair(R, FN));
  1160. std::unique_ptr<FieldInit> &I = ThePool[TheKey];
  1161. if (!I) I.reset(new FieldInit(R, FN));
  1162. return I.get();
  1163. }
  1164. Init *FieldInit::getBit(unsigned Bit) const {
  1165. if (getType() == BitRecTy::get())
  1166. return const_cast<FieldInit*>(this);
  1167. return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
  1168. }
  1169. Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV,
  1170. unsigned Elt) const {
  1171. if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName))
  1172. if (ListInit *LI = dyn_cast<ListInit>(ListVal)) {
  1173. if (Elt >= LI->size()) return nullptr;
  1174. Init *E = LI->getElement(Elt);
  1175. // If the element is set to some value, or if we are resolving a
  1176. // reference to a specific variable and that variable is explicitly
  1177. // unset, then replace the VarListElementInit with it.
  1178. if (RV || !isa<UnsetInit>(E))
  1179. return E;
  1180. }
  1181. return nullptr;
  1182. }
  1183. Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const {
  1184. Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec;
  1185. if (Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName)) {
  1186. Init *BVR = BitsVal->resolveReferences(R, RV);
  1187. return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this);
  1188. }
  1189. if (NewRec != Rec)
  1190. return FieldInit::get(NewRec, FieldName);
  1191. return const_cast<FieldInit *>(this);
  1192. }
  1193. static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, const std::string &VN,
  1194. ArrayRef<Init *> ArgRange,
  1195. ArrayRef<std::string> NameRange) {
  1196. ID.AddPointer(V);
  1197. ID.AddString(VN);
  1198. ArrayRef<Init *>::iterator Arg = ArgRange.begin();
  1199. ArrayRef<std::string>::iterator Name = NameRange.begin();
  1200. while (Arg != ArgRange.end()) {
  1201. assert(Name != NameRange.end() && "Arg name underflow!");
  1202. ID.AddPointer(*Arg++);
  1203. ID.AddString(*Name++);
  1204. }
  1205. assert(Name == NameRange.end() && "Arg name overflow!");
  1206. }
  1207. DagInit *
  1208. DagInit::get(Init *V, const std::string &VN,
  1209. ArrayRef<Init *> ArgRange,
  1210. ArrayRef<std::string> NameRange) {
  1211. static FoldingSet<DagInit> ThePool;
  1212. static std::vector<std::unique_ptr<DagInit>> TheActualPool;
  1213. FoldingSetNodeID ID;
  1214. ProfileDagInit(ID, V, VN, ArgRange, NameRange);
  1215. void *IP = nullptr;
  1216. if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP))
  1217. return I;
  1218. DagInit *I = new DagInit(V, VN, ArgRange, NameRange);
  1219. ThePool.InsertNode(I, IP);
  1220. TheActualPool.push_back(std::unique_ptr<DagInit>(I));
  1221. return I;
  1222. }
  1223. DagInit *
  1224. DagInit::get(Init *V, const std::string &VN,
  1225. const std::vector<std::pair<Init*, std::string> > &args) {
  1226. std::vector<Init *> Args;
  1227. std::vector<std::string> Names;
  1228. for (const auto &Arg : args) {
  1229. Args.push_back(Arg.first);
  1230. Names.push_back(Arg.second);
  1231. }
  1232. return DagInit::get(V, VN, Args, Names);
  1233. }
  1234. void DagInit::Profile(FoldingSetNodeID &ID) const {
  1235. ProfileDagInit(ID, Val, ValName, Args, ArgNames);
  1236. }
  1237. Init *DagInit::convertInitializerTo(RecTy *Ty) const {
  1238. if (isa<DagRecTy>(Ty))
  1239. return const_cast<DagInit *>(this);
  1240. return nullptr;
  1241. }
  1242. Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const {
  1243. std::vector<Init*> NewArgs;
  1244. for (unsigned i = 0, e = Args.size(); i != e; ++i)
  1245. NewArgs.push_back(Args[i]->resolveReferences(R, RV));
  1246. Init *Op = Val->resolveReferences(R, RV);
  1247. if (Args != NewArgs || Op != Val)
  1248. return DagInit::get(Op, ValName, NewArgs, ArgNames);
  1249. return const_cast<DagInit *>(this);
  1250. }
  1251. std::string DagInit::getAsString() const {
  1252. std::string Result = "(" + Val->getAsString();
  1253. if (!ValName.empty())
  1254. Result += ":" + ValName;
  1255. if (!Args.empty()) {
  1256. Result += " " + Args[0]->getAsString();
  1257. if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0];
  1258. for (unsigned i = 1, e = Args.size(); i != e; ++i) {
  1259. Result += ", " + Args[i]->getAsString();
  1260. if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i];
  1261. }
  1262. }
  1263. return Result + ")";
  1264. }
  1265. //===----------------------------------------------------------------------===//
  1266. // Other implementations
  1267. //===----------------------------------------------------------------------===//
  1268. RecordVal::RecordVal(Init *N, RecTy *T, bool P)
  1269. : NameAndPrefix(N, P), Ty(T) {
  1270. Value = UnsetInit::get()->convertInitializerTo(Ty);
  1271. assert(Value && "Cannot create unset value for current type!");
  1272. }
  1273. RecordVal::RecordVal(const std::string &N, RecTy *T, bool P)
  1274. : NameAndPrefix(StringInit::get(N), P), Ty(T) {
  1275. Value = UnsetInit::get()->convertInitializerTo(Ty);
  1276. assert(Value && "Cannot create unset value for current type!");
  1277. }
  1278. const std::string &RecordVal::getName() const {
  1279. return cast<StringInit>(getNameInit())->getValue();
  1280. }
  1281. void RecordVal::dump() const { errs() << *this; }
  1282. void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
  1283. if (getPrefix()) OS << "field ";
  1284. OS << *getType() << " " << getNameInitAsString();
  1285. if (getValue())
  1286. OS << " = " << *getValue();
  1287. if (PrintSem) OS << ";\n";
  1288. }
  1289. unsigned Record::LastID = 0;
  1290. void Record::init() {
  1291. checkName();
  1292. // Every record potentially has a def at the top. This value is
  1293. // replaced with the top-level def name at instantiation time.
  1294. RecordVal DN("NAME", StringRecTy::get(), 0);
  1295. addValue(DN);
  1296. }
  1297. void Record::checkName() {
  1298. // Ensure the record name has string type.
  1299. const TypedInit *TypedName = cast<const TypedInit>(Name);
  1300. if (!isa<StringRecTy>(TypedName->getType()))
  1301. PrintFatalError(getLoc(), "Record name is not a string!");
  1302. }
  1303. DefInit *Record::getDefInit() {
  1304. if (!TheInit)
  1305. TheInit.reset(new DefInit(this, new RecordRecTy(this)));
  1306. return TheInit.get();
  1307. }
  1308. const std::string &Record::getName() const {
  1309. return cast<StringInit>(Name)->getValue();
  1310. }
  1311. void Record::setName(Init *NewName) {
  1312. Name = NewName;
  1313. checkName();
  1314. // DO NOT resolve record values to the name at this point because
  1315. // there might be default values for arguments of this def. Those
  1316. // arguments might not have been resolved yet so we don't want to
  1317. // prematurely assume values for those arguments were not passed to
  1318. // this def.
  1319. //
  1320. // Nonetheless, it may be that some of this Record's values
  1321. // reference the record name. Indeed, the reason for having the
  1322. // record name be an Init is to provide this flexibility. The extra
  1323. // resolve steps after completely instantiating defs takes care of
  1324. // this. See TGParser::ParseDef and TGParser::ParseDefm.
  1325. }
  1326. void Record::setName(const std::string &Name) {
  1327. setName(StringInit::get(Name));
  1328. }
  1329. /// resolveReferencesTo - If anything in this record refers to RV, replace the
  1330. /// reference to RV with the RHS of RV. If RV is null, we resolve all possible
  1331. /// references.
  1332. void Record::resolveReferencesTo(const RecordVal *RV) {
  1333. for (unsigned i = 0, e = Values.size(); i != e; ++i) {
  1334. if (RV == &Values[i]) // Skip resolve the same field as the given one
  1335. continue;
  1336. if (Init *V = Values[i].getValue())
  1337. if (Values[i].setValue(V->resolveReferences(*this, RV)))
  1338. PrintFatalError(getLoc(), "Invalid value is found when setting '" +
  1339. Values[i].getNameInitAsString() +
  1340. "' after resolving references" +
  1341. (RV ? " against '" + RV->getNameInitAsString() +
  1342. "' of (" + RV->getValue()->getAsUnquotedString() +
  1343. ")"
  1344. : "") + "\n");
  1345. }
  1346. Init *OldName = getNameInit();
  1347. Init *NewName = Name->resolveReferences(*this, RV);
  1348. if (NewName != OldName) {
  1349. // Re-register with RecordKeeper.
  1350. setName(NewName);
  1351. }
  1352. }
  1353. void Record::dump() const { errs() << *this; }
  1354. raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
  1355. OS << R.getNameInitAsString();
  1356. const std::vector<Init *> &TArgs = R.getTemplateArgs();
  1357. if (!TArgs.empty()) {
  1358. OS << "<";
  1359. bool NeedComma = false;
  1360. for (const Init *TA : TArgs) {
  1361. if (NeedComma) OS << ", ";
  1362. NeedComma = true;
  1363. const RecordVal *RV = R.getValue(TA);
  1364. assert(RV && "Template argument record not found??");
  1365. RV->print(OS, false);
  1366. }
  1367. OS << ">";
  1368. }
  1369. OS << " {";
  1370. ArrayRef<Record *> SC = R.getSuperClasses();
  1371. if (!SC.empty()) {
  1372. OS << "\t//";
  1373. for (const Record *Super : SC)
  1374. OS << " " << Super->getNameInitAsString();
  1375. }
  1376. OS << "\n";
  1377. for (const RecordVal &Val : R.getValues())
  1378. if (Val.getPrefix() && !R.isTemplateArg(Val.getName()))
  1379. OS << Val;
  1380. for (const RecordVal &Val : R.getValues())
  1381. if (!Val.getPrefix() && !R.isTemplateArg(Val.getName()))
  1382. OS << Val;
  1383. return OS << "}\n";
  1384. }
  1385. /// getValueInit - Return the initializer for a value with the specified name,
  1386. /// or abort if the field does not exist.
  1387. ///
  1388. Init *Record::getValueInit(StringRef FieldName) const {
  1389. const RecordVal *R = getValue(FieldName);
  1390. if (!R || !R->getValue())
  1391. PrintFatalError(getLoc(), "Record `" + getName() +
  1392. "' does not have a field named `" + FieldName + "'!\n");
  1393. return R->getValue();
  1394. }
  1395. /// getValueAsString - This method looks up the specified field and returns its
  1396. /// value as a string, aborts if the field does not exist or if
  1397. /// the value is not a string.
  1398. ///
  1399. std::string Record::getValueAsString(StringRef FieldName) const {
  1400. const RecordVal *R = getValue(FieldName);
  1401. if (!R || !R->getValue())
  1402. PrintFatalError(getLoc(), "Record `" + getName() +
  1403. "' does not have a field named `" + FieldName + "'!\n");
  1404. if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
  1405. return SI->getValue();
  1406. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1407. FieldName + "' does not have a string initializer!");
  1408. }
  1409. /// getValueAsBitsInit - This method looks up the specified field and returns
  1410. /// its value as a BitsInit, aborts if the field does not exist or if
  1411. /// the value is not the right type.
  1412. ///
  1413. BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
  1414. const RecordVal *R = getValue(FieldName);
  1415. if (!R || !R->getValue())
  1416. PrintFatalError(getLoc(), "Record `" + getName() +
  1417. "' does not have a field named `" + FieldName + "'!\n");
  1418. if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
  1419. return BI;
  1420. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1421. FieldName + "' does not have a BitsInit initializer!");
  1422. }
  1423. /// getValueAsListInit - This method looks up the specified field and returns
  1424. /// its value as a ListInit, aborting if the field does not exist or if
  1425. /// the value is not the right type.
  1426. ///
  1427. ListInit *Record::getValueAsListInit(StringRef FieldName) const {
  1428. const RecordVal *R = getValue(FieldName);
  1429. if (!R || !R->getValue())
  1430. PrintFatalError(getLoc(), "Record `" + getName() +
  1431. "' does not have a field named `" + FieldName + "'!\n");
  1432. if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
  1433. return LI;
  1434. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1435. FieldName + "' does not have a list initializer!");
  1436. }
  1437. /// getValueAsListOfDefs - This method looks up the specified field and returns
  1438. /// its value as a vector of records, aborting if the field does not exist
  1439. /// or if the value is not the right type.
  1440. ///
  1441. std::vector<Record*>
  1442. Record::getValueAsListOfDefs(StringRef FieldName) const {
  1443. ListInit *List = getValueAsListInit(FieldName);
  1444. std::vector<Record*> Defs;
  1445. for (Init *I : List->getValues()) {
  1446. if (DefInit *DI = dyn_cast<DefInit>(I))
  1447. Defs.push_back(DI->getDef());
  1448. else
  1449. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1450. FieldName + "' list is not entirely DefInit!");
  1451. }
  1452. return Defs;
  1453. }
  1454. /// getValueAsInt - This method looks up the specified field and returns its
  1455. /// value as an int64_t, aborting if the field does not exist or if the value
  1456. /// is not the right type.
  1457. ///
  1458. int64_t Record::getValueAsInt(StringRef FieldName) const {
  1459. const RecordVal *R = getValue(FieldName);
  1460. if (!R || !R->getValue())
  1461. PrintFatalError(getLoc(), "Record `" + getName() +
  1462. "' does not have a field named `" + FieldName + "'!\n");
  1463. if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
  1464. return II->getValue();
  1465. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1466. FieldName + "' does not have an int initializer!");
  1467. }
  1468. /// getValueAsListOfInts - This method looks up the specified field and returns
  1469. /// its value as a vector of integers, aborting if the field does not exist or
  1470. /// if the value is not the right type.
  1471. ///
  1472. std::vector<int64_t>
  1473. Record::getValueAsListOfInts(StringRef FieldName) const {
  1474. ListInit *List = getValueAsListInit(FieldName);
  1475. std::vector<int64_t> Ints;
  1476. for (Init *I : List->getValues()) {
  1477. if (IntInit *II = dyn_cast<IntInit>(I))
  1478. Ints.push_back(II->getValue());
  1479. else
  1480. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1481. FieldName + "' does not have a list of ints initializer!");
  1482. }
  1483. return Ints;
  1484. }
  1485. /// getValueAsListOfStrings - This method looks up the specified field and
  1486. /// returns its value as a vector of strings, aborting if the field does not
  1487. /// exist or if the value is not the right type.
  1488. ///
  1489. std::vector<std::string>
  1490. Record::getValueAsListOfStrings(StringRef FieldName) const {
  1491. ListInit *List = getValueAsListInit(FieldName);
  1492. std::vector<std::string> Strings;
  1493. for (Init *I : List->getValues()) {
  1494. if (StringInit *SI = dyn_cast<StringInit>(I))
  1495. Strings.push_back(SI->getValue());
  1496. else
  1497. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1498. FieldName + "' does not have a list of strings initializer!");
  1499. }
  1500. return Strings;
  1501. }
  1502. /// getValueAsDef - This method looks up the specified field and returns its
  1503. /// value as a Record, aborting if the field does not exist or if the value
  1504. /// is not the right type.
  1505. ///
  1506. Record *Record::getValueAsDef(StringRef FieldName) const {
  1507. const RecordVal *R = getValue(FieldName);
  1508. if (!R || !R->getValue())
  1509. PrintFatalError(getLoc(), "Record `" + getName() +
  1510. "' does not have a field named `" + FieldName + "'!\n");
  1511. if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
  1512. return DI->getDef();
  1513. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1514. FieldName + "' does not have a def initializer!");
  1515. }
  1516. /// getValueAsBit - This method looks up the specified field and returns its
  1517. /// value as a bit, aborting if the field does not exist or if the value is
  1518. /// not the right type.
  1519. ///
  1520. bool Record::getValueAsBit(StringRef FieldName) const {
  1521. const RecordVal *R = getValue(FieldName);
  1522. if (!R || !R->getValue())
  1523. PrintFatalError(getLoc(), "Record `" + getName() +
  1524. "' does not have a field named `" + FieldName + "'!\n");
  1525. if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
  1526. return BI->getValue();
  1527. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1528. FieldName + "' does not have a bit initializer!");
  1529. }
  1530. bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
  1531. const RecordVal *R = getValue(FieldName);
  1532. if (!R || !R->getValue())
  1533. PrintFatalError(getLoc(), "Record `" + getName() +
  1534. "' does not have a field named `" + FieldName.str() + "'!\n");
  1535. if (isa<UnsetInit>(R->getValue())) {
  1536. Unset = true;
  1537. return false;
  1538. }
  1539. Unset = false;
  1540. if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
  1541. return BI->getValue();
  1542. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1543. FieldName + "' does not have a bit initializer!");
  1544. }
  1545. /// getValueAsDag - This method looks up the specified field and returns its
  1546. /// value as an Dag, aborting if the field does not exist or if the value is
  1547. /// not the right type.
  1548. ///
  1549. DagInit *Record::getValueAsDag(StringRef FieldName) const {
  1550. const RecordVal *R = getValue(FieldName);
  1551. if (!R || !R->getValue())
  1552. PrintFatalError(getLoc(), "Record `" + getName() +
  1553. "' does not have a field named `" + FieldName + "'!\n");
  1554. if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
  1555. return DI;
  1556. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  1557. FieldName + "' does not have a dag initializer!");
  1558. }
  1559. void MultiClass::dump() const {
  1560. errs() << "Record:\n";
  1561. Rec.dump();
  1562. errs() << "Defs:\n";
  1563. for (const auto &Proto : DefPrototypes)
  1564. Proto->dump();
  1565. }
  1566. void RecordKeeper::dump() const { errs() << *this; }
  1567. raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
  1568. OS << "------------- Classes -----------------\n";
  1569. for (const auto &C : RK.getClasses())
  1570. OS << "class " << *C.second;
  1571. OS << "------------- Defs -----------------\n";
  1572. for (const auto &D : RK.getDefs())
  1573. OS << "def " << *D.second;
  1574. return OS;
  1575. }
  1576. /// getAllDerivedDefinitions - This method returns all concrete definitions
  1577. /// that derive from the specified class name. If a class with the specified
  1578. /// name does not exist, an error is printed and true is returned.
  1579. std::vector<Record*>
  1580. RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
  1581. Record *Class = getClass(ClassName);
  1582. if (!Class)
  1583. PrintFatalError("ERROR: Couldn't find the `" + ClassName + "' class!\n");
  1584. std::vector<Record*> Defs;
  1585. for (const auto &D : getDefs())
  1586. if (D.second->isSubClassOf(Class))
  1587. Defs.push_back(D.second.get());
  1588. return Defs;
  1589. }
  1590. /// QualifyName - Return an Init with a qualifier prefix referring
  1591. /// to CurRec's name.
  1592. Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
  1593. Init *Name, const std::string &Scoper) {
  1594. RecTy *Type = cast<TypedInit>(Name)->getType();
  1595. BinOpInit *NewName =
  1596. BinOpInit::get(BinOpInit::STRCONCAT,
  1597. BinOpInit::get(BinOpInit::STRCONCAT,
  1598. CurRec.getNameInit(),
  1599. StringInit::get(Scoper),
  1600. Type)->Fold(&CurRec, CurMultiClass),
  1601. Name,
  1602. Type);
  1603. if (CurMultiClass && Scoper != "::") {
  1604. NewName =
  1605. BinOpInit::get(BinOpInit::STRCONCAT,
  1606. BinOpInit::get(BinOpInit::STRCONCAT,
  1607. CurMultiClass->Rec.getNameInit(),
  1608. StringInit::get("::"),
  1609. Type)->Fold(&CurRec, CurMultiClass),
  1610. NewName->Fold(&CurRec, CurMultiClass),
  1611. Type);
  1612. }
  1613. return NewName->Fold(&CurRec, CurMultiClass);
  1614. }
  1615. /// QualifyName - Return an Init with a qualifier prefix referring
  1616. /// to CurRec's name.
  1617. Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass,
  1618. const std::string &Name,
  1619. const std::string &Scoper) {
  1620. return QualifyName(CurRec, CurMultiClass, StringInit::get(Name), Scoper);
  1621. }