[51587aa] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
[a08ba92] | 7 | // MLEMutator.cc --
|
---|
[51587aa] | 8 | //
|
---|
[843054c2] | 9 | // Author : Rodolfo G. Esteves
|
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[be5aa1b] | 11 | // Last Modified By : Rob Schluntz
|
---|
| 12 | // Last Modified On : Wed May 27 16:19:32 2015
|
---|
| 13 | // Update Count : 44
|
---|
[51587aa] | 14 | //
|
---|
[a08ba92] | 15 |
|
---|
[51b73452] | 16 | #include <cassert>
|
---|
| 17 | #include <algorithm>
|
---|
| 18 |
|
---|
| 19 | #include "MLEMutator.h"
|
---|
| 20 | #include "SynTree/Statement.h"
|
---|
| 21 |
|
---|
| 22 | namespace ControlStruct {
|
---|
[a08ba92] | 23 | MLEMutator::~MLEMutator() {
|
---|
| 24 | delete targetTable;
|
---|
| 25 | targetTable = 0;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
|
---|
| 29 | bool labeledBlock = false;
|
---|
[be5aa1b] | 30 | if ( !(cmpndStmt->get_labels().empty()) ) {
|
---|
[a08ba92] | 31 | labeledBlock = true;
|
---|
| 32 | enclosingBlocks.push_back( Entry( cmpndStmt ) );
|
---|
| 33 | } // if
|
---|
| 34 |
|
---|
| 35 | std::list< Statement * > &kids = cmpndStmt->get_kids();
|
---|
| 36 | for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
|
---|
| 37 | *k = (*k)->acceptMutator(*this);
|
---|
| 38 |
|
---|
| 39 | if ( ! get_breakLabel().empty() ) {
|
---|
| 40 | std::list< Statement * >::iterator next = k; next++;
|
---|
| 41 | if ( next == kids.end() ) {
|
---|
| 42 | std::list<Label> ls; ls.push_back( get_breakLabel() );
|
---|
| 43 | kids.push_back( new NullStmt( ls ) );
|
---|
[be5aa1b] | 44 | } else {
|
---|
[a08ba92] | 45 | (*next)->get_labels().push_back( get_breakLabel() );
|
---|
[be5aa1b] | 46 | }
|
---|
[a08ba92] | 47 |
|
---|
| 48 | set_breakLabel("");
|
---|
| 49 | } // if
|
---|
| 50 | } // for
|
---|
| 51 |
|
---|
| 52 | if ( labeledBlock ) {
|
---|
| 53 | assert( ! enclosingBlocks.empty() );
|
---|
[be5aa1b] | 54 | if ( ! enclosingBlocks.back().get_breakExit().empty() ) {
|
---|
[a08ba92] | 55 | set_breakLabel( enclosingBlocks.back().get_breakExit() );
|
---|
[be5aa1b] | 56 | }
|
---|
[a08ba92] | 57 | enclosingBlocks.pop_back();
|
---|
| 58 | } // if
|
---|
| 59 |
|
---|
| 60 | //mutateAll( cmpndStmt->get_kids(), *this );
|
---|
| 61 | return cmpndStmt;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
|
---|
| 65 | enclosingLoops.push_back( Entry( whileStmt ) );
|
---|
| 66 | whileStmt->set_body ( whileStmt->get_body()->acceptMutator( *this ) );
|
---|
| 67 |
|
---|
| 68 | Entry &e = enclosingLoops.back();
|
---|
| 69 | assert ( e == whileStmt );
|
---|
| 70 | whileStmt->set_body( mutateLoop( whileStmt->get_body(), e ) );
|
---|
| 71 | enclosingLoops.pop_back();
|
---|
| 72 |
|
---|
| 73 | return whileStmt;
|
---|
[d9a0e76] | 74 | }
|
---|
| 75 |
|
---|
[a08ba92] | 76 | Statement *MLEMutator::mutate( ForStmt *forStmt ) {
|
---|
| 77 | enclosingLoops.push_back( Entry( forStmt ) );
|
---|
| 78 | maybeMutate( forStmt->get_body(), *this );
|
---|
| 79 |
|
---|
| 80 | Entry &e = enclosingLoops.back();
|
---|
| 81 | assert ( e == forStmt );
|
---|
| 82 | forStmt->set_body( mutateLoop( forStmt->get_body(), e ) );
|
---|
| 83 | enclosingLoops.pop_back();
|
---|
| 84 |
|
---|
| 85 | return forStmt;
|
---|
[d9a0e76] | 86 | }
|
---|
[a08ba92] | 87 |
|
---|
| 88 | Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
|
---|
[be5aa1b] | 89 | std::string originalTarget = branchStmt->get_originalTarget();
|
---|
| 90 |
|
---|
[a08ba92] | 91 | if ( branchStmt->get_type() == BranchStmt::Goto )
|
---|
| 92 | return branchStmt;
|
---|
| 93 |
|
---|
| 94 | // test if continue target is a loop
|
---|
[be5aa1b] | 95 | if ( branchStmt->get_type() == BranchStmt::Continue) {
|
---|
| 96 | if ( enclosingLoops.empty() ) {
|
---|
| 97 | throw SemanticError( "'continue' outside a loop" );
|
---|
| 98 | } else if ( std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) == enclosingLoops.end() ) {
|
---|
| 99 | throw SemanticError( "'continue' target label must be an enclosing loop: " + originalTarget );
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
[a08ba92] | 102 |
|
---|
| 103 | if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) )
|
---|
| 104 | throw SemanticError( "'break' outside a loop or switch" );
|
---|
| 105 |
|
---|
| 106 | if ( branchStmt->get_target() == "" ) return branchStmt;
|
---|
| 107 |
|
---|
| 108 | if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() )
|
---|
[be5aa1b] | 109 | throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget ); // shouldn't happen (since that's already checked)
|
---|
[a08ba92] | 110 |
|
---|
| 111 | std::list< Entry >::iterator check;
|
---|
| 112 | if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() )
|
---|
| 113 | // not in loop, checking if in block
|
---|
| 114 | if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() )
|
---|
| 115 | // neither in loop nor in block, checking if in switch/choose
|
---|
| 116 | if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() )
|
---|
[be5aa1b] | 117 | throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing control structure: " + originalTarget );
|
---|
[a08ba92] | 118 |
|
---|
| 119 | if ( enclosingLoops.back() == (*check) )
|
---|
| 120 | return branchStmt; // exit the innermost loop (labels unnecessary)
|
---|
| 121 |
|
---|
| 122 | Label newLabel;
|
---|
| 123 | switch ( branchStmt->get_type() ) {
|
---|
| 124 | case BranchStmt::Break:
|
---|
[be5aa1b] | 125 | if ( check->get_breakExit() != "" ) {
|
---|
| 126 | newLabel = check->get_breakExit();
|
---|
| 127 | } else {
|
---|
| 128 | newLabel = generator->newLabel();
|
---|
| 129 | check->set_breakExit( newLabel );
|
---|
| 130 | } // if
|
---|
| 131 | break;
|
---|
[a08ba92] | 132 | case BranchStmt::Continue:
|
---|
[be5aa1b] | 133 | if ( check->get_contExit() != "" ) {
|
---|
| 134 | newLabel = check->get_contExit();
|
---|
| 135 | } else {
|
---|
| 136 | newLabel = generator->newLabel();
|
---|
| 137 | check->set_contExit( newLabel );
|
---|
| 138 | } // if
|
---|
| 139 | break;
|
---|
[a08ba92] | 140 | default:
|
---|
[be5aa1b] | 141 | return 0; // shouldn't be here
|
---|
[a08ba92] | 142 | } // switch
|
---|
| 143 |
|
---|
| 144 | return new BranchStmt( std::list<Label>(), newLabel, BranchStmt::Goto );
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[be5aa1b] | 147 | template< typename SwitchClass >
|
---|
| 148 | Statement *handleSwitchStmt( SwitchClass *switchStmt, MLEMutator &mutator ) {
|
---|
| 149 | // set up some aliases so that the rest of the code isn't messy
|
---|
| 150 | typedef MLEMutator::Entry Entry;
|
---|
| 151 | LabelGenerator *generator = mutator.generator;
|
---|
| 152 | std::list< Entry > &enclosingSwitches = mutator.enclosingSwitches;
|
---|
[a08ba92] | 153 |
|
---|
| 154 | Label brkLabel = generator->newLabel();
|
---|
| 155 | enclosingSwitches.push_back( Entry(switchStmt, "", brkLabel) );
|
---|
[be5aa1b] | 156 | mutateAll( switchStmt->get_branches(), mutator ); {
|
---|
[a08ba92] | 157 | // check if this is necessary (if there is a break to this point, otherwise do not generate
|
---|
| 158 | std::list<Label> temp; temp.push_back( brkLabel );
|
---|
| 159 | switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
|
---|
| 160 | }
|
---|
| 161 | assert ( enclosingSwitches.back() == switchStmt );
|
---|
| 162 | enclosingSwitches.pop_back();
|
---|
| 163 | return switchStmt;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[be5aa1b] | 166 | Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
|
---|
| 167 | return handleSwitchStmt( switchStmt, *this );
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[a08ba92] | 170 | Statement *MLEMutator::mutate( ChooseStmt *switchStmt ) {
|
---|
[be5aa1b] | 171 | return handleSwitchStmt( switchStmt, *this );
|
---|
[a08ba92] | 172 | }
|
---|
| 173 |
|
---|
| 174 | Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
|
---|
| 175 | CompoundStmt *newBody;
|
---|
| 176 | if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
|
---|
| 177 | newBody = new CompoundStmt( std::list< Label >() );
|
---|
| 178 | newBody->get_kids().push_back( bodyLoop );
|
---|
| 179 | } // if
|
---|
| 180 |
|
---|
| 181 | Label endLabel = e.get_contExit();
|
---|
| 182 |
|
---|
| 183 | if ( e.get_breakExit() != "" ) {
|
---|
| 184 | if ( endLabel == "" ) endLabel = generator->newLabel();
|
---|
| 185 | // check for whether this label is used or not, so as to not generate extraneous gotos
|
---|
| 186 | if (e.breakExitUsed)
|
---|
| 187 | newBody->get_kids().push_back( new BranchStmt( std::list< Label >(), endLabel, BranchStmt::Goto ) );
|
---|
| 188 | // xxx
|
---|
| 189 | //std::list< Label > ls; ls.push_back( e.get_breakExit() );
|
---|
| 190 | set_breakLabel( e.get_breakExit() );
|
---|
| 191 | //newBody->get_kids().push_back( new BranchStmt( ls, "", BranchStmt::Break ) );
|
---|
| 192 | } // if
|
---|
| 193 |
|
---|
| 194 | if ( e.get_breakExit() != "" || e.get_contExit() != "" ) {
|
---|
| 195 | if (dynamic_cast< NullStmt *>( newBody->get_kids().back() ))
|
---|
| 196 | newBody->get_kids().back()->get_labels().push_back( endLabel );
|
---|
| 197 | else {
|
---|
| 198 | std::list < Label > ls; ls.push_back( endLabel );
|
---|
| 199 | newBody->get_kids().push_back( new NullStmt( ls ) );
|
---|
| 200 | } // if
|
---|
| 201 | } // if
|
---|
| 202 |
|
---|
| 203 | return newBody;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | //*** Entry's methods
|
---|
| 207 | void MLEMutator::Entry::set_contExit( Label l ) {
|
---|
| 208 | assert ( contExit == "" || contExit == l );
|
---|
| 209 | contExit = l;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | void MLEMutator::Entry::set_breakExit( Label l ) {
|
---|
| 213 | assert ( breakExit == "" || breakExit == l );
|
---|
| 214 | breakExit = l;
|
---|
[d9a0e76] | 215 | }
|
---|
[51b73452] | 216 | } // namespace ControlStruct
|
---|
[a08ba92] | 217 |
|
---|
[51587aa] | 218 | // Local Variables: //
|
---|
| 219 | // tab-width: 4 //
|
---|
| 220 | // mode: c++ //
|
---|
| 221 | // compile-command: "make install" //
|
---|
| 222 | // End: //
|
---|