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 : Mon Jul 20 13:58:35 2015
|
---|
13 | // Update Count : 176
|
---|
14 | //
|
---|
15 |
|
---|
16 | // NOTE: There are two known subtle differences from the code that uC++
|
---|
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 |
|
---|
23 | #include <cassert>
|
---|
24 | #include <algorithm>
|
---|
25 |
|
---|
26 | #include "MLEMutator.h"
|
---|
27 | #include "SynTree/Statement.h"
|
---|
28 | #include "SynTree/Expression.h"
|
---|
29 |
|
---|
30 | namespace ControlStruct {
|
---|
31 | MLEMutator::~MLEMutator() {
|
---|
32 | delete targetTable;
|
---|
33 | targetTable = 0;
|
---|
34 | }
|
---|
35 |
|
---|
36 | // break labels have to come after the statement they break out of,
|
---|
37 | // so mutate a statement, then if they inform us through the breakLabel field
|
---|
38 | // tha they need a place to jump to on a break statement, add the break label
|
---|
39 | // to the body of statements
|
---|
40 | void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
|
---|
41 | for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
|
---|
42 | *k = (*k)->acceptMutator(*this);
|
---|
43 |
|
---|
44 | if ( ! get_breakLabel().empty() ) {
|
---|
45 | std::list< Statement * >::iterator next = k+1;
|
---|
46 | Statement * stmt = 0;
|
---|
47 | if ( next == kids.end() ) {
|
---|
48 | std::list<Label> ls; ls.push_back( get_breakLabel() );
|
---|
49 | kids.push_back( stmt = new NullStmt( ls ) );
|
---|
50 | } else {
|
---|
51 | (stmt = *next)->get_labels().push_back( get_breakLabel() );
|
---|
52 | }
|
---|
53 | stmt->get_labels().front().set_statement( stmt );
|
---|
54 |
|
---|
55 | set_breakLabel("");
|
---|
56 | } // if
|
---|
57 | } // for
|
---|
58 | }
|
---|
59 |
|
---|
60 | CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
|
---|
61 | bool labeledBlock = !(cmpndStmt->get_labels().empty());
|
---|
62 | if ( labeledBlock ) {
|
---|
63 | Label brkLabel = generator->newLabel("blockBreak");
|
---|
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 );
|
---|
71 |
|
---|
72 | if ( labeledBlock ) {
|
---|
73 | assert( ! enclosingBlocks.empty() );
|
---|
74 | if ( ! enclosingBlocks.back().useBreakExit().empty() ) {
|
---|
75 | set_breakLabel( enclosingBlocks.back().useBreakExit() );
|
---|
76 | }
|
---|
77 | enclosingBlocks.pop_back();
|
---|
78 | } // if
|
---|
79 |
|
---|
80 | return cmpndStmt;
|
---|
81 | }
|
---|
82 |
|
---|
83 | template< typename LoopClass >
|
---|
84 | Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) {
|
---|
85 | // remember this as the most recent enclosing loop, then mutate
|
---|
86 | // the body of the loop -- this will determine whether brkLabel
|
---|
87 | // and contLabel are used with branch statements
|
---|
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
|
---|
95 | Entry &e = enclosingLoops.back();
|
---|
96 | assert ( e == loopStmt );
|
---|
97 |
|
---|
98 | // this will take the necessary steps to add definitions of the previous
|
---|
99 | // two labels, if they are used.
|
---|
100 | loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) );
|
---|
101 | enclosingLoops.pop_back();
|
---|
102 |
|
---|
103 | return loopStmt;
|
---|
104 | }
|
---|
105 |
|
---|
106 | Statement *MLEMutator::mutate( CaseStmt *caseStmt ) {
|
---|
107 | caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) );
|
---|
108 | fixBlock( caseStmt->get_statements() );
|
---|
109 |
|
---|
110 | return caseStmt;
|
---|
111 | }
|
---|
112 |
|
---|
113 | template< typename SwitchClass >
|
---|
114 | Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) {
|
---|
115 | // generate a label for breaking out of a labeled switch
|
---|
116 | Label brkLabel = generator->newLabel("switchBreak");
|
---|
117 | enclosingSwitches.push_back( Entry(switchStmt, brkLabel) );
|
---|
118 | mutateAll( switchStmt->get_branches(), *this );
|
---|
119 |
|
---|
120 | Entry &e = enclosingSwitches.back();
|
---|
121 | assert ( e == switchStmt );
|
---|
122 |
|
---|
123 | // only generate break label if labeled break is used
|
---|
124 | if (e.isBreakUsed()) {
|
---|
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
|
---|
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 );
|
---|
135 | Statement * stmt = new BranchStmt( temp, Label(""), BranchStmt::Break );
|
---|
136 | stmt->get_labels().front().set_statement( stmt );
|
---|
137 | c->get_statements().push_back( stmt );
|
---|
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;
|
---|
144 | }
|
---|
145 |
|
---|
146 | Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
|
---|
147 | std::string originalTarget = branchStmt->get_originalTarget();
|
---|
148 |
|
---|
149 | if ( branchStmt->get_type() == BranchStmt::Goto )
|
---|
150 | return branchStmt;
|
---|
151 |
|
---|
152 | // test if continue target is a loop
|
---|
153 | if ( branchStmt->get_type() == BranchStmt::Continue) {
|
---|
154 | if ( enclosingLoops.empty() ) {
|
---|
155 | throw SemanticError( "'continue' outside a loop" );
|
---|
156 | } else if ( branchStmt->get_target() != "" && std::find( enclosingLoops.begin(), enclosingLoops.end(), (*targetTable)[branchStmt->get_target()] ) == enclosingLoops.end() ) {
|
---|
157 | throw SemanticError( "'continue' target label must be an enclosing loop: " + originalTarget );
|
---|
158 | }
|
---|
159 | }
|
---|
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() )
|
---|
167 | throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget ); // shouldn't happen (since that's already checked)
|
---|
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() )
|
---|
175 | throw SemanticError("The target specified in the exit loop statement does not correspond to an enclosing control structure: " + originalTarget );
|
---|
176 |
|
---|
177 | // what about exiting innermost block or switch???
|
---|
178 | if ( enclosingLoops.back() == (*check) )
|
---|
179 | return branchStmt; // exit the innermost loop (labels unnecessary)
|
---|
180 |
|
---|
181 | // branch error checks, get the appropriate label name and create a goto
|
---|
182 | Label exitLabel;
|
---|
183 | switch ( branchStmt->get_type() ) {
|
---|
184 | case BranchStmt::Break:
|
---|
185 | assert( check->useBreakExit() != "");
|
---|
186 | exitLabel = check->useBreakExit();
|
---|
187 | break;
|
---|
188 | case BranchStmt::Continue:
|
---|
189 | assert( check->useContExit() != "");
|
---|
190 | exitLabel = check->useContExit();
|
---|
191 | break;
|
---|
192 | default:
|
---|
193 | assert(0); // shouldn't be here
|
---|
194 | } // switch
|
---|
195 |
|
---|
196 | return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto );
|
---|
197 | }
|
---|
198 |
|
---|
199 | Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
|
---|
200 | // ensure loop body is a block
|
---|
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 |
|
---|
207 | // only generate these when needed
|
---|
208 |
|
---|
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() );
|
---|
212 | Statement * stmt = new NullStmt( labels );
|
---|
213 | stmt->get_labels().front().set_statement( stmt );
|
---|
214 | newBody->get_kids().push_back( stmt );
|
---|
215 | }
|
---|
216 |
|
---|
217 | if ( e.isBreakUsed() ) {
|
---|
218 | // break label goes after the loop -- it'll get set by the
|
---|
219 | // outer mutator if we do this
|
---|
220 | set_breakLabel( e.useBreakExit() );
|
---|
221 | }
|
---|
222 |
|
---|
223 | return newBody;
|
---|
224 | }
|
---|
225 |
|
---|
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 );
|
---|
236 | }
|
---|
237 |
|
---|
238 | Statement *MLEMutator::mutate( ChooseStmt *switchStmt ) {
|
---|
239 | return handleSwitchStmt( switchStmt );
|
---|
240 | }
|
---|
241 |
|
---|
242 | } // namespace ControlStruct
|
---|
243 |
|
---|
244 | // Local Variables: //
|
---|
245 | // tab-width: 4 //
|
---|
246 | // mode: c++ //
|
---|
247 | // compile-command: "make install" //
|
---|
248 | // End: //
|
---|