Changeset ffad73a for src/GenPoly
- Timestamp:
- Dec 15, 2015, 4:57:31 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 5f6c42c
- Parents:
- 78dd0da
- Location:
- src/GenPoly
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
r78dd0da rffad73a 155 155 // with the same mangled name, so we need to further mangle the names. 156 156 for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) { 157 if ( isPoly Val( (*retval)->get_type(), tyVars ) ) {157 if ( isPolyType( (*retval)->get_type(), tyVars ) ) { 158 158 name << "P"; 159 159 } else { … … 164 164 std::list< DeclarationWithType *> ¶mList = function->get_parameters(); 165 165 for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) { 166 if ( isPoly Val( (*arg)->get_type(), tyVars ) ) {166 if ( isPolyType( (*arg)->get_type(), tyVars ) ) { 167 167 name << "P"; 168 168 } else { … … 326 326 } 327 327 328 TypeInstType *isPolyType( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {329 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {330 if ( env ) {331 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {332 return isPolyType( newType, env, tyVars );333 } // if334 } // if335 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {336 return typeInst;337 } else {338 return 0;339 } // if340 } else {341 return 0;342 } // if343 }344 345 328 Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) { 346 329 if ( useRetval ) { … … 351 334 ObjectDecl *newObj = makeTemporary( retType->clone() ); 352 335 Expression *paramExpr = new VariableExpr( newObj ); 353 if ( ! isPolyType( newObj->get_type(), env, scopeTyVars) ) {336 if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) { 354 337 paramExpr = new AddressExpr( paramExpr ); 355 338 } // if … … 377 360 Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) { 378 361 Expression *ret = appExpr; 379 if ( ! function->get_returnVals().empty() && isPoly Val( function->get_returnVals().front()->get_type(), tyVars ) ) {362 if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) { 380 363 ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg ); 381 364 } // if … … 391 374 void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) { 392 375 assert( ! arg->get_results().empty() ); 393 // /if ( ! dynamic_cast< PointerType *>( arg->get_results().front() ) ) {376 // if ( ! dynamic_cast< PointerType *>( arg->get_results().front() ) ) { 394 377 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( param ); 395 378 if ( typeInst && exprTyVars.find( typeInst->get_name() ) != exprTyVars.end() ) { … … 411 394 } // if 412 395 } // if 413 // /}396 // } 414 397 } 415 398 … … 469 452 // actually make the adapter type 470 453 FunctionType *adapter = adaptee->clone(); 471 if ( ! adapter->get_returnVals().empty() && isPoly Val( adapter->get_returnVals().front()->get_type(), tyVars ) ) {454 if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) { 472 455 makeRetParm( adapter ); 473 456 } // if … … 479 462 assert( param ); 480 463 assert( arg ); 481 /// std::cout << "arg type is "; 482 /// arg->get_type()->print( std::cout ); 483 /// std::cout << "param type is "; 484 /// param->get_type()->print( std::cout ); 485 /// std::cout << " tyVars are: "; 486 /// printTyVarMap( std::cout, tyVars ); 487 if ( isPolyVal( realParam->get_type(), tyVars ) ) { 488 /// if ( dynamic_cast< PointerType *>( arg->get_type() ) ) { 489 /// return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() ); 490 /// } else { 464 if ( isPolyType( realParam->get_type(), tyVars ) ) { 465 // if ( dynamic_cast< PointerType *>( arg->get_type() ) ) { 466 // return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() ); 467 // } else { 491 468 if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) { 492 469 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); … … 495 472 return deref; 496 473 } // if 497 // /}474 // } 498 475 } // if 499 476 return new VariableExpr( param ); … … 542 519 addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars ); 543 520 bodyStmt = new ExprStmt( noLabels, adapteeApp ); 544 } else if ( isPoly Val( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {521 } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { 545 522 if ( (*param)->get_name() == "" ) { 546 523 (*param)->set_name( "_ret" ); … … 614 591 } // passAdapters 615 592 616 TypeInstType *isPolyPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {617 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {618 return isPolyType( ptr->get_base(), env, tyVars );619 } else if ( env ) {620 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {621 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {622 return isPolyPtr( newType, env, tyVars );623 } // if624 } // if625 } // if626 return 0;627 }628 629 TypeInstType *isPolyPtrPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {630 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {631 return isPolyPtr( ptr->get_base(), env, tyVars );632 } else if ( env ) {633 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {634 if ( Type *newType = env->lookup( typeInst->get_name() ) ) {635 return isPolyPtrPtr( newType, env, tyVars );636 } // if637 } // if638 } // if639 return 0;640 }641 642 593 Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) { 643 594 NameExpr *opExpr; … … 670 621 assert( ! appExpr->get_results().empty() ); 671 622 assert( appExpr->get_args().size() == 2 ); 672 Type InstType *typeInst1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), env, scopeTyVars);673 Type InstType *typeInst2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), env, scopeTyVars);674 assert( ! typeInst1 || ! typeInst2 );623 Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env ); 624 Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env ); 625 assert( ! baseType1 || ! baseType2 ); 675 626 UntypedExpr *ret = 0; 676 if ( typeInst1 || typeInst2 ) {627 if ( baseType1 || baseType2 ) { 677 628 ret = new UntypedExpr( new NameExpr( "?+?" ) ); 678 629 } // if 679 if ( typeInst1 ) {630 if ( baseType1 ) { 680 631 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 681 632 multiply->get_args().push_back( appExpr->get_args().back() ); 682 multiply->get_args().push_back( new NameExpr( sizeofName( typeInst1 ) ) );633 multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) ); 683 634 ret->get_args().push_back( appExpr->get_args().front() ); 684 635 ret->get_args().push_back( multiply ); 685 } else if ( typeInst2 ) {636 } else if ( baseType2 ) { 686 637 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 687 638 multiply->get_args().push_back( appExpr->get_args().front() ); 688 multiply->get_args().push_back( new NameExpr( sizeofName( typeInst2 ) ) );639 multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) ); 689 640 ret->get_args().push_back( multiply ); 690 641 ret->get_args().push_back( appExpr->get_args().back() ); 691 642 } // if 692 if ( typeInst1 || typeInst2 ) {643 if ( baseType1 || baseType2 ) { 693 644 ret->get_results().push_front( appExpr->get_results().front()->clone() ); 694 645 if ( appExpr->get_env() ) { … … 703 654 assert( ! appExpr->get_results().empty() ); 704 655 assert( ! appExpr->get_args().empty() ); 705 if ( isPolyType( appExpr->get_results().front(), env, scopeTyVars) ) {656 if ( isPolyType( appExpr->get_results().front(), scopeTyVars, env ) ) { 706 657 Expression *ret = appExpr->get_args().front(); 707 658 delete ret->get_results().front(); … … 718 669 assert( ! appExpr->get_results().empty() ); 719 670 assert( appExpr->get_args().size() == 1 ); 720 if ( Type InstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars) ) {671 if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) { 721 672 Type *tempType = appExpr->get_results().front()->clone(); 722 673 if ( env ) { … … 732 683 assignExpr->get_args().push_back( appExpr->get_args().front()->clone() ); 733 684 } // if 734 CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, typeInst, varExpr->get_var()->get_name() == "?++" ) );685 CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) ); 735 686 return new CommaExpr( firstComma, tempExpr ); 736 687 } // if … … 738 689 assert( ! appExpr->get_results().empty() ); 739 690 assert( appExpr->get_args().size() == 1 ); 740 if ( Type InstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars) ) {741 return makeIncrDecrExpr( appExpr, typeInst, varExpr->get_var()->get_name() == "++?" );691 if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) { 692 return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" ); 742 693 } // if 743 694 } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) { 744 695 assert( ! appExpr->get_results().empty() ); 745 696 assert( appExpr->get_args().size() == 2 ); 746 Type InstType *typeInst1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), env, scopeTyVars);747 Type InstType *typeInst2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), env, scopeTyVars);748 if ( typeInst1 && typeInst2 ) {697 Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env ); 698 Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env ); 699 if ( baseType1 && baseType2 ) { 749 700 UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) ); 750 701 divide->get_args().push_back( appExpr ); 751 divide->get_args().push_back( new NameExpr( sizeofName( typeInst1 ) ) );702 divide->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) ); 752 703 divide->get_results().push_front( appExpr->get_results().front()->clone() ); 753 704 if ( appExpr->get_env() ) { … … 756 707 } // if 757 708 return divide; 758 } else if ( typeInst1 ) {709 } else if ( baseType1 ) { 759 710 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 760 711 multiply->get_args().push_back( appExpr->get_args().back() ); 761 multiply->get_args().push_back( new NameExpr( sizeofName( typeInst1 ) ) );712 multiply->get_args().push_back( new NameExpr( sizeofName( baseType1 ) ) ); 762 713 appExpr->get_args().back() = multiply; 763 } else if ( typeInst2 ) {714 } else if ( baseType2 ) { 764 715 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 765 716 multiply->get_args().push_back( appExpr->get_args().front() ); 766 multiply->get_args().push_back( new NameExpr( sizeofName( typeInst2 ) ) );717 multiply->get_args().push_back( new NameExpr( sizeofName( baseType2 ) ) ); 767 718 appExpr->get_args().front() = multiply; 768 719 } // if … … 770 721 assert( ! appExpr->get_results().empty() ); 771 722 assert( appExpr->get_args().size() == 2 ); 772 Type InstType *typeInst = isPolyPtr( appExpr->get_results().front(), env, scopeTyVars);773 if ( typeInst) {723 Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ); 724 if ( baseType ) { 774 725 UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) ); 775 726 multiply->get_args().push_back( appExpr->get_args().back() ); 776 multiply->get_args().push_back( new NameExpr( sizeofName( typeInst) ) );727 multiply->get_args().push_back( new NameExpr( sizeofName( baseType ) ) ); 777 728 appExpr->get_args().back() = multiply; 778 729 } // if … … 841 792 842 793 Expression *Pass1::mutate( UntypedExpr *expr ) { 843 if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), env, scopeTyVars) ) {794 if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) { 844 795 if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) { 845 796 if ( name->get_name() == "*?" ) { … … 857 808 assert( ! addrExpr->get_arg()->get_results().empty() ); 858 809 addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) ); 859 if ( isPolyType( addrExpr->get_arg()->get_results().front(), env, scopeTyVars) ) {810 if ( isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env ) ) { 860 811 Expression *ret = addrExpr->get_arg(); 861 812 delete ret->get_results().front(); … … 1104 1055 Statement *Pass3::mutate( DeclStmt *declStmt ) { 1105 1056 if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) { 1106 if ( isPoly Val( objectDecl->get_type(), scopeTyVars ) ) {1057 if ( isPolyType( objectDecl->get_type(), scopeTyVars ) ) { 1107 1058 // change initialization of a polymorphic value object 1108 1059 // to allocate storage with alloca 1109 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( objectDecl->get_type() ); 1110 assert( typeInst ); 1060 Type *declType = objectDecl->get_type(); 1111 1061 UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) ); 1112 alloc->get_args().push_back( new NameExpr( sizeofName( typeInst) ) );1062 alloc->get_args().push_back( new NameExpr( sizeofName( declType ) ) ); 1113 1063 1114 1064 delete objectDecl->get_init(); -
src/GenPoly/GenPoly.cc
r78dd0da rffad73a 15 15 16 16 #include "GenPoly.h" 17 18 #include "SymTab/Mangler.h" 19 #include "SynTree/Expression.h" 17 20 #include "SynTree/Type.h" 18 21 … … 21 24 22 25 namespace GenPoly { 23 /// A function needs an adapter if it returns a polymorphic value or if any of its24 /// parameters have polymorphic type25 26 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { 26 if ( ! adaptee->get_returnVals().empty() && isPoly Val( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {27 if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { 27 28 return true; 28 29 } // if 29 30 for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) { 30 if ( isPoly Val( (*innerArg)->get_type(), tyVars ) ) {31 if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) { 31 32 return true; 32 33 } // if … … 66 67 } 67 68 68 bool isPolyVal( Type *type, const TyVarMap &tyVars ) { 69 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { 69 namespace { 70 /// Checks a parameter list for polymorphic parameters 71 bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { 72 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { 73 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); 74 assert(paramType && "Aggregate parameters should be type expressions"); 75 if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true; 76 } 77 return false; 78 } 79 } 80 81 Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { 82 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { 83 if ( env ) { 84 if ( Type *newType = env->lookup( typeInst->get_name() ) ) { 85 return isPolyType( newType, tyVars, env ); 86 } // if 87 } // if 70 88 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { 71 return true; 89 return type; 90 } 91 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { 92 if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type; 93 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { 94 if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type; 95 } 96 return 0; 97 } 98 99 Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { 100 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { 101 return isPolyType( ptr->get_base(), tyVars, env ); 102 } else if ( env ) { 103 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) { 104 if ( Type *newType = env->lookup( typeInst->get_name() ) ) { 105 return isPolyPtr( newType, tyVars, env ); 106 } // if 72 107 } // if 73 108 } // if 74 return false; 75 } 76 77 bool isPolyObj( Type *type, const TyVarMap &tyVars ) { 78 if ( isPolyVal( type, tyVars ) ) { 79 return true; 80 } else if ( PointerType *pt = dynamic_cast<PointerType*>( type ) ) { 81 return isPolyObj( pt->get_base(), tyVars ); 82 } else { 83 return false; 84 } 109 return 0; 85 110 } 86 111 … … 91 116 os << std::endl; 92 117 } 118 119 std::string sizeofName( Type *ty ) { 120 return std::string( "_sizeof_" ) + SymTab::Mangler::mangle( ty, false, false ); 121 } 122 123 std::string alignofName( Type *ty ) { 124 return std::string( "_alignof_" ) + SymTab::Mangler::mangle( ty, false, false ); 125 } 93 126 } // namespace GenPoly 94 127 -
src/GenPoly/GenPoly.h
r78dd0da rffad73a 20 20 #include <string> 21 21 #include <iostream> 22 22 23 #include "SynTree/Declaration.h" 24 #include "SynTree/TypeSubstitution.h" 23 25 24 26 namespace GenPoly { 25 27 typedef std::map< std::string, TypeDecl::Kind > TyVarMap; 26 28 29 /// A function needs an adapter if it returns a polymorphic value or if any of its 30 /// parameters have polymorphic type 27 31 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVarr ); 32 33 /// true iff function has polymorphic return type 28 34 bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ); 29 35 bool isPolyRet( FunctionType *function, std::string &name ); 30 36 bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ); 31 // bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars );32 bool isPolyVal( Type *type, const TyVarMap &tyVars );33 37 34 // true if type variable or any number of pointers to type variable 35 bool isPolyObj( Type *type, const TyVarMap &tyVars ); 38 /// returns polymorphic type if is polymorphic type, NULL otherwise; will look up substitution in env if provided 39 Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 ); 40 41 /// returns polymorphic type if is pointer to polymorphic type, NULL otherwise; will look up substitution in env if provided 42 Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env = 0 ); 43 44 /// Prints type variable map 36 45 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ); 46 47 /// Gets the name of the sizeof parameter for the type 48 std::string sizeofName( Type *ty ); 49 50 /// Gets the name of the alignof parameter for the type 51 std::string alignofName( Type *ty ); 37 52 } // namespace GenPoly 38 53 -
src/GenPoly/ScrubTyVars.cc
r78dd0da rffad73a 19 19 #include "GenPoly.h" 20 20 #include "ScrubTyVars.h" 21 22 #include "SymTab/Mangler.h"23 21 24 22 #include "SynTree/Mutator.h" … … 78 76 return Mutator::mutate( pointer ); 79 77 } 80 81 std::string sizeofName( Type *ty ) {82 return std::string( "_sizeof_" ) + SymTab::Mangler::mangle( ty, false, false );83 }84 85 std::string alignofName( Type *ty ) {86 return std::string( "_alignof_" ) + SymTab::Mangler::mangle( ty, false, false );87 }88 78 } // namespace GenPoly 89 79 -
src/GenPoly/ScrubTyVars.h
r78dd0da rffad73a 59 59 return static_cast< SynTreeClass* >( target->acceptMutator( scrubber ) ); 60 60 } 61 62 /// Gets the name of the sizeof parameter for the type63 std::string sizeofName( Type *ty );64 65 /// Gets the name of the alignof parameter for the type66 std::string alignofName( Type *ty );67 61 } // namespace GenPoly 68 62
Note: See TracChangeset
for help on using the changeset viewer.