From edff5c2b63f5e4fcd33ea4be4584c764510ce280 Mon Sep 17 00:00:00 2001 From: Su Xing Date: Thu, 23 Mar 2023 17:30:08 +0800 Subject: [PATCH] Use std::set as FunctionType storage instead of std::unordered_set --- src/CMakeLists.txt | 1 + src/IR.cpp | 32 ++++++++++++-------------------- src/IR.h | 6 +++--- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d0bc1c6..d5c1b45 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) diff --git a/src/IR.cpp b/src/IR.cpp index 13de99d..2a64b5e 100644 --- a/src/IR.cpp +++ b/src/IR.cpp @@ -5,32 +5,15 @@ #include #include #include -#include +#include #include -namespace std { +namespace sysy { //===----------------------------------------------------------------------===// // Types //===----------------------------------------------------------------------===// -template <> struct hash { - size_t operator()(const sysy::FunctionType &type) { - auto num = 1 + type.getNumParams(); - vector buffer(1, type.getReturnType()); - copy(type.getParamTypes().begin(), type.getParamTypes().end(), - back_inserter(buffer)); - string bytes(reinterpret_cast(buffer.data()), - buffer.size() * sizeof(sysy::Type *)); - hash 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 ¶mTypes) { - static std::unordered_set functionTypes; + static std::set 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 diff --git a/src/IR.h b/src/IR.h index 5f53177..beb3114 100644 --- a/src/IR.h +++ b/src/IR.h @@ -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 paramTypes; -protected: +public: FunctionType(Type *returnType) : Type(kFunction), returnType(returnType) {} FunctionType(Type *returnType, const std::vector ¶mTypes = {}) : Type(kFunction), returnType(returnType), paramTypes(paramTypes) {} @@ -611,7 +611,7 @@ public: }; GlobalValue *addGlobalValue(Type *type, const std::vector &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