Changeset 141b786
- Timestamp:
- Nov 9, 2016, 2:21:05 PM (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:
- b726084
- Parents:
- 23bb1b9
- Location:
- src
- Files:
-
- 2 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/FixInit.cc
r23bb1b9 r141b786 65 65 static void resolveImplicitCalls( std::list< Declaration * > & translationUnit ); 66 66 67 typedef SymTab::Indexer Parent; 68 using Parent::visit; 69 67 70 virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ); 71 virtual void visit( UniqueExpr * unqExpr ); 68 72 69 73 /// create and resolve ctor/dtor expression: fname(var, [cpArg]) … … 178 182 179 183 virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr ); 184 virtual Expression * mutate( UniqueExpr * unqExpr ); 180 185 }; 181 186 … … 421 426 void ResolveCopyCtors::visit( ImplicitCopyCtorExpr *impCpCtorExpr ) { 422 427 CP_CTOR_PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; ) 423 Visitor::visit( impCpCtorExpr );428 Parent::visit( impCpCtorExpr ); 424 429 env = impCpCtorExpr->get_env(); // xxx - maybe we really should just have a PolyIndexer... 425 430 … … 455 460 } 456 461 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 457 484 458 485 Expression * FixCopyCtors::mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) { … … 526 553 return callExpr; 527 554 } // 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; 528 586 } 529 587 -
src/Makefile.in
r23bb1b9 r141b786 189 189 SynTree/driver_cfa_cpp-TypeSubstitution.$(OBJEXT) \ 190 190 SynTree/driver_cfa_cpp-Attribute.$(OBJEXT) \ 191 SynTree/driver_cfa_cpp-VarExprReplacer.$(OBJEXT) \ 191 192 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) 193 195 am_driver_cfa_cpp_OBJECTS = $(am__objects_1) 194 196 driver_cfa_cpp_OBJECTS = $(am_driver_cfa_cpp_OBJECTS) … … 403 405 SynTree/Initializer.cc SynTree/Visitor.cc SynTree/Mutator.cc \ 404 406 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 407 410 MAINTAINERCLEANFILES = Parser/parser.output ${libdir}/${notdir \ 408 411 ${cfa_cpplib_PROGRAMS}} … … 764 767 SynTree/driver_cfa_cpp-Attribute.$(OBJEXT): SynTree/$(am__dirstamp) \ 765 768 SynTree/$(DEPDIR)/$(am__dirstamp) 769 SynTree/driver_cfa_cpp-VarExprReplacer.$(OBJEXT): \ 770 SynTree/$(am__dirstamp) SynTree/$(DEPDIR)/$(am__dirstamp) 766 771 Tuples/$(am__dirstamp): 767 772 @$(MKDIR_P) Tuples … … 774 779 Tuples/driver_cfa_cpp-TupleExpansion.$(OBJEXT): \ 775 780 Tuples/$(am__dirstamp) Tuples/$(DEPDIR)/$(am__dirstamp) 781 Tuples/driver_cfa_cpp-Explode.$(OBJEXT): Tuples/$(am__dirstamp) \ 782 Tuples/$(DEPDIR)/$(am__dirstamp) 776 783 driver/$(am__dirstamp): 777 784 @$(MKDIR_P) driver … … 878 885 -rm -f SynTree/driver_cfa_cpp-TypeofType.$(OBJEXT) 879 886 -rm -f SynTree/driver_cfa_cpp-VarArgsType.$(OBJEXT) 887 -rm -f SynTree/driver_cfa_cpp-VarExprReplacer.$(OBJEXT) 880 888 -rm -f SynTree/driver_cfa_cpp-Visitor.$(OBJEXT) 881 889 -rm -f SynTree/driver_cfa_cpp-VoidType.$(OBJEXT) 882 890 -rm -f SynTree/driver_cfa_cpp-ZeroOneType.$(OBJEXT) 891 -rm -f Tuples/driver_cfa_cpp-Explode.$(OBJEXT) 883 892 -rm -f Tuples/driver_cfa_cpp-TupleAssignment.$(OBJEXT) 884 893 -rm -f Tuples/driver_cfa_cpp-TupleExpansion.$(OBJEXT) … … 984 993 @AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-TypeofType.Po@am__quote@ 985 994 @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@ 986 996 @AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-Visitor.Po@am__quote@ 987 997 @AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-VoidType.Po@am__quote@ 988 998 @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@ 989 1000 @AMDEP_TRUE@@am__include@ @am__quote@Tuples/$(DEPDIR)/driver_cfa_cpp-TupleAssignment.Po@am__quote@ 990 1001 @AMDEP_TRUE@@am__include@ @am__quote@Tuples/$(DEPDIR)/driver_cfa_cpp-TupleExpansion.Po@am__quote@ … … 2406 2417 @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` 2407 2418 2419 SynTree/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 2426 SynTree/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 2408 2433 Tuples/driver_cfa_cpp-TupleAssignment.o: Tuples/TupleAssignment.cc 2409 2434 @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 … … 2433 2458 @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ 2434 2459 @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 2461 Tuples/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 2468 Tuples/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` 2435 2474 2436 2475 .ll.cc: -
src/ResolvExpr/AlternativeFinder.cc
r23bb1b9 r141b786 39 39 #include "SymTab/Validate.h" 40 40 #include "Tuples/Tuples.h" 41 #include "Tuples/Explode.h" 41 42 #include "Common/utility.h" 42 43 #include "InitTweak/InitTweak.h" … … 240 241 } // if 241 242 } // 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 } 242 251 } // if 243 252 } … … 1057 1066 1058 1067 void AlternativeFinder::visit( UniqueExpr *unqExpr ) { 1059 // this won't work because the unqExprs wont share an expression anymore...1060 1068 AlternativeFinder finder( indexer, env ); 1061 1069 finder.findWithAdjustment( unqExpr->get_expr() ); 1062 1070 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" 1070 1072 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 ) ); 1076 1074 } 1077 1075 } -
src/SynTree/Expression.cc
r23bb1b9 r141b786 609 609 610 610 long long UniqueExpr::count = 0; 611 UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( new Expression* ), object( new ObjectDecl* ), id( idVal ) { 611 UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) { 612 assert( expr ); 612 613 assert( count != -1 ); 613 614 if ( id == -1 ) id = count++; 614 set_expr( expr );615 assert( expr );616 615 if ( expr->get_result() ) { 617 616 set_result( expr->get_result()->clone() ); 618 617 } 619 set_object( nullptr ); 620 } 621 UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( other.expr ), object( other.object ), id( other.id ) { 618 } 619 UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) { 622 620 } 623 621 UniqueExpr::~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; 630 625 } 631 626 void UniqueExpr::print( std::ostream &os, int indent ) const { -
src/SynTree/Expression.h
r23bb1b9 r141b786 742 742 ~UniqueExpr(); 743 743 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; } 749 752 750 753 int get_id() const { return id; } … … 755 758 virtual void print( std::ostream &os, int indent = 0 ) const; 756 759 private: 757 std::shared_ptr< Expression * > expr; 758 std::shared_ptr< ObjectDecl * > object; 760 Expression * expr; 761 ObjectDecl * object; 762 VariableExpr * var; 759 763 int id; 760 764 static long long count; -
src/Tuples/TupleAssignment.cc
r23bb1b9 r141b786 20 20 #include "SynTree/Initializer.h" 21 21 #include "Tuples.h" 22 #include "Explode.h" 22 23 #include "Common/SemanticError.h" 23 24 #include "InitTweak/InitTweak.h" -
src/Tuples/TupleExpansion.cc
r23bb1b9 r141b786 41 41 public: 42 42 typedef GenPoly::DeclMutator Parent; 43 43 44 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 } 45 53 }; 46 54 … … 107 115 /// given a expression representing the member and an expression representing the aggregate, 108 116 /// reconstructs a flattened UntypedMemberExpr with the right precedence 109 Expression * reconstructMemberExpr( Expression * member, UniqueExpr* aggr ) {117 Expression * reconstructMemberExpr( Expression * member, Expression * aggr ) { 110 118 if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( member ) ) { 111 119 // construct a new UntypedMemberExpr with the correct structure , and recursively … … 127 135 Expression * MemberTupleExpander::mutate( UntypedMemberExpr * memberExpr ) { 128 136 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 ); 130 142 for ( Expression *& expr : tupleExpr->get_exprs() ) { 131 expr = reconstructMemberExpr( expr, unqExpr );132 } 133 delete unqExpr;143 expr = reconstructMemberExpr( expr, aggr ); 144 } 145 delete aggr; 134 146 return tupleExpr; 135 147 } else { … … 142 154 Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) { 143 155 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(); 167 190 } 168 191 169 192 Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) { 170 // xxx - Parent::mutate?193 assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) ); 171 194 CompoundStmt * compoundStmt = new CompoundStmt( noLabels ); 172 195 std::list< Statement * > & stmts = compoundStmt->get_kids(); … … 192 215 int cnt = 0; 193 216 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 ) ); 195 218 } 196 219 typeMap[mangleName] = decl; -
src/Tuples/Tuples.h
r23bb1b9 r141b786 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Mon May 18 15:04:02 201513 // Update Count : 211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Nov 9 13:17:58 2016 13 // Update Count : 15 14 14 // 15 15 … … 18 18 19 19 #include <string> 20 #include <vector>21 #include "ResolvExpr/AlternativeFinder.h"22 #include "ResolvExpr/Resolver.h"23 20 24 21 #include "SynTree/Expression.h" 25 22 #include "SynTree/Declaration.h" 26 23 #include "SynTree/Type.h" 24 25 #include "ResolvExpr/AlternativeFinder.h" 27 26 28 27 namespace Tuples { … … 45 44 /// returns true if the expression may contain side-effects. 46 45 bool maybeImpure( Expression * expr ); 47 48 49 /// helper function used by explode50 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 components58 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 components64 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 components69 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 alternative86 *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-type91 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 alternatives97 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 }108 46 } // namespace Tuples 109 47 -
src/Tuples/module.mk
r23bb1b9 r141b786 16 16 17 17 SRC += Tuples/TupleAssignment.cc \ 18 Tuples/TupleExpansion.cc 18 Tuples/TupleExpansion.cc \ 19 Tuples/Explode.cc -
src/main.cc
r23bb1b9 r141b786 251 251 } // if 252 252 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 expr254 Tuples::expandUniqueExpr( translationUnit );255 256 253 // fix ObjectDecl - replaces ConstructorInit nodes 257 254 OPTPRINT( "fixInit" ) … … 261 258 return 0; 262 259 } // 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 ); 263 263 264 264 OPTPRINT("instantiateGenerics")
Note: See TracChangeset
for help on using the changeset viewer.