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  

labelnode.cc

Go to the documentation of this file.
00001 // $Id: labelnode.cc,v 1.3 2003/08/07 23:13:08 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 labelNode::labelNode(const char * name, stmtNode * stmt, const Coord coord)
00045   : targetNode(Label, stmt, coord),
00046     _name(string(name)),
00047     _references()
00048 {}
00049 
00050 // -- Used in BuildLabel and ResolveGoto in procedure.c
00051 
00052 labelNode::labelNode(idNode * ident, stmtNode * the_stmt)
00053   : targetNode(Label, the_stmt, ident->coord()), 
00054     _name(ident->name()),
00055     _references()
00056 {
00057   /* -- NOTE: This is all made obsolete by goto_label_walker....
00058 
00059   labelNode * orig = Symbols::Labels->insert(ident->text(), this);
00060 
00061   if (orig) {
00062 
00063     // -- Conflict
00064 
00065     if ( ! is_undeclared() )
00066       if ( ! orig->is_undeclared() )
00067         CBZ::SyntaxError(coord(),
00068                          string("multiple definitions of label `") +
00069                          orig->name() + string("'"));
00070       else {
00071         // -- Original is undeclared
00072         // Loop over all the references to the old one and point them here.
00073 
00074         goto_list & olist = orig->references();
00075         goto_list_p p;
00076 
00077         for (p = olist.begin(); p != olist.end(); ++p)
00078           (*p)->label(this);
00079 
00080         // --  Prevent EndOfScope checker from whining
00081         orig->stmt((blockNode *)0);
00082         _references = orig->references();
00083       }
00084   }
00085 
00086   */
00087 }
00088 
00089 // -- Build an undeclared label (used in ResolveGoto when we reach
00090 // a goto before seeing the label.
00091 // Now obsolete.
00092 
00093 labelNode::labelNode(idNode * ident)
00094   : targetNode(Label, (stmtNode *) 0, ident->coord()), 
00095     _name(ident->name()),
00096     _references()
00097 {
00098   // labelNode * orig = Symbols::Labels->insert(ident->text(), this);
00099 }
00100 
00101 // ------------------------------------------------------------
00102 // Undeclared
00103 // ------------------------------------------------------------
00104 // A label is undeclared if it has a stmt(), but the statement
00105 // is of type "Undeclared".
00106 
00107 bool labelNode::is_undeclared() const
00108 {
00109   return (stmt() && stmt()->typ() == Undeclared);
00110 }
00111 
00112 // ------------------------------------------------------------
00113 //  Walker
00114 // ------------------------------------------------------------
00115 
00116 void labelNode::visit(Visitor * the_visitor) 
00117 {
00118   the_visitor->at_label(this);
00119 }
00120 
00121 void labelNode::walk(Walker & the_walker)
00122 {
00123   Walker::Order ord = the_walker.order(); 
00124 
00125   if (ord == Walker::Preorder || ord == Walker::Both)
00126     the_walker.at_label(this, Walker::Preorder);
00127 
00128   if (the_walker.depth() == Walker::Subtree) {
00129     // -- Visit the children 
00130 
00131     if (stmt())
00132       stmt()->walk(the_walker);
00133   }
00134 
00135   if (ord == Walker::Postorder || ord == Walker::Both)
00136     the_walker.at_label(this, Walker::Postorder);
00137 }
00138 
00139 // ------------------------------------------------------------
00140 //  Dataflow
00141 // ------------------------------------------------------------
00142 
00143 void labelNode::dataflow(FlowVal * v, FlowProblem & fp)
00144 {
00145   if (fp.forward()) {
00146     v->meet(at_entry());
00147 
00148     fp.flow_label(v, this, FlowProblem::Entry);
00149 
00150     if (stmt())
00151       stmt()->dataflow(v, fp);
00152 
00153     fp.flow_label(v, this, FlowProblem::Exit);
00154   }
00155   else {
00156     fp.flow_label(v, this, FlowProblem::Exit);
00157 
00158     if (stmt())
00159       stmt()->dataflow(v, fp);
00160 
00161     fp.flow_label(v, this, FlowProblem::Entry);
00162 
00163     v->meet(at_entry());
00164   }
00165 }
00166 
00167 // ------------------------------------------------------------
00168 // Output
00169 // ------------------------------------------------------------
00170 
00171 void labelNode::output_stmt(output_context & ct, Node * parent)
00172 {
00173   ct << name() << ':';
00174 
00175   if (stmt())
00176     stmt()->output(ct, this);
00177   else
00178     ct << ';';
00179 }
00180 
00181 // ------------------------------------------------------------
00182 //  Changer
00183 // ------------------------------------------------------------
00184 
00185 Node * labelNode::change(Changer & the_changer, bool redispatch)
00186 {
00187   Changer::Order ord = the_changer.order(); 
00188   labelNode * the_label = this;
00189 
00190   if ((ord == Changer::Preorder || ord == Changer::Both) && ! redispatch)
00191     the_label = (labelNode *) the_changer.at_label(the_label, Changer::Preorder);
00192 
00193   if (the_label) {
00194 
00195     if (the_label != this)
00196       return the_label->change(the_changer, true);
00197 
00198     blockNode * old_stmt = the_label->stmt();
00199     if (old_stmt) {
00200       blockNode * new_stmt = (blockNode *) old_stmt->change(the_changer);
00201       if (old_stmt != new_stmt) {
00202         //if (the_changer.delete_old())
00203           //delete old_stmt;
00204         the_label->stmt(new_stmt);
00205       }
00206     }
00207 
00208   }
00209 
00210   if ((ord == Changer::Postorder || ord == Changer::Both) && ! redispatch)
00211     the_label = (labelNode *) the_changer.at_label(the_label, Changer::Postorder);
00212 
00213   return the_label;
00214 }
00215 
00216 
00217 // ------------------------------------------------------------
00218 // Destructor
00219 // ------------------------------------------------------------
00220 
00221 labelNode::~labelNode()
00222 {
00223 }

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