ROSE  0.9.11.42
BinaryToSource.h
1 #ifndef ROSE_BinaryAnalysis_BinaryToSource_H
2 #define ROSE_BinaryAnalysis_BinaryToSource_H
3 
4 #include <Diagnostics.h>
5 #include <Partitioner2/Partitioner.h>
6 #include <RoseException.h>
7 #include <Sawyer/CommandLine.h>
8 #include <SourceAstSemantics2.h>
9 #include <TraceSemantics2.h>
10 
11 namespace Rose {
12 namespace BinaryAnalysis {
13 
19 public:
20 
22  struct Settings {
26 
30 
33 
36 
42 
45 
48  : traceRiscOps(false), traceInsnExecution(false), allocateMemoryArray(false), zeroMemoryArray(false) {}
49  };
50 
52  class Exception: public Rose::Exception {
53  public:
55  Exception(const std::string &mesg): Rose::Exception(mesg) {}
56  };
57 
58 private:
59  static Diagnostics::Facility mlog;
60  Settings settings_;
61  Disassembler *disassembler_;
65 
66 public:
72  : disassembler_(NULL) {}
73 
78  explicit BinaryToSource(const Settings &settings)
79  : settings_(settings), disassembler_(NULL) {}
80 
83 
87  static void initDiagnostics();
88 
92  const Settings& settings() const { return settings_; }
93 
100  void generateSource(const Partitioner2::Partitioner&, std::ostream&);
101 
102 #if 0 // [Robb P. Matzke 2015-10-06]: not implemented yet
103 
108  void generateSource(const Partitioner2::Partitioner&, SgSourceFile *sourceAst);
109 #endif
110 
111 private:
112  // (Re)initialize the instruction semantics machinery so it is suitable for this architecture.
113  void init(const Partitioner2::Partitioner&);
114 
115  // Emit the file prologue, #include statements, extern declarations, etc.
116  void emitFilePrologue(const Partitioner2::Partitioner&, std::ostream&);
117 
118  // Declare the global register variables
119  void declareGlobalRegisters(std::ostream&);
120 
121  // Define interrupt handlers
122  void defineInterrupts(std::ostream&);
123 
124  // Emit accumulated side effects and/or SSA. */
125  void emitEffects(std::ostream&);
126 
127  // Emit code for one instruction
128  void emitInstruction(SgAsmInstruction*, std::ostream&);
129 
130  // Emit code for one basic block
131  void emitBasicBlock(const Partitioner2::Partitioner&, const Partitioner2::BasicBlock::Ptr&, std::ostream&);
132 
133  // Emit code for one function
134  void emitFunction(const Partitioner2::Partitioner&, const Partitioner2::Function::Ptr&, std::ostream&);
135 
136  // Emit code for all functions
137  void emitAllFunctions(const Partitioner2::Partitioner&, std::ostream&);
138 
139  // Emit the function_call dispatcher that decides which C function should be called next.
140  void emitFunctionDispatcher(const Partitioner2::Partitioner&, std::ostream&);
141 
142  // Emit the initialize_memory function.
143  void emitMemoryInitialization(const Partitioner2::Partitioner&, std::ostream&);
144 
145  // Emit the "main" function.
146  void emitMain(const Partitioner2::Partitioner&, std::ostream&);
147 };
148 
149 } // namespace
150 } // namespace
151 
152 #endif
void generateSource(const Partitioner2::Partitioner &, std::ostream &)
Generate source code as text.
bool zeroMemoryArray
Whether to zero the memory array, or just allocated with malloc.
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1579
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.
static void initDiagnostics()
Initialize diagnostic streams.
static Sawyer::CommandLine::SwitchGroup commandLineSwitches(Settings &)
Command-line switch parsing.
A collection of related switch declarations.
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:293
Base class for all ROSE exceptions.
Definition: RoseException.h:9
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:48