Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
tcon2c.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 /* ====================================================================
00037  * ====================================================================
00038  *
00039  *
00040  * Revision history:
00041  *  07-Nov-94 - Original Version
00042  *
00043  * Description:
00044  *
00045  *    See tcon2c.h for a description of the exported functions and 
00046  *    variables.
00047  *
00048  * ====================================================================
00049  * ====================================================================
00050  */
00051 #ifdef _KEEP_RCS_ID
00052 #endif /* _KEEP_RCS_ID */
00053 
00054 #include "whirl2c_common.h"
00055 #include "tcon2c.h"
00056 #include "alloca.h"
00057 
00058 /*---------------------- Hidden utilities ---------------------*/
00059 /*-------------------------------------------------------------*/
00060   
00061 static char *
00062 Remove_Trailing_Zero_Fraction(char *strbase)
00063 {
00064    /* Expect the input to be of the form: "d.dddde+dd", where a '-' may 
00065     * occur in place of the '+', or the '+' could be omitted.  We view the
00066     * 'e' as any letter.
00067     */
00068    INT last, i;
00069 
00070    /* Get to the first digit from the right, which is non-zero.
00071     */
00072    for (last = 0; strbase[last] != '\0'; last++);
00073    for (i = last-1; strbase[i] == '0'; i--);
00074 
00075    /* Remove any unnecesary exponent part and the trailing zeros in the
00076     * fractional part.
00077     */
00078    if (strbase[i] < '0' || strbase[i] > '9')
00079    {
00080       while (strbase[i] < '0' || strbase[i] > '9') i--;
00081       while (strbase[i] == '0') i--;
00082       if (strbase[i] == '.')
00083       {
00084          strbase[i+1] = '0';
00085          last = i+2;
00086       }
00087       else
00088       {
00089          last = i+1;
00090       }
00091    }
00092    else
00093    {
00094       INT j, remove_to;
00095 
00096       while (strbase[i] >= '0' && strbase[i] <= '9') i--; /* skip exp digits */
00097       while (strbase[i] < '0' || strbase[i] > '9') i--; /* skip exp letters */
00098       remove_to = i;
00099 
00100       while (strbase[i] == '0') i--; /* skip zero digits in the fraction */
00101       if (strbase[i] == '.')
00102          i += 1;
00103 
00104       /* Move exponent part up till just after the non-zero fractional part
00105        */
00106       for (j = remove_to+1; j < last; j++)
00107          strbase[++i] = strbase[j];
00108       last = i+1;
00109    }
00110    strbase[last] = '\0';
00111 
00112    return strbase;
00113 } /* Remove_Trailing_Zero_Fraction */
00114 
00115 
00116 static char *
00117 TCON2C_append_string_char(char *str, char ch)
00118 {
00119   BOOL escape;
00120   char escaped_ch;
00121   
00122   switch (ch)
00123   {
00124   case '\n':
00125      escaped_ch = 'n';
00126      escape = TRUE;
00127      break;
00128   case '\0':
00129      escaped_ch = '0';
00130      escape = TRUE;
00131      break;
00132   case '\t':
00133      escaped_ch = 't';
00134      escape = TRUE;
00135      break;
00136   case '\b':
00137      escaped_ch = 'b';
00138      escape = TRUE;
00139      break;
00140   case '\r':
00141      escaped_ch = 'r';
00142      escape = TRUE;
00143      break;
00144   case '\f':
00145      escaped_ch = 'f';
00146      escape = TRUE;
00147      break;
00148   case '\v':
00149      escaped_ch = 'v';
00150      escape = TRUE;
00151      break;
00152   case '\\':
00153      escaped_ch = '\\';
00154      escape = TRUE;
00155      break;
00156   case '\'':
00157      escaped_ch = '\'';
00158      escape = TRUE;
00159      break;
00160   case '\"':
00161      escaped_ch = '\"';
00162      escape = TRUE;
00163      break;
00164   default: 
00165      escaped_ch = ch;
00166      escape = FALSE;
00167      break;
00168   }
00169   if (escape)
00170      *str++ = '\\';
00171   *str++ = escaped_ch;
00172 
00173   return str;
00174 } /* TCON2C_append_string_char */
00175 
00176 
00177 void 
00178 TCON2C_Append_String_Const(TOKEN_BUFFER tokens, 
00179                            const char  *orig_str, 
00180                            INT32        strlen)
00181 {
00182    const char *str_base;
00183    char       *str;
00184    INT32       stridx;
00185 
00186    str_base = str = (char *)alloca(2*strlen + 3); /* "'", orig_str, "'", and "\0" */
00187    *(str++) = '\"';
00188    for (stridx = 0; stridx < strlen; stridx++)
00189       str = TCON2C_append_string_char(str, orig_str[stridx]);
00190 //   while (str[-1] == '\0') str--;
00191    if (orig_str[strlen-1] == '\0'){
00192        str--;
00193        str--;
00194      }
00195    *(str++) = '\"';
00196    *(str++) = '\0';
00197    Append_Token_String(tokens, str_base);
00198 } /* TCON2C_Append_String_Const */
00199 
00200 
00201 /*---------------------- Exported functions -------------------*/
00202 /*-------------------------------------------------------------*/
00203 
00204 void 
00205 TCON2C_initialize(void)
00206 {
00207    return; /* do nothing for now */
00208 } /* TCON2C_initialize */
00209 
00210 
00211 void 
00212 TCON2C_finalize(void)
00213 {
00214    return; /* do nothing for now */
00215 } /* TCON2C_finalize */
00216 
00217 
00218 void 
00219 TCON2C_translate(TOKEN_BUFFER tokens, TCON tvalue)
00220 {
00221    char        *strbase;
00222    char        *str;
00223    INT32        max_strlen, strlen, stridx;
00224    
00225    switch (TCON_ty(tvalue))
00226    {
00227    case MTYPE_STR:
00228       max_strlen = (Get_Maximum_Linelength()*2)/3;
00229       strlen = Targ_String_Length(tvalue);
00230       strbase = Targ_String_Address(tvalue);
00231       if (max_strlen > 0 && max_strlen < strlen)
00232       {
00233          /* We need to split the string constant into substrings */
00234          str = (char *)alloca(max_strlen + 1);
00235          while (strlen > max_strlen)
00236          {
00237             for (stridx = 0; stridx < max_strlen; stridx++)
00238                str[stridx] = strbase[stridx];
00239             str[stridx] = '\0';
00240             strbase = &strbase[stridx];
00241             strlen -= max_strlen;
00242             TCON2C_Append_String_Const(tokens, str, max_strlen);
00243          }
00244       }
00245       TCON2C_Append_String_Const(tokens, strbase, strlen);
00246       break;
00247 
00248     case MTYPE_I1:
00249     case MTYPE_I2:
00250     case MTYPE_I4:
00251       Append_Token_String(tokens, Targ_Print("%1d", tvalue));
00252       break;
00253       
00254     case MTYPE_U1:
00255     case MTYPE_U2:
00256     case MTYPE_U4:
00257       Append_Token_String(tokens, Targ_Print("%1uU", tvalue));
00258       break;
00259 
00260     case MTYPE_I8:
00261       Append_Token_String(tokens, Targ_Print("%1lldLL", tvalue));
00262       break;
00263 
00264     case MTYPE_U8:
00265       Append_Token_String(tokens, Targ_Print("%1lluULL", tvalue));
00266       break;
00267 
00268     case MTYPE_F4:
00269       str = Targ_Print("%.10e", tvalue);
00270       strbase = Remove_Trailing_Zero_Fraction(str);
00271       /* Undo the 'e'->'d' conversion */
00272       if ((str = strchr(strbase, 'd')) != NULL)
00273          *str = 'e';
00274 
00275       /* Add F suffix */
00276       Append_Token_String(tokens, Concat2_Strings(strbase, "F"));
00277       break;
00278 
00279     case MTYPE_F8:
00280       str = Targ_Print("%.20e", tvalue);
00281       strbase = Remove_Trailing_Zero_Fraction(str);
00282       /* Undo the 'e'->'d' conversion */
00283       if ((str = strchr(strbase, 'd')) != NULL)
00284          *str = 'e';
00285       Append_Token_String(tokens, strbase);
00286       break;
00287 
00288     case MTYPE_FQ:
00289       str = Targ_Print(NULL, tvalue);
00290       strbase = Remove_Trailing_Zero_Fraction(str);
00291       /* Undo the 'e'->'d' conversion */
00292       if ((str = strchr(strbase, 'd')) != NULL)
00293          *str = 'e'; 
00294 
00295       /* Add L suffix */
00296       Append_Token_String(tokens, Concat2_Strings(strbase, "L"));
00297       break;
00298 
00299     case MTYPE_C4:
00300     case MTYPE_C8:
00301     case MTYPE_CQ:
00302       Append_Token_Special(tokens, '{');
00303       TCON2C_translate(tokens, Extract_Complex_Real(tvalue));
00304       Append_Token_Special(tokens, ',');
00305       TCON2C_translate(tokens, Extract_Complex_Imag(tvalue));
00306       Append_Token_Special(tokens, '}');
00307       break;
00308 
00309    default:
00310       /* Only expression nodes should be handled here */
00311       ErrMsg (EC_Invalid_Case, "TCON2C_translate", __LINE__);
00312       Append_Token_String(tokens, "/*quad_constant*/");
00313       break;
00314    } /* switch */
00315 } /* TCON2C_translate */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines