[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 | // |
---|
[23b6f4d7] | 7 | // MLEMutator.cc -- |
---|
[51587aa] | 8 | // |
---|
[843054c2] | 9 | // Author : Rodolfo G. Esteves |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[adcc065] | 11 | // Last Modified By : Peter A. Buhr |
---|
[8688ce1] | 12 | // Last Modified On : Thu Aug 4 11:21:32 2016 |
---|
| 13 | // Update Count : 202 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[adcc065] | 16 | // NOTE: There are two known subtle differences from the code that uC++ generates for the same input |
---|
| 17 | // -CFA puts the break label inside at the end of a switch, uC++ puts it after |
---|
| 18 | // -CFA puts the break label after a block, uC++ puts it inside at the end |
---|
| 19 | // It is unclear if these differences are important, but if they are, then the fix would go in this file, since this is |
---|
| 20 | // where these labels are generated. |
---|
[994ec2c] | 21 | |
---|
[d180746] | 22 | #include <ext/alloc_traits.h> // for __alloc_traits<>::value_type |
---|
| 23 | #include <algorithm> // for find, find_if |
---|
| 24 | #include <cassert> // for assert, assertf |
---|
| 25 | #include <memory> // for allocator_traits<>::value_... |
---|
[51b7345] | 26 | |
---|
[d180746] | 27 | #include "Common/utility.h" // for toString, operator+ |
---|
| 28 | #include "ControlStruct/LabelGenerator.h" // for LabelGenerator |
---|
[51b7345] | 29 | #include "MLEMutator.h" |
---|
[d180746] | 30 | #include "SynTree/Attribute.h" // for Attribute |
---|
| 31 | #include "SynTree/Expression.h" // for Expression |
---|
| 32 | #include "SynTree/Statement.h" // for BranchStmt, CompoundStmt |
---|
[51b7345] | 33 | |
---|
| 34 | namespace ControlStruct { |
---|
[a08ba92] | 35 | MLEMutator::~MLEMutator() { |
---|
| 36 | delete targetTable; |
---|
| 37 | targetTable = 0; |
---|
| 38 | } |
---|
[e39aa0f] | 39 | namespace { |
---|
| 40 | Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; } |
---|
| 41 | } |
---|
[a08ba92] | 42 | |
---|
[adcc065] | 43 | // break labels have to come after the statement they break out of, so mutate a statement, then if they inform us |
---|
| 44 | // through the breakLabel field tha they need a place to jump to on a break statement, add the break label to the |
---|
| 45 | // body of statements |
---|
[27de955] | 46 | void MLEMutator::fixBlock( std::list< Statement * > &kids ) { |
---|
[a08ba92] | 47 | for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) { |
---|
| 48 | *k = (*k)->acceptMutator(*this); |
---|
| 49 | |
---|
| 50 | if ( ! get_breakLabel().empty() ) { |
---|
[d939274] | 51 | std::list< Statement * >::iterator next = k+1; |
---|
[e39aa0f] | 52 | std::list<Label> ls; ls.push_back( get_breakLabel() ); |
---|
| 53 | kids.insert( next, new NullStmt( ls ) ); |
---|
[a08ba92] | 54 | set_breakLabel(""); |
---|
| 55 | } // if |
---|
| 56 | } // for |
---|
[27de955] | 57 | } |
---|
| 58 | |
---|
| 59 | CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) { |
---|
| 60 | bool labeledBlock = !(cmpndStmt->get_labels().empty()); |
---|
| 61 | if ( labeledBlock ) { |
---|
[af68f0a] | 62 | Label brkLabel = generator->newLabel("blockBreak", cmpndStmt); |
---|
[e39aa0f] | 63 | enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) ); |
---|
[27de955] | 64 | } // if |
---|
| 65 | |
---|
[adcc065] | 66 | // a child statement may set the break label - if they do, attach it to the next statement |
---|
[27de955] | 67 | std::list< Statement * > &kids = cmpndStmt->get_kids(); |
---|
| 68 | fixBlock( kids ); |
---|
[a08ba92] | 69 | |
---|
| 70 | if ( labeledBlock ) { |
---|
[e39aa0f] | 71 | assert( ! enclosingControlStructures.empty() ); |
---|
| 72 | if ( ! enclosingControlStructures.back().useBreakExit().empty() ) { |
---|
| 73 | set_breakLabel( enclosingControlStructures.back().useBreakExit() ); |
---|
[adcc065] | 74 | } // if |
---|
[e39aa0f] | 75 | enclosingControlStructures.pop_back(); |
---|
[a08ba92] | 76 | } // if |
---|
| 77 | |
---|
| 78 | return cmpndStmt; |
---|
| 79 | } |
---|
| 80 | |
---|
[27de955] | 81 | template< typename LoopClass > |
---|
| 82 | Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) { |
---|
[adcc065] | 83 | // remember this as the most recent enclosing loop, then mutate the body of the loop -- this will determine |
---|
| 84 | // whether brkLabel and contLabel are used with branch statements and will recursively do the same to nested |
---|
| 85 | // loops |
---|
[af68f0a] | 86 | Label brkLabel = generator->newLabel("loopBreak", loopStmt); |
---|
| 87 | Label contLabel = generator->newLabel("loopContinue", loopStmt); |
---|
[e39aa0f] | 88 | enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) ); |
---|
[27de955] | 89 | loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) ); |
---|
| 90 | |
---|
[af68f0a] | 91 | assert( ! enclosingControlStructures.empty() ); |
---|
[e39aa0f] | 92 | Entry &e = enclosingControlStructures.back(); |
---|
[adcc065] | 93 | // sanity check that the enclosing loops have been popped correctly |
---|
[27de955] | 94 | assert ( e == loopStmt ); |
---|
| 95 | |
---|
[adcc065] | 96 | // this will take the necessary steps to add definitions of the previous two labels, if they are used. |
---|
[27de955] | 97 | loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) ); |
---|
[e39aa0f] | 98 | enclosingControlStructures.pop_back(); |
---|
[a08ba92] | 99 | |
---|
[27de955] | 100 | return loopStmt; |
---|
[d9a0e76] | 101 | } |
---|
| 102 | |
---|
[27de955] | 103 | Statement *MLEMutator::mutate( CaseStmt *caseStmt ) { |
---|
| 104 | caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) ); |
---|
| 105 | fixBlock( caseStmt->get_statements() ); |
---|
[a08ba92] | 106 | |
---|
[27de955] | 107 | return caseStmt; |
---|
| 108 | } |
---|
| 109 | |
---|
[adcc065] | 110 | template< typename IfClass > |
---|
| 111 | Statement *MLEMutator::handleIfStmt( IfClass *ifStmt ) { |
---|
| 112 | // generate a label for breaking out of a labeled if |
---|
| 113 | bool labeledBlock = !(ifStmt->get_labels().empty()); |
---|
| 114 | if ( labeledBlock ) { |
---|
[af68f0a] | 115 | Label brkLabel = generator->newLabel("blockBreak", ifStmt); |
---|
[adcc065] | 116 | enclosingControlStructures.push_back( Entry( ifStmt, brkLabel ) ); |
---|
| 117 | } // if |
---|
| 118 | |
---|
| 119 | Parent::mutate( ifStmt ); |
---|
[af68f0a] | 120 | |
---|
[adcc065] | 121 | if ( labeledBlock ) { |
---|
| 122 | if ( ! enclosingControlStructures.back().useBreakExit().empty() ) { |
---|
| 123 | set_breakLabel( enclosingControlStructures.back().useBreakExit() ); |
---|
| 124 | } // if |
---|
| 125 | enclosingControlStructures.pop_back(); |
---|
| 126 | } // if |
---|
| 127 | return ifStmt; |
---|
| 128 | } |
---|
| 129 | |
---|
[27de955] | 130 | template< typename SwitchClass > |
---|
| 131 | Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) { |
---|
[23b6f4d7] | 132 | // generate a label for breaking out of a labeled switch |
---|
[af68f0a] | 133 | Label brkLabel = generator->newLabel("switchBreak", switchStmt); |
---|
[e39aa0f] | 134 | enclosingControlStructures.push_back( Entry(switchStmt, brkLabel) ); |
---|
[8688ce1] | 135 | mutateAll( switchStmt->get_statements(), *this ); |
---|
[a08ba92] | 136 | |
---|
[e39aa0f] | 137 | Entry &e = enclosingControlStructures.back(); |
---|
[27de955] | 138 | assert ( e == switchStmt ); |
---|
| 139 | |
---|
| 140 | // only generate break label if labeled break is used |
---|
[adcc065] | 141 | if ( e.isBreakUsed() ) { |
---|
| 142 | // for the purposes of keeping switch statements uniform (i.e. all statements that are direct children of a |
---|
| 143 | // switch should be CastStmts), append the exit label + break to the last case statement; create a default |
---|
| 144 | // case if there are no cases |
---|
[8688ce1] | 145 | std::list< Statement * > &statements = switchStmt->get_statements(); |
---|
| 146 | if ( statements.empty() ) { |
---|
| 147 | statements.push_back( CaseStmt::makeDefault() ); |
---|
[adcc065] | 148 | } // if |
---|
[27de955] | 149 | |
---|
[8688ce1] | 150 | if ( CaseStmt * c = dynamic_cast< CaseStmt * >( statements.back() ) ) { |
---|
[27de955] | 151 | std::list<Label> temp; temp.push_back( brkLabel ); |
---|
[e39aa0f] | 152 | c->get_statements().push_back( new BranchStmt( temp, Label("brkLabel"), BranchStmt::Break ) ); |
---|
[8688ce1] | 153 | } else assert(0); // as of this point, all statements of a switch are still CaseStmts |
---|
[adcc065] | 154 | } // if |
---|
[27de955] | 155 | |
---|
[e39aa0f] | 156 | assert ( enclosingControlStructures.back() == switchStmt ); |
---|
| 157 | enclosingControlStructures.pop_back(); |
---|
[27de955] | 158 | return switchStmt; |
---|
[d9a0e76] | 159 | } |
---|
[a08ba92] | 160 | |
---|
[33c4b81] | 161 | void addUnused( Statement * stmt, const Label & originalTarget ) { |
---|
| 162 | // break/continue without a label doesn't need unused attribute |
---|
| 163 | if ( originalTarget == "" ) return; |
---|
| 164 | // add unused attribute to the originalTarget of a labelled break/continue |
---|
| 165 | for ( Label & l : stmt->get_labels() ) { |
---|
| 166 | // find the label to add unused attribute to |
---|
| 167 | if ( l == originalTarget ) { |
---|
| 168 | for ( Attribute * attr : l.get_attributes() ) { |
---|
| 169 | // ensure attribute isn't added twice |
---|
| 170 | if ( attr->get_name() == "unused" ) return; |
---|
| 171 | } |
---|
| 172 | l.get_attributes().push_back( new Attribute( "unused" ) ); |
---|
| 173 | return; |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | assertf( false, "Could not find label '%s' on statement %s", originalTarget.get_name().c_str(), toString( stmt ).c_str() ); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
[a08ba92] | 180 | Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) { |
---|
[be5aa1b] | 181 | std::string originalTarget = branchStmt->get_originalTarget(); |
---|
| 182 | |
---|
[e39aa0f] | 183 | std::list< Entry >::reverse_iterator targetEntry; |
---|
[af68f0a] | 184 | switch ( branchStmt->get_type() ) { |
---|
| 185 | case BranchStmt::Goto: |
---|
| 186 | return branchStmt; |
---|
| 187 | case BranchStmt::Continue: |
---|
| 188 | case BranchStmt::Break: { |
---|
| 189 | bool isContinue = branchStmt->get_type() == BranchStmt::Continue; |
---|
| 190 | // unlabeled break/continue |
---|
| 191 | if ( branchStmt->get_target() == "" ) { |
---|
| 192 | if ( isContinue ) { |
---|
| 193 | // continue target is outermost loop |
---|
| 194 | targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), [](Entry &e) { return isLoop( e.get_controlStructure() ); } ); |
---|
| 195 | } else { |
---|
| 196 | // break target is outmost control structure |
---|
| 197 | if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" ); |
---|
| 198 | targetEntry = enclosingControlStructures.rbegin(); |
---|
| 199 | } // if |
---|
| 200 | } else { |
---|
| 201 | // labeled break/continue - lookup label in table to find attached control structure |
---|
| 202 | targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] ); |
---|
| 203 | } // if |
---|
| 204 | // ensure that selected target is valid |
---|
| 205 | if ( targetEntry == enclosingControlStructures.rend() || (isContinue && ! isLoop( targetEntry->get_controlStructure() ) ) ) { |
---|
| 206 | throw SemanticError( toString( (isContinue ? "'continue'" : "'break'"), " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), originalTarget ) ); |
---|
| 207 | } // if |
---|
| 208 | break; |
---|
| 209 | } |
---|
| 210 | default: |
---|
| 211 | assert( false ); |
---|
| 212 | } // switch |
---|
[a08ba92] | 213 | |
---|
[27de955] | 214 | // branch error checks, get the appropriate label name and create a goto |
---|
| 215 | Label exitLabel; |
---|
[a08ba92] | 216 | switch ( branchStmt->get_type() ) { |
---|
| 217 | case BranchStmt::Break: |
---|
[e39aa0f] | 218 | assert( targetEntry->useBreakExit() != ""); |
---|
| 219 | exitLabel = targetEntry->useBreakExit(); |
---|
[be5aa1b] | 220 | break; |
---|
[a08ba92] | 221 | case BranchStmt::Continue: |
---|
[e39aa0f] | 222 | assert( targetEntry->useContExit() != ""); |
---|
| 223 | exitLabel = targetEntry->useContExit(); |
---|
[be5aa1b] | 224 | break; |
---|
[a08ba92] | 225 | default: |
---|
[27de955] | 226 | assert(0); // shouldn't be here |
---|
[a08ba92] | 227 | } // switch |
---|
| 228 | |
---|
[33c4b81] | 229 | // add unused attribute to label to silence warnings |
---|
| 230 | addUnused( targetEntry->get_controlStructure(), branchStmt->get_originalTarget() ); |
---|
| 231 | |
---|
[af68f0a] | 232 | // transform break/continue statements into goto to simplify later handling of branches |
---|
| 233 | delete branchStmt; |
---|
| 234 | return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto ); |
---|
[a08ba92] | 235 | } |
---|
| 236 | |
---|
| 237 | Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) { |
---|
[d939274] | 238 | // ensure loop body is a block |
---|
[a08ba92] | 239 | CompoundStmt *newBody; |
---|
| 240 | if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) { |
---|
| 241 | newBody = new CompoundStmt( std::list< Label >() ); |
---|
| 242 | newBody->get_kids().push_back( bodyLoop ); |
---|
| 243 | } // if |
---|
| 244 | |
---|
[27de955] | 245 | // only generate these when needed |
---|
[a08ba92] | 246 | |
---|
[27de955] | 247 | if ( e.isContUsed() ) { |
---|
| 248 | // continue label goes in the body as the last statement |
---|
| 249 | std::list< Label > labels; labels.push_back( e.useContExit() ); |
---|
[e39aa0f] | 250 | newBody->get_kids().push_back( new NullStmt( labels ) ); |
---|
[adcc065] | 251 | } // if |
---|
[27de955] | 252 | |
---|
| 253 | if ( e.isBreakUsed() ) { |
---|
[adcc065] | 254 | // break label goes after the loop -- it'll get set by the outer mutator if we do this |
---|
[23b6f4d7] | 255 | set_breakLabel( e.useBreakExit() ); |
---|
[adcc065] | 256 | } // if |
---|
[a08ba92] | 257 | |
---|
| 258 | return newBody; |
---|
| 259 | } |
---|
| 260 | |
---|
[27de955] | 261 | Statement *MLEMutator::mutate( WhileStmt *whileStmt ) { |
---|
| 262 | return handleLoopStmt( whileStmt ); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | Statement *MLEMutator::mutate( ForStmt *forStmt ) { |
---|
| 266 | return handleLoopStmt( forStmt ); |
---|
| 267 | } |
---|
| 268 | |
---|
[adcc065] | 269 | Statement *MLEMutator::mutate( IfStmt *ifStmt ) { |
---|
| 270 | return handleIfStmt( ifStmt ); |
---|
| 271 | } |
---|
| 272 | |
---|
[27de955] | 273 | Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) { |
---|
| 274 | return handleSwitchStmt( switchStmt ); |
---|
[a08ba92] | 275 | } |
---|
[51b7345] | 276 | } // namespace ControlStruct |
---|
[a08ba92] | 277 | |
---|
[51587aa] | 278 | // Local Variables: // |
---|
| 279 | // tab-width: 4 // |
---|
| 280 | // mode: c++ // |
---|
| 281 | // compile-command: "make install" // |
---|
| 282 | // End: // |
---|