C-Breeze
C Compiler Infrastructure

[ Project home page]
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

returnnode.cc

Go to the documentation of this file.
00001 // $Id: returnnode.cc,v 1.4 2003/08/07 23:13:11 pnav Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 // 
00009 //  Samuel Z. Guyer
00010 //  Daniel A. Jimenez
00011 //  Calvin Lin
00012 // 
00013 //  Permission is hereby granted, free of charge, to any person
00014 //  obtaining a copy of this software and associated documentation
00015 //  files (the "Software"), to deal in the Software without
00016 //  restriction, including without limitation the rights to use, copy,
00017 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00018 //  of the Software, and to permit persons to whom the Software is
00019 //  furnished to do so, subject to the following conditions:
00020 //  
00021 //  The above copyright notice and this permission notice shall be
00022 //  included in all copies or substantial portions of the Software.
00023 //  
00024 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00028 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00029 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00030 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 //  THE SOFTWARE.
00032 //
00033 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00034 //  Computer Science for inspiring parts of the C-Breeze design.
00035 //
00036 // ----------------------------------------------------------------------
00037 
00038 #include "c_breeze.h"
00039 
00040 // --------------------------------------------------------------------
00041 // Constructors
00042 // --------------------------------------------------------------------
00043 
00044 returnNode::returnNode(exprNode * expr, procNode * proc, const Coord coord)
00045   : jumpNode(Return, coord),
00046     _expr(expr),
00047     _proc(proc),
00048     _is_proc_exit(false)
00049 {}
00050 
00051 returnNode::returnNode(exprNode * expr, procNode * proc, bool proc_exit,
00052                        const Coord coord):
00053   jumpNode(Return, coord),
00054   _expr(expr),
00055   _proc(proc),
00056   _is_proc_exit(proc_exit)
00057 {}
00058 
00059 // ------------------------------------------------------------
00060 //  Walker
00061 // ------------------------------------------------------------
00062 
00063 void returnNode::visit(Visitor * the_visitor) 
00064 {
00065   the_visitor->at_return(this);
00066 }
00067 
00068 void returnNode::walk(Walker & the_walker)
00069 {
00070   Walker::Order ord = the_walker.order(); 
00071 
00072   if (ord == Walker::Preorder || ord == Walker::Both)
00073     the_walker.at_return(this, Walker::Preorder);
00074 
00075   if (the_walker.depth() == Walker::Subtree) {
00076     // -- Visit the children 
00077 
00078     if (expr())
00079       expr()->walk(the_walker);
00080   }
00081 
00082   if (ord == Walker::Postorder || ord == Walker::Both)
00083     the_walker.at_return(this, Walker::Postorder);
00084 }
00085 
00086 // ------------------------------------------------------------
00087 //  Dataflow
00088 // ------------------------------------------------------------
00089 
00090 void returnNode::dataflow(FlowVal * v, FlowProblem & fp)
00091 {
00092   if (fp.forward()) {
00093     fp.flow_return(v, this, FlowProblem::Entry);
00094 
00095     // -- Dataflow the expr and push values to end of the procedure
00096 
00097     if (expr())
00098       if (fp.basicblocks()) {
00099         fp.flow_basicblock(v, expr(), FlowProblem::Entry);
00100         fp.flow_basicblock(v, expr(), FlowProblem::Exit);
00101       }
00102       else
00103         expr()->dataflow(v, fp);
00104     
00105     if (proc())
00106       proc()->at_exit()->meet_and_diff(v, fp);
00107 
00108     v->to_top();
00109 
00110     fp.flow_return(v, this, FlowProblem::Exit);
00111   }
00112   else {
00113     fp.flow_return(v, this, FlowProblem::Exit);
00114 
00115     // -- Get values from the end of the procedure
00116 
00117     v->to_top();
00118 
00119     if (proc())
00120       v->meet(proc()->at_exit());
00121 
00122     if (expr())
00123       if (fp.basicblocks()) {
00124         fp.flow_basicblock(v, expr(), FlowProblem::Exit);
00125         fp.flow_basicblock(v, expr(), FlowProblem::Entry);
00126       }
00127       else
00128         expr()->dataflow(v, fp);
00129 
00130     fp.flow_return(v, this, FlowProblem::Entry);
00131   }
00132 }
00133 
00134 // clone() must be different here to ensure that we only ever have
00135 // one returnNode that claims to be the procedure exit per procNode
00136 Node * returnNode::clone() const { 
00137   returnNode * ret = new returnNode(*this);
00138   ret->proc_exit(false);
00139   return ret;
00140 }
00141 
00142 // ------------------------------------------------------------
00143 // Output
00144 // ------------------------------------------------------------
00145 
00146 void returnNode::output_stmt(output_context & ct, Node * parent)
00147 {
00148   ct << "return ";
00149   if (expr())
00150     expr()->output(ct, this);
00151   ct << ';';
00152 }
00153 
00154 // ------------------------------------------------------------
00155 //  Changer
00156 // ------------------------------------------------------------
00157 
00158 Node * returnNode::change(Changer & the_changer, bool redispatch)
00159 {
00160   Changer::Order ord = the_changer.order(); 
00161   returnNode * the_return = this;
00162 
00163   if ((ord == Changer::Preorder || ord == Changer::Both) && ! redispatch)
00164     the_return = (returnNode *) the_changer.at_return(the_return, Changer::Preorder);
00165 
00166   if (the_return) {
00167 
00168     if (the_return != this)
00169       return the_return->change(the_changer, true);
00170 
00171     exprNode * old_expr = the_return->expr();
00172     if (old_expr) {
00173       exprNode * new_expr = (exprNode *) old_expr->change(the_changer);
00174       if (old_expr != new_expr) {
00175         //if (the_changer.delete_old())
00176           //delete old_expr;
00177         the_return->expr(new_expr);
00178       }
00179     }
00180 
00181   }
00182 
00183   if ((ord == Changer::Postorder || ord == Changer::Both) && ! redispatch)
00184     the_return = (returnNode *) the_changer.at_return(the_return, Changer::Postorder);
00185 
00186   return the_return;
00187 }
00188 
00189 
00190 // ------------------------------------------------------------
00191 // Destructor
00192 // ------------------------------------------------------------
00193 
00194 returnNode::~returnNode()
00195 {
00196   //delete _expr;
00197 }

Generated on August 27, 2003
Back to the C-Breeze home page