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 : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Aug 4 11:21:32 2016 |
---|
13 | // Update Count : 202 |
---|
14 | // |
---|
15 | |
---|
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. |
---|
21 | |
---|
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_... |
---|
26 | |
---|
27 | #include "Common/utility.h" // for toString, operator+ |
---|
28 | #include "ControlStruct/LabelGenerator.h" // for LabelGenerator |
---|
29 | #include "MLEMutator.h" |
---|
30 | #include "SynTree/Attribute.h" // for Attribute |
---|
31 | #include "SynTree/Expression.h" // for Expression |
---|
32 | #include "SynTree/Statement.h" // for BranchStmt, CompoundStmt |
---|
33 | |
---|
34 | namespace ControlStruct { |
---|
35 | MLEMutator::~MLEMutator() { |
---|
36 | delete targetTable; |
---|
37 | targetTable = 0; |
---|
38 | } |
---|
39 | namespace { |
---|
40 | Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; } |
---|
41 | } |
---|
42 | |
---|
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 |
---|
46 | void MLEMutator::fixBlock( std::list< Statement * > &kids ) { |
---|
47 | for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) { |
---|
48 | *k = (*k)->acceptMutator(*visitor); |
---|
49 | |
---|
50 | if ( ! get_breakLabel().empty() ) { |
---|
51 | std::list< Statement * >::iterator next = k+1; |
---|
52 | std::list<Label> ls; ls.push_back( get_breakLabel() ); |
---|
53 | kids.insert( next, new NullStmt( ls ) ); |
---|
54 | set_breakLabel(""); |
---|
55 | } // if |
---|
56 | } // for |
---|
57 | } |
---|
58 | |
---|
59 | void MLEMutator::premutate( CompoundStmt *cmpndStmt ) { |
---|
60 | visit_children = false; |
---|
61 | bool labeledBlock = !(cmpndStmt->labels.empty()); |
---|
62 | if ( labeledBlock ) { |
---|
63 | Label brkLabel = generator->newLabel("blockBreak", cmpndStmt); |
---|
64 | enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) ); |
---|
65 | } // if |
---|
66 | |
---|
67 | // a child statement may set the break label - if they do, attach it to the next statement |
---|
68 | std::list< Statement * > &kids = cmpndStmt->kids; |
---|
69 | fixBlock( kids ); |
---|
70 | |
---|
71 | if ( labeledBlock ) { |
---|
72 | assert( ! enclosingControlStructures.empty() ); |
---|
73 | if ( ! enclosingControlStructures.back().useBreakExit().empty() ) { |
---|
74 | set_breakLabel( enclosingControlStructures.back().useBreakExit() ); |
---|
75 | } // if |
---|
76 | enclosingControlStructures.pop_back(); |
---|
77 | } // if |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
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 | |
---|
100 | Statement *MLEMutator::postmutate( BranchStmt *branchStmt ) throw ( SemanticError ) { |
---|
101 | std::string originalTarget = branchStmt->originalTarget; |
---|
102 | |
---|
103 | std::list< Entry >::reverse_iterator targetEntry; |
---|
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 |
---|
133 | |
---|
134 | // branch error checks, get the appropriate label name and create a goto |
---|
135 | Label exitLabel; |
---|
136 | switch ( branchStmt->type ) { |
---|
137 | case BranchStmt::Break: |
---|
138 | assert( targetEntry->useBreakExit() != ""); |
---|
139 | exitLabel = targetEntry->useBreakExit(); |
---|
140 | break; |
---|
141 | case BranchStmt::Continue: |
---|
142 | assert( targetEntry->useContExit() != ""); |
---|
143 | exitLabel = targetEntry->useContExit(); |
---|
144 | break; |
---|
145 | default: |
---|
146 | assert(0); // shouldn't be here |
---|
147 | } // switch |
---|
148 | |
---|
149 | // add unused attribute to label to silence warnings |
---|
150 | addUnused( targetEntry->get_controlStructure(), branchStmt->originalTarget ); |
---|
151 | |
---|
152 | // transform break/continue statements into goto to simplify later handling of branches |
---|
153 | delete branchStmt; |
---|
154 | return new BranchStmt( exitLabel, BranchStmt::Goto ); |
---|
155 | } |
---|
156 | |
---|
157 | Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) { |
---|
158 | // ensure loop body is a block |
---|
159 | CompoundStmt *newBody; |
---|
160 | if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) { |
---|
161 | newBody = new CompoundStmt(); |
---|
162 | newBody->get_kids().push_back( bodyLoop ); |
---|
163 | } // if |
---|
164 | |
---|
165 | // only generate these when needed |
---|
166 | |
---|
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() ); |
---|
170 | newBody->get_kids().push_back( new NullStmt( labels ) ); |
---|
171 | } // if |
---|
172 | |
---|
173 | if ( e.isBreakUsed() ) { |
---|
174 | // break label goes after the loop -- it'll get set by the outer mutator if we do this |
---|
175 | set_breakLabel( e.useBreakExit() ); |
---|
176 | } // if |
---|
177 | |
---|
178 | return newBody; |
---|
179 | } |
---|
180 | |
---|
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 ); |
---|
210 | } |
---|
211 | |
---|
212 | Statement * MLEMutator::postmutate( WhileStmt * whileStmt ) { |
---|
213 | return posthandleLoopStmt( whileStmt ); |
---|
214 | } |
---|
215 | |
---|
216 | Statement * MLEMutator::postmutate( ForStmt * forStmt ) { |
---|
217 | return posthandleLoopStmt( forStmt ); |
---|
218 | } |
---|
219 | |
---|
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; |
---|
276 | } |
---|
277 | } // namespace ControlStruct |
---|
278 | |
---|
279 | // Local Variables: // |
---|
280 | // tab-width: 4 // |
---|
281 | // mode: c++ // |
---|
282 | // compile-command: "make install" // |
---|
283 | // End: // |
---|