[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 | // |
---|
[f1e012b] | 7 | // Specialize.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[8cbf8cd] | 11 | // Last Modified By : Rob Schluntz |
---|
[fea7ca7] | 12 | // Last Modified On : Thu Apr 28 15:17:45 2016 |
---|
[771b3c3] | 13 | // Update Count : 24 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include <cassert> |
---|
| 17 | |
---|
| 18 | #include "Specialize.h" |
---|
[7754cde] | 19 | #include "GenPoly.h" |
---|
[51b7345] | 20 | #include "PolyMutator.h" |
---|
| 21 | |
---|
[68cd1ce] | 22 | #include "Parser/ParseNode.h" |
---|
| 23 | |
---|
[51b7345] | 24 | #include "SynTree/Expression.h" |
---|
[68cd1ce] | 25 | #include "SynTree/Statement.h" |
---|
[51b7345] | 26 | #include "SynTree/Type.h" |
---|
| 27 | #include "SynTree/TypeSubstitution.h" |
---|
| 28 | #include "SynTree/Mutator.h" |
---|
| 29 | #include "ResolvExpr/FindOpenVars.h" |
---|
[d3b7937] | 30 | #include "Common/UniqueName.h" |
---|
| 31 | #include "Common/utility.h" |
---|
[51b7345] | 32 | |
---|
| 33 | namespace GenPoly { |
---|
[01aeade] | 34 | const std::list<Label> noLabels; |
---|
| 35 | |
---|
| 36 | class Specialize : public PolyMutator { |
---|
| 37 | public: |
---|
| 38 | Specialize( std::string paramPrefix = "_p" ); |
---|
| 39 | |
---|
| 40 | virtual Expression * mutate( ApplicationExpr *applicationExpr ); |
---|
| 41 | virtual Expression * mutate( AddressExpr *castExpr ); |
---|
| 42 | virtual Expression * mutate( CastExpr *castExpr ); |
---|
[fea7ca7] | 43 | // virtual Expression * mutate( LogicalExpr *logicalExpr ); |
---|
| 44 | // virtual Expression * mutate( ConditionalExpr *conditionalExpr ); |
---|
| 45 | // virtual Expression * mutate( CommaExpr *commaExpr ); |
---|
[01aeade] | 46 | |
---|
| 47 | private: |
---|
[698664b3] | 48 | Expression *createThunkFunction( FunctionType *funType, Expression *actual, InferredParams *inferParams = 0 ); |
---|
[01aeade] | 49 | Expression *doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams = 0 ); |
---|
| 50 | void handleExplicitParams( ApplicationExpr *appExpr ); |
---|
| 51 | |
---|
| 52 | UniqueName thunkNamer; |
---|
| 53 | std::string paramPrefix; |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | void convertSpecializations( std::list< Declaration* >& translationUnit ) { |
---|
| 57 | Specialize specializer; |
---|
| 58 | mutateAll( translationUnit, specializer ); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | Specialize::Specialize( std::string paramPrefix ) |
---|
| 62 | : thunkNamer( "_thunk" ), paramPrefix( paramPrefix ) { |
---|
| 63 | } |
---|
| 64 | |
---|
[698664b3] | 65 | /// Looks up open variables in actual type, returning true if any of them are bound in the environment or formal type. |
---|
[01aeade] | 66 | bool needsSpecialization( Type *formalType, Type *actualType, TypeSubstitution *env ) { |
---|
| 67 | if ( env ) { |
---|
| 68 | using namespace ResolvExpr; |
---|
| 69 | OpenVarSet openVars, closedVars; |
---|
| 70 | AssertionSet need, have; |
---|
| 71 | findOpenVars( formalType, openVars, closedVars, need, have, false ); |
---|
| 72 | findOpenVars( actualType, openVars, closedVars, need, have, true ); |
---|
| 73 | for ( OpenVarSet::const_iterator openVar = openVars.begin(); openVar != openVars.end(); ++openVar ) { |
---|
| 74 | Type *boundType = env->lookup( openVar->first ); |
---|
| 75 | if ( ! boundType ) continue; |
---|
| 76 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( boundType ) ) { |
---|
| 77 | if ( closedVars.find( typeInst->get_name() ) == closedVars.end() ) { |
---|
| 78 | return true; |
---|
| 79 | } // if |
---|
| 80 | } else { |
---|
| 81 | return true; |
---|
| 82 | } // if |
---|
| 83 | } // for |
---|
| 84 | return false; |
---|
| 85 | } else { |
---|
| 86 | return false; |
---|
| 87 | } // if |
---|
| 88 | } |
---|
| 89 | |
---|
[698664b3] | 90 | /// Generates a thunk that calls `actual` with type `funType` and returns its address |
---|
| 91 | Expression * Specialize::createThunkFunction( FunctionType *funType, Expression *actual, InferredParams *inferParams ) { |
---|
| 92 | FunctionType *newType = funType->clone(); |
---|
| 93 | if ( env ) { |
---|
| 94 | TypeSubstitution newEnv( *env ); |
---|
| 95 | // it is important to replace only occurrences of type variables that occur free in the |
---|
| 96 | // thunk's type |
---|
| 97 | newEnv.applyFree( newType ); |
---|
| 98 | } // if |
---|
| 99 | // create new thunk with same signature as formal type (C linkage, empty body) |
---|
| 100 | FunctionDecl *thunkFunc = new FunctionDecl( thunkNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, newType, new CompoundStmt( std::list< std::string >() ), false, false ); |
---|
| 101 | thunkFunc->fixUniqueId(); |
---|
| 102 | |
---|
| 103 | // thread thunk parameters into call to actual function, naming thunk parameters as we go |
---|
| 104 | UniqueName paramNamer( paramPrefix ); |
---|
| 105 | ApplicationExpr *appExpr = new ApplicationExpr( actual ); |
---|
| 106 | for ( std::list< DeclarationWithType* >::iterator param = thunkFunc->get_functionType()->get_parameters().begin(); param != thunkFunc->get_functionType()->get_parameters().end(); ++param ) { |
---|
| 107 | (*param )->set_name( paramNamer.newName() ); |
---|
| 108 | appExpr->get_args().push_back( new VariableExpr( *param ) ); |
---|
| 109 | } // for |
---|
| 110 | appExpr->set_env( maybeClone( env ) ); |
---|
| 111 | if ( inferParams ) { |
---|
| 112 | appExpr->get_inferParams() = *inferParams; |
---|
| 113 | } // if |
---|
| 114 | |
---|
| 115 | // handle any specializations that may still be present |
---|
| 116 | std::string oldParamPrefix = paramPrefix; |
---|
| 117 | paramPrefix += "p"; |
---|
| 118 | // save stmtsToAdd in oldStmts |
---|
| 119 | std::list< Statement* > oldStmts; |
---|
| 120 | oldStmts.splice( oldStmts.end(), stmtsToAdd ); |
---|
| 121 | handleExplicitParams( appExpr ); |
---|
| 122 | paramPrefix = oldParamPrefix; |
---|
| 123 | // write any statements added for recursive specializations into the thunk body |
---|
| 124 | thunkFunc->get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd ); |
---|
| 125 | // restore oldStmts into stmtsToAdd |
---|
| 126 | stmtsToAdd.splice( stmtsToAdd.end(), oldStmts ); |
---|
| 127 | |
---|
| 128 | // add return (or valueless expression) to the thunk |
---|
| 129 | Statement *appStmt; |
---|
| 130 | if ( funType->get_returnVals().empty() ) { |
---|
| 131 | appStmt = new ExprStmt( noLabels, appExpr ); |
---|
| 132 | } else { |
---|
| 133 | appStmt = new ReturnStmt( noLabels, appExpr ); |
---|
| 134 | } // if |
---|
| 135 | thunkFunc->get_statements()->get_kids().push_back( appStmt ); |
---|
| 136 | |
---|
| 137 | // add thunk definition to queue of statements to add |
---|
| 138 | stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) ); |
---|
| 139 | // return address of thunk function as replacement expression |
---|
| 140 | return new AddressExpr( new VariableExpr( thunkFunc ) ); |
---|
| 141 | } |
---|
[f1e012b] | 142 | |
---|
[01aeade] | 143 | Expression * Specialize::doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams ) { |
---|
[771b3c3] | 144 | assert( ! actual->get_results().empty() ); // using front, should have this assert |
---|
[01aeade] | 145 | if ( needsSpecialization( formalType, actual->get_results().front(), env ) ) { |
---|
| 146 | FunctionType *funType; |
---|
[698664b3] | 147 | if ( ( funType = getFunctionType( formalType ) ) ) { |
---|
| 148 | ApplicationExpr *appExpr; |
---|
| 149 | VariableExpr *varExpr; |
---|
| 150 | if ( ( appExpr = dynamic_cast<ApplicationExpr*>( actual ) ) ) { |
---|
| 151 | return createThunkFunction( funType, appExpr->get_function(), inferParams ); |
---|
| 152 | } else if ( ( varExpr = dynamic_cast<VariableExpr*>( actual ) ) ) { |
---|
| 153 | return createThunkFunction( funType, varExpr, inferParams ); |
---|
[01aeade] | 154 | } else { |
---|
[698664b3] | 155 | // This likely won't work, as anything that could build an ApplicationExpr probably hit one of the previous two branches |
---|
| 156 | return createThunkFunction( funType, actual, inferParams ); |
---|
| 157 | } |
---|
[01aeade] | 158 | } else { |
---|
| 159 | return actual; |
---|
| 160 | } // if |
---|
| 161 | } else { |
---|
| 162 | return actual; |
---|
| 163 | } // if |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) { |
---|
| 167 | // create thunks for the explicit parameters |
---|
| 168 | assert( ! appExpr->get_function()->get_results().empty() ); |
---|
[698664b3] | 169 | FunctionType *function = getFunctionType( appExpr->get_function()->get_results().front() ); |
---|
| 170 | assert( function ); |
---|
[01aeade] | 171 | std::list< DeclarationWithType* >::iterator formal; |
---|
| 172 | std::list< Expression* >::iterator actual; |
---|
| 173 | for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) { |
---|
| 174 | *actual = doSpecialization( (*formal )->get_type(), *actual, &appExpr->get_inferParams() ); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | Expression * Specialize::mutate( ApplicationExpr *appExpr ) { |
---|
| 179 | appExpr->get_function()->acceptMutator( *this ); |
---|
| 180 | mutateAll( appExpr->get_args(), *this ); |
---|
| 181 | |
---|
| 182 | // create thunks for the inferred parameters |
---|
| 183 | for ( InferredParams::iterator inferParam = appExpr->get_inferParams().begin(); inferParam != appExpr->get_inferParams().end(); ++inferParam ) { |
---|
| 184 | inferParam->second.expr = doSpecialization( inferParam->second.formalType, inferParam->second.expr, &appExpr->get_inferParams() ); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | handleExplicitParams( appExpr ); |
---|
| 188 | |
---|
| 189 | return appExpr; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | Expression * Specialize::mutate( AddressExpr *addrExpr ) { |
---|
| 193 | addrExpr->get_arg()->acceptMutator( *this ); |
---|
[8cbf8cd] | 194 | assert( ! addrExpr->get_results().empty() ); |
---|
[01aeade] | 195 | addrExpr->set_arg( doSpecialization( addrExpr->get_results().front(), addrExpr->get_arg() ) ); |
---|
| 196 | return addrExpr; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | Expression * Specialize::mutate( CastExpr *castExpr ) { |
---|
| 200 | castExpr->get_arg()->acceptMutator( *this ); |
---|
[803deb1] | 201 | if ( castExpr->get_results().empty() ) { |
---|
| 202 | // can't specialize if we don't have a return value |
---|
| 203 | return castExpr; |
---|
| 204 | } |
---|
[698664b3] | 205 | Expression *specialized = doSpecialization( castExpr->get_results().front(), castExpr->get_arg() ); |
---|
| 206 | if ( specialized != castExpr->get_arg() ) { |
---|
| 207 | // assume here that the specialization incorporates the cast |
---|
| 208 | return specialized; |
---|
| 209 | } else { |
---|
| 210 | return castExpr; |
---|
| 211 | } |
---|
[01aeade] | 212 | } |
---|
| 213 | |
---|
[fea7ca7] | 214 | // Removing these for now. Richard put these in for some reason, but it's not clear why. |
---|
| 215 | // In particular, copy constructors produce a comma expression, and with this code the parts |
---|
| 216 | // of that comma expression are not specialized, which causes problems. |
---|
[01aeade] | 217 | |
---|
[fea7ca7] | 218 | // Expression * Specialize::mutate( LogicalExpr *logicalExpr ) { |
---|
| 219 | // return logicalExpr; |
---|
| 220 | // } |
---|
[01aeade] | 221 | |
---|
[fea7ca7] | 222 | // Expression * Specialize::mutate( ConditionalExpr *condExpr ) { |
---|
| 223 | // return condExpr; |
---|
| 224 | // } |
---|
| 225 | |
---|
| 226 | // Expression * Specialize::mutate( CommaExpr *commaExpr ) { |
---|
| 227 | // return commaExpr; |
---|
| 228 | // } |
---|
[51b7345] | 229 | } // namespace GenPoly |
---|
[01aeade] | 230 | |
---|
[51587aa] | 231 | // Local Variables: // |
---|
| 232 | // tab-width: 4 // |
---|
| 233 | // mode: c++ // |
---|
| 234 | // compile-command: "make install" // |
---|
| 235 | // End: // |
---|