ROSE  0.9.9.109
BinaryToSource.h
1 #ifndef ROSE_BinaryAnalysis_BinaryToSource_H
2 #define ROSE_BinaryAnalysis_BinaryToSource_H
3 
4 #include <Partitioner2/Partitioner.h>
5 #include <SourceAstSemantics2.h>
6 #include <TraceSemantics2.h>
7 
8 namespace Rose {
9 namespace BinaryAnalysis {
10 
16 public:
17 
19  struct Settings {
23 
27 
30 
33 
39 
42  : traceRiscOps(false), traceInsnExecution(false), allocateMemoryArray(false) {}
43  };
44 
46  class Exception: public std::runtime_error {
47  public:
49  Exception(const std::string &mesg): std::runtime_error(mesg) {}
50  };
51 
52 private:
53  Settings settings_;
54  Disassembler *disassembler_;
58 
59 public:
65  : disassembler_(NULL) {}
66 
71  explicit BinaryToSource(const Settings &settings)
72  : settings_(settings), disassembler_(NULL) {}
73 
77  const Settings& settings() const { return settings_; }
78 
85  void generateSource(const Partitioner2::Partitioner&, std::ostream&);
86 
87 #if 0 // [Robb P. Matzke 2015-10-06]: not implemented yet
88 
93  void generateSource(const Partitioner2::Partitioner&, SgSourceFile *sourceAst);
94 #endif
95 
96 private:
97  // (Re)initialize the instruction semantics machinery so it is suitable for this architecture.
98  void init(const Partitioner2::Partitioner&);
99 
100  // Emit the file prologue, #include statements, extern declarations, etc.
101  void emitFilePrologue(const Partitioner2::Partitioner&, std::ostream&);
102 
103  // Declare the global register variables
104  void declareGlobalRegisters(std::ostream&);
105 
106  // Emit accumulated side effects and/or SSA. */
107  void emitEffects(std::ostream&);
108 
109  // Emit code for one instruction
110  void emitInstruction(SgAsmInstruction*, std::ostream&);
111 
112  // Emit code for one basic block
113  void emitBasicBlock(const Partitioner2::Partitioner&, const Partitioner2::BasicBlock::Ptr&, std::ostream&);
114 
115  // Emit code for one function
116  void emitFunction(const Partitioner2::Partitioner&, const Partitioner2::Function::Ptr&, std::ostream&);
117 
118  // Emit code for all functions
119  void emitAllFunctions(const Partitioner2::Partitioner&, std::ostream&);
120 
121  // Emit the function_call dispatcher that decides which C function should be called next.
122  void emitFunctionDispatcher(const Partitioner2::Partitioner&, std::ostream&);
123 
124  // Emit the initialize_memory function.
125  void emitMemoryInitialization(const Partitioner2::Partitioner&, std::ostream&);
126 
127  // Emit the "main" function.
128  void emitMain(std::ostream&);
129 };
130 
131 } // namespace
132 } // namespace
133 
134 #endif
void generateSource(const Partitioner2::Partitioner &, std::ostream &)
Generate source code as text.
Base class for machine instructions.
Sawyer::Optional< rose_addr_t > initialInstructionPointer
Optional initial value for the instruction pointer.
Sawyer::Optional< rose_addr_t > initialStackPointer
Optional initial value for the stack pointer.
Convert binary to low-level C source code.
STL namespace.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to trace-semantics RISC operations.
bool traceInsnExecution
Generate code that prints instructions.
Main namespace for the ROSE library.
Exception(const std::string &mesg)
Constructs an exception with the specified message.
Exceptions thrown by this analysis.
BinaryToSource(const Settings &settings)
Construct the analyzer with specified settings.
boost::shared_ptr< class Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
Sawyer::Optional< rose_addr_t > allocateMemoryArray
Whether to allocate the global memory array.
BinaryToSource()
Default constructor.
const Settings & settings() const
Property: Configuration settings.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer for basic semantic operations.
Settings to control this translator's behavior.
Settings()
Constructs the default settings.
bool traceRiscOps
Trace the RISC operators for debugging.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:289
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:41