source: src/ControlStruct/MLEMutator.cc @ 8688ce1

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 8688ce1 was 8688ce1, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

move case-list management into parser

  • Property mode set to 100644
File size: 9.6 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 : 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 <cassert>
23#include <algorithm>
24
25#include "MLEMutator.h"
26#include "SynTree/Statement.h"
27#include "SynTree/Expression.h"
28#include "SynTree/Attribute.h"
29
30namespace ControlStruct {
31        MLEMutator::~MLEMutator() {
32                delete targetTable;
33                targetTable = 0;
34        }
35        namespace {
36                Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; }
37        }
38
39        // break labels have to come after the statement they break out of, so mutate a statement, then if they inform us
40        // through the breakLabel field tha they need a place to jump to on a break statement, add the break label to the
41        // body of statements
42        void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
43                for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
44                        *k = (*k)->acceptMutator(*this);
45
46                        if ( ! get_breakLabel().empty() ) {
47                                std::list< Statement * >::iterator next = k+1;
48                                std::list<Label> ls; ls.push_back( get_breakLabel() );
49                                kids.insert( next, new NullStmt( ls ) );
50                                set_breakLabel("");
51                        } // if
52                } // for
53        }
54
55        CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
56                bool labeledBlock = !(cmpndStmt->get_labels().empty());
57                if ( labeledBlock ) {
58                        Label brkLabel = generator->newLabel("blockBreak");
59                        enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) );
60                } // if
61
62                // a child statement may set the break label - if they do, attach it to the next statement
63                std::list< Statement * > &kids = cmpndStmt->get_kids();
64                fixBlock( kids );
65
66                if ( labeledBlock ) {
67                        assert( ! enclosingControlStructures.empty() );
68                        if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
69                                set_breakLabel( enclosingControlStructures.back().useBreakExit() );
70                        } // if
71                        enclosingControlStructures.pop_back();
72                } // if
73
74                return cmpndStmt;
75        }
76
77        template< typename LoopClass >
78        Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) {
79                // remember this as the most recent enclosing loop, then mutate the body of the loop -- this will determine
80                // whether brkLabel and contLabel are used with branch statements and will recursively do the same to nested
81                // loops
82                Label brkLabel = generator->newLabel("loopBreak");
83                Label contLabel = generator->newLabel("loopContinue");
84                enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) );
85                loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) );
86
87                Entry &e = enclosingControlStructures.back();
88                // sanity check that the enclosing loops have been popped correctly
89                assert ( e == loopStmt );
90
91                // this will take the necessary steps to add definitions of the previous two labels, if they are used.
92                loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) );
93                enclosingControlStructures.pop_back();
94
95                return loopStmt;
96        }
97
98        Statement *MLEMutator::mutate( CaseStmt *caseStmt ) {
99                caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) );
100                fixBlock( caseStmt->get_statements() );
101
102                return caseStmt;
103        }
104
105        template< typename IfClass >
106        Statement *MLEMutator::handleIfStmt( IfClass *ifStmt ) {
107                // generate a label for breaking out of a labeled if
108                bool labeledBlock = !(ifStmt->get_labels().empty());
109                if ( labeledBlock ) {
110                        Label brkLabel = generator->newLabel("blockBreak");
111                        enclosingControlStructures.push_back( Entry( ifStmt, brkLabel ) );
112                } // if
113
114                Parent::mutate( ifStmt );
115               
116                if ( labeledBlock ) {
117                        if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
118                                set_breakLabel( enclosingControlStructures.back().useBreakExit() );
119                        } // if
120                        enclosingControlStructures.pop_back();
121                } // if
122                return ifStmt;
123        }
124
125        template< typename SwitchClass >
126        Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) {
127                // generate a label for breaking out of a labeled switch
128                Label brkLabel = generator->newLabel("switchBreak");
129                enclosingControlStructures.push_back( Entry(switchStmt, brkLabel) );
130                mutateAll( switchStmt->get_statements(), *this );
131
132                Entry &e = enclosingControlStructures.back();
133                assert ( e == switchStmt );
134
135                // only generate break label if labeled break is used
136                if ( e.isBreakUsed() ) {
137                        // for the purposes of keeping switch statements uniform (i.e. all statements that are direct children of a
138                        // switch should be CastStmts), append the exit label + break to the last case statement; create a default
139                        // case if there are no cases
140                        std::list< Statement * > &statements = switchStmt->get_statements();
141                        if ( statements.empty() ) {
142                                statements.push_back( CaseStmt::makeDefault() );
143                        } // if
144
145                        if ( CaseStmt * c = dynamic_cast< CaseStmt * >( statements.back() ) ) {
146                                std::list<Label> temp; temp.push_back( brkLabel );
147                                c->get_statements().push_back( new BranchStmt( temp, Label("brkLabel"), BranchStmt::Break ) );
148                        } else assert(0); // as of this point, all statements of a switch are still CaseStmts
149                } // if
150
151                assert ( enclosingControlStructures.back() == switchStmt );
152                enclosingControlStructures.pop_back();
153                return switchStmt;
154        }
155
156        Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
157                std::string originalTarget = branchStmt->get_originalTarget();
158
159                std::list< Entry >::reverse_iterator targetEntry;
160                if ( branchStmt->get_type() == BranchStmt::Goto ) {
161                        return branchStmt;
162                } else if ( branchStmt->get_type() == BranchStmt::Continue) {
163                        // continue target must be a loop
164                        if ( branchStmt->get_target() == "" ) {
165                                targetEntry = std::find_if( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), [](Entry &e) { return isLoop( e.get_controlStructure() ); } );
166                        } else {
167                                // labelled continue - lookup label in table ot find attached control structure
168                                targetEntry = std::find( enclosingControlStructures.rbegin(), enclosingControlStructures.rend(), (*targetTable)[branchStmt->get_target()] );
169                        } // if
170                        if ( targetEntry == enclosingControlStructures.rend() || ! isLoop( targetEntry->get_controlStructure() ) ) {
171                                throw SemanticError( "'continue' target must be an enclosing loop: " + originalTarget );
172                        } // if
173                } else if ( branchStmt->get_type() == BranchStmt::Break ) {
174                        if ( enclosingControlStructures.empty() ) throw SemanticError( "'break' outside a loop, switch, or labelled block" );
175                        targetEntry = enclosingControlStructures.rbegin();
176                } else {
177                        assert( false );
178                } // if
179
180                if ( branchStmt->get_target() != "" && targetTable->find( branchStmt->get_target() ) == targetTable->end() ) {
181                        throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget );  // shouldn't happen (since that's already checked)
182                } // if
183
184                // branch error checks, get the appropriate label name and create a goto
185                Label exitLabel;
186                switch ( branchStmt->get_type() ) {
187                  case BranchStmt::Break:
188                                assert( targetEntry->useBreakExit() != "");
189                                exitLabel = targetEntry->useBreakExit();
190                                break;
191                  case BranchStmt::Continue:
192                                assert( targetEntry->useContExit() != "");
193                                exitLabel = targetEntry->useContExit();
194                                break;
195                  default:
196                                assert(0);                                      // shouldn't be here
197                } // switch
198
199                if ( branchStmt->get_target() == "" && branchStmt->get_type() != BranchStmt::Continue ) {
200                        // unlabelled break/continue - can keep branch as break/continue for extra semantic information, but add
201                        // exitLabel as its destination so that label passes can easily determine where the break/continue goes to
202                        branchStmt->set_target( exitLabel );
203                        return branchStmt;
204                } else {
205                        // labelled break/continue - can't easily emulate this with break and continue, so transform into a goto
206                        delete branchStmt;
207                        return new BranchStmt( std::list<Label>(), exitLabel, BranchStmt::Goto );
208                } // if
209        }
210
211        Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
212                // ensure loop body is a block
213                CompoundStmt *newBody;
214                if ( ! (newBody = dynamic_cast<CompoundStmt *>( bodyLoop )) ) {
215                        newBody = new CompoundStmt( std::list< Label >() );
216                        newBody->get_kids().push_back( bodyLoop );
217                } // if
218
219                // only generate these when needed
220
221                if ( e.isContUsed() ) {
222                        // continue label goes in the body as the last statement
223                        std::list< Label > labels; labels.push_back( e.useContExit() );
224                        newBody->get_kids().push_back( new NullStmt( labels ) );
225                } // if
226
227                if ( e.isBreakUsed() ) {
228                        // break label goes after the loop -- it'll get set by the outer mutator if we do this
229                        set_breakLabel( e.useBreakExit() );
230                } // if
231
232                return newBody;
233        }
234
235        Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
236                return handleLoopStmt( whileStmt );
237        }
238
239        Statement *MLEMutator::mutate( ForStmt *forStmt ) {
240                return handleLoopStmt( forStmt );
241        }
242
243        Statement *MLEMutator::mutate( IfStmt *ifStmt ) {
244                return handleIfStmt( ifStmt );
245        }
246
247        Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
248                return handleSwitchStmt( switchStmt );
249        }
250} // namespace ControlStruct
251
252// Local Variables: //
253// tab-width: 4 //
254// mode: c++ //
255// compile-command: "make install" //
256// End: //
Note: See TracBrowser for help on using the repository browser.