ROSE  0.9.10.29
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  RegisterDictionary *get_register_dictionary() const { return regdict; }
320  void set_register_dictionary(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  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  }
759 #endif
760 
761 
763  // Real constructors
764 protected:
765  RegisterState() {} // for serialization
766 
767  RegisterState(const SValuePtr &protoval, const RegisterDictionary *regdict)
768  : protoval_(protoval), regdict(regdict) {
769  ASSERT_not_null(protoval_);
770  }
771 
772 public:
775 
776 public:
777  virtual ~RegisterState() {}
778 
780  // Static allocating constructors. None are needed--this class is abstract.
781 
782 
784  // Virtual constructors.
785 public:
790  virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const = 0;
791 
793  virtual RegisterStatePtr clone() const = 0;
794 
796  // Dynamic pointer casts. No-op since this is the base class.
797 public:
798  static RegisterStatePtr promote(const RegisterStatePtr &x) {
799  ASSERT_not_null(x);
800  return x;
801  }
802 
803 public:
805  // The rest of the API...
806 
815  MergerPtr merger() const { return merger_; }
816  void merger(const MergerPtr &m) { merger_ = m; }
820  SValuePtr protoval() const { return protoval_; }
821 
822  // [Robb Matzke 2016-01-22]: deprecated
823  SValuePtr get_protoval() const ROSE_DEPRECATED("use protoval instead") {
824  return protoval();
825  }
826 
845  virtual void clear() = 0;
846 
848  virtual void zero() = 0;
849 
853  virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops) = 0;
854 
868  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) = 0;
869 
873  virtual SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) = 0;
874 
881  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops) = 0;
882 
885  void print(std::ostream &stream, const std::string prefix="") const {
886  Formatter fmt;
887  fmt.set_line_prefix(prefix);
888  print(stream, fmt);
889  }
890  virtual void print(std::ostream&, Formatter&) const = 0;
895  RegisterStatePtr obj;
896  Formatter &fmt;
897  public:
898  WithFormatter(const RegisterStatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
899  void print(std::ostream &stream) const { obj->print(stream, fmt); }
900  };
901 
909  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
913 };
914 
916 typedef boost::shared_ptr<class RegisterStateX86> RegisterStateX86Ptr;
917 
926 public:
927  static const size_t n_gprs = 8;
928  static const size_t n_segregs = 6;
929  static const size_t n_flags = 32;
930  static const size_t n_st = 8;
931  static const size_t n_xmm = 8;
941  // Real constructors
943 protected:
945  : RegisterState(protoval, regdict) {
946  clear();
947  }
948 
949  RegisterStateX86(const RegisterStateX86 &other): RegisterState(other) {
950  ip = other.ip->copy();
951  for (size_t i=0; i<n_gprs; ++i)
952  gpr[i] = other.gpr[i]->copy();
953  for (size_t i=0; i<n_segregs; ++i)
954  segreg[i] = other.segreg[i]->copy();
955  for (size_t i=0; i<n_flags; ++i)
956  flag[i] = other.flag[i]->copy();
957  for (size_t i=0; i<n_st; ++i)
958  st[i] = other.st[i]->copy();
959  fpstatus = other.fpstatus;
960  for (size_t i=0; i<n_xmm; ++i)
961  xmm[i] = other.xmm[i]->copy();
962  }
963 
965  // Static allocating constructors
966 public:
971  return RegisterStateX86Ptr(new RegisterStateX86(protoval, regdict));
972  }
973 
976  return RegisterStateX86Ptr(new RegisterStateX86(*other));
977  }
978 
980  // Virtual constructors
981 public:
982  virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const ROSE_OVERRIDE {
983  return instance(protoval, regdict);
984  }
985 
986  virtual RegisterStatePtr clone() const ROSE_OVERRIDE {
987  return RegisterStatePtr(new RegisterStateX86(*this));
988  }
989 
991  // Dynamic pointer casts
992 public:
996  RegisterStateX86Ptr retval = boost::dynamic_pointer_cast<RegisterStateX86>(from);
997  ASSERT_not_null(retval);
998  return retval;
999  }
1000 
1002  // Methods we inherited
1003 public:
1004  virtual void clear() ROSE_OVERRIDE;
1005  virtual void zero() ROSE_OVERRIDE;
1006  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) ROSE_OVERRIDE;
1007  virtual SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) ROSE_OVERRIDE;
1008  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops) ROSE_OVERRIDE;
1009  virtual void print(std::ostream&, Formatter&) const ROSE_OVERRIDE;
1010  virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops) ROSE_OVERRIDE;
1011 
1013  // Methods first declared at this level of the class hierarchy
1014 protected:
1015  // helpers for readRegister()
1016  virtual SValuePtr readRegisterGpr(RegisterDescriptor reg, RiscOperators *ops);
1017  virtual SValuePtr readRegisterFlag(RegisterDescriptor reg, RiscOperators *ops);
1018  virtual SValuePtr readRegisterSeg(RegisterDescriptor reg, RiscOperators *ops);
1019  virtual SValuePtr readRegisterIp(RegisterDescriptor reg, RiscOperators *ops);
1020  virtual SValuePtr readRegisterSt(RegisterDescriptor reg, RiscOperators *ops);
1021  virtual SValuePtr readRegisterXmm(RegisterDescriptor reg, RiscOperators *ops);
1022  virtual SValuePtr readRegisterFpStatus(RegisterDescriptor reg, RiscOperators *ops);
1023 
1024  // helpers for writeRegister()
1025  virtual void writeRegisterGpr(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops);
1026  virtual void writeRegisterFlag(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops);
1027  virtual void writeRegisterSeg(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops);
1028  virtual void writeRegisterIp(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops);
1029  virtual void writeRegisterSt(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops);
1030  virtual void writeRegisterXmm(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops);
1031  virtual void writeRegisterFpStatus(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops);
1032 
1033  // Generate a name for initial values.
1034  virtual std::string initialValueName(RegisterDescriptor) const;
1035 };
1036 
1037 
1038 
1040 // Memory State
1042 
1044 typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
1045 
1049 class MemoryState: public boost::enable_shared_from_this<MemoryState> {
1050  SValuePtr addrProtoval_;
1051  SValuePtr valProtoval_;
1052  ByteOrder::Endianness byteOrder_;
1053  MergerPtr merger_;
1054  bool byteRestricted_; // are cell values all exactly one byte wide?
1055 
1057  // Serialization
1058 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
1059 private:
1060  friend class boost::serialization::access;
1061 
1062  template<class S>
1063  void serialize(S &s, const unsigned /*version*/) {
1064  s & BOOST_SERIALIZATION_NVP(addrProtoval_);
1065  s & BOOST_SERIALIZATION_NVP(valProtoval_);
1066  s & BOOST_SERIALIZATION_NVP(byteOrder_);
1067  //s & merger_ -- not saved
1068  s & BOOST_SERIALIZATION_NVP(byteRestricted_);
1069  }
1070 #endif
1071 
1072 
1074  // Real constructors
1075 protected:
1076  MemoryState() // for serialization
1077  : byteOrder_(ByteOrder::ORDER_UNSPECIFIED), byteRestricted_(true) {}
1078 
1079  explicit MemoryState(const SValuePtr &addrProtoval, const SValuePtr &valProtoval)
1080  : addrProtoval_(addrProtoval), valProtoval_(valProtoval), byteOrder_(ByteOrder::ORDER_UNSPECIFIED),
1081  byteRestricted_(true) {
1082  ASSERT_not_null(addrProtoval);
1083  ASSERT_not_null(valProtoval);
1084  }
1085 
1086  MemoryState(const MemoryStatePtr &other)
1087  : addrProtoval_(other->addrProtoval_), valProtoval_(other->valProtoval_), byteOrder_(ByteOrder::ORDER_UNSPECIFIED),
1088  merger_(other->merger_), byteRestricted_(other->byteRestricted_) {}
1089 
1090 public:
1092  typedef MemoryStatePtr Ptr;
1093 
1094 public:
1095  virtual ~MemoryState() {}
1096 
1098  // Static allocating constructors. None needed since this class is abstract
1099 
1101  // Virtual constructors
1102 public:
1109  virtual MemoryStatePtr create(const SValuePtr &addrProtoval, const SValuePtr &valProtoval) const = 0;
1110 
1112  virtual MemoryStatePtr clone() const = 0;
1113 
1115  // Dynamic pointer casts. No-op since this is the base class.
1116 public:
1117  static MemoryStatePtr promote(const MemoryStatePtr &x) {
1118  ASSERT_not_null(x);
1119  return x;
1120  }
1121 
1123  // Methods first declared at this level of the class hierarchy
1124 public:
1133  MergerPtr merger() const { return merger_; }
1134  void merger(const MergerPtr &m) { merger_ = m; }
1139  SValuePtr get_addr_protoval() const { return addrProtoval_; }
1140 
1143  SValuePtr get_val_protoval() const { return valProtoval_; }
1144 
1146  virtual void clear() = 0;
1147 
1155  bool byteRestricted() const { return byteRestricted_; }
1156  void byteRestricted(bool b) { byteRestricted_ = b; }
1161  ByteOrder::Endianness get_byteOrder() const { return byteOrder_; }
1162  void set_byteOrder(ByteOrder::Endianness bo) { byteOrder_ = bo; }
1168  virtual bool merge(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps) = 0;
1169 
1189  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
1190  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1191 
1196  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
1197  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1198 
1208  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value,
1209  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1210 
1213  void print(std::ostream &stream, const std::string prefix="") const {
1214  Formatter fmt;
1215  fmt.set_line_prefix(prefix);
1216  print(stream, fmt);
1217  }
1218  virtual void print(std::ostream&, Formatter&) const = 0;
1223  MemoryStatePtr obj;
1224  Formatter &fmt;
1225  public:
1226  WithFormatter(const MemoryStatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1227  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1228  };
1229 
1237  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1240 };
1241 
1242 
1243 
1245 // State
1247 
1249 typedef boost::shared_ptr<class State> StatePtr;
1250 
1267 class State: public boost::enable_shared_from_this<State> {
1268  SValuePtr protoval_; // Initial value used to create additional values as needed.
1269  RegisterStatePtr registers_; // All machine register values for this semantic state.
1270  MemoryStatePtr memory_; // All memory for this semantic state.
1271 
1273  // Serialization
1274 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
1275 private:
1276  friend class boost::serialization::access;
1277 
1278  template<class S>
1279  void serialize(S &s, const unsigned /*version*/) {
1280  s & BOOST_SERIALIZATION_NVP(protoval_);
1281  s & BOOST_SERIALIZATION_NVP(registers_);
1282  s & BOOST_SERIALIZATION_NVP(memory_);
1283  }
1284 #endif
1285 
1286 
1288  // Real constructors
1289 protected:
1290  // needed for serialization
1291  State() {}
1292 
1293  State(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
1294  : registers_(registers), memory_(memory) {
1295  ASSERT_not_null(registers);
1296  ASSERT_not_null(memory);
1297  protoval_ = registers->protoval();
1298  ASSERT_not_null(protoval_);
1299  }
1300 
1301  // deep-copy the registers and memory
1302  State(const State &other)
1303  : boost::enable_shared_from_this<State>(other), protoval_(other.protoval_) {
1304  registers_ = other.registers_->clone();
1305  memory_ = other.memory_->clone();
1306  }
1307 
1308 public:
1310  typedef StatePtr Ptr;
1311 
1312 public:
1313  virtual ~State() {}
1314 
1316  // Static allocating constructors
1317 public:
1319  static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory) {
1320  return StatePtr(new State(registers, memory));
1321  }
1322 
1324  static StatePtr instance(const StatePtr &other) {
1325  return StatePtr(new State(*other));
1326  }
1327 
1329  // Virtual constructors
1330 public:
1332  virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const {
1333  return instance(registers, memory);
1334  }
1335 
1339  virtual StatePtr clone() const {
1340  StatePtr self = boost::const_pointer_cast<State>(shared_from_this());
1341  return instance(self);
1342  }
1343 
1345  // Dynamic pointer casts. No-op since this is the base class.
1346 public:
1347  static StatePtr promote(const StatePtr &x) {
1348  ASSERT_not_null(x);
1349  return x;
1350  }
1351 
1353  // Other methods that are part of our API. Most of these just chain to either the register state and/or the memory state.
1354 public:
1356  SValuePtr protoval() const { return protoval_; }
1357 
1358  // [Robb Matzke 2016-01-22]: deprecated
1359  SValuePtr get_protoval() const ROSE_DEPRECATED("use protoval instead") {
1360  return protoval();
1361  }
1362 
1364  virtual void clear();
1365 
1369  virtual void zero_registers();
1370 
1374  virtual void clear_memory();
1375 
1380  return registers_;
1381  }
1382 
1383  // [Robb Matzke 2016-01-22]: deprecated
1384  RegisterStatePtr get_register_state() ROSE_DEPRECATED("use registerState instead") {
1385  return registerState();
1386  }
1387 
1392  return memory_;
1393  }
1394 
1395  // [Robb Matzke 2016-01-22]: deprecated
1396  MemoryStatePtr get_memory_state() ROSE_DEPRECATED("use memoryState instead") {
1397  return memoryState();
1398  }
1399 
1404  virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
1405 
1410  virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
1411 
1416  virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
1417 
1422  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
1423  RiscOperators *addrOps, RiscOperators *valOps);
1424 
1429  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
1430  RiscOperators *addrOps, RiscOperators *valOps);
1431 
1436  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
1437 
1443  void printRegisters(std::ostream &stream, const std::string &prefix = "");
1444  virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
1447  // [Robb Matzke 2015-11-16]: deprecated
1448  void print_registers(std::ostream &stream, const std::string &prefix = "") ROSE_DEPRECATED("use printRegisters instead") {
1449  printRegisters(stream, prefix);
1450  }
1451 
1452  // [Robb Matzke 2015-11-16]: deprecated
1453  virtual void print_registers(std::ostream &stream, Formatter &fmt) const ROSE_DEPRECATED("use printRegisters instead") {
1454  printRegisters(stream, fmt);
1455  }
1456 
1462  void printMemory(std::ostream &stream, const std::string &prefix = "") const;
1463  virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
1466  // [Robb Matzke 2015-11-16]: deprecated
1467  void print_memory(std::ostream &stream, const std::string prefix = "") const ROSE_DEPRECATED("use printMemory instead") {
1468  printMemory(stream, prefix);
1469  }
1470 
1471  // [Robb Matzke 2015-11-16]: deprecated
1472  virtual void print_memory(std::ostream &stream, Formatter &fmt) const ROSE_DEPRECATED("use printMemory instead") {
1473  printMemory(stream, fmt);
1474  }
1475 
1478  void print(std::ostream &stream, const std::string &prefix = "") const;
1479  virtual void print(std::ostream&, Formatter&) const;
1484  StatePtr obj;
1485  Formatter &fmt;
1486  public:
1487  WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1488  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1489  };
1490 
1498  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1507  virtual bool merge(const StatePtr &other, RiscOperators *ops);
1508 };
1509 
1510 
1511 
1513 // RISC Operators
1515 
1517 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
1518 
1539 class RiscOperators: public boost::enable_shared_from_this<RiscOperators> {
1540  SValuePtr protoval_; // Prototypical value used for its virtual constructors
1541  StatePtr currentState_; // State upon which RISC operators operate
1542  StatePtr initialState_; // Lazily updated initial state; see readMemory
1543  SmtSolverPtr solver_; // Optional SMT solver
1544  SgAsmInstruction *currentInsn_; // Current instruction, as set by latest startInstruction call
1545  size_t nInsns_; // Number of instructions processed
1546  std::string name_; // Name to use for debugging
1547 
1549  // Serialization
1550 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
1551 private:
1552  friend class boost::serialization::access;
1553 
1554  template<class S>
1555  void serialize(S &s, const unsigned /*version*/) {
1556  s & BOOST_SERIALIZATION_NVP(protoval_);
1557  s & BOOST_SERIALIZATION_NVP(currentState_);
1558  s & BOOST_SERIALIZATION_NVP(initialState_);
1559  s & BOOST_SERIALIZATION_NVP(solver_);
1560  s & BOOST_SERIALIZATION_NVP(currentInsn_);
1561  s & BOOST_SERIALIZATION_NVP(nInsns_);
1562  s & BOOST_SERIALIZATION_NVP(name_);
1563  }
1564 #endif
1565 
1567  // Real constructors
1568 protected:
1569  // for serialization
1570  RiscOperators()
1571  : currentInsn_(NULL), nInsns_(0) {}
1572 
1573  explicit RiscOperators(const SValuePtr &protoval, const SmtSolverPtr &solver = SmtSolverPtr())
1574  : protoval_(protoval), solver_(solver), currentInsn_(NULL), nInsns_(0) {
1575  ASSERT_not_null(protoval_);
1576  }
1577 
1578  explicit RiscOperators(const StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr())
1579  : currentState_(state), solver_(solver), currentInsn_(NULL), nInsns_(0) {
1580  ASSERT_not_null(state);
1581  protoval_ = state->protoval();
1582  }
1583 
1584 public:
1587 
1588 public:
1589  virtual ~RiscOperators() {}
1590 
1592  // Static allocating constructors. None needed since this class is abstract.
1593 
1594 
1596  // Virtual constructors.
1597 public:
1601  virtual RiscOperatorsPtr create(const SValuePtr &protoval, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
1602 
1607  virtual RiscOperatorsPtr create(const StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
1608 
1610  // Dynamic pointer casts. No-op since this is the base class.
1611 public:
1612  static RiscOperatorsPtr promote(const RiscOperatorsPtr &x) {
1613  ASSERT_not_null(x);
1614  return x;
1615  }
1616 
1618  // Other methods part of our API
1619 public:
1623  virtual SValuePtr protoval() const { return protoval_; }
1624 
1625  // [Robb Matzke 2016-01-22]: deprecated
1626  virtual SValuePtr get_protoval() const ROSE_DEPRECATED("use protoval instead") {
1627  return protoval();
1628  }
1629 
1638  virtual SmtSolverPtr solver() const { return solver_; }
1639  virtual void solver(const SmtSolverPtr &s) { solver_ = s; }
1653  virtual StatePtr currentState() const { return currentState_; }
1654  virtual void currentState(const StatePtr &s) { currentState_ = s; }
1657  // [Robb Matzke 2016-01-22]: deprecated
1658  virtual StatePtr get_state() const ROSE_DEPRECATED("use currentState instead") {
1659  return currentState();
1660  }
1661 
1662  // [Robb Matzke 2016-01-22]: deprecated
1663  virtual void set_state(const StatePtr &s) ROSE_DEPRECATED("use currentState instead") {
1664  currentState(s);
1665  }
1666 
1704  virtual StatePtr initialState() const { return initialState_; }
1705  virtual void initialState(const StatePtr &s) { initialState_ = s; }
1713  virtual const std::string& name() const { return name_; }
1714  virtual void name(const std::string &s) { name_ = s; }
1717  // [Robb Matzke 2016-01-22]: deprecated
1718  virtual const std::string& get_name() const ROSE_DEPRECATED("use name instead") { return name(); }
1719  virtual void set_name(const std::string &s) ROSE_DEPRECATED("use name instead") { name(s); }
1720 
1723  void print(std::ostream &stream, const std::string prefix="") const {
1724  Formatter fmt;
1725  fmt.set_line_prefix(prefix);
1726  print(stream, fmt);
1727  }
1728  virtual void print(std::ostream &stream, Formatter &fmt) const {
1729  currentState_->print(stream, fmt);
1730  }
1735  RiscOperatorsPtr obj;
1736  Formatter &fmt;
1737  public:
1738  WithFormatter(const RiscOperatorsPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1739  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1740  };
1741 
1749  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1758  virtual size_t nInsns() const { return nInsns_; }
1759  virtual void nInsns(size_t n) { nInsns_ = n; }
1762  // [Robb Matzke 2016-01-22]: deprecated
1763  virtual size_t get_ninsns() const ROSE_DEPRECATED("use nInsns instead") { return nInsns(); }
1764  virtual void set_ninsns(size_t n) ROSE_DEPRECATED("use nInsns instead") { nInsns(n); }
1765 
1771  return currentInsn_;
1772  }
1773 
1774  // [Robb Matzke 2016-01-22]: deprecated
1775  virtual SgAsmInstruction *get_insn() const ROSE_DEPRECATED("use currentInstruction instead") {
1776  return currentInstruction();
1777  }
1778 
1781  virtual void startInstruction(SgAsmInstruction *insn);
1782 
1785  virtual void finishInstruction(SgAsmInstruction *insn) {
1786  ASSERT_not_null(insn);
1787  ASSERT_require(currentInsn_==insn);
1788  currentInsn_ = NULL;
1789  };
1790 
1791 
1793  // Value Construction Operations
1795  // The trailing underscores are necessary for for undefined_() on some machines, so we just add one to the end of all the
1796  // virtual constructors for consistency.
1797 
1799  virtual SValuePtr undefined_(size_t nbits) {
1800  return protoval_->undefined_(nbits);
1801  }
1802  virtual SValuePtr unspecified_(size_t nbits) {
1803  return protoval_->unspecified_(nbits);
1804  }
1805 
1807  virtual SValuePtr number_(size_t nbits, uint64_t value) {
1808  return protoval_->number_(nbits, value);
1809  }
1810 
1812  virtual SValuePtr boolean_(bool value) {
1813  return protoval_->boolean_(value);
1814  }
1815 
1817  virtual SValuePtr bottom_(size_t nbits) {
1818  return protoval_->bottom_(nbits);
1819  }
1820 
1821 
1823  // x86-specific Operations (FIXME)
1825 
1829  return a->copy();
1830  }
1831 
1836  return a->copy();
1837  }
1838 
1843  return a->copy();
1844  }
1845 
1847  virtual void hlt() {}
1848 
1850  virtual void cpuid() {}
1851 
1853  virtual SValuePtr rdtsc() { return unspecified_(64); }
1854 
1855 
1857  // Boolean Operations
1859 
1862  virtual SValuePtr and_(const SValuePtr &a, const SValuePtr &b) = 0;
1863 
1866  virtual SValuePtr or_(const SValuePtr &a, const SValuePtr &b) = 0;
1867 
1870  virtual SValuePtr xor_(const SValuePtr &a, const SValuePtr &b) = 0;
1871 
1873  virtual SValuePtr invert(const SValuePtr &a) = 0;
1874 
1878  virtual SValuePtr extract(const SValuePtr &a, size_t begin_bit, size_t end_bit) = 0;
1879 
1883  virtual SValuePtr concat(const SValuePtr &a, const SValuePtr &b) = 0;
1884 
1887  virtual SValuePtr leastSignificantSetBit(const SValuePtr &a) = 0;
1888 
1891  virtual SValuePtr mostSignificantSetBit(const SValuePtr &a) = 0;
1892 
1896  virtual SValuePtr rotateLeft(const SValuePtr &a, const SValuePtr &nbits) = 0;
1897 
1901  virtual SValuePtr rotateRight(const SValuePtr &a, const SValuePtr &nbits) = 0;
1902 
1906  virtual SValuePtr shiftLeft(const SValuePtr &a, const SValuePtr &nbits) = 0;
1907 
1911  virtual SValuePtr shiftRight(const SValuePtr &a, const SValuePtr &nbits) = 0;
1912 
1917  virtual SValuePtr shiftRightArithmetic(const SValuePtr &a, const SValuePtr &nbits) = 0;
1918 
1919 
1921  // Comparison Operations
1923 
1926  virtual SValuePtr equalToZero(const SValuePtr &a) = 0;
1927 
1931  virtual SValuePtr ite(const SValuePtr &cond, const SValuePtr &a, const SValuePtr &b) = 0;
1932 
1939  SValuePtr equal(const SValuePtr &a, const SValuePtr &b) ROSE_DEPRECATED("use isEqual instead");
1940  virtual SValuePtr isEqual(const SValuePtr &a, const SValuePtr &b);
1941  virtual SValuePtr isNotEqual(const SValuePtr &a, const SValuePtr &b);
1951  virtual SValuePtr isUnsignedLessThan(const SValuePtr &a, const SValuePtr &b);
1952  virtual SValuePtr isUnsignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1953  virtual SValuePtr isUnsignedGreaterThan(const SValuePtr &a, const SValuePtr &b);
1954  virtual SValuePtr isUnsignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1964  virtual SValuePtr isSignedLessThan(const SValuePtr &a, const SValuePtr &b);
1965  virtual SValuePtr isSignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1966  virtual SValuePtr isSignedGreaterThan(const SValuePtr &a, const SValuePtr &b);
1967  virtual SValuePtr isSignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1970  // Integer Arithmetic Operations
1973 
1976  virtual SValuePtr unsignedExtend(const SValuePtr &a, size_t new_width) {
1977  return a->copy(new_width);
1978  }
1979 
1982  virtual SValuePtr signExtend(const SValuePtr &a, size_t new_width) = 0;
1983 
1986  virtual SValuePtr add(const SValuePtr &a, const SValuePtr &b) = 0;
1987 
1990  virtual SValuePtr subtract(const SValuePtr &minuend, const SValuePtr &subtrahend);
1991 
2008  virtual SValuePtr addWithCarries(const SValuePtr &a, const SValuePtr &b, const SValuePtr &c,
2009  SValuePtr &carry_out/*output*/) = 0;
2010 
2012  virtual SValuePtr negate(const SValuePtr &a) = 0;
2013 
2015  virtual SValuePtr signedDivide(const SValuePtr &a, const SValuePtr &b) = 0;
2016 
2018  virtual SValuePtr signedModulo(const SValuePtr &a, const SValuePtr &b) = 0;
2019 
2021  virtual SValuePtr signedMultiply(const SValuePtr &a, const SValuePtr &b) = 0;
2022 
2024  virtual SValuePtr unsignedDivide(const SValuePtr &a, const SValuePtr &b) = 0;
2025 
2027  virtual SValuePtr unsignedModulo(const SValuePtr &a, const SValuePtr &b) = 0;
2028 
2030  virtual SValuePtr unsignedMultiply(const SValuePtr &a, const SValuePtr &b) = 0;
2031 
2032 
2034  // Interrupt and system calls
2036 
2041  virtual void interrupt(int /*majr*/, int /*minr*/) {}
2042 
2043 
2045  // Floating-point operations
2046  //
2047  // For now these all have default implementations that throw NotImplemented, but we might change them to pure virtual
2048  // sometime in the future so they're consistent with most other RISC operators. [Robb P. Matzke 2015-08-03]
2050 
2052  virtual SValuePtr fpFromInteger(const SValuePtr &intValue, SgAsmFloatType *fpType);
2053 
2059  virtual SValuePtr fpToInteger(const SValuePtr &fpValue, SgAsmFloatType *fpType, const SValuePtr &dflt);
2060 
2064  virtual SValuePtr fpConvert(const SValuePtr &a, SgAsmFloatType *aType, SgAsmFloatType *retType);
2065 
2067  virtual SValuePtr fpIsNan(const SValuePtr &fpValue, SgAsmFloatType *fpType);
2068 
2070  virtual SValuePtr fpIsDenormalized(const SValuePtr &fpValue, SgAsmFloatType *fpType);
2071 
2073  virtual SValuePtr fpIsZero(const SValuePtr &fpValue, SgAsmFloatType *fpType);
2074 
2079  virtual SValuePtr fpIsInfinity(const SValuePtr &fpValue, SgAsmFloatType *fpType);
2080 
2084  virtual SValuePtr fpSign(const SValuePtr &fpValue, SgAsmFloatType *fpType);
2085 
2091  virtual SValuePtr fpEffectiveExponent(const SValuePtr &fpValue, SgAsmFloatType *fpType);
2092 
2096  virtual SValuePtr fpAdd(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
2097 
2102  virtual SValuePtr fpSubtract(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
2103 
2107  virtual SValuePtr fpMultiply(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
2108 
2112  virtual SValuePtr fpDivide(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
2113 
2117  virtual SValuePtr fpSquareRoot(const SValuePtr &a, SgAsmFloatType *fpType);
2118 
2122  virtual SValuePtr fpRoundTowardZero(const SValuePtr &a, SgAsmFloatType *fpType);
2123 
2124 
2126  // State Accessing Operations
2128 
2153  virtual SValuePtr readRegister(RegisterDescriptor reg) { // old subclasses can still override this if they want,
2154  return readRegister(reg, undefined_(reg.get_nbits())); // but new subclasses should not override this method.
2155  }
2156  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt); // new subclasses override this
2168  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &a) {
2169  ASSERT_not_null(currentState_);
2170  currentState_->writeRegister(reg, a, this);
2171  }
2172 
2178  virtual SValuePtr peekRegister(RegisterDescriptor, const SValuePtr &dflt);
2179 
2202  virtual SValuePtr readMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt,
2203  const SValuePtr &cond) = 0;
2204 
2215  virtual void writeMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &data,
2216  const SValuePtr &cond) = 0;
2217 
2222  virtual SValuePtr peekMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt) = 0;
2223 };
2224 
2225 
2226 
2228 // Instruction Dispatcher
2230 
2232 typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
2233 
2236 public:
2237  virtual ~InsnProcessor() {}
2238  virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
2239 };
2240 
2254 class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
2255 protected:
2256  RiscOperatorsPtr operators;
2258  size_t addrWidth_;
2261  // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
2262  // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iproc_key() virtual method.
2263  typedef std::vector<InsnProcessor*> InsnProcessors;
2264  InsnProcessors iproc_table;
2265 
2266 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
2267 private:
2268  friend class boost::serialization::access;
2269 
2270  template<class S>
2271  void serialize(S &s, const unsigned /*version*/) {
2272  s & BOOST_SERIALIZATION_NVP(operators);
2273  s & BOOST_SERIALIZATION_NVP(regdict);
2274  s & BOOST_SERIALIZATION_NVP(addrWidth_);
2275  s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
2276  //s & iproc_table; -- not saved
2277  }
2278 #endif
2279 
2281  // Real constructors
2282 protected:
2283  // Prototypical constructor
2284  Dispatcher(): regdict(NULL), addrWidth_(0), autoResetInstructionPointer_(true) {}
2285 
2286  // Prototypical constructor
2287  Dispatcher(size_t addrWidth, const RegisterDictionary *regs)
2288  : regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {}
2289 
2290  Dispatcher(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionary *regs)
2291  : operators(ops), regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {
2292  ASSERT_not_null(operators);
2293  ASSERT_not_null(regs);
2294  }
2295 
2296 public:
2299 
2300 public:
2301  virtual ~Dispatcher() {
2302  for (InsnProcessors::iterator iter=iproc_table.begin(); iter!=iproc_table.end(); ++iter)
2303  delete *iter;
2304  }
2305 
2307  // Static allocating constructors. None since this is an abstract class
2308 
2309 
2311  // Virtual constructors
2312 public:
2314  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const = 0;
2315 
2317  // Methods to process instructions
2318 public:
2320  virtual void processInstruction(SgAsmInstruction *insn);
2321 
2323  // Instruction processor table operations
2324 public:
2328  virtual InsnProcessor *iproc_lookup(SgAsmInstruction *insn);
2329 
2333  virtual void iproc_replace(SgAsmInstruction *insn, InsnProcessor *iproc);
2334 
2336  virtual int iproc_key(SgAsmInstruction*) const = 0;
2337 
2341  virtual void iproc_set(int key, InsnProcessor *iproc);
2342 
2344  virtual InsnProcessor *iproc_get(int key);
2345 
2347  // Convenience methods that defer the call to some member object
2348 public:
2350  virtual RiscOperatorsPtr get_operators() const { return operators; }
2351 
2354  virtual StatePtr currentState() const { return operators ? operators->currentState() : StatePtr(); }
2355 
2356  // [Robb Matzke 2016-01-22]: deprecated
2357  virtual StatePtr get_state() const ROSE_DEPRECATED("use currentState instead") {
2358  return currentState();
2359  }
2360 
2362  virtual SValuePtr protoval() const { return operators ? operators->protoval() : SValuePtr(); }
2363 
2364  // [Robb Matzke 2016-01-22]: deprecated
2365  virtual SValuePtr get_protoval() const ROSE_DEPRECATED("use protoval instead") {
2366  return protoval();
2367  }
2368 
2374  return operators ? operators->currentInstruction() : NULL;
2375  }
2376 
2377  // [Robb Matzke 2016-01-22]: deprecated
2378  virtual SgAsmInstruction *get_insn() const ROSE_DEPRECATED("use currentInstruction instead") {
2379  return currentInstruction();
2380  }
2381 
2383  virtual SValuePtr undefined_(size_t nbits) const {
2384  ASSERT_not_null(operators);
2385  return operators->undefined_(nbits);
2386  }
2387  virtual SValuePtr unspecified_(size_t nbits) const {
2388  ASSERT_not_null(operators);
2389  return operators->unspecified_(nbits);
2390  }
2391 
2393  virtual SValuePtr number_(size_t nbits, uint64_t number) const {
2394  ASSERT_not_null(operators);
2395  return operators->number_(nbits, number);
2396  }
2397 
2399  // Methods related to registers
2400 public:
2413  return regdict;
2414  }
2416  this->regdict = regdict;
2417  }
2424  virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
2425 
2432  size_t addressWidth() const { return addrWidth_; }
2433  void addressWidth(size_t nbits);
2437  virtual RegisterDescriptor instructionPointerRegister() const = 0;
2438 
2440  virtual RegisterDescriptor stackPointerRegister() const = 0;
2441 
2448  bool autoResetInstructionPointer() const { return autoResetInstructionPointer_; }
2449  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
2453  // Miscellaneous methods that tend to be the same for most dispatchers
2455 public:
2456 
2465  virtual void advanceInstructionPointer(SgAsmInstruction*);
2466 
2470  virtual RegisterDescriptor segmentRegister(SgAsmMemoryReferenceExpression*);
2471 
2475  virtual void incrementRegisters(SgAsmExpression*);
2476 
2480  virtual void decrementRegisters(SgAsmExpression*);
2481 
2485  virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
2486 
2492  virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
2493 
2497  virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
2498 };
2499 
2500 
2501 
2503 // Printing
2505 
2506 std::ostream& operator<<(std::ostream&, const Exception&);
2507 std::ostream& operator<<(std::ostream&, const SValue&);
2508 std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
2509 std::ostream& operator<<(std::ostream&, const MemoryState&);
2510 std::ostream& operator<<(std::ostream&, const MemoryState::WithFormatter&);
2511 std::ostream& operator<<(std::ostream&, const RegisterState&);
2512 std::ostream& operator<<(std::ostream&, const RegisterState::WithFormatter&);
2513 std::ostream& operator<<(std::ostream&, const State&);
2514 std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
2515 std::ostream& operator<<(std::ostream&, const RiscOperators&);
2516 std::ostream& operator<<(std::ostream&, const RiscOperators::WithFormatter&);
2517 
2518 } // namespace
2519 } // namespace
2520 } // namespace
2521 } // namespace
2522 
2523 #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.
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:28
virtual SgAsmInstruction * currentInstruction() const
Returns current instruction.
virtual RegisterStatePtr clone() const =0
Make a copy of this register state.
The location was written on behalf of an instruction.
virtual SValuePtr filterIndirectJumpTarget(const SValuePtr &a)
Invoked to filter indirect jumps.
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.
MemoryStatePtr memoryState() const
Property: Memory state.
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.
virtual void zero() ROSE_OVERRIDE
Set all registers to the zero.
WithFormatter operator+(Formatter &fmt)
Used for printing memory states with formatting.
virtual void set_width(size_t nbits)
Accessor for value width.
virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) ROSE_OVERRIDE
Read a value from a register.
WithFormatter with_format(Formatter &fmt)
Used for printing values with formatting.
void clear_suppress_initial_values()
Whether register initial values should be suppressed.
virtual void print(std::ostream &, Formatter &) const ROSE_OVERRIDE
Print the register contents.
bool isFalse() const
Returns true if concrete zero.
Base class for machine instructions.
Collection of streams.
Definition: Message.h:1579
virtual SValuePtr unspecified_(size_t nbits) const =0
Create a new unspecified semantic value.
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.
RegisterStatePtr Ptr
Shared-ownership pointer for a RegisterState object.
virtual bool is_number() const =0
Determines if the value is a concrete number.
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.
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.
SValuePtr Ptr
Shared-ownership pointer for an SValue object.
WithFormatter operator+(Formatter &fmt)
Used for printing values with formatting.
MemoryStatePtr Ptr
Shared-ownership pointer for a MemoryState.
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 bool must_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Returns true if two values must be equal.
Small object support.
Definition: SmallObject.h:19
virtual void cpuid()
Invoked for the x86 CPUID instruction.
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.
void print(std::ostream &stream, const std::string prefix="") const
Print the register contents.
Describes (part of) a physical CPU register.
void byteRestricted(bool b)
Indicates whether memory cell values are required to be eight bits wide.
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.
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 SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) ROSE_OVERRIDE
Read a register without side effects.
static const size_t n_st
Number of ST registers (not counting _st_top pseudo 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 SValuePtr boolean_(bool value)
Returns a Boolean value.
virtual size_t nInsns() const
Property: Number of instructions processed.
static const size_t n_segregs
Number of segmentation registers in this state.
virtual StatePtr currentState() const
Property: Current semantic state.
Sawyer::SharedPointer< class SmtSolver > SmtSolverPtr
Reference-counting pointer for SMT solvers.
virtual void clear() ROSE_OVERRIDE
Removes stored values from the register state.
SValuePtr get_addr_protoval() const
Return the address protoval.
boost::shared_ptr< class RegisterState > RegisterStatePtr
Shared-ownership pointer to a register state.
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 void hlt()
Invoked for the x86 HLT instruction.
Reference to memory locations.
boost::shared_ptr< class Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
static RegisterStateX86Ptr instance(const SValuePtr &protoval, const RegisterDictionary *regdict)
Instantiate a new register state.
static StatePtr instance(const StatePtr &other)
Instantiate a new copy of an existing state.
virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const =0
Virtual constructor.
The set of all registers and their values for a 32-bit x86 architecture.
virtual SgAsmInstruction * currentInstruction() const
Returns the instruction that is being processed.
virtual SValuePtr filterReturnTarget(const SValuePtr &a)
Invoked to filter return targets.
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.
WithFormatter with_format(Formatter &fmt)
Used for printing register states with formatting.
void clear_show_properties()
Whether to show register properties.
WithFormatter with_format(Formatter &fmt)
Used for printing states with formatting.
virtual SValuePtr undefined_(size_t nbits) const =0
Create a new undefined semantic value.
static const size_t n_flags
Number of flag registers in this state.
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.
Functor that knows how to dispatch a single kind of instruction.
bool memoryAddressesMayAlias() const
Whether memory addresses can alias one another.
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 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 StatePtr initialState() const
Property: Optional lazily updated initial state.
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 number_(size_t nbits, uint64_t number) const =0
Create a new concrete semantic value.
static const size_t n_gprs
Number of general-purpose registers in this state.
const RegisterDictionary * regdict
Registers that are able to be stored by this state.
void set_register_dictionary(RegisterDictionary *rd)
The register dictionary which is used for printing register names.
virtual void initialState(const StatePtr &s)
Property: Optional lazily updated initial state.
static RegisterStateX86Ptr promote(const RegisterStatePtr &from)
Run-time promotion of a base register state pointer to a RegisterStateX86 pointer.
virtual bool isBottom() const =0
Determines whether a value is a data-flow bottom.
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.
void memoryAddressesMayAlias(bool b)
Whether memory addresses can alias one another.
static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
Instantiate a new state object with specified register and memory states.
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:22
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 bool may_equal(const SValuePtr &other, const SmtSolverPtr &solver=SmtSolverPtr()) const =0
Returns true if two values could be equal.
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 bottom_(size_t nBits) const =0
Data-flow bottom value.
virtual void name(const std::string &s)
Property: Name used for debugging.
virtual size_t get_width() const
Accessor for value width.
virtual SValuePtr bottom_(size_t nbits)
Returns a data-flow bottom value.
WithFormatter with_format(Formatter &fmt)
Used for printing RISC operators with formatting.
SValuePtr get_val_protoval() const
Return the value protoval.
virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &a)
Writes a value to a register.
virtual RegisterStatePtr clone() const ROSE_OVERRIDE
Make a copy of this register state.
virtual RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const ROSE_OVERRIDE
Virtual constructor.
void set_byteOrder(ByteOrder::Endianness bo)
Memory byte order.
SValuePtr flag[n_flags]
Control/status flags (i.e., FLAG register).
void memoryMergeDebugging(bool b)
Turn on output for memory merge debugging.
bool get_show_properties() const
Whether to show register properties.
bool autoResetInstructionPointer_
Reset instruction pointer register for each instruction.
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
RegisterDictionary * get_register_dictionary() const
The register dictionary which is used for printing register names.
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 const std::string & name() const
Property: Name used for debugging.
virtual SValuePtr readRegister(RegisterDescriptor reg)
Reads a value from a register.
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.
bool byteRestricted() const
Indicates whether memory cell values are required to be eight bits wide.
Base class for exceptions thrown by instruction semantics.
virtual uint64_t get_number() const =0
Return the concrete number for this value.
virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops) ROSE_OVERRIDE
Merge register states for data flow analysis.
virtual SValuePtr number_(size_t nbits, uint64_t number) const
Return a semantic value representing a number.
static RegisterStateX86Ptr instance(const RegisterStateX86Ptr &other)
Instantiate a new copy of an existing register state.
virtual SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops)=0
Read a register without side effects.
Floating point types.
virtual void print(std::ostream &stream, Formatter &fmt) const
Print multi-line output for this object.
The location was read on behalf of an instruction.
virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops) ROSE_OVERRIDE
Write a value to a register.
StatePtr Ptr
Shared-ownership pointer for a State.
Adjusts a Formatter for one additional level of indentation.
boost::shared_ptr< class RegisterStateX86 > RegisterStateX86Ptr
Shared-ownership pointer to an x86 register state.