ROSE  0.11.98.0
ToSource.h
1 #ifndef ROSE_BinaryAnalysis_ToSource_H
2 #define ROSE_BinaryAnalysis_ToSource_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/Disassembler/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/Partitioner2/Partitioner.h>
8 #include <Rose/BinaryAnalysis/InstructionSemantics/SourceAstSemantics.h>
9 #include <Rose/BinaryAnalysis/InstructionSemantics/TraceSemantics.h>
10 #include <Rose/Diagnostics.h>
11 #include <Rose/Exception.h>
12 
13 #include <Sawyer/CommandLine.h>
14 
15 namespace Rose {
16 namespace BinaryAnalysis {
17 
23 public:
24 
26  struct Settings {
30 
34 
37 
40 
46 
49 
52  : traceRiscOps(false), traceInsnExecution(false), allocateMemoryArray(false), zeroMemoryArray(false) {}
53  };
54 
56  class Exception: public Rose::Exception {
57  public:
59  Exception(const std::string &mesg): Rose::Exception(mesg) {}
60  };
61 
62 private:
63  static Diagnostics::Facility mlog;
64  Settings settings_;
65  Disassembler::BasePtr disassembler_;
69 
70 public:
76 
81  explicit BinaryToSource(const Settings&);
82 
83  ~BinaryToSource();
84 
87 
91  static void initDiagnostics();
92 
96  const Settings& settings() const { return settings_; }
97 
104  void generateSource(const Partitioner2::Partitioner&, std::ostream&);
105 
106 #if 0 // [Robb P. Matzke 2015-10-06]: not implemented yet
107 
112  void generateSource(const Partitioner2::Partitioner&, SgSourceFile *sourceAst);
113 #endif
114 
115 private:
116  // (Re)initialize the instruction semantics machinery so it is suitable for this architecture.
117  void init(const Partitioner2::Partitioner&);
118 
119  // Emit the file prologue, #include statements, extern declarations, etc.
120  void emitFilePrologue(const Partitioner2::Partitioner&, std::ostream&);
121 
122  // Declare the global register variables
123  void declareGlobalRegisters(std::ostream&);
124 
125  // Define interrupt handlers
126  void defineInterrupts(std::ostream&);
127 
128  // Emit accumulated side effects and/or SSA. */
129  void emitEffects(std::ostream&);
130 
131  // Emit code for one instruction
132  void emitInstruction(SgAsmInstruction*, std::ostream&);
133 
134  // Emit code for one basic block
135  void emitBasicBlock(const Partitioner2::Partitioner&, const Partitioner2::BasicBlock::Ptr&, std::ostream&);
136 
137  // Emit code for one function
138  void emitFunction(const Partitioner2::Partitioner&, const Partitioner2::Function::Ptr&, std::ostream&);
139 
140  // Emit code for all functions
141  void emitAllFunctions(const Partitioner2::Partitioner&, std::ostream&);
142 
143  // Emit the function_call dispatcher that decides which C function should be called next.
144  void emitFunctionDispatcher(const Partitioner2::Partitioner&, std::ostream&);
145 
146  // Emit the initialize_memory function.
147  void emitMemoryInitialization(const Partitioner2::Partitioner&, std::ostream&);
148 
149  // Emit the "main" function.
150  void emitMain(const Partitioner2::Partitioner&, std::ostream&);
151 };
152 
153 } // namespace
154 } // namespace
155 
156 #endif
157 #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.
Definition: ToSource.h:48
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer for basic semantic operations.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to trace-semantics RISC operations.
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.
Definition: ToSource.h:36
Sawyer::Optional< rose_addr_t > initialStackPointer
Optional initial value for the stack pointer.
Definition: ToSource.h:39
Convert binary to low-level C source code.
Definition: ToSource.h:22
static void initDiagnostics()
Initialize diagnostic streams.
static Sawyer::CommandLine::SwitchGroup commandLineSwitches(Settings &)
Command-line switch parsing.
A collection of related switch declarations.
bool traceInsnExecution
Generate code that prints instructions.
Definition: ToSource.h:33
Main namespace for the ROSE library.
Exception(const std::string &mesg)
Constructs an exception with the specified message.
Definition: ToSource.h:59
Exceptions thrown by this analysis.
Definition: ToSource.h:56
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.
Definition: ToSource.h:45
BinaryToSource()
Default constructor.
const Settings & settings() const
Property: Configuration settings.
Definition: ToSource.h:96
Binary analysis.
Settings to control this translator's behavior.
Definition: ToSource.h:26
Settings()
Constructs the default settings.
Definition: ToSource.h:51
bool traceRiscOps
Trace the RISC operators for debugging.
Definition: ToSource.h:29
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:294
Base class for all ROSE exceptions.
Definition: Rose/Exception.h:9