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