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  

dismantle.cc

Go to the documentation of this file.
00001 // $Id: dismantle.cc,v 1.11 2003/08/11 17:28:44 abrown 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 // dismantle.cc
00040 //
00041 
00042 #include "c_breeze.h"
00043 #include "dismantle.h"
00044 #include "semcheck.h"
00045 #include "ref_clone_changer.h"
00046 
00047 op_id_map DismantleUtil::not_relop;
00048 
00049 void DismantleUtil::init() {
00050   if ( not_relop.empty() ) {
00051     not_relop['<'] = Operator::GE;
00052     not_relop[Operator::GE] = '<';
00053     not_relop['>'] = Operator::LE;
00054     not_relop[Operator::LE] = '>';
00055     not_relop[Operator::EQ] = Operator::NE;
00056     not_relop[Operator::NE] = Operator::EQ;
00057   }
00058 }
00059 
00060 exprNode * DismantleUtil::Not(exprNode * the_expr) {
00061   if ( the_expr->typ() == Binary ) {
00062     binaryNode * the_binary = (binaryNode *) the_expr;
00063     if ( not_relop.find(the_binary->op()->id()) != not_relop.end() ) {
00064       the_binary->op(Operators::table[not_relop[the_binary->op()->id()]]);
00065       return the_binary;
00066     }
00067   } else if ( the_expr->typ() == Unary ) {
00068     unaryNode * the_unary = (unaryNode *) the_expr;
00069     if ( the_unary->op()->id() == '!' )
00070       return the_unary->expr();
00071   }
00072   unaryNode * ret = new unaryNode('!', the_expr, the_expr->coord());
00073   ret->type(the_expr->type()); // TODO: is this right? does ! apply to floats?
00074   return ret;
00075 }
00076 
00077 declNode * DismantleUtil::new_temp_decl(typeNode * the_type,
00078                                                  Coord coord) {
00079   typeNode * decl_type = NULL;
00080   if ( the_type->is_pointer() ) {
00081     decl_type = 
00082       new ptrNode(typeNode::NONE,
00083                   (typeNode *) ref_clone_changer::clone(the_type->type(),
00084                                                         false));
00085   } else {
00086     decl_type = (typeNode *) ref_clone_changer::clone(the_type, false);
00087   }
00088   declNode * ret = new declNode(DismantleUtil::new_id(),
00089                                 declNode::NONE,
00090                                 decl_type,
00091                                 NULL,
00092                                 NULL,
00093                                 coord);
00094   ret->decl_location(declNode::BLOCK);
00095   return ret;
00096 }
00097 
00098 labelNode * DismantleUtil::new_label(Coord coord) {
00099   return new labelNode(new_id(), empty_block(), coord);
00100 }
00101 
00102 Dismantle::Dismantle(int flags):
00103   Changer(Preorder, Subtree, false),
00104   _flags(flags)
00105 {}
00106 
00107 void Dismantle::dismantle_control(unitNode * the_unit) {
00108   Dismantle tad(DISMANTLE_CONTROL);
00109   the_unit->change(tad);
00110 }
00111 
00112 void Dismantle::dismantle(unitNode * the_unit) {
00113   Dismantle tad(DISMANTLE_CONTROL | DISMANTLE_EXPRS);
00114   the_unit->change(tad);
00115 }
00116 
00117 Node * Dismantle::at_unit(unitNode * the_unit, Order ord) {
00118   DismantleUtil::init();
00119 
00120   // turn all 'static' variables into globals
00121   StaticToGlobalDismantle stgd;
00122   the_unit->change(stgd);
00123 
00124   // Fill in the blanks in initializers & dismantle the initialization of
00125   // scalars
00126   InitializerDismantle id;
00127   the_unit->change(id);
00128 
00129   semcheck_walker::check(the_unit, false);
00130 
00131   return the_unit;
00132 }
00133 
00134 Node * Dismantle::at_proc(procNode * the_proc, Order ord) {
00135   // 'dismantle' labels, so they label empty stmts
00136   LabelDismantle ld;
00137   the_proc->change(ld);
00138 
00139   // dismantle control flow: loops, &&, ||, ?:
00140   ControlDismantle cd;
00141   the_proc->change(cd);
00142 
00143   // opt:  dismantle expressions into threeAddrNodes
00144   if ( _flags & DISMANTLE_EXPRS ) {
00145     ExpressionDismantle::init();
00146     ExpressionDismantle ed;
00147     the_proc->change(ed);
00148   }
00149 
00150   // Flatten the resulting code
00151   FlattenDismantle fd;
00152   the_proc->change(fd);
00153 
00154   return the_proc;
00155 }

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