Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
isa_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 // isa_gen.cxx
00038 //
00039 //  Generate an interface to create a new ISA (actually just an enum of
00040 //  all the opcodes).
00041 //    
00043 //
00044 
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <stdarg.h>
00049 
00050 #include "gen_util.h"
00051 #include "isa_gen.h"
00052 
00053 static const char * const interface[] = {
00054   "/* ====================================================================",
00055   " * ====================================================================",
00056   " *",
00057   " * Description:",
00058   " *",
00059   " *   A description of the ISA (actually just an enum of all the opcodes).",
00060   " *   The description exports the following:",
00061   " *",
00062   " *   TOPCODE stands for Target OPCODE; prefix is TOP.",
00063   " *",
00064   " *   typedef (enum) TOP",
00065   " *      Contains all the target opcodes.  Their names have the form",
00066   " *      TOP_<name>.",
00067   " *",
00068   " *   typedef mTOP",
00069   " *      The smallest integer type that can contain all values of a TOP,",
00070   " *      including TOP_UNDEFINED -- useful for conserving space in tables.",
00071   " *",
00072   " *   const TOP TOP_UNDEFINED",
00073   " *      Useful value guaranteed not to be a valid TOP.",
00074   " *",
00075   " *   const int TOP_count",
00076   " *      Gives the number of topcodes.",
00077   " *",
00078   " *   const char* TOP_Name(TOP topcode)",
00079   " *      Returns an assembler style name for the given TOP.",
00080   " *",
00081   " * ====================================================================",
00082   " * ====================================================================",
00083   " */",
00084   NULL
00085 };
00086 
00087 
00089 static char* Dot_To_Line(const char* str)
00091 //  Copy <str> to newly allocated memory, replacing "." with "_" and return
00092 //  the result.
00094 {
00095   char *result = (char*) malloc(strlen(str)+1);
00096   const char *s;
00097   char *r;
00098 
00099   for (s = str, r = result; *s != 0; ++s, ++r) {
00100     if (*s == '.')
00101       *r = '_';
00102     else
00103       *r = *s;
00104   }
00105 
00106   *r = 0;
00107 
00108   return result;
00109 }
00110 
00111 
00113 void ISA_Create (const char *isa_name, ...)
00115 //  Emit the topcode header and c files.
00117 {
00118   FILE* hfile = fopen("topcode.h","w");
00119   FILE* cfile = fopen("topcode.c","w");
00120   FILE* efile = fopen("topcode.Exported","w");
00121   char *instruction_name;
00122   int instruction_count = 0;
00123   va_list ap;
00124 
00125   fprintf(cfile,"#include \"topcode.h\"\n");
00126 
00127   Emit_Header (hfile, "TOPCODE", interface);
00128 
00129   fprintf(hfile,"typedef enum topcode {");
00130   fprintf(cfile,"static const char* const top_names[] = {");
00131 
00132   bool is_first = true;
00133   va_start(ap,isa_name);
00134   while ((instruction_name = va_arg (ap, char *)) != NULL) {
00135     fprintf(hfile,"%s\n  TOP_%s",is_first ? "" : ",",
00136                                  Dot_To_Line(instruction_name));
00137     fprintf(cfile,"%s\n  \"%s\"",is_first ? "" : ",",
00138                                  instruction_name);
00139     if ( is_first )
00140       is_first = false;
00141 
00142     instruction_count++;
00143   }
00144   va_end(ap);
00145 
00146   fprintf(hfile,",\n  TOP_UNDEFINED");
00147   fprintf(cfile,",\n  \"UNDEFINED\"");
00148 
00149   fprintf(hfile,"\n} TOP;\n");
00150   fprintf(cfile,"\n};\n");
00151 
00152   fprintf(hfile,"\ntypedef %s mTOP;\n", 
00153                 (instruction_count+1) <= 256 ? "mUINT8" : "mUINT16");
00154 
00155   fprintf(hfile,"\n#define TOP_count %d\n", instruction_count);
00156 
00157   fprintf(hfile,"\nextern const char* TOP_Name(TOP topcode);\n");
00158   fprintf(efile,"TOP_Name\n");
00159   fprintf(cfile,"\nconst char* TOP_Name(TOP topcode)\n{\n"
00160                 "  return top_names[(int)topcode];\n"
00161                 "}\n");
00162 
00163   Emit_Footer (hfile);
00164 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines