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