source: src/ControlStruct/MLEMutator.cc@ c2931ea

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since c2931ea was 23b6f4d7, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

change Label type from std::string to a custom Label class, link label to its statement

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