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