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 : Rob Schluntz |
---|
12 | // Last Modified On : Wed Jan 20 12:40:33 2016 |
---|
13 | // Update Count : 18 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> |
---|
17 | |
---|
18 | #include "Specialize.h" |
---|
19 | #include "GenPoly.h" |
---|
20 | #include "PolyMutator.h" |
---|
21 | |
---|
22 | #include "Parser/ParseNode.h" |
---|
23 | |
---|
24 | #include "SynTree/Expression.h" |
---|
25 | #include "SynTree/Statement.h" |
---|
26 | #include "SynTree/Type.h" |
---|
27 | #include "SynTree/TypeSubstitution.h" |
---|
28 | #include "SynTree/Mutator.h" |
---|
29 | #include "ResolvExpr/FindOpenVars.h" |
---|
30 | #include "Common/UniqueName.h" |
---|
31 | #include "Common/utility.h" |
---|
32 | |
---|
33 | namespace GenPoly { |
---|
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 ); |
---|
43 | virtual Expression * mutate( LogicalExpr *logicalExpr ); |
---|
44 | virtual Expression * mutate( ConditionalExpr *conditionalExpr ); |
---|
45 | virtual Expression * mutate( CommaExpr *commaExpr ); |
---|
46 | |
---|
47 | private: |
---|
48 | Expression *createThunkFunction( FunctionType *funType, Expression *actual, InferredParams *inferParams = 0 ); |
---|
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 | |
---|
65 | /// Looks up open variables in actual type, returning true if any of them are bound in the environment or formal type. |
---|
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 | |
---|
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 | } |
---|
142 | |
---|
143 | Expression * Specialize::doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams ) { |
---|
144 | assert( ! actual->get_results().empty() ); |
---|
145 | if ( needsSpecialization( formalType, actual->get_results().front(), env ) ) { |
---|
146 | FunctionType *funType; |
---|
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 ); |
---|
154 | } else { |
---|
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 | } |
---|
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() ); |
---|
169 | FunctionType *function = getFunctionType( appExpr->get_function()->get_results().front() ); |
---|
170 | assert( function ); |
---|
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 ); |
---|
194 | assert( ! addrExpr->get_results().empty() ); |
---|
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 ); |
---|
201 | if ( castExpr->get_results().empty() ) { |
---|
202 | // can't specialize if we don't have a return value |
---|
203 | return castExpr; |
---|
204 | } |
---|
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 | } |
---|
212 | } |
---|
213 | |
---|
214 | Expression * Specialize::mutate( LogicalExpr *logicalExpr ) { |
---|
215 | return logicalExpr; |
---|
216 | } |
---|
217 | |
---|
218 | Expression * Specialize::mutate( ConditionalExpr *condExpr ) { |
---|
219 | return condExpr; |
---|
220 | } |
---|
221 | |
---|
222 | Expression * Specialize::mutate( CommaExpr *commaExpr ) { |
---|
223 | return commaExpr; |
---|
224 | } |
---|
225 | } // namespace GenPoly |
---|
226 | |
---|
227 | // Local Variables: // |
---|
228 | // tab-width: 4 // |
---|
229 | // mode: c++ // |
---|
230 | // compile-command: "make install" // |
---|
231 | // End: // |
---|