| 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 : Wed May 27 16:16:14 2015 | 
|---|
| 13 | // Update Count     : 4 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 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 { | 
|---|
| 26 | LabelFixer::Entry::Entry( Statement *to, Statement *from ) : definition ( to ) { | 
|---|
| 27 | if ( from != 0 ) | 
|---|
| 28 | usage.push_back( from ); | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | bool LabelFixer::Entry::insideLoop() { | 
|---|
| 32 | return ( dynamic_cast< ForStmt * > ( definition ) || | 
|---|
| 33 | dynamic_cast< WhileStmt * > ( definition )  ); | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | LabelFixer::LabelFixer( LabelGenerator *gen ) : generator ( gen ) { | 
|---|
| 37 | if ( generator == 0 ) | 
|---|
| 38 | generator = LabelGenerator::getGenerator(); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | void LabelFixer::visit( FunctionDecl *functionDecl ) { | 
|---|
| 42 | maybeAccept( functionDecl->get_statements(), *this ); | 
|---|
| 43 |  | 
|---|
| 44 | MLEMutator mlemut( resolveJumps(), generator ); | 
|---|
| 45 | functionDecl->acceptMutator( mlemut ); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | void LabelFixer::visit( Statement *stmt ) { | 
|---|
| 49 | std::list< Label > &labels = stmt->get_labels(); | 
|---|
| 50 |  | 
|---|
| 51 | if ( ! labels.empty() ) { | 
|---|
| 52 | Label current = setLabelsDef( labels, stmt ); | 
|---|
| 53 | labels.clear(); | 
|---|
| 54 | labels.push_front( current ); | 
|---|
| 55 | } // if | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | void LabelFixer::visit( BranchStmt *branchStmt ) { | 
|---|
| 59 | visit ( ( Statement * )branchStmt );  // the labels this statement might have | 
|---|
| 60 |  | 
|---|
| 61 | Label target; | 
|---|
| 62 | if ( (target = branchStmt->get_target()) != "" ) { | 
|---|
| 63 | setLabelsUsg( target, branchStmt ); | 
|---|
| 64 | } //else       /* computed goto or normal exit-loop statements */ | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | Label LabelFixer::setLabelsDef( std::list< Label > &llabel, Statement *definition ) { | 
|---|
| 68 | assert( definition != 0 ); | 
|---|
| 69 | Entry *entry = new Entry( definition ); | 
|---|
| 70 | bool used = false; | 
|---|
| 71 |  | 
|---|
| 72 | for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) | 
|---|
| 73 | if ( labelTable.find( *i ) == labelTable.end() ) | 
|---|
| 74 | { used = true; labelTable[ *i ] = entry; } // undefined and unused | 
|---|
| 75 | else | 
|---|
| 76 | if ( labelTable[ *i ]->defined() ) | 
|---|
| 77 | throw SemanticError( "Duplicate definition of label: " + *i ); | 
|---|
| 78 | else | 
|---|
| 79 | labelTable[ *i ]->set_definition( definition ); | 
|---|
| 80 |  | 
|---|
| 81 | if ( ! used ) delete entry; | 
|---|
| 82 |  | 
|---|
| 83 | return labelTable[ llabel.front() ]->get_label();  // this *must* exist | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | Label LabelFixer::setLabelsUsg( Label orgValue, Statement *use ) { | 
|---|
| 87 | assert( use != 0 ); | 
|---|
| 88 |  | 
|---|
| 89 | if ( labelTable.find( orgValue ) != labelTable.end() ) | 
|---|
| 90 | labelTable[ orgValue ]->add_use( use );         // the label has been defined or used before | 
|---|
| 91 | else | 
|---|
| 92 | labelTable[ orgValue ] = new Entry( 0, use ); | 
|---|
| 93 |  | 
|---|
| 94 | return labelTable[ orgValue ]->get_label(); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | std::map<Label, Statement * > *LabelFixer::resolveJumps() throw ( SemanticError ) { | 
|---|
| 98 | std::map< Statement *, Entry * > def_us; | 
|---|
| 99 |  | 
|---|
| 100 | for ( std::map< Label, Entry *>::iterator i = labelTable.begin(); i != labelTable.end(); i++ ) { | 
|---|
| 101 | Entry *e = i->second; | 
|---|
| 102 |  | 
|---|
| 103 | if ( def_us.find ( e->get_definition() ) == def_us.end() ) { | 
|---|
| 104 | def_us[ e->get_definition() ] = e; | 
|---|
| 105 | } else { | 
|---|
| 106 | if ( e->used() ) | 
|---|
| 107 | def_us[ e->get_definition() ]->add_uses( e->get_uses() ); | 
|---|
| 108 | } | 
|---|
| 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 | } | 
|---|
| 143 | }  // namespace ControlStruct | 
|---|
| 144 |  | 
|---|
| 145 | // Local Variables: // | 
|---|
| 146 | // tab-width: 4 // | 
|---|
| 147 | // mode: c++ // | 
|---|
| 148 | // compile-command: "make install" // | 
|---|
| 149 | // End: // | 
|---|