ROSE  0.11.50.0
SValue.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_SValue_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_SValue_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <Rose/BinaryAnalysis/InstructionSemantics2/BaseSemantics/Types.h>
7 #include <Rose/BinaryAnalysis/SmtSolver.h>
8 
9 #include <boost/serialization/access.hpp>
10 #include <boost/serialization/export.hpp>
11 #include <boost/serialization/nvp.hpp>
12 #include <Sawyer/SharedPointer.h>
13 
14 namespace Rose {
15 namespace BinaryAnalysis {
16 namespace InstructionSemantics2 {
17 namespace BaseSemantics {
18 
20 // Semantic Values
22 
23 // This is leftover for compatibility with an older API. The old API had code like this:
24 // User::SValue user_svalue = BaseSemantics::dynamic_pointer_cast<User::SValue>(base_svalue);
25 // Which can be replaced now with
26 // User::SValue user_svalue = base_svalue.dynamicCast<User::SValue>();
27 template<class To, class From>
28 Sawyer::SharedPointer<To> dynamic_pointer_cast(const Sawyer::SharedPointer<From> &from) {
29  return from.template dynamicCast<To>();
30 }
31 
47 public:
49  using Ptr = SValuePtr;
50 
51 protected:
52  size_t width;
54  // Serialization
56 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
57 private:
58  friend class boost::serialization::access;
59 
60  template<class S>
61  void serialize(S &s, const unsigned /*version*/) {
62  s & BOOST_SERIALIZATION_NVP(width);
63  }
64 #endif
65 
67  // Normal, protected, C++ constructors
68 protected:
69  SValue(): width(0) {} // needed for serialization
70  explicit SValue(size_t nbits): width(nbits) {} // hot
71  SValue(const SValue &other): Sawyer::SharedObject(other), width(other.width) {}
72 
73 public:
74  virtual ~SValue() {}
75 
77  // Allocating static constructor. None are needed--this class is abstract.
78 
80  // Allocating virtual constructors. undefined_() needs underscores, so we do so consistently for all
81  // these allocating virtual c'tors. However, we use copy() rather than copy_() because this one is fundamentally
82  // different: the object (this) is use for more than just selecting which virtual method to invoke.
83  //
84  // The naming scheme we use here is a bit different than for most other objects for historical reasons. Most other classes
85  // use "create" and "clone" as the virtual constructor names, but SValue uses names ending in undercore, and "copy". The
86  // other difference (at least in this base class) is that we don't define any real constructors or static allocating
87  // constructors (usually named "instance")--it's because this is an abstract class.
88 public:
94  virtual SValuePtr undefined_(size_t nbits) const = 0; // hot
95 
104  virtual SValuePtr unspecified_(size_t nbits) const = 0;
105 
111  virtual SValuePtr bottom_(size_t nBits) const = 0;
112 
116  virtual SValuePtr number_(size_t nbits, uint64_t number) const = 0; // hot
117 
121  virtual SValuePtr boolean_(bool value) const { return number_(1, value?1:0); }
122 
126  virtual SValuePtr copy(size_t new_width=0) const = 0;
127 
155  createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const = 0;
156 
163  SValuePtr createMerged(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const /*final*/ {
164  return createOptionalMerge(other, merger, solver).orElse(copy());
165  }
166 
168  // Dynamic pointer casts. No-ops since this is the base class
169 public:
170  static SValuePtr promote(const SValuePtr &x) {
171  ASSERT_not_null(x);
172  return x;
173  }
174 
176  // The rest of the API...
177 public:
183  size_t nBits() const /*final*/;
184 
190  virtual bool isBottom() const = 0;
191 
199  bool isConcrete() const /*final*/;
200 
207  Sawyer::Optional<uint64_t> toUnsigned() const /*final*/;
208 
215  Sawyer::Optional<int64_t> toSigned() const /*final*/;
216 
222  bool mustEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
223 
229  bool mayEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
230 
234  bool isTrue() const /*final*/;
235 
239  bool isFalse() const /*final*/;
240 
251  std::string comment() const /*final*/;
252  void comment(const std::string&) const /*final*/; // const is intentional (see documentation)
258  virtual void hash(Combinatorics::Hasher&) const = 0;
259 
263  void print(std::ostream&) const;
264  virtual void print(std::ostream&, Formatter&) const = 0;
269  SValuePtr obj;
270  Formatter &fmt;
271  public:
272  WithFormatter(const SValuePtr &svalue, Formatter &fmt): obj(svalue), fmt(fmt) {}
273  void print(std::ostream &stream) const { obj->print(stream, fmt); }
274  };
275 
285  WithFormatter operator+(const std::string &linePrefix);
288  // This is the virtual interface that uses names that are not consistent with most of the rest of binary analysis. Calling
290  // these directly is deprecated and we may make them protected at some time. [Robb Matzke 2021-03-18].
292 public: // for backward compatibility for now, but assume protected
294  virtual bool is_number() const = 0;
295 
297  virtual uint64_t get_number() const = 0;
298 
302  virtual size_t get_width() const { return width; }
303  virtual void set_width(size_t nbits) { width = nbits; }
307  virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
308 
310  virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
311 
317  virtual std::string get_comment() const { return ""; }
318  virtual void set_comment(const std::string&) const {} // const is intended; cf. doxygen comment
320 };
321 
322 std::ostream& operator<<(std::ostream&, const SValue&);
323 std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
324 
325 } // namespace
326 } // namespace
327 } // namespace
328 } // namespace
329 
331 
332 #endif
333 #endif
SharedObject()
Default constructor.
Definition: SharedObject.h:70
virtual std::string get_comment() const
Some subclasses support the ability to add comments to values.
Definition: SValue.h:317
virtual void set_width(size_t nbits)
Virtual API.
Definition: SValue.h:303
WithFormatter with_format(Formatter &fmt)
Used for printing values with formatting.
Definition: SValue.h:283
virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Virtual API.
void print(std::ostream &) const
Print a value to a stream using default format.
bool isFalse() const
Returns true if concrete zero.
virtual SValuePtr boolean_(bool value) const
Create a new, Boolean value.
Definition: SValue.h:121
SValuePtr createMerged(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const
Create a new value by merging two existing values.
Definition: SValue.h:163
WithFormatter operator+(Formatter &fmt)
Used for printing values with formatting.
Definition: SValue.h:284
STL namespace.
Holds a value or nothing.
Definition: Optional.h:49
Small object support.
Definition: SmallObject.h:19
virtual void hash(Combinatorics::Hasher &) const =0
Hash this semantic value.
Main namespace for the ROSE library.
virtual SValuePtr unspecified_(size_t nbits) const =0
Create a new unspecified semantic value.
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
Name space for the entire library.
Definition: FeasiblePath.h:787
Sawyer::Optional< uint64_t > toUnsigned() const
Converts a concrete value to a native unsigned integer.
Sawyer::Optional< int64_t > toSigned() const
Converts a concrete value to a native signed integer.
bool mustEqual(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const
Tests two values for equality.
Creates SharedPointer from this.
virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Virtual API.
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.
Definition: SValue.h:318
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
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
bool isConcrete() const
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.
bool mayEqual(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const
Tests two values for possible equality.
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.
std::shared_ptr< class SmtSolver > SmtSolverPtr
Reference-counting pointer for SMT solvers.
Definition: SmtSolver.h:25