ROSE  0.9.10.205
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 "Registers.h"
7 #include "FormatRestorer.h"
8 
9 #include <boost/shared_ptr.hpp>
10 #include <boost/enable_shared_from_this.hpp>
11 #include <boost/optional.hpp>
12 #include <boost/serialization/access.hpp>
13 #ifndef USE_ROSE // [Robb P Matzke 2016-11-11]: ROSE cannot compile this header
14 #include <boost/serialization/shared_ptr.hpp>
15 #endif
16 #include <boost/serialization/string.hpp>
17 #include <Sawyer/Assert.h>
18 #include <Sawyer/IntervalMap.h>
19 #include <Sawyer/IntervalSetMap.h>
20 #include <Sawyer/Map.h>
21 #include <Sawyer/Optional.h>
22 #include <Sawyer/Set.h>
23 
24 namespace Rose {
25 namespace BinaryAnalysis {
26 
294 namespace InstructionSemantics2 {
295 
298 
300 void initDiagnostics();
301 
304 namespace BaseSemantics {
305 
306 class RiscOperators;
307 
311 class Formatter {
312 public:
313  Formatter(): regdict(NULL), suppress_initial_values(false), indentation_suffix(" "), show_latest_writers(true),
314  show_properties(true) {}
315  virtual ~Formatter() {}
316 
319  const RegisterDictionary *get_register_dictionary() const { return regdict; }
320  void set_register_dictionary(const RegisterDictionary *rd) { regdict = rd; }
326  bool get_suppress_initial_values() const { return suppress_initial_values; }
327  void set_suppress_initial_values(bool b=true) { suppress_initial_values=b; }
333  std::string get_line_prefix() const { return line_prefix; }
334  void set_line_prefix(const std::string &s) { line_prefix = s; }
339  std::string get_indentation_suffix() const { return indentation_suffix; }
340  void set_indentation_suffix(const std::string &s) { indentation_suffix = s; }
345  bool get_show_latest_writers() const { return show_latest_writers; }
346  void set_show_latest_writers(bool b=true) { show_latest_writers = b; }
347  void clear_show_latest_writers() { show_latest_writers = false; }
352  bool get_show_properties() const { return show_properties; }
353  void set_show_properties(bool b=true) { show_properties = b; }
354  void clear_show_properties() { show_properties = false; }
357 protected:
358  const RegisterDictionary *regdict;
359  bool suppress_initial_values;
360  std::string line_prefix;
361  std::string indentation_suffix;
362  bool show_latest_writers;
363  bool show_properties;
364 };
365 
369 class Indent {
370 private:
371  Formatter &fmt;
372  std::string old_line_prefix;
373 public:
374  Indent(Formatter &fmt): fmt(fmt) {
375  old_line_prefix = fmt.get_line_prefix();
376  fmt.set_line_prefix(old_line_prefix + fmt.get_indentation_suffix());
377  }
378  ~Indent() {
379  fmt.set_line_prefix(old_line_prefix);
380  }
381 };
382 
402 };
403 
406 
407 
408 
410 // Exceptions
412 
414 class Exception: public std::runtime_error {
415 public:
416  SgAsmInstruction *insn;
417  Exception(const std::string &mesg, SgAsmInstruction *insn): std::runtime_error(mesg), insn(insn) {}
418  void print(std::ostream&) const;
419 };
420 
421 class NotImplemented: public Exception {
422 public:
423  NotImplemented(const std::string &mesg, SgAsmInstruction *insn)
424  : Exception(mesg, insn) {}
425 };
426 
427 
428 
430 // Merging states
432 
435 
453  bool memoryAddressesMayAlias_;
454  bool memoryMergeDebugging_;
455 
456 protected:
457  Merger(): memoryAddressesMayAlias_(true), memoryMergeDebugging_(false) {}
458 
459 public:
461  typedef MergerPtr Ptr;
462 
464  static Ptr instance() {
465  return Ptr(new Merger);
466  }
467 
474  bool memoryAddressesMayAlias() const { return memoryAddressesMayAlias_; }
475  void memoryAddressesMayAlias(bool b) { memoryAddressesMayAlias_ = b; }
485  bool memoryMergeDebugging() const { return memoryMergeDebugging_; }
486  void memoryMergeDebugging(bool b) { memoryMergeDebugging_ = b; }
488 };
489 
491 // Semantic Values
493 
494 // This is leftover for compatibility with an older API. The old API had code like this:
495 // User::SValue user_svalue = BaseSemantics::dynamic_pointer_cast<User::SValue>(base_svalue);
496 // Which can be replaced now with
497 // User::SValue user_svalue = base_svalue.dynamicCast<User::SValue>();
498 template<class To, class From>
499 Sawyer::SharedPointer<To> dynamic_pointer_cast(const Sawyer::SharedPointer<From> &from) {
500  return from.template dynamicCast<To>();
501 }
502 
505 
521 protected:
522  size_t width;
524  // Serialization
526 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
527 private:
528  friend class boost::serialization::access;
529 
530  template<class S>
531  void serialize(S &s, const unsigned /*version*/) {
532  s & BOOST_SERIALIZATION_NVP(width);
533  }
534 #endif
535 
537  // Normal, protected, C++ constructors
538 protected:
539  SValue(): width(0) {} // needed for serialization
540  explicit SValue(size_t nbits): width(nbits) {} // hot
541  SValue(const SValue &other): Sawyer::SharedObject(other), width(other.width) {}
542 
543 public:
545  typedef SValuePtr Ptr;
546 
547 public:
548  virtual ~SValue() {}
549 
551  // Allocating static constructor. None are needed--this class is abstract.
552 
554  // Allocating virtual constructors. undefined_() needs underscores, so we do so consistently for all
555  // these allocating virtual c'tors. However, we use copy() rather than copy_() because this one is fundamentally
556  // different: the object (this) is use for more than just selecting which virtual method to invoke.
557  //
558  // The naming scheme we use here is a bit different than for most other objects for historical reasons. Most other classes
559  // use "create" and "clone" as the virtual constructor names, but SValue uses names ending in undercore, and "copy". The
560  // other difference (at least in this base class) is that we don't define any real constructors or static allocating
561  // constructors (usually named "instance")--it's because this is an abstract class.
562 public:
568  virtual SValuePtr undefined_(size_t nbits) const = 0; // hot
569 
578  virtual SValuePtr unspecified_(size_t nbits) const = 0;
579 
585  virtual SValuePtr bottom_(size_t nBits) const = 0;
586 
590  virtual SValuePtr number_(size_t nbits, uint64_t number) const = 0; // hot
591 
595  virtual SValuePtr boolean_(bool value) const { return number_(1, value?1:0); }
596 
600  virtual SValuePtr copy(size_t new_width=0) const = 0;
601 
629  createOptionalMerge(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const = 0;
630 
637  SValuePtr createMerged(const SValuePtr &other, const MergerPtr &merger, const SmtSolverPtr &solver) const /*final*/ {
638  return createOptionalMerge(other, merger, solver).orElse(copy());
639  }
640 
642  // Dynamic pointer casts. No-ops since this is the base class
643 public:
644  static SValuePtr promote(const SValuePtr &x) {
645  ASSERT_not_null(x);
646  return x;
647  }
648 
650  // The rest of the API...
651 public:
657  virtual bool isBottom() const = 0;
658 
661  virtual bool is_number() const = 0;
662 
665  virtual uint64_t get_number() const = 0;
666 
669  virtual size_t get_width() const { return width; }
670  virtual void set_width(size_t nbits) { width = nbits; }
674  virtual bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
675 
677  virtual bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
678 
681  bool isTrue() const {
682  return is_number() && get_number()!=0;
683  }
684 
687  bool isFalse() const {
688  return is_number() && get_number()==0;
689  }
690 
694  void print(std::ostream &stream) const { Formatter fmt; print(stream, fmt); }
695  virtual void print(std::ostream&, Formatter&) const = 0;
700  SValuePtr obj;
701  Formatter &fmt;
702  public:
703  WithFormatter(const SValuePtr &svalue, Formatter &fmt): obj(svalue), fmt(fmt) {}
704  void print(std::ostream &stream) const { obj->print(stream, fmt); }
705  };
706 
723  virtual std::string get_comment() const { return ""; }
724  virtual void set_comment(const std::string&) const {} // const is intended; cf. doxygen comment
726 };
727 
728 
729 
731 // Register States
733 
735 typedef boost::shared_ptr<class RegisterState> RegisterStatePtr;
736 
740 class RegisterState: public boost::enable_shared_from_this<RegisterState> {
741 private:
742  MergerPtr merger_;
743  SValuePtr protoval_;
745 protected:
748  // Serialization
750 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
751 private:
752  friend class boost::serialization::access;
753 
754  template<class S>
755  void serialize(S &s, const unsigned version) {
756  //s & merger_; -- not saved
757  s & BOOST_SERIALIZATION_NVP(protoval_);
758  if (version >= 1)
759  s & BOOST_SERIALIZATION_NVP(regdict);
760  }
761 #endif
762 
763 
765  // Real constructors
766 protected:
767  RegisterState()
768  : regdict(NULL) {} // for serialization
769 
770  RegisterState(const SValuePtr &protoval, const RegisterDictionary *regdict)
771  : protoval_(protoval), regdict(regdict) {
772  ASSERT_not_null(protoval_);
773  }
774 
775 public:
777  typedef RegisterStatePtr Ptr;
778 
779 public:
780  virtual ~RegisterState() {}
781 
783  // Static allocating constructors. None are needed--this class is abstract.
784 
785 
787  // Virtual constructors.
788 public:
793  virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const = 0;
794 
796  virtual RegisterStatePtr clone() const = 0;
797 
799  // Dynamic pointer casts. No-op since this is the base class.
800 public:
801  static RegisterStatePtr promote(const RegisterStatePtr &x) {
802  ASSERT_not_null(x);
803  return x;
804  }
805 
806 public:
808  // The rest of the API...
809 
818  MergerPtr merger() const { return merger_; }
819  void merger(const MergerPtr &m) { merger_ = m; }
823  SValuePtr protoval() const { return protoval_; }
824 
829  void set_register_dictionary(const RegisterDictionary *rd) { regdict = rd; }
842  virtual void clear() = 0;
843 
845  virtual void zero() = 0;
846 
850  virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops) = 0;
851 
865  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) = 0;
866 
870  virtual SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) = 0;
871 
878  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops) = 0;
879 
882  void print(std::ostream &stream, const std::string prefix="") const {
883  Formatter fmt;
884  fmt.set_line_prefix(prefix);
885  print(stream, fmt);
886  }
887  virtual void print(std::ostream&, Formatter&) const = 0;
892  RegisterStatePtr obj;
893  Formatter &fmt;
894  public:
895  WithFormatter(const RegisterStatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
896  void print(std::ostream &stream) const { obj->print(stream, fmt); }
897  };
898 
906  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
910 };
911 
912 
914 // Memory State
916 
918 typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
919 
923 class MemoryState: public boost::enable_shared_from_this<MemoryState> {
924  SValuePtr addrProtoval_;
925  SValuePtr valProtoval_;
926  ByteOrder::Endianness byteOrder_;
927  MergerPtr merger_;
928  bool byteRestricted_; // are cell values all exactly one byte wide?
929 
931  // Serialization
932 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
933 private:
934  friend class boost::serialization::access;
935 
936  template<class S>
937  void serialize(S &s, const unsigned /*version*/) {
938  s & BOOST_SERIALIZATION_NVP(addrProtoval_);
939  s & BOOST_SERIALIZATION_NVP(valProtoval_);
940  s & BOOST_SERIALIZATION_NVP(byteOrder_);
941  //s & merger_ -- not saved
942  s & BOOST_SERIALIZATION_NVP(byteRestricted_);
943  }
944 #endif
945 
946 
948  // Real constructors
949 protected:
950  MemoryState() // for serialization
951  : byteOrder_(ByteOrder::ORDER_UNSPECIFIED), byteRestricted_(true) {}
952 
953  explicit MemoryState(const SValuePtr &addrProtoval, const SValuePtr &valProtoval)
954  : addrProtoval_(addrProtoval), valProtoval_(valProtoval), byteOrder_(ByteOrder::ORDER_UNSPECIFIED),
955  byteRestricted_(true) {
956  ASSERT_not_null(addrProtoval);
957  ASSERT_not_null(valProtoval);
958  }
959 
960  MemoryState(const MemoryStatePtr &other)
961  : addrProtoval_(other->addrProtoval_), valProtoval_(other->valProtoval_), byteOrder_(ByteOrder::ORDER_UNSPECIFIED),
962  merger_(other->merger_), byteRestricted_(other->byteRestricted_) {}
963 
964 public:
966  typedef MemoryStatePtr Ptr;
967 
968 public:
969  virtual ~MemoryState() {}
970 
972  // Static allocating constructors. None needed since this class is abstract
973 
975  // Virtual constructors
976 public:
983  virtual MemoryStatePtr create(const SValuePtr &addrProtoval, const SValuePtr &valProtoval) const = 0;
984 
986  virtual MemoryStatePtr clone() const = 0;
987 
989  // Dynamic pointer casts. No-op since this is the base class.
990 public:
991  static MemoryStatePtr promote(const MemoryStatePtr &x) {
992  ASSERT_not_null(x);
993  return x;
994  }
995 
997  // Methods first declared at this level of the class hierarchy
998 public:
1007  MergerPtr merger() const { return merger_; }
1008  void merger(const MergerPtr &m) { merger_ = m; }
1013  SValuePtr get_addr_protoval() const { return addrProtoval_; }
1014 
1017  SValuePtr get_val_protoval() const { return valProtoval_; }
1018 
1020  virtual void clear() = 0;
1021 
1029  bool byteRestricted() const { return byteRestricted_; }
1030  void byteRestricted(bool b) { byteRestricted_ = b; }
1035  ByteOrder::Endianness get_byteOrder() const { return byteOrder_; }
1036  void set_byteOrder(ByteOrder::Endianness bo) { byteOrder_ = bo; }
1042  virtual bool merge(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps) = 0;
1043 
1063  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
1064  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1065 
1070  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
1071  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1072 
1082  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value,
1083  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1084 
1087  void print(std::ostream &stream, const std::string prefix="") const {
1088  Formatter fmt;
1089  fmt.set_line_prefix(prefix);
1090  print(stream, fmt);
1091  }
1092  virtual void print(std::ostream&, Formatter&) const = 0;
1097  MemoryStatePtr obj;
1098  Formatter &fmt;
1099  public:
1100  WithFormatter(const MemoryStatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1101  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1102  };
1103 
1111  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1114 };
1115 
1116 
1117 
1119 // State
1121 
1123 typedef boost::shared_ptr<class State> StatePtr;
1124 
1141 class State: public boost::enable_shared_from_this<State> {
1142  SValuePtr protoval_; // Initial value used to create additional values as needed.
1143  RegisterStatePtr registers_; // All machine register values for this semantic state.
1144  MemoryStatePtr memory_; // All memory for this semantic state.
1145 
1147  // Serialization
1148 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
1149 private:
1150  friend class boost::serialization::access;
1151 
1152  template<class S>
1153  void serialize(S &s, const unsigned /*version*/) {
1154  s & BOOST_SERIALIZATION_NVP(protoval_);
1155  s & BOOST_SERIALIZATION_NVP(registers_);
1156  s & BOOST_SERIALIZATION_NVP(memory_);
1157  }
1158 #endif
1159 
1160 
1162  // Real constructors
1163 protected:
1164  // needed for serialization
1165  State() {}
1166 
1167  State(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
1168  : registers_(registers), memory_(memory) {
1169  ASSERT_not_null(registers);
1170  ASSERT_not_null(memory);
1171  protoval_ = registers->protoval();
1172  ASSERT_not_null(protoval_);
1173  }
1174 
1175  // deep-copy the registers and memory
1176  State(const State &other)
1177  : boost::enable_shared_from_this<State>(other), protoval_(other.protoval_) {
1178  registers_ = other.registers_->clone();
1179  memory_ = other.memory_->clone();
1180  }
1181 
1182 public:
1184  typedef StatePtr Ptr;
1185 
1186 public:
1187  virtual ~State() {}
1188 
1190  // Static allocating constructors
1191 public:
1193  static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory) {
1194  return StatePtr(new State(registers, memory));
1195  }
1196 
1198  static StatePtr instance(const StatePtr &other) {
1199  return StatePtr(new State(*other));
1200  }
1201 
1203  // Virtual constructors
1204 public:
1206  virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const {
1207  return instance(registers, memory);
1208  }
1209 
1213  virtual StatePtr clone() const {
1214  StatePtr self = boost::const_pointer_cast<State>(shared_from_this());
1215  return instance(self);
1216  }
1217 
1219  // Dynamic pointer casts. No-op since this is the base class.
1220 public:
1221  static StatePtr promote(const StatePtr &x) {
1222  ASSERT_not_null(x);
1223  return x;
1224  }
1225 
1227  // Other methods that are part of our API. Most of these just chain to either the register state and/or the memory state.
1228 public:
1230  SValuePtr protoval() const { return protoval_; }
1231 
1233  virtual void clear();
1234 
1238  virtual void zero_registers();
1239 
1243  virtual void clear_memory();
1244 
1248  RegisterStatePtr registerState() const {
1249  return registers_;
1250  }
1251 
1255  MemoryStatePtr memoryState() const {
1256  return memory_;
1257  }
1258 
1263  virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
1264 
1269  virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
1270 
1275  virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
1276 
1281  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
1282  RiscOperators *addrOps, RiscOperators *valOps);
1283 
1288  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
1289  RiscOperators *addrOps, RiscOperators *valOps);
1290 
1295  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
1296 
1302  void printRegisters(std::ostream &stream, const std::string &prefix = "");
1303  virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
1311  void printMemory(std::ostream &stream, const std::string &prefix = "") const;
1312  virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
1317  void print(std::ostream &stream, const std::string &prefix = "") const;
1318  virtual void print(std::ostream&, Formatter&) const;
1323  StatePtr obj;
1324  Formatter &fmt;
1325  public:
1326  WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1327  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1328  };
1329 
1337  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1346  virtual bool merge(const StatePtr &other, RiscOperators *ops);
1347 };
1348 
1349 
1350 
1352 // RISC Operators
1354 
1356 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
1357 
1378 class RiscOperators: public boost::enable_shared_from_this<RiscOperators> {
1379  SValuePtr protoval_; // Prototypical value used for its virtual constructors
1380  StatePtr currentState_; // State upon which RISC operators operate
1381  StatePtr initialState_; // Lazily updated initial state; see readMemory
1382  SmtSolverPtr solver_; // Optional SMT solver
1383  SgAsmInstruction *currentInsn_; // Current instruction, as set by latest startInstruction call
1384  size_t nInsns_; // Number of instructions processed
1385  std::string name_; // Name to use for debugging
1386 
1388  // Serialization
1389 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
1390 private:
1391  friend class boost::serialization::access;
1392 
1393  template<class S>
1394  void serialize(S &s, const unsigned /*version*/) {
1395  s & BOOST_SERIALIZATION_NVP(protoval_);
1396  s & BOOST_SERIALIZATION_NVP(currentState_);
1397  s & BOOST_SERIALIZATION_NVP(initialState_);
1398  s & BOOST_SERIALIZATION_NVP(solver_);
1399  s & BOOST_SERIALIZATION_NVP(currentInsn_);
1400  s & BOOST_SERIALIZATION_NVP(nInsns_);
1401  s & BOOST_SERIALIZATION_NVP(name_);
1402  }
1403 #endif
1404 
1406  // Real constructors
1407 protected:
1408  // for serialization
1409  RiscOperators()
1410  : currentInsn_(NULL), nInsns_(0) {}
1411 
1412  explicit RiscOperators(const SValuePtr &protoval, const SmtSolverPtr &solver = SmtSolverPtr())
1413  : protoval_(protoval), solver_(solver), currentInsn_(NULL), nInsns_(0) {
1414  ASSERT_not_null(protoval_);
1415  }
1416 
1417  explicit RiscOperators(const StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr())
1418  : currentState_(state), solver_(solver), currentInsn_(NULL), nInsns_(0) {
1419  ASSERT_not_null(state);
1420  protoval_ = state->protoval();
1421  }
1422 
1423 public:
1425  typedef RiscOperatorsPtr Ptr;
1426 
1427 public:
1428  virtual ~RiscOperators() {}
1429 
1431  // Static allocating constructors. None needed since this class is abstract.
1432 
1433 
1435  // Virtual constructors.
1436 public:
1440  virtual RiscOperatorsPtr create(const SValuePtr &protoval, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
1441 
1446  virtual RiscOperatorsPtr create(const StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
1447 
1449  // Dynamic pointer casts. No-op since this is the base class.
1450 public:
1451  static RiscOperatorsPtr promote(const RiscOperatorsPtr &x) {
1452  ASSERT_not_null(x);
1453  return x;
1454  }
1455 
1457  // Other methods part of our API
1458 public:
1462  virtual SValuePtr protoval() const { return protoval_; }
1463 
1472  virtual SmtSolverPtr solver() const { return solver_; }
1473  virtual void solver(const SmtSolverPtr &s) { solver_ = s; }
1487  virtual StatePtr currentState() const { return currentState_; }
1488  virtual void currentState(const StatePtr &s) { currentState_ = s; }
1528  virtual StatePtr initialState() const { return initialState_; }
1529  virtual void initialState(const StatePtr &s) { initialState_ = s; }
1537  virtual const std::string& name() const { return name_; }
1538  virtual void name(const std::string &s) { name_ = s; }
1543  void print(std::ostream &stream, const std::string prefix="") const {
1544  Formatter fmt;
1545  fmt.set_line_prefix(prefix);
1546  print(stream, fmt);
1547  }
1548  virtual void print(std::ostream &stream, Formatter &fmt) const {
1549  currentState_->print(stream, fmt);
1550  }
1555  RiscOperatorsPtr obj;
1556  Formatter &fmt;
1557  public:
1558  WithFormatter(const RiscOperatorsPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1559  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1560  };
1561 
1569  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1578  virtual size_t nInsns() const { return nInsns_; }
1579  virtual void nInsns(size_t n) { nInsns_ = n; }
1587  return currentInsn_;
1588  }
1589 
1592  virtual void startInstruction(SgAsmInstruction *insn);
1593 
1596  virtual void finishInstruction(SgAsmInstruction *insn) {
1597  ASSERT_not_null(insn);
1598  ASSERT_require(currentInsn_==insn);
1599  currentInsn_ = NULL;
1600  };
1601 
1602 
1604  // Value Construction Operations
1606  // The trailing underscores are necessary for for undefined_() on some machines, so we just add one to the end of all the
1607  // virtual constructors for consistency.
1608 
1610  virtual SValuePtr undefined_(size_t nbits) {
1611  return protoval_->undefined_(nbits);
1612  }
1613  virtual SValuePtr unspecified_(size_t nbits) {
1614  return protoval_->unspecified_(nbits);
1615  }
1616 
1618  virtual SValuePtr number_(size_t nbits, uint64_t value) {
1619  return protoval_->number_(nbits, value);
1620  }
1621 
1623  virtual SValuePtr boolean_(bool value) {
1624  return protoval_->boolean_(value);
1625  }
1626 
1628  virtual SValuePtr bottom_(size_t nbits) {
1629  return protoval_->bottom_(nbits);
1630  }
1631 
1632 
1634  // x86-specific Operations (FIXME)
1636 
1639  virtual SValuePtr filterCallTarget(const SValuePtr &a) {
1640  return a->copy();
1641  }
1642 
1646  virtual SValuePtr filterReturnTarget(const SValuePtr &a) {
1647  return a->copy();
1648  }
1649 
1653  virtual SValuePtr filterIndirectJumpTarget(const SValuePtr &a) {
1654  return a->copy();
1655  }
1656 
1658  virtual void hlt() {}
1659 
1661  virtual void cpuid() {}
1662 
1664  virtual SValuePtr rdtsc() { return unspecified_(64); }
1665 
1666 
1668  // Boolean Operations
1670 
1673  virtual SValuePtr and_(const SValuePtr &a, const SValuePtr &b) = 0;
1674 
1677  virtual SValuePtr or_(const SValuePtr &a, const SValuePtr &b) = 0;
1678 
1681  virtual SValuePtr xor_(const SValuePtr &a, const SValuePtr &b) = 0;
1682 
1684  virtual SValuePtr invert(const SValuePtr &a) = 0;
1685 
1689  virtual SValuePtr extract(const SValuePtr &a, size_t begin_bit, size_t end_bit) = 0;
1690 
1694  virtual SValuePtr concat(const SValuePtr &a, const SValuePtr &b) = 0;
1695 
1698  virtual SValuePtr leastSignificantSetBit(const SValuePtr &a) = 0;
1699 
1702  virtual SValuePtr mostSignificantSetBit(const SValuePtr &a) = 0;
1703 
1707  virtual SValuePtr rotateLeft(const SValuePtr &a, const SValuePtr &nbits) = 0;
1708 
1712  virtual SValuePtr rotateRight(const SValuePtr &a, const SValuePtr &nbits) = 0;
1713 
1717  virtual SValuePtr shiftLeft(const SValuePtr &a, const SValuePtr &nbits) = 0;
1718 
1722  virtual SValuePtr shiftRight(const SValuePtr &a, const SValuePtr &nbits) = 0;
1723 
1728  virtual SValuePtr shiftRightArithmetic(const SValuePtr &a, const SValuePtr &nbits) = 0;
1729 
1730 
1732  // Comparison Operations
1734 
1737  virtual SValuePtr equalToZero(const SValuePtr &a) = 0;
1738 
1742  virtual SValuePtr ite(const SValuePtr &cond, const SValuePtr &a, const SValuePtr &b) = 0;
1743 
1750  virtual SValuePtr isEqual(const SValuePtr &a, const SValuePtr &b);
1751  virtual SValuePtr isNotEqual(const SValuePtr &a, const SValuePtr &b);
1761  virtual SValuePtr isUnsignedLessThan(const SValuePtr &a, const SValuePtr &b);
1762  virtual SValuePtr isUnsignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1763  virtual SValuePtr isUnsignedGreaterThan(const SValuePtr &a, const SValuePtr &b);
1764  virtual SValuePtr isUnsignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1774  virtual SValuePtr isSignedLessThan(const SValuePtr &a, const SValuePtr &b);
1775  virtual SValuePtr isSignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1776  virtual SValuePtr isSignedGreaterThan(const SValuePtr &a, const SValuePtr &b);
1777  virtual SValuePtr isSignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1780  // Integer Arithmetic Operations
1783 
1786  virtual SValuePtr unsignedExtend(const SValuePtr &a, size_t new_width) {
1787  return a->copy(new_width);
1788  }
1789 
1792  virtual SValuePtr signExtend(const SValuePtr &a, size_t new_width) = 0;
1793 
1796  virtual SValuePtr add(const SValuePtr &a, const SValuePtr &b) = 0;
1797 
1800  virtual SValuePtr subtract(const SValuePtr &minuend, const SValuePtr &subtrahend);
1801 
1818  virtual SValuePtr addWithCarries(const SValuePtr &a, const SValuePtr &b, const SValuePtr &c,
1819  SValuePtr &carry_out/*output*/) = 0;
1820 
1822  virtual SValuePtr negate(const SValuePtr &a) = 0;
1823 
1825  virtual SValuePtr signedDivide(const SValuePtr &a, const SValuePtr &b) = 0;
1826 
1828  virtual SValuePtr signedModulo(const SValuePtr &a, const SValuePtr &b) = 0;
1829 
1831  virtual SValuePtr signedMultiply(const SValuePtr &a, const SValuePtr &b) = 0;
1832 
1834  virtual SValuePtr unsignedDivide(const SValuePtr &a, const SValuePtr &b) = 0;
1835 
1837  virtual SValuePtr unsignedModulo(const SValuePtr &a, const SValuePtr &b) = 0;
1838 
1840  virtual SValuePtr unsignedMultiply(const SValuePtr &a, const SValuePtr &b) = 0;
1841 
1842 
1844  // Interrupt and system calls
1846 
1851  virtual void interrupt(int /*majr*/, int /*minr*/) {}
1852 
1853 
1855  // Floating-point operations
1856  //
1857  // For now these all have default implementations that throw NotImplemented, but we might change them to pure virtual
1858  // sometime in the future so they're consistent with most other RISC operators. [Robb P. Matzke 2015-08-03]
1860 
1862  virtual SValuePtr fpFromInteger(const SValuePtr &intValue, SgAsmFloatType *fpType);
1863 
1869  virtual SValuePtr fpToInteger(const SValuePtr &fpValue, SgAsmFloatType *fpType, const SValuePtr &dflt);
1870 
1874  virtual SValuePtr fpConvert(const SValuePtr &a, SgAsmFloatType *aType, SgAsmFloatType *retType);
1875 
1877  virtual SValuePtr fpIsNan(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1878 
1880  virtual SValuePtr fpIsDenormalized(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1881 
1883  virtual SValuePtr fpIsZero(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1884 
1889  virtual SValuePtr fpIsInfinity(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1890 
1894  virtual SValuePtr fpSign(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1895 
1901  virtual SValuePtr fpEffectiveExponent(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1902 
1906  virtual SValuePtr fpAdd(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1907 
1912  virtual SValuePtr fpSubtract(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1913 
1917  virtual SValuePtr fpMultiply(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1918 
1922  virtual SValuePtr fpDivide(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1923 
1927  virtual SValuePtr fpSquareRoot(const SValuePtr &a, SgAsmFloatType *fpType);
1928 
1932  virtual SValuePtr fpRoundTowardZero(const SValuePtr &a, SgAsmFloatType *fpType);
1933 
1934 
1936  // State Accessing Operations
1938 
1963  virtual SValuePtr readRegister(RegisterDescriptor reg) { // old subclasses can still override this if they want,
1964  return readRegister(reg, undefined_(reg.get_nbits())); // but new subclasses should not override this method.
1965  }
1966  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt); // new subclasses override this
1978  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &a) {
1979  ASSERT_not_null(currentState_);
1980  currentState_->writeRegister(reg, a, this);
1981  }
1982 
1990  virtual SValuePtr peekRegister(RegisterDescriptor, const SValuePtr &dflt);
1992  return peekRegister(reg, undefined_(reg.get_nbits()));
1993  }
2018  virtual SValuePtr readMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt,
2019  const SValuePtr &cond) = 0;
2020 
2031  virtual void writeMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &data,
2032  const SValuePtr &cond) = 0;
2033 
2038  virtual SValuePtr peekMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt) = 0;
2039 };
2040 
2041 
2042 
2044 // Instruction Dispatcher
2046 
2048 typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
2049 
2052 public:
2053  virtual ~InsnProcessor() {}
2054  virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
2055 };
2056 
2070 class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
2071 protected:
2072  RiscOperatorsPtr operators;
2074  size_t addrWidth_;
2077  // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
2078  // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iproc_key() virtual method.
2079  typedef std::vector<InsnProcessor*> InsnProcessors;
2080  InsnProcessors iproc_table;
2081 
2082 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
2083 private:
2084  friend class boost::serialization::access;
2085 
2086  template<class S>
2087  void serialize(S &s, const unsigned /*version*/) {
2088  s & BOOST_SERIALIZATION_NVP(operators);
2089  s & BOOST_SERIALIZATION_NVP(regdict);
2090  s & BOOST_SERIALIZATION_NVP(addrWidth_);
2091  s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
2092  //s & iproc_table; -- not saved
2093  }
2094 #endif
2095 
2097  // Real constructors
2098 protected:
2099  // Prototypical constructor
2100  Dispatcher(): regdict(NULL), addrWidth_(0), autoResetInstructionPointer_(true) {}
2101 
2102  // Prototypical constructor
2103  Dispatcher(size_t addrWidth, const RegisterDictionary *regs)
2104  : regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {}
2105 
2106  Dispatcher(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionary *regs)
2107  : operators(ops), regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {
2108  ASSERT_not_null(operators);
2109  ASSERT_not_null(regs);
2110  }
2111 
2112 public:
2114  typedef DispatcherPtr Ptr;
2115 
2116 public:
2117  virtual ~Dispatcher() {
2118  for (InsnProcessors::iterator iter=iproc_table.begin(); iter!=iproc_table.end(); ++iter)
2119  delete *iter;
2120  }
2121 
2123  // Static allocating constructors. None since this is an abstract class
2124 
2125 
2127  // Virtual constructors
2128 public:
2130  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const = 0;
2131 
2133  // Methods to process instructions
2134 public:
2136  virtual void processInstruction(SgAsmInstruction *insn);
2137 
2139  // Instruction processor table operations
2140 public:
2145 
2149  virtual void iproc_replace(SgAsmInstruction *insn, InsnProcessor *iproc);
2150 
2152  virtual int iproc_key(SgAsmInstruction*) const = 0;
2153 
2157  virtual void iproc_set(int key, InsnProcessor *iproc);
2158 
2160  virtual InsnProcessor *iproc_get(int key);
2161 
2163  // Convenience methods that defer the call to some member object
2164 public:
2166  virtual RiscOperatorsPtr get_operators() const { return operators; }
2167 
2170  virtual StatePtr currentState() const { return operators ? operators->currentState() : StatePtr(); }
2171 
2173  virtual SValuePtr protoval() const { return operators ? operators->protoval() : SValuePtr(); }
2174 
2180  return operators ? operators->currentInstruction() : NULL;
2181  }
2182 
2184  virtual SValuePtr undefined_(size_t nbits) const {
2185  ASSERT_not_null(operators);
2186  return operators->undefined_(nbits);
2187  }
2188  virtual SValuePtr unspecified_(size_t nbits) const {
2189  ASSERT_not_null(operators);
2190  return operators->unspecified_(nbits);
2191  }
2192 
2194  virtual SValuePtr number_(size_t nbits, uint64_t number) const {
2195  ASSERT_not_null(operators);
2196  return operators->number_(nbits, number);
2197  }
2198 
2200  // Methods related to registers
2201 public:
2214  return regdict;
2215  }
2216  virtual void set_register_dictionary(const RegisterDictionary *regdict) {
2217  this->regdict = regdict;
2218  }
2225  virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
2226 
2233  size_t addressWidth() const { return addrWidth_; }
2234  void addressWidth(size_t nbits);
2238  virtual RegisterDescriptor instructionPointerRegister() const = 0;
2239 
2241  virtual RegisterDescriptor stackPointerRegister() const = 0;
2242 
2250  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
2254  // Miscellaneous methods that tend to be the same for most dispatchers
2256 public:
2257 
2267 
2272 
2276  virtual void incrementRegisters(SgAsmExpression*);
2277 
2281  virtual void decrementRegisters(SgAsmExpression*);
2282 
2286  virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
2287 
2293  virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
2294 
2298  virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
2299 };
2300 
2301 
2302 
2304 // Printing
2306 
2307 std::ostream& operator<<(std::ostream&, const Exception&);
2308 std::ostream& operator<<(std::ostream&, const SValue&);
2309 std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
2310 std::ostream& operator<<(std::ostream&, const MemoryState&);
2311 std::ostream& operator<<(std::ostream&, const MemoryState::WithFormatter&);
2312 std::ostream& operator<<(std::ostream&, const RegisterState&);
2313 std::ostream& operator<<(std::ostream&, const RegisterState::WithFormatter&);
2314 std::ostream& operator<<(std::ostream&, const State&);
2315 std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
2316 std::ostream& operator<<(std::ostream&, const RiscOperators&);
2317 std::ostream& operator<<(std::ostream&, const RiscOperators::WithFormatter&);
2318 
2319 } // namespace
2320 } // namespace
2321 } // namespace
2322 } // namespace
2323 
2324 // Class versions must be at global scope
2326 
2327 #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.
Describes (part of) a physical CPU register.
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.
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.
Defines registers available for a particular architecture.
Definition: Registers.h:32
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.
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.
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.