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