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 | // CopyParams.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 May 19 07:33:31 2015 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
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 |
---|
22 | |
---|
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 |
---|
32 | |
---|
33 | namespace GenPoly { |
---|
34 | class CopyParams : public Visitor { |
---|
35 | public: |
---|
36 | CopyParams(); |
---|
37 | |
---|
38 | virtual void visit( FunctionDecl *funcDecl ); |
---|
39 | virtual void visit( AddressExpr *addrExpr ); |
---|
40 | |
---|
41 | private: |
---|
42 | std::set< UniqueId > modVars; |
---|
43 | UniqueName namer; |
---|
44 | }; |
---|
45 | |
---|
46 | /// creates local copies of polymorphic function parameters |
---|
47 | void copyParams( std::list< Declaration* > &translationUnit ) { |
---|
48 | CopyParams copier; |
---|
49 | acceptAll( translationUnit, copier ); |
---|
50 | } |
---|
51 | |
---|
52 | CopyParams::CopyParams() : namer( "_cp" ) {} |
---|
53 | |
---|
54 | void CopyParams::visit( FunctionDecl *funcDecl ) { |
---|
55 | if ( funcDecl->statements ) { |
---|
56 | funcDecl->statements->accept( *this ); |
---|
57 | |
---|
58 | if ( ! modVars.empty() ) { |
---|
59 | std::map< std::string, DeclarationWithType* > assignOps; |
---|
60 | // xxx - this needs to use constructors, not assignment operators |
---|
61 | // assume the assignment operator is the first assert param after any "type" parameter |
---|
62 | for ( Type::ForallList::const_iterator tyVar = funcDecl->type->forall.begin(); tyVar != funcDecl->type->forall.end(); ++tyVar ) { |
---|
63 | if ( (*tyVar)->get_kind() == TypeDecl::Any ) { |
---|
64 | assert( !(*tyVar)->assertions.empty() ); |
---|
65 | assert( (*tyVar)->assertions.front()->name == "?=?" ); |
---|
66 | assignOps[ (*tyVar)->name ] = (*tyVar)->assertions.front(); |
---|
67 | } // if |
---|
68 | } // for |
---|
69 | for ( std::list< DeclarationWithType* >::iterator param = funcDecl->type->parameters.begin(); param != funcDecl->type->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 | assertf( typeInst, "Found in modVars for function %s: unique id: %u, %s", funcDecl->name.c_str(), (*param)->get_uniqueId(), toString( *param ).c_str() ); |
---|
74 | std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->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->args.push_back( new VariableExpr( oldParam ) ); |
---|
81 | assign->args.push_back( new VariableExpr( *param ) ); |
---|
82 | funcDecl->statements->push_front( new ExprStmt( noLabels, assign ) ); |
---|
83 | funcDecl->statements->push_front( new DeclStmt( noLabels, oldParam ) ); |
---|
84 | } // if |
---|
85 | modVars.erase( var ); |
---|
86 | } // if |
---|
87 | } // for |
---|
88 | } // if |
---|
89 | } // if |
---|
90 | } |
---|
91 | |
---|
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->arg ) ) { |
---|
100 | if ( dynamic_cast< TypeInstType* >( varExpr->var->get_type() ) ) { |
---|
101 | assertf( varExpr->var->get_uniqueId(), "Address-taken variable does not have a unique ID: %s", toString( varExpr->var ).c_str() ); |
---|
102 | modVars.insert( varExpr->var->get_uniqueId() ); |
---|
103 | std::cerr << "added unique id: " << varExpr->var->get_uniqueId() << " var: " << varExpr->var << std::endl; |
---|
104 | } // if |
---|
105 | } // if |
---|
106 | } |
---|
107 | } // namespace GenPoly |
---|
108 | |
---|
109 | // Local Variables: // |
---|
110 | // tab-width: 4 // |
---|
111 | // mode: c++ // |
---|
112 | // compile-command: "make install" // |
---|
113 | // End: // |
---|