source: src/ControlStruct/LabelFixer.cc @ b70b6fc

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

building runtime library (first attempt)

  • Property mode set to 100644
File size: 4.4 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//
[a08ba92]7// LabelFixer.cc --
[51587aa]8//
[843054c2]9// Author           : Rodolfo G. Esteves
[51587aa]10// Created On       : Mon May 18 07:44:20 2015
[1869adf]11// Last Modified By : Rob Schluntz
[e766208]12// Last Modified On : Tue Jul 28 13:32:43 2015
13// Update Count     : 156
[51587aa]14//
[a08ba92]15
[51b7345]16#include <list>
17#include <cassert>
18
19#include "LabelFixer.h"
20#include "MLEMutator.h"
[1869adf]21#include "SynTree/Expression.h"
[51b7345]22#include "SynTree/Statement.h"
23#include "SynTree/Declaration.h"
[d3b7937]24#include "Common/utility.h"
[51b7345]25
[46cbfe1]26#include <iostream>
27
[51b7345]28namespace ControlStruct {
[a08ba92]29        bool LabelFixer::Entry::insideLoop() {
30                return ( dynamic_cast< ForStmt * > ( definition ) ||
[46cbfe1]31                        dynamic_cast< WhileStmt * > ( definition )  );
[a08ba92]32        }
[d9a0e76]33
[a08ba92]34        LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) {
35                if ( generator == 0 )
36                        generator = LabelGenerator::getGenerator();
37        }
[51b7345]38
[a08ba92]39        void LabelFixer::visit( FunctionDecl *functionDecl ) {
[0a0a65b]40                // need to go into a nested function in a fresh state
41                std::map < Label, Entry *> oldLabelTable = labelTable;
42                labelTable.clear();
43
[be5aa1b]44                maybeAccept( functionDecl->get_statements(), *this );
[d9a0e76]45
[a08ba92]46                MLEMutator mlemut( resolveJumps(), generator );
47                functionDecl->acceptMutator( mlemut );
[0a0a65b]48
49                // and remember the outer function's labels when
50                // returning to it
51                labelTable = oldLabelTable;
[a08ba92]52        }
[51b7345]53
[46cbfe1]54        // prune to at most one label definition for each statement
[a08ba92]55        void LabelFixer::visit( Statement *stmt ) {
56                std::list< Label > &labels = stmt->get_labels();
[51b7345]57
[a08ba92]58                if ( ! labels.empty() ) {
[46cbfe1]59                        // only remember one label for each statement
[a08ba92]60                        Label current = setLabelsDef( labels, stmt );
61                } // if
62        }
[d9a0e76]63
[a08ba92]64        void LabelFixer::visit( BranchStmt *branchStmt ) {
[46cbfe1]65                visit ( ( Statement * )branchStmt );
[d9a0e76]66
[46cbfe1]67                // for labeled branches, add an entry to the label table
68                Label target = branchStmt->get_target();
69                if ( target != "" ) {
[a08ba92]70                        setLabelsUsg( target, branchStmt );
[46cbfe1]71                }
[d9a0e76]72        }
73
[1869adf]74        void LabelFixer::visit( UntypedExpr *untyped ) {
75                if ( NameExpr * func = dynamic_cast< NameExpr * >( untyped->get_function() ) ) {
76                        if ( func->get_name() == "&&" ) {
77                                NameExpr * arg = dynamic_cast< NameExpr * >( untyped->get_args().front() );
78                                Label target = arg->get_name();
79                                assert( target != "" );
80                                setLabelsUsg( target, untyped );
81                        } else {
82                                Visitor::visit( untyped );
83                        }
84                }
85        }
86
87
[46cbfe1]88        // sets the definition of the labelTable entry to be the provided
89        // statement for every label in the list parameter. Happens for every kind of statement
[a08ba92]90        Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) {
91                assert( definition != 0 );
[46cbfe1]92                assert( llabel.size() > 0 );
93
[954463b8]94                Entry * e = new Entry( definition );
95
[46cbfe1]96                for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) {
[954463b8]97                        if ( labelTable.find( *i ) == labelTable.end() ) {
98                                // all labels on this statement need to use the same entry, so this should only be created once
[46cbfe1]99                                // undefined and unused until now, add an entry
[954463b8]100                                labelTable[ *i ] =  e;
101                        } else if ( labelTable[ *i ]->defined() ) {
[46cbfe1]102                                // defined twice, error
103                                throw SemanticError( "Duplicate definition of label: " + *i );
104                        }       else {
[954463b8]105                                // used previously, but undefined until now -> link with this entry
[e766208]106                                delete labelTable[ *i ];
[954463b8]107                                labelTable[ *i ] = e;
[46cbfe1]108                        } // if
109                } // for
[a08ba92]110
[46cbfe1]111                // produce one of the labels attached to this statement to be
112                // temporarily used as the canonical label
113                return labelTable[ llabel.front() ]->get_label();
[a08ba92]114        }
115
[e766208]116        // A label was used, add it ot the table if it isn't already there
[1869adf]117        template< typename UsageNode >
118        void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) {
[a08ba92]119                assert( use != 0 );
120
[e766208]121                // add label with an unknown origin
122                if ( labelTable.find( orgValue ) == labelTable.end() ) {
123                        labelTable[ orgValue ] = new Entry( 0 );
[46cbfe1]124                }
[a08ba92]125        }
126
[e766208]127        // Builds a table that maps a label to its defining statement.
[a08ba92]128        std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) {
129                std::map< Label, Statement * > *ret = new std::map< Label, Statement * >();
[e766208]130                for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) {
131                        if ( ! i->second->defined() ) {
132                                throw SemanticError( "Use of undefined label: " + i->first );
133                        }
134                        (*ret)[ i->first ] = i->second->get_definition();
[46cbfe1]135                }
[a08ba92]136
137                return ret;
138        }
[51b7345]139}  // namespace ControlStruct
[a08ba92]140
[51587aa]141// Local Variables: //
142// tab-width: 4 //
143// mode: c++ //
144// compile-command: "make install" //
145// End: //
Note: See TracBrowser for help on using the repository browser.