ROSE  0.9.10.91
BinaryUnparserBase.h
1 #ifndef ROSE_BinaryAnalysis_UnparserBase_H
2 #define ROSE_BinaryAnalysis_UnparserBase_H
3 
4 #include <BinaryUnparser.h>
5 #include <Partitioner2/BasicTypes.h>
6 #include <Partitioner2/FunctionCallGraph.h>
7 #include <Sawyer/Map.h>
8 #include <Sawyer/SharedObject.h>
9 #include <Registers.h>
10 
11 namespace Rose {
12 namespace BinaryAnalysis {
13 
15 namespace Unparser {
16 
21 class State {
22 public:
25 private:
26  const Partitioner2::Partitioner &partitioner_;
28  Partitioner2::FunctionPtr currentFunction_;
29  Partitioner2::BasicBlockPtr currentBasicBlock_;
30  std::string nextInsnLabel_;
31  AddrString basicBlockLabels_;
32  RegisterNames registerNames_;
33  const Base &frontUnparser_;
34  std::vector<unsigned> cfgVertexReachability_;
35  Sawyer::Container::Map<unsigned, std::string> reachabilityNames_; // map reachability value to name
36 
37 public:
38  State(const Partitioner2::Partitioner&, const Settings&, const Base &frontUnparser);
39  virtual ~State();
40 
41  const Partitioner2::Partitioner& partitioner() const;
42 
43  const Partitioner2::FunctionCallGraph& cg() const;
44 
45  const std::vector<unsigned> cfgVertexReachability() const;
46  void cfgVertexReachability(const std::vector<unsigned>&);
47  unsigned isCfgVertexReachable(size_t vertexId) const;
48 
63  void reachabilityName(unsigned value, const std::string &name);
64  std::string reachabilityName(unsigned value) const;
67  Partitioner2::FunctionPtr currentFunction() const;
68  void currentFunction(const Partitioner2::FunctionPtr&);
69 
70  Partitioner2::BasicBlockPtr currentBasicBlock() const;
71  void currentBasicBlock(const Partitioner2::BasicBlockPtr&);
72 
73  const std::string& nextInsnLabel() const;
74  void nextInsnLabel(const std::string&);
75 
76  const RegisterNames& registerNames() const;
77  void registerNames(const RegisterNames &r);
78 
79  const AddrString& basicBlockLabels() const;
80  AddrString& basicBlockLabels();
81 
82  const Base& frontUnparser() const;
83 };
84 
101 class Base: public Sawyer::SharedObject {
102 public:
104 
105 private:
106  Ptr nextUnparser_;
107 
108 protected:
109  Base();
110  explicit Base(const Ptr &nextUnparser);
111 
112 public:
113  virtual Ptr copy() const = 0;
114  virtual ~Base();
115 
116  virtual const Settings& settings() const = 0;
117  virtual Settings& settings() = 0;
118 
119  Ptr nextUnparser() const { return nextUnparser_; }
120 
128  void operator()(std::ostream &out, const Partitioner2::Partitioner &p) const /*final*/ {
129  unparse(out, p);
130  }
131  void operator()(std::ostream &out, const Partitioner2::Partitioner &p,
132  SgAsmInstruction *insn) const /*final*/ {
133  unparse(out, p, insn);
134  }
135  void operator()(std::ostream &out, const Partitioner2::Partitioner &p,
136  const Partitioner2::BasicBlockPtr &bb) const /*final*/{
137  unparse(out, p, bb);
138  }
139  void operator()(std::ostream &out, const Partitioner2::Partitioner &p,
140  const Partitioner2::DataBlockPtr &db) const /*final*/ {
141  unparse(out, p, db);
142  }
143  void operator()(std::ostream &out, const Partitioner2::Partitioner &p,
144  const Partitioner2::FunctionPtr &f) const /*final*/ {
145  unparse(out, p, f);
146  }
154  std::string operator()(const Partitioner2::Partitioner &p) const /*final*/ {
155  return unparse(p);
156  }
157  std::string operator()(const Partitioner2::Partitioner &p, SgAsmInstruction *insn) const /*final*/ {
158  return unparse(p, insn);
159  }
160  std::string operator()(const Partitioner2::Partitioner &p, const Partitioner2::BasicBlockPtr &bb) const /*final*/ {
161  return unparse(p, bb);
162  }
163  std::string operator()(const Partitioner2::Partitioner &p, const Partitioner2::DataBlockPtr &db) const /*final*/ {
164  return unparse(p, db);
165  }
166  std::string operator()(const Partitioner2::Partitioner &p, const Partitioner2::FunctionPtr &f) const /*final*/ {
167  return unparse(p, f);
168  }
174 public:
180  void unparse(std::ostream&, const Partitioner2::Partitioner&) const /*final*/;
181  void unparse(std::ostream&, const Partitioner2::Partitioner&, SgAsmInstruction*) const /*final*/;
182  void unparse(std::ostream&, const Partitioner2::Partitioner&, const Partitioner2::BasicBlockPtr&) const /*final*/;
183  void unparse(std::ostream&, const Partitioner2::Partitioner&, const Partitioner2::DataBlockPtr&) const /*final*/;
184  void unparse(std::ostream&, const Partitioner2::Partitioner&, const Partitioner2::FunctionPtr&) const /*final*/;
185 
186  std::string unparse(const Partitioner2::Partitioner&) const /*final*/;
187  std::string unparse(const Partitioner2::Partitioner&, SgAsmInstruction*) const /*final*/;
188  std::string unparse(const Partitioner2::Partitioner&, const Partitioner2::BasicBlockPtr&) const /*final*/;
189  std::string unparse(const Partitioner2::Partitioner&, const Partitioner2::DataBlockPtr&) const /*final*/;
190  std::string unparse(const Partitioner2::Partitioner&, const Partitioner2::FunctionPtr&) const /*final*/;
193 public:
216  virtual void emitFunction(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
217  virtual void emitFunctionPrologue(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
218  virtual void emitFunctionBody(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
219  virtual void emitFunctionEpilogue(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
220 
221  virtual void emitFunctionReasons(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
222  virtual void emitFunctionCallers(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
223  virtual void emitFunctionCallees(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
224  virtual void emitFunctionComment(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
225  virtual void emitFunctionStackDelta(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
226  virtual void emitFunctionCallingConvention(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
227  virtual void emitFunctionNoopAnalysis(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
228  virtual void emitFunctionMayReturn(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
229 
230  virtual void emitDataBlock(std::ostream&, const Partitioner2::DataBlockPtr&, State&) const;
231  virtual void emitDataBlockPrologue(std::ostream&, const Partitioner2::DataBlockPtr&, State&) const;
232  virtual void emitDataBlockBody(std::ostream&, const Partitioner2::DataBlockPtr&, State&) const;
233  virtual void emitDataBlockEpilogue(std::ostream&, const Partitioner2::DataBlockPtr&, State&) const;
234 
235  virtual void emitBasicBlock(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
236  virtual void emitBasicBlockPrologue(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
237  virtual void emitBasicBlockBody(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
238  virtual void emitBasicBlockEpilogue(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
239 
240  virtual void emitBasicBlockComment(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
241  virtual void emitBasicBlockSharing(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
242  virtual void emitBasicBlockPredecessors(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
243  virtual void emitBasicBlockSuccessors(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
244  virtual void emitBasicBlockReachability(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
245 
246  virtual void emitInstruction(std::ostream&, SgAsmInstruction*, State&) const;
247  virtual void emitInstructionPrologue(std::ostream&, SgAsmInstruction*, State&) const;
248  virtual void emitInstructionBody(std::ostream&, SgAsmInstruction*, State&) const;
249  virtual void emitInstructionEpilogue(std::ostream&, SgAsmInstruction*, State&) const;
250 
251  virtual void emitInstructionAddress(std::ostream&, SgAsmInstruction*, State&) const;
252  virtual void emitInstructionBytes(std::ostream&, SgAsmInstruction*, State&) const;
253  virtual void emitInstructionStackDelta(std::ostream&, SgAsmInstruction*, State&) const;
254  virtual void emitInstructionMnemonic(std::ostream&, SgAsmInstruction*, State&) const;
255  virtual void emitInstructionOperands(std::ostream&, SgAsmInstruction*, State&) const;
256  virtual void emitInstructionComment(std::ostream&, SgAsmInstruction*, State&) const;
257 
258  virtual void emitInstructionSemantics(std::ostream&, SgAsmInstruction*, State&) const;
259 
260  virtual void emitOperand(std::ostream&, SgAsmExpression*, State&) const;
261  virtual void emitOperandPrologue(std::ostream&, SgAsmExpression*, State&) const;
262  virtual void emitOperandBody(std::ostream&, SgAsmExpression*, State&) const;
263  virtual void emitOperandEpilogue(std::ostream&, SgAsmExpression*, State&) const;
264 
265  virtual void emitRegister(std::ostream&, RegisterDescriptor, State&) const;
266  virtual std::vector<std::string> emitUnsignedInteger(std::ostream&, const Sawyer::Container::BitVector&, State&) const;
267  virtual std::vector<std::string> emitSignedInteger(std::ostream&, const Sawyer::Container::BitVector&, State&) const;
268  virtual std::vector<std::string> emitInteger(std::ostream&, const Sawyer::Container::BitVector&, State&,
269  bool isSigned) const;
270  virtual bool emitAddress(std::ostream&, rose_addr_t, State&, bool always=true) const;
271  virtual bool emitAddress(std::ostream&, const Sawyer::Container::BitVector&, State&, bool always=true) const;
272  virtual void emitCommentBlock(std::ostream&, const std::string&, State&, const std::string &prefix = ";;; ") const;
273  virtual void emitTypeName(std::ostream&, SgAsmType*, State&) const;
276  //----- Utility functions -----
277 public:
278  static std::string leftJustify(const std::string&, size_t nchars);
279 
280  static std::string juxtaposeColumns(const std::vector<std::string> &content, const std::vector<size_t> &minWidths,
281  const std::string &columnSeparator = " ");
282 };
283 
284 
286 // Python API wrappers and functions
288 
289 #ifdef ROSE_ENABLE_PYTHON_API
290 class PythonBase {
291  Base::Ptr base_;
292 
293 public:
294  PythonBase() {}
295 
296  explicit PythonBase(const Base::Ptr &base)
297  : base_(base) {
298  ASSERT_not_null(base);
299  }
300 
301  std::string unparse(const Partitioner2::Partitioner &p) const {
302  return base_->unparse(p);
303  }
304 
305  void print(const Partitioner2::Partitioner &p) const {
306  base_->unparse(std::cout, p);
307  }
308 };
309 #endif
310 
311 } // namespace
312 } // namespace
313 } // namespace
314 
315 #endif
std::string operator()(const Partitioner2::Partitioner &p, SgAsmInstruction *insn) const
Emit the entity to a string.
virtual void emitCommentBlock(std::ostream &, const std::string &, State &, const std::string &prefix=";;; ") const
Mid-level unparser function.
virtual void emitBasicBlockBody(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
virtual void emitFunctionCallees(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual void emitFunctionReasons(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual void emitDataBlock(std::ostream &, const Partitioner2::DataBlockPtr &, State &) const
Mid-level unparser function.
virtual void emitInstructionMnemonic(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
virtual void emitInstructionSemantics(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
virtual void emitInstructionPrologue(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
virtual void emitOperandPrologue(std::ostream &, SgAsmExpression *, State &) const
Mid-level unparser function.
virtual void emitInstructionAddress(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
Sawyer::Container::Map< rose_addr_t, std::string > AddrString
Map from address to string.
virtual void emitRegister(std::ostream &, RegisterDescriptor, State &) const
Mid-level unparser function.
virtual void emitInstructionComment(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
Base class for machine instructions.
std::string operator()(const Partitioner2::Partitioner &p, const Partitioner2::DataBlockPtr &db) const
Emit the entity to a string.
Abstract base class for unparsers.
void operator()(std::ostream &out, const Partitioner2::Partitioner &p, const Partitioner2::DataBlockPtr &db) const
Emit the entity to an output stream.
virtual void emitInstruction(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
virtual void emitFunctionNoopAnalysis(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
STL namespace.
virtual void emitBasicBlockSharing(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
virtual void emitInstructionStackDelta(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
void operator()(std::ostream &out, const Partitioner2::Partitioner &p, const Partitioner2::BasicBlockPtr &bb) const
Emit the entity to an output stream.
void operator()(std::ostream &out, const Partitioner2::Partitioner &p, const Partitioner2::FunctionPtr &f) const
Emit the entity to an output stream.
Main namespace for the ROSE library.
virtual void emitBasicBlockComment(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
Describes (part of) a physical CPU register.
Settings that control unparsing.
virtual void emitInstructionOperands(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
Name space for the entire library.
Definition: Access.h:13
std::string operator()(const Partitioner2::Partitioner &p) const
Emit the entity to a string.
std::string operator()(const Partitioner2::Partitioner &p, const Partitioner2::BasicBlockPtr &bb) const
Emit the entity to a string.
void operator()(std::ostream &out, const Partitioner2::Partitioner &p) const
Emit the entity to an output stream.
virtual void emitInstructionBytes(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
virtual void emitInstructionBody(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
virtual void emitOperandEpilogue(std::ostream &, SgAsmExpression *, State &) const
Mid-level unparser function.
virtual void emitFunctionPrologue(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual std::vector< std::string > emitUnsignedInteger(std::ostream &, const Sawyer::Container::BitVector &, State &) const
Mid-level unparser function.
virtual void emitDataBlockPrologue(std::ostream &, const Partitioner2::DataBlockPtr &, State &) const
Mid-level unparser function.
virtual void emitFunctionMayReturn(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual void emitFunctionStackDelta(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual void emitFunctionCallingConvention(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual void emitDataBlockEpilogue(std::ostream &, const Partitioner2::DataBlockPtr &, State &) const
Mid-level unparser function.
std::string operator()(const Partitioner2::Partitioner &p, const Partitioner2::FunctionPtr &f) const
Emit the entity to a string.
virtual std::vector< std::string > emitSignedInteger(std::ostream &, const Sawyer::Container::BitVector &, State &) const
Mid-level unparser function.
virtual void emitFunctionCallers(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual std::vector< std::string > emitInteger(std::ostream &, const Sawyer::Container::BitVector &, State &, bool isSigned) const
Mid-level unparser function.
virtual void emitDataBlockBody(std::ostream &, const Partitioner2::DataBlockPtr &, State &) const
Mid-level unparser function.
virtual void emitBasicBlockReachability(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
virtual void emitBasicBlockEpilogue(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
Base class for expressions.
virtual void emitBasicBlockSuccessors(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
virtual void emitFunction(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
Prints a register name even when no dictionary is available or when the dictionary doesn't contain an...
Definition: Registers.h:354
virtual void emitFunctionEpilogue(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual bool emitAddress(std::ostream &, rose_addr_t, State &, bool always=true) const
Mid-level unparser function.
Base class for binary types.
Base class for reference counted objects.
Definition: SharedObject.h:22
virtual void emitFunctionComment(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual void emitInstructionEpilogue(std::ostream &, SgAsmInstruction *, State &) const
Mid-level unparser function.
virtual void emitFunctionBody(std::ostream &, const Partitioner2::FunctionPtr &, State &) const
Mid-level unparser function.
virtual void emitOperand(std::ostream &, SgAsmExpression *, State &) const
Mid-level unparser function.
virtual void emitBasicBlockPrologue(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
virtual void emitBasicBlock(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
virtual void emitTypeName(std::ostream &, SgAsmType *, State &) const
Mid-level unparser function.
void reachabilityName(unsigned value, const std::string &name)
Assign a reachability name to a reachability value.
virtual void emitBasicBlockPredecessors(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:293
void unparse(std::ostream &, const Partitioner2::Partitioner &) const
High-level unparsing function.
virtual void emitOperandBody(std::ostream &, SgAsmExpression *, State &) const
Mid-level unparser function.
void operator()(std::ostream &out, const Partitioner2::Partitioner &p, SgAsmInstruction *insn) const
Emit the entity to an output stream.