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 : Wed Jun 21 17:35:04 2017 |
---|
13 | // Update Count : 19 |
---|
14 | // |
---|
15 | |
---|
16 | #include <stddef.h> // for size_t |
---|
17 | #include <cassert> // for assert |
---|
18 | #include <list> // for list |
---|
19 | |
---|
20 | #include "Common/PassVisitor.h" // for PassVisitor, WithDeclsToAdd, WithGu... |
---|
21 | #include "Common/ScopedMap.h" // for ScopedMap |
---|
22 | #include "Common/utility.h" // for CodeLocation |
---|
23 | #include "GenPoly/DeclMutator.h" // for DeclMutator |
---|
24 | #include "InitTweak/InitTweak.h" // for getFunction |
---|
25 | #include "Parser/LinkageSpec.h" // for Spec, C, Intrinsic |
---|
26 | #include "PassVisitor.h" // for mutateAll |
---|
27 | #include "SynTree/Constant.h" // for Constant |
---|
28 | #include "SynTree/Declaration.h" // for StructDecl, DeclarationWithType |
---|
29 | #include "SynTree/Expression.h" // for UntypedMemberExpr, Expression, Uniq... |
---|
30 | #include "SynTree/Label.h" // for operator==, Label |
---|
31 | #include "SynTree/Mutator.h" // for Mutator |
---|
32 | #include "SynTree/Type.h" // for Type, Type::Qualifiers, TupleType |
---|
33 | #include "SynTree/Visitor.h" // for Visitor |
---|
34 | |
---|
35 | class CompoundStmt; |
---|
36 | class TypeSubstitution; |
---|
37 | |
---|
38 | namespace Tuples { |
---|
39 | namespace { |
---|
40 | class MemberTupleExpander final : public Mutator { |
---|
41 | public: |
---|
42 | typedef Mutator Parent; |
---|
43 | using Parent::mutate; |
---|
44 | |
---|
45 | virtual Expression * mutate( UntypedMemberExpr * memberExpr ) override; |
---|
46 | }; |
---|
47 | |
---|
48 | class UniqueExprExpander final : public GenPoly::DeclMutator { |
---|
49 | public: |
---|
50 | typedef GenPoly::DeclMutator Parent; |
---|
51 | using Parent::mutate; |
---|
52 | |
---|
53 | virtual Expression * mutate( UniqueExpr * unqExpr ) override; |
---|
54 | |
---|
55 | std::map< int, Expression * > decls; // not vector, because order added may not be increasing order |
---|
56 | |
---|
57 | ~UniqueExprExpander() { |
---|
58 | for ( std::pair<const int, Expression *> & p : decls ) { |
---|
59 | delete p.second; |
---|
60 | } |
---|
61 | } |
---|
62 | }; |
---|
63 | |
---|
64 | class TupleAssignExpander : public Mutator { |
---|
65 | public: |
---|
66 | typedef Mutator Parent; |
---|
67 | using Parent::mutate; |
---|
68 | |
---|
69 | virtual Expression * mutate( TupleAssignExpr * tupleExpr ); |
---|
70 | }; |
---|
71 | |
---|
72 | struct TupleTypeReplacer : public WithDeclsToAdd, public WithGuards, public WithTypeSubstitution { |
---|
73 | Type * postmutate( TupleType * tupleType ); |
---|
74 | |
---|
75 | void premutate( CompoundStmt * ) { |
---|
76 | GuardScope( typeMap ); |
---|
77 | } |
---|
78 | private: |
---|
79 | ScopedMap< int, StructDecl * > typeMap; |
---|
80 | }; |
---|
81 | |
---|
82 | class TupleIndexExpander { |
---|
83 | public: |
---|
84 | Expression * postmutate( TupleIndexExpr * tupleExpr ); |
---|
85 | }; |
---|
86 | |
---|
87 | class TupleExprExpander final : public Mutator { |
---|
88 | public: |
---|
89 | typedef Mutator Parent; |
---|
90 | using Parent::mutate; |
---|
91 | |
---|
92 | virtual Expression * mutate( TupleExpr * tupleExpr ) override; |
---|
93 | }; |
---|
94 | } |
---|
95 | |
---|
96 | void expandMemberTuples( std::list< Declaration * > & translationUnit ) { |
---|
97 | MemberTupleExpander expander; |
---|
98 | mutateAll( translationUnit, expander ); |
---|
99 | } |
---|
100 | |
---|
101 | void expandUniqueExpr( std::list< Declaration * > & translationUnit ) { |
---|
102 | UniqueExprExpander unqExpander; |
---|
103 | unqExpander.mutateDeclarationList( translationUnit ); |
---|
104 | } |
---|
105 | |
---|
106 | void expandTuples( std::list< Declaration * > & translationUnit ) { |
---|
107 | TupleAssignExpander assnExpander; |
---|
108 | mutateAll( translationUnit, assnExpander ); |
---|
109 | |
---|
110 | PassVisitor<TupleTypeReplacer> replacer; |
---|
111 | mutateAll( translationUnit, replacer ); |
---|
112 | |
---|
113 | PassVisitor<TupleIndexExpander> idxExpander; |
---|
114 | mutateAll( translationUnit, idxExpander ); |
---|
115 | |
---|
116 | TupleExprExpander exprExpander; |
---|
117 | mutateAll( translationUnit, exprExpander ); |
---|
118 | } |
---|
119 | |
---|
120 | namespace { |
---|
121 | /// given a expression representing the member and an expression representing the aggregate, |
---|
122 | /// reconstructs a flattened UntypedMemberExpr with the right precedence |
---|
123 | Expression * reconstructMemberExpr( Expression * member, Expression * aggr, CodeLocation & loc ) { |
---|
124 | if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( member ) ) { |
---|
125 | // construct a new UntypedMemberExpr with the correct structure , and recursively |
---|
126 | // expand that member expression. |
---|
127 | MemberTupleExpander expander; |
---|
128 | UntypedMemberExpr * inner = new UntypedMemberExpr( memberExpr->get_aggregate(), aggr->clone() ); |
---|
129 | UntypedMemberExpr * newMemberExpr = new UntypedMemberExpr( memberExpr->get_member(), inner ); |
---|
130 | inner->location = newMemberExpr->location = loc; |
---|
131 | memberExpr->set_member(nullptr); |
---|
132 | memberExpr->set_aggregate(nullptr); |
---|
133 | delete memberExpr; |
---|
134 | return newMemberExpr->acceptMutator( expander ); |
---|
135 | } else { |
---|
136 | // not a member expression, so there is nothing to do but attach and return |
---|
137 | UntypedMemberExpr * newMemberExpr = new UntypedMemberExpr( member, aggr->clone() ); |
---|
138 | newMemberExpr->location = loc; |
---|
139 | return newMemberExpr; |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | Expression * MemberTupleExpander::mutate( UntypedMemberExpr * memberExpr ) { |
---|
145 | if ( UntypedTupleExpr * tupleExpr = dynamic_cast< UntypedTupleExpr * > ( memberExpr->get_member() ) ) { |
---|
146 | Expression * aggr = memberExpr->get_aggregate()->clone()->acceptMutator( *this ); |
---|
147 | // aggregate expressions which might be impure must be wrapped in unique expressions |
---|
148 | // xxx - if there's a member-tuple expression nested in the aggregate, this currently generates the wrong code if a UniqueExpr is not used, and it's purely an optimization to remove the UniqueExpr |
---|
149 | // if ( Tuples::maybeImpure( memberExpr->get_aggregate() ) ) aggr = new UniqueExpr( aggr ); |
---|
150 | aggr = new UniqueExpr( aggr ); |
---|
151 | for ( Expression *& expr : tupleExpr->get_exprs() ) { |
---|
152 | expr = reconstructMemberExpr( expr, aggr, memberExpr->location ); |
---|
153 | expr->location = memberExpr->location; |
---|
154 | } |
---|
155 | delete aggr; |
---|
156 | tupleExpr->location = memberExpr->location; |
---|
157 | return tupleExpr; |
---|
158 | } else { |
---|
159 | // there may be a tuple expr buried in the aggregate |
---|
160 | // xxx - this is a memory leak |
---|
161 | UntypedMemberExpr * newMemberExpr = new UntypedMemberExpr( memberExpr->get_member()->clone(), memberExpr->get_aggregate()->acceptMutator( *this ) ); |
---|
162 | newMemberExpr->location = memberExpr->location; |
---|
163 | return newMemberExpr; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) { |
---|
168 | unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) ); |
---|
169 | const int id = unqExpr->get_id(); |
---|
170 | |
---|
171 | // on first time visiting a unique expr with a particular ID, generate the expression that replaces all UniqueExprs with that ID, |
---|
172 | // and lookup on subsequent hits. This ensures that all unique exprs with the same ID reference the same variable. |
---|
173 | if ( ! decls.count( id ) ) { |
---|
174 | Expression * assignUnq; |
---|
175 | Expression * var = unqExpr->get_var(); |
---|
176 | if ( unqExpr->get_object() ) { |
---|
177 | // an object was generated to represent this unique expression -- it should be added to the list of declarations now |
---|
178 | addDeclaration( unqExpr->get_object() ); |
---|
179 | unqExpr->set_object( nullptr ); |
---|
180 | // steal the expr from the unqExpr |
---|
181 | assignUnq = UntypedExpr::createAssign( unqExpr->get_var()->clone(), unqExpr->get_expr() ); |
---|
182 | unqExpr->set_expr( nullptr ); |
---|
183 | } else { |
---|
184 | // steal the already generated assignment to var from the unqExpr - this has been generated by FixInit |
---|
185 | Expression * expr = unqExpr->get_expr(); |
---|
186 | CommaExpr * commaExpr = safe_dynamic_cast< CommaExpr * >( expr ); |
---|
187 | assignUnq = commaExpr->get_arg1(); |
---|
188 | commaExpr->set_arg1( nullptr ); |
---|
189 | } |
---|
190 | ObjectDecl * finished = new ObjectDecl( toString( "_unq", id, "_finished_" ), Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::Bool ), |
---|
191 | new SingleInit( new ConstantExpr( Constant::from_int( 0 ) ) ) ); |
---|
192 | addDeclaration( finished ); |
---|
193 | // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N)) |
---|
194 | // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code. |
---|
195 | Expression * assignFinished = UntypedExpr::createAssign( new VariableExpr(finished), new ConstantExpr( Constant::from_int( 1 ) ) ); |
---|
196 | ConditionalExpr * condExpr = new ConditionalExpr( new VariableExpr( finished ), var->clone(), |
---|
197 | new CommaExpr( new CommaExpr( assignUnq, assignFinished ), var->clone() ) ); |
---|
198 | condExpr->set_result( var->get_result()->clone() ); |
---|
199 | condExpr->set_env( maybeClone( unqExpr->get_env() ) ); |
---|
200 | decls[id] = condExpr; |
---|
201 | } |
---|
202 | delete unqExpr; |
---|
203 | return decls[id]->clone(); |
---|
204 | } |
---|
205 | |
---|
206 | Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) { |
---|
207 | assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) ); |
---|
208 | StmtExpr * ret = assnExpr->get_stmtExpr(); |
---|
209 | assnExpr->set_stmtExpr( nullptr ); |
---|
210 | // move env to StmtExpr |
---|
211 | ret->set_env( assnExpr->get_env() ); |
---|
212 | assnExpr->set_env( nullptr ); |
---|
213 | delete assnExpr; |
---|
214 | return ret; |
---|
215 | } |
---|
216 | |
---|
217 | Type * TupleTypeReplacer::postmutate( TupleType * tupleType ) { |
---|
218 | unsigned tupleSize = tupleType->size(); |
---|
219 | if ( ! typeMap.count( tupleSize ) ) { |
---|
220 | // generate struct type to replace tuple type based on the number of components in the tuple |
---|
221 | StructDecl * decl = new StructDecl( toString( "_tuple", tupleSize, "_" ) ); |
---|
222 | decl->set_body( true ); |
---|
223 | for ( size_t i = 0; i < tupleSize; ++i ) { |
---|
224 | TypeDecl * tyParam = new TypeDecl( toString( "tuple_param_", tupleSize, "_", i ), Type::StorageClasses(), nullptr, TypeDecl::Any ); |
---|
225 | decl->get_members().push_back( new ObjectDecl( toString("field_", i ), Type::StorageClasses(), LinkageSpec::C, nullptr, new TypeInstType( Type::Qualifiers(), tyParam->get_name(), tyParam ), nullptr ) ); |
---|
226 | decl->get_parameters().push_back( tyParam ); |
---|
227 | } |
---|
228 | if ( tupleSize == 0 ) { |
---|
229 | // empty structs are not standard C. Add a dummy field to empty tuples to silence warnings when a compound literal Tuple0 is created. |
---|
230 | decl->get_members().push_back( new ObjectDecl( "dummy", Type::StorageClasses(), LinkageSpec::C, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) ); |
---|
231 | } |
---|
232 | typeMap[tupleSize] = decl; |
---|
233 | declsToAddBefore.push_back( decl ); |
---|
234 | } |
---|
235 | Type::Qualifiers qualifiers = tupleType->get_qualifiers(); |
---|
236 | |
---|
237 | StructDecl * decl = typeMap[tupleSize]; |
---|
238 | StructInstType * newType = new StructInstType( qualifiers, decl ); |
---|
239 | for ( auto p : group_iterate( tupleType->get_types(), decl->get_parameters() ) ) { |
---|
240 | Type * t = std::get<0>(p); |
---|
241 | TypeDecl * td = std::get<1>(p); |
---|
242 | newType->get_parameters().push_back( new TypeExpr( t->clone() ) ); |
---|
243 | if ( env ) { |
---|
244 | // add bindings to the type environment. |
---|
245 | // xxx - This may not be sufficient, it may be necessary to rename type variables on StructInstType? |
---|
246 | env->add( td->get_name(), t->clone() ); |
---|
247 | } |
---|
248 | } |
---|
249 | delete tupleType; |
---|
250 | return newType; |
---|
251 | } |
---|
252 | |
---|
253 | Expression * TupleIndexExpander::postmutate( TupleIndexExpr * tupleExpr ) { |
---|
254 | Expression * tuple = tupleExpr->get_tuple(); |
---|
255 | assert( tuple ); |
---|
256 | tupleExpr->set_tuple( nullptr ); |
---|
257 | unsigned int idx = tupleExpr->get_index(); |
---|
258 | TypeSubstitution * env = tupleExpr->get_env(); |
---|
259 | tupleExpr->set_env( nullptr ); |
---|
260 | delete tupleExpr; |
---|
261 | |
---|
262 | StructInstType * type = safe_dynamic_cast< StructInstType * >( tuple->get_result() ); |
---|
263 | StructDecl * structDecl = type->get_baseStruct(); |
---|
264 | assert( structDecl->get_members().size() > idx ); |
---|
265 | Declaration * member = *std::next(structDecl->get_members().begin(), idx); |
---|
266 | MemberExpr * memExpr = new MemberExpr( safe_dynamic_cast< DeclarationWithType * >( member ), tuple ); |
---|
267 | memExpr->set_env( env ); |
---|
268 | return memExpr; |
---|
269 | } |
---|
270 | |
---|
271 | Expression * replaceTupleExpr( Type * result, const std::list< Expression * > & exprs, TypeSubstitution * env ) { |
---|
272 | if ( result->isVoid() ) { |
---|
273 | // void result - don't need to produce a value for cascading - just output a chain of comma exprs |
---|
274 | assert( ! exprs.empty() ); |
---|
275 | std::list< Expression * >::const_iterator iter = exprs.begin(); |
---|
276 | Expression * expr = new CastExpr( *iter++ ); |
---|
277 | for ( ; iter != exprs.end(); ++iter ) { |
---|
278 | expr = new CommaExpr( expr, new CastExpr( *iter ) ); |
---|
279 | } |
---|
280 | expr->set_env( env ); |
---|
281 | return expr; |
---|
282 | } else { |
---|
283 | // typed tuple expression - produce a compound literal which performs each of the expressions |
---|
284 | // as a distinct part of its initializer - the produced compound literal may be used as part of |
---|
285 | // another expression |
---|
286 | std::list< Initializer * > inits; |
---|
287 | for ( Expression * expr : exprs ) { |
---|
288 | inits.push_back( new SingleInit( expr ) ); |
---|
289 | } |
---|
290 | Expression * expr = new CompoundLiteralExpr( result, new ListInit( inits ) ); |
---|
291 | expr->set_env( env ); |
---|
292 | return expr; |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | Expression * TupleExprExpander::mutate( TupleExpr * tupleExpr ) { |
---|
297 | // recursively expand sub-tuple-expressions |
---|
298 | tupleExpr = safe_dynamic_cast<TupleExpr *>(Parent::mutate(tupleExpr)); |
---|
299 | Type * result = tupleExpr->get_result(); |
---|
300 | std::list< Expression * > exprs = tupleExpr->get_exprs(); |
---|
301 | assert( result ); |
---|
302 | TypeSubstitution * env = tupleExpr->get_env(); |
---|
303 | |
---|
304 | // remove data from shell and delete it |
---|
305 | tupleExpr->set_result( nullptr ); |
---|
306 | tupleExpr->get_exprs().clear(); |
---|
307 | tupleExpr->set_env( nullptr ); |
---|
308 | delete tupleExpr; |
---|
309 | |
---|
310 | return replaceTupleExpr( result, exprs, env ); |
---|
311 | } |
---|
312 | |
---|
313 | Type * makeTupleType( const std::list< Expression * > & exprs ) { |
---|
314 | // produce the TupleType which aggregates the types of the exprs |
---|
315 | std::list< Type * > types; |
---|
316 | Type::Qualifiers qualifiers( Type::Const | Type::Volatile | Type::Restrict | Type::Lvalue | Type::Atomic | Type::Mutex ); |
---|
317 | for ( Expression * expr : exprs ) { |
---|
318 | assert( expr->get_result() ); |
---|
319 | if ( expr->get_result()->isVoid() ) { |
---|
320 | // if the type of any expr is void, the type of the entire tuple is void |
---|
321 | return new VoidType( Type::Qualifiers() ); |
---|
322 | } |
---|
323 | Type * type = expr->get_result()->clone(); |
---|
324 | types.push_back( type ); |
---|
325 | // the qualifiers on the tuple type are the qualifiers that exist on all component types |
---|
326 | qualifiers &= type->get_qualifiers(); |
---|
327 | } // for |
---|
328 | if ( exprs.empty() ) qualifiers = Type::Qualifiers(); |
---|
329 | return new TupleType( qualifiers, types ); |
---|
330 | } |
---|
331 | |
---|
332 | TypeInstType * isTtype( Type * type ) { |
---|
333 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( type ) ) { |
---|
334 | if ( inst->get_baseType() && inst->get_baseType()->get_kind() == TypeDecl::Ttype ) { |
---|
335 | return inst; |
---|
336 | } |
---|
337 | } |
---|
338 | return nullptr; |
---|
339 | } |
---|
340 | |
---|
341 | namespace { |
---|
342 | /// 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 |
---|
343 | class ImpurityDetector : public Visitor { |
---|
344 | public: |
---|
345 | typedef Visitor Parent; |
---|
346 | virtual void visit( ApplicationExpr * appExpr ) { |
---|
347 | if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) { |
---|
348 | if ( function->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
349 | if ( function->get_name() == "*?" || function->get_name() == "?[?]" ) { |
---|
350 | // intrinsic dereference, subscript are pure, but need to recursively look for impurity |
---|
351 | Parent::visit( appExpr ); |
---|
352 | return; |
---|
353 | } |
---|
354 | } |
---|
355 | } |
---|
356 | maybeImpure = true; |
---|
357 | } |
---|
358 | virtual void visit( __attribute__((unused)) UntypedExpr * untypedExpr ) { maybeImpure = true; } |
---|
359 | bool maybeImpure = false; |
---|
360 | }; |
---|
361 | } // namespace |
---|
362 | |
---|
363 | bool maybeImpure( Expression * expr ) { |
---|
364 | ImpurityDetector detector; |
---|
365 | expr->accept( detector ); |
---|
366 | return detector.maybeImpure; |
---|
367 | } |
---|
368 | } // namespace Tuples |
---|
369 | |
---|
370 | // Local Variables: // |
---|
371 | // tab-width: 4 // |
---|
372 | // mode: c++ // |
---|
373 | // compile-command: "make install" // |
---|
374 | // End: // |
---|