ROSE 0.11.145.237
SValue.h
1#ifndef ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_SValue_H
2#define ROSE_BinaryAnalysis_InstructionSemantics_BaseSemantics_SValue_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/BasicTypes.h>
7#include <Combinatorics.h> // ROSE
8
9#include <Sawyer/SharedObject.h>
10#include <Sawyer/SmallObject.h>
11
12#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
13#include <boost/serialization/access.hpp>
14#include <boost/serialization/export.hpp>
15#include <boost/serialization/nvp.hpp>
16#endif
17
18namespace Rose {
19namespace BinaryAnalysis {
20namespace InstructionSemantics {
21namespace BaseSemantics {
22
24// Semantic Values
26
42public:
44 using Ptr = SValuePtr;
45
46protected:
47 size_t width;
50 // Serialization
51#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
52private:
53 friend class boost::serialization::access;
54
55 template<class S>
56 void serialize(S &s, const unsigned /*version*/) {
57 s & BOOST_SERIALIZATION_NVP(width);
58 }
59#endif
60
62 // Normal, protected, C++ constructors
63protected:
64 SValue(); // needed for serialization
65 explicit SValue(size_t nbits); // hot
66 SValue(const SValue &other);
67
68public:
69 virtual ~SValue();
70
72 // Allocating static constructor. None are needed--this class is abstract.
73
75 // Allocating virtual constructors. undefined_() needs underscores, so we do so consistently for all
76 // these allocating virtual c'tors. However, we use copy() rather than copy_() because this one is fundamentally
77 // different: the object (this) is use for more than just selecting which virtual method to invoke.
78 //
79 // The naming scheme we use here is a bit different than for most other objects for historical reasons. Most other classes
80 // use "create" and "clone" as the virtual constructor names, but SValue uses names ending in undercore, and "copy". The
81 // other difference (at least in this base class) is that we don't define any real constructors or static allocating
82 // constructors (usually named "instance")--it's because this is an abstract class.
83public:
89 virtual SValuePtr undefined_(size_t nbits) const = 0; // hot
90
99 virtual SValuePtr unspecified_(size_t nbits) const = 0;
100
106 virtual SValuePtr bottom_(size_t nBits) const = 0;
107
111 virtual SValuePtr number_(size_t nbits, uint64_t number) const = 0; // hot
112
116 virtual SValuePtr boolean_(bool value) const { return number_(1, value?1:0); }
117
121 virtual SValuePtr copy(size_t new_width=0) const = 0;
122
150 createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const = 0;
151
158 SValuePtr createMerged(const SValuePtr &other, const MergerPtr&, const SmtSolverPtr&) const /*final*/;
159
161 // Dynamic pointer casts. No-ops since this is the base class
162public:
163 static SValuePtr promote(const SValuePtr&);
164
166 // The rest of the API...
167public:
173 size_t nBits() const /*final*/;
174
180 virtual bool isBottom() const = 0;
181
189 bool isConcrete() const /*final*/;
190
197 Sawyer::Optional<uint64_t> toUnsigned() const /*final*/;
198
205 Sawyer::Optional<int64_t> toSigned() const /*final*/;
206
212 bool mustEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
213
219 bool mayEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
220
224 bool isTrue() const /*final*/;
225
229 bool isFalse() const /*final*/;
230
241 std::string comment() const /*final*/;
242 void comment(const std::string&) const /*final*/; // const is intentional (see documentation)
248 virtual void hash(Combinatorics::Hasher&) const = 0;
249
253 void print(std::ostream&) const;
254 virtual void print(std::ostream&, Formatter&) const = 0;
261 std::string toString() const;
262
265 SValuePtr obj;
266 Formatter &fmt;
267 public:
269 void print(std::ostream&) const;
270 };
271
281 WithFormatter operator+(const std::string &linePrefix);
285 // This is the virtual interface that uses names that are not consistent with most of the rest of binary analysis. Calling
286 // these directly is deprecated and we may make them protected at some time. [Robb Matzke 2021-03-18].
288public: // for backward compatibility for now, but assume protected
290 virtual bool is_number() const = 0;
291
293 virtual uint64_t get_number() const = 0;
294
298 virtual size_t get_width() const { return width; }
299 virtual void set_width(size_t nbits) { width = nbits; }
303 virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
304
306 virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
307
313 virtual std::string get_comment() const;
314 virtual void set_comment(const std::string&) const; // const is intended; cf. doxygen comment
316};
317
318std::ostream& operator<<(std::ostream&, const SValue&);
319std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
320
321} // namespace
322} // namespace
323} // namespace
324} // namespace
325
326#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
328#endif
329
330#endif
331#endif
Sawyer::Optional< uint64_t > toUnsigned() const
Converts a concrete value to a native unsigned integer.
void print(std::ostream &) const
Print a value to a stream using default format.
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 bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Virtual API.
virtual void set_width(size_t nbits)
Virtual API.
Definition SValue.h:299
virtual void set_comment(const std::string &) const
Some subclasses support the ability to add comments to values.
WithFormatter with_format(Formatter &)
Used for printing values with formatting.
SValuePtr createMerged(const SValuePtr &other, const MergerPtr &, const SmtSolverPtr &) const
Create a new value by merging two existing values.
virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Virtual API.
virtual SValuePtr number_(size_t nbits, uint64_t number) const =0
Create a new concrete semantic value.
WithFormatter operator+(Formatter &)
Used for printing values with formatting.
WithFormatter operator+(const std::string &linePrefix)
Used for printing values with formatting.
std::string toString() const
Render this symbolic expression as a string.
virtual SValuePtr boolean_(bool value) const
Create a new, Boolean value.
Definition SValue.h:116
virtual SValuePtr undefined_(size_t nbits) const =0
Create a new undefined semantic value.
virtual bool isBottom() const =0
Determines whether a value is a data-flow bottom.
virtual SValuePtr unspecified_(size_t nbits) const =0
Create a new unspecified semantic value.
bool mayEqual(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const
Tests two values for possible equality.
virtual std::string get_comment() const
Some subclasses support the ability to add comments to values.
bool isFalse() const
Returns true if concrete zero.
bool isTrue() const
Returns true if concrete non-zero.
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.
bool mustEqual(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const
Tests two values for equality.
Sawyer::Optional< int64_t > toSigned() const
Converts a concrete value to a native signed integer.
bool isConcrete() const
Determines if the value is a concrete number.
virtual SValuePtr bottom_(size_t nBits) const =0
Data-flow bottom value.
Holds a value or nothing.
Definition Optional.h:56
Creates SharedPointer from this.
Base class for reference counted objects.
Small object support.
Definition SmallObject.h:19
Base classes for instruction semantics.
Sawyer::SharedPointer< SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.
Sawyer support library.