Refactor switch compiler factories
This commit is contained in:
parent
6fef1c5444
commit
b9b66b9244
7 changed files with 25 additions and 50 deletions
|
@ -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<PcListReader>(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();
|
||||
}
|
||||
|
|
|
@ -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<AbstractSwitchCompilerFactory> switch_factory;
|
||||
std::unique_ptr<AbstractSwitchCompiler> switch_compiler;
|
||||
};
|
||||
|
|
|
@ -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++;
|
||||
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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() {}
|
||||
|
|
|
@ -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<AbstractSwitchCompiler> operator()(
|
||||
const SwitchStatement& sw,
|
||||
int indent=0) = 0;
|
||||
};
|
||||
|
||||
template<class Compiler>
|
||||
class SwitchCompilerFactory : public AbstractSwitchCompilerFactory {
|
||||
public:
|
||||
virtual std::shared_ptr<AbstractSwitchCompiler> operator()(
|
||||
const SwitchStatement& sw,
|
||||
int indent=0)
|
||||
{
|
||||
return std::shared_ptr<AbstractSwitchCompiler>(
|
||||
new Compiler(sw, indent));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -109,7 +109,7 @@ int main(int argc, char** argv) {
|
|||
ss << "_fde_" << fde.beg_ip;
|
||||
return ss.str();
|
||||
},
|
||||
new SwitchCompilerFactory<NativeSwitchCompiler>());
|
||||
new NativeSwitchCompiler());
|
||||
code_gen.generate();
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue