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 : Rodolfo G. Esteves |
---|
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 | |
---|
16 | #ifndef _TUPLE_ASSIGNMENT_H_ |
---|
17 | #define _TUPLE_ASSIGNMENT_H_ |
---|
18 | |
---|
19 | #include <string> |
---|
20 | #include <vector> |
---|
21 | #include "ResolvExpr/AlternativeFinder.h" |
---|
22 | |
---|
23 | #include "SynTree/Expression.h" |
---|
24 | #include "SynTree/Declaration.h" |
---|
25 | #include "SynTree/Type.h" |
---|
26 | |
---|
27 | namespace Tuples { |
---|
28 | class TupleAssignSpotter { |
---|
29 | public: |
---|
30 | // dispatcher for Tuple (multiple and mass) assignment operations |
---|
31 | TupleAssignSpotter( ResolvExpr::AlternativeFinder * = 0 ); |
---|
32 | ~TupleAssignSpotter() { delete matcher; matcher = 0; } |
---|
33 | |
---|
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 ); |
---|
49 | |
---|
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 ); |
---|
53 | |
---|
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 | }; |
---|
60 | |
---|
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 | }; |
---|
74 | |
---|
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 | }; |
---|
85 | |
---|
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 | }; |
---|
94 | |
---|
95 | friend class Matcher; |
---|
96 | |
---|
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 | }; |
---|
105 | |
---|
106 | ResolvExpr::Cost extract_cost( ResolvExpr::Alternative & ); |
---|
107 | |
---|
108 | template< typename InputIterator, typename OutputIterator > |
---|
109 | void findMinCostAlt( InputIterator begin, InputIterator end, OutputIterator out ) { |
---|
110 | using namespace ResolvExpr; |
---|
111 | AltList alternatives; |
---|
112 | |
---|
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 | } |
---|
128 | } // namespace Tuples |
---|
129 | |
---|
130 | #endif // _TUPLE_ASSIGNMENT_H_ |
---|
131 | |
---|
132 | // Local Variables: // |
---|
133 | // tab-width: 4 // |
---|
134 | // mode: c++ // |
---|
135 | // compile-command: "make install" // |
---|
136 | // End: // |
---|