source: src/GenPoly/Specialize.cc@ 02e5ab6

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 string with_gc
Last change on this file since 02e5ab6 was 8cbf8cd, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

added some list emptiness checks in Specialize.cc

  • Property mode set to 100644
File size: 7.2 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 "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 assert( ! actual->get_results().empty() );
89 if ( needsSpecialization( formalType, actual->get_results().front(), env ) ) {
90 PointerType *ptrType;
91 FunctionType *funType;
92 if ( ( ptrType = dynamic_cast< PointerType* >( formalType ) ) && ( funType = dynamic_cast< FunctionType* >( ptrType->get_base() ) ) ) {
93 FunctionType *newType = funType->clone();
94 if ( env ) {
95 TypeSubstitution newEnv( *env );
96 // it is important to replace only occurrences of type variables that occur free in the
97 // thunk's type
98 newEnv.applyFree( newType );
99 } // if
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 UniqueName paramNamer( paramPrefix );
104 ApplicationExpr *appExpr = new ApplicationExpr( actual );
105 for ( std::list< DeclarationWithType* >::iterator param = thunkFunc->get_functionType()->get_parameters().begin(); param != thunkFunc->get_functionType()->get_parameters().end(); ++param ) {
106 (*param )->set_name( paramNamer.newName() );
107 appExpr->get_args().push_back( new VariableExpr( *param ) );
108 } // for
109 appExpr->set_env( maybeClone( env ) );
110 if ( inferParams ) {
111 appExpr->get_inferParams() = *inferParams;
112 } // if
113
114 // handle any specializations that may still be present
115 std::string oldParamPrefix = paramPrefix;
116 paramPrefix += "p";
117 std::list< Statement* > oldStmts;
118 oldStmts.splice( oldStmts.end(), stmtsToAdd );
119 handleExplicitParams( appExpr );
120 paramPrefix = oldParamPrefix;
121 thunkFunc->get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd );
122 stmtsToAdd.splice( stmtsToAdd.end(), oldStmts );
123
124 Statement *appStmt;
125 if ( funType->get_returnVals().empty() ) {
126 appStmt = new ExprStmt( noLabels, appExpr );
127 } else {
128 appStmt = new ReturnStmt( noLabels, appExpr );
129 } // if
130 thunkFunc->get_statements()->get_kids().push_back( appStmt );
131 stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) );
132 return new AddressExpr( new VariableExpr( thunkFunc ) );
133 } else {
134 return actual;
135 } // if
136 } else {
137 return actual;
138 } // if
139 }
140
141 void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) {
142 // create thunks for the explicit parameters
143 assert( ! appExpr->get_function()->get_results().empty() );
144 PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() );
145 assert( pointer );
146 FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
147 std::list< DeclarationWithType* >::iterator formal;
148 std::list< Expression* >::iterator actual;
149 for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) {
150 *actual = doSpecialization( (*formal )->get_type(), *actual, &appExpr->get_inferParams() );
151 }
152 }
153
154 Expression * Specialize::mutate( ApplicationExpr *appExpr ) {
155 appExpr->get_function()->acceptMutator( *this );
156 mutateAll( appExpr->get_args(), *this );
157
158 // create thunks for the inferred parameters
159 for ( InferredParams::iterator inferParam = appExpr->get_inferParams().begin(); inferParam != appExpr->get_inferParams().end(); ++inferParam ) {
160 inferParam->second.expr = doSpecialization( inferParam->second.formalType, inferParam->second.expr, &appExpr->get_inferParams() );
161 }
162
163 handleExplicitParams( appExpr );
164
165 return appExpr;
166 }
167
168 Expression * Specialize::mutate( AddressExpr *addrExpr ) {
169 addrExpr->get_arg()->acceptMutator( *this );
170 assert( ! addrExpr->get_results().empty() );
171 addrExpr->set_arg( doSpecialization( addrExpr->get_results().front(), addrExpr->get_arg() ) );
172 return addrExpr;
173 }
174
175 Expression * Specialize::mutate( CastExpr *castExpr ) {
176 castExpr->get_arg()->acceptMutator( *this );
177 if ( ! castExpr->get_results().empty() ) {
178 // this may not be the correct condition, but previously the next statement
179 // was happening unchecked, causing a crash on a cast to void
180 castExpr->set_arg( doSpecialization( castExpr->get_results().front(), castExpr->get_arg() ) );
181 }
182 return castExpr;
183 }
184
185 Expression * Specialize::mutate( LogicalExpr *logicalExpr ) {
186 return logicalExpr;
187 }
188
189 Expression * Specialize::mutate( ConditionalExpr *condExpr ) {
190 return condExpr;
191 }
192
193 Expression * Specialize::mutate( CommaExpr *commaExpr ) {
194 return commaExpr;
195 }
196} // namespace GenPoly
197
198// Local Variables: //
199// tab-width: 4 //
200// mode: c++ //
201// compile-command: "make install" //
202// End: //
Note: See TracBrowser for help on using the repository browser.