[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 |
---|
[a08ba92] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Tue May 19 15:25:59 2015 |
---|
| 13 | // Update Count : 1 |
---|
[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 | |
---|
| 25 | namespace ControlStruct { |
---|
[a08ba92] | 26 | LabelFixer::Entry::Entry( Statement *to, Statement *from ) : definition ( to ) { |
---|
| 27 | if ( from != 0 ) |
---|
| 28 | usage.push_back( from ); |
---|
| 29 | } |
---|
[d9a0e76] | 30 | |
---|
[a08ba92] | 31 | bool LabelFixer::Entry::insideLoop() { |
---|
| 32 | return ( dynamic_cast< ForStmt * > ( definition ) || |
---|
| 33 | dynamic_cast< WhileStmt * > ( definition ) ); |
---|
| 34 | } |
---|
[d9a0e76] | 35 | |
---|
[a08ba92] | 36 | LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) { |
---|
| 37 | if ( generator == 0 ) |
---|
| 38 | generator = LabelGenerator::getGenerator(); |
---|
| 39 | } |
---|
[51b7345] | 40 | |
---|
[a08ba92] | 41 | void LabelFixer::visit( FunctionDecl *functionDecl ) { |
---|
| 42 | if ( functionDecl->get_statements() != 0 ) |
---|
| 43 | functionDecl->get_statements()->accept( *this ); |
---|
[d9a0e76] | 44 | |
---|
[a08ba92] | 45 | MLEMutator mlemut( resolveJumps(), generator ); |
---|
| 46 | functionDecl->acceptMutator( mlemut ); |
---|
| 47 | } |
---|
[51b7345] | 48 | |
---|
[a08ba92] | 49 | void LabelFixer::visit( Statement *stmt ) { |
---|
| 50 | std::list< Label > &labels = stmt->get_labels(); |
---|
[51b7345] | 51 | |
---|
[a08ba92] | 52 | if ( ! labels.empty() ) { |
---|
| 53 | Label current = setLabelsDef( labels, stmt ); |
---|
| 54 | labels.clear(); |
---|
| 55 | labels.push_front( current ); |
---|
| 56 | } // if |
---|
| 57 | } |
---|
[d9a0e76] | 58 | |
---|
[a08ba92] | 59 | void LabelFixer::visit( BranchStmt *branchStmt ) { |
---|
| 60 | visit ( ( Statement * )branchStmt ); // the labels this statement might have |
---|
[d9a0e76] | 61 | |
---|
[a08ba92] | 62 | Label target; |
---|
| 63 | if ( (target = branchStmt->get_target()) != "" ) { |
---|
| 64 | setLabelsUsg( target, branchStmt ); |
---|
| 65 | } //else /* computed goto or normal exit-loop statements */ |
---|
[d9a0e76] | 66 | } |
---|
| 67 | |
---|
[a08ba92] | 68 | Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) { |
---|
| 69 | assert( definition != 0 ); |
---|
| 70 | Entry *entry = new Entry( definition ); |
---|
| 71 | bool used = false; |
---|
| 72 | |
---|
| 73 | for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) |
---|
| 74 | if ( labelTable.find( *i ) == labelTable.end() ) |
---|
| 75 | { used = true; labelTable[ *i ] = entry; } // undefined and unused |
---|
| 76 | else |
---|
| 77 | if ( labelTable[ *i ]->defined() ) |
---|
| 78 | throw SemanticError( "Duplicate definition of label: " + *i ); |
---|
| 79 | else |
---|
| 80 | labelTable[ *i ]->set_definition( definition ); |
---|
| 81 | |
---|
| 82 | if ( ! used ) delete entry; |
---|
| 83 | |
---|
| 84 | return labelTable[ llabel.front() ]->get_label(); // this *must* exist |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | Label LabelFixer::setLabelsUsg( Label orgValue, Statement *use ) { |
---|
| 88 | assert( use != 0 ); |
---|
| 89 | |
---|
| 90 | if ( labelTable.find( orgValue ) != labelTable.end() ) |
---|
| 91 | labelTable[ orgValue ]->add_use( use ); // the label has been defined or used before |
---|
| 92 | else |
---|
| 93 | labelTable[ orgValue ] = new Entry( 0, use ); |
---|
| 94 | |
---|
| 95 | return labelTable[ orgValue ]->get_label(); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) { |
---|
| 99 | std::map< Statement *, Entry * > def_us; |
---|
| 100 | |
---|
| 101 | for ( std::map< Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); i++ ) { |
---|
| 102 | Entry *e = i->second; |
---|
| 103 | |
---|
| 104 | if ( def_us.find ( e->get_definition() ) == def_us.end() ) |
---|
| 105 | def_us[ e->get_definition() ] = e; |
---|
| 106 | else |
---|
| 107 | if ( e->used() ) |
---|
| 108 | def_us[ e->get_definition() ]->add_uses( e->get_uses() ); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | // get rid of labelTable |
---|
| 112 | for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) { |
---|
| 113 | Statement *to = (*i).first; |
---|
| 114 | std::list< Statement *> &from = (*i).second->get_uses(); |
---|
| 115 | Label finalLabel = generator->newLabel(); |
---|
| 116 | (*i).second->set_label( finalLabel ); |
---|
| 117 | |
---|
| 118 | if ( to == 0 ) { |
---|
| 119 | BranchStmt *first_use = dynamic_cast<BranchStmt *>(from.back()); |
---|
| 120 | Label undef(""); |
---|
| 121 | if ( first_use != 0 ) |
---|
| 122 | undef = first_use->get_target(); |
---|
| 123 | throw SemanticError ( "'" + undef + "' label not defined"); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | to->get_labels().clear(); |
---|
| 127 | to->get_labels().push_back( finalLabel ); |
---|
| 128 | |
---|
| 129 | for ( std::list< Statement *>::iterator j = from.begin(); j != from.end(); j++ ) { |
---|
| 130 | BranchStmt *jumpTo = dynamic_cast< BranchStmt * > ( *j ); |
---|
| 131 | assert( jumpTo != 0 ); |
---|
| 132 | jumpTo->set_target( finalLabel ); |
---|
| 133 | } // for |
---|
| 134 | } // for |
---|
| 135 | |
---|
| 136 | // reverse table |
---|
| 137 | std::map< Label, Statement * > *ret = new std::map< Label, Statement * >(); |
---|
| 138 | for ( std::map< Statement *, Entry * >::iterator i = def_us.begin(); i != def_us.end(); i++ ) |
---|
| 139 | (*ret)[ (*i).second->get_label() ] = (*i).first; |
---|
| 140 | |
---|
| 141 | return ret; |
---|
| 142 | } |
---|
[51b7345] | 143 | } // namespace ControlStruct |
---|
[a08ba92] | 144 | |
---|
[51587aa] | 145 | // Local Variables: // |
---|
| 146 | // tab-width: 4 // |
---|
| 147 | // mode: c++ // |
---|
| 148 | // compile-command: "make install" // |
---|
| 149 | // End: // |
---|