Changeset 6eb8948 for src/Tuples
- Timestamp:
- Sep 12, 2016, 6:32:46 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- f006f01
- Parents:
- 8f7cea1
- git-author:
- Rob Schluntz <rschlunt@…> (09/12/16 18:24:34)
- git-committer:
- Rob Schluntz <rschlunt@…> (09/12/16 18:32:46)
- Location:
- src/Tuples
- Files:
-
- 1 added
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/Tuples/TupleAssignment.cc ¶
r8f7cea1 r6eb8948 18 18 #include "ResolvExpr/typeops.h" 19 19 #include "SynTree/Expression.h" 20 #include "TupleAssignment.h" 20 #include "SynTree/Initializer.h" 21 #include "Tuples.h" 21 22 #include "Common/SemanticError.h" 22 23 … … 40 41 // records for assignment generation 41 42 struct Options { 42 std::list< ResolvExpr::AltList > get_best();43 43 void print( std::ostream & ); 44 44 int size() const { return options.size(); } … … 51 51 }; 52 52 53 classMatcher {53 struct Matcher { 54 54 public: 55 55 Matcher( TupleAssignSpotter &spotter, Expression *_lhs, Expression *_rhs ); 56 56 virtual ~Matcher() {} 57 57 virtual void match( std::list< Expression * > &out ) = 0; 58 virtual void solve() = 0;59 static UntypedExpr *createAssgn( Expression *left, Expression *right );60 protected:61 58 std::list< Expression * > lhs, rhs; 62 59 TupleAssignSpotter &spotter; 63 }; 64 65 class MassAssignMatcher : public Matcher { 60 std::list< ObjectDecl * > tmpDecls; 61 }; 62 63 struct MassAssignMatcher : public Matcher { 66 64 public: 67 65 MassAssignMatcher( TupleAssignSpotter &spotter, Expression *lhs, Expression *rhs ) : Matcher( spotter, lhs, rhs ) { … … 69 67 } 70 68 virtual void match( std::list< Expression * > &out ); 71 virtual void solve(); 72 }; 73 74 class MultipleAssignMatcher : public Matcher { 69 }; 70 71 struct MultipleAssignMatcher : public Matcher { 75 72 public: 76 73 MultipleAssignMatcher( TupleAssignSpotter &spot, Expression *lhs, Expression *rhs ); 77 74 virtual void match( std::list< Expression * > &out ); 78 virtual void solve();79 75 }; 80 76 81 77 ResolvExpr::AlternativeFinder ¤tFinder; 82 Expression *rhs, *lhs;78 // Expression *rhs, *lhs; 83 79 Matcher *matcher = nullptr; 84 80 Options options; … … 149 145 150 146 if ( new_assigns.empty() ) return; 151 std::list< Expression * > solved_assigns;152 147 ResolvExpr::AltList current; 153 148 // now resolve new assignments … … 161 156 current.push_back( alts.front() ); 162 157 } 163 options.options.push_back( current ); 164 165 matcher->solve(); 158 159 // extract expressions from the assignment alternatives to produce a list of assignments that 160 // together form a single alternative 161 std::list< Expression *> solved_assigns; 162 for ( ResolvExpr::Alternative & alt : current ) { 163 solved_assigns.push_back( alt.expr->clone() ); 164 } 165 // xxx - need to do this?? 166 // TypeEnvironment compositeEnv; 167 // simpleCombineEnvironments( i->begin(), i->end(), compositeEnv ); 168 currentFinder.get_alternatives().push_front( ResolvExpr::Alternative(new TupleAssignExpr(solved_assigns, matcher->tmpDecls), currentFinder.get_environ(), ResolvExpr::sumCost( current ) ) ); 166 169 } 167 170 … … 179 182 } 180 183 181 UntypedExpr * TupleAssignSpotter::Matcher::createAssgn( Expression *left, Expression*right ) {184 UntypedExpr * createAssgn( ObjectDecl *left, ObjectDecl *right ) { 182 185 assert( left && right ); 183 186 std::list< Expression * > args; 184 args.push_back( new AddressExpr( left->clone() ) );185 args.push_back( right->clone() );187 args.push_back( new AddressExpr( new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ new VariableExpr( left ) } ) ) ); 188 args.push_back( new VariableExpr( right ) ); 186 189 return new UntypedExpr( new NameExpr( "?=?" ), args ); 187 190 } 188 191 192 ObjectDecl * newObject( UniqueName & namer, Expression * expr ) { 193 Type * type; 194 assert( expr->get_results().size() >= 1 ); 195 if ( expr->get_results().size() > 1 ) { 196 TupleType * tt = new TupleType( Type::Qualifiers() ); 197 cloneAll( expr->get_results(), tt->get_types() ); 198 type = tt; 199 } else { 200 type = expr->get_results().front()->clone(); 201 } 202 return new ObjectDecl( namer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, type, new SingleInit( expr->clone() ) ); 203 } 204 189 205 void TupleAssignSpotter::MassAssignMatcher::match( std::list< Expression * > &out ) { 206 static UniqueName lhsNamer( "__massassign_L" ); 207 static UniqueName rhsNamer( "__massassign_R" ); 190 208 assert ( ! lhs.empty() && rhs.size() == 1); 191 209 210 ObjectDecl * rtmp = newObject( rhsNamer, rhs.front() ); 192 211 for ( Expression * l : lhs ) { 193 out.push_back( createAssgn( l, rhs.front() ) ); 194 } 195 } 196 197 void TupleAssignSpotter::MassAssignMatcher::solve() { 198 assert( ! spotter.options.empty() ); 199 for ( std::list< ResolvExpr::AltList >::iterator i = spotter.options.begin(); i != spotter.options.end(); ++i ) { 200 // extract expressions from the alternatives to produce a list of assignments that 201 // together form a single alternative 202 std::list< Expression *> solved_assigns; 203 for ( ResolvExpr::Alternative & alt : *i ) { 204 solved_assigns.push_back( alt.expr ); 205 } 206 spotter.currentFinder.get_alternatives().push_front( ResolvExpr::Alternative(new SolvedTupleExpr(solved_assigns), spotter.currentFinder.get_environ(), ResolvExpr::sumCost( *i ) ) ); 207 } 212 ObjectDecl * ltmp = newObject( lhsNamer, new AddressExpr( l ) ); 213 out.push_back( createAssgn( ltmp, rtmp ) ); 214 tmpDecls.push_back( ltmp ); 215 } 216 tmpDecls.push_back( rtmp ); 208 217 } 209 218 210 219 void TupleAssignSpotter::MultipleAssignMatcher::match( std::list< Expression * > &out ) { 220 static UniqueName lhsNamer( "__multassign_L" ); 221 static UniqueName rhsNamer( "__multassign_R" ); 211 222 // xxx - need more complicated matching? 212 223 if ( lhs.size() == rhs.size() ) { 213 zipWith( lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), back_inserter(out), TupleAssignSpotter::Matcher::createAssgn ); 214 } 215 } 216 217 void TupleAssignSpotter::MultipleAssignMatcher::solve() { 218 // options.print( std::cerr ); 219 std::list< ResolvExpr::AltList > best = spotter.options.get_best(); 220 if ( best.size() == 1 ) { 221 std::list<Expression *> solved_assigns; 222 for ( ResolvExpr::AltList::iterator i = best.front().begin(); i != best.front().end(); ++i ) { 223 solved_assigns.push_back( i->expr ); 224 } 225 /* assigning cost zero? */ 226 spotter.currentFinder.get_alternatives().push_front( ResolvExpr::Alternative(new SolvedTupleExpr(solved_assigns), spotter.currentFinder.get_environ(), ResolvExpr::Cost() ) ); 227 } 228 } 229 230 std::list< ResolvExpr::AltList > TupleAssignSpotter::Options::get_best() { 231 std::list< ResolvExpr::AltList > ret; 232 std::list< ResolvExpr::AltList > solns; 233 for ( ResolvExpr::AltList & i : options ) { 234 ResolvExpr::AltList current; 235 findMinCost( i.begin(), i.end(), back_inserter(current) ); 236 solns.push_back( ResolvExpr::AltList(current.begin(), current.end()) ); 237 } 238 // need to combine -- previously "lift_intersection", which 239 // did some madness involving, effectively, the intersection of 240 // a bunch of AltLists 241 std::list<ResolvExpr::AltList> result = solns; 242 if ( result.size() != 1 ) { 243 throw SemanticError("Ambiguous tuple expression"); 244 } 245 ret.push_back( *result.begin() ); 246 return ret; 224 std::list< ObjectDecl * > ltmp; 225 std::list< ObjectDecl * > rtmp; 226 std::transform( lhs.begin(), lhs.end(), back_inserter( ltmp ), []( Expression * expr ){ 227 return newObject( lhsNamer, new AddressExpr( expr ) ); 228 }); 229 std::transform( rhs.begin(), rhs.end(), back_inserter( rtmp ), []( Expression * expr ){ 230 return newObject( rhsNamer, expr ); 231 }); 232 zipWith( ltmp.begin(), ltmp.end(), rtmp.begin(), rtmp.end(), back_inserter(out), createAssgn ); 233 tmpDecls.splice( tmpDecls.end(), ltmp ); 234 tmpDecls.splice( tmpDecls.end(), rtmp ); 235 } 247 236 } 248 237 -
TabularUnified src/Tuples/Tuples.h ¶
r8f7cea1 r6eb8948 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // Tuple Assignment.h --7 // Tuples.h -- 8 8 // 9 9 // Author : Rodolfo G. Esteves … … 14 14 // 15 15 16 #ifndef _TUPLE _ASSIGNMENT_H_17 #define _TUPLE_ ASSIGNMENT_H_16 #ifndef _TUPLES_H_ 17 #define _TUPLE_H_ 18 18 19 19 #include <string> … … 26 26 27 27 namespace Tuples { 28 // TupleAssignment.cc 28 29 void handleTupleAssignment( ResolvExpr::AlternativeFinder & currentFinder, UntypedExpr * assign, std::list<ResolvExpr::AltList> & possibilities ); 30 31 // TupleExpansion.cc 32 void expandTuples( std::list< Declaration * > & translationUnit ); 29 33 } // namespace Tuples 30 34 -
TabularUnified src/Tuples/module.mk ¶
r8f7cea1 r6eb8948 15 15 ############################################################################### 16 16 17 SRC += Tuples/TupleAssignment.cc 17 SRC += Tuples/TupleAssignment.cc \ 18 Tuples/TupleExpansion.cc
Note: See TracChangeset
for help on using the changeset viewer.