00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "nodeinfo.h"
00041 #include "linker.h"
00042
00043
00044
00045
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
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
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
00139 s = s.substr(pos+strlen(CG_PREFIX),string::npos);
00140
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
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 }
00200
00201 };
00202
00203 Phases READ("read", new P(true));
00204 Phases WRITE("write", new P(false));