Ignore:
Timestamp:
Dec 16, 2014, 9:41:50 PM (11 years ago)
Author:
Peter A. Buhr <pabuhr@…>
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:
17cd4eb
Parents:
3848e0e
Message:

remove Parser.old, add -XCFA to driver, copy ptrdiff_t from stddef.h in preclude, remove casts from initialization constants, adjust formatting

Location:
translator/ControlStruct
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • translator/ControlStruct/CaseRangeMutator.cc

    r3848e0e rd9a0e76  
    1313
    1414namespace ControlStruct {
    15 
    16   Statement* CaseRangeMutator::mutate(ChooseStmt   *chooseStmt)
    17   {
    18     /* There shouldn't be any `choose' statements by now, throw an exception or something. */
    19     throw( 0 ) ; /* FIXME */
    20   }
    21 
    22   Statement* CaseRangeMutator::mutate(SwitchStmt   *switchStmt)
    23   {
    24     std::list< Statement * > &cases = switchStmt->get_branches();
    25 
    26     // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator
    27     std::list< Statement * >::iterator i = cases.begin();
    28     while (  i != cases.end() ) {
    29       (*i)->acceptMutator( *this );
    30 
    31       if ( ! newCaseLabels.empty() ) {
    32         std::list< Statement * > newCases;
    33 
    34         // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) );
    35 
    36         for ( std::list< Expression * >::iterator j = newCaseLabels.begin();
    37               j != newCaseLabels.end(); j++ ) {
    38           std::list<Label> emptyLabels;
    39           std::list< Statement *> emptyStmts;
    40           newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) );
    41         }
    42 
    43         if( CaseStmt *currentCase = dynamic_cast< CaseStmt * > ( *i ) )
    44           if ( !currentCase->get_statements().empty() ) {
    45             CaseStmt *lastCase = dynamic_cast< CaseStmt * > ( newCases.back() );
    46             if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases
    47             // transfer the statement block (if any) to the new list:
    48             lastCase->set_statements( currentCase->get_statements() );
    49           }
    50         std::list< Statement * >::iterator j = i; advance( j, 1 );
    51         replace ( cases, i, newCases );
    52         i = j;
    53         newCaseLabels.clear();
    54       } else
    55         i++;
     15    Statement *CaseRangeMutator::mutate(ChooseStmt *chooseStmt) {
     16        /* There shouldn't be any `choose' statements by now, throw an exception or something. */
     17        throw( 0 ) ; /* FIXME */
    5618    }
    5719
    58     return switchStmt;
    59   }
     20    Statement *CaseRangeMutator::mutate(SwitchStmt *switchStmt) {
     21        std::list< Statement * > &cases = switchStmt->get_branches();
    6022
    61   Statement* CaseRangeMutator::mutate(FallthruStmt *fallthruStmt)
    62   {
    63     //delete fallthruStmt;
    64     return new NullStmt();
    65   }
     23        // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator
     24        std::list< Statement * >::iterator i = cases.begin();
     25        while (  i != cases.end() ) {
     26            (*i)->acceptMutator( *this );
    6627
    67   Statement* CaseRangeMutator::mutate(CaseStmt *caseStmt)
    68   {
    69     UntypedExpr *cond;
    70     if ( (cond = dynamic_cast< UntypedExpr * >( caseStmt->get_condition() )) != 0 ) {
    71       NameExpr *nmfunc;
    72       if ( (nmfunc = dynamic_cast< NameExpr *>( cond->get_function() )) != 0 ) {
    73         if ( nmfunc->get_name() == std::string("Range") ) {
    74           assert( cond->get_args().size() == 2 );
    75           std::list<Expression *>::iterator i = cond->get_args().begin();
    76           Expression *lo = *i, *hi = *(++i); // "unnecessary" temporaries
    77           fillRange( lo, hi);
    78         }
    79       }
    80     } else if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) {
    81       // case list
    82       assert( ! tcond->get_exprs().empty() );
    83       for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ )
    84         newCaseLabels.push_back( *i ); // do I need to clone them?
     28            if ( ! newCaseLabels.empty() ) {
     29                std::list< Statement * > newCases;
     30
     31                // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) );
     32
     33                for ( std::list< Expression * >::iterator j = newCaseLabels.begin();
     34                      j != newCaseLabels.end(); j++ ) {
     35                    std::list<Label> emptyLabels;
     36                    std::list< Statement *> emptyStmts;
     37                    newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) );
     38                }
     39
     40                if ( CaseStmt *currentCase = dynamic_cast< CaseStmt * > ( *i ) )
     41                    if ( ! currentCase->get_statements().empty() ) {
     42                        CaseStmt *lastCase = dynamic_cast< CaseStmt * > ( newCases.back() );
     43                        if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases
     44                        // transfer the statement block (if any) to the new list:
     45                        lastCase->set_statements( currentCase->get_statements() );
     46                    }
     47                std::list< Statement * >::iterator j = i; advance( j, 1 );
     48                replace ( cases, i, newCases );
     49                i = j;
     50                newCaseLabels.clear();
     51            } else
     52                i++;
     53        } // while
     54
     55        return switchStmt;
    8556    }
    8657
    87     std::list< Statement * > &stmts = caseStmt->get_statements();
    88     mutateAll ( stmts, *this );
    89 
    90     return caseStmt;
    91   }
    92 
    93   void CaseRangeMutator::fillRange(Expression *lo, Expression *hi) {
    94     // generate the actual range (and check for consistency)
    95     Constant *c_lo, *c_hi;
    96     ConstantExpr *ce_lo, *ce_hi;
    97     ce_lo = dynamic_cast< ConstantExpr * >( lo );
    98     ce_hi = dynamic_cast< ConstantExpr * >( hi );
    99 
    100     if ( ce_lo && ce_hi ) {
    101       c_lo = ce_lo->get_constant(); c_hi = ce_hi->get_constant();
    102     } /* else {
    103       if ( !ce_lo ) ;
    104       if ( !ce_hi ) ;
    105       } */
    106     BasicType *ty_lo = dynamic_cast< BasicType * >( c_lo->get_type() ),
    107                        *ty_hi = dynamic_cast< BasicType * >( c_hi->get_type() );
    108    
    109     if( !ty_lo || !ty_hi )
    110       return; // one of them is not a constant
    111 
    112 
    113     switch( ty_lo->get_kind() ) {
    114     case BasicType::Char:
    115     case BasicType::UnsignedChar:
    116       switch( ty_hi->get_kind() )
    117         {
    118         case BasicType::Char:
    119         case BasicType::UnsignedChar:
    120           // first case, they are both printable ASCII characters represented as 'x'
    121           if ( c_lo->get_value().size() == 3 && c_hi->get_value().size() == 3 ) {
    122             char ch_lo = (c_lo->get_value())[1], ch_hi = (c_hi->get_value())[1];
    123 
    124             if ( ch_lo > ch_hi ) { char t=ch_lo; ch_lo=ch_hi; ch_hi=t; }
    125 
    126             for( char c = ch_lo; c <=  ch_hi; c++ ){
    127               Type::Qualifiers q;
    128               Constant cnst( new BasicType(q, BasicType::Char),
    129                                       std::string("'") + c + std::string("'") );
    130               newCaseLabels.push_back( new ConstantExpr( cnst ) );
    131             }
    132 
    133             return;
    134           }
    135           break;
    136         default:
    137           // error: incompatible constants
    138           break;
    139         }
    140       break;
    141     case BasicType::ShortSignedInt:
    142     case BasicType::ShortUnsignedInt:
    143     case BasicType::SignedInt:
    144     case BasicType::UnsignedInt:
    145     case BasicType::LongSignedInt:
    146     case BasicType::LongUnsignedInt:
    147     case BasicType::LongLongSignedInt:
    148     case BasicType::LongLongUnsignedInt:
    149       switch( ty_hi->get_kind() )
    150         {
    151         case BasicType::ShortSignedInt:
    152         case BasicType::ShortUnsignedInt:
    153         case BasicType::SignedInt:
    154         case BasicType::UnsignedInt:
    155         case BasicType::LongSignedInt:
    156         case BasicType::LongUnsignedInt:
    157         case BasicType::LongLongSignedInt:
    158         case BasicType::LongLongUnsignedInt: {
    159           int i_lo = atoi(c_lo->get_value().c_str()),
    160               i_hi = atoi(c_hi->get_value().c_str());
    161 
    162           if ( i_lo > i_hi ) { int t=i_lo; i_lo=i_hi; i_hi=t; }
    163 
    164           for( int c = i_lo; c <=  i_hi; c++ ){
    165             Type::Qualifiers q;
    166             Constant cnst( new BasicType(q, ty_hi->get_kind()), // figure can't hurt (used to think in positives)
    167                                     toString< int >( c ) );
    168             newCaseLabels.push_back( new ConstantExpr( cnst ) );
    169           }
    170 
    171           return;
    172         }
    173         default:
    174           // error: incompatible constants
    175           break;
    176         }
    177       break;
    178     default:
    179       break;
     58    Statement *CaseRangeMutator::mutate(FallthruStmt *fallthruStmt) {
     59        //delete fallthruStmt;
     60        return new NullStmt();
    18061    }
    18162
    182 /* End: */{
    183       // invalid range, signal a warning (it still generates the two case labels)
    184       newCaseLabels.push_back( lo );
    185       newCaseLabels.push_back( hi );
    186       return;
     63    Statement *CaseRangeMutator::mutate(CaseStmt *caseStmt) {
     64        UntypedExpr *cond;
     65        if ( (cond = dynamic_cast< UntypedExpr * >( caseStmt->get_condition() )) != 0 ) {
     66            NameExpr *nmfunc;
     67            if ( (nmfunc = dynamic_cast< NameExpr *>( cond->get_function() )) != 0 ) {
     68                if ( nmfunc->get_name() == std::string("Range") ) {
     69                    assert( cond->get_args().size() == 2 );
     70                    std::list<Expression *>::iterator i = cond->get_args().begin();
     71                    Expression *lo = *i, *hi = *(++i); // "unnecessary" temporaries
     72                    fillRange( lo, hi);
     73                }
     74            }
     75        } else if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) {
     76            // case list
     77            assert( ! tcond->get_exprs().empty() );
     78            for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ )
     79                newCaseLabels.push_back( *i ); // do I need to clone them?
     80        } // if
     81
     82        std::list< Statement * > &stmts = caseStmt->get_statements();
     83        mutateAll ( stmts, *this );
     84
     85        return caseStmt;
    18786    }
    188   }
    18987
     88    void CaseRangeMutator::fillRange(Expression *lo, Expression *hi) {
     89        // generate the actual range (and check for consistency)
     90        Constant *c_lo, *c_hi;
     91        ConstantExpr *ce_lo, *ce_hi;
     92        ce_lo = dynamic_cast< ConstantExpr * >( lo );
     93        ce_hi = dynamic_cast< ConstantExpr * >( hi );
     94
     95        if ( ce_lo && ce_hi ) {
     96            c_lo = ce_lo->get_constant(); c_hi = ce_hi->get_constant();
     97        } /* else {
     98             if ( ! ce_lo ) ;
     99             if ( ! ce_hi ) ;
     100             } */
     101        BasicType *ty_lo = dynamic_cast< BasicType * >( c_lo->get_type() ),
     102            *ty_hi = dynamic_cast< BasicType * >( c_hi->get_type() );
     103   
     104        if ( ! ty_lo || ! ty_hi )
     105            return; // one of them is not a constant
     106
     107        switch ( ty_lo->get_kind() ) {
     108          case BasicType::Char:
     109          case BasicType::UnsignedChar:
     110            switch ( ty_hi->get_kind() ){
     111                  case BasicType::Char:
     112                  case BasicType::UnsignedChar:
     113                    // first case, they are both printable ASCII characters represented as 'x'
     114                    if ( c_lo->get_value().size() == 3 && c_hi->get_value().size() == 3 ) {
     115                        char ch_lo = (c_lo->get_value())[1], ch_hi = (c_hi->get_value())[1];
     116
     117                        if ( ch_lo > ch_hi ) { char t=ch_lo; ch_lo=ch_hi; ch_hi=t; }
     118
     119                        for( char c = ch_lo; c <=  ch_hi; c++ ){
     120                            Type::Qualifiers q;
     121                            Constant cnst( new BasicType(q, BasicType::Char),
     122                                           std::string("'") + c + std::string("'") );
     123                            newCaseLabels.push_back( new ConstantExpr( cnst ) );
     124                        }
     125
     126                        return;
     127                    }
     128                    break;
     129                  default:
     130                    // error: incompatible constants
     131                    break;
     132                }
     133            break;
     134          case BasicType::ShortSignedInt:
     135          case BasicType::ShortUnsignedInt:
     136          case BasicType::SignedInt:
     137          case BasicType::UnsignedInt:
     138          case BasicType::LongSignedInt:
     139          case BasicType::LongUnsignedInt:
     140          case BasicType::LongLongSignedInt:
     141          case BasicType::LongLongUnsignedInt:
     142            switch ( ty_hi->get_kind() ) {
     143              case BasicType::ShortSignedInt:
     144              case BasicType::ShortUnsignedInt:
     145              case BasicType::SignedInt:
     146              case BasicType::UnsignedInt:
     147              case BasicType::LongSignedInt:
     148              case BasicType::LongUnsignedInt:
     149              case BasicType::LongLongSignedInt:
     150              case BasicType::LongLongUnsignedInt: {
     151                  int i_lo = atoi(c_lo->get_value().c_str()),
     152                      i_hi = atoi(c_hi->get_value().c_str());
     153
     154                  if ( i_lo > i_hi ) { int t=i_lo; i_lo=i_hi; i_hi=t; }
     155
     156                  for( int c = i_lo; c <=  i_hi; c++ ){
     157                      Type::Qualifiers q;
     158                      Constant cnst( new BasicType(q, ty_hi->get_kind()), // figure can't hurt (used to think in positives)
     159                                     toString< int >( c ) );
     160                      newCaseLabels.push_back( new ConstantExpr( cnst ) );
     161                  }
     162
     163                  return;
     164              }
     165              default:
     166                // error: incompatible constants
     167                break;
     168            }
     169            break;
     170          default:
     171            break;
     172        } // switch
     173
     174        /* End: */{
     175            // invalid range, signal a warning (it still generates the two case labels)
     176            newCaseLabels.push_back( lo );
     177            newCaseLabels.push_back( hi );
     178            return;
     179        }
     180    }
    190181} // namespace ControlStruct
  • translator/ControlStruct/CaseRangeMutator.h

    r3848e0e rd9a0e76  
    77
    88namespace ControlStruct {
     9    class CaseRangeMutator : public Mutator {
     10      public:
     11        CaseRangeMutator() {}
    912
    10   class CaseRangeMutator : public Mutator
    11   {
    12   public:
    13     CaseRangeMutator() {}
     13        virtual Statement *mutate( ChooseStmt * );
     14        virtual Statement *mutate( SwitchStmt * );
     15        virtual Statement *mutate( FallthruStmt * );
     16        virtual Statement *mutate( CaseStmt * );
     17      private:
     18        void fillRange( Expression *lo, Expression *hi );
    1419
    15     virtual Statement* mutate(ChooseStmt   *);
    16     virtual Statement* mutate(SwitchStmt   *);
    17     virtual Statement* mutate(FallthruStmt *);
    18     virtual Statement* mutate(CaseStmt     *);
    19 
    20   private:
    21     void fillRange(Expression *lo, Expression *hi);
    22 
    23     Expression *currentCondition;
    24     std::list< Expression * > newCaseLabels;
    25   };
     20        Expression *currentCondition;
     21        std::list< Expression * > newCaseLabels;
     22    };
    2623
    2724} // namespace ControlStruct
    2825
    29 #endif // #ifndef CASERNG_MUTATOR_H
     26#endif // CASERNG_MUTATOR_H
    3027
    3128/*
  • translator/ControlStruct/ChooseMutator.cc

    r3848e0e rd9a0e76  
    55
    66namespace ControlStruct {
    7 
    8   Statement* ChooseMutator::mutate(ChooseStmt   *chooseStmt)
    9   {
    10     bool enclosingChoose = insideChoose;
    11     insideChoose = true;
    12     mutateAll( chooseStmt->get_branches(), *this );
    13     insideChoose = enclosingChoose;
    14 
    15     return new SwitchStmt( chooseStmt->get_labels(),  chooseStmt->get_condition(), chooseStmt->get_branches() );
    16   }
    17 
    18   Statement* ChooseMutator::mutate(SwitchStmt   *switchStmt)
    19   {
    20     bool enclosingChoose = insideChoose;
    21     insideChoose = false;
    22     mutateAll( switchStmt->get_branches(), *this );
    23     insideChoose = enclosingChoose;
    24 
    25     return switchStmt;
    26   }
    27 
    28   Statement* ChooseMutator::mutate(FallthruStmt *fallthruStmt)
    29   {
    30     delete fallthruStmt;
    31     return new NullStmt();
    32   }
    33 
    34   Statement* ChooseMutator::mutate(CaseStmt *caseStmt)
    35   {
    36 
    37     std::list< Statement * > &stmts = caseStmt->get_statements();
    38 
    39     if ( insideChoose ) {
    40       BranchStmt *posBrk;
    41       if ( (( posBrk = dynamic_cast< BranchStmt * > ( stmts.back() ) ) &&
    42             ( posBrk->get_type() == BranchStmt::Break ))  // last statement in the list is a (superfluous) 'break'
    43            || dynamic_cast< FallthruStmt * > ( stmts.back() ) )
    44         ;
    45       else {
    46         stmts.push_back( new BranchStmt( std::list< Label >(), "", BranchStmt::Break ) );
    47       }
     7    Statement *ChooseMutator::mutate( ChooseStmt *chooseStmt) {
     8        bool enclosingChoose = insideChoose;
     9        insideChoose = true;
     10        mutateAll( chooseStmt->get_branches(), *this );
     11        insideChoose = enclosingChoose;
     12        return new SwitchStmt( chooseStmt->get_labels(),  chooseStmt->get_condition(), chooseStmt->get_branches() );
    4813    }
    4914
    50     mutateAll ( stmts, *this );
     15    Statement *ChooseMutator::mutate( SwitchStmt *switchStmt ) {
     16        bool enclosingChoose = insideChoose;
     17        insideChoose = false;
     18        mutateAll( switchStmt->get_branches(), *this );
     19        insideChoose = enclosingChoose;
     20        return switchStmt;
     21    }
    5122
    52     return caseStmt;
    53   }
     23    Statement *ChooseMutator::mutate( FallthruStmt *fallthruStmt ) {
     24        delete fallthruStmt;
     25        return new NullStmt();
     26    }
    5427
     28    Statement* ChooseMutator::mutate(CaseStmt *caseStmt) {
     29        std::list< Statement * > &stmts = caseStmt->get_statements();
     30
     31        if ( insideChoose ) {
     32            BranchStmt *posBrk;
     33            if ( (( posBrk = dynamic_cast< BranchStmt * > ( stmts.back() ) ) &&
     34                  ( posBrk->get_type() == BranchStmt::Break ))  // last statement in the list is a (superfluous) 'break'
     35                 || dynamic_cast< FallthruStmt * > ( stmts.back() ) )
     36                ;
     37            else {
     38                stmts.push_back( new BranchStmt( std::list< Label >(), "", BranchStmt::Break ) );
     39            } // if
     40        } // if
     41
     42        mutateAll ( stmts, *this );
     43        return caseStmt;
     44    }
    5545} // namespace ControlStruct
  • translator/ControlStruct/ChooseMutator.h

    r3848e0e rd9a0e76  
    88namespace ControlStruct {
    99
    10   class ChooseMutator : public Mutator
    11   {
    12   public:
    13     ChooseMutator() : insideChoose( false ) {}
     10    class ChooseMutator : public Mutator {
     11      public:
     12        ChooseMutator() : insideChoose( false ) {}
    1413
    15     virtual Statement* mutate(ChooseStmt   *);
    16     virtual Statement* mutate(SwitchStmt   *);
    17     virtual Statement* mutate(FallthruStmt *);
    18     virtual Statement* mutate(CaseStmt     *);
    19   private:
    20     bool insideChoose;
    21   };
    22 
     14        virtual Statement *mutate( ChooseStmt * );
     15        virtual Statement *mutate( SwitchStmt * );
     16        virtual Statement *mutate( FallthruStmt * );
     17        virtual Statement *mutate( CaseStmt * );
     18      private:
     19        bool insideChoose;
     20    };
    2321} // namespace ControlStruct
    2422
    25 #endif // #ifndef CHOOSE_MUTATOR_H
     23#endif // CHOOSE_MUTATOR_H
    2624
    2725/*
  • translator/ControlStruct/ForExprMutator.cc

    r3848e0e rd9a0e76  
    44
    55namespace ControlStruct {
    6   Statement* ForExprMutator::mutate(ForStmt *forStmt)
    7   {
    8     DeclStmt *decl;
    9     if (( decl = dynamic_cast< DeclStmt * > ( forStmt->get_initialization() )) != 0 )
    10       {
    11         // create compound statement, move declaration outside, leave _for_ as-is
    12         CompoundStmt *block = new CompoundStmt( std::list< Label >() );
    13         std::list<Statement *> &stmts = block->get_kids();
     6    Statement *ForExprMutator::mutate( ForStmt *forStmt ) {
     7        DeclStmt *decl;
     8        if (( decl = dynamic_cast< DeclStmt * > ( forStmt->get_initialization() )) != 0 ) {
     9            // create compound statement, move declaration outside, leave _for_ as-is
     10            CompoundStmt *block = new CompoundStmt( std::list< Label >() );
     11            std::list<Statement *> &stmts = block->get_kids();
    1412
    15         stmts.push_back( decl );
    16         forStmt->set_initialization( 0 );
    17         stmts.push_back( forStmt );
     13            stmts.push_back( decl );
     14            forStmt->set_initialization( 0 );
     15            stmts.push_back( forStmt );
    1816
    19         return block;
    20       }
    21     // ForStmt still needs to be fixed
    22     else
    23       return forStmt;
    24   }
    25 
     17            return block;
     18        } // if
     19        // ForStmt still needs to be fixed
     20        else
     21            return forStmt;
     22    }
    2623} // namespace ControlStruct
  • translator/ControlStruct/ForExprMutator.h

    r3848e0e rd9a0e76  
    77
    88namespace ControlStruct {
    9 
    10   class ForExprMutator : public Mutator
    11   {
    12   public:
    13     virtual Statement* mutate(ForStmt   *);
    14   };
    15 
     9    class ForExprMutator : public Mutator {
     10      public:
     11        virtual Statement *mutate( ForStmt * );
     12    };
    1613} // namespace ControlStruct
    1714
    18 #endif // #ifndef CHOOSE_MUTATOR_H
     15#endif // CHOOSE_MUTATOR_H
    1916
    2017/*
  • translator/ControlStruct/LabelFixer.cc

    r3848e0e rd9a0e76  
    99
    1010namespace ControlStruct {
    11   LabelFixer::Entry::Entry( Statement *to, Statement *from ) :
    12     definition ( to )
    13   {
    14     if ( from != 0 )
    15     usage.push_back( from );
    16   }
     11    LabelFixer::Entry::Entry( Statement *to, Statement *from ) : definition ( to ) {
     12        if ( from != 0 )
     13            usage.push_back( from );
     14    }
    1715
    18   bool LabelFixer::Entry::insideLoop()
    19   {
    20     return ( dynamic_cast< ForStmt * > ( definition ) ||
    21              dynamic_cast< WhileStmt * > ( definition )  );
    22   }
     16    bool LabelFixer::Entry::insideLoop() {
     17        return ( dynamic_cast< ForStmt * > ( definition ) ||
     18                 dynamic_cast< WhileStmt * > ( definition )  );
     19    }
    2320
    24   LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen )
    25   {
    26     if ( generator == 0 )
    27       generator = LabelGenerator::getGenerator();
    28   }
     21    LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) {
     22        if ( generator == 0 )
     23            generator = LabelGenerator::getGenerator();
     24    }
    2925
    30   void LabelFixer::visit(FunctionDecl *functionDecl)
    31   {
    32     if ( functionDecl->get_statements() != 0 )
    33       functionDecl->get_statements()->accept( *this );
     26    void LabelFixer::visit(FunctionDecl *functionDecl) {
     27        if ( functionDecl->get_statements() != 0 )
     28            functionDecl->get_statements()->accept( *this );
    3429
    35     MLEMutator mlemut( resolveJumps(), generator );
    36     functionDecl->acceptMutator( mlemut );
    37   }
     30        MLEMutator mlemut( resolveJumps(), generator );
     31        functionDecl->acceptMutator( mlemut );
     32    }
    3833
    39   void LabelFixer::visit(Statement *stmt )
    40   {
    41     std::list< Label > &labels = stmt->get_labels();
     34    void LabelFixer::visit(Statement *stmt ) {
     35        std::list< Label > &labels = stmt->get_labels();
    4236
    43     if ( ! labels.empty() ) {
    44       Label current = setLabelsDef( labels, stmt );
    45       labels.clear();
    46       labels.push_front( current );
     37        if ( ! labels.empty() ) {
     38            Label current = setLabelsDef( labels, stmt );
     39            labels.clear();
     40            labels.push_front( current );
     41        } // if
    4742    }
    48   }
    4943
    50   void LabelFixer::visit(BranchStmt *branchStmt)
    51   {
    52     visit ( ( Statement * )branchStmt );  // the labels this statement might have
     44    void LabelFixer::visit(BranchStmt *branchStmt) {
     45        visit ( ( Statement * )branchStmt );  // the labels this statement might have
    5346
    54     Label target;
    55     if ( (target = branchStmt->get_target()) != "" ) {
    56       setLabelsUsg( target, branchStmt );
    57     } //else       /* computed goto or normal exit-loop statements */
    58   }
     47        Label target;
     48        if ( (target = branchStmt->get_target()) != "" ) {
     49            setLabelsUsg( target, branchStmt );
     50        } //else       /* computed goto or normal exit-loop statements */
     51    }
    5952
    6053
    61   Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition )
    62   {
    63     assert( definition != 0 );
    64     Entry *entry = new Entry( definition );
    65     bool used = false;
     54    Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) {
     55        assert( definition != 0 );
     56        Entry *entry = new Entry( definition );
     57        bool used = false;
    6658
    67     for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ )
    68       if ( labelTable.find( *i ) == labelTable.end() )
    69         { used = true; labelTable[ *i ] = entry; } // undefined and unused
    70       else
    71         if( labelTable[ *i ]->defined() )
    72           throw SemanticError("Duplicate definition of label: " + *i );
    73         else
    74           labelTable[ *i ]->set_definition( definition );
     59        for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ )
     60            if ( labelTable.find( *i ) == labelTable.end() )
     61                { used = true; labelTable[ *i ] = entry; } // undefined and unused
     62            else
     63                if( labelTable[ *i ]->defined() )
     64                    throw SemanticError("Duplicate definition of label: " + *i );
     65                else
     66                    labelTable[ *i ]->set_definition( definition );
    7567
    76     if (! used ) delete entry;
     68        if (! used ) delete entry;
    7769
    78     return labelTable[ llabel.front() ]->get_label();  // this *must* exist
    79   }
    80 
    81   Label LabelFixer::setLabelsUsg( Label orgValue, Statement *use )
    82   {
    83     assert( use != 0 );
    84 
    85     if ( labelTable.find( orgValue ) != labelTable.end() )
    86       labelTable[ orgValue ]->add_use( use );         // the label has been defined or used before
    87     else
    88       labelTable[ orgValue ] = new Entry( 0, use );
    89 
    90     return labelTable[ orgValue ]->get_label();
    91   }
    92 
    93   std::map < Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError )
    94   {
    95     std::map < Statement *, Entry * > def_us;
    96 
    97     for( std::map < Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); i++ ) {
    98       Entry *e = i->second;
    99 
    100       if ( def_us.find ( e->get_definition() ) == def_us.end() )
    101           def_us[ e->get_definition() ] = e;
    102       else
    103         if(e->used())
    104           def_us[ e->get_definition() ]->add_uses( e->get_uses() );
     70        return labelTable[ llabel.front() ]->get_label();  // this *must* exist
    10571    }
    10672
    107     // get rid of labelTable
    108     for( std::map < Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) {
    109       Statement *to = (*i).first;
    110       std::list < Statement *> &from = (*i).second->get_uses();
    111       Label finalLabel = generator->newLabel();
    112       (*i).second->set_label( finalLabel );
     73    Label LabelFixer::setLabelsUsg( Label orgValue, Statement *use ) {
     74        assert( use != 0 );
    11375
    114       if ( to == 0 ) {
    115         BranchStmt *first_use = dynamic_cast<BranchStmt *>(from.back());
    116         Label undef("");
    117         if ( first_use != 0 )
    118           undef = first_use->get_target();
    119         throw SemanticError ( "'" + undef + "' label not defined");
    120       }
     76        if ( labelTable.find( orgValue ) != labelTable.end() )
     77            labelTable[ orgValue ]->add_use( use );         // the label has been defined or used before
     78        else
     79            labelTable[ orgValue ] = new Entry( 0, use );
    12180
    122       to->get_labels().clear();
    123       to->get_labels().push_back( finalLabel );
    124 
    125       for ( std::list< Statement *>::iterator j = from.begin(); j != from.end(); j++ ) {
    126         BranchStmt *jumpTo = dynamic_cast < BranchStmt * > ( *j );
    127         assert( jumpTo != 0 );
    128         jumpTo->set_target( finalLabel );
    129       }
     81        return labelTable[ orgValue ]->get_label();
    13082    }
    13183
    132     // reverse table
    133     std::map < Label, Statement * > *ret = new std::map < Label, Statement * >();
    134     for(std::map < Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ )
    135       (*ret)[ (*i).second->get_label() ] = (*i).first;
     84    std::map < Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) {
     85        std::map < Statement *, Entry * > def_us;
    13686
    137     return ret;
    138   }
     87        for ( std::map < Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); i++ ) {
     88            Entry *e = i->second;
    13989
     90            if ( def_us.find ( e->get_definition() ) == def_us.end() )
     91                def_us[ e->get_definition() ] = e;
     92            else
     93                if(e->used())
     94                    def_us[ e->get_definition() ]->add_uses( e->get_uses() );
     95        }
     96
     97        // get rid of labelTable
     98        for ( std::map < Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) {
     99            Statement *to = (*i).first;
     100            std::list < Statement *> &from = (*i).second->get_uses();
     101            Label finalLabel = generator->newLabel();
     102            (*i).second->set_label( finalLabel );
     103
     104            if ( to == 0 ) {
     105                BranchStmt *first_use = dynamic_cast<BranchStmt *>(from.back());
     106                Label undef("");
     107                if ( first_use != 0 )
     108                    undef = first_use->get_target();
     109                throw SemanticError ( "'" + undef + "' label not defined");
     110            }
     111
     112            to->get_labels().clear();
     113            to->get_labels().push_back( finalLabel );
     114
     115            for ( std::list< Statement *>::iterator j = from.begin(); j != from.end(); j++ ) {
     116                BranchStmt *jumpTo = dynamic_cast < BranchStmt * > ( *j );
     117                assert( jumpTo != 0 );
     118                jumpTo->set_target( finalLabel );
     119            } // for
     120        } // for
     121
     122        // reverse table
     123        std::map < Label, Statement * > *ret = new std::map < Label, Statement * >();
     124        for (std::map < Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ )
     125            (*ret)[ (*i).second->get_label() ] = (*i).first;
     126
     127        return ret;
     128    }
    140129}  // namespace ControlStruct
  • translator/ControlStruct/LabelFixer.h

    r3848e0e rd9a0e76  
    1212
    1313namespace ControlStruct {
     14    class LabelFixer : public Visitor {
     15        typedef Visitor Parent;
     16      public:
     17        LabelFixer( LabelGenerator *gen = 0 );
    1418
    15   class LabelFixer : public Visitor
    16     {
    17       typedef Visitor Parent;
     19        std::map < Label, Statement * > *resolveJumps() throw ( SemanticError );
    1820
    19     public:
    20       LabelFixer( LabelGenerator *gen = 0 );
     21        // Declarations
     22        virtual void visit( FunctionDecl *functionDecl );
    2123
    22       std::map < Label, Statement * > *resolveJumps() throw ( SemanticError );
     24        // Statements
     25        void visit( Statement *stmt );
    2326
    24       // Declarations
    25       virtual void visit(FunctionDecl *functionDecl);
     27        virtual void visit( CompoundStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     28        virtual void visit( NullStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     29        virtual void visit( ExprStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     30        virtual void visit( IfStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     31        virtual void visit( WhileStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     32        virtual void visit( ForStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     33        virtual void visit( SwitchStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     34        virtual void visit( ChooseStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     35        virtual void visit( FallthruStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     36        virtual void visit( CaseStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     37        virtual void visit( ReturnStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     38        virtual void visit( TryStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     39        virtual void visit( CatchStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     40        virtual void visit( DeclStmt *stmt ) { visit( (Statement *)stmt ); return Parent::visit( stmt ); }
     41        virtual void visit( BranchStmt *branchStmt );
    2642
    27       // Statements
    28       void visit(Statement *stmt);
     43        Label setLabelsDef( std::list< Label > &, Statement *definition );
     44        Label setLabelsUsg( Label, Statement *usage = 0 );
    2945
    30       virtual void visit(CompoundStmt *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    31       virtual void visit(NullStmt     *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    32       virtual void visit(ExprStmt     *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    33       virtual void visit(IfStmt       *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    34       virtual void visit(WhileStmt    *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    35       virtual void visit(ForStmt      *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    36       virtual void visit(SwitchStmt   *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    37       virtual void visit(ChooseStmt   *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    38       virtual void visit(FallthruStmt *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    39       virtual void visit(CaseStmt     *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    40       virtual void visit(ReturnStmt   *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    41       virtual void visit(TryStmt      *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    42       virtual void visit(CatchStmt    *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    43       virtual void visit(DeclStmt     *stmt) {  visit( (Statement *)stmt ); return Parent::visit(stmt); }
    44       virtual void visit(BranchStmt   *branchStmt);
     46      private:
     47        class Entry {
     48          public:
     49            Entry( Statement *to = 0, Statement *from = 0 );
     50            bool used() { return ( usage.empty() ); }
     51            bool defined() { return ( definition != 0 ); }
     52            bool insideLoop();
    4553
    46       Label setLabelsDef( std::list< Label > &, Statement *definition );
    47       Label setLabelsUsg( Label, Statement *usage = 0 );
     54            Label get_label() const { return label; }
     55            Statement *get_definition() const { return definition; }
     56            std::list< Statement *> &get_uses() { return usage; }
    4857
    49     private:
    50       class Entry
    51       {
    52       public:
    53         Entry( Statement *to = 0, Statement *from = 0 );
    54         bool used() { return ( usage.empty() ); }
    55         bool defined() { return ( definition != 0 ); }
    56         bool insideLoop();
     58            void add_use ( Statement *use ) { usage.push_back( use ); }
     59            void add_uses ( std::list<Statement *> uses ) { usage.insert( usage.end(), uses.begin(), uses.end() ); }
     60            void set_definition( Statement *def ) { definition = def; }
    5761
    58         Label get_label() const { return label; }
    59         Statement *get_definition() const { return definition; }
    60         std::list< Statement *> &get_uses() { return usage; }
    61 
    62         void add_use ( Statement *use ) { usage.push_back(use); }
    63         void add_uses ( std::list<Statement *> uses ) { usage.insert( usage.end(), uses.begin(), uses.end() ); }
    64         void set_definition( Statement *def ) { definition = def; }
    65 
    66         void set_label( Label lab ) { label = lab; }
    67         Label gset_label() const { return label; }
    68       private:
    69         Label label; 
    70         Statement *definition;
    71         std::list<Statement *> usage;
    72       };
     62            void set_label( Label lab ) { label = lab; }
     63            Label gset_label() const { return label; }
     64          private:
     65            Label label; 
     66            Statement *definition;
     67            std::list<Statement *> usage;
     68        };
    7369             
    74       std::map < Label, Entry *> labelTable;
    75       LabelGenerator *generator;
     70        std::map < Label, Entry *> labelTable;
     71        LabelGenerator *generator;
    7672    };
    7773} // namespace ControlStruct
    7874
    79 #endif // #ifndef LABEL_FIXER_H
     75#endif // LABEL_FIXER_H
    8076
    8177/*
  • translator/ControlStruct/LabelGenerator.cc

    r3848e0e rd9a0e76  
    55
    66namespace ControlStruct {
     7    LabelGenerator *LabelGenerator::labelGenerator = 0;
    78
    8   LabelGenerator *LabelGenerator::labelGenerator = 0;
     9    LabelGenerator *LabelGenerator::getGenerator() {
     10        if ( LabelGenerator::labelGenerator == 0 )
     11            LabelGenerator::labelGenerator = new LabelGenerator();
    912
    10   LabelGenerator *LabelGenerator::getGenerator() {
    11     if ( LabelGenerator::labelGenerator == 0 )
    12       LabelGenerator::labelGenerator = new LabelGenerator();
     13        return labelGenerator;
     14    }
    1315
    14     return labelGenerator;
    15   }
    16 
    17   Label LabelGenerator::newLabel() {
    18     std::ostrstream os;
    19     os << "__L" << current++ << "__";// << std::ends;
    20     os.freeze( false );
    21     std::string ret = std::string (os.str(), os.pcount());
    22     return Label( ret );
    23   }
    24 
     16    Label LabelGenerator::newLabel() {
     17        std::ostrstream os;
     18        os << "__L" << current++ << "__";// << std::ends;
     19        os.freeze( false );
     20        std::string ret = std::string (os.str(), os.pcount());
     21        return Label( ret );
     22    }
    2523} // namespace ControlStruct
  • translator/ControlStruct/LabelGenerator.h

    r3848e0e rd9a0e76  
    55
    66namespace ControlStruct {
    7 
    8   class LabelGenerator
    9   {
    10   public:
    11     static LabelGenerator *getGenerator();
    12     Label newLabel();
    13     void reset() { current = 0; }
    14     void rewind() { current--; }
    15 
    16   protected:
    17     LabelGenerator(): current(0) {}
    18 
    19   private:
    20     int current;
    21     static LabelGenerator *labelGenerator;
    22   };
    23 
     7    class LabelGenerator {
     8      public:
     9        static LabelGenerator *getGenerator();
     10        Label newLabel();
     11        void reset() { current = 0; }
     12        void rewind() { current--; }
     13      protected:
     14        LabelGenerator(): current(0) {}
     15      private:
     16        int current;
     17        static LabelGenerator *labelGenerator;
     18    };
    2419} // namespace ControlStruct
    2520
    26 #endif // #ifndef LABEL_GENERATOR_H
     21#endif // LABEL_GENERATOR_H
    2722
    2823/*
  • translator/ControlStruct/LabelTypeChecker.cc

    r3848e0e rd9a0e76  
    1111
    1212namespace ControlStruct {
     13    void LabelTypeChecker::visit(UntypedExpr *untypedExpr){
     14        assert( untypedExpr != 0 );
     15        NameExpr *fname;
     16        if ( ((fname = dynamic_cast<NameExpr *>(untypedExpr->get_function())) != 0)
     17            && fname->get_name() == std::string("LabAddress") )
     18            std::cerr << "Taking the label of an address." << std::endl;
     19        else {
     20            acceptAll( untypedExpr->get_results(), *this );
     21            acceptAll( untypedExpr->get_args(), *this );
     22        } // if
     23        return;
     24    }
    1325
    14   void LabelTypeChecker::visit(UntypedExpr *untypedExpr){
    15     assert( untypedExpr != 0 );
    16     NameExpr *fname;
    17     if( ((fname = dynamic_cast<NameExpr *>(untypedExpr->get_function())) != 0)
    18         && fname->get_name() == std::string("LabAddress") )
    19       std::cerr << "Taking the label of an address." << std::endl;
    20     else {
    21       acceptAll( untypedExpr->get_results(), *this );
    22       acceptAll( untypedExpr->get_args(), *this );
     26    void LabelTypeChecker::visit(CompoundStmt *compoundStmt) {
     27        index.enterScope();
     28        acceptAll( compoundStmt->get_kids(), *this );
     29        index.leaveScope();
    2330    }
    24     return;
    25   }
    2631
    27   void LabelTypeChecker::visit(CompoundStmt *compoundStmt) {
    28     index.enterScope();
    29     acceptAll( compoundStmt->get_kids(), *this );
    30     index.leaveScope();
    31   }
     32    void LabelTypeChecker::visit(DeclStmt *declStmt){
     33        declStmt->accept( index );
    3234
    33   void LabelTypeChecker::visit(DeclStmt *declStmt){
    34     declStmt->accept( index );
     35        //ObjectDecl *odecl = 0;
     36        // if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ){
     37        return;
     38    }
    3539
    36     //ObjectDecl *odecl = 0;
    37     // if( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ){
    38     return;
    39   }
     40    void LabelTypeChecker::visit(BranchStmt *branchStmt) {
     41        if ( branchStmt->get_type() != BranchStmt::Goto ) return;
     42        Expression *target;
     43        if ( (target = branchStmt->get_computedTarget()) == 0 ) return;
    4044
    41   void LabelTypeChecker::visit(BranchStmt *branchStmt) {
    42     if( branchStmt->get_type() != BranchStmt::Goto ) return;
    43     Expression *target;
    44     if( (target = branchStmt->get_computedTarget()) == 0 ) return;
     45        NameExpr *name;
     46        if ( ((name = dynamic_cast<NameExpr *>(target)) == 0) )
     47            return; // Not a name expression
     48   
     49        std::list< DeclarationWithType * > interps;
     50        index.lookupId(name->get_name(), interps);
     51        if ( interps.size() != 1)
     52            // in case of multiple declarations
     53            throw SemanticError("Illegal label expression: " + name->get_name() );
    4554
    46     NameExpr *name;
    47     if( ((name = dynamic_cast<NameExpr *>(target)) == 0) )
    48       return; // Not a name expression
    49    
    50     std::list< DeclarationWithType * > interps;
    51     index.lookupId(name->get_name(), interps);
    52     if ( interps.size() != 1)
    53       // in case of multiple declarations
    54       throw SemanticError("Illegal label expression: " + name->get_name() );
     55        PointerType *ptr;
     56        if ( (ptr = dynamic_cast<PointerType *>(interps.front()->get_type())) != 0 )
     57            if ( dynamic_cast<VoidType *>(ptr->get_base()) != 0 )
     58                return;
     59            else
     60                throw SemanticError("Wrong type of parameter for computed goto");
     61        else
     62            throw SemanticError("Wrong type of parameter for computed goto");
    5563
    56     PointerType *ptr;
    57     if ( (ptr = dynamic_cast<PointerType *>(interps.front()->get_type())) != 0 )
    58       if ( dynamic_cast<VoidType *>(ptr->get_base()) != 0 )
    5964        return;
    60       else
    61         throw SemanticError("Wrong type of parameter for computed goto");
    62     else
    63         throw SemanticError("Wrong type of parameter for computed goto");
    64 
    65     return;
    66   }
     65    }
    6766} // namespace ControlStruct
    68 
    69 
    70 
    71 
    72 
  • translator/ControlStruct/LabelTypeChecker.h

    r3848e0e rd9a0e76  
    99
    1010namespace ControlStruct {
     11    class LabelTypeChecker : public Visitor {
     12      public:
     13        //LabelTypeChecker() {
    1114
    12   class LabelTypeChecker : public Visitor
    13   {
    14   public:
    15     //LabelTypeChecker() {
    16 
    17     virtual void visit(CompoundStmt *compoundStmt);
    18     virtual void visit(DeclStmt *declStmt);
    19     virtual void visit(BranchStmt *branchStmt);
    20     virtual void visit(UntypedExpr *untypedExpr);
    21   private:
    22     SymTab::Indexer index;
    23   };
    24 
     15        virtual void visit( CompoundStmt *compoundStmt );
     16        virtual void visit( DeclStmt *declStmt );
     17        virtual void visit( BranchStmt *branchStmt );
     18        virtual void visit( UntypedExpr *untypedExpr );
     19      private:
     20        SymTab::Indexer index;
     21    };
    2522} // namespace ControlStruct
    2623
    27 #endif // #ifndef LABEL_TYPE_H
     24#endif // LABEL_TYPE_H
    2825
    2926/*
  • translator/ControlStruct/MLEMutator.cc

    r3848e0e rd9a0e76  
    77
    88namespace ControlStruct {
    9   MLEMutator::~MLEMutator()
    10   {
    11     delete targetTable;
    12     targetTable = 0;
    13   }
    14 
    15   CompoundStmt* MLEMutator::mutate(CompoundStmt *cmpndStmt)
    16   {
    17     bool labeledBlock = false;
    18     if (!((cmpndStmt->get_labels()).empty())) {
    19       labeledBlock = true;
    20       enclosingBlocks.push_back( Entry( cmpndStmt ) );
     9    MLEMutator::~MLEMutator() {
     10        delete targetTable;
     11        targetTable = 0;
    2112    }
    2213
    23     std::list< Statement * > &kids = cmpndStmt->get_kids();
    24     for( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
    25       *k = (*k)->acceptMutator(*this);
     14    CompoundStmt* MLEMutator::mutate(CompoundStmt *cmpndStmt) {
     15        bool labeledBlock = false;
     16        if ( !((cmpndStmt->get_labels()).empty()) ) {
     17            labeledBlock = true;
     18            enclosingBlocks.push_back( Entry( cmpndStmt ) );
     19        }
    2620
    27       if (!get_breakLabel().empty()) {
    28         std::list< Statement * >::iterator next = k; next++;
    29         if( next == kids.end() ) {
    30           std::list<Label> ls; ls.push_back( get_breakLabel() );
    31           kids.push_back( new NullStmt(ls) );
    32         } else
    33           (*next)->get_labels().push_back( get_breakLabel() );
     21        std::list< Statement * > &kids = cmpndStmt->get_kids();
     22        for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
     23            *k = (*k)->acceptMutator(*this);
    3424
    35         set_breakLabel("");
    36       }
     25            if ( !get_breakLabel().empty() ) {
     26                std::list< Statement * >::iterator next = k; next++;
     27                if ( next == kids.end() ) {
     28                    std::list<Label> ls; ls.push_back( get_breakLabel() );
     29                    kids.push_back( new NullStmt(ls) );
     30                } else
     31                    (*next)->get_labels().push_back( get_breakLabel() );
     32
     33                set_breakLabel("");
     34            } // if
     35        } // for
     36
     37        if ( labeledBlock ) {
     38            assert( ! enclosingBlocks.empty() );
     39            if ( ! enclosingBlocks.back().get_breakExit().empty() )
     40                set_breakLabel( enclosingBlocks.back().get_breakExit() );
     41            enclosingBlocks.pop_back();
     42        } // if
     43
     44        //mutateAll( cmpndStmt->get_kids(), *this );
     45        return cmpndStmt;
    3746    }
    3847
    39     if ( labeledBlock ) {
    40       assert( ! enclosingBlocks.empty() );
    41       if( ! enclosingBlocks.back().get_breakExit().empty() )
    42         set_breakLabel( enclosingBlocks.back().get_breakExit() );
    43       enclosingBlocks.pop_back();
     48    Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
     49        enclosingLoops.push_back( Entry( whileStmt ) );
     50        whileStmt->set_body ( whileStmt->get_body()->acceptMutator( *this ) );
     51
     52        Entry &e = enclosingLoops.back();
     53        assert ( e == whileStmt );
     54        whileStmt->set_body( mutateLoop( whileStmt->get_body(), e ) );
     55        enclosingLoops.pop_back();
     56
     57        return whileStmt;
    4458    }
    4559
    46     //mutateAll( cmpndStmt->get_kids(), *this );
    47     return cmpndStmt;
    48   }
     60    Statement *MLEMutator::mutate( ForStmt *forStmt ) {
     61        enclosingLoops.push_back( Entry( forStmt ) );
     62        maybeMutate( forStmt->get_body(), *this );
    4963
    50   Statement *MLEMutator::mutate( WhileStmt *whileStmt )
    51   {
    52     enclosingLoops.push_back( Entry( whileStmt ) );
    53     whileStmt->set_body ( whileStmt->get_body()->acceptMutator( *this ) );
     64        Entry &e = enclosingLoops.back();
     65        assert ( e == forStmt );
     66        forStmt->set_body( mutateLoop( forStmt->get_body(), e ) );
     67        enclosingLoops.pop_back();
    5468
    55     Entry &e = enclosingLoops.back();
    56     assert ( e == whileStmt );
    57     whileStmt->set_body( mutateLoop( whileStmt->get_body(), e ) );
    58     enclosingLoops.pop_back();
    59 
    60     return whileStmt;
    61   }
    62 
    63   Statement *MLEMutator::mutate( ForStmt *forStmt )
    64   {
    65     enclosingLoops.push_back( Entry( forStmt ) );
    66     maybeMutate( forStmt->get_body(), *this );
    67 
    68     Entry &e = enclosingLoops.back();
    69     assert ( e == forStmt );
    70     forStmt->set_body( mutateLoop( forStmt->get_body(), e ) );
    71     enclosingLoops.pop_back();
    72 
    73     return forStmt;
    74   }
    75 
    76   Statement *MLEMutator::mutate( BranchStmt *branchStmt )
    77     throw ( SemanticError )
    78   {
    79     if ( branchStmt->get_type() == BranchStmt::Goto )
    80       return branchStmt;
    81 
    82     // test if continue target is a loop
    83     if ( branchStmt->get_type() == BranchStmt::Continue && enclosingLoops.empty() )
    84       throw SemanticError( "'continue' outside a loop" );
    85 
    86   if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) )
    87       throw SemanticError( "'break' outside a loop or switch" );
    88 
    89     if ( branchStmt->get_target() == "" ) return branchStmt;
    90 
    91     if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() )
    92       throw SemanticError("The label defined in the exit loop statement does not exist." );  // shouldn't happen (since that's already checked)
    93 
    94     std::list< Entry >::iterator check;
    95     if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() )
    96       // not in loop, checking if in switch/choose
    97       if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() )
    98         // neither in loop nor in block, checking if in switch/choose
    99         if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() )
    100           throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing loop.");
    101 
    102     if ( enclosingLoops.back() == (*check) )
    103       return branchStmt;                      // exit the innermost loop (labels not necessary)
    104 
    105     Label newLabel;
    106     switch( branchStmt->get_type() ) {
    107     case BranchStmt::Break:
    108       if ( check->get_breakExit() != "" )
    109         newLabel = check->get_breakExit();
    110       else
    111         { newLabel = generator->newLabel(); check->set_breakExit( newLabel ); }
    112       break;
    113     case BranchStmt::Continue:
    114       if ( check->get_contExit() != "" )
    115         newLabel = check->get_contExit();
    116       else
    117         { newLabel = generator->newLabel(); check->set_contExit( newLabel ); }
    118       break;
    119 
    120     default:
    121       // shouldn't be here
    122       return 0;
     69        return forStmt;
    12370    }
    12471
    125     return new BranchStmt(std::list<Label>(), newLabel, BranchStmt::Goto );
    126   }
     72    Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
     73        if ( branchStmt->get_type() == BranchStmt::Goto )
     74            return branchStmt;
     75
     76        // test if continue target is a loop
     77        if ( branchStmt->get_type() == BranchStmt::Continue && enclosingLoops.empty() )
     78            throw SemanticError( "'continue' outside a loop" );
     79
     80        if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) )
     81            throw SemanticError( "'break' outside a loop or switch" );
     82
     83        if ( branchStmt->get_target() == "" ) return branchStmt;
     84
     85        if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() )
     86            throw SemanticError("The label defined in the exit loop statement does not exist." );  // shouldn't happen (since that's already checked)
     87
     88        std::list< Entry >::iterator check;
     89        if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() )
     90            // not in loop, checking if in switch/choose
     91            if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() )
     92                // neither in loop nor in block, checking if in switch/choose
     93                if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() )
     94                    throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing loop.");
     95
     96        if ( enclosingLoops.back() == (*check) )
     97            return branchStmt;                      // exit the innermost loop (labels not necessary)
     98
     99        Label newLabel;
     100        switch( branchStmt->get_type() ) {
     101          case BranchStmt::Break:
     102            if ( check->get_breakExit() != "" )
     103                newLabel = check->get_breakExit();
     104            else { newLabel = generator->newLabel(); check->set_breakExit( newLabel ); }
     105            break;
     106          case BranchStmt::Continue:
     107            if ( check->get_contExit() != "" )
     108                newLabel = check->get_contExit();
     109            else { newLabel = generator->newLabel(); check->set_contExit( newLabel ); }
     110            break;
     111          default:
     112            // shouldn't be here
     113            return 0;
     114        } // switch
     115
     116        return new BranchStmt(std::list<Label>(), newLabel, BranchStmt::Goto );
     117    }
    127118
    128119
    129   Statement *MLEMutator::mutate(SwitchStmt *switchStmt)
    130   {
    131     Label brkLabel = generator->newLabel();
    132     enclosingSwitches.push_back( Entry(switchStmt, "", brkLabel) );
    133     mutateAll( switchStmt->get_branches(), *this );
    134     {
    135       // check if this is necessary (if there is a break to this point, otherwise do not generate
    136       std::list<Label> temp; temp.push_back( brkLabel );
    137       switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
    138     }
    139     assert ( enclosingSwitches.back() == switchStmt );
    140     enclosingSwitches.pop_back();
    141     return switchStmt;
    142   }
    143 
    144   Statement *MLEMutator::mutate(ChooseStmt *switchStmt)
    145   {
    146     Label brkLabel = generator->newLabel();
    147     enclosingSwitches.push_back( Entry(switchStmt,"", brkLabel) );
    148     mutateAll( switchStmt->get_branches(), *this );
    149     {
    150       // check if this is necessary (if there is a break to this point, otherwise do not generate
    151       std::list<Label> temp; temp.push_back( brkLabel );
    152       switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
    153     }
    154     assert ( enclosingSwitches.back() == switchStmt );
    155     enclosingSwitches.pop_back();
    156     return switchStmt;
    157   }
    158 
    159   Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
    160     CompoundStmt *newBody;
    161     if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
    162       newBody = new CompoundStmt( std::list< Label >() );
    163       newBody->get_kids().push_back( bodyLoop );
     120    Statement *MLEMutator::mutate(SwitchStmt *switchStmt) {
     121        Label brkLabel = generator->newLabel();
     122        enclosingSwitches.push_back( Entry(switchStmt, "", brkLabel) );
     123        mutateAll( switchStmt->get_branches(), *this ); {
     124            // check if this is necessary (if there is a break to this point, otherwise do not generate
     125            std::list<Label> temp; temp.push_back( brkLabel );
     126            switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
     127        }
     128        assert ( enclosingSwitches.back() == switchStmt );
     129        enclosingSwitches.pop_back();
     130        return switchStmt;
    164131    }
    165132
    166     Label endLabel = e.get_contExit();
    167 
    168     if ( e.get_breakExit() != "" ) {
    169       if ( endLabel == "" ) endLabel = generator->newLabel();
    170       // check for whether this label is used or not, so as to not generate extraneous gotos
    171       if (e.breakExitUsed)
    172         newBody->get_kids().push_back( new BranchStmt( std::list< Label >(), endLabel, BranchStmt::Goto ) );
    173       // xxx
    174       //std::list< Label > ls; ls.push_back( e.get_breakExit() );
    175       set_breakLabel( e.get_breakExit() );
    176       //newBody->get_kids().push_back( new BranchStmt( ls, "", BranchStmt::Break ) );
     133    Statement *MLEMutator::mutate(ChooseStmt *switchStmt) {
     134        Label brkLabel = generator->newLabel();
     135        enclosingSwitches.push_back( Entry(switchStmt,"", brkLabel) );
     136        mutateAll( switchStmt->get_branches(), *this ); {
     137            // check if this is necessary (if there is a break to this point, otherwise do not generate
     138            std::list<Label> temp; temp.push_back( brkLabel );
     139            switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
     140        }
     141        assert ( enclosingSwitches.back() == switchStmt );
     142        enclosingSwitches.pop_back();
     143        return switchStmt;
    177144    }
    178145
    179     if ( e.get_breakExit() != "" || e.get_contExit() != "" ){
    180       if(dynamic_cast< NullStmt *>( newBody->get_kids().back() ))
    181         newBody->get_kids().back()->get_labels().push_back( endLabel );
    182       else {
    183         std::list < Label > ls; ls.push_back( endLabel );
    184         newBody->get_kids().push_back( new NullStmt( ls ) );
    185       }
     146    Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
     147        CompoundStmt *newBody;
     148        if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
     149            newBody = new CompoundStmt( std::list< Label >() );
     150            newBody->get_kids().push_back( bodyLoop );
     151        } // if
     152
     153        Label endLabel = e.get_contExit();
     154
     155        if ( e.get_breakExit() != "" ) {
     156            if ( endLabel == "" ) endLabel = generator->newLabel();
     157            // check for whether this label is used or not, so as to not generate extraneous gotos
     158            if (e.breakExitUsed)
     159                newBody->get_kids().push_back( new BranchStmt( std::list< Label >(), endLabel, BranchStmt::Goto ) );
     160            // xxx
     161            //std::list< Label > ls; ls.push_back( e.get_breakExit() );
     162            set_breakLabel( e.get_breakExit() );
     163            //newBody->get_kids().push_back( new BranchStmt( ls, "", BranchStmt::Break ) );
     164        } // if
     165
     166        if ( e.get_breakExit() != "" || e.get_contExit() != "" ){
     167            if (dynamic_cast< NullStmt *>( newBody->get_kids().back() ))
     168                newBody->get_kids().back()->get_labels().push_back( endLabel );
     169            else {
     170                std::list < Label > ls; ls.push_back( endLabel );
     171                newBody->get_kids().push_back( new NullStmt( ls ) );
     172            } // if
     173        } // if
     174
     175        return newBody;
    186176    }
    187177
    188     return newBody;
    189   }
     178    //*** Entry's methods
     179    void MLEMutator::Entry::set_contExit( Label l ) {
     180        assert ( contExit == "" || contExit == l );
     181        contExit = l;
     182    }
    190183
    191   //*** Entry's methods
    192   void MLEMutator::Entry::set_contExit( Label l )
    193   {
    194     assert ( contExit == "" || contExit == l );
    195     contExit = l;
    196   }
    197 
    198   void MLEMutator::Entry::set_breakExit( Label l )
    199   {
    200     assert ( breakExit == "" || breakExit == l );
    201     breakExit = l;
    202   }
    203 
     184    void MLEMutator::Entry::set_breakExit( Label l ) {
     185        assert ( breakExit == "" || breakExit == l );
     186        breakExit = l;
     187    }
    204188} // namespace ControlStruct
  • translator/ControlStruct/MLEMutator.h

    r3848e0e rd9a0e76  
    1212
    1313namespace ControlStruct {
     14    class MLEMutator : public Mutator {
     15        class Entry;
     16      public:
     17        MLEMutator( std::map <Label, Statement *> *t, LabelGenerator *gen = 0 ) : targetTable( t ), breakLabel(std::string("")), generator( gen ) {}
     18        ~MLEMutator();
    1419
    15   class MLEMutator : public Mutator {
    16     class Entry;
     20        CompoundStmt *mutate( CompoundStmt *cmpndStmt );
     21        Statement *mutate( WhileStmt *whileStmt );
     22        Statement *mutate( ForStmt *forStmt );
     23        Statement *mutate( BranchStmt *branchStmt ) throw ( SemanticError );
    1724
    18   public:
    19     MLEMutator( std::map <Label, Statement *> *t, LabelGenerator *gen = 0 ) : targetTable( t ), breakLabel(std::string("")), generator( gen ) {}
    20     ~MLEMutator();
     25        Statement *mutate( SwitchStmt *switchStmt );
     26        Statement *mutate( ChooseStmt *switchStmt );
    2127
    22     CompoundStmt *mutate( CompoundStmt *cmpndStmt );
    23     Statement *mutate( WhileStmt *whileStmt );
    24     Statement *mutate( ForStmt *forStmt );
    25     Statement *mutate( BranchStmt *branchStmt ) throw ( SemanticError );
     28        Statement *mutateLoop( Statement *bodyLoop, Entry &e );
    2629
    27     Statement *mutate( SwitchStmt *switchStmt );
    28     Statement *mutate( ChooseStmt *switchStmt );
     30        Label &get_breakLabel() { return breakLabel; }
     31        void set_breakLabel( Label newValue ) { breakLabel = newValue; }
     32      private:
     33        class Entry {
     34          public:
     35            explicit Entry( Statement *_loop = 0, Label _contExit = Label(""), Label _breakExit = Label("") ) :
     36                loop( _loop ), contExit( _contExit ), breakExit( _breakExit ), contExitUsed( false ), breakExitUsed( false ) {}
    2937
    30     Statement *mutateLoop( Statement *bodyLoop, Entry &e );
     38            bool operator==( const Statement *stmt ) { return ( loop == stmt ); }
     39            bool operator!=( const Statement *stmt ) { return ( loop != stmt ); }
    3140
    32     Label &get_breakLabel() { return breakLabel; }
    33     void set_breakLabel( Label newValue ) { breakLabel = newValue; }
     41            bool operator==( const Entry &other ) { return ( loop == other.get_loop() ); }
    3442
    35   private:
    36     class Entry {
    37     public:
    38       explicit Entry( Statement *_loop = 0, Label _contExit = Label(""), Label _breakExit = Label("") ) :
    39         loop( _loop ), contExit( _contExit ), breakExit( _breakExit ), contExitUsed( false ), breakExitUsed( false ) {}
     43            Statement *get_loop() const { return loop; }
    4044
    41       bool operator==( const Statement *stmt ) { return ( loop == stmt ) ; }
    42       bool operator!=( const Statement *stmt ) { return ( loop != stmt ) ; }
     45            Label get_contExit() const { return contExit; }
     46            void set_contExit( Label );
    4347
    44       bool operator==( const Entry &other ) { return ( loop == other.get_loop() ) ; }
     48            Label get_breakExit() const { return breakExit; }
     49            void set_breakExit( Label );
    4550
    46       Statement *get_loop() const { return loop; }
     51          private:
     52            Statement *loop;
     53            Label contExit, breakExit;
     54          public: // hack, provide proper [sg]etters
     55            bool contExitUsed, breakExitUsed;
     56        };
    4757
    48       Label get_contExit() const { return contExit; }
    49       void set_contExit( Label );
    50 
    51       Label get_breakExit() const { return breakExit; }
    52       void set_breakExit( Label );
    53 
    54     private:
    55       Statement *loop;
    56       Label contExit, breakExit;
    57     public: // hack, provide proper [sg]etters
    58       bool contExitUsed, breakExitUsed;
     58        std::map <Label, Statement *> *targetTable;
     59        std::list < Entry > enclosingBlocks,enclosingLoops,enclosingSwitches;
     60        Label breakLabel;
     61        LabelGenerator *generator;
    5962    };
    60 
    61     std::map <Label, Statement *> *targetTable;
    62     std::list < Entry > enclosingBlocks,enclosingLoops,enclosingSwitches;
    63     Label breakLabel;
    64     LabelGenerator *generator;
    65   };
    6663
    6764} // namespace ControlStruct
  • translator/ControlStruct/Mutate.cc

    r3848e0e rd9a0e76  
    2020
    2121namespace ControlStruct {
     22    void mutate( std::list< Declaration * > translationUnit ) {
     23        ChooseMutator chmut;
     24        ForExprMutator formut;
     25        CaseRangeMutator ranges;  // has to run after ChooseMutator
     26        LabelFixer lfix;
     27        //ExceptMutator exc;
     28        LabelTypeChecker lbl;
    2229
    23   void mutate( std::list< Declaration * > translationUnit )
    24   {
    25     ChooseMutator chmut;
    26     ForExprMutator formut;
    27     CaseRangeMutator ranges;  // has to run after ChooseMutator
    28     LabelFixer lfix;
    29     //ExceptMutator exc;
    30     LabelTypeChecker lbl;
    31 
    32     mutateAll( translationUnit , formut );
    33     acceptAll( translationUnit , lfix );
    34     mutateAll( translationUnit , chmut );
    35     mutateAll( translationUnit , ranges );
    36     //mutateAll( translationUnit , exc );
    37     //acceptAll( translationUnit , lbl );
    38   }
    39 
     30        mutateAll( translationUnit, formut );
     31        acceptAll( translationUnit, lfix );
     32        mutateAll( translationUnit, chmut );
     33        mutateAll( translationUnit, ranges );
     34        //mutateAll( translationUnit, exc );
     35        //acceptAll( translationUnit, lbl );
     36    }
    4037} // namespace CodeGen
    41 
    42 
  • translator/ControlStruct/Mutate.h

    r3848e0e rd9a0e76  
    88
    99namespace ControlStruct {
    10 
    11   void mutate( std::list< Declaration* > translationUnit );
    12 
     10    void mutate( std::list< Declaration* > translationUnit );
    1311} // namespace ControlStruct
    1412
    15 #endif // #ifndef CTRLS_MUTATE_H
     13#endif // CTRLS_MUTATE_H
    1614
    1715/*
Note: See TracChangeset for help on using the changeset viewer.