Changeset 9f10c4b8 for src/Tuples
- Timestamp:
- Aug 22, 2017, 5:31:27 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- d104b02
- Parents:
- 1ae06fa
- Location:
- src/Tuples
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Tuples/TupleExpansion.cc
r1ae06fa r9f10c4b8 41 41 }; 42 42 43 struct UniqueExprExpander final : public GenPoly::DeclMutator { 44 typedef GenPoly::DeclMutator Parent; 45 using Parent::mutate; 46 47 virtual Expression * mutate( UniqueExpr * unqExpr ) override; 43 struct UniqueExprExpander final : public WithDeclsToAdd { 44 Expression * postmutate( UniqueExpr * unqExpr ); 48 45 49 46 std::map< int, Expression * > decls; // not vector, because order added may not be increasing order … … 56 53 }; 57 54 58 struct TupleAssignExpander : public Mutator { 59 typedef Mutator Parent; 60 using Parent::mutate; 61 62 virtual Expression * mutate( TupleAssignExpr * tupleExpr ); 55 struct TupleAssignExpander { 56 Expression * postmutate( TupleAssignExpr * tupleExpr ); 63 57 }; 64 58 … … 77 71 }; 78 72 79 struct TupleExprExpander final : public Mutator { 80 typedef Mutator Parent; 81 using Parent::mutate; 82 83 virtual Expression * mutate( TupleExpr * tupleExpr ) override; 73 struct TupleExprExpander final { 74 Expression * postmutate( TupleExpr * tupleExpr ); 84 75 }; 85 76 } … … 91 82 92 83 void expandUniqueExpr( std::list< Declaration * > & translationUnit ) { 93 UniqueExprExpanderunqExpander;94 unqExpander.mutateDeclarationList( translationUnit);84 PassVisitor<UniqueExprExpander> unqExpander; 85 mutateAll( translationUnit, unqExpander ); 95 86 } 96 87 97 88 void expandTuples( std::list< Declaration * > & translationUnit ) { 98 TupleAssignExpanderassnExpander;89 PassVisitor<TupleAssignExpander> assnExpander; 99 90 mutateAll( translationUnit, assnExpander ); 100 91 … … 105 96 mutateAll( translationUnit, idxExpander ); 106 97 107 TupleExprExpanderexprExpander;98 PassVisitor<TupleExprExpander> exprExpander; 108 99 mutateAll( translationUnit, exprExpander ); 109 100 } … … 138 129 // aggregate expressions which might be impure must be wrapped in unique expressions 139 130 // 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 );131 // if ( Tuples::maybeImpureIgnoreUnique( memberExpr->get_aggregate() ) ) aggr = new UniqueExpr( aggr ); 141 132 aggr = new UniqueExpr( aggr ); 142 133 for ( Expression *& expr : tupleExpr->get_exprs() ) { … … 156 147 } 157 148 158 Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) { 159 unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) ); 149 Expression * UniqueExprExpander::postmutate( UniqueExpr * unqExpr ) { 160 150 const int id = unqExpr->get_id(); 161 151 … … 167 157 if ( unqExpr->get_object() ) { 168 158 // an object was generated to represent this unique expression -- it should be added to the list of declarations now 169 addDeclaration( unqExpr->get_object() );159 declsToAddBefore.push_back( unqExpr->get_object() ); 170 160 unqExpr->set_object( nullptr ); 171 161 // steal the expr from the unqExpr … … 181 171 ObjectDecl * finished = new ObjectDecl( toString( "_unq", id, "_finished_" ), Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::Bool ), 182 172 new SingleInit( new ConstantExpr( Constant::from_int( 0 ) ) ) ); 183 addDeclaration( finished );173 declsToAddBefore.push_back( finished ); 184 174 // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N)) 185 175 // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code. … … 195 185 } 196 186 197 Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) { 198 assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) ); 187 Expression * TupleAssignExpander::postmutate( TupleAssignExpr * assnExpr ) { 199 188 StmtExpr * ret = assnExpr->get_stmtExpr(); 200 189 assnExpr->set_stmtExpr( nullptr ); … … 279 268 } 280 269 281 Expression * TupleExprExpander::mutate( TupleExpr * tupleExpr ) { 282 // recursively expand sub-tuple-expressions 283 tupleExpr = safe_dynamic_cast<TupleExpr *>(Parent::mutate(tupleExpr)); 270 Expression * TupleExprExpander::postmutate( TupleExpr * tupleExpr ) { 284 271 Type * result = tupleExpr->get_result(); 285 272 std::list< Expression * > exprs = tupleExpr->get_exprs(); … … 328 315 class ImpurityDetector : public Visitor { 329 316 public: 317 ImpurityDetector( bool ignoreUnique ) : ignoreUnique( ignoreUnique ) {} 318 330 319 typedef Visitor Parent; 331 320 virtual void visit( ApplicationExpr * appExpr ) { … … 341 330 maybeImpure = true; 342 331 } 343 virtual void visit( __attribute__((unused)) UntypedExpr * untypedExpr ) { maybeImpure = true; } 332 virtual void visit( UntypedExpr * ) { maybeImpure = true; } 333 virtual void visit( UniqueExpr * unq ) { 334 if ( ignoreUnique ) { 335 // bottom out at unique expression. 336 // The existence of a unique expression doesn't change the purity of an expression. 337 // That is, even if the wrapped expression is impure, the wrapper protects the rest of the expression. 338 return; 339 } 340 maybeAccept( unq->expr, *this ); 341 } 342 344 343 bool maybeImpure = false; 344 bool ignoreUnique; 345 345 }; 346 346 } // namespace 347 347 348 348 bool maybeImpure( Expression * expr ) { 349 ImpurityDetector detector; 349 ImpurityDetector detector( false ); 350 expr->accept( detector ); 351 return detector.maybeImpure; 352 } 353 354 bool maybeImpureIgnoreUnique( Expression * expr ) { 355 ImpurityDetector detector( true ); 350 356 expr->accept( detector ); 351 357 return detector.maybeImpure; -
src/Tuples/Tuples.h
r1ae06fa r9f10c4b8 46 46 /// returns true if the expression may contain side-effects. 47 47 bool maybeImpure( Expression * expr ); 48 49 /// returns true if the expression may contain side-effect, ignoring the presence of unique expressions. 50 bool maybeImpureIgnoreUnique( Expression * expr ); 48 51 } // namespace Tuples 49 52
Note: See TracChangeset
for help on using the changeset viewer.