mirror of
https://github.com/RPCS3/llvm.git
synced 2026-01-31 01:25:19 +01:00
The loop in findNextSetBit() runs one pass more than it should. On 64-bit architectures this does not cause a problem, but 32-bit architectures mask the shift count to 5 bits which limits the number of shifts inside a range of 0 to 31. Shifting by 32 has the same effect as shifting by 0, so if the first bit in the set is 1, the function will return with Index different from EndIndexVal. Because of that, range-based for loops iterating thorough architectures will continue until hitting a 0 in the set, resulting in n additional iterations, where n is equal to the number of consecutive 1 bits at the start the set. Ultimately TBDv1.WriteFile and TBDv2.WriteFile will output additional architectures causing a failure in the unit tests. Patch by Milos Stojanovic. Differential Revision: https://reviews.llvm.org/D60198 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357759 91177308-0d34-0410-b5e6-96231b3b80d8
160 lines
4.2 KiB
C++
160 lines
4.2 KiB
C++
//===- llvm/TextAPI/MachO/ArchitectureSet.h - ArchitectureSet ---*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Defines the architecture set.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_TEXTAPI_MACHO_ARCHITECTURE_SET_H
|
|
#define LLVM_TEXTAPI_MACHO_ARCHITECTURE_SET_H
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include "llvm/TextAPI/MachO/Architecture.h"
|
|
#include <cstddef>
|
|
#include <iterator>
|
|
#include <limits>
|
|
#include <vector>
|
|
|
|
namespace llvm {
|
|
namespace MachO {
|
|
|
|
class ArchitectureSet {
|
|
private:
|
|
using ArchSetType = uint32_t;
|
|
|
|
const static ArchSetType EndIndexVal =
|
|
std::numeric_limits<ArchSetType>::max();
|
|
ArchSetType ArchSet{0};
|
|
|
|
public:
|
|
constexpr ArchitectureSet() = default;
|
|
constexpr ArchitectureSet(ArchSetType Raw) : ArchSet(Raw) {}
|
|
ArchitectureSet(Architecture Arch) : ArchitectureSet() { set(Arch); }
|
|
ArchitectureSet(const std::vector<Architecture> &Archs);
|
|
|
|
void set(Architecture Arch) {
|
|
if (Arch == AK_unknown)
|
|
return;
|
|
ArchSet |= 1U << static_cast<int>(Arch);
|
|
}
|
|
|
|
void clear(Architecture Arch) { ArchSet &= ~(1U << static_cast<int>(Arch)); }
|
|
|
|
bool has(Architecture Arch) const {
|
|
return ArchSet & (1U << static_cast<int>(Arch));
|
|
}
|
|
|
|
bool contains(ArchitectureSet Archs) const {
|
|
return (ArchSet & Archs.ArchSet) == Archs.ArchSet;
|
|
}
|
|
|
|
size_t count() const;
|
|
|
|
bool empty() const { return ArchSet == 0; }
|
|
|
|
ArchSetType rawValue() const { return ArchSet; }
|
|
|
|
template <typename Ty>
|
|
class arch_iterator
|
|
: public std::iterator<std::forward_iterator_tag, Architecture, size_t> {
|
|
private:
|
|
ArchSetType Index;
|
|
Ty *ArchSet;
|
|
|
|
void findNextSetBit() {
|
|
if (Index == EndIndexVal)
|
|
return;
|
|
while (++Index < sizeof(Ty) * 8) {
|
|
if (*ArchSet & (1UL << Index))
|
|
return;
|
|
}
|
|
|
|
Index = EndIndexVal;
|
|
}
|
|
|
|
public:
|
|
arch_iterator(Ty *ArchSet, ArchSetType Index = 0)
|
|
: Index(Index), ArchSet(ArchSet) {
|
|
if (Index != EndIndexVal && !(*ArchSet & (1UL << Index)))
|
|
findNextSetBit();
|
|
}
|
|
|
|
Architecture operator*() const { return static_cast<Architecture>(Index); }
|
|
|
|
arch_iterator &operator++() {
|
|
findNextSetBit();
|
|
return *this;
|
|
}
|
|
|
|
arch_iterator operator++(int) {
|
|
auto tmp = *this;
|
|
findNextSetBit();
|
|
return tmp;
|
|
}
|
|
|
|
bool operator==(const arch_iterator &o) const {
|
|
return std::tie(Index, ArchSet) == std::tie(o.Index, o.ArchSet);
|
|
}
|
|
|
|
bool operator!=(const arch_iterator &o) const { return !(*this == o); }
|
|
};
|
|
|
|
ArchitectureSet operator&(const ArchitectureSet &o) {
|
|
return {ArchSet & o.ArchSet};
|
|
}
|
|
|
|
ArchitectureSet operator|(const ArchitectureSet &o) {
|
|
return {ArchSet | o.ArchSet};
|
|
}
|
|
|
|
ArchitectureSet &operator|=(const ArchitectureSet &o) {
|
|
ArchSet |= o.ArchSet;
|
|
return *this;
|
|
}
|
|
|
|
ArchitectureSet &operator|=(const Architecture &Arch) {
|
|
set(Arch);
|
|
return *this;
|
|
}
|
|
|
|
bool operator==(const ArchitectureSet &o) const {
|
|
return ArchSet == o.ArchSet;
|
|
}
|
|
|
|
bool operator!=(const ArchitectureSet &o) const {
|
|
return ArchSet != o.ArchSet;
|
|
}
|
|
|
|
bool operator<(const ArchitectureSet &o) const { return ArchSet < o.ArchSet; }
|
|
|
|
using iterator = arch_iterator<ArchSetType>;
|
|
using const_iterator = arch_iterator<const ArchSetType>;
|
|
|
|
iterator begin() { return {&ArchSet}; }
|
|
iterator end() { return {&ArchSet, EndIndexVal}; }
|
|
|
|
const_iterator begin() const { return {&ArchSet}; }
|
|
const_iterator end() const { return {&ArchSet, EndIndexVal}; }
|
|
|
|
operator std::string() const;
|
|
operator std::vector<Architecture>() const;
|
|
void print(raw_ostream &OS) const;
|
|
};
|
|
|
|
inline ArchitectureSet operator|(const Architecture &lhs,
|
|
const Architecture &rhs) {
|
|
return ArchitectureSet(lhs) | ArchitectureSet(rhs);
|
|
}
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, ArchitectureSet Set);
|
|
|
|
} // end namespace MachO.
|
|
} // end namespace llvm.
|
|
|
|
#endif // LLVM_TEXTAPI_MACHO_ARCHITECTURE_SET_H
|