Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

LegoRWrt.C

Go to the documentation of this file.
00001 /*
00002  *   Name:        LegoRWrt.C
00003  *
00004  *   Designer:    Willie Glover
00005  *
00006  *   Created:     4/14/96
00007  *
00008  *   Description: Routine to write out regions and ops in REBEL.
00009  *
00010  *   Location:    /ncsu/tinker/LEGO/src/util/LegoRWrt.C
00011  *
00012  *
00013  * 4/14/96 WTG:  created this file to test op structure
00014  * ...        :  updated to write everything else out, apparently...
00015  * 3/16/97 WAH:  fixed LegoWrite (legoRegion *, FILE *, int) to follow
00016  *               legoOp linkage instead of iterating over them
00017  * 3/19/97 WAH:  Added trace writing.
00018  * 3/26/97 WAH:  now writes out edge attributes.
00019  * 4/2/97 WAH:   Got LegoWrite to close its file pointer.
00020  * 4/15/97 WAH:  Investigated writing of predicate operands, name changes.
00021  * 4/18/97 WAH:  Fixed writing of empty out() list for ops.
00022  * 7/8/97 WAH:   Adjusted blockid writing of op.
00023  * 9/10/97 WAH:  Repaired calling LegoWrite with legoModule * only.
00024  * 9/23/97 WAH:  New Rebel changes.
00025  * 10/1/97 WAH:  Added writing of (empty) mem_vrs dictionary; removed
00026  *               writing of "flags ()" if no flags in region.
00027  * 10/6/97 WAH:  Changed cb writing control from #define to knob.
00028  * 12/8/97 WAH:  Fixed usage of knobs file to ensure always created
00029  *               for any LegoWrite call.
00030  * 1/22/98 WAH:  Fixed blockId writing (not "block", it's "bb_id").
00031  * 3/25/98 WAH:  Changed LegoWrite #4 to default to stdout.
00032  * 4/28/02 HZ:   Added support for the IA64 opcodes (i.e., LegoWrt writes the
00033  *               Rebel version of IA64 asm generated by IA64Read.
00034  */
00035 // ---------------------------------------------------------------------------
00036 
00037 #include "lego.H"
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <stl.h>
00041 #include <string.h>
00042 #include "TinkerKnobs.H"
00043 #include "legocompress.H"
00044 
00045 // Comment out the following line to use the new OT_LITERAL_CB writing style.
00046 //#define OLD_OT_LITERAL_CB
00047 
00048 // #define stderr wwout
00049 
00050 // extern FILE *wwout;
00051 
00052 #define INDENT_SIZE (80)
00053 #define INDENT_INC (2)
00054 
00055 char *ParseMap(int);
00056 char *RegionMap(int);
00057 //void  LegoErr(char*);
00058 void  LegoFree(legoOp*);
00059 int  LegoWrite (legoRegion*, FILE *, int);
00060 int  LegoWrite (legoRegion*, char *);
00061 int  LegoWrite (legoRegion*);
00062 
00063 void WriteJump(legoProc*, FILE*);
00064 void WriteAttrs(legoProc*, FILE*);
00065 void WriteEdges(legoProc*, FILE*);
00066 void WriteData(legoProc*, FILE*);
00067 void WriteData(legoModule*, FILE*);
00068 void WriteMem(legoProc*, FILE*);
00069 
00070 /*-----------------------------------------------------------------
00071  * enum rStyles { STATIC='s', ROTATING='r'};  // static or rotating registers
00072  *-----------------------------------------------------------------*/
00073   static const char * const regRTN[] = {"r","vr","mvr","fr","br"};
00074   static const char * const regFTN[] = {"gpr","fpr","pr","btr","cr"};
00075   static const char * const regDTN[] = {"vo","bo","ch","uch","sh","ush","i",
00076                                         "ui","lng","ulng","llng","ullng",
00077                                         "lllng","ulllng","ptr","f","d","p",
00078                                         "b"};
00079 
00080   int lineLength;
00081   knobs *K = NULL;
00082 
00083 /*
00084  * WriteOprd (designed by Willie Glover)
00085  *   oprdPtr = pointer to legoOprd to write
00086  *   outfile = FILE to which to write
00087  *   returns: nothing
00088  *
00089  * Writes out a legoOprd in REBEL format. Does not traverse to next
00090  * operand automatically.
00091  *
00092  * 9/3/97 WAH:   Fixed writing of OT_LITERAL_D (%d -> %lf), OT_LITERAL_C
00093  *               (%s -> %c).
00094  * 9/23/97 WAH:  Added writing of addrStyle for OT_LITERAL_L, OT_LITERAL_S;
00095  *               changed OT_LITERAL_CB writing from "cb" to just "b";
00096  *               updated OT_BOOLEAN/OT_LITERAL_P.
00097  * 9/26/97 WAH:  Added new operand types (14: VO, BO, (U)CH, (U)SH, UI,
00098  *               (U)(L)(L)LNG, PTR.
00099  * 10/31/97 WAH: Added non-printing of invalid operands.
00100  */
00101 void WriteOprd (legoOprd* oprdPtr, FILE *outfile)
00102 {
00103   double dblLit;
00104   float  fltLit;
00105   int    intLit, chrLit, cbLit, predLit, wrtMacro;
00106   long long llLit;
00107   char *lblLit;
00108   char *strLit;
00109   int regN, regT, regS, dataT, fileT, omegaData, intV;     // register data
00110   enum addrStyles as;
00111   char newcbstyle;
00112   if (oprdPtr == NULL) { return; }
00113   if (!(oprdPtr->GetValid())) return;
00114   int srcOprdType = oprdPtr->GetOprdType();
00115   switch (srcOprdType) {
00116     case OT_VACANT:  // storage allocated, but not set
00117          lineLength += fprintf(outfile,"VACANT");
00118          break;
00119   case OT_LITERAL_VO:
00120     lineLength += fprintf(outfile,"vo<>");
00121     break;
00122   case OT_LITERAL_BO:
00123     intLit = oprdPtr->GetLiteralBoolean();
00124     lineLength += fprintf(outfile,"bo<%d>",intLit);
00125     break;
00126   case OT_LITERAL_CH:
00127     intLit = oprdPtr->GetLiteralChar();
00128     lineLength += fprintf(outfile,"ch<%d>",intLit);
00129     break;
00130   case OT_LITERAL_UCH:
00131     intLit = oprdPtr->GetLiteralChar();
00132     lineLength += fprintf(outfile,"uch<%d>",intLit);
00133     break;
00134   case OT_LITERAL_SH:
00135     intLit = oprdPtr->GetLiteralShort();
00136     lineLength += fprintf(outfile,"sh<%d>",intLit);
00137     break;
00138   case OT_LITERAL_USH:
00139     intLit = oprdPtr->GetLiteralUShort();
00140     lineLength += fprintf(outfile,"ush<%d>",intLit);
00141     break;
00142     case OT_LITERAL_I:
00143          intLit = oprdPtr->GetLiteralInteger();
00144          lineLength += fprintf(outfile,"i<%d>",intLit);
00145          break;
00146   case OT_LITERAL_UI:
00147     llLit = oprdPtr->GetLiteralUInteger();
00148     lineLength += fprintf(outfile,"ui<%ld>",intLit);
00149     break;
00150   case OT_LITERAL_LNG:
00151     intLit = oprdPtr->GetLiteralLong();
00152     lineLength += fprintf(outfile,"lng<%ld>",intLit);
00153     break;
00154   case OT_LITERAL_ULNG:
00155     intLit = oprdPtr->GetLiteralULong();
00156     lineLength += fprintf(outfile,"ulng<%uld>",intLit);
00157     break;
00158     // Sau. seen some serious bugs in here.
00159     // changing only for LONG LONG right now (02/23/02) 
00160   case OT_LITERAL_LLNG:
00161     llLit = oprdPtr->GetLiteralLongLong();
00162     lineLength += fprintf(outfile,"llng<%lld>",llLit);
00163     break;
00164   case OT_LITERAL_ULLNG:
00165     intLit = oprdPtr->GetLiteralULongLong();
00166     lineLength += fprintf(outfile,"ullng<%uld>",intLit);
00167     break;
00168   case OT_LITERAL_LLLNG:
00169     intLit = oprdPtr->GetLiteralLongLongLong();
00170     lineLength += fprintf(outfile,"lllng<%ld>",intLit);
00171     break;
00172   case OT_LITERAL_ULLLNG:
00173     intLit = oprdPtr->GetLiteralULongLongLong();
00174     lineLength += fprintf(outfile,"ulllng<%uld>",intLit);
00175     break;
00176   case OT_LITERAL_PTR:
00177     intLit = oprdPtr->GetLiteralPointer();
00178     lineLength += fprintf(outfile,"ptr<%ld>",intLit);
00179     break;
00180     case OT_LITERAL_F:
00181          fltLit = oprdPtr->GetLiteralFloat();
00182          lineLength += fprintf(outfile,"f<%f>",fltLit);
00183          break;
00184     case OT_LITERAL_D:
00185          dblLit = oprdPtr->GetLiteralDouble();
00186          lineLength += fprintf(outfile,"d<%.0lf>",dblLit);
00187          break;
00188     case OT_LITERAL_C:
00189          chrLit = oprdPtr->GetLiteralChar();
00190          lineLength += fprintf(outfile,"c<%c>",chrLit);
00191          break;
00192     case OT_LITERAL_L:
00193          lblLit = oprdPtr->GetLiteralLabel();
00194          as = oprdPtr->GetLiteralAddrStyle();
00195          if (as == AS_UNDEF)
00196            lineLength += fprintf(outfile,"l<%s>",lblLit);
00197          else
00198            lineLength += fprintf(outfile,"l:%s<%s>",ParseMap(as+G_ABS-1),
00199                                  lblLit);
00200          break;
00201     case OT_LITERAL_S:
00202          strLit = oprdPtr->GetLiteralString();
00203          as = oprdPtr->GetLiteralAddrStyle();
00204          if (as == AS_UNDEF)
00205            lineLength += fprintf(outfile,"s<\"%s\">",strLit);
00206          else
00207            lineLength += fprintf(outfile,"s:%s<\"%s\">",ParseMap(as+G_ABS-1),
00208                                  strLit);
00209          break;
00210     case OT_LITERAL_CB:
00211          cbLit = oprdPtr->GetLiteralControlBlock();
00212          //#ifdef OLD_OT_LITERAL_CB (inverse of if-then-else)
00213          K->Read ("ir:new-cb-style", newcbstyle);
00214          if (newcbstyle)
00215            lineLength += fprintf(outfile,"b<%d>",cbLit);
00216          //#else
00217          else
00218            lineLength += fprintf(outfile,"cb<%d>",cbLit);
00219          //#endif
00220          break;
00221     case OT_LITERAL_P:
00222          predLit = oprdPtr->GetLiteralPredicate();
00223          if (predLit) { lineLength += fprintf(outfile,"p<t>"); }
00224          else         { lineLength += fprintf(outfile,"p<f>"); }
00225          break;
00226     case OT_REG:
00227          regT = oprdPtr->GetOprdRegType();
00228          regS = oprdPtr->GetOprdRegStyle();
00229          dataT = oprdPtr->GetOprdDataType();
00230          fileT = oprdPtr->GetOprdFileType();
00231          regN = oprdPtr->GetOprdRegNum();
00232          intV = oprdPtr->GetOprdIntValue();
00233          omegaData = oprdPtr->GetOprdOmega();
00234          switch (regT) {
00235            case RT_R:
00236               lineLength += fprintf(outfile,"r<%d",regN);
00237               if (omegaData != 0) { lineLength += fprintf(outfile," %d",omegaData); }
00238               lineLength += fprintf(outfile,":%s %s>",regDTN[dataT],regFTN[fileT]);
00239               break;
00240            case RT_VR:
00241               lineLength += fprintf(outfile,"vr<%d",regN);
00242               if (omegaData != 0) { lineLength += fprintf(outfile," %d",omegaData); }
00243               lineLength += fprintf(outfile,":%s>",regDTN[dataT]);
00244               break;
00245            case RT_MVR:
00246               lineLength += fprintf(outfile,"mvr<%d",regN);
00247               if (omegaData != 0) { lineLength += fprintf(outfile," %d",omegaData); }
00248               lineLength += fprintf(outfile,">");
00249               break;
00250            case RT_FR:
00251               lineLength += fprintf(outfile,"fr<%d",regN);
00252               if (omegaData != 0) { lineLength += fprintf(outfile," %d",omegaData); }
00253               lineLength += fprintf(outfile,":%s %s %c>",regDTN[dataT],regFTN[fileT],regS);
00254               break;
00255            case RT_BR:
00256               lineLength += fprintf(outfile,"br<%d",regN);
00257               if (omegaData != 0) { lineLength += fprintf(outfile," %d",omegaData); }
00258               lineLength += fprintf(outfile,":%s %s %c %d>",
00259                       regDTN[dataT],regFTN[fileT],regS,intV);
00260               break;
00261            default:
00262               LegoNonFatal ("WriteOprd", "Reg type still not found.");
00263               break;
00264         }
00265          break;
00266     case OT_MACRO:
00267  //        fprintf(stderr,"LW(%p-%d)",oprdPtr,oprdPtr->GetOprdRegNum());
00268          wrtMacro = oprdPtr->GetOprdRegNum();
00269          if ( wrtMacro < 255 ) { wrtMacro = 256; }
00270          lineLength += fprintf(outfile,"m<%s>",ParseMap(wrtMacro));
00271          break;
00272     case OT_UNDEFINED:
00273          lineLength += fprintf(outfile,"u<>");
00274          break;
00275     default:
00276          LegoNonFatal ("WriteOprd", "Src or dest operand not found.");
00277          break;
00278   }
00279   return;
00280 }
00281 
00282 /*
00283  * WriteOps (designed by Willie Glover)
00284  *   legoOpPtr = pointer to first legoOp to write
00285  *   singleOp = whether to write just first op or all ops
00286  *   outfile = file to which to write
00287  *   offset = number of spaces to indent from left
00288  *   returns: 1 if writing successful, 0 otherwise
00289  *
00290  * Writes out a single legoOp in REBEL format if singleOp is nonzero,
00291  * otherwise writes out all ops from legoOpPtr until the end of the
00292  * encompassing region.
00293  *
00294  * 9/23/97 WAH:  Added flag writing (!); repairs to attribute writing.
00295  * 10/2/97 WAH:  More repairs to attribute writing.
00296  */
00297 int  WriteOps (legoOp* legoOpPtr,
00298                         int singleOp,
00299                         FILE *outfile,
00300                         int offset=0)
00301 {
00302   char indStr[INDENT_SIZE];       // string to hold record offset
00303   unsigned indSz;        // specifies record offset
00304   unsigned idx;
00305   for (idx = 0; idx<INDENT_SIZE; idx++) { indStr[idx] = '\0'; }
00306   indSz = offset;
00307   for (idx = 0; idx<indSz; idx++) { indStr[idx] = ' '; }
00308 
00309   legoOprd *nextPOprdPtr;
00310  //  fprintf(outfile,"\n");
00311   lineLength = 0;
00312 
00313 /*-----------------------------------------------------------------
00314  * write opid
00315  *-----------------------------------------------------------------*/
00316   int wrtOpId = legoOpPtr->GetOpId();
00317   lineLength += fprintf(outfile,"%sop %d", indStr, wrtOpId);
00318   fflush(outfile);
00319 
00320 /*-----------------------------------------------------------------
00321  * write opcode
00322  *-----------------------------------------------------------------*/
00323   if(legoOpPtr->GetOpcodePtr() == NULL)
00324   {
00325       int wrtOpcode = legoOpPtr->GetOpcode();
00326       lineLength += fprintf(outfile," (%s",ParseMap(wrtOpcode));
00327       fflush(outfile);
00328   }
00329   else
00330   {
00331       //HZ: write the base opcode and the qualifiers
00332      lineLength += fprintf(outfile, "(%s",
00333               legoOpPtr->GetOpcodePtr()->GetOpcString());
00334      fflush(outfile);
00335   }
00336 
00337 /*-----------------------------------------------------------------
00338  *  write destination operands
00339  *
00340  *  Get the first destination operand and write it out.  If the
00341  *     first operand is not NULL, then write out any additional
00342  *     destination operands until the a NULL pointer is reached.
00343  *-----------------------------------------------------------------*/
00344   legoOprd *destPtr = legoOpPtr->GetDestOprdPtr();
00345   lineLength += fprintf(outfile," [");
00346   WriteOprd(destPtr,outfile);
00347   if (destPtr != NULL) {
00348      legoOprd *nextDOprdPtr = destPtr->GetNextOprdPtr();
00349      if (nextDOprdPtr != NULL) {
00350         destPtr = nextDOprdPtr;
00351         lineLength += fprintf(outfile," ");
00352         WriteOprd(destPtr,outfile);
00353         nextDOprdPtr = destPtr->GetNextOprdPtr();
00354         if (nextDOprdPtr != NULL) {
00355            lineLength += fprintf(outfile," ");
00356            WriteOprd(nextDOprdPtr,outfile);
00357         }
00358      }
00359   }
00360     lineLength += fprintf(outfile,"]");
00361   fflush(outfile);
00362 
00363 /*-----------------------------------------------------------------
00364  * write source operands
00365  *
00366  *  Get the first source operand and write it out.  If the
00367  *     first operand is not NULL, then write out any additional
00368  *     source operands until the a NULL pointer is reached.
00369  *-----------------------------------------------------------------*/
00370   legoOprd *srcPtr = legoOpPtr->GetSrcOprdPtr();
00371   lineLength += fprintf(outfile," [");
00372   WriteOprd(srcPtr,outfile);
00373   if (srcPtr != NULL) {
00374      legoOprd *nextSOprdPtr = srcPtr->GetNextOprdPtr();
00375      if (nextSOprdPtr != NULL) {
00376         srcPtr = nextSOprdPtr;
00377         lineLength += fprintf(outfile," ");
00378         WriteOprd(srcPtr,outfile);
00379         nextSOprdPtr = srcPtr->GetNextOprdPtr();
00380         if (nextSOprdPtr != NULL) {
00381            lineLength += fprintf(outfile," ");
00382            WriteOprd(nextSOprdPtr,outfile);
00383            //HZ: add two more possible src oprd
00384            nextSOprdPtr = nextSOprdPtr->GetNextOprdPtr();
00385            if (nextSOprdPtr != NULL) 
00386            {
00387                lineLength += fprintf(outfile," ");
00388                WriteOprd(nextSOprdPtr,outfile);
00389                nextSOprdPtr = nextSOprdPtr->GetNextOprdPtr();
00390                if(nextSOprdPtr != NULL) 
00391                {
00392                    lineLength += fprintf(outfile," ");
00393                    WriteOprd(nextSOprdPtr,outfile);
00394                }
00395            }
00396 
00397         }
00398      }
00399   }
00400   lineLength += fprintf(outfile,"]");
00401   fflush(outfile);
00402 
00403 /*-----------------------------------------------------------------
00404  *  write predicate operand
00405  *
00406  *  Get the first predicate operand and write it out.  If more
00407  *     than one predicate is specified, exit with a error.
00408  *-----------------------------------------------------------------*/
00409   legoOprd *predPtr = legoOpPtr->GetPredOprdPtr();
00410   if (predPtr != NULL) {
00411      lineLength += fprintf(outfile," ");
00412      WriteOprd(predPtr,outfile);
00413      nextPOprdPtr = predPtr->GetNextOprdPtr();
00414      if (nextPOprdPtr != NULL) {
00415         LegoNonFatal ("WriteOps", "Multiple predicate operands for op %d.",
00416                       legoOpPtr->GetOpId());
00417      }
00418   }
00419   if (lineLength > 65) { fprintf(outfile,"\n%s",indStr); lineLength = 0; }
00420   fflush(outfile);
00421 
00422 /*-----------------------------------------------------------------
00423  * write scheduled opcode
00424  *-----------------------------------------------------------------*/
00425   if ( legoOpPtr->GetSchedOpcode() != NULL ) {
00426      lineLength += fprintf(outfile," s_opcode(%s)",
00427                                    legoOpPtr->GetSchedOpcode());
00428      if (lineLength > 65) { fprintf(outfile,"\n%s",indStr); lineLength = 0; }
00429   }
00430   fflush(outfile);
00431 
00432 /*-----------------------------------------------------------------
00433  * write related opcodes
00434  *-----------------------------------------------------------------*/
00435   opList *chkRelOpsPtr = legoOpPtr->GetRelatedOpsPtr();
00436   if (chkRelOpsPtr != NULL) {
00437      int wrtRelOpId = (chkRelOpsPtr->GetOpPtr())->GetOpId();
00438      double wrtRelOpFF = chkRelOpsPtr->GetWeight();
00439      lineLength += fprintf(outfile," related_ops(op-%d(%f)",wrtRelOpId,wrtRelOpFF);
00440      opList *chkRelONPtr = chkRelOpsPtr->GetNextListPtr();
00441      while (chkRelONPtr != NULL) {
00442         chkRelOpsPtr = chkRelONPtr;
00443         wrtRelOpId = (chkRelOpsPtr->GetOpPtr())->GetOpId();
00444         wrtRelOpFF = chkRelOpsPtr->GetWeight();
00445         chkRelONPtr = chkRelOpsPtr->GetNextListPtr();
00446         lineLength += fprintf(outfile," op-%d(%d)",wrtRelOpId,wrtRelOpFF);
00447      }
00448      lineLength += fprintf(outfile,")");
00449   }
00450   if (lineLength > 65) { fprintf(outfile,"\n%s",indStr); lineLength = 0; }
00451   fflush(outfile);
00452 
00453 /*-----------------------------------------------------------------
00454  * write scheduled time
00455  *-----------------------------------------------------------------*/
00456   int wrtSTime = legoOpPtr->GetSchedTime();
00457   if ( wrtSTime > 0 ) {
00458      lineLength += fprintf(outfile," s_time(%d)",wrtSTime);
00459   }
00460   if (lineLength > 65) { fprintf(outfile,"\n%s",indStr); lineLength = 0; }
00461   fflush(outfile);
00462 /*-----------------------------------------------------------------
00463  * write spec
00464  *-----------------------------------------------------------------*/
00465   int wrtSpecul = legoOpPtr->GetSpeculative();
00466   if (wrtSpecul == 1) { lineLength += fprintf(outfile," spec"); }
00467   if (wrtSpecul == 0) { lineLength += fprintf(outfile," nonspec"); }
00468   if (lineLength > 65) { fprintf(outfile,"\n%s",indStr); lineLength = 0; }
00469   fflush(outfile);
00470 
00471 /*-----------------------------------------------------------------
00472  * write attributes for op
00473  *-----------------------------------------------------------------*/
00474    attrList *wrtOpAttrListPtr = legoOpPtr->GetOpAttrListPtr();
00475    if (wrtOpAttrListPtr == NULL) { /* do nothing */ }
00476    else {
00477       lineLength += fprintf(outfile," attr(");
00478       while (wrtOpAttrListPtr != NULL)
00479         {
00480           switch (wrtOpAttrListPtr->GetAttrType())
00481             {
00482             case ATTR_LC:
00483               {
00484                 int tmpAttrId = wrtOpAttrListPtr->GetAttrId();
00485                 if ( tmpAttrId != 0 ) {
00486                   lineLength += fprintf(outfile,"lc ^%d", tmpAttrId);
00487                 } // end if
00488               }
00489               break;
00490             case ATTR_LIVE:
00491               {
00492                 int tmpAttrId = wrtOpAttrListPtr->GetAttrId();
00493                 if ( tmpAttrId != 0 ) {
00494                   lineLength += fprintf(outfile,"live ^%d", tmpAttrId);
00495                 } // end if
00496               }
00497               break;
00498             case ATTR_TBLNAME:
00499               lineLength += fprintf(outfile, "tbl_name(%s)",
00500                                     wrtOpAttrListPtr->GetTableName());
00501               break;
00502             default:
00503               break;
00504             } // end switch
00505           if (lineLength > 65)
00506             { fprintf(outfile,"\n%s",indStr); lineLength = 0; }
00507 
00508           wrtOpAttrListPtr = wrtOpAttrListPtr->GetNextListPtr();
00509           if (wrtOpAttrListPtr != NULL)
00510             lineLength += fprintf(outfile," ");
00511         }
00512       lineLength += fprintf(outfile,")");
00513    }
00514   if (lineLength > 65) { fprintf(outfile,"\n%s",indStr); lineLength = 0; }
00515   fflush(outfile);
00516 
00517 /*-----------------------------------------------------------------
00518  * write flags for op
00519  *-----------------------------------------------------------------*/
00520   flags *wrtOpFlagPtr = legoOpPtr->GetOpFlagPtr();
00521   if (wrtOpFlagPtr == NULL) { /* do nothing */ }
00522   else {
00523     lineLength += fprintf(outfile," flags(");
00524     flags *tmpFlagPtr = wrtOpFlagPtr;
00525     while ( tmpFlagPtr != NULL ) {
00526       if (tmpFlagPtr->GetFlagName() == 0)  // happens if blank flags read in
00527         continue;
00528       lineLength += fprintf(outfile,"%s",ParseMap(tmpFlagPtr->GetFlagName()));
00529       tmpFlagPtr = tmpFlagPtr->GetNextFlagPtr();
00530       if ( tmpFlagPtr != NULL ) { lineLength += fprintf(outfile," "); }
00531     }
00532     lineLength += fprintf(outfile,")");    
00533   }
00534   if (lineLength > 65) { fprintf(outfile,"\n%s",indStr); lineLength = 0; }
00535   fflush(outfile);
00536 
00537 /*-----------------------------------------------------------------
00538  * get in edge info
00539  *-----------------------------------------------------------------*/
00540   opList *wrtListPtr = legoOpPtr->GetInListPtr();
00541   if ( wrtListPtr != NULL ) {
00542     lineLength += fprintf(outfile," in_edges(");
00543     while ( wrtListPtr != NULL ) {
00544        if ( wrtListPtr->GetOpId() == 0 ) { break; }
00545        fprintf(outfile,"op-%d(%d)",wrtListPtr->GetOpId(),
00546                                   (int) wrtListPtr->GetWeight());
00547        wrtListPtr = wrtListPtr->GetNextListPtr();
00548        if ( wrtListPtr != NULL ) {
00549          lineLength += fprintf(outfile," ");
00550        }
00551     }
00552   lineLength += fprintf(outfile,")");
00553   }
00554   fflush(outfile);
00555 
00556 /*-----------------------------------------------------------------
00557  * get out edge info
00558  *-----------------------------------------------------------------*/
00559   wrtListPtr = legoOpPtr->GetOutListPtr();
00560   if ( wrtListPtr != NULL ) {
00561     lineLength += fprintf(outfile," out(");
00562     while ( wrtListPtr != NULL ) {
00563        if ( wrtListPtr->GetOpId() == 0) { break; }
00564        fprintf(outfile,"op-%d(%d)",wrtListPtr->GetOpId(),
00565                                   (int) wrtListPtr->GetWeight());
00566        wrtListPtr = wrtListPtr->GetNextListPtr();
00567        if ( wrtListPtr != NULL ) {
00568          lineLength += fprintf(outfile," ");
00569        }
00570     }
00571   lineLength += fprintf(outfile,")");
00572   }
00573   fflush(outfile);
00574 
00575 /*-----------------------------------------------------------------
00576  * get next op in region
00577  *-----------------------------------------------------------------*/
00578   legoOp *wrtNextOp = legoOpPtr->GetNextLink();
00579 
00580 /*-----------------------------------------------------------------
00581  * write block id
00582  *-----------------------------------------------------------------*/
00583   int wrtBlockId= legoOpPtr->GetBlockId();
00584   if (wrtBlockId != -1) {
00585     lineLength += fprintf(outfile," bb_id(%d)",wrtBlockId);
00586   }
00587   fprintf(outfile,")\n");
00588   fflush(outfile);
00589 
00590 /*-----------------------------------------------------------------
00591  * Call WriteOps recursively until NULL next op
00592  *-----------------------------------------------------------------*/
00593   if ((wrtNextOp != NULL) && (singleOp == 0)) {
00594      if (!WriteOps(wrtNextOp,singleOp,outfile,offset)) { return 0;}
00595   }
00596 
00597   return 1;
00598 } // WriteOps
00599 
00600 /*
00601  * DumpOps (designed by Willie Glover)
00602  *   legoOpPtr = pointer to first legoOp to write
00603  *   returns: 1 if writing successful, 0 otherwise
00604  *
00605  * Calls WriteOps to write all ops from legoOpPtr until the end of the
00606  * encompassing region to standard error.
00607  */
00608 int  DumpOps (legoOp* legoOpPtr)
00609 {
00610   return WriteOps(legoOpPtr,0,stderr);
00611 } // DumpOps
00612 
00613 /*
00614  * Note: LegoWrite is an overloaded function. Three different varieties
00615  * are described below; only the last two have real meat. They will be
00616  * referred to as #1, #2, #3, and #4.
00617 
00618 /*
00619  * LegoWrite (designed by Willie Glover) #1
00620  *   legoRegPtr = pointer to legoRegion to write
00621  *   rblOutFile = name of Rebel file to create
00622  *   returns: 1 always
00623  *
00624  * Writes a legoRegion to the file given by calling LegoWrite #3 with
00625  * offset zero.
00626  */
00627 int  LegoWrite (legoRegion* legoRegPtr, char *rblOutFile)
00628 {
00629   FILE *rblout;
00630 
00631   if (legoRegPtr->GetRegionType() == RT_MODULE)
00632     LegoWrite ((legoModule *) legoRegPtr, rblOutFile);
00633   else
00634     {
00635       /*
00636        * get the REBEL output file
00637        */
00638       rblout = fopen(rblOutFile, "w");
00639       if (!rblout) {
00640         printf("Fatal Error: LegoWrite - output file %s open failed\n",rblOutFile);
00641         exit(2);
00642       }
00643       LegoWrite (legoRegPtr, rblout, 0);
00644       fclose (rblout);
00645     }
00646   return 1;
00647 }
00648 
00649 /*
00650  * LegoWrite (designed by Willie Glover) #2
00651  *  legoRegPtr = pointer to legoRegion to write
00652  *  returns: 1 always
00653  *
00654  * Same as LegoWrite #1, but writes to standard error.
00655  */
00656 int  LegoWrite (legoRegion* legoRegPtr)
00657 {
00658   if (legoRegPtr->GetRegionType() == RT_MODULE)
00659     LegoWrite ((legoModule *) legoRegPtr, NULL);
00660   else
00661     LegoWrite (legoRegPtr, stderr, 0);
00662   return 1;
00663 }
00664 /*
00665  * LegoWrite (designed by Willie Glover) #3
00666  *   legoRegPtr = pointer to legoRegion to write
00667  *   filPtr = file to which to write
00668  *   offset = initial number of spaces to indent from left
00669  *   returns: 1 always
00670  *
00671  * Writes a legoRegion to the file given. Should NOT be called with
00672  * a legoModule.
00673  *
00674  * 2/19/97 WAH: Fixed entry/exit opList writing.
00675  *----------------------------------------------------------------------*/
00676 int  LegoWrite (legoRegion* legoRegPtr, FILE *filPtr, int offset)
00677 {
00678   char indStr[INDENT_SIZE]; // string to hold record offset
00679   unsigned indSz;           // specifies record offset
00680   unsigned idx;
00681   legoProc* procPtr;
00682   legoModule* moduPtr;  // dummy statement
00683   for (idx = 0; idx<INDENT_SIZE; idx++) { indStr[idx] = '\0'; }
00684   indSz = offset;
00685   for (idx = 0; idx<indSz; idx++) { indStr[idx] = ' '; }
00686   int allocatedKnobs;
00687 
00688   if (K == NULL)
00689     {
00690       K = new knobs();
00691       allocatedKnobs = 1;
00692     } // end if
00693   else
00694     allocatedKnobs = 0;
00695 
00696 /*-----------------------------------------------------------------
00697  * get the region type
00698  *-----------------------------------------------------------------*/
00699   enum regionTypes rType = legoRegPtr->GetRegionType();
00700 
00701 /*-----------------------------------------------------------------
00702  * write region type (use map to regionTypes)
00703  *-----------------------------------------------------------------*/
00704   fprintf(filPtr,"%s",indStr);
00705   if (rType == RT_PROC) {
00706      procPtr = (legoProc*)legoRegPtr;
00707      if (procPtr->GetScope() == LIR_GLOBAL) {
00708        fprintf(filPtr,"global ");
00709      } 
00710      else if (procPtr->GetScope() == LIR_GNU_WEAK) {
00711        fprintf(filPtr,"gnu_weak ");
00712      } 
00713   }
00714   fprintf(filPtr,"%s",RegionMap((int)rType));
00715 
00716 /*-----------------------------------------------------------------
00717  * if proc, write procedure name
00718  *-----------------------------------------------------------------*/
00719   if (rType == RT_PROC) {
00720      procPtr = (legoProc*)legoRegPtr;
00721      fprintf(filPtr," %s",procPtr->GetProcName());
00722   }
00723 
00724 /*----------------------------------
00725  * write regionId
00726  *----------------------------------*/
00727    fprintf(filPtr," %d (",legoRegPtr->GetRegionId());
00728    indSz = indSz + INDENT_INC;
00729    for ( ; idx < indSz; idx++) {indStr[idx] = ' ';}
00730    //   indStr[idx-1] = ' ';
00731    fprintf(filPtr,"\n%s",indStr);
00732 /*----------------------------------
00733  * write weight
00734  *----------------------------------*/
00735    //   fprintf(filPtr,"weight(%10.0f)",legoRegPtr->GetWeight());
00736    fprintf(filPtr,"weight(%.0f)",legoRegPtr->GetWeight());
00737    fprintf(filPtr,"\n%s",indStr);
00738 /*----------------------------------
00739  * write entry_ops / exit_ops
00740  *----------------------------------*/
00741    fprintf(filPtr,"entry_ops(");
00742    if ( legoRegPtr->GetEntryOpsPtr() != NULL ) {
00743      opList *tmpoplist = legoRegPtr->GetEntryOpsPtr();
00744      while (tmpoplist != NULL)
00745        {
00746          if (tmpoplist->GetValid() == 0) break;
00747          fprintf(filPtr,"%d",(tmpoplist->GetOpPtr()->GetOpId()));
00748          tmpoplist = tmpoplist->GetNextListPtr();
00749          if (tmpoplist != NULL) { fprintf(filPtr," "); }
00750        } // end while
00751    } // end if
00752    fprintf(filPtr,") ");
00753 
00754    fprintf(filPtr,"exit_ops(");
00755    if ( legoRegPtr->GetExitOpsPtr() != NULL ) {
00756      opList *tmpoplist = legoRegPtr->GetExitOpsPtr();
00757      while (tmpoplist != NULL)
00758        {
00759          if (tmpoplist->GetValid() == 0) break;
00760          fprintf(filPtr,"%d",(tmpoplist->GetOpPtr()->GetOpId()));
00761          tmpoplist = tmpoplist->GetNextListPtr();
00762          if (tmpoplist != NULL) { fprintf(filPtr," "); }
00763        } // end while
00764    } // end if
00765    fprintf(filPtr,")");
00766    fprintf(filPtr,"\n%s",indStr);
00767 
00768 /*----------------------------------
00769  * write entry edges  (proc edges must be purged)
00770  *----------------------------------*/
00771    fprintf(filPtr,"entry_edges(");
00772    edgeList *tmpListPtr = legoRegPtr->GetInEdgesPtr();
00773    while ( tmpListPtr != NULL ) {
00774      if ( tmpListPtr->GetValid() == 0 ) { break; }
00775      fprintf(filPtr,"ctrl ^%d",tmpListPtr->GetEdgeId());
00776      tmpListPtr = tmpListPtr->GetNextListPtr();
00777      if ( tmpListPtr != NULL ) { fprintf(filPtr," "); }
00778    }
00779    fprintf(filPtr,") ");
00780 
00781 /*-----------------------------------------------------------------
00782  * write exit edges
00783  *-----------------------------------------------------------------*/
00784    fprintf(filPtr,"exit_edges(");
00785    tmpListPtr = legoRegPtr->GetOutEdgesPtr();
00786    while ( tmpListPtr != NULL ) {
00787      if ( tmpListPtr->GetValid() == 0 ) { break; }
00788      fprintf(filPtr,"ctrl ^%d",tmpListPtr->GetEdgeId());
00789      tmpListPtr = tmpListPtr->GetNextListPtr();
00790      if ( tmpListPtr != NULL ) { fprintf(filPtr," "); }
00791    }
00792    fprintf(filPtr,")");
00793 
00794 /*-----------------------------------------------------------------
00795  * write end of line and record offset
00796  *-----------------------------------------------------------------*/
00797    fprintf(filPtr,"\n%s",indStr);
00798 
00799 /*-----------------------------------------------------------------
00800  * write flags
00801  *-----------------------------------------------------------------*/
00802    flags *tmpFlagPtr = legoRegPtr->GetFlagPtr();
00803 //   fprintf(stderr,"\n tmpFlagPtr =%p\n",tmpFlagPtr); 
00804 //   fprintf(stderr,"\n tmpFlagPtr->GetFlagName()=%d\n",tmpFlagPtr->GetFlagName()); 
00805 //   fprintf(stderr,"\n ParseMap(tmpFlagPtr->GetFlagName())=%d\n",
00806 //                      ParseMap(tmpFlagPtr->GetFlagName())); 
00807    if ( tmpFlagPtr != NULL )
00808      {
00809        fprintf(filPtr,"flags(");
00810 
00811        while ( tmpFlagPtr != NULL ) {
00812          if (tmpFlagPtr->GetFlagName() == 0)  // happens if blank flags read in
00813            break;
00814          fprintf(filPtr,"%s",ParseMap(tmpFlagPtr->GetFlagName()));
00815          tmpFlagPtr = tmpFlagPtr->GetNextFlagPtr();
00816          if ( tmpFlagPtr != NULL ) { fprintf(filPtr," "); }
00817        }
00818        fprintf(filPtr,")");
00819      }
00820 
00821 /*-----------------------------------------------------------------
00822  * write end of line and record offset
00823  *-----------------------------------------------------------------*/
00824    fprintf(filPtr,"\n%s",indStr);
00825 
00826 /*-----------------------------------------------------------------
00827  * write attributes
00828  *-----------------------------------------------------------------*/
00829    fprintf(filPtr,"attr(");
00830    attrList *wrtRegAttrListPtr = legoRegPtr->GetRegionAttrListPtr();
00831    if (wrtRegAttrListPtr == NULL) { /* do nothing */ }
00832    else {
00833       int tmpAttrId = (legoRegPtr->GetRegionAttrListPtr())->GetAttrId();
00834       if ( tmpAttrId != 0 ) {
00835          fprintf(filPtr,"lc ^%d", tmpAttrId);
00836       }
00837    }
00838    fprintf(filPtr,")");
00839 
00840 /*-----------------------------------------------------------------
00841  * write end of line and record offset
00842  *-----------------------------------------------------------------*/
00843    fprintf(filPtr,"\n%s",indStr);
00844 
00845 /*-----------------------------------------------------------------
00846  * write subregion prefix
00847  *-----------------------------------------------------------------*/
00848    fprintf(filPtr,"subregions(");
00849 
00850 /*-----------------------------------------------------------------
00851  * write end of line and record offset
00852  *-----------------------------------------------------------------*/
00853    //   fprintf(filPtr,"\n%s",indStr);
00854    fprintf(filPtr,"\n");
00855 
00856 /*-----------------------------------------------------------------
00857  * Iterate through all items in set
00858  *-----------------------------------------------------------------*/
00859    int itemCount = legoRegPtr->GetCount();
00860 //   fprintf(stderr,"IN LegoWrite(), itemCount = %d\n",itemCount);
00861 //   fprintf(stderr,"IN LegoWrite(), rType = %d\n",rType);
00862    /*
00863     * WAH: I'll assume for sanity reasons that the first op in an
00864     * op-holding block is the first op in control flow for it too.
00865     */
00866    if (IS_BLOCK(rType) && itemCount > 0)
00867      WriteOps ((legoOp *)legoRegPtr->GetItem(0), 0,
00868                         filPtr, indSz+INDENT_INC);
00869    else
00870      {
00871        for (idx=0; idx<itemCount; idx++) {
00872          if ( (rType == RT_PROC) ||
00873               (rType == RT_LOOPBODY) ||
00874               (rType == RT_LOOP) ||
00875               (rType == RT_TREE) ||
00876               (rType == RT_TRACE) ) {
00877            LegoWrite((legoRegion*)legoRegPtr->GetItem(idx),filPtr,
00878                      indSz+INDENT_INC);
00879          }
00880          else {
00881            //         WriteOps((legoOp*)legoRegPtr->GetItem(idx),
00882            //                           1,filPtr,indSz+INDENT_INC);
00883            LegoNonFatal ("LegoWrite", "Don't know how to write region "
00884                          " of type %d.\n", rType);
00885          }
00886        } // end for
00887      } // end else
00888 
00889 /*-----------------------------------------------------------------
00890  * write record offset
00891  *-----------------------------------------------------------------*/
00892    fprintf(filPtr,"%s",indStr);
00893 
00894 /*-----------------------------------------------------------------
00895  * close subregions
00896  *-----------------------------------------------------------------*/
00897    indSz = indSz - INDENT_INC;
00898    indStr[indSz] = '\0';
00899 
00900    fprintf(filPtr,")\n%s",indStr);
00901 
00902 /*-----------------------------------------------------------------
00903  * close region
00904  *-----------------------------------------------------------------*/
00905    //   fprintf(filPtr,")\n%s",indStr);
00906    fprintf(filPtr,")\n");
00907 
00908 
00909 // ----------------------------------------------------------------
00910 
00911    if (allocatedKnobs)
00912      {
00913        delete K; K = NULL;
00914      } // end if
00915 
00916    return 1;
00917 }
00918 
00919 /*
00920  * LegoWrite (designed by Willie Glover) #4
00921  *   legoModPtr = pointer to legoModule to write
00922  *   rblOutFile = name of file to which to write
00923  *   returns: 1 always
00924  *
00925  * Writes a legoModule to the file named.
00926  */
00927 int  LegoWrite (legoModule* legoModPtr, char *rblOutFile=NULL )
00928 {
00929   FILE *filPtr = NULL;
00930   legoRegion *legoRegPtr;
00931   legoRegion *modRegPtr;
00932   int idx;
00933   vector<char *> *modComments;
00934   vector<char *>::iterator comi;
00935   char *cmdString;
00936   char usegzip;
00937 
00938 //  K = new knobs();
00939 //  K->SetDefaultPanel ("ir");
00940 //  K->Read("usegzip", usegzip);
00941   usegzip = TRUE; 
00942 /*
00943  * get the REBEL output file
00944  */
00945        if ( rblOutFile == NULL ) { filPtr = stdout; }
00946        //if (!filPtr) { filPtr = fopen(rblOutFile, "w"); }
00947        if (!filPtr)
00948        {
00949           if (usegzip) {
00950              // here we write a compressed file
00951             if ((cmdString = (char *)calloc(strlen(rblOutFile)+strlen(COMPRESS_CMD)
00952                                          +8,sizeof(char)))!=NULL)
00953             {
00954                sprintf(cmdString,"%s > %s",COMPRESS_CMD,rblOutFile);
00955                filPtr = popen(cmdString, "w");
00956                free(cmdString);
00957             }
00958             else
00959             {
00960                rblOutFile = NULL; // so we don't call pclose on filPtr
00961             }
00962           }
00963           else {
00964              // just write plain text file
00965              filPtr = fopen(rblOutFile, "w");
00966           }
00967        }
00968        if (!filPtr) {
00969            LegoNonFatal ("LegoWrite", "Output file open failed - writing "
00970                          "to stderr.");
00971            filPtr = stderr;
00972        }
00973        //  char comText[100];     // comment data
00974        //  FILE *comfile;
00975 
00976 /*-----------------------------------------------------------------
00977  * write the comment file
00978  *-----------------------------------------------------------------*/
00979   //  if((comfile = fopen("rbl_com","r")) == NULL) {
00980   //     LegoNonFatal ("LegoWrite", "Can't open comment file.");
00981   //  }
00982   //  else {
00983   //    while ( fgets(comText,sizeof(comText),comfile) != NULL) {
00984   //      if ( comText[3] != '+' )
00985   //        { fprintf(filPtr,"%s",comText); }
00986   //     }
00987   //  }
00988   //  fprintf(filPtr,"\n");
00989   modComments = legoModPtr->GetComments();
00990   for (comi = modComments->begin(); comi != modComments->end(); comi++)
00991       //HZ: add the SMODCMT and EMODCMT to define the start and end of the 
00992       //comment string
00993     fprintf (filPtr, "SMODCMT %s EMODCMT\n", *comi);
00994   fprintf (filPtr, "\n");
00995 /*----------------------------------------------------------------
00996  * search through the set and write all the procs.
00997  *----------------------------------------------------------------*/
00998    modRegPtr = legoModPtr;
00999    WriteData((legoModule*)modRegPtr, filPtr);
01000    int itemCount = modRegPtr->GetCount();
01001    for (idx=0; idx<itemCount; idx++) {
01002          LegoWrite((legoRegion*)modRegPtr->GetItem(idx),filPtr,0);
01003 /*-----------------------------------------------------------------
01004  * Write data segment
01005  *-----------------------------------------------------------------*/
01006 //         WriteData((legoProc*)modRegPtr->GetItem(idx), filPtr);
01007 
01008 /*-----------------------------------------------------------------
01009  * Write edge dictionary
01010  *-----------------------------------------------------------------*/
01011          WriteEdges((legoProc*)modRegPtr->GetItem(idx), filPtr);
01012 
01013 /*-----------------------------------------------------------------
01014  * Write attr dictionary
01015  *-----------------------------------------------------------------*/
01016          WriteAttrs((legoProc*)modRegPtr->GetItem(idx), filPtr);
01017 
01018 /*-----------------------------------------------------------------
01019  * Write memvrs dictionary
01020  *-----------------------------------------------------------------*/
01021          WriteMem((legoProc*)modRegPtr->GetItem(idx), filPtr);
01022 
01023 /*-----------------------------------------------------------------
01024  * Write jump dictionary
01025  *-----------------------------------------------------------------*/
01026          WriteJump((legoProc*)modRegPtr->GetItem(idx), filPtr);
01027    }
01028 
01029    if (usegzip) {
01030       if (rblOutFile != NULL) pclose (filPtr);
01031    }
01032    else {
01033       if (rblOutFile != NULL) fclose (filPtr);
01034    }
01035    //   fclose (comfile);
01036   return 1;
01037 }
01038 

Generated on Mon Jul 21 20:27:43 2003 for TINKER LEGO DOC by doxygen 1.3.2