[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
|
---|
[de62360d] | 12 | // Last Modified On : Sat Jun 13 15:54:07 2015
|
---|
| 13 | // Update Count : 6
|
---|
[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:
|
---|
| 47 | Expression *doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams = 0 );
|
---|
| 48 | void handleExplicitParams( ApplicationExpr *appExpr );
|
---|
| 49 |
|
---|
| 50 | UniqueName thunkNamer;
|
---|
| 51 | std::string paramPrefix;
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | void convertSpecializations( std::list< Declaration* >& translationUnit ) {
|
---|
| 55 | Specialize specializer;
|
---|
| 56 | mutateAll( translationUnit, specializer );
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | Specialize::Specialize( std::string paramPrefix )
|
---|
| 60 | : thunkNamer( "_thunk" ), paramPrefix( paramPrefix ) {
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | bool needsSpecialization( Type *formalType, Type *actualType, TypeSubstitution *env ) {
|
---|
| 64 | if ( env ) {
|
---|
| 65 | using namespace ResolvExpr;
|
---|
| 66 | OpenVarSet openVars, closedVars;
|
---|
| 67 | AssertionSet need, have;
|
---|
| 68 | findOpenVars( formalType, openVars, closedVars, need, have, false );
|
---|
| 69 | findOpenVars( actualType, openVars, closedVars, need, have, true );
|
---|
| 70 | for ( OpenVarSet::const_iterator openVar = openVars.begin(); openVar != openVars.end(); ++openVar ) {
|
---|
| 71 | Type *boundType = env->lookup( openVar->first );
|
---|
| 72 | if ( ! boundType ) continue;
|
---|
| 73 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( boundType ) ) {
|
---|
| 74 | if ( closedVars.find( typeInst->get_name() ) == closedVars.end() ) {
|
---|
| 75 | return true;
|
---|
| 76 | } // if
|
---|
| 77 | } else {
|
---|
| 78 | return true;
|
---|
| 79 | } // if
|
---|
| 80 | } // for
|
---|
| 81 | return false;
|
---|
| 82 | } else {
|
---|
| 83 | return false;
|
---|
| 84 | } // if
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | Expression * Specialize::doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams ) {
|
---|
| 88 | if ( needsSpecialization( formalType, actual->get_results().front(), env ) ) {
|
---|
| 89 | PointerType *ptrType;
|
---|
| 90 | FunctionType *funType;
|
---|
| 91 | if ( ( ptrType = dynamic_cast< PointerType* >( formalType ) ) && ( funType = dynamic_cast< FunctionType* >( ptrType->get_base() ) ) ) {
|
---|
| 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
|
---|
[de62360d] | 99 | FunctionDecl *thunkFunc = new FunctionDecl( thunkNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, newType, new CompoundStmt( std::list< std::string >() ), false, false );
|
---|
[01aeade] | 100 | thunkFunc->fixUniqueId();
|
---|
| 101 |
|
---|
| 102 | UniqueName paramNamer( paramPrefix );
|
---|
| 103 | ApplicationExpr *appExpr = new ApplicationExpr( actual );
|
---|
| 104 | for ( std::list< DeclarationWithType* >::iterator param = thunkFunc->get_functionType()->get_parameters().begin(); param != thunkFunc->get_functionType()->get_parameters().end(); ++param ) {
|
---|
| 105 | (*param )->set_name( paramNamer.newName() );
|
---|
| 106 | appExpr->get_args().push_back( new VariableExpr( *param ) );
|
---|
| 107 | } // for
|
---|
| 108 | appExpr->set_env( maybeClone( env ) );
|
---|
| 109 | if ( inferParams ) {
|
---|
| 110 | appExpr->get_inferParams() = *inferParams;
|
---|
| 111 | } // if
|
---|
| 112 |
|
---|
| 113 | // handle any specializations that may still be present
|
---|
| 114 | std::string oldParamPrefix = paramPrefix;
|
---|
| 115 | paramPrefix += "p";
|
---|
| 116 | std::list< Statement* > oldStmts;
|
---|
| 117 | oldStmts.splice( oldStmts.end(), stmtsToAdd );
|
---|
| 118 | handleExplicitParams( appExpr );
|
---|
| 119 | paramPrefix = oldParamPrefix;
|
---|
| 120 | thunkFunc->get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd );
|
---|
| 121 | stmtsToAdd.splice( stmtsToAdd.end(), oldStmts );
|
---|
| 122 |
|
---|
| 123 | Statement *appStmt;
|
---|
| 124 | if ( funType->get_returnVals().empty() ) {
|
---|
| 125 | appStmt = new ExprStmt( noLabels, appExpr );
|
---|
| 126 | } else {
|
---|
| 127 | appStmt = new ReturnStmt( noLabels, appExpr );
|
---|
| 128 | } // if
|
---|
| 129 | thunkFunc->get_statements()->get_kids().push_back( appStmt );
|
---|
| 130 | stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) );
|
---|
| 131 | return new AddressExpr( new VariableExpr( thunkFunc ) );
|
---|
| 132 | } else {
|
---|
| 133 | return actual;
|
---|
| 134 | } // if
|
---|
| 135 | } else {
|
---|
| 136 | return actual;
|
---|
| 137 | } // if
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) {
|
---|
| 141 | // create thunks for the explicit parameters
|
---|
| 142 | assert( ! appExpr->get_function()->get_results().empty() );
|
---|
| 143 | PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() );
|
---|
| 144 | assert( pointer );
|
---|
| 145 | FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
|
---|
| 146 | std::list< DeclarationWithType* >::iterator formal;
|
---|
| 147 | std::list< Expression* >::iterator actual;
|
---|
| 148 | for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) {
|
---|
| 149 | *actual = doSpecialization( (*formal )->get_type(), *actual, &appExpr->get_inferParams() );
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | Expression * Specialize::mutate( ApplicationExpr *appExpr ) {
|
---|
| 154 | appExpr->get_function()->acceptMutator( *this );
|
---|
| 155 | mutateAll( appExpr->get_args(), *this );
|
---|
| 156 |
|
---|
| 157 | // create thunks for the inferred parameters
|
---|
| 158 | for ( InferredParams::iterator inferParam = appExpr->get_inferParams().begin(); inferParam != appExpr->get_inferParams().end(); ++inferParam ) {
|
---|
| 159 | inferParam->second.expr = doSpecialization( inferParam->second.formalType, inferParam->second.expr, &appExpr->get_inferParams() );
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | handleExplicitParams( appExpr );
|
---|
| 163 |
|
---|
| 164 | return appExpr;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | Expression * Specialize::mutate( AddressExpr *addrExpr ) {
|
---|
| 168 | addrExpr->get_arg()->acceptMutator( *this );
|
---|
| 169 | addrExpr->set_arg( doSpecialization( addrExpr->get_results().front(), addrExpr->get_arg() ) );
|
---|
| 170 | return addrExpr;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | Expression * Specialize::mutate( CastExpr *castExpr ) {
|
---|
| 174 | castExpr->get_arg()->acceptMutator( *this );
|
---|
| 175 | castExpr->set_arg( doSpecialization( castExpr->get_results().front(), castExpr->get_arg() ) );
|
---|
| 176 | return castExpr;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | Expression * Specialize::mutate( LogicalExpr *logicalExpr ) {
|
---|
| 180 | return logicalExpr;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | Expression * Specialize::mutate( ConditionalExpr *condExpr ) {
|
---|
| 184 | return condExpr;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | Expression * Specialize::mutate( CommaExpr *commaExpr ) {
|
---|
| 188 | return commaExpr;
|
---|
| 189 | }
|
---|
[51b73452] | 190 | } // namespace GenPoly
|
---|
[01aeade] | 191 |
|
---|
[51587aa] | 192 | // Local Variables: //
|
---|
| 193 | // tab-width: 4 //
|
---|
| 194 | // mode: c++ //
|
---|
| 195 | // compile-command: "make install" //
|
---|
| 196 | // End: //
|
---|