| 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 | //
|
|---|
| 7 | // MLEMutator.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rodolfo G. Esteves
|
|---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
|---|
| 11 | // Last Modified By : Rob Schluntz
|
|---|
| 12 | // Last Modified On : Wed Jun 03 15:09:27 2015
|
|---|
| 13 | // Update Count : 170
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <cassert>
|
|---|
| 17 | #include <algorithm>
|
|---|
| 18 |
|
|---|
| 19 | #include "MLEMutator.h"
|
|---|
| 20 | #include "SynTree/Statement.h"
|
|---|
| 21 | #include "SynTree/Expression.h"
|
|---|
| 22 |
|
|---|
| 23 | namespace ControlStruct {
|
|---|
| 24 | MLEMutator::~MLEMutator() {
|
|---|
| 25 | delete targetTable;
|
|---|
| 26 | targetTable = 0;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | // break labels have to come after the statement they break out of,
|
|---|
| 30 | // so mutate a statement, then if they inform us through the breakLabel field
|
|---|
| 31 | // tha they need a place to jump to on a break statement, add the break label
|
|---|
| 32 | // to the body of statements
|
|---|
| 33 | void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
|
|---|
| 34 | for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
|
|---|
| 35 | *k = (*k)->acceptMutator(*this);
|
|---|
| 36 |
|
|---|
| 37 | if ( ! get_breakLabel().empty() ) {
|
|---|
| 38 | std::list< Statement * >::iterator next = k+1;
|
|---|
| 39 | if ( next == kids.end() ) {
|
|---|
| 40 | std::list<Label> ls; ls.push_back( get_breakLabel() );
|
|---|
| 41 | kids.push_back( new NullStmt( ls ) );
|
|---|
| 42 | } else {
|
|---|
| 43 | (*next)->get_labels().push_back( get_breakLabel() );
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | set_breakLabel("");
|
|---|
| 47 | } // if
|
|---|
| 48 | } // for
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
|
|---|
| 52 | bool labeledBlock = !(cmpndStmt->get_labels().empty());
|
|---|
| 53 | if ( labeledBlock ) {
|
|---|
| 54 | Label brkLabel = generator->newLabel();
|
|---|
| 55 | enclosingBlocks.push_back( Entry( cmpndStmt, brkLabel ) );
|
|---|
| 56 | } // if
|
|---|
| 57 |
|
|---|
| 58 | // a child statement may set the break label
|
|---|
| 59 | // - if they do, attach it to the next statement
|
|---|
| 60 | std::list< Statement * > &kids = cmpndStmt->get_kids();
|
|---|
| 61 | fixBlock( kids );
|
|---|
| 62 |
|
|---|
| 63 | if ( labeledBlock ) {
|
|---|
| 64 | assert( ! enclosingBlocks.empty() );
|
|---|
| 65 | if ( ! enclosingBlocks.back().useBreakExit().empty() ) {
|
|---|
| 66 | set_breakLabel( enclosingBlocks.back().useBreakExit() );
|
|---|
| 67 | }
|
|---|
| 68 | enclosingBlocks.pop_back();
|
|---|
| 69 | } // if
|
|---|
| 70 |
|
|---|
| 71 | return cmpndStmt;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | template< typename LoopClass >
|
|---|
| 75 | Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) {
|
|---|
| 76 | // remember this as the most recent enclosing loop, then mutate
|
|---|
| 77 | // the body of the loop -- this will do SOMETHING with branch statements
|
|---|
| 78 | // and will recursively do the same to nested loops
|
|---|
| 79 | Label brkLabel = generator->newLabel("loopBreak");
|
|---|
| 80 | Label contLabel = generator->newLabel("loopContinue");
|
|---|
| 81 | enclosingLoops.push_back( Entry( loopStmt, brkLabel, contLabel ) );
|
|---|
| 82 | loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) );
|
|---|
| 83 |
|
|---|
| 84 | // sanity check that the enclosing loops have been popped correctly
|
|---|
| 85 | Entry &e = enclosingLoops.back();
|
|---|
| 86 | assert ( e == loopStmt );
|
|---|
| 87 |
|
|---|
| 88 | // generate labels as needed
|
|---|
| 89 | loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) );
|
|---|
| 90 | enclosingLoops.pop_back();
|
|---|
| 91 |
|
|---|
| 92 | return loopStmt;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | Statement *MLEMutator::mutate( CaseStmt *caseStmt ) {
|
|---|
| 96 | caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) );
|
|---|
| 97 | fixBlock( caseStmt->get_statements() );
|
|---|
| 98 |
|
|---|
| 99 | return caseStmt;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | template< typename SwitchClass >
|
|---|
| 103 | Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) {
|
|---|
| 104 | // generate a label for breaking out of a labeled switch
|
|---|
| 105 | Label brkLabel = generator->newLabel("switchBreak");
|
|---|
| 106 | enclosingSwitches.push_back( Entry(switchStmt, brkLabel) );
|
|---|
| 107 | mutateAll( switchStmt->get_branches(), *this );
|
|---|
| 108 |
|
|---|
| 109 | Entry &e = enclosingSwitches.back();
|
|---|
| 110 | assert ( e == switchStmt );
|
|---|
| 111 |
|
|---|
| 112 | // only generate break label if labeled break is used
|
|---|
| 113 | if (e.isBreakUsed()) {
|
|---|
| 114 | // for the purposes of keeping switch statements uniform (i.e. all statements that are
|
|---|
| 115 | // direct children of a switch should be CastStmts), append the exit label + break to the
|
|---|
| 116 | // last case statement; create a default case if there are no cases
|
|---|
| 117 | std::list< Statement * > &branches = switchStmt->get_branches();
|
|---|
| 118 | if ( branches.empty() ) {
|
|---|
| 119 | branches.push_back( CaseStmt::makeDefault() );
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | if ( CaseStmt * c = dynamic_cast< CaseStmt * >( branches.back() ) ) {
|
|---|
| 123 | std::list<Label> temp; temp.push_back( brkLabel );
|
|---|
| 124 | c->get_statements().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
|
|---|
| 125 | } else assert(0); // as of this point, all branches of a switch are still CaseStmts
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | assert ( enclosingSwitches.back() == switchStmt );
|
|---|
| 129 | enclosingSwitches.pop_back();
|
|---|
| 130 | return switchStmt;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
|
|---|
| 134 | std::string originalTarget = branchStmt->get_originalTarget();
|
|---|
| 135 |
|
|---|
| 136 | if ( branchStmt->get_type() == BranchStmt::Goto )
|
|---|
| 137 | return branchStmt;
|
|---|
| 138 |
|
|---|
| 139 | // test if continue target is a loop
|
|---|
| 140 | if ( branchStmt->get_type() == BranchStmt::Continue) {
|
|---|
| 141 | if ( enclosingLoops.empty() ) {
|
|---|
| 142 | throw SemanticError( "'continue' outside a loop" );
|
|---|
| 143 | } else if ( std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) == enclosingLoops.end() ) {
|
|---|
| 144 | throw SemanticError( "'continue' target label must be an enclosing loop: " + originalTarget );
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) )
|
|---|
| 149 | throw SemanticError( "'break' outside a loop or switch" );
|
|---|
| 150 |
|
|---|
| 151 | if ( branchStmt->get_target() == "" ) return branchStmt;
|
|---|
| 152 |
|
|---|
| 153 | if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() )
|
|---|
| 154 | throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget ); // shouldn't happen (since that's already checked)
|
|---|
| 155 |
|
|---|
| 156 | std::list< Entry >::iterator check;
|
|---|
| 157 | if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() )
|
|---|
| 158 | // not in loop, checking if in block
|
|---|
| 159 | if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() )
|
|---|
| 160 | // neither in loop nor in block, checking if in switch/choose
|
|---|
| 161 | if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() )
|
|---|
| 162 | throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing control structure: " + originalTarget );
|
|---|
| 163 |
|
|---|
| 164 | // what about exiting innermost block or switch???
|
|---|
| 165 | if ( enclosingLoops.back() == (*check) )
|
|---|
| 166 | return branchStmt; // exit the innermost loop (labels unnecessary)
|
|---|
| 167 |
|
|---|
| 168 | // branch error checks, get the appropriate label name and create a goto
|
|---|
| 169 | Label exitLabel;
|
|---|
| 170 | switch ( branchStmt->get_type() ) {
|
|---|
| 171 | case BranchStmt::Break:
|
|---|
| 172 | assert( check->useBreakExit() != "");
|
|---|
| 173 | exitLabel = check->useBreakExit();
|
|---|
| 174 | break;
|
|---|
| 175 | case BranchStmt::Continue:
|
|---|
| 176 | assert( check->useContExit() != "");
|
|---|
| 177 | exitLabel = check->useContExit();
|
|---|
| 178 | break;
|
|---|
| 179 | default:
|
|---|
| 180 | assert(0); // shouldn't be here
|
|---|
| 181 | } // switch
|
|---|
| 182 |
|
|---|
| 183 | return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto );
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
|
|---|
| 187 | // ensure loop body is a block
|
|---|
| 188 | CompoundStmt *newBody;
|
|---|
| 189 | if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
|
|---|
| 190 | newBody = new CompoundStmt( std::list< Label >() );
|
|---|
| 191 | newBody->get_kids().push_back( bodyLoop );
|
|---|
| 192 | } // if
|
|---|
| 193 |
|
|---|
| 194 | // only generate these when needed
|
|---|
| 195 |
|
|---|
| 196 | if ( e.isContUsed() ) {
|
|---|
| 197 | // continue label goes in the body as the last statement
|
|---|
| 198 | std::list< Label > labels; labels.push_back( e.useContExit() );
|
|---|
| 199 | newBody->get_kids().push_back( new NullStmt( labels ) );
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | if ( e.isBreakUsed() ) {
|
|---|
| 203 | // break label goes after the loop -- it'll get set by the
|
|---|
| 204 | // outer mutator if we do this
|
|---|
| 205 | set_breakLabel( e.useBreakExit() );
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | return newBody;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
|
|---|
| 212 | return handleLoopStmt( whileStmt );
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | Statement *MLEMutator::mutate( ForStmt *forStmt ) {
|
|---|
| 216 | return handleLoopStmt( forStmt );
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
|
|---|
| 220 | return handleSwitchStmt( switchStmt );
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | Statement *MLEMutator::mutate( ChooseStmt *switchStmt ) {
|
|---|
| 224 | return handleSwitchStmt( switchStmt );
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | } // namespace ControlStruct
|
|---|
| 228 |
|
|---|
| 229 | // Local Variables: //
|
|---|
| 230 | // tab-width: 4 //
|
|---|
| 231 | // mode: c++ //
|
|---|
| 232 | // compile-command: "make install" //
|
|---|
| 233 | // End: //
|
|---|