Record.cpp 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839
  1. //===- Record.cpp - Record implementation ---------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Implement the tablegen record classes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/TableGen/Record.h"
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/FoldingSet.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringExtras.h"
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/Config/llvm-config.h"
  22. #include "llvm/Support/Allocator.h"
  23. #include "llvm/Support/Casting.h"
  24. #include "llvm/Support/Compiler.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include "llvm/Support/ManagedStatic.h"
  27. #include "llvm/Support/SMLoc.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include "llvm/TableGen/Error.h"
  30. #include <cassert>
  31. #include <cstdint>
  32. #include <map>
  33. #include <memory>
  34. #include <string>
  35. #include <utility>
  36. #include <vector>
  37. using namespace llvm;
  38. #define DEBUG_TYPE "tblgen-records"
  39. //===----------------------------------------------------------------------===//
  40. // Context
  41. //===----------------------------------------------------------------------===//
  42. namespace llvm {
  43. namespace detail {
  44. /// This class contains all of the contextual static state of the Record
  45. /// classes. This allows for better lifetime management and control of the used
  46. /// static data.
  47. struct RecordContext {
  48. RecordContext()
  49. : AnyRecord(0), TrueBitInit(true, &SharedBitRecTy),
  50. FalseBitInit(false, &SharedBitRecTy), StringInitStringPool(Allocator),
  51. StringInitCodePool(Allocator), LastRecordID(0) {}
  52. BumpPtrAllocator Allocator;
  53. std::vector<BitsRecTy *> SharedBitsRecTys;
  54. BitRecTy SharedBitRecTy;
  55. IntRecTy SharedIntRecTy;
  56. StringRecTy SharedStringRecTy;
  57. DagRecTy SharedDagRecTy;
  58. RecordRecTy AnyRecord;
  59. UnsetInit TheUnsetInit;
  60. BitInit TrueBitInit;
  61. BitInit FalseBitInit;
  62. FoldingSet<BitsInit> TheBitsInitPool;
  63. std::map<int64_t, IntInit *> TheIntInitPool;
  64. StringMap<StringInit *, BumpPtrAllocator &> StringInitStringPool;
  65. StringMap<StringInit *, BumpPtrAllocator &> StringInitCodePool;
  66. FoldingSet<ListInit> TheListInitPool;
  67. FoldingSet<UnOpInit> TheUnOpInitPool;
  68. FoldingSet<BinOpInit> TheBinOpInitPool;
  69. FoldingSet<TernOpInit> TheTernOpInitPool;
  70. FoldingSet<FoldOpInit> TheFoldOpInitPool;
  71. FoldingSet<IsAOpInit> TheIsAOpInitPool;
  72. DenseMap<std::pair<RecTy *, Init *>, VarInit *> TheVarInitPool;
  73. DenseMap<std::pair<TypedInit *, unsigned>, VarBitInit *> TheVarBitInitPool;
  74. DenseMap<std::pair<TypedInit *, unsigned>, VarListElementInit *>
  75. TheVarListElementInitPool;
  76. FoldingSet<VarDefInit> TheVarDefInitPool;
  77. DenseMap<std::pair<Init *, StringInit *>, FieldInit *> TheFieldInitPool;
  78. FoldingSet<CondOpInit> TheCondOpInitPool;
  79. FoldingSet<DagInit> TheDagInitPool;
  80. unsigned LastRecordID;
  81. };
  82. } // namespace detail
  83. } // namespace llvm
  84. ManagedStatic<detail::RecordContext> Context;
  85. //===----------------------------------------------------------------------===//
  86. // Type implementations
  87. //===----------------------------------------------------------------------===//
  88. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  89. LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); }
  90. #endif
  91. ListRecTy *RecTy::getListTy() {
  92. if (!ListTy)
  93. ListTy = new(Context->Allocator) ListRecTy(this);
  94. return ListTy;
  95. }
  96. bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  97. assert(RHS && "NULL pointer");
  98. return Kind == RHS->getRecTyKind();
  99. }
  100. bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; }
  101. BitRecTy *BitRecTy::get() { return &Context->SharedBitRecTy; }
  102. bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{
  103. if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind)
  104. return true;
  105. if (const BitsRecTy *BitsTy = dyn_cast<BitsRecTy>(RHS))
  106. return BitsTy->getNumBits() == 1;
  107. return false;
  108. }
  109. BitsRecTy *BitsRecTy::get(unsigned Sz) {
  110. if (Sz >= Context->SharedBitsRecTys.size())
  111. Context->SharedBitsRecTys.resize(Sz + 1);
  112. BitsRecTy *&Ty = Context->SharedBitsRecTys[Sz];
  113. if (!Ty)
  114. Ty = new (Context->Allocator) BitsRecTy(Sz);
  115. return Ty;
  116. }
  117. std::string BitsRecTy::getAsString() const {
  118. return "bits<" + utostr(Size) + ">";
  119. }
  120. bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  121. if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type
  122. return cast<BitsRecTy>(RHS)->Size == Size;
  123. RecTyKind kind = RHS->getRecTyKind();
  124. return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind);
  125. }
  126. bool BitsRecTy::typeIsA(const RecTy *RHS) const {
  127. if (const BitsRecTy *RHSb = dyn_cast<BitsRecTy>(RHS))
  128. return RHSb->Size == Size;
  129. return false;
  130. }
  131. IntRecTy *IntRecTy::get() { return &Context->SharedIntRecTy; }
  132. bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  133. RecTyKind kind = RHS->getRecTyKind();
  134. return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
  135. }
  136. StringRecTy *StringRecTy::get() { return &Context->SharedStringRecTy; }
  137. std::string StringRecTy::getAsString() const {
  138. return "string";
  139. }
  140. bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  141. RecTyKind Kind = RHS->getRecTyKind();
  142. return Kind == StringRecTyKind;
  143. }
  144. std::string ListRecTy::getAsString() const {
  145. return "list<" + ElementTy->getAsString() + ">";
  146. }
  147. bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  148. if (const auto *ListTy = dyn_cast<ListRecTy>(RHS))
  149. return ElementTy->typeIsConvertibleTo(ListTy->getElementType());
  150. return false;
  151. }
  152. bool ListRecTy::typeIsA(const RecTy *RHS) const {
  153. if (const ListRecTy *RHSl = dyn_cast<ListRecTy>(RHS))
  154. return getElementType()->typeIsA(RHSl->getElementType());
  155. return false;
  156. }
  157. DagRecTy *DagRecTy::get() { return &Context->SharedDagRecTy; }
  158. std::string DagRecTy::getAsString() const {
  159. return "dag";
  160. }
  161. static void ProfileRecordRecTy(FoldingSetNodeID &ID,
  162. ArrayRef<Record *> Classes) {
  163. ID.AddInteger(Classes.size());
  164. for (Record *R : Classes)
  165. ID.AddPointer(R);
  166. }
  167. RecordRecTy *RecordRecTy::get(ArrayRef<Record *> UnsortedClasses) {
  168. if (UnsortedClasses.empty())
  169. return &Context->AnyRecord;
  170. FoldingSet<RecordRecTy> &ThePool =
  171. UnsortedClasses[0]->getRecords().RecordTypePool;
  172. SmallVector<Record *, 4> Classes(UnsortedClasses.begin(),
  173. UnsortedClasses.end());
  174. llvm::sort(Classes, [](Record *LHS, Record *RHS) {
  175. return LHS->getNameInitAsString() < RHS->getNameInitAsString();
  176. });
  177. FoldingSetNodeID ID;
  178. ProfileRecordRecTy(ID, Classes);
  179. void *IP = nullptr;
  180. if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, IP))
  181. return Ty;
  182. #ifndef NDEBUG
  183. // Check for redundancy.
  184. for (unsigned i = 0; i < Classes.size(); ++i) {
  185. for (unsigned j = 0; j < Classes.size(); ++j) {
  186. assert(i == j || !Classes[i]->isSubClassOf(Classes[j]));
  187. }
  188. assert(&Classes[0]->getRecords() == &Classes[i]->getRecords());
  189. }
  190. #endif
  191. void *Mem = Context->Allocator.Allocate(
  192. totalSizeToAlloc<Record *>(Classes.size()), alignof(RecordRecTy));
  193. RecordRecTy *Ty = new(Mem) RecordRecTy(Classes.size());
  194. std::uninitialized_copy(Classes.begin(), Classes.end(),
  195. Ty->getTrailingObjects<Record *>());
  196. ThePool.InsertNode(Ty, IP);
  197. return Ty;
  198. }
  199. void RecordRecTy::Profile(FoldingSetNodeID &ID) const {
  200. ProfileRecordRecTy(ID, getClasses());
  201. }
  202. std::string RecordRecTy::getAsString() const {
  203. if (NumClasses == 1)
  204. return getClasses()[0]->getNameInitAsString();
  205. std::string Str = "{";
  206. bool First = true;
  207. for (Record *R : getClasses()) {
  208. if (!First)
  209. Str += ", ";
  210. First = false;
  211. Str += R->getNameInitAsString();
  212. }
  213. Str += "}";
  214. return Str;
  215. }
  216. bool RecordRecTy::isSubClassOf(Record *Class) const {
  217. return llvm::any_of(getClasses(), [Class](Record *MySuperClass) {
  218. return MySuperClass == Class ||
  219. MySuperClass->isSubClassOf(Class);
  220. });
  221. }
  222. bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
  223. if (this == RHS)
  224. return true;
  225. const RecordRecTy *RTy = dyn_cast<RecordRecTy>(RHS);
  226. if (!RTy)
  227. return false;
  228. return llvm::all_of(RTy->getClasses(), [this](Record *TargetClass) {
  229. return isSubClassOf(TargetClass);
  230. });
  231. }
  232. bool RecordRecTy::typeIsA(const RecTy *RHS) const {
  233. return typeIsConvertibleTo(RHS);
  234. }
  235. static RecordRecTy *resolveRecordTypes(RecordRecTy *T1, RecordRecTy *T2) {
  236. SmallVector<Record *, 4> CommonSuperClasses;
  237. SmallVector<Record *, 4> Stack(T1->classes_begin(), T1->classes_end());
  238. while (!Stack.empty()) {
  239. Record *R = Stack.pop_back_val();
  240. if (T2->isSubClassOf(R)) {
  241. CommonSuperClasses.push_back(R);
  242. } else {
  243. R->getDirectSuperClasses(Stack);
  244. }
  245. }
  246. return RecordRecTy::get(CommonSuperClasses);
  247. }
  248. RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) {
  249. if (T1 == T2)
  250. return T1;
  251. if (RecordRecTy *RecTy1 = dyn_cast<RecordRecTy>(T1)) {
  252. if (RecordRecTy *RecTy2 = dyn_cast<RecordRecTy>(T2))
  253. return resolveRecordTypes(RecTy1, RecTy2);
  254. }
  255. if (T1->typeIsConvertibleTo(T2))
  256. return T2;
  257. if (T2->typeIsConvertibleTo(T1))
  258. return T1;
  259. if (ListRecTy *ListTy1 = dyn_cast<ListRecTy>(T1)) {
  260. if (ListRecTy *ListTy2 = dyn_cast<ListRecTy>(T2)) {
  261. RecTy* NewType = resolveTypes(ListTy1->getElementType(),
  262. ListTy2->getElementType());
  263. if (NewType)
  264. return NewType->getListTy();
  265. }
  266. }
  267. return nullptr;
  268. }
  269. //===----------------------------------------------------------------------===//
  270. // Initializer implementations
  271. //===----------------------------------------------------------------------===//
  272. void Init::anchor() {}
  273. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  274. LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); }
  275. #endif
  276. UnsetInit *UnsetInit::get() { return &Context->TheUnsetInit; }
  277. Init *UnsetInit::getCastTo(RecTy *Ty) const {
  278. return const_cast<UnsetInit *>(this);
  279. }
  280. Init *UnsetInit::convertInitializerTo(RecTy *Ty) const {
  281. return const_cast<UnsetInit *>(this);
  282. }
  283. BitInit *BitInit::get(bool V) {
  284. return V ? &Context->TrueBitInit : &Context->FalseBitInit;
  285. }
  286. Init *BitInit::convertInitializerTo(RecTy *Ty) const {
  287. if (isa<BitRecTy>(Ty))
  288. return const_cast<BitInit *>(this);
  289. if (isa<IntRecTy>(Ty))
  290. return IntInit::get(getValue());
  291. if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
  292. // Can only convert single bit.
  293. if (BRT->getNumBits() == 1)
  294. return BitsInit::get(const_cast<BitInit *>(this));
  295. }
  296. return nullptr;
  297. }
  298. static void
  299. ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) {
  300. ID.AddInteger(Range.size());
  301. for (Init *I : Range)
  302. ID.AddPointer(I);
  303. }
  304. BitsInit *BitsInit::get(ArrayRef<Init *> Range) {
  305. FoldingSetNodeID ID;
  306. ProfileBitsInit(ID, Range);
  307. void *IP = nullptr;
  308. if (BitsInit *I = Context->TheBitsInitPool.FindNodeOrInsertPos(ID, IP))
  309. return I;
  310. void *Mem = Context->Allocator.Allocate(
  311. totalSizeToAlloc<Init *>(Range.size()), alignof(BitsInit));
  312. BitsInit *I = new(Mem) BitsInit(Range.size());
  313. std::uninitialized_copy(Range.begin(), Range.end(),
  314. I->getTrailingObjects<Init *>());
  315. Context->TheBitsInitPool.InsertNode(I, IP);
  316. return I;
  317. }
  318. void BitsInit::Profile(FoldingSetNodeID &ID) const {
  319. ProfileBitsInit(ID, makeArrayRef(getTrailingObjects<Init *>(), NumBits));
  320. }
  321. Init *BitsInit::convertInitializerTo(RecTy *Ty) const {
  322. if (isa<BitRecTy>(Ty)) {
  323. if (getNumBits() != 1) return nullptr; // Only accept if just one bit!
  324. return getBit(0);
  325. }
  326. if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
  327. // If the number of bits is right, return it. Otherwise we need to expand
  328. // or truncate.
  329. if (getNumBits() != BRT->getNumBits()) return nullptr;
  330. return const_cast<BitsInit *>(this);
  331. }
  332. if (isa<IntRecTy>(Ty)) {
  333. int64_t Result = 0;
  334. for (unsigned i = 0, e = getNumBits(); i != e; ++i)
  335. if (auto *Bit = dyn_cast<BitInit>(getBit(i)))
  336. Result |= static_cast<int64_t>(Bit->getValue()) << i;
  337. else
  338. return nullptr;
  339. return IntInit::get(Result);
  340. }
  341. return nullptr;
  342. }
  343. Init *
  344. BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
  345. SmallVector<Init *, 16> NewBits(Bits.size());
  346. for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
  347. if (Bits[i] >= getNumBits())
  348. return nullptr;
  349. NewBits[i] = getBit(Bits[i]);
  350. }
  351. return BitsInit::get(NewBits);
  352. }
  353. bool BitsInit::isConcrete() const {
  354. for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
  355. if (!getBit(i)->isConcrete())
  356. return false;
  357. }
  358. return true;
  359. }
  360. std::string BitsInit::getAsString() const {
  361. std::string Result = "{ ";
  362. for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
  363. if (i) Result += ", ";
  364. if (Init *Bit = getBit(e-i-1))
  365. Result += Bit->getAsString();
  366. else
  367. Result += "*";
  368. }
  369. return Result + " }";
  370. }
  371. // resolveReferences - If there are any field references that refer to fields
  372. // that have been filled in, we can propagate the values now.
  373. Init *BitsInit::resolveReferences(Resolver &R) const {
  374. bool Changed = false;
  375. SmallVector<Init *, 16> NewBits(getNumBits());
  376. Init *CachedBitVarRef = nullptr;
  377. Init *CachedBitVarResolved = nullptr;
  378. for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
  379. Init *CurBit = getBit(i);
  380. Init *NewBit = CurBit;
  381. if (VarBitInit *CurBitVar = dyn_cast<VarBitInit>(CurBit)) {
  382. if (CurBitVar->getBitVar() != CachedBitVarRef) {
  383. CachedBitVarRef = CurBitVar->getBitVar();
  384. CachedBitVarResolved = CachedBitVarRef->resolveReferences(R);
  385. }
  386. assert(CachedBitVarResolved && "Unresolved bitvar reference");
  387. NewBit = CachedBitVarResolved->getBit(CurBitVar->getBitNum());
  388. } else {
  389. // getBit(0) implicitly converts int and bits<1> values to bit.
  390. NewBit = CurBit->resolveReferences(R)->getBit(0);
  391. }
  392. if (isa<UnsetInit>(NewBit) && R.keepUnsetBits())
  393. NewBit = CurBit;
  394. NewBits[i] = NewBit;
  395. Changed |= CurBit != NewBit;
  396. }
  397. if (Changed)
  398. return BitsInit::get(NewBits);
  399. return const_cast<BitsInit *>(this);
  400. }
  401. IntInit *IntInit::get(int64_t V) {
  402. IntInit *&I = Context->TheIntInitPool[V];
  403. if (!I)
  404. I = new (Context->Allocator) IntInit(V);
  405. return I;
  406. }
  407. std::string IntInit::getAsString() const {
  408. return itostr(Value);
  409. }
  410. static bool canFitInBitfield(int64_t Value, unsigned NumBits) {
  411. // For example, with NumBits == 4, we permit Values from [-7 .. 15].
  412. return (NumBits >= sizeof(Value) * 8) ||
  413. (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1);
  414. }
  415. Init *IntInit::convertInitializerTo(RecTy *Ty) const {
  416. if (isa<IntRecTy>(Ty))
  417. return const_cast<IntInit *>(this);
  418. if (isa<BitRecTy>(Ty)) {
  419. int64_t Val = getValue();
  420. if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit!
  421. return BitInit::get(Val != 0);
  422. }
  423. if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
  424. int64_t Value = getValue();
  425. // Make sure this bitfield is large enough to hold the integer value.
  426. if (!canFitInBitfield(Value, BRT->getNumBits()))
  427. return nullptr;
  428. SmallVector<Init *, 16> NewBits(BRT->getNumBits());
  429. for (unsigned i = 0; i != BRT->getNumBits(); ++i)
  430. NewBits[i] = BitInit::get(Value & ((i < 64) ? (1LL << i) : 0));
  431. return BitsInit::get(NewBits);
  432. }
  433. return nullptr;
  434. }
  435. Init *
  436. IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
  437. SmallVector<Init *, 16> NewBits(Bits.size());
  438. for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
  439. if (Bits[i] >= 64)
  440. return nullptr;
  441. NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i]));
  442. }
  443. return BitsInit::get(NewBits);
  444. }
  445. AnonymousNameInit *AnonymousNameInit::get(unsigned V) {
  446. return new (Context->Allocator) AnonymousNameInit(V);
  447. }
  448. StringInit *AnonymousNameInit::getNameInit() const {
  449. return StringInit::get(getAsString());
  450. }
  451. std::string AnonymousNameInit::getAsString() const {
  452. return "anonymous_" + utostr(Value);
  453. }
  454. Init *AnonymousNameInit::resolveReferences(Resolver &R) const {
  455. auto *Old = const_cast<Init *>(static_cast<const Init *>(this));
  456. auto *New = R.resolve(Old);
  457. New = New ? New : Old;
  458. if (R.isFinal())
  459. if (auto *Anonymous = dyn_cast<AnonymousNameInit>(New))
  460. return Anonymous->getNameInit();
  461. return New;
  462. }
  463. StringInit *StringInit::get(StringRef V, StringFormat Fmt) {
  464. auto &InitMap = Fmt == SF_String ? Context->StringInitStringPool
  465. : Context->StringInitCodePool;
  466. auto &Entry = *InitMap.insert(std::make_pair(V, nullptr)).first;
  467. if (!Entry.second)
  468. Entry.second = new (Context->Allocator) StringInit(Entry.getKey(), Fmt);
  469. return Entry.second;
  470. }
  471. Init *StringInit::convertInitializerTo(RecTy *Ty) const {
  472. if (isa<StringRecTy>(Ty))
  473. return const_cast<StringInit *>(this);
  474. return nullptr;
  475. }
  476. static void ProfileListInit(FoldingSetNodeID &ID,
  477. ArrayRef<Init *> Range,
  478. RecTy *EltTy) {
  479. ID.AddInteger(Range.size());
  480. ID.AddPointer(EltTy);
  481. for (Init *I : Range)
  482. ID.AddPointer(I);
  483. }
  484. ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) {
  485. FoldingSetNodeID ID;
  486. ProfileListInit(ID, Range, EltTy);
  487. void *IP = nullptr;
  488. if (ListInit *I = Context->TheListInitPool.FindNodeOrInsertPos(ID, IP))
  489. return I;
  490. assert(Range.empty() || !isa<TypedInit>(Range[0]) ||
  491. cast<TypedInit>(Range[0])->getType()->typeIsConvertibleTo(EltTy));
  492. void *Mem = Context->Allocator.Allocate(
  493. totalSizeToAlloc<Init *>(Range.size()), alignof(ListInit));
  494. ListInit *I = new (Mem) ListInit(Range.size(), EltTy);
  495. std::uninitialized_copy(Range.begin(), Range.end(),
  496. I->getTrailingObjects<Init *>());
  497. Context->TheListInitPool.InsertNode(I, IP);
  498. return I;
  499. }
  500. void ListInit::Profile(FoldingSetNodeID &ID) const {
  501. RecTy *EltTy = cast<ListRecTy>(getType())->getElementType();
  502. ProfileListInit(ID, getValues(), EltTy);
  503. }
  504. Init *ListInit::convertInitializerTo(RecTy *Ty) const {
  505. if (getType() == Ty)
  506. return const_cast<ListInit*>(this);
  507. if (auto *LRT = dyn_cast<ListRecTy>(Ty)) {
  508. SmallVector<Init*, 8> Elements;
  509. Elements.reserve(getValues().size());
  510. // Verify that all of the elements of the list are subclasses of the
  511. // appropriate class!
  512. bool Changed = false;
  513. RecTy *ElementType = LRT->getElementType();
  514. for (Init *I : getValues())
  515. if (Init *CI = I->convertInitializerTo(ElementType)) {
  516. Elements.push_back(CI);
  517. if (CI != I)
  518. Changed = true;
  519. } else
  520. return nullptr;
  521. if (!Changed)
  522. return const_cast<ListInit*>(this);
  523. return ListInit::get(Elements, ElementType);
  524. }
  525. return nullptr;
  526. }
  527. Init *ListInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
  528. if (Elements.size() == 1) {
  529. if (Elements[0] >= size())
  530. return nullptr;
  531. return getElement(Elements[0]);
  532. }
  533. SmallVector<Init*, 8> Vals;
  534. Vals.reserve(Elements.size());
  535. for (unsigned Element : Elements) {
  536. if (Element >= size())
  537. return nullptr;
  538. Vals.push_back(getElement(Element));
  539. }
  540. return ListInit::get(Vals, getElementType());
  541. }
  542. Record *ListInit::getElementAsRecord(unsigned i) const {
  543. assert(i < NumValues && "List element index out of range!");
  544. DefInit *DI = dyn_cast<DefInit>(getElement(i));
  545. if (!DI)
  546. PrintFatalError("Expected record in list!");
  547. return DI->getDef();
  548. }
  549. Init *ListInit::resolveReferences(Resolver &R) const {
  550. SmallVector<Init*, 8> Resolved;
  551. Resolved.reserve(size());
  552. bool Changed = false;
  553. for (Init *CurElt : getValues()) {
  554. Init *E = CurElt->resolveReferences(R);
  555. Changed |= E != CurElt;
  556. Resolved.push_back(E);
  557. }
  558. if (Changed)
  559. return ListInit::get(Resolved, getElementType());
  560. return const_cast<ListInit *>(this);
  561. }
  562. bool ListInit::isComplete() const {
  563. for (Init *Element : *this) {
  564. if (!Element->isComplete())
  565. return false;
  566. }
  567. return true;
  568. }
  569. bool ListInit::isConcrete() const {
  570. for (Init *Element : *this) {
  571. if (!Element->isConcrete())
  572. return false;
  573. }
  574. return true;
  575. }
  576. std::string ListInit::getAsString() const {
  577. std::string Result = "[";
  578. const char *sep = "";
  579. for (Init *Element : *this) {
  580. Result += sep;
  581. sep = ", ";
  582. Result += Element->getAsString();
  583. }
  584. return Result + "]";
  585. }
  586. Init *OpInit::getBit(unsigned Bit) const {
  587. if (getType() == BitRecTy::get())
  588. return const_cast<OpInit*>(this);
  589. return VarBitInit::get(const_cast<OpInit*>(this), Bit);
  590. }
  591. static void
  592. ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *Op, RecTy *Type) {
  593. ID.AddInteger(Opcode);
  594. ID.AddPointer(Op);
  595. ID.AddPointer(Type);
  596. }
  597. UnOpInit *UnOpInit::get(UnaryOp Opc, Init *LHS, RecTy *Type) {
  598. FoldingSetNodeID ID;
  599. ProfileUnOpInit(ID, Opc, LHS, Type);
  600. void *IP = nullptr;
  601. if (UnOpInit *I = Context->TheUnOpInitPool.FindNodeOrInsertPos(ID, IP))
  602. return I;
  603. UnOpInit *I = new (Context->Allocator) UnOpInit(Opc, LHS, Type);
  604. Context->TheUnOpInitPool.InsertNode(I, IP);
  605. return I;
  606. }
  607. void UnOpInit::Profile(FoldingSetNodeID &ID) const {
  608. ProfileUnOpInit(ID, getOpcode(), getOperand(), getType());
  609. }
  610. Init *UnOpInit::Fold(Record *CurRec, bool IsFinal) const {
  611. switch (getOpcode()) {
  612. case CAST:
  613. if (isa<StringRecTy>(getType())) {
  614. if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
  615. return LHSs;
  616. if (DefInit *LHSd = dyn_cast<DefInit>(LHS))
  617. return StringInit::get(LHSd->getAsString());
  618. if (IntInit *LHSi =
  619. dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())))
  620. return StringInit::get(LHSi->getAsString());
  621. } else if (isa<RecordRecTy>(getType())) {
  622. if (StringInit *Name = dyn_cast<StringInit>(LHS)) {
  623. if (!CurRec && !IsFinal)
  624. break;
  625. assert(CurRec && "NULL pointer");
  626. Record *D;
  627. // Self-references are allowed, but their resolution is delayed until
  628. // the final resolve to ensure that we get the correct type for them.
  629. auto *Anonymous = dyn_cast<AnonymousNameInit>(CurRec->getNameInit());
  630. if (Name == CurRec->getNameInit() ||
  631. (Anonymous && Name == Anonymous->getNameInit())) {
  632. if (!IsFinal)
  633. break;
  634. D = CurRec;
  635. } else {
  636. D = CurRec->getRecords().getDef(Name->getValue());
  637. if (!D) {
  638. if (IsFinal)
  639. PrintFatalError(CurRec->getLoc(),
  640. Twine("Undefined reference to record: '") +
  641. Name->getValue() + "'\n");
  642. break;
  643. }
  644. }
  645. DefInit *DI = DefInit::get(D);
  646. if (!DI->getType()->typeIsA(getType())) {
  647. PrintFatalError(CurRec->getLoc(),
  648. Twine("Expected type '") +
  649. getType()->getAsString() + "', got '" +
  650. DI->getType()->getAsString() + "' in: " +
  651. getAsString() + "\n");
  652. }
  653. return DI;
  654. }
  655. }
  656. if (Init *NewInit = LHS->convertInitializerTo(getType()))
  657. return NewInit;
  658. break;
  659. case NOT:
  660. if (IntInit *LHSi =
  661. dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get())))
  662. return IntInit::get(LHSi->getValue() ? 0 : 1);
  663. break;
  664. case HEAD:
  665. if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
  666. assert(!LHSl->empty() && "Empty list in head");
  667. return LHSl->getElement(0);
  668. }
  669. break;
  670. case TAIL:
  671. if (ListInit *LHSl = dyn_cast<ListInit>(LHS)) {
  672. assert(!LHSl->empty() && "Empty list in tail");
  673. // Note the +1. We can't just pass the result of getValues()
  674. // directly.
  675. return ListInit::get(LHSl->getValues().slice(1), LHSl->getElementType());
  676. }
  677. break;
  678. case SIZE:
  679. if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
  680. return IntInit::get(LHSl->size());
  681. if (DagInit *LHSd = dyn_cast<DagInit>(LHS))
  682. return IntInit::get(LHSd->arg_size());
  683. if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
  684. return IntInit::get(LHSs->getValue().size());
  685. break;
  686. case EMPTY:
  687. if (ListInit *LHSl = dyn_cast<ListInit>(LHS))
  688. return IntInit::get(LHSl->empty());
  689. if (DagInit *LHSd = dyn_cast<DagInit>(LHS))
  690. return IntInit::get(LHSd->arg_empty());
  691. if (StringInit *LHSs = dyn_cast<StringInit>(LHS))
  692. return IntInit::get(LHSs->getValue().empty());
  693. break;
  694. case GETDAGOP:
  695. if (DagInit *Dag = dyn_cast<DagInit>(LHS)) {
  696. DefInit *DI = DefInit::get(Dag->getOperatorAsDef({}));
  697. if (!DI->getType()->typeIsA(getType())) {
  698. PrintFatalError(CurRec->getLoc(),
  699. Twine("Expected type '") +
  700. getType()->getAsString() + "', got '" +
  701. DI->getType()->getAsString() + "' in: " +
  702. getAsString() + "\n");
  703. } else {
  704. return DI;
  705. }
  706. }
  707. break;
  708. }
  709. return const_cast<UnOpInit *>(this);
  710. }
  711. Init *UnOpInit::resolveReferences(Resolver &R) const {
  712. Init *lhs = LHS->resolveReferences(R);
  713. if (LHS != lhs || (R.isFinal() && getOpcode() == CAST))
  714. return (UnOpInit::get(getOpcode(), lhs, getType()))
  715. ->Fold(R.getCurrentRecord(), R.isFinal());
  716. return const_cast<UnOpInit *>(this);
  717. }
  718. std::string UnOpInit::getAsString() const {
  719. std::string Result;
  720. switch (getOpcode()) {
  721. case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break;
  722. case NOT: Result = "!not"; break;
  723. case HEAD: Result = "!head"; break;
  724. case TAIL: Result = "!tail"; break;
  725. case SIZE: Result = "!size"; break;
  726. case EMPTY: Result = "!empty"; break;
  727. case GETDAGOP: Result = "!getdagop"; break;
  728. }
  729. return Result + "(" + LHS->getAsString() + ")";
  730. }
  731. static void
  732. ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *RHS,
  733. RecTy *Type) {
  734. ID.AddInteger(Opcode);
  735. ID.AddPointer(LHS);
  736. ID.AddPointer(RHS);
  737. ID.AddPointer(Type);
  738. }
  739. BinOpInit *BinOpInit::get(BinaryOp Opc, Init *LHS, Init *RHS, RecTy *Type) {
  740. FoldingSetNodeID ID;
  741. ProfileBinOpInit(ID, Opc, LHS, RHS, Type);
  742. void *IP = nullptr;
  743. if (BinOpInit *I = Context->TheBinOpInitPool.FindNodeOrInsertPos(ID, IP))
  744. return I;
  745. BinOpInit *I = new (Context->Allocator) BinOpInit(Opc, LHS, RHS, Type);
  746. Context->TheBinOpInitPool.InsertNode(I, IP);
  747. return I;
  748. }
  749. void BinOpInit::Profile(FoldingSetNodeID &ID) const {
  750. ProfileBinOpInit(ID, getOpcode(), getLHS(), getRHS(), getType());
  751. }
  752. static StringInit *ConcatStringInits(const StringInit *I0,
  753. const StringInit *I1) {
  754. SmallString<80> Concat(I0->getValue());
  755. Concat.append(I1->getValue());
  756. return StringInit::get(Concat,
  757. StringInit::determineFormat(I0->getFormat(),
  758. I1->getFormat()));
  759. }
  760. static StringInit *interleaveStringList(const ListInit *List,
  761. const StringInit *Delim) {
  762. if (List->size() == 0)
  763. return StringInit::get("");
  764. StringInit *Element = dyn_cast<StringInit>(List->getElement(0));
  765. if (!Element)
  766. return nullptr;
  767. SmallString<80> Result(Element->getValue());
  768. StringInit::StringFormat Fmt = StringInit::SF_String;
  769. for (unsigned I = 1, E = List->size(); I < E; ++I) {
  770. Result.append(Delim->getValue());
  771. StringInit *Element = dyn_cast<StringInit>(List->getElement(I));
  772. if (!Element)
  773. return nullptr;
  774. Result.append(Element->getValue());
  775. Fmt = StringInit::determineFormat(Fmt, Element->getFormat());
  776. }
  777. return StringInit::get(Result, Fmt);
  778. }
  779. static StringInit *interleaveIntList(const ListInit *List,
  780. const StringInit *Delim) {
  781. if (List->size() == 0)
  782. return StringInit::get("");
  783. IntInit *Element =
  784. dyn_cast_or_null<IntInit>(List->getElement(0)
  785. ->convertInitializerTo(IntRecTy::get()));
  786. if (!Element)
  787. return nullptr;
  788. SmallString<80> Result(Element->getAsString());
  789. for (unsigned I = 1, E = List->size(); I < E; ++I) {
  790. Result.append(Delim->getValue());
  791. IntInit *Element =
  792. dyn_cast_or_null<IntInit>(List->getElement(I)
  793. ->convertInitializerTo(IntRecTy::get()));
  794. if (!Element)
  795. return nullptr;
  796. Result.append(Element->getAsString());
  797. }
  798. return StringInit::get(Result);
  799. }
  800. Init *BinOpInit::getStrConcat(Init *I0, Init *I1) {
  801. // Shortcut for the common case of concatenating two strings.
  802. if (const StringInit *I0s = dyn_cast<StringInit>(I0))
  803. if (const StringInit *I1s = dyn_cast<StringInit>(I1))
  804. return ConcatStringInits(I0s, I1s);
  805. return BinOpInit::get(BinOpInit::STRCONCAT, I0, I1, StringRecTy::get());
  806. }
  807. static ListInit *ConcatListInits(const ListInit *LHS,
  808. const ListInit *RHS) {
  809. SmallVector<Init *, 8> Args;
  810. llvm::append_range(Args, *LHS);
  811. llvm::append_range(Args, *RHS);
  812. return ListInit::get(Args, LHS->getElementType());
  813. }
  814. Init *BinOpInit::getListConcat(TypedInit *LHS, Init *RHS) {
  815. assert(isa<ListRecTy>(LHS->getType()) && "First arg must be a list");
  816. // Shortcut for the common case of concatenating two lists.
  817. if (const ListInit *LHSList = dyn_cast<ListInit>(LHS))
  818. if (const ListInit *RHSList = dyn_cast<ListInit>(RHS))
  819. return ConcatListInits(LHSList, RHSList);
  820. return BinOpInit::get(BinOpInit::LISTCONCAT, LHS, RHS, LHS->getType());
  821. }
  822. Init *BinOpInit::Fold(Record *CurRec) const {
  823. switch (getOpcode()) {
  824. case CONCAT: {
  825. DagInit *LHSs = dyn_cast<DagInit>(LHS);
  826. DagInit *RHSs = dyn_cast<DagInit>(RHS);
  827. if (LHSs && RHSs) {
  828. DefInit *LOp = dyn_cast<DefInit>(LHSs->getOperator());
  829. DefInit *ROp = dyn_cast<DefInit>(RHSs->getOperator());
  830. if ((!LOp && !isa<UnsetInit>(LHSs->getOperator())) ||
  831. (!ROp && !isa<UnsetInit>(RHSs->getOperator())))
  832. break;
  833. if (LOp && ROp && LOp->getDef() != ROp->getDef()) {
  834. PrintFatalError(Twine("Concatenated Dag operators do not match: '") +
  835. LHSs->getAsString() + "' vs. '" + RHSs->getAsString() +
  836. "'");
  837. }
  838. Init *Op = LOp ? LOp : ROp;
  839. if (!Op)
  840. Op = UnsetInit::get();
  841. SmallVector<Init*, 8> Args;
  842. SmallVector<StringInit*, 8> ArgNames;
  843. for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) {
  844. Args.push_back(LHSs->getArg(i));
  845. ArgNames.push_back(LHSs->getArgName(i));
  846. }
  847. for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) {
  848. Args.push_back(RHSs->getArg(i));
  849. ArgNames.push_back(RHSs->getArgName(i));
  850. }
  851. return DagInit::get(Op, nullptr, Args, ArgNames);
  852. }
  853. break;
  854. }
  855. case LISTCONCAT: {
  856. ListInit *LHSs = dyn_cast<ListInit>(LHS);
  857. ListInit *RHSs = dyn_cast<ListInit>(RHS);
  858. if (LHSs && RHSs) {
  859. SmallVector<Init *, 8> Args;
  860. llvm::append_range(Args, *LHSs);
  861. llvm::append_range(Args, *RHSs);
  862. return ListInit::get(Args, LHSs->getElementType());
  863. }
  864. break;
  865. }
  866. case LISTSPLAT: {
  867. TypedInit *Value = dyn_cast<TypedInit>(LHS);
  868. IntInit *Size = dyn_cast<IntInit>(RHS);
  869. if (Value && Size) {
  870. SmallVector<Init *, 8> Args(Size->getValue(), Value);
  871. return ListInit::get(Args, Value->getType());
  872. }
  873. break;
  874. }
  875. case STRCONCAT: {
  876. StringInit *LHSs = dyn_cast<StringInit>(LHS);
  877. StringInit *RHSs = dyn_cast<StringInit>(RHS);
  878. if (LHSs && RHSs)
  879. return ConcatStringInits(LHSs, RHSs);
  880. break;
  881. }
  882. case INTERLEAVE: {
  883. ListInit *List = dyn_cast<ListInit>(LHS);
  884. StringInit *Delim = dyn_cast<StringInit>(RHS);
  885. if (List && Delim) {
  886. StringInit *Result;
  887. if (isa<StringRecTy>(List->getElementType()))
  888. Result = interleaveStringList(List, Delim);
  889. else
  890. Result = interleaveIntList(List, Delim);
  891. if (Result)
  892. return Result;
  893. }
  894. break;
  895. }
  896. case EQ:
  897. case NE:
  898. case LE:
  899. case LT:
  900. case GE:
  901. case GT: {
  902. // First see if we have two bit, bits, or int.
  903. IntInit *LHSi =
  904. dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
  905. IntInit *RHSi =
  906. dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
  907. if (LHSi && RHSi) {
  908. bool Result;
  909. switch (getOpcode()) {
  910. case EQ: Result = LHSi->getValue() == RHSi->getValue(); break;
  911. case NE: Result = LHSi->getValue() != RHSi->getValue(); break;
  912. case LE: Result = LHSi->getValue() <= RHSi->getValue(); break;
  913. case LT: Result = LHSi->getValue() < RHSi->getValue(); break;
  914. case GE: Result = LHSi->getValue() >= RHSi->getValue(); break;
  915. case GT: Result = LHSi->getValue() > RHSi->getValue(); break;
  916. default: llvm_unreachable("unhandled comparison");
  917. }
  918. return BitInit::get(Result);
  919. }
  920. // Next try strings.
  921. StringInit *LHSs = dyn_cast<StringInit>(LHS);
  922. StringInit *RHSs = dyn_cast<StringInit>(RHS);
  923. if (LHSs && RHSs) {
  924. bool Result;
  925. switch (getOpcode()) {
  926. case EQ: Result = LHSs->getValue() == RHSs->getValue(); break;
  927. case NE: Result = LHSs->getValue() != RHSs->getValue(); break;
  928. case LE: Result = LHSs->getValue() <= RHSs->getValue(); break;
  929. case LT: Result = LHSs->getValue() < RHSs->getValue(); break;
  930. case GE: Result = LHSs->getValue() >= RHSs->getValue(); break;
  931. case GT: Result = LHSs->getValue() > RHSs->getValue(); break;
  932. default: llvm_unreachable("unhandled comparison");
  933. }
  934. return BitInit::get(Result);
  935. }
  936. // Finally, !eq and !ne can be used with records.
  937. if (getOpcode() == EQ || getOpcode() == NE) {
  938. DefInit *LHSd = dyn_cast<DefInit>(LHS);
  939. DefInit *RHSd = dyn_cast<DefInit>(RHS);
  940. if (LHSd && RHSd)
  941. return BitInit::get((getOpcode() == EQ) ? LHSd == RHSd
  942. : LHSd != RHSd);
  943. }
  944. break;
  945. }
  946. case SETDAGOP: {
  947. DagInit *Dag = dyn_cast<DagInit>(LHS);
  948. DefInit *Op = dyn_cast<DefInit>(RHS);
  949. if (Dag && Op) {
  950. SmallVector<Init*, 8> Args;
  951. SmallVector<StringInit*, 8> ArgNames;
  952. for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) {
  953. Args.push_back(Dag->getArg(i));
  954. ArgNames.push_back(Dag->getArgName(i));
  955. }
  956. return DagInit::get(Op, nullptr, Args, ArgNames);
  957. }
  958. break;
  959. }
  960. case ADD:
  961. case SUB:
  962. case MUL:
  963. case AND:
  964. case OR:
  965. case XOR:
  966. case SHL:
  967. case SRA:
  968. case SRL: {
  969. IntInit *LHSi =
  970. dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
  971. IntInit *RHSi =
  972. dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
  973. if (LHSi && RHSi) {
  974. int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
  975. int64_t Result;
  976. switch (getOpcode()) {
  977. default: llvm_unreachable("Bad opcode!");
  978. case ADD: Result = LHSv + RHSv; break;
  979. case SUB: Result = LHSv - RHSv; break;
  980. case MUL: Result = LHSv * RHSv; break;
  981. case AND: Result = LHSv & RHSv; break;
  982. case OR: Result = LHSv | RHSv; break;
  983. case XOR: Result = LHSv ^ RHSv; break;
  984. case SHL: Result = (uint64_t)LHSv << (uint64_t)RHSv; break;
  985. case SRA: Result = LHSv >> RHSv; break;
  986. case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
  987. }
  988. return IntInit::get(Result);
  989. }
  990. break;
  991. }
  992. }
  993. return const_cast<BinOpInit *>(this);
  994. }
  995. Init *BinOpInit::resolveReferences(Resolver &R) const {
  996. Init *lhs = LHS->resolveReferences(R);
  997. Init *rhs = RHS->resolveReferences(R);
  998. if (LHS != lhs || RHS != rhs)
  999. return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
  1000. ->Fold(R.getCurrentRecord());
  1001. return const_cast<BinOpInit *>(this);
  1002. }
  1003. std::string BinOpInit::getAsString() const {
  1004. std::string Result;
  1005. switch (getOpcode()) {
  1006. case CONCAT: Result = "!con"; break;
  1007. case ADD: Result = "!add"; break;
  1008. case SUB: Result = "!sub"; break;
  1009. case MUL: Result = "!mul"; break;
  1010. case AND: Result = "!and"; break;
  1011. case OR: Result = "!or"; break;
  1012. case XOR: Result = "!xor"; break;
  1013. case SHL: Result = "!shl"; break;
  1014. case SRA: Result = "!sra"; break;
  1015. case SRL: Result = "!srl"; break;
  1016. case EQ: Result = "!eq"; break;
  1017. case NE: Result = "!ne"; break;
  1018. case LE: Result = "!le"; break;
  1019. case LT: Result = "!lt"; break;
  1020. case GE: Result = "!ge"; break;
  1021. case GT: Result = "!gt"; break;
  1022. case LISTCONCAT: Result = "!listconcat"; break;
  1023. case LISTSPLAT: Result = "!listsplat"; break;
  1024. case STRCONCAT: Result = "!strconcat"; break;
  1025. case INTERLEAVE: Result = "!interleave"; break;
  1026. case SETDAGOP: Result = "!setdagop"; break;
  1027. }
  1028. return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")";
  1029. }
  1030. static void
  1031. ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, Init *LHS, Init *MHS,
  1032. Init *RHS, RecTy *Type) {
  1033. ID.AddInteger(Opcode);
  1034. ID.AddPointer(LHS);
  1035. ID.AddPointer(MHS);
  1036. ID.AddPointer(RHS);
  1037. ID.AddPointer(Type);
  1038. }
  1039. TernOpInit *TernOpInit::get(TernaryOp Opc, Init *LHS, Init *MHS, Init *RHS,
  1040. RecTy *Type) {
  1041. FoldingSetNodeID ID;
  1042. ProfileTernOpInit(ID, Opc, LHS, MHS, RHS, Type);
  1043. void *IP = nullptr;
  1044. if (TernOpInit *I = Context->TheTernOpInitPool.FindNodeOrInsertPos(ID, IP))
  1045. return I;
  1046. TernOpInit *I = new (Context->Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type);
  1047. Context->TheTernOpInitPool.InsertNode(I, IP);
  1048. return I;
  1049. }
  1050. void TernOpInit::Profile(FoldingSetNodeID &ID) const {
  1051. ProfileTernOpInit(ID, getOpcode(), getLHS(), getMHS(), getRHS(), getType());
  1052. }
  1053. static Init *ItemApply(Init *LHS, Init *MHSe, Init *RHS, Record *CurRec) {
  1054. MapResolver R(CurRec);
  1055. R.set(LHS, MHSe);
  1056. return RHS->resolveReferences(R);
  1057. }
  1058. static Init *ForeachDagApply(Init *LHS, DagInit *MHSd, Init *RHS,
  1059. Record *CurRec) {
  1060. bool Change = false;
  1061. Init *Val = ItemApply(LHS, MHSd->getOperator(), RHS, CurRec);
  1062. if (Val != MHSd->getOperator())
  1063. Change = true;
  1064. SmallVector<std::pair<Init *, StringInit *>, 8> NewArgs;
  1065. for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) {
  1066. Init *Arg = MHSd->getArg(i);
  1067. Init *NewArg;
  1068. StringInit *ArgName = MHSd->getArgName(i);
  1069. if (DagInit *Argd = dyn_cast<DagInit>(Arg))
  1070. NewArg = ForeachDagApply(LHS, Argd, RHS, CurRec);
  1071. else
  1072. NewArg = ItemApply(LHS, Arg, RHS, CurRec);
  1073. NewArgs.push_back(std::make_pair(NewArg, ArgName));
  1074. if (Arg != NewArg)
  1075. Change = true;
  1076. }
  1077. if (Change)
  1078. return DagInit::get(Val, nullptr, NewArgs);
  1079. return MHSd;
  1080. }
  1081. // Applies RHS to all elements of MHS, using LHS as a temp variable.
  1082. static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
  1083. Record *CurRec) {
  1084. if (DagInit *MHSd = dyn_cast<DagInit>(MHS))
  1085. return ForeachDagApply(LHS, MHSd, RHS, CurRec);
  1086. if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) {
  1087. SmallVector<Init *, 8> NewList(MHSl->begin(), MHSl->end());
  1088. for (Init *&Item : NewList) {
  1089. Init *NewItem = ItemApply(LHS, Item, RHS, CurRec);
  1090. if (NewItem != Item)
  1091. Item = NewItem;
  1092. }
  1093. return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
  1094. }
  1095. return nullptr;
  1096. }
  1097. // Evaluates RHS for all elements of MHS, using LHS as a temp variable.
  1098. // Creates a new list with the elements that evaluated to true.
  1099. static Init *FilterHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type,
  1100. Record *CurRec) {
  1101. if (ListInit *MHSl = dyn_cast<ListInit>(MHS)) {
  1102. SmallVector<Init *, 8> NewList;
  1103. for (Init *Item : MHSl->getValues()) {
  1104. Init *Include = ItemApply(LHS, Item, RHS, CurRec);
  1105. if (!Include)
  1106. return nullptr;
  1107. if (IntInit *IncludeInt = dyn_cast_or_null<IntInit>(
  1108. Include->convertInitializerTo(IntRecTy::get()))) {
  1109. if (IncludeInt->getValue())
  1110. NewList.push_back(Item);
  1111. } else {
  1112. return nullptr;
  1113. }
  1114. }
  1115. return ListInit::get(NewList, cast<ListRecTy>(Type)->getElementType());
  1116. }
  1117. return nullptr;
  1118. }
  1119. Init *TernOpInit::Fold(Record *CurRec) const {
  1120. switch (getOpcode()) {
  1121. case SUBST: {
  1122. DefInit *LHSd = dyn_cast<DefInit>(LHS);
  1123. VarInit *LHSv = dyn_cast<VarInit>(LHS);
  1124. StringInit *LHSs = dyn_cast<StringInit>(LHS);
  1125. DefInit *MHSd = dyn_cast<DefInit>(MHS);
  1126. VarInit *MHSv = dyn_cast<VarInit>(MHS);
  1127. StringInit *MHSs = dyn_cast<StringInit>(MHS);
  1128. DefInit *RHSd = dyn_cast<DefInit>(RHS);
  1129. VarInit *RHSv = dyn_cast<VarInit>(RHS);
  1130. StringInit *RHSs = dyn_cast<StringInit>(RHS);
  1131. if (LHSd && MHSd && RHSd) {
  1132. Record *Val = RHSd->getDef();
  1133. if (LHSd->getAsString() == RHSd->getAsString())
  1134. Val = MHSd->getDef();
  1135. return DefInit::get(Val);
  1136. }
  1137. if (LHSv && MHSv && RHSv) {
  1138. std::string Val = std::string(RHSv->getName());
  1139. if (LHSv->getAsString() == RHSv->getAsString())
  1140. Val = std::string(MHSv->getName());
  1141. return VarInit::get(Val, getType());
  1142. }
  1143. if (LHSs && MHSs && RHSs) {
  1144. std::string Val = std::string(RHSs->getValue());
  1145. std::string::size_type found;
  1146. std::string::size_type idx = 0;
  1147. while (true) {
  1148. found = Val.find(std::string(LHSs->getValue()), idx);
  1149. if (found == std::string::npos)
  1150. break;
  1151. Val.replace(found, LHSs->getValue().size(),
  1152. std::string(MHSs->getValue()));
  1153. idx = found + MHSs->getValue().size();
  1154. }
  1155. return StringInit::get(Val);
  1156. }
  1157. break;
  1158. }
  1159. case FOREACH: {
  1160. if (Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), CurRec))
  1161. return Result;
  1162. break;
  1163. }
  1164. case FILTER: {
  1165. if (Init *Result = FilterHelper(LHS, MHS, RHS, getType(), CurRec))
  1166. return Result;
  1167. break;
  1168. }
  1169. case IF: {
  1170. if (IntInit *LHSi = dyn_cast_or_null<IntInit>(
  1171. LHS->convertInitializerTo(IntRecTy::get()))) {
  1172. if (LHSi->getValue())
  1173. return MHS;
  1174. return RHS;
  1175. }
  1176. break;
  1177. }
  1178. case DAG: {
  1179. ListInit *MHSl = dyn_cast<ListInit>(MHS);
  1180. ListInit *RHSl = dyn_cast<ListInit>(RHS);
  1181. bool MHSok = MHSl || isa<UnsetInit>(MHS);
  1182. bool RHSok = RHSl || isa<UnsetInit>(RHS);
  1183. if (isa<UnsetInit>(MHS) && isa<UnsetInit>(RHS))
  1184. break; // Typically prevented by the parser, but might happen with template args
  1185. if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) {
  1186. SmallVector<std::pair<Init *, StringInit *>, 8> Children;
  1187. unsigned Size = MHSl ? MHSl->size() : RHSl->size();
  1188. for (unsigned i = 0; i != Size; ++i) {
  1189. Init *Node = MHSl ? MHSl->getElement(i) : UnsetInit::get();
  1190. Init *Name = RHSl ? RHSl->getElement(i) : UnsetInit::get();
  1191. if (!isa<StringInit>(Name) && !isa<UnsetInit>(Name))
  1192. return const_cast<TernOpInit *>(this);
  1193. Children.emplace_back(Node, dyn_cast<StringInit>(Name));
  1194. }
  1195. return DagInit::get(LHS, nullptr, Children);
  1196. }
  1197. break;
  1198. }
  1199. case SUBSTR: {
  1200. StringInit *LHSs = dyn_cast<StringInit>(LHS);
  1201. IntInit *MHSi = dyn_cast<IntInit>(MHS);
  1202. IntInit *RHSi = dyn_cast<IntInit>(RHS);
  1203. if (LHSs && MHSi && RHSi) {
  1204. int64_t StringSize = LHSs->getValue().size();
  1205. int64_t Start = MHSi->getValue();
  1206. int64_t Length = RHSi->getValue();
  1207. if (Start < 0 || Start > StringSize)
  1208. PrintError(CurRec->getLoc(),
  1209. Twine("!substr start position is out of range 0...") +
  1210. std::to_string(StringSize) + ": " +
  1211. std::to_string(Start));
  1212. if (Length < 0)
  1213. PrintError(CurRec->getLoc(), "!substr length must be nonnegative");
  1214. return StringInit::get(LHSs->getValue().substr(Start, Length),
  1215. LHSs->getFormat());
  1216. }
  1217. break;
  1218. }
  1219. case FIND: {
  1220. StringInit *LHSs = dyn_cast<StringInit>(LHS);
  1221. StringInit *MHSs = dyn_cast<StringInit>(MHS);
  1222. IntInit *RHSi = dyn_cast<IntInit>(RHS);
  1223. if (LHSs && MHSs && RHSi) {
  1224. int64_t SourceSize = LHSs->getValue().size();
  1225. int64_t Start = RHSi->getValue();
  1226. if (Start < 0 || Start > SourceSize)
  1227. PrintError(CurRec->getLoc(),
  1228. Twine("!find start position is out of range 0...") +
  1229. std::to_string(SourceSize) + ": " +
  1230. std::to_string(Start));
  1231. auto I = LHSs->getValue().find(MHSs->getValue(), Start);
  1232. if (I == std::string::npos)
  1233. return IntInit::get(-1);
  1234. return IntInit::get(I);
  1235. }
  1236. break;
  1237. }
  1238. }
  1239. return const_cast<TernOpInit *>(this);
  1240. }
  1241. Init *TernOpInit::resolveReferences(Resolver &R) const {
  1242. Init *lhs = LHS->resolveReferences(R);
  1243. if (getOpcode() == IF && lhs != LHS) {
  1244. if (IntInit *Value = dyn_cast_or_null<IntInit>(
  1245. lhs->convertInitializerTo(IntRecTy::get()))) {
  1246. // Short-circuit
  1247. if (Value->getValue())
  1248. return MHS->resolveReferences(R);
  1249. return RHS->resolveReferences(R);
  1250. }
  1251. }
  1252. Init *mhs = MHS->resolveReferences(R);
  1253. Init *rhs;
  1254. if (getOpcode() == FOREACH || getOpcode() == FILTER) {
  1255. ShadowResolver SR(R);
  1256. SR.addShadow(lhs);
  1257. rhs = RHS->resolveReferences(SR);
  1258. } else {
  1259. rhs = RHS->resolveReferences(R);
  1260. }
  1261. if (LHS != lhs || MHS != mhs || RHS != rhs)
  1262. return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, getType()))
  1263. ->Fold(R.getCurrentRecord());
  1264. return const_cast<TernOpInit *>(this);
  1265. }
  1266. std::string TernOpInit::getAsString() const {
  1267. std::string Result;
  1268. bool UnquotedLHS = false;
  1269. switch (getOpcode()) {
  1270. case DAG: Result = "!dag"; break;
  1271. case FILTER: Result = "!filter"; UnquotedLHS = true; break;
  1272. case FOREACH: Result = "!foreach"; UnquotedLHS = true; break;
  1273. case IF: Result = "!if"; break;
  1274. case SUBST: Result = "!subst"; break;
  1275. case SUBSTR: Result = "!substr"; break;
  1276. case FIND: Result = "!find"; break;
  1277. }
  1278. return (Result + "(" +
  1279. (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) +
  1280. ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")");
  1281. }
  1282. static void ProfileFoldOpInit(FoldingSetNodeID &ID, Init *Start, Init *List,
  1283. Init *A, Init *B, Init *Expr, RecTy *Type) {
  1284. ID.AddPointer(Start);
  1285. ID.AddPointer(List);
  1286. ID.AddPointer(A);
  1287. ID.AddPointer(B);
  1288. ID.AddPointer(Expr);
  1289. ID.AddPointer(Type);
  1290. }
  1291. FoldOpInit *FoldOpInit::get(Init *Start, Init *List, Init *A, Init *B,
  1292. Init *Expr, RecTy *Type) {
  1293. FoldingSetNodeID ID;
  1294. ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type);
  1295. void *IP = nullptr;
  1296. if (FoldOpInit *I = Context->TheFoldOpInitPool.FindNodeOrInsertPos(ID, IP))
  1297. return I;
  1298. FoldOpInit *I =
  1299. new (Context->Allocator) FoldOpInit(Start, List, A, B, Expr, Type);
  1300. Context->TheFoldOpInitPool.InsertNode(I, IP);
  1301. return I;
  1302. }
  1303. void FoldOpInit::Profile(FoldingSetNodeID &ID) const {
  1304. ProfileFoldOpInit(ID, Start, List, A, B, Expr, getType());
  1305. }
  1306. Init *FoldOpInit::Fold(Record *CurRec) const {
  1307. if (ListInit *LI = dyn_cast<ListInit>(List)) {
  1308. Init *Accum = Start;
  1309. for (Init *Elt : *LI) {
  1310. MapResolver R(CurRec);
  1311. R.set(A, Accum);
  1312. R.set(B, Elt);
  1313. Accum = Expr->resolveReferences(R);
  1314. }
  1315. return Accum;
  1316. }
  1317. return const_cast<FoldOpInit *>(this);
  1318. }
  1319. Init *FoldOpInit::resolveReferences(Resolver &R) const {
  1320. Init *NewStart = Start->resolveReferences(R);
  1321. Init *NewList = List->resolveReferences(R);
  1322. ShadowResolver SR(R);
  1323. SR.addShadow(A);
  1324. SR.addShadow(B);
  1325. Init *NewExpr = Expr->resolveReferences(SR);
  1326. if (Start == NewStart && List == NewList && Expr == NewExpr)
  1327. return const_cast<FoldOpInit *>(this);
  1328. return get(NewStart, NewList, A, B, NewExpr, getType())
  1329. ->Fold(R.getCurrentRecord());
  1330. }
  1331. Init *FoldOpInit::getBit(unsigned Bit) const {
  1332. return VarBitInit::get(const_cast<FoldOpInit *>(this), Bit);
  1333. }
  1334. std::string FoldOpInit::getAsString() const {
  1335. return (Twine("!foldl(") + Start->getAsString() + ", " + List->getAsString() +
  1336. ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() +
  1337. ", " + Expr->getAsString() + ")")
  1338. .str();
  1339. }
  1340. static void ProfileIsAOpInit(FoldingSetNodeID &ID, RecTy *CheckType,
  1341. Init *Expr) {
  1342. ID.AddPointer(CheckType);
  1343. ID.AddPointer(Expr);
  1344. }
  1345. IsAOpInit *IsAOpInit::get(RecTy *CheckType, Init *Expr) {
  1346. FoldingSetNodeID ID;
  1347. ProfileIsAOpInit(ID, CheckType, Expr);
  1348. void *IP = nullptr;
  1349. if (IsAOpInit *I = Context->TheIsAOpInitPool.FindNodeOrInsertPos(ID, IP))
  1350. return I;
  1351. IsAOpInit *I = new (Context->Allocator) IsAOpInit(CheckType, Expr);
  1352. Context->TheIsAOpInitPool.InsertNode(I, IP);
  1353. return I;
  1354. }
  1355. void IsAOpInit::Profile(FoldingSetNodeID &ID) const {
  1356. ProfileIsAOpInit(ID, CheckType, Expr);
  1357. }
  1358. Init *IsAOpInit::Fold() const {
  1359. if (TypedInit *TI = dyn_cast<TypedInit>(Expr)) {
  1360. // Is the expression type known to be (a subclass of) the desired type?
  1361. if (TI->getType()->typeIsConvertibleTo(CheckType))
  1362. return IntInit::get(1);
  1363. if (isa<RecordRecTy>(CheckType)) {
  1364. // If the target type is not a subclass of the expression type, or if
  1365. // the expression has fully resolved to a record, we know that it can't
  1366. // be of the required type.
  1367. if (!CheckType->typeIsConvertibleTo(TI->getType()) || isa<DefInit>(Expr))
  1368. return IntInit::get(0);
  1369. } else {
  1370. // We treat non-record types as not castable.
  1371. return IntInit::get(0);
  1372. }
  1373. }
  1374. return const_cast<IsAOpInit *>(this);
  1375. }
  1376. Init *IsAOpInit::resolveReferences(Resolver &R) const {
  1377. Init *NewExpr = Expr->resolveReferences(R);
  1378. if (Expr != NewExpr)
  1379. return get(CheckType, NewExpr)->Fold();
  1380. return const_cast<IsAOpInit *>(this);
  1381. }
  1382. Init *IsAOpInit::getBit(unsigned Bit) const {
  1383. return VarBitInit::get(const_cast<IsAOpInit *>(this), Bit);
  1384. }
  1385. std::string IsAOpInit::getAsString() const {
  1386. return (Twine("!isa<") + CheckType->getAsString() + ">(" +
  1387. Expr->getAsString() + ")")
  1388. .str();
  1389. }
  1390. RecTy *TypedInit::getFieldType(StringInit *FieldName) const {
  1391. if (RecordRecTy *RecordType = dyn_cast<RecordRecTy>(getType())) {
  1392. for (Record *Rec : RecordType->getClasses()) {
  1393. if (RecordVal *Field = Rec->getValue(FieldName))
  1394. return Field->getType();
  1395. }
  1396. }
  1397. return nullptr;
  1398. }
  1399. Init *
  1400. TypedInit::convertInitializerTo(RecTy *Ty) const {
  1401. if (getType() == Ty || getType()->typeIsA(Ty))
  1402. return const_cast<TypedInit *>(this);
  1403. if (isa<BitRecTy>(getType()) && isa<BitsRecTy>(Ty) &&
  1404. cast<BitsRecTy>(Ty)->getNumBits() == 1)
  1405. return BitsInit::get({const_cast<TypedInit *>(this)});
  1406. return nullptr;
  1407. }
  1408. Init *TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
  1409. BitsRecTy *T = dyn_cast<BitsRecTy>(getType());
  1410. if (!T) return nullptr; // Cannot subscript a non-bits variable.
  1411. unsigned NumBits = T->getNumBits();
  1412. SmallVector<Init *, 16> NewBits;
  1413. NewBits.reserve(Bits.size());
  1414. for (unsigned Bit : Bits) {
  1415. if (Bit >= NumBits)
  1416. return nullptr;
  1417. NewBits.push_back(VarBitInit::get(const_cast<TypedInit *>(this), Bit));
  1418. }
  1419. return BitsInit::get(NewBits);
  1420. }
  1421. Init *TypedInit::getCastTo(RecTy *Ty) const {
  1422. // Handle the common case quickly
  1423. if (getType() == Ty || getType()->typeIsA(Ty))
  1424. return const_cast<TypedInit *>(this);
  1425. if (Init *Converted = convertInitializerTo(Ty)) {
  1426. assert(!isa<TypedInit>(Converted) ||
  1427. cast<TypedInit>(Converted)->getType()->typeIsA(Ty));
  1428. return Converted;
  1429. }
  1430. if (!getType()->typeIsConvertibleTo(Ty))
  1431. return nullptr;
  1432. return UnOpInit::get(UnOpInit::CAST, const_cast<TypedInit *>(this), Ty)
  1433. ->Fold(nullptr);
  1434. }
  1435. Init *TypedInit::convertInitListSlice(ArrayRef<unsigned> Elements) const {
  1436. ListRecTy *T = dyn_cast<ListRecTy>(getType());
  1437. if (!T) return nullptr; // Cannot subscript a non-list variable.
  1438. if (Elements.size() == 1)
  1439. return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]);
  1440. SmallVector<Init*, 8> ListInits;
  1441. ListInits.reserve(Elements.size());
  1442. for (unsigned Element : Elements)
  1443. ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this),
  1444. Element));
  1445. return ListInit::get(ListInits, T->getElementType());
  1446. }
  1447. VarInit *VarInit::get(StringRef VN, RecTy *T) {
  1448. Init *Value = StringInit::get(VN);
  1449. return VarInit::get(Value, T);
  1450. }
  1451. VarInit *VarInit::get(Init *VN, RecTy *T) {
  1452. VarInit *&I = Context->TheVarInitPool[std::make_pair(T, VN)];
  1453. if (!I)
  1454. I = new (Context->Allocator) VarInit(VN, T);
  1455. return I;
  1456. }
  1457. StringRef VarInit::getName() const {
  1458. StringInit *NameString = cast<StringInit>(getNameInit());
  1459. return NameString->getValue();
  1460. }
  1461. Init *VarInit::getBit(unsigned Bit) const {
  1462. if (getType() == BitRecTy::get())
  1463. return const_cast<VarInit*>(this);
  1464. return VarBitInit::get(const_cast<VarInit*>(this), Bit);
  1465. }
  1466. Init *VarInit::resolveReferences(Resolver &R) const {
  1467. if (Init *Val = R.resolve(VarName))
  1468. return Val;
  1469. return const_cast<VarInit *>(this);
  1470. }
  1471. VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) {
  1472. VarBitInit *&I = Context->TheVarBitInitPool[std::make_pair(T, B)];
  1473. if (!I)
  1474. I = new(Context->Allocator) VarBitInit(T, B);
  1475. return I;
  1476. }
  1477. std::string VarBitInit::getAsString() const {
  1478. return TI->getAsString() + "{" + utostr(Bit) + "}";
  1479. }
  1480. Init *VarBitInit::resolveReferences(Resolver &R) const {
  1481. Init *I = TI->resolveReferences(R);
  1482. if (TI != I)
  1483. return I->getBit(getBitNum());
  1484. return const_cast<VarBitInit*>(this);
  1485. }
  1486. VarListElementInit *VarListElementInit::get(TypedInit *T, unsigned E) {
  1487. VarListElementInit *&I =
  1488. Context->TheVarListElementInitPool[std::make_pair(T, E)];
  1489. if (!I)
  1490. I = new (Context->Allocator) VarListElementInit(T, E);
  1491. return I;
  1492. }
  1493. std::string VarListElementInit::getAsString() const {
  1494. return TI->getAsString() + "[" + utostr(Element) + "]";
  1495. }
  1496. Init *VarListElementInit::resolveReferences(Resolver &R) const {
  1497. Init *NewTI = TI->resolveReferences(R);
  1498. if (ListInit *List = dyn_cast<ListInit>(NewTI)) {
  1499. // Leave out-of-bounds array references as-is. This can happen without
  1500. // being an error, e.g. in the untaken "branch" of an !if expression.
  1501. if (getElementNum() < List->size())
  1502. return List->getElement(getElementNum());
  1503. }
  1504. if (NewTI != TI && isa<TypedInit>(NewTI))
  1505. return VarListElementInit::get(cast<TypedInit>(NewTI), getElementNum());
  1506. return const_cast<VarListElementInit *>(this);
  1507. }
  1508. Init *VarListElementInit::getBit(unsigned Bit) const {
  1509. if (getType() == BitRecTy::get())
  1510. return const_cast<VarListElementInit*>(this);
  1511. return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit);
  1512. }
  1513. DefInit::DefInit(Record *D)
  1514. : TypedInit(IK_DefInit, D->getType()), Def(D) {}
  1515. DefInit *DefInit::get(Record *R) {
  1516. return R->getDefInit();
  1517. }
  1518. Init *DefInit::convertInitializerTo(RecTy *Ty) const {
  1519. if (auto *RRT = dyn_cast<RecordRecTy>(Ty))
  1520. if (getType()->typeIsConvertibleTo(RRT))
  1521. return const_cast<DefInit *>(this);
  1522. return nullptr;
  1523. }
  1524. RecTy *DefInit::getFieldType(StringInit *FieldName) const {
  1525. if (const RecordVal *RV = Def->getValue(FieldName))
  1526. return RV->getType();
  1527. return nullptr;
  1528. }
  1529. std::string DefInit::getAsString() const { return std::string(Def->getName()); }
  1530. static void ProfileVarDefInit(FoldingSetNodeID &ID,
  1531. Record *Class,
  1532. ArrayRef<Init *> Args) {
  1533. ID.AddInteger(Args.size());
  1534. ID.AddPointer(Class);
  1535. for (Init *I : Args)
  1536. ID.AddPointer(I);
  1537. }
  1538. VarDefInit *VarDefInit::get(Record *Class, ArrayRef<Init *> Args) {
  1539. FoldingSetNodeID ID;
  1540. ProfileVarDefInit(ID, Class, Args);
  1541. void *IP = nullptr;
  1542. if (VarDefInit *I = Context->TheVarDefInitPool.FindNodeOrInsertPos(ID, IP))
  1543. return I;
  1544. void *Mem = Context->Allocator.Allocate(totalSizeToAlloc<Init *>(Args.size()),
  1545. alignof(VarDefInit));
  1546. VarDefInit *I = new (Mem) VarDefInit(Class, Args.size());
  1547. std::uninitialized_copy(Args.begin(), Args.end(),
  1548. I->getTrailingObjects<Init *>());
  1549. Context->TheVarDefInitPool.InsertNode(I, IP);
  1550. return I;
  1551. }
  1552. void VarDefInit::Profile(FoldingSetNodeID &ID) const {
  1553. ProfileVarDefInit(ID, Class, args());
  1554. }
  1555. DefInit *VarDefInit::instantiate() {
  1556. if (!Def) {
  1557. RecordKeeper &Records = Class->getRecords();
  1558. auto NewRecOwner = std::make_unique<Record>(Records.getNewAnonymousName(),
  1559. Class->getLoc(), Records,
  1560. /*IsAnonymous=*/true);
  1561. Record *NewRec = NewRecOwner.get();
  1562. // Copy values from class to instance
  1563. for (const RecordVal &Val : Class->getValues())
  1564. NewRec->addValue(Val);
  1565. // Copy assertions from class to instance.
  1566. NewRec->appendAssertions(Class);
  1567. // Substitute and resolve template arguments
  1568. ArrayRef<Init *> TArgs = Class->getTemplateArgs();
  1569. MapResolver R(NewRec);
  1570. for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
  1571. if (i < args_size())
  1572. R.set(TArgs[i], getArg(i));
  1573. else
  1574. R.set(TArgs[i], NewRec->getValue(TArgs[i])->getValue());
  1575. NewRec->removeValue(TArgs[i]);
  1576. }
  1577. NewRec->resolveReferences(R);
  1578. // Add superclasses.
  1579. ArrayRef<std::pair<Record *, SMRange>> SCs = Class->getSuperClasses();
  1580. for (const auto &SCPair : SCs)
  1581. NewRec->addSuperClass(SCPair.first, SCPair.second);
  1582. NewRec->addSuperClass(Class,
  1583. SMRange(Class->getLoc().back(),
  1584. Class->getLoc().back()));
  1585. // Resolve internal references and store in record keeper
  1586. NewRec->resolveReferences();
  1587. Records.addDef(std::move(NewRecOwner));
  1588. // Check the assertions.
  1589. NewRec->checkRecordAssertions();
  1590. Def = DefInit::get(NewRec);
  1591. }
  1592. return Def;
  1593. }
  1594. Init *VarDefInit::resolveReferences(Resolver &R) const {
  1595. TrackUnresolvedResolver UR(&R);
  1596. bool Changed = false;
  1597. SmallVector<Init *, 8> NewArgs;
  1598. NewArgs.reserve(args_size());
  1599. for (Init *Arg : args()) {
  1600. Init *NewArg = Arg->resolveReferences(UR);
  1601. NewArgs.push_back(NewArg);
  1602. Changed |= NewArg != Arg;
  1603. }
  1604. if (Changed) {
  1605. auto New = VarDefInit::get(Class, NewArgs);
  1606. if (!UR.foundUnresolved())
  1607. return New->instantiate();
  1608. return New;
  1609. }
  1610. return const_cast<VarDefInit *>(this);
  1611. }
  1612. Init *VarDefInit::Fold() const {
  1613. if (Def)
  1614. return Def;
  1615. TrackUnresolvedResolver R;
  1616. for (Init *Arg : args())
  1617. Arg->resolveReferences(R);
  1618. if (!R.foundUnresolved())
  1619. return const_cast<VarDefInit *>(this)->instantiate();
  1620. return const_cast<VarDefInit *>(this);
  1621. }
  1622. std::string VarDefInit::getAsString() const {
  1623. std::string Result = Class->getNameInitAsString() + "<";
  1624. const char *sep = "";
  1625. for (Init *Arg : args()) {
  1626. Result += sep;
  1627. sep = ", ";
  1628. Result += Arg->getAsString();
  1629. }
  1630. return Result + ">";
  1631. }
  1632. FieldInit *FieldInit::get(Init *R, StringInit *FN) {
  1633. FieldInit *&I = Context->TheFieldInitPool[std::make_pair(R, FN)];
  1634. if (!I)
  1635. I = new (Context->Allocator) FieldInit(R, FN);
  1636. return I;
  1637. }
  1638. Init *FieldInit::getBit(unsigned Bit) const {
  1639. if (getType() == BitRecTy::get())
  1640. return const_cast<FieldInit*>(this);
  1641. return VarBitInit::get(const_cast<FieldInit*>(this), Bit);
  1642. }
  1643. Init *FieldInit::resolveReferences(Resolver &R) const {
  1644. Init *NewRec = Rec->resolveReferences(R);
  1645. if (NewRec != Rec)
  1646. return FieldInit::get(NewRec, FieldName)->Fold(R.getCurrentRecord());
  1647. return const_cast<FieldInit *>(this);
  1648. }
  1649. Init *FieldInit::Fold(Record *CurRec) const {
  1650. if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
  1651. Record *Def = DI->getDef();
  1652. if (Def == CurRec)
  1653. PrintFatalError(CurRec->getLoc(),
  1654. Twine("Attempting to access field '") +
  1655. FieldName->getAsUnquotedString() + "' of '" +
  1656. Rec->getAsString() + "' is a forbidden self-reference");
  1657. Init *FieldVal = Def->getValue(FieldName)->getValue();
  1658. if (FieldVal->isConcrete())
  1659. return FieldVal;
  1660. }
  1661. return const_cast<FieldInit *>(this);
  1662. }
  1663. bool FieldInit::isConcrete() const {
  1664. if (DefInit *DI = dyn_cast<DefInit>(Rec)) {
  1665. Init *FieldVal = DI->getDef()->getValue(FieldName)->getValue();
  1666. return FieldVal->isConcrete();
  1667. }
  1668. return false;
  1669. }
  1670. static void ProfileCondOpInit(FoldingSetNodeID &ID,
  1671. ArrayRef<Init *> CondRange,
  1672. ArrayRef<Init *> ValRange,
  1673. const RecTy *ValType) {
  1674. assert(CondRange.size() == ValRange.size() &&
  1675. "Number of conditions and values must match!");
  1676. ID.AddPointer(ValType);
  1677. ArrayRef<Init *>::iterator Case = CondRange.begin();
  1678. ArrayRef<Init *>::iterator Val = ValRange.begin();
  1679. while (Case != CondRange.end()) {
  1680. ID.AddPointer(*Case++);
  1681. ID.AddPointer(*Val++);
  1682. }
  1683. }
  1684. void CondOpInit::Profile(FoldingSetNodeID &ID) const {
  1685. ProfileCondOpInit(ID,
  1686. makeArrayRef(getTrailingObjects<Init *>(), NumConds),
  1687. makeArrayRef(getTrailingObjects<Init *>() + NumConds, NumConds),
  1688. ValType);
  1689. }
  1690. CondOpInit *
  1691. CondOpInit::get(ArrayRef<Init *> CondRange,
  1692. ArrayRef<Init *> ValRange, RecTy *Ty) {
  1693. assert(CondRange.size() == ValRange.size() &&
  1694. "Number of conditions and values must match!");
  1695. FoldingSetNodeID ID;
  1696. ProfileCondOpInit(ID, CondRange, ValRange, Ty);
  1697. void *IP = nullptr;
  1698. if (CondOpInit *I = Context->TheCondOpInitPool.FindNodeOrInsertPos(ID, IP))
  1699. return I;
  1700. void *Mem = Context->Allocator.Allocate(
  1701. totalSizeToAlloc<Init *>(2 * CondRange.size()), alignof(BitsInit));
  1702. CondOpInit *I = new(Mem) CondOpInit(CondRange.size(), Ty);
  1703. std::uninitialized_copy(CondRange.begin(), CondRange.end(),
  1704. I->getTrailingObjects<Init *>());
  1705. std::uninitialized_copy(ValRange.begin(), ValRange.end(),
  1706. I->getTrailingObjects<Init *>()+CondRange.size());
  1707. Context->TheCondOpInitPool.InsertNode(I, IP);
  1708. return I;
  1709. }
  1710. Init *CondOpInit::resolveReferences(Resolver &R) const {
  1711. SmallVector<Init*, 4> NewConds;
  1712. bool Changed = false;
  1713. for (const Init *Case : getConds()) {
  1714. Init *NewCase = Case->resolveReferences(R);
  1715. NewConds.push_back(NewCase);
  1716. Changed |= NewCase != Case;
  1717. }
  1718. SmallVector<Init*, 4> NewVals;
  1719. for (const Init *Val : getVals()) {
  1720. Init *NewVal = Val->resolveReferences(R);
  1721. NewVals.push_back(NewVal);
  1722. Changed |= NewVal != Val;
  1723. }
  1724. if (Changed)
  1725. return (CondOpInit::get(NewConds, NewVals,
  1726. getValType()))->Fold(R.getCurrentRecord());
  1727. return const_cast<CondOpInit *>(this);
  1728. }
  1729. Init *CondOpInit::Fold(Record *CurRec) const {
  1730. for ( unsigned i = 0; i < NumConds; ++i) {
  1731. Init *Cond = getCond(i);
  1732. Init *Val = getVal(i);
  1733. if (IntInit *CondI = dyn_cast_or_null<IntInit>(
  1734. Cond->convertInitializerTo(IntRecTy::get()))) {
  1735. if (CondI->getValue())
  1736. return Val->convertInitializerTo(getValType());
  1737. } else
  1738. return const_cast<CondOpInit *>(this);
  1739. }
  1740. PrintFatalError(CurRec->getLoc(),
  1741. CurRec->getName() +
  1742. " does not have any true condition in:" +
  1743. this->getAsString());
  1744. return nullptr;
  1745. }
  1746. bool CondOpInit::isConcrete() const {
  1747. for (const Init *Case : getConds())
  1748. if (!Case->isConcrete())
  1749. return false;
  1750. for (const Init *Val : getVals())
  1751. if (!Val->isConcrete())
  1752. return false;
  1753. return true;
  1754. }
  1755. bool CondOpInit::isComplete() const {
  1756. for (const Init *Case : getConds())
  1757. if (!Case->isComplete())
  1758. return false;
  1759. for (const Init *Val : getVals())
  1760. if (!Val->isConcrete())
  1761. return false;
  1762. return true;
  1763. }
  1764. std::string CondOpInit::getAsString() const {
  1765. std::string Result = "!cond(";
  1766. for (unsigned i = 0; i < getNumConds(); i++) {
  1767. Result += getCond(i)->getAsString() + ": ";
  1768. Result += getVal(i)->getAsString();
  1769. if (i != getNumConds()-1)
  1770. Result += ", ";
  1771. }
  1772. return Result + ")";
  1773. }
  1774. Init *CondOpInit::getBit(unsigned Bit) const {
  1775. return VarBitInit::get(const_cast<CondOpInit *>(this), Bit);
  1776. }
  1777. static void ProfileDagInit(FoldingSetNodeID &ID, Init *V, StringInit *VN,
  1778. ArrayRef<Init *> ArgRange,
  1779. ArrayRef<StringInit *> NameRange) {
  1780. ID.AddPointer(V);
  1781. ID.AddPointer(VN);
  1782. ArrayRef<Init *>::iterator Arg = ArgRange.begin();
  1783. ArrayRef<StringInit *>::iterator Name = NameRange.begin();
  1784. while (Arg != ArgRange.end()) {
  1785. assert(Name != NameRange.end() && "Arg name underflow!");
  1786. ID.AddPointer(*Arg++);
  1787. ID.AddPointer(*Name++);
  1788. }
  1789. assert(Name == NameRange.end() && "Arg name overflow!");
  1790. }
  1791. DagInit *DagInit::get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
  1792. ArrayRef<StringInit *> NameRange) {
  1793. FoldingSetNodeID ID;
  1794. ProfileDagInit(ID, V, VN, ArgRange, NameRange);
  1795. void *IP = nullptr;
  1796. if (DagInit *I = Context->TheDagInitPool.FindNodeOrInsertPos(ID, IP))
  1797. return I;
  1798. void *Mem = Context->Allocator.Allocate(
  1799. totalSizeToAlloc<Init *, StringInit *>(ArgRange.size(), NameRange.size()),
  1800. alignof(BitsInit));
  1801. DagInit *I = new (Mem) DagInit(V, VN, ArgRange.size(), NameRange.size());
  1802. std::uninitialized_copy(ArgRange.begin(), ArgRange.end(),
  1803. I->getTrailingObjects<Init *>());
  1804. std::uninitialized_copy(NameRange.begin(), NameRange.end(),
  1805. I->getTrailingObjects<StringInit *>());
  1806. Context->TheDagInitPool.InsertNode(I, IP);
  1807. return I;
  1808. }
  1809. DagInit *
  1810. DagInit::get(Init *V, StringInit *VN,
  1811. ArrayRef<std::pair<Init*, StringInit*>> args) {
  1812. SmallVector<Init *, 8> Args;
  1813. SmallVector<StringInit *, 8> Names;
  1814. for (const auto &Arg : args) {
  1815. Args.push_back(Arg.first);
  1816. Names.push_back(Arg.second);
  1817. }
  1818. return DagInit::get(V, VN, Args, Names);
  1819. }
  1820. void DagInit::Profile(FoldingSetNodeID &ID) const {
  1821. ProfileDagInit(ID, Val, ValName, makeArrayRef(getTrailingObjects<Init *>(), NumArgs), makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames));
  1822. }
  1823. Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const {
  1824. if (DefInit *DefI = dyn_cast<DefInit>(Val))
  1825. return DefI->getDef();
  1826. PrintFatalError(Loc, "Expected record as operator");
  1827. return nullptr;
  1828. }
  1829. Init *DagInit::resolveReferences(Resolver &R) const {
  1830. SmallVector<Init*, 8> NewArgs;
  1831. NewArgs.reserve(arg_size());
  1832. bool ArgsChanged = false;
  1833. for (const Init *Arg : getArgs()) {
  1834. Init *NewArg = Arg->resolveReferences(R);
  1835. NewArgs.push_back(NewArg);
  1836. ArgsChanged |= NewArg != Arg;
  1837. }
  1838. Init *Op = Val->resolveReferences(R);
  1839. if (Op != Val || ArgsChanged)
  1840. return DagInit::get(Op, ValName, NewArgs, getArgNames());
  1841. return const_cast<DagInit *>(this);
  1842. }
  1843. bool DagInit::isConcrete() const {
  1844. if (!Val->isConcrete())
  1845. return false;
  1846. for (const Init *Elt : getArgs()) {
  1847. if (!Elt->isConcrete())
  1848. return false;
  1849. }
  1850. return true;
  1851. }
  1852. std::string DagInit::getAsString() const {
  1853. std::string Result = "(" + Val->getAsString();
  1854. if (ValName)
  1855. Result += ":" + ValName->getAsUnquotedString();
  1856. if (!arg_empty()) {
  1857. Result += " " + getArg(0)->getAsString();
  1858. if (getArgName(0)) Result += ":$" + getArgName(0)->getAsUnquotedString();
  1859. for (unsigned i = 1, e = getNumArgs(); i != e; ++i) {
  1860. Result += ", " + getArg(i)->getAsString();
  1861. if (getArgName(i)) Result += ":$" + getArgName(i)->getAsUnquotedString();
  1862. }
  1863. }
  1864. return Result + ")";
  1865. }
  1866. //===----------------------------------------------------------------------===//
  1867. // Other implementations
  1868. //===----------------------------------------------------------------------===//
  1869. RecordVal::RecordVal(Init *N, RecTy *T, FieldKind K)
  1870. : Name(N), TyAndKind(T, K) {
  1871. setValue(UnsetInit::get());
  1872. assert(Value && "Cannot create unset value for current type!");
  1873. }
  1874. // This constructor accepts the same arguments as the above, but also
  1875. // a source location.
  1876. RecordVal::RecordVal(Init *N, SMLoc Loc, RecTy *T, FieldKind K)
  1877. : Name(N), Loc(Loc), TyAndKind(T, K) {
  1878. setValue(UnsetInit::get());
  1879. assert(Value && "Cannot create unset value for current type!");
  1880. }
  1881. StringRef RecordVal::getName() const {
  1882. return cast<StringInit>(getNameInit())->getValue();
  1883. }
  1884. std::string RecordVal::getPrintType() const {
  1885. if (getType() == StringRecTy::get()) {
  1886. if (auto *StrInit = dyn_cast<StringInit>(Value)) {
  1887. if (StrInit->hasCodeFormat())
  1888. return "code";
  1889. else
  1890. return "string";
  1891. } else {
  1892. return "string";
  1893. }
  1894. } else {
  1895. return TyAndKind.getPointer()->getAsString();
  1896. }
  1897. }
  1898. bool RecordVal::setValue(Init *V) {
  1899. if (V) {
  1900. Value = V->getCastTo(getType());
  1901. if (Value) {
  1902. assert(!isa<TypedInit>(Value) ||
  1903. cast<TypedInit>(Value)->getType()->typeIsA(getType()));
  1904. if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
  1905. if (!isa<BitsInit>(Value)) {
  1906. SmallVector<Init *, 64> Bits;
  1907. Bits.reserve(BTy->getNumBits());
  1908. for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I)
  1909. Bits.push_back(Value->getBit(I));
  1910. Value = BitsInit::get(Bits);
  1911. }
  1912. }
  1913. }
  1914. return Value == nullptr;
  1915. }
  1916. Value = nullptr;
  1917. return false;
  1918. }
  1919. // This version of setValue takes a source location and resets the
  1920. // location in the RecordVal.
  1921. bool RecordVal::setValue(Init *V, SMLoc NewLoc) {
  1922. Loc = NewLoc;
  1923. if (V) {
  1924. Value = V->getCastTo(getType());
  1925. if (Value) {
  1926. assert(!isa<TypedInit>(Value) ||
  1927. cast<TypedInit>(Value)->getType()->typeIsA(getType()));
  1928. if (BitsRecTy *BTy = dyn_cast<BitsRecTy>(getType())) {
  1929. if (!isa<BitsInit>(Value)) {
  1930. SmallVector<Init *, 64> Bits;
  1931. Bits.reserve(BTy->getNumBits());
  1932. for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I)
  1933. Bits.push_back(Value->getBit(I));
  1934. Value = BitsInit::get(Bits);
  1935. }
  1936. }
  1937. }
  1938. return Value == nullptr;
  1939. }
  1940. Value = nullptr;
  1941. return false;
  1942. }
  1943. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  1944. #include "llvm/TableGen/Record.h"
  1945. LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; }
  1946. #endif
  1947. void RecordVal::print(raw_ostream &OS, bool PrintSem) const {
  1948. if (isNonconcreteOK()) OS << "field ";
  1949. OS << getPrintType() << " " << getNameInitAsString();
  1950. if (getValue())
  1951. OS << " = " << *getValue();
  1952. if (PrintSem) OS << ";\n";
  1953. }
  1954. void Record::checkName() {
  1955. // Ensure the record name has string type.
  1956. const TypedInit *TypedName = cast<const TypedInit>(Name);
  1957. if (!isa<StringRecTy>(TypedName->getType()))
  1958. PrintFatalError(getLoc(), Twine("Record name '") + Name->getAsString() +
  1959. "' is not a string!");
  1960. }
  1961. RecordRecTy *Record::getType() {
  1962. SmallVector<Record *, 4> DirectSCs;
  1963. getDirectSuperClasses(DirectSCs);
  1964. return RecordRecTy::get(DirectSCs);
  1965. }
  1966. DefInit *Record::getDefInit() {
  1967. if (!CorrespondingDefInit)
  1968. CorrespondingDefInit = new (Context->Allocator) DefInit(this);
  1969. return CorrespondingDefInit;
  1970. }
  1971. unsigned Record::getNewUID() { return Context->LastRecordID++; }
  1972. void Record::setName(Init *NewName) {
  1973. Name = NewName;
  1974. checkName();
  1975. // DO NOT resolve record values to the name at this point because
  1976. // there might be default values for arguments of this def. Those
  1977. // arguments might not have been resolved yet so we don't want to
  1978. // prematurely assume values for those arguments were not passed to
  1979. // this def.
  1980. //
  1981. // Nonetheless, it may be that some of this Record's values
  1982. // reference the record name. Indeed, the reason for having the
  1983. // record name be an Init is to provide this flexibility. The extra
  1984. // resolve steps after completely instantiating defs takes care of
  1985. // this. See TGParser::ParseDef and TGParser::ParseDefm.
  1986. }
  1987. // NOTE for the next two functions:
  1988. // Superclasses are in post-order, so the final one is a direct
  1989. // superclass. All of its transitive superclases immediately precede it,
  1990. // so we can step through the direct superclasses in reverse order.
  1991. bool Record::hasDirectSuperClass(const Record *Superclass) const {
  1992. ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
  1993. for (int I = SCs.size() - 1; I >= 0; --I) {
  1994. const Record *SC = SCs[I].first;
  1995. if (SC == Superclass)
  1996. return true;
  1997. I -= SC->getSuperClasses().size();
  1998. }
  1999. return false;
  2000. }
  2001. void Record::getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const {
  2002. ArrayRef<std::pair<Record *, SMRange>> SCs = getSuperClasses();
  2003. while (!SCs.empty()) {
  2004. Record *SC = SCs.back().first;
  2005. SCs = SCs.drop_back(1 + SC->getSuperClasses().size());
  2006. Classes.push_back(SC);
  2007. }
  2008. }
  2009. void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) {
  2010. Init *OldName = getNameInit();
  2011. Init *NewName = Name->resolveReferences(R);
  2012. if (NewName != OldName) {
  2013. // Re-register with RecordKeeper.
  2014. setName(NewName);
  2015. }
  2016. // Resolve the field values.
  2017. for (RecordVal &Value : Values) {
  2018. if (SkipVal == &Value) // Skip resolve the same field as the given one
  2019. continue;
  2020. if (Init *V = Value.getValue()) {
  2021. Init *VR = V->resolveReferences(R);
  2022. if (Value.setValue(VR)) {
  2023. std::string Type;
  2024. if (TypedInit *VRT = dyn_cast<TypedInit>(VR))
  2025. Type =
  2026. (Twine("of type '") + VRT->getType()->getAsString() + "' ").str();
  2027. PrintFatalError(
  2028. getLoc(),
  2029. Twine("Invalid value ") + Type + "found when setting field '" +
  2030. Value.getNameInitAsString() + "' of type '" +
  2031. Value.getType()->getAsString() +
  2032. "' after resolving references: " + VR->getAsUnquotedString() +
  2033. "\n");
  2034. }
  2035. }
  2036. }
  2037. // Resolve the assertion expressions.
  2038. for (auto &Assertion : Assertions) {
  2039. Init *Value = Assertion.Condition->resolveReferences(R);
  2040. Assertion.Condition = Value;
  2041. Value = Assertion.Message->resolveReferences(R);
  2042. Assertion.Message = Value;
  2043. }
  2044. }
  2045. void Record::resolveReferences(Init *NewName) {
  2046. RecordResolver R(*this);
  2047. R.setName(NewName);
  2048. R.setFinal(true);
  2049. resolveReferences(R);
  2050. }
  2051. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  2052. LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; }
  2053. #endif
  2054. raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) {
  2055. OS << R.getNameInitAsString();
  2056. ArrayRef<Init *> TArgs = R.getTemplateArgs();
  2057. if (!TArgs.empty()) {
  2058. OS << "<";
  2059. bool NeedComma = false;
  2060. for (const Init *TA : TArgs) {
  2061. if (NeedComma) OS << ", ";
  2062. NeedComma = true;
  2063. const RecordVal *RV = R.getValue(TA);
  2064. assert(RV && "Template argument record not found??");
  2065. RV->print(OS, false);
  2066. }
  2067. OS << ">";
  2068. }
  2069. OS << " {";
  2070. ArrayRef<std::pair<Record *, SMRange>> SC = R.getSuperClasses();
  2071. if (!SC.empty()) {
  2072. OS << "\t//";
  2073. for (const auto &SuperPair : SC)
  2074. OS << " " << SuperPair.first->getNameInitAsString();
  2075. }
  2076. OS << "\n";
  2077. for (const RecordVal &Val : R.getValues())
  2078. if (Val.isNonconcreteOK() && !R.isTemplateArg(Val.getNameInit()))
  2079. OS << Val;
  2080. for (const RecordVal &Val : R.getValues())
  2081. if (!Val.isNonconcreteOK() && !R.isTemplateArg(Val.getNameInit()))
  2082. OS << Val;
  2083. return OS << "}\n";
  2084. }
  2085. SMLoc Record::getFieldLoc(StringRef FieldName) const {
  2086. const RecordVal *R = getValue(FieldName);
  2087. if (!R)
  2088. PrintFatalError(getLoc(), "Record `" + getName() +
  2089. "' does not have a field named `" + FieldName + "'!\n");
  2090. return R->getLoc();
  2091. }
  2092. Init *Record::getValueInit(StringRef FieldName) const {
  2093. const RecordVal *R = getValue(FieldName);
  2094. if (!R || !R->getValue())
  2095. PrintFatalError(getLoc(), "Record `" + getName() +
  2096. "' does not have a field named `" + FieldName + "'!\n");
  2097. return R->getValue();
  2098. }
  2099. StringRef Record::getValueAsString(StringRef FieldName) const {
  2100. llvm::Optional<StringRef> S = getValueAsOptionalString(FieldName);
  2101. if (!S.hasValue())
  2102. PrintFatalError(getLoc(), "Record `" + getName() +
  2103. "' does not have a field named `" + FieldName + "'!\n");
  2104. return S.getValue();
  2105. }
  2106. llvm::Optional<StringRef>
  2107. Record::getValueAsOptionalString(StringRef FieldName) const {
  2108. const RecordVal *R = getValue(FieldName);
  2109. if (!R || !R->getValue())
  2110. return llvm::Optional<StringRef>();
  2111. if (isa<UnsetInit>(R->getValue()))
  2112. return llvm::Optional<StringRef>();
  2113. if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
  2114. return SI->getValue();
  2115. PrintFatalError(getLoc(),
  2116. "Record `" + getName() + "', ` field `" + FieldName +
  2117. "' exists but does not have a string initializer!");
  2118. }
  2119. BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
  2120. const RecordVal *R = getValue(FieldName);
  2121. if (!R || !R->getValue())
  2122. PrintFatalError(getLoc(), "Record `" + getName() +
  2123. "' does not have a field named `" + FieldName + "'!\n");
  2124. if (BitsInit *BI = dyn_cast<BitsInit>(R->getValue()))
  2125. return BI;
  2126. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
  2127. "' exists but does not have a bits value");
  2128. }
  2129. ListInit *Record::getValueAsListInit(StringRef FieldName) const {
  2130. const RecordVal *R = getValue(FieldName);
  2131. if (!R || !R->getValue())
  2132. PrintFatalError(getLoc(), "Record `" + getName() +
  2133. "' does not have a field named `" + FieldName + "'!\n");
  2134. if (ListInit *LI = dyn_cast<ListInit>(R->getValue()))
  2135. return LI;
  2136. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
  2137. "' exists but does not have a list value");
  2138. }
  2139. std::vector<Record*>
  2140. Record::getValueAsListOfDefs(StringRef FieldName) const {
  2141. ListInit *List = getValueAsListInit(FieldName);
  2142. std::vector<Record*> Defs;
  2143. for (Init *I : List->getValues()) {
  2144. if (DefInit *DI = dyn_cast<DefInit>(I))
  2145. Defs.push_back(DI->getDef());
  2146. else
  2147. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  2148. FieldName + "' list is not entirely DefInit!");
  2149. }
  2150. return Defs;
  2151. }
  2152. int64_t Record::getValueAsInt(StringRef FieldName) const {
  2153. const RecordVal *R = getValue(FieldName);
  2154. if (!R || !R->getValue())
  2155. PrintFatalError(getLoc(), "Record `" + getName() +
  2156. "' does not have a field named `" + FieldName + "'!\n");
  2157. if (IntInit *II = dyn_cast<IntInit>(R->getValue()))
  2158. return II->getValue();
  2159. PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
  2160. FieldName +
  2161. "' exists but does not have an int value: " +
  2162. R->getValue()->getAsString());
  2163. }
  2164. std::vector<int64_t>
  2165. Record::getValueAsListOfInts(StringRef FieldName) const {
  2166. ListInit *List = getValueAsListInit(FieldName);
  2167. std::vector<int64_t> Ints;
  2168. for (Init *I : List->getValues()) {
  2169. if (IntInit *II = dyn_cast<IntInit>(I))
  2170. Ints.push_back(II->getValue());
  2171. else
  2172. PrintFatalError(getLoc(),
  2173. Twine("Record `") + getName() + "', field `" + FieldName +
  2174. "' exists but does not have a list of ints value: " +
  2175. I->getAsString());
  2176. }
  2177. return Ints;
  2178. }
  2179. std::vector<StringRef>
  2180. Record::getValueAsListOfStrings(StringRef FieldName) const {
  2181. ListInit *List = getValueAsListInit(FieldName);
  2182. std::vector<StringRef> Strings;
  2183. for (Init *I : List->getValues()) {
  2184. if (StringInit *SI = dyn_cast<StringInit>(I))
  2185. Strings.push_back(SI->getValue());
  2186. else
  2187. PrintFatalError(getLoc(),
  2188. Twine("Record `") + getName() + "', field `" + FieldName +
  2189. "' exists but does not have a list of strings value: " +
  2190. I->getAsString());
  2191. }
  2192. return Strings;
  2193. }
  2194. Record *Record::getValueAsDef(StringRef FieldName) const {
  2195. const RecordVal *R = getValue(FieldName);
  2196. if (!R || !R->getValue())
  2197. PrintFatalError(getLoc(), "Record `" + getName() +
  2198. "' does not have a field named `" + FieldName + "'!\n");
  2199. if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
  2200. return DI->getDef();
  2201. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  2202. FieldName + "' does not have a def initializer!");
  2203. }
  2204. Record *Record::getValueAsOptionalDef(StringRef FieldName) const {
  2205. const RecordVal *R = getValue(FieldName);
  2206. if (!R || !R->getValue())
  2207. PrintFatalError(getLoc(), "Record `" + getName() +
  2208. "' does not have a field named `" + FieldName + "'!\n");
  2209. if (DefInit *DI = dyn_cast<DefInit>(R->getValue()))
  2210. return DI->getDef();
  2211. if (isa<UnsetInit>(R->getValue()))
  2212. return nullptr;
  2213. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  2214. FieldName + "' does not have either a def initializer or '?'!");
  2215. }
  2216. bool Record::getValueAsBit(StringRef FieldName) const {
  2217. const RecordVal *R = getValue(FieldName);
  2218. if (!R || !R->getValue())
  2219. PrintFatalError(getLoc(), "Record `" + getName() +
  2220. "' does not have a field named `" + FieldName + "'!\n");
  2221. if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
  2222. return BI->getValue();
  2223. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  2224. FieldName + "' does not have a bit initializer!");
  2225. }
  2226. bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
  2227. const RecordVal *R = getValue(FieldName);
  2228. if (!R || !R->getValue())
  2229. PrintFatalError(getLoc(), "Record `" + getName() +
  2230. "' does not have a field named `" + FieldName.str() + "'!\n");
  2231. if (isa<UnsetInit>(R->getValue())) {
  2232. Unset = true;
  2233. return false;
  2234. }
  2235. Unset = false;
  2236. if (BitInit *BI = dyn_cast<BitInit>(R->getValue()))
  2237. return BI->getValue();
  2238. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  2239. FieldName + "' does not have a bit initializer!");
  2240. }
  2241. DagInit *Record::getValueAsDag(StringRef FieldName) const {
  2242. const RecordVal *R = getValue(FieldName);
  2243. if (!R || !R->getValue())
  2244. PrintFatalError(getLoc(), "Record `" + getName() +
  2245. "' does not have a field named `" + FieldName + "'!\n");
  2246. if (DagInit *DI = dyn_cast<DagInit>(R->getValue()))
  2247. return DI;
  2248. PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
  2249. FieldName + "' does not have a dag initializer!");
  2250. }
  2251. // Check all record assertions: For each one, resolve the condition
  2252. // and message, then call CheckAssert().
  2253. // Note: The condition and message are probably already resolved,
  2254. // but resolving again allows calls before records are resolved.
  2255. void Record::checkRecordAssertions() {
  2256. RecordResolver R(*this);
  2257. R.setFinal(true);
  2258. for (const auto &Assertion : getAssertions()) {
  2259. Init *Condition = Assertion.Condition->resolveReferences(R);
  2260. Init *Message = Assertion.Message->resolveReferences(R);
  2261. CheckAssert(Assertion.Loc, Condition, Message);
  2262. }
  2263. }
  2264. // Report a warning if the record has unused template arguments.
  2265. void Record::checkUnusedTemplateArgs() {
  2266. for (const Init *TA : getTemplateArgs()) {
  2267. const RecordVal *Arg = getValue(TA);
  2268. if (!Arg->isUsed())
  2269. PrintWarning(Arg->getLoc(),
  2270. "unused template argument: " + Twine(Arg->getName()));
  2271. }
  2272. }
  2273. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  2274. LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; }
  2275. #endif
  2276. raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) {
  2277. OS << "------------- Classes -----------------\n";
  2278. for (const auto &C : RK.getClasses())
  2279. OS << "class " << *C.second;
  2280. OS << "------------- Defs -----------------\n";
  2281. for (const auto &D : RK.getDefs())
  2282. OS << "def " << *D.second;
  2283. return OS;
  2284. }
  2285. /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
  2286. /// an identifier.
  2287. Init *RecordKeeper::getNewAnonymousName() {
  2288. return AnonymousNameInit::get(AnonCounter++);
  2289. }
  2290. // These functions implement the phase timing facility. Starting a timer
  2291. // when one is already running stops the running one.
  2292. void RecordKeeper::startTimer(StringRef Name) {
  2293. if (TimingGroup) {
  2294. if (LastTimer && LastTimer->isRunning()) {
  2295. LastTimer->stopTimer();
  2296. if (BackendTimer) {
  2297. LastTimer->clear();
  2298. BackendTimer = false;
  2299. }
  2300. }
  2301. LastTimer = new Timer("", Name, *TimingGroup);
  2302. LastTimer->startTimer();
  2303. }
  2304. }
  2305. void RecordKeeper::stopTimer() {
  2306. if (TimingGroup) {
  2307. assert(LastTimer && "No phase timer was started");
  2308. LastTimer->stopTimer();
  2309. }
  2310. }
  2311. void RecordKeeper::startBackendTimer(StringRef Name) {
  2312. if (TimingGroup) {
  2313. startTimer(Name);
  2314. BackendTimer = true;
  2315. }
  2316. }
  2317. void RecordKeeper::stopBackendTimer() {
  2318. if (TimingGroup) {
  2319. if (BackendTimer) {
  2320. stopTimer();
  2321. BackendTimer = false;
  2322. }
  2323. }
  2324. }
  2325. // We cache the record vectors for single classes. Many backends request
  2326. // the same vectors multiple times.
  2327. std::vector<Record *> RecordKeeper::getAllDerivedDefinitions(
  2328. StringRef ClassName) const {
  2329. auto Pair = ClassRecordsMap.try_emplace(ClassName);
  2330. if (Pair.second)
  2331. Pair.first->second = getAllDerivedDefinitions(makeArrayRef(ClassName));
  2332. return Pair.first->second;
  2333. }
  2334. std::vector<Record *> RecordKeeper::getAllDerivedDefinitions(
  2335. ArrayRef<StringRef> ClassNames) const {
  2336. SmallVector<Record *, 2> ClassRecs;
  2337. std::vector<Record *> Defs;
  2338. assert(ClassNames.size() > 0 && "At least one class must be passed.");
  2339. for (const auto &ClassName : ClassNames) {
  2340. Record *Class = getClass(ClassName);
  2341. if (!Class)
  2342. PrintFatalError("The class '" + ClassName + "' is not defined\n");
  2343. ClassRecs.push_back(Class);
  2344. }
  2345. for (const auto &OneDef : getDefs()) {
  2346. if (all_of(ClassRecs, [&OneDef](const Record *Class) {
  2347. return OneDef.second->isSubClassOf(Class);
  2348. }))
  2349. Defs.push_back(OneDef.second.get());
  2350. }
  2351. return Defs;
  2352. }
  2353. Init *MapResolver::resolve(Init *VarName) {
  2354. auto It = Map.find(VarName);
  2355. if (It == Map.end())
  2356. return nullptr;
  2357. Init *I = It->second.V;
  2358. if (!It->second.Resolved && Map.size() > 1) {
  2359. // Resolve mutual references among the mapped variables, but prevent
  2360. // infinite recursion.
  2361. Map.erase(It);
  2362. I = I->resolveReferences(*this);
  2363. Map[VarName] = {I, true};
  2364. }
  2365. return I;
  2366. }
  2367. Init *RecordResolver::resolve(Init *VarName) {
  2368. Init *Val = Cache.lookup(VarName);
  2369. if (Val)
  2370. return Val;
  2371. if (llvm::is_contained(Stack, VarName))
  2372. return nullptr; // prevent infinite recursion
  2373. if (RecordVal *RV = getCurrentRecord()->getValue(VarName)) {
  2374. if (!isa<UnsetInit>(RV->getValue())) {
  2375. Val = RV->getValue();
  2376. Stack.push_back(VarName);
  2377. Val = Val->resolveReferences(*this);
  2378. Stack.pop_back();
  2379. }
  2380. } else if (Name && VarName == getCurrentRecord()->getNameInit()) {
  2381. Stack.push_back(VarName);
  2382. Val = Name->resolveReferences(*this);
  2383. Stack.pop_back();
  2384. }
  2385. Cache[VarName] = Val;
  2386. return Val;
  2387. }
  2388. Init *TrackUnresolvedResolver::resolve(Init *VarName) {
  2389. Init *I = nullptr;
  2390. if (R) {
  2391. I = R->resolve(VarName);
  2392. if (I && !FoundUnresolved) {
  2393. // Do not recurse into the resolved initializer, as that would change
  2394. // the behavior of the resolver we're delegating, but do check to see
  2395. // if there are unresolved variables remaining.
  2396. TrackUnresolvedResolver Sub;
  2397. I->resolveReferences(Sub);
  2398. FoundUnresolved |= Sub.FoundUnresolved;
  2399. }
  2400. }
  2401. if (!I)
  2402. FoundUnresolved = true;
  2403. return I;
  2404. }
  2405. Init *HasReferenceResolver::resolve(Init *VarName)
  2406. {
  2407. if (VarName == VarNameToTrack)
  2408. Found = true;
  2409. return nullptr;
  2410. }