ROSE  0.9.10.91
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:
774  typedef RegisterStatePtr Ptr;
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 
831  void set_register_dictionary(const RegisterDictionary *rd) { regdict = rd; }
844  virtual void clear() = 0;
845 
847  virtual void zero() = 0;
848 
852  virtual bool merge(const RegisterStatePtr &other, RiscOperators *ops) = 0;
853 
867  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) = 0;
868 
872  virtual SValuePtr peekRegister(RegisterDescriptor reg, const SValuePtr &dflt, RiscOperators *ops) = 0;
873 
880  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &value, RiscOperators *ops) = 0;
881 
884  void print(std::ostream &stream, const std::string prefix="") const {
885  Formatter fmt;
886  fmt.set_line_prefix(prefix);
887  print(stream, fmt);
888  }
889  virtual void print(std::ostream&, Formatter&) const = 0;
894  RegisterStatePtr obj;
895  Formatter &fmt;
896  public:
897  WithFormatter(const RegisterStatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
898  void print(std::ostream &stream) const { obj->print(stream, fmt); }
899  };
900 
908  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
912 };
913 
914 
916 // Memory State
918 
920 typedef boost::shared_ptr<class MemoryState> MemoryStatePtr;
921 
925 class MemoryState: public boost::enable_shared_from_this<MemoryState> {
926  SValuePtr addrProtoval_;
927  SValuePtr valProtoval_;
928  ByteOrder::Endianness byteOrder_;
929  MergerPtr merger_;
930  bool byteRestricted_; // are cell values all exactly one byte wide?
931 
933  // Serialization
934 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
935 private:
936  friend class boost::serialization::access;
937 
938  template<class S>
939  void serialize(S &s, const unsigned /*version*/) {
940  s & BOOST_SERIALIZATION_NVP(addrProtoval_);
941  s & BOOST_SERIALIZATION_NVP(valProtoval_);
942  s & BOOST_SERIALIZATION_NVP(byteOrder_);
943  //s & merger_ -- not saved
944  s & BOOST_SERIALIZATION_NVP(byteRestricted_);
945  }
946 #endif
947 
948 
950  // Real constructors
951 protected:
952  MemoryState() // for serialization
953  : byteOrder_(ByteOrder::ORDER_UNSPECIFIED), byteRestricted_(true) {}
954 
955  explicit MemoryState(const SValuePtr &addrProtoval, const SValuePtr &valProtoval)
956  : addrProtoval_(addrProtoval), valProtoval_(valProtoval), byteOrder_(ByteOrder::ORDER_UNSPECIFIED),
957  byteRestricted_(true) {
958  ASSERT_not_null(addrProtoval);
959  ASSERT_not_null(valProtoval);
960  }
961 
962  MemoryState(const MemoryStatePtr &other)
963  : addrProtoval_(other->addrProtoval_), valProtoval_(other->valProtoval_), byteOrder_(ByteOrder::ORDER_UNSPECIFIED),
964  merger_(other->merger_), byteRestricted_(other->byteRestricted_) {}
965 
966 public:
968  typedef MemoryStatePtr Ptr;
969 
970 public:
971  virtual ~MemoryState() {}
972 
974  // Static allocating constructors. None needed since this class is abstract
975 
977  // Virtual constructors
978 public:
985  virtual MemoryStatePtr create(const SValuePtr &addrProtoval, const SValuePtr &valProtoval) const = 0;
986 
988  virtual MemoryStatePtr clone() const = 0;
989 
991  // Dynamic pointer casts. No-op since this is the base class.
992 public:
993  static MemoryStatePtr promote(const MemoryStatePtr &x) {
994  ASSERT_not_null(x);
995  return x;
996  }
997 
999  // Methods first declared at this level of the class hierarchy
1000 public:
1009  MergerPtr merger() const { return merger_; }
1010  void merger(const MergerPtr &m) { merger_ = m; }
1015  SValuePtr get_addr_protoval() const { return addrProtoval_; }
1016 
1019  SValuePtr get_val_protoval() const { return valProtoval_; }
1020 
1022  virtual void clear() = 0;
1023 
1031  bool byteRestricted() const { return byteRestricted_; }
1032  void byteRestricted(bool b) { byteRestricted_ = b; }
1037  ByteOrder::Endianness get_byteOrder() const { return byteOrder_; }
1038  void set_byteOrder(ByteOrder::Endianness bo) { byteOrder_ = bo; }
1044  virtual bool merge(const MemoryStatePtr &other, RiscOperators *addrOps, RiscOperators *valOps) = 0;
1045 
1065  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
1066  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1067 
1072  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
1073  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1074 
1084  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value,
1085  RiscOperators *addrOps, RiscOperators *valOps) = 0;
1086 
1089  void print(std::ostream &stream, const std::string prefix="") const {
1090  Formatter fmt;
1091  fmt.set_line_prefix(prefix);
1092  print(stream, fmt);
1093  }
1094  virtual void print(std::ostream&, Formatter&) const = 0;
1099  MemoryStatePtr obj;
1100  Formatter &fmt;
1101  public:
1102  WithFormatter(const MemoryStatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1103  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1104  };
1105 
1113  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1116 };
1117 
1118 
1119 
1121 // State
1123 
1125 typedef boost::shared_ptr<class State> StatePtr;
1126 
1143 class State: public boost::enable_shared_from_this<State> {
1144  SValuePtr protoval_; // Initial value used to create additional values as needed.
1145  RegisterStatePtr registers_; // All machine register values for this semantic state.
1146  MemoryStatePtr memory_; // All memory for this semantic state.
1147 
1149  // Serialization
1150 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
1151 private:
1152  friend class boost::serialization::access;
1153 
1154  template<class S>
1155  void serialize(S &s, const unsigned /*version*/) {
1156  s & BOOST_SERIALIZATION_NVP(protoval_);
1157  s & BOOST_SERIALIZATION_NVP(registers_);
1158  s & BOOST_SERIALIZATION_NVP(memory_);
1159  }
1160 #endif
1161 
1162 
1164  // Real constructors
1165 protected:
1166  // needed for serialization
1167  State() {}
1168 
1169  State(const RegisterStatePtr &registers, const MemoryStatePtr &memory)
1170  : registers_(registers), memory_(memory) {
1171  ASSERT_not_null(registers);
1172  ASSERT_not_null(memory);
1173  protoval_ = registers->protoval();
1174  ASSERT_not_null(protoval_);
1175  }
1176 
1177  // deep-copy the registers and memory
1178  State(const State &other)
1179  : boost::enable_shared_from_this<State>(other), protoval_(other.protoval_) {
1180  registers_ = other.registers_->clone();
1181  memory_ = other.memory_->clone();
1182  }
1183 
1184 public:
1186  typedef StatePtr Ptr;
1187 
1188 public:
1189  virtual ~State() {}
1190 
1192  // Static allocating constructors
1193 public:
1195  static StatePtr instance(const RegisterStatePtr &registers, const MemoryStatePtr &memory) {
1196  return StatePtr(new State(registers, memory));
1197  }
1198 
1200  static StatePtr instance(const StatePtr &other) {
1201  return StatePtr(new State(*other));
1202  }
1203 
1205  // Virtual constructors
1206 public:
1208  virtual StatePtr create(const RegisterStatePtr &registers, const MemoryStatePtr &memory) const {
1209  return instance(registers, memory);
1210  }
1211 
1215  virtual StatePtr clone() const {
1216  StatePtr self = boost::const_pointer_cast<State>(shared_from_this());
1217  return instance(self);
1218  }
1219 
1221  // Dynamic pointer casts. No-op since this is the base class.
1222 public:
1223  static StatePtr promote(const StatePtr &x) {
1224  ASSERT_not_null(x);
1225  return x;
1226  }
1227 
1229  // Other methods that are part of our API. Most of these just chain to either the register state and/or the memory state.
1230 public:
1232  SValuePtr protoval() const { return protoval_; }
1233 
1234  // [Robb Matzke 2016-01-22]: deprecated
1235  SValuePtr get_protoval() const ROSE_DEPRECATED("use protoval instead") {
1236  return protoval();
1237  }
1238 
1240  virtual void clear();
1241 
1245  virtual void zero_registers();
1246 
1250  virtual void clear_memory();
1251 
1255  RegisterStatePtr registerState() const {
1256  return registers_;
1257  }
1258 
1259  // [Robb Matzke 2016-01-22]: deprecated
1260  RegisterStatePtr get_register_state() ROSE_DEPRECATED("use registerState instead") {
1261  return registerState();
1262  }
1263 
1267  MemoryStatePtr memoryState() const {
1268  return memory_;
1269  }
1270 
1271  // [Robb Matzke 2016-01-22]: deprecated
1272  MemoryStatePtr get_memory_state() ROSE_DEPRECATED("use memoryState instead") {
1273  return memoryState();
1274  }
1275 
1280  virtual SValuePtr readRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
1281 
1286  virtual SValuePtr peekRegister(RegisterDescriptor desc, const SValuePtr &dflt, RiscOperators *ops);
1287 
1292  virtual void writeRegister(RegisterDescriptor desc, const SValuePtr &value, RiscOperators *ops);
1293 
1298  virtual SValuePtr readMemory(const SValuePtr &address, const SValuePtr &dflt,
1299  RiscOperators *addrOps, RiscOperators *valOps);
1300 
1305  virtual SValuePtr peekMemory(const SValuePtr &address, const SValuePtr &dflt,
1306  RiscOperators *addrOps, RiscOperators *valOps);
1307 
1312  virtual void writeMemory(const SValuePtr &addr, const SValuePtr &value, RiscOperators *addrOps, RiscOperators *valOps);
1313 
1319  void printRegisters(std::ostream &stream, const std::string &prefix = "");
1320  virtual void printRegisters(std::ostream &stream, Formatter &fmt) const;
1323  // [Robb Matzke 2015-11-16]: deprecated
1324  void print_registers(std::ostream &stream, const std::string &prefix = "") ROSE_DEPRECATED("use printRegisters instead") {
1325  printRegisters(stream, prefix);
1326  }
1327 
1328  // [Robb Matzke 2015-11-16]: deprecated
1329  virtual void print_registers(std::ostream &stream, Formatter &fmt) const ROSE_DEPRECATED("use printRegisters instead") {
1330  printRegisters(stream, fmt);
1331  }
1332 
1338  void printMemory(std::ostream &stream, const std::string &prefix = "") const;
1339  virtual void printMemory(std::ostream &stream, Formatter &fmt) const;
1342  // [Robb Matzke 2015-11-16]: deprecated
1343  void print_memory(std::ostream &stream, const std::string prefix = "") const ROSE_DEPRECATED("use printMemory instead") {
1344  printMemory(stream, prefix);
1345  }
1346 
1347  // [Robb Matzke 2015-11-16]: deprecated
1348  virtual void print_memory(std::ostream &stream, Formatter &fmt) const ROSE_DEPRECATED("use printMemory instead") {
1349  printMemory(stream, fmt);
1350  }
1351 
1354  void print(std::ostream &stream, const std::string &prefix = "") const;
1355  virtual void print(std::ostream&, Formatter&) const;
1360  StatePtr obj;
1361  Formatter &fmt;
1362  public:
1363  WithFormatter(const StatePtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1364  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1365  };
1366 
1374  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1383  virtual bool merge(const StatePtr &other, RiscOperators *ops);
1384 };
1385 
1386 
1387 
1389 // RISC Operators
1391 
1393 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
1394 
1415 class RiscOperators: public boost::enable_shared_from_this<RiscOperators> {
1416  SValuePtr protoval_; // Prototypical value used for its virtual constructors
1417  StatePtr currentState_; // State upon which RISC operators operate
1418  StatePtr initialState_; // Lazily updated initial state; see readMemory
1419  SmtSolverPtr solver_; // Optional SMT solver
1420  SgAsmInstruction *currentInsn_; // Current instruction, as set by latest startInstruction call
1421  size_t nInsns_; // Number of instructions processed
1422  std::string name_; // Name to use for debugging
1423 
1425  // Serialization
1426 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
1427 private:
1428  friend class boost::serialization::access;
1429 
1430  template<class S>
1431  void serialize(S &s, const unsigned /*version*/) {
1432  s & BOOST_SERIALIZATION_NVP(protoval_);
1433  s & BOOST_SERIALIZATION_NVP(currentState_);
1434  s & BOOST_SERIALIZATION_NVP(initialState_);
1435  s & BOOST_SERIALIZATION_NVP(solver_);
1436  s & BOOST_SERIALIZATION_NVP(currentInsn_);
1437  s & BOOST_SERIALIZATION_NVP(nInsns_);
1438  s & BOOST_SERIALIZATION_NVP(name_);
1439  }
1440 #endif
1441 
1443  // Real constructors
1444 protected:
1445  // for serialization
1446  RiscOperators()
1447  : currentInsn_(NULL), nInsns_(0) {}
1448 
1449  explicit RiscOperators(const SValuePtr &protoval, const SmtSolverPtr &solver = SmtSolverPtr())
1450  : protoval_(protoval), solver_(solver), currentInsn_(NULL), nInsns_(0) {
1451  ASSERT_not_null(protoval_);
1452  }
1453 
1454  explicit RiscOperators(const StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr())
1455  : currentState_(state), solver_(solver), currentInsn_(NULL), nInsns_(0) {
1456  ASSERT_not_null(state);
1457  protoval_ = state->protoval();
1458  }
1459 
1460 public:
1462  typedef RiscOperatorsPtr Ptr;
1463 
1464 public:
1465  virtual ~RiscOperators() {}
1466 
1468  // Static allocating constructors. None needed since this class is abstract.
1469 
1470 
1472  // Virtual constructors.
1473 public:
1477  virtual RiscOperatorsPtr create(const SValuePtr &protoval, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
1478 
1483  virtual RiscOperatorsPtr create(const StatePtr &state, const SmtSolverPtr &solver = SmtSolverPtr()) const = 0;
1484 
1486  // Dynamic pointer casts. No-op since this is the base class.
1487 public:
1488  static RiscOperatorsPtr promote(const RiscOperatorsPtr &x) {
1489  ASSERT_not_null(x);
1490  return x;
1491  }
1492 
1494  // Other methods part of our API
1495 public:
1499  virtual SValuePtr protoval() const { return protoval_; }
1500 
1501  // [Robb Matzke 2016-01-22]: deprecated
1502  virtual SValuePtr get_protoval() const ROSE_DEPRECATED("use protoval instead") {
1503  return protoval();
1504  }
1505 
1514  virtual SmtSolverPtr solver() const { return solver_; }
1515  virtual void solver(const SmtSolverPtr &s) { solver_ = s; }
1529  virtual StatePtr currentState() const { return currentState_; }
1530  virtual void currentState(const StatePtr &s) { currentState_ = s; }
1533  // [Robb Matzke 2016-01-22]: deprecated
1534  virtual StatePtr get_state() const ROSE_DEPRECATED("use currentState instead") {
1535  return currentState();
1536  }
1537 
1538  // [Robb Matzke 2016-01-22]: deprecated
1539  virtual void set_state(const StatePtr &s) ROSE_DEPRECATED("use currentState instead") {
1540  currentState(s);
1541  }
1542 
1580  virtual StatePtr initialState() const { return initialState_; }
1581  virtual void initialState(const StatePtr &s) { initialState_ = s; }
1589  virtual const std::string& name() const { return name_; }
1590  virtual void name(const std::string &s) { name_ = s; }
1593  // [Robb Matzke 2016-01-22]: deprecated
1594  virtual const std::string& get_name() const ROSE_DEPRECATED("use name instead") { return name(); }
1595  virtual void set_name(const std::string &s) ROSE_DEPRECATED("use name instead") { name(s); }
1596 
1599  void print(std::ostream &stream, const std::string prefix="") const {
1600  Formatter fmt;
1601  fmt.set_line_prefix(prefix);
1602  print(stream, fmt);
1603  }
1604  virtual void print(std::ostream &stream, Formatter &fmt) const {
1605  currentState_->print(stream, fmt);
1606  }
1611  RiscOperatorsPtr obj;
1612  Formatter &fmt;
1613  public:
1614  WithFormatter(const RiscOperatorsPtr &obj, Formatter &fmt): obj(obj), fmt(fmt) {}
1615  void print(std::ostream &stream) const { obj->print(stream, fmt); }
1616  };
1617 
1625  WithFormatter with_format(Formatter &fmt) { return WithFormatter(shared_from_this(), fmt); }
1634  virtual size_t nInsns() const { return nInsns_; }
1635  virtual void nInsns(size_t n) { nInsns_ = n; }
1638  // [Robb Matzke 2016-01-22]: deprecated
1639  virtual size_t get_ninsns() const ROSE_DEPRECATED("use nInsns instead") { return nInsns(); }
1640  virtual void set_ninsns(size_t n) ROSE_DEPRECATED("use nInsns instead") { nInsns(n); }
1641 
1647  return currentInsn_;
1648  }
1649 
1650  // [Robb Matzke 2016-01-22]: deprecated
1651  virtual SgAsmInstruction *get_insn() const ROSE_DEPRECATED("use currentInstruction instead") {
1652  return currentInstruction();
1653  }
1654 
1657  virtual void startInstruction(SgAsmInstruction *insn);
1658 
1661  virtual void finishInstruction(SgAsmInstruction *insn) {
1662  ASSERT_not_null(insn);
1663  ASSERT_require(currentInsn_==insn);
1664  currentInsn_ = NULL;
1665  };
1666 
1667 
1669  // Value Construction Operations
1671  // The trailing underscores are necessary for for undefined_() on some machines, so we just add one to the end of all the
1672  // virtual constructors for consistency.
1673 
1675  virtual SValuePtr undefined_(size_t nbits) {
1676  return protoval_->undefined_(nbits);
1677  }
1678  virtual SValuePtr unspecified_(size_t nbits) {
1679  return protoval_->unspecified_(nbits);
1680  }
1681 
1683  virtual SValuePtr number_(size_t nbits, uint64_t value) {
1684  return protoval_->number_(nbits, value);
1685  }
1686 
1688  virtual SValuePtr boolean_(bool value) {
1689  return protoval_->boolean_(value);
1690  }
1691 
1693  virtual SValuePtr bottom_(size_t nbits) {
1694  return protoval_->bottom_(nbits);
1695  }
1696 
1697 
1699  // x86-specific Operations (FIXME)
1701 
1704  virtual SValuePtr filterCallTarget(const SValuePtr &a) {
1705  return a->copy();
1706  }
1707 
1711  virtual SValuePtr filterReturnTarget(const SValuePtr &a) {
1712  return a->copy();
1713  }
1714 
1718  virtual SValuePtr filterIndirectJumpTarget(const SValuePtr &a) {
1719  return a->copy();
1720  }
1721 
1723  virtual void hlt() {}
1724 
1726  virtual void cpuid() {}
1727 
1729  virtual SValuePtr rdtsc() { return unspecified_(64); }
1730 
1731 
1733  // Boolean Operations
1735 
1738  virtual SValuePtr and_(const SValuePtr &a, const SValuePtr &b) = 0;
1739 
1742  virtual SValuePtr or_(const SValuePtr &a, const SValuePtr &b) = 0;
1743 
1746  virtual SValuePtr xor_(const SValuePtr &a, const SValuePtr &b) = 0;
1747 
1749  virtual SValuePtr invert(const SValuePtr &a) = 0;
1750 
1754  virtual SValuePtr extract(const SValuePtr &a, size_t begin_bit, size_t end_bit) = 0;
1755 
1759  virtual SValuePtr concat(const SValuePtr &a, const SValuePtr &b) = 0;
1760 
1763  virtual SValuePtr leastSignificantSetBit(const SValuePtr &a) = 0;
1764 
1767  virtual SValuePtr mostSignificantSetBit(const SValuePtr &a) = 0;
1768 
1772  virtual SValuePtr rotateLeft(const SValuePtr &a, const SValuePtr &nbits) = 0;
1773 
1777  virtual SValuePtr rotateRight(const SValuePtr &a, const SValuePtr &nbits) = 0;
1778 
1782  virtual SValuePtr shiftLeft(const SValuePtr &a, const SValuePtr &nbits) = 0;
1783 
1787  virtual SValuePtr shiftRight(const SValuePtr &a, const SValuePtr &nbits) = 0;
1788 
1793  virtual SValuePtr shiftRightArithmetic(const SValuePtr &a, const SValuePtr &nbits) = 0;
1794 
1795 
1797  // Comparison Operations
1799 
1802  virtual SValuePtr equalToZero(const SValuePtr &a) = 0;
1803 
1807  virtual SValuePtr ite(const SValuePtr &cond, const SValuePtr &a, const SValuePtr &b) = 0;
1808 
1815  SValuePtr equal(const SValuePtr &a, const SValuePtr &b) ROSE_DEPRECATED("use isEqual instead");
1816  virtual SValuePtr isEqual(const SValuePtr &a, const SValuePtr &b);
1817  virtual SValuePtr isNotEqual(const SValuePtr &a, const SValuePtr &b);
1827  virtual SValuePtr isUnsignedLessThan(const SValuePtr &a, const SValuePtr &b);
1828  virtual SValuePtr isUnsignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1829  virtual SValuePtr isUnsignedGreaterThan(const SValuePtr &a, const SValuePtr &b);
1830  virtual SValuePtr isUnsignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1840  virtual SValuePtr isSignedLessThan(const SValuePtr &a, const SValuePtr &b);
1841  virtual SValuePtr isSignedLessThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1842  virtual SValuePtr isSignedGreaterThan(const SValuePtr &a, const SValuePtr &b);
1843  virtual SValuePtr isSignedGreaterThanOrEqual(const SValuePtr &a, const SValuePtr &b);
1846  // Integer Arithmetic Operations
1849 
1852  virtual SValuePtr unsignedExtend(const SValuePtr &a, size_t new_width) {
1853  return a->copy(new_width);
1854  }
1855 
1858  virtual SValuePtr signExtend(const SValuePtr &a, size_t new_width) = 0;
1859 
1862  virtual SValuePtr add(const SValuePtr &a, const SValuePtr &b) = 0;
1863 
1866  virtual SValuePtr subtract(const SValuePtr &minuend, const SValuePtr &subtrahend);
1867 
1884  virtual SValuePtr addWithCarries(const SValuePtr &a, const SValuePtr &b, const SValuePtr &c,
1885  SValuePtr &carry_out/*output*/) = 0;
1886 
1888  virtual SValuePtr negate(const SValuePtr &a) = 0;
1889 
1891  virtual SValuePtr signedDivide(const SValuePtr &a, const SValuePtr &b) = 0;
1892 
1894  virtual SValuePtr signedModulo(const SValuePtr &a, const SValuePtr &b) = 0;
1895 
1897  virtual SValuePtr signedMultiply(const SValuePtr &a, const SValuePtr &b) = 0;
1898 
1900  virtual SValuePtr unsignedDivide(const SValuePtr &a, const SValuePtr &b) = 0;
1901 
1903  virtual SValuePtr unsignedModulo(const SValuePtr &a, const SValuePtr &b) = 0;
1904 
1906  virtual SValuePtr unsignedMultiply(const SValuePtr &a, const SValuePtr &b) = 0;
1907 
1908 
1910  // Interrupt and system calls
1912 
1917  virtual void interrupt(int /*majr*/, int /*minr*/) {}
1918 
1919 
1921  // Floating-point operations
1922  //
1923  // For now these all have default implementations that throw NotImplemented, but we might change them to pure virtual
1924  // sometime in the future so they're consistent with most other RISC operators. [Robb P. Matzke 2015-08-03]
1926 
1928  virtual SValuePtr fpFromInteger(const SValuePtr &intValue, SgAsmFloatType *fpType);
1929 
1935  virtual SValuePtr fpToInteger(const SValuePtr &fpValue, SgAsmFloatType *fpType, const SValuePtr &dflt);
1936 
1940  virtual SValuePtr fpConvert(const SValuePtr &a, SgAsmFloatType *aType, SgAsmFloatType *retType);
1941 
1943  virtual SValuePtr fpIsNan(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1944 
1946  virtual SValuePtr fpIsDenormalized(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1947 
1949  virtual SValuePtr fpIsZero(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1950 
1955  virtual SValuePtr fpIsInfinity(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1956 
1960  virtual SValuePtr fpSign(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1961 
1967  virtual SValuePtr fpEffectiveExponent(const SValuePtr &fpValue, SgAsmFloatType *fpType);
1968 
1972  virtual SValuePtr fpAdd(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1973 
1978  virtual SValuePtr fpSubtract(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1979 
1983  virtual SValuePtr fpMultiply(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1984 
1988  virtual SValuePtr fpDivide(const SValuePtr &a, const SValuePtr &b, SgAsmFloatType *fpType);
1989 
1993  virtual SValuePtr fpSquareRoot(const SValuePtr &a, SgAsmFloatType *fpType);
1994 
1998  virtual SValuePtr fpRoundTowardZero(const SValuePtr &a, SgAsmFloatType *fpType);
1999 
2000 
2002  // State Accessing Operations
2004 
2029  virtual SValuePtr readRegister(RegisterDescriptor reg) { // old subclasses can still override this if they want,
2030  return readRegister(reg, undefined_(reg.get_nbits())); // but new subclasses should not override this method.
2031  }
2032  virtual SValuePtr readRegister(RegisterDescriptor reg, const SValuePtr &dflt); // new subclasses override this
2044  virtual void writeRegister(RegisterDescriptor reg, const SValuePtr &a) {
2045  ASSERT_not_null(currentState_);
2046  currentState_->writeRegister(reg, a, this);
2047  }
2048 
2054  virtual SValuePtr peekRegister(RegisterDescriptor, const SValuePtr &dflt);
2055 
2078  virtual SValuePtr readMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt,
2079  const SValuePtr &cond) = 0;
2080 
2091  virtual void writeMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &data,
2092  const SValuePtr &cond) = 0;
2093 
2098  virtual SValuePtr peekMemory(RegisterDescriptor segreg, const SValuePtr &addr, const SValuePtr &dflt) = 0;
2099 };
2100 
2101 
2102 
2104 // Instruction Dispatcher
2106 
2108 typedef boost::shared_ptr<class Dispatcher> DispatcherPtr;
2109 
2112 public:
2113  virtual ~InsnProcessor() {}
2114  virtual void process(const DispatcherPtr &dispatcher, SgAsmInstruction *insn) = 0;
2115 };
2116 
2130 class Dispatcher: public boost::enable_shared_from_this<Dispatcher> {
2131 protected:
2132  RiscOperatorsPtr operators;
2134  size_t addrWidth_;
2137  // Dispatchers keep a table of all the kinds of instructions they can handle. The lookup key is typically some sort of
2138  // instruction identifier, such as from SgAsmX86Instruction::get_kind(), and comes from the iproc_key() virtual method.
2139  typedef std::vector<InsnProcessor*> InsnProcessors;
2140  InsnProcessors iproc_table;
2141 
2142 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
2143 private:
2144  friend class boost::serialization::access;
2145 
2146  template<class S>
2147  void serialize(S &s, const unsigned /*version*/) {
2148  s & BOOST_SERIALIZATION_NVP(operators);
2149  s & BOOST_SERIALIZATION_NVP(regdict);
2150  s & BOOST_SERIALIZATION_NVP(addrWidth_);
2151  s & BOOST_SERIALIZATION_NVP(autoResetInstructionPointer_);
2152  //s & iproc_table; -- not saved
2153  }
2154 #endif
2155 
2157  // Real constructors
2158 protected:
2159  // Prototypical constructor
2160  Dispatcher(): regdict(NULL), addrWidth_(0), autoResetInstructionPointer_(true) {}
2161 
2162  // Prototypical constructor
2163  Dispatcher(size_t addrWidth, const RegisterDictionary *regs)
2164  : regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {}
2165 
2166  Dispatcher(const RiscOperatorsPtr &ops, size_t addrWidth, const RegisterDictionary *regs)
2167  : operators(ops), regdict(regs), addrWidth_(addrWidth), autoResetInstructionPointer_(true) {
2168  ASSERT_not_null(operators);
2169  ASSERT_not_null(regs);
2170  }
2171 
2172 public:
2174  typedef DispatcherPtr Ptr;
2175 
2176 public:
2177  virtual ~Dispatcher() {
2178  for (InsnProcessors::iterator iter=iproc_table.begin(); iter!=iproc_table.end(); ++iter)
2179  delete *iter;
2180  }
2181 
2183  // Static allocating constructors. None since this is an abstract class
2184 
2185 
2187  // Virtual constructors
2188 public:
2190  virtual DispatcherPtr create(const RiscOperatorsPtr &ops, size_t addrWidth=0, const RegisterDictionary *regs=NULL) const = 0;
2191 
2193  // Methods to process instructions
2194 public:
2196  virtual void processInstruction(SgAsmInstruction *insn);
2197 
2199  // Instruction processor table operations
2200 public:
2205 
2209  virtual void iproc_replace(SgAsmInstruction *insn, InsnProcessor *iproc);
2210 
2212  virtual int iproc_key(SgAsmInstruction*) const = 0;
2213 
2217  virtual void iproc_set(int key, InsnProcessor *iproc);
2218 
2220  virtual InsnProcessor *iproc_get(int key);
2221 
2223  // Convenience methods that defer the call to some member object
2224 public:
2226  virtual RiscOperatorsPtr get_operators() const { return operators; }
2227 
2230  virtual StatePtr currentState() const { return operators ? operators->currentState() : StatePtr(); }
2231 
2232  // [Robb Matzke 2016-01-22]: deprecated
2233  virtual StatePtr get_state() const ROSE_DEPRECATED("use currentState instead") {
2234  return currentState();
2235  }
2236 
2238  virtual SValuePtr protoval() const { return operators ? operators->protoval() : SValuePtr(); }
2239 
2240  // [Robb Matzke 2016-01-22]: deprecated
2241  virtual SValuePtr get_protoval() const ROSE_DEPRECATED("use protoval instead") {
2242  return protoval();
2243  }
2244 
2250  return operators ? operators->currentInstruction() : NULL;
2251  }
2252 
2253  // [Robb Matzke 2016-01-22]: deprecated
2254  virtual SgAsmInstruction *get_insn() const ROSE_DEPRECATED("use currentInstruction instead") {
2255  return currentInstruction();
2256  }
2257 
2259  virtual SValuePtr undefined_(size_t nbits) const {
2260  ASSERT_not_null(operators);
2261  return operators->undefined_(nbits);
2262  }
2263  virtual SValuePtr unspecified_(size_t nbits) const {
2264  ASSERT_not_null(operators);
2265  return operators->unspecified_(nbits);
2266  }
2267 
2269  virtual SValuePtr number_(size_t nbits, uint64_t number) const {
2270  ASSERT_not_null(operators);
2271  return operators->number_(nbits, number);
2272  }
2273 
2275  // Methods related to registers
2276 public:
2289  return regdict;
2290  }
2291  virtual void set_register_dictionary(const RegisterDictionary *regdict) {
2292  this->regdict = regdict;
2293  }
2300  virtual RegisterDescriptor findRegister(const std::string &regname, size_t nbits=0, bool allowMissing=false) const;
2301 
2308  size_t addressWidth() const { return addrWidth_; }
2309  void addressWidth(size_t nbits);
2313  virtual RegisterDescriptor instructionPointerRegister() const = 0;
2314 
2316  virtual RegisterDescriptor stackPointerRegister() const = 0;
2317 
2325  void autoResetInstructionPointer(bool b) { autoResetInstructionPointer_ = b; }
2329  // Miscellaneous methods that tend to be the same for most dispatchers
2331 public:
2332 
2342 
2347 
2351  virtual void incrementRegisters(SgAsmExpression*);
2352 
2356  virtual void decrementRegisters(SgAsmExpression*);
2357 
2361  virtual SValuePtr effectiveAddress(SgAsmExpression*, size_t nbits=0);
2362 
2368  virtual SValuePtr read(SgAsmExpression*, size_t value_nbits=0, size_t addr_nbits=0);
2369 
2373  virtual void write(SgAsmExpression*, const SValuePtr &value, size_t addr_nbits=0);
2374 };
2375 
2376 
2377 
2379 // Printing
2381 
2382 std::ostream& operator<<(std::ostream&, const Exception&);
2383 std::ostream& operator<<(std::ostream&, const SValue&);
2384 std::ostream& operator<<(std::ostream&, const SValue::WithFormatter&);
2385 std::ostream& operator<<(std::ostream&, const MemoryState&);
2386 std::ostream& operator<<(std::ostream&, const MemoryState::WithFormatter&);
2387 std::ostream& operator<<(std::ostream&, const RegisterState&);
2388 std::ostream& operator<<(std::ostream&, const RegisterState::WithFormatter&);
2389 std::ostream& operator<<(std::ostream&, const State&);
2390 std::ostream& operator<<(std::ostream&, const State::WithFormatter&);
2391 std::ostream& operator<<(std::ostream&, const RiscOperators&);
2392 std::ostream& operator<<(std::ostream&, const RiscOperators::WithFormatter&);
2393 
2394 } // namespace
2395 } // namespace
2396 } // namespace
2397 } // namespace
2398 
2399 #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:28
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.
Definition: Access.h:13
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.
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.
SValuePtr equal(const SValuePtr &a, const SValuePtr &b) ROSE_DEPRECATED("use isEqual instead")
Equality comparison.
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.
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.
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: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 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.
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 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.
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.