Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
gen_util.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 #include <stdio.h>
00037 #include "gen_util.h"
00038 
00039 void Emit_Header (FILE *hfile, 
00040                   const char *name,
00041                   const char * const *interface_desc)
00042 {
00043   int i;
00044 
00045   if (interface_desc) {
00046     for (i = 0; interface_desc[i] != NULL; ++i) {
00047       fprintf(hfile, "%s\n", interface_desc[i]);
00048     }
00049   }
00050 
00051   fprintf(hfile, "\n#ifndef %s_INCLUDED\n", name);
00052   fprintf(hfile, "#define %s_INCLUDED\n", name);
00053 
00054   fprintf (hfile, "#ifdef __cplusplus\n"
00055                   "extern \"C\" {\n"
00056                   "#endif\n\n");
00057 
00058   /* Pull in appropriate stuff from common/com/defs.h here, so that
00059    * the header can be used whether defs.h has been included or not.
00060    */
00061   fprintf (hfile, "#ifndef defs_INCLUDED\n"
00062                   "#define defs_INCLUDED\n"
00063                   "typedef signed int INT;\n"
00064                   "typedef signed int INT32;\n"
00065                   "typedef signed long long INT64;\n"
00066                   "typedef signed char mINT8;\n"
00067                   "typedef signed short mINT16;\n"
00068                   "typedef signed int mINT32;\n"
00069                   "typedef signed long long mINT64;\n"
00070                   "typedef unsigned int UINT;\n"
00071                   "typedef unsigned int UINT32;\n"
00072                   "typedef unsigned long long UINT64;\n"
00073                   "typedef unsigned char mUINT8;\n"
00074                   "typedef unsigned short mUINT16;\n"
00075                   "typedef unsigned int mUINT32;\n"
00076                   "typedef unsigned long long mUINT64;\n"
00077                   "typedef int BOOL;\n"
00078                   "typedef unsigned char mBOOL;\n"
00079                   "#ifndef TRUE\n"
00080                   "#define TRUE    ((BOOL) 1)\n"
00081                   "#endif\n"
00082                   "#ifndef FALSE\n"
00083                   "#define FALSE   ((BOOL) 0)\n"
00084                   "#endif\n"
00085                   "#if (defined(_LANGUAGE_C) || defined(__GNUC__)) && !defined(inline)\n"
00086                   "#define inline static __inline\n"
00087                   "#endif\n"
00088                   "#endif\n\n");
00089 }
00090 
00091 void Emit_Footer (FILE *hfile)
00092 {
00093   fprintf (hfile, "\n#ifdef __cplusplus\n"
00094                   "}\n"
00095                   "#endif\n"
00096                   "#endif\n");    
00097 }
00098 
00099 typedef enum {
00100   DK_MACRO
00101 } DEFINITION_KIND;
00102 
00103 typedef struct definition {
00104   DEFINITION_KIND kind;
00105   const char *name;
00106   const char *s;
00107   struct definition *next;
00108 } DEFINITION;
00109 
00110 static DEFINITION *defs;
00111 static DEFINITION *lastdef;
00112 
00113 void Define_Macro (const char *name, const char *def)
00114 {
00115   DEFINITION *newdef = new DEFINITION;
00116   newdef->kind = DK_MACRO;
00117   newdef->name = name;
00118   newdef->s = def;
00119   newdef->next = NULL;
00120   if (defs == NULL) {
00121     defs = newdef;
00122   } else {
00123     lastdef->next = newdef;
00124   }
00125   lastdef = newdef;
00126 }
00127 
00128 void Emit_Definitions (FILE *hfile, const char *prefix)
00129 {
00130   DEFINITION *def;
00131 
00132   if (defs != NULL) fprintf(hfile, "\n");
00133 
00134   for (def = defs; def != NULL; def = def->next) {
00135     int c;
00136     int pos;
00137     const char *s = def->s;
00138     pos = fprintf(hfile, "#define %s%s ", prefix, def->name);
00139     while (pos++ < 40) fputc(' ', hfile);
00140     fprintf(hfile, "(\"");
00141     while (c = *s++) {
00142       if (c == '\\') {
00143         fprintf(hfile, "\\\\");
00144       } else if (c < ' ') {
00145         switch (c) {
00146         case '\n':
00147           fprintf(hfile, "\\n");
00148           break;
00149         case '\t':
00150           fprintf(hfile, "\\t");
00151           break;
00152         default:
00153           fprintf(hfile, "\\%03o", c);
00154           break;
00155         }
00156       } else {
00157         fputc(c, hfile);
00158       }
00159     }
00160     fprintf(hfile, "\")\n");
00161   }
00162 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines