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  

testNodeInfo.cc

Go to the documentation of this file.
00001 // $Id: testNodeInfo.cc,v 1.4 2003/08/07 23:13:36 pnav Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2003 University of Texas at Austin
00008 // 
00009 //  Samuel Z. Guyer
00010 //  Adam Brown
00011 //  Teck Bok Tok
00012 //  Paul Arthur Navratil
00013 //  Calvin Lin
00014 // 
00015 //  Permission is hereby granted, free of charge, to any person
00016 //  obtaining a copy of this software and associated documentation
00017 //  files (the "Software"), to deal in the Software without
00018 //  restriction, including without limitation the rights to use, copy,
00019 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00020 //  of the Software, and to permit persons to whom the Software is
00021 //  furnished to do so, subject to the following conditions:
00022 //  
00023 //  The above copyright notice and this permission notice shall be
00024 //  included in all copies or substantial portions of the Software.
00025 //  
00026 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00030 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00031 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00032 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00033 //  THE SOFTWARE.
00034 //
00035 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00036 //  Computer Science for inspiring parts of the C-Breeze design.
00037 //
00038 // ----------------------------------------------------------------------
00039 
00040 #include "nodeinfo.h"
00041 #include "linker.h"
00042 
00043 /* test program for NodeInfo.
00044  * This program writes some AST nodes to a file name "file".
00045  * It also writes the callgraph to the same file.
00046  */
00047 
00048 class W : public Walker {
00049 public:
00050   NodeInfo *ni;
00051   int_list container;
00052   W() : Walker(Both, Subtree) {
00053     ni = new NodeInfo("file", false,true);
00054     container.push_back(-1);
00055   }
00056 
00057   void at_type(typeNode *type, Order ord) {
00058     if(ord==Postorder) {
00059       if(ni->nodeIndex(type) == container.back())
00060         container.pop_back();
00061       return;
00062     }
00063     if(type->typ()==Func) return;
00064     ni->writeType(type);
00065     if(ni->nodeIndex(type)>0)
00066       container.push_back(ni->nodeIndex(type));
00067   }
00068   void at_proc(procNode *proc, Order ord) {
00069     if(ord==Postorder) {
00070       if(ni->nodeIndex(proc) == container.back())
00071         container.pop_back();
00072       return;
00073     }
00074     ni->writeProc(proc);
00075     if(ni->nodeIndex(proc)>0)
00076       container.push_back(ni->nodeIndex(proc));
00077   }
00078 
00079 #define CG_PREFIX "Callgraph:"
00080   void at_call(callNode *call, Order ord) {
00081     if(ord==Postorder) return;
00082     ni->writeCall(call);
00083     procNode *proc = call->proc();
00084     if(!proc) return;
00085     ni->writeProc(proc);
00086     ostringstream ost;
00087     ost << CG_PREFIX << ni->nodeIndex(call) << ":" << ni->nodeIndex(proc)
00088         << endl << '\0';
00089     ni->writeString(ost.str());
00090   }
00091   void at_decl(declNode *decl, Order ord)
00092   { if(ord==Postorder) return;
00093     ni->writeDecl(decl, container.back()); }
00094   void at_stmt(stmtNode *stmt, Order ord)
00095   { if(ord==Postorder) return;
00096     ni->writeStmt(stmt, container.back()); }
00097 };
00098 
00099 extern char *TypNames[];
00100 
00101 class P : public Phase {
00102 public:
00103   bool read;
00104   P(bool _read) : read(_read), Phase() {}
00105   void run() {
00106     if(!read) {
00107       Linker linker;
00108       linker.link();
00109       W w;
00110       for(unit_list_p u=CBZ::Program.begin(); u!=CBZ::Program.end(); u++)
00111         (*u)->walk(w);
00112       return;
00113     }
00114 
00115     /*
00116     cout << "#units: " << CBZ::Program.size() << endl;
00117     for(unit_list_p u=CBZ::Program.begin(); u!=CBZ::Program.end(); u++) {
00118       cout << "#defs = " << (*u)->defs().size() << endl;
00119       for(def_list_p d=(*u)->defs().begin(); d!=(*u)->defs().end(); d++)
00120         cout << (*d)->typ() << " @" << (*d)->coord() << endl;
00121       for(suespec_list_p p=(*u)->suespecs().begin();
00122           p!=(*u)->suespecs().end();p++)
00123         cout << (*p)->name() << " @" << (*p)->coord() << endl;
00124     }*/
00125 
00126     // read...
00127     NodeInfo *ni = new NodeInfo("file", true, true);
00128     int n = ni->nStrings();
00129     for(int i=1; i<=n; i++) {
00130       Node *node = ni->indexNode(i);
00131       if(!node) {
00132         string s = ni->index(i);
00133         int pos = s.find(CG_PREFIX);
00134         if(pos != 0) {
00135           cerr << "invalid line #" << i << endl;
00136           continue;
00137         }
00138         // get rid of prefix
00139         s = s.substr(pos+strlen(CG_PREFIX),string::npos);
00140         // get caller
00141         pos = s.find(':');
00142         string caller_str = s.substr(0,pos);
00143         Node *caller = ni->indexNode( atoi(caller_str.c_str()) );
00144         if(!caller) continue;
00145         assert(caller->typ() == Call);
00146 
00147         // get callees
00148         s = s.substr(pos+1,string::npos);
00149         Node *callee = ni->indexNode( atoi(s.c_str()) );
00150         if(!callee) continue;
00151         assert(callee->typ() == Proc);
00152         cout << "line " << i << " " << CG_PREFIX << ni->nodeIndex(caller)
00153              << "->" << ni->nodeIndex(callee) << endl;
00154       }
00155       else if(node->typ()==Array)
00156         cout << "line " << i << " Array " << type_name((arrayNode*)node)<<endl;
00157       else if(node->typ()==Ptr)
00158         cout << "line " << i << " Ptr " << type_name((ptrNode*)node)<<endl;
00159       else if(node->typ()==Func)
00160         cout << "line " << i << " Func " << type_name((funcNode*)node) <<endl;
00161       else
00162         cout << "line " << i << " typ=" << TypNames[node->typ()] << " @"
00163              << node->coord() << endl;
00164     }
00165   }
00166   string type_name(typeNode *t) {
00167     assert(t!=NULL);
00168     switch(t->typ()) {
00169       case Prim: {
00170         basic_type basic = ((primNode*)t)->basic();
00171         return basic.to_string();
00172       }
00173 
00174       case Tdef:
00175         return ((tdefNode*)t)->name();
00176 
00177       case Array:
00178         return type_name( ((arrayNode*) t)->type() ) + "[]\0";
00179 
00180       case Ptr:
00181         return type_name( ((ptrNode*) t)->type() ) + "*\0";
00182 
00183       case Func:
00184         return type_name( ((funcNode*)t)->type() ) + "(func)";
00185 
00186       case Struct:
00187       case Union:
00188       case Enum:
00189         return string(TypNames[t->typ()]) + "-" + ((sueNode*)t)->spec()->name();
00190 
00191       case sueSpec:
00192         return string(TypNames[t->typ()]) + "-" + ((suespecNode*)t)->name();
00193 
00194       default:
00195         cout << "type_name: unknown typenode "
00196              << t->typ() << endl;
00197         exit(1);
00198     }
00199   } // type_name
00200 
00201 };
00202 
00203 Phases READ("read", new P(true));
00204 Phases WRITE("write", new P(false));

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