#ifndef wasm_analysis_lattice_h
#define wasm_analysis_lattice_h
#include <iostream>
#include <unordered_map>
#include <vector>
#include "wasm.h"
namespace wasm::analysis {
enum LatticeComparison { NO_RELATION, EQUAL, LESS, GREATER };
inline LatticeComparison reverseComparison(LatticeComparison comparison) {
if (comparison == LatticeComparison::LESS) {
return LatticeComparison::GREATER;
} else if (comparison == LatticeComparison::GREATER) {
return LatticeComparison::LESS;
} else {
return comparison;
}
}
template<typename Lattice>
constexpr bool has_getBottom =
std::is_invocable_r<typename Lattice::Element,
decltype(&Lattice::getBottom),
Lattice>::value;
template<typename Lattice>
constexpr bool has_compare =
std::is_invocable_r<LatticeComparison,
decltype(&Lattice::compare),
Lattice,
const typename Lattice::Element&,
const typename Lattice::Element&>::value;
template<typename Element>
constexpr bool has_makeLeastUpperBound =
std::is_invocable_r<void,
decltype(&Element::makeLeastUpperBound),
Element,
const Element&>::value;
template<typename Element>
constexpr bool has_isTop =
std::is_invocable_r<bool, decltype(&Element::isTop), const Element>::value;
template<typename Element>
constexpr bool has_isBottom =
std::is_invocable_r<bool, decltype(&Element::isBottom), const Element>::value;
template<typename Lattice>
constexpr bool is_lattice = has_getBottom<Lattice>&& has_compare<Lattice>&&
has_makeLeastUpperBound<typename Lattice::Element>&& has_isTop<
typename Lattice::Element>&& has_isBottom<typename Lattice::Element>;
class FiniteIntPowersetLattice {
size_t setSize;
public:
FiniteIntPowersetLattice(size_t setSize) : setSize(setSize) {}
size_t getSetSize() { return setSize; }
class Element {
std::vector<bool> bitvector;
Element(size_t latticeSetSize) : bitvector(latticeSetSize) {}
public:
Element(Element&& source) = default;
Element(const Element& source) = default;
Element& operator=(Element&& source) = default;
Element& operator=(const Element& source) = default;
size_t count() const;
bool get(size_t index) { return bitvector[index]; }
void set(size_t index, bool value) { bitvector[index] = value; }
bool isTop() const { return count() == bitvector.size(); }
bool isBottom() const { return count() == 0; }
bool makeLeastUpperBound(const Element& other);
void print(std::ostream& os);
friend FiniteIntPowersetLattice;
};
LatticeComparison compare(const Element& left, const Element& right);
Element getBottom();
};
template<typename T> class FinitePowersetLattice {
FiniteIntPowersetLattice intLattice;
std::vector<T> members;
std::unordered_map<T, size_t> memberIndices;
public:
using Element = FiniteIntPowersetLattice::Element;
FinitePowersetLattice(std::vector<T>&& setMembers)
: intLattice(setMembers.size()), members(std::move(setMembers)) {
for (size_t i = 0; i < members.size(); ++i) {
memberIndices[members[i]] = i;
}
}
using membersIterator = typename std::vector<T>::const_iterator;
membersIterator membersBegin() { return members.cbegin(); }
membersIterator membersEnd() { return members.cend(); }
size_t getSetSize() { return intLattice.getSetSize(); }
T indexToMember(size_t index) { return members[index]; }
size_t memberToIndex(T member) { return memberIndices[member]; }
void add(Element* element, T member) {
element->set(memberIndices[member], true);
}
void remove(Element* element, T member) {
element->set(memberIndices[member], false);
}
bool exists(Element* element, T member) {
return element->get(memberIndices[member]);
}
LatticeComparison compare(const Element& left, const Element& right) {
return intLattice.compare(left, right);
}
Element getBottom() { return intLattice.getBottom(); }
};
}
#include "powerset-lattice-impl.h"
#endif