Changeset 28e58fd for src/Tuples/TupleExpansion.cc
- Timestamp:
- Aug 25, 2017, 10:38:34 AM (8 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:
- 800d275
- Parents:
- af08051 (diff), 3eab308c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Tuples/TupleExpansion.cc
raf08051 r28e58fd 37 37 namespace Tuples { 38 38 namespace { 39 class MemberTupleExpander final : public Mutator { 40 public: 39 struct MemberTupleExpander final : public Mutator { 41 40 typedef Mutator Parent; 42 41 using Parent::mutate; … … 45 44 }; 46 45 47 class UniqueExprExpander final : public GenPoly::DeclMutator { 48 public: 49 typedef GenPoly::DeclMutator Parent; 50 using Parent::mutate; 51 52 virtual Expression * mutate( UniqueExpr * unqExpr ) override; 46 struct UniqueExprExpander final : public WithDeclsToAdd { 47 Expression * postmutate( UniqueExpr * unqExpr ); 53 48 54 49 std::map< int, Expression * > decls; // not vector, because order added may not be increasing order … … 61 56 }; 62 57 63 class TupleAssignExpander : public Mutator { 64 public: 65 typedef Mutator Parent; 66 using Parent::mutate; 67 68 virtual Expression * mutate( TupleAssignExpr * tupleExpr ); 58 struct TupleAssignExpander { 59 Expression * postmutate( TupleAssignExpr * tupleExpr ); 69 60 }; 70 61 … … 79 70 }; 80 71 81 class TupleIndexExpander { 82 public: 72 struct TupleIndexExpander { 83 73 Expression * postmutate( TupleIndexExpr * tupleExpr ); 84 74 }; 85 75 86 class TupleExprExpander final : public Mutator { 87 public: 88 typedef Mutator Parent; 89 using Parent::mutate; 90 91 virtual Expression * mutate( TupleExpr * tupleExpr ) override; 76 struct TupleExprExpander final { 77 Expression * postmutate( TupleExpr * tupleExpr ); 92 78 }; 93 79 } … … 99 85 100 86 void expandUniqueExpr( std::list< Declaration * > & translationUnit ) { 101 UniqueExprExpanderunqExpander;102 unqExpander.mutateDeclarationList( translationUnit);87 PassVisitor<UniqueExprExpander> unqExpander; 88 mutateAll( translationUnit, unqExpander ); 103 89 } 104 90 105 91 void expandTuples( std::list< Declaration * > & translationUnit ) { 106 TupleAssignExpanderassnExpander;92 PassVisitor<TupleAssignExpander> assnExpander; 107 93 mutateAll( translationUnit, assnExpander ); 108 94 … … 113 99 mutateAll( translationUnit, idxExpander ); 114 100 115 TupleExprExpanderexprExpander;101 PassVisitor<TupleExprExpander> exprExpander; 116 102 mutateAll( translationUnit, exprExpander ); 117 103 } … … 146 132 // aggregate expressions which might be impure must be wrapped in unique expressions 147 133 // 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 148 // if ( Tuples::maybeImpure ( memberExpr->get_aggregate() ) ) aggr = new UniqueExpr( aggr );134 // if ( Tuples::maybeImpureIgnoreUnique( memberExpr->get_aggregate() ) ) aggr = new UniqueExpr( aggr ); 149 135 aggr = new UniqueExpr( aggr ); 150 136 for ( Expression *& expr : tupleExpr->get_exprs() ) { … … 164 150 } 165 151 166 Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) { 167 unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) ); 152 Expression * UniqueExprExpander::postmutate( UniqueExpr * unqExpr ) { 168 153 const int id = unqExpr->get_id(); 169 154 … … 175 160 if ( unqExpr->get_object() ) { 176 161 // an object was generated to represent this unique expression -- it should be added to the list of declarations now 177 addDeclaration( unqExpr->get_object() );162 declsToAddBefore.push_back( unqExpr->get_object() ); 178 163 unqExpr->set_object( nullptr ); 179 164 // steal the expr from the unqExpr … … 189 174 ObjectDecl * finished = new ObjectDecl( toString( "_unq", id, "_finished_" ), Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::Bool ), 190 175 new SingleInit( new ConstantExpr( Constant::from_int( 0 ) ) ) ); 191 addDeclaration( finished );176 declsToAddBefore.push_back( finished ); 192 177 // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N)) 193 178 // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code. … … 203 188 } 204 189 205 Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) { 206 assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) ); 190 Expression * TupleAssignExpander::postmutate( TupleAssignExpr * assnExpr ) { 207 191 StmtExpr * ret = assnExpr->get_stmtExpr(); 208 192 assnExpr->set_stmtExpr( nullptr ); … … 238 222 for ( auto p : group_iterate( tupleType->get_types(), decl->get_parameters() ) ) { 239 223 Type * t = std::get<0>(p); 240 TypeDecl * td = std::get<1>(p);241 224 newType->get_parameters().push_back( new TypeExpr( t->clone() ) ); 242 if ( env ) {243 // add bindings to the type environment.244 // xxx - This may not be sufficient, it may be necessary to rename type variables on StructInstType?245 env->add( td->get_name(), t->clone() );246 }247 225 } 248 226 delete tupleType; … … 293 271 } 294 272 295 Expression * TupleExprExpander::mutate( TupleExpr * tupleExpr ) { 296 // recursively expand sub-tuple-expressions 297 tupleExpr = safe_dynamic_cast<TupleExpr *>(Parent::mutate(tupleExpr)); 273 Expression * TupleExprExpander::postmutate( TupleExpr * tupleExpr ) { 298 274 Type * result = tupleExpr->get_result(); 299 275 std::list< Expression * > exprs = tupleExpr->get_exprs(); … … 342 318 class ImpurityDetector : public Visitor { 343 319 public: 320 ImpurityDetector( bool ignoreUnique ) : ignoreUnique( ignoreUnique ) {} 321 344 322 typedef Visitor Parent; 345 323 virtual void visit( ApplicationExpr * appExpr ) { … … 355 333 maybeImpure = true; 356 334 } 357 virtual void visit( __attribute__((unused)) UntypedExpr * untypedExpr ) { maybeImpure = true; } 335 virtual void visit( UntypedExpr * ) { maybeImpure = true; } 336 virtual void visit( UniqueExpr * unq ) { 337 if ( ignoreUnique ) { 338 // bottom out at unique expression. 339 // The existence of a unique expression doesn't change the purity of an expression. 340 // That is, even if the wrapped expression is impure, the wrapper protects the rest of the expression. 341 return; 342 } 343 maybeAccept( unq->expr, *this ); 344 } 345 358 346 bool maybeImpure = false; 347 bool ignoreUnique; 359 348 }; 360 349 } // namespace 361 350 362 351 bool maybeImpure( Expression * expr ) { 363 ImpurityDetector detector; 352 ImpurityDetector detector( false ); 353 expr->accept( detector ); 354 return detector.maybeImpure; 355 } 356 357 bool maybeImpureIgnoreUnique( Expression * expr ) { 358 ImpurityDetector detector( true ); 364 359 expr->accept( detector ); 365 360 return detector.maybeImpure;
Note:
See TracChangeset
for help on using the changeset viewer.