00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00042
00043
00044
00046
00047
00048
00049
00050
00051
00052 #include <stddef.h>
00053 #include <stdlib.h>
00054 #include <stdarg.h>
00055 #include <stdio.h>
00056 #include <assert.h>
00057 #include <vector>
00058 #include "gen_util.h"
00059 #include "isa_enums_gen.h"
00060
00061 typedef struct {
00062 const char *ecv_ecname;
00063 const char *ecv_name;
00064 int ecv_int;
00065 } ECV_struct;
00066
00067 typedef struct {
00068 const char *ec_name;
00069 int first_ecv;
00070 int last_ecv;
00071 } EC_struct;
00072
00073 static std::vector<ECV_struct> all_ecv;
00074 static std::vector<EC_struct> all_ec;
00075
00076 static const char * const interface[] = {
00077 "/* ====================================================================",
00078 " * ====================================================================",
00079 " *",
00080 " * Description:",
00081 " *",
00082 " * A list of all the enum classes used in an ISA.",
00083 " * It exports the following:",
00084 " *",
00085 " * typedef (enum) ISA_ENUM_CLASS",
00086 " * An enumeration of the enum classes.",
00087 " *",
00088 " * typedef (enum) ISA_ENUM_CLASS_VALUE",
00089 " * An enumeration of the enum class values.",
00090 " *",
00091 " * typedef (struct) ISA_ENUM_CLASS_INFO",
00092 " * Contains info about first and last ECV in the EC.",
00093 " * The contents are private.",
00094 " *",
00095 " * typedef (struct) ISA_ENUM_CLASS_VALUE_INFO",
00096 " * Contains info about name and int-value of the ECV.",
00097 " * The contents are private.",
00098 " *",
00099 " * const char * ISA_EC_Name (ISA_ENUM_CLASS)",
00100 " * Returns name of EC.",
00101 " *",
00102 " * ISA_ENUM_CLASS_VALUE ISA_EC_First_Value (ISA_ENUM_CLASS)",
00103 " * Returns the first ECV for the specified EC.",
00104 " *",
00105 " * ISA_ENUM_CLASS_VALUE ISA_EC_Last_Value (ISA_ENUM_CLASS)",
00106 " * Returns the last ECV for the specified EC.",
00107 " * Note that it assumes all ECV for an EC are in the",
00108 " * first/last range given by the above two functions.",
00109 " *",
00110 " * const char * ISA_ECV_Name (ISA_ENUM_CLASS_VALUE)",
00111 " * Returns name of ECV.",
00112 " *",
00113 " * INT ISA_ECV_Intval (ISA_ENUM_CLASS_VALUE)",
00114 " * Returns int-value of ECV.",
00115 " *",
00116 " * ====================================================================",
00117 " * ====================================================================",
00118 " */",
00119 NULL
00120 };
00121
00123 void ISA_Enums_Begin (void)
00125
00127 {
00128
00129 ECV_struct current_ecv = {"","UNDEFINED",UNDEFINED};
00130 all_ecv.push_back (current_ecv);
00131 EC_struct current_ec = {"UNDEFINED",0,0};
00132 all_ec.push_back (current_ec);
00133 }
00134
00136 void ISA_Create_Enum_Class ( const char* name, ...)
00138
00140 {
00141 const char *ecv_name;
00142 va_list ap;
00143 EC_struct current_ec;
00144 ECV_struct current_ecv;
00145 current_ec.ec_name = name;
00146 current_ec.first_ecv = all_ecv.size();
00147 va_start(ap, name);
00148 do {
00149 ecv_name = va_arg(ap, char*);
00150 current_ecv.ecv_ecname = name;
00151 current_ecv.ecv_name = ecv_name ? ecv_name : "";
00152 current_ecv.ecv_int = va_arg(ap, int);
00153 if (current_ecv.ecv_int == UNDEFINED)
00154 break;
00155 all_ecv.push_back (current_ecv);
00156 } while (ecv_name != NULL);
00157 va_end(ap);
00158 current_ec.last_ecv = all_ecv.size() - 1;
00159 all_ec.push_back (current_ec);
00160 }
00161
00162
00163 static char*
00164 Print_ECV_EName (const char *name)
00165 {
00166
00167 static char buf[80];
00168 char *p = (char*) name;
00169 int i = 0;
00170 if (name == NULL || name[0] == '\0')
00171 return "_none";
00172 else if (name[0] != '\0' && name[0] != '.' && name[0] != '_') {
00173
00174 buf[0] = '_';
00175 ++i;
00176 }
00177 for ( ; *p != '\0'; ++p) {
00178 switch (*p) {
00179 case '.':
00180 buf[i++] = '_';
00181 break;
00182 case '@':
00183
00184 break;
00185 default:
00186 buf[i++] = *p;
00187 break;
00188 }
00189 }
00190 buf[i] = '\0';
00191 return buf;
00192 }
00193
00194 static char*
00195 Print_ECV_Name (ECV_struct ecv)
00196 {
00197 if (ecv.ecv_int == UNDEFINED) {
00198 return "ECV_UNDEFINED";
00199 } else {
00200 static char buf[80];
00201 int i;
00202
00203 i = sprintf(buf, "ECV%s", Print_ECV_EName (ecv.ecv_ecname));
00204 sprintf(buf+i, "%s", Print_ECV_EName(ecv.ecv_name));
00205 return buf;
00206 }
00207 }
00208
00210 void ISA_Enums_End(void)
00212
00214 {
00215 std::vector<EC_struct>::iterator iec;
00216 std::vector<ECV_struct>::iterator iecv;
00217 ECV_struct tecv;
00218
00219 #define FNAME "targ_isa_enums"
00220 char buf[1000];
00221 sprintf (buf, "%s.h", FNAME);
00222 FILE* hfile = fopen(buf, "w");
00223 sprintf (buf, "%s.c", FNAME);
00224 FILE* cfile = fopen(buf, "w");
00225 sprintf (buf, "%s.Exported", FNAME);
00226 FILE* efile = fopen(buf, "w");
00227
00228 fprintf(cfile,"#include \"%s.h\"\n\n", FNAME);
00229
00230 sprintf (buf, "%s", FNAME);
00231 Emit_Header (hfile, buf, interface);
00232
00233 fprintf(hfile, "\ntypedef enum {\n");
00234 for ( iec = all_ec.begin(); iec != all_ec.end(); ++iec) {
00235 fprintf(hfile, "\tEC%s,\n", Print_ECV_EName(iec->ec_name));
00236 }
00237 fprintf(hfile, "\tEC_MAX\n");
00238 fprintf(hfile, "} ISA_ENUM_CLASS;\n");
00239 fprintf(hfile, "\ntypedef enum {\n");
00240 for ( iecv = all_ecv.begin(); iecv != all_ecv.end(); ++iecv) {
00241 tecv = *iecv;
00242 fprintf(hfile, "\t%s,\n", Print_ECV_Name(tecv));
00243 }
00244 fprintf(hfile, "\tECV_MAX\n");
00245 fprintf(hfile, "} ISA_ENUM_CLASS_VALUE;\n");
00246
00247 fprintf(hfile, "\ntypedef struct {\n"
00248 " char *name;\n"
00249 " ISA_ENUM_CLASS_VALUE first;\n"
00250 " ISA_ENUM_CLASS_VALUE last;\n"
00251 "} ISA_ENUM_CLASS_INFO;\n");
00252 fprintf(hfile, "extern const ISA_ENUM_CLASS_INFO ISA_ENUM_CLASS_info[];\n");
00253 fprintf(efile, "ISA_ENUM_CLASS_info\n");
00254 fprintf(cfile, "const ISA_ENUM_CLASS_INFO ISA_ENUM_CLASS_info[] = {\n");
00255 for ( iec = all_ec.begin(); iec != all_ec.end(); ++iec) {
00256 fprintf(cfile, "\t{ \"EC%s\",", Print_ECV_EName(iec->ec_name));
00257 tecv = all_ecv[iec->first_ecv];
00258 fprintf(cfile, "\t%s,", Print_ECV_Name(tecv));
00259 tecv = all_ecv[iec->last_ecv];
00260 fprintf(cfile, "\t%s },\n", Print_ECV_Name(tecv));
00261 }
00262 fprintf(cfile, "};\n\n");
00263
00264 fprintf(hfile, "\ntypedef struct {\n"
00265 " char *name;\n"
00266 " INT intval;\n"
00267 "} ISA_ENUM_CLASS_VALUE_INFO;\n");
00268 fprintf(hfile, "extern const ISA_ENUM_CLASS_VALUE_INFO ISA_ENUM_CLASS_VALUE_info[];\n\n");
00269 fprintf(efile, "ISA_ENUM_CLASS_VALUE_info\n");
00270 fprintf(cfile, "const ISA_ENUM_CLASS_VALUE_INFO ISA_ENUM_CLASS_VALUE_info[] = {\n");
00271 for ( iecv = all_ecv.begin(); iecv != all_ecv.end(); ++iecv) {
00272 fprintf(cfile, "\t{ \"%s\",\t%d },\n", iecv->ecv_name, iecv->ecv_int);
00273 }
00274 fprintf(cfile, "};\n\n");
00275
00276 fprintf(hfile, "inline const char * ISA_EC_Name (ISA_ENUM_CLASS ec)\n"
00277 "{\n"
00278 " return ISA_ENUM_CLASS_info[ec].name;\n"
00279 "}\n\n");
00280
00281 fprintf(hfile, "inline ISA_ENUM_CLASS_VALUE ISA_EC_First_Value (ISA_ENUM_CLASS ec)\n"
00282 "{\n"
00283 " return ISA_ENUM_CLASS_info[ec].first;\n"
00284 "}\n\n");
00285
00286 fprintf(hfile, "inline ISA_ENUM_CLASS_VALUE ISA_EC_Last_Value (ISA_ENUM_CLASS ec)\n"
00287 "{\n"
00288 " return ISA_ENUM_CLASS_info[ec].last;\n"
00289 "}\n\n");
00290
00291 fprintf(hfile, "inline const char * ISA_ECV_Name (ISA_ENUM_CLASS_VALUE ecv)\n"
00292 "{\n"
00293 " return ISA_ENUM_CLASS_VALUE_info[ecv].name;\n"
00294 "}\n\n");
00295
00296 fprintf(hfile, "inline INT ISA_ECV_Intval (ISA_ENUM_CLASS_VALUE ecv)\n"
00297 "{\n"
00298 " return ISA_ENUM_CLASS_VALUE_info[ecv].intval;\n"
00299 "}\n\n");
00300
00301 Emit_Footer (hfile);
00302 }