ROSE  0.9.12.28
BaseSemanticsSValue.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_SValue_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_SValue_H
3 
4 #include <BaseSemanticsTypes.h>
5 #include <BinarySmtSolver.h>
6 
7 #include <boost/serialization/access.hpp>
8 #include <boost/serialization/nvp.hpp>
9 #include <Sawyer/SharedPointer.h>
10 
11 namespace Rose {
12 namespace BinaryAnalysis {
13 namespace InstructionSemantics2 {
14 namespace BaseSemantics {
15 
17 // Semantic Values
19 
20 // This is leftover for compatibility with an older API. The old API had code like this:
21 // User::SValue user_svalue = BaseSemantics::dynamic_pointer_cast<User::SValue>(base_svalue);
22 // Which can be replaced now with
23 // User::SValue user_svalue = base_svalue.dynamicCast<User::SValue>();
24 template<class To, class From>
25 Sawyer::SharedPointer<To> dynamic_pointer_cast(const Sawyer::SharedPointer<From> &from) {
26  return from.template dynamicCast<To>();
27 }
28 
44 protected:
45  size_t width;
47  // Serialization
49 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
50 private:
51  friend class boost::serialization::access;
52 
53  template<class S>
54  void serialize(S &s, const unsigned /*version*/) {
55  s & BOOST_SERIALIZATION_NVP(width);
56  }
57 #endif
58 
60  // Normal, protected, C++ constructors
61 protected:
62  SValue(): width(0) {} // needed for serialization
63  explicit SValue(size_t nbits): width(nbits) {} // hot
64  SValue(const SValue &other): Sawyer::SharedObject(other), width(other.width) {}
65 
66 public:
68  typedef SValuePtr Ptr;
69 
70 public:
71  virtual ~SValue() {}
72 
74  // Allocating static constructor. None are needed--this class is abstract.
75 
77  // Allocating virtual constructors. undefined_() needs underscores, so we do so consistently for all
78  // these allocating virtual c'tors. However, we use copy() rather than copy_() because this one is fundamentally
79  // different: the object (this) is use for more than just selecting which virtual method to invoke.
80  //
81  // The naming scheme we use here is a bit different than for most other objects for historical reasons. Most other classes
82  // use "create" and "clone" as the virtual constructor names, but SValue uses names ending in undercore, and "copy". The
83  // other difference (at least in this base class) is that we don't define any real constructors or static allocating
84  // constructors (usually named "instance")--it's because this is an abstract class.
85 public:
91  virtual SValuePtr undefined_(size_t nbits) const = 0; // hot
92 
101  virtual SValuePtr unspecified_(size_t nbits) const = 0;
102 
108  virtual SValuePtr bottom_(size_t nBits) const = 0;
109 
113  virtual SValuePtr number_(size_t nbits, uint64_t number) const = 0; // hot
114 
118  virtual SValuePtr boolean_(bool value) const { return number_(1, value?1:0); }
119 
123  virtual SValuePtr copy(size_t new_width=0) const = 0;
124 
152  createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const = 0;
153 
160  SValuePtr createMerged(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const /*final*/ {
161  return createOptionalMerge(other, merger, solver).orElse(copy());
162  }
163 
165  // Dynamic pointer casts. No-ops since this is the base class
166 public:
167  static SValuePtr promote(const SValuePtr &x) {
168  ASSERT_not_null(x);
169  return x;
170  }
171 
173  // The rest of the API...
174 public:
180  virtual bool isBottom() const = 0;
181 
184  virtual bool is_number() const = 0;
185 
188  virtual uint64_t get_number() const = 0;
189 
192  virtual size_t get_width() const { return width; }
193  virtual void set_width(size_t nbits) { width = nbits; }
197  virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
198 
200  virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
201 
204  bool isTrue() const {
205  return is_number() && get_number()!=0;
206  }
207 
210  bool isFalse() const {
211  return is_number() && get_number()==0;
212  }
213 
217  void print(std::ostream&) const;
218  virtual void print(std::ostream&, Formatter&) const = 0;
223  SValuePtr obj;
224  Formatter &fmt;
225  public:
226  WithFormatter(const SValuePtr &svalue, Formatter &fmt): obj(svalue), fmt(fmt) {}
227  void print(std::ostream &stream) const { obj->print(stream, fmt); }
228  };
229 
246  virtual std::string get_comment() const { return ""; }
247  virtual void set_comment(const std::string&) const {} // const is intended; cf. doxygen comment
249 };
250 
251 std::ostream& operator<<(std::ostream&, const SValue&);
252 std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
253 
254 } // namespace
255 } // namespace
256 } // namespace
257 } // namespace
258 
259 #endif
SharedObject()
Default constructor.
Definition: SharedObject.h:70
virtual std::string get_comment() const
Some subclasses support the ability to add comments to values.
virtual void set_width(size_t nbits)
Accessor for value width.
WithFormatter with_format(Formatter &fmt)
Used for printing values with formatting.
virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Returns true if two values must be equal.
void print(std::ostream &) const
Print a value to a stream using default format.
virtual SValuePtr boolean_(bool value) const
Create a new, Boolean value.
SValuePtr createMerged(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const
Create a new value by merging two existing values.
SValuePtr Ptr
Shared-ownership pointer for an SValue object.
WithFormatter operator+(Formatter &fmt)
Used for printing values with formatting.
Holds a value or nothing.
Definition: Optional.h:49
Small object support.
Definition: SmallObject.h:19
Main namespace for the ROSE library.
virtual SValuePtr unspecified_(size_t nbits) const =0
Create a new unspecified semantic value.
Reference-counting smart pointer.
Definition: SharedPointer.h:67
Name space for the entire library.
Sawyer::SharedPointer< class SmtSolver > SmtSolverPtr
Reference-counting pointer for SMT solvers.
virtual uint64_t get_number() const =0
Return the concrete number for this value.
Creates SharedPointer from this.
virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Returns true if two values could be equal.
bool isTrue() const
Returns true if concrete non-zero.
virtual void set_comment(const std::string &) const
Some subclasses support the ability to add comments to values.
virtual SValuePtr bottom_(size_t nBits) const =0
Data-flow bottom value.
virtual bool isBottom() const =0
Determines whether a value is a data-flow bottom.
virtual SValuePtr number_(size_t nbits, uint64_t number) const =0
Create a new concrete semantic value.
Base class for reference counted objects.
Definition: SharedObject.h:64
virtual size_t get_width() const
Accessor for value width.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
virtual bool is_number() const =0
Determines if the value is a concrete number.
virtual SValuePtr undefined_(size_t nbits) const =0
Create a new undefined semantic value.
virtual Sawyer::Optional< SValuePtr > createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const =0
Possibly create a new value by merging two existing values.
virtual SValuePtr copy(size_t new_width=0) const =0
Create a new value from an existing value, changing the width if new_width is non-zero.