ROSE  0.9.12.28
RegisterStateGeneric.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_RegisterStateGeneric_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_RegisterStateGeneric_H
3 
4 #include <BaseSemantics2.h>
5 
6 #include <boost/serialization/access.hpp>
7 #include <boost/serialization/base_object.hpp>
8 #include <boost/serialization/export.hpp>
9 #include <Sawyer/IntervalSetMap.h>
10 
11 namespace Rose {
12 namespace BinaryAnalysis {
13 namespace InstructionSemantics2 {
14 namespace BaseSemantics {
15 
17 typedef boost::shared_ptr<class RegisterStateGeneric> RegisterStateGenericPtr;
18 
38  // Basic Types
40 public:
45  class RegisterNotPresent: public std::runtime_error {
46  RegisterDescriptor desc_;
47  public:
49  : std::runtime_error("accessed register is not available in register state") {}
50  };
51 
56 
61  struct RegStore {
62  unsigned majr, minr;
63 
64 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
65  private:
66  friend class boost::serialization::access;
67 
68  template<class S>
69  void serialize(S &s, const unsigned /*version*/) {
70  s & BOOST_SERIALIZATION_NVP(majr);
71  s & BOOST_SERIALIZATION_NVP(minr);
72  }
73 #endif
74 
75  public:
76  RegStore() // for serialization
77  : majr(0), minr(0) {}
78  RegStore(RegisterDescriptor d) // implicit
79  : majr(d.majorNumber()), minr(d.minorNumber()) {}
80  bool operator<(const RegStore &other) const {
81  return majr<other.majr || (majr==other.majr && minr<other.minr);
82  }
83  };
84 
85 
87  // Types for storing values
89 public:
91  struct RegPair {
92  RegisterDescriptor desc;
93  SValuePtr value;
94 
95 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
96  private:
97  friend class boost::serialization::access;
98 
99  template<class S>
100  void serialize(S &s, const unsigned /*version*/) {
101  s & BOOST_SERIALIZATION_NVP(desc);
102  s & BOOST_SERIALIZATION_NVP(value);
103  }
104 #endif
105 
106  public:
107  RegPair() {} // for serialization
108 
109  public:
110  RegPair(RegisterDescriptor desc, const SValuePtr &value): desc(desc), value(value) {}
111  BitRange location() const { return BitRange::baseSize(desc.offset(), desc.nBits()); }
112  };
113 
115  typedef std::vector<RegPair> RegPairs;
116 
119 
120 
122  // Types for Boolean properties
124 public:
131 
136 
137 
139  // Types for storing addresses
141 public:
144 
147 
150 
151 
153  // Data members
155 private:
156  RegisterProperties properties_; // Boolean properties for each bit of each register.
157  RegisterAddressSet writers_; // Writing instruction address set for each bit of each register
158  bool accessModifiesExistingLocations_; // Can read/write modify existing locations?
159  bool accessCreatesLocations_; // Can new locations be created?
160 
161 protected:
170  Registers registers_;
171 
173  // Serialization
175 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
176 private:
177  friend class boost::serialization::access;
178 
179  template<class S>
180  void serialize(S &s, const unsigned /*version*/) {
181  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(RegisterState);
182  s & BOOST_SERIALIZATION_NVP(properties_);
183  s & BOOST_SERIALIZATION_NVP(writers_);
184  s & BOOST_SERIALIZATION_NVP(accessModifiesExistingLocations_);
185  s & BOOST_SERIALIZATION_NVP(accessCreatesLocations_);
186  s & BOOST_SERIALIZATION_NVP(registers_);
187  }
188 #endif
189 
191  // Normal constructors
192  //
193  // These are protected because objects of this class are reference counted and always allocated on the heap.
195 protected:
196  RegisterStateGeneric() // for serialization
197  : accessModifiesExistingLocations_(true), accessCreatesLocations_(true) {}
198 
199  explicit RegisterStateGeneric(const SValuePtr &protoval, const RegisterDictionary *regdict)
200  : RegisterState(protoval, regdict), accessModifiesExistingLocations_(true), accessCreatesLocations_(true) {
201  clear();
202  }
203 
204  RegisterStateGeneric(const RegisterStateGeneric &other)
205  : RegisterState(other), properties_(other.properties_), writers_(other.writers_),
206  accessModifiesExistingLocations_(other.accessModifiesExistingLocations_),
207  accessCreatesLocations_(other.accessCreatesLocations_), registers_(other.registers_) {
208  deep_copy_values();
209  }
210 
211 
213  // Static allocating constructors
215 public:
222  static RegisterStateGenericPtr instance(const SValuePtr &protoval, const RegisterDictionary *regdict) {
223  return RegisterStateGenericPtr(new RegisterStateGeneric(protoval, regdict));
224  }
225 
227  static RegisterStateGenericPtr instance(const RegisterStateGenericPtr &other) {
228  return RegisterStateGenericPtr(new RegisterStateGeneric(*other));
229  }
230 
231 
233  // Virtual constructors
235 public:
236  virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const ROSE_OVERRIDE {
237  return instance(protoval, regdict);
238  }
239 
240  virtual RegisterStatePtr clone() const ROSE_OVERRIDE {
242  }
243 
245  // Dynamic pointer casts
247 public:
250  static RegisterStateGenericPtr promote(const RegisterStatePtr &from) {
251  RegisterStateGenericPtr retval = boost::dynamic_pointer_cast<RegisterStateGeneric>(from);
252  ASSERT_not_null(retval);
253  return retval;
254  }
255 
256 
258  // Object properties
260 public:
275  bool accessModifiesExistingLocations() const /*final*/ { return accessModifiesExistingLocations_; }
276  virtual void accessModifiesExistingLocations(bool b) { accessModifiesExistingLocations_ = b; }
284  RegisterStateGeneric *rstate_;
285  bool savedValue_;
286  public:
288  : rstate_(rstate), savedValue_(rstate->accessModifiesExistingLocations()) {
289  rstate_->accessModifiesExistingLocations(newValue);
290  }
292  rstate_->accessModifiesExistingLocations(savedValue_);
293  }
294  };
295 
303  bool accessCreatesLocations() const /*final*/ { return accessCreatesLocations_; }
304  virtual void accessCreatesLocations(bool b) { accessCreatesLocations_ = b; }
312  RegisterStateGeneric *rstate_;
313  bool savedValue_;
314  public:
315  AccessCreatesLocationsGuard(RegisterStateGeneric *rstate, bool newValue)
316  : rstate_(rstate), savedValue_(rstate->accessCreatesLocations()) {
317  rstate_->accessCreatesLocations(newValue);
318  }
320  rstate_->accessCreatesLocations(savedValue_);
321  }
322  };
323 
325  // Inherited non-constructors
327 public:
328  virtual void clear() ROSE_OVERRIDE;
329  virtual void zero() ROSE_OVERRIDE;
330  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) ROSE_OVERRIDE;
331  virtual SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) ROSE_OVERRIDE;
332  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops) ROSE_OVERRIDE;
333  virtual void print(std::ostream&, Formatter&) const ROSE_OVERRIDE;
334  virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops) ROSE_OVERRIDE;
335 
336 
338  // Initialization
340 public:
344  virtual void initialize_large();
345 
350  virtual void initialize_small();
351 
356  void initialize_nonoverlapping(const std::vector<RegisterDescriptor>&, bool initialize_to_zero);
357 
358 
360  // Value storage queries
362 public:
372  virtual RegPairs get_stored_registers() const;
373 
384  virtual bool is_partly_stored(RegisterDescriptor) const;
385 
388  virtual bool is_wholly_stored(RegisterDescriptor) const;
389 
394  virtual bool is_exactly_stored(RegisterDescriptor) const;
395 
401  virtual ExtentMap stored_parts(RegisterDescriptor) const;
402 
407  virtual RegPairs overlappingRegisters(RegisterDescriptor) const;
408 
412  virtual void erase_register(RegisterDescriptor, RiscOperators*);
413 
414 
416  // Traversals
418 public:
420  class Visitor {
421  public:
422  virtual ~Visitor() {}
423  virtual SValuePtr operator()(RegisterDescriptor, const SValuePtr&) = 0;
424  };
425 
459  virtual void traverse(Visitor&);
460 
461 
463  // Writer addresses
465 public:
473  virtual bool hasWritersAny(RegisterDescriptor) const;
474  virtual bool hasWritersAll(RegisterDescriptor) const;
482  virtual AddressSet getWritersUnion(RegisterDescriptor) const;
483 
489  virtual AddressSet getWritersIntersection(RegisterDescriptor) const;
490 
496  virtual bool insertWriters(RegisterDescriptor, const AddressSet &writerVas);
497 
503  virtual void eraseWriters(RegisterDescriptor, const AddressSet &writerVas);
504 
509  virtual void setWriters(RegisterDescriptor, const AddressSet &writers);
510 
517  virtual void eraseWriters(RegisterDescriptor);
518  virtual void eraseWriters();
522  // Per-register bit Boolean properties
525 public:
553 
560 
566 
572  virtual void eraseProperties(RegisterDescriptor);
573  virtual void eraseProperties();
580  virtual std::vector<RegisterDescriptor>
581  findProperties(const InputOutputPropertySet &required,
582  const InputOutputPropertySet &prohibited = InputOutputPropertySet()) const;
583 
589 
595 
597  // Non-public APIs
599 protected:
600  void deep_copy_values();
601 
602  // Given a register descriptor return information about what's stored in the state. The two return values are:
603  //
604  // accessedParts represent the parts of the reigster (matching major and minor numbers) that are present in the
605  // state and overlap with the specified register.
606  //
607  // preservedParts represent the parts of the register that are present in the state but don't overlap with the
608  // specified register.
609  void scanAccessedLocations(RegisterDescriptor reg, RiscOperators *ops,
610  RegPairs &accessedParts /*out*/, RegPairs &preservedParts /*out*/) const;
611 
612  // Given a register descriptor, zero out all the stored parts of the same register (by matching major and minor numbers)
613  // if the stored part overlaps with the specified register.
614  void clearOverlappingLocations(RegisterDescriptor);
615 
616  void assertStorageConditions(const std::string &where, RegisterDescriptor what) const;
617 };
618 
619 } // namespace
620 } // namespace
621 } // namespace
622 } // namespace
623 
624 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
626 #endif
627 
628 #endif
static RegisterStateGenericPtr instance(const SValuePtr &protoval, const RegisterDictionary *regdict)
Instantiate a new register state.
virtual RegPairs get_stored_registers() const
Returns the list of all registers and their values.
unsigned majorNumber() const
Property: Major number.
virtual void initialize_small()
Initialize all registers of the dictionary.
virtual bool is_partly_stored(RegisterDescriptor) const
Determines if some of the specified register is stored in the state.
size_t nBits() const
Property: Size in bits.
virtual void traverse(Visitor &)
Traverse register/value pairs.
boost::shared_ptr< class RegisterStateGeneric > RegisterStateGenericPtr
Shared-ownership pointer to generic register states.
bool accessCreatesLocations() const
Property: Whether access can create new locations.
virtual std::vector< RegisterDescriptor > findProperties(const InputOutputPropertySet &required, const InputOutputPropertySet &prohibited=InputOutputPropertySet()) const
Get registers having certain properties.
STL namespace.
virtual InputOutputPropertySet getPropertiesIntersection(RegisterDescriptor) const
Get properties.
Mapping from integers to sets.
virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops) ROSE_OVERRIDE
Write a value to a register.
virtual InputOutputPropertySet getPropertiesUnion(RegisterDescriptor) const
Get properties.
static RegisterStateGenericPtr promote(const RegisterStatePtr &from)
Run-time promotion of a base register state pointer to a RegisterStateGeneric pointer.
virtual void initialize_large()
Initialize all registers of the dictionary.
Main namespace for the ROSE library.
Sawyer::Container::Set< rose_addr_t > AddressSet
Set of virtual addresses.
virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const ROSE_OVERRIDE
Virtual constructor.
virtual void print(std::ostream &, Formatter &) const ROSE_OVERRIDE
Print the register contents.
Sawyer::Container::Map< RegStore, BitAddressSet > RegisterAddressSet
Virtual addresses for all registers.
virtual ExtentMap stored_parts(RegisterDescriptor) const
Returns a description of which bits of a register are stored.
static RegisterStateGenericPtr instance(const RegisterStateGenericPtr &other)
Instantiate a new copy of an existing register state.
static Interval baseSize(size_t lo, size_t size)
Construct an interval from one endpoint and a size.
Definition: Interval.h:161
virtual RegisterStatePtr clone() const ROSE_OVERRIDE
Make a copy of this register state.
bool accessModifiesExistingLocations() const
Property: Whether stored registers are adapted to access patterns.
Sawyer::Container::Map< RegStore, BitProperties > RegisterProperties
Boolean properties for all registers.
Sawyer::Container::Map< RegStore, RegPairs > Registers
Values for all registers.
Describes (part of) a physical CPU register.
virtual bool hasPropertyAny(RegisterDescriptor, InputOutputProperty) const
Whether a register has the specified property.
virtual AddressSet getWritersIntersection(RegisterDescriptor) const
Get writer information.
virtual void setWriters(RegisterDescriptor, const AddressSet &writers)
Set writer information.
virtual RegPairs overlappingRegisters(RegisterDescriptor) const
Find stored registers overlapping with specified register.
virtual bool insertProperties(RegisterDescriptor, const InputOutputPropertySet &)
Insert Boolean properties.
size_t offset() const
Property: Offset to least-significant bit.
virtual void updateWriteProperties(RegisterDescriptor, InputOutputProperty)
Update write properties.
virtual void setProperties(RegisterDescriptor, const InputOutputPropertySet &)
Assign property set.
virtual bool insertWriters(RegisterDescriptor, const AddressSet &writerVas)
Insert writer information.
virtual bool is_wholly_stored(RegisterDescriptor) const
Determines if the specified register is wholly stored in the state.
Sawyer::Container::Interval< size_t > BitRange
A range of bits indexes.
Base class for most instruction semantics RISC operators.
virtual void updateReadProperties(RegisterDescriptor)
Update read properties.
const RegisterDictionary * regdict
Registers that are able to be stored by this state.
virtual void accessModifiesExistingLocations(bool b)
Property: Whether stored registers are adapted to access patterns.
virtual SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) ROSE_OVERRIDE
Read a register without side effects.
virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops) ROSE_OVERRIDE
Merge register states for data flow analysis.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
virtual void zero() ROSE_OVERRIDE
Set all registers to the zero.
virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) ROSE_OVERRIDE
Read a value from a register.
virtual void clear() ROSE_OVERRIDE
Removes stored values from the register state.
virtual bool hasPropertyAll(RegisterDescriptor, InputOutputProperty) const
Whether a register has the specified property.
void initialize_nonoverlapping(const std::vector< RegisterDescriptor > &, bool initialize_to_zero)
Initialize the specified registers of the dictionary.
virtual bool hasWritersAny(RegisterDescriptor) const
Whether a register has writers.
virtual bool is_exactly_stored(RegisterDescriptor) const
Determines if the specified register is stored exactly in the state.
virtual void erase_register(RegisterDescriptor, RiscOperators *)
Cause a register to not be stored.
Sawyer::Container::IntervalSetMap< BitRange, InputOutputPropertySet > BitProperties
Boolean properties per bit.
Sawyer::Container::IntervalSetMap< BitRange, AddressSet > BitAddressSet
Virtual addresses per bit.
Defines registers available for a particular architecture.
Definition: Registers.h:35
unsigned minorNumber() const
Property: Minor number.
Sawyer::Container::Set< InputOutputProperty > InputOutputPropertySet
Set of Boolean properties.
virtual void accessCreatesLocations(bool b)
Property: Whether access can create new locations.
virtual bool hasWritersAll(RegisterDescriptor) const
Whether a register has writers.
virtual AddressSet getWritersUnion(RegisterDescriptor) const
Get writer information.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.