source: src/ControlStruct/MLEMutator.cc@ b2152e7a

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 string with_gc
Last change on this file since b2152e7a was b2152e7a, checked in by Rob Schluntz <rschlunt@…>, 11 years ago

fix MLE hack that puts a dangling break statement outside of a case statement in a switch

  • Property mode set to 100644
File size: 8.0 KB
Line 
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//
7// MLEMutator.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Tue Jun 02 13:35:49 2015
13// Update Count : 91
14//
15
16#include <cassert>
17#include <algorithm>
18
19#include "MLEMutator.h"
20#include "SynTree/Statement.h"
21
22namespace ControlStruct {
23 MLEMutator::~MLEMutator() {
24 delete targetTable;
25 targetTable = 0;
26 }
27
28 CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
29 bool labeledBlock = false;
30 if ( !(cmpndStmt->get_labels().empty()) ) {
31 labeledBlock = true;
32 enclosingBlocks.push_back( Entry( cmpndStmt ) );
33 } // if
34
35 std::list< Statement * > &kids = cmpndStmt->get_kids();
36 for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
37 *k = (*k)->acceptMutator(*this);
38
39 if ( ! get_breakLabel().empty() ) {
40 std::list< Statement * >::iterator next = k; next++;
41 if ( next == kids.end() ) {
42 std::list<Label> ls; ls.push_back( get_breakLabel() );
43 kids.push_back( new NullStmt( ls ) );
44 } else {
45 (*next)->get_labels().push_back( get_breakLabel() );
46 }
47
48 set_breakLabel("");
49 } // if
50 } // for
51
52 if ( labeledBlock ) {
53 assert( ! enclosingBlocks.empty() );
54 if ( ! enclosingBlocks.back().get_breakExit().empty() ) {
55 set_breakLabel( enclosingBlocks.back().get_breakExit() );
56 }
57 enclosingBlocks.pop_back();
58 } // if
59
60 //mutateAll( cmpndStmt->get_kids(), *this );
61 return cmpndStmt;
62 }
63
64 Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
65 enclosingLoops.push_back( Entry( whileStmt ) );
66 whileStmt->set_body ( whileStmt->get_body()->acceptMutator( *this ) );
67
68 Entry &e = enclosingLoops.back();
69 assert ( e == whileStmt );
70 whileStmt->set_body( mutateLoop( whileStmt->get_body(), e ) );
71 enclosingLoops.pop_back();
72
73 return whileStmt;
74 }
75
76 Statement *MLEMutator::mutate( ForStmt *forStmt ) {
77 enclosingLoops.push_back( Entry( forStmt ) );
78 maybeMutate( forStmt->get_body(), *this );
79
80 Entry &e = enclosingLoops.back();
81 assert ( e == forStmt );
82 forStmt->set_body( mutateLoop( forStmt->get_body(), e ) );
83 enclosingLoops.pop_back();
84
85 return forStmt;
86 }
87
88 Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
89 std::string originalTarget = branchStmt->get_originalTarget();
90
91 if ( branchStmt->get_type() == BranchStmt::Goto )
92 return branchStmt;
93
94 // test if continue target is a loop
95 if ( branchStmt->get_type() == BranchStmt::Continue) {
96 if ( enclosingLoops.empty() ) {
97 throw SemanticError( "'continue' outside a loop" );
98 } else if ( std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) == enclosingLoops.end() ) {
99 throw SemanticError( "'continue' target label must be an enclosing loop: " + originalTarget );
100 }
101 }
102
103 if ( branchStmt->get_type() == BranchStmt::Break && (enclosingLoops.empty() && enclosingSwitches.empty() && enclosingBlocks.empty() ) )
104 throw SemanticError( "'break' outside a loop or switch" );
105
106 if ( branchStmt->get_target() == "" ) return branchStmt;
107
108 if ( targetTable->find( branchStmt->get_target() ) == targetTable->end() )
109 throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget ); // shouldn't happen (since that's already checked)
110
111 std::list< Entry >::iterator check;
112 if ( ( check = std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) ) == enclosingLoops.end() )
113 // not in loop, checking if in block
114 if ( (check = std::find( enclosingBlocks.begin(), enclosingBlocks.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingBlocks.end() )
115 // neither in loop nor in block, checking if in switch/choose
116 if ( (check = std::find( enclosingSwitches.begin(), enclosingSwitches.end(), (*targetTable)[branchStmt->get_target()] )) == enclosingSwitches.end() )
117 throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing control structure: " + originalTarget );
118
119 if ( enclosingLoops.back() == (*check) )
120 return branchStmt; // exit the innermost loop (labels unnecessary)
121
122 Label newLabel;
123 switch ( branchStmt->get_type() ) {
124 case BranchStmt::Break:
125 if ( check->get_breakExit() != "" ) {
126 newLabel = check->get_breakExit();
127 } else {
128 newLabel = generator->newLabel();
129 check->set_breakExit( newLabel );
130 } // if
131 break;
132 case BranchStmt::Continue:
133 if ( check->get_contExit() != "" ) {
134 newLabel = check->get_contExit();
135 } else {
136 newLabel = generator->newLabel();
137 check->set_contExit( newLabel );
138 } // if
139 break;
140 default:
141 return 0; // shouldn't be here
142 } // switch
143
144 return new BranchStmt( std::list<Label>(), newLabel, BranchStmt::Goto );
145 }
146
147 template< typename SwitchClass >
148 Statement *handleSwitchStmt( SwitchClass *switchStmt, MLEMutator &mutator ) {
149 // set up some aliases so that the rest of the code isn't messy
150 typedef MLEMutator::Entry Entry;
151 LabelGenerator *generator = mutator.generator;
152 std::list< Entry > &enclosingSwitches = mutator.enclosingSwitches;
153
154 Label brkLabel = generator->newLabel();
155 enclosingSwitches.push_back( Entry(switchStmt, "", brkLabel) );
156 mutateAll( switchStmt->get_branches(), mutator );
157 {
158 // check if this is necessary (if there is a break to this point, otherwise do not generate
159
160 // for the purposes of keeping switch statements uniform (i.e. all statements that are
161 // direct children of a switch should be CastStmts), append the exit label + break to the
162 // last case statement; create a default case if there are no cases
163 std::list< Statement * > &branches = switchStmt->get_branches();
164 if ( branches.empty() ) {
165 branches.push_back( CaseStmt::makeDefault() );
166 }
167
168 if ( CaseStmt * c = dynamic_cast< CaseStmt * >( branches.back() ) ) {
169 std::list<Label> temp; temp.push_back( brkLabel );
170 c->get_statements().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
171 } else assert(0);
172 }
173 assert ( enclosingSwitches.back() == switchStmt );
174 enclosingSwitches.pop_back();
175 return switchStmt;
176 }
177
178 Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
179 return handleSwitchStmt( switchStmt, *this );
180 }
181
182 Statement *MLEMutator::mutate( ChooseStmt *switchStmt ) {
183 return handleSwitchStmt( switchStmt, *this );
184 }
185
186 Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
187 CompoundStmt *newBody;
188 if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
189 newBody = new CompoundStmt( std::list< Label >() );
190 newBody->get_kids().push_back( bodyLoop );
191 } // if
192
193 Label endLabel = e.get_contExit();
194
195 if ( e.get_breakExit() != "" ) {
196 if ( endLabel == "" ) endLabel = generator->newLabel();
197 // check for whether this label is used or not, so as to not generate extraneous gotos
198 if (e.breakExitUsed)
199 newBody->get_kids().push_back( new BranchStmt( std::list< Label >(), endLabel, BranchStmt::Goto ) );
200 // xxx
201 //std::list< Label > ls; ls.push_back( e.get_breakExit() );
202 set_breakLabel( e.get_breakExit() );
203 //newBody->get_kids().push_back( new BranchStmt( ls, "", BranchStmt::Break ) );
204 } // if
205
206 if ( e.get_breakExit() != "" || e.get_contExit() != "" ) {
207 if (dynamic_cast< NullStmt *>( newBody->get_kids().back() ))
208 newBody->get_kids().back()->get_labels().push_back( endLabel );
209 else {
210 std::list < Label > ls; ls.push_back( endLabel );
211 newBody->get_kids().push_back( new NullStmt( ls ) );
212 } // if
213 } // if
214
215 return newBody;
216 }
217
218 //*** Entry's methods
219 void MLEMutator::Entry::set_contExit( Label l ) {
220 assert ( contExit == "" || contExit == l );
221 contExit = l;
222 }
223
224 void MLEMutator::Entry::set_breakExit( Label l ) {
225 assert ( breakExit == "" || breakExit == l );
226 breakExit = l;
227 }
228} // namespace ControlStruct
229
230// Local Variables: //
231// tab-width: 4 //
232// mode: c++ //
233// compile-command: "make install" //
234// End: //
Note: See TracBrowser for help on using the repository browser.