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