Ignore:
Timestamp:
Jul 6, 2016, 6:06:27 PM (9 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, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
3f869f0
Parents:
e04c5ff
Message:

add labelled break to if statement, update comment formatting, add random number test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/MLEMutator.cc

    re04c5ff radcc065  
    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 : Mon Jul 20 13:58:35 2015
    13 // Update Count     : 176
    14 //
    15 
    16 // NOTE: There are two known subtle differences from the code that uC++
    17 // generates for the same input
    18 // -CFA puts the break label inside at the end of a switch, uC++ puts it after
    19 // -CFA puts the break label after a block, uC++ puts it inside at the end
    20 // we don't yet know if these differences are important, but if they are then
    21 // the fix would go in this file, since this is where these labels are generated.
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Wed Jul  6 17:40:02 2016
     13// Update Count     : 196
     14//
     15
     16// NOTE: There are two known subtle differences from the code that uC++ generates for the same input
     17//   -CFA puts the break label inside at the end of a switch, uC++ puts it after
     18//   -CFA puts the break label after a block, uC++ puts it inside at the end
     19// It is unclear if these differences are important, but if they are, then the fix would go in this file, since this is
     20// where these labels are generated.
    2221
    2322#include <cassert>
     
    3837        }
    3938
    40         // break labels have to come after the statement they break out of,
    41         // so mutate a statement, then if they inform us through the breakLabel field
    42         // tha they need a place to jump to on a break statement, add the break label
    43         // to the body of statements
     39        // break labels have to come after the statement they break out of, so mutate a statement, then if they inform us
     40        // through the breakLabel field tha they need a place to jump to on a break statement, add the break label to the
     41        // body of statements
    4442        void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
    4543                for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
     
    6260                } // if
    6361
    64                 // a child statement may set the break label
    65                 // - if they do, attach it to the next statement
     62                // a child statement may set the break label - if they do, attach it to the next statement
    6663                std::list< Statement * > &kids = cmpndStmt->get_kids();
    6764                fixBlock( kids );
     
    7168                        if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
    7269                                set_breakLabel( enclosingControlStructures.back().useBreakExit() );
    73                         }
     70                        } // if
    7471                        enclosingControlStructures.pop_back();
    7572                } // if
     
    8077        template< typename LoopClass >
    8178        Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) {
    82                 // remember this as the most recent enclosing loop, then mutate
    83                 // the body of the loop -- this will determine whether brkLabel
    84                 // and contLabel are used with branch statements
    85                 // and will recursively do the same to nested loops
     79                // remember this as the most recent enclosing loop, then mutate the body of the loop -- this will determine
     80                // whether brkLabel and contLabel are used with branch statements and will recursively do the same to nested
     81                // loops
    8682                Label brkLabel = generator->newLabel("loopBreak");
    8783                Label contLabel = generator->newLabel("loopContinue");
     
    8985                loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) );
    9086
     87                Entry &e = enclosingControlStructures.back();
    9188                // sanity check that the enclosing loops have been popped correctly
    92                 Entry &e = enclosingControlStructures.back();
    9389                assert ( e == loopStmt );
    9490
    95                 // this will take the necessary steps to add definitions of the previous
    96                 // two labels, if they are used.
     91                // this will take the necessary steps to add definitions of the previous two labels, if they are used.
    9792                loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) );
    9893                enclosingControlStructures.pop_back();
     
    106101
    107102                return caseStmt;
     103        }
     104
     105        template< typename IfClass >
     106        Statement *MLEMutator::handleIfStmt( IfClass *ifStmt ) {
     107                // generate a label for breaking out of a labeled if
     108                bool labeledBlock = !(ifStmt->get_labels().empty());
     109                if ( labeledBlock ) {
     110                        Label brkLabel = generator->newLabel("blockBreak");
     111                        enclosingControlStructures.push_back( Entry( ifStmt, brkLabel ) );
     112                } // if
     113
     114                Parent::mutate( ifStmt );
     115               
     116                if ( labeledBlock ) {
     117                        if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
     118                                set_breakLabel( enclosingControlStructures.back().useBreakExit() );
     119                        } // if
     120                        enclosingControlStructures.pop_back();
     121                } // if
     122                return ifStmt;
    108123        }
    109124
     
    119134
    120135                // only generate break label if labeled break is used
    121                 if (e.isBreakUsed()) {
    122                         // for the purposes of keeping switch statements uniform (i.e. all statements that are
    123                         // direct children of a switch should be CastStmts), append the exit label + break to the
    124                         // last case statement; create a default case if there are no cases
     136                if ( e.isBreakUsed() ) {
     137                        // for the purposes of keeping switch statements uniform (i.e. all statements that are direct children of a
     138                        // switch should be CastStmts), append the exit label + break to the last case statement; create a default
     139                        // case if there are no cases
    125140                        std::list< Statement * > &branches = switchStmt->get_branches();
    126141                        if ( branches.empty() ) {
    127142                                branches.push_back( CaseStmt::makeDefault() );
    128                         }
     143                        } // if
    129144
    130145                        if ( CaseStmt * c = dynamic_cast< CaseStmt * >( branches.back() ) ) {
     
    132147                                c->get_statements().push_back( new BranchStmt( temp, Label("brkLabel"), BranchStmt::Break ) );
    133148                        } else assert(0); // as of this point, all branches of a switch are still CaseStmts
    134                 }
     149                } // if
    135150
    136151                assert ( enclosingControlStructures.back() == switchStmt );
     
    152167                                // labelled continue - lookup label in table ot find attached control structure
    153168                                targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] );
    154                         }
     169                        } // if
    155170                        if ( targetEntry == enclosingControlStructures.rend() || ! isLoop( targetEntry->get_controlStructure() ) ) {
    156171                                throw SemanticError( "'continue' target must be an enclosing loop: " + originalTarget );
    157                         }
     172                        } // if
    158173                } else if ( branchStmt->get_type() == BranchStmt::Break ) {
    159174                        if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" );
     
    161176                } else {
    162177                        assert( false );
    163                 }
     178                } // if
    164179
    165180                if ( branchStmt->get_target() != "" && targetTable->find( branchStmt->get_target() ) == targetTable->end() ) {
    166181                        throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget );  // shouldn't happen (since that's already checked)
    167                 }
     182                } // if
    168183
    169184                // branch error checks, get the appropriate label name and create a goto
     
    191206                        delete branchStmt;
    192207                        return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto );
    193                 }
     208                } // if
    194209        }
    195210
     
    208223                        std::list< Label > labels; labels.push_back( e.useContExit() );
    209224                        newBody->get_kids().push_back( new NullStmt( labels ) );
    210                 }
     225                } // if
    211226
    212227                if ( e.isBreakUsed() ) {
    213                         // break label goes after the loop -- it'll get set by the
    214                         // outer mutator if we do this
     228                        // break label goes after the loop -- it'll get set by the outer mutator if we do this
    215229                        set_breakLabel( e.useBreakExit() );
    216                 }
     230                } // if
    217231
    218232                return newBody;
     
    227241        }
    228242
     243        Statement *MLEMutator::mutate( IfStmt *ifStmt ) {
     244                return handleIfStmt( ifStmt );
     245        }
     246
    229247        Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
    230248                return handleSwitchStmt( switchStmt );
     
    234252                return handleSwitchStmt( switchStmt );
    235253        }
    236 
    237254} // namespace ControlStruct
    238255
Note: See TracChangeset for help on using the changeset viewer.