Otawa  0.10
SymbolicExpr.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * Symbolic Expression definition and processor
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2011, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 
24 #ifndef OTAWA_DATA_CLP_SYMBOLICEXPR_H_
25 #define OTAWA_DATA_CLP_SYMBOLICEXPR_H_
26 
27 #include <elm/io.h>
28 #include <elm/io/Output.h>
29 #include <elm/genstruct/Vector.h>
30 #include <elm/string/AutoString.h>
31 #include <otawa/cfg/BasicBlock.h>
33 #include <otawa/data/clp/ClpPack.h>
34 
35 using namespace elm;
36 using namespace elm::genstruct;
37 
38 namespace otawa {
39 
40 namespace hard {
41  class Platform;
42 } // hard
43 
44 namespace se{
45 
46  typedef clp::Value V;
47 
49  typedef enum {
52  ADDR,
53  REG,
54  NEG,
55  ADD,
56  CMP,
57  CMPU,
59  LE,
60  LT,
61  GE,
62  GT,
63  EQ,
64  NE,
66  ULE,
67  ULT,
68  UGE,
69  UGT,
71  } op_t;
72 
74  class SymbExpr{
75  public:
81  SymbExpr(op_t op=NONE, SymbExpr *a=NULL, SymbExpr *b=NULL, V val=0, SymbExpr *parent=NULL):
82  _op(op), _a(a), _b(b), _val(val), _parent(parent) {
83  ASSERT(_op >= NONE && _op <= OR);
84  if(_a != NULL)
85  _a->set_parent(this);
86  if(_b != NULL)
87  _b->set_parent(this);
88  }
93  SymbExpr(const SymbExpr &expr):
94  _op(expr._op), _val(expr._val), _parent(expr._parent) {
95  ASSERT(_op >= NONE && _op <= OR);
96  set_a(expr._a); // to copy expr._a
97  set_b(expr._b); // to copy expr._b
98  }
104  virtual ~SymbExpr(){
105  delete _a;
106  delete _b;
107  if (_parent != NULL){
108  if (_parent->_a == this)
109  _parent->_a = NULL;
110  else if (_parent->_b == this)
111  _parent->_b = NULL;
112  }
113  }
114 
120  virtual SymbExpr* copy(void);
126  virtual SymbExpr& operator=(const SymbExpr& expr);
132  virtual bool operator==(const SymbExpr& expr) const;
138  bool operator!=(const SymbExpr& expr) const;
139 
144  op_t op(void) const { return _op; }
148  SymbExpr* a(void) const { return _a; }
152  SymbExpr* b(void) const { return _b; }
156  V val(void) const { return _val; }
160  SymbExpr* parent(void) const { return _parent; }
161 
170  void replace(SymbExpr *searched_se, SymbExpr *new_se);
174  virtual String asString(const hard::Platform *pf = 0);
180  void print(io::Output& out, const hard::Platform *pf = 0);
185  virtual void canonize(void);
191  virtual Vector<V> used_reg(void);
197  virtual Vector<V> used_addr(void);
198 
209  void set_a(SymbExpr *a){
210  SymbExpr *olda = _a;
211  if (a) {
212  _a = a->copy();
213  _a->set_parent(this);
214  } else
215  _a = NULL;
216  // in last, because a can be delete here, we must copy it before
217  delete olda;
218  }
228  void set_b(SymbExpr *b){
229  SymbExpr *oldb = _b;
230  if (b) {
231  _b = b->copy();
232  _b->set_parent(this);
233  } else
234  _b = NULL;
235  // in last, because b can be delete here, we must copy it before
236  delete oldb;
237  }
244  void set_parent(SymbExpr *parent) { _parent = parent ; }
245 
246  protected:
253  };
254 
256  class SEConst: public SymbExpr{
257  public:
264  SEConst(V value, SymbExpr *parent=NULL):
265  SymbExpr(CONST, NULL, NULL, value, parent) {}
270  SEConst(const SEConst &expr): SymbExpr(expr) {}
271 
277  virtual SEConst* copy(void);
283  virtual SymbExpr& operator=(const SEConst& expr);
289  virtual bool operator==(const SymbExpr& expr) const;
290 
295  virtual String asString(const hard::Platform *pf = 0);
300  virtual void canonize(void);
301  };
302 
304  class SEAddr: public SymbExpr{
305  public:
312  SEAddr(V value, SymbExpr* parent=NULL):
313  SymbExpr(ADDR, NULL, NULL, value, parent) {}
318  SEAddr(const SEAddr &expr): SymbExpr(expr) {}
319 
325  virtual SEAddr* copy(void);
331  virtual SymbExpr& operator=(const SEAddr& expr);
337  virtual bool operator==(const SymbExpr& expr) const;
338 
343  virtual String asString(const hard::Platform *pf = 0);
348  virtual void canonize(void);
354  virtual Vector<V> used_addr(void);
355  };
356 
358  class SEReg: public SymbExpr{
359  public:
366  SEReg(V value, SymbExpr *parent=NULL):
367  SymbExpr(REG, NULL, NULL, value, parent) {}
372  SEReg(const SEReg &expr): SymbExpr(expr) {}
373 
379  virtual SEReg* copy(void);
385  virtual SymbExpr& operator=(const SEReg& expr);
391  virtual bool operator==(const SymbExpr& expr) const;
392 
397  virtual String asString(const hard::Platform *pf = 0);
402  virtual void canonize(void);
408  virtual Vector<V> used_reg(void);
409  };
410 
412  class SENeg: public SymbExpr{
413  public:
422  SENeg(SymbExpr *expr=NULL, SymbExpr *parent=NULL):
423  SymbExpr(NEG, expr, NULL, 0, parent) {}
428  SENeg(const SENeg &expr): SymbExpr(expr) {}
429 
435  virtual SENeg* copy(void);
441  virtual SymbExpr& operator=(const SENeg& expr);
447  virtual bool operator==(const SymbExpr& expr) const;
448 
453  virtual String asString(const hard::Platform *pf = 0);
458  virtual void canonize(void);
459  };
460 
462  class SEAdd: public SymbExpr{
463  public:
475  SEAdd(SymbExpr *a=NULL, SymbExpr *b=NULL, SymbExpr *parent=NULL):
476  SymbExpr(ADD, a, b, 0, parent) {}
481  SEAdd(const SEAdd &expr): SymbExpr(expr) {}
482 
488  virtual SEAdd* copy(void);
494  virtual SymbExpr& operator=(const SEAdd& expr);
500  virtual bool operator==(const SymbExpr& expr) const;
501 
506  virtual String asString(const hard::Platform *pf = 0);
511  virtual void canonize(void);
512  };
513 
520  class SECmp: public SymbExpr{
521  public:
539  SECmp(op_t op, SymbExpr *a=NULL, SymbExpr *b=NULL, SymbExpr *parent=NULL):
540  SymbExpr(op, a, b, 0, parent) {}
545  SECmp(const SECmp &expr): SymbExpr(expr) {}
546 
552  virtual SECmp* copy(void);
558  virtual SymbExpr& operator=(const SECmp& expr);
564  virtual bool operator==(const SymbExpr& expr) const;
565 
570  virtual String asString(const hard::Platform *pf = 0);
575  virtual void canonize(void);
579  SECmp* logicalNot(void);
580  };
581 
582 
585 
587  public:
588  FilterBuilder(BasicBlock *_bb, clp::ClpProblem& problem);
589  private:
590  void getFilters(void);
591  void iterateBranchPaths(Inst *inst, const Vector<Inst *>& insts);
592  sem::cond_t reverseCond(sem::cond_t cond);
593  SECmp *makeFilters(SECmp *se, Inst *cur_inst, sem::Block& block);
594  void addFilters(SECmp *se, const Vector<Inst *>& insts);
595 
602  };
603 
604 
611  void applyFilter(V &v, se::op_t cmp_op, V f);
612 } // se
613 
614 } // otawa
615 
617  { out << "=> "; sym->print(out); return out; }
618 // output
620  cerr << "ici !\n";
621  se.print(out);
622  return out;
623 }
624 
625 
626 #endif /* OTAWA_DATA_CLP_SYMBOLICEXPR_H_ */
Definition: SymbolicExpr.h:50
bool operator==(const CString &s1, const CString &s2)
Definition: SymbolicExpr.h:70
struct otawa::sem::inst inst
equal
Definition: SymbolicExpr.h:64
SymbExpr * a(void) const
Definition: SymbolicExpr.h:148
void copy(T *target, const T *source, int size)
less or equal
Definition: SymbolicExpr.h:60
SEReg(const SEReg &expr)
Copy constructor.
Definition: SymbolicExpr.h:372
Identifier< Vector< SECmp * > > REG_FILTERS
SymbExpr * _a
Definition: SymbolicExpr.h:249
void applyFilter(V &v, se::op_t cmp_op, V f)
Apply a filter on the value.
Definition: clp_symbolic_expr.cpp:967
Undeterminate unsigned compare.
Definition: SymbolicExpr.h:59
void set_parent(SymbExpr *parent)
Set the reference to the parent of this.
Definition: SymbolicExpr.h:244
Vector< V > known_reg
Definition: SymbolicExpr.h:599
op_t op(void) const
Accessors.
Definition: SymbolicExpr.h:144
SEReg(V value, SymbExpr *parent=NULL)
Constructors.
Definition: SymbolicExpr.h:366
SEConst(V value, SymbExpr *parent=NULL)
Constructors.
Definition: SymbolicExpr.h:264
Register.
Definition: SymbolicExpr.h:54
io::Output cerr(io::err)
SEAdd(const SEAdd &expr)
Copy constructor.
Definition: SymbolicExpr.h:481
Addition.
Definition: SymbolicExpr.h:56
Definition: SymbolicExpr.h:51
A block represents a sequence of semantic instructions inst.
Definition: inst.h:183
SEConst(const SEConst &expr)
Copy constructor.
Definition: SymbolicExpr.h:270
SymbExpr * _b
Definition: SymbolicExpr.h:250
void print(io::Output &out, const hard::Platform *pf=0)
Output a string representation of the expression.
Definition: clp_symbolic_expr.cpp:74
Memory reference.
Definition: SymbolicExpr.h:53
virtual SymbExpr * copy(void)
Operators.
Definition: clp_symbolic_expr.cpp:41
A pack of CLP states inside a BasicBlock.
Definition: ClpPack.h:41
Vector< SECmp * > reg_filters
Definition: SymbolicExpr.h:597
Symbolic expression.
Definition: SymbolicExpr.h:74
SymbExpr(const SymbExpr &expr)
Copy constructor.
Definition: SymbolicExpr.h:93
A set of values represented by a Circular Linear Progression.
Definition: ClpValue.h:66
value_t value(CString name, int value)
not equal
Definition: SymbolicExpr.h:66
Undeterminate compare.
Definition: SymbolicExpr.h:57
Vector< V > known_addr
Definition: SymbolicExpr.h:600
Definition: SymbolicExpr.h:586
Compare symbolic expression This class define three type of compare expression: condition (if): SECmp...
Definition: SymbolicExpr.h:520
SENeg(SymbExpr *expr=NULL, SymbExpr *parent=NULL)
Constructors.
Definition: SymbolicExpr.h:422
void set_b(SymbExpr *b)
Set the second child expression (b)
Definition: SymbolicExpr.h:228
bool operator!=(const CString &s1, const CString &s2)
SENeg(const SENeg &expr)
Copy constructor.
Definition: SymbolicExpr.h:428
Register.
Definition: SymbolicExpr.h:358
op_t _op
Attributes.
Definition: SymbolicExpr.h:248
Negation.
Definition: SymbolicExpr.h:412
Definition: SymbolicExpr.h:69
BasicBlock * bb
Definition: SymbolicExpr.h:596
sys::SystemOutStream & out
SymbExpr * parent(void) const
Definition: SymbolicExpr.h:160
cond_t
Definition: inst.h:76
greater or equal
Definition: SymbolicExpr.h:62
V _val
Definition: SymbolicExpr.h:251
SEAddr(const SEAddr &expr)
Copy constructor.
Definition: SymbolicExpr.h:318
clp::ClpStatePack pack
Definition: SymbolicExpr.h:601
This class records information about the architecture where the processed program will run...
Definition: Platform.h:48
Constant.
Definition: SymbolicExpr.h:52
This class represents identifier with a typed associated value.
Definition: Identifier.h:51
Vector< SECmp * > addr_filters
Definition: SymbolicExpr.h:598
This is the minimal definition of a basic block.
Definition: BasicBlock.h:43
Addition.
Definition: SymbolicExpr.h:462
AutoString & operator<<(CString str, const T &value)
clp::Value V
Definition: SymbolicExpr.h:46
This class represents assembly instruction of a piece of code.
Definition: Inst.h:62
SEAdd(SymbExpr *a=NULL, SymbExpr *b=NULL, SymbExpr *parent=NULL)
Constructors.
Definition: SymbolicExpr.h:475
Memory reference.
Definition: SymbolicExpr.h:304
Definition: SymbolicExpr.h:68
Constants.
Definition: SymbolicExpr.h:256
SymbExpr * b(void) const
Definition: SymbolicExpr.h:152
Definition: SymbolicExpr.h:67
SEAddr(V value, SymbExpr *parent=NULL)
Constructors.
Definition: SymbolicExpr.h:312
greater than
Definition: SymbolicExpr.h:63
SECmp(op_t op, SymbExpr *a=NULL, SymbExpr *b=NULL, SymbExpr *parent=NULL)
Constructors.
Definition: SymbolicExpr.h:539
SymbExpr * _parent
Definition: SymbolicExpr.h:252
less than
Definition: SymbolicExpr.h:61
virtual ~SymbExpr()
Destructor of a symbolic expression This recursively delete a and b childs, and set to NULL the refer...
Definition: SymbolicExpr.h:104
Oposite of.
Definition: SymbolicExpr.h:55
void print(Output &out, const T &v)
Definition: Identifier.h:44
SymbExpr(op_t op=NONE, SymbExpr *a=NULL, SymbExpr *b=NULL, V val=0, SymbExpr *parent=NULL)
Constructor and destructor.
Definition: SymbolicExpr.h:81
Identifier< Vector< SECmp * > > ADDR_FILTERS
V val(void) const
Definition: SymbolicExpr.h:156
SECmp(const SECmp &expr)
Copy constructor.
Definition: SymbolicExpr.h:545
void set_a(SymbExpr *a)
Setters.
Definition: SymbolicExpr.h:209
op_t
Defined operators.
Definition: SymbolicExpr.h:49