source: src/ControlStruct/MLEMutator.cc @ 08a40fd

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 08a40fd was 994ec2c, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fix warning in logical expression

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