ROSE 0.11.145.147
NullSemantics.h
1#ifndef ROSE_BinaryAnalysis_InstructionSemantics_NullSemantics_H
2#define ROSE_BinaryAnalysis_InstructionSemantics_NullSemantics_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/BasicTypes.h>
7#include <Rose/BinaryAnalysis/InstructionSemantics/BaseSemantics.h>
8
9namespace Rose {
10namespace BinaryAnalysis { // documented elsewhere
11namespace InstructionSemantics { // documented elsewhere
12
13
18namespace NullSemantics {
19
21// Semantic values
23
26
29public:
32
34 using Ptr = SValuePtr;
35
37 // Real constructors.
38protected:
39 explicit SValue(size_t nbits): BaseSemantics::SValue(nbits) {}
40
42 // Static allocating constructors
43public:
46 return SValuePtr(new SValue(1));
47 }
48
50 static SValuePtr instance(size_t nbits) {
51 return SValuePtr(new SValue(nbits));
52 }
53
55 static SValuePtr instance(size_t nbits, uint64_t /*number*/) {
56 return SValuePtr(new SValue(nbits)); // the number is not important in this domain
57 }
58
60 static SValuePtr instance(const SValuePtr &other) {
61 return SValuePtr(new SValue(*other));
62 }
63
65 // Virtual constructors
66public:
67 virtual BaseSemantics::SValuePtr bottom_(size_t nBits) const override {
68 return instance(nBits);
69 }
70 virtual BaseSemantics::SValuePtr undefined_(size_t nBits) const override {
71 return instance(nBits);
72 }
73 virtual BaseSemantics::SValuePtr unspecified_(size_t nBits) const override {
74 return instance(nBits);
75 }
76 virtual BaseSemantics::SValuePtr number_(size_t nBits, uint64_t number) const override {
77 return instance(nBits, number);
78 }
79 virtual BaseSemantics::SValuePtr copy(size_t new_width=0) const override {
80 SValuePtr retval(new SValue(*this));
81 if (new_width!=0 && new_width!=retval->nBits())
82 retval->set_width(new_width);
83 return retval;
84 }
87 return Sawyer::Nothing();
88 }
89
91 // Dynamic pointer casting
92public:
95 SValuePtr retval = v.dynamicCast<SValue>();
96 ASSERT_not_null(retval);
97 return retval;
98 }
99
101 // Implementations of functions inherited
102public:
103 virtual bool isBottom() const override {
104 return false;
105 }
106
107 virtual void print(std::ostream &stream, BaseSemantics::Formatter&) const override {
108 stream <<"VOID[" <<nBits() <<"]";
109 }
110
111 virtual void hash(Combinatorics::Hasher &hasher) const override {
112 hasher.insert(0); // hash depends on number of SValues hashed, but not any content
113 }
114
116 // Override legacy members. These now are called by the camelCase names in the base class. Eventually we'll switch the
117 // camelCase names to be the virtual functions and get rid of the snake_case names, so be sure to specify "override" in
118 // your own code so you know when we make the switch.
119public:
120 // See isConcrete
121 virtual bool is_number() const override {
122 return false;
123 }
124
125 // See toUnsigned and toSigned
126 virtual uint64_t get_number() const override {
127 ASSERT_not_reachable("not a number");
128 uint64_t retval;
129 return retval;
130 }
131
132 // See mayEqual
133 virtual bool may_equal(const BaseSemantics::SValuePtr&, const SmtSolverPtr& = SmtSolverPtr()) const override {
134 return true;
135 }
136
137 // See mustEqual
138 virtual bool must_equal(const BaseSemantics::SValuePtr &other, const SmtSolverPtr& = SmtSolverPtr()) const override {
139 return this == getRawPointer(other); // must be equal if they're both the same object
140 }
141
142};
143
144
146// Register state
148
150typedef boost::shared_ptr<class RegisterState> RegisterStatePtr;
151
156public:
159
162
163protected:
164 RegisterState(const RegisterState &other)
165 : BaseSemantics::RegisterState(other) {}
166
169
170public:
172 return RegisterStatePtr(new RegisterState(protoval, regdict));
173 }
174
176 const RegisterDictionaryPtr &regdict) const override {
177 return instance(protoval, regdict);
178 }
179
180 virtual BaseSemantics::RegisterStatePtr clone() const override {
181 return RegisterStatePtr(new RegisterState(*this));
182 }
183
184 static RegisterStatePtr promote(const BaseSemantics::RegisterStatePtr &from) {
185 RegisterStatePtr retval = boost::dynamic_pointer_cast<RegisterState>(from);
186 ASSERT_not_null(retval);
187 return retval;
188 }
189
191 return false;
192 }
193
194 virtual void clear() override {}
195 virtual void zero() override {}
196
199 return protoval()->undefined_(reg.nBits());
200 }
201
204 return protoval()->undefined_(reg.nBits());
205 }
206
208
209 virtual void updateReadProperties(RegisterDescriptor) override {}
211
212 virtual void hash(Combinatorics::Hasher&, BaseSemantics::RiscOperators*) const override {}
213
214 virtual void print(std::ostream&, BaseSemantics::Formatter&) const override {}
215};
216
217
219// Memory state
221
223typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
224
229public:
232
235
236protected:
237 MemoryState(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval)
238 : BaseSemantics::MemoryState(addrProtoval, valProtoval) {}
239
240 MemoryState(const MemoryStatePtr &other)
241 : BaseSemantics::MemoryState(other) {}
242
243public:
244 static MemoryStatePtr instance(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval) {
245 return MemoryStatePtr(new MemoryState(addrProtoval, valProtoval));
246 }
247
248public:
250 const BaseSemantics::SValuePtr &valProtoval) const override {
251 return instance(addrProtoval, valProtoval);
252 }
253
254 virtual BaseSemantics::MemoryStatePtr clone() const override {
255 return MemoryStatePtr(new MemoryState(*this));
256 }
257
258public:
259 static MemoryStatePtr promote(const BaseSemantics::MemoryStatePtr &x) {
260 MemoryStatePtr retval = boost::dynamic_pointer_cast<MemoryState>(x);
261 ASSERT_not_null(x);
262 return retval;
263 }
264
265public:
266 virtual void clear() override {}
267
269 const BaseSemantics::SValuePtr &dflt,
270 BaseSemantics::RiscOperators */*addrOps*/,
271 BaseSemantics::RiscOperators */*valOps*/) override {
272 return dflt->copy();
273 }
274
275 virtual void writeMemory(const BaseSemantics::SValuePtr &/*addr*/, const BaseSemantics::SValuePtr &/*value*/,
276 BaseSemantics::RiscOperators */*addrOps*/, BaseSemantics::RiscOperators */*valOps*/) override {}
277
279 BaseSemantics::RiscOperators */*addrOps*/,
280 BaseSemantics::RiscOperators */*valOps*/) override {
281 return dflt->copy();
282 }
283
285 BaseSemantics::RiscOperators */*valOps*/) const override {}
286
287 virtual void print(std::ostream&, BaseSemantics::Formatter&) const override {}
288
289 virtual bool merge(const BaseSemantics::MemoryStatePtr &/*other*/, BaseSemantics::RiscOperators */*addrOps*/,
290 BaseSemantics::RiscOperators */*valOps*/) override {
291 return false;
292 }
293};
294
295
297// Complete state
299
300typedef BaseSemantics::State State;
301typedef BaseSemantics::StatePtr StatePtr;
302
303
305// RISC operators
307
309typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
310
313public:
316
319
321 // Real constructors
322protected:
324
325 explicit RiscOperators(const BaseSemantics::StatePtr&, const SmtSolverPtr&);
326
328 // Static allocating constructors
329public:
331
335
338 const SmtSolverPtr &solver = SmtSolverPtr());
339
342
344 // Virtual constructors
345public:
347 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
348
350 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
351
353 // Risc operators inherited
354public:
356 const BaseSemantics::SValuePtr &b_) override;
357
359 const BaseSemantics::SValuePtr &b_) override;
360
362 const BaseSemantics::SValuePtr &b_) override;
363
365
367 size_t begin_bit, size_t end_bit) override;
368
370 const BaseSemantics::SValuePtr &b_) override;
371
373
375
377 const BaseSemantics::SValuePtr &sa_) override;
378
380 const BaseSemantics::SValuePtr &sa_) override;
381
383 const BaseSemantics::SValuePtr &sa_) override;
384
386 const BaseSemantics::SValuePtr &sa_) override;
387
389 const BaseSemantics::SValuePtr &sa_) override;
390
392
394 const BaseSemantics::SValuePtr &a_,
395 const BaseSemantics::SValuePtr &b_,
396 IteStatus&) override;
397
398 virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override;
399
401 const BaseSemantics::SValuePtr &b_) override;
402
404 const BaseSemantics::SValuePtr &b_,
405 const BaseSemantics::SValuePtr &c_,
406 BaseSemantics::SValuePtr &carry_out/*out*/) override;
407
409
411 const BaseSemantics::SValuePtr &b_) override;
412
414 const BaseSemantics::SValuePtr &b_) override;
415
417 const BaseSemantics::SValuePtr &b_) override;
418
420 const BaseSemantics::SValuePtr &b_) override;
421
423 const BaseSemantics::SValuePtr &b_) override;
424
426 const BaseSemantics::SValuePtr &b_) override;
427
429 const BaseSemantics::SValuePtr &addr,
430 const BaseSemantics::SValuePtr &dflt,
431 const BaseSemantics::SValuePtr &cond) override;
432
434 const BaseSemantics::SValuePtr &addr,
435 const BaseSemantics::SValuePtr &dflt) override;
436
437 virtual void writeMemory(RegisterDescriptor segreg,
438 const BaseSemantics::SValuePtr &addr,
439 const BaseSemantics::SValuePtr &data,
440 const BaseSemantics::SValuePtr &cond) override;
441};
442
443} // namespace
444} // namespace
445} // namespace
446} // namespace
447
448#endif
449#endif
RegisterDictionaryPtr regdict
Registers that are able to be stored by this state.
Base class for most instruction semantics RISC operators.
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
Base class for semantics machine states.
Definition State.h:41
virtual BaseSemantics::MemoryStatePtr create(const BaseSemantics::SValuePtr &addrProtoval, const BaseSemantics::SValuePtr &valProtoval) const override
Virtual allocating constructor.
virtual bool merge(const BaseSemantics::MemoryStatePtr &, BaseSemantics::RiscOperators *, BaseSemantics::RiscOperators *) override
Merge memory states for data flow analysis.
virtual BaseSemantics::SValuePtr peekMemory(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *, BaseSemantics::RiscOperators *) override
Read a value from memory without side effects.
virtual BaseSemantics::MemoryStatePtr clone() const override
Virtual allocating copy constructor.
virtual void print(std::ostream &, BaseSemantics::Formatter &) const override
Print a memory state to more than one line of output.
virtual void writeMemory(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, BaseSemantics::RiscOperators *, BaseSemantics::RiscOperators *) override
Write a value to memory.
virtual void hash(Combinatorics::Hasher &, BaseSemantics::RiscOperators *, BaseSemantics::RiscOperators *) const override
Calculate a hash for this memory state.
virtual BaseSemantics::SValuePtr readMemory(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *, BaseSemantics::RiscOperators *) override
Read a value from memory.
virtual void hash(Combinatorics::Hasher &, BaseSemantics::RiscOperators *) const override
Hash the register state.
virtual BaseSemantics::SValuePtr readRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &, BaseSemantics::RiscOperators *) override
Read a value from a register.
virtual void updateReadProperties(RegisterDescriptor) override
Update register properties after reading a register.
virtual void print(std::ostream &, BaseSemantics::Formatter &) const override
Print the register contents.
virtual void zero() override
Set all registers to the zero.
virtual void clear() override
Removes stored values from the register state.
virtual void updateWriteProperties(RegisterDescriptor, BaseSemantics::InputOutputProperty) override
Update register properties after writing to a register.
virtual BaseSemantics::SValuePtr peekRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &, BaseSemantics::RiscOperators *) override
Read a register without side effects.
virtual BaseSemantics::RegisterStatePtr clone() const override
Make a copy of this register state.
virtual void writeRegister(RegisterDescriptor, const BaseSemantics::SValuePtr &, BaseSemantics::RiscOperators *) override
Write a value to a register.
virtual BaseSemantics::RegisterStatePtr create(const BaseSemantics::SValuePtr &protoval, const RegisterDictionaryPtr &regdict) const override
Virtual constructor.
virtual bool merge(const BaseSemantics::RegisterStatePtr &, BaseSemantics::RiscOperators *) override
Merge register states for data flow analysis.
NullSemantics operators always return a new undefined value.
virtual BaseSemantics::SValuePtr unsignedModulo(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Calculates modulo with unsigned values.
virtual BaseSemantics::SValuePtr rotateRight(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Rotate bits to the right.
virtual BaseSemantics::SValuePtr iteWithStatus(const BaseSemantics::SValuePtr &sel_, const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_, IteStatus &) override
If-then-else with status.
virtual BaseSemantics::SValuePtr unsignedDivide(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Divides two unsigned values.
virtual BaseSemantics::SValuePtr signedModulo(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Calculates modulo with signed values.
virtual BaseSemantics::SValuePtr readMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt, const BaseSemantics::SValuePtr &cond) override
Reads a value from memory.
virtual BaseSemantics::SValuePtr leastSignificantSetBit(const BaseSemantics::SValuePtr &a_) override
Returns position of least significant set bit; zero when no bits are set.
virtual BaseSemantics::SValuePtr shiftRightArithmetic(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted right arithmetically (with sign bit).
virtual BaseSemantics::SValuePtr invert(const BaseSemantics::SValuePtr &a_) override
One's complement.
virtual BaseSemantics::SValuePtr xor_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise XOR of two values.
virtual BaseSemantics::SValuePtr rotateLeft(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Rotate bits to the left.
virtual BaseSemantics::SValuePtr and_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise AND of two values.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
virtual BaseSemantics::SValuePtr concat(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Concatenates the bits of two values.
virtual BaseSemantics::SValuePtr shiftRight(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted right logically (no sign bit).
virtual BaseSemantics::SValuePtr unsignedMultiply(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Multiply two unsigned values.
virtual BaseSemantics::SValuePtr signedDivide(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Divides two signed values.
static RiscOperatorsPtr instanceFromRegisters(const RegisterDictionaryPtr &regdict)
Instantiate a new RiscOperators object and configures it to use semantic values and states that are d...
virtual void writeMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override
Writes a value to memory.
virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a_, size_t new_width) override
Sign extends a value.
virtual BaseSemantics::SValuePtr add(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Adds two integers of equal size.
virtual BaseSemantics::SValuePtr mostSignificantSetBit(const BaseSemantics::SValuePtr &a_) override
Returns position of most significant set bit; zero when no bits are set.
virtual BaseSemantics::SValuePtr equalToZero(const BaseSemantics::SValuePtr &a_) override
Determines whether a value is equal to zero.
virtual BaseSemantics::SValuePtr addWithCarries(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_, const BaseSemantics::SValuePtr &c_, BaseSemantics::SValuePtr &carry_out) override
Add two values of equal size and a carry bit.
static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr())
Static allocating constructor.
virtual BaseSemantics::SValuePtr peekMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt) override
Read memory without side effects.
virtual BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr &a_, size_t begin_bit, size_t end_bit) override
Extracts bits from a value.
static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr &, const SmtSolverPtr &solver=SmtSolverPtr())
Constructor.
virtual BaseSemantics::SValuePtr or_(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Computes bit-wise OR of two values.
virtual BaseSemantics::SValuePtr signedMultiply(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &b_) override
Multiplies two signed values.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::StatePtr &, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
virtual BaseSemantics::SValuePtr negate(const BaseSemantics::SValuePtr &a_) override
Two's complement.
virtual BaseSemantics::SValuePtr shiftLeft(const BaseSemantics::SValuePtr &a_, const BaseSemantics::SValuePtr &sa_) override
Returns arg shifted left.
virtual bool may_equal(const BaseSemantics::SValuePtr &, const SmtSolverPtr &=SmtSolverPtr()) const override
Virtual API.
virtual BaseSemantics::SValuePtr undefined_(size_t nBits) const override
Create a new undefined semantic value.
virtual Sawyer::Optional< BaseSemantics::SValuePtr > createOptionalMerge(const BaseSemantics::SValuePtr &, const BaseSemantics::MergerPtr &, const SmtSolverPtr &) const override
Possibly create a new value by merging two existing values.
virtual BaseSemantics::SValuePtr unspecified_(size_t nBits) const override
Create a new unspecified semantic value.
static SValuePtr instance(const SValuePtr &other)
Instantiate a new copy of an existing value.
virtual void print(std::ostream &stream, BaseSemantics::Formatter &) const override
Print a value to a stream using default format.
virtual BaseSemantics::SValuePtr copy(size_t new_width=0) const override
Create a new value from an existing value, changing the width if new_width is non-zero.
static SValuePtr instance(size_t nbits, uint64_t)
Instantiate a new concrete value.
virtual bool must_equal(const BaseSemantics::SValuePtr &other, const SmtSolverPtr &=SmtSolverPtr()) const override
Virtual API.
virtual uint64_t get_number() const override
Virtual API.
static SValuePtr promote(const BaseSemantics::SValuePtr &v)
Promote a base value to a NullSemantics value.
virtual bool isBottom() const override
Determines whether a value is a data-flow bottom.
static SValuePtr instance()
Instantiate a new prototypical values.
virtual BaseSemantics::SValuePtr bottom_(size_t nBits) const override
Data-flow bottom value.
virtual void hash(Combinatorics::Hasher &hasher) const override
Hash this semantic value.
static SValuePtr instance(size_t nbits)
Instantiate a new undefined value.
virtual BaseSemantics::SValuePtr number_(size_t nBits, uint64_t number) const override
Create a new concrete semantic value.
Describes (part of) a physical CPU register.
size_t nBits() const
Property: Size in bits.
void insert(const std::string &x)
Insert data into the digest.
Represents no value.
Definition Optional.h:36
Holds a value or nothing.
Definition Optional.h:56
Reference-counting intrusive smart pointer.
SharedPointer< U > dynamicCast() const
Dynamic cast.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
boost::shared_ptr< MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
boost::shared_ptr< RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
boost::shared_ptr< class RegisterState > RegisterStatePtr
Shared-ownership pointer.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer.
boost::shared_ptr< class MemoryState > MemoryStatePtr
Shared-ownership pointer.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.