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  

donode.cc

Go to the documentation of this file.
00001 // $Id: donode.cc,v 1.3 2003/08/07 23:13:03 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 doNode::doNode(stmtNode * body, exprNode * cond, const Coord coord,
00045                const Coord while_coord)
00046   : loopNode(Do, cond, body, coord),
00047     _while_coord(while_coord)
00048 {}
00049 
00050 // ------------------------------------------------------------
00051 //  Walker
00052 // ------------------------------------------------------------
00053 
00054 void doNode::visit(Visitor * the_visitor) 
00055 {
00056   the_visitor->at_do(this);
00057 }
00058 
00059 void doNode::walk(Walker & the_walker)
00060 {
00061   Walker::Order ord = the_walker.order(); 
00062 
00063   if (ord == Walker::Preorder || ord == Walker::Both)
00064     the_walker.at_do(this, Walker::Preorder);
00065 
00066   if (the_walker.depth() == Walker::Subtree) {
00067     // -- Visit the children 
00068 
00069     if (body())
00070       body()->walk(the_walker);
00071 
00072     if (cond())
00073       cond()->walk(the_walker);
00074   }
00075 
00076   if (ord == Walker::Postorder || ord == Walker::Both)
00077     the_walker.at_do(this, Walker::Postorder);
00078 }
00079 
00080 // ------------------------------------------------------------
00081 //  Dataflow
00082 // ------------------------------------------------------------
00083 
00084 void doNode::dataflow(FlowVal * v, FlowProblem & fp)
00085 {
00086   if (fp.forward()) {
00087     fp.flow_do(v, this, FlowProblem::Entry);
00088 
00089     // -- Get the values from the previous loop iteration
00090 
00091     v->meet(at_loop_head());
00092 
00093     // -- Dataflow the loop body (collect break values in at_exit and
00094     // continue values in at_loop_tail)
00095 
00096     if (body())
00097       body()->dataflow(v, fp);
00098 
00099     // -- Get the collected continue values
00100 
00101     v->meet(at_loop_tail());
00102 
00103     if (cond())
00104       if (fp.basicblocks()) {
00105         fp.flow_basicblock(v, cond(), FlowProblem::Entry);
00106         fp.flow_basicblock(v, cond(), FlowProblem::Exit);
00107       }
00108       else
00109         cond()->dataflow(v, fp);
00110     
00111     // -- Transfer values at the end of the loop to the loop head
00112 
00113     at_loop_head()->meet_and_diff(v, fp);
00114 
00115     // -- If there is no condition, then flow cannot go directly from
00116     // the cond to the exit.
00117 
00118     if (! cond())
00119       v->to_top();
00120 
00121     // -- Get the collected break values
00122 
00123     v->meet(at_exit());
00124 
00125     fp.flow_do(v, this, FlowProblem::Exit);
00126   }
00127   else {
00128     fp.flow_do(v, this, FlowProblem::Exit);
00129 
00130     // -- at_exit holds the values need by breaks
00131 
00132     at_exit()->meet_and_diff(v, fp);
00133 
00134     // -- Transfer values at loop head to the loop end
00135 
00136     at_loop_tail()->meet_and_diff(at_loop_head(), fp);
00137 
00138     // -- If there is no condition, then flow cannot go directly from
00139     // the cond to the exit.
00140 
00141     if (! cond())
00142       v->to_top();
00143 
00144     // -- Merge in the loop values
00145 
00146     v->meet(at_loop_tail());
00147 
00148     if (cond())
00149       if (fp.basicblocks()) {
00150         fp.flow_basicblock(v, cond(), FlowProblem::Exit);
00151         fp.flow_basicblock(v, cond(), FlowProblem::Entry);
00152       }
00153       else
00154         cond()->dataflow(v, fp);
00155 
00156     // -- Dataflow the body (breaks and continues read values from
00157     // at_exit and at_loop_end, respectively).
00158 
00159     if (body())
00160       body()->dataflow(v, fp);
00161 
00162     // -- Save the values for the next loop iteration
00163 
00164     at_loop_head()->meet_and_diff(v, fp);
00165 
00166     fp.flow_do(v, this, FlowProblem::Entry);
00167   }
00168 }
00169 
00170 // ------------------------------------------------------------
00171 // Output
00172 // ------------------------------------------------------------
00173 
00174 void doNode::output_stmt(output_context & ct, Node * parent)
00175 {
00176   ct << "do";
00177   ct.space();
00178 
00179   if (body())
00180     body()->output(ct, this);
00181   else
00182     ct << ';';
00183 
00184   ct.space();
00185   ct << "while " << '(' ;
00186 
00187   if (cond())
00188     cond()->output(ct, this);
00189 
00190   ct << ')' << ';' ;
00191 }
00192 
00193 // ------------------------------------------------------------
00194 //  Changer
00195 // ------------------------------------------------------------
00196 
00197 Node * doNode::change(Changer & the_changer, bool redispatch)
00198 {
00199   Changer::Order ord = the_changer.order(); 
00200   doNode * the_do = this;
00201 
00202   if ((ord == Changer::Preorder || ord == Changer::Both) && ! redispatch)
00203     the_do = (doNode *) the_changer.at_do(the_do, Changer::Preorder);
00204 
00205   if (the_do) {
00206 
00207     if (the_do != this)
00208       return the_do->change(the_changer, true);
00209 
00210     blockNode * old_body = the_do->body();
00211     if (old_body) {
00212       blockNode * new_body = (blockNode *) old_body->change(the_changer);
00213       if (old_body != new_body) {
00214         //if (the_changer.delete_old())
00215           //delete old_body;
00216         the_do->body(new_body);
00217       }
00218     }
00219 
00220     exprNode * old_cond = the_do->cond();
00221     if (old_cond) {
00222       exprNode * new_cond = (exprNode *) old_cond->change(the_changer);
00223       if (old_cond != new_cond) {
00224         //if (the_changer.delete_old())
00225           //delete old_cond;
00226         the_do->cond(new_cond);
00227       }
00228     }
00229 
00230   }
00231 
00232   if ((ord == Changer::Postorder || ord == Changer::Both) && ! redispatch)
00233     the_do = (doNode *) the_changer.at_do(the_do, Changer::Postorder);
00234 
00235   return the_do;
00236 }
00237 
00238 
00239 // ------------------------------------------------------------
00240 // Destructor
00241 // ------------------------------------------------------------
00242 
00243 doNode::~doNode()
00244 {
00245 }

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