[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 |
---|
[be5aa1b] | 11 | // Last Modified By : Rob Schluntz |
---|
[46cbfe1] | 12 | // Last Modified On : Fri May 29 15:57:12 2015 |
---|
| 13 | // Update Count : 82 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[51b7345] | 16 | #include <list> |
---|
| 17 | #include <cassert> |
---|
| 18 | |
---|
| 19 | #include "LabelFixer.h" |
---|
| 20 | #include "MLEMutator.h" |
---|
| 21 | #include "SynTree/Statement.h" |
---|
| 22 | #include "SynTree/Declaration.h" |
---|
| 23 | #include "utility.h" |
---|
| 24 | |
---|
[46cbfe1] | 25 | #include <iostream> |
---|
| 26 | |
---|
[51b7345] | 27 | namespace ControlStruct { |
---|
[46cbfe1] | 28 | LabelFixer::Entry::Entry( Statement *to, BranchStmt *from ) : definition ( to ) { |
---|
[a08ba92] | 29 | if ( from != 0 ) |
---|
| 30 | usage.push_back( from ); |
---|
| 31 | } |
---|
[d9a0e76] | 32 | |
---|
[a08ba92] | 33 | bool LabelFixer::Entry::insideLoop() { |
---|
| 34 | return ( dynamic_cast< ForStmt * > ( definition ) || |
---|
[46cbfe1] | 35 | dynamic_cast< WhileStmt * > ( definition ) ); |
---|
[a08ba92] | 36 | } |
---|
[d9a0e76] | 37 | |
---|
[a08ba92] | 38 | LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) { |
---|
| 39 | if ( generator == 0 ) |
---|
| 40 | generator = LabelGenerator::getGenerator(); |
---|
| 41 | } |
---|
[51b7345] | 42 | |
---|
[a08ba92] | 43 | void LabelFixer::visit( FunctionDecl *functionDecl ) { |
---|
[be5aa1b] | 44 | maybeAccept( functionDecl->get_statements(), *this ); |
---|
[d9a0e76] | 45 | |
---|
[a08ba92] | 46 | MLEMutator mlemut( resolveJumps(), generator ); |
---|
| 47 | functionDecl->acceptMutator( mlemut ); |
---|
| 48 | } |
---|
[51b7345] | 49 | |
---|
[46cbfe1] | 50 | // prune to at most one label definition for each statement |
---|
[a08ba92] | 51 | void LabelFixer::visit( Statement *stmt ) { |
---|
| 52 | std::list< Label > &labels = stmt->get_labels(); |
---|
[51b7345] | 53 | |
---|
[a08ba92] | 54 | if ( ! labels.empty() ) { |
---|
[46cbfe1] | 55 | // only remember one label for each statement |
---|
[a08ba92] | 56 | Label current = setLabelsDef( labels, stmt ); |
---|
| 57 | labels.clear(); |
---|
| 58 | labels.push_front( current ); |
---|
| 59 | } // if |
---|
| 60 | } |
---|
[d9a0e76] | 61 | |
---|
[a08ba92] | 62 | void LabelFixer::visit( BranchStmt *branchStmt ) { |
---|
[46cbfe1] | 63 | visit ( ( Statement * )branchStmt ); |
---|
[d9a0e76] | 64 | |
---|
[46cbfe1] | 65 | // for labeled branches, add an entry to the label table |
---|
| 66 | Label target = branchStmt->get_target(); |
---|
| 67 | if ( target != "" ) { |
---|
[a08ba92] | 68 | setLabelsUsg( target, branchStmt ); |
---|
[46cbfe1] | 69 | } |
---|
[d9a0e76] | 70 | } |
---|
| 71 | |
---|
[46cbfe1] | 72 | // sets the definition of the labelTable entry to be the provided |
---|
| 73 | // statement for every label in the list parameter. Happens for every kind of statement |
---|
[a08ba92] | 74 | Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) { |
---|
| 75 | assert( definition != 0 ); |
---|
[46cbfe1] | 76 | assert( llabel.size() > 0 ); |
---|
| 77 | |
---|
| 78 | for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) { |
---|
| 79 | if ( labelTable.find( *i ) == labelTable.end() ) { |
---|
| 80 | // undefined and unused until now, add an entry |
---|
| 81 | labelTable[ *i ] = new Entry( definition ); |
---|
| 82 | } else if ( labelTable[ *i ]->defined() ) {\ |
---|
| 83 | // defined twice, error |
---|
| 84 | throw SemanticError( "Duplicate definition of label: " + *i ); |
---|
| 85 | } else { |
---|
| 86 | // used previously, but undefined until now -> set its definition to this location |
---|
| 87 | labelTable[ *i ]->set_definition( definition ); |
---|
| 88 | } // if |
---|
| 89 | } // for |
---|
[a08ba92] | 90 | |
---|
[46cbfe1] | 91 | // produce one of the labels attached to this statement to be |
---|
| 92 | // temporarily used as the canonical label |
---|
| 93 | return labelTable[ llabel.front() ]->get_label(); |
---|
[a08ba92] | 94 | } |
---|
| 95 | |
---|
[46cbfe1] | 96 | // Remember all uses of a label. |
---|
| 97 | void LabelFixer::setLabelsUsg( Label orgValue, BranchStmt *use ) { |
---|
[a08ba92] | 98 | assert( use != 0 ); |
---|
| 99 | |
---|
[46cbfe1] | 100 | if ( labelTable.find( orgValue ) != labelTable.end() ) { |
---|
| 101 | // the label has been defined or used before |
---|
| 102 | labelTable[ orgValue ]->add_use( use ); |
---|
| 103 | } else { |
---|
[a08ba92] | 104 | labelTable[ orgValue ] = new Entry( 0, use ); |
---|
[46cbfe1] | 105 | } |
---|
[a08ba92] | 106 | } |
---|
| 107 | |
---|
[46cbfe1] | 108 | // Ultimately builds a table that maps a label to its defining statement. |
---|
| 109 | // In the process, |
---|
[a08ba92] | 110 | std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) { |
---|
| 111 | std::map< Statement *, Entry * > def_us; |
---|
| 112 | |
---|
[46cbfe1] | 113 | // combine the entries for all labels that target the same location |
---|
| 114 | for ( std::map< Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) { |
---|
[a08ba92] | 115 | Entry *e = i->second; |
---|
| 116 | |
---|
[be5aa1b] | 117 | if ( def_us.find ( e->get_definition() ) == def_us.end() ) { |
---|
| 118 | def_us[ e->get_definition() ] = e; |
---|
[46cbfe1] | 119 | } else if ( e->used() ) { |
---|
| 120 | def_us[ e->get_definition() ]->add_uses( e->get_uses() ); |
---|
[be5aa1b] | 121 | } |
---|
[a08ba92] | 122 | } |
---|
| 123 | |
---|
[46cbfe1] | 124 | // create a unique label for each target location. |
---|
| 125 | for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); ++i ) { |
---|
[a08ba92] | 126 | Statement *to = (*i).first; |
---|
[46cbfe1] | 127 | Entry * entry = (*i).second; |
---|
| 128 | std::list< BranchStmt *> &from = entry->get_uses(); |
---|
[a08ba92] | 129 | |
---|
[46cbfe1] | 130 | // no label definition found |
---|
[a08ba92] | 131 | if ( to == 0 ) { |
---|
[46cbfe1] | 132 | Label undef = from.back()->get_target(); |
---|
[a08ba92] | 133 | throw SemanticError ( "'" + undef + "' label not defined"); |
---|
| 134 | } |
---|
| 135 | |
---|
[46cbfe1] | 136 | // generate a new label, and attach it to its defining statement |
---|
| 137 | // as the only label on that statement |
---|
| 138 | Label finalLabel = generator->newLabel(); |
---|
| 139 | entry->set_label( finalLabel ); |
---|
| 140 | |
---|
[a08ba92] | 141 | to->get_labels().clear(); |
---|
| 142 | to->get_labels().push_back( finalLabel ); |
---|
| 143 | |
---|
[46cbfe1] | 144 | // redirect each of the source branch statements to the new target label |
---|
| 145 | for ( std::list< BranchStmt *>::iterator j = from.begin(); j != from.end(); ++j ) { |
---|
| 146 | BranchStmt *jump = *j; |
---|
| 147 | assert( jump != 0 ); |
---|
| 148 | jump->set_target( finalLabel ); |
---|
[a08ba92] | 149 | } // for |
---|
| 150 | } // for |
---|
| 151 | |
---|
[46cbfe1] | 152 | // create a table where each label maps to its defining statement |
---|
[a08ba92] | 153 | std::map< Label, Statement * > *ret = new std::map< Label, Statement * >(); |
---|
[46cbfe1] | 154 | for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); ++i ) { |
---|
[a08ba92] | 155 | (*ret)[ (*i).second->get_label() ] = (*i).first; |
---|
[46cbfe1] | 156 | } |
---|
[a08ba92] | 157 | |
---|
| 158 | return ret; |
---|
| 159 | } |
---|
[51b7345] | 160 | } // namespace ControlStruct |
---|
[a08ba92] | 161 | |
---|
[51587aa] | 162 | // Local Variables: // |
---|
| 163 | // tab-width: 4 // |
---|
| 164 | // mode: c++ // |
---|
| 165 | // compile-command: "make install" // |
---|
| 166 | // End: // |
---|