source: src/GenPoly/Specialize.cc@ e47f529

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since e47f529 was 7754cde, checked in by Aaron Moss <a3moss@…>, 10 years ago

First draft of passing generic size/align

  • Property mode set to 100644
File size: 8.4 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 : Rob Schluntz
12// Last Modified On : Tue Sep 22 14:04:13 2015
13// Update Count : 15
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 "UniqueName.h"
31#include "utility.h"
32
33namespace 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 if ( needsSpecialization( formalType, actual->get_results().front(), env ) ) {
145 FunctionType *funType;
146 if ( ( funType = getFunctionType( formalType ) ) ) {
147 ApplicationExpr *appExpr;
148 VariableExpr *varExpr;
149 if ( ( appExpr = dynamic_cast<ApplicationExpr*>( actual ) ) ) {
150 return createThunkFunction( funType, appExpr->get_function(), inferParams );
151 } else if ( ( varExpr = dynamic_cast<VariableExpr*>( actual ) ) ) {
152 return createThunkFunction( funType, varExpr, inferParams );
153 } else {
154 // This likely won't work, as anything that could build an ApplicationExpr probably hit one of the previous two branches
155 return createThunkFunction( funType, actual, inferParams );
156 }
157 } else {
158 return actual;
159 } // if
160 } else {
161 return actual;
162 } // if
163 }
164
165 void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) {
166 // create thunks for the explicit parameters
167 assert( ! appExpr->get_function()->get_results().empty() );
168 FunctionType *function = getFunctionType( appExpr->get_function()->get_results().front() );
169 assert( function );
170 std::list< DeclarationWithType* >::iterator formal;
171 std::list< Expression* >::iterator actual;
172 for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) {
173 *actual = doSpecialization( (*formal )->get_type(), *actual, &appExpr->get_inferParams() );
174 }
175 }
176
177 Expression * Specialize::mutate( ApplicationExpr *appExpr ) {
178 appExpr->get_function()->acceptMutator( *this );
179 mutateAll( appExpr->get_args(), *this );
180
181 // create thunks for the inferred parameters
182 for ( InferredParams::iterator inferParam = appExpr->get_inferParams().begin(); inferParam != appExpr->get_inferParams().end(); ++inferParam ) {
183 inferParam->second.expr = doSpecialization( inferParam->second.formalType, inferParam->second.expr, &appExpr->get_inferParams() );
184 }
185
186 handleExplicitParams( appExpr );
187
188 return appExpr;
189 }
190
191 Expression * Specialize::mutate( AddressExpr *addrExpr ) {
192 addrExpr->get_arg()->acceptMutator( *this );
193 assert( ! addrExpr->get_results().empty() );
194 addrExpr->set_arg( doSpecialization( addrExpr->get_results().front(), addrExpr->get_arg() ) );
195 return addrExpr;
196 }
197
198 Expression * Specialize::mutate( CastExpr *castExpr ) {
199 castExpr->get_arg()->acceptMutator( *this );
200 Expression *specialized = doSpecialization( castExpr->get_results().front(), castExpr->get_arg() );
201 if ( specialized != castExpr->get_arg() ) {
202 // assume here that the specialization incorporates the cast
203 return specialized;
204 } else {
205 return castExpr;
206 }
207 }
208
209 Expression * Specialize::mutate( LogicalExpr *logicalExpr ) {
210 return logicalExpr;
211 }
212
213 Expression * Specialize::mutate( ConditionalExpr *condExpr ) {
214 return condExpr;
215 }
216
217 Expression * Specialize::mutate( CommaExpr *commaExpr ) {
218 return commaExpr;
219 }
220} // namespace GenPoly
221
222// Local Variables: //
223// tab-width: 4 //
224// mode: c++ //
225// compile-command: "make install" //
226// End: //
Note: See TracBrowser for help on using the repository browser.