Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    r843054c2 r7c64920  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sat May 16 23:52:08 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat May 16 23:55:30 2015
    13 // Update Count     : 3
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Thu May 21 16:21:09 2015
     13// Update Count     : 12
    1414//
    1515
     
    107107                                                PRINT(
    108108                                                        std::cout << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl;
    109                                                         )
    110                                                         selected[ mangleName ] = current;
     109                                                )
     110                                                selected[ mangleName ] = current;
    111111                                        } else if ( candidate->cost == mapPlace->second.candidate->cost ) {
    112112                                                PRINT(
    113113                                                        std::cout << "marking ambiguous" << std::endl;
    114                                                         )
    115                                                         mapPlace->second.isAmbiguous = true;
     114                                                )
     115                                                mapPlace->second.isAmbiguous = true;
    116116                                        }
    117117                                } else {
     
    122122                        PRINT(
    123123                                std::cout << "there are " << selected.size() << " alternatives before elimination" << std::endl;
    124                                 )
    125 
    126                                 // accept the alternatives that were unambiguous
    127                                 for ( std::map< std::string, PruneStruct >::iterator target = selected.begin(); target != selected.end(); ++target ) {
    128                                         if ( ! target->second.isAmbiguous ) {
    129                                                 Alternative &alt = *target->second.candidate;
    130                                                 for ( std::list< Type* >::iterator result = alt.expr->get_results().begin(); result != alt.expr->get_results().end(); ++result ) {
    131                                                         alt.env.applyFree( *result );
    132                                                 }
    133                                                 *out++ = alt;
     124                        )
     125
     126                        // accept the alternatives that were unambiguous
     127                        for ( std::map< std::string, PruneStruct >::iterator target = selected.begin(); target != selected.end(); ++target ) {
     128                                if ( ! target->second.isAmbiguous ) {
     129                                        Alternative &alt = *target->second.candidate;
     130                                        for ( std::list< Type* >::iterator result = alt.expr->get_results().begin(); result != alt.expr->get_results().end(); ++result ) {
     131                                                alt.env.applyFree( *result );
    134132                                        }
    135                                 }
     133                                        *out++ = alt;
     134                                }
     135                        }
    136136
    137137                }
     
    183183                                std::cout << "findSubExprs" << std::endl;
    184184                                printAlts( finder.alternatives, std::cout );
    185                                 )
    186                                 *out++ = finder;
     185                        )
     186                        *out++ = finder;
    187187                }
    188188        }
     
    205205                        std::cout << "alternatives before prune:" << std::endl;
    206206                        printAlts( alternatives, std::cout );
    207                         )
    208                         AltList::iterator oldBegin = alternatives.begin();
     207                )
     208                AltList::iterator oldBegin = alternatives.begin();
    209209                pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ), indexer );
    210210                if ( alternatives.begin() == oldBegin ) {
     
    221221                PRINT(
    222222                        std::cout << "there are " << alternatives.size() << " alternatives after elimination" << std::endl;
    223                         )
    224                         }
     223                )
     224        }
    225225
    226226        void AlternativeFinder::findWithAdjustment( Expression *expr ) {
     
    264264                                std::cout << "--- results are" << std::endl;
    265265                                printAll( (*actualExpr)->get_results(), std::cout, 8 );
    266                                 )
    267                                 std::list< DeclarationWithType* >::iterator startFormal = formal;
     266                        )
     267                        std::list< DeclarationWithType* >::iterator startFormal = formal;
    268268                        Cost actualCost;
    269269                        for ( std::list< Type* >::iterator actual = (*actualExpr)->get_results().begin(); actual != (*actualExpr)->get_results().end(); ++actual ) {
     
    281281                                        std::cout << std::endl << " to ";
    282282                                        (*formal)->get_type()->print( std::cout, 8 );
    283                                         )
    284                                         Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
     283                                )
     284                                Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
    285285                                PRINT(
    286286                                        std::cout << std::endl << "cost is" << newCost << std::endl;
    287                                         )
    288 
    289                                         if ( newCost == Cost::infinity ) {
    290                                                 return newCost;
    291                                         }
     287                                )
     288
     289                                if ( newCost == Cost::infinity ) {
     290                                        return newCost;
     291                                }
    292292                                convCost += newCost;
    293293                                actualCost += newCost;
     
    381381                                        (*actual)->print( std::cerr );
    382382                                        std::cerr << std::endl;
    383                                         )
    384                                         if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
    385                                                 return false;
    386                                         }
     383                                )
     384                                if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
     385                                        return false;
     386                                }
    387387                                formal++;
    388388                        }
     
    429429                                        std::cerr << "recursing with new set:" << std::endl;
    430430                                        printAssertionSet( newNeed, std::cerr, 8 );
    431                                         )
    432                                         inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, level+1, indexer, out );
     431                                )
     432                                inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, level+1, indexer, out );
    433433                                return;
    434434                        }
     
    444444                        curDecl->print( std::cerr );
    445445                        std::cerr << std::endl;
    446                         )
    447                         std::list< DeclarationWithType* > candidates;
     446                )
     447                std::list< DeclarationWithType* > candidates;
    448448                decls.lookupId( curDecl->get_name(), candidates );
    449449///   if ( candidates.empty() ) { std::cout << "no candidates!" << std::endl; }
     
    453453                                (*candidate)->print( std::cout );
    454454                                std::cout << std::endl;
    455                                 )
    456                                 AssertionSet newHave, newerNeed( newNeed );
     455                        )
     456                        AssertionSet newHave, newerNeed( newNeed );
    457457                        TypeEnvironment newEnv( newAlt.env );
    458458                        OpenVarSet newOpenVars( openVars );
     
    466466                                adjType->print( std::cerr );
    467467                                std::cerr << std::endl;
     468                        )
     469                        if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) {
     470                                PRINT(
     471                                        std::cerr << "success!" << std::endl;
    468472                                )
    469                                 if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) {
    470                                         PRINT(
    471                                                 std::cerr << "success!" << std::endl;
    472                                                 )
    473                                                 SymTab::Indexer newDecls( decls );
    474                                         addToIndexer( newHave, newDecls );
    475                                         Alternative newerAlt( newAlt );
    476                                         newerAlt.env = newEnv;
    477                                         assert( (*candidate)->get_uniqueId() );
    478                                         Expression *varExpr = new VariableExpr( static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) ) );
    479                                         deleteAll( varExpr->get_results() );
    480                                         varExpr->get_results().clear();
    481                                         varExpr->get_results().push_front( adjType->clone() );
    482                                         PRINT(
    483                                                 std::cout << "satisfying assertion " << curDecl->get_uniqueId() << " ";
    484                                                 curDecl->print( std::cout );
    485                                                 std::cout << " with declaration " << (*candidate)->get_uniqueId() << " ";
    486                                                 (*candidate)->print( std::cout );
    487                                                 std::cout << std::endl;
    488                                                 )
    489                                                 ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr );
    490                                         // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions
    491                                         appExpr->get_inferParams()[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr );
    492                                         inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, level, indexer, out );
    493                                 } else {
    494                                         delete adjType;
    495                                 }
     473                                SymTab::Indexer newDecls( decls );
     474                                addToIndexer( newHave, newDecls );
     475                                Alternative newerAlt( newAlt );
     476                                newerAlt.env = newEnv;
     477                                assert( (*candidate)->get_uniqueId() );
     478                                Expression *varExpr = new VariableExpr( static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) ) );
     479                                deleteAll( varExpr->get_results() );
     480                                varExpr->get_results().clear();
     481                                varExpr->get_results().push_front( adjType->clone() );
     482                                PRINT(
     483                                        std::cout << "satisfying assertion " << curDecl->get_uniqueId() << " ";
     484                                        curDecl->print( std::cout );
     485                                        std::cout << " with declaration " << (*candidate)->get_uniqueId() << " ";
     486                                        (*candidate)->print( std::cout );
     487                                        std::cout << std::endl;
     488                                )
     489                                ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr );
     490                                // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions
     491                                appExpr->get_inferParams()[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr );
     492                                inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, level, indexer, out );
     493                        } else {
     494                                delete adjType;
     495                        }
    496496                }
    497497        }
     
    509509                        std::cout << "============= new indexer" << std::endl;
    510510                        decls.print( std::cout );
    511                         )
    512                         addToIndexer( have, decls );
     511                )
     512                addToIndexer( have, decls );
    513513                AssertionSet newNeed;
    514514                inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, 0, indexer, out );
     
    533533                                std::cout << "need assertions:" << std::endl;
    534534                                printAssertionSet( resultNeed, std::cout, 8 );
    535                                 )
    536                                 inferParameters( resultNeed, resultHave, newAlt, openVars, out );
     535                        )
     536                        inferParameters( resultNeed, resultHave, newAlt, openVars, out );
    537537                }
    538538        }
     
    570570                                std::cout << "working on alternative: " << std::endl;
    571571                                func->print( std::cout, 8 );
    572                                 )
    573                                 // check if the type is pointer to function
    574                                 PointerType *pointer;
     572                        )
     573                        // check if the type is pointer to function
     574                        PointerType *pointer;
    575575                        if ( func->expr->get_results().size() == 1 && ( pointer = dynamic_cast< PointerType* >( func->expr->get_results().front() ) ) ) {
    576576                                if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) {
     
    603603                                                std::cout << "known function ops:" << std::endl;
    604604                                                printAlts( funcOpFinder.alternatives, std::cout, 8 );
    605                                                 )
    606                                                 }
     605                                        )
     606                                }
    607607
    608608                                for ( AltList::const_iterator funcOp = funcOpFinder.alternatives.begin(); funcOp != funcOpFinder.alternatives.end(); ++funcOp ) {
     
    642642                                withFunc->env.print( std::cout, 8 );
    643643                                std::cout << "cost of conversion is:" << cvtCost << std::endl;
    644                                 )
    645                                 if ( cvtCost != Cost::infinity ) {
    646                                         withFunc->cvtCost = cvtCost;
    647                                         alternatives.push_back( *withFunc );
    648                                 } // if
     644                        )
     645                        if ( cvtCost != Cost::infinity ) {
     646                                withFunc->cvtCost = cvtCost;
     647                                alternatives.push_back( *withFunc );
     648                        } // if
    649649                } // for
    650650                candidates.clear();
     
    737737                indexer.lookupId( nameExpr->get_name(), declList );
    738738                PRINT( std::cerr << "nameExpr is " << nameExpr->get_name() << std::endl; )
    739                         for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) {
    740                                 VariableExpr newExpr( *i, nameExpr->get_argName() );
    741                                 alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) );
    742                                 PRINT(
    743                                         std::cerr << "decl is ";
    744                                         (*i)->print( std::cerr );
    745                                         std::cerr << std::endl;
    746                                         std::cerr << "newExpr is ";
    747                                         newExpr.print( std::cerr );
    748                                         std::cerr << std::endl;
    749                                         )
    750                                         renameTypes( alternatives.back().expr );
    751                                 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) {
    752                                         addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), "" );
    753                                 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( (*i)->get_type() ) ) {
    754                                         addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), "" );
    755                                 } // if
    756                         } // for
     739                for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) {
     740                        VariableExpr newExpr( *i, nameExpr->get_argName() );
     741                        alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) );
     742                        PRINT(
     743                                std::cerr << "decl is ";
     744                                (*i)->print( std::cerr );
     745                                std::cerr << std::endl;
     746                                std::cerr << "newExpr is ";
     747                                newExpr.print( std::cerr );
     748                                std::cerr << std::endl;
     749                        )
     750                        renameTypes( alternatives.back().expr );
     751                        if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) {
     752                                addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), "" );
     753                        } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( (*i)->get_type() ) ) {
     754                                addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), "" );
     755                        } // if
     756                } // for
    757757        }
    758758
     
    794794                        argType->print( std::cout );
    795795                        std::cout << std::endl;
    796                         )
    797                         if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
    798                                 alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) );
    799                                 for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) {
    800                                         alternatives.back().expr->get_results().push_back( (*i)->get_type()->clone() );
    801                                 } // for
    802                         } // if
     796                )
     797                if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
     798                        alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) );
     799                        for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) {
     800                                alternatives.back().expr->get_results().push_back( (*i)->get_type()->clone() );
     801                        } // for
     802                } // if
    803803        }
    804804
Note: See TracChangeset for help on using the changeset viewer.