ROSE  0.9.11.10
BaseSemantics2.h
1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics2_BaseSemantics_H
3 
4 #include "BinarySmtSolver.h"
5 #include "Diagnostics.h"
6 #include "FormatRestorer.h"
7 #include "Registers.h"
8 #include "RoseException.h"
9 
10 #include <boost/shared_ptr.hpp>
11 #include <boost/enable_shared_from_this.hpp>
12 #include <boost/optional.hpp>
13 #include <boost/serialization/access.hpp>
14 #ifndef USE_ROSE // [Robb P Matzke 2016-11-11]: ROSE cannot compile this header
15 #include <boost/serialization/shared_ptr.hpp>
16 #endif
17 #include <boost/serialization/string.hpp>
18 #include <Sawyer/Assert.h>
19 #include <Sawyer/IntervalMap.h>
20 #include <Sawyer/IntervalSetMap.h>
21 #include <Sawyer/Map.h>
22 #include <Sawyer/Optional.h>
23 #include <Sawyer/Set.h>
24 
25 namespace Rose {
26 namespace BinaryAnalysis {
27 
295 namespace InstructionSemantics2 {
296 
299 
301 void initDiagnostics();
302 
305 namespace BaseSemantics {
306 
307 class RiscOperators;
308 
312 class Formatter {
313 public:
314  Formatter(): regdict(NULL), suppress_initial_values(false), indentation_suffix(" "), show_latest_writers(true),
315  show_properties(true) {}
316  virtual ~Formatter() {}
317 
320  const RegisterDictionary *get_register_dictionary() const { return regdict; }
321  void set_register_dictionary(const RegisterDictionary *rd) { regdict = rd; }
327  bool get_suppress_initial_values() const { return suppress_initial_values; }
328  void set_suppress_initial_values(bool b=true) { suppress_initial_values=b; }
334  std::string get_line_prefix() const { return line_prefix; }
335  void set_line_prefix(const std::string &s) { line_prefix = s; }
340  std::string get_indentation_suffix() const { return indentation_suffix; }
341  void set_indentation_suffix(const std::string &s) { indentation_suffix = s; }
346  bool get_show_latest_writers() const { return show_latest_writers; }
347  void set_show_latest_writers(bool b=true) { show_latest_writers = b; }
348  void clear_show_latest_writers() { show_latest_writers = false; }
353  bool get_show_properties() const { return show_properties; }
354  void set_show_properties(bool b=true) { show_properties = b; }
355  void clear_show_properties() { show_properties = false; }
358 protected:
359  const RegisterDictionary *regdict;
360  bool suppress_initial_values;
361  std::string line_prefix;
362  std::string indentation_suffix;
363  bool show_latest_writers;
364  bool show_properties;
365 };
366 
370 class Indent {
371 private:
372  Formatter &fmt;
373  std::string old_line_prefix;
374 public:
375  Indent(Formatter &fmt): fmt(fmt) {
376  old_line_prefix = fmt.get_line_prefix();
377  fmt.set_line_prefix(old_line_prefix + fmt.get_indentation_suffix());
378  }
379  ~Indent() {
380  fmt.set_line_prefix(old_line_prefix);
381  }
382 };
383 
403 };
404 
407 
408 
409 
411 // Exceptions
413 
415 class Exception: public Rose::Exception {
416 public:
417  SgAsmInstruction *insn;
418  Exception(const std::string &mesg, SgAsmInstruction *insn): Rose::Exception(mesg), insn(insn) {}
419  void print(std::ostream&) const;
420 };
421 
422 class NotImplemented: public Exception {
423 public:
424  NotImplemented(const std::string &mesg, SgAsmInstruction *insn)
425  : Exception(mesg, insn) {}
426 };
427 
428 
429 
431 // Merging states
433 
436 
454  bool memoryAddressesMayAlias_;
455  bool memoryMergeDebugging_;
456 
457 protected:
458  Merger(): memoryAddressesMayAlias_(true), memoryMergeDebugging_(false) {}
459 
460 public:
462  typedef MergerPtr Ptr;
463 
465  static Ptr instance() {
466  return Ptr(new Merger);
467  }
468 
475  bool memoryAddressesMayAlias() const { return memoryAddressesMayAlias_; }
476  void memoryAddressesMayAlias(bool b) { memoryAddressesMayAlias_ = b; }
486  bool memoryMergeDebugging() const { return memoryMergeDebugging_; }
487  void memoryMergeDebugging(bool b) { memoryMergeDebugging_ = b; }
489 };
490 
492 // Semantic Values
494 
495 // This is leftover for compatibility with an older API. The old API had code like this:
496 // User::SValue user_svalue = BaseSemantics::dynamic_pointer_cast<User::SValue>(base_svalue);
497 // Which can be replaced now with
498 // User::SValue user_svalue = base_svalue.dynamicCast<User::SValue>();
499 template<class To, class From>
500 Sawyer::SharedPointer<To> dynamic_pointer_cast(const Sawyer::SharedPointer<From> &from) {
501  return from.template dynamicCast<To>();
502 }
503 
506 
522 protected:
523  size_t width;
525  // Serialization
527 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
528 private:
529  friend class boost::serialization::access;
530 
531  template<class S>
532  void serialize(S &s, const unsigned /*version*/) {
533  s & BOOST_SERIALIZATION_NVP(width);
534  }
535 #endif
536 
538  // Normal, protected, C++ constructors
539 protected:
540  SValue(): width(0) {} // needed for serialization
541  explicit SValue(size_t nbits): width(nbits) {} // hot
542  SValue(const SValue &other): Sawyer::SharedObject(other), width(other.width) {}
543 
544 public:
546  typedef SValuePtr Ptr;
547 
548 public:
549  virtual ~SValue() {}
550 
552  // Allocating static constructor. None are needed--this class is abstract.
553 
555  // Allocating virtual constructors. undefined_() needs underscores, so we do so consistently for all
556  // these allocating virtual c'tors. However, we use copy() rather than copy_() because this one is fundamentally
557  // different: the object (this) is use for more than just selecting which virtual method to invoke.
558  //
559  // The naming scheme we use here is a bit different than for most other objects for historical reasons. Most other classes
560  // use "create" and "clone" as the virtual constructor names, but SValue uses names ending in undercore, and "copy". The
561  // other difference (at least in this base class) is that we don't define any real constructors or static allocating
562  // constructors (usually named "instance")--it's because this is an abstract class.
563 public:
569  virtual SValuePtr undefined_(size_t nbits) const = 0; // hot
570 
579  virtual SValuePtr unspecified_(size_t nbits) const = 0;
580 
586  virtual SValuePtr bottom_(size_t nBits) const = 0;
587 
591  virtual SValuePtr number_(size_t nbits, uint64_t number) const = 0; // hot
592 
596  virtual SValuePtr boolean_(bool value) const { return number_(1, value?1:0); }
597 
601  virtual SValuePtr copy(size_t new_width=0) const = 0;
602 
630  createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const = 0;
631 
638  SValuePtr createMerged(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const /*final*/ {
639  return createOptionalMerge(other, merger, solver).orElse(copy());
640  }
641 
643  // Dynamic pointer casts. No-ops since this is the base class
644 public:
645  static SValuePtr promote(const SValuePtr &x) {
646  ASSERT_not_null(x);
647  return x;
648  }
649 
651  // The rest of the API...
652 public:
658  virtual bool isBottom() const = 0;
659 
662  virtual bool is_number() const = 0;
663 
666  virtual uint64_t get_number() const = 0;
667 
670  virtual size_t get_width() const { return width; }
671  virtual void set_width(size_t nbits) { width = nbits; }
675  virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
676 
678  virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
679 
682  bool isTrue() const {
683  return is_number() && get_number()!=0;
684  }
685 
688  bool isFalse() const {
689  return is_number() && get_number()==0;
690  }
691 
695  void print(std::ostream &stream) const { Formatter fmt; print(stream, fmt); }
696  virtual void print(std::ostream&, Formatter&) const = 0;
701  SValuePtr obj;
702  Formatter &fmt;
703  public:
704  WithFormatter(const SValuePtr &svalue, Formatter &fmt): obj(svalue), fmt(fmt) {}
705  void print(std::ostream &stream) const { obj->print(stream, fmt); }
706  };
707 
724  virtual std::string get_comment() const { return ""; }
725  virtual void set_comment(const std::string&) const {} // const is intended; cf. doxygen comment
727 };
728 
729 
730 
732 // Register States
734 
736 typedef boost::shared_ptr<class RegisterState> RegisterStatePtr;
737 
741 class RegisterState: public boost::enable_shared_from_this<RegisterState> {
742 private:
743  MergerPtr merger_;
744  SValuePtr protoval_;
746 protected:
749  // Serialization
751 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
752 private:
753  friend class boost::serialization::access;
754 
755  template<class S>
756  void serialize(S &s, const unsigned version) {
757  //s & merger_; -- not saved
758  s & BOOST_SERIALIZATION_NVP(protoval_);
759  if (version >= 1)
760  s & BOOST_SERIALIZATION_NVP(regdict);
761  }
762 #endif
763 
764 
766  // Real constructors
767 protected:
768  RegisterState()
769  : regdict(NULL) {} // for serialization
770 
771  RegisterState(const SValuePtr &protoval, const RegisterDictionary *regdict)
772  : protoval_(protoval), regdict(regdict) {
773  ASSERT_not_null(protoval_);
774  }
775 
776 public:
778  typedef RegisterStatePtr Ptr;
779 
780 public:
781  virtual ~RegisterState() {}
782 
784  // Static allocating constructors. None are needed--this class is abstract.
785 
786 
788  // Virtual constructors.
789 public:
794  virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const = 0;
795 
797  virtual RegisterStatePtr clone() const = 0;
798 
800  // Dynamic pointer casts. No-op since this is the base class.
801 public:
802  static RegisterStatePtr promote(const RegisterStatePtr &x) {
803  ASSERT_not_null(x);
804  return x;
805  }
806 
807 public:
809  // The rest of the API...
810 
819  MergerPtr merger() const { return merger_; }
820  void merger(const MergerPtr &m) { merger_ = m; }
824  SValuePtr protoval() const { return protoval_; }
825 
830  void set_register_dictionary(const RegisterDictionary *rd) { regdict = rd; }
843  virtual void clear() = 0;
844 
846  virtual void zero() = 0;
847 
851  virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops) = 0;
852 
866  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) = 0;
867 
871  virtual SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) = 0;
872 
879  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops) = 0;
880 
883  void print(std::ostream &stream, const std::string prefix="") const {
884  Formatter fmt;
885  fmt.set_line_prefix(prefix);
886  print(stream, fmt);
887  }
888  virtual void print(std::ostream&, Formatter&) const = 0;
893  RegisterStatePtr obj;
894  Formatter &fmt;
895  public:
896  WithFormatter(const RegisterStatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
897  void print(std::ostream &stream) const { obj->print(stream, fmt); }
898  };
899 
907  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
911 };
912 
913 
915 // Memory State
917 
919 typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
920 
924 class MemoryState: public boost::enable_shared_from_this<MemoryState> {
925  SValuePtr addrProtoval_;
926  SValuePtr valProtoval_;
927  ByteOrder::Endianness byteOrder_;
928  MergerPtr merger_;
929  bool byteRestricted_; // are cell values all exactly one byte wide?
930 
932  // Serialization
933 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
934 private:
935  friend class boost::serialization::access;
936 
937  template<class S>
938  void serialize(S &s, const unsigned /*version*/) {
939  s & BOOST_SERIALIZATION_NVP(addrProtoval_);
940  s & BOOST_SERIALIZATION_NVP(valProtoval_);
941  s & BOOST_SERIALIZATION_NVP(byteOrder_);
942  //s & merger_ -- not saved
943  s & BOOST_SERIALIZATION_NVP(byteRestricted_);
944  }
945 #endif
946 
947 
949  // Real constructors
950 protected:
951  MemoryState() // for serialization
952  : byteOrder_(ByteOrder::ORDER_UNSPECIFIED), byteRestricted_(true) {}
953 
954  explicit MemoryState(const SValuePtr &addrProtoval, const SValuePtr &valProtoval)
955  : addrProtoval_(addrProtoval), valProtoval_(valProtoval), byteOrder_(ByteOrder::ORDER_UNSPECIFIED),
956  byteRestricted_(true) {
957  ASSERT_not_null(addrProtoval);
958  ASSERT_not_null(valProtoval);
959  }
960 
961  MemoryState(const MemoryStatePtr &other)
962  : addrProtoval_(other->addrProtoval_), valProtoval_(other->valProtoval_), byteOrder_(ByteOrder::ORDER_UNSPECIFIED),
963  merger_(other->merger_), byteRestricted_(other->byteRestricted_) {}
964 
965 public:
967  typedef MemoryStatePtr Ptr;
968 
969 public:
970  virtual ~MemoryState() {}
971 
973  // Static allocating constructors. None needed since this class is abstract
974 
976  // Virtual constructors
977 public:
984  virtual MemoryStatePtr create(const SValuePtr &addrProtoval, const SValuePtr &valProtoval) const = 0;
985 
987  virtual MemoryStatePtr clone() const = 0;
988 
990  // Dynamic pointer casts. No-op since this is the base class.
991 public:
992  static MemoryStatePtr promote(const MemoryStatePtr &x) {
993  ASSERT_not_null(x);
994  return x;
995  }
996 
998  // Methods first declared at this level of the class hierarchy
999 public:
1008  MergerPtr merger() const { return merger_; }
1009  void merger(const MergerPtr &m) { merger_ = m; }
1014  SValuePtr get_addr_protoval() const { return addrProtoval_; }
1015 
1018  SValuePtr get_val_protoval() const { return valProtoval_; }
1019 
1021  virtual void clear() = 0;
1022 
1030  bool byteRestricted() const { return byteRestricted_; }
1031  void byteRestricted(bool b) { byteRestricted_ = b; }
1036  ByteOrder::Endianness get_byteOrder() const { return byteOrder_; }
1037  void set_byteOrder(ByteOrder::Endianness bo) { byteOrder_ = bo; }
1043  virtual bool merge(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps) = 0;
1044 
1064  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
1065  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1066 
1071  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
1072  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1073 
1083  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value,
1084  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1085 
1088  void print(std::ostream &stream, const std::string prefix="") const {
1089  Formatter fmt;
1090  fmt.set_line_prefix(prefix);
1091  print(stream, fmt);
1092  }
1093  virtual void print(std::ostream&, Formatter&) const = 0;
1098  MemoryStatePtr obj;
1099  Formatter &fmt;
1100  public:
1101  WithFormatter(const MemoryStatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1102  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1103  };
1104 
1112  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1115 };
1116 
1117 
1118 
1120 // State
1122 
1124 typedef boost::shared_ptr<class State> StatePtr;
1125 
1142 class State: public boost::enable_shared_from_this<State> {
1143  SValuePtr protoval_; // Initial value used to create additional values as needed.
1144  RegisterStatePtr registers_; // All machine register values for this semantic state.
1145  MemoryStatePtr memory_; // All memory for this semantic state.
1146 
1148  // Serialization
1149 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
1150 private:
1151  friend class boost::serialization::access;
1152 
1153  template<class S>
1154  void serialize(S &s, const unsigned /*version*/) {
1155  s & BOOST_SERIALIZATION_NVP(protoval_);
1156  s & BOOST_SERIALIZATION_NVP(registers_);
1157  s & BOOST_SERIALIZATION_NVP(memory_);
1158  }
1159 #endif
1160 
1161 
1163  // Real constructors
1164 protected:
1165  // needed for serialization
1166  State() {}
1167 
1168  State(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
1169  : registers_(registers), memory_(memory) {
1170  ASSERT_not_null(registers);
1171  ASSERT_not_null(memory);
1172  protoval_ = registers->protoval();
1173  ASSERT_not_null(protoval_);
1174  }
1175 
1176  // deep-copy the registers and memory
1177  State(const State &other)
1178  : boost::enable_shared_from_this<State>(other), protoval_(other.protoval_) {
1179  registers_ = other.registers_->clone();
1180  memory_ = other.memory_->clone();
1181  }
1182 
1183 public:
1185  typedef StatePtr Ptr;
1186 
1187 public:
1188  virtual ~State() {}
1189 
1191  // Static allocating constructors
1192 public:
1194  static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory) {
1195  return StatePtr(new State(registers, memory));
1196  }
1197 
1199  static StatePtr instance(const StatePtr &other) {
1200  return StatePtr(new State(*other));
1201  }
1202 
1204  // Virtual constructors
1205 public:
1207  virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const {
1208  return instance(registers, memory);
1209  }
1210 
1214  virtual StatePtr clone() const {
1215  StatePtr self = boost::const_pointer_cast<State>(shared_from_this());
1216  return instance(self);
1217  }
1218 
1220  // Dynamic pointer casts. No-op since this is the base class.
1221 public:
1222  static StatePtr promote(const StatePtr &x) {
1223  ASSERT_not_null(x);
1224  return x;
1225  }
1226 
1228  // Other methods that are part of our API. Most of these just chain to either the register state and/or the memory state.
1229 public:
1231  SValuePtr protoval() const { return protoval_; }
1232 
1234  virtual void clear();
1235 
1239  virtual void zero_registers();
1240 
1244  virtual void clear_memory();
1245 
1249  RegisterStatePtr registerState() const {
1250  return registers_;
1251  }
1252 
1256  MemoryStatePtr memoryState() const {
1257  return memory_;
1258  }
1259 
1264  virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
1265 
1270  virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
1271 
1276  virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
1277 
1282  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
1283  RiscOperators *addrOps, RiscOperators *valOps);
1284 
1289  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
1290  RiscOperators *addrOps, RiscOperators *valOps);
1291 
1296  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
1297 
1303  void printRegisters(std::ostream &stream, const std::string &prefix = "");
1304  virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
1312  void printMemory(std::ostream &stream, const std::string &prefix = "") const;
1313  virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
1318  void print(std::ostream &stream, const std::string &prefix = "") const;
1319  virtual void print(std::ostream&, Formatter&) const;
1324  StatePtr obj;
1325  Formatter &fmt;
1326  public:
1327  WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1328  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1329  };
1330 
1338  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1347  virtual bool merge(const StatePtr &other, RiscOperators *ops);
1348 };
1349 
1350 
1351 
1353 // RISC Operators
1355 
1357 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
1358 
1379 class RiscOperators: public boost::enable_shared_from_this<RiscOperators> {
1380  SValuePtr protoval_; // Prototypical value used for its virtual constructors
1381  StatePtr currentState_; // State upon which RISC operators operate
1382  StatePtr initialState_; // Lazily updated initial state; see readMemory
1383  SmtSolverPtr solver_; // Optional SMT solver
1384  SgAsmInstruction *currentInsn_; // Current instruction, as set by latest startInstruction call
1385  size_t nInsns_; // Number of instructions processed
1386  std::string name_; // Name to use for debugging
1387 
1389  // Serialization
1390 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
1391 private:
1392  friend class boost::serialization::access;
1393 
1394  template<class S>
1395  void serialize(S &s, const unsigned /*version*/) {
1396  s & BOOST_SERIALIZATION_NVP(protoval_);
1397  s & BOOST_SERIALIZATION_NVP(currentState_);
1398  s & BOOST_SERIALIZATION_NVP(initialState_);
1399  s & BOOST_SERIALIZATION_NVP(solver_);
1400  s & BOOST_SERIALIZATION_NVP(currentInsn_);
1401  s & BOOST_SERIALIZATION_NVP(nInsns_);
1402  s & BOOST_SERIALIZATION_NVP(name_);
1403  }
1404 #endif
1405 
1407  // Real constructors
1408 protected:
1409  // for serialization
1410  RiscOperators()
1411  : currentInsn_(NULL), nInsns_(0) {}
1412 
1413  explicit RiscOperators(const SValuePtr &protoval, const SmtSolverPtr &solver = SmtSolverPtr())
1414  : protoval_(protoval), solver_(solver), currentInsn_(NULL), nInsns_(0) {
1415  ASSERT_not_null(protoval_);
1416  }
1417 
1418  explicit RiscOperators(const StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr())
1419  : currentState_(state), solver_(solver), currentInsn_(NULL), nInsns_(0) {
1420  ASSERT_not_null(state);
1421  protoval_ = state->protoval();
1422  }
1423 
1424 public:
1426  typedef RiscOperatorsPtr Ptr;
1427 
1428 public:
1429  virtual ~RiscOperators() {}
1430 
1432  // Static allocating constructors. None needed since this class is abstract.
1433 
1434 
1436  // Virtual constructors.
1437 public:
1441  virtual RiscOperatorsPtr create(const SValuePtr &protoval, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
1442 
1447  virtual RiscOperatorsPtr create(const StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
1448 
1450  // Dynamic pointer casts. No-op since this is the base class.
1451 public:
1452  static RiscOperatorsPtr promote(const RiscOperatorsPtr &x) {
1453  ASSERT_not_null(x);
1454  return x;
1455  }
1456 
1458  // Other methods part of our API
1459 public:
1463  virtual SValuePtr protoval() const { return protoval_; }
1464 
1473  virtual SmtSolverPtr solver() const { return solver_; }
1474  virtual void solver(const SmtSolverPtr &s) { solver_ = s; }
1488  virtual StatePtr currentState() const { return currentState_; }
1489  virtual void currentState(const StatePtr &s) { currentState_ = s; }
1529  virtual StatePtr initialState() const { return initialState_; }
1530  virtual void initialState(const StatePtr &s) { initialState_ = s; }
1538  virtual const std::string& name() const { return name_; }
1539  virtual void name(const std::string &s) { name_ = s; }
1544  void print(std::ostream &stream, const std::string prefix="") const {
1545  Formatter fmt;
1546  fmt.set_line_prefix(prefix);
1547  print(stream, fmt);
1548  }
1549  virtual void print(std::ostream &stream, Formatter &fmt) const {
1550  currentState_->print(stream, fmt);
1551  }
1556  RiscOperatorsPtr obj;
1557  Formatter &fmt;
1558  public:
1559  WithFormatter(const RiscOperatorsPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1560  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1561  };
1562 
1570  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1579  virtual size_t nInsns() const { return nInsns_; }
1580  virtual void nInsns(size_t n) { nInsns_ = n; }
1588  return currentInsn_;
1589  }
1590 
1593  virtual void startInstruction(SgAsmInstruction *insn);
1594 
1597  virtual void finishInstruction(SgAsmInstruction *insn) {
1598  ASSERT_not_null(insn);
1599  ASSERT_require(currentInsn_==insn);
1600  currentInsn_ = NULL;
1601  };
1602 
1603 
1605  // Value Construction Operations
1607  // The trailing underscores are necessary for for undefined_() on some machines, so we just add one to the end of all the
1608  // virtual constructors for consistency.
1609 
1611  virtual SValuePtr undefined_(size_t nbits) {
1612  return protoval_->undefined_(nbits);
1613  }
1614  virtual SValuePtr unspecified_(size_t nbits) {
1615  return protoval_->unspecified_(nbits);
1616  }
1617 
1619  virtual SValuePtr number_(size_t nbits, uint64_t value) {
1620  return protoval_->number_(nbits, value);
1621  }
1622 
1624  virtual SValuePtr boolean_(bool value) {
1625  return protoval_->boolean_(value);
1626  }
1627 
1629  virtual SValuePtr bottom_(size_t nbits) {
1630  return protoval_->bottom_(nbits);
1631  }
1632 
1633 
1635  // x86-specific Operations (FIXME)
1637 
1640  virtual SValuePtr filterCallTarget(const SValuePtr &a) {
1641  return a->copy();
1642  }
1643 
1647  virtual SValuePtr filterReturnTarget(const SValuePtr &a) {
1648  return a->copy();
1649  }
1650 
1654  virtual SValuePtr filterIndirectJumpTarget(const SValuePtr &a) {
1655  return a->copy();
1656  }
1657 
1659  virtual void hlt() {}
1660 
1662  virtual void cpuid() {}
1663 
1665  virtual SValuePtr rdtsc() { return unspecified_(64); }
1666 
1667 
1669  // Boolean Operations
1671 
1674  virtual SValuePtr and_(const SValuePtr &a, const SValuePtr &b) = 0;
1675 
1678  virtual SValuePtr or_(const SValuePtr &a, const SValuePtr &b) = 0;
1679 
1682  virtual SValuePtr xor_(const SValuePtr &a, const SValuePtr &b) = 0;
1683 
1685  virtual SValuePtr invert(const SValuePtr &a) = 0;
1686 
1690  virtual SValuePtr extract(const SValuePtr &a, size_t begin_bit, size_t end_bit) = 0;
1691 
1695  virtual SValuePtr concat(const SValuePtr &a, const SValuePtr &b) = 0;
1696 
1699  virtual SValuePtr leastSignificantSetBit(const SValuePtr &a) = 0;
1700 
1703  virtual SValuePtr mostSignificantSetBit(const SValuePtr &a) = 0;
1704 
1708  virtual SValuePtr rotateLeft(const SValuePtr &a, const SValuePtr &nbits) = 0;
1709 
1713  virtual SValuePtr rotateRight(const SValuePtr &a, const SValuePtr &nbits) = 0;
1714 
1718  virtual SValuePtr shiftLeft(const SValuePtr &a, const SValuePtr &nbits) = 0;
1719 
1723  virtual SValuePtr shiftRight(const SValuePtr &a, const SValuePtr &nbits) = 0;
1724 
1729  virtual SValuePtr shiftRightArithmetic(const SValuePtr &a, const SValuePtr &nbits) = 0;
1730 
1731 
1733  // Comparison Operations
1735 
1738  virtual SValuePtr equalToZero(const SValuePtr &a) = 0;
1739 
1743  virtual SValuePtr ite(const SValuePtr &cond, const SValuePtr &a, const SValuePtr &b) = 0;
1744 
1751  virtual SValuePtr isEqual(const SValuePtr &a, const SValuePtr &b);
1752  virtual SValuePtr isNotEqual(const SValuePtr &a, const SValuePtr &b);
1762  virtual SValuePtr isUnsignedLessThan(const SValuePtr &a, const SValuePtr &b);
1763  virtual SValuePtr isUnsignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1764  virtual SValuePtr isUnsignedGreaterThan(const SValuePtr &a, const SValuePtr &b);
1765  virtual SValuePtr isUnsignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1775  virtual SValuePtr isSignedLessThan(const SValuePtr &a, const SValuePtr &b);
1776  virtual SValuePtr isSignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1777  virtual SValuePtr isSignedGreaterThan(const SValuePtr &a, const SValuePtr &b);
1778  virtual SValuePtr isSignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1781  // Integer Arithmetic Operations
1784 
1787  virtual SValuePtr unsignedExtend(const SValuePtr &a, size_t new_width) {
1788  return a->copy(new_width);
1789  }
1790 
1793  virtual SValuePtr signExtend(const SValuePtr &a, size_t new_width) = 0;
1794 
1797  virtual SValuePtr add(const SValuePtr &a, const SValuePtr &b) = 0;
1798 
1801  virtual SValuePtr subtract(const SValuePtr &minuend, const SValuePtr &subtrahend);
1802 
1819  virtual SValuePtr addWithCarries(const SValuePtr &a, const SValuePtr &b, const SValuePtr &c,
1820  SValuePtr &carry_out/*output*/) = 0;
1821 
1823  virtual SValuePtr negate(const SValuePtr &a) = 0;
1824 
1826  virtual SValuePtr signedDivide(const SValuePtr &a, const SValuePtr &b) = 0;
1827 
1829  virtual SValuePtr signedModulo(const SValuePtr &a, const SValuePtr &b) = 0;
1830 
1832  virtual SValuePtr signedMultiply(const SValuePtr &a, const SValuePtr &b) = 0;
1833 
1835  virtual SValuePtr unsignedDivide(const SValuePtr &a, const SValuePtr &b) = 0;
1836 
1838  virtual SValuePtr unsignedModulo(const SValuePtr &a, const SValuePtr &b) = 0;
1839 
1841  virtual SValuePtr unsignedMultiply(const SValuePtr &a, const SValuePtr &b) = 0;
1842 
1843 
1845  // Interrupt and system calls
1847 
1852  virtual void interrupt(int /*majr*/, int /*minr*/) {}
1853 
1854 
1856  // Floating-point operations
1857  //
1858  // For now these all have default implementations that throw NotImplemented, but we might change them to pure virtual
1859  // sometime in the future so they're consistent with most other RISC operators. [Robb P. Matzke 2015-08-03]
1861 
1863  virtual SValuePtr fpFromInteger(const SValuePtr &intValue, SgAsmFloatType *fpType);
1864 
1870  virtual SValuePtr fpToInteger(const SValuePtr &fpValue, SgAsmFloatType *fpType, const SValuePtr &dflt);
1871 
1875  virtual SValuePtr fpConvert(const SValuePtr &a, SgAsmFloatType *aType, SgAsmFloatType *retType);
1876 
1878  virtual SValuePtr fpIsNan(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1879 
1881  virtual SValuePtr fpIsDenormalized(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1882 
1884  virtual SValuePtr fpIsZero(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1885 
1890  virtual SValuePtr fpIsInfinity(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1891 
1895  virtual SValuePtr fpSign(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1896 
1902  virtual SValuePtr fpEffectiveExponent(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1903 
1907  virtual SValuePtr fpAdd(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1908 
1913  virtual SValuePtr fpSubtract(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1914 
1918  virtual SValuePtr fpMultiply(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1919 
1923  virtual SValuePtr fpDivide(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1924 
1928  virtual SValuePtr fpSquareRoot(const SValuePtr &a, SgAsmFloatType *fpType);
1929 
1933  virtual SValuePtr fpRoundTowardZero(const SValuePtr &a, SgAsmFloatType *fpType);
1934 
1935 
1937  // State Accessing Operations
1939 
1964  virtual SValuePtr readRegister(RegisterDescriptor reg) { // old subclasses can still override this if they want,
1965  return readRegister(reg, undefined_(reg.get_nbits())); // but new subclasses should not override this method.
1966  }
1967  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt); // new subclasses override this
1979  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &a) {
1980  ASSERT_not_null(currentState_);
1981  currentState_->writeRegister(reg, a, this);
1982  }
1983 
1991  virtual SValuePtr peekRegister(RegisterDescriptor, const SValuePtr &dflt);
1993  return peekRegister(reg, undefined_(reg.get_nbits()));
1994  }
2019  virtual SValuePtr readMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt,
2020  const SValuePtr &cond) = 0;
2021 
2032  virtual void writeMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &data,
2033  const SValuePtr &cond) = 0;
2034 
2039  virtual SValuePtr peekMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt) = 0;
2040 };
2041 
2042 
2043 
2045 // Instruction Dispatcher
2047 
2049 typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
2050 
2053 public:
2054  virtual ~InsnProcessor() {}
2055  virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
2056 };
2057 
2071 class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
2072 protected:
2073  RiscOperatorsPtr operators;
2075  size_t addrWidth_;
2078  // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
2079  // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iproc_key() virtual method.
2080  typedef std::vector<InsnProcessor*> InsnProcessors;
2081  InsnProcessors iproc_table;
2082 
2083 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
2084 private:
2085  friend class boost::serialization::access;
2086 
2087  template<class S>
2088  void serialize(S &s, const unsigned /*version*/) {
2089  s & BOOST_SERIALIZATION_NVP(operators);
2090  s & BOOST_SERIALIZATION_NVP(regdict);
2091  s & BOOST_SERIALIZATION_NVP(addrWidth_);
2092  s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
2093  //s & iproc_table; -- not saved
2094  }
2095 #endif
2096 
2098  // Real constructors
2099 protected:
2100  // Prototypical constructor
2101  Dispatcher(): regdict(NULL), addrWidth_(0), autoResetInstructionPointer_(true) {}
2102 
2103  // Prototypical constructor
2104  Dispatcher(size_t addrWidth, const RegisterDictionary *regs)
2105  : regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {}
2106 
2107  Dispatcher(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionary *regs)
2108  : operators(ops), regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {
2109  ASSERT_not_null(operators);
2110  ASSERT_not_null(regs);
2111  }
2112 
2113 public:
2115  typedef DispatcherPtr Ptr;
2116 
2117 public:
2118  virtual ~Dispatcher() {
2119  for (InsnProcessors::iterator iter=iproc_table.begin(); iter!=iproc_table.end(); ++iter)
2120  delete *iter;
2121  }
2122 
2124  // Static allocating constructors. None since this is an abstract class
2125 
2126 
2128  // Virtual constructors
2129 public:
2131  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const = 0;
2132 
2134  // Methods to process instructions
2135 public:
2137  virtual void processInstruction(SgAsmInstruction *insn);
2138 
2140  // Instruction processor table operations
2141 public:
2146 
2150  virtual void iproc_replace(SgAsmInstruction *insn, InsnProcessor *iproc);
2151 
2153  virtual int iproc_key(SgAsmInstruction*) const = 0;
2154 
2158  virtual void iproc_set(int key, InsnProcessor *iproc);
2159 
2161  virtual InsnProcessor *iproc_get(int key);
2162 
2164  // Convenience methods that defer the call to some member object
2165 public:
2167  virtual RiscOperatorsPtr get_operators() const { return operators; }
2168 
2171  virtual StatePtr currentState() const { return operators ? operators->currentState() : StatePtr(); }
2172 
2174  virtual SValuePtr protoval() const { return operators ? operators->protoval() : SValuePtr(); }
2175 
2181  return operators ? operators->currentInstruction() : NULL;
2182  }
2183 
2185  virtual SValuePtr undefined_(size_t nbits) const {
2186  ASSERT_not_null(operators);
2187  return operators->undefined_(nbits);
2188  }
2189  virtual SValuePtr unspecified_(size_t nbits) const {
2190  ASSERT_not_null(operators);
2191  return operators->unspecified_(nbits);
2192  }
2193 
2195  virtual SValuePtr number_(size_t nbits, uint64_t number) const {
2196  ASSERT_not_null(operators);
2197  return operators->number_(nbits, number);
2198  }
2199 
2201  // Methods related to registers
2202 public:
2215  return regdict;
2216  }
2217  virtual void set_register_dictionary(const RegisterDictionary *regdict) {
2218  this->regdict = regdict;
2219  }
2226  virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
2227 
2234  size_t addressWidth() const { return addrWidth_; }
2235  void addressWidth(size_t nbits);
2239  virtual RegisterDescriptor instructionPointerRegister() const = 0;
2240 
2242  virtual RegisterDescriptor stackPointerRegister() const = 0;
2243 
2251  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
2255  // Miscellaneous methods that tend to be the same for most dispatchers
2257 public:
2258 
2268 
2273 
2277  virtual void incrementRegisters(SgAsmExpression*);
2278 
2282  virtual void decrementRegisters(SgAsmExpression*);
2283 
2287  virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
2288 
2294  virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
2295 
2299  virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
2300 };
2301 
2302 
2303 
2305 // Printing
2307 
2308 std::ostream& operator<<(std::ostream&, const Exception&);
2309 std::ostream& operator<<(std::ostream&, const SValue&);
2310 std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
2311 std::ostream& operator<<(std::ostream&, const MemoryState&);
2312 std::ostream& operator<<(std::ostream&, const MemoryState::WithFormatter&);
2313 std::ostream& operator<<(std::ostream&, const RegisterState&);
2314 std::ostream& operator<<(std::ostream&, const RegisterState::WithFormatter&);
2315 std::ostream& operator<<(std::ostream&, const State&);
2316 std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
2317 std::ostream& operator<<(std::ostream&, const RiscOperators&);
2318 std::ostream& operator<<(std::ostream&, const RiscOperators::WithFormatter&);
2319 
2320 } // namespace
2321 } // namespace
2322 } // namespace
2323 } // namespace
2324 
2325 // Class versions must be at global scope
2327 
2328 #endif
void autoResetInstructionPointer(bool b)
Property: Reset instruction pointer register for each instruction.
bool autoResetInstructionPointer() const
Property: Reset instruction pointer register for each instruction.
MergerPtr Ptr
Shared ownership pointer for Merger.
virtual SValuePtr signedMultiply(const SValuePtr &a, const SValuePtr &b)=0
Multiplies two signed values.
void clear_show_latest_writers()
Whether to show latest writer information for register and memory states.
The location was read without having the IO_WRITE or IO_INIT property.
SharedObject()
Default constructor.
Definition: SharedObject.h:70
virtual SValuePtr fpIsDenormalized(const SValuePtr &fpValue, SgAsmFloatType *fpType)
Whether a floating-point value is denormalized.
virtual SgAsmInstruction * currentInstruction() const
Returns current instruction.
virtual SValuePtr rotateRight(const SValuePtr &a, const SValuePtr &nbits)=0
Rotate bits to the right.
The location was written on behalf of an instruction.
virtual SValuePtr filterIndirectJumpTarget(const SValuePtr &a)
Invoked to filter indirect jumps.
virtual SValuePtr isEqual(const SValuePtr &a, const SValuePtr &b)
Equality comparison.
virtual void interrupt(int, int)
Invoked for instructions that cause an interrupt.
void set_line_prefix(const std::string &s)
The string to print at the start of each line.
virtual RegisterDescriptor segmentRegister(SgAsmMemoryReferenceExpression *)
Returns a register descriptor for the segment part of a memory reference expression.
MemoryStatePtr memoryState() const
Property: Memory state.
virtual SValuePtr fpMultiply(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType)
Multiply two floating-point values.
virtual SValuePtr undefined_(size_t nbits)
Returns a new undefined value.
virtual std::string get_comment() const
Some subclasses support the ability to add comments to values.
WithFormatter operator+(Formatter &fmt)
Used for printing memory states with formatting.
virtual void set_width(size_t nbits)
Accessor for value width.
WithFormatter with_format(Formatter &fmt)
Used for printing values with formatting.
void printMemory(std::ostream &stream, const std::string &prefix="") const
Print memory contents.
void clear_suppress_initial_values()
Whether register initial values should be suppressed.
virtual SValuePtr isSignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b)
Comparison for signed values.
virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Returns true if two values must be equal.
virtual SValuePtr isSignedLessThan(const SValuePtr &a, const SValuePtr &b)
Comparison for signed values.
virtual SValuePtr leastSignificantSetBit(const SValuePtr &a)=0
Returns position of least significant set bit; zero when no bits are set.
virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)=0
Read a value from memory without side effects.
bool isFalse() const
Returns true if concrete zero.
virtual void iproc_replace(SgAsmInstruction *insn, InsnProcessor *iproc)
Replace an instruction processor with another.
virtual void writeMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &data, const SValuePtr &cond)=0
Writes a value to memory.
virtual RegisterStatePtr clone() const =0
Make a copy of this register state.
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1579
const RegisterDictionary * get_register_dictionary() const
The register dictionary should be compatible with the register dictionary used for other parts of bin...
void print(std::ostream &stream, const std::string prefix="") const
Print a memory state to more than one line of output.
WithFormatter operator+(Formatter &fmt)
Used for printing RISC operators with formatting.
virtual SValuePtr undefined_(size_t nbits) const
Return a new undefined semantic value.
virtual void finishInstruction(SgAsmInstruction *insn)
Called at the end of every instruction.
RegisterStatePtr registerState() const
Property: Register state.
virtual void clear()=0
Removes stored values from the register state.
virtual SValuePtr boolean_(bool value) const
Create a new, Boolean value.
bool get_suppress_initial_values() const
Whether register initial values should be suppressed.
virtual void incrementRegisters(SgAsmExpression *)
Increment all auto-increment registers in the expression.
RegisterStatePtr Ptr
Shared-ownership pointer for a RegisterState object.
virtual SValuePtr fpFromInteger(const SValuePtr &intValue, SgAsmFloatType *fpType)
Construct a floating-point value from an integer value.
virtual SValuePtr fpRoundTowardZero(const SValuePtr &a, SgAsmFloatType *fpType)
Round toward zero.
virtual RegisterDescriptor instructionPointerRegister() const =0
Returns the instruction pointer register.
SValuePtr createMerged(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const
Create a new value by merging two existing values.
virtual StatePtr currentState() const
Get a pointer to the state object.
virtual void write(SgAsmExpression *, const SValuePtr &value, size_t addr_nbits=0)
Writes to an L-value expression.
std::string get_indentation_suffix() const
Indentation string appended to the line prefix for multi-level, multi-line outputs.
virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops)=0
Merge register states for data flow analysis.
virtual SValuePtr rotateLeft(const SValuePtr &a, const SValuePtr &nbits)=0
Rotate bits to the left.
SValuePtr Ptr
Shared-ownership pointer for an SValue object.
virtual SValuePtr extract(const SValuePtr &a, size_t begin_bit, size_t end_bit)=0
Extracts bits from a value.
WithFormatter operator+(Formatter &fmt)
Used for printing values with formatting.
MemoryStatePtr Ptr
Shared-ownership pointer for a MemoryState.
virtual void advanceInstructionPointer(SgAsmInstruction *)
Update the instruction pointer register.
Holds a value or nothing.
Definition: Optional.h:49
ByteOrder::Endianness get_byteOrder() const
Memory byte order.
virtual SValuePtr protoval() const
Return the prototypical value.
virtual SValuePtr fpDivide(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType)
Divide one floating-point value by another.
Small object support.
Definition: SmallObject.h:19
virtual MemoryStatePtr create(const SValuePtr &addrProtoval, const SValuePtr &valProtoval) const =0
Virtual allocating constructor.
virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read register without side effects.
virtual void cpuid()
Invoked for the x86 CPUID instruction.
virtual SValuePtr fpSign(const SValuePtr &fpValue, SgAsmFloatType *fpType)
Sign of floating-point value.
void initDiagnostics()
Initialize diagnostics for instruction semantics.
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops)=0
Write a value to a register.
virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps)
Write a value to memory.
Main namespace for the ROSE library.
void print(std::ostream &stream, const std::string prefix="") const
Print the register contents.
virtual SValuePtr unspecified_(size_t nbits) const =0
Create a new unspecified semantic value.
virtual SValuePtr fpToInteger(const SValuePtr &fpValue, SgAsmFloatType *fpType, const SValuePtr &dflt)
Construct an integer value from a floating-point value.
virtual SValuePtr unsignedMultiply(const SValuePtr &a, const SValuePtr &b)=0
Multiply two unsigned values.
void byteRestricted(bool b)
Indicates whether memory cell values are required to be eight bits wide.
virtual SValuePtr addWithCarries(const SValuePtr &a, const SValuePtr &b, const SValuePtr &c, SValuePtr &carry_out)=0
Add two values of equal size and a carry bit.
void set_suppress_initial_values(bool b=true)
Whether register initial values should be suppressed.
bool memoryMergeDebugging() const
Turn on output for memory merge debugging.
virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const
Virtual constructor.
virtual SValuePtr peekMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt)=0
Read memory without side effects.
void set_show_latest_writers(bool b=true)
Whether to show latest writer information for register and memory states.
virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops)=0
Read a value from a register.
virtual void solver(const SmtSolverPtr &s)
Property: Satisfiability module theory (SMT) solver.
virtual const RegisterDictionary * get_register_dictionary() const
Access the register dictionary.
WithFormatter operator+(Formatter &fmt)
Used for printing states with formatting.
InputOutputProperty
Boolean properties related to I/O.
virtual InsnProcessor * iproc_lookup(SgAsmInstruction *insn)
Lookup the processor for an instruction.
virtual SValuePtr readMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt, const SValuePtr &cond)=0
Reads a value from memory.
virtual SValuePtr or_(const SValuePtr &a, const SValuePtr &b)=0
Computes bit-wise OR of two values.
virtual SValuePtr boolean_(bool value)
Returns a Boolean value.
virtual size_t nInsns() const
Property: Number of instructions processed.
Name space for the entire library.
virtual SValuePtr shiftRight(const SValuePtr &a, const SValuePtr &nbits)=0
Returns arg shifted right logically (no sign bit).
virtual StatePtr currentState() const
Property: Current semantic state.
Sawyer::SharedPointer< class SmtSolver > SmtSolverPtr
Reference-counting pointer for SMT solvers.
virtual SValuePtr add(const SValuePtr &a, const SValuePtr &b)=0
Adds two integers of equal size.
virtual SValuePtr isSignedGreaterThan(const SValuePtr &a, const SValuePtr &b)
Comparison for signed values.
virtual uint64_t get_number() const =0
Return the concrete number for this value.
virtual SValuePtr ite(const SValuePtr &cond, const SValuePtr &a, const SValuePtr &b)=0
If-then-else.
SValuePtr get_addr_protoval() const
Return the address protoval.
virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read from memory without side effects.
boost::shared_ptr< class RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
virtual SValuePtr equalToZero(const SValuePtr &a)=0
Determines whether a value is equal to zero.
virtual SValuePtr isUnsignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b)
Comparison for unsigned values.
bool get_show_latest_writers() const
Whether to show latest writer information for register and memory states.
boost::shared_ptr< class State > StatePtr
Shared-ownership pointer to a semantic state.
RiscOperatorsPtr Ptr
Shared-ownership pointer for a RiscOperators object.
virtual int iproc_key(SgAsmInstruction *) const =0
Given an instruction, return the InsnProcessor key that can be used as an index into the iproc_table...
virtual void hlt()
Invoked for the x86 HLT instruction.
virtual void clear_memory()
Clear all memory locations.
const RegisterDictionary * get_register_dictionary() const
The register dictionary which is used for printing register names.
virtual void zero_registers()
Initialize all registers to zero.
Reference to memory locations.
boost::shared_ptr< class Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
static StatePtr instance(const StatePtr &other)
Instantiate a new copy of an existing state.
virtual SValuePtr shiftRightArithmetic(const SValuePtr &a, const SValuePtr &nbits)=0
Returns arg shifted right arithmetically (with sign bit).
virtual bool merge(const StatePtr &other, RiscOperators *ops)
Merge operation for data flow analysis.
virtual SValuePtr xor_(const SValuePtr &a, const SValuePtr &b)=0
Computes bit-wise XOR of two values.
virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)=0
Read a value from memory.
Base classes for instruction semantics.
virtual SValuePtr subtract(const SValuePtr &minuend, const SValuePtr &subtrahend)
Subtract one value from another.
virtual SgAsmInstruction * currentInstruction() const
Returns the instruction that is being processed.
virtual SValuePtr fpIsInfinity(const SValuePtr &fpValue, SgAsmFloatType *fpType)
Whether a floating-point value is infinity.
virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const =0
Virtual constructor.
virtual SValuePtr filterReturnTarget(const SValuePtr &a)
Invoked to filter return targets.
virtual void processInstruction(SgAsmInstruction *insn)
Process a single instruction.
Describes (part of) a physical CPU register.
WithFormatter operator+(Formatter &fmt)
Used for printing register states with formatting.
size_t addressWidth() const
Property: Width of memory addresses.
virtual void nInsns(size_t n)
Property: Number of instructions processed.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
virtual RiscOperatorsPtr get_operators() const
Get a pointer to the RISC operators object.
Creates SharedPointer from this.
virtual SValuePtr fpIsZero(const SValuePtr &fpValue, SgAsmFloatType *fpType)
Whether a floating-point value is equal to zero.
WithFormatter with_format(Formatter &fmt)
Used for printing register states with formatting.
virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Returns true if two values could be equal.
void clear_show_properties()
Whether to show register properties.
WithFormatter with_format(Formatter &fmt)
Used for printing states with formatting.
virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const =0
Virtual constructor.
virtual SValuePtr invert(const SValuePtr &a)=0
One's complement.
virtual SValuePtr fpSquareRoot(const SValuePtr &a, SgAsmFloatType *fpType)
Square root.
virtual SValuePtr unsignedExtend(const SValuePtr &a, size_t new_width)
Extend (or shrink) operand a so it is nbits wide by adding or removing high-order bits...
virtual SValuePtr filterCallTarget(const SValuePtr &a)
Invoked to filter call targets.
Sawyer::Message::Facility mlog
Diagnostics logging facility for instruction semantics.
Sawyer::SharedPointer< class SValue > SValuePtr
Shared-ownership pointer to a semantic value in any domain.
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.
virtual SValuePtr isUnsignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b)
Comparison for unsigned values.
virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt, RiscOperators *addrOps, RiscOperators *valOps)
Read a value from memory.
Functor that knows how to dispatch a single kind of instruction.
virtual bool merge(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps)=0
Merge memory states for data flow analysis.
bool memoryAddressesMayAlias() const
Whether memory addresses can alias one another.
virtual SValuePtr bottom_(size_t nBits) const =0
Data-flow bottom value.
virtual SValuePtr fpAdd(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType)
Add two floating-point values.
virtual void currentState(const StatePtr &s)
Property: Current semantic state.
DispatcherPtr Ptr
Shared-ownership pointer for a Dispatcher object.
void print(std::ostream &stream, const std::string prefix="") const
Print multi-line output for this object.
The location was read without having the IO_WRITE property.
The location was written without an instruction.
virtual bool isBottom() const =0
Determines whether a value is a data-flow bottom.
virtual void decrementRegisters(SgAsmExpression *)
Decrement all auto-decrement registers in the expression.
virtual StatePtr initialState() const
Property: Optional lazily updated initial state.
virtual SValuePtr effectiveAddress(SgAsmExpression *, size_t nbits=0)
Returns a memory address by evaluating the address expression.
virtual SValuePtr fpIsNan(const SValuePtr &fpValue, SgAsmFloatType *fpType)
Whether a floating-point value is a special not-a-number bit pattern.
Base class for most instruction semantics RISC operators.
const RegisterDictionary * regdict
See set_register_dictionary().
Base class for expressions.
virtual SValuePtr rdtsc()
Invoked for the x86 RDTSC instruction.
virtual SValuePtr unsignedModulo(const SValuePtr &a, const SValuePtr &b)=0
Calculates modulo with unsigned values.
SValuePtr peekRegister(RegisterDescriptor reg)
Obtain a register value without side effects.
const RegisterDictionary * regdict
Registers that are able to be stored by this state.
virtual void initialState(const StatePtr &s)
Property: Optional lazily updated initial state.
virtual SValuePtr number_(size_t nbits, uint64_t number) const =0
Create a new concrete semantic value.
void memoryAddressesMayAlias(bool b)
Whether memory addresses can alias one another.
virtual SValuePtr fpConvert(const SValuePtr &a, SgAsmFloatType *aType, SgAsmFloatType *retType)
Convert from one floating-point type to another.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
virtual SValuePtr concat(const SValuePtr &a, const SValuePtr &b)=0
Concatenates the bits of two values.
Sawyer::SharedPointer< class Merger > MergerPtr
Shared-ownership pointer for Merger classes.
void set_show_properties(bool b=true)
Whether to show register properties.
Base class for reference counted objects.
Definition: SharedObject.h:64
void set_register_dictionary(const RegisterDictionary *rd)
The register dictionary should be compatible with the register dictionary used for other parts of bin...
virtual StatePtr clone() const
Virtual copy constructor.
boost::shared_ptr< class MemoryState > MemoryStatePtr
Shared-ownership pointer to a memory state.
std::string get_line_prefix() const
The string to print at the start of each line.
virtual SValuePtr fpEffectiveExponent(const SValuePtr &fpValue, SgAsmFloatType *fpType)
Exponent of floating-point value.
WithFormatter with_format(Formatter &fmt)
Used for printing memory states with formatting.
void print(std::ostream &stream) const
Print a value to a stream using default format.
virtual SValuePtr isUnsignedLessThan(const SValuePtr &a, const SValuePtr &b)
Comparison for unsigned values.
virtual SValuePtr signExtend(const SValuePtr &a, size_t new_width)=0
Sign extends a value.
virtual void name(const std::string &s)
Property: Name used for debugging.
virtual size_t get_width() const
Accessor for value width.
virtual MemoryStatePtr clone() const =0
Virtual allocating copy constructor.
virtual void startInstruction(SgAsmInstruction *insn)
Called at the beginning of every instruction.
virtual SValuePtr bottom_(size_t nbits)
Returns a data-flow bottom value.
WithFormatter with_format(Formatter &fmt)
Used for printing RISC operators with formatting.
virtual SValuePtr mostSignificantSetBit(const SValuePtr &a)=0
Returns position of most significant set bit; zero when no bits are set.
SValuePtr get_val_protoval() const
Return the value protoval.
virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const
Lookup a register by name.
void printRegisters(std::ostream &stream, const std::string &prefix="")
Print the register contents.
virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &a)
Writes a value to a register.
virtual bool is_number() const =0
Determines if the value is a concrete number.
virtual void iproc_set(int key, InsnProcessor *iproc)
Set an iproc table entry to the specified value.
void set_byteOrder(ByteOrder::Endianness bo)
Memory byte order.
virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops)
Write a value to a register.
void memoryMergeDebugging(bool b)
Turn on output for memory merge debugging.
virtual InsnProcessor * iproc_get(int key)
Obtain an iproc table entry for the specified key.
bool get_show_properties() const
Whether to show register properties.
virtual SValuePtr negate(const SValuePtr &a)=0
Two's complement.
bool autoResetInstructionPointer_
Reset instruction pointer register for each instruction.
virtual SValuePtr shiftLeft(const SValuePtr &a, const SValuePtr &nbits)=0
Returns arg shifted left.
virtual SValuePtr isUnsignedGreaterThan(const SValuePtr &a, const SValuePtr &b)
Comparison for unsigned values.
virtual SValuePtr peekRegister(RegisterDescriptor, const SValuePtr &dflt)
Obtain a register value without side effects.
Defines registers available for a particular architecture.
Definition: Registers.h:35
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
virtual void set_register_dictionary(const RegisterDictionary *regdict)
Access the register dictionary.
void set_indentation_suffix(const std::string &s)
Indentation string appended to the line prefix for multi-level, multi-line outputs.
virtual RiscOperatorsPtr create(const SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Virtual allocating constructor.
virtual const std::string & name() const
Property: Name used for debugging.
virtual RegisterDescriptor stackPointerRegister() const =0
Returns the stack pointer register.
void print(std::ostream &stream, const std::string &prefix="") const
Print the state.
virtual SValuePtr readRegister(RegisterDescriptor reg)
Reads a value from a register.
virtual SValuePtr undefined_(size_t nbits) const =0
Create a new undefined semantic value.
virtual SValuePtr number_(size_t nbits, uint64_t value)
Returns a number of the specified bit width.
Sawyer::Container::Set< InputOutputProperty > InputOutputPropertySet
Set of Boolean properties.
Base class for all ROSE exceptions.
Definition: RoseException.h:9
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 byteRestricted() const
Indicates whether memory cell values are required to be eight bits wide.
virtual SValuePtr fpSubtract(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType)
Subtract one floating-point value from another.
Base class for exceptions thrown by instruction semantics.
virtual SValuePtr number_(size_t nbits, uint64_t number) const
Return a semantic value representing a number.
virtual SValuePtr unsignedDivide(const SValuePtr &a, const SValuePtr &b)=0
Divides two unsigned values.
virtual SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops)=0
Read a register without side effects.
virtual SValuePtr signedModulo(const SValuePtr &a, const SValuePtr &b)=0
Calculates modulo with signed values.
virtual SValuePtr isNotEqual(const SValuePtr &a, const SValuePtr &b)
Equality comparison.
Floating point types.
virtual void print(std::ostream &stream, Formatter &fmt) const
Print multi-line output for this object.
virtual SValuePtr isSignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b)
Comparison for signed values.
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.
virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps)=0
Write a value to memory.
The location was read on behalf of an instruction.
void set_register_dictionary(const RegisterDictionary *rd)
The register dictionary which is used for printing register names.
virtual SValuePtr read(SgAsmExpression *, size_t value_nbits=0, size_t addr_nbits=0)
Reads an R-value expression.
StatePtr Ptr
Shared-ownership pointer for a State.
Adjusts a Formatter for one additional level of indentation.
virtual SValuePtr and_(const SValuePtr &a, const SValuePtr &b)=0
Computes bit-wise AND of two values.
virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops)
Read a value from a register.
virtual SValuePtr signedDivide(const SValuePtr &a, const SValuePtr &b)=0
Divides two signed values.