ROSE  0.11.22.0
Assembler.h
1 #ifndef ROSE_BinaryAnalysis_Assembler_H
2 #define ROSE_BinaryAnalysis_Assembler_H
3 
4 #include <featureTests.h>
5 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
6 
7 #include <RoseException.h>
8 
9 namespace Rose {
10 namespace BinaryAnalysis {
11 
58 class Assembler {
59 public:
61  class Exception: public Rose::Exception {
62  public:
64  Exception(const std::string &reason, SgAsmInstruction *insn)
65  : Rose::Exception(reason), insn(insn)
66  {}
68  Exception(const std::string &reason)
69  : Rose::Exception(reason), insn(NULL)
70  {}
71  void print(std::ostream&) const;
72  friend std::ostream& operator<<(std::ostream&, const Exception&);
73 
75  };
76 
81  {
87  };
88 
89  Assembler()
91  {}
92 
93  virtual ~Assembler() {}
94 
96  static Assembler *create(SgAsmInterpretation *interp);
97 
99  static Assembler *create(SgAsmGenericHeader*);
100 
101  /*==========================================================================================================================
102  * Main public assembly methods
103  *========================================================================================================================== */
104 public:
108  virtual SgUnsignedCharList assembleOne(SgAsmInstruction *insn) = 0;
109 
113  SgUnsignedCharList assembleBlock(SgAsmBlock*);
114 
118  SgUnsignedCharList assembleBlock(const std::vector<SgAsmInstruction*> &insns, rose_addr_t starting_rva);
119 
121  virtual SgUnsignedCharList assembleProgram(const std::string &source) = 0;
122 
123  /*==========================================================================================================================
124  * Assembler properties and settings
125  *========================================================================================================================== */
126 public:
129  p_encoding_type = et;
130  }
131 
134  return p_encoding_type;
135  }
136 
138  void set_debug(FILE *f) {
139  p_debug = f;
140  }
141 
143  FILE *get_debug() const {
144  return p_debug;
145  }
146 
147  /*==========================================================================================================================
148  * Data members
149  *========================================================================================================================== */
150 protected:
151  FILE *p_debug;
153 };
154 
155 } // namespace
156 } // namespace
157 
158 #endif
159 #endif
virtual SgUnsignedCharList assembleProgram(const std::string &source)=0
Assembles a program from an assembly listing.
FILE * get_debug() const
Returns the file currently used for debugging; null implies no debugging.
Definition: Assembler.h:143
virtual SgUnsignedCharList assembleOne(SgAsmInstruction *insn)=0
This is the lowest level architecture-independent assembly function and is implemented in the archite...
Returns the shortest possible encoding.
Definition: Assembler.h:82
SgUnsignedCharList assembleBlock(SgAsmBlock *)
Assembles a single basic block of instructions, packing them together and adjusting their virtual add...
Instruction basic block.
Exception(const std::string &reason, SgAsmInstruction *insn)
An exception bound to a particular instruction being assembled.
Definition: Assembler.h:64
Base class for machine instructions.
EncodingType p_encoding_type
Which encoding should be returned by assembleOne.
Definition: Assembler.h:152
Main namespace for the ROSE library.
EncodingType get_encoding_type() const
Returns the encoding type employed by this assembler.
Definition: Assembler.h:133
static Assembler * create(SgAsmInterpretation *interp)
Creates an assembler that is appropriate for assembling instructions in the specified interpretation...
Returns an encoding that matches the SgAsmInstruction::p_raw_bytes.
Definition: Assembler.h:84
Base class for container file headers.
Returns the longest encoding.
Definition: Assembler.h:83
FILE * p_debug
Set to non-null to get debugging info.
Definition: Assembler.h:151
void set_debug(FILE *f)
Sends assembler diagnostics to the specified output stream.
Definition: Assembler.h:138
EncodingType
Assemblers can often assemble a single instruction various ways.
Definition: Assembler.h:80
SgAsmInstruction * insn
Instruction associated with an assembly error.
Definition: Assembler.h:74
Exception thrown by the assemblers.
Definition: Assembler.h:61
Exception(const std::string &reason)
An exception not bound to a particular instruction.
Definition: Assembler.h:68
Base class for all ROSE exceptions.
Definition: RoseException.h:9
Represents an interpretation of a binary container.
void set_encoding_type(EncodingType et)
Controls how the assembleOne() method determines which encoding to return.
Definition: Assembler.h:128
Virtual base class for instruction assemblers.
Definition: Assembler.h:58