Changeset 141b786


Ignore:
Timestamp:
Nov 9, 2016, 2:21:05 PM (5 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
b726084
Parents:
23bb1b9
Message:

rework UniqueExpr?, handle UniqueExpr? in FixInit?, fix translation for UniqueExprs?, refactor explode functions and fix AddressExpr? distribution over exploded tuple exprs

Location:
src
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixInit.cc

    r23bb1b9 r141b786  
    6565                        static void resolveImplicitCalls( std::list< Declaration * > & translationUnit );
    6666
     67                        typedef SymTab::Indexer Parent;
     68                        using Parent::visit;
     69
    6770                        virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
     71                        virtual void visit( UniqueExpr * unqExpr );
    6872
    6973                        /// create and resolve ctor/dtor expression: fname(var, [cpArg])
     
    178182
    179183                        virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr );
     184                        virtual Expression * mutate( UniqueExpr * unqExpr );
    180185                };
    181186
     
    421426                void ResolveCopyCtors::visit( ImplicitCopyCtorExpr *impCpCtorExpr ) {
    422427                        CP_CTOR_PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; )
    423                         Visitor::visit( impCpCtorExpr );
     428                        Parent::visit( impCpCtorExpr );
    424429                        env = impCpCtorExpr->get_env(); // xxx - maybe we really should just have a PolyIndexer...
    425430
     
    455460                }
    456461
     462                void ResolveCopyCtors::visit( UniqueExpr * unqExpr ) {
     463                        static std::unordered_set< int > vars;
     464                        if ( vars.count( unqExpr->get_id() ) ) {
     465                                // xxx - hack to prevent double-handling of unique exprs, otherwise too many temporary variables and destructors are generated
     466                                return;
     467                        }
     468
     469                        Parent::visit( unqExpr );
     470                        // it should never be necessary to wrap a void-returning expression in a UniqueExpr - if this assumption changes, this needs to be rethought
     471                        assert( unqExpr->get_result() );
     472                        if ( ImplicitCopyCtorExpr * impCpCtorExpr = dynamic_cast<ImplicitCopyCtorExpr*>( unqExpr->get_expr() ) ) {
     473                                // note the variable used as the result from the call
     474                                assert( impCpCtorExpr->get_result() && impCpCtorExpr->get_returnDecls().size() == 1 );
     475                                unqExpr->set_var( new VariableExpr( impCpCtorExpr->get_returnDecls().front() ) );
     476                        } else {
     477                                // expr isn't a call expr, so create a new temporary variable to use to hold the value of the unique expression
     478                                unqExpr->set_object( new ObjectDecl( toString("_unq_expr_", unqExpr->get_id()), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, unqExpr->get_result()->clone(), nullptr ) );
     479                                unqExpr->set_var( new VariableExpr( unqExpr->get_object() ) );
     480                        }
     481                        vars.insert( unqExpr->get_id() );
     482                }
     483
    457484
    458485                Expression * FixCopyCtors::mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) {
     
    526553                                return callExpr;
    527554                        } // if
     555                }
     556
     557                Expression * FixCopyCtors::mutate( UniqueExpr * unqExpr ) {
     558                        static std::unordered_map< int, UniqueExpr * > unqMap;
     559                        static std::unordered_set< int > addDeref;
     560                        // has to be done to clean up ImplicitCopyCtorExpr nodes, even when this node was skipped in previous passes
     561                        unqExpr = safe_dynamic_cast< UniqueExpr * >( Parent::mutate( unqExpr ) );
     562                        if ( unqMap.count( unqExpr->get_id() ) ) {
     563                                // take data from other UniqueExpr to ensure consistency
     564                                delete unqExpr->get_expr();
     565                                unqExpr->set_expr( unqMap[unqExpr->get_id()]->get_expr()->clone() );
     566                                delete unqExpr->get_result();
     567                                unqExpr->set_result( maybeClone( unqExpr->get_expr()->get_result() ) );
     568                                if ( addDeref.count( unqExpr->get_id() ) ) {
     569                                        // other UniqueExpr was dereferenced because it was an lvalue return, so this one should be too
     570                                        return UntypedExpr::createDeref( unqExpr );
     571                                }
     572                                return unqExpr;
     573                        }
     574                        unqMap[unqExpr->get_id()] = unqExpr;
     575                        if ( UntypedExpr * deref = dynamic_cast< UntypedExpr * >( unqExpr->get_expr() ) ) {
     576                                // unique expression is now a dereference, because the inner expression is an lvalue returning function call.
     577                                // Normalize the expression by dereferencing the unique expression, rather than the inner expression
     578                                // (i.e. move the dereference out a level)
     579                                assert( getFunctionName( deref ) == "*?" );
     580                                unqExpr->set_expr( getCallArg( deref, 0 ) );
     581                                getCallArg( deref, 0 ) = unqExpr;
     582                                addDeref.insert( unqExpr->get_id() );
     583                                return deref;
     584                        }
     585                        return unqExpr;
    528586                }
    529587
  • src/Makefile.in

    r23bb1b9 r141b786  
    189189        SynTree/driver_cfa_cpp-TypeSubstitution.$(OBJEXT) \
    190190        SynTree/driver_cfa_cpp-Attribute.$(OBJEXT) \
     191        SynTree/driver_cfa_cpp-VarExprReplacer.$(OBJEXT) \
    191192        Tuples/driver_cfa_cpp-TupleAssignment.$(OBJEXT) \
    192         Tuples/driver_cfa_cpp-TupleExpansion.$(OBJEXT)
     193        Tuples/driver_cfa_cpp-TupleExpansion.$(OBJEXT) \
     194        Tuples/driver_cfa_cpp-Explode.$(OBJEXT)
    193195am_driver_cfa_cpp_OBJECTS = $(am__objects_1)
    194196driver_cfa_cpp_OBJECTS = $(am_driver_cfa_cpp_OBJECTS)
     
    403405        SynTree/Initializer.cc SynTree/Visitor.cc SynTree/Mutator.cc \
    404406        SynTree/AddStmtVisitor.cc SynTree/TypeSubstitution.cc \
    405         SynTree/Attribute.cc Tuples/TupleAssignment.cc \
    406         Tuples/TupleExpansion.cc
     407        SynTree/Attribute.cc SynTree/VarExprReplacer.cc \
     408        Tuples/TupleAssignment.cc Tuples/TupleExpansion.cc \
     409        Tuples/Explode.cc
    407410MAINTAINERCLEANFILES = Parser/parser.output ${libdir}/${notdir \
    408411        ${cfa_cpplib_PROGRAMS}}
     
    764767SynTree/driver_cfa_cpp-Attribute.$(OBJEXT): SynTree/$(am__dirstamp) \
    765768        SynTree/$(DEPDIR)/$(am__dirstamp)
     769SynTree/driver_cfa_cpp-VarExprReplacer.$(OBJEXT):  \
     770        SynTree/$(am__dirstamp) SynTree/$(DEPDIR)/$(am__dirstamp)
    766771Tuples/$(am__dirstamp):
    767772        @$(MKDIR_P) Tuples
     
    774779Tuples/driver_cfa_cpp-TupleExpansion.$(OBJEXT):  \
    775780        Tuples/$(am__dirstamp) Tuples/$(DEPDIR)/$(am__dirstamp)
     781Tuples/driver_cfa_cpp-Explode.$(OBJEXT): Tuples/$(am__dirstamp) \
     782        Tuples/$(DEPDIR)/$(am__dirstamp)
    776783driver/$(am__dirstamp):
    777784        @$(MKDIR_P) driver
     
    878885        -rm -f SynTree/driver_cfa_cpp-TypeofType.$(OBJEXT)
    879886        -rm -f SynTree/driver_cfa_cpp-VarArgsType.$(OBJEXT)
     887        -rm -f SynTree/driver_cfa_cpp-VarExprReplacer.$(OBJEXT)
    880888        -rm -f SynTree/driver_cfa_cpp-Visitor.$(OBJEXT)
    881889        -rm -f SynTree/driver_cfa_cpp-VoidType.$(OBJEXT)
    882890        -rm -f SynTree/driver_cfa_cpp-ZeroOneType.$(OBJEXT)
     891        -rm -f Tuples/driver_cfa_cpp-Explode.$(OBJEXT)
    883892        -rm -f Tuples/driver_cfa_cpp-TupleAssignment.$(OBJEXT)
    884893        -rm -f Tuples/driver_cfa_cpp-TupleExpansion.$(OBJEXT)
     
    984993@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-TypeofType.Po@am__quote@
    985994@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-VarArgsType.Po@am__quote@
     995@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-VarExprReplacer.Po@am__quote@
    986996@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-Visitor.Po@am__quote@
    987997@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-VoidType.Po@am__quote@
    988998@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-ZeroOneType.Po@am__quote@
     999@AMDEP_TRUE@@am__include@ @am__quote@Tuples/$(DEPDIR)/driver_cfa_cpp-Explode.Po@am__quote@
    9891000@AMDEP_TRUE@@am__include@ @am__quote@Tuples/$(DEPDIR)/driver_cfa_cpp-TupleAssignment.Po@am__quote@
    9901001@AMDEP_TRUE@@am__include@ @am__quote@Tuples/$(DEPDIR)/driver_cfa_cpp-TupleExpansion.Po@am__quote@
     
    24062417@am__fastdepCXX_FALSE@  $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o SynTree/driver_cfa_cpp-Attribute.obj `if test -f 'SynTree/Attribute.cc'; then $(CYGPATH_W) 'SynTree/Attribute.cc'; else $(CYGPATH_W) '$(srcdir)/SynTree/Attribute.cc'; fi`
    24072418
     2419SynTree/driver_cfa_cpp-VarExprReplacer.o: SynTree/VarExprReplacer.cc
     2420@am__fastdepCXX_TRUE@   $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT SynTree/driver_cfa_cpp-VarExprReplacer.o -MD -MP -MF SynTree/$(DEPDIR)/driver_cfa_cpp-VarExprReplacer.Tpo -c -o SynTree/driver_cfa_cpp-VarExprReplacer.o `test -f 'SynTree/VarExprReplacer.cc' || echo '$(srcdir)/'`SynTree/VarExprReplacer.cc
     2421@am__fastdepCXX_TRUE@   $(AM_V_at)$(am__mv) SynTree/$(DEPDIR)/driver_cfa_cpp-VarExprReplacer.Tpo SynTree/$(DEPDIR)/driver_cfa_cpp-VarExprReplacer.Po
     2422@AMDEP_TRUE@@am__fastdepCXX_FALSE@      $(AM_V_CXX)source='SynTree/VarExprReplacer.cc' object='SynTree/driver_cfa_cpp-VarExprReplacer.o' libtool=no @AMDEPBACKSLASH@
     2423@AMDEP_TRUE@@am__fastdepCXX_FALSE@      DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     2424@am__fastdepCXX_FALSE@  $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o SynTree/driver_cfa_cpp-VarExprReplacer.o `test -f 'SynTree/VarExprReplacer.cc' || echo '$(srcdir)/'`SynTree/VarExprReplacer.cc
     2425
     2426SynTree/driver_cfa_cpp-VarExprReplacer.obj: SynTree/VarExprReplacer.cc
     2427@am__fastdepCXX_TRUE@   $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT SynTree/driver_cfa_cpp-VarExprReplacer.obj -MD -MP -MF SynTree/$(DEPDIR)/driver_cfa_cpp-VarExprReplacer.Tpo -c -o SynTree/driver_cfa_cpp-VarExprReplacer.obj `if test -f 'SynTree/VarExprReplacer.cc'; then $(CYGPATH_W) 'SynTree/VarExprReplacer.cc'; else $(CYGPATH_W) '$(srcdir)/SynTree/VarExprReplacer.cc'; fi`
     2428@am__fastdepCXX_TRUE@   $(AM_V_at)$(am__mv) SynTree/$(DEPDIR)/driver_cfa_cpp-VarExprReplacer.Tpo SynTree/$(DEPDIR)/driver_cfa_cpp-VarExprReplacer.Po
     2429@AMDEP_TRUE@@am__fastdepCXX_FALSE@      $(AM_V_CXX)source='SynTree/VarExprReplacer.cc' object='SynTree/driver_cfa_cpp-VarExprReplacer.obj' libtool=no @AMDEPBACKSLASH@
     2430@AMDEP_TRUE@@am__fastdepCXX_FALSE@      DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     2431@am__fastdepCXX_FALSE@  $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o SynTree/driver_cfa_cpp-VarExprReplacer.obj `if test -f 'SynTree/VarExprReplacer.cc'; then $(CYGPATH_W) 'SynTree/VarExprReplacer.cc'; else $(CYGPATH_W) '$(srcdir)/SynTree/VarExprReplacer.cc'; fi`
     2432
    24082433Tuples/driver_cfa_cpp-TupleAssignment.o: Tuples/TupleAssignment.cc
    24092434@am__fastdepCXX_TRUE@   $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT Tuples/driver_cfa_cpp-TupleAssignment.o -MD -MP -MF Tuples/$(DEPDIR)/driver_cfa_cpp-TupleAssignment.Tpo -c -o Tuples/driver_cfa_cpp-TupleAssignment.o `test -f 'Tuples/TupleAssignment.cc' || echo '$(srcdir)/'`Tuples/TupleAssignment.cc
     
    24332458@AMDEP_TRUE@@am__fastdepCXX_FALSE@      DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
    24342459@am__fastdepCXX_FALSE@  $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o Tuples/driver_cfa_cpp-TupleExpansion.obj `if test -f 'Tuples/TupleExpansion.cc'; then $(CYGPATH_W) 'Tuples/TupleExpansion.cc'; else $(CYGPATH_W) '$(srcdir)/Tuples/TupleExpansion.cc'; fi`
     2460
     2461Tuples/driver_cfa_cpp-Explode.o: Tuples/Explode.cc
     2462@am__fastdepCXX_TRUE@   $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT Tuples/driver_cfa_cpp-Explode.o -MD -MP -MF Tuples/$(DEPDIR)/driver_cfa_cpp-Explode.Tpo -c -o Tuples/driver_cfa_cpp-Explode.o `test -f 'Tuples/Explode.cc' || echo '$(srcdir)/'`Tuples/Explode.cc
     2463@am__fastdepCXX_TRUE@   $(AM_V_at)$(am__mv) Tuples/$(DEPDIR)/driver_cfa_cpp-Explode.Tpo Tuples/$(DEPDIR)/driver_cfa_cpp-Explode.Po
     2464@AMDEP_TRUE@@am__fastdepCXX_FALSE@      $(AM_V_CXX)source='Tuples/Explode.cc' object='Tuples/driver_cfa_cpp-Explode.o' libtool=no @AMDEPBACKSLASH@
     2465@AMDEP_TRUE@@am__fastdepCXX_FALSE@      DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     2466@am__fastdepCXX_FALSE@  $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o Tuples/driver_cfa_cpp-Explode.o `test -f 'Tuples/Explode.cc' || echo '$(srcdir)/'`Tuples/Explode.cc
     2467
     2468Tuples/driver_cfa_cpp-Explode.obj: Tuples/Explode.cc
     2469@am__fastdepCXX_TRUE@   $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT Tuples/driver_cfa_cpp-Explode.obj -MD -MP -MF Tuples/$(DEPDIR)/driver_cfa_cpp-Explode.Tpo -c -o Tuples/driver_cfa_cpp-Explode.obj `if test -f 'Tuples/Explode.cc'; then $(CYGPATH_W) 'Tuples/Explode.cc'; else $(CYGPATH_W) '$(srcdir)/Tuples/Explode.cc'; fi`
     2470@am__fastdepCXX_TRUE@   $(AM_V_at)$(am__mv) Tuples/$(DEPDIR)/driver_cfa_cpp-Explode.Tpo Tuples/$(DEPDIR)/driver_cfa_cpp-Explode.Po
     2471@AMDEP_TRUE@@am__fastdepCXX_FALSE@      $(AM_V_CXX)source='Tuples/Explode.cc' object='Tuples/driver_cfa_cpp-Explode.obj' libtool=no @AMDEPBACKSLASH@
     2472@AMDEP_TRUE@@am__fastdepCXX_FALSE@      DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     2473@am__fastdepCXX_FALSE@  $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o Tuples/driver_cfa_cpp-Explode.obj `if test -f 'Tuples/Explode.cc'; then $(CYGPATH_W) 'Tuples/Explode.cc'; else $(CYGPATH_W) '$(srcdir)/Tuples/Explode.cc'; fi`
    24352474
    24362475.ll.cc:
  • src/ResolvExpr/AlternativeFinder.cc

    r23bb1b9 r141b786  
    3939#include "SymTab/Validate.h"
    4040#include "Tuples/Tuples.h"
     41#include "Tuples/Explode.h"
    4142#include "Common/utility.h"
    4243#include "InitTweak/InitTweak.h"
     
    240241                                } // if
    241242                        } // if
     243                } else if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( member ) ) {
     244                        // xxx - temporary hack until 0/1 are int constants
     245                        if ( nameExpr->get_name() == "0" || nameExpr->get_name() == "1" ) {
     246                                std::stringstream ss( nameExpr->get_name() );
     247                                int val;
     248                                ss >> val;
     249                                alternatives.push_back( Alternative( new TupleIndexExpr( expr->clone(), val ), env, newCost ) );
     250                        }
    242251                } // if
    243252        }
     
    10571066
    10581067        void AlternativeFinder::visit( UniqueExpr *unqExpr ) {
    1059                 // this won't work because the unqExprs wont share an expression anymore...
    10601068                AlternativeFinder finder( indexer, env );
    10611069                finder.findWithAdjustment( unqExpr->get_expr() );
    10621070                for ( Alternative & alt : finder.alternatives ) {
    1063                         // xxx - attach a resolved ConstructorInit node?
    1064                         // xxx - is it possible to make the objDecl's type const?
    1065                         static UniqueName tempNamer( "_unq_expr_" );
    1066                         ObjectDecl * objDecl = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, alt.expr->get_result()->clone(), nullptr );
    1067                         // must be done on two lines because genCtorInit accesses objDecl's fields
    1068                         objDecl->set_init( InitTweak::genCtorInit( objDecl ) );
    1069 
     1071                        // ensure that the id is passed on to the UniqueExpr alternative so that the expressions are "linked"
    10701072                        UniqueExpr * newUnqExpr = new UniqueExpr( alt.expr->clone(), unqExpr->get_id() );
    1071                         newUnqExpr->set_object( objDecl );
    1072 
    1073                         resolveObject( indexer, objDecl );
    1074 
    1075                         alternatives.push_back( Alternative( newUnqExpr, env, Cost::zero ) );
     1073                        alternatives.push_back( Alternative( newUnqExpr, alt.env, alt.cost ) );
    10761074                }
    10771075        }
  • src/SynTree/Expression.cc

    r23bb1b9 r141b786  
    609609
    610610long long UniqueExpr::count = 0;
    611 UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( new Expression* ), object( new ObjectDecl* ), id( idVal ) {
     611UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) {
     612        assert( expr );
    612613        assert( count != -1 );
    613614        if ( id == -1 ) id = count++;
    614         set_expr( expr );
    615         assert( expr );
    616615        if ( expr->get_result() ) {
    617616                set_result( expr->get_result()->clone() );
    618617        }
    619         set_object( nullptr );
    620 }
    621 UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( other.expr ), object( other.object ), id( other.id ) {
     618}
     619UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) {
    622620}
    623621UniqueExpr::~UniqueExpr() {
    624         if ( expr.unique() ) {
    625                 delete *expr;
    626         }
    627         if ( object.unique() ) {
    628                 delete *object;
    629         }
     622        delete expr;
     623        delete object;
     624        delete var;
    630625}
    631626void UniqueExpr::print( std::ostream &os, int indent ) const {
  • src/SynTree/Expression.h

    r23bb1b9 r141b786  
    742742        ~UniqueExpr();
    743743
    744         Expression * get_expr() const { return *expr; }
    745         UniqueExpr * set_expr( Expression * newValue ) { *expr = newValue; return this; }
    746 
    747         ObjectDecl * get_object() const { return *object; }
    748         UniqueExpr * set_object( ObjectDecl * newValue ) { *object = newValue; return this; }
     744        Expression * get_expr() const { return expr; }
     745        UniqueExpr * set_expr( Expression * newValue ) { expr = newValue; return this; }
     746
     747        ObjectDecl * get_object() const { return object; }
     748        UniqueExpr * set_object( ObjectDecl * newValue ) { object = newValue; return this; }
     749
     750        VariableExpr * get_var() const { return var; }
     751        UniqueExpr * set_var( VariableExpr * newValue ) { var = newValue; return this; }
    749752
    750753        int get_id() const { return id; }
     
    755758        virtual void print( std::ostream &os, int indent = 0 ) const;
    756759private:
    757         std::shared_ptr< Expression * > expr;
    758         std::shared_ptr< ObjectDecl * > object;
     760        Expression * expr;
     761        ObjectDecl * object;
     762        VariableExpr * var;
    759763        int id;
    760764        static long long count;
  • src/Tuples/TupleAssignment.cc

    r23bb1b9 r141b786  
    2020#include "SynTree/Initializer.h"
    2121#include "Tuples.h"
     22#include "Explode.h"
    2223#include "Common/SemanticError.h"
    2324#include "InitTweak/InitTweak.h"
  • src/Tuples/TupleExpansion.cc

    r23bb1b9 r141b786  
    4141                public:
    4242                        typedef GenPoly::DeclMutator Parent;
     43
    4344                        virtual Expression * mutate( UniqueExpr * unqExpr );
    44                         std::map< int, ObjectDecl * > decls; // not vector, because order added may not be increasing order
     45
     46                        std::map< int, Expression * > decls; // not vector, because order added may not be increasing order
     47
     48                        ~UniqueExprExpander() {
     49                                for ( std::pair<const int, Expression *> & p : decls ) {
     50                                        delete p.second;
     51                                }
     52                        }
    4553                };
    4654
     
    107115                /// given a expression representing the member and an expression representing the aggregate,
    108116                /// reconstructs a flattened UntypedMemberExpr with the right precedence
    109                 Expression * reconstructMemberExpr( Expression * member, UniqueExpr * aggr ) {
     117                Expression * reconstructMemberExpr( Expression * member, Expression * aggr ) {
    110118                        if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( member ) ) {
    111119                                // construct a new UntypedMemberExpr with the correct structure , and recursively
     
    127135        Expression * MemberTupleExpander::mutate( UntypedMemberExpr * memberExpr ) {
    128136                if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * > ( memberExpr->get_member() ) ) {
    129                         UniqueExpr * unqExpr = new UniqueExpr( memberExpr->get_aggregate()->clone() );
     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 );
    130142                        for ( Expression *& expr : tupleExpr->get_exprs() ) {
    131                                 expr = reconstructMemberExpr( expr, unqExpr );
    132                         }
    133                         delete unqExpr;
     143                                expr = reconstructMemberExpr( expr, aggr );
     144                        }
     145                        delete aggr;
    134146                        return tupleExpr;
    135147                } else {
     
    142154        Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) {
    143155                unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) );
    144                 if ( ! decls.count( unqExpr->get_id() ) ) {
    145                         // xxx - it's possible (likely?) that expressions can appear in the wrong order because of this. Need to ensure they're placed in the correct location.
    146 
    147                         // xxx - this doesn't work, because it would need to be placed after fixInit, but fixInit doesn't know (and shouldn't have to know) about the existance of UniqueExprs - i.e. it will visit them twice
    148                         // need to construct/destruct unique exprs in general - maybe it's not worth it and fixInit should handle UniqueExpr explicitly?
    149                         // currently, tmp is being destructed before unqExpr is used, which suggests there should be a separate lifetime for unqExpr from the tmp_ret
    150 
    151                         // if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( unqExpr->get_expr() ) ) {
    152                         //      if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( commaExpr->get_arg2() ) ) {
    153                         //              // steal existing decl from expr
    154                         //              if ( ObjectDecl * decl = dynamic_cast< ObjectDecl * >( varExpr->get_var() ) ) {
    155                         //                      decls[unqExpr->get_id()] = decl;
    156                         //                      return unqExpr->get_expr()->clone();
    157                         //              }
    158                         //      }
    159                         // }
    160 
    161                         ObjectDecl * objDecl = unqExpr->get_object();
    162                         unqExpr->set_object( nullptr );
    163                         decls[unqExpr->get_id()] = objDecl;
    164                         addDeclaration( objDecl );
    165                 }
    166                 return new VariableExpr( decls[unqExpr->get_id()] );
     156                const int id = unqExpr->get_id();
     157
     158                // on first time visiting a unique expr with a particular ID, generate the expression that replaces all UniqueExprs with that ID,
     159                // and lookup on subsequent hits. This ensures that all unique exprs with the same ID reference the same variable.
     160                if ( ! decls.count( id ) ) {
     161                        Expression * assignUnq;
     162                        Expression * var = unqExpr->get_var();
     163                        if ( unqExpr->get_object() ) {
     164                                // an object was generated to represent this unique expression -- it should be added to the list of declarations now
     165                                addDeclaration( unqExpr->get_object() );
     166                                unqExpr->set_object( nullptr );
     167                                // steal the expr from the unqExpr
     168                                assignUnq = UntypedExpr::createAssign( unqExpr->get_var()->clone(), unqExpr->get_expr() );
     169                                unqExpr->set_expr( nullptr );
     170                        } else {
     171                                // steal the already generated assignment to var from the unqExpr - this has been generated by FixInit
     172                                Expression * expr = unqExpr->get_expr();
     173                                CommaExpr * commaExpr = safe_dynamic_cast< CommaExpr * >( expr );
     174                                assignUnq = commaExpr->get_arg1();
     175                                commaExpr->set_arg1( nullptr );
     176                        }
     177                        BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool );
     178                        ObjectDecl * finished = new ObjectDecl( toString( "_unq_expr_finished_", id ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::Bool ), new SingleInit( new ConstantExpr( Constant( boolType->clone(), "0" ) ), noDesignators ) );
     179                        addDeclaration( finished );
     180                        // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N))
     181                        // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code.
     182                        Expression * assignFinished = UntypedExpr::createAssign( new VariableExpr(finished), new ConstantExpr( Constant( boolType->clone(), "1" ) ) );
     183                        ConditionalExpr * condExpr = new ConditionalExpr( new VariableExpr( finished ), var->clone(),
     184                                new CommaExpr( new CommaExpr( assignUnq, assignFinished ), var->clone() ) );
     185                        condExpr->set_result( var->get_result()->clone() );
     186                        decls[id] = condExpr;
     187                }
     188                delete unqExpr;
     189                return decls[id]->clone();
    167190        }
    168191
    169192        Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) {
    170                 // xxx - Parent::mutate?
     193                assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) );
    171194                CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
    172195                std::list< Statement * > & stmts = compoundStmt->get_kids();
     
    192215                        int cnt = 0;
    193216                        for ( Type * t : *newType ) {
    194                                 decl->get_members().push_back( new ObjectDecl( "field_"+std::to_string(++cnt), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) );
     217                                decl->get_members().push_back( new ObjectDecl( toString("field_", cnt++), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, t->clone(), nullptr ) );
    195218                        }
    196219                        typeMap[mangleName] = decl;
  • src/Tuples/Tuples.h

    r23bb1b9 r141b786  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon May 18 15:04:02 2015
    13 // Update Count     : 2
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Nov 9 13:17:58 2016
     13// Update Count     : 15
    1414//
    1515
     
    1818
    1919#include <string>
    20 #include <vector>
    21 #include "ResolvExpr/AlternativeFinder.h"
    22 #include "ResolvExpr/Resolver.h"
    2320
    2421#include "SynTree/Expression.h"
    2522#include "SynTree/Declaration.h"
    2623#include "SynTree/Type.h"
     24
     25#include "ResolvExpr/AlternativeFinder.h"
    2726
    2827namespace Tuples {
     
    4544        /// returns true if the expression may contain side-effects.
    4645        bool maybeImpure( Expression * expr );
    47 
    48 
    49         /// helper function used by explode
    50         template< typename OutputIterator >
    51         void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out ) {
    52                 Type * res = expr->get_result();
    53                 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) {
    54                         ResolvExpr::AltList alts;
    55                         explodeUnique( addrExpr->get_arg(), alt, indexer, back_inserter( alts ) );
    56                         for ( ResolvExpr::Alternative & alt : alts ) {
    57                                 // distribute '&' over all components
    58                                 alt.expr = new AddressExpr( alt.expr );
    59                                 *out++ = alt;
    60                         }
    61                 } else if ( TupleType * tupleType = dynamic_cast< TupleType * > ( res ) ) {
    62                         if ( TupleExpr * tupleExpr = dynamic_cast< TupleExpr * >( expr ) ) {
    63                                 // can open tuple expr and dump its exploded components
    64                                 for ( Expression * expr : tupleExpr->get_exprs() ) {
    65                                         explodeUnique( expr, alt, indexer, out );
    66                                 }
    67                         } else {
    68                                 // tuple type, but not tuple expr - recursively index into its components
    69                                 Expression * arg = expr->clone();
    70                                 if ( Tuples::maybeImpure( arg ) ) {
    71                                         // expressions which may contain side effects require a single unique instance of the expression.
    72                                         // resolve the UniqueExpr (which should be relatively cheap, since the argument is already resolved)
    73                                         // so that its accompanying object is properly constructed and destructed.
    74                                         arg = new UniqueExpr( arg );
    75                                         arg = ResolvExpr::resolveInVoidContext( arg, indexer );
    76                                 }
    77                                 for ( unsigned int i = 0; i < tupleType->size(); i++ ) {
    78                                         TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i );
    79                                         explodeUnique( idx, alt, indexer, out );
    80                                         delete idx;
    81                                 }
    82                                 delete arg;
    83                         }
    84                 } else {
    85                         // atomic (non-tuple) type - output a clone of the expression in a new alternative
    86                         *out++ = ResolvExpr::Alternative( expr->clone(), alt.env, alt.cost, alt.cvtCost );
    87                 }
    88         }
    89 
    90         /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type
    91         template< typename OutputIterator >
    92         void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out ) {
    93                 explodeUnique( alt.expr, alt, indexer, out );
    94         }
    95 
    96         // explode list of alternatives
    97         template< typename AltIterator, typename OutputIterator >
    98         void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out ) {
    99                 for ( ; altBegin != altEnd; ++altBegin ) {
    100                         explode( *altBegin, indexer, out );
    101                 }
    102         }
    103 
    104         template< typename OutputIterator >
    105         void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out ) {
    106                 explode( alts.begin(), alts.end(), indexer, out );
    107         }
    10846} // namespace Tuples
    10947
  • src/Tuples/module.mk

    r23bb1b9 r141b786  
    1616
    1717SRC +=  Tuples/TupleAssignment.cc \
    18         Tuples/TupleExpansion.cc
     18        Tuples/TupleExpansion.cc \
     19        Tuples/Explode.cc
  • src/main.cc

    r23bb1b9 r141b786  
    251251                } // if
    252252
    253                 OPTPRINT( "expandUniqueExpr" ); // xxx - is this the right place for this? want to expand ASAP so that subsequent passes don't need to worry about double-visiting a unique expr
    254                 Tuples::expandUniqueExpr( translationUnit );
    255 
    256253                // fix ObjectDecl - replaces ConstructorInit nodes
    257254                OPTPRINT( "fixInit" )
     
    261258                        return 0;
    262259                } // if
     260
     261                OPTPRINT( "expandUniqueExpr" ); // xxx - is this the right place for this? want to expand ASAP so that subsequent 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
     262                Tuples::expandUniqueExpr( translationUnit );
    263263
    264264                OPTPRINT("instantiateGenerics")
Note: See TracChangeset for help on using the changeset viewer.