ArchitectureSet.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===- ArchitectureSet.cpp ------------------------------------------------===//
  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. // Implements the architecture set.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/TextAPI/ArchitectureSet.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. namespace llvm {
  15. namespace MachO {
  16. ArchitectureSet::ArchitectureSet(const std::vector<Architecture> &Archs)
  17. : ArchitectureSet() {
  18. for (auto Arch : Archs) {
  19. if (Arch == AK_unknown)
  20. continue;
  21. set(Arch);
  22. }
  23. }
  24. size_t ArchitectureSet::count() const {
  25. // popcnt
  26. size_t Cnt = 0;
  27. for (unsigned i = 0; i < sizeof(ArchSetType) * 8; ++i)
  28. if (ArchSet & (1U << i))
  29. ++Cnt;
  30. return Cnt;
  31. }
  32. ArchitectureSet::operator std::string() const {
  33. if (empty())
  34. return "[(empty)]";
  35. std::string result;
  36. auto size = count();
  37. for (auto arch : *this) {
  38. result.append(std::string(getArchitectureName(arch)));
  39. size -= 1;
  40. if (size)
  41. result.append(" ");
  42. }
  43. return result;
  44. }
  45. ArchitectureSet::operator std::vector<Architecture>() const {
  46. std::vector<Architecture> archs;
  47. for (auto arch : *this) {
  48. if (arch == AK_unknown)
  49. continue;
  50. archs.emplace_back(arch);
  51. }
  52. return archs;
  53. }
  54. void ArchitectureSet::print(raw_ostream &os) const { os << std::string(*this); }
  55. raw_ostream &operator<<(raw_ostream &os, ArchitectureSet set) {
  56. set.print(os);
  57. return os;
  58. }
  59. } // end namespace MachO.
  60. } // end namespace llvm.