[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_...
|
---|
[51b73452] | 26 |
|
---|
[d180746] | 27 | #include "Common/utility.h" // for toString, operator+
|
---|
| 28 | #include "ControlStruct/LabelGenerator.h" // for LabelGenerator
|
---|
[51b73452] | 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
|
---|
[51b73452] | 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() ) ) {
|
---|
[ba3706f] | 151 | Statement * stmt = new BranchStmt( Label("brkLabel"), BranchStmt::Break );
|
---|
| 152 | stmt->labels.push_back( brkLabel );
|
---|
| 153 | c->get_statements().push_back( stmt );
|
---|
[8688ce1] | 154 | } else assert(0); // as of this point, all statements of a switch are still CaseStmts
|
---|
[adcc065] | 155 | } // if
|
---|
[27de955] | 156 |
|
---|
[e39aa0f] | 157 | assert ( enclosingControlStructures.back() == switchStmt );
|
---|
| 158 | enclosingControlStructures.pop_back();
|
---|
[27de955] | 159 | return switchStmt;
|
---|
[d9a0e76] | 160 | }
|
---|
[a08ba92] | 161 |
|
---|
[33c4b81] | 162 | void addUnused( Statement * stmt, const Label & originalTarget ) {
|
---|
| 163 | // break/continue without a label doesn't need unused attribute
|
---|
| 164 | if ( originalTarget == "" ) return;
|
---|
| 165 | // add unused attribute to the originalTarget of a labelled break/continue
|
---|
| 166 | for ( Label & l : stmt->get_labels() ) {
|
---|
| 167 | // find the label to add unused attribute to
|
---|
| 168 | if ( l == originalTarget ) {
|
---|
| 169 | for ( Attribute * attr : l.get_attributes() ) {
|
---|
| 170 | // ensure attribute isn't added twice
|
---|
| 171 | if ( attr->get_name() == "unused" ) return;
|
---|
| 172 | }
|
---|
| 173 | l.get_attributes().push_back( new Attribute( "unused" ) );
|
---|
| 174 | return;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | assertf( false, "Could not find label '%s' on statement %s", originalTarget.get_name().c_str(), toString( stmt ).c_str() );
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
[a08ba92] | 181 | Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
|
---|
[be5aa1b] | 182 | std::string originalTarget = branchStmt->get_originalTarget();
|
---|
| 183 |
|
---|
[e39aa0f] | 184 | std::list< Entry >::reverse_iterator targetEntry;
|
---|
[af68f0a] | 185 | switch ( branchStmt->get_type() ) {
|
---|
| 186 | case BranchStmt::Goto:
|
---|
| 187 | return branchStmt;
|
---|
| 188 | case BranchStmt::Continue:
|
---|
| 189 | case BranchStmt::Break: {
|
---|
| 190 | bool isContinue = branchStmt->get_type() == BranchStmt::Continue;
|
---|
| 191 | // unlabeled break/continue
|
---|
| 192 | if ( branchStmt->get_target() == "" ) {
|
---|
| 193 | if ( isContinue ) {
|
---|
| 194 | // continue target is outermost loop
|
---|
| 195 | targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), [](Entry &e) { return isLoop( e.get_controlStructure() ); } );
|
---|
| 196 | } else {
|
---|
| 197 | // break target is outmost control structure
|
---|
| 198 | if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" );
|
---|
| 199 | targetEntry = enclosingControlStructures.rbegin();
|
---|
| 200 | } // if
|
---|
| 201 | } else {
|
---|
| 202 | // labeled break/continue - lookup label in table to find attached control structure
|
---|
| 203 | targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] );
|
---|
| 204 | } // if
|
---|
| 205 | // ensure that selected target is valid
|
---|
| 206 | if ( targetEntry == enclosingControlStructures.rend() || (isContinue && ! isLoop( targetEntry->get_controlStructure() ) ) ) {
|
---|
| 207 | throw SemanticError( toString( (isContinue ? "'continue'" : "'break'"), " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), originalTarget ) );
|
---|
| 208 | } // if
|
---|
| 209 | break;
|
---|
| 210 | }
|
---|
| 211 | default:
|
---|
| 212 | assert( false );
|
---|
| 213 | } // switch
|
---|
[a08ba92] | 214 |
|
---|
[27de955] | 215 | // branch error checks, get the appropriate label name and create a goto
|
---|
| 216 | Label exitLabel;
|
---|
[a08ba92] | 217 | switch ( branchStmt->get_type() ) {
|
---|
| 218 | case BranchStmt::Break:
|
---|
[e39aa0f] | 219 | assert( targetEntry->useBreakExit() != "");
|
---|
| 220 | exitLabel = targetEntry->useBreakExit();
|
---|
[be5aa1b] | 221 | break;
|
---|
[a08ba92] | 222 | case BranchStmt::Continue:
|
---|
[e39aa0f] | 223 | assert( targetEntry->useContExit() != "");
|
---|
| 224 | exitLabel = targetEntry->useContExit();
|
---|
[be5aa1b] | 225 | break;
|
---|
[a08ba92] | 226 | default:
|
---|
[27de955] | 227 | assert(0); // shouldn't be here
|
---|
[a08ba92] | 228 | } // switch
|
---|
| 229 |
|
---|
[33c4b81] | 230 | // add unused attribute to label to silence warnings
|
---|
| 231 | addUnused( targetEntry->get_controlStructure(), branchStmt->get_originalTarget() );
|
---|
| 232 |
|
---|
[af68f0a] | 233 | // transform break/continue statements into goto to simplify later handling of branches
|
---|
| 234 | delete branchStmt;
|
---|
[ba3706f] | 235 | return new BranchStmt( exitLabel, BranchStmt::Goto );
|
---|
[a08ba92] | 236 | }
|
---|
| 237 |
|
---|
| 238 | Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
|
---|
[d939274] | 239 | // ensure loop body is a block
|
---|
[a08ba92] | 240 | CompoundStmt *newBody;
|
---|
| 241 | if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
|
---|
[ba3706f] | 242 | newBody = new CompoundStmt();
|
---|
[a08ba92] | 243 | newBody->get_kids().push_back( bodyLoop );
|
---|
| 244 | } // if
|
---|
| 245 |
|
---|
[27de955] | 246 | // only generate these when needed
|
---|
[a08ba92] | 247 |
|
---|
[27de955] | 248 | if ( e.isContUsed() ) {
|
---|
| 249 | // continue label goes in the body as the last statement
|
---|
| 250 | std::list< Label > labels; labels.push_back( e.useContExit() );
|
---|
[e39aa0f] | 251 | newBody->get_kids().push_back( new NullStmt( labels ) );
|
---|
[adcc065] | 252 | } // if
|
---|
[27de955] | 253 |
|
---|
| 254 | if ( e.isBreakUsed() ) {
|
---|
[adcc065] | 255 | // break label goes after the loop -- it'll get set by the outer mutator if we do this
|
---|
[23b6f4d7] | 256 | set_breakLabel( e.useBreakExit() );
|
---|
[adcc065] | 257 | } // if
|
---|
[a08ba92] | 258 |
|
---|
| 259 | return newBody;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[27de955] | 262 | Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
|
---|
| 263 | return handleLoopStmt( whileStmt );
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | Statement *MLEMutator::mutate( ForStmt *forStmt ) {
|
---|
| 267 | return handleLoopStmt( forStmt );
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[adcc065] | 270 | Statement *MLEMutator::mutate( IfStmt *ifStmt ) {
|
---|
| 271 | return handleIfStmt( ifStmt );
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[27de955] | 274 | Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
|
---|
| 275 | return handleSwitchStmt( switchStmt );
|
---|
[a08ba92] | 276 | }
|
---|
[51b73452] | 277 | } // namespace ControlStruct
|
---|
[a08ba92] | 278 |
|
---|
[51587aa] | 279 | // Local Variables: //
|
---|
| 280 | // tab-width: 4 //
|
---|
| 281 | // mode: c++ //
|
---|
| 282 | // compile-command: "make install" //
|
---|
| 283 | // End: //
|
---|