Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
fortout.c
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 
00037 static char USMID[] = "\n@(#)5.0_pl/sources/fortout.c   5.2     05/27/99 10:30:26\n";
00038 
00039 # include "defines.h"           /* Machine dependent ifdefs */
00040 
00041 # include "host.m"              /* Host machine dependent macros.*/
00042 # include "host.h"              /* Host machine dependent header.*/
00043 # include "target.m"            /* Target machine dependent macros.*/
00044 # include "target.h"            /* Target machine dependent header.*/
00045 
00046 # include "globals.m"
00047 # include "tokens.m"
00048 # include "sytb.m"
00049 
00050 # include "globals.h"
00051 # include "tokens.h"
00052 # include "sytb.h"
00053 
00054 static  void print_attr_f(int, FILE *);
00055 static  char start[20];
00056 static  int  start_column;
00057 
00058     
00059 /******************************************************************************\
00060 |*                                                                            *|
00061 |* Description:                                                               *|
00062 |*                                                                            *|
00063 |* Input parameters:                                                          *|
00064 |*      NONE                                                                  *|
00065 |*                                                                            *|
00066 |* Output parameters:                                                         *|
00067 |*      NONE                                                                  *|
00068 |*                                                                            *|
00069 |* Returns:                                                                   *|
00070 |*      NOTHING                                                               *|
00071 |*                                                                            *|
00072 \******************************************************************************/
00073 
00074 void    print_scp_to_fortran(int        ln_fw_idx,
00075                              int        ln_lw_idx,
00076                              int        pgm_attr_idx,
00077                              FILE       *outfile)
00078 
00079 {
00080    int          attr_idx;
00081    int          i;
00082    int          ln_idx;
00083 
00084 
00085    for (i=1; i < 20; i++) start[i]      = '\0';
00086    start[0]     = '\n';
00087    start_column = 1;  /* Points to column containing first NULL. */
00088 
00089    fprintf(outfile, "%s!DIR$ FREE", start);
00090 
00091    print_attr_f(pgm_attr_idx, outfile);
00092 
00093    for (ln_idx = ln_fw_idx; ln_idx <= ln_lw_idx; ln_idx++) {
00094       attr_idx  = LN_ATTR_IDX(ln_idx);
00095 
00096       switch (AT_OBJ_CLASS(attr_idx)) {
00097       case Data_Obj:
00098 
00099          if (ATD_CLASS(attr_idx) != Dummy_Argument) {
00100             print_attr_f(attr_idx, outfile);
00101          }
00102          break;
00103 
00104       case Pgm_Unit:
00105 
00106          if (ATP_IN_INTERFACE_BLK(attr_idx)) {
00107 
00108             if (ATP_IN_UNNAMED_INTERFACE(attr_idx)) {
00109                print_attr_f(attr_idx, outfile);
00110             }
00111          }
00112          else if (attr_idx != pgm_attr_idx) {
00113             print_attr_f(attr_idx, outfile);
00114          }
00115          break;
00116 
00117       case Label:  /* Done during IR printing */
00118          break;
00119 
00120       case Derived_Type:
00121       case Interface:
00122       case Namelist_Grp:
00123       case Stmt_Func:
00124          print_attr_f(attr_idx, outfile);
00125          break;
00126       }
00127    }
00128 
00129    fprintf(outfile, "%sEND\n", start);
00130 
00131    return;
00132 }
00133 
00134     
00135 /******************************************************************************\
00136 |*                                                                            *|
00137 |* Description:                                                               *|
00138 |*                                                                            *|
00139 |* Input parameters:                                                          *|
00140 |*      NONE                                                                  *|
00141 |*                                                                            *|
00142 |* Output parameters:                                                         *|
00143 |*      NONE                                                                  *|
00144 |*                                                                            *|
00145 |* Returns:                                                                   *|
00146 |*      NOTHING                                                               *|
00147 |*                                                                            *|
00148 \******************************************************************************/
00149 
00150 static  void print_attr_f (int           attr_idx,
00151                            FILE         *outfile)
00152 
00153 {
00154    char         *comma;
00155    int           i;
00156    int           len;
00157    int           newlen;
00158    int           num_dargs;
00159    int           save_start_column;
00160    int           sn_idx;
00161 
00162 
00163    switch (AT_OBJ_CLASS(attr_idx)) {
00164    case Data_Obj:
00165 
00166       if (ATD_IN_COMMON(attr_idx) && 
00167           SB_FIRST_ATTR_IDX(ATD_STOR_BLK_IDX(attr_idx)) == attr_idx) {
00168 
00169          /* First attr in the common block - print block */
00170 
00171          if (SB_BLANK_COMMON(ATD_STOR_BLK_IDX(attr_idx))) {
00172             fprintf(outfile, "%sCOMMON // ", start);
00173             fprintf(outfile, "%sCOMMON /%s/ ", start,
00174                              SB_NAME_PTR(ATD_STOR_BLK_IDX(attr_idx)));
00175          }
00176       }
00177 
00178       if (ATD_TYPE_IDX(attr_idx) != NULL_IDX) {
00179          fprintf(outfile, "%s%s :: ", start,
00180                  print_type_f(ATD_TYPE_IDX(attr_idx)));
00181       }
00182       else {
00183          fprintf(outfile, "%s", start);
00184       }
00185       fprintf(outfile, "%s", AT_OBJ_NAME_PTR(attr_idx));
00186       break;
00187 
00188    case Pgm_Unit:
00189       num_dargs = 0;
00190 
00191       switch (ATP_PGM_UNIT(attr_idx)) {
00192       case Pgm_Unknown:
00193          break;
00194 
00195       case Function:
00196       case Subroutine:
00197          fprintf(outfile, "%s", start);
00198          len = start_column - 1;
00199 
00200          if (ATP_IN_UNNAMED_INTERFACE(attr_idx)) {
00201             fprintf(outfile, "INTERFACE ");
00202             save_start_column   = start_column;
00203 
00204             if (start_column <= 18) {
00205                start[start_column++]    = ' ';
00206                start[start_column++]    = ' ';
00207             }
00208             fprintf(outfile, "%s", start);
00209             len = start_column - 1;
00210          }
00211 
00212          if (ATP_RECURSIVE(attr_idx)) {
00213             fprintf(outfile, "RECURSIVE ");
00214             len = 10;
00215          }
00216 
00217          if (ATP_PURE(attr_idx)) {
00218             fprintf(outfile, "PURE ");
00219             len += 5;
00220          }
00221 
00222          if (ATP_ELEMENTAL(attr_idx)) {
00223             fprintf(outfile, "ELEMENTAL ");
00224             len += 10;
00225          }
00226 
00227          if (ATP_PGM_UNIT(attr_idx) == Function) {
00228             fprintf(outfile, "FUNCTION ");
00229             len += 9;
00230          }
00231          else {
00232             fprintf(outfile, "SUBROUTINE ");
00233             len += 11;
00234          }
00235 
00236          fprintf(outfile,"%s(", AT_OBJ_NAME_PTR(attr_idx));
00237          len   += AT_NAME_LEN(attr_idx) + 2;
00238 
00239          if (ATP_EXPL_ITRFC(attr_idx) && ATP_EXTRA_DARG(attr_idx)) {
00240             num_dargs = ATP_NUM_DARGS(attr_idx) - 1;
00241             sn_idx    = ATP_FIRST_IDX(attr_idx) + 1;
00242          }
00243          else {
00244             num_dargs = ATP_NUM_DARGS(attr_idx);
00245             sn_idx    = ATP_FIRST_IDX(attr_idx);
00246          }
00247 
00248          comma  = " ";
00249 
00250          for (i = num_dargs; i > 0; i--) {
00251             newlen      = len + AT_NAME_LEN(SN_ATTR_IDX(sn_idx)) + 1;
00252 
00253             if (newlen > 78) {
00254                fprintf(outfile, "%s &%s    & ", comma, start);
00255                len = 6 + start_column - 1;
00256             }
00257             else {
00258                fprintf(outfile, "%s", comma);
00259                len++;
00260             }
00261 
00262             fprintf(outfile, "%s", AT_OBJ_NAME_PTR(SN_ATTR_IDX(sn_idx)));
00263             len   += AT_NAME_LEN(SN_ATTR_IDX(sn_idx));
00264             sn_idx++;
00265             comma       = ",";
00266          }
00267 
00268          fprintf(outfile, ")");
00269 
00270          if (ATP_RSLT_NAME(attr_idx)) {
00271 
00272             if ((len + 10 + AT_NAME_LEN(ATP_RSLT_IDX(attr_idx))) > 80) {
00273                fprintf(outfile, " &%s    & ", start);
00274             }
00275             fprintf(outfile, " RESULT(%s)", 
00276                              AT_OBJ_NAME_PTR(ATP_RSLT_IDX(attr_idx)));
00277          }
00278    
00279          if (num_dargs > 0) {
00280             sn_idx    = ATP_EXTRA_DARG(attr_idx) ? ATP_FIRST_IDX(attr_idx) + 1:
00281                                                    ATP_FIRST_IDX(attr_idx);
00282 
00283             for (i = ATP_NUM_DARGS(attr_idx); i > 0; i--) {
00284                print_attr_f(SN_ATTR_IDX(sn_idx), outfile);
00285                sn_idx++;
00286             }
00287          }
00288 
00289          if (ATP_PGM_UNIT(attr_idx) == Function) {
00290             fprintf(outfile, "%sEND FUNCTION", start);
00291          }
00292          else {
00293             fprintf(outfile, "%sEND SUBROUTINE", start);
00294          }
00295 
00296          if (ATP_IN_UNNAMED_INTERFACE(attr_idx)) {
00297 
00298            if (save_start_column != start_column) {
00299               start[--start_column]     = '\0';
00300               start[--start_column]     = '\0';
00301            }
00302            fprintf(outfile, "%sEND INTERFACE", start);
00303          }
00304          
00305          break;
00306 
00307       case Program:
00308          fprintf(outfile, "%sPROGRAM %s", start, AT_OBJ_NAME_PTR(attr_idx));
00309          break;
00310 
00311       case Blockdata:
00312          fprintf(outfile, "%sBLOCKDATA %s", start, AT_OBJ_NAME_PTR(attr_idx));
00313 
00314          break;
00315 
00316       case Module:
00317          fprintf(outfile, "%sMODULE %s", start, AT_OBJ_NAME_PTR(attr_idx));
00318          break;
00319       }
00320 
00321       if (ATP_DCL_EXTERNAL(attr_idx)) {
00322          fprintf(outfile, "%sEXTERNAL %s", start, AT_OBJ_NAME_PTR(attr_idx));
00323       }
00324 
00325       if (ATP_STACK_DIR(attr_idx)) {
00326          fprintf(outfile, "%s!DIR$ STACK", start);
00327       }
00328 
00329       if (ATP_SAVE_ALL(attr_idx)) {
00330          fprintf(outfile, "%sSAVE", start);
00331       }
00332 
00333       if (ATP_SYMMETRIC(attr_idx)) {
00334          fprintf(outfile, "%s!DIR$ SYMMETRIC", start);
00335       }
00336 
00337       if (ATP_USES_EREGS(attr_idx)) {
00338          fprintf(outfile, "%s!DIR$ USES_EREGS", start);
00339       }
00340       break;
00341 
00342    case Label:
00343       break;
00344 
00345    case Derived_Type:
00346       sn_idx    = ATT_FIRST_CPNT_IDX(attr_idx);
00347       fprintf(outfile, "%sTYPE :: %s", start, AT_OBJ_NAME_PTR(attr_idx));
00348       save_start_column = start_column;
00349 
00350       if (start_column <= 18) {
00351          start[start_column++]  = ' ';
00352          start[start_column++]  = ' ';
00353       }
00354 
00355       for (i = ATT_NUM_CPNTS(attr_idx); i > 0; i--) {
00356          print_attr_f(SN_ATTR_IDX(sn_idx), outfile);
00357          sn_idx = SN_SIBLING_LINK(sn_idx);
00358       }
00359 
00360       if (save_start_column != start_column) {
00361          start[--start_column]  = '\0';
00362          start[--start_column]  = '\0';
00363       }
00364 
00365       fprintf(outfile, "%sEND TYPE %s", start, AT_OBJ_NAME_PTR(attr_idx));
00366       break;
00367 
00368    case Interface:
00369       sn_idx    = ATI_FIRST_SPECIFIC_IDX(attr_idx);
00370       fprintf(outfile, "%sINTERFACE %s", start,
00371               (ATI_UNNAMED_INTERFACE(attr_idx) ? " " :
00372                AT_OBJ_NAME_PTR(attr_idx)));
00373 
00374       save_start_column = start_column;
00375 
00376       if (start_column <= 18) {
00377          start[start_column++]  = ' ';
00378          start[start_column++]  = ' ';
00379       }
00380 
00381 
00382       for (i = ATI_NUM_SPECIFICS(attr_idx); i > 0; i--) {
00383 
00384          if (ATP_PROC(SN_ATTR_IDX(sn_idx)) == Module_Proc) {
00385             fprintf(outfile, "%sMODULE PROCEDURE %s", start,
00386                     AT_OBJ_NAME_PTR(SN_ATTR_IDX(sn_idx)));
00387          }
00388          else {
00389             print_attr_f(SN_ATTR_IDX(sn_idx), outfile);
00390          }
00391          sn_idx = SN_SIBLING_LINK(sn_idx);
00392       }
00393 
00394       if (save_start_column != start_column) {
00395          start[--start_column]  = '\0';
00396          start[--start_column]  = '\0';
00397       }
00398 
00399       fprintf(outfile, "%sEND INTERFACE", start);
00400 
00401       if (ATI_DCL_INTRINSIC(attr_idx)) {
00402          fprintf(outfile, "%sINTRINSIC :: %s", start, 
00403                  AT_OBJ_NAME_PTR(attr_idx));
00404       }
00405 
00406       break;
00407 
00408    case Namelist_Grp:
00409 
00410       sn_idx    = ATN_FIRST_NAMELIST_IDX(attr_idx);
00411       len       = 80;
00412 
00413       for (i = ATN_NUM_NAMELIST(attr_idx); i > 0; i--) {
00414          newlen = len + AT_NAME_LEN(SN_ATTR_IDX(sn_idx)) + 1;
00415 
00416          if (newlen > 80) {
00417             fprintf(outfile, "%sNAMELIST /%s/ ", start,
00418                     AT_OBJ_NAME_PTR(attr_idx));
00419             len = AT_NAME_LEN(attr_idx) + 12 + start_column - 1;
00420          }
00421          else {
00422             fprintf(outfile, "%s", ",");
00423             len++;
00424          }
00425 
00426          fprintf(outfile, "%s", AT_OBJ_NAME_PTR(SN_ATTR_IDX(sn_idx)));
00427          len   += AT_NAME_LEN(SN_ATTR_IDX(sn_idx));
00428          sn_idx = SN_SIBLING_LINK(sn_idx);
00429       }
00430 
00431 #     ifdef _DEBUG
00432 
00433       if (ATN_NAMELIST_DESC(attr_idx) != NULL_IDX) {
00434 
00435          if (len > 64) {
00436             fprintf(outfile, "%s", start);
00437          }
00438 
00439          fprintf(outfile, "  ! (%s)", 
00440                           AT_OBJ_NAME_PTR(ATN_NAMELIST_DESC(attr_idx)));
00441       }
00442 #     endif
00443       break;
00444 
00445    case Stmt_Func:
00446       break;
00447 
00448    }
00449 
00450    fflush(outfile);
00451    return;
00452 
00453 }  /* print_attr_f */
00454 
00455 /******************************************************************************\
00456 |*                                                                            *|
00457 |* Description:                                                               *|
00458 |*      Print type in a Fortran format.                                       *|
00459 |*                                                                            *|
00460 |* Input parameters:                                                          *|
00461 |*      NONE                                                                  *|
00462 |*                                                                            *|
00463 |* Output parameters:                                                         *|
00464 |*      NONE                                                                  *|
00465 |*                                                                            *|
00466 |* Returns:                                                                   *|
00467 |*      NOTHING                                                               *|
00468 |*                                                                            *|
00469 \******************************************************************************/
00470 
00471 char  *print_type_f(int  type_idx)
00472 
00473 {
00474                 int     kind;
00475    static       char    str[80];
00476                 char    str1[80];
00477 
00478 
00479    if (type_idx == NULL_IDX) {
00480       sprintf(str, "NULL");
00481    }
00482    else if (TYP_TYPE(type_idx) <= Last_Linear_Type) {
00483 
00484       if (TYP_DESC(type_idx) == Star_Typed) {
00485          sprintf(str, "%s * %d", 
00486                        basic_type_str[TYP_TYPE(type_idx)],
00487                        TYP_DCL_VALUE(type_idx));
00488       }
00489       else if (TYP_DESC(type_idx) == Kind_Typed) {
00490          sprintf(str, "%s (kind=%d)", 
00491                        basic_type_str[TYP_TYPE(type_idx)],
00492                        TYP_DCL_VALUE(type_idx));
00493       }
00494       else {  /* Default Typed */
00495 
00496          /* Print a kind type, so we know exactly what we've got. */
00497 
00498          switch (TYP_LINEAR(type_idx)) {
00499          case Integer_1:
00500          case Logical_1:
00501             kind        = 1;
00502             break;
00503          case Integer_2:
00504          case Logical_2:
00505             kind        = 2;
00506             break;
00507          case Integer_4:
00508          case Logical_4:
00509          case Real_4:
00510          case Complex_4:
00511             kind        = 4;
00512             break;
00513          case Integer_8:
00514          case Logical_8:
00515          case Real_8:
00516          case Complex_8:
00517             kind        = 8;
00518             break;
00519          case Real_16:
00520          case Complex_16:
00521             kind        = 16;
00522             break;
00523          default:
00524             kind        = 0;
00525             break;
00526          }
00527 
00528          if (kind == 0) {
00529             sprintf(str, "%s", basic_type_str[TYP_TYPE(type_idx)]);
00530          }
00531          else {
00532             sprintf(str, "%s (%d)", basic_type_str[TYP_TYPE(type_idx)], kind);
00533          }
00534       }
00535    }
00536    else if (TYP_TYPE(type_idx) == Typeless) {
00537       sprintf(str, "Typeless * %s", 
00538                     CONVERT_CVAL_TO_STR((&TYP_BIT_LEN(type_idx)),
00539                                          Integer_8,
00540                                          str1));
00541    }
00542    else if (TYP_TYPE(type_idx) != Character) {
00543       sprintf(str, "type(%s)", AT_OBJ_NAME_PTR(TYP_IDX(type_idx)));
00544    }
00545    else if (TYP_CHAR_CLASS(type_idx) == Assumed_Size_Char) {
00546       sprintf(str, "CHARACTER*(*)");
00547    }
00548    else if (TYP_CHAR_CLASS(type_idx) == Const_Len_Char) {
00549       sprintf(str, "CHARACTER*(%s)",
00550               convert_to_string(&CN_CONST(TYP_IDX(type_idx)),
00551                                  CN_TYPE_IDX(TYP_IDX(type_idx)),
00552                                  str1));
00553    }
00554    else {  /* Variable or unknown length char - print (tmp_idx = idx) */
00555       sprintf(str, "CHARACTER*(%s)", AT_OBJ_NAME_PTR(TYP_IDX(type_idx)));
00556    }
00557 
00558    return(str);
00559 
00560 } /* print_type_f */
00561 
00562 /******************************************************************************\
00563 |*                                                                            *|
00564 |* Description:                                                               *|
00565 |*      Prints a constant from the constant table.                            *|
00566 |*                                                                            *|
00567 |* Input parameters:                                                          *|
00568 |*      NONE                                                                  *|
00569 |*                                                                            *|
00570 |* Output parameters:                                                         *|
00571 |*      NONE                                                                  *|
00572 |*                                                                            *|
00573 |* Returns:                                                                   *|
00574 |*      NOTHING                                                               *|
00575 |*                                                                            *|
00576 \******************************************************************************/
00577 
00578 void  print_const_f(FILE        *outfile,
00579                    int           cn_idx)
00580 
00581 {
00582    long64       i;
00583    long64       length;
00584    int          type_idx;
00585    char         str[80];
00586 
00587 
00588    type_idx = CN_TYPE_IDX(cn_idx);
00589 
00590    switch (TYP_TYPE(type_idx)) {
00591    case Typeless:
00592       convert_to_string_fmt     = Hex_Fmt;
00593       fprintf(outfile, "0x%s", convert_to_string(&CN_CONST(cn_idx), 
00594                                                  type_idx,
00595                                                  str));
00596 
00597       if (TYP_BIT_LEN(type_idx) > TARGET_BITS_PER_WORD) {
00598 
00599          length = (TYP_BIT_LEN(type_idx) + TARGET_BITS_PER_WORD - 1) / 
00600                                            TARGET_BITS_PER_WORD;
00601          for (i = 1; i < length; i++) {
00602             convert_to_string_fmt = Hex_Fmt;
00603             fprintf(outfile, "  %s",
00604                     convert_to_string(&CP_CONSTANT(CN_POOL_IDX(cn_idx)+i),
00605                                       type_idx,
00606                                       str));
00607          }
00608       }
00609 
00610       break;
00611 
00612    case Integer:
00613       fprintf(outfile, "%s", convert_to_string(&CN_CONST(cn_idx),type_idx,str));
00614       break;
00615 
00616    case Real:
00617       fprintf(outfile, "%s", convert_to_string(&CN_CONST(cn_idx),type_idx,str));
00618       break;
00619 
00620    case Character:
00621       fprintf(outfile, "\"%s\"", (char *) &CN_CONST(cn_idx));
00622       break;
00623 
00624    case Logical:
00625       fprintf(outfile, "%s", (THIS_IS_TRUE(&(CN_CONST(cn_idx)), type_idx) ?
00626                               ".TRUE." : ".FALSE."));
00627       break;
00628 
00629    case Complex:
00630       fprintf(outfile, "%s", convert_to_string(&CN_CONST(cn_idx),type_idx,str));
00631       break;
00632    }
00633 
00634    return;
00635 
00636 } /* print_const_f */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines