source: translator/GenPoly/Specialize.cc @ 51587aa

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 51587aa was 51587aa, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: fourth groups of files

  • Property mode set to 100644
File size: 7.1 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// XXX.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By :
12// Last Modified On :
13// Update Count     : 0
14//
15/*
16 * This file is part of the Cforall project
17 *
18 * $Id: Specialize.cc,v 1.4 2005/08/29 20:14:13 rcbilson Exp $
19 *
20 */
21
22#include <cassert>
23
24#include "Specialize.h"
25#include "PolyMutator.h"
26
27#include "SynTree/Declaration.h"
28#include "SynTree/Statement.h"
29#include "SynTree/Expression.h"
30#include "SynTree/Type.h"
31#include "SynTree/TypeSubstitution.h"
32#include "SynTree/Mutator.h"
33#include "ResolvExpr/FindOpenVars.h"
34#include "UniqueName.h"
35#include "utility.h"
36
37
38namespace GenPoly {
39
40const std::list<Label> noLabels;
41
42class Specialize : public PolyMutator
43{
44public:
45  Specialize( std::string paramPrefix = "_p" );
46 
47  virtual Expression* mutate(ApplicationExpr *applicationExpr);
48  virtual Expression* mutate(AddressExpr *castExpr);
49  virtual Expression* mutate(CastExpr *castExpr);
50  virtual Expression* mutate(LogicalExpr *logicalExpr);
51  virtual Expression* mutate(ConditionalExpr *conditionalExpr);
52  virtual Expression* mutate(CommaExpr *commaExpr);
53
54private:
55  Expression *doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams = 0 );
56  void handleExplicitParams( ApplicationExpr *appExpr );
57 
58  UniqueName thunkNamer;
59  std::string paramPrefix;
60};
61
62void
63convertSpecializations( std::list< Declaration* >& translationUnit )
64{
65  Specialize specializer;
66  mutateAll( translationUnit, specializer );
67}
68
69Specialize::Specialize( std::string paramPrefix )
70  : thunkNamer( "_thunk" ), paramPrefix( paramPrefix )
71{
72}
73
74bool
75needsSpecialization( Type *formalType, Type *actualType, TypeSubstitution *env )
76{
77  if ( env ) {
78    using namespace ResolvExpr;
79    OpenVarSet openVars, closedVars;
80    AssertionSet need, have;
81    findOpenVars( formalType, openVars, closedVars, need, have, false );
82    findOpenVars( actualType, openVars, closedVars, need, have, true );
83    for ( OpenVarSet::const_iterator openVar = openVars.begin(); openVar != openVars.end(); ++openVar ) {
84      Type *boundType = env->lookup( openVar->first );
85      if ( ! boundType ) continue;
86      if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( boundType ) ) {
87        if ( closedVars.find( typeInst->get_name() ) == closedVars.end() ) {
88          return true;
89        }
90      } else {
91        return true;
92      }
93    }
94    return false;
95  } else {
96    return false;
97  }
98}
99
100Expression*
101Specialize::doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams )
102{
103  if ( needsSpecialization( formalType, actual->get_results().front(), env ) ) {
104    PointerType *ptrType;
105    FunctionType *funType;
106    if ( ( ptrType = dynamic_cast< PointerType* >( formalType ) ) && ( funType = dynamic_cast< FunctionType* >( ptrType->get_base() ) ) ) {
107      FunctionType *newType = funType->clone();
108      if ( env ) {
109        TypeSubstitution newEnv( *env );
110        // it is important to replace only occurrences of type variables that occur free in the
111        // thunk's type
112        newEnv.applyFree( newType );
113      }
114      FunctionDecl *thunkFunc = new FunctionDecl( thunkNamer.newName(), Declaration::NoStorageClass, LinkageSpec::C, newType, new CompoundStmt( std::list< std::string >() ), false );
115      thunkFunc->fixUniqueId();
116     
117      UniqueName paramNamer( paramPrefix );
118      ApplicationExpr *appExpr = new ApplicationExpr( actual );
119      for ( std::list< DeclarationWithType* >::iterator param = thunkFunc->get_functionType()->get_parameters().begin(); param != thunkFunc->get_functionType()->get_parameters().end(); ++param ) {
120        (*param)->set_name( paramNamer.newName() );
121        appExpr->get_args().push_back( new VariableExpr( *param ) );
122      }
123      appExpr->set_env( maybeClone( env ) );
124      if ( inferParams ) {
125        appExpr->get_inferParams() = *inferParams;
126      }
127     
128      // handle any specializations that may still be present
129      std::string oldParamPrefix = paramPrefix;
130      paramPrefix += "p";
131      std::list< Statement* > oldStmts;
132      oldStmts.splice( oldStmts.end(), stmtsToAdd );
133      handleExplicitParams( appExpr );
134      paramPrefix = oldParamPrefix;
135      thunkFunc->get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd );
136      stmtsToAdd.splice( stmtsToAdd.end(), oldStmts );
137     
138      Statement *appStmt;
139      if ( funType->get_returnVals().empty() ) {
140        appStmt = new ExprStmt( noLabels, appExpr );
141      } else {
142        appStmt = new ReturnStmt( noLabels, appExpr );
143      }
144      thunkFunc->get_statements()->get_kids().push_back( appStmt );
145      stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) );
146      return new AddressExpr( new VariableExpr( thunkFunc ) );
147    } else {
148      return actual;
149    }
150  } else {
151    return actual;
152  }
153}
154
155void
156Specialize::handleExplicitParams( ApplicationExpr *appExpr )
157{
158  // create thunks for the explicit parameters
159  assert( ! appExpr->get_function()->get_results().empty() );
160  PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() );
161  assert( pointer );
162  FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
163  std::list< DeclarationWithType* >::iterator formal;
164  std::list< Expression* >::iterator actual;
165  for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) {
166    *actual = doSpecialization( (*formal)->get_type(), *actual, &appExpr->get_inferParams() );
167  }
168}
169
170Expression* 
171Specialize::mutate(ApplicationExpr *appExpr)
172{
173  appExpr->get_function()->acceptMutator( *this );
174  mutateAll( appExpr->get_args(), *this );
175 
176  // create thunks for the inferred parameters
177  for ( InferredParams::iterator inferParam = appExpr->get_inferParams().begin(); inferParam != appExpr->get_inferParams().end(); ++inferParam ) {
178    inferParam->second.expr = doSpecialization( inferParam->second.formalType, inferParam->second.expr, &appExpr->get_inferParams() );
179  }
180 
181  handleExplicitParams(appExpr);
182 
183  return appExpr;
184}
185
186Expression* 
187Specialize::mutate(AddressExpr *addrExpr)
188{
189  addrExpr->get_arg()->acceptMutator( *this );
190  addrExpr->set_arg( doSpecialization( addrExpr->get_results().front(), addrExpr->get_arg() ) );
191  return addrExpr;
192}
193
194Expression* 
195Specialize::mutate(CastExpr *castExpr)
196{
197  castExpr->get_arg()->acceptMutator( *this );
198  castExpr->set_arg( doSpecialization( castExpr->get_results().front(), castExpr->get_arg() ) );
199  return castExpr;
200}
201
202Expression* 
203Specialize::mutate(LogicalExpr *logicalExpr)
204{
205  return logicalExpr;
206}
207
208Expression* 
209Specialize::mutate(ConditionalExpr *condExpr)
210{
211  return condExpr;
212}
213
214Expression* 
215Specialize::mutate(CommaExpr *commaExpr)
216{
217  return commaExpr;
218}
219
220} // namespace GenPoly
221// Local Variables: //
222// tab-width: 4 //
223// mode: c++ //
224// compile-command: "make install" //
225// End: //
Note: See TracBrowser for help on using the repository browser.