Changeset 51587aa for translator/Tuples
- Timestamp:
- May 18, 2015, 11:45:33 PM (11 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, stuck-waitfor-destruct, with_gc
- Children:
- 01aeade
- Parents:
- 0dd3a2f
- Location:
- translator/Tuples
- Files:
-
- 16 edited
-
AssignExpand.cc (modified) (2 diffs)
-
AssignExpand.h (modified) (2 diffs)
-
FlattenTuple.cc (modified) (2 diffs)
-
FlattenTuple.h (modified) (2 diffs)
-
FunctionChecker.cc (modified) (2 diffs)
-
FunctionChecker.h (modified) (2 diffs)
-
FunctionFixer.cc (modified) (2 diffs)
-
FunctionFixer.h (modified) (2 diffs)
-
MultRet.cc (modified) (2 diffs)
-
MultRet.h (modified) (2 diffs)
-
Mutate.cc (modified) (2 diffs)
-
Mutate.h (modified) (2 diffs)
-
NameMatcher.cc (modified) (1 diff)
-
NameMatcher.h (modified) (2 diffs)
-
TupleAssignment.cc (modified) (2 diffs)
-
TupleAssignment.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
translator/Tuples/AssignExpand.cc
r0dd3a2f r51587aa 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 // AssignExpand.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 11:24:47 2015 13 // Update Count : 2 14 // 15 1 16 #include <ctime> 2 17 #include <cstdlib> … … 14 29 15 30 namespace Tuples { 16 AssignExpander::AssignExpander() : temporaryNamer("__tpl") {}31 AssignExpander::AssignExpander() : temporaryNamer("__tpl") {} 17 32 18 Statement *AssignExpander::mutate( ExprStmt *exprStmt ) {19 replace.clear();20 extra.clear();21 extra2.clear();22 exprStmt->set_expr( maybeMutate( exprStmt->get_expr(), *this ) );33 Statement *AssignExpander::mutate( ExprStmt *exprStmt ) { 34 replace.clear(); 35 extra.clear(); 36 extra2.clear(); 37 exprStmt->set_expr( maybeMutate( exprStmt->get_expr(), *this ) ); 23 38 24 CompoundStmt *newSt = 0;25 if (! extra.empty() ) {26 if ( ! newSt )27 newSt= new CompoundStmt(std::list<Label>());39 CompoundStmt *newSt = 0; 40 if (! extra.empty() ) { 41 if ( ! newSt ) 42 newSt= new CompoundStmt(std::list<Label>()); 28 43 29 newSt->get_kids().splice(newSt->get_kids().end(), extra);30 } 44 newSt->get_kids().splice(newSt->get_kids().end(), extra); 45 } // if 31 46 32 if (! extra2.empty() ) {33 if ( ! newSt )34 newSt= new CompoundStmt(std::list<Label>());47 if ( ! extra2.empty() ) { 48 if ( ! newSt ) 49 newSt= new CompoundStmt(std::list<Label>()); 35 50 36 newSt->get_kids().splice(newSt->get_kids().end(), extra2);37 }51 newSt->get_kids().splice(newSt->get_kids().end(), extra2); 52 } 38 53 39 if (! replace.empty() ) {40 if ( ! newSt )41 newSt= new CompoundStmt(std::list<Label>());54 if ( ! replace.empty() ) { 55 if ( ! newSt ) 56 newSt= new CompoundStmt(std::list<Label>()); 42 57 43 for ( std::list<Expression *>::iterator r = replace.begin(); r != replace.end(); r++ )44 newSt->get_kids().push_back( new ExprStmt( std::list<Label>(), *r ));45 }58 for ( std::list<Expression *>::iterator r = replace.begin(); r != replace.end(); r++ ) 59 newSt->get_kids().push_back( new ExprStmt( std::list<Label>(), *r )); 60 } 46 61 47 if ( newSt ) return newSt; else return exprStmt;48 }62 if ( newSt ) return newSt; else return exprStmt; 63 } 49 64 50 Expression *AssignExpander::mutate( SolvedTupleExpr *tupleExpr ) {51 /*52 std::list<Expression *> &exprs = tupleExpr->get_exprs();65 Expression *AssignExpander::mutate( SolvedTupleExpr *tupleExpr ) { 66 /* 67 std::list<Expression *> &exprs = tupleExpr->get_exprs(); 53 68 54 if ( tupleExpr->get_type() == SolvedTupleExpr::MASS ) {55 // extract lhs of assignments, assert that rhs is the same, create temporaries56 assert (! exprs.empty());57 ApplicationExpr *ap1 = dynamic_cast< ApplicationExpr * >( exprs.front() );58 std::list<Expression *> &args = ap1->get_args();59 assert(args.size() == 2);60 std::list<Type *> &temp_types = args.back()->get_results();61 assert(temp_types.size() == 1);62 extra.push_back(new DeclStmt( std::list<Label>(), new ObjectDecl(temporaryNamer.newName(), Declaration::Auto, LinkageSpec::C, 0, temp_types.front(), 0 ) ));69 if ( tupleExpr->get_type() == SolvedTupleExpr::MASS ) { 70 // extract lhs of assignments, assert that rhs is the same, create temporaries 71 assert (! exprs.empty()); 72 ApplicationExpr *ap1 = dynamic_cast< ApplicationExpr * >( exprs.front() ); 73 std::list<Expression *> &args = ap1->get_args(); 74 assert(args.size() == 2); 75 std::list<Type *> &temp_types = args.back()->get_results(); 76 assert(temp_types.size() == 1); 77 extra.push_back(new DeclStmt( std::list<Label>(), new ObjectDecl(temporaryNamer.newName(), Declaration::Auto, LinkageSpec::C, 0, temp_types.front(), 0 ) )); 63 78 64 for ( std::list<Expression *>::iterator e = exprs.begin(); e != exprs.end(); e++ ) {65 ApplicationExpr *ap = dynamic_cast< ApplicationExpr * >( *e );66 assert( ap != 0 );67 replace.push_back(ap);68 }79 for ( std::list<Expression *>::iterator e = exprs.begin(); e != exprs.end(); e++ ) { 80 ApplicationExpr *ap = dynamic_cast< ApplicationExpr * >( *e ); 81 assert( ap != 0 ); 82 replace.push_back(ap); 83 } 69 84 70 return tupleExpr;71 } else if ( tupleExpr->get_type() == SolvedTupleExpr::MULTIPLE ||85 return tupleExpr; 86 } else if ( tupleExpr->get_type() == SolvedTupleExpr::MULTIPLE || 72 87 tupleExpr->get_type() == SolvedTupleExpr::MASS ) */ { 73 std::list<Expression *> &comps = tupleExpr->get_exprs();74 for ( std::list<Expression *>::iterator i = comps.begin(); i != comps.end(); ++i ) {75 std::list<Statement *> decls;76 std::list<Statement *> temps;77 std::list<Statement *> assigns;78 if ( ApplicationExpr *app = dynamic_cast< ApplicationExpr * >(*i) ) {79 assert( app->get_args().size() == 2 );88 std::list<Expression *> &comps = tupleExpr->get_exprs(); 89 for ( std::list<Expression *>::iterator i = comps.begin(); i != comps.end(); ++i ) { 90 std::list<Statement *> decls; 91 std::list<Statement *> temps; 92 std::list<Statement *> assigns; 93 if ( ApplicationExpr *app = dynamic_cast< ApplicationExpr * >(*i) ) { 94 assert( app->get_args().size() == 2 ); 80 95 81 Expression *lhsT = app->get_args().front();82 Expression *rhsT = app->get_args().back();83 // after the round of type analysis this should be true84 assert( lhsT->get_results().size() == 1 );85 assert( rhsT->get_results().size() == 1 );86 // declare temporaries87 ObjectDecl *lhs = new ObjectDecl( temporaryNamer.newName("_lhs_"), Declaration::NoStorageClass, LinkageSpec::Intrinsic, 0,88 lhsT->get_results().front(), 0 );89 decls.push_back( new DeclStmt( std::list< Label >(), lhs ) );90 ObjectDecl *rhs = new ObjectDecl( temporaryNamer.newName("_rhs_"), Declaration::NoStorageClass, LinkageSpec::Intrinsic, 0,91 rhsT->get_results().front(), 0);92 decls.push_back( new DeclStmt( std::list< Label >(), rhs ));96 Expression *lhsT = app->get_args().front(); 97 Expression *rhsT = app->get_args().back(); 98 // after the round of type analysis this should be true 99 assert( lhsT->get_results().size() == 1 ); 100 assert( rhsT->get_results().size() == 1 ); 101 // declare temporaries 102 ObjectDecl *lhs = new ObjectDecl( temporaryNamer.newName("_lhs_"), Declaration::NoStorageClass, LinkageSpec::Intrinsic, 0, 103 lhsT->get_results().front(), 0 ); 104 decls.push_back( new DeclStmt( std::list< Label >(), lhs ) ); 105 ObjectDecl *rhs = new ObjectDecl( temporaryNamer.newName("_rhs_"), Declaration::NoStorageClass, LinkageSpec::Intrinsic, 0, 106 rhsT->get_results().front(), 0); 107 decls.push_back( new DeclStmt( std::list< Label >(), rhs )); 93 108 94 109 95 // create temporary for lhs, assign address96 UntypedExpr *assgnL = new UntypedExpr( new NameExpr( "?=?" ) );97 assgnL->get_args().push_back( new VariableExpr( lhs ) );98 assgnL->get_args().push_back( lhsT );99 temps.push_back( new ExprStmt(std::list<Label>(), assgnL) );110 // create temporary for lhs, assign address 111 UntypedExpr *assgnL = new UntypedExpr( new NameExpr( "?=?" ) ); 112 assgnL->get_args().push_back( new VariableExpr( lhs ) ); 113 assgnL->get_args().push_back( lhsT ); 114 temps.push_back( new ExprStmt(std::list<Label>(), assgnL) ); 100 115 101 // create temporary for rhs, assign value102 UntypedExpr *assgnR = new UntypedExpr( new NameExpr( "?=?" ) );103 assgnR->get_args().push_back( new VariableExpr( rhs ) );104 assgnR->get_args().push_back( rhsT );105 temps.push_back( new ExprStmt(std::list<Label>(), assgnR) );116 // create temporary for rhs, assign value 117 UntypedExpr *assgnR = new UntypedExpr( new NameExpr( "?=?" ) ); 118 assgnR->get_args().push_back( new VariableExpr( rhs ) ); 119 assgnR->get_args().push_back( rhsT ); 120 temps.push_back( new ExprStmt(std::list<Label>(), assgnR) ); 106 121 107 // assign rhs to lhs 108 UntypedExpr *assgn = new UntypedExpr( new NameExpr( "?=?" ) ); 109 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 110 deref->get_args().push_back( new VariableExpr( lhs ) ); 111 assgn->get_args().push_back( deref ); 112 assgn->get_args().push_back( new VariableExpr( rhs ) ); 113 assigns.push_back( new ExprStmt(std::list<Label>(), assgn) ); 114 115 } else 116 throw CompilerError("Solved Tuple should contain only assignment statements"); 122 // assign rhs to lhs 123 UntypedExpr *assgn = new UntypedExpr( new NameExpr( "?=?" ) ); 124 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); 125 deref->get_args().push_back( new VariableExpr( lhs ) ); 126 assgn->get_args().push_back( deref ); 127 assgn->get_args().push_back( new VariableExpr( rhs ) ); 128 assigns.push_back( new ExprStmt(std::list<Label>(), assgn) ); 129 } else 130 throw CompilerError("Solved Tuple should contain only assignment statements"); 117 131 118 extra.splice( extra.begin(), decls ); 119 extra.splice( extra.end(), temps ); 120 extra2.splice( extra2.end(), assigns ); 121 } 122 123 return tupleExpr; 124 } 125 126 throw 0; // shouldn't be here 127 } 128 129 132 extra.splice( extra.begin(), decls ); 133 extra.splice( extra.end(), temps ); 134 extra2.splice( extra2.end(), assigns ); 135 } // for 136 return tupleExpr; 137 } 138 throw 0; // shouldn't be here 139 } 130 140 } // namespace Tuples 131 141 142 // Local Variables: // 143 // tab-width: 4 // 144 // mode: c++ // 145 // compile-command: "make install" // 146 // End: // -
translator/Tuples/AssignExpand.h
r0dd3a2f r51587aa 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 // AssignExpand.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 11:25:53 2015 13 // Update Count : 3 14 // 15 1 16 #ifndef _ASSIGN_EXPAND_H_ 2 17 #define _ASSIGN_EXPAND_H_ … … 11 26 12 27 namespace Tuples { 13 class AssignExpander : public Mutator 14 { 15 typedef Mutator Parent; 16 17 public: 18 AssignExpander(); 19 virtual Statement *mutate( ExprStmt *expr ); 20 virtual Expression *mutate( SolvedTupleExpr *tupleExpr ); 21 22 private: 23 std::list<Statement *> extra, extra2; 24 std::list<Expression *> replace; 25 UniqueName temporaryNamer; 26 }; 27 28 class AssignExpander : public Mutator { 29 typedef Mutator Parent; 30 public: 31 AssignExpander(); 32 virtual Statement *mutate( ExprStmt *expr ); 33 virtual Expression *mutate( SolvedTupleExpr *tupleExpr ); 34 private: 35 std::list<Statement *> extra, extra2; 36 std::list<Expression *> replace; 37 UniqueName temporaryNamer; 38 }; 28 39 } // namespace Tuples 29 40 30 41 31 #endif // #ifndef_ASSIGN_EXPAND_H_42 #endif // _ASSIGN_EXPAND_H_ 32 43 33 / *34 Local Variables: 35 mode: c++ 36 End: 37 */44 // Local Variables: // 45 // tab-width: 4 // 46 // mode: c++ // 47 // compile-command: "make install" // 48 // End: // -
translator/Tuples/FlattenTuple.cc
r0dd3a2f r51587aa 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 // FlattenTuple.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 11:26:56 2015 13 // Update Count : 1 14 // 15 1 16 #include <list> 2 17 #include <vector> … … 6 21 #include "FlattenTuple.h" 7 22 23 namespace Tuples { 24 FlattenTuple::FlattenTuple() { 25 } 8 26 9 namespace Tuples { 27 FlattenTuple::~FlattenTuple() { 28 } 10 29 11 FlattenTuple::FlattenTuple() 12 { 13 } 30 Expression *FlattenTuple::mutate( TupleExpr *tupleExpr ) { 31 CollectArgs c; 14 32 15 FlattenTuple::~FlattenTuple() 16 { 17 } 33 acceptAll( tupleExpr->get_exprs(), c ); 34 tupleExpr->set_exprs( c.get_args() ); 18 35 19 Expression *FlattenTuple::mutate( TupleExpr *tupleExpr ) 20 { 21 CollectArgs c; 36 return tupleExpr; 37 } 22 38 23 acceptAll( tupleExpr->get_exprs(), c ); 24 tupleExpr->set_exprs( c.get_args() ); 39 void FlattenTuple::CollectArgs::visit( UntypedExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 40 void FlattenTuple::CollectArgs::visit( NameExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 41 void FlattenTuple::CollectArgs::visit( CastExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 42 void FlattenTuple::CollectArgs::visit( AddressExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 43 void FlattenTuple::CollectArgs::visit( UntypedMemberExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 44 void FlattenTuple::CollectArgs::visit( MemberExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 45 void FlattenTuple::CollectArgs::visit( VariableExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 46 void FlattenTuple::CollectArgs::visit( ConstantExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 47 void FlattenTuple::CollectArgs::visit( SizeofExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 48 void FlattenTuple::CollectArgs::visit( AttrExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 49 void FlattenTuple::CollectArgs::visit( LogicalExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 50 void FlattenTuple::CollectArgs::visit( ConditionalExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 51 void FlattenTuple::CollectArgs::visit( CommaExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 52 void FlattenTuple::CollectArgs::visit( TypeExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 53 void FlattenTuple::CollectArgs::visit( UntypedValofExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 25 54 26 return tupleExpr; 27 } 55 void FlattenTuple::CollectArgs::visit( TupleExpr *tupleExpr) { 56 acceptAll( tupleExpr->get_exprs(), *this ); 57 //currentArgs.splice( currentArgs.end(), c.get_args() ); 58 } 59 } // namespace Tuples 28 60 29 void FlattenTuple::CollectArgs::visit( UntypedExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 30 void FlattenTuple::CollectArgs::visit( NameExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 31 void FlattenTuple::CollectArgs::visit( CastExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 32 void FlattenTuple::CollectArgs::visit( AddressExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 33 void FlattenTuple::CollectArgs::visit( UntypedMemberExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 34 void FlattenTuple::CollectArgs::visit( MemberExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 35 void FlattenTuple::CollectArgs::visit( VariableExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 36 void FlattenTuple::CollectArgs::visit( ConstantExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 37 void FlattenTuple::CollectArgs::visit( SizeofExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 38 void FlattenTuple::CollectArgs::visit( AttrExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 39 void FlattenTuple::CollectArgs::visit( LogicalExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 40 void FlattenTuple::CollectArgs::visit( ConditionalExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 41 void FlattenTuple::CollectArgs::visit( CommaExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 42 void FlattenTuple::CollectArgs::visit( TypeExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 43 void FlattenTuple::CollectArgs::visit( UntypedValofExpr *expr ) { currentArgs.insert( currentArgs.end(), expr ); } 44 45 void FlattenTuple::CollectArgs::visit( TupleExpr *tupleExpr) 46 { 47 acceptAll( tupleExpr->get_exprs(), *this ); 48 49 //currentArgs.splice( currentArgs.end(), c.get_args() ); 50 } 51 } // namespace Tuples 61 // Local Variables: // 62 // tab-width: 4 // 63 // mode: c++ // 64 // compile-command: "make install" // 65 // End: // -
translator/Tuples/FlattenTuple.h
r0dd3a2f r51587aa 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 // FlattenTuple.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 11:28:15 2015 13 // Update Count : 3 14 // 15 1 16 #ifndef _FLATTEN_TUPLE_H_ 2 17 #define _FLATTEN_TUPLE_H_ … … 8 23 9 24 namespace Tuples { 10 class FlattenTuple : public Mutator 11 { 12 typedef Mutator Parent; 25 class FlattenTuple : public Mutator { 26 typedef Mutator Parent; 27 public: 28 FlattenTuple(); 29 ~FlattenTuple(); 13 30 14 public: 15 FlattenTuple(); 16 ~FlattenTuple(); 31 virtual Expression *mutate( TupleExpr *tupleExpr ); 32 private: 33 class CollectArgs : public Visitor { 34 public: 35 virtual void visit( UntypedExpr * ); 36 virtual void visit( NameExpr * ); 37 virtual void visit( CastExpr *); 38 virtual void visit( AddressExpr * ); 39 virtual void visit( UntypedMemberExpr * ); 40 virtual void visit( MemberExpr * ); 41 virtual void visit( VariableExpr * ); 42 virtual void visit( ConstantExpr * ); 43 virtual void visit( SizeofExpr * ); 44 virtual void visit( AttrExpr * ); 45 virtual void visit( LogicalExpr * ); 46 virtual void visit( ConditionalExpr * ); 47 virtual void visit( CommaExpr * ); 48 virtual void visit( TypeExpr * ); 49 virtual void visit( UntypedValofExpr * ); 17 50 18 virtual Expression *mutate( TupleExpr *tupleExpr );51 virtual void visit( TupleExpr *tupleExpr ); 19 52 20 private: 21 class CollectArgs : public Visitor 22 { 23 public: 24 virtual void visit( UntypedExpr * ); 25 virtual void visit( NameExpr * ); 26 virtual void visit( CastExpr *); 27 virtual void visit( AddressExpr * ); 28 virtual void visit( UntypedMemberExpr * ); 29 virtual void visit( MemberExpr * ); 30 virtual void visit( VariableExpr * ); 31 virtual void visit( ConstantExpr * ); 32 virtual void visit( SizeofExpr * ); 33 virtual void visit( AttrExpr * ); 34 virtual void visit( LogicalExpr * ); 35 virtual void visit( ConditionalExpr * ); 36 virtual void visit( CommaExpr * ); 37 virtual void visit( TypeExpr * ); 38 virtual void visit( UntypedValofExpr * ); 53 std::list< Expression *> &get_args() { return currentArgs; } 54 private: 55 std::list< Expression * > currentArgs; 56 }; 39 57 40 virtual void visit( TupleExpr *tupleExpr ); 41 42 std::list< Expression *> &get_args() { return currentArgs; } 43 private: 44 std::list< Expression * > currentArgs; 45 }; 46 47 }; 48 58 }; 49 59 } // namespace Tuples 50 60 61 #endif // _FLATTEN_TUPLE_H_ 51 62 52 #endif // #ifndef _FLATTEN_TUPLE_H_ 53 54 /* 55 Local Variables: 56 mode: c++ 57 End: 58 */ 63 // Local Variables: // 64 // tab-width: 4 // 65 // mode: c++ // 66 // compile-command: "make install" // 67 // End: // -
translator/Tuples/FunctionChecker.cc
r0dd3a2f r51587aa 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 // FunctionChecker.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 11:59:55 2015 13 // Update Count : 3 14 // 15 1 16 #include "FunctionChecker.h" 2 17 #include "FunctionFixer.h" … … 8 23 9 24 namespace Tuples { 10 using namespace std;25 using namespace std; 11 26 12 void checkFunctions( std::list< Declaration * > translationUnit ) 13 { 14 FunctionChecker fchk(true); 15 TupleDistrib td; 16 FunctionFixer ff; 27 void checkFunctions( std::list< Declaration * > translationUnit ) { 28 FunctionChecker fchk( true ); 29 TupleDistrib td; 30 FunctionFixer ff; 17 31 18 mutateAll( translationUnit , fchk ); 19 mutateAll( translationUnit , ff ); 20 mutateAll( translationUnit , td ); 21 return; 22 } 23 24 FunctionChecker::FunctionChecker( bool _topLevel, UniqueName *_nameGen ) : topLevel( _topLevel ), nameGen( _nameGen ) { 25 if ( topLevel) { 26 assert( ! nameGen ); 27 nameGen = new UniqueName("_MVR_"); 28 } else 29 assert( nameGen ); 30 } 31 32 FunctionChecker::~FunctionChecker() { 33 if ( topLevel) { 34 delete nameGen; 35 nameGen = 0; 36 } 37 } 38 39 Statement* FunctionChecker::mutate(ExprStmt *exprStmt) { 40 exprStmt->set_expr( maybeMutate( exprStmt->get_expr(), *this ) ); 41 if ( ! tempExpr.empty() ) { 42 assert ( ! temporaries.empty() ); 43 CompoundStmt *newBlock = new CompoundStmt( std::list< Label >() ); 44 // declarations 45 for ( std::list< ObjectDecl *>::iterator d = temporaries.begin(); d != temporaries.end(); ++d ) 46 newBlock->get_kids().push_back( new DeclStmt( std::list<Label>(), *d ) ); 47 // new expression statements 48 for ( std::list< Expression *>::iterator e = tempExpr.begin(); e != tempExpr.end(); ++e ) 49 newBlock->get_kids().push_back( new ExprStmt( std::list<Label>(), *e ) ); 50 51 newBlock->get_kids().push_back( exprStmt ); 52 return newBlock; 53 } else 54 return exprStmt; 55 } 56 57 Expression* FunctionChecker::mutate(ApplicationExpr *applicationExpr) { 58 if ( topLevel ) 59 ; // In top level of Functionchecker 60 61 if ( applicationExpr->get_results().size() > 1 ) { 62 for ( std::list< Type *>::iterator res = applicationExpr->get_results().begin(); res != applicationExpr->get_results().end(); res++ ) 63 temporaries.push_back( new ObjectDecl(nameGen->newName(),Declaration::Auto,LinkageSpec::AutoGen, 0, (*res)->clone(), 0 ) ); 64 65 assert( ! temporaries.empty() ); 66 } 67 68 applicationExpr->set_function( maybeMutate( applicationExpr->get_function(), *this ) ); 69 70 std::list< Expression * > newArgs; 71 for ( std::list< Expression *>::iterator e = applicationExpr->get_args().begin(); e != applicationExpr->get_args().end(); ++e ) { 72 FunctionChecker rec( false, nameGen ); 73 (*e)->acceptMutator( rec ); 74 75 if ( ! rec.temporaries.empty() ) { 76 TupleExpr *lhs = new TupleExpr; 77 std::list< Expression * > &tmem = lhs->get_exprs(); 78 for ( std::list<ObjectDecl *>::iterator d = rec.temporaries.begin(); d != rec.temporaries.end(); ++d ) { 79 tmem.push_back( new VariableExpr( *d ) ); 80 newArgs.push_back( new VariableExpr( *d ) ); 32 mutateAll( translationUnit , fchk ); 33 mutateAll( translationUnit , ff ); 34 mutateAll( translationUnit , td ); 35 return; 81 36 } 82 37 83 // construct tuple assignment 84 std::list<Expression *> args; 85 args.push_back( new AddressExpr(lhs) ); 86 args.push_back( *e ); 87 tempExpr.push_back( new UntypedExpr( new NameExpr("?=?"), args ) ); 38 FunctionChecker::FunctionChecker( bool _topLevel, UniqueName *_nameGen ) : topLevel( _topLevel ), nameGen( _nameGen ) { 39 if ( topLevel ) { 40 assert( ! nameGen ); 41 nameGen = new UniqueName("_MVR_"); 42 } else 43 assert( nameGen ); 44 } 88 45 89 temporaries.splice( temporaries.end(), rec.temporaries ); 90 } else 91 newArgs.push_back( *e ); 92 // percolate to recursive calls 93 } 46 FunctionChecker::~FunctionChecker() { 47 if ( topLevel ) { 48 delete nameGen; 49 nameGen = 0; 50 } 51 } 94 52 95 applicationExpr->get_args().clear(); 96 std::copy( newArgs.begin(), newArgs.end(), back_inserter(applicationExpr->get_args()) ); 53 Statement* FunctionChecker::mutate( ExprStmt *exprStmt ) { 54 exprStmt->set_expr( maybeMutate( exprStmt->get_expr(), *this ) ); 55 if ( ! tempExpr.empty() ) { 56 assert ( ! temporaries.empty() ); 57 CompoundStmt *newBlock = new CompoundStmt( std::list< Label >() ); 58 // declarations 59 for ( std::list< ObjectDecl *>::iterator d = temporaries.begin(); d != temporaries.end(); ++d ) 60 newBlock->get_kids().push_back( new DeclStmt( std::list<Label>(), *d ) ); 61 // new expression statements 62 for ( std::list< Expression *>::iterator e = tempExpr.begin(); e != tempExpr.end(); ++e ) 63 newBlock->get_kids().push_back( new ExprStmt( std::list<Label>(), *e ) ); 97 64 98 return applicationExpr; 99 } 65 newBlock->get_kids().push_back( exprStmt ); 66 return newBlock; 67 } else 68 return exprStmt; 69 } 100 70 101 Expression* TupleDistrib::mutate(UntypedExpr *expr) { 102 if ( NameExpr *assgnop = dynamic_cast< NameExpr * >(expr->get_function()) ) { 103 if ( assgnop->get_name() == std::string("?=?") ) { 104 std::list<Expression *> &args = expr->get_args(); 105 assert(args.size() == 2); 106 //if args.front() points to a tuple and if args.back() is already resolved 107 if ( AddressExpr *addr = dynamic_cast<AddressExpr *>(args.front()) ) 108 if ( TupleExpr *lhs = dynamic_cast<TupleExpr *>(addr->get_arg()) ) 109 if ( ApplicationExpr *rhs = dynamic_cast<ApplicationExpr *>( args.back() ) ) { 110 for ( std::list<Expression *>::iterator tc = lhs->get_exprs().begin(); tc != lhs->get_exprs().end(); ++tc ) 111 rhs->get_args().push_back( new AddressExpr( *tc ) ); 112 return rhs; // XXX 113 } 114 } else 115 assert(false); // It's not an assignment, shouldn't be here 116 } 117 return expr; 118 } 71 Expression* FunctionChecker::mutate( ApplicationExpr *applicationExpr ) { 72 if ( topLevel ) 73 ; // In top level of Functionchecker 119 74 120 } // namespace Tuples 75 if ( applicationExpr->get_results().size() > 1 ) { 76 for ( std::list< Type *>::iterator res = applicationExpr->get_results().begin(); res != applicationExpr->get_results().end(); res++ ) 77 temporaries.push_back( new ObjectDecl( nameGen->newName(),Declaration::Auto,LinkageSpec::AutoGen, 0, (*res )->clone(), 0 ) ); 78 79 assert( ! temporaries.empty() ); 80 } 81 82 applicationExpr->set_function( maybeMutate( applicationExpr->get_function(), *this ) ); 83 84 std::list< Expression * > newArgs; 85 for ( std::list< Expression *>::iterator e = applicationExpr->get_args().begin(); e != applicationExpr->get_args().end(); ++e ) { 86 FunctionChecker rec( false, nameGen ); 87 (*e )->acceptMutator( rec ); 88 89 if ( ! rec.temporaries.empty() ) { 90 TupleExpr *lhs = new TupleExpr; 91 std::list< Expression * > &tmem = lhs->get_exprs(); 92 for ( std::list<ObjectDecl *>::iterator d = rec.temporaries.begin(); d != rec.temporaries.end(); ++d ) { 93 tmem.push_back( new VariableExpr( *d ) ); 94 newArgs.push_back( new VariableExpr( *d ) ); 95 } 96 97 // construct tuple assignment 98 std::list<Expression *> args; 99 args.push_back( new AddressExpr( lhs ) ); 100 args.push_back( *e ); 101 tempExpr.push_back( new UntypedExpr( new NameExpr("?=?"), args ) ); 102 103 temporaries.splice( temporaries.end(), rec.temporaries ); 104 } else 105 newArgs.push_back( *e ); 106 // percolate to recursive calls 107 } 108 109 applicationExpr->get_args().clear(); 110 std::copy( newArgs.begin(), newArgs.end(), back_inserter( applicationExpr->get_args()) ); 111 112 return applicationExpr; 113 } 114 115 Expression* TupleDistrib::mutate( UntypedExpr *expr ) { 116 if ( NameExpr *assgnop = dynamic_cast< NameExpr * >( expr->get_function()) ) { 117 if ( assgnop->get_name() == std::string("?=?") ) { 118 std::list<Expression *> &args = expr->get_args(); 119 assert( args.size() == 2 ); 120 //if args.front() points to a tuple and if args.back() is already resolved 121 if ( AddressExpr *addr = dynamic_cast<AddressExpr *>( args.front()) ) 122 if ( TupleExpr *lhs = dynamic_cast<TupleExpr *>( addr->get_arg()) ) 123 if ( ApplicationExpr *rhs = dynamic_cast<ApplicationExpr *>( args.back() ) ) { 124 for ( std::list<Expression *>::iterator tc = lhs->get_exprs().begin(); tc != lhs->get_exprs().end(); ++tc ) 125 rhs->get_args().push_back( new AddressExpr( *tc ) ); 126 return rhs; // XXX 127 } // if 128 } else 129 assert( false ); // It's not an assignment, shouldn't be here 130 } // if 131 return expr; 132 } 133 134 } 135 136 // namespace Tuples 137 // Local Variables: // 138 // tab-width: 4 // 139 // mode: c++ // 140 // compile-command: "make install" // 141 // End: // -
translator/Tuples/FunctionChecker.h
r0dd3a2f r51587aa 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 // FunctionChecker.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 12:00:57 2015 13 // Update Count : 2 14 // 15 1 16 #ifndef _TUPLE_FUNCHK_H_ 2 17 #define _TUPLE_FUNCHK_H_ … … 16 31 #include "SynTree/Statement.h" 17 32 33 namespace Tuples { 34 class FunctionChecker : public Mutator { 35 typedef Mutator Parent; 36 public: 37 FunctionChecker( bool _topLevel = false, UniqueName *_nameGen = 0 ); 38 ~FunctionChecker(); 18 39 19 namespace Tuples { 20 class FunctionChecker : public Mutator 21 { 22 typedef Mutator Parent; 40 virtual Expression* mutate(ApplicationExpr *applicationExpr); 41 virtual Statement* mutate(ExprStmt *exprStmt); 42 private: 43 bool topLevel; 44 UniqueName *nameGen; 45 std::list< ObjectDecl * > temporaries; 46 std::list< Expression * > tempExpr; 47 }; 23 48 24 public: 25 FunctionChecker( bool _topLevel = false, UniqueName *_nameGen = 0 ); 26 ~FunctionChecker(); 49 class TupleDistrib : public Mutator { 50 public: 51 virtual Expression* mutate(UntypedExpr *utExpr); 52 private: 53 }; 27 54 28 virtual Expression* mutate(ApplicationExpr *applicationExpr); 29 virtual Statement* mutate(ExprStmt *exprStmt); 30 31 private: 32 bool topLevel; 33 UniqueName *nameGen; 34 std::list< ObjectDecl * > temporaries; 35 std::list< Expression * > tempExpr; 36 }; 37 38 class TupleDistrib : public Mutator { 39 public: 40 virtual Expression* mutate(UntypedExpr *utExpr); 41 42 private: 43 }; 44 45 void checkFunctions( std::list< Declaration * > translationUnit ); 46 55 void checkFunctions( std::list< Declaration * > translationUnit ); 47 56 } // namespace Tuples 48 57 49 #endif // #ifndef_TUPLE_FUNCHK_H_58 #endif // _TUPLE_FUNCHK_H_ 50 59 51 / *52 Local Variables: 53 mode: c++ 54 End: 55 */60 // Local Variables: // 61 // tab-width: 4 // 62 // mode: c++ // 63 // compile-command: "make install" // 64 // End: // -
translator/Tuples/FunctionFixer.cc
r0dd3a2f r51587aa 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 // FunctionFixer.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 12:02:22 2015 13 // Update Count : 1 14 // 15 1 16 #include <list> 2 17 #include <vector> … … 7 22 8 23 namespace Tuples { 9 DeclarationWithType *FunctionFixer::mutate( FunctionDecl *functionDecl ) {10 functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) );11 mutateAll( functionDecl->get_oldDecls(), *this );12 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );13 index.visit( functionDecl );14 rets.clear();15 return functionDecl;16 }24 DeclarationWithType *FunctionFixer::mutate( FunctionDecl *functionDecl ) { 25 functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) ); 26 mutateAll( functionDecl->get_oldDecls(), *this ); 27 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); 28 index.visit( functionDecl ); 29 rets.clear(); 30 return functionDecl; 31 } 17 32 18 Type *FunctionFixer::mutate( FunctionType *functionType ) 19 { 20 typedef std::list< DeclarationWithType * > Decls; 33 Type *FunctionFixer::mutate( FunctionType *functionType ) { 34 typedef std::list< DeclarationWithType * > Decls; 21 35 22 if ( functionType->get_returnVals().size() <= 1 ) return functionType;23 std::copy( functionType->get_returnVals().begin(), functionType->get_returnVals().end(), back_inserter(rets) );36 if ( functionType->get_returnVals().size() <= 1 ) return functionType; 37 std::copy( functionType->get_returnVals().begin(), functionType->get_returnVals().end(), back_inserter(rets) ); 24 38 25 Type::Qualifiers qual;26 for ( Decls::iterator i = rets.begin(); i != rets.end(); i++ ) {27 (*i)->set_type( new PointerType( qual, (*i)->get_type() ) );28 functionType->get_parameters().push_back( *i );29 } 39 Type::Qualifiers qual; 40 for ( Decls::iterator i = rets.begin(); i != rets.end(); i++ ) { 41 (*i)->set_type( new PointerType( qual, (*i)->get_type() ) ); 42 functionType->get_parameters().push_back( *i ); 43 } // for 30 44 31 functionType->get_returnVals() = *(new std::list< DeclarationWithType * >());45 functionType->get_returnVals() = *(new std::list< DeclarationWithType * >()); 32 46 33 functionType->set_isVarArgs( false );34 return functionType;35 }47 functionType->set_isVarArgs( false ); 48 return functionType; 49 } 36 50 37 Statement *FunctionFixer::mutate( ReturnStmt *retStmt ) 38 { 39 bool tupleReturn = false; 40 Expression *rhs = 0; 41 // also check if returning multiple values 42 if ( CastExpr *cst = dynamic_cast<CastExpr *>( retStmt->get_expr() ) ) { 43 if ( ApplicationExpr *app = dynamic_cast<ApplicationExpr *>( cst->get_arg() ) ) { 44 if ( app->get_results().size() > 1 ) { // doesn't need to be ApplicationExpr 45 tupleReturn = true; 46 rhs = app; 51 Statement *FunctionFixer::mutate( ReturnStmt *retStmt ) { 52 bool tupleReturn = false; 53 Expression *rhs = 0; 54 // also check if returning multiple values 55 if ( CastExpr *cst = dynamic_cast<CastExpr *>( retStmt->get_expr() ) ) { 56 if ( ApplicationExpr *app = dynamic_cast<ApplicationExpr *>( cst->get_arg() ) ) { 57 if ( app->get_results().size() > 1 ) { // doesn't need to be ApplicationExpr 58 tupleReturn = true; 59 rhs = app; 60 } 61 } else if ( TupleExpr *t = dynamic_cast<TupleExpr *>( cst->get_arg() ) ) { 62 tupleReturn = true; 63 assert( rets.size() == t->get_exprs().size() ); // stupid check, resolve expression 64 rhs = t; 65 } // if 66 67 if ( tupleReturn ) { 68 assert ( rhs != 0 ); 69 std::list< Expression * > lhs; 70 for ( std::list< DeclarationWithType * >::iterator d = rets.begin(); d != rets.end(); ++d ) { 71 std::list<Expression *> largs; 72 largs.push_back(new VariableExpr( *d )); 73 Expression *exp = ResolvExpr::resolveInVoidContext( new CastExpr( new UntypedExpr(new NameExpr("*?"), largs), (*d)->get_type()), 74 index ); 75 lhs.push_back(exp); 76 } // for 77 std::list< Expression * > args; 78 TupleExpr *tlhs = new TupleExpr; tlhs->set_exprs( lhs ); 79 args.push_back( new AddressExpr(tlhs) ); 80 args.push_back(rhs); 81 82 return new ExprStmt( std::list< Label>(), new UntypedExpr( new NameExpr("?=?"), args ) ); 83 } // if 84 } // if 85 /* 86 else 87 std::cerr << "Empty return statement" << std::endl; 88 */ 89 90 return retStmt; 47 91 } 48 } else if ( TupleExpr *t = dynamic_cast<TupleExpr *>( cst->get_arg() ) ) {49 tupleReturn = true;50 assert( rets.size() == t->get_exprs().size() ); // stupid check, resolve expression51 rhs = t;52 }53 92 54 if ( tupleReturn ) { 55 assert ( rhs != 0 ); 56 std::list< Expression * > lhs; 57 for ( std::list< DeclarationWithType * >::iterator d = rets.begin(); d != rets.end(); ++d ) { 58 std::list<Expression *> largs; 59 largs.push_back(new VariableExpr( *d )); 60 Expression *exp = ResolvExpr::resolveInVoidContext( new CastExpr( new UntypedExpr(new NameExpr("*?"), largs), (*d)->get_type()), 61 index ); 62 lhs.push_back(exp); 93 Expression* FunctionFixer::mutate( VariableExpr *variableExpr ) { 94 if ( rets.empty() ) return variableExpr; 95 mutateAll( variableExpr->get_results(), *this ); 96 if ( std::find( rets.begin(), rets.end(), variableExpr->get_var() ) != rets.end() ) 97 // if ( PointerType *ptr = dynamic_cast<PointerType *>(variableExpr->get_var()->get_type()) ) { 98 if ( dynamic_cast<PointerType *>(variableExpr->get_var()->get_type()) != 0 ) { 99 std::list<Expression *> largs; 100 largs.push_back( new AddressExpr(variableExpr) ); 101 Expression *expr = ResolvExpr::resolveInVoidContext( /*new CastExpr(*/new UntypedExpr( new NameExpr( "*?" ), largs )/*, 102 ptr->get_base()),*/, index); 103 if ( ApplicationExpr *app = dynamic_cast< ApplicationExpr * >( expr ) ) { 104 assert( app->get_args().size() == 1 ); 105 app->get_args().pop_front(); 106 app->get_args().push_back( variableExpr ); 107 return app; 108 } 109 } 110 return variableExpr; 63 111 } 64 std::list< Expression * > args;65 TupleExpr *tlhs = new TupleExpr; tlhs->set_exprs( lhs );66 args.push_back( new AddressExpr(tlhs) );67 args.push_back(rhs);68 112 69 return new ExprStmt( std::list< Label>(), new UntypedExpr( new NameExpr("?=?"), args ) ); 70 } 71 } 72 /* 73 else 74 std::cerr << "Empty return statement" << std::endl; 75 */ 76 77 return retStmt; 78 } 79 80 Expression* FunctionFixer::mutate( VariableExpr *variableExpr ) { 81 if ( rets.empty() ) return variableExpr; 82 mutateAll( variableExpr->get_results(), *this ); 83 if ( std::find( rets.begin(), rets.end(), variableExpr->get_var() ) != rets.end() ) 84 // if ( PointerType *ptr = dynamic_cast<PointerType *>(variableExpr->get_var()->get_type()) ) { 85 if ( dynamic_cast<PointerType *>(variableExpr->get_var()->get_type()) != 0 ) { 86 std::list<Expression *> largs; 87 largs.push_back( new AddressExpr(variableExpr) ); 88 Expression *expr = ResolvExpr::resolveInVoidContext( /*new CastExpr(*/new UntypedExpr( new NameExpr( "*?" ), largs )/*, 89 ptr->get_base()),*/, index); 90 if ( ApplicationExpr *app = dynamic_cast< ApplicationExpr * >( expr ) ) { 91 assert( app->get_args().size() == 1 ); 92 app->get_args().pop_front(); 93 app->get_args().push_back( variableExpr ); 94 return app; 113 /* 114 Expression* FunctionFixer::mutate(ApplicationExpr *applicationExpr) { 115 std::cerr << "In Application Expression" << std::endl; 116 mutateAll( applicationExpr->get_results(), *this ); 117 applicationExpr->set_function( maybeMutate( applicationExpr->get_function(), *this ) ); 118 mutateAll( applicationExpr->get_args(), *this ); 119 return applicationExpr; 95 120 } 96 } 97 return variableExpr; 98 } 99 100 /* 101 Expression* FunctionFixer::mutate(ApplicationExpr *applicationExpr) { 102 std::cerr << "In Application Expression" << std::endl; 103 mutateAll( applicationExpr->get_results(), *this ); 104 applicationExpr->set_function( maybeMutate( applicationExpr->get_function(), *this ) ); 105 mutateAll( applicationExpr->get_args(), *this ); 106 return applicationExpr; 107 } 108 */ 121 */ 109 122 } // namespace Tuples 123 // Local Variables: // 124 // tab-width: 4 // 125 // mode: c++ // 126 // compile-command: "make install" // 127 // End: // -
translator/Tuples/FunctionFixer.h
r0dd3a2f r51587aa 1 #ifndef _FUNFIX_H_ 2 #define _FUNFIX_H_ 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 // FunctionFixer.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 14:58:45 2015 13 // Update Count : 3 14 // 15 16 #ifndef _FUNCTIONFIXER_H_ 17 #define _FUNCTIONFIXER_H_ 3 18 4 19 #include "SynTree/Mutator.h" … … 14 29 15 30 namespace Tuples { 16 class FunctionFixer : public Mutator 17 { 18 typedef Mutator Parent; 31 class FunctionFixer : public Mutator { 32 typedef Mutator Parent; 33 public: 34 FunctionFixer() {} 35 virtual ~FunctionFixer() {} 36 virtual Type *mutate( FunctionType *functionType ); 37 virtual Statement *mutate( ReturnStmt *retStmt ); 38 virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); 39 virtual Expression *mutate( VariableExpr *variableExpr); 19 40 20 public: 21 FunctionFixer() {} 22 virtual ~FunctionFixer() {} 23 virtual Type *mutate( FunctionType *functionType ); 24 virtual Statement *mutate( ReturnStmt *retStmt ); 25 virtual DeclarationWithType *mutate( FunctionDecl *functionDecl ); 26 virtual Expression *mutate( VariableExpr *variableExpr); 41 // indexer runs 42 virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) 43 { index.visit( objectDecl ); return objectDecl; } 44 virtual TypeDecl *mutate( TypeDecl *typeDecl ) 45 { index.visit( typeDecl ); return typeDecl; } 46 virtual TypedefDecl *mutate( TypedefDecl *typeDecl ) 47 { index.visit( typeDecl ); return typeDecl; } 48 virtual StructDecl *mutate( StructDecl *aggregateDecl ) 49 { index.visit( aggregateDecl ); return aggregateDecl; } 50 virtual UnionDecl *mutate( UnionDecl *aggregateDecl ) 51 { index.visit( aggregateDecl ); return aggregateDecl; } 52 virtual EnumDecl *mutate( EnumDecl *aggregateDecl ) 53 { index.visit( aggregateDecl ); return aggregateDecl; } 27 54 28 // indexer runs 29 virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) 30 { index.visit( objectDecl ); return objectDecl; } 31 virtual TypeDecl *mutate( TypeDecl *typeDecl ) 32 { index.visit( typeDecl ); return typeDecl; } 33 virtual TypedefDecl *mutate( TypedefDecl *typeDecl ) 34 { index.visit( typeDecl ); return typeDecl; } 35 virtual StructDecl *mutate( StructDecl *aggregateDecl ) 36 { index.visit( aggregateDecl ); return aggregateDecl; } 37 virtual UnionDecl *mutate( UnionDecl *aggregateDecl ) 38 { index.visit( aggregateDecl ); return aggregateDecl; } 39 virtual EnumDecl *mutate( EnumDecl *aggregateDecl ) 40 { index.visit( aggregateDecl ); return aggregateDecl; } 41 42 virtual Type *mutate( StructInstType *aggrInst ) 43 { index.visit( aggrInst ); return aggrInst; } 44 virtual Type *mutate( UnionInstType *aggrInst ) 45 { index.visit( aggrInst ); return aggrInst; } 46 47 48 49 private: 50 std::list< DeclarationWithType * > rets; 51 SymTab::Indexer index; 52 }; 53 55 virtual Type *mutate( StructInstType *aggrInst ) 56 { index.visit( aggrInst ); return aggrInst; } 57 virtual Type *mutate( UnionInstType *aggrInst ) 58 { index.visit( aggrInst ); return aggrInst; } 59 private: 60 std::list< DeclarationWithType * > rets; 61 SymTab::Indexer index; 62 }; 54 63 } // namespace Tuples 55 64 65 #endif // _FUNCTIONFIXER_H_ 56 66 57 #endif // #ifndef _FUNFIX_H_ 58 59 /* 60 Local Variables: 61 mode: c++ 62 End: 63 */ 67 // Local Variables: // 68 // tab-width: 4 // 69 // mode: c++ // 70 // compile-command: "make install" // 71 // End: // -
translator/Tuples/MultRet.cc
r0dd3a2f r51587aa 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 // MultRet.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 12:37:57 2015 13 // Update Count : 1 14 // 15 1 16 #include <list> 2 17 #include <vector> … … 11 26 12 27 namespace Tuples { 28 MVRMutator::MVRMutator() : newVars( 0 ), newCode( 0 ) { 29 } 13 30 14 MVRMutator::MVRMutator() : newVars( 0 ), newCode( 0 ) 15 { 16 } 31 MVRMutator::~MVRMutator() { 32 } 17 33 18 MVRMutator::~MVRMutator() 19 { 20 } 34 int MVRMutator::curVal = 0; 21 35 22 int MVRMutator::curVal = 0; 36 Statement *MVRMutator::mutate( ExprStmt *exprStmt ) { 37 MVRMutator toplevel; 38 exprStmt->set_expr( maybeMutate( exprStmt->get_expr(), toplevel ) ); 23 39 24 Statement *MVRMutator::mutate( ExprStmt *exprStmt ){ 25 MVRMutator toplevel; 26 exprStmt->set_expr( maybeMutate( exprStmt->get_expr(), toplevel ) ); 40 if ( toplevel.hasCode() ) { 41 assert ( toplevel.getVars() != 0 ); 27 42 28 if ( toplevel.hasCode() ) { 29 assert ( toplevel.getVars() != 0 ); 43 typedef std::list<Statement *> Statements; 30 44 31 typedef std::list<Statement *> Statements;45 CompoundStmt *code = new CompoundStmt( std::list< Label >() ); 32 46 33 CompoundStmt *code = new CompoundStmt( std::list< Label >() ); 47 // copy variables 48 Statements &vars = toplevel.getVars()->get_kids(); 49 for ( Statements::iterator i = vars.begin(); i != vars.end(); i++ ) 50 code->get_kids().push_back( *i ); 34 51 35 // copy variables36 Statements &vars = toplevel.getVars()->get_kids();37 for ( Statements::iterator i = vars.begin(); i != vars.end(); i++ )38 code->get_kids().push_back( *i );52 // copy statements 53 Statements &block = toplevel.getCode()->get_kids(); 54 for ( Statements::iterator i = block.begin(); i != block.end(); i++ ) 55 code->get_kids().push_back( *i ); 39 56 40 // copy statements 41 Statements &block = toplevel.getCode()->get_kids(); 42 for ( Statements::iterator i = block.begin(); i != block.end(); i++ ) 43 code->get_kids().push_back( *i );57 return code; 58 } else 59 return exprStmt; 60 } 44 61 45 return code; 46 } else 47 return exprStmt; 48 } 62 Expression *MVRMutator::mutate( ApplicationExpr *appExpr ) { 63 // appExpr->set_function( maybeMutate( appExpr->get_function(), *this ) ); 64 bool mulretp = false; 65 VariableExpr *funname; 66 if ( (funname = dynamic_cast<VariableExpr *>(appExpr->get_function())) == 0 ) goto DoArgs; 49 67 68 FunctionDecl *fundecl; 69 if ((fundecl = dynamic_cast<FunctionDecl *>(funname->get_var())) == 0) goto DoArgs; 70 { 71 typedef std::list<DeclarationWithType*> RetType; 50 72 51 Expression *MVRMutator::mutate( ApplicationExpr *appExpr ) 52 { 53 // appExpr->set_function( maybeMutate( appExpr->get_function(), *this ) ); 54 bool mulretp = false; 55 VariableExpr *funname; 56 if ( (funname = dynamic_cast<VariableExpr *>(appExpr->get_function())) == 0 ) goto DoArgs; 73 RetType &rets = fundecl->get_functionType()->get_returnVals(); 74 if ( rets.size() <= 1 ) goto DoArgs; 75 mulretp = true; 57 76 58 FunctionDecl *fundecl; 59 if ((fundecl = dynamic_cast<FunctionDecl *>(funname->get_var())) == 0) goto DoArgs;77 if ( newVars == 0 ) 78 newVars = new CompoundStmt( std::list<Label>(0) ); 60 79 61 { 62 typedef std::list<DeclarationWithType*> RetType; 80 for (RetType::iterator i = rets.begin() ; i != rets.end(); i++) { 81 DeclStmt *arg = newVar( *i ); 82 newVars->get_kids().push_back( arg ); 83 add_pending( arg->get_decl() ); 84 } // for 85 } 63 86 64 RetType &rets = fundecl->get_functionType()->get_returnVals(); 65 if ( rets.size() <= 1 ) goto DoArgs; 66 mulretp = true; 87 DoArgs: 88 // mutate the argument list 89 typedef std::list< Expression *> Exprs; 90 Exprs &args = appExpr->get_args(); 91 std::list< Expression * > newArgs; 92 for ( Exprs::iterator i = args.begin(); i != args.end(); i++ ) { 93 MVRMutator next; 94 Expression *mutated = (*i)->acceptMutator( next ); 67 95 68 if ( newVars == 0 ) 69 newVars = new CompoundStmt( std::list<Label>(0) ); 96 if ( next.hasCode() ) { 97 // merge new vars and bodies 98 typedef std::list< Statement * > Stmts; 99 Stmts &vars = next.getVars()->get_kids(); 100 Stmts &block = next.getCode()->get_kids(); 70 101 71 for (RetType::iterator i = rets.begin() ; i != rets.end(); i++) { 72 DeclStmt *arg = newVar( *i ); 73 newVars->get_kids().push_back( arg ); 74 add_pending( arg->get_decl() ); 75 } 76 } 102 if (newVars == 0) 103 newVars = new CompoundStmt( std::list< Label >() ); 104 for ( Stmts::iterator i = vars.begin(); i != vars.end(); i++ ) // std::splice? -- need to append lists 105 newVars->get_kids().push_back( *i ); 77 106 78 DoArgs: 79 // mutate the argument list 80 typedef std::list< Expression *> Exprs; 81 Exprs &args = appExpr->get_args(); 82 std::list< Expression * > newArgs; 83 for ( Exprs::iterator i = args.begin(); i != args.end(); i++ ) { 84 MVRMutator next; 85 Expression *mutated = (*i)->acceptMutator( next ); 107 if (newCode == 0) 108 newCode = new CompoundStmt( std::list< Label >() ); 109 for ( Stmts::iterator i = block.begin(); i != block.end(); i++ ) 110 newCode->get_kids().push_back( *i ); 111 } // if 86 112 87 if ( next.hasCode() ) { 88 // merge new vars and bodies 89 typedef std::list< Statement * > Stmts; 90 Stmts &vars = next.getVars()->get_kids(); 91 Stmts &block = next.getCode()->get_kids(); 113 if ( next.hasResults() ) { 114 Exprs &res = next.get_results(); 115 for ( Exprs::iterator i = res.begin(); i != res.end(); i++ ) 116 newArgs.push_back( *i ); 117 } else 118 newArgs.push_back( mutated ); 119 } 92 120 93 if (newVars == 0) 94 newVars = new CompoundStmt( std::list< Label >() ); 95 for ( Stmts::iterator i = vars.begin(); i != vars.end(); i++ ) // std::splice? -- need to append lists 96 newVars->get_kids().push_back( *i ); 121 appExpr->get_args() = newArgs; // new argument list 97 122 98 if (newCode == 0) 99 newCode = new CompoundStmt( std::list< Label >() ); 100 for ( Stmts::iterator i = block.begin(); i != block.end(); i++ ) 101 newCode->get_kids().push_back( *i ); 123 if ( mulretp ) { 124 // add 'out' parameters 125 if ( ! argsToAdd.empty() ) 126 for (std::list< Expression *>::iterator i = argsToAdd.begin(); i != argsToAdd.end(); i++) 127 (appExpr->get_args()).push_back( *i ); 128 // clear 'out' parameters ( so that the list can be reused -- substitute by auto_ptr later? ) 102 129 103 } 130 if (newCode == 0) 131 newCode = new CompoundStmt( std::list<Label>(0) ); 132 } // if 104 133 105 if ( next.hasResults() ) { 106 Exprs &res = next.get_results(); 107 for ( Exprs::iterator i = res.begin(); i != res.end(); i++ ) 108 newArgs.push_back( *i ); 109 } else 110 newArgs.push_back( mutated ); 111 } 134 // add to block of code 135 if ( newCode != 0 ) 136 newCode->get_kids().push_back( new ExprStmt( std::list<Label>(), appExpr ) ); 112 137 113 appExpr->get_args() = newArgs; // new argument list 138 return appExpr; 139 } 114 140 141 // Auxiliary function to generate new names for the `output' parameters 142 DeclStmt *MVRMutator::newVar( DeclarationWithType *reqDecl ) { 143 // std::ostrstream os; 144 // os << "__" << curVal++ << "__";// << std::ends; 145 // os.freeze( false ); 115 146 116 if ( mulretp ) { 117 // add 'out' parameters 118 if ( ! argsToAdd.empty() ) 119 for (std::list< Expression *>::iterator i = argsToAdd.begin(); i != argsToAdd.end(); i++) 120 (appExpr->get_args()).push_back( *i ); 121 // clear 'out' parameters ( so that the list can be reused -- substitute by auto_ptr later? ) 147 ObjectDecl *decl; 148 if ((decl = dynamic_cast<ObjectDecl *>( reqDecl )) != 0) 149 // return new DeclStmt( new ObjectDecl( std::string (os.str(), os.pcount()), ); 150 return new DeclStmt( std::list<Label>(), decl ); 151 else 152 return 0; 153 } 122 154 123 if (newCode == 0) 124 newCode = new CompoundStmt( std::list<Label>(0) );125 } 155 void MVRMutator::add_pending( Declaration *decl ) { 156 ObjectDecl *obj; 157 if ( (obj = dynamic_cast< ObjectDecl * >( decl )) == 0 ) return; 126 158 127 // add to block of code 128 if ( newCode != 0 ) 129 newCode->get_kids().push_back( new ExprStmt( std::list<Label>(), appExpr ) ); 159 VariableExpr *var = new VariableExpr(obj, 0 ); 160 results.push_back( var ); // probably change this name to newResults or something 161 argsToAdd.push_back( new AddressExpr( var ) ); 162 return; 163 } 164 } 130 165 131 return appExpr; 132 } 133 134 // Auxiliary function to generate new names for the `output' parameters 135 DeclStmt *MVRMutator::newVar( DeclarationWithType *reqDecl ) { 136 // std::ostrstream os; 137 // os << "__" << curVal++ << "__";// << std::ends; 138 // os.freeze( false ); 139 140 ObjectDecl *decl; 141 if ((decl = dynamic_cast<ObjectDecl *>( reqDecl )) != 0) 142 // return new DeclStmt( new ObjectDecl( std::string (os.str(), os.pcount()), ); 143 return new DeclStmt( std::list<Label>(), decl ); 144 else 145 return 0; 146 } 147 148 void MVRMutator::add_pending( Declaration *decl ) { 149 ObjectDecl *obj; 150 if ( (obj = dynamic_cast< ObjectDecl * >( decl )) == 0 ) return; 151 152 VariableExpr *var = new VariableExpr(obj, 0 ); 153 results.push_back( var ); // probably change this name to newResults or something 154 argsToAdd.push_back( new AddressExpr( var ) ); 155 return; 156 } 157 } 166 // Local Variables: // 167 // tab-width: 4 // 168 // mode: c++ // 169 // compile-command: "make install" // 170 // End: // -
translator/Tuples/MultRet.h
r0dd3a2f r51587aa 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 // MultRet.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 12:39:20 2015 13 // Update Count : 3 14 // 15 1 16 #ifndef _MULTRET_H_ 2 17 #define _MULTRET_H_ … … 9 24 10 25 namespace Tuples { 11 class MVRMutator : public Mutator 12 { 13 typedef Mutator Parent; 26 class MVRMutator : public Mutator { 27 typedef Mutator Parent; 28 public: 29 MVRMutator(); 30 ~MVRMutator(); 14 31 15 public: 16 MVRMutator(); 17 ~MVRMutator(); 32 virtual Statement *mutate( ExprStmt *exprStmt ); 33 virtual Expression *mutate( ApplicationExpr *appExpr ); 18 34 19 virtual Statement *mutate( ExprStmt *exprStmt ); 20 virtual Expression *mutate( ApplicationExpr *appExpr ); 35 bool hasCode() const { return ( newCode != 0 ); } 36 CompoundStmt *getCode() const { return newCode; } 37 CompoundStmt *getVars() const { return newVars; } 21 38 22 bool hasCode() const { return ( newCode != 0 ); } 23 CompoundStmt *getCode() const { return newCode; } 24 CompoundStmt *getVars() const { return newVars; } 39 bool hasResults() const { return (! results.empty()); } 40 std::list<Expression *> &get_results() { return results; } 41 private: 42 CompoundStmt *newVars; 43 CompoundStmt *newCode; 44 std::list<Expression *> argsToAdd; 45 std::list<Expression *> results; 25 46 26 bool hasResults() const { return (! results.empty()); } 27 std::list<Expression *> &get_results() { return results; } 28 29 private: 30 CompoundStmt *newVars; 31 CompoundStmt *newCode; 32 std::list<Expression *> argsToAdd; 33 std::list<Expression *> results; 34 35 static int curVal; 36 DeclStmt *newVar( DeclarationWithType * ); 37 void add_pending( Declaration * ); 38 }; 39 47 static int curVal; 48 DeclStmt *newVar( DeclarationWithType * ); 49 void add_pending( Declaration * ); 50 }; 40 51 } // namespace Tuples 41 52 53 #endif // _MULTRET_H_ 42 54 43 #endif // #ifndef _MULTRET_H_ 44 45 /* 46 Local Variables: 47 mode: c++ 48 End: 49 */ 55 // Local Variables: // 56 // tab-width: 4 // 57 // mode: c++ // 58 // compile-command: "make install" // 59 // End: // -
translator/Tuples/Mutate.cc
r0dd3a2f r51587aa 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 // Mutate.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 14:57:10 2015 13 // Update Count : 2 14 // 15 1 16 #include "SynTree/Mutator.h" 2 17 … … 11 26 12 27 namespace Tuples { 28 void mutate( std::list< Declaration * > translationUnit ) { 29 //FunctionFixer fst; 30 AssignExpander snd; 13 31 14 void mutate( std::list< Declaration * > translationUnit ) 15 { 16 //FunctionFixer fst; 17 AssignExpander snd; 18 19 //mutateAll( translationUnit, fst ); 20 mutateAll( translationUnit, snd ); 21 } 32 //mutateAll( translationUnit, fst ); 33 mutateAll( translationUnit, snd ); 34 } 22 35 } // namespace Tuples 23 36 24 37 // Local Variables: // 38 // tab-width: 4 // 39 // mode: c++ // 40 // compile-command: "make install" // 41 // End: // -
translator/Tuples/Mutate.h
r0dd3a2f r51587aa 1 #ifndef TUPLES_MUTATE_H 2 #define TUPLES_MUTATE_H 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 // Mutate.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 14:58:05 2015 13 // Update Count : 2 14 // 15 16 #ifndef MUTATE_H 17 #define MUTATE_H 3 18 4 19 #include <list> … … 7 22 #include "SynTree/Declaration.h" 8 23 9 10 24 namespace Tuples { 11 12 void mutate( std::list< Declaration* > translationUnit ); 13 void mulReturn( std::list< Declaration * > translationUnit ); 14 25 void mutate( std::list< Declaration* > translationUnit ); 26 void mulReturn( std::list< Declaration * > translationUnit ); 15 27 } // namespace Tuples 16 28 17 #endif // #ifndef TUPLES_MUTATE_H29 #endif // MUTATE_H 18 30 19 / *20 Local Variables: 21 mode: c++ 22 End: 23 */31 // Local Variables: // 32 // tab-width: 4 // 33 // mode: c++ // 34 // compile-command: "make install" // 35 // End: // -
translator/Tuples/NameMatcher.cc
r0dd3a2f r51587aa 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 // NameMatcher.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 15:00:06 2015 13 // Update Count : 1 14 // 15 1 16 #include "NameMatcher.h" 2 17 #include "NameMatcher.h" 3 18 4 19 namespace Tuples { 5 NameMatcher::NameMatcher( std::list< DeclarationWithType* > &formals ) 6 : current( 0 ) { 7 int cnt = 0; 8 for ( std::list< DeclarationWithType *>::const_iterator f = formals.begin(); f != formals.end(); ++f ) { 9 table.insert( std::pair< std::string, int >( (*f)->get_name(), cnt++ ) ); 10 index.push_back(*f); 11 } 12 exprs.reserve( index.size() ); 13 } 20 NameMatcher::NameMatcher( std::list< DeclarationWithType* > &formals ) : current( 0 ) { 21 int cnt = 0; 22 for ( std::list< DeclarationWithType *>::const_iterator f = formals.begin(); f != formals.end(); ++f ) { 23 table.insert( std::pair< std::string, int >( (*f)->get_name(), cnt++ ) ); 24 index.push_back(*f); 25 } // for 26 exprs.reserve( index.size() ); 27 } 14 28 15 NameMatcher::~NameMatcher() {}29 NameMatcher::~NameMatcher() {} 16 30 17 void NameMatcher::match( ResolvExpr::AltList &alternatives ) throw (NoMatch) {18 if ( alternatives.size() != index.size() )19 throw NoMatch("Length of actuals and formals differ");31 void NameMatcher::match( ResolvExpr::AltList &alternatives ) throw (NoMatch) { 32 if ( alternatives.size() != index.size() ) 33 throw NoMatch("Length of actuals and formals differ"); 20 34 21 for ( ResolvExpr::AltList::const_iterator a = alternatives.begin(); a != alternatives.end(); ++a ) {22 if ( a->expr->get_argName() != 0 )23 if ( NameExpr *name = dynamic_cast<NameExpr *>( a->expr->get_argName() ) ) {24 if ( table.find( name->get_name() ) != table.end() ) {25 std::cerr << "Rearranging to " << table[ name->get_name() ] << "position in the list." << std::endl;26 exprs[ table[ name->get_name() ] ] = &(*a);27 } else28 throw NoMatch( name->get_name() + "no such designation" );29 } /*else if ( TupleExpr *tup = dynamic_cast<TupleExpr *>( a->expr->get_argName() ) )30 std::cerr << "Designated expression" << std::endl; */31 exprs.push_back( &(*a) );32 } 35 for ( ResolvExpr::AltList::const_iterator a = alternatives.begin(); a != alternatives.end(); ++a ) { 36 if ( a->expr->get_argName() != 0 ) 37 if ( NameExpr *name = dynamic_cast<NameExpr *>( a->expr->get_argName() ) ) { 38 if ( table.find( name->get_name() ) != table.end() ) { 39 std::cerr << "Rearranging to " << table[ name->get_name() ] << "position in the list." << std::endl; 40 exprs[ table[ name->get_name() ] ] = &(*a); 41 } else 42 throw NoMatch( name->get_name() + "no such designation" ); 43 } /*else if ( TupleExpr *tup = dynamic_cast<TupleExpr *>( a->expr->get_argName() ) ) 44 std::cerr << "Designated expression" << std::endl; */ 45 exprs.push_back( &(*a) ); 46 } // for 33 47 34 /*std::cerr << "In matcher/match: ";35 if ( exprs.size() != index.size() )36 std::cerr << "exprs and index differ in length" << std::endl;37 else38 std::cerr << "is all good." << std::endl;39 */40 }48 /*std::cerr << "In matcher/match: "; 49 if ( exprs.size() != index.size() ) 50 std::cerr << "exprs and index differ in length" << std::endl; 51 else 52 std::cerr << "is all good." << std::endl; 53 */ 54 } 41 55 42 ResolvExpr::Alternative &NameMatcher::get_next() throw (NoMoreElements) {43 if ( current++ >= (int)(index.size()) )44 throw NoMoreElements();45 return *(new ResolvExpr::Alternative());46 }56 ResolvExpr::Alternative &NameMatcher::get_next() throw (NoMoreElements) { 57 if ( current++ >= (int)(index.size()) ) 58 throw NoMoreElements(); 59 return *(new ResolvExpr::Alternative()); 60 } 47 61 } // namespace Tuples 62 63 // Local Variables: // 64 // tab-width: 4 // 65 // mode: c++ // 66 // compile-command: "make install" // 67 // End: // -
translator/Tuples/NameMatcher.h
r0dd3a2f r51587aa 1 #ifndef _TUPLE_NAMEMATCH_H_ 2 #define _TUPLE_NAMEMATCH_H_ 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 // NameMatcher.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 15:01:37 2015 13 // Update Count : 3 14 // 15 16 #ifndef _NAMEMATCHER_H_ 17 #define _NAMEMATCHER_H_ 3 18 4 19 #include <map> … … 17 32 18 33 namespace Tuples { 19 struct NoMoreElements {};20 struct NoMatch {21 NoMatch( std::string msg ) : message( msg ) {}22 std::string message;23 };34 struct NoMoreElements {}; 35 struct NoMatch { 36 NoMatch( std::string msg ) : message( msg ) {} 37 std::string message; 38 }; 24 39 25 class NameMatcher 26 { 27 public: 28 NameMatcher( std::list< DeclarationWithType* >& ); 29 ~NameMatcher(); 40 class NameMatcher { 41 public: 42 NameMatcher( std::list< DeclarationWithType* >& ); 43 ~NameMatcher(); 30 44 31 void match( ResolvExpr::AltList &alternatives ) throw (NoMatch) ;32 ResolvExpr::Alternative &get_next() throw (NoMoreElements);45 void match( ResolvExpr::AltList &alternatives ) throw (NoMatch) ; 46 ResolvExpr::Alternative &get_next() throw (NoMoreElements); 33 47 34 private: 35 int current; 36 std::vector< DeclarationWithType* > index; 37 std::vector< const ResolvExpr::Alternative * > exprs; 38 std::map< std::string, int> table; 39 }; 40 48 private: 49 int current; 50 std::vector< DeclarationWithType* > index; 51 std::vector< const ResolvExpr::Alternative * > exprs; 52 std::map< std::string, int> table; 53 }; 41 54 } // namespace Tuples 42 55 43 #endif // #ifndef _TUPLE_NAMEMATCH_H_56 #endif // _NAMEMATCHER_H_ 44 57 45 / *46 Local Variables: 47 mode: c++ 48 End: 49 */58 // Local Variables: // 59 // tab-width: 4 // 60 // mode: c++ // 61 // compile-command: "make install" // 62 // End: // -
translator/Tuples/TupleAssignment.cc
r0dd3a2f r51587aa 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 // TupleAssignment.cc -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 15:02:53 2015 13 // Update Count : 2 14 // 15 1 16 #include "ResolvExpr/AlternativeFinder.h" 2 17 #include "ResolvExpr/Alternative.h" … … 14 29 15 30 namespace Tuples { 16 TupleAssignSpotter::TupleAssignSpotter( ResolvExpr::AlternativeFinder *f = 0 ) 17 : currentFinder(f), matcher(0), hasMatched( false ) {} 18 19 bool TupleAssignSpotter::pointsToTuple( Expression *expr ) { 20 // also check for function returning tuple of reference types 21 if (AddressExpr *addr = dynamic_cast<AddressExpr *>(expr) ) 22 if ( isTuple(addr->get_arg() ) ) 23 return true; 24 return false; 25 } 26 27 bool TupleAssignSpotter::isTupleVar( DeclarationWithType *decl ) { 28 if ( dynamic_cast<TupleType *>(decl->get_type()) ) 29 return true; 30 return false; 31 } 32 33 bool TupleAssignSpotter::isTuple( Expression *expr, bool isRight ) { 34 // true if `expr' is an expression returning a tuple: tuple, tuple variable or MRV function 35 if ( ! expr ) return false; 36 37 if ( dynamic_cast<TupleExpr *>(expr) ) 38 return true; 39 else if ( VariableExpr *var = dynamic_cast<VariableExpr *>(expr) ) { 40 if ( isTupleVar(var->get_var()) ) 41 return true; 42 } 43 44 return false; 45 } 46 47 bool TupleAssignSpotter::match() { 48 assert ( matcher != 0 ); 49 50 std::list< Expression * > new_assigns; 51 if (! matcher->match(new_assigns) ) 52 return false; 53 54 if ( new_assigns.empty() ) return false; 55 /*return */matcher->solve( new_assigns ); 56 if ( dynamic_cast<TupleAssignSpotter::MultipleAssignMatcher *>( matcher ) ) { 57 // now resolve new assignments 58 std::list< Expression * > solved_assigns; 59 ResolvExpr::AltList solved_alts; 60 assert( currentFinder != 0 ); 61 62 ResolvExpr::AltList current; 63 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i ) { 64 //try { 65 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); 66 finder.findWithAdjustment(*i); 67 // prune expressions that don't coincide with 68 ResolvExpr::AltList alts = finder.get_alternatives(); 69 assert( alts.size() == 1 ); 70 assert(alts.front().expr != 0 ); 71 current.push_back( finder.get_alternatives().front() ); 72 solved_assigns.push_back( alts.front().expr->clone() ); 73 //solved_assigns.back()->print(std::cerr); 74 /*} catch( ... ) { 75 continue; // no reasonable alternative found 76 }*/ 77 } 78 options.add_option( current ); 79 80 return true; 81 } else { // mass assignment 82 //if ( new_assigns.empty() ) return false; 83 std::list< Expression * > solved_assigns; 84 ResolvExpr::AltList solved_alts; 85 assert( currentFinder != 0 ); 86 87 ResolvExpr::AltList current; 88 if ( optMass.empty() ) { 89 for ( std::list< Expression * >::size_type i = 0; i != new_assigns.size(); ++i ) 90 optMass.push_back( ResolvExpr::AltList() ); 91 } 92 int cnt = 0; 93 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i, cnt++ ) { 94 95 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); 96 finder.findWithAdjustment(*i); 97 ResolvExpr::AltList alts = finder.get_alternatives(); 98 assert( alts.size() == 1 ); 99 assert(alts.front().expr != 0 ); 100 current.push_back( finder.get_alternatives().front() ); 101 optMass[cnt].push_back( finder.get_alternatives().front() ); 102 solved_assigns.push_back( alts.front().expr->clone() ); 103 } 104 105 return true; 106 } 107 108 return false; 109 } 110 111 bool TupleAssignSpotter::isMVR( Expression *expr ) { 112 if ( expr->get_results().size() > 1 ) { 113 // MVR processing 114 return true; 115 } 116 return false; 117 } 118 119 bool TupleAssignSpotter::isTupleAssignment( UntypedExpr * expr, std::list<ResolvExpr::AltList> &possibilities ) { 120 if ( NameExpr *assgnop = dynamic_cast< NameExpr * >(expr->get_function()) ) { 121 122 if ( assgnop->get_name() == std::string("?=?") ) { 123 124 for ( std::list<ResolvExpr::AltList>::iterator ali = possibilities.begin(); ali != possibilities.end(); ++ali ) { 125 assert( ali->size() == 2 ); 126 ResolvExpr::AltList::iterator opit = ali->begin(); 127 ResolvExpr::Alternative op1 = *opit, op2 = *(++opit); 128 129 if ( pointsToTuple(op1.expr) ) { // also handles tuple vars 130 if ( isTuple( op2.expr, true ) ) 131 matcher = new MultipleAssignMatcher(op1.expr, op2.expr); 132 else if ( isMVR( op2.expr ) ) { 133 // handle MVR differently 134 } else 135 // mass assignment 136 matcher = new MassAssignMatcher(op1.expr, op2.expr); 137 138 std::list< ResolvExpr::AltList > options; 139 if ( match() ) 140 /* 141 if ( hasMatched ) { 142 // throw SemanticError("Ambiguous tuple assignment"); 143 } else {*/ 144 // Matched for the first time 145 hasMatched = true; 146 /*} */ 147 } /* else if ( isTuple( op2 ) ) 148 throw SemanticError("Inapplicable tuple assignment."); 149 */ 150 } 151 152 if ( hasMatched ) { 153 if ( dynamic_cast<TupleAssignSpotter::MultipleAssignMatcher *>( matcher ) ) { 154 //options.print( std::cerr ); 155 std::list< ResolvExpr::AltList >best = options.get_best(); 156 if ( best.size() == 1 ) { 157 std::list<Expression *> solved_assigns; 158 for ( ResolvExpr::AltList::iterator i = best.front().begin(); i != best.front().end(); ++i ){ 159 solved_assigns.push_back( i->expr ); 160 } 161 /* assigning cost zero? */ 162 currentFinder->get_alternatives().push_front( ResolvExpr::Alternative(new SolvedTupleExpr(solved_assigns/*, SolvedTupleExpr::MULTIPLE*/), currentFinder->get_environ(), ResolvExpr::Cost() ) ); 163 } 164 } else { 165 assert(! optMass.empty() ); 166 ResolvExpr::AltList winners; 167 for ( std::vector< ResolvExpr::AltList >::iterator i = optMass.begin(); i != optMass.end(); ++i ) 168 findMinCostAlt( i->begin(), i->end(), back_inserter(winners) ); 169 170 std::list< Expression *> solved_assigns; 171 for ( ResolvExpr::AltList::iterator i = winners.begin(); i != winners.end(); ++i ) 172 solved_assigns.push_back( i->expr ); 173 currentFinder->get_alternatives().push_front( ResolvExpr::Alternative(new SolvedTupleExpr(solved_assigns/*, SolvedTupleExpr::MASS*/), currentFinder->get_environ(), ResolvExpr::Cost() ) ); 174 } 175 } 176 } 177 } 178 return hasMatched; 179 } 180 181 void TupleAssignSpotter::Matcher::init( Expression *_lhs, Expression *_rhs ) { 182 lhs.clear(); 183 if (AddressExpr *addr = dynamic_cast<AddressExpr *>(_lhs) ) 184 if ( TupleExpr *tuple = dynamic_cast<TupleExpr *>(addr->get_arg()) ) 185 std::copy( tuple->get_exprs().begin(), tuple->get_exprs().end(), back_inserter(lhs) ); 186 187 rhs.clear(); 188 } 189 190 TupleAssignSpotter::Matcher::Matcher( /*TupleAssignSpotter &spot,*/ Expression *_lhs, Expression *_rhs ) /*: own_spotter(spot) */{ 191 init(_lhs,_rhs); 192 } 193 194 TupleAssignSpotter::MultipleAssignMatcher::MultipleAssignMatcher( Expression *_lhs, Expression *_rhs )/* : own_spotter(spot) */{ 195 init(_lhs,_rhs); 196 197 if ( TupleExpr *tuple = dynamic_cast<TupleExpr *>(_rhs) ) 198 std::copy( tuple->get_exprs().begin(), tuple->get_exprs().end(), back_inserter(rhs) ); 199 } 200 201 UntypedExpr *TupleAssignSpotter::Matcher::createAssgn( Expression *left, Expression *right ) { 202 if ( left && right ) { 203 std::list< Expression * > args; 204 args.push_back(new AddressExpr(left->clone())); args.push_back(right->clone()); 205 return new UntypedExpr(new NameExpr("?=?"), args); 206 } else 207 throw 0; // xxx - diagnose the problem 208 } 209 210 bool TupleAssignSpotter::MassAssignMatcher::match( std::list< Expression * > &out ) { 211 if ( lhs.empty() || (rhs.size() != 1) ) return false; 212 213 for ( std::list< Expression * >::iterator l = lhs.begin(); l != lhs.end(); l++ ) { 214 std::list< Expression * > args; 215 args.push_back( new AddressExpr(*l) ); 216 args.push_back( rhs.front() ); 217 out.push_back( new UntypedExpr(new NameExpr("?=?"), args) ); 218 } 219 220 return true; 221 } 222 223 bool TupleAssignSpotter::MassAssignMatcher::solve( std::list< Expression * > &assigns ) { 224 /* 225 std::list< Expression * > solved_assigns; 226 ResolvExpr::AltList solved_alts; 227 assert( currentFinder != 0 ); 228 229 ResolvExpr::AltList current; 230 if ( optMass.empty() ) { 231 for ( std::list< Expression * >::size_type i = 0; i != new_assigns.size(); ++i ) 232 optMass.push_back( ResolvExpr::AltList() ); 233 } 234 int cnt = 0; 235 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i, cnt++ ) { 236 237 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); 238 finder.findWithAdjustment(*i); 239 ResolvExpr::AltList alts = finder.get_alternatives(); 240 assert( alts.size() == 1 ); 241 assert(alts.front().expr != 0 ); 242 current.push_back( finder.get_alternatives().front() ); 243 optMass[cnt].push_back( finder.get_alternatives().front() ); 244 solved_assigns.push_back( alts.front().expr->clone() ); 245 } 246 */ 247 return true; 248 } 249 250 bool TupleAssignSpotter::MultipleAssignMatcher::match( std::list< Expression * > &out ) { 251 // need more complicated matching 252 if ( lhs.size() == rhs.size() ) { 253 zipWith( lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), back_inserter(out), TupleAssignSpotter::Matcher::createAssgn ); 254 return true; 255 } //else 256 //std::cerr << "The length of (left, right) is: (" << lhs.size() << "," << rhs.size() << ")" << std::endl;*/ 257 return false; 258 } 259 260 bool TupleAssignSpotter::MultipleAssignMatcher::solve( std::list< Expression * > &assigns ) { 261 /* 262 std::list< Expression * > solved_assigns; 263 ResolvExpr::AltList solved_alts; 264 assert( currentFinder != 0 ); 265 266 ResolvExpr::AltList current; 267 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i ) { 268 //try { 269 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); 270 finder.findWithAdjustment(*i); 271 // prune expressions that don't coincide with 272 ResolvExpr::AltList alts = finder.get_alternatives(); 273 assert( alts.size() == 1 ); 274 assert(alts.front().expr != 0 ); 275 current.push_back( finder.get_alternatives().front() ); 276 solved_assigns.push_back( alts.front().expr->clone() ); 277 //solved_assigns.back()->print(std::cerr); 278 //} catch( ... ) { 279 //continue; // no reasonable alternative found 280 //} 281 } 282 options.add_option( current ); 283 */ 284 285 return true; 286 } 287 288 void TupleAssignSpotter::Options::add_option( ResolvExpr::AltList &opt ) { 289 using namespace std; 290 291 options.push_back( opt ); 292 /* 293 vector< Cost > costs; 294 costs.reserve( opt.size() ); 295 transform( opt.begin(), opt.end(), back_inserter(costs), ptr_fun(extract_cost) ); 296 */ 297 // transpose matrix 298 if ( costMatrix.empty() ) 299 for ( unsigned int i = 0; i< opt.size(); ++i) 300 costMatrix.push_back( vector<ResolvExpr::Cost>() ); 301 302 int cnt = 0; 303 for ( ResolvExpr::AltList::iterator i = opt.begin(); i != opt.end(); ++i, cnt++ ) 304 costMatrix[cnt].push_back( i->cost ); 305 306 return; 307 } 308 309 std::list< ResolvExpr::AltList > TupleAssignSpotter::Options::get_best() { 310 using namespace std; 311 using namespace ResolvExpr; 312 list< ResolvExpr::AltList > ret; 313 list< multiset<int> > solns; 314 for ( vector< vector<Cost> >::iterator i = costMatrix.begin(); i != costMatrix.end(); ++i ) { 315 list<int> current; 316 findMinCost( i->begin(), i->end(), back_inserter(current) ); 317 solns.push_back( multiset<int>(current.begin(), current.end()) ); 318 } 319 // need to combine 320 multiset<int> result; 321 lift_intersection( solns.begin(), solns.end(), inserter( result, result.begin() ) ); 322 if ( result.size() != 1 ) 323 throw SemanticError("Ambiguous tuple expression"); 324 ret.push_back(get_option( *(result.begin() ))); 325 return ret; 326 } 327 328 void TupleAssignSpotter::Options::print( std::ostream &ostr ) { 329 using namespace std; 330 331 for ( vector< vector < ResolvExpr::Cost > >::iterator i = costMatrix.begin(); i != costMatrix.end(); ++i ) { 332 for ( vector < ResolvExpr::Cost >::iterator j = i->begin(); j != i->end(); ++j ) 333 ostr << *j << " " ; 334 ostr << std::endl; 335 } 336 337 return; 338 } 339 340 ResolvExpr::Cost extract_cost( ResolvExpr::Alternative &alt ) { 341 return alt.cost; 342 } 343 344 template< typename InputIterator, typename OutputIterator > 345 void 346 TupleAssignSpotter::Options::findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) 347 { 348 using namespace ResolvExpr; 349 std::list<int> alternatives; 350 351 // select the alternatives that have the minimum parameter cost 352 Cost minCost = Cost::infinity; 353 unsigned int index = 0; 354 for ( InputIterator i = begin; i != end; ++i, index++ ) { 355 if ( *i < minCost ) { 356 minCost = *i; 357 alternatives.clear(); 358 alternatives.push_back( index ); 359 } else if ( *i == minCost ) { 360 alternatives.push_back( index ); 361 } 362 } 363 std::copy( alternatives.begin(), alternatives.end(), out ); 364 } 365 366 template< class InputIterator, class OutputIterator > 367 void TupleAssignSpotter::Options::lift_intersection( InputIterator begin, InputIterator end, OutputIterator out ){ 368 if ( begin == end ) return; 369 InputIterator test = begin; 370 371 if (++test == end) 372 { copy(begin->begin(), begin->end(), out); return; } 373 374 375 std::multiset<int> cur; // InputIterator::value_type::value_type 376 copy( begin->begin(), begin->end(), inserter( cur, cur.begin() ) ); 377 378 while ( test != end ) { 379 std::multiset<int> temp; 380 set_intersection( cur.begin(), cur.end(), test->begin(), test->end(), inserter(temp,temp.begin()) ); 381 cur.clear(); 382 copy( temp.begin(), temp.end(), inserter(cur,cur.begin())); 383 ++test; 384 } 385 386 copy( cur.begin(), cur.end(), out ); 387 return; 388 } 389 390 391 ResolvExpr::AltList TupleAssignSpotter::Options::get_option( std::list< ResolvExpr::AltList >::size_type index ) { 392 if ( index >= options.size() ) 393 throw 0; // XXX 394 std::list< ResolvExpr::AltList >::iterator it = options.begin(); 395 for ( std::list< ResolvExpr::AltList >::size_type i = 0; i < index; ++i, ++it ); 396 return *it; 397 } 398 399 31 TupleAssignSpotter::TupleAssignSpotter( ResolvExpr::AlternativeFinder *f = 0 ) 32 : currentFinder(f), matcher(0), hasMatched( false ) {} 33 34 bool TupleAssignSpotter::pointsToTuple( Expression *expr ) { 35 // also check for function returning tuple of reference types 36 if (AddressExpr *addr = dynamic_cast<AddressExpr *>(expr) ) 37 if ( isTuple(addr->get_arg() ) ) 38 return true; 39 return false; 40 } 41 42 bool TupleAssignSpotter::isTupleVar( DeclarationWithType *decl ) { 43 if ( dynamic_cast<TupleType *>(decl->get_type()) ) 44 return true; 45 return false; 46 } 47 48 bool TupleAssignSpotter::isTuple( Expression *expr, bool isRight ) { 49 // true if `expr' is an expression returning a tuple: tuple, tuple variable or MRV function 50 if ( ! expr ) return false; 51 52 if ( dynamic_cast<TupleExpr *>(expr) ) 53 return true; 54 else if ( VariableExpr *var = dynamic_cast<VariableExpr *>(expr) ) { 55 if ( isTupleVar(var->get_var()) ) 56 return true; 57 } 58 59 return false; 60 } 61 62 bool TupleAssignSpotter::match() { 63 assert ( matcher != 0 ); 64 65 std::list< Expression * > new_assigns; 66 if (! matcher->match(new_assigns) ) 67 return false; 68 69 if ( new_assigns.empty() ) return false; 70 /*return */matcher->solve( new_assigns ); 71 if ( dynamic_cast<TupleAssignSpotter::MultipleAssignMatcher *>( matcher ) ) { 72 // now resolve new assignments 73 std::list< Expression * > solved_assigns; 74 ResolvExpr::AltList solved_alts; 75 assert( currentFinder != 0 ); 76 77 ResolvExpr::AltList current; 78 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i ) { 79 //try { 80 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); 81 finder.findWithAdjustment(*i); 82 // prune expressions that don't coincide with 83 ResolvExpr::AltList alts = finder.get_alternatives(); 84 assert( alts.size() == 1 ); 85 assert(alts.front().expr != 0 ); 86 current.push_back( finder.get_alternatives().front() ); 87 solved_assigns.push_back( alts.front().expr->clone() ); 88 //solved_assigns.back()->print(std::cerr); 89 /*} catch( ... ) { 90 continue; // no reasonable alternative found 91 }*/ 92 } 93 options.add_option( current ); 94 95 return true; 96 } else { // mass assignment 97 //if ( new_assigns.empty() ) return false; 98 std::list< Expression * > solved_assigns; 99 ResolvExpr::AltList solved_alts; 100 assert( currentFinder != 0 ); 101 102 ResolvExpr::AltList current; 103 if ( optMass.empty() ) { 104 for ( std::list< Expression * >::size_type i = 0; i != new_assigns.size(); ++i ) 105 optMass.push_back( ResolvExpr::AltList() ); 106 } 107 int cnt = 0; 108 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i, cnt++ ) { 109 110 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); 111 finder.findWithAdjustment(*i); 112 ResolvExpr::AltList alts = finder.get_alternatives(); 113 assert( alts.size() == 1 ); 114 assert(alts.front().expr != 0 ); 115 current.push_back( finder.get_alternatives().front() ); 116 optMass[cnt].push_back( finder.get_alternatives().front() ); 117 solved_assigns.push_back( alts.front().expr->clone() ); 118 } 119 120 return true; 121 } 122 123 return false; 124 } 125 126 bool TupleAssignSpotter::isMVR( Expression *expr ) { 127 if ( expr->get_results().size() > 1 ) { 128 // MVR processing 129 return true; 130 } 131 return false; 132 } 133 134 bool TupleAssignSpotter::isTupleAssignment( UntypedExpr * expr, std::list<ResolvExpr::AltList> &possibilities ) { 135 if ( NameExpr *assgnop = dynamic_cast< NameExpr * >(expr->get_function()) ) { 136 137 if ( assgnop->get_name() == std::string("?=?") ) { 138 139 for ( std::list<ResolvExpr::AltList>::iterator ali = possibilities.begin(); ali != possibilities.end(); ++ali ) { 140 assert( ali->size() == 2 ); 141 ResolvExpr::AltList::iterator opit = ali->begin(); 142 ResolvExpr::Alternative op1 = *opit, op2 = *(++opit); 143 144 if ( pointsToTuple(op1.expr) ) { // also handles tuple vars 145 if ( isTuple( op2.expr, true ) ) 146 matcher = new MultipleAssignMatcher(op1.expr, op2.expr); 147 else if ( isMVR( op2.expr ) ) { 148 // handle MVR differently 149 } else 150 // mass assignment 151 matcher = new MassAssignMatcher(op1.expr, op2.expr); 152 153 std::list< ResolvExpr::AltList > options; 154 if ( match() ) 155 /* 156 if ( hasMatched ) { 157 // throw SemanticError("Ambiguous tuple assignment"); 158 } else {*/ 159 // Matched for the first time 160 hasMatched = true; 161 /*} */ 162 } /* else if ( isTuple( op2 ) ) 163 throw SemanticError("Inapplicable tuple assignment."); 164 */ 165 } 166 167 if ( hasMatched ) { 168 if ( dynamic_cast<TupleAssignSpotter::MultipleAssignMatcher *>( matcher ) ) { 169 //options.print( std::cerr ); 170 std::list< ResolvExpr::AltList >best = options.get_best(); 171 if ( best.size() == 1 ) { 172 std::list<Expression *> solved_assigns; 173 for ( ResolvExpr::AltList::iterator i = best.front().begin(); i != best.front().end(); ++i ){ 174 solved_assigns.push_back( i->expr ); 175 } 176 /* assigning cost zero? */ 177 currentFinder->get_alternatives().push_front( ResolvExpr::Alternative(new SolvedTupleExpr(solved_assigns/*, SolvedTupleExpr::MULTIPLE*/), currentFinder->get_environ(), ResolvExpr::Cost() ) ); 178 } 179 } else { 180 assert(! optMass.empty() ); 181 ResolvExpr::AltList winners; 182 for ( std::vector< ResolvExpr::AltList >::iterator i = optMass.begin(); i != optMass.end(); ++i ) 183 findMinCostAlt( i->begin(), i->end(), back_inserter(winners) ); 184 185 std::list< Expression *> solved_assigns; 186 for ( ResolvExpr::AltList::iterator i = winners.begin(); i != winners.end(); ++i ) 187 solved_assigns.push_back( i->expr ); 188 currentFinder->get_alternatives().push_front( ResolvExpr::Alternative(new SolvedTupleExpr(solved_assigns/*, SolvedTupleExpr::MASS*/), currentFinder->get_environ(), ResolvExpr::Cost() ) ); 189 } 190 } 191 } 192 } 193 return hasMatched; 194 } 195 196 void TupleAssignSpotter::Matcher::init( Expression *_lhs, Expression *_rhs ) { 197 lhs.clear(); 198 if (AddressExpr *addr = dynamic_cast<AddressExpr *>(_lhs) ) 199 if ( TupleExpr *tuple = dynamic_cast<TupleExpr *>(addr->get_arg()) ) 200 std::copy( tuple->get_exprs().begin(), tuple->get_exprs().end(), back_inserter(lhs) ); 201 202 rhs.clear(); 203 } 204 205 TupleAssignSpotter::Matcher::Matcher( /*TupleAssignSpotter &spot,*/ Expression *_lhs, Expression *_rhs ) /*: own_spotter(spot) */{ 206 init(_lhs,_rhs); 207 } 208 209 TupleAssignSpotter::MultipleAssignMatcher::MultipleAssignMatcher( Expression *_lhs, Expression *_rhs )/* : own_spotter(spot) */{ 210 init(_lhs,_rhs); 211 212 if ( TupleExpr *tuple = dynamic_cast<TupleExpr *>(_rhs) ) 213 std::copy( tuple->get_exprs().begin(), tuple->get_exprs().end(), back_inserter(rhs) ); 214 } 215 216 UntypedExpr *TupleAssignSpotter::Matcher::createAssgn( Expression *left, Expression *right ) { 217 if ( left && right ) { 218 std::list< Expression * > args; 219 args.push_back(new AddressExpr(left->clone())); args.push_back(right->clone()); 220 return new UntypedExpr(new NameExpr("?=?"), args); 221 } else 222 throw 0; // xxx - diagnose the problem 223 } 224 225 bool TupleAssignSpotter::MassAssignMatcher::match( std::list< Expression * > &out ) { 226 if ( lhs.empty() || (rhs.size() != 1) ) return false; 227 228 for ( std::list< Expression * >::iterator l = lhs.begin(); l != lhs.end(); l++ ) { 229 std::list< Expression * > args; 230 args.push_back( new AddressExpr(*l) ); 231 args.push_back( rhs.front() ); 232 out.push_back( new UntypedExpr(new NameExpr("?=?"), args) ); 233 } 234 235 return true; 236 } 237 238 bool TupleAssignSpotter::MassAssignMatcher::solve( std::list< Expression * > &assigns ) { 239 /* 240 std::list< Expression * > solved_assigns; 241 ResolvExpr::AltList solved_alts; 242 assert( currentFinder != 0 ); 243 244 ResolvExpr::AltList current; 245 if ( optMass.empty() ) { 246 for ( std::list< Expression * >::size_type i = 0; i != new_assigns.size(); ++i ) 247 optMass.push_back( ResolvExpr::AltList() ); 248 } 249 int cnt = 0; 250 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i, cnt++ ) { 251 252 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); 253 finder.findWithAdjustment(*i); 254 ResolvExpr::AltList alts = finder.get_alternatives(); 255 assert( alts.size() == 1 ); 256 assert(alts.front().expr != 0 ); 257 current.push_back( finder.get_alternatives().front() ); 258 optMass[cnt].push_back( finder.get_alternatives().front() ); 259 solved_assigns.push_back( alts.front().expr->clone() ); 260 } 261 */ 262 return true; 263 } 264 265 bool TupleAssignSpotter::MultipleAssignMatcher::match( std::list< Expression * > &out ) { 266 // need more complicated matching 267 if ( lhs.size() == rhs.size() ) { 268 zipWith( lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), back_inserter(out), TupleAssignSpotter::Matcher::createAssgn ); 269 return true; 270 } //else 271 //std::cerr << "The length of (left, right) is: (" << lhs.size() << "," << rhs.size() << ")" << std::endl;*/ 272 return false; 273 } 274 275 bool TupleAssignSpotter::MultipleAssignMatcher::solve( std::list< Expression * > &assigns ) { 276 /* 277 std::list< Expression * > solved_assigns; 278 ResolvExpr::AltList solved_alts; 279 assert( currentFinder != 0 ); 280 281 ResolvExpr::AltList current; 282 for ( std::list< Expression * >::iterator i = new_assigns.begin(); i != new_assigns.end(); ++i ) { 283 //try { 284 ResolvExpr::AlternativeFinder finder( currentFinder->get_indexer(), currentFinder->get_environ() ); 285 finder.findWithAdjustment(*i); 286 // prune expressions that don't coincide with 287 ResolvExpr::AltList alts = finder.get_alternatives(); 288 assert( alts.size() == 1 ); 289 assert(alts.front().expr != 0 ); 290 current.push_back( finder.get_alternatives().front() ); 291 solved_assigns.push_back( alts.front().expr->clone() ); 292 //solved_assigns.back()->print(std::cerr); 293 //} catch( ... ) { 294 //continue; // no reasonable alternative found 295 //} 296 } 297 options.add_option( current ); 298 */ 299 300 return true; 301 } 302 303 void TupleAssignSpotter::Options::add_option( ResolvExpr::AltList &opt ) { 304 using namespace std; 305 306 options.push_back( opt ); 307 /* 308 vector< Cost > costs; 309 costs.reserve( opt.size() ); 310 transform( opt.begin(), opt.end(), back_inserter(costs), ptr_fun(extract_cost) ); 311 */ 312 // transpose matrix 313 if ( costMatrix.empty() ) 314 for ( unsigned int i = 0; i< opt.size(); ++i) 315 costMatrix.push_back( vector<ResolvExpr::Cost>() ); 316 317 int cnt = 0; 318 for ( ResolvExpr::AltList::iterator i = opt.begin(); i != opt.end(); ++i, cnt++ ) 319 costMatrix[cnt].push_back( i->cost ); 320 321 return; 322 } 323 324 std::list< ResolvExpr::AltList > TupleAssignSpotter::Options::get_best() { 325 using namespace std; 326 using namespace ResolvExpr; 327 list< ResolvExpr::AltList > ret; 328 list< multiset<int> > solns; 329 for ( vector< vector<Cost> >::iterator i = costMatrix.begin(); i != costMatrix.end(); ++i ) { 330 list<int> current; 331 findMinCost( i->begin(), i->end(), back_inserter(current) ); 332 solns.push_back( multiset<int>(current.begin(), current.end()) ); 333 } 334 // need to combine 335 multiset<int> result; 336 lift_intersection( solns.begin(), solns.end(), inserter( result, result.begin() ) ); 337 if ( result.size() != 1 ) 338 throw SemanticError("Ambiguous tuple expression"); 339 ret.push_back(get_option( *(result.begin() ))); 340 return ret; 341 } 342 343 void TupleAssignSpotter::Options::print( std::ostream &ostr ) { 344 using namespace std; 345 346 for ( vector< vector < ResolvExpr::Cost > >::iterator i = costMatrix.begin(); i != costMatrix.end(); ++i ) { 347 for ( vector < ResolvExpr::Cost >::iterator j = i->begin(); j != i->end(); ++j ) 348 ostr << *j << " " ; 349 ostr << std::endl; 350 } // for 351 return; 352 } 353 354 ResolvExpr::Cost extract_cost( ResolvExpr::Alternative &alt ) { 355 return alt.cost; 356 } 357 358 template< typename InputIterator, typename OutputIterator > 359 void TupleAssignSpotter::Options::findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) { 360 using namespace ResolvExpr; 361 std::list<int> alternatives; 362 363 // select the alternatives that have the minimum parameter cost 364 Cost minCost = Cost::infinity; 365 unsigned int index = 0; 366 for ( InputIterator i = begin; i != end; ++i, index++ ) { 367 if ( *i < minCost ) { 368 minCost = *i; 369 alternatives.clear(); 370 alternatives.push_back( index ); 371 } else if ( *i == minCost ) { 372 alternatives.push_back( index ); 373 } 374 } 375 std::copy( alternatives.begin(), alternatives.end(), out ); 376 } 377 378 template< class InputIterator, class OutputIterator > 379 void TupleAssignSpotter::Options::lift_intersection( InputIterator begin, InputIterator end, OutputIterator out ){ 380 if ( begin == end ) return; 381 InputIterator test = begin; 382 383 if (++test == end) 384 { copy(begin->begin(), begin->end(), out); return; } 385 386 387 std::multiset<int> cur; // InputIterator::value_type::value_type 388 copy( begin->begin(), begin->end(), inserter( cur, cur.begin() ) ); 389 390 while ( test != end ) { 391 std::multiset<int> temp; 392 set_intersection( cur.begin(), cur.end(), test->begin(), test->end(), inserter(temp,temp.begin()) ); 393 cur.clear(); 394 copy( temp.begin(), temp.end(), inserter(cur,cur.begin())); 395 ++test; 396 } 397 398 copy( cur.begin(), cur.end(), out ); 399 return; 400 } 401 402 ResolvExpr::AltList TupleAssignSpotter::Options::get_option( std::list< ResolvExpr::AltList >::size_type index ) { 403 if ( index >= options.size() ) 404 throw 0; // XXX 405 std::list< ResolvExpr::AltList >::iterator it = options.begin(); 406 for ( std::list< ResolvExpr::AltList >::size_type i = 0; i < index; ++i, ++it ); 407 return *it; 408 } 400 409 } // namespace Tuples 410 411 // Local Variables: // 412 // tab-width: 4 // 413 // mode: c++ // 414 // compile-command: "make install" // 415 // End: // -
translator/Tuples/TupleAssignment.h
r0dd3a2f r51587aa 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 // TupleAssignment.h -- 8 // 9 // Author : Richard C. Bilson 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 18 15:04:02 2015 13 // Update Count : 2 14 // 15 1 16 #ifndef _TUPLE_ASSIGNMENT_H_ 2 17 #define _TUPLE_ASSIGNMENT_H_ … … 11 26 12 27 namespace Tuples { 13 class TupleAssignSpotter {14 public:15 // dispatcher for Tuple (multiple and mass) assignment operations16 TupleAssignSpotter( ResolvExpr::AlternativeFinder * );17 ~TupleAssignSpotter() { delete matcher; matcher = 0; }28 class TupleAssignSpotter { 29 public: 30 // dispatcher for Tuple (multiple and mass) assignment operations 31 TupleAssignSpotter( ResolvExpr::AlternativeFinder * ); 32 ~TupleAssignSpotter() { delete matcher; matcher = 0; } 18 33 19 bool pointsToTuple( Expression * ); 20 static bool isTupleVar( DeclarationWithType * ); 21 bool isTuple( Expression *, bool isRight = false ); 22 bool isMVR( Expression * ); 23 bool isTupleAssignment( UntypedExpr *, std::list<ResolvExpr::AltList> & ); 24 bool match(); 34 bool pointsToTuple( Expression * ); 35 static bool isTupleVar( DeclarationWithType * ); 36 bool isTuple( Expression *, bool isRight = false ); 37 bool isMVR( Expression * ); 38 bool isTupleAssignment( UntypedExpr *, std::list<ResolvExpr::AltList> & ); 39 bool match(); 40 private: 41 // records for assignment generation 42 class Options { 43 public: 44 void add_option( ResolvExpr::AltList &opt ); 45 std::list< ResolvExpr::AltList > get_best(); 46 void print( std::ostream & ); 47 int size() const { return options.size(); } 48 ResolvExpr::AltList get_option( std::list< ResolvExpr::AltList >::size_type index ); 25 49 26 private: 27 // records for assignment generation 28 class Options { 29 public: 30 void add_option( ResolvExpr::AltList &opt ); 31 std::list< ResolvExpr::AltList > get_best(); 32 void print( std::ostream & ); 33 int size() const { return options.size(); } 34 ResolvExpr::AltList get_option( std::list< ResolvExpr::AltList >::size_type index ); 50 // should really use the one in ResolvExpr/AlternativeFinder, but it's too coupled with the object 51 template< typename InputIterator, typename OutputIterator > 52 void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ); 35 53 36 // should really use the one in ResolvExpr/AlternativeFinder, but it's too coupled with the object 37 template< typename InputIterator, typename OutputIterator > 38 void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ); 54 template< typename InputIterator, typename OutputIterator > 55 void lift_intersection( InputIterator begin, InputIterator end, OutputIterator out ); 56 private: 57 std::list< ResolvExpr::AltList > options; 58 std::vector< std::vector< ResolvExpr::Cost > > costMatrix; 59 }; 39 60 40 template< typename InputIterator, typename OutputIterator > 41 void lift_intersection( InputIterator begin, InputIterator end, OutputIterator out ); 42 private: 43 std::list< ResolvExpr::AltList > options; 44 std::vector< std::vector< ResolvExpr::Cost > > costMatrix; 45 }; 61 class Matcher { 62 public: 63 Matcher( /*TupleAssignSpotter &spot, */Expression *_lhs, Expression *_rhs ); 64 virtual ~Matcher() {} 65 virtual bool match( std::list< Expression * > &out ) = 0; 66 virtual bool solve( std::list< Expression * > &assigns ) = 0; 67 static UntypedExpr *createAssgn( Expression *left, Expression *right ); 68 protected: 69 Matcher() /*: own_spotter( TupleAssignSpotter(0) ) */{} 70 void init(/* TupleAssignSpotter &, */Expression *_lhs, Expression *_rhs ); 71 std::list< Expression * > lhs, rhs; 72 //TupleAssignSpotter &own_spotter; 73 }; 46 74 47 class Matcher { 48 public: 49 Matcher( /*TupleAssignSpotter &spot, */Expression *_lhs, Expression *_rhs ); 50 virtual ~Matcher() {} 51 virtual bool match( std::list< Expression * > &out ) = 0; 52 virtual bool solve( std::list< Expression * > &assigns ) = 0; 53 static UntypedExpr *createAssgn( Expression *left, Expression *right ); 54 protected: 55 Matcher() /*: own_spotter( TupleAssignSpotter(0) ) */{} 56 void init(/* TupleAssignSpotter &, */Expression *_lhs, Expression *_rhs ); 57 std::list< Expression * > lhs, rhs; 58 //TupleAssignSpotter &own_spotter; 59 }; 75 class MassAssignMatcher : public Matcher { 76 public: 77 MassAssignMatcher( Expression *_lhs, Expression *_rhs ) : Matcher( _lhs, _rhs ) { 78 rhs.push_back( _rhs ); 79 } 80 virtual bool match( std::list< Expression * > &out ); 81 virtual bool solve( std::list< Expression * > &assigns ); 82 private: 83 //std::vector< ResolvExpr::AltList > optMass; 84 }; 60 85 61 class MassAssignMatcher : public Matcher { 62 public: 63 MassAssignMatcher( Expression *_lhs, Expression *_rhs ) : Matcher( _lhs, _rhs ) { 64 rhs.push_back( _rhs ); 65 } 66 virtual bool match( std::list< Expression * > &out ); 67 virtual bool solve( std::list< Expression * > &assigns ); 68 private: 69 //std::vector< ResolvExpr::AltList > optMass; 70 }; 86 class MultipleAssignMatcher : public Matcher { 87 public: 88 MultipleAssignMatcher( Expression *_lhs, Expression *_rhs ); 89 virtual bool match( std::list< Expression * > &out ); 90 virtual bool solve( std::list< Expression * > &assigns ); 91 private: 92 //Options options; 93 }; 71 94 72 class MultipleAssignMatcher : public Matcher { 73 public: 74 MultipleAssignMatcher( Expression *_lhs, Expression *_rhs ); 75 virtual bool match( std::list< Expression * > &out ); 76 virtual bool solve( std::list< Expression * > &assigns ); 77 private: 78 //Options options; 79 }; 95 friend class Matcher; 80 96 81 friend class Matcher; 97 ResolvExpr::AlternativeFinder *currentFinder; 98 //std::list<Expression *> rhs, lhs; 99 Expression *rhs, *lhs; 100 Matcher *matcher; 101 bool hasMatched; 102 Options options; 103 std::vector< ResolvExpr::AltList > optMass; 104 }; 82 105 83 ResolvExpr::AlternativeFinder *currentFinder; 84 //std::list<Expression *> rhs, lhs; 85 Expression *rhs, *lhs; 86 Matcher *matcher; 87 bool hasMatched; 88 Options options; 89 std::vector< ResolvExpr::AltList > optMass; 90 }; 106 ResolvExpr::Cost extract_cost( ResolvExpr::Alternative & ); 91 107 108 template< typename InputIterator, typename OutputIterator > 109 void findMinCostAlt( InputIterator begin, InputIterator end, OutputIterator out ) { 110 using namespace ResolvExpr; 111 AltList alternatives; 92 112 93 ResolvExpr::Cost extract_cost( ResolvExpr::Alternative & ); 94 95 template< typename InputIterator, typename OutputIterator > 96 void 97 findMinCostAlt( InputIterator begin, InputIterator end, OutputIterator out ) 98 { 99 using namespace ResolvExpr; 100 AltList alternatives; 101 102 // select the alternatives that have the minimum parameter cost 103 Cost minCost = Cost::infinity; 104 for ( AltList::iterator i = begin; i != end; ++i ) { 105 if ( i->cost < minCost ) { 106 minCost = i->cost; 107 i->cost = i->cvtCost; 108 alternatives.clear(); 109 alternatives.push_back( *i ); 110 } else if ( i->cost == minCost ) { 111 i->cost = i->cvtCost; 112 alternatives.push_back( *i ); 113 } 114 } 115 std::copy( alternatives.begin(), alternatives.end(), out ); 116 } 117 113 // select the alternatives that have the minimum parameter cost 114 Cost minCost = Cost::infinity; 115 for ( AltList::iterator i = begin; i != end; ++i ) { 116 if ( i->cost < minCost ) { 117 minCost = i->cost; 118 i->cost = i->cvtCost; 119 alternatives.clear(); 120 alternatives.push_back( *i ); 121 } else if ( i->cost == minCost ) { 122 i->cost = i->cvtCost; 123 alternatives.push_back( *i ); 124 } 125 } 126 std::copy( alternatives.begin(), alternatives.end(), out ); 127 } 118 128 } // namespace Tuples 119 129 130 #endif // _TUPLE_ASSIGNMENT_H_ 120 131 121 #endif // #ifndef _TUPLE_ASSIGNMENT_H_ 122 123 /* 124 Local Variables: 125 mode: c++ 126 End: 127 */ 132 // Local Variables: // 133 // tab-width: 4 // 134 // mode: c++ // 135 // compile-command: "make install" // 136 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.