ROSE  0.9.9.139
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 
10 namespace Rose {
11 namespace BinaryAnalysis {
12 namespace InstructionSemantics2 {
13 namespace BaseSemantics {
14 
16 typedef boost::shared_ptr<class RegisterStateGeneric> RegisterStateGenericPtr;
17 
37  // Basic Types
39 public:
44  class RegisterNotPresent: public std::runtime_error {
45  RegisterDescriptor desc_;
46  public:
48  : std::runtime_error("accessed register is not available in register state") {}
49  };
50 
55 
60  struct RegStore {
61  unsigned majr, minr;
62 
63 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
64  private:
65  friend class boost::serialization::access;
66 
67  template<class S>
68  void serialize(S &s, const unsigned version) {
69  s & BOOST_SERIALIZATION_NVP(majr);
70  s & BOOST_SERIALIZATION_NVP(minr);
71  }
72 #endif
73 
74  public:
75  RegStore() // for serialization
76  : majr(0), minr(0) {}
77  RegStore(RegisterDescriptor d) // implicit
78  : majr(d.get_major()), minr(d.get_minor()) {}
79  bool operator<(const RegStore &other) const {
80  return majr<other.majr || (majr==other.majr && minr<other.minr);
81  }
82  };
83 
84 
86  // Types for storing values
88 public:
90  struct RegPair {
91  RegisterDescriptor desc;
92  SValuePtr value;
93 
94 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
95  private:
96  friend class boost::serialization::access;
97 
98  template<class S>
99  void serialize(S &s, const unsigned version) {
100  s & BOOST_SERIALIZATION_NVP(desc);
101  s & BOOST_SERIALIZATION_NVP(value);
102  }
103 #endif
104 
105  protected:
106  RegPair() {} // for serialization
107 
108  public:
109  RegPair(RegisterDescriptor desc, const SValuePtr &value): desc(desc), value(value) {}
110  BitRange location() const { return BitRange::baseSize(desc.get_offset(), desc.get_nbits()); }
111  };
112 
114  typedef std::vector<RegPair> RegPairs;
115 
118 
119 
121  // Types for Boolean properties
123 public:
130 
135 
136 
138  // Types for storing addresses
140 public:
143 
146 
149 
150 
152  // Data members
154 private:
155  RegisterProperties properties_; // Boolean properties for each bit of each register.
156  RegisterAddressSet writers_; // Writing instruction address set for each bit of each register
157  bool accessModifiesExistingLocations_; // Can read/write modify existing locations?
158  bool accessCreatesLocations_; // Can new locations be created?
159 
160 protected:
169  Registers registers_;
170 
172  // Serialization
174 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
175 private:
176  friend class boost::serialization::access;
177 
178  template<class S>
179  void serialize(S &s, const unsigned version) {
180  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(RegisterState);
181  s & BOOST_SERIALIZATION_NVP(properties_);
182  s & BOOST_SERIALIZATION_NVP(writers_);
183  s & BOOST_SERIALIZATION_NVP(accessModifiesExistingLocations_);
184  s & BOOST_SERIALIZATION_NVP(accessCreatesLocations_);
185  s & BOOST_SERIALIZATION_NVP(registers_);
186  }
187 #endif
188 
190  // Normal constructors
191  //
192  // These are protected because objects of this class are reference counted and always allocated on the heap.
194 protected:
195  RegisterStateGeneric() // for serialization
196  : accessModifiesExistingLocations_(true), accessCreatesLocations_(true) {}
197 
198  explicit RegisterStateGeneric(const SValuePtr &protoval, const RegisterDictionary *regdict)
199  : RegisterState(protoval, regdict), accessModifiesExistingLocations_(true), accessCreatesLocations_(true) {
200  clear();
201  }
202 
203  RegisterStateGeneric(const RegisterStateGeneric &other)
204  : RegisterState(other), properties_(other.properties_), writers_(other.writers_),
205  accessModifiesExistingLocations_(other.accessModifiesExistingLocations_),
206  accessCreatesLocations_(other.accessCreatesLocations_), registers_(other.registers_) {
207  deep_copy_values();
208  }
209 
210 
212  // Static allocating constructors
214 public:
221  static RegisterStateGenericPtr instance(const SValuePtr &protoval, const RegisterDictionary *regdict) {
222  return RegisterStateGenericPtr(new RegisterStateGeneric(protoval, regdict));
223  }
224 
226  static RegisterStateGenericPtr instance(const RegisterStateGenericPtr &other) {
227  return RegisterStateGenericPtr(new RegisterStateGeneric(*other));
228  }
229 
230 
232  // Virtual constructors
234 public:
235  virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const ROSE_OVERRIDE {
236  return instance(protoval, regdict);
237  }
238 
239  virtual RegisterStatePtr clone() const ROSE_OVERRIDE {
241  }
242 
244  // Dynamic pointer casts
246 public:
249  static RegisterStateGenericPtr promote(const RegisterStatePtr &from) {
250  RegisterStateGenericPtr retval = boost::dynamic_pointer_cast<RegisterStateGeneric>(from);
251  ASSERT_not_null(retval);
252  return retval;
253  }
254 
255 
257  // Object properties
259 public:
274  bool accessModifiesExistingLocations() const /*final*/ { return accessModifiesExistingLocations_; }
275  virtual void accessModifiesExistingLocations(bool b) { accessModifiesExistingLocations_ = b; }
283  RegisterStateGeneric *rstate_;
284  bool savedValue_;
285  public:
287  : rstate_(rstate), savedValue_(rstate->accessModifiesExistingLocations()) {
288  rstate_->accessModifiesExistingLocations(newValue);
289  }
291  rstate_->accessModifiesExistingLocations(savedValue_);
292  }
293  };
294 
302  bool accessCreatesLocations() const /*final*/ { return accessCreatesLocations_; }
303  virtual void accessCreatesLocations(bool b) { accessCreatesLocations_ = b; }
311  RegisterStateGeneric *rstate_;
312  bool savedValue_;
313  public:
314  AccessCreatesLocationsGuard(RegisterStateGeneric *rstate, bool newValue)
315  : rstate_(rstate), savedValue_(rstate->accessCreatesLocations()) {
316  rstate_->accessCreatesLocations(newValue);
317  }
319  rstate_->accessCreatesLocations(savedValue_);
320  }
321  };
322 
323  // [Robb P. Matzke 2015-09-23]: deprecated
324  bool coalesceOnRead() const /*final*/ ROSE_DEPRECATED("use accessModifiesExistingLocations instead") {
326  }
327 
328  // [Robb P. Matzke 2015-09-23]: deprecated
329  virtual void coalesceOnRead(bool b) ROSE_DEPRECATED("use accessModifiesExistingLocations instead") {
331  }
332 
333  // [Robb P. Matzke 2015-09-23]: deprecated
334  class NoCoalesceOnRead // ROSE_DEPRECATED("use AccessModifiesExistingLocationsGuard isntead")
336  public:
337  explicit NoCoalesceOnRead(RegisterStateGeneric *rstate)
338  : AccessModifiesExistingLocationsGuard(rstate, false) {}
339  };
340 
341 
343  // Inherited non-constructors
345 public:
346  virtual void clear() ROSE_OVERRIDE;
347  virtual void zero() ROSE_OVERRIDE;
348  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) ROSE_OVERRIDE;
349  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops) ROSE_OVERRIDE;
350  virtual void print(std::ostream&, Formatter&) const ROSE_OVERRIDE;
351  virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops) ROSE_OVERRIDE;
352 
353 
355  // Initialization
357 public:
361  virtual void initialize_large();
362 
367  virtual void initialize_small();
368 
373  void initialize_nonoverlapping(const std::vector<RegisterDescriptor>&, bool initialize_to_zero);
374 
375 
377  // Value storage queries
379 public:
389  virtual RegPairs get_stored_registers() const;
390 
401  virtual bool is_partly_stored(RegisterDescriptor) const;
402 
405  virtual bool is_wholly_stored(RegisterDescriptor) const;
406 
411  virtual bool is_exactly_stored(RegisterDescriptor) const;
412 
418  virtual ExtentMap stored_parts(RegisterDescriptor) const;
419 
424  virtual RegPairs overlappingRegisters(RegisterDescriptor) const;
425 
429  virtual void erase_register(RegisterDescriptor, RiscOperators*);
430 
431 
433  // Traversals
435 public:
437  class Visitor {
438  public:
439  virtual ~Visitor() {}
440  virtual SValuePtr operator()(RegisterDescriptor, const SValuePtr&) = 0;
441  };
442 
476  virtual void traverse(Visitor&);
477 
478 
480  // Writer addresses
482 public:
490  virtual bool hasWritersAny(RegisterDescriptor) const;
491  virtual bool hasWritersAll(RegisterDescriptor) const;
499  virtual AddressSet getWritersUnion(RegisterDescriptor) const;
500 
506  virtual AddressSet getWritersIntersection(RegisterDescriptor) const;
507 
513  virtual bool insertWriters(RegisterDescriptor, const AddressSet &writerVas);
514 
520  virtual void eraseWriters(RegisterDescriptor, const AddressSet &writerVas);
521 
526  virtual void setWriters(RegisterDescriptor, const AddressSet &writers);
527 
534  virtual void eraseWriters(RegisterDescriptor);
535  virtual void eraseWriters();
539  // Per-register bit Boolean properties
542 public:
570 
577 
583 
589  virtual void eraseProperties(RegisterDescriptor);
590  virtual void eraseProperties();
597  virtual std::vector<RegisterDescriptor>
598  findProperties(const InputOutputPropertySet &required,
599  const InputOutputPropertySet &prohibited = InputOutputPropertySet()) const;
600 
606 
612 
614  // Deprecated APIs
616 public:
617 
618  // Deprecated [Robb P. Matzke 2015-08-12]
619  virtual void set_latest_writer(RegisterDescriptor, rose_addr_t writer_va) ROSE_DEPRECATED("use setWriters instead");
620  virtual void clear_latest_writer(RegisterDescriptor) ROSE_DEPRECATED("use eraseWriters instead");
621  virtual void clear_latest_writers() ROSE_DEPRECATED("use eraseWriters instead");
622  virtual std::set<rose_addr_t> get_latest_writers(RegisterDescriptor) const
623  ROSE_DEPRECATED("use getWritersUnion instead");
624 
625  virtual bool get_coalesceOnRead() ROSE_DEPRECATED("use accessModifiesExistingLocations instead") {
627  }
628  virtual bool set_coalesceOnRead(bool b=true) ROSE_DEPRECATED("use accessModifiesExistingLocations instead") {
629  bool retval=accessModifiesExistingLocations();
631  return retval;
632  }
633  virtual bool clear_coalescOnRead() ROSE_DEPRECATED("use accessModifiesExistingLocations instead") {
634  bool retval = accessModifiesExistingLocations();
636  return retval;
637  }
638 
639 
641  // Non-public APIs
643 protected:
644  void deep_copy_values();
645 
646  RegPairs& scanAccessedLocations(RegisterDescriptor reg, RiscOperators *ops, bool markOverlapping,
647  RegPairs &accessedParts /*out*/, RegPairs &preservedParts /*out*/);
648 
649  void assertStorageConditions(const std::string &where, RegisterDescriptor what) const;
650 };
651 
652 } // namespace
653 } // namespace
654 } // namespace
655 } // namespace
656 
657 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
659 #endif
660 
661 #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.
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.
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.
Describes (part of) a physical CPU register.
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.
InputOutputProperty
Boolean properties related to I/O.
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
boost::shared_ptr< class RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
virtual RegisterStatePtr clone() const ROSE_OVERRIDE
Make a copy of this register state.
Base classes for instruction semantics.
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.
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.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
virtual bool insertProperties(RegisterDescriptor, const InputOutputPropertySet &)
Insert Boolean properties.
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.
Defines registers available for a particular architecture.
Definition: Registers.h:32
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 bool merge(const RegisterStatePtr &other, RiscOperators *ops) ROSE_OVERRIDE
Merge register states for data flow analysis.
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.
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.