Changeset 51587aa for translator/Tuples/AssignExpand.cc
- Timestamp:
- May 18, 2015, 11:45:33 PM (9 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, with_gc
- Children:
- 01aeade
- Parents:
- 0dd3a2f
- File:
-
- 1 edited
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 31 AssignExpander::AssignExpander() : temporaryNamer("__tpl") {} 17 32 18 19 20 21 22 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 25 26 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 30 } 44 newSt->get_kids().splice(newSt->get_kids().end(), extra); 45 } // if 31 46 32 if (! extra2.empty() ) {33 34 newSt= new CompoundStmt(std::list<Label>());47 if ( ! extra2.empty() ) { 48 if ( ! newSt ) 49 newSt= new CompoundStmt(std::list<Label>()); 35 50 36 37 51 newSt->get_kids().splice(newSt->get_kids().end(), extra2); 52 } 38 53 39 if (! replace.empty() ) {40 41 newSt= new CompoundStmt(std::list<Label>());54 if ( ! replace.empty() ) { 55 if ( ! newSt ) 56 newSt= new CompoundStmt(std::list<Label>()); 42 57 43 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 48 62 if ( newSt ) return newSt; else return exprStmt; 63 } 49 64 50 51 52 65 Expression *AssignExpander::mutate( SolvedTupleExpr *tupleExpr ) { 66 /* 67 std::list<Expression *> &exprs = tupleExpr->get_exprs(); 53 68 54 55 56 57 58 59 60 61 62 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 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 71 85 return tupleExpr; 86 } else if ( tupleExpr->get_type() == SolvedTupleExpr::MULTIPLE || 72 87 tupleExpr->get_type() == SolvedTupleExpr::MASS ) */ { 73 74 75 std::list<Statement *> decls;76 std::list<Statement *> temps;77 std::list<Statement *> assigns;78 if ( ApplicationExpr *app = dynamic_cast< ApplicationExpr * >(*i) ) {79 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 82 83 84 85 86 87 88 89 90 91 92 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 96 97 98 99 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 102 103 104 105 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: //
Note: See TracChangeset
for help on using the changeset viewer.