source: src/Tuples/TupleExpansion.cc @ bf32bb8

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since bf32bb8 was bf32bb8, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

implement transformation for MemberTupleExprs?

  • Property mode set to 100644
File size: 9.9 KB
Line 
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           : 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:02:53 2015
13// Update Count     : 2
14//
15
16#include <iterator>
17#include <iostream>
18#include <cassert>
19#include "Tuples.h"
20#include "GenPoly/DeclMutator.h"
21#include "SynTree/Mutator.h"
22#include "SynTree/Statement.h"
23#include "SynTree/Declaration.h"
24#include "SynTree/Type.h"
25#include "SynTree/Expression.h"
26#include "SynTree/Initializer.h"
27#include "SymTab/Mangler.h"
28#include "Common/ScopedMap.h"
29#include "ResolvExpr/typeops.h"
30
31namespace Tuples {
32        namespace {
33                class MemberTupleExpander : public Mutator {
34                public:
35                        typedef Mutator Parent;
36                        virtual Expression * mutate( UntypedMemberExpr * memberExpr );
37                };
38
39                class UniqueExprExpander : public GenPoly::DeclMutator {
40                public:
41                        typedef GenPoly::DeclMutator Parent;
42                        virtual Expression * mutate( UniqueExpr * unqExpr );
43                        std::map< int, ObjectDecl * > decls; // not vector, because order added may not be increasing order
44                };
45
46                class TupleAssignExpander : public Mutator {
47                public:
48                        typedef Mutator Parent;
49                        virtual Expression * mutate( TupleAssignExpr * tupleExpr );
50                };
51
52                class TupleTypeReplacer : public GenPoly::DeclMutator {
53                  public:
54                        typedef GenPoly::DeclMutator Parent;
55
56                        virtual Type * mutate( TupleType * tupleType );
57
58                        virtual CompoundStmt * mutate( CompoundStmt * stmt ) {
59                                typeMap.beginScope();
60                                stmt = Parent::mutate( stmt );
61                                typeMap.endScope();
62                                return stmt;
63                        }
64                  private:
65                        ScopedMap< std::string, StructDecl * > typeMap;
66                };
67
68                class TupleIndexExpander : public Mutator {
69                public:
70                        typedef Mutator Parent;
71                        virtual Expression * mutate( TupleIndexExpr * tupleExpr );
72                };
73
74                class TupleExprExpander : public Mutator {
75                public:
76                        typedef Mutator Parent;
77                        virtual Expression * mutate( TupleExpr * tupleExpr );
78                };
79        }
80
81        void expandMemberTuples( std::list< Declaration * > & translationUnit ) {
82                MemberTupleExpander expander;
83                mutateAll( translationUnit, expander );
84        }
85
86        void expandUniqueExpr( std::list< Declaration * > & translationUnit ) {
87                UniqueExprExpander unqExpander;
88                unqExpander.mutateDeclarationList( translationUnit );
89        }
90
91        void expandTuples( std::list< Declaration * > & translationUnit ) {
92                TupleAssignExpander assnExpander;
93                mutateAll( translationUnit, assnExpander );
94
95                TupleTypeReplacer replacer;
96                replacer.mutateDeclarationList( translationUnit );
97
98                TupleIndexExpander idxExpander;
99                mutateAll( translationUnit, idxExpander );
100
101                TupleExprExpander exprExpander;
102                mutateAll( translationUnit, exprExpander );
103        }
104
105        namespace {
106                /// given a expression representing the member and an expression representing the aggregate,
107                /// reconstructs a flattened UntypedMemberExpr with the right precedence
108                Expression * reconstructMemberExpr( Expression * member, UniqueExpr * aggr ) {
109                        if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( member ) ) {
110                                // construct a new UntypedMemberExpr with the correct structure , and recursively
111                                // expand that member expression.
112                                MemberTupleExpander expander;
113                                UntypedMemberExpr * newMemberExpr = new UntypedMemberExpr( memberExpr->get_member(), new UntypedMemberExpr( memberExpr->get_aggregate(), aggr->clone() ) );
114
115                                memberExpr->set_member(nullptr);
116                                memberExpr->set_aggregate(nullptr);
117                                delete memberExpr;
118                                return newMemberExpr->acceptMutator( expander );
119                        } else {
120                                // not a member expression, so there is nothing to do but attach and return
121                                return new UntypedMemberExpr( member, aggr->clone() );
122                        }
123                }
124        }
125
126        Expression * MemberTupleExpander::mutate( UntypedMemberExpr * memberExpr ) {
127                if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * > ( memberExpr->get_member() ) ) {
128                        UniqueExpr * unqExpr = new UniqueExpr( memberExpr->get_aggregate()->clone() );
129                        for ( Expression *& expr : tupleExpr->get_exprs() ) {
130                                expr = reconstructMemberExpr( expr, unqExpr );
131                        }
132                        delete unqExpr;
133                        return tupleExpr;
134                } else {
135                        return memberExpr;
136                }
137        }
138
139        Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) {
140                static UniqueName tempNamer( "_unq_expr_" );
141                unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) );
142                if ( ! decls.count( unqExpr->get_id() ) ) {
143                        // xxx - it's possible (likely?) that expressions can appear in the wrong order because of this. Need to ensure they're placed in the correct location.
144                        // xxx - is it possible to make the decl's type const?
145                        ObjectDecl * decl = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, unqExpr->get_result()->clone(), new SingleInit( unqExpr->get_expr()->clone() ) );
146                        decls[unqExpr->get_id()] = decl;
147                        addDeclaration( decl );
148                }
149                return new VariableExpr( decls[unqExpr->get_id()] );
150        }
151
152        Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) {
153                // xxx - Parent::mutate?
154                CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
155                std::list< Statement * > & stmts = compoundStmt->get_kids();
156                for ( ObjectDecl * obj : assnExpr->get_tempDecls() ) {
157                        stmts.push_back( new DeclStmt( noLabels, obj ) );
158                }
159                TupleExpr * tupleExpr = new TupleExpr( assnExpr->get_assigns() );
160                assert( tupleExpr->get_result() );
161                stmts.push_back( new ExprStmt( noLabels, tupleExpr ) );
162                assnExpr->get_tempDecls().clear();
163                assnExpr->get_assigns().clear();
164                delete assnExpr;
165                return new StmtExpr( compoundStmt );
166        }
167
168        Type * TupleTypeReplacer::mutate( TupleType * tupleType ) {
169                std::string mangleName = SymTab::Mangler::mangleType( tupleType );
170                TupleType * newType = safe_dynamic_cast< TupleType * > ( Parent::mutate( tupleType ) );
171                if ( ! typeMap.count( mangleName ) ) {
172                        // generate struct type to replace tuple type
173                        StructDecl * decl = new StructDecl( "_tuple_type_" + mangleName );
174                        decl->set_body( true );
175                        int cnt = 0;
176                        for ( Type * t : *newType ) {
177                                decl->get_members().push_back( new ObjectDecl( "field_"+std::to_string(++cnt), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) );
178                        }
179                        typeMap[mangleName] = decl;
180                        addDeclaration( decl );
181                }
182                Type::Qualifiers qualifiers = newType->get_qualifiers();
183                delete newType;
184                return new StructInstType( qualifiers, typeMap[mangleName] );
185        }
186
187        Expression * TupleIndexExpander::mutate( TupleIndexExpr * tupleExpr ) {
188                Expression * tuple = maybeMutate( tupleExpr->get_tuple(), *this );
189                assert( tuple );
190                tupleExpr->set_tuple( nullptr );
191                unsigned int idx = tupleExpr->get_index();
192                delete tupleExpr;
193
194                StructInstType * type = safe_dynamic_cast< StructInstType * >( tuple->get_result() );
195                StructDecl * structDecl = type->get_baseStruct();
196                assert( structDecl->get_members().size() > idx );
197                Declaration * member = *std::next(structDecl->get_members().begin(), idx);
198                return new MemberExpr( safe_dynamic_cast< DeclarationWithType * >( member ), tuple );
199        }
200
201        Expression * replaceTupleExpr( Type * result, const std::list< Expression * > & exprs ) {
202                if ( result->isVoid() ) {
203                        // void result - don't need to produce a value for cascading - just output a chain of comma exprs
204                        assert( ! exprs.empty() );
205                        std::list< Expression * >::const_iterator iter = exprs.begin();
206                        Expression * expr = *iter++;
207                        for ( ; iter != exprs.end(); ++iter ) {
208                                expr = new CommaExpr( expr, *iter );
209                        }
210                        return expr;
211                } else {
212                        // typed tuple expression - produce a compound literal which performs each of the expressions
213                        // as a distinct part of its initializer - the produced compound literal may be used as part of
214                        // another expression
215                        std::list< Initializer * > inits;
216                        for ( Expression * expr : exprs ) {
217                                inits.push_back( new SingleInit( expr ) );
218                        }
219                        return new CompoundLiteralExpr( result, new ListInit( inits ) );
220                }
221        }
222
223        Expression * TupleExprExpander::mutate( TupleExpr * tupleExpr ) {
224                // recursively expand sub-tuple-expressions
225                tupleExpr = safe_dynamic_cast<TupleExpr *>(Parent::mutate(tupleExpr));
226                Type * result = tupleExpr->get_result();
227                std::list< Expression * > exprs = tupleExpr->get_exprs();
228                assert( result );
229
230                // remove data from shell and delete it
231                tupleExpr->set_result( nullptr );
232                tupleExpr->get_exprs().clear();
233                delete tupleExpr;
234
235                return replaceTupleExpr( result, exprs );
236        }
237
238        Type * makeTupleType( const std::list< Expression * > & exprs ) {
239                // produce the TupleType which aggregates the types of the exprs
240                TupleType *tupleType = new TupleType( Type::Qualifiers(true, true, true, true, true, false) );
241                Type::Qualifiers &qualifiers = tupleType->get_qualifiers();
242                for ( Expression * expr : exprs ) {
243                        assert( expr->get_result() );
244                        if ( expr->get_result()->isVoid() ) {
245                                // if the type of any expr is void, the type of the entire tuple is void
246                                delete tupleType;
247                                return new VoidType( Type::Qualifiers() );
248                        }
249                        Type * type = expr->get_result()->clone();
250                        tupleType->get_types().push_back( type );
251                        // the qualifiers on the tuple type are the qualifiers that exist on all component types
252                        qualifiers &= type->get_qualifiers();
253                } // for
254                return tupleType;
255        }
256
257        namespace {
258                /// determines if impurity (read: side-effects) may exist in a piece of code. Currently gives a very crude approximation, wherein any function call expression means the code may be impure
259                class ImpurityDetector : public Visitor {
260                public:
261                        typedef Visitor Parent;
262                        virtual void visit( ApplicationExpr * appExpr ) { maybeImpure = true;   }
263                        virtual void visit( UntypedExpr * untypedExpr ) { maybeImpure = true; }
264                        bool maybeImpure = false;
265                };
266        } // namespace
267
268        bool maybeImpure( Expression * expr ) {
269                ImpurityDetector detector;
270                expr->accept( detector );
271                return detector.maybeImpure;
272        }
273} // namespace Tuples
274
275// Local Variables: //
276// tab-width: 4 //
277// mode: c++ //
278// compile-command: "make install" //
279// End: //
280
Note: See TracBrowser for help on using the repository browser.