ROSE  0.11.31.0
BinaryToSource.h
1 #ifndef ROSE_BinaryAnalysis_BinaryToSource_H
2 #define ROSE_BinaryAnalysis_BinaryToSource_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Diagnostics.h>
7 #include <Partitioner2/Partitioner.h>
8 #include <RoseException.h>
9 #include <Sawyer/CommandLine.h>
10 #include <SourceAstSemantics2.h>
11 #include <TraceSemantics2.h>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 
21 public:
22 
24  struct Settings {
28 
32 
35 
38 
44 
47 
50  : traceRiscOps(false), traceInsnExecution(false), allocateMemoryArray(false), zeroMemoryArray(false) {}
51  };
52 
54  class Exception: public Rose::Exception {
55  public:
57  Exception(const std::string &mesg): Rose::Exception(mesg) {}
58  };
59 
60 private:
61  static Diagnostics::Facility mlog;
62  Settings settings_;
63  Disassembler *disassembler_;
67 
68 public:
74  : disassembler_(NULL) {}
75 
80  explicit BinaryToSource(const Settings &settings)
81  : settings_(settings), disassembler_(NULL) {}
82 
85 
89  static void initDiagnostics();
90 
94  const Settings& settings() const { return settings_; }
95 
102  void generateSource(const Partitioner2::Partitioner&, std::ostream&);
103 
104 #if 0 // [Robb P. Matzke 2015-10-06]: not implemented yet
105 
110  void generateSource(const Partitioner2::Partitioner&, SgSourceFile *sourceAst);
111 #endif
112 
113 private:
114  // (Re)initialize the instruction semantics machinery so it is suitable for this architecture.
115  void init(const Partitioner2::Partitioner&);
116 
117  // Emit the file prologue, #include statements, extern declarations, etc.
118  void emitFilePrologue(const Partitioner2::Partitioner&, std::ostream&);
119 
120  // Declare the global register variables
121  void declareGlobalRegisters(std::ostream&);
122 
123  // Define interrupt handlers
124  void defineInterrupts(std::ostream&);
125 
126  // Emit accumulated side effects and/or SSA. */
127  void emitEffects(std::ostream&);
128 
129  // Emit code for one instruction
130  void emitInstruction(SgAsmInstruction*, std::ostream&);
131 
132  // Emit code for one basic block
133  void emitBasicBlock(const Partitioner2::Partitioner&, const Partitioner2::BasicBlock::Ptr&, std::ostream&);
134 
135  // Emit code for one function
136  void emitFunction(const Partitioner2::Partitioner&, const Partitioner2::Function::Ptr&, std::ostream&);
137 
138  // Emit code for all functions
139  void emitAllFunctions(const Partitioner2::Partitioner&, std::ostream&);
140 
141  // Emit the function_call dispatcher that decides which C function should be called next.
142  void emitFunctionDispatcher(const Partitioner2::Partitioner&, std::ostream&);
143 
144  // Emit the initialize_memory function.
145  void emitMemoryInitialization(const Partitioner2::Partitioner&, std::ostream&);
146 
147  // Emit the "main" function.
148  void emitMain(const Partitioner2::Partitioner&, std::ostream&);
149 };
150 
151 } // namespace
152 } // namespace
153 
154 #endif
155 #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:1606
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< 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:323
Base class for all ROSE exceptions.
Definition: RoseException.h:9
Virtual base class for instruction disassemblers.
Definition: Disassembler.h:50