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