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