blob: f665706fad811763fdcfb1ba48340716f3fedfc9 [file] [log] [blame]
Juergen Ributzka32cb5942019-03-22 22:46:52 +00001//===- 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
13#include "llvm/TextAPI/MachO/ArchitectureSet.h"
Simon Pilgrimd1765cf2020-05-30 12:36:16 +010014#include "llvm/Support/raw_ostream.h"
Juergen Ributzka32cb5942019-03-22 22:46:52 +000015
16namespace llvm {
17namespace MachO {
18
19ArchitectureSet::ArchitectureSet(const std::vector<Architecture> &Archs)
20 : ArchitectureSet() {
21 for (auto Arch : Archs) {
Juergen Ributzka875565e2019-04-04 22:56:50 +000022 if (Arch == AK_unknown)
Juergen Ributzka32cb5942019-03-22 22:46:52 +000023 continue;
24 set(Arch);
25 }
26}
27
28size_t ArchitectureSet::count() const {
29 // popcnt
30 size_t Cnt = 0;
31 for (unsigned i = 0; i < sizeof(ArchSetType) * 8; ++i)
32 if (ArchSet & (1U << i))
33 ++Cnt;
34 return Cnt;
35}
36
37ArchitectureSet::operator std::string() const {
38 if (empty())
39 return "[(empty)]";
40
41 std::string result;
42 auto size = count();
43 for (auto arch : *this) {
Benjamin Krameradcd0262020-01-28 20:23:46 +010044 result.append(std::string(getArchitectureName(arch)));
Juergen Ributzka32cb5942019-03-22 22:46:52 +000045 size -= 1;
46 if (size)
47 result.append(" ");
48 }
49 return result;
50}
51
52ArchitectureSet::operator std::vector<Architecture>() const {
53 std::vector<Architecture> archs;
54 for (auto arch : *this) {
Juergen Ributzka875565e2019-04-04 22:56:50 +000055 if (arch == AK_unknown)
Juergen Ributzka32cb5942019-03-22 22:46:52 +000056 continue;
57 archs.emplace_back(arch);
58 }
59 return archs;
60}
61
62void ArchitectureSet::print(raw_ostream &os) const { os << std::string(*this); }
63
64raw_ostream &operator<<(raw_ostream &os, ArchitectureSet set) {
65 set.print(os);
66 return os;
67}
68
69} // end namespace MachO.
70} // end namespace llvm.