source: src/GenPoly/Specialize.cc @ de62360d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since de62360d was de62360d, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

fix computed goto, fixed -std=, implicit typedefs for enum and aggregates, add _Noreturn _Thread_local

  • Property mode set to 100644
File size: 7.0 KB
Line 
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//
7// Specialize.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jun 13 15:54:07 2015
13// Update Count     : 6
14//
15
16#include <cassert>
17
18#include "Specialize.h"
19#include "PolyMutator.h"
20
21#include "Parser/ParseNode.h"
22
23#include "SynTree/Expression.h"
24#include "SynTree/Statement.h"
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
32namespace GenPoly {
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
99                                FunctionDecl *thunkFunc = new FunctionDecl( thunkNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, newType, new CompoundStmt( std::list< std::string >() ), false, false );
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        }
190} // namespace GenPoly
191
192// Local Variables: //
193// tab-width: 4 //
194// mode: c++ //
195// compile-command: "make install" //
196// End: //
Note: See TracBrowser for help on using the repository browser.