示例#1
0
// Constraints up to but not including iterator it have been unified.
// The current solution is soln
// The set of all solutions is in solns
bool Constraints::doSolve(std::list<Exp*>::iterator it, ConstraintMap& soln, std::list<ConstraintMap>& solns) {
LOG << "Begin doSolve at level " << ++level << "\n";
LOG << "Soln now: " << soln.prints() << "\n";
	if (it == disjunctions.end()) {
		// We have gotten to the end with no unification failures
		// Copy the current set of constraints as a solution
		//if (soln.size() == 0)
			// Awkward. There is a trivial solution, but we have no constraints
			// So make a constraint of always-true
			//soln.insert(new Terminal(opTrue));
		// Copy the fixed constraints
		soln.makeUnion(fixed);
		solns.push_back(soln);
LOG << "Exiting doSolve at level " << level-- << " returning true\n";
		return true;
	}

	Exp* dj = *it;
	// Iterate through each disjunction d of dj
	Exp* rem1 = dj;		  // Remainder
	bool anyUnified = false;
	Exp* d;
	while ((d = nextDisjunct(rem1)) != NULL) {
LOG << " $$ d is " << d << ", rem1 is " << ((rem1==0)?"NULL":rem1->prints()) << " $$\n";
		// Match disjunct d against the fixed types; it could be compatible,
		// compatible and generate an additional constraint, or be
		// incompatible
		ConstraintMap extra;	  // Just for this disjunct
		Exp* c;
		Exp* rem2 = d;
		bool unified = true;
		while ((c = nextConjunct(rem2)) != NULL) {
LOG << "   $$ c is " << c << ", rem2 is " << ((rem2==0)?"NULL":rem2->prints()) << " $$\n";
			if (c->isFalse()) {
				unified = false;
				break;
			}
			assert(c->isEquality());
			Exp* lhs = ((Binary*)c)->getSubExp1();
			Exp* rhs = ((Binary*)c)->getSubExp2();
			extra.insert(lhs, rhs);
			ConstraintMap::iterator kk;
			kk = fixed.find(lhs);
			if (kk != fixed.end()) {
				unified &= unify(rhs, kk->second, extra);
LOG << "Unified now " << unified << "; extra now " << extra.prints() << "\n";
				if (!unified) break;
			}
		}
		if (unified)
			// True if any disjuncts had all the conjuncts satisfied
			anyUnified = true;
		if (!unified) continue;
		// Use this disjunct
		// We can't just difference out extra if this fails; it may remove
		// elements from soln that should not be removed
		// So need a copy of the old set in oldSoln
		ConstraintMap oldSoln = soln;
		soln.makeUnion(extra);
		doSolve(++it, soln, solns);
		// Revert to the previous soln (whether doSolve returned true or not)
		// If this recursion did any good, it will have gotten to the end and
		// added the resultant soln to solns
		soln = oldSoln;
LOG << "After doSolve returned: soln back to: " << soln.prints() << "\n";
		// Back to the current disjunction
		it--;
		// Continue for more disjuncts this disjunction
	}
	// We have run out of disjuncts. Return true if any disjuncts had no
	// unification failures
LOG << "Exiting doSolve at level " << level-- << " returning " << anyUnified << "\n";
	return anyUnified;
}
示例#2
0
bool Constraints::solve(std::list<ConstraintMap>& solns) {
LOG << conSet.size() << " constraints:";
std::ostringstream os; conSet.print(os); LOG << os.str().c_str();
	// Replace Ta[loc] = ptr(alpha) with
	//		   Tloc = alpha
	LocationSet::iterator cc;
	for (cc = conSet.begin(); cc != conSet.end(); cc++) {
		Exp* c = *cc;
		if (!c->isEquality()) continue;
		Exp* left = ((Binary*)c)->getSubExp1();
		if (!left->isTypeOf()) continue;
		Exp* leftSub = ((Unary*)left)->getSubExp1();
		if (!leftSub->isAddrOf()) continue;
		Exp* right = ((Binary*)c)->getSubExp2();
		if (!right->isTypeVal()) continue;
		Type* t = ((TypeVal*)right)->getType();
		if (!t->isPointer()) continue;
		// Don't modify a key in a map
		Exp* clone = c->clone();
		// left is typeof(addressof(something)) -> typeof(something)
		left = ((Binary*)clone)->getSubExp1();
		leftSub = ((Unary*)left)->getSubExp1();
		Exp* something = ((Unary*)leftSub)->getSubExp1();
		((Unary*)left)->setSubExp1ND(something);
		((Unary*)leftSub)->setSubExp1ND(NULL);
		delete leftSub;
		// right is <alpha*> -> <alpha>
		right = ((Binary*)clone)->getSubExp2();
		t = ((TypeVal*)right)->getType();
		((TypeVal*)right)->setType(((PointerType*)t)->getPointsTo()->clone());
		delete t;
		conSet.remove(c);
		conSet.insert(clone);
		delete c;
	}

	// Sort constraints into a few categories. Disjunctions go to a special
	// list, always true is just ignored, and constraints of the form
	// typeof(x) = y (where y is a type value) go to a map called fixed.
	// Constraint terms of the form Tx = Ty go into a map of LocationSets
	// called equates for fast lookup
	for (cc = conSet.begin(); cc != conSet.end(); cc++) {
		Exp* c = *cc;
		if (c->isTrue()) continue;
		if (c->isFalse()) {
			if (VERBOSE || DEBUG_TA)
				LOG << "Constraint failure: always false constraint\n";
			return false;
		}
		if (c->isDisjunction()) {
			disjunctions.push_back(c);
			continue;
		}
		// Break up conjunctions into terms
		Exp* rem = c, *term;
		while ((term = nextConjunct(rem)) != NULL) {
			assert(term->isEquality());
			Exp* lhs = ((Binary*)term)->getSubExp1();
			Exp* rhs = ((Binary*)term)->getSubExp2();
			if (rhs->isTypeOf()) {
				// Of the form typeof(x) = typeof(z)
				// Insert into equates 
				equates.addEquate(lhs, rhs);
			} else {
				// Of the form typeof(x) = <typeval>
				// Insert into fixed
				assert(rhs->isTypeVal());
				fixed[lhs] = rhs;
			}
		}
	}

	{LOG << "\n" << (unsigned)disjunctions.size() << " disjunctions: "; std::list<Exp*>::iterator dd;
	 for (dd = disjunctions.begin(); dd != disjunctions.end(); dd++) LOG << *dd << ",\n"; LOG << "\n";}
	LOG << fixed.size() << " fixed: " << fixed.prints();
	LOG << equates.size() << " equates: " << equates.prints();

	// Substitute the fixed types into the disjunctions
	substIntoDisjuncts(fixed);

	// Substitute the fixed types into the equates. This may generate more
	// fixed types
	substIntoEquates(fixed);

	LOG << "\nAfter substitute fixed into equates:\n";
	{LOG << "\n" << (unsigned)disjunctions.size() << " disjunctions: "; std::list<Exp*>::iterator dd;
	 for (dd = disjunctions.begin(); dd != disjunctions.end(); dd++) LOG << *dd << ",\n"; LOG << "\n";}
	LOG << fixed.size() << " fixed: " << fixed.prints();
	LOG << equates.size() << " equates: " << equates.prints();
	// Substitute again the fixed types into the disjunctions
	// (since there may be more fixed types from the above)
	substIntoDisjuncts(fixed);

	LOG << "\nAfter second substitute fixed into disjunctions:\n";
	{LOG << "\n" << (unsigned)disjunctions.size() << " disjunctions: "; std::list<Exp*>::iterator dd;
	 for (dd = disjunctions.begin(); dd != disjunctions.end(); dd++) LOG << *dd << ",\n"; LOG << "\n";}
	LOG << fixed.size() << " fixed: " << fixed.prints();
	LOG << equates.size() << " equates: " << equates.prints();

	ConstraintMap soln;
	bool ret = doSolve(disjunctions.begin(), soln, solns);
	if (ret) {
		// For each solution, we need to find disjunctions of the form
		// <alphaN> = <type>	  or
		// <type>	= <alphaN>
		// and substitute these into each part of the solution
		std::list<ConstraintMap>::iterator it;
		for (it = solns.begin(); it != solns.end(); it++)
			it->substAlpha();
	}
	return ret;
}