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