ROSE 0.11.145.192
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#include <boost/serialization/access.hpp>
13#include <boost/serialization/export.hpp>
14#include <boost/serialization/nvp.hpp>
15
16namespace Rose {
17namespace BinaryAnalysis {
18namespace InstructionSemantics {
19namespace BaseSemantics {
20
22// Semantic Values
24
40public:
42 using Ptr = SValuePtr;
43
44protected:
45 size_t width;
48 // Serialization
49#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
50private:
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
61protected:
62 SValue(); // needed for serialization
63 explicit SValue(size_t nbits); // hot
64 SValue(const SValue &other);
65
66public:
67 virtual ~SValue();
68
70 // Allocating static constructor. None are needed--this class is abstract.
71
73 // Allocating virtual constructors. undefined_() needs underscores, so we do so consistently for all
74 // these allocating virtual c'tors. However, we use copy() rather than copy_() because this one is fundamentally
75 // different: the object (this) is use for more than just selecting which virtual method to invoke.
76 //
77 // The naming scheme we use here is a bit different than for most other objects for historical reasons. Most other classes
78 // use "create" and "clone" as the virtual constructor names, but SValue uses names ending in undercore, and "copy". The
79 // other difference (at least in this base class) is that we don't define any real constructors or static allocating
80 // constructors (usually named "instance")--it's because this is an abstract class.
81public:
87 virtual SValuePtr undefined_(size_t nbits) const = 0; // hot
88
97 virtual SValuePtr unspecified_(size_t nbits) const = 0;
98
104 virtual SValuePtr bottom_(size_t nBits) const = 0;
105
109 virtual SValuePtr number_(size_t nbits, uint64_t number) const = 0; // hot
110
114 virtual SValuePtr boolean_(bool value) const { return number_(1, value?1:0); }
115
119 virtual SValuePtr copy(size_t new_width=0) const = 0;
120
148 createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const = 0;
149
156 SValuePtr createMerged(const SValuePtr &other, const MergerPtr&, const SmtSolverPtr&) const /*final*/;
157
159 // Dynamic pointer casts. No-ops since this is the base class
160public:
161 static SValuePtr promote(const SValuePtr&);
162
164 // The rest of the API...
165public:
171 size_t nBits() const /*final*/;
172
178 virtual bool isBottom() const = 0;
179
187 bool isConcrete() const /*final*/;
188
195 Sawyer::Optional<uint64_t> toUnsigned() const /*final*/;
196
203 Sawyer::Optional<int64_t> toSigned() const /*final*/;
204
210 bool mustEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
211
217 bool mayEqual(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const /*final*/;
218
222 bool isTrue() const /*final*/;
223
227 bool isFalse() const /*final*/;
228
239 std::string comment() const /*final*/;
240 void comment(const std::string&) const /*final*/; // const is intentional (see documentation)
246 virtual void hash(Combinatorics::Hasher&) const = 0;
247
251 void print(std::ostream&) const;
252 virtual void print(std::ostream&, Formatter&) const = 0;
259 std::string toString() const;
260
263 SValuePtr obj;
264 Formatter &fmt;
265 public:
267 void print(std::ostream&) const;
268 };
269
279 WithFormatter operator+(const std::string &linePrefix);
283 // This is the virtual interface that uses names that are not consistent with most of the rest of binary analysis. Calling
284 // these directly is deprecated and we may make them protected at some time. [Robb Matzke 2021-03-18].
286public: // for backward compatibility for now, but assume protected
288 virtual bool is_number() const = 0;
289
291 virtual uint64_t get_number() const = 0;
292
296 virtual size_t get_width() const { return width; }
297 virtual void set_width(size_t nbits) { width = nbits; }
301 virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
302
304 virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
305
311 virtual std::string get_comment() const;
312 virtual void set_comment(const std::string&) const; // const is intended; cf. doxygen comment
314};
315
316std::ostream& operator<<(std::ostream&, const SValue&);
317std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
318
319} // namespace
320} // namespace
321} // namespace
322} // namespace
323
325
326#endif
327#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:297
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:114
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.