[51587aa] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[01aeade] | 7 | // Specialize.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[01aeade] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Tue May 19 07:55:09 2015 |
---|
| 13 | // Update Count : 4 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include <cassert> |
---|
| 17 | |
---|
| 18 | #include "Specialize.h" |
---|
| 19 | #include "PolyMutator.h" |
---|
| 20 | |
---|
| 21 | #include "SynTree/Declaration.h" |
---|
| 22 | #include "SynTree/Statement.h" |
---|
| 23 | #include "SynTree/Expression.h" |
---|
| 24 | #include "SynTree/Type.h" |
---|
| 25 | #include "SynTree/TypeSubstitution.h" |
---|
| 26 | #include "SynTree/Mutator.h" |
---|
| 27 | #include "ResolvExpr/FindOpenVars.h" |
---|
| 28 | #include "UniqueName.h" |
---|
| 29 | #include "utility.h" |
---|
| 30 | |
---|
| 31 | namespace GenPoly { |
---|
[01aeade] | 32 | const std::list<Label> noLabels; |
---|
| 33 | |
---|
| 34 | class Specialize : public PolyMutator { |
---|
| 35 | public: |
---|
| 36 | Specialize( std::string paramPrefix = "_p" ); |
---|
| 37 | |
---|
| 38 | virtual Expression * mutate( ApplicationExpr *applicationExpr ); |
---|
| 39 | virtual Expression * mutate( AddressExpr *castExpr ); |
---|
| 40 | virtual Expression * mutate( CastExpr *castExpr ); |
---|
| 41 | virtual Expression * mutate( LogicalExpr *logicalExpr ); |
---|
| 42 | virtual Expression * mutate( ConditionalExpr *conditionalExpr ); |
---|
| 43 | virtual Expression * mutate( CommaExpr *commaExpr ); |
---|
| 44 | |
---|
| 45 | private: |
---|
| 46 | Expression *doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams = 0 ); |
---|
| 47 | void handleExplicitParams( ApplicationExpr *appExpr ); |
---|
| 48 | |
---|
| 49 | UniqueName thunkNamer; |
---|
| 50 | std::string paramPrefix; |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | void convertSpecializations( std::list< Declaration* >& translationUnit ) { |
---|
| 54 | Specialize specializer; |
---|
| 55 | mutateAll( translationUnit, specializer ); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | Specialize::Specialize( std::string paramPrefix ) |
---|
| 59 | : thunkNamer( "_thunk" ), paramPrefix( paramPrefix ) { |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | bool needsSpecialization( Type *formalType, Type *actualType, TypeSubstitution *env ) { |
---|
| 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 | } // if |
---|
| 76 | } else { |
---|
| 77 | return true; |
---|
| 78 | } // if |
---|
| 79 | } // for |
---|
| 80 | return false; |
---|
| 81 | } else { |
---|
| 82 | return false; |
---|
| 83 | } // if |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | Expression * Specialize::doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams ) { |
---|
| 87 | if ( needsSpecialization( formalType, actual->get_results().front(), env ) ) { |
---|
| 88 | PointerType *ptrType; |
---|
| 89 | FunctionType *funType; |
---|
| 90 | if ( ( ptrType = dynamic_cast< PointerType* >( formalType ) ) && ( funType = dynamic_cast< FunctionType* >( ptrType->get_base() ) ) ) { |
---|
| 91 | FunctionType *newType = funType->clone(); |
---|
| 92 | if ( env ) { |
---|
| 93 | TypeSubstitution newEnv( *env ); |
---|
| 94 | // it is important to replace only occurrences of type variables that occur free in the |
---|
| 95 | // thunk's type |
---|
| 96 | newEnv.applyFree( newType ); |
---|
| 97 | } // if |
---|
| 98 | FunctionDecl *thunkFunc = new FunctionDecl( thunkNamer.newName(), Declaration::NoStorageClass, LinkageSpec::C, newType, new CompoundStmt( std::list< std::string >() ), false ); |
---|
| 99 | thunkFunc->fixUniqueId(); |
---|
| 100 | |
---|
| 101 | UniqueName paramNamer( paramPrefix ); |
---|
| 102 | ApplicationExpr *appExpr = new ApplicationExpr( actual ); |
---|
| 103 | for ( std::list< DeclarationWithType* >::iterator param = thunkFunc->get_functionType()->get_parameters().begin(); param != thunkFunc->get_functionType()->get_parameters().end(); ++param ) { |
---|
| 104 | (*param )->set_name( paramNamer.newName() ); |
---|
| 105 | appExpr->get_args().push_back( new VariableExpr( *param ) ); |
---|
| 106 | } // for |
---|
| 107 | appExpr->set_env( maybeClone( env ) ); |
---|
| 108 | if ( inferParams ) { |
---|
| 109 | appExpr->get_inferParams() = *inferParams; |
---|
| 110 | } // if |
---|
| 111 | |
---|
| 112 | // handle any specializations that may still be present |
---|
| 113 | std::string oldParamPrefix = paramPrefix; |
---|
| 114 | paramPrefix += "p"; |
---|
| 115 | std::list< Statement* > oldStmts; |
---|
| 116 | oldStmts.splice( oldStmts.end(), stmtsToAdd ); |
---|
| 117 | handleExplicitParams( appExpr ); |
---|
| 118 | paramPrefix = oldParamPrefix; |
---|
| 119 | thunkFunc->get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd ); |
---|
| 120 | stmtsToAdd.splice( stmtsToAdd.end(), oldStmts ); |
---|
| 121 | |
---|
| 122 | Statement *appStmt; |
---|
| 123 | if ( funType->get_returnVals().empty() ) { |
---|
| 124 | appStmt = new ExprStmt( noLabels, appExpr ); |
---|
| 125 | } else { |
---|
| 126 | appStmt = new ReturnStmt( noLabels, appExpr ); |
---|
| 127 | } // if |
---|
| 128 | thunkFunc->get_statements()->get_kids().push_back( appStmt ); |
---|
| 129 | stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) ); |
---|
| 130 | return new AddressExpr( new VariableExpr( thunkFunc ) ); |
---|
| 131 | } else { |
---|
| 132 | return actual; |
---|
| 133 | } // if |
---|
| 134 | } else { |
---|
| 135 | return actual; |
---|
| 136 | } // if |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) { |
---|
| 140 | // create thunks for the explicit parameters |
---|
| 141 | assert( ! appExpr->get_function()->get_results().empty() ); |
---|
| 142 | PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() ); |
---|
| 143 | assert( pointer ); |
---|
| 144 | FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ); |
---|
| 145 | std::list< DeclarationWithType* >::iterator formal; |
---|
| 146 | std::list< Expression* >::iterator actual; |
---|
| 147 | for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) { |
---|
| 148 | *actual = doSpecialization( (*formal )->get_type(), *actual, &appExpr->get_inferParams() ); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | Expression * Specialize::mutate( ApplicationExpr *appExpr ) { |
---|
| 153 | appExpr->get_function()->acceptMutator( *this ); |
---|
| 154 | mutateAll( appExpr->get_args(), *this ); |
---|
| 155 | |
---|
| 156 | // create thunks for the inferred parameters |
---|
| 157 | for ( InferredParams::iterator inferParam = appExpr->get_inferParams().begin(); inferParam != appExpr->get_inferParams().end(); ++inferParam ) { |
---|
| 158 | inferParam->second.expr = doSpecialization( inferParam->second.formalType, inferParam->second.expr, &appExpr->get_inferParams() ); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | handleExplicitParams( appExpr ); |
---|
| 162 | |
---|
| 163 | return appExpr; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | Expression * Specialize::mutate( AddressExpr *addrExpr ) { |
---|
| 167 | addrExpr->get_arg()->acceptMutator( *this ); |
---|
| 168 | addrExpr->set_arg( doSpecialization( addrExpr->get_results().front(), addrExpr->get_arg() ) ); |
---|
| 169 | return addrExpr; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | Expression * Specialize::mutate( CastExpr *castExpr ) { |
---|
| 173 | castExpr->get_arg()->acceptMutator( *this ); |
---|
| 174 | castExpr->set_arg( doSpecialization( castExpr->get_results().front(), castExpr->get_arg() ) ); |
---|
| 175 | return castExpr; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | Expression * Specialize::mutate( LogicalExpr *logicalExpr ) { |
---|
| 179 | return logicalExpr; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | Expression * Specialize::mutate( ConditionalExpr *condExpr ) { |
---|
| 183 | return condExpr; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | Expression * Specialize::mutate( CommaExpr *commaExpr ) { |
---|
| 187 | return commaExpr; |
---|
| 188 | } |
---|
[51b7345] | 189 | } // namespace GenPoly |
---|
[01aeade] | 190 | |
---|
[51587aa] | 191 | // Local Variables: // |
---|
| 192 | // tab-width: 4 // |
---|
| 193 | // mode: c++ // |
---|
| 194 | // compile-command: "make install" // |
---|
| 195 | // End: // |
---|