[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++ ) { |
---|
[94e025a2] | 48 | *k = (*k)->acceptMutator(*visitor); |
---|
[a08ba92] | 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 | |
---|
[94e025a2] | 59 | void MLEMutator::premutate( CompoundStmt *cmpndStmt ) { |
---|
| 60 | visit_children = false; |
---|
| 61 | bool labeledBlock = !(cmpndStmt->labels.empty()); |
---|
[27de955] | 62 | if ( labeledBlock ) { |
---|
[af68f0a] | 63 | Label brkLabel = generator->newLabel("blockBreak", cmpndStmt); |
---|
[e39aa0f] | 64 | enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) ); |
---|
[27de955] | 65 | } // if |
---|
| 66 | |
---|
[adcc065] | 67 | // a child statement may set the break label - if they do, attach it to the next statement |
---|
[94e025a2] | 68 | std::list< Statement * > &kids = cmpndStmt->kids; |
---|
[27de955] | 69 | fixBlock( kids ); |
---|
[a08ba92] | 70 | |
---|
| 71 | if ( labeledBlock ) { |
---|
[e39aa0f] | 72 | assert( ! enclosingControlStructures.empty() ); |
---|
| 73 | if ( ! enclosingControlStructures.back().useBreakExit().empty() ) { |
---|
| 74 | set_breakLabel( enclosingControlStructures.back().useBreakExit() ); |
---|
[adcc065] | 75 | } // if |
---|
[e39aa0f] | 76 | enclosingControlStructures.pop_back(); |
---|
[a08ba92] | 77 | } // if |
---|
[adcc065] | 78 | } |
---|
| 79 | |
---|
[a08ba92] | 80 | |
---|
[33c4b81] | 81 | void addUnused( Statement * stmt, const Label & originalTarget ) { |
---|
| 82 | // break/continue without a label doesn't need unused attribute |
---|
| 83 | if ( originalTarget == "" ) return; |
---|
| 84 | // add unused attribute to the originalTarget of a labelled break/continue |
---|
| 85 | for ( Label & l : stmt->get_labels() ) { |
---|
| 86 | // find the label to add unused attribute to |
---|
| 87 | if ( l == originalTarget ) { |
---|
| 88 | for ( Attribute * attr : l.get_attributes() ) { |
---|
| 89 | // ensure attribute isn't added twice |
---|
| 90 | if ( attr->get_name() == "unused" ) return; |
---|
| 91 | } |
---|
| 92 | l.get_attributes().push_back( new Attribute( "unused" ) ); |
---|
| 93 | return; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | assertf( false, "Could not find label '%s' on statement %s", originalTarget.get_name().c_str(), toString( stmt ).c_str() ); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
[94e025a2] | 100 | Statement *MLEMutator::postmutate( BranchStmt *branchStmt ) throw ( SemanticError ) { |
---|
| 101 | std::string originalTarget = branchStmt->originalTarget; |
---|
[be5aa1b] | 102 | |
---|
[e39aa0f] | 103 | std::list< Entry >::reverse_iterator targetEntry; |
---|
[af68f0a] | 104 | switch ( branchStmt->get_type() ) { |
---|
| 105 | case BranchStmt::Goto: |
---|
| 106 | return branchStmt; |
---|
| 107 | case BranchStmt::Continue: |
---|
| 108 | case BranchStmt::Break: { |
---|
| 109 | bool isContinue = branchStmt->get_type() == BranchStmt::Continue; |
---|
| 110 | // unlabeled break/continue |
---|
| 111 | if ( branchStmt->get_target() == "" ) { |
---|
| 112 | if ( isContinue ) { |
---|
| 113 | // continue target is outermost loop |
---|
| 114 | targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), [](Entry &e) { return isLoop( e.get_controlStructure() ); } ); |
---|
| 115 | } else { |
---|
| 116 | // break target is outmost control structure |
---|
| 117 | if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" ); |
---|
| 118 | targetEntry = enclosingControlStructures.rbegin(); |
---|
| 119 | } // if |
---|
| 120 | } else { |
---|
| 121 | // labeled break/continue - lookup label in table to find attached control structure |
---|
| 122 | targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] ); |
---|
| 123 | } // if |
---|
| 124 | // ensure that selected target is valid |
---|
| 125 | if ( targetEntry == enclosingControlStructures.rend() || (isContinue && ! isLoop( targetEntry->get_controlStructure() ) ) ) { |
---|
| 126 | throw SemanticError( toString( (isContinue ? "'continue'" : "'break'"), " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), originalTarget ) ); |
---|
| 127 | } // if |
---|
| 128 | break; |
---|
| 129 | } |
---|
| 130 | default: |
---|
| 131 | assert( false ); |
---|
| 132 | } // switch |
---|
[a08ba92] | 133 | |
---|
[27de955] | 134 | // branch error checks, get the appropriate label name and create a goto |
---|
| 135 | Label exitLabel; |
---|
[94e025a2] | 136 | switch ( branchStmt->type ) { |
---|
[a08ba92] | 137 | case BranchStmt::Break: |
---|
[e39aa0f] | 138 | assert( targetEntry->useBreakExit() != ""); |
---|
| 139 | exitLabel = targetEntry->useBreakExit(); |
---|
[be5aa1b] | 140 | break; |
---|
[a08ba92] | 141 | case BranchStmt::Continue: |
---|
[e39aa0f] | 142 | assert( targetEntry->useContExit() != ""); |
---|
| 143 | exitLabel = targetEntry->useContExit(); |
---|
[be5aa1b] | 144 | break; |
---|
[a08ba92] | 145 | default: |
---|
[27de955] | 146 | assert(0); // shouldn't be here |
---|
[a08ba92] | 147 | } // switch |
---|
| 148 | |
---|
[33c4b81] | 149 | // add unused attribute to label to silence warnings |
---|
[94e025a2] | 150 | addUnused( targetEntry->get_controlStructure(), branchStmt->originalTarget ); |
---|
[33c4b81] | 151 | |
---|
[af68f0a] | 152 | // transform break/continue statements into goto to simplify later handling of branches |
---|
| 153 | delete branchStmt; |
---|
[ba3706f] | 154 | return new BranchStmt( exitLabel, BranchStmt::Goto ); |
---|
[a08ba92] | 155 | } |
---|
| 156 | |
---|
| 157 | Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) { |
---|
[d939274] | 158 | // ensure loop body is a block |
---|
[a08ba92] | 159 | CompoundStmt *newBody; |
---|
| 160 | if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) { |
---|
[ba3706f] | 161 | newBody = new CompoundStmt(); |
---|
[a08ba92] | 162 | newBody->get_kids().push_back( bodyLoop ); |
---|
| 163 | } // if |
---|
| 164 | |
---|
[27de955] | 165 | // only generate these when needed |
---|
[a08ba92] | 166 | |
---|
[27de955] | 167 | if ( e.isContUsed() ) { |
---|
| 168 | // continue label goes in the body as the last statement |
---|
| 169 | std::list< Label > labels; labels.push_back( e.useContExit() ); |
---|
[e39aa0f] | 170 | newBody->get_kids().push_back( new NullStmt( labels ) ); |
---|
[adcc065] | 171 | } // if |
---|
[27de955] | 172 | |
---|
| 173 | if ( e.isBreakUsed() ) { |
---|
[adcc065] | 174 | // break label goes after the loop -- it'll get set by the outer mutator if we do this |
---|
[23b6f4d7] | 175 | set_breakLabel( e.useBreakExit() ); |
---|
[adcc065] | 176 | } // if |
---|
[a08ba92] | 177 | |
---|
| 178 | return newBody; |
---|
| 179 | } |
---|
| 180 | |
---|
[94e025a2] | 181 | template< typename LoopClass > |
---|
| 182 | void MLEMutator::prehandleLoopStmt( LoopClass * loopStmt ) { |
---|
| 183 | // remember this as the most recent enclosing loop, then mutate the body of the loop -- this will determine |
---|
| 184 | // whether brkLabel and contLabel are used with branch statements and will recursively do the same to nested |
---|
| 185 | // loops |
---|
| 186 | Label brkLabel = generator->newLabel("loopBreak", loopStmt); |
---|
| 187 | Label contLabel = generator->newLabel("loopContinue", loopStmt); |
---|
| 188 | enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) ); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | template< typename LoopClass > |
---|
| 192 | Statement * MLEMutator::posthandleLoopStmt( LoopClass * loopStmt ) { |
---|
| 193 | assert( ! enclosingControlStructures.empty() ); |
---|
| 194 | Entry &e = enclosingControlStructures.back(); |
---|
| 195 | // sanity check that the enclosing loops have been popped correctly |
---|
| 196 | assert ( e == loopStmt ); |
---|
| 197 | |
---|
| 198 | // this will take the necessary steps to add definitions of the previous two labels, if they are used. |
---|
| 199 | loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) ); |
---|
| 200 | enclosingControlStructures.pop_back(); |
---|
| 201 | return loopStmt; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | void MLEMutator::premutate( WhileStmt * whileStmt ) { |
---|
| 205 | return prehandleLoopStmt( whileStmt ); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | void MLEMutator::premutate( ForStmt * forStmt ) { |
---|
| 209 | return prehandleLoopStmt( forStmt ); |
---|
[27de955] | 210 | } |
---|
| 211 | |
---|
[94e025a2] | 212 | Statement * MLEMutator::postmutate( WhileStmt * whileStmt ) { |
---|
| 213 | return posthandleLoopStmt( whileStmt ); |
---|
[27de955] | 214 | } |
---|
| 215 | |
---|
[94e025a2] | 216 | Statement * MLEMutator::postmutate( ForStmt * forStmt ) { |
---|
| 217 | return posthandleLoopStmt( forStmt ); |
---|
[adcc065] | 218 | } |
---|
| 219 | |
---|
[94e025a2] | 220 | void MLEMutator::premutate( IfStmt * ifStmt ) { |
---|
| 221 | // generate a label for breaking out of a labeled if |
---|
| 222 | bool labeledBlock = !(ifStmt->get_labels().empty()); |
---|
| 223 | if ( labeledBlock ) { |
---|
| 224 | Label brkLabel = generator->newLabel("blockBreak", ifStmt); |
---|
| 225 | enclosingControlStructures.push_back( Entry( ifStmt, brkLabel ) ); |
---|
| 226 | } // if |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | Statement * MLEMutator::postmutate( IfStmt * ifStmt ) { |
---|
| 230 | bool labeledBlock = !(ifStmt->get_labels().empty()); |
---|
| 231 | if ( labeledBlock ) { |
---|
| 232 | if ( ! enclosingControlStructures.back().useBreakExit().empty() ) { |
---|
| 233 | set_breakLabel( enclosingControlStructures.back().useBreakExit() ); |
---|
| 234 | } // if |
---|
| 235 | enclosingControlStructures.pop_back(); |
---|
| 236 | } // if |
---|
| 237 | return ifStmt; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | void MLEMutator::premutate( CaseStmt *caseStmt ) { |
---|
| 241 | visit_children = false; |
---|
| 242 | caseStmt->condition = maybeMutate( caseStmt->condition, *visitor ); |
---|
| 243 | fixBlock( caseStmt->stmts ); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | void MLEMutator::premutate( SwitchStmt *switchStmt ) { |
---|
| 247 | // generate a label for breaking out of a labeled switch |
---|
| 248 | Label brkLabel = generator->newLabel("switchBreak", switchStmt); |
---|
| 249 | enclosingControlStructures.push_back( Entry(switchStmt, brkLabel) ); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | Statement * MLEMutator::postmutate( SwitchStmt * switchStmt ) { |
---|
| 253 | Entry &e = enclosingControlStructures.back(); |
---|
| 254 | assert ( e == switchStmt ); |
---|
| 255 | |
---|
| 256 | // only generate break label if labeled break is used |
---|
| 257 | if ( e.isBreakUsed() ) { |
---|
| 258 | // for the purposes of keeping switch statements uniform (i.e. all statements that are direct children of a |
---|
| 259 | // switch should be CastStmts), append the exit label + break to the last case statement; create a default |
---|
| 260 | // case if there are no cases |
---|
| 261 | std::list< Statement * > &statements = switchStmt->statements; |
---|
| 262 | if ( statements.empty() ) { |
---|
| 263 | statements.push_back( CaseStmt::makeDefault() ); |
---|
| 264 | } // if |
---|
| 265 | |
---|
| 266 | if ( CaseStmt * c = dynamic_cast< CaseStmt * >( statements.back() ) ) { |
---|
| 267 | Statement * stmt = new BranchStmt( Label("brkLabel"), BranchStmt::Break ); |
---|
| 268 | stmt->labels.push_back( e.useBreakExit() ); |
---|
| 269 | c->stmts.push_back( stmt ); |
---|
| 270 | } else assert(0); // as of this point, all statements of a switch are still CaseStmts |
---|
| 271 | } // if |
---|
| 272 | |
---|
| 273 | assert ( enclosingControlStructures.back() == switchStmt ); |
---|
| 274 | enclosingControlStructures.pop_back(); |
---|
| 275 | return switchStmt; |
---|
[a08ba92] | 276 | } |
---|
[51b7345] | 277 | } // namespace ControlStruct |
---|
[a08ba92] | 278 | |
---|
[51587aa] | 279 | // Local Variables: // |
---|
| 280 | // tab-width: 4 // |
---|
| 281 | // mode: c++ // |
---|
| 282 | // compile-command: "make install" // |
---|
| 283 | // End: // |
---|