Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
proc_properties_gen.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 //  proc_properties_gen.cxx
00038 //
00039 //  Generate an interface for specifying properties (attributes) for 
00040 //  various processors in the PROC.
00041 //
00043 //
00044 
00045 
00046 #include <stddef.h>
00047 #include <stdlib.h>
00048 #include <stdarg.h>
00049 #include <stdio.h>
00050 #include <assert.h>
00051 
00052 #include <list>
00053 #include <vector>
00054 
00055 using namespace std;
00056 
00057 #include "gen_util.h"
00058 #include "targ_proc.h"
00059 #include "proc_properties_gen.h"
00060 
00061 
00062 struct proc_property {
00063   const char* name;         // Name given for documentation and debugging
00064   int bit_position;         // bit postion in flag word
00065   vector <bool> members;    // set of opcodes that have this property
00066 };
00067 
00068 // Define special bit position values to indicate properties which
00069 // are constant for all processors.
00070 enum {
00071   BIT_ALWAYS_TRUE = -1,
00072   BIT_ALWAYS_FALSE = -2
00073 };
00074 
00075 
00076 static int proc_property_count = 0;  // How many properties?
00077 static list<PROC_PROPERTY> properties; // All the properties
00078 
00079 static const char * const interface[] = {
00080   "/* ====================================================================",
00081   " * ====================================================================",
00082   " *",
00083   " * Description:",
00084   " *",
00085   " *   A description of the properties (attributes) for the processors",
00086   " *   in the PROC. The description exports the following:",
00087   " *",
00088   " *   BOOL PROC_xxx(void)",
00089   " *       Return true/false if PROCESSOR_Value has/does-not-have the",
00090   " *       property 'xxx'.",
00091   " *",
00092   " * ====================================================================",
00093   " * ====================================================================",
00094   " */",
00095   NULL
00096 };
00097 
00098 
00100 void PROC_Properties_Begin( const char* /* name */ )
00102 //  See interface description.
00104 {
00105 }
00106 
00108 PROC_PROPERTY PROC_Property_Create( const char* name )
00110 //  See interface description.
00112 {
00113   PROC_PROPERTY result = new proc_property;
00114 
00115   proc_property_count++;
00116 
00117   result->name = name;
00118   result->members = vector <bool> (PROCESSOR_count, false);
00119 
00120   properties.push_back(result);
00121 
00122   return result;
00123 }
00124 
00126 void Processor_Group( PROC_PROPERTY property, ... )
00128 //  See interface description.
00130 {
00131   va_list ap;
00132   PROCESSOR opcode;
00133 
00134   va_start(ap,property);
00135   while ( (opcode = static_cast<PROCESSOR>(va_arg(ap,int)))
00136           != PROCESSOR_UNDEFINED ) {
00137     property->members[(int)opcode] = true;
00138   }
00139   va_end(ap);
00140 }
00141 
00143 void PROC_Properties_End(void)
00145 //  See interface description.
00147 {
00148   list<PROC_PROPERTY>::iterator isi;
00149   int bit_pos;
00150   char filename[1000];
00151   sprintf (filename, "targ_proc_properties.h");
00152   FILE* hfile = fopen(filename, "w");
00153   sprintf (filename, "targ_proc_properties.c");
00154   FILE* cfile = fopen(filename, "w");
00155   sprintf (filename, "targ_proc_properties.Exported");
00156   FILE* efile = fopen(filename, "w");
00157 
00158   fprintf(cfile,"#include \"targ_proc_properties.h\"\n\n");
00159 
00160   Emit_Header (hfile, "targ_proc_properties", interface);
00161   fprintf(hfile, "#include \"targ_proc.h\"\n");
00162 
00163   // Assign bit positions to all the properties, and note which ones,
00164   // if any, are constant for all processors.
00165   bit_pos = 0;
00166   for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00167     int code;
00168     PROC_PROPERTY property = *isi;
00169     bool all_same = true;
00170     bool first_member = property->members[0];
00171     for (code = 1; code < PROCESSOR_count; code++) {
00172       if (property->members[code] != first_member) {
00173         all_same = false;
00174         break;
00175       }
00176     }
00177 
00178     if (all_same) {
00179       property->bit_position = first_member ? BIT_ALWAYS_TRUE : BIT_ALWAYS_FALSE;
00180     } else {
00181       property->bit_position = bit_pos++;
00182     }
00183   }
00184 
00185   char *int_type;
00186   char *int_suffix;
00187   int int_size;
00188   if (bit_pos <= 8) {
00189     int_type = "mUINT8";
00190     int_suffix = "";
00191     int_size = 8;
00192   } else if (bit_pos <= 16) {
00193     int_type = "mUINT16";
00194     int_suffix = "";
00195     int_size = 16;
00196   } else if (bit_pos <= 32) {
00197     int_type = "mUINT32";
00198     int_suffix = "U";
00199     int_size = 32;
00200   } else {
00201     assert (bit_pos <= 64);
00202     int_type = "mUINT64";
00203     int_suffix = "ULL";
00204     int_size = 64;
00205   }
00206   fprintf (hfile, "\nextern const %s PROC_PROPERTIES_flags[];\n\n", int_type);
00207   fprintf (efile, "PROC_PROPERTIES_flags\n");
00208   fprintf (cfile,"const %s PROC_PROPERTIES_flags[] = {\n", int_type);
00209 
00210   for (int code = 0; code < PROCESSOR_count; code++) {
00211     unsigned long long flag_value = 0;
00212 
00213     for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00214       PROC_PROPERTY property = *isi;
00215       if (property->members[code] && property->bit_position >= 0) {
00216         flag_value |= (1ULL << property->bit_position);
00217       }
00218     }
00219     fprintf (cfile, "  0x%0*llx%s, /* %s:", int_size / 4,
00220                                             flag_value, 
00221                                             int_suffix,
00222                                             PROCESSOR_Name((PROCESSOR)code));
00223     for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00224       PROC_PROPERTY property = *isi;
00225       if (property->members[code] && property->bit_position >= 0) {
00226         fprintf (cfile, " %s", property->name);
00227       }
00228     }
00229     fprintf (cfile, " */\n");
00230   }
00231   fprintf (cfile, "  0x%0*llx%s  /* UNDEFINED */\n"
00232                   "};\n",
00233                   int_size / 4,
00234                   0ULL,
00235                   int_suffix);
00236 
00237   for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00238     PROC_PROPERTY property = *isi;
00239     if (property->bit_position >= 0) {
00240       fprintf (hfile, "#define PROP_%-16s 0x%llx%s\n", 
00241                property->name, 
00242                (1ULL << property->bit_position),
00243                int_suffix);
00244     }
00245   }
00246 
00247   fprintf (hfile, "\n");
00248   for ( isi = properties.begin(); isi != properties.end(); ++isi ) {
00249     PROC_PROPERTY property = *isi;
00250     if (property->bit_position >= 0) {
00251       fprintf (hfile, 
00252                "#define PROC_%s() \\\n"
00253                "  (PROC_PROPERTIES_flags[(INT)PROCESSOR_Value] & PROP_%s)\n",
00254                property->name,
00255                property->name);
00256     } else {
00257       fprintf (hfile, 
00258                "#define PROC_%s() (%d)\n",
00259                property->name,
00260                property->bit_position == BIT_ALWAYS_TRUE ? 1 : 0);
00261     }
00262   }
00263 
00264   Emit_Footer (hfile);
00265 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines