source: src/Tuples/TupleExpansion.cc@ 1ae06fa

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 1ae06fa was f6582243, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Fix member expressions in the InstantiateGeneric pass so that they correctly refer to the member from the instantiation

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