ROSE 0.11.145.147
MultiSemantics.h
1#ifndef ROSE_BinaryAnalysis_InstructionSemantics_MultiSemantics_H
2#define ROSE_BinaryAnalysis_InstructionSemantics_MultiSemantics_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 { // documented elsewhere
10namespace BinaryAnalysis { // documented elsewhere
11namespace InstructionSemantics { // documented elsewhere
12
60namespace MultiSemantics {
61
64public:
65 std::vector<std::string> subdomain_names;
66};
67
69// Semantic values
71
74
85public:
88
90 using Ptr = SValuePtr;
91
92protected:
93 typedef std::vector<BaseSemantics::SValuePtr> Subvalues;
94 Subvalues subvalues;
95
96protected:
97 // Protected constructors
98 explicit SValue(size_t nbits);
99 SValue(const SValue &other);
100
101 void init(const SValue &other);
102
104 // Static allocating constructors
105public:
108
111
113 // Virtual allocating constructors
114public:
115
116 virtual BaseSemantics::SValuePtr bottom_(size_t nbits) const override;
117
121 virtual BaseSemantics::SValuePtr undefined_(size_t nbits) const override;
122
126 virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) const override;
127
130 virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t number) const override;
131
134 virtual SValuePtr create_empty(size_t nbits) const;
135
136 virtual BaseSemantics::SValuePtr copy(size_t /*new_width*/=0) const override;
137
140 const SmtSolverPtr&) const override;
141
143 // Override virtual methods
144public:
145 virtual bool isBottom() const override;
146 virtual void print(std::ostream&, BaseSemantics::Formatter&) const override;
147 virtual void hash(Combinatorics::Hasher&) const override;
148
150 // Override legacy methods. Override these for now, but always call the camelCase names from the base class. Eventually
151 // these snake_case names will go away and the camelCase will become the virtual functions, so be sure to specify
152 // "override" in your own code so you get notified when that change occurs.
153public:
156 virtual bool may_equal(const BaseSemantics::SValuePtr &other,
157 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
158
162 virtual bool must_equal(const BaseSemantics::SValuePtr &other,
163 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
164
165 virtual void set_width(size_t nbits) override;
166
170 virtual bool is_number() const override;
171
172 virtual uint64_t get_number() const override;
173
174
176 // Additional methods first declared at this level of the class hierarchy
177public:
180 virtual bool is_valid(size_t idx) const;
181
184 virtual void invalidate(size_t idx);
185
187 virtual BaseSemantics::SValuePtr get_subvalue(size_t idx) const;
188
192 virtual void set_subvalue(size_t idx, const BaseSemantics::SValuePtr &value);
193};
194
195
197// Register states
199
200typedef void RegisterState;
201
203typedef boost::shared_ptr<void> RegisterStatePtr;
204
206// Memory states
208
209typedef void MemoryState;
210
212typedef boost::shared_ptr<void> MemoryStatePtr;
213
215// Complete state
217
218typedef void State;
219
221typedef boost::shared_ptr<void> StatePtr;
222
224// RISC operators
226
228typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
229
235public:
238
241
242protected:
243 typedef std::vector<BaseSemantics::RiscOperatorsPtr> Subdomains;
244 Subdomains subdomains;
245 std::vector<bool> active;
246 Formatter formatter; // contains names for the subdomains
247
249 // Real constructors
250protected:
252
253 explicit RiscOperators(const BaseSemantics::StatePtr&, const SmtSolverPtr&);
254
256 // Static allocating constructors
257public:
259
264
265 static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval,
266 const SmtSolverPtr &solver = SmtSolverPtr());
267
268 static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr&, const SmtSolverPtr &solver = SmtSolverPtr());
269
271 // Virtual constructors
272public:
274 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
275
277 const SmtSolverPtr &solver = SmtSolverPtr()) const override;
278
280 // Dynamic pointer casts
281public:
282 static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &ops);
283
285 // Methods first defined at this level of the class hiearchy
286public:
292 virtual size_t add_subdomain(const BaseSemantics::RiscOperatorsPtr &subdomain, const std::string &name, bool activate=true);
293
296 return formatter;
297 }
298
300 virtual size_t nsubdomains() const;
301
304
308 virtual bool is_active(size_t idx) const;
309
312 virtual void clear_active(size_t idx);
313
317 virtual void set_active(size_t idx, bool status);
318
321 virtual void before(size_t /*idx*/) {}
322
325 virtual void after(size_t /*idx*/) {}
326
328 virtual SValuePtr svalue_empty(size_t nbits) {
329 return SValue::promote(protoval())->create_empty(nbits);
330 }
331
347 class Cursor {
348 public:
349 typedef std::vector<SValuePtr> Inputs;
350 protected:
351 RiscOperators *ops_;
352 Inputs inputs_;
353 size_t idx_;
354 public:
355 Cursor(RiscOperators *ops, const SValuePtr &arg1=SValuePtr(), const SValuePtr &arg2=SValuePtr(),
356 const SValuePtr &arg3=SValuePtr());
357 Cursor(RiscOperators *ops, const Inputs &inputs);
358
365
366 bool at_end() const;
367 void next();
368 size_t idx() const;
374
375 protected:
376 void init(const SValuePtr &arg1, const SValuePtr &arg2, const SValuePtr &arg3);
377 void init();
378 void skip_invalid();
379 bool inputs_are_valid() const;
380 };
381
383 // RISC operations and other overrides
384public:
385 virtual void print(std::ostream &o, BaseSemantics::Formatter&) const override;
386 virtual void startInstruction(SgAsmInstruction *insn) override;
387 virtual void finishInstruction(SgAsmInstruction *insn) override;
388 virtual BaseSemantics::SValuePtr undefined_(size_t nbits) override;
389 virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) override;
390 virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) override;
391 virtual BaseSemantics::SValuePtr boolean_(bool) override;
392 virtual BaseSemantics::SValuePtr bottom_(size_t nbits) override;
400 virtual BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr &a, size_t begin_bit, size_t end_bit) override;
405 const BaseSemantics::SValuePtr &nbits) override;
407 const BaseSemantics::SValuePtr &nbits) override;
409 const BaseSemantics::SValuePtr &nbits) override;
411 const BaseSemantics::SValuePtr &nbits) override;
413 const BaseSemantics::SValuePtr &nbits) override;
416 const BaseSemantics::SValuePtr &b, IteStatus&) override;
417 virtual BaseSemantics::SValuePtr unsignedExtend(const BaseSemantics::SValuePtr &a, size_t new_width) override;
418 virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a, size_t new_width) override;
422 BaseSemantics::SValuePtr &carry_out/*output*/) override;
425 const BaseSemantics::SValuePtr &b) override;
427 const BaseSemantics::SValuePtr &b) override;
429 const BaseSemantics::SValuePtr &b) override;
431 const BaseSemantics::SValuePtr &b) override;
433 const BaseSemantics::SValuePtr &b) override;
435 const BaseSemantics::SValuePtr &b) override;
438 const BaseSemantics::SValuePtr&) override;
447 SgAsmFloatType*) override;
449 SgAsmFloatType*) override;
451 SgAsmFloatType*) override;
453 SgAsmFloatType*) override;
458 const BaseSemantics::SValuePtr &dflt) override;
460 const BaseSemantics::SValuePtr &dflt) override;
461 virtual void writeRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &a) override;
463 const BaseSemantics::SValuePtr &dflt,
464 const BaseSemantics::SValuePtr &cond) override;
466 const BaseSemantics::SValuePtr &dflt) override;
468 const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override;
469};
470
471} // namespace
472} // namespace
473} // namespace
474} // namespace
475
476#endif
477#endif
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.
virtual const std::string & name() const
Property: Name used for debugging.
Helps printing multidomain values by allowing the user to specify a name for each subdomain.
size_t idx() const
Return the subdomain index for the current cursor position.
BaseSemantics::RiscOperatorsPtr operator->() const
Return the subdomain for the current cursor position.
static Inputs inputs(const BaseSemantics::SValuePtr &arg1=BaseSemantics::SValuePtr(), const BaseSemantics::SValuePtr &arg2=BaseSemantics::SValuePtr(), const BaseSemantics::SValuePtr &arg3=BaseSemantics::SValuePtr())
Class method to construct the array of inputs from a variable number of arguments.
BaseSemantics::RiscOperatorsPtr operator*() const
Return the subdomain for the current cursor position.
BaseSemantics::SValuePtr operator()(const BaseSemantics::SValuePtr &) const
Returns subdomain value of its multidomain argument.
bool at_end() const
Returns true when the cursor has gone past the last valid subdomain.
Defines RISC operators for the MultiSemantics domain.
virtual BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr &a, size_t begin_bit, size_t end_bit) override
Extracts bits from a value.
virtual BaseSemantics::SValuePtr add(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Adds two integers of equal size.
virtual BaseSemantics::SValuePtr invert(const BaseSemantics::SValuePtr &a) override
One's complement.
virtual void before(size_t)
Called before each subdomain RISC operation.
virtual BaseSemantics::SValuePtr fpIsZero(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is equal to zero.
virtual BaseSemantics::SValuePtr peekMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt) override
Read memory without side effects.
virtual BaseSemantics::SValuePtr peekRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt) override
Obtain a register value without side effects.
virtual void clear_active(size_t idx)
Makes a subdomain inactive.
virtual BaseSemantics::SValuePtr or_(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Computes bit-wise OR of two 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 reinterpret(const BaseSemantics::SValuePtr &, SgAsmType *) override
Reinterpret an expression as a different type.
virtual BaseSemantics::SValuePtr signedMultiply(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Multiplies two signed values.
virtual BaseSemantics::SValuePtr unsignedModulo(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Calculates modulo with unsigned values.
virtual BaseSemantics::SValuePtr boolean_(bool) override
Returns a Boolean value.
virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) override
Returns a number of the specified bit width.
virtual BaseSemantics::SValuePtr fpIsInfinity(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is infinity.
virtual BaseSemantics::SValuePtr undefined_(size_t nbits) override
Returns a new undefined value.
virtual BaseSemantics::SValuePtr fpConvert(const BaseSemantics::SValuePtr &, SgAsmFloatType *, SgAsmFloatType *) override
Convert from one floating-point type to another.
virtual void startInstruction(SgAsmInstruction *insn) override
Called at the beginning of every instruction.
virtual void print(std::ostream &o, BaseSemantics::Formatter &) const override
Print multi-line output for this object.
virtual BaseSemantics::SValuePtr negate(const BaseSemantics::SValuePtr &a) override
Two's complement.
virtual BaseSemantics::SValuePtr rotateLeft(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &nbits) override
Rotate bits to the left.
virtual BaseSemantics::SValuePtr fpIsNan(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is a special not-a-number bit pattern.
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 and_(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Computes bit-wise AND of two values.
virtual BaseSemantics::SValuePtr filterIndirectJumpTarget(const BaseSemantics::SValuePtr &) override
Invoked to filter indirect jumps.
virtual BaseSemantics::SValuePtr bottom_(size_t nbits) override
Returns a data-flow bottom value.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
virtual BaseSemantics::RiscOperatorsPtr get_subdomain(size_t idx) const
Returns the RiscOperators for a subdomain.
static RiscOperatorsPtr instanceFromRegisters(const RegisterDictionaryPtr &)
Static allocating constructor.
virtual BaseSemantics::SValuePtr fpAdd(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Add two floating-point values.
virtual BaseSemantics::SValuePtr fpEffectiveExponent(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Exponent of floating-point value.
virtual BaseSemantics::SValuePtr shiftRight(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &nbits) override
Returns arg shifted right logically (no sign bit).
virtual BaseSemantics::SValuePtr fpRoundTowardZero(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Round toward zero.
virtual BaseSemantics::SValuePtr concat(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Concatenates the bits of two values.
virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) override
Returns a new undefined value.
virtual BaseSemantics::SValuePtr xor_(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Computes bit-wise XOR of two values.
virtual void finishInstruction(SgAsmInstruction *insn) override
Called at the end of every instruction.
virtual BaseSemantics::SValuePtr filterReturnTarget(const BaseSemantics::SValuePtr &) override
Invoked to filter return targets.
virtual BaseSemantics::SValuePtr fpSign(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Sign of floating-point value.
virtual BaseSemantics::SValuePtr signedDivide(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Divides two signed values.
virtual BaseSemantics::SValuePtr fpIsDenormalized(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Whether a floating-point value is denormalized.
virtual BaseSemantics::SValuePtr fpMultiply(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Multiply two floating-point values.
virtual BaseSemantics::SValuePtr signedModulo(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Calculates modulo with signed values.
virtual void after(size_t)
Called after each subdomain RISC operation.
virtual BaseSemantics::SValuePtr fpSubtract(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Subtract one floating-point value from another.
virtual void set_active(size_t idx, bool status)
Makes a subdomain active or inactive.
virtual BaseSemantics::SValuePtr equalToZero(const BaseSemantics::SValuePtr &a) override
Determines whether a value is equal to zero.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::StatePtr &, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
virtual BaseSemantics::SValuePtr fpSquareRoot(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Square root.
virtual Formatter & get_formatter()
Returns a formatter containing the names of the subdomains.
virtual BaseSemantics::SValuePtr unsignedMultiply(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Multiply two unsigned values.
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.
virtual BaseSemantics::SValuePtr iteWithStatus(const BaseSemantics::SValuePtr &cond, const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b, IteStatus &) override
If-then-else with status.
virtual void writeRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &a) override
Writes a value to a register.
virtual BaseSemantics::SValuePtr fpDivide(const BaseSemantics::SValuePtr &, const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Divide one floating-point value by another.
virtual BaseSemantics::SValuePtr readRegister(RegisterDescriptor reg, const BaseSemantics::SValuePtr &dflt) override
Reads a value from a register.
virtual BaseSemantics::SValuePtr shiftRightArithmetic(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &nbits) override
Returns arg shifted right arithmetically (with sign bit).
virtual void writeMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override
Writes a value to memory.
virtual bool is_active(size_t idx) const
Returns true if a subdomain is active.
virtual size_t add_subdomain(const BaseSemantics::RiscOperatorsPtr &subdomain, const std::string &name, bool activate=true)
Add a subdomain to this MultiSemantics domain.
virtual BaseSemantics::SValuePtr shiftLeft(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &nbits) override
Returns arg shifted left.
virtual BaseSemantics::SValuePtr filterCallTarget(const BaseSemantics::SValuePtr &) override
Invoked to filter call targets.
virtual BaseSemantics::SValuePtr fpToInteger(const BaseSemantics::SValuePtr &, SgAsmFloatType *, const BaseSemantics::SValuePtr &) override
Construct an integer value from a floating-point value.
virtual BaseSemantics::SValuePtr rotateRight(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &nbits) override
Rotate bits to the right.
virtual BaseSemantics::SValuePtr signExtend(const BaseSemantics::SValuePtr &a, size_t new_width) override
Sign extends a value.
virtual BaseSemantics::SValuePtr unsignedDivide(const BaseSemantics::SValuePtr &a, const BaseSemantics::SValuePtr &b) override
Divides two unsigned values.
virtual BaseSemantics::SValuePtr unsignedExtend(const BaseSemantics::SValuePtr &a, size_t new_width) override
Extend (or shrink) operand a so it is nbits wide by adding or removing high-order bits.
virtual SValuePtr svalue_empty(size_t nbits)
Convenience function for SValue::create_empty().
virtual size_t nsubdomains() const
Returns the number of subdomains added to this MultiDomain.
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 fpFromInteger(const BaseSemantics::SValuePtr &, SgAsmFloatType *) override
Construct a floating-point value from an integer value.
Type of values manipulated by the MultiSemantics domain.
virtual void print(std::ostream &, BaseSemantics::Formatter &) const override
Print a value to a stream using default format.
virtual void set_subvalue(size_t idx, const BaseSemantics::SValuePtr &value)
Insert a subdomain value.
virtual BaseSemantics::SValuePtr copy(size_t=0) const override
Create a new value from an existing value, changing the width if new_width is non-zero.
virtual BaseSemantics::SValuePtr undefined_(size_t nbits) const override
Create a new undefined MultiSemantics value.
virtual Sawyer::Optional< BaseSemantics::SValuePtr > createOptionalMerge(const BaseSemantics::SValuePtr &other, 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 MultiSemantics value.
virtual bool isBottom() const override
Determines whether a value is a data-flow bottom.
virtual bool is_valid(size_t idx) const
Returns true if a subdomain value is valid.
static SValuePtr promote(const BaseSemantics::SValuePtr &)
Promote a base value to a MultiSemantics value.
virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t number) const override
Create a MultiSemantics value holding a concrete value.
virtual uint64_t get_number() const override
Virtual API.
static SValuePtr instance()
Construct a prototypical value.
virtual BaseSemantics::SValuePtr get_subvalue(size_t idx) const
Return a subdomain value.
virtual bool must_equal(const BaseSemantics::SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Determines if two multidomain values must be equal.
virtual BaseSemantics::SValuePtr bottom_(size_t nbits) const override
Data-flow bottom value.
virtual void invalidate(size_t idx)
Removes a subdomain value and marks it as invalid.
virtual void set_width(size_t nbits) override
Virtual API.
virtual bool is_number() const override
Determines if the value is a concrete number.
virtual void hash(Combinatorics::Hasher &) const override
Hash this semantic value.
virtual SValuePtr create_empty(size_t nbits) const
Create a new MultiSemantics value with no valid subvalues.
virtual bool may_equal(const BaseSemantics::SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Determines if two multidomain values might be equal.
Describes (part of) a physical CPU register.
Holds a value or nothing.
Definition Optional.h:56
Reference-counting intrusive smart pointer.
Floating point types.
Base class for machine instructions.
Base class for binary types.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer to a multi-semantic value.
boost::shared_ptr< void > RegisterStatePtr
Shared-ownership pointer to a multi-semantics register state.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to multi-semantics RISC operators.
boost::shared_ptr< void > MemoryStatePtr
Shared-ownership pointer to a multi-semantics memory state.
boost::shared_ptr< void > StatePtr
Shared-ownership pointer to a multi-semantics state.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
The ROSE library.