source: src/ControlStruct/MLEMutator.cc @ 4e06c1e

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 4e06c1e was 4e06c1e, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

changes for switch and choose statements

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[51587aa]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//
[23b6f4d7]7// MLEMutator.cc --
[51587aa]8//
[843054c2]9// Author           : Rodolfo G. Esteves
[51587aa]10// Created On       : Mon May 18 07:44:20 2015
[adcc065]11// Last Modified By : Peter A. Buhr
[4e06c1e]12// Last Modified On : Tue Jul 12 17:36:51 2016
13// Update Count     : 197
[51587aa]14//
[a08ba92]15
[adcc065]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.
[994ec2c]21
[51b7345]22#include <cassert>
23#include <algorithm>
24
25#include "MLEMutator.h"
26#include "SynTree/Statement.h"
[27de955]27#include "SynTree/Expression.h"
[e39aa0f]28#include "SynTree/Attribute.h"
[51b7345]29
30namespace ControlStruct {
[a08ba92]31        MLEMutator::~MLEMutator() {
32                delete targetTable;
33                targetTable = 0;
34        }
[e39aa0f]35        namespace {
36                Statement * isLoop( Statement * stmt ) { return dynamic_cast< WhileStmt * >( stmt ) ? stmt : dynamic_cast< ForStmt * >( stmt ) ? stmt : 0; }
37        }
[a08ba92]38
[adcc065]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
[27de955]42        void MLEMutator::fixBlock( std::list< Statement * > &kids ) {
[a08ba92]43                for ( std::list< Statement * >::iterator k = kids.begin(); k != kids.end(); k++ ) {
44                        *k = (*k)->acceptMutator(*this);
45
46                        if ( ! get_breakLabel().empty() ) {
[d939274]47                                std::list< Statement * >::iterator next = k+1;
[e39aa0f]48                                std::list<Label> ls; ls.push_back( get_breakLabel() );
49                                kids.insert( next, new NullStmt( ls ) );
[a08ba92]50                                set_breakLabel("");
51                        } // if
52                } // for
[27de955]53        }
54
55        CompoundStmt* MLEMutator::mutate( CompoundStmt *cmpndStmt ) {
56                bool labeledBlock = !(cmpndStmt->get_labels().empty());
57                if ( labeledBlock ) {
[eb3261f]58                        Label brkLabel = generator->newLabel("blockBreak");
[e39aa0f]59                        enclosingControlStructures.push_back( Entry( cmpndStmt, brkLabel ) );
[27de955]60                } // if
61
[adcc065]62                // a child statement may set the break label - if they do, attach it to the next statement
[27de955]63                std::list< Statement * > &kids = cmpndStmt->get_kids();
64                fixBlock( kids );
[a08ba92]65
66                if ( labeledBlock ) {
[e39aa0f]67                        assert( ! enclosingControlStructures.empty() );
68                        if ( ! enclosingControlStructures.back().useBreakExit().empty() ) {
69                                set_breakLabel( enclosingControlStructures.back().useBreakExit() );
[adcc065]70                        } // if
[e39aa0f]71                        enclosingControlStructures.pop_back();
[a08ba92]72                } // if
73
74                return cmpndStmt;
75        }
76
[27de955]77        template< typename LoopClass >
78        Statement *MLEMutator::handleLoopStmt( LoopClass *loopStmt ) {
[adcc065]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
[27de955]82                Label brkLabel = generator->newLabel("loopBreak");
83                Label contLabel = generator->newLabel("loopContinue");
[e39aa0f]84                enclosingControlStructures.push_back( Entry( loopStmt, brkLabel, contLabel ) );
[27de955]85                loopStmt->set_body ( loopStmt->get_body()->acceptMutator( *this ) );
86
[e39aa0f]87                Entry &e = enclosingControlStructures.back();
[adcc065]88                // sanity check that the enclosing loops have been popped correctly
[27de955]89                assert ( e == loopStmt );
90
[adcc065]91                // this will take the necessary steps to add definitions of the previous two labels, if they are used.
[27de955]92                loopStmt->set_body( mutateLoop( loopStmt->get_body(), e ) );
[e39aa0f]93                enclosingControlStructures.pop_back();
[a08ba92]94
[27de955]95                return loopStmt;
[d9a0e76]96        }
97
[27de955]98        Statement *MLEMutator::mutate( CaseStmt *caseStmt ) {
99                caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) );
100                fixBlock( caseStmt->get_statements() );
[a08ba92]101
[27de955]102                return caseStmt;
103        }
104
[adcc065]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
[27de955]125        template< typename SwitchClass >
126        Statement *MLEMutator::handleSwitchStmt( SwitchClass *switchStmt ) {
[23b6f4d7]127                // generate a label for breaking out of a labeled switch
[27de955]128                Label brkLabel = generator->newLabel("switchBreak");
[e39aa0f]129                enclosingControlStructures.push_back( Entry(switchStmt, brkLabel) );
[23b6f4d7]130                mutateAll( switchStmt->get_branches(), *this );
[a08ba92]131
[e39aa0f]132                Entry &e = enclosingControlStructures.back();
[27de955]133                assert ( e == switchStmt );
134
135                // only generate break label if labeled break is used
[adcc065]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
[27de955]140                        std::list< Statement * > &branches = switchStmt->get_branches();
141                        if ( branches.empty() ) {
142                                branches.push_back( CaseStmt::makeDefault() );
[adcc065]143                        } // if
[27de955]144
145                        if ( CaseStmt * c = dynamic_cast< CaseStmt * >( branches.back() ) ) {
146                                std::list<Label> temp; temp.push_back( brkLabel );
[e39aa0f]147                                c->get_statements().push_back( new BranchStmt( temp, Label("brkLabel"), BranchStmt::Break ) );
[27de955]148                        } else assert(0); // as of this point, all branches of a switch are still CaseStmts
[adcc065]149                } // if
[27de955]150
[e39aa0f]151                assert ( enclosingControlStructures.back() == switchStmt );
152                enclosingControlStructures.pop_back();
[27de955]153                return switchStmt;
[d9a0e76]154        }
[a08ba92]155
156        Statement *MLEMutator::mutate( BranchStmt *branchStmt ) throw ( SemanticError ) {
[be5aa1b]157                std::string originalTarget = branchStmt->get_originalTarget();
158
[e39aa0f]159                std::list< Entry >::reverse_iterator targetEntry;
160                if ( branchStmt->get_type() == BranchStmt::Goto ) {
[a08ba92]161                        return branchStmt;
[e39aa0f]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()] );
[adcc065]169                        } // if
[e39aa0f]170                        if ( targetEntry == enclosingControlStructures.rend() || ! isLoop( targetEntry->get_controlStructure() ) ) {
171                                throw SemanticError( "'continue' target must be an enclosing loop: " + originalTarget );
[adcc065]172                        } // if
[e39aa0f]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 );
[adcc065]178                } // if
[a08ba92]179
[e39aa0f]180                if ( branchStmt->get_target() != "" && targetTable->find( branchStmt->get_target() ) == targetTable->end() ) {
[be5aa1b]181                        throw SemanticError("The label defined in the exit loop statement does not exist: " + originalTarget );  // shouldn't happen (since that's already checked)
[adcc065]182                } // if
[a08ba92]183
[27de955]184                // branch error checks, get the appropriate label name and create a goto
185                Label exitLabel;
[a08ba92]186                switch ( branchStmt->get_type() ) {
187                  case BranchStmt::Break:
[e39aa0f]188                                assert( targetEntry->useBreakExit() != "");
189                                exitLabel = targetEntry->useBreakExit();
[be5aa1b]190                                break;
[a08ba92]191                  case BranchStmt::Continue:
[e39aa0f]192                                assert( targetEntry->useContExit() != "");
193                                exitLabel = targetEntry->useContExit();
[be5aa1b]194                                break;
[a08ba92]195                  default:
[27de955]196                                assert(0);                                      // shouldn't be here
[a08ba92]197                } // switch
198
[e39aa0f]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 );
[adcc065]208                } // if
[a08ba92]209        }
210
211        Statement *MLEMutator::mutateLoop( Statement *bodyLoop, Entry &e ) {
[d939274]212                // ensure loop body is a block
[a08ba92]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
[27de955]219                // only generate these when needed
[a08ba92]220
[27de955]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() );
[e39aa0f]224                        newBody->get_kids().push_back( new NullStmt( labels ) );
[adcc065]225                } // if
[27de955]226
227                if ( e.isBreakUsed() ) {
[adcc065]228                        // break label goes after the loop -- it'll get set by the outer mutator if we do this
[23b6f4d7]229                        set_breakLabel( e.useBreakExit() );
[adcc065]230                } // if
[a08ba92]231
232                return newBody;
233        }
234
[27de955]235        Statement *MLEMutator::mutate( WhileStmt *whileStmt ) {
236                return handleLoopStmt( whileStmt );
237        }
238
239        Statement *MLEMutator::mutate( ForStmt *forStmt ) {
240                return handleLoopStmt( forStmt );
241        }
242
[adcc065]243        Statement *MLEMutator::mutate( IfStmt *ifStmt ) {
244                return handleIfStmt( ifStmt );
245        }
246
[27de955]247        Statement *MLEMutator::mutate( SwitchStmt *switchStmt ) {
248                return handleSwitchStmt( switchStmt );
[a08ba92]249        }
[51b7345]250} // namespace ControlStruct
[a08ba92]251
[51587aa]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.