Record.h 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the main TableGen data structures, including the TableGen
  11. // types, values, and high-level data structures.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TABLEGEN_RECORD_H
  15. #define LLVM_TABLEGEN_RECORD_H
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/FoldingSet.h"
  18. #include "llvm/ADT/PointerIntPair.h"
  19. #include "llvm/Support/Casting.h"
  20. #include "llvm/Support/DataTypes.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Support/SMLoc.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include <map>
  25. namespace llvm {
  26. class ListRecTy;
  27. struct MultiClass;
  28. class Record;
  29. class RecordVal;
  30. class RecordKeeper;
  31. //===----------------------------------------------------------------------===//
  32. // Type Classes
  33. //===----------------------------------------------------------------------===//
  34. class RecTy {
  35. public:
  36. /// \brief Subclass discriminator (for dyn_cast<> et al.)
  37. enum RecTyKind {
  38. BitRecTyKind,
  39. BitsRecTyKind,
  40. IntRecTyKind,
  41. StringRecTyKind,
  42. ListRecTyKind,
  43. DagRecTyKind,
  44. RecordRecTyKind
  45. };
  46. private:
  47. RecTyKind Kind;
  48. std::unique_ptr<ListRecTy> ListTy;
  49. public:
  50. RecTyKind getRecTyKind() const { return Kind; }
  51. RecTy(RecTyKind K) : Kind(K) {}
  52. virtual ~RecTy() {}
  53. virtual std::string getAsString() const = 0;
  54. void print(raw_ostream &OS) const { OS << getAsString(); }
  55. void dump() const;
  56. /// typeIsConvertibleTo - Return true if all values of 'this' type can be
  57. /// converted to the specified type.
  58. virtual bool typeIsConvertibleTo(const RecTy *RHS) const;
  59. /// getListTy - Returns the type representing list<this>.
  60. ListRecTy *getListTy();
  61. };
  62. inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
  63. Ty.print(OS);
  64. return OS;
  65. }
  66. /// BitRecTy - 'bit' - Represent a single bit
  67. ///
  68. class BitRecTy : public RecTy {
  69. static BitRecTy Shared;
  70. BitRecTy() : RecTy(BitRecTyKind) {}
  71. public:
  72. static bool classof(const RecTy *RT) {
  73. return RT->getRecTyKind() == BitRecTyKind;
  74. }
  75. static BitRecTy *get() { return &Shared; }
  76. std::string getAsString() const override { return "bit"; }
  77. bool typeIsConvertibleTo(const RecTy *RHS) const override;
  78. };
  79. /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
  80. ///
  81. class BitsRecTy : public RecTy {
  82. unsigned Size;
  83. explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
  84. public:
  85. static bool classof(const RecTy *RT) {
  86. return RT->getRecTyKind() == BitsRecTyKind;
  87. }
  88. static BitsRecTy *get(unsigned Sz);
  89. unsigned getNumBits() const { return Size; }
  90. std::string getAsString() const override;
  91. bool typeIsConvertibleTo(const RecTy *RHS) const override;
  92. };
  93. /// IntRecTy - 'int' - Represent an integer value of no particular size
  94. ///
  95. class IntRecTy : public RecTy {
  96. static IntRecTy Shared;
  97. IntRecTy() : RecTy(IntRecTyKind) {}
  98. public:
  99. static bool classof(const RecTy *RT) {
  100. return RT->getRecTyKind() == IntRecTyKind;
  101. }
  102. static IntRecTy *get() { return &Shared; }
  103. std::string getAsString() const override { return "int"; }
  104. bool typeIsConvertibleTo(const RecTy *RHS) const override;
  105. };
  106. /// StringRecTy - 'string' - Represent an string value
  107. ///
  108. class StringRecTy : public RecTy {
  109. static StringRecTy Shared;
  110. StringRecTy() : RecTy(StringRecTyKind) {}
  111. public:
  112. static bool classof(const RecTy *RT) {
  113. return RT->getRecTyKind() == StringRecTyKind;
  114. }
  115. static StringRecTy *get() { return &Shared; }
  116. std::string getAsString() const override;
  117. };
  118. /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
  119. /// the specified type.
  120. ///
  121. class ListRecTy : public RecTy {
  122. RecTy *Ty;
  123. explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
  124. friend ListRecTy *RecTy::getListTy();
  125. public:
  126. static bool classof(const RecTy *RT) {
  127. return RT->getRecTyKind() == ListRecTyKind;
  128. }
  129. static ListRecTy *get(RecTy *T) { return T->getListTy(); }
  130. RecTy *getElementType() const { return Ty; }
  131. std::string getAsString() const override;
  132. bool typeIsConvertibleTo(const RecTy *RHS) const override;
  133. };
  134. /// DagRecTy - 'dag' - Represent a dag fragment
  135. ///
  136. class DagRecTy : public RecTy {
  137. static DagRecTy Shared;
  138. DagRecTy() : RecTy(DagRecTyKind) {}
  139. public:
  140. static bool classof(const RecTy *RT) {
  141. return RT->getRecTyKind() == DagRecTyKind;
  142. }
  143. static DagRecTy *get() { return &Shared; }
  144. std::string getAsString() const override;
  145. };
  146. /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
  147. /// (R32 X = EAX).
  148. ///
  149. class RecordRecTy : public RecTy {
  150. Record *Rec;
  151. explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
  152. friend class Record;
  153. public:
  154. static bool classof(const RecTy *RT) {
  155. return RT->getRecTyKind() == RecordRecTyKind;
  156. }
  157. static RecordRecTy *get(Record *R);
  158. Record *getRecord() const { return Rec; }
  159. std::string getAsString() const override;
  160. bool typeIsConvertibleTo(const RecTy *RHS) const override;
  161. };
  162. /// resolveTypes - Find a common type that T1 and T2 convert to.
  163. /// Return 0 if no such type exists.
  164. ///
  165. RecTy *resolveTypes(RecTy *T1, RecTy *T2);
  166. //===----------------------------------------------------------------------===//
  167. // Initializer Classes
  168. //===----------------------------------------------------------------------===//
  169. class Init {
  170. protected:
  171. /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
  172. ///
  173. /// This enum is laid out by a preorder traversal of the inheritance
  174. /// hierarchy, and does not contain an entry for abstract classes, as per
  175. /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
  176. ///
  177. /// We also explicitly include "first" and "last" values for each
  178. /// interior node of the inheritance tree, to make it easier to read the
  179. /// corresponding classof().
  180. ///
  181. /// We could pack these a bit tighter by not having the IK_FirstXXXInit
  182. /// and IK_LastXXXInit be their own values, but that would degrade
  183. /// readability for really no benefit.
  184. enum InitKind {
  185. IK_BitInit,
  186. IK_FirstTypedInit,
  187. IK_BitsInit,
  188. IK_DagInit,
  189. IK_DefInit,
  190. IK_FieldInit,
  191. IK_IntInit,
  192. IK_ListInit,
  193. IK_FirstOpInit,
  194. IK_BinOpInit,
  195. IK_TernOpInit,
  196. IK_UnOpInit,
  197. IK_LastOpInit,
  198. IK_StringInit,
  199. IK_VarInit,
  200. IK_VarListElementInit,
  201. IK_LastTypedInit,
  202. IK_UnsetInit,
  203. IK_VarBitInit
  204. };
  205. private:
  206. const InitKind Kind;
  207. Init(const Init &) = delete;
  208. Init &operator=(const Init &) = delete;
  209. virtual void anchor();
  210. public:
  211. InitKind getKind() const { return Kind; }
  212. protected:
  213. explicit Init(InitKind K) : Kind(K) {}
  214. public:
  215. virtual ~Init() {}
  216. /// isComplete - This virtual method should be overridden by values that may
  217. /// not be completely specified yet.
  218. virtual bool isComplete() const { return true; }
  219. /// print - Print out this value.
  220. void print(raw_ostream &OS) const { OS << getAsString(); }
  221. /// getAsString - Convert this value to a string form.
  222. virtual std::string getAsString() const = 0;
  223. /// getAsUnquotedString - Convert this value to a string form,
  224. /// without adding quote markers. This primaruly affects
  225. /// StringInits where we will not surround the string value with
  226. /// quotes.
  227. virtual std::string getAsUnquotedString() const { return getAsString(); }
  228. /// dump - Debugging method that may be called through a debugger, just
  229. /// invokes print on stderr.
  230. void dump() const;
  231. /// convertInitializerTo - This virtual function converts to the appropriate
  232. /// Init based on the passed in type.
  233. virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
  234. /// convertInitializerBitRange - This method is used to implement the bitrange
  235. /// selection operator. Given an initializer, it selects the specified bits
  236. /// out, returning them as a new init of bits type. If it is not legal to use
  237. /// the bit subscript operator on this initializer, return null.
  238. ///
  239. virtual Init *
  240. convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
  241. return nullptr;
  242. }
  243. /// convertInitListSlice - This method is used to implement the list slice
  244. /// selection operator. Given an initializer, it selects the specified list
  245. /// elements, returning them as a new init of list type. If it is not legal
  246. /// to take a slice of this, return null.
  247. ///
  248. virtual Init *
  249. convertInitListSlice(const std::vector<unsigned> &Elements) const {
  250. return nullptr;
  251. }
  252. /// getFieldType - This method is used to implement the FieldInit class.
  253. /// Implementors of this method should return the type of the named field if
  254. /// they are of record type.
  255. ///
  256. virtual RecTy *getFieldType(const std::string &FieldName) const {
  257. return nullptr;
  258. }
  259. /// getFieldInit - This method complements getFieldType to return the
  260. /// initializer for the specified field. If getFieldType returns non-null
  261. /// this method should return non-null, otherwise it returns null.
  262. ///
  263. virtual Init *getFieldInit(Record &R, const RecordVal *RV,
  264. const std::string &FieldName) const {
  265. return nullptr;
  266. }
  267. /// resolveReferences - This method is used by classes that refer to other
  268. /// variables which may not be defined at the time the expression is formed.
  269. /// If a value is set for the variable later, this method will be called on
  270. /// users of the value to allow the value to propagate out.
  271. ///
  272. virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
  273. return const_cast<Init *>(this);
  274. }
  275. /// getBit - This method is used to return the initializer for the specified
  276. /// bit.
  277. virtual Init *getBit(unsigned Bit) const = 0;
  278. /// getBitVar - This method is used to retrieve the initializer for bit
  279. /// reference. For non-VarBitInit, it simply returns itself.
  280. virtual Init *getBitVar() const { return const_cast<Init*>(this); }
  281. /// getBitNum - This method is used to retrieve the bit number of a bit
  282. /// reference. For non-VarBitInit, it simply returns 0.
  283. virtual unsigned getBitNum() const { return 0; }
  284. };
  285. inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
  286. I.print(OS); return OS;
  287. }
  288. /// TypedInit - This is the common super-class of types that have a specific,
  289. /// explicit, type.
  290. ///
  291. class TypedInit : public Init {
  292. RecTy *Ty;
  293. TypedInit(const TypedInit &Other) = delete;
  294. TypedInit &operator=(const TypedInit &Other) = delete;
  295. protected:
  296. explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
  297. ~TypedInit() {
  298. // If this is a DefInit we need to delete the RecordRecTy.
  299. if (getKind() == IK_DefInit)
  300. delete Ty;
  301. }
  302. public:
  303. static bool classof(const Init *I) {
  304. return I->getKind() >= IK_FirstTypedInit &&
  305. I->getKind() <= IK_LastTypedInit;
  306. }
  307. RecTy *getType() const { return Ty; }
  308. Init *convertInitializerTo(RecTy *Ty) const override;
  309. Init *
  310. convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
  311. Init *
  312. convertInitListSlice(const std::vector<unsigned> &Elements) const override;
  313. /// getFieldType - This method is used to implement the FieldInit class.
  314. /// Implementors of this method should return the type of the named field if
  315. /// they are of record type.
  316. ///
  317. RecTy *getFieldType(const std::string &FieldName) const override;
  318. /// resolveListElementReference - This method is used to implement
  319. /// VarListElementInit::resolveReferences. If the list element is resolvable
  320. /// now, we return the resolved value, otherwise we return null.
  321. virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
  322. unsigned Elt) const = 0;
  323. };
  324. /// UnsetInit - ? - Represents an uninitialized value
  325. ///
  326. class UnsetInit : public Init {
  327. UnsetInit() : Init(IK_UnsetInit) {}
  328. UnsetInit(const UnsetInit &) = delete;
  329. UnsetInit &operator=(const UnsetInit &Other) = delete;
  330. public:
  331. static bool classof(const Init *I) {
  332. return I->getKind() == IK_UnsetInit;
  333. }
  334. static UnsetInit *get();
  335. Init *convertInitializerTo(RecTy *Ty) const override;
  336. Init *getBit(unsigned Bit) const override {
  337. return const_cast<UnsetInit*>(this);
  338. }
  339. bool isComplete() const override { return false; }
  340. std::string getAsString() const override { return "?"; }
  341. };
  342. /// BitInit - true/false - Represent a concrete initializer for a bit.
  343. ///
  344. class BitInit : public Init {
  345. bool Value;
  346. explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
  347. BitInit(const BitInit &Other) = delete;
  348. BitInit &operator=(BitInit &Other) = delete;
  349. public:
  350. static bool classof(const Init *I) {
  351. return I->getKind() == IK_BitInit;
  352. }
  353. static BitInit *get(bool V);
  354. bool getValue() const { return Value; }
  355. Init *convertInitializerTo(RecTy *Ty) const override;
  356. Init *getBit(unsigned Bit) const override {
  357. assert(Bit < 1 && "Bit index out of range!");
  358. return const_cast<BitInit*>(this);
  359. }
  360. std::string getAsString() const override { return Value ? "1" : "0"; }
  361. };
  362. /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
  363. /// It contains a vector of bits, whose size is determined by the type.
  364. ///
  365. class BitsInit : public TypedInit, public FoldingSetNode {
  366. std::vector<Init*> Bits;
  367. BitsInit(ArrayRef<Init *> Range)
  368. : TypedInit(IK_BitsInit, BitsRecTy::get(Range.size())),
  369. Bits(Range.begin(), Range.end()) {}
  370. BitsInit(const BitsInit &Other) = delete;
  371. BitsInit &operator=(const BitsInit &Other) = delete;
  372. public:
  373. static bool classof(const Init *I) {
  374. return I->getKind() == IK_BitsInit;
  375. }
  376. static BitsInit *get(ArrayRef<Init *> Range);
  377. void Profile(FoldingSetNodeID &ID) const;
  378. unsigned getNumBits() const { return Bits.size(); }
  379. Init *convertInitializerTo(RecTy *Ty) const override;
  380. Init *
  381. convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
  382. bool isComplete() const override {
  383. for (unsigned i = 0; i != getNumBits(); ++i)
  384. if (!getBit(i)->isComplete()) return false;
  385. return true;
  386. }
  387. bool allInComplete() const {
  388. for (unsigned i = 0; i != getNumBits(); ++i)
  389. if (getBit(i)->isComplete()) return false;
  390. return true;
  391. }
  392. std::string getAsString() const override;
  393. /// resolveListElementReference - This method is used to implement
  394. /// VarListElementInit::resolveReferences. If the list element is resolvable
  395. /// now, we return the resolved value, otherwise we return null.
  396. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  397. unsigned Elt) const override {
  398. llvm_unreachable("Illegal element reference off bits<n>");
  399. }
  400. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  401. Init *getBit(unsigned Bit) const override {
  402. assert(Bit < Bits.size() && "Bit index out of range!");
  403. return Bits[Bit];
  404. }
  405. };
  406. /// IntInit - 7 - Represent an initialization by a literal integer value.
  407. ///
  408. class IntInit : public TypedInit {
  409. int64_t Value;
  410. explicit IntInit(int64_t V)
  411. : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
  412. IntInit(const IntInit &Other) = delete;
  413. IntInit &operator=(const IntInit &Other) = delete;
  414. public:
  415. static bool classof(const Init *I) {
  416. return I->getKind() == IK_IntInit;
  417. }
  418. static IntInit *get(int64_t V);
  419. int64_t getValue() const { return Value; }
  420. Init *convertInitializerTo(RecTy *Ty) const override;
  421. Init *
  422. convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
  423. std::string getAsString() const override;
  424. /// resolveListElementReference - This method is used to implement
  425. /// VarListElementInit::resolveReferences. If the list element is resolvable
  426. /// now, we return the resolved value, otherwise we return null.
  427. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  428. unsigned Elt) const override {
  429. llvm_unreachable("Illegal element reference off int");
  430. }
  431. Init *getBit(unsigned Bit) const override {
  432. return BitInit::get((Value & (1ULL << Bit)) != 0);
  433. }
  434. };
  435. /// StringInit - "foo" - Represent an initialization by a string value.
  436. ///
  437. class StringInit : public TypedInit {
  438. std::string Value;
  439. explicit StringInit(const std::string &V)
  440. : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
  441. StringInit(const StringInit &Other) = delete;
  442. StringInit &operator=(const StringInit &Other) = delete;
  443. public:
  444. static bool classof(const Init *I) {
  445. return I->getKind() == IK_StringInit;
  446. }
  447. static StringInit *get(StringRef);
  448. const std::string &getValue() const { return Value; }
  449. Init *convertInitializerTo(RecTy *Ty) const override;
  450. std::string getAsString() const override { return "\"" + Value + "\""; }
  451. std::string getAsUnquotedString() const override { return Value; }
  452. /// resolveListElementReference - This method is used to implement
  453. /// VarListElementInit::resolveReferences. If the list element is resolvable
  454. /// now, we return the resolved value, otherwise we return null.
  455. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  456. unsigned Elt) const override {
  457. llvm_unreachable("Illegal element reference off string");
  458. }
  459. Init *getBit(unsigned Bit) const override {
  460. llvm_unreachable("Illegal bit reference off string");
  461. }
  462. };
  463. /// ListInit - [AL, AH, CL] - Represent a list of defs
  464. ///
  465. class ListInit : public TypedInit, public FoldingSetNode {
  466. std::vector<Init*> Values;
  467. public:
  468. typedef std::vector<Init*>::const_iterator const_iterator;
  469. private:
  470. explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
  471. : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
  472. Values(Range.begin(), Range.end()) {}
  473. ListInit(const ListInit &Other) = delete;
  474. ListInit &operator=(const ListInit &Other) = delete;
  475. public:
  476. static bool classof(const Init *I) {
  477. return I->getKind() == IK_ListInit;
  478. }
  479. static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
  480. void Profile(FoldingSetNodeID &ID) const;
  481. Init *getElement(unsigned i) const {
  482. assert(i < Values.size() && "List element index out of range!");
  483. return Values[i];
  484. }
  485. Record *getElementAsRecord(unsigned i) const;
  486. Init *
  487. convertInitListSlice(const std::vector<unsigned> &Elements) const override;
  488. Init *convertInitializerTo(RecTy *Ty) const override;
  489. /// resolveReferences - This method is used by classes that refer to other
  490. /// variables which may not be defined at the time they expression is formed.
  491. /// If a value is set for the variable later, this method will be called on
  492. /// users of the value to allow the value to propagate out.
  493. ///
  494. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  495. std::string getAsString() const override;
  496. ArrayRef<Init*> getValues() const { return Values; }
  497. const_iterator begin() const { return Values.begin(); }
  498. const_iterator end () const { return Values.end(); }
  499. size_t size () const { return Values.size(); }
  500. bool empty() const { return Values.empty(); }
  501. /// resolveListElementReference - This method is used to implement
  502. /// VarListElementInit::resolveReferences. If the list element is resolvable
  503. /// now, we return the resolved value, otherwise we return null.
  504. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  505. unsigned Elt) const override;
  506. Init *getBit(unsigned Bit) const override {
  507. llvm_unreachable("Illegal bit reference off list");
  508. }
  509. };
  510. /// OpInit - Base class for operators
  511. ///
  512. class OpInit : public TypedInit {
  513. OpInit(const OpInit &Other) = delete;
  514. OpInit &operator=(OpInit &Other) = delete;
  515. protected:
  516. explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
  517. public:
  518. static bool classof(const Init *I) {
  519. return I->getKind() >= IK_FirstOpInit &&
  520. I->getKind() <= IK_LastOpInit;
  521. }
  522. // Clone - Clone this operator, replacing arguments with the new list
  523. virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
  524. virtual unsigned getNumOperands() const = 0;
  525. virtual Init *getOperand(unsigned i) const = 0;
  526. // Fold - If possible, fold this to a simpler init. Return this if not
  527. // possible to fold.
  528. virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
  529. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  530. unsigned Elt) const override;
  531. Init *getBit(unsigned Bit) const override;
  532. };
  533. /// UnOpInit - !op (X) - Transform an init.
  534. ///
  535. class UnOpInit : public OpInit {
  536. public:
  537. enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
  538. private:
  539. UnaryOp Opc;
  540. Init *LHS;
  541. UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
  542. : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
  543. UnOpInit(const UnOpInit &Other) = delete;
  544. UnOpInit &operator=(const UnOpInit &Other) = delete;
  545. public:
  546. static bool classof(const Init *I) {
  547. return I->getKind() == IK_UnOpInit;
  548. }
  549. static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
  550. // Clone - Clone this operator, replacing arguments with the new list
  551. OpInit *clone(std::vector<Init *> &Operands) const override {
  552. assert(Operands.size() == 1 &&
  553. "Wrong number of operands for unary operation");
  554. return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
  555. }
  556. unsigned getNumOperands() const override { return 1; }
  557. Init *getOperand(unsigned i) const override {
  558. assert(i == 0 && "Invalid operand id for unary operator");
  559. return getOperand();
  560. }
  561. UnaryOp getOpcode() const { return Opc; }
  562. Init *getOperand() const { return LHS; }
  563. // Fold - If possible, fold this to a simpler init. Return this if not
  564. // possible to fold.
  565. Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
  566. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  567. std::string getAsString() const override;
  568. };
  569. /// BinOpInit - !op (X, Y) - Combine two inits.
  570. ///
  571. class BinOpInit : public OpInit {
  572. public:
  573. enum BinaryOp { ADD, AND, SHL, SRA, SRL, LISTCONCAT, STRCONCAT, CONCAT, EQ };
  574. private:
  575. BinaryOp Opc;
  576. Init *LHS, *RHS;
  577. BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
  578. OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
  579. BinOpInit(const BinOpInit &Other) = delete;
  580. BinOpInit &operator=(const BinOpInit &Other) = delete;
  581. public:
  582. static bool classof(const Init *I) {
  583. return I->getKind() == IK_BinOpInit;
  584. }
  585. static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
  586. RecTy *Type);
  587. // Clone - Clone this operator, replacing arguments with the new list
  588. OpInit *clone(std::vector<Init *> &Operands) const override {
  589. assert(Operands.size() == 2 &&
  590. "Wrong number of operands for binary operation");
  591. return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
  592. }
  593. unsigned getNumOperands() const override { return 2; }
  594. Init *getOperand(unsigned i) const override {
  595. switch (i) {
  596. default: llvm_unreachable("Invalid operand id for binary operator");
  597. case 0: return getLHS();
  598. case 1: return getRHS();
  599. }
  600. }
  601. BinaryOp getOpcode() const { return Opc; }
  602. Init *getLHS() const { return LHS; }
  603. Init *getRHS() const { return RHS; }
  604. // Fold - If possible, fold this to a simpler init. Return this if not
  605. // possible to fold.
  606. Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
  607. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  608. std::string getAsString() const override;
  609. };
  610. /// TernOpInit - !op (X, Y, Z) - Combine two inits.
  611. ///
  612. class TernOpInit : public OpInit {
  613. public:
  614. enum TernaryOp { SUBST, FOREACH, IF };
  615. private:
  616. TernaryOp Opc;
  617. Init *LHS, *MHS, *RHS;
  618. TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
  619. RecTy *Type) :
  620. OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
  621. TernOpInit(const TernOpInit &Other) = delete;
  622. TernOpInit &operator=(const TernOpInit &Other) = delete;
  623. public:
  624. static bool classof(const Init *I) {
  625. return I->getKind() == IK_TernOpInit;
  626. }
  627. static TernOpInit *get(TernaryOp opc, Init *lhs,
  628. Init *mhs, Init *rhs,
  629. RecTy *Type);
  630. // Clone - Clone this operator, replacing arguments with the new list
  631. OpInit *clone(std::vector<Init *> &Operands) const override {
  632. assert(Operands.size() == 3 &&
  633. "Wrong number of operands for ternary operation");
  634. return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
  635. getType());
  636. }
  637. unsigned getNumOperands() const override { return 3; }
  638. Init *getOperand(unsigned i) const override {
  639. switch (i) {
  640. default: llvm_unreachable("Invalid operand id for ternary operator");
  641. case 0: return getLHS();
  642. case 1: return getMHS();
  643. case 2: return getRHS();
  644. }
  645. }
  646. TernaryOp getOpcode() const { return Opc; }
  647. Init *getLHS() const { return LHS; }
  648. Init *getMHS() const { return MHS; }
  649. Init *getRHS() const { return RHS; }
  650. // Fold - If possible, fold this to a simpler init. Return this if not
  651. // possible to fold.
  652. Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
  653. bool isComplete() const override { return false; }
  654. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  655. std::string getAsString() const override;
  656. };
  657. /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
  658. ///
  659. class VarInit : public TypedInit {
  660. Init *VarName;
  661. explicit VarInit(const std::string &VN, RecTy *T)
  662. : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
  663. explicit VarInit(Init *VN, RecTy *T)
  664. : TypedInit(IK_VarInit, T), VarName(VN) {}
  665. VarInit(const VarInit &Other) = delete;
  666. VarInit &operator=(const VarInit &Other) = delete;
  667. public:
  668. static bool classof(const Init *I) {
  669. return I->getKind() == IK_VarInit;
  670. }
  671. static VarInit *get(const std::string &VN, RecTy *T);
  672. static VarInit *get(Init *VN, RecTy *T);
  673. const std::string &getName() const;
  674. Init *getNameInit() const { return VarName; }
  675. std::string getNameInitAsString() const {
  676. return getNameInit()->getAsUnquotedString();
  677. }
  678. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  679. unsigned Elt) const override;
  680. RecTy *getFieldType(const std::string &FieldName) const override;
  681. Init *getFieldInit(Record &R, const RecordVal *RV,
  682. const std::string &FieldName) const override;
  683. /// resolveReferences - This method is used by classes that refer to other
  684. /// variables which may not be defined at the time they expression is formed.
  685. /// If a value is set for the variable later, this method will be called on
  686. /// users of the value to allow the value to propagate out.
  687. ///
  688. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  689. Init *getBit(unsigned Bit) const override;
  690. std::string getAsString() const override { return getName(); }
  691. };
  692. /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
  693. ///
  694. class VarBitInit : public Init {
  695. TypedInit *TI;
  696. unsigned Bit;
  697. VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
  698. assert(T->getType() &&
  699. (isa<IntRecTy>(T->getType()) ||
  700. (isa<BitsRecTy>(T->getType()) &&
  701. cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
  702. "Illegal VarBitInit expression!");
  703. }
  704. VarBitInit(const VarBitInit &Other) = delete;
  705. VarBitInit &operator=(const VarBitInit &Other) = delete;
  706. public:
  707. static bool classof(const Init *I) {
  708. return I->getKind() == IK_VarBitInit;
  709. }
  710. static VarBitInit *get(TypedInit *T, unsigned B);
  711. Init *convertInitializerTo(RecTy *Ty) const override;
  712. Init *getBitVar() const override { return TI; }
  713. unsigned getBitNum() const override { return Bit; }
  714. std::string getAsString() const override;
  715. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  716. Init *getBit(unsigned B) const override {
  717. assert(B < 1 && "Bit index out of range!");
  718. return const_cast<VarBitInit*>(this);
  719. }
  720. };
  721. /// VarListElementInit - List[4] - Represent access to one element of a var or
  722. /// field.
  723. class VarListElementInit : public TypedInit {
  724. TypedInit *TI;
  725. unsigned Element;
  726. VarListElementInit(TypedInit *T, unsigned E)
  727. : TypedInit(IK_VarListElementInit,
  728. cast<ListRecTy>(T->getType())->getElementType()),
  729. TI(T), Element(E) {
  730. assert(T->getType() && isa<ListRecTy>(T->getType()) &&
  731. "Illegal VarBitInit expression!");
  732. }
  733. VarListElementInit(const VarListElementInit &Other) = delete;
  734. void operator=(const VarListElementInit &Other) = delete;
  735. public:
  736. static bool classof(const Init *I) {
  737. return I->getKind() == IK_VarListElementInit;
  738. }
  739. static VarListElementInit *get(TypedInit *T, unsigned E);
  740. TypedInit *getVariable() const { return TI; }
  741. unsigned getElementNum() const { return Element; }
  742. /// resolveListElementReference - This method is used to implement
  743. /// VarListElementInit::resolveReferences. If the list element is resolvable
  744. /// now, we return the resolved value, otherwise we return null.
  745. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  746. unsigned Elt) const override;
  747. std::string getAsString() const override;
  748. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  749. Init *getBit(unsigned Bit) const override;
  750. };
  751. /// DefInit - AL - Represent a reference to a 'def' in the description
  752. ///
  753. class DefInit : public TypedInit {
  754. Record *Def;
  755. DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
  756. friend class Record;
  757. DefInit(const DefInit &Other) = delete;
  758. DefInit &operator=(const DefInit &Other) = delete;
  759. public:
  760. static bool classof(const Init *I) {
  761. return I->getKind() == IK_DefInit;
  762. }
  763. static DefInit *get(Record*);
  764. Init *convertInitializerTo(RecTy *Ty) const override;
  765. Record *getDef() const { return Def; }
  766. //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
  767. RecTy *getFieldType(const std::string &FieldName) const override;
  768. Init *getFieldInit(Record &R, const RecordVal *RV,
  769. const std::string &FieldName) const override;
  770. std::string getAsString() const override;
  771. Init *getBit(unsigned Bit) const override {
  772. llvm_unreachable("Illegal bit reference off def");
  773. }
  774. /// resolveListElementReference - This method is used to implement
  775. /// VarListElementInit::resolveReferences. If the list element is resolvable
  776. /// now, we return the resolved value, otherwise we return null.
  777. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  778. unsigned Elt) const override {
  779. llvm_unreachable("Illegal element reference off def");
  780. }
  781. };
  782. /// FieldInit - X.Y - Represent a reference to a subfield of a variable
  783. ///
  784. class FieldInit : public TypedInit {
  785. Init *Rec; // Record we are referring to
  786. std::string FieldName; // Field we are accessing
  787. FieldInit(Init *R, const std::string &FN)
  788. : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
  789. assert(getType() && "FieldInit with non-record type!");
  790. }
  791. FieldInit(const FieldInit &Other) = delete;
  792. FieldInit &operator=(const FieldInit &Other) = delete;
  793. public:
  794. static bool classof(const Init *I) {
  795. return I->getKind() == IK_FieldInit;
  796. }
  797. static FieldInit *get(Init *R, const std::string &FN);
  798. Init *getBit(unsigned Bit) const override;
  799. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  800. unsigned Elt) const override;
  801. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  802. std::string getAsString() const override {
  803. return Rec->getAsString() + "." + FieldName;
  804. }
  805. };
  806. /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
  807. /// to have at least one value then a (possibly empty) list of arguments. Each
  808. /// argument can have a name associated with it.
  809. ///
  810. class DagInit : public TypedInit, public FoldingSetNode {
  811. Init *Val;
  812. std::string ValName;
  813. std::vector<Init*> Args;
  814. std::vector<std::string> ArgNames;
  815. DagInit(Init *V, const std::string &VN,
  816. ArrayRef<Init *> ArgRange,
  817. ArrayRef<std::string> NameRange)
  818. : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
  819. Args(ArgRange.begin(), ArgRange.end()),
  820. ArgNames(NameRange.begin(), NameRange.end()) {}
  821. DagInit(const DagInit &Other) = delete;
  822. DagInit &operator=(const DagInit &Other) = delete;
  823. public:
  824. static bool classof(const Init *I) {
  825. return I->getKind() == IK_DagInit;
  826. }
  827. static DagInit *get(Init *V, const std::string &VN,
  828. ArrayRef<Init *> ArgRange,
  829. ArrayRef<std::string> NameRange);
  830. static DagInit *get(Init *V, const std::string &VN,
  831. const std::vector<
  832. std::pair<Init*, std::string> > &args);
  833. void Profile(FoldingSetNodeID &ID) const;
  834. Init *convertInitializerTo(RecTy *Ty) const override;
  835. Init *getOperator() const { return Val; }
  836. const std::string &getName() const { return ValName; }
  837. unsigned getNumArgs() const { return Args.size(); }
  838. Init *getArg(unsigned Num) const {
  839. assert(Num < Args.size() && "Arg number out of range!");
  840. return Args[Num];
  841. }
  842. const std::string &getArgName(unsigned Num) const {
  843. assert(Num < ArgNames.size() && "Arg number out of range!");
  844. return ArgNames[Num];
  845. }
  846. Init *resolveReferences(Record &R, const RecordVal *RV) const override;
  847. std::string getAsString() const override;
  848. typedef std::vector<Init*>::const_iterator const_arg_iterator;
  849. typedef std::vector<std::string>::const_iterator const_name_iterator;
  850. inline const_arg_iterator arg_begin() const { return Args.begin(); }
  851. inline const_arg_iterator arg_end () const { return Args.end(); }
  852. inline size_t arg_size () const { return Args.size(); }
  853. inline bool arg_empty() const { return Args.empty(); }
  854. inline const_name_iterator name_begin() const { return ArgNames.begin(); }
  855. inline const_name_iterator name_end () const { return ArgNames.end(); }
  856. inline size_t name_size () const { return ArgNames.size(); }
  857. inline bool name_empty() const { return ArgNames.empty(); }
  858. Init *getBit(unsigned Bit) const override {
  859. llvm_unreachable("Illegal bit reference off dag");
  860. }
  861. Init *resolveListElementReference(Record &R, const RecordVal *RV,
  862. unsigned Elt) const override {
  863. llvm_unreachable("Illegal element reference off dag");
  864. }
  865. };
  866. //===----------------------------------------------------------------------===//
  867. // High-Level Classes
  868. // //
  869. ///////////////////////////////////////////////////////////////////////////////
  870. class RecordVal {
  871. PointerIntPair<Init *, 1, bool> NameAndPrefix;
  872. RecTy *Ty;
  873. Init *Value;
  874. public:
  875. RecordVal(Init *N, RecTy *T, bool P);
  876. RecordVal(const std::string &N, RecTy *T, bool P);
  877. const std::string &getName() const;
  878. const Init *getNameInit() const { return NameAndPrefix.getPointer(); }
  879. std::string getNameInitAsString() const {
  880. return getNameInit()->getAsUnquotedString();
  881. }
  882. bool getPrefix() const { return NameAndPrefix.getInt(); }
  883. RecTy *getType() const { return Ty; }
  884. Init *getValue() const { return Value; }
  885. bool setValue(Init *V) {
  886. if (V) {
  887. Value = V->convertInitializerTo(Ty);
  888. return Value == nullptr;
  889. }
  890. Value = nullptr;
  891. return false;
  892. }
  893. void dump() const;
  894. void print(raw_ostream &OS, bool PrintSem = true) const;
  895. };
  896. inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
  897. RV.print(OS << " ");
  898. return OS;
  899. }
  900. class Record {
  901. static unsigned LastID;
  902. // Unique record ID.
  903. unsigned ID;
  904. Init *Name;
  905. // Location where record was instantiated, followed by the location of
  906. // multiclass prototypes used.
  907. SmallVector<SMLoc, 4> Locs;
  908. std::vector<Init *> TemplateArgs;
  909. std::vector<RecordVal> Values;
  910. std::vector<Record *> SuperClasses;
  911. std::vector<SMRange> SuperClassRanges;
  912. // Tracks Record instances. Not owned by Record.
  913. RecordKeeper &TrackedRecords;
  914. std::unique_ptr<DefInit> TheInit;
  915. bool IsAnonymous;
  916. // Class-instance values can be used by other defs. For example, Struct<i>
  917. // is used here as a template argument to another class:
  918. //
  919. // multiclass MultiClass<int i> {
  920. // def Def : Class<Struct<i>>;
  921. //
  922. // These need to get fully resolved before instantiating any other
  923. // definitions that use them (e.g. Def). However, inside a multiclass they
  924. // can't be immediately resolved so we mark them ResolveFirst to fully
  925. // resolve them later as soon as the multiclass is instantiated.
  926. bool ResolveFirst;
  927. void init();
  928. void checkName();
  929. public:
  930. // Constructs a record.
  931. explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
  932. bool Anonymous = false) :
  933. ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
  934. TrackedRecords(records), IsAnonymous(Anonymous), ResolveFirst(false) {
  935. init();
  936. }
  937. explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
  938. RecordKeeper &records, bool Anonymous = false)
  939. : Record(StringInit::get(N), locs, records, Anonymous) {}
  940. // When copy-constructing a Record, we must still guarantee a globally unique
  941. // ID number. Don't copy TheInit either since it's owned by the original
  942. // record. All other fields can be copied normally.
  943. Record(const Record &O) :
  944. ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
  945. Values(O.Values), SuperClasses(O.SuperClasses),
  946. SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
  947. IsAnonymous(O.IsAnonymous),
  948. ResolveFirst(O.ResolveFirst) { }
  949. static unsigned getNewUID() { return LastID++; }
  950. unsigned getID() const { return ID; }
  951. const std::string &getName() const;
  952. Init *getNameInit() const {
  953. return Name;
  954. }
  955. const std::string getNameInitAsString() const {
  956. return getNameInit()->getAsUnquotedString();
  957. }
  958. void setName(Init *Name); // Also updates RecordKeeper.
  959. void setName(const std::string &Name); // Also updates RecordKeeper.
  960. ArrayRef<SMLoc> getLoc() const { return Locs; }
  961. /// get the corresponding DefInit.
  962. DefInit *getDefInit();
  963. ArrayRef<Init *> getTemplateArgs() const {
  964. return TemplateArgs;
  965. }
  966. ArrayRef<RecordVal> getValues() const { return Values; }
  967. ArrayRef<Record *> getSuperClasses() const { return SuperClasses; }
  968. ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
  969. bool isTemplateArg(Init *Name) const {
  970. for (Init *TA : TemplateArgs)
  971. if (TA == Name) return true;
  972. return false;
  973. }
  974. bool isTemplateArg(StringRef Name) const {
  975. return isTemplateArg(StringInit::get(Name));
  976. }
  977. const RecordVal *getValue(const Init *Name) const {
  978. for (const RecordVal &Val : Values)
  979. if (Val.getNameInit() == Name) return &Val;
  980. return nullptr;
  981. }
  982. const RecordVal *getValue(StringRef Name) const {
  983. return getValue(StringInit::get(Name));
  984. }
  985. RecordVal *getValue(const Init *Name) {
  986. for (RecordVal &Val : Values)
  987. if (Val.getNameInit() == Name) return &Val;
  988. return nullptr;
  989. }
  990. RecordVal *getValue(StringRef Name) {
  991. return getValue(StringInit::get(Name));
  992. }
  993. void addTemplateArg(Init *Name) {
  994. assert(!isTemplateArg(Name) && "Template arg already defined!");
  995. TemplateArgs.push_back(Name);
  996. }
  997. void addTemplateArg(StringRef Name) {
  998. addTemplateArg(StringInit::get(Name));
  999. }
  1000. void addValue(const RecordVal &RV) {
  1001. assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
  1002. Values.push_back(RV);
  1003. if (Values.size() > 1)
  1004. // Keep NAME at the end of the list. It makes record dumps a
  1005. // bit prettier and allows TableGen tests to be written more
  1006. // naturally. Tests can use CHECK-NEXT to look for Record
  1007. // fields they expect to see after a def. They can't do that if
  1008. // NAME is the first Record field.
  1009. std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
  1010. }
  1011. void removeValue(Init *Name) {
  1012. for (unsigned i = 0, e = Values.size(); i != e; ++i)
  1013. if (Values[i].getNameInit() == Name) {
  1014. Values.erase(Values.begin()+i);
  1015. return;
  1016. }
  1017. llvm_unreachable("Cannot remove an entry that does not exist!");
  1018. }
  1019. void removeValue(StringRef Name) {
  1020. removeValue(StringInit::get(Name));
  1021. }
  1022. bool isSubClassOf(const Record *R) const {
  1023. for (const Record *SC : SuperClasses)
  1024. if (SC == R)
  1025. return true;
  1026. return false;
  1027. }
  1028. bool isSubClassOf(StringRef Name) const {
  1029. for (const Record *SC : SuperClasses)
  1030. if (SC->getNameInitAsString() == Name)
  1031. return true;
  1032. return false;
  1033. }
  1034. void addSuperClass(Record *R, SMRange Range) {
  1035. assert(!isSubClassOf(R) && "Already subclassing record!");
  1036. SuperClasses.push_back(R);
  1037. SuperClassRanges.push_back(Range);
  1038. }
  1039. /// resolveReferences - If there are any field references that refer to fields
  1040. /// that have been filled in, we can propagate the values now.
  1041. ///
  1042. void resolveReferences() { resolveReferencesTo(nullptr); }
  1043. /// resolveReferencesTo - If anything in this record refers to RV, replace the
  1044. /// reference to RV with the RHS of RV. If RV is null, we resolve all
  1045. /// possible references.
  1046. void resolveReferencesTo(const RecordVal *RV);
  1047. RecordKeeper &getRecords() const {
  1048. return TrackedRecords;
  1049. }
  1050. bool isAnonymous() const {
  1051. return IsAnonymous;
  1052. }
  1053. bool isResolveFirst() const {
  1054. return ResolveFirst;
  1055. }
  1056. void setResolveFirst(bool b) {
  1057. ResolveFirst = b;
  1058. }
  1059. void dump() const;
  1060. //===--------------------------------------------------------------------===//
  1061. // High-level methods useful to tablegen back-ends
  1062. //
  1063. /// getValueInit - Return the initializer for a value with the specified name,
  1064. /// or throw an exception if the field does not exist.
  1065. ///
  1066. Init *getValueInit(StringRef FieldName) const;
  1067. /// Return true if the named field is unset.
  1068. bool isValueUnset(StringRef FieldName) const {
  1069. return isa<UnsetInit>(getValueInit(FieldName));
  1070. }
  1071. /// getValueAsString - This method looks up the specified field and returns
  1072. /// its value as a string, throwing an exception if the field does not exist
  1073. /// or if the value is not a string.
  1074. ///
  1075. std::string getValueAsString(StringRef FieldName) const;
  1076. /// getValueAsBitsInit - This method looks up the specified field and returns
  1077. /// its value as a BitsInit, throwing an exception if the field does not exist
  1078. /// or if the value is not the right type.
  1079. ///
  1080. BitsInit *getValueAsBitsInit(StringRef FieldName) const;
  1081. /// getValueAsListInit - This method looks up the specified field and returns
  1082. /// its value as a ListInit, throwing an exception if the field does not exist
  1083. /// or if the value is not the right type.
  1084. ///
  1085. ListInit *getValueAsListInit(StringRef FieldName) const;
  1086. /// getValueAsListOfDefs - This method looks up the specified field and
  1087. /// returns its value as a vector of records, throwing an exception if the
  1088. /// field does not exist or if the value is not the right type.
  1089. ///
  1090. std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
  1091. /// getValueAsListOfInts - This method looks up the specified field and
  1092. /// returns its value as a vector of integers, throwing an exception if the
  1093. /// field does not exist or if the value is not the right type.
  1094. ///
  1095. std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
  1096. /// getValueAsListOfStrings - This method looks up the specified field and
  1097. /// returns its value as a vector of strings, throwing an exception if the
  1098. /// field does not exist or if the value is not the right type.
  1099. ///
  1100. std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
  1101. /// getValueAsDef - This method looks up the specified field and returns its
  1102. /// value as a Record, throwing an exception if the field does not exist or if
  1103. /// the value is not the right type.
  1104. ///
  1105. Record *getValueAsDef(StringRef FieldName) const;
  1106. /// getValueAsBit - This method looks up the specified field and returns its
  1107. /// value as a bit, throwing an exception if the field does not exist or if
  1108. /// the value is not the right type.
  1109. ///
  1110. bool getValueAsBit(StringRef FieldName) const;
  1111. /// getValueAsBitOrUnset - This method looks up the specified field and
  1112. /// returns its value as a bit. If the field is unset, sets Unset to true and
  1113. /// returns false.
  1114. ///
  1115. bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
  1116. /// getValueAsInt - This method looks up the specified field and returns its
  1117. /// value as an int64_t, throwing an exception if the field does not exist or
  1118. /// if the value is not the right type.
  1119. ///
  1120. int64_t getValueAsInt(StringRef FieldName) const;
  1121. /// getValueAsDag - This method looks up the specified field and returns its
  1122. /// value as an Dag, throwing an exception if the field does not exist or if
  1123. /// the value is not the right type.
  1124. ///
  1125. DagInit *getValueAsDag(StringRef FieldName) const;
  1126. };
  1127. raw_ostream &operator<<(raw_ostream &OS, const Record &R);
  1128. struct MultiClass {
  1129. Record Rec; // Placeholder for template args and Name.
  1130. typedef std::vector<std::unique_ptr<Record>> RecordVector;
  1131. RecordVector DefPrototypes;
  1132. void dump() const;
  1133. MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
  1134. Rec(Name, Loc, Records) {}
  1135. };
  1136. class RecordKeeper {
  1137. typedef std::map<std::string, std::unique_ptr<Record>> RecordMap;
  1138. RecordMap Classes, Defs;
  1139. public:
  1140. const RecordMap &getClasses() const { return Classes; }
  1141. const RecordMap &getDefs() const { return Defs; }
  1142. Record *getClass(const std::string &Name) const {
  1143. auto I = Classes.find(Name);
  1144. return I == Classes.end() ? nullptr : I->second.get();
  1145. }
  1146. Record *getDef(const std::string &Name) const {
  1147. auto I = Defs.find(Name);
  1148. return I == Defs.end() ? nullptr : I->second.get();
  1149. }
  1150. void addClass(std::unique_ptr<Record> R) {
  1151. bool Ins = Classes.insert(std::make_pair(R->getName(),
  1152. std::move(R))).second;
  1153. (void)Ins;
  1154. assert(Ins && "Class already exists");
  1155. }
  1156. void addDef(std::unique_ptr<Record> R) {
  1157. bool Ins = Defs.insert(std::make_pair(R->getName(),
  1158. std::move(R))).second;
  1159. (void)Ins;
  1160. assert(Ins && "Record already exists");
  1161. }
  1162. //===--------------------------------------------------------------------===//
  1163. // High-level helper methods, useful for tablegen backends...
  1164. /// getAllDerivedDefinitions - This method returns all concrete definitions
  1165. /// that derive from the specified class name. If a class with the specified
  1166. /// name does not exist, an exception is thrown.
  1167. std::vector<Record*>
  1168. getAllDerivedDefinitions(const std::string &ClassName) const;
  1169. void dump() const;
  1170. };
  1171. /// LessRecord - Sorting predicate to sort record pointers by name.
  1172. ///
  1173. struct LessRecord {
  1174. bool operator()(const Record *Rec1, const Record *Rec2) const {
  1175. return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
  1176. }
  1177. };
  1178. /// LessRecordByID - Sorting predicate to sort record pointers by their
  1179. /// unique ID. If you just need a deterministic order, use this, since it
  1180. /// just compares two `unsigned`; the other sorting predicates require
  1181. /// string manipulation.
  1182. struct LessRecordByID {
  1183. bool operator()(const Record *LHS, const Record *RHS) const {
  1184. return LHS->getID() < RHS->getID();
  1185. }
  1186. };
  1187. /// LessRecordFieldName - Sorting predicate to sort record pointers by their
  1188. /// name field.
  1189. ///
  1190. struct LessRecordFieldName {
  1191. bool operator()(const Record *Rec1, const Record *Rec2) const {
  1192. return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
  1193. }
  1194. };
  1195. struct LessRecordRegister {
  1196. static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
  1197. struct RecordParts {
  1198. SmallVector<std::pair< bool, StringRef>, 4> Parts;
  1199. RecordParts(StringRef Rec) {
  1200. if (Rec.empty())
  1201. return;
  1202. size_t Len = 0;
  1203. const char *Start = Rec.data();
  1204. const char *Curr = Start;
  1205. bool isDigitPart = ascii_isdigit(Curr[0]);
  1206. for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
  1207. bool isDigit = ascii_isdigit(Curr[I]);
  1208. if (isDigit != isDigitPart) {
  1209. Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
  1210. Len = 0;
  1211. Start = &Curr[I];
  1212. isDigitPart = ascii_isdigit(Curr[I]);
  1213. }
  1214. }
  1215. // Push the last part.
  1216. Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
  1217. }
  1218. size_t size() { return Parts.size(); }
  1219. std::pair<bool, StringRef> getPart(size_t i) {
  1220. assert (i < Parts.size() && "Invalid idx!");
  1221. return Parts[i];
  1222. }
  1223. };
  1224. bool operator()(const Record *Rec1, const Record *Rec2) const {
  1225. RecordParts LHSParts(StringRef(Rec1->getName()));
  1226. RecordParts RHSParts(StringRef(Rec2->getName()));
  1227. size_t LHSNumParts = LHSParts.size();
  1228. size_t RHSNumParts = RHSParts.size();
  1229. assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
  1230. if (LHSNumParts != RHSNumParts)
  1231. return LHSNumParts < RHSNumParts;
  1232. // We expect the registers to be of the form [_a-zA-z]+([0-9]*[_a-zA-Z]*)*.
  1233. for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
  1234. std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
  1235. std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
  1236. // Expect even part to always be alpha.
  1237. assert (LHSPart.first == false && RHSPart.first == false &&
  1238. "Expected both parts to be alpha.");
  1239. if (int Res = LHSPart.second.compare(RHSPart.second))
  1240. return Res < 0;
  1241. }
  1242. for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
  1243. std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
  1244. std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
  1245. // Expect odd part to always be numeric.
  1246. assert (LHSPart.first == true && RHSPart.first == true &&
  1247. "Expected both parts to be numeric.");
  1248. if (LHSPart.second.size() != RHSPart.second.size())
  1249. return LHSPart.second.size() < RHSPart.second.size();
  1250. unsigned LHSVal, RHSVal;
  1251. bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
  1252. assert(!LHSFailed && "Unable to convert LHS to integer.");
  1253. bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
  1254. assert(!RHSFailed && "Unable to convert RHS to integer.");
  1255. if (LHSVal != RHSVal)
  1256. return LHSVal < RHSVal;
  1257. }
  1258. return LHSNumParts < RHSNumParts;
  1259. }
  1260. };
  1261. raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
  1262. /// QualifyName - Return an Init with a qualifier prefix referring
  1263. /// to CurRec's name.
  1264. Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
  1265. Init *Name, const std::string &Scoper);
  1266. /// QualifyName - Return an Init with a qualifier prefix referring
  1267. /// to CurRec's name.
  1268. Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
  1269. const std::string &Name, const std::string &Scoper);
  1270. } // End llvm namespace
  1271. #endif