dwarf-assembly/src/CodeGenerator.hpp

69 lines
2.1 KiB
C++
Raw Normal View History

2018-04-23 18:58:05 +02:00
/** Generates C code from SimpleDwarf */
#pragma once
#include <ostream>
2018-04-24 17:44:09 +02:00
#include <functional>
2018-05-11 13:09:35 +02:00
#include <memory>
2018-04-23 18:58:05 +02:00
#include "SimpleDwarf.hpp"
2018-05-11 13:09:35 +02:00
#include "PcListReader.hpp"
2018-06-25 11:33:36 +02:00
#include "SwitchStatement.hpp"
2018-04-23 18:58:05 +02:00
class CodeGenerator {
public:
2018-04-24 17:44:09 +02:00
/// A function deriving a generated function name from a FDE
typedef std::function<std::string(const SimpleDwarf::Fde&)>
NamingScheme;
2018-04-23 18:58:05 +02:00
/// Unimplemented case
class NotImplementedCase: public std::exception {};
class InvalidPcList: public std::exception {};
2018-04-23 18:58:05 +02:00
/** Create a CodeGenerator to generate code for the given dwarf, on the
* given std::ostream object (eg. cout). */
2018-04-24 17:44:09 +02:00
CodeGenerator(const SimpleDwarf& dwarf, std::ostream& os,
2018-06-25 11:33:36 +02:00
NamingScheme naming_scheme,
2018-06-25 11:54:45 +02:00
AbstractSwitchCompiler* sw_compiler);
2018-04-23 18:58:05 +02:00
/// Actually generate the code on the given stream
void generate();
private: //meth
2018-04-24 17:44:09 +02:00
struct LookupEntry {
std::string name;
uintptr_t beg, end;
};
2018-06-25 11:33:36 +02:00
SwitchStatement gen_fresh_switch() const;
void switch_append_fde(
SwitchStatement& sw,
const SimpleDwarf::Fde& fde) const;
2018-04-23 18:58:05 +02:00
void gen_of_dwarf();
void gen_unwind_func_header(const std::string& name);
void gen_unwind_func_footer();
void gen_function_of_fde(const SimpleDwarf::Fde& fde);
2018-06-25 11:33:36 +02:00
void gen_of_row_content(
2018-04-23 18:58:05 +02:00
const SimpleDwarf::DwRow& row,
2018-06-25 11:33:36 +02:00
std::ostream& stream) const;
2018-04-23 18:58:05 +02:00
void gen_of_reg(
2018-06-25 11:33:36 +02:00
const SimpleDwarf::DwRegister& reg,
std::ostream& stream) const;
2018-04-23 18:58:05 +02:00
2018-04-24 17:44:09 +02:00
void gen_lookup(const std::vector<LookupEntry>& entries);
2018-06-25 11:33:36 +02:00
bool check_reg_defined(const SimpleDwarf::DwRegister& reg) const;
bool check_reg_valid(const SimpleDwarf::DwRegister& reg) const;
2018-06-22 09:12:32 +02:00
2018-04-23 18:58:05 +02:00
private:
SimpleDwarf dwarf;
std::ostream& os;
2018-05-11 13:09:35 +02:00
std::unique_ptr<PcListReader> pc_list;
2018-04-24 17:44:09 +02:00
NamingScheme naming_scheme;
2018-06-25 11:33:36 +02:00
2018-06-25 11:54:45 +02:00
std::unique_ptr<AbstractSwitchCompiler> switch_compiler;
2018-04-23 18:58:05 +02:00
};