Changeset b7fd9daf


Ignore:
Timestamp:
Nov 10, 2021, 7:47:45 PM (8 months ago)
Author:
Fangren Yu <f37yu@…>
Branches:
enum, forall-pointer-decay, master
Children:
3249dd8
Parents:
1622af5 (diff), f95634e (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 'new-ast-unique-expr'

Location:
src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Convert.cpp

    r1622af5 rb7fd9daf  
    10411041
    10421042        const ast::Expr * visit( const ast::StmtExpr * node ) override final {
    1043                 auto stmts = node->stmts;
    1044                 // disable sharing between multiple StmtExprs explicitly.
    1045                 // this should no longer be true.
    1046 
    10471043                auto rslt = new StmtExpr(
    1048                         get<CompoundStmt>().accept1(stmts)
     1044                        get<CompoundStmt>().accept1(node->stmts)
    10491045                );
    10501046
    10511047                rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
    10521048                rslt->dtors       = get<Expression>().acceptL(node->dtors);
    1053                 if (node->resultExpr) {
    1054                         // this MUST be found by children visit
    1055                         rslt->resultExpr  = strict_dynamic_cast<ExprStmt *>(readonlyCache.at(node->resultExpr));
    1056                 }
     1049
     1050                // is this even used after convert?
     1051                //if (tmp->resultExpr) {
     1052                //      // this MUST be found by children visit
     1053                //      rslt->resultExpr  = strict_dynamic_cast<ExprStmt *>(readonlyCache.at(tmp->resultExpr));
     1054                //}
    10571055
    10581056                auto expr = visitBaseExpr( node, rslt );
  • src/AST/Copy.hpp

    r1622af5 rb7fd9daf  
    8888        }
    8989
     90        void postvisit( const StmtExpr * node ) {
     91                readonlyInsert( &node->resultExpr );
     92        }
     93
    9094        void postvisit( const MemberExpr * node ) {
    9195                readonlyInsert( &node->member );
     
    122126node_t * deepCopy( const node_t * localRoot ) {
    123127        Pass< DeepCopyCore > dc;
    124         node_t const * newRoot = localRoot->accept( dc );
     128        node_t const * newRoot = strict_dynamic_cast<node_t const*>(localRoot->accept( dc ));
    125129        dc.core.readonlyUpdates();
    126130        return const_cast< node_t * >( newRoot );
  • src/AST/Expr.hpp

    r1622af5 rb7fd9daf  
    767767
    768768/// An expression which must only be evaluated once
    769 class UniqueExpr final : public Expr {
     769class
     770UniqueExpr final : public Expr {
    770771        static unsigned long long nextId;
    771772public:
  • src/Tuples/TupleExpansionNew.cpp

    r1622af5 rb7fd9daf  
    2121                void previsit( const ast::UntypedMemberExpr * ) { visit_children = false; }
    2222        const ast::Expr * postvisit( const ast::UntypedMemberExpr * memberExpr );
     23        };
     24        struct UniqueExprExpander final : public ast::WithDeclsToAdd<> {
     25                const ast::Expr * postvisit( const ast::UniqueExpr * unqExpr );
     26                std::map< int, ast::ptr<ast::Expr> > decls; // not vector, because order added may not be increasing order
    2327        };
    2428} // namespace
     
    6670        }
    6771} // namespace
     72
     73void expandUniqueExpr( ast::TranslationUnit & translationUnit ) {
     74        ast::Pass< UniqueExprExpander >::run( translationUnit );
     75}
     76
     77namespace {
     78        const ast::Expr * UniqueExprExpander::postvisit( const ast::UniqueExpr * unqExpr ) {
     79                const CodeLocation loc = unqExpr->location;
     80                const int id = unqExpr->id;
     81
     82                // on first time visiting a unique expr with a particular ID, generate the expression that replaces all UniqueExprs with that ID,
     83                // and lookup on subsequent hits. This ensures that all unique exprs with the same ID reference the same variable.
     84                if ( ! decls.count( id ) ) {
     85                        ast::ptr< ast::Expr > assignUnq;
     86                        const ast::VariableExpr * var = unqExpr->var;
     87                        if ( unqExpr->object ) {
     88                                // an object was generated to represent this unique expression -- it should be added to the list of declarations now
     89                                declsToAddBefore.push_back( unqExpr->object.as< ast::Decl >() );
     90                                // deep copy required due to unresolved issues with UniqueExpr
     91                                assignUnq = ast::UntypedExpr::createAssign( loc, var, unqExpr->expr );
     92                        } else {
     93                                const auto commaExpr = unqExpr->expr.strict_as< ast::CommaExpr >();
     94                                assignUnq = commaExpr->arg1;
     95                        }
     96                        auto finished = new ast::ObjectDecl( loc, toString( "_unq", id, "_finished_" ), new ast::BasicType( ast::BasicType::Kind::Bool ),
     97                                new ast::SingleInit( loc, ast::ConstantExpr::from_int( loc, 0 ) ), {}, ast::Linkage::Cforall );
     98                        declsToAddBefore.push_back( finished );
     99                        // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N))
     100                        // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code.
     101                        auto assignFinished = ast::UntypedExpr::createAssign( loc, new ast::VariableExpr( loc, finished ),
     102                                ast::ConstantExpr::from_int( loc, 1 ) );
     103                        auto condExpr = new ast::ConditionalExpr( loc, new ast::VariableExpr( loc, finished ), var,
     104                                new ast::CommaExpr( loc, new ast::CommaExpr( loc, assignUnq, assignFinished ), var ) );
     105                        condExpr->result = var->result;
     106                        condExpr->env = unqExpr->env;
     107                        decls[id] = condExpr;
     108                }
     109                //delete unqExpr;
     110                return ast::deepCopy(decls[id].get());
     111        }
     112} // namespace
    68113} // namespace Tuples
  • src/Tuples/Tuples.h

    r1622af5 rb7fd9daf  
    4646        /// replaces UniqueExprs with a temporary variable and one call
    4747        void expandUniqueExpr( std::list< Declaration * > & translationUnit );
     48        void expandUniqueExpr( ast::TranslationUnit & translationUnit );
    4849
    4950        /// returns VoidType if any of the expressions have Voidtype, otherwise TupleType of the Expression result types
  • src/main.cc

    r1622af5 rb7fd9daf  
    381381                        PASS( "Fix Init", InitTweak::fix(transUnit, buildingLibrary()));
    382382
     383                        // fix ObjectDecl - replaces ConstructorInit nodes
     384                        if ( ctorinitp ) {
     385                                dump( move( transUnit ) );
     386                                return EXIT_SUCCESS;
     387                        } // if
     388
     389                        // Currently not working due to unresolved issues with UniqueExpr
     390                        PASS( "Expand Unique Expr", Tuples::expandUniqueExpr( transUnit ) ); // xxx - is this the right place for this? want to expand ASAP so tha, sequent passes don't need to worry about double-visiting a unique expr - needs to go after InitTweak::fix so that copy constructed return declarations are reused
    383391                        translationUnit = convert( move( transUnit ) );
    384392                } else {
     
    435443
    436444                        PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) );
     445
     446                        // fix ObjectDecl - replaces ConstructorInit nodes
     447                        if ( ctorinitp ) {
     448                                dump ( translationUnit );
     449                                return EXIT_SUCCESS;
     450                        } // if
     451
     452                        PASS( "Expand Unique Expr", Tuples::expandUniqueExpr( translationUnit ) ); // xxx - is this the right place for this? want to expand ASAP so tha, sequent passes don't need to worry about double-visiting a unique expr - needs to go after InitTweak::fix so that copy constructed return declarations are reused
    437453                }
    438 
    439                 // fix ObjectDecl - replaces ConstructorInit nodes
    440                 if ( ctorinitp ) {
    441                         dump ( translationUnit );
    442                         return EXIT_SUCCESS;
    443                 } // if
    444 
    445                 PASS( "Expand Unique Expr", Tuples::expandUniqueExpr( translationUnit ) ); // xxx - is this the right place for this? want to expand ASAP so tha, sequent passes don't need to worry about double-visiting a unique expr - needs to go after InitTweak::fix so that copy constructed return declarations are reused
    446454
    447455                PASS( "Translate Tries" , ControlStruct::translateTries( translationUnit ) );
Note: See TracChangeset for help on using the changeset viewer.