Ignore:
Timestamp:
Aug 25, 2017, 10:38:34 AM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Tuples/TupleExpansion.cc

    raf08051 r28e58fd  
    3737namespace Tuples {
    3838        namespace {
    39                 class MemberTupleExpander final : public Mutator {
    40                 public:
     39                struct MemberTupleExpander final : public Mutator {
    4140                        typedef Mutator Parent;
    4241                        using Parent::mutate;
     
    4544                };
    4645
    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 );
    5348
    5449                        std::map< int, Expression * > decls; // not vector, because order added may not be increasing order
     
    6156                };
    6257
    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 );
    6960                };
    7061
     
    7970                };
    8071
    81                 class TupleIndexExpander {
    82                 public:
     72                struct TupleIndexExpander {
    8373                        Expression * postmutate( TupleIndexExpr * tupleExpr );
    8474                };
    8575
    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 );
    9278                };
    9379        }
     
    9985
    10086        void expandUniqueExpr( std::list< Declaration * > & translationUnit ) {
    101                 UniqueExprExpander unqExpander;
    102                 unqExpander.mutateDeclarationList( translationUnit );
     87                PassVisitor<UniqueExprExpander> unqExpander;
     88                mutateAll( translationUnit, unqExpander );
    10389        }
    10490
    10591        void expandTuples( std::list< Declaration * > & translationUnit ) {
    106                 TupleAssignExpander assnExpander;
     92                PassVisitor<TupleAssignExpander> assnExpander;
    10793                mutateAll( translationUnit, assnExpander );
    10894
     
    11399                mutateAll( translationUnit, idxExpander );
    114100
    115                 TupleExprExpander exprExpander;
     101                PassVisitor<TupleExprExpander> exprExpander;
    116102                mutateAll( translationUnit, exprExpander );
    117103        }
     
    146132                        // aggregate expressions which might be impure must be wrapped in unique expressions
    147133                        // 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 );
    149135                        aggr = new UniqueExpr( aggr );
    150136                        for ( Expression *& expr : tupleExpr->get_exprs() ) {
     
    164150        }
    165151
    166         Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) {
    167                 unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) );
     152        Expression * UniqueExprExpander::postmutate( UniqueExpr * unqExpr ) {
    168153                const int id = unqExpr->get_id();
    169154
     
    175160                        if ( unqExpr->get_object() ) {
    176161                                // 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() );
    178163                                unqExpr->set_object( nullptr );
    179164                                // steal the expr from the unqExpr
     
    189174                        ObjectDecl * finished = new ObjectDecl( toString( "_unq", id, "_finished_" ), Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::Bool ),
    190175                                                                                                        new SingleInit( new ConstantExpr( Constant::from_int( 0 ) ) ) );
    191                         addDeclaration( finished );
     176                        declsToAddBefore.push_back( finished );
    192177                        // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N))
    193178                        // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code.
     
    203188        }
    204189
    205         Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) {
    206                 assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) );
     190        Expression * TupleAssignExpander::postmutate( TupleAssignExpr * assnExpr ) {
    207191                StmtExpr * ret = assnExpr->get_stmtExpr();
    208192                assnExpr->set_stmtExpr( nullptr );
     
    238222                for ( auto p : group_iterate( tupleType->get_types(), decl->get_parameters() ) ) {
    239223                        Type * t = std::get<0>(p);
    240                         TypeDecl * td = std::get<1>(p);
    241224                        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                         }
    247225                }
    248226                delete tupleType;
     
    293271        }
    294272
    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 ) {
    298274                Type * result = tupleExpr->get_result();
    299275                std::list< Expression * > exprs = tupleExpr->get_exprs();
     
    342318                class ImpurityDetector : public Visitor {
    343319                public:
     320                        ImpurityDetector( bool ignoreUnique ) : ignoreUnique( ignoreUnique ) {}
     321
    344322                        typedef Visitor Parent;
    345323                        virtual void visit( ApplicationExpr * appExpr ) {
     
    355333                                maybeImpure = true;
    356334                        }
    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
    358346                        bool maybeImpure = false;
     347                        bool ignoreUnique;
    359348                };
    360349        } // namespace
    361350
    362351        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 );
    364359                expr->accept( detector );
    365360                return detector.maybeImpure;
Note: See TracChangeset for help on using the changeset viewer.