| 1 | /* | 
|---|
| 2 | * This file is part of the Cforall project | 
|---|
| 3 | * | 
|---|
| 4 | * $Id: Specialize.cc,v 1.4 2005/08/29 20:14:13 rcbilson Exp $ | 
|---|
| 5 | * | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include <cassert> | 
|---|
| 9 |  | 
|---|
| 10 | #include "Specialize.h" | 
|---|
| 11 | #include "PolyMutator.h" | 
|---|
| 12 |  | 
|---|
| 13 | #include "SynTree/Declaration.h" | 
|---|
| 14 | #include "SynTree/Statement.h" | 
|---|
| 15 | #include "SynTree/Expression.h" | 
|---|
| 16 | #include "SynTree/Type.h" | 
|---|
| 17 | #include "SynTree/TypeSubstitution.h" | 
|---|
| 18 | #include "SynTree/Mutator.h" | 
|---|
| 19 | #include "ResolvExpr/FindOpenVars.h" | 
|---|
| 20 | #include "UniqueName.h" | 
|---|
| 21 | #include "utility.h" | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | namespace GenPoly { | 
|---|
| 25 |  | 
|---|
| 26 | const std::list<Label> noLabels; | 
|---|
| 27 |  | 
|---|
| 28 | class Specialize : public PolyMutator | 
|---|
| 29 | { | 
|---|
| 30 | public: | 
|---|
| 31 | Specialize( std::string paramPrefix = "_p" ); | 
|---|
| 32 |  | 
|---|
| 33 | virtual Expression* mutate(ApplicationExpr *applicationExpr); | 
|---|
| 34 | virtual Expression* mutate(AddressExpr *castExpr); | 
|---|
| 35 | virtual Expression* mutate(CastExpr *castExpr); | 
|---|
| 36 | virtual Expression* mutate(LogicalExpr *logicalExpr); | 
|---|
| 37 | virtual Expression* mutate(ConditionalExpr *conditionalExpr); | 
|---|
| 38 | virtual Expression* mutate(CommaExpr *commaExpr); | 
|---|
| 39 |  | 
|---|
| 40 | private: | 
|---|
| 41 | Expression *doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams = 0 ); | 
|---|
| 42 | void handleExplicitParams( ApplicationExpr *appExpr ); | 
|---|
| 43 |  | 
|---|
| 44 | UniqueName thunkNamer; | 
|---|
| 45 | std::string paramPrefix; | 
|---|
| 46 | }; | 
|---|
| 47 |  | 
|---|
| 48 | void | 
|---|
| 49 | convertSpecializations( std::list< Declaration* >& translationUnit ) | 
|---|
| 50 | { | 
|---|
| 51 | Specialize specializer; | 
|---|
| 52 | mutateAll( translationUnit, specializer ); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | Specialize::Specialize( std::string paramPrefix ) | 
|---|
| 56 | : thunkNamer( "_thunk" ), paramPrefix( paramPrefix ) | 
|---|
| 57 | { | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | bool | 
|---|
| 61 | needsSpecialization( Type *formalType, Type *actualType, TypeSubstitution *env ) | 
|---|
| 62 | { | 
|---|
| 63 | if( env ) { | 
|---|
| 64 | using namespace ResolvExpr; | 
|---|
| 65 | OpenVarSet openVars, closedVars; | 
|---|
| 66 | AssertionSet need, have; | 
|---|
| 67 | findOpenVars( formalType, openVars, closedVars, need, have, false ); | 
|---|
| 68 | findOpenVars( actualType, openVars, closedVars, need, have, true ); | 
|---|
| 69 | for( OpenVarSet::const_iterator openVar = openVars.begin(); openVar != openVars.end(); ++openVar ) { | 
|---|
| 70 | Type *boundType = env->lookup( openVar->first ); | 
|---|
| 71 | if( !boundType ) continue; | 
|---|
| 72 | if( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( boundType ) ) { | 
|---|
| 73 | if( closedVars.find( typeInst->get_name() ) == closedVars.end() ) { | 
|---|
| 74 | return true; | 
|---|
| 75 | } | 
|---|
| 76 | } else { | 
|---|
| 77 | return true; | 
|---|
| 78 | } | 
|---|
| 79 | } | 
|---|
| 80 | return false; | 
|---|
| 81 | } else { | 
|---|
| 82 | return false; | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | Expression* | 
|---|
| 87 | Specialize::doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams ) | 
|---|
| 88 | { | 
|---|
| 89 | if( needsSpecialization( formalType, actual->get_results().front(), env ) ) { | 
|---|
| 90 | PointerType *ptrType; | 
|---|
| 91 | FunctionType *funType; | 
|---|
| 92 | if( ( ptrType = dynamic_cast< PointerType* >( formalType ) ) && ( funType = dynamic_cast< FunctionType* >( ptrType->get_base() ) ) ) { | 
|---|
| 93 | FunctionType *newType = funType->clone(); | 
|---|
| 94 | if( env ) { | 
|---|
| 95 | TypeSubstitution newEnv( *env ); | 
|---|
| 96 | // it is important to replace only occurrences of type variables that occur free in the | 
|---|
| 97 | // thunk's type | 
|---|
| 98 | newEnv.applyFree( newType ); | 
|---|
| 99 | } | 
|---|
| 100 | FunctionDecl *thunkFunc = new FunctionDecl( thunkNamer.newName(), Declaration::NoStorageClass, LinkageSpec::C, newType, new CompoundStmt( std::list< std::string >() ), false ); | 
|---|
| 101 | thunkFunc->fixUniqueId(); | 
|---|
| 102 |  | 
|---|
| 103 | UniqueName paramNamer( paramPrefix ); | 
|---|
| 104 | ApplicationExpr *appExpr = new ApplicationExpr( actual ); | 
|---|
| 105 | for( std::list< DeclarationWithType* >::iterator param = thunkFunc->get_functionType()->get_parameters().begin(); param != thunkFunc->get_functionType()->get_parameters().end(); ++param ) { | 
|---|
| 106 | (*param)->set_name( paramNamer.newName() ); | 
|---|
| 107 | appExpr->get_args().push_back( new VariableExpr( *param ) ); | 
|---|
| 108 | } | 
|---|
| 109 | appExpr->set_env( maybeClone( env ) ); | 
|---|
| 110 | if( inferParams ) { | 
|---|
| 111 | appExpr->get_inferParams() = *inferParams; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | // handle any specializations that may still be present | 
|---|
| 115 | std::string oldParamPrefix = paramPrefix; | 
|---|
| 116 | paramPrefix += "p"; | 
|---|
| 117 | std::list< Statement* > oldStmts; | 
|---|
| 118 | oldStmts.splice( oldStmts.end(), stmtsToAdd ); | 
|---|
| 119 | handleExplicitParams( appExpr ); | 
|---|
| 120 | paramPrefix = oldParamPrefix; | 
|---|
| 121 | thunkFunc->get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd ); | 
|---|
| 122 | stmtsToAdd.splice( stmtsToAdd.end(), oldStmts ); | 
|---|
| 123 |  | 
|---|
| 124 | Statement *appStmt; | 
|---|
| 125 | if( funType->get_returnVals().empty() ) { | 
|---|
| 126 | appStmt = new ExprStmt( noLabels, appExpr ); | 
|---|
| 127 | } else { | 
|---|
| 128 | appStmt = new ReturnStmt( noLabels, appExpr ); | 
|---|
| 129 | } | 
|---|
| 130 | thunkFunc->get_statements()->get_kids().push_back( appStmt ); | 
|---|
| 131 | stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) ); | 
|---|
| 132 | return new AddressExpr( new VariableExpr( thunkFunc ) ); | 
|---|
| 133 | } else { | 
|---|
| 134 | return actual; | 
|---|
| 135 | } | 
|---|
| 136 | } else { | 
|---|
| 137 | return actual; | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void | 
|---|
| 142 | Specialize::handleExplicitParams( ApplicationExpr *appExpr ) | 
|---|
| 143 | { | 
|---|
| 144 | // create thunks for the explicit parameters | 
|---|
| 145 | assert( !appExpr->get_function()->get_results().empty() ); | 
|---|
| 146 | PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() ); | 
|---|
| 147 | assert( pointer ); | 
|---|
| 148 | FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ); | 
|---|
| 149 | std::list< DeclarationWithType* >::iterator formal; | 
|---|
| 150 | std::list< Expression* >::iterator actual; | 
|---|
| 151 | for( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) { | 
|---|
| 152 | *actual = doSpecialization( (*formal)->get_type(), *actual, &appExpr->get_inferParams() ); | 
|---|
| 153 | } | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | Expression* | 
|---|
| 157 | Specialize::mutate(ApplicationExpr *appExpr) | 
|---|
| 158 | { | 
|---|
| 159 | appExpr->get_function()->acceptMutator( *this ); | 
|---|
| 160 | mutateAll( appExpr->get_args(), *this ); | 
|---|
| 161 |  | 
|---|
| 162 | // create thunks for the inferred parameters | 
|---|
| 163 | for( InferredParams::iterator inferParam = appExpr->get_inferParams().begin(); inferParam != appExpr->get_inferParams().end(); ++inferParam ) { | 
|---|
| 164 | inferParam->second.expr = doSpecialization( inferParam->second.formalType, inferParam->second.expr, &appExpr->get_inferParams() ); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | handleExplicitParams(appExpr); | 
|---|
| 168 |  | 
|---|
| 169 | return appExpr; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | Expression* | 
|---|
| 173 | Specialize::mutate(AddressExpr *addrExpr) | 
|---|
| 174 | { | 
|---|
| 175 | addrExpr->get_arg()->acceptMutator( *this ); | 
|---|
| 176 | addrExpr->set_arg( doSpecialization( addrExpr->get_results().front(), addrExpr->get_arg() ) ); | 
|---|
| 177 | return addrExpr; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | Expression* | 
|---|
| 181 | Specialize::mutate(CastExpr *castExpr) | 
|---|
| 182 | { | 
|---|
| 183 | castExpr->get_arg()->acceptMutator( *this ); | 
|---|
| 184 | castExpr->set_arg( doSpecialization( castExpr->get_results().front(), castExpr->get_arg() ) ); | 
|---|
| 185 | return castExpr; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | Expression* | 
|---|
| 189 | Specialize::mutate(LogicalExpr *logicalExpr) | 
|---|
| 190 | { | 
|---|
| 191 | return logicalExpr; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | Expression* | 
|---|
| 195 | Specialize::mutate(ConditionalExpr *condExpr) | 
|---|
| 196 | { | 
|---|
| 197 | return condExpr; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | Expression* | 
|---|
| 201 | Specialize::mutate(CommaExpr *commaExpr) | 
|---|
| 202 | { | 
|---|
| 203 | return commaExpr; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | } // namespace GenPoly | 
|---|