| 1 | //===- Pass.cpp - Passes that operate on Sandbox IR -----------------------===// |
|---|---|
| 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 | #include "llvm/SandboxIR/Pass.h" |
| 10 | #include "llvm/Support/Debug.h" |
| 11 | |
| 12 | using namespace llvm::sandboxir; |
| 13 | |
| 14 | #ifndef NDEBUG |
| 15 | void Pass::dump() const { |
| 16 | print(dbgs()); |
| 17 | dbgs() << "\n"; |
| 18 | } |
| 19 | #endif // NDEBUG |
| 20 | |
| 21 | bool AuxPassArg::set(bool NewVal) { |
| 22 | Registry->Entries[ArgIdx].Val = NewVal; |
| 23 | return NewVal; |
| 24 | } |
| 25 | |
| 26 | bool AuxPassArg::get() const { return Registry->Entries[ArgIdx].Val; } |
| 27 | |
| 28 | llvm::StringRef AuxPassArg::getFlagStr() const { |
| 29 | return Registry->Entries[ArgIdx].FlagStr; |
| 30 | } |
| 31 | |
| 32 | #ifndef NDEBUG |
| 33 | void AuxPassArg::print(raw_ostream &OS) const { |
| 34 | auto &E = Registry->Entries[ArgIdx]; |
| 35 | OS << E.FlagStr << " : "<< E.Val; |
| 36 | } |
| 37 | |
| 38 | void AuxPassArg::dump() const { |
| 39 | print(dbgs()); |
| 40 | dbgs() << "\n"; |
| 41 | } |
| 42 | #endif |
| 43 | |
| 44 | AuxPassArgsRegistry::Entry *AuxPassArgsRegistry::getEntry(StringRef Flag) { |
| 45 | for (Entry &E : Entries) { |
| 46 | if (E.FlagStr == Flag) |
| 47 | return &E; |
| 48 | } |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | AuxPassArg AuxPassArgsRegistry::createArg(StringRef Flag) { |
| 53 | AuxPassArg NewArg(Entries.size(), this); |
| 54 | Entries.emplace_back(Args&: Flag, Args: false); |
| 55 | return NewArg; |
| 56 | } |
| 57 | |
| 58 | void AuxPassArgsRegistry::parse(StringRef ArgsStr) { |
| 59 | SmallVector<StringRef> Parts; |
| 60 | ArgsStr.split(A&: Parts, Separator: ','); |
| 61 | for (StringRef Part : Parts) { |
| 62 | if (Part.empty()) |
| 63 | continue; |
| 64 | Entry *E = getEntry(Flag: Part); |
| 65 | if (E == nullptr) { |
| 66 | std::string ErrStr; |
| 67 | raw_string_ostream ErrSS(ErrStr); |
| 68 | ErrSS << "Unsupported argument: '"<< Part |
| 69 | << "'. List of supported args:\n"; |
| 70 | for (const auto &[ArgStr, Val] : Entries) |
| 71 | ErrSS << " '"<< ArgStr << "'\n"; |
| 72 | reportFatalUsageError(reason: ErrStr.c_str()); |
| 73 | } |
| 74 | *E = {Part, true}; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #ifndef NDEBUG |
| 79 | void AuxPassArgsRegistry::print(raw_ostream &OS) const { |
| 80 | if (Entries.empty()) { |
| 81 | OS << "No args created yet!\n"; |
| 82 | return; |
| 83 | } |
| 84 | for (const Entry &E : Entries) |
| 85 | OS << E.FlagStr << " : "<< E.Val << "\n"; |
| 86 | } |
| 87 | |
| 88 | void AuxPassArgsRegistry::dump() const { |
| 89 | print(dbgs()); |
| 90 | dbgs() << "\n"; |
| 91 | } |
| 92 | #endif |
| 93 |