Use std::set as FunctionType storage instead of std::unordered_set

main
Su Xing 3 years ago
parent 9f1d1628cf
commit edff5c2b63

@ -15,6 +15,7 @@ target_link_libraries(SysYParser PUBLIC antlr4_shared)
add_executable(sysyc
sysyc.cpp
SysYFormatter.cpp
IR.cpp
)
target_include_directories(sysyc PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(sysyc PRIVATE SysYParser)

@ -5,32 +5,15 @@
#include <iterator>
#include <map>
#include <memory>
#include <unordered_set>
#include <set>
#include <vector>
namespace std {
namespace sysy {
//===----------------------------------------------------------------------===//
// Types
//===----------------------------------------------------------------------===//
template <> struct hash<sysy::FunctionType> {
size_t operator()(const sysy::FunctionType &type) {
auto num = 1 + type.getNumParams();
vector<sysy::Type *> buffer(1, type.getReturnType());
copy(type.getParamTypes().begin(), type.getParamTypes().end(),
back_inserter(buffer));
string bytes(reinterpret_cast<char *>(buffer.data()),
buffer.size() * sizeof(sysy::Type *));
hash<string> hasher;
return hasher(bytes);
}
};
} // namespace std
namespace sysy {
Type *Type::getIntType() {
static Type intType(kInt);
return &intType;
@ -68,9 +51,18 @@ PointerType *PointerType::get(Type *baseType) {
return &(result.first->second);
}
bool operator<(const FunctionType &lhs, const FunctionType &rhs) {
return lhs.getReturnType() < rhs.getReturnType() or
lhs.getParamTypes().size() < rhs.getParamTypes().size() and
std::lexicographical_compare(lhs.getParamTypes().begin(),
lhs.getParamTypes().end(),
rhs.getParamTypes().begin(),
rhs.getParamTypes().end());
}
FunctionType *FunctionType::get(Type *returnType,
const std::vector<Type *> &paramTypes) {
static std::unordered_set<FunctionType> functionTypes;
static std::set<FunctionType> functionTypes;
auto result = functionTypes.emplace(returnType, paramTypes);
// STL set objects are immutable, sothe emplace method returns const_iterator.
// We are sure FunctionType pointers cannot be used to mutate the underlying

@ -78,7 +78,7 @@ class PointerType : public Type {
private:
Type *baseType;
protected:
public:
PointerType(Type *baseType) : Type(kPointer), baseType(baseType) {}
public:
@ -93,7 +93,7 @@ private:
Type *returnType;
std::vector<Type *> paramTypes;
protected:
public:
FunctionType(Type *returnType) : Type(kFunction), returnType(returnType) {}
FunctionType(Type *returnType, const std::vector<Type *> &paramTypes = {})
: Type(kFunction), returnType(returnType), paramTypes(paramTypes) {}
@ -611,7 +611,7 @@ public:
};
GlobalValue *addGlobalValue(Type *type, const std::vector<Value *> &dims = {},
const std::string &name = "") {
globals.emplace_back(type, dims, name);
globals.emplace_back(new GlobalValue(type, dims, name));
return globals.back().get();
}
}; // class Module

Loading…
Cancel
Save