| 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 | |
| 13 | #include "llvm/TableGen/Record.h" |
| 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | #include "llvm/ADT/FoldingSet.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/Config/llvm-config.h" |
| 23 | #include "llvm/Support/Allocator.h" |
| 24 | #include "llvm/Support/Casting.h" |
| 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include "llvm/Support/MathExtras.h" |
| 28 | #include "llvm/Support/Regex.h" |
| 29 | #include "llvm/Support/SMLoc.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | #include "llvm/TableGen/Error.h" |
| 32 | #include "llvm/TableGen/TGTimer.h" |
| 33 | #include <cassert> |
| 34 | #include <cstdint> |
| 35 | #include <map> |
| 36 | #include <memory> |
| 37 | #include <string> |
| 38 | #include <utility> |
| 39 | #include <vector> |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | #define DEBUG_TYPE "tblgen-records" |
| 44 | |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | // Context |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
| 49 | /// This class represents the internal implementation of the RecordKeeper. |
| 50 | /// It contains all of the contextual static state of the Record classes. It is |
| 51 | /// kept out-of-line to simplify dependencies, and also make it easier for |
| 52 | /// internal classes to access the uniquer state of the keeper. |
| 53 | struct detail::RecordKeeperImpl { |
| 54 | RecordKeeperImpl(RecordKeeper &RK) |
| 55 | : SharedBitRecTy(RK), SharedIntRecTy(RK), SharedStringRecTy(RK), |
| 56 | SharedDagRecTy(RK), AnyRecord(RK, {}), TheUnsetInit(RK), |
| 57 | TrueBitInit(true, &SharedBitRecTy), |
| 58 | FalseBitInit(false, &SharedBitRecTy), StringInitStringPool(Allocator), |
| 59 | StringInitCodePool(Allocator), AnonCounter(0), LastRecordID(0) {} |
| 60 | |
| 61 | BumpPtrAllocator Allocator; |
| 62 | std::vector<BitsRecTy *> SharedBitsRecTys; |
| 63 | BitRecTy SharedBitRecTy; |
| 64 | IntRecTy SharedIntRecTy; |
| 65 | StringRecTy SharedStringRecTy; |
| 66 | DagRecTy SharedDagRecTy; |
| 67 | |
| 68 | RecordRecTy AnyRecord; |
| 69 | UnsetInit TheUnsetInit; |
| 70 | BitInit TrueBitInit; |
| 71 | BitInit FalseBitInit; |
| 72 | |
| 73 | FoldingSet<ArgumentInit> TheArgumentInitPool; |
| 74 | FoldingSet<BitsInit> TheBitsInitPool; |
| 75 | std::map<int64_t, IntInit *> TheIntInitPool; |
| 76 | StringMap<const StringInit *, BumpPtrAllocator &> StringInitStringPool; |
| 77 | StringMap<const StringInit *, BumpPtrAllocator &> StringInitCodePool; |
| 78 | FoldingSet<ListInit> TheListInitPool; |
| 79 | FoldingSet<UnOpInit> TheUnOpInitPool; |
| 80 | FoldingSet<BinOpInit> TheBinOpInitPool; |
| 81 | FoldingSet<TernOpInit> TheTernOpInitPool; |
| 82 | FoldingSet<FoldOpInit> TheFoldOpInitPool; |
| 83 | FoldingSet<IsAOpInit> TheIsAOpInitPool; |
| 84 | FoldingSet<ExistsOpInit> TheExistsOpInitPool; |
| 85 | FoldingSet<InstancesOpInit> TheInstancesOpInitPool; |
| 86 | DenseMap<std::pair<const RecTy *, const Init *>, VarInit *> TheVarInitPool; |
| 87 | DenseMap<std::pair<const TypedInit *, unsigned>, VarBitInit *> |
| 88 | TheVarBitInitPool; |
| 89 | FoldingSet<VarDefInit> TheVarDefInitPool; |
| 90 | DenseMap<std::pair<const Init *, const StringInit *>, FieldInit *> |
| 91 | TheFieldInitPool; |
| 92 | FoldingSet<CondOpInit> TheCondOpInitPool; |
| 93 | FoldingSet<DagInit> TheDagInitPool; |
| 94 | FoldingSet<RecordRecTy> RecordTypePool; |
| 95 | |
| 96 | unsigned AnonCounter; |
| 97 | unsigned LastRecordID; |
| 98 | |
| 99 | void dumpAllocationStats(raw_ostream &OS) const; |
| 100 | }; |
| 101 | |
| 102 | void detail::RecordKeeperImpl::dumpAllocationStats(raw_ostream &OS) const { |
| 103 | // Dump memory allocation related stats. |
| 104 | OS << "TheArgumentInitPool size = " << TheArgumentInitPool.size() << '\n'; |
| 105 | OS << "TheBitsInitPool size = " << TheBitsInitPool.size() << '\n'; |
| 106 | OS << "TheIntInitPool size = " << TheIntInitPool.size() << '\n'; |
| 107 | OS << "StringInitStringPool size = " << StringInitStringPool.size() << '\n'; |
| 108 | OS << "StringInitCodePool size = " << StringInitCodePool.size() << '\n'; |
| 109 | OS << "TheListInitPool size = " << TheListInitPool.size() << '\n'; |
| 110 | OS << "TheUnOpInitPool size = " << TheUnOpInitPool.size() << '\n'; |
| 111 | OS << "TheBinOpInitPool size = " << TheBinOpInitPool.size() << '\n'; |
| 112 | OS << "TheTernOpInitPool size = " << TheTernOpInitPool.size() << '\n'; |
| 113 | OS << "TheFoldOpInitPool size = " << TheFoldOpInitPool.size() << '\n'; |
| 114 | OS << "TheIsAOpInitPool size = " << TheIsAOpInitPool.size() << '\n'; |
| 115 | OS << "TheExistsOpInitPool size = " << TheExistsOpInitPool.size() << '\n'; |
| 116 | OS << "TheCondOpInitPool size = " << TheCondOpInitPool.size() << '\n'; |
| 117 | OS << "TheDagInitPool size = " << TheDagInitPool.size() << '\n'; |
| 118 | OS << "RecordTypePool size = " << RecordTypePool.size() << '\n'; |
| 119 | OS << "TheVarInitPool size = " << TheVarInitPool.size() << '\n'; |
| 120 | OS << "TheVarBitInitPool size = " << TheVarBitInitPool.size() << '\n'; |
| 121 | OS << "TheVarDefInitPool size = " << TheVarDefInitPool.size() << '\n'; |
| 122 | OS << "TheFieldInitPool size = " << TheFieldInitPool.size() << '\n'; |
| 123 | OS << "Bytes allocated = " << Allocator.getBytesAllocated() << '\n'; |
| 124 | OS << "Total allocator memory = " << Allocator.getTotalMemory() << "\n\n" ; |
| 125 | |
| 126 | OS << "Number of records instantiated = " << LastRecordID << '\n'; |
| 127 | OS << "Number of anonymous records = " << AnonCounter << '\n'; |
| 128 | } |
| 129 | |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | // Type implementations |
| 132 | //===----------------------------------------------------------------------===// |
| 133 | |
| 134 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 135 | LLVM_DUMP_METHOD void RecTy::dump() const { print(errs()); } |
| 136 | #endif |
| 137 | |
| 138 | const ListRecTy *RecTy::getListTy() const { |
| 139 | if (!ListTy) |
| 140 | ListTy = new (RK.getImpl().Allocator) ListRecTy(this); |
| 141 | return ListTy; |
| 142 | } |
| 143 | |
| 144 | bool RecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
| 145 | assert(RHS && "NULL pointer" ); |
| 146 | return Kind == RHS->getRecTyKind(); |
| 147 | } |
| 148 | |
| 149 | bool RecTy::typeIsA(const RecTy *RHS) const { return this == RHS; } |
| 150 | |
| 151 | const BitRecTy *BitRecTy::get(RecordKeeper &RK) { |
| 152 | return &RK.getImpl().SharedBitRecTy; |
| 153 | } |
| 154 | |
| 155 | bool BitRecTy::typeIsConvertibleTo(const RecTy *RHS) const{ |
| 156 | if (RecTy::typeIsConvertibleTo(RHS) || RHS->getRecTyKind() == IntRecTyKind) |
| 157 | return true; |
| 158 | if (const auto *BitsTy = dyn_cast<BitsRecTy>(Val: RHS)) |
| 159 | return BitsTy->getNumBits() == 1; |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | const BitsRecTy *BitsRecTy::get(RecordKeeper &RK, unsigned Sz) { |
| 164 | detail::RecordKeeperImpl &RKImpl = RK.getImpl(); |
| 165 | if (Sz >= RKImpl.SharedBitsRecTys.size()) |
| 166 | RKImpl.SharedBitsRecTys.resize(new_size: Sz + 1); |
| 167 | BitsRecTy *&Ty = RKImpl.SharedBitsRecTys[Sz]; |
| 168 | if (!Ty) |
| 169 | Ty = new (RKImpl.Allocator) BitsRecTy(RK, Sz); |
| 170 | return Ty; |
| 171 | } |
| 172 | |
| 173 | std::string BitsRecTy::getAsString() const { |
| 174 | return "bits<" + utostr(X: Size) + ">" ; |
| 175 | } |
| 176 | |
| 177 | bool BitsRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
| 178 | if (RecTy::typeIsConvertibleTo(RHS)) //argument and the sender are same type |
| 179 | return cast<BitsRecTy>(Val: RHS)->Size == Size; |
| 180 | RecTyKind kind = RHS->getRecTyKind(); |
| 181 | return (kind == BitRecTyKind && Size == 1) || (kind == IntRecTyKind); |
| 182 | } |
| 183 | |
| 184 | const IntRecTy *IntRecTy::get(RecordKeeper &RK) { |
| 185 | return &RK.getImpl().SharedIntRecTy; |
| 186 | } |
| 187 | |
| 188 | bool IntRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
| 189 | RecTyKind kind = RHS->getRecTyKind(); |
| 190 | return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind; |
| 191 | } |
| 192 | |
| 193 | const StringRecTy *StringRecTy::get(RecordKeeper &RK) { |
| 194 | return &RK.getImpl().SharedStringRecTy; |
| 195 | } |
| 196 | |
| 197 | std::string StringRecTy::getAsString() const { |
| 198 | return "string" ; |
| 199 | } |
| 200 | |
| 201 | bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
| 202 | RecTyKind Kind = RHS->getRecTyKind(); |
| 203 | return Kind == StringRecTyKind; |
| 204 | } |
| 205 | |
| 206 | std::string ListRecTy::getAsString() const { |
| 207 | return "list<" + ElementTy->getAsString() + ">" ; |
| 208 | } |
| 209 | |
| 210 | bool ListRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
| 211 | if (const auto *ListTy = dyn_cast<ListRecTy>(Val: RHS)) |
| 212 | return ElementTy->typeIsConvertibleTo(RHS: ListTy->getElementType()); |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | bool ListRecTy::typeIsA(const RecTy *RHS) const { |
| 217 | if (const auto *RHSl = dyn_cast<ListRecTy>(Val: RHS)) |
| 218 | return getElementType()->typeIsA(RHS: RHSl->getElementType()); |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | const DagRecTy *DagRecTy::get(RecordKeeper &RK) { |
| 223 | return &RK.getImpl().SharedDagRecTy; |
| 224 | } |
| 225 | |
| 226 | std::string DagRecTy::getAsString() const { |
| 227 | return "dag" ; |
| 228 | } |
| 229 | |
| 230 | static void ProfileRecordRecTy(FoldingSetNodeID &ID, |
| 231 | ArrayRef<const Record *> Classes) { |
| 232 | ID.AddInteger(I: Classes.size()); |
| 233 | for (const Record *R : Classes) |
| 234 | ID.AddPointer(Ptr: R); |
| 235 | } |
| 236 | |
| 237 | RecordRecTy::RecordRecTy(RecordKeeper &RK, ArrayRef<const Record *> Classes) |
| 238 | : RecTy(RecordRecTyKind, RK), NumClasses(Classes.size()) { |
| 239 | llvm::uninitialized_copy(Src&: Classes, Dst: getTrailingObjects()); |
| 240 | } |
| 241 | |
| 242 | const RecordRecTy *RecordRecTy::get(RecordKeeper &RK, |
| 243 | ArrayRef<const Record *> UnsortedClasses) { |
| 244 | detail::RecordKeeperImpl &RKImpl = RK.getImpl(); |
| 245 | if (UnsortedClasses.empty()) |
| 246 | return &RKImpl.AnyRecord; |
| 247 | |
| 248 | FoldingSet<RecordRecTy> &ThePool = RKImpl.RecordTypePool; |
| 249 | |
| 250 | SmallVector<const Record *, 4> Classes(UnsortedClasses); |
| 251 | llvm::sort(C&: Classes, Comp: [](const Record *LHS, const Record *RHS) { |
| 252 | return LHS->getNameInitAsString() < RHS->getNameInitAsString(); |
| 253 | }); |
| 254 | |
| 255 | FoldingSetNodeID ID; |
| 256 | ProfileRecordRecTy(ID, Classes); |
| 257 | |
| 258 | void *IP = nullptr; |
| 259 | if (RecordRecTy *Ty = ThePool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 260 | return Ty; |
| 261 | |
| 262 | #ifndef NDEBUG |
| 263 | // Check for redundancy. |
| 264 | for (unsigned i = 0; i < Classes.size(); ++i) { |
| 265 | for (unsigned j = 0; j < Classes.size(); ++j) { |
| 266 | assert(i == j || !Classes[i]->isSubClassOf(Classes[j])); |
| 267 | } |
| 268 | assert(&Classes[0]->getRecords() == &Classes[i]->getRecords()); |
| 269 | } |
| 270 | #endif |
| 271 | |
| 272 | void *Mem = RKImpl.Allocator.Allocate( |
| 273 | Size: totalSizeToAlloc<const Record *>(Counts: Classes.size()), Alignment: alignof(RecordRecTy)); |
| 274 | RecordRecTy *Ty = new (Mem) RecordRecTy(RK, Classes); |
| 275 | ThePool.InsertNode(N: Ty, InsertPos: IP); |
| 276 | return Ty; |
| 277 | } |
| 278 | |
| 279 | const RecordRecTy *RecordRecTy::get(const Record *Class) { |
| 280 | assert(Class && "unexpected null class" ); |
| 281 | return get(RK&: Class->getRecords(), UnsortedClasses: {Class}); |
| 282 | } |
| 283 | |
| 284 | void RecordRecTy::Profile(FoldingSetNodeID &ID) const { |
| 285 | ProfileRecordRecTy(ID, Classes: getClasses()); |
| 286 | } |
| 287 | |
| 288 | std::string RecordRecTy::getAsString() const { |
| 289 | if (NumClasses == 1) |
| 290 | return getClasses()[0]->getNameInitAsString(); |
| 291 | |
| 292 | std::string Str = "{" ; |
| 293 | ListSeparator LS; |
| 294 | for (const Record *R : getClasses()) { |
| 295 | Str += LS; |
| 296 | Str += R->getNameInitAsString(); |
| 297 | } |
| 298 | Str += "}" ; |
| 299 | return Str; |
| 300 | } |
| 301 | |
| 302 | bool RecordRecTy::isSubClassOf(const Record *Class) const { |
| 303 | return llvm::any_of(Range: getClasses(), P: [Class](const Record *MySuperClass) { |
| 304 | return MySuperClass == Class || MySuperClass->isSubClassOf(R: Class); |
| 305 | }); |
| 306 | } |
| 307 | |
| 308 | bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const { |
| 309 | if (this == RHS) |
| 310 | return true; |
| 311 | |
| 312 | const auto *RTy = dyn_cast<RecordRecTy>(Val: RHS); |
| 313 | if (!RTy) |
| 314 | return false; |
| 315 | |
| 316 | return llvm::all_of(Range: RTy->getClasses(), P: [this](const Record *TargetClass) { |
| 317 | return isSubClassOf(Class: TargetClass); |
| 318 | }); |
| 319 | } |
| 320 | |
| 321 | bool RecordRecTy::typeIsA(const RecTy *RHS) const { |
| 322 | return typeIsConvertibleTo(RHS); |
| 323 | } |
| 324 | |
| 325 | static const RecordRecTy *resolveRecordTypes(const RecordRecTy *T1, |
| 326 | const RecordRecTy *T2) { |
| 327 | SmallVector<const Record *, 4> CommonSuperClasses; |
| 328 | SmallVector<const Record *, 4> Stack(T1->getClasses()); |
| 329 | |
| 330 | while (!Stack.empty()) { |
| 331 | const Record *R = Stack.pop_back_val(); |
| 332 | |
| 333 | if (T2->isSubClassOf(Class: R)) |
| 334 | CommonSuperClasses.push_back(Elt: R); |
| 335 | else |
| 336 | llvm::append_range(C&: Stack, R: make_first_range(c: R->getDirectSuperClasses())); |
| 337 | } |
| 338 | |
| 339 | return RecordRecTy::get(RK&: T1->getRecordKeeper(), UnsortedClasses: CommonSuperClasses); |
| 340 | } |
| 341 | |
| 342 | const RecTy *llvm::resolveTypes(const RecTy *T1, const RecTy *T2) { |
| 343 | if (T1 == T2) |
| 344 | return T1; |
| 345 | |
| 346 | if (const auto *RecTy1 = dyn_cast<RecordRecTy>(Val: T1)) { |
| 347 | if (const auto *RecTy2 = dyn_cast<RecordRecTy>(Val: T2)) |
| 348 | return resolveRecordTypes(T1: RecTy1, T2: RecTy2); |
| 349 | } |
| 350 | |
| 351 | assert(T1 != nullptr && "Invalid record type" ); |
| 352 | if (T1->typeIsConvertibleTo(RHS: T2)) |
| 353 | return T2; |
| 354 | |
| 355 | assert(T2 != nullptr && "Invalid record type" ); |
| 356 | if (T2->typeIsConvertibleTo(RHS: T1)) |
| 357 | return T1; |
| 358 | |
| 359 | if (const auto *ListTy1 = dyn_cast<ListRecTy>(Val: T1)) { |
| 360 | if (const auto *ListTy2 = dyn_cast<ListRecTy>(Val: T2)) { |
| 361 | const RecTy *NewType = |
| 362 | resolveTypes(T1: ListTy1->getElementType(), T2: ListTy2->getElementType()); |
| 363 | if (NewType) |
| 364 | return NewType->getListTy(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return nullptr; |
| 369 | } |
| 370 | |
| 371 | //===----------------------------------------------------------------------===// |
| 372 | // Initializer implementations |
| 373 | //===----------------------------------------------------------------------===// |
| 374 | |
| 375 | void Init::anchor() {} |
| 376 | |
| 377 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 378 | LLVM_DUMP_METHOD void Init::dump() const { return print(errs()); } |
| 379 | #endif |
| 380 | |
| 381 | RecordKeeper &Init::getRecordKeeper() const { |
| 382 | if (auto *TyInit = dyn_cast<TypedInit>(Val: this)) |
| 383 | return TyInit->getType()->getRecordKeeper(); |
| 384 | if (auto *ArgInit = dyn_cast<ArgumentInit>(Val: this)) |
| 385 | return ArgInit->getRecordKeeper(); |
| 386 | return cast<UnsetInit>(Val: this)->getRecordKeeper(); |
| 387 | } |
| 388 | |
| 389 | UnsetInit *UnsetInit::get(RecordKeeper &RK) { |
| 390 | return &RK.getImpl().TheUnsetInit; |
| 391 | } |
| 392 | |
| 393 | const Init *UnsetInit::getCastTo(const RecTy *Ty) const { return this; } |
| 394 | |
| 395 | const Init *UnsetInit::convertInitializerTo(const RecTy *Ty) const { |
| 396 | return this; |
| 397 | } |
| 398 | |
| 399 | static void ProfileArgumentInit(FoldingSetNodeID &ID, const Init *Value, |
| 400 | ArgAuxType Aux) { |
| 401 | auto I = Aux.index(); |
| 402 | ID.AddInteger(I); |
| 403 | if (I == ArgumentInit::Positional) |
| 404 | ID.AddInteger(I: std::get<ArgumentInit::Positional>(v&: Aux)); |
| 405 | if (I == ArgumentInit::Named) |
| 406 | ID.AddPointer(Ptr: std::get<ArgumentInit::Named>(v&: Aux)); |
| 407 | ID.AddPointer(Ptr: Value); |
| 408 | } |
| 409 | |
| 410 | void ArgumentInit::Profile(FoldingSetNodeID &ID) const { |
| 411 | ProfileArgumentInit(ID, Value, Aux); |
| 412 | } |
| 413 | |
| 414 | const ArgumentInit *ArgumentInit::get(const Init *Value, ArgAuxType Aux) { |
| 415 | FoldingSetNodeID ID; |
| 416 | ProfileArgumentInit(ID, Value, Aux); |
| 417 | |
| 418 | RecordKeeper &RK = Value->getRecordKeeper(); |
| 419 | detail::RecordKeeperImpl &RKImpl = RK.getImpl(); |
| 420 | void *IP = nullptr; |
| 421 | if (const ArgumentInit *I = |
| 422 | RKImpl.TheArgumentInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 423 | return I; |
| 424 | |
| 425 | ArgumentInit *I = new (RKImpl.Allocator) ArgumentInit(Value, Aux); |
| 426 | RKImpl.TheArgumentInitPool.InsertNode(N: I, InsertPos: IP); |
| 427 | return I; |
| 428 | } |
| 429 | |
| 430 | const Init *ArgumentInit::resolveReferences(Resolver &R) const { |
| 431 | const Init *NewValue = Value->resolveReferences(R); |
| 432 | if (NewValue != Value) |
| 433 | return cloneWithValue(Value: NewValue); |
| 434 | |
| 435 | return this; |
| 436 | } |
| 437 | |
| 438 | BitInit *BitInit::get(RecordKeeper &RK, bool V) { |
| 439 | return V ? &RK.getImpl().TrueBitInit : &RK.getImpl().FalseBitInit; |
| 440 | } |
| 441 | |
| 442 | const Init *BitInit::convertInitializerTo(const RecTy *Ty) const { |
| 443 | if (isa<BitRecTy>(Val: Ty)) |
| 444 | return this; |
| 445 | |
| 446 | if (isa<IntRecTy>(Val: Ty)) |
| 447 | return IntInit::get(RK&: getRecordKeeper(), V: getValue()); |
| 448 | |
| 449 | if (auto *BRT = dyn_cast<BitsRecTy>(Val: Ty)) { |
| 450 | // Can only convert single bit. |
| 451 | if (BRT->getNumBits() == 1) |
| 452 | return BitsInit::get(RK&: getRecordKeeper(), Range: this); |
| 453 | } |
| 454 | |
| 455 | return nullptr; |
| 456 | } |
| 457 | |
| 458 | static void ProfileBitsInit(FoldingSetNodeID &ID, |
| 459 | ArrayRef<const Init *> Range) { |
| 460 | ID.AddInteger(I: Range.size()); |
| 461 | |
| 462 | for (const Init *I : Range) |
| 463 | ID.AddPointer(Ptr: I); |
| 464 | } |
| 465 | |
| 466 | BitsInit::BitsInit(RecordKeeper &RK, ArrayRef<const Init *> Bits) |
| 467 | : TypedInit(IK_BitsInit, BitsRecTy::get(RK, Sz: Bits.size())), |
| 468 | NumBits(Bits.size()) { |
| 469 | llvm::uninitialized_copy(Src&: Bits, Dst: getTrailingObjects()); |
| 470 | } |
| 471 | |
| 472 | BitsInit *BitsInit::get(RecordKeeper &RK, ArrayRef<const Init *> Bits) { |
| 473 | FoldingSetNodeID ID; |
| 474 | ProfileBitsInit(ID, Range: Bits); |
| 475 | |
| 476 | detail::RecordKeeperImpl &RKImpl = RK.getImpl(); |
| 477 | void *IP = nullptr; |
| 478 | if (BitsInit *I = RKImpl.TheBitsInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 479 | return I; |
| 480 | |
| 481 | void *Mem = RKImpl.Allocator.Allocate( |
| 482 | Size: totalSizeToAlloc<const Init *>(Counts: Bits.size()), Alignment: alignof(BitsInit)); |
| 483 | BitsInit *I = new (Mem) BitsInit(RK, Bits); |
| 484 | RKImpl.TheBitsInitPool.InsertNode(N: I, InsertPos: IP); |
| 485 | return I; |
| 486 | } |
| 487 | |
| 488 | void BitsInit::Profile(FoldingSetNodeID &ID) const { |
| 489 | ProfileBitsInit(ID, Range: getBits()); |
| 490 | } |
| 491 | |
| 492 | const Init *BitsInit::convertInitializerTo(const RecTy *Ty) const { |
| 493 | if (isa<BitRecTy>(Val: Ty)) { |
| 494 | if (getNumBits() != 1) return nullptr; // Only accept if just one bit! |
| 495 | return getBit(Bit: 0); |
| 496 | } |
| 497 | |
| 498 | if (auto *BRT = dyn_cast<BitsRecTy>(Val: Ty)) { |
| 499 | // If the number of bits is right, return it. Otherwise we need to expand |
| 500 | // or truncate. |
| 501 | if (getNumBits() != BRT->getNumBits()) return nullptr; |
| 502 | return this; |
| 503 | } |
| 504 | |
| 505 | if (isa<IntRecTy>(Val: Ty)) { |
| 506 | std::optional<int64_t> Result = convertInitializerToInt(); |
| 507 | if (Result) |
| 508 | return IntInit::get(RK&: getRecordKeeper(), V: *Result); |
| 509 | } |
| 510 | |
| 511 | return nullptr; |
| 512 | } |
| 513 | |
| 514 | std::optional<int64_t> BitsInit::convertInitializerToInt() const { |
| 515 | int64_t Result = 0; |
| 516 | for (auto [Idx, InitV] : enumerate(First: getBits())) |
| 517 | if (auto *Bit = dyn_cast<BitInit>(Val: InitV)) |
| 518 | Result |= static_cast<int64_t>(Bit->getValue()) << Idx; |
| 519 | else |
| 520 | return std::nullopt; |
| 521 | return Result; |
| 522 | } |
| 523 | |
| 524 | uint64_t BitsInit::convertKnownBitsToInt() const { |
| 525 | uint64_t Result = 0; |
| 526 | for (auto [Idx, InitV] : enumerate(First: getBits())) |
| 527 | if (auto *Bit = dyn_cast<BitInit>(Val: InitV)) |
| 528 | Result |= static_cast<int64_t>(Bit->getValue()) << Idx; |
| 529 | return Result; |
| 530 | } |
| 531 | |
| 532 | const Init * |
| 533 | BitsInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { |
| 534 | SmallVector<const Init *, 16> NewBits(Bits.size()); |
| 535 | |
| 536 | for (auto [Bit, NewBit] : zip_equal(t&: Bits, u&: NewBits)) { |
| 537 | if (Bit >= getNumBits()) |
| 538 | return nullptr; |
| 539 | NewBit = getBit(Bit); |
| 540 | } |
| 541 | return BitsInit::get(RK&: getRecordKeeper(), Bits: NewBits); |
| 542 | } |
| 543 | |
| 544 | bool BitsInit::isComplete() const { |
| 545 | return all_of(Range: getBits(), P: [](const Init *Bit) { return Bit->isComplete(); }); |
| 546 | } |
| 547 | bool BitsInit::allInComplete() const { |
| 548 | return all_of(Range: getBits(), P: [](const Init *Bit) { return !Bit->isComplete(); }); |
| 549 | } |
| 550 | bool BitsInit::isConcrete() const { |
| 551 | return all_of(Range: getBits(), P: [](const Init *Bit) { return Bit->isConcrete(); }); |
| 552 | } |
| 553 | |
| 554 | std::string BitsInit::getAsString() const { |
| 555 | std::string Result = "{ " ; |
| 556 | ListSeparator LS; |
| 557 | for (const Init *Bit : reverse(C: getBits())) { |
| 558 | Result += LS; |
| 559 | if (Bit) |
| 560 | Result += Bit->getAsString(); |
| 561 | else |
| 562 | Result += "*" ; |
| 563 | } |
| 564 | return Result + " }" ; |
| 565 | } |
| 566 | |
| 567 | // resolveReferences - If there are any field references that refer to fields |
| 568 | // that have been filled in, we can propagate the values now. |
| 569 | const Init *BitsInit::resolveReferences(Resolver &R) const { |
| 570 | bool Changed = false; |
| 571 | SmallVector<const Init *, 16> NewBits(getNumBits()); |
| 572 | |
| 573 | const Init *CachedBitVarRef = nullptr; |
| 574 | const Init *CachedBitVarResolved = nullptr; |
| 575 | |
| 576 | for (auto [CurBit, NewBit] : zip_equal(t: getBits(), u&: NewBits)) { |
| 577 | NewBit = CurBit; |
| 578 | |
| 579 | if (const auto *CurBitVar = dyn_cast<VarBitInit>(Val: CurBit)) { |
| 580 | if (CurBitVar->getBitVar() != CachedBitVarRef) { |
| 581 | CachedBitVarRef = CurBitVar->getBitVar(); |
| 582 | CachedBitVarResolved = CachedBitVarRef->resolveReferences(R); |
| 583 | } |
| 584 | assert(CachedBitVarResolved && "Unresolved bitvar reference" ); |
| 585 | NewBit = CachedBitVarResolved->getBit(Bit: CurBitVar->getBitNum()); |
| 586 | } else { |
| 587 | // getBit(0) implicitly converts int and bits<1> values to bit. |
| 588 | NewBit = CurBit->resolveReferences(R)->getBit(Bit: 0); |
| 589 | } |
| 590 | |
| 591 | if (isa<UnsetInit>(Val: NewBit) && R.keepUnsetBits()) |
| 592 | NewBit = CurBit; |
| 593 | Changed |= CurBit != NewBit; |
| 594 | } |
| 595 | |
| 596 | if (Changed) |
| 597 | return BitsInit::get(RK&: getRecordKeeper(), Bits: NewBits); |
| 598 | |
| 599 | return this; |
| 600 | } |
| 601 | |
| 602 | IntInit *IntInit::get(RecordKeeper &RK, int64_t V) { |
| 603 | IntInit *&I = RK.getImpl().TheIntInitPool[V]; |
| 604 | if (!I) |
| 605 | I = new (RK.getImpl().Allocator) IntInit(RK, V); |
| 606 | return I; |
| 607 | } |
| 608 | |
| 609 | std::string IntInit::getAsString() const { |
| 610 | return itostr(X: Value); |
| 611 | } |
| 612 | |
| 613 | static bool canFitInBitfield(int64_t Value, unsigned NumBits) { |
| 614 | // For example, with NumBits == 4, we permit Values from [-7 .. 15]. |
| 615 | return (NumBits >= sizeof(Value) * 8) || |
| 616 | (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1); |
| 617 | } |
| 618 | |
| 619 | const Init *IntInit::convertInitializerTo(const RecTy *Ty) const { |
| 620 | if (isa<IntRecTy>(Val: Ty)) |
| 621 | return this; |
| 622 | |
| 623 | if (isa<BitRecTy>(Val: Ty)) { |
| 624 | int64_t Val = getValue(); |
| 625 | if (Val != 0 && Val != 1) return nullptr; // Only accept 0 or 1 for a bit! |
| 626 | return BitInit::get(RK&: getRecordKeeper(), V: Val != 0); |
| 627 | } |
| 628 | |
| 629 | if (const auto *BRT = dyn_cast<BitsRecTy>(Val: Ty)) { |
| 630 | int64_t Value = getValue(); |
| 631 | // Make sure this bitfield is large enough to hold the integer value. |
| 632 | if (!canFitInBitfield(Value, NumBits: BRT->getNumBits())) |
| 633 | return nullptr; |
| 634 | |
| 635 | SmallVector<const Init *, 16> NewBits(BRT->getNumBits()); |
| 636 | for (unsigned i = 0; i != BRT->getNumBits(); ++i) |
| 637 | NewBits[i] = |
| 638 | BitInit::get(RK&: getRecordKeeper(), V: Value & ((i < 64) ? (1LL << i) : 0)); |
| 639 | |
| 640 | return BitsInit::get(RK&: getRecordKeeper(), Bits: NewBits); |
| 641 | } |
| 642 | |
| 643 | return nullptr; |
| 644 | } |
| 645 | |
| 646 | const Init *IntInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { |
| 647 | SmallVector<const Init *, 16> NewBits(Bits.size()); |
| 648 | |
| 649 | for (auto [Bit, NewBit] : zip_equal(t&: Bits, u&: NewBits)) { |
| 650 | if (Bit >= 64) |
| 651 | return nullptr; |
| 652 | |
| 653 | NewBit = BitInit::get(RK&: getRecordKeeper(), V: Value & (INT64_C(1) << Bit)); |
| 654 | } |
| 655 | return BitsInit::get(RK&: getRecordKeeper(), Bits: NewBits); |
| 656 | } |
| 657 | |
| 658 | AnonymousNameInit *AnonymousNameInit::get(RecordKeeper &RK, unsigned V) { |
| 659 | return new (RK.getImpl().Allocator) AnonymousNameInit(RK, V); |
| 660 | } |
| 661 | |
| 662 | const StringInit *AnonymousNameInit::getNameInit() const { |
| 663 | return StringInit::get(RK&: getRecordKeeper(), getAsString()); |
| 664 | } |
| 665 | |
| 666 | std::string AnonymousNameInit::getAsString() const { |
| 667 | return "anonymous_" + utostr(X: Value); |
| 668 | } |
| 669 | |
| 670 | const Init *AnonymousNameInit::resolveReferences(Resolver &R) const { |
| 671 | auto *Old = this; |
| 672 | auto *New = R.resolve(VarName: Old); |
| 673 | New = New ? New : Old; |
| 674 | if (R.isFinal()) |
| 675 | if (const auto *Anonymous = dyn_cast<AnonymousNameInit>(Val: New)) |
| 676 | return Anonymous->getNameInit(); |
| 677 | return New; |
| 678 | } |
| 679 | |
| 680 | const StringInit *StringInit::get(RecordKeeper &RK, StringRef V, |
| 681 | StringFormat Fmt) { |
| 682 | detail::RecordKeeperImpl &RKImpl = RK.getImpl(); |
| 683 | auto &InitMap = Fmt == SF_String ? RKImpl.StringInitStringPool |
| 684 | : RKImpl.StringInitCodePool; |
| 685 | auto &Entry = *InitMap.try_emplace(Key: V, Args: nullptr).first; |
| 686 | if (!Entry.second) |
| 687 | Entry.second = new (RKImpl.Allocator) StringInit(RK, Entry.getKey(), Fmt); |
| 688 | return Entry.second; |
| 689 | } |
| 690 | |
| 691 | const Init *StringInit::convertInitializerTo(const RecTy *Ty) const { |
| 692 | if (isa<StringRecTy>(Val: Ty)) |
| 693 | return this; |
| 694 | |
| 695 | return nullptr; |
| 696 | } |
| 697 | |
| 698 | static void ProfileListInit(FoldingSetNodeID &ID, |
| 699 | ArrayRef<const Init *> Elements, |
| 700 | const RecTy *EltTy) { |
| 701 | ID.AddInteger(I: Elements.size()); |
| 702 | ID.AddPointer(Ptr: EltTy); |
| 703 | |
| 704 | for (const Init *E : Elements) |
| 705 | ID.AddPointer(Ptr: E); |
| 706 | } |
| 707 | |
| 708 | ListInit::ListInit(ArrayRef<const Init *> Elements, const RecTy *EltTy) |
| 709 | : TypedInit(IK_ListInit, ListRecTy::get(T: EltTy)), |
| 710 | NumElements(Elements.size()) { |
| 711 | llvm::uninitialized_copy(Src&: Elements, Dst: getTrailingObjects()); |
| 712 | } |
| 713 | |
| 714 | const ListInit *ListInit::get(ArrayRef<const Init *> Elements, |
| 715 | const RecTy *EltTy) { |
| 716 | FoldingSetNodeID ID; |
| 717 | ProfileListInit(ID, Elements, EltTy); |
| 718 | |
| 719 | detail::RecordKeeperImpl &RK = EltTy->getRecordKeeper().getImpl(); |
| 720 | void *IP = nullptr; |
| 721 | if (const ListInit *I = RK.TheListInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 722 | return I; |
| 723 | |
| 724 | assert(Elements.empty() || !isa<TypedInit>(Elements[0]) || |
| 725 | cast<TypedInit>(Elements[0])->getType()->typeIsConvertibleTo(EltTy)); |
| 726 | |
| 727 | void *Mem = RK.Allocator.Allocate( |
| 728 | Size: totalSizeToAlloc<const Init *>(Counts: Elements.size()), Alignment: alignof(ListInit)); |
| 729 | ListInit *I = new (Mem) ListInit(Elements, EltTy); |
| 730 | RK.TheListInitPool.InsertNode(N: I, InsertPos: IP); |
| 731 | return I; |
| 732 | } |
| 733 | |
| 734 | void ListInit::Profile(FoldingSetNodeID &ID) const { |
| 735 | const RecTy *EltTy = cast<ListRecTy>(Val: getType())->getElementType(); |
| 736 | ProfileListInit(ID, Elements: getElements(), EltTy); |
| 737 | } |
| 738 | |
| 739 | const Init *ListInit::convertInitializerTo(const RecTy *Ty) const { |
| 740 | if (getType() == Ty) |
| 741 | return this; |
| 742 | |
| 743 | if (const auto *LRT = dyn_cast<ListRecTy>(Val: Ty)) { |
| 744 | SmallVector<const Init *, 8> Elements; |
| 745 | Elements.reserve(N: size()); |
| 746 | |
| 747 | // Verify that all of the elements of the list are subclasses of the |
| 748 | // appropriate class! |
| 749 | bool Changed = false; |
| 750 | const RecTy *ElementType = LRT->getElementType(); |
| 751 | for (const Init *I : getElements()) |
| 752 | if (const Init *CI = I->convertInitializerTo(Ty: ElementType)) { |
| 753 | Elements.push_back(Elt: CI); |
| 754 | if (CI != I) |
| 755 | Changed = true; |
| 756 | } else { |
| 757 | return nullptr; |
| 758 | } |
| 759 | |
| 760 | if (!Changed) |
| 761 | return this; |
| 762 | return ListInit::get(Elements, EltTy: ElementType); |
| 763 | } |
| 764 | |
| 765 | return nullptr; |
| 766 | } |
| 767 | |
| 768 | const Record *ListInit::getElementAsRecord(unsigned Idx) const { |
| 769 | const auto *DI = dyn_cast<DefInit>(Val: getElement(Idx)); |
| 770 | if (!DI) |
| 771 | PrintFatalError(Msg: "expected record type for the element with index " + |
| 772 | Twine(Idx) + " in list " + getAsString()); |
| 773 | return DI->getDef(); |
| 774 | } |
| 775 | |
| 776 | const Init *ListInit::resolveReferences(Resolver &R) const { |
| 777 | SmallVector<const Init *, 8> Resolved; |
| 778 | Resolved.reserve(N: size()); |
| 779 | bool Changed = false; |
| 780 | |
| 781 | for (const Init *CurElt : getElements()) { |
| 782 | const Init *E = CurElt->resolveReferences(R); |
| 783 | Changed |= E != CurElt; |
| 784 | Resolved.push_back(Elt: E); |
| 785 | } |
| 786 | |
| 787 | if (Changed) |
| 788 | return ListInit::get(Elements: Resolved, EltTy: getElementType()); |
| 789 | return this; |
| 790 | } |
| 791 | |
| 792 | bool ListInit::isComplete() const { |
| 793 | return all_of(Range: *this, |
| 794 | P: [](const Init *Element) { return Element->isComplete(); }); |
| 795 | } |
| 796 | |
| 797 | bool ListInit::isConcrete() const { |
| 798 | return all_of(Range: *this, |
| 799 | P: [](const Init *Element) { return Element->isConcrete(); }); |
| 800 | } |
| 801 | |
| 802 | std::string ListInit::getAsString() const { |
| 803 | std::string Result = "[" ; |
| 804 | ListSeparator LS; |
| 805 | for (const Init *Element : *this) { |
| 806 | Result += LS; |
| 807 | Result += Element->getAsString(); |
| 808 | } |
| 809 | return Result + "]" ; |
| 810 | } |
| 811 | |
| 812 | const Init *OpInit::getBit(unsigned Bit) const { |
| 813 | if (getType() == BitRecTy::get(RK&: getRecordKeeper())) |
| 814 | return this; |
| 815 | return VarBitInit::get(T: this, B: Bit); |
| 816 | } |
| 817 | |
| 818 | static void ProfileUnOpInit(FoldingSetNodeID &ID, unsigned Opcode, |
| 819 | const Init *Op, const RecTy *Type) { |
| 820 | ID.AddInteger(I: Opcode); |
| 821 | ID.AddPointer(Ptr: Op); |
| 822 | ID.AddPointer(Ptr: Type); |
| 823 | } |
| 824 | |
| 825 | const UnOpInit *UnOpInit::get(UnaryOp Opc, const Init *LHS, const RecTy *Type) { |
| 826 | FoldingSetNodeID ID; |
| 827 | ProfileUnOpInit(ID, Opcode: Opc, Op: LHS, Type); |
| 828 | |
| 829 | detail::RecordKeeperImpl &RK = Type->getRecordKeeper().getImpl(); |
| 830 | void *IP = nullptr; |
| 831 | if (const UnOpInit *I = RK.TheUnOpInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 832 | return I; |
| 833 | |
| 834 | UnOpInit *I = new (RK.Allocator) UnOpInit(Opc, LHS, Type); |
| 835 | RK.TheUnOpInitPool.InsertNode(N: I, InsertPos: IP); |
| 836 | return I; |
| 837 | } |
| 838 | |
| 839 | void UnOpInit::Profile(FoldingSetNodeID &ID) const { |
| 840 | ProfileUnOpInit(ID, Opcode: getOpcode(), Op: getOperand(), Type: getType()); |
| 841 | } |
| 842 | |
| 843 | const Init *UnOpInit::Fold(const Record *CurRec, bool IsFinal) const { |
| 844 | RecordKeeper &RK = getRecordKeeper(); |
| 845 | switch (getOpcode()) { |
| 846 | case REPR: |
| 847 | if (LHS->isConcrete()) { |
| 848 | // If it is a Record, print the full content. |
| 849 | if (const auto *Def = dyn_cast<DefInit>(Val: LHS)) { |
| 850 | std::string S; |
| 851 | raw_string_ostream OS(S); |
| 852 | OS << *Def->getDef(); |
| 853 | return StringInit::get(RK, V: S); |
| 854 | } else { |
| 855 | // Otherwise, print the value of the variable. |
| 856 | // |
| 857 | // NOTE: we could recursively !repr the elements of a list, |
| 858 | // but that could produce a lot of output when printing a |
| 859 | // defset. |
| 860 | return StringInit::get(RK, V: LHS->getAsString()); |
| 861 | } |
| 862 | } |
| 863 | break; |
| 864 | case TOLOWER: |
| 865 | if (const auto *LHSs = dyn_cast<StringInit>(Val: LHS)) |
| 866 | return StringInit::get(RK, V: LHSs->getValue().lower()); |
| 867 | break; |
| 868 | case TOUPPER: |
| 869 | if (const auto *LHSs = dyn_cast<StringInit>(Val: LHS)) |
| 870 | return StringInit::get(RK, V: LHSs->getValue().upper()); |
| 871 | break; |
| 872 | case CAST: |
| 873 | if (isa<StringRecTy>(Val: getType())) { |
| 874 | if (const auto *LHSs = dyn_cast<StringInit>(Val: LHS)) |
| 875 | return LHSs; |
| 876 | |
| 877 | if (const auto *LHSd = dyn_cast<DefInit>(Val: LHS)) |
| 878 | return StringInit::get(RK, V: LHSd->getAsString()); |
| 879 | |
| 880 | if (const auto *LHSi = dyn_cast_or_null<IntInit>( |
| 881 | Val: LHS->convertInitializerTo(Ty: IntRecTy::get(RK)))) |
| 882 | return StringInit::get(RK, V: LHSi->getAsString()); |
| 883 | |
| 884 | } else if (isa<RecordRecTy>(Val: getType())) { |
| 885 | if (const auto *Name = dyn_cast<StringInit>(Val: LHS)) { |
| 886 | const Record *D = RK.getDef(Name: Name->getValue()); |
| 887 | if (!D && CurRec) { |
| 888 | // Self-references are allowed, but their resolution is delayed until |
| 889 | // the final resolve to ensure that we get the correct type for them. |
| 890 | auto *Anonymous = dyn_cast<AnonymousNameInit>(Val: CurRec->getNameInit()); |
| 891 | if (Name == CurRec->getNameInit() || |
| 892 | (Anonymous && Name == Anonymous->getNameInit())) { |
| 893 | if (!IsFinal) |
| 894 | break; |
| 895 | D = CurRec; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | auto PrintFatalErrorHelper = [CurRec](const Twine &T) { |
| 900 | if (CurRec) |
| 901 | PrintFatalError(ErrorLoc: CurRec->getLoc(), Msg: T); |
| 902 | else |
| 903 | PrintFatalError(Msg: T); |
| 904 | }; |
| 905 | |
| 906 | if (!D) { |
| 907 | if (IsFinal) { |
| 908 | PrintFatalErrorHelper(Twine("Undefined reference to record: '" ) + |
| 909 | Name->getValue() + "'\n" ); |
| 910 | } |
| 911 | break; |
| 912 | } |
| 913 | |
| 914 | DefInit *DI = D->getDefInit(); |
| 915 | if (!DI->getType()->typeIsA(RHS: getType())) { |
| 916 | PrintFatalErrorHelper(Twine("Expected type '" ) + |
| 917 | getType()->getAsString() + "', got '" + |
| 918 | DI->getType()->getAsString() + "' in: " + |
| 919 | getAsString() + "\n" ); |
| 920 | } |
| 921 | return DI; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | if (const Init *NewInit = LHS->convertInitializerTo(Ty: getType())) |
| 926 | return NewInit; |
| 927 | break; |
| 928 | |
| 929 | case INITIALIZED: |
| 930 | if (isa<UnsetInit>(Val: LHS)) |
| 931 | return IntInit::get(RK, V: 0); |
| 932 | if (LHS->isConcrete()) |
| 933 | return IntInit::get(RK, V: 1); |
| 934 | break; |
| 935 | |
| 936 | case NOT: |
| 937 | if (const auto *LHSi = dyn_cast_or_null<IntInit>( |
| 938 | Val: LHS->convertInitializerTo(Ty: IntRecTy::get(RK)))) |
| 939 | return IntInit::get(RK, V: LHSi->getValue() ? 0 : 1); |
| 940 | break; |
| 941 | |
| 942 | case HEAD: |
| 943 | if (const auto *LHSl = dyn_cast<ListInit>(Val: LHS)) { |
| 944 | assert(!LHSl->empty() && "Empty list in head" ); |
| 945 | return LHSl->getElement(Idx: 0); |
| 946 | } |
| 947 | break; |
| 948 | |
| 949 | case TAIL: |
| 950 | if (const auto *LHSl = dyn_cast<ListInit>(Val: LHS)) { |
| 951 | assert(!LHSl->empty() && "Empty list in tail" ); |
| 952 | // Note the slice(1). We can't just pass the result of getElements() |
| 953 | // directly. |
| 954 | return ListInit::get(Elements: LHSl->getElements().slice(N: 1), |
| 955 | EltTy: LHSl->getElementType()); |
| 956 | } |
| 957 | break; |
| 958 | |
| 959 | case SIZE: |
| 960 | if (const auto *LHSl = dyn_cast<ListInit>(Val: LHS)) |
| 961 | return IntInit::get(RK, V: LHSl->size()); |
| 962 | if (const auto *LHSd = dyn_cast<DagInit>(Val: LHS)) |
| 963 | return IntInit::get(RK, V: LHSd->arg_size()); |
| 964 | if (const auto *LHSs = dyn_cast<StringInit>(Val: LHS)) |
| 965 | return IntInit::get(RK, V: LHSs->getValue().size()); |
| 966 | break; |
| 967 | |
| 968 | case EMPTY: |
| 969 | if (const auto *LHSl = dyn_cast<ListInit>(Val: LHS)) |
| 970 | return IntInit::get(RK, V: LHSl->empty()); |
| 971 | if (const auto *LHSd = dyn_cast<DagInit>(Val: LHS)) |
| 972 | return IntInit::get(RK, V: LHSd->arg_empty()); |
| 973 | if (const auto *LHSs = dyn_cast<StringInit>(Val: LHS)) |
| 974 | return IntInit::get(RK, V: LHSs->getValue().empty()); |
| 975 | break; |
| 976 | |
| 977 | case GETDAGOP: |
| 978 | if (const auto *Dag = dyn_cast<DagInit>(Val: LHS)) { |
| 979 | // TI is not necessarily a def due to the late resolution in multiclasses, |
| 980 | // but has to be a TypedInit. |
| 981 | auto *TI = cast<TypedInit>(Val: Dag->getOperator()); |
| 982 | if (!TI->getType()->typeIsA(RHS: getType())) { |
| 983 | PrintFatalError(ErrorLoc: CurRec->getLoc(), |
| 984 | Msg: Twine("Expected type '" ) + getType()->getAsString() + |
| 985 | "', got '" + TI->getType()->getAsString() + |
| 986 | "' in: " + getAsString() + "\n" ); |
| 987 | } else { |
| 988 | return Dag->getOperator(); |
| 989 | } |
| 990 | } |
| 991 | break; |
| 992 | |
| 993 | case GETDAGOPNAME: |
| 994 | if (const auto *Dag = dyn_cast<DagInit>(Val: LHS)) { |
| 995 | return Dag->getName(); |
| 996 | } |
| 997 | break; |
| 998 | |
| 999 | case LOG2: |
| 1000 | if (const auto *LHSi = dyn_cast_or_null<IntInit>( |
| 1001 | Val: LHS->convertInitializerTo(Ty: IntRecTy::get(RK)))) { |
| 1002 | int64_t LHSv = LHSi->getValue(); |
| 1003 | if (LHSv <= 0) { |
| 1004 | PrintFatalError(ErrorLoc: CurRec->getLoc(), |
| 1005 | Msg: "Illegal operation: logtwo is undefined " |
| 1006 | "on arguments less than or equal to 0" ); |
| 1007 | } else { |
| 1008 | uint64_t Log = Log2_64(Value: LHSv); |
| 1009 | assert(Log <= INT64_MAX && |
| 1010 | "Log of an int64_t must be smaller than INT64_MAX" ); |
| 1011 | return IntInit::get(RK, V: static_cast<int64_t>(Log)); |
| 1012 | } |
| 1013 | } |
| 1014 | break; |
| 1015 | |
| 1016 | case LISTFLATTEN: |
| 1017 | if (const auto *LHSList = dyn_cast<ListInit>(Val: LHS)) { |
| 1018 | const auto *InnerListTy = dyn_cast<ListRecTy>(Val: LHSList->getElementType()); |
| 1019 | // list of non-lists, !listflatten() is a NOP. |
| 1020 | if (!InnerListTy) |
| 1021 | return LHS; |
| 1022 | |
| 1023 | auto Flatten = |
| 1024 | [](const ListInit *List) -> std::optional<std::vector<const Init *>> { |
| 1025 | std::vector<const Init *> Flattened; |
| 1026 | // Concatenate elements of all the inner lists. |
| 1027 | for (const Init *InnerInit : List->getElements()) { |
| 1028 | const auto *InnerList = dyn_cast<ListInit>(Val: InnerInit); |
| 1029 | if (!InnerList) |
| 1030 | return std::nullopt; |
| 1031 | llvm::append_range(C&: Flattened, R: InnerList->getElements()); |
| 1032 | }; |
| 1033 | return Flattened; |
| 1034 | }; |
| 1035 | |
| 1036 | auto Flattened = Flatten(LHSList); |
| 1037 | if (Flattened) |
| 1038 | return ListInit::get(Elements: *Flattened, EltTy: InnerListTy->getElementType()); |
| 1039 | } |
| 1040 | break; |
| 1041 | } |
| 1042 | return this; |
| 1043 | } |
| 1044 | |
| 1045 | const Init *UnOpInit::resolveReferences(Resolver &R) const { |
| 1046 | const Init *lhs = LHS->resolveReferences(R); |
| 1047 | |
| 1048 | if (LHS != lhs || (R.isFinal() && getOpcode() == CAST)) |
| 1049 | return (UnOpInit::get(Opc: getOpcode(), LHS: lhs, Type: getType())) |
| 1050 | ->Fold(CurRec: R.getCurrentRecord(), IsFinal: R.isFinal()); |
| 1051 | return this; |
| 1052 | } |
| 1053 | |
| 1054 | std::string UnOpInit::getAsString() const { |
| 1055 | std::string Result; |
| 1056 | switch (getOpcode()) { |
| 1057 | case CAST: Result = "!cast<" + getType()->getAsString() + ">" ; break; |
| 1058 | case NOT: Result = "!not" ; break; |
| 1059 | case HEAD: Result = "!head" ; break; |
| 1060 | case TAIL: Result = "!tail" ; break; |
| 1061 | case SIZE: Result = "!size" ; break; |
| 1062 | case EMPTY: Result = "!empty" ; break; |
| 1063 | case GETDAGOP: Result = "!getdagop" ; break; |
| 1064 | case GETDAGOPNAME: |
| 1065 | Result = "!getdagopname" ; |
| 1066 | break; |
| 1067 | case LOG2 : Result = "!logtwo" ; break; |
| 1068 | case LISTFLATTEN: |
| 1069 | Result = "!listflatten" ; |
| 1070 | break; |
| 1071 | case REPR: |
| 1072 | Result = "!repr" ; |
| 1073 | break; |
| 1074 | case TOLOWER: |
| 1075 | Result = "!tolower" ; |
| 1076 | break; |
| 1077 | case TOUPPER: |
| 1078 | Result = "!toupper" ; |
| 1079 | break; |
| 1080 | case INITIALIZED: |
| 1081 | Result = "!initialized" ; |
| 1082 | break; |
| 1083 | } |
| 1084 | return Result + "(" + LHS->getAsString() + ")" ; |
| 1085 | } |
| 1086 | |
| 1087 | static void ProfileBinOpInit(FoldingSetNodeID &ID, unsigned Opcode, |
| 1088 | const Init *LHS, const Init *RHS, |
| 1089 | const RecTy *Type) { |
| 1090 | ID.AddInteger(I: Opcode); |
| 1091 | ID.AddPointer(Ptr: LHS); |
| 1092 | ID.AddPointer(Ptr: RHS); |
| 1093 | ID.AddPointer(Ptr: Type); |
| 1094 | } |
| 1095 | |
| 1096 | const BinOpInit *BinOpInit::get(BinaryOp Opc, const Init *LHS, const Init *RHS, |
| 1097 | const RecTy *Type) { |
| 1098 | FoldingSetNodeID ID; |
| 1099 | ProfileBinOpInit(ID, Opcode: Opc, LHS, RHS, Type); |
| 1100 | |
| 1101 | detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl(); |
| 1102 | void *IP = nullptr; |
| 1103 | if (const BinOpInit *I = RK.TheBinOpInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 1104 | return I; |
| 1105 | |
| 1106 | BinOpInit *I = new (RK.Allocator) BinOpInit(Opc, LHS, RHS, Type); |
| 1107 | RK.TheBinOpInitPool.InsertNode(N: I, InsertPos: IP); |
| 1108 | return I; |
| 1109 | } |
| 1110 | |
| 1111 | void BinOpInit::Profile(FoldingSetNodeID &ID) const { |
| 1112 | ProfileBinOpInit(ID, Opcode: getOpcode(), LHS: getLHS(), RHS: getRHS(), Type: getType()); |
| 1113 | } |
| 1114 | |
| 1115 | static const StringInit *ConcatStringInits(const StringInit *I0, |
| 1116 | const StringInit *I1) { |
| 1117 | SmallString<80> Concat(I0->getValue()); |
| 1118 | Concat.append(RHS: I1->getValue()); |
| 1119 | return StringInit::get( |
| 1120 | RK&: I0->getRecordKeeper(), V: Concat, |
| 1121 | Fmt: StringInit::determineFormat(Fmt1: I0->getFormat(), Fmt2: I1->getFormat())); |
| 1122 | } |
| 1123 | |
| 1124 | static const StringInit *interleaveStringList(const ListInit *List, |
| 1125 | const StringInit *Delim) { |
| 1126 | if (List->size() == 0) |
| 1127 | return StringInit::get(RK&: List->getRecordKeeper(), V: "" ); |
| 1128 | const auto *Element = dyn_cast<StringInit>(Val: List->getElement(Idx: 0)); |
| 1129 | if (!Element) |
| 1130 | return nullptr; |
| 1131 | SmallString<80> Result(Element->getValue()); |
| 1132 | StringInit::StringFormat Fmt = StringInit::SF_String; |
| 1133 | |
| 1134 | for (const Init *Elem : List->getElements().drop_front()) { |
| 1135 | Result.append(RHS: Delim->getValue()); |
| 1136 | const auto *Element = dyn_cast<StringInit>(Val: Elem); |
| 1137 | if (!Element) |
| 1138 | return nullptr; |
| 1139 | Result.append(RHS: Element->getValue()); |
| 1140 | Fmt = StringInit::determineFormat(Fmt1: Fmt, Fmt2: Element->getFormat()); |
| 1141 | } |
| 1142 | return StringInit::get(RK&: List->getRecordKeeper(), V: Result, Fmt); |
| 1143 | } |
| 1144 | |
| 1145 | static const StringInit *interleaveIntList(const ListInit *List, |
| 1146 | const StringInit *Delim) { |
| 1147 | RecordKeeper &RK = List->getRecordKeeper(); |
| 1148 | if (List->size() == 0) |
| 1149 | return StringInit::get(RK, V: "" ); |
| 1150 | const auto *Element = dyn_cast_or_null<IntInit>( |
| 1151 | Val: List->getElement(Idx: 0)->convertInitializerTo(Ty: IntRecTy::get(RK))); |
| 1152 | if (!Element) |
| 1153 | return nullptr; |
| 1154 | SmallString<80> Result(Element->getAsString()); |
| 1155 | |
| 1156 | for (const Init *Elem : List->getElements().drop_front()) { |
| 1157 | Result.append(RHS: Delim->getValue()); |
| 1158 | const auto *Element = dyn_cast_or_null<IntInit>( |
| 1159 | Val: Elem->convertInitializerTo(Ty: IntRecTy::get(RK))); |
| 1160 | if (!Element) |
| 1161 | return nullptr; |
| 1162 | Result.append(RHS: Element->getAsString()); |
| 1163 | } |
| 1164 | return StringInit::get(RK, V: Result); |
| 1165 | } |
| 1166 | |
| 1167 | const Init *BinOpInit::getStrConcat(const Init *I0, const Init *I1) { |
| 1168 | // Shortcut for the common case of concatenating two strings. |
| 1169 | if (const auto *I0s = dyn_cast<StringInit>(Val: I0)) |
| 1170 | if (const auto *I1s = dyn_cast<StringInit>(Val: I1)) |
| 1171 | return ConcatStringInits(I0: I0s, I1: I1s); |
| 1172 | return BinOpInit::get(Opc: BinOpInit::STRCONCAT, LHS: I0, RHS: I1, |
| 1173 | Type: StringRecTy::get(RK&: I0->getRecordKeeper())); |
| 1174 | } |
| 1175 | |
| 1176 | static const ListInit *ConcatListInits(const ListInit *LHS, |
| 1177 | const ListInit *RHS) { |
| 1178 | SmallVector<const Init *, 8> Args; |
| 1179 | llvm::append_range(C&: Args, R: *LHS); |
| 1180 | llvm::append_range(C&: Args, R: *RHS); |
| 1181 | return ListInit::get(Elements: Args, EltTy: LHS->getElementType()); |
| 1182 | } |
| 1183 | |
| 1184 | const Init *BinOpInit::getListConcat(const TypedInit *LHS, const Init *RHS) { |
| 1185 | assert(isa<ListRecTy>(LHS->getType()) && "First arg must be a list" ); |
| 1186 | |
| 1187 | // Shortcut for the common case of concatenating two lists. |
| 1188 | if (const auto *LHSList = dyn_cast<ListInit>(Val: LHS)) |
| 1189 | if (const auto *RHSList = dyn_cast<ListInit>(Val: RHS)) |
| 1190 | return ConcatListInits(LHS: LHSList, RHS: RHSList); |
| 1191 | return BinOpInit::get(Opc: BinOpInit::LISTCONCAT, LHS, RHS, Type: LHS->getType()); |
| 1192 | } |
| 1193 | |
| 1194 | std::optional<bool> BinOpInit::CompareInit(unsigned Opc, const Init *LHS, |
| 1195 | const Init *RHS) const { |
| 1196 | // First see if we have two bit, bits, or int. |
| 1197 | const auto *LHSi = dyn_cast_or_null<IntInit>( |
| 1198 | Val: LHS->convertInitializerTo(Ty: IntRecTy::get(RK&: getRecordKeeper()))); |
| 1199 | const auto *RHSi = dyn_cast_or_null<IntInit>( |
| 1200 | Val: RHS->convertInitializerTo(Ty: IntRecTy::get(RK&: getRecordKeeper()))); |
| 1201 | |
| 1202 | if (LHSi && RHSi) { |
| 1203 | bool Result; |
| 1204 | switch (Opc) { |
| 1205 | case EQ: |
| 1206 | Result = LHSi->getValue() == RHSi->getValue(); |
| 1207 | break; |
| 1208 | case NE: |
| 1209 | Result = LHSi->getValue() != RHSi->getValue(); |
| 1210 | break; |
| 1211 | case LE: |
| 1212 | Result = LHSi->getValue() <= RHSi->getValue(); |
| 1213 | break; |
| 1214 | case LT: |
| 1215 | Result = LHSi->getValue() < RHSi->getValue(); |
| 1216 | break; |
| 1217 | case GE: |
| 1218 | Result = LHSi->getValue() >= RHSi->getValue(); |
| 1219 | break; |
| 1220 | case GT: |
| 1221 | Result = LHSi->getValue() > RHSi->getValue(); |
| 1222 | break; |
| 1223 | default: |
| 1224 | llvm_unreachable("unhandled comparison" ); |
| 1225 | } |
| 1226 | return Result; |
| 1227 | } |
| 1228 | |
| 1229 | // Next try strings. |
| 1230 | const auto *LHSs = dyn_cast<StringInit>(Val: LHS); |
| 1231 | const auto *RHSs = dyn_cast<StringInit>(Val: RHS); |
| 1232 | |
| 1233 | if (LHSs && RHSs) { |
| 1234 | bool Result; |
| 1235 | switch (Opc) { |
| 1236 | case EQ: |
| 1237 | Result = LHSs->getValue() == RHSs->getValue(); |
| 1238 | break; |
| 1239 | case NE: |
| 1240 | Result = LHSs->getValue() != RHSs->getValue(); |
| 1241 | break; |
| 1242 | case LE: |
| 1243 | Result = LHSs->getValue() <= RHSs->getValue(); |
| 1244 | break; |
| 1245 | case LT: |
| 1246 | Result = LHSs->getValue() < RHSs->getValue(); |
| 1247 | break; |
| 1248 | case GE: |
| 1249 | Result = LHSs->getValue() >= RHSs->getValue(); |
| 1250 | break; |
| 1251 | case GT: |
| 1252 | Result = LHSs->getValue() > RHSs->getValue(); |
| 1253 | break; |
| 1254 | default: |
| 1255 | llvm_unreachable("unhandled comparison" ); |
| 1256 | } |
| 1257 | return Result; |
| 1258 | } |
| 1259 | |
| 1260 | // Finally, !eq and !ne can be used with records. |
| 1261 | if (Opc == EQ || Opc == NE) { |
| 1262 | const auto *LHSd = dyn_cast<DefInit>(Val: LHS); |
| 1263 | const auto *RHSd = dyn_cast<DefInit>(Val: RHS); |
| 1264 | if (LHSd && RHSd) |
| 1265 | return (Opc == EQ) ? LHSd == RHSd : LHSd != RHSd; |
| 1266 | } |
| 1267 | |
| 1268 | return std::nullopt; |
| 1269 | } |
| 1270 | |
| 1271 | static std::optional<unsigned> |
| 1272 | getDagArgNoByKey(const DagInit *Dag, const Init *Key, std::string &Error) { |
| 1273 | // Accessor by index |
| 1274 | if (const auto *Idx = dyn_cast<IntInit>(Val: Key)) { |
| 1275 | int64_t Pos = Idx->getValue(); |
| 1276 | if (Pos < 0) { |
| 1277 | // The index is negative. |
| 1278 | Error = |
| 1279 | (Twine("index " ) + std::to_string(val: Pos) + Twine(" is negative" )).str(); |
| 1280 | return std::nullopt; |
| 1281 | } |
| 1282 | if (Pos >= Dag->getNumArgs()) { |
| 1283 | // The index is out-of-range. |
| 1284 | Error = (Twine("index " ) + std::to_string(val: Pos) + |
| 1285 | " is out of range (dag has " + |
| 1286 | std::to_string(val: Dag->getNumArgs()) + " arguments)" ) |
| 1287 | .str(); |
| 1288 | return std::nullopt; |
| 1289 | } |
| 1290 | return Pos; |
| 1291 | } |
| 1292 | assert(isa<StringInit>(Key)); |
| 1293 | // Accessor by name |
| 1294 | const auto *Name = dyn_cast<StringInit>(Val: Key); |
| 1295 | auto ArgNo = Dag->getArgNo(Name: Name->getValue()); |
| 1296 | if (!ArgNo) { |
| 1297 | // The key is not found. |
| 1298 | Error = (Twine("key '" ) + Name->getValue() + Twine("' is not found" )).str(); |
| 1299 | return std::nullopt; |
| 1300 | } |
| 1301 | return *ArgNo; |
| 1302 | } |
| 1303 | |
| 1304 | const Init *BinOpInit::Fold(const Record *CurRec) const { |
| 1305 | switch (getOpcode()) { |
| 1306 | case CONCAT: { |
| 1307 | const auto *LHSs = dyn_cast<DagInit>(Val: LHS); |
| 1308 | const auto *RHSs = dyn_cast<DagInit>(Val: RHS); |
| 1309 | if (LHSs && RHSs) { |
| 1310 | const auto *LOp = dyn_cast<DefInit>(Val: LHSs->getOperator()); |
| 1311 | const auto *ROp = dyn_cast<DefInit>(Val: RHSs->getOperator()); |
| 1312 | if ((!LOp && !isa<UnsetInit>(Val: LHSs->getOperator())) || |
| 1313 | (!ROp && !isa<UnsetInit>(Val: RHSs->getOperator()))) |
| 1314 | break; |
| 1315 | if (LOp && ROp && LOp->getDef() != ROp->getDef()) { |
| 1316 | PrintFatalError(Msg: Twine("Concatenated Dag operators do not match: '" ) + |
| 1317 | LHSs->getAsString() + "' vs. '" + RHSs->getAsString() + |
| 1318 | "'" ); |
| 1319 | } |
| 1320 | const Init *Op = LOp ? LOp : ROp; |
| 1321 | if (!Op) |
| 1322 | Op = UnsetInit::get(RK&: getRecordKeeper()); |
| 1323 | |
| 1324 | SmallVector<std::pair<const Init *, const StringInit *>, 8> Args; |
| 1325 | llvm::append_range(C&: Args, R: LHSs->getArgAndNames()); |
| 1326 | llvm::append_range(C&: Args, R: RHSs->getArgAndNames()); |
| 1327 | // Use the name of the LHS DAG if it's set, otherwise the name of the RHS. |
| 1328 | const auto *NameInit = LHSs->getName(); |
| 1329 | if (!NameInit) |
| 1330 | NameInit = RHSs->getName(); |
| 1331 | return DagInit::get(V: Op, VN: NameInit, ArgAndNames: Args); |
| 1332 | } |
| 1333 | break; |
| 1334 | } |
| 1335 | case MATCH: { |
| 1336 | const auto *StrInit = dyn_cast<StringInit>(Val: LHS); |
| 1337 | if (!StrInit) |
| 1338 | return this; |
| 1339 | |
| 1340 | const auto *RegexInit = dyn_cast<StringInit>(Val: RHS); |
| 1341 | if (!RegexInit) |
| 1342 | return this; |
| 1343 | |
| 1344 | StringRef RegexStr = RegexInit->getValue(); |
| 1345 | llvm::Regex Matcher(RegexStr); |
| 1346 | if (!Matcher.isValid()) |
| 1347 | PrintFatalError(Msg: Twine("invalid regex '" ) + RegexStr + Twine("'" )); |
| 1348 | |
| 1349 | return BitInit::get(RK&: LHS->getRecordKeeper(), |
| 1350 | V: Matcher.match(String: StrInit->getValue())); |
| 1351 | } |
| 1352 | case LISTCONCAT: { |
| 1353 | const auto *LHSs = dyn_cast<ListInit>(Val: LHS); |
| 1354 | const auto *RHSs = dyn_cast<ListInit>(Val: RHS); |
| 1355 | if (LHSs && RHSs) { |
| 1356 | SmallVector<const Init *, 8> Args; |
| 1357 | llvm::append_range(C&: Args, R: *LHSs); |
| 1358 | llvm::append_range(C&: Args, R: *RHSs); |
| 1359 | return ListInit::get(Elements: Args, EltTy: LHSs->getElementType()); |
| 1360 | } |
| 1361 | break; |
| 1362 | } |
| 1363 | case LISTSPLAT: { |
| 1364 | const auto *Value = dyn_cast<TypedInit>(Val: LHS); |
| 1365 | const auto *Count = dyn_cast<IntInit>(Val: RHS); |
| 1366 | if (Value && Count) { |
| 1367 | if (Count->getValue() < 0) |
| 1368 | PrintFatalError(Msg: Twine("!listsplat count " ) + Count->getAsString() + |
| 1369 | " is negative" ); |
| 1370 | SmallVector<const Init *, 8> Args(Count->getValue(), Value); |
| 1371 | return ListInit::get(Elements: Args, EltTy: Value->getType()); |
| 1372 | } |
| 1373 | break; |
| 1374 | } |
| 1375 | case LISTREMOVE: { |
| 1376 | const auto *LHSs = dyn_cast<ListInit>(Val: LHS); |
| 1377 | const auto *RHSs = dyn_cast<ListInit>(Val: RHS); |
| 1378 | if (LHSs && RHSs) { |
| 1379 | SmallVector<const Init *, 8> Args; |
| 1380 | for (const Init *EltLHS : *LHSs) { |
| 1381 | bool Found = false; |
| 1382 | for (const Init *EltRHS : *RHSs) { |
| 1383 | if (std::optional<bool> Result = CompareInit(Opc: EQ, LHS: EltLHS, RHS: EltRHS)) { |
| 1384 | if (*Result) { |
| 1385 | Found = true; |
| 1386 | break; |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | if (!Found) |
| 1391 | Args.push_back(Elt: EltLHS); |
| 1392 | } |
| 1393 | return ListInit::get(Elements: Args, EltTy: LHSs->getElementType()); |
| 1394 | } |
| 1395 | break; |
| 1396 | } |
| 1397 | case LISTELEM: { |
| 1398 | const auto *TheList = dyn_cast<ListInit>(Val: LHS); |
| 1399 | const auto *Idx = dyn_cast<IntInit>(Val: RHS); |
| 1400 | if (!TheList || !Idx) |
| 1401 | break; |
| 1402 | auto i = Idx->getValue(); |
| 1403 | if (i < 0 || i >= (ssize_t)TheList->size()) |
| 1404 | break; |
| 1405 | return TheList->getElement(Idx: i); |
| 1406 | } |
| 1407 | case LISTSLICE: { |
| 1408 | const auto *TheList = dyn_cast<ListInit>(Val: LHS); |
| 1409 | const auto *SliceIdxs = dyn_cast<ListInit>(Val: RHS); |
| 1410 | if (!TheList || !SliceIdxs) |
| 1411 | break; |
| 1412 | SmallVector<const Init *, 8> Args; |
| 1413 | Args.reserve(N: SliceIdxs->size()); |
| 1414 | for (auto *I : *SliceIdxs) { |
| 1415 | auto *II = dyn_cast<IntInit>(Val: I); |
| 1416 | if (!II) |
| 1417 | goto unresolved; |
| 1418 | auto i = II->getValue(); |
| 1419 | if (i < 0 || i >= (ssize_t)TheList->size()) |
| 1420 | goto unresolved; |
| 1421 | Args.push_back(Elt: TheList->getElement(Idx: i)); |
| 1422 | } |
| 1423 | return ListInit::get(Elements: Args, EltTy: TheList->getElementType()); |
| 1424 | } |
| 1425 | case RANGEC: { |
| 1426 | const auto *LHSi = dyn_cast<IntInit>(Val: LHS); |
| 1427 | const auto *RHSi = dyn_cast<IntInit>(Val: RHS); |
| 1428 | if (!LHSi || !RHSi) |
| 1429 | break; |
| 1430 | |
| 1431 | int64_t Start = LHSi->getValue(); |
| 1432 | int64_t End = RHSi->getValue(); |
| 1433 | SmallVector<const Init *, 8> Args; |
| 1434 | if (getOpcode() == RANGEC) { |
| 1435 | // Closed interval |
| 1436 | if (Start <= End) { |
| 1437 | // Ascending order |
| 1438 | Args.reserve(N: End - Start + 1); |
| 1439 | for (auto i = Start; i <= End; ++i) |
| 1440 | Args.push_back(Elt: IntInit::get(RK&: getRecordKeeper(), V: i)); |
| 1441 | } else { |
| 1442 | // Descending order |
| 1443 | Args.reserve(N: Start - End + 1); |
| 1444 | for (auto i = Start; i >= End; --i) |
| 1445 | Args.push_back(Elt: IntInit::get(RK&: getRecordKeeper(), V: i)); |
| 1446 | } |
| 1447 | } else if (Start < End) { |
| 1448 | // Half-open interval (excludes `End`) |
| 1449 | Args.reserve(N: End - Start); |
| 1450 | for (auto i = Start; i < End; ++i) |
| 1451 | Args.push_back(Elt: IntInit::get(RK&: getRecordKeeper(), V: i)); |
| 1452 | } else { |
| 1453 | // Empty set |
| 1454 | } |
| 1455 | return ListInit::get(Elements: Args, EltTy: LHSi->getType()); |
| 1456 | } |
| 1457 | case STRCONCAT: { |
| 1458 | const auto *LHSs = dyn_cast<StringInit>(Val: LHS); |
| 1459 | const auto *RHSs = dyn_cast<StringInit>(Val: RHS); |
| 1460 | if (LHSs && RHSs) |
| 1461 | return ConcatStringInits(I0: LHSs, I1: RHSs); |
| 1462 | break; |
| 1463 | } |
| 1464 | case INTERLEAVE: { |
| 1465 | const auto *List = dyn_cast<ListInit>(Val: LHS); |
| 1466 | const auto *Delim = dyn_cast<StringInit>(Val: RHS); |
| 1467 | if (List && Delim) { |
| 1468 | const StringInit *Result; |
| 1469 | if (isa<StringRecTy>(Val: List->getElementType())) |
| 1470 | Result = interleaveStringList(List, Delim); |
| 1471 | else |
| 1472 | Result = interleaveIntList(List, Delim); |
| 1473 | if (Result) |
| 1474 | return Result; |
| 1475 | } |
| 1476 | break; |
| 1477 | } |
| 1478 | case EQ: |
| 1479 | case NE: |
| 1480 | case LE: |
| 1481 | case LT: |
| 1482 | case GE: |
| 1483 | case GT: { |
| 1484 | if (std::optional<bool> Result = CompareInit(Opc: getOpcode(), LHS, RHS)) |
| 1485 | return BitInit::get(RK&: getRecordKeeper(), V: *Result); |
| 1486 | break; |
| 1487 | } |
| 1488 | case GETDAGARG: { |
| 1489 | const auto *Dag = dyn_cast<DagInit>(Val: LHS); |
| 1490 | if (Dag && isa<IntInit, StringInit>(Val: RHS)) { |
| 1491 | std::string Error; |
| 1492 | auto ArgNo = getDagArgNoByKey(Dag, Key: RHS, Error); |
| 1493 | if (!ArgNo) |
| 1494 | PrintFatalError(ErrorLoc: CurRec->getLoc(), Msg: "!getdagarg " + Error); |
| 1495 | |
| 1496 | assert(*ArgNo < Dag->getNumArgs()); |
| 1497 | |
| 1498 | const Init *Arg = Dag->getArg(Num: *ArgNo); |
| 1499 | if (const auto *TI = dyn_cast<TypedInit>(Val: Arg)) |
| 1500 | if (!TI->getType()->typeIsConvertibleTo(RHS: getType())) |
| 1501 | return UnsetInit::get(RK&: Dag->getRecordKeeper()); |
| 1502 | return Arg; |
| 1503 | } |
| 1504 | break; |
| 1505 | } |
| 1506 | case GETDAGNAME: { |
| 1507 | const auto *Dag = dyn_cast<DagInit>(Val: LHS); |
| 1508 | const auto *Idx = dyn_cast<IntInit>(Val: RHS); |
| 1509 | if (Dag && Idx) { |
| 1510 | int64_t Pos = Idx->getValue(); |
| 1511 | if (Pos < 0 || Pos >= Dag->getNumArgs()) { |
| 1512 | // The index is out-of-range. |
| 1513 | PrintError(ErrorLoc: CurRec->getLoc(), |
| 1514 | Msg: Twine("!getdagname index is out of range 0..." ) + |
| 1515 | std::to_string(val: Dag->getNumArgs() - 1) + ": " + |
| 1516 | std::to_string(val: Pos)); |
| 1517 | } |
| 1518 | const Init *ArgName = Dag->getArgName(Num: Pos); |
| 1519 | if (!ArgName) |
| 1520 | return UnsetInit::get(RK&: getRecordKeeper()); |
| 1521 | return ArgName; |
| 1522 | } |
| 1523 | break; |
| 1524 | } |
| 1525 | case SETDAGOP: { |
| 1526 | const auto *Dag = dyn_cast<DagInit>(Val: LHS); |
| 1527 | const auto *Op = dyn_cast<DefInit>(Val: RHS); |
| 1528 | if (Dag && Op) |
| 1529 | return DagInit::get(V: Op, Args: Dag->getArgs(), ArgNames: Dag->getArgNames()); |
| 1530 | break; |
| 1531 | } |
| 1532 | case SETDAGOPNAME: { |
| 1533 | const auto *Dag = dyn_cast<DagInit>(Val: LHS); |
| 1534 | const auto *Op = dyn_cast<StringInit>(Val: RHS); |
| 1535 | if (Dag && Op) |
| 1536 | return DagInit::get(V: Dag->getOperator(), VN: Op, Args: Dag->getArgs(), |
| 1537 | ArgNames: Dag->getArgNames()); |
| 1538 | break; |
| 1539 | } |
| 1540 | case ADD: |
| 1541 | case SUB: |
| 1542 | case MUL: |
| 1543 | case DIV: |
| 1544 | case AND: |
| 1545 | case OR: |
| 1546 | case XOR: |
| 1547 | case SHL: |
| 1548 | case SRA: |
| 1549 | case SRL: { |
| 1550 | const auto *LHSi = dyn_cast_or_null<IntInit>( |
| 1551 | Val: LHS->convertInitializerTo(Ty: IntRecTy::get(RK&: getRecordKeeper()))); |
| 1552 | const auto *RHSi = dyn_cast_or_null<IntInit>( |
| 1553 | Val: RHS->convertInitializerTo(Ty: IntRecTy::get(RK&: getRecordKeeper()))); |
| 1554 | if (LHSi && RHSi) { |
| 1555 | int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); |
| 1556 | int64_t Result; |
| 1557 | switch (getOpcode()) { |
| 1558 | default: llvm_unreachable("Bad opcode!" ); |
| 1559 | case ADD: Result = LHSv + RHSv; break; |
| 1560 | case SUB: Result = LHSv - RHSv; break; |
| 1561 | case MUL: Result = LHSv * RHSv; break; |
| 1562 | case DIV: |
| 1563 | if (RHSv == 0) |
| 1564 | PrintFatalError(ErrorLoc: CurRec->getLoc(), |
| 1565 | Msg: "Illegal operation: division by zero" ); |
| 1566 | else if (LHSv == INT64_MIN && RHSv == -1) |
| 1567 | PrintFatalError(ErrorLoc: CurRec->getLoc(), |
| 1568 | Msg: "Illegal operation: INT64_MIN / -1" ); |
| 1569 | else |
| 1570 | Result = LHSv / RHSv; |
| 1571 | break; |
| 1572 | case AND: Result = LHSv & RHSv; break; |
| 1573 | case OR: Result = LHSv | RHSv; break; |
| 1574 | case XOR: Result = LHSv ^ RHSv; break; |
| 1575 | case SHL: |
| 1576 | if (RHSv < 0 || RHSv >= 64) |
| 1577 | PrintFatalError(ErrorLoc: CurRec->getLoc(), |
| 1578 | Msg: "Illegal operation: out of bounds shift" ); |
| 1579 | Result = (uint64_t)LHSv << (uint64_t)RHSv; |
| 1580 | break; |
| 1581 | case SRA: |
| 1582 | if (RHSv < 0 || RHSv >= 64) |
| 1583 | PrintFatalError(ErrorLoc: CurRec->getLoc(), |
| 1584 | Msg: "Illegal operation: out of bounds shift" ); |
| 1585 | Result = LHSv >> (uint64_t)RHSv; |
| 1586 | break; |
| 1587 | case SRL: |
| 1588 | if (RHSv < 0 || RHSv >= 64) |
| 1589 | PrintFatalError(ErrorLoc: CurRec->getLoc(), |
| 1590 | Msg: "Illegal operation: out of bounds shift" ); |
| 1591 | Result = (uint64_t)LHSv >> (uint64_t)RHSv; |
| 1592 | break; |
| 1593 | } |
| 1594 | return IntInit::get(RK&: getRecordKeeper(), V: Result); |
| 1595 | } |
| 1596 | break; |
| 1597 | } |
| 1598 | } |
| 1599 | unresolved: |
| 1600 | return this; |
| 1601 | } |
| 1602 | |
| 1603 | const Init *BinOpInit::resolveReferences(Resolver &R) const { |
| 1604 | const Init *NewLHS = LHS->resolveReferences(R); |
| 1605 | |
| 1606 | unsigned Opc = getOpcode(); |
| 1607 | if (Opc == AND || Opc == OR) { |
| 1608 | // Short-circuit. Regardless whether this is a logical or bitwise |
| 1609 | // AND/OR. |
| 1610 | // Ideally we could also short-circuit `!or(true, ...)`, but it's |
| 1611 | // difficult to do it right without knowing if rest of the operands |
| 1612 | // are all `bit` or not. Therefore, we're only implementing a relatively |
| 1613 | // limited version of short-circuit against all ones (`true` is casted |
| 1614 | // to 1 rather than all ones before we evaluate `!or`). |
| 1615 | if (const auto *LHSi = dyn_cast_or_null<IntInit>( |
| 1616 | Val: NewLHS->convertInitializerTo(Ty: IntRecTy::get(RK&: getRecordKeeper())))) { |
| 1617 | if ((Opc == AND && !LHSi->getValue()) || |
| 1618 | (Opc == OR && LHSi->getValue() == -1)) |
| 1619 | return LHSi; |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | const Init *NewRHS = RHS->resolveReferences(R); |
| 1624 | |
| 1625 | if (LHS != NewLHS || RHS != NewRHS) |
| 1626 | return (BinOpInit::get(Opc: getOpcode(), LHS: NewLHS, RHS: NewRHS, Type: getType())) |
| 1627 | ->Fold(CurRec: R.getCurrentRecord()); |
| 1628 | return this; |
| 1629 | } |
| 1630 | |
| 1631 | std::string BinOpInit::getAsString() const { |
| 1632 | std::string Result; |
| 1633 | switch (getOpcode()) { |
| 1634 | case LISTELEM: |
| 1635 | case LISTSLICE: |
| 1636 | return LHS->getAsString() + "[" + RHS->getAsString() + "]" ; |
| 1637 | case RANGEC: |
| 1638 | return LHS->getAsString() + "..." + RHS->getAsString(); |
| 1639 | case CONCAT: Result = "!con" ; break; |
| 1640 | case MATCH: |
| 1641 | Result = "!match" ; |
| 1642 | break; |
| 1643 | case ADD: Result = "!add" ; break; |
| 1644 | case SUB: Result = "!sub" ; break; |
| 1645 | case MUL: Result = "!mul" ; break; |
| 1646 | case DIV: Result = "!div" ; break; |
| 1647 | case AND: Result = "!and" ; break; |
| 1648 | case OR: Result = "!or" ; break; |
| 1649 | case XOR: Result = "!xor" ; break; |
| 1650 | case SHL: Result = "!shl" ; break; |
| 1651 | case SRA: Result = "!sra" ; break; |
| 1652 | case SRL: Result = "!srl" ; break; |
| 1653 | case EQ: Result = "!eq" ; break; |
| 1654 | case NE: Result = "!ne" ; break; |
| 1655 | case LE: Result = "!le" ; break; |
| 1656 | case LT: Result = "!lt" ; break; |
| 1657 | case GE: Result = "!ge" ; break; |
| 1658 | case GT: Result = "!gt" ; break; |
| 1659 | case LISTCONCAT: Result = "!listconcat" ; break; |
| 1660 | case LISTSPLAT: Result = "!listsplat" ; break; |
| 1661 | case LISTREMOVE: |
| 1662 | Result = "!listremove" ; |
| 1663 | break; |
| 1664 | case STRCONCAT: Result = "!strconcat" ; break; |
| 1665 | case INTERLEAVE: Result = "!interleave" ; break; |
| 1666 | case SETDAGOP: Result = "!setdagop" ; break; |
| 1667 | case SETDAGOPNAME: |
| 1668 | Result = "!setdagopname" ; |
| 1669 | break; |
| 1670 | case GETDAGARG: |
| 1671 | Result = "!getdagarg<" + getType()->getAsString() + ">" ; |
| 1672 | break; |
| 1673 | case GETDAGNAME: |
| 1674 | Result = "!getdagname" ; |
| 1675 | break; |
| 1676 | } |
| 1677 | return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")" ; |
| 1678 | } |
| 1679 | |
| 1680 | static void ProfileTernOpInit(FoldingSetNodeID &ID, unsigned Opcode, |
| 1681 | const Init *LHS, const Init *MHS, const Init *RHS, |
| 1682 | const RecTy *Type) { |
| 1683 | ID.AddInteger(I: Opcode); |
| 1684 | ID.AddPointer(Ptr: LHS); |
| 1685 | ID.AddPointer(Ptr: MHS); |
| 1686 | ID.AddPointer(Ptr: RHS); |
| 1687 | ID.AddPointer(Ptr: Type); |
| 1688 | } |
| 1689 | |
| 1690 | const TernOpInit *TernOpInit::get(TernaryOp Opc, const Init *LHS, |
| 1691 | const Init *MHS, const Init *RHS, |
| 1692 | const RecTy *Type) { |
| 1693 | FoldingSetNodeID ID; |
| 1694 | ProfileTernOpInit(ID, Opcode: Opc, LHS, MHS, RHS, Type); |
| 1695 | |
| 1696 | detail::RecordKeeperImpl &RK = LHS->getRecordKeeper().getImpl(); |
| 1697 | void *IP = nullptr; |
| 1698 | if (TernOpInit *I = RK.TheTernOpInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 1699 | return I; |
| 1700 | |
| 1701 | TernOpInit *I = new (RK.Allocator) TernOpInit(Opc, LHS, MHS, RHS, Type); |
| 1702 | RK.TheTernOpInitPool.InsertNode(N: I, InsertPos: IP); |
| 1703 | return I; |
| 1704 | } |
| 1705 | |
| 1706 | void TernOpInit::Profile(FoldingSetNodeID &ID) const { |
| 1707 | ProfileTernOpInit(ID, Opcode: getOpcode(), LHS: getLHS(), MHS: getMHS(), RHS: getRHS(), Type: getType()); |
| 1708 | } |
| 1709 | |
| 1710 | static const Init *ItemApply(const Init *LHS, const Init *MHSe, const Init *RHS, |
| 1711 | const Record *CurRec) { |
| 1712 | MapResolver R(CurRec); |
| 1713 | R.set(Key: LHS, Value: MHSe); |
| 1714 | return RHS->resolveReferences(R); |
| 1715 | } |
| 1716 | |
| 1717 | static const Init *ForeachDagApply(const Init *LHS, const DagInit *MHSd, |
| 1718 | const Init *RHS, const Record *CurRec) { |
| 1719 | bool Change = false; |
| 1720 | const Init *Val = ItemApply(LHS, MHSe: MHSd->getOperator(), RHS, CurRec); |
| 1721 | if (Val != MHSd->getOperator()) |
| 1722 | Change = true; |
| 1723 | |
| 1724 | SmallVector<std::pair<const Init *, const StringInit *>, 8> NewArgs; |
| 1725 | for (auto [Arg, ArgName] : MHSd->getArgAndNames()) { |
| 1726 | const Init *NewArg; |
| 1727 | |
| 1728 | if (const auto *Argd = dyn_cast<DagInit>(Val: Arg)) |
| 1729 | NewArg = ForeachDagApply(LHS, MHSd: Argd, RHS, CurRec); |
| 1730 | else |
| 1731 | NewArg = ItemApply(LHS, MHSe: Arg, RHS, CurRec); |
| 1732 | |
| 1733 | NewArgs.emplace_back(Args&: NewArg, Args&: ArgName); |
| 1734 | if (Arg != NewArg) |
| 1735 | Change = true; |
| 1736 | } |
| 1737 | |
| 1738 | if (Change) |
| 1739 | return DagInit::get(V: Val, VN: MHSd->getName(), ArgAndNames: NewArgs); |
| 1740 | return MHSd; |
| 1741 | } |
| 1742 | |
| 1743 | // Applies RHS to all elements of MHS, using LHS as a temp variable. |
| 1744 | static const Init *ForeachHelper(const Init *LHS, const Init *MHS, |
| 1745 | const Init *RHS, const RecTy *Type, |
| 1746 | const Record *CurRec) { |
| 1747 | if (const auto *MHSd = dyn_cast<DagInit>(Val: MHS)) |
| 1748 | return ForeachDagApply(LHS, MHSd, RHS, CurRec); |
| 1749 | |
| 1750 | if (const auto *MHSl = dyn_cast<ListInit>(Val: MHS)) { |
| 1751 | SmallVector<const Init *, 8> NewList(MHSl->begin(), MHSl->end()); |
| 1752 | |
| 1753 | for (const Init *&Item : NewList) { |
| 1754 | const Init *NewItem = ItemApply(LHS, MHSe: Item, RHS, CurRec); |
| 1755 | if (NewItem != Item) |
| 1756 | Item = NewItem; |
| 1757 | } |
| 1758 | return ListInit::get(Elements: NewList, EltTy: cast<ListRecTy>(Val: Type)->getElementType()); |
| 1759 | } |
| 1760 | |
| 1761 | return nullptr; |
| 1762 | } |
| 1763 | |
| 1764 | // Evaluates RHS for all elements of MHS, using LHS as a temp variable. |
| 1765 | // Creates a new list with the elements that evaluated to true. |
| 1766 | static const Init *FilterHelper(const Init *LHS, const Init *MHS, |
| 1767 | const Init *RHS, const RecTy *Type, |
| 1768 | const Record *CurRec) { |
| 1769 | if (const auto *MHSl = dyn_cast<ListInit>(Val: MHS)) { |
| 1770 | SmallVector<const Init *, 8> NewList; |
| 1771 | |
| 1772 | for (const Init *Item : MHSl->getElements()) { |
| 1773 | const Init *Include = ItemApply(LHS, MHSe: Item, RHS, CurRec); |
| 1774 | if (!Include) |
| 1775 | return nullptr; |
| 1776 | if (const auto *IncludeInt = |
| 1777 | dyn_cast_or_null<IntInit>(Val: Include->convertInitializerTo( |
| 1778 | Ty: IntRecTy::get(RK&: LHS->getRecordKeeper())))) { |
| 1779 | if (IncludeInt->getValue()) |
| 1780 | NewList.push_back(Elt: Item); |
| 1781 | } else { |
| 1782 | return nullptr; |
| 1783 | } |
| 1784 | } |
| 1785 | return ListInit::get(Elements: NewList, EltTy: cast<ListRecTy>(Val: Type)->getElementType()); |
| 1786 | } |
| 1787 | |
| 1788 | return nullptr; |
| 1789 | } |
| 1790 | |
| 1791 | const Init *TernOpInit::Fold(const Record *CurRec) const { |
| 1792 | RecordKeeper &RK = getRecordKeeper(); |
| 1793 | switch (getOpcode()) { |
| 1794 | case SUBST: { |
| 1795 | const auto *LHSd = dyn_cast<DefInit>(Val: LHS); |
| 1796 | const auto *LHSv = dyn_cast<VarInit>(Val: LHS); |
| 1797 | const auto *LHSs = dyn_cast<StringInit>(Val: LHS); |
| 1798 | |
| 1799 | const auto *MHSd = dyn_cast<DefInit>(Val: MHS); |
| 1800 | const auto *MHSv = dyn_cast<VarInit>(Val: MHS); |
| 1801 | const auto *MHSs = dyn_cast<StringInit>(Val: MHS); |
| 1802 | |
| 1803 | const auto *RHSd = dyn_cast<DefInit>(Val: RHS); |
| 1804 | const auto *RHSv = dyn_cast<VarInit>(Val: RHS); |
| 1805 | const auto *RHSs = dyn_cast<StringInit>(Val: RHS); |
| 1806 | |
| 1807 | if (LHSd && MHSd && RHSd) { |
| 1808 | const Record *Val = RHSd->getDef(); |
| 1809 | if (LHSd->getAsString() == RHSd->getAsString()) |
| 1810 | Val = MHSd->getDef(); |
| 1811 | return Val->getDefInit(); |
| 1812 | } |
| 1813 | if (LHSv && MHSv && RHSv) { |
| 1814 | std::string Val = RHSv->getName().str(); |
| 1815 | if (LHSv->getAsString() == RHSv->getAsString()) |
| 1816 | Val = MHSv->getName().str(); |
| 1817 | return VarInit::get(VN: Val, T: getType()); |
| 1818 | } |
| 1819 | if (LHSs && MHSs && RHSs) { |
| 1820 | std::string Val = RHSs->getValue().str(); |
| 1821 | |
| 1822 | std::string::size_type Idx = 0; |
| 1823 | while (true) { |
| 1824 | std::string::size_type Found = Val.find(svt: LHSs->getValue(), pos: Idx); |
| 1825 | if (Found == std::string::npos) |
| 1826 | break; |
| 1827 | Val.replace(pos: Found, n: LHSs->getValue().size(), str: MHSs->getValue().str()); |
| 1828 | Idx = Found + MHSs->getValue().size(); |
| 1829 | } |
| 1830 | |
| 1831 | return StringInit::get(RK, V: Val); |
| 1832 | } |
| 1833 | break; |
| 1834 | } |
| 1835 | |
| 1836 | case FOREACH: { |
| 1837 | if (const Init *Result = ForeachHelper(LHS, MHS, RHS, Type: getType(), CurRec)) |
| 1838 | return Result; |
| 1839 | break; |
| 1840 | } |
| 1841 | |
| 1842 | case FILTER: { |
| 1843 | if (const Init *Result = FilterHelper(LHS, MHS, RHS, Type: getType(), CurRec)) |
| 1844 | return Result; |
| 1845 | break; |
| 1846 | } |
| 1847 | |
| 1848 | case IF: { |
| 1849 | if (const auto *LHSi = dyn_cast_or_null<IntInit>( |
| 1850 | Val: LHS->convertInitializerTo(Ty: IntRecTy::get(RK)))) { |
| 1851 | if (LHSi->getValue()) |
| 1852 | return MHS; |
| 1853 | return RHS; |
| 1854 | } |
| 1855 | break; |
| 1856 | } |
| 1857 | |
| 1858 | case DAG: { |
| 1859 | const auto *MHSl = dyn_cast<ListInit>(Val: MHS); |
| 1860 | const auto *RHSl = dyn_cast<ListInit>(Val: RHS); |
| 1861 | bool MHSok = MHSl || isa<UnsetInit>(Val: MHS); |
| 1862 | bool RHSok = RHSl || isa<UnsetInit>(Val: RHS); |
| 1863 | |
| 1864 | if (isa<UnsetInit>(Val: MHS) && isa<UnsetInit>(Val: RHS)) |
| 1865 | break; // Typically prevented by the parser, but might happen with template args |
| 1866 | |
| 1867 | if (MHSok && RHSok && (!MHSl || !RHSl || MHSl->size() == RHSl->size())) { |
| 1868 | SmallVector<std::pair<const Init *, const StringInit *>, 8> Children; |
| 1869 | unsigned Size = MHSl ? MHSl->size() : RHSl->size(); |
| 1870 | for (unsigned i = 0; i != Size; ++i) { |
| 1871 | const Init *Node = MHSl ? MHSl->getElement(Idx: i) : UnsetInit::get(RK); |
| 1872 | const Init *Name = RHSl ? RHSl->getElement(Idx: i) : UnsetInit::get(RK); |
| 1873 | if (!isa<StringInit>(Val: Name) && !isa<UnsetInit>(Val: Name)) |
| 1874 | return this; |
| 1875 | Children.emplace_back(Args&: Node, Args: dyn_cast<StringInit>(Val: Name)); |
| 1876 | } |
| 1877 | return DagInit::get(V: LHS, ArgAndNames: Children); |
| 1878 | } |
| 1879 | break; |
| 1880 | } |
| 1881 | |
| 1882 | case RANGE: { |
| 1883 | const auto *LHSi = dyn_cast<IntInit>(Val: LHS); |
| 1884 | const auto *MHSi = dyn_cast<IntInit>(Val: MHS); |
| 1885 | const auto *RHSi = dyn_cast<IntInit>(Val: RHS); |
| 1886 | if (!LHSi || !MHSi || !RHSi) |
| 1887 | break; |
| 1888 | |
| 1889 | auto Start = LHSi->getValue(); |
| 1890 | auto End = MHSi->getValue(); |
| 1891 | auto Step = RHSi->getValue(); |
| 1892 | if (Step == 0) |
| 1893 | PrintError(ErrorLoc: CurRec->getLoc(), Msg: "Step of !range can't be 0" ); |
| 1894 | |
| 1895 | SmallVector<const Init *, 8> Args; |
| 1896 | if (Start < End && Step > 0) { |
| 1897 | Args.reserve(N: (End - Start) / Step); |
| 1898 | for (auto I = Start; I < End; I += Step) |
| 1899 | Args.push_back(Elt: IntInit::get(RK&: getRecordKeeper(), V: I)); |
| 1900 | } else if (Start > End && Step < 0) { |
| 1901 | Args.reserve(N: (Start - End) / -Step); |
| 1902 | for (auto I = Start; I > End; I += Step) |
| 1903 | Args.push_back(Elt: IntInit::get(RK&: getRecordKeeper(), V: I)); |
| 1904 | } else { |
| 1905 | // Empty set |
| 1906 | } |
| 1907 | return ListInit::get(Elements: Args, EltTy: LHSi->getType()); |
| 1908 | } |
| 1909 | |
| 1910 | case SUBSTR: { |
| 1911 | const auto *LHSs = dyn_cast<StringInit>(Val: LHS); |
| 1912 | const auto *MHSi = dyn_cast<IntInit>(Val: MHS); |
| 1913 | const auto *RHSi = dyn_cast<IntInit>(Val: RHS); |
| 1914 | if (LHSs && MHSi && RHSi) { |
| 1915 | int64_t StringSize = LHSs->getValue().size(); |
| 1916 | int64_t Start = MHSi->getValue(); |
| 1917 | int64_t Length = RHSi->getValue(); |
| 1918 | if (Start < 0 || Start > StringSize) |
| 1919 | PrintError(ErrorLoc: CurRec->getLoc(), |
| 1920 | Msg: Twine("!substr start position is out of range 0..." ) + |
| 1921 | std::to_string(val: StringSize) + ": " + |
| 1922 | std::to_string(val: Start)); |
| 1923 | if (Length < 0) |
| 1924 | PrintError(ErrorLoc: CurRec->getLoc(), Msg: "!substr length must be nonnegative" ); |
| 1925 | return StringInit::get(RK, V: LHSs->getValue().substr(Start, N: Length), |
| 1926 | Fmt: LHSs->getFormat()); |
| 1927 | } |
| 1928 | break; |
| 1929 | } |
| 1930 | |
| 1931 | case FIND: { |
| 1932 | const auto *LHSs = dyn_cast<StringInit>(Val: LHS); |
| 1933 | const auto *MHSs = dyn_cast<StringInit>(Val: MHS); |
| 1934 | const auto *RHSi = dyn_cast<IntInit>(Val: RHS); |
| 1935 | if (LHSs && MHSs && RHSi) { |
| 1936 | int64_t SourceSize = LHSs->getValue().size(); |
| 1937 | int64_t Start = RHSi->getValue(); |
| 1938 | if (Start < 0 || Start > SourceSize) |
| 1939 | PrintError(ErrorLoc: CurRec->getLoc(), |
| 1940 | Msg: Twine("!find start position is out of range 0..." ) + |
| 1941 | std::to_string(val: SourceSize) + ": " + |
| 1942 | std::to_string(val: Start)); |
| 1943 | auto I = LHSs->getValue().find(Str: MHSs->getValue(), From: Start); |
| 1944 | if (I == std::string::npos) |
| 1945 | return IntInit::get(RK, V: -1); |
| 1946 | return IntInit::get(RK, V: I); |
| 1947 | } |
| 1948 | break; |
| 1949 | } |
| 1950 | |
| 1951 | case SETDAGARG: { |
| 1952 | const auto *Dag = dyn_cast<DagInit>(Val: LHS); |
| 1953 | if (Dag && isa<IntInit, StringInit>(Val: MHS)) { |
| 1954 | std::string Error; |
| 1955 | auto ArgNo = getDagArgNoByKey(Dag, Key: MHS, Error); |
| 1956 | if (!ArgNo) |
| 1957 | PrintFatalError(ErrorLoc: CurRec->getLoc(), Msg: "!setdagarg " + Error); |
| 1958 | |
| 1959 | assert(*ArgNo < Dag->getNumArgs()); |
| 1960 | |
| 1961 | SmallVector<const Init *, 8> Args(Dag->getArgs()); |
| 1962 | Args[*ArgNo] = RHS; |
| 1963 | return DagInit::get(V: Dag->getOperator(), VN: Dag->getName(), Args, |
| 1964 | ArgNames: Dag->getArgNames()); |
| 1965 | } |
| 1966 | break; |
| 1967 | } |
| 1968 | |
| 1969 | case SETDAGNAME: { |
| 1970 | const auto *Dag = dyn_cast<DagInit>(Val: LHS); |
| 1971 | if (Dag && isa<IntInit, StringInit>(Val: MHS)) { |
| 1972 | std::string Error; |
| 1973 | auto ArgNo = getDagArgNoByKey(Dag, Key: MHS, Error); |
| 1974 | if (!ArgNo) |
| 1975 | PrintFatalError(ErrorLoc: CurRec->getLoc(), Msg: "!setdagname " + Error); |
| 1976 | |
| 1977 | assert(*ArgNo < Dag->getNumArgs()); |
| 1978 | |
| 1979 | SmallVector<const StringInit *, 8> Names(Dag->getArgNames()); |
| 1980 | Names[*ArgNo] = dyn_cast<StringInit>(Val: RHS); |
| 1981 | return DagInit::get(V: Dag->getOperator(), VN: Dag->getName(), Args: Dag->getArgs(), |
| 1982 | ArgNames: Names); |
| 1983 | } |
| 1984 | break; |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | return this; |
| 1989 | } |
| 1990 | |
| 1991 | const Init *TernOpInit::resolveReferences(Resolver &R) const { |
| 1992 | const Init *lhs = LHS->resolveReferences(R); |
| 1993 | |
| 1994 | if (getOpcode() == IF && lhs != LHS) { |
| 1995 | if (const auto *Value = dyn_cast_or_null<IntInit>( |
| 1996 | Val: lhs->convertInitializerTo(Ty: IntRecTy::get(RK&: getRecordKeeper())))) { |
| 1997 | // Short-circuit |
| 1998 | if (Value->getValue()) |
| 1999 | return MHS->resolveReferences(R); |
| 2000 | return RHS->resolveReferences(R); |
| 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | const Init *mhs = MHS->resolveReferences(R); |
| 2005 | const Init *rhs; |
| 2006 | |
| 2007 | if (getOpcode() == FOREACH || getOpcode() == FILTER) { |
| 2008 | ShadowResolver SR(R); |
| 2009 | SR.addShadow(Key: lhs); |
| 2010 | rhs = RHS->resolveReferences(R&: SR); |
| 2011 | } else { |
| 2012 | rhs = RHS->resolveReferences(R); |
| 2013 | } |
| 2014 | |
| 2015 | if (LHS != lhs || MHS != mhs || RHS != rhs) |
| 2016 | return (TernOpInit::get(Opc: getOpcode(), LHS: lhs, MHS: mhs, RHS: rhs, Type: getType())) |
| 2017 | ->Fold(CurRec: R.getCurrentRecord()); |
| 2018 | return this; |
| 2019 | } |
| 2020 | |
| 2021 | std::string TernOpInit::getAsString() const { |
| 2022 | std::string Result; |
| 2023 | bool UnquotedLHS = false; |
| 2024 | switch (getOpcode()) { |
| 2025 | case DAG: Result = "!dag" ; break; |
| 2026 | case FILTER: Result = "!filter" ; UnquotedLHS = true; break; |
| 2027 | case FOREACH: Result = "!foreach" ; UnquotedLHS = true; break; |
| 2028 | case IF: Result = "!if" ; break; |
| 2029 | case RANGE: |
| 2030 | Result = "!range" ; |
| 2031 | break; |
| 2032 | case SUBST: Result = "!subst" ; break; |
| 2033 | case SUBSTR: Result = "!substr" ; break; |
| 2034 | case FIND: Result = "!find" ; break; |
| 2035 | case SETDAGARG: |
| 2036 | Result = "!setdagarg" ; |
| 2037 | break; |
| 2038 | case SETDAGNAME: |
| 2039 | Result = "!setdagname" ; |
| 2040 | break; |
| 2041 | } |
| 2042 | return (Result + "(" + |
| 2043 | (UnquotedLHS ? LHS->getAsUnquotedString() : LHS->getAsString()) + |
| 2044 | ", " + MHS->getAsString() + ", " + RHS->getAsString() + ")" ); |
| 2045 | } |
| 2046 | |
| 2047 | static void ProfileFoldOpInit(FoldingSetNodeID &ID, const Init *Start, |
| 2048 | const Init *List, const Init *A, const Init *B, |
| 2049 | const Init *Expr, const RecTy *Type) { |
| 2050 | ID.AddPointer(Ptr: Start); |
| 2051 | ID.AddPointer(Ptr: List); |
| 2052 | ID.AddPointer(Ptr: A); |
| 2053 | ID.AddPointer(Ptr: B); |
| 2054 | ID.AddPointer(Ptr: Expr); |
| 2055 | ID.AddPointer(Ptr: Type); |
| 2056 | } |
| 2057 | |
| 2058 | const FoldOpInit *FoldOpInit::get(const Init *Start, const Init *List, |
| 2059 | const Init *A, const Init *B, |
| 2060 | const Init *Expr, const RecTy *Type) { |
| 2061 | FoldingSetNodeID ID; |
| 2062 | ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type); |
| 2063 | |
| 2064 | detail::RecordKeeperImpl &RK = Start->getRecordKeeper().getImpl(); |
| 2065 | void *IP = nullptr; |
| 2066 | if (const FoldOpInit *I = RK.TheFoldOpInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 2067 | return I; |
| 2068 | |
| 2069 | FoldOpInit *I = new (RK.Allocator) FoldOpInit(Start, List, A, B, Expr, Type); |
| 2070 | RK.TheFoldOpInitPool.InsertNode(N: I, InsertPos: IP); |
| 2071 | return I; |
| 2072 | } |
| 2073 | |
| 2074 | void FoldOpInit::Profile(FoldingSetNodeID &ID) const { |
| 2075 | ProfileFoldOpInit(ID, Start, List, A, B, Expr, Type: getType()); |
| 2076 | } |
| 2077 | |
| 2078 | const Init *FoldOpInit::Fold(const Record *CurRec) const { |
| 2079 | if (const auto *LI = dyn_cast<ListInit>(Val: List)) { |
| 2080 | const Init *Accum = Start; |
| 2081 | for (const Init *Elt : *LI) { |
| 2082 | MapResolver R(CurRec); |
| 2083 | R.set(Key: A, Value: Accum); |
| 2084 | R.set(Key: B, Value: Elt); |
| 2085 | Accum = Expr->resolveReferences(R); |
| 2086 | } |
| 2087 | return Accum; |
| 2088 | } |
| 2089 | return this; |
| 2090 | } |
| 2091 | |
| 2092 | const Init *FoldOpInit::resolveReferences(Resolver &R) const { |
| 2093 | const Init *NewStart = Start->resolveReferences(R); |
| 2094 | const Init *NewList = List->resolveReferences(R); |
| 2095 | ShadowResolver SR(R); |
| 2096 | SR.addShadow(Key: A); |
| 2097 | SR.addShadow(Key: B); |
| 2098 | const Init *NewExpr = Expr->resolveReferences(R&: SR); |
| 2099 | |
| 2100 | if (Start == NewStart && List == NewList && Expr == NewExpr) |
| 2101 | return this; |
| 2102 | |
| 2103 | return get(Start: NewStart, List: NewList, A, B, Expr: NewExpr, Type: getType()) |
| 2104 | ->Fold(CurRec: R.getCurrentRecord()); |
| 2105 | } |
| 2106 | |
| 2107 | const Init *FoldOpInit::getBit(unsigned Bit) const { |
| 2108 | return VarBitInit::get(T: this, B: Bit); |
| 2109 | } |
| 2110 | |
| 2111 | std::string FoldOpInit::getAsString() const { |
| 2112 | return (Twine("!foldl(" ) + Start->getAsString() + ", " + List->getAsString() + |
| 2113 | ", " + A->getAsUnquotedString() + ", " + B->getAsUnquotedString() + |
| 2114 | ", " + Expr->getAsString() + ")" ) |
| 2115 | .str(); |
| 2116 | } |
| 2117 | |
| 2118 | static void ProfileIsAOpInit(FoldingSetNodeID &ID, const RecTy *CheckType, |
| 2119 | const Init *Expr) { |
| 2120 | ID.AddPointer(Ptr: CheckType); |
| 2121 | ID.AddPointer(Ptr: Expr); |
| 2122 | } |
| 2123 | |
| 2124 | const IsAOpInit *IsAOpInit::get(const RecTy *CheckType, const Init *Expr) { |
| 2125 | |
| 2126 | FoldingSetNodeID ID; |
| 2127 | ProfileIsAOpInit(ID, CheckType, Expr); |
| 2128 | |
| 2129 | detail::RecordKeeperImpl &RK = Expr->getRecordKeeper().getImpl(); |
| 2130 | void *IP = nullptr; |
| 2131 | if (const IsAOpInit *I = RK.TheIsAOpInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 2132 | return I; |
| 2133 | |
| 2134 | IsAOpInit *I = new (RK.Allocator) IsAOpInit(CheckType, Expr); |
| 2135 | RK.TheIsAOpInitPool.InsertNode(N: I, InsertPos: IP); |
| 2136 | return I; |
| 2137 | } |
| 2138 | |
| 2139 | void IsAOpInit::Profile(FoldingSetNodeID &ID) const { |
| 2140 | ProfileIsAOpInit(ID, CheckType, Expr); |
| 2141 | } |
| 2142 | |
| 2143 | const Init *IsAOpInit::Fold() const { |
| 2144 | if (const auto *TI = dyn_cast<TypedInit>(Val: Expr)) { |
| 2145 | // Is the expression type known to be (a subclass of) the desired type? |
| 2146 | if (TI->getType()->typeIsConvertibleTo(RHS: CheckType)) |
| 2147 | return IntInit::get(RK&: getRecordKeeper(), V: 1); |
| 2148 | |
| 2149 | if (isa<RecordRecTy>(Val: CheckType)) { |
| 2150 | // If the target type is not a subclass of the expression type once the |
| 2151 | // expression has been made concrete, or if the expression has fully |
| 2152 | // resolved to a record, we know that it can't be of the required type. |
| 2153 | if ((!CheckType->typeIsConvertibleTo(RHS: TI->getType()) && |
| 2154 | Expr->isConcrete()) || |
| 2155 | isa<DefInit>(Val: Expr)) |
| 2156 | return IntInit::get(RK&: getRecordKeeper(), V: 0); |
| 2157 | } else { |
| 2158 | // We treat non-record types as not castable. |
| 2159 | return IntInit::get(RK&: getRecordKeeper(), V: 0); |
| 2160 | } |
| 2161 | } |
| 2162 | return this; |
| 2163 | } |
| 2164 | |
| 2165 | const Init *IsAOpInit::resolveReferences(Resolver &R) const { |
| 2166 | const Init *NewExpr = Expr->resolveReferences(R); |
| 2167 | if (Expr != NewExpr) |
| 2168 | return get(CheckType, Expr: NewExpr)->Fold(); |
| 2169 | return this; |
| 2170 | } |
| 2171 | |
| 2172 | const Init *IsAOpInit::getBit(unsigned Bit) const { |
| 2173 | return VarBitInit::get(T: this, B: Bit); |
| 2174 | } |
| 2175 | |
| 2176 | std::string IsAOpInit::getAsString() const { |
| 2177 | return (Twine("!isa<" ) + CheckType->getAsString() + ">(" + |
| 2178 | Expr->getAsString() + ")" ) |
| 2179 | .str(); |
| 2180 | } |
| 2181 | |
| 2182 | static void ProfileExistsOpInit(FoldingSetNodeID &ID, const RecTy *CheckType, |
| 2183 | const Init *Expr) { |
| 2184 | ID.AddPointer(Ptr: CheckType); |
| 2185 | ID.AddPointer(Ptr: Expr); |
| 2186 | } |
| 2187 | |
| 2188 | const ExistsOpInit *ExistsOpInit::get(const RecTy *CheckType, |
| 2189 | const Init *Expr) { |
| 2190 | FoldingSetNodeID ID; |
| 2191 | ProfileExistsOpInit(ID, CheckType, Expr); |
| 2192 | |
| 2193 | detail::RecordKeeperImpl &RK = Expr->getRecordKeeper().getImpl(); |
| 2194 | void *IP = nullptr; |
| 2195 | if (const ExistsOpInit *I = |
| 2196 | RK.TheExistsOpInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 2197 | return I; |
| 2198 | |
| 2199 | ExistsOpInit *I = new (RK.Allocator) ExistsOpInit(CheckType, Expr); |
| 2200 | RK.TheExistsOpInitPool.InsertNode(N: I, InsertPos: IP); |
| 2201 | return I; |
| 2202 | } |
| 2203 | |
| 2204 | void ExistsOpInit::Profile(FoldingSetNodeID &ID) const { |
| 2205 | ProfileExistsOpInit(ID, CheckType, Expr); |
| 2206 | } |
| 2207 | |
| 2208 | const Init *ExistsOpInit::Fold(const Record *CurRec, bool IsFinal) const { |
| 2209 | if (const auto *Name = dyn_cast<StringInit>(Val: Expr)) { |
| 2210 | // Look up all defined records to see if we can find one. |
| 2211 | const Record *D = CheckType->getRecordKeeper().getDef(Name: Name->getValue()); |
| 2212 | if (D) { |
| 2213 | // Check if types are compatible. |
| 2214 | return IntInit::get(RK&: getRecordKeeper(), |
| 2215 | V: D->getDefInit()->getType()->typeIsA(RHS: CheckType)); |
| 2216 | } |
| 2217 | |
| 2218 | if (CurRec) { |
| 2219 | // Self-references are allowed, but their resolution is delayed until |
| 2220 | // the final resolve to ensure that we get the correct type for them. |
| 2221 | auto *Anonymous = dyn_cast<AnonymousNameInit>(Val: CurRec->getNameInit()); |
| 2222 | if (Name == CurRec->getNameInit() || |
| 2223 | (Anonymous && Name == Anonymous->getNameInit())) { |
| 2224 | if (!IsFinal) |
| 2225 | return this; |
| 2226 | |
| 2227 | // No doubt that there exists a record, so we should check if types are |
| 2228 | // compatible. |
| 2229 | return IntInit::get(RK&: getRecordKeeper(), |
| 2230 | V: CurRec->getType()->typeIsA(RHS: CheckType)); |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | if (IsFinal) |
| 2235 | return IntInit::get(RK&: getRecordKeeper(), V: 0); |
| 2236 | } |
| 2237 | return this; |
| 2238 | } |
| 2239 | |
| 2240 | const Init *ExistsOpInit::resolveReferences(Resolver &R) const { |
| 2241 | const Init *NewExpr = Expr->resolveReferences(R); |
| 2242 | if (Expr != NewExpr || R.isFinal()) |
| 2243 | return get(CheckType, Expr: NewExpr)->Fold(CurRec: R.getCurrentRecord(), IsFinal: R.isFinal()); |
| 2244 | return this; |
| 2245 | } |
| 2246 | |
| 2247 | const Init *ExistsOpInit::getBit(unsigned Bit) const { |
| 2248 | return VarBitInit::get(T: this, B: Bit); |
| 2249 | } |
| 2250 | |
| 2251 | std::string ExistsOpInit::getAsString() const { |
| 2252 | return (Twine("!exists<" ) + CheckType->getAsString() + ">(" + |
| 2253 | Expr->getAsString() + ")" ) |
| 2254 | .str(); |
| 2255 | } |
| 2256 | |
| 2257 | static void ProfileInstancesOpInit(FoldingSetNodeID &ID, const RecTy *Type, |
| 2258 | const Init *Regex) { |
| 2259 | ID.AddPointer(Ptr: Type); |
| 2260 | ID.AddPointer(Ptr: Regex); |
| 2261 | } |
| 2262 | |
| 2263 | const InstancesOpInit *InstancesOpInit::get(const RecTy *Type, |
| 2264 | const Init *Regex) { |
| 2265 | FoldingSetNodeID ID; |
| 2266 | ProfileInstancesOpInit(ID, Type, Regex); |
| 2267 | |
| 2268 | detail::RecordKeeperImpl &RK = Regex->getRecordKeeper().getImpl(); |
| 2269 | void *IP = nullptr; |
| 2270 | if (const InstancesOpInit *I = |
| 2271 | RK.TheInstancesOpInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 2272 | return I; |
| 2273 | |
| 2274 | InstancesOpInit *I = new (RK.Allocator) InstancesOpInit(Type, Regex); |
| 2275 | RK.TheInstancesOpInitPool.InsertNode(N: I, InsertPos: IP); |
| 2276 | return I; |
| 2277 | } |
| 2278 | |
| 2279 | void InstancesOpInit::Profile(FoldingSetNodeID &ID) const { |
| 2280 | ProfileInstancesOpInit(ID, Type, Regex); |
| 2281 | } |
| 2282 | |
| 2283 | const Init *InstancesOpInit::Fold(const Record *CurRec, bool IsFinal) const { |
| 2284 | if (CurRec && !IsFinal) |
| 2285 | return this; |
| 2286 | |
| 2287 | const auto *RegexInit = dyn_cast<StringInit>(Val: Regex); |
| 2288 | if (!RegexInit) |
| 2289 | return this; |
| 2290 | |
| 2291 | StringRef RegexStr = RegexInit->getValue(); |
| 2292 | llvm::Regex Matcher(RegexStr); |
| 2293 | if (!Matcher.isValid()) |
| 2294 | PrintFatalError(Msg: Twine("invalid regex '" ) + RegexStr + Twine("'" )); |
| 2295 | |
| 2296 | const RecordKeeper &RK = Type->getRecordKeeper(); |
| 2297 | SmallVector<Init *, 8> Selected; |
| 2298 | for (auto &Def : RK.getAllDerivedDefinitionsIfDefined(ClassName: Type->getAsString())) |
| 2299 | if (Matcher.match(String: Def->getName())) |
| 2300 | Selected.push_back(Elt: Def->getDefInit()); |
| 2301 | |
| 2302 | return ListInit::get(Elements: Selected, EltTy: Type); |
| 2303 | } |
| 2304 | |
| 2305 | const Init *InstancesOpInit::resolveReferences(Resolver &R) const { |
| 2306 | const Init *NewRegex = Regex->resolveReferences(R); |
| 2307 | if (Regex != NewRegex || R.isFinal()) |
| 2308 | return get(Type, Regex: NewRegex)->Fold(CurRec: R.getCurrentRecord(), IsFinal: R.isFinal()); |
| 2309 | return this; |
| 2310 | } |
| 2311 | |
| 2312 | const Init *InstancesOpInit::getBit(unsigned Bit) const { |
| 2313 | return VarBitInit::get(T: this, B: Bit); |
| 2314 | } |
| 2315 | |
| 2316 | std::string InstancesOpInit::getAsString() const { |
| 2317 | return "!instances<" + Type->getAsString() + ">(" + Regex->getAsString() + |
| 2318 | ")" ; |
| 2319 | } |
| 2320 | |
| 2321 | const RecTy *TypedInit::getFieldType(const StringInit *FieldName) const { |
| 2322 | if (const auto *RecordType = dyn_cast<RecordRecTy>(Val: getType())) { |
| 2323 | for (const Record *Rec : RecordType->getClasses()) { |
| 2324 | if (const RecordVal *Field = Rec->getValue(Name: FieldName)) |
| 2325 | return Field->getType(); |
| 2326 | } |
| 2327 | } |
| 2328 | return nullptr; |
| 2329 | } |
| 2330 | |
| 2331 | const Init *TypedInit::convertInitializerTo(const RecTy *Ty) const { |
| 2332 | if (getType() == Ty || getType()->typeIsA(RHS: Ty)) |
| 2333 | return this; |
| 2334 | |
| 2335 | if (isa<BitRecTy>(Val: getType()) && isa<BitsRecTy>(Val: Ty) && |
| 2336 | cast<BitsRecTy>(Val: Ty)->getNumBits() == 1) |
| 2337 | return BitsInit::get(RK&: getRecordKeeper(), Bits: {this}); |
| 2338 | |
| 2339 | return nullptr; |
| 2340 | } |
| 2341 | |
| 2342 | const Init * |
| 2343 | TypedInit::convertInitializerBitRange(ArrayRef<unsigned> Bits) const { |
| 2344 | const auto *T = dyn_cast<BitsRecTy>(Val: getType()); |
| 2345 | if (!T) return nullptr; // Cannot subscript a non-bits variable. |
| 2346 | unsigned NumBits = T->getNumBits(); |
| 2347 | |
| 2348 | SmallVector<const Init *, 16> NewBits; |
| 2349 | NewBits.reserve(N: Bits.size()); |
| 2350 | for (unsigned Bit : Bits) { |
| 2351 | if (Bit >= NumBits) |
| 2352 | return nullptr; |
| 2353 | |
| 2354 | NewBits.push_back(Elt: VarBitInit::get(T: this, B: Bit)); |
| 2355 | } |
| 2356 | return BitsInit::get(RK&: getRecordKeeper(), Bits: NewBits); |
| 2357 | } |
| 2358 | |
| 2359 | const Init *TypedInit::getCastTo(const RecTy *Ty) const { |
| 2360 | // Handle the common case quickly |
| 2361 | if (getType() == Ty || getType()->typeIsA(RHS: Ty)) |
| 2362 | return this; |
| 2363 | |
| 2364 | if (const Init *Converted = convertInitializerTo(Ty)) { |
| 2365 | assert(!isa<TypedInit>(Converted) || |
| 2366 | cast<TypedInit>(Converted)->getType()->typeIsA(Ty)); |
| 2367 | return Converted; |
| 2368 | } |
| 2369 | |
| 2370 | if (!getType()->typeIsConvertibleTo(RHS: Ty)) |
| 2371 | return nullptr; |
| 2372 | |
| 2373 | return UnOpInit::get(Opc: UnOpInit::CAST, LHS: this, Type: Ty)->Fold(CurRec: nullptr); |
| 2374 | } |
| 2375 | |
| 2376 | const VarInit *VarInit::get(StringRef VN, const RecTy *T) { |
| 2377 | const Init *Value = StringInit::get(RK&: T->getRecordKeeper(), V: VN); |
| 2378 | return VarInit::get(VN: Value, T); |
| 2379 | } |
| 2380 | |
| 2381 | const VarInit *VarInit::get(const Init *VN, const RecTy *T) { |
| 2382 | detail::RecordKeeperImpl &RK = T->getRecordKeeper().getImpl(); |
| 2383 | VarInit *&I = RK.TheVarInitPool[{T, VN}]; |
| 2384 | if (!I) |
| 2385 | I = new (RK.Allocator) VarInit(VN, T); |
| 2386 | return I; |
| 2387 | } |
| 2388 | |
| 2389 | StringRef VarInit::getName() const { |
| 2390 | const auto *NameString = cast<StringInit>(Val: getNameInit()); |
| 2391 | return NameString->getValue(); |
| 2392 | } |
| 2393 | |
| 2394 | const Init *VarInit::getBit(unsigned Bit) const { |
| 2395 | if (getType() == BitRecTy::get(RK&: getRecordKeeper())) |
| 2396 | return this; |
| 2397 | return VarBitInit::get(T: this, B: Bit); |
| 2398 | } |
| 2399 | |
| 2400 | const Init *VarInit::resolveReferences(Resolver &R) const { |
| 2401 | if (const Init *Val = R.resolve(VarName)) |
| 2402 | return Val; |
| 2403 | return this; |
| 2404 | } |
| 2405 | |
| 2406 | const VarBitInit *VarBitInit::get(const TypedInit *T, unsigned B) { |
| 2407 | detail::RecordKeeperImpl &RK = T->getRecordKeeper().getImpl(); |
| 2408 | VarBitInit *&I = RK.TheVarBitInitPool[{T, B}]; |
| 2409 | if (!I) |
| 2410 | I = new (RK.Allocator) VarBitInit(T, B); |
| 2411 | return I; |
| 2412 | } |
| 2413 | |
| 2414 | std::string VarBitInit::getAsString() const { |
| 2415 | return TI->getAsString() + "{" + utostr(X: Bit) + "}" ; |
| 2416 | } |
| 2417 | |
| 2418 | const Init *VarBitInit::resolveReferences(Resolver &R) const { |
| 2419 | const Init *I = TI->resolveReferences(R); |
| 2420 | if (TI != I) |
| 2421 | return I->getBit(Bit: getBitNum()); |
| 2422 | |
| 2423 | return this; |
| 2424 | } |
| 2425 | |
| 2426 | DefInit::DefInit(const Record *D) |
| 2427 | : TypedInit(IK_DefInit, D->getType()), Def(D) {} |
| 2428 | |
| 2429 | const Init *DefInit::convertInitializerTo(const RecTy *Ty) const { |
| 2430 | if (auto *RRT = dyn_cast<RecordRecTy>(Val: Ty)) |
| 2431 | if (getType()->typeIsConvertibleTo(RHS: RRT)) |
| 2432 | return this; |
| 2433 | return nullptr; |
| 2434 | } |
| 2435 | |
| 2436 | const RecTy *DefInit::getFieldType(const StringInit *FieldName) const { |
| 2437 | if (const RecordVal *RV = Def->getValue(Name: FieldName)) |
| 2438 | return RV->getType(); |
| 2439 | return nullptr; |
| 2440 | } |
| 2441 | |
| 2442 | std::string DefInit::getAsString() const { return Def->getName().str(); } |
| 2443 | |
| 2444 | static void ProfileVarDefInit(FoldingSetNodeID &ID, const Record *Class, |
| 2445 | ArrayRef<const ArgumentInit *> Args) { |
| 2446 | ID.AddInteger(I: Args.size()); |
| 2447 | ID.AddPointer(Ptr: Class); |
| 2448 | |
| 2449 | for (const Init *I : Args) |
| 2450 | ID.AddPointer(Ptr: I); |
| 2451 | } |
| 2452 | |
| 2453 | VarDefInit::VarDefInit(SMLoc Loc, const Record *Class, |
| 2454 | ArrayRef<const ArgumentInit *> Args) |
| 2455 | : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Loc(Loc), Class(Class), |
| 2456 | NumArgs(Args.size()) { |
| 2457 | llvm::uninitialized_copy(Src&: Args, Dst: getTrailingObjects()); |
| 2458 | } |
| 2459 | |
| 2460 | const VarDefInit *VarDefInit::get(SMLoc Loc, const Record *Class, |
| 2461 | ArrayRef<const ArgumentInit *> Args) { |
| 2462 | FoldingSetNodeID ID; |
| 2463 | ProfileVarDefInit(ID, Class, Args); |
| 2464 | |
| 2465 | detail::RecordKeeperImpl &RK = Class->getRecords().getImpl(); |
| 2466 | void *IP = nullptr; |
| 2467 | if (const VarDefInit *I = RK.TheVarDefInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 2468 | return I; |
| 2469 | |
| 2470 | void *Mem = RK.Allocator.Allocate( |
| 2471 | Size: totalSizeToAlloc<const ArgumentInit *>(Counts: Args.size()), Alignment: alignof(VarDefInit)); |
| 2472 | VarDefInit *I = new (Mem) VarDefInit(Loc, Class, Args); |
| 2473 | RK.TheVarDefInitPool.InsertNode(N: I, InsertPos: IP); |
| 2474 | return I; |
| 2475 | } |
| 2476 | |
| 2477 | void VarDefInit::Profile(FoldingSetNodeID &ID) const { |
| 2478 | ProfileVarDefInit(ID, Class, Args: args()); |
| 2479 | } |
| 2480 | |
| 2481 | const DefInit *VarDefInit::instantiate() { |
| 2482 | if (Def) |
| 2483 | return Def; |
| 2484 | |
| 2485 | RecordKeeper &Records = Class->getRecords(); |
| 2486 | auto NewRecOwner = std::make_unique<Record>( |
| 2487 | args: Records.getNewAnonymousName(), args&: Loc, args&: Records, args: Record::RK_AnonymousDef); |
| 2488 | Record *NewRec = NewRecOwner.get(); |
| 2489 | |
| 2490 | // Copy values from class to instance |
| 2491 | for (const RecordVal &Val : Class->getValues()) |
| 2492 | NewRec->addValue(RV: Val); |
| 2493 | |
| 2494 | // Copy assertions from class to instance. |
| 2495 | NewRec->appendAssertions(Rec: Class); |
| 2496 | |
| 2497 | // Copy dumps from class to instance. |
| 2498 | NewRec->appendDumps(Rec: Class); |
| 2499 | |
| 2500 | // Substitute and resolve template arguments |
| 2501 | ArrayRef<const Init *> TArgs = Class->getTemplateArgs(); |
| 2502 | MapResolver R(NewRec); |
| 2503 | |
| 2504 | for (const Init *Arg : TArgs) { |
| 2505 | R.set(Key: Arg, Value: NewRec->getValue(Name: Arg)->getValue()); |
| 2506 | NewRec->removeValue(Name: Arg); |
| 2507 | } |
| 2508 | |
| 2509 | for (auto *Arg : args()) { |
| 2510 | if (Arg->isPositional()) |
| 2511 | R.set(Key: TArgs[Arg->getIndex()], Value: Arg->getValue()); |
| 2512 | if (Arg->isNamed()) |
| 2513 | R.set(Key: Arg->getName(), Value: Arg->getValue()); |
| 2514 | } |
| 2515 | |
| 2516 | NewRec->resolveReferences(R); |
| 2517 | |
| 2518 | // Add superclass. |
| 2519 | NewRec->addDirectSuperClass( |
| 2520 | R: Class, Range: SMRange(Class->getLoc().back(), Class->getLoc().back())); |
| 2521 | |
| 2522 | // Resolve internal references and store in record keeper |
| 2523 | NewRec->resolveReferences(); |
| 2524 | Records.addDef(R: std::move(NewRecOwner)); |
| 2525 | |
| 2526 | // Check the assertions. |
| 2527 | NewRec->checkRecordAssertions(); |
| 2528 | |
| 2529 | // Check the assertions. |
| 2530 | NewRec->emitRecordDumps(); |
| 2531 | |
| 2532 | return Def = NewRec->getDefInit(); |
| 2533 | } |
| 2534 | |
| 2535 | const Init *VarDefInit::resolveReferences(Resolver &R) const { |
| 2536 | TrackUnresolvedResolver UR(&R); |
| 2537 | bool Changed = false; |
| 2538 | SmallVector<const ArgumentInit *, 8> NewArgs; |
| 2539 | NewArgs.reserve(N: args_size()); |
| 2540 | |
| 2541 | for (const ArgumentInit *Arg : args()) { |
| 2542 | const auto *NewArg = cast<ArgumentInit>(Val: Arg->resolveReferences(R&: UR)); |
| 2543 | NewArgs.push_back(Elt: NewArg); |
| 2544 | Changed |= NewArg != Arg; |
| 2545 | } |
| 2546 | |
| 2547 | if (Changed) { |
| 2548 | auto *New = VarDefInit::get(Loc, Class, Args: NewArgs); |
| 2549 | if (!UR.foundUnresolved()) |
| 2550 | return const_cast<VarDefInit *>(New)->instantiate(); |
| 2551 | return New; |
| 2552 | } |
| 2553 | return this; |
| 2554 | } |
| 2555 | |
| 2556 | const Init *VarDefInit::Fold() const { |
| 2557 | if (Def) |
| 2558 | return Def; |
| 2559 | |
| 2560 | TrackUnresolvedResolver R; |
| 2561 | for (const Init *Arg : args()) |
| 2562 | Arg->resolveReferences(R); |
| 2563 | |
| 2564 | if (!R.foundUnresolved()) |
| 2565 | return const_cast<VarDefInit *>(this)->instantiate(); |
| 2566 | return this; |
| 2567 | } |
| 2568 | |
| 2569 | std::string VarDefInit::getAsString() const { |
| 2570 | std::string Result = Class->getNameInitAsString() + "<" ; |
| 2571 | ListSeparator LS; |
| 2572 | for (const Init *Arg : args()) { |
| 2573 | Result += LS; |
| 2574 | Result += Arg->getAsString(); |
| 2575 | } |
| 2576 | return Result + ">" ; |
| 2577 | } |
| 2578 | |
| 2579 | const FieldInit *FieldInit::get(const Init *R, const StringInit *FN) { |
| 2580 | detail::RecordKeeperImpl &RK = R->getRecordKeeper().getImpl(); |
| 2581 | FieldInit *&I = RK.TheFieldInitPool[{R, FN}]; |
| 2582 | if (!I) |
| 2583 | I = new (RK.Allocator) FieldInit(R, FN); |
| 2584 | return I; |
| 2585 | } |
| 2586 | |
| 2587 | const Init *FieldInit::getBit(unsigned Bit) const { |
| 2588 | if (getType() == BitRecTy::get(RK&: getRecordKeeper())) |
| 2589 | return this; |
| 2590 | return VarBitInit::get(T: this, B: Bit); |
| 2591 | } |
| 2592 | |
| 2593 | const Init *FieldInit::resolveReferences(Resolver &R) const { |
| 2594 | const Init *NewRec = Rec->resolveReferences(R); |
| 2595 | if (NewRec != Rec) |
| 2596 | return FieldInit::get(R: NewRec, FN: FieldName)->Fold(CurRec: R.getCurrentRecord()); |
| 2597 | return this; |
| 2598 | } |
| 2599 | |
| 2600 | const Init *FieldInit::Fold(const Record *CurRec) const { |
| 2601 | if (const auto *DI = dyn_cast<DefInit>(Val: Rec)) { |
| 2602 | const Record *Def = DI->getDef(); |
| 2603 | if (Def == CurRec) |
| 2604 | PrintFatalError(ErrorLoc: CurRec->getLoc(), |
| 2605 | Msg: Twine("Attempting to access field '" ) + |
| 2606 | FieldName->getAsUnquotedString() + "' of '" + |
| 2607 | Rec->getAsString() + "' is a forbidden self-reference" ); |
| 2608 | const Init *FieldVal = Def->getValue(Name: FieldName)->getValue(); |
| 2609 | if (FieldVal->isConcrete()) |
| 2610 | return FieldVal; |
| 2611 | } |
| 2612 | return this; |
| 2613 | } |
| 2614 | |
| 2615 | bool FieldInit::isConcrete() const { |
| 2616 | if (const auto *DI = dyn_cast<DefInit>(Val: Rec)) { |
| 2617 | const Init *FieldVal = DI->getDef()->getValue(Name: FieldName)->getValue(); |
| 2618 | return FieldVal->isConcrete(); |
| 2619 | } |
| 2620 | return false; |
| 2621 | } |
| 2622 | |
| 2623 | static void ProfileCondOpInit(FoldingSetNodeID &ID, |
| 2624 | ArrayRef<const Init *> Conds, |
| 2625 | ArrayRef<const Init *> Vals, |
| 2626 | const RecTy *ValType) { |
| 2627 | assert(Conds.size() == Vals.size() && |
| 2628 | "Number of conditions and values must match!" ); |
| 2629 | ID.AddPointer(Ptr: ValType); |
| 2630 | |
| 2631 | for (const auto &[Cond, Val] : zip(t&: Conds, u&: Vals)) { |
| 2632 | ID.AddPointer(Ptr: Cond); |
| 2633 | ID.AddPointer(Ptr: Val); |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | CondOpInit::CondOpInit(ArrayRef<const Init *> Conds, |
| 2638 | ArrayRef<const Init *> Values, const RecTy *Type) |
| 2639 | : TypedInit(IK_CondOpInit, Type), NumConds(Conds.size()), ValType(Type) { |
| 2640 | const Init **TrailingObjects = getTrailingObjects(); |
| 2641 | llvm::uninitialized_copy(Src&: Conds, Dst: TrailingObjects); |
| 2642 | llvm::uninitialized_copy(Src&: Values, Dst: TrailingObjects + NumConds); |
| 2643 | } |
| 2644 | |
| 2645 | void CondOpInit::Profile(FoldingSetNodeID &ID) const { |
| 2646 | ProfileCondOpInit(ID, Conds: getConds(), Vals: getVals(), ValType); |
| 2647 | } |
| 2648 | |
| 2649 | const CondOpInit *CondOpInit::get(ArrayRef<const Init *> Conds, |
| 2650 | ArrayRef<const Init *> Values, |
| 2651 | const RecTy *Ty) { |
| 2652 | assert(Conds.size() == Values.size() && |
| 2653 | "Number of conditions and values must match!" ); |
| 2654 | |
| 2655 | FoldingSetNodeID ID; |
| 2656 | ProfileCondOpInit(ID, Conds, Vals: Values, ValType: Ty); |
| 2657 | |
| 2658 | detail::RecordKeeperImpl &RK = Ty->getRecordKeeper().getImpl(); |
| 2659 | void *IP = nullptr; |
| 2660 | if (const CondOpInit *I = RK.TheCondOpInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 2661 | return I; |
| 2662 | |
| 2663 | void *Mem = RK.Allocator.Allocate( |
| 2664 | Size: totalSizeToAlloc<const Init *>(Counts: 2 * Conds.size()), Alignment: alignof(CondOpInit)); |
| 2665 | CondOpInit *I = new (Mem) CondOpInit(Conds, Values, Ty); |
| 2666 | RK.TheCondOpInitPool.InsertNode(N: I, InsertPos: IP); |
| 2667 | return I; |
| 2668 | } |
| 2669 | |
| 2670 | const Init *CondOpInit::resolveReferences(Resolver &R) const { |
| 2671 | SmallVector<const Init *, 4> NewConds; |
| 2672 | SmallVector<const Init *, 4> NewVals; |
| 2673 | |
| 2674 | bool Changed = false; |
| 2675 | for (auto [Cond, Val] : getCondAndVals()) { |
| 2676 | const Init *NewCond = Cond->resolveReferences(R); |
| 2677 | NewConds.push_back(Elt: NewCond); |
| 2678 | Changed |= NewCond != Cond; |
| 2679 | |
| 2680 | const Init *NewVal = Val->resolveReferences(R); |
| 2681 | NewVals.push_back(Elt: NewVal); |
| 2682 | Changed |= NewVal != Val; |
| 2683 | } |
| 2684 | |
| 2685 | if (Changed) |
| 2686 | return (CondOpInit::get(Conds: NewConds, Values: NewVals, |
| 2687 | Ty: getValType()))->Fold(CurRec: R.getCurrentRecord()); |
| 2688 | |
| 2689 | return this; |
| 2690 | } |
| 2691 | |
| 2692 | const Init *CondOpInit::Fold(const Record *CurRec) const { |
| 2693 | RecordKeeper &RK = getRecordKeeper(); |
| 2694 | for (auto [Cond, Val] : getCondAndVals()) { |
| 2695 | if (const auto *CondI = dyn_cast_or_null<IntInit>( |
| 2696 | Val: Cond->convertInitializerTo(Ty: IntRecTy::get(RK)))) { |
| 2697 | if (CondI->getValue()) |
| 2698 | return Val->convertInitializerTo(Ty: getValType()); |
| 2699 | } else { |
| 2700 | return this; |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | PrintFatalError(ErrorLoc: CurRec->getLoc(), |
| 2705 | Msg: CurRec->getNameInitAsString() + |
| 2706 | " does not have any true condition in:" + |
| 2707 | this->getAsString()); |
| 2708 | return nullptr; |
| 2709 | } |
| 2710 | |
| 2711 | bool CondOpInit::isConcrete() const { |
| 2712 | return all_of(Range: getCondAndVals(), P: [](const auto &Pair) { |
| 2713 | return std::get<0>(Pair)->isConcrete() && std::get<1>(Pair)->isConcrete(); |
| 2714 | }); |
| 2715 | } |
| 2716 | |
| 2717 | bool CondOpInit::isComplete() const { |
| 2718 | return all_of(Range: getCondAndVals(), P: [](const auto &Pair) { |
| 2719 | return std::get<0>(Pair)->isComplete() && std::get<1>(Pair)->isComplete(); |
| 2720 | }); |
| 2721 | } |
| 2722 | |
| 2723 | std::string CondOpInit::getAsString() const { |
| 2724 | std::string Result = "!cond(" ; |
| 2725 | ListSeparator LS; |
| 2726 | for (auto [Cond, Val] : getCondAndVals()) { |
| 2727 | Result += LS; |
| 2728 | Result += Cond->getAsString() + ": " ; |
| 2729 | Result += Val->getAsString(); |
| 2730 | } |
| 2731 | return Result + ")" ; |
| 2732 | } |
| 2733 | |
| 2734 | const Init *CondOpInit::getBit(unsigned Bit) const { |
| 2735 | return VarBitInit::get(T: this, B: Bit); |
| 2736 | } |
| 2737 | |
| 2738 | static void ProfileDagInit(FoldingSetNodeID &ID, const Init *V, |
| 2739 | const StringInit *VN, ArrayRef<const Init *> Args, |
| 2740 | ArrayRef<const StringInit *> ArgNames) { |
| 2741 | ID.AddPointer(Ptr: V); |
| 2742 | ID.AddPointer(Ptr: VN); |
| 2743 | |
| 2744 | for (auto [Arg, Name] : zip_equal(t&: Args, u&: ArgNames)) { |
| 2745 | ID.AddPointer(Ptr: Arg); |
| 2746 | ID.AddPointer(Ptr: Name); |
| 2747 | } |
| 2748 | } |
| 2749 | |
| 2750 | DagInit::DagInit(const Init *V, const StringInit *VN, |
| 2751 | ArrayRef<const Init *> Args, |
| 2752 | ArrayRef<const StringInit *> ArgNames) |
| 2753 | : TypedInit(IK_DagInit, DagRecTy::get(RK&: V->getRecordKeeper())), Val(V), |
| 2754 | ValName(VN), NumArgs(Args.size()) { |
| 2755 | llvm::uninitialized_copy(Src&: Args, Dst: getTrailingObjects<const Init *>()); |
| 2756 | llvm::uninitialized_copy(Src&: ArgNames, Dst: getTrailingObjects<const StringInit *>()); |
| 2757 | } |
| 2758 | |
| 2759 | const DagInit *DagInit::get(const Init *V, const StringInit *VN, |
| 2760 | ArrayRef<const Init *> Args, |
| 2761 | ArrayRef<const StringInit *> ArgNames) { |
| 2762 | assert(Args.size() == ArgNames.size() && |
| 2763 | "Number of DAG args and arg names must match!" ); |
| 2764 | |
| 2765 | FoldingSetNodeID ID; |
| 2766 | ProfileDagInit(ID, V, VN, Args, ArgNames); |
| 2767 | |
| 2768 | detail::RecordKeeperImpl &RK = V->getRecordKeeper().getImpl(); |
| 2769 | void *IP = nullptr; |
| 2770 | if (const DagInit *I = RK.TheDagInitPool.FindNodeOrInsertPos(ID, InsertPos&: IP)) |
| 2771 | return I; |
| 2772 | |
| 2773 | void *Mem = |
| 2774 | RK.Allocator.Allocate(Size: totalSizeToAlloc<const Init *, const StringInit *>( |
| 2775 | Counts: Args.size(), Counts: ArgNames.size()), |
| 2776 | Alignment: alignof(DagInit)); |
| 2777 | DagInit *I = new (Mem) DagInit(V, VN, Args, ArgNames); |
| 2778 | RK.TheDagInitPool.InsertNode(N: I, InsertPos: IP); |
| 2779 | return I; |
| 2780 | } |
| 2781 | |
| 2782 | const DagInit *DagInit::get( |
| 2783 | const Init *V, const StringInit *VN, |
| 2784 | ArrayRef<std::pair<const Init *, const StringInit *>> ArgAndNames) { |
| 2785 | SmallVector<const Init *, 8> Args(make_first_range(c&: ArgAndNames)); |
| 2786 | SmallVector<const StringInit *, 8> Names(make_second_range(c&: ArgAndNames)); |
| 2787 | return DagInit::get(V, VN, Args, ArgNames: Names); |
| 2788 | } |
| 2789 | |
| 2790 | void DagInit::Profile(FoldingSetNodeID &ID) const { |
| 2791 | ProfileDagInit(ID, V: Val, VN: ValName, Args: getArgs(), ArgNames: getArgNames()); |
| 2792 | } |
| 2793 | |
| 2794 | const Record *DagInit::getOperatorAsDef(ArrayRef<SMLoc> Loc) const { |
| 2795 | if (const auto *DefI = dyn_cast<DefInit>(Val)) |
| 2796 | return DefI->getDef(); |
| 2797 | PrintFatalError(ErrorLoc: Loc, Msg: "Expected record as operator" ); |
| 2798 | return nullptr; |
| 2799 | } |
| 2800 | |
| 2801 | std::optional<unsigned> DagInit::getArgNo(StringRef Name) const { |
| 2802 | ArrayRef<const StringInit *> ArgNames = getArgNames(); |
| 2803 | auto It = llvm::find_if(Range&: ArgNames, P: [Name](const StringInit *ArgName) { |
| 2804 | return ArgName && ArgName->getValue() == Name; |
| 2805 | }); |
| 2806 | if (It == ArgNames.end()) |
| 2807 | return std::nullopt; |
| 2808 | return std::distance(first: ArgNames.begin(), last: It); |
| 2809 | } |
| 2810 | |
| 2811 | const Init *DagInit::resolveReferences(Resolver &R) const { |
| 2812 | SmallVector<const Init *, 8> NewArgs; |
| 2813 | NewArgs.reserve(N: arg_size()); |
| 2814 | bool ArgsChanged = false; |
| 2815 | for (const Init *Arg : getArgs()) { |
| 2816 | const Init *NewArg = Arg->resolveReferences(R); |
| 2817 | NewArgs.push_back(Elt: NewArg); |
| 2818 | ArgsChanged |= NewArg != Arg; |
| 2819 | } |
| 2820 | |
| 2821 | const Init *Op = Val->resolveReferences(R); |
| 2822 | if (Op != Val || ArgsChanged) |
| 2823 | return DagInit::get(V: Op, VN: ValName, Args: NewArgs, ArgNames: getArgNames()); |
| 2824 | |
| 2825 | return this; |
| 2826 | } |
| 2827 | |
| 2828 | bool DagInit::isConcrete() const { |
| 2829 | if (!Val->isConcrete()) |
| 2830 | return false; |
| 2831 | return all_of(Range: getArgs(), P: [](const Init *Elt) { return Elt->isConcrete(); }); |
| 2832 | } |
| 2833 | |
| 2834 | std::string DagInit::getAsString() const { |
| 2835 | std::string Result = "(" + Val->getAsString(); |
| 2836 | if (ValName) |
| 2837 | Result += ":$" + ValName->getAsUnquotedString(); |
| 2838 | if (!arg_empty()) { |
| 2839 | Result += " " ; |
| 2840 | ListSeparator LS; |
| 2841 | for (auto [Arg, Name] : getArgAndNames()) { |
| 2842 | Result += LS; |
| 2843 | Result += Arg->getAsString(); |
| 2844 | if (Name) |
| 2845 | Result += ":$" + Name->getAsUnquotedString(); |
| 2846 | } |
| 2847 | } |
| 2848 | return Result + ")" ; |
| 2849 | } |
| 2850 | |
| 2851 | //===----------------------------------------------------------------------===// |
| 2852 | // Other implementations |
| 2853 | //===----------------------------------------------------------------------===// |
| 2854 | |
| 2855 | RecordVal::RecordVal(const Init *N, const RecTy *T, FieldKind K) |
| 2856 | : Name(N), TyAndKind(T, K) { |
| 2857 | setValue(UnsetInit::get(RK&: N->getRecordKeeper())); |
| 2858 | assert(Value && "Cannot create unset value for current type!" ); |
| 2859 | } |
| 2860 | |
| 2861 | // This constructor accepts the same arguments as the above, but also |
| 2862 | // a source location. |
| 2863 | RecordVal::RecordVal(const Init *N, SMLoc Loc, const RecTy *T, FieldKind K) |
| 2864 | : Name(N), Loc(Loc), TyAndKind(T, K) { |
| 2865 | setValue(UnsetInit::get(RK&: N->getRecordKeeper())); |
| 2866 | assert(Value && "Cannot create unset value for current type!" ); |
| 2867 | } |
| 2868 | |
| 2869 | StringRef RecordVal::getName() const { |
| 2870 | return cast<StringInit>(Val: getNameInit())->getValue(); |
| 2871 | } |
| 2872 | |
| 2873 | std::string RecordVal::getPrintType() const { |
| 2874 | if (getType() == StringRecTy::get(RK&: getRecordKeeper())) { |
| 2875 | if (const auto *StrInit = dyn_cast<StringInit>(Val: Value)) { |
| 2876 | if (StrInit->hasCodeFormat()) |
| 2877 | return "code" ; |
| 2878 | else |
| 2879 | return "string" ; |
| 2880 | } else { |
| 2881 | return "string" ; |
| 2882 | } |
| 2883 | } else { |
| 2884 | return TyAndKind.getPointer()->getAsString(); |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | bool RecordVal::setValue(const Init *V) { |
| 2889 | if (!V) { |
| 2890 | Value = nullptr; |
| 2891 | return false; |
| 2892 | } |
| 2893 | |
| 2894 | Value = V->getCastTo(Ty: getType()); |
| 2895 | if (!Value) |
| 2896 | return true; |
| 2897 | |
| 2898 | assert(!isa<TypedInit>(Value) || |
| 2899 | cast<TypedInit>(Value)->getType()->typeIsA(getType())); |
| 2900 | if (const auto *BTy = dyn_cast<BitsRecTy>(Val: getType())) { |
| 2901 | if (isa<BitsInit>(Val: Value)) |
| 2902 | return false; |
| 2903 | SmallVector<const Init *, 64> Bits(BTy->getNumBits()); |
| 2904 | for (unsigned I = 0, E = BTy->getNumBits(); I < E; ++I) |
| 2905 | Bits[I] = Value->getBit(Bit: I); |
| 2906 | Value = BitsInit::get(RK&: V->getRecordKeeper(), Bits); |
| 2907 | } |
| 2908 | |
| 2909 | return false; |
| 2910 | } |
| 2911 | |
| 2912 | // This version of setValue takes a source location and resets the |
| 2913 | // location in the RecordVal. |
| 2914 | bool RecordVal::setValue(const Init *V, SMLoc NewLoc) { |
| 2915 | Loc = NewLoc; |
| 2916 | return setValue(V); |
| 2917 | } |
| 2918 | |
| 2919 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 2920 | LLVM_DUMP_METHOD void RecordVal::dump() const { errs() << *this; } |
| 2921 | #endif |
| 2922 | |
| 2923 | void RecordVal::print(raw_ostream &OS, bool PrintSem) const { |
| 2924 | if (isNonconcreteOK()) OS << "field " ; |
| 2925 | OS << getPrintType() << " " << getNameInitAsString(); |
| 2926 | |
| 2927 | if (getValue()) |
| 2928 | OS << " = " << *getValue(); |
| 2929 | |
| 2930 | if (PrintSem) OS << ";\n" ; |
| 2931 | } |
| 2932 | |
| 2933 | void Record::updateClassLoc(SMLoc Loc) { |
| 2934 | assert(Locs.size() == 1); |
| 2935 | ForwardDeclarationLocs.push_back(Elt: Locs.front()); |
| 2936 | |
| 2937 | Locs.clear(); |
| 2938 | Locs.push_back(Elt: Loc); |
| 2939 | } |
| 2940 | |
| 2941 | void Record::checkName() { |
| 2942 | // Ensure the record name has string type. |
| 2943 | const auto *TypedName = cast<const TypedInit>(Val: Name); |
| 2944 | if (!isa<StringRecTy>(Val: TypedName->getType())) |
| 2945 | PrintFatalError(ErrorLoc: getLoc(), Msg: Twine("Record name '" ) + Name->getAsString() + |
| 2946 | "' is not a string!" ); |
| 2947 | } |
| 2948 | |
| 2949 | const RecordRecTy *Record::getType() const { |
| 2950 | SmallVector<const Record *> DirectSCs( |
| 2951 | make_first_range(c: getDirectSuperClasses())); |
| 2952 | return RecordRecTy::get(RK&: TrackedRecords, UnsortedClasses: DirectSCs); |
| 2953 | } |
| 2954 | |
| 2955 | DefInit *Record::getDefInit() const { |
| 2956 | if (!CorrespondingDefInit) { |
| 2957 | CorrespondingDefInit = |
| 2958 | new (TrackedRecords.getImpl().Allocator) DefInit(this); |
| 2959 | } |
| 2960 | return CorrespondingDefInit; |
| 2961 | } |
| 2962 | |
| 2963 | unsigned Record::getNewUID(RecordKeeper &RK) { |
| 2964 | return RK.getImpl().LastRecordID++; |
| 2965 | } |
| 2966 | |
| 2967 | void Record::setName(const Init *NewName) { |
| 2968 | Name = NewName; |
| 2969 | checkName(); |
| 2970 | // DO NOT resolve record values to the name at this point because |
| 2971 | // there might be default values for arguments of this def. Those |
| 2972 | // arguments might not have been resolved yet so we don't want to |
| 2973 | // prematurely assume values for those arguments were not passed to |
| 2974 | // this def. |
| 2975 | // |
| 2976 | // Nonetheless, it may be that some of this Record's values |
| 2977 | // reference the record name. Indeed, the reason for having the |
| 2978 | // record name be an Init is to provide this flexibility. The extra |
| 2979 | // resolve steps after completely instantiating defs takes care of |
| 2980 | // this. See TGParser::ParseDef and TGParser::ParseDefm. |
| 2981 | } |
| 2982 | |
| 2983 | void Record::resolveReferences(Resolver &R, const RecordVal *SkipVal) { |
| 2984 | const Init *OldName = getNameInit(); |
| 2985 | const Init *NewName = Name->resolveReferences(R); |
| 2986 | if (NewName != OldName) { |
| 2987 | // Re-register with RecordKeeper. |
| 2988 | setName(NewName); |
| 2989 | } |
| 2990 | |
| 2991 | // Resolve the field values. |
| 2992 | for (RecordVal &Value : Values) { |
| 2993 | if (SkipVal == &Value) // Skip resolve the same field as the given one |
| 2994 | continue; |
| 2995 | if (const Init *V = Value.getValue()) { |
| 2996 | const Init *VR = V->resolveReferences(R); |
| 2997 | if (Value.setValue(VR)) { |
| 2998 | std::string Type; |
| 2999 | if (const auto *VRT = dyn_cast<TypedInit>(Val: VR)) |
| 3000 | Type = |
| 3001 | (Twine("of type '" ) + VRT->getType()->getAsString() + "' " ).str(); |
| 3002 | PrintFatalError( |
| 3003 | ErrorLoc: getLoc(), |
| 3004 | Msg: Twine("Invalid value " ) + Type + "found when setting field '" + |
| 3005 | Value.getNameInitAsString() + "' of type '" + |
| 3006 | Value.getType()->getAsString() + |
| 3007 | "' after resolving references: " + VR->getAsUnquotedString() + |
| 3008 | "\n" ); |
| 3009 | } |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | // Resolve the assertion expressions. |
| 3014 | for (AssertionInfo &Assertion : Assertions) { |
| 3015 | const Init *Value = Assertion.Condition->resolveReferences(R); |
| 3016 | Assertion.Condition = Value; |
| 3017 | Value = Assertion.Message->resolveReferences(R); |
| 3018 | Assertion.Message = Value; |
| 3019 | } |
| 3020 | // Resolve the dump expressions. |
| 3021 | for (DumpInfo &Dump : Dumps) { |
| 3022 | const Init *Value = Dump.Message->resolveReferences(R); |
| 3023 | Dump.Message = Value; |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | void Record::resolveReferences(const Init *NewName) { |
| 3028 | RecordResolver R(*this); |
| 3029 | R.setName(NewName); |
| 3030 | R.setFinal(true); |
| 3031 | resolveReferences(R); |
| 3032 | } |
| 3033 | |
| 3034 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 3035 | LLVM_DUMP_METHOD void Record::dump() const { errs() << *this; } |
| 3036 | #endif |
| 3037 | |
| 3038 | raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { |
| 3039 | OS << R.getNameInitAsString(); |
| 3040 | |
| 3041 | ArrayRef<const Init *> TArgs = R.getTemplateArgs(); |
| 3042 | if (!TArgs.empty()) { |
| 3043 | OS << "<" ; |
| 3044 | ListSeparator LS; |
| 3045 | for (const Init *TA : TArgs) { |
| 3046 | const RecordVal *RV = R.getValue(Name: TA); |
| 3047 | assert(RV && "Template argument record not found??" ); |
| 3048 | OS << LS; |
| 3049 | RV->print(OS, PrintSem: false); |
| 3050 | } |
| 3051 | OS << ">" ; |
| 3052 | } |
| 3053 | |
| 3054 | OS << " {" ; |
| 3055 | std::vector<const Record *> SCs = R.getSuperClasses(); |
| 3056 | if (!SCs.empty()) { |
| 3057 | OS << "\t//" ; |
| 3058 | for (const Record *SC : SCs) |
| 3059 | OS << " " << SC->getNameInitAsString(); |
| 3060 | } |
| 3061 | OS << "\n" ; |
| 3062 | |
| 3063 | for (const RecordVal &Val : R.getValues()) |
| 3064 | if (Val.isNonconcreteOK() && !R.isTemplateArg(Name: Val.getNameInit())) |
| 3065 | OS << Val; |
| 3066 | for (const RecordVal &Val : R.getValues()) |
| 3067 | if (!Val.isNonconcreteOK() && !R.isTemplateArg(Name: Val.getNameInit())) |
| 3068 | OS << Val; |
| 3069 | |
| 3070 | return OS << "}\n" ; |
| 3071 | } |
| 3072 | |
| 3073 | SMLoc Record::getFieldLoc(StringRef FieldName) const { |
| 3074 | const RecordVal *R = getValue(Name: FieldName); |
| 3075 | if (!R) |
| 3076 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + |
| 3077 | "' does not have a field named `" + FieldName + "'!\n" ); |
| 3078 | return R->getLoc(); |
| 3079 | } |
| 3080 | |
| 3081 | const Init *Record::getValueInit(StringRef FieldName) const { |
| 3082 | const RecordVal *R = getValue(Name: FieldName); |
| 3083 | if (!R || !R->getValue()) |
| 3084 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + |
| 3085 | "' does not have a field named `" + FieldName + "'!\n" ); |
| 3086 | return R->getValue(); |
| 3087 | } |
| 3088 | |
| 3089 | StringRef Record::getValueAsString(StringRef FieldName) const { |
| 3090 | const Init *I = getValueInit(FieldName); |
| 3091 | if (const auto *SI = dyn_cast<StringInit>(Val: I)) |
| 3092 | return SI->getValue(); |
| 3093 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + "', field `" + FieldName + |
| 3094 | "' exists but does not have a string value" ); |
| 3095 | } |
| 3096 | |
| 3097 | std::optional<StringRef> |
| 3098 | Record::getValueAsOptionalString(StringRef FieldName) const { |
| 3099 | const RecordVal *R = getValue(Name: FieldName); |
| 3100 | if (!R || !R->getValue()) |
| 3101 | return std::nullopt; |
| 3102 | if (isa<UnsetInit>(Val: R->getValue())) |
| 3103 | return std::nullopt; |
| 3104 | |
| 3105 | if (const auto *SI = dyn_cast<StringInit>(Val: R->getValue())) |
| 3106 | return SI->getValue(); |
| 3107 | |
| 3108 | PrintFatalError(ErrorLoc: getLoc(), |
| 3109 | Msg: "Record `" + getName() + "', ` field `" + FieldName + |
| 3110 | "' exists but does not have a string initializer!" ); |
| 3111 | } |
| 3112 | |
| 3113 | const BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { |
| 3114 | const Init *I = getValueInit(FieldName); |
| 3115 | if (const auto *BI = dyn_cast<BitsInit>(Val: I)) |
| 3116 | return BI; |
| 3117 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + "', field `" + FieldName + |
| 3118 | "' exists but does not have a bits value" ); |
| 3119 | } |
| 3120 | |
| 3121 | const ListInit *Record::getValueAsListInit(StringRef FieldName) const { |
| 3122 | const Init *I = getValueInit(FieldName); |
| 3123 | if (const auto *LI = dyn_cast<ListInit>(Val: I)) |
| 3124 | return LI; |
| 3125 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + "', field `" + FieldName + |
| 3126 | "' exists but does not have a list value" ); |
| 3127 | } |
| 3128 | |
| 3129 | std::vector<const Record *> |
| 3130 | Record::getValueAsListOfDefs(StringRef FieldName) const { |
| 3131 | const ListInit *List = getValueAsListInit(FieldName); |
| 3132 | std::vector<const Record *> Defs; |
| 3133 | for (const Init *I : List->getElements()) { |
| 3134 | if (const auto *DI = dyn_cast<DefInit>(Val: I)) |
| 3135 | Defs.push_back(x: DI->getDef()); |
| 3136 | else |
| 3137 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + "', field `" + |
| 3138 | FieldName + |
| 3139 | "' list is not entirely DefInit!" ); |
| 3140 | } |
| 3141 | return Defs; |
| 3142 | } |
| 3143 | |
| 3144 | int64_t Record::getValueAsInt(StringRef FieldName) const { |
| 3145 | const Init *I = getValueInit(FieldName); |
| 3146 | if (const auto *II = dyn_cast<IntInit>(Val: I)) |
| 3147 | return II->getValue(); |
| 3148 | PrintFatalError( |
| 3149 | ErrorLoc: getLoc(), |
| 3150 | Msg: Twine("Record `" ) + getName() + "', field `" + FieldName + |
| 3151 | "' exists but does not have an int value: " + I->getAsString()); |
| 3152 | } |
| 3153 | |
| 3154 | std::vector<int64_t> |
| 3155 | Record::getValueAsListOfInts(StringRef FieldName) const { |
| 3156 | const ListInit *List = getValueAsListInit(FieldName); |
| 3157 | std::vector<int64_t> Ints; |
| 3158 | for (const Init *I : List->getElements()) { |
| 3159 | if (const auto *II = dyn_cast<IntInit>(Val: I)) |
| 3160 | Ints.push_back(x: II->getValue()); |
| 3161 | else |
| 3162 | PrintFatalError(ErrorLoc: getLoc(), |
| 3163 | Msg: Twine("Record `" ) + getName() + "', field `" + FieldName + |
| 3164 | "' exists but does not have a list of ints value: " + |
| 3165 | I->getAsString()); |
| 3166 | } |
| 3167 | return Ints; |
| 3168 | } |
| 3169 | |
| 3170 | std::vector<StringRef> |
| 3171 | Record::getValueAsListOfStrings(StringRef FieldName) const { |
| 3172 | const ListInit *List = getValueAsListInit(FieldName); |
| 3173 | std::vector<StringRef> Strings; |
| 3174 | for (const Init *I : List->getElements()) { |
| 3175 | if (const auto *SI = dyn_cast<StringInit>(Val: I)) |
| 3176 | Strings.push_back(x: SI->getValue()); |
| 3177 | else |
| 3178 | PrintFatalError(ErrorLoc: getLoc(), |
| 3179 | Msg: Twine("Record `" ) + getName() + "', field `" + FieldName + |
| 3180 | "' exists but does not have a list of strings value: " + |
| 3181 | I->getAsString()); |
| 3182 | } |
| 3183 | return Strings; |
| 3184 | } |
| 3185 | |
| 3186 | const Record *Record::getValueAsDef(StringRef FieldName) const { |
| 3187 | const Init *I = getValueInit(FieldName); |
| 3188 | if (const auto *DI = dyn_cast<DefInit>(Val: I)) |
| 3189 | return DI->getDef(); |
| 3190 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + "', field `" + |
| 3191 | FieldName + "' does not have a def initializer!" ); |
| 3192 | } |
| 3193 | |
| 3194 | const Record *Record::getValueAsOptionalDef(StringRef FieldName) const { |
| 3195 | const Init *I = getValueInit(FieldName); |
| 3196 | if (const auto *DI = dyn_cast<DefInit>(Val: I)) |
| 3197 | return DI->getDef(); |
| 3198 | if (isa<UnsetInit>(Val: I)) |
| 3199 | return nullptr; |
| 3200 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + "', field `" + |
| 3201 | FieldName + "' does not have either a def initializer or '?'!" ); |
| 3202 | } |
| 3203 | |
| 3204 | bool Record::getValueAsBit(StringRef FieldName) const { |
| 3205 | const Init *I = getValueInit(FieldName); |
| 3206 | if (const auto *BI = dyn_cast<BitInit>(Val: I)) |
| 3207 | return BI->getValue(); |
| 3208 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + "', field `" + |
| 3209 | FieldName + "' does not have a bit initializer!" ); |
| 3210 | } |
| 3211 | |
| 3212 | bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { |
| 3213 | const Init *I = getValueInit(FieldName); |
| 3214 | if (isa<UnsetInit>(Val: I)) { |
| 3215 | Unset = true; |
| 3216 | return false; |
| 3217 | } |
| 3218 | Unset = false; |
| 3219 | if (const auto *BI = dyn_cast<BitInit>(Val: I)) |
| 3220 | return BI->getValue(); |
| 3221 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + "', field `" + |
| 3222 | FieldName + "' does not have a bit initializer!" ); |
| 3223 | } |
| 3224 | |
| 3225 | const DagInit *Record::getValueAsDag(StringRef FieldName) const { |
| 3226 | const Init *I = getValueInit(FieldName); |
| 3227 | if (const auto *DI = dyn_cast<DagInit>(Val: I)) |
| 3228 | return DI; |
| 3229 | PrintFatalError(ErrorLoc: getLoc(), Msg: "Record `" + getName() + "', field `" + |
| 3230 | FieldName + "' does not have a dag initializer!" ); |
| 3231 | } |
| 3232 | |
| 3233 | // Check all record assertions: For each one, resolve the condition |
| 3234 | // and message, then call CheckAssert(). |
| 3235 | // Note: The condition and message are probably already resolved, |
| 3236 | // but resolving again allows calls before records are resolved. |
| 3237 | void Record::checkRecordAssertions() { |
| 3238 | RecordResolver R(*this); |
| 3239 | R.setFinal(true); |
| 3240 | |
| 3241 | bool AnyFailed = false; |
| 3242 | for (const auto &Assertion : getAssertions()) { |
| 3243 | const Init *Condition = Assertion.Condition->resolveReferences(R); |
| 3244 | const Init *Message = Assertion.Message->resolveReferences(R); |
| 3245 | AnyFailed |= CheckAssert(Loc: Assertion.Loc, Condition, Message); |
| 3246 | } |
| 3247 | |
| 3248 | if (!AnyFailed) |
| 3249 | return; |
| 3250 | |
| 3251 | // If any of the record assertions failed, print some context that will |
| 3252 | // help see where the record that caused these assert failures is defined. |
| 3253 | PrintError(Rec: this, Msg: "assertion failed in this record" ); |
| 3254 | } |
| 3255 | |
| 3256 | void Record::emitRecordDumps() { |
| 3257 | RecordResolver R(*this); |
| 3258 | R.setFinal(true); |
| 3259 | |
| 3260 | for (const DumpInfo &Dump : getDumps()) { |
| 3261 | const Init *Message = Dump.Message->resolveReferences(R); |
| 3262 | dumpMessage(Loc: Dump.Loc, Message); |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | // Report a warning if the record has unused template arguments. |
| 3267 | void Record::checkUnusedTemplateArgs() { |
| 3268 | for (const Init *TA : getTemplateArgs()) { |
| 3269 | const RecordVal *Arg = getValue(Name: TA); |
| 3270 | if (!Arg->isUsed()) |
| 3271 | PrintWarning(WarningLoc: Arg->getLoc(), |
| 3272 | Msg: "unused template argument: " + Twine(Arg->getName())); |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | RecordKeeper::RecordKeeper() |
| 3277 | : Impl(std::make_unique<detail::RecordKeeperImpl>(args&: *this)), |
| 3278 | Timer(std::make_unique<TGTimer>()) {} |
| 3279 | |
| 3280 | RecordKeeper::~RecordKeeper() = default; |
| 3281 | |
| 3282 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 3283 | LLVM_DUMP_METHOD void RecordKeeper::dump() const { errs() << *this; } |
| 3284 | #endif |
| 3285 | |
| 3286 | raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { |
| 3287 | OS << "------------- Classes -----------------\n" ; |
| 3288 | for (const auto &[_, C] : RK.getClasses()) |
| 3289 | OS << "class " << *C; |
| 3290 | |
| 3291 | OS << "------------- Defs -----------------\n" ; |
| 3292 | for (const auto &[_, D] : RK.getDefs()) |
| 3293 | OS << "def " << *D; |
| 3294 | return OS; |
| 3295 | } |
| 3296 | |
| 3297 | /// GetNewAnonymousName - Generate a unique anonymous name that can be used as |
| 3298 | /// an identifier. |
| 3299 | const Init *RecordKeeper::getNewAnonymousName() { |
| 3300 | return AnonymousNameInit::get(RK&: *this, V: getImpl().AnonCounter++); |
| 3301 | } |
| 3302 | |
| 3303 | ArrayRef<const Record *> |
| 3304 | RecordKeeper::getAllDerivedDefinitions(StringRef ClassName) const { |
| 3305 | // We cache the record vectors for single classes. Many backends request |
| 3306 | // the same vectors multiple times. |
| 3307 | auto [Iter, Inserted] = Cache.try_emplace(k: ClassName.str()); |
| 3308 | if (Inserted) |
| 3309 | Iter->second = getAllDerivedDefinitions(ClassNames: ArrayRef(ClassName)); |
| 3310 | return Iter->second; |
| 3311 | } |
| 3312 | |
| 3313 | std::vector<const Record *> |
| 3314 | RecordKeeper::getAllDerivedDefinitions(ArrayRef<StringRef> ClassNames) const { |
| 3315 | SmallVector<const Record *, 2> ClassRecs; |
| 3316 | std::vector<const Record *> Defs; |
| 3317 | |
| 3318 | assert(ClassNames.size() > 0 && "At least one class must be passed." ); |
| 3319 | for (StringRef ClassName : ClassNames) { |
| 3320 | const Record *Class = getClass(Name: ClassName); |
| 3321 | if (!Class) |
| 3322 | PrintFatalError(Msg: "The class '" + ClassName + "' is not defined\n" ); |
| 3323 | ClassRecs.push_back(Elt: Class); |
| 3324 | } |
| 3325 | |
| 3326 | for (const auto &OneDef : getDefs()) { |
| 3327 | if (all_of(Range&: ClassRecs, P: [&OneDef](const Record *Class) { |
| 3328 | return OneDef.second->isSubClassOf(R: Class); |
| 3329 | })) |
| 3330 | Defs.push_back(x: OneDef.second.get()); |
| 3331 | } |
| 3332 | llvm::sort(C&: Defs, Comp: LessRecord()); |
| 3333 | return Defs; |
| 3334 | } |
| 3335 | |
| 3336 | ArrayRef<const Record *> |
| 3337 | RecordKeeper::getAllDerivedDefinitionsIfDefined(StringRef ClassName) const { |
| 3338 | if (getClass(Name: ClassName)) |
| 3339 | return getAllDerivedDefinitions(ClassName); |
| 3340 | return Cache["" ]; |
| 3341 | } |
| 3342 | |
| 3343 | void RecordKeeper::dumpAllocationStats(raw_ostream &OS) const { |
| 3344 | Impl->dumpAllocationStats(OS); |
| 3345 | } |
| 3346 | |
| 3347 | const Init *MapResolver::resolve(const Init *VarName) { |
| 3348 | auto It = Map.find(Val: VarName); |
| 3349 | if (It == Map.end()) |
| 3350 | return nullptr; |
| 3351 | |
| 3352 | const Init *I = It->second.V; |
| 3353 | |
| 3354 | if (!It->second.Resolved && Map.size() > 1) { |
| 3355 | // Resolve mutual references among the mapped variables, but prevent |
| 3356 | // infinite recursion. |
| 3357 | Map.erase(I: It); |
| 3358 | I = I->resolveReferences(R&: *this); |
| 3359 | Map[VarName] = {I, true}; |
| 3360 | } |
| 3361 | |
| 3362 | return I; |
| 3363 | } |
| 3364 | |
| 3365 | const Init *RecordResolver::resolve(const Init *VarName) { |
| 3366 | const Init *Val = Cache.lookup(Val: VarName); |
| 3367 | if (Val) |
| 3368 | return Val; |
| 3369 | |
| 3370 | if (llvm::is_contained(Range&: Stack, Element: VarName)) |
| 3371 | return nullptr; // prevent infinite recursion |
| 3372 | |
| 3373 | if (const RecordVal *RV = getCurrentRecord()->getValue(Name: VarName)) { |
| 3374 | if (!isa<UnsetInit>(Val: RV->getValue())) { |
| 3375 | Val = RV->getValue(); |
| 3376 | Stack.push_back(Elt: VarName); |
| 3377 | Val = Val->resolveReferences(R&: *this); |
| 3378 | Stack.pop_back(); |
| 3379 | } |
| 3380 | } else if (Name && VarName == getCurrentRecord()->getNameInit()) { |
| 3381 | Stack.push_back(Elt: VarName); |
| 3382 | Val = Name->resolveReferences(R&: *this); |
| 3383 | Stack.pop_back(); |
| 3384 | } |
| 3385 | |
| 3386 | Cache[VarName] = Val; |
| 3387 | return Val; |
| 3388 | } |
| 3389 | |
| 3390 | const Init *TrackUnresolvedResolver::resolve(const Init *VarName) { |
| 3391 | const Init *I = nullptr; |
| 3392 | |
| 3393 | if (R) { |
| 3394 | I = R->resolve(VarName); |
| 3395 | if (I && !FoundUnresolved) { |
| 3396 | // Do not recurse into the resolved initializer, as that would change |
| 3397 | // the behavior of the resolver we're delegating, but do check to see |
| 3398 | // if there are unresolved variables remaining. |
| 3399 | TrackUnresolvedResolver Sub; |
| 3400 | I->resolveReferences(R&: Sub); |
| 3401 | FoundUnresolved |= Sub.FoundUnresolved; |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | if (!I) |
| 3406 | FoundUnresolved = true; |
| 3407 | return I; |
| 3408 | } |
| 3409 | |
| 3410 | const Init *HasReferenceResolver::resolve(const Init *VarName) { |
| 3411 | if (VarName == VarNameToTrack) |
| 3412 | Found = true; |
| 3413 | return nullptr; |
| 3414 | } |
| 3415 | |