| 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 | // LabelFixer.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 : Tue Jul 28 13:32:43 2015 | 
|---|
| 13 | // Update Count     : 156 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <list> | 
|---|
| 17 | #include <cassert> | 
|---|
| 18 |  | 
|---|
| 19 | #include "LabelFixer.h" | 
|---|
| 20 | #include "MLEMutator.h" | 
|---|
| 21 | #include "SynTree/Expression.h" | 
|---|
| 22 | #include "SynTree/Statement.h" | 
|---|
| 23 | #include "SynTree/Declaration.h" | 
|---|
| 24 | #include "Common/utility.h" | 
|---|
| 25 |  | 
|---|
| 26 | #include <iostream> | 
|---|
| 27 |  | 
|---|
| 28 | namespace ControlStruct { | 
|---|
| 29 | bool LabelFixer::Entry::insideLoop() { | 
|---|
| 30 | return ( dynamic_cast< ForStmt * > ( definition ) || | 
|---|
| 31 | dynamic_cast< WhileStmt * > ( definition )  ); | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) { | 
|---|
| 35 | if ( generator == 0 ) | 
|---|
| 36 | generator = LabelGenerator::getGenerator(); | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | void LabelFixer::visit( FunctionDecl *functionDecl ) { | 
|---|
| 40 | // need to go into a nested function in a fresh state | 
|---|
| 41 | std::map < Label, Entry *> oldLabelTable = labelTable; | 
|---|
| 42 | labelTable.clear(); | 
|---|
| 43 |  | 
|---|
| 44 | maybeAccept( functionDecl->get_statements(), *this ); | 
|---|
| 45 |  | 
|---|
| 46 | MLEMutator mlemut( resolveJumps(), generator ); | 
|---|
| 47 | functionDecl->acceptMutator( mlemut ); | 
|---|
| 48 |  | 
|---|
| 49 | // and remember the outer function's labels when | 
|---|
| 50 | // returning to it | 
|---|
| 51 | labelTable = oldLabelTable; | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | // prune to at most one label definition for each statement | 
|---|
| 55 | void LabelFixer::visit( Statement *stmt ) { | 
|---|
| 56 | std::list< Label > &labels = stmt->get_labels(); | 
|---|
| 57 |  | 
|---|
| 58 | if ( ! labels.empty() ) { | 
|---|
| 59 | // only remember one label for each statement | 
|---|
| 60 | Label current = setLabelsDef( labels, stmt ); | 
|---|
| 61 | } // if | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | void LabelFixer::visit( BranchStmt *branchStmt ) { | 
|---|
| 65 | visit ( ( Statement * )branchStmt ); | 
|---|
| 66 |  | 
|---|
| 67 | // for labeled branches, add an entry to the label table | 
|---|
| 68 | Label target = branchStmt->get_target(); | 
|---|
| 69 | if ( target != "" ) { | 
|---|
| 70 | setLabelsUsg( target, branchStmt ); | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 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 |  | 
|---|
| 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 | 
|---|
| 90 | Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) { | 
|---|
| 91 | assert( definition != 0 ); | 
|---|
| 92 | assert( llabel.size() > 0 ); | 
|---|
| 93 |  | 
|---|
| 94 | Entry * e = new Entry( definition ); | 
|---|
| 95 |  | 
|---|
| 96 | for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) { | 
|---|
| 97 | Label & l = *i; | 
|---|
| 98 | l.set_statement( definition ); // attach statement to the label to be used later | 
|---|
| 99 | if ( labelTable.find( l ) == labelTable.end() ) { | 
|---|
| 100 | // all labels on this statement need to use the same entry, so this should only be created once | 
|---|
| 101 | // undefined and unused until now, add an entry | 
|---|
| 102 | labelTable[ l ] =  e; | 
|---|
| 103 | } else if ( labelTable[ l ]->defined() ) { | 
|---|
| 104 | // defined twice, error | 
|---|
| 105 | throw SemanticError( "Duplicate definition of label: " + l.get_name() ); | 
|---|
| 106 | }       else { | 
|---|
| 107 | // used previously, but undefined until now -> link with this entry | 
|---|
| 108 | delete labelTable[ l ]; | 
|---|
| 109 | labelTable[ l ] = e; | 
|---|
| 110 | } // if | 
|---|
| 111 | } // for | 
|---|
| 112 |  | 
|---|
| 113 | // produce one of the labels attached to this statement to be | 
|---|
| 114 | // temporarily used as the canonical label | 
|---|
| 115 | return labelTable[ llabel.front() ]->get_label(); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | // A label was used, add it to the table if it isn't already there | 
|---|
| 119 | template< typename UsageNode > | 
|---|
| 120 | void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) { | 
|---|
| 121 | assert( use != 0 ); | 
|---|
| 122 |  | 
|---|
| 123 | // add label with an unknown origin | 
|---|
| 124 | if ( labelTable.find( orgValue ) == labelTable.end() ) { | 
|---|
| 125 | labelTable[ orgValue ] = new Entry( 0 ); | 
|---|
| 126 | } | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | // Builds a table that maps a label to its defining statement. | 
|---|
| 130 | std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) { | 
|---|
| 131 | std::map< Label, Statement * > *ret = new std::map< Label, Statement * >(); | 
|---|
| 132 | for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) { | 
|---|
| 133 | if ( ! i->second->defined() ) { | 
|---|
| 134 | throw SemanticError( "Use of undefined label: " + i->first.get_name() ); | 
|---|
| 135 | } | 
|---|
| 136 | (*ret)[ i->first ] = i->second->get_definition(); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | return ret; | 
|---|
| 140 | } | 
|---|
| 141 | }  // namespace ControlStruct | 
|---|
| 142 |  | 
|---|
| 143 | // Local Variables: // | 
|---|
| 144 | // tab-width: 4 // | 
|---|
| 145 | // mode: c++ // | 
|---|
| 146 | // compile-command: "make install" // | 
|---|
| 147 | // End: // | 
|---|