00001 #ifndef Rose_InsnSemanticsExpr_H
00002 #define Rose_InsnSemanticsExpr_H
00003
00004 #include <boost/shared_ptr.hpp>
00005 #include <boost/enable_shared_from_this.hpp>
00006
00007 class SMTSolver;
00008
00012 namespace InsnSemanticsExpr {
00013
00017 enum Operator
00018 {
00019 OP_ADD,
00020 OP_AND,
00021 OP_ASR,
00022 OP_BV_AND,
00023 OP_BV_OR,
00024 OP_BV_XOR,
00025 OP_CONCAT,
00026 OP_EQ,
00027 OP_EXTRACT,
00028 OP_INVERT,
00029 OP_ITE,
00030 OP_LSSB,
00031 OP_MSSB,
00032 OP_NE,
00033 OP_NEGATE,
00034 OP_NOOP,
00035 OP_OR,
00036 OP_ROL,
00037 OP_ROR,
00038 OP_SDIV,
00039 OP_SEXTEND,
00040 OP_SGE,
00041 OP_SGT,
00042 OP_SHL0,
00043 OP_SHL1,
00044 OP_SHR0,
00045 OP_SHR1,
00046 OP_SLE,
00047 OP_SLT,
00048 OP_SMOD,
00049 OP_SMUL,
00050 OP_UDIV,
00051 OP_UEXTEND,
00052 OP_UGE,
00053 OP_UGT,
00054 OP_ULE,
00055 OP_ULT,
00056 OP_UMOD,
00057 OP_UMUL,
00058 OP_ZEROP,
00059 };
00060
00061 const char *to_str(Operator o);
00062
00063 typedef std::map<uint64_t, uint64_t> RenameMap;
00064
00065 class TreeNode;
00066 class InternalNode;
00067 class LeafNode;
00068
00069 typedef boost::shared_ptr<const TreeNode> TreeNodePtr;
00070 typedef boost::shared_ptr<const InternalNode> InternalNodePtr;
00071 typedef boost::shared_ptr<const LeafNode> LeafNodePtr;
00072
00073 class Visitor {
00074 public:
00075 virtual ~Visitor() {}
00076 virtual void operator()(const TreeNodePtr&) = 0;
00077 };
00078
00087 class TreeNode: public boost::enable_shared_from_this<TreeNode> {
00088 protected:
00089 size_t nbits;
00090 std::string comment;
00091 public:
00092 TreeNode(size_t nbits, std::string comment=""): nbits(nbits), comment(comment) { assert(nbits>0); }
00093
00097 virtual void print(std::ostream&, RenameMap *rmap=NULL) const = 0;
00098
00101 virtual bool equal_to(const TreeNodePtr& other, SMTSolver*) const = 0;
00102
00107 virtual bool is_known() const = 0;
00108
00111 virtual uint64_t get_value() const = 0;
00112
00115 const std::string& get_comment() const { return comment; }
00116 void set_comment(const std::string &s) { comment=s; }
00121 size_t get_nbits() const { return nbits; }
00122
00125 virtual void depth_first_visit(Visitor*) const = 0;
00126
00128 std::set<LeafNodePtr> get_variables() const;
00129
00131 InternalNodePtr isInternalNode() const {
00132 return boost::dynamic_pointer_cast<const InternalNode>(shared_from_this());
00133 }
00134
00136 LeafNodePtr isLeafNode() const {
00137 return boost::dynamic_pointer_cast<const LeafNode>(shared_from_this());
00138 }
00139 };
00140
00145 class InternalNode: public TreeNode {
00146 private:
00147 Operator op;
00148 std::vector<TreeNodePtr> children;
00149
00150
00151
00152 InternalNode(size_t nbits, Operator op, const std::string comment="")
00153 : TreeNode(nbits, comment), op(op) {}
00154 InternalNode(size_t nbits, Operator op, const TreeNodePtr &a, std::string comment="")
00155 : TreeNode(nbits, comment), op(op) {
00156 add_child(a);
00157 }
00158 InternalNode(size_t nbits, Operator op, const TreeNodePtr &a, const TreeNodePtr &b, std::string comment="")
00159 : TreeNode(nbits, comment), op(op) {
00160 add_child(a);
00161 add_child(b);
00162 }
00163 InternalNode(size_t nbits, Operator op, const TreeNodePtr &a, const TreeNodePtr &b, const TreeNodePtr &c,
00164 std::string comment="")
00165 : TreeNode(nbits, comment), op(op) {
00166 add_child(a);
00167 add_child(b);
00168 add_child(c);
00169 }
00170
00171 public:
00173 static InternalNodePtr create(size_t nbits, Operator op, const std::string comment="") {
00174 InternalNodePtr retval(new InternalNode(nbits, op, comment));
00175 return retval;
00176 }
00177 static InternalNodePtr create(size_t nbits, Operator op, const TreeNodePtr &a, const std::string comment="") {
00178 InternalNodePtr retval(new InternalNode(nbits, op, a, comment));
00179 return retval;
00180 }
00181 static InternalNodePtr create(size_t nbits, Operator op, const TreeNodePtr &a, const TreeNodePtr &b,
00182 const std::string comment="") {
00183 InternalNodePtr retval(new InternalNode(nbits, op, a, b, comment));
00184 return retval;
00185 }
00186 static InternalNodePtr create(size_t nbits, Operator op, const TreeNodePtr &a, const TreeNodePtr &b, const TreeNodePtr &c,
00187 const std::string comment="") {
00188 InternalNodePtr retval(new InternalNode(nbits, op, a, b, c, comment));
00189 return retval;
00190 }
00193
00194 virtual void print(std::ostream &o, RenameMap *rmap=NULL) const;
00195
00196
00197 virtual bool equal_to(const TreeNodePtr &other, SMTSolver*) const;
00198
00199
00200 virtual bool is_known() const {
00201 return false;
00202 }
00203
00204
00205 virtual uint64_t get_value() const { ROSE_ASSERT(!"not a constant value"); return 0;}
00206
00207
00208 virtual void depth_first_visit(Visitor*) const;
00209
00211 size_t size() const { return children.size(); }
00212
00214 TreeNodePtr child(size_t idx) const { return children[idx]; }
00215
00217 Operator get_operator() const { return op; }
00218
00221 void add_child(const TreeNodePtr &child);
00222 };
00223
00225 class LeafNode: public TreeNode {
00226 private:
00227 bool known;
00228 union {
00229 uint64_t ival;
00230 uint64_t name;
00231 };
00232
00233
00234 LeafNode(std::string comment=""): TreeNode(32, comment), known(true), ival(0) {}
00235
00236 public:
00238 static LeafNodePtr create_variable(size_t nbits, std::string comment="");
00239
00242 static LeafNodePtr create_integer(size_t nbits, uint64_t n, std::string comment="");
00243
00244
00245 virtual bool is_known() const;
00246
00247
00248 virtual uint64_t get_value() const;
00249
00252 uint64_t get_name() const;
00253
00254
00255 virtual void print(std::ostream &o, RenameMap *rmap=NULL) const;
00256
00257
00258 virtual bool equal_to(const TreeNodePtr &other, SMTSolver*) const;
00259
00260
00261 virtual void depth_first_visit(Visitor*) const;
00262 };
00263 };
00264
00265
00266 std::ostream& operator<<(std::ostream &o, const InsnSemanticsExpr::TreeNode &node);
00267
00268 #endif