source: src/ControlStruct/MLEMutator.cc @ 46cbfe1

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

error if continue statement targets a location that is not an enclosing loop, better error messages for branch statements with labels, formatting, refactoring

  • Property mode set to 100644
File size: 7.5 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 : Wed May 27 16:19:32 2015
13// Update Count     : 44
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                        // check if this is necessary (if there is a break to this point, otherwise do not generate
158                        std::list<Label> temp; temp.push_back( brkLabel );
159                        switchStmt->get_branches().push_back( new BranchStmt( temp, Label(""), BranchStmt::Break ) );
160                }
161                assert ( enclosingSwitches.back() == switchStmt );
162                enclosingSwitches.pop_back();
163                return switchStmt;
164        }
165
166        Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
167                return handleSwitchStmt( switchStmt, *this );
168        }
169
170        Statement *MLEMutator::mutate( ChooseStmt *switchStmt ) {
171                return handleSwitchStmt( switchStmt, *this );           
172        }
173
174        Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
175                CompoundStmt *newBody;
176                if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
177                        newBody = new CompoundStmt( std::list< Label >() );
178                        newBody->get_kids().push_back( bodyLoop );
179                } // if
180
181                Label endLabel = e.get_contExit();
182
183                if ( e.get_breakExit() != "" ) {
184                        if ( endLabel == "" ) endLabel = generator->newLabel();
185                        // check for whether this label is used or not, so as to not generate extraneous gotos
186                        if (e.breakExitUsed)
187                                newBody->get_kids().push_back( new BranchStmt( std::list< Label >(), endLabel, BranchStmt::Goto ) );
188                        // xxx
189                        //std::list< Label > ls; ls.push_back( e.get_breakExit() );
190                        set_breakLabel( e.get_breakExit() );
191                        //newBody->get_kids().push_back( new BranchStmt( ls, "", BranchStmt::Break ) );
192                } // if
193
194                if ( e.get_breakExit() != "" || e.get_contExit() != "" ) {
195                        if (dynamic_cast< NullStmt *>( newBody->get_kids().back() ))
196                                newBody->get_kids().back()->get_labels().push_back( endLabel );
197                        else {
198                                std::list < Label > ls; ls.push_back( endLabel );
199                                newBody->get_kids().push_back( new NullStmt( ls ) );
200                        } // if
201                } // if
202
203                return newBody;
204        }
205
206        //*** Entry's methods
207        void MLEMutator::Entry::set_contExit( Label l ) {
208                assert ( contExit == "" || contExit == l );
209                contExit = l;
210        }
211
212        void MLEMutator::Entry::set_breakExit( Label l ) {
213                assert ( breakExit == "" || breakExit == l );
214                breakExit = l;
215        }
216} // namespace ControlStruct
217
218// Local Variables: //
219// tab-width: 4 //
220// mode: c++ //
221// compile-command: "make install" //
222// End: //
Note: See TracBrowser for help on using the repository browser.