| 1 | #include <cassert>
|
|---|
| 2 | #include <algorithm>
|
|---|
| 3 |
|
|---|
| 4 | #include "MLEMutator.h"
|
|---|
| 5 | #include "SynTree/Statement.h"
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | namespace 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 ) );
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 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);
|
|---|
| 26 |
|
|---|
| 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() );
|
|---|
| 34 |
|
|---|
| 35 | set_breakLabel("");
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 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();
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | //mutateAll( cmpndStmt->get_kids(), *this );
|
|---|
| 47 | return cmpndStmt;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | Statement *MLEMutator::mutate( WhileStmt *whileStmt )
|
|---|
| 51 | {
|
|---|
| 52 | enclosingLoops.push_back( Entry( whileStmt ) );
|
|---|
| 53 | whileStmt->set_body ( whileStmt->get_body()->acceptMutator( *this ) );
|
|---|
| 54 |
|
|---|
| 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 | if ( branchStmt->get_type() == BranchStmt::Continue && enclosingLoops.empty() )
|
|---|
| 83 | throw SemanticError( "'continue' outside a loop" );
|
|---|
| 84 |
|
|---|
| 85 | if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) )
|
|---|
| 86 | throw SemanticError( "'break' outside a loop or switch" );
|
|---|
| 87 |
|
|---|
| 88 | if ( branchStmt->get_target() == "" ) return branchStmt;
|
|---|
| 89 |
|
|---|
| 90 | if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() )
|
|---|
| 91 | throw SemanticError("The label defined in the exit loop statement does not exist." ); // shouldn't happen (since that's already checked)
|
|---|
| 92 |
|
|---|
| 93 | std::list< Entry >::iterator check;
|
|---|
| 94 | if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() )
|
|---|
| 95 | // not in loop, checking if in switch/choose
|
|---|
| 96 | if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() )
|
|---|
| 97 | // neither in loop nor in block, checking if in switch/choose
|
|---|
| 98 | if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() )
|
|---|
| 99 | throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing loop.");
|
|---|
| 100 |
|
|---|
| 101 | if ( enclosingLoops.back() == (*check) )
|
|---|
| 102 | return branchStmt; // exit the innermost loop (labels not necessary)
|
|---|
| 103 |
|
|---|
| 104 | Label newLabel;
|
|---|
| 105 | switch( branchStmt->get_type() ) {
|
|---|
| 106 | case BranchStmt::Break:
|
|---|
| 107 | if ( check->get_breakExit() != "" )
|
|---|
| 108 | newLabel = check->get_breakExit();
|
|---|
| 109 | else
|
|---|
| 110 | { newLabel = generator->newLabel(); check->set_breakExit( newLabel ); }
|
|---|
| 111 | break;
|
|---|
| 112 | case BranchStmt::Continue:
|
|---|
| 113 | if ( check->get_contExit() != "" )
|
|---|
| 114 | newLabel = check->get_contExit();
|
|---|
| 115 | else
|
|---|
| 116 | { newLabel = generator->newLabel(); check->set_contExit( newLabel ); }
|
|---|
| 117 | break;
|
|---|
| 118 |
|
|---|
| 119 | default:
|
|---|
| 120 | // shouldn't be here
|
|---|
| 121 | return 0;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | return new BranchStmt(std::list<Label>(), newLabel, BranchStmt::Goto );
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | Statement *MLEMutator::mutate(SwitchStmt *switchStmt)
|
|---|
| 129 | {
|
|---|
| 130 | Label brkLabel = generator->newLabel();
|
|---|
| 131 | enclosingSwitches.push_back( Entry(switchStmt, "", brkLabel) );
|
|---|
| 132 | mutateAll( switchStmt->get_branches(), *this );
|
|---|
| 133 | {
|
|---|
| 134 | // check if this is necessary (if there is a break to this point, otherwise do not generate
|
|---|
| 135 | std::list<Label> temp; temp.push_back( brkLabel );
|
|---|
| 136 | switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
|
|---|
| 137 | }
|
|---|
| 138 | assert ( enclosingSwitches.back() == switchStmt );
|
|---|
| 139 | enclosingSwitches.pop_back();
|
|---|
| 140 | return switchStmt;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | Statement *MLEMutator::mutate(ChooseStmt *switchStmt)
|
|---|
| 144 | {
|
|---|
| 145 | Label brkLabel = generator->newLabel();
|
|---|
| 146 | enclosingSwitches.push_back( Entry(switchStmt,"", brkLabel) );
|
|---|
| 147 | mutateAll( switchStmt->get_branches(), *this );
|
|---|
| 148 | {
|
|---|
| 149 | // check if this is necessary (if there is a break to this point, otherwise do not generate
|
|---|
| 150 | std::list<Label> temp; temp.push_back( brkLabel );
|
|---|
| 151 | switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
|
|---|
| 152 | }
|
|---|
| 153 | assert ( enclosingSwitches.back() == switchStmt );
|
|---|
| 154 | enclosingSwitches.pop_back();
|
|---|
| 155 | return switchStmt;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
|
|---|
| 159 | CompoundStmt *newBody;
|
|---|
| 160 | if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
|
|---|
| 161 | newBody = new CompoundStmt( std::list< Label >() );
|
|---|
| 162 | newBody->get_kids().push_back( bodyLoop );
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | Label endLabel = e.get_contExit();
|
|---|
| 166 |
|
|---|
| 167 | if ( e.get_breakExit() != "" ) {
|
|---|
| 168 | if ( endLabel == "" ) endLabel = generator->newLabel();
|
|---|
| 169 | // check for whether this label is used or not, so as to not generate extraneous gotos
|
|---|
| 170 | if (e.breakExitUsed)
|
|---|
| 171 | newBody->get_kids().push_back( new BranchStmt( std::list< Label >(), endLabel, BranchStmt::Goto ) );
|
|---|
| 172 | // xxx
|
|---|
| 173 | //std::list< Label > ls; ls.push_back( e.get_breakExit() );
|
|---|
| 174 | set_breakLabel( e.get_breakExit() );
|
|---|
| 175 | //newBody->get_kids().push_back( new BranchStmt( ls, "", BranchStmt::Break ) );
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | if ( e.get_breakExit() != "" || e.get_contExit() != "" ){
|
|---|
| 179 | if(dynamic_cast< NullStmt *>( newBody->get_kids().back() ))
|
|---|
| 180 | newBody->get_kids().back()->get_labels().push_back( endLabel );
|
|---|
| 181 | else {
|
|---|
| 182 | std::list < Label > ls; ls.push_back( endLabel );
|
|---|
| 183 | newBody->get_kids().push_back( new NullStmt( ls ) );
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | return newBody;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | //*** Entry's methods
|
|---|
| 191 | void MLEMutator::Entry::set_contExit( Label l )
|
|---|
| 192 | {
|
|---|
| 193 | assert ( contExit == "" || contExit == l );
|
|---|
| 194 | contExit = l;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | void MLEMutator::Entry::set_breakExit( Label l )
|
|---|
| 198 | {
|
|---|
| 199 | assert ( breakExit == "" || breakExit == l );
|
|---|
| 200 | breakExit = l;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | } // namespace ControlStruct
|
|---|