Changes in / [f678663e:093f1a0]


Ignore:
Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/LabelFixer.cc

    rf678663e r093f1a0  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Tue Jul 28 13:32:43 2015
    13 // Update Count     : 156
     12// Last Modified On : Wed Jul 08 12:36:46 2015
     13// Update Count     : 145
    1414//
    1515
     
    2727
    2828namespace ControlStruct {
     29        LabelFixer::Entry::Entry( Statement *to, Statement *from ) : definition ( to ) {
     30                if ( from != 0 ) {
     31                        UsageLoc loc; loc.stmt = from;
     32                        usage.push_back( loc );
     33                }
     34        }
     35
     36        LabelFixer::Entry::Entry( Statement *to, Expression *from ) : definition ( to ) {
     37                if ( from != 0 ) {
     38                        UsageLoc loc; loc.expr = from;
     39                        usage.push_back( loc );
     40                }
     41        }
     42
     43
    2944        bool LabelFixer::Entry::insideLoop() {
    3045                return ( dynamic_cast< ForStmt * > ( definition ) ||
    3146                        dynamic_cast< WhileStmt * > ( definition )  );
     47        }
     48
     49        void LabelFixer::Entry::UsageLoc::accept( Visitor & visitor ) {
     50                if ( dynamic_cast< Statement * >( stmt ) ) {
     51                        stmt->accept( visitor );
     52                } else {
     53                        expr->accept( visitor );
     54                }
    3255        }
    3356
     
    5477        // prune to at most one label definition for each statement
    5578        void LabelFixer::visit( Statement *stmt ) {
     79                currentStatement = stmt;
    5680                std::list< Label > &labels = stmt->get_labels();
    5781
     
    5983                        // only remember one label for each statement
    6084                        Label current = setLabelsDef( labels, stmt );
     85                        labels.clear();
     86                        labels.push_front( current );
    6187                } // if
    6288        }
     
    104130                        }       else {
    105131                                // used previously, but undefined until now -> link with this entry
    106                                 delete labelTable[ *i ];
     132                                Entry * oldEntry = labelTable[ *i ];
     133                                e->add_uses( *oldEntry );
    107134                                labelTable[ *i ] = e;
    108135                        } // if
     
    114141        }
    115142
    116         // A label was used, add it ot the table if it isn't already there
     143        // Remember all uses of a label.
    117144        template< typename UsageNode >
    118145        void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) {
    119146                assert( use != 0 );
    120147
    121                 // add label with an unknown origin
    122                 if ( labelTable.find( orgValue ) == labelTable.end() ) {
    123                         labelTable[ orgValue ] = new Entry( 0 );
    124                 }
    125         }
    126 
    127         // Builds a table that maps a label to its defining statement.
     148                if ( labelTable.find( orgValue ) != labelTable.end() ) {
     149                        // the label has been defined or used before
     150                        labelTable[ orgValue ]->add_use( use );
     151                } else {
     152                        labelTable[ orgValue ] = new Entry( 0, use );
     153                }
     154        }
     155
     156        class LabelGetter : public Visitor {
     157                public:
     158                LabelGetter( Label &label ) : label( label ) {}
     159
     160                virtual void visit( BranchStmt * branchStmt ) {
     161                        label = branchStmt->get_target();
     162                }
     163
     164                virtual void visit( UntypedExpr * untyped ) {
     165                        NameExpr * name = dynamic_cast< NameExpr * >( untyped->get_function() );
     166                        assert( name );
     167                        assert( name->get_name() == "&&" );
     168                        NameExpr * arg = dynamic_cast< NameExpr * >( untyped->get_args().front() );
     169                        assert( arg );
     170                        label = arg->get_name();
     171                }               
     172
     173                private:
     174                        Label &label;
     175        };
     176
     177        class LabelSetter : public Visitor {
     178                public:
     179                LabelSetter( Label label ) : label( label ) {}
     180
     181                virtual void visit( BranchStmt * branchStmt ) {
     182                        branchStmt->set_target( label );
     183                }
     184
     185                virtual void visit( UntypedExpr * untyped ) {
     186                        NameExpr * name = dynamic_cast< NameExpr * >( untyped->get_function() );
     187                        assert( name );
     188                        assert( name->get_name() == "&&" );
     189                        NameExpr * arg = dynamic_cast< NameExpr * >( untyped->get_args().front() );
     190                        assert( arg );
     191                        arg->set_name( label );
     192                }
     193
     194        private:
     195                Label label;
     196        };
     197
     198        // Ultimately builds a table that maps a label to its defining statement.
     199        // In the process,
    128200        std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) {
     201                std::map< Statement *, Entry * > def_us;
     202
     203                // combine the entries for all labels that target the same location
     204                for ( std::map< Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) {
     205                        Entry *e = i->second;
     206
     207                        if ( def_us.find ( e->get_definition() ) == def_us.end() ) {
     208                                def_us[ e->get_definition() ] = e;
     209                        } else if ( e->used() ) {
     210                                def_us[ e->get_definition() ]->add_uses( *e );
     211                        }
     212                }
     213
     214                // create a unique label for each target location.
     215                for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); ++i ) {
     216                        Statement *to = (*i).first;
     217                        Entry * entry = (*i).second;
     218                        std::list< Entry::UsageLoc > &from = entry->get_uses();
     219
     220                        // no label definition found
     221                        if ( to == 0 ) {
     222                                Label undef;
     223                                LabelGetter getLabel( undef );
     224                                from.back().accept( getLabel );
     225                                // Label undef = getLabel( from.back()->get_target() );
     226                                throw SemanticError ( "'" + undef + "' label not defined");
     227                        } // if
     228
     229                        // generate a new label, and attach it to its defining statement as the only label on that statement
     230                        Label finalLabel = generator->newLabel( to->get_labels().back() );
     231                        entry->set_label( finalLabel );
     232
     233                        to->get_labels().clear();
     234                        to->get_labels().push_back( finalLabel );
     235
     236                        // redirect each of the source branch statements to the new target label
     237                        for ( std::list< Entry::UsageLoc >::iterator j = from.begin(); j != from.end(); ++j ) {
     238                                LabelSetter setLabel( finalLabel );
     239                                (*j).accept( setLabel );
     240                                // setLabel( *j, finalLabel );
     241
     242                                // BranchStmt *jump = *j;
     243                                // assert( jump != 0 );
     244                                // jump->set_target( finalLabel );
     245                        } // for
     246                } // for
     247
     248                // create a table where each label maps to its defining statement
    129249                std::map< Label, Statement * > *ret = new std::map< Label, Statement * >();
    130                 for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) {
    131                         if ( ! i->second->defined() ) {
    132                                 throw SemanticError( "Use of undefined label: " + i->first );
    133                         }
    134                         (*ret)[ i->first ] = i->second->get_definition();
     250                for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); ++i ) {
     251                        (*ret)[ (*i).second->get_label() ] = (*i).first;
    135252                }
    136253
  • src/ControlStruct/LabelFixer.h

    rf678663e r093f1a0  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Tue Jul 28 13:09:02 2015
    13 // Update Count     : 31
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jun 29 17:24:39 2015
     13// Update Count     : 29
    1414//
    1515
     
    6363                class Entry {
    6464                        public:
     65                        union UsageLoc {
     66                                Statement * stmt;
     67                                Expression * expr;
     68
     69                                void accept( Visitor &visitor );
     70                        };
     71
    6572                        Entry( Statement *to ) : definition( to ) {}
     73                        Entry( Statement *to, Statement *from );
     74                        Entry( Statement *to, Expression *from );
     75                        bool used() { return ( usage.empty() ); }
    6676                        bool defined() { return ( definition != 0 ); }
    6777                        bool insideLoop();
     
    7383                        void set_definition( Statement *def ) { definition = def; }
    7484
     85                        std::list< UsageLoc > &get_uses() { return usage; }
     86                        void add_use( Statement *use ) {
     87                                UsageLoc loc;
     88                                loc.stmt = use;
     89                                usage.push_back( loc );
     90                        }
     91                        void add_use( Expression *use ) {
     92                                UsageLoc loc;
     93                                loc.expr = use;
     94                                usage.push_back( loc );                                 
     95                        }
     96
     97                        void add_uses ( Entry &other ) { usage.insert( usage.end(), other.usage.begin(), other.usage.end() ); }
    7598                  private:
    7699                        Label label; 
    77100                        Statement *definition;
     101                        std::list<UsageLoc> usage;
    78102                };
    79103                 
    80104                std::map < Label, Entry *> labelTable;
    81105                LabelGenerator *generator;
     106                Statement * currentStatement;
    82107        };
    83108} // namespace ControlStruct
  • src/GenPoly/Box.cc

    rf678663e r093f1a0  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Thu Jul 30 14:35:40 2015
    13 // Update Count     : 54
     12// Last Modified On : Wed Jun 24 16:19:07 2015
     13// Update Count     : 10
    1414//
    1515
     
    527527
    528528                void Pass1::passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars ) {
    529                         // collect a list of function types passed as parameters or implicit parameters (assertions)
    530529                        std::list< DeclarationWithType *> &paramList = functionType->get_parameters();
    531530                        std::list< FunctionType *> functions;
     
    538537                                findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter );
    539538                        } // for
    540 
    541                         // parameter function types for which an appropriate adapter has been generated.
    542                         // we cannot use the types after applying substitutions, since two different
    543                         // parameter types may be unified to the same type
    544539                        std::set< std::string > adaptersDone;
    545 
    546540                        for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
    547541                                FunctionType *realFunction = (*funType)->clone();
     542                                assert( env );
     543                                env->apply( realFunction );
     544
    548545                                std::string mangleName = SymTab::Mangler::mangle( realFunction );
    549 
    550                                 // only attempt to create an adapter or pass one as a parameter if we haven't
    551                                 // already done so for this pre-substitution parameter function type.
    552546                                if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
    553                                         std::string mangleName = SymTab::Mangler::mangle( realFunction );
    554                                         adaptersDone.insert( adaptersDone.begin(), mangleName );
    555                                        
    556                                         // apply substitution to type variables to figure out what the
    557                                         // adapter's type should look like
    558                                         assert( env );
    559                                         env->apply( realFunction );
    560                                         mangleName = SymTab::Mangler::mangle( realFunction );
    561547                                        AdapterMap & adapters = Pass1::adapters.top();
    562548                                        AdapterMap::iterator adapter = adapters.find( mangleName );
     
    569555                                                continue;
    570556                                        } else if ( adapter == adapters.end() ) {
    571                                                 // adapter has not been created yet in the current scope, so define it
    572557                                                FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
    573558                                                adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) );
     
    575560                                        } // if
    576561                                        assert( adapter != adapters.end() );
    577 
    578                                         // add the appropriate adapter as a parameter
    579562                                        appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
     563                                        // appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
     564                                        adaptersDone.insert( adaptersDone.begin(), mangleName );
    580565                                } // if
    581566                        } // for
     
    895880
    896881                void Pass1::doBeginScope() {
    897                         // actually, maybe this could (should?) push
    898                         // a copy of the current map
    899882                        adapters.push(AdapterMap());
    900883                }
     
    10681051                        if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
    10691052                                if ( isPolyVal( objectDecl->get_type(), scopeTyVars ) ) {
    1070                                         // change initialization of a polymorphic value object
    1071                                         // to allocate storage with alloca
    10721053                                        TypeInstType *typeInst = dynamic_cast< TypeInstType *>( objectDecl->get_type() );
    10731054                                        assert( typeInst );
    10741055                                        UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
    10751056                                        alloc->get_args().push_back( new NameExpr( typeInst->get_name() ) );
    1076 
    1077                                         delete objectDecl->get_init();
    1078 
    1079                                         std::list<Expression*> designators;
    1080                                         objectDecl->set_init( new SingleInit( alloc, designators ) );
     1057                                        UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
     1058                                        assign->get_args().push_back( new VariableExpr( objectDecl ) );
     1059                                        assign->get_args().push_back( alloc );
     1060                                        stmtsToAddAfter.push_back( new ExprStmt( noLabels, assign ) );
    10811061                                }
    10821062                        }
  • src/ResolvExpr/Cost.h

    rf678663e r093f1a0  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 09:39:50 2015
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Wed Jul 22 16:43:10 2015
    13 // Update Count     : 4
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Sun May 17 09:42:04 2015
     13// Update Count     : 3
    1414//
    1515
     
    5656
    5757        inline void Cost::incPoly( int inc ) {
    58                 poly += inc;
     58                unsafe += inc;
    5959        }
    6060
    6161        inline void Cost::incSafe( int inc ) {
    62                 safe += inc;
     62                unsafe += inc;
    6363        }
    6464
  • src/main.cc

    rf678663e r093f1a0  
    99// Author           : Richard C. Bilson
    1010// Created On       : Fri May 15 23:12:02 2015
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Thu Jul 30 16:08:18 2015
    13 // Update Count     : 167
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Thu Jul 30 15:36:41 2015
     13// Update Count     : 146
    1414//
    1515
     
    228228                // add the assignment statement after the
    229229                // initialization of a type parameter
     230                OPTPRINT( "tweak" )
     231                InitTweak::tweak( translationUnit );
    230232                OPTPRINT( "validate" )
    231233                SymTab::validate( translationUnit, symtabp );
     
    249251                OPTPRINT( "fixNames" )
    250252                CodeGen::fixNames( translationUnit );
    251                 OPTPRINT( "tweak" )
    252                 InitTweak::tweak( translationUnit );
    253253
    254254                if ( libcfap ) {
Note: See TracChangeset for help on using the changeset viewer.