From b9b66b9244fc176fb338df9503e044d3fefa67cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Mon, 25 Jun 2018 11:54:45 +0200 Subject: [PATCH] Refactor switch compiler factories --- src/CodeGenerator.cpp | 10 ++++------ src/CodeGenerator.hpp | 4 ++-- src/NativeSwitchCompiler.cpp | 6 +++--- src/NativeSwitchCompiler.hpp | 4 ++-- src/SwitchStatement.cpp | 17 ++++++++--------- src/SwitchStatement.hpp | 32 +++++--------------------------- src/main.cpp | 2 +- 7 files changed, 25 insertions(+), 50 deletions(-) diff --git a/src/CodeGenerator.cpp b/src/CodeGenerator.cpp index 4c07022..fe95f9e 100644 --- a/src/CodeGenerator.cpp +++ b/src/CodeGenerator.cpp @@ -43,9 +43,9 @@ CodeGenerator::CodeGenerator( const SimpleDwarf& dwarf, std::ostream& os, NamingScheme naming_scheme, - AbstractSwitchCompilerFactory* factory) : + AbstractSwitchCompiler* sw_compiler) : dwarf(dwarf), os(os), pc_list(nullptr), - naming_scheme(naming_scheme), switch_factory(factory) + naming_scheme(naming_scheme), switch_compiler(sw_compiler) { if(!settings::pc_list.empty()) { pc_list = make_unique(settings::pc_list); @@ -122,8 +122,7 @@ void CodeGenerator::gen_of_dwarf() { SwitchStatement sw_stmt = gen_fresh_switch(); for(const auto& fde: dwarf.fde_list) switch_append_fde(sw_stmt, fde); - auto sw_compiler = (*switch_factory)(sw_stmt); - (*sw_compiler)(os); + (*switch_compiler)(os, sw_stmt); gen_unwind_func_footer(); break; } @@ -150,8 +149,7 @@ void CodeGenerator::gen_function_of_fde(const SimpleDwarf::Fde& fde) { SwitchStatement sw_stmt = gen_fresh_switch(); switch_append_fde(sw_stmt, fde); - auto sw_compiler = (*switch_factory)(sw_stmt); - (*sw_compiler)(os); + (*switch_compiler)(os, sw_stmt); gen_unwind_func_footer(); } diff --git a/src/CodeGenerator.hpp b/src/CodeGenerator.hpp index 9434825..6ddb748 100644 --- a/src/CodeGenerator.hpp +++ b/src/CodeGenerator.hpp @@ -25,7 +25,7 @@ class CodeGenerator { * given std::ostream object (eg. cout). */ CodeGenerator(const SimpleDwarf& dwarf, std::ostream& os, NamingScheme naming_scheme, - AbstractSwitchCompilerFactory* factory); + AbstractSwitchCompiler* sw_compiler); /// Actually generate the code on the given stream void generate(); @@ -64,5 +64,5 @@ class CodeGenerator { NamingScheme naming_scheme; - std::unique_ptr switch_factory; + std::unique_ptr switch_compiler; }; diff --git a/src/NativeSwitchCompiler.cpp b/src/NativeSwitchCompiler.cpp index b27ef3d..d46f94e 100644 --- a/src/NativeSwitchCompiler.cpp +++ b/src/NativeSwitchCompiler.cpp @@ -3,11 +3,11 @@ using namespace std; NativeSwitchCompiler::NativeSwitchCompiler( - const SwitchStatement& sw, int indent): - AbstractSwitchCompiler(sw, indent) + int indent): + AbstractSwitchCompiler(indent) {} -void NativeSwitchCompiler::to_stream(ostream& os) { +void NativeSwitchCompiler::to_stream(ostream& os, const SwitchStatement& sw) { os << indent() << "switch(" << sw.switch_var << ") {\n"; indent_count++; diff --git a/src/NativeSwitchCompiler.hpp b/src/NativeSwitchCompiler.hpp index bc36544..c726ddd 100644 --- a/src/NativeSwitchCompiler.hpp +++ b/src/NativeSwitchCompiler.hpp @@ -4,7 +4,7 @@ class NativeSwitchCompiler: public AbstractSwitchCompiler { public: - NativeSwitchCompiler(const SwitchStatement& sw, int indent=0); + NativeSwitchCompiler(int indent=0); private: - virtual void to_stream(std::ostream& os); + virtual void to_stream(std::ostream& os, const SwitchStatement& sw); }; diff --git a/src/SwitchStatement.cpp b/src/SwitchStatement.cpp index 97b7a45..0fe6012 100644 --- a/src/SwitchStatement.cpp +++ b/src/SwitchStatement.cpp @@ -5,19 +5,20 @@ using namespace std; AbstractSwitchCompiler::AbstractSwitchCompiler( - const SwitchStatement& sw, int indent) - : sw(sw), indent_count(indent) + : indent_count(indent) { } -void AbstractSwitchCompiler::operator()(ostream& os) { - to_stream(os); +void AbstractSwitchCompiler::operator()( + ostream& os, const SwitchStatement& sw) +{ + to_stream(os, sw); } -string AbstractSwitchCompiler::operator()() { +string AbstractSwitchCompiler::operator()(const SwitchStatement& sw) { ostringstream os; - (*this)(os); + (*this)(os, sw); return os.str(); } @@ -31,7 +32,7 @@ std::string AbstractSwitchCompiler::indent_str(const std::string& str) { << str.substr(last_find + 1, find_pos - last_find); // includes \n last_find = find_pos; } - if(last_find + 1 < (int)str.size()) { + if(last_find + 1 < (ssize_t)str.size()) { out << indent() << str.substr(last_find + 1) << '\n'; @@ -45,5 +46,3 @@ std::string AbstractSwitchCompiler::indent() const { std::string AbstractSwitchCompiler::endcl() const { return string("\n") + indent(); } - -AbstractSwitchCompilerFactory::AbstractSwitchCompilerFactory() {} diff --git a/src/SwitchStatement.hpp b/src/SwitchStatement.hpp index 565008f..68029e8 100644 --- a/src/SwitchStatement.hpp +++ b/src/SwitchStatement.hpp @@ -21,38 +21,16 @@ struct SwitchStatement { class AbstractSwitchCompiler { public: - AbstractSwitchCompiler(const SwitchStatement& sw, - int indent=0); - void operator()(std::ostream& os); - std::string operator()(); + AbstractSwitchCompiler(int indent=0); + void operator()(std::ostream& os, const SwitchStatement& sw); + std::string operator()(const SwitchStatement& sw); protected: - virtual void to_stream(std::ostream& os) = 0; + virtual void to_stream( + std::ostream& os, const SwitchStatement& sw) = 0; std::string indent_str(const std::string& str) ; std::string indent() const; std::string endcl() const; - - SwitchStatement sw; int indent_count; }; - -class AbstractSwitchCompilerFactory { - public: - AbstractSwitchCompilerFactory(); - virtual std::shared_ptr operator()( - const SwitchStatement& sw, - int indent=0) = 0; -}; - -template -class SwitchCompilerFactory : public AbstractSwitchCompilerFactory { - public: - virtual std::shared_ptr operator()( - const SwitchStatement& sw, - int indent=0) - { - return std::shared_ptr( - new Compiler(sw, indent)); - } -}; diff --git a/src/main.cpp b/src/main.cpp index 7ac3230..4bf58be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -109,7 +109,7 @@ int main(int argc, char** argv) { ss << "_fde_" << fde.beg_ip; return ss.str(); }, - new SwitchCompilerFactory()); + new NativeSwitchCompiler()); code_gen.generate(); return 0;