Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
com_whirlview.cxx
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2 of the GNU General Public License as
00007   published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if 
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU General Public License along
00021   with this program; if not, write the Free Software Foundation, Inc., 59
00022   Temple Place - Suite 330, Boston MA 02111-1307, USA.
00023 
00024   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00025   Mountain View, CA 94043, or:
00026 
00027   http://www.sgi.com
00028 
00029   For further information regarding this notice, see:
00030 
00031   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00032 
00033 */
00034 
00035 
00036 /* -*-Mode: c++;-*- (Tell emacs to use c++ mode) */
00037 /* ====================================================================
00038  *
00039  * ====================================================================
00040  */
00041 
00042 #include <stdio.h>
00043 #include <string.h>
00044 
00045 #include <vector>
00046 
00047 using namespace std;
00048 
00049 #include "defs.h"
00050 #include "errors.h"
00051 #include "srcpos.h"
00052 #include "opcode.h"
00053 #include "wn.h"         /* Whirl Node descriptor */
00054 #include "wn_simp.h"    /* Whirl simplifier (can be stubs) */
00055 
00056 // #include "stab.h"
00057 // #include "const.h"
00058 // #include "targ_const.h"
00059 // #include "strtab.h"
00060 // #include "wio.h"
00061 // #include "wintrinsic.h"
00062 // #include "wn_pragmas.h"
00063 
00064 #include "DaVinci.h"
00065 #include "wb_util.h"
00066 #include "com_whirlview.h"
00067 #include "fb_whirl.h"
00068 
00069 #define MAXEXPR 5000
00070 
00071 static MEM_POOL DV_wv_mempool;
00072 static BOOL     DV_wv_mempool_init = FALSE;
00073 static DaVinci *DV      = NULL;
00074 static WN      *Func_wn = NULL;
00075 
00076 static void draw_expr(WN *);
00077 static void draw_stmt(WN *);
00078 
00079 static char *
00080 id_str(WN *wn)
00081 {
00082   static char dv_id[64];
00083   INT         len;
00084 
00085  if ( wn ) {
00086    // don't need unique labels for display - node_id is unique.
00087    len = sprintf( dv_id, "%d:%d",
00088                   OPCODE_mapcat(WN_opcode(wn)), WN_map_id(wn) );
00089  } else {
00090    strcpy( dv_id, "NULL-WN" );
00091  }
00092 
00093  return dv_id;
00094 }
00095 
00096 static void
00097 draw_expr(WN *wn)
00098 
00099 {
00100   NODE_TYPE nt;
00101   EDGE_TYPE et;
00102   INT       i;
00103 
00104   DV->Node_Begin( NODE_ID(wn), id_str(wn), nt );
00105 
00106   for (i = 0; i < WN_kid_count(wn); i++) {
00107     WN *wn2 = WN_kid(wn,i);
00108     DV->Out_Edge( EDGE_ID(wn, wn2), et, NODE_ID(wn2) );
00109   }
00110   DV->Node_End();
00111 
00112   for (i = 0; i < WN_kid_count(wn); i++) {
00113     draw_expr( WN_kid(wn,i) );
00114   }
00115 }
00116 
00117 static void
00118 draw_stmt(WN *wn, BOOL show_expr)
00119 {
00120   NODE_TYPE nt;
00121   EDGE_TYPE et;
00122 
00123   vector<WN*> kids;
00124 
00125   switch (WN_opcode(wn)) {
00126   case OPC_BLOCK: {
00127     WN *wn2 = WN_first(wn);
00128     while (wn2) {
00129       kids.push_back( wn2 );
00130       wn2 = WN_next(wn2);
00131     }
00132     break;
00133   }
00134   case OPC_IF:
00135     nt.Shape( NS_RHOMBUS );
00136     if ( WN_then(wn) ) {
00137       kids.push_back( WN_then(wn) );  // THEN.
00138     }
00139     if ( show_expr ) {
00140       kids.push_back( WN_if_test(wn) ); // condition-expr.
00141     }
00142     if ( WN_else(wn) ) {
00143       kids.push_back( WN_else(wn) );  // ELSE.
00144     }
00145     break;
00146 
00147   case OPC_DO_LOOP:
00148     nt.Shape( NS_ELLIPSE );
00149     if ( show_expr ) kids.push_back( WN_index(wn) );   // INDX-expr.
00150     kids.push_back( WN_start(wn) );   // INIT.
00151     if ( show_expr ) kids.push_back( WN_end(wn) );     // COMP-expr.
00152     kids.push_back( WN_do_body(wn) ); // BODY.
00153     kids.push_back( WN_step(wn) );    // INCR.
00154     break;
00155 
00156   case OPC_COMPGOTO:
00157     if ( show_expr ) kids.push_back( WN_kid(wn,0) );   // SWCH-expr.
00158     kids.push_back( WN_kid(wn,1) );   // JMPS.
00159     if ( WN_kid_count(wn) > 2 ) {
00160       kids.push_back( WN_kid(wn,2) ); // DFLT.
00161     }
00162     break;
00163 
00164   case OPC_XGOTO:
00165     if ( show_expr ) kids.push_back( WN_kid(wn,0) );   // SWCH-expr.
00166     kids.push_back( WN_kid(wn,1) );   // JMPS.
00167     break;
00168 
00169   default: 
00170     {
00171       for (INT i = 0; i < WN_kid_count(wn); ++i) {
00172         WN* wn2 = WN_kid(wn,i);
00173         FmtAssert(wn2, ("Null kid in draw_stmt"));
00174         OPCODE opc2 = WN_opcode(wn2);
00175         if ( show_expr && OPCODE_is_expression(opc2) ) {
00176           kids.push_back( wn2 );
00177         } else if ( OPCODE_is_stmt(opc2) || OPCODE_is_scf(opc2) ) {
00178           kids.push_back( wn2 );
00179         }
00180       }
00181     }
00182   }
00183 
00184 
00185   DV->Node_Begin( NODE_ID(wn), id_str(wn), nt );
00186 
00187   vector<WN*>::iterator wn_iter;
00188 
00189   for (wn_iter = kids.begin(); wn_iter != kids.end(); ++wn_iter) {
00190     WN *wn2 = *wn_iter;
00191 
00192     DV->Out_Edge( EDGE_ID(wn,wn2), et, NODE_ID(wn2) );
00193   }
00194   DV->Node_End();
00195 
00196   for (wn_iter = kids.begin(); wn_iter != kids.end(); ++wn_iter) {
00197     WN *wn2 = *wn_iter;
00198 
00199     draw_stmt( wn2, show_expr );
00200   }
00201 }
00202 
00203 static void
00204 draw_whirl_tree(WN *wn, BOOL show_expr)
00205 {
00206   DV->Graph_Begin();
00207 
00208   if ( OPCODE_is_expression(WN_opcode(wn)) ) {
00209     draw_expr( wn );
00210   } else if ( OPCODE_is_stmt(WN_opcode(wn))
00211               || OPCODE_is_scf(WN_opcode(wn)) ) {
00212     draw_stmt( wn, show_expr );
00213   } else {
00214     FmtAssert( FALSE, ("opcode of unknown type") );
00215   }
00216   DV->Graph_End();
00217 }
00218 
00219 class Callback : public DaVinci_Callback {
00220   WN *node_sel;  // last node selected (or NULL).
00221 public:
00222   virtual void Node_Select(const INT n_ids, const NODE_ID id_array[]);
00223   virtual void Edge_Select(const EDGE_ID& id);
00224   virtual void Menu_Select(const char *menu_id);
00225 };
00226 
00227 void
00228 Callback::Node_Select(const INT n_ids, const NODE_ID id_array[])
00229 {
00230   char buf[ MAXEXPR ]; // more: better storage management.
00231 
00232   for (INT i = 0; i < n_ids; ++i) {
00233     WN *wn      = (WN *)id_array[i];
00234     WN *head_wn = ( Func_wn ? Func_wn : wn );
00235     INT end = WB_Dump_Whirl_Expr(head_wn, wn, buf, 0);
00236     buf[end] = '\0';
00237     printf("%p: %s\n", wn, buf);
00238   }
00239   node_sel = (n_ids > 0 ? (WN *)id_array[n_ids - 1] : NULL);
00240 }
00241 
00242 void
00243 Callback::Edge_Select(const EDGE_ID& id)
00244 {
00245   EDGE_TYPE et;
00246 
00247   et.Color( "red" );
00248   DV->Change_Attr( id, et );  // just to ack edge select event ..
00249 }
00250 
00251 void
00252 Callback::Menu_Select(const char *menu_id)
00253 {
00254   if ( strcmp( menu_id, "EXPAND" ) == 0 && node_sel ) {
00255     printf("selecting EXPAND for node %s\n", id_str(node_sel));
00256     // MORE: use graph update capability to expand tree.
00257     //       need to track which nodes have already been plotted.
00258   }
00259 }
00260 
00261 
00262 static MENU_INFO DV_Menu[] = {
00263   { "EXPAND", "Expand Subtree", true, 0, NULL }
00264   // more: add other useful queries here.
00265 };
00266 #define N_DV_MENU   ( sizeof(DV_Menu) / sizeof(DV_Menu[0]) )
00267 
00268 // more: consider du_mgr ..?
00269 
00270 void
00271 dV_view_whirl(WN *wn, const char *title, BOOL show_expr, FILE *trace_fp)
00272 {
00273   if ( ! DaVinci::enabled(true) ) return;
00274 
00275   Func_wn = (WN_operator(wn) == OPR_FUNC_ENTRY ? wn : NULL);
00276 
00277   const char *trace_fname = getenv("DV_TRACE_FILE");
00278   bool        local_trace = false;
00279 
00280   if ( trace_fp == NULL && trace_fname ) {
00281     if ( (trace_fp = fopen(trace_fname, "w")) != NULL ) {
00282       local_trace = true;
00283     } else {
00284       fprintf(stderr, "DV_TRACE_FILE not writeable\n");
00285       perror( trace_fname );
00286     }
00287   }
00288   FmtAssert( DV == NULL, ("dV_view_fb_cfg: DV is null"));
00289   if ( ! DV_wv_mempool_init ) {
00290     MEM_POOL_Initialize(&DV_wv_mempool, "DV_wv_mempool", FALSE);
00291     DV_wv_mempool_init = TRUE;
00292   }
00293   DV = CXX_NEW(DaVinci(&DV_wv_mempool, trace_fp), &DV_wv_mempool);
00294 
00295   const char *window_title = (title ? title : "com_whirlview tree display");
00296   DV->Title( window_title  );
00297   draw_whirl_tree( wn, show_expr );
00298   DV->Menu_Create( N_DV_MENU, DV_Menu );
00299 
00300   Callback callback;
00301   DV->Event_Loop( &callback );
00302 
00303   CXX_DELETE(DV, &DV_wv_mempool);
00304   DV      = NULL;
00305   Func_wn = NULL;
00306 
00307   if ( local_trace ) {
00308     (void)fclose( trace_fp );
00309   }
00310 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines