00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "scv/scv_util.h"
00039 #include "scv/scv_constraint.h"
00040
00041 #include "cuddObj.hh"
00042 #include "cudd.h"
00043 #include "cuddInt.h"
00044 #include "util.h"
00045
00046 #include <float.h>
00047
00049
00050
00051
00053
00054 static int sDebugLevel = 0;
00055 static void _scv_push_constraint(scv_constraint_base * c);
00056
00057 scv_constraint_base::scv_constraint_base() :
00058 mode_(scv_extensions_if::RANDOM), scan_counter_(-1), ignore_(0)
00059 {
00060 _scv_push_constraint(this);
00061 }
00062
00063 scv_constraint_base::~scv_constraint_base() {}
00064
00065 void scv_constraint_base::next()
00066 {
00067 uninitialize();
00068 scv_constraint_manager::set_value(this, true);
00069 }
00070
00071 void scv_constraint_base::set_mode(scv_extensions_if::mode_t m)
00072 {
00073 if (m == scv_extensions_if::DISTRIBUTION) {
00074 _scv_message::message(_scv_message::CONSTRAINT_INVALID_MODE,
00075 get_name());
00076 } else {
00077 if (mode_ == scv_extensions_if::SCAN) {
00078 scan_counter_ = -1;
00079 _scv_message::message(_scv_message::NOT_IMPLEMENTED_YET, "SCAN mode for complex constraints (will treat as RANDOM).");
00080 mode_ = scv_extensions_if::RANDOM;
00081 } else {
00082 mode_ = m;
00083 }
00084 list<scv_smart_ptr_if*>::iterator i;
00085 for (i= get_members().begin(); i != get_members().end(); ++i) {
00086 scv_extensions_if* e = (*i)->get_extensions_ptr();
00087 e->get_constraint_data()->set_ext_mode(m, 0, 0);
00088 }
00089 }
00090 }
00091
00092 scv_extensions_if::mode_t scv_constraint_base::get_mode(void) const
00093 {
00094 return mode_;
00095 }
00096
00097 void scv_constraint_base::set_random(scv_shared_ptr<scv_random> g)
00098 {
00099 gen_ = g;
00100 list<scv_smart_ptr_if*>::iterator i;
00101 for (i= get_members().begin(); i != get_members().end(); ++i) {
00102 scv_extensions_if* e = (*i)->get_extensions_ptr();
00103 e->set_random(g);
00104 }
00105 }
00106
00107 scv_shared_ptr<scv_random> scv_constraint_base::get_random(void)
00108 {
00109 if (gen_.isNull()) {
00110 gen_ = new scv_random(get_name());
00111 scv_shared_ptr<scv_random> g(gen_);
00112 gen_ = g;
00113 }
00114 return gen_;
00115 }
00116
00117 void scv_constraint_base::get_members(list<scv_smart_ptr_if*>& vlist)
00118 {
00119 list<scv_smart_ptr_if*>::iterator i;
00120 for (i = pointers_.begin(); i != pointers_.end(); i++) {
00121 vlist.push_back(*i);
00122 }
00123 }
00124
00125 list<scv_smart_ptr_if*>& scv_constraint_base::get_members(void)
00126 {
00127 return pointers_;
00128 }
00129
00130 int scv_constraint_base::debug_ = scv_debug::INITIAL_DEBUG_LEVEL;
00131
00132 int scv_constraint_base::get_debug()
00133 {
00134 return debug_;
00135 }
00136
00137 const char *scv_constraint_base::get_name() const
00138 {
00139 return name_.c_str();
00140 }
00141
00142 const string& scv_constraint_base::get_name_string() const
00143 {
00144 return name_;
00145 }
00146
00147 const char *scv_constraint_base::kind() const
00148 {
00149 static const char *name = "scv_constraint_base";
00150 return name;
00151 }
00152
00153 void scv_constraint_base::print(ostream& o, int details, int indent) const
00154 {
00155 int local_indent = indent;
00156 string spaces="";
00157
00158 for (int i=0; i < indent; i++) spaces += " ";
00159
00160 if (details == 0 || details == 1) {
00161 o << spaces << kind() << " Name: " << name_ << endl;
00162 o << spaces << " Hard constraints: " << _hard_constraints << endl;
00163 o << spaces << " Soft constraints: " << _soft_constraints << endl;
00164 }
00165
00166 if (details == 0 || details == 2) {
00167 o << spaces << " Number of elements: " << pointers_.size() << endl;
00168 o << spaces << " Current value of elements: " << endl;
00169 list<scv_smart_ptr_if*>::const_iterator i;
00170 for (i= pointers_.begin(); i != pointers_.end(); ++i) {
00171 o << spaces << " " << (*i)->get_name() << ": " ;
00172 if ((*i)->get_extensions_ptr()->is_record() ||
00173 (*i)->get_extensions_ptr()->is_array()) {
00174 o << endl;
00175 local_indent = indent + 4;
00176 } else {
00177 local_indent = indent;
00178 }
00179 (*i)->print(o, details, local_indent);
00180 }
00181 }
00182 }
00183
00184 void scv_constraint_base::set_debug(int debug)
00185 {
00186 if ( debug_ == debug ) return;
00187 debug_ = debug;
00188 scv_debug::set_facility_level(scv_debug::RANDOMIZATION, debug);
00189 }
00190
00191 void scv_constraint_base::show(int details, int indent) const
00192 {
00193 print(scv_out, details, indent);
00194 }
00195
00196 scv_expression scv_constraint_base::get_constraint() const
00197 {
00198 return scv_constraint_base::eh() && scv_constraint_base::ebh();
00199 }
00200
00201 scv_expression scv_constraint_base::get_soft_constraint() const
00202 {
00203 return scv_constraint_base::es() && scv_constraint_base::ebs();
00204 }
00205
00206 scv_constraint_base* scv_constraint_base::get_copy(scv_constraint_base * from)
00207 {
00208 assert(0);
00209 return NULL;
00210 }
00211
00212 void scv_constraint_base::uninitialize()
00213 {
00214 list<scv_smart_ptr_if*>::iterator i;
00215 for (i= pointers_.begin(); i != pointers_.end(); ++i)
00216 (*i)->get_extensions_ptr()->uninitialize();
00217 }
00218
00219 void scv_constraint_base::initialize()
00220 {
00221 scv_constraint_manager::set_value(this, true);
00222 }
00223
00224 bddNodeT& scv_constraint_base::get_bdd()
00225 {
00226 static bddNodeT& bh = scv_constraint_manager::get_bdd(
00227 scv_constraint_base::get_constraint(), this, true);
00228 static bddNodeT& bs = scv_constraint_manager::get_bdd(
00229 scv_constraint_base::get_soft_constraint(), this, false);
00230 static bddNodeT& b = _scv_bdd_and(bh, bs, this);
00231 if (ignore_) {
00232 return bh;
00233 } else {
00234 return b;
00235 }
00236 }
00237
00238 void scv_constraint_base::init_bdd()
00239 {
00240 return;
00241 }
00242
00243 scv_expression& scv_constraint_base::eh() const
00244 {
00245 static scv_expression e = true;
00246 return e;
00247 }
00248
00249 scv_expression& scv_constraint_base::es() const
00250 {
00251 static scv_expression e = true;
00252 return e;
00253 }
00254
00255 scv_expression& scv_constraint_base::ebh() const
00256 {
00257 static scv_expression e = true;
00258 return e;
00259 }
00260
00261 scv_expression& scv_constraint_base::ebs() const
00262 {
00263 static scv_expression e = true;
00264 return e;
00265 }
00266
00267 void scv_constraint_base::set_up_members(list<scv_smart_ptr_if*>& members)
00268 {
00269 pointers_ = members;
00270 scv_constraint_manager::reset();
00271 list<scv_smart_ptr_if*>::iterator i;
00272 for (i= pointers_.begin(); i != pointers_.end(); ++i) {
00273 (*i)->get_extensions_ptr()->set_constraint(this);
00274
00275 }
00276 }
00277
00278 void scv_constraint_base::ignore_soft_constraint(void)
00279 {
00280 ignore_ = true;
00281 }
00282
00283 bool scv_constraint_base::is_ignored_soft_constraint(void)
00284 {
00285 return ignore_;
00286 }
00287
00288 int scv_constraint_base::get_scan_counter(void)
00289 {
00290 return ++scan_counter_;
00291 }
00292
00293 void scv_constraint_base::set_expression_string(const char * e, bool hard_constraint)
00294 {
00295 if (hard_constraint) {
00296 _hard_constraints = e;
00297 } else {
00298 _soft_constraints = e;
00299 }
00300 }
00301
00303
00304
00305
00306
00307
00309
00310 void _scv_remove(_scv_expr* e);
00311 static void _scv_remove_data(_scv_expr* e);
00312 static void _scv_remove_if_bdd(_scv_expr* e);
00313 static bool _scv_is_avoid_duplicate(scv_constraint_base * c);
00314 static bool _scv_is_scan(scv_constraint_base * c);
00315
00316 _scv_constraint_manager* scv_constraint_manager::globalConstraintManagerP=NULL;
00317 static _scv_expr* errorExprP;
00318
00319 bddNodeT& scv_constraint_manager::get_bdd(scv_expression e, scv_constraint_base* c, bool hard_constraint)
00320 {
00321 return getConstraintManagerP()->get_bdd(e, c, hard_constraint);
00322 }
00323
00324 void scv_constraint_manager::init_bdd(scv_expression e, scv_constraint_base* c, bool hard_constraint)
00325 {
00326 getConstraintManagerP()->init_bdd(e, c, hard_constraint);
00327 }
00328
00329 void scv_constraint_manager::init_maxvar(const scv_expression& he, const scv_expression& se)
00330 {
00331 getConstraintManagerP()->countMaxVar(he);
00332 getConstraintManagerP()->countMaxVar(se);
00333 }
00334
00335 void scv_constraint_manager::set_value(scv_extensions_if* s, bddNodeT *b,
00336 _scv_constraint_data *cdata_)
00337 {
00338 getConstraintManagerP()->assignRandomValue(s, b, cdata_);
00339 }
00340
00341 void scv_constraint_manager::set_value(scv_constraint_base* c, bool simplify)
00342 {
00343 getConstraintManagerP()->assignRandomValue(c, simplify);
00344 }
00345
00346 void scv_constraint_manager::wrapup(scv_extensions_if* s, void *argP)
00347 {
00348 getConstraintManagerP()->wrapup(s);
00349 }
00350
00351 void scv_constraint_manager::reset(void)
00352 {
00353 getConstraintManagerP()->reset();
00354 }
00355
00356 void scv_constraint_manager::add_extension(scv_extensions_if *s)
00357 {
00358 getConstraintManagerP()->add_extension(s);
00359 }
00360
00361 _scv_constraint_manager* scv_constraint_manager::getConstraintManagerP(void)
00362 {
00363 SCV_STARTUP();
00364 return globalConstraintManagerP;
00365 }
00366
00368
00369
00370
00372
00373 _scv_expr::_scv_expr()
00374 {
00375 type = _scv_expr::EMPTY;
00376 bit_width = 0;
00377 vecsize = 0;
00378 sigLsb = 0;
00379 sigMsb = 0;
00380 value.uvalue = 0;
00381 isVar = 0;
00382 }
00383
00384 _scv_expr::_scv_expr(const _scv_expr& other)
00385 {
00386 type = other.type;
00387 bit_width = other.bit_width;
00388 vecsize = other.vecsize;
00389 sigLsb = other.sigLsb;
00390 sigMsb = other.sigMsb;
00391 value = other.value;
00392 isVar = other.isVar;
00393 sc_data = other.sc_data;
00394 }
00395
00396 _scv_expr::~_scv_expr() {}
00397
00398 _scv_expr& _scv_expr::operator=(const _scv_expr& rhs)
00399 {
00400 type = rhs.type;
00401 bit_width = rhs.bit_width;
00402 vecsize = rhs.vecsize;
00403 sigLsb = rhs.sigLsb;
00404 sigMsb = rhs.sigMsb;
00405 value = rhs.value;
00406 isVar = rhs.isVar;
00407 sc_data = rhs.sc_data;
00408
00409 return *this;
00410 }
00411
00412 void _scv_expr::setType(ExprValueType t)
00413 {
00414 type = t;
00415 }
00416
00417 void _scv_expr::setBddNodeP(bddNodeT* b)
00418 {
00419 value.bdd = b;
00420 }
00421
00422 void _scv_expr::setIntValue(long long i)
00423 {
00424 value.ivalue = i;
00425 }
00426
00427 void _scv_expr::setBooleanValue(bool b)
00428 {
00429 value.bvalue = b;
00430 }
00431
00432 void _scv_expr::setUnsignedValue(unsigned long long u)
00433 {
00434 value.uvalue = u;
00435 }
00436
00437 void _scv_expr::setBigValue(sc_bv_base& v, int bw)
00438 {
00439 scv_shared_ptr<sc_bv_base> tmp(new sc_bv_base(bw));
00440 sc_data = tmp;
00441 *sc_data = v;
00442 }
00443
00444 void _scv_expr::setDoubleValue(double d)
00445 {
00446 value.dvalue = d;
00447 }
00448
00449 void _scv_expr::setStringValue(const char* s)
00450 {
00451 value.cvalue = new char[strlen(s)+1];
00452 strcpy(value.cvalue, s);
00453 }
00454
00455 void _scv_expr::setBddVectorP(bddVectorT* v)
00456 {
00457 value.bddvec = v;
00458 }
00459
00460 void _scv_expr::setExtensionP(scv_extensions_if* s)
00461 {
00462 value.ext = s;
00463 }
00464
00465 void _scv_expr::setVecSize(int vsize)
00466 {
00467 vecsize = vsize;
00468 }
00469
00470 void _scv_expr::setBitWidth(int bw)
00471 {
00472 bit_width = bw;
00473 }
00474
00475 void _scv_expr::setSigWidth(int lsb, int msb)
00476 {
00477 sigLsb = lsb;
00478 sigMsb = msb;
00479 }
00480
00481 void _scv_expr::setSigMsb(int msb)
00482 {
00483 sigMsb = msb;
00484 }
00485
00486 void _scv_expr::setSigLsb(int lsb)
00487 {
00488 sigLsb = lsb;
00489 }
00490
00491 const _scv_expr::ExprValueType _scv_expr::get_type(void) const
00492 {
00493 return type;
00494 }
00495
00496 bddNodeT* _scv_expr::getBddNodeP(void) const
00497 {
00498 return value.bdd;
00499 }
00500
00501 long long _scv_expr::getIntValue(void) const
00502 {
00503 return value.ivalue;
00504 }
00505
00506 unsigned long long _scv_expr::getUnsignedValue(void) const
00507 {
00508 return value.uvalue;
00509 }
00510
00511 sc_bv_base _scv_expr::getBigValue(void) const
00512 {
00513 return *sc_data;
00514 }
00515
00516 bool _scv_expr::getBoolValue(void) const
00517 {
00518 return value.bvalue;
00519 }
00520
00521 double _scv_expr::getDoubleValue(void) const
00522 {
00523 return value.dvalue;
00524 }
00525
00526 char* _scv_expr::getStringValue(void) const
00527 {
00528 return value.cvalue;
00529 }
00530
00531 bddVectorT* _scv_expr::getBddVectorP(void) const
00532 {
00533 return value.bddvec;
00534 }
00535
00536 scv_extensions_if* _scv_expr::getExtensionP(void) const
00537 {
00538 return value.ext;
00539 }
00540
00541 int _scv_expr::getVecSize(void) const
00542 {
00543 if (type == BDDVECTOR || type == BDDVECTOR_SIGNED) {
00544 return vecsize;
00545 } else {
00546 _scv_constraint_error::internalError(
00547 "Accessing vector size for non-vector expression");
00548 return 0;
00549 }
00550 }
00551
00552 int _scv_expr::getBitWidth(void) const
00553 {
00554 return bit_width;
00555 }
00556
00557 int _scv_expr::getSigMsb(void) const
00558 {
00559 if (type == BDDVECTOR || type == BDDVECTOR_SIGNED) {
00560 return sigMsb;
00561 } else {
00562 _scv_constraint_error::internalError(
00563 "Accessing lsb for non-vector expression.");
00564 return 0;
00565 }
00566 }
00567
00568 int _scv_expr::getSigLsb(void) const
00569 {
00570 if (type == BDDVECTOR || type == BDDVECTOR_SIGNED) {
00571 return sigLsb;
00572 } else {
00573 _scv_constraint_error::internalError(
00574 "Accessing lsb for non-vector expression.");
00575 return 0;
00576 }
00577 }
00578
00580
00581
00582
00583
00584
00586
00587 _scv_constraint_manager::_scv_constraint_manager():maxTime(300),
00588 maxMemory(0), verboseLevel(0)
00589 {
00590 _mgr = new Cudd();
00591 nthvar = 0;
00592 multiple = 0;
00593 iteration = 1;
00594 maxvar = 1;
00595 numBddVar = 0;
00596 maxNumBits = 0;
00597 eProb = new int;
00598 mode = scv_extensions_if::RANDOM;
00599 numBitsUnsigned = 8 * sizeof(unsigned);
00600 numBitsSigned = 8 * sizeof(int);
00601 valueIndex = new unsigned[CUDD_MAXINDEX];
00602 oneNode = _mgr->bddOne().getNode();
00603 zeroNode = _mgr->bddZero().getNode();
00604 exprRepOne = new _scv_expr;
00605 exprRepZero = new _scv_expr;
00606
00607 extHash = new _scv_open_table<_smartDataRecordT>(SIZE_HINT);
00608 countExtHash = new _scv_associative_array<scv_extensions_if*, int>
00609 ("countExtHash", 0);
00610 avoidDuplicateHash = new _scv_associative_array<scv_constraint_base*,
00611 bddNodeT*> ("avoidDuplicateHash", 0);
00612 avoidDuplicateExtHash = new _scv_associative_array<scv_extensions_if*,
00613 bddNodeT*> ("avoidDuplicateExtHash", 0);
00614 nodeHashP = new _scv_open_table<int>(SIZE_HINT);
00615 nodeWeightHash = new _scv_associative_array<ddNodeT*, double>
00616 ("nodeWeightHash", 0);
00617 wasVisited = new _scv_associative_array<ddNodeT*, int>
00618 ("wasVisited", 0);
00619 enumVarList = new list<scv_extensions_if*>;
00620 }
00621
00622 _scv_constraint_manager::~ _scv_constraint_manager()
00623 {
00624 delete eProb;
00625 delete[] valueIndex;
00626 delete extHash;
00627 delete countExtHash;
00628 delete avoidDuplicateHash;
00629 delete avoidDuplicateExtHash;
00630 delete nodeHashP;
00631 delete nodeWeightHash;
00632 delete wasVisited;
00633 }
00634
00635 void _scv_constraint_manager::reset(void)
00636 {
00637 nthvar = 0;
00638 maxvar = 0;
00639 maxNumBits = 0;
00640 numBddVar = 0;
00641 countExtHash->clear();
00642 enumVarList->clear();
00643 }
00644
00645 bddNodeT& _scv_constraint_manager::get_bdd(scv_expression e, scv_constraint_base * c, bool hard_constraint)
00646 {
00647 bddNodeT* b = NULL;
00648 _scv_expr t1 = getExpressionRep(e);
00649 _scv_expr t2 = getBasicEnumConstraint();
00650 _scv_expr t = exprAnd(t1, t2);
00651 _scv_remove_data(&t1);
00652 _scv_remove_data(&t2);
00653 if (isOverConstrained(t)) {
00654 _scv_constraint_error::cannotMeetConstraint(c->get_name());
00655 b = getExprRepOne().getBddNodeP();
00656 } else {
00657 b = t.getBddNodeP();
00658 check_sparse_var(c, b);
00659 }
00660 return *b;
00661 }
00662
00663 void _scv_constraint_manager::add_sparse_var(scv_extensions_if* e, bddNodeT* b)
00664 {
00665 int size;
00666
00667 if (e->is_record()) {
00668 size = e->get_num_fields();
00669 for (int j=0; j<size; ++j) {
00670 scv_extensions_if *ef = e->get_field(j);
00671 add_sparse_var(ef, b);
00672 }
00673 } else if (e->is_array()) {
00674 size = e->get_array_size();
00675 for (int j=0; j<size; ++j) {
00676 scv_extensions_if *ef = e->get_array_elt(j);
00677 add_sparse_var(ef, b);
00678 }
00679 } else {
00680 int bitWidth=0;
00681 _smartDataRecordT * sDataP;
00682
00683 bitWidth = e->get_bitwidth();
00684
00685 if (verboseLevel > 3) {
00686 scv_out << "Extension: " << e << " BitWidth : " << bitWidth << " Has Extension: " << extHash->get(e, sDataP) << endl;
00687 }
00688
00689 if (extHash->get(e, sDataP)) {
00690 if (bitWidth < maxNumBits) {
00691 for (int j = bitWidth; j < maxNumBits; j++) {
00692 *b = (*b & (!_mgr->bddVar((maxvar*j)+sDataP->startIndex)));
00693 if (verboseLevel > 3) {
00694 scv_out << " EXTRA FOR SIZE: " << ((maxvar*j)+sDataP->startIndex) << endl;
00695 }
00696 }
00697 }
00698 }
00699 }
00700 }
00701
00702 void _scv_constraint_manager::check_sparse_var(scv_constraint_base * c, bddNodeT* b)
00703 {
00704 list<scv_smart_ptr_if*>::iterator i;
00705 list<scv_smart_ptr_if*>& pointers_ = c->get_members();
00706 scv_extensions_if* e = NULL;
00707
00708 for (i= pointers_.begin(); i != pointers_.end(); ++i) {
00709 e = (*i)->get_extensions_ptr();
00710 add_sparse_var(e, b);
00711 }
00712 }
00713
00714 void _scv_constraint_manager::init_bdd(scv_expression e, scv_constraint_base* c, bool hard_constraint)
00715 {
00716 c->set_expression_string(e.get_expression_string(), hard_constraint);
00717 initExpression(e);
00718 }
00719
00720 _scv_expr _scv_constraint_manager::simplifyMember(scv_extensions_if* e, _scv_expr t1, scv_constraint_base* c, bool& remove_, bool& over_constraint)
00721 {
00722 int size;
00723 _scv_expr tmp;
00724 if (e->is_record()) {
00725 size = e->get_num_fields();
00726 for (int j=0; j<size; ++j) {
00727 scv_extensions_if *ef = e->get_field(j);
00728 t1 = simplifyMember(ef, t1, c, remove_, over_constraint);
00729 if (over_constraint) break;
00730 }
00731 } else if (e->is_array()) {
00732 size = e->get_array_size();
00733 for (int j=0; j<size; ++j) {
00734 scv_extensions_if *ef = e->get_array_elt(j);
00735 t1 = simplifyMember(ef, t1, c, remove_, over_constraint);
00736 if (over_constraint) break;
00737 }
00738 } else {
00739 bool tmp_remove = false;
00740
00741 tmp = simplifyField(t1, e, tmp_remove);
00742 if (isOverConstrained(tmp)) {
00743 if (!c->is_ignored_soft_constraint()) {
00744 _scv_constraint_error::ignoredLevel(c->get_name());
00745 c->ignore_soft_constraint();
00746 if (tmp_remove) {
00747 _scv_remove_data(&tmp);
00748 }
00749 tmp.setBddNodeP(simplifyConstraint(c, remove_ ));
00750 } else {
00751 _scv_constraint_error::cannotMeetConstraint(c->get_name());
00752 over_constraint = true;
00753 }
00754 }
00755 if (tmp_remove && (t1.getBddNodeP() != &c->get_bdd())) {
00756 _scv_remove_data(&t1);
00757 }
00758 remove_ = tmp_remove;
00759 t1 = tmp;
00760 }
00761 return t1;
00762 }
00763
00764 bddNodeT* _scv_constraint_manager::simplifyConstraint(scv_constraint_base* c, bool& remove_, bddNodeT* b)
00765 {
00766 _scv_expr t1;
00767
00768 remove_ = false;
00769 bool over_constraint = false;
00770
00771 if (b) {
00772 t1.setBddNodeP(b);
00773 } else {
00774 t1.setBddNodeP(&(c->get_bdd()));
00775 }
00776 t1.setType(_scv_expr::BDD);
00777 list<scv_smart_ptr_if*>::iterator i;
00778 for (i= c->get_members().begin(); i != c->get_members().end(); ++i) {
00779 scv_extensions_if* e = (*i)->get_extensions_ptr();
00780 t1 = simplifyMember(e, t1, c, remove_, over_constraint);
00781 if (over_constraint) break;
00782 }
00783 return t1.getBddNodeP();
00784 }
00785
00786 _scv_expr _scv_constraint_manager::simplifyField(_scv_expr t1, scv_extensions_if* s, bool & remove_)
00787 {
00788 _scv_expr expr;
00789
00790 _smartDataRecordT * sDataP;
00791
00792 if (!extHash->get(s, sDataP)) {
00793 return t1;
00794 }
00795
00796 if (s->is_initialized()) {
00797 switch(s->get_type()) {
00798 case scv_extensions_if::INTEGER:
00799 case scv_extensions_if::ENUMERATION:
00800 {
00801 int bitwidth = s->get_bitwidth();
00802 if (bitwidth <= 64) {
00803 long long value = s->get_integer();
00804 expr = exprEqual(createExprRep(s), createExprRep(value, bitwidth));
00805 } else {
00806 sc_bv_base value(bitwidth);
00807 s->get_value(value);
00808 expr = exprEqual(createExprRep(s), createExprRep(value, bitwidth));
00809 }
00810 t1 = exprAnd(t1, expr);
00811 _scv_remove_data(&expr);
00812 remove_ = true;
00813 break;
00814 }
00815 case scv_extensions_if::UNSIGNED:
00816 case scv_extensions_if::BIT_VECTOR:
00817 case scv_extensions_if::LOGIC_VECTOR:
00818 {
00819 int bitwidth = s->get_bitwidth();
00820 if (bitwidth <= 64) {
00821 unsigned long long value = s->get_unsigned();
00822 expr = exprEqual(createExprRep(s), createExprRep(value, bitwidth));
00823 } else {
00824 sc_bv_base value(bitwidth);
00825 s->get_value(value);
00826 expr = exprEqual(createExprRep(s), createExprRep(value, bitwidth));
00827 }
00828 t1 = exprAnd(t1, expr);
00829 _scv_remove_data(&expr);
00830 remove_ = true;
00831 break;
00832 }
00833 case scv_extensions_if::BOOLEAN:
00834 {
00835 bool value = s->get_bool();
00836 int bitwidth = s->get_bitwidth();
00837 expr = exprEqual(createExprRep(s), createExprRep(value, bitwidth));
00838 t1 = exprAnd(t1, expr);
00839 _scv_remove_data(&expr);
00840 remove_ = true;
00841 break;
00842 }
00843 default: {
00844 _scv_constraint_error::notImplementedYet(s->get_type_name());
00845 break;
00846 }
00847 }
00848 }
00849 return t1;
00850 }
00851
00852 void _scv_constraint_manager::assignRandomValue(scv_extensions_if* s, bddNodeT * b, scv_shared_ptr<scv_random> g)
00853 {
00854 _smartDataRecordT * sDataP;
00855
00856 setRandomGen(g);
00857
00858 if (!extHash->get(s, sDataP)) {
00859 if (!s->is_initialized()) {
00860 s->next();
00861 }
00862 } else {
00863 int msize = getSizeOfBddVec(s);
00864 for (int i=0; i<msize; i++) {
00865 valueIndex[i*sDataP->numvar+sDataP->startIndex] = 2;
00866 }
00867 getVectorFromBdd(b->getNode());
00868 setValue(s, sDataP->startIndex, msize, sDataP->numvar);
00869 }
00870 }
00871
00872 void _scv_constraint_manager::assignRandomValue(scv_extensions_if* s, bddNodeT * b, _scv_constraint_data * cdata_)
00873 {
00874 scv_shared_ptr<scv_random> g = cdata_->get_random(s);
00875 _smartDataRecordT * sDataP;
00876
00877 setRandomGen(g);
00878
00879 if (!extHash->get(s, sDataP)) {
00880 if (!s->is_initialized()) {
00881 generate_value_range_constraint(s, cdata_);
00882 }
00883 } else {
00884 int msize = getSizeOfBddVec(s);
00885 bool _avoid_duplicate = false;
00886 bool remove_ = false;
00887 bddNodeT* sb;
00888 _scv_expr t, expr, expr_n, expr_f;
00889
00890 scv_constraint_base* c = cdata_->get_constraint();
00891
00892 if (cdata_->is_scan_mode()) {
00893 mode = scv_extensions_if::SCAN;
00894 randomnext = cdata_->prev_val_.unsigned_;
00895 randomnext = randomnext << 1;
00896 cdata_->prev_val_.unsigned_ += cdata_->lb_scan_;
00897 } else if (cdata_->is_avoid_duplicate_mode()) {
00898 _avoid_duplicate = true;
00899
00900 sb = avoidDuplicateHash->getValue(c);
00901 if (sb != &c->get_bdd()) {
00902 remove_ = true;
00903 }
00904 t.setBddNodeP(sb);
00905 t.setType(_scv_expr::BDD);
00906
00907 } else {
00908 mode = scv_extensions_if::RANDOM;
00909 }
00910
00911 for (int i=0; i<msize; i++) {
00912 valueIndex[i*sDataP->numvar+sDataP->startIndex] = 2;
00913 }
00914
00915 getVectorFromBdd(b->getNode());
00916
00917 if (_avoid_duplicate) {
00918 expr = assignValueMember(s, _avoid_duplicate);
00919
00920 expr_n = exprNot(expr);
00921 expr_f = exprAnd(t, expr_n);
00922
00923 _scv_remove_data(&expr_n);
00924 _scv_remove_data(&expr);
00925 if (isOverConstrained(expr_f)) {
00926 avoidDuplicateHash->insert(c, 0);
00927 } else {
00928 avoidDuplicateHash->insert(c, expr_f.getBddNodeP());
00929 }
00930 if (remove_) {
00931 _scv_remove_data(&t);
00932 }
00933 } else {
00934 setValue(s, sDataP->startIndex, msize, sDataP->numvar);
00935 }
00936 }
00937 }
00938
00939 void _scv_constraint_manager::initMember(scv_extensions_if* e)
00940 {
00941 _smartDataRecordT * sDataP=NULL;
00942 if (e->is_record()) {
00943 int size = e->get_num_fields();
00944
00945 for (int j=0; j < size; j++) {
00946 scv_extensions_if* ef = e->get_field(j);
00947 initMember(ef);
00948 }
00949 } else if (e->is_array()) {
00950 int size = e->get_array_size();
00951
00952 for (int j=0; j < size; j++) {
00953 scv_extensions_if* ef = e->get_array_elt(j);
00954 initMember(ef);
00955 }
00956 } else {
00957 _scv_constraint_data::gen_mode gen_mode =
00958 e->get_constraint_data()->get_mode();
00959
00960 if ( (gen_mode == _scv_constraint_data::DISTRIBUTION) ||
00961 (gen_mode == _scv_constraint_data::DISTRIBUTION_RANGE) ||
00962 (gen_mode == _scv_constraint_data::RANGE_CONSTRAINT)) {
00963
00964 if (verboseLevel > 3) {
00965 scv_out << " Doing Initialize on: " << e->get_name() << endl;
00966 }
00967 e->initialize();
00968 }
00969 if (extHash->get(e, sDataP)) {
00970
00971 if ( !(gen_mode == _scv_constraint_data::DISTRIBUTION) ||
00972 (gen_mode == _scv_constraint_data::DISTRIBUTION_RANGE) ||
00973 (gen_mode == _scv_constraint_data::RANGE_CONSTRAINT)) {
00974 int msize = getSizeOfBddVec(e);
00975 for (int k=0; k<msize; k++) {
00976 valueIndex[k*sDataP->numvar+sDataP->startIndex] = 2;
00977 }
00978 }
00979 }
00980 }
00981 return;
00982 }
00983
00984 _scv_expr _scv_constraint_manager::assignValueMember(scv_extensions_if* e, bool _avoid_duplicate)
00985 {
00986 _smartDataRecordT * sDataP=NULL;
00987 _scv_expr expr;
00988 _scv_expr tmp;
00989 _scv_expr tmp1;
00990
00991 if (_avoid_duplicate) {
00992 expr = getExprRepOne();
00993 }
00994
00995 if (e->is_record()) {
00996 int size = e->get_num_fields();
00997
00998 for (int j=0; j < size; j++) {
00999 scv_extensions_if* ef = e->get_field(j);
01000 if (_avoid_duplicate) {
01001 tmp1 = expr;
01002 }
01003 tmp = assignValueMember(ef, _avoid_duplicate);
01004 if (_avoid_duplicate) {
01005 expr = exprAnd(expr, tmp);
01006 _scv_remove_data(&tmp);
01007 _scv_remove_data(&tmp1);
01008 }
01009 }
01010 } else if (e->is_array()) {
01011 int size = e->get_array_size();
01012 for (int j=0; j < size; j++) {
01013 scv_extensions_if* ef = e->get_array_elt(j);
01014 if (_avoid_duplicate) {
01015 tmp1 = expr;
01016 }
01017 tmp = assignValueMember(ef, _avoid_duplicate);
01018 if (_avoid_duplicate) {
01019 expr = exprAnd(expr, tmp);
01020 _scv_remove_data(&tmp);
01021 _scv_remove_data(&tmp1);
01022 }
01023 }
01024 } else {
01025 bool should_remove=false;
01026
01027 if (extHash->get(e, sDataP)) {
01028 if (!e->is_initialized()) {
01029 int msize = getSizeOfBddVec(e);
01030 setValue(e, sDataP->startIndex, msize, sDataP->numvar);
01031 if (_avoid_duplicate) {
01032 tmp = simplifyField(expr, e, should_remove);
01033 _scv_remove_data(&expr);
01034 expr = tmp;
01035 }
01036 }
01037 } else {
01038 if (!e->is_initialized()) {
01039 e->next();
01040 }
01041 }
01042 }
01043 return expr;
01044 }
01045
01046 void _scv_constraint_manager::assignRandomValue(scv_constraint_base* c, bool simplify)
01047 {
01048 list<scv_smart_ptr_if*>::iterator i;
01049
01050 list<scv_smart_ptr_if*>& pointers_ = c->get_members();
01051
01052 setRandomGen(c->get_random());
01053
01054 for (i= pointers_.begin(); i != pointers_.end(); ++i) {
01055 scv_extensions_if* e = (*i)->get_extensions_ptr();
01056 initMember(e);
01057 }
01058
01059 bool remove_ = false;
01060 bool _avoid_duplicate = false;
01061 _scv_expr t;
01062 bddNodeT* sb;
01063 _scv_expr expr;
01064 _scv_expr tmp, tmp1;
01065
01066 _avoid_duplicate = _scv_is_avoid_duplicate(c);
01067 if (_avoid_duplicate) {
01068 remove_ = true;
01069 sb = avoidDuplicateHash->getValue(c);
01070 if (!sb) {
01071 if (simplify) {
01072 sb = simplifyConstraint(c, remove_);
01073 } else {
01074 sb = &c->get_bdd();
01075 remove_ = false;
01076 }
01077 }
01078 } else {
01079 if (simplify) {
01080 sb = simplifyConstraint(c, remove_);
01081 } else {
01082 sb = &c->get_bdd();
01083 }
01084 }
01085 t.setBddNodeP(sb);
01086 t.setType(_scv_expr::BDD);
01087
01088 if (_scv_is_scan(c)) {
01089 mode = scv_extensions_if::SCAN;
01090 randomnext = (unsigned int)c->get_scan_counter();
01091 randomnext = randomnext << 1;
01092 } else {
01093 mode = scv_extensions_if::RANDOM;
01094 }
01095
01096 getVectorFromBdd(sb->getNode());
01097
01098 if (_avoid_duplicate) {
01099 expr = getExprRepOne();
01100 }
01101
01102 for (i= pointers_.begin(); i != pointers_.end(); ++i) {
01103 scv_extensions_if* e = (*i)->get_extensions_ptr();
01104 _scv_constraint_data::gen_mode gen_mode = e->get_constraint_data()->get_mode();
01105
01106 if ( !((gen_mode == _scv_constraint_data::DISTRIBUTION) ||
01107 (gen_mode == _scv_constraint_data::DISTRIBUTION_RANGE) ||
01108 (gen_mode == _scv_constraint_data::RANGE_CONSTRAINT)) ) {
01109
01110 if (_avoid_duplicate) {
01111 tmp1 = expr;
01112 }
01113
01114 tmp = assignValueMember(e, _avoid_duplicate);
01115
01116 if (_avoid_duplicate) {
01117 expr = exprAnd(expr, tmp);
01118 _scv_remove_data(&tmp1);
01119 _scv_remove_data(&tmp);
01120 }
01121 }
01122 }
01123
01124 if (_avoid_duplicate) {
01125 _scv_expr expr_n = exprNot(expr);
01126 _scv_expr expr_f = exprAnd(t, expr_n);
01127
01128 _scv_remove_data(&expr_n);
01129 _scv_remove_data(&expr);
01130 if (isOverConstrained(expr_f)) {
01131 avoidDuplicateHash->insert(c, 0);
01132 _scv_remove_data(&expr_f);
01133 } else {
01134 avoidDuplicateHash->insert(c, expr_f.getBddNodeP());
01135 }
01136 }
01137
01138 if (remove_) {
01139 _scv_remove_data(&t);
01140 }
01141
01142 }
01143
01144 void _scv_constraint_manager::wrapup(scv_extensions_if* s)
01145 {
01146 _smartDataRecordT* sDataP;
01147
01148 if (s->is_record()) {
01149 int size = s->get_num_fields();
01150 for (int i=0; i<size; ++i) {
01151 scv_extensions_if* ef = s->get_field(i);
01152 wrapup(ef);
01153 }
01154 } else if (s->is_array()) {
01155 int size = s->get_array_size();
01156 for (int i=0; i<size; ++i) {
01157 scv_extensions_if* ef = s->get_array_elt(i);
01158 wrapup(ef);
01159 }
01160 } else {
01161 if (extHash->get(s, sDataP)) {
01162 extHash->remove(s);
01163 }
01164 }
01165 }
01166
01167 void _scv_constraint_manager::add_extension(scv_extensions_if* s)
01168 {
01169 if (s->is_record()) {
01170 int size = s->get_num_fields();
01171 for (int i=0; i<size; ++i) {
01172 add_extension(s->get_field(i));
01173 }
01174 } else if (s->is_array()) {
01175 int size = s->get_array_size();
01176 for (int i=0; i<size; ++i) {
01177 add_extension(s->get_array_elt(i));
01178 }
01179 } else {
01180 createExprRep(s);
01181 }
01182 }
01183
01184 bool _scv_constraint_manager::has_complex_constraint(scv_extensions_if* s)
01185
01186 {
01187 _smartDataRecordT * sDataP = NULL;
01188 return extHash->get(s, sDataP);
01189 }
01190
01191 _scv_expr _scv_constraint_manager::getConstantExprRep(_scv_expr e, int signExtend) const
01192 {
01193 unsigned binVal[1024];
01194 _scv_expr newElem;
01195
01196
01197 int msize = e.getBitWidth();
01198 int resultSize = msize;
01199
01200 if (signExtend != -1 && msize != signExtend) {
01201 resultSize = (msize >= signExtend) ? msize : signExtend;
01202 }
01203
01204 bddVectorT *vec = new bddVectorT(resultSize);
01205 bddVectorT& vecR = *vec;
01206
01207 switch(e.get_type()) {
01208 case _scv_expr::UNSIGNED :
01209 case _scv_expr::SC_BV_BASE :
01210 case _scv_expr::UNSIGNED_64BIT : {
01211 getVector(e, binVal);
01212 for (int i=0; i<resultSize; i++) {
01213 if (i >= msize) {
01214 vecR[i] = _mgr->bddZero();
01215 } else {
01216 if (binVal[i] == 0) {
01217 vecR[i] = _mgr->bddZero();
01218 } else if (binVal[i] == 1) {
01219 vecR[i] = _mgr->bddOne();
01220 }
01221 }
01222 }
01223 newElem.setType(_scv_expr::BDDVECTOR);
01224 newElem.setBddVectorP(vec);
01225 newElem.setVecSize(resultSize);
01226 break;
01227 }
01228 case _scv_expr::INT :
01229 case _scv_expr::BOOLEAN : {
01230 getVector(e, binVal);
01231 for (int i=0; i<resultSize; i++) {
01232 if (i >= msize) {
01233 if (binVal[msize-1] == 1) {
01234 vecR[i] = _mgr->bddOne();
01235 } else {
01236 vecR[i] = _mgr->bddZero();
01237 }
01238 } else {
01239 if (binVal[i] == 0) {
01240 vecR[i] = _mgr->bddZero();
01241 } else if (binVal[i] == 1) {
01242 vecR[i] = _mgr->bddOne();
01243 }
01244 }
01245 }
01246 newElem.setType(_scv_expr::BDDVECTOR);
01247 newElem.setBddVectorP(vec);
01248 newElem.setVecSize(resultSize);
01249 break;
01250 }
01251 case _scv_expr::DOUBLE :
01252 case _scv_expr::STRING :
01253 case _scv_expr::RECORD :
01254 case _scv_expr::ARRAY : {
01255 break;
01256 }
01257 default:
01258 _scv_constraint_error::internalError(
01259 "Unknown expression type- getConstantExprRep.\n");
01260 break;
01261 }
01262 return newElem;
01263 }
01264
01265 void _scv_constraint_manager::countMaxVar(const scv_expression& e)
01266 {
01267 switch (e.get_operator()) {
01268 case scv_expression::EMPTY:
01269 break;
01270 case scv_expression::INT_CONSTANT:
01271 case scv_expression::UNSIGNED_CONSTANT:
01272 case scv_expression::DOUBLE_CONSTANT:
01273 case scv_expression::BOOLEAN_CONSTANT:
01274 return;
01275 case scv_expression::EXTENSION:
01276 checkExprRep(e.get_extension());
01277 return;
01278 case scv_expression::PLUS:
01279 case scv_expression::MINUS:
01280 case scv_expression::MULTIPLY:
01281 case scv_expression::EQUAL:
01282 case scv_expression::NOT_EQUAL:
01283 case scv_expression::GREATER_THAN:
01284 case scv_expression::LESS_THAN:
01285 case scv_expression::GREATER_OR_EQUAL:
01286 case scv_expression::LESS_OR_EQUAL:
01287 case scv_expression::AND:
01288 case scv_expression::OR:
01289 countMaxVar(e.get_left());
01290 countMaxVar(e.get_right());
01291 return;
01292 case scv_expression::NOT:
01293 countMaxVar(e.get_left());
01294 return;
01295 default:
01296 _scv_constraint_error::internalError(
01297 "init bdd for unknown operator type in countMaxVar\n");
01298 break;
01299 }
01300 return;
01301 }
01302
01303 void _scv_constraint_manager::initExpression(const scv_expression& e)
01304 {
01305 switch (e.get_operator()) {
01306 case scv_expression::EMPTY:
01307 break;
01308 case scv_expression::INT_CONSTANT:
01309 case scv_expression::UNSIGNED_CONSTANT:
01310 case scv_expression::DOUBLE_CONSTANT:
01311 case scv_expression::BOOLEAN_CONSTANT:
01312 return;
01313 case scv_expression::EXTENSION:
01314 createExprRep(e.get_extension());
01315 return;
01316 case scv_expression::PLUS:
01317 case scv_expression::MINUS:
01318 case scv_expression::MULTIPLY:
01319 case scv_expression::EQUAL:
01320 case scv_expression::NOT_EQUAL:
01321 case scv_expression::GREATER_THAN:
01322 case scv_expression::LESS_THAN:
01323 case scv_expression::GREATER_OR_EQUAL:
01324 case scv_expression::LESS_OR_EQUAL:
01325 case scv_expression::AND:
01326 case scv_expression::OR:
01327 initExpression(e.get_left());
01328 initExpression(e.get_right());
01329 return;
01330 case scv_expression::NOT:
01331 initExpression(e.get_left());
01332 return;
01333 default:
01334 _scv_constraint_error::internalError(
01335 "init bdd for unknown operator type in initExpression\n");
01336 break;
01337 }
01338 return;
01339 }
01340
01341 _scv_expr _scv_constraint_manager::getExpressionRep(const scv_expression& e)
01342 {
01343 switch (e.get_operator()) {
01344 case scv_expression::EMPTY:
01345 _scv_constraint_error::internalError(
01346 "Generating internal BddRep for an EMPTY scv_expression");
01347 break;
01348 case scv_expression::INT_CONSTANT:
01349 return createExprRep(e.get_int_value(), e.get_bit_width());
01350 case scv_expression::UNSIGNED_CONSTANT:
01351 return createExprRep(e.get_unsigned_value(), e.get_bit_width());
01352 case scv_expression::SC_BIGINT_CONSTANT:
01353 case scv_expression::SC_BIGUINT_CONSTANT:
01354 case scv_expression::SC_BV_CONSTANT: {
01355 sc_bv_base val(e.get_bit_width());
01356 e.get_value(val);
01357 return createExprRep(val, e.get_bit_width());
01358 }
01359 case scv_expression::BOOLEAN_CONSTANT:
01360 return createExprRep(e.get_bool_value(), e.get_bit_width());
01361 case scv_expression::DOUBLE_CONSTANT:
01362 return createExprRep(e.get_double_value());
01363 case scv_expression::EXTENSION:
01364 return createExprRep(e.get_extension());
01365 case scv_expression::PLUS:
01366 {
01367 _scv_expr t1 = getExpressionRep(e.get_left());
01368 _scv_expr t2 = getExpressionRep(e.get_right());
01369 _scv_expr t3 = exprPlus(t1, t2);
01370 _scv_remove_if_bdd(&t1);
01371 _scv_remove_if_bdd(&t2);
01372 return t3;
01373 }
01374 case scv_expression::MINUS:
01375 {
01376 _scv_expr t1 = getExpressionRep(e.get_left());
01377 _scv_expr t2 = getExpressionRep(e.get_right());
01378 _scv_expr t3 = exprMinus(t1, t2);
01379 _scv_remove_if_bdd(&t1);
01380 _scv_remove_if_bdd(&t2);
01381 return t3;
01382 }
01383 case scv_expression::MULTIPLY:
01384 {
01385 _scv_expr t1 = getExpressionRep(e.get_left());
01386 _scv_expr t2 = getExpressionRep(e.get_right());
01387 _scv_expr t3 = exprMultiply(t1, t2);
01388 _scv_remove_if_bdd(&t1);
01389 _scv_remove_if_bdd(&t2);
01390 return t3;
01391 }
01392 case scv_expression::EQUAL:
01393 {
01394 _scv_expr t1 = getExpressionRep(e.get_left());
01395 _scv_expr t2 = getExpressionRep(e.get_right());
01396 _scv_expr t3 = exprEqual(t1, t2);
01397 _scv_remove_if_bdd(&t1);
01398 _scv_remove_if_bdd(&t2);
01399 return t3;
01400 }
01401 case scv_expression::NOT_EQUAL:
01402 {
01403 _scv_expr t1 = getExpressionRep(e.get_left());
01404 _scv_expr t2 = getExpressionRep(e.get_right());
01405 _scv_expr t3 = exprNEqual(t1, t2);
01406 _scv_remove_if_bdd(&t1);
01407 _scv_remove_if_bdd(&t2);
01408 return t3;
01409 }
01410 case scv_expression::GREATER_THAN:
01411 {
01412 _scv_expr t1 = getExpressionRep(e.get_left());
01413 _scv_expr t2 = getExpressionRep(e.get_right());
01414 _scv_expr t3 = exprGThan(t1, t2);
01415 _scv_remove_if_bdd(&t1);
01416 _scv_remove_if_bdd(&t2);
01417 return t3;
01418 }
01419 case scv_expression::LESS_THAN:
01420 {
01421 _scv_expr t1 = getExpressionRep(e.get_left());
01422 _scv_expr t2 = getExpressionRep(e.get_right());
01423 _scv_expr t3 = exprLThan(t1, t2);
01424 _scv_remove_if_bdd(&t1);
01425 _scv_remove_if_bdd(&t2);
01426 return t3;
01427 }
01428 case scv_expression::GREATER_OR_EQUAL:
01429 {
01430 _scv_expr t1 = getExpressionRep(e.get_left());
01431 _scv_expr t2 = getExpressionRep(e.get_right());
01432 _scv_expr t3 = exprGEq(t1, t2);
01433 _scv_remove_if_bdd(&t1);
01434 _scv_remove_if_bdd(&t2);
01435 return t3;
01436 }
01437 case scv_expression::LESS_OR_EQUAL:
01438 {
01439 _scv_expr t1 = getExpressionRep(e.get_left());
01440 _scv_expr t2 = getExpressionRep(e.get_right());
01441 _scv_expr t3 = exprLEq(t1, t2);
01442 _scv_remove_if_bdd(&t1);
01443 _scv_remove_if_bdd(&t2);
01444 return t3;
01445 }
01446 case scv_expression::AND:
01447 {
01448 _scv_expr t1 = getExpressionRep(e.get_left());
01449 _scv_expr t2 = getExpressionRep(e.get_right());
01450 _scv_expr t3 = exprAnd(t1, t2);
01451 _scv_remove_if_bdd(&t1);
01452 _scv_remove_if_bdd(&t2);
01453 return t3;
01454 }
01455 case scv_expression::OR:
01456 {
01457 _scv_expr t1 = getExpressionRep(e.get_left());
01458 _scv_expr t2 = getExpressionRep(e.get_right());
01459 _scv_expr t3 = exprOr(t1, t2);
01460 _scv_remove_if_bdd(&t1);
01461 _scv_remove_if_bdd(&t2);
01462 return t3;
01463 }
01464 case scv_expression::NOT:
01465 {
01466 _scv_expr t1 = getExpressionRep(e.get_left());
01467 _scv_expr t3 = exprNot(t1);
01468 _scv_remove_if_bdd(&t1);
01469 return t3;
01470 }
01471 default:
01472 _scv_constraint_error::internalError(
01473 "creating bdd for unknown operator type in getExpressionRep\n");
01474 break;
01475 }
01476 return *errorExprP;
01477 }
01478
01479 _scv_expr _scv_constraint_manager::getExprRepZero(void)
01480 {
01481 bddNodeT* internalZero = new bddNodeT;
01482 *internalZero = _mgr->bddZero();
01483 exprRepZero->setType(_scv_expr::BDD);
01484 exprRepZero->setBddNodeP(internalZero);
01485 return *exprRepZero;
01486 }
01487
01488 _scv_expr _scv_constraint_manager::getExprRepOne(void)
01489 {
01490 bddNodeT* internalOne = new bddNodeT;
01491 *internalOne = _mgr->bddOne();
01492 exprRepOne->setType(_scv_expr::BDD);
01493 exprRepOne->setBddNodeP(internalOne);
01494 return *exprRepOne;
01495 }
01496
01497 _scv_expr _scv_constraint_manager::exprAnd(const _scv_expr& e1, const _scv_expr& e2)
01498 {
01499 _scv_expr newElem;
01500
01501 if (e1.isBdd() && e2.isBdd()) {
01502 bddNodeT *tmp = new bddNodeT;
01503 *tmp = (*(e1.getBddNodeP()) & *(e2.getBddNodeP()));
01504 newElem.setType(_scv_expr::BDD);
01505 newElem.setBddNodeP(tmp);
01506 } else if (e1.isBddVector() && e2.isBddVector()) {
01507 newElem = _exprAnd(e1, e2);
01508 } else if (e1.isBdd() && e2.isBddVector()) {
01509 bddNodeT *tmp = new bddNodeT;
01510 _scv_expr tmpElem;
01511 tmpElem = _exprAnd(e2, e2);
01512 *tmp = (*(e1.getBddNodeP()) & *tmpElem.getBddNodeP());
01513 newElem.setType(_scv_expr::BDD);
01514 newElem.setBddNodeP(tmp);
01515 _scv_remove_data(&tmpElem);
01516 } else if (e1.isBddVector() && e2.isBdd()) {
01517 bddNodeT *tmp = new bddNodeT;
01518 _scv_expr tmpElem;
01519 tmpElem = _exprAnd(e1, e1);
01520 *tmp = (*(e2.getBddNodeP()) & *tmpElem.getBddNodeP());
01521 newElem.setType(_scv_expr::BDD);
01522 newElem.setBddNodeP(tmp);
01523 _scv_remove_data(&tmpElem);
01524 } else if (e1.isBddVector() && e2.isConstant()) {
01525 _scv_expr constElem = getConstantExprRep(e2);
01526 newElem = _exprAnd(e1, constElem);
01527 delete constElem.getBddVectorP();
01528 } else if (e1.isConstant() && e2.isBddVector()) {
01529 _scv_expr constElem = getConstantExprRep(e1);
01530 newElem = _exprAnd(constElem, e2);
01531 delete constElem.getBddVectorP();
01532 } else if (e1.isConstant() && e2.isConstant()) {
01533 _scv_expr constElem1 = getConstantExprRep(e1);
01534 _scv_expr constElem2 = getConstantExprRep(e2);
01535 newElem = _exprAnd(constElem1, constElem2);
01536 delete constElem1.getBddVectorP();
01537 delete constElem2.getBddVectorP();
01538 } else if (e1.isConstant() && e2.isBdd()) {
01539 newElem = e2;
01540 bddNodeT *tmp = new bddNodeT;
01541 *tmp = *e2.getBddNodeP();
01542 newElem.setBddNodeP(tmp);
01543 } else if (e1.isBdd() && e2.isConstant()) {
01544 newElem = e1;
01545 bddNodeT *tmp = new bddNodeT;
01546 *tmp = *e1.getBddNodeP();
01547 newElem.setBddNodeP(tmp);
01548 } else if (e1.isDouble() || e2.isDouble()) {
01549 _scv_constraint_error::ignoreDoubleConstraints();
01550 newElem = getExprRepOne();
01551 } else if (e1.isString() || e2.isString()) {
01552 _scv_constraint_error::ignoreDoubleConstraints();
01553 newElem = getExprRepOne();
01554 } else if (e1.isEmpty() || e2.isEmpty()) {
01555 } else {
01556 _scv_constraint_error::internalError(
01557 "Trying exprAnd for unknown types\n");
01558 }
01559 return newElem;
01560 }
01561
01562 _scv_expr _scv_constraint_manager::_exprAnd(const _scv_expr& e1, const _scv_expr& e2)
01563 {
01564 _scv_expr newElem;
01565 bddNodeT *tmp = new bddNodeT;
01566 bddNodeT tmpE1;
01567 bddNodeT tmpE2;
01568
01569 bddVectorT *e1Vec = e1.getBddVectorP();
01570 bddVectorT *e2Vec = e2.getBddVectorP();
01571 int e1size = e1.getVecSize();
01572 int e2size = e2.getVecSize();
01573
01574 bddVectorT& e1VecR = *e1Vec;
01575 bddVectorT& e2VecR = *e2Vec;
01576
01577 tmpE1 = _mgr->bddZero();
01578 tmpE2 = _mgr->bddZero();
01579
01580 for (int i=0; i<e1size; ++i) {
01581 tmpE1 = tmpE1 | e1VecR[i];
01582 }
01583 for (int i=0; i<e2size; ++i) {
01584 tmpE2 = tmpE2 | e2VecR[i];
01585 }
01586 *tmp = (tmpE1 & tmpE2);
01587 newElem.setType(_scv_expr::BDD);
01588 newElem.setBddNodeP(tmp);
01589 return newElem;
01590 }
01591
01592 _scv_expr _scv_constraint_manager::exprOr(const _scv_expr& e1, const _scv_expr& e2)
01593 {
01594 _scv_expr newElem;
01595
01596 if (e1.isBdd() && e2.isBdd()) {
01597 bddNodeT *tmp = new bddNodeT;
01598 *tmp = (*(e1.getBddNodeP()) | *(e2.getBddNodeP()));
01599 newElem.setType(_scv_expr::BDD);
01600 newElem.setBddNodeP(tmp);
01601 } else if (e1.isBddVector() && e2.isBddVector()) {
01602 newElem = _exprOr(e1, e2);
01603 } else if (e1.isBdd() && e2.isBddVector()) {
01604 bddNodeT *tmp = new bddNodeT;
01605 _scv_expr tmpElem;
01606 tmpElem = _exprOr(e2, e2);
01607 *tmp = (*(e1.getBddNodeP()) | *tmpElem.getBddNodeP());
01608 newElem.setType(_scv_expr::BDD);
01609 newElem.setBddNodeP(tmp);
01610 _scv_remove_data(&tmpElem);
01611 } else if (e1.isBddVector() && e2.isBdd()) {
01612 bddNodeT *tmp = new bddNodeT;
01613 _scv_expr tmpElem;
01614 tmpElem = _exprOr(e1, e1);
01615 *tmp = (*(e2.getBddNodeP()) | *tmpElem.getBddNodeP());
01616 newElem.setType(_scv_expr::BDD);
01617 newElem.setBddNodeP(tmp);
01618 _scv_remove_data(&tmpElem);
01619 } else if (e1.isBddVector() && e2.isConstant()) {
01620 _scv_expr constElem = getConstantExprRep(e2);
01621 newElem = _exprOr(e1, constElem);
01622 delete constElem.getBddVectorP();
01623 } else if (e1.isConstant() && e2.isBddVector()) {
01624 _scv_expr constElem = getConstantExprRep(e1);
01625 newElem = _exprOr(constElem, e2);
01626 delete constElem.getBddVectorP();
01627 } else if (e1.isConstant() && e2.isConstant()) {
01628 _scv_expr constElem1 = getConstantExprRep(e1);
01629 _scv_expr constElem2 = getConstantExprRep(e2);
01630 newElem = _exprOr(constElem1, constElem2);
01631 delete constElem1.getBddVectorP();
01632 delete constElem2.getBddVectorP();
01633 } else if (e1.isDouble() || e2.isDouble()) {
01634 _scv_constraint_error::ignoreDoubleConstraints();
01635 newElem = getExprRepOne();
01636 }