xref: /src/contrib/llvm-project/llvm/lib/TextAPI/ArchitectureSet.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1e6d15924SDimitry Andric //===- ArchitectureSet.cpp ------------------------------------------------===//
2e6d15924SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e6d15924SDimitry Andric //
7e6d15924SDimitry Andric //===----------------------------------------------------------------------===//
8e6d15924SDimitry Andric //
9e6d15924SDimitry Andric // Implements the architecture set.
10e6d15924SDimitry Andric //
11e6d15924SDimitry Andric //===----------------------------------------------------------------------===//
12e6d15924SDimitry Andric 
13344a3780SDimitry Andric #include "llvm/TextAPI/ArchitectureSet.h"
14cfca06d7SDimitry Andric #include "llvm/Support/raw_ostream.h"
15e6d15924SDimitry Andric 
16e6d15924SDimitry Andric namespace llvm {
17e6d15924SDimitry Andric namespace MachO {
18e6d15924SDimitry Andric 
ArchitectureSet(const std::vector<Architecture> & Archs)19e6d15924SDimitry Andric ArchitectureSet::ArchitectureSet(const std::vector<Architecture> &Archs)
20e6d15924SDimitry Andric     : ArchitectureSet() {
21e6d15924SDimitry Andric   for (auto Arch : Archs) {
22e6d15924SDimitry Andric     if (Arch == AK_unknown)
23e6d15924SDimitry Andric       continue;
24e6d15924SDimitry Andric     set(Arch);
25e6d15924SDimitry Andric   }
26e6d15924SDimitry Andric }
27e6d15924SDimitry Andric 
count() const28e6d15924SDimitry Andric size_t ArchitectureSet::count() const {
29e6d15924SDimitry Andric   // popcnt
30e6d15924SDimitry Andric   size_t Cnt = 0;
31e6d15924SDimitry Andric   for (unsigned i = 0; i < sizeof(ArchSetType) * 8; ++i)
32e6d15924SDimitry Andric     if (ArchSet & (1U << i))
33e6d15924SDimitry Andric       ++Cnt;
34e6d15924SDimitry Andric   return Cnt;
35e6d15924SDimitry Andric }
36e6d15924SDimitry Andric 
operator std::string() const37e6d15924SDimitry Andric ArchitectureSet::operator std::string() const {
38e6d15924SDimitry Andric   if (empty())
39e6d15924SDimitry Andric     return "[(empty)]";
40e6d15924SDimitry Andric 
41e6d15924SDimitry Andric   std::string result;
42e6d15924SDimitry Andric   auto size = count();
43e6d15924SDimitry Andric   for (auto arch : *this) {
44cfca06d7SDimitry Andric     result.append(std::string(getArchitectureName(arch)));
45e6d15924SDimitry Andric     size -= 1;
46e6d15924SDimitry Andric     if (size)
47e6d15924SDimitry Andric       result.append(" ");
48e6d15924SDimitry Andric   }
49e6d15924SDimitry Andric   return result;
50e6d15924SDimitry Andric }
51e6d15924SDimitry Andric 
operator std::vector<Architecture>() const52e6d15924SDimitry Andric ArchitectureSet::operator std::vector<Architecture>() const {
53e6d15924SDimitry Andric   std::vector<Architecture> archs;
54e6d15924SDimitry Andric   for (auto arch : *this) {
55e6d15924SDimitry Andric     if (arch == AK_unknown)
56e6d15924SDimitry Andric       continue;
57e6d15924SDimitry Andric     archs.emplace_back(arch);
58e6d15924SDimitry Andric   }
59e6d15924SDimitry Andric   return archs;
60e6d15924SDimitry Andric }
61e6d15924SDimitry Andric 
print(raw_ostream & os) const62e6d15924SDimitry Andric void ArchitectureSet::print(raw_ostream &os) const { os << std::string(*this); }
63e6d15924SDimitry Andric 
operator <<(raw_ostream & os,ArchitectureSet set)64e6d15924SDimitry Andric raw_ostream &operator<<(raw_ostream &os, ArchitectureSet set) {
65e6d15924SDimitry Andric   set.print(os);
66e6d15924SDimitry Andric   return os;
67e6d15924SDimitry Andric }
68e6d15924SDimitry Andric 
69e6d15924SDimitry Andric } // end namespace MachO.
70e6d15924SDimitry Andric } // end namespace llvm.
71