ROSE  0.9.9.109
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/SharedObject.h>
8 #include <Registers.h>
9 
10 namespace Rose {
11 namespace BinaryAnalysis {
12 
14 namespace Unparser {
15 
20 class State {
21 public:
24 private:
25  const Partitioner2::Partitioner &partitioner_;
27  Partitioner2::FunctionPtr currentFunction_;
28  Partitioner2::BasicBlockPtr currentBasicBlock_;
29  std::string nextInsnLabel_;
30  AddrString basicBlockLabels_;
31  RegisterNames registerNames_;
32  const Base &frontUnparser_;
33 
34 public:
35  State(const Partitioner2::Partitioner&, const Settings&, const Base &frontUnparser);
36  virtual ~State();
37 
38  const Partitioner2::Partitioner& partitioner() const;
39  const Partitioner2::FunctionCallGraph& cg() const;
40  Partitioner2::FunctionPtr currentFunction() const;
41  void currentFunction(const Partitioner2::FunctionPtr&);
42  Partitioner2::BasicBlockPtr currentBasicBlock() const;
43  void currentBasicBlock(const Partitioner2::BasicBlockPtr&);
44  const std::string& nextInsnLabel() const;
45  void nextInsnLabel(const std::string&);
46  const RegisterNames& registerNames() const;
47  void registerNames(const RegisterNames &r);
48  const AddrString& basicBlockLabels() const;
49  AddrString& basicBlockLabels();
50  const Base& frontUnparser() const;
51 };
52 
69 class Base: public Sawyer::SharedObject {
70 public:
72 
73 private:
74  Ptr nextUnparser_;
75 
76 protected:
77  Base();
78  explicit Base(const Ptr &nextUnparser);
79 
80 public:
81  virtual Ptr copy() const = 0;
82  virtual ~Base();
83 
84  virtual const Settings& settings() const = 0;
85  virtual Settings& settings() = 0;
86 
87  Ptr nextUnparser() const { return nextUnparser_; }
88 
96  void operator()(std::ostream &out, const Partitioner2::Partitioner &p) const /*final*/ {
97  unparse(out, p);
98  }
99  void operator()(std::ostream &out, const Partitioner2::Partitioner &p,
100  SgAsmInstruction *insn) const /*final*/ {
101  unparse(out, p, insn);
102  }
103  void operator()(std::ostream &out, const Partitioner2::Partitioner &p,
104  const Partitioner2::BasicBlockPtr &bb) const /*final*/{
105  unparse(out, p, bb);
106  }
107  void operator()(std::ostream &out, const Partitioner2::Partitioner &p,
108  const Partitioner2::DataBlockPtr &db) const /*final*/ {
109  unparse(out, p, db);
110  }
111  void operator()(std::ostream &out, const Partitioner2::Partitioner &p,
112  const Partitioner2::FunctionPtr &f) const /*final*/ {
113  unparse(out, p, f);
114  }
122  std::string operator()(const Partitioner2::Partitioner &p) const /*final*/ {
123  return unparse(p);
124  }
125  std::string operator()(const Partitioner2::Partitioner &p, SgAsmInstruction *insn) const /*final*/ {
126  return unparse(p, insn);
127  }
128  std::string operator()(const Partitioner2::Partitioner &p, const Partitioner2::BasicBlockPtr &bb) const /*final*/ {
129  return unparse(p, bb);
130  }
131  std::string operator()(const Partitioner2::Partitioner &p, const Partitioner2::DataBlockPtr &db) const /*final*/ {
132  return unparse(p, db);
133  }
134  std::string operator()(const Partitioner2::Partitioner &p, const Partitioner2::FunctionPtr &f) const /*final*/ {
135  return unparse(p, f);
136  }
142 public:
148  void unparse(std::ostream&, const Partitioner2::Partitioner&) const /*final*/;
149  void unparse(std::ostream&, const Partitioner2::Partitioner&, SgAsmInstruction*) const /*final*/;
150  void unparse(std::ostream&, const Partitioner2::Partitioner&, const Partitioner2::BasicBlockPtr&) const /*final*/;
151  void unparse(std::ostream&, const Partitioner2::Partitioner&, const Partitioner2::DataBlockPtr&) const /*final*/;
152  void unparse(std::ostream&, const Partitioner2::Partitioner&, const Partitioner2::FunctionPtr&) const /*final*/;
153 
154  std::string unparse(const Partitioner2::Partitioner&) const /*final*/;
155  std::string unparse(const Partitioner2::Partitioner&, SgAsmInstruction*) const /*final*/;
156  std::string unparse(const Partitioner2::Partitioner&, const Partitioner2::BasicBlockPtr&) const /*final*/;
157  std::string unparse(const Partitioner2::Partitioner&, const Partitioner2::DataBlockPtr&) const /*final*/;
158  std::string unparse(const Partitioner2::Partitioner&, const Partitioner2::FunctionPtr&) const /*final*/;
161 public:
184  virtual void emitFunction(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
185  virtual void emitFunctionPrologue(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
186  virtual void emitFunctionBody(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
187  virtual void emitFunctionEpilogue(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
188 
189  virtual void emitFunctionReasons(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
190  virtual void emitFunctionCallers(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
191  virtual void emitFunctionCallees(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
192  virtual void emitFunctionComment(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
193  virtual void emitFunctionStackDelta(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
194  virtual void emitFunctionCallingConvention(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
195  virtual void emitFunctionNoopAnalysis(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
196  virtual void emitFunctionMayReturn(std::ostream&, const Partitioner2::FunctionPtr&, State&) const;
197 
198  virtual void emitDataBlock(std::ostream&, const Partitioner2::DataBlockPtr&, State&) const;
199  virtual void emitDataBlockPrologue(std::ostream&, const Partitioner2::DataBlockPtr&, State&) const;
200  virtual void emitDataBlockBody(std::ostream&, const Partitioner2::DataBlockPtr&, State&) const;
201  virtual void emitDataBlockEpilogue(std::ostream&, const Partitioner2::DataBlockPtr&, State&) const;
202 
203  virtual void emitBasicBlock(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
204  virtual void emitBasicBlockPrologue(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
205  virtual void emitBasicBlockBody(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
206  virtual void emitBasicBlockEpilogue(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
207 
208  virtual void emitBasicBlockComment(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
209  virtual void emitBasicBlockSharing(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
210  virtual void emitBasicBlockPredecessors(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
211  virtual void emitBasicBlockSuccessors(std::ostream&, const Partitioner2::BasicBlockPtr&, State&) const;
212 
213  virtual void emitInstruction(std::ostream&, SgAsmInstruction*, State&) const;
214  virtual void emitInstructionPrologue(std::ostream&, SgAsmInstruction*, State&) const;
215  virtual void emitInstructionBody(std::ostream&, SgAsmInstruction*, State&) const;
216  virtual void emitInstructionEpilogue(std::ostream&, SgAsmInstruction*, State&) const;
217 
218  virtual void emitInstructionAddress(std::ostream&, SgAsmInstruction*, State&) const;
219  virtual void emitInstructionBytes(std::ostream&, SgAsmInstruction*, State&) const;
220  virtual void emitInstructionStackDelta(std::ostream&, SgAsmInstruction*, State&) const;
221  virtual void emitInstructionMnemonic(std::ostream&, SgAsmInstruction*, State&) const;
222  virtual void emitInstructionOperands(std::ostream&, SgAsmInstruction*, State&) const;
223  virtual void emitInstructionComment(std::ostream&, SgAsmInstruction*, State&) const;
224 
225  virtual void emitOperand(std::ostream&, SgAsmExpression*, State&) const;
226  virtual void emitOperandPrologue(std::ostream&, SgAsmExpression*, State&) const;
227  virtual void emitOperandBody(std::ostream&, SgAsmExpression*, State&) const;
228  virtual void emitOperandEpilogue(std::ostream&, SgAsmExpression*, State&) const;
229 
230  virtual void emitRegister(std::ostream&, RegisterDescriptor, State&) const;
231  virtual std::vector<std::string> emitUnsignedInteger(std::ostream&, const Sawyer::Container::BitVector&, State&) const;
232  virtual std::vector<std::string> emitSignedInteger(std::ostream&, const Sawyer::Container::BitVector&, State&) const;
233  virtual std::vector<std::string> emitInteger(std::ostream&, const Sawyer::Container::BitVector&, State&,
234  bool isSigned) const;
235  virtual bool emitAddress(std::ostream&, rose_addr_t, State&, bool always=true) const;
236  virtual bool emitAddress(std::ostream&, const Sawyer::Container::BitVector&, State&, bool always=true) const;
237  virtual void emitCommentBlock(std::ostream&, const std::string&, State&, const std::string &prefix = ";;; ") const;
238  virtual void emitTypeName(std::ostream&, SgAsmType*, State&) const;
241  //----- Utility functions -----
242 public:
243  static std::string leftJustify(const std::string&, size_t nchars);
244 
245  static std::string juxtaposeColumns(const std::vector<std::string> &content, const std::vector<size_t> &minWidths,
246  const std::string &columnSeparator = " ");
247 };
248 
249 
250 } // namespace
251 } // namespace
252 } // namespace
253 
254 #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 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:11
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 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.
virtual void emitBasicBlockPredecessors(std::ostream &, const Partitioner2::BasicBlockPtr &, State &) const
Mid-level unparser function.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:289
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.