Changeset a32b204 for translator/Tuples/TupleAssignment.cc
- Timestamp:
- May 17, 2015, 1:19:35 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 0dd3a2f
- Parents:
- b87a5ed
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/Tuples/TupleAssignment.cc
rb87a5ed ra32b204 19 19 bool TupleAssignSpotter::pointsToTuple( Expression *expr ) { 20 20 // also check for function returning tuple of reference types 21 if (AddressExpr *addr = dynamic_cast<AddressExpr *>(expr) )22 if ( isTuple(addr->get_arg() ) )21 if (AddressExpr *addr = dynamic_cast<AddressExpr *>(expr) ) 22 if ( isTuple(addr->get_arg() ) ) 23 23 return true; 24 24 return false; … … 26 26 27 27 bool TupleAssignSpotter::isTupleVar( DeclarationWithType *decl ) { 28 if ( dynamic_cast<TupleType *>(decl->get_type()) )28 if ( dynamic_cast<TupleType *>(decl->get_type()) ) 29 29 return true; 30 30 return false; … … 33 33 bool TupleAssignSpotter::isTuple( Expression *expr, bool isRight ) { 34 34 // true if `expr' is an expression returning a tuple: tuple, tuple variable or MRV function 35 if ( ! expr ) return false;36 37 if ( dynamic_cast<TupleExpr *>(expr) )38 return true; 39 else if ( VariableExpr *var = dynamic_cast<VariableExpr *>(expr) ) {40 if ( isTupleVar(var->get_var()) )35 if ( ! expr ) return false; 36 37 if ( dynamic_cast<TupleExpr *>(expr) ) 38 return true; 39 else if ( VariableExpr *var = dynamic_cast<VariableExpr *>(expr) ) { 40 if ( isTupleVar(var->get_var()) ) 41 41 return true; 42 42 } … … 52 52 return false; 53 53 54 if ( new_assigns.empty() ) return false;54 if ( new_assigns.empty() ) return false; 55 55 /*return */matcher->solve( new_assigns ); 56 if ( dynamic_cast<TupleAssignSpotter::MultipleAssignMatcher *>( matcher ) ) {56 if ( dynamic_cast<TupleAssignSpotter::MultipleAssignMatcher *>( matcher ) ) { 57 57 // now resolve new assignments 58 58 std::list< Expression * > solved_assigns; … … 61 61 62 62 ResolvExpr::AltList current; 63 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i ) {63 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i ) { 64 64 //try { 65 65 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); … … 80 80 return true; 81 81 } else { // mass assignment 82 //if ( new_assigns.empty() ) return false;82 //if ( new_assigns.empty() ) return false; 83 83 std::list< Expression * > solved_assigns; 84 84 ResolvExpr::AltList solved_alts; … … 87 87 ResolvExpr::AltList current; 88 88 if ( optMass.empty() ) { 89 for ( std::list< Expression * >::size_type i = 0; i != new_assigns.size(); ++i )89 for ( std::list< Expression * >::size_type i = 0; i != new_assigns.size(); ++i ) 90 90 optMass.push_back( ResolvExpr::AltList() ); 91 91 } 92 92 int cnt = 0; 93 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i, cnt++ ) {93 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i, cnt++ ) { 94 94 95 95 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); … … 110 110 111 111 bool TupleAssignSpotter::isMVR( Expression *expr ) { 112 if ( expr->get_results().size() > 1 ) {112 if ( expr->get_results().size() > 1 ) { 113 113 // MVR processing 114 114 return true; … … 118 118 119 119 bool TupleAssignSpotter::isTupleAssignment( UntypedExpr * expr, std::list<ResolvExpr::AltList> &possibilities ) { 120 if ( NameExpr *assgnop = dynamic_cast< NameExpr * >(expr->get_function()) ) {121 122 if ( assgnop->get_name() == std::string("?=?") ) {123 124 for ( std::list<ResolvExpr::AltList>::iterator ali = possibilities.begin(); ali != possibilities.end(); ++ali ) {120 if ( NameExpr *assgnop = dynamic_cast< NameExpr * >(expr->get_function()) ) { 121 122 if ( assgnop->get_name() == std::string("?=?") ) { 123 124 for ( std::list<ResolvExpr::AltList>::iterator ali = possibilities.begin(); ali != possibilities.end(); ++ali ) { 125 125 assert( ali->size() == 2 ); 126 126 ResolvExpr::AltList::iterator opit = ali->begin(); 127 127 ResolvExpr::Alternative op1 = *opit, op2 = *(++opit); 128 128 129 if ( pointsToTuple(op1.expr) ) { // also handles tuple vars129 if ( pointsToTuple(op1.expr) ) { // also handles tuple vars 130 130 if ( isTuple( op2.expr, true ) ) 131 131 matcher = new MultipleAssignMatcher(op1.expr, op2.expr); 132 else if ( isMVR( op2.expr ) ) {132 else if ( isMVR( op2.expr ) ) { 133 133 // handle MVR differently 134 134 } else … … 137 137 138 138 std::list< ResolvExpr::AltList > options; 139 if ( match() )139 if ( match() ) 140 140 /* 141 if ( hasMatched ) {141 if ( hasMatched ) { 142 142 // throw SemanticError("Ambiguous tuple assignment"); 143 143 } else {*/ … … 150 150 } 151 151 152 if ( hasMatched ) {153 if ( dynamic_cast<TupleAssignSpotter::MultipleAssignMatcher *>( matcher ) ) {152 if ( hasMatched ) { 153 if ( dynamic_cast<TupleAssignSpotter::MultipleAssignMatcher *>( matcher ) ) { 154 154 //options.print( std::cerr ); 155 155 std::list< ResolvExpr::AltList >best = options.get_best(); 156 if ( best.size() == 1 ) {156 if ( best.size() == 1 ) { 157 157 std::list<Expression *> solved_assigns; 158 for ( ResolvExpr::AltList::iterator i = best.front().begin(); i != best.front().end(); ++i ){158 for ( ResolvExpr::AltList::iterator i = best.front().begin(); i != best.front().end(); ++i ){ 159 159 solved_assigns.push_back( i->expr ); 160 160 } … … 165 165 assert(! optMass.empty() ); 166 166 ResolvExpr::AltList winners; 167 for ( std::vector< ResolvExpr::AltList >::iterator i = optMass.begin(); i != optMass.end(); ++i )167 for ( std::vector< ResolvExpr::AltList >::iterator i = optMass.begin(); i != optMass.end(); ++i ) 168 168 findMinCostAlt( i->begin(), i->end(), back_inserter(winners) ); 169 169 170 170 std::list< Expression *> solved_assigns; 171 for ( ResolvExpr::AltList::iterator i = winners.begin(); i != winners.end(); ++i )171 for ( ResolvExpr::AltList::iterator i = winners.begin(); i != winners.end(); ++i ) 172 172 solved_assigns.push_back( i->expr ); 173 173 currentFinder->get_alternatives().push_front( ResolvExpr::Alternative(new SolvedTupleExpr(solved_assigns/*, SolvedTupleExpr::MASS*/), currentFinder->get_environ(), ResolvExpr::Cost() ) ); … … 181 181 void TupleAssignSpotter::Matcher::init( Expression *_lhs, Expression *_rhs ) { 182 182 lhs.clear(); 183 if (AddressExpr *addr = dynamic_cast<AddressExpr *>(_lhs) )184 if ( TupleExpr *tuple = dynamic_cast<TupleExpr *>(addr->get_arg()) )183 if (AddressExpr *addr = dynamic_cast<AddressExpr *>(_lhs) ) 184 if ( TupleExpr *tuple = dynamic_cast<TupleExpr *>(addr->get_arg()) ) 185 185 std::copy( tuple->get_exprs().begin(), tuple->get_exprs().end(), back_inserter(lhs) ); 186 186 … … 195 195 init(_lhs,_rhs); 196 196 197 if ( TupleExpr *tuple = dynamic_cast<TupleExpr *>(_rhs) )197 if ( TupleExpr *tuple = dynamic_cast<TupleExpr *>(_rhs) ) 198 198 std::copy( tuple->get_exprs().begin(), tuple->get_exprs().end(), back_inserter(rhs) ); 199 199 } … … 211 211 if ( lhs.empty() || (rhs.size() != 1) ) return false; 212 212 213 for ( std::list< Expression * >::iterator l = lhs.begin(); l != lhs.end(); l++ ) {213 for ( std::list< Expression * >::iterator l = lhs.begin(); l != lhs.end(); l++ ) { 214 214 std::list< Expression * > args; 215 215 args.push_back( new AddressExpr(*l) ); … … 229 229 ResolvExpr::AltList current; 230 230 if ( optMass.empty() ) { 231 for ( std::list< Expression * >::size_type i = 0; i != new_assigns.size(); ++i )231 for ( std::list< Expression * >::size_type i = 0; i != new_assigns.size(); ++i ) 232 232 optMass.push_back( ResolvExpr::AltList() ); 233 233 } 234 234 int cnt = 0; 235 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i, cnt++ ) {235 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i, cnt++ ) { 236 236 237 237 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); … … 250 250 bool TupleAssignSpotter::MultipleAssignMatcher::match( std::list< Expression * > &out ) { 251 251 // need more complicated matching 252 if ( lhs.size() == rhs.size() ) {252 if ( lhs.size() == rhs.size() ) { 253 253 zipWith( lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), back_inserter(out), TupleAssignSpotter::Matcher::createAssgn ); 254 254 return true; … … 265 265 266 266 ResolvExpr::AltList current; 267 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i ) {267 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i ) { 268 268 //try { 269 269 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); … … 297 297 // transpose matrix 298 298 if ( costMatrix.empty() ) 299 for ( unsigned int i = 0; i< opt.size(); ++i)299 for ( unsigned int i = 0; i< opt.size(); ++i) 300 300 costMatrix.push_back( vector<ResolvExpr::Cost>() ); 301 301 302 302 int cnt = 0; 303 for ( ResolvExpr::AltList::iterator i = opt.begin(); i != opt.end(); ++i, cnt++ )303 for ( ResolvExpr::AltList::iterator i = opt.begin(); i != opt.end(); ++i, cnt++ ) 304 304 costMatrix[cnt].push_back( i->cost ); 305 305 … … 312 312 list< ResolvExpr::AltList > ret; 313 313 list< multiset<int> > solns; 314 for ( vector< vector<Cost> >::iterator i = costMatrix.begin(); i != costMatrix.end(); ++i ) {314 for ( vector< vector<Cost> >::iterator i = costMatrix.begin(); i != costMatrix.end(); ++i ) { 315 315 list<int> current; 316 316 findMinCost( i->begin(), i->end(), back_inserter(current) ); … … 320 320 multiset<int> result; 321 321 lift_intersection( solns.begin(), solns.end(), inserter( result, result.begin() ) ); 322 if ( result.size() != 1 )322 if ( result.size() != 1 ) 323 323 throw SemanticError("Ambiguous tuple expression"); 324 324 ret.push_back(get_option( *(result.begin() ))); … … 329 329 using namespace std; 330 330 331 for ( vector< vector < ResolvExpr::Cost > >::iterator i = costMatrix.begin(); i != costMatrix.end(); ++i ) {332 for ( vector < ResolvExpr::Cost >::iterator j = i->begin(); j != i->end(); ++j )331 for ( vector< vector < ResolvExpr::Cost > >::iterator i = costMatrix.begin(); i != costMatrix.end(); ++i ) { 332 for ( vector < ResolvExpr::Cost >::iterator j = i->begin(); j != i->end(); ++j ) 333 333 ostr << *j << " " ; 334 334 ostr << std::endl; … … 352 352 Cost minCost = Cost::infinity; 353 353 unsigned int index = 0; 354 for ( InputIterator i = begin; i != end; ++i, index++ ) {355 if ( *i < minCost ) {354 for ( InputIterator i = begin; i != end; ++i, index++ ) { 355 if ( *i < minCost ) { 356 356 minCost = *i; 357 357 alternatives.clear(); 358 358 alternatives.push_back( index ); 359 } else if ( *i == minCost ) {359 } else if ( *i == minCost ) { 360 360 alternatives.push_back( index ); 361 361 } … … 366 366 template< class InputIterator, class OutputIterator > 367 367 void TupleAssignSpotter::Options::lift_intersection( InputIterator begin, InputIterator end, OutputIterator out ){ 368 if ( begin == end ) return;368 if ( begin == end ) return; 369 369 InputIterator test = begin; 370 370 371 if (++test == end)371 if (++test == end) 372 372 { copy(begin->begin(), begin->end(), out); return; } 373 373 … … 376 376 copy( begin->begin(), begin->end(), inserter( cur, cur.begin() ) ); 377 377 378 while ( test != end ) {378 while ( test != end ) { 379 379 std::multiset<int> temp; 380 380 set_intersection( cur.begin(), cur.end(), test->begin(), test->end(), inserter(temp,temp.begin()) ); … … 390 390 391 391 ResolvExpr::AltList TupleAssignSpotter::Options::get_option( std::list< ResolvExpr::AltList >::size_type index ) { 392 if ( index >= options.size() )392 if ( index >= options.size() ) 393 393 throw 0; // XXX 394 394 std::list< ResolvExpr::AltList >::iterator it = options.begin(); 395 for ( std::list< ResolvExpr::AltList >::size_type i = 0; i < index; ++i, ++it );395 for ( std::list< ResolvExpr::AltList >::size_type i = 0; i < index; ++i, ++it ); 396 396 return *it; 397 397 }
Note: See TracChangeset
for help on using the changeset viewer.