| [51587aa] | 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 | // | 
|---|
| [6943f051] | 7 | // CopyParams.cc -- | 
|---|
| [51587aa] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| [6943f051] | 11 | // Last Modified By : Rob Schluntz | 
|---|
| [01aeade] | 12 | // Last Modified On : Tue May 19 07:33:31 2015 | 
|---|
|  | 13 | // Update Count     : 1 | 
|---|
| [51587aa] | 14 | // | 
|---|
| [01aeade] | 15 |  | 
|---|
| [08fc48f] | 16 | #include <cassert>                 // for assert | 
|---|
|  | 17 | #include <list>                    // for list, _List_iterator, _List_const_... | 
|---|
|  | 18 | #include <map>                     // for map, _Rb_tree_const_iterator, map<... | 
|---|
|  | 19 | #include <set>                     // for set, set<>::const_iterator | 
|---|
|  | 20 | #include <string>                  // for string, operator== | 
|---|
|  | 21 | #include <utility>                 // for pair | 
|---|
| [51b73452] | 22 |  | 
|---|
| [08fc48f] | 23 | #include "Common/SemanticError.h"  // for SemanticError | 
|---|
|  | 24 | #include "Common/UniqueName.h"     // for UniqueName | 
|---|
|  | 25 | #include "SynTree/Declaration.h"   // for DeclarationWithType, TypeDecl, Fun... | 
|---|
|  | 26 | #include "SynTree/Expression.h"    // for VariableExpr, ApplicationExpr, Add... | 
|---|
|  | 27 | #include "SynTree/Label.h"         // for Label, noLabels | 
|---|
|  | 28 | #include "SynTree/Statement.h"     // for CompoundStmt, DeclStmt, ExprStmt | 
|---|
|  | 29 | #include "SynTree/SynTree.h"       // for UniqueId | 
|---|
|  | 30 | #include "SynTree/Type.h"          // for FunctionType, TypeInstType, Type | 
|---|
|  | 31 | #include "SynTree/Visitor.h"       // for acceptAll, Visitor | 
|---|
| [51b73452] | 32 |  | 
|---|
|  | 33 | namespace GenPoly { | 
|---|
| [01aeade] | 34 | class CopyParams : public Visitor { | 
|---|
|  | 35 | public: | 
|---|
|  | 36 | CopyParams(); | 
|---|
| [6943f051] | 37 |  | 
|---|
| [01aeade] | 38 | virtual void visit( FunctionDecl *funcDecl ); | 
|---|
|  | 39 | virtual void visit( AddressExpr *addrExpr ); | 
|---|
| [51b73452] | 40 |  | 
|---|
| [01aeade] | 41 | private: | 
|---|
|  | 42 | std::set< UniqueId > modVars; | 
|---|
|  | 43 | UniqueName namer; | 
|---|
|  | 44 | }; | 
|---|
| [51b73452] | 45 |  | 
|---|
| [fc638d2] | 46 | /// creates local copies of polymorphic function parameters | 
|---|
| [01aeade] | 47 | void copyParams( std::list< Declaration* > &translationUnit ) { | 
|---|
|  | 48 | CopyParams copier; | 
|---|
|  | 49 | acceptAll( translationUnit, copier ); | 
|---|
|  | 50 | } | 
|---|
| [51b73452] | 51 |  | 
|---|
| [01aeade] | 52 | CopyParams::CopyParams() : namer( "_cp" ) {} | 
|---|
| [51b73452] | 53 |  | 
|---|
| [01aeade] | 54 | void CopyParams::visit( FunctionDecl *funcDecl ) { | 
|---|
|  | 55 | if ( funcDecl->get_statements() ) { | 
|---|
|  | 56 | funcDecl->get_statements()->accept( *this ); | 
|---|
| [6943f051] | 57 |  | 
|---|
| [01aeade] | 58 | if ( ! modVars.empty() ) { | 
|---|
|  | 59 | std::map< std::string, DeclarationWithType* > assignOps; | 
|---|
| [fc638d2] | 60 | // xxx - this needs to use constructors, not assignment operators | 
|---|
| [01aeade] | 61 | // assume the assignment operator is the first assert param after any "type" parameter | 
|---|
| [8c49c0e] | 62 | for ( Type::ForallList::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) { | 
|---|
| [01aeade] | 63 | if ( (*tyVar)->get_kind() == TypeDecl::Any ) { | 
|---|
|  | 64 | assert( !(*tyVar)->get_assertions().empty() ); | 
|---|
| [6943f051] | 65 | assert( (*tyVar)->get_assertions().front()->get_name() == "?=?" ); | 
|---|
| [01aeade] | 66 | assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front(); | 
|---|
|  | 67 | } // if | 
|---|
|  | 68 | } // for | 
|---|
|  | 69 | for ( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { | 
|---|
|  | 70 | std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() ); | 
|---|
|  | 71 | if ( var != modVars.end() ) { | 
|---|
|  | 72 | TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() ); | 
|---|
|  | 73 | assert( typeInst ); | 
|---|
|  | 74 | std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() ); | 
|---|
|  | 75 | if ( assignOp != assignOps.end() ) { | 
|---|
|  | 76 | DeclarationWithType *oldParam = *param; | 
|---|
|  | 77 | *param = (*param)->clone(); | 
|---|
|  | 78 | (*param)->set_mangleName( namer.newName( (*param)->get_mangleName() ) ); | 
|---|
|  | 79 | ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) ); | 
|---|
|  | 80 | assign->get_args().push_back( new VariableExpr( oldParam ) ); | 
|---|
|  | 81 | assign->get_args().push_back( new VariableExpr( *param ) ); | 
|---|
|  | 82 | funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) ); | 
|---|
|  | 83 | funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) ); | 
|---|
|  | 84 | } // if | 
|---|
|  | 85 | modVars.erase( var ); | 
|---|
|  | 86 | } // if | 
|---|
|  | 87 | } // for | 
|---|
| [17cd4eb] | 88 | } // if | 
|---|
| [01aeade] | 89 | } // if | 
|---|
|  | 90 | } | 
|---|
| [51b73452] | 91 |  | 
|---|
| [01aeade] | 92 | // this test is insufficient because it is possible for values to be modified by being passed to other polymorphic | 
|---|
|  | 93 | // routines (e.g., assignment operators) without having their addresses explicitly taken. Some thought is needed to | 
|---|
|  | 94 | // make sure that all of the correct cases are identified where copies are necessary. | 
|---|
|  | 95 | // | 
|---|
|  | 96 | // As a temporary measure, for correctness at the expense of performance, ignore the modVars list entirely and copy | 
|---|
|  | 97 | // every parameter of TypeInstType* when visiting the FunctionDecl. | 
|---|
|  | 98 | void CopyParams::visit( AddressExpr *addrExpr ) { | 
|---|
|  | 99 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) { | 
|---|
|  | 100 | if ( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) { | 
|---|
|  | 101 | modVars.insert( varExpr->get_var()->get_uniqueId() ); | 
|---|
|  | 102 | } // if | 
|---|
|  | 103 | } // if | 
|---|
|  | 104 | } | 
|---|
| [51b73452] | 105 | } // namespace GenPoly | 
|---|
| [01aeade] | 106 |  | 
|---|
| [51587aa] | 107 | // Local Variables: // | 
|---|
|  | 108 | // tab-width: 4 // | 
|---|
|  | 109 | // mode: c++ // | 
|---|
|  | 110 | // compile-command: "make install" // | 
|---|
|  | 111 | // End: // | 
|---|