C-Breeze
C Compiler Infrastructure

[ Project home page]

dismantle.h

Go to the documentation of this file.
00001 // $Id: dismantle.h,v 1.23 2003/11/20 19:06:14 toktb Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 // 
00009 //  Adam Brown
00010 //  Samuel Z. Guyer
00011 //  Daniel A. Jimenez
00012 //  Calvin Lin
00013 // 
00014 //  Permission is hereby granted, free of charge, to any person
00015 //  obtaining a copy of this software and associated documentation
00016 //  files (the "Software"), to deal in the Software without
00017 //  restriction, including without limitation the rights to use, copy,
00018 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00019 //  of the Software, and to permit persons to whom the Software is
00020 //  furnished to do so, subject to the following conditions:
00021 //  
00022 //  The above copyright notice and this permission notice shall be
00023 //  included in all copies or substantial portions of the Software.
00024 //  
00025 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00026 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00027 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00028 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00029 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00030 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00031 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00032 //  THE SOFTWARE.
00033 //
00034 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00035 //  Computer Science for inspiring parts of the C-Breeze design.
00036 //
00037 // ----------------------------------------------------------------------
00038 
00039 #ifndef CBZ_DISMANTLE_H
00040 #define CBZ_DISMANTLE_H
00041 
00042 #include <map>
00043 #include <set>
00044 #include <stack>
00045 
00046 typedef map<unsigned int, unsigned int> op_id_map;
00047 typedef set<unsigned int> op_id_set;
00048 
00049 class DismantleUtil {
00050  public:
00051   static void init(void);
00052   static stmtNode * empty_stmt(void) {
00053     return new exprstmtNode(NULL);
00054   }
00055   static blockNode * empty_block(void) {
00056     blockNode * ret = new blockNode(NULL, NULL);
00057     ret->stmts().push_back(empty_stmt());
00058     return ret;
00059   }
00060   // TODO: implement REAL unique id generator
00061   static char* new_id(const char *s = "") {
00062     char * buf = new char[80];
00063     snprintf(buf, 80, "__%s%s", CBZ::get_temp_id_str().c_str(), s);
00064     return buf;
00065   }
00066   static op_id_map not_relop;
00067   static exprNode * Not(exprNode *);
00068   static declNode * new_temp_decl(typeNode *, Coord);
00069   static labelNode * new_label(Coord);
00070 };
00071 
00112 class Dismantle : public Changer {
00113  private:
00114   int _flags;
00115 
00116   Dismantle(int flags);
00117  public:
00118   static const int DISMANTLE_CONTROL    = 1 << 0;
00119   static const int DISMANTLE_EXPRS      = 1 << 1;
00120 
00121   static void dismantle_control(unitNode *);
00122   static void dismantle(unitNode *);
00123   virtual Node * at_unit(unitNode *, Order);
00124   virtual Node * at_proc(procNode *, Order);
00125 };
00126 
00127 class StaticToGlobalDismantle : public Changer {
00128  public:
00129   StaticToGlobalDismantle(void);
00130   virtual Node * at_unit(unitNode *, Order);
00131   virtual Node * at_proc(procNode *, Order);
00132   virtual Node * at_block(blockNode *, Order);
00133   virtual Node * at_decl(declNode *, Order);
00134 
00135  private:
00136   unitNode * _cur_unit;
00137   procNode * _cur_proc;
00138   block_list _blockStack;
00139   map<declNode *, decl_list> _declsToMove;
00140 
00141   // indicates whether one of the statics refers to the containing procedure
00142   // declaration, thus requiring procedure to be declared early
00143   bool _declareProc;
00144   set < suespecNode * > _topLevelSuespecs;
00145   map < typeNode *, string > _newTypedefName;
00146 
00147   void rename_decl(declNode *, procNode *);
00148 };
00149 
00150 class LabelDismantle : public Changer {
00151  public:
00152   LabelDismantle(void);
00153   virtual Node * at_label(labelNode *, Order);
00154 };
00155 
00156 class InitializerDismantle : public Changer {
00157  public:
00158   InitializerDismantle(void);
00159   virtual Node * at_block(blockNode *, Order);
00160   virtual Node * at_decl(declNode *, Order);
00161 
00162  private:
00163   map<blockNode *, blockNode *> _assgblock;
00164   stack<blockNode *> _blockStack;
00165   
00166   exprNode * eval_or_cast(exprNode *, typeNode *);
00167 
00168   void init_scalar(declNode *, exprNode *);
00169   initializerNode * init_array(arrayNode *, expr_list_p &, expr_list_p,
00170                                bool inSublist);
00171   initializerNode * init_struct(structNode *, expr_list_p &, expr_list_p,
00172                                 bool inSublist);
00173 
00174   bool isStringLiteral(exprNode *);
00175   initializerNode * strLit2Init(const char *);
00176 };
00177 
00178 class ControlDismantle : public Changer {
00179  public:
00180   ControlDismantle(void);
00181   virtual Node * at_proc(procNode *, Order ord);
00182 };
00183 
00184 class BreakContinueChanger : public Changer {
00185  private:
00186   Node * _top;
00187   labelNode * _break_label;
00188   labelNode * _continue_label;
00189   loopNode  * _new_container;
00190 
00191  public:
00192   BreakContinueChanger(Node *, labelNode *, labelNode *, loopNode *L=NULL);
00193   virtual Node * at_break(breakNode *, Order);
00194   virtual Node * at_continue(continueNode *, Order);
00195 };
00196 
00197 class LoopDismantle : public Changer {
00198  public:
00199   LoopDismantle(void);
00200   virtual Node * at_while(whileNode *, Order);
00201   virtual Node * at_for(forNode *, Order);
00202   virtual Node * at_do(doNode *, Order);
00203 };
00204 
00205 class TernaryDismantle : public Changer {
00206   map<stmtNode*,blockNode*> _new_block;
00207   stmtNode                 *_cur_stmt;
00208   int                       _in_type;
00209 
00210  public:
00211   TernaryDismantle(void);
00212   virtual Node * at_stmt(stmtNode *, Order);
00213   virtual Node * at_ternary(ternaryNode *, Order);
00214   virtual Node * at_type(typeNode *, Order);
00215 };
00216 
00217 class SelectionDismantle : public Changer {
00218  private:
00219   map<stmtNode*,blockNode*> _expr_block;
00220   stmtNode *_cur_stmt;
00221   bool _in_expr;
00222 
00223  public:
00224   SelectionDismantle(void);
00225   virtual Node * at_exprstmt(exprstmtNode *, Order);
00226   virtual Node * at_return(returnNode *, Order);
00227   virtual Node * at_binary(binaryNode *, Order);
00228   virtual Node * at_if(ifNode *, Order);
00229   virtual Node * at_switch(switchNode *, Order);
00230   virtual Node * at_stmt(stmtNode *, Order);
00231  private:
00232   indexNode * comparison_operand(exprNode *, blockNode *);
00233   Node * andand_oror_in_expr(stmtNode *, Order);
00234 };
00235 
00236 class ReturnDismantle : public Changer {
00237  public:
00238   ReturnDismantle(void);
00239   virtual Node * at_proc(procNode *, Order);
00240   virtual Node * at_return(returnNode *, Order);
00241 };
00242 
00243 class ArrowDismantle : public Changer {
00244  public:
00245   ArrowDismantle(void);
00246   virtual Node * at_binary(binaryNode *, Order);
00247 };
00248 
00249 class ExpressionDismantle : public Changer {
00250  private:
00251   blockNode * _new_block;
00252   int _in_type;
00253   static op_id_map _op_assign_map;
00254   static op_id_set _op_post;
00255 
00256  public:
00257   static void init();
00258 
00259   ExpressionDismantle(void);
00260   virtual Node * at_exprstmt(exprstmtNode *, Order);
00261   virtual Node * at_type(typeNode *, Order);
00262   virtual Node * at_binary(binaryNode *, Order);
00263   virtual Node * at_unary(unaryNode *, Order);
00264   virtual Node * at_call(callNode *, Order);
00265   virtual Node * at_comma(commaNode *, Order);
00266   virtual Node * at_cast(castNode *, Order);
00267 };
00268 
00269 class FlattenDismantle : public Changer {
00270  private:
00271   blockNode * _new_body;
00272   stmtNode * _last_stmt;
00273   labelNode * _return_label;
00274   procNode * _cur_proc;
00275   set<string> _vars;
00276   set<string> _labels;
00277 
00278  public:
00279   FlattenDismantle(void);
00280   virtual Node * at_proc(procNode *, Order);
00281   virtual Node * at_decl(declNode *, Order);
00282   virtual Node * at_label(labelNode *, Order);
00283   virtual Node * at_threeAddr(threeAddrNode *, Order);
00284   virtual Node * at_goto(gotoNode *, Order);
00285   virtual Node * at_return(returnNode *, Order);
00286   virtual Node * at_exprstmt(exprstmtNode *, Order);
00287 };
00288 
00289 #endif /* CBZ_DISMANTLE_H */

Generated on February 1, 2006
Back to the C-Breeze home page