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